3
3
#extension GL_GOOGLE_include_directive : enable
4
4
#extension GL_ARB_separate_shader_objects : enable
5
5
#extension GL_EXT_samplerless_texture_functions : enable
6
+ #extension GL_KHR_memory_scope_semantics : enable
7
+
8
+ // #define MARK_PAGES 1
9
+ // #define HIZ_PAGES 1
10
+ // #define COMPOSE 1
11
+ // #define DEBUG 1
12
+ #define LWC 1
6
13
7
14
#include "virtual_shadow/vsm_common.glsl"
8
15
#include "lighting/tonemapping.glsl"
9
16
#include "scene.glsl"
10
17
#include "common.glsl"
11
18
12
19
#if defined(GL_COMPUTE_SHADER)
13
- layout(local_size_x = 8, local_size_y = 8) in;
20
+ //layout(local_size_x = 8, local_size_y = 8) in;
21
+ layout(local_size_x = 16, local_size_y = 8) in;
14
22
#endif
15
23
16
24
const vec3 debugColors[] = {
@@ -35,17 +43,17 @@ layout(binding = 0, std140) uniform UboScene {
35
43
layout(binding = 1) uniform texture2D gbufDiffuse;
36
44
layout(binding = 2) uniform utexture2D gbufNormal;
37
45
layout(binding = 3) uniform texture2D depth;
38
- #if defined(MARK_PAGES)
39
- layout(binding = 4, r32ui) uniform uimage3D pageTbl;
46
+ #if defined(MARK_PAGES) || defined(HIZ_PAGES)
47
+ layout(binding = 4, r32ui) uniform uimage3D pageTbl;
40
48
#elif defined(COMPOSE) || defined(DEBUG)
41
- layout(binding = 4) uniform utexture3D pageTbl;
49
+ layout(binding = 4) uniform utexture3D pageTbl;
42
50
# if defined(VSM_ATOMIC)
43
- layout(binding = 5) uniform utexture2D pageData;
51
+ layout(binding = 5) uniform utexture2D pageData;
44
52
# else
45
- layout(binding = 5) uniform texture2D pageData;
53
+ layout(binding = 5) uniform texture2D pageData;
46
54
# endif
47
55
#endif
48
- layout(binding = 6, r32ui) uniform uimage3D pageTblDepth;
56
+ layout(binding = 6, r32ui) uniform uimage3D pageTblDepth;
49
57
50
58
#if defined(COMPOSE) || defined(DEBUG)
51
59
layout(binding = 7, std430) readonly buffer Pages { VsmHeader header; uint pageList[]; } vsm;
@@ -62,6 +70,58 @@ ivec2 pixelPos = ivec2(gl_GlobalInvocationID.xy);
62
70
ivec2 pixelPos = ivec2(gl_FragCoord.xy);
63
71
#endif
64
72
73
+ #if defined(GL_COMPUTE_SHADER)
74
+ shared uint pageHiZ[2];
75
+
76
+ uint pack565_16(ivec3 a, uint b) {
77
+ uint x = (a.x & 0x1F) << 0;
78
+ uint y = (a.y & 0x3F) << 5;
79
+ uint z = (a.z & 0x1F) << 11;
80
+ uint w = ( b & 0xFFFF) << 16;
81
+ //uint d = uint(z*0xFFFF) << 16;
82
+ return x | y | z | w;
83
+ }
84
+
85
+ uvec4 unpack565_16(uint p) {
86
+ uvec4 ret;
87
+ ret.x = (p >> 0) & 0x1F;
88
+ ret.y = (p >> 5) & 0x3F;
89
+ ret.z = (p >> 11) & 0x1F;
90
+ ret.w = (p >> 16);
91
+ return ret;
92
+ }
93
+
94
+ void insertHiZ(ivec3 at, float z) {
95
+ if(z>=1)
96
+ return;
97
+ //imageAtomicStore(pageTbl, at, 1u, gl_ScopeDevice, gl_StorageSemanticsNone, gl_SemanticsRelaxed);
98
+ //return;
99
+
100
+ uint t = pack565_16(at,uint(z*0xFFFF));
101
+ uint cur = packUint4x8(uvec4(at,0));
102
+ for(uint i=0; i<pageHiZ.length(); ++i) {
103
+ uint v = atomicMin(pageHiZ[i], cur);
104
+ if(v==0xFFFFFFFF || v==cur)
105
+ return;
106
+ cur = (v > cur) ? v : cur;
107
+ }
108
+ imageAtomicStore(pageTbl, ivec3(at), 1u, gl_ScopeDevice, gl_StorageSemanticsNone, gl_SemanticsRelaxed);
109
+ imageAtomicMin(pageTblDepth, ivec3(at), 1234);
110
+ }
111
+
112
+ void flushHiZ() {
113
+ const uint lane = gl_LocalInvocationIndex;
114
+ if(lane >= pageHiZ.length())
115
+ return;
116
+ const uint v = pageHiZ[lane];
117
+ if(v==0xFFFFFFFF)
118
+ return;
119
+ uvec3 at = unpackUint4x8(v).xyz;
120
+ imageAtomicStore(pageTbl, ivec3(at), 1u, gl_ScopeDevice, gl_StorageSemanticsNone, gl_SemanticsRelaxed);
121
+ imageAtomicMin(pageTblDepth, ivec3(at), 1234);
122
+ }
123
+ #endif
124
+
65
125
float drawInt(in vec2 where, in int n) {
66
126
const float RESOLUTION = 0.5;
67
127
int i=int((where*=RESOLUTION).y);
@@ -90,7 +150,7 @@ uint hash(uvec3 gridPos) {
90
150
return (gridPos.x * 18397) + (gridPos.y * 20483) + (gridPos.z * 29303);
91
151
}
92
152
93
- vec4 worldPosLwc (ivec2 frag, float depth) {
153
+ vec4 worldPos (ivec2 frag, float depth) {
94
154
const vec2 fragCoord = ((frag.xy+0.5)*scene.screenResInv)*2.0 - vec2(1.0);
95
155
const vec4 scr = vec4(fragCoord.x, fragCoord.y, depth, 1.0);
96
156
#if defined(LWC)
@@ -101,7 +161,7 @@ vec4 worldPosLwc(ivec2 frag, float depth) {
101
161
}
102
162
103
163
vec3 shadowPos(float z, ivec2 offset) {
104
- const vec4 wpos = worldPosLwc (pixelPos+offset, z);
164
+ const vec4 wpos = worldPos (pixelPos+offset, z);
105
165
#if defined(LWC)
106
166
vec4 shPos = scene.viewVirtualShadowLwc * wpos;
107
167
#else
@@ -140,9 +200,12 @@ float shadowTest(vec2 page, int mip, in float refZ, bool isATest) {
140
200
141
201
void markPage(ivec2 pageI, int mip, float depth) {
142
202
#if defined(MARK_PAGES)
143
- //imageAtomicAdd(pageTbl, ivec3(pageI, mip), 1u);
144
- imageStore(pageTbl, ivec3(pageI, mip), uvec4(1u));
145
- //imageAtomicMin(pageTblDepth, ivec3(pageI, mip), floatBitsToUint(depth));
203
+ //imageAtomicExchange(pageTbl, ivec3(pageI, mip), 1u);
204
+ //imageStore(pageTbl, ivec3(pageI, mip), uvec4(1u));
205
+
206
+ //NOTE: require vulkan memory-model extension or vulkan 1.2
207
+ //imageAtomicStore(pageTbl, ivec3(pageI, mip), 1u, gl_ScopeDevice, gl_StorageSemanticsNone, gl_SemanticsRelaxed);
208
+ insertHiZ(ivec3(pageI, mip), depth);
146
209
#endif
147
210
}
148
211
@@ -180,7 +243,7 @@ vec3 directLight(vec2 page, int mip, float refZ) {
180
243
}
181
244
#endif
182
245
183
- void main () {
246
+ void realMain () {
184
247
#if defined(DEBUG)
185
248
outColor = vec4(0,0,0, 1);
186
249
#endif
@@ -202,7 +265,7 @@ void main() {
202
265
if(z==1)
203
266
return;
204
267
205
- #if defined(MARK_PAGES)
268
+ #if defined(MARK_PAGES) || defined(HIZ_PAGES)
206
269
{
207
270
const vec3 normal = normalFetch(gbufNormal, pixelPos);
208
271
if(dot(scene.sunDir,normal)<=0)
@@ -226,7 +289,7 @@ void main() {
226
289
return;
227
290
228
291
ivec2 pageI = ivec2((page*0.5+0.5)*VSM_PAGE_TBL_SIZE);
229
- #if defined(MARK_PAGES)
292
+ #if defined(MARK_PAGES) || defined(HIZ_PAGES)
230
293
markPage(pageI, mip, shPos0.z);
231
294
#elif defined(COMPOSE)
232
295
outColor = vec4(directLight(page, mip, shPos0.z), 1);
@@ -246,3 +309,18 @@ void main() {
246
309
outColor = vec4(color.xyz,1);
247
310
#endif
248
311
}
312
+
313
+ void main() {
314
+ #if defined(GL_COMPUTE_SHADER)
315
+ const uint lane = gl_LocalInvocationIndex;
316
+ if(lane < pageHiZ.length())
317
+ pageHiZ[lane] = 0xFFFFFFFF;
318
+ barrier();
319
+ #endif
320
+
321
+ realMain();
322
+ #if defined(GL_COMPUTE_SHADER)
323
+ barrier();
324
+ flushHiZ();
325
+ #endif
326
+ }
0 commit comments