<p>opengl has several methods that together let you reflect the shader IO.</p>

<p>However it is much simpler if the shader IO is uniform across all your shaders. IOW have a set of default bindings/locations for everything. Then you only need some meta data for each shader where you deviate from those defaults for some reason.</p>

<p>You then only need to query that the binding exists to see whether you need to provide the data. This has the added bonus that if an input gets optimized out you'll not waste time giving data it doesn't need.</p>

CC BY-SA 3.0
2018-03-28T09:31:28.850Z
For most of our shaders, we use fixed slots but in some cases we need to retrieve uniform bindings (eg. I'm generating UBOs for bindless textures).
Nacho
CC BY-SA 3.0
2018-03-28T08:34:31.547Z
2018-03-28T08:34:31.547Z
ratchet freak
<p>I'm planning the port of our DX based renderer to OpenGL. The target will be Desktop OpenGL 4.x. For DX we use shader reflection but as far as I know OpenGL just has <em>glGetProgramInterfaceiv</em> , <em>glGetUniform</em> and frieds. Theres also SPIR-V (I have never worked with it).</p>

<p>My main concerns right now are:</p>

<ul>
<li><strong>Using gl*:</strong> it will be done at runtime, so this will be potentially slow as there are a lot of shaders to load (+ the interface is ugly as fuck). <em>EDIT: I guess this could be done on a prebuild step too (?).</em></li>
<li><strong>Use SPIR-V:</strong> means that we need to add another build step and I don't see much doc around. </li>
</ul>

<p>I would like to hear advice from people with experience with this.
Thanks a lot!</p>

CC BY-SA 3.0
2018-03-27T15:36:34.000Z
what do you need reflected? uniform and attrib locations?
ratchet freak
CC BY-SA 3.0
2018-03-27T17:02:35.217Z
Yes, I think I'm only looking for names + bindings (also from inside UBOs) also uniform types (UBOs,SSBOs,images etc)
Nacho
<opengl><shader>
Dealing with GL reflection
Nacho
# Dealing with GL reflection

(Asked by: username_0 on 2018-03-27)

I'm planning the port of our DX based renderer to OpenGL. The target will be Desktop OpenGL 4.x. For DX we use shader reflection but as far as I know OpenGL just has *glGetProgramInterfaceiv* , *glGetUniform* and frieds. Theres also SPIR-V (I have never worked with it).

My main concerns right now are:

* **Using gl\*:** it will be done at runtime, so this will be potentially slow as there are a lot of shaders to load (+ the interface is ugly as fuck). *EDIT: I guess this could be done on a prebuild step too (?).*
* **Use SPIR-V:** means that we need to add another build step and I don't see much doc around.

I would like to hear advice from people with experience with this.
Thanks a lot!

**Tags:** opengl, shader

> **Comments:**
> **username_1**: what do you need reflected? uniform and attrib locations?  
> **username_0**: Yes, I think I'm only looking for names + bindings (also from inside UBOs) also uniform types (UBOs,SSBOs,images etc)  

---

## 1 answers

### Answer by username_1 on 2018-03-28. Score: 1

opengl has several methods that together let you reflect the shader IO.

However it is much simpler if the shader IO is uniform across all your shaders. IOW have a set of default bindings/locations for everything. Then you only need some meta data for each shader where you deviate from those defaults for some reason.

You then only need to query that the binding exists to see whether you need to provide the data. This has the added bonus that if an input gets optimized out you'll not waste time giving data it doesn't need.

> **Comments:**
> **username_0**: For most of our shaders, we use fixed slots but in some cases we need to retrieve uniform bindings (eg. I'm generating UBOs for bindless textures).  

---

