|
| 1 | +import { device, presentationFormat } from 'WebGPU/device' |
| 2 | + |
| 3 | +function createTexture( |
| 4 | + textureData: Uint8Array<ArrayBuffer>, |
| 5 | + textureWidth: number, |
| 6 | + textureHeight: number |
| 7 | +): GPUTexture { |
| 8 | + const texture = device.createTexture({ |
| 9 | + label: 'manually crafted placeholder texture', |
| 10 | + size: [textureWidth, textureHeight], |
| 11 | + format: presentationFormat, |
| 12 | + usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST, |
| 13 | + }) |
| 14 | + device.queue.writeTexture( |
| 15 | + { texture }, |
| 16 | + textureData, |
| 17 | + { bytesPerRow: textureWidth * 4 }, |
| 18 | + { width: textureWidth, height: textureHeight } |
| 19 | + ) |
| 20 | + |
| 21 | + return texture |
| 22 | +} |
| 23 | + |
| 24 | +// lazy initialziation base on presentationFormat |
| 25 | +const INITIAL_RGB_COLOR = [255, 205, 58, 255] |
| 26 | +// if we keep just let color, we won't know if channels where already swapped or not |
| 27 | +// and init texture is called each time user visits creator |
| 28 | +let color = INITIAL_RGB_COLOR |
| 29 | + |
| 30 | +// 5×5 pixel font glyphs |
| 31 | +// prettier-ignore |
| 32 | +const FONT: Record<string, number[][]> = { |
| 33 | + A: [[0,1,1,1,0],[1,0,0,0,1],[1,1,1,1,1],[1,0,0,0,1],[1,0,0,0,1]], |
| 34 | + D: [[1,1,1,1,0],[1,0,0,0,1],[1,0,0,0,1],[1,0,0,0,1],[1,1,1,1,0]], |
| 35 | + E: [[1,1,1,1,1],[1,0,0,0,0],[1,1,1,1,0],[1,0,0,0,0],[1,1,1,1,1]], |
| 36 | + F: [[1,1,1,1,1],[1,0,0,0,0],[1,1,1,1,0],[1,0,0,0,0],[1,0,0,0,0]], |
| 37 | + G: [[0,1,1,1,0],[1,0,0,0,0],[1,0,1,1,1],[1,0,0,0,1],[0,1,1,1,0]], |
| 38 | + I: [[1,1,1,1,1],[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0],[1,1,1,1,1]], |
| 39 | + M: [[1,0,0,0,1],[1,1,0,1,1],[1,0,1,0,1],[1,0,0,0,1],[1,0,0,0,1]], |
| 40 | + N: [[1,0,0,0,1],[1,1,0,0,1],[1,0,1,0,1],[1,0,0,1,1],[1,0,0,0,1]], |
| 41 | + L: [[1,0,0,0,0],[1,0,0,0,0],[1,0,0,0,0],[1,0,0,0,0],[1,1,1,1,1]], |
| 42 | + O: [[0,1,1,1,0],[1,0,0,0,1],[1,0,0,0,1],[1,0,0,0,1],[0,1,1,1,0]], |
| 43 | + R: [[1,1,1,1,0],[1,0,0,0,1],[1,1,1,1,0],[1,0,0,1,0],[1,0,0,0,1]], |
| 44 | + T: [[1,1,1,1,1],[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0]], |
| 45 | + U: [[1,0,0,0,1],[1,0,0,0,1],[1,0,0,0,1],[1,0,0,0,1],[0,1,1,1,0]], |
| 46 | + X: [[1,0,0,0,1],[0,1,0,1,0],[0,0,1,0,0],[0,1,0,1,0],[1,0,0,0,1]], |
| 47 | + ' ': [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]], |
| 48 | + } |
| 49 | + |
| 50 | +function drawText(pixels: Uint8Array, w: number, h: number, text: string, x0: number, y0: number) { |
| 51 | + let x = x0 |
| 52 | + for (const ch of text) { |
| 53 | + const g = FONT[ch] |
| 54 | + if (g) { |
| 55 | + for (let row = 0; row < 5; row++) { |
| 56 | + for (let col = 0; col < 5; col++) { |
| 57 | + if (g[row][col]) { |
| 58 | + setPixel(pixels, w, h, x + col, y0 + 4 - row) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + x += 6 // 5px glyph + 1px gap |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +const getBorderPixels = (w: number, h: number) => { |
| 68 | + const pixels = new Uint8Array(w * h * 4) |
| 69 | + |
| 70 | + for (let i = 0; i < w * h; i++) { |
| 71 | + if (i % w === 0 || i % w === w - 1 || i < w || i > w * h - w) { |
| 72 | + pixels[i * 4 + 0] = color[0] |
| 73 | + pixels[i * 4 + 1] = color[1] |
| 74 | + pixels[i * 4 + 2] = color[2] |
| 75 | + } |
| 76 | + pixels[i * 4 + 3] = 255 // opaque black by default |
| 77 | + } |
| 78 | + |
| 79 | + return pixels |
| 80 | +} |
| 81 | + |
| 82 | +function setPixel(pixels: Uint8Array, w: number, h: number, x: number, y: number) { |
| 83 | + if (x < 0 || x >= w || y < 0 || y >= h) return |
| 84 | + |
| 85 | + const i = (y * w + x) * 4 |
| 86 | + pixels[i] = color[0] |
| 87 | + pixels[i + 1] = color[1] |
| 88 | + pixels[i + 2] = color[2] |
| 89 | + pixels[i + 3] = color[3] |
| 90 | +} |
| 91 | + |
| 92 | +export function getLoadingTexture(): GPUTexture { |
| 93 | + if (presentationFormat === 'bgra8unorm') { |
| 94 | + color = [INITIAL_RGB_COLOR[2], INITIAL_RGB_COLOR[1], INITIAL_RGB_COLOR[0], INITIAL_RGB_COLOR[3]] |
| 95 | + } |
| 96 | + |
| 97 | + const W = 72 |
| 98 | + const H = 40 |
| 99 | + const pixels = getBorderPixels(W, H) |
| 100 | + |
| 101 | + function drawText(text: string, x0: number, y0: number) { |
| 102 | + let x = x0 |
| 103 | + for (const ch of text) { |
| 104 | + const g = FONT[ch] |
| 105 | + if (g) { |
| 106 | + for (let row = 0; row < 5; row++) |
| 107 | + for (let col = 0; col < 5; col++) |
| 108 | + if (g[row][col]) setPixel(pixels, W, H, x + col, y0 + 4 - row) |
| 109 | + } |
| 110 | + x += 6 // 5px glyph + 1px gap |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // "LOADING": 7 chars × 6 - 1 = 41px; center in 72: start x=16 |
| 115 | + drawText('LOADING', 16, 17) |
| 116 | + |
| 117 | + return createTexture(pixels, W, H) |
| 118 | +} |
| 119 | + |
| 120 | +export function getErrorTexture(): GPUTexture { |
| 121 | + const W = 72 |
| 122 | + const H = 40 |
| 123 | + const pixels = getBorderPixels(W, H) |
| 124 | + |
| 125 | + // "IMAGE NOT": 9 chars × 6 - 1 = 53px; center in 72: start x=10 |
| 126 | + drawText(pixels, W, H, 'IMAGE NOT', 10, 14) |
| 127 | + // "FOUND": 5 chars × 6 - 1 = 29px; center in 72: start x=21 |
| 128 | + drawText(pixels, W, H, 'FOUND', 21, 6) |
| 129 | + |
| 130 | + // Mario-style ? mark (20×16) centered horizontally (start col 26) |
| 131 | + // High y = top of screen; mark[0] is the arch top → placed at y = 35 (highest) |
| 132 | + // prettier-ignore |
| 133 | + const mark = [ |
| 134 | + [0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0], |
| 135 | + [0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0], // wide arch top: cols 1–16 |
| 136 | + [0,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0], // left wall cols 1–4, right cols 13–16 |
| 137 | + [0,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0], |
| 138 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0], // right wall descends one more row |
| 139 | + [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0], // diagonal: cols 11–14 |
| 140 | + [0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0], // cols 9–12 |
| 141 | + [0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0], // cols 7–10 (stem begins) |
| 142 | + [0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0], // stem |
| 143 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], // gap |
| 144 | + [0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0], // dot |
| 145 | + [0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0], |
| 146 | + ] |
| 147 | + |
| 148 | + for (let r = 0; r < mark.length; r++) { |
| 149 | + for (let c = 0; c < 20; c++) { |
| 150 | + if (mark[r][c]) { |
| 151 | + setPixel(pixels, W, H, 26 + c, 34 - r) |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return createTexture(pixels, W, H) |
| 157 | +} |
0 commit comments