Skip to content
This repository was archived by the owner on Oct 31, 2025. It is now read-only.

Commit fcef9ba

Browse files
authored
Implement ddx, ddy, fwidth (#256)
1 parent da0453d commit fcef9ba

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

crates/spirv-std/src/lib.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![no_std]
22
#![feature(register_attr, repr_simd, core_intrinsics)]
3+
#![cfg_attr(target_arch = "spirv", feature(asm))]
34
#![register_attr(spirv)]
45
// Our standard Clippy lints that we use in Embark projects, we opt out of a few that are not appropriate for the specific crate (yet)
56
#![warn(
@@ -97,6 +98,68 @@ pointer_addrspace!(incoming_ray_payload_khr, IncomingRayPayloadKHR, true);
9798
pointer_addrspace!(shader_record_buffer_khr, ShaderRecordBufferKHR, true);
9899
pointer_addrspace!(physical_storage_buffer, PhysicalStorageBuffer, true);
99100

101+
pub trait Derivative {
102+
fn ddx(self) -> Self;
103+
fn ddx_fine(self) -> Self;
104+
fn ddx_coarse(self) -> Self;
105+
fn ddy(self) -> Self;
106+
fn ddy_fine(self) -> Self;
107+
fn ddy_coarse(self) -> Self;
108+
fn fwidth(self) -> Self;
109+
fn fwidth_fine(self) -> Self;
110+
fn fwidth_coarse(self) -> Self;
111+
}
112+
113+
#[cfg(target_arch = "spirv")]
114+
macro_rules! deriv_caps {
115+
(true) => {
116+
asm!("OpCapability DerivativeControl")
117+
};
118+
(false) => {};
119+
}
120+
121+
macro_rules! deriv_fn {
122+
($name:ident, $inst:ident, $needs_caps:tt) => {
123+
fn $name(self) -> Self {
124+
#[cfg(not(target_arch = "spirv"))]
125+
panic!(concat!(stringify!($name), " is not supported on the CPU"));
126+
#[cfg(target_arch = "spirv")]
127+
unsafe {
128+
let o;
129+
deriv_caps!($needs_caps);
130+
asm!(
131+
concat!("{1} = ", stringify!($inst), " typeof{0} {0}"),
132+
in(reg) self,
133+
out(reg) o,
134+
);
135+
o
136+
}
137+
}
138+
};
139+
}
140+
macro_rules! deriv_impl {
141+
($ty:ty) => {
142+
impl Derivative for $ty {
143+
deriv_fn!(ddx, OpDPdx, false);
144+
deriv_fn!(ddx_fine, OpDPdxFine, true);
145+
deriv_fn!(ddx_coarse, OpDPdxCoarse, true);
146+
deriv_fn!(ddy, OpDPdy, false);
147+
deriv_fn!(ddy_fine, OpDPdyFine, true);
148+
deriv_fn!(ddy_coarse, OpDPdyCoarse, true);
149+
deriv_fn!(fwidth, OpFwidth, false);
150+
deriv_fn!(fwidth_fine, OpFwidthFine, true);
151+
deriv_fn!(fwidth_coarse, OpFwidthCoarse, true);
152+
}
153+
};
154+
}
155+
156+
// "must be a scalar or vector of floating-point type. The component width must be 32 bits."
157+
deriv_impl!(f32);
158+
// TODO: Fix rustc to support these
159+
// deriv_impl!(glam::Vec2);
160+
// deriv_impl!(glam::Vec3);
161+
// deriv_impl!(glam::Vec4);
162+
100163
/// libcore requires a few external symbols to be defined:
101164
/// <https://github.com/rust-lang/rust/blob/c2bc344eb23d8c1d18e803b3f1e631cf99926fbb/library/core/src/lib.rs#L23-L27>
102165
/// TODO: This is copied from `compiler_builtins/mem.rs`. Can we use that one instead? The note in the above link says

0 commit comments

Comments
 (0)