PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
gs: upload about 60% less data per frame
Samir Nuri06468becca0d
diff --git a/runtime/src/gs/gpu/gpu.cpp b/runtime/src/gs/gpu/gpu.cppindex e5f1d18..b6e1bb9 100644--- a/runtime/src/gs/gpu/gpu.cpp+++ b/runtime/src/gs/gpu/gpu.cpp@@ -12,8 +12,10 @@#include <cstdio>#include <cstdlib>#include <cstring>+#include <deque>#include <memory>#include <string>+#include <unordered_map>extern const char kGsShader[];extern const char kBlitShader[];@@ -32,7 +34,7 @@ const char *const kEntryPoints[kPipelines] = {"upload", "copy_read", "copy_writeconstexpr std::uint32_t kParamStride = 256;constexpr std::uint32_t kClutSlots = 2048;constexpr std::uint32_t kClutEntries = 512;-constexpr std::uint32_t kPrimWords = 40;+constexpr std::uint32_t kPrimWords = 28;constexpr std::uint32_t kSetupWords = 24;Mode g_mode = Mode::off;@@ -209,6 +211,88 @@ void dispatch_linear(Pipeline p, std::uint32_t items, std::uint32_t param) {g_commands.push_back({p, std::min(groups, 1024u), (groups + 1023) / 1024, param});}+constexpr std::uint32_t kCacheWords = 4u << 20;+constexpr std::size_t kCacheMinBytes = 256;++struct CacheEntry {+ std::uint64_t key;+ std::uint32_t offset, words;+ std::size_t bytes;+ std::uint64_t used;+};++WGPUBuffer g_cache = nullptr;+std::vector<std::uint32_t> g_cache_copy;+std::deque<CacheEntry> g_cache_ring;+std::unordered_multimap<std::uint64_t, CacheEntry *> g_cache_index;+std::unordered_map<std::uint64_t, std::uint32_t> g_cache_seen;+std::uint32_t g_cache_head = 0;+std::uint64_t g_serial = 1;++std::uint64_t content_key(const std::uint8_t *p, std::size_t n) {+ std::uint64_t h = static_cast<std::uint64_t>(n) * 0x9E3779B97F4A7C15ull;+ const auto mix = [&h](std::uint64_t v) {+ h ^= v + 0x9E3779B97F4A7C15ull + (h << 6) + (h >> 2);+ h *= 0xBF58476D1CE4E5B9ull;+ };+ const std::size_t step = std::max<std::size_t>(8, (n / 64) & ~static_cast<std::size_t>(7));+ std::uint64_t v;+ for (std::size_t i = 0; i + 8 <= n; i += step) {+ std::memcpy(&v, p + i, 8);+ mix(v);+ }+ v = 0;+ std::memcpy(&v, p + n - std::min<std::size_t>(n, 8), std::min<std::size_t>(n, 8));+ mix(v);+ return h;+}++bool cache_find(std::uint64_t key, const std::uint8_t *bytes, std::size_t n, std::uint32_t &offset) {+ const auto [first, last] = g_cache_index.equal_range(key);+ for (auto it = first; it != last; ++it) {+ CacheEntry &e = *it->second;+ if (e.bytes != n || std::memcmp(g_cache_copy.data() + e.offset, bytes, n) != 0) continue;+ e.used = g_serial;+ offset = e.offset;+ return true;+ }+ return false;+}++bool cache_insert(std::uint64_t key, const std::uint8_t *bytes, std::size_t n, std::uint32_t &offset) {+ const auto words = static_cast<std::uint32_t>((n + 3) / 4 + 1);+ if (words > kCacheWords / 4) return false;+ const auto evict_front = [] {+ CacheEntry &front = g_cache_ring.front();+ if (front.used == g_serial) return false;+ const auto [first, last] = g_cache_index.equal_range(front.key);+ for (auto it = first; it != last; ++it)+ if (it->second == &front) {+ g_cache_index.erase(it);+ break;+ }+ g_cache_ring.pop_front();+ return true;+ };+ if (g_cache_head + words > kCacheWords) {+ while (!g_cache_ring.empty() && g_cache_ring.front().offset >= g_cache_head)+ if (!evict_front()) return false;+ g_cache_head = 0;+ }+ while (!g_cache_ring.empty() && g_cache_ring.front().offset < g_cache_head + words)+ if (!evict_front()) return false;+ offset = g_cache_head;+ std::uint32_t *to = g_cache_copy.data() + offset;+ to[words - 1] = 0;+ to[words - 2] = 0;+ std::memcpy(to, bytes, n);+ wgpuQueueWriteBuffer(g_queue, g_cache, static_cast<std::uint64_t>(offset) * 4, to, static_cast<std::uint64_t>(words) * 4);+ g_cache_ring.push_back({key, offset, words, n, g_serial});+ g_cache_index.emplace(key, &g_cache_ring.back());+ g_cache_head += words;+ return true;+}+std::uint32_t arena_append(const void *bytes, std::size_t size) {const std::uint32_t offset = static_cast<std::uint32_t>(g_arena.size());const std::size_t words = (size + 3) / 4 + 1;@@ -305,28 +389,31 @@ void flush_batch() {for (std::uint32_t t : g_touched) total += g_tile_count[t];const std::uint32_t tiles = static_cast<std::uint32_t>(g_touched.size());const std::uint32_t tile_offset = static_cast<std::uint32_t>(g_arena.size());- const std::uint32_t list_offset = tile_offset + tiles * 4;- g_arena.grow(tiles * 4 + total);- std::uint32_t cursor = list_offset;+ const std::uint32_t list_offset = tile_offset + tiles * 2 + 2;+ const std::uint32_t list_words = (total + 1) / 2;+ std::uint32_t *grown = g_arena.grow(tiles * 2 + 2 + list_words);+ if (list_words) grown[tiles * 2 + 2 + list_words - 1] = 0;+ std::uint32_t cursor = 0;for (std::uint32_t i = 0; i < tiles; ++i) {const std::uint32_t t = g_touched[i];- std::uint32_t *rec = g_arena.data() + tile_offset + i * 4;- rec[3] = 0;- rec[0] = (t % static_cast<std::uint32_t>(g_tiles_per_row)) | ((t / static_cast<std::uint32_t>(g_tiles_per_row)) << 16);- rec[1] = cursor;- rec[2] = g_tile_count[t];+ grown[i * 2] = (t % static_cast<std::uint32_t>(g_tiles_per_row)) | ((t / static_cast<std::uint32_t>(g_tiles_per_row)) << 16);+ grown[i * 2 + 1] = cursor;g_tile_fill[t] = cursor;cursor += g_tile_count[t];}- std::uint32_t *arena = g_arena.data();- for (std::uint32_t i = 0; i < prims; ++i)+ grown[tiles * 2] = 0;+ grown[tiles * 2 + 1] = cursor;+ auto *halves = reinterpret_cast<unsigned char *>(grown + tiles * 2 + 2);+ for (std::uint32_t i = 0; i < prims; ++i) {+ const auto index = static_cast<std::uint16_t>(i);for (std::uint32_t k = g_span_start[i]; k < g_span_start[i + 1]; ++k) {const Span &sp = g_spans[k];std::uint32_t t = static_cast<std::uint32_t>(sp.ty * g_tiles_per_row + sp.tx0);- for (int tx = sp.tx0; tx <= sp.tx1; ++tx, ++t) arena[g_tile_fill[t]++] = i;+ for (int tx = sp.tx0; tx <= sp.tx1; ++tx, ++t) std::memcpy(halves + 2u * g_tile_fill[t]++, &index, 2);}+ }for (std::uint32_t t : g_touched) g_tile_count[t] = 0;- const std::uint32_t param = push_params({tile_offset, prim_offset, setup_offset, tiles});+ const std::uint32_t param = push_params({tile_offset, prim_offset, setup_offset, tiles, list_offset});g_commands.push_back({kRaster, std::min(tiles, 1024u), (tiles + 1023) / 1024, param});b.prims.clear();b.setups.clear();@@ -400,8 +487,15 @@ std::uint64_t queue_upload(const std::uint8_t *bytes, std::size_t size, std::uinif (!pixels) return 0;const std::uint64_t used = lead_bits + static_cast<std::uint64_t>(pixels) * im.bpp;PreOp op;- const std::uint32_t offset = arena_append(bytes, static_cast<std::size_t>((used + 7) / 8));- const std::uint32_t rec[11] = {im.dbp, im.dbw, im.dpsm, im.dsax, im.dsay, im.rrw, im.done, pixels, offset, lead_bits, im.bpp};+ const auto n = static_cast<std::size_t>((used + 7) / 8);+ std::uint32_t offset = 0, source = 0;+ if (n >= kCacheMinBytes) {+ const std::uint64_t key = content_key(bytes, n);+ if (cache_find(key, bytes, n, offset) || (++g_cache_seen[key] >= 2 && cache_insert(key, bytes, n, offset))) source = 0x80000000u;+ if (g_cache_seen.size() > 65536) g_cache_seen.clear();+ }+ if (!source) offset = arena_append(bytes, n);+ const std::uint32_t rec[11] = {im.dbp, im.dbw, im.dpsm, im.dsax, im.dsay, im.rrw, im.done, pixels, offset, lead_bits, im.bpp | source};std::memcpy(op.rec, rec, sizeof rec);const std::uint32_t first = im.done / im.rrw, last = (im.done + pixels - 1) / im.rrw;op.range = region(im.dbp, im.dbw, im.dpsm, static_cast<int>(im.dsax), static_cast<int>(im.dsay + first), static_cast<int>(im.dsax + im.rrw - 1),@@ -466,9 +560,9 @@ WGPUBuffer make_buffer(std::uint64_t size, WGPUBufferUsage usage) {void rebuild_group() {if (g_group) wgpuBindGroupRelease(g_group);- WGPUBindGroupEntry e[6] = {};- WGPUBuffer buffers[6] = {g_vram, g_tables, g_params, g_data, g_clut, g_scratch};- for (int i = 0; i < 6; ++i) {+ WGPUBindGroupEntry e[7] = {};+ WGPUBuffer buffers[7] = {g_vram, g_tables, g_params, g_data, g_clut, g_scratch, g_cache};+ for (int i = 0; i < 7; ++i) {e[i] = WGPU_BIND_GROUP_ENTRY_INIT;e[i].binding = static_cast<std::uint32_t>(i);e[i].buffer = buffers[i];@@ -476,7 +570,7 @@ void rebuild_group() {}WGPUBindGroupDescriptor d = WGPU_BIND_GROUP_DESCRIPTOR_INIT;d.layout = g_layout;- d.entryCount = 6;+ d.entryCount = 7;d.entries = e;g_group = wgpuDeviceCreateBindGroup(g_device, &d);}@@ -586,6 +680,7 @@ void submit(WGPUTextureView present_view = nullptr) {wgpuDevicePoll(g_device, false, nullptr);#endifg_commands.clear();+ ++g_serial;g_param_records.clear();g_arena.clear();g_clut_loads = 0;@@ -663,11 +758,13 @@ std::uint32_t make_setup(const State &st, std::uint64_t attr, std::uint32_t *w)return flags;}-void attrs(std::uint32_t *slot, const Vertex &v, bool fst, const Vertex *color) {+void attrs(std::uint32_t *rec, int n, const Vertex &v, bool fst, const Vertex *color) {const Vertex &c = color ? *color : v;- const float a[8] = {static_cast<float>(c.r), static_cast<float>(c.g), static_cast<float>(c.b), static_cast<float>(c.a),- fst ? static_cast<float>(v.u) : v.s, fst ? static_cast<float>(v.v) : v.t, fst ? 1.0f : v.q, static_cast<float>(v.f)};- for (int i = 0; i < 8; ++i) slot[i] = f2u(a[i]);+ rec[12 + n] = c.r | (static_cast<std::uint32_t>(c.g) << 8) | (static_cast<std::uint32_t>(c.b) << 16) | (static_cast<std::uint32_t>(c.a) << 24);+ rec[15 + n * 3] = f2u(fst ? static_cast<float>(v.u) : v.s);+ rec[16 + n * 3] = f2u(fst ? static_cast<float>(v.v) : v.t);+ rec[17 + n * 3] = f2u(fst ? 1.0f : v.q);+ rec[11] |= static_cast<std::uint32_t>(v.f) << (8 * n);}}@@ -754,6 +851,8 @@ bool init(Mode m) {wgpuQueueWriteBuffer(g_queue, g_tables, 0, tables.data(), tables.size() * 4);g_clut = make_buffer(static_cast<std::uint64_t>(kClutSlots) * kClutEntries * 4, WGPUBufferUsage_Storage);g_scratch = make_buffer((static_cast<std::uint64_t>(g_copy_words) + 2ull * g_field_words) * 4, WGPUBufferUsage_Storage | WGPUBufferUsage_CopySrc);+ g_cache = make_buffer(static_cast<std::uint64_t>(kCacheWords) * 4, WGPUBufferUsage_Storage | WGPUBufferUsage_CopyDst);+ g_cache_copy.assign(kCacheWords, 0);g_readback = make_buffer(static_cast<std::uint64_t>(g_copy_words) * 4, WGPUBufferUsage_MapRead | WGPUBufferUsage_CopyDst);if (g_timestamps) {WGPUQuerySetDescriptor qd = WGPU_QUERY_SET_DESCRIPTOR_INIT;@@ -764,10 +863,11 @@ bool init(Mode m) {g_query_read = make_buffer(16, WGPUBufferUsage_MapRead | WGPUBufferUsage_CopyDst);}- WGPUBindGroupLayoutEntry le[6] = {};- const WGPUBufferBindingType types[6] = {WGPUBufferBindingType_Storage, WGPUBufferBindingType_ReadOnlyStorage, WGPUBufferBindingType_Uniform,- WGPUBufferBindingType_ReadOnlyStorage, WGPUBufferBindingType_Storage, WGPUBufferBindingType_Storage};- for (int i = 0; i < 6; ++i) {+ WGPUBindGroupLayoutEntry le[7] = {};+ const WGPUBufferBindingType types[7] = {WGPUBufferBindingType_Storage, WGPUBufferBindingType_ReadOnlyStorage, WGPUBufferBindingType_Uniform,+ WGPUBufferBindingType_ReadOnlyStorage, WGPUBufferBindingType_Storage, WGPUBufferBindingType_Storage,+ WGPUBufferBindingType_ReadOnlyStorage};+ for (int i = 0; i < 7; ++i) {le[i] = WGPU_BIND_GROUP_LAYOUT_ENTRY_INIT;le[i].binding = static_cast<std::uint32_t>(i);le[i].visibility = WGPUShaderStage_Compute;@@ -776,7 +876,7 @@ bool init(Mode m) {le[i].buffer.minBindingSize = i == 2 ? kParamStride : 0;}WGPUBindGroupLayoutDescriptor ld = WGPU_BIND_GROUP_LAYOUT_DESCRIPTOR_INIT;- ld.entryCount = 6;+ ld.entryCount = 7;ld.entries = le;g_layout = wgpuDeviceCreateBindGroupLayout(g_device, &ld);WGPUPipelineLayoutDescriptor pld = WGPU_PIPELINE_LAYOUT_DESCRIPTOR_INIT;@@ -839,10 +939,10 @@ void draw(const State &st, const Vertex *v, int count) {const float xy[6] = {x0, y0, x1, y1, x2, y2};for (int i = 0; i < 6; ++i) rec[4 + i] = f2u(xy[i]);rec[10] = f2u(1.0f / area);- for (int i = 0; i < 3; ++i) attrs(rec + 12 + i * 8, v[i], fst, iip ? nullptr : &v[2]);- rec[36] = v[0].z;- rec[37] = f2u(static_cast<float>(static_cast<std::int64_t>(v[1].z) - static_cast<std::int64_t>(v[0].z)));- rec[38] = f2u(static_cast<float>(static_cast<std::int64_t>(v[2].z) - static_cast<std::int64_t>(v[0].z)));+ for (int i = 0; i < 3; ++i) attrs(rec, i, v[i], fst, iip ? nullptr : &v[2]);+ rec[24] = v[0].z;+ rec[25] = f2u(static_cast<float>(static_cast<std::int64_t>(v[1].z) - static_cast<std::int64_t>(v[0].z)));+ rec[26] = f2u(static_cast<float>(static_cast<std::int64_t>(v[2].z) - static_cast<std::int64_t>(v[0].z)));break;}case 6: {@@ -856,9 +956,9 @@ void draw(const State &st, const Vertex *v, int count) {rec[5] = f2u(y0);rec[6] = f2u(x1 - x0);rec[7] = f2u(y1 - y0);- attrs(rec + 12, v[0], fst, nullptr);- attrs(rec + 20, v[1], fst, nullptr);- rec[36] = v[1].z;+ attrs(rec, 0, v[0], fst, nullptr);+ attrs(rec, 1, v[1], fst, nullptr);+ rec[24] = v[1].z;break;}case 1: case 2: {@@ -874,10 +974,10 @@ void draw(const State &st, const Vertex *v, int count) {rec[6] = f2u(x1);rec[7] = f2u(y1);rec[8] = static_cast<std::uint32_t>(steps);- attrs(rec + 12, v[0], fst, iip ? nullptr : &v[1]);- attrs(rec + 20, v[1], fst, nullptr);- rec[36] = v[0].z;- rec[37] = f2u(static_cast<float>(static_cast<std::int64_t>(v[1].z) - static_cast<std::int64_t>(v[0].z)));+ attrs(rec, 0, v[0], fst, iip ? nullptr : &v[1]);+ attrs(rec, 1, v[1], fst, nullptr);+ rec[24] = v[0].z;+ rec[25] = f2u(static_cast<float>(static_cast<std::int64_t>(v[1].z) - static_cast<std::int64_t>(v[0].z)));break;}case 0: {@@ -888,8 +988,8 @@ void draw(const State &st, const Vertex *v, int count) {rec[1] = 3;rec[4] = static_cast<std::uint32_t>(x);rec[5] = static_cast<std::uint32_t>(y);- attrs(rec + 12, v[0], fst, nullptr);- rec[36] = v[0].z;+ attrs(rec, 0, v[0], fst, nullptr);+ rec[24] = v[0].z;break;}default:diff --git a/runtime/src/gs/gpu/gs.wgsl b/runtime/src/gs/gpu/gs.wgslindex d3381ca..bbc2ae6 100644--- a/runtime/src/gs/gpu/gs.wgsl+++ b/runtime/src/gs/gpu/gs.wgsl@@ -8,6 +8,7 @@ struct Params {@group(0) @binding(3) var<storage, read> data: array<u32>;@group(0) @binding(4) var<storage, read_write> clut: array<u32>;@group(0) @binding(5) var<storage, read_write> scratch: array<u32>;+@group(0) @binding(6) var<storage, read> cache: array<u32>;override SCALE: u32 = 1u;@@ -261,6 +262,20 @@ fn bits_at(offset: u32, bit: u32, count: u32) -> u32 {return v & ((1u << count) - 1u);}+fn cache_bits_at(offset: u32, bit: u32, count: u32) -> u32 {+ let w = offset + bit / 32u;+ let s = bit % 32u;+ var v = cache[w] >> s;+ if (s + count > 32u) {+ v = v | (cache[w + 1u] << (32u - s));+ }+ if (count == 32u) {+ return v;+ }+ return v & ((1u << count) - 1u);+}++@compute @workgroup_size(64)fn upload(@builtin(global_invocation_id) gid: vec3<u32>) {let i = gid.x + gid.y * 64u * 1024u;@@ -284,8 +299,13 @@ fn upload(@builtin(global_invocation_id) gid: vec3<u32>) {let p = data[r + 6u] + k;let x = (data[r + 3u] + p % rrw) & 0x7FFu;let y = (data[r + 4u] + p / rrw) & 0x7FFu;- let bpp = data[r + 10u];- let v = bits_at(data[r + 8u], data[r + 9u] + k * bpp, bpp);+ let bpp = data[r + 10u] & 0xFFu;+ var v = 0u;+ if ((data[r + 10u] & 0x80000000u) != 0u) {+ v = cache_bits_at(data[r + 8u], data[r + 9u] + k * bpp, bpp);+ } else {+ v = bits_at(data[r + 8u], data[r + 9u] + k * bpp, bpp);+ }write_pixel(data[r + 2u], data[r], data[r + 1u], x, y, v);}@@ -893,7 +913,7 @@ fn plot(s: Setup, x: i32, y: i32, k: u32, at: Attr) {}}-const PRIM_WORDS: u32 = 40u;+const PRIM_WORDS: u32 = 28u;const SETUP_WORDS: u32 = 24u;fn fat(i: u32) -> f32 {@@ -901,15 +921,16 @@ fn fat(i: u32) -> f32 {}fn vertex_attr(p: u32, n: u32) -> vec4<f32> {- return vec4<f32>(fat(p + 12u + n * 8u), fat(p + 13u + n * 8u), fat(p + 14u + n * 8u), fat(p + 15u + n * 8u));+ let c = data[p + 12u + n];+ return vec4<f32>(f32(c & 0xFFu), f32((c >> 8u) & 0xFFu), f32((c >> 16u) & 0xFFu), f32(c >> 24u));}fn vertex_tex(p: u32, n: u32) -> vec4<f32> {- return vec4<f32>(fat(p + 16u + n * 8u), fat(p + 17u + n * 8u), fat(p + 18u + n * 8u), fat(p + 19u + n * 8u));+ return vec4<f32>(fat(p + 15u + n * 3u), fat(p + 16u + n * 3u), fat(p + 17u + n * 3u), f32((data[p + 11u] >> (n * 8u)) & 0xFFu));}fn z_at(p: u32, d: f32) -> u32 {- let z0 = data[p + 36u];+ let z0 = data[p + 24u];let step = floor(d);if (step >= 0.0) {return z0 + u32(min(step, f32(0xFFFFFFFFu - z0)));@@ -937,7 +958,7 @@ fn shade_at(p: u32, s: Setup, x: i32, y: i32, k: u32, px: f32, py: f32) {}let c = vertex_attr(p, 0u) * w0 + vertex_attr(p, 1u) * w1 + vertex_attr(p, 2u) * w2;let t = vertex_tex(p, 0u) * w0 + vertex_tex(p, 1u) * w1 + vertex_tex(p, 2u) * w2;- at = Attr(c.x, c.y, c.z, c.w, t.x, t.y, t.z, t.w, z_at(p, fat(p + 37u) * w1 + fat(p + 38u) * w2));+ at = Attr(c.x, c.y, c.z, c.w, t.x, t.y, t.z, t.w, z_at(p, fat(p + 25u) * w1 + fat(p + 26u) * w2));}case 1u {let x0 = fat(p + 4u);@@ -958,7 +979,7 @@ fn shade_at(p: u32, s: Setup, x: i32, y: i32, k: u32, px: f32, py: f32) {ty = (py - y0) / dy;}let c = vertex_attr(p, 1u);- at = Attr(c.x, c.y, c.z, c.w, a.x + (b.x - a.x) * tx, a.y + (b.y - a.y) * ty, b.z, b.w, data[p + 36u]);+ at = Attr(c.x, c.y, c.z, c.w, a.x + (b.x - a.x) * tx, a.y + (b.y - a.y) * ty, b.z, b.w, data[p + 24u]);}case 2u {let x0 = fat(p + 4u);@@ -997,7 +1018,7 @@ fn shade_at(p: u32, s: Setup, x: i32, y: i32, k: u32, px: f32, py: f32) {}let c = vertex_attr(p, 0u) * (1.0 - t) + vertex_attr(p, 1u) * t;let tex = vertex_tex(p, 0u) * (1.0 - t) + vertex_tex(p, 1u) * t;- at = Attr(c.x, c.y, c.z, c.w, tex.x, tex.y, tex.z, tex.w, z_at(p, fat(p + 37u) * t));+ at = Attr(c.x, c.y, c.z, c.w, tex.x, tex.y, tex.z, tex.w, z_at(p, fat(p + 25u) * t));}default {if (x != i32(data[p + 4u]) || y != i32(data[p + 5u])) {@@ -1005,7 +1026,7 @@ fn shade_at(p: u32, s: Setup, x: i32, y: i32, k: u32, px: f32, py: f32) {}let c = vertex_attr(p, 0u);let t = vertex_tex(p, 0u);- at = Attr(c.x, c.y, c.z, c.w, t.x, t.y, t.z, t.w, data[p + 36u]);+ at = Attr(c.x, c.y, c.z, c.w, t.x, t.y, t.z, t.w, data[p + 24u]);}}plot(s, x, y, k, at);@@ -1022,10 +1043,11 @@ fn resolve(@builtin(global_invocation_id) gid: vec3<u32>) {@compute @workgroup_size(16, 16)fn raster(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {- let tile = prm(0u) + (wid.x + wid.y * 1024u) * 4u;- if (wid.x + wid.y * 1024u >= prm(3u)) {+ let index = wid.x + wid.y * 1024u;+ if (index >= prm(3u)) {return;}+ let tile = prm(0u) + index * 2u;let origin = data[tile];let sx = (origin & 0xFFFFu) * 16u + lid.x;let sy = (origin >> 16u) * 16u + lid.y;@@ -1034,14 +1056,16 @@ fn raster(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) llet k = (sy % SCALE) * SCALE + sx % SCALE;let px = f32(x) + f32(sx % SCALE) / f32(SCALE);let py = f32(y) + f32(sy % SCALE) / f32(SCALE);- let list = data[tile + 1u];- let count = data[tile + 2u];+ let first = data[tile + 1u];+ let count = data[tile + 3u] - first;+ let lists = prm(4u);let prims = prm(1u);let setups = prm(2u);var last_setup = 0xFFFFFFFFu;var s: Setup;for (var i = 0u; i < count; i = i + 1u) {- let p = prims + data[list + i] * PRIM_WORDS;+ let h = first + i;+ let p = prims + ((data[lists + (h >> 1u)] >> ((h & 1u) * 16u)) & 0xFFFFu) * PRIM_WORDS;let si = data[p];if (si != last_setup) {s = load_setup(setups + si * SETUP_WORDS);