feat(webgpu): HDR post-process primitives — float textures, storage-texture bindings, RESOLVE→HDR target (#27)

Adds the minimal WebGPU-backend surface an HDR bloom/post chain needs:

1. wgpuCreateStorageImage2D + StorageImage2D<>: runtime rgba16float (or
   other format) textures with STORAGE_BINDING|TEXTURE_BINDING usage, no
   CompressedTextureAsset upload — GPU-produced HDR targets.
2. UICustomBindingKind::StorageTexture (kind 5): write-only storage-texture
   binding carrying a texel format (the freed UICustomBinding::format byte),
   wired through all three binding paths (UI custom / RT / plain compute).
   Float textures already sample via SampledTexture (sampleType float).
3. PipelineRTWebGPU::Init(..., hdrOutputFormat): RESOLVE writes the linear
   accumulator into a user rgba16float texture (RTPass::outTexHandle)
   instead of the rgba8unorm canvas, leaving the composite→swapchain pass to
   the app. Default RGBA8Unorm keeps the canvas path byte-identical.

Existing RT examples updated for the _pad→format field rename; the default
paths are unchanged (verified RTStress builds + renders).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
catbot 2026-06-09 12:55:14 +00:00
commit 097cc37347
12 changed files with 198 additions and 22 deletions

View file

@ -46,6 +46,7 @@ function stub(name) {
"wgpuCreateAtlasTexture", "wgpuWriteAtlasRegion", "wgpuDestroyTexture",
"wgpuCreateImage2D", "wgpuWriteImage2D",
"wgpuCreateImage2DArray", "wgpuWriteImage2DLayer",
"wgpuCreateStorageImage2D",
"wgpuCreateLinearClampSampler", "wgpuCreateLinearRepeatSampler",
"wgpuFrameBegin", "wgpuFrameEnd",
"wgpuDispatchQuads", "wgpuDispatchCircles", "wgpuDispatchImages", "wgpuDispatchText",
@ -835,6 +836,42 @@ env.wgpuWriteImage2DLayer = (handle, layer, level, srcPtr, byteSize, w, h) => {
}
};
// Map the C++ WebGPUTexelFormat enum (u8) to a WGSL/GPU format string.
// Kept deliberately small — the HDR post-process path needs rgba16float;
// rgba8unorm (0) is the default so an unspecified `format` byte stays the
// pre-existing behaviour. Storage-texture bindings (UICustomBindingKind 5)
// and wgpuCreateStorageImage2D both run through here.
function texelFormatStr(f) {
switch (f | 0) {
case 1: return "rgba16float";
case 2: return "r32float";
default: return "rgba8unorm";
}
}
// Runtime-allocated 2D texture with STORAGE_BINDING + TEXTURE_BINDING usage
// and a caller-chosen format. Unlike wgpuCreateImage2D (rgba8unorm, sampled,
// CPU-upload only) this is the allocation path an HDR post-process chain
// needs: an rgba16float target a compute pass can BOTH write
// (texture_storage_2d<rgba16float, write>) and sample
// (texture_2d<f32>, filterable). No CompressedTextureAsset upload — the
// content is produced on the GPU. COPY_DST so it can be cleared / seeded;
// COPY_SRC so it can be read back or blitted.
env.wgpuCreateStorageImage2D = (w, h, format) => {
const handle = newHandle();
const tex = device.createTexture({
size: [w, h],
format: texelFormatStr(format),
usage: GPUTextureUsage.STORAGE_BINDING
| GPUTextureUsage.TEXTURE_BINDING
| GPUTextureUsage.COPY_SRC
| GPUTextureUsage.COPY_DST,
});
textures.set(handle, tex);
textureViews.set(handle, tex.createView());
return handle;
};
env.wgpuWriteImage2D = (handle, srcPtr, byteSize, w, h) => {
const tex = textures.get(handle);
if (!tex) return;
@ -1065,6 +1102,7 @@ env.wgpuLoadCustomShader = (wgslPtr, wgslLen, bindingsPtr, bindingsCount, rayQue
group: dv.getUint8(i*8 + 0),
binding: dv.getUint8(i*8 + 1),
kind: dv.getUint8(i*8 + 2),
format: dv.getUint8(i*8 + 3), // texel format for storage-texture kind
pushOffset: dv.getUint32(i*8 + 4, true),
});
}
@ -1130,6 +1168,7 @@ env.wgpuLoadCustomShader = (wgslPtr, wgslLen, bindingsPtr, bindingsCount, rayQue
else if (b.kind === 1) e.texture = { sampleType: "float", viewDimension: "2d" };
else if (b.kind === 2) e.sampler = { type: "filtering" };
else if (b.kind === 3) e.texture = { sampleType: "float", viewDimension: "2d-array" };
else if (b.kind === 5) e.storageTexture = { format: texelFormatStr(b.format), access: "write-only", viewDimension: "2d" };
return e;
});
bgls.push(device.createBindGroupLayout({ entries }));
@ -1230,6 +1269,7 @@ env.wgpuDispatchCustom = (pipelineHandle, pushPtr, pushBytes, handlesPtr, handle
else if (b.kind === 1) resource = textureViews.get(h);
else if (b.kind === 2) resource = samplers.get(h);
else if (b.kind === 3) resource = textureViews.get(h);
else if (b.kind === 5) resource = textureViews.get(h);
return { binding: b.binding, resource };
});
const bg = device.createBindGroup({ layout: pipe.bgls[g], entries });
@ -3060,9 +3100,17 @@ function wfLogTimestamps(ts, data) {
console.log(`[crafter-wgpu] RT passes: ${parts.join(" | ")} | total ${(totalNs/1000).toFixed(1)}us`);
}
env.wgpuLoadRTPipeline = (wgslPtr, wgslLen, bindingsPtr, bindingsCount) => {
env.wgpuLoadRTPipeline = (wgslPtr, wgslLen, bindingsPtr, bindingsCount, hdrOutputFormat) => {
if (!rtState.vertHeap) rtInit();
const userPart = new TextDecoder().decode(memU8().subarray(wgslPtr, wgslPtr + wgslLen));
// hdrOutputFormat (0 = none): when set, RESOLVE writes the linear
// accumulator into a user-provided storage texture of this format
// (rgba16float for HDR post-process) instead of the rgba8unorm canvas
// ping-pong. The app owns the downstream composite→swapchain pass. The
// binding-6 format must match in BOTH the WGSL `outImage` declaration
// and the data bind-group layout, so we swap both below.
const hdrOut = (hdrOutputFormat | 0) !== 0;
const outFmtStr = hdrOut ? texelFormatStr(hdrOutputFormat) : "rgba8unorm";
// Insert helpers at the marker; prepend prelude.
const marker = "// @CRAFTER_RT_LIBRARY_HELPERS_HERE";
@ -3076,7 +3124,12 @@ env.wgpuLoadRTPipeline = (wgslPtr, wgslLen, bindingsPtr, bindingsCount) => {
// Wavefront assembly: types + bindings | user CH/miss/resolve + wfPayload
// + switches (beforeHelpers) | pure helpers | wavefront helpers | user
// raygen + the five @compute entry points (afterHelpers).
const fullWgsl = rtWgslTypes + rtWgslWavefrontBindings + "\n"
const wavefrontBindings = hdrOut
? rtWgslWavefrontBindings.replace(
"var outImage : texture_storage_2d<rgba8unorm, write>;",
"var outImage : texture_storage_2d<" + outFmtStr + ", write>;")
: rtWgslWavefrontBindings;
const fullWgsl = rtWgslTypes + wavefrontBindings + "\n"
+ beforeHelpers + "\n" + rtWgslPureHelpers + "\n"
+ rtWgslWavefrontHelpers + "\n" + afterHelpers;
@ -3102,6 +3155,7 @@ env.wgpuLoadRTPipeline = (wgslPtr, wgslLen, bindingsPtr, bindingsCount) => {
group: g,
binding: dv.getUint8(i*8 + 1),
kind: dv.getUint8(i*8 + 2),
format: dv.getUint8(i*8 + 3), // texel format for storage-texture kind
pushOffset: dv.getUint32(i*8 + 4, true),
});
}
@ -3124,7 +3178,7 @@ env.wgpuLoadRTPipeline = (wgslPtr, wgslLen, bindingsPtr, bindingsCount) => {
const dataBgl = device.createBindGroupLayout({ entries: [
sb(0), sb(1), sb(2), sb(3), sb(4), sb(5),
{ binding: 6, visibility: GPUShaderStage.COMPUTE,
storageTexture: { format: "rgba8unorm", access: "write-only", viewDimension: "2d" } },
storageTexture: { format: outFmtStr, access: "write-only", viewDimension: "2d" } },
sb(7), sb(8), sb(9),
rw(10), rw(11), rw(12), rw(13), rw(14), rw(15),
]});
@ -3143,6 +3197,7 @@ env.wgpuLoadRTPipeline = (wgslPtr, wgslLen, bindingsPtr, bindingsCount) => {
else if (b.kind === 2) e.sampler = { type: "filtering" };
else if (b.kind === 3) e.texture = { sampleType: "float", viewDimension: "2d-array" };
else if (b.kind === 4) e.buffer = { type: "storage" };
else if (b.kind === 5) e.storageTexture = { format: texelFormatStr(b.format), access: "write-only", viewDimension: "2d" };
return e;
});
userBgls.push(device.createBindGroupLayout({ entries }));
@ -3172,7 +3227,7 @@ env.wgpuLoadRTPipeline = (wgslPtr, wgslLen, bindingsPtr, bindingsCount) => {
shadePipe: mk(userLayout, "wfShade"),
resolvePipe: mk(userLayout, "wfResolve"),
paramsBgl, dataBgl, indirectBgl, emptyBgl, userBgls,
byGroup, sortedGroups, traceHasUser,
byGroup, sortedGroups, traceHasUser, hdrOutput: hdrOut,
};
const handle = newHandle();
rtPipelines.set(handle, entry);
@ -3203,6 +3258,7 @@ function wfUserBindGroups(pipe, handlesPtr, handlesCount) {
else if (b.kind === 2) resource = samplers.get(h);
else if (b.kind === 3) resource = textureViews.get(h);
else if (b.kind === 4) resource = { buffer: buffers.get(h) };
else if (b.kind === 5) resource = textureViews.get(h);
return { binding: b.binding, resource };
});
out.push({ group: g, bindGroup: device.createBindGroup({ layout: pipe.userBgls[bglIdx], entries }) });
@ -3214,7 +3270,7 @@ function wfUserBindGroups(pipe, handlesPtr, handlesCount) {
env.wgpuDispatchRT = (pipelineHandle, pushPtr, pushBytes,
tlasBufHandle, instanceCount, gx, gy,
handlesPtr, handlesCount, maxDepth) => {
handlesPtr, handlesCount, maxDepth, outTexHandle) => {
if (!state.encoder) return;
const pipe = rtPipelines.get(pipelineHandle);
const tlas = buffers.get(tlasBufHandle);
@ -3222,6 +3278,17 @@ env.wgpuDispatchRT = (pipelineHandle, pushPtr, pushBytes,
console.error("[crafter-wgpu] wgpuDispatchRT: unknown pipeline or tlas");
return;
}
// HDR-output pipelines write RESOLVE into a user-provided storage
// texture (binding 6) rather than the canvas ping-pong. The handle is
// required in that case and its format must match the one the pipeline
// was loaded with.
if (pipe.hdrOutput) {
const userOut = textureViews.get(outTexHandle >>> 0);
if (!userOut) {
console.error("[crafter-wgpu] wgpuDispatchRT: HDR pipeline needs a valid outTexHandle");
return;
}
}
const entryOrderBuf = buffers.get(rtState.currentEntryOrder);
const bvhBuf = buffers.get(rtState.currentBvh);
if (!entryOrderBuf || !bvhBuf) {
@ -3259,7 +3326,9 @@ env.wgpuDispatchRT = (pipelineHandle, pushPtr, pushBytes,
writeSlot(1 + 3 * depth, 1, depth); // RESOLVE
queue.writeBuffer(wf.paramsRing, 0, ring, 0, passCount * 64);
const outView = state.outIsPing ? state.pingView : state.pongView;
const outView = pipe.hdrOutput
? textureViews.get(outTexHandle >>> 0)
: (state.outIsPing ? state.pingView : state.pongView);
const paramsBg = device.createBindGroup({
layout: pipe.paramsBgl,
entries: [{ binding: 0, resource: { buffer: wf.paramsRing, offset: 0, size: 256 } }],
@ -3389,9 +3458,12 @@ env.wgpuDispatchRT = (pipelineHandle, pushPtr, pushBytes,
}
// Reopen the frame's shared pass so wgpuFrameEnd / later UI work as
// before, and flip ping-pong so the blit picks the texture RESOLVE wrote.
// before. For canvas output, flip ping-pong so the blit picks the
// texture RESOLVE wrote. For HDR output RESOLVE wrote a user texture and
// left the ping-pong untouched, so the flip is skipped — the app's own
// composite pass is responsible for writing the canvas.
state.pass = enc.beginComputePass();
state.outIsPing = !state.outIsPing;
if (!pipe.hdrOutput) state.outIsPing = !state.outIsPing;
};
// ── Standalone compute pipelines ────────────────────────────────────────
@ -3434,6 +3506,7 @@ env.wgpuLoadComputePipeline = (wgslPtr, wgslLen, pushUniformSize,
group: dv.getUint8(i*8 + 0),
binding: dv.getUint8(i*8 + 1),
kind: dv.getUint8(i*8 + 2),
format: dv.getUint8(i*8 + 3), // texel format for storage-texture kind
pushOffset: dv.getUint32(i*8 + 4, true),
});
}
@ -3492,6 +3565,7 @@ env.wgpuLoadComputePipeline = (wgslPtr, wgslLen, pushUniformSize,
else if (b.kind === 2) e.sampler = { type: "filtering" };
else if (b.kind === 3) e.texture = { sampleType: "float", viewDimension: "2d-array" };
else if (b.kind === 4) e.buffer = { type: "storage" }; // read-write storage
else if (b.kind === 5) e.storageTexture = { format: texelFormatStr(b.format), access: "write-only", viewDimension: "2d" }; // write-only storage texture
return e;
});
bgls.push(device.createBindGroupLayout({ entries }));
@ -3620,6 +3694,7 @@ env.wgpuDispatchCompute = (pipelineHandle, pushPtr, pushBytes,
else if (b.kind === 1) resource = textureViews.get(h);
else if (b.kind === 2) resource = samplers.get(h);
else if (b.kind === 3) resource = textureViews.get(h);
else if (b.kind === 5) resource = textureViews.get(h);
return { binding: b.binding, resource };
});
userBGs.push(device.createBindGroup({ layout: pipe.bgls[bglIdx++], entries }));