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 |
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
28 |
|
|
// per-pixel linked list. Order Independent Transparency.
|
29 |
8777ce17
|
Leszek Koltunski
|
|
30 |
344ac0e4
|
Leszek Koltunski
|
uniform uvec2 u_Size;
|
31 |
8777ce17
|
Leszek Koltunski
|
|
32 |
375b3950
|
Leszek Koltunski
|
layout (std430,binding=1) buffer linkedlist // first (u_Size.x*u_Size.y) uints - head pointers,
|
33 |
|
|
{ // one for each pixel in the Output rectangle.
|
34 |
|
|
uint u_Records[]; //
|
35 |
|
|
}; // Next 3*u_numRecords uints - actual linked list, i.e.
|
36 |
|
|
// triplets of (pointer,depth,rgba).
|
37 |
|
|
|
38 |
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
39 |
|
|
|
40 |
|
|
vec4 convert(uint rgba)
|
41 |
8777ce17
|
Leszek Koltunski
|
{
|
42 |
375b3950
|
Leszek Koltunski
|
return vec4( float((rgba>>24u)&255u),float((rgba>>16u)&255u),float((rgba>>8u)&255u),float(rgba&255u) ) / 255.0;
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
46 |
90eb5f53
|
Leszek Koltunski
|
// A over B (https://en.wikipedia.org/wiki/Alpha_compositing)
|
47 |
375b3950
|
Leszek Koltunski
|
|
48 |
90eb5f53
|
Leszek Koltunski
|
|
49 |
|
|
vec4 blend(vec4 A,vec4 B)
|
50 |
375b3950
|
Leszek Koltunski
|
{
|
51 |
90eb5f53
|
Leszek Koltunski
|
float b = B.a * (1.0-A.a);
|
52 |
|
|
float a = A.a + b;
|
53 |
|
|
|
54 |
|
|
return vec4( (A.rgb*A.a + B.rgb*b)/a , a );
|
55 |
375b3950
|
Leszek Koltunski
|
}
|
56 |
8777ce17
|
Leszek Koltunski
|
|
57 |
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
58 |
33f59f22
|
Leszek Koltunski
|
// Pass4 of the OIT algorithm - keep traversing the linked list, build the final color and blend it.
|
59 |
8777ce17
|
Leszek Koltunski
|
|
60 |
|
|
void main()
|
61 |
|
|
{
|
62 |
344ac0e4
|
Leszek Koltunski
|
uint prev = uint(v_Pixel.x) + uint(v_Pixel.y) * u_Size.x;
|
63 |
33f59f22
|
Leszek Koltunski
|
uint curr = u_Records[prev];
|
64 |
375b3950
|
Leszek Koltunski
|
|
65 |
56c6ca24
|
Leszek Koltunski
|
if (curr != 0u)
|
66 |
b36613d8
|
Leszek Koltunski
|
{
|
67 |
56c6ca24
|
Leszek Koltunski
|
const float S= 2147483647.0;
|
68 |
33f59f22
|
Leszek Koltunski
|
gl_FragDepth = 1.0 - 2.0*float(u_Records[curr+1u])/S;
|
69 |
|
|
vec4 color = convert(u_Records[curr+2u]);
|
70 |
2aef1f4d
|
Leszek Koltunski
|
curr = u_Records[curr];
|
71 |
375b3950
|
Leszek Koltunski
|
|
72 |
33f59f22
|
Leszek Koltunski
|
while (curr != 0u)
|
73 |
b36613d8
|
Leszek Koltunski
|
{
|
74 |
33f59f22
|
Leszek Koltunski
|
color = blend( color , convert(u_Records[curr+2u]) );
|
75 |
2aef1f4d
|
Leszek Koltunski
|
curr = u_Records[curr];
|
76 |
b36613d8
|
Leszek Koltunski
|
}
|
77 |
56c6ca24
|
Leszek Koltunski
|
|
78 |
33f59f22
|
Leszek Koltunski
|
fragColor = color;
|
79 |
|
|
}
|
80 |
|
|
else discard;
|
81 |
8777ce17
|
Leszek Koltunski
|
}
|