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. --- glow/src/triangle/gradient.rs | 92 ++++++++++++++++++++++++------------------- glow/src/triangle/solid.rs | 62 ++++++++++++++++++----------- 2 files changed, 91 insertions(+), 63 deletions(-) (limited to 'glow/src/triangle') diff --git a/glow/src/triangle/gradient.rs b/glow/src/triangle/gradient.rs index d1b10d77..547871e2 100644 --- a/glow/src/triangle/gradient.rs +++ b/glow/src/triangle/gradient.rs @@ -1,18 +1,42 @@ use crate::program::Version; -use crate::triangle::{simple_triangle_program, update_transform}; +use crate::triangle::{simple_triangle_program, set_transform}; use glow::{Context, HasContext, NativeProgram}; +use iced_graphics::gradient::Linear; use iced_graphics::gradient::Gradient; -use iced_graphics::widget::canvas::gradient::Linear; use iced_graphics::Transformation; #[derive(Debug)] -pub(super) struct GradientProgram { - pub(super) program: ::Program, - pub(super) uniform_data: GradientUniformData, +pub struct GradientProgram { + pub program: ::Program, + pub uniform_data: GradientUniformData, +} + +#[derive(Debug)] +pub struct GradientUniformData { + gradient: Gradient, + transform: Transformation, + uniform_locations: GradientUniformLocations, +} + +#[derive(Debug)] +struct GradientUniformLocations { + gradient_start_location: ::UniformLocation, + gradient_end_location: ::UniformLocation, + color_stops_size_location: ::UniformLocation, + //currently the maximum number of stops is 64 due to needing to allocate the + //memory for the array of stops with a const value in GLSL + color_stops_locations: [ColorStopLocation; 64], + transform_location: ::UniformLocation, +} + +#[derive(Copy, Debug, Clone)] +struct ColorStopLocation { + color: ::UniformLocation, + offset: ::UniformLocation, } impl GradientProgram { - pub(super) fn new(gl: &Context, shader_version: &Version) -> Self { + pub fn new(gl: &Context, shader_version: &Version) -> Self { let program = simple_triangle_program( gl, shader_version, @@ -25,15 +49,17 @@ impl GradientProgram { } } - pub(super) fn set_uniforms<'a>( + pub fn write_uniforms( &mut self, gl: &Context, gradient: &Gradient, - transform: Option, + transform: &Transformation, ) { - update_transform(gl, self.program, transform); + if transform != &self.uniform_data.transform { + set_transform(gl, self.uniform_data.uniform_locations.transform_location, *transform); + } - if &self.uniform_data.current_gradient != gradient { + if &self.uniform_data.gradient != gradient { match gradient { Gradient::Linear(linear) => { let gradient_start: [f32; 2] = (linear.start).into(); @@ -104,31 +130,17 @@ impl GradientProgram { } } - self.uniform_data.current_gradient = gradient.clone(); + self.uniform_data.gradient = gradient.clone(); } } -} - -#[derive(Debug)] -pub(super) struct GradientUniformData { - current_gradient: Gradient, - uniform_locations: GradientUniformLocations, -} -#[derive(Debug)] -struct GradientUniformLocations { - gradient_start_location: ::UniformLocation, - gradient_end_location: ::UniformLocation, - color_stops_size_location: ::UniformLocation, - //currently the maximum number of stops is 64 due to needing to allocate the - //memory for the array of stops with a const value in GLSL - color_stops_locations: [ColorStopLocation; 64], -} + pub fn use_program(&mut self, gl: &glow::Context, gradient: &Gradient, transform: &Transformation) { + unsafe { + gl.use_program(Some(self.program)) + } -#[derive(Copy, Debug, Clone)] -struct ColorStopLocation { - color: ::UniformLocation, - offset: ::UniformLocation, + self.write_uniforms(gl, gradient, transform); + } } impl GradientUniformData { @@ -153,10 +165,7 @@ impl GradientUniformData { &format!("color_stop_offsets[{}]", index), ) } - .expect(&format!( - "Gradient - Color stop offset with index {}", - index - )); + .expect("Gradient - Color stop offset location."); let color = unsafe { gl.get_uniform_location( @@ -164,25 +173,28 @@ impl GradientUniformData { &format!("color_stop_colors[{}]", index), ) } - .expect(&format!( - "Gradient - Color stop colors with index {}", - index - )); + .expect("Gradient - Color stop color location."); ColorStopLocation { color, offset } }); + let transform_location = + unsafe { gl.get_uniform_location(program, "u_Transform") } + .expect("Get transform location."); + GradientUniformData { - current_gradient: Gradient::Linear(Linear { + gradient: Gradient::Linear(Linear { start: Default::default(), end: Default::default(), color_stops: vec![], }), + transform: Transformation::identity(), uniform_locations: GradientUniformLocations { gradient_start_location, gradient_end_location, color_stops_size_location, color_stops_locations, + transform_location, }, } } diff --git a/glow/src/triangle/solid.rs b/glow/src/triangle/solid.rs index 3a33cea8..d5b73eb9 100644 --- a/glow/src/triangle/solid.rs +++ b/glow/src/triangle/solid.rs @@ -1,13 +1,38 @@ use crate::program::Version; -use crate::triangle::{simple_triangle_program, update_transform}; +use crate::triangle::{set_transform, simple_triangle_program}; use crate::Color; use glow::{Context, HasContext, NativeProgram}; use iced_graphics::Transformation; #[derive(Debug)] pub struct SolidProgram { - pub(crate) program: ::Program, - pub(crate) uniform_data: SolidUniformData, + program: ::Program, + uniform_data: SolidUniformData, +} + +#[derive(Debug)] +pub(crate) struct SolidUniformData { + pub color: Color, + pub color_location: ::UniformLocation, + pub transform: Transformation, + pub transform_location: ::UniformLocation, +} + +impl SolidUniformData { + fn new(gl: &Context, program: NativeProgram) -> Self { + Self { + color: Color::TRANSPARENT, + color_location: unsafe { + gl.get_uniform_location(program, "color") + } + .expect("Solid - Color uniform location."), + transform: Transformation::identity(), + transform_location: unsafe { + gl.get_uniform_location(program, "u_Transform") + } + .expect("Get transform location."), + } + } } impl SolidProgram { @@ -24,15 +49,17 @@ impl SolidProgram { } } - pub fn set_uniforms<'a>( + pub fn write_uniforms( &mut self, gl: &Context, color: &Color, - transform: Option, + transform: &Transformation, ) { - update_transform(gl, self.program, transform); + if transform != &self.uniform_data.transform { + set_transform(gl, self.uniform_data.transform_location, *transform) + } - if &self.uniform_data.color != color { + if color != &self.uniform_data.color { unsafe { gl.uniform_4_f32( Some(&self.uniform_data.color_location), @@ -46,22 +73,11 @@ impl SolidProgram { self.uniform_data.color = *color; } } -} - -#[derive(Debug)] -pub(crate) struct SolidUniformData { - pub color: Color, - pub color_location: ::UniformLocation, -} -impl SolidUniformData { - fn new(gl: &Context, program: NativeProgram) -> Self { - Self { - color: Color::TRANSPARENT, - color_location: unsafe { - gl.get_uniform_location(program, "color") - } - .expect("Solid - Color uniform location."), + pub fn use_program(&mut self, gl: &glow::Context, color: &Color, transform: &Transformation) { + unsafe { + gl.use_program(Some(self.program)) } + self.write_uniforms(gl, color, transform) } -} +} \ No newline at end of file -- cgit