-
Notifications
You must be signed in to change notification settings - Fork 1
/
AccumulateVDI.comp
352 lines (284 loc) · 14.6 KB
/
AccumulateVDI.comp
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
if (vis && step > localNear && step < localFar)
{
transparentSample = false;
vec4 x = sampleVolume(wpos);
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel && thresh_found) {
// debugPrintfEXT("Step: %d, color of sample: (%f, %f, %f, %f) at position: (%f, %f, %f, %f)", i, x.rgba, wpos.xyzw);
// }
// #endif
//TODO: the below check should be removed for efficiency
if(x.r > -0.5 || lastSample) { // we need to process this sample only if it is from a volume that actually exists at this sample point
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel && thresh_found) {
// debugPrintfEXT("Step: %d, color of sample: (%f, %f, %f, %f) at position: (%f, %f, %f, %f)", i, x.rgba, wpos.xyzw);
// }
// #endif
float newAlpha = x.a;
float w = adjustOpacity(newAlpha, length(wpos - wprev)); //TODO: jump length can be precalculated
vec3 newColor = x.rgb;
if(w <= minOpacity) {
transparentSample = true;
}
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel && lastSample) {
// debugPrintfEXT("lastsample is: (%f, %f, %f, %f). Is transparent? %d", x.rgba, transparentSample);
// }
// #endif
if(supersegmentIsOpen) {
// imageStore(OutputSubVDIColor, ivec3(3 * supersegmentNum + 1, gl_GlobalInvocationID.y, gl_GlobalInvocationID.x), vec4(0));
// imageStore(OutputSubVDIColor, ivec3(3 * supersegmentNum + 2, gl_GlobalInvocationID.y, gl_GlobalInvocationID.x), vec4(0));
// imageStore(OutputSubVDIColor, ivec3(3 * supersegmentNum, gl_GlobalInvocationID.y, gl_GlobalInvocationID.x), vec4(0));
// supersegmentNum++;
// float segLen = step - supSegStartPoint;
// float segLen = ndc_step - supSegStartPoint;
// float segLen = length(wprev - supseg_start_w);
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel && (w_prev_non_transp != wprev) && thresh_found) {
// debugPrintfEXT("Supseg_num: %d. w_prev_non_transp: (%f, %f, %f, %f), wprev: (%f, %f, %f, %f).", supersegmentNum, w_prev_non_transp.xyzw, wprev.xyzw);
// }
// #endif
vec4 jump_pos = mix(wfront, wback, nw * steps_in_supseg); //TODO: jump length of single step can be precalculated and scaled to num steps in supseg
float segLen = length(jump_pos - wfront);
supersegmentAdjusted.rgb = curV.rgb / curV.a;
supersegmentAdjusted.a = adjustOpacity(curV.a, 1.0/segLen);
// supersegmentAdjusted.a = curV.a;
// supersegmentAdjusted = x;
vec4 withSample; // the supersegment with the sample accumulated
withSample.rgb = curV.rgb + (1 - curV.a) * newColor * w;
withSample.a = curV.a + (1 - curV.a) * w;
vec4 next_jump_pos = mix(wfront, wback, nw * (steps_in_supseg + 1));
float segLen_withSample = length(next_jump_pos - wfront);
vec4 withSample_adjusted;
withSample_adjusted.rgb = withSample.rgb / withSample.a;
withSample_adjusted.a = adjustOpacity(withSample.a, 1.0/segLen_withSample);
float diff = diffPremultiplied(supersegmentAdjusted, x);
// float diff = diffPremultiplied(supersegmentAdjusted, vec4(newColor, w));
// float diff = diffPremultiplied(supersegmentAdjusted, withSample_adjusted);
bool newSupSeg = false;
if(diff >= newSupSegThresh) {
newSupSeg = true;
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel && thresh_found) {
// debugPrintfEXT("Termination criteria reached for %dth time, color of supseg was: (%f, %f, %f, %f)", num_terminations, supersegmentAdjusted.rgba);
// debugPrintfEXT("Length used for opacity correction: %f and steps in supseg: %d", segLen, steps_in_supseg);
// debugPrintfEXT("Sample value was: (%f, %f, %f, %f) and after adjusting: (%f, %f, %f, %f)", x.rgba, vec4(newColor, w));
//
// float inv = 1.0/segLen;
// float p = pow((1.0 - curV.a), inv);
// debugPrintfEXT("Opacity before adjusting was: %f and inv: %f and p: %f", curV.a, inv, p);
//
// }
// #endif
}
// if(((newSupSeg) || transparentSample) && !(lastSupersegment && supsegs_written)) { //closing a supersegment
if((newSupSeg)) { //closing a supersegment
num_terminations++;
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel) {
// debugPrintfEXT("Closing supseg with steps_trunc_trans: %d", steps_trunc_trans);
// }
// #endif
supersegmentIsOpen = false;
// supSegEndPoint = step;
vec4 jump_pos_trunc = mix(wfront, wback, nw * steps_trunc_trans); //TODO: jump length of single step can be precalculated and scaled to num steps in supseg
float segLen_trunc = length(jump_pos - wfront);
// float segLen_trunc = length(jump_pos_trunc - wfront);
supersegmentAdjusted.rgb = curV.rgb / curV.a; //TODO: this perhaps does not need to be recalculated here
supersegmentAdjusted.a = adjustOpacity(curV.a, 1.0/segLen_trunc);
#if WORLD_ABS
vec3 diff = w_prev_non_transp.xyz - ro_world.xyz;
if(!(abs(rd_world.x) < 0.05)) {
supSegEndPoint = diff.x / (rd_world.x);
} else if (!(abs(rd_world.y) < 0.05)) {
supSegEndPoint = diff.y / (rd_world.y);
} else {
supSegEndPoint = diff.z / (rd_world.z);
}
#else
#if INT_DEPTHS
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel && thresh_found) {
// debugPrintfEXT("Supseg: %d. Recording end point as : %d. ndc at this stage: %f", supersegmentNum, (supSegStartPoint + steps_in_supseg), ndc_step);
// }
// #endif
supSegEndPoint = supSegStartPoint + steps_trunc_trans;
#else
supSegEndPoint = ndc_step;
#endif
#endif
steps_in_supseg = 0;
steps_trunc_trans = 0;
if(thresh_found) {
if(!supsegs_written) {
writeSupersegment(supersegmentNum, supSegStartPoint, supSegEndPoint, supersegmentAdjusted);
#if INT_DEPTHS
float start_step = tnear + nw * supSegStartPoint;
vec4 start_w = mix(wfront, wback, start_step);
float end_step = tnear + nw * supSegEndPoint;
vec4 end_w = mix(wfront, wback, end_step);
#else
vec4 start_w = ipv * vec4(uv, supSegStartPoint, 1);
start_w *= 1. / start_w.w;
vec4 end_w = ipv * vec4(uv, supSegEndPoint, 1);
end_w *= 1. / end_w.w;
#endif
vec4 start_v = ViewMatrices[0] * start_w;
vec4 end_v = ViewMatrices[0] * end_w;
int start_cell = findZInterval_view(start_v.z);
int end_cell = findZInterval_view(end_v.z);
for(int j = start_cell; j <= end_cell; j++) {
grid_cell.z = j;
update_cell_count(grid_cell);
}
}
supersegmentNum++;
}
}
}
// if( (!lastSample) && (!supersegmentIsOpen) && (!transparentSample) ) { //opening a supersegment
if( (!supersegmentIsOpen) && (!transparentSample) ) { //opening a supersegment
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel && thresh_found) {
// debugPrintfEXT("Opening supseg: %d. Sample value was: (%f, %f, %f, %f). Step number: %d", supersegmentNum, x.rgba, i);
// }
// #endif
supersegmentIsOpen = true;
// supSegStartPoint = step;
#if WORLD_ABS
vec3 diff = w_prev_non_transp.xyz - ro_world.xyz;
if(!(abs(rd_world.x) < 0.05)) {
supSegStartPoint = diff.x / (rd_world.x);
} else if (!(abs(rd_world.y) < 0.05)) {
supSegStartPoint = diff.y / (rd_world.y);
} else {
supSegStartPoint = diff.z / (rd_world.z);
}
#else
#if INT_DEPTHS
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel && thresh_found) {
// debugPrintfEXT("Supseg: %d. Recording start point as : %d. ndc at this stage: %f", supersegmentNum, i, ndc_step);
// }
// #endif
supSegStartPoint = i;
#else
vec4 ndcStart = pv * wpos;
ndcStart *= 1. / ndcStart.w;
float start_step = ndcStart.z;
supSegStartPoint = start_step;
#endif
#endif
supseg_start_w = wpos;
curV = vec4( 0 ); //TODO: should this be x instead?
}
// if(!transparentSample) {
if(supersegmentIsOpen) {
// float w = newAlpha;
curV.rgb = curV.rgb + (1 - curV.a) * newColor * w;
curV.a = curV.a + (1 - curV.a) * w;
if(supsegs_written) {
vec4 supseg_color = imageLoad(OutputSubVDIColor, ivec3(supersegmentNum, gl_GlobalInvocationID.y, gl_GlobalInvocationID.x));
float diff = diffPremultiplied(supseg_color, x);
quantization_error += diff;
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel) {
// debugPrintfEXT("Step: %d. Supseg: (%f, %f, %f, %f), sample: (%f, %f, %f, %f), diff: %f", i, supseg_color.rgba, x.rgba, diff);
// }
// #endif
}
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel && thresh_found) {
// debugPrintfEXT("Step: %d. Supseg: %d color so far: (%f, %f, %f, %f)", i, supersegmentNum, curV.rgba);
// }
// #endif
steps_in_supseg++;
if(!transparentSample) {
steps_trunc_trans = steps_in_supseg;
w_prev_non_transp = wpos;
float step_next = step + nw;
vec4 wnext = mix(wfront, wback, step_next);
ndcPos = pv * wnext;
ndcPos *= 1. / ndcPos.w;
ndc_step = ndcPos.z;
}
}
v.rgb = v.rgb + (1.0f - v.a) * newColor * w; //TODO Remove
v.a = v.a + (1.0f - v.a) * w;
if(lastSample && supersegmentIsOpen) { //close the supersegment after the last sample is accumulated
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel) {
// debugPrintfEXT("Closing supseg (last sample) with steps_trunc_trans: %d", steps_trunc_trans);
// }
// #endif
vec4 jump_pos = mix(wfront, wback, nw * steps_trunc_trans);
float segLen = length(jump_pos - wfront);
supersegmentAdjusted.rgb = curV.rgb / curV.a;
supersegmentAdjusted.a = adjustOpacity(curV.a, 1.0/segLen);
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel && thresh_found) {
// debugPrintfEXT("jump_pos: (%f, %f, %f, %f), wfront: (%f, %f, %f, %f), seglen: %f, supersegmentAdjusted: (%f, %f, %f, %f). curV was: (%f, %f, %f, %f)", jump_pos.xyzw, wfront.xyzw, segLen, supersegmentAdjusted.rgba, curV.rgba);
// }
// #endif
num_terminations++;
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel) {
// debugPrintfEXT("Closing supseg");
// }
// #endif
supersegmentIsOpen = false;
// supSegEndPoint = step;
#if WORLD_ABS
vec3 diff = w_prev_non_transp.xyz - ro_world.xyz;
if(!(abs(rd_world.x) < 0.05)) {
supSegEndPoint = diff.x / (rd_world.x);
} else if (!(abs(rd_world.y) < 0.05)) {
supSegEndPoint = diff.y / (rd_world.y);
} else {
supSegEndPoint = diff.z / (rd_world.z);
}
#else
#if INT_DEPTHS
supSegEndPoint = supSegStartPoint + steps_trunc_trans;
#else
supSegEndPoint = ndc_step;
#endif
#endif
steps_in_supseg = 0;
if(thresh_found) {
if(!supsegs_written) {
writeSupersegment(supersegmentNum, supSegStartPoint, supSegEndPoint, supersegmentAdjusted);
#if INT_DEPTHS
float start_step = tnear + nw * supSegStartPoint;
vec4 start_w = mix(wfront, wback, start_step);
float end_step = tnear + nw * supSegEndPoint;
vec4 end_w = mix(wfront, wback, end_step);
#else
vec4 start_w = ipv * vec4(uv, supSegStartPoint, 1);
start_w *= 1. / start_w.w;
vec4 end_w = ipv * vec4(uv, supSegEndPoint, 1);
end_w *= 1. / end_w.w;
#endif
vec4 start_v = ViewMatrices[0] * start_w;
vec4 end_v = ViewMatrices[0] * end_w;
int start_cell = findZInterval_view(start_v.z);
int end_cell = findZInterval_view(end_v.z);
for(int j = start_cell; j <= end_cell; j++) {
grid_cell.z = j;
update_cell_count(grid_cell);
}
}
supersegmentNum++;
}
}
// #if USE_PRINTF
// if(gl_GlobalInvocationID.xy == debug_pixel && thresh_found) {
// debugPrintfEXT("Accumulated so far: within supseg: (%f, %f, %f, %f) overall: (%f, %f, %f, %f)", curV.rgba, v.rgba);
// }
// #endif
// }
// if(v.a >= 1.0f) {
// break;
// }
}
}