Skip to content

Commit 0739db6

Browse files
committed
use spirv_std: update rust-gpu, use new derivatives
1 parent 18e3243 commit 0739db6

File tree

5 files changed

+16
-107
lines changed

5 files changed

+16
-107
lines changed

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ spirv-builder.workspace = true
3232
members = ["shaders", "shared"]
3333

3434
[workspace.dependencies]
35-
spirv-builder = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "ac0c7035d53ae0bf87fbff12cc8ad4e6f6628834", default-features = false }
36-
spirv-std = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "ac0c7035d53ae0bf87fbff12cc8ad4e6f6628834" }
35+
spirv-builder = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "0b37696e9f5edde8fa0c1363a88e6c8cb8e6ff68", default-features = false }
36+
spirv-std = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "0b37696e9f5edde8fa0c1363a88e6c8cb8e6ff68" }
3737

3838
# Compile build-dependencies in release mode with
3939
# the same settings as regular dependencies.

shaders/src/filtering_procedurals.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! */
2020
//! ```
2121
22+
use spirv_std::arch::Derivative;
2223
use shared::*;
2324
use spirv_std::glam::{vec2, vec3, vec4, Vec2, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles};
2425

@@ -400,8 +401,8 @@ impl Inputs {
400401
uvw = tex_coords(pos);
401402

402403
// calc texture sampling footprint
403-
ddx_uvw = uvw + uvw.ddx();
404-
ddy_uvw = uvw + uvw.ddy();
404+
ddx_uvw = uvw + uvw.dfdx();
405+
ddy_uvw = uvw + uvw.dfdy();
405406
}
406407
// shading
407408
let mate: Vec3;

