c++openglglslshaderssao

Is the GLSL version requirement of this SSDO demo self-conflicting?


I tried to run the SSDO demo code from here but met with several problems that confused me even further after I had spent hours searching for solutions. Nevertheless, now I am almost certain that the problems are with the GLSL version.

Note that:


Stage 1

Before any modifications to the code, I got a bunch of error messages:

ERROR: 0:15: Invalid call of undeclared identifier 'texelFetch2D'
ERROR: 0:16: Invalid call of undeclared identifier 'texelFetch2D'
ERROR: 0:17: Invalid call of undeclared identifier 'texelFetch2D'
ERROR: 0:19: Use of undeclared identifier 'pixelPosition'
ERROR: 0:20: Use of undeclared identifier 'lightVector'
ERROR: 0:21: Use of undeclared identifier 'lightVector'
ERROR: 0:21: Use of undeclared identifier 'lightDistance'
ERROR: 0:23: Use of undeclared identifier 'pixelNormal'
ERROR: 0:24: Use of undeclared identifier 'pixelNormal'
ERROR: 0:24: Use of undeclared identifier 'pixelNormal'
ERROR: 0:25: Use of undeclared identifier 'lightDir'
ERROR: 0:25: Use of undeclared identifier 'pixelNormal'
ERROR: 0:27: Use of undeclared identifier 'cosAlpha'
ERROR: 0:27: Use of undeclared identifier 'pixelReflectance'
ERROR: 0:32: Use of undeclared identifier 'diffuseColor'

ERROR: One or more attached shaders not successfully compiled
...

And the messages went on similarly for other shaders.
Correspondingly, the error messages shown above were related to create_direct_light_buffer.frag:


varying vec4 position;
varying vec3 normal;

uniform vec4 lightPosition;
uniform vec4 lightColor;

uniform sampler2D positionTexture;
uniform sampler2D normalTexture;
uniform sampler2D reflectanceTexture;


void main()
{   
    vec4 pixelPosition = texelFetch2D(positionTexture, ivec2(gl_FragCoord.xy), 0);
    vec4 pixelNormal = texelFetch2D(normalTexture, ivec2(gl_FragCoord.xy), 0);
    vec4 pixelReflectance = texelFetch2D(reflectanceTexture, ivec2(gl_FragCoord.xy), 0);
    
    vec3 lightVector = lightPosition.xyz - pixelPosition.xyz;       // vector to light source
    float lightDistance = length(lightVector);         // distance to light source
    vec3 lightDir = lightVector / lightDistance;       // normalized vector to light source

    pixelNormal.w = 0.0;
    pixelNormal = normalize(pixelNormal);
    float cosAlpha = max(0.0, dot(lightDir.xyz, pixelNormal.xyz));
    
    vec4 diffuseColor = vec4(cosAlpha) * pixelReflectance;
    // vec4 ambientColor = vec4(0.2, 0.2, 0.2, 1.0);
    
    // diffuseColor += ambientColor;

    gl_FragColor = diffuseColor;
    gl_FragColor.a = 1.0;
}

Apparently I searched for texelFetch2D, but garnered only very limited information. Yet based on the information, I tentatively assumed that replacing it with texelFetch would help, which requires a GLSL version no less than 1.30, according to this page.


Stage 2

So I added #version 130 at the very beginning of create_direct_light_buffer.frag, and the error messages turned into:

ERROR: 0:1: '' :  version '130' is not supported

ERROR: One or more attached shaders not successfully compiled
...

Then I took @flyx's advice, adding to the main function to specify the version:

glutInitContextVersion(3, 2);
glutInitContextProfile(GLUT_CORE_PROFILE);

I started to get lost.


Stage 3

The error messages turned into:

