shader dsl: add discard() for cut-out fragments - #5
Open
shadowcodex wants to merge 1 commit into
Open
Conversation
|
@shadowcodex is attempting to deploy a commit to the Eric Rowell's projects Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
discard()to the shader DSL. A fragment stage can throw the current fragment away, and the GPU writes no colour and no depth for it.Why
A sprite with a transparent edge has one option today: blend it. A blended program cannot write depth, because a part-transparent fragment has no single depth to record. So the application has to sort sprites back-to-front on the CPU every frame and re-send the instance data.
discard()removes the fragment instead. Every fragment that survives is opaque, so the program writes depth and the GPU orders the sprites per pixel. Nothing sorts, nothing re-uploads.What changed
Small: one new IR statement, one keyword in the WGSL emitter, one case in the constant folder, and the builtin plus its export.
The compiler permits it in the fragment stage only, and as a statement only. It gives a named error for each way of getting it wrong — in
vertex(), inside a helper (whichvertex()may call, where discarding is meaningless), used as a value, and called with arguments.Tests
9 new tests in
packages/brometal/tests/discard.test.ts: WGSL output inside a branch and from anelse, survival through the optimizer, top-level use before the return, all four rejections, and a varying kept alive when only the discard condition reads it — that last one is the case a dead-varying pass would prune and quietly break.327 tests pass,
npm run typecheckclean.Provenance, and a note on scope
This is extracted from #2, which bundles
discard()with seven other library changes and four demos. That PR is being closed in favour of focused ones, of which this is the first — it carries nothing butdiscard().It is ported, not cherry-picked: #2 predates the WebGL2 removal, so its
emit-glslhalf no longer applies and its GLSL assertion is dropped. The result-shape rename fromfragmentSrc/vertexSrctowgslSrcis reflected in the tests. The implementation and the error messages are otherwise the ones from that branch.Over to you
Genuine questions, not politeness: