wgpu: Reduce gradient.ratios uniform memory by 4x

This commit is contained in:
Nathan Adams 2022-12-24 23:21:58 +01:00
parent 312e72451f
commit 91561e8c35
2 changed files with 11 additions and 14 deletions

View File

@ -1,15 +1,8 @@
/// Shader used for drawing all flavors of gradients. /// Shader used for drawing all flavors of gradients.
struct wrapped_f32 {
elem: f32,
_padding1: f32,
_padding2: f32,
_padding3: f32,
}
struct Gradient { struct Gradient {
colors: array<vec4<f32>, 16>, colors: array<vec4<f32>, 16>,
ratios: array<wrapped_f32,16u>, ratios: array<vec4<f32>, 4u>, // secretly array<f32; 16> but this let's us squeeze it into alignment
gradient_type: i32, gradient_type: i32,
num_colors: u32, num_colors: u32,
repeat_mode: i32, repeat_mode: i32,
@ -33,6 +26,10 @@ fn main_vertex(in: VertexInput) -> VertexOutput {
return VertexOutput(pos, uv); return VertexOutput(pos, uv);
} }
fn ratio(i: u32) -> f32 {
return gradient.ratios[i / 4u][i % 4u];
}
@fragment @fragment
fn main_fragment(in: VertexOutput) -> @location(0) vec4<f32> { fn main_fragment(in: VertexOutput) -> @location(0) vec4<f32> {
let last = gradient.num_colors - 1u; let last = gradient.num_colors - 1u;
@ -91,17 +88,17 @@ fn main_fragment(in: VertexOutput) -> @location(0) vec4<f32> {
break; 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. // Find the two gradient colors bordering our position.
var j: u32; var j: u32;
for( j = 1u; t > gradient.ratios[j].elem; j = j + 1u) { for( j = 1u; t > ratio(j); j = j + 1u) {
// Noop // Noop
} }
let i = j - 1u; let i = j - 1u;
// Lerp between the two colors. // 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<f32> = mix(gradient.colors[i], gradient.colors[j], a); var color: vec4<f32> = mix(gradient.colors[i], gradient.colors[j], a);
if( gradient.interpolation != 0 ) { if( gradient.interpolation != 0 ) {
color = linear_to_srgb(color); color = linear_to_srgb(color);

View File

@ -108,7 +108,7 @@ impl From<TessVertex> for Vertex {
#[derive(Copy, Clone, Debug, Pod, Zeroable)] #[derive(Copy, Clone, Debug, Pod, Zeroable)]
struct GradientUniforms { struct GradientUniforms {
colors: [[f32; 4]; 16], colors: [[f32; 4]; 16],
ratios: [[f32; 4]; 16], ratios: [f32; 16],
gradient_type: i32, gradient_type: i32,
num_colors: u32, num_colors: u32,
repeat_mode: i32, repeat_mode: i32,
@ -119,11 +119,11 @@ struct GradientUniforms {
impl From<TessGradient> for GradientUniforms { impl From<TessGradient> for GradientUniforms {
fn from(gradient: TessGradient) -> Self { 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]; let mut colors = [[0.0; 4]; 16];
for i in 0..gradient.num_colors { 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]); colors[i].copy_from_slice(&gradient.colors[i]);
} }