Existing uniform not found

Started by Zekutsu, November 26, 2019, 11:01:32

Previous topic - Next topic

Zekutsu

Hi,

I've added a new uniform to a fragment shader (to implement normal mapping), but it just can't find it (the specific uniform is "normalMap").

Here's the code:

https://github.com/CristianCamillo/LWJGL-3-Guide

Why can't it be located?

Thank you in advance.

KaiHH

If you mean this shader, then the reason is that the OpenGL GLSL compiler agressively optimizes/removes unused uniforms. It does via checking whether there is any dependency graph from any output of the fragment shader to a uniform. And this dependency does not exist, because ultimately in line 170, the variable `currNormal` is not being used to produce a fragment output.

Zekutsu

Quote from: KaiHH on November 26, 2019, 12:09:06
If you mean this shader, then the reason is that the OpenGL GLSL compiler agressively optimizes/removes unused uniforms. It does via checking whether there is any dependency graph from any output of the fragment shader to a uniform. And this dependency does not exist, because ultimately in line 170, the variable `currNormal` is not being used to produce a fragment output.

Ok so, for what I understood, even if the actual uniform (normalMap) is used inside a function but the end result of the latter is not used, all the "stuff" that is used, and only used, there will just be removed?


EDIT

GOD currNormal was not actually used in the row below and that was causing all the problem.

Man THANK YOU SO MUCH, I spent hours trying to fix it looking through the whole project!

KaiHH

Only instructions that any fragment shader output depends on (transitively) remains in the shader.
That also means that potentially whole functions are removed iff there is no instructions dependency path from a fragment shader output back to a call of this function.
So simply start at a fragment shader output write instructions (such as gl_FragColor or any `out` variable) and walk your way backwards to all instructions / code paths this output write instruction depends on and the transitive closure (dependencies of those dependencies) of this. Every instruction (and also uniform delcaration) that you did not visit via this algorithm will be removed.