From 91561e8c3585a00d8180c7080f1e1b5542785ab7 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Sat, 24 Dec 2022 23:21:58 +0100 Subject: [PATCH] wgpu: Reduce gradient.ratios uniform memory by 4x --- render/wgpu/shaders/gradient_uniform.wgsl | 19 ++++++++----------- render/wgpu/src/lib.rs | 6 +++--- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/render/wgpu/shaders/gradient_uniform.wgsl b/render/wgpu/shaders/gradient_uniform.wgsl index be1ff2840..5341678ee 100644 --- a/render/wgpu/shaders/gradient_uniform.wgsl +++ b/render/wgpu/shaders/gradient_uniform.wgsl @@ -1,15 +1,8 @@ /// Shader used for drawing all flavors of gradients. -struct wrapped_f32 { - elem: f32, - _padding1: f32, - _padding2: f32, - _padding3: f32, -} - struct Gradient { colors: array, 16>, - ratios: array, + ratios: array, 4u>, // secretly array but this let's us squeeze it into alignment gradient_type: i32, num_colors: u32, repeat_mode: i32, @@ -33,6 +26,10 @@ fn main_vertex(in: VertexInput) -> VertexOutput { return VertexOutput(pos, uv); } +fn ratio(i: u32) -> f32 { + return gradient.ratios[i / 4u][i % 4u]; +} + @fragment fn main_fragment(in: VertexOutput) -> @location(0) vec4 { let last = gradient.num_colors - 1u; @@ -91,17 +88,17 @@ fn main_fragment(in: VertexOutput) -> @location(0) vec4 { break; } } - t = clamp(t, gradient.ratios[0].elem, gradient.ratios[last].elem); + t = clamp(t, ratio(0u), ratio(last)); // Find the two gradient colors bordering our position. var j: u32; - for( j = 1u; t > gradient.ratios[j].elem; j = j + 1u) { + for( j = 1u; t > ratio(j); j = j + 1u) { // Noop } let i = j - 1u; // Lerp between the two colors. - let a = (t - gradient.ratios[i].elem) / (gradient.ratios[j].elem - gradient.ratios[i].elem); + let a = (t - ratio(i)) / (ratio(j) - ratio(i)); var color: vec4 = mix(gradient.colors[i], gradient.colors[j], a); if( gradient.interpolation != 0 ) { color = linear_to_srgb(color); diff --git a/render/wgpu/src/lib.rs b/render/wgpu/src/lib.rs index 58af6cc68..2e9c5ee43 100644 --- a/render/wgpu/src/lib.rs +++ b/render/wgpu/src/lib.rs @@ -108,7 +108,7 @@ impl From for Vertex { #[derive(Copy, Clone, Debug, Pod, Zeroable)] struct GradientUniforms { colors: [[f32; 4]; 16], - ratios: [[f32; 4]; 16], + ratios: [f32; 16], gradient_type: i32, num_colors: u32, repeat_mode: i32, @@ -119,11 +119,11 @@ struct GradientUniforms { impl From for GradientUniforms { fn from(gradient: TessGradient) -> Self { - let mut ratios = [[0.0; 4]; 16]; + let mut ratios = [0.0; 16]; let mut colors = [[0.0; 4]; 16]; for i in 0..gradient.num_colors { - ratios[i] = [gradient.ratios[i], 0.0, 0.0, 0.0]; + ratios[i] = gradient.ratios[i]; colors[i].copy_from_slice(&gradient.colors[i]); }