-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathrenderer_gl_mesh.cpp
281 lines (225 loc) · 8.52 KB
/
renderer_gl_mesh.cpp
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
/*
* Copyright (c) 2016-2022, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2016-2022 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#include <algorithm>
#include <cassert>
#include "nvmeshlet_builder.hpp"
#include "renderer.hpp"
#include "resources_gl.hpp"
#include "common.h"
namespace meshlettest {
//////////////////////////////////////////////////////////////////////////
class RendererMeshGL : public Renderer
{
public:
class Type : public Renderer::Type
{
bool isAvailable(const nvgl::ContextWindow* contextWindow) const override { return has_GL_NV_mesh_shader != 0; }
[[nodiscard]] const char* name() const override { return "GL mesh nv"; }
[[nodiscard]] Renderer* create() const override
{
auto* renderer = new RendererMeshGL();
return renderer;
}
Resources* resources() override { return ResourcesGL::get(); }
[[nodiscard]] unsigned int priority() const override { return 4; }
};
class TypeVbum : public Renderer::Type
{
bool isAvailable(const nvgl::ContextWindow* contextWindow) const override
{
return has_GL_NV_vertex_buffer_unified_memory && has_GL_NV_uniform_buffer_unified_memory && has_GL_NV_mesh_shader;
}
[[nodiscard]] const char* name() const override { return "GL mesh nv nvbindless"; }
[[nodiscard]] Renderer* create() const override
{
auto* renderer = new RendererMeshGL();
renderer->m_bindless = true;
return renderer;
}
[[nodiscard]] unsigned int priority() const override { return 4; }
Resources* resources() override { return ResourcesGL::get(); }
};
public:
bool init(RenderList* NV_RESTRICT list, Resources* resources, const Config& config) override;
void deinit() override;
void draw(const FrameConfig& global) override;
bool m_bindless = false;
private:
const RenderList* NV_RESTRICT m_list{};
ResourcesGL* NV_RESTRICT m_resources{};
Config m_config;
};
static RendererMeshGL::Type s_uborange;
static RendererMeshGL::TypeVbum s_uborange_vbum;
bool RendererMeshGL::init(RenderList* NV_RESTRICT list, Resources* resources, const Config& config)
{
m_list = list;
m_resources = (ResourcesGL*)resources;
m_config = config;
return true;
}
void RendererMeshGL::deinit() {}
void RendererMeshGL::draw(const FrameConfig& global)
{
ResourcesGL* NV_RESTRICT res = m_resources;
const CadScene* NV_RESTRICT scene = m_list->m_scene;
const CadSceneGL& sceneGL = res->m_scene;
const nvgl::ProfilerGL::Section profile(res->m_profilerGL, "Render");
bool bindless = m_bindless;
size_t vertexSize = scene->getVertexSize();
size_t vertexAttributeSize = scene->getVertexAttributeSize();
// generic state setup
glViewport(0, 0, res->m_framebuffer.renderWidth, res->m_framebuffer.renderHeight);
glBindFramebuffer(GL_FRAMEBUFFER, res->m_framebuffer.fboScene);
glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
if(res->m_cullBackFace)
{
glEnable(GL_CULL_FACE);
}
else
{
glDisable(GL_CULL_FACE);
}
if(res->m_clipping)
{
for(int i = 0; i < NUM_CLIPPING_PLANES; i++)
{
glEnable(GL_CLIP_DISTANCE0 + i);
}
}
glNamedBufferSubData(res->m_common.viewBuffer.buffer, 0, sizeof(SceneData), &global.sceneUbo);
glNamedBufferSubData(res->m_common.statsBuffer.buffer, 0, sizeof(CullStats), &m_list->m_stats);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, SSBO_SCENE_STATS, res->m_common.statsBuffer);
if(bindless)
{
glEnableClientState(GL_UNIFORM_BUFFER_UNIFIED_NV);
glBufferAddressRangeNV(GL_UNIFORM_BUFFER_ADDRESS_NV, UBO_SCENE_VIEW, 0, 0);
glBufferAddressRangeNV(GL_UNIFORM_BUFFER_ADDRESS_NV, UBO_OBJECT, 0, 0);
glBufferAddressRangeNV(GL_UNIFORM_BUFFER_ADDRESS_NV, UBO_GEOMETRY, 0, 0);
}
if(bindless)
{
glBufferAddressRangeNV(GL_UNIFORM_BUFFER_ADDRESS_NV, UBO_SCENE_VIEW, res->m_common.viewBuffer.bufferADDR, sizeof(SceneData));
}
else
{
glBindBufferBase(GL_UNIFORM_BUFFER, UBO_SCENE_VIEW, res->m_common.viewBuffer.buffer);
}
{
int lastMaterial = -1;
int lastGeometry = -1;
int lastMatrix = -1;
int lastChunk = -1;
bool lastTask = false;
int statsGeometry = 0;
int statsMatrix = 0;
int statsMaterial = 0;
int statsDraw = 0;
GLuint meshTaskProgram = m_config.useCulling ? res->m_programs.draw_object_cull_mesh_task : res->m_programs.draw_object_mesh_task;
GLuint meshNoTaskProgram = m_config.useCulling ? res->m_programs.draw_object_cull_mesh : res->m_programs.draw_object_mesh;
bool first = true;
for(const auto & di : m_list->m_drawItems)
{
bool useTask = di.task;
if(first || useTask != lastTask)
{
glUseProgram(useTask ? meshTaskProgram : meshNoTaskProgram);
lastTask = useTask;
first = false;
}
if(lastGeometry != di.geometryIndex)
{
const CadSceneGL::Geometry& geo = sceneGL.m_geometry[di.geometryIndex];
int chunk = int(geo.mem.chunkIndex);
if(lastChunk != chunk)
{
if(bindless)
{
glBufferAddressRangeNV(GL_UNIFORM_BUFFER_ADDRESS_NV, UBO_GEOMETRY,
res->m_setup.geometryBindings.bufferADDR + sizeof(CadSceneGL::GeometryUbo) * chunk,
sizeof(CadSceneGL::GeometryUbo));
}
else
{
glBindBufferRange(GL_UNIFORM_BUFFER, UBO_GEOMETRY, res->m_setup.geometryBindings.buffer,
sizeof(CadSceneGL::GeometryUbo) * chunk, sizeof(CadSceneGL::GeometryUbo));
}
lastChunk = chunk;
}
// we use the same vertex offset for both vbo and abo, our allocator should ensure this condition.
assert(uint32_t(geo.vbo.offset / vertexSize) == uint32_t(geo.abo.offset / vertexAttributeSize));
glUniform4ui(0, uint32_t(geo.topoMeshlet.offset / sizeof(NVMeshlet::MeshletDesc)),
uint32_t(geo.topoPrim.offset), 0, uint32_t(geo.vbo.offset / vertexSize));
lastGeometry = di.geometryIndex;
statsGeometry++;
}
if(lastMatrix != di.matrixIndex)
{
if(bindless)
{
glBufferAddressRangeNV(GL_UNIFORM_BUFFER_ADDRESS_NV, UBO_OBJECT,
res->m_scene.m_buffers.matrices.bufferADDR + res->m_alignedMatrixSize * di.matrixIndex,
sizeof(CadScene::MatrixNode));
}
else
{
glBindBufferRange(GL_UNIFORM_BUFFER, UBO_OBJECT, res->m_scene.m_buffers.matrices.buffer,
res->m_alignedMatrixSize * di.matrixIndex, sizeof(CadScene::MatrixNode));
}
lastMatrix = di.matrixIndex;
statsMatrix++;
}
glUniform4ui(1, di.meshlet.offset, di.meshlet.offset + di.meshlet.count - 1, 0, 0);
uint32_t count = useTask ?
((di.meshlet.count + m_list->m_config.taskNumMeshlets - 1) / m_list->m_config.taskNumMeshlets) :
((di.meshlet.count + m_list->m_config.meshNumMeshlets - 1) / m_list->m_config.meshNumMeshlets);
glDrawMeshTasksNV(0, count);
statsDraw++;
}
(void)statsGeometry;
(void)statsMatrix;
(void)statsDraw;
}
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, SSBO_SCENE_STATS, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, UBO_SCENE_VIEW, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, UBO_OBJECT, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, UBO_GEOMETRY, 0);
res->copyStats();
if(res->m_clipping)
{
for(int i = 0; i < NUM_CLIPPING_PLANES; i++)
{
glDisable(GL_CLIP_DISTANCE0 + i);
}
}
if(m_bindless)
{
glDisableClientState(GL_UNIFORM_BUFFER_UNIFIED_NV);
}
if(global.meshletBoxes)
{
res->drawBoundingBoxes(m_list);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
} // namespace meshlettest