8

I'm still very inexperienced with shaders but I'm climbing the learning curve well.

Something I've stumbled upon this morning is an effort to use a same-named uniform in both fragment and vertex shaders.

The uniform can be defined in both shaders, but it can only be accessed exclusively in one or the other shader, but not both. If I try to access the shader in both, the program does not compile.

The last idea I had was they are compiled as separate symbols, but I don't know how to get access to them except by glGetUniformLocation. Calling that twice with the same string returns the same uniform location... not a great help.

The shaders are pretty simple, and everything works perfectly except for the single line work-or-not tests that have no effect on the rendering.

Is there a special approach for using the same-named uniform with same value between the two shaders?

Tom Pace
  • 2,347
  • 1
  • 24
  • 32

1 Answers1

13

SOLVED! The precision qualifier isn't usually a requirement from my experience. I wasn't using it... and because my uniform was an integer.

I read a confirmation that uniforms can be same-named between vertex and fragment shaders here:

http://www.gamedev.net/topic/535321-glsl-the-same-named-uniform-in-vertex-and-fragment-shaders/

So I believed it was my own mistake.

Finally, I tried adding the highp precision qualifer, and that has SOLVED the issue of crashing.

Technically:

Setup 1 (crash)

Vertex shader:
uniform int frameNum;

Fragment shader:
uniform int frameNum;

Setup 2 (crash)

Vertex shader:
uniform highp int frameNum;

Fragment shader:
uniform int frameNum;

Setup 3 (SUCCESS)

Vertex shader:
uniform highp int frameNum;

Fragment shader:
uniform highp int frameNum;

Setup 4 (SUCCESS)

Vertex shader:
uniform int frameNum;

Fragment shader:
uniform highp int frameNum;
Tom Pace
  • 2,347
  • 1
  • 24
  • 32
  • 1
    Speaking of precision qualifiers... you may benefit from knowing that in GLES 2.0, `highp` is an optional precision in fragment shaders. It is guaranteed to work in vertex shaders, but in fragment shaders an implementation is allowed to not support it (the same way vertex texture lookups are optional). ES 3.0 makes it mandatory, but in ES 2.0 you should check to see if a pre-processor definition for `GL_FRAGMENT_PRECISION_HIGH` exists before using `highp` in a fragment shader. As for your justification for not using a precision qualifier, integers support them too. – Andon M. Coleman Mar 24 '14 at 00:16
  • And because of this in fragment shaders you must explicitly specify `float` precision - it doesn't have default `highp` precision like in fragment shaders. Reference: http://www.khronos.org/files/opengles_shading_language.pdf, paragraph 4.5.3. On certain hardware fragment shaders are valid w/o specifying precision but in order to be 100% standard compliant you must specify `float` precision in fragment shaders. `precision mediump float;` will be OK for any GPU. – keaukraine Mar 24 '14 at 07:30
  • @keaukraine that's a surprise... It seemed like float would be a naturally pre-defined precision if unspecified. I noticed you used "fragment shaders" twice in your first sentence. Do you mean fragment shaders must specify float, where vertex shaders have highp? I'm referencing the OpenGL ES Shading Language 1.0 quick reference card as I learn/practice. – Tom Pace Mar 24 '14 at 16:53
  • @AndonM.Coleman I saw the note about highp being optional in the quick reference card for GLSL 1.0. But the integers needing precision qualifiers to enable support in my use case of same-named uniforms was a surprise and counter-intuitive. Thank your for the advice! I will look into GL_FRAGMENT_PRECISION_HIGH – Tom Pace Mar 24 '14 at 16:56
  • @TomPace Yes sorry I incorrectly referenced to fragment shaders - thank you for pointing out to this mistake. Vertex shaders have default `precision highp float` while fragment shaders don't have any default precision for `float`. – keaukraine Mar 25 '14 at 08:11
  • 1
    Helped me in a weird issue with points not being drawn after uniform multiplication to calculate gl_PointSize. In my case, adding `mediump` to the uniform worked. Thank you! – Rodia Sep 21 '19 at 20:52