WARNING: Could not find vertex shader attribute 'PRLGLSL_MultiTexCoord5' to match BindAttributeLocation request.
WARNING: Could not find vertex shader attribute 'PRLGLSL_MultiTexCoord6' to match BindAttributeLocation request.
WARNING: Could not find vertex shader attribute 'PRLGLSL_MultiTexCoord0' to match BindAttributeLocation request.
WARNING: Could not find vertex shader attribute 'PRLGLSL_SecondaryColor' to match BindAttributeLocation request.
WARNING: Could not find vertex shader attribute 'PRLGLSL_MultiTexCoord4' to match BindAttributeLocation request.
WARNING: Could not find vertex shader attribute 'PRLGLSL_MultiTexCoord7' to match BindAttributeLocation request.
WARNING: Could not find vertex shader attribute 'PRLGLSL_FogCoord' to match BindAttributeLocation request.
WARNING: Could not find vertex shader attribute 'PRLGLSL_MultiTexCoord1' to match BindAttributeLocation request.
WARNING: Could not find vertex shader attribute 'PRLGLSL_MultiTexCoord3' to match BindAttributeLocation request.
WARNING: Could not find vertex shader attribute 'PRLGLSL_MultiTexCoord2' to match BindAttributeLocation request.

C
ERROR: 0:2: 'varying' : syntax error: syntax error

ERROR: One or more attached shaders not successfully compiled
...

These warnings did not seem to pertain to the create_direct_light_buffer.frag, and moreover I could not find any questions and solutions regarding them, so I just let them go.
When I tried to figure out the varying error, I found this post, which claimed that varying was replaced by in or out since GLSL version 1.30.

This is the conflict in the title.


My Expectation

  1. Is the demo correct at all?
  2. If it is possible to run the project, what should I do?

Since I am new to OpenGL, if someone could download the source code to try on his/her own computer and tell me how to work it out, I would be extremely grateful.
Else if some highly-experienced experts could give me some general advice on what might be wrong or how to solve this issue, I would be equally thankful.


Solution

    1. Is the demo correct at all?

    No. Let's look at some of the details here:

    stage 1

    Apparently I searched for texelFetch2D, but garnered only very limited information. Yet based on the information, I tentatively assumed that replacing it with texelFetch would help, which requires a GLSL version no less than 1.30, according to this page.

    texelFetch2D does not exist in standard GLSL. It is a function introduced in the GL_EXT_gpu_shader4 extension, which never went directly into core GL functionality. Conceptually, texelFetch is the correct replacement when using GLSL 130 or above.

    Altohugh this shader might compile on some implementations, it is not backed up by any standard, and shouldn't be assumed to work.

    Interestingly, in SSBO.frag, the demo correctly requests that extension, so that code should work on an implementation supporting that extension:

    #version 120
    #extension GL_EXT_gpu_shader4 : enable
    

    Just switching to #version 130 won't work either, because you have to rewrite the whole shader to GLSL 130 syntax, where a lot of details have changed (varyings are replaced by the more general in/out scheme, for example).

    stage 2 and stage 3

    Then I took @flyx's advice, adding to the main function to specify the version:

    glutInitContextVersion(3, 2);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    

    That won't work. In core profile OpenGL, all lthe deprecated legacy functionality is not available, and that tutorial is heavily depending on such deprecated functionality. It would require a major rewrite to make this code work with core profile OpenGL.

    However:

    My environment is Microsoft Visual Studio Community 2019 on Windows 10 pro supported by Parallels Desktop on a 15" MacBook Pro 2018.

    On the Mac side of things, OpenGL is deprecated in general by Apple, and besides that, MacOS only supports legacy GL up to GL 2.1, and core profile GL beginning from GL 3.2. According to this parallels knowledge base entry, the same restrictions are forwarded to the windows guest. MacOS' legacy GL won't support EXT_gpu_shader4 this demos depends on, and higher GL versions whcih would also incorporate the needed functionality will require core profiles, which will render that demo unusable.

    If it is possible to run the project, what should I do?

    So basically: no chance.

    So, what you should do: