I have a 2D texture on 2D plane. When I rotate this plane in orthographic projection everything is fine but when I rotate it in prespective projection then there is lines of white noise on the plane and it depends on angle of rotation, but if there is no rotation it just start flickering with all black depending on translation. I It hepends only with textures that are binded in the texture slot > 0, so texture that is binded in 0 slot (glBindTextureUnit) behave fine in both projection!!!.
Left is orthograpic projection, right is perspective projection
That still doesn’t answer the question. If you don’t understand the question, just show the entire fragment shader, including the variable declarations.
Given that you’re explicitly converting tex_index to an int, I’m guessing it isn’t an int. If it’s written by the vertex shader, it at least needs the flat qualifier. And in OpenGL 3.x, that isn’t enough.
For GLSL 3:
For GLSL 4:
For GLSL 3, tex_index must be a uniform variable or an expression involving only uniform variables. For GLSL 4 the restrictions are relaxed somewhat, but not entirely; you still can’t index arrays of samplers with arbitrary expressions. It’s not actually specified that flat-qualified fragment shader inputs are dynamically uniform, but I’ve yet to encounter an implementation where they aren’t.
For GLSL 3, this isn’t valid. For GLSL 4, it may or may not be. It would be safer to convert the value to an int in the vertex shader, as fragment shader inputs which are integers (or vectors of integers) are implicitly flat-qualified.
Man, you are a genius, thank you very much, I couldn’t find this on the Internet for two days.
Before, I cast float (tex_index) to int in fragment shader as you can see by posted code above.
And now I did what you said. cast float to int in vertex shader with flat prefix and it works perfectly.
Thank you very much.
In other words, an expression being constant for all fragments within a triangle (e.g. a flat-qualified fragment shader input) doesn’t necessarily make it “dynamically uniform”. It may or may not be, depending upon the implementation. And if it isn’t, then it isn’t valid as an index into an array of samplers.
In practice, the expression u_Textures[v_TextureIndex] is only evaluated once for each “wavefront” (concurrently-executed group of invocations). If the invocations process fragments from different triangles, v_TextureIndex can have different values and one value will be used for all fragments.
Using conditionals avoids this issue, but creates another: implicit derivatives are undefined in non-uniform control flow. If any of the textures use mipmaps, the mipmap level may not be calculated correctly. You need to calculate the derivatives or level-of-detail outside the conditional and use textureGrad or textureLod instead of texture.
Where possible, it’s preferable to use array textures (sampler2DArray) for this purpose. This entails some loss of flexibility: the layers of an array texture all have the same size and format, and texture parameters (filters, wrap modes) apply to the array texture as a whole.