1 |
8777ce17
|
Leszek Koltunski
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
f89a986e
|
Leszek Koltunski
|
precision highp float;
|
21 |
47511918
|
Leszek Koltunski
|
precision highp int;
|
22 |
8777ce17
|
Leszek Koltunski
|
|
23 |
375b3950
|
Leszek Koltunski
|
out vec4 fragColor; // The output color
|
24 |
|
|
in vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
|
25 |
|
|
in vec2 v_Pixel; // location of the current fragment, in pixels
|
26 |
|
|
|
27 |
56c6ca24
|
Leszek Koltunski
|
uniform sampler2D u_Texture;
|
28 |
375b3950
|
Leszek Koltunski
|
uniform sampler2D u_DepthTexture;
|
29 |
|
|
|
30 |
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
31 |
|
|
// per-pixel linked list. Order Independent Transparency.
|
32 |
8777ce17
|
Leszek Koltunski
|
|
33 |
e029600f
|
Leszek Koltunski
|
uniform vec2 u_Size;
|
34 |
8777ce17
|
Leszek Koltunski
|
|
35 |
375b3950
|
Leszek Koltunski
|
layout (std430,binding=1) buffer linkedlist // first (u_Size.x*u_Size.y) uints - head pointers,
|
36 |
|
|
{ // one for each pixel in the Output rectangle.
|
37 |
|
|
uint u_Records[]; //
|
38 |
|
|
}; // Next 3*u_numRecords uints - actual linked list, i.e.
|
39 |
|
|
// triplets of (pointer,depth,rgba).
|
40 |
|
|
|
41 |
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
42 |
|
|
|
43 |
|
|
vec4 convert(uint rgba)
|
44 |
8777ce17
|
Leszek Koltunski
|
{
|
45 |
375b3950
|
Leszek Koltunski
|
return vec4( float((rgba>>24u)&255u),float((rgba>>16u)&255u),float((rgba>>8u)&255u),float(rgba&255u) ) / 255.0;
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
49 |
|
|
// https://en.wikipedia.org/wiki/Alpha_compositing (premultiplied)
|
50 |
|
|
|
51 |
|
|
vec4 blend(vec4 clr,vec4 srf)
|
52 |
|
|
{
|
53 |
|
|
return clr + (1.0 - clr.a) * vec4(srf.rgb * srf.a , srf.a);
|
54 |
|
|
}
|
55 |
8777ce17
|
Leszek Koltunski
|
|
56 |
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
57 |
56c6ca24
|
Leszek Koltunski
|
// Pass3 of the OIT algorithm - traverse the per-pixel LinkedList and build the final color.
|
58 |
8777ce17
|
Leszek Koltunski
|
|
59 |
|
|
void main()
|
60 |
|
|
{
|
61 |
56c6ca24
|
Leszek Koltunski
|
float texdepth = texture(u_DepthTexture, v_TexCoordinate).r;
|
62 |
|
|
vec4 color = texture(u_Texture , v_TexCoordinate);
|
63 |
|
|
uint index = uint(v_Pixel.x + v_Pixel.y * u_Size.x);
|
64 |
|
|
uint curr = u_Records[index];
|
65 |
375b3950
|
Leszek Koltunski
|
|
66 |
56c6ca24
|
Leszek Koltunski
|
if (curr != 0u)
|
67 |
b36613d8
|
Leszek Koltunski
|
{
|
68 |
56c6ca24
|
Leszek Koltunski
|
const float S= 2147483647.0;
|
69 |
|
|
uint depth = u_Records[curr+1u];
|
70 |
|
|
uint texdepthuint = uint(S*(1.0-texdepth)/2.0);
|
71 |
375b3950
|
Leszek Koltunski
|
|
72 |
56c6ca24
|
Leszek Koltunski
|
if( depth >= texdepthuint )
|
73 |
b36613d8
|
Leszek Koltunski
|
{
|
74 |
56c6ca24
|
Leszek Koltunski
|
vec4 clr= convert(u_Records[curr+2u]);
|
75 |
|
|
curr = u_Records[curr];
|
76 |
|
|
|
77 |
|
|
while (curr > 0u)
|
78 |
|
|
{
|
79 |
|
|
depth= u_Records[curr+1u]; // keep walking the linked list
|
80 |
|
|
if( depth < texdepthuint ) break; // until we reach scene depth
|
81 |
|
|
clr= blend( clr, convert(u_Records[curr+2u]) ); // and blending the colors in
|
82 |
|
|
curr = u_Records[curr];
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
color = blend( clr, color);
|
86 |
b36613d8
|
Leszek Koltunski
|
}
|
87 |
|
|
}
|
88 |
56c6ca24
|
Leszek Koltunski
|
|
89 |
|
|
gl_FragDepth = texdepth;
|
90 |
|
|
fragColor = color;
|
91 |
8777ce17
|
Leszek Koltunski
|
}
|