Skip to content

Commit

Permalink
Support push constants output in naga_oil composition strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
sytherax authored and Swoorup committed Aug 10, 2024
1 parent 2334515 commit cf33819
Show file tree
Hide file tree
Showing 13 changed files with 1,627 additions and 1,410 deletions.
5 changes: 3 additions & 2 deletions example/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use miette::{IntoDiagnostic, Result};
use wgsl_bindgen::qs::quote;
use wgsl_bindgen::{
GlamWgslTypeMap, Regex, WgslBindgenOptionBuilder, WgslShaderSourceType,
WgslTypeSerializeStrategy,
GlamWgslTypeMap, Regex, WgslBindgenOptionBuilder, WgslShaderIrCapabilities,
WgslShaderSourceType, WgslTypeSerializeStrategy,
};

fn main() -> Result<()> {
Expand All @@ -13,6 +13,7 @@ fn main() -> Result<()> {
.skip_hash_check(true)
.serialization_strategy(WgslTypeSerializeStrategy::Bytemuck)
.type_map(GlamWgslTypeMap)
.ir_capabilities(WgslShaderIrCapabilities::PUSH_CONSTANT)
.override_struct_field_type(
[("utils::types::VectorsU32", "a", quote!(crate::MyTwoU32))].map(Into::into),
)
Expand Down
8 changes: 7 additions & 1 deletion example/shaders/triangle.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ fn vs_main(in: VertexInput) -> VertexOutput {
return out;
}

struct PushConstants {
color_matrix: mat4x4<f32>
}

var<push_constant> constants: PushConstants;

// wgsl outputs with pipeline constants are not supported.
// https://github.com/gfx-rs/wgpu/blob/abba12ae4e5488b08d9e189fc37dab5e1755b443/naga/src/back/wgsl/writer.rs#L108-L113
// override force_black: bool;
Expand All @@ -44,5 +50,5 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
// } else {
// return vec4(color * uniforms.color_rgb.rgb * scale, 1.0);
// }
return vec4(color * uniforms.color_rgb.rgb, 1.0);
return constants.color_matrix * vec4(color * uniforms.color_rgb.rgb, 1.0);
}
21 changes: 19 additions & 2 deletions example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ impl State {
.request_device(
&wgpu::DeviceDescriptor {
label: None,
required_features: wgpu::Features::TEXTURE_COMPRESSION_BC,
required_limits: wgpu::Limits::default(),
required_features: wgpu::Features::TEXTURE_COMPRESSION_BC
| wgpu::Features::PUSH_CONSTANTS,
required_limits: wgpu::Limits {
max_push_constant_size: 128,
..Default::default()
},
memory_hints: Default::default(),
},
None,
)
Expand Down Expand Up @@ -99,6 +104,7 @@ impl State {
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview: None,
cache: Default::default(),
});

// Create a gradient texture.
Expand Down Expand Up @@ -242,6 +248,17 @@ impl State {

render_pass.set_pipeline(&self.pipeline);

// Push constant data also needs to follow alignment rules.
let push_constant = shader_bindings::triangle::PushConstants {
color_matrix: glam::Mat4::IDENTITY,
};

render_pass.set_push_constants(
wgpu::ShaderStages::VERTEX_FRAGMENT,
0,
&bytemuck::cast_slice(&[push_constant]),
);

// Use this function to ensure all bind groups are set.
self.bind_group0.set(&mut render_pass);
self.bind_group1.set(&mut render_pass);
Expand Down
Loading

0 comments on commit cf33819

Please sign in to comment.