forked from m-schuetz/compute_rasterizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_loop_las_hqs.h
309 lines (228 loc) · 9.65 KB
/
compute_loop_las_hqs.h
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
#pragma once
#include <string>
#include <queue>
#include <vector>
#include <mutex>
#include <thread>
#include "glm/common.hpp"
#include "glm/matrix.hpp"
#include <glm/gtx/transform.hpp>
#include "nlohmann/json.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include "unsuck.hpp"
#include "ProgressiveFileBuffer.h"
#include "Shader.h"
#include "Box.h"
#include "Debug.h"
#include "Camera.h"
#include "LasLoader.h"
#include "Frustum.h"
#include "Renderer.h"
#include "GLTimerQueries.h"
#include "Method.h"
#include "compute/ComputeLasLoader.h"
using namespace std;
using namespace std::chrono_literals;
using nlohmann::json;
struct ComputeLoopLasHqs : public Method{
struct UniformData{
mat4 world;
mat4 view;
mat4 proj;
mat4 transform;
mat4 transformFrustum;
int pointsPerThread;
int enableFrustumCulling;
int showBoundingBox;
int numPoints;
ivec2 imageSize;
int colorizeChunks;
int colorizeOverdraw;
};
struct DebugData{
uint32_t value = 0;
bool enabled = false;
uint32_t depth_numPointsProcessed = 0;
uint32_t depth_numNodesProcessed = 0;
uint32_t depth_numPointsRendered = 0;
uint32_t depth_numNodesRendered = 0;
uint32_t color_numPointsProcessed = 0;
uint32_t color_numNodesProcessed = 0;
uint32_t color_numPointsRendered = 0;
uint32_t color_numNodesRendered = 0;
uint32_t numPointsVisible = 0;
};
string source = "";
Shader* csDepth = nullptr;
Shader* csColor = nullptr;
Shader* csResolve = nullptr;
GLBuffer ssDepth;
GLBuffer ssRGBA;
GLBuffer ssDebug;
GLBuffer ssBoundingBoxes;
GLBuffer uniformBuffer;
UniformData uniformData;
shared_ptr<ComputeLasData> las = nullptr;
Renderer* renderer = nullptr;
ComputeLoopLasHqs(Renderer* renderer, shared_ptr<ComputeLasData> las){
this->name = "loop_las_hqs";
this->description = R"ER01(
Like compute las, but also
averages overlapping points
)ER01";
this->las = las;
this->group = "10-10-10 bit encoded";
csDepth = new Shader({ {"./modules/compute_loop_las_hqs/depth.cs", GL_COMPUTE_SHADER} });
csColor = new Shader({ {"./modules/compute_loop_las_hqs/color.cs", GL_COMPUTE_SHADER} });
csResolve = new Shader({ {"./modules/compute_loop_las_hqs/resolve.cs", GL_COMPUTE_SHADER} });
ssDepth = renderer->createBuffer(4 * 2048 * 2048);
ssRGBA = renderer->createBuffer(16 * 2048 * 2048);
ssDebug = renderer->createBuffer(256);
ssBoundingBoxes = renderer->createBuffer(48 * 1'000'000);
uniformBuffer = renderer->createUniformBuffer(512);
GLuint zero = 0;
glClearNamedBufferData(ssDebug.handle, GL_R32UI, GL_RED, GL_UNSIGNED_INT, &zero);
glClearNamedBufferData(ssBoundingBoxes.handle, GL_R32UI, GL_RED, GL_UNSIGNED_INT, &zero);
this->renderer = renderer;
}
void update(Renderer* renderer){
if(Runtime::resource != (Resource*)las.get()){
if(Runtime::resource != nullptr){
Runtime::resource->unload(renderer);
}
las->load(renderer);
Runtime::resource = (Resource*)las.get();
}
}
void render(Renderer* renderer) {
GLTimerQueries::timestamp("compute-loop-start");
las->process(renderer);
if(las->numPointsLoaded == 0){
return;
}
auto fbo = renderer->views[0].framebuffer;
auto camera = renderer->camera;
// Update Uniform Buffer
{
mat4 world;
mat4 view = renderer->views[0].view;
mat4 proj = renderer->views[0].proj;
mat4 worldView = view * world;
mat4 worldViewProj = proj * view * world;
uniformData.world = world;
uniformData.view = view;
uniformData.proj = proj;
uniformData.transform = worldViewProj;
if(Debug::updateFrustum){
uniformData.transformFrustum = worldViewProj;
}
uniformData.pointsPerThread = POINTS_PER_THREAD;
uniformData.numPoints = las->numPointsLoaded;
uniformData.enableFrustumCulling = Debug::frustumCullingEnabled ? 1 : 0;
uniformData.showBoundingBox = Debug::showBoundingBox ? 1 : 0;
uniformData.imageSize = {fbo->width, fbo->height};
uniformData.colorizeChunks = Debug::colorizeChunks;
uniformData.colorizeOverdraw = Debug::colorizeOverdraw;
glNamedBufferSubData(uniformBuffer.handle, 0, sizeof(UniformData), &uniformData);
}
if(Debug::enableShaderDebugValue){
DebugData data;
data.enabled = true;
glNamedBufferSubData(ssDebug.handle, 0, sizeof(DebugData), &data);
}
// DEPTH
if(csDepth->program != -1){
GLTimerQueries::timestamp("depth-start");
glUseProgram(csDepth->program);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, ssDepth.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, ssRGBA.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 30, ssDebug.handle);
glBindBufferBase(GL_UNIFORM_BUFFER, 31, uniformBuffer.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 40, las->ssBatches.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 41, las->ssXyz_12b.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 42, las->ssXyz_8b.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 43, las->ssXyz_4b.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 44, las->ssColors.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 50, ssBoundingBoxes.handle);
glBindImageTexture(0, fbo->colorAttachments[0]->handle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8UI);
int numBatches = ceil(double(las->numPointsLoaded) / double(POINTS_PER_WORKGROUP));
glDispatchCompute(numBatches, 1, 1);
GLTimerQueries::timestamp("depth-end");
}
// COLORS
if(csColor->program != -1){
GLTimerQueries::timestamp("color-start");
glUseProgram(csColor->program);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, ssDepth.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, ssRGBA.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 30, ssDebug.handle);
glBindBufferBase(GL_UNIFORM_BUFFER, 31, uniformBuffer.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 40, las->ssBatches.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 41, las->ssXyz_12b.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 42, las->ssXyz_8b.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 43, las->ssXyz_4b.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 44, las->ssColors.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 50, ssBoundingBoxes.handle);
glBindImageTexture(0, fbo->colorAttachments[0]->handle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8UI);
int numBatches = ceil(double(las->numPointsLoaded) / double(POINTS_PER_WORKGROUP));
glDispatchCompute(numBatches, 1, 1);
GLTimerQueries::timestamp("color-end");
}
// RESOLVE
if(csResolve->program != -1){
GLTimerQueries::timestamp("resolve-start");
glUseProgram(csResolve->program);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, ssDepth.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, ssRGBA.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 30, ssDebug.handle);
glBindBufferBase(GL_UNIFORM_BUFFER, 31, uniformBuffer.handle);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 44, las->ssColors.handle);
glBindImageTexture(0, fbo->colorAttachments[0]->handle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8UI);
int groups_x = ceil(float(fbo->width) / 16.0f);
int groups_y = ceil(float(fbo->height) / 16.0f);
glDispatchCompute(groups_x, groups_y, 1);
glMemoryBarrier(GL_ALL_BARRIER_BITS);
GLTimerQueries::timestamp("resolve-end");
}
// READ DEBUG VALUES
if(Debug::enableShaderDebugValue){
glMemoryBarrier(GL_ALL_BARRIER_BITS);
DebugData data;
glGetNamedBufferSubData(ssDebug.handle, 0, sizeof(DebugData), &data);
// Debug::getInstance()->values["debug value"] = formatNumber(data.value);
auto dbg = Debug::getInstance();
dbg->pushFrameStat("[depth] #nodes processed" , formatNumber(data.depth_numNodesProcessed));
dbg->pushFrameStat("[depth] #nodes rendered" , formatNumber(data.depth_numNodesRendered));
dbg->pushFrameStat("[depth] #points processed", formatNumber(data.depth_numPointsProcessed));
dbg->pushFrameStat("[depth] #points rendered" , formatNumber(data.depth_numPointsRendered));
dbg->pushFrameStat("divider" , "");
dbg->pushFrameStat("[color] #nodes processed" , formatNumber(data.color_numNodesProcessed));
dbg->pushFrameStat("[color] #nodes rendered" , formatNumber(data.color_numNodesRendered));
dbg->pushFrameStat("[color] #points processed", formatNumber(data.color_numPointsProcessed));
dbg->pushFrameStat("[color] #points rendered" , formatNumber(data.color_numPointsRendered));
dbg->pushFrameStat("divider" , "");
dbg->pushFrameStat("#points visible" , formatNumber(data.numPointsVisible));
glMemoryBarrier(GL_ALL_BARRIER_BITS);
}
// BOUNDING BOXES
if(Debug::showBoundingBox){
glMemoryBarrier(GL_ALL_BARRIER_BITS);
glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle);
auto camera = renderer->camera;
renderer->drawBoundingBoxes(camera.get(), ssBoundingBoxes);
}
{ // CLEAR
glMemoryBarrier(GL_ALL_BARRIER_BITS);
GLuint zero = 0;
float inf = -Infinity;
GLuint intbits;
memcpy(&intbits, &inf, 4);
glClearNamedBufferSubData(ssDepth.handle, GL_R32UI, 0, fbo->width * fbo->height * 4, GL_RED, GL_UNSIGNED_INT, &intbits);
glClearNamedBufferSubData(ssRGBA.handle, GL_R32UI, 0, fbo->width * fbo->height * 16, GL_RED, GL_UNSIGNED_INT, &zero);
glClearNamedBufferData(ssDebug.handle, GL_R32UI, GL_RED, GL_UNSIGNED_INT, &zero);
glClearNamedBufferSubData(ssBoundingBoxes.handle, GL_R32UI, 0, 48, GL_RED, GL_UNSIGNED_INT, &zero);
glMemoryBarrier(GL_ALL_BARRIER_BITS);
}
GLTimerQueries::timestamp("compute-loop-end");
}
};