Skip to content

Commit 6d09a20

Browse files
committed
Merge pull request godotengine#97247 from thimenesup/draw_indirect_rd
Add draw indirect to Rendering Device
2 parents 9169ace + d36a872 commit 6d09a20

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed

doc/classes/RenderingDevice.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@
324324
Submits [param draw_list] for rendering on the GPU. This is the raster equivalent to [method compute_list_dispatch].
325325
</description>
326326
</method>
327+
<method name="draw_list_draw_indirect">
328+
<return type="void" />
329+
<param index="0" name="draw_list" type="int" />
330+
<param index="1" name="use_indices" type="bool" />
331+
<param index="2" name="buffer" type="RID" />
332+
<param index="3" name="offset" type="int" default="0" />
333+
<param index="4" name="draw_count" type="int" default="1" />
334+
<param index="5" name="stride" type="int" default="0" />
335+
<description>
336+
Submits [param draw_list] for rendering on the GPU with the given parameters stored in the [param buffer] at [param offset]. Parameters being integers: vertex count, instance count, first vertex, first instance. And when using indices: index count, instance count, first index, vertex offset, first instance. Buffer must have been created with [constant STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT] flag.
337+
</description>
338+
</method>
327339
<method name="draw_list_enable_scissor">
328340
<return type="void" />
329341
<param index="0" name="draw_list" type="int" />

servers/rendering/rendering_device.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4440,6 +4440,117 @@ void RenderingDevice::draw_list_draw(DrawListID p_list, bool p_use_indices, uint
44404440
dl->state.draw_count++;
44414441
}
44424442

