Here's a practical approach to reducing VRAM usage in your PBR shader while maintaining visual quality:
Immediate Solutions
- Reduce Texture Resolution
- Drop from 4K (4096×4096) to 2K (2048×2048)
- This alone reduces memory usage by 75%
- For most cases, 2K textures are sufficient for close-up detail
- Use mipmaps to handle distance falloff properly
- Optimize Texture Formats
// Instead of separate textures:
uniform sampler2D albedoMap; // RGB - 4K
uniform sampler2D normalMap; // RGB - 4K
uniform sampler2D roughnessMap; // R - 4K
uniform sampler2D metallicMap; // R - 4K
uniform sampler2D aoMap; // R - 4K
// Combine into:
uniform sampler2D albedoMap; // RGB - 2K
uniform sampler2D normalMap; // RGB - 2K
uniform sampler2D materialMap; // RGB - 2K (R=roughness, G=metallic, B=AO)
- Use Appropriate Compression
- For diffuse/albedo: BC1/DXT1 (if opacity not needed) or BC3/DXT5
- For normals: BC5/3DC (better quality for normal maps)
- For packed material maps: BC7 for highest quality
Advanced Optimization Strategies
- Texture Atlas Implementation
// In your vertex shader:
out vec2 vUV;
out vec2 vAtlasOffset;
// In your fragment shader:
vec2 atlasUV = vUV * atlasScale + vAtlasOffset;
vec3 albedo = texture(atlasTexture, atlasUV).rgb;
- Procedural Detail Generation
// Add detail without textures
float microDetail = noise(uv * detailScale);
roughness = baseRoughness + microDetail * detailStrength;
- Tiled Texturing
// Make smaller textures tile seamlessly
vec2 tiledUV = fract(uv * tileScale);
vec3 albedo = texture(albedoMap, tiledUV).rgb;
Memory-Efficient Material System
- Implement Material ID System
// Instead of unique textures per material
uniform sampler2DArray materialArray; // Shared texture array
uniform int materialID;
// In shader
vec3 albedo = texture(materialArray, vec3(uv, materialID)).rgb;
- Dynamic LOD System
float computeLOD(vec3 viewPos, vec3 worldPos) {
float distance = length(viewPos - worldPos);
return log2(distance * lodScale);
}
// In shader
vec3 albedo = textureLod(albedoMap, uv, computeLOD(viewPos, worldPos)).rgb;
Example Implementation
#version 450
// Inputs
in vec2 vUV;
in vec3 vWorldPos;
in vec3 vNormal;
// Textures
uniform sampler2D albedoMap; // 2K RGB - BC1/DXT1
uniform sampler2D normalMap; // 2K RGB - BC5
uniform sampler2D materialMap; // 2K RGB - BC7 (R=rough, G=metal, B=AO)
// Camera
uniform vec3 viewPos;
void main() {
// Compute appropriate LOD
float distance = length(viewPos - vWorldPos);
float lod = log2(distance * 0.1);
// Sample textures with LOD
vec3 albedo = textureLod(albedoMap, vUV, lod).rgb;
vec3 normal = normalize(textureLod(normalMap, vUV, lod).rgb * 2.0 - 1.0);
vec3 material = textureLod(materialMap, vUV, lod).rgb;
// Unpack material properties
float roughness = material.r;
float metallic = material.g;
float ao = material.b;
// Optional: Add procedural detail at close range
if(distance < 2.0) {
float detail = noise(vUV * 20.0);
roughness = mix(roughness, roughness * detail, 1.0 - distance * 0.5);
}
// Continue with PBR calculations...
}
Additional Tips
- Texture Streaming
- Implement a system to load higher resolution textures only when needed
- Keep lower resolution mips resident in VRAM
- Stream in higher resolution data based on camera distance
- Material Instances
- Create material instances that share base textures but vary parameters
- Use uniform buffers to store per-instance parameters
- Distance-Based Detail
float detail = 1.0 - smoothstep(nearDistance, farDistance, distance);
vec3 highResAlbedo = textureLod(albedoMap, uv, 0).rgb;
vec3 lowResAlbedo = textureLod(albedoMap, uv, 4).rgb;
vec3 finalAlbedo = mix(lowResAlbedo, highResAlbedo, detail);
- Virtual Texturing
- Consider implementing virtual texturing for very large textures
- Only load required portions of textures into VRAM
These optimizations should significantly reduce VRAM usage while maintaining visual quality where it matters most. Start with the simplest solutions (reducing resolution, combining textures) and move to more complex solutions if needed.