AnsweredAssumed Answered

False arithmetic results in GLSL shader when adressing integer vector with array subscripting

Question asked by einzuweitesfeld on Apr 13, 2019
Latest reply on Apr 18, 2019 by dorisyan

I have the following Fragment Shader


#version 460


layout(location = 0)   out vec4 Helligkeit;


void main( void ){
  //Declaring "sip" as ivec2 yields the erroneous behaviour ...
  ivec2 sip = ivec2(-1);


  //... declaring it as an int[2] however does not
  //int sip[2];
  //sip[0] = sip[1] = -1;


  //Doing arithmetics on "sip" gives strange results if array subscripting is used with non-constant expressions on vectors
  int jkl  =    sip[gl_PrimitiveID%2];  // using a built-in per-shader variables like gl_PrimitiveID does give the error but uniform variables do as well
  int asdf =  1-sip[gl_PrimitiveID%2];  // defining "asdf" as 1-jkl however does NOT yield the error


  if( 1-jkl  !=  asdf )    // This clause should never be true!
    Helligkeit = vec4(1.0f, 0.0f, 0.0f, 1.0f);
    Helligkeit = vec4(0.0f, 1.0f, 0.0f, 1.0f);


and the result I observe running it is a red drawing on the screen i.e. 1-jkl does not equal asdf despite them computing the same expression semantically. In fact the value of asdf is -2147483648 i.e. the smallest signed 32-bit integer.

I don't think this is correct behaviour and might be a bug in the GLSL compiler.

This behaviour does not occur when "sip" is declared as an array of integers or when jkl and asdf  are adressing "sip" with a constant expression. However, the GLSL specification does explicitly allow non-constant expressions for vector subscripting (GLSL core spec, section 5.5).


I have attached a zip file with a Visual C++ 2015 project demonstrating the error which contains the following relevant files:

  • Quelle1.cpp                 The C++ file that sets all the OpenGL context up
  • Shaders/test.vert         A trivial VS
  • Shaders/test.frag         The FS already shown above


I'm using an RX 480 card on Windows 7 64-bit with the following driver versions:

Radeon Settings Version - 2019.0326.2353.42986
View Release Notes -
Driver Packaging Version -
Provider - Advanced Micro Devices, Inc.
2D Driver Version -
Direct3D® Version -
OpenGL® Version - 25.20.15000.13547
OpenCL™ Version - 10.0.2766.5
AMD Audio Driver Version -
Vulkan™ Driver Version - 2.0.78
Vulkan™ API Version - 1.1.101