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/solid.rs | 169 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 wgpu/src/triangle/solid.rs (limited to 'wgpu/src/triangle/solid.rs') diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs new file mode 100644 index 00000000..a3cbd72b --- /dev/null +++ b/wgpu/src/triangle/solid.rs @@ -0,0 +1,169 @@ +use crate::buffers::dynamic_buffers::DynamicBuffer; +use crate::triangle::{ + default_fragment_target, default_multisample_state, + default_triangle_primitive_state, vertex_buffer_layout, +}; +use crate::{settings, Color}; +use encase::ShaderType; +use glam::Vec4; +use iced_graphics::Transformation; + +pub(super) struct SolidPipeline { + pipeline: wgpu::RenderPipeline, + pub(super) buffer: DynamicBuffer, + bind_group_layout: wgpu::BindGroupLayout, + bind_group: wgpu::BindGroup, +} + +#[derive(Debug, Clone, Copy, ShaderType)] +pub(super) struct SolidUniforms { + transform: glam::Mat4, + color: Vec4, +} + +impl SolidUniforms { + pub fn new(transform: Transformation, color: Color) -> Self { + Self { + transform: transform.into(), + color: Vec4::new(color.r, color.g, color.b, color.a), + } + } +} + +impl SolidPipeline { + /// Creates a new [SolidPipeline] using `triangle_solid.wgsl` shader. + pub fn new( + device: &wgpu::Device, + format: wgpu::TextureFormat, + antialiasing: Option, + ) -> Self { + let buffer = DynamicBuffer::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(SolidUniforms::min_size()), + }, + count: None, + }], + }); + + let bind_group = SolidPipeline::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/triangle_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: &[vertex_buffer_layout()], + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_solid", + targets: &[default_fragment_target(format)], + }), + primitive: default_triangle_primitive_state(), + depth_stencil: None, + multisample: default_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(SolidUniforms::min_size()), + }), + }], + }) + } + + /// Pushes a new solid uniform to the CPU buffer. + pub fn push(&mut self, transform: Transformation, color: &Color) { + self.buffer.push(&SolidUniforms::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 = SolidPipeline::bind_group( + device, + self.buffer.raw(), + &self.bind_group_layout, + ) + } + + self.buffer.write(device, staging_belt, encoder); + } + + /// 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>, + index: usize, + ) { + render_pass.set_pipeline(&self.pipeline); + + render_pass.set_bind_group( + 0, + &self.bind_group, + &[self.buffer.offset_at_index(index)], + ); + } +} \ No newline at end of file -- 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/solid.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'wgpu/src/triangle/solid.rs') diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs index a3cbd72b..e7e9098a 100644 --- a/wgpu/src/triangle/solid.rs +++ b/wgpu/src/triangle/solid.rs @@ -8,15 +8,15 @@ use encase::ShaderType; use glam::Vec4; use iced_graphics::Transformation; -pub(super) struct SolidPipeline { +pub struct SolidPipeline { pipeline: wgpu::RenderPipeline, - pub(super) buffer: DynamicBuffer, + pub(crate) buffer: DynamicBuffer, bind_group_layout: wgpu::BindGroupLayout, bind_group: wgpu::BindGroup, } #[derive(Debug, Clone, Copy, ShaderType)] -pub(super) struct SolidUniforms { +pub struct SolidUniforms { transform: glam::Mat4, color: Vec4, } @@ -156,14 +156,13 @@ impl SolidPipeline { pub fn configure_render_pass<'a>( &'a self, render_pass: &mut wgpu::RenderPass<'a>, - index: usize, + count: usize, ) { render_pass.set_pipeline(&self.pipeline); - render_pass.set_bind_group( 0, &self.bind_group, - &[self.buffer.offset_at_index(index)], - ); + &[self.buffer.offset_at_index(count)], + ) } -} \ No newline at end of file +} -- 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/solid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src/triangle/solid.rs') diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs index e7e9098a..d2b4d13b 100644 --- a/wgpu/src/triangle/solid.rs +++ b/wgpu/src/triangle/solid.rs @@ -1,4 +1,4 @@ -use crate::buffers::dynamic_buffers::DynamicBuffer; +use crate::buffers::dynamic::DynamicBuffer; use crate::triangle::{ default_fragment_target, default_multisample_state, default_triangle_primitive_state, vertex_buffer_layout, -- 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/solid.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'wgpu/src/triangle/solid.rs') diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs index d2b4d13b..abba4851 100644 --- a/wgpu/src/triangle/solid.rs +++ b/wgpu/src/triangle/solid.rs @@ -10,13 +10,13 @@ use iced_graphics::Transformation; pub struct SolidPipeline { pipeline: wgpu::RenderPipeline, - pub(crate) buffer: DynamicBuffer, + pub(super) buffer: DynamicBuffer, bind_group_layout: wgpu::BindGroupLayout, bind_group: wgpu::BindGroup, } #[derive(Debug, Clone, Copy, ShaderType)] -pub struct SolidUniforms { +pub(super) struct SolidUniforms { transform: glam::Mat4, color: Vec4, } -- 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/solid.rs | 55 ++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 29 deletions(-) (limited to 'wgpu/src/triangle/solid.rs') diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs index abba4851..4000b059 100644 --- a/wgpu/src/triangle/solid.rs +++ b/wgpu/src/triangle/solid.rs @@ -1,27 +1,24 @@ -use crate::buffers::dynamic::DynamicBuffer; -use crate::triangle::{ - default_fragment_target, default_multisample_state, - default_triangle_primitive_state, vertex_buffer_layout, -}; +use crate::buffers::dynamic; +use crate::triangle; use crate::{settings, Color}; use encase::ShaderType; use glam::Vec4; use iced_graphics::Transformation; -pub struct SolidPipeline { +pub struct Pipeline { pipeline: wgpu::RenderPipeline, - pub(super) buffer: DynamicBuffer, + pub(super) buffer: dynamic::Buffer, bind_group_layout: wgpu::BindGroupLayout, bind_group: wgpu::BindGroup, } #[derive(Debug, Clone, Copy, ShaderType)] -pub(super) struct SolidUniforms { +pub(super) struct Uniforms { transform: glam::Mat4, color: Vec4, } -impl SolidUniforms { +impl Uniforms { pub fn new(transform: Transformation, color: Color) -> Self { Self { transform: transform.into(), @@ -30,34 +27,34 @@ impl SolidUniforms { } } -impl SolidPipeline { - /// Creates a new [SolidPipeline] using `triangle_solid.wgsl` shader. +impl Pipeline { + /// Creates a new [SolidPipeline] using `solid.wgsl` shader. pub fn new( device: &wgpu::Device, format: wgpu::TextureFormat, antialiasing: Option, ) -> Self { - let buffer = DynamicBuffer::uniform( + let buffer = dynamic::Buffer::uniform( device, - "iced_wgpu::triangle [SOLID] uniforms", + "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"), + 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(SolidUniforms::min_size()), + min_binding_size: Some(Uniforms::min_size()), }, count: None, }], }); - let bind_group = SolidPipeline::bind_group( + let bind_group = Pipeline::bind_group( device, &buffer.raw(), &bind_group_layout, @@ -65,36 +62,36 @@ impl SolidPipeline { let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some("iced_wgpu::triangle [SOLID] pipeline layout"), + 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"), + label: Some("iced_wgpu::triangle::solid create shader module"), source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( - include_str!("../shader/triangle_solid.wgsl"), + include_str!("../shader/solid.wgsl"), )), }); let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some("iced_wgpu::triangle [SOLID] pipeline"), + label: Some("iced_wgpu::triangle::solid pipeline"), layout: Some(&layout), vertex: wgpu::VertexState { module: &shader, entry_point: "vs_main", - buffers: &[vertex_buffer_layout()], + buffers: &[triangle::vertex_buffer_layout()], }, fragment: Some(wgpu::FragmentState { module: &shader, - entry_point: "fs_solid", - targets: &[default_fragment_target(format)], + entry_point: "fs_main", + targets: &[triangle::fragment_target(format)], }), - primitive: default_triangle_primitive_state(), + primitive: triangle::primitive_state(), depth_stencil: None, - multisample: default_multisample_state(antialiasing), + multisample: triangle::multisample_state(antialiasing), multiview: None, }); @@ -112,14 +109,14 @@ impl SolidPipeline { layout: &wgpu::BindGroupLayout, ) -> wgpu::BindGroup { device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("iced_wgpu::triangle [SOLID] bind group"), + 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(SolidUniforms::min_size()), + size: Some(Uniforms::min_size()), }), }], }) @@ -127,7 +124,7 @@ impl SolidPipeline { /// Pushes a new solid uniform to the CPU buffer. pub fn push(&mut self, transform: Transformation, color: &Color) { - self.buffer.push(&SolidUniforms::new(transform, *color)); + self.buffer.push(&Uniforms::new(transform, *color)); } /// Writes the contents of the solid CPU buffer to the GPU buffer, resizing the GPU buffer @@ -141,7 +138,7 @@ impl SolidPipeline { let uniforms_resized = self.buffer.resize(device); if uniforms_resized { - self.bind_group = SolidPipeline::bind_group( + self.bind_group = Pipeline::bind_group( device, self.buffer.raw(), &self.bind_group_layout, -- cgit From 20a0577034b40a6bbabee9bbbfc085f3fd5016c0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 04:33:54 +0100 Subject: Reuse last buffer in `Frame` if `mesh_style` matches --- wgpu/src/triangle/solid.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'wgpu/src/triangle/solid.rs') diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs index 4000b059..0373ebee 100644 --- a/wgpu/src/triangle/solid.rs +++ b/wgpu/src/triangle/solid.rs @@ -54,11 +54,8 @@ impl Pipeline { }], }); - let bind_group = Pipeline::bind_group( - device, - &buffer.raw(), - &bind_group_layout, - ); + let bind_group = + Pipeline::bind_group(device, &buffer.raw(), &bind_group_layout); let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { -- cgit From 62465842099908f9e50b8edabfec709b37b1ade3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 04:37:23 +0100 Subject: Convert colors to linear RGB before uploading in `solid` pipelines --- wgpu/src/triangle/solid.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'wgpu/src/triangle/solid.rs') diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs index 0373ebee..75455310 100644 --- a/wgpu/src/triangle/solid.rs +++ b/wgpu/src/triangle/solid.rs @@ -20,9 +20,11 @@ pub(super) struct Uniforms { 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(color.r, color.g, color.b, color.a), + color: Vec4::new(r, g, b, a), } } } -- 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/solid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src/triangle/solid.rs') diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs index 75455310..9d85ff7b 100644 --- a/wgpu/src/triangle/solid.rs +++ b/wgpu/src/triangle/solid.rs @@ -57,7 +57,7 @@ impl Pipeline { }); let bind_group = - Pipeline::bind_group(device, &buffer.raw(), &bind_group_layout); + Pipeline::bind_group(device, buffer.raw(), &bind_group_layout); let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { -- 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/solid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src/triangle/solid.rs') diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs index 9d85ff7b..6b5dad41 100644 --- a/wgpu/src/triangle/solid.rs +++ b/wgpu/src/triangle/solid.rs @@ -1,4 +1,4 @@ -use crate::buffers::dynamic; +use crate::buffer::dynamic; use crate::triangle; use crate::{settings, Color}; use encase::ShaderType; -- 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/solid.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'wgpu/src/triangle/solid.rs') diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs index 6b5dad41..2e1052f2 100644 --- a/wgpu/src/triangle/solid.rs +++ b/wgpu/src/triangle/solid.rs @@ -147,6 +147,13 @@ impl Pipeline { 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>( @@ -154,7 +161,6 @@ impl Pipeline { render_pass: &mut wgpu::RenderPass<'a>, count: usize, ) { - render_pass.set_pipeline(&self.pipeline); render_pass.set_bind_group( 0, &self.bind_group, -- cgit