Skip to content

Commit bc799fe

Browse files
committed
Zephyr: Renderer: use atomic counter to control number of final draws
1 parent 774c15b commit bc799fe

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

zephyr/renderer/src/backend/opengl/render_backend.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ namespace zephyr {
3131
glCreateBuffers(1u, &m_gl_draw_count_ubo);
3232
glNamedBufferStorage(m_gl_draw_count_ubo, sizeof(u32), nullptr, GL_DYNAMIC_STORAGE_BIT);
3333

34+
glCreateBuffers(1u, &m_gl_draw_count_out_ac);
35+
glNamedBufferStorage(m_gl_draw_count_out_ac, sizeof(GLuint), nullptr, 0);
36+
3437
glEnable(GL_DEPTH_TEST);
3538

3639
m_render_geometry_manager = std::make_unique<OpenGLRenderGeometryManager>();
@@ -113,6 +116,7 @@ namespace zephyr {
113116
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1u, m_gl_render_bundle_ssbo);
114117
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2u, m_gl_draw_list_ssbo);
115118
glBindBufferBase(GL_UNIFORM_BUFFER, 0u, m_gl_draw_count_ubo);
119+
glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0u, m_gl_draw_count_out_ac);
116120

117121
const GLuint workgroup_size = 32u;
118122
const GLuint workgroup_group_count = (number_of_draws + workgroup_size - 1u) / workgroup_size;
@@ -122,6 +126,7 @@ namespace zephyr {
122126
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1u, 0u);
123127
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2u, 0u);
124128
glBindBufferBase(GL_UNIFORM_BUFFER, 0u, 0u);
129+
glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0u, 0u);
125130
}
126131

127132
// 3. draw everything written to the Draw List SSBO
@@ -130,14 +135,17 @@ namespace zephyr {
130135

131136
glBindVertexArray(m_render_geometry_manager->GetVAOFromLayout(RenderGeometryLayout{key}));
132137
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, m_gl_draw_list_ssbo);
138+
glBindBuffer(GL_PARAMETER_BUFFER, m_gl_draw_count_out_ac);
133139
glBindBufferBase(GL_UNIFORM_BUFFER, 0u, m_gl_camera_ubo);
134140
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0u, m_gl_render_bundle_ssbo);
135141

136-
glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
137-
glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr, (GLsizei)number_of_draws, sizeof(OpenGLDrawElementsIndirectCommand));
142+
glMemoryBarrier(GL_COMMAND_BARRIER_BIT | GL_ATOMIC_COUNTER_BARRIER_BIT);
143+
//glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr, (GLsizei)number_of_draws, sizeof(OpenGLDrawElementsIndirectCommand));
144+
glMultiDrawElementsIndirectCount(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr, 0u, (GLsizei)number_of_draws, sizeof(OpenGLDrawElementsIndirectCommand));
138145

139146
glBindVertexArray(0u);
140147
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0u);
148+
glBindBuffer(GL_PARAMETER_BUFFER, 0u);
141149
glBindBufferBase(GL_UNIFORM_BUFFER, 0u, 0u);
142150
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0u, 0u);
143151
}
@@ -234,11 +242,19 @@ namespace zephyr {
234242
uint u_draw_count;
235243
};
236244
245+
layout(binding = 0) uniform atomic_uint u_draw_count_out;
246+
237247
void main() {
238248
const uint draw_index = gl_GlobalInvocationID.x;
239249
250+
if(draw_index == 0u) {
251+
atomicCounterExchange(u_draw_count_out, 0u);
252+
}
253+
barrier();
254+
240255
if(draw_index < u_draw_count) {
241256
b_command_buffer[draw_index] = rb_geometry_commands[rb_render_bundle_items[draw_index].draw_command_id];
257+
atomicCounterIncrement(u_draw_count_out);
242258
}
243259
}
244260
)", GL_COMPUTE_SHADER);

zephyr/renderer/src/backend/opengl/render_backend.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ namespace zephyr {
5454
GLuint m_gl_draw_list_ssbo{};
5555
GLuint m_gl_camera_ubo{};
5656
GLuint m_gl_draw_count_ubo{};
57+
GLuint m_gl_draw_count_out_ac{};
5758

5859
std::unique_ptr<OpenGLRenderGeometryManager> m_render_geometry_manager{};
5960
};

0 commit comments

Comments
 (0)