[wgpu 0.7] Update triangle.rs

This commit is contained in:
Poly 2021-02-03 20:06:07 +01:00
parent 5f935c34fd
commit e2595ac0aa
1 changed files with 60 additions and 52 deletions

View File

@ -86,8 +86,9 @@ impl Pipeline {
entries: &[wgpu::BindGroupLayoutEntry { entries: &[wgpu::BindGroupLayoutEntry {
binding: 0, binding: 0,
visibility: wgpu::ShaderStage::VERTEX, visibility: wgpu::ShaderStage::VERTEX,
ty: wgpu::BindingType::UniformBuffer { ty: wgpu::BindingType::Buffer {
dynamic: true, ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: wgpu::BufferSize::new( min_binding_size: wgpu::BufferSize::new(
mem::size_of::<Uniforms>() as u64, mem::size_of::<Uniforms>() as u64,
), ),
@ -109,11 +110,13 @@ impl Pipeline {
layout: &constants_layout, layout: &constants_layout,
entries: &[wgpu::BindGroupEntry { entries: &[wgpu::BindGroupEntry {
binding: 0, binding: 0,
resource: wgpu::BindingResource::Buffer( resource: wgpu::BindingResource::Buffer {
constants_buffer buffer: &constants_buffer.raw,
.raw offset: 0,
.slice(0..std::mem::size_of::<Uniforms>() as u64), size: wgpu::BufferSize::new(
), std::mem::size_of::<Uniforms>() as u64,
),
},
}], }],
}); });
@ -124,11 +127,11 @@ impl Pipeline {
bind_group_layouts: &[&constants_layout], bind_group_layouts: &[&constants_layout],
}); });
let vs_module = device.create_shader_module(wgpu::include_spirv!( let vs_module = device.create_shader_module(&wgpu::include_spirv!(
"shader/triangle.vert.spv" "shader/triangle.vert.spv"
)); ));
let fs_module = device.create_shader_module(wgpu::include_spirv!( let fs_module = device.create_shader_module(&wgpu::include_spirv!(
"shader/triangle.frag.spv" "shader/triangle.frag.spv"
)); ));
@ -136,49 +139,21 @@ impl Pipeline {
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("iced_wgpu::triangle pipeline"), label: Some("iced_wgpu::triangle pipeline"),
layout: Some(&layout), layout: Some(&layout),
vertex_stage: wgpu::ProgrammableStageDescriptor { vertex: wgpu::VertexState {
module: &vs_module, module: &vs_module,
entry_point: "main", entry_point: "main",
}, buffers: &[wgpu::VertexBufferLayout {
fragment_stage: Some(wgpu::ProgrammableStageDescriptor { array_stride: mem::size_of::<Vertex2D>() as u64,
module: &fs_module,
entry_point: "main",
}),
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Cw,
cull_mode: wgpu::CullMode::None,
..Default::default()
}),
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
color_states: &[wgpu::ColorStateDescriptor {
format,
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,
vertex_state: wgpu::VertexStateDescriptor {
index_format: wgpu::IndexFormat::Uint32,
vertex_buffers: &[wgpu::VertexBufferDescriptor {
stride: mem::size_of::<Vertex2D>() as u64,
step_mode: wgpu::InputStepMode::Vertex, step_mode: wgpu::InputStepMode::Vertex,
attributes: &[ attributes: &[
// Position // Position
wgpu::VertexAttributeDescriptor { wgpu::VertexAttribute {
shader_location: 0, shader_location: 0,
format: wgpu::VertexFormat::Float2, format: wgpu::VertexFormat::Float2,
offset: 0, offset: 0,
}, },
// Color // Color
wgpu::VertexAttributeDescriptor { wgpu::VertexAttribute {
shader_location: 1, shader_location: 1,
format: wgpu::VertexFormat::Float4, format: wgpu::VertexFormat::Float4,
offset: 4 * 2, offset: 4 * 2,
@ -186,11 +161,38 @@ impl Pipeline {
], ],
}], }],
}, },
sample_count: u32::from( fragment: Some(wgpu::FragmentState {
antialiasing.map(|a| a.sample_count()).unwrap_or(1), module: &fs_module,
), entry_point: "main",
sample_mask: !0, targets: &[wgpu::ColorTargetState {
alpha_to_coverage_enabled: false, format,
color_blend: wgpu::BlendState {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
alpha_blend: wgpu::BlendState {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
write_mask: wgpu::ColorWrite::ALL,
}],
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
front_face: wgpu::FrontFace::Cw,
cull_mode: wgpu::CullMode::None,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: u32::from(
antialiasing.map(|a| a.sample_count()).unwrap_or(1),
),
mask: !0,
alpha_to_coverage_enabled: false,
},
}); });
Pipeline { Pipeline {
@ -252,11 +254,15 @@ impl Pipeline {
layout: &self.constants_layout, layout: &self.constants_layout,
entries: &[wgpu::BindGroupEntry { entries: &[wgpu::BindGroupEntry {
binding: 0, binding: 0,
resource: wgpu::BindingResource::Buffer( resource: wgpu::BindingResource::Buffer {
self.uniforms_buffer.raw.slice( buffer: &self.uniforms_buffer.raw,
0..std::mem::size_of::<Uniforms>() as u64, offset: 0,
), size: wgpu::BufferSize::new(std::mem::size_of::<
), Uniforms,
>(
)
as u64),
},
}], }],
}); });
} }
@ -356,6 +362,7 @@ impl Pipeline {
let mut render_pass = let mut render_pass =
encoder.begin_render_pass(&wgpu::RenderPassDescriptor { encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("iced_wgpu::triangle Render Pass"),
color_attachments: &[ color_attachments: &[
wgpu::RenderPassColorAttachmentDescriptor { wgpu::RenderPassColorAttachmentDescriptor {
attachment, attachment,
@ -390,6 +397,7 @@ impl Pipeline {
self.index_buffer self.index_buffer
.raw .raw
.slice(index_offset * mem::size_of::<u32>() as u64..), .slice(index_offset * mem::size_of::<u32>() as u64..),
wgpu::IndexFormat::Uint32,
); );
render_pass.set_vertex_buffer( render_pass.set_vertex_buffer(