]> git.nega.tv - josh/narcissus/commitdiff
shark-shaders: Shrink glyph draw command slightly
authorJosh Simmons <josh@nega.tv>
Sun, 10 Nov 2024 13:15:51 +0000 (14:15 +0100)
committerJosh Simmons <josh@nega.tv>
Sun, 10 Nov 2024 13:15:51 +0000 (14:15 +0100)
title/shark-shaders/shaders/draw_2d.h
title/shark-shaders/shaders/draw_2d_bin_1_scatter.comp
title/shark-shaders/shaders/draw_2d_bin_3_resolve.comp
title/shark-shaders/shaders/draw_2d_rasterize.comp
title/shark-shaders/src/pipelines.rs

index b39368eadb6431dced0c4ee07d0b035f7df671a0..f79e3dd135de1e78a5c0c60d7c301937820f9054 100644 (file)
@@ -20,7 +20,7 @@ struct Glyph {
 };
 
 struct Draw2dCmd {
-    uint type;
+    uint packed_type;
     uint words[7];
 };
 
@@ -50,9 +50,9 @@ Draw2dCmdRect decode_rect(Draw2dCmd cmd) {
 
 Draw2dCmdGlyph decode_glyph(Draw2dCmd cmd) {
     return Draw2dCmdGlyph(
-        cmd.words[0],
-        vec2(uintBitsToFloat(cmd.words[1]), uintBitsToFloat(cmd.words[2])),
-        cmd.words[3]
+        cmd.packed_type & 0xffffff,
+        vec2(uintBitsToFloat(cmd.words[0]), uintBitsToFloat(cmd.words[1])),
+        cmd.words[2]
     );
 }
 
index 7c117833c9e92079512af602eb286556eb7d7c64..935c380db30c19a2f8318caac7e1625c4d592572 100644 (file)
@@ -47,11 +47,11 @@ void main() {
     vec2 cmd_max = vec2(-99999.9);
     if (in_bounds) {
         const Draw2dCmd cmd = constants.draw_buffer.values[draw_index];
-        const uint type = cmd.type;
+        const uint cmd_type = cmd.packed_type >> 24;
         for (;;) {
-            const uint scalar_type = subgroupBroadcastFirst(type);
+            const uint scalar_type = subgroupBroadcastFirst(cmd_type);
             [[branch]]
-            if (scalar_type == type) {
+            if (scalar_type == cmd_type) {
                 switch (scalar_type) {
                     case DRAW_2D_CMD_RECT:
                         const Draw2dCmdRect cmd_rect = decode_rect(cmd);
index 9ffd05e8fbeccf5046ee10580a7cc3d0d48278a2..bde745d97b4231fcadcc633cd0e45ff70e3662f7 100644 (file)
@@ -85,10 +85,12 @@ void main() {
             vec2 cmd_max = vec2(-99999.9);
 
             const Draw2dCmd cmd = constants.draw_buffer.values[draw_index];
+            const uint cmd_type = cmd.packed_type >> 24;
+
             for (;;) {
-                const uint scalar_type = subgroupBroadcastFirst(cmd.type);
+                const uint scalar_type = subgroupBroadcastFirst(cmd_type);
                 [[branch]]
-                if (scalar_type == cmd.type) {
+                if (scalar_type == cmd_type) {
                     switch (scalar_type) {
                         case DRAW_2D_CMD_RECT:
                             const Draw2dCmdRect cmd_rect = decode_rect(cmd);
index bdcc6f1ba2471f01313ead5aa1a1fb9139201dc0..d36bf76da86adf55d88c53c9154b8aaca064fec8 100644 (file)
@@ -88,9 +88,10 @@ void main() {
 
             const uint base_index = (constants.coarse_buffer.values[i] & 0xffff) * 32;
             const Draw2dCmd cmd = constants.draw_buffer.values[base_index + index];
+            const uint cmd_type = cmd.packed_type >> 24;
 
             vec4 primitive_color = vec4(0.0);
-            switch (cmd.type) {
+            switch (cmd_type) {
                 case DRAW_2D_CMD_RECT:
                     const Draw2dCmdRect cmd_rect = decode_rect(cmd);
                     const vec2 rect_min = cmd_rect.position - cmd_rect.half_extent - cmd_rect.border_width;
index 9eb81551dd4ef8f92fbb5ad6e96dca67d5e3ebfc..5a4fb366410f00782e5ca11bf8b9ab83be182ad7 100644 (file)
@@ -19,7 +19,7 @@ pub struct Vertex {
     pub texcoord: [f32; 4],
 }
 
-#[repr(u32)]
+#[repr(u8)]
 enum Draw2dCmdType {
     Rect,
     Glyph,
@@ -32,23 +32,23 @@ pub union Draw2dCmd {
     glyph: CmdGlyph,
 }
 
+const _: () = assert!(std::mem::size_of::<Draw2dCmd>() == 32);
+
 #[repr(C)]
 #[derive(Clone, Copy)]
 struct CmdGlyph {
-    r#type: u32,
-    index: u32,
+    packed: u32,
     x: f32,
     y: f32,
     color: u32,
-    _padding: [u8; 12],
 }
 
-const _: () = assert!(std::mem::size_of::<CmdGlyph>() == std::mem::size_of::<Draw2dCmd>());
+const _: () = assert!(std::mem::size_of::<CmdGlyph>() == 16);
 
 #[repr(C)]
 #[derive(Clone, Copy)]
 struct CmdRect {
-    r#type: u32,
+    packed: u32,
     border_width: f32,
     x: f32,
     y: f32,
@@ -58,19 +58,18 @@ struct CmdRect {
     border_color: u32,
 }
 
-const _: () = assert!(std::mem::size_of::<CmdRect>() == std::mem::size_of::<Draw2dCmd>());
+const _: () = assert!(std::mem::size_of::<CmdRect>() == 32);
 
 impl Draw2dCmd {
     #[inline(always)]
-    pub fn glyph(glyph_index: TouchedGlyphIndex, color: u32, x: f32, y: f32) -> Self {
+    pub fn glyph(touched_glyph_index: TouchedGlyphIndex, color: u32, x: f32, y: f32) -> Self {
         Self {
             glyph: CmdGlyph {
-                r#type: Draw2dCmdType::Glyph as u32,
-                index: glyph_index.as_u32(),
+                packed: (Draw2dCmdType::Glyph as u32) << 24
+                    | (touched_glyph_index.as_u32() & 0xffffff),
                 x,
                 y,
                 color,
-                _padding: default(),
             },
         }
     }
@@ -87,7 +86,7 @@ impl Draw2dCmd {
     ) -> Self {
         Self {
             rect: CmdRect {
-                r#type: Draw2dCmdType::Rect as u32,
+                packed: (Draw2dCmdType::Rect as u32) << 24,
                 border_width,
                 x,
                 y,