From 5a75a6e6f3cab8c83c8d0b49361c8a54f73a5356 Mon Sep 17 00:00:00 2001 From: Gregg Tavares Date: Thu, 5 Dec 2024 12:10:09 +0900 Subject: [PATCH] Add back unicode for texture diagrams MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Example: ``` 0 1 2 3 ╔═══╤═══╦═══╤═══╗ 0 ║ a │ ║ │ ║ ╟───┼───╫───┼───╢ 1 ║ │ ║ │ ║ ╠═══╪═══╬═══╪═══╣ 2 ║ │ ║ │ ║ ╟───┼───╫───┼───╢ 3 ║ │ ║ │ b ║ ╚═══╧═══╩═══╧═══╝ ``` --- .../expression/call/builtin/texture_utils.ts | 105 +++++++++++++----- 1 file changed, 76 insertions(+), 29 deletions(-) diff --git a/src/webgpu/shader/execution/expression/call/builtin/texture_utils.ts b/src/webgpu/shader/execution/expression/call/builtin/texture_utils.ts index 175244697829..f097a64325ce 100644 --- a/src/webgpu/shader/execution/expression/call/builtin/texture_utils.ts +++ b/src/webgpu/shader/execution/expression/call/builtin/texture_utils.ts @@ -3428,25 +3428,70 @@ async function identifySamplePoints( // example when blockWidth = 2, blockHeight = 2 // // 0 1 2 3 - // +===+===+===+===+ - // 0 # a | # | # - // +---+---+---+---+ - // 1 # | # | # - // +===+===+===+===+ - // 2 # | # | # - // +---+---+---+---+ - // 3 # | # | b # - // +===+===+===+===+ + // ╔═══╤═══╦═══╤═══╗ + // 0 ║ a │ ║ │ ║ + // ╟───┼───╫───┼───╢ + // 1 ║ │ ║ │ ║ + // ╠═══╪═══╬═══╪═══╣ + // 2 ║ │ ║ │ ║ + // ╟───┼───╫───┼───╢ + // 3 ║ │ ║ │ b ║ + // ╚═══╧═══╩═══╧═══╝ const lines: string[] = []; const letter = (idx: number) => String.fromCodePoint(idx < 30 ? 97 + idx : idx + 9600 - 30); // 97: 'a' let idCount = 0; const { blockWidth, blockHeight } = kTextureFormatInfo[texture.descriptor.format]; - const [blockHChar, blockVChar] = Math.max(blockWidth, blockHeight) > 1 ? ['=', '#'] : ['-', '|']; - const blockHCell = '+'.padStart(4, blockHChar); // generates ---+ or ===+ // range + concatenate results. const rangeCat = (num: number, fn: (i: number) => T) => range(num, fn).join(''); + const joinFn = (arr: string[], fn: (i: number) => string) => { + const joins = range(arr.length - 1, fn); + return arr.map((s, i) => `${s}${joins[i] ?? ''}`).join(''); + }; + /* prettier-ignore */ + const blockParts = { + top: { left: '╔', fill: '═══', right: '╗', block: '╦', texel: '╤' }, + mid: { left: '╠', fill: '═══', right: '╣', block: '╬', texel: '╪' }, + bot: { left: '╚', fill: '═══', right: '╝', block: '╩', texel: '╧' }, + texelMid: { left: '╟', fill: '───', right: '╢', block: '╫', texel: '┼' }, + value: { left: '║', fill: ' ', right: '║', block: '║', texel: '│' }, + }; + /* prettier-ignore */ + const nonBlockParts = { + top: { left: '┌', fill: '───', right: '┐', block: '┬', texel: '┬' }, + mid: { left: '├', fill: '───', right: '┤', block: '┼', texel: '┼' }, + bot: { left: '└', fill: '───', right: '┘', block: '┴', texel: '┴' }, + texelMid: { left: '├', fill: '───', right: '┤', block: '┼', texel: '┼' }, + value: { left: '│', fill: ' ', right: '│', block: '│', texel: '│' }, + }; + const parts = Math.max(blockWidth, blockHeight) > 1 ? blockParts : nonBlockParts; + /** + * Makes a row that's [left, fill, texel, fill, block, fill, texel, fill, right] + * except if `contents` is supplied then it would be + * [left, contents[0], texel, contents[1], block, contents[2], texel, contents[3], right] + */ + const makeRow = ( + width: number, + { + left, + fill, + right, + block, + texel, + }: { + left: string; + fill: string; + right: string; + block: string; + texel: string; + }, + contents?: string[] + ) => { + return `${left}${joinFn(contents ?? new Array(width).fill(fill), x => { + return (x + 1) % blockWidth === 0 ? block : texel; + })}${right}`; + }; for (let mipLevel = 0; mipLevel < mipLevelCount; ++mipLevel) { const level = levels[mipLevel]; @@ -3477,30 +3522,32 @@ async function identifySamplePoints( } lines.push(` ${rangeCat(width, x => ` ${x.toString().padEnd(2)}`)}`); - lines.push(` +${rangeCat(width, () => blockHCell)}`); + lines.push(` ${makeRow(width, parts.top)}`); for (let y = 0; y < height; y++) { { - let line = `${y.toString().padStart(2)} ${blockVChar}`; - for (let x = 0; x < width; x++) { - const colChar = (x + 1) % blockWidth === 0 ? blockVChar : '|'; - const texelIdx = x + y * texelsPerRow; - const weight = layerEntries.get(texelIdx); - if (weight !== undefined) { - line += ` ${letter(idCount + orderedTexelIndices.length)} ${colChar}`; - orderedTexelIndices.push(texelIdx); - } else { - line += ` ${colChar}`; - } - } - lines.push(line); - } - if (y < height - 1) { lines.push( - ` +${rangeCat(width, () => ((y + 1) % blockHeight === 0 ? blockHCell : '---+'))}` + `${y.toString().padStart(2)} ${makeRow( + width, + parts.value, + range(width, x => { + const texelIdx = x + y * texelsPerRow; + const weight = layerEntries.get(texelIdx); + if (weight !== undefined) { + const id = letter(idCount + orderedTexelIndices.length); + orderedTexelIndices.push(texelIdx); + return ` ${id} `; + } else { + return ' '; + } + }) + )}` ); } + // It's either a block row, a texel row, or the last row. + const lineParts = + y < height - 1 ? ((y + 1) % blockHeight === 0 ? parts.mid : parts.texelMid) : parts.bot; + lines.push(` ${makeRow(width, lineParts)}`); } - lines.push(` +${range(width, () => blockHCell).join('')}`); const pad2 = (n: number) => n.toString().padStart(2); const pad3 = (n: number) => n.toString().padStart(3);