Skip to content

Commit

Permalink
expose push constant stages, closes #70
Browse files Browse the repository at this point in the history
  • Loading branch information
ScanMountGoat committed Nov 11, 2024
1 parent 155cd64 commit 1b0e651
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
3 changes: 1 addition & 2 deletions example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,8 @@ impl State {
color_matrix: glam::Mat4::IDENTITY,
})
.unwrap();
// TODO: Expose accessed stages for push constants in generated code.
render_pass.set_push_constants(
wgpu::ShaderStages::FRAGMENT,
shader::PUSH_CONSTANT_STAGES,
0,
&push_constant_bytes.into_inner(),
);
Expand Down
3 changes: 2 additions & 1 deletion example/src/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ pub fn create_shader_module(device: &wgpu::Device) -> wgpu::ShaderModule {
source: wgpu::ShaderSource::Wgsl(source),
})
}
pub const PUSH_CONSTANT_STAGES: wgpu::ShaderStages = wgpu::ShaderStages::FRAGMENT;
pub fn create_pipeline_layout(device: &wgpu::Device) -> wgpu::PipelineLayout {
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
Expand All @@ -267,7 +268,7 @@ pub fn create_pipeline_layout(device: &wgpu::Device) -> wgpu::PipelineLayout {
&bind_groups::BindGroup1::get_bind_group_layout(device),
],
push_constant_ranges: &[wgpu::PushConstantRange {
stages: wgpu::ShaderStages::FRAGMENT,
stages: PUSH_CONSTANT_STAGES,
range: 0..64,
}],
})
Expand Down
32 changes: 22 additions & 10 deletions wgsl_to_wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ fn create_shader_module_inner(
})
.collect();

let push_constant_range = push_constant_range(&module, &global_stages, entry_stages);
let (push_constant_range, push_constant_stages) =
push_constant_range_stages(&module, &global_stages, entry_stages).unzip();

let create_pipeline_layout = quote! {
pub fn create_pipeline_layout(device: &wgpu::Device) -> wgpu::PipelineLayout {
Expand All @@ -264,6 +265,12 @@ fn create_shader_module_inner(

let override_constants = pipeline_overridable_constants(&module);

let push_constant_stages = push_constant_stages.map(|stages| {
quote! {
pub const PUSH_CONSTANT_STAGES: wgpu::ShaderStages = #stages;
}
});

let output = quote! {
#structs
#(#consts)*
Expand All @@ -275,6 +282,7 @@ fn create_shader_module_inner(
#vertex_states
#fragment_states
#create_shader_module
#push_constant_stages
#create_pipeline_layout
};

Expand All @@ -285,11 +293,11 @@ fn create_shader_module_inner(
}
}

fn push_constant_range(
fn push_constant_range_stages(
module: &naga::Module,
global_stages: &BTreeMap<String, wgpu::ShaderStages>,
entry_stages: wgpu::ShaderStages,
) -> Option<TokenStream> {
) -> Option<(TokenStream, TokenStream)> {
// Assume only one variable is used with var<push_constant> in WGSL.
let (_, global) = module
.global_variables
Expand All @@ -311,12 +319,15 @@ fn push_constant_range(
// Use a single push constant range for all shader stages.
// This allows easily setting push constants in a single call with offset 0.
let size = Literal::usize_unsuffixed(push_constant_size as usize);
Some(quote! {
wgpu::PushConstantRange {
stages: #stages,
range: 0..#size
}
})
Some((
quote! {
wgpu::PushConstantRange {
stages: PUSH_CONSTANT_STAGES,
range: 0..#size
}
},
stages,
))
}

fn pretty_print(output: TokenStream) -> String {
Expand Down Expand Up @@ -511,6 +522,7 @@ mod test {
source: wgpu::ShaderSource::Wgsl(source),
})
}
pub const PUSH_CONSTANT_STAGES: wgpu::ShaderStages = wgpu::ShaderStages::FRAGMENT;
pub fn create_pipeline_layout(device: &wgpu::Device) -> wgpu::PipelineLayout {
device
.create_pipeline_layout(
Expand All @@ -519,7 +531,7 @@ mod test {
bind_group_layouts: &[],
push_constant_ranges: &[
wgpu::PushConstantRange {
stages: wgpu::ShaderStages::FRAGMENT,
stages: PUSH_CONSTANT_STAGES,
range: 0..16,
},
],
Expand Down

0 comments on commit 1b0e651

Please sign in to comment.