## TL;DR
Shaders are GPU programs that define how 3D geometry transforms into screen pixels. Vertex shaders position geometry; fragment shaders color each pixel. Modern real-time rendering relies entirely on custom shaders for lighting, materials, and effects.
## Shader Pipeline
### Vertex Shader
Transforms 3D vertex positions from model space → world space → view space → clip space. Also computes per-vertex data (normals, UVs, tangents) for later stages.
### Fragment (Pixel) Shader
Computes the final color of each pixel. This is where lighting models (Phong, PBR), texture sampling, normal mapping, and post-processing effects execute. Fragment shaders run millions of times per frame — optimization is critical.
### Geometry and Tessellation Shaders
Optional pipeline stages for generating or subdividing geometry on the GPU. Tessellation dynamically adds detail to meshes based on camera distance for LOD effects.
## Common Shader Techniques
- **Phong/Blinn-Phong**: Classic specular lighting model
- **PBR (Physically Based Rendering)**: Energy-conserving materials using roughness/metallic maps
- **Normal Mapping**: Simulates surface detail without extra geometry
- **Shadow Mapping**: Depth comparison from light's perspective
- **Screen-Space Effects**: Ambient occlusion, reflections, bloom
## Performance Tips
- Avoid dynamic branching (if/else) when possible — GPUs execute all paths
- Prefer texture lookups over complex math in fragment shaders
- Batch draw calls by material/shader to minimize state changes
- Use mipmaps to reduce texture bandwidth at distance