1# gl_ClipDistance extension support in Vulkan back-end
2
3OpenGL GLSL's `gl_ClipDistance` is supported by Vulkan. However, OpenGL supports disabling/enabling
4individual `gl_ClipDistance[i]` on the API level side. Writing to `gl_ClipDistance[i]` in shader
5will be ignored if it is disabled. Vulkan doesn't have any equivalent API to disable/enable the
6writing, though writing to a `gl_ClipDistance[i]` variable automatically enables it.
7
8To implement this enabling/disabling API in Vulkan back-end:
9
10- The shader compiler will translate each `gl_ClipDistance[i]` assignment to an assignment to
11  `ANGLEClipDistance[i]` variable.
12- A special driver uniform variable `clipDistancesEnabled` will contain one bit flag for each
13  enabled `gl_ClipDistance[i]`. This variable supports up to 32 `gl_ClipDistance` indices.
14- At the end of vertex shader, the enabled `gl_ClipDistance[i]` will be assigned the respective
15  value from `ANGLEClipDistance[i]`. On the other hand, those disabled elements will be assigned
16  zero value. This step is described in the following code:
17    ```
18    for (int index : arraylength(gl_ClipDistance))
19    {
20        if (ANGLEUniforms.clipDistancesEnabled & (0x1 << index))
21            gl_ClipDistance[index] = ANGLEClipDistance[index];
22        else
23            gl_ClipDistance[index] = 0;
24    }
25    ```
26- Some minor optimizations:
27    - Only those indices that are referenced in the original code will be used in if else block
28      above.
29    - Those elements whose index not referenced in the original code will be zeroised instead.
30    - If the original code only uses up to an index < `gl_MaxClipDistances`, then
31      the loop will have at most `index+1` iterations. If there is at least one index not being
32      integral constant value known at compile time then declared size of `gl_ClipDistance`
33      will be the loop size.
34    - Finally, if the original code doesn't use `gl_ClipDistance`, then all the steps above will be
35      omitted.