Skip to content

Commit

Permalink
Indirect cube
Browse files Browse the repository at this point in the history
  • Loading branch information
FredrikNoren committed Nov 2, 2021
1 parent ca7ca74 commit 7da68cb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
3 changes: 2 additions & 1 deletion wgpu-hal/src/metal/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,8 @@ impl super::PrivateCapabilities {
| F::VERTEX_WRITABLE_STORAGE
| F::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
| F::POLYGON_MODE_LINE
| F::CLEAR_COMMANDS;
| F::CLEAR_COMMANDS
| F::MULTI_DRAW_INDIRECT;

features.set(F::DEPTH_CLAMPING, self.supports_depth_clamping);

Expand Down
36 changes: 35 additions & 1 deletion wgpu/examples/cube/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ fn create_texels(size: usize) -> Vec<u8> {
.collect()
}

#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct DrawIndexedIndirect {
vertex_count: u32,
instance_count: u32,
base_index: u32,
vertex_offset: i32,
base_instance: u32,
}

struct Example {
vertex_buf: wgpu::Buffer,
index_buf: wgpu::Buffer,
Expand All @@ -91,6 +101,7 @@ struct Example {
uniform_buf: wgpu::Buffer,
pipeline: wgpu::RenderPipeline,
pipeline_wire: Option<wgpu::RenderPipeline>,
commands: wgpu::Buffer,
}

impl Example {
Expand All @@ -110,6 +121,9 @@ impl framework::Example for Example {
fn optional_features() -> wgt::Features {
wgt::Features::POLYGON_MODE_LINE
}
fn required_features() -> wgpu::Features {
wgt::Features::MULTI_DRAW_INDIRECT
}

fn init(
config: &wgpu::SurfaceConfiguration,
Expand All @@ -133,6 +147,24 @@ impl framework::Example for Example {
usage: wgpu::BufferUsages::INDEX,
});

let commands = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Commands Buffer"),
contents: bytemuck::cast_slice(&[DrawIndexedIndirect {
vertex_count: index_data.len() as u32,
instance_count: 1,
base_index: 0,
vertex_offset: 0,
base_instance: 0,
}, DrawIndexedIndirect {
vertex_count: index_data.len() as u32,
instance_count: 1,
base_index: 0,
vertex_offset: 0,
base_instance: 1,
}]),
usage: wgpu::BufferUsages::INDIRECT,
});

// Create pipeline layout
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
Expand Down Expand Up @@ -310,6 +342,7 @@ impl framework::Example for Example {
uniform_buf,
pipeline,
pipeline_wire,
commands,
}
}

Expand Down Expand Up @@ -362,7 +395,8 @@ impl framework::Example for Example {
rpass.set_vertex_buffer(0, self.vertex_buf.slice(..));
rpass.pop_debug_group();
rpass.insert_debug_marker("Draw!");
rpass.draw_indexed(0..self.index_count as u32, 0, 0..1);
rpass.multi_draw_indexed_indirect(&self.commands, 0, 2);
// rpass.draw_indexed(0..self.index_count as u32, 0, 0..1);
if let Some(ref pipe) = self.pipeline_wire {
rpass.set_pipeline(pipe);
rpass.draw_indexed(0..self.index_count as u32, 0, 0..1);
Expand Down
3 changes: 2 additions & 1 deletion wgpu/examples/cube/shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ var<uniform> r_locals: Locals;
fn vs_main(
[[location(0)]] position: vec4<f32>,
[[location(1)]] tex_coord: vec2<f32>,
[[builtin(instance_index)]] instance_index: u32
) -> VertexOutput {
var out: VertexOutput;
out.tex_coord = tex_coord;
out.position = r_locals.transform * position;
out.position = r_locals.transform * (position + vec4<f32>(f32(instance_index) * 2.5, 0., 0., 0.));
return out;
}

Expand Down

0 comments on commit 7da68cb

Please sign in to comment.