File size: 7,824 Bytes
3f22414 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | /**
* GpuTileRenderer — renders CHW float32 ORT output buffers directly to a
* GPU texture via a WGSL fragment shader, avoiding the GPU→CPU readback.
*
* Usage:
* const renderer = new GpuTileRenderer(device);
* renderer.configure(canvas, outW, outH);
* // per tile:
* renderer.renderTile(gpuBuffer, tileW, tileH, destX, destY, overlap, outputScale);
* renderer.presentToCanvas();
* // cleanup:
* renderer.destroy();
*/
import { overlapCrop } from './tiling.js';
const SHADER = /* wgsl */ `
struct Params {
tileW: u32,
tileH: u32,
destX: u32,
destY: u32,
outputScale: f32,
}
@group(0) @binding(0) var<storage, read> chw: array<f32>;
@group(0) @binding(1) var<uniform> params: Params;
@vertex fn vs(@builtin(vertex_index) vi: u32) -> @builtin(position) vec4f {
var pos = array<vec2f, 6>(
vec2f(-1, -1), vec2f(1, -1), vec2f(-1, 1),
vec2f(-1, 1), vec2f(1, -1), vec2f(1, 1),
);
return vec4f(pos[vi], 0, 1);
}
@fragment fn fs(@builtin(position) pos: vec4f) -> @location(0) vec4f {
let x = u32(pos.x) - params.destX;
let y = u32(pos.y) - params.destY;
let plane = params.tileW * params.tileH;
let s = params.outputScale;
return vec4f(
clamp(chw[y * params.tileW + x] * s, 0.0, 1.0),
clamp(chw[plane + y * params.tileW + x] * s, 0.0, 1.0),
clamp(chw[2u * plane + y * params.tileW + x] * s, 0.0, 1.0),
1.0,
);
}
`;
const PARAMS_SIZE = 5 * 4; // 5 u32/f32 fields × 4 bytes, padded to 16-byte alignment
const PARAMS_BUFFER_SIZE = Math.ceil(PARAMS_SIZE / 16) * 16;
/**
* Thrown by GpuTileRenderer.configure() when the requested canvas/texture
* dimensions exceed the device's maxTextureDimension2D. Callers can catch
* this and fall back to a CPU readback path instead of silently producing
* a black canvas (which is what WebGPU does on validation failure).
*/
export class GpuOutputTooLargeError extends Error {
constructor(width, height, maxDim) {
super(`Output ${width}×${height} exceeds GPU max texture dimension (${maxDim}).`);
this.name = 'GpuOutputTooLargeError';
this.width = width;
this.height = height;
this.maxDim = maxDim;
}
}
export class GpuTileRenderer {
#device;
#pipeline = null;
#bindGroupLayout = null;
#paramsBuffer = null;
#outputTexture = null;
#canvasCtx = null;
#canvasFormat;
#width = 0;
#height = 0;
#lost = false;
constructor(device) {
this.#device = device;
this.#canvasFormat = navigator.gpu.getPreferredCanvasFormat();
this.#initPipeline();
this.#device.lost.then((info) => {
this.#lost = true;
console.warn('[GpuTileRenderer] GPU device lost:', info.message);
});
}
get lost() { return this.#lost; }
/**
* @throws {GpuOutputTooLargeError} when width/height exceeds
* device.limits.maxTextureDimension2D. Both the WebGPU canvas surface
* and the persistent output texture are subject to that cap; exceeding
* it causes WebGPU to silently produce a black canvas, so we surface
* the failure up-front instead.
*/
configure(canvas, width, height) {
const maxDim = this.#device.limits?.maxTextureDimension2D ?? 8192;
if (width > maxDim || height > maxDim) {
throw new GpuOutputTooLargeError(width, height, maxDim);
}
this.#canvasCtx = canvas.getContext('webgpu');
this.#canvasCtx.configure({
device: this.#device,
format: this.#canvasFormat,
alphaMode: 'opaque',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_DST,
});
this.#width = width;
this.#height = height;
this.#outputTexture?.destroy();
this.#outputTexture = this.#device.createTexture({
size: [width, height],
format: this.#canvasFormat,
usage:
GPUTextureUsage.RENDER_ATTACHMENT |
GPUTextureUsage.COPY_SRC |
GPUTextureUsage.TEXTURE_BINDING,
});
// Clear the persistent texture to black
const encoder = this.#device.createCommandEncoder();
const pass = encoder.beginRenderPass({
colorAttachments: [{
view: this.#outputTexture.createView(),
loadOp: 'clear',
clearValue: { r: 0, g: 0, b: 0, a: 1 },
storeOp: 'store',
}],
});
pass.end();
this.#device.queue.submit([encoder.finish()]);
}
/**
* Render one tile from an ORT GPU buffer onto the persistent output texture.
*
* @param {GPUBuffer} gpuBuffer - ORT output tensor's GPUBuffer (CHW float32)
* @param {number} tileW - output tile width in pixels
* @param {number} tileH - output tile height in pixels
* @param {number} destX - destination X on the output texture
* @param {number} destY - destination Y on the output texture
* @param {number} overlap - overlap in output-space pixels
* @param {number} outputScale - multiply CHW values by this (1.0 for 0-1 models, 1/255 for 0-255 models)
*/
renderTile(gpuBuffer, tileW, tileH, destX, destY, overlap, outputScale) {
const scissor = overlapCrop(destX, destY, tileW, tileH, this.#width, this.#height, overlap);
if (scissor.w <= 0 || scissor.h <= 0) return;
// Write tile params to uniform buffer
const paramsData = new ArrayBuffer(PARAMS_BUFFER_SIZE);
const u32 = new Uint32Array(paramsData);
const f32 = new Float32Array(paramsData);
u32[0] = tileW;
u32[1] = tileH;
u32[2] = destX;
u32[3] = destY;
f32[4] = outputScale;
this.#device.queue.writeBuffer(this.#paramsBuffer, 0, paramsData);
const bindGroup = this.#device.createBindGroup({
layout: this.#bindGroupLayout,
entries: [
{ binding: 0, resource: { buffer: gpuBuffer } },
{ binding: 1, resource: { buffer: this.#paramsBuffer } },
],
});
const encoder = this.#device.createCommandEncoder();
const pass = encoder.beginRenderPass({
colorAttachments: [{
view: this.#outputTexture.createView(),
loadOp: 'load',
storeOp: 'store',
}],
});
pass.setPipeline(this.#pipeline);
pass.setBindGroup(0, bindGroup);
pass.setViewport(destX, destY, tileW, tileH, 0, 1);
pass.setScissorRect(scissor.x, scissor.y, scissor.w, scissor.h);
pass.draw(6);
pass.end();
this.#device.queue.submit([encoder.finish()]);
}
/** Copy the persistent output texture to the canvas for display / toBlob. */
presentToCanvas() {
const canvasTex = this.#canvasCtx.getCurrentTexture();
const encoder = this.#device.createCommandEncoder();
encoder.copyTextureToTexture(
{ texture: this.#outputTexture },
{ texture: canvasTex },
[this.#width, this.#height],
);
this.#device.queue.submit([encoder.finish()]);
}
destroy() {
this.#outputTexture?.destroy();
this.#outputTexture = null;
this.#paramsBuffer?.destroy();
this.#paramsBuffer = null;
}
#initPipeline() {
const module = this.#device.createShaderModule({ code: SHADER });
this.#bindGroupLayout = this.#device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.FRAGMENT,
buffer: { type: 'read-only-storage' },
},
{
binding: 1,
visibility: GPUShaderStage.FRAGMENT,
buffer: { type: 'uniform' },
},
],
});
const pipelineLayout = this.#device.createPipelineLayout({
bindGroupLayouts: [this.#bindGroupLayout],
});
this.#pipeline = this.#device.createRenderPipeline({
layout: pipelineLayout,
vertex: { module, entryPoint: 'vs' },
fragment: {
module,
entryPoint: 'fs',
targets: [{ format: this.#canvasFormat }],
},
});
this.#paramsBuffer = this.#device.createBuffer({
size: PARAMS_BUFFER_SIZE,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
}
}
|