1 |
d333eb6b
|
Leszek Koltunski
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2016 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
7602a827
|
Leszek Koltunski
|
// This file is part of DistortedLibrary. //
|
5 |
d333eb6b
|
Leszek Koltunski
|
// //
|
6 |
7602a827
|
Leszek Koltunski
|
// DistortedLibrary is free software: you can redistribute it and/or modify //
|
7 |
d333eb6b
|
Leszek Koltunski
|
// 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 |
7602a827
|
Leszek Koltunski
|
// DistortedLibrary is distributed in the hope that it will be useful, //
|
12 |
d333eb6b
|
Leszek Koltunski
|
// 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 |
7602a827
|
Leszek Koltunski
|
// along with DistortedLibrary. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
d333eb6b
|
Leszek Koltunski
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
6a06a912
|
Leszek Koltunski
|
|
20 |
341151fc
|
Leszek Koltunski
|
precision highp float;
|
21 |
c1a38ba3
|
Leszek Koltunski
|
precision highp int;
|
22 |
94f6d472
|
Leszek Koltunski
|
|
23 |
f2367b75
|
Leszek Koltunski
|
in vec3 v_Position; // Interpolated position for this fragment.
|
24 |
|
|
in vec3 v_Normal; // Interpolated normal for this fragment.
|
25 |
|
|
in vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
|
26 |
5e331bc8
|
Leszek Koltunski
|
|
27 |
f2367b75
|
Leszek Koltunski
|
out vec4 fragColor; // The output color
|
28 |
94f6d472
|
Leszek Koltunski
|
|
29 |
|
|
uniform sampler2D u_Texture; // The input texture.
|
30 |
6a06a912
|
Leszek Koltunski
|
|
31 |
5e331bc8
|
Leszek Koltunski
|
#ifdef OIT
|
32 |
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
33 |
|
|
// per-pixel linked list. Order Independent Transparency.
|
34 |
|
|
|
35 |
|
|
uniform uvec2 u_Size;
|
36 |
|
|
uniform uint u_numRecords;
|
37 |
|
|
|
38 |
|
|
layout (binding=0, offset=0) uniform atomic_uint u_Counter;
|
39 |
|
|
|
40 |
|
|
layout (std430,binding=1) buffer linkedlist // first (u_Size.x*u_Size.y) uints - head pointers,
|
41 |
|
|
{ // one for each pixel in the Output rectangle.
|
42 |
|
|
uint u_Records[]; //
|
43 |
|
|
}; // Next 3*u_numRecords uints - actual linked list, i.e.
|
44 |
|
|
// triplets of (pointer,depth,rgba).
|
45 |
|
|
#endif
|
46 |
|
|
|
47 |
6a06a912
|
Leszek Koltunski
|
#if NUM_FRAGMENT>0
|
48 |
ad33f883
|
Leszek Koltunski
|
uniform int fNumEffects; // total number of fragment effects
|
49 |
15aa7d94
|
Leszek Koltunski
|
uniform int fName[NUM_FRAGMENT]; // their namess.
|
50 |
b24e4719
|
Leszek Koltunski
|
uniform vec4 fUniforms[3*NUM_FRAGMENT]; // i-th effect is 3 consecutive vec4's: [3*i], [3*i+1], [3*i+2].
|
51 |
|
|
// The first vec4 is the Interpolated values,
|
52 |
|
|
// second vec4: first float - cache, next 3: Center, the third - the Region.
|
53 |
03cb451d
|
Leszek Koltunski
|
#endif // NUM_FRAGMENT>0
|
54 |
6a06a912
|
Leszek Koltunski
|
|
55 |
5e331bc8
|
Leszek Koltunski
|
#ifdef OIT
|
56 |
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
57 |
|
|
// Concurrent insert to a linked list. Tim Harris, 'pragmatic implementation of non-blocking
|
58 |
|
|
// linked-lists', 2001.
|
59 |
|
|
// This arranges fragments by decreasing 'depth', so one would think - from back to front, but
|
60 |
|
|
// in main() below the depth is mapped with S*(1-depth)/2, so it is really front to back.
|
61 |
|
|
|
62 |
|
|
void insert( vec2 ij, uint depth, uint rgba )
|
63 |
|
|
{
|
64 |
|
|
uint ptr = atomicCounterIncrement(u_Counter);
|
65 |
|
|
|
66 |
|
|
if( ptr<u_numRecords )
|
67 |
|
|
{
|
68 |
|
|
ptr = 3u*ptr + u_Size.x*u_Size.y;
|
69 |
|
|
|
70 |
|
|
u_Records[ptr+1u] = depth;
|
71 |
|
|
u_Records[ptr+2u] = rgba;
|
72 |
|
|
|
73 |
|
|
memoryBarrier();
|
74 |
|
|
|
75 |
|
|
uint prev = uint(ij.x) + uint(ij.y) * u_Size.x;
|
76 |
|
|
uint curr = u_Records[prev];
|
77 |
|
|
|
78 |
|
|
while (true)
|
79 |
|
|
{
|
80 |
|
|
if ( curr==0u || depth > u_Records[curr+1u] ) // need to insert here
|
81 |
|
|
{
|
82 |
|
|
u_Records[ptr] = curr; // next of new record is curr
|
83 |
|
|
memoryBarrier();
|
84 |
|
|
uint res = atomicCompSwap( u_Records[prev], curr, ptr );
|
85 |
|
|
|
86 |
|
|
if (res==curr) break; // done!
|
87 |
|
|
else curr = res; // could not insert! retry from same place in list
|
88 |
|
|
}
|
89 |
|
|
else // advance in list
|
90 |
|
|
{
|
91 |
|
|
prev = curr;
|
92 |
|
|
curr = u_Records[prev];
|
93 |
|
|
}
|
94 |
|
|
}
|
95 |
|
|
}
|
96 |
|
|
}
|
97 |
c1a38ba3
|
Leszek Koltunski
|
|
98 |
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
99 |
|
|
|
100 |
|
|
uint convert(vec4 c)
|
101 |
|
|
{
|
102 |
|
|
return ((uint(255.0*c.r))<<24u) + ((uint(255.0*c.g))<<16u) + ((uint(255.0*c.b))<<8u) + uint(255.0*c.a);
|
103 |
|
|
}
|
104 |
|
|
|
105 |
5e331bc8
|
Leszek Koltunski
|
#endif // OIT
|
106 |
|
|
|
107 |
6a06a912
|
Leszek Koltunski
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
108 |
|
|
|
109 |
|
|
void main()
|
110 |
|
|
{
|
111 |
5e331bc8
|
Leszek Koltunski
|
vec4 color = texture(u_Texture,v_TexCoordinate);
|
112 |
4018b3b7
|
Leszek Koltunski
|
|
113 |
|
|
#if NUM_FRAGMENT>0
|
114 |
b24e4719
|
Leszek Koltunski
|
vec3 diff;
|
115 |
7cd24173
|
leszek
|
float degree;
|
116 |
|
|
int effect=0;
|
117 |
03cb451d
|
Leszek Koltunski
|
|
118 |
6a06a912
|
Leszek Koltunski
|
for(int i=0; i<fNumEffects; i++)
|
119 |
|
|
{
|
120 |
b24e4719
|
Leszek Koltunski
|
diff = (v_Position - fUniforms[effect+1].yzw)/fUniforms[effect+2].xyz;
|
121 |
7cd24173
|
leszek
|
degree = max(0.0,1.0-dot(diff,diff));
|
122 |
341c803d
|
Leszek Koltunski
|
|
123 |
7cd24173
|
leszek
|
// ENABLED EFFECTS WILL BE INSERTED HERE
|
124 |
03cb451d
|
Leszek Koltunski
|
|
125 |
b24e4719
|
Leszek Koltunski
|
effect+=3;
|
126 |
6a06a912
|
Leszek Koltunski
|
}
|
127 |
|
|
#endif
|
128 |
ff8ad0a7
|
Leszek Koltunski
|
|
129 |
6544040f
|
Leszek Koltunski
|
float z = v_Normal.z;
|
130 |
|
|
if( z<0.0 ) z = -z;
|
131 |
|
|
|
132 |
|
|
vec4 converted = vec4(color.rgb * (1.0 + 7.0*z) * 0.125, color.a);
|
133 |
001fb991
|
Leszek Koltunski
|
|
134 |
5e331bc8
|
Leszek Koltunski
|
#ifdef OIT
|
135 |
6544040f
|
Leszek Koltunski
|
if( converted.a > 0.97 )
|
136 |
5e331bc8
|
Leszek Koltunski
|
{
|
137 |
001fb991
|
Leszek Koltunski
|
fragColor= converted;
|
138 |
5e331bc8
|
Leszek Koltunski
|
}
|
139 |
|
|
else
|
140 |
|
|
{
|
141 |
001fb991
|
Leszek Koltunski
|
if( converted.a > 0.0 )
|
142 |
5e331bc8
|
Leszek Koltunski
|
{
|
143 |
|
|
const float S= 2147483647.0; // max signed int. Could probably be max unsigned int but this is enough.
|
144 |
001fb991
|
Leszek Koltunski
|
vec2 pixel = vec2(gl_FragCoord.xy);
|
145 |
|
|
insert(pixel, uint(S*(1.0-gl_FragCoord.z)/2.0), convert(converted) );
|
146 |
5e331bc8
|
Leszek Koltunski
|
}
|
147 |
|
|
discard;
|
148 |
|
|
}
|
149 |
|
|
#else
|
150 |
001fb991
|
Leszek Koltunski
|
fragColor = converted;
|
151 |
5e331bc8
|
Leszek Koltunski
|
#endif
|
152 |
341c803d
|
Leszek Koltunski
|
}
|