2022-11-30 18:00:47 +00:00
|
|
|
#include "oit.h"
|
|
|
|
|
2022-12-01 14:03:55 +00:00
|
|
|
vec4
|
|
|
|
BlendFrags (vec4 color)
|
2022-11-30 18:00:47 +00:00
|
|
|
{
|
|
|
|
#define MAX_FRAGMENTS 64
|
|
|
|
FragData frags[MAX_FRAGMENTS];
|
|
|
|
int numFrags = 0;
|
2023-01-24 01:09:00 +00:00
|
|
|
ivec3 coord = ivec3(gl_FragCoord.xy, gl_ViewIndex);
|
2022-11-30 18:00:47 +00:00
|
|
|
int index = imageLoad (heads, coord).r;
|
|
|
|
|
|
|
|
//FIXME use a heap and prioritize closer fragments
|
|
|
|
while (index != -1 && numFrags < MAX_FRAGMENTS) {
|
|
|
|
frags[numFrags] = fragments[index];
|
|
|
|
numFrags++;
|
|
|
|
index = fragments[index].next;
|
|
|
|
}
|
|
|
|
//insertion sort
|
|
|
|
for (int i = 1; i < numFrags; i++) {
|
|
|
|
FragData toInsert = frags[i];
|
|
|
|
int j = i;
|
|
|
|
while (j > 0 && toInsert.depth > frags[j - 1].depth) {
|
|
|
|
frags[j] = frags[j - 1];
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
frags[j] = toInsert;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < numFrags; i++) {
|
|
|
|
color = mix (color, frags[i].color,
|
|
|
|
clamp (frags[i].color.a, 0.0f, 1.0f));
|
|
|
|
}
|
2022-12-01 14:03:55 +00:00
|
|
|
return color;
|
2022-11-30 18:00:47 +00:00
|
|
|
}
|