From 40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 10:52:58 -0700 Subject: Adds linear gradient support to 2D meshes in the canvas widget. --- wgpu/src/triangle.rs | 605 +++++++++++++++++++++------------------------------ 1 file changed, 242 insertions(+), 363 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index fd06dddf..d632c26c 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -1,429 +1,308 @@ //! Draw meshes of triangles. use crate::{settings, Transformation}; -use iced_graphics::layer; +use core::fmt; +use std::fmt::Formatter; -use bytemuck::{Pod, Zeroable}; -use std::mem; +use iced_graphics::layer::Meshes; +use iced_graphics::shader::Shader; +use iced_graphics::Size; +use crate::buffers::buffer::{needs_recreate, StaticBuffer}; +use crate::triangle::gradient::GradientPipeline; +use crate::triangle::solid::SolidPipeline; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; +mod gradient; mod msaa; +mod solid; -const UNIFORM_BUFFER_SIZE: usize = 50; -const VERTEX_BUFFER_SIZE: usize = 10_000; -const INDEX_BUFFER_SIZE: usize = 10_000; - +/// Triangle pipeline for all mesh layers in a [`iced_graphics::Canvas`] widget. #[derive(Debug)] pub(crate) struct Pipeline { - pipeline: wgpu::RenderPipeline, blit: Option, - constants_layout: wgpu::BindGroupLayout, - constants: wgpu::BindGroup, - uniforms_buffer: Buffer, - vertex_buffer: Buffer, - index_buffer: Buffer, + // these are optional so we don't allocate any memory to the GPU if + // application has no triangle meshes. + vertex_buffer: Option, + index_buffer: Option, + pipelines: TrianglePipelines, } -#[derive(Debug)] -struct Buffer { - label: &'static str, - raw: wgpu::Buffer, - size: usize, - usage: wgpu::BufferUsages, - _type: std::marker::PhantomData, +/// Supported triangle pipelines for different fills. Both use the same vertex shader. +pub(crate) struct TrianglePipelines { + solid: SolidPipeline, + gradient: GradientPipeline, } -impl Buffer { - 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::() * size) as u64, - usage, - mapped_at_creation: false, - }); - - Buffer { - label, - raw, - size, - usage, - _type: std::marker::PhantomData, - } +impl fmt::Debug for TrianglePipelines { + 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::() * size) as u64, - usage: self.usage, - mapped_at_creation: false, - }); - - self.size = size; - } +impl TrianglePipelines { + /// 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 GL programs, listed in [TrianglePipelines]. pub fn new( device: &wgpu::Device, format: wgpu::TextureFormat, antialiasing: Option, ) -> 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::() 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::() 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, - wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, - ), - index_buffer: Buffer::new( - "iced_wgpu::triangle index buffer", - device, - INDEX_BUFFER_SIZE, - wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST, - ), + vertex_buffer: None, + index_buffer: None, + pipelines: TrianglePipelines { + solid: SolidPipeline::new(device, format, antialiasing), + gradient: GradientPipeline::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, transformation: Transformation, scale_factor: f32, - meshes: &[layer::Mesh<'_>], + meshes: &Meshes<'_>, ) { - // 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::() as u64, - ), - }, - ), - }], - }); + //count the total number of vertices & indices we need to handle + let (total_vertices, total_indices) = meshes.attribute_count(); + println!("total vertices: {}, total indices: {}", total_vertices, total_indices); + + //Only create buffers if they need to be re-sized or don't exist + if needs_recreate(&self.vertex_buffer, total_vertices) { + //mapped to GPU at creation with total vertices + self.vertex_buffer = Some(StaticBuffer::new( + device, + "iced_wgpu::triangle vertex buffer", + //TODO: a more reasonable default to prevent frequent resizing calls + // before this was 10_000 + (std::mem::size_of::() * total_vertices) as u64, + wgpu::BufferUsages::VERTEX, + meshes.0.len(), + )) } - let mut uniforms: Vec = 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; - - // 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::() * last_vertex) as u64, - vertices_size, - device, - ); + if needs_recreate(&self.index_buffer, total_indices) { + //mapped to GPU at creation with total indices + self.index_buffer = Some(StaticBuffer::new( + device, + "iced_wgpu::triangle index buffer", + //TODO: a more reasonable default to prevent frequent resizing calls + // before this was 10_000 + (std::mem::size_of::() * total_indices) as u64, + wgpu::BufferUsages::INDEX, + meshes.0.len(), + )); + } - vertex_buffer.copy_from_slice(vertices); + if let Some(vertex_buffer) = &mut self.vertex_buffer { + if let Some(index_buffer) = &mut self.index_buffer { + let mut offset_v = 0; + let mut offset_i = 0; + //TODO: store this more efficiently + let mut indices_lengths = Vec::with_capacity(meshes.0.len()); + + //iterate through meshes to write all attribute data + for mesh in meshes.0.iter() { + let transform = transformation + * Transformation::translate( + mesh.origin.x, + mesh.origin.y, + ); + + println!("Mesh attribute data: Vertex: {:?}, Index: {:?}", mesh.buffers.vertices, mesh.buffers.indices); + + let vertices = bytemuck::cast_slice(&mesh.buffers.vertices); + let indices = bytemuck::cast_slice(&mesh.buffers.indices); + + //TODO: it's (probably) more efficient to reduce this write command and + // iterate first and then upload + println!("vertex buffer len: {}, index length: {}", vertices.len(), indices.len()); + vertex_buffer.write(offset_v, vertices); + index_buffer.write(offset_i, indices); + + offset_v += vertices.len() as u64; + offset_i += indices.len() as u64; + indices_lengths.push(mesh.buffers.indices.len()); + + match mesh.shader { + Shader::Solid(color) => { + self.pipelines.solid.push(transform, color); + } + Shader::Gradient(gradient) => { + self.pipelines.gradient.push(transform, gradient); + } + } } + //done writing to gpu buffer, unmap from host memory since we don't need it + //anymore + vertex_buffer.flush(); + index_buffer.flush(); + + //resize & memcpy uniforms from CPU buffers to GPU buffers for all pipelines + self.pipelines.write(device, staging_belt, encoder); + + //configure the render pass now that the data is uploaded to the GPU { - let mut index_buffer = staging_belt.write_buffer( - encoder, - &self.index_buffer.raw, - (std::mem::size_of::() * last_index) as u64, - indices_size, - device, + //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 { + label: Some("iced_wgpu::triangle render pass"), + color_attachments: &[Some( + wgpu::RenderPassColorAttachment { + view: attachment, + resolve_target, + ops: wgpu::Operations { load, store: true }, + }, + )], + depth_stencil_attachment: None, + }, ); - index_buffer.copy_from_slice(indices); + //TODO: do this a better way; store it in the respective pipelines perhaps + // to be more readable + let mut num_solids = 0; + let mut num_gradients = 0; + + //TODO: try to avoid this extra iteration if possible + for index in 0..meshes.0.len() { + let clip_bounds = + (meshes.0[index].clip_bounds * scale_factor).snap(); + + render_pass.set_scissor_rect( + clip_bounds.x, + clip_bounds.y, + clip_bounds.width, + clip_bounds.height, + ); + + match meshes.0[index].shader { + Shader::Solid(_) => { + self.pipelines.solid.configure_render_pass( + &mut render_pass, + num_solids, + ); + num_solids += 1; + } + Shader::Gradient(_) => { + self.pipelines.gradient.configure_render_pass( + &mut render_pass, + num_gradients, + ); + num_gradients += 1; + } + } + + render_pass.set_index_buffer( + index_buffer.slice_from_index::(index), + wgpu::IndexFormat::Uint32, + ); + + render_pass.set_vertex_buffer( + 0, + vertex_buffer.slice_from_index::(index), + ); + + render_pass.draw_indexed( + 0..(indices_lengths[index] as u32), + 0, + 0..1, + ); + } } - - uniforms.push(transform); - offsets.push(( - last_vertex as u64, - last_index as u64, - mesh.buffers.indices.len(), - )); - - last_vertex += mesh.buffers.vertices.len(); - last_index += mesh.buffers.indices.len(); - } - } - - let uniforms = bytemuck::cast_slice(&uniforms); - - if let Some(uniforms_size) = - wgpu::BufferSize::new(uniforms.len() as u64) - { - let mut uniforms_buffer = staging_belt.write_buffer( - encoder, - &self.uniforms_buffer.raw, - 0, - uniforms_size, - device, - ); - - uniforms_buffer.copy_from_slice(uniforms); - } - - { - 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) - }; - - let mut render_pass = - encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("iced_wgpu::triangle render pass"), - color_attachments: &[Some( - wgpu::RenderPassColorAttachment { - view: attachment, - resolve_target, - ops: wgpu::Operations { load, store: true }, - }, - )], - depth_stencil_attachment: None, - }); - - render_pass.set_pipeline(&self.pipeline); - - for (i, (vertex_offset, index_offset, indices)) in - offsets.into_iter().enumerate() - { - let clip_bounds = (meshes[i].clip_bounds * scale_factor).snap(); - - render_pass.set_scissor_rect( - clip_bounds.x, - clip_bounds.y, - clip_bounds.width, - clip_bounds.height, - ); - - render_pass.set_bind_group( - 0, - &self.constants, - &[(std::mem::size_of::() * i) as u32], - ); - - render_pass.set_index_buffer( - self.index_buffer - .raw - .slice(index_offset * mem::size_of::() as u64..), - wgpu::IndexFormat::Uint32, - ); - - render_pass.set_vertex_buffer( - 0, - self.vertex_buffer.raw.slice( - vertex_offset * mem::size_of::() as u64.., - ), - ); - - render_pass.draw_indexed(0..indices as u32, 0, 0..1); } } if let Some(blit) = &mut self.blit { blit.draw(encoder, target); } + + //cleanup + self.pipelines.clear(); } } -#[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::() 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 default_fragment_target( + texture_format: wgpu::TextureFormat, +) -> Option { + Some(wgpu::ColorTargetState { + format: texture_format, + blend: Some(wgpu::BlendState::ALPHA_BLENDING), + write_mask: wgpu::ColorWrites::ALL, + }) +} + +fn default_triangle_primitive_state() -> wgpu::PrimitiveState { + wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, + front_face: wgpu::FrontFace::Cw, + ..Default::default() } } -impl From for Uniforms { - fn from(transformation: Transformation) -> Uniforms { - Self { - transform: transformation.into(), - _padding_a: [0.0; 32], - _padding_b: [0.0; 16], - } +fn default_multisample_state( + antialiasing: Option, +) -> wgpu::MultisampleState { + wgpu::MultisampleState { + count: antialiasing.map(|a| a.sample_count()).unwrap_or(1), + mask: !0, + alpha_to_coverage_enabled: false, } } -- cgit From 0f434c74d68d32ecbf2362d1edbac66976dcd8ab Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 16:11:05 -0700 Subject: Removed some leftover debugging. --- wgpu/src/triangle.rs | 4 ---- 1 file changed, 4 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index d632c26c..f1770e9a 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -92,7 +92,6 @@ impl Pipeline { ) { //count the total number of vertices & indices we need to handle let (total_vertices, total_indices) = meshes.attribute_count(); - println!("total vertices: {}, total indices: {}", total_vertices, total_indices); //Only create buffers if they need to be re-sized or don't exist if needs_recreate(&self.vertex_buffer, total_vertices) { @@ -136,14 +135,11 @@ impl Pipeline { mesh.origin.y, ); - println!("Mesh attribute data: Vertex: {:?}, Index: {:?}", mesh.buffers.vertices, mesh.buffers.indices); - let vertices = bytemuck::cast_slice(&mesh.buffers.vertices); let indices = bytemuck::cast_slice(&mesh.buffers.indices); //TODO: it's (probably) more efficient to reduce this write command and // iterate first and then upload - println!("vertex buffer len: {}, index length: {}", vertices.len(), indices.len()); vertex_buffer.write(offset_v, vertices); index_buffer.write(offset_i, indices); -- cgit From 6e7b3ced0b1daf368e44e181ecdb4ae529877eb6 Mon Sep 17 00:00:00 2001 From: shan Date: Tue, 4 Oct 2022 18:24:46 -0700 Subject: Reworked wgpu buffers, updated glow side to have proper transform location storage, attempting to fix visibility modifiers, implemented some of the feedback received in initial PR. --- wgpu/src/triangle.rs | 303 ++++++++++++++++++++++++--------------------------- 1 file changed, 143 insertions(+), 160 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index f1770e9a..df5e3132 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -3,11 +3,11 @@ use crate::{settings, Transformation}; use core::fmt; use std::fmt::Formatter; -use iced_graphics::layer::Meshes; +use iced_graphics::layer::{attribute_count_of, Mesh}; use iced_graphics::shader::Shader; use iced_graphics::Size; -use crate::buffers::buffer::{needs_recreate, StaticBuffer}; +use crate::buffers::buffer::StaticBuffer; use crate::triangle::gradient::GradientPipeline; use crate::triangle::solid::SolidPipeline; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; @@ -20,10 +20,9 @@ mod solid; #[derive(Debug)] pub(crate) struct Pipeline { blit: Option, - // these are optional so we don't allocate any memory to the GPU if - // application has no triangle meshes. - vertex_buffer: Option, - index_buffer: Option, + vertex_buffer: StaticBuffer, + index_buffer: StaticBuffer, + index_strides: Vec, pipelines: TrianglePipelines, } @@ -69,8 +68,17 @@ impl Pipeline { ) -> Pipeline { Pipeline { blit: antialiasing.map(|a| msaa::Blit::new(device, format, a)), - vertex_buffer: None, - index_buffer: None, + vertex_buffer: StaticBuffer::new( + device, + "iced_wgpu::triangle vertex buffer", + wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + ), + index_buffer: StaticBuffer::new( + device, + "iced_wgpu::triangle vertex buffer", + wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST, + ), + index_strides: Vec::new(), pipelines: TrianglePipelines { solid: SolidPipeline::new(device, format, antialiasing), gradient: GradientPipeline::new(device, format, antialiasing), @@ -88,177 +96,152 @@ impl Pipeline { target_size: Size, transformation: Transformation, scale_factor: f32, - meshes: &Meshes<'_>, + meshes: &[Mesh<'_>], ) { - //count the total number of vertices & indices we need to handle - let (total_vertices, total_indices) = meshes.attribute_count(); + //count the total amount of vertices & indices we need to handle + let (total_vertices, total_indices) = attribute_count_of(meshes); + + // Then we ensure the current attribute buffers are big enough, resizing if necessary + // with wgpu this means recreating the buffer. + + //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.recreate_if_needed(device, total_vertices); + let _ = self.index_buffer.recreate_if_needed(device, total_indices); + + //prepare dynamic buffers & data store for writing + self.index_strides.clear(); + self.pipelines.clear(); + + let mut vertex_offset = 0; + let mut index_offset = 0; - //Only create buffers if they need to be re-sized or don't exist - if needs_recreate(&self.vertex_buffer, total_vertices) { - //mapped to GPU at creation with total vertices - self.vertex_buffer = Some(StaticBuffer::new( + for mesh in meshes { + let transform = transformation + * Transformation::translate(mesh.origin.x, mesh.origin.y); + + //write to both buffers + let new_vertex_offset = self.vertex_buffer.write( device, - "iced_wgpu::triangle vertex buffer", - //TODO: a more reasonable default to prevent frequent resizing calls - // before this was 10_000 - (std::mem::size_of::() * total_vertices) as u64, - wgpu::BufferUsages::VERTEX, - meshes.0.len(), - )) - } + staging_belt, + encoder, + vertex_offset, + &mesh.buffers.vertices, + ); - if needs_recreate(&self.index_buffer, total_indices) { - //mapped to GPU at creation with total indices - self.index_buffer = Some(StaticBuffer::new( + let new_index_offset = self.index_buffer.write( device, - "iced_wgpu::triangle index buffer", - //TODO: a more reasonable default to prevent frequent resizing calls - // before this was 10_000 - (std::mem::size_of::() * total_indices) as u64, - wgpu::BufferUsages::INDEX, - meshes.0.len(), - )); - } + staging_belt, + encoder, + index_offset, + &mesh.buffers.indices, + ); - if let Some(vertex_buffer) = &mut self.vertex_buffer { - if let Some(index_buffer) = &mut self.index_buffer { - let mut offset_v = 0; - let mut offset_i = 0; - //TODO: store this more efficiently - let mut indices_lengths = Vec::with_capacity(meshes.0.len()); - - //iterate through meshes to write all attribute data - for mesh in meshes.0.iter() { - let transform = transformation - * Transformation::translate( - mesh.origin.x, - mesh.origin.y, - ); + vertex_offset = vertex_offset + new_vertex_offset; + index_offset = index_offset + new_index_offset; - let vertices = bytemuck::cast_slice(&mesh.buffers.vertices); - let indices = bytemuck::cast_slice(&mesh.buffers.indices); - - //TODO: it's (probably) more efficient to reduce this write command and - // iterate first and then upload - vertex_buffer.write(offset_v, vertices); - index_buffer.write(offset_i, indices); - - offset_v += vertices.len() as u64; - offset_i += indices.len() as u64; - indices_lengths.push(mesh.buffers.indices.len()); - - match mesh.shader { - Shader::Solid(color) => { - self.pipelines.solid.push(transform, color); - } - Shader::Gradient(gradient) => { - self.pipelines.gradient.push(transform, gradient); - } - } + self.index_strides.push(mesh.buffers.indices.len() as u32); + + //push uniform data to CPU buffers + match mesh.shader { + Shader::Solid(color) => { + self.pipelines.solid.push(transform, color); } + Shader::Gradient(gradient) => { + self.pipelines.gradient.push(transform, gradient); + } + } + } - //done writing to gpu buffer, unmap from host memory since we don't need it - //anymore - vertex_buffer.flush(); - index_buffer.flush(); - - //resize & memcpy uniforms from CPU buffers to GPU buffers for all pipelines - self.pipelines.write(device, staging_belt, encoder); - - //configure the render pass now that the data is uploaded to the GPU - { - //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 { - label: Some("iced_wgpu::triangle render pass"), - color_attachments: &[Some( - wgpu::RenderPassColorAttachment { - view: attachment, - resolve_target, - ops: wgpu::Operations { load, store: true }, - }, - )], - depth_stencil_attachment: None, + //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 + { + //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 { + label: Some("iced_wgpu::triangle render pass"), + color_attachments: &[Some( + wgpu::RenderPassColorAttachment { + view: attachment, + resolve_target, + ops: wgpu::Operations { load, store: true }, }, - ); - - //TODO: do this a better way; store it in the respective pipelines perhaps - // to be more readable - let mut num_solids = 0; - let mut num_gradients = 0; - - //TODO: try to avoid this extra iteration if possible - for index in 0..meshes.0.len() { - let clip_bounds = - (meshes.0[index].clip_bounds * scale_factor).snap(); - - render_pass.set_scissor_rect( - clip_bounds.x, - clip_bounds.y, - clip_bounds.width, - clip_bounds.height, - ); - - match meshes.0[index].shader { - Shader::Solid(_) => { - self.pipelines.solid.configure_render_pass( - &mut render_pass, - num_solids, - ); - num_solids += 1; - } - Shader::Gradient(_) => { - self.pipelines.gradient.configure_render_pass( - &mut render_pass, - num_gradients, - ); - num_gradients += 1; - } - } - - render_pass.set_index_buffer( - index_buffer.slice_from_index::(index), - wgpu::IndexFormat::Uint32, + )], + depth_stencil_attachment: None, + }); + + //TODO I can't figure out a clean way to encapsulate these into their appropriate + // structs without displeasing the borrow checker due to the lifetime requirements of + // render_pass & using a mutable reference to each pipeline in a loop... + let mut num_solids = 0; + let mut num_gradients = 0; + + for (index, mesh) in meshes.iter().enumerate() { + let clip_bounds = (mesh.clip_bounds * scale_factor).snap(); + + render_pass.set_scissor_rect( + clip_bounds.x, + clip_bounds.y, + clip_bounds.width, + clip_bounds.height, + ); + + match mesh.shader { + Shader::Solid(_) => { + self.pipelines.solid.configure_render_pass( + &mut render_pass, + num_solids, ); - - render_pass.set_vertex_buffer( - 0, - vertex_buffer.slice_from_index::(index), - ); - - render_pass.draw_indexed( - 0..(indices_lengths[index] as u32), - 0, - 0..1, + num_solids += 1; + } + Shader::Gradient(_) => { + self.pipelines.gradient.configure_render_pass( + &mut render_pass, + num_gradients, ); + num_gradients += 1; } - } + }; + + render_pass.set_vertex_buffer( + 0, + self.vertex_buffer.slice_from_index(index), + ); + + render_pass.set_index_buffer( + self.index_buffer.slice_from_index(index), + wgpu::IndexFormat::Uint32, + ); + + render_pass.draw_indexed( + 0..(self.index_strides[index] as u32), + 0, + 0..1, + ); } } if let Some(blit) = &mut self.blit { blit.draw(encoder, target); } - - //cleanup - self.pipelines.clear(); } } -- cgit From 30432cbade3d9b25c4df62656a7494db3f4ea82a Mon Sep 17 00:00:00 2001 From: shan Date: Wed, 5 Oct 2022 10:49:58 -0700 Subject: Readjusted namespaces, removed Geometry example as it's no longer relevant. --- wgpu/src/triangle.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index df5e3132..c22f118c 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -4,13 +4,13 @@ use core::fmt; use std::fmt::Formatter; use iced_graphics::layer::{attribute_count_of, Mesh}; -use iced_graphics::shader::Shader; -use iced_graphics::Size; +use iced_graphics::{layer, Size}; -use crate::buffers::buffer::StaticBuffer; +use crate::buffers::StaticBuffer; use crate::triangle::gradient::GradientPipeline; use crate::triangle::solid::SolidPipeline; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; +use layer::mesh; mod gradient; mod msaa; @@ -107,7 +107,9 @@ impl Pipeline { //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.recreate_if_needed(device, total_vertices); + let _ = self + .vertex_buffer + .recreate_if_needed(device, total_vertices); let _ = self.index_buffer.recreate_if_needed(device, total_indices); //prepare dynamic buffers & data store for writing @@ -144,11 +146,11 @@ impl Pipeline { self.index_strides.push(mesh.buffers.indices.len() as u32); //push uniform data to CPU buffers - match mesh.shader { - Shader::Solid(color) => { + match mesh.style { + mesh::Style::Solid(color) => { self.pipelines.solid.push(transform, color); } - Shader::Gradient(gradient) => { + mesh::Style::Gradient(gradient) => { self.pipelines.gradient.push(transform, gradient); } } @@ -204,15 +206,15 @@ impl Pipeline { clip_bounds.height, ); - match mesh.shader { - Shader::Solid(_) => { + match mesh.style { + mesh::Style::Solid(_) => { self.pipelines.solid.configure_render_pass( &mut render_pass, num_solids, ); num_solids += 1; } - Shader::Gradient(_) => { + mesh::Style::Gradient(_) => { self.pipelines.gradient.configure_render_pass( &mut render_pass, num_gradients, -- cgit From e540b7f6428a6648a696494c6d9dda20663375fa Mon Sep 17 00:00:00 2001 From: shan Date: Wed, 5 Oct 2022 11:23:36 -0700 Subject: Fixed issue with offsets not being reset properly leading to borked draws. --- wgpu/src/triangle.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index c22f118c..a7ad7b77 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -241,6 +241,9 @@ impl Pipeline { } } + self.vertex_buffer.clear(); + self.index_buffer.clear(); + if let Some(blit) = &mut self.blit { blit.draw(encoder, target); } -- cgit From cb7c4676543cd508dfae8d4dcbd9cc8b61b1a94e Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 07:28:05 -0700 Subject: Fixed lint issues & cleaned up some documentation. --- wgpu/src/triangle.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index a7ad7b77..6e35be3c 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -26,7 +26,7 @@ pub(crate) struct Pipeline { pipelines: TrianglePipelines, } -/// Supported triangle pipelines for different fills. Both use the same vertex shader. +/// Supported triangle pipelines for different fills. pub(crate) struct TrianglePipelines { solid: SolidPipeline, gradient: GradientPipeline, -- cgit From 72feba51bed41db0bc04b43167d5d3b43007fd44 Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 19:13:40 -0700 Subject: Fixed some imports/documentation. --- wgpu/src/triangle.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index 6e35be3c..791c9833 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -101,8 +101,7 @@ impl Pipeline { //count the total amount of vertices & indices we need to handle let (total_vertices, total_indices) = attribute_count_of(meshes); - // Then we ensure the current attribute buffers are big enough, resizing if necessary - // with wgpu this means recreating the buffer. + // Then we ensure the current attribute buffers are big enough, resizing if necessary. //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 @@ -190,9 +189,6 @@ impl Pipeline { depth_stencil_attachment: None, }); - //TODO I can't figure out a clean way to encapsulate these into their appropriate - // structs without displeasing the borrow checker due to the lifetime requirements of - // render_pass & using a mutable reference to each pipeline in a loop... let mut num_solids = 0; let mut num_gradients = 0; -- cgit From f9a6efcaa03728f43aaa105af8936c1ed4778388 Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 19:41:00 -0700 Subject: Fixed some more imports/documentation. --- wgpu/src/triangle.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index 791c9833..48ddf28a 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -12,9 +12,9 @@ use crate::triangle::solid::SolidPipeline; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; use layer::mesh; +mod solid; mod gradient; mod msaa; -mod solid; /// Triangle pipeline for all mesh layers in a [`iced_graphics::Canvas`] widget. #[derive(Debug)] @@ -60,7 +60,7 @@ impl TrianglePipelines { } impl Pipeline { - /// Creates supported GL programs, listed in [TrianglePipelines]. + /// Creates supported pipelines, listed in [TrianglePipelines]. pub fn new( device: &wgpu::Device, format: wgpu::TextureFormat, -- cgit From 215e6c95be7370bdd29c75b904463f06f942b7c0 Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 7 Oct 2022 13:21:32 -0700 Subject: More import adjusting. --- wgpu/src/triangle.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index 48ddf28a..feca72dc 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -3,14 +3,13 @@ use crate::{settings, Transformation}; use core::fmt; use std::fmt::Formatter; -use iced_graphics::layer::{attribute_count_of, Mesh}; -use iced_graphics::{layer, Size}; +use iced_graphics::layer::{Mesh, mesh, mesh::attribute_count_of}; +use iced_graphics::Size; use crate::buffers::StaticBuffer; use crate::triangle::gradient::GradientPipeline; use crate::triangle::solid::SolidPipeline; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; -use layer::mesh; mod solid; mod gradient; -- cgit From c4565759e4294540f54a81e4d91ddea7a769d3d4 Mon Sep 17 00:00:00 2001 From: bungoboingo Date: Tue, 18 Oct 2022 15:18:37 -0700 Subject: Cleaned up namespaces re: PR comments. --- wgpu/src/triangle.rs | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index feca72dc..0956c57d 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -3,12 +3,10 @@ use crate::{settings, Transformation}; use core::fmt; use std::fmt::Formatter; -use iced_graphics::layer::{Mesh, mesh, mesh::attribute_count_of}; +use iced_graphics::layer::{Mesh, mesh}; use iced_graphics::Size; use crate::buffers::StaticBuffer; -use crate::triangle::gradient::GradientPipeline; -use crate::triangle::solid::SolidPipeline; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; mod solid; @@ -22,22 +20,22 @@ pub(crate) struct Pipeline { vertex_buffer: StaticBuffer, index_buffer: StaticBuffer, index_strides: Vec, - pipelines: TrianglePipelines, + pipelines: PipelineList, } /// Supported triangle pipelines for different fills. -pub(crate) struct TrianglePipelines { - solid: SolidPipeline, - gradient: GradientPipeline, +pub(crate) struct PipelineList { + solid: solid::Pipeline, + gradient: gradient::Pipeline, } -impl fmt::Debug for TrianglePipelines { +impl fmt::Debug for PipelineList { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { f.debug_struct("TrianglePipelines").finish() } } -impl TrianglePipelines { +impl PipelineList { /// Resets each pipeline's buffers. fn clear(&mut self) { self.solid.buffer.clear(); @@ -78,9 +76,9 @@ impl Pipeline { wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST, ), index_strides: Vec::new(), - pipelines: TrianglePipelines { - solid: SolidPipeline::new(device, format, antialiasing), - gradient: GradientPipeline::new(device, format, antialiasing), + pipelines: PipelineList { + solid: solid::Pipeline::new(device, format, antialiasing), + gradient: gradient::Pipeline::new(device, format, antialiasing), }, } } @@ -98,7 +96,7 @@ impl Pipeline { meshes: &[Mesh<'_>], ) { //count the total amount of vertices & indices we need to handle - let (total_vertices, total_indices) = attribute_count_of(meshes); + let (total_vertices, total_indices) = mesh::attribute_count_of(meshes); // Then we ensure the current attribute buffers are big enough, resizing if necessary. @@ -107,8 +105,8 @@ impl Pipeline { //the majority of use cases. Therefore we will write GPU data every frame (for now). let _ = self .vertex_buffer - .recreate_if_needed(device, total_vertices); - let _ = self.index_buffer.recreate_if_needed(device, total_indices); + .resize(device, total_vertices); + let _ = self.index_buffer.resize(device, total_indices); //prepare dynamic buffers & data store for writing self.index_strides.clear(); @@ -258,7 +256,7 @@ fn vertex_buffer_layout<'a>() -> wgpu::VertexBufferLayout<'a> { } } -fn default_fragment_target( +fn fragment_target( texture_format: wgpu::TextureFormat, ) -> Option { Some(wgpu::ColorTargetState { @@ -268,7 +266,7 @@ fn default_fragment_target( }) } -fn default_triangle_primitive_state() -> wgpu::PrimitiveState { +fn primitive_state() -> wgpu::PrimitiveState { wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleList, front_face: wgpu::FrontFace::Cw, @@ -276,7 +274,7 @@ fn default_triangle_primitive_state() -> wgpu::PrimitiveState { } } -fn default_multisample_state( +fn multisample_state( antialiasing: Option, ) -> wgpu::MultisampleState { wgpu::MultisampleState { -- cgit From b95745340441835bd25b5cadc2342254631f8c05 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 04:35:16 +0100 Subject: Run `cargo fmt` --- wgpu/src/triangle.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index 0956c57d..a21e0fac 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -3,15 +3,15 @@ use crate::{settings, Transformation}; use core::fmt; use std::fmt::Formatter; -use iced_graphics::layer::{Mesh, mesh}; +use iced_graphics::layer::{mesh, Mesh}; use iced_graphics::Size; use crate::buffers::StaticBuffer; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; -mod solid; mod gradient; mod msaa; +mod solid; /// Triangle pipeline for all mesh layers in a [`iced_graphics::Canvas`] widget. #[derive(Debug)] @@ -103,9 +103,7 @@ impl Pipeline { //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.vertex_buffer.resize(device, total_vertices); let _ = self.index_buffer.resize(device, total_indices); //prepare dynamic buffers & data store for writing -- cgit From 7e22e2d45293c5916812be03dc7367134b69b3ad Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 04:53:27 +0100 Subject: Fix lints by `clippy` --- wgpu/src/triangle.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index a21e0fac..2af35588 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -134,8 +134,8 @@ impl Pipeline { &mesh.buffers.indices, ); - vertex_offset = vertex_offset + new_vertex_offset; - index_offset = index_offset + new_index_offset; + vertex_offset += new_vertex_offset; + index_offset += new_index_offset; self.index_strides.push(mesh.buffers.indices.len() as u32); -- cgit From 99cf98971dae22ae65adb2104c5a3eec578649f1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 05:00:35 +0100 Subject: Rename `buffers` module to `buffer` ... and move `StaticBuffer` to nested `static` module --- wgpu/src/triangle.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index 2af35588..28051edf 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -6,7 +6,7 @@ use std::fmt::Formatter; use iced_graphics::layer::{mesh, Mesh}; use iced_graphics::Size; -use crate::buffers::StaticBuffer; +use crate::buffer::r#static::Buffer; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; mod gradient; @@ -17,8 +17,8 @@ mod solid; #[derive(Debug)] pub(crate) struct Pipeline { blit: Option, - vertex_buffer: StaticBuffer, - index_buffer: StaticBuffer, + vertex_buffer: Buffer, + index_buffer: Buffer, index_strides: Vec, pipelines: PipelineList, } @@ -65,12 +65,12 @@ impl Pipeline { ) -> Pipeline { Pipeline { blit: antialiasing.map(|a| msaa::Blit::new(device, format, a)), - vertex_buffer: StaticBuffer::new( + vertex_buffer: Buffer::new( device, "iced_wgpu::triangle vertex buffer", wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, ), - index_buffer: StaticBuffer::new( + index_buffer: Buffer::new( device, "iced_wgpu::triangle vertex buffer", wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST, -- cgit From 93e309f491a8941bafb919e75d660e65071475f4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 05:06:09 +0100 Subject: Reuse last set pipeline for `triangle` in `iced_wgpu` --- wgpu/src/triangle.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index 28051edf..6e64e189 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -186,6 +186,7 @@ impl Pipeline { let mut num_solids = 0; let mut num_gradients = 0; + let mut last_is_solid = None; for (index, mesh) in meshes.iter().enumerate() { let clip_bounds = (mesh.clip_bounds * scale_factor).snap(); @@ -199,17 +200,35 @@ impl Pipeline { match mesh.style { mesh::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; } mesh::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; } }; -- cgit From 84d1b79fefc88534835fdfbe79bc0eb3b43627cf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 05:50:53 +0100 Subject: Move `mesh::Style` to `triangle` and reuse it in `fill` and `stroke` --- wgpu/src/triangle.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index 6e64e189..765f7d30 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -3,7 +3,8 @@ use crate::{settings, Transformation}; use core::fmt; use std::fmt::Formatter; -use iced_graphics::layer::{mesh, Mesh}; +use iced_graphics::layer::mesh::{self, Mesh}; +use iced_graphics::triangle; use iced_graphics::Size; use crate::buffer::r#static::Buffer; @@ -141,10 +142,10 @@ impl Pipeline { //push uniform data to CPU buffers match mesh.style { - mesh::Style::Solid(color) => { + triangle::Style::Solid(color) => { self.pipelines.solid.push(transform, color); } - mesh::Style::Gradient(gradient) => { + triangle::Style::Gradient(gradient) => { self.pipelines.gradient.push(transform, gradient); } } @@ -199,7 +200,7 @@ impl Pipeline { ); match mesh.style { - mesh::Style::Solid(_) => { + triangle::Style::Solid(_) => { if !last_is_solid.unwrap_or(false) { self.pipelines .solid @@ -215,7 +216,7 @@ impl Pipeline { num_solids += 1; } - mesh::Style::Gradient(_) => { + triangle::Style::Gradient(_) => { if last_is_solid.unwrap_or(true) { self.pipelines .gradient -- cgit From f31c8f2504ea7c004c5caed8913e5da28d2e50e2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 06:05:23 +0100 Subject: Refactor imports of `triangle` modules in `iced_glow` and `iced_wgpu` --- wgpu/src/triangle.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'wgpu/src/triangle.rs') diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index 765f7d30..f9abf2b5 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -1,18 +1,18 @@ //! Draw meshes of triangles. -use crate::{settings, Transformation}; -use core::fmt; -use std::fmt::Formatter; +mod gradient; +mod msaa; +mod solid; + +use crate::buffer::r#static::Buffer; +use crate::settings; +use crate::Transformation; use iced_graphics::layer::mesh::{self, Mesh}; -use iced_graphics::triangle; +use iced_graphics::triangle::{self, Vertex2D}; use iced_graphics::Size; -use crate::buffer::r#static::Buffer; -pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; - -mod gradient; -mod msaa; -mod solid; +use core::fmt; +use std::fmt::Formatter; /// Triangle pipeline for all mesh layers in a [`iced_graphics::Canvas`] widget. #[derive(Debug)] -- cgit