Revision 8777ce17
Added by Leszek Koltunski about 6 years ago
src/main/res/raw/blit_depth_fragment_shader.glsl | ||
---|---|---|
1 | 1 |
////////////////////////////////////////////////////////////////////////////////////////////// |
2 |
// Copyright 2016 Leszek Koltunski //
|
|
2 |
// Copyright 2018 Leszek Koltunski //
|
|
3 | 3 |
// // |
4 | 4 |
// This file is part of Distorted. // |
5 | 5 |
// // |
... | ... | |
18 | 18 |
////////////////////////////////////////////////////////////////////////////////////////////// |
19 | 19 |
|
20 | 20 |
precision lowp float; |
21 |
precision highp uint; |
|
21 | 22 |
|
22 | 23 |
#if __VERSION__ != 100 |
23 | 24 |
out vec4 fragColor; // The output color |
24 | 25 |
in vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment. |
26 |
in vec2 v_Pixel; // location of the current fragment, in pixels |
|
25 | 27 |
#define TEXTURE texture |
26 | 28 |
#define FRAG_COLOR fragColor |
27 | 29 |
#else |
28 | 30 |
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment. |
31 |
varying vec2 v_Pixel; // location of the current fragment, in pixels |
|
29 | 32 |
#define TEXTURE texture2D |
30 | 33 |
#define FRAG_COLOR gl_FragColor |
31 | 34 |
#endif |
... | ... | |
33 | 36 |
uniform sampler2D u_Texture; |
34 | 37 |
uniform sampler2D u_DepthTexture; |
35 | 38 |
|
39 |
////////////////////////////////////////////////////////////////////////////////////////////// |
|
40 |
// per-pixel linked list. Order Independent Transparency. |
|
41 |
|
|
42 |
uniform ivec2 u_Size; |
|
43 |
uniform uint u_numRecords; |
|
44 |
|
|
45 |
layout (binding=0, offset=0) uniform atomic_uint u_Counter; // initialize to 0 |
|
46 |
|
|
47 |
layout (std430,binding=1) buffer linkedlist // first (u_Size.x*u_Size.y) uints - head pointers, |
|
48 |
{ // one for each pixel in the Output rectangle. |
|
49 |
uint u_Records[]; // |
|
50 |
}; // Next 3*u_numRecords uints - actual linked list, i.e. |
|
51 |
// triplets of (pointer,depth,rgba). |
|
52 |
|
|
53 |
////////////////////////////////////////////////////////////////////////////////////////////// |
|
54 |
// Concurrent insert to a linked list. Tim Harris, 'pragmatic implementation of non-blocking |
|
55 |
// linked-lists', 2001. |
|
56 |
// This arranges fragments by decreasing 'depth', so one would think - from back to front, but |
|
57 |
// in main() below the depth is mapped with S*(1-depth)/2, so it is really front to back. |
|
58 |
|
|
59 |
void insert( vec2 ij, uint depth, uint rgba ) |
|
60 |
{ |
|
61 |
uint ptr = atomicCounterIncrement(u_Counter)-1u; |
|
62 |
|
|
63 |
if( ptr<u_numRecords ) |
|
64 |
{ |
|
65 |
ptr = 3u*ptr + uint(u_Size.x*u_Size.y); |
|
66 |
|
|
67 |
u_Records[ptr+1u] = depth; |
|
68 |
u_Records[ptr+2u] = rgba; |
|
69 |
|
|
70 |
memoryBarrier(); |
|
71 |
|
|
72 |
uint prev = uint(ij.x + ij.y * float(u_Size.x)); |
|
73 |
uint curr = u_Records[prev]; |
|
74 |
/* |
|
75 |
while (true) |
|
76 |
{ |
|
77 |
if ( curr==0u || depth > u_Records[curr+1u] ) // need to insert here |
|
78 |
{ |
|
79 |
*/ |
|
80 |
u_Records[ptr] = curr; // next of new record is curr |
|
81 |
memoryBarrier(); |
|
82 |
uint res = atomicCompSwap( u_Records[prev], curr, ptr ); |
|
83 |
/* |
|
84 |
if (res==curr) break; // done! |
|
85 |
else curr = res; // could not insert! retry from same place in list |
|
86 |
} |
|
87 |
else // advance in list |
|
88 |
{ |
|
89 |
prev = curr; |
|
90 |
curr = u_Records[prev]; |
|
91 |
} |
|
92 |
} |
|
93 |
*/ |
|
94 |
} |
|
95 |
} |
|
96 |
|
|
97 |
////////////////////////////////////////////////////////////////////////////////////////////// |
|
98 |
|
|
99 |
uint convert(vec4 c) |
|
100 |
{ |
|
101 |
return ((uint(255.0*c.r))<<24u) + ((uint(255.0*c.g))<<16u) + ((uint(255.0*c.b))<<8u) + uint(255.0*c.a); |
|
102 |
} |
|
103 |
|
|
36 | 104 |
////////////////////////////////////////////////////////////////////////////////////////////// |
37 | 105 |
|
38 | 106 |
void main() |
39 | 107 |
{ |
40 |
gl_FragDepth = TEXTURE(u_DepthTexture,v_TexCoordinate).r; |
|
41 |
FRAG_COLOR = TEXTURE(u_Texture ,v_TexCoordinate); |
|
108 |
vec4 frag = TEXTURE(u_Texture , v_TexCoordinate); |
|
109 |
float depth= TEXTURE(u_DepthTexture, v_TexCoordinate).r; |
|
110 |
|
|
111 |
if( frag.a > 0.95 ) |
|
112 |
{ |
|
113 |
gl_FragDepth = depth; |
|
114 |
FRAG_COLOR = frag; |
|
115 |
} |
|
116 |
else if( frag.a > 0.0 ) |
|
117 |
{ |
|
118 |
const float S= 2147483647.0; // max signed int. Could probably be max unsigned int but this is enough. |
|
119 |
insert(v_Pixel, uint(S*(1.0-depth)/2.0), convert(frag) ); |
|
120 |
discard; |
|
121 |
} |
|
42 | 122 |
} |
Also available in: Unified diff
Order Independent Transparency. Does not work yet.