4443+
void RenderingDevice::draw_list_draw_indirect(DrawListID p_list, bool p_use_indices, RID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {
4444+
ERR_RENDER_THREAD_GUARD();
4445+
4446+
DrawList *dl = _get_draw_list_ptr(p_list);
4447+
ERR_FAIL_NULL(dl);
4448+
4449+
Buffer *buffer = storage_buffer_owner.get_or_null(p_buffer);
4450+
ERR_FAIL_NULL(buffer);
4451+
4452+
ERR_FAIL_COND_MSG(!buffer->usage.has_flag(RDD::BUFFER_USAGE_INDIRECT_BIT), "Buffer provided was not created to do indirect dispatch.");
4453+
4454+
#ifdef DEBUG_ENABLED
4455+
ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
4456+
#endif
4457+
4458+
#ifdef DEBUG_ENABLED
4459+
ERR_FAIL_COND_MSG(!dl->validation.pipeline_active,
4460+
"No render pipeline was set before attempting to draw.");
4461+
if (dl->validation.pipeline_vertex_format != INVALID_ID) {
4462+
// Pipeline uses vertices, validate format.
4463+
ERR_FAIL_COND_MSG(dl->validation.vertex_format == INVALID_ID,
4464+
"No vertex array was bound, and render pipeline expects vertices.");
4465+
// Make sure format is right.
4466+
ERR_FAIL_COND_MSG(dl->validation.pipeline_vertex_format != dl->validation.vertex_format,
4467+
"The vertex format used to create the pipeline does not match the vertex format bound.");
4468+
}
4469+
4470+
if (dl->validation.pipeline_push_constant_size > 0) {
4471+
// Using push constants, check that they were supplied.
4472+
ERR_FAIL_COND_MSG(!dl->validation.pipeline_push_constant_supplied,
4473+
"The shader in this pipeline requires a push constant to be set before drawing, but it's not present.");
4474+
}
4475+
#endif
4476+
4477+
#ifdef DEBUG_ENABLED
4478+
for (uint32_t i = 0; i < dl->state.set_count; i++) {
4479+
if (dl->state.sets[i].pipeline_expected_format == 0) {
4480+
// Nothing expected by this pipeline.
4481+
continue;
4482+
}
4483+
4484+
if (dl->state.sets[i].pipeline_expected_format != dl->state.sets[i].uniform_set_format) {
4485+
if (dl->state.sets[i].uniform_set_format == 0) {
4486+
ERR_FAIL_MSG(vformat("Uniforms were never supplied for set (%d) at the time of drawing, which are required by the pipeline.", i));
4487+
} else if (uniform_set_owner.owns(dl->state.sets[i].uniform_set)) {
4488+
UniformSet *us = uniform_set_owner.get_or_null(dl->state.sets[i].uniform_set);
4489+
ERR_FAIL_MSG(vformat("Uniforms supplied for set (%d):\n%s\nare not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n%s", i, _shader_uniform_debug(us->shader_id, us->shader_set), _shader_uniform_debug(dl->state.pipeline_shader)));
4490+
} else {
4491+
ERR_FAIL_MSG(vformat("Uniforms supplied for set (%s, which was just freed) are not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n%s", i, _shader_uniform_debug(dl->state.pipeline_shader)));
4492+
}
4493+
}
4494+
}
4495+
#endif
4496+
4497+
// Prepare descriptor sets if the API doesn't use pipeline barriers.
4498+
if (!driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
4499+
for (uint32_t i = 0; i < dl->state.set_count; i++) {
4500+
if (dl->state.sets[i].pipeline_expected_format == 0) {
4501+
// Nothing expected by this pipeline.
4502+
continue;
4503+
}
4504+
4505+
draw_graph.add_draw_list_uniform_set_prepare_for_use(dl->state.pipeline_shader_driver_id, dl->state.sets[i].uniform_set_driver_id, i);
4506+
}
4507+
}
4508+
4509+
// Bind descriptor sets.
4510+
for (uint32_t i = 0; i < dl->state.set_count; i++) {
4511+
if (dl->state.sets[i].pipeline_expected_format == 0) {
4512+
continue; // Nothing expected by this pipeline.
4513+
}
4514+
if (!dl->state.sets[i].bound) {
4515+
// All good, see if this requires re-binding.
4516+
draw_graph.add_draw_list_bind_uniform_set(dl->state.pipeline_shader_driver_id, dl->state.sets[i].uniform_set_driver_id, i);
4517+
4518+
UniformSet *uniform_set = uniform_set_owner.get_or_null(dl->state.sets[i].uniform_set);
4519+
_uniform_set_update_shared(uniform_set);
4520+
4521+
draw_graph.add_draw_list_usages(uniform_set->draw_trackers, uniform_set->draw_trackers_usage);
4522+
4523+
dl->state.sets[i].bound = true;
4524+
}
4525+
}
4526+
4527+
if (p_use_indices) {
4528+
#ifdef DEBUG_ENABLED
4529+
ERR_FAIL_COND_MSG(!dl->validation.index_array_count,
4530+
"Draw command requested indices, but no index buffer was set.");
4531+
4532+
ERR_FAIL_COND_MSG(dl->validation.pipeline_uses_restart_indices != dl->validation.index_buffer_uses_restart_indices,
4533+
"The usage of restart indices in index buffer does not match the render primitive in the pipeline.");
4534+
#endif
4535+
4536+
ERR_FAIL_COND_MSG(p_offset + 20 > buffer->size, "Offset provided (+20) is past the end of buffer.");
4537+
4538+
draw_graph.add_draw_list_draw_indexed_indirect(buffer->driver_id, p_offset, p_draw_count, p_stride);
4539+
} else {
4540+
ERR_FAIL_COND_MSG(p_offset + 16 > buffer->size, "Offset provided (+16) is past the end of buffer.");
4541+
4542+
draw_graph.add_draw_list_draw_indirect(buffer->driver_id, p_offset, p_draw_count, p_stride);
4543+
}
4544+
4545+
dl->state.draw_count++;
4546+
4547+
if (buffer->draw_tracker != nullptr) {
4548+
draw_graph.add_draw_list_usage(buffer->draw_tracker, RDG::RESOURCE_USAGE_INDIRECT_BUFFER_READ);
4549+
}
4550+
4551+
_check_transfer_worker_buffer(buffer);
4552+
}
4553+
44434554
void RenderingDevice::draw_list_enable_scissor(DrawListID p_list, const Rect2 &p_rect) {
44444555
ERR_RENDER_THREAD_GUARD();
44454556

@@ -6656,6 +6767,7 @@ void RenderingDevice::_bind_methods() {
66566767
ClassDB::bind_method(D_METHOD("draw_list_set_push_constant", "draw_list", "buffer", "size_bytes"), &RenderingDevice::_draw_list_set_push_constant);
66576768

66586769
ClassDB::bind_method(D_METHOD("draw_list_draw", "draw_list", "use_indices", "instances", "procedural_vertex_count"), &RenderingDevice::draw_list_draw, DEFVAL(0));
6770+
ClassDB::bind_method(D_METHOD("draw_list_draw_indirect", "draw_list", "use_indices", "buffer", "offset", "draw_count", "stride"), &RenderingDevice::draw_list_draw_indirect, DEFVAL(0), DEFVAL(1), DEFVAL(0));
66596771

66606772
ClassDB::bind_method(D_METHOD("draw_list_enable_scissor", "draw_list", "rect"), &RenderingDevice::draw_list_enable_scissor, DEFVAL(Rect2()));
66616773
ClassDB::bind_method(D_METHOD("draw_list_disable_scissor", "draw_list"), &RenderingDevice::draw_list_disable_scissor);

servers/rendering/rendering_device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,7 @@ class RenderingDevice : public RenderingDeviceCommons {
11861186
void draw_list_set_push_constant(DrawListID p_list, const void *p_data, uint32_t p_data_size);
11871187

11881188
void draw_list_draw(DrawListID p_list, bool p_use_indices, uint32_t p_instances = 1, uint32_t p_procedural_vertices = 0);
1189+
void draw_list_draw_indirect(DrawListID p_list, bool p_use_indices, RID p_buffer, uint32_t p_offset = 0, uint32_t p_draw_count = 1, uint32_t p_stride = 0);
11891190

11901191
void draw_list_enable_scissor(DrawListID p_list, const Rect2 &p_rect);
11911192
void draw_list_disable_scissor(DrawListID p_list);

servers/rendering/rendering_device_graph.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,16 @@ void RenderingDeviceGraph::_run_draw_list_command(RDD::CommandBufferID p_command
708708
driver->command_render_draw_indexed(p_command_buffer, draw_indexed_instruction->index_count, draw_indexed_instruction->instance_count, draw_indexed_instruction->first_index, 0, 0);
709709
instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);
710710
} break;
711+
case DrawListInstruction::TYPE_DRAW_INDIRECT: {
712+
const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);
713+
driver->command_render_draw_indirect(p_command_buffer, draw_indirect_instruction->buffer, draw_indirect_instruction->offset, draw_indirect_instruction->draw_count, draw_indirect_instruction->stride);
714+
instruction_data_cursor += sizeof(DrawListDrawIndirectInstruction);
715+
} break;
716+
case DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT: {
717+
const DrawListDrawIndexedIndirectInstruction *draw_indexed_indirect_instruction = reinterpret_cast<const DrawListDrawIndexedIndirectInstruction *>(instruction);
718+
driver->command_render_draw_indexed_indirect(p_command_buffer, draw_indexed_indirect_instruction->buffer, draw_indexed_indirect_instruction->offset, draw_indexed_indirect_instruction->draw_count, draw_indexed_indirect_instruction->stride);
719+
instruction_data_cursor += sizeof(DrawListDrawIndexedIndirectInstruction);
720+
} break;
711721
case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {
712722
const DrawListExecuteCommandsInstruction *execute_commands_instruction = reinterpret_cast<const DrawListExecuteCommandsInstruction *>(instruction);
713723
driver->command_buffer_execute_secondary(p_command_buffer, execute_commands_instruction->command_buffer);
@@ -1189,6 +1199,16 @@ void RenderingDeviceGraph::_print_draw_list(const uint8_t *p_instruction_data, u
11891199
print_line("\tDRAW INDICES", draw_indexed_instruction->index_count, "INSTANCES", draw_indexed_instruction->instance_count, "FIRST INDEX", draw_indexed_instruction->first_index);
11901200
instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);
11911201
} break;
1202+
case DrawListInstruction::TYPE_DRAW_INDIRECT: {
1203+
const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);
1204+
print_line("\tDRAW INDIRECT BUFFER ID", itos(draw_indirect_instruction->buffer.id), "OFFSET", draw_indirect_instruction->offset, "DRAW COUNT", draw_indirect_instruction->draw_count, "STRIDE", draw_indirect_instruction->stride);
1205+
instruction_data_cursor += sizeof(DrawListDrawIndirectInstruction);
1206+
} break;
1207+
case DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT: {
1208+
const DrawListDrawIndexedIndirectInstruction *draw_indexed_indirect_instruction = reinterpret_cast<const DrawListDrawIndexedIndirectInstruction *>(instruction);
1209+
print_line("\tDRAW INDEXED INDIRECT BUFFER ID", itos(draw_indexed_indirect_instruction->buffer.id), "OFFSET", draw_indexed_indirect_instruction->offset, "DRAW COUNT", draw_indexed_indirect_instruction->draw_count, "STRIDE", draw_indexed_indirect_instruction->stride);
1210+
instruction_data_cursor += sizeof(DrawListDrawIndexedIndirectInstruction);
1211+
} break;
11921212
case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {
11931213
print_line("\tEXECUTE COMMANDS");
11941214
instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);
@@ -1611,6 +1631,26 @@ void RenderingDeviceGraph::add_draw_list_draw_indexed(uint32_t p_index_count, ui
16111631
instruction->first_index = p_first_index;
16121632
}
16131633

1634+
void RenderingDeviceGraph::add_draw_list_draw_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {
1635+
DrawListDrawIndirectInstruction *instruction = reinterpret_cast<DrawListDrawIndirectInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndirectInstruction)));
1636+
instruction->type = DrawListInstruction::TYPE_DRAW_INDIRECT;
1637+
instruction->buffer = p_buffer;
1638+
instruction->offset = p_offset;
1639+
instruction->draw_count = p_draw_count;
1640+
instruction->stride = p_stride;
1641+
draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
1642+
}
1643+
1644+
void RenderingDeviceGraph::add_draw_list_draw_indexed_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {
1645+
DrawListDrawIndexedIndirectInstruction *instruction = reinterpret_cast<DrawListDrawIndexedIndirectInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndexedIndirectInstruction)));
1646+
instruction->type = DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT;
1647+
instruction->buffer = p_buffer;
1648+
instruction->offset = p_offset;
1649+
instruction->draw_count = p_draw_count;
1650+
instruction->stride = p_stride;
1651+
draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
1652+
}
1653+
16141654
void RenderingDeviceGraph::add_draw_list_execute_commands(RDD::CommandBufferID p_command_buffer) {
16151655
DrawListExecuteCommandsInstruction *instruction = reinterpret_cast<DrawListExecuteCommandsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListExecuteCommandsInstruction)));
16161656
instruction->type = DrawListInstruction::TYPE_EXECUTE_COMMANDS;

