Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ spirv-builder.workspace = true
members = ["shaders", "shared"]

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

# Compile build-dependencies in release mode with
# the same settings as regular dependencies.
Expand Down
5 changes: 3 additions & 2 deletions shaders/src/filtering_procedurals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//! */
//! ```

use spirv_std::arch::Derivative;
use shared::*;
use spirv_std::glam::{vec2, vec3, vec4, Vec2, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles};

Expand Down Expand Up @@ -400,8 +401,8 @@ impl Inputs {
uvw = tex_coords(pos);

// calc texture sampling footprint
ddx_uvw = uvw + uvw.ddx();
ddy_uvw = uvw + uvw.ddy();
ddx_uvw = uvw + uvw.dfdx();
ddy_uvw = uvw + uvw.dfdy();
}
// shading
let mate: Vec3;
Expand Down
7 changes: 4 additions & 3 deletions shaders/src/skyline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! */
//! ```

use spirv_std::arch::Derivative;
use crate::SampleCube;
use shared::*;
use spirv_std::glam::{vec2, vec3, Vec2, Vec2Swizzles, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles};
Expand Down Expand Up @@ -770,8 +771,8 @@ impl<C0: SampleCube> State<C0> {
let mut window_ref: f32 = 0.0;
// texture map the sides of buildings
if (normal.y < 0.1) && (dist_and_mat.y == 0.0) {
let posdx: Vec3 = pos.ddx();
let posdy: Vec3 = pos.ddy();
let posdx: Vec3 = pos.dfdx();
let posdy: Vec3 = pos.dfdy();
let _pos_grad: Vec3 = posdx * hash21(uv) + posdy * hash21(uv * 7.6543);

// Quincunx antialias the building texture and normal map.
Expand Down Expand Up @@ -930,7 +931,7 @@ impl<C0: SampleCube> State<C0> {
if dist_and_mat.y >= 100.0 {
let mut yfade: f32 = 0.01_f32.max(1.0_f32.min(ref_.y * 100.0));
// low-res way of making lines at the edges of car windows. Not sure I like it.
yfade *= saturate(1.0 - (window_mask.ddx() * window_mask.ddy()).abs() * 250.995);
yfade *= saturate(1.0 - (window_mask.dfdx() * window_mask.dfdy()).abs() * 250.995);
final_color += self.get_env_map_skyline(ref_, self.sun_dir, pos.y - 1.5)
* 0.3
* yfade
Expand Down
105 changes: 2 additions & 103 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use bytemuck::{Pod, Zeroable};
use core::f32::consts::PI;
use core::ops::{Add, Mul, Sub};
use spirv_std::glam::{vec2, vec3, vec4, Vec2, Vec3, Vec3A, Vec4};
use spirv_std::glam::{vec2, vec3, vec4, Vec2, Vec3, Vec4};

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

pub trait Derivative {
fn ddx(self) -> Self;
fn ddx_fine(self) -> Self;
fn ddx_coarse(self) -> Self;
fn ddy(self) -> Self;
fn ddy_fine(self) -> Self;
fn ddy_coarse(self) -> Self;
fn fwidth(self) -> Self;
fn fwidth_fine(self) -> Self;
fn fwidth_coarse(self) -> Self;
}

#[cfg(target_arch = "spirv")]
macro_rules! deriv_caps {
(true) => {
core::arch::asm!("OpCapability DerivativeControl")
};
(false) => {};
}

macro_rules! deriv_fn {
($name:ident, $inst:ident, $needs_caps:tt) => {
fn $name(self) -> Self {
#[cfg(not(target_arch = "spirv"))]
panic!(concat!(stringify!($name), " is not supported on the CPU"));
#[cfg(target_arch = "spirv")]
unsafe {
let mut result = Default::default();
deriv_caps!($needs_caps);
core::arch::asm!(
"%input = OpLoad typeof*{1} {1}",
concat!("%result = ", stringify!($inst), " typeof*{1} %input"),
"OpStore {0} %result",
in(reg) &mut result,
in(reg) &self,
);
result
}
}
};
}
macro_rules! deriv_impl {
($ty:ty) => {
impl Derivative for $ty {
deriv_fn!(ddx, OpDPdx, false);
deriv_fn!(ddx_fine, OpDPdxFine, true);
deriv_fn!(ddx_coarse, OpDPdxCoarse, true);
deriv_fn!(ddy, OpDPdy, false);
deriv_fn!(ddy_fine, OpDPdyFine, true);
deriv_fn!(ddy_coarse, OpDPdyCoarse, true);
deriv_fn!(fwidth, OpFwidth, false);
deriv_fn!(fwidth_fine, OpFwidthFine, true);
deriv_fn!(fwidth_coarse, OpFwidthCoarse, true);
}
};
}

// "must be a scalar or vector of floating-point type. The component width must be 32 bits."
deriv_impl!(f32);
deriv_impl!(Vec2);
deriv_impl!(Vec3A);
deriv_impl!(Vec4);

impl Derivative for Vec3 {
fn ddx(self) -> Self {
Vec3A::from(self).ddx().into()
}
fn ddx_fine(self) -> Self {
Vec3A::from(self).ddx_fine().into()
}
fn ddx_coarse(self) -> Self {
Vec3A::from(self).ddx_coarse().into()
}
fn ddy(self) -> Self {
Vec3A::from(self).ddy().into()
}
fn ddy_fine(self) -> Self {
Vec3A::from(self).ddy_fine().into()
}
fn ddy_coarse(self) -> Self {
Vec3A::from(self).ddy_coarse().into()
}
fn fwidth(self) -> Self {
Vec3A::from(self).fwidth().into()
}
fn fwidth_fine(self) -> Self {
Vec3A::from(self).fwidth_fine().into()
}
fn fwidth_coarse(self) -> Self {
Vec3A::from(self).fwidth_coarse().into()
}
}

pub fn discard() {
#[cfg(target_arch = "spirv")]
unsafe {
use core::arch::asm;
asm!(
"OpExtension \"SPV_EXT_demote_to_helper_invocation\"",
"OpCapability DemoteToHelperInvocationEXT",
"OpDemoteToHelperInvocationEXT"
);
}
unsafe { spirv_std::arch::demote_to_helper_invocation() }
}