Skip to content
This repository has been archived by the owner on Jun 18, 2021. It is now read-only.

Commit

Permalink
Implement PUSH_CONSTANTS feature
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Jul 13, 2020
1 parent 5f4f35d commit a670472
Show file tree
Hide file tree
Showing 19 changed files with 214 additions and 81 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ vulkan = ["wgc/gfx-backend-vulkan"]
package = "wgpu-core"
version = "0.5"
git = "https://github.com/gfx-rs/wgpu"
rev = "d7ee89018bed6b2e050b8160b2bd837d89f598a6"
rev = "f67771fb87ce17a7f7e09f9a01149b4132196928"
features = ["raw-window-handle"]

[dependencies.wgt]
package = "wgpu-types"
version = "0.5"
git = "https://github.com/gfx-rs/wgpu"
rev = "d7ee89018bed6b2e050b8160b2bd837d89f598a6"
rev = "f67771fb87ce17a7f7e09f9a01149b4132196928"

[dependencies]
arrayvec = "0.5"
Expand Down
3 changes: 2 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ For the simplest examples without using any helping code (see `framework.rs` her
- `hello-triangle` for graphics and presentation
- `hello-compute` for pure computing

Notably, `capture` example show rendering without a surface/window. It reads back the contents and saves them to a file.
Notably, `capture` example shows rendering without a surface/window. It reads back the contents and saves them to a file.

All framework-based examples render to the window.

Expand All @@ -32,6 +32,7 @@ All framework-based examples render to the window.
| compute passes | :star: | | | | | | | |
| optional extensions | | | | | | | :star: | |
| - binding indexing | | | | | | | :star: | |
| - push constants | | | | | | | :star: | |
| WGSL shaders | | | | | | | | |

## Hacking
Expand Down
2 changes: 2 additions & 0 deletions examples/boids/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ impl framework::Example for Example {
let compute_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&compute_bind_group_layout],
push_constant_ranges: &[],
});

// create render pipeline with empty bind group layout

let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[],
push_constant_ranges: &[],
});

let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
Expand Down
1 change: 1 addition & 0 deletions examples/cube/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl framework::Example for Example {
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});

// Create the texture
Expand Down
7 changes: 6 additions & 1 deletion examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub trait Example: 'static + Sized {
fn needed_features() -> wgpu::Features {
wgpu::Features::empty()
}
fn needed_limits() -> wgpu::Limits {
wgpu::Limits::default()
}
fn init(
sc_desc: &wgpu::SwapChainDescriptor,
device: &wgpu::Device,
Expand Down Expand Up @@ -97,12 +100,14 @@ async fn setup<E: Example>(title: &str) -> Setup {

let adapter_features = adapter.features();

let needed_limits = E::needed_limits();

let trace_dir = std::env::var("WGPU_TRACE");
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
features: adapter_features & needed_features,
limits: wgpu::Limits::default(),
limits: needed_limits,
shader_validation: true,
},
trace_dir.ok().as_ref().map(std::path::Path::new),
Expand Down
1 change: 1 addition & 0 deletions examples/hello-compute/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {

let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});

let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
Expand Down
1 change: 1 addition & 0 deletions examples/hello-triangle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::

let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[],
push_constant_ranges: &[],
});

let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
Expand Down
1 change: 1 addition & 0 deletions examples/hello/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// This example shows how to describe the adapter in use.
async fn run() {
#[cfg_attr(target_arch = "wasm32", allow(unused_variables))]
let adapter = wgpu::Instance::new(wgpu::BackendBit::PRIMARY)
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::Default,
Expand Down
2 changes: 2 additions & 0 deletions examples/mipmap/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl Example {
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});

let vs_module = device.create_shader_module(wgpu::include_spirv!("blit.vert.spv"));
Expand Down Expand Up @@ -249,6 +250,7 @@ impl framework::Example for Example {
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});

// Create the texture
Expand Down
1 change: 1 addition & 0 deletions examples/msaa-line/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl framework::Example for Example {

let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[],
push_constant_ranges: &[],
});

let multisampled_framebuffer =
Expand Down
2 changes: 2 additions & 0 deletions examples/shadow/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ impl framework::Example for Example {
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout, &local_bind_group_layout],
push_constant_ranges: &[],
});

let uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
Expand Down Expand Up @@ -553,6 +554,7 @@ impl framework::Example for Example {
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout, &local_bind_group_layout],
push_constant_ranges: &[],
});

let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
Expand Down
1 change: 1 addition & 0 deletions examples/skybox/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl framework::Example for Skybox {

let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});

