Skip to content

Commit

Permalink
WIP feat: add OpImageSampleExplicitLod to spirv-std
Browse files Browse the repository at this point in the history
  • Loading branch information
DeanBDean committed Mar 17, 2021
1 parent 144a447 commit 24062d5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/spirv-builder/src/test/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,28 @@ pub fn main(image: UniformConstant<Image2d>, mut output: Output<glam::Vec4>) {
"#);
}

#[test]
fn image_sample_lod() {
val(r#"
#[spirv(fragment)]
pub fn main(image: UniformConstant<Image2dArray>, sampler: UniformConstant<Sampler>, mut output: Output<glam::Vec4>) {
let result = image.sample_by_lod(*sampler, glam::Vec3A::new(0.0, 0.0, 1.0), 0.0);
*output = result;
}
"#);
}

#[test]
fn image_sample_gradient() {
val(r#"
#[spirv(fragment)]
pub fn main(image: UniformConstant<Image2dArray>, sampler: UniformConstant<Sampler>, mut output: Output<glam::Vec4>) {
let result = image.sample_by_gradient(*sampler, glam::Vec3A::new(0.0, 0.0, 1.0), glam::Vec3A::new(0.0, 0.0, 1.0));
*output = result;
}
"#);
}

/// Helper to generate all of the `ptr_*` tests below, which test that the various
/// ways to use raw pointer `read`/`write`/`copy`, to copy a single value, work,
/// and that the resulting SPIR-V uses either a pair of `OpLoad` and `OpStore`,
Expand Down
44 changes: 44 additions & 0 deletions crates/spirv-std/src/textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,50 @@ impl Image2dArray {
result
}
}
#[spirv_std_macros::gpu_only]
#[cfg(feature = "const-generics")]
pub fn sample_by_lod<V: Vector<f32, 4>>(&self, sampler: Sampler, coordinate: impl Vector<f32, 3>, lod: f32) -> V {
let mut result = Default::default();
unsafe {
asm!(
"%image = OpLoad _ {this}",
"%sampler = OpLoad _ {sampler}",
"%coordinate = OpLoad _ {coordinate}",
"%lod = OpLoad _ {lod}",
"%sampledImage = OpSampledImage _ %image %sampler",
"%result = OpImageSampleExplicitLod _ %sampledImage %coordinate Lod %lod",
"OpStore {result} %result",
result = in(reg) &mut result,
this = in(reg) self,
sampler = in(reg) &sampler,
coordinate = in(reg) &coordinate,
lod = in(reg) &lod
);
}
result
}
#[spirv_std_macros::gpu_only]
#[cfg(feature = "const-generics")]
pub fn sample_by_gradient<V: Vector<f32, 4>>(&self, sampler: Sampler, coordinate: impl Vector<f32, 3>, gradient: impl Vector<f32, 3>) -> V {
let mut result = Default::default();
unsafe {
asm!(
"%image = OpLoad _ {this}",
"%sampler = OpLoad _ {sampler}",
"%coordinate = OpLoad _ {coordinate}",
"%gradient = OpLoad _ {gradient}",
"%sampledImage = OpSampledImage _ %image %sampler",
"%result = OpImageSampleExplicitLod _ %sampledImage %coordinate Grad %gradient",
"OpStore {result} %result",
result = in(reg) &mut result,
this = in(reg) self,
sampler = in(reg) &sampler,
coordinate = in(reg) &coordinate,
gradient = in(reg) &gradient,
);
}
result
}
}

#[spirv(sampled_image)]
Expand Down

0 comments on commit 24062d5

Please sign in to comment.