arkavo-org / arkavo-org/VRMMetalKit
MToon fragment shader is ALU/texture heavy with no early-exit
- Dominant language
- Swift
- Stars
- 6
- Forks
- 2
- Avg merge
- 18h 51m
- Merged PRs (30d)
- 26
Description
## Summary
The MToon fragment shader (`mtoon_fragment_v2`) samples up to 8 textures per fragment, runs 3 light loops, and performs expensive transcendental math (`pow`, `cos`, `sin`, `dfdx`/`dfdy` for TBN reconstruction). There is no early-exit for simple or unlit materials.
## Impact
- High ALU and texture pressure on the GPU.
- Screen-space TBN reconstruction (`dfdx`/`dfdy` + `rsqrt`) is executed for every normal-mapped fragment.
- `pow()` for rim Fresnel is evaluated per fragment even when rim lighting contribution is near zero.
## Location
- Fragment shader: `Sources/VRMMetalKit/Shaders/MToonShader.metal:514–708`
- TBN reconstruction: `:563–576`
- Rim pow: `:699`, `:731`
- Light loops: `:612–636`
## Suggested Fixes
1. **Shader specialization via `[[function_constant]]`:** Compile variants without normal maps, without rim, or with 0/1/2 lights.
2. **Early-exit branch:** Add a fast path for materials with `shadeToony == 1.0` and no normal map / no rim.
3. **Precompute TBN:** Move TBN to vertex shader if the normal map transform is uniform.
4. **Use half-precision** for fragment color math where possible (per roadmap item #7).
Contributor guide
Research direction
Start with Sources/VRMMetalKit/Shaders/MToonShader.metal:514–708, focusing on TBN reconstruction at 563–576, light loops at 612–636, and rim pow calls at 699 and 731. Compare the proposed specialization, early-exit, TBN, and precision approaches against the shader’s existing paths. Done means a selected optimization preserves MToon behavior and reduces the identified ALU or texture costs.
Written by the indexing model from the issue text.
Assessment
- Domain
- computer-graphics, performance
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100