PDA

View Full Version : [GLSL 130] Sharing variables between several fragments.



laurent7601
10-21-2015, 02:53 AM
Hi!

I want to do a fragment linked list, but my graphic card doesn't seems to support opengl 420. (Anyway the mesa driver doesn't support it yet)
On windows the driver support it but it crash when I want to display something with modern openGL.

So I'm trying to figure out if there's an alternative with older openGL version.

I want to do something like this :



"#version 130 \n"
"uniform sampler2D depthBuffer;\n"
"uniform sampler2D frameBuffer;\n"
"uniform sampler2D texture;\n"
"vec4 backgroundColor = vec4(0, 0, 0, 1);\n"
"uniform vec3 resolution;\n"
"uniform float haveTexture;\n"
"in mat4 projMat;\n"
"vec4 fragmentList[16];\n"
"float depthList[16];\n"
"int nbFrags;\n"
"void main() {\n"
" vec4 texel = texture2D(texture, gl_TexCoord[0].xy);\n"
" vec4 colors[2];\n"
" colors[1] = texel * gl_Color;\n"
" colors[0] = gl_Color;\n"
" bool b = (haveTexture == 1);\n"
" vec4 color = colors[int(b)];\n"
" float z = (gl_FragCoord.w != 1.f) ? (inverse(projMat) * vec4(0, 0, 0, gl_FragCoord.w)).w : gl_FragCoord.z;\n"
/*Check the fragment's position in the list depending on its depth.*/
" int pos = 0;\n"
" while(pos < nbFrags && z >= depthList[pos]) {\n"
" pos++;\n"
" }"
/*Move every fragment with are before the current fragment in the list and insert the current fragment.*/
" int i = nbFrags;\n"
" while (i > pos) {\n"
" depthList[i] = depthList[i-1];\n"
" fragmentList[i] = fragmentList[i-1];\n"
" i--;\n"
" }\n"
" depthList[pos] = z;\n"
" fragmentList[pos] = color;\n"
" vec4 finalColor = backgroundColor;\n"
" nbFrags++;\n"
/*Apply the alpha blending in order from bottom to top.*/
" for (int i = 0; i < nbFrags; i++) {\n"
" finalColor = fragmentList[pos] * fragmentList[pos].a + finalColor * (1 - fragmentList[pos].a);\n"
" }\n"
" gl_FragColor = finalColor;\n"
"}";

But that code doesn't work, because the variables are not shared between fragment's layers.

Alfonse Reinheart
10-21-2015, 07:01 AM
Fragments don't have "layers". I'm guessing the word you're looking for is "invocations (https://www.opengl.org/wiki/Shader#Execution_and_invocations)".

And yes, fragment shaders do not allow the sharing of values among invocations. Indeed, the only shader stages where this is possible are TCS (https://www.opengl.org/wiki/Tessellation_Control_Shader) and compute (https://www.opengl.org/wiki/Compute_Shader).