From f5896540e0913cdf90f04e7a6843b6e1219bb3fe Mon Sep 17 00:00:00 2001 From: Josh Simmons Date: Mon, 10 Jun 2024 08:38:29 +0200 Subject: [PATCH] shark: Fix out-of-bounds writes in bin clear --- title/shark-shaders/shaders/compute_bindings.h | 1 + .../shaders/primitive_2d_bin_clear.comp.glsl | 10 ++++++++-- title/shark/src/main.rs | 2 ++ title/shark/src/pipelines/mod.rs | 5 ++++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/title/shark-shaders/shaders/compute_bindings.h b/title/shark-shaders/shaders/compute_bindings.h index f8cb41e..88498ed 100644 --- a/title/shark-shaders/shaders/compute_bindings.h +++ b/title/shark-shaders/shaders/compute_bindings.h @@ -36,6 +36,7 @@ layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer Ti struct ComputeUniforms { uvec2 screen_resolution; uvec2 atlas_resolution; + uvec2 tile_resolution; uint num_primitives; uint num_primitives_32; diff --git a/title/shark-shaders/shaders/primitive_2d_bin_clear.comp.glsl b/title/shark-shaders/shaders/primitive_2d_bin_clear.comp.glsl index d9e138b..5707be1 100644 --- a/title/shark-shaders/shaders/primitive_2d_bin_clear.comp.glsl +++ b/title/shark-shaders/shaders/primitive_2d_bin_clear.comp.glsl @@ -17,6 +17,12 @@ layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in; void main() { - uniforms.tiles.values[gl_GlobalInvocationID.x * TILE_STRIDE + TILE_BITMAP_RANGE_OFFSET + 0] = 0xffffffff; - uniforms.tiles.values[gl_GlobalInvocationID.x * TILE_STRIDE + TILE_BITMAP_RANGE_OFFSET + 1] = 0; + if (gl_GlobalInvocationID.x >= uniforms.tile_resolution.x * uniforms.tile_resolution.y) { + return; + } + + const uint index = gl_GlobalInvocationID.x * TILE_STRIDE + TILE_BITMAP_RANGE_OFFSET; + + uniforms.tiles.values[index + 0] = 0xffffffff; + uniforms.tiles.values[index + 1] = 0; } diff --git a/title/shark/src/main.rs b/title/shark/src/main.rs index 6e8aeb6..2830aa2 100644 --- a/title/shark/src/main.rs +++ b/title/shark/src/main.rs @@ -1471,6 +1471,8 @@ impl<'gpu> DrawState<'gpu> { num_primitives, num_primitives_32, num_primitives_1024, + tile_resolution_x: self.tile_resolution_x, + tile_resolution_y: self.tile_resolution_y, tile_stride: self.tile_resolution_x, glyphs_buffer: gpu.get_buffer_address(glyph_buffer.to_arg()), glyph_instances_buffer: gpu diff --git a/title/shark/src/pipelines/mod.rs b/title/shark/src/pipelines/mod.rs index 3f38de7..4464b1a 100644 --- a/title/shark/src/pipelines/mod.rs +++ b/title/shark/src/pipelines/mod.rs @@ -13,13 +13,16 @@ pub const TILE_STRIDE: u32 = TILE_BITMAP_WORDS_L0 + TILE_BITMAP_WORDS_L1 + 2; pub struct PrimitiveUniforms { pub screen_resolution_x: u32, pub screen_resolution_y: u32, + pub atlas_resolution_x: u32, pub atlas_resolution_y: u32, + pub tile_resolution_x: u32, + pub tile_resolution_y: u32, + pub num_primitives: u32, pub num_primitives_32: u32, pub num_primitives_1024: u32, - pub tile_stride: u32, pub glyphs_buffer: u64, -- 2.49.0