Reducing memory throughput in a fragment shader is crucial for improving GPU performance, minimizing bottlenecks, and increasing rendering efficiency—especially on bandwidth-constrained platforms like mobile devices or VR. Memory throughput in this context refers to the volume of data read from or written to memory (textures, framebuffers, etc.) during fragment processing.
Here's a comprehensive, categorized list of techniques to reduce memory throughput in fragment shaders, along with explanations:
🔁 GENERAL STRATEGIES
1. Minimize Texture Lookups
- Reduce the number of
texture() calls in the shader.
- Combine multiple texture maps into a single texture atlas or packed texture (e.g., pack roughness, metalness, and AO into different channels of one texture).
- Use compressed textures (e.g., BCn, ASTC, ETC2) to reduce bandwidth (though not memory reads in the fragment shader directly, but bandwidth-wise).
2. Avoid Redundant Computations
- Cache expensive or repeated calculations in temporary variables to avoid recomputation and redundant memory reads.
3. Reduce Render Target Writes
- Only write to the framebuffer or multiple render targets (MRT) when really needed (
gl_FragColor, gl_FragData[], etc).
- In multi-pass rendering, avoid unnecessary outputs in early passes.
🧮 OPTIMIZE ALGORITHM CHOICES
4. Use Cheaper Approximations
- Use approximate math instead of high-precision computation if visual fidelity is not heavily affected.
- E.g., replace
pow(x, y) with exp2(y * log2(x)) or use a lookup.
- Replace complex instructions with texture-based lookups only if lookup is fewer and cheaper than math.
5. Avoid Branches with Divergence
- Divergent branches can lead to all branches being evaluated, increasing memory access if conditional texture sampling is involved.
🖼 TEXTURE / SAMPLING BASED OPTIMIZATIONS
6. Use Mipmapping
7. Leverage Texture Gathering
- Use
textureGather() to fetch multiple neighboring texels in one call, often used for complex filters like PCF shadows.
📉 REDUCE OVERDRAW
8. Use Early-Z / Depth Testing Effectively
- Enable and configure early fragment tests to prevent unnecessary fragment shading:
- Use
discard sparingly, as it disables Early-Z.
- Pre-pass Z or depth pre-pass helps eliminate overdraw in opaque rendering.
- Front-to-back rendering order: reduce overdraw by rendering closer objects first.
9. Use Occlusion Culling and Level-of-Detail (LOD)
- Cull invisible geometry early to avoid executing fragment shaders at all.
- Reduce complexity/detail of distant objects/textures.
🎨 FRAMEBUFFER / RENDER TARGET MANAGEMENT
10. Avoid Unnecessary MRT (Multiple Render Targets) Writes
- Only enable multiple render targets when necessary. Writing to more than one render target increases bandwidth use significantly.
11. Restrict Color Outputs
- Avoid high-precision or high-channel framebuffer formats unless required:
- Prefer
GL_RGBA8 over GL_RGBA32F unless you truly need float precision.
- Consider
GL_RGB565 or other low-bit formats for mobile performance.
12. Use Blending Carefully
- Blending can require reading and writing the same framebuffer memory:
- Avoid alpha blending when not needed.
- Use order-independent transparency (OIT) sparingly.
🧠 SHADER ARCHITECTURE AND USAGE
13. Do Work in Vertex or Compute Shaders
- Move per-fragment computations to vertex shaders or preprocessing steps when possible. Interpolation is cheaper than computing in the fragment stage.
14. Use Screen-space Techniques Efficiently
- For post-processing, use lower-resolution render targets when possible (e.g., half or quarter-resolution for effects like bloom or SSAO).
- Avoid full-resolution passes when not necessary.
🗃 TEXTURE FORMATS AND USAGE
15. Prefer Lower Bit Depths
- Use lower precision texture formats for data storage:
GL_RGBA8 instead of GL_RGBA32F when float precision is overkill.
- Use
GL_R16F, GL_RG8, etc., if only a subset of channels or precision is needed.
16. Channel Packing
🧱 HARDWARE-FRIENDLY PRACTICES
17. Use Mediump or Lowp Where Possible (especially in mobile/OpenGL ES)
- Use lower precision where acceptable:
precision mediump float;
18. Align Memory Access
- Optimize access patterns to match hardware expectations (e.g., avoiding strided or misaligned access in procedural textures or buffers).
🧪 DEBUGGING AND TOOLS
19. Use GPU Profilers (NSight, RenderDoc, Xcode, etc.)
- Identify hotspots and high-memory-throughput regions.
- Look for:
- High texture memory bandwidth.
- Framebuffer bandwidth bottlenecks.
- Overdraw metrics.
📚 ADVANCED TECHNIQUES (if applicable)
20. Use Tile-based Rendering Aware Design
- Many mobile GPUs (e.g., Adreno, Mali) use tile-based deferred rendering. Reducing framebuffer bandwidth is critical here.
- Avoid unnecessary blending and framebuffer reads.
- Take advantage of depth pre-passes and scissor testing.
21. Use Deferred Shading Carefully
- In forward rendering, memory write cost is per-pixel final color.
- In deferred rendering, memory throughput can be much higher due to large G-buffers (normals, depth, material info).
✔ SUMMARY CHEAT SHEET
| Technique | Benefit |
|---|
| Reduce texture reads | ↓ Bandwidth |
| Use low-bit formats | ↓ Texture & framebuffer memory access |
| Move logic to vertex/compute | ↓ Fragment workload |
| Efficient depth testing & culling | ↓ Overdraw |
| Use mipmaps & LOD | ↓ Texture bandwidth |
| Combine maps / channel pack | ↓ Sampling cost |
| Avoid discard & blending | ↑ Early-Z benefit |
| Reduce render target writes | ↓ Framebuffer bandwidth |
| Profile with tools | 🎯 Real bottlenecks |
If you're working in GLSL, optimizing fragment shader memory usage is a tight balance between performance and visual fidelity. Most of these strategies are complementary and should be applied based on profiling results and hardware constraints.
Let me know if you'd like examples for any strategy!