Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/rendering_device_graph.cpp
10277 views
1
/**************************************************************************/
2
/* rendering_device_graph.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "rendering_device_graph.h"
32
33
#define PRINT_RENDER_GRAPH 0
34
#define FORCE_FULL_ACCESS_BITS 0
35
#define PRINT_RESOURCE_TRACKER_TOTAL 0
36
#define PRINT_COMMAND_RECORDING 0
37
38
RenderingDeviceGraph::RenderingDeviceGraph() {
39
driver_honors_barriers = false;
40
driver_clears_with_copy_engine = false;
41
}
42
43
RenderingDeviceGraph::~RenderingDeviceGraph() {
44
}
45
46
String RenderingDeviceGraph::_usage_to_string(ResourceUsage p_usage) {
47
switch (p_usage) {
48
case RESOURCE_USAGE_NONE:
49
return "None";
50
case RESOURCE_USAGE_COPY_FROM:
51
return "Copy From";
52
case RESOURCE_USAGE_COPY_TO:
53
return "Copy To";
54
case RESOURCE_USAGE_RESOLVE_FROM:
55
return "Resolve From";
56
case RESOURCE_USAGE_RESOLVE_TO:
57
return "Resolve To";
58
case RESOURCE_USAGE_UNIFORM_BUFFER_READ:
59
return "Uniform Buffer Read";
60
case RESOURCE_USAGE_INDIRECT_BUFFER_READ:
61
return "Indirect Buffer Read";
62
case RESOURCE_USAGE_TEXTURE_BUFFER_READ:
63
return "Texture Buffer Read";
64
case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:
65
return "Texture Buffer Read Write";
66
case RESOURCE_USAGE_STORAGE_BUFFER_READ:
67
return "Storage Buffer Read";
68
case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:
69
return "Storage Buffer Read Write";
70
case RESOURCE_USAGE_VERTEX_BUFFER_READ:
71
return "Vertex Buffer Read";
72
case RESOURCE_USAGE_INDEX_BUFFER_READ:
73
return "Index Buffer Read";
74
case RESOURCE_USAGE_TEXTURE_SAMPLE:
75
return "Texture Sample";
76
case RESOURCE_USAGE_STORAGE_IMAGE_READ:
77
return "Storage Image Read";
78
case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
79
return "Storage Image Read Write";
80
case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
81
return "Attachment Color Read Write";
82
case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
83
return "Attachment Depth Stencil Read Write";
84
case RESOURCE_USAGE_GENERAL:
85
return "General";
86
default:
87
ERR_FAIL_V_MSG("Invalid", vformat("Invalid resource usage %d.", p_usage));
88
}
89
}
90
91
bool RenderingDeviceGraph::_is_write_usage(ResourceUsage p_usage) {
92
switch (p_usage) {
93
case RESOURCE_USAGE_COPY_FROM:
94
case RESOURCE_USAGE_RESOLVE_FROM:
95
case RESOURCE_USAGE_UNIFORM_BUFFER_READ:
96
case RESOURCE_USAGE_INDIRECT_BUFFER_READ:
97
case RESOURCE_USAGE_TEXTURE_BUFFER_READ:
98
case RESOURCE_USAGE_STORAGE_BUFFER_READ:
99
case RESOURCE_USAGE_VERTEX_BUFFER_READ:
100
case RESOURCE_USAGE_INDEX_BUFFER_READ:
101
case RESOURCE_USAGE_TEXTURE_SAMPLE:
102
case RESOURCE_USAGE_STORAGE_IMAGE_READ:
103
case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:
104
case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:
105
return false;
106
case RESOURCE_USAGE_COPY_TO:
107
case RESOURCE_USAGE_RESOLVE_TO:
108
case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:
109
case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:
110
case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
111
case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
112
case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
113
case RESOURCE_USAGE_GENERAL:
114
return true;
115
default:
116
DEV_ASSERT(false && "Invalid resource tracker usage.");
117
return false;
118
}
119
}
120
121
RDD::TextureLayout RenderingDeviceGraph::_usage_to_image_layout(ResourceUsage p_usage) {
122
switch (p_usage) {
123
case RESOURCE_USAGE_COPY_FROM:
124
return RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL;
125
case RESOURCE_USAGE_COPY_TO:
126
return RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL;
127
case RESOURCE_USAGE_RESOLVE_FROM:
128
return RDD::TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL;
129
case RESOURCE_USAGE_RESOLVE_TO:
130
return RDD::TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL;
131
case RESOURCE_USAGE_TEXTURE_SAMPLE:
132
return RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
133
case RESOURCE_USAGE_STORAGE_IMAGE_READ:
134
case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
135
return RDD::TEXTURE_LAYOUT_STORAGE_OPTIMAL;
136
case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
137
return RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
138
case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
139
return RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
140
case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:
141
return RDD::TEXTURE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL;
142
case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:
143
return RDD::TEXTURE_LAYOUT_FRAGMENT_DENSITY_MAP_ATTACHMENT_OPTIMAL;
144
case RESOURCE_USAGE_GENERAL:
145
return RDD::TEXTURE_LAYOUT_GENERAL;
146
case RESOURCE_USAGE_NONE:
147
return RDD::TEXTURE_LAYOUT_UNDEFINED;
148
default:
149
DEV_ASSERT(false && "Invalid resource tracker usage or not an image usage.");
150
return RDD::TEXTURE_LAYOUT_UNDEFINED;
151
}
152
}
153
154
RDD::BarrierAccessBits RenderingDeviceGraph::_usage_to_access_bits(ResourceUsage p_usage) {
155
#if FORCE_FULL_ACCESS_BITS
156
return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT);
157
#else
158
switch (p_usage) {
159
case RESOURCE_USAGE_NONE:
160
return RDD::BarrierAccessBits(0);
161
case RESOURCE_USAGE_COPY_FROM:
162
return RDD::BARRIER_ACCESS_COPY_READ_BIT;
163
case RESOURCE_USAGE_COPY_TO:
164
return RDD::BARRIER_ACCESS_COPY_WRITE_BIT;
165
case RESOURCE_USAGE_RESOLVE_FROM:
166
return RDD::BARRIER_ACCESS_RESOLVE_READ_BIT;
167
case RESOURCE_USAGE_RESOLVE_TO:
168
return RDD::BARRIER_ACCESS_RESOLVE_WRITE_BIT;
169
case RESOURCE_USAGE_UNIFORM_BUFFER_READ:
170
return RDD::BARRIER_ACCESS_UNIFORM_READ_BIT;
171
case RESOURCE_USAGE_INDIRECT_BUFFER_READ:
172
return RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT;
173
case RESOURCE_USAGE_STORAGE_BUFFER_READ:
174
case RESOURCE_USAGE_STORAGE_IMAGE_READ:
175
case RESOURCE_USAGE_TEXTURE_BUFFER_READ:
176
case RESOURCE_USAGE_TEXTURE_SAMPLE:
177
return RDD::BARRIER_ACCESS_SHADER_READ_BIT;
178
case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:
179
case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:
180
case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
181
return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
182
case RESOURCE_USAGE_VERTEX_BUFFER_READ:
183
return RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
184
case RESOURCE_USAGE_INDEX_BUFFER_READ:
185
return RDD::BARRIER_ACCESS_INDEX_READ_BIT;
186
case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
187
return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);
188
case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
189
return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT);
190
case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:
191
return RDD::BARRIER_ACCESS_FRAGMENT_SHADING_RATE_ATTACHMENT_READ_BIT;
192
case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:
193
return RDD::BARRIER_ACCESS_FRAGMENT_DENSITY_MAP_ATTACHMENT_READ_BIT;
194
case RESOURCE_USAGE_GENERAL:
195
return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT);
196
default:
197
DEV_ASSERT(false && "Invalid usage.");
198
return RDD::BarrierAccessBits(0);
199
}
200
#endif
201
}
202
203
bool RenderingDeviceGraph::_check_command_intersection(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) const {
204
if (p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE && p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE) {
205
// We don't check possible intersections for usages that aren't consecutive color or depth writes.
206
return true;
207
}
208
209
const uint32_t previous_command_data_offset = command_data_offsets[p_previous_command_index];
210
const uint32_t current_command_data_offset = command_data_offsets[p_command_index];
211
const RecordedDrawListCommand &previous_draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[previous_command_data_offset]);
212
const RecordedDrawListCommand &current_draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[current_command_data_offset]);
213
if (previous_draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST || current_draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST) {
214
// We don't check possible intersections if both commands aren't draw lists.
215
return true;
216
}
217
218
// We check if the region used by both draw lists have an intersection.
219
return previous_draw_list_command.region.intersects(current_draw_list_command.region);
220
}
221
222
bool RenderingDeviceGraph::_check_command_partial_coverage(ResourceTracker *p_resource_tracker, int32_t p_command_index) const {
223
if (p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE && p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE) {
224
// We don't check for partial coverage in usages that aren't attachment writes.
225
return false;
226
}
227
228
const uint32_t command_data_offset = command_data_offsets[p_command_index];
229
const RecordedDrawListCommand &draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[command_data_offset]);
230
if (draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST) {
231
// We don't check for partial coverage on commands that aren't draw lists.
232
return false;
233
}
234
235
Rect2i texture_region(Point2i(0, 0), p_resource_tracker->texture_size);
236
return !draw_list_command.region.encloses(texture_region);
237
}
238
239
int32_t RenderingDeviceGraph::_add_to_command_list(int32_t p_command_index, int32_t p_list_index) {
240
DEV_ASSERT(p_command_index < int32_t(command_count));
241
DEV_ASSERT(p_list_index < int32_t(command_list_nodes.size()));
242
243
int32_t next_index = int32_t(command_list_nodes.size());
244
command_list_nodes.resize(next_index + 1);
245
246
RecordedCommandListNode &new_node = command_list_nodes[next_index];
247
new_node.command_index = p_command_index;
248
new_node.next_list_index = p_list_index;
249
return next_index;
250
}
251
252
void RenderingDeviceGraph::_add_adjacent_command(int32_t p_previous_command_index, int32_t p_command_index, RecordedCommand *r_command) {
253
const uint32_t previous_command_data_offset = command_data_offsets[p_previous_command_index];
254
RecordedCommand &previous_command = *reinterpret_cast<RecordedCommand *>(&command_data[previous_command_data_offset]);
255
previous_command.adjacent_command_list_index = _add_to_command_list(p_command_index, previous_command.adjacent_command_list_index);
256
previous_command.next_stages = previous_command.next_stages | r_command->self_stages;
257
r_command->previous_stages = r_command->previous_stages | previous_command.self_stages;
258
}
259
260
int32_t RenderingDeviceGraph::_add_to_slice_read_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index) {
261
DEV_ASSERT(p_command_index < int32_t(command_count));
262
DEV_ASSERT(p_list_index < int32_t(read_slice_list_nodes.size()));
263
264
int32_t next_index = int32_t(read_slice_list_nodes.size());
265
read_slice_list_nodes.resize(next_index + 1);
266
267
RecordedSliceListNode &new_node = read_slice_list_nodes[next_index];
268
new_node.command_index = p_command_index;
269
new_node.next_list_index = p_list_index;
270
new_node.subresources = p_subresources;
271
return next_index;
272
}
273
274
int32_t RenderingDeviceGraph::_add_to_write_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index, bool p_partial_coverage) {
275
DEV_ASSERT(p_command_index < int32_t(command_count));
276
DEV_ASSERT(p_list_index < int32_t(write_slice_list_nodes.size()));
277
278
int32_t next_index = int32_t(write_slice_list_nodes.size());
279
write_slice_list_nodes.resize(next_index + 1);
280
281
RecordedSliceListNode &new_node = write_slice_list_nodes[next_index];
282
new_node.command_index = p_command_index;
283
new_node.next_list_index = p_list_index;
284
new_node.subresources = p_subresources;
285
new_node.partial_coverage = p_partial_coverage;
286
return next_index;
287
}
288
289
// Ensures all commands are 8-byte aligned.
290
#define GRAPH_ALIGN(x) (((x) + 7u) & 0xFFFFFFF8u)
291
292
RenderingDeviceGraph::RecordedCommand *RenderingDeviceGraph::_allocate_command(uint32_t p_command_size, int32_t &r_command_index) {
293
uint32_t command_data_offset = command_data.size();
294
command_data_offset = GRAPH_ALIGN(command_data_offset);
295
command_data_offsets.push_back(command_data_offset);
296
command_data.resize(command_data_offset + p_command_size);
297
r_command_index = command_count++;
298
RecordedCommand *new_command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
299
*new_command = RecordedCommand();
300
return new_command;
301
}
302
303
RenderingDeviceGraph::DrawListInstruction *RenderingDeviceGraph::_allocate_draw_list_instruction(uint32_t p_instruction_size) {
304
uint32_t draw_list_data_offset = draw_instruction_list.data.size();
305
draw_list_data_offset = GRAPH_ALIGN(draw_list_data_offset);
306
draw_instruction_list.data.resize(draw_list_data_offset + p_instruction_size);
307
return reinterpret_cast<DrawListInstruction *>(&draw_instruction_list.data[draw_list_data_offset]);
308
}
309
310
RenderingDeviceGraph::ComputeListInstruction *RenderingDeviceGraph::_allocate_compute_list_instruction(uint32_t p_instruction_size) {
311
uint32_t compute_list_data_offset = compute_instruction_list.data.size();
312
compute_list_data_offset = GRAPH_ALIGN(compute_list_data_offset);
313
compute_instruction_list.data.resize(compute_list_data_offset + p_instruction_size);
314
return reinterpret_cast<ComputeListInstruction *>(&compute_instruction_list.data[compute_list_data_offset]);
315
}
316
317
void RenderingDeviceGraph::_check_discardable_attachment_dependency(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) {
318
if (!p_resource_tracker->is_discardable) {
319
return;
320
}
321
322
// Check if the command is a a draw list that clears the attachment completely. If it is, we don't need to modify the previous draw list.
323
uint32_t command_offset = command_data_offsets[p_command_index];
324
RecordedDrawListCommand *draw_list_command = reinterpret_cast<RecordedDrawListCommand *>(&command_data[command_offset]);
325
if (draw_list_command->type == RecordedCommand::TYPE_DRAW_LIST) {
326
ResourceTracker **trackers = draw_list_command->trackers();
327
for (uint32_t i = 0; i < draw_list_command->trackers_count; i++) {
328
if (trackers[i] == p_resource_tracker && draw_list_command->load_ops()[i] == RDD::ATTACHMENT_LOAD_OP_CLEAR) {
329
return;
330
}
331
}
332
}
333
334
// Check if the previous command is a draw list.
335
uint32_t previous_command_offset = command_data_offsets[p_previous_command_index];
336
RecordedDrawListCommand *previous_draw_list_command = reinterpret_cast<RecordedDrawListCommand *>(&command_data[previous_command_offset]);
337
if (previous_draw_list_command->type != RecordedCommand::TYPE_DRAW_LIST) {
338
return;
339
}
340
341
// Search for the tracker inside the draw list command and modify the store operation accordingly.
342
ResourceTracker **trackers = previous_draw_list_command->trackers();
343
for (uint32_t i = 0; i < previous_draw_list_command->trackers_count; i++) {
344
if (trackers[i] == p_resource_tracker) {
345
previous_draw_list_command->store_ops()[i] = RDD::ATTACHMENT_STORE_OP_STORE;
346
return;
347
}
348
}
349
}
350
351
void RenderingDeviceGraph::_add_command_to_graph(ResourceTracker **p_resource_trackers, ResourceUsage *p_resource_usages, uint32_t p_resource_count, int32_t p_command_index, RecordedCommand *r_command) {
352
// Assign the next stages derived from the stages the command requires first.
353
r_command->next_stages = r_command->self_stages;
354
355
if (command_label_index >= 0) {
356
// If a label is active, tag the command with the label.
357
r_command->label_index = command_label_index;
358
}
359
360
if (r_command->type == RecordedCommand::TYPE_CAPTURE_TIMESTAMP) {
361
// All previous commands starting from the previous timestamp should be adjacent to this command.
362
int32_t start_command_index = uint32_t(MAX(command_timestamp_index, 0));
363
for (int32_t i = start_command_index; i < p_command_index; i++) {
364
_add_adjacent_command(i, p_command_index, r_command);
365
}
366
367
// Make this command the new active timestamp command.
368
command_timestamp_index = p_command_index;
369
} else if (command_timestamp_index >= 0) {
370
// Timestamp command should be adjacent to this command.
371
_add_adjacent_command(command_timestamp_index, p_command_index, r_command);
372
}
373
374
if (command_synchronization_pending) {
375
// All previous commands should be adjacent to this command.
376
int32_t start_command_index = uint32_t(MAX(command_synchronization_index, 0));
377
for (int32_t i = start_command_index; i < p_command_index; i++) {
378
_add_adjacent_command(i, p_command_index, r_command);
379
}
380
381
command_synchronization_index = p_command_index;
382
command_synchronization_pending = false;
383
} else if (command_synchronization_index >= 0) {
384
// Synchronization command should be adjacent to this command.
385
_add_adjacent_command(command_synchronization_index, p_command_index, r_command);
386
}
387
388
for (uint32_t i = 0; i < p_resource_count; i++) {
389
ResourceTracker *resource_tracker = p_resource_trackers[i];
390
DEV_ASSERT(resource_tracker != nullptr);
391
392
resource_tracker->reset_if_outdated(tracking_frame);
393
394
const RDD::TextureSubresourceRange &subresources = resource_tracker->texture_subresources;
395
const Rect2i resource_tracker_rect(subresources.base_mipmap, subresources.base_layer, subresources.mipmap_count, subresources.layer_count);
396
Rect2i search_tracker_rect = resource_tracker_rect;
397
398
ResourceUsage new_resource_usage = p_resource_usages[i];
399
bool write_usage = _is_write_usage(new_resource_usage);
400
BitField<RDD::BarrierAccessBits> new_usage_access = _usage_to_access_bits(new_resource_usage);
401
bool is_resource_a_slice = resource_tracker->parent != nullptr;
402
if (is_resource_a_slice) {
403
// This resource depends on a parent resource.
404
resource_tracker->parent->reset_if_outdated(tracking_frame);
405
406
if (resource_tracker->texture_slice_command_index != p_command_index) {
407
// Indicate this slice has been used by this command.
408
resource_tracker->texture_slice_command_index = p_command_index;
409
}
410
411
if (resource_tracker->parent->usage == RESOURCE_USAGE_NONE) {
412
if (resource_tracker->parent->texture_driver_id.id != 0) {
413
// If the resource is a texture, we transition it entirely to the layout determined by the first slice that uses it.
414
_add_texture_barrier_to_command(resource_tracker->parent->texture_driver_id, RDD::BarrierAccessBits(0), new_usage_access, RDG::RESOURCE_USAGE_NONE, new_resource_usage, resource_tracker->parent->texture_subresources, command_normalization_barriers, r_command->normalization_barrier_index, r_command->normalization_barrier_count);
415
}
416
417
// If the parent hasn't been used yet, we assign the usage of the slice to the entire resource.
418
resource_tracker->parent->usage = new_resource_usage;
419
420
// Also assign the usage to the slice and consider it a write operation. Consider the parent's current usage access as its own.
421
resource_tracker->usage = new_resource_usage;
422
resource_tracker->usage_access = resource_tracker->parent->usage_access;
423
write_usage = true;
424
425
// Indicate the area that should be tracked is the entire resource.
426
const RDD::TextureSubresourceRange &parent_subresources = resource_tracker->parent->texture_subresources;
427
search_tracker_rect = Rect2i(parent_subresources.base_mipmap, parent_subresources.base_layer, parent_subresources.mipmap_count, parent_subresources.layer_count);
428
} else if (resource_tracker->in_parent_dirty_list) {
429
if (resource_tracker->parent->usage == new_resource_usage) {
430
// The slice will be transitioned to the resource of the parent and can be deleted from the dirty list.
431
ResourceTracker *previous_tracker = nullptr;
432
ResourceTracker *current_tracker = resource_tracker->parent->dirty_shared_list;
433
bool initialized_dirty_rect = false;
434
while (current_tracker != nullptr) {
435
current_tracker->reset_if_outdated(tracking_frame);
436
437
if (current_tracker == resource_tracker) {
438
current_tracker->in_parent_dirty_list = false;
439
440
if (previous_tracker != nullptr) {
441
previous_tracker->next_shared = current_tracker->next_shared;
442
} else {
443
resource_tracker->parent->dirty_shared_list = current_tracker->next_shared;
444
}
445
446
current_tracker = current_tracker->next_shared;
447
} else {
448
if (initialized_dirty_rect) {
449
resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(current_tracker->texture_slice_or_dirty_rect);
450
} else {
451
resource_tracker->parent->texture_slice_or_dirty_rect = current_tracker->texture_slice_or_dirty_rect;
452
initialized_dirty_rect = true;
453
}
454
455
previous_tracker = current_tracker;
456
current_tracker = current_tracker->next_shared;
457
}
458
}
459
}
460
} else {
461
if (resource_tracker->parent->dirty_shared_list != nullptr && resource_tracker->parent->texture_slice_or_dirty_rect.intersects(resource_tracker->texture_slice_or_dirty_rect)) {
462
// There's an intersection with the current dirty area of the parent and the slice. We must verify if the intersection is against a slice
463
// that was used in this command or not. Any slice we can find that wasn't used by this command must be reverted to the layout of the parent.
464
ResourceTracker *previous_tracker = nullptr;
465
ResourceTracker *current_tracker = resource_tracker->parent->dirty_shared_list;
466
bool initialized_dirty_rect = false;
467
while (current_tracker != nullptr) {
468
current_tracker->reset_if_outdated(tracking_frame);
469
470
if (current_tracker->texture_slice_or_dirty_rect.intersects(resource_tracker->texture_slice_or_dirty_rect)) {
471
if (current_tracker->command_frame == tracking_frame && current_tracker->texture_slice_command_index == p_command_index) {
472
ERR_FAIL_MSG("Texture slices that overlap can't be used in the same command.");
473
} else {
474
// Delete the slice from the dirty list and revert it to the usage of the parent.
475
if (current_tracker->texture_driver_id.id != 0) {
476
_add_texture_barrier_to_command(current_tracker->texture_driver_id, current_tracker->usage_access, new_usage_access, current_tracker->usage, resource_tracker->parent->usage, current_tracker->texture_subresources, command_normalization_barriers, r_command->normalization_barrier_index, r_command->normalization_barrier_count);
477
478
// Merge the area of the slice with the current tracking area of the command and indicate it's a write usage as well.
479
search_tracker_rect = search_tracker_rect.merge(current_tracker->texture_slice_or_dirty_rect);
480
write_usage = true;
481
}
482
483
current_tracker->in_parent_dirty_list = false;
484
485
if (previous_tracker != nullptr) {
486
previous_tracker->next_shared = current_tracker->next_shared;
487
} else {
488
resource_tracker->parent->dirty_shared_list = current_tracker->next_shared;
489
}
490
491
current_tracker = current_tracker->next_shared;
492
}
493
} else {
494
// Recalculate the dirty rect of the parent so the deleted slices are excluded.
495
if (initialized_dirty_rect) {
496
resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(current_tracker->texture_slice_or_dirty_rect);
497
} else {
498
resource_tracker->parent->texture_slice_or_dirty_rect = current_tracker->texture_slice_or_dirty_rect;
499
initialized_dirty_rect = true;
500
}
501
502
previous_tracker = current_tracker;
503
current_tracker = current_tracker->next_shared;
504
}
505
}
506
}
507
508
// If it wasn't in the list, assume the usage is the same as the parent. Consider the parent's current usage access as its own.
509
resource_tracker->usage = resource_tracker->parent->usage;
510
resource_tracker->usage_access = resource_tracker->parent->usage_access;
511
512
if (resource_tracker->usage != new_resource_usage) {
513
// Insert to the dirty list if the requested usage is different.
514
resource_tracker->next_shared = resource_tracker->parent->dirty_shared_list;
515
resource_tracker->parent->dirty_shared_list = resource_tracker;
516
resource_tracker->in_parent_dirty_list = true;
517
if (resource_tracker->parent->dirty_shared_list != nullptr) {
518
resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(resource_tracker->texture_slice_or_dirty_rect);
519
} else {
520
resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->texture_slice_or_dirty_rect;
521
}
522
}
523
}
524
} else {
525
ResourceTracker *current_tracker = resource_tracker->dirty_shared_list;
526
if (current_tracker != nullptr) {
527
// Consider the usage as write if we must transition any of the slices.
528
write_usage = true;
529
}
530
531
while (current_tracker != nullptr) {
532
current_tracker->reset_if_outdated(tracking_frame);
533
534
if (current_tracker->texture_driver_id.id != 0) {
535
// Transition all slices to the layout of the parent resource.
536
_add_texture_barrier_to_command(current_tracker->texture_driver_id, current_tracker->usage_access, new_usage_access, current_tracker->usage, resource_tracker->usage, current_tracker->texture_subresources, command_normalization_barriers, r_command->normalization_barrier_index, r_command->normalization_barrier_count);
537
}
538
539
current_tracker->in_parent_dirty_list = false;
540
current_tracker = current_tracker->next_shared;
541
}
542
543
resource_tracker->dirty_shared_list = nullptr;
544
}
545
546
// Use the resource's parent tracker directly for all search operations.
547
bool resource_has_parent = resource_tracker->parent != nullptr;
548
ResourceTracker *search_tracker = resource_has_parent ? resource_tracker->parent : resource_tracker;
549
bool different_usage = resource_tracker->usage != new_resource_usage;
550
bool write_usage_after_write = (write_usage && search_tracker->write_command_or_list_index >= 0);
551
if (different_usage || write_usage_after_write) {
552
// A barrier must be pushed if the usage is different of it's a write usage and there was already a command that wrote to this resource previously.
553
if (resource_tracker->texture_driver_id.id != 0) {
554
if (resource_tracker->usage_access.is_empty()) {
555
// FIXME: If the tracker does not know the previous type of usage, assume the generic memory write one.
556
// Tracking access bits across texture slices can be tricky, so this failsafe can be removed once that's improved.
557
resource_tracker->usage_access = RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;
558
}
559
560
_add_texture_barrier_to_command(resource_tracker->texture_driver_id, resource_tracker->usage_access, new_usage_access, resource_tracker->usage, new_resource_usage, resource_tracker->texture_subresources, command_transition_barriers, r_command->transition_barrier_index, r_command->transition_barrier_count);
561
} else if (resource_tracker->buffer_driver_id.id != 0) {
562
#if USE_BUFFER_BARRIERS
563
_add_buffer_barrier_to_command(resource_tracker->buffer_driver_id, resource_tracker->usage_access, new_usage_access, r_command->buffer_barrier_index, r_command->buffer_barrier_count);
564
#endif
565
// Memory barriers are pushed regardless of buffer barriers being used or not.
566
r_command->memory_barrier.src_access = r_command->memory_barrier.src_access | resource_tracker->usage_access;
567
r_command->memory_barrier.dst_access = r_command->memory_barrier.dst_access | new_usage_access;
568
} else {
569
DEV_ASSERT(false && "Resource tracker does not contain a valid buffer or texture ID.");
570
}
571
}
572
573
// Always update the access of the tracker according to the latest usage.
574
resource_tracker->usage_access = new_usage_access;
575
576
// Always accumulate the stages of the tracker with the commands that use it.
577
search_tracker->current_frame_stages = search_tracker->current_frame_stages | r_command->self_stages;
578
579
if (!search_tracker->previous_frame_stages.is_empty()) {
580
// Add to the command the stages the tracker was used on in the previous frame.
581
r_command->previous_stages = r_command->previous_stages | search_tracker->previous_frame_stages;
582
search_tracker->previous_frame_stages.clear();
583
}
584
585
if (different_usage) {
586
// Even if the usage of the resource isn't a write usage explicitly, a different usage implies a transition and it should therefore be considered a write.
587
// In the case of buffers however, this is not exactly necessary if the driver does not consider different buffer usages as different states.
588
write_usage = write_usage || bool(resource_tracker->texture_driver_id) || driver_buffers_require_transitions;
589
resource_tracker->usage = new_resource_usage;
590
}
591
592
bool write_usage_has_partial_coverage = !different_usage && _check_command_partial_coverage(resource_tracker, p_command_index);
593
if (search_tracker->write_command_or_list_index >= 0) {
594
if (search_tracker->write_command_list_enabled) {
595
// Make this command adjacent to any commands that wrote to this resource and intersect with the slice if it applies.
596
// For buffers or textures that never use slices, this list will only be one element long at most.
597
int32_t previous_write_list_index = -1;
598
int32_t write_list_index = search_tracker->write_command_or_list_index;
599
while (write_list_index >= 0) {
600
const RecordedSliceListNode &write_list_node = write_slice_list_nodes[write_list_index];
601
if (!resource_has_parent || search_tracker_rect.intersects(write_list_node.subresources)) {
602
if (write_list_node.command_index == p_command_index) {
603
ERR_FAIL_COND_MSG(!resource_has_parent, "Command can't have itself as a dependency.");
604
} else if (!write_list_node.partial_coverage || _check_command_intersection(resource_tracker, write_list_node.command_index, p_command_index)) {
605
_check_discardable_attachment_dependency(search_tracker, write_list_node.command_index, p_command_index);
606
607
// Command is dependent on this command. Add this command to the adjacency list of the write command.
608
_add_adjacent_command(write_list_node.command_index, p_command_index, r_command);
609
610
if (resource_has_parent && write_usage && search_tracker_rect.encloses(write_list_node.subresources) && !write_usage_has_partial_coverage) {
611
// Eliminate redundant writes from the list.
612
if (previous_write_list_index >= 0) {
613
RecordedSliceListNode &previous_list_node = write_slice_list_nodes[previous_write_list_index];
614
previous_list_node.next_list_index = write_list_node.next_list_index;
615
} else {
616
search_tracker->write_command_or_list_index = write_list_node.next_list_index;
617
}
618
619
write_list_index = write_list_node.next_list_index;
620
continue;
621
}
622
}
623
}
624
625
previous_write_list_index = write_list_index;
626
write_list_index = write_list_node.next_list_index;
627
}
628
} else {
629
// The index is just the latest command index that wrote to the resource.
630
if (search_tracker->write_command_or_list_index == p_command_index) {
631
ERR_FAIL_MSG("Command can't have itself as a dependency.");
632
} else {
633
_check_discardable_attachment_dependency(search_tracker, search_tracker->write_command_or_list_index, p_command_index);
634
_add_adjacent_command(search_tracker->write_command_or_list_index, p_command_index, r_command);
635
}
636
}
637
}
638
639
if (write_usage) {
640
bool use_write_list = resource_has_parent || write_usage_has_partial_coverage;
641
if (use_write_list) {
642
if (!search_tracker->write_command_list_enabled && search_tracker->write_command_or_list_index >= 0) {
643
// Write command list was not being used but there was a write command recorded. Add a new node with the entire parent resource's subresources and the recorded command index to the list.
644
const RDD::TextureSubresourceRange &tracker_subresources = search_tracker->texture_subresources;
645
Rect2i tracker_rect(tracker_subresources.base_mipmap, tracker_subresources.base_layer, tracker_subresources.mipmap_count, tracker_subresources.layer_count);
646
search_tracker->write_command_or_list_index = _add_to_write_list(search_tracker->write_command_or_list_index, tracker_rect, -1, false);
647
}
648
649
search_tracker->write_command_or_list_index = _add_to_write_list(p_command_index, search_tracker_rect, search_tracker->write_command_or_list_index, write_usage_has_partial_coverage);
650
search_tracker->write_command_list_enabled = true;
651
} else {
652
search_tracker->write_command_or_list_index = p_command_index;
653
search_tracker->write_command_list_enabled = false;
654
}
655
656
// We add this command to the adjacency list of all commands that were reading from the entire resource.
657
int32_t read_full_command_list_index = search_tracker->read_full_command_list_index;
658
while (read_full_command_list_index >= 0) {
659
int32_t read_full_command_index = command_list_nodes[read_full_command_list_index].command_index;
660
int32_t read_full_next_index = command_list_nodes[read_full_command_list_index].next_list_index;
661
if (read_full_command_index == p_command_index) {
662
if (!resource_has_parent) {
663
// Only slices are allowed to be in different usages in the same command as they are guaranteed to have no overlap in the same command.
664
ERR_FAIL_MSG("Command can't have itself as a dependency.");
665
}
666
} else {
667
// Add this command to the adjacency list of each command that was reading this resource.
668
_add_adjacent_command(read_full_command_index, p_command_index, r_command);
669
}
670
671
read_full_command_list_index = read_full_next_index;
672
}
673
674
if (!use_write_list) {
675
// Clear the full list if this resource is not a slice.
676
search_tracker->read_full_command_list_index = -1;
677
}
678
679
// We add this command to the adjacency list of all commands that were reading from resource slices.
680
int32_t previous_slice_command_list_index = -1;
681
int32_t read_slice_command_list_index = search_tracker->read_slice_command_list_index;
682
while (read_slice_command_list_index >= 0) {
683
const RecordedSliceListNode &read_list_node = read_slice_list_nodes[read_slice_command_list_index];
684
if (!use_write_list || search_tracker_rect.encloses(read_list_node.subresources)) {
685
if (previous_slice_command_list_index >= 0) {
686
// Erase this element and connect the previous one to the next element.
687
read_slice_list_nodes[previous_slice_command_list_index].next_list_index = read_list_node.next_list_index;
688
} else {
689
// Erase this element from the head of the list.
690
DEV_ASSERT(search_tracker->read_slice_command_list_index == read_slice_command_list_index);
691
search_tracker->read_slice_command_list_index = read_list_node.next_list_index;
692
}
693
694
// Advance to the next element.
695
read_slice_command_list_index = read_list_node.next_list_index;
696
} else {
697
previous_slice_command_list_index = read_slice_command_list_index;
698
read_slice_command_list_index = read_list_node.next_list_index;
699
}
700
701
if (!resource_has_parent || search_tracker_rect.intersects(read_list_node.subresources)) {
702
// Add this command to the adjacency list of each command that was reading this resource.
703
// We only add the dependency if there's an intersection between slices or this resource isn't a slice.
704
_add_adjacent_command(read_list_node.command_index, p_command_index, r_command);
705
}
706
}
707
} else if (resource_has_parent) {
708
// We add a read dependency to the tracker to indicate this command reads from the resource slice.
709
search_tracker->read_slice_command_list_index = _add_to_slice_read_list(p_command_index, resource_tracker_rect, search_tracker->read_slice_command_list_index);
710
} else {
711
// We add a read dependency to the tracker to indicate this command reads from the entire resource.
712
search_tracker->read_full_command_list_index = _add_to_command_list(p_command_index, search_tracker->read_full_command_list_index);
713
}
714
}
715
}
716
717
void RenderingDeviceGraph::_add_texture_barrier_to_command(RDD::TextureID p_texture_id, BitField<RDD::BarrierAccessBits> p_src_access, BitField<RDD::BarrierAccessBits> p_dst_access, ResourceUsage p_prev_usage, ResourceUsage p_next_usage, RDD::TextureSubresourceRange p_subresources, LocalVector<RDD::TextureBarrier> &r_barrier_vector, int32_t &r_barrier_index, int32_t &r_barrier_count) {
718
if (!driver_honors_barriers) {
719
return;
720
}
721
722
if (r_barrier_index < 0) {
723
r_barrier_index = r_barrier_vector.size();
724
}
725
726
RDD::TextureBarrier texture_barrier;
727
texture_barrier.texture = p_texture_id;
728
texture_barrier.src_access = p_src_access;
729
texture_barrier.dst_access = p_dst_access;
730
texture_barrier.prev_layout = _usage_to_image_layout(p_prev_usage);
731
texture_barrier.next_layout = _usage_to_image_layout(p_next_usage);
732
texture_barrier.subresources = p_subresources;
733
r_barrier_vector.push_back(texture_barrier);
734
r_barrier_count++;
735
}
736
737
#if USE_BUFFER_BARRIERS
738
void RenderingDeviceGraph::_add_buffer_barrier_to_command(RDD::BufferID p_buffer_id, BitField<RDD::BarrierAccessBits> p_src_access, BitField<RDD::BarrierAccessBits> p_dst_access, int32_t &r_barrier_index, int32_t &r_barrier_count) {
739
if (!driver_honors_barriers) {
740
return;
741
}
742
743
if (r_barrier_index < 0) {
744
r_barrier_index = command_buffer_barriers.size();
745
}
746
747
RDD::BufferBarrier buffer_barrier;
748
buffer_barrier.buffer = p_buffer_id;
749
buffer_barrier.src_access = p_src_access;
750
buffer_barrier.dst_access = p_dst_access;
751
buffer_barrier.offset = 0;
752
buffer_barrier.size = RDD::BUFFER_WHOLE_SIZE;
753
command_buffer_barriers.push_back(buffer_barrier);
754
r_barrier_count++;
755
}
756
#endif
757
758
void RenderingDeviceGraph::_run_compute_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
759
uint32_t instruction_data_cursor = 0;
760
while (instruction_data_cursor < p_instruction_data_size) {
761
DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);
762
763
const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);
764
switch (instruction->type) {
765
case ComputeListInstruction::TYPE_BIND_PIPELINE: {
766
const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);
767
driver->command_bind_compute_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);
768
instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);
769
} break;
770
case ComputeListInstruction::TYPE_BIND_UNIFORM_SETS: {
771
const ComputeListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const ComputeListBindUniformSetsInstruction *>(instruction);
772
driver->command_bind_compute_uniform_sets(p_command_buffer, VectorView<RDD::UniformSetID>(bind_uniform_sets_instruction->uniform_set_ids(), bind_uniform_sets_instruction->set_count), bind_uniform_sets_instruction->shader, bind_uniform_sets_instruction->first_set_index, bind_uniform_sets_instruction->set_count);
773
instruction_data_cursor += sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;
774
} break;
775
case ComputeListInstruction::TYPE_DISPATCH: {
776
const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);
777
driver->command_compute_dispatch(p_command_buffer, dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);
778
instruction_data_cursor += sizeof(ComputeListDispatchInstruction);
779
} break;
780
case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {
781
const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);
782
driver->command_compute_dispatch_indirect(p_command_buffer, dispatch_indirect_instruction->buffer, dispatch_indirect_instruction->offset);
783
instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);
784
} break;
785
case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {
786
const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);
787
const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));
788
driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);
789
instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);
790
instruction_data_cursor += set_push_constant_instruction->size;
791
} break;
792
case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
793
const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);
794
driver->command_uniform_set_prepare_for_use(p_command_buffer, uniform_set_prepare_for_use_instruction->uniform_set, uniform_set_prepare_for_use_instruction->shader, uniform_set_prepare_for_use_instruction->set_index);
795
instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);
796
} break;
797
default:
798
DEV_ASSERT(false && "Unknown compute list instruction type.");
799
return;
800
}
801
802
instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);
803
}
804
}
805
806
void RenderingDeviceGraph::_get_draw_list_render_pass_and_framebuffer(const RecordedDrawListCommand *p_draw_list_command, RDD::RenderPassID &r_render_pass, RDD::FramebufferID &r_framebuffer) {
807
DEV_ASSERT(p_draw_list_command->trackers_count <= 21 && "Max number of attachments that can be encoded into the key.");
808
809
// Build a unique key from the load and store ops for each attachment.
810
const RDD::AttachmentLoadOp *load_ops = p_draw_list_command->load_ops();
811
const RDD::AttachmentStoreOp *store_ops = p_draw_list_command->store_ops();
812
uint64_t key = 0;
813
for (uint32_t i = 0; i < p_draw_list_command->trackers_count; i++) {
814
key |= uint64_t(load_ops[i]) << (i * 3);
815
key |= uint64_t(store_ops[i]) << (i * 3 + 2);
816
}
817
818
// Check the storage map if the render pass and the framebuffer needs to be created.
819
FramebufferCache *framebuffer_cache = p_draw_list_command->framebuffer_cache;
820
HashMap<uint64_t, FramebufferStorage>::Iterator it = framebuffer_cache->storage_map.find(key);
821
if (it == framebuffer_cache->storage_map.end()) {
822
FramebufferStorage storage;
823
VectorView<RDD::AttachmentLoadOp> load_ops_view(load_ops, p_draw_list_command->trackers_count);
824
VectorView<RDD::AttachmentStoreOp> store_ops_view(store_ops, p_draw_list_command->trackers_count);
825
storage.render_pass = render_pass_creation_function(driver, load_ops_view, store_ops_view, framebuffer_cache->render_pass_creation_user_data);
826
ERR_FAIL_COND(!storage.render_pass);
827
828
storage.framebuffer = driver->framebuffer_create(storage.render_pass, framebuffer_cache->textures, framebuffer_cache->width, framebuffer_cache->height);
829
ERR_FAIL_COND(!storage.framebuffer);
830
831
it = framebuffer_cache->storage_map.insert(key, storage);
832
}
833
834
r_render_pass = it->value.render_pass;
835
r_framebuffer = it->value.framebuffer;
836
}
837
838
void RenderingDeviceGraph::_run_draw_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
839
uint32_t instruction_data_cursor = 0;
840
while (instruction_data_cursor < p_instruction_data_size) {
841
DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);
842
843
const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);
844
switch (instruction->type) {
845
case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {
846
const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);
847
driver->command_render_bind_index_buffer(p_command_buffer, bind_index_buffer_instruction->buffer, bind_index_buffer_instruction->format, bind_index_buffer_instruction->offset);
848
instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);
849
} break;
850
case DrawListInstruction::TYPE_BIND_PIPELINE: {
851
const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);
852
driver->command_bind_render_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);
853
instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);
854
} break;
855
case DrawListInstruction::TYPE_BIND_UNIFORM_SETS: {
856
const DrawListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const DrawListBindUniformSetsInstruction *>(instruction);
857
driver->command_bind_render_uniform_sets(p_command_buffer, VectorView<RDD::UniformSetID>(bind_uniform_sets_instruction->uniform_set_ids(), bind_uniform_sets_instruction->set_count), bind_uniform_sets_instruction->shader, bind_uniform_sets_instruction->first_set_index, bind_uniform_sets_instruction->set_count);
858
instruction_data_cursor += sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;
859
} break;
860
case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {
861
const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);
862
driver->command_render_bind_vertex_buffers(p_command_buffer, bind_vertex_buffers_instruction->vertex_buffers_count, bind_vertex_buffers_instruction->vertex_buffers(), bind_vertex_buffers_instruction->vertex_buffer_offsets());
863
instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);
864
instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;
865
instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;
866
} break;
867
case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {
868
const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);
869
const VectorView attachments_clear_view(clear_attachments_instruction->attachments_clear(), clear_attachments_instruction->attachments_clear_count);
870
const VectorView attachments_clear_rect_view(clear_attachments_instruction->attachments_clear_rect(), clear_attachments_instruction->attachments_clear_rect_count);
871
driver->command_render_clear_attachments(p_command_buffer, attachments_clear_view, attachments_clear_rect_view);
872
instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);
873
instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;
874
instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;
875
} break;
876
case DrawListInstruction::TYPE_DRAW: {
877
const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);
878
driver->command_render_draw(p_command_buffer, draw_instruction->vertex_count, draw_instruction->instance_count, 0, 0);
879
instruction_data_cursor += sizeof(DrawListDrawInstruction);
880
} break;
881
case DrawListInstruction::TYPE_DRAW_INDEXED: {
882
const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);
883
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);
884
instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);
885
} break;
886
case DrawListInstruction::TYPE_DRAW_INDIRECT: {
887
const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);
888
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);
889
instruction_data_cursor += sizeof(DrawListDrawIndirectInstruction);
890
} break;
891
case DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT: {
892
const DrawListDrawIndexedIndirectInstruction *draw_indexed_indirect_instruction = reinterpret_cast<const DrawListDrawIndexedIndirectInstruction *>(instruction);
893
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);
894
instruction_data_cursor += sizeof(DrawListDrawIndexedIndirectInstruction);
895
} break;
896
case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {
897
const DrawListExecuteCommandsInstruction *execute_commands_instruction = reinterpret_cast<const DrawListExecuteCommandsInstruction *>(instruction);
898
driver->command_buffer_execute_secondary(p_command_buffer, execute_commands_instruction->command_buffer);
899
instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);
900
} break;
901
case DrawListInstruction::TYPE_NEXT_SUBPASS: {
902
const DrawListNextSubpassInstruction *next_subpass_instruction = reinterpret_cast<const DrawListNextSubpassInstruction *>(instruction);
903
driver->command_next_render_subpass(p_command_buffer, next_subpass_instruction->command_buffer_type);
904
instruction_data_cursor += sizeof(DrawListNextSubpassInstruction);
905
} break;
906
case DrawListInstruction::TYPE_SET_BLEND_CONSTANTS: {
907
const DrawListSetBlendConstantsInstruction *set_blend_constants_instruction = reinterpret_cast<const DrawListSetBlendConstantsInstruction *>(instruction);
908
driver->command_render_set_blend_constants(p_command_buffer, set_blend_constants_instruction->color);
909
instruction_data_cursor += sizeof(DrawListSetBlendConstantsInstruction);
910
} break;
911
case DrawListInstruction::TYPE_SET_LINE_WIDTH: {
912
const DrawListSetLineWidthInstruction *set_line_width_instruction = reinterpret_cast<const DrawListSetLineWidthInstruction *>(instruction);
913
driver->command_render_set_line_width(p_command_buffer, set_line_width_instruction->width);
914
instruction_data_cursor += sizeof(DrawListSetLineWidthInstruction);
915
} break;
916
case DrawListInstruction::TYPE_SET_PUSH_CONSTANT: {
917
const DrawListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const DrawListSetPushConstantInstruction *>(instruction);
918
const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));
919
driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);
920
instruction_data_cursor += sizeof(DrawListSetPushConstantInstruction);
921
instruction_data_cursor += set_push_constant_instruction->size;
922
} break;
923
case DrawListInstruction::TYPE_SET_SCISSOR: {
924
const DrawListSetScissorInstruction *set_scissor_instruction = reinterpret_cast<const DrawListSetScissorInstruction *>(instruction);
925
driver->command_render_set_scissor(p_command_buffer, set_scissor_instruction->rect);
926
instruction_data_cursor += sizeof(DrawListSetScissorInstruction);
927
} break;
928
case DrawListInstruction::TYPE_SET_VIEWPORT: {
929
const DrawListSetViewportInstruction *set_viewport_instruction = reinterpret_cast<const DrawListSetViewportInstruction *>(instruction);
930
driver->command_render_set_viewport(p_command_buffer, set_viewport_instruction->rect);
931
instruction_data_cursor += sizeof(DrawListSetViewportInstruction);
932
} break;
933
case DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
934
const DrawListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const DrawListUniformSetPrepareForUseInstruction *>(instruction);
935
driver->command_uniform_set_prepare_for_use(p_command_buffer, uniform_set_prepare_for_use_instruction->uniform_set, uniform_set_prepare_for_use_instruction->shader, uniform_set_prepare_for_use_instruction->set_index);
936
instruction_data_cursor += sizeof(DrawListUniformSetPrepareForUseInstruction);
937
} break;
938
default:
939
DEV_ASSERT(false && "Unknown draw list instruction type.");
940
return;
941
}
942
943
instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);
944
}
945
}
946
947
void RenderingDeviceGraph::_add_draw_list_begin(FramebufferCache *p_framebuffer_cache, RDD::RenderPassID p_render_pass, RDD::FramebufferID p_framebuffer, Rect2i p_region, VectorView<AttachmentOperation> p_attachment_operations, VectorView<RDD::RenderPassClearValue> p_attachment_clear_values, BitField<RDD::PipelineStageBits> p_stages, uint32_t p_breadcrumb, bool p_split_cmd_buffer) {
948
DEV_ASSERT(p_attachment_operations.size() == p_attachment_clear_values.size());
949
950
draw_instruction_list.clear();
951
draw_instruction_list.index++;
952
draw_instruction_list.framebuffer_cache = p_framebuffer_cache;
953
draw_instruction_list.render_pass = p_render_pass;
954
draw_instruction_list.framebuffer = p_framebuffer;
955
draw_instruction_list.region = p_region;
956
draw_instruction_list.stages = p_stages;
957
draw_instruction_list.attachment_operations.resize(p_attachment_operations.size());
958
draw_instruction_list.attachment_clear_values.resize(p_attachment_clear_values.size());
959
960
for (uint32_t i = 0; i < p_attachment_operations.size(); i++) {
961
draw_instruction_list.attachment_operations[i] = p_attachment_operations[i];
962
draw_instruction_list.attachment_clear_values[i] = p_attachment_clear_values[i];
963
}
964
965
draw_instruction_list.split_cmd_buffer = p_split_cmd_buffer;
966
967
#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
968
draw_instruction_list.breadcrumb = p_breadcrumb;
969
#endif
970
}
971
972
void RenderingDeviceGraph::_run_secondary_command_buffer_task(const SecondaryCommandBuffer *p_secondary) {
973
driver->command_buffer_begin_secondary(p_secondary->command_buffer, p_secondary->render_pass, 0, p_secondary->framebuffer);
974
_run_draw_list_command(p_secondary->command_buffer, p_secondary->instruction_data.ptr(), p_secondary->instruction_data.size());
975
driver->command_buffer_end(p_secondary->command_buffer);
976
}
977
978
void RenderingDeviceGraph::_wait_for_secondary_command_buffer_tasks() {
979
for (uint32_t i = 0; i < frames[frame].secondary_command_buffers_used; i++) {
980
WorkerThreadPool::TaskID &task = frames[frame].secondary_command_buffers[i].task;
981
if (task != WorkerThreadPool::INVALID_TASK_ID) {
982
WorkerThreadPool::get_singleton()->wait_for_task_completion(task);
983
task = WorkerThreadPool::INVALID_TASK_ID;
984
}
985
}
986
}
987
988
void RenderingDeviceGraph::_run_render_commands(int32_t p_level, const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool, int32_t &r_current_label_index, int32_t &r_current_label_level) {
989
for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
990
const uint32_t command_index = p_sorted_commands[i].index;
991
const uint32_t command_data_offset = command_data_offsets[command_index];
992
const RecordedCommand *command = reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);
993
_run_label_command_change(r_command_buffer, command->label_index, p_level, false, true, &p_sorted_commands[i], p_sorted_commands_count - i, r_current_label_index, r_current_label_level);
994
995
switch (command->type) {
996
case RecordedCommand::TYPE_BUFFER_CLEAR: {
997
const RecordedBufferClearCommand *buffer_clear_command = reinterpret_cast<const RecordedBufferClearCommand *>(command);
998
driver->command_clear_buffer(r_command_buffer, buffer_clear_command->buffer, buffer_clear_command->offset, buffer_clear_command->size);
999
} break;
1000
case RecordedCommand::TYPE_BUFFER_COPY: {
1001
const RecordedBufferCopyCommand *buffer_copy_command = reinterpret_cast<const RecordedBufferCopyCommand *>(command);
1002
driver->command_copy_buffer(r_command_buffer, buffer_copy_command->source, buffer_copy_command->destination, buffer_copy_command->region);
1003
} break;
1004
case RecordedCommand::TYPE_BUFFER_GET_DATA: {
1005
const RecordedBufferGetDataCommand *buffer_get_data_command = reinterpret_cast<const RecordedBufferGetDataCommand *>(command);
1006
driver->command_copy_buffer(r_command_buffer, buffer_get_data_command->source, buffer_get_data_command->destination, buffer_get_data_command->region);
1007
} break;
1008
case RecordedCommand::TYPE_BUFFER_UPDATE: {
1009
const RecordedBufferUpdateCommand *buffer_update_command = reinterpret_cast<const RecordedBufferUpdateCommand *>(command);
1010
const RecordedBufferCopy *command_buffer_copies = buffer_update_command->buffer_copies();
1011
for (uint32_t j = 0; j < buffer_update_command->buffer_copies_count; j++) {
1012
driver->command_copy_buffer(r_command_buffer, command_buffer_copies[j].source, buffer_update_command->destination, command_buffer_copies[j].region);
1013
}
1014
} break;
1015
case RecordedCommand::TYPE_DRIVER_CALLBACK: {
1016
const RecordedDriverCallbackCommand *driver_callback_command = reinterpret_cast<const RecordedDriverCallbackCommand *>(command);
1017
driver_callback_command->callback(driver, r_command_buffer, driver_callback_command->userdata);
1018
} break;
1019
case RecordedCommand::TYPE_COMPUTE_LIST: {
1020
if (device.workarounds.avoid_compute_after_draw && workarounds_state.draw_list_found) {
1021
// Avoid compute after draw workaround. Refer to the comment that enables this in the Vulkan driver for more information.
1022
workarounds_state.draw_list_found = false;
1023
1024
// Create or reuse a command buffer and finish recording the current one.
1025
driver->command_buffer_end(r_command_buffer);
1026
1027
while (r_command_buffer_pool.buffers_used >= r_command_buffer_pool.buffers.size()) {
1028
RDD::CommandBufferID command_buffer = driver->command_buffer_create(r_command_buffer_pool.pool);
1029
RDD::SemaphoreID command_semaphore = driver->semaphore_create();
1030
r_command_buffer_pool.buffers.push_back(command_buffer);
1031
r_command_buffer_pool.semaphores.push_back(command_semaphore);
1032
}
1033
1034
// Start recording on the next usable command buffer from the pool.
1035
uint32_t command_buffer_index = r_command_buffer_pool.buffers_used++;
1036
r_command_buffer = r_command_buffer_pool.buffers[command_buffer_index];
1037
driver->command_buffer_begin(r_command_buffer);
1038
}
1039
1040
const RecordedComputeListCommand *compute_list_command = reinterpret_cast<const RecordedComputeListCommand *>(command);
1041
_run_compute_list_command(r_command_buffer, compute_list_command->instruction_data(), compute_list_command->instruction_data_size);
1042
} break;
1043
case RecordedCommand::TYPE_DRAW_LIST: {
1044
if (device.workarounds.avoid_compute_after_draw) {
1045
// Indicate that a draw list was encountered for the workaround.
1046
workarounds_state.draw_list_found = true;
1047
}
1048
1049
const RecordedDrawListCommand *draw_list_command = reinterpret_cast<const RecordedDrawListCommand *>(command);
1050
1051
if (draw_list_command->split_cmd_buffer) {
1052
// Create or reuse a command buffer and finish recording the current one.
1053
driver->command_buffer_end(r_command_buffer);
1054
1055
while (r_command_buffer_pool.buffers_used >= r_command_buffer_pool.buffers.size()) {
1056
RDD::CommandBufferID command_buffer = driver->command_buffer_create(r_command_buffer_pool.pool);
1057
RDD::SemaphoreID command_semaphore = driver->semaphore_create();
1058
r_command_buffer_pool.buffers.push_back(command_buffer);
1059
r_command_buffer_pool.semaphores.push_back(command_semaphore);
1060
}
1061
1062
// Start recording on the next usable command buffer from the pool.
1063
uint32_t command_buffer_index = r_command_buffer_pool.buffers_used++;
1064
r_command_buffer = r_command_buffer_pool.buffers[command_buffer_index];
1065
driver->command_buffer_begin(r_command_buffer);
1066
}
1067
1068
const VectorView clear_values(draw_list_command->clear_values(), draw_list_command->clear_values_count);
1069
#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
1070
driver->command_insert_breadcrumb(r_command_buffer, draw_list_command->breadcrumb);
1071
#endif
1072
RDD::RenderPassID render_pass;
1073
RDD::FramebufferID framebuffer;
1074
if (draw_list_command->framebuffer_cache != nullptr) {
1075
_get_draw_list_render_pass_and_framebuffer(draw_list_command, render_pass, framebuffer);
1076
} else {
1077
render_pass = draw_list_command->render_pass;
1078
framebuffer = draw_list_command->framebuffer;
1079
}
1080
1081
if (framebuffer && render_pass) {
1082
driver->command_begin_render_pass(r_command_buffer, render_pass, framebuffer, draw_list_command->command_buffer_type, draw_list_command->region, clear_values);
1083
_run_draw_list_command(r_command_buffer, draw_list_command->instruction_data(), draw_list_command->instruction_data_size);
1084
driver->command_end_render_pass(r_command_buffer);
1085
}
1086
} break;
1087
case RecordedCommand::TYPE_TEXTURE_CLEAR: {
1088
const RecordedTextureClearCommand *texture_clear_command = reinterpret_cast<const RecordedTextureClearCommand *>(command);
1089
driver->command_clear_color_texture(r_command_buffer, texture_clear_command->texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, texture_clear_command->color, texture_clear_command->range);
1090
} break;
1091
case RecordedCommand::TYPE_TEXTURE_COPY: {
1092
const RecordedTextureCopyCommand *texture_copy_command = reinterpret_cast<const RecordedTextureCopyCommand *>(command);
1093
const VectorView<RDD::TextureCopyRegion> command_texture_copy_regions_view(texture_copy_command->texture_copy_regions(), texture_copy_command->texture_copy_regions_count);
1094
driver->command_copy_texture(r_command_buffer, texture_copy_command->from_texture, RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL, texture_copy_command->to_texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, command_texture_copy_regions_view);
1095
} break;
1096
case RecordedCommand::TYPE_TEXTURE_GET_DATA: {
1097
const RecordedTextureGetDataCommand *texture_get_data_command = reinterpret_cast<const RecordedTextureGetDataCommand *>(command);
1098
const VectorView<RDD::BufferTextureCopyRegion> command_buffer_texture_copy_regions_view(texture_get_data_command->buffer_texture_copy_regions(), texture_get_data_command->buffer_texture_copy_regions_count);
1099
driver->command_copy_texture_to_buffer(r_command_buffer, texture_get_data_command->from_texture, RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL, texture_get_data_command->to_buffer, command_buffer_texture_copy_regions_view);
1100
} break;
1101
case RecordedCommand::TYPE_TEXTURE_RESOLVE: {
1102
const RecordedTextureResolveCommand *texture_resolve_command = reinterpret_cast<const RecordedTextureResolveCommand *>(command);
1103
driver->command_resolve_texture(r_command_buffer, texture_resolve_command->from_texture, RDD::TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL, texture_resolve_command->src_layer, texture_resolve_command->src_mipmap, texture_resolve_command->to_texture, RDD::TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL, texture_resolve_command->dst_layer, texture_resolve_command->dst_mipmap);
1104
} break;
1105
case RecordedCommand::TYPE_TEXTURE_UPDATE: {
1106
const RecordedTextureUpdateCommand *texture_update_command = reinterpret_cast<const RecordedTextureUpdateCommand *>(command);
1107
const RecordedBufferToTextureCopy *command_buffer_to_texture_copies = texture_update_command->buffer_to_texture_copies();
1108
for (uint32_t j = 0; j < texture_update_command->buffer_to_texture_copies_count; j++) {
1109
driver->command_copy_buffer_to_texture(r_command_buffer, command_buffer_to_texture_copies[j].from_buffer, texture_update_command->to_texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, command_buffer_to_texture_copies[j].region);
1110
}
1111
} break;
1112
case RecordedCommand::TYPE_CAPTURE_TIMESTAMP: {
1113
const RecordedCaptureTimestampCommand *texture_capture_timestamp_command = reinterpret_cast<const RecordedCaptureTimestampCommand *>(command);
1114
driver->command_timestamp_write(r_command_buffer, texture_capture_timestamp_command->pool, texture_capture_timestamp_command->index);
1115
} break;
1116
default: {
1117
DEV_ASSERT(false && "Unknown recorded command type.");
1118
return;
1119
}
1120
}
1121
}
1122
}
1123
1124
void RenderingDeviceGraph::_run_label_command_change(RDD::CommandBufferID p_command_buffer, int32_t p_new_label_index, int32_t p_new_level, bool p_ignore_previous_value, bool p_use_label_for_empty, const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, int32_t &r_current_label_index, int32_t &r_current_label_level) {
1125
if (command_label_count == 0) {
1126
// Ignore any label operations if no labels were pushed.
1127
return;
1128
}
1129
1130
if (p_ignore_previous_value || p_new_label_index != r_current_label_index || p_new_level != r_current_label_level) {
1131
if (!p_ignore_previous_value && (p_use_label_for_empty || r_current_label_index >= 0 || r_current_label_level >= 0)) {
1132
// End the current label.
1133
driver->command_end_label(p_command_buffer);
1134
}
1135
1136
String label_name;
1137
Color label_color;
1138
if (p_new_label_index >= 0) {
1139
const char *label_chars = &command_label_chars[command_label_offsets[p_new_label_index]];
1140
label_name.append_utf8(label_chars);
1141
label_color = command_label_colors[p_new_label_index];
1142
} else if (p_use_label_for_empty) {
1143
label_name = "Command graph";
1144
label_color = Color(1, 1, 1, 1);
1145
} else {
1146
return;
1147
}
1148
1149
// Add the level to the name.
1150
label_name += " (L" + itos(p_new_level) + ")";
1151
1152
if (p_sorted_commands != nullptr && p_sorted_commands_count > 0) {
1153
// Analyze the commands in the level that have the same label to detect what type of operations are performed.
1154
bool copy_commands = false;
1155
bool compute_commands = false;
1156
bool draw_commands = false;
1157
bool custom_commands = false;
1158
for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
1159
const uint32_t command_index = p_sorted_commands[i].index;
1160
const uint32_t command_data_offset = command_data_offsets[command_index];
1161
const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
1162
if (command->label_index != p_new_label_index) {
1163
break;
1164
}
1165
1166
switch (command->type) {
1167
case RecordedCommand::TYPE_BUFFER_CLEAR:
1168
case RecordedCommand::TYPE_BUFFER_COPY:
1169
case RecordedCommand::TYPE_BUFFER_GET_DATA:
1170
case RecordedCommand::TYPE_BUFFER_UPDATE:
1171
case RecordedCommand::TYPE_TEXTURE_CLEAR:
1172
case RecordedCommand::TYPE_TEXTURE_COPY:
1173
case RecordedCommand::TYPE_TEXTURE_GET_DATA:
1174
case RecordedCommand::TYPE_TEXTURE_RESOLVE:
1175
case RecordedCommand::TYPE_TEXTURE_UPDATE: {
1176
copy_commands = true;
1177
} break;
1178
case RecordedCommand::TYPE_COMPUTE_LIST: {
1179
compute_commands = true;
1180
} break;
1181
case RecordedCommand::TYPE_DRAW_LIST: {
1182
draw_commands = true;
1183
} break;
1184
case RecordedCommand::TYPE_DRIVER_CALLBACK: {
1185
custom_commands = true;
1186
} break;
1187
default: {
1188
// Ignore command.
1189
} break;
1190
}
1191
1192
if (copy_commands && compute_commands && draw_commands && custom_commands) {
1193
// There's no more command types to find.
1194
break;
1195
}
1196
}
1197
1198
if (copy_commands || compute_commands || draw_commands || custom_commands) {
1199
// Add the operations to the name.
1200
bool plus_after_copy = copy_commands && (compute_commands || draw_commands || custom_commands);
1201
bool plus_after_compute = compute_commands && (draw_commands || custom_commands);
1202
bool plus_after_draw = draw_commands && custom_commands;
1203
label_name += " (";
1204
label_name += copy_commands ? "Copy" : "";
1205
label_name += plus_after_copy ? "+" : "";
1206
label_name += compute_commands ? "Compute" : "";
1207
label_name += plus_after_compute ? "+" : "";
1208
label_name += draw_commands ? "Draw" : "";
1209
label_name += plus_after_draw ? "+" : "";
1210
label_name += custom_commands ? "Custom" : "";
1211
label_name += ")";
1212
}
1213
}
1214
1215
// Start the new label.
1216
CharString label_name_utf8 = label_name.utf8();
1217
driver->command_begin_label(p_command_buffer, label_name_utf8.get_data(), label_color);
1218
1219
r_current_label_index = p_new_label_index;
1220
r_current_label_level = p_new_level;
1221
}
1222
}
1223
1224
void RenderingDeviceGraph::_boost_priority_for_render_commands(RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, uint32_t &r_boosted_priority) {
1225
if (p_sorted_commands_count == 0) {
1226
return;
1227
}
1228
1229
const uint32_t boosted_priority_value = 0;
1230
if (r_boosted_priority > 0) {
1231
bool perform_sort = false;
1232
for (uint32_t j = 0; j < p_sorted_commands_count; j++) {
1233
if (p_sorted_commands[j].priority == r_boosted_priority) {
1234
p_sorted_commands[j].priority = boosted_priority_value;
1235
perform_sort = true;
1236
}
1237
}
1238
1239
if (perform_sort) {
1240
SortArray<RecordedCommandSort> command_sorter;
1241
command_sorter.sort(p_sorted_commands, p_sorted_commands_count);
1242
}
1243
}
1244
1245
if (p_sorted_commands[p_sorted_commands_count - 1].priority != boosted_priority_value) {
1246
r_boosted_priority = p_sorted_commands[p_sorted_commands_count - 1].priority;
1247
}
1248
}
1249
1250
void RenderingDeviceGraph::_group_barriers_for_render_commands(RDD::CommandBufferID p_command_buffer, const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, bool p_full_memory_barrier) {
1251
if (!driver_honors_barriers) {
1252
return;
1253
}
1254
1255
barrier_group.clear();
1256
barrier_group.src_stages = RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT;
1257
barrier_group.dst_stages = RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
1258
1259
for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
1260
const uint32_t command_index = p_sorted_commands[i].index;
1261
const uint32_t command_data_offset = command_data_offsets[command_index];
1262
const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
1263
1264
#if PRINT_COMMAND_RECORDING
1265
print_line(vformat("Grouping barriers for #%d", command_index));
1266
#endif
1267
1268
// Merge command's stage bits with the barrier group.
1269
barrier_group.src_stages = barrier_group.src_stages | command->previous_stages;
1270
barrier_group.dst_stages = barrier_group.dst_stages | command->next_stages;
1271
1272
// Merge command's memory barrier bits with the barrier group.
1273
barrier_group.memory_barrier.src_access = barrier_group.memory_barrier.src_access | command->memory_barrier.src_access;
1274
barrier_group.memory_barrier.dst_access = barrier_group.memory_barrier.dst_access | command->memory_barrier.dst_access;
1275
1276
// Gather texture barriers.
1277
for (int32_t j = 0; j < command->normalization_barrier_count; j++) {
1278
const RDD::TextureBarrier &recorded_barrier = command_normalization_barriers[command->normalization_barrier_index + j];
1279
barrier_group.normalization_barriers.push_back(recorded_barrier);
1280
#if PRINT_COMMAND_RECORDING
1281
print_line(vformat("Normalization Barrier #%d", barrier_group.normalization_barriers.size() - 1));
1282
#endif
1283
}
1284
1285
for (int32_t j = 0; j < command->transition_barrier_count; j++) {
1286
const RDD::TextureBarrier &recorded_barrier = command_transition_barriers[command->transition_barrier_index + j];
1287
barrier_group.transition_barriers.push_back(recorded_barrier);
1288
#if PRINT_COMMAND_RECORDING
1289
print_line(vformat("Transition Barrier #%d", barrier_group.transition_barriers.size() - 1));
1290
#endif
1291
}
1292
1293
#if USE_BUFFER_BARRIERS
1294
// Gather buffer barriers.
1295
for (int32_t j = 0; j < command->buffer_barrier_count; j++) {
1296
const RDD::BufferBarrier &recorded_barrier = command_buffer_barriers[command->buffer_barrier_index + j];
1297
barrier_group.buffer_barriers.push_back(recorded_barrier);
1298
}
1299
#endif
1300
}
1301
1302
if (p_full_memory_barrier) {
1303
barrier_group.src_stages = RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT;
1304
barrier_group.dst_stages = RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT;
1305
barrier_group.memory_barrier.src_access = RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;
1306
barrier_group.memory_barrier.dst_access = RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;
1307
}
1308
1309
const bool is_memory_barrier_empty = barrier_group.memory_barrier.src_access.is_empty() && barrier_group.memory_barrier.dst_access.is_empty();
1310
const bool are_texture_barriers_empty = barrier_group.normalization_barriers.is_empty() && barrier_group.transition_barriers.is_empty();
1311
#if USE_BUFFER_BARRIERS
1312
const bool are_buffer_barriers_empty = barrier_group.buffer_barriers.is_empty();
1313
#else
1314
const bool are_buffer_barriers_empty = true;
1315
#endif
1316
if (is_memory_barrier_empty && are_texture_barriers_empty && are_buffer_barriers_empty) {
1317
// Commands don't require synchronization.
1318
return;
1319
}
1320
1321
const VectorView<RDD::MemoryBarrier> memory_barriers = !is_memory_barrier_empty ? barrier_group.memory_barrier : VectorView<RDD::MemoryBarrier>();
1322
const VectorView<RDD::TextureBarrier> texture_barriers = barrier_group.normalization_barriers.is_empty() ? barrier_group.transition_barriers : barrier_group.normalization_barriers;
1323
#if USE_BUFFER_BARRIERS
1324
const VectorView<RDD::BufferBarrier> buffer_barriers = !are_buffer_barriers_empty ? barrier_group.buffer_barriers : VectorView<RDD::BufferBarrier>();
1325
#else
1326
const VectorView<RDD::BufferBarrier> buffer_barriers = VectorView<RDD::BufferBarrier>();
1327
#endif
1328
1329
driver->command_pipeline_barrier(p_command_buffer, barrier_group.src_stages, barrier_group.dst_stages, memory_barriers, buffer_barriers, texture_barriers);
1330
1331
bool separate_texture_barriers = !barrier_group.normalization_barriers.is_empty() && !barrier_group.transition_barriers.is_empty();
1332
if (separate_texture_barriers) {
1333
driver->command_pipeline_barrier(p_command_buffer, barrier_group.src_stages, barrier_group.dst_stages, VectorView<RDD::MemoryBarrier>(), VectorView<RDD::BufferBarrier>(), barrier_group.transition_barriers);
1334
}
1335
}
1336
1337
void RenderingDeviceGraph::_print_render_commands(const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count) {
1338
for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
1339
const uint32_t command_index = p_sorted_commands[i].index;
1340
const uint32_t command_level = p_sorted_commands[i].level;
1341
const uint32_t command_data_offset = command_data_offsets[command_index];
1342
const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
1343
switch (command->type) {
1344
case RecordedCommand::TYPE_BUFFER_CLEAR: {
1345
const RecordedBufferClearCommand *buffer_clear_command = reinterpret_cast<const RecordedBufferClearCommand *>(command);
1346
print_line(command_index, "LEVEL", command_level, "BUFFER CLEAR DESTINATION", itos(buffer_clear_command->buffer.id));
1347
} break;
1348
case RecordedCommand::TYPE_BUFFER_COPY: {
1349
const RecordedBufferCopyCommand *buffer_copy_command = reinterpret_cast<const RecordedBufferCopyCommand *>(command);
1350
print_line(command_index, "LEVEL", command_level, "BUFFER COPY SOURCE", itos(buffer_copy_command->source.id), "DESTINATION", itos(buffer_copy_command->destination.id));
1351
} break;
1352
case RecordedCommand::TYPE_BUFFER_GET_DATA: {
1353
const RecordedBufferGetDataCommand *buffer_get_data_command = reinterpret_cast<const RecordedBufferGetDataCommand *>(command);
1354
print_line(command_index, "LEVEL", command_level, "BUFFER GET DATA DESTINATION", itos(buffer_get_data_command->destination.id));
1355
} break;
1356
case RecordedCommand::TYPE_BUFFER_UPDATE: {
1357
const RecordedBufferUpdateCommand *buffer_update_command = reinterpret_cast<const RecordedBufferUpdateCommand *>(command);
1358
print_line(command_index, "LEVEL", command_level, "BUFFER UPDATE DESTINATION", itos(buffer_update_command->destination.id), "COPIES", buffer_update_command->buffer_copies_count);
1359
} break;
1360
case RecordedCommand::TYPE_DRIVER_CALLBACK: {
1361
print_line(command_index, "LEVEL", command_level, "DRIVER CALLBACK");
1362
} break;
1363
case RecordedCommand::TYPE_COMPUTE_LIST: {
1364
const RecordedComputeListCommand *compute_list_command = reinterpret_cast<const RecordedComputeListCommand *>(command);
1365
print_line(command_index, "LEVEL", command_level, "COMPUTE LIST SIZE", compute_list_command->instruction_data_size);
1366
} break;
1367
case RecordedCommand::TYPE_DRAW_LIST: {
1368
const RecordedDrawListCommand *draw_list_command = reinterpret_cast<const RecordedDrawListCommand *>(command);
1369
print_line(command_index, "LEVEL", command_level, "DRAW LIST SIZE", draw_list_command->instruction_data_size);
1370
} break;
1371
case RecordedCommand::TYPE_TEXTURE_CLEAR: {
1372
const RecordedTextureClearCommand *texture_clear_command = reinterpret_cast<const RecordedTextureClearCommand *>(command);
1373
print_line(command_index, "LEVEL", command_level, "TEXTURE CLEAR", itos(texture_clear_command->texture.id), "COLOR", texture_clear_command->color);
1374
} break;
1375
case RecordedCommand::TYPE_TEXTURE_COPY: {
1376
const RecordedTextureCopyCommand *texture_copy_command = reinterpret_cast<const RecordedTextureCopyCommand *>(command);
1377
print_line(command_index, "LEVEL", command_level, "TEXTURE COPY FROM", itos(texture_copy_command->from_texture.id), "TO", itos(texture_copy_command->to_texture.id));
1378
} break;
1379
case RecordedCommand::TYPE_TEXTURE_GET_DATA: {
1380
print_line(command_index, "LEVEL", command_level, "TEXTURE GET DATA");
1381
} break;
1382
case RecordedCommand::TYPE_TEXTURE_RESOLVE: {
1383
const RecordedTextureResolveCommand *texture_resolve_command = reinterpret_cast<const RecordedTextureResolveCommand *>(command);
1384
print_line(command_index, "LEVEL", command_level, "TEXTURE RESOLVE FROM", itos(texture_resolve_command->from_texture.id), "TO", itos(texture_resolve_command->to_texture.id));
1385
} break;
1386
case RecordedCommand::TYPE_TEXTURE_UPDATE: {
1387
const RecordedTextureUpdateCommand *texture_update_command = reinterpret_cast<const RecordedTextureUpdateCommand *>(command);
1388
print_line(command_index, "LEVEL", command_level, "TEXTURE UPDATE TO", itos(texture_update_command->to_texture.id));
1389
} break;
1390
case RecordedCommand::TYPE_CAPTURE_TIMESTAMP: {
1391
const RecordedCaptureTimestampCommand *texture_capture_timestamp_command = reinterpret_cast<const RecordedCaptureTimestampCommand *>(command);
1392
print_line(command_index, "LEVEL", command_level, "CAPTURE TIMESTAMP POOL", itos(texture_capture_timestamp_command->pool.id), "INDEX", texture_capture_timestamp_command->index);
1393
} break;
1394
default:
1395
DEV_ASSERT(false && "Unknown recorded command type.");
1396
return;
1397
}
1398
}
1399
}
1400
1401
void RenderingDeviceGraph::_print_draw_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
1402
uint32_t instruction_data_cursor = 0;
1403
while (instruction_data_cursor < p_instruction_data_size) {
1404
DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);
1405
1406
const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);
1407
switch (instruction->type) {
1408
case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {
1409
const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);
1410
print_line("\tBIND INDEX BUFFER ID", itos(bind_index_buffer_instruction->buffer.id), "FORMAT", bind_index_buffer_instruction->format, "OFFSET", bind_index_buffer_instruction->offset);
1411
instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);
1412
} break;
1413
case DrawListInstruction::TYPE_BIND_PIPELINE: {
1414
const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);
1415
print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));
1416
instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);
1417
} break;
1418
case DrawListInstruction::TYPE_BIND_UNIFORM_SETS: {
1419
const DrawListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const DrawListBindUniformSetsInstruction *>(instruction);
1420
print_line("\tBIND UNIFORM SETS COUNT", bind_uniform_sets_instruction->set_count);
1421
for (uint32_t i = 0; i < bind_uniform_sets_instruction->set_count; i++) {
1422
print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_sets_instruction->uniform_set_ids()[i].id), "START INDEX", bind_uniform_sets_instruction->first_set_index);
1423
}
1424
instruction_data_cursor += sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;
1425
} break;
1426
case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {
1427
const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);
1428
print_line("\tBIND VERTEX BUFFERS COUNT", bind_vertex_buffers_instruction->vertex_buffers_count);
1429
instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);
1430
instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;
1431
instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;
1432
} break;
1433
case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {
1434
const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);
1435
print_line("\tATTACHMENTS CLEAR COUNT", clear_attachments_instruction->attachments_clear_count, "RECT COUNT", clear_attachments_instruction->attachments_clear_rect_count);
1436
instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);
1437
instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;
1438
instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;
1439
} break;
1440
case DrawListInstruction::TYPE_DRAW: {
1441
const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);
1442
print_line("\tDRAW VERTICES", draw_instruction->vertex_count, "INSTANCES", draw_instruction->instance_count);
1443
instruction_data_cursor += sizeof(DrawListDrawInstruction);
1444
} break;
1445
case DrawListInstruction::TYPE_DRAW_INDEXED: {
1446
const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);
1447
print_line("\tDRAW INDICES", draw_indexed_instruction->index_count, "INSTANCES", draw_indexed_instruction->instance_count, "FIRST INDEX", draw_indexed_instruction->first_index);
1448
instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);
1449
} break;
1450
case DrawListInstruction::TYPE_DRAW_INDIRECT: {
1451
const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);
1452
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);
1453
instruction_data_cursor += sizeof(DrawListDrawIndirectInstruction);
1454
} break;
1455
case DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT: {
1456
const DrawListDrawIndexedIndirectInstruction *draw_indexed_indirect_instruction = reinterpret_cast<const DrawListDrawIndexedIndirectInstruction *>(instruction);
1457
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);
1458
instruction_data_cursor += sizeof(DrawListDrawIndexedIndirectInstruction);
1459
} break;
1460
case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {
1461
print_line("\tEXECUTE COMMANDS");
1462
instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);
1463
} break;
1464
case DrawListInstruction::TYPE_NEXT_SUBPASS: {
1465
print_line("\tNEXT SUBPASS");
1466
instruction_data_cursor += sizeof(DrawListNextSubpassInstruction);
1467
} break;
1468
case DrawListInstruction::TYPE_SET_BLEND_CONSTANTS: {
1469
const DrawListSetBlendConstantsInstruction *set_blend_constants_instruction = reinterpret_cast<const DrawListSetBlendConstantsInstruction *>(instruction);
1470
print_line("\tSET BLEND CONSTANTS COLOR", set_blend_constants_instruction->color);
1471
instruction_data_cursor += sizeof(DrawListSetBlendConstantsInstruction);
1472
} break;
1473
case DrawListInstruction::TYPE_SET_LINE_WIDTH: {
1474
const DrawListSetLineWidthInstruction *set_line_width_instruction = reinterpret_cast<const DrawListSetLineWidthInstruction *>(instruction);
1475
print_line("\tSET LINE WIDTH", set_line_width_instruction->width);
1476
instruction_data_cursor += sizeof(DrawListSetLineWidthInstruction);
1477
} break;
1478
case DrawListInstruction::TYPE_SET_PUSH_CONSTANT: {
1479
const DrawListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const DrawListSetPushConstantInstruction *>(instruction);
1480
print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);
1481
instruction_data_cursor += sizeof(DrawListSetPushConstantInstruction);
1482
instruction_data_cursor += set_push_constant_instruction->size;
1483
} break;
1484
case DrawListInstruction::TYPE_SET_SCISSOR: {
1485
const DrawListSetScissorInstruction *set_scissor_instruction = reinterpret_cast<const DrawListSetScissorInstruction *>(instruction);
1486
print_line("\tSET SCISSOR", set_scissor_instruction->rect);
1487
instruction_data_cursor += sizeof(DrawListSetScissorInstruction);
1488
} break;
1489
case DrawListInstruction::TYPE_SET_VIEWPORT: {
1490
const DrawListSetViewportInstruction *set_viewport_instruction = reinterpret_cast<const DrawListSetViewportInstruction *>(instruction);
1491
print_line("\tSET VIEWPORT", set_viewport_instruction->rect);
1492
instruction_data_cursor += sizeof(DrawListSetViewportInstruction);
1493
} break;
1494
case DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
1495
const DrawListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const DrawListUniformSetPrepareForUseInstruction *>(instruction);
1496
print_line("\tUNIFORM SET PREPARE FOR USE ID", itos(uniform_set_prepare_for_use_instruction->uniform_set.id), "SHADER ID", itos(uniform_set_prepare_for_use_instruction->shader.id), "INDEX", uniform_set_prepare_for_use_instruction->set_index);
1497
instruction_data_cursor += sizeof(DrawListUniformSetPrepareForUseInstruction);
1498
} break;
1499
default:
1500
DEV_ASSERT(false && "Unknown draw list instruction type.");
1501
return;
1502
}
1503
1504
instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);
1505
}
1506
}
1507
1508
void RenderingDeviceGraph::_print_compute_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
1509
uint32_t instruction_data_cursor = 0;
1510
while (instruction_data_cursor < p_instruction_data_size) {
1511
DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);
1512
1513
const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);
1514
switch (instruction->type) {
1515
case ComputeListInstruction::TYPE_BIND_PIPELINE: {
1516
const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);
1517
print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));
1518
instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);
1519
} break;
1520
case ComputeListInstruction::TYPE_BIND_UNIFORM_SETS: {
1521
const ComputeListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const ComputeListBindUniformSetsInstruction *>(instruction);
1522
print_line("\tBIND UNIFORM SETS COUNT", bind_uniform_sets_instruction->set_count);
1523
for (uint32_t i = 0; i < bind_uniform_sets_instruction->set_count; i++) {
1524
print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_sets_instruction->uniform_set_ids()[i].id), "START INDEX", bind_uniform_sets_instruction->first_set_index);
1525
}
1526
instruction_data_cursor += sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;
1527
} break;
1528
case ComputeListInstruction::TYPE_DISPATCH: {
1529
const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);
1530
print_line("\tDISPATCH", dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);
1531
instruction_data_cursor += sizeof(ComputeListDispatchInstruction);
1532
} break;
1533
case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {
1534
const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);
1535
print_line("\tDISPATCH INDIRECT BUFFER ID", itos(dispatch_indirect_instruction->buffer.id), "OFFSET", dispatch_indirect_instruction->offset);
1536
instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);
1537
} break;
1538
case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {
1539
const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);
1540
print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);
1541
instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);
1542
instruction_data_cursor += set_push_constant_instruction->size;
1543
} break;
1544
case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
1545
const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);
1546
print_line("\tUNIFORM SET PREPARE FOR USE ID", itos(uniform_set_prepare_for_use_instruction->uniform_set.id), "SHADER ID", itos(uniform_set_prepare_for_use_instruction->shader.id), "INDEX", itos(uniform_set_prepare_for_use_instruction->set_index));
1547
instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);
1548
} break;
1549
default:
1550
DEV_ASSERT(false && "Unknown compute list instruction type.");
1551
return;
1552
}
1553
1554
instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);
1555
}
1556
}
1557
1558
void RenderingDeviceGraph::initialize(RDD *p_driver, RenderingContextDriver::Device p_device, RenderPassCreationFunction p_render_pass_creation_function, uint32_t p_frame_count, RDD::CommandQueueFamilyID p_secondary_command_queue_family, uint32_t p_secondary_command_buffers_per_frame) {
1559
DEV_ASSERT(p_driver != nullptr);
1560
DEV_ASSERT(p_render_pass_creation_function != nullptr);
1561
DEV_ASSERT(p_frame_count > 0);
1562
1563
driver = p_driver;
1564
device = p_device;
1565
render_pass_creation_function = p_render_pass_creation_function;
1566
frames.resize(p_frame_count);
1567
1568
for (uint32_t i = 0; i < p_frame_count; i++) {
1569
frames[i].secondary_command_buffers.resize(p_secondary_command_buffers_per_frame);
1570
1571
for (uint32_t j = 0; j < p_secondary_command_buffers_per_frame; j++) {
1572
SecondaryCommandBuffer &secondary = frames[i].secondary_command_buffers[j];
1573
secondary.command_pool = driver->command_pool_create(p_secondary_command_queue_family, RDD::COMMAND_BUFFER_TYPE_SECONDARY);
1574
secondary.command_buffer = driver->command_buffer_create(secondary.command_pool);
1575
secondary.task = WorkerThreadPool::INVALID_TASK_ID;
1576
}
1577
}
1578
1579
driver_honors_barriers = driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS);
1580
driver_clears_with_copy_engine = driver->api_trait_get(RDD::API_TRAIT_CLEARS_WITH_COPY_ENGINE);
1581
driver_buffers_require_transitions = driver->api_trait_get(RDD::API_TRAIT_BUFFERS_REQUIRE_TRANSITIONS);
1582
}
1583
1584
void RenderingDeviceGraph::finalize() {
1585
if (!frames.is_empty()) {
1586
_wait_for_secondary_command_buffer_tasks();
1587
}
1588
1589
for (Frame &f : frames) {
1590
for (SecondaryCommandBuffer &secondary : f.secondary_command_buffers) {
1591
if (secondary.command_pool.id != 0) {
1592
driver->command_pool_free(secondary.command_pool);
1593
}
1594
}
1595
}
1596
1597
frames.clear();
1598
}
1599
1600
void RenderingDeviceGraph::begin() {
1601
command_data.clear();
1602
command_data_offsets.clear();
1603
command_normalization_barriers.clear();
1604
command_transition_barriers.clear();
1605
command_buffer_barriers.clear();
1606
command_label_chars.clear();
1607
command_label_colors.clear();
1608
command_label_offsets.clear();
1609
command_list_nodes.clear();
1610
read_slice_list_nodes.clear();
1611
write_slice_list_nodes.clear();
1612
command_count = 0;
1613
command_label_count = 0;
1614
command_timestamp_index = -1;
1615
command_synchronization_index = -1;
1616
command_synchronization_pending = false;
1617
command_label_index = -1;
1618
frames[frame].secondary_command_buffers_used = 0;
1619
draw_instruction_list.index = 0;
1620
compute_instruction_list.index = 0;
1621
tracking_frame++;
1622
1623
#ifdef DEV_ENABLED
1624
write_dependency_counters.clear();
1625
#endif
1626
}
1627
1628
void RenderingDeviceGraph::add_buffer_clear(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, uint32_t p_offset, uint32_t p_size) {
1629
DEV_ASSERT(p_dst_tracker != nullptr);
1630
1631
int32_t command_index;
1632
RecordedBufferClearCommand *command = static_cast<RecordedBufferClearCommand *>(_allocate_command(sizeof(RecordedBufferClearCommand), command_index));
1633
command->type = RecordedCommand::TYPE_BUFFER_CLEAR;
1634
command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
1635
command->buffer = p_dst;
1636
command->offset = p_offset;
1637
command->size = p_size;
1638
1639
ResourceUsage usage = RESOURCE_USAGE_COPY_TO;
1640
_add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
1641
}
1642
1643
void RenderingDeviceGraph::add_buffer_copy(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, RDD::BufferCopyRegion p_region) {
1644
// Source tracker is allowed to be null as it could be a read-only buffer.
1645
DEV_ASSERT(p_dst_tracker != nullptr);
1646
1647
int32_t command_index;
1648
RecordedBufferCopyCommand *command = static_cast<RecordedBufferCopyCommand *>(_allocate_command(sizeof(RecordedBufferCopyCommand), command_index));
1649
command->type = RecordedCommand::TYPE_BUFFER_COPY;
1650
command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
1651
command->source = p_src;
1652
command->destination = p_dst;
1653
command->region = p_region;
1654
1655
ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
1656
ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };
1657
_add_command_to_graph(trackers, usages, p_src_tracker != nullptr ? 2 : 1, command_index, command);
1658
}
1659
1660
void RenderingDeviceGraph::add_buffer_get_data(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, RDD::BufferCopyRegion p_region) {
1661
// Source tracker is allowed to be null as it could be a read-only buffer.
1662
int32_t command_index;
1663
RecordedBufferGetDataCommand *command = static_cast<RecordedBufferGetDataCommand *>(_allocate_command(sizeof(RecordedBufferGetDataCommand), command_index));
1664
command->type = RecordedCommand::TYPE_BUFFER_GET_DATA;
1665
command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
1666
command->source = p_src;
1667
command->destination = p_dst;
1668
command->region = p_region;
1669
1670
if (p_src_tracker != nullptr) {
1671
ResourceUsage usage = RESOURCE_USAGE_COPY_FROM;
1672
_add_command_to_graph(&p_src_tracker, &usage, 1, command_index, command);
1673
} else {
1674
_add_command_to_graph(nullptr, nullptr, 0, command_index, command);
1675
}
1676
}
1677
1678
void RenderingDeviceGraph::add_buffer_update(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferCopy> p_buffer_copies) {
1679
DEV_ASSERT(p_dst_tracker != nullptr);
1680
1681
size_t buffer_copies_size = p_buffer_copies.size() * sizeof(RecordedBufferCopy);
1682
uint64_t command_size = sizeof(RecordedBufferUpdateCommand) + buffer_copies_size;
1683
int32_t command_index;
1684
RecordedBufferUpdateCommand *command = static_cast<RecordedBufferUpdateCommand *>(_allocate_command(command_size, command_index));
1685
command->type = RecordedCommand::TYPE_BUFFER_UPDATE;
1686
command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
1687
command->destination = p_dst;
1688
command->buffer_copies_count = p_buffer_copies.size();
1689
1690
RecordedBufferCopy *buffer_copies = command->buffer_copies();
1691
for (uint32_t i = 0; i < command->buffer_copies_count; i++) {
1692
buffer_copies[i] = p_buffer_copies[i];
1693
}
1694
1695
ResourceUsage buffer_usage = RESOURCE_USAGE_COPY_TO;
1696
_add_command_to_graph(&p_dst_tracker, &buffer_usage, 1, command_index, command);
1697
}
1698
1699
void RenderingDeviceGraph::add_driver_callback(RDD::DriverCallback p_callback, void *p_userdata, VectorView<ResourceTracker *> p_trackers, VectorView<RenderingDeviceGraph::ResourceUsage> p_usages) {
1700
DEV_ASSERT(p_trackers.size() == p_usages.size());
1701
1702
int32_t command_index;
1703
RecordedDriverCallbackCommand *command = static_cast<RecordedDriverCallbackCommand *>(_allocate_command(sizeof(RecordedDriverCallbackCommand), command_index));
1704
command->type = RecordedCommand::TYPE_DRIVER_CALLBACK;
1705
command->callback = p_callback;
1706
command->userdata = p_userdata;
1707
_add_command_to_graph((ResourceTracker **)p_trackers.ptr(), (ResourceUsage *)p_usages.ptr(), p_trackers.size(), command_index, command);
1708
}
1709
1710
void RenderingDeviceGraph::add_compute_list_begin(RDD::BreadcrumbMarker p_phase, uint32_t p_breadcrumb_data) {
1711
compute_instruction_list.clear();
1712
#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
1713
compute_instruction_list.breadcrumb = p_breadcrumb_data | (p_phase & ((1 << 16) - 1));
1714
#endif
1715
compute_instruction_list.index++;
1716
}
1717
1718
void RenderingDeviceGraph::add_compute_list_bind_pipeline(RDD::PipelineID p_pipeline) {
1719
ComputeListBindPipelineInstruction *instruction = reinterpret_cast<ComputeListBindPipelineInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListBindPipelineInstruction)));
1720
instruction->type = ComputeListInstruction::TYPE_BIND_PIPELINE;
1721
instruction->pipeline = p_pipeline;
1722
compute_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
1723
}
1724
1725
void RenderingDeviceGraph::add_compute_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
1726
add_compute_list_bind_uniform_sets(p_shader, VectorView(&p_uniform_set, 1), set_index, 1);
1727
}
1728
1729
void RenderingDeviceGraph::add_compute_list_bind_uniform_sets(RDD::ShaderID p_shader, VectorView<RDD::UniformSetID> p_uniform_sets, uint32_t p_first_set_index, uint32_t p_set_count) {
1730
DEV_ASSERT(p_uniform_sets.size() >= p_set_count);
1731
1732
uint32_t instruction_size = sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * p_set_count;
1733
ComputeListBindUniformSetsInstruction *instruction = reinterpret_cast<ComputeListBindUniformSetsInstruction *>(_allocate_compute_list_instruction(instruction_size));
1734
instruction->type = ComputeListInstruction::TYPE_BIND_UNIFORM_SETS;
1735
instruction->shader = p_shader;
1736
instruction->first_set_index = p_first_set_index;
1737
instruction->set_count = p_set_count;
1738
1739
RDD::UniformSetID *ids = instruction->uniform_set_ids();
1740
for (uint32_t i = 0; i < p_set_count; i++) {
1741
ids[i] = p_uniform_sets[i];
1742
}
1743
}
1744
1745
void RenderingDeviceGraph::add_compute_list_dispatch(uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) {
1746
ComputeListDispatchInstruction *instruction = reinterpret_cast<ComputeListDispatchInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListDispatchInstruction)));
1747
instruction->type = ComputeListInstruction::TYPE_DISPATCH;
1748
instruction->x_groups = p_x_groups;
1749
instruction->y_groups = p_y_groups;
1750
instruction->z_groups = p_z_groups;
1751
}
1752
1753
void RenderingDeviceGraph::add_compute_list_dispatch_indirect(RDD::BufferID p_buffer, uint32_t p_offset) {
1754
ComputeListDispatchIndirectInstruction *instruction = reinterpret_cast<ComputeListDispatchIndirectInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListDispatchIndirectInstruction)));
1755
instruction->type = ComputeListInstruction::TYPE_DISPATCH_INDIRECT;
1756
instruction->buffer = p_buffer;
1757
instruction->offset = p_offset;
1758
compute_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
1759
}
1760
1761
void RenderingDeviceGraph::add_compute_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {
1762
uint32_t instruction_size = sizeof(ComputeListSetPushConstantInstruction) + p_data_size;
1763
ComputeListSetPushConstantInstruction *instruction = reinterpret_cast<ComputeListSetPushConstantInstruction *>(_allocate_compute_list_instruction(instruction_size));
1764
instruction->type = ComputeListInstruction::TYPE_SET_PUSH_CONSTANT;
1765
instruction->size = p_data_size;
1766
instruction->shader = p_shader;
1767
memcpy(instruction->data(), p_data, p_data_size);
1768
}
1769
1770
void RenderingDeviceGraph::add_compute_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
1771
ComputeListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<ComputeListUniformSetPrepareForUseInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListUniformSetPrepareForUseInstruction)));
1772
instruction->type = ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;
1773
instruction->shader = p_shader;
1774
instruction->uniform_set = p_uniform_set;
1775
instruction->set_index = set_index;
1776
}
1777
1778
void RenderingDeviceGraph::add_compute_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {
1779
DEV_ASSERT(p_tracker != nullptr);
1780
1781
p_tracker->reset_if_outdated(tracking_frame);
1782
1783
if (p_tracker->compute_list_index != compute_instruction_list.index) {
1784
compute_instruction_list.command_trackers.push_back(p_tracker);
1785
compute_instruction_list.command_tracker_usages.push_back(p_usage);
1786
p_tracker->compute_list_index = compute_instruction_list.index;
1787
p_tracker->compute_list_usage = p_usage;
1788
}
1789
#ifdef DEV_ENABLED
1790
else if (p_tracker->compute_list_usage != p_usage) {
1791
ERR_FAIL_MSG(vformat("Tracker can't have more than one type of usage in the same compute list. Compute list usage is %s and the requested usage is %s.", _usage_to_string(p_tracker->compute_list_usage), _usage_to_string(p_usage)));
1792
}
1793
#endif
1794
}
1795
1796
void RenderingDeviceGraph::add_compute_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {
1797
DEV_ASSERT(p_trackers.size() == p_usages.size());
1798
1799
for (uint32_t i = 0; i < p_trackers.size(); i++) {
1800
add_compute_list_usage(p_trackers[i], p_usages[i]);
1801
}
1802
}
1803
1804
void RenderingDeviceGraph::add_compute_list_end() {
1805
int32_t command_index;
1806
uint32_t instruction_data_size = compute_instruction_list.data.size();
1807
uint32_t command_size = sizeof(RecordedComputeListCommand) + instruction_data_size;
1808
RecordedComputeListCommand *command = static_cast<RecordedComputeListCommand *>(_allocate_command(command_size, command_index));
1809
command->type = RecordedCommand::TYPE_COMPUTE_LIST;
1810
command->self_stages = compute_instruction_list.stages;
1811
command->instruction_data_size = instruction_data_size;
1812
memcpy(command->instruction_data(), compute_instruction_list.data.ptr(), instruction_data_size);
1813
_add_command_to_graph(compute_instruction_list.command_trackers.ptr(), compute_instruction_list.command_tracker_usages.ptr(), compute_instruction_list.command_trackers.size(), command_index, command);
1814
}
1815
1816
void RenderingDeviceGraph::add_draw_list_begin(FramebufferCache *p_framebuffer_cache, Rect2i p_region, VectorView<AttachmentOperation> p_attachment_operations, VectorView<RDD::RenderPassClearValue> p_attachment_clear_values, BitField<RDD::PipelineStageBits> p_stages, uint32_t p_breadcrumb, bool p_split_cmd_buffer) {
1817
_add_draw_list_begin(p_framebuffer_cache, RDD::RenderPassID(), RDD::FramebufferID(), p_region, p_attachment_operations, p_attachment_clear_values, p_stages, p_breadcrumb, p_split_cmd_buffer);
1818
}
1819
1820
void RenderingDeviceGraph::add_draw_list_begin(RDD::RenderPassID p_render_pass, RDD::FramebufferID p_framebuffer, Rect2i p_region, VectorView<AttachmentOperation> p_attachment_operations, VectorView<RDD::RenderPassClearValue> p_attachment_clear_values, BitField<RDD::PipelineStageBits> p_stages, uint32_t p_breadcrumb, bool p_split_cmd_buffer) {
1821
_add_draw_list_begin(nullptr, p_render_pass, p_framebuffer, p_region, p_attachment_operations, p_attachment_clear_values, p_stages, p_breadcrumb, p_split_cmd_buffer);
1822
}
1823
1824
void RenderingDeviceGraph::add_draw_list_bind_index_buffer(RDD::BufferID p_buffer, RDD::IndexBufferFormat p_format, uint32_t p_offset) {
1825
DrawListBindIndexBufferInstruction *instruction = reinterpret_cast<DrawListBindIndexBufferInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindIndexBufferInstruction)));
1826
instruction->type = DrawListInstruction::TYPE_BIND_INDEX_BUFFER;
1827
instruction->buffer = p_buffer;
1828
instruction->format = p_format;
1829
instruction->offset = p_offset;
1830
1831
if (instruction->buffer.id != 0) {
1832
draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);
1833
}
1834
}
1835
1836
void RenderingDeviceGraph::add_draw_list_bind_pipeline(RDD::PipelineID p_pipeline, BitField<RDD::PipelineStageBits> p_pipeline_stage_bits) {
1837
DrawListBindPipelineInstruction *instruction = reinterpret_cast<DrawListBindPipelineInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindPipelineInstruction)));
1838
instruction->type = DrawListInstruction::TYPE_BIND_PIPELINE;
1839
instruction->pipeline = p_pipeline;
1840
draw_instruction_list.stages = draw_instruction_list.stages | p_pipeline_stage_bits;
1841
}
1842
1843
void RenderingDeviceGraph::add_draw_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
1844
add_draw_list_bind_uniform_sets(p_shader, VectorView(&p_uniform_set, 1), set_index, 1);
1845
}
1846
1847
void RenderingDeviceGraph::add_draw_list_bind_uniform_sets(RDD::ShaderID p_shader, VectorView<RDD::UniformSetID> p_uniform_sets, uint32_t p_first_index, uint32_t p_set_count) {
1848
DEV_ASSERT(p_uniform_sets.size() >= p_set_count);
1849
1850
uint32_t instruction_size = sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * p_set_count;
1851
DrawListBindUniformSetsInstruction *instruction = reinterpret_cast<DrawListBindUniformSetsInstruction *>(_allocate_draw_list_instruction(instruction_size));
1852
instruction->type = DrawListInstruction::TYPE_BIND_UNIFORM_SETS;
1853
instruction->shader = p_shader;
1854
instruction->first_set_index = p_first_index;
1855
instruction->set_count = p_set_count;
1856
1857
for (uint32_t i = 0; i < p_set_count; i++) {
1858
instruction->uniform_set_ids()[i] = p_uniform_sets[i];
1859
}
1860
}
1861
1862
void RenderingDeviceGraph::add_draw_list_bind_vertex_buffers(VectorView<RDD::BufferID> p_vertex_buffers, VectorView<uint64_t> p_vertex_buffer_offsets) {
1863
DEV_ASSERT(p_vertex_buffers.size() == p_vertex_buffer_offsets.size());
1864
1865
uint32_t instruction_size = sizeof(DrawListBindVertexBuffersInstruction) + sizeof(RDD::BufferID) * p_vertex_buffers.size() + sizeof(uint64_t) * p_vertex_buffer_offsets.size();
1866
DrawListBindVertexBuffersInstruction *instruction = reinterpret_cast<DrawListBindVertexBuffersInstruction *>(_allocate_draw_list_instruction(instruction_size));
1867
instruction->type = DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS;
1868
instruction->vertex_buffers_count = p_vertex_buffers.size();
1869
1870
RDD::BufferID *vertex_buffers = instruction->vertex_buffers();
1871
uint64_t *vertex_buffer_offsets = instruction->vertex_buffer_offsets();
1872
for (uint32_t i = 0; i < instruction->vertex_buffers_count; i++) {
1873
vertex_buffers[i] = p_vertex_buffers[i];
1874
vertex_buffer_offsets[i] = p_vertex_buffer_offsets[i];
1875
}
1876
1877
if (instruction->vertex_buffers_count > 0) {
1878
draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);
1879
}
1880
}
1881
1882
void RenderingDeviceGraph::add_draw_list_clear_attachments(VectorView<RDD::AttachmentClear> p_attachments_clear, VectorView<Rect2i> p_attachments_clear_rect) {
1883
uint32_t instruction_size = sizeof(DrawListClearAttachmentsInstruction) + sizeof(RDD::AttachmentClear) * p_attachments_clear.size() + sizeof(Rect2i) * p_attachments_clear_rect.size();
1884
DrawListClearAttachmentsInstruction *instruction = reinterpret_cast<DrawListClearAttachmentsInstruction *>(_allocate_draw_list_instruction(instruction_size));
1885
instruction->type = DrawListInstruction::TYPE_CLEAR_ATTACHMENTS;
1886
instruction->attachments_clear_count = p_attachments_clear.size();
1887
instruction->attachments_clear_rect_count = p_attachments_clear_rect.size();
1888
1889
RDD::AttachmentClear *attachments_clear = instruction->attachments_clear();
1890
Rect2i *attachments_clear_rect = instruction->attachments_clear_rect();
1891
for (uint32_t i = 0; i < instruction->attachments_clear_count; i++) {
1892
attachments_clear[i] = p_attachments_clear[i];
1893
}
1894
1895
for (uint32_t i = 0; i < instruction->attachments_clear_rect_count; i++) {
1896
attachments_clear_rect[i] = p_attachments_clear_rect[i];
1897
}
1898
}
1899
1900
void RenderingDeviceGraph::add_draw_list_draw(uint32_t p_vertex_count, uint32_t p_instance_count) {
1901
DrawListDrawInstruction *instruction = reinterpret_cast<DrawListDrawInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawInstruction)));
1902
instruction->type = DrawListInstruction::TYPE_DRAW;
1903
instruction->vertex_count = p_vertex_count;
1904
instruction->instance_count = p_instance_count;
1905
}
1906
1907
void RenderingDeviceGraph::add_draw_list_draw_indexed(uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index) {
1908
DrawListDrawIndexedInstruction *instruction = reinterpret_cast<DrawListDrawIndexedInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndexedInstruction)));
1909
instruction->type = DrawListInstruction::TYPE_DRAW_INDEXED;
1910
instruction->index_count = p_index_count;
1911
instruction->instance_count = p_instance_count;
1912
instruction->first_index = p_first_index;
1913
}
1914
1915
void RenderingDeviceGraph::add_draw_list_draw_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {
1916
DrawListDrawIndirectInstruction *instruction = reinterpret_cast<DrawListDrawIndirectInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndirectInstruction)));
1917
instruction->type = DrawListInstruction::TYPE_DRAW_INDIRECT;
1918
instruction->buffer = p_buffer;
1919
instruction->offset = p_offset;
1920
instruction->draw_count = p_draw_count;
1921
instruction->stride = p_stride;
1922
draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
1923
}
1924
1925
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) {
1926
DrawListDrawIndexedIndirectInstruction *instruction = reinterpret_cast<DrawListDrawIndexedIndirectInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndexedIndirectInstruction)));
1927
instruction->type = DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT;
1928
instruction->buffer = p_buffer;
1929
instruction->offset = p_offset;
1930
instruction->draw_count = p_draw_count;
1931
instruction->stride = p_stride;
1932
draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
1933
}
1934
1935
void RenderingDeviceGraph::add_draw_list_execute_commands(RDD::CommandBufferID p_command_buffer) {
1936
DrawListExecuteCommandsInstruction *instruction = reinterpret_cast<DrawListExecuteCommandsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListExecuteCommandsInstruction)));
1937
instruction->type = DrawListInstruction::TYPE_EXECUTE_COMMANDS;
1938
instruction->command_buffer = p_command_buffer;
1939
}
1940
1941
void RenderingDeviceGraph::add_draw_list_next_subpass(RDD::CommandBufferType p_command_buffer_type) {
1942
DrawListNextSubpassInstruction *instruction = reinterpret_cast<DrawListNextSubpassInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListNextSubpassInstruction)));
1943
instruction->type = DrawListInstruction::TYPE_NEXT_SUBPASS;
1944
instruction->command_buffer_type = p_command_buffer_type;
1945
}
1946
1947
void RenderingDeviceGraph::add_draw_list_set_blend_constants(const Color &p_color) {
1948
DrawListSetBlendConstantsInstruction *instruction = reinterpret_cast<DrawListSetBlendConstantsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetBlendConstantsInstruction)));
1949
instruction->type = DrawListInstruction::TYPE_SET_BLEND_CONSTANTS;
1950
instruction->color = p_color;
1951
}
1952
1953
void RenderingDeviceGraph::add_draw_list_set_line_width(float p_width) {
1954
DrawListSetLineWidthInstruction *instruction = reinterpret_cast<DrawListSetLineWidthInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetLineWidthInstruction)));
1955
instruction->type = DrawListInstruction::TYPE_SET_LINE_WIDTH;
1956
instruction->width = p_width;
1957
}
1958
1959
void RenderingDeviceGraph::add_draw_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {
1960
uint32_t instruction_size = sizeof(DrawListSetPushConstantInstruction) + p_data_size;
1961
DrawListSetPushConstantInstruction *instruction = reinterpret_cast<DrawListSetPushConstantInstruction *>(_allocate_draw_list_instruction(instruction_size));
1962
instruction->type = DrawListInstruction::TYPE_SET_PUSH_CONSTANT;
1963
instruction->size = p_data_size;
1964
instruction->shader = p_shader;
1965
memcpy(instruction->data(), p_data, p_data_size);
1966
}
1967
1968
void RenderingDeviceGraph::add_draw_list_set_scissor(Rect2i p_rect) {
1969
DrawListSetScissorInstruction *instruction = reinterpret_cast<DrawListSetScissorInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetScissorInstruction)));
1970
instruction->type = DrawListInstruction::TYPE_SET_SCISSOR;
1971
instruction->rect = p_rect;
1972
}
1973
1974
void RenderingDeviceGraph::add_draw_list_set_viewport(Rect2i p_rect) {
1975
DrawListSetViewportInstruction *instruction = reinterpret_cast<DrawListSetViewportInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetViewportInstruction)));
1976
instruction->type = DrawListInstruction::TYPE_SET_VIEWPORT;
1977
instruction->rect = p_rect;
1978
}
1979
1980
void RenderingDeviceGraph::add_draw_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
1981
DrawListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<DrawListUniformSetPrepareForUseInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListUniformSetPrepareForUseInstruction)));
1982
instruction->type = DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;
1983
instruction->shader = p_shader;
1984
instruction->uniform_set = p_uniform_set;
1985
instruction->set_index = set_index;
1986
}
1987
1988
void RenderingDeviceGraph::add_draw_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {
1989
p_tracker->reset_if_outdated(tracking_frame);
1990
1991
if (p_tracker->draw_list_index != draw_instruction_list.index) {
1992
draw_instruction_list.command_trackers.push_back(p_tracker);
1993
draw_instruction_list.command_tracker_usages.push_back(p_usage);
1994
p_tracker->draw_list_index = draw_instruction_list.index;
1995
p_tracker->draw_list_usage = p_usage;
1996
}
1997
#ifdef DEV_ENABLED
1998
else if (p_tracker->draw_list_usage != p_usage) {
1999
ERR_FAIL_MSG(vformat("Tracker can't have more than one type of usage in the same draw list. Draw list usage is %s and the requested usage is %s.", _usage_to_string(p_tracker->draw_list_usage), _usage_to_string(p_usage)));
2000
}
2001
#endif
2002
}
2003
2004
void RenderingDeviceGraph::add_draw_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {
2005
DEV_ASSERT(p_trackers.size() == p_usages.size());
2006
2007
for (uint32_t i = 0; i < p_trackers.size(); i++) {
2008
add_draw_list_usage(p_trackers[i], p_usages[i]);
2009
}
2010
}
2011
2012
void RenderingDeviceGraph::add_draw_list_end() {
2013
FramebufferCache *framebuffer_cache = draw_instruction_list.framebuffer_cache;
2014
int32_t command_index;
2015
uint32_t clear_values_size = sizeof(RDD::RenderPassClearValue) * draw_instruction_list.attachment_clear_values.size();
2016
uint32_t trackers_count = framebuffer_cache != nullptr ? framebuffer_cache->trackers.size() : 0;
2017
uint32_t trackers_and_ops_size = (sizeof(ResourceTracker *) + sizeof(RDD::AttachmentLoadOp) + sizeof(RDD::AttachmentStoreOp)) * trackers_count;
2018
uint32_t instruction_data_size = draw_instruction_list.data.size();
2019
uint32_t command_size = sizeof(RecordedDrawListCommand) + clear_values_size + trackers_and_ops_size + instruction_data_size;
2020
RecordedDrawListCommand *command = static_cast<RecordedDrawListCommand *>(_allocate_command(command_size, command_index));
2021
command->type = RecordedCommand::TYPE_DRAW_LIST;
2022
command->self_stages = draw_instruction_list.stages;
2023
command->framebuffer_cache = framebuffer_cache;
2024
command->render_pass = draw_instruction_list.render_pass;
2025
command->framebuffer = draw_instruction_list.framebuffer;
2026
command->instruction_data_size = instruction_data_size;
2027
command->command_buffer_type = RDD::COMMAND_BUFFER_TYPE_PRIMARY;
2028
command->region = draw_instruction_list.region;
2029
#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
2030
command->breadcrumb = draw_instruction_list.breadcrumb;
2031
#endif
2032
command->split_cmd_buffer = draw_instruction_list.split_cmd_buffer;
2033
command->clear_values_count = draw_instruction_list.attachment_clear_values.size();
2034
command->trackers_count = trackers_count;
2035
2036
// Initialize the load and store operations to their default behaviors. The store behavior will be modified if a command depends on the result of this render pass.
2037
uint32_t attachment_op_count = draw_instruction_list.attachment_operations.size();
2038
ResourceTracker **trackers = command->trackers();
2039
RDD::AttachmentLoadOp *load_ops = command->load_ops();
2040
RDD::AttachmentStoreOp *store_ops = command->store_ops();
2041
for (uint32_t i = 0; i < command->trackers_count; i++) {
2042
ResourceTracker *resource_tracker = framebuffer_cache->trackers[i];
2043
if (resource_tracker != nullptr) {
2044
if (i < command->clear_values_count && i < attachment_op_count && draw_instruction_list.attachment_operations[i] == ATTACHMENT_OPERATION_CLEAR) {
2045
load_ops[i] = RDD::ATTACHMENT_LOAD_OP_CLEAR;
2046
} else if (i < attachment_op_count && draw_instruction_list.attachment_operations[i] == ATTACHMENT_OPERATION_IGNORE) {
2047
load_ops[i] = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
2048
} else if (resource_tracker->is_discardable) {
2049
bool resource_has_parent = resource_tracker->parent != nullptr;
2050
ResourceTracker *search_tracker = resource_has_parent ? resource_tracker->parent : resource_tracker;
2051
search_tracker->reset_if_outdated(tracking_frame);
2052
bool resource_was_modified_this_frame = search_tracker->write_command_or_list_index >= 0;
2053
load_ops[i] = resource_was_modified_this_frame ? RDD::ATTACHMENT_LOAD_OP_LOAD : RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
2054
} else {
2055
load_ops[i] = RDD::ATTACHMENT_LOAD_OP_LOAD;
2056
}
2057
2058
store_ops[i] = resource_tracker->is_discardable ? RDD::ATTACHMENT_STORE_OP_DONT_CARE : RDD::ATTACHMENT_STORE_OP_STORE;
2059
} else {
2060
load_ops[i] = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
2061
store_ops[i] = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
2062
}
2063
2064
trackers[i] = resource_tracker;
2065
}
2066
2067
RDD::RenderPassClearValue *clear_values = command->clear_values();
2068
for (uint32_t i = 0; i < command->clear_values_count; i++) {
2069
clear_values[i] = draw_instruction_list.attachment_clear_values[i];
2070
}
2071
2072
memcpy(command->instruction_data(), draw_instruction_list.data.ptr(), instruction_data_size);
2073
_add_command_to_graph(draw_instruction_list.command_trackers.ptr(), draw_instruction_list.command_tracker_usages.ptr(), draw_instruction_list.command_trackers.size(), command_index, command);
2074
}
2075
2076
void RenderingDeviceGraph::add_texture_clear(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, const Color &p_color, const RDD::TextureSubresourceRange &p_range) {
2077
DEV_ASSERT(p_dst_tracker != nullptr);
2078
2079
int32_t command_index;
2080
RecordedTextureClearCommand *command = static_cast<RecordedTextureClearCommand *>(_allocate_command(sizeof(RecordedTextureClearCommand), command_index));
2081
command->type = RecordedCommand::TYPE_TEXTURE_CLEAR;
2082
command->texture = p_dst;
2083
command->color = p_color;
2084
command->range = p_range;
2085
2086
ResourceUsage usage;
2087
if (driver_clears_with_copy_engine) {
2088
command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
2089
usage = RESOURCE_USAGE_COPY_TO;
2090
} else {
2091
// If the driver is uncapable of using the copy engine for clearing the image (e.g. D3D12), we must either transition the
2092
// resource to a render target or a storage image as that's the only two ways it can perform the operation.
2093
if (p_dst_tracker->texture_usage & RDD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) {
2094
command->self_stages = RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
2095
usage = RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE;
2096
} else {
2097
command->self_stages = RDD::PIPELINE_STAGE_CLEAR_STORAGE_BIT;
2098
usage = RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE;
2099
}
2100
}
2101
2102
_add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
2103
}
2104
2105
void RenderingDeviceGraph::add_texture_copy(RDD::TextureID p_src, ResourceTracker *p_src_tracker, RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RDD::TextureCopyRegion> p_texture_copy_regions) {
2106
DEV_ASSERT(p_src_tracker != nullptr);
2107
DEV_ASSERT(p_dst_tracker != nullptr);
2108
2109
int32_t command_index;
2110
uint64_t command_size = sizeof(RecordedTextureCopyCommand) + p_texture_copy_regions.size() * sizeof(RDD::TextureCopyRegion);
2111
RecordedTextureCopyCommand *command = static_cast<RecordedTextureCopyCommand *>(_allocate_command(command_size, command_index));
2112
command->type = RecordedCommand::TYPE_TEXTURE_COPY;
2113
command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
2114
command->from_texture = p_src;
2115
command->to_texture = p_dst;
2116
command->texture_copy_regions_count = p_texture_copy_regions.size();
2117
2118
RDD::TextureCopyRegion *texture_copy_regions = command->texture_copy_regions();
2119
for (uint32_t i = 0; i < command->texture_copy_regions_count; i++) {
2120
texture_copy_regions[i] = p_texture_copy_regions[i];
2121
}
2122
2123
ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
2124
ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };
2125
_add_command_to_graph(trackers, usages, 2, command_index, command);
2126
}
2127
2128
void RenderingDeviceGraph::add_texture_get_data(RDD::TextureID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, VectorView<RDD::BufferTextureCopyRegion> p_buffer_texture_copy_regions, ResourceTracker *p_dst_tracker) {
2129
DEV_ASSERT(p_src_tracker != nullptr);
2130
2131
int32_t command_index;
2132
uint64_t command_size = sizeof(RecordedTextureGetDataCommand) + p_buffer_texture_copy_regions.size() * sizeof(RDD::BufferTextureCopyRegion);
2133
RecordedTextureGetDataCommand *command = static_cast<RecordedTextureGetDataCommand *>(_allocate_command(command_size, command_index));
2134
command->type = RecordedCommand::TYPE_TEXTURE_GET_DATA;
2135
command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
2136
command->from_texture = p_src;
2137
command->to_buffer = p_dst;
2138
command->buffer_texture_copy_regions_count = p_buffer_texture_copy_regions.size();
2139
2140
RDD::BufferTextureCopyRegion *buffer_texture_copy_regions = command->buffer_texture_copy_regions();
2141
for (uint32_t i = 0; i < command->buffer_texture_copy_regions_count; i++) {
2142
buffer_texture_copy_regions[i] = p_buffer_texture_copy_regions[i];
2143
}
2144
2145
if (p_dst_tracker != nullptr) {
2146
// Add the optional destination tracker if it was provided.
2147
ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
2148
ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };
2149
_add_command_to_graph(trackers, usages, 2, command_index, command);
2150
} else {
2151
ResourceUsage usage = RESOURCE_USAGE_COPY_FROM;
2152
_add_command_to_graph(&p_src_tracker, &usage, 1, command_index, command);
2153
}
2154
}
2155
2156
void RenderingDeviceGraph::add_texture_resolve(RDD::TextureID p_src, ResourceTracker *p_src_tracker, RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, uint32_t p_src_layer, uint32_t p_src_mipmap, uint32_t p_dst_layer, uint32_t p_dst_mipmap) {
2157
DEV_ASSERT(p_src_tracker != nullptr);
2158
DEV_ASSERT(p_dst_tracker != nullptr);
2159
2160
int32_t command_index;
2161
RecordedTextureResolveCommand *command = static_cast<RecordedTextureResolveCommand *>(_allocate_command(sizeof(RecordedTextureResolveCommand), command_index));
2162
command->type = RecordedCommand::TYPE_TEXTURE_RESOLVE;
2163
command->self_stages = RDD::PIPELINE_STAGE_RESOLVE_BIT;
2164
command->from_texture = p_src;
2165
command->to_texture = p_dst;
2166
command->src_layer = p_src_layer;
2167
command->src_mipmap = p_src_mipmap;
2168
command->dst_layer = p_dst_layer;
2169
command->dst_mipmap = p_dst_mipmap;
2170
2171
ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
2172
ResourceUsage usages[2] = { RESOURCE_USAGE_RESOLVE_TO, RESOURCE_USAGE_RESOLVE_FROM };
2173
_add_command_to_graph(trackers, usages, 2, command_index, command);
2174
}
2175
2176
void RenderingDeviceGraph::add_texture_update(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferToTextureCopy> p_buffer_copies, VectorView<ResourceTracker *> p_buffer_trackers) {
2177
DEV_ASSERT(p_dst_tracker != nullptr);
2178
2179
int32_t command_index;
2180
uint64_t command_size = sizeof(RecordedTextureUpdateCommand) + p_buffer_copies.size() * sizeof(RecordedBufferToTextureCopy);
2181
RecordedTextureUpdateCommand *command = static_cast<RecordedTextureUpdateCommand *>(_allocate_command(command_size, command_index));
2182
command->type = RecordedCommand::TYPE_TEXTURE_UPDATE;
2183
command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
2184
command->to_texture = p_dst;
2185
command->buffer_to_texture_copies_count = p_buffer_copies.size();
2186
2187
RecordedBufferToTextureCopy *buffer_to_texture_copies = command->buffer_to_texture_copies();
2188
for (uint32_t i = 0; i < command->buffer_to_texture_copies_count; i++) {
2189
buffer_to_texture_copies[i] = p_buffer_copies[i];
2190
}
2191
2192
if (p_buffer_trackers.size() > 0) {
2193
// Add the optional buffer trackers if they were provided.
2194
thread_local LocalVector<ResourceTracker *> trackers;
2195
thread_local LocalVector<ResourceUsage> usages;
2196
trackers.clear();
2197
usages.clear();
2198
for (uint32_t i = 0; i < p_buffer_trackers.size(); i++) {
2199
trackers.push_back(p_buffer_trackers[i]);
2200
usages.push_back(RESOURCE_USAGE_COPY_FROM);
2201
}
2202
2203
trackers.push_back(p_dst_tracker);
2204
usages.push_back(RESOURCE_USAGE_COPY_TO);
2205
2206
_add_command_to_graph(trackers.ptr(), usages.ptr(), trackers.size(), command_index, command);
2207
} else {
2208
ResourceUsage usage = RESOURCE_USAGE_COPY_TO;
2209
_add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
2210
}
2211
}
2212
2213
void RenderingDeviceGraph::add_capture_timestamp(RDD::QueryPoolID p_query_pool, uint32_t p_index) {
2214
int32_t command_index;
2215
RecordedCaptureTimestampCommand *command = static_cast<RecordedCaptureTimestampCommand *>(_allocate_command(sizeof(RecordedCaptureTimestampCommand), command_index));
2216
command->type = RecordedCommand::TYPE_CAPTURE_TIMESTAMP;
2217
command->self_stages = 0;
2218
command->pool = p_query_pool;
2219
command->index = p_index;
2220
_add_command_to_graph(nullptr, nullptr, 0, command_index, command);
2221
}
2222
2223
void RenderingDeviceGraph::add_synchronization() {
2224
// Synchronization is only acknowledged if commands have been recorded on the graph already.
2225
if (command_count > 0) {
2226
command_synchronization_pending = true;
2227
}
2228
}
2229
2230
void RenderingDeviceGraph::begin_label(const Span<char> &p_label_name, const Color &p_color) {
2231
uint32_t command_label_offset = command_label_chars.size();
2232
int command_label_size = p_label_name.size();
2233
command_label_chars.resize(command_label_offset + command_label_size + 1);
2234
memcpy(&command_label_chars[command_label_offset], p_label_name.ptr(), command_label_size);
2235
command_label_chars[command_label_offset + command_label_size] = '\0';
2236
command_label_colors.push_back(p_color);
2237
command_label_offsets.push_back(command_label_offset);
2238
command_label_index = command_label_count;
2239
command_label_count++;
2240
}
2241
2242
void RenderingDeviceGraph::end_label() {
2243
command_label_index = -1;
2244
}
2245
2246
void RenderingDeviceGraph::end(bool p_reorder_commands, bool p_full_barriers, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool) {
2247
if (command_count == 0) {
2248
// No commands have been logged, do nothing.
2249
return;
2250
}
2251
2252
thread_local LocalVector<RecordedCommandSort> commands_sorted;
2253
if (p_reorder_commands) {
2254
thread_local LocalVector<int64_t> command_stack;
2255
thread_local LocalVector<int32_t> sorted_command_indices;
2256
thread_local LocalVector<uint32_t> command_degrees;
2257
int32_t adjacency_list_index = 0;
2258
int32_t command_index;
2259
2260
// Count all the incoming connections to every node by traversing their adjacency list.
2261
command_degrees.resize(command_count);
2262
memset(command_degrees.ptr(), 0, sizeof(uint32_t) * command_degrees.size());
2263
for (uint32_t i = 0; i < command_count; i++) {
2264
const RecordedCommand &recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offsets[i]]);
2265
adjacency_list_index = recorded_command.adjacent_command_list_index;
2266
while (adjacency_list_index >= 0) {
2267
const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];
2268
DEV_ASSERT((command_list_node.command_index != int32_t(i)) && "Command can't have itself as a dependency.");
2269
command_degrees[command_list_node.command_index] += 1;
2270
adjacency_list_index = command_list_node.next_list_index;
2271
}
2272
}
2273
2274
// Push to the stack all nodes that have no incoming connections.
2275
command_stack.clear();
2276
for (uint32_t i = 0; i < command_count; i++) {
2277
if (command_degrees[i] == 0) {
2278
command_stack.push_back(i);
2279
}
2280
}
2281
2282
sorted_command_indices.clear();
2283
while (!command_stack.is_empty()) {
2284
// Pop command from the stack.
2285
command_index = command_stack[command_stack.size() - 1];
2286
command_stack.resize(command_stack.size() - 1);
2287
2288
// Add it to the sorted commands.
2289
sorted_command_indices.push_back(command_index);
2290
2291
// Search for its adjacents and lower their degree for every visit. If the degree reaches zero, we push the command to the stack.
2292
const uint32_t command_data_offset = command_data_offsets[command_index];
2293
const RecordedCommand &recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);
2294
adjacency_list_index = recorded_command.adjacent_command_list_index;
2295
while (adjacency_list_index >= 0) {
2296
const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];
2297
uint32_t &command_degree = command_degrees[command_list_node.command_index];
2298
DEV_ASSERT(command_degree > 0);
2299
command_degree--;
2300
if (command_degree == 0) {
2301
command_stack.push_back(command_list_node.command_index);
2302
}
2303
2304
adjacency_list_index = command_list_node.next_list_index;
2305
}
2306
}
2307
2308
// Batch buffer, texture, draw lists and compute operations together.
2309
const uint32_t PriorityTable[RecordedCommand::TYPE_MAX] = {
2310
0, // TYPE_NONE
2311
1, // TYPE_BUFFER_CLEAR
2312
1, // TYPE_BUFFER_COPY
2313
1, // TYPE_BUFFER_GET_DATA
2314
1, // TYPE_BUFFER_UPDATE
2315
4, // TYPE_COMPUTE_LIST
2316
3, // TYPE_DRAW_LIST
2317
2, // TYPE_TEXTURE_CLEAR
2318
2, // TYPE_TEXTURE_COPY
2319
2, // TYPE_TEXTURE_GET_DATA
2320
2, // TYPE_TEXTURE_RESOLVE
2321
2, // TYPE_TEXTURE_UPDATE
2322
2, // TYPE_CAPTURE_TIMESTAMP
2323
5, // TYPE_DRIVER_CALLBACK
2324
};
2325
2326
commands_sorted.clear();
2327
commands_sorted.resize(command_count);
2328
2329
for (uint32_t i = 0; i < command_count; i++) {
2330
const int32_t sorted_command_index = sorted_command_indices[i];
2331
const uint32_t command_data_offset = command_data_offsets[sorted_command_index];
2332
const RecordedCommand recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);
2333
const uint32_t next_command_level = commands_sorted[sorted_command_index].level + 1;
2334
adjacency_list_index = recorded_command.adjacent_command_list_index;
2335
while (adjacency_list_index >= 0) {
2336
const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];
2337
uint32_t &adjacent_command_level = commands_sorted[command_list_node.command_index].level;
2338
if (adjacent_command_level < next_command_level) {
2339
adjacent_command_level = next_command_level;
2340
}
2341
2342
adjacency_list_index = command_list_node.next_list_index;
2343
}
2344
2345
commands_sorted[sorted_command_index].index = sorted_command_index;
2346
commands_sorted[sorted_command_index].priority = PriorityTable[recorded_command.type];
2347
}
2348
} else {
2349
commands_sorted.clear();
2350
commands_sorted.resize(command_count);
2351
2352
for (uint32_t i = 0; i < command_count; i++) {
2353
commands_sorted[i].index = i;
2354
}
2355
}
2356
2357
_wait_for_secondary_command_buffer_tasks();
2358
2359
if (command_count > 0) {
2360
int32_t current_label_index = -1;
2361
int32_t current_label_level = -1;
2362
_run_label_command_change(r_command_buffer, -1, -1, true, true, nullptr, 0, current_label_index, current_label_level);
2363
2364
if (device.workarounds.avoid_compute_after_draw) {
2365
// Reset the state of the workaround.
2366
workarounds_state.draw_list_found = false;
2367
}
2368
2369
if (p_reorder_commands) {
2370
#if PRINT_RENDER_GRAPH
2371
print_line("BEFORE SORT");
2372
_print_render_commands(commands_sorted.ptr(), command_count);
2373
#endif
2374
2375
commands_sorted.sort();
2376
2377
#if PRINT_RENDER_GRAPH
2378
print_line("AFTER SORT");
2379
_print_render_commands(commands_sorted.ptr(), command_count);
2380
#endif
2381
2382
#if PRINT_COMMAND_RECORDING
2383
print_line(vformat("Recording %d commands", command_count));
2384
#endif
2385
2386
uint32_t boosted_priority = 0;
2387
uint32_t current_level = commands_sorted[0].level;
2388
uint32_t current_level_start = 0;
2389
for (uint32_t i = 0; i < command_count; i++) {
2390
if (current_level != commands_sorted[i].level) {
2391
RecordedCommandSort *level_command_ptr = &commands_sorted[current_level_start];
2392
uint32_t level_command_count = i - current_level_start;
2393
_boost_priority_for_render_commands(level_command_ptr, level_command_count, boosted_priority);
2394
_group_barriers_for_render_commands(r_command_buffer, level_command_ptr, level_command_count, p_full_barriers);
2395
_run_render_commands(current_level, level_command_ptr, level_command_count, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);
2396
current_level = commands_sorted[i].level;
2397
current_level_start = i;
2398
}
2399
}
2400
2401
RecordedCommandSort *level_command_ptr = &commands_sorted[current_level_start];
2402
uint32_t level_command_count = command_count - current_level_start;
2403
_boost_priority_for_render_commands(level_command_ptr, level_command_count, boosted_priority);
2404
_group_barriers_for_render_commands(r_command_buffer, level_command_ptr, level_command_count, p_full_barriers);
2405
_run_render_commands(current_level, level_command_ptr, level_command_count, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);
2406
2407
#if PRINT_RENDER_GRAPH
2408
print_line("COMMANDS", command_count, "LEVELS", current_level + 1);
2409
#endif
2410
} else {
2411
for (uint32_t i = 0; i < command_count; i++) {
2412
_group_barriers_for_render_commands(r_command_buffer, &commands_sorted[i], 1, p_full_barriers);
2413
_run_render_commands(i, &commands_sorted[i], 1, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);
2414
}
2415
}
2416
2417
_run_label_command_change(r_command_buffer, -1, -1, false, false, nullptr, 0, current_label_index, current_label_level);
2418
2419
#if PRINT_COMMAND_RECORDING
2420
print_line(vformat("Recorded %d commands", command_count));
2421
#endif
2422
}
2423
2424
// Advance the frame counter. It's not necessary to do this if no commands are recorded because that means no secondary command buffers were used.
2425
frame = (frame + 1) % frames.size();
2426
}
2427
2428
#if PRINT_RESOURCE_TRACKER_TOTAL
2429
static uint32_t resource_tracker_total = 0;
2430
#endif
2431
2432
RenderingDeviceGraph::ResourceTracker *RenderingDeviceGraph::resource_tracker_create() {
2433
#if PRINT_RESOURCE_TRACKER_TOTAL
2434
print_line("Resource trackers:", ++resource_tracker_total);
2435
#endif
2436
return memnew(ResourceTracker);
2437
}
2438
2439
void RenderingDeviceGraph::resource_tracker_free(ResourceTracker *p_tracker) {
2440
if (p_tracker == nullptr) {
2441
return;
2442
}
2443
2444
if (p_tracker->in_parent_dirty_list) {
2445
// Delete the tracker from the parent's dirty linked list.
2446
if (p_tracker->parent->dirty_shared_list == p_tracker) {
2447
p_tracker->parent->dirty_shared_list = p_tracker->next_shared;
2448
} else {
2449
ResourceTracker *node = p_tracker->parent->dirty_shared_list;
2450
while (node != nullptr) {
2451
if (node->next_shared == p_tracker) {
2452
node->next_shared = p_tracker->next_shared;
2453
node = nullptr;
2454
} else {
2455
node = node->next_shared;
2456
}
2457
}
2458
}
2459
}
2460
2461
memdelete(p_tracker);
2462
2463
#if PRINT_RESOURCE_TRACKER_TOTAL
2464
print_line("Resource trackers:", --resource_tracker_total);
2465
#endif
2466
}
2467
2468
RenderingDeviceGraph::FramebufferCache *RenderingDeviceGraph::framebuffer_cache_create() {
2469
return memnew(FramebufferCache);
2470
}
2471
2472
void RenderingDeviceGraph::framebuffer_cache_free(RDD *p_driver, FramebufferCache *p_cache) {
2473
DEV_ASSERT(p_driver != nullptr);
2474
2475
if (p_cache == nullptr) {
2476
return;
2477
}
2478
2479
for (KeyValue<uint64_t, FramebufferStorage> &E : p_cache->storage_map) {
2480
p_driver->framebuffer_free(E.value.framebuffer);
2481
p_driver->render_pass_free(E.value.render_pass);
2482
}
2483
2484
memdelete(p_cache);
2485
}
2486
2487