iced-yoda/wgpu/src/quad.rs

308 lines
10 KiB
Rust
Raw Normal View History

2019-10-07 03:56:16 +02:00
use crate::Transformation;
2020-05-19 22:55:12 +02:00
use iced_graphics::layer;
2019-10-27 03:10:49 +01:00
use iced_native::Rectangle;
2019-10-07 03:56:16 +02:00
use bytemuck::{Pod, Zeroable};
2019-10-07 03:56:16 +02:00
use std::mem;
2020-08-27 13:03:42 +02:00
use wgpu::util::DeviceExt;
2019-10-07 03:56:16 +02:00
2019-11-22 22:14:24 +01:00
#[derive(Debug)]
2019-10-07 03:56:16 +02:00
pub struct Pipeline {
pipeline: wgpu::RenderPipeline,
constants: wgpu::BindGroup,
constants_buffer: wgpu::Buffer,
2019-10-07 03:56:16 +02:00
vertices: wgpu::Buffer,
indices: wgpu::Buffer,
instances: wgpu::Buffer,
}
impl Pipeline {
pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Pipeline {
2019-10-07 03:56:16 +02:00
let constant_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
2020-08-31 14:41:41 +02:00
label: Some("iced_wgpu::quad uniforms layout"),
2020-08-27 13:03:42 +02:00
entries: &[wgpu::BindGroupLayoutEntry {
2019-10-07 03:56:16 +02:00
binding: 0,
2021-08-19 03:06:35 +02:00
visibility: wgpu::ShaderStages::VERTEX,
2021-02-03 19:21:02 +01:00
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
2020-08-27 13:03:42 +02:00
min_binding_size: wgpu::BufferSize::new(
2021-05-19 08:09:03 -07:00
mem::size_of::<Uniforms>() as wgpu::BufferAddress,
2020-08-27 13:03:42 +02:00
),
},
count: None,
2019-10-07 03:56:16 +02:00
}],
});
let constants_buffer = device.create_buffer(&wgpu::BufferDescriptor {
2020-08-31 14:41:41 +02:00
label: Some("iced_wgpu::quad uniforms buffer"),
2021-05-19 08:09:03 -07:00
size: mem::size_of::<Uniforms>() as wgpu::BufferAddress,
2021-08-19 03:06:35 +02:00
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
2019-10-07 03:56:16 +02:00
let constants = device.create_bind_group(&wgpu::BindGroupDescriptor {
2020-08-31 14:41:41 +02:00
label: Some("iced_wgpu::quad uniforms bind group"),
2019-10-07 03:56:16 +02:00
layout: &constant_layout,
2020-08-27 13:03:42 +02:00
entries: &[wgpu::BindGroupEntry {
2019-10-07 03:56:16 +02:00
binding: 0,
2021-05-19 08:09:03 -07:00
resource: constants_buffer.as_entire_binding(),
2019-10-07 03:56:16 +02:00
}],
});
let layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
2020-08-31 14:41:41 +02:00
label: Some("iced_wgpu::quad pipeline layout"),
2020-08-27 13:03:42 +02:00
push_constant_ranges: &[],
2019-10-07 03:56:16 +02:00
bind_group_layouts: &[&constant_layout],
});
2021-04-11 18:55:57 -07:00
let shader =
device.create_shader_module(&wgpu::ShaderModuleDescriptor {
label: Some("iced_wgpu::quad::shader"),
source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(
include_str!("shader/quad.wgsl"),
)),
});
2019-10-07 03:56:16 +02:00
let pipeline =
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
2020-08-31 14:41:41 +02:00
label: Some("iced_wgpu::quad pipeline"),
2020-08-27 13:03:42 +02:00
layout: Some(&layout),
2021-02-03 19:21:02 +01:00
vertex: wgpu::VertexState {
2021-04-11 18:55:57 -07:00
module: &shader,
entry_point: "vs_main",
2021-02-03 19:21:02 +01:00
buffers: &[
wgpu::VertexBufferLayout {
array_stride: mem::size_of::<Vertex>() as u64,
2021-08-19 03:06:35 +02:00
step_mode: wgpu::VertexStepMode::Vertex,
2021-02-03 19:21:02 +01:00
attributes: &[wgpu::VertexAttribute {
shader_location: 0,
2021-04-11 18:55:57 -07:00
format: wgpu::VertexFormat::Float32x2,
2019-10-07 03:56:16 +02:00
offset: 0,
}],
},
2021-02-03 19:21:02 +01:00
wgpu::VertexBufferLayout {
array_stride: mem::size_of::<layer::Quad>() as u64,
2021-08-19 03:06:35 +02:00
step_mode: wgpu::VertexStepMode::Instance,
2021-06-20 11:29:23 +02:00
attributes: &wgpu::vertex_attr_array!(
1 => Float32x2,
2 => Float32x2,
3 => Float32x4,
4 => Float32x4,
5 => Float32,
6 => Float32,
),
},
],
},
2021-02-03 19:21:02 +01:00
fragment: Some(wgpu::FragmentState {
2021-04-11 18:55:57 -07:00
module: &shader,
entry_point: "fs_main",
2021-02-03 19:21:02 +01:00
targets: &[wgpu::ColorTargetState {
format,
2021-04-11 18:55:57 -07:00
blend: Some(wgpu::BlendState {
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
alpha: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
}),
2021-08-19 03:06:35 +02:00
write_mask: wgpu::ColorWrites::ALL,
2021-02-03 19:21:02 +01:00
}],
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
front_face: wgpu::FrontFace::Cw,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
2021-12-20 20:48:28 +01:00
multiview: None,
2019-10-07 03:56:16 +02:00
});
2020-08-27 13:03:42 +02:00
let vertices =
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
2020-08-31 14:41:41 +02:00
label: Some("iced_wgpu::quad vertex buffer"),
contents: bytemuck::cast_slice(&QUAD_VERTS),
2021-08-19 03:06:35 +02:00
usage: wgpu::BufferUsages::VERTEX,
2020-08-27 13:03:42 +02:00
});
2019-10-07 03:56:16 +02:00
2020-08-27 13:03:42 +02:00
let indices =
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
2020-08-31 14:41:41 +02:00
label: Some("iced_wgpu::quad index buffer"),
contents: bytemuck::cast_slice(&QUAD_INDICES),
2021-08-19 03:06:35 +02:00
usage: wgpu::BufferUsages::INDEX,
2020-08-27 13:03:42 +02:00
});
2019-10-07 03:56:16 +02:00
let instances = device.create_buffer(&wgpu::BufferDescriptor {
2020-08-31 14:41:41 +02:00
label: Some("iced_wgpu::quad instance buffer"),
2020-05-19 22:55:12 +02:00
size: mem::size_of::<layer::Quad>() as u64 * MAX_INSTANCES as u64,
2021-08-19 03:06:35 +02:00
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
2020-08-27 13:03:42 +02:00
mapped_at_creation: false,
2019-10-07 03:56:16 +02:00
});
Pipeline {
pipeline,
constants,
constants_buffer,
2019-10-07 03:56:16 +02:00
vertices,
indices,
instances,
}
}
pub fn draw(
&mut self,
device: &wgpu::Device,
2020-08-27 13:03:42 +02:00
staging_belt: &mut wgpu::util::StagingBelt,
2019-10-07 03:56:16 +02:00
encoder: &mut wgpu::CommandEncoder,
2020-05-19 22:55:12 +02:00
instances: &[layer::Quad],
2019-10-07 03:56:16 +02:00
transformation: Transformation,
scale: f32,
2019-10-27 03:10:49 +01:00
bounds: Rectangle<u32>,
2019-10-07 03:56:16 +02:00
target: &wgpu::TextureView,
) {
let uniforms = Uniforms::new(transformation, scale);
2020-08-27 13:03:42 +02:00
{
let mut constants_buffer = staging_belt.write_buffer(
encoder,
&self.constants_buffer,
0,
wgpu::BufferSize::new(mem::size_of::<Uniforms>() as u64)
.unwrap(),
device,
);
2019-10-07 03:56:16 +02:00
constants_buffer.copy_from_slice(bytemuck::bytes_of(&uniforms));
2020-08-27 13:03:42 +02:00
}
2019-10-07 03:56:16 +02:00
let mut i = 0;
let total = instances.len();
while i < total {
2020-05-19 22:55:12 +02:00
let end = (i + MAX_INSTANCES).min(total);
2019-10-07 03:56:16 +02:00
let amount = end - i;
2020-08-27 13:03:42 +02:00
let instance_bytes = bytemuck::cast_slice(&instances[i..end]);
2019-10-07 03:56:16 +02:00
2020-08-27 13:03:42 +02:00
let mut instance_buffer = staging_belt.write_buffer(
encoder,
2019-10-07 03:56:16 +02:00
&self.instances,
0,
2020-08-27 13:03:42 +02:00
wgpu::BufferSize::new(instance_bytes.len() as u64).unwrap(),
device,
2019-10-07 03:56:16 +02:00
);
2020-08-27 13:03:42 +02:00
instance_buffer.copy_from_slice(instance_bytes);
2019-10-07 03:56:16 +02:00
{
let mut render_pass =
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
2021-02-03 19:21:02 +01:00
label: Some("iced_wgpu::quad render pass"),
2021-04-11 18:55:57 -07:00
color_attachments: &[wgpu::RenderPassColorAttachment {
view: target,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: true,
2019-10-07 03:56:16 +02:00
},
2021-04-11 18:55:57 -07:00
}],
2019-10-07 03:56:16 +02:00
depth_stencil_attachment: None,
});
render_pass.set_pipeline(&self.pipeline);
render_pass.set_bind_group(0, &self.constants, &[]);
2021-02-03 19:21:02 +01:00
render_pass.set_index_buffer(
self.indices.slice(..),
wgpu::IndexFormat::Uint16,
);
2020-08-27 13:03:42 +02:00
render_pass.set_vertex_buffer(0, self.vertices.slice(..));
render_pass.set_vertex_buffer(1, self.instances.slice(..));
2019-10-27 03:10:49 +01:00
render_pass.set_scissor_rect(
bounds.x,
bounds.y,
bounds.width,
// TODO: Address anti-aliasing adjustments properly
bounds.height,
2019-10-27 03:10:49 +01:00
);
2019-10-07 03:56:16 +02:00
render_pass.draw_indexed(
0..QUAD_INDICES.len() as u32,
0,
0..amount as u32,
);
}
2020-05-19 22:55:12 +02:00
i += MAX_INSTANCES;
2019-10-07 03:56:16 +02:00
}
}
}
2019-12-01 19:03:05 +01:00
#[repr(C)]
#[derive(Clone, Copy, Zeroable, Pod)]
2019-10-07 03:56:16 +02:00
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],
},
];
2020-05-19 22:55:12 +02:00
const MAX_INSTANCES: usize = 100_000;
#[repr(C)]
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
struct Uniforms {
transform: [f32; 16],
scale: f32,
// Uniforms must be aligned to their largest member,
// this uses a mat4x4<f32> which aligns to 16, so align to that
_padding: [f32; 3],
}
impl Uniforms {
fn new(transformation: Transformation, scale: f32) -> Uniforms {
Self {
transform: *transformation.as_ref(),
scale,
2021-05-19 22:07:27 -07:00
_padding: [0.0; 3],
}
}
}
impl Default for Uniforms {
fn default() -> Self {
Self {
transform: *Transformation::identity().as_ref(),
scale: 1.0,
2021-05-19 22:07:27 -07:00
_padding: [0.0; 3],
}
}
}