Give compute shaders access to workgroup info#8820
Conversation
|
So I was testing this shader: countPixelsChanged = buildComputeShader({
declarations: `
@group(0) @binding(1)
var prevFrame: texture_2d<f32>;
@group(0) @binding(2)
var prevFrame_sampler: sampler;
@group(0) @binding(3)
var nextFrame: texture_2d<f32>;
@group(0) @binding(4)
var nextFrame_sampler: sampler;
@group(0) @binding(5)
var<storage, read_write> pixelsChanged: array<f32>;
var<workgroup> localCount: atomic<u32>;
fn luminance(c: vec3<f32>) -> f32 {
return dot(c, vec3<f32>(0.2126, 0.7152, 0.0722));
}
`,
'void computeIteration': `(inputs: ComputeInputs) {
if (inputs.localIndex == 0) {
atomicStore(&localCount, 0u);
}
workgroupBarrier();
let coord = vec2<i32>(inputs.index.xy);
let prevColor = textureLoad(prevFrame, coord, 0).rgb;
let nextColor = textureLoad(nextFrame, coord, 0).rgb;
let prevLum = luminance(prevColor);
let nextLum = luminance(nextColor);
let delta = abs(nextLum - prevLum);
let flashed = delta > 0.01; // TODO
if (flashed) {
atomicAdd(&localCount, 1u);
}
workgroupBarrier();
// One thread writes one float
if (inputs.localIndex == 0) {
let workgroupsX =
(uniforms.uPhysicalCount.x + 7) / 8;
let groupIndex =
inputs.workgroupID.x +
inputs.workgroupID.y * workgroupsX;
pixelsChanged[groupIndex] =
f32(atomicLoad(&localCount));
}
}`
});This works in Firefox, but in Chrome gives: ...because currently before running the compute shader hook, we have a possible early return. This is because the number of threads you have will not always be EXACTLY the number of iterations of the shader you want to run, and it makes for a very simple compute shader API if we early return those few extra ones so that you don't have to worry about it in your own functions. But having that early return seems to be fundamentally incompatible with the use of barriers. So maybe that means we need a separate hook or shader for these advanced use cases? |
|
example of the above: this works in Firefox but not Chrome https://editor.p5js.org/davepagurek/sketches/xRmwoEIcK example of a working compute shader in both browsers due to it not using barriers: https://editor.p5js.org/davepagurek/sketches/IqdsZPqze That second sketch doesn't need barriers due to its use of |
|
I looked at the diff, the early return guard in compute.js fires before the hook even runs, so any maybe a separate entry point or a flag on |
When I first implemented compute shaders, the API for the hook was very simple: just pass in a
vec3index.We've been talking about what it would take to implement something like WCAG flash detection via a shader. A compute shader could possibly do this, but would be done most efficiently with some reduction of sums in workgroup scope before writing out to a final value. This is an initial draft at starting to give compute shaders some of that access in shader hooks/strands without changing the existing API -- you can still refer to
indexas before, but this exposes a few more properties with more info.