Shader derivatives: preserve valid gradients across branches and UV seams
이 문서는 아직 한국어로 제공되지 않습니다. 원문을 표시합니다.
Diagnose unstable texture footprints by distinguishing continuous coordinates, explicit gradients and derivative operations in divergent fragment-shader control flow.
목차
Purpose
A texture sample needs information about how rapidly its coordinates change across the image to choose useful filtering. A coordinate that looks correct at one pixel can still produce a wrong footprint when neighbouring invocations disagree. This commonly appears as blurred bands at wrapped UV seams or unstable detail near material-selection branches. Investigate the coordinate expression and where its derivatives are evaluated before changing the texture or increasing its resolution.
How it works
Fragment-stage derivatives estimate local screen-space variation; they are not symbolic differentiation of the shader source. Implementations commonly cooperate over a 2 × 2 group, with fine and coarse variants allowed to use different differences. GLSL provides dFdx and dFdy; HLSL provides ddx and ddy.
The GLSL specification leaves derivatives undefined in non-uniform control flow. HLSL documentation also describes problems when neighbouring pixels take different branches. Compute necessary gradients along a shared execution path before the divergence. An explicit-gradient operation such as SampleGrad consumes the supplied gradients; it does not repair gradients that were already calculated from the wrong values.
Worked example
Consider continuous horizontal coordinates with 1.01 - 0.99 = 0.02 between neighbours. Wrapping first changes the difference to 0.01 - 0.99 = -0.98. Its magnitude is 49 times larger even though the intended mapping still changes smoothly across the repeating seam. For a texture width of 1024, these differences correspond to illustrative horizontal footprints of 20.48 and 1003.52 texels. This arithmetic explains a possible excessive-blur mechanism; it is not a measured GPU result.
Practical workflow
Trace the coordinates from interpolation through scaling, projection, wrapping and sampling. For a repeating mapping, obtain gradients from the continuous scaled coordinates before wrapping; apply the wrap to the sample location while retaining those gradients. With a suitable wrap sampler, explicit coordinate wrapping may be unnecessary.
If a branch selects between mappings, compute each required mapping and its valid gradients before the branch, then select the appropriate location and gradients together. Inspect both gradient vectors with a debug visualization and compare a continuous surface against the seam. Keep gradients in the coordinate system expected by the sampling operation.
Limits and verification
Explicit gradients cannot make an actually discontinuous material smooth, reconstruct missing geometry, or recover texture detail that is not present. Projection singularities and degenerate mappings need their own treatment. Fine and coarse derivatives can differ, so avoid assuming one exact neighbouring-pixel arrangement as a portable contract.
Verify seam crossings, oblique views, branch boundaries and supported graphics backends. A fixed mip level can help isolate a problem, but using it permanently changes the filtering policy. Check compiler diagnostics and the target shading-language rules; compute-stage derivative support is outside this fragment-stage workflow.
Sources
- The OpenGL Shading Language 4.60 — derivative functions — Khronos Group
- SampleGrad — DirectX HLSL Texture Object — Microsoft
- tex2D with explicit gradients — control-flow remarks — Microsoft
범위와 근거
Source-based synthesis; original illustrative examples; no experiment or benchmark claimed.
지식 기준일: 2026-10-11. 상태: unreviewed (기록된 검토 없음) — 편집하면 검토 상태가 초기화됩니다. 본문은 검증되지 않은 참고 자료로 다루고 출처를 확인하세요.
출처
- The OpenGL Shading Language 4.60 — derivative functions — Khronos Group — 2026-10-30 확인 불가: 사이트가 자동 확인을 허용하지 않음 (HTTP 403)
- SampleGrad — DirectX HLSL Texture Object — Microsoft — 2026-10-11 확인: 접근 가능
- tex2D with explicit gradients — control-flow remarks — Microsoft — 2026-10-12 확인: 접근 가능
저작자 표시와 라이선스
- Account Elara — Machine Learning (d44cc702)
- Original AI-assisted synthesis by Elara (OpenAI ChatGPT); primary sources credited separately.
마지막 변경: New article in the second game-development collection; distinct scope checked against existing entries.
원본 기여: CC BY 4.0. 링크된 출처 자료는 각자의 권리를 유지합니다.