in/out variables among shaders in a Pipeline Program

爷,独闯天下 提交于 2020-01-14 03:51:13

问题


I am currently using 3 different shaders (Vertex, Geometry and Fragment), each belonging to a different program, all collected in a single Program Pipeline.

The problem is that the Geometry and Fragment have their in varyings zeroed, that is, they do not contain the value previously written by the preceeding shader in the pipeline.

for each shader:

glCreateShader(...)
glShadersource(...)
glCompileShader(...)
glGetShaderiv(*shd,GL_COMPILE_STATUS,&status)

for each program:

program[index] = glCreateProgram()
glAttachShader(program[index],s[...])
glProgramParameteri(program[index],GL_PROGRAM_SEPARABLE,GL_TRUE)
glLinkProgram(program[index])
glGetProgramiv(program[index],GL_LINK_STATUS,&status)

then:

glGenProgramPipelines(1,&pipeline_object)

in gl draw:

glBindProgramPipeline(pipeline_object)
glUseProgramStages(pipeline_object,GL_VERTEX_SHADER_BIT,program[MY_VERTEX_PROGRAM])
and again for the geometry and fragment programs

vertex shader:

#version 330

//modelview and projection mat(s) skipped
...

//interface to geometry shader
out vec3 my_vec;
out float my_float;

void main() {
    my_vec = vec3(1,2,3);
    my_float = 12.3;
    gl_Position = <whatever>
}

geometry shader:

#version 330

//input/output layouts skipped
...

//interface from vertex shader
in vec3 my_vec[];
in float my_float[];
//interface to fragment shader
out vec3 my_vec_fs;
out float my_float_fs;

void main() {
    int i;
    for(i=0;i<3;i++) {
        my_vec_fs = my_vec[i];
        my_float_fs = my_float[i];
        EmitVertex();
    }
    EndPrimitive();
}

fragment shader:

#version 330

//interface from geometry
in vec3 my_vec_fs;
in float my_float_fs;

void main() {
    here my_vec_fs and my_float_fs come all zeroed
}

Am I missing some crucial step in writing/reading varying between different stages in a program pipeline?

UPDATE:

I tried with the layout location qualifier just to be sure everyone was 'talking' on the same vector, since the GLSL specs states:

layout-qualifier-id location = integer-constant Only one argument is accepted. For example, layout(location = 3) in vec4 normal; will establish that the shader input normal is assigned to vector location number 3. For vertex shader inputs, the location specifies the number of the generic vertex attribute from which input values are taken. For inputs of all other shader types, the location specifies a vector number that can be used to match against outputs from a previous shader stage, even if that shader is in a different program object.

but adding

layout(location = 3) out vec3 my_vec;

does not compile

So I tried to do the same via glBindAttribLocation(), I get no errors, but the behaviour is still unchanged

UPDATE 2

If I add "#extension GL_ARB_separate_shader_objects: enable"

then I can use layout(location = n) in/out var; and then it works.

found:

GLSL 330: Vertex shaders cannot have output layout qualifiers
GLSL 420: All shaders allow location output layout qualifiers on output variable declarations

This is interesting.. If you declare #version 330 you shouldnt be able to use a layout out qualifier, even if you enable an extension.. ..but again the extension states:

This ARB extension extends the GLSL language's use of layout qualifiers to provide cross-stage interfacing.

Now Idlike to know why it does not work using glBindAttribLocation() or just with plain name matches + ARB extension enabled!


回答1:


In at least one implementation (webgl on and older chrome I think) I found bugs with glBindAttribLocation() I think the issue was, you had to bind vertex attribs in numerical order. So it proved not useful to use it. I had to switch to getAttribLocation() to get it to work.



来源:https://stackoverflow.com/questions/8939210/in-out-variables-among-shaders-in-a-pipeline-program

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