shaders/src/skyline.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! */
1212
//! ```
1313
14+
use spirv_std::arch::Derivative;
1415
use crate::SampleCube;
1516
use shared::*;
1617
use spirv_std::glam::{vec2, vec3, Vec2, Vec2Swizzles, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles};
@@ -770,8 +771,8 @@ impl<C0: SampleCube> State<C0> {
770771
let mut window_ref: f32 = 0.0;
771772
// texture map the sides of buildings
772773
if (normal.y < 0.1) && (dist_and_mat.y == 0.0) {
773-
let posdx: Vec3 = pos.ddx();
774-
let posdy: Vec3 = pos.ddy();
774+
let posdx: Vec3 = pos.dfdx();
775+
let posdy: Vec3 = pos.dfdy();
775776
let _pos_grad: Vec3 = posdx * hash21(uv) + posdy * hash21(uv * 7.6543);
776777

777778
// Quincunx antialias the building texture and normal map.
@@ -930,7 +931,7 @@ impl<C0: SampleCube> State<C0> {
930931
if dist_and_mat.y >= 100.0 {
931932
let mut yfade: f32 = 0.01_f32.max(1.0_f32.min(ref_.y * 100.0));
932933
// low-res way of making lines at the edges of car windows. Not sure I like it.
933-
yfade *= saturate(1.0 - (window_mask.ddx() * window_mask.ddy()).abs() * 250.995);
934+
yfade *= saturate(1.0 - (window_mask.dfdx() * window_mask.dfdy()).abs() * 250.995);
934935
final_color += self.get_env_map_skyline(ref_, self.sun_dir, pos.y - 1.5)
935936
* 0.3
936937
* yfade

shared/src/lib.rs

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use bytemuck::{Pod, Zeroable};
77
use core::f32::consts::PI;
88
use core::ops::{Add, Mul, Sub};
9-
use spirv_std::glam::{vec2, vec3, vec4, Vec2, Vec3, Vec3A, Vec4};
9+
use spirv_std::glam::{vec2, vec3, vec4, Vec2, Vec3, Vec4};
1010

1111
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
1212
// we tie #[no_std] above to the same condition, so it's fine.
@@ -350,99 +350,6 @@ impl VecExt for Vec4 {
350350
}
351351
}
352352

353-
pub trait Derivative {
354-
fn ddx(self) -> Self;
355-
fn ddx_fine(self) -> Self;
356-
fn ddx_coarse(self) -> Self;
357-
fn ddy(self) -> Self;
358-
fn ddy_fine(self) -> Self;
359-
fn ddy_coarse(self) -> Self;
360-
fn fwidth(self) -> Self;
361-
fn fwidth_fine(self) -> Self;
362-
fn fwidth_coarse(self) -> Self;
363-
}
364-
365-
#[cfg(target_arch = "spirv")]
366-
macro_rules! deriv_caps {
367-
(true) => {
368-
core::arch::asm!("OpCapability DerivativeControl")
369-
};
370-
(false) => {};
371-
}
372-
373-
macro_rules! deriv_fn {
374-
($name:ident, $inst:ident, $needs_caps:tt) => {
375-
fn $name(self) -> Self {
376-
#[cfg(not(target_arch = "spirv"))]
377-
panic!(concat!(stringify!($name), " is not supported on the CPU"));
378-
#[cfg(target_arch = "spirv")]
379-
unsafe {
380-
let mut result = Default::default();
381-
deriv_caps!($needs_caps);
382-
core::arch::asm!(
383-
"%input = OpLoad typeof*{1} {1}",
384-
concat!("%result = ", stringify!($inst), " typeof*{1} %input"),
385-
"OpStore {0} %result",
386-
in(reg) &mut result,
387-
in(reg) &self,
388-
);
389-
result
390-
}
391-
}
392-
};
393-
}
394-
macro_rules! deriv_impl {
395-
($ty:ty) => {
396-
impl Derivative for $ty {
397-
deriv_fn!(ddx, OpDPdx, false);
398-
deriv_fn!(ddx_fine, OpDPdxFine, true);
399-
deriv_fn!(ddx_coarse, OpDPdxCoarse, true);
400-
deriv_fn!(ddy, OpDPdy, false);
401-
deriv_fn!(ddy_fine, OpDPdyFine, true);
402-
deriv_fn!(ddy_coarse, OpDPdyCoarse, true);
403-
deriv_fn!(fwidth, OpFwidth, false);
404-
deriv_fn!(fwidth_fine, OpFwidthFine, true);
405-
deriv_fn!(fwidth_coarse, OpFwidthCoarse, true);
406-
}
407-
};
408-
}
409-
410-
// "must be a scalar or vector of floating-point type. The component width must be 32 bits."
411-
deriv_impl!(f32);
412-
deriv_impl!(Vec2);
413-
deriv_impl!(Vec3A);
414-
deriv_impl!(Vec4);
415-
416-
impl Derivative for Vec3 {
417-
fn ddx(self) -> Self {
418-
Vec3A::from(self).ddx().into()
419-
}
420-
fn ddx_fine(self) -> Self {
421-
Vec3A::from(self).ddx_fine().into()
422-
}
423-
fn ddx_coarse(self) -> Self {
424-
Vec3A::from(self).ddx_coarse().into()
425-
}
426-
fn ddy(self) -> Self {
427-
Vec3A::from(self).ddy().into()
428-
}
429-
fn ddy_fine(self) -> Self {
430-
Vec3A::from(self).ddy_fine().into()
431-
}
432-
fn ddy_coarse(self) -> Self {
433-
Vec3A::from(self).ddy_coarse().into()
434-
}
435-
fn fwidth(self) -> Self {
436-
Vec3A::from(self).fwidth().into()
437-
}
438-
fn fwidth_fine(self) -> Self {
439-
Vec3A::from(self).fwidth_fine().into()
440-
}
441-
fn fwidth_coarse(self) -> Self {
442-
Vec3A::from(self).fwidth_coarse().into()
443-
}
444-
}
445-
446353
pub fn discard() {
447354
unsafe { spirv_std::arch::demote_to_helper_invocation() }
448355
}

0 commit comments

Comments
 (0)