Repository navigation
Conversation
davepagurek
left a comment
There was a problem hiding this comment.
This is looking good! I think the approach here is sound and your handling of storage buffers in textures for non-compute shaders is cool. I left a few thoughts on the API and some things that have been performance issues in the past.
| // Data textures must not be alpha-premultiplied on upload (p5 leaves | ||
| // premultiplying on outside text drawing, which would zero RGB wherever | ||
| // alpha is 0, e.g. a scalar list holding 1.0 in red). | ||
| const previousPremultiply = gl.getParameter(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL); |
There was a problem hiding this comment.
getParameter calls are often a bottleneck; we might want to consider recording the current state to a variable when we change it so that we don't need to read from the GPU here.
There was a problem hiding this comment.
yes, that sounds like a good fix to me. i would prefer tracking the current state in a variable over changing the layout, which would cost 2x the texels for dim-4 fields.
| return ['x', 'y', 'z', 'w'].slice(0, field.dim).map(c => value[c]); | ||
| } | ||
| if (value?.isColor) return value._getRGBA().slice(0, field.dim); | ||
| return [Number(value)]; |
There was a problem hiding this comment.
Do storage buffers handle p5.Matrix yet? If so we can add that here, otherwise feel free to ignore!
There was a problem hiding this comment.
checked it, storage buffers dont handle p5.Matrix yet
| let offset = 0; | ||
| fields = buffer._schema.fields.map(field => { | ||
| const entry = { ...field, offset }; | ||
| offset += texelCountForDim(field.dim); |
| blocks: sortedIDs, | ||
| types: sortedIDs.map(id => cpuIR.cfg.blockTypes[id]) | ||
| }; | ||
| emitRange(g, cpuIR, sorted, 0, sortedIDs.length); |
There was a problem hiding this comment.
Is the idea that this will map the different nodes to the helpers above, e.g. callBuiltin? That works. We might want to set up a benchmark of how fast something compiled like this runs (maybe an example of balls bouncing off the sides of the canvas?) compared to a hand-coded js version to see where we're at in terms of performance so that we can optimize over time.
There was a problem hiding this comment.
yes, generated body calls helpers.
added a benchmark which compares lowering + running a 100-particle position += velocity compute shader against the equivalent hand-written loop. it clears the cached compiled hook per iteration so lowering is included.
the handwritten loop is about 12x faster (averaged over 5 runs). lowering is ~2% of the combined call, meaning the gap is per-particle storage/vector dispatch.
| throw new Error(`compute() counts must be non-negative integers. Got (${x}, ${y}, ${z}).`); | ||
| } | ||
| if (total > CPU_COMPUTE_WARN_ITEMS) { | ||
| console.warn( |
There was a problem hiding this comment.
Currently it looks like this will run every frame right? I wonder if rather than warning based on the number of items, we only enable the CPU fallback if you opt in, e.g. calling enableComputeShaderFallback() or something, as a way of acknowledging that you've thought about the counts etc. Could be worth making an example where we check the current renderer and change the number of instances based on it.
There was a problem hiding this comment.
good catch. i should remove that warning.
i agree that opting in makes sense, though i'm not sure we need to introduce a new function for it.
could we keep it simpler by only enabling the fallback when the user explicitly provides a instance count for the webgl renderer as an explicit opt-in to the fallback, without introducing another function??
the message could cover two cases:
- if the author's browser doesn't support webgpu: "compute shaders are not supported in your browser. define instance count for webgl renderer using
rendererTypefor cpu fallback." - if their browser supports webgpu: "to make your sketch accessible to browsers without webgpu support, define instance count for the webgl renderer using
rendererType."
an example would be a good idea. there isn't an example for compute shaders yet, so we could combine both concepts into one.
There was a problem hiding this comment.
That's a good idea. Right now it takes a variable number of dimensions so adding more positional arguments is difficult, possibly it'd need to have a new overload that looks like compute(shader, { gpu: [x, y, z], cpu: [x, y z] }) if you want a fallback? or a { cpu: [dimensions] } options argument at the end of the gpu positional arguments?
d0c34ee to
4b3770d
Compare
|
@skyyash i think this might be aliasing the PHI with its first input here. if i save a value before a loop and then update it inside the loop, WebGPU keeps the old value, but the CPU fallback ends up updating both. i traced it back to the PHI reusing the first input’s temp. giving the PHI its own temp made the CPU result match WebGPU again in the cases i tried. do you think the PHI should always get its own temporary here? |
@bhabishnu good catch! yes i also think, the phi nodes always need their own variables to avoid aliasing inputs. |
Resolves #9223
Changes:
draft
PR Checklist
npm run lintpasses