Repository navigation
Expand file tree
/
Copy pathrender.cs
More file actions
545 lines (417 loc) · 14.1 KB
/
Copy pathrender.cs
File metadata and controls
545 lines (417 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#version 450
// Frustum culling code adapted from three.js
// see https://github.com/mrdoob/three.js/blob/c7d06c02e302ab9c20fe8b33eade4b61c6712654/src/math/Frustum.js
// Three.js license: MIT
// see https://github.com/mrdoob/three.js/blob/a65c32328f7ac4c602079ca51078e3e4fee3123e/LICENSE
#extension GL_NV_shader_atomic_int64 : enable
#extension GL_NV_gpu_shader5 : enable
#extension GL_KHR_shader_subgroup_arithmetic : require
#extension GL_NV_shader_atomic_float : require
#define STEPS_30BIT 1073741824
#define MASK_30BIT 1073741823
#define STEPS_20BIT 1048576
#define MASK_20BIT 1048575
#define STEPS_10BIT 1024
#define MASK_10BIT 1023
#define Infinity (1.0 / 0.0)
struct Batch{
int state;
float min_x;
float min_y;
float min_z;
float max_x;
float max_y;
float max_z;
int numPoints;
int lod_offset;
int lod_numPoints;
int padding3;
int padding4;
int padding5;
int padding6;
int padding7;
int padding8;
};
struct BoundingBox{
vec4 position; // 16 0
vec4 size; // 16 16
uint color; // 4 32
// size: 48
};
struct Point{
vec3 position;
uint32_t color;
};
layout(local_size_x = 128, local_size_y = 1) in;
layout (std430, binding = 1) buffer abc_0 { uint64_t ssFramebuffer[]; };
layout (std430, binding = 40) buffer abc_2 { Batch ssBatches[]; };
layout (std430, binding = 41) buffer abc_3 { uint32_t ssXyz_12b[]; };
layout (std430, binding = 42) buffer abc_4 { uint32_t ssXyz_8b[]; };
layout (std430, binding = 43) buffer abc_5 { uint32_t ssXyz_4b[]; };
layout (std430, binding = 44) buffer abc_6 { uint32_t ssRGBA[]; };
layout (std430, binding = 30) buffer abc_1 {
uint32_t value;
bool enabled;
uint32_t numPointsProcessed;
uint32_t numNodesProcessed;
uint32_t numPointsRendered;
uint32_t numNodesRendered;
uint32_t numPointsVisible;
uint32_t numLow;
uint32_t numMed;
uint32_t numHig;
} debug;
// layout (std430, binding = 45) buffer data_lod {
// int numPoints;
// uint32_t ssLOD[];
// } lod;
layout (std430, binding = 50) buffer data_bb {
uint count;
uint instanceCount;
uint first;
uint baseInstance;
uint pad0; uint pad1; uint pad2; uint pad3;
uint pad4; uint pad5; uint pad6; uint pad7;
// offset: 48
BoundingBox ssBoxes[];
} boundingBoxes;
layout(std140, binding = 31) uniform UniformData{
mat4 world;
mat4 view;
mat4 proj;
mat4 transform;
mat4 transformFrustum;
int pointsPerThread;
int enableFrustumCulling;
int showBoundingBox;
int numPoints;
ivec2 imageSize;
} uniforms;
uint SPECTRAL[5] = {
0x00ba832b,
0x00a4ddab,
0x00bfffff,
0x0061aefd,
0x001c19d7
};
// void renderLOD(Batch batch){
// int loopSize = int(ceil(float(batch.lod_numPoints) / 128.0));
// for(int i = 0; i < loopSize; i++){
// uint localIndex = i * gl_WorkGroupSize.x + gl_LocalInvocationID.x;
// int lodPointIndex = int(batch.lod_offset + localIndex);
// if(localIndex < batch.lod_numPoints)
// {
// uint32_t encoded = lod.ssLOD[16 + lodPointIndex];
// // set leftmost bit to flag as lod point
// uint32_t pointIndex = lodPointIndex | 0x80000000;
// vec3 wgMin = vec3(batch.min_x, batch.min_y, batch.min_z);
// vec3 wgMax = vec3(batch.max_x, batch.max_y, batch.max_z);
// vec3 boxSize = wgMax - wgMin;
// uint32_t X = (encoded >> 0) & MASK_10BIT;
// uint32_t Y = (encoded >> 10) & MASK_10BIT;
// uint32_t Z = (encoded >> 20) & MASK_10BIT;
// float x = float(X) * (boxSize.x / STEPS_10BIT) + wgMin.x;
// float y = float(Y) * (boxSize.y / STEPS_10BIT) + wgMin.y;
// float z = float(Z) * (boxSize.z / STEPS_10BIT) + wgMin.z;
// vec3 point = vec3(x, y, z);
// // now project to screen
// vec4 pos = vec4(point, 1.0);
// pos = uniforms.transform * pos;
// pos.xyz = pos.xyz / pos.w;
// bool isInsideFrustum = true;
// if(pos.w <= 0.0 || pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0){
// isInsideFrustum = false;
// }
// if(isInsideFrustum){
// vec2 imgPos = (pos.xy * 0.5 + 0.5) * uniforms.imageSize;
// ivec2 pixelCoords = ivec2(imgPos);
// int pixelID = pixelCoords.x + pixelCoords.y * uniforms.imageSize.x;
// uint32_t depth = floatBitsToInt(pos.w);
// uint64_t newPoint = (uint64_t(depth) << 32UL) | pointIndex;
// uint64_t oldPoint = ssFramebuffer[pixelID];
// if(newPoint < oldPoint){
// atomicMin(ssFramebuffer[pixelID], newPoint);
// }
// }
// }
// }
// return;
// }
struct Plane{
vec3 normal;
float constant;
};
float t(int index){
int a = index % 4;
int b = index / 4;
return uniforms.transformFrustum[b][a];
}
float distanceToPoint(vec3 point, Plane plane){
return dot(plane.normal, point) + plane.constant;
}
Plane createPlane(float x, float y, float z, float w){
float nLength = length(vec3(x, y, z));
Plane plane;
plane.normal = vec3(x, y, z) / nLength;
plane.constant = w / nLength;
return plane;
}
Plane[6] frustumPlanes(){
Plane planes[6] = {
createPlane(t( 3) - t(0), t( 7) - t(4), t(11) - t( 8), t(15) - t(12)),
createPlane(t( 3) + t(0), t( 7) + t(4), t(11) + t( 8), t(15) + t(12)),
createPlane(t( 3) + t(1), t( 7) + t(5), t(11) + t( 9), t(15) + t(13)),
createPlane(t( 3) - t(1), t( 7) - t(5), t(11) - t( 9), t(15) - t(13)),
createPlane(t( 3) - t(2), t( 7) - t(6), t(11) - t(10), t(15) - t(14)),
createPlane(t( 3) + t(2), t( 7) + t(6), t(11) + t(10), t(15) + t(14)),
};
return planes;
}
bool intersectsFrustum(vec3 wgMin, vec3 wgMax){
Plane[] planes = frustumPlanes();
for(int i = 0; i < 6; i++){
Plane plane = planes[i];
vec3 vector;
vector.x = plane.normal.x > 0.0 ? wgMax.x : wgMin.x;
vector.y = plane.normal.y > 0.0 ? wgMax.y : wgMin.y;
vector.z = plane.normal.z > 0.0 ? wgMax.z : wgMin.z;
float d = distanceToPoint(vector, plane);
if(d < 0){
return false;
}
}
return true;
}
int getPrecisionLevel(vec3 wgMin, vec3 wgMax){
vec3 wgCenter = (wgMin + wgMax) / 2.0;
float wgRadius = distance(wgMin, wgMax);
vec4 viewCenter = uniforms.view * uniforms.world * vec4(wgCenter, 1.0);
vec4 viewEdge = viewCenter + vec4(wgRadius, 0.0, 0.0, 0.0);
vec4 projCenter = uniforms.proj * viewCenter;
vec4 projEdge = uniforms.proj * viewEdge;
projCenter.xy = projCenter.xy / projCenter.w;
projEdge.xy = projEdge.xy / projEdge.w;
vec2 screenCenter = uniforms.imageSize.xy * (projCenter.xy + 1.0) / 2.0;
vec2 screenEdge = uniforms.imageSize.xy * (projEdge.xy + 1.0) / 2.0;
float pixelSize = distance(screenEdge, screenCenter);
int level = 0;
if(pixelSize < 100){
level = 4;
}else if(pixelSize < 200){
level = 3;
}else if(pixelSize < 500){
level = 2;
}else if(pixelSize < 10000){
level = 1;
}else{
level = 0;
}
// if(pixelSize < 32){
// level = 10;
// }
return level;
}
void main(){
uint batchIndex = gl_WorkGroupID.x;
uint numPointsPerBatch = uniforms.pointsPerThread * gl_WorkGroupSize.x;
uint wgFirstPoint = batchIndex * numPointsPerBatch;
Batch batch = ssBatches[batchIndex];
if(debug.enabled && gl_LocalInvocationID.x == 0){
atomicAdd(debug.numNodesProcessed, 1);
}
vec3 wgMin = vec3(batch.min_x, batch.min_y, batch.min_z);
vec3 wgMax = vec3(batch.max_x, batch.max_y, batch.max_z);
vec3 boxSize = wgMax - wgMin;
// FRUSTUM CULLING
if((uniforms.enableFrustumCulling != 0) && !intersectsFrustum(wgMin, wgMax)){
return;
}
int level = getPrecisionLevel(wgMin, wgMax);
// if(level != 10){
// return;
// }
// if(((batchIndex) % 2) == 0){
// return;
// }
// if(((batchIndex) % 3) == 0){
// return;
// }
// if(((batchIndex) % 5) == 0){
// return;
// }
// if(((batchIndex) % 7) == 0){
// return;
// }
// if(((batchIndex) % 11) == 0){
// return;
// }
// level = 2;
uint32_t boxColor = 0x000000FF;
// { // paper figure: bit resolution
// // RETZ
// // float upper = 1350.0;
// // MORRO BAY
// float lower = 3450.0;
// float upper = 3900.0;
// // if(length(boxSize) < 300)
// if(length(boxSize) < lower || length(boxSize) > upper)
// // if(length(boxSize) < upper)
// // if(length(boxSize) < 300 || length(boxSize) > 350)
// {
// // return;
// }
// // if(length(boxSize) < upper)
// if(length(boxSize) < lower || length(boxSize) > upper)
// {
// boxColor = 0x0000ffff;
// }else{
// boxColor = 0x000000FF;
// }
// }
if(debug.enabled && gl_LocalInvocationID.x == 0){
if(level == 0){
atomicAdd(debug.numHig, 1);
}else if(level == 1){
atomicAdd(debug.numMed, 1);
}else{
atomicAdd(debug.numLow, 1);
}
}
// POPULATE BOUNDING BOX BUFFER, if enabled
if((uniforms.showBoundingBox != 0) && gl_LocalInvocationID.x == 0){
uint boxIndex = atomicAdd(boundingBoxes.instanceCount, 1);
boundingBoxes.count = 24;
boundingBoxes.first = 0;
boundingBoxes.baseInstance = 0;
vec3 wgPos = (wgMin + wgMax) / 2.0;
vec3 wgSize = wgMax - wgMin;
uint color = 0x0000FF00;
if(level > 0){
color = 0x000000FF;
}
color = SPECTRAL[level];
color = boxColor;
BoundingBox box;
box.position = vec4(wgPos, 0.0);
box.size = vec4(wgSize, 0.0);
box.color = color;
boundingBoxes.ssBoxes[boxIndex] = box;
// debug.numPointsRendered = 100000;
}
if(debug.enabled && gl_LocalInvocationID.x == 0){
atomicAdd(debug.numNodesRendered, 1);
}
int loopSize = uniforms.pointsPerThread;
#define RENDER_STRIDE_128
// #define RENDER_4_CONSECUTIVE
#if defined(RENDER_STRIDE_128)
for(int i = 0; i < loopSize; i++){
uint localIndex = i * gl_WorkGroupSize.x + gl_LocalInvocationID.x;
uint index = wgFirstPoint + localIndex;
#elif defined(RENDER_4_CONSECUTIVE)
uint rollSize = 4;
uint jump = 0;
for(int i = 0; i < loopSize; i++){
if(i > 0 && (i % rollSize) == 0){
jump++;
}
uint localIndex = jump * rollSize * gl_WorkGroupSize.x + rollSize * gl_LocalInvocationID.x + i % rollSize;
uint index = wgFirstPoint + localIndex;
#endif
if(index > uniforms.numPoints){
return;
}
// if(index > 25 * 1000 * 1000){
// return;
// }
if(debug.enabled){
atomicAdd(debug.numPointsProcessed, 1);
}
vec3 point;
// - to reduce memory bandwidth, we load lower precision coordinates for smaller screen-size bounding boxes
// - coordinates are stored in up to three 4 byte values
// - A single 4 byte value holds 3 x 10 bit integer components (x, y, z)
// - if more precision is required, we can load another 4 byte with additional 3 x 10 bit precision
// - 10 bits grants us 1024 individual values => we can present 1 meter in millimeter precision
// 20 bits: 1km in mm precsision
// 30 bits: 1000km in mm precision (but not really because we are loosing precision due to float conversions)
if(level == 0){
// 12 byte ( 30 bit per axis)
uint32_t b4 = ssXyz_4b[index];
uint32_t b8 = ssXyz_8b[index];
uint32_t b12 = ssXyz_12b[index];
uint32_t X_4 = (b4 >> 0) & MASK_10BIT;
uint32_t Y_4 = (b4 >> 10) & MASK_10BIT;
uint32_t Z_4 = (b4 >> 20) & MASK_10BIT;
uint32_t X_8 = (b8 >> 0) & MASK_10BIT;
uint32_t Y_8 = (b8 >> 10) & MASK_10BIT;
uint32_t Z_8 = (b8 >> 20) & MASK_10BIT;
uint32_t X_12 = (b12 >> 0) & MASK_10BIT;
uint32_t Y_12 = (b12 >> 10) & MASK_10BIT;
uint32_t Z_12 = (b12 >> 20) & MASK_10BIT;
uint32_t X = (X_4 << 20) | (X_8 << 10) | X_12;
uint32_t Y = (Y_4 << 20) | (Y_8 << 10) | X_12;
uint32_t Z = (Z_4 << 20) | (Z_8 << 10) | X_12;
float x = float(X) * (boxSize.x / STEPS_30BIT) + wgMin.x;
float y = float(Y) * (boxSize.y / STEPS_30BIT) + wgMin.y;
float z = float(Z) * (boxSize.z / STEPS_30BIT) + wgMin.z;
point = vec3(x, y, z);
}else if(level == 1){
// 8 byte (20 bits per axis)
uint32_t b4 = ssXyz_4b[index];
uint32_t b8 = ssXyz_8b[index];
uint32_t X_4 = (b4 >> 0) & MASK_10BIT;
uint32_t Y_4 = (b4 >> 10) & MASK_10BIT;
uint32_t Z_4 = (b4 >> 20) & MASK_10BIT;
uint32_t X_8 = (b8 >> 0) & MASK_10BIT;
uint32_t Y_8 = (b8 >> 10) & MASK_10BIT;
uint32_t Z_8 = (b8 >> 20) & MASK_10BIT;
uint32_t X = (X_4 << 20) | (X_8 << 10);
uint32_t Y = (Y_4 << 20) | (Y_8 << 10);
uint32_t Z = (Z_4 << 20) | (Z_8 << 10);
float x = float(X) * (boxSize.x / STEPS_30BIT) + wgMin.x;
float y = float(Y) * (boxSize.y / STEPS_30BIT) + wgMin.y;
float z = float(Z) * (boxSize.z / STEPS_30BIT) + wgMin.z;
point = vec3(x, y, z);
}else{
// 4 byte (10 bits per axis)
uint32_t encoded = ssXyz_4b[index];
uint32_t X = (encoded >> 0) & MASK_10BIT;
uint32_t Y = (encoded >> 10) & MASK_10BIT;
uint32_t Z = (encoded >> 20) & MASK_10BIT;
float x = float(X) * (boxSize.x / STEPS_10BIT) + wgMin.x;
float y = float(Y) * (boxSize.y / STEPS_10BIT) + wgMin.y;
float z = float(Z) * (boxSize.z / STEPS_10BIT) + wgMin.z;
// int bits = 10 - 8;
// X = (X >> bits) << bits;
// Y = (Y >> bits) << bits;
// Z = (Z >> bits) << bits;
// x = float(X) * (boxSize.x / STEPS_10BIT) + wgMin.x;
// y = float(Y) * (boxSize.y / STEPS_10BIT) + wgMin.y;
// z = float(Z) * (boxSize.z / STEPS_10BIT) + wgMin.z;
point = vec3(x, y, z);
}
// now project to screen
vec4 pos = vec4(point, 1.0);
pos = uniforms.transform * pos;
pos.xyz = pos.xyz / pos.w;
bool isInsideFrustum = !(pos.w <= 0.0 || pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0);
if(isInsideFrustum){
vec2 imgPos = (pos.xy * 0.5 + 0.5) * uniforms.imageSize;
ivec2 pixelCoords = ivec2(imgPos);
int pixelID = pixelCoords.x + pixelCoords.y * uniforms.imageSize.x;
// index = (batchIndex * batchIndex) % 20011;
// index = index * 1234;
uint32_t depth = floatBitsToInt(pos.w);
uint64_t newPoint = (uint64_t(depth) << 32UL) | uint64_t(index);
uint64_t oldPoint = ssFramebuffer[pixelID];
if(newPoint < oldPoint){
atomicMin(ssFramebuffer[pixelID], newPoint);
if(debug.enabled){
atomicAdd(debug.numPointsRendered, 1);
}
}
}
}
// debug.numNodesRendered = int(boxSize.z * 10.0);
}