Render colored quads
This commit is contained in:
parent
70c17b053b
commit
c9510db551
13 changed files with 396 additions and 6 deletions
269
wgpu/src/quad.rs
Normal file
269
wgpu/src/quad.rs
Normal file
|
|
@ -0,0 +1,269 @@
|
|||
use crate::Transformation;
|
||||
|
||||
use std::mem;
|
||||
|
||||
pub struct Pipeline {
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
constants: wgpu::BindGroup,
|
||||
transform: wgpu::Buffer,
|
||||
vertices: wgpu::Buffer,
|
||||
indices: wgpu::Buffer,
|
||||
instances: wgpu::Buffer,
|
||||
}
|
||||
|
||||
impl Pipeline {
|
||||
pub fn new(device: &mut wgpu::Device) -> Pipeline {
|
||||
let constant_layout =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
bindings: &[wgpu::BindGroupLayoutBinding {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStage::VERTEX,
|
||||
ty: wgpu::BindingType::UniformBuffer { dynamic: false },
|
||||
}],
|
||||
});
|
||||
|
||||
let matrix: [f32; 16] = Transformation::identity().into();
|
||||
|
||||
let transform = device
|
||||
.create_buffer_mapped(
|
||||
16,
|
||||
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
||||
)
|
||||
.fill_from_slice(&matrix[..]);
|
||||
|
||||
let constants = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &constant_layout,
|
||||
bindings: &[wgpu::Binding {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::Buffer {
|
||||
buffer: &transform,
|
||||
range: 0..64,
|
||||
},
|
||||
}],
|
||||
});
|
||||
|
||||
let layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
bind_group_layouts: &[&constant_layout],
|
||||
});
|
||||
|
||||
let vs = include_bytes!("shader/quad.vert.spv");
|
||||
let vs_module = device.create_shader_module(
|
||||
&wgpu::read_spirv(std::io::Cursor::new(&vs[..]))
|
||||
.expect("Read quad vertex shader as SPIR-V"),
|
||||
);
|
||||
|
||||
let fs = include_bytes!("shader/quad.frag.spv");
|
||||
let fs_module = device.create_shader_module(
|
||||
&wgpu::read_spirv(std::io::Cursor::new(&fs[..]))
|
||||
.expect("Read quad fragment shader as SPIR-V"),
|
||||
);
|
||||
|
||||
let pipeline =
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
layout: &layout,
|
||||
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
||||
module: &vs_module,
|
||||
entry_point: "main",
|
||||
},
|
||||
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
|
||||
module: &fs_module,
|
||||
entry_point: "main",
|
||||
}),
|
||||
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
|
||||
front_face: wgpu::FrontFace::Cw,
|
||||
cull_mode: wgpu::CullMode::None,
|
||||
depth_bias: 0,
|
||||
depth_bias_slope_scale: 0.0,
|
||||
depth_bias_clamp: 0.0,
|
||||
}),
|
||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
color_states: &[wgpu::ColorStateDescriptor {
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
color_blend: wgpu::BlendDescriptor {
|
||||
src_factor: wgpu::BlendFactor::SrcAlpha,
|
||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
||||
operation: wgpu::BlendOperation::Add,
|
||||
},
|
||||
alpha_blend: wgpu::BlendDescriptor {
|
||||
src_factor: wgpu::BlendFactor::One,
|
||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
||||
operation: wgpu::BlendOperation::Add,
|
||||
},
|
||||
write_mask: wgpu::ColorWrite::ALL,
|
||||
}],
|
||||
depth_stencil_state: None,
|
||||
index_format: wgpu::IndexFormat::Uint16,
|
||||
vertex_buffers: &[
|
||||
wgpu::VertexBufferDescriptor {
|
||||
stride: mem::size_of::<Vertex>() as u64,
|
||||
step_mode: wgpu::InputStepMode::Vertex,
|
||||
attributes: &[wgpu::VertexAttributeDescriptor {
|
||||
shader_location: 0,
|
||||
format: wgpu::VertexFormat::Float2,
|
||||
offset: 0,
|
||||
}],
|
||||
},
|
||||
wgpu::VertexBufferDescriptor {
|
||||
stride: mem::size_of::<Quad>() as u64,
|
||||
step_mode: wgpu::InputStepMode::Instance,
|
||||
attributes: &[
|
||||
wgpu::VertexAttributeDescriptor {
|
||||
shader_location: 1,
|
||||
format: wgpu::VertexFormat::Float2,
|
||||
offset: 0,
|
||||
},
|
||||
wgpu::VertexAttributeDescriptor {
|
||||
shader_location: 2,
|
||||
format: wgpu::VertexFormat::Float2,
|
||||
offset: 4 * 2,
|
||||
},
|
||||
wgpu::VertexAttributeDescriptor {
|
||||
shader_location: 3,
|
||||
format: wgpu::VertexFormat::Float4,
|
||||
offset: 4 * (2 + 2),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
sample_count: 1,
|
||||
sample_mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
});
|
||||
|
||||
let vertices = device
|
||||
.create_buffer_mapped(QUAD_VERTS.len(), wgpu::BufferUsage::VERTEX)
|
||||
.fill_from_slice(&QUAD_VERTS);
|
||||
|
||||
let indices = device
|
||||
.create_buffer_mapped(QUAD_INDICES.len(), wgpu::BufferUsage::INDEX)
|
||||
.fill_from_slice(&QUAD_INDICES);
|
||||
|
||||
let instances = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
size: mem::size_of::<Quad>() as u64 * Quad::MAX as u64,
|
||||
usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST,
|
||||
});
|
||||
|
||||
Pipeline {
|
||||
pipeline,
|
||||
constants,
|
||||
transform,
|
||||
vertices,
|
||||
indices,
|
||||
instances,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(
|
||||
&mut self,
|
||||
device: &mut wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
instances: &[Quad],
|
||||
transformation: Transformation,
|
||||
target: &wgpu::TextureView,
|
||||
) {
|
||||
let matrix: [f32; 16] = transformation.into();
|
||||
|
||||
let transform_buffer = device
|
||||
.create_buffer_mapped(16, wgpu::BufferUsage::COPY_SRC)
|
||||
.fill_from_slice(&matrix[..]);
|
||||
|
||||
encoder.copy_buffer_to_buffer(
|
||||
&transform_buffer,
|
||||
0,
|
||||
&self.transform,
|
||||
0,
|
||||
16 * 4,
|
||||
);
|
||||
|
||||
let mut i = 0;
|
||||
let total = instances.len();
|
||||
|
||||
while i < total {
|
||||
let end = (i + Quad::MAX).min(total);
|
||||
let amount = end - i;
|
||||
|
||||
let instance_buffer = device
|
||||
.create_buffer_mapped(amount, wgpu::BufferUsage::COPY_SRC)
|
||||
.fill_from_slice(&instances[i..end]);
|
||||
|
||||
encoder.copy_buffer_to_buffer(
|
||||
&instance_buffer,
|
||||
0,
|
||||
&self.instances,
|
||||
0,
|
||||
(mem::size_of::<Quad>() * amount) as u64,
|
||||
);
|
||||
|
||||
{
|
||||
let mut render_pass =
|
||||
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
color_attachments: &[
|
||||
wgpu::RenderPassColorAttachmentDescriptor {
|
||||
attachment: target,
|
||||
resolve_target: None,
|
||||
load_op: wgpu::LoadOp::Load,
|
||||
store_op: wgpu::StoreOp::Store,
|
||||
clear_color: wgpu::Color {
|
||||
r: 0.0,
|
||||
g: 0.0,
|
||||
b: 0.0,
|
||||
a: 0.0,
|
||||
},
|
||||
},
|
||||
],
|
||||
depth_stencil_attachment: None,
|
||||
});
|
||||
|
||||
render_pass.set_pipeline(&self.pipeline);
|
||||
render_pass.set_bind_group(0, &self.constants, &[]);
|
||||
render_pass.set_index_buffer(&self.indices, 0);
|
||||
render_pass.set_vertex_buffers(
|
||||
0,
|
||||
&[(&self.vertices, 0), (&self.instances, 0)],
|
||||
);
|
||||
|
||||
render_pass.draw_indexed(
|
||||
0..QUAD_INDICES.len() as u32,
|
||||
0,
|
||||
0..amount as u32,
|
||||
);
|
||||
}
|
||||
|
||||
i += Quad::MAX;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Vertex {
|
||||
_position: [f32; 2],
|
||||
}
|
||||
|
||||
const QUAD_INDICES: [u16; 6] = [0, 1, 2, 0, 2, 3];
|
||||
|
||||
const QUAD_VERTS: [Vertex; 4] = [
|
||||
Vertex {
|
||||
_position: [0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
_position: [1.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
_position: [1.0, 1.0],
|
||||
},
|
||||
Vertex {
|
||||
_position: [0.0, 1.0],
|
||||
},
|
||||
];
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Quad {
|
||||
pub position: [f32; 2],
|
||||
pub scale: [f32; 2],
|
||||
pub color: [f32; 4],
|
||||
}
|
||||
|
||||
impl Quad {
|
||||
const MAX: usize = 100_000;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue