1
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2018 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
precision highp float;
|
21
|
precision highp uint;
|
22
|
|
23
|
#if __VERSION__ != 100
|
24
|
out vec4 fragColor; // The output color
|
25
|
in vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
|
26
|
in vec2 v_Pixel; // location of the current fragment, in pixels
|
27
|
#define TEXTURE texture
|
28
|
#define FRAG_COLOR fragColor
|
29
|
#else
|
30
|
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
|
31
|
varying vec2 v_Pixel; // location of the current fragment, in pixels
|
32
|
#define TEXTURE texture2D
|
33
|
#define FRAG_COLOR gl_FragColor
|
34
|
#endif
|
35
|
|
36
|
uniform sampler2D u_DepthTexture;
|
37
|
|
38
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
39
|
// per-pixel linked list. Order Independent Transparency.
|
40
|
|
41
|
uniform ivec2 u_Size;
|
42
|
|
43
|
layout (std430,binding=1) buffer linkedlist // first (u_Size.x*u_Size.y) uints - head pointers,
|
44
|
{ // one for each pixel in the Output rectangle.
|
45
|
uint u_Records[]; //
|
46
|
}; // Next 3*u_numRecords uints - actual linked list, i.e.
|
47
|
// triplets of (pointer,depth,rgba).
|
48
|
|
49
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
vec4 convert(uint rgba)
|
52
|
{
|
53
|
return vec4( float((rgba>>24u)&255u),float((rgba>>16u)&255u),float((rgba>>8u)&255u),float(rgba&255u) ) / 255.0;
|
54
|
}
|
55
|
|
56
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
// https://en.wikipedia.org/wiki/Alpha_compositing (premultiplied)
|
58
|
|
59
|
vec4 blend(vec4 clr,vec4 srf)
|
60
|
{
|
61
|
return clr + (1.0 - clr.a) * vec4(srf.rgb * srf.a , srf.a);
|
62
|
}
|
63
|
|
64
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
65
|
|
66
|
void main()
|
67
|
{
|
68
|
uint index = uint(v_Pixel.x + v_Pixel.y * float(u_Size.x));
|
69
|
|
70
|
if( u_Records[index] == 0u ) discard;
|
71
|
else
|
72
|
{
|
73
|
index = u_Records[index];
|
74
|
FRAG_COLOR = convert(u_Records[index+2u]);
|
75
|
}
|
76
|
|
77
|
u_Records[index] = 0u;
|
78
|
|
79
|
/*
|
80
|
uint index = uint(v_Pixel.x + v_Pixel.y * float(u_Size.x));
|
81
|
uint curr = u_Records[index];
|
82
|
|
83
|
if (curr == 0u) discard;
|
84
|
|
85
|
vec4 color= vec4(0.0,0.0,0.0,0.0);
|
86
|
u_Records[index] = 0u;
|
87
|
|
88
|
while (curr > 0u) // keep walking the linked list
|
89
|
{ // and blending the colors in
|
90
|
curr = u_Records[curr]; //
|
91
|
//color= blend( color, convert(u_Records[curr+2u]) ); //
|
92
|
|
93
|
color = convert(u_Records[curr+2u]);
|
94
|
|
95
|
if( color.a == 0.0 ) color = vec4(0.0,1.0,0.0,1.0);
|
96
|
}
|
97
|
|
98
|
|
99
|
if( curr>0u )
|
100
|
{
|
101
|
curr = u_Records[curr];
|
102
|
color= convert(u_Records[curr+2u]);
|
103
|
}
|
104
|
|
105
|
gl_FragDepth = TEXTURE(u_DepthTexture, v_TexCoordinate).r;
|
106
|
FRAG_COLOR = color;
|
107
|
|
108
|
//else
|
109
|
// FRAG_COLOR = vec4(1.0,0.0,0.0,1.0);
|
110
|
*/
|
111
|
}
|