1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
const std::string fragmentShader =
R"(#version 140
#extension GL_ARB_shader_atomic_counters : require
#extension GL_ARB_shading_language_420pack : require
#extension GL_ARB_shader_image_load_store : require
#extension GL_ARB_shader_storage_buffer_object : require
#define MAX_FRAGMENTS 75
struct NodeType {
vec4 color;
float depth;
uint next;
};
layout(binding = 0, offset = 0) uniform atomic_uint nextNodeCounter;
layout(binding = 0, r32ui) coherent uniform uimage2D headPointers;
layout(binding = 0, std430) coherent buffer linkedLists {
NodeType nodes[];
};
uniform uint maxNodes;
uniform float haveTexture;
uniform sampler2D texture;
void main() {
vec4 texel = texture2D(texture, gl_TexCoord[0].xy);
vec4 color = (haveTexture > 0.9) ? gl_Color * texel : gl_Color;
if (color.a > 0) {
uint nodeIdx = atomicCounterIncrement(nextNodeCounter) + 1u;
if (nodeIdx < maxNodes) {
uint prevHead = imageAtomicExchange(headPointers, ivec2(gl_FragCoord.xy), nodeIdx);
nodes[nodeIdx].color = color;
nodes[nodeIdx].depth = gl_FragCoord.z;
nodes[nodeIdx].next = prevHead;
}
}
})";
const std::string fragmentShader2 =
R"(
#version 140
#extension GL_ARB_shader_atomic_counters : require
#extension GL_ARB_shading_language_420pack : require
#extension GL_ARB_shader_image_load_store : require
#extension GL_ARB_shader_storage_buffer_object : require
#define MAX_FRAGMENTS 75
struct NodeType {
vec4 color;
float depth;
uint next;
};
layout(binding = 0, r32ui) coherent uniform uimage2D headPointers;
layout(binding = 0, std430) coherent buffer linkedLists {
NodeType nodes[];
};
void main() {
NodeType frags[MAX_FRAGMENTS];
int count = 0;
uint n = imageLoad(headPointers, ivec2(gl_FragCoord.xy)).r;
while( n != 0u && count < MAX_FRAGMENTS) {
frags[count] = nodes[n];
n = frags[count].next;
imageStore(headPointers, ivec2(gl_FragCoord.xy), uvec4(n, 0, 0, 0));
count++;
}
//merge sort
int i, j1, j2, k;
int a, b, c;
int step = 1;
NodeType leftArray[MAX_FRAGMENTS/2]; //for merge sort
while (step <= count)
{
i = 0;
while (i < count - step)
{
////////////////////////////////////////////////////////////////////////
//merge(step, i, i + step, min(i + step + step, count));
a = i;
b = i + step;
c = (i + step + step) >= count ? count : (i + step + step);
for (k = 0; k < step; k++)
leftArray[k] = frags[a + k];
j1 = 0;
j2 = 0;
for (k = a; k < c; k++)
{
if (b + j1 >= c || (j2 < step && leftArray[j2].depth > frags[b + j1].depth))
frags[k] = leftArray[j2++];
else
frags[k] = frags[b + j1++];
}
////////////////////////////////////////////////////////////////////////
i += 2 * step;
}
step *= 2;
}
vec4 color = vec4(0, 0, 0, 0);
for( int i = 0; i < count; i++ )
{
color = mix( color, frags[i].color, frags[i].color.a);
}
gl_FragColor = color;
})"; |
Partager