summaryrefslogtreecommitdiffstats
path: root/wgpu
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2022-11-03 18:57:09 +0100
committerLibravatar GitHub <noreply@github.com>2022-11-03 18:57:09 +0100
commitd222b5c8b0befab665c20ba0112b28199df0ae44 (patch)
tree0ac3a59f04e1c1ca89ff43800efbefd825b015ea /wgpu
parenta8f510c39917b2ac42fcc854f0a7eff13aee9838 (diff)
parentf31c8f2504ea7c004c5caed8913e5da28d2e50e2 (diff)
downloadiced-d222b5c8b0befab665c20ba0112b28199df0ae44.tar.gz
iced-d222b5c8b0befab665c20ba0112b28199df0ae44.tar.bz2
iced-d222b5c8b0befab665c20ba0112b28199df0ae44.zip
Merge pull request #1448 from bungoboingo/fear/linear-gradients
Add linear gradient support to canvas widget
Diffstat (limited to 'wgpu')
-rw-r--r--wgpu/Cargo.toml7
-rw-r--r--wgpu/src/backend.rs9
-rw-r--r--wgpu/src/buffer.rs3
-rw-r--r--wgpu/src/buffer/dynamic.rs199
-rw-r--r--wgpu/src/buffer/static.rs117
-rw-r--r--wgpu/src/lib.rs1
-rw-r--r--wgpu/src/shader/gradient.wgsl88
-rw-r--r--wgpu/src/shader/solid.wgsl17
-rw-r--r--wgpu/src/triangle.rs514
-rw-r--r--wgpu/src/triangle/gradient.rs268
-rw-r--r--wgpu/src/triangle/solid.rs170
11 files changed, 1067 insertions, 326 deletions
diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml
index 55eec73f..9a57e58b 100644
--- a/wgpu/Cargo.toml
+++ b/wgpu/Cargo.toml
@@ -69,6 +69,13 @@ optional = true
version = "0.6"
optional = true
+[dependencies.encase]
+version = "0.3.0"
+features = ["glam"]
+
+[dependencies.glam]
+version = "0.21.3"
+
[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
all-features = true
diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs
index 8c875254..80026673 100644
--- a/wgpu/src/backend.rs
+++ b/wgpu/src/backend.rs
@@ -94,8 +94,7 @@ impl Backend {
staging_belt,
encoder,
frame,
- target_size.width,
- target_size.height,
+ target_size,
);
}
@@ -112,8 +111,7 @@ impl Backend {
staging_belt: &mut wgpu::util::StagingBelt,
encoder: &mut wgpu::CommandEncoder,
target: &wgpu::TextureView,
- target_width: u32,
- target_height: u32,
+ target_size: Size<u32>,
) {
let bounds = (layer.bounds * scale_factor).snap();
@@ -143,8 +141,7 @@ impl Backend {
staging_belt,
encoder,
target,
- target_width,
- target_height,
+ target_size,
scaled,
scale_factor,
&layer.meshes,
diff --git a/wgpu/src/buffer.rs b/wgpu/src/buffer.rs
new file mode 100644
index 00000000..7c092d0b
--- /dev/null
+++ b/wgpu/src/buffer.rs
@@ -0,0 +1,3 @@
+//! Utilities for buffer operations.
+pub mod dynamic;
+pub mod r#static;
diff --git a/wgpu/src/buffer/dynamic.rs b/wgpu/src/buffer/dynamic.rs
new file mode 100644
index 00000000..c0c48c74
--- /dev/null
+++ b/wgpu/src/buffer/dynamic.rs
@@ -0,0 +1,199 @@
+//! Utilities for uniform buffer operations.
+use encase::private::WriteInto;
+use encase::ShaderType;
+use std::marker::PhantomData;
+
+/// A dynamic buffer is any type of buffer which does not have a static offset.
+pub(crate) struct Buffer<T: ShaderType> {
+ offsets: Vec<wgpu::DynamicOffset>,
+ cpu: Internal,
+ gpu: wgpu::Buffer,
+ label: &'static str,
+ size: u64,
+ _data: PhantomData<T>,
+}
+
+impl<T: ShaderType + WriteInto> Buffer<T> {
+ /// Creates a new dynamic uniform buffer.
+ pub fn uniform(device: &wgpu::Device, label: &'static str) -> Self {
+ Buffer::new(
+ device,
+ Internal::Uniform(encase::DynamicUniformBuffer::new(Vec::new())),
+ label,
+ wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ )
+ }
+
+ /// Creates a new dynamic storage buffer.
+ pub fn storage(device: &wgpu::Device, label: &'static str) -> Self {
+ Buffer::new(
+ device,
+ Internal::Storage(encase::DynamicStorageBuffer::new(Vec::new())),
+ label,
+ wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
+ )
+ }
+
+ fn new(
+ device: &wgpu::Device,
+ dynamic_buffer_type: Internal,
+ label: &'static str,
+ usage: wgpu::BufferUsages,
+ ) -> Self {
+ let initial_size = u64::from(T::min_size());
+
+ Self {
+ offsets: Vec::new(),
+ cpu: dynamic_buffer_type,
+ gpu: Buffer::<T>::create_gpu_buffer(
+ device,
+ label,
+ usage,
+ initial_size,
+ ),
+ label,
+ size: initial_size,
+ _data: Default::default(),
+ }
+ }
+
+ fn create_gpu_buffer(
+ device: &wgpu::Device,
+ label: &'static str,
+ usage: wgpu::BufferUsages,
+ size: u64,
+ ) -> wgpu::Buffer {
+ device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some(label),
+ size,
+ usage,
+ mapped_at_creation: false,
+ })
+ }
+
+ /// Write a new value to the CPU buffer with proper alignment. Stores the returned offset value
+ /// in the buffer for future use.
+ pub fn push(&mut self, value: &T) {
+ //this write operation on the cpu buffer will adjust for uniform alignment requirements
+ let offset = self.cpu.write(value);
+ self.offsets.push(offset as u32);
+ }
+
+ /// Resize buffer contents if necessary. This will re-create the GPU buffer if current size is
+ /// less than the newly computed size from the CPU buffer.
+ ///
+ /// If the gpu buffer is resized, its bind group will need to be recreated!
+ pub fn resize(&mut self, device: &wgpu::Device) -> bool {
+ let new_size = self.cpu.get_ref().len() as u64;
+
+ if self.size < new_size {
+ let usages = match self.cpu {
+ Internal::Uniform(_) => {
+ wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST
+ }
+ Internal::Storage(_) => {
+ wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST
+ }
+ };
+
+ self.gpu = Buffer::<T>::create_gpu_buffer(
+ device, self.label, usages, new_size,
+ );
+ self.size = new_size;
+ true
+ } else {
+ false
+ }
+ }
+
+ /// Write the contents of this dynamic buffer to the GPU via staging belt command.
+ pub fn write(
+ &mut self,
+ device: &wgpu::Device,
+ staging_belt: &mut wgpu::util::StagingBelt,
+ encoder: &mut wgpu::CommandEncoder,
+ ) {
+ let size = self.cpu.get_ref().len();
+
+ if let Some(buffer_size) = wgpu::BufferSize::new(size as u64) {
+ let mut buffer = staging_belt.write_buffer(
+ encoder,
+ &self.gpu,
+ 0,
+ buffer_size,
+ device,
+ );
+
+ buffer.copy_from_slice(self.cpu.get_ref());
+ }
+ }
+
+ // Gets the aligned offset at the given index from the CPU buffer.
+ pub fn offset_at_index(&self, index: usize) -> wgpu::DynamicOffset {
+ let offset = self
+ .offsets
+ .get(index)
+ .copied()
+ .expect("Index not found in offsets.");
+
+ offset
+ }
+
+ /// Returns a reference to the GPU buffer.
+ pub fn raw(&self) -> &wgpu::Buffer {
+ &self.gpu
+ }
+
+ /// Reset the buffer.
+ pub fn clear(&mut self) {
+ self.offsets.clear();
+ self.cpu.clear();
+ }
+}
+
+// Currently supported dynamic buffers.
+enum Internal {
+ Uniform(encase::DynamicUniformBuffer<Vec<u8>>),
+ Storage(encase::DynamicStorageBuffer<Vec<u8>>),
+}
+
+impl Internal {
+ /// Writes the current value to its CPU buffer with proper alignment.
+ pub(super) fn write<T: ShaderType + WriteInto>(
+ &mut self,
+ value: &T,
+ ) -> wgpu::DynamicOffset {
+ match self {
+ Internal::Uniform(buf) => buf
+ .write(value)
+ .expect("Error when writing to dynamic uniform buffer.")
+ as u32,
+ Internal::Storage(buf) => buf
+ .write(value)
+ .expect("Error when writing to dynamic storage buffer.")
+ as u32,
+ }
+ }
+
+ /// Returns bytearray of aligned CPU buffer.
+ pub(super) fn get_ref(&self) -> &Vec<u8> {
+ match self {
+ Internal::Uniform(buf) => buf.as_ref(),
+ Internal::Storage(buf) => buf.as_ref(),
+ }
+ }
+
+ /// Resets the CPU buffer.
+ pub(super) fn clear(&mut self) {
+ match self {
+ Internal::Uniform(buf) => {
+ buf.as_mut().clear();
+ buf.set_offset(0);
+ }
+ Internal::Storage(buf) => {
+ buf.as_mut().clear();
+ buf.set_offset(0);
+ }
+ }
+ }
+}
diff --git a/wgpu/src/buffer/static.rs b/wgpu/src/buffer/static.rs
new file mode 100644
index 00000000..cf06790c
--- /dev/null
+++ b/wgpu/src/buffer/static.rs
@@ -0,0 +1,117 @@
+use bytemuck::{Pod, Zeroable};
+use std::marker::PhantomData;
+use std::mem;
+
+//128 triangles/indices
+const DEFAULT_STATIC_BUFFER_COUNT: wgpu::BufferAddress = 128;
+
+/// A generic buffer struct useful for items which have no alignment requirements
+/// (e.g. Vertex, Index buffers) & no dynamic offsets.
+#[derive(Debug)]
+pub(crate) struct Buffer<T> {
+ //stored sequentially per mesh iteration; refers to the offset index in the GPU buffer
+ offsets: Vec<wgpu::BufferAddress>,
+ label: &'static str,
+ usages: wgpu::BufferUsages,
+ gpu: wgpu::Buffer,
+ size: wgpu::BufferAddress,
+ _data: PhantomData<T>,
+}
+
+impl<T: Pod + Zeroable> Buffer<T> {
+ /// Initialize a new static buffer.
+ pub fn new(
+ device: &wgpu::Device,
+ label: &'static str,
+ usages: wgpu::BufferUsages,
+ ) -> Self {
+ let size = (mem::size_of::<T>() as u64) * DEFAULT_STATIC_BUFFER_COUNT;
+
+ Self {
+ offsets: Vec::new(),
+ label,
+ usages,
+ gpu: Self::gpu_buffer(device, label, size, usages),
+ size,
+ _data: PhantomData,
+ }
+ }
+
+ fn gpu_buffer(
+ device: &wgpu::Device,
+ label: &'static str,
+ size: wgpu::BufferAddress,
+ usage: wgpu::BufferUsages,
+ ) -> wgpu::Buffer {
+ device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some(label),
+ size,
+ usage,
+ mapped_at_creation: false,
+ })
+ }
+
+ /// Returns whether or not the buffer needs to be recreated. This can happen whenever mesh data
+ /// changes & a redraw is requested.
+ pub fn resize(&mut self, device: &wgpu::Device, new_count: usize) -> bool {
+ let size = (mem::size_of::<T>() * new_count) as u64;
+
+ if self.size < size {
+ self.offsets.clear();
+ self.size = size;
+ self.gpu = Self::gpu_buffer(device, self.label, size, self.usages);
+ true
+ } else {
+ false
+ }
+ }
+
+ /// Writes the current vertex data to the gpu buffer with a memcpy & stores its offset.
+ ///
+ /// Returns the size of the written bytes.
+ pub fn write(
+ &mut self,
+ device: &wgpu::Device,
+ staging_belt: &mut wgpu::util::StagingBelt,
+ encoder: &mut wgpu::CommandEncoder,
+ offset: u64,
+ content: &[T],
+ ) -> u64 {
+ let bytes = bytemuck::cast_slice(content);
+ let bytes_size = bytes.len() as u64;
+
+ if let Some(buffer_size) = wgpu::BufferSize::new(bytes_size) {
+ let mut buffer = staging_belt.write_buffer(
+ encoder,
+ &self.gpu,
+ offset,
+ buffer_size,
+ device,
+ );
+
+ buffer.copy_from_slice(bytes);
+
+ self.offsets.push(offset);
+ }
+
+ bytes_size
+ }
+
+ fn offset_at(&self, index: usize) -> &wgpu::BufferAddress {
+ self.offsets
+ .get(index)
+ .expect("Offset at index does not exist.")
+ }
+
+ /// Returns the slice calculated from the offset stored at the given index.
+ /// e.g. to calculate the slice for the 2nd mesh in the layer, this would be the offset at index
+ /// 1 that we stored earlier when writing.
+ pub fn slice_from_index(&self, index: usize) -> wgpu::BufferSlice<'_> {
+ self.gpu.slice(self.offset_at(index)..)
+ }
+
+ /// Clears any temporary data from the buffer.
+ pub fn clear(&mut self) {
+ self.offsets.clear()
+ }
+}
diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs
index 3a98c6bd..1295516b 100644
--- a/wgpu/src/lib.rs
+++ b/wgpu/src/lib.rs
@@ -43,6 +43,7 @@ pub mod triangle;
pub mod window;
mod backend;
+mod buffer;
mod quad;
mod text;
diff --git a/wgpu/src/shader/gradient.wgsl b/wgpu/src/shader/gradient.wgsl
new file mode 100644
index 00000000..63825aec
--- /dev/null
+++ b/wgpu/src/shader/gradient.wgsl
@@ -0,0 +1,88 @@
+struct Uniforms {
+ transform: mat4x4<f32>,
+ //xy = start, wz = end
+ position: vec4<f32>,
+ //x = start stop, y = end stop, zw = padding
+ stop_range: vec4<i32>,
+}
+
+struct Stop {
+ color: vec4<f32>,
+ offset: f32,
+};
+
+@group(0) @binding(0)
+var<uniform> uniforms: Uniforms;
+
+@group(0) @binding(1)
+var<storage, read> color_stops: array<Stop>;
+
+struct VertexOutput {
+ @builtin(position) position: vec4<f32>,
+ @location(0) raw_position: vec2<f32>
+}
+
+@vertex
+fn vs_main(@location(0) input: vec2<f32>) -> VertexOutput {
+ var output: VertexOutput;
+ output.position = uniforms.transform * vec4<f32>(input.xy, 0.0, 1.0);
+ output.raw_position = input;
+
+ return output;
+}
+
+//TODO: rewrite without branching
+@fragment
+fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
+ let start = uniforms.position.xy;
+ let end = uniforms.position.zw;
+ let start_stop = uniforms.stop_range.x;
+ let end_stop = uniforms.stop_range.y;
+
+ let v1 = end - start;
+ let v2 = input.raw_position.xy - start;
+ let unit = normalize(v1);
+ let offset = dot(unit, v2) / length(v1);
+
+ let min_stop = color_stops[start_stop];
+ let max_stop = color_stops[end_stop];
+
+ var color: vec4<f32>;
+
+ if (offset <= min_stop.offset) {
+ color = min_stop.color;
+ } else if (offset >= max_stop.offset) {
+ color = max_stop.color;
+ } else {
+ var min = min_stop;
+ var max = max_stop;
+ var min_index = start_stop;
+ var max_index = end_stop;
+
+ loop {
+ if (min_index >= max_index - 1) {
+ break;
+ }
+
+ let index = min_index + (max_index - min_index) / 2;
+
+ let stop = color_stops[index];
+
+ if (offset <= stop.offset) {
+ max = stop;
+ max_index = index;
+ } else {
+ min = stop;
+ min_index = index;
+ }
+ }
+
+ color = mix(min.color, max.color, smoothstep(
+ min.offset,
+ max.offset,
+ offset
+ ));
+ }
+
+ return color;
+}
diff --git a/wgpu/src/shader/solid.wgsl b/wgpu/src/shader/solid.wgsl
new file mode 100644
index 00000000..68a8fea3
--- /dev/null
+++ b/wgpu/src/shader/solid.wgsl
@@ -0,0 +1,17 @@
+struct Uniforms {
+ transform: mat4x4<f32>,
+ color: vec4<f32>
+}
+
+@group(0) @binding(0)
+var<uniform> uniforms: Uniforms;
+
+@vertex
+fn vs_main(@location(0) input: vec2<f32>) -> @builtin(position) vec4<f32> {
+ return uniforms.transform * vec4<f32>(input.xy, 0.0, 1.0);
+}
+
+@fragment
+fn fs_main() -> @location(0) vec4<f32> {
+ return uniforms.color;
+}
diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs
index fd06dddf..f9abf2b5 100644
--- a/wgpu/src/triangle.rs
+++ b/wgpu/src/triangle.rs
@@ -1,345 +1,176 @@
//! Draw meshes of triangles.
-use crate::{settings, Transformation};
-use iced_graphics::layer;
-
-use bytemuck::{Pod, Zeroable};
-use std::mem;
+mod gradient;
+mod msaa;
+mod solid;
-pub use iced_graphics::triangle::{Mesh2D, Vertex2D};
+use crate::buffer::r#static::Buffer;
+use crate::settings;
+use crate::Transformation;
-mod msaa;
+use iced_graphics::layer::mesh::{self, Mesh};
+use iced_graphics::triangle::{self, Vertex2D};
+use iced_graphics::Size;
-const UNIFORM_BUFFER_SIZE: usize = 50;
-const VERTEX_BUFFER_SIZE: usize = 10_000;
-const INDEX_BUFFER_SIZE: usize = 10_000;
+use core::fmt;
+use std::fmt::Formatter;
+/// Triangle pipeline for all mesh layers in a [`iced_graphics::Canvas`] widget.
#[derive(Debug)]
pub(crate) struct Pipeline {
- pipeline: wgpu::RenderPipeline,
blit: Option<msaa::Blit>,
- constants_layout: wgpu::BindGroupLayout,
- constants: wgpu::BindGroup,
- uniforms_buffer: Buffer<Uniforms>,
vertex_buffer: Buffer<Vertex2D>,
index_buffer: Buffer<u32>,
+ index_strides: Vec<u32>,
+ pipelines: PipelineList,
}
-#[derive(Debug)]
-struct Buffer<T> {
- label: &'static str,
- raw: wgpu::Buffer,
- size: usize,
- usage: wgpu::BufferUsages,
- _type: std::marker::PhantomData<T>,
+/// Supported triangle pipelines for different fills.
+pub(crate) struct PipelineList {
+ solid: solid::Pipeline,
+ gradient: gradient::Pipeline,
}
-impl<T> Buffer<T> {
- pub fn new(
- label: &'static str,
- device: &wgpu::Device,
- size: usize,
- usage: wgpu::BufferUsages,
- ) -> Self {
- let raw = device.create_buffer(&wgpu::BufferDescriptor {
- label: Some(label),
- size: (std::mem::size_of::<T>() * size) as u64,
- usage,
- mapped_at_creation: false,
- });
-
- Buffer {
- label,
- raw,
- size,
- usage,
- _type: std::marker::PhantomData,
- }
+impl fmt::Debug for PipelineList {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ f.debug_struct("TrianglePipelines").finish()
}
+}
- pub fn expand(&mut self, device: &wgpu::Device, size: usize) -> bool {
- let needs_resize = self.size < size;
-
- if needs_resize {
- self.raw = device.create_buffer(&wgpu::BufferDescriptor {
- label: Some(self.label),
- size: (std::mem::size_of::<T>() * size) as u64,
- usage: self.usage,
- mapped_at_creation: false,
- });
-
- self.size = size;
- }
+impl PipelineList {
+ /// Resets each pipeline's buffers.
+ fn clear(&mut self) {
+ self.solid.buffer.clear();
+ self.gradient.uniform_buffer.clear();
+ self.gradient.storage_buffer.clear();
+ }
- needs_resize
+ /// Writes the contents of each pipeline's CPU buffer to the GPU, resizing the GPU buffer
+ /// beforehand if necessary.
+ fn write(
+ &mut self,
+ device: &wgpu::Device,
+ staging_belt: &mut wgpu::util::StagingBelt,
+ encoder: &mut wgpu::CommandEncoder,
+ ) {
+ self.solid.write(device, staging_belt, encoder);
+ self.gradient.write(device, staging_belt, encoder);
}
}
impl Pipeline {
+ /// Creates supported pipelines, listed in [TrianglePipelines].
pub fn new(
device: &wgpu::Device,
format: wgpu::TextureFormat,
antialiasing: Option<settings::Antialiasing>,
) -> Pipeline {
- let constants_layout =
- device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
- label: Some("iced_wgpu::triangle uniforms layout"),
- entries: &[wgpu::BindGroupLayoutEntry {
- binding: 0,
- visibility: wgpu::ShaderStages::VERTEX,
- ty: wgpu::BindingType::Buffer {
- ty: wgpu::BufferBindingType::Uniform,
- has_dynamic_offset: true,
- min_binding_size: wgpu::BufferSize::new(
- mem::size_of::<Uniforms>() as u64,
- ),
- },
- count: None,
- }],
- });
-
- let constants_buffer = Buffer::new(
- "iced_wgpu::triangle uniforms buffer",
- device,
- UNIFORM_BUFFER_SIZE,
- wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
- );
-
- let constant_bind_group =
- device.create_bind_group(&wgpu::BindGroupDescriptor {
- label: Some("iced_wgpu::triangle uniforms bind group"),
- layout: &constants_layout,
- entries: &[wgpu::BindGroupEntry {
- binding: 0,
- resource: wgpu::BindingResource::Buffer(
- wgpu::BufferBinding {
- buffer: &constants_buffer.raw,
- offset: 0,
- size: wgpu::BufferSize::new(std::mem::size_of::<
- Uniforms,
- >(
- )
- as u64),
- },
- ),
- }],
- });
-
- let layout =
- device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
- label: Some("iced_wgpu::triangle pipeline layout"),
- push_constant_ranges: &[],
- bind_group_layouts: &[&constants_layout],
- });
-
- let shader =
- device.create_shader_module(wgpu::ShaderModuleDescriptor {
- label: Some("iced_wgpu::triangle::shader"),
- source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(
- include_str!("shader/triangle.wgsl"),
- )),
- });
-
- let pipeline =
- device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
- label: Some("iced_wgpu::triangle pipeline"),
- layout: Some(&layout),
- vertex: wgpu::VertexState {
- module: &shader,
- entry_point: "vs_main",
- buffers: &[wgpu::VertexBufferLayout {
- array_stride: mem::size_of::<Vertex2D>() as u64,
- step_mode: wgpu::VertexStepMode::Vertex,
- attributes: &wgpu::vertex_attr_array!(
- // Position
- 0 => Float32x2,
- // Color
- 1 => Float32x4,
- ),
- }],
- },
- fragment: Some(wgpu::FragmentState {
- module: &shader,
- entry_point: "fs_main",
- targets: &[Some(wgpu::ColorTargetState {
- format,
- blend: Some(wgpu::BlendState::ALPHA_BLENDING),
- write_mask: wgpu::ColorWrites::ALL,
- })],
- }),
- primitive: wgpu::PrimitiveState {
- topology: wgpu::PrimitiveTopology::TriangleList,
- front_face: wgpu::FrontFace::Cw,
- ..Default::default()
- },
- depth_stencil: None,
- multisample: wgpu::MultisampleState {
- count: antialiasing.map(|a| a.sample_count()).unwrap_or(1),
- mask: !0,
- alpha_to_coverage_enabled: false,
- },
- multiview: None,
- });
-
Pipeline {
- pipeline,
blit: antialiasing.map(|a| msaa::Blit::new(device, format, a)),
- constants_layout,
- constants: constant_bind_group,
- uniforms_buffer: constants_buffer,
vertex_buffer: Buffer::new(
- "iced_wgpu::triangle vertex buffer",
device,
- VERTEX_BUFFER_SIZE,
+ "iced_wgpu::triangle vertex buffer",
wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
),
index_buffer: Buffer::new(
- "iced_wgpu::triangle index buffer",
device,
- INDEX_BUFFER_SIZE,
+ "iced_wgpu::triangle vertex buffer",
wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,
),
+ index_strides: Vec::new(),
+ pipelines: PipelineList {
+ solid: solid::Pipeline::new(device, format, antialiasing),
+ gradient: gradient::Pipeline::new(device, format, antialiasing),
+ },
}
}
+ /// Draws the contents of the current layer's meshes to the [target].
pub fn draw(
&mut self,
device: &wgpu::Device,
staging_belt: &mut wgpu::util::StagingBelt,
encoder: &mut wgpu::CommandEncoder,
target: &wgpu::TextureView,
- target_width: u32,
- target_height: u32,
+ target_size: Size<u32>,
transformation: Transformation,
scale_factor: f32,
- meshes: &[layer::Mesh<'_>],
+ meshes: &[Mesh<'_>],
) {
- // This looks a bit crazy, but we are just counting how many vertices
- // and indices we will need to handle.
- // TODO: Improve readability
- let (total_vertices, total_indices) = meshes
- .iter()
- .map(|layer::Mesh { buffers, .. }| {
- (buffers.vertices.len(), buffers.indices.len())
- })
- .fold((0, 0), |(total_v, total_i), (v, i)| {
- (total_v + v, total_i + i)
- });
-
- // Then we ensure the current buffers are big enough, resizing if
- // necessary
- let _ = self.vertex_buffer.expand(device, total_vertices);
- let _ = self.index_buffer.expand(device, total_indices);
-
- // If the uniforms buffer is resized, then we need to recreate its
- // bind group.
- if self.uniforms_buffer.expand(device, meshes.len()) {
- self.constants =
- device.create_bind_group(&wgpu::BindGroupDescriptor {
- label: Some("iced_wgpu::triangle uniforms buffer"),
- layout: &self.constants_layout,
- entries: &[wgpu::BindGroupEntry {
- binding: 0,
- resource: wgpu::BindingResource::Buffer(
- wgpu::BufferBinding {
- buffer: &self.uniforms_buffer.raw,
- offset: 0,
- size: wgpu::BufferSize::new(
- std::mem::size_of::<Uniforms>() as u64,
- ),
- },
- ),
- }],
- });
- }
+ //count the total amount of vertices & indices we need to handle
+ let (total_vertices, total_indices) = mesh::attribute_count_of(meshes);
- let mut uniforms: Vec<Uniforms> = Vec::with_capacity(meshes.len());
- let mut offsets: Vec<(
- wgpu::BufferAddress,
- wgpu::BufferAddress,
- usize,
- )> = Vec::with_capacity(meshes.len());
- let mut last_vertex = 0;
- let mut last_index = 0;
+ // Then we ensure the current attribute buffers are big enough, resizing if necessary.
- // We upload everything upfront
- for mesh in meshes {
- let transform = (transformation
- * Transformation::translate(mesh.origin.x, mesh.origin.y))
- .into();
-
- let vertices = bytemuck::cast_slice(&mesh.buffers.vertices);
- let indices = bytemuck::cast_slice(&mesh.buffers.indices);
-
- if let (Some(vertices_size), Some(indices_size)) = (
- wgpu::BufferSize::new(vertices.len() as u64),
- wgpu::BufferSize::new(indices.len() as u64),
- ) {
- {
- let mut vertex_buffer = staging_belt.write_buffer(
- encoder,
- &self.vertex_buffer.raw,
- (std::mem::size_of::<Vertex2D>() * last_vertex) as u64,
- vertices_size,
- device,
- );
-
- vertex_buffer.copy_from_slice(vertices);
- }
+ //We are not currently using the return value of these functions as we have no system in
+ //place to calculate mesh diff, or to know whether or not that would be more performant for
+ //the majority of use cases. Therefore we will write GPU data every frame (for now).
+ let _ = self.vertex_buffer.resize(device, total_vertices);
+ let _ = self.index_buffer.resize(device, total_indices);
- {
- let mut index_buffer = staging_belt.write_buffer(
- encoder,
- &self.index_buffer.raw,
- (std::mem::size_of::<u32>() * last_index) as u64,
- indices_size,
- device,
- );
+ //prepare dynamic buffers & data store for writing
+ self.index_strides.clear();
+ self.pipelines.clear();
- index_buffer.copy_from_slice(indices);
- }
-
- uniforms.push(transform);
- offsets.push((
- last_vertex as u64,
- last_index as u64,
- mesh.buffers.indices.len(),
- ));
+ let mut vertex_offset = 0;
+ let mut index_offset = 0;
- last_vertex += mesh.buffers.vertices.len();
- last_index += mesh.buffers.indices.len();
- }
- }
-
- let uniforms = bytemuck::cast_slice(&uniforms);
+ for mesh in meshes {
+ let transform = transformation
+ * Transformation::translate(mesh.origin.x, mesh.origin.y);
- if let Some(uniforms_size) =
- wgpu::BufferSize::new(uniforms.len() as u64)
- {
- let mut uniforms_buffer = staging_belt.write_buffer(
+ //write to both buffers
+ let new_vertex_offset = self.vertex_buffer.write(
+ device,
+ staging_belt,
encoder,
- &self.uniforms_buffer.raw,
- 0,
- uniforms_size,
+ vertex_offset,
+ &mesh.buffers.vertices,
+ );
+
+ let new_index_offset = self.index_buffer.write(
device,
+ staging_belt,
+ encoder,
+ index_offset,
+ &mesh.buffers.indices,
);
- uniforms_buffer.copy_from_slice(uniforms);
+ vertex_offset += new_vertex_offset;
+ index_offset += new_index_offset;
+
+ self.index_strides.push(mesh.buffers.indices.len() as u32);
+
+ //push uniform data to CPU buffers
+ match mesh.style {
+ triangle::Style::Solid(color) => {
+ self.pipelines.solid.push(transform, color);
+ }
+ triangle::Style::Gradient(gradient) => {
+ self.pipelines.gradient.push(transform, gradient);
+ }
+ }
}
+ //write uniform data to GPU
+ self.pipelines.write(device, staging_belt, encoder);
+
+ //configure the render pass now that the data is uploaded to the GPU
{
- let (attachment, resolve_target, load) =
- if let Some(blit) = &mut self.blit {
- let (attachment, resolve_target) =
- blit.targets(device, target_width, target_height);
-
- (
- attachment,
- Some(resolve_target),
- wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
- )
- } else {
- (target, None, wgpu::LoadOp::Load)
- };
+ //configure antialiasing pass
+ let (attachment, resolve_target, load) = if let Some(blit) =
+ &mut self.blit
+ {
+ let (attachment, resolve_target) =
+ blit.targets(device, target_size.width, target_size.height);
+
+ (
+ attachment,
+ Some(resolve_target),
+ wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
+ )
+ } else {
+ (target, None, wgpu::LoadOp::Load)
+ };
let mut render_pass =
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
@@ -354,12 +185,12 @@ impl Pipeline {
depth_stencil_attachment: None,
});
- render_pass.set_pipeline(&self.pipeline);
+ let mut num_solids = 0;
+ let mut num_gradients = 0;
+ let mut last_is_solid = None;
- for (i, (vertex_offset, index_offset, indices)) in
- offsets.into_iter().enumerate()
- {
- let clip_bounds = (meshes[i].clip_bounds * scale_factor).snap();
+ for (index, mesh) in meshes.iter().enumerate() {
+ let clip_bounds = (mesh.clip_bounds * scale_factor).snap();
render_pass.set_scissor_rect(
clip_bounds.x,
@@ -368,62 +199,105 @@ impl Pipeline {
clip_bounds.height,
);
- render_pass.set_bind_group(
+ match mesh.style {
+ triangle::Style::Solid(_) => {
+ if !last_is_solid.unwrap_or(false) {
+ self.pipelines
+ .solid
+ .set_render_pass_pipeline(&mut render_pass);
+
+ last_is_solid = Some(true);
+ }
+
+ self.pipelines.solid.configure_render_pass(
+ &mut render_pass,
+ num_solids,
+ );
+
+ num_solids += 1;
+ }
+ triangle::Style::Gradient(_) => {
+ if last_is_solid.unwrap_or(true) {
+ self.pipelines
+ .gradient
+ .set_render_pass_pipeline(&mut render_pass);
+
+ last_is_solid = Some(false);
+ }
+
+ self.pipelines.gradient.configure_render_pass(
+ &mut render_pass,
+ num_gradients,
+ );
+
+ num_gradients += 1;
+ }
+ };
+
+ render_pass.set_vertex_buffer(
0,
- &self.constants,
- &[(std::mem::size_of::<Uniforms>() * i) as u32],
+ self.vertex_buffer.slice_from_index(index),
);
render_pass.set_index_buffer(
- self.index_buffer
- .raw
- .slice(index_offset * mem::size_of::<u32>() as u64..),
+ self.index_buffer.slice_from_index(index),
wgpu::IndexFormat::Uint32,
);
- render_pass.set_vertex_buffer(
+ render_pass.draw_indexed(
+ 0..(self.index_strides[index] as u32),
0,
- self.vertex_buffer.raw.slice(
- vertex_offset * mem::size_of::<Vertex2D>() as u64..,
- ),
+ 0..1,
);
-
- render_pass.draw_indexed(0..indices as u32, 0, 0..1);
}
}
+ self.vertex_buffer.clear();
+ self.index_buffer.clear();
+
if let Some(blit) = &mut self.blit {
blit.draw(encoder, target);
}
}
}
-#[repr(C)]
-#[derive(Debug, Clone, Copy, Zeroable, Pod)]
-struct Uniforms {
- transform: [f32; 16],
- // We need to align this to 256 bytes to please `wgpu`...
- // TODO: Be smarter and stop wasting memory!
- _padding_a: [f32; 32],
- _padding_b: [f32; 16],
+//utility functions for individual pipelines with shared functionality
+fn vertex_buffer_layout<'a>() -> wgpu::VertexBufferLayout<'a> {
+ wgpu::VertexBufferLayout {
+ array_stride: std::mem::size_of::<Vertex2D>() as u64,
+ step_mode: wgpu::VertexStepMode::Vertex,
+ attributes: &[wgpu::VertexAttribute {
+ format: wgpu::VertexFormat::Float32x2,
+ offset: 0,
+ shader_location: 0,
+ }],
+ }
}
-impl Default for Uniforms {
- fn default() -> Self {
- Self {
- transform: *Transformation::identity().as_ref(),
- _padding_a: [0.0; 32],
- _padding_b: [0.0; 16],
- }
+fn fragment_target(
+ texture_format: wgpu::TextureFormat,
+) -> Option<wgpu::ColorTargetState> {
+ Some(wgpu::ColorTargetState {
+ format: texture_format,
+ blend: Some(wgpu::BlendState::ALPHA_BLENDING),
+ write_mask: wgpu::ColorWrites::ALL,
+ })
+}
+
+fn primitive_state() -> wgpu::PrimitiveState {
+ wgpu::PrimitiveState {
+ topology: wgpu::PrimitiveTopology::TriangleList,
+ front_face: wgpu::FrontFace::Cw,
+ ..Default::default()
}
}
-impl From<Transformation> for Uniforms {
- fn from(transformation: Transformation) -> Uniforms {
- Self {
- transform: transformation.into(),
- _padding_a: [0.0; 32],
- _padding_b: [0.0; 16],
- }
+fn multisample_state(
+ antialiasing: Option<settings::Antialiasing>,
+) -> wgpu::MultisampleState {
+ wgpu::MultisampleState {
+ count: antialiasing.map(|a| a.sample_count()).unwrap_or(1),
+ mask: !0,
+ alpha_to_coverage_enabled: false,
}
}
diff --git a/wgpu/src/triangle/gradient.rs b/wgpu/src/triangle/gradient.rs
new file mode 100644
index 00000000..b06cbac6
--- /dev/null
+++ b/wgpu/src/triangle/gradient.rs
@@ -0,0 +1,268 @@
+use crate::buffer::dynamic;
+use crate::settings;
+use crate::triangle;
+use encase::ShaderType;
+use glam::{IVec4, Vec4};
+use iced_graphics::gradient::Gradient;
+use iced_graphics::Transformation;
+
+pub struct Pipeline {
+ pipeline: wgpu::RenderPipeline,
+ pub(super) uniform_buffer: dynamic::Buffer<Uniforms>,
+ pub(super) storage_buffer: dynamic::Buffer<Storage>,
+ color_stop_offset: i32,
+ //Need to store these and then write them all at once
+ //or else they will be padded to 256 and cause gaps in the storage buffer
+ color_stops_pending_write: Storage,
+ bind_group_layout: wgpu::BindGroupLayout,
+ bind_group: wgpu::BindGroup,
+}
+
+#[derive(Debug, ShaderType)]
+pub(super) struct Uniforms {
+ transform: glam::Mat4,
+ //xy = start, zw = end
+ direction: Vec4,
+ //x = start stop, y = end stop, zw = padding
+ stop_range: IVec4,
+}
+
+#[derive(Debug, ShaderType)]
+pub(super) struct ColorStop {
+ color: Vec4,
+ offset: f32,
+}
+
+#[derive(ShaderType)]
+pub(super) struct Storage {
+ #[size(runtime)]
+ pub color_stops: Vec<ColorStop>,
+}
+
+impl Pipeline {
+ /// Creates a new [GradientPipeline] using `gradient.wgsl` shader.
+ pub(super) fn new(
+ device: &wgpu::Device,
+ format: wgpu::TextureFormat,
+ antialiasing: Option<settings::Antialiasing>,
+ ) -> Self {
+ let uniform_buffer = dynamic::Buffer::uniform(
+ device,
+ "iced_wgpu::triangle::gradient uniforms",
+ );
+
+ //Note: with a WASM target storage buffers are not supported. Will need to use UBOs & static
+ // sized array (eg like the 32-sized array on OpenGL side right now) to make gradients work
+ let storage_buffer = dynamic::Buffer::storage(
+ device,
+ "iced_wgpu::triangle::gradient storage",
+ );
+
+ let bind_group_layout =
+ device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("iced_wgpu::triangle::gradient bind group layout"),
+ entries: &[
+ wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Uniform,
+ has_dynamic_offset: true,
+ min_binding_size: Some(Uniforms::min_size()),
+ },
+ count: None,
+ },
+ wgpu::BindGroupLayoutEntry {
+ binding: 1,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Storage {
+ read_only: true,
+ },
+ has_dynamic_offset: false,
+ min_binding_size: Some(Storage::min_size()),
+ },
+ count: None,
+ },
+ ],
+ });
+
+ let bind_group = Pipeline::bind_group(
+ device,
+ uniform_buffer.raw(),
+ storage_buffer.raw(),
+ &bind_group_layout,
+ );
+
+ let layout =
+ device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+ label: Some("iced_wgpu::triangle::gradient pipeline layout"),
+ bind_group_layouts: &[&bind_group_layout],
+ push_constant_ranges: &[],
+ });
+
+ let shader =
+ device.create_shader_module(wgpu::ShaderModuleDescriptor {
+ label: Some(
+ "iced_wgpu::triangle::gradient create shader module",
+ ),
+ source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(
+ include_str!("../shader/gradient.wgsl"),
+ )),
+ });
+
+ let pipeline =
+ device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some("iced_wgpu::triangle::gradient pipeline"),
+ layout: Some(&layout),
+ vertex: wgpu::VertexState {
+ module: &shader,
+ entry_point: "vs_main",
+ buffers: &[triangle::vertex_buffer_layout()],
+ },
+ fragment: Some(wgpu::FragmentState {
+ module: &shader,
+ entry_point: "fs_main",
+ targets: &[triangle::fragment_target(format)],
+ }),
+ primitive: triangle::primitive_state(),
+ depth_stencil: None,
+ multisample: triangle::multisample_state(antialiasing),
+ multiview: None,
+ });
+
+ Self {
+ pipeline,
+ uniform_buffer,
+ storage_buffer,
+ color_stop_offset: 0,
+ color_stops_pending_write: Storage {
+ color_stops: vec![],
+ },
+ bind_group_layout,
+ bind_group,
+ }
+ }
+
+ /// Pushes a new gradient uniform to the CPU buffer.
+ pub fn push(&mut self, transform: Transformation, gradient: &Gradient) {
+ match gradient {
+ Gradient::Linear(linear) => {
+ let start_offset = self.color_stop_offset;
+ let end_offset =
+ (linear.color_stops.len() as i32) + start_offset - 1;
+
+ self.uniform_buffer.push(&Uniforms {
+ transform: transform.into(),
+ direction: Vec4::new(
+ linear.start.x,
+ linear.start.y,
+ linear.end.x,
+ linear.end.y,
+ ),
+ stop_range: IVec4::new(start_offset, end_offset, 0, 0),
+ });
+
+ self.color_stop_offset = end_offset + 1;
+
+ let stops: Vec<ColorStop> = linear
+ .color_stops
+ .iter()
+ .map(|stop| {
+ let [r, g, b, a] = stop.color.into_linear();
+
+ ColorStop {
+ offset: stop.offset,
+ color: Vec4::new(r, g, b, a),
+ }
+ })
+ .collect();
+
+ self.color_stops_pending_write.color_stops.extend(stops);
+ }
+ }
+ }
+
+ fn bind_group(
+ device: &wgpu::Device,
+ uniform_buffer: &wgpu::Buffer,
+ storage_buffer: &wgpu::Buffer,
+ layout: &wgpu::BindGroupLayout,
+ ) -> wgpu::BindGroup {
+ device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("iced_wgpu::triangle::gradient bind group"),
+ layout,
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::Buffer(
+ wgpu::BufferBinding {
+ buffer: uniform_buffer,
+ offset: 0,
+ size: Some(Uniforms::min_size()),
+ },
+ ),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: storage_buffer.as_entire_binding(),
+ },
+ ],
+ })
+ }
+
+ /// Writes the contents of the gradient CPU buffer to the GPU buffer, resizing the GPU buffer
+ /// beforehand if necessary.
+ pub fn write(
+ &mut self,
+ device: &wgpu::Device,
+ staging_belt: &mut wgpu::util::StagingBelt,
+ encoder: &mut wgpu::CommandEncoder,
+ ) {
+ //first write the pending color stops to the CPU buffer
+ self.storage_buffer.push(&self.color_stops_pending_write);
+
+ //resize buffers if needed
+ let uniforms_resized = self.uniform_buffer.resize(device);
+ let storage_resized = self.storage_buffer.resize(device);
+
+ if uniforms_resized || storage_resized {
+ //recreate bind groups if any buffers were resized
+ self.bind_group = Pipeline::bind_group(
+ device,
+ self.uniform_buffer.raw(),
+ self.storage_buffer.raw(),
+ &self.bind_group_layout,
+ );
+ }
+
+ //write to GPU
+ self.uniform_buffer.write(device, staging_belt, encoder);
+ self.storage_buffer.write(device, staging_belt, encoder);
+
+ //cleanup
+ self.color_stop_offset = 0;
+ self.color_stops_pending_write.color_stops.clear();
+ }
+
+ pub fn set_render_pass_pipeline<'a>(
+ &'a self,
+ render_pass: &mut wgpu::RenderPass<'a>,
+ ) {
+ render_pass.set_pipeline(&self.pipeline);
+ }
+
+ /// Configures the current render pass to draw the gradient at its offset stored in the
+ /// [DynamicBuffer] at [index].
+ pub fn configure_render_pass<'a>(
+ &'a self,
+ render_pass: &mut wgpu::RenderPass<'a>,
+ count: usize,
+ ) {
+ render_pass.set_bind_group(
+ 0,
+ &self.bind_group,
+ &[self.uniform_buffer.offset_at_index(count)],
+ )
+ }
+}
diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs
new file mode 100644
index 00000000..2e1052f2
--- /dev/null
+++ b/wgpu/src/triangle/solid.rs
@@ -0,0 +1,170 @@
+use crate::buffer::dynamic;
+use crate::triangle;
+use crate::{settings, Color};
+use encase::ShaderType;
+use glam::Vec4;
+use iced_graphics::Transformation;
+
+pub struct Pipeline {
+ pipeline: wgpu::RenderPipeline,
+ pub(super) buffer: dynamic::Buffer<Uniforms>,
+ bind_group_layout: wgpu::BindGroupLayout,
+ bind_group: wgpu::BindGroup,
+}
+
+#[derive(Debug, Clone, Copy, ShaderType)]
+pub(super) struct Uniforms {
+ transform: glam::Mat4,
+ color: Vec4,
+}
+
+impl Uniforms {
+ pub fn new(transform: Transformation, color: Color) -> Self {
+ let [r, g, b, a] = color.into_linear();
+
+ Self {
+ transform: transform.into(),
+ color: Vec4::new(r, g, b, a),
+ }
+ }
+}
+
+impl Pipeline {
+ /// Creates a new [SolidPipeline] using `solid.wgsl` shader.
+ pub fn new(
+ device: &wgpu::Device,
+ format: wgpu::TextureFormat,
+ antialiasing: Option<settings::Antialiasing>,
+ ) -> Self {
+ let buffer = dynamic::Buffer::uniform(
+ device,
+ "iced_wgpu::triangle::solid uniforms",
+ );
+
+ let bind_group_layout =
+ device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("iced_wgpu::triangle::solid bind group layout"),
+ entries: &[wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Uniform,
+ has_dynamic_offset: true,
+ min_binding_size: Some(Uniforms::min_size()),
+ },
+ count: None,
+ }],
+ });
+
+ let bind_group =
+ Pipeline::bind_group(device, buffer.raw(), &bind_group_layout);
+
+ let layout =
+ device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+ label: Some("iced_wgpu::triangle::solid pipeline layout"),
+ bind_group_layouts: &[&bind_group_layout],
+ push_constant_ranges: &[],
+ });
+
+ let shader =
+ device.create_shader_module(wgpu::ShaderModuleDescriptor {
+ label: Some("iced_wgpu::triangle::solid create shader module"),
+ source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(
+ include_str!("../shader/solid.wgsl"),
+ )),
+ });
+
+ let pipeline =
+ device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some("iced_wgpu::triangle::solid pipeline"),
+ layout: Some(&layout),
+ vertex: wgpu::VertexState {
+ module: &shader,
+ entry_point: "vs_main",
+ buffers: &[triangle::vertex_buffer_layout()],
+ },
+ fragment: Some(wgpu::FragmentState {
+ module: &shader,
+ entry_point: "fs_main",
+ targets: &[triangle::fragment_target(format)],
+ }),
+ primitive: triangle::primitive_state(),
+ depth_stencil: None,
+ multisample: triangle::multisample_state(antialiasing),
+ multiview: None,
+ });
+
+ Self {
+ pipeline,
+ buffer,
+ bind_group_layout,
+ bind_group,
+ }
+ }
+
+ fn bind_group(
+ device: &wgpu::Device,
+ buffer: &wgpu::Buffer,
+ layout: &wgpu::BindGroupLayout,
+ ) -> wgpu::BindGroup {
+ device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("iced_wgpu::triangle::solid bind group"),
+ layout,
+ entries: &[wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
+ buffer,
+ offset: 0,
+ size: Some(Uniforms::min_size()),
+ }),
+ }],
+ })
+ }
+
+ /// Pushes a new solid uniform to the CPU buffer.
+ pub fn push(&mut self, transform: Transformation, color: &Color) {
+ self.buffer.push(&Uniforms::new(transform, *color));
+ }
+
+ /// Writes the contents of the solid CPU buffer to the GPU buffer, resizing the GPU buffer
+ /// beforehand if necessary.
+ pub fn write(
+ &mut self,
+ device: &wgpu::Device,
+ staging_belt: &mut wgpu::util::StagingBelt,
+ encoder: &mut wgpu::CommandEncoder,
+ ) {
+ let uniforms_resized = self.buffer.resize(device);
+
+ if uniforms_resized {
+ self.bind_group = Pipeline::bind_group(
+ device,
+ self.buffer.raw(),
+ &self.bind_group_layout,
+ )
+ }
+
+ self.buffer.write(device, staging_belt, encoder);
+ }
+
+ pub fn set_render_pass_pipeline<'a>(
+ &'a self,
+ render_pass: &mut wgpu::RenderPass<'a>,
+ ) {
+ render_pass.set_pipeline(&self.pipeline);
+ }
+
+ /// Configures the current render pass to draw the solid at its offset stored in the
+ /// [DynamicBuffer] at [index].
+ pub fn configure_render_pass<'a>(
+ &'a self,
+ render_pass: &mut wgpu::RenderPass<'a>,
+ count: usize,
+ ) {
+ render_pass.set_bind_group(
+ 0,
+ &self.bind_group,
+ &[self.buffer.offset_at_index(count)],
+ )
+ }
+}