GLSL: shader linking fail (but no log)

感情迁移 提交于 2019-12-24 01:54:14

问题


I'm trying to create a little shader for brightness and contrast of the window (that I've seen here).

I can load the file, and compile the shader successfully. But I fail to link it. My problem is that the log has no output, so I can't see what's wrong with it. How can I check the linking problem? Where can I find informations about linking failing, and check why linking can fail (I'm new to shaders).

I'm using Ubuntu 12.04.

This is the initialization code

if (GLEW_ARB_fragment_shader) {
    // I enter here so I suppose that shader is enabled for
    // my graphics card
    std::cout << "arb shader enabled" << std::endl;
}

// Loading shader
string fragmentShaderSource;
GLint len;
std::ifstream in("/path/to/file.glsl");
fragmentShaderSource = std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
len = fragmentShaderSource->size();

// I've checked the string and file seems to be loaded properly.

// Creating shader
GLint flength;
GLuint fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
GLcharARB** text = new (GLcharARB*);
text[0] = (char*)fragmentShaderSource.c_str();
glShaderSourceARB(fragmentShader, 1, (const GLcharARB**) text, &flength);

// Compile shader
glCompileShaderARB(fragmentShader);
GLint compiled;

glGetObjectParameteriv(ShaderObject, GL_COMPILE_STATUS, &compiled);
if (compiled)
{
    // I enter here so I suppose that compilation is ok.
    std::cout << "shader compiled" << std::endl;
} 

// Attaching to program
GLuint program;
program = glCreateProgram();
glAttachShader(program, fragmentShader);

// Linking
glLinkProgram(program);

// Link check
GLint linked;
glGetProgramivARB(program, GL_LINK_STATUS, &linked);
if (linked) {
    std::cout << "linked" << std::endl;
} else {
    // I enter here so linking is failed
    std::cout << "not linked" << std::endl;
    GLint blen = 0;
    GLsizei slen = 0;
    glGetShaderiv(program, GL_INFO_LOG_LENGTH, &blen);
    // blen is equal to zero so I cannot print the log message
    // because it's absent
    if (blen > 1) {
        GLchar* linking_log = (GLchar*) malloc(blen);
        glGetProgramInfoLog(program, blen, &slen, linking_log);
        glGetInfoLogARB(program, blen, &slen, linking_log);
        std::cout << "compiler_log:\n" << linking_log << std::endl;
        free(linking_log);
    }
}

And this is the glsl code that I load

uniform float Brightness : register(C0);
uniform float Contrast : register(C1);

sampler2D Texture1Sampler : register(S0);

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 pixelColor = tex2D(Texture1Sampler, uv);
    pixelColor.rgb /= pixelColor.a;

    // Apply contrast.
    pixelColor.rgb = ((pixelColor.rgb - 0.5f) * max(Contrast, 0)) + 0.5f;

    // Apply brightness.
    pixelColor.rgb += Brightness;

    // Return final pixel color.
    pixelColor.rgb *= pixelColor.a;
    return pixelColor;
}

EDIT:

I fixed log, when linking fails, I obtain following output:

Fragment info
-------------
0(1) : warning C7557: OpenGL does not allow Cg-style semantics
0(2) : warning C7557: OpenGL does not allow Cg-style semantics
0(4) : warning C7557: OpenGL does not allow Cg-style semantics
0(4) : warning C7554: OpenGL requires sampler variables to be explicitly declared as uniform
0(6) : warning C7506: OpenGL does not define the global type float4
0(6) : warning C7506: OpenGL does not define the global type float2
0(6) : warning C7557: OpenGL does not allow Cg-style semantics
0(6) : warning C7557: OpenGL does not allow Cg-style semantics
0(6) : warning C7527: OpenGL requires main to take no parameters
0(6) : warning C7530: OpenGL requires main to return void
0(9) : warning C7506: OpenGL does not define the global function tex2D
0(13) : warning C7502: OpenGL does not allow type suffix 'f' on constant literals in versions below 120
0(13) : warning C7011: implicit cast from "int" to "float"
0(13) : warning C7502: OpenGL does not allow type suffix 'f' on constant literals in versions below 120

EDIT2:

I've fixed fragment shader

uniform float Brightness;
uniform float Contrast;
uniform vec2 vTextureCoord;
uniform sampler2D Texture1Sampler;

void main() {
    vec4 textureColor = texture2D(Texture1Sampler, vTextureCoord);
    vec3 fragRGB = textureColor.rgb / textureColor.a;
    fragRGB.rgb = ((fragRGB.rgb - 0.5) * max(Contrast, 0.0)) + 0.5;
    fragRGB.rgb += Brightness;
    fragRGB.rgb *= textureColor.a;
    gl_FragColor = vec4(fragRGB, textureColor.a);
}

And I've added a basic vertex shader

attribute vec4 gl_Vertex;

void main(){
  gl_Position = gl_Vertex;
}

I've added them to program. Now all compilation warning disappeared, but linking fails again.

program = glCreateProgram(); 
glAttachShader(program, fragmentShader); // fragment shader compiled. No warnings
glAttachShader(program, vertexShader); // vertex shader compiled. No warnings
glLinkProgram(program);
GLint linked;
glGetProgramivARB(program, GL_LINK_STATUS, &linked); // linked != GL_TRUE. Linking failed.

What I'm yet doing wrong?


回答1:


You are only attaching a fragment shader and not a vertex shader. In fully programmable openGL both are required. Your code should be:

glAttachShader(program, fragmentShader);
glAttachShader(program, vertexShader);

Linking happens after shaders are attached to a program, and since both vertex and fragment shaders are required linking failed.

Moreover you are basically writing Cg in GLSL.

float4 main(float2 uv : TEXCOORD) : COLOR // These Cg semantics are not accepted in GLSL

The point is that GLSL doen't use Cg like semantics and you need to use GLSL special out variables. Check the following psudo-GLSL code.

 in vec3 vertex;
    //vertex shader.
    void main() // write a main and output should be done using special variables
    {
      // output using special variables.
    gl_Position = vertex;
    }


    //fragment shader.
    uniform vec4 color;
    void main() // write a main and output should be done using special variables 
    {
      // output using special variables.
    gl_FragColor = color;
    }

I actually recommend that you pick a GLSL language tutorial like this one.



来源:https://stackoverflow.com/questions/21555186/glsl-shader-linking-fail-but-no-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!