servers/rendering/rendering_device_graph.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class RenderingDeviceGraph {
6969
TYPE_CLEAR_ATTACHMENTS,
7070
TYPE_DRAW,
7171
TYPE_DRAW_INDEXED,
72+
TYPE_DRAW_INDIRECT,
73+
TYPE_DRAW_INDEXED_INDIRECT,
7274
TYPE_EXECUTE_COMMANDS,
7375
TYPE_NEXT_SUBPASS,
7476
TYPE_SET_BLEND_CONSTANTS,
@@ -472,6 +474,20 @@ class RenderingDeviceGraph {
472474
uint32_t first_index = 0;
473475
};
474476

477+
struct DrawListDrawIndirectInstruction : DrawListInstruction {
478+
RDD::BufferID buffer;
479+
uint32_t offset = 0;
480+
uint32_t draw_count = 0;
481+
uint32_t stride = 0;
482+
};
483+
484+
struct DrawListDrawIndexedIndirectInstruction : DrawListInstruction {
485+
RDD::BufferID buffer;
486+
uint32_t offset = 0;
487+
uint32_t draw_count = 0;
488+
uint32_t stride = 0;
489+
};
490+
475491
struct DrawListEndRenderPassInstruction : DrawListInstruction {
476492
// No contents.
477493
};
@@ -684,6 +700,8 @@ class RenderingDeviceGraph {
684700
void add_draw_list_clear_attachments(VectorView<RDD::AttachmentClear> p_attachments_clear, VectorView<Rect2i> p_attachments_clear_rect);
685701
void add_draw_list_draw(uint32_t p_vertex_count, uint32_t p_instance_count);
686702
void add_draw_list_draw_indexed(uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index);
703+
void add_draw_list_draw_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride);
704+
void add_draw_list_draw_indexed_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride);
687705
void add_draw_list_execute_commands(RDD::CommandBufferID p_command_buffer);
688706
void add_draw_list_next_subpass(RDD::CommandBufferType p_command_buffer_type);
689707
void add_draw_list_set_blend_constants(const Color &p_color);

0 commit comments

Comments
 (0)