// Create the render pipeline
Expand Down
96 changes: 33 additions & 63 deletions examples/texture-arrays/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ struct Uniform {
unsafe impl Pod for Uniform {}
unsafe impl Zeroable for Uniform {}

struct UniformWorkaroundData {
bind_group_layout: wgpu::BindGroupLayout,
bind_group0: wgpu::BindGroup,
bind_group1: wgpu::BindGroup,
}

fn vertex(pos: [i8; 2], tc: [i8; 2], index: i8) -> Vertex {
Vertex {
_pos: [pos[0] as f32, pos[1] as f32],
Expand Down Expand Up @@ -81,7 +75,7 @@ struct Example {
bind_group: wgpu::BindGroup,
vertex_buffer: wgpu::Buffer,
index_buffer: wgpu::Buffer,
uniform_workaround_data: Option<UniformWorkaroundData>,
uniform_workaround: bool,
}

impl framework::Example for Example {
Expand All @@ -90,6 +84,13 @@ impl framework::Example for Example {
| wgpu::Features::SAMPLED_TEXTURE_ARRAY_NON_UNIFORM_INDEXING
| wgpu::Features::SAMPLED_TEXTURE_ARRAY_DYNAMIC_INDEXING
| wgpu::Features::SAMPLED_TEXTURE_BINDING_ARRAY
| wgpu::Features::PUSH_CONSTANTS
}
fn needed_limits() -> wgpu::Limits {
wgpu::Limits {
max_push_constant_size: 4,
..wgpu::Limits::default()
}
}
fn init(
sc_desc: &wgpu::SwapChainDescriptor,
Expand Down Expand Up @@ -131,56 +132,6 @@ impl framework::Example for Example {
let index_buffer = device
.create_buffer_with_data(bytemuck::cast_slice(&index_data), wgpu::BufferUsage::INDEX);

let uniform_workaround_data = if uniform_workaround {
let buffer0 = device.create_buffer_with_data(
&bytemuck::cast_slice(&[Uniform { index: 0 }]),
wgpu::BufferUsage::UNIFORM,
);
let buffer1 = device.create_buffer_with_data(
&bytemuck::cast_slice(&[Uniform { index: 1 }]),
wgpu::BufferUsage::UNIFORM,
);

let bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry::new(
0,
wgpu::ShaderStage::FRAGMENT,
wgpu::BindingType::UniformBuffer {
dynamic: false,
min_binding_size: None,
},
)],
label: Some("uniform workaround bind group layout"),
});

let bind_group0 = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(buffer0.slice(..)),
}],
label: Some("uniform workaround bind group 0"),
});

let bind_group1 = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(buffer1.slice(..)),
}],
label: Some("uniform workaround bind group 1"),
});

Some(UniformWorkaroundData {
bind_group_layout,
bind_group0,
bind_group1,
})
} else {
None
};

let red_texture_data = create_texture_data(Color::RED);
let green_texture_data = create_texture_data(Color::GREEN);

Expand Down Expand Up @@ -289,13 +240,18 @@ impl framework::Example for Example {
label: Some("bind group"),
});

let pipeline_layout = if let Some(ref workaround) = uniform_workaround_data {
let pipeline_layout = if uniform_workaround {
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout, &workaround.bind_group_layout],
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[wgpu::PushConstantRange {
stages: wgpu::ShaderStage::FRAGMENT,
range: 0..4,
}],
})
} else {
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
})
};

Expand Down Expand Up @@ -342,7 +298,7 @@ impl framework::Example for Example {
index_buffer,
bind_group,
pipeline,
uniform_workaround_data,
uniform_workaround,
}
}
fn resize(
Expand Down Expand Up @@ -378,14 +334,28 @@ impl framework::Example for Example {
}],
depth_stencil_attachment: None,
});

let uniform_workaround_data = if self.uniform_workaround {
Some([Uniform { index: 0 }, Uniform { index: 1 }])
} else {
None
};
rpass.set_pipeline(&self.pipeline);
rpass.set_bind_group(0, &self.bind_group, &[]);
rpass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
rpass.set_index_buffer(self.index_buffer.slice(..));
if let Some(ref workaround) = self.uniform_workaround_data {
rpass.set_bind_group(1, &workaround.bind_group0, &[]);
if let Some(ref data) = uniform_workaround_data {
rpass.set_push_constants(
wgpu::ShaderStage::FRAGMENT,
0,
bytemuck::cast_slice(&data[0..1]),
);
rpass.draw_indexed(0..6, 0, 0..1);
rpass.set_bind_group(1, &workaround.bind_group1, &[]);
rpass.set_push_constants(
wgpu::ShaderStage::FRAGMENT,
0,
bytemuck::cast_slice(&data[1..2]),
);
rpass.draw_indexed(6..12, 0, 0..1);
} else {
rpass.draw_indexed(0..12, 0, 0..1);
Expand Down
2 changes: 1 addition & 1 deletion examples/texture-arrays/uniform.frag
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ layout(location = 0) out vec4 o_Color;

layout(set = 0, binding = 0) uniform texture2D u_Textures[2];
layout(set = 0, binding = 1) uniform sampler u_Sampler;
layout(set = 1, binding = 0) uniform Uniforms {
layout(push_constant) uniform Uniforms {
int u_Index; // dynamically uniform
};

Expand Down
Binary file modified examples/texture-arrays/uniform.frag.spv
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/water/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,13 @@ impl framework::Example for Example {
let water_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&water_bind_group_layout],
push_constant_ranges: &[],
});

let terrain_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&terrain_bind_group_layout],
push_constant_ranges: &[],
});

let water_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
Expand Down
Loading

0 comments on commit a670472

Please sign in to comment.