gl_MaxClipDistances Limit in OpenGL and GLSL
This article examines the hardware limits of user-defined clipping in
modern OpenGL and GLSL, focusing on the gl_MaxClipDistances
constant. You will learn the typical and minimum number of clipping
planes exposed across consumer hardware, how hardware constraints shape
these limits, how to query them at runtime, and alternative strategies
for applications requiring more clipping planes than the driver natively
supports.
The Standard Maximum: 8 Clip Planes
In modern OpenGL (version 3.0 and newer) and GLSL, the typical
maximum number of user clip planes exposed via
gl_MaxClipDistances is 8.
The core OpenGL specification mandates that the
implementation-dependent constant GL_MAX_CLIP_DISTANCES
must be at least 8. While graphics hardware manufacturers are permitted
to expose more, virtually all modern consumer GPUs from NVIDIA, AMD, and
Intel expose exactly 8.
Why the Limit Is Typically 8
The widespread standard of 8 clipping planes exists primarily due to cross-API hardware parity:
- DirectX Alignment: Direct3D 10, 11, and 12 specify
a hardware limit of 8 clip distances (
SV_ClipDistance). Because GPU silicon is designed to satisfy both DirectX and OpenGL specifications efficiently, hardware vendors build fixed-function clipping units that match this baseline requirement. - Hardware Overhead: Clipping requires dedicated interpolation and boundary checks during primitive assembly and rasterization. Supporting more than 8 simultaneous arbitrary clipping planes in hardware would increase silicon area and complexity for a feature rarely needed in higher quantities.
Querying the Limit
You can determine the exact limit available on a target system using either GLSL or host OpenGL API calls.
In GLSL
GLSL provides the built-in integer constant:
// gl_MaxClipDistances is available in vertex, geometry, and fragment stages
int maxPlanes = gl_MaxClipDistances;In C / C++ (OpenGL Host API)
You can verify the value on the CPU side before compiling your shaders:
GLint maxClipDistances = 0;
glGetIntegerv(GL_MAX_CLIP_DISTANCES, &maxClipDistances);How to Use gl_ClipDistance
To utilize hardware clip planes up to the limit:
- Enable the Planes on the Host: Each plane must be
enabled individually using
glEnable(GL_CLIP_DISTANCE0 + i)whereiranges from0togl_MaxClipDistances - 1. - Assign Distances in the Shader: In the vertex or
geometry shader, write signed float distances to the
gl_ClipDistance[]array. A negative distance indicates that the vertex is clipped.
#version 330 core
layout(location = 0) in vec3 aPos;
uniform vec4 uClipPlane0; // Plane equation (A, B, C, D)
void main()
{
gl_Position = vec4(aPos, 1.0);
// Calculate distance to the plane: dot(plane.xyz, pos) + plane.w
gl_ClipDistance[0] = dot(uClipPlane0, vec4(aPos, 1.0));
}Handling More Than 8 Clip Planes
If an application requires more than 8 simultaneous clipping boundaries, you cannot rely exclusively on fixed-function hardware clipping. Common workarounds include:
- Fragment Shader Discard: Calculate plane equations
manually inside the fragment shader and call
discardwhen the fragment falls outside any defined boundary. This approach has no strict limit on the number of planes, though it may disable early-Z optimizations. - Multi-Pass Rendering: Render the geometry in multiple passes using different sets of clipping planes and combine the results via depth testing and stencil buffers.