| 1 | /**************************************************************************/ |
| 2 | /* rendering_device.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.h" |
| 32 | #include "rendering_device.compat.inc" |
| 33 | |
| 34 | #include "rendering_device_binds.h" |
| 35 | #include "shader_include_db.h" |
| 36 | |
| 37 | #include "core/config/project_settings.h" |
| 38 | #include "core/io/dir_access.h" |
| 39 | #include "core/io/file_access.h" |
| 40 | #include "core/profiling/profiling.h" |
| 41 | #include "core/templates/fixed_vector.h" |
| 42 | #include "modules/modules_enabled.gen.h" |
| 43 | #include "servers/rendering/rendering_shader_container.h" |
| 44 | |
| 45 | #ifdef MODULE_GLSLANG_ENABLED |
| 46 | #include "modules/glslang/shader_compile.h" |
| 47 | #endif |
| 48 | |
| 49 | #define FORCE_SEPARATE_PRESENT_QUEUE 0 |
| 50 | #define PRINT_FRAMEBUFFER_FORMAT 0 |
| 51 | |
| 52 | #define ERR_RENDER_THREAD_MSG String("This function (") + String(__func__) + String(") can only be called from the render thread. ") |
| 53 | #define ERR_RENDER_THREAD_GUARD() ERR_FAIL_COND_MSG(render_thread_id != Thread::get_caller_id(), ERR_RENDER_THREAD_MSG); |
| 54 | #define ERR_RENDER_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(render_thread_id != Thread::get_caller_id(), (m_ret), ERR_RENDER_THREAD_MSG); |
| 55 | |
| 56 | /**************************/ |
| 57 | /**** HELPER FUNCTIONS ****/ |
| 58 | /**************************/ |
| 59 | |
| 60 | static String _get_device_vendor_name(const RenderingContextDriver::Device &p_device) { |
| 61 | switch (p_device.vendor) { |
| 62 | case RenderingContextDriver::Vendor::VENDOR_AMD: |
| 63 | return "AMD"; |
| 64 | case RenderingContextDriver::Vendor::VENDOR_IMGTEC: |
| 65 | return "ImgTec"; |
| 66 | case RenderingContextDriver::Vendor::VENDOR_APPLE: |
| 67 | return "Apple"; |
| 68 | case RenderingContextDriver::Vendor::VENDOR_NVIDIA: |
| 69 | return "NVIDIA"; |
| 70 | case RenderingContextDriver::Vendor::VENDOR_ARM: |
| 71 | return "ARM"; |
| 72 | case RenderingContextDriver::Vendor::VENDOR_MICROSOFT: |
| 73 | return "Microsoft"; |
| 74 | case RenderingContextDriver::Vendor::VENDOR_QUALCOMM: |
| 75 | return "Qualcomm"; |
| 76 | case RenderingContextDriver::Vendor::VENDOR_INTEL: |
| 77 | return "Intel"; |
| 78 | default: |
| 79 | return "Unknown"; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static String _get_device_type_name(const RenderingContextDriver::Device &p_device) { |
| 84 | switch (p_device.type) { |
| 85 | case RenderingContextDriver::DEVICE_TYPE_INTEGRATED_GPU: |
| 86 | return "Integrated"; |
| 87 | case RenderingContextDriver::DEVICE_TYPE_DISCRETE_GPU: |
| 88 | return "Discrete"; |
| 89 | case RenderingContextDriver::DEVICE_TYPE_VIRTUAL_GPU: |
| 90 | return "Virtual"; |
| 91 | case RenderingContextDriver::DEVICE_TYPE_CPU: |
| 92 | return "CPU"; |
| 93 | case RenderingContextDriver::DEVICE_TYPE_OTHER: |
| 94 | default: |
| 95 | return "Other"; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static uint32_t _get_device_type_score(const RenderingContextDriver::Device &p_device) { |
| 100 | static const bool prefer_integrated = OS::get_singleton()->get_user_prefers_integrated_gpu(); |
| 101 | switch (p_device.type) { |
| 102 | case RenderingContextDriver::DEVICE_TYPE_INTEGRATED_GPU: |
| 103 | return prefer_integrated ? 5 : 4; |
| 104 | case RenderingContextDriver::DEVICE_TYPE_DISCRETE_GPU: |
| 105 | return prefer_integrated ? 4 : 5; |
| 106 | case RenderingContextDriver::DEVICE_TYPE_VIRTUAL_GPU: |
| 107 | return 3; |
| 108 | case RenderingContextDriver::DEVICE_TYPE_CPU: |
| 109 | return 2; |
| 110 | case RenderingContextDriver::DEVICE_TYPE_OTHER: |
| 111 | default: |
| 112 | return 1; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /**************************/ |
| 117 | /**** RENDERING DEVICE ****/ |
| 118 | /**************************/ |
| 119 | |
| 120 | // When true, the command graph will attempt to reorder the rendering commands submitted by the user based on the dependencies detected from |
| 121 | // the commands automatically. This should improve rendering performance in most scenarios at the cost of some extra CPU overhead. |
| 122 | // |
| 123 | // This behavior can be disabled if it's suspected that the graph is not detecting dependencies correctly and more control over the order of |
| 124 | // the commands is desired (e.g. debugging). |
| 125 | |
| 126 | #define RENDER_GRAPH_REORDER 1 |
| 127 | |
| 128 | // Synchronization barriers are issued between the graph's levels only with the necessary amount of detail to achieve the correct result. If |
| 129 | // it's suspected that the graph is not doing this correctly, full barriers can be issued instead that will block all types of operations |
| 130 | // between the synchronization levels. This setting will have a very negative impact on performance when enabled, so it's only intended for |
| 131 | // debugging purposes. |
| 132 | |
| 133 | #define RENDER_GRAPH_FULL_BARRIERS 0 |
| 134 | |
| 135 | // The command graph can automatically issue secondary command buffers and record them on background threads when they reach an arbitrary |
| 136 | // size threshold. This can be very beneficial towards reducing the time the main thread takes to record all the rendering commands. However, |
| 137 | // this setting is not enabled by default as it's been shown to cause some strange issues with certain IHVs that have yet to be understood. |
| 138 | |
| 139 | #define SECONDARY_COMMAND_BUFFERS_PER_FRAME 0 |
| 140 | |
| 141 | RenderingDevice *RenderingDevice::singleton = nullptr; |
| 142 | |
| 143 | RenderingDevice *RenderingDevice::get_singleton() { |
| 144 | return singleton; |
| 145 | } |
| 146 | |
| 147 | /***************************/ |
| 148 | /**** ID INFRASTRUCTURE ****/ |
| 149 | /***************************/ |
| 150 | |
| 151 | void RenderingDevice::_add_dependency(RID p_id, RID p_depends_on) { |
| 152 | _THREAD_SAFE_METHOD_ |
| 153 | |
| 154 | HashSet<RID> *set = dependency_map.getptr(p_depends_on); |
| 155 | if (set == nullptr) { |
| 156 | set = &dependency_map.insert(p_depends_on, HashSet<RID>())->value; |
| 157 | } |
| 158 | set->insert(p_id); |
| 159 | |
| 160 | set = reverse_dependency_map.getptr(p_id); |
| 161 | if (set == nullptr) { |
| 162 | set = &reverse_dependency_map.insert(p_id, HashSet<RID>())->value; |
| 163 | } |
| 164 | set->insert(p_depends_on); |
| 165 | } |
| 166 | |
| 167 | void RenderingDevice::_free_dependencies(RID p_id) { |
| 168 | _THREAD_SAFE_METHOD_ |
| 169 | |
| 170 | // Direct dependencies must be freed. |
| 171 | |
| 172 | HashMap<RID, HashSet<RID>>::Iterator E = dependency_map.find(p_id); |
| 173 | if (E) { |
| 174 | while (E->value.size()) { |
| 175 | free_rid(*E->value.begin()); |
| 176 | } |
| 177 | dependency_map.remove(E); |
| 178 | } |
| 179 | |
| 180 | // Reverse dependencies must be unreferenced. |
| 181 | E = reverse_dependency_map.find(p_id); |
| 182 | |
| 183 | if (E) { |
| 184 | for (const RID &F : E->value) { |
| 185 | HashMap<RID, HashSet<RID>>::Iterator G = dependency_map.find(F); |
| 186 | ERR_CONTINUE(!G); |
| 187 | ERR_CONTINUE(!G->value.has(p_id)); |
| 188 | G->value.erase(p_id); |
| 189 | } |
| 190 | |
| 191 | reverse_dependency_map.remove(E); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /*******************************/ |
| 196 | /**** SHADER INFRASTRUCTURE ****/ |
| 197 | /*******************************/ |
| 198 | |
| 199 | Vector<uint8_t> RenderingDevice::shader_compile_spirv_from_source(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language, String *r_error, bool p_allow_cache) { |
| 200 | switch (p_language) { |
| 201 | #ifdef MODULE_GLSLANG_ENABLED |
| 202 | case ShaderLanguage::SHADER_LANGUAGE_GLSL: { |
| 203 | ShaderLanguageVersion language_version = driver->get_shader_container_format().get_shader_language_version(); |
| 204 | ShaderSpirvVersion spirv_version = driver->get_shader_container_format().get_shader_spirv_version(); |
| 205 | return compile_glslang_shader(p_stage, ShaderIncludeDB::parse_include_files(p_source_code), language_version, spirv_version, r_error); |
| 206 | } |
| 207 | #endif |
| 208 | default: |
| 209 | ERR_FAIL_V_MSG(Vector<uint8_t>(), "Shader language is not supported."); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | RID RenderingDevice::shader_create_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv, const String &p_shader_name) { |
| 214 | Vector<uint8_t> bytecode = shader_compile_binary_from_spirv(p_spirv, p_shader_name); |
| 215 | ERR_FAIL_COND_V(bytecode.is_empty(), RID()); |
| 216 | return shader_create_from_bytecode(bytecode); |
| 217 | } |
| 218 | |
| 219 | /***************************/ |
| 220 | /**** BUFFER MANAGEMENT ****/ |
| 221 | /***************************/ |
| 222 | |
| 223 | RenderingDevice::Buffer *RenderingDevice::_get_buffer_from_owner(RID p_buffer) { |
| 224 | Buffer *buffer = nullptr; |
| 225 | if (vertex_buffer_owner.owns(p_buffer)) { |
| 226 | buffer = vertex_buffer_owner.get_or_null(p_buffer); |
| 227 | } else if (index_buffer_owner.owns(p_buffer)) { |
| 228 | buffer = index_buffer_owner.get_or_null(p_buffer); |
| 229 | } else if (uniform_buffer_owner.owns(p_buffer)) { |
| 230 | buffer = uniform_buffer_owner.get_or_null(p_buffer); |
| 231 | } else if (texture_buffer_owner.owns(p_buffer)) { |
| 232 | DEV_ASSERT(false && "FIXME: Broken."); |
| 233 | //buffer = texture_buffer_owner.get_or_null(p_buffer)->buffer; |
| 234 | } else if (storage_buffer_owner.owns(p_buffer)) { |
| 235 | buffer = storage_buffer_owner.get_or_null(p_buffer); |
| 236 | } |
| 237 | return buffer; |
| 238 | } |
| 239 | |
| 240 | Error RenderingDevice::_buffer_initialize(Buffer *p_buffer, Span<uint8_t> p_data, uint32_t p_required_align) { |
| 241 | uint32_t transfer_worker_offset; |
| 242 | TransferWorker *transfer_worker = _acquire_transfer_worker(p_data.size(), p_required_align, transfer_worker_offset); |
| 243 | p_buffer->transfer_worker_index = transfer_worker->index; |
| 244 | |
| 245 | { |
| 246 | MutexLock lock(transfer_worker->operations_mutex); |
| 247 | p_buffer->transfer_worker_operation = ++transfer_worker->operations_counter; |
| 248 | } |
| 249 | |
| 250 | // Copy to the worker's staging buffer. |
| 251 | uint8_t *data_ptr = driver->buffer_map(transfer_worker->staging_buffer); |
| 252 | ERR_FAIL_NULL_V(data_ptr, ERR_CANT_CREATE); |
| 253 | |
| 254 | memcpy(data_ptr + transfer_worker_offset, p_data.ptr(), p_data.size()); |
| 255 | driver->buffer_unmap(transfer_worker->staging_buffer); |
| 256 | |
| 257 | // Copy from the staging buffer to the real buffer. |
| 258 | RDD::BufferCopyRegion region; |
| 259 | region.src_offset = transfer_worker_offset; |
| 260 | region.dst_offset = 0; |
| 261 | region.size = p_data.size(); |
| 262 | driver->command_copy_buffer(transfer_worker->command_buffer, transfer_worker->staging_buffer, p_buffer->driver_id, region); |
| 263 | |
| 264 | _release_transfer_worker(transfer_worker); |
| 265 | |
| 266 | return OK; |
| 267 | } |
| 268 | |
| 269 | Error RenderingDevice::_insert_staging_block(StagingBuffers &p_staging_buffers) { |
| 270 | StagingBufferBlock block; |
| 271 | |
| 272 | block.driver_id = driver->buffer_create(p_staging_buffers.block_size, p_staging_buffers.usage_bits, RDD::MEMORY_ALLOCATION_TYPE_CPU, frames_drawn); |
| 273 | ERR_FAIL_COND_V(!block.driver_id, ERR_CANT_CREATE); |
| 274 | |
| 275 | block.frame_used = 0; |
| 276 | block.fill_amount = 0; |
| 277 | block.data_ptr = driver->buffer_map(block.driver_id); |
| 278 | |
| 279 | if (block.data_ptr == nullptr) { |
| 280 | driver->buffer_free(block.driver_id); |
| 281 | return ERR_CANT_CREATE; |
| 282 | } |
| 283 | |
| 284 | p_staging_buffers.blocks.insert(p_staging_buffers.current, block); |
| 285 | return OK; |
| 286 | } |
| 287 | |
| 288 | Error RenderingDevice::_staging_buffer_allocate(StagingBuffers &p_staging_buffers, uint32_t p_amount, uint32_t p_required_align, uint32_t &r_alloc_offset, uint32_t &r_alloc_size, StagingRequiredAction &r_required_action, bool p_can_segment) { |
| 289 | // Determine a block to use. |
| 290 | |
| 291 | r_alloc_size = p_amount; |
| 292 | r_required_action = STAGING_REQUIRED_ACTION_NONE; |
| 293 | |
| 294 | while (true) { |
| 295 | r_alloc_offset = 0; |
| 296 | |
| 297 | // See if we can use current block. |
| 298 | if (p_staging_buffers.blocks[p_staging_buffers.current].frame_used == frames_drawn) { |
| 299 | // We used this block this frame, let's see if there is still room. |
| 300 | |
| 301 | uint32_t write_from = p_staging_buffers.blocks[p_staging_buffers.current].fill_amount; |
| 302 | |
| 303 | { |
| 304 | uint32_t align_remainder = write_from % p_required_align; |
| 305 | if (align_remainder != 0) { |
| 306 | write_from += p_required_align - align_remainder; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | int32_t available_bytes = int32_t(p_staging_buffers.block_size) - int32_t(write_from); |
| 311 | |
| 312 | if ((int32_t)p_amount < available_bytes) { |
| 313 | // All is good, we should be ok, all will fit. |
| 314 | r_alloc_offset = write_from; |
| 315 | } else if (p_can_segment && available_bytes >= (int32_t)p_required_align) { |
| 316 | // Ok all won't fit but at least we can fit a chunkie. |
| 317 | // All is good, update what needs to be written to. |
| 318 | r_alloc_offset = write_from; |
| 319 | r_alloc_size = available_bytes - (available_bytes % p_required_align); |
| 320 | |
| 321 | } else { |
| 322 | // Can't fit it into this buffer. |
| 323 | // Will need to try next buffer. |
| 324 | |
| 325 | p_staging_buffers.current = (p_staging_buffers.current + 1) % p_staging_buffers.blocks.size(); |
| 326 | |
| 327 | // Before doing anything, though, let's check that we didn't manage to fill all blocks. |
| 328 | // Possible in a single frame. |
| 329 | if (p_staging_buffers.blocks[p_staging_buffers.current].frame_used == frames_drawn) { |
| 330 | // Guess we did.. ok, let's see if we can insert a new block. |
| 331 | if ((uint64_t)p_staging_buffers.blocks.size() * p_staging_buffers.block_size < p_staging_buffers.max_size) { |
| 332 | // We can, so we are safe. |
| 333 | Error err = _insert_staging_block(p_staging_buffers); |
| 334 | if (err) { |
| 335 | return err; |
| 336 | } |
| 337 | // Claim for this frame. |
| 338 | p_staging_buffers.blocks.write[p_staging_buffers.current].frame_used = frames_drawn; |
| 339 | } else { |
| 340 | // Ok, worst case scenario, all the staging buffers belong to this frame |
| 341 | // and this frame is not even done. |
| 342 | // If this is the main thread, it means the user is likely loading a lot of resources at once,. |
| 343 | // Otherwise, the thread should just be blocked until the next frame (currently unimplemented). |
| 344 | r_required_action = STAGING_REQUIRED_ACTION_FLUSH_AND_STALL_ALL; |
| 345 | } |
| 346 | |
| 347 | } else { |
| 348 | // Not from current frame, so continue and try again. |
| 349 | continue; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | } else if (p_staging_buffers.blocks[p_staging_buffers.current].frame_used <= frames_drawn - frames.size()) { |
| 354 | // This is an old block, which was already processed, let's reuse. |
| 355 | p_staging_buffers.blocks.write[p_staging_buffers.current].frame_used = frames_drawn; |
| 356 | p_staging_buffers.blocks.write[p_staging_buffers.current].fill_amount = 0; |
| 357 | } else { |
| 358 | // This block may still be in use, let's not touch it unless we have to, so.. can we create a new one? |
| 359 | if ((uint64_t)p_staging_buffers.blocks.size() * p_staging_buffers.block_size < p_staging_buffers.max_size) { |
| 360 | // We are still allowed to create a new block, so let's do that and insert it for current pos. |
| 361 | Error err = _insert_staging_block(p_staging_buffers); |
| 362 | if (err) { |
| 363 | return err; |
| 364 | } |
| 365 | // Claim for this frame. |
| 366 | p_staging_buffers.blocks.write[p_staging_buffers.current].frame_used = frames_drawn; |
| 367 | } else { |
| 368 | // Oops, we are out of room and we can't create more. |
| 369 | // Let's flush older frames. |
| 370 | // The logic here is that if a game is loading a lot of data from the main thread, it will need to be stalled anyway. |
| 371 | // If loading from a separate thread, we can block that thread until next frame when more room is made (not currently implemented, though). |
| 372 | r_required_action = STAGING_REQUIRED_ACTION_STALL_PREVIOUS; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // All was good, break. |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | p_staging_buffers.used = true; |
| 381 | |
| 382 | return OK; |
| 383 | } |
| 384 | |
| 385 | void RenderingDevice::_staging_buffer_execute_required_action(StagingBuffers &p_staging_buffers, StagingRequiredAction p_required_action) { |
| 386 | switch (p_required_action) { |
| 387 | case STAGING_REQUIRED_ACTION_NONE: { |
| 388 | // Do nothing. |
| 389 | } break; |
| 390 | case STAGING_REQUIRED_ACTION_FLUSH_AND_STALL_ALL: { |
| 391 | _flush_and_stall_for_all_frames(); |
| 392 | |
| 393 | // Clear the whole staging buffer. |
| 394 | for (int i = 0; i < p_staging_buffers.blocks.size(); i++) { |
| 395 | p_staging_buffers.blocks.write[i].frame_used = 0; |
| 396 | p_staging_buffers.blocks.write[i].fill_amount = 0; |
| 397 | } |
| 398 | |
| 399 | // Claim for current frame. |
| 400 | p_staging_buffers.blocks.write[p_staging_buffers.current].frame_used = frames_drawn; |
| 401 | } break; |
| 402 | case STAGING_REQUIRED_ACTION_STALL_PREVIOUS: { |
| 403 | _stall_for_previous_frames(); |
| 404 | |
| 405 | for (int i = 0; i < p_staging_buffers.blocks.size(); i++) { |
| 406 | // Clear all blocks but the ones from this frame. |
| 407 | int block_idx = (i + p_staging_buffers.current) % p_staging_buffers.blocks.size(); |
| 408 | if (p_staging_buffers.blocks[block_idx].frame_used == frames_drawn) { |
| 409 | break; // Ok, we reached something from this frame, abort. |
| 410 | } |
| 411 | |
| 412 | p_staging_buffers.blocks.write[block_idx].frame_used = 0; |
| 413 | p_staging_buffers.blocks.write[block_idx].fill_amount = 0; |
| 414 | } |
| 415 | |
| 416 | // Claim for current frame. |
| 417 | p_staging_buffers.blocks.write[p_staging_buffers.current].frame_used = frames_drawn; |
| 418 | } break; |
| 419 | default: { |
| 420 | DEV_ASSERT(false && "Unknown required action."); |
| 421 | } break; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | Error RenderingDevice::buffer_copy(RID p_src_buffer, RID p_dst_buffer, uint32_t p_src_offset, uint32_t p_dst_offset, uint32_t p_size) { |
| 426 | ERR_RENDER_THREAD_GUARD_V(ERR_UNAVAILABLE); |
| 427 | |
| 428 | ERR_FAIL_COND_V_MSG(draw_list.active, ERR_INVALID_PARAMETER, |
| 429 | "Copying buffers is forbidden during creation of a draw list"); |
| 430 | ERR_FAIL_COND_V_MSG(compute_list.active, ERR_INVALID_PARAMETER, |
| 431 | "Copying buffers is forbidden during creation of a compute list"); |
| 432 | |
| 433 | Buffer *src_buffer = _get_buffer_from_owner(p_src_buffer); |
| 434 | if (!src_buffer) { |
| 435 | ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Source buffer argument is not a valid buffer of any type."); |
| 436 | } |
| 437 | |
| 438 | Buffer *dst_buffer = _get_buffer_from_owner(p_dst_buffer); |
| 439 | if (!dst_buffer) { |
| 440 | ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Destination buffer argument is not a valid buffer of any type."); |
| 441 | } |
| 442 | |
| 443 | // Validate the copy's dimensions for both buffers. |
| 444 | ERR_FAIL_COND_V_MSG((p_size + p_src_offset) > src_buffer->size, ERR_INVALID_PARAMETER, "Size is larger than the source buffer."); |
| 445 | ERR_FAIL_COND_V_MSG((p_size + p_dst_offset) > dst_buffer->size, ERR_INVALID_PARAMETER, "Size is larger than the destination buffer."); |
| 446 | |
| 447 | _check_transfer_worker_buffer(src_buffer); |
| 448 | _check_transfer_worker_buffer(dst_buffer); |
| 449 | |
| 450 | // Perform the copy. |
| 451 | RDD::BufferCopyRegion region; |
| 452 | region.src_offset = p_src_offset; |
| 453 | region.dst_offset = p_dst_offset; |
| 454 | region.size = p_size; |
| 455 | |
| 456 | if (_buffer_make_mutable(dst_buffer, p_dst_buffer)) { |
| 457 | // The destination buffer must be mutable to be used as a copy destination. |
| 458 | draw_graph.add_synchronization(); |
| 459 | } |
| 460 | |
| 461 | draw_graph.add_buffer_copy(src_buffer->driver_id, src_buffer->draw_tracker, dst_buffer->driver_id, dst_buffer->draw_tracker, region); |
| 462 | |
| 463 | return OK; |
| 464 | } |
| 465 | |
| 466 | Error RenderingDevice::buffer_update(RID p_buffer, uint32_t p_offset, uint32_t p_size, const void *p_data, bool p_skip_check) { |
| 467 | ERR_RENDER_THREAD_GUARD_V(ERR_UNAVAILABLE); |
| 468 | |
| 469 | copy_bytes_count += p_size; |
| 470 | |
| 471 | ERR_FAIL_COND_V_MSG(draw_list.active && !p_skip_check, ERR_INVALID_PARAMETER, |
| 472 | "Updating buffers is forbidden during creation of a draw list"); |
| 473 | ERR_FAIL_COND_V_MSG(compute_list.active && !p_skip_check, ERR_INVALID_PARAMETER, |
| 474 | "Updating buffers is forbidden during creation of a compute list"); |
| 475 | |
| 476 | Buffer *buffer = _get_buffer_from_owner(p_buffer); |
| 477 | ERR_FAIL_NULL_V_MSG(buffer, ERR_INVALID_PARAMETER, "Buffer argument is not a valid buffer of any type."); |
| 478 | ERR_FAIL_COND_V_MSG(p_offset + p_size > buffer->size, ERR_INVALID_PARAMETER, "Attempted to write buffer (" + itos((p_offset + p_size) - buffer->size) + " bytes) past the end."); |
| 479 | |
| 480 | if (buffer->usage.has_flag(RDD::BUFFER_USAGE_DYNAMIC_PERSISTENT_BIT)) { |
| 481 | uint8_t *dst_data = driver->buffer_persistent_map_advance(buffer->driver_id, frames_drawn); |
| 482 | |
| 483 | memcpy(dst_data + p_offset, p_data, p_size); |
| 484 | direct_copy_count++; |
| 485 | buffer_flush(p_buffer); |
| 486 | return OK; |
| 487 | } |
| 488 | |
| 489 | _check_transfer_worker_buffer(buffer); |
| 490 | |
| 491 | // Submitting may get chunked for various reasons, so convert this to a task. |
| 492 | size_t to_submit = p_size; |
| 493 | size_t submit_from = 0; |
| 494 | |
| 495 | thread_local LocalVector<RDG::RecordedBufferCopy> command_buffer_copies_vector; |
| 496 | command_buffer_copies_vector.clear(); |
| 497 | |
| 498 | const uint8_t *src_data = reinterpret_cast<const uint8_t *>(p_data); |
| 499 | const uint32_t required_align = 32; |
| 500 | while (to_submit > 0) { |
| 501 | uint32_t block_write_offset; |
| 502 | uint32_t block_write_amount; |
| 503 | StagingRequiredAction required_action; |
| 504 | |
| 505 | Error err = _staging_buffer_allocate(upload_staging_buffers, MIN(to_submit, upload_staging_buffers.block_size), required_align, block_write_offset, block_write_amount, required_action); |
| 506 | if (err) { |
| 507 | return err; |
| 508 | } |
| 509 | |
| 510 | if (!command_buffer_copies_vector.is_empty() && required_action == STAGING_REQUIRED_ACTION_FLUSH_AND_STALL_ALL) { |
| 511 | if (_buffer_make_mutable(buffer, p_buffer)) { |
| 512 | // The buffer must be mutable to be used as a copy destination. |
| 513 | draw_graph.add_synchronization(); |
| 514 | } |
| 515 | |
| 516 | draw_graph.add_buffer_update(buffer->driver_id, buffer->draw_tracker, command_buffer_copies_vector); |
| 517 | command_buffer_copies_vector.clear(); |
| 518 | } |
| 519 | |
| 520 | _staging_buffer_execute_required_action(upload_staging_buffers, required_action); |
| 521 | |
| 522 | // Copy to staging buffer. |
| 523 | memcpy(upload_staging_buffers.blocks[upload_staging_buffers.current].data_ptr + block_write_offset, src_data + submit_from, block_write_amount); |
| 524 | |
| 525 | // Insert a command to copy this. |
| 526 | RDD::BufferCopyRegion region; |
| 527 | region.src_offset = block_write_offset; |
| 528 | region.dst_offset = submit_from + p_offset; |
| 529 | region.size = block_write_amount; |
| 530 | |
| 531 | RDG::RecordedBufferCopy buffer_copy; |
| 532 | buffer_copy.source = upload_staging_buffers.blocks[upload_staging_buffers.current].driver_id; |
| 533 | buffer_copy.region = region; |
| 534 | command_buffer_copies_vector.push_back(buffer_copy); |
| 535 | |
| 536 | upload_staging_buffers.blocks.write[upload_staging_buffers.current].fill_amount = block_write_offset + block_write_amount; |
| 537 | |
| 538 | to_submit -= block_write_amount; |
| 539 | submit_from += block_write_amount; |
| 540 | } |
| 541 | |
| 542 | if (!command_buffer_copies_vector.is_empty()) { |
| 543 | if (_buffer_make_mutable(buffer, p_buffer)) { |
| 544 | // The buffer must be mutable to be used as a copy destination. |
| 545 | draw_graph.add_synchronization(); |
| 546 | } |
| 547 | |
| 548 | draw_graph.add_buffer_update(buffer->driver_id, buffer->draw_tracker, command_buffer_copies_vector); |
| 549 | } |
| 550 | |
| 551 | gpu_copy_count++; |
| 552 | |
| 553 | return OK; |
| 554 | } |
| 555 | |
| 556 | Error RenderingDevice::driver_callback_add(RDD::DriverCallback p_callback, void *p_userdata, VectorView<CallbackResource> p_resources) { |
| 557 | ERR_RENDER_THREAD_GUARD_V(ERR_UNAVAILABLE); |
| 558 | |
| 559 | ERR_FAIL_COND_V_MSG(draw_list.active, ERR_INVALID_PARAMETER, |
| 560 | "Driver callback is forbidden during creation of a draw list"); |
| 561 | ERR_FAIL_COND_V_MSG(compute_list.active, ERR_INVALID_PARAMETER, |
| 562 | "Driver callback is forbidden during creation of a compute list"); |
| 563 | |
| 564 | thread_local LocalVector<RDG::ResourceTracker *> trackers; |
| 565 | thread_local LocalVector<RDG::ResourceUsage> usages; |
| 566 | |
| 567 | uint32_t resource_count = p_resources.size(); |
| 568 | trackers.resize(resource_count); |
| 569 | usages.resize(resource_count); |
| 570 | |
| 571 | if (resource_count > 0) { |
| 572 | for (uint32_t i = 0; i < p_resources.size(); i++) { |
| 573 | const CallbackResource &cr = p_resources[i]; |
| 574 | switch (cr.type) { |
| 575 | case CALLBACK_RESOURCE_TYPE_BUFFER: { |
| 576 | Buffer *buffer = _get_buffer_from_owner(cr.rid); |
| 577 | if (!buffer) { |
| 578 | ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, vformat("Argument %d is not a valid buffer of any type.", i)); |
| 579 | } |
| 580 | if (_buffer_make_mutable(buffer, cr.rid)) { |
| 581 | draw_graph.add_synchronization(); |
| 582 | } |
| 583 | trackers[i] = buffer->draw_tracker; |
| 584 | usages[i] = (RDG::ResourceUsage)cr.usage; |
| 585 | } break; |
| 586 | case CALLBACK_RESOURCE_TYPE_TEXTURE: { |
| 587 | Texture *texture = texture_owner.get_or_null(cr.rid); |
| 588 | if (!texture) { |
| 589 | ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, vformat("Argument %d is not a valid texture.", i)); |
| 590 | } |
| 591 | if (_texture_make_mutable(texture, cr.rid)) { |
| 592 | draw_graph.add_synchronization(); |
| 593 | } |
| 594 | trackers[i] = texture->draw_tracker; |
| 595 | usages[i] = (RDG::ResourceUsage)cr.usage; |
| 596 | } break; |
| 597 | default: { |
| 598 | CRASH_NOW_MSG("Invalid callback resource type."); |
| 599 | } break; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | draw_graph.add_driver_callback(p_callback, p_userdata, trackers, usages); |
| 605 | |
| 606 | return OK; |
| 607 | } |
| 608 | |
| 609 | String RenderingDevice::get_perf_report() const { |
| 610 | String perf_report_text; |
| 611 | perf_report_text += " gpu:" + String::num_int64(gpu_copy_count); |
| 612 | perf_report_text += " direct:" + String::num_int64(direct_copy_count); |
| 613 | perf_report_text += " bytes:" + String::num_int64(copy_bytes_count); |
| 614 | |
| 615 | perf_report_text += " lazily alloc:" + String::num_int64(driver->get_lazily_memory_used()); |
| 616 | return perf_report_text; |
| 617 | } |
| 618 | |
| 619 | void RenderingDevice::update_perf_report() { |
| 620 | prev_gpu_copy_count = gpu_copy_count; |
| 621 | prev_copy_bytes_count = copy_bytes_count; |
| 622 | gpu_copy_count = 0; |
| 623 | direct_copy_count = 0; |
| 624 | copy_bytes_count = 0; |
| 625 | } |
| 626 | |
| 627 | Error RenderingDevice::buffer_clear(RID p_buffer, uint32_t p_offset, uint32_t p_size) { |
| 628 | ERR_RENDER_THREAD_GUARD_V(ERR_UNAVAILABLE); |
| 629 | |
| 630 | ERR_FAIL_COND_V_MSG((p_size % 4) != 0, ERR_INVALID_PARAMETER, |
| 631 | "Size must be a multiple of four"); |
| 632 | ERR_FAIL_COND_V_MSG(draw_list.active, ERR_INVALID_PARAMETER, |
| 633 | "Updating buffers in is forbidden during creation of a draw list"); |
| 634 | ERR_FAIL_COND_V_MSG(compute_list.active, ERR_INVALID_PARAMETER, |
| 635 | "Updating buffers is forbidden during creation of a compute list"); |
| 636 | |
| 637 | Buffer *buffer = _get_buffer_from_owner(p_buffer); |
| 638 | if (!buffer) { |
| 639 | ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Buffer argument is not a valid buffer of any type."); |
| 640 | } |
| 641 | |
| 642 | ERR_FAIL_COND_V_MSG(p_offset + p_size > buffer->size, ERR_INVALID_PARAMETER, |
| 643 | "Attempted to write buffer (" + itos((p_offset + p_size) - buffer->size) + " bytes) past the end."); |
| 644 | |
| 645 | _check_transfer_worker_buffer(buffer); |
| 646 | |
| 647 | if (_buffer_make_mutable(buffer, p_buffer)) { |
| 648 | // The destination buffer must be mutable to be used as a clear destination. |
| 649 | draw_graph.add_synchronization(); |
| 650 | } |
| 651 | |
| 652 | draw_graph.add_buffer_clear(buffer->driver_id, buffer->draw_tracker, p_offset, p_size); |
| 653 | |
| 654 | return OK; |
| 655 | } |
| 656 | |
| 657 | Vector<uint8_t> RenderingDevice::buffer_get_data(RID p_buffer, uint32_t p_offset, uint32_t p_size) { |
| 658 | ERR_RENDER_THREAD_GUARD_V(Vector<uint8_t>()); |
| 659 | |
| 660 | Buffer *buffer = _get_buffer_from_owner(p_buffer); |
| 661 | if (!buffer) { |
| 662 | ERR_FAIL_V_MSG(Vector<uint8_t>(), "Buffer is either invalid or this type of buffer can't be retrieved."); |
| 663 | } |
| 664 | |
| 665 | // Size of buffer to retrieve. |
| 666 | if (!p_size) { |
| 667 | p_size = buffer->size; |
| 668 | } else { |
| 669 | ERR_FAIL_COND_V_MSG(p_size + p_offset > buffer->size, Vector<uint8_t>(), |
| 670 | "Size is larger than the buffer."); |
| 671 | } |
| 672 | |
| 673 | _check_transfer_worker_buffer(buffer); |
| 674 | |
| 675 | RDD::BufferID tmp_buffer = driver->buffer_create(buffer->size, RDD::BUFFER_USAGE_TRANSFER_TO_BIT, RDD::MEMORY_ALLOCATION_TYPE_CPU, frames_drawn); |
| 676 | ERR_FAIL_COND_V(!tmp_buffer, Vector<uint8_t>()); |
| 677 | |
| 678 | RDD::BufferCopyRegion region; |
| 679 | region.src_offset = p_offset; |
| 680 | region.size = p_size; |
| 681 | |
| 682 | draw_graph.add_buffer_get_data(buffer->driver_id, buffer->draw_tracker, tmp_buffer, region); |
| 683 | |
| 684 | // Flush everything so memory can be safely mapped. |
| 685 | _flush_and_stall_for_all_frames(); |
| 686 | |
| 687 | uint8_t *buffer_mem = driver->buffer_map(tmp_buffer); |
| 688 | ERR_FAIL_NULL_V(buffer_mem, Vector<uint8_t>()); |
| 689 | |
| 690 | Vector<uint8_t> buffer_data; |
| 691 | { |
| 692 | buffer_data.resize(p_size); |
| 693 | uint8_t *w = buffer_data.ptrw(); |
| 694 | memcpy(w, buffer_mem, p_size); |
| 695 | } |
| 696 | |
| 697 | driver->buffer_unmap(tmp_buffer); |
| 698 | |
| 699 | driver->buffer_free(tmp_buffer); |
| 700 | |
| 701 | return buffer_data; |
| 702 | } |
| 703 | |
| 704 | Error RenderingDevice::buffer_get_data_async(RID p_buffer, const Callable &p_callback, uint32_t p_offset, uint32_t p_size) { |
| 705 | ERR_RENDER_THREAD_GUARD_V(ERR_UNAVAILABLE); |
| 706 | |
| 707 | Buffer *buffer = _get_buffer_from_owner(p_buffer); |
| 708 | if (buffer == nullptr) { |
| 709 | ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Buffer is either invalid or this type of buffer can't be retrieved."); |
| 710 | } |
| 711 | |
| 712 | if (p_size == 0) { |
| 713 | p_size = buffer->size; |
| 714 | } |
| 715 | |
| 716 | ERR_FAIL_COND_V_MSG(p_size + p_offset > buffer->size, ERR_INVALID_PARAMETER, "Size is larger than the buffer."); |
| 717 | ERR_FAIL_COND_V_MSG(!p_callback.is_valid(), ERR_INVALID_PARAMETER, "Callback must be valid."); |
| 718 | |
| 719 | _check_transfer_worker_buffer(buffer); |
| 720 | |
| 721 | BufferGetDataRequest get_data_request; |
| 722 | get_data_request.callback = p_callback; |
| 723 | get_data_request.frame_local_index = frames[frame].download_buffer_copy_regions.size(); |
| 724 | get_data_request.size = p_size; |
| 725 | |
| 726 | const uint32_t required_align = 32; |
| 727 | uint32_t block_write_offset; |
| 728 | uint32_t block_write_amount; |
| 729 | StagingRequiredAction required_action; |
| 730 | uint32_t to_submit = p_size; |
| 731 | uint32_t submit_from = 0; |
| 732 | while (to_submit > 0) { |
| 733 | Error err = _staging_buffer_allocate(download_staging_buffers, MIN(to_submit, download_staging_buffers.block_size), required_align, block_write_offset, block_write_amount, required_action); |
| 734 | if (err) { |
| 735 | return err; |
| 736 | } |
| 737 | |
| 738 | const bool flush_frames = (get_data_request.frame_local_count > 0) && required_action == STAGING_REQUIRED_ACTION_FLUSH_AND_STALL_ALL; |
| 739 | if (flush_frames) { |
| 740 | if (_buffer_make_mutable(buffer, p_buffer)) { |
| 741 | // The buffer must be mutable to be used as a copy source. |
| 742 | draw_graph.add_synchronization(); |
| 743 | } |
| 744 | |
| 745 | for (uint32_t i = 0; i < get_data_request.frame_local_count; i++) { |
| 746 | uint32_t local_index = get_data_request.frame_local_index + i; |
| 747 | draw_graph.add_buffer_get_data(buffer->driver_id, buffer->draw_tracker, frames[frame].download_buffer_staging_buffers[local_index], frames[frame].download_buffer_copy_regions[local_index]); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | _staging_buffer_execute_required_action(download_staging_buffers, required_action); |
| 752 | |
| 753 | if (flush_frames) { |
| 754 | get_data_request.frame_local_count = 0; |
| 755 | get_data_request.frame_local_index = frames[frame].download_buffer_copy_regions.size(); |
| 756 | } |
| 757 | |
| 758 | RDD::BufferCopyRegion region; |
| 759 | region.src_offset = submit_from + p_offset; |
| 760 | region.dst_offset = block_write_offset; |
| 761 | region.size = block_write_amount; |
| 762 | |
| 763 | frames[frame].download_buffer_staging_buffers.push_back(download_staging_buffers.blocks[download_staging_buffers.current].driver_id); |
| 764 | frames[frame].download_buffer_copy_regions.push_back(region); |
| 765 | get_data_request.frame_local_count++; |
| 766 | |
| 767 | download_staging_buffers.blocks.write[download_staging_buffers.current].fill_amount = block_write_offset + block_write_amount; |
| 768 | |
| 769 | to_submit -= block_write_amount; |
| 770 | submit_from += block_write_amount; |
| 771 | } |
| 772 | |
| 773 | if (get_data_request.frame_local_count > 0) { |
| 774 | if (_buffer_make_mutable(buffer, p_buffer)) { |
| 775 | // The buffer must be mutable to be used as a copy source. |
| 776 | draw_graph.add_synchronization(); |
| 777 | } |
| 778 | |
| 779 | for (uint32_t i = 0; i < get_data_request.frame_local_count; i++) { |
| 780 | uint32_t local_index = get_data_request.frame_local_index + i; |
| 781 | draw_graph.add_buffer_get_data(buffer->driver_id, buffer->draw_tracker, frames[frame].download_buffer_staging_buffers[local_index], frames[frame].download_buffer_copy_regions[local_index]); |
| 782 | } |
| 783 | |
| 784 | frames[frame].download_buffer_get_data_requests.push_back(get_data_request); |
| 785 | } |
| 786 | |
| 787 | return OK; |
| 788 | } |
| 789 | |
| 790 | uint64_t RenderingDevice::buffer_get_device_address(RID p_buffer) { |
| 791 | ERR_RENDER_THREAD_GUARD_V(0); |
| 792 | |
| 793 | Buffer *buffer = _get_buffer_from_owner(p_buffer); |
| 794 | ERR_FAIL_NULL_V_MSG(buffer, 0, "Buffer argument is not a valid buffer of any type."); |
| 795 | ERR_FAIL_COND_V_MSG(!buffer->usage.has_flag(RDD::BUFFER_USAGE_DEVICE_ADDRESS_BIT), 0, "Buffer was not created with device address flag."); |
| 796 | |
| 797 | return driver->buffer_get_device_address(buffer->driver_id); |
| 798 | } |
| 799 | |
| 800 | uint8_t *RenderingDevice::buffer_persistent_map_advance(RID p_buffer) { |
| 801 | ERR_RENDER_THREAD_GUARD_V(0); |
| 802 | |
| 803 | Buffer *buffer = _get_buffer_from_owner(p_buffer); |
| 804 | ERR_FAIL_NULL_V_MSG(buffer, nullptr, "Buffer argument is not a valid buffer of any type."); |
| 805 | direct_copy_count++; |
| 806 | return driver->buffer_persistent_map_advance(buffer->driver_id, frames_drawn); |
| 807 | } |
| 808 | |
| 809 | void RenderingDevice::buffer_flush(RID p_buffer) { |
| 810 | ERR_RENDER_THREAD_GUARD(); |
| 811 | |
| 812 | Buffer *buffer = _get_buffer_from_owner(p_buffer); |
| 813 | ERR_FAIL_NULL_MSG(buffer, "Buffer argument is not a valid buffer of any type."); |
| 814 | driver->buffer_flush(buffer->driver_id); |
| 815 | } |
| 816 | |
| 817 | RID RenderingDevice::storage_buffer_create(uint32_t p_size_bytes, Span<uint8_t> p_data, BitField<StorageBufferUsage> p_usage, BitField<BufferCreationBits> p_creation_bits) { |
| 818 | ERR_FAIL_COND_V(p_data.size() && (uint32_t)p_data.size() != p_size_bytes, RID()); |
| 819 | |
| 820 | Buffer buffer; |
| 821 | buffer.size = p_size_bytes; |
| 822 | buffer.usage = (RDD::BUFFER_USAGE_TRANSFER_FROM_BIT | RDD::BUFFER_USAGE_TRANSFER_TO_BIT | RDD::BUFFER_USAGE_STORAGE_BIT); |
| 823 | if (p_creation_bits.has_flag(BUFFER_CREATION_DYNAMIC_PERSISTENT_BIT)) { |
| 824 | buffer.usage.set_flag(RDD::BUFFER_USAGE_DYNAMIC_PERSISTENT_BIT); |
| 825 | |
| 826 | // This is a precaution: Persistent buffers are meant for frequent CPU -> GPU transfers. |
| 827 | // Writing to this buffer from GPU might cause sync issues if both CPU & GPU try to write at the |
| 828 | // same time. It's probably fine (since CPU always advances the pointer before writing) but let's |
| 829 | // stick to the known/intended use cases and scream if we deviate from it. |
| 830 | buffer.usage.clear_flag(RDD::BUFFER_USAGE_TRANSFER_TO_BIT); |
| 831 | } |
| 832 | if (p_usage.has_flag(STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT)) { |
| 833 | buffer.usage.set_flag(RDD::BUFFER_USAGE_INDIRECT_BIT); |
| 834 | } |
| 835 | if (p_creation_bits.has_flag(BUFFER_CREATION_DEVICE_ADDRESS_BIT)) { |
| 836 | #ifdef DEBUG_ENABLED |
| 837 | ERR_FAIL_COND_V_MSG(!has_feature(SUPPORTS_BUFFER_DEVICE_ADDRESS), RID(), |
| 838 | "The GPU doesn't support buffer address flag."); |
| 839 | #endif |
| 840 | |
| 841 | buffer.usage.set_flag(RDD::BUFFER_USAGE_DEVICE_ADDRESS_BIT); |
| 842 | } |
| 843 | buffer.driver_id = driver->buffer_create(buffer.size, buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU, frames_drawn); |
| 844 | ERR_FAIL_COND_V(!buffer.driver_id, RID()); |
| 845 | |
| 846 | // Storage buffers are assumed to be mutable. |
| 847 | buffer.draw_tracker = RDG::resource_tracker_create(); |
| 848 | buffer.draw_tracker->buffer_driver_id = buffer.driver_id; |
| 849 | |
| 850 | if (p_data.size()) { |
| 851 | _buffer_initialize(&buffer, p_data); |
| 852 | } |
| 853 | |
| 854 | _THREAD_SAFE_LOCK_ |
| 855 | buffer_memory += buffer.size; |
| 856 | _THREAD_SAFE_UNLOCK_ |
| 857 | |
| 858 | RID id = storage_buffer_owner.make_rid(buffer); |
| 859 | #ifdef DEV_ENABLED |
| 860 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 861 | #endif |
| 862 | return id; |
| 863 | } |
| 864 | |
| 865 | RID RenderingDevice::texture_buffer_create(uint32_t p_size_elements, DataFormat p_format, Span<uint8_t> p_data) { |
| 866 | uint32_t element_size = get_format_vertex_size(p_format); |
| 867 | ERR_FAIL_COND_V_MSG(element_size == 0, RID(), "Format requested is not supported for texture buffers"); |
| 868 | uint64_t size_bytes = uint64_t(element_size) * p_size_elements; |
| 869 | |
| 870 | ERR_FAIL_COND_V(p_data.size() && (uint32_t)p_data.size() != size_bytes, RID()); |
| 871 | |
| 872 | Buffer texture_buffer; |
| 873 | texture_buffer.size = size_bytes; |
| 874 | BitField<RDD::BufferUsageBits> usage = (RDD::BUFFER_USAGE_TRANSFER_FROM_BIT | RDD::BUFFER_USAGE_TRANSFER_TO_BIT | RDD::BUFFER_USAGE_TEXEL_BIT); |
| 875 | texture_buffer.driver_id = driver->buffer_create(size_bytes, usage, RDD::MEMORY_ALLOCATION_TYPE_GPU, frames_drawn); |
| 876 | ERR_FAIL_COND_V(!texture_buffer.driver_id, RID()); |
| 877 | |
| 878 | // Texture buffers are assumed to be immutable unless they don't have initial data. |
| 879 | if (p_data.is_empty()) { |
| 880 | texture_buffer.draw_tracker = RDG::resource_tracker_create(); |
| 881 | texture_buffer.draw_tracker->buffer_driver_id = texture_buffer.driver_id; |
| 882 | } |
| 883 | |
| 884 | bool ok = driver->buffer_set_texel_format(texture_buffer.driver_id, p_format); |
| 885 | if (!ok) { |
| 886 | driver->buffer_free(texture_buffer.driver_id); |
| 887 | ERR_FAIL_V(RID()); |
| 888 | } |
| 889 | |
| 890 | if (p_data.size()) { |
| 891 | _buffer_initialize(&texture_buffer, p_data); |
| 892 | } |
| 893 | |
| 894 | _THREAD_SAFE_LOCK_ |
| 895 | buffer_memory += size_bytes; |
| 896 | _THREAD_SAFE_UNLOCK_ |
| 897 | |
| 898 | RID id = texture_buffer_owner.make_rid(texture_buffer); |
| 899 | #ifdef DEV_ENABLED |
| 900 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 901 | #endif |
| 902 | return id; |
| 903 | } |
| 904 | |
| 905 | /*****************/ |
| 906 | /**** TEXTURE ****/ |
| 907 | /*****************/ |
| 908 | |
| 909 | RID RenderingDevice::texture_create(const TextureFormat &p_format, const TextureView &p_view, const Vector<Vector<uint8_t>> &p_data) { |
| 910 | // Some adjustments will happen. |
| 911 | TextureFormat format = p_format; |
| 912 | |
| 913 | if (format.shareable_formats.size()) { |
| 914 | ERR_FAIL_COND_V_MSG(!format.shareable_formats.has(format.format), RID(), |
| 915 | "If supplied a list of shareable formats, the current format must be present in the list"); |
| 916 | ERR_FAIL_COND_V_MSG(p_view.format_override != DATA_FORMAT_MAX && !format.shareable_formats.has(p_view.format_override), RID(), |
| 917 | "If supplied a list of shareable formats, the current view format override must be present in the list"); |
| 918 | } |
| 919 | |
| 920 | ERR_FAIL_INDEX_V(format.texture_type, RDD::TEXTURE_TYPE_MAX, RID()); |
| 921 | |
| 922 | ERR_FAIL_COND_V_MSG(format.width < 1, RID(), "Width must be equal or greater than 1 for all textures"); |
| 923 | |
| 924 | if (format.texture_type != TEXTURE_TYPE_1D && format.texture_type != TEXTURE_TYPE_1D_ARRAY) { |
| 925 | ERR_FAIL_COND_V_MSG(format.height < 1, RID(), "Height must be equal or greater than 1 for 2D and 3D textures"); |
| 926 | } |
| 927 | |
| 928 | if (format.texture_type == TEXTURE_TYPE_3D) { |
| 929 | ERR_FAIL_COND_V_MSG(format.depth < 1, RID(), "Depth must be equal or greater than 1 for 3D textures"); |
| 930 | } |
| 931 | |
| 932 | ERR_FAIL_COND_V(format.mipmaps < 1, RID()); |
| 933 | |
| 934 | if (format.texture_type == TEXTURE_TYPE_1D_ARRAY || format.texture_type == TEXTURE_TYPE_2D_ARRAY || format.texture_type == TEXTURE_TYPE_CUBE_ARRAY || format.texture_type == TEXTURE_TYPE_CUBE) { |
| 935 | ERR_FAIL_COND_V_MSG(format.array_layers < 1, RID(), |
| 936 | "Number of layers must be equal or greater than 1 for arrays and cubemaps."); |
| 937 | ERR_FAIL_COND_V_MSG((format.texture_type == TEXTURE_TYPE_CUBE_ARRAY || format.texture_type == TEXTURE_TYPE_CUBE) && (format.array_layers % 6) != 0, RID(), |
| 938 | "Cubemap and cubemap array textures must provide a layer number that is multiple of 6"); |
| 939 | ERR_FAIL_COND_V_MSG(((format.texture_type == TEXTURE_TYPE_CUBE_ARRAY || format.texture_type == TEXTURE_TYPE_CUBE)) && (format.width != format.height), RID(), |
| 940 | "Cubemap and cubemap array textures must have equal width and height."); |
| 941 | ERR_FAIL_COND_V_MSG(format.array_layers > driver->limit_get(LIMIT_MAX_TEXTURE_ARRAY_LAYERS), RID(), "Number of layers exceeds device maximum."); |
| 942 | } else { |
| 943 | format.array_layers = 1; |
| 944 | } |
| 945 | |
| 946 | ERR_FAIL_INDEX_V(format.samples, TEXTURE_SAMPLES_MAX, RID()); |
| 947 | |
| 948 | ERR_FAIL_COND_V_MSG(format.usage_bits == 0, RID(), "No usage bits specified (at least one is needed)"); |
| 949 | |
| 950 | format.height = format.texture_type != TEXTURE_TYPE_1D && format.texture_type != TEXTURE_TYPE_1D_ARRAY ? format.height : 1; |
| 951 | format.depth = format.texture_type == TEXTURE_TYPE_3D ? format.depth : 1; |
| 952 | |
| 953 | uint64_t size_max = 0; |
| 954 | switch (format.texture_type) { |
| 955 | case TEXTURE_TYPE_1D: |
| 956 | case TEXTURE_TYPE_1D_ARRAY: |
| 957 | size_max = driver->limit_get(LIMIT_MAX_TEXTURE_SIZE_1D); |
| 958 | break; |
| 959 | case TEXTURE_TYPE_2D: |
| 960 | case TEXTURE_TYPE_2D_ARRAY: |
| 961 | size_max = driver->limit_get(LIMIT_MAX_TEXTURE_SIZE_2D); |
| 962 | break; |
| 963 | case TEXTURE_TYPE_CUBE: |
| 964 | case TEXTURE_TYPE_CUBE_ARRAY: |
| 965 | size_max = driver->limit_get(LIMIT_MAX_TEXTURE_SIZE_CUBE); |
| 966 | break; |
| 967 | case TEXTURE_TYPE_3D: |
| 968 | size_max = driver->limit_get(LIMIT_MAX_TEXTURE_SIZE_3D); |
| 969 | break; |
| 970 | case TEXTURE_TYPE_MAX: |
| 971 | break; |
| 972 | } |
| 973 | ERR_FAIL_COND_V_MSG(format.width > size_max || format.height > size_max || format.depth > size_max, RID(), "Texture dimensions exceed device maximum."); |
| 974 | |
| 975 | uint32_t required_mipmaps = get_image_required_mipmaps(format.width, format.height, format.depth); |
| 976 | |
| 977 | ERR_FAIL_COND_V_MSG(required_mipmaps < format.mipmaps, RID(), |
| 978 | "Too many mipmaps requested for texture format and dimensions (" + itos(format.mipmaps) + "), maximum allowed: (" + itos(required_mipmaps) + ")."); |
| 979 | |
| 980 | Vector<Vector<uint8_t>> data = p_data; |
| 981 | bool immediate_flush = false; |
| 982 | |
| 983 | // If this is a VRS texture, we make sure that it is created with valid initial data. This prevents a crash on Qualcomm Snapdragon XR2 Gen 1 |
| 984 | // (used in Quest 2, Quest Pro, Pico 4, HTC Vive XR Elite and others) where the driver will read the texture before we've had time to finish updating it. |
| 985 | if (data.is_empty() && (p_format.usage_bits & TEXTURE_USAGE_VRS_ATTACHMENT_BIT)) { |
| 986 | immediate_flush = true; |
| 987 | for (uint32_t i = 0; i < format.array_layers; i++) { |
| 988 | uint32_t required_size = get_image_format_required_size(format.format, format.width, format.height, format.depth, format.mipmaps); |
| 989 | Vector<uint8_t> layer; |
| 990 | layer.resize(required_size); |
| 991 | layer.fill(255); |
| 992 | data.push_back(layer); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | uint32_t forced_usage_bits = _texture_vrs_method_to_usage_bits(); |
| 997 | if (data.size()) { |
| 998 | ERR_FAIL_COND_V_MSG(data.size() != (int)format.array_layers, RID(), |
| 999 | "Default supplied data for image format is of invalid length (" + itos(data.size()) + "), should be (" + itos(format.array_layers) + ")."); |
| 1000 | |
| 1001 | for (uint32_t i = 0; i < format.array_layers; i++) { |
| 1002 | uint32_t required_size = get_image_format_required_size(format.format, format.width, format.height, format.depth, format.mipmaps); |
| 1003 | ERR_FAIL_COND_V_MSG((uint32_t)data[i].size() != required_size, RID(), |
| 1004 | "Data for slice index " + itos(i) + " (mapped to layer " + itos(i) + ") differs in size (supplied: " + itos(data[i].size()) + ") than what is required by the format (" + itos(required_size) + ")."); |
| 1005 | } |
| 1006 | |
| 1007 | ERR_FAIL_COND_V_MSG(format.usage_bits & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, RID(), |
| 1008 | "Textures created as depth attachments can't be initialized with data directly. Use RenderingDevice::texture_update() instead."); |
| 1009 | |
| 1010 | if (!(format.usage_bits & TEXTURE_USAGE_CAN_UPDATE_BIT)) { |
| 1011 | forced_usage_bits |= TEXTURE_USAGE_CAN_UPDATE_BIT; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | { |
| 1016 | // Validate that this image is supported for the intended use. |
| 1017 | bool cpu_readable = (format.usage_bits & RDD::TEXTURE_USAGE_CPU_READ_BIT); |
| 1018 | BitField<RDD::TextureUsageBits> supported_usage = driver->texture_get_usages_supported_by_format(format.format, cpu_readable); |
| 1019 | |
| 1020 | String format_text = "'" + String(FORMAT_NAMES[format.format]) + "'"; |
| 1021 | |
| 1022 | if ((format.usage_bits & TEXTURE_USAGE_SAMPLING_BIT) && !supported_usage.has_flag(TEXTURE_USAGE_SAMPLING_BIT)) { |
| 1023 | ERR_FAIL_V_MSG(RID(), "Format " + format_text + " does not support usage as sampling texture."); |
| 1024 | } |
| 1025 | if ((format.usage_bits & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) && !supported_usage.has_flag(TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) { |
| 1026 | ERR_FAIL_V_MSG(RID(), "Format " + format_text + " does not support usage as color attachment."); |
| 1027 | } |
| 1028 | if ((format.usage_bits & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) && !supported_usage.has_flag(TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) { |
| 1029 | ERR_FAIL_V_MSG(RID(), "Format " + format_text + " does not support usage as depth-stencil attachment."); |
| 1030 | } |
| 1031 | if ((format.usage_bits & TEXTURE_USAGE_STORAGE_BIT) && !supported_usage.has_flag(TEXTURE_USAGE_STORAGE_BIT)) { |
| 1032 | ERR_FAIL_V_MSG(RID(), "Format " + format_text + " does not support usage as storage image."); |
| 1033 | } |
| 1034 | if ((format.usage_bits & TEXTURE_USAGE_STORAGE_ATOMIC_BIT) && !supported_usage.has_flag(TEXTURE_USAGE_STORAGE_ATOMIC_BIT)) { |
| 1035 | ERR_FAIL_V_MSG(RID(), "Format " + format_text + " does not support usage as atomic storage image."); |
| 1036 | } |
| 1037 | if ((format.usage_bits & TEXTURE_USAGE_VRS_ATTACHMENT_BIT) && !supported_usage.has_flag(TEXTURE_USAGE_VRS_ATTACHMENT_BIT)) { |
| 1038 | ERR_FAIL_V_MSG(RID(), "Format " + format_text + " does not support usage as variable shading rate attachment."); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | // Transfer and validate view info. |
| 1043 | |
| 1044 | RDD::TextureView tv; |
| 1045 | if (p_view.format_override == DATA_FORMAT_MAX) { |
| 1046 | tv.format = format.format; |
| 1047 | } else { |
| 1048 | ERR_FAIL_INDEX_V(p_view.format_override, DATA_FORMAT_MAX, RID()); |
| 1049 | tv.format = p_view.format_override; |
| 1050 | } |
| 1051 | ERR_FAIL_INDEX_V(p_view.swizzle_r, TEXTURE_SWIZZLE_MAX, RID()); |
| 1052 | ERR_FAIL_INDEX_V(p_view.swizzle_g, TEXTURE_SWIZZLE_MAX, RID()); |
| 1053 | ERR_FAIL_INDEX_V(p_view.swizzle_b, TEXTURE_SWIZZLE_MAX, RID()); |
| 1054 | ERR_FAIL_INDEX_V(p_view.swizzle_a, TEXTURE_SWIZZLE_MAX, RID()); |
| 1055 | tv.swizzle_r = p_view.swizzle_r; |
| 1056 | tv.swizzle_g = p_view.swizzle_g; |
| 1057 | tv.swizzle_b = p_view.swizzle_b; |
| 1058 | tv.swizzle_a = p_view.swizzle_a; |
| 1059 | |
| 1060 | // Create. |
| 1061 | |
| 1062 | Texture texture; |
| 1063 | format.usage_bits |= forced_usage_bits; |
| 1064 | texture.driver_id = driver->texture_create(format, tv); |
| 1065 | ERR_FAIL_COND_V(!texture.driver_id, RID()); |
| 1066 | texture.type = format.texture_type; |
| 1067 | texture.format = format.format; |
| 1068 | texture.width = format.width; |
| 1069 | texture.height = format.height; |
| 1070 | texture.depth = format.depth; |
| 1071 | texture.layers = format.array_layers; |
| 1072 | texture.mipmaps = format.mipmaps; |
| 1073 | texture.base_mipmap = 0; |
| 1074 | texture.base_layer = 0; |
| 1075 | texture.is_resolve_buffer = format.is_resolve_buffer; |
| 1076 | texture.is_discardable = format.is_discardable; |
| 1077 | texture.usage_flags = format.usage_bits & ~forced_usage_bits; |
| 1078 | texture.samples = format.samples; |
| 1079 | texture.allowed_shared_formats = format.shareable_formats; |
| 1080 | texture.has_initial_data = !data.is_empty(); |
| 1081 | |
| 1082 | if (driver->api_trait_get(RDD::API_TRAIT_TEXTURE_OUTPUTS_REQUIRE_CLEARS)) { |
| 1083 | // Check if a clear for this texture must be performed the first time it's used if the driver requires explicit clears after initialization. |
| 1084 | texture.pending_clear = !texture.has_initial_data && (format.usage_bits & (TEXTURE_USAGE_STORAGE_BIT | TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)); |
| 1085 | } |
| 1086 | |
| 1087 | if ((format.usage_bits & (TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT))) { |
| 1088 | texture.read_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT); |
| 1089 | texture.barrier_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT); |
| 1090 | if (format_has_stencil(format.format)) { |
| 1091 | texture.barrier_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_STENCIL_BIT); |
| 1092 | } |
| 1093 | } else { |
| 1094 | texture.read_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_COLOR_BIT); |
| 1095 | texture.barrier_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_COLOR_BIT); |
| 1096 | } |
| 1097 | |
| 1098 | texture.bound = false; |
| 1099 | |
| 1100 | // Textures are only assumed to be immutable if they have initial data and none of the other bits that indicate write usage are enabled. |
| 1101 | bool texture_mutable_by_default = texture.usage_flags & (TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT | TEXTURE_USAGE_STORAGE_BIT | TEXTURE_USAGE_STORAGE_ATOMIC_BIT); |
| 1102 | if (data.is_empty() || texture_mutable_by_default) { |
| 1103 | _texture_make_mutable(&texture, RID()); |
| 1104 | } |
| 1105 | |
| 1106 | texture_memory += driver->texture_get_allocation_size(texture.driver_id); |
| 1107 | |
| 1108 | RID id = texture_owner.make_rid(texture); |
| 1109 | #ifdef DEV_ENABLED |
| 1110 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 1111 | #endif |
| 1112 | |
| 1113 | if (data.size()) { |
| 1114 | const bool use_general_in_copy_queues = driver->api_trait_get(RDD::API_TRAIT_USE_GENERAL_IN_COPY_QUEUES); |
| 1115 | const RDD::TextureLayout dst_layout = use_general_in_copy_queues ? RDD::TEXTURE_LAYOUT_GENERAL : RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL; |
| 1116 | for (uint32_t i = 0; i < format.array_layers; i++) { |
| 1117 | _texture_initialize(id, i, data[i], dst_layout, immediate_flush); |
| 1118 | } |
| 1119 | |
| 1120 | if (texture.draw_tracker != nullptr) { |
| 1121 | texture.draw_tracker->usage = use_general_in_copy_queues ? RDG::RESOURCE_USAGE_GENERAL : RDG::RESOURCE_USAGE_COPY_TO; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | return id; |
| 1126 | } |
| 1127 | |
| 1128 | RID RenderingDevice::texture_create_shared(const TextureView &p_view, RID p_with_texture) { |
| 1129 | Texture *src_texture = texture_owner.get_or_null(p_with_texture); |
| 1130 | ERR_FAIL_NULL_V(src_texture, RID()); |
| 1131 | |
| 1132 | if (src_texture->owner.is_valid()) { // Ahh this is a share. The RenderingDeviceDriver needs the actual owner. |
| 1133 | p_with_texture = src_texture->owner; |
| 1134 | src_texture = texture_owner.get_or_null(src_texture->owner); |
| 1135 | ERR_FAIL_NULL_V(src_texture, RID()); // This is a bug. |
| 1136 | } |
| 1137 | |
| 1138 | // Create view. |
| 1139 | |
| 1140 | Texture texture = *src_texture; |
| 1141 | texture.slice_trackers = nullptr; |
| 1142 | texture.shared_fallback = nullptr; |
| 1143 | |
| 1144 | RDD::TextureView tv; |
| 1145 | bool create_shared = true; |
| 1146 | bool raw_reintepretation = false; |
| 1147 | if (p_view.format_override == DATA_FORMAT_MAX || p_view.format_override == texture.format) { |
| 1148 | tv.format = texture.format; |
| 1149 | } else { |
| 1150 | ERR_FAIL_INDEX_V(p_view.format_override, DATA_FORMAT_MAX, RID()); |
| 1151 | |
| 1152 | ERR_FAIL_COND_V_MSG(!texture.allowed_shared_formats.has(p_view.format_override), RID(), |
| 1153 | "Format override is not in the list of allowed shareable formats for original texture."); |
| 1154 | tv.format = p_view.format_override; |
| 1155 | create_shared = driver->texture_can_make_shared_with_format(texture.driver_id, p_view.format_override, raw_reintepretation); |
| 1156 | } |
| 1157 | tv.swizzle_r = p_view.swizzle_r; |
| 1158 | tv.swizzle_g = p_view.swizzle_g; |
| 1159 | tv.swizzle_b = p_view.swizzle_b; |
| 1160 | tv.swizzle_a = p_view.swizzle_a; |
| 1161 | |
| 1162 | if (create_shared) { |
| 1163 | texture.driver_id = driver->texture_create_shared(texture.driver_id, tv); |
| 1164 | } else { |
| 1165 | // The regular view will use the same format as the main texture. |
| 1166 | RDD::TextureView regular_view = tv; |
| 1167 | regular_view.format = src_texture->format; |
| 1168 | texture.driver_id = driver->texture_create_shared(texture.driver_id, regular_view); |
| 1169 | |
| 1170 | // Create the independent texture for the alias. |
| 1171 | RDD::TextureFormat alias_format = texture.texture_format(); |
| 1172 | alias_format.format = tv.format; |
| 1173 | alias_format.usage_bits = TEXTURE_USAGE_SAMPLING_BIT | TEXTURE_USAGE_CAN_COPY_TO_BIT; |
| 1174 | |
| 1175 | _texture_check_shared_fallback(src_texture); |
| 1176 | _texture_check_shared_fallback(&texture); |
| 1177 | |
| 1178 | texture.shared_fallback->texture = driver->texture_create(alias_format, tv); |
| 1179 | texture.shared_fallback->raw_reinterpretation = raw_reintepretation; |
| 1180 | texture_memory += driver->texture_get_allocation_size(texture.shared_fallback->texture); |
| 1181 | |
| 1182 | RDG::ResourceTracker *tracker = RDG::resource_tracker_create(); |
| 1183 | tracker->texture_driver_id = texture.shared_fallback->texture; |
| 1184 | tracker->texture_size = Size2i(texture.width, texture.height); |
| 1185 | tracker->texture_subresources = texture.barrier_range(); |
| 1186 | tracker->texture_usage = alias_format.usage_bits; |
| 1187 | tracker->is_discardable = texture.is_discardable; |
| 1188 | tracker->reference_count = 1; |
| 1189 | texture.shared_fallback->texture_tracker = tracker; |
| 1190 | texture.shared_fallback->revision = 0; |
| 1191 | |
| 1192 | if (raw_reintepretation && src_texture->shared_fallback->buffer.id == 0) { |
| 1193 | // For shared textures of the same size, we create the buffer on the main texture if it doesn't have it already. |
| 1194 | _texture_create_reinterpret_buffer(src_texture); |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | ERR_FAIL_COND_V(!texture.driver_id, RID()); |
| 1199 | |
| 1200 | if (texture.draw_tracker != nullptr) { |
| 1201 | texture.draw_tracker->reference_count++; |
| 1202 | } |
| 1203 | |
| 1204 | texture.owner = p_with_texture; |
| 1205 | RID id = texture_owner.make_rid(texture); |
| 1206 | #ifdef DEV_ENABLED |
| 1207 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 1208 | #endif |
| 1209 | _add_dependency(id, p_with_texture); |
| 1210 | |
| 1211 | return id; |
| 1212 | } |
| 1213 | |
| 1214 | RID RenderingDevice::texture_create_from_extension(TextureType p_type, DataFormat p_format, TextureSamples p_samples, BitField<RenderingDevice::TextureUsageBits> p_usage, uint64_t p_image, uint64_t p_width, uint64_t p_height, uint64_t p_depth, uint64_t p_layers, uint64_t p_mipmaps) { |
| 1215 | // This method creates a texture object using a VkImage created by an extension, module or other external source (OpenXR uses this). |
| 1216 | |
| 1217 | Texture texture; |
| 1218 | texture.type = p_type; |
| 1219 | texture.format = p_format; |
| 1220 | texture.samples = p_samples; |
| 1221 | texture.width = p_width; |
| 1222 | texture.height = p_height; |
| 1223 | texture.depth = p_depth; |
| 1224 | texture.layers = p_layers; |
| 1225 | texture.mipmaps = p_mipmaps; |
| 1226 | texture.usage_flags = p_usage; |
| 1227 | texture.base_mipmap = 0; |
| 1228 | texture.base_layer = 0; |
| 1229 | texture.allowed_shared_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_UNORM); |
| 1230 | texture.allowed_shared_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_SRGB); |
| 1231 | |
| 1232 | if (p_usage.has_flag(TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) || p_usage.has_flag(TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT)) { |
| 1233 | texture.read_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT); |
| 1234 | texture.barrier_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT); |
| 1235 | /*if (format_has_stencil(p_format.format)) { |
| 1236 | texture.barrier_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_STENCIL_BIT); |
| 1237 | }*/ |
| 1238 | } else { |
| 1239 | texture.read_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_COLOR_BIT); |
| 1240 | texture.barrier_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_COLOR_BIT); |
| 1241 | } |
| 1242 | |
| 1243 | texture.driver_id = driver->texture_create_from_extension(p_image, p_type, p_format, p_layers, (texture.usage_flags & (TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT)), p_mipmaps); |
| 1244 | ERR_FAIL_COND_V(!texture.driver_id, RID()); |
| 1245 | |
| 1246 | _texture_make_mutable(&texture, RID()); |
| 1247 | |
| 1248 | RID id = texture_owner.make_rid(texture); |
| 1249 | #ifdef DEV_ENABLED |
| 1250 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 1251 | #endif |
| 1252 | |
| 1253 | return id; |
| 1254 | } |
| 1255 | |
| 1256 | RID RenderingDevice::texture_create_shared_from_slice(const TextureView &p_view, RID p_with_texture, uint32_t p_layer, uint32_t p_mipmap, uint32_t p_mipmaps, TextureSliceType p_slice_type, uint32_t p_layers) { |
| 1257 | Texture *src_texture = texture_owner.get_or_null(p_with_texture); |
| 1258 | ERR_FAIL_NULL_V(src_texture, RID()); |
| 1259 | |
| 1260 | if (src_texture->owner.is_valid()) { // // Ahh this is a share. The RenderingDeviceDriver needs the actual owner. |
| 1261 | p_with_texture = src_texture->owner; |
| 1262 | src_texture = texture_owner.get_or_null(src_texture->owner); |
| 1263 | ERR_FAIL_NULL_V(src_texture, RID()); // This is a bug. |
| 1264 | } |
| 1265 | |
| 1266 | ERR_FAIL_COND_V_MSG(p_slice_type == TEXTURE_SLICE_CUBEMAP && (src_texture->type != TEXTURE_TYPE_CUBE && src_texture->type != TEXTURE_TYPE_CUBE_ARRAY), RID(), |
| 1267 | "Can only create a cubemap slice from a cubemap or cubemap array mipmap"); |
| 1268 | |
| 1269 | ERR_FAIL_COND_V_MSG(p_slice_type == TEXTURE_SLICE_3D && src_texture->type != TEXTURE_TYPE_3D, RID(), |
| 1270 | "Can only create a 3D slice from a 3D texture"); |
| 1271 | |
| 1272 | ERR_FAIL_COND_V_MSG(p_slice_type == TEXTURE_SLICE_2D_ARRAY && (src_texture->type != TEXTURE_TYPE_2D_ARRAY), RID(), |
| 1273 | "Can only create an array slice from a 2D array mipmap"); |
| 1274 | |
| 1275 | // Create view. |
| 1276 | |
| 1277 | ERR_FAIL_UNSIGNED_INDEX_V(p_mipmap, src_texture->mipmaps, RID()); |
| 1278 | ERR_FAIL_COND_V(p_mipmap + p_mipmaps > src_texture->mipmaps, RID()); |
| 1279 | ERR_FAIL_UNSIGNED_INDEX_V(p_layer, src_texture->layers, RID()); |
| 1280 | |
| 1281 | int slice_layers = 1; |
| 1282 | if (p_layers != 0) { |
| 1283 | ERR_FAIL_COND_V_MSG(p_layers > 1 && p_slice_type != TEXTURE_SLICE_2D_ARRAY, RID(), "layer slicing only supported for 2D arrays"); |
| 1284 | ERR_FAIL_COND_V_MSG(p_layer + p_layers > src_texture->layers, RID(), "layer slice is out of bounds"); |
| 1285 | slice_layers = p_layers; |
| 1286 | } else if (p_slice_type == TEXTURE_SLICE_2D_ARRAY) { |
| 1287 | ERR_FAIL_COND_V_MSG(p_layer != 0, RID(), "layer must be 0 when obtaining a 2D array mipmap slice"); |
| 1288 | slice_layers = src_texture->layers; |
| 1289 | } else if (p_slice_type == TEXTURE_SLICE_CUBEMAP) { |
| 1290 | slice_layers = 6; |
| 1291 | } |
| 1292 | |
| 1293 | Texture texture = *src_texture; |
| 1294 | texture.slice_trackers = nullptr; |
| 1295 | texture.shared_fallback = nullptr; |
| 1296 | |
| 1297 | get_image_format_required_size(texture.format, texture.width, texture.height, texture.depth, p_mipmap + 1, &texture.width, &texture.height); |
| 1298 | texture.mipmaps = p_mipmaps; |
| 1299 | texture.layers = slice_layers; |
| 1300 | texture.base_mipmap = p_mipmap; |
| 1301 | texture.base_layer = p_layer; |
| 1302 | |
| 1303 | if (p_slice_type == TEXTURE_SLICE_2D) { |
| 1304 | texture.type = TEXTURE_TYPE_2D; |
| 1305 | } else if (p_slice_type == TEXTURE_SLICE_3D) { |
| 1306 | texture.type = TEXTURE_TYPE_3D; |
| 1307 | } |
| 1308 | |
| 1309 | RDD::TextureView tv; |
| 1310 | bool create_shared = true; |
| 1311 | bool raw_reintepretation = false; |
| 1312 | if (p_view.format_override == DATA_FORMAT_MAX || p_view.format_override == texture.format) { |
| 1313 | tv.format = texture.format; |
| 1314 | } else { |
| 1315 | ERR_FAIL_INDEX_V(p_view.format_override, DATA_FORMAT_MAX, RID()); |
| 1316 | |
| 1317 | ERR_FAIL_COND_V_MSG(!texture.allowed_shared_formats.has(p_view.format_override), RID(), |
| 1318 | "Format override is not in the list of allowed shareable formats for original texture."); |
| 1319 | tv.format = p_view.format_override; |
| 1320 | create_shared = driver->texture_can_make_shared_with_format(texture.driver_id, p_view.format_override, raw_reintepretation); |
| 1321 | } |
| 1322 | |
| 1323 | tv.swizzle_r = p_view.swizzle_r; |
| 1324 | tv.swizzle_g = p_view.swizzle_g; |
| 1325 | tv.swizzle_b = p_view.swizzle_b; |
| 1326 | tv.swizzle_a = p_view.swizzle_a; |
| 1327 | |
| 1328 | if (p_slice_type == TEXTURE_SLICE_CUBEMAP) { |
| 1329 | ERR_FAIL_COND_V_MSG(p_layer >= src_texture->layers, RID(), |
| 1330 | "Specified layer is invalid for cubemap"); |
| 1331 | ERR_FAIL_COND_V_MSG((p_layer % 6) != 0, RID(), |
| 1332 | "Specified layer must be a multiple of 6."); |
| 1333 | } |
| 1334 | |
| 1335 | if (create_shared) { |
| 1336 | texture.driver_id = driver->texture_create_shared_from_slice(src_texture->driver_id, tv, p_slice_type, p_layer, slice_layers, p_mipmap, p_mipmaps); |
| 1337 | } else { |
| 1338 | // The regular view will use the same format as the main texture. |
| 1339 | RDD::TextureView regular_view = tv; |
| 1340 | regular_view.format = src_texture->format; |
| 1341 | texture.driver_id = driver->texture_create_shared_from_slice(src_texture->driver_id, regular_view, p_slice_type, p_layer, slice_layers, p_mipmap, p_mipmaps); |
| 1342 | |
| 1343 | // Create the independent texture for the slice. |
| 1344 | RDD::TextureSubresourceRange slice_range = texture.barrier_range(); |
| 1345 | slice_range.base_mipmap = 0; |
| 1346 | slice_range.base_layer = 0; |
| 1347 | |
| 1348 | RDD::TextureFormat slice_format = texture.texture_format(); |
| 1349 | slice_format.width = MAX(texture.width >> p_mipmap, 1U); |
| 1350 | slice_format.height = MAX(texture.height >> p_mipmap, 1U); |
| 1351 | slice_format.depth = MAX(texture.depth >> p_mipmap, 1U); |
| 1352 | slice_format.format = tv.format; |
| 1353 | slice_format.usage_bits = TEXTURE_USAGE_SAMPLING_BIT | TEXTURE_USAGE_CAN_COPY_TO_BIT; |
| 1354 | |
| 1355 | _texture_check_shared_fallback(src_texture); |
| 1356 | _texture_check_shared_fallback(&texture); |
| 1357 | |
| 1358 | texture.shared_fallback->texture = driver->texture_create(slice_format, tv); |
| 1359 | texture.shared_fallback->raw_reinterpretation = raw_reintepretation; |
| 1360 | texture_memory += driver->texture_get_allocation_size(texture.shared_fallback->texture); |
| 1361 | |
| 1362 | RDG::ResourceTracker *tracker = RDG::resource_tracker_create(); |
| 1363 | tracker->texture_driver_id = texture.shared_fallback->texture; |
| 1364 | tracker->texture_size = Size2i(texture.width, texture.height); |
| 1365 | tracker->texture_subresources = slice_range; |
| 1366 | tracker->texture_usage = slice_format.usage_bits; |
| 1367 | tracker->is_discardable = slice_format.is_discardable; |
| 1368 | tracker->reference_count = 1; |
| 1369 | texture.shared_fallback->texture_tracker = tracker; |
| 1370 | texture.shared_fallback->revision = 0; |
| 1371 | |
| 1372 | if (raw_reintepretation && src_texture->shared_fallback->buffer.id == 0) { |
| 1373 | // For shared texture slices, we create the buffer on the slice if the source texture has no reinterpretation buffer. |
| 1374 | _texture_create_reinterpret_buffer(&texture); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | ERR_FAIL_COND_V(!texture.driver_id, RID()); |
| 1379 | |
| 1380 | const Rect2i slice_rect(p_mipmap, p_layer, p_mipmaps, slice_layers); |
| 1381 | texture.owner = p_with_texture; |
| 1382 | texture.slice_type = p_slice_type; |
| 1383 | texture.slice_rect = slice_rect; |
| 1384 | |
| 1385 | // If parent is mutable, make slice mutable by default. |
| 1386 | if (src_texture->draw_tracker != nullptr) { |
| 1387 | texture.draw_tracker = nullptr; |
| 1388 | _texture_make_mutable(&texture, RID()); |
| 1389 | } |
| 1390 | |
| 1391 | RID id = texture_owner.make_rid(texture); |
| 1392 | #ifdef DEV_ENABLED |
| 1393 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 1394 | #endif |
| 1395 | _add_dependency(id, p_with_texture); |
| 1396 | |
| 1397 | return id; |
| 1398 | } |
| 1399 | |
| 1400 | static _ALWAYS_INLINE_ void _copy_region(uint8_t const *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_x, uint32_t p_src_y, uint32_t p_src_w, uint32_t p_src_h, uint32_t p_src_full_w, uint32_t p_dst_pitch, uint32_t p_unit_size) { |
| 1401 | uint32_t src_offset = (p_src_y * p_src_full_w + p_src_x) * p_unit_size; |
| 1402 | uint32_t dst_offset = 0; |
| 1403 | for (uint32_t y = p_src_h; y > 0; y--) { |
| 1404 | uint8_t const *__restrict src = p_src + src_offset; |
| 1405 | uint8_t *__restrict dst = p_dst + dst_offset; |
| 1406 | for (uint32_t x = p_src_w * p_unit_size; x > 0; x--) { |
| 1407 | *dst = *src; |
| 1408 | src++; |
| 1409 | dst++; |
| 1410 | } |
| 1411 | src_offset += p_src_full_w * p_unit_size; |
| 1412 | dst_offset += p_dst_pitch; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | static _ALWAYS_INLINE_ void _copy_region_block_or_regular(const uint8_t *p_read_ptr, uint8_t *p_write_ptr, uint32_t p_x, uint32_t p_y, uint32_t p_width, uint32_t p_region_w, uint32_t p_region_h, uint32_t p_block_w, uint32_t p_block_h, uint32_t p_dst_pitch, uint32_t p_pixel_size, uint32_t p_block_size) { |
| 1417 | if (p_block_w != 1 || p_block_h != 1) { |
| 1418 | // Block format. |
| 1419 | uint32_t xb = p_x / p_block_w; |
| 1420 | uint32_t yb = p_y / p_block_h; |
| 1421 | uint32_t wb = p_width / p_block_w; |
| 1422 | uint32_t region_wb = p_region_w / p_block_w; |
| 1423 | uint32_t region_hb = p_region_h / p_block_h; |
| 1424 | _copy_region(p_read_ptr, p_write_ptr, xb, yb, region_wb, region_hb, wb, p_dst_pitch, p_block_size); |
| 1425 | } else { |
| 1426 | // Regular format. |
| 1427 | _copy_region(p_read_ptr, p_write_ptr, p_x, p_y, p_region_w, p_region_h, p_width, p_dst_pitch, p_pixel_size); |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | uint32_t RenderingDevice::_texture_layer_count(Texture *p_texture) const { |
| 1432 | switch (p_texture->type) { |
| 1433 | case TEXTURE_TYPE_CUBE: |
| 1434 | case TEXTURE_TYPE_CUBE_ARRAY: |
| 1435 | return p_texture->layers * 6; |
| 1436 | default: |
| 1437 | return p_texture->layers; |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | uint32_t greatest_common_denominator(uint32_t a, uint32_t b) { |
| 1442 | // Euclidean algorithm. |
| 1443 | uint32_t t; |
| 1444 | while (b != 0) { |
| 1445 | t = b; |
| 1446 | b = a % b; |
| 1447 | a = t; |
| 1448 | } |
| 1449 | |
| 1450 | return a; |
| 1451 | } |
| 1452 | |
| 1453 | uint32_t least_common_multiple(uint32_t a, uint32_t b) { |
| 1454 | if (a == 0 || b == 0) { |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | return (a / greatest_common_denominator(a, b)) * b; |
| 1459 | } |
| 1460 | |
| 1461 | uint32_t RenderingDevice::_texture_alignment(Texture *p_texture) const { |
| 1462 | uint32_t alignment = get_compressed_image_format_block_byte_size(p_texture->format); |
| 1463 | if (alignment == 1) { |
| 1464 | alignment = get_image_format_pixel_size(p_texture->format); |
| 1465 | } |
| 1466 | |
| 1467 | return least_common_multiple(alignment, driver->api_trait_get(RDD::API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT)); |
| 1468 | } |
| 1469 | |
| 1470 | Error RenderingDevice::_texture_initialize(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, RDD::TextureLayout p_dst_layout, bool p_immediate_flush) { |
| 1471 | Texture *texture = texture_owner.get_or_null(p_texture); |
| 1472 | ERR_FAIL_NULL_V(texture, ERR_INVALID_PARAMETER); |
| 1473 | |
| 1474 | if (texture->owner != RID()) { |
| 1475 | p_texture = texture->owner; |
| 1476 | texture = texture_owner.get_or_null(texture->owner); |
| 1477 | ERR_FAIL_NULL_V(texture, ERR_BUG); // This is a bug. |
| 1478 | } |
| 1479 | |
| 1480 | uint32_t layer_count = _texture_layer_count(texture); |
| 1481 | ERR_FAIL_COND_V(p_layer >= layer_count, ERR_INVALID_PARAMETER); |
| 1482 | |
| 1483 | uint32_t width, height; |
| 1484 | uint32_t tight_mip_size = get_image_format_required_size(texture->format, texture->width, texture->height, texture->depth, texture->mipmaps, &width, &height); |
| 1485 | uint32_t required_size = tight_mip_size; |
| 1486 | uint32_t required_align = _texture_alignment(texture); |
| 1487 | |
| 1488 | ERR_FAIL_COND_V_MSG(required_size != (uint32_t)p_data.size(), ERR_INVALID_PARAMETER, |
| 1489 | "Required size for texture update (" + itos(required_size) + ") does not match data supplied size (" + itos(p_data.size()) + ")."); |
| 1490 | |
| 1491 | uint32_t block_w, block_h; |
| 1492 | get_compressed_image_format_block_dimensions(texture->format, block_w, block_h); |
| 1493 | |
| 1494 | uint32_t pixel_size = get_image_format_pixel_size(texture->format); |
| 1495 | uint32_t pixel_rshift = get_compressed_image_format_pixel_rshift(texture->format); |
| 1496 | uint32_t block_size = get_compressed_image_format_block_byte_size(texture->format); |
| 1497 | |
| 1498 | // The algorithm operates on two passes, one to figure out the total size the staging buffer will require to allocate and another one where the copy is actually performed. |
| 1499 | uint32_t staging_worker_offset = 0; |
| 1500 | uint32_t staging_local_offset = 0; |
| 1501 | TransferWorker *transfer_worker = nullptr; |
| 1502 | const uint8_t *read_ptr = p_data.ptr(); |
| 1503 | uint8_t *write_ptr = nullptr; |
| 1504 | for (uint32_t pass = 0; pass < 2; pass++) { |
| 1505 | const bool copy_pass = (pass == 1); |
| 1506 | if (copy_pass) { |
| 1507 | transfer_worker = _acquire_transfer_worker(staging_local_offset, required_align, staging_worker_offset); |
| 1508 | texture->transfer_worker_index = transfer_worker->index; |
| 1509 | |
| 1510 | { |
| 1511 | MutexLock lock(transfer_worker->operations_mutex); |
| 1512 | texture->transfer_worker_operation = ++transfer_worker->operations_counter; |
| 1513 | } |
| 1514 | |
| 1515 | staging_local_offset = 0; |
| 1516 | |
| 1517 | write_ptr = driver->buffer_map(transfer_worker->staging_buffer); |
| 1518 | ERR_FAIL_NULL_V(write_ptr, ERR_CANT_CREATE); |
| 1519 | |
| 1520 | if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) { |
| 1521 | // Transition the texture to the optimal layout. |
| 1522 | RDD::TextureBarrier tb; |
| 1523 | tb.texture = texture->driver_id; |
| 1524 | tb.dst_access = RDD::BARRIER_ACCESS_COPY_WRITE_BIT; |
| 1525 | tb.prev_layout = RDD::TEXTURE_LAYOUT_UNDEFINED; |
| 1526 | tb.next_layout = p_dst_layout; |
| 1527 | tb.subresources.aspect = texture->barrier_aspect_flags; |
| 1528 | tb.subresources.mipmap_count = texture->mipmaps; |
| 1529 | tb.subresources.base_layer = p_layer; |
| 1530 | tb.subresources.layer_count = 1; |
| 1531 | driver->command_pipeline_barrier(transfer_worker->command_buffer, RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, RDD::PIPELINE_STAGE_COPY_BIT, {}, {}, tb); |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | uint32_t mipmap_offset = 0; |
| 1536 | uint32_t logic_width = texture->width; |
| 1537 | uint32_t logic_height = texture->height; |
| 1538 | for (uint32_t mm_i = 0; mm_i < texture->mipmaps; mm_i++) { |
| 1539 | uint32_t depth = 0; |
| 1540 | uint32_t image_total = get_image_format_required_size(texture->format, texture->width, texture->height, texture->depth, mm_i + 1, &width, &height, &depth); |
| 1541 | |
| 1542 | const uint8_t *read_ptr_mipmap = read_ptr + mipmap_offset; |
| 1543 | tight_mip_size = image_total - mipmap_offset; |
| 1544 | |
| 1545 | for (uint32_t z = 0; z < depth; z++) { |
| 1546 | if (required_align > 0) { |
| 1547 | uint32_t align_offset = staging_local_offset % required_align; |
| 1548 | if (align_offset != 0) { |
| 1549 | staging_local_offset += required_align - align_offset; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | uint32_t pitch = (width * pixel_size * block_w) >> pixel_rshift; |
| 1554 | uint32_t pitch_step = driver->api_trait_get(RDD::API_TRAIT_TEXTURE_DATA_ROW_PITCH_STEP); |
| 1555 | pitch = STEPIFY(pitch, pitch_step); |
| 1556 | uint32_t to_allocate = pitch * height; |
| 1557 | to_allocate >>= pixel_rshift; |
| 1558 | |
| 1559 | if (copy_pass) { |
| 1560 | const uint8_t *read_ptr_mipmap_layer = read_ptr_mipmap + (tight_mip_size / depth) * z; |
| 1561 | uint64_t staging_buffer_offset = staging_worker_offset + staging_local_offset; |
| 1562 | uint8_t *write_ptr_mipmap_layer = write_ptr + staging_buffer_offset; |
| 1563 | _copy_region_block_or_regular(read_ptr_mipmap_layer, write_ptr_mipmap_layer, 0, 0, width, width, height, block_w, block_h, pitch, pixel_size, block_size); |
| 1564 | |
| 1565 | RDD::BufferTextureCopyRegion copy_region; |
| 1566 | copy_region.buffer_offset = staging_buffer_offset; |
| 1567 | copy_region.row_pitch = pitch; |
| 1568 | copy_region.texture_subresource.aspect = texture->read_aspect_flags.has_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT) ? RDD::TEXTURE_ASPECT_DEPTH : RDD::TEXTURE_ASPECT_COLOR; |
| 1569 | copy_region.texture_subresource.mipmap = mm_i; |
| 1570 | copy_region.texture_subresource.layer = p_layer; |
| 1571 | copy_region.texture_offset = Vector3i(0, 0, z); |
| 1572 | copy_region.texture_region_size = Vector3i(logic_width, logic_height, 1); |
| 1573 | driver->command_copy_buffer_to_texture(transfer_worker->command_buffer, transfer_worker->staging_buffer, texture->driver_id, p_dst_layout, copy_region); |
| 1574 | } |
| 1575 | |
| 1576 | staging_local_offset += to_allocate; |
| 1577 | } |
| 1578 | |
| 1579 | mipmap_offset = image_total; |
| 1580 | logic_width = MAX(1u, logic_width >> 1); |
| 1581 | logic_height = MAX(1u, logic_height >> 1); |
| 1582 | } |
| 1583 | |
| 1584 | if (copy_pass) { |
| 1585 | driver->buffer_unmap(transfer_worker->staging_buffer); |
| 1586 | |
| 1587 | // If the texture does not have a tracker, it means it must be transitioned to the sampling state. |
| 1588 | if (texture->draw_tracker == nullptr && driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) { |
| 1589 | RDD::TextureBarrier tb; |
| 1590 | tb.texture = texture->driver_id; |
| 1591 | tb.src_access = RDD::BARRIER_ACCESS_COPY_WRITE_BIT; |
| 1592 | tb.prev_layout = p_dst_layout; |
| 1593 | tb.next_layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 1594 | tb.subresources.aspect = texture->barrier_aspect_flags; |
| 1595 | tb.subresources.mipmap_count = texture->mipmaps; |
| 1596 | tb.subresources.base_layer = p_layer; |
| 1597 | tb.subresources.layer_count = 1; |
| 1598 | transfer_worker->texture_barriers.push_back(tb); |
| 1599 | } |
| 1600 | |
| 1601 | if (p_immediate_flush) { |
| 1602 | _end_transfer_worker(transfer_worker); |
| 1603 | _submit_transfer_worker(transfer_worker); |
| 1604 | _wait_for_transfer_worker(transfer_worker); |
| 1605 | } |
| 1606 | |
| 1607 | _release_transfer_worker(transfer_worker); |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | return OK; |
| 1612 | } |
| 1613 | |
| 1614 | Error RenderingDevice::texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data) { |
| 1615 | ERR_RENDER_THREAD_GUARD_V(ERR_UNAVAILABLE); |
| 1616 | |
| 1617 | ERR_FAIL_COND_V_MSG(draw_list.active || compute_list.active, ERR_INVALID_PARAMETER, "Updating textures is forbidden during creation of a draw or compute list"); |
| 1618 | |
| 1619 | Texture *texture = texture_owner.get_or_null(p_texture); |
| 1620 | ERR_FAIL_NULL_V(texture, ERR_INVALID_PARAMETER); |
| 1621 | |
| 1622 | if (texture->owner != RID()) { |
| 1623 | p_texture = texture->owner; |
| 1624 | texture = texture_owner.get_or_null(texture->owner); |
| 1625 | ERR_FAIL_NULL_V(texture, ERR_BUG); // This is a bug. |
| 1626 | } |
| 1627 | |
| 1628 | ERR_FAIL_COND_V_MSG(texture->bound, ERR_CANT_ACQUIRE_RESOURCE, |
| 1629 | "Texture can't be updated while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to update this texture."); |
| 1630 | |
| 1631 | ERR_FAIL_COND_V_MSG(!(texture->usage_flags & TEXTURE_USAGE_CAN_UPDATE_BIT), ERR_INVALID_PARAMETER, "Texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_UPDATE_BIT` to be set to be updatable."); |
| 1632 | |
| 1633 | uint32_t layer_count = _texture_layer_count(texture); |
| 1634 | ERR_FAIL_COND_V(p_layer >= layer_count, ERR_INVALID_PARAMETER); |
| 1635 | |
| 1636 | uint32_t width, height; |
| 1637 | uint32_t tight_mip_size = get_image_format_required_size(texture->format, texture->width, texture->height, texture->depth, texture->mipmaps, &width, &height); |
| 1638 | uint32_t required_size = tight_mip_size; |
| 1639 | uint32_t required_align = _texture_alignment(texture); |
| 1640 | |
| 1641 | ERR_FAIL_COND_V_MSG(required_size != (uint32_t)p_data.size(), ERR_INVALID_PARAMETER, |
| 1642 | "Required size for texture update (" + itos(required_size) + ") does not match data supplied size (" + itos(p_data.size()) + ")."); |
| 1643 | |
| 1644 | // Clear the texture if the driver requires it during its first use. |
| 1645 | _texture_check_pending_clear(p_texture, texture); |
| 1646 | |
| 1647 | _check_transfer_worker_texture(texture); |
| 1648 | |
| 1649 | uint32_t block_w, block_h; |
| 1650 | get_compressed_image_format_block_dimensions(texture->format, block_w, block_h); |
| 1651 | |
| 1652 | uint32_t pixel_size = get_image_format_pixel_size(texture->format); |
| 1653 | uint32_t pixel_rshift = get_compressed_image_format_pixel_rshift(texture->format); |
| 1654 | uint32_t block_size = get_compressed_image_format_block_byte_size(texture->format); |
| 1655 | |
| 1656 | uint32_t region_size = texture_upload_region_size_px; |
| 1657 | |
| 1658 | const uint8_t *read_ptr = p_data.ptr(); |
| 1659 | |
| 1660 | thread_local LocalVector<RDG::RecordedBufferToTextureCopy> command_buffer_to_texture_copies_vector; |
| 1661 | command_buffer_to_texture_copies_vector.clear(); |
| 1662 | |
| 1663 | // Indicate the texture will get modified for the shared texture fallback. |
| 1664 | _texture_update_shared_fallback(p_texture, texture, true); |
| 1665 | |
| 1666 | uint32_t mipmap_offset = 0; |
| 1667 | |
| 1668 | uint32_t logic_width = texture->width; |
| 1669 | uint32_t logic_height = texture->height; |
| 1670 | |
| 1671 | for (uint32_t mm_i = 0; mm_i < texture->mipmaps; mm_i++) { |
| 1672 | uint32_t depth = 0; |
| 1673 | uint32_t image_total = get_image_format_required_size(texture->format, texture->width, texture->height, texture->depth, mm_i + 1, &width, &height, &depth); |
| 1674 | |
| 1675 | const uint8_t *read_ptr_mipmap = read_ptr + mipmap_offset; |
| 1676 | tight_mip_size = image_total - mipmap_offset; |
| 1677 | |
| 1678 | for (uint32_t z = 0; z < depth; z++) { |
| 1679 | const uint8_t *read_ptr_mipmap_layer = read_ptr_mipmap + (tight_mip_size / depth) * z; |
| 1680 | for (uint32_t y = 0; y < height; y += region_size) { |
| 1681 | for (uint32_t x = 0; x < width; x += region_size) { |
| 1682 | uint32_t region_w = MIN(region_size, width - x); |
| 1683 | uint32_t region_h = MIN(region_size, height - y); |
| 1684 | |
| 1685 | uint32_t region_logic_w = MIN(region_size, logic_width - x); |
| 1686 | uint32_t region_logic_h = MIN(region_size, logic_height - y); |
| 1687 | |
| 1688 | uint32_t region_pitch = (region_w * pixel_size * block_w) >> pixel_rshift; |
| 1689 | uint32_t pitch_step = driver->api_trait_get(RDD::API_TRAIT_TEXTURE_DATA_ROW_PITCH_STEP); |
| 1690 | region_pitch = STEPIFY(region_pitch, pitch_step); |
| 1691 | uint32_t to_allocate = region_pitch * region_h; |
| 1692 | uint32_t alloc_offset = 0, alloc_size = 0; |
| 1693 | StagingRequiredAction required_action; |
| 1694 | Error err = _staging_buffer_allocate(upload_staging_buffers, to_allocate, required_align, alloc_offset, alloc_size, required_action, false); |
| 1695 | ERR_FAIL_COND_V(err, ERR_CANT_CREATE); |
| 1696 | |
| 1697 | if (!command_buffer_to_texture_copies_vector.is_empty() && required_action == STAGING_REQUIRED_ACTION_FLUSH_AND_STALL_ALL) { |
| 1698 | if (_texture_make_mutable(texture, p_texture)) { |
| 1699 | // The texture must be mutable to be used as a copy destination. |
| 1700 | draw_graph.add_synchronization(); |
| 1701 | } |
| 1702 | |
| 1703 | // If the staging buffer requires flushing everything, we submit the command early and clear the current vector. |
| 1704 | draw_graph.add_texture_update(texture->driver_id, texture->draw_tracker, command_buffer_to_texture_copies_vector); |
| 1705 | command_buffer_to_texture_copies_vector.clear(); |
| 1706 | } |
| 1707 | |
| 1708 | _staging_buffer_execute_required_action(upload_staging_buffers, required_action); |
| 1709 | |
| 1710 | uint8_t *write_ptr = upload_staging_buffers.blocks[upload_staging_buffers.current].data_ptr + alloc_offset; |
| 1711 | |
| 1712 | ERR_FAIL_COND_V(region_w % block_w, ERR_BUG); |
| 1713 | ERR_FAIL_COND_V(region_h % block_h, ERR_BUG); |
| 1714 | |
| 1715 | _copy_region_block_or_regular(read_ptr_mipmap_layer, write_ptr, x, y, width, region_w, region_h, block_w, block_h, region_pitch, pixel_size, block_size); |
| 1716 | |
| 1717 | RDD::BufferTextureCopyRegion copy_region; |
| 1718 | copy_region.buffer_offset = alloc_offset; |
| 1719 | copy_region.row_pitch = region_pitch; |
| 1720 | copy_region.texture_subresource.aspect = texture->read_aspect_flags.has_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT) ? RDD::TEXTURE_ASPECT_DEPTH : RDD::TEXTURE_ASPECT_COLOR; |
| 1721 | copy_region.texture_subresource.mipmap = mm_i; |
| 1722 | copy_region.texture_subresource.layer = p_layer; |
| 1723 | copy_region.texture_offset = Vector3i(x, y, z); |
| 1724 | copy_region.texture_region_size = Vector3i(region_logic_w, region_logic_h, 1); |
| 1725 | |
| 1726 | RDG::RecordedBufferToTextureCopy buffer_to_texture_copy; |
| 1727 | buffer_to_texture_copy.from_buffer = upload_staging_buffers.blocks[upload_staging_buffers.current].driver_id; |
| 1728 | buffer_to_texture_copy.region = copy_region; |
| 1729 | command_buffer_to_texture_copies_vector.push_back(buffer_to_texture_copy); |
| 1730 | |
| 1731 | upload_staging_buffers.blocks.write[upload_staging_buffers.current].fill_amount = alloc_offset + alloc_size; |
| 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | mipmap_offset = image_total; |
| 1737 | logic_width = MAX(1u, logic_width >> 1); |
| 1738 | logic_height = MAX(1u, logic_height >> 1); |
| 1739 | } |
| 1740 | |
| 1741 | if (_texture_make_mutable(texture, p_texture)) { |
| 1742 | // The texture must be mutable to be used as a copy destination. |
| 1743 | draw_graph.add_synchronization(); |
| 1744 | } |
| 1745 | |
| 1746 | draw_graph.add_texture_update(texture->driver_id, texture->draw_tracker, command_buffer_to_texture_copies_vector); |
| 1747 | |
| 1748 | return OK; |
| 1749 | } |
| 1750 | |
| 1751 | void RenderingDevice::_texture_check_shared_fallback(Texture *p_texture) { |
| 1752 | if (p_texture->shared_fallback == nullptr) { |
| 1753 | p_texture->shared_fallback = memnew(Texture::SharedFallback); |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | void RenderingDevice::_texture_update_shared_fallback(RID p_texture_rid, Texture *p_texture, bool p_for_writing) { |
| 1758 | if (p_texture->shared_fallback == nullptr) { |
| 1759 | // This texture does not use any of the shared texture fallbacks. |
| 1760 | return; |
| 1761 | } |
| 1762 | |
| 1763 | if (p_texture->owner.is_valid()) { |
| 1764 | Texture *owner_texture = texture_owner.get_or_null(p_texture->owner); |
| 1765 | ERR_FAIL_NULL(owner_texture); |
| 1766 | if (p_for_writing) { |
| 1767 | // Only the main texture is used for writing when using the shared fallback. |
| 1768 | owner_texture->shared_fallback->revision++; |
| 1769 | } else if (p_texture->shared_fallback->revision != owner_texture->shared_fallback->revision) { |
| 1770 | // Copy the contents of the main texture into the shared texture fallback slice. Update the revision. |
| 1771 | _texture_copy_shared(p_texture->owner, owner_texture, p_texture_rid, p_texture); |
| 1772 | p_texture->shared_fallback->revision = owner_texture->shared_fallback->revision; |
| 1773 | } |
| 1774 | } else if (p_for_writing) { |
| 1775 | // Increment the revision of the texture so shared texture fallback slices must be updated. |
| 1776 | p_texture->shared_fallback->revision++; |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | void RenderingDevice::_texture_free_shared_fallback(Texture *p_texture) { |
| 1781 | if (p_texture->shared_fallback != nullptr) { |
| 1782 | if (p_texture->shared_fallback->texture_tracker != nullptr) { |
| 1783 | RDG::resource_tracker_free(p_texture->shared_fallback->texture_tracker); |
| 1784 | } |
| 1785 | |
| 1786 | if (p_texture->shared_fallback->buffer_tracker != nullptr) { |
| 1787 | RDG::resource_tracker_free(p_texture->shared_fallback->buffer_tracker); |
| 1788 | } |
| 1789 | |
| 1790 | if (p_texture->shared_fallback->texture.id != 0) { |
| 1791 | texture_memory -= driver->texture_get_allocation_size(p_texture->shared_fallback->texture); |
| 1792 | driver->texture_free(p_texture->shared_fallback->texture); |
| 1793 | } |
| 1794 | |
| 1795 | if (p_texture->shared_fallback->buffer.id != 0) { |
| 1796 | buffer_memory -= driver->buffer_get_allocation_size(p_texture->shared_fallback->buffer); |
| 1797 | driver->buffer_free(p_texture->shared_fallback->buffer); |
| 1798 | } |
| 1799 | |
| 1800 | memdelete(p_texture->shared_fallback); |
| 1801 | p_texture->shared_fallback = nullptr; |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | void RenderingDevice::_texture_copy_shared(RID p_src_texture_rid, Texture *p_src_texture, RID p_dst_texture_rid, Texture *p_dst_texture) { |
| 1806 | // The only type of copying allowed is from the main texture to the slice texture, as slice textures are not allowed to be used for writing when using this fallback. |
| 1807 | DEV_ASSERT(p_src_texture != nullptr); |
| 1808 | DEV_ASSERT(p_dst_texture != nullptr); |
| 1809 | DEV_ASSERT(p_src_texture->owner.is_null()); |
| 1810 | DEV_ASSERT(p_dst_texture->owner == p_src_texture_rid); |
| 1811 | |
| 1812 | bool src_made_mutable = _texture_make_mutable(p_src_texture, p_src_texture_rid); |
| 1813 | bool dst_made_mutable = _texture_make_mutable(p_dst_texture, p_dst_texture_rid); |
| 1814 | if (src_made_mutable || dst_made_mutable) { |
| 1815 | draw_graph.add_synchronization(); |
| 1816 | } |
| 1817 | |
| 1818 | if (p_dst_texture->shared_fallback->raw_reinterpretation) { |
| 1819 | // If one of the textures is a main texture and they have a reinterpret buffer, we prefer using that as it's guaranteed to be big enough to hold |
| 1820 | // anything and it's how the shared textures that don't use slices are created. |
| 1821 | bool src_has_buffer = p_src_texture->shared_fallback->buffer.id != 0; |
| 1822 | bool dst_has_buffer = p_dst_texture->shared_fallback->buffer.id != 0; |
| 1823 | bool from_src = p_src_texture->owner.is_null() && src_has_buffer; |
| 1824 | bool from_dst = p_dst_texture->owner.is_null() && dst_has_buffer; |
| 1825 | if (!from_src && !from_dst) { |
| 1826 | // If neither texture passed the condition, we just pick whichever texture has a reinterpretation buffer. |
| 1827 | from_src = src_has_buffer; |
| 1828 | from_dst = dst_has_buffer; |
| 1829 | } |
| 1830 | |
| 1831 | // Pick the buffer and tracker to use from the right texture. |
| 1832 | RDD::BufferID shared_buffer; |
| 1833 | RDG::ResourceTracker *shared_buffer_tracker = nullptr; |
| 1834 | if (from_src) { |
| 1835 | shared_buffer = p_src_texture->shared_fallback->buffer; |
| 1836 | shared_buffer_tracker = p_src_texture->shared_fallback->buffer_tracker; |
| 1837 | } else if (from_dst) { |
| 1838 | shared_buffer = p_dst_texture->shared_fallback->buffer; |
| 1839 | shared_buffer_tracker = p_dst_texture->shared_fallback->buffer_tracker; |
| 1840 | } else { |
| 1841 | DEV_ASSERT(false && "This path should not be reachable."); |
| 1842 | } |
| 1843 | |
| 1844 | // Copying each mipmap from main texture to a buffer and then to the slice texture. |
| 1845 | thread_local LocalVector<RDD::BufferTextureCopyRegion> get_data_vector; |
| 1846 | thread_local LocalVector<RDG::RecordedBufferToTextureCopy> update_vector; |
| 1847 | get_data_vector.clear(); |
| 1848 | update_vector.clear(); |
| 1849 | |
| 1850 | uint32_t buffer_size = 0; |
| 1851 | uint32_t transfer_alignment = driver->api_trait_get(RDD::API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT); |
| 1852 | |
| 1853 | for (uint32_t i = 0; i < p_dst_texture->layers; i++) { |
| 1854 | for (uint32_t j = 0; j < p_dst_texture->mipmaps; j++) { |
| 1855 | // FIXME: When using reinterpretation buffers, the only texture aspect supported is color. Depth or stencil contents won't get copied. |
| 1856 | RDD::TextureSubresource texture_subresource; |
| 1857 | texture_subresource.aspect = RDD::TEXTURE_ASPECT_COLOR; |
| 1858 | texture_subresource.layer = i; |
| 1859 | texture_subresource.mipmap = j; |
| 1860 | |
| 1861 | RDD::TextureCopyableLayout copyable_layout; |
| 1862 | driver->texture_get_copyable_layout(p_dst_texture->shared_fallback->texture, texture_subresource, ©able_layout); |
| 1863 | |
| 1864 | uint32_t mipmap = p_dst_texture->base_mipmap + j; |
| 1865 | |
| 1866 | RDD::BufferTextureCopyRegion get_data_region; |
| 1867 | get_data_region.buffer_offset = STEPIFY(buffer_size, transfer_alignment); |
| 1868 | get_data_region.row_pitch = copyable_layout.row_pitch; |
| 1869 | get_data_region.texture_subresource.aspect = RDD::TEXTURE_ASPECT_COLOR; |
| 1870 | get_data_region.texture_subresource.layer = p_dst_texture->base_layer + i; |
| 1871 | get_data_region.texture_subresource.mipmap = mipmap; |
| 1872 | get_data_region.texture_region_size.x = MAX(1U, p_src_texture->width >> mipmap); |
| 1873 | get_data_region.texture_region_size.y = MAX(1U, p_src_texture->height >> mipmap); |
| 1874 | get_data_region.texture_region_size.z = MAX(1U, p_src_texture->depth >> mipmap); |
| 1875 | get_data_vector.push_back(get_data_region); |
| 1876 | |
| 1877 | RDG::RecordedBufferToTextureCopy update_copy; |
| 1878 | update_copy.from_buffer = shared_buffer; |
| 1879 | update_copy.region.buffer_offset = get_data_region.buffer_offset; |
| 1880 | update_copy.region.row_pitch = get_data_region.row_pitch; |
| 1881 | update_copy.region.texture_subresource.aspect = RDD::TEXTURE_ASPECT_COLOR; |
| 1882 | update_copy.region.texture_subresource.layer = texture_subresource.layer; |
| 1883 | update_copy.region.texture_subresource.mipmap = texture_subresource.mipmap; |
| 1884 | update_copy.region.texture_region_size.x = get_data_region.texture_region_size.x; |
| 1885 | update_copy.region.texture_region_size.y = get_data_region.texture_region_size.y; |
| 1886 | update_copy.region.texture_region_size.z = get_data_region.texture_region_size.z; |
| 1887 | update_vector.push_back(update_copy); |
| 1888 | |
| 1889 | buffer_size = get_data_region.buffer_offset + copyable_layout.size; |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | DEV_ASSERT(buffer_size <= driver->buffer_get_allocation_size(shared_buffer)); |
| 1894 | |
| 1895 | draw_graph.add_texture_get_data(p_src_texture->driver_id, p_src_texture->draw_tracker, shared_buffer, get_data_vector, shared_buffer_tracker); |
| 1896 | draw_graph.add_texture_update(p_dst_texture->shared_fallback->texture, p_dst_texture->shared_fallback->texture_tracker, update_vector, shared_buffer_tracker); |
| 1897 | } else { |
| 1898 | // Raw reinterpretation is not required. Use a regular texture copy. |
| 1899 | RDD::TextureCopyRegion copy_region; |
| 1900 | copy_region.src_subresources.aspect = p_src_texture->read_aspect_flags; |
| 1901 | copy_region.src_subresources.base_layer = p_dst_texture->base_layer; |
| 1902 | copy_region.src_subresources.layer_count = p_dst_texture->layers; |
| 1903 | copy_region.dst_subresources.aspect = p_dst_texture->read_aspect_flags; |
| 1904 | copy_region.dst_subresources.base_layer = 0; |
| 1905 | copy_region.dst_subresources.layer_count = copy_region.src_subresources.layer_count; |
| 1906 | |
| 1907 | // Copying each mipmap from main texture to to the slice texture. |
| 1908 | thread_local LocalVector<RDD::TextureCopyRegion> region_vector; |
| 1909 | region_vector.clear(); |
| 1910 | for (uint32_t i = 0; i < p_dst_texture->mipmaps; i++) { |
| 1911 | uint32_t mipmap = p_dst_texture->base_mipmap + i; |
| 1912 | copy_region.src_subresources.mipmap = mipmap; |
| 1913 | copy_region.dst_subresources.mipmap = i; |
| 1914 | copy_region.size.x = MAX(1U, p_src_texture->width >> mipmap); |
| 1915 | copy_region.size.y = MAX(1U, p_src_texture->height >> mipmap); |
| 1916 | copy_region.size.z = MAX(1U, p_src_texture->depth >> mipmap); |
| 1917 | region_vector.push_back(copy_region); |
| 1918 | } |
| 1919 | |
| 1920 | draw_graph.add_texture_copy(p_src_texture->driver_id, p_src_texture->draw_tracker, p_dst_texture->shared_fallback->texture, p_dst_texture->shared_fallback->texture_tracker, region_vector); |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | void RenderingDevice::_texture_create_reinterpret_buffer(Texture *p_texture) { |
| 1925 | uint64_t row_pitch_step = driver->api_trait_get(RDD::API_TRAIT_TEXTURE_DATA_ROW_PITCH_STEP); |
| 1926 | uint64_t transfer_alignment = driver->api_trait_get(RDD::API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT); |
| 1927 | uint32_t pixel_bytes = get_image_format_pixel_size(p_texture->format); |
| 1928 | uint32_t row_pitch = STEPIFY(p_texture->width * pixel_bytes, row_pitch_step); |
| 1929 | uint64_t buffer_size = STEPIFY(pixel_bytes * row_pitch * p_texture->height * p_texture->depth, transfer_alignment); |
| 1930 | p_texture->shared_fallback->buffer = driver->buffer_create(buffer_size, RDD::BUFFER_USAGE_TRANSFER_FROM_BIT | RDD::BUFFER_USAGE_TRANSFER_TO_BIT, RDD::MEMORY_ALLOCATION_TYPE_GPU, frames_drawn); |
| 1931 | buffer_memory += driver->buffer_get_allocation_size(p_texture->shared_fallback->buffer); |
| 1932 | |
| 1933 | RDG::ResourceTracker *tracker = RDG::resource_tracker_create(); |
| 1934 | tracker->buffer_driver_id = p_texture->shared_fallback->buffer; |
| 1935 | p_texture->shared_fallback->buffer_tracker = tracker; |
| 1936 | } |
| 1937 | |
| 1938 | uint32_t RenderingDevice::_texture_vrs_method_to_usage_bits() const { |
| 1939 | switch (vrs_method) { |
| 1940 | case VRS_METHOD_FRAGMENT_SHADING_RATE: |
| 1941 | return RDD::TEXTURE_USAGE_VRS_FRAGMENT_SHADING_RATE_BIT; |
| 1942 | case VRS_METHOD_FRAGMENT_DENSITY_MAP: |
| 1943 | return RDD::TEXTURE_USAGE_VRS_FRAGMENT_DENSITY_MAP_BIT; |
| 1944 | default: |
| 1945 | return 0; |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | void RenderingDevice::_texture_check_pending_clear(RID p_texture_rid, Texture *p_texture) { |
| 1950 | DEV_ASSERT(p_texture != nullptr); |
| 1951 | |
| 1952 | if (!p_texture->pending_clear) { |
| 1953 | return; |
| 1954 | } |
| 1955 | |
| 1956 | bool clear = true; |
| 1957 | p_texture->pending_clear = false; |
| 1958 | |
| 1959 | if (p_texture->owner.is_valid()) { |
| 1960 | // Check the owner texture instead if it exists. |
| 1961 | p_texture_rid = p_texture->owner; |
| 1962 | p_texture = texture_owner.get_or_null(p_texture_rid); |
| 1963 | clear = p_texture->pending_clear; |
| 1964 | } |
| 1965 | |
| 1966 | if (p_texture != nullptr && clear) { |
| 1967 | if (p_texture->usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) { |
| 1968 | _texture_clear_depth_stencil(p_texture_rid, p_texture, 0.0f, 0, 0, p_texture->mipmaps, 0, p_texture->layers); |
| 1969 | } else { |
| 1970 | _texture_clear_color(p_texture_rid, p_texture, Color(), 0, p_texture->mipmaps, 0, p_texture->layers); |
| 1971 | } |
| 1972 | p_texture->pending_clear = false; |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | void RenderingDevice::_texture_clear_color(RID p_texture_rid, Texture *p_texture, const Color &p_color, uint32_t p_base_mipmap, uint32_t p_mipmaps, uint32_t p_base_layer, uint32_t p_layers) { |
| 1977 | _check_transfer_worker_texture(p_texture); |
| 1978 | |
| 1979 | RDD::TextureSubresourceRange range; |
| 1980 | range.aspect = RDD::TEXTURE_ASPECT_COLOR_BIT; |
| 1981 | range.base_mipmap = p_texture->base_mipmap + p_base_mipmap; |
| 1982 | range.mipmap_count = p_mipmaps; |
| 1983 | range.base_layer = p_texture->base_layer + p_base_layer; |
| 1984 | range.layer_count = p_layers; |
| 1985 | |
| 1986 | // Indicate the texture will get modified for the shared texture fallback. |
| 1987 | _texture_update_shared_fallback(p_texture_rid, p_texture, true); |
| 1988 | |
| 1989 | if (_texture_make_mutable(p_texture, p_texture_rid)) { |
| 1990 | // The texture must be mutable to be used as a clear destination. |
| 1991 | draw_graph.add_synchronization(); |
| 1992 | } |
| 1993 | |
| 1994 | draw_graph.add_texture_clear_color(p_texture->driver_id, p_texture->draw_tracker, p_color, range); |
| 1995 | } |
| 1996 | |
| 1997 | void RenderingDevice::_texture_clear_depth_stencil(RID p_texture_rid, Texture *p_texture, float p_depth, uint8_t p_stencil, uint32_t p_base_mipmap, uint32_t p_mipmaps, uint32_t p_base_layer, uint32_t p_layers) { |
| 1998 | _check_transfer_worker_texture(p_texture); |
| 1999 | |
| 2000 | RDD::TextureSubresourceRange range; |
| 2001 | if (format_has_depth(p_texture->format)) { |
| 2002 | range.aspect.set_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT); |
| 2003 | } |
| 2004 | if (format_has_stencil(p_texture->format)) { |
| 2005 | range.aspect.set_flag(RDD::TEXTURE_ASPECT_STENCIL_BIT); |
| 2006 | } |
| 2007 | range.base_mipmap = p_texture->base_mipmap + p_base_mipmap; |
| 2008 | range.mipmap_count = p_mipmaps; |
| 2009 | range.base_layer = p_texture->base_layer + p_base_layer; |
| 2010 | range.layer_count = p_layers; |
| 2011 | |
| 2012 | // Indicate the texture will get modified for the shared texture fallback. |
| 2013 | _texture_update_shared_fallback(p_texture_rid, p_texture, true); |
| 2014 | |
| 2015 | if (_texture_make_mutable(p_texture, p_texture_rid)) { |
| 2016 | // The texture must be mutable to be used as a clear destination. |
| 2017 | draw_graph.add_synchronization(); |
| 2018 | } |
| 2019 | |
| 2020 | draw_graph.add_texture_clear_depth_stencil(p_texture->driver_id, p_texture->draw_tracker, p_depth, p_stencil, range); |
| 2021 | } |
| 2022 | |
| 2023 | Vector<uint8_t> RenderingDevice::texture_get_data(RID p_texture, uint32_t p_layer) { |
| 2024 | ERR_RENDER_THREAD_GUARD_V(Vector<uint8_t>()); |
| 2025 | |
| 2026 | Texture *tex = texture_owner.get_or_null(p_texture); |
| 2027 | ERR_FAIL_NULL_V(tex, Vector<uint8_t>()); |
| 2028 | |
| 2029 | ERR_FAIL_COND_V_MSG(tex->bound, Vector<uint8_t>(), |
| 2030 | "Texture can't be retrieved while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to retrieve this texture."); |
| 2031 | ERR_FAIL_COND_V_MSG(!(tex->usage_flags & TEXTURE_USAGE_CAN_COPY_FROM_BIT), Vector<uint8_t>(), |
| 2032 | "Texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_FROM_BIT` to be set to be retrieved."); |
| 2033 | |
| 2034 | ERR_FAIL_COND_V(p_layer >= tex->layers, Vector<uint8_t>()); |
| 2035 | |
| 2036 | // Clear the texture if the driver requires it during its first use. |
| 2037 | _texture_check_pending_clear(p_texture, tex); |
| 2038 | |
| 2039 | _check_transfer_worker_texture(tex); |
| 2040 | |
| 2041 | if (tex->usage_flags & TEXTURE_USAGE_CPU_READ_BIT) { |
| 2042 | return driver->texture_get_data(tex->driver_id, p_layer); |
| 2043 | } else { |
| 2044 | RDD::TextureAspect aspect = tex->read_aspect_flags.has_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT) ? RDD::TEXTURE_ASPECT_DEPTH : RDD::TEXTURE_ASPECT_COLOR; |
| 2045 | uint32_t mip_alignment = driver->api_trait_get(RDD::API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT); |
| 2046 | uint32_t buffer_size = 0; |
| 2047 | |
| 2048 | thread_local LocalVector<RDD::TextureCopyableLayout> mip_layouts; |
| 2049 | thread_local LocalVector<RDD::BufferTextureCopyRegion> copy_regions; |
| 2050 | mip_layouts.resize(tex->mipmaps); |
| 2051 | copy_regions.resize(tex->mipmaps); |
| 2052 | |
| 2053 | for (uint32_t i = 0; i < tex->mipmaps; i++) { |
| 2054 | RDD::TextureSubresource subres; |
| 2055 | subres.aspect = aspect; |
| 2056 | subres.layer = p_layer; |
| 2057 | subres.mipmap = i; |
| 2058 | |
| 2059 | RDD::TextureCopyableLayout &mip_layout = mip_layouts[i]; |
| 2060 | driver->texture_get_copyable_layout(tex->driver_id, subres, &mip_layout); |
| 2061 | |
| 2062 | uint32_t mip_offset = STEPIFY(buffer_size, mip_alignment); |
| 2063 | buffer_size = mip_offset + mip_layout.size; |
| 2064 | |
| 2065 | RDD::BufferTextureCopyRegion ©_region = copy_regions[i]; |
| 2066 | copy_region.buffer_offset = mip_offset; |
| 2067 | copy_region.row_pitch = mip_layout.row_pitch; |
| 2068 | copy_region.texture_subresource.aspect = aspect; |
| 2069 | copy_region.texture_subresource.mipmap = i; |
| 2070 | copy_region.texture_subresource.layer = p_layer; |
| 2071 | copy_region.texture_region_size.x = MAX(1u, tex->width >> i); |
| 2072 | copy_region.texture_region_size.y = MAX(1u, tex->height >> i); |
| 2073 | copy_region.texture_region_size.z = MAX(1u, tex->depth >> i); |
| 2074 | } |
| 2075 | |
| 2076 | RDD::BufferID tmp_buffer = driver->buffer_create(buffer_size, RDD::BUFFER_USAGE_TRANSFER_TO_BIT, RDD::MEMORY_ALLOCATION_TYPE_CPU, frames_drawn); |
| 2077 | ERR_FAIL_COND_V(!tmp_buffer, Vector<uint8_t>()); |
| 2078 | |
| 2079 | if (_texture_make_mutable(tex, p_texture)) { |
| 2080 | // The texture must be mutable to be used as a copy source due to layout transitions. |
| 2081 | draw_graph.add_synchronization(); |
| 2082 | } |
| 2083 | |
| 2084 | draw_graph.add_texture_get_data(tex->driver_id, tex->draw_tracker, tmp_buffer, copy_regions); |
| 2085 | |
| 2086 | // Flush everything so memory can be safely mapped. |
| 2087 | _flush_and_stall_for_all_frames(); |
| 2088 | |
| 2089 | const uint8_t *read_ptr = driver->buffer_map(tmp_buffer); |
| 2090 | ERR_FAIL_NULL_V(read_ptr, Vector<uint8_t>()); |
| 2091 | |
| 2092 | uint32_t block_w = 0; |
| 2093 | uint32_t block_h = 0; |
| 2094 | get_compressed_image_format_block_dimensions(tex->format, block_w, block_h); |
| 2095 | |
| 2096 | Vector<uint8_t> buffer_data; |
| 2097 | uint32_t tight_buffer_size = get_image_format_required_size(tex->format, tex->width, tex->height, tex->depth, tex->mipmaps); |
| 2098 | buffer_data.resize(tight_buffer_size); |
| 2099 | |
| 2100 | uint8_t *write_ptr = buffer_data.ptrw(); |
| 2101 | |
| 2102 | for (uint32_t i = 0; i < tex->mipmaps; i++) { |
| 2103 | uint32_t width = 0, height = 0, depth = 0; |
| 2104 | |
| 2105 | uint32_t tight_mip_size = get_image_format_required_size( |
| 2106 | tex->format, |
| 2107 | MAX(1u, tex->width >> i), |
| 2108 | MAX(1u, tex->height >> i), |
| 2109 | MAX(1u, tex->depth >> i), |
| 2110 | 1, |
| 2111 | &width, |
| 2112 | &height, |
| 2113 | &depth); |
| 2114 | |
| 2115 | uint32_t row_count = (height / block_h) * depth; |
| 2116 | uint32_t tight_row_pitch = tight_mip_size / row_count; |
| 2117 | |
| 2118 | const uint8_t *rp = read_ptr + copy_regions[i].buffer_offset; |
| 2119 | uint32_t row_pitch = mip_layouts[i].row_pitch; |
| 2120 | |
| 2121 | if (tight_row_pitch == row_pitch) { |
| 2122 | // Same row pitch, we can copy directly. |
| 2123 | memcpy(write_ptr, rp, tight_mip_size); |
| 2124 | write_ptr += tight_mip_size; |
| 2125 | } else { |
| 2126 | // Copy row-by-row to erase padding. |
| 2127 | for (uint32_t j = 0; j < row_count; j++) { |
| 2128 | memcpy(write_ptr, rp, tight_row_pitch); |
| 2129 | rp += row_pitch; |
| 2130 | write_ptr += tight_row_pitch; |
| 2131 | } |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | driver->buffer_unmap(tmp_buffer); |
| 2136 | driver->buffer_free(tmp_buffer); |
| 2137 | |
| 2138 | return buffer_data; |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | Error RenderingDevice::texture_get_data_async(RID p_texture, uint32_t p_layer, const Callable &p_callback) { |
| 2143 | ERR_RENDER_THREAD_GUARD_V(ERR_UNAVAILABLE); |
| 2144 | |
| 2145 | Texture *tex = texture_owner.get_or_null(p_texture); |
| 2146 | ERR_FAIL_NULL_V(tex, ERR_INVALID_PARAMETER); |
| 2147 | |
| 2148 | ERR_FAIL_COND_V_MSG(tex->bound, ERR_INVALID_PARAMETER, "Texture can't be retrieved while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to retrieve this texture."); |
| 2149 | ERR_FAIL_COND_V_MSG(!(tex->usage_flags & TEXTURE_USAGE_CAN_COPY_FROM_BIT), ERR_INVALID_PARAMETER, "Texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_FROM_BIT` to be set to be retrieved."); |
| 2150 | ERR_FAIL_COND_V(p_layer >= tex->layers, ERR_INVALID_PARAMETER); |
| 2151 | |
| 2152 | // Clear the texture if the driver requires it during its first use. |
| 2153 | _texture_check_pending_clear(p_texture, tex); |
| 2154 | |
| 2155 | _check_transfer_worker_texture(tex); |
| 2156 | |
| 2157 | if (_texture_make_mutable(tex, p_texture)) { |
| 2158 | // The texture must be mutable to be used as a copy source due to layout transitions. |
| 2159 | draw_graph.add_synchronization(); |
| 2160 | } |
| 2161 | |
| 2162 | TextureGetDataRequest get_data_request; |
| 2163 | get_data_request.callback = p_callback; |
| 2164 | get_data_request.frame_local_index = frames[frame].download_buffer_texture_copy_regions.size(); |
| 2165 | get_data_request.width = tex->width; |
| 2166 | get_data_request.height = tex->height; |
| 2167 | get_data_request.depth = tex->depth; |
| 2168 | get_data_request.format = tex->format; |
| 2169 | get_data_request.mipmaps = tex->mipmaps; |
| 2170 | |
| 2171 | uint32_t block_w, block_h; |
| 2172 | get_compressed_image_format_block_dimensions(tex->format, block_w, block_h); |
| 2173 | |
| 2174 | uint32_t pixel_size = get_image_format_pixel_size(tex->format); |
| 2175 | uint32_t pixel_rshift = get_compressed_image_format_pixel_rshift(tex->format); |
| 2176 | |
| 2177 | uint32_t w, h, d; |
| 2178 | uint32_t required_align = driver->api_trait_get(RDD::API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT); |
| 2179 | uint32_t pitch_step = driver->api_trait_get(RDD::API_TRAIT_TEXTURE_DATA_ROW_PITCH_STEP); |
| 2180 | uint32_t region_size = texture_download_region_size_px; |
| 2181 | uint32_t logic_w = tex->width; |
| 2182 | uint32_t logic_h = tex->height; |
| 2183 | uint32_t mipmap_offset = 0; |
| 2184 | uint32_t block_write_offset; |
| 2185 | uint32_t block_write_amount; |
| 2186 | StagingRequiredAction required_action; |
| 2187 | for (uint32_t i = 0; i < tex->mipmaps; i++) { |
| 2188 | uint32_t image_total = get_image_format_required_size(tex->format, tex->width, tex->height, tex->depth, i + 1, &w, &h, &d); |
| 2189 | uint32_t tight_mip_size = image_total - mipmap_offset; |
| 2190 | for (uint32_t z = 0; z < d; z++) { |
| 2191 | for (uint32_t y = 0; y < h; y += region_size) { |
| 2192 | for (uint32_t x = 0; x < w; x += region_size) { |
| 2193 | uint32_t region_w = MIN(region_size, w - x); |
| 2194 | uint32_t region_h = MIN(region_size, h - y); |
| 2195 | ERR_FAIL_COND_V(region_w % block_w, ERR_BUG); |
| 2196 | ERR_FAIL_COND_V(region_h % block_h, ERR_BUG); |
| 2197 | |
| 2198 | uint32_t region_logic_w = MIN(region_size, logic_w - x); |
| 2199 | uint32_t region_logic_h = MIN(region_size, logic_h - y); |
| 2200 | uint32_t region_pitch = (region_w * pixel_size * block_w) >> pixel_rshift; |
| 2201 | region_pitch = STEPIFY(region_pitch, pitch_step); |
| 2202 | |
| 2203 | uint32_t to_allocate = region_pitch * region_h; |
| 2204 | Error err = _staging_buffer_allocate(download_staging_buffers, to_allocate, required_align, block_write_offset, block_write_amount, required_action, false); |
| 2205 | ERR_FAIL_COND_V(err, ERR_CANT_CREATE); |
| 2206 | |
| 2207 | const bool flush_frames = (get_data_request.frame_local_count > 0) && required_action == STAGING_REQUIRED_ACTION_FLUSH_AND_STALL_ALL; |
| 2208 | if (flush_frames) { |
| 2209 | for (uint32_t j = 0; j < get_data_request.frame_local_count; j++) { |
| 2210 | uint32_t local_index = get_data_request.frame_local_index + j; |
| 2211 | draw_graph.add_texture_get_data(tex->driver_id, tex->draw_tracker, frames[frame].download_texture_staging_buffers[local_index], frames[frame].download_buffer_texture_copy_regions[local_index]); |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | _staging_buffer_execute_required_action(download_staging_buffers, required_action); |
| 2216 | |
| 2217 | if (flush_frames) { |
| 2218 | get_data_request.frame_local_count = 0; |
| 2219 | get_data_request.frame_local_index = frames[frame].download_buffer_texture_copy_regions.size(); |
| 2220 | } |
| 2221 | |
| 2222 | RDD::BufferTextureCopyRegion copy_region; |
| 2223 | copy_region.buffer_offset = block_write_offset; |
| 2224 | copy_region.row_pitch = region_pitch; |
| 2225 | copy_region.texture_subresource.aspect = tex->read_aspect_flags.has_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT) ? RDD::TEXTURE_ASPECT_DEPTH : RDD::TEXTURE_ASPECT_COLOR; |
| 2226 | copy_region.texture_subresource.mipmap = i; |
| 2227 | copy_region.texture_subresource.layer = p_layer; |
| 2228 | copy_region.texture_offset = Vector3i(x, y, z); |
| 2229 | copy_region.texture_region_size = Vector3i(region_logic_w, region_logic_h, 1); |
| 2230 | frames[frame].download_texture_staging_buffers.push_back(download_staging_buffers.blocks[download_staging_buffers.current].driver_id); |
| 2231 | frames[frame].download_buffer_texture_copy_regions.push_back(copy_region); |
| 2232 | frames[frame].download_texture_mipmap_offsets.push_back(mipmap_offset + (tight_mip_size / d) * z); |
| 2233 | get_data_request.frame_local_count++; |
| 2234 | |
| 2235 | download_staging_buffers.blocks.write[download_staging_buffers.current].fill_amount = block_write_offset + block_write_amount; |
| 2236 | } |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | mipmap_offset = image_total; |
| 2241 | logic_w = MAX(1u, logic_w >> 1); |
| 2242 | logic_h = MAX(1u, logic_h >> 1); |
| 2243 | } |
| 2244 | |
| 2245 | if (get_data_request.frame_local_count > 0) { |
| 2246 | for (uint32_t i = 0; i < get_data_request.frame_local_count; i++) { |
| 2247 | uint32_t local_index = get_data_request.frame_local_index + i; |
| 2248 | draw_graph.add_texture_get_data(tex->driver_id, tex->draw_tracker, frames[frame].download_texture_staging_buffers[local_index], frames[frame].download_buffer_texture_copy_regions[local_index]); |
| 2249 | } |
| 2250 | |
| 2251 | frames[frame].download_texture_get_data_requests.push_back(get_data_request); |
| 2252 | } |
| 2253 | |
| 2254 | return OK; |
| 2255 | } |
| 2256 | |
| 2257 | bool RenderingDevice::texture_is_shared(RID p_texture) { |
| 2258 | ERR_RENDER_THREAD_GUARD_V(false); |
| 2259 | |
| 2260 | Texture *tex = texture_owner.get_or_null(p_texture); |
| 2261 | ERR_FAIL_NULL_V(tex, false); |
| 2262 | return tex->owner.is_valid(); |
| 2263 | } |
| 2264 | |
| 2265 | bool RenderingDevice::texture_is_valid(RID p_texture) { |
| 2266 | ERR_RENDER_THREAD_GUARD_V(false); |
| 2267 | |
| 2268 | return texture_owner.owns(p_texture); |
| 2269 | } |
| 2270 | |
| 2271 | RD::TextureFormat RenderingDevice::texture_get_format(RID p_texture) { |
| 2272 | ERR_RENDER_THREAD_GUARD_V(TextureFormat()); |
| 2273 | |
| 2274 | Texture *tex = texture_owner.get_or_null(p_texture); |
| 2275 | ERR_FAIL_NULL_V(tex, TextureFormat()); |
| 2276 | |
| 2277 | TextureFormat tf; |
| 2278 | |
| 2279 | tf.format = tex->format; |
| 2280 | tf.width = tex->width; |
| 2281 | tf.height = tex->height; |
| 2282 | tf.depth = tex->depth; |
| 2283 | tf.array_layers = tex->layers; |
| 2284 | tf.mipmaps = tex->mipmaps; |
| 2285 | tf.texture_type = tex->type; |
| 2286 | tf.samples = tex->samples; |
| 2287 | tf.usage_bits = tex->usage_flags; |
| 2288 | tf.shareable_formats = tex->allowed_shared_formats; |
| 2289 | tf.is_resolve_buffer = tex->is_resolve_buffer; |
| 2290 | tf.is_discardable = tex->is_discardable; |
| 2291 | |
| 2292 | return tf; |
| 2293 | } |
| 2294 | |
| 2295 | Size2i RenderingDevice::texture_size(RID p_texture) { |
| 2296 | ERR_RENDER_THREAD_GUARD_V(Size2i()); |
| 2297 | |
| 2298 | Texture *tex = texture_owner.get_or_null(p_texture); |
| 2299 | ERR_FAIL_NULL_V(tex, Size2i()); |
| 2300 | return Size2i(tex->width, tex->height); |
| 2301 | } |
| 2302 | |
| 2303 | #ifndef DISABLE_DEPRECATED |
| 2304 | uint64_t RenderingDevice::texture_get_native_handle(RID p_texture) { |
| 2305 | return get_driver_resource(DRIVER_RESOURCE_TEXTURE, p_texture); |
| 2306 | } |
| 2307 | #endif |
| 2308 | |
| 2309 | Error RenderingDevice::texture_copy(RID p_from_texture, RID p_to_texture, const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_size, uint32_t p_src_mipmap, uint32_t p_dst_mipmap, uint32_t p_src_layer, uint32_t p_dst_layer) { |
| 2310 | ERR_RENDER_THREAD_GUARD_V(ERR_UNAVAILABLE); |
| 2311 | |
| 2312 | Texture *src_tex = texture_owner.get_or_null(p_from_texture); |
| 2313 | ERR_FAIL_NULL_V(src_tex, ERR_INVALID_PARAMETER); |
| 2314 | |
| 2315 | ERR_FAIL_COND_V_MSG(src_tex->bound, ERR_INVALID_PARAMETER, |
| 2316 | "Source texture can't be copied while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to copy this texture."); |
| 2317 | ERR_FAIL_COND_V_MSG(!(src_tex->usage_flags & TEXTURE_USAGE_CAN_COPY_FROM_BIT), ERR_INVALID_PARAMETER, |
| 2318 | "Source texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_FROM_BIT` to be set to be retrieved."); |
| 2319 | |
| 2320 | uint32_t src_width, src_height, src_depth; |
| 2321 | get_image_format_required_size(src_tex->format, src_tex->width, src_tex->height, src_tex->depth, p_src_mipmap + 1, &src_width, &src_height, &src_depth); |
| 2322 | |
| 2323 | ERR_FAIL_COND_V(p_from.x < 0 || p_from.x + p_size.x > src_width, ERR_INVALID_PARAMETER); |
| 2324 | ERR_FAIL_COND_V(p_from.y < 0 || p_from.y + p_size.y > src_height, ERR_INVALID_PARAMETER); |
| 2325 | ERR_FAIL_COND_V(p_from.z < 0 || p_from.z + p_size.z > src_depth, ERR_INVALID_PARAMETER); |
| 2326 | ERR_FAIL_COND_V(p_src_mipmap >= src_tex->mipmaps, ERR_INVALID_PARAMETER); |
| 2327 | ERR_FAIL_COND_V(p_src_layer >= src_tex->layers, ERR_INVALID_PARAMETER); |
| 2328 | |
| 2329 | Texture *dst_tex = texture_owner.get_or_null(p_to_texture); |
| 2330 | ERR_FAIL_NULL_V(dst_tex, ERR_INVALID_PARAMETER); |
| 2331 | |
| 2332 | ERR_FAIL_COND_V_MSG(dst_tex->bound, ERR_INVALID_PARAMETER, |
| 2333 | "Destination texture can't be copied while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to copy this texture."); |
| 2334 | ERR_FAIL_COND_V_MSG(!(dst_tex->usage_flags & TEXTURE_USAGE_CAN_COPY_TO_BIT), ERR_INVALID_PARAMETER, |
| 2335 | "Destination texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_TO_BIT` to be set to be retrieved."); |
| 2336 | |
| 2337 | uint32_t dst_width, dst_height, dst_depth; |
| 2338 | get_image_format_required_size(dst_tex->format, dst_tex->width, dst_tex->height, dst_tex->depth, p_dst_mipmap + 1, &dst_width, &dst_height, &dst_depth); |
| 2339 | |
| 2340 | ERR_FAIL_COND_V(p_to.x < 0 || p_to.x + p_size.x > dst_width, ERR_INVALID_PARAMETER); |
| 2341 | ERR_FAIL_COND_V(p_to.y < 0 || p_to.y + p_size.y > dst_height, ERR_INVALID_PARAMETER); |
| 2342 | ERR_FAIL_COND_V(p_to.z < 0 || p_to.z + p_size.z > dst_depth, ERR_INVALID_PARAMETER); |
| 2343 | ERR_FAIL_COND_V(p_dst_mipmap >= dst_tex->mipmaps, ERR_INVALID_PARAMETER); |
| 2344 | ERR_FAIL_COND_V(p_dst_layer >= dst_tex->layers, ERR_INVALID_PARAMETER); |
| 2345 | |
| 2346 | ERR_FAIL_COND_V_MSG(src_tex->read_aspect_flags != dst_tex->read_aspect_flags, ERR_INVALID_PARAMETER, |
| 2347 | "Source and destination texture must be of the same type (color or depth)."); |
| 2348 | |
| 2349 | // Clear the textures if the driver requires it during its first use. |
| 2350 | _texture_check_pending_clear(p_from_texture, src_tex); |
| 2351 | _texture_check_pending_clear(p_to_texture, dst_tex); |
| 2352 | |
| 2353 | _check_transfer_worker_texture(src_tex); |
| 2354 | _check_transfer_worker_texture(dst_tex); |
| 2355 | |
| 2356 | RDD::TextureCopyRegion copy_region; |
| 2357 | copy_region.src_subresources.aspect = src_tex->read_aspect_flags; |
| 2358 | copy_region.src_subresources.mipmap = p_src_mipmap; |
| 2359 | copy_region.src_subresources.base_layer = p_src_layer; |
| 2360 | copy_region.src_subresources.layer_count = 1; |
| 2361 | copy_region.src_offset = p_from; |
| 2362 | |
| 2363 | copy_region.dst_subresources.aspect = dst_tex->read_aspect_flags; |
| 2364 | copy_region.dst_subresources.mipmap = p_dst_mipmap; |
| 2365 | copy_region.dst_subresources.base_layer = p_dst_layer; |
| 2366 | copy_region.dst_subresources.layer_count = 1; |
| 2367 | copy_region.dst_offset = p_to; |
| 2368 | |
| 2369 | copy_region.size = p_size; |
| 2370 | |
| 2371 | // Indicate the texture will get modified for the shared texture fallback. |
| 2372 | _texture_update_shared_fallback(p_to_texture, dst_tex, true); |
| 2373 | |
| 2374 | // The textures must be mutable to be used in the copy operation. |
| 2375 | bool src_made_mutable = _texture_make_mutable(src_tex, p_from_texture); |
| 2376 | bool dst_made_mutable = _texture_make_mutable(dst_tex, p_to_texture); |
| 2377 | if (src_made_mutable || dst_made_mutable) { |
| 2378 | draw_graph.add_synchronization(); |
| 2379 | } |
| 2380 | |
| 2381 | draw_graph.add_texture_copy(src_tex->driver_id, src_tex->draw_tracker, dst_tex->driver_id, dst_tex->draw_tracker, copy_region); |
| 2382 | |
| 2383 | return OK; |
| 2384 | } |
| 2385 | |
| 2386 | Error RenderingDevice::texture_resolve_multisample(RID p_from_texture, RID p_to_texture) { |
| 2387 | ERR_RENDER_THREAD_GUARD_V(ERR_UNAVAILABLE); |
| 2388 | |
| 2389 | Texture *src_tex = texture_owner.get_or_null(p_from_texture); |
| 2390 | ERR_FAIL_NULL_V(src_tex, ERR_INVALID_PARAMETER); |
| 2391 | |
| 2392 | ERR_FAIL_COND_V_MSG(src_tex->bound, ERR_INVALID_PARAMETER, |
| 2393 | "Source texture can't be copied while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to copy this texture."); |
| 2394 | ERR_FAIL_COND_V_MSG(!(src_tex->usage_flags & TEXTURE_USAGE_CAN_COPY_FROM_BIT), ERR_INVALID_PARAMETER, |
| 2395 | "Source texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_FROM_BIT` to be set to be retrieved."); |
| 2396 | |
| 2397 | ERR_FAIL_COND_V_MSG(src_tex->type != TEXTURE_TYPE_2D, ERR_INVALID_PARAMETER, "Source texture must be 2D (or a slice of a 3D/Cube texture)"); |
| 2398 | ERR_FAIL_COND_V_MSG(src_tex->samples == TEXTURE_SAMPLES_1, ERR_INVALID_PARAMETER, "Source texture must be multisampled."); |
| 2399 | |
| 2400 | Texture *dst_tex = texture_owner.get_or_null(p_to_texture); |
| 2401 | ERR_FAIL_NULL_V(dst_tex, ERR_INVALID_PARAMETER); |
| 2402 | |
| 2403 | ERR_FAIL_COND_V_MSG(dst_tex->bound, ERR_INVALID_PARAMETER, |
| 2404 | "Destination texture can't be copied while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to copy this texture."); |
| 2405 | ERR_FAIL_COND_V_MSG(!(dst_tex->usage_flags & TEXTURE_USAGE_CAN_COPY_TO_BIT), ERR_INVALID_PARAMETER, |
| 2406 | "Destination texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_TO_BIT` to be set to be retrieved."); |
| 2407 | |
| 2408 | ERR_FAIL_COND_V_MSG(dst_tex->type != TEXTURE_TYPE_2D, ERR_INVALID_PARAMETER, "Destination texture must be 2D (or a slice of a 3D/Cube texture)."); |
| 2409 | ERR_FAIL_COND_V_MSG(dst_tex->samples != TEXTURE_SAMPLES_1, ERR_INVALID_PARAMETER, "Destination texture must not be multisampled."); |
| 2410 | |
| 2411 | ERR_FAIL_COND_V_MSG(src_tex->format != dst_tex->format, ERR_INVALID_PARAMETER, "Source and Destination textures must be the same format."); |
| 2412 | ERR_FAIL_COND_V_MSG(src_tex->width != dst_tex->width && src_tex->height != dst_tex->height && src_tex->depth != dst_tex->depth, ERR_INVALID_PARAMETER, "Source and Destination textures must have the same dimensions."); |
| 2413 | |
| 2414 | ERR_FAIL_COND_V_MSG(src_tex->read_aspect_flags != dst_tex->read_aspect_flags, ERR_INVALID_PARAMETER, |
| 2415 | "Source and destination texture must be of the same type (color or depth)."); |
| 2416 | |
| 2417 | // Indicate the texture will get modified for the shared texture fallback. |
| 2418 | _texture_update_shared_fallback(p_to_texture, dst_tex, true); |
| 2419 | |
| 2420 | // Clear the textures if the driver requires it during its first use. |
| 2421 | _texture_check_pending_clear(p_from_texture, src_tex); |
| 2422 | _texture_check_pending_clear(p_to_texture, dst_tex); |
| 2423 | |
| 2424 | _check_transfer_worker_texture(src_tex); |
| 2425 | _check_transfer_worker_texture(dst_tex); |
| 2426 | |
| 2427 | // The textures must be mutable to be used in the resolve operation. |
| 2428 | bool src_made_mutable = _texture_make_mutable(src_tex, p_from_texture); |
| 2429 | bool dst_made_mutable = _texture_make_mutable(dst_tex, p_to_texture); |
| 2430 | if (src_made_mutable || dst_made_mutable) { |
| 2431 | draw_graph.add_synchronization(); |
| 2432 | } |
| 2433 | |
| 2434 | draw_graph.add_texture_resolve(src_tex->driver_id, src_tex->draw_tracker, dst_tex->driver_id, dst_tex->draw_tracker, src_tex->base_layer, src_tex->base_mipmap, dst_tex->base_layer, dst_tex->base_mipmap); |
| 2435 | |
| 2436 | return OK; |
| 2437 | } |
| 2438 | |
| 2439 | void RenderingDevice::texture_set_discardable(RID p_texture, bool p_discardable) { |
| 2440 | ERR_RENDER_THREAD_GUARD(); |
| 2441 | |
| 2442 | Texture *texture = texture_owner.get_or_null(p_texture); |
| 2443 | ERR_FAIL_NULL(texture); |
| 2444 | |
| 2445 | texture->is_discardable = p_discardable; |
| 2446 | |
| 2447 | if (texture->draw_tracker != nullptr) { |
| 2448 | texture->draw_tracker->is_discardable = p_discardable; |
| 2449 | } |
| 2450 | |
| 2451 | if (texture->shared_fallback != nullptr && texture->shared_fallback->texture_tracker != nullptr) { |
| 2452 | texture->shared_fallback->texture_tracker->is_discardable = p_discardable; |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | bool RenderingDevice::texture_is_discardable(RID p_texture) { |
| 2457 | ERR_RENDER_THREAD_GUARD_V(false); |
| 2458 | |
| 2459 | Texture *texture = texture_owner.get_or_null(p_texture); |
| 2460 | ERR_FAIL_NULL_V(texture, false); |
| 2461 | |
| 2462 | return texture->is_discardable; |
| 2463 | } |
| 2464 | |
| 2465 | Error RenderingDevice::texture_clear(RID p_texture, const Color &p_color, uint32_t p_base_mipmap, uint32_t p_mipmaps, uint32_t p_base_layer, uint32_t p_layers) { |
| 2466 | ERR_RENDER_THREAD_GUARD_V(ERR_UNAVAILABLE); |
| 2467 | |
| 2468 | Texture *src_tex = texture_owner.get_or_null(p_texture); |
| 2469 | ERR_FAIL_NULL_V(src_tex, ERR_INVALID_PARAMETER); |
| 2470 | |
| 2471 | ERR_FAIL_COND_V_MSG(src_tex->bound, ERR_INVALID_PARAMETER, |
| 2472 | "Source texture can't be cleared while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to clear this texture."); |
| 2473 | |
| 2474 | ERR_FAIL_COND_V(p_layers == 0, ERR_INVALID_PARAMETER); |
| 2475 | ERR_FAIL_COND_V(p_mipmaps == 0, ERR_INVALID_PARAMETER); |
| 2476 | |
| 2477 | ERR_FAIL_COND_V_MSG(!(src_tex->usage_flags & TEXTURE_USAGE_CAN_COPY_TO_BIT), ERR_INVALID_PARAMETER, |
| 2478 | "Source texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_TO_BIT` to be set to be cleared."); |
| 2479 | |
| 2480 | ERR_FAIL_COND_V(p_base_mipmap + p_mipmaps > src_tex->mipmaps, ERR_INVALID_PARAMETER); |
| 2481 | ERR_FAIL_COND_V(p_base_layer + p_layers > src_tex->layers, ERR_INVALID_PARAMETER); |
| 2482 | |
| 2483 | // Clear the texture if the driver requires it during its first use. |
| 2484 | _texture_check_pending_clear(p_texture, src_tex); |
| 2485 | |
| 2486 | _texture_clear_color(p_texture, src_tex, p_color, p_base_mipmap, p_mipmaps, p_base_layer, p_layers); |
| 2487 | |
| 2488 | return OK; |
| 2489 | } |
| 2490 | |
| 2491 | bool RenderingDevice::texture_is_format_supported_for_usage(DataFormat p_format, BitField<RenderingDevice::TextureUsageBits> p_usage) const { |
| 2492 | ERR_FAIL_INDEX_V(p_format, DATA_FORMAT_MAX, false); |
| 2493 | |
| 2494 | bool cpu_readable = (p_usage & RDD::TEXTURE_USAGE_CPU_READ_BIT); |
| 2495 | BitField<TextureUsageBits> supported = driver->texture_get_usages_supported_by_format(p_format, cpu_readable); |
| 2496 | bool any_unsupported = (((int64_t)supported) | ((int64_t)p_usage)) != ((int64_t)supported); |
| 2497 | return !any_unsupported; |
| 2498 | } |
| 2499 | |
| 2500 | /*********************/ |
| 2501 | /**** FRAMEBUFFER ****/ |
| 2502 | /*********************/ |
| 2503 | |
| 2504 | RDD::RenderPassID RenderingDevice::_render_pass_create(RenderingDeviceDriver *p_driver, const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, VectorView<RDD::AttachmentLoadOp> p_load_ops, VectorView<RDD::AttachmentStoreOp> p_store_ops, uint32_t p_view_count, VRSMethod p_vrs_method, int32_t p_vrs_attachment, Size2i p_vrs_texel_size, Vector<TextureSamples> *r_samples) { |
| 2505 | // NOTE: |
| 2506 | // Before the refactor to RenderingDevice-RenderingDeviceDriver, there was commented out code to |
| 2507 | // specify dependencies to external subpasses. Since it had been unused for a long timel it wasn't ported |
| 2508 | // to the new architecture. |
| 2509 | |
| 2510 | LocalVector<int32_t> attachment_last_pass; |
| 2511 | attachment_last_pass.resize(p_attachments.size()); |
| 2512 | |
| 2513 | if (p_view_count > 1) { |
| 2514 | const RDD::MultiviewCapabilities &capabilities = p_driver->get_multiview_capabilities(); |
| 2515 | |
| 2516 | // This only works with multiview! |
| 2517 | ERR_FAIL_COND_V_MSG(!capabilities.is_supported, RDD::RenderPassID(), "Multiview not supported"); |
| 2518 | |
| 2519 | // Make sure we limit this to the number of views we support. |
| 2520 | ERR_FAIL_COND_V_MSG(p_view_count > capabilities.max_view_count, RDD::RenderPassID(), "Hardware does not support requested number of views for Multiview render pass"); |
| 2521 | } |
| 2522 | |
| 2523 | LocalVector<RDD::Attachment> attachments; |
| 2524 | LocalVector<uint32_t> attachment_remap; |
| 2525 | |
| 2526 | for (int i = 0; i < p_attachments.size(); i++) { |
| 2527 | if (p_attachments[i].usage_flags == AttachmentFormat::UNUSED_ATTACHMENT) { |
| 2528 | attachment_remap.push_back(RDD::AttachmentReference::UNUSED); |
| 2529 | continue; |
| 2530 | } |
| 2531 | |
| 2532 | ERR_FAIL_INDEX_V(p_attachments[i].format, DATA_FORMAT_MAX, RDD::RenderPassID()); |
| 2533 | ERR_FAIL_INDEX_V(p_attachments[i].samples, TEXTURE_SAMPLES_MAX, RDD::RenderPassID()); |
| 2534 | ERR_FAIL_COND_V_MSG(!(p_attachments[i].usage_flags & (TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT | TEXTURE_USAGE_INPUT_ATTACHMENT_BIT | TEXTURE_USAGE_VRS_ATTACHMENT_BIT)), |
| 2535 | RDD::RenderPassID(), "Texture format for index (" + itos(i) + ") requires an attachment (color, depth-stencil, input or VRS) bit set."); |
| 2536 | |
| 2537 | RDD::Attachment description; |
| 2538 | description.format = p_attachments[i].format; |
| 2539 | description.samples = p_attachments[i].samples; |
| 2540 | |
| 2541 | // We can setup a framebuffer where we write to our VRS texture to set it up. |
| 2542 | // We make the assumption here that if our texture is actually used as our VRS attachment. |
| 2543 | // It is used as such for each subpass. This is fairly certain seeing the restrictions on subpasses. |
| 2544 | bool is_vrs = (p_attachments[i].usage_flags & TEXTURE_USAGE_VRS_ATTACHMENT_BIT) && i == p_vrs_attachment; |
| 2545 | if (is_vrs) { |
| 2546 | description.load_op = RDD::ATTACHMENT_LOAD_OP_LOAD; |
| 2547 | description.store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE; |
| 2548 | description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE; |
| 2549 | description.stencil_store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE; |
| 2550 | description.initial_layout = _vrs_layout_from_method(p_vrs_method); |
| 2551 | description.final_layout = _vrs_layout_from_method(p_vrs_method); |
| 2552 | } else { |
| 2553 | if (p_attachments[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) { |
| 2554 | description.load_op = p_load_ops[i]; |
| 2555 | description.store_op = p_store_ops[i]; |
| 2556 | description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE; |
| 2557 | description.stencil_store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE; |
| 2558 | description.initial_layout = RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 2559 | description.final_layout = RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 2560 | } else if (p_attachments[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) { |
| 2561 | description.load_op = p_load_ops[i]; |
| 2562 | description.store_op = p_store_ops[i]; |
| 2563 | description.stencil_load_op = p_load_ops[i]; |
| 2564 | description.stencil_store_op = p_store_ops[i]; |
| 2565 | description.initial_layout = RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 2566 | description.final_layout = RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 2567 | } else if (p_attachments[i].usage_flags & TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT) { |
| 2568 | description.load_op = p_load_ops[i]; |
| 2569 | description.store_op = p_store_ops[i]; |
| 2570 | description.stencil_load_op = p_load_ops[i]; |
| 2571 | description.stencil_store_op = p_store_ops[i]; |
| 2572 | description.initial_layout = RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 2573 | description.final_layout = RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 2574 | } else { |
| 2575 | description.load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE; |
| 2576 | description.store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE; |
| 2577 | description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE; |
| 2578 | description.stencil_store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE; |
| 2579 | description.initial_layout = RDD::TEXTURE_LAYOUT_UNDEFINED; |
| 2580 | description.final_layout = RDD::TEXTURE_LAYOUT_UNDEFINED; |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | attachment_last_pass[i] = -1; |
| 2585 | attachment_remap.push_back(attachments.size()); |
| 2586 | attachments.push_back(description); |
| 2587 | } |
| 2588 | |
| 2589 | LocalVector<RDD::Subpass> subpasses; |
| 2590 | subpasses.resize(p_passes.size()); |
| 2591 | LocalVector<RDD::SubpassDependency> subpass_dependencies; |
| 2592 | |
| 2593 | for (int i = 0; i < p_passes.size(); i++) { |
| 2594 | const FramebufferPass *pass = &p_passes[i]; |
| 2595 | RDD::Subpass &subpass = subpasses[i]; |
| 2596 | |
| 2597 | TextureSamples texture_samples = TEXTURE_SAMPLES_1; |
| 2598 | bool is_multisample_first = true; |
| 2599 | |
| 2600 | for (int j = 0; j < pass->color_attachments.size(); j++) { |
| 2601 | int32_t attachment = pass->color_attachments[j]; |
| 2602 | RDD::AttachmentReference reference; |
| 2603 | if (attachment == ATTACHMENT_UNUSED) { |
| 2604 | reference.attachment = RDD::AttachmentReference::UNUSED; |
| 2605 | reference.layout = RDD::TEXTURE_LAYOUT_UNDEFINED; |
| 2606 | } else { |
| 2607 | ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), color attachment (" + itos(j) + ")."); |
| 2608 | ERR_FAIL_COND_V_MSG(!(p_attachments[attachment].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it's marked as depth, but it's not usable as color attachment."); |
| 2609 | ERR_FAIL_COND_V_MSG(attachment_last_pass[attachment] == i, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it already was used for something else before in this pass."); |
| 2610 | |
| 2611 | if (is_multisample_first) { |
| 2612 | texture_samples = p_attachments[attachment].samples; |
| 2613 | is_multisample_first = false; |
| 2614 | } else { |
| 2615 | ERR_FAIL_COND_V_MSG(texture_samples != p_attachments[attachment].samples, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), if an attachment is marked as multisample, all of them should be multisample and use the same number of samples."); |
| 2616 | } |
| 2617 | reference.attachment = attachment_remap[attachment]; |
| 2618 | reference.layout = RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 2619 | attachment_last_pass[attachment] = i; |
| 2620 | } |
| 2621 | reference.aspect = RDD::TEXTURE_ASPECT_COLOR_BIT; |
| 2622 | subpass.color_references.push_back(reference); |
| 2623 | } |
| 2624 | |
| 2625 | for (int j = 0; j < pass->input_attachments.size(); j++) { |
| 2626 | int32_t attachment = pass->input_attachments[j]; |
| 2627 | RDD::AttachmentReference reference; |
| 2628 | if (attachment == ATTACHMENT_UNUSED) { |
| 2629 | reference.attachment = RDD::AttachmentReference::UNUSED; |
| 2630 | reference.layout = RDD::TEXTURE_LAYOUT_UNDEFINED; |
| 2631 | } else { |
| 2632 | ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), input attachment (" + itos(j) + ")."); |
| 2633 | ERR_FAIL_COND_V_MSG(!(p_attachments[attachment].usage_flags & TEXTURE_USAGE_INPUT_ATTACHMENT_BIT), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it isn't marked as an input texture."); |
| 2634 | ERR_FAIL_COND_V_MSG(attachment_last_pass[attachment] == i, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it already was used for something else before in this pass."); |
| 2635 | reference.attachment = attachment_remap[attachment]; |
| 2636 | reference.layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 2637 | attachment_last_pass[attachment] = i; |
| 2638 | } |
| 2639 | reference.aspect = RDD::TEXTURE_ASPECT_COLOR_BIT; |
| 2640 | subpass.input_references.push_back(reference); |
| 2641 | } |
| 2642 | |
| 2643 | if (pass->resolve_attachments.size() > 0) { |
| 2644 | ERR_FAIL_COND_V_MSG(pass->resolve_attachments.size() != pass->color_attachments.size(), RDD::RenderPassID(), "The amount of resolve attachments (" + itos(pass->resolve_attachments.size()) + ") must match the number of color attachments (" + itos(pass->color_attachments.size()) + ")."); |
| 2645 | ERR_FAIL_COND_V_MSG(texture_samples == TEXTURE_SAMPLES_1, RDD::RenderPassID(), "Resolve attachments specified, but color attachments are not multisample."); |
| 2646 | } |
| 2647 | for (int j = 0; j < pass->resolve_attachments.size(); j++) { |
| 2648 | int32_t attachment = pass->resolve_attachments[j]; |
| 2649 | attachments[attachment].load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE; |
| 2650 | |
| 2651 | RDD::AttachmentReference reference; |
| 2652 | if (attachment == ATTACHMENT_UNUSED) { |
| 2653 | reference.attachment = RDD::AttachmentReference::UNUSED; |
| 2654 | reference.layout = RDD::TEXTURE_LAYOUT_UNDEFINED; |
| 2655 | } else { |
| 2656 | ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), resolve attachment (" + itos(j) + ")."); |
| 2657 | ERR_FAIL_COND_V_MSG(pass->color_attachments[j] == ATTACHMENT_UNUSED, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), resolve attachment (" + itos(j) + "), the respective color attachment is marked as unused."); |
| 2658 | ERR_FAIL_COND_V_MSG(!(p_attachments[attachment].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), resolve attachment, it isn't marked as a color texture."); |
| 2659 | ERR_FAIL_COND_V_MSG(attachment_last_pass[attachment] == i, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it already was used for something else before in this pass."); |
| 2660 | bool multisample = p_attachments[attachment].samples > TEXTURE_SAMPLES_1; |
| 2661 | ERR_FAIL_COND_V_MSG(multisample, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), resolve attachments can't be multisample."); |
| 2662 | reference.attachment = attachment_remap[attachment]; |
| 2663 | reference.layout = RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; // RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL |
| 2664 | attachment_last_pass[attachment] = i; |
| 2665 | } |
| 2666 | reference.aspect = RDD::TEXTURE_ASPECT_COLOR_BIT; |
| 2667 | subpass.resolve_references.push_back(reference); |
| 2668 | } |
| 2669 | |
| 2670 | if (pass->depth_attachment != ATTACHMENT_UNUSED) { |
| 2671 | int32_t attachment = pass->depth_attachment; |
| 2672 | ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer depth format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), depth attachment."); |
| 2673 | ERR_FAIL_COND_V_MSG(!(p_attachments[attachment].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT), RDD::RenderPassID(), "Invalid framebuffer depth format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it's marked as depth, but it's not a depth attachment."); |
| 2674 | ERR_FAIL_COND_V_MSG(attachment_last_pass[attachment] == i, RDD::RenderPassID(), "Invalid framebuffer depth format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it already was used for something else before in this pass."); |
| 2675 | subpass.depth_stencil_reference.attachment = attachment_remap[attachment]; |
| 2676 | subpass.depth_stencil_reference.layout = RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 2677 | attachment_last_pass[attachment] = i; |
| 2678 | |
| 2679 | if (is_multisample_first) { |
| 2680 | texture_samples = p_attachments[attachment].samples; |
| 2681 | is_multisample_first = false; |
| 2682 | } else { |
| 2683 | ERR_FAIL_COND_V_MSG(texture_samples != p_attachments[attachment].samples, RDD::RenderPassID(), "Invalid framebuffer depth format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), if an attachment is marked as multisample, all of them should be multisample and use the same number of samples including the depth."); |
| 2684 | } |
| 2685 | |
| 2686 | if (pass->depth_resolve_attachment != ATTACHMENT_UNUSED) { |
| 2687 | attachment = pass->depth_resolve_attachment; |
| 2688 | |
| 2689 | // As our fallbacks are handled outside of our pass, we should never be setting up a render pass with a depth resolve attachment when not supported. |
| 2690 | ERR_FAIL_COND_V_MSG(!p_driver->has_feature(SUPPORTS_FRAMEBUFFER_DEPTH_RESOLVE), RDD::RenderPassID(), "Invalid framebuffer depth format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), a depth resolve attachment was supplied when driver doesn't support this feature."); |
| 2691 | |
| 2692 | ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer depth resolve format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), depth resolve attachment."); |
| 2693 | ERR_FAIL_COND_V_MSG(!(p_attachments[attachment].usage_flags & TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT), RDD::RenderPassID(), "Invalid framebuffer depth resolve format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it's marked as depth, but it's not a depth resolve attachment."); |
| 2694 | ERR_FAIL_COND_V_MSG(attachment_last_pass[attachment] == i, RDD::RenderPassID(), "Invalid framebuffer depth resolve format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it already was used for something else before in this pass."); |
| 2695 | |
| 2696 | subpass.depth_resolve_reference.attachment = attachment_remap[attachment]; |
| 2697 | subpass.depth_resolve_reference.layout = RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 2698 | attachment_last_pass[attachment] = i; |
| 2699 | } |
| 2700 | |
| 2701 | } else { |
| 2702 | subpass.depth_stencil_reference.attachment = RDD::AttachmentReference::UNUSED; |
| 2703 | subpass.depth_stencil_reference.layout = RDD::TEXTURE_LAYOUT_UNDEFINED; |
| 2704 | } |
| 2705 | |
| 2706 | if (p_vrs_method == VRS_METHOD_FRAGMENT_SHADING_RATE && p_vrs_attachment >= 0) { |
| 2707 | int32_t attachment = p_vrs_attachment; |
| 2708 | ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer VRS format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), VRS attachment."); |
| 2709 | ERR_FAIL_COND_V_MSG(!(p_attachments[attachment].usage_flags & TEXTURE_USAGE_VRS_ATTACHMENT_BIT), RDD::RenderPassID(), "Invalid framebuffer VRS format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it's marked as VRS, but it's not a VRS attachment."); |
| 2710 | ERR_FAIL_COND_V_MSG(attachment_last_pass[attachment] == i, RDD::RenderPassID(), "Invalid framebuffer VRS attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it already was used for something else before in this pass."); |
| 2711 | |
| 2712 | subpass.fragment_shading_rate_reference.attachment = attachment_remap[attachment]; |
| 2713 | subpass.fragment_shading_rate_reference.layout = RDD::TEXTURE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL; |
| 2714 | subpass.fragment_shading_rate_texel_size = p_vrs_texel_size; |
| 2715 | |
| 2716 | attachment_last_pass[attachment] = i; |
| 2717 | } |
| 2718 | |
| 2719 | for (int j = 0; j < pass->preserve_attachments.size(); j++) { |
| 2720 | int32_t attachment = pass->preserve_attachments[j]; |
| 2721 | |
| 2722 | ERR_FAIL_COND_V_MSG(attachment == ATTACHMENT_UNUSED, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), preserve attachment (" + itos(j) + "). Preserve attachments can't be unused."); |
| 2723 | |
| 2724 | ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), preserve attachment (" + itos(j) + ")."); |
| 2725 | |
| 2726 | if (attachment_last_pass[attachment] != i) { |
| 2727 | // Preserve can still be used to keep depth or color from being discarded after use. |
| 2728 | attachment_last_pass[attachment] = i; |
| 2729 | subpasses[i].preserve_attachments.push_back(attachment); |
| 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | if (r_samples) { |
| 2734 | r_samples->push_back(texture_samples); |
| 2735 | } |
| 2736 | |
| 2737 | if (i > 0) { |
| 2738 | RDD::SubpassDependency dependency; |
| 2739 | dependency.src_subpass = i - 1; |
| 2740 | dependency.dst_subpass = i; |
| 2741 | dependency.src_stages = (RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | RDD::PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | RDD::PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT); |
| 2742 | dependency.dst_stages = (RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | RDD::PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | RDD::PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT); |
| 2743 | dependency.src_access = (RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT); |
| 2744 | dependency.dst_access = (RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_INPUT_ATTACHMENT_READ_BIT); |
| 2745 | subpass_dependencies.push_back(dependency); |
| 2746 | } |
| 2747 | } |
| 2748 | |
| 2749 | RDD::AttachmentReference fragment_density_map_attachment_reference; |
| 2750 | if (p_vrs_method == VRS_METHOD_FRAGMENT_DENSITY_MAP && p_vrs_attachment >= 0) { |
| 2751 | fragment_density_map_attachment_reference.attachment = p_vrs_attachment; |
| 2752 | fragment_density_map_attachment_reference.layout = RDD::TEXTURE_LAYOUT_FRAGMENT_DENSITY_MAP_ATTACHMENT_OPTIMAL; |
| 2753 | } |
| 2754 | |
| 2755 | RDD::RenderPassID render_pass = p_driver->render_pass_create(attachments, subpasses, subpass_dependencies, p_view_count, fragment_density_map_attachment_reference); |
| 2756 | ERR_FAIL_COND_V(!render_pass, RDD::RenderPassID()); |
| 2757 | |
| 2758 | return render_pass; |
| 2759 | } |
| 2760 | |
| 2761 | RDD::RenderPassID RenderingDevice::_render_pass_create_from_graph(RenderingDeviceDriver *p_driver, VectorView<RDD::AttachmentLoadOp> p_load_ops, VectorView<RDD::AttachmentStoreOp> p_store_ops, void *p_user_data) { |
| 2762 | DEV_ASSERT(p_driver != nullptr); |
| 2763 | DEV_ASSERT(p_user_data != nullptr); |
| 2764 | |
| 2765 | // The graph delegates the creation of the render pass to the user according to the load and store ops that were determined as necessary after |
| 2766 | // resolving the dependencies between commands. This function creates a render pass for the framebuffer accordingly. |
| 2767 | const FramebufferFormatKey *key = (const FramebufferFormatKey *)(p_user_data); |
| 2768 | return _render_pass_create(p_driver, key->attachments, key->passes, p_load_ops, p_store_ops, key->view_count, key->vrs_method, key->vrs_attachment, key->vrs_texel_size); |
| 2769 | } |
| 2770 | |
| 2771 | RDG::ResourceUsage RenderingDevice::_vrs_usage_from_method(VRSMethod p_method) { |
| 2772 | switch (p_method) { |
| 2773 | case VRS_METHOD_FRAGMENT_SHADING_RATE: |
| 2774 | return RDG::RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ; |
| 2775 | case VRS_METHOD_FRAGMENT_DENSITY_MAP: |
| 2776 | return RDG::RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ; |
| 2777 | default: |
| 2778 | return RDG::RESOURCE_USAGE_NONE; |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | RDD::PipelineStageBits RenderingDevice::_vrs_stages_from_method(VRSMethod p_method) { |
| 2783 | switch (p_method) { |
| 2784 | case VRS_METHOD_FRAGMENT_SHADING_RATE: |
| 2785 | return RDD::PIPELINE_STAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT; |
| 2786 | case VRS_METHOD_FRAGMENT_DENSITY_MAP: |
| 2787 | return RDD::PIPELINE_STAGE_FRAGMENT_DENSITY_PROCESS_BIT; |
| 2788 | default: |
| 2789 | return RDD::PipelineStageBits(0); |
| 2790 | } |
| 2791 | } |
| 2792 | |
| 2793 | RDD::TextureLayout RenderingDevice::_vrs_layout_from_method(VRSMethod p_method) { |
| 2794 | switch (p_method) { |
| 2795 | case VRS_METHOD_FRAGMENT_SHADING_RATE: |
| 2796 | return RDD::TEXTURE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL; |
| 2797 | case VRS_METHOD_FRAGMENT_DENSITY_MAP: |
| 2798 | return RDD::TEXTURE_LAYOUT_FRAGMENT_DENSITY_MAP_ATTACHMENT_OPTIMAL; |
| 2799 | default: |
| 2800 | return RDD::TEXTURE_LAYOUT_UNDEFINED; |
| 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | void RenderingDevice::_vrs_detect_method() { |
| 2805 | const RDD::FragmentShadingRateCapabilities &fsr_capabilities = driver->get_fragment_shading_rate_capabilities(); |
| 2806 | const RDD::FragmentDensityMapCapabilities &fdm_capabilities = driver->get_fragment_density_map_capabilities(); |
| 2807 | if (fsr_capabilities.attachment_supported) { |
| 2808 | vrs_method = VRS_METHOD_FRAGMENT_SHADING_RATE; |
| 2809 | } else if (fdm_capabilities.attachment_supported) { |
| 2810 | vrs_method = VRS_METHOD_FRAGMENT_DENSITY_MAP; |
| 2811 | } |
| 2812 | |
| 2813 | switch (vrs_method) { |
| 2814 | case VRS_METHOD_FRAGMENT_SHADING_RATE: |
| 2815 | vrs_format = DATA_FORMAT_R8_UINT; |
| 2816 | vrs_texel_size = Vector2i(16, 16).clamp(fsr_capabilities.min_texel_size, fsr_capabilities.max_texel_size); |
| 2817 | break; |
| 2818 | case VRS_METHOD_FRAGMENT_DENSITY_MAP: |
| 2819 | vrs_format = DATA_FORMAT_R8G8_UNORM; |
| 2820 | vrs_texel_size = Vector2i(32, 32).clamp(fdm_capabilities.min_texel_size, fdm_capabilities.max_texel_size); |
| 2821 | break; |
| 2822 | default: |
| 2823 | break; |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | RD::VRSMethod RenderingDevice::vrs_get_method() const { |
| 2828 | return vrs_method; |
| 2829 | } |
| 2830 | |
| 2831 | RD::DataFormat RenderingDevice::vrs_get_format() const { |
| 2832 | return vrs_format; |
| 2833 | } |
| 2834 | |
| 2835 | Size2i RenderingDevice::vrs_get_texel_size() const { |
| 2836 | return vrs_texel_size; |
| 2837 | } |
| 2838 | |
| 2839 | RenderingDevice::FramebufferFormatID RenderingDevice::framebuffer_format_create(const Vector<AttachmentFormat> &p_format, uint32_t p_view_count, int32_t p_fragment_density_map_attachment) { |
| 2840 | FramebufferPass pass; |
| 2841 | for (int i = 0; i < p_format.size(); i++) { |
| 2842 | if (p_format[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) { |
| 2843 | pass.depth_attachment = i; |
| 2844 | } else if (p_format[i].usage_flags & TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT) { |
| 2845 | pass.depth_resolve_attachment = i; |
| 2846 | } else { |
| 2847 | pass.color_attachments.push_back(i); |
| 2848 | } |
| 2849 | } |
| 2850 | |
| 2851 | Vector<FramebufferPass> passes; |
| 2852 | passes.push_back(pass); |
| 2853 | return framebuffer_format_create_multipass(p_format, passes, p_view_count, p_fragment_density_map_attachment); |
| 2854 | } |
| 2855 | |
| 2856 | RenderingDevice::FramebufferFormatID RenderingDevice::framebuffer_format_create_multipass(const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, uint32_t p_view_count, int32_t p_vrs_attachment) { |
| 2857 | _THREAD_SAFE_METHOD_ |
| 2858 | |
| 2859 | FramebufferFormatKey key; |
| 2860 | key.attachments = p_attachments; |
| 2861 | key.passes = p_passes; |
| 2862 | key.view_count = p_view_count; |
| 2863 | key.vrs_method = vrs_method; |
| 2864 | key.vrs_attachment = p_vrs_attachment; |
| 2865 | key.vrs_texel_size = vrs_texel_size; |
| 2866 | |
| 2867 | const RBMap<FramebufferFormatKey, FramebufferFormatID>::Element *E = framebuffer_format_cache.find(key); |
| 2868 | if (E) { |
| 2869 | // Exists, return. |
| 2870 | return E->get(); |
| 2871 | } |
| 2872 | |
| 2873 | Vector<TextureSamples> samples; |
| 2874 | LocalVector<RDD::AttachmentLoadOp> load_ops; |
| 2875 | LocalVector<RDD::AttachmentStoreOp> store_ops; |
| 2876 | for (int64_t i = 0; i < p_attachments.size(); i++) { |
| 2877 | load_ops.push_back(RDD::ATTACHMENT_LOAD_OP_CLEAR); |
| 2878 | store_ops.push_back(RDD::ATTACHMENT_STORE_OP_STORE); |
| 2879 | } |
| 2880 | |
| 2881 | RDD::RenderPassID render_pass = _render_pass_create(driver, p_attachments, p_passes, load_ops, store_ops, p_view_count, vrs_method, p_vrs_attachment, vrs_texel_size, &samples); // Actions don't matter for this use case. |
| 2882 | if (!render_pass) { // Was likely invalid. |
| 2883 | return INVALID_ID; |
| 2884 | } |
| 2885 | |
| 2886 | FramebufferFormatID id = FramebufferFormatID(framebuffer_format_cache.size()) | (FramebufferFormatID(ID_TYPE_FRAMEBUFFER_FORMAT) << FramebufferFormatID(ID_BASE_SHIFT)); |
| 2887 | E = framebuffer_format_cache.insert(key, id); |
| 2888 | |
| 2889 | FramebufferFormat fb_format; |
| 2890 | fb_format.E = E; |
| 2891 | fb_format.render_pass = render_pass; |
| 2892 | fb_format.pass_samples = samples; |
| 2893 | fb_format.view_count = p_view_count; |
| 2894 | framebuffer_formats[id] = fb_format; |
| 2895 | |
| 2896 | #if PRINT_FRAMEBUFFER_FORMAT |
| 2897 | print_line("FRAMEBUFFER FORMAT:", id, "ATTACHMENTS:", p_attachments.size(), "PASSES:", p_passes.size()); |
| 2898 | for (RD::AttachmentFormat attachment : p_attachments) { |
| 2899 | print_line("FORMAT:", attachment.format, "SAMPLES:", attachment.samples, "USAGE FLAGS:", attachment.usage_flags); |
| 2900 | } |
| 2901 | #endif |
| 2902 | |
| 2903 | return id; |
| 2904 | } |
| 2905 | |
| 2906 | RenderingDevice::FramebufferFormatID RenderingDevice::framebuffer_format_create_empty(TextureSamples p_samples) { |
| 2907 | _THREAD_SAFE_METHOD_ |
| 2908 | |
| 2909 | FramebufferFormatKey key; |
| 2910 | key.passes.push_back(FramebufferPass()); |
| 2911 | |
| 2912 | const RBMap<FramebufferFormatKey, FramebufferFormatID>::Element *E = framebuffer_format_cache.find(key); |
| 2913 | if (E) { |
| 2914 | // Exists, return. |
| 2915 | return E->get(); |
| 2916 | } |
| 2917 | |
| 2918 | LocalVector<RDD::Subpass> subpass; |
| 2919 | subpass.resize(1); |
| 2920 | |
| 2921 | RDD::RenderPassID render_pass = driver->render_pass_create({}, subpass, {}, 1, RDD::AttachmentReference()); |
| 2922 | ERR_FAIL_COND_V(!render_pass, FramebufferFormatID()); |
| 2923 | |
| 2924 | FramebufferFormatID id = FramebufferFormatID(framebuffer_format_cache.size()) | (FramebufferFormatID(ID_TYPE_FRAMEBUFFER_FORMAT) << FramebufferFormatID(ID_BASE_SHIFT)); |
| 2925 | |
| 2926 | E = framebuffer_format_cache.insert(key, id); |
| 2927 | |
| 2928 | FramebufferFormat fb_format; |
| 2929 | fb_format.E = E; |
| 2930 | fb_format.render_pass = render_pass; |
| 2931 | fb_format.pass_samples.push_back(p_samples); |
| 2932 | framebuffer_formats[id] = fb_format; |
| 2933 | |
| 2934 | #if PRINT_FRAMEBUFFER_FORMAT |
| 2935 | print_line("FRAMEBUFFER FORMAT:", id, "ATTACHMENTS: EMPTY"); |
| 2936 | #endif |
| 2937 | |
| 2938 | return id; |
| 2939 | } |
| 2940 | |
| 2941 | RenderingDevice::TextureSamples RenderingDevice::framebuffer_format_get_texture_samples(FramebufferFormatID p_format, uint32_t p_pass) { |
| 2942 | _THREAD_SAFE_METHOD_ |
| 2943 | |
| 2944 | HashMap<FramebufferFormatID, FramebufferFormat>::Iterator E = framebuffer_formats.find(p_format); |
| 2945 | ERR_FAIL_COND_V(!E, TEXTURE_SAMPLES_1); |
| 2946 | ERR_FAIL_COND_V(p_pass >= uint32_t(E->value.pass_samples.size()), TEXTURE_SAMPLES_1); |
| 2947 | |
| 2948 | return E->value.pass_samples[p_pass]; |
| 2949 | } |
| 2950 | |
| 2951 | RID RenderingDevice::framebuffer_create_empty(const Size2i &p_size, TextureSamples p_samples, FramebufferFormatID p_format_check) { |
| 2952 | _THREAD_SAFE_METHOD_ |
| 2953 | |
| 2954 | Framebuffer framebuffer; |
| 2955 | framebuffer.format_id = framebuffer_format_create_empty(p_samples); |
| 2956 | ERR_FAIL_COND_V(p_format_check != INVALID_FORMAT_ID && framebuffer.format_id != p_format_check, RID()); |
| 2957 | framebuffer.size = p_size; |
| 2958 | framebuffer.view_count = 1; |
| 2959 | |
| 2960 | RDG::FramebufferCache *framebuffer_cache = RDG::framebuffer_cache_create(); |
| 2961 | framebuffer_cache->width = p_size.width; |
| 2962 | framebuffer_cache->height = p_size.height; |
| 2963 | framebuffer.framebuffer_cache = framebuffer_cache; |
| 2964 | |
| 2965 | RID id = framebuffer_owner.make_rid(framebuffer); |
| 2966 | #ifdef DEV_ENABLED |
| 2967 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 2968 | #endif |
| 2969 | |
| 2970 | // This relies on the fact that HashMap will not change the address of an object after it's been inserted into the container. |
| 2971 | framebuffer_cache->render_pass_creation_user_data = (void *)(&framebuffer_formats[framebuffer.format_id].E->key()); |
| 2972 | |
| 2973 | return id; |
| 2974 | } |
| 2975 | |
| 2976 | RID RenderingDevice::framebuffer_create(const Vector<RID> &p_texture_attachments, FramebufferFormatID p_format_check, uint32_t p_view_count) { |
| 2977 | _THREAD_SAFE_METHOD_ |
| 2978 | |
| 2979 | FramebufferPass pass; |
| 2980 | |
| 2981 | for (int i = 0; i < p_texture_attachments.size(); i++) { |
| 2982 | Texture *texture = texture_owner.get_or_null(p_texture_attachments[i]); |
| 2983 | |
| 2984 | ERR_FAIL_COND_V_MSG(texture && texture->layers != p_view_count, RID(), "Layers of our texture doesn't match view count for this framebuffer"); |
| 2985 | |
| 2986 | if (texture != nullptr) { |
| 2987 | _check_transfer_worker_texture(texture); |
| 2988 | } |
| 2989 | |
| 2990 | if (texture && texture->usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) { |
| 2991 | pass.depth_attachment = i; |
| 2992 | } else if (texture && texture->usage_flags & TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT) { |
| 2993 | pass.depth_resolve_attachment = i; |
| 2994 | } else if (texture && texture->usage_flags & TEXTURE_USAGE_VRS_ATTACHMENT_BIT) { |
| 2995 | // Prevent the VRS attachment from being added to the color_attachments. |
| 2996 | } else { |
| 2997 | if (texture && texture->is_resolve_buffer) { |
| 2998 | pass.resolve_attachments.push_back(i); |
| 2999 | } else { |
| 3000 | pass.color_attachments.push_back(texture ? i : ATTACHMENT_UNUSED); |
| 3001 | } |
| 3002 | } |
| 3003 | } |
| 3004 | |
| 3005 | Vector<FramebufferPass> passes; |
| 3006 | passes.push_back(pass); |
| 3007 | |
| 3008 | return framebuffer_create_multipass(p_texture_attachments, passes, p_format_check, p_view_count); |
| 3009 | } |
| 3010 | |
| 3011 | RID RenderingDevice::framebuffer_create_multipass(const Vector<RID> &p_texture_attachments, const Vector<FramebufferPass> &p_passes, FramebufferFormatID p_format_check, uint32_t p_view_count) { |
| 3012 | _THREAD_SAFE_METHOD_ |
| 3013 | |
| 3014 | Vector<AttachmentFormat> attachments; |
| 3015 | LocalVector<RDD::TextureID> textures; |
| 3016 | LocalVector<RDG::ResourceTracker *> trackers; |
| 3017 | int32_t vrs_attachment = -1; |
| 3018 | attachments.resize(p_texture_attachments.size()); |
| 3019 | Size2i size; |
| 3020 | bool size_set = false; |
| 3021 | for (int i = 0; i < p_texture_attachments.size(); i++) { |
| 3022 | AttachmentFormat af; |
| 3023 | Texture *texture = texture_owner.get_or_null(p_texture_attachments[i]); |
| 3024 | if (!texture) { |
| 3025 | af.usage_flags = AttachmentFormat::UNUSED_ATTACHMENT; |
| 3026 | trackers.push_back(nullptr); |
| 3027 | } else { |
| 3028 | ERR_FAIL_COND_V_MSG(texture->layers != p_view_count, RID(), "Layers of our texture doesn't match view count for this framebuffer"); |
| 3029 | |
| 3030 | _check_transfer_worker_texture(texture); |
| 3031 | |
| 3032 | if (i != 0 && texture->usage_flags & TEXTURE_USAGE_VRS_ATTACHMENT_BIT) { |
| 3033 | // Detect if the texture is the fragment density map and it's not the first attachment. |
| 3034 | vrs_attachment = i; |
| 3035 | } |
| 3036 | |
| 3037 | if (!size_set) { |
| 3038 | size.width = texture->width; |
| 3039 | size.height = texture->height; |
| 3040 | size_set = true; |
| 3041 | } else if (texture->usage_flags & TEXTURE_USAGE_VRS_ATTACHMENT_BIT) { |
| 3042 | // If this is not the first attachment we assume this is used as the VRS attachment. |
| 3043 | // In this case this texture will be 1/16th the size of the color attachment. |
| 3044 | // So we skip the size check. |
| 3045 | } else { |
| 3046 | ERR_FAIL_COND_V_MSG((uint32_t)size.width != texture->width || (uint32_t)size.height != texture->height, RID(), |
| 3047 | "All textures in a framebuffer should be the same size."); |
| 3048 | } |
| 3049 | |
| 3050 | af.format = texture->format; |
| 3051 | af.samples = texture->samples; |
| 3052 | af.usage_flags = texture->usage_flags; |
| 3053 | |
| 3054 | _texture_make_mutable(texture, p_texture_attachments[i]); |
| 3055 | |
| 3056 | textures.push_back(texture->driver_id); |
| 3057 | trackers.push_back(texture->draw_tracker); |
| 3058 | } |
| 3059 | attachments.write[i] = af; |
| 3060 | } |
| 3061 | |
| 3062 | ERR_FAIL_COND_V_MSG(!size_set, RID(), "All attachments unused."); |
| 3063 | |
| 3064 | FramebufferFormatID format_id = framebuffer_format_create_multipass(attachments, p_passes, p_view_count, vrs_attachment); |
| 3065 | if (format_id == INVALID_ID) { |
| 3066 | return RID(); |
| 3067 | } |
| 3068 | |
| 3069 | ERR_FAIL_COND_V_MSG(p_format_check != INVALID_ID && format_id != p_format_check, RID(), |
| 3070 | "The format used to check this framebuffer differs from the intended framebuffer format."); |
| 3071 | |
| 3072 | Framebuffer framebuffer; |
| 3073 | framebuffer.format_id = format_id; |
| 3074 | framebuffer.texture_ids = p_texture_attachments; |
| 3075 | framebuffer.size = size; |
| 3076 | framebuffer.view_count = p_view_count; |
| 3077 | |
| 3078 | RDG::FramebufferCache *framebuffer_cache = RDG::framebuffer_cache_create(); |
| 3079 | framebuffer_cache->width = size.width; |
| 3080 | framebuffer_cache->height = size.height; |
| 3081 | framebuffer_cache->textures = textures; |
| 3082 | framebuffer_cache->trackers = trackers; |
| 3083 | framebuffer.framebuffer_cache = framebuffer_cache; |
| 3084 | |
| 3085 | RID id = framebuffer_owner.make_rid(framebuffer); |
| 3086 | #ifdef DEV_ENABLED |
| 3087 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 3088 | #endif |
| 3089 | |
| 3090 | for (int i = 0; i < p_texture_attachments.size(); i++) { |
| 3091 | if (p_texture_attachments[i].is_valid()) { |
| 3092 | _add_dependency(id, p_texture_attachments[i]); |
| 3093 | } |
| 3094 | } |
| 3095 | |
| 3096 | // This relies on the fact that HashMap will not change the address of an object after it's been inserted into the container. |
| 3097 | framebuffer_cache->render_pass_creation_user_data = (void *)(&framebuffer_formats[framebuffer.format_id].E->key()); |
| 3098 | |
| 3099 | return id; |
| 3100 | } |
| 3101 | |
| 3102 | RenderingDevice::FramebufferFormatID RenderingDevice::framebuffer_get_format(RID p_framebuffer) { |
| 3103 | _THREAD_SAFE_METHOD_ |
| 3104 | |
| 3105 | Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_framebuffer); |
| 3106 | ERR_FAIL_NULL_V(framebuffer, INVALID_ID); |
| 3107 | |
| 3108 | return framebuffer->format_id; |
| 3109 | } |
| 3110 | |
| 3111 | Size2 RenderingDevice::framebuffer_get_size(RID p_framebuffer) { |
| 3112 | _THREAD_SAFE_METHOD_ |
| 3113 | |
| 3114 | Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_framebuffer); |
| 3115 | ERR_FAIL_NULL_V(framebuffer, Size2(0, 0)); |
| 3116 | |
| 3117 | return framebuffer->size; |
| 3118 | } |
| 3119 | |
| 3120 | bool RenderingDevice::framebuffer_is_valid(RID p_framebuffer) const { |
| 3121 | _THREAD_SAFE_METHOD_ |
| 3122 | |
| 3123 | return framebuffer_owner.owns(p_framebuffer); |
| 3124 | } |
| 3125 | |
| 3126 | void RenderingDevice::framebuffer_set_invalidation_callback(RID p_framebuffer, InvalidationCallback p_callback, void *p_userdata) { |
| 3127 | _THREAD_SAFE_METHOD_ |
| 3128 | |
| 3129 | Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_framebuffer); |
| 3130 | ERR_FAIL_NULL(framebuffer); |
| 3131 | |
| 3132 | framebuffer->invalidated_callback = p_callback; |
| 3133 | framebuffer->invalidated_callback_userdata = p_userdata; |
| 3134 | } |
| 3135 | |
| 3136 | /*****************/ |
| 3137 | /**** SAMPLER ****/ |
| 3138 | /*****************/ |
| 3139 | |
| 3140 | RID RenderingDevice::sampler_create(const SamplerState &p_state) { |
| 3141 | _THREAD_SAFE_METHOD_ |
| 3142 | |
| 3143 | ERR_FAIL_INDEX_V(p_state.repeat_u, SAMPLER_REPEAT_MODE_MAX, RID()); |
| 3144 | ERR_FAIL_INDEX_V(p_state.repeat_v, SAMPLER_REPEAT_MODE_MAX, RID()); |
| 3145 | ERR_FAIL_INDEX_V(p_state.repeat_w, SAMPLER_REPEAT_MODE_MAX, RID()); |
| 3146 | ERR_FAIL_INDEX_V(p_state.compare_op, COMPARE_OP_MAX, RID()); |
| 3147 | ERR_FAIL_INDEX_V(p_state.border_color, SAMPLER_BORDER_COLOR_MAX, RID()); |
| 3148 | |
| 3149 | RDD::SamplerID sampler = driver->sampler_create(p_state); |
| 3150 | ERR_FAIL_COND_V(!sampler, RID()); |
| 3151 | |
| 3152 | RID id = sampler_owner.make_rid(sampler); |
| 3153 | #ifdef DEV_ENABLED |
| 3154 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 3155 | #endif |
| 3156 | return id; |
| 3157 | } |
| 3158 | |
| 3159 | bool RenderingDevice::sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_sampler_filter) const { |
| 3160 | _THREAD_SAFE_METHOD_ |
| 3161 | |
| 3162 | ERR_FAIL_INDEX_V(p_format, DATA_FORMAT_MAX, false); |
| 3163 | |
| 3164 | return driver->sampler_is_format_supported_for_filter(p_format, p_sampler_filter); |
| 3165 | } |
| 3166 | |
| 3167 | /***********************/ |
| 3168 | /**** VERTEX BUFFER ****/ |
| 3169 | /***********************/ |
| 3170 | |
| 3171 | RID RenderingDevice::vertex_buffer_create(uint32_t p_size_bytes, Span<uint8_t> p_data, BitField<BufferCreationBits> p_creation_bits) { |
| 3172 | ERR_FAIL_COND_V(p_data.size() && (uint32_t)p_data.size() != p_size_bytes, RID()); |
| 3173 | |
| 3174 | Buffer buffer; |
| 3175 | buffer.size = p_size_bytes; |
| 3176 | buffer.usage = RDD::BUFFER_USAGE_TRANSFER_FROM_BIT | RDD::BUFFER_USAGE_TRANSFER_TO_BIT | RDD::BUFFER_USAGE_VERTEX_BIT; |
| 3177 | if (p_creation_bits.has_flag(BUFFER_CREATION_AS_STORAGE_BIT)) { |
| 3178 | buffer.usage.set_flag(RDD::BUFFER_USAGE_STORAGE_BIT); |
| 3179 | } |
| 3180 | if (p_creation_bits.has_flag(BUFFER_CREATION_DYNAMIC_PERSISTENT_BIT)) { |
| 3181 | buffer.usage.set_flag(RDD::BUFFER_USAGE_DYNAMIC_PERSISTENT_BIT); |
| 3182 | |
| 3183 | // Persistent buffers expect frequent CPU -> GPU writes, so GPU writes should avoid the same path. |
| 3184 | buffer.usage.clear_flag(RDD::BUFFER_USAGE_TRANSFER_TO_BIT); |
| 3185 | } |
| 3186 | if (p_creation_bits.has_flag(BUFFER_CREATION_DEVICE_ADDRESS_BIT)) { |
| 3187 | buffer.usage.set_flag(RDD::BUFFER_USAGE_DEVICE_ADDRESS_BIT); |
| 3188 | } |
| 3189 | buffer.driver_id = driver->buffer_create(buffer.size, buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU, frames_drawn); |
| 3190 | ERR_FAIL_COND_V(!buffer.driver_id, RID()); |
| 3191 | |
| 3192 | // Vertex buffers are assumed to be immutable unless they don't have initial data or they've been marked for storage explicitly. |
| 3193 | if (p_data.is_empty() || p_creation_bits.has_flag(BUFFER_CREATION_AS_STORAGE_BIT) || p_creation_bits.has_flag(BUFFER_CREATION_DYNAMIC_PERSISTENT_BIT)) { |
| 3194 | buffer.draw_tracker = RDG::resource_tracker_create(); |
| 3195 | buffer.draw_tracker->buffer_driver_id = buffer.driver_id; |
| 3196 | } |
| 3197 | |
| 3198 | if (p_data.size()) { |
| 3199 | _buffer_initialize(&buffer, p_data); |
| 3200 | } |
| 3201 | |
| 3202 | _THREAD_SAFE_LOCK_ |
| 3203 | buffer_memory += buffer.size; |
| 3204 | _THREAD_SAFE_UNLOCK_ |
| 3205 | |
| 3206 | RID id = vertex_buffer_owner.make_rid(buffer); |
| 3207 | #ifdef DEV_ENABLED |
| 3208 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 3209 | #endif |
| 3210 | return id; |
| 3211 | } |
| 3212 | |
| 3213 | // Internally reference counted, this ID is warranted to be unique for the same description, but needs to be freed as many times as it was allocated. |
| 3214 | RenderingDevice::VertexFormatID RenderingDevice::vertex_format_create(const Vector<VertexAttribute> &p_vertex_descriptions) { |
| 3215 | _THREAD_SAFE_METHOD_ |
| 3216 | |
| 3217 | VertexDescriptionKey key; |
| 3218 | key.vertex_formats = p_vertex_descriptions; |
| 3219 | |
| 3220 | VertexFormatID *idptr = vertex_format_cache.getptr(key); |
| 3221 | if (idptr) { |
| 3222 | return *idptr; |
| 3223 | } |
| 3224 | |
| 3225 | VertexAttributeBindingsMap bindings; |
| 3226 | bool has_implicit = false; |
| 3227 | bool has_explicit = false; |
| 3228 | Vector<VertexAttribute> vertex_descriptions = p_vertex_descriptions; |
| 3229 | HashSet<int> used_locations; |
| 3230 | for (int i = 0; i < vertex_descriptions.size(); i++) { |
| 3231 | VertexAttribute &attr = vertex_descriptions.write[i]; |
| 3232 | ERR_CONTINUE(attr.format >= DATA_FORMAT_MAX); |
| 3233 | ERR_FAIL_COND_V(used_locations.has(attr.location), INVALID_ID); |
| 3234 | |
| 3235 | ERR_FAIL_COND_V_MSG(get_format_vertex_size(attr.format) == 0, INVALID_ID, |
| 3236 | vformat("Data format for attribute (%d), '%s', is not valid for a vertex array.", attr.location, String(FORMAT_NAMES[attr.format]))); |
| 3237 | |
| 3238 | if (attr.binding == UINT32_MAX) { |
| 3239 | attr.binding = i; // Implicitly assigned binding |
| 3240 | has_implicit = true; |
| 3241 | } else { |
| 3242 | has_explicit = true; |
| 3243 | } |
| 3244 | ERR_FAIL_COND_V_MSG(!(has_implicit ^ has_explicit), INVALID_ID, "Vertex attributes must use either all explicit or all implicit bindings."); |
| 3245 | |
| 3246 | const VertexAttributeBinding *existing = bindings.getptr(attr.binding); |
| 3247 | if (!existing) { |
| 3248 | bindings.insert(attr.binding, VertexAttributeBinding(attr.stride, attr.frequency)); |
| 3249 | } else { |
| 3250 | ERR_FAIL_COND_V_MSG(existing->stride != attr.stride, INVALID_ID, |
| 3251 | vformat("Vertex attributes with binding (%d) have an inconsistent stride.", attr.binding)); |
| 3252 | ERR_FAIL_COND_V_MSG(existing->frequency != attr.frequency, INVALID_ID, |
| 3253 | vformat("Vertex attributes with binding (%d) have an inconsistent frequency.", attr.binding)); |
| 3254 | } |
| 3255 | |
| 3256 | used_locations.insert(attr.location); |
| 3257 | } |
| 3258 | |
| 3259 | RDD::VertexFormatID driver_id = driver->vertex_format_create(vertex_descriptions, bindings); |
| 3260 | ERR_FAIL_COND_V(!driver_id, 0); |
| 3261 | |
| 3262 | VertexFormatID id = (vertex_format_cache.size() | ((int64_t)ID_TYPE_VERTEX_FORMAT << ID_BASE_SHIFT)); |
| 3263 | vertex_format_cache[key] = id; |
| 3264 | VertexDescriptionCache &ce = vertex_formats.insert(id, VertexDescriptionCache())->value; |
| 3265 | ce.vertex_formats = vertex_descriptions; |
| 3266 | ce.bindings = std::move(bindings); |
| 3267 | ce.driver_id = driver_id; |
| 3268 | return id; |
| 3269 | } |
| 3270 | |
| 3271 | RID RenderingDevice::vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const Vector<RID> &p_src_buffers, const Vector<uint64_t> &p_offsets) { |
| 3272 | _THREAD_SAFE_METHOD_ |
| 3273 | |
| 3274 | ERR_FAIL_COND_V(!vertex_formats.has(p_vertex_format), RID()); |
| 3275 | const VertexDescriptionCache &vd = vertex_formats[p_vertex_format]; |
| 3276 | |
| 3277 | VertexArray vertex_array; |
| 3278 | |
| 3279 | if (p_offsets.is_empty()) { |
| 3280 | vertex_array.offsets.resize_initialized(p_src_buffers.size()); |
| 3281 | } else { |
| 3282 | ERR_FAIL_COND_V(p_offsets.size() != p_src_buffers.size(), RID()); |
| 3283 | vertex_array.offsets = p_offsets; |
| 3284 | } |
| 3285 | |
| 3286 | vertex_array.vertex_count = p_vertex_count; |
| 3287 | vertex_array.description = p_vertex_format; |
| 3288 | vertex_array.max_instances_allowed = 0xFFFFFFFF; // By default as many as you want. |
| 3289 | vertex_array.buffers.resize(p_src_buffers.size()); |
| 3290 | |
| 3291 | HashSet<RID> unique_buffers; |
| 3292 | unique_buffers.reserve(p_src_buffers.size()); |
| 3293 | |
| 3294 | for (const VertexAttribute &atf : vd.vertex_formats) { |
| 3295 | ERR_FAIL_COND_V_MSG(atf.binding >= p_src_buffers.size(), RID(), vformat("Vertex attribute location (%d) is missing a buffer for binding (%d).", atf.location, atf.binding)); |
| 3296 | RID buf = p_src_buffers[atf.binding]; |
| 3297 | ERR_FAIL_COND_V(!vertex_buffer_owner.owns(buf), RID()); |
| 3298 | |
| 3299 | Buffer *buffer = vertex_buffer_owner.get_or_null(buf); |
| 3300 | |
| 3301 | // Validate with buffer. |
| 3302 | { |
| 3303 | uint32_t element_size = get_format_vertex_size(atf.format); |
| 3304 | ERR_FAIL_COND_V(element_size == 0, RID()); // Should never happen since this was prevalidated. |
| 3305 | |
| 3306 | if (atf.frequency == VERTEX_FREQUENCY_VERTEX) { |
| 3307 | // Validate size for regular drawing. |
| 3308 | uint64_t total_size = uint64_t(atf.stride) * (p_vertex_count - 1) + atf.offset + element_size; |
| 3309 | ERR_FAIL_COND_V_MSG(total_size > buffer->size, RID(), |
| 3310 | vformat("Vertex attribute (%d) will read past the end of the buffer.", atf.location)); |
| 3311 | |
| 3312 | } else { |
| 3313 | // Validate size for instances drawing. |
| 3314 | uint64_t available = buffer->size - atf.offset; |
| 3315 | ERR_FAIL_COND_V_MSG(available < element_size, RID(), |
| 3316 | vformat("Vertex attribute (%d) uses instancing, but it's just too small.", atf.location)); |
| 3317 | |
| 3318 | uint32_t instances_allowed = available / atf.stride; |
| 3319 | vertex_array.max_instances_allowed = MIN(instances_allowed, vertex_array.max_instances_allowed); |
| 3320 | } |
| 3321 | } |
| 3322 | |
| 3323 | vertex_array.buffers.write[atf.binding] = buffer->driver_id; |
| 3324 | |
| 3325 | if (unique_buffers.has(buf)) { |
| 3326 | // No need to add dependencies multiple times. |
| 3327 | continue; |
| 3328 | } |
| 3329 | |
| 3330 | unique_buffers.insert(buf); |
| 3331 | |
| 3332 | if (buffer->draw_tracker != nullptr) { |
| 3333 | vertex_array.draw_trackers.push_back(buffer->draw_tracker); |
| 3334 | } else { |
| 3335 | vertex_array.untracked_buffers.insert(buf); |
| 3336 | } |
| 3337 | |
| 3338 | if (buffer->transfer_worker_index >= 0) { |
| 3339 | vertex_array.transfer_worker_indices.push_back(buffer->transfer_worker_index); |
| 3340 | vertex_array.transfer_worker_operations.push_back(buffer->transfer_worker_operation); |
| 3341 | } |
| 3342 | } |
| 3343 | |
| 3344 | RID id = vertex_array_owner.make_rid(vertex_array); |
| 3345 | for (const RID &buf : unique_buffers) { |
| 3346 | _add_dependency(id, buf); |
| 3347 | } |
| 3348 | |
| 3349 | return id; |
| 3350 | } |
| 3351 | |
| 3352 | RID RenderingDevice::index_buffer_create(uint32_t p_index_count, IndexBufferFormat p_format, Span<uint8_t> p_data, bool p_use_restart_indices, BitField<BufferCreationBits> p_creation_bits) { |
| 3353 | ERR_FAIL_COND_V(p_index_count == 0, RID()); |
| 3354 | |
| 3355 | IndexBuffer index_buffer; |
| 3356 | index_buffer.format = p_format; |
| 3357 | index_buffer.supports_restart_indices = p_use_restart_indices; |
| 3358 | index_buffer.index_count = p_index_count; |
| 3359 | uint32_t size_bytes = p_index_count * ((p_format == INDEX_BUFFER_FORMAT_UINT16) ? 2 : 4); |
| 3360 | #ifdef DEBUG_ENABLED |
| 3361 | if (p_data.size()) { |
| 3362 | index_buffer.max_index = 0; |
| 3363 | ERR_FAIL_COND_V_MSG((uint32_t)p_data.size() != size_bytes, RID(), |
| 3364 | "Default index buffer initializer array size (" + itos(p_data.size()) + ") does not match format required size (" + itos(size_bytes) + ")."); |
| 3365 | const uint8_t *r = p_data.ptr(); |
| 3366 | if (p_format == INDEX_BUFFER_FORMAT_UINT16) { |
| 3367 | const uint16_t *index16 = (const uint16_t *)r; |
| 3368 | for (uint32_t i = 0; i < p_index_count; i++) { |
| 3369 | if (p_use_restart_indices && index16[i] == 0xFFFF) { |
| 3370 | continue; // Restart index, ignore. |
| 3371 | } |
| 3372 | index_buffer.max_index = MAX(index16[i], index_buffer.max_index); |
| 3373 | } |
| 3374 | } else { |
| 3375 | const uint32_t *index32 = (const uint32_t *)r; |
| 3376 | for (uint32_t i = 0; i < p_index_count; i++) { |
| 3377 | if (p_use_restart_indices && index32[i] == 0xFFFFFFFF) { |
| 3378 | continue; // Restart index, ignore. |
| 3379 | } |
| 3380 | index_buffer.max_index = MAX(index32[i], index_buffer.max_index); |
| 3381 | } |
| 3382 | } |
| 3383 | } else { |
| 3384 | index_buffer.max_index = 0xFFFFFFFF; |
| 3385 | } |
| 3386 | #else |
| 3387 | index_buffer.max_index = 0xFFFFFFFF; |
| 3388 | #endif |
| 3389 | index_buffer.size = size_bytes; |
| 3390 | index_buffer.usage = (RDD::BUFFER_USAGE_TRANSFER_FROM_BIT | RDD::BUFFER_USAGE_TRANSFER_TO_BIT | RDD::BUFFER_USAGE_INDEX_BIT); |
| 3391 | if (p_creation_bits.has_flag(BUFFER_CREATION_DEVICE_ADDRESS_BIT)) { |
| 3392 | index_buffer.usage.set_flag(RDD::BUFFER_USAGE_DEVICE_ADDRESS_BIT); |
| 3393 | } |
| 3394 | index_buffer.driver_id = driver->buffer_create(index_buffer.size, index_buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU, frames_drawn); |
| 3395 | ERR_FAIL_COND_V(!index_buffer.driver_id, RID()); |
| 3396 | |
| 3397 | // Index buffers are assumed to be immutable unless they don't have initial data. |
| 3398 | if (p_data.is_empty()) { |
| 3399 | index_buffer.draw_tracker = RDG::resource_tracker_create(); |
| 3400 | index_buffer.draw_tracker->buffer_driver_id = index_buffer.driver_id; |
| 3401 | } |
| 3402 | |
| 3403 | if (p_data.size()) { |
| 3404 | _buffer_initialize(&index_buffer, p_data); |
| 3405 | } |
| 3406 | |
| 3407 | _THREAD_SAFE_LOCK_ |
| 3408 | buffer_memory += index_buffer.size; |
| 3409 | _THREAD_SAFE_UNLOCK_ |
| 3410 | |
| 3411 | RID id = index_buffer_owner.make_rid(index_buffer); |
| 3412 | #ifdef DEV_ENABLED |
| 3413 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 3414 | #endif |
| 3415 | return id; |
| 3416 | } |
| 3417 | |
| 3418 | RID RenderingDevice::index_array_create(RID p_index_buffer, uint32_t p_index_offset, uint32_t p_index_count) { |
| 3419 | _THREAD_SAFE_METHOD_ |
| 3420 | |
| 3421 | ERR_FAIL_COND_V(!index_buffer_owner.owns(p_index_buffer), RID()); |
| 3422 | |
| 3423 | IndexBuffer *index_buffer = index_buffer_owner.get_or_null(p_index_buffer); |
| 3424 | |
| 3425 | ERR_FAIL_COND_V(p_index_count == 0, RID()); |
| 3426 | ERR_FAIL_COND_V(p_index_offset + p_index_count > index_buffer->index_count, RID()); |
| 3427 | |
| 3428 | IndexArray index_array; |
| 3429 | index_array.max_index = index_buffer->max_index; |
| 3430 | index_array.driver_id = index_buffer->driver_id; |
| 3431 | index_array.draw_tracker = index_buffer->draw_tracker; |
| 3432 | index_array.offset = p_index_offset; |
| 3433 | index_array.indices = p_index_count; |
| 3434 | index_array.format = index_buffer->format; |
| 3435 | index_array.supports_restart_indices = index_buffer->supports_restart_indices; |
| 3436 | index_array.transfer_worker_index = index_buffer->transfer_worker_index; |
| 3437 | index_array.transfer_worker_operation = index_buffer->transfer_worker_operation; |
| 3438 | |
| 3439 | RID id = index_array_owner.make_rid(index_array); |
| 3440 | _add_dependency(id, p_index_buffer); |
| 3441 | return id; |
| 3442 | } |
| 3443 | |
| 3444 | /****************/ |
| 3445 | /**** SHADER ****/ |
| 3446 | /****************/ |
| 3447 | |
| 3448 | // Keep the values in sync with the `UniformType` enum (file rendering_device_commons.h). |
| 3449 | static const char *SHADER_UNIFORM_NAMES[RenderingDevice::UNIFORM_TYPE_MAX] = { |
| 3450 | "Sampler", |
| 3451 | "CombinedSampler", // UNIFORM_TYPE_SAMPLER_WITH_TEXTURE |
| 3452 | "Texture", |
| 3453 | "Image", |
| 3454 | "TextureBuffer", |
| 3455 | "SamplerTextureBuffer", |
| 3456 | "ImageBuffer", |
| 3457 | "UniformBuffer", |
| 3458 | "StorageBuffer", |
| 3459 | "InputAttachment", |
| 3460 | "UniformBufferDynamic", |
| 3461 | "StorageBufferDynamic", |
| 3462 | }; |
| 3463 | |
| 3464 | String RenderingDevice::_shader_uniform_debug(RID p_shader, int p_set) { |
| 3465 | String ret; |
| 3466 | const Shader *shader = shader_owner.get_or_null(p_shader); |
| 3467 | ERR_FAIL_NULL_V(shader, String()); |
| 3468 | for (int i = 0; i < shader->uniform_sets.size(); i++) { |
| 3469 | if (p_set >= 0 && i != p_set) { |
| 3470 | continue; |
| 3471 | } |
| 3472 | for (int j = 0; j < shader->uniform_sets[i].size(); j++) { |
| 3473 | const ShaderUniform &ui = shader->uniform_sets[i][j]; |
| 3474 | if (!ret.is_empty()) { |
| 3475 | ret += "\n"; |
| 3476 | } |
| 3477 | ret += "Set: " + itos(i) + " Binding: " + itos(ui.binding) + " Type: " + SHADER_UNIFORM_NAMES[ui.type] + " Writable: " + (ui.writable ? "Y" : "N") + " Length: " + itos(ui.length); |
| 3478 | } |
| 3479 | } |
| 3480 | return ret; |
| 3481 | } |
| 3482 | |
| 3483 | Vector<uint8_t> RenderingDevice::shader_compile_binary_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv, const String &p_shader_name) { |
| 3484 | const RenderingShaderContainerFormat &container_format = driver->get_shader_container_format(); |
| 3485 | Ref<RenderingShaderContainer> shader_container = container_format.create_container(); |
| 3486 | ERR_FAIL_COND_V(shader_container.is_null(), Vector<uint8_t>()); |
| 3487 | |
| 3488 | // Compile shader binary from SPIR-V. |
| 3489 | bool code_compiled = shader_container->set_code_from_spirv(p_shader_name, p_spirv); |
| 3490 | ERR_FAIL_COND_V_MSG(!code_compiled, Vector<uint8_t>(), vformat("Failed to compile code to native for SPIR-V.")); |
| 3491 | |
| 3492 | return shader_container->to_bytes(); |
| 3493 | } |
| 3494 | |
| 3495 | RID RenderingDevice::shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary, RID p_placeholder) { |
| 3496 | // Immutable samplers : |
| 3497 | // Expanding api when creating shader to allow passing optionally a set of immutable samplers |
| 3498 | // keeping existing api but extending it by sending an empty set. |
| 3499 | Vector<PipelineImmutableSampler> immutable_samplers; |
| 3500 | return shader_create_from_bytecode_with_samplers(p_shader_binary, p_placeholder, immutable_samplers); |
| 3501 | } |
| 3502 | |
| 3503 | RID RenderingDevice::shader_create_from_bytecode_with_samplers(const Vector<uint8_t> &p_shader_binary, RID p_placeholder, const Vector<PipelineImmutableSampler> &p_immutable_samplers) { |
| 3504 | _THREAD_SAFE_METHOD_ |
| 3505 | |
| 3506 | Ref<RenderingShaderContainer> shader_container = driver->get_shader_container_format().create_container(); |
| 3507 | ERR_FAIL_COND_V(shader_container.is_null(), RID()); |
| 3508 | |
| 3509 | bool parsed_container = shader_container->from_bytes(p_shader_binary); |
| 3510 | ERR_FAIL_COND_V_MSG(!parsed_container, RID(), "Failed to parse shader container from binary."); |
| 3511 | |
| 3512 | Vector<RDD::ImmutableSampler> driver_immutable_samplers; |
| 3513 | for (const PipelineImmutableSampler &source_sampler : p_immutable_samplers) { |
| 3514 | RDD::ImmutableSampler driver_sampler; |
| 3515 | driver_sampler.type = source_sampler.uniform_type; |
| 3516 | driver_sampler.binding = source_sampler.binding; |
| 3517 | |
| 3518 | for (uint32_t j = 0; j < source_sampler.get_id_count(); j++) { |
| 3519 | RDD::SamplerID *sampler_driver_id = sampler_owner.get_or_null(source_sampler.get_id(j)); |
| 3520 | driver_sampler.ids.push_back(*sampler_driver_id); |
| 3521 | } |
| 3522 | |
| 3523 | driver_immutable_samplers.append(driver_sampler); |
| 3524 | } |
| 3525 | |
| 3526 | RDD::ShaderID shader_id = driver->shader_create_from_container(shader_container, driver_immutable_samplers); |
| 3527 | ERR_FAIL_COND_V(!shader_id, RID()); |
| 3528 | |
| 3529 | // All good, let's create modules. |
| 3530 | |
| 3531 | RID id; |
| 3532 | if (p_placeholder.is_null()) { |
| 3533 | id = shader_owner.make_rid(); |
| 3534 | } else { |
| 3535 | id = p_placeholder; |
| 3536 | } |
| 3537 | |
| 3538 | Shader *shader = shader_owner.get_or_null(id); |
| 3539 | ERR_FAIL_NULL_V(shader, RID()); |
| 3540 | |
| 3541 | *((ShaderReflection *)shader) = shader_container->get_shader_reflection(); |
| 3542 | shader->name.clear(); |
| 3543 | shader->name.append_utf8(shader_container->shader_name); |
| 3544 | shader->driver_id = shader_id; |
| 3545 | shader->layout_hash = driver->shader_get_layout_hash(shader_id); |
| 3546 | |
| 3547 | for (int i = 0; i < shader->uniform_sets.size(); i++) { |
| 3548 | uint32_t format = 0; // No format, default. |
| 3549 | |
| 3550 | if (shader->uniform_sets[i].size()) { |
| 3551 | // Sort and hash. |
| 3552 | |
| 3553 | shader->uniform_sets.write[i].sort(); |
| 3554 | |
| 3555 | UniformSetFormat usformat; |
| 3556 | usformat.uniforms = shader->uniform_sets[i]; |
| 3557 | RBMap<UniformSetFormat, uint32_t>::Element *E = uniform_set_format_cache.find(usformat); |
| 3558 | if (E) { |
| 3559 | format = E->get(); |
| 3560 | } else { |
| 3561 | format = uniform_set_format_cache.size() + 1; |
| 3562 | uniform_set_format_cache.insert(usformat, format); |
| 3563 | } |
| 3564 | } |
| 3565 | |
| 3566 | shader->set_formats.push_back(format); |
| 3567 | } |
| 3568 | |
| 3569 | for (ShaderStage stage : shader->stages_vector) { |
| 3570 | switch (stage) { |
| 3571 | case SHADER_STAGE_VERTEX: |
| 3572 | shader->stage_bits.set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT); |
| 3573 | break; |
| 3574 | case SHADER_STAGE_FRAGMENT: |
| 3575 | shader->stage_bits.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT); |
| 3576 | break; |
| 3577 | case SHADER_STAGE_TESSELATION_CONTROL: |
| 3578 | shader->stage_bits.set_flag(RDD::PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT); |
| 3579 | break; |
| 3580 | case SHADER_STAGE_TESSELATION_EVALUATION: |
| 3581 | shader->stage_bits.set_flag(RDD::PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT); |
| 3582 | break; |
| 3583 | case SHADER_STAGE_COMPUTE: |
| 3584 | shader->stage_bits.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT); |
| 3585 | break; |
| 3586 | default: |
| 3587 | DEV_ASSERT(false && "Unknown shader stage."); |
| 3588 | break; |
| 3589 | } |
| 3590 | } |
| 3591 | |
| 3592 | #ifdef DEV_ENABLED |
| 3593 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 3594 | #endif |
| 3595 | return id; |
| 3596 | } |
| 3597 | |
| 3598 | void RenderingDevice::shader_destroy_modules(RID p_shader) { |
| 3599 | Shader *shader = shader_owner.get_or_null(p_shader); |
| 3600 | ERR_FAIL_NULL(shader); |
| 3601 | driver->shader_destroy_modules(shader->driver_id); |
| 3602 | } |
| 3603 | |
| 3604 | RID RenderingDevice::shader_create_placeholder() { |
| 3605 | _THREAD_SAFE_METHOD_ |
| 3606 | |
| 3607 | Shader shader; |
| 3608 | return shader_owner.make_rid(shader); |
| 3609 | } |
| 3610 | |
| 3611 | uint64_t RenderingDevice::shader_get_vertex_input_attribute_mask(RID p_shader) { |
| 3612 | _THREAD_SAFE_METHOD_ |
| 3613 | |
| 3614 | const Shader *shader = shader_owner.get_or_null(p_shader); |
| 3615 | ERR_FAIL_NULL_V(shader, 0); |
| 3616 | return shader->vertex_input_mask; |
| 3617 | } |
| 3618 | |
| 3619 | /******************/ |
| 3620 | /**** UNIFORMS ****/ |
| 3621 | /******************/ |
| 3622 | |
| 3623 | RID RenderingDevice::uniform_buffer_create(uint32_t p_size_bytes, Span<uint8_t> p_data, BitField<BufferCreationBits> p_creation_bits) { |
| 3624 | ERR_FAIL_COND_V(p_data.size() && (uint32_t)p_data.size() != p_size_bytes, RID()); |
| 3625 | |
| 3626 | Buffer buffer; |
| 3627 | buffer.size = p_size_bytes; |
| 3628 | buffer.usage = (RDD::BUFFER_USAGE_TRANSFER_TO_BIT | RDD::BUFFER_USAGE_UNIFORM_BIT); |
| 3629 | if (p_creation_bits.has_flag(BUFFER_CREATION_DEVICE_ADDRESS_BIT)) { |
| 3630 | buffer.usage.set_flag(RDD::BUFFER_USAGE_DEVICE_ADDRESS_BIT); |
| 3631 | } |
| 3632 | if (p_creation_bits.has_flag(BUFFER_CREATION_DYNAMIC_PERSISTENT_BIT)) { |
| 3633 | buffer.usage.set_flag(RDD::BUFFER_USAGE_DYNAMIC_PERSISTENT_BIT); |
| 3634 | |
| 3635 | // This is a precaution: Persistent buffers are meant for frequent CPU -> GPU transfers. |
| 3636 | // Writing to this buffer from GPU might cause sync issues if both CPU & GPU try to write at the |
| 3637 | // same time. It's probably fine (since CPU always advances the pointer before writing) but let's |
| 3638 | // stick to the known/intended use cases and scream if we deviate from it. |
| 3639 | buffer.usage.clear_flag(RDD::BUFFER_USAGE_TRANSFER_TO_BIT); |
| 3640 | } |
| 3641 | buffer.driver_id = driver->buffer_create(buffer.size, buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU, frames_drawn); |
| 3642 | ERR_FAIL_COND_V(!buffer.driver_id, RID()); |
| 3643 | |
| 3644 | // Uniform buffers are assumed to be immutable unless they don't have initial data. |
| 3645 | if (p_data.is_empty()) { |
| 3646 | buffer.draw_tracker = RDG::resource_tracker_create(); |
| 3647 | buffer.draw_tracker->buffer_driver_id = buffer.driver_id; |
| 3648 | } |
| 3649 | |
| 3650 | if (p_data.size()) { |
| 3651 | _buffer_initialize(&buffer, p_data); |
| 3652 | } |
| 3653 | |
| 3654 | _THREAD_SAFE_LOCK_ |
| 3655 | buffer_memory += buffer.size; |
| 3656 | _THREAD_SAFE_UNLOCK_ |
| 3657 | |
| 3658 | RID id = uniform_buffer_owner.make_rid(buffer); |
| 3659 | #ifdef DEV_ENABLED |
| 3660 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 3661 | #endif |
| 3662 | return id; |
| 3663 | } |
| 3664 | |
| 3665 | void RenderingDevice::_uniform_set_update_shared(UniformSet *p_uniform_set) { |
| 3666 | for (UniformSet::SharedTexture shared : p_uniform_set->shared_textures_to_update) { |
| 3667 | Texture *texture = texture_owner.get_or_null(shared.texture); |
| 3668 | ERR_CONTINUE(texture == nullptr); |
| 3669 | _texture_update_shared_fallback(shared.texture, texture, shared.writing); |
| 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | void RenderingDevice::_uniform_set_update_clears(UniformSet *p_uniform_set) { |
| 3674 | if (p_uniform_set->pending_clear_textures.is_empty()) { |
| 3675 | return; |
| 3676 | } |
| 3677 | |
| 3678 | for (RID texture_id : p_uniform_set->pending_clear_textures) { |
| 3679 | Texture *texture = texture_owner.get_or_null(texture_id); |
| 3680 | if (texture != nullptr) { |
| 3681 | _texture_check_pending_clear(texture_id, texture); |
| 3682 | } |
| 3683 | } |
| 3684 | |
| 3685 | p_uniform_set->pending_clear_textures.clear(); |
| 3686 | } |
| 3687 | |
| 3688 | RID RenderingDevice::uniform_set_create(const VectorView<RD::Uniform> &p_uniforms, RID p_shader, uint32_t p_shader_set, bool p_linear_pool) { |
| 3689 | _THREAD_SAFE_METHOD_ |
| 3690 | |
| 3691 | ERR_FAIL_COND_V(p_uniforms.size() == 0, RID()); |
| 3692 | |
| 3693 | Shader *shader = shader_owner.get_or_null(p_shader); |
| 3694 | ERR_FAIL_NULL_V(shader, RID()); |
| 3695 | |
| 3696 | ERR_FAIL_COND_V_MSG(p_shader_set >= (uint32_t)shader->uniform_sets.size() || shader->uniform_sets[p_shader_set].is_empty(), RID(), |
| 3697 | "Desired set (" + itos(p_shader_set) + ") not used by shader."); |
| 3698 | // See that all sets in shader are satisfied. |
| 3699 | |
| 3700 | const Vector<ShaderUniform> &set = shader->uniform_sets[p_shader_set]; |
| 3701 | |
| 3702 | uint32_t uniform_count = p_uniforms.size(); |
| 3703 | const Uniform *uniforms = p_uniforms.ptr(); |
| 3704 | |
| 3705 | uint32_t set_uniform_count = set.size(); |
| 3706 | const ShaderUniform *set_uniforms = set.ptr(); |
| 3707 | |
| 3708 | LocalVector<RDD::BoundUniform> driver_uniforms; |
| 3709 | driver_uniforms.resize(set_uniform_count); |
| 3710 | |
| 3711 | // Used for verification to make sure a uniform set does not use a framebuffer bound texture. |
| 3712 | LocalVector<UniformSet::AttachableTexture> attachable_textures; |
| 3713 | Vector<RDG::ResourceTracker *> draw_trackers; |
| 3714 | Vector<RDG::ResourceUsage> draw_trackers_usage; |
| 3715 | HashMap<RID, RDG::ResourceUsage> untracked_usage; |
| 3716 | Vector<UniformSet::SharedTexture> shared_textures_to_update; |
| 3717 | LocalVector<RID> pending_clear_textures; |
| 3718 | |
| 3719 | for (uint32_t i = 0; i < set_uniform_count; i++) { |
| 3720 | const ShaderUniform &set_uniform = set_uniforms[i]; |
| 3721 | int uniform_idx = -1; |
| 3722 | for (int j = 0; j < (int)uniform_count; j++) { |
| 3723 | if (uniforms[j].binding == set_uniform.binding) { |
| 3724 | uniform_idx = j; |
| 3725 | break; |
| 3726 | } |
| 3727 | } |
| 3728 | ERR_FAIL_COND_V_MSG(uniform_idx == -1, RID(), |
| 3729 | "All the shader bindings for the given set must be covered by the uniforms provided. Binding (" + itos(set_uniform.binding) + "), set (" + itos(p_shader_set) + ") was not provided."); |
| 3730 | |
| 3731 | const Uniform &uniform = uniforms[uniform_idx]; |
| 3732 | |
| 3733 | ERR_FAIL_INDEX_V(uniform.uniform_type, RD::UNIFORM_TYPE_MAX, RID()); |
| 3734 | ERR_FAIL_COND_V_MSG(uniform.uniform_type != set_uniform.type, RID(), "Shader '" + shader->name + "' Mismatch uniform type for binding (" + itos(set_uniform.binding) + "), set (" + itos(p_shader_set) + "). Expected '" + SHADER_UNIFORM_NAMES[set_uniform.type] + "', supplied: '" + SHADER_UNIFORM_NAMES[uniform.uniform_type] + "'."); |
| 3735 | |
| 3736 | RDD::BoundUniform &driver_uniform = driver_uniforms[i]; |
| 3737 | driver_uniform.type = uniform.uniform_type; |
| 3738 | driver_uniform.binding = uniform.binding; |
| 3739 | |
| 3740 | // Mark immutable samplers to be skipped when creating uniform set. |
| 3741 | driver_uniform.immutable_sampler = uniform.immutable_sampler; |
| 3742 | |
| 3743 | switch (uniform.uniform_type) { |
| 3744 | case UNIFORM_TYPE_SAMPLER: { |
| 3745 | if (uniform.get_id_count() != (uint32_t)set_uniform.length) { |
| 3746 | if (set_uniform.length > 1) { |
| 3747 | ERR_FAIL_V_MSG(RID(), "Sampler (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") sampler elements, so it should be provided equal number of sampler IDs to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 3748 | } else { |
| 3749 | ERR_FAIL_V_MSG(RID(), "Sampler (binding: " + itos(uniform.binding) + ") should provide one ID referencing a sampler (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | for (uint32_t j = 0; j < uniform.get_id_count(); j++) { |
| 3754 | RDD::SamplerID *sampler_driver_id = sampler_owner.get_or_null(uniform.get_id(j)); |
| 3755 | ERR_FAIL_NULL_V_MSG(sampler_driver_id, RID(), "Sampler (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") is not a valid sampler."); |
| 3756 | |
| 3757 | driver_uniform.ids.push_back(*sampler_driver_id); |
| 3758 | } |
| 3759 | } break; |
| 3760 | case UNIFORM_TYPE_SAMPLER_WITH_TEXTURE: { |
| 3761 | if (uniform.get_id_count() != (uint32_t)set_uniform.length * 2) { |
| 3762 | if (set_uniform.length > 1) { |
| 3763 | ERR_FAIL_V_MSG(RID(), "SamplerTexture (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") sampler&texture elements, so it should provided twice the amount of IDs (sampler,texture pairs) to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 3764 | } else { |
| 3765 | ERR_FAIL_V_MSG(RID(), "SamplerTexture (binding: " + itos(uniform.binding) + ") should provide two IDs referencing a sampler and then a texture (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 3766 | } |
| 3767 | } |
| 3768 | |
| 3769 | for (uint32_t j = 0; j < uniform.get_id_count(); j += 2) { |
| 3770 | RDD::SamplerID *sampler_driver_id = sampler_owner.get_or_null(uniform.get_id(j + 0)); |
| 3771 | ERR_FAIL_NULL_V_MSG(sampler_driver_id, RID(), "SamplerBuffer (binding: " + itos(uniform.binding) + ", index " + itos(j + 1) + ") is not a valid sampler."); |
| 3772 | |
| 3773 | RID texture_id = uniform.get_id(j + 1); |
| 3774 | Texture *texture = texture_owner.get_or_null(texture_id); |
| 3775 | ERR_FAIL_NULL_V_MSG(texture, RID(), "Texture (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") is not a valid texture."); |
| 3776 | |
| 3777 | ERR_FAIL_COND_V_MSG(!(texture->usage_flags & TEXTURE_USAGE_SAMPLING_BIT), RID(), |
| 3778 | "Texture (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") needs the TEXTURE_USAGE_SAMPLING_BIT usage flag set in order to be used as uniform."); |
| 3779 | |
| 3780 | if ((texture->usage_flags & (TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT | TEXTURE_USAGE_INPUT_ATTACHMENT_BIT))) { |
| 3781 | UniformSet::AttachableTexture attachable_texture; |
| 3782 | attachable_texture.bind = set_uniform.binding; |
| 3783 | attachable_texture.texture = texture->owner.is_valid() ? texture->owner : uniform.get_id(j + 1); |
| 3784 | attachable_textures.push_back(attachable_texture); |
| 3785 | } |
| 3786 | |
| 3787 | if (texture->pending_clear) { |
| 3788 | pending_clear_textures.push_back(texture_id); |
| 3789 | } |
| 3790 | |
| 3791 | RDD::TextureID driver_id = texture->driver_id; |
| 3792 | RDG::ResourceTracker *tracker = texture->draw_tracker; |
| 3793 | if (texture->shared_fallback != nullptr && texture->shared_fallback->texture.id != 0) { |
| 3794 | driver_id = texture->shared_fallback->texture; |
| 3795 | tracker = texture->shared_fallback->texture_tracker; |
| 3796 | shared_textures_to_update.push_back({ false, texture_id }); |
| 3797 | } |
| 3798 | |
| 3799 | if (tracker != nullptr) { |
| 3800 | draw_trackers.push_back(tracker); |
| 3801 | draw_trackers_usage.push_back(RDG::RESOURCE_USAGE_TEXTURE_SAMPLE); |
| 3802 | } else { |
| 3803 | untracked_usage[texture_id] = RDG::RESOURCE_USAGE_TEXTURE_SAMPLE; |
| 3804 | } |
| 3805 | |
| 3806 | DEV_ASSERT(!texture->owner.is_valid() || texture_owner.get_or_null(texture->owner)); |
| 3807 | |
| 3808 | driver_uniform.ids.push_back(*sampler_driver_id); |
| 3809 | driver_uniform.ids.push_back(driver_id); |
| 3810 | _check_transfer_worker_texture(texture); |
| 3811 | } |
| 3812 | } break; |
| 3813 | case UNIFORM_TYPE_TEXTURE: { |
| 3814 | if (uniform.get_id_count() != (uint32_t)set_uniform.length) { |
| 3815 | if (set_uniform.length > 1) { |
| 3816 | ERR_FAIL_V_MSG(RID(), "Texture (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") textures, so it should be provided equal number of texture IDs to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 3817 | } else { |
| 3818 | ERR_FAIL_V_MSG(RID(), "Texture (binding: " + itos(uniform.binding) + ") should provide one ID referencing a texture (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 3819 | } |
| 3820 | } |
| 3821 | |
| 3822 | for (uint32_t j = 0; j < uniform.get_id_count(); j++) { |
| 3823 | RID texture_id = uniform.get_id(j); |
| 3824 | Texture *texture = texture_owner.get_or_null(texture_id); |
| 3825 | ERR_FAIL_NULL_V_MSG(texture, RID(), "Texture (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") is not a valid texture."); |
| 3826 | |
| 3827 | ERR_FAIL_COND_V_MSG(!(texture->usage_flags & TEXTURE_USAGE_SAMPLING_BIT), RID(), |
| 3828 | "Texture (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") needs the TEXTURE_USAGE_SAMPLING_BIT usage flag set in order to be used as uniform."); |
| 3829 | |
| 3830 | if ((texture->usage_flags & (TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT | TEXTURE_USAGE_INPUT_ATTACHMENT_BIT))) { |
| 3831 | UniformSet::AttachableTexture attachable_texture; |
| 3832 | attachable_texture.bind = set_uniform.binding; |
| 3833 | attachable_texture.texture = texture->owner.is_valid() ? texture->owner : uniform.get_id(j); |
| 3834 | attachable_textures.push_back(attachable_texture); |
| 3835 | } |
| 3836 | |
| 3837 | if (texture->pending_clear) { |
| 3838 | pending_clear_textures.push_back(texture_id); |
| 3839 | } |
| 3840 | |
| 3841 | RDD::TextureID driver_id = texture->driver_id; |
| 3842 | RDG::ResourceTracker *tracker = texture->draw_tracker; |
| 3843 | if (texture->shared_fallback != nullptr && texture->shared_fallback->texture.id != 0) { |
| 3844 | driver_id = texture->shared_fallback->texture; |
| 3845 | tracker = texture->shared_fallback->texture_tracker; |
| 3846 | shared_textures_to_update.push_back({ false, texture_id }); |
| 3847 | } |
| 3848 | |
| 3849 | if (tracker != nullptr) { |
| 3850 | draw_trackers.push_back(tracker); |
| 3851 | draw_trackers_usage.push_back(RDG::RESOURCE_USAGE_TEXTURE_SAMPLE); |
| 3852 | } else { |
| 3853 | untracked_usage[texture_id] = RDG::RESOURCE_USAGE_TEXTURE_SAMPLE; |
| 3854 | } |
| 3855 | |
| 3856 | DEV_ASSERT(!texture->owner.is_valid() || texture_owner.get_or_null(texture->owner)); |
| 3857 | |
| 3858 | driver_uniform.ids.push_back(driver_id); |
| 3859 | _check_transfer_worker_texture(texture); |
| 3860 | } |
| 3861 | } break; |
| 3862 | case UNIFORM_TYPE_IMAGE: { |
| 3863 | if (uniform.get_id_count() != (uint32_t)set_uniform.length) { |
| 3864 | if (set_uniform.length > 1) { |
| 3865 | ERR_FAIL_V_MSG(RID(), "Image (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") textures, so it should be provided equal number of texture IDs to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 3866 | } else { |
| 3867 | ERR_FAIL_V_MSG(RID(), "Image (binding: " + itos(uniform.binding) + ") should provide one ID referencing a texture (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 3868 | } |
| 3869 | } |
| 3870 | |
| 3871 | for (uint32_t j = 0; j < uniform.get_id_count(); j++) { |
| 3872 | RID texture_id = uniform.get_id(j); |
| 3873 | Texture *texture = texture_owner.get_or_null(texture_id); |
| 3874 | |
| 3875 | ERR_FAIL_NULL_V_MSG(texture, RID(), |
| 3876 | "Image (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") is not a valid texture."); |
| 3877 | |
| 3878 | ERR_FAIL_COND_V_MSG(!(texture->usage_flags & TEXTURE_USAGE_STORAGE_BIT), RID(), |
| 3879 | "Image (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") needs the TEXTURE_USAGE_STORAGE_BIT usage flag set in order to be used as uniform."); |
| 3880 | |
| 3881 | if (texture->owner.is_null() && texture->shared_fallback != nullptr) { |
| 3882 | shared_textures_to_update.push_back({ true, texture_id }); |
| 3883 | } |
| 3884 | |
| 3885 | if (texture->pending_clear) { |
| 3886 | pending_clear_textures.push_back(texture_id); |
| 3887 | } |
| 3888 | |
| 3889 | if (_texture_make_mutable(texture, texture_id)) { |
| 3890 | // The texture must be mutable as a layout transition will be required. |
| 3891 | draw_graph.add_synchronization(); |
| 3892 | } |
| 3893 | |
| 3894 | if (texture->draw_tracker != nullptr) { |
| 3895 | draw_trackers.push_back(texture->draw_tracker); |
| 3896 | |
| 3897 | if (set_uniform.writable) { |
| 3898 | draw_trackers_usage.push_back(RDG::RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE); |
| 3899 | } else { |
| 3900 | draw_trackers_usage.push_back(RDG::RESOURCE_USAGE_STORAGE_IMAGE_READ); |
| 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | DEV_ASSERT(!texture->owner.is_valid() || texture_owner.get_or_null(texture->owner)); |
| 3905 | |
| 3906 | driver_uniform.ids.push_back(texture->driver_id); |
| 3907 | _check_transfer_worker_texture(texture); |
| 3908 | } |
| 3909 | } break; |
| 3910 | case UNIFORM_TYPE_TEXTURE_BUFFER: { |
| 3911 | if (uniform.get_id_count() != (uint32_t)set_uniform.length) { |
| 3912 | if (set_uniform.length > 1) { |
| 3913 | ERR_FAIL_V_MSG(RID(), "Buffer (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") texture buffer elements, so it should be provided equal number of texture buffer IDs to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 3914 | } else { |
| 3915 | ERR_FAIL_V_MSG(RID(), "Buffer (binding: " + itos(uniform.binding) + ") should provide one ID referencing a texture buffer (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 3916 | } |
| 3917 | } |
| 3918 | |
| 3919 | for (uint32_t j = 0; j < uniform.get_id_count(); j++) { |
| 3920 | RID buffer_id = uniform.get_id(j); |
| 3921 | Buffer *buffer = texture_buffer_owner.get_or_null(buffer_id); |
| 3922 | ERR_FAIL_NULL_V_MSG(buffer, RID(), "Texture Buffer (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") is not a valid texture buffer."); |
| 3923 | |
| 3924 | if (set_uniform.writable && _buffer_make_mutable(buffer, buffer_id)) { |
| 3925 | // The buffer must be mutable if it's used for writing. |
| 3926 | draw_graph.add_synchronization(); |
| 3927 | } |
| 3928 | |
| 3929 | if (buffer->draw_tracker != nullptr) { |
| 3930 | draw_trackers.push_back(buffer->draw_tracker); |
| 3931 | |
| 3932 | if (set_uniform.writable) { |
| 3933 | draw_trackers_usage.push_back(RDG::RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE); |
| 3934 | } else { |
| 3935 | draw_trackers_usage.push_back(RDG::RESOURCE_USAGE_TEXTURE_BUFFER_READ); |
| 3936 | } |
| 3937 | } else { |
| 3938 | untracked_usage[buffer_id] = RDG::RESOURCE_USAGE_TEXTURE_BUFFER_READ; |
| 3939 | } |
| 3940 | |
| 3941 | driver_uniform.ids.push_back(buffer->driver_id); |
| 3942 | _check_transfer_worker_buffer(buffer); |
| 3943 | } |
| 3944 | } break; |
| 3945 | case UNIFORM_TYPE_SAMPLER_WITH_TEXTURE_BUFFER: { |
| 3946 | if (uniform.get_id_count() != (uint32_t)set_uniform.length * 2) { |
| 3947 | if (set_uniform.length > 1) { |
| 3948 | ERR_FAIL_V_MSG(RID(), "SamplerBuffer (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") sampler buffer elements, so it should provided twice the amount of IDs (sampler,buffer pairs) to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 3949 | } else { |
| 3950 | ERR_FAIL_V_MSG(RID(), "SamplerBuffer (binding: " + itos(uniform.binding) + ") should provide two IDs referencing a sampler and then a texture buffer (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 3951 | } |
| 3952 | } |
| 3953 | |
| 3954 | for (uint32_t j = 0; j < uniform.get_id_count(); j += 2) { |
| 3955 | RDD::SamplerID *sampler_driver_id = sampler_owner.get_or_null(uniform.get_id(j + 0)); |
| 3956 | ERR_FAIL_NULL_V_MSG(sampler_driver_id, RID(), "SamplerBuffer (binding: " + itos(uniform.binding) + ", index " + itos(j + 1) + ") is not a valid sampler."); |
| 3957 | |
| 3958 | RID buffer_id = uniform.get_id(j + 1); |
| 3959 | Buffer *buffer = texture_buffer_owner.get_or_null(buffer_id); |
| 3960 | ERR_FAIL_NULL_V_MSG(buffer, RID(), "SamplerBuffer (binding: " + itos(uniform.binding) + ", index " + itos(j + 1) + ") is not a valid texture buffer."); |
| 3961 | |
| 3962 | if (buffer->draw_tracker != nullptr) { |
| 3963 | draw_trackers.push_back(buffer->draw_tracker); |
| 3964 | draw_trackers_usage.push_back(RDG::RESOURCE_USAGE_TEXTURE_BUFFER_READ); |
| 3965 | } else { |
| 3966 | untracked_usage[buffer_id] = RDG::RESOURCE_USAGE_TEXTURE_BUFFER_READ; |
| 3967 | } |
| 3968 | |
| 3969 | driver_uniform.ids.push_back(*sampler_driver_id); |
| 3970 | driver_uniform.ids.push_back(buffer->driver_id); |
| 3971 | _check_transfer_worker_buffer(buffer); |
| 3972 | } |
| 3973 | } break; |
| 3974 | case UNIFORM_TYPE_IMAGE_BUFFER: { |
| 3975 | // Todo. |
| 3976 | } break; |
| 3977 | case UNIFORM_TYPE_UNIFORM_BUFFER: |
| 3978 | case UNIFORM_TYPE_UNIFORM_BUFFER_DYNAMIC: { |
| 3979 | ERR_FAIL_COND_V_MSG(uniform.get_id_count() != 1, RID(), |
| 3980 | "Uniform buffer supplied (binding: " + itos(uniform.binding) + ") must provide one ID (" + itos(uniform.get_id_count()) + " provided)."); |
| 3981 | |
| 3982 | RID buffer_id = uniform.get_id(0); |
| 3983 | Buffer *buffer = uniform_buffer_owner.get_or_null(buffer_id); |
| 3984 | ERR_FAIL_NULL_V_MSG(buffer, RID(), "Uniform buffer supplied (binding: " + itos(uniform.binding) + ") is invalid."); |
| 3985 | |
| 3986 | ERR_FAIL_COND_V_MSG(buffer->size < (uint32_t)set_uniform.length, RID(), |
| 3987 | "Uniform buffer supplied (binding: " + itos(uniform.binding) + ") size (" + itos(buffer->size) + ") is smaller than size of shader uniform: (" + itos(set_uniform.length) + ")."); |
| 3988 | |
| 3989 | if (buffer->draw_tracker != nullptr) { |
| 3990 | draw_trackers.push_back(buffer->draw_tracker); |
| 3991 | draw_trackers_usage.push_back(RDG::RESOURCE_USAGE_UNIFORM_BUFFER_READ); |
| 3992 | } else { |
| 3993 | untracked_usage[buffer_id] = RDG::RESOURCE_USAGE_UNIFORM_BUFFER_READ; |
| 3994 | } |
| 3995 | |
| 3996 | driver_uniform.ids.push_back(buffer->driver_id); |
| 3997 | _check_transfer_worker_buffer(buffer); |
| 3998 | } break; |
| 3999 | case UNIFORM_TYPE_STORAGE_BUFFER: |
| 4000 | case UNIFORM_TYPE_STORAGE_BUFFER_DYNAMIC: { |
| 4001 | ERR_FAIL_COND_V_MSG(uniform.get_id_count() != 1, RID(), |
| 4002 | "Storage buffer supplied (binding: " + itos(uniform.binding) + ") must provide one ID (" + itos(uniform.get_id_count()) + " provided)."); |
| 4003 | |
| 4004 | Buffer *buffer = nullptr; |
| 4005 | |
| 4006 | RID buffer_id = uniform.get_id(0); |
| 4007 | if (storage_buffer_owner.owns(buffer_id)) { |
| 4008 | buffer = storage_buffer_owner.get_or_null(buffer_id); |
| 4009 | } else if (vertex_buffer_owner.owns(buffer_id)) { |
| 4010 | buffer = vertex_buffer_owner.get_or_null(buffer_id); |
| 4011 | |
| 4012 | ERR_FAIL_COND_V_MSG(!(buffer->usage.has_flag(RDD::BUFFER_USAGE_STORAGE_BIT)), RID(), "Vertex buffer supplied (binding: " + itos(uniform.binding) + ") was not created with storage flag."); |
| 4013 | } |
| 4014 | ERR_FAIL_NULL_V_MSG(buffer, RID(), "Storage buffer supplied (binding: " + itos(uniform.binding) + ") is invalid."); |
| 4015 | |
| 4016 | // If 0, then it's sized on link time. |
| 4017 | ERR_FAIL_COND_V_MSG(set_uniform.length > 0 && buffer->size != (uint32_t)set_uniform.length, RID(), |
| 4018 | "Storage buffer supplied (binding: " + itos(uniform.binding) + ") size (" + itos(buffer->size) + ") does not match size of shader uniform: (" + itos(set_uniform.length) + ")."); |
| 4019 | |
| 4020 | if (set_uniform.writable && _buffer_make_mutable(buffer, buffer_id)) { |
| 4021 | // The buffer must be mutable if it's used for writing. |
| 4022 | draw_graph.add_synchronization(); |
| 4023 | } |
| 4024 | |
| 4025 | if (buffer->draw_tracker != nullptr) { |
| 4026 | draw_trackers.push_back(buffer->draw_tracker); |
| 4027 | |
| 4028 | if (set_uniform.writable) { |
| 4029 | draw_trackers_usage.push_back(RDG::RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE); |
| 4030 | } else { |
| 4031 | draw_trackers_usage.push_back(RDG::RESOURCE_USAGE_STORAGE_BUFFER_READ); |
| 4032 | } |
| 4033 | } else { |
| 4034 | untracked_usage[buffer_id] = RDG::RESOURCE_USAGE_STORAGE_BUFFER_READ; |
| 4035 | } |
| 4036 | |
| 4037 | driver_uniform.ids.push_back(buffer->driver_id); |
| 4038 | _check_transfer_worker_buffer(buffer); |
| 4039 | } break; |
| 4040 | case UNIFORM_TYPE_INPUT_ATTACHMENT: { |
| 4041 | ERR_FAIL_COND_V_MSG(shader->is_compute, RID(), "InputAttachment (binding: " + itos(uniform.binding) + ") supplied for compute shader (this is not allowed)."); |
| 4042 | |
| 4043 | if (uniform.get_id_count() != (uint32_t)set_uniform.length) { |
| 4044 | if (set_uniform.length > 1) { |
| 4045 | ERR_FAIL_V_MSG(RID(), "InputAttachment (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") textures, so it should be provided equal number of texture IDs to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 4046 | } else { |
| 4047 | ERR_FAIL_V_MSG(RID(), "InputAttachment (binding: " + itos(uniform.binding) + ") should provide one ID referencing a texture (IDs provided: " + itos(uniform.get_id_count()) + ")."); |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | for (uint32_t j = 0; j < uniform.get_id_count(); j++) { |
| 4052 | RID texture_id = uniform.get_id(j); |
| 4053 | Texture *texture = texture_owner.get_or_null(texture_id); |
| 4054 | |
| 4055 | ERR_FAIL_NULL_V_MSG(texture, RID(), |
| 4056 | "InputAttachment (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") is not a valid texture."); |
| 4057 | |
| 4058 | ERR_FAIL_COND_V_MSG(!(texture->usage_flags & TEXTURE_USAGE_SAMPLING_BIT), RID(), |
| 4059 | "InputAttachment (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") needs the TEXTURE_USAGE_SAMPLING_BIT usage flag set in order to be used as uniform."); |
| 4060 | |
| 4061 | DEV_ASSERT(!texture->owner.is_valid() || texture_owner.get_or_null(texture->owner)); |
| 4062 | |
| 4063 | driver_uniform.ids.push_back(texture->driver_id); |
| 4064 | _check_transfer_worker_texture(texture); |
| 4065 | } |
| 4066 | } break; |
| 4067 | default: { |
| 4068 | } |
| 4069 | } |
| 4070 | } |
| 4071 | |
| 4072 | RDD::UniformSetID driver_uniform_set = driver->uniform_set_create(driver_uniforms, shader->driver_id, p_shader_set, p_linear_pool ? frame : -1); |
| 4073 | ERR_FAIL_COND_V(!driver_uniform_set, RID()); |
| 4074 | |
| 4075 | UniformSet uniform_set; |
| 4076 | uniform_set.driver_id = driver_uniform_set; |
| 4077 | uniform_set.format = shader->set_formats[p_shader_set]; |
| 4078 | uniform_set.attachable_textures = attachable_textures; |
| 4079 | uniform_set.draw_trackers = draw_trackers; |
| 4080 | uniform_set.draw_trackers_usage = draw_trackers_usage; |
| 4081 | uniform_set.untracked_usage = untracked_usage; |
| 4082 | uniform_set.shared_textures_to_update = shared_textures_to_update; |
| 4083 | uniform_set.pending_clear_textures = pending_clear_textures; |
| 4084 | uniform_set.shader_set = p_shader_set; |
| 4085 | uniform_set.shader_id = p_shader; |
| 4086 | |
| 4087 | RID id = uniform_set_owner.make_rid(uniform_set); |
| 4088 | #ifdef DEV_ENABLED |
| 4089 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 4090 | #endif |
| 4091 | // Add dependencies. |
| 4092 | _add_dependency(id, p_shader); |
| 4093 | for (uint32_t i = 0; i < uniform_count; i++) { |
| 4094 | const Uniform &uniform = uniforms[i]; |
| 4095 | int id_count = uniform.get_id_count(); |
| 4096 | for (int j = 0; j < id_count; j++) { |
| 4097 | _add_dependency(id, uniform.get_id(j)); |
| 4098 | } |
| 4099 | } |
| 4100 | |
| 4101 | return id; |
| 4102 | } |
| 4103 | |
| 4104 | bool RenderingDevice::uniform_set_is_valid(RID p_uniform_set) { |
| 4105 | _THREAD_SAFE_METHOD_ |
| 4106 | |
| 4107 | return uniform_set_owner.owns(p_uniform_set); |
| 4108 | } |
| 4109 | |
| 4110 | void RenderingDevice::uniform_set_set_invalidation_callback(RID p_uniform_set, InvalidationCallback p_callback, void *p_userdata) { |
| 4111 | _THREAD_SAFE_METHOD_ |
| 4112 | |
| 4113 | UniformSet *us = uniform_set_owner.get_or_null(p_uniform_set); |
| 4114 | ERR_FAIL_NULL(us); |
| 4115 | us->invalidated_callback = p_callback; |
| 4116 | us->invalidated_callback_userdata = p_userdata; |
| 4117 | } |
| 4118 | |
| 4119 | bool RenderingDevice::uniform_sets_have_linear_pools() const { |
| 4120 | return driver->uniform_sets_have_linear_pools(); |
| 4121 | } |
| 4122 | |
| 4123 | /*******************/ |
| 4124 | /**** PIPELINES ****/ |
| 4125 | /*******************/ |
| 4126 | |
| 4127 | RID RenderingDevice::render_pipeline_create(RID p_shader, FramebufferFormatID p_framebuffer_format, VertexFormatID p_vertex_format, RenderPrimitive p_render_primitive, const PipelineRasterizationState &p_rasterization_state, const PipelineMultisampleState &p_multisample_state, const PipelineDepthStencilState &p_depth_stencil_state, const PipelineColorBlendState &p_blend_state, BitField<PipelineDynamicStateFlags> p_dynamic_state_flags, uint32_t p_for_render_pass, const Vector<PipelineSpecializationConstant> &p_specialization_constants) { |
| 4128 | // Needs a shader. |
| 4129 | Shader *shader = shader_owner.get_or_null(p_shader); |
| 4130 | ERR_FAIL_NULL_V(shader, RID()); |
| 4131 | ERR_FAIL_COND_V_MSG(shader->is_compute, RID(), "Compute shaders can't be used in render pipelines"); |
| 4132 | |
| 4133 | // Validate pre-raster shader. One of stages must be vertex shader or mesh shader (not implemented yet). |
| 4134 | ERR_FAIL_COND_V_MSG(!shader->stage_bits.has_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT), RID(), "Pre-raster shader (vertex shader) is not provided for pipeline creation."); |
| 4135 | |
| 4136 | FramebufferFormat fb_format; |
| 4137 | { |
| 4138 | _THREAD_SAFE_METHOD_ |
| 4139 | |
| 4140 | if (p_framebuffer_format == INVALID_ID) { |
| 4141 | // If nothing provided, use an empty one (no attachments). |
| 4142 | p_framebuffer_format = framebuffer_format_create(Vector<AttachmentFormat>()); |
| 4143 | } |
| 4144 | ERR_FAIL_COND_V(!framebuffer_formats.has(p_framebuffer_format), RID()); |
| 4145 | fb_format = framebuffer_formats[p_framebuffer_format]; |
| 4146 | } |
| 4147 | |
| 4148 | // Validate shader vs. framebuffer. |
| 4149 | { |
| 4150 | ERR_FAIL_COND_V_MSG(p_for_render_pass >= uint32_t(fb_format.E->key().passes.size()), RID(), "Render pass requested for pipeline creation (" + itos(p_for_render_pass) + ") is out of bounds"); |
| 4151 | const FramebufferPass &pass = fb_format.E->key().passes[p_for_render_pass]; |
| 4152 | uint32_t output_mask = 0; |
| 4153 | for (int i = 0; i < pass.color_attachments.size(); i++) { |
| 4154 | if (pass.color_attachments[i] != ATTACHMENT_UNUSED) { |
| 4155 | output_mask |= 1 << i; |
| 4156 | } |
| 4157 | } |
| 4158 | ERR_FAIL_COND_V_MSG(shader->fragment_output_mask != output_mask, RID(), |
| 4159 | "Mismatch fragment shader output mask (" + itos(shader->fragment_output_mask) + ") and framebuffer color output mask (" + itos(output_mask) + ") when binding both in render pipeline."); |
| 4160 | } |
| 4161 | |
| 4162 | RDD::VertexFormatID driver_vertex_format; |
| 4163 | if (p_vertex_format != INVALID_ID) { |
| 4164 | // Uses vertices, else it does not. |
| 4165 | ERR_FAIL_COND_V(!vertex_formats.has(p_vertex_format), RID()); |
| 4166 | const VertexDescriptionCache &vd = vertex_formats[p_vertex_format]; |
| 4167 | driver_vertex_format = vertex_formats[p_vertex_format].driver_id; |
| 4168 | |
| 4169 | // Validate with inputs. |
| 4170 | for (uint32_t i = 0; i < 64; i++) { |
| 4171 | if (!(shader->vertex_input_mask & ((uint64_t)1) << i)) { |
| 4172 | continue; |
| 4173 | } |
| 4174 | bool found = false; |
| 4175 | for (int j = 0; j < vd.vertex_formats.size(); j++) { |
| 4176 | if (vd.vertex_formats[j].location == i) { |
| 4177 | found = true; |
| 4178 | break; |
| 4179 | } |
| 4180 | } |
| 4181 | |
| 4182 | ERR_FAIL_COND_V_MSG(!found, RID(), |
| 4183 | "Shader vertex input location (" + itos(i) + ") not provided in vertex input description for pipeline creation."); |
| 4184 | } |
| 4185 | |
| 4186 | } else { |
| 4187 | ERR_FAIL_COND_V_MSG(shader->vertex_input_mask != 0, RID(), |
| 4188 | "Shader contains vertex inputs, but no vertex input description was provided for pipeline creation."); |
| 4189 | } |
| 4190 | |
| 4191 | ERR_FAIL_INDEX_V(p_render_primitive, RENDER_PRIMITIVE_MAX, RID()); |
| 4192 | |
| 4193 | ERR_FAIL_INDEX_V(p_rasterization_state.cull_mode, 3, RID()); |
| 4194 | |
| 4195 | if (p_multisample_state.sample_mask.size()) { |
| 4196 | // Use sample mask. |
| 4197 | ERR_FAIL_COND_V((int)TEXTURE_SAMPLES_COUNT[p_multisample_state.sample_count] != p_multisample_state.sample_mask.size(), RID()); |
| 4198 | } |
| 4199 | |
| 4200 | ERR_FAIL_INDEX_V(p_depth_stencil_state.depth_compare_operator, COMPARE_OP_MAX, RID()); |
| 4201 | |
| 4202 | ERR_FAIL_INDEX_V(p_depth_stencil_state.front_op.fail, STENCIL_OP_MAX, RID()); |
| 4203 | ERR_FAIL_INDEX_V(p_depth_stencil_state.front_op.pass, STENCIL_OP_MAX, RID()); |
| 4204 | ERR_FAIL_INDEX_V(p_depth_stencil_state.front_op.depth_fail, STENCIL_OP_MAX, RID()); |
| 4205 | ERR_FAIL_INDEX_V(p_depth_stencil_state.front_op.compare, COMPARE_OP_MAX, RID()); |
| 4206 | |
| 4207 | ERR_FAIL_INDEX_V(p_depth_stencil_state.back_op.fail, STENCIL_OP_MAX, RID()); |
| 4208 | ERR_FAIL_INDEX_V(p_depth_stencil_state.back_op.pass, STENCIL_OP_MAX, RID()); |
| 4209 | ERR_FAIL_INDEX_V(p_depth_stencil_state.back_op.depth_fail, STENCIL_OP_MAX, RID()); |
| 4210 | ERR_FAIL_INDEX_V(p_depth_stencil_state.back_op.compare, COMPARE_OP_MAX, RID()); |
| 4211 | |
| 4212 | ERR_FAIL_INDEX_V(p_blend_state.logic_op, LOGIC_OP_MAX, RID()); |
| 4213 | |
| 4214 | const FramebufferPass &pass = fb_format.E->key().passes[p_for_render_pass]; |
| 4215 | ERR_FAIL_COND_V(p_blend_state.attachments.size() < pass.color_attachments.size(), RID()); |
| 4216 | for (int i = 0; i < pass.color_attachments.size(); i++) { |
| 4217 | if (pass.color_attachments[i] != ATTACHMENT_UNUSED) { |
| 4218 | ERR_FAIL_INDEX_V(p_blend_state.attachments[i].src_color_blend_factor, BLEND_FACTOR_MAX, RID()); |
| 4219 | ERR_FAIL_INDEX_V(p_blend_state.attachments[i].dst_color_blend_factor, BLEND_FACTOR_MAX, RID()); |
| 4220 | ERR_FAIL_INDEX_V(p_blend_state.attachments[i].color_blend_op, BLEND_OP_MAX, RID()); |
| 4221 | |
| 4222 | ERR_FAIL_INDEX_V(p_blend_state.attachments[i].src_alpha_blend_factor, BLEND_FACTOR_MAX, RID()); |
| 4223 | ERR_FAIL_INDEX_V(p_blend_state.attachments[i].dst_alpha_blend_factor, BLEND_FACTOR_MAX, RID()); |
| 4224 | ERR_FAIL_INDEX_V(p_blend_state.attachments[i].alpha_blend_op, BLEND_OP_MAX, RID()); |
| 4225 | } |
| 4226 | } |
| 4227 | |
| 4228 | for (int i = 0; i < shader->specialization_constants.size(); i++) { |
| 4229 | const ShaderSpecializationConstant &sc = shader->specialization_constants[i]; |
| 4230 | for (int j = 0; j < p_specialization_constants.size(); j++) { |
| 4231 | const PipelineSpecializationConstant &psc = p_specialization_constants[j]; |
| 4232 | if (psc.constant_id == sc.constant_id) { |
| 4233 | ERR_FAIL_COND_V_MSG(psc.type != sc.type, RID(), "Specialization constant provided for id (" + itos(sc.constant_id) + ") is of the wrong type."); |
| 4234 | break; |
| 4235 | } |
| 4236 | } |
| 4237 | } |
| 4238 | |
| 4239 | RenderPipeline pipeline; |
| 4240 | pipeline.driver_id = driver->render_pipeline_create( |
| 4241 | shader->driver_id, |
| 4242 | driver_vertex_format, |
| 4243 | p_render_primitive, |
| 4244 | p_rasterization_state, |
| 4245 | p_multisample_state, |
| 4246 | p_depth_stencil_state, |
| 4247 | p_blend_state, |
| 4248 | pass.color_attachments, |
| 4249 | p_dynamic_state_flags, |
| 4250 | fb_format.render_pass, |
| 4251 | p_for_render_pass, |
| 4252 | p_specialization_constants); |
| 4253 | ERR_FAIL_COND_V(!pipeline.driver_id, RID()); |
| 4254 | |
| 4255 | if (pipeline_cache_enabled) { |
| 4256 | update_pipeline_cache(); |
| 4257 | } |
| 4258 | |
| 4259 | pipeline.shader = p_shader; |
| 4260 | pipeline.shader_driver_id = shader->driver_id; |
| 4261 | pipeline.shader_layout_hash = shader->layout_hash; |
| 4262 | pipeline.set_formats = shader->set_formats; |
| 4263 | pipeline.push_constant_size = shader->push_constant_size; |
| 4264 | pipeline.stage_bits = shader->stage_bits; |
| 4265 | |
| 4266 | #ifdef DEBUG_ENABLED |
| 4267 | pipeline.validation.dynamic_state = p_dynamic_state_flags; |
| 4268 | pipeline.validation.framebuffer_format = p_framebuffer_format; |
| 4269 | pipeline.validation.render_pass = p_for_render_pass; |
| 4270 | pipeline.validation.vertex_format = p_vertex_format; |
| 4271 | pipeline.validation.uses_restart_indices = p_render_primitive == RENDER_PRIMITIVE_TRIANGLE_STRIPS_WITH_RESTART_INDEX; |
| 4272 | |
| 4273 | static const uint32_t primitive_divisor[RENDER_PRIMITIVE_MAX] = { |
| 4274 | 1, 2, 1, 1, 1, 3, 1, 1, 1, 1, 1 |
| 4275 | }; |
| 4276 | pipeline.validation.primitive_divisor = primitive_divisor[p_render_primitive]; |
| 4277 | static const uint32_t primitive_minimum[RENDER_PRIMITIVE_MAX] = { |
| 4278 | 1, |
| 4279 | 2, |
| 4280 | 2, |
| 4281 | 2, |
| 4282 | 2, |
| 4283 | 3, |
| 4284 | 3, |
| 4285 | 3, |
| 4286 | 3, |
| 4287 | 3, |
| 4288 | 1, |
| 4289 | }; |
| 4290 | pipeline.validation.primitive_minimum = primitive_minimum[p_render_primitive]; |
| 4291 | #endif |
| 4292 | |
| 4293 | // Create ID to associate with this pipeline. |
| 4294 | RID id = render_pipeline_owner.make_rid(pipeline); |
| 4295 | { |
| 4296 | _THREAD_SAFE_METHOD_ |
| 4297 | |
| 4298 | #ifdef DEV_ENABLED |
| 4299 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 4300 | #endif |
| 4301 | // Now add all the dependencies. |
| 4302 | _add_dependency(id, p_shader); |
| 4303 | } |
| 4304 | |
| 4305 | return id; |
| 4306 | } |
| 4307 | |
| 4308 | bool RenderingDevice::render_pipeline_is_valid(RID p_pipeline) { |
| 4309 | _THREAD_SAFE_METHOD_ |
| 4310 | |
| 4311 | return render_pipeline_owner.owns(p_pipeline); |
| 4312 | } |
| 4313 | |
| 4314 | RID RenderingDevice::compute_pipeline_create(RID p_shader, const Vector<PipelineSpecializationConstant> &p_specialization_constants) { |
| 4315 | Shader *shader; |
| 4316 | |
| 4317 | { |
| 4318 | _THREAD_SAFE_METHOD_ |
| 4319 | |
| 4320 | // Needs a shader. |
| 4321 | shader = shader_owner.get_or_null(p_shader); |
| 4322 | ERR_FAIL_NULL_V(shader, RID()); |
| 4323 | |
| 4324 | ERR_FAIL_COND_V_MSG(!shader->is_compute, RID(), |
| 4325 | "Non-compute shaders can't be used in compute pipelines"); |
| 4326 | } |
| 4327 | |
| 4328 | for (int i = 0; i < shader->specialization_constants.size(); i++) { |
| 4329 | const ShaderSpecializationConstant &sc = shader->specialization_constants[i]; |
| 4330 | for (int j = 0; j < p_specialization_constants.size(); j++) { |
| 4331 | const PipelineSpecializationConstant &psc = p_specialization_constants[j]; |
| 4332 | if (psc.constant_id == sc.constant_id) { |
| 4333 | ERR_FAIL_COND_V_MSG(psc.type != sc.type, RID(), "Specialization constant provided for id (" + itos(sc.constant_id) + ") is of the wrong type."); |
| 4334 | break; |
| 4335 | } |
| 4336 | } |
| 4337 | } |
| 4338 | |
| 4339 | ComputePipeline pipeline; |
| 4340 | pipeline.driver_id = driver->compute_pipeline_create(shader->driver_id, p_specialization_constants); |
| 4341 | ERR_FAIL_COND_V(!pipeline.driver_id, RID()); |
| 4342 | |
| 4343 | if (pipeline_cache_enabled) { |
| 4344 | update_pipeline_cache(); |
| 4345 | } |
| 4346 | |
| 4347 | pipeline.shader = p_shader; |
| 4348 | pipeline.shader_driver_id = shader->driver_id; |
| 4349 | pipeline.shader_layout_hash = shader->layout_hash; |
| 4350 | pipeline.set_formats = shader->set_formats; |
| 4351 | pipeline.push_constant_size = shader->push_constant_size; |
| 4352 | pipeline.local_group_size[0] = shader->compute_local_size[0]; |
| 4353 | pipeline.local_group_size[1] = shader->compute_local_size[1]; |
| 4354 | pipeline.local_group_size[2] = shader->compute_local_size[2]; |
| 4355 | |
| 4356 | // Create ID to associate with this pipeline. |
| 4357 | RID id = compute_pipeline_owner.make_rid(pipeline); |
| 4358 | { |
| 4359 | _THREAD_SAFE_METHOD_ |
| 4360 | |
| 4361 | #ifdef DEV_ENABLED |
| 4362 | set_resource_name(id, "RID:" + itos(id.get_id())); |
| 4363 | #endif |
| 4364 | // Now add all the dependencies. |
| 4365 | _add_dependency(id, p_shader); |
| 4366 | } |
| 4367 | |
| 4368 | return id; |
| 4369 | } |
| 4370 | |
| 4371 | bool RenderingDevice::compute_pipeline_is_valid(RID p_pipeline) { |
| 4372 | _THREAD_SAFE_METHOD_ |
| 4373 | |
| 4374 | return compute_pipeline_owner.owns(p_pipeline); |
| 4375 | } |
| 4376 | |
| 4377 | /****************/ |
| 4378 | /**** SCREEN ****/ |
| 4379 | /****************/ |
| 4380 | |
| 4381 | uint32_t RenderingDevice::_get_swap_chain_desired_count() const { |
| 4382 | return MAX(2U, uint32_t(GLOBAL_GET_CACHED(uint32_t, "rendering/rendering_device/vsync/swapchain_image_count"))); |
| 4383 | } |
| 4384 | |
| 4385 | Error RenderingDevice::screen_create(DisplayServer::WindowID p_screen) { |
| 4386 | _THREAD_SAFE_METHOD_ |
| 4387 | |
| 4388 | RenderingContextDriver::SurfaceID surface = context->surface_get_from_window(p_screen); |
| 4389 | ERR_FAIL_COND_V_MSG(surface == 0, ERR_CANT_CREATE, "A surface was not created for the screen."); |
| 4390 | |
| 4391 | HashMap<DisplayServer::WindowID, RDD::SwapChainID>::ConstIterator it = screen_swap_chains.find(p_screen); |
| 4392 | ERR_FAIL_COND_V_MSG(it != screen_swap_chains.end(), ERR_CANT_CREATE, "A swap chain was already created for the screen."); |
| 4393 | |
| 4394 | RDD::SwapChainID swap_chain = driver->swap_chain_create(surface); |
| 4395 | ERR_FAIL_COND_V_MSG(swap_chain.id == 0, ERR_CANT_CREATE, "Unable to create swap chain."); |
| 4396 | |
| 4397 | screen_swap_chains[p_screen] = swap_chain; |
| 4398 | |
| 4399 | return OK; |
| 4400 | } |
| 4401 | |
| 4402 | Error RenderingDevice::screen_prepare_for_drawing(DisplayServer::WindowID p_screen) { |
| 4403 | _THREAD_SAFE_METHOD_ |
| 4404 | |
| 4405 | // After submitting work, acquire the swapchain image(s). |
| 4406 | HashMap<DisplayServer::WindowID, RDD::SwapChainID>::ConstIterator it = screen_swap_chains.find(p_screen); |
| 4407 | ERR_FAIL_COND_V_MSG(it == screen_swap_chains.end(), ERR_CANT_CREATE, "A swap chain was not created for the screen."); |
| 4408 | |
| 4409 | // Erase the framebuffer corresponding to this screen from the map in case any of the operations fail. |
| 4410 | screen_framebuffers.erase(p_screen); |
| 4411 | |
| 4412 | // If this frame has already queued this swap chain for presentation, we present it and remove it from the pending list. |
| 4413 | uint32_t to_present_index = 0; |
| 4414 | while (to_present_index < frames[frame].swap_chains_to_present.size()) { |
| 4415 | if (frames[frame].swap_chains_to_present[to_present_index] == it->value) { |
| 4416 | driver->command_queue_execute_and_present(present_queue, {}, {}, {}, {}, it->value); |
| 4417 | frames[frame].swap_chains_to_present.remove_at(to_present_index); |
| 4418 | } else { |
| 4419 | to_present_index++; |
| 4420 | } |
| 4421 | } |
| 4422 | |
| 4423 | bool resize_required = false; |
| 4424 | RDD::FramebufferID framebuffer = driver->swap_chain_acquire_framebuffer(main_queue, it->value, resize_required); |
| 4425 | if (resize_required) { |
| 4426 | // Flush everything so nothing can be using the swap chain before resizing it. |
| 4427 | _flush_and_stall_for_all_frames(); |
| 4428 | |
| 4429 | Error err = driver->swap_chain_resize(main_queue, it->value, _get_swap_chain_desired_count()); |
| 4430 | if (err != OK) { |
| 4431 | // Resize is allowed to fail silently because the window can be minimized. |
| 4432 | return err; |
| 4433 | } |
| 4434 | |
| 4435 | framebuffer = driver->swap_chain_acquire_framebuffer(main_queue, it->value, resize_required); |
| 4436 | } |
| 4437 | |
| 4438 | if (framebuffer.id == 0) { |
| 4439 | // Some drivers like NVIDIA are fast enough to invalidate the swap chain between resizing and acquisition (GH-94104). |
| 4440 | // This typically occurs during continuous window resizing operations, especially if done quickly. |
| 4441 | // Allow this to fail silently since it has no visual consequences. |
| 4442 | return ERR_CANT_CREATE; |
| 4443 | } |
| 4444 | |
| 4445 | // Store the framebuffer that will be used next to draw to this screen. |
| 4446 | screen_framebuffers[p_screen] = framebuffer; |
| 4447 | frames[frame].swap_chains_to_present.push_back(it->value); |
| 4448 | |
| 4449 | return OK; |
| 4450 | } |
| 4451 | |
| 4452 | int RenderingDevice::screen_get_width(DisplayServer::WindowID p_screen) const { |
| 4453 | _THREAD_SAFE_METHOD_ |
| 4454 | |
| 4455 | RenderingContextDriver::SurfaceID surface = context->surface_get_from_window(p_screen); |
| 4456 | ERR_FAIL_COND_V_MSG(surface == 0, 0, "A surface was not created for the screen."); |
| 4457 | return context->surface_get_width(surface); |
| 4458 | } |
| 4459 | |
| 4460 | int RenderingDevice::screen_get_height(DisplayServer::WindowID p_screen) const { |
| 4461 | _THREAD_SAFE_METHOD_ |
| 4462 | |
| 4463 | RenderingContextDriver::SurfaceID surface = context->surface_get_from_window(p_screen); |
| 4464 | ERR_FAIL_COND_V_MSG(surface == 0, 0, "A surface was not created for the screen."); |
| 4465 | return context->surface_get_height(surface); |
| 4466 | } |
| 4467 | |
| 4468 | int RenderingDevice::screen_get_pre_rotation_degrees(DisplayServer::WindowID p_screen) const { |
| 4469 | _THREAD_SAFE_METHOD_ |
| 4470 | |
| 4471 | HashMap<DisplayServer::WindowID, RDD::SwapChainID>::ConstIterator it = screen_swap_chains.find(p_screen); |
| 4472 | ERR_FAIL_COND_V_MSG(it == screen_swap_chains.end(), ERR_CANT_CREATE, "A swap chain was not created for the screen."); |
| 4473 | |
| 4474 | return driver->swap_chain_get_pre_rotation_degrees(it->value); |
| 4475 | } |
| 4476 | |
| 4477 | RenderingDevice::FramebufferFormatID RenderingDevice::screen_get_framebuffer_format(DisplayServer::WindowID p_screen) const { |
| 4478 | _THREAD_SAFE_METHOD_ |
| 4479 | |
| 4480 | HashMap<DisplayServer::WindowID, RDD::SwapChainID>::ConstIterator it = screen_swap_chains.find(p_screen); |
| 4481 | ERR_FAIL_COND_V_MSG(it == screen_swap_chains.end(), INVALID_ID, "Screen was never prepared."); |
| 4482 | |
| 4483 | DataFormat format = driver->swap_chain_get_format(it->value); |
| 4484 | ERR_FAIL_COND_V(format == DATA_FORMAT_MAX, INVALID_ID); |
| 4485 | |
| 4486 | AttachmentFormat attachment; |
| 4487 | attachment.format = format; |
| 4488 | attachment.samples = TEXTURE_SAMPLES_1; |
| 4489 | attachment.usage_flags = TEXTURE_USAGE_COLOR_ATTACHMENT_BIT; |
| 4490 | Vector<AttachmentFormat> screen_attachment; |
| 4491 | screen_attachment.push_back(attachment); |
| 4492 | return const_cast<RenderingDevice *>(this)->framebuffer_format_create(screen_attachment); |
| 4493 | } |
| 4494 | |
| 4495 | Error RenderingDevice::screen_free(DisplayServer::WindowID p_screen) { |
| 4496 | _THREAD_SAFE_METHOD_ |
| 4497 | |
| 4498 | HashMap<DisplayServer::WindowID, RDD::SwapChainID>::ConstIterator it = screen_swap_chains.find(p_screen); |
| 4499 | ERR_FAIL_COND_V_MSG(it == screen_swap_chains.end(), FAILED, "Screen was never created."); |
| 4500 | |
| 4501 | // Flush everything so nothing can be using the swap chain before erasing it. |
| 4502 | _flush_and_stall_for_all_frames(); |
| 4503 | |
| 4504 | const DisplayServer::WindowID screen = it->key; |
| 4505 | const RDD::SwapChainID swap_chain = it->value; |
| 4506 | driver->swap_chain_free(swap_chain); |
| 4507 | screen_framebuffers.erase(screen); |
| 4508 | screen_swap_chains.erase(screen); |
| 4509 | |
| 4510 | return OK; |
| 4511 | } |
| 4512 | |
| 4513 | /*******************/ |
| 4514 | /**** DRAW LIST ****/ |
| 4515 | /*******************/ |
| 4516 | |
| 4517 | RenderingDevice::DrawListID RenderingDevice::draw_list_begin_for_screen(DisplayServer::WindowID p_screen, const Color &p_clear_color) { |
| 4518 | ERR_RENDER_THREAD_GUARD_V(INVALID_ID); |
| 4519 | |
| 4520 | ERR_FAIL_COND_V_MSG(draw_list.active, INVALID_ID, "Only one draw list can be active at the same time."); |
| 4521 | ERR_FAIL_COND_V_MSG(compute_list.active, INVALID_ID, "Only one draw/compute list can be active at the same time."); |
| 4522 | |
| 4523 | RenderingContextDriver::SurfaceID surface = context->surface_get_from_window(p_screen); |
| 4524 | HashMap<DisplayServer::WindowID, RDD::SwapChainID>::ConstIterator sc_it = screen_swap_chains.find(p_screen); |
| 4525 | HashMap<DisplayServer::WindowID, RDD::FramebufferID>::ConstIterator fb_it = screen_framebuffers.find(p_screen); |
| 4526 | ERR_FAIL_COND_V_MSG(surface == 0, 0, "A surface was not created for the screen."); |
| 4527 | ERR_FAIL_COND_V_MSG(sc_it == screen_swap_chains.end(), INVALID_ID, "Screen was never prepared."); |
| 4528 | ERR_FAIL_COND_V_MSG(fb_it == screen_framebuffers.end(), INVALID_ID, "Framebuffer was never prepared."); |
| 4529 | |
| 4530 | Rect2i viewport = Rect2i(0, 0, context->surface_get_width(surface), context->surface_get_height(surface)); |
| 4531 | |
| 4532 | _draw_list_start(viewport); |
| 4533 | #ifdef DEBUG_ENABLED |
| 4534 | draw_list_framebuffer_format = screen_get_framebuffer_format(p_screen); |
| 4535 | #endif |
| 4536 | draw_list_subpass_count = 1; |
| 4537 | |
| 4538 | RDD::RenderPassClearValue clear_value; |
| 4539 | clear_value.color = p_clear_color; |
| 4540 | |
| 4541 | RDD::RenderPassID render_pass = driver->swap_chain_get_render_pass(sc_it->value); |
| 4542 | draw_graph.add_draw_list_begin(render_pass, fb_it->value, viewport, RDG::ATTACHMENT_OPERATION_CLEAR, clear_value, RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, RDD::BreadcrumbMarker::BLIT_PASS, split_swapchain_into_its_own_cmd_buffer); |
| 4543 | |
| 4544 | draw_graph.add_draw_list_set_viewport(viewport); |
| 4545 | draw_graph.add_draw_list_set_scissor(viewport); |
| 4546 | |
| 4547 | return int64_t(ID_TYPE_DRAW_LIST) << ID_BASE_SHIFT; |
| 4548 | } |
| 4549 | |
| 4550 | RenderingDevice::DrawListID RenderingDevice::_draw_list_begin_bind(RID p_framebuffer, BitField<DrawFlags> p_draw_flags, const Vector<Color> &p_clear_color_values, float p_clear_depth_value, uint32_t p_clear_stencil_value, const Rect2 &p_region, uint32_t p_breadcrumb) { |
| 4551 | return draw_list_begin(p_framebuffer, p_draw_flags, p_clear_color_values, p_clear_depth_value, p_clear_stencil_value, p_region, p_breadcrumb); |
| 4552 | } |
| 4553 | |
| 4554 | RenderingDevice::DrawListID RenderingDevice::draw_list_begin(RID p_framebuffer, BitField<DrawFlags> p_draw_flags, VectorView<Color> p_clear_color_values, float p_clear_depth_value, uint32_t p_clear_stencil_value, const Rect2 &p_region, uint32_t p_breadcrumb) { |
| 4555 | ERR_RENDER_THREAD_GUARD_V(INVALID_ID); |
| 4556 | |
| 4557 | ERR_FAIL_COND_V_MSG(draw_list.active, INVALID_ID, "Only one draw list can be active at the same time."); |
| 4558 | |
| 4559 | Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_framebuffer); |
| 4560 | ERR_FAIL_NULL_V(framebuffer, INVALID_ID); |
| 4561 | |
| 4562 | const FramebufferFormatKey &framebuffer_key = framebuffer_formats[framebuffer->format_id].E->key(); |
| 4563 | Point2i viewport_offset; |
| 4564 | Point2i viewport_size = framebuffer->size; |
| 4565 | |
| 4566 | if (p_region != Rect2() && p_region != Rect2(Vector2(), viewport_size)) { // Check custom region. |
| 4567 | Rect2i viewport(viewport_offset, viewport_size); |
| 4568 | Rect2i regioni = p_region; |
| 4569 | if (!((regioni.position.x >= viewport.position.x) && (regioni.position.y >= viewport.position.y) && |
| 4570 | ((regioni.position.x + regioni.size.x) <= (viewport.position.x + viewport.size.x)) && |
| 4571 | ((regioni.position.y + regioni.size.y) <= (viewport.position.y + viewport.size.y)))) { |
| 4572 | ERR_FAIL_V_MSG(INVALID_ID, "When supplying a custom region, it must be contained within the framebuffer rectangle"); |
| 4573 | } |
| 4574 | |
| 4575 | viewport_offset = regioni.position; |
| 4576 | viewport_size = regioni.size; |
| 4577 | } |
| 4578 | |
| 4579 | thread_local LocalVector<RDG::AttachmentOperation> operations; |
| 4580 | thread_local LocalVector<RDD::RenderPassClearValue> clear_values; |
| 4581 | thread_local LocalVector<RDG::ResourceTracker *> resource_trackers; |
| 4582 | thread_local LocalVector<RDG::ResourceUsage> resource_usages; |
| 4583 | BitField<RDD::PipelineStageBits> stages = {}; |
| 4584 | operations.resize(framebuffer->texture_ids.size()); |
| 4585 | clear_values.resize(framebuffer->texture_ids.size()); |
| 4586 | resource_trackers.clear(); |
| 4587 | resource_usages.clear(); |
| 4588 | stages.clear(); |
| 4589 | |
| 4590 | uint32_t color_index = 0; |
| 4591 | for (int i = 0; i < framebuffer->texture_ids.size(); i++) { |
| 4592 | RID texture_rid = framebuffer->texture_ids[i]; |
| 4593 | Texture *texture = texture_owner.get_or_null(texture_rid); |
| 4594 | if (texture == nullptr) { |
| 4595 | operations[i] = RDG::ATTACHMENT_OPERATION_DEFAULT; |
| 4596 | clear_values[i] = RDD::RenderPassClearValue(); |
| 4597 | continue; |
| 4598 | } |
| 4599 | |
| 4600 | // Clear the texture if the driver requires it during its first use. |
| 4601 | _texture_check_pending_clear(texture_rid, texture); |
| 4602 | |
| 4603 | // Indicate the texture will get modified for the shared texture fallback. |
| 4604 | _texture_update_shared_fallback(texture_rid, texture, true); |
| 4605 | |
| 4606 | RDG::AttachmentOperation operation = RDG::ATTACHMENT_OPERATION_DEFAULT; |
| 4607 | RDD::RenderPassClearValue clear_value; |
| 4608 | if (framebuffer_key.vrs_attachment == i && (texture->usage_flags & TEXTURE_USAGE_VRS_ATTACHMENT_BIT)) { |
| 4609 | resource_trackers.push_back(texture->draw_tracker); |
| 4610 | resource_usages.push_back(_vrs_usage_from_method(framebuffer_key.vrs_method)); |
| 4611 | stages.set_flag(_vrs_stages_from_method(framebuffer_key.vrs_method)); |
| 4612 | } else if (texture->usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) { |
| 4613 | if (p_draw_flags.has_flag(DrawFlags(DRAW_CLEAR_COLOR_0 << color_index))) { |
| 4614 | ERR_FAIL_COND_V_MSG(color_index >= p_clear_color_values.size(), INVALID_ID, vformat("Color texture (%d) was specified to be cleared but no color value was provided.", color_index)); |
| 4615 | operation = RDG::ATTACHMENT_OPERATION_CLEAR; |
| 4616 | clear_value.color = p_clear_color_values[color_index]; |
| 4617 | } else if (p_draw_flags.has_flag(DrawFlags(DRAW_IGNORE_COLOR_0 << color_index))) { |
| 4618 | operation = RDG::ATTACHMENT_OPERATION_IGNORE; |
| 4619 | } |
| 4620 | |
| 4621 | resource_trackers.push_back(texture->draw_tracker); |
| 4622 | resource_usages.push_back(RDG::RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE); |
| 4623 | stages.set_flag(RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT); |
| 4624 | color_index++; |
| 4625 | } else if (texture->usage_flags & (TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT)) { |
| 4626 | if (p_draw_flags.has_flag(DRAW_CLEAR_DEPTH) || p_draw_flags.has_flag(DRAW_CLEAR_STENCIL)) { |
| 4627 | operation = RDG::ATTACHMENT_OPERATION_CLEAR; |
| 4628 | clear_value.depth = p_clear_depth_value; |
| 4629 | clear_value.stencil = p_clear_stencil_value; |
| 4630 | } else if (p_draw_flags.has_flag(DRAW_IGNORE_DEPTH) || p_draw_flags.has_flag(DRAW_IGNORE_STENCIL)) { |
| 4631 | operation = RDG::ATTACHMENT_OPERATION_IGNORE; |
| 4632 | } |
| 4633 | |
| 4634 | resource_trackers.push_back(texture->draw_tracker); |
| 4635 | resource_usages.push_back(RDG::RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE); |
| 4636 | stages.set_flag(RDD::PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT); |
| 4637 | stages.set_flag(RDD::PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT); |
| 4638 | } |
| 4639 | |
| 4640 | operations[i] = operation; |
| 4641 | clear_values[i] = clear_value; |
| 4642 | } |
| 4643 | |
| 4644 | draw_graph.add_draw_list_begin(framebuffer->framebuffer_cache, Rect2i(viewport_offset, viewport_size), operations, clear_values, stages, p_breadcrumb); |
| 4645 | draw_graph.add_draw_list_usages(resource_trackers, resource_usages); |
| 4646 | |
| 4647 | // Mark textures as bound. |
| 4648 | draw_list_bound_textures.clear(); |
| 4649 | |
| 4650 | for (int i = 0; i < framebuffer->texture_ids.size(); i++) { |
| 4651 | Texture *texture = texture_owner.get_or_null(framebuffer->texture_ids[i]); |
| 4652 | if (texture == nullptr) { |
| 4653 | continue; |
| 4654 | } |
| 4655 | |
| 4656 | texture->bound = true; |
| 4657 | draw_list_bound_textures.push_back(framebuffer->texture_ids[i]); |
| 4658 | } |
| 4659 | |
| 4660 | _draw_list_start(Rect2i(viewport_offset, viewport_size)); |
| 4661 | #ifdef DEBUG_ENABLED |
| 4662 | draw_list_framebuffer_format = framebuffer->format_id; |
| 4663 | #endif |
| 4664 | draw_list_current_subpass = 0; |
| 4665 | draw_list_subpass_count = framebuffer_key.passes.size(); |
| 4666 | |
| 4667 | Rect2i viewport_rect(viewport_offset, viewport_size); |
| 4668 | draw_graph.add_draw_list_set_viewport(viewport_rect); |
| 4669 | draw_graph.add_draw_list_set_scissor(viewport_rect); |
| 4670 | |
| 4671 | return int64_t(ID_TYPE_DRAW_LIST) << ID_BASE_SHIFT; |
| 4672 | } |
| 4673 | |
| 4674 | #ifndef DISABLE_DEPRECATED |
| 4675 | Error RenderingDevice::draw_list_begin_split(RID p_framebuffer, uint32_t p_splits, DrawListID *r_split_ids, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values, float p_clear_depth, uint32_t p_clear_stencil, const Rect2 &p_region, const Vector<RID> &p_storage_textures) { |
| 4676 | ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Deprecated. Split draw lists are used automatically by RenderingDevice."); |
| 4677 | } |
| 4678 | #endif |
| 4679 | |
| 4680 | void RenderingDevice::draw_list_set_blend_constants(DrawListID p_list, const Color &p_color) { |
| 4681 | ERR_RENDER_THREAD_GUARD(); |
| 4682 | |
| 4683 | ERR_FAIL_COND(!draw_list.active); |
| 4684 | |
| 4685 | draw_graph.add_draw_list_set_blend_constants(p_color); |
| 4686 | } |
| 4687 | |
| 4688 | void RenderingDevice::draw_list_bind_render_pipeline(DrawListID p_list, RID p_render_pipeline) { |
| 4689 | ERR_RENDER_THREAD_GUARD(); |
| 4690 | |
| 4691 | ERR_FAIL_COND(!draw_list.active); |
| 4692 | |
| 4693 | const RenderPipeline *pipeline = render_pipeline_owner.get_or_null(p_render_pipeline); |
| 4694 | ERR_FAIL_NULL(pipeline); |
| 4695 | #ifdef DEBUG_ENABLED |
| 4696 | ERR_FAIL_COND(pipeline->validation.framebuffer_format != draw_list_framebuffer_format && pipeline->validation.render_pass != draw_list_current_subpass); |
| 4697 | #endif |
| 4698 | |
| 4699 | if (p_render_pipeline == draw_list.state.pipeline) { |
| 4700 | return; // Redundant state, return. |
| 4701 | } |
| 4702 | |
| 4703 | draw_list.state.pipeline = p_render_pipeline; |
| 4704 | |
| 4705 | draw_graph.add_draw_list_bind_pipeline(pipeline->driver_id, pipeline->stage_bits); |
| 4706 | |
| 4707 | if (draw_list.state.pipeline_shader != pipeline->shader) { |
| 4708 | // Shader changed, so descriptor sets may become incompatible. |
| 4709 | |
| 4710 | uint32_t pcount = pipeline->set_formats.size(); // Formats count in this pipeline. |
| 4711 | draw_list.state.set_count = MAX(draw_list.state.set_count, pcount); |
| 4712 | const uint32_t *pformats = pipeline->set_formats.ptr(); // Pipeline set formats. |
| 4713 | |
| 4714 | uint32_t first_invalid_set = UINT32_MAX; // All valid by default. |
| 4715 | if (pipeline->push_constant_size != draw_list.state.pipeline_push_constant_size) { |
| 4716 | // All sets must be invalidated as the pipeline layout is not compatible if the push constant range is different. |
| 4717 | draw_list.state.pipeline_push_constant_size = pipeline->push_constant_size; |
| 4718 | first_invalid_set = 0; |
| 4719 | } else { |
| 4720 | switch (driver->api_trait_get(RDD::API_TRAIT_SHADER_CHANGE_INVALIDATION)) { |
| 4721 | case RDD::SHADER_CHANGE_INVALIDATION_ALL_BOUND_UNIFORM_SETS: { |
| 4722 | first_invalid_set = 0; |
| 4723 | } break; |
| 4724 | case RDD::SHADER_CHANGE_INVALIDATION_INCOMPATIBLE_SETS_PLUS_CASCADE: { |
| 4725 | for (uint32_t i = 0; i < pcount; i++) { |
| 4726 | if (draw_list.state.sets[i].pipeline_expected_format != pformats[i]) { |
| 4727 | first_invalid_set = i; |
| 4728 | break; |
| 4729 | } |
| 4730 | } |
| 4731 | } break; |
| 4732 | case RDD::SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH: { |
| 4733 | if (draw_list.state.pipeline_shader_layout_hash != pipeline->shader_layout_hash) { |
| 4734 | first_invalid_set = 0; |
| 4735 | } |
| 4736 | } break; |
| 4737 | } |
| 4738 | } |
| 4739 | |
| 4740 | if (pipeline->push_constant_size) { |
| 4741 | #ifdef DEBUG_ENABLED |
| 4742 | draw_list.validation.pipeline_push_constant_supplied = false; |
| 4743 | #endif |
| 4744 | } |
| 4745 | |
| 4746 | for (uint32_t i = 0; i < pcount; i++) { |
| 4747 | draw_list.state.sets[i].bound = draw_list.state.sets[i].bound && i < first_invalid_set; |
| 4748 | draw_list.state.sets[i].pipeline_expected_format = pformats[i]; |
| 4749 | } |
| 4750 | |
| 4751 | for (uint32_t i = pcount; i < draw_list.state.set_count; i++) { |
| 4752 | // Unbind the ones above (not used) if exist. |
| 4753 | draw_list.state.sets[i].bound = false; |
| 4754 | } |
| 4755 | |
| 4756 | draw_list.state.set_count = pcount; // Update set count. |
| 4757 | |
| 4758 | draw_list.state.pipeline_shader = pipeline->shader; |
| 4759 | draw_list.state.pipeline_shader_driver_id = pipeline->shader_driver_id; |
| 4760 | draw_list.state.pipeline_shader_layout_hash = pipeline->shader_layout_hash; |
| 4761 | } |
| 4762 | |
| 4763 | #ifdef DEBUG_ENABLED |
| 4764 | // Update render pass pipeline info. |
| 4765 | draw_list.validation.pipeline_active = true; |
| 4766 | draw_list.validation.pipeline_dynamic_state = pipeline->validation.dynamic_state; |
| 4767 | draw_list.validation.pipeline_vertex_format = pipeline->validation.vertex_format; |
| 4768 | draw_list.validation.pipeline_uses_restart_indices = pipeline->validation.uses_restart_indices; |
| 4769 | draw_list.validation.pipeline_primitive_divisor = pipeline->validation.primitive_divisor; |
| 4770 | draw_list.validation.pipeline_primitive_minimum = pipeline->validation.primitive_minimum; |
| 4771 | draw_list.validation.pipeline_push_constant_size = pipeline->push_constant_size; |
| 4772 | #endif |
| 4773 | } |
| 4774 | |
| 4775 | void RenderingDevice::draw_list_bind_uniform_set(DrawListID p_list, RID p_uniform_set, uint32_t p_index) { |
| 4776 | ERR_RENDER_THREAD_GUARD(); |
| 4777 | |
| 4778 | #ifdef DEBUG_ENABLED |
| 4779 | ERR_FAIL_COND_MSG(p_index >= driver->limit_get(LIMIT_MAX_BOUND_UNIFORM_SETS) || p_index >= MAX_UNIFORM_SETS, |
| 4780 | "Attempting to bind a descriptor set (" + itos(p_index) + ") greater than what the hardware supports (" + itos(driver->limit_get(LIMIT_MAX_BOUND_UNIFORM_SETS)) + ")."); |
| 4781 | #endif |
| 4782 | |
| 4783 | ERR_FAIL_COND(!draw_list.active); |
| 4784 | |
| 4785 | const UniformSet *uniform_set = uniform_set_owner.get_or_null(p_uniform_set); |
| 4786 | ERR_FAIL_NULL(uniform_set); |
| 4787 | |
| 4788 | if (p_index > draw_list.state.set_count) { |
| 4789 | draw_list.state.set_count = p_index; |
| 4790 | } |
| 4791 | |
| 4792 | draw_list.state.sets[p_index].uniform_set_driver_id = uniform_set->driver_id; // Update set pointer. |
| 4793 | draw_list.state.sets[p_index].bound = false; // Needs rebind. |
| 4794 | draw_list.state.sets[p_index].uniform_set_format = uniform_set->format; |
| 4795 | draw_list.state.sets[p_index].uniform_set = p_uniform_set; |
| 4796 | |
| 4797 | #ifdef DEBUG_ENABLED |
| 4798 | { // Validate that textures bound are not attached as framebuffer bindings. |
| 4799 | uint32_t attachable_count = uniform_set->attachable_textures.size(); |
| 4800 | const UniformSet::AttachableTexture *attachable_ptr = uniform_set->attachable_textures.ptr(); |
| 4801 | uint32_t bound_count = draw_list_bound_textures.size(); |
| 4802 | const RID *bound_ptr = draw_list_bound_textures.ptr(); |
| 4803 | for (uint32_t i = 0; i < attachable_count; i++) { |
| 4804 | for (uint32_t j = 0; j < bound_count; j++) { |
| 4805 | ERR_FAIL_COND_MSG(attachable_ptr[i].texture == bound_ptr[j], |
| 4806 | "Attempted to use the same texture in framebuffer attachment and a uniform (set: " + itos(p_index) + ", binding: " + itos(attachable_ptr[i].bind) + "), this is not allowed."); |
| 4807 | } |
| 4808 | } |
| 4809 | } |
| 4810 | #endif |
| 4811 | } |
| 4812 | |
| 4813 | void RenderingDevice::draw_list_bind_vertex_array(DrawListID p_list, RID p_vertex_array) { |
| 4814 | ERR_RENDER_THREAD_GUARD(); |
| 4815 | |
| 4816 | ERR_FAIL_COND(!draw_list.active); |
| 4817 | |
| 4818 | VertexArray *vertex_array = vertex_array_owner.get_or_null(p_vertex_array); |
| 4819 | ERR_FAIL_NULL(vertex_array); |
| 4820 | |
| 4821 | if (draw_list.state.vertex_array == p_vertex_array) { |
| 4822 | return; // Already set. |
| 4823 | } |
| 4824 | |
| 4825 | _check_transfer_worker_vertex_array(vertex_array); |
| 4826 | |
| 4827 | draw_list.state.vertex_array = p_vertex_array; |
| 4828 | |
| 4829 | #ifdef DEBUG_ENABLED |
| 4830 | draw_list.validation.vertex_format = vertex_array->description; |
| 4831 | draw_list.validation.vertex_max_instances_allowed = vertex_array->max_instances_allowed; |
| 4832 | #endif |
| 4833 | draw_list.validation.vertex_array_size = vertex_array->vertex_count; |
| 4834 | |
| 4835 | draw_graph.add_draw_list_bind_vertex_buffers(vertex_array->buffers, vertex_array->offsets); |
| 4836 | |
| 4837 | for (int i = 0; i < vertex_array->draw_trackers.size(); i++) { |
| 4838 | draw_graph.add_draw_list_usage(vertex_array->draw_trackers[i], RDG::RESOURCE_USAGE_VERTEX_BUFFER_READ); |
| 4839 | } |
| 4840 | } |
| 4841 | |
| 4842 | void RenderingDevice::draw_list_bind_vertex_buffers_format(DrawListID p_list, VertexFormatID p_vertex_format, uint32_t p_vertex_count, const Span<RID> &p_vertex_buffers, const Span<uint64_t> &p_offsets) { |
| 4843 | ERR_RENDER_THREAD_GUARD(); |
| 4844 | |
| 4845 | ERR_FAIL_COND(!draw_list.active); |
| 4846 | |
| 4847 | const VertexDescriptionCache *vertex_description = vertex_formats.getptr(p_vertex_format); |
| 4848 | ERR_FAIL_NULL_MSG(vertex_description, "Supplied vertex format does not exist."); |
| 4849 | |
| 4850 | Span<uint64_t> offsets_span = p_offsets; |
| 4851 | FixedVector<uint64_t, 32> offsets; |
| 4852 | if (offsets_span.is_empty()) { |
| 4853 | offsets.resize_initialized(p_vertex_buffers.size()); |
| 4854 | offsets_span = offsets; |
| 4855 | } else { |
| 4856 | ERR_FAIL_COND_MSG(offsets_span.size() != p_vertex_buffers.size(), |
| 4857 | "Number of vertex buffer offsets (" + itos(offsets_span.size()) + ") does not match number of vertex buffers (" + itos(p_vertex_buffers.size()) + ")."); |
| 4858 | } |
| 4859 | |
| 4860 | FixedVector<RDD::BufferID, 32> driver_buffers; |
| 4861 | driver_buffers.resize_initialized(p_vertex_buffers.size()); |
| 4862 | |
| 4863 | FixedVector<RDG::ResourceTracker *, 32> draw_trackers; |
| 4864 | |
| 4865 | #if DEBUG_ENABLED |
| 4866 | uint32_t max_instances_allowed = 0xFFFFFFFF; |
| 4867 | #endif |
| 4868 | |
| 4869 | for (uint32_t i = 0; i < p_vertex_buffers.size(); i++) { |
| 4870 | RID buffer_rid = p_vertex_buffers[i]; |
| 4871 | if (buffer_rid.is_null()) { |
| 4872 | // The buffer array can be sparse. |
| 4873 | continue; |
| 4874 | } |
| 4875 | ERR_FAIL_COND_MSG(!vertex_buffer_owner.owns(buffer_rid), "Vertex buffer at index " + itos(i) + " is invalid."); |
| 4876 | |
| 4877 | Buffer *buffer = vertex_buffer_owner.get_or_null(buffer_rid); |
| 4878 | ERR_FAIL_NULL(buffer); |
| 4879 | |
| 4880 | _check_transfer_worker_buffer(buffer); |
| 4881 | |
| 4882 | #if DEBUG_ENABLED |
| 4883 | uint64_t binding_offset = offsets_span[i]; |
| 4884 | ERR_FAIL_COND_MSG(binding_offset > buffer->size, "Vertex buffer offset for attachment (" + itos(i) + ") exceeds buffer size."); |
| 4885 | |
| 4886 | const VertexAttribute &attribute = vertex_description->vertex_formats[i]; |
| 4887 | uint32_t element_size = get_format_vertex_size(attribute.format); |
| 4888 | ERR_FAIL_COND_MSG(element_size == 0, "Vertex attribute format for attachment (" + itos(i) + ") is invalid."); |
| 4889 | |
| 4890 | uint64_t attribute_offset = binding_offset + attribute.offset; |
| 4891 | ERR_FAIL_COND_MSG(attribute_offset > buffer->size, "Vertex attribute offset for attachment (" + itos(i) + ") exceeds buffer size."); |
| 4892 | ERR_FAIL_COND_MSG(attribute_offset + element_size > buffer->size, |
| 4893 | "Vertex buffer (" + itos(i) + ") will read past the end of the buffer."); |
| 4894 | |
| 4895 | if (attribute.frequency == VERTEX_FREQUENCY_VERTEX) { |
| 4896 | ERR_FAIL_COND_MSG(p_vertex_count == 0, "Vertex count must be greater than 0 when binding vertex buffers."); |
| 4897 | |
| 4898 | uint64_t required_size = attribute_offset + element_size; |
| 4899 | if (p_vertex_count > 1) { |
| 4900 | required_size += uint64_t(attribute.stride) * (uint64_t(p_vertex_count) - 1); |
| 4901 | } |
| 4902 | |
| 4903 | ERR_FAIL_COND_MSG(required_size > buffer->size, |
| 4904 | "Vertex buffer (" + itos(i) + ") will read past the end of the buffer."); |
| 4905 | } else { |
| 4906 | uint64_t available = buffer->size - attribute_offset; |
| 4907 | ERR_FAIL_COND_MSG(available < element_size, |
| 4908 | "Vertex buffer (" + itos(i) + ") uses instancing, but it's just too small."); |
| 4909 | |
| 4910 | uint32_t instances_allowed = attribute.stride == 0 ? 0 : uint32_t(buffer->size / attribute.stride); |
| 4911 | max_instances_allowed = MIN(instances_allowed, max_instances_allowed); |
| 4912 | } |
| 4913 | #endif |
| 4914 | |
| 4915 | driver_buffers[i] = buffer->driver_id; |
| 4916 | |
| 4917 | if (buffer->draw_tracker != nullptr) { |
| 4918 | draw_trackers.push_back(buffer->draw_tracker); |
| 4919 | } |
| 4920 | } |
| 4921 | |
| 4922 | draw_list.state.vertex_array = RID(); |
| 4923 | |
| 4924 | draw_graph.add_draw_list_bind_vertex_buffers(driver_buffers, offsets_span); |
| 4925 | |
| 4926 | for (RDG::ResourceTracker *tracker : draw_trackers) { |
| 4927 | draw_graph.add_draw_list_usage(tracker, RDG::RESOURCE_USAGE_VERTEX_BUFFER_READ); |
| 4928 | } |
| 4929 | |
| 4930 | draw_list.validation.vertex_array_size = p_vertex_count; |
| 4931 | |
| 4932 | #ifdef DEBUG_ENABLED |
| 4933 | draw_list.validation.vertex_format = p_vertex_format; |
| 4934 | draw_list.validation.vertex_max_instances_allowed = max_instances_allowed; |
| 4935 | #endif |
| 4936 | } |
| 4937 | |
| 4938 | void RenderingDevice::draw_list_bind_index_array(DrawListID p_list, RID p_index_array) { |
| 4939 | ERR_RENDER_THREAD_GUARD(); |
| 4940 | |
| 4941 | ERR_FAIL_COND(!draw_list.active); |
| 4942 | |
| 4943 | IndexArray *index_array = index_array_owner.get_or_null(p_index_array); |
| 4944 | ERR_FAIL_NULL(index_array); |
| 4945 | |
| 4946 | if (draw_list.state.index_array == p_index_array) { |
| 4947 | return; // Already set. |
| 4948 | } |
| 4949 | |
| 4950 | _check_transfer_worker_index_array(index_array); |
| 4951 | |
| 4952 | draw_list.state.index_array = p_index_array; |
| 4953 | #ifdef DEBUG_ENABLED |
| 4954 | draw_list.validation.index_array_max_index = index_array->max_index; |
| 4955 | #endif |
| 4956 | draw_list.validation.index_array_count = index_array->indices; |
| 4957 | |
| 4958 | const uint64_t offset_bytes = index_array->offset * (index_array->format == INDEX_BUFFER_FORMAT_UINT16 ? sizeof(uint16_t) : sizeof(uint32_t)); |
| 4959 | draw_graph.add_draw_list_bind_index_buffer(index_array->driver_id, index_array->format, offset_bytes); |
| 4960 | |
| 4961 | if (index_array->draw_tracker != nullptr) { |
| 4962 | draw_graph.add_draw_list_usage(index_array->draw_tracker, RDG::RESOURCE_USAGE_INDEX_BUFFER_READ); |
| 4963 | } |
| 4964 | } |
| 4965 | |
| 4966 | void RenderingDevice::draw_list_set_line_width(DrawListID p_list, float p_width) { |
| 4967 | ERR_RENDER_THREAD_GUARD(); |
| 4968 | |
| 4969 | ERR_FAIL_COND(!draw_list.active); |
| 4970 | |
| 4971 | draw_graph.add_draw_list_set_line_width(p_width); |
| 4972 | } |
| 4973 | |
| 4974 | void RenderingDevice::draw_list_set_push_constant(DrawListID p_list, const void *p_data, uint32_t p_data_size) { |
| 4975 | ERR_RENDER_THREAD_GUARD(); |
| 4976 | |
| 4977 | ERR_FAIL_COND(!draw_list.active); |
| 4978 | |
| 4979 | #ifdef DEBUG_ENABLED |
| 4980 | ERR_FAIL_COND_MSG(p_data_size != draw_list.validation.pipeline_push_constant_size, |
| 4981 | "This render pipeline requires (" + itos(draw_list.validation.pipeline_push_constant_size) + ") bytes of push constant data, supplied: (" + itos(p_data_size) + ")"); |
| 4982 | #endif |
| 4983 | |
| 4984 | draw_graph.add_draw_list_set_push_constant(draw_list.state.pipeline_shader_driver_id, p_data, p_data_size); |
| 4985 | |
| 4986 | #ifdef DEBUG_ENABLED |
| 4987 | draw_list.validation.pipeline_push_constant_supplied = true; |
| 4988 | #endif |
| 4989 | } |
| 4990 | |
| 4991 | void RenderingDevice::draw_list_draw(DrawListID p_list, bool p_use_indices, uint32_t p_instances, uint32_t p_procedural_vertices) { |
| 4992 | ERR_RENDER_THREAD_GUARD(); |
| 4993 | |
| 4994 | ERR_FAIL_COND(!draw_list.active); |
| 4995 | |
| 4996 | #ifdef DEBUG_ENABLED |
| 4997 | ERR_FAIL_COND_MSG(!draw_list.validation.pipeline_active, |
| 4998 | "No render pipeline was set before attempting to draw."); |
| 4999 | if (draw_list.validation.pipeline_vertex_format != INVALID_ID) { |
| 5000 | // Pipeline uses vertices, validate format. |
| 5001 | ERR_FAIL_COND_MSG(draw_list.validation.vertex_format == INVALID_ID, |
| 5002 | "No vertex array was bound, and render pipeline expects vertices."); |
| 5003 | // Make sure format is right. |
| 5004 | ERR_FAIL_COND_MSG(draw_list.validation.pipeline_vertex_format != draw_list.validation.vertex_format, |
| 5005 | "The vertex format used to create the pipeline does not match the vertex format bound."); |
| 5006 | // Make sure number of instances is valid. |
| 5007 | ERR_FAIL_COND_MSG(p_instances > draw_list.validation.vertex_max_instances_allowed, |
| 5008 | "Number of instances requested (" + itos(p_instances) + " is larger than the maximum number supported by the bound vertex array (" + itos(draw_list.validation.vertex_max_instances_allowed) + ")."); |
| 5009 | } |
| 5010 | |
| 5011 | if (draw_list.validation.pipeline_push_constant_size > 0) { |
| 5012 | // Using push constants, check that they were supplied. |
| 5013 | ERR_FAIL_COND_MSG(!draw_list.validation.pipeline_push_constant_supplied, |
| 5014 | "The shader in this pipeline requires a push constant to be set before drawing, but it's not present."); |
| 5015 | } |
| 5016 | |
| 5017 | #endif |
| 5018 | |
| 5019 | #ifdef DEBUG_ENABLED |
| 5020 | for (uint32_t i = 0; i < draw_list.state.set_count; i++) { |
| 5021 | if (draw_list.state.sets[i].pipeline_expected_format == 0) { |
| 5022 | // Nothing expected by this pipeline. |
| 5023 | continue; |
| 5024 | } |
| 5025 | |
| 5026 | if (draw_list.state.sets[i].pipeline_expected_format != draw_list.state.sets[i].uniform_set_format) { |
| 5027 | if (draw_list.state.sets[i].uniform_set_format == 0) { |
| 5028 | ERR_FAIL_MSG(vformat("Uniforms were never supplied for set (%d) at the time of drawing, which are required by the pipeline.", i)); |
| 5029 | } else if (uniform_set_owner.owns(draw_list.state.sets[i].uniform_set)) { |
| 5030 | UniformSet *us = uniform_set_owner.get_or_null(draw_list.state.sets[i].uniform_set); |
| 5031 | const String us_info = us ? vformat("(%d):\n%s\n", i, _shader_uniform_debug(us->shader_id, us->shader_set)) : vformat("(%d, which was just freed) ", i); |
| 5032 | ERR_FAIL_MSG(vformat("Uniforms supplied for set %sare not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n%s", us_info, _shader_uniform_debug(draw_list.state.pipeline_shader))); |
| 5033 | } else { |
| 5034 | ERR_FAIL_MSG(vformat("Uniforms supplied for set (%d, which was just freed) are not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n%s", i, _shader_uniform_debug(draw_list.state.pipeline_shader))); |
| 5035 | } |
| 5036 | } |
| 5037 | } |
| 5038 | #endif |
| 5039 | thread_local LocalVector<RDD::UniformSetID> valid_descriptor_ids; |
| 5040 | valid_descriptor_ids.clear(); |
| 5041 | valid_descriptor_ids.resize(draw_list.state.set_count); |
| 5042 | uint32_t valid_set_count = 0; |
| 5043 | uint32_t first_set_index = 0; |
| 5044 | uint32_t last_set_index = 0; |
| 5045 | bool found_first_set = false; |
| 5046 | |
| 5047 | for (uint32_t i = 0; i < draw_list.state.set_count; i++) { |
| 5048 | if (draw_list.state.sets[i].pipeline_expected_format == 0) { |
| 5049 | continue; // Nothing expected by this pipeline. |
| 5050 | } |
| 5051 | |
| 5052 | if (!draw_list.state.sets[i].bound && !found_first_set) { |
| 5053 | first_set_index = i; |
| 5054 | found_first_set = true; |
| 5055 | } |
| 5056 | // Prepare descriptor sets if the API doesn't use pipeline barriers. |
| 5057 | if (!driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) { |
| 5058 | draw_graph.add_draw_list_uniform_set_prepare_for_use(draw_list.state.pipeline_shader_driver_id, draw_list.state.sets[i].uniform_set_driver_id, i); |
| 5059 | } |
| 5060 | } |
| 5061 | |
| 5062 | // Bind descriptor sets. |
| 5063 | for (uint32_t i = first_set_index; i < draw_list.state.set_count; i++) { |
| 5064 | if (draw_list.state.sets[i].pipeline_expected_format == 0) { |
| 5065 | continue; // Nothing expected by this pipeline. |
| 5066 | } |
| 5067 | |
| 5068 | if (!draw_list.state.sets[i].bound) { |
| 5069 | // Batch contiguous descriptor sets in a single call. |
| 5070 | if (descriptor_set_batching) { |
| 5071 | // All good, see if this requires re-binding. |
| 5072 | if (i - last_set_index > 1) { |
| 5073 | // If the descriptor sets are not contiguous, bind the previous ones and start a new batch. |
| 5074 | draw_graph.add_draw_list_bind_uniform_sets(draw_list.state.pipeline_shader_driver_id, valid_descriptor_ids, first_set_index, valid_set_count); |
| 5075 | |
| 5076 | first_set_index = i; |
| 5077 | valid_set_count = 1; |
| 5078 | valid_descriptor_ids[0] = draw_list.state.sets[i].uniform_set_driver_id; |
| 5079 | } else { |
| 5080 | // Otherwise, keep storing in the current batch. |
| 5081 | valid_descriptor_ids[valid_set_count] = draw_list.state.sets[i].uniform_set_driver_id; |
| 5082 | valid_set_count++; |
| 5083 | } |
| 5084 | |
| 5085 | UniformSet *uniform_set = uniform_set_owner.get_or_null(draw_list.state.sets[i].uniform_set); |
| 5086 | ERR_FAIL_NULL(uniform_set); |
| 5087 | _uniform_set_update_shared(uniform_set); |
| 5088 | _uniform_set_update_clears(uniform_set); |
| 5089 | |
| 5090 | draw_graph.add_draw_list_usages(uniform_set->draw_trackers, uniform_set->draw_trackers_usage); |
| 5091 | draw_list.state.sets[i].bound = true; |
| 5092 | |
| 5093 | last_set_index = i; |
| 5094 | } else { |
| 5095 | draw_graph.add_draw_list_bind_uniform_set(draw_list.state.pipeline_shader_driver_id, draw_list.state.sets[i].uniform_set_driver_id, i); |
| 5096 | } |
| 5097 | } |
| 5098 | } |
| 5099 | |
| 5100 | // Bind the remaining batch. |
| 5101 | if (descriptor_set_batching && valid_set_count > 0) { |
| 5102 | draw_graph.add_draw_list_bind_uniform_sets(draw_list.state.pipeline_shader_driver_id, valid_descriptor_ids, first_set_index, valid_set_count); |
| 5103 | } |
| 5104 | |
| 5105 | if (p_use_indices) { |
| 5106 | #ifdef DEBUG_ENABLED |
| 5107 | ERR_FAIL_COND_MSG(p_procedural_vertices > 0, |
| 5108 | "Procedural vertices can't be used together with indices."); |
| 5109 | |
| 5110 | ERR_FAIL_COND_MSG(!draw_list.validation.index_array_count, |
| 5111 | "Draw command requested indices, but no index buffer was set."); |
| 5112 | |
| 5113 | ERR_FAIL_COND_MSG(draw_list.validation.pipeline_uses_restart_indices != draw_list.validation.index_buffer_uses_restart_indices, |
| 5114 | "The usage of restart indices in index buffer does not match the render primitive in the pipeline."); |
| 5115 | #endif |
| 5116 | uint32_t to_draw = draw_list.validation.index_array_count; |
| 5117 | |
| 5118 | #ifdef DEBUG_ENABLED |
| 5119 | ERR_FAIL_COND_MSG(to_draw < draw_list.validation.pipeline_primitive_minimum, |
| 5120 | "Too few indices (" + itos(to_draw) + ") for the render primitive set in the render pipeline (" + itos(draw_list.validation.pipeline_primitive_minimum) + ")."); |
| 5121 | |
| 5122 | ERR_FAIL_COND_MSG((to_draw % draw_list.validation.pipeline_primitive_divisor) != 0, |
| 5123 | "Index amount (" + itos(to_draw) + ") must be a multiple of the amount of indices required by the render primitive (" + itos(draw_list.validation.pipeline_primitive_divisor) + ")."); |
| 5124 | #endif |
| 5125 | |
| 5126 | draw_graph.add_draw_list_draw_indexed(to_draw, p_instances, 0); |
| 5127 | } else { |
| 5128 | uint32_t to_draw; |
| 5129 | |
| 5130 | if (p_procedural_vertices > 0) { |
| 5131 | to_draw = p_procedural_vertices; |
| 5132 | } else { |
| 5133 | #ifdef DEBUG_ENABLED |
| 5134 | ERR_FAIL_COND_MSG(draw_list.validation.pipeline_vertex_format == INVALID_ID, |
| 5135 | "Draw command lacks indices, but pipeline format does not use vertices."); |
| 5136 | #endif |
| 5137 | to_draw = draw_list.validation.vertex_array_size; |
| 5138 | } |
| 5139 | |
| 5140 | #ifdef DEBUG_ENABLED |
| 5141 | ERR_FAIL_COND_MSG(to_draw < draw_list.validation.pipeline_primitive_minimum, |
| 5142 | "Too few vertices (" + itos(to_draw) + ") for the render primitive set in the render pipeline (" + itos(draw_list.validation.pipeline_primitive_minimum) + ")."); |
| 5143 | |
| 5144 | ERR_FAIL_COND_MSG((to_draw % draw_list.validation.pipeline_primitive_divisor) != 0, |
| 5145 | "Vertex amount (" + itos(to_draw) + ") must be a multiple of the amount of vertices required by the render primitive (" + itos(draw_list.validation.pipeline_primitive_divisor) + ")."); |
| 5146 | #endif |
| 5147 | |
| 5148 | draw_graph.add_draw_list_draw(to_draw, p_instances); |
| 5149 | } |
| 5150 | |
| 5151 | draw_list.state.draw_count++; |
| 5152 | } |
| 5153 | |
| 5154 | void RenderingDevice::draw_list_draw_indirect(DrawListID p_list, bool p_use_indices, RID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) { |
| 5155 | ERR_RENDER_THREAD_GUARD(); |
| 5156 | |
| 5157 | ERR_FAIL_COND(!draw_list.active); |
| 5158 | |
| 5159 | Buffer *buffer = storage_buffer_owner.get_or_null(p_buffer); |
| 5160 | ERR_FAIL_NULL(buffer); |
| 5161 | |
| 5162 | ERR_FAIL_COND_MSG(!buffer->usage.has_flag(RDD::BUFFER_USAGE_INDIRECT_BIT), "Buffer provided was not created to do indirect dispatch."); |
| 5163 | |
| 5164 | #ifdef DEBUG_ENABLED |
| 5165 | ERR_FAIL_COND_MSG(!draw_list.validation.pipeline_active, |
| 5166 | "No render pipeline was set before attempting to draw."); |
| 5167 | if (draw_list.validation.pipeline_vertex_format != INVALID_ID) { |
| 5168 | // Pipeline uses vertices, validate format. |
| 5169 | ERR_FAIL_COND_MSG(draw_list.validation.vertex_format == INVALID_ID, |
| 5170 | "No vertex array was bound, and render pipeline expects vertices."); |
| 5171 | // Make sure format is right. |
| 5172 | ERR_FAIL_COND_MSG(draw_list.validation.pipeline_vertex_format != draw_list.validation.vertex_format, |
| 5173 | "The vertex format used to create the pipeline does not match the vertex format bound."); |
| 5174 | } |
| 5175 | |
| 5176 | if (draw_list.validation.pipeline_push_constant_size > 0) { |
| 5177 | // Using push constants, check that they were supplied. |
| 5178 | ERR_FAIL_COND_MSG(!draw_list.validation.pipeline_push_constant_supplied, |
| 5179 | "The shader in this pipeline requires a push constant to be set before drawing, but it's not present."); |
| 5180 | } |
| 5181 | #endif |
| 5182 | |
| 5183 | #ifdef DEBUG_ENABLED |
| 5184 | for (uint32_t i = 0; i < draw_list.state.set_count; i++) { |
| 5185 | if (draw_list.state.sets[i].pipeline_expected_format == 0) { |
| 5186 | // Nothing expected by this pipeline. |
| 5187 | continue; |
| 5188 | } |
| 5189 | |
| 5190 | if (draw_list.state.sets[i].pipeline_expected_format != draw_list.state.sets[i].uniform_set_format) { |
| 5191 | if (draw_list.state.sets[i].uniform_set_format == 0) { |
| 5192 | ERR_FAIL_MSG(vformat("Uniforms were never supplied for set (%d) at the time of drawing, which are required by the pipeline.", i)); |
| 5193 | } else if (uniform_set_owner.owns(draw_list.state.sets[i].uniform_set)) { |
| 5194 | UniformSet *us = uniform_set_owner.get_or_null(draw_list.state.sets[i].uniform_set); |
| 5195 | const String us_info = us ? vformat("(%d):\n%s\n", i, _shader_uniform_debug(us->shader_id, us->shader_set)) : vformat("(%d, which was just freed) ", i); |
| 5196 | ERR_FAIL_MSG(vformat("Uniforms supplied for set %sare not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n%s", us_info, _shader_uniform_debug(draw_list.state.pipeline_shader))); |
| 5197 | } else { |
| 5198 | ERR_FAIL_MSG(vformat("Uniforms supplied for set (%d, which was just freed) are not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n%s", i, _shader_uniform_debug(draw_list.state.pipeline_shader))); |
| 5199 | } |
| 5200 | } |
| 5201 | } |
| 5202 | #endif |
| 5203 | |
| 5204 | // Prepare descriptor sets if the API doesn't use pipeline barriers. |
| 5205 | if (!driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) { |
| 5206 | for (uint32_t i = 0; i < draw_list.state.set_count; i++) { |
| 5207 | if (draw_list.state.sets[i].pipeline_expected_format == 0) { |
| 5208 | // Nothing expected by this pipeline. |
| 5209 | continue; |
| 5210 | } |
| 5211 | |
| 5212 | draw_graph.add_draw_list_uniform_set_prepare_for_use(draw_list.state.pipeline_shader_driver_id, draw_list.state.sets[i].uniform_set_driver_id, i); |
| 5213 | } |
| 5214 | } |
| 5215 | |
| 5216 | // Bind descriptor sets. |
| 5217 | for (uint32_t i = 0; i < draw_list.state.set_count; i++) { |
| 5218 | if (draw_list.state.sets[i].pipeline_expected_format == 0) { |
| 5219 | continue; // Nothing expected by this pipeline. |
| 5220 | } |
| 5221 | if (!draw_list.state.sets[i].bound) { |
| 5222 | // All good, see if this requires re-binding. |
| 5223 | draw_graph.add_draw_list_bind_uniform_set(draw_list.state.pipeline_shader_driver_id, draw_list.state.sets[i].uniform_set_driver_id, i); |
| 5224 | |
| 5225 | UniformSet *uniform_set = uniform_set_owner.get_or_null(draw_list.state.sets[i].uniform_set); |
| 5226 | ERR_FAIL_NULL(uniform_set); |
| 5227 | _uniform_set_update_shared(uniform_set); |
| 5228 | _uniform_set_update_clears(uniform_set); |
| 5229 | |
| 5230 | draw_graph.add_draw_list_usages(uniform_set->draw_trackers, uniform_set->draw_trackers_usage); |
| 5231 | |
| 5232 | draw_list.state.sets[i].bound = true; |
| 5233 | } |
| 5234 | } |
| 5235 | |
| 5236 | if (p_use_indices) { |
| 5237 | #ifdef DEBUG_ENABLED |
| 5238 | ERR_FAIL_COND_MSG(!draw_list.validation.index_array_count, |
| 5239 | "Draw command requested indices, but no index buffer was set."); |
| 5240 | |
| 5241 | ERR_FAIL_COND_MSG(draw_list.validation.pipeline_uses_restart_indices != draw_list.validation.index_buffer_uses_restart_indices, |
| 5242 | "The usage of restart indices in index buffer does not match the render primitive in the pipeline."); |
| 5243 | #endif |
| 5244 | |
| 5245 | ERR_FAIL_COND_MSG(p_offset + 20 > buffer->size, "Offset provided (+20) is past the end of buffer."); |
| 5246 | |
| 5247 | draw_graph.add_draw_list_draw_indexed_indirect(buffer->driver_id, p_offset, p_draw_count, p_stride); |
| 5248 | } else { |
| 5249 | ERR_FAIL_COND_MSG(p_offset + 16 > buffer->size, "Offset provided (+16) is past the end of buffer."); |
| 5250 | |
| 5251 | draw_graph.add_draw_list_draw_indirect(buffer->driver_id, p_offset, p_draw_count, p_stride); |
| 5252 | } |
| 5253 | |
| 5254 | draw_list.state.draw_count++; |
| 5255 | |
| 5256 | if (buffer->draw_tracker != nullptr) { |
| 5257 | draw_graph.add_draw_list_usage(buffer->draw_tracker, RDG::RESOURCE_USAGE_INDIRECT_BUFFER_READ); |
| 5258 | } |
| 5259 | |
| 5260 | _check_transfer_worker_buffer(buffer); |
| 5261 | } |
| 5262 | |
| 5263 | void RenderingDevice::draw_list_set_viewport(DrawListID p_list, const Rect2 &p_rect) { |
| 5264 | ERR_FAIL_COND(!draw_list.active); |
| 5265 | |
| 5266 | if (p_rect.get_area() == 0) { |
| 5267 | return; |
| 5268 | } |
| 5269 | |
| 5270 | draw_list.viewport = p_rect; |
| 5271 | draw_graph.add_draw_list_set_viewport(p_rect); |
| 5272 | } |
| 5273 | |
| 5274 | void RenderingDevice::draw_list_enable_scissor(DrawListID p_list, const Rect2 &p_rect) { |
| 5275 | ERR_RENDER_THREAD_GUARD(); |
| 5276 | |
| 5277 | ERR_FAIL_COND(!draw_list.active); |
| 5278 | |
| 5279 | Rect2i rect = p_rect; |
| 5280 | rect.position += draw_list.viewport.position; |
| 5281 | |
| 5282 | rect = draw_list.viewport.intersection(rect); |
| 5283 | |
| 5284 | if (rect.get_area() == 0) { |
| 5285 | return; |
| 5286 | } |
| 5287 | |
| 5288 | draw_graph.add_draw_list_set_scissor(rect); |
| 5289 | } |
| 5290 | |
| 5291 | void RenderingDevice::draw_list_disable_scissor(DrawListID p_list) { |
| 5292 | ERR_RENDER_THREAD_GUARD(); |
| 5293 | |
| 5294 | ERR_FAIL_COND(!draw_list.active); |
| 5295 | |
| 5296 | draw_graph.add_draw_list_set_scissor(draw_list.viewport); |
| 5297 | } |
| 5298 | |
| 5299 | uint32_t RenderingDevice::draw_list_get_current_pass() { |
| 5300 | ERR_RENDER_THREAD_GUARD_V(0); |
| 5301 | |
| 5302 | return draw_list_current_subpass; |
| 5303 | } |
| 5304 | |
| 5305 | RenderingDevice::DrawListID RenderingDevice::draw_list_switch_to_next_pass() { |
| 5306 | ERR_RENDER_THREAD_GUARD_V(INVALID_ID); |
| 5307 | |
| 5308 | ERR_FAIL_COND_V(!draw_list.active, INVALID_FORMAT_ID); |
| 5309 | ERR_FAIL_COND_V(draw_list_current_subpass >= draw_list_subpass_count - 1, INVALID_FORMAT_ID); |
| 5310 | |
| 5311 | draw_list_current_subpass++; |
| 5312 | |
| 5313 | Rect2i viewport; |
| 5314 | _draw_list_end(&viewport); |
| 5315 | |
| 5316 | draw_graph.add_draw_list_next_subpass(RDD::COMMAND_BUFFER_TYPE_PRIMARY); |
| 5317 | |
| 5318 | _draw_list_start(viewport); |
| 5319 | |
| 5320 | return int64_t(ID_TYPE_DRAW_LIST) << ID_BASE_SHIFT; |
| 5321 | } |
| 5322 | |
| 5323 | #ifndef DISABLE_DEPRECATED |
| 5324 | Error RenderingDevice::draw_list_switch_to_next_pass_split(uint32_t p_splits, DrawListID *r_split_ids) { |
| 5325 | ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Deprecated. Split draw lists are used automatically by RenderingDevice."); |
| 5326 | } |
| 5327 | #endif |
| 5328 | |
| 5329 | void RenderingDevice::_draw_list_start(const Rect2i &p_viewport) { |
| 5330 | draw_list.viewport = p_viewport; |
| 5331 | draw_list.active = true; |
| 5332 | } |
| 5333 | |
| 5334 | void RenderingDevice::_draw_list_end(Rect2i *r_last_viewport) { |
| 5335 | if (r_last_viewport) { |
| 5336 | *r_last_viewport = draw_list.viewport; |
| 5337 | } |
| 5338 | |
| 5339 | draw_list = DrawList(); |
| 5340 | } |
| 5341 | |
| 5342 | void RenderingDevice::draw_list_end() { |
| 5343 | ERR_RENDER_THREAD_GUARD(); |
| 5344 | |
| 5345 | ERR_FAIL_COND_MSG(!draw_list.active, "Immediate draw list is already inactive."); |
| 5346 | |
| 5347 | draw_graph.add_draw_list_end(); |
| 5348 | |
| 5349 | _draw_list_end(); |
| 5350 | |
| 5351 | for (uint32_t i = 0; i < draw_list_bound_textures.size(); i++) { |
| 5352 | Texture *texture = texture_owner.get_or_null(draw_list_bound_textures[i]); |
| 5353 | ERR_CONTINUE(!texture); // Wtf. |
| 5354 | if (texture->usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) { |
| 5355 | texture->bound = false; |
| 5356 | } |
| 5357 | if (texture->usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) { |
| 5358 | texture->bound = false; |
| 5359 | } |
| 5360 | if (texture->usage_flags & TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT) { |
| 5361 | texture->bound = false; |
| 5362 | } |
| 5363 | } |
| 5364 | |
| 5365 | draw_list_bound_textures.clear(); |
| 5366 | } |
| 5367 | |
| 5368 | /***********************/ |
| 5369 | /**** COMPUTE LISTS ****/ |
| 5370 | /***********************/ |
| 5371 | |
| 5372 | RenderingDevice::ComputeListID RenderingDevice::compute_list_begin() { |
| 5373 | ERR_RENDER_THREAD_GUARD_V(INVALID_ID); |
| 5374 | |
| 5375 | ERR_FAIL_COND_V_MSG(compute_list.active, INVALID_ID, "Only one draw/compute list can be active at the same time."); |
| 5376 | |
| 5377 | compute_list.active = true; |
| 5378 | |
| 5379 | draw_graph.add_compute_list_begin(); |
| 5380 | |
| 5381 | return ID_TYPE_COMPUTE_LIST; |
| 5382 | } |
| 5383 | |
| 5384 | void RenderingDevice::compute_list_bind_compute_pipeline(ComputeListID p_list, RID p_compute_pipeline) { |
| 5385 | ERR_RENDER_THREAD_GUARD(); |
| 5386 | |
| 5387 | ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST); |
| 5388 | ERR_FAIL_COND(!compute_list.active); |
| 5389 | |
| 5390 | const ComputePipeline *pipeline = compute_pipeline_owner.get_or_null(p_compute_pipeline); |
| 5391 | ERR_FAIL_NULL(pipeline); |
| 5392 | |
| 5393 | if (p_compute_pipeline == compute_list.state.pipeline) { |
| 5394 | return; // Redundant state, return. |
| 5395 | } |
| 5396 | |
| 5397 | compute_list.state.pipeline = p_compute_pipeline; |
| 5398 | |
| 5399 | draw_graph.add_compute_list_bind_pipeline(pipeline->driver_id); |
| 5400 | |
| 5401 | if (compute_list.state.pipeline_shader != pipeline->shader) { |
| 5402 | // Shader changed, so descriptor sets may become incompatible. |
| 5403 | |
| 5404 | uint32_t pcount = pipeline->set_formats.size(); // Formats count in this pipeline. |
| 5405 | compute_list.state.set_count = MAX(compute_list.state.set_count, pcount); |
| 5406 | const uint32_t *pformats = pipeline->set_formats.ptr(); // Pipeline set formats. |
| 5407 | |
| 5408 | uint32_t first_invalid_set = UINT32_MAX; // All valid by default. |
| 5409 | switch (driver->api_trait_get(RDD::API_TRAIT_SHADER_CHANGE_INVALIDATION)) { |
| 5410 | case RDD::SHADER_CHANGE_INVALIDATION_ALL_BOUND_UNIFORM_SETS: { |
| 5411 | first_invalid_set = 0; |
| 5412 | } break; |
| 5413 | case RDD::SHADER_CHANGE_INVALIDATION_INCOMPATIBLE_SETS_PLUS_CASCADE: { |
| 5414 | for (uint32_t i = 0; i < pcount; i++) { |
| 5415 | if (compute_list.state.sets[i].pipeline_expected_format != pformats[i]) { |
| 5416 | first_invalid_set = i; |
| 5417 | break; |
| 5418 | } |
| 5419 | } |
| 5420 | } break; |
| 5421 | case RDD::SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH: { |
| 5422 | if (compute_list.state.pipeline_shader_layout_hash != pipeline->shader_layout_hash) { |
| 5423 | first_invalid_set = 0; |
| 5424 | } |
| 5425 | } break; |
| 5426 | } |
| 5427 | |
| 5428 | for (uint32_t i = 0; i < pcount; i++) { |
| 5429 | compute_list.state.sets[i].bound = compute_list.state.sets[i].bound && i < first_invalid_set; |
| 5430 | compute_list.state.sets[i].pipeline_expected_format = pformats[i]; |
| 5431 | } |
| 5432 | |
| 5433 | for (uint32_t i = pcount; i < compute_list.state.set_count; i++) { |
| 5434 | // Unbind the ones above (not used) if exist. |
| 5435 | compute_list.state.sets[i].bound = false; |
| 5436 | } |
| 5437 | |
| 5438 | compute_list.state.set_count = pcount; // Update set count. |
| 5439 | |
| 5440 | if (pipeline->push_constant_size) { |
| 5441 | #ifdef DEBUG_ENABLED |
| 5442 | compute_list.validation.pipeline_push_constant_supplied = false; |
| 5443 | #endif |
| 5444 | } |
| 5445 | |
| 5446 | compute_list.state.pipeline_shader = pipeline->shader; |
| 5447 | compute_list.state.pipeline_shader_driver_id = pipeline->shader_driver_id; |
| 5448 | compute_list.state.pipeline_shader_layout_hash = pipeline->shader_layout_hash; |
| 5449 | compute_list.state.local_group_size[0] = pipeline->local_group_size[0]; |
| 5450 | compute_list.state.local_group_size[1] = pipeline->local_group_size[1]; |
| 5451 | compute_list.state.local_group_size[2] = pipeline->local_group_size[2]; |
| 5452 | } |
| 5453 | |
| 5454 | #ifdef DEBUG_ENABLED |
| 5455 | // Update compute pass pipeline info. |
| 5456 | compute_list.validation.pipeline_active = true; |
| 5457 | compute_list.validation.pipeline_push_constant_size = pipeline->push_constant_size; |
| 5458 | #endif |
| 5459 | } |
| 5460 | |
| 5461 | void RenderingDevice::compute_list_bind_uniform_set(ComputeListID p_list, RID p_uniform_set, uint32_t p_index) { |
| 5462 | ERR_RENDER_THREAD_GUARD(); |
| 5463 | |
| 5464 | ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST); |
| 5465 | ERR_FAIL_COND(!compute_list.active); |
| 5466 | |
| 5467 | #ifdef DEBUG_ENABLED |
| 5468 | ERR_FAIL_COND_MSG(p_index >= driver->limit_get(LIMIT_MAX_BOUND_UNIFORM_SETS) || p_index >= MAX_UNIFORM_SETS, |
| 5469 | "Attempting to bind a descriptor set (" + itos(p_index) + ") greater than what the hardware supports (" + itos(driver->limit_get(LIMIT_MAX_BOUND_UNIFORM_SETS)) + ")."); |
| 5470 | #endif |
| 5471 | |
| 5472 | UniformSet *uniform_set = uniform_set_owner.get_or_null(p_uniform_set); |
| 5473 | ERR_FAIL_NULL(uniform_set); |
| 5474 | |
| 5475 | if (p_index > compute_list.state.set_count) { |
| 5476 | compute_list.state.set_count = p_index; |
| 5477 | } |
| 5478 | |
| 5479 | compute_list.state.sets[p_index].uniform_set_driver_id = uniform_set->driver_id; // Update set pointer. |
| 5480 | compute_list.state.sets[p_index].bound = false; // Needs rebind. |
| 5481 | compute_list.state.sets[p_index].uniform_set_format = uniform_set->format; |
| 5482 | compute_list.state.sets[p_index].uniform_set = p_uniform_set; |
| 5483 | |
| 5484 | #if 0 |
| 5485 | { // Validate that textures bound are not attached as framebuffer bindings. |
| 5486 | uint32_t attachable_count = uniform_set->attachable_textures.size(); |
| 5487 | const RID *attachable_ptr = uniform_set->attachable_textures.ptr(); |
| 5488 | uint32_t bound_count = draw_list_bound_textures.size(); |
| 5489 | const RID *bound_ptr = draw_list_bound_textures.ptr(); |
| 5490 | for (uint32_t i = 0; i < attachable_count; i++) { |
| 5491 | for (uint32_t j = 0; j < bound_count; j++) { |
| 5492 | ERR_FAIL_COND_MSG(attachable_ptr[i] == bound_ptr[j], |
| 5493 | "Attempted to use the same texture in framebuffer attachment and a uniform set, this is not allowed."); |
| 5494 | } |
| 5495 | } |
| 5496 | } |
| 5497 | #endif |
| 5498 | } |
| 5499 | |
| 5500 | void RenderingDevice::compute_list_set_push_constant(ComputeListID p_list, const void *p_data, uint32_t p_data_size) { |
| 5501 | ERR_RENDER_THREAD_GUARD(); |
| 5502 | |
| 5503 | ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST); |
| 5504 | ERR_FAIL_COND(!compute_list.active); |
| 5505 | ERR_FAIL_COND_MSG(p_data_size > MAX_PUSH_CONSTANT_SIZE, "Push constants can't be bigger than 128 bytes to maintain compatibility."); |
| 5506 | |
| 5507 | #ifdef DEBUG_ENABLED |
| 5508 | ERR_FAIL_COND_MSG(p_data_size != compute_list.validation.pipeline_push_constant_size, |
| 5509 | "This compute pipeline requires (" + itos(compute_list.validation.pipeline_push_constant_size) + ") bytes of push constant data, supplied: (" + itos(p_data_size) + ")"); |
| 5510 | #endif |
| 5511 | |
| 5512 | draw_graph.add_compute_list_set_push_constant(compute_list.state.pipeline_shader_driver_id, p_data, p_data_size); |
| 5513 | |
| 5514 | // Store it in the state in case we need to restart the compute list. |
| 5515 | memcpy(compute_list.state.push_constant_data, p_data, p_data_size); |
| 5516 | compute_list.state.push_constant_size = p_data_size; |
| 5517 | |
| 5518 | #ifdef DEBUG_ENABLED |
| 5519 | compute_list.validation.pipeline_push_constant_supplied = true; |
| 5520 | #endif |
| 5521 | } |
| 5522 | |
| 5523 | void RenderingDevice::compute_list_dispatch(ComputeListID p_list, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) { |
| 5524 | ERR_RENDER_THREAD_GUARD(); |
| 5525 | |
| 5526 | ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST); |
| 5527 | ERR_FAIL_COND(!compute_list.active); |
| 5528 | |
| 5529 | #ifdef DEBUG_ENABLED |
| 5530 | ERR_FAIL_COND_MSG(p_x_groups == 0, "Dispatch amount of X compute groups (" + itos(p_x_groups) + ") is zero."); |
| 5531 | ERR_FAIL_COND_MSG(p_z_groups == 0, "Dispatch amount of Z compute groups (" + itos(p_z_groups) + ") is zero."); |
| 5532 | ERR_FAIL_COND_MSG(p_y_groups == 0, "Dispatch amount of Y compute groups (" + itos(p_y_groups) + ") is zero."); |
| 5533 | ERR_FAIL_COND_MSG(p_x_groups > driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X), |
| 5534 | "Dispatch amount of X compute groups (" + itos(p_x_groups) + ") is larger than device limit (" + itos(driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X)) + ")"); |
| 5535 | ERR_FAIL_COND_MSG(p_y_groups > driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Y), |
| 5536 | "Dispatch amount of Y compute groups (" + itos(p_y_groups) + ") is larger than device limit (" + itos(driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Y)) + ")"); |
| 5537 | ERR_FAIL_COND_MSG(p_z_groups > driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Z), |
| 5538 | "Dispatch amount of Z compute groups (" + itos(p_z_groups) + ") is larger than device limit (" + itos(driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Z)) + ")"); |
| 5539 | #endif |
| 5540 | |
| 5541 | #ifdef DEBUG_ENABLED |
| 5542 | |
| 5543 | ERR_FAIL_COND_MSG(!compute_list.validation.pipeline_active, "No compute pipeline was set before attempting to draw."); |
| 5544 | |
| 5545 | if (compute_list.validation.pipeline_push_constant_size > 0) { |
| 5546 | // Using push constants, check that they were supplied. |
| 5547 | ERR_FAIL_COND_MSG(!compute_list.validation.pipeline_push_constant_supplied, |
| 5548 | "The shader in this pipeline requires a push constant to be set before drawing, but it's not present."); |
| 5549 | } |
| 5550 | |
| 5551 | #endif |
| 5552 | |
| 5553 | #ifdef DEBUG_ENABLED |
| 5554 | for (uint32_t i = 0; i < compute_list.state.set_count; i++) { |
| 5555 | if (compute_list.state.sets[i].pipeline_expected_format == 0) { |
| 5556 | // Nothing expected by this pipeline. |
| 5557 | continue; |
| 5558 | } |
| 5559 | |
| 5560 | if (compute_list.state.sets[i].pipeline_expected_format != compute_list.state.sets[i].uniform_set_format) { |
| 5561 | if (compute_list.state.sets[i].uniform_set_format == 0) { |
| 5562 | ERR_FAIL_MSG("Uniforms were never supplied for set (" + itos(i) + ") at the time of drawing, which are required by the pipeline."); |
| 5563 | } else if (uniform_set_owner.owns(compute_list.state.sets[i].uniform_set)) { |
| 5564 | UniformSet *us = uniform_set_owner.get_or_null(compute_list.state.sets[i].uniform_set); |
| 5565 | ERR_FAIL_MSG("Uniforms supplied for set (" + itos(i) + "):\n" + _shader_uniform_debug(us->shader_id, us->shader_set) + "\nare not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n" + _shader_uniform_debug(compute_list.state.pipeline_shader)); |
| 5566 | } else { |
| 5567 | ERR_FAIL_MSG("Uniforms supplied for set (" + itos(i) + ", which was just freed) are not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n" + _shader_uniform_debug(compute_list.state.pipeline_shader)); |
| 5568 | } |
| 5569 | } |
| 5570 | } |
| 5571 | #endif |
| 5572 | thread_local LocalVector<RDD::UniformSetID> valid_descriptor_ids; |
| 5573 | valid_descriptor_ids.clear(); |
| 5574 | valid_descriptor_ids.resize(compute_list.state.set_count); |
| 5575 | |
| 5576 | uint32_t valid_set_count = 0; |
| 5577 | uint32_t first_set_index = 0; |
| 5578 | uint32_t last_set_index = 0; |
| 5579 | bool found_first_set = false; |
| 5580 | |
| 5581 | for (uint32_t i = 0; i < compute_list.state.set_count; i++) { |
| 5582 | if (compute_list.state.sets[i].pipeline_expected_format == 0) { |
| 5583 | // Nothing expected by this pipeline. |
| 5584 | continue; |
| 5585 | } |
| 5586 | |
| 5587 | if (!compute_list.state.sets[i].bound && !found_first_set) { |
| 5588 | first_set_index = i; |
| 5589 | found_first_set = true; |
| 5590 | } |
| 5591 | // Prepare descriptor sets if the API doesn't use pipeline barriers. |
| 5592 | if (!driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) { |
| 5593 | draw_graph.add_compute_list_uniform_set_prepare_for_use(compute_list.state.pipeline_shader_driver_id, compute_list.state.sets[i].uniform_set_driver_id, i); |
| 5594 | } |
| 5595 | } |
| 5596 | |
| 5597 | // Bind descriptor sets. |
| 5598 | for (uint32_t i = first_set_index; i < compute_list.state.set_count; i++) { |
| 5599 | if (compute_list.state.sets[i].pipeline_expected_format == 0) { |
| 5600 | continue; // Nothing expected by this pipeline. |
| 5601 | } |
| 5602 | |
| 5603 | if (!compute_list.state.sets[i].bound) { |
| 5604 | // Descriptor set batching |
| 5605 | if (descriptor_set_batching) { |
| 5606 | // All good, see if this requires re-binding. |
| 5607 | if (i - last_set_index > 1) { |
| 5608 | // If the descriptor sets are not contiguous, bind the previous ones and start a new batch. |
| 5609 | draw_graph.add_compute_list_bind_uniform_sets(compute_list.state.pipeline_shader_driver_id, valid_descriptor_ids, first_set_index, valid_set_count); |
| 5610 | |
| 5611 | first_set_index = i; |
| 5612 | valid_set_count = 1; |
| 5613 | valid_descriptor_ids[0] = compute_list.state.sets[i].uniform_set_driver_id; |
| 5614 | } else { |
| 5615 | // Otherwise, keep storing in the current batch. |
| 5616 | valid_descriptor_ids[valid_set_count] = compute_list.state.sets[i].uniform_set_driver_id; |
| 5617 | valid_set_count++; |
| 5618 | } |
| 5619 | |
| 5620 | last_set_index = i; |
| 5621 | } else { |
| 5622 | draw_graph.add_compute_list_bind_uniform_set(compute_list.state.pipeline_shader_driver_id, compute_list.state.sets[i].uniform_set_driver_id, i); |
| 5623 | } |
| 5624 | UniformSet *uniform_set = uniform_set_owner.get_or_null(compute_list.state.sets[i].uniform_set); |
| 5625 | _uniform_set_update_shared(uniform_set); |
| 5626 | _uniform_set_update_clears(uniform_set); |
| 5627 | |
| 5628 | draw_graph.add_compute_list_usages(uniform_set->draw_trackers, uniform_set->draw_trackers_usage); |
| 5629 | compute_list.state.sets[i].bound = true; |
| 5630 | } |
| 5631 | } |
| 5632 | |
| 5633 | // Bind the remaining batch. |
| 5634 | if (valid_set_count > 0) { |
| 5635 | draw_graph.add_compute_list_bind_uniform_sets(compute_list.state.pipeline_shader_driver_id, valid_descriptor_ids, first_set_index, valid_set_count); |
| 5636 | } |
| 5637 | draw_graph.add_compute_list_dispatch(p_x_groups, p_y_groups, p_z_groups); |
| 5638 | compute_list.state.dispatch_count++; |
| 5639 | } |
| 5640 | |
| 5641 | void RenderingDevice::compute_list_dispatch_threads(ComputeListID p_list, uint32_t p_x_threads, uint32_t p_y_threads, uint32_t p_z_threads) { |
| 5642 | ERR_RENDER_THREAD_GUARD(); |
| 5643 | |
| 5644 | ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST); |
| 5645 | ERR_FAIL_COND(!compute_list.active); |
| 5646 | |
| 5647 | #ifdef DEBUG_ENABLED |
| 5648 | ERR_FAIL_COND_MSG(p_x_threads == 0, "Dispatch amount of X compute threads (" + itos(p_x_threads) + ") is zero."); |
| 5649 | ERR_FAIL_COND_MSG(p_y_threads == 0, "Dispatch amount of Y compute threads (" + itos(p_y_threads) + ") is zero."); |
| 5650 | ERR_FAIL_COND_MSG(p_z_threads == 0, "Dispatch amount of Z compute threads (" + itos(p_z_threads) + ") is zero."); |
| 5651 | #endif |
| 5652 | |
| 5653 | #ifdef DEBUG_ENABLED |
| 5654 | |
| 5655 | ERR_FAIL_COND_MSG(!compute_list.validation.pipeline_active, "No compute pipeline was set before attempting to draw."); |
| 5656 | |
| 5657 | if (compute_list.validation.pipeline_push_constant_size > 0) { |
| 5658 | // Using push constants, check that they were supplied. |
| 5659 | ERR_FAIL_COND_MSG(!compute_list.validation.pipeline_push_constant_supplied, |
| 5660 | "The shader in this pipeline requires a push constant to be set before drawing, but it's not present."); |
| 5661 | } |
| 5662 | |
| 5663 | #endif |
| 5664 | |
| 5665 | compute_list_dispatch(p_list, Math::division_round_up(p_x_threads, compute_list.state.local_group_size[0]), Math::division_round_up(p_y_threads, compute_list.state.local_group_size[1]), Math::division_round_up(p_z_threads, compute_list.state.local_group_size[2])); |
| 5666 | } |
| 5667 | |
| 5668 | void RenderingDevice::compute_list_dispatch_indirect(ComputeListID p_list, RID p_buffer, uint32_t p_offset) { |
| 5669 | ERR_RENDER_THREAD_GUARD(); |
| 5670 | |
| 5671 | ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST); |
| 5672 | ERR_FAIL_COND(!compute_list.active); |
| 5673 | |
| 5674 | Buffer *buffer = storage_buffer_owner.get_or_null(p_buffer); |
| 5675 | ERR_FAIL_NULL(buffer); |
| 5676 | |
| 5677 | ERR_FAIL_COND_MSG(!buffer->usage.has_flag(RDD::BUFFER_USAGE_INDIRECT_BIT), "Buffer provided was not created to do indirect dispatch."); |
| 5678 | |
| 5679 | ERR_FAIL_COND_MSG(p_offset + 12 > buffer->size, "Offset provided (+12) is past the end of buffer."); |
| 5680 | |
| 5681 | #ifdef DEBUG_ENABLED |
| 5682 | |
| 5683 | ERR_FAIL_COND_MSG(!compute_list.validation.pipeline_active, "No compute pipeline was set before attempting to draw."); |
| 5684 | |
| 5685 | if (compute_list.validation.pipeline_push_constant_size > 0) { |
| 5686 | // Using push constants, check that they were supplied. |
| 5687 | ERR_FAIL_COND_MSG(!compute_list.validation.pipeline_push_constant_supplied, |
| 5688 | "The shader in this pipeline requires a push constant to be set before drawing, but it's not present."); |
| 5689 | } |
| 5690 | |
| 5691 | #endif |
| 5692 | |
| 5693 | #ifdef DEBUG_ENABLED |
| 5694 | for (uint32_t i = 0; i < compute_list.state.set_count; i++) { |
| 5695 | if (compute_list.state.sets[i].pipeline_expected_format == 0) { |
| 5696 | // Nothing expected by this pipeline. |
| 5697 | continue; |
| 5698 | } |
| 5699 | |
| 5700 | if (compute_list.state.sets[i].pipeline_expected_format != compute_list.state.sets[i].uniform_set_format) { |
| 5701 | if (compute_list.state.sets[i].uniform_set_format == 0) { |
| 5702 | ERR_FAIL_MSG("Uniforms were never supplied for set (" + itos(i) + ") at the time of drawing, which are required by the pipeline."); |
| 5703 | } else if (uniform_set_owner.owns(compute_list.state.sets[i].uniform_set)) { |
| 5704 | UniformSet *us = uniform_set_owner.get_or_null(compute_list.state.sets[i].uniform_set); |
| 5705 | ERR_FAIL_MSG("Uniforms supplied for set (" + itos(i) + "):\n" + _shader_uniform_debug(us->shader_id, us->shader_set) + "\nare not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n" + _shader_uniform_debug(compute_list.state.pipeline_shader)); |
| 5706 | } else { |
| 5707 | ERR_FAIL_MSG("Uniforms supplied for set (" + itos(i) + ", which was just freed) are not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n" + _shader_uniform_debug(compute_list.state.pipeline_shader)); |
| 5708 | } |
| 5709 | } |
| 5710 | } |
| 5711 | #endif |
| 5712 | thread_local LocalVector<RDD::UniformSetID> valid_descriptor_ids; |
| 5713 | valid_descriptor_ids.clear(); |
| 5714 | valid_descriptor_ids.resize(compute_list.state.set_count); |
| 5715 | |
| 5716 | uint32_t valid_set_count = 0; |
| 5717 | uint32_t first_set_index = 0; |
| 5718 | uint32_t last_set_index = 0; |
| 5719 | bool found_first_set = false; |
| 5720 | |
| 5721 | for (uint32_t i = 0; i < compute_list.state.set_count; i++) { |
| 5722 | if (compute_list.state.sets[i].pipeline_expected_format == 0) { |
| 5723 | // Nothing expected by this pipeline. |
| 5724 | continue; |
| 5725 | } |
| 5726 | |
| 5727 | if (!compute_list.state.sets[i].bound && !found_first_set) { |
| 5728 | first_set_index = i; |
| 5729 | found_first_set = true; |
| 5730 | } |
| 5731 | |
| 5732 | // Prepare descriptor sets if the API doesn't use pipeline barriers. |
| 5733 | if (!driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) { |
| 5734 | draw_graph.add_compute_list_uniform_set_prepare_for_use(compute_list.state.pipeline_shader_driver_id, compute_list.state.sets[i].uniform_set_driver_id, i); |
| 5735 | } |
| 5736 | } |
| 5737 | |
| 5738 | // Bind descriptor sets. |
| 5739 | for (uint32_t i = first_set_index; i < compute_list.state.set_count; i++) { |
| 5740 | if (compute_list.state.sets[i].pipeline_expected_format == 0) { |
| 5741 | continue; // Nothing expected by this pipeline. |
| 5742 | } |
| 5743 | |
| 5744 | if (!compute_list.state.sets[i].bound) { |
| 5745 | // All good, see if this requires re-binding. |
| 5746 | if (i - last_set_index > 1) { |
| 5747 | // If the descriptor sets are not contiguous, bind the previous ones and start a new batch. |
| 5748 | draw_graph.add_compute_list_bind_uniform_sets(compute_list.state.pipeline_shader_driver_id, valid_descriptor_ids, first_set_index, valid_set_count); |
| 5749 | |
| 5750 | first_set_index = i; |
| 5751 | valid_set_count = 1; |
| 5752 | valid_descriptor_ids[0] = compute_list.state.sets[i].uniform_set_driver_id; |
| 5753 | } else { |
| 5754 | // Otherwise, keep storing in the current batch. |
| 5755 | valid_descriptor_ids[valid_set_count] = compute_list.state.sets[i].uniform_set_driver_id; |
| 5756 | valid_set_count++; |
| 5757 | } |
| 5758 | |
| 5759 | last_set_index = i; |
| 5760 | |
| 5761 | UniformSet *uniform_set = uniform_set_owner.get_or_null(compute_list.state.sets[i].uniform_set); |
| 5762 | _uniform_set_update_shared(uniform_set); |
| 5763 | _uniform_set_update_clears(uniform_set); |
| 5764 | |
| 5765 | draw_graph.add_compute_list_usages(uniform_set->draw_trackers, uniform_set->draw_trackers_usage); |
| 5766 | compute_list.state.sets[i].bound = true; |
| 5767 | } |
| 5768 | } |
| 5769 | |
| 5770 | // Bind the remaining batch. |
| 5771 | if (valid_set_count > 0) { |
| 5772 | draw_graph.add_compute_list_bind_uniform_sets(compute_list.state.pipeline_shader_driver_id, valid_descriptor_ids, first_set_index, valid_set_count); |
| 5773 | } |
| 5774 | |
| 5775 | draw_graph.add_compute_list_dispatch_indirect(buffer->driver_id, p_offset); |
| 5776 | compute_list.state.dispatch_count++; |
| 5777 | |
| 5778 | if (buffer->draw_tracker != nullptr) { |
| 5779 | draw_graph.add_compute_list_usage(buffer->draw_tracker, RDG::RESOURCE_USAGE_INDIRECT_BUFFER_READ); |
| 5780 | } |
| 5781 | |
| 5782 | _check_transfer_worker_buffer(buffer); |
| 5783 | } |
| 5784 | |
| 5785 | void RenderingDevice::compute_list_add_barrier(ComputeListID p_list) { |
| 5786 | ERR_RENDER_THREAD_GUARD(); |
| 5787 | |
| 5788 | compute_list_barrier_state = compute_list.state; |
| 5789 | compute_list_end(); |
| 5790 | compute_list_begin(); |
| 5791 | |
| 5792 | if (compute_list_barrier_state.pipeline.is_valid()) { |
| 5793 | compute_list_bind_compute_pipeline(p_list, compute_list_barrier_state.pipeline); |
| 5794 | } |
| 5795 | |
| 5796 | for (uint32_t i = 0; i < compute_list_barrier_state.set_count; i++) { |
| 5797 | if (compute_list_barrier_state.sets[i].uniform_set.is_valid()) { |
| 5798 | compute_list_bind_uniform_set(p_list, compute_list_barrier_state.sets[i].uniform_set, i); |
| 5799 | } |
| 5800 | } |
| 5801 | |
| 5802 | if (compute_list_barrier_state.push_constant_size > 0) { |
| 5803 | compute_list_set_push_constant(p_list, compute_list_barrier_state.push_constant_data, compute_list_barrier_state.push_constant_size); |
| 5804 | } |
| 5805 | } |
| 5806 | |
| 5807 | void RenderingDevice::compute_list_end() { |
| 5808 | ERR_RENDER_THREAD_GUARD(); |
| 5809 | |
| 5810 | ERR_FAIL_COND(!compute_list.active); |
| 5811 | |
| 5812 | draw_graph.add_compute_list_end(); |
| 5813 | |
| 5814 | compute_list = ComputeList(); |
| 5815 | } |
| 5816 | |
| 5817 | #ifndef DISABLE_DEPRECATED |
| 5818 | void RenderingDevice::barrier(BitField<BarrierMask> p_from, BitField<BarrierMask> p_to) { |
| 5819 | WARN_PRINT("Deprecated. Barriers are automatically inserted by RenderingDevice."); |
| 5820 | } |
| 5821 | |
| 5822 | void RenderingDevice::full_barrier() { |
| 5823 | WARN_PRINT("Deprecated. Barriers are automatically inserted by RenderingDevice."); |
| 5824 | } |
| 5825 | #endif |
| 5826 | |
| 5827 | /*************************/ |
| 5828 | /**** TRANSFER WORKER ****/ |
| 5829 | /*************************/ |
| 5830 | |
| 5831 | static uint32_t _get_alignment_offset(uint32_t p_offset, uint32_t p_required_align) { |
| 5832 | uint32_t alignment_offset = (p_required_align > 0) ? (p_offset % p_required_align) : 0; |
| 5833 | if (alignment_offset != 0) { |
| 5834 | // If a particular alignment is required, add the offset as part of the required size. |
| 5835 | alignment_offset = p_required_align - alignment_offset; |
| 5836 | } |
| 5837 | |
| 5838 | return alignment_offset; |
| 5839 | } |
| 5840 | |
| 5841 | RenderingDevice::TransferWorker *RenderingDevice::_acquire_transfer_worker(uint32_t p_transfer_size, uint32_t p_required_align, uint32_t &r_staging_offset) { |
| 5842 | // Find the first worker that is not currently executing anything and has enough size for the transfer. |
| 5843 | // If no workers are available, we make a new one. If we're not allowed to make new ones, we wait until one of them is available. |
| 5844 | TransferWorker *transfer_worker = nullptr; |
| 5845 | uint32_t available_list_index = 0; |
| 5846 | bool transfer_worker_busy = true; |
| 5847 | bool transfer_worker_full = true; |
| 5848 | { |
| 5849 | MutexLock pool_lock(transfer_worker_pool_mutex); |
| 5850 | |
| 5851 | // If no workers are available and we've reached the max pool capacity, wait until one of them becomes available. |
| 5852 | bool transfer_worker_pool_full = transfer_worker_pool.size() >= transfer_worker_pool_max_size; |
| 5853 | while (transfer_worker_pool_available_list.is_empty() && transfer_worker_pool_full) { |
| 5854 | transfer_worker_pool_condition.wait(pool_lock); |
| 5855 | } |
| 5856 | |
| 5857 | // Look at all available workers first. |
| 5858 | for (uint32_t i = 0; i < transfer_worker_pool_available_list.size(); i++) { |
| 5859 | uint32_t worker_index = transfer_worker_pool_available_list[i]; |
| 5860 | TransferWorker *candidate_worker = transfer_worker_pool[worker_index]; |
| 5861 | candidate_worker->thread_mutex.lock(); |
| 5862 | |
| 5863 | // Figure out if the worker can fit the transfer. |
| 5864 | uint32_t alignment_offset = _get_alignment_offset(candidate_worker->staging_buffer_size_in_use, p_required_align); |
| 5865 | uint32_t required_size = candidate_worker->staging_buffer_size_in_use + p_transfer_size + alignment_offset; |
| 5866 | bool candidate_worker_busy = candidate_worker->submitted; |
| 5867 | bool candidate_worker_full = required_size > candidate_worker->staging_buffer_size_allocated; |
| 5868 | bool pick_candidate = false; |
| 5869 | if (!candidate_worker_busy && !candidate_worker_full) { |
| 5870 | // A worker that can fit the transfer and is not waiting for a previous execution is the best possible candidate. |
| 5871 | pick_candidate = true; |
| 5872 | } else if (!candidate_worker_busy) { |
| 5873 | // The worker can't fit the transfer but it's not currently doing anything. |
| 5874 | // We pick it as a possible candidate if the current one is busy. |
| 5875 | pick_candidate = transfer_worker_busy; |
| 5876 | } else if (!candidate_worker_full) { |
| 5877 | // The worker can fit the transfer but it's currently executing previous work. |
| 5878 | // We pick it as a possible candidate if the current one is both busy and full. |
| 5879 | pick_candidate = transfer_worker_busy && transfer_worker_full; |
| 5880 | } else if (transfer_worker == nullptr) { |
| 5881 | // The worker can't fit the transfer and it's currently executing work, so it's the worst candidate. |
| 5882 | // We only pick if no candidate has been picked yet. |
| 5883 | pick_candidate = true; |
| 5884 | } |
| 5885 | |
| 5886 | if (pick_candidate) { |
| 5887 | if (transfer_worker != nullptr) { |
| 5888 | // Release the lock for the worker that was picked previously. |
| 5889 | transfer_worker->thread_mutex.unlock(); |
| 5890 | } |
| 5891 | |
| 5892 | // Keep the lock active for this worker. |
| 5893 | transfer_worker = candidate_worker; |
| 5894 | transfer_worker_busy = candidate_worker_busy; |
| 5895 | transfer_worker_full = candidate_worker_full; |
| 5896 | available_list_index = i; |
| 5897 | |
| 5898 | if (!transfer_worker_busy && !transfer_worker_full) { |
| 5899 | // Best possible candidate, stop searching early. |
| 5900 | break; |
| 5901 | } |
| 5902 | } else { |
| 5903 | // Release the lock for the candidate. |
| 5904 | candidate_worker->thread_mutex.unlock(); |
| 5905 | } |
| 5906 | } |
| 5907 | |
| 5908 | if (transfer_worker != nullptr) { |
| 5909 | // A worker was picked, remove it from the available list. |
| 5910 | transfer_worker_pool_available_list.remove_at(available_list_index); |
| 5911 | } else { |
| 5912 | DEV_ASSERT(!transfer_worker_pool_full && "A transfer worker should never be created when the pool is full."); |
| 5913 | |
| 5914 | // No existing worker was picked, we create a new one. |
| 5915 | transfer_worker = memnew(TransferWorker); |
| 5916 | transfer_worker->command_fence = driver->fence_create(); |
| 5917 | transfer_worker->command_pool = driver->command_pool_create(transfer_queue_family, RDD::COMMAND_BUFFER_TYPE_PRIMARY); |
| 5918 | transfer_worker->command_buffer = driver->command_buffer_create(transfer_worker->command_pool); |
| 5919 | transfer_worker->index = transfer_worker_pool.size(); |
| 5920 | transfer_worker_pool.push_back(transfer_worker); |
| 5921 | transfer_worker_operation_used_by_draw.push_back(0); |
| 5922 | transfer_worker->thread_mutex.lock(); |
| 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | if (transfer_worker->submitted) { |
| 5927 | // Wait for the worker if the command buffer was submitted but it hasn't finished processing yet. |
| 5928 | _wait_for_transfer_worker(transfer_worker); |
| 5929 | } |
| 5930 | |
| 5931 | uint32_t alignment_offset = _get_alignment_offset(transfer_worker->staging_buffer_size_in_use, p_required_align); |
| 5932 | transfer_worker->max_transfer_size = MAX(transfer_worker->max_transfer_size, p_transfer_size); |
| 5933 | |
| 5934 | uint32_t required_size = transfer_worker->staging_buffer_size_in_use + p_transfer_size + alignment_offset; |
| 5935 | if (required_size > transfer_worker->staging_buffer_size_allocated) { |
| 5936 | // If there's not enough bytes to use on the staging buffer, we submit everything pending from the worker and wait for the work to be finished. |
| 5937 | if (transfer_worker->recording) { |
| 5938 | _end_transfer_worker(transfer_worker); |
| 5939 | _submit_transfer_worker(transfer_worker); |
| 5940 | } |
| 5941 | |
| 5942 | if (transfer_worker->submitted) { |
| 5943 | _wait_for_transfer_worker(transfer_worker); |
| 5944 | } |
| 5945 | |
| 5946 | alignment_offset = 0; |
| 5947 | |
| 5948 | // If the staging buffer can't fit the transfer, we recreate the buffer. |
| 5949 | const uint32_t expected_buffer_size_minimum = 16 * 1024; |
| 5950 | uint32_t expected_buffer_size = MAX(transfer_worker->max_transfer_size, expected_buffer_size_minimum); |
| 5951 | if (expected_buffer_size > transfer_worker->staging_buffer_size_allocated) { |
| 5952 | if (transfer_worker->staging_buffer.id != 0) { |
| 5953 | driver->buffer_free(transfer_worker->staging_buffer); |
| 5954 | } |
| 5955 | |
| 5956 | uint32_t new_staging_buffer_size = next_power_of_2(expected_buffer_size); |
| 5957 | transfer_worker->staging_buffer_size_allocated = new_staging_buffer_size; |
| 5958 | transfer_worker->staging_buffer = driver->buffer_create(new_staging_buffer_size, RDD::BUFFER_USAGE_TRANSFER_FROM_BIT, RDD::MEMORY_ALLOCATION_TYPE_CPU, frames_drawn); |
| 5959 | } |
| 5960 | } |
| 5961 | |
| 5962 | // Add the alignment before storing the offset that will be returned. |
| 5963 | transfer_worker->staging_buffer_size_in_use += alignment_offset; |
| 5964 | |
| 5965 | // Store the offset to return and increment the current size. |
| 5966 | r_staging_offset = transfer_worker->staging_buffer_size_in_use; |
| 5967 | transfer_worker->staging_buffer_size_in_use += p_transfer_size; |
| 5968 | |
| 5969 | if (!transfer_worker->recording) { |
| 5970 | // Begin the command buffer if the worker wasn't recording yet. |
| 5971 | driver->command_buffer_begin(transfer_worker->command_buffer); |
| 5972 | transfer_worker->recording = true; |
| 5973 | } |
| 5974 | |
| 5975 | return transfer_worker; |
| 5976 | } |
| 5977 | |
| 5978 | void RenderingDevice::_release_transfer_worker(TransferWorker *p_transfer_worker) { |
| 5979 | p_transfer_worker->thread_mutex.unlock(); |
| 5980 | |
| 5981 | transfer_worker_pool_mutex.lock(); |
| 5982 | transfer_worker_pool_available_list.push_back(p_transfer_worker->index); |
| 5983 | transfer_worker_pool_mutex.unlock(); |
| 5984 | transfer_worker_pool_condition.notify_one(); |
| 5985 | } |
| 5986 | |
| 5987 | void RenderingDevice::_end_transfer_worker(TransferWorker *p_transfer_worker) { |
| 5988 | driver->command_buffer_end(p_transfer_worker->command_buffer); |
| 5989 | p_transfer_worker->recording = false; |
| 5990 | } |
| 5991 | |
| 5992 | void RenderingDevice::_submit_transfer_worker(TransferWorker *p_transfer_worker, VectorView<RDD::SemaphoreID> p_signal_semaphores) { |
| 5993 | driver->command_queue_execute_and_present(transfer_queue, {}, p_transfer_worker->command_buffer, p_signal_semaphores, p_transfer_worker->command_fence, {}); |
| 5994 | |
| 5995 | for (uint32_t i = 0; i < p_signal_semaphores.size(); i++) { |
| 5996 | // Indicate the frame should wait on these semaphores before executing the main command buffer. |
| 5997 | frames[frame].semaphores_to_wait_on.push_back(p_signal_semaphores[i]); |
| 5998 | } |
| 5999 | |
| 6000 | p_transfer_worker->submitted = true; |
| 6001 | |
| 6002 | { |
| 6003 | MutexLock lock(p_transfer_worker->operations_mutex); |
| 6004 | p_transfer_worker->operations_submitted = p_transfer_worker->operations_counter; |
| 6005 | } |
| 6006 | } |
| 6007 | |
| 6008 | void RenderingDevice::_wait_for_transfer_worker(TransferWorker *p_transfer_worker) { |
| 6009 | driver->fence_wait(p_transfer_worker->command_fence); |
| 6010 | driver->command_pool_reset(p_transfer_worker->command_pool); |
| 6011 | p_transfer_worker->staging_buffer_size_in_use = 0; |
| 6012 | p_transfer_worker->submitted = false; |
| 6013 | |
| 6014 | { |
| 6015 | MutexLock lock(p_transfer_worker->operations_mutex); |
| 6016 | p_transfer_worker->operations_processed = p_transfer_worker->operations_submitted; |
| 6017 | } |
| 6018 | |
| 6019 | _flush_barriers_for_transfer_worker(p_transfer_worker); |
| 6020 | } |
| 6021 | |
| 6022 | void RenderingDevice::_flush_barriers_for_transfer_worker(TransferWorker *p_transfer_worker) { |
| 6023 | // Caller must have already acquired the mutex for the worker. |
| 6024 | if (!p_transfer_worker->texture_barriers.is_empty()) { |
| 6025 | MutexLock transfer_worker_lock(transfer_worker_pool_texture_barriers_mutex); |
| 6026 | for (uint32_t i = 0; i < p_transfer_worker->texture_barriers.size(); i++) { |
| 6027 | transfer_worker_pool_texture_barriers.push_back(p_transfer_worker->texture_barriers[i]); |
| 6028 | } |
| 6029 | |
| 6030 | p_transfer_worker->texture_barriers.clear(); |
| 6031 | } |
| 6032 | } |
| 6033 | |
| 6034 | void RenderingDevice::_check_transfer_worker_operation(uint32_t p_transfer_worker_index, uint64_t p_transfer_worker_operation) { |
| 6035 | TransferWorker *transfer_worker = transfer_worker_pool[p_transfer_worker_index]; |
| 6036 | MutexLock lock(transfer_worker->operations_mutex); |
| 6037 | uint64_t &dst_operation = transfer_worker_operation_used_by_draw[transfer_worker->index]; |
| 6038 | dst_operation = MAX(dst_operation, p_transfer_worker_operation); |
| 6039 | } |
| 6040 | |
| 6041 | void RenderingDevice::_check_transfer_worker_buffer(Buffer *p_buffer) { |
| 6042 | if (p_buffer->transfer_worker_index >= 0) { |
| 6043 | _check_transfer_worker_operation(p_buffer->transfer_worker_index, p_buffer->transfer_worker_operation); |
| 6044 | p_buffer->transfer_worker_index = -1; |
| 6045 | } |
| 6046 | } |
| 6047 | |
| 6048 | void RenderingDevice::_check_transfer_worker_texture(Texture *p_texture) { |
| 6049 | if (p_texture->transfer_worker_index >= 0) { |
| 6050 | _check_transfer_worker_operation(p_texture->transfer_worker_index, p_texture->transfer_worker_operation); |
| 6051 | p_texture->transfer_worker_index = -1; |
| 6052 | } |
| 6053 | } |
| 6054 | |
| 6055 | void RenderingDevice::_check_transfer_worker_vertex_array(VertexArray *p_vertex_array) { |
| 6056 | if (!p_vertex_array->transfer_worker_indices.is_empty()) { |
| 6057 | for (int i = 0; i < p_vertex_array->transfer_worker_indices.size(); i++) { |
| 6058 | _check_transfer_worker_operation(p_vertex_array->transfer_worker_indices[i], p_vertex_array->transfer_worker_operations[i]); |
| 6059 | } |
| 6060 | |
| 6061 | p_vertex_array->transfer_worker_indices.clear(); |
| 6062 | p_vertex_array->transfer_worker_operations.clear(); |
| 6063 | } |
| 6064 | } |
| 6065 | |
| 6066 | void RenderingDevice::_check_transfer_worker_index_array(IndexArray *p_index_array) { |
| 6067 | if (p_index_array->transfer_worker_index >= 0) { |
| 6068 | _check_transfer_worker_operation(p_index_array->transfer_worker_index, p_index_array->transfer_worker_operation); |
| 6069 | p_index_array->transfer_worker_index = -1; |
| 6070 | } |
| 6071 | } |
| 6072 | |
| 6073 | void RenderingDevice::_submit_transfer_workers(RDD::CommandBufferID p_draw_command_buffer) { |
| 6074 | MutexLock transfer_worker_lock(transfer_worker_pool_mutex); |
| 6075 | for (uint32_t i = 0; i < transfer_worker_pool.size(); i++) { |
| 6076 | TransferWorker *worker = transfer_worker_pool[i]; |
| 6077 | if (p_draw_command_buffer) { |
| 6078 | MutexLock lock(worker->operations_mutex); |
| 6079 | if (worker->operations_processed >= transfer_worker_operation_used_by_draw[worker->index]) { |
| 6080 | // The operation used by the draw has already been processed, we don't need to wait on the worker. |
| 6081 | continue; |
| 6082 | } |
| 6083 | } |
| 6084 | |
| 6085 | { |
| 6086 | MutexLock lock(worker->thread_mutex); |
| 6087 | if (worker->recording) { |
| 6088 | VectorView<RDD::SemaphoreID> semaphores = p_draw_command_buffer ? frames[frame].transfer_worker_semaphores[i] : VectorView<RDD::SemaphoreID>(); |
| 6089 | _end_transfer_worker(worker); |
| 6090 | _submit_transfer_worker(worker, semaphores); |
| 6091 | } |
| 6092 | |
| 6093 | if (p_draw_command_buffer) { |
| 6094 | _flush_barriers_for_transfer_worker(worker); |
| 6095 | } |
| 6096 | } |
| 6097 | } |
| 6098 | } |
| 6099 | |
| 6100 | void RenderingDevice::_submit_transfer_barriers(RDD::CommandBufferID p_draw_command_buffer) { |
| 6101 | MutexLock transfer_worker_lock(transfer_worker_pool_texture_barriers_mutex); |
| 6102 | if (!transfer_worker_pool_texture_barriers.is_empty()) { |
| 6103 | driver->command_pipeline_barrier(p_draw_command_buffer, RDD::PIPELINE_STAGE_COPY_BIT, RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT, {}, {}, transfer_worker_pool_texture_barriers); |
| 6104 | transfer_worker_pool_texture_barriers.clear(); |
| 6105 | } |
| 6106 | } |
| 6107 | |
| 6108 | void RenderingDevice::_wait_for_transfer_workers() { |
| 6109 | MutexLock transfer_worker_lock(transfer_worker_pool_mutex); |
| 6110 | for (TransferWorker *worker : transfer_worker_pool) { |
| 6111 | MutexLock lock(worker->thread_mutex); |
| 6112 | if (worker->submitted) { |
| 6113 | _wait_for_transfer_worker(worker); |
| 6114 | } |
| 6115 | } |
| 6116 | } |
| 6117 | |
| 6118 | void RenderingDevice::_free_transfer_workers() { |
| 6119 | MutexLock transfer_worker_lock(transfer_worker_pool_mutex); |
| 6120 | for (TransferWorker *worker : transfer_worker_pool) { |
| 6121 | driver->fence_free(worker->command_fence); |
| 6122 | driver->buffer_free(worker->staging_buffer); |
| 6123 | driver->command_pool_free(worker->command_pool); |
| 6124 | memdelete(worker); |
| 6125 | } |
| 6126 | |
| 6127 | transfer_worker_pool.clear(); |
| 6128 | } |
| 6129 | |
| 6130 | /***********************/ |
| 6131 | /**** COMMAND GRAPH ****/ |
| 6132 | /***********************/ |
| 6133 | |
| 6134 | bool RenderingDevice::_texture_make_mutable(Texture *p_texture, RID p_texture_id) { |
| 6135 | if (p_texture->draw_tracker != nullptr) { |
| 6136 | // Texture already has a tracker. |
| 6137 | return false; |
| 6138 | } else { |
| 6139 | if (p_texture->owner.is_valid()) { |
| 6140 | // Texture has an owner. |
| 6141 | Texture *owner_texture = texture_owner.get_or_null(p_texture->owner); |
| 6142 | ERR_FAIL_NULL_V(owner_texture, false); |
| 6143 | |
| 6144 | if (owner_texture->draw_tracker != nullptr) { |
| 6145 | // Create a tracker for this dependency in particular. |
| 6146 | if (p_texture->slice_type == TEXTURE_SLICE_MAX) { |
| 6147 | // Shared texture. |
| 6148 | p_texture->draw_tracker = owner_texture->draw_tracker; |
| 6149 | p_texture->draw_tracker->reference_count++; |
| 6150 | } else { |
| 6151 | // Slice texture. |
| 6152 | if (owner_texture->slice_trackers == nullptr) { |
| 6153 | owner_texture->slice_trackers = memnew((HashMap<Rect2i, RDG::ResourceTracker *>)); |
| 6154 | } |
| 6155 | HashMap<Rect2i, RDG::ResourceTracker *>::ConstIterator draw_tracker_iterator = owner_texture->slice_trackers->find(p_texture->slice_rect); |
| 6156 | RDG::ResourceTracker *draw_tracker = nullptr; |
| 6157 | if (draw_tracker_iterator != owner_texture->slice_trackers->end()) { |
| 6158 | // Reuse the tracker at the matching rectangle. |
| 6159 | draw_tracker = draw_tracker_iterator->value; |
| 6160 | } else { |
| 6161 | // Create a new tracker and store it on the map. |
| 6162 | draw_tracker = RDG::resource_tracker_create(); |
| 6163 | draw_tracker->parent = owner_texture->draw_tracker; |
| 6164 | draw_tracker->texture_driver_id = p_texture->driver_id; |
| 6165 | draw_tracker->texture_size = Size2i(p_texture->width, p_texture->height); |
| 6166 | draw_tracker->texture_subresources = p_texture->barrier_range(); |
| 6167 | draw_tracker->texture_usage = p_texture->usage_flags; |
| 6168 | draw_tracker->texture_slice_or_dirty_rect = p_texture->slice_rect; |
| 6169 | (*owner_texture->slice_trackers)[p_texture->slice_rect] = draw_tracker; |
| 6170 | } |
| 6171 | |
| 6172 | p_texture->draw_tracker = draw_tracker; |
| 6173 | p_texture->draw_tracker->reference_count++; |
| 6174 | } |
| 6175 | |
| 6176 | if (p_texture_id.is_valid()) { |
| 6177 | _dependencies_make_mutable(p_texture_id, p_texture->draw_tracker); |
| 6178 | } |
| 6179 | } else { |
| 6180 | // Delegate this to the owner instead, as it'll make all its dependencies mutable. |
| 6181 | _texture_make_mutable(owner_texture, p_texture->owner); |
| 6182 | } |
| 6183 | } else { |
| 6184 | // Regular texture. |
| 6185 | p_texture->draw_tracker = RDG::resource_tracker_create(); |
| 6186 | p_texture->draw_tracker->texture_driver_id = p_texture->driver_id; |
| 6187 | p_texture->draw_tracker->texture_size = Size2i(p_texture->width, p_texture->height); |
| 6188 | p_texture->draw_tracker->texture_subresources = p_texture->barrier_range(); |
| 6189 | p_texture->draw_tracker->texture_usage = p_texture->usage_flags; |
| 6190 | p_texture->draw_tracker->is_discardable = p_texture->is_discardable; |
| 6191 | p_texture->draw_tracker->reference_count = 1; |
| 6192 | |
| 6193 | if (p_texture_id.is_valid()) { |
| 6194 | if (p_texture->has_initial_data) { |
| 6195 | // If the texture was initialized with initial data but wasn't made mutable from the start, assume the texture sampling usage. |
| 6196 | p_texture->draw_tracker->usage = RDG::RESOURCE_USAGE_TEXTURE_SAMPLE; |
| 6197 | } |
| 6198 | |
| 6199 | _dependencies_make_mutable(p_texture_id, p_texture->draw_tracker); |
| 6200 | } |
| 6201 | } |
| 6202 | |
| 6203 | return true; |
| 6204 | } |
| 6205 | } |
| 6206 | |
| 6207 | bool RenderingDevice::_buffer_make_mutable(Buffer *p_buffer, RID p_buffer_id) { |
| 6208 | if (p_buffer->draw_tracker != nullptr) { |
| 6209 | // Buffer already has a tracker. |
| 6210 | return false; |
| 6211 | } else { |
| 6212 | // Create a tracker for the buffer and make all its dependencies mutable. |
| 6213 | p_buffer->draw_tracker = RDG::resource_tracker_create(); |
| 6214 | p_buffer->draw_tracker->buffer_driver_id = p_buffer->driver_id; |
| 6215 | if (p_buffer_id.is_valid()) { |
| 6216 | _dependencies_make_mutable(p_buffer_id, p_buffer->draw_tracker); |
| 6217 | } |
| 6218 | |
| 6219 | return true; |
| 6220 | } |
| 6221 | } |
| 6222 | |
| 6223 | bool RenderingDevice::_vertex_array_make_mutable(VertexArray *p_vertex_array, RID p_resource_id, RDG::ResourceTracker *p_resource_tracker) { |
| 6224 | if (!p_vertex_array->untracked_buffers.has(p_resource_id)) { |
| 6225 | // Vertex array thinks the buffer is already tracked or does not use it. |
| 6226 | return false; |
| 6227 | } else { |
| 6228 | // Vertex array is aware of the buffer but it isn't being tracked. |
| 6229 | p_vertex_array->draw_trackers.push_back(p_resource_tracker); |
| 6230 | p_vertex_array->untracked_buffers.erase(p_resource_id); |
| 6231 | return true; |
| 6232 | } |
| 6233 | } |
| 6234 | |
| 6235 | bool RenderingDevice::_index_array_make_mutable(IndexArray *p_index_array, RDG::ResourceTracker *p_resource_tracker) { |
| 6236 | if (p_index_array->draw_tracker != nullptr) { |
| 6237 | // Index array already has a tracker. |
| 6238 | return false; |
| 6239 | } else { |
| 6240 | // Index array should assign the tracker from the buffer. |
| 6241 | p_index_array->draw_tracker = p_resource_tracker; |
| 6242 | return true; |
| 6243 | } |
| 6244 | } |
| 6245 | |
| 6246 | bool RenderingDevice::_uniform_set_make_mutable(UniformSet *p_uniform_set, RID p_resource_id, RDG::ResourceTracker *p_resource_tracker) { |
| 6247 | HashMap<RID, RDG::ResourceUsage>::Iterator E = p_uniform_set->untracked_usage.find(p_resource_id); |
| 6248 | if (!E) { |
| 6249 | // Uniform set thinks the resource is already tracked or does not use it. |
| 6250 | return false; |
| 6251 | } else { |
| 6252 | // Uniform set has seen the resource but hasn't added its tracker yet. |
| 6253 | p_uniform_set->draw_trackers.push_back(p_resource_tracker); |
| 6254 | p_uniform_set->draw_trackers_usage.push_back(E->value); |
| 6255 | p_uniform_set->untracked_usage.remove(E); |
| 6256 | return true; |
| 6257 | } |
| 6258 | } |
| 6259 | |
| 6260 | bool RenderingDevice::_dependency_make_mutable(RID p_id, RID p_resource_id, RDG::ResourceTracker *p_resource_tracker) { |
| 6261 | if (texture_owner.owns(p_id)) { |
| 6262 | Texture *texture = texture_owner.get_or_null(p_id); |
| 6263 | return _texture_make_mutable(texture, p_id); |
| 6264 | } else if (vertex_array_owner.owns(p_id)) { |
| 6265 | VertexArray *vertex_array = vertex_array_owner.get_or_null(p_id); |
| 6266 | return _vertex_array_make_mutable(vertex_array, p_resource_id, p_resource_tracker); |
| 6267 | } else if (index_array_owner.owns(p_id)) { |
| 6268 | IndexArray *index_array = index_array_owner.get_or_null(p_id); |
| 6269 | return _index_array_make_mutable(index_array, p_resource_tracker); |
| 6270 | } else if (uniform_set_owner.owns(p_id)) { |
| 6271 | UniformSet *uniform_set = uniform_set_owner.get_or_null(p_id); |
| 6272 | return _uniform_set_make_mutable(uniform_set, p_resource_id, p_resource_tracker); |
| 6273 | } else { |
| 6274 | DEV_ASSERT(false && "Unknown resource type to make mutable."); |
| 6275 | return false; |
| 6276 | } |
| 6277 | } |
| 6278 | |
| 6279 | bool RenderingDevice::_dependencies_make_mutable_recursive(RID p_id, RDG::ResourceTracker *p_resource_tracker) { |
| 6280 | bool made_mutable = false; |
| 6281 | HashMap<RID, HashSet<RID>>::Iterator E = dependency_map.find(p_id); |
| 6282 | if (E) { |
| 6283 | for (RID rid : E->value) { |
| 6284 | made_mutable = _dependency_make_mutable(rid, p_id, p_resource_tracker) || made_mutable; |
| 6285 | } |
| 6286 | } |
| 6287 | |
| 6288 | return made_mutable; |
| 6289 | } |
| 6290 | |
| 6291 | bool RenderingDevice::_dependencies_make_mutable(RID p_id, RDG::ResourceTracker *p_resource_tracker) { |
| 6292 | _THREAD_SAFE_METHOD_ |
| 6293 | return _dependencies_make_mutable_recursive(p_id, p_resource_tracker); |
| 6294 | } |
| 6295 | |
| 6296 | /**************************/ |
| 6297 | /**** FRAME MANAGEMENT ****/ |
| 6298 | /**************************/ |
| 6299 | |
| 6300 | void RenderingDevice::free_rid(RID p_rid) { |
| 6301 | ERR_RENDER_THREAD_GUARD(); |
| 6302 | |
| 6303 | _free_dependencies(p_rid); // Recursively erase dependencies first, to avoid potential API problems. |
| 6304 | _free_internal(p_rid); |
| 6305 | } |
| 6306 | |
| 6307 | void RenderingDevice::_free_internal(RID p_id) { |
| 6308 | #ifdef DEV_ENABLED |
| 6309 | String resource_name; |
| 6310 | if (resource_names.has(p_id)) { |
| 6311 | resource_name = resource_names[p_id]; |
| 6312 | resource_names.erase(p_id); |
| 6313 | } |
| 6314 | #endif |
| 6315 | |
| 6316 | // Push everything so it's disposed of next time this frame index is processed (means, it's safe to do it). |
| 6317 | if (texture_owner.owns(p_id)) { |
| 6318 | Texture *texture = texture_owner.get_or_null(p_id); |
| 6319 | _check_transfer_worker_texture(texture); |
| 6320 | |
| 6321 | RDG::ResourceTracker *draw_tracker = texture->draw_tracker; |
| 6322 | if (draw_tracker != nullptr) { |
| 6323 | draw_tracker->reference_count--; |
| 6324 | if (draw_tracker->reference_count == 0) { |
| 6325 | RDG::resource_tracker_free(draw_tracker); |
| 6326 | |
| 6327 | if (texture->owner.is_valid() && (texture->slice_type != TEXTURE_SLICE_MAX)) { |
| 6328 | // If this was a texture slice, erase the tracker from the map. |
| 6329 | Texture *owner_texture = texture_owner.get_or_null(texture->owner); |
| 6330 | if (owner_texture != nullptr && owner_texture->slice_trackers != nullptr) { |
| 6331 | owner_texture->slice_trackers->erase(texture->slice_rect); |
| 6332 | |
| 6333 | if (owner_texture->slice_trackers->is_empty()) { |
| 6334 | memdelete(owner_texture->slice_trackers); |
| 6335 | owner_texture->slice_trackers = nullptr; |
| 6336 | } |
| 6337 | } |
| 6338 | } |
| 6339 | } |
| 6340 | } |
| 6341 | |
| 6342 | frames[frame].textures_to_dispose_of.push_back(*texture); |
| 6343 | texture_owner.free(p_id); |
| 6344 | } else if (framebuffer_owner.owns(p_id)) { |
| 6345 | Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_id); |
| 6346 | frames[frame].framebuffers_to_dispose_of.push_back(*framebuffer); |
| 6347 | |
| 6348 | if (framebuffer->invalidated_callback != nullptr) { |
| 6349 | framebuffer->invalidated_callback(framebuffer->invalidated_callback_userdata); |
| 6350 | } |
| 6351 | |
| 6352 | framebuffer_owner.free(p_id); |
| 6353 | } else if (sampler_owner.owns(p_id)) { |
| 6354 | RDD::SamplerID sampler_driver_id = *sampler_owner.get_or_null(p_id); |
| 6355 | frames[frame].samplers_to_dispose_of.push_back(sampler_driver_id); |
| 6356 | sampler_owner.free(p_id); |
| 6357 | } else if (vertex_buffer_owner.owns(p_id)) { |
| 6358 | Buffer *vertex_buffer = vertex_buffer_owner.get_or_null(p_id); |
| 6359 | _check_transfer_worker_buffer(vertex_buffer); |
| 6360 | |
| 6361 | RDG::resource_tracker_free(vertex_buffer->draw_tracker); |
| 6362 | frames[frame].buffers_to_dispose_of.push_back(*vertex_buffer); |
| 6363 | vertex_buffer_owner.free(p_id); |
| 6364 | } else if (vertex_array_owner.owns(p_id)) { |
| 6365 | vertex_array_owner.free(p_id); |
| 6366 | } else if (index_buffer_owner.owns(p_id)) { |
| 6367 | IndexBuffer *index_buffer = index_buffer_owner.get_or_null(p_id); |
| 6368 | _check_transfer_worker_buffer(index_buffer); |
| 6369 | |
| 6370 | RDG::resource_tracker_free(index_buffer->draw_tracker); |
| 6371 | frames[frame].buffers_to_dispose_of.push_back(*index_buffer); |
| 6372 | index_buffer_owner.free(p_id); |
| 6373 | } else if (index_array_owner.owns(p_id)) { |
| 6374 | index_array_owner.free(p_id); |
| 6375 | } else if (shader_owner.owns(p_id)) { |
| 6376 | Shader *shader = shader_owner.get_or_null(p_id); |
| 6377 | if (shader->driver_id) { // Not placeholder? |
| 6378 | frames[frame].shaders_to_dispose_of.push_back(*shader); |
| 6379 | } |
| 6380 | shader_owner.free(p_id); |
| 6381 | } else if (uniform_buffer_owner.owns(p_id)) { |
| 6382 | Buffer *uniform_buffer = uniform_buffer_owner.get_or_null(p_id); |
| 6383 | _check_transfer_worker_buffer(uniform_buffer); |
| 6384 | |
| 6385 | RDG::resource_tracker_free(uniform_buffer->draw_tracker); |
| 6386 | frames[frame].buffers_to_dispose_of.push_back(*uniform_buffer); |
| 6387 | uniform_buffer_owner.free(p_id); |
| 6388 | } else if (texture_buffer_owner.owns(p_id)) { |
| 6389 | Buffer *texture_buffer = texture_buffer_owner.get_or_null(p_id); |
| 6390 | _check_transfer_worker_buffer(texture_buffer); |
| 6391 | |
| 6392 | RDG::resource_tracker_free(texture_buffer->draw_tracker); |
| 6393 | frames[frame].buffers_to_dispose_of.push_back(*texture_buffer); |
| 6394 | texture_buffer_owner.free(p_id); |
| 6395 | } else if (storage_buffer_owner.owns(p_id)) { |
| 6396 | Buffer *storage_buffer = storage_buffer_owner.get_or_null(p_id); |
| 6397 | _check_transfer_worker_buffer(storage_buffer); |
| 6398 | |
| 6399 | RDG::resource_tracker_free(storage_buffer->draw_tracker); |
| 6400 | frames[frame].buffers_to_dispose_of.push_back(*storage_buffer); |
| 6401 | storage_buffer_owner.free(p_id); |
| 6402 | } else if (uniform_set_owner.owns(p_id)) { |
| 6403 | UniformSet *uniform_set = uniform_set_owner.get_or_null(p_id); |
| 6404 | frames[frame].uniform_sets_to_dispose_of.push_back(*uniform_set); |
| 6405 | uniform_set_owner.free(p_id); |
| 6406 | |
| 6407 | if (uniform_set->invalidated_callback != nullptr) { |
| 6408 | uniform_set->invalidated_callback(uniform_set->invalidated_callback_userdata); |
| 6409 | } |
| 6410 | } else if (render_pipeline_owner.owns(p_id)) { |
| 6411 | RenderPipeline *pipeline = render_pipeline_owner.get_or_null(p_id); |
| 6412 | frames[frame].render_pipelines_to_dispose_of.push_back(*pipeline); |
| 6413 | render_pipeline_owner.free(p_id); |
| 6414 | } else if (compute_pipeline_owner.owns(p_id)) { |
| 6415 | ComputePipeline *pipeline = compute_pipeline_owner.get_or_null(p_id); |
| 6416 | frames[frame].compute_pipelines_to_dispose_of.push_back(*pipeline); |
| 6417 | compute_pipeline_owner.free(p_id); |
| 6418 | } else { |
| 6419 | #ifdef DEV_ENABLED |
| 6420 | ERR_PRINT("Attempted to free invalid ID: " + itos(p_id.get_id()) + " " + resource_name); |
| 6421 | #else |
| 6422 | ERR_PRINT("Attempted to free invalid ID: " + itos(p_id.get_id())); |
| 6423 | #endif |
| 6424 | } |
| 6425 | |
| 6426 | frames_pending_resources_for_processing = uint32_t(frames.size()); |
| 6427 | } |
| 6428 | |
| 6429 | // The full list of resources that can be named is in the VkObjectType enum. |
| 6430 | // We just expose the resources that are owned and can be accessed easily. |
| 6431 | void RenderingDevice::set_resource_name(RID p_id, const String &p_name) { |
| 6432 | _THREAD_SAFE_METHOD_ |
| 6433 | |
| 6434 | if (texture_owner.owns(p_id)) { |
| 6435 | Texture *texture = texture_owner.get_or_null(p_id); |
| 6436 | driver->set_object_name(RDD::OBJECT_TYPE_TEXTURE, texture->driver_id, p_name); |
| 6437 | } else if (framebuffer_owner.owns(p_id)) { |
| 6438 | //Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_id); |
| 6439 | // Not implemented for now as the relationship between Framebuffer and RenderPass is very complex. |
| 6440 | } else if (sampler_owner.owns(p_id)) { |
| 6441 | RDD::SamplerID sampler_driver_id = *sampler_owner.get_or_null(p_id); |
| 6442 | driver->set_object_name(RDD::OBJECT_TYPE_SAMPLER, sampler_driver_id, p_name); |
| 6443 | } else if (vertex_buffer_owner.owns(p_id)) { |
| 6444 | Buffer *vertex_buffer = vertex_buffer_owner.get_or_null(p_id); |
| 6445 | driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, vertex_buffer->driver_id, p_name); |
| 6446 | } else if (index_buffer_owner.owns(p_id)) { |
| 6447 | IndexBuffer *index_buffer = index_buffer_owner.get_or_null(p_id); |
| 6448 | driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, index_buffer->driver_id, p_name); |
| 6449 | } else if (shader_owner.owns(p_id)) { |
| 6450 | Shader *shader = shader_owner.get_or_null(p_id); |
| 6451 | driver->set_object_name(RDD::OBJECT_TYPE_SHADER, shader->driver_id, p_name); |
| 6452 | } else if (uniform_buffer_owner.owns(p_id)) { |
| 6453 | Buffer *uniform_buffer = uniform_buffer_owner.get_or_null(p_id); |
| 6454 | driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, uniform_buffer->driver_id, p_name); |
| 6455 | } else if (texture_buffer_owner.owns(p_id)) { |
| 6456 | Buffer *texture_buffer = texture_buffer_owner.get_or_null(p_id); |
| 6457 | driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, texture_buffer->driver_id, p_name); |
| 6458 | } else if (storage_buffer_owner.owns(p_id)) { |
| 6459 | Buffer *storage_buffer = storage_buffer_owner.get_or_null(p_id); |
| 6460 | driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, storage_buffer->driver_id, p_name); |
| 6461 | } else if (uniform_set_owner.owns(p_id)) { |
| 6462 | UniformSet *uniform_set = uniform_set_owner.get_or_null(p_id); |
| 6463 | driver->set_object_name(RDD::OBJECT_TYPE_UNIFORM_SET, uniform_set->driver_id, p_name); |
| 6464 | } else if (render_pipeline_owner.owns(p_id)) { |
| 6465 | RenderPipeline *pipeline = render_pipeline_owner.get_or_null(p_id); |
| 6466 | driver->set_object_name(RDD::OBJECT_TYPE_PIPELINE, pipeline->driver_id, p_name); |
| 6467 | } else if (compute_pipeline_owner.owns(p_id)) { |
| 6468 | ComputePipeline *pipeline = compute_pipeline_owner.get_or_null(p_id); |
| 6469 | driver->set_object_name(RDD::OBJECT_TYPE_PIPELINE, pipeline->driver_id, p_name); |
| 6470 | } else { |
| 6471 | ERR_PRINT("Attempted to name invalid ID: " + itos(p_id.get_id())); |
| 6472 | return; |
| 6473 | } |
| 6474 | #ifdef DEV_ENABLED |
| 6475 | resource_names[p_id] = p_name; |
| 6476 | #endif |
| 6477 | } |
| 6478 | |
| 6479 | void RenderingDevice::_draw_command_begin_label(String p_label_name, const Color &p_color) { |
| 6480 | draw_command_begin_label(p_label_name.utf8().span(), p_color); |
| 6481 | } |
| 6482 | |
| 6483 | void RenderingDevice::draw_command_begin_label(const Span<char> p_label_name, const Color &p_color) { |
| 6484 | ERR_RENDER_THREAD_GUARD(); |
| 6485 | |
| 6486 | if (!context->is_debug_utils_enabled()) { |
| 6487 | return; |
| 6488 | } |
| 6489 | |
| 6490 | draw_graph.begin_label(p_label_name, p_color); |
| 6491 | } |
| 6492 | |
| 6493 | #ifndef DISABLE_DEPRECATED |
| 6494 | void RenderingDevice::draw_command_insert_label(String p_label_name, const Color &p_color) { |
| 6495 | WARN_PRINT("Deprecated. Inserting labels no longer applies due to command reordering."); |
| 6496 | } |
| 6497 | #endif |
| 6498 | |
| 6499 | void RenderingDevice::draw_command_end_label() { |
| 6500 | ERR_RENDER_THREAD_GUARD(); |
| 6501 | |
| 6502 | draw_graph.end_label(); |
| 6503 | } |
| 6504 | |
| 6505 | String RenderingDevice::get_device_vendor_name() const { |
| 6506 | return _get_device_vendor_name(device); |
| 6507 | } |
| 6508 | |
| 6509 | String RenderingDevice::get_device_name() const { |
| 6510 | return device.name; |
| 6511 | } |
| 6512 | |
| 6513 | RenderingDevice::DeviceType RenderingDevice::get_device_type() const { |
| 6514 | return DeviceType(device.type); |
| 6515 | } |
| 6516 | |
| 6517 | String RenderingDevice::get_device_api_name() const { |
| 6518 | return driver->get_api_name(); |
| 6519 | } |
| 6520 | |
| 6521 | bool RenderingDevice::is_composite_alpha_supported() const { |
| 6522 | return driver->is_composite_alpha_supported(main_queue); |
| 6523 | } |
| 6524 | |
| 6525 | String RenderingDevice::get_device_api_version() const { |
| 6526 | return driver->get_api_version(); |
| 6527 | } |
| 6528 | |
| 6529 | String RenderingDevice::get_device_pipeline_cache_uuid() const { |
| 6530 | return driver->get_pipeline_cache_uuid(); |
| 6531 | } |
| 6532 | |
| 6533 | void RenderingDevice::swap_buffers(bool p_present) { |
| 6534 | ERR_RENDER_THREAD_GUARD(); |
| 6535 | |
| 6536 | GodotProfileZoneGroupedFirst(_profile_zone, "_end_frame"); |
| 6537 | _end_frame(); |
| 6538 | |
| 6539 | GodotProfileZoneGrouped(_profile_zone, "_execute_frame"); |
| 6540 | _execute_frame(p_present); |
| 6541 | |
| 6542 | // Advance to the next frame and begin recording again. |
| 6543 | frame = (frame + 1) % frames.size(); |
| 6544 | |
| 6545 | GodotProfileZoneGrouped(_profile_zone, "_begin_frame"); |
| 6546 | _begin_frame(true); |
| 6547 | } |
| 6548 | |
| 6549 | void RenderingDevice::submit() { |
| 6550 | ERR_RENDER_THREAD_GUARD(); |
| 6551 | ERR_FAIL_COND_MSG(is_main_instance, "Only local devices can submit and sync."); |
| 6552 | ERR_FAIL_COND_MSG(local_device_processing, "device already submitted, call sync to wait until done."); |
| 6553 | |
| 6554 | _end_frame(); |
| 6555 | _execute_frame(false); |
| 6556 | local_device_processing = true; |
| 6557 | } |
| 6558 | |
| 6559 | void RenderingDevice::sync() { |
| 6560 | ERR_RENDER_THREAD_GUARD(); |
| 6561 | ERR_FAIL_COND_MSG(is_main_instance, "Only local devices can submit and sync."); |
| 6562 | ERR_FAIL_COND_MSG(!local_device_processing, "sync can only be called after a submit"); |
| 6563 | |
| 6564 | _begin_frame(true); |
| 6565 | local_device_processing = false; |
| 6566 | } |
| 6567 | |
| 6568 | void RenderingDevice::_free_pending_resources(int p_frame) { |
| 6569 | // Free in dependency usage order, so nothing weird happens. |
| 6570 | // Pipelines. |
| 6571 | while (frames[p_frame].render_pipelines_to_dispose_of.front()) { |
| 6572 | RenderPipeline *pipeline = &frames[p_frame].render_pipelines_to_dispose_of.front()->get(); |
| 6573 | |
| 6574 | driver->pipeline_free(pipeline->driver_id); |
| 6575 | |
| 6576 | frames[p_frame].render_pipelines_to_dispose_of.pop_front(); |
| 6577 | } |
| 6578 | |
| 6579 | while (frames[p_frame].compute_pipelines_to_dispose_of.front()) { |
| 6580 | ComputePipeline *pipeline = &frames[p_frame].compute_pipelines_to_dispose_of.front()->get(); |
| 6581 | |
| 6582 | driver->pipeline_free(pipeline->driver_id); |
| 6583 | |
| 6584 | frames[p_frame].compute_pipelines_to_dispose_of.pop_front(); |
| 6585 | } |
| 6586 | |
| 6587 | // Uniform sets. |
| 6588 | while (frames[p_frame].uniform_sets_to_dispose_of.front()) { |
| 6589 | UniformSet *uniform_set = &frames[p_frame].uniform_sets_to_dispose_of.front()->get(); |
| 6590 | |
| 6591 | driver->uniform_set_free(uniform_set->driver_id); |
| 6592 | |
| 6593 | frames[p_frame].uniform_sets_to_dispose_of.pop_front(); |
| 6594 | } |
| 6595 | |
| 6596 | // Shaders. |
| 6597 | while (frames[p_frame].shaders_to_dispose_of.front()) { |
| 6598 | Shader *shader = &frames[p_frame].shaders_to_dispose_of.front()->get(); |
| 6599 | |
| 6600 | driver->shader_free(shader->driver_id); |
| 6601 | |
| 6602 | frames[p_frame].shaders_to_dispose_of.pop_front(); |
| 6603 | } |
| 6604 | |
| 6605 | // Samplers. |
| 6606 | while (frames[p_frame].samplers_to_dispose_of.front()) { |
| 6607 | RDD::SamplerID sampler = frames[p_frame].samplers_to_dispose_of.front()->get(); |
| 6608 | |
| 6609 | driver->sampler_free(sampler); |
| 6610 | |
| 6611 | frames[p_frame].samplers_to_dispose_of.pop_front(); |
| 6612 | } |
| 6613 | |
| 6614 | // Framebuffers. |
| 6615 | while (frames[p_frame].framebuffers_to_dispose_of.front()) { |
| 6616 | Framebuffer *framebuffer = &frames[p_frame].framebuffers_to_dispose_of.front()->get(); |
| 6617 | draw_graph.framebuffer_cache_free(driver, framebuffer->framebuffer_cache); |
| 6618 | frames[p_frame].framebuffers_to_dispose_of.pop_front(); |
| 6619 | } |
| 6620 | |
| 6621 | // Textures. |
| 6622 | while (frames[p_frame].textures_to_dispose_of.front()) { |
| 6623 | Texture *texture = &frames[p_frame].textures_to_dispose_of.front()->get(); |
| 6624 | if (texture->bound) { |
| 6625 | WARN_PRINT("Deleted a texture while it was bound."); |
| 6626 | } |
| 6627 | |
| 6628 | _texture_free_shared_fallback(texture); |
| 6629 | |
| 6630 | texture_memory -= driver->texture_get_allocation_size(texture->driver_id); |
| 6631 | driver->texture_free(texture->driver_id); |
| 6632 | |
| 6633 | frames[p_frame].textures_to_dispose_of.pop_front(); |
| 6634 | } |
| 6635 | |
| 6636 | // Buffers. |
| 6637 | while (frames[p_frame].buffers_to_dispose_of.front()) { |
| 6638 | Buffer &buffer = frames[p_frame].buffers_to_dispose_of.front()->get(); |
| 6639 | driver->buffer_free(buffer.driver_id); |
| 6640 | buffer_memory -= buffer.size; |
| 6641 | |
| 6642 | frames[p_frame].buffers_to_dispose_of.pop_front(); |
| 6643 | } |
| 6644 | |
| 6645 | if (frames_pending_resources_for_processing > 0u) { |
| 6646 | --frames_pending_resources_for_processing; |
| 6647 | } |
| 6648 | } |
| 6649 | |
| 6650 | uint32_t RenderingDevice::get_frame_delay() const { |
| 6651 | return frames.size(); |
| 6652 | } |
| 6653 | |
| 6654 | uint64_t RenderingDevice::get_memory_usage(MemoryType p_type) const { |
| 6655 | switch (p_type) { |
| 6656 | case MEMORY_BUFFERS: { |
| 6657 | return buffer_memory; |
| 6658 | } |
| 6659 | case MEMORY_TEXTURES: { |
| 6660 | return texture_memory; |
| 6661 | } |
| 6662 | case MEMORY_TOTAL: { |
| 6663 | return driver->get_total_memory_used(); |
| 6664 | } |
| 6665 | default: { |
| 6666 | DEV_ASSERT(false); |
| 6667 | return 0; |
| 6668 | } |
| 6669 | } |
| 6670 | } |
| 6671 | |
| 6672 | void RenderingDevice::_begin_frame(bool p_presented) { |
| 6673 | GodotProfileZoneGroupedFirst(_profile_zone, "_stall_for_frame"); |
| 6674 | // Before writing to this frame, wait for it to be finished. |
| 6675 | _stall_for_frame(frame); |
| 6676 | |
| 6677 | if (command_pool_reset_enabled) { |
| 6678 | GodotProfileZoneGrouped(_profile_zone, "driver->command_pool_reset"); |
| 6679 | bool reset = driver->command_pool_reset(frames[frame].command_pool); |
| 6680 | ERR_FAIL_COND(!reset); |
| 6681 | } |
| 6682 | |
| 6683 | if (p_presented) { |
| 6684 | GodotProfileZoneGrouped(_profile_zone, "update_perf_report"); |
| 6685 | update_perf_report(); |
| 6686 | driver->linear_uniform_set_pools_reset(frame); |
| 6687 | } |
| 6688 | |
| 6689 | // Begin recording on the frame's command buffers. |
| 6690 | GodotProfileZoneGrouped(_profile_zone, "driver->begin_segment"); |
| 6691 | driver->begin_segment(frame, frames_drawn++); |
| 6692 | GodotProfileZoneGrouped(_profile_zone, "driver->command_buffer_begin"); |
| 6693 | driver->command_buffer_begin(frames[frame].command_buffer); |
| 6694 | |
| 6695 | // Reset the graph. |
| 6696 | GodotProfileZoneGrouped(_profile_zone, "draw_graph.begin"); |
| 6697 | draw_graph.begin(); |
| 6698 | |
| 6699 | // Erase pending resources. |
| 6700 | GodotProfileZoneGrouped(_profile_zone, "_free_pending_resources"); |
| 6701 | _free_pending_resources(frame); |
| 6702 | |
| 6703 | // Advance staging buffers if used. |
| 6704 | if (upload_staging_buffers.used) { |
| 6705 | upload_staging_buffers.current = (upload_staging_buffers.current + 1) % upload_staging_buffers.blocks.size(); |
| 6706 | upload_staging_buffers.used = false; |
| 6707 | } |
| 6708 | |
| 6709 | if (download_staging_buffers.used) { |
| 6710 | download_staging_buffers.current = (download_staging_buffers.current + 1) % download_staging_buffers.blocks.size(); |
| 6711 | download_staging_buffers.used = false; |
| 6712 | } |
| 6713 | |
| 6714 | if (frames[frame].timestamp_count) { |
| 6715 | driver->timestamp_query_pool_get_results(frames[frame].timestamp_pool, frames[frame].timestamp_count, frames[frame].timestamp_result_values.ptr()); |
| 6716 | driver->command_timestamp_query_pool_reset(frames[frame].command_buffer, frames[frame].timestamp_pool, frames[frame].timestamp_count); |
| 6717 | SWAP(frames[frame].timestamp_names, frames[frame].timestamp_result_names); |
| 6718 | SWAP(frames[frame].timestamp_cpu_values, frames[frame].timestamp_cpu_result_values); |
| 6719 | } |
| 6720 | |
| 6721 | frames[frame].timestamp_result_count = frames[frame].timestamp_count; |
| 6722 | frames[frame].timestamp_count = 0; |
| 6723 | frames[frame].index = Engine::get_singleton()->get_frames_drawn(); |
| 6724 | } |
| 6725 | |
| 6726 | void RenderingDevice::_end_frame() { |
| 6727 | if (draw_list.active) { |
| 6728 | ERR_PRINT("Found open draw list at the end of the frame, this should never happen (further drawing will likely not work)."); |
| 6729 | } |
| 6730 | |
| 6731 | if (compute_list.active) { |
| 6732 | ERR_PRINT("Found open compute list at the end of the frame, this should never happen (further compute will likely not work)."); |
| 6733 | } |
| 6734 | |
| 6735 | // The command buffer must be copied into a stack variable as the driver workarounds can change the command buffer in use. |
| 6736 | RDD::CommandBufferID command_buffer = frames[frame].command_buffer; |
| 6737 | GodotProfileZoneGroupedFirst(_profile_zone, "_submit_transfer_workers"); |
| 6738 | _submit_transfer_workers(command_buffer); |
| 6739 | GodotProfileZoneGrouped(_profile_zone, "_submit_transfer_barriers"); |
| 6740 | _submit_transfer_barriers(command_buffer); |
| 6741 | |
| 6742 | GodotProfileZoneGrouped(_profile_zone, "draw_graph.end"); |
| 6743 | draw_graph.end(RENDER_GRAPH_REORDER, RENDER_GRAPH_FULL_BARRIERS, command_buffer, frames[frame].command_buffer_pool); |
| 6744 | GodotProfileZoneGrouped(_profile_zone, "driver->command_buffer_end"); |
| 6745 | driver->command_buffer_end(command_buffer); |
| 6746 | GodotProfileZoneGrouped(_profile_zone, "driver->end_segment"); |
| 6747 | driver->end_segment(); |
| 6748 | } |
| 6749 | |
| 6750 | void RenderingDevice::execute_chained_cmds(bool p_present_swap_chain, RenderingDeviceDriver::FenceID p_draw_fence, |
| 6751 | RenderingDeviceDriver::SemaphoreID p_dst_draw_semaphore_to_signal) { |
| 6752 | // Execute command buffers and use semaphores to wait on the execution of the previous one. |
| 6753 | // Normally there's only one command buffer, but driver workarounds can force situations where |
| 6754 | // there'll be more. |
| 6755 | uint32_t command_buffer_count = 1; |
| 6756 | RDG::CommandBufferPool &buffer_pool = frames[frame].command_buffer_pool; |
| 6757 | if (buffer_pool.buffers_used > 0) { |
| 6758 | command_buffer_count += buffer_pool.buffers_used; |
| 6759 | buffer_pool.buffers_used = 0; |
| 6760 | } |
| 6761 | |
| 6762 | thread_local LocalVector<RDD::SwapChainID> swap_chains; |
| 6763 | swap_chains.clear(); |
| 6764 | |
| 6765 | // Instead of having just one command; we have potentially many (which had to be split due to an |
| 6766 | // Adreno workaround on mobile, only if the workaround is active). Thus we must execute all of them |
| 6767 | // and chain them together via semaphores as dependent executions. |
| 6768 | thread_local LocalVector<RDD::SemaphoreID> wait_semaphores; |
| 6769 | wait_semaphores = frames[frame].semaphores_to_wait_on; |
| 6770 | |
| 6771 | for (uint32_t i = 0; i < command_buffer_count; i++) { |
| 6772 | RDD::CommandBufferID command_buffer; |
| 6773 | RDD::SemaphoreID signal_semaphore; |
| 6774 | RDD::FenceID signal_fence; |
| 6775 | if (i > 0) { |
| 6776 | command_buffer = buffer_pool.buffers[i - 1]; |
| 6777 | } else { |
| 6778 | command_buffer = frames[frame].command_buffer; |
| 6779 | } |
| 6780 | |
| 6781 | if (i == (command_buffer_count - 1)) { |
| 6782 | // This is the last command buffer, it should signal the semaphore & fence. |
| 6783 | signal_semaphore = p_dst_draw_semaphore_to_signal; |
| 6784 | signal_fence = p_draw_fence; |
| 6785 | |
| 6786 | if (p_present_swap_chain) { |
| 6787 | // Just present the swap chains as part of the last command execution. |
| 6788 | swap_chains = frames[frame].swap_chains_to_present; |
| 6789 | } |
| 6790 | } else { |
| 6791 | signal_semaphore = buffer_pool.semaphores[i]; |
| 6792 | // Semaphores always need to be signaled if it's not the last command buffer. |
| 6793 | } |
| 6794 | |
| 6795 | driver->command_queue_execute_and_present(main_queue, wait_semaphores, command_buffer, |
| 6796 | signal_semaphore ? signal_semaphore : VectorView<RDD::SemaphoreID>(), signal_fence, |
| 6797 | swap_chains); |
| 6798 | |
| 6799 | // Make the next command buffer wait on the semaphore signaled by this one. |
| 6800 | wait_semaphores.resize(1); |
| 6801 | wait_semaphores[0] = signal_semaphore; |
| 6802 | } |
| 6803 | |
| 6804 | frames[frame].semaphores_to_wait_on.clear(); |
| 6805 | } |
| 6806 | |
| 6807 | void RenderingDevice::_execute_frame(bool p_present) { |
| 6808 | // Check whether this frame should present the swap chains and in which queue. |
| 6809 | const bool frame_can_present = p_present && !frames[frame].swap_chains_to_present.is_empty(); |
| 6810 | const bool separate_present_queue = main_queue != present_queue; |
| 6811 | |
| 6812 | // The semaphore is required if the frame can be presented and a separate present queue is used; |
| 6813 | // since the separate queue will wait for that semaphore before presenting. |
| 6814 | const RDD::SemaphoreID semaphore = (frame_can_present && separate_present_queue) |
| 6815 | ? frames[frame].semaphore |
| 6816 | : RDD::SemaphoreID(nullptr); |
| 6817 | const bool present_swap_chain = frame_can_present && !separate_present_queue; |
| 6818 | |
| 6819 | execute_chained_cmds(present_swap_chain, frames[frame].fence, semaphore); |
| 6820 | // Indicate the fence has been signaled so the next time the frame's contents need to be |
| 6821 | // used, the CPU needs to wait on the work to be completed. |
| 6822 | frames[frame].fence_signaled = true; |
| 6823 | |
| 6824 | if (frame_can_present) { |
| 6825 | if (separate_present_queue) { |
| 6826 | // Issue the presentation separately if the presentation queue is different from the main queue. |
| 6827 | driver->command_queue_execute_and_present(present_queue, frames[frame].semaphore, {}, {}, {}, frames[frame].swap_chains_to_present); |
| 6828 | } |
| 6829 | |
| 6830 | frames[frame].swap_chains_to_present.clear(); |
| 6831 | } |
| 6832 | } |
| 6833 | |
| 6834 | void RenderingDevice::_stall_for_frame(uint32_t p_frame) { |
| 6835 | thread_local PackedByteArray packed_byte_array; |
| 6836 | |
| 6837 | if (frames[p_frame].fence_signaled) { |
| 6838 | GodotProfileZoneGroupedFirst(_profile_zone, "driver->fence_wait"); |
| 6839 | driver->fence_wait(frames[p_frame].fence); |
| 6840 | frames[p_frame].fence_signaled = false; |
| 6841 | |
| 6842 | // Flush any pending requests for asynchronous buffer downloads. |
| 6843 | if (!frames[p_frame].download_buffer_get_data_requests.is_empty()) { |
| 6844 | GodotProfileZoneGrouped(_profile_zone, "flush asynchronous buffer downloads"); |
| 6845 | for (uint32_t i = 0; i < frames[p_frame].download_buffer_get_data_requests.size(); i++) { |
| 6846 | const BufferGetDataRequest &request = frames[p_frame].download_buffer_get_data_requests[i]; |
| 6847 | packed_byte_array.resize(request.size); |
| 6848 | |
| 6849 | uint32_t array_offset = 0; |
| 6850 | for (uint32_t j = 0; j < request.frame_local_count; j++) { |
| 6851 | uint32_t local_index = request.frame_local_index + j; |
| 6852 | const RDD::BufferCopyRegion ®ion = frames[p_frame].download_buffer_copy_regions[local_index]; |
| 6853 | uint8_t *buffer_data = driver->buffer_map(frames[p_frame].download_buffer_staging_buffers[local_index]); |
| 6854 | memcpy(&packed_byte_array.write[array_offset], &buffer_data[region.dst_offset], region.size); |
| 6855 | driver->buffer_unmap(frames[p_frame].download_buffer_staging_buffers[local_index]); |
| 6856 | array_offset += region.size; |
| 6857 | } |
| 6858 | |
| 6859 | request.callback.call(packed_byte_array); |
| 6860 | } |
| 6861 | |
| 6862 | frames[p_frame].download_buffer_staging_buffers.clear(); |
| 6863 | frames[p_frame].download_buffer_copy_regions.clear(); |
| 6864 | frames[p_frame].download_buffer_get_data_requests.clear(); |
| 6865 | } |
| 6866 | |
| 6867 | // Flush any pending requests for asynchronous texture downloads. |
| 6868 | if (!frames[p_frame].download_texture_get_data_requests.is_empty()) { |
| 6869 | GodotProfileZoneGrouped(_profile_zone, "flush asynchronous texture downloads"); |
| 6870 | for (uint32_t i = 0; i < frames[p_frame].download_texture_get_data_requests.size(); i++) { |
| 6871 | const TextureGetDataRequest &request = frames[p_frame].download_texture_get_data_requests[i]; |
| 6872 | uint32_t texture_size = get_image_format_required_size(request.format, request.width, request.height, request.depth, request.mipmaps); |
| 6873 | packed_byte_array.resize(texture_size); |
| 6874 | |
| 6875 | // Find the block size of the texture's format. |
| 6876 | uint32_t block_w = 0; |
| 6877 | uint32_t block_h = 0; |
| 6878 | get_compressed_image_format_block_dimensions(request.format, block_w, block_h); |
| 6879 | |
| 6880 | uint32_t block_size = get_compressed_image_format_block_byte_size(request.format); |
| 6881 | uint32_t pixel_size = get_image_format_pixel_size(request.format); |
| 6882 | uint32_t region_size = texture_download_region_size_px; |
| 6883 | |
| 6884 | for (uint32_t j = 0; j < request.frame_local_count; j++) { |
| 6885 | uint32_t local_index = request.frame_local_index + j; |
| 6886 | const RDD::BufferTextureCopyRegion ®ion = frames[p_frame].download_buffer_texture_copy_regions[local_index]; |
| 6887 | uint32_t w = STEPIFY(request.width >> region.texture_subresource.mipmap, block_w); |
| 6888 | uint32_t h = STEPIFY(request.height >> region.texture_subresource.mipmap, block_h); |
| 6889 | uint32_t region_w = MIN(region_size, w - region.texture_offset.x); |
| 6890 | uint32_t region_h = MIN(region_size, h - region.texture_offset.y); |
| 6891 | |
| 6892 | uint8_t *buffer_data = driver->buffer_map(frames[p_frame].download_texture_staging_buffers[local_index]); |
| 6893 | const uint8_t *read_ptr = buffer_data + region.buffer_offset; |
| 6894 | uint8_t *write_ptr = packed_byte_array.ptrw() + frames[p_frame].download_texture_mipmap_offsets[local_index]; |
| 6895 | uint32_t unit_size = pixel_size; |
| 6896 | if (block_w != 1 || block_h != 1) { |
| 6897 | unit_size = block_size; |
| 6898 | } |
| 6899 | |
| 6900 | write_ptr += ((region.texture_offset.y / block_h) * (w / block_w) + (region.texture_offset.x / block_w)) * unit_size; |
| 6901 | for (uint32_t y = region_h / block_h; y > 0; y--) { |
| 6902 | memcpy(write_ptr, read_ptr, (region_w / block_w) * unit_size); |
| 6903 | write_ptr += (w / block_w) * unit_size; |
| 6904 | read_ptr += region.row_pitch; |
| 6905 | } |
| 6906 | |
| 6907 | driver->buffer_unmap(frames[p_frame].download_texture_staging_buffers[local_index]); |
| 6908 | } |
| 6909 | |
| 6910 | request.callback.call(packed_byte_array); |
| 6911 | } |
| 6912 | |
| 6913 | GodotProfileZoneGrouped(_profile_zone, "clear buffers"); |
| 6914 | frames[p_frame].download_texture_staging_buffers.clear(); |
| 6915 | frames[p_frame].download_buffer_texture_copy_regions.clear(); |
| 6916 | frames[p_frame].download_texture_mipmap_offsets.clear(); |
| 6917 | frames[p_frame].download_texture_get_data_requests.clear(); |
| 6918 | } |
| 6919 | } |
| 6920 | } |
| 6921 | |
| 6922 | void RenderingDevice::_stall_for_previous_frames() { |
| 6923 | for (uint32_t i = 0; i < frames.size(); i++) { |
| 6924 | _stall_for_frame(i); |
| 6925 | } |
| 6926 | } |
| 6927 | |
| 6928 | void RenderingDevice::_flush_and_stall_for_all_frames(bool p_begin_frame) { |
| 6929 | _stall_for_previous_frames(); |
| 6930 | _end_frame(); |
| 6931 | _execute_frame(false); |
| 6932 | |
| 6933 | if (p_begin_frame) { |
| 6934 | _begin_frame(); |
| 6935 | } else { |
| 6936 | _stall_for_frame(frame); |
| 6937 | } |
| 6938 | } |
| 6939 | |
| 6940 | Error RenderingDevice::initialize(RenderingContextDriver *p_context, DisplayServer::WindowID p_main_window) { |
| 6941 | ERR_RENDER_THREAD_GUARD_V(ERR_UNAVAILABLE); |
| 6942 | |
| 6943 | Error err; |
| 6944 | RenderingContextDriver::SurfaceID main_surface = 0; |
| 6945 | is_main_instance = (singleton == this) && (p_main_window != DisplayServer::INVALID_WINDOW_ID); |
| 6946 | if (p_main_window != DisplayServer::INVALID_WINDOW_ID) { |
| 6947 | // Retrieve the surface from the main window if it was specified. |
| 6948 | main_surface = p_context->surface_get_from_window(p_main_window); |
| 6949 | ERR_FAIL_COND_V(main_surface == 0, FAILED); |
| 6950 | } |
| 6951 | |
| 6952 | context = p_context; |
| 6953 | driver = context->driver_create(); |
| 6954 | |
| 6955 | print_verbose("Devices:"); |
| 6956 | int32_t device_index = Engine::get_singleton()->get_gpu_index(); |
| 6957 | const uint32_t device_count = context->device_get_count(); |
| 6958 | const bool detect_device = (device_index < 0) || (device_index >= int32_t(device_count)); |
| 6959 | uint32_t device_type_score = 0; |
| 6960 | for (uint32_t i = 0; i < device_count; i++) { |
| 6961 | RenderingContextDriver::Device device_option = context->device_get(i); |
| 6962 | String name = device_option.name; |
| 6963 | String vendor = _get_device_vendor_name(device_option); |
| 6964 | String type = _get_device_type_name(device_option); |
| 6965 | bool present_supported = main_surface != 0 ? context->device_supports_present(i, main_surface) : false; |
| 6966 | print_verbose(" #" + itos(i) + ": " + vendor + " " + name + " - " + (present_supported ? "Supported" : "Unsupported") + ", " + type); |
| 6967 | if (detect_device && (present_supported || main_surface == 0)) { |
| 6968 | // If a window was specified, present must be supported by the device to be available as an option. |
| 6969 | // Assign a score for each type of device and prefer the device with the higher score. |
| 6970 | uint32_t option_score = _get_device_type_score(device_option); |
| 6971 | if (option_score > device_type_score) { |
| 6972 | device_index = i; |
| 6973 | device_type_score = option_score; |
| 6974 | } |
| 6975 | } |
| 6976 | } |
| 6977 | |
| 6978 | ERR_FAIL_COND_V_MSG((device_index < 0) || (device_index >= int32_t(device_count)), ERR_CANT_CREATE, "None of the devices supports both graphics and present queues."); |
| 6979 | |
| 6980 | uint32_t frame_count = 1; |
| 6981 | if (main_surface != 0) { |
| 6982 | frame_count = MAX(2U, uint32_t(GLOBAL_GET("rendering/rendering_device/vsync/frame_queue_size"))); |
| 6983 | } |
| 6984 | |
| 6985 | frame = 0; |
| 6986 | max_timestamp_query_elements = GLOBAL_GET("debug/settings/profiler/max_timestamp_query_elements"); |
| 6987 | |
| 6988 | device = context->device_get(device_index); |
| 6989 | err = driver->initialize(device_index, frame_count); |
| 6990 | ERR_FAIL_COND_V_MSG(err != OK, FAILED, "Failed to initialize driver for device."); |
| 6991 | |
| 6992 | if (is_main_instance) { |
| 6993 | // Only the singleton instance with a display should print this information. |
| 6994 | String rendering_method; |
| 6995 | if (OS::get_singleton()->get_current_rendering_method() == "mobile") { |
| 6996 | rendering_method = "Forward Mobile"; |
| 6997 | } else { |
| 6998 | rendering_method = "Forward+"; |
| 6999 | } |
| 7000 | |
| 7001 | // Output our device version. |
| 7002 | Engine::get_singleton()->print_header(vformat("%s %s - %s - Using Device #%d: %s - %s", get_device_api_name(), get_device_api_version(), rendering_method, device_index, _get_device_vendor_name(device), device.name)); |
| 7003 | } |
| 7004 | |
| 7005 | // Pick the main queue family. It is worth noting we explicitly do not request the transfer bit, as apparently the specification defines |
| 7006 | // that the existence of either the graphics or compute bit implies that the queue can also do transfer operations, but it is optional |
| 7007 | // to indicate whether it supports them or not with the dedicated transfer bit if either is set. |
| 7008 | BitField<RDD::CommandQueueFamilyBits> main_queue_bits = {}; |
| 7009 | main_queue_bits.set_flag(RDD::COMMAND_QUEUE_FAMILY_GRAPHICS_BIT); |
| 7010 | main_queue_bits.set_flag(RDD::COMMAND_QUEUE_FAMILY_COMPUTE_BIT); |
| 7011 | |
| 7012 | #if !FORCE_SEPARATE_PRESENT_QUEUE |
| 7013 | // Needing to use a separate queue for presentation is an edge case that remains to be seen what hardware triggers it at all. |
| 7014 | main_queue_family = driver->command_queue_family_get(main_queue_bits, main_surface); |
| 7015 | if (!main_queue_family && (main_surface != 0)) |
| 7016 | #endif |
| 7017 | { |
| 7018 | // If it was not possible to find a main queue that supports the surface, we attempt to get two different queues instead. |
| 7019 | main_queue_family = driver->command_queue_family_get(main_queue_bits); |
| 7020 | present_queue_family = driver->command_queue_family_get(BitField<RDD::CommandQueueFamilyBits>(), main_surface); |
| 7021 | ERR_FAIL_COND_V(!present_queue_family, FAILED); |
| 7022 | } |
| 7023 | |
| 7024 | ERR_FAIL_COND_V(!main_queue_family, FAILED); |
| 7025 | |
| 7026 | // Create the main queue. |
| 7027 | main_queue = driver->command_queue_create(main_queue_family, true); |
| 7028 | ERR_FAIL_COND_V(!main_queue, FAILED); |
| 7029 | |
| 7030 | transfer_queue_family = driver->command_queue_family_get(RDD::COMMAND_QUEUE_FAMILY_TRANSFER_BIT); |
| 7031 | if (!transfer_queue_family) { |
| 7032 | // Use main queue family if transfer queue family is not supported. |
| 7033 | transfer_queue_family = main_queue_family; |
| 7034 | } |
| 7035 | |
| 7036 | // Create the transfer queue. |
| 7037 | transfer_queue = driver->command_queue_create(transfer_queue_family); |
| 7038 | ERR_FAIL_COND_V(!transfer_queue, FAILED); |
| 7039 | |
| 7040 | if (present_queue_family) { |
| 7041 | // Create the present queue. |
| 7042 | present_queue = driver->command_queue_create(present_queue_family); |
| 7043 | ERR_FAIL_COND_V(!present_queue, FAILED); |
| 7044 | } else { |
| 7045 | // Use main queue as the present queue. |
| 7046 | present_queue = main_queue; |
| 7047 | present_queue_family = main_queue_family; |
| 7048 | } |
| 7049 | |
| 7050 | // Use the processor count as the max amount of transfer workers that can be created. |
| 7051 | transfer_worker_pool_max_size = OS::get_singleton()->get_processor_count(); |
| 7052 | |
| 7053 | frames.resize(frame_count); |
| 7054 | |
| 7055 | // Create data for all the frames. |
| 7056 | bool frame_failed = false; |
| 7057 | for (uint32_t i = 0; i < frames.size(); i++) { |
| 7058 | frames[i].index = 0; |
| 7059 | |
| 7060 | // Create command pool, command buffers, semaphores and fences. |
| 7061 | frames[i].command_pool = driver->command_pool_create(main_queue_family, RDD::COMMAND_BUFFER_TYPE_PRIMARY); |
| 7062 | if (!frames[i].command_pool) { |
| 7063 | frame_failed = true; |
| 7064 | break; |
| 7065 | } |
| 7066 | frames[i].command_buffer = driver->command_buffer_create(frames[i].command_pool); |
| 7067 | if (!frames[i].command_buffer) { |
| 7068 | frame_failed = true; |
| 7069 | break; |
| 7070 | } |
| 7071 | frames[i].semaphore = driver->semaphore_create(); |
| 7072 | if (!frames[i].semaphore) { |
| 7073 | frame_failed = true; |
| 7074 | break; |
| 7075 | } |
| 7076 | frames[i].fence = driver->fence_create(); |
| 7077 | if (!frames[i].fence) { |
| 7078 | frame_failed = true; |
| 7079 | break; |
| 7080 | } |
| 7081 | frames[i].fence_signaled = false; |
| 7082 | |
| 7083 | // Create query pool. |
| 7084 | frames[i].timestamp_pool = driver->timestamp_query_pool_create(max_timestamp_query_elements); |
| 7085 | frames[i].timestamp_names.resize(max_timestamp_query_elements); |
| 7086 | frames[i].timestamp_cpu_values.resize(max_timestamp_query_elements); |
| 7087 | frames[i].timestamp_count = 0; |
| 7088 | frames[i].timestamp_result_names.resize(max_timestamp_query_elements); |
| 7089 | frames[i].timestamp_cpu_result_values.resize(max_timestamp_query_elements); |
| 7090 | frames[i].timestamp_result_values.resize(max_timestamp_query_elements); |
| 7091 | frames[i].timestamp_result_count = 0; |
| 7092 | |
| 7093 | // Assign the main queue family and command pool to the command buffer pool. |
| 7094 | frames[i].command_buffer_pool.pool = frames[i].command_pool; |
| 7095 | |
| 7096 | // Create the semaphores for the transfer workers. |
| 7097 | frames[i].transfer_worker_semaphores.resize(transfer_worker_pool_max_size); |
| 7098 | for (uint32_t j = 0; j < transfer_worker_pool_max_size; j++) { |
| 7099 | frames[i].transfer_worker_semaphores[j] = driver->semaphore_create(); |
| 7100 | if (!frames[i].transfer_worker_semaphores[j]) { |
| 7101 | frame_failed = true; |
| 7102 | break; |
| 7103 | } |
| 7104 | } |
| 7105 | } |
| 7106 | if (frame_failed) { |
| 7107 | // Clean up created data. |
| 7108 | for (uint32_t i = 0; i < frames.size(); i++) { |
| 7109 | if (frames[i].command_pool) { |
| 7110 | driver->command_pool_free(frames[i].command_pool); |
| 7111 | } |
| 7112 | if (frames[i].semaphore) { |
| 7113 | driver->semaphore_free(frames[i].semaphore); |
| 7114 | } |
| 7115 | if (frames[i].fence) { |
| 7116 | driver->fence_free(frames[i].fence); |
| 7117 | } |
| 7118 | if (frames[i].timestamp_pool) { |
| 7119 | driver->timestamp_query_pool_free(frames[i].timestamp_pool); |
| 7120 | } |
| 7121 | for (uint32_t j = 0; j < frames[i].transfer_worker_semaphores.size(); j++) { |
| 7122 | if (frames[i].transfer_worker_semaphores[j]) { |
| 7123 | driver->semaphore_free(frames[i].transfer_worker_semaphores[j]); |
| 7124 | } |
| 7125 | } |
| 7126 | } |
| 7127 | frames.clear(); |
| 7128 | ERR_FAIL_V_MSG(FAILED, "Failed to create frame data."); |
| 7129 | } |
| 7130 | |
| 7131 | // Start from frame count, so everything else is immediately old. |
| 7132 | frames_drawn = frames.size(); |
| 7133 | |
| 7134 | // Initialize recording on the first frame. |
| 7135 | driver->begin_segment(frame, frames_drawn++); |
| 7136 | driver->command_buffer_begin(frames[0].command_buffer); |
| 7137 | |
| 7138 | // Create draw graph and start it initialized as well. |
| 7139 | draw_graph.initialize(driver, device, &_render_pass_create_from_graph, frames.size(), main_queue_family, SECONDARY_COMMAND_BUFFERS_PER_FRAME); |
| 7140 | draw_graph.begin(); |
| 7141 | |
| 7142 | for (uint32_t i = 0; i < frames.size(); i++) { |
| 7143 | // Reset all queries in a query pool before doing any operations with them.. |
| 7144 | driver->command_timestamp_query_pool_reset(frames[0].command_buffer, frames[i].timestamp_pool, max_timestamp_query_elements); |
| 7145 | } |
| 7146 | |
| 7147 | // Convert block size from KB. |
| 7148 | upload_staging_buffers.block_size = GLOBAL_GET("rendering/rendering_device/staging_buffer/block_size_kb"); |
| 7149 | upload_staging_buffers.block_size = MAX(4u, upload_staging_buffers.block_size); |
| 7150 | upload_staging_buffers.block_size *= 1024; |
| 7151 | |
| 7152 | // Convert staging buffer size from MB. |
| 7153 | upload_staging_buffers.max_size = GLOBAL_GET("rendering/rendering_device/staging_buffer/max_size_mb"); |
| 7154 | upload_staging_buffers.max_size = MAX(1u, upload_staging_buffers.max_size); |
| 7155 | upload_staging_buffers.max_size *= 1024 * 1024; |
| 7156 | upload_staging_buffers.max_size = MAX(upload_staging_buffers.max_size, upload_staging_buffers.block_size * 4); |
| 7157 | |
| 7158 | // Copy the sizes to the download staging buffers. |
| 7159 | download_staging_buffers.block_size = upload_staging_buffers.block_size; |
| 7160 | download_staging_buffers.max_size = upload_staging_buffers.max_size; |
| 7161 | |
| 7162 | texture_upload_region_size_px = GLOBAL_GET("rendering/rendering_device/staging_buffer/texture_upload_region_size_px"); |
| 7163 | texture_upload_region_size_px = nearest_power_of_2_templated(texture_upload_region_size_px); |
| 7164 | |
| 7165 | texture_download_region_size_px = GLOBAL_GET("rendering/rendering_device/staging_buffer/texture_download_region_size_px"); |
| 7166 | texture_download_region_size_px = nearest_power_of_2_templated(texture_download_region_size_px); |
| 7167 | |
| 7168 | // Ensure current staging block is valid and at least one per frame exists. |
| 7169 | upload_staging_buffers.current = 0; |
| 7170 | upload_staging_buffers.used = false; |
| 7171 | upload_staging_buffers.usage_bits = RDD::BUFFER_USAGE_TRANSFER_FROM_BIT; |
| 7172 | |
| 7173 | download_staging_buffers.current = 0; |
| 7174 | download_staging_buffers.used = false; |
| 7175 | download_staging_buffers.usage_bits = RDD::BUFFER_USAGE_TRANSFER_TO_BIT; |
| 7176 | |
| 7177 | for (uint32_t i = 0; i < frames.size(); i++) { |
| 7178 | // Staging was never used, create the blocks. |
| 7179 | err = _insert_staging_block(upload_staging_buffers); |
| 7180 | ERR_FAIL_COND_V(err, FAILED); |
| 7181 | |
| 7182 | err = _insert_staging_block(download_staging_buffers); |
| 7183 | ERR_FAIL_COND_V(err, FAILED); |
| 7184 | } |
| 7185 | |
| 7186 | draw_list = DrawList(); |
| 7187 | compute_list = ComputeList(); |
| 7188 | |
| 7189 | bool project_pipeline_cache_enable = GLOBAL_GET("rendering/rendering_device/pipeline_cache/enable"); |
| 7190 | if (is_main_instance && project_pipeline_cache_enable) { |
| 7191 | // Only the instance that is not a local device and is also the singleton is allowed to manage a pipeline cache. |
| 7192 | pipeline_cache_file_path = vformat("user://vulkan/pipelines.%s.%s", |
| 7193 | OS::get_singleton()->get_current_rendering_method(), |
| 7194 | device.name.validate_filename().replace_char(' ', '_').to_lower()); |
| 7195 | if (Engine::get_singleton()->is_editor_hint()) { |
| 7196 | pipeline_cache_file_path += ".editor"; |
| 7197 | } |
| 7198 | pipeline_cache_file_path += ".cache"; |
| 7199 | |
| 7200 | Vector<uint8_t> cache_data = _load_pipeline_cache(); |
| 7201 | pipeline_cache_enabled = driver->pipeline_cache_create(cache_data); |
| 7202 | if (pipeline_cache_enabled) { |
| 7203 | pipeline_cache_size = driver->pipeline_cache_query_size(); |
| 7204 | print_verbose(vformat("Startup PSO cache (%.1f MiB)", pipeline_cache_size / (1024.0f * 1024.0f))); |
| 7205 | } |
| 7206 | } |
| 7207 | |
| 7208 | // Find the best method available for VRS on the current hardware. |
| 7209 | _vrs_detect_method(); |
| 7210 | |
| 7211 | return OK; |
| 7212 | } |
| 7213 | |
| 7214 | Vector<uint8_t> RenderingDevice::_load_pipeline_cache() { |
| 7215 | DirAccess::make_dir_recursive_absolute(pipeline_cache_file_path.get_base_dir()); |
| 7216 | |
| 7217 | if (FileAccess::exists(pipeline_cache_file_path)) { |
| 7218 | Error file_error; |
| 7219 | Vector<uint8_t> file_data = FileAccess::get_file_as_bytes(pipeline_cache_file_path, &file_error); |
| 7220 | return file_data; |
| 7221 | } else { |
| 7222 | return Vector<uint8_t>(); |
| 7223 | } |
| 7224 | } |
| 7225 | |
| 7226 | void RenderingDevice::update_pipeline_cache(bool p_closing) { |
| 7227 | _THREAD_SAFE_METHOD_ |
| 7228 | |
| 7229 | { |
| 7230 | bool still_saving = pipeline_cache_save_task != WorkerThreadPool::INVALID_TASK_ID && !WorkerThreadPool::get_singleton()->is_task_completed(pipeline_cache_save_task); |
| 7231 | if (still_saving) { |
| 7232 | if (p_closing) { |
| 7233 | WorkerThreadPool::get_singleton()->wait_for_task_completion(pipeline_cache_save_task); |
| 7234 | pipeline_cache_save_task = WorkerThreadPool::INVALID_TASK_ID; |
| 7235 | } else { |
| 7236 | // We can't save until the currently running save is done. We'll retry next time; worst case, we'll save when exiting. |
| 7237 | return; |
| 7238 | } |
| 7239 | } |
| 7240 | } |
| 7241 | |
| 7242 | { |
| 7243 | size_t new_pipelines_cache_size = driver->pipeline_cache_query_size(); |
| 7244 | ERR_FAIL_COND(!new_pipelines_cache_size); |
| 7245 | size_t difference = new_pipelines_cache_size - pipeline_cache_size; |
| 7246 | |
| 7247 | bool must_save = false; |
| 7248 | |
| 7249 | if (p_closing) { |
| 7250 | must_save = difference > 0; |
| 7251 | } else { |
| 7252 | float save_interval = GLOBAL_GET("rendering/rendering_device/pipeline_cache/save_chunk_size_mb"); |
| 7253 | must_save = difference > 0 && difference / (1024.0f * 1024.0f) >= save_interval; |
| 7254 | } |
| 7255 | |
| 7256 | if (must_save) { |
| 7257 | pipeline_cache_size = new_pipelines_cache_size; |
| 7258 | } else { |
| 7259 | return; |
| 7260 | } |
| 7261 | } |
| 7262 | |
| 7263 | if (p_closing) { |
| 7264 | _save_pipeline_cache(this); |
| 7265 | } else { |
| 7266 | pipeline_cache_save_task = WorkerThreadPool::get_singleton()->add_native_task(&_save_pipeline_cache, this, false, "PipelineCacheSave"); |
| 7267 | } |
| 7268 | } |
| 7269 | |
| 7270 | void RenderingDevice::_save_pipeline_cache(void *p_data) { |
| 7271 | RenderingDevice *self = static_cast<RenderingDevice *>(p_data); |
| 7272 | |
| 7273 | self->_thread_safe_.lock(); |
| 7274 | Vector<uint8_t> cache_blob = self->driver->pipeline_cache_serialize(); |
| 7275 | self->_thread_safe_.unlock(); |
| 7276 | |
| 7277 | if (cache_blob.is_empty()) { |
| 7278 | return; |
| 7279 | } |
| 7280 | print_verbose(vformat("Updated PSO cache (%.1f MiB)", cache_blob.size() / (1024.0f * 1024.0f))); |
| 7281 | |
| 7282 | Ref<FileAccess> f = FileAccess::open(self->pipeline_cache_file_path, FileAccess::WRITE, nullptr); |
| 7283 | if (f.is_valid()) { |
| 7284 | f->store_buffer(cache_blob); |
| 7285 | } |
| 7286 | } |
| 7287 | |
| 7288 | template <typename T> |
| 7289 | void RenderingDevice::_free_rids(T &p_owner, const char *p_type) { |
| 7290 | LocalVector<RID> owned = p_owner.get_owned_list(); |
| 7291 | if (owned.size()) { |
| 7292 | if (owned.size() == 1) { |
| 7293 | WARN_PRINT(vformat("1 RID of type \"%s\" was leaked.", p_type)); |
| 7294 | } else { |
| 7295 | WARN_PRINT(vformat("%d RIDs of type \"%s\" were leaked.", owned.size(), p_type)); |
| 7296 | } |
| 7297 | for (const RID &rid : owned) { |
| 7298 | #ifdef DEV_ENABLED |
| 7299 | if (resource_names.has(rid)) { |
| 7300 | print_line(String(" - ") + resource_names[rid]); |
| 7301 | } |
| 7302 | #endif |
| 7303 | free_rid(rid); |
| 7304 | } |
| 7305 | } |
| 7306 | } |
| 7307 | |
| 7308 | void RenderingDevice::capture_timestamp(const String &p_name) { |
| 7309 | ERR_RENDER_THREAD_GUARD(); |
| 7310 | |
| 7311 | ERR_FAIL_COND_MSG(draw_list.active && draw_list.state.draw_count > 0, "Capturing timestamps during draw list creation is not allowed. Offending timestamp was: " + p_name); |
| 7312 | ERR_FAIL_COND_MSG(compute_list.active && compute_list.state.dispatch_count > 0, "Capturing timestamps during compute list creation is not allowed. Offending timestamp was: " + p_name); |
| 7313 | ERR_FAIL_COND_MSG(frames[frame].timestamp_count >= max_timestamp_query_elements, vformat("Tried capturing more timestamps than the configured maximum (%d). You can increase this limit in the project settings under 'Debug/Settings' called 'Max Timestamp Query Elements'.", max_timestamp_query_elements)); |
| 7314 | |
| 7315 | draw_graph.add_capture_timestamp(frames[frame].timestamp_pool, frames[frame].timestamp_count); |
| 7316 | |
| 7317 | frames[frame].timestamp_names[frames[frame].timestamp_count] = p_name; |
| 7318 | frames[frame].timestamp_cpu_values[frames[frame].timestamp_count] = OS::get_singleton()->get_ticks_usec(); |
| 7319 | frames[frame].timestamp_count++; |
| 7320 | } |
| 7321 | |
| 7322 | uint64_t RenderingDevice::get_driver_resource(DriverResource p_resource, RID p_rid, uint64_t p_index) { |
| 7323 | ERR_RENDER_THREAD_GUARD_V(0); |
| 7324 | |
| 7325 | uint64_t driver_id = 0; |
| 7326 | switch (p_resource) { |
| 7327 | case DRIVER_RESOURCE_LOGICAL_DEVICE: |
| 7328 | case DRIVER_RESOURCE_PHYSICAL_DEVICE: |
| 7329 | case DRIVER_RESOURCE_TOPMOST_OBJECT: |
| 7330 | break; |
| 7331 | case DRIVER_RESOURCE_COMMAND_QUEUE: |
| 7332 | driver_id = main_queue.id; |
| 7333 | break; |
| 7334 | case DRIVER_RESOURCE_QUEUE_FAMILY: |
| 7335 | driver_id = main_queue_family.id; |
| 7336 | break; |
| 7337 | case DRIVER_RESOURCE_TEXTURE: |
| 7338 | case DRIVER_RESOURCE_TEXTURE_VIEW: |
| 7339 | case DRIVER_RESOURCE_TEXTURE_DATA_FORMAT: { |
| 7340 | Texture *tex = texture_owner.get_or_null(p_rid); |
| 7341 | ERR_FAIL_NULL_V(tex, 0); |
| 7342 | |
| 7343 | driver_id = tex->driver_id.id; |
| 7344 | } break; |
| 7345 | case DRIVER_RESOURCE_SAMPLER: { |
| 7346 | RDD::SamplerID *sampler_driver_id = sampler_owner.get_or_null(p_rid); |
| 7347 | ERR_FAIL_NULL_V(sampler_driver_id, 0); |
| 7348 | |
| 7349 | driver_id = (*sampler_driver_id).id; |
| 7350 | } break; |
| 7351 | case DRIVER_RESOURCE_UNIFORM_SET: { |
| 7352 | UniformSet *uniform_set = uniform_set_owner.get_or_null(p_rid); |
| 7353 | ERR_FAIL_NULL_V(uniform_set, 0); |
| 7354 | |
| 7355 | driver_id = uniform_set->driver_id.id; |
| 7356 | } break; |
| 7357 | case DRIVER_RESOURCE_BUFFER: { |
| 7358 | Buffer *buffer = nullptr; |
| 7359 | if (vertex_buffer_owner.owns(p_rid)) { |
| 7360 | buffer = vertex_buffer_owner.get_or_null(p_rid); |
| 7361 | } else if (index_buffer_owner.owns(p_rid)) { |
| 7362 | buffer = index_buffer_owner.get_or_null(p_rid); |
| 7363 | } else if (uniform_buffer_owner.owns(p_rid)) { |
| 7364 | buffer = uniform_buffer_owner.get_or_null(p_rid); |
| 7365 | } else if (texture_buffer_owner.owns(p_rid)) { |
| 7366 | buffer = texture_buffer_owner.get_or_null(p_rid); |
| 7367 | } else if (storage_buffer_owner.owns(p_rid)) { |
| 7368 | buffer = storage_buffer_owner.get_or_null(p_rid); |
| 7369 | } |
| 7370 | ERR_FAIL_NULL_V(buffer, 0); |
| 7371 | |
| 7372 | driver_id = buffer->driver_id.id; |
| 7373 | } break; |
| 7374 | case DRIVER_RESOURCE_COMPUTE_PIPELINE: { |
| 7375 | ComputePipeline *compute_pipeline = compute_pipeline_owner.get_or_null(p_rid); |
| 7376 | ERR_FAIL_NULL_V(compute_pipeline, 0); |
| 7377 | |
| 7378 | driver_id = compute_pipeline->driver_id.id; |
| 7379 | } break; |
| 7380 | case DRIVER_RESOURCE_RENDER_PIPELINE: { |
| 7381 | RenderPipeline *render_pipeline = render_pipeline_owner.get_or_null(p_rid); |
| 7382 | ERR_FAIL_NULL_V(render_pipeline, 0); |
| 7383 | |
| 7384 | driver_id = render_pipeline->driver_id.id; |
| 7385 | } break; |
| 7386 | default: { |
| 7387 | ERR_FAIL_V(0); |
| 7388 | } break; |
| 7389 | } |
| 7390 | |
| 7391 | return driver->get_resource_native_handle(p_resource, driver_id); |
| 7392 | } |
| 7393 | |
| 7394 | String RenderingDevice::get_driver_and_device_memory_report() const { |
| 7395 | return context->get_driver_and_device_memory_report(); |
| 7396 | } |
| 7397 | |
| 7398 | String RenderingDevice::get_tracked_object_name(uint32_t p_type_index) const { |
| 7399 | return context->get_tracked_object_name(p_type_index); |
| 7400 | } |
| 7401 | |
| 7402 | uint64_t RenderingDevice::get_tracked_object_type_count() const { |
| 7403 | return context->get_tracked_object_type_count(); |
| 7404 | } |
| 7405 | |
| 7406 | uint64_t RenderingDevice::get_driver_total_memory() const { |
| 7407 | return context->get_driver_total_memory(); |
| 7408 | } |
| 7409 | |
| 7410 | uint64_t RenderingDevice::get_driver_allocation_count() const { |
| 7411 | return context->get_driver_allocation_count(); |
| 7412 | } |
| 7413 | |
| 7414 | uint64_t RenderingDevice::get_driver_memory_by_object_type(uint32_t p_type) const { |
| 7415 | return context->get_driver_memory_by_object_type(p_type); |
| 7416 | } |
| 7417 | |
| 7418 | uint64_t RenderingDevice::get_driver_allocs_by_object_type(uint32_t p_type) const { |
| 7419 | return context->get_driver_allocs_by_object_type(p_type); |
| 7420 | } |
| 7421 | |
| 7422 | uint64_t RenderingDevice::get_device_total_memory() const { |
| 7423 | return context->get_device_total_memory(); |
| 7424 | } |
| 7425 | |
| 7426 | uint64_t RenderingDevice::get_device_allocation_count() const { |
| 7427 | return context->get_device_allocation_count(); |
| 7428 | } |
| 7429 | |
| 7430 | uint64_t RenderingDevice::get_device_memory_by_object_type(uint32_t type) const { |
| 7431 | return context->get_device_memory_by_object_type(type); |
| 7432 | } |
| 7433 | |
| 7434 | uint64_t RenderingDevice::get_device_allocs_by_object_type(uint32_t type) const { |
| 7435 | return context->get_device_allocs_by_object_type(type); |
| 7436 | } |
| 7437 | |
| 7438 | uint32_t RenderingDevice::get_captured_timestamps_count() const { |
| 7439 | ERR_RENDER_THREAD_GUARD_V(0); |
| 7440 | return frames[frame].timestamp_result_count; |
| 7441 | } |
| 7442 | |
| 7443 | uint64_t RenderingDevice::get_captured_timestamps_frame() const { |
| 7444 | ERR_RENDER_THREAD_GUARD_V(0); |
| 7445 | return frames[frame].index; |
| 7446 | } |
| 7447 | |
| 7448 | uint64_t RenderingDevice::get_captured_timestamp_gpu_time(uint32_t p_index) const { |
| 7449 | ERR_RENDER_THREAD_GUARD_V(0); |
| 7450 | ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, 0); |
| 7451 | return driver->timestamp_query_result_to_time(frames[frame].timestamp_result_values[p_index]); |
| 7452 | } |
| 7453 | |
| 7454 | uint64_t RenderingDevice::get_captured_timestamp_cpu_time(uint32_t p_index) const { |
| 7455 | ERR_RENDER_THREAD_GUARD_V(0); |
| 7456 | ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, 0); |
| 7457 | return frames[frame].timestamp_cpu_result_values[p_index]; |
| 7458 | } |
| 7459 | |
| 7460 | String RenderingDevice::get_captured_timestamp_name(uint32_t p_index) const { |
| 7461 | ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, String()); |
| 7462 | return frames[frame].timestamp_result_names[p_index]; |
| 7463 | } |
| 7464 | |
| 7465 | uint64_t RenderingDevice::limit_get(Limit p_limit) const { |
| 7466 | return driver->limit_get(p_limit); |
| 7467 | } |
| 7468 | |
| 7469 | void RenderingDevice::finalize() { |
| 7470 | ERR_RENDER_THREAD_GUARD(); |
| 7471 | |
| 7472 | if (!frames.is_empty()) { |
| 7473 | // Wait for all frames to have finished rendering. |
| 7474 | _flush_and_stall_for_all_frames(false); |
| 7475 | } |
| 7476 | |
| 7477 | // Wait for transfer workers to finish. |
| 7478 | _submit_transfer_workers(); |
| 7479 | _wait_for_transfer_workers(); |
| 7480 | |
| 7481 | // Delete everything the graph has created. |
| 7482 | draw_graph.finalize(); |
| 7483 | |
| 7484 | // Free all resources. |
| 7485 | _free_rids(render_pipeline_owner, "Pipeline"); |
| 7486 | _free_rids(compute_pipeline_owner, "Compute"); |
| 7487 | _free_rids(uniform_set_owner, "UniformSet"); |
| 7488 | _free_rids(texture_buffer_owner, "TextureBuffer"); |
| 7489 | _free_rids(storage_buffer_owner, "StorageBuffer"); |
| 7490 | _free_rids(uniform_buffer_owner, "UniformBuffer"); |
| 7491 | _free_rids(shader_owner, "Shader"); |
| 7492 | _free_rids(index_array_owner, "IndexArray"); |
| 7493 | _free_rids(index_buffer_owner, "IndexBuffer"); |
| 7494 | _free_rids(vertex_array_owner, "VertexArray"); |
| 7495 | _free_rids(vertex_buffer_owner, "VertexBuffer"); |
| 7496 | _free_rids(framebuffer_owner, "Framebuffer"); |
| 7497 | _free_rids(sampler_owner, "Sampler"); |
| 7498 | { |
| 7499 | // For textures it's a bit more difficult because they may be shared. |
| 7500 | LocalVector<RID> owned = texture_owner.get_owned_list(); |
| 7501 | if (owned.size()) { |
| 7502 | if (owned.size() == 1) { |
| 7503 | WARN_PRINT("1 RID of type \"Texture\" was leaked."); |
| 7504 | } else { |
| 7505 | WARN_PRINT(vformat("%d RIDs of type \"Texture\" were leaked.", owned.size())); |
| 7506 | } |
| 7507 | LocalVector<RID> owned_non_shared; |
| 7508 | // Free shared first. |
| 7509 | for (const RID &texture_rid : owned) { |
| 7510 | if (texture_is_shared(texture_rid)) { |
| 7511 | #ifdef DEV_ENABLED |
| 7512 | if (resource_names.has(texture_rid)) { |
| 7513 | print_line(String(" - ") + resource_names[texture_rid]); |
| 7514 | } |
| 7515 | #endif |
| 7516 | free_rid(texture_rid); |
| 7517 | } else { |
| 7518 | owned_non_shared.push_back(texture_rid); |
| 7519 | } |
| 7520 | } |
| 7521 | // Free non shared second, this will avoid an error trying to free unexisting textures due to dependencies. |
| 7522 | for (const RID &texture_rid : owned_non_shared) { |
| 7523 | #ifdef DEV_ENABLED |
| 7524 | if (resource_names.has(texture_rid)) { |
| 7525 | print_line(String(" - ") + resource_names[texture_rid]); |
| 7526 | } |
| 7527 | #endif |
| 7528 | free_rid(texture_rid); |
| 7529 | } |
| 7530 | } |
| 7531 | } |
| 7532 | |
| 7533 | // Erase the transfer workers after all resources have been freed. |
| 7534 | _free_transfer_workers(); |
| 7535 | |
| 7536 | // Free everything pending. |
| 7537 | for (uint32_t i = 0; i < frames.size(); i++) { |
| 7538 | int f = (frame + i) % frames.size(); |
| 7539 | _free_pending_resources(f); |
| 7540 | driver->command_pool_free(frames[i].command_pool); |
| 7541 | driver->timestamp_query_pool_free(frames[i].timestamp_pool); |
| 7542 | driver->semaphore_free(frames[i].semaphore); |
| 7543 | driver->fence_free(frames[i].fence); |
| 7544 | |
| 7545 | RDG::CommandBufferPool &buffer_pool = frames[i].command_buffer_pool; |
| 7546 | for (uint32_t j = 0; j < buffer_pool.buffers.size(); j++) { |
| 7547 | driver->semaphore_free(buffer_pool.semaphores[j]); |
| 7548 | } |
| 7549 | |
| 7550 | for (uint32_t j = 0; j < frames[i].transfer_worker_semaphores.size(); j++) { |
| 7551 | driver->semaphore_free(frames[i].transfer_worker_semaphores[j]); |
| 7552 | } |
| 7553 | } |
| 7554 | |
| 7555 | if (pipeline_cache_enabled) { |
| 7556 | update_pipeline_cache(true); |
| 7557 | driver->pipeline_cache_free(); |
| 7558 | } |
| 7559 | |
| 7560 | frames.clear(); |
| 7561 | |
| 7562 | for (int i = 0; i < upload_staging_buffers.blocks.size(); i++) { |
| 7563 | driver->buffer_unmap(upload_staging_buffers.blocks[i].driver_id); |
| 7564 | driver->buffer_free(upload_staging_buffers.blocks[i].driver_id); |
| 7565 | } |
| 7566 | |
| 7567 | for (int i = 0; i < download_staging_buffers.blocks.size(); i++) { |
| 7568 | driver->buffer_unmap(download_staging_buffers.blocks[i].driver_id); |
| 7569 | driver->buffer_free(download_staging_buffers.blocks[i].driver_id); |
| 7570 | } |
| 7571 | |
| 7572 | while (vertex_formats.size()) { |
| 7573 | HashMap<VertexFormatID, VertexDescriptionCache>::Iterator temp = vertex_formats.begin(); |
| 7574 | driver->vertex_format_free(temp->value.driver_id); |
| 7575 | vertex_formats.remove(temp); |
| 7576 | } |
| 7577 | |
| 7578 | for (KeyValue<FramebufferFormatID, FramebufferFormat> &E : framebuffer_formats) { |
| 7579 | driver->render_pass_free(E.value.render_pass); |
| 7580 | } |
| 7581 | framebuffer_formats.clear(); |
| 7582 | |
| 7583 | // Delete the swap chains created for the screens. |
| 7584 | for (const KeyValue<DisplayServer::WindowID, RDD::SwapChainID> &it : screen_swap_chains) { |
| 7585 | driver->swap_chain_free(it.value); |
| 7586 | } |
| 7587 | |
| 7588 | screen_swap_chains.clear(); |
| 7589 | |
| 7590 | // Delete the command queues. |
| 7591 | if (present_queue) { |
| 7592 | if (main_queue != present_queue) { |
| 7593 | // Only delete the present queue if it's unique. |
| 7594 | driver->command_queue_free(present_queue); |
| 7595 | } |
| 7596 | |
| 7597 | present_queue = RDD::CommandQueueID(); |
| 7598 | } |
| 7599 | |
| 7600 | if (transfer_queue) { |
| 7601 | if (main_queue != transfer_queue) { |
| 7602 | // Only delete the transfer queue if it's unique. |
| 7603 | driver->command_queue_free(transfer_queue); |
| 7604 | } |
| 7605 | |
| 7606 | transfer_queue = RDD::CommandQueueID(); |
| 7607 | } |
| 7608 | |
| 7609 | if (main_queue) { |
| 7610 | driver->command_queue_free(main_queue); |
| 7611 | main_queue = RDD::CommandQueueID(); |
| 7612 | } |
| 7613 | |
| 7614 | // Delete the driver once everything else has been deleted. |
| 7615 | if (driver != nullptr) { |
| 7616 | context->driver_free(driver); |
| 7617 | driver = nullptr; |
| 7618 | } |
| 7619 | |
| 7620 | // All these should be clear at this point. |
| 7621 | ERR_FAIL_COND(dependency_map.size()); |
| 7622 | ERR_FAIL_COND(reverse_dependency_map.size()); |
| 7623 | } |
| 7624 | |
| 7625 | void RenderingDevice::_set_max_fps(int p_max_fps) { |
| 7626 | for (const KeyValue<DisplayServer::WindowID, RDD::SwapChainID> &it : screen_swap_chains) { |
| 7627 | driver->swap_chain_set_max_fps(it.value, p_max_fps); |
| 7628 | } |
| 7629 | } |
| 7630 | |
| 7631 | RenderingDevice *RenderingDevice::create_local_device() { |
| 7632 | RenderingDevice *rd = memnew(RenderingDevice); |
| 7633 | if (rd->initialize(context) != OK) { |
| 7634 | memdelete(rd); |
| 7635 | return nullptr; |
| 7636 | } |
| 7637 | return rd; |
| 7638 | } |
| 7639 | |
| 7640 | bool RenderingDevice::has_feature(const Features p_feature) const { |
| 7641 | // Some features can be deduced from the capabilities without querying the driver and looking at the capabilities. |
| 7642 | switch (p_feature) { |
| 7643 | case SUPPORTS_MULTIVIEW: { |
| 7644 | const RDD::MultiviewCapabilities &multiview_capabilities = driver->get_multiview_capabilities(); |
| 7645 | return multiview_capabilities.is_supported && multiview_capabilities.max_view_count > 1; |
| 7646 | } |
| 7647 | case SUPPORTS_ATTACHMENT_VRS: { |
| 7648 | const RDD::FragmentShadingRateCapabilities &fsr_capabilities = driver->get_fragment_shading_rate_capabilities(); |
| 7649 | const RDD::FragmentDensityMapCapabilities &fdm_capabilities = driver->get_fragment_density_map_capabilities(); |
| 7650 | return fsr_capabilities.attachment_supported || fdm_capabilities.attachment_supported; |
| 7651 | } |
| 7652 | default: |
| 7653 | return driver->has_feature(p_feature); |
| 7654 | } |
| 7655 | } |
| 7656 | |
| 7657 | void RenderingDevice::_bind_methods() { |
| 7658 | ClassDB::bind_method(D_METHOD("texture_create", "format", "view", "data"), &RenderingDevice::_texture_create, DEFVAL(Array())); |
| 7659 | ClassDB::bind_method(D_METHOD("texture_create_shared", "view", "with_texture"), &RenderingDevice::_texture_create_shared); |
| 7660 | ClassDB::bind_method(D_METHOD("texture_create_shared_from_slice", "view", "with_texture", "layer", "mipmap", "mipmaps", "slice_type"), &RenderingDevice::_texture_create_shared_from_slice, DEFVAL(1), DEFVAL(TEXTURE_SLICE_2D)); |
| 7661 | ClassDB::bind_method(D_METHOD("texture_create_from_extension", "type", "format", "samples", "usage_flags", "image", "width", "height", "depth", "layers", "mipmaps"), &RenderingDevice::texture_create_from_extension, DEFVAL(1)); |
| 7662 | |
| 7663 | ClassDB::bind_method(D_METHOD("texture_update", "texture", "layer", "data"), &RenderingDevice::texture_update); |
| 7664 | ClassDB::bind_method(D_METHOD("texture_get_data", "texture", "layer"), &RenderingDevice::texture_get_data); |
| 7665 | ClassDB::bind_method(D_METHOD("texture_get_data_async", "texture", "layer", "callback"), &RenderingDevice::texture_get_data_async); |
| 7666 | |
| 7667 | ClassDB::bind_method(D_METHOD("texture_is_format_supported_for_usage", "format", "usage_flags"), &RenderingDevice::texture_is_format_supported_for_usage); |
| 7668 | |
| 7669 | ClassDB::bind_method(D_METHOD("texture_is_shared", "texture"), &RenderingDevice::texture_is_shared); |
| 7670 | ClassDB::bind_method(D_METHOD("texture_is_valid", "texture"), &RenderingDevice::texture_is_valid); |
| 7671 | |
| 7672 | ClassDB::bind_method(D_METHOD("texture_set_discardable", "texture", "discardable"), &RenderingDevice::texture_set_discardable); |
| 7673 | ClassDB::bind_method(D_METHOD("texture_is_discardable", "texture"), &RenderingDevice::texture_is_discardable); |
| 7674 | |
| 7675 | ClassDB::bind_method(D_METHOD("texture_copy", "from_texture", "to_texture", "from_pos", "to_pos", "size", "src_mipmap", "dst_mipmap", "src_layer", "dst_layer"), &RenderingDevice::texture_copy); |
| 7676 | ClassDB::bind_method(D_METHOD("texture_clear", "texture", "color", "base_mipmap", "mipmap_count", "base_layer", "layer_count"), &RenderingDevice::texture_clear); |
| 7677 | ClassDB::bind_method(D_METHOD("texture_resolve_multisample", "from_texture", "to_texture"), &RenderingDevice::texture_resolve_multisample); |
| 7678 | |
| 7679 | ClassDB::bind_method(D_METHOD("texture_get_format", "texture"), &RenderingDevice::_texture_get_format); |
| 7680 | #ifndef DISABLE_DEPRECATED |
| 7681 | ClassDB::bind_method(D_METHOD("texture_get_native_handle", "texture"), &RenderingDevice::texture_get_native_handle); |
| 7682 | #endif |
| 7683 | |
| 7684 | ClassDB::bind_method(D_METHOD("framebuffer_format_create", "attachments", "view_count"), &RenderingDevice::_framebuffer_format_create, DEFVAL(1)); |
| 7685 | ClassDB::bind_method(D_METHOD("framebuffer_format_create_multipass", "attachments", "passes", "view_count"), &RenderingDevice::_framebuffer_format_create_multipass, DEFVAL(1)); |
| 7686 | ClassDB::bind_method(D_METHOD("framebuffer_format_create_empty", "samples"), &RenderingDevice::framebuffer_format_create_empty, DEFVAL(TEXTURE_SAMPLES_1)); |
| 7687 | ClassDB::bind_method(D_METHOD("framebuffer_format_get_texture_samples", "format", "render_pass"), &RenderingDevice::framebuffer_format_get_texture_samples, DEFVAL(0)); |
| 7688 | ClassDB::bind_method(D_METHOD("framebuffer_create", "textures", "validate_with_format", "view_count"), &RenderingDevice::_framebuffer_create, DEFVAL(INVALID_FORMAT_ID), DEFVAL(1)); |
| 7689 | ClassDB::bind_method(D_METHOD("framebuffer_create_multipass", "textures", "passes", "validate_with_format", "view_count"), &RenderingDevice::_framebuffer_create_multipass, DEFVAL(INVALID_FORMAT_ID), DEFVAL(1)); |
| 7690 | ClassDB::bind_method(D_METHOD("framebuffer_create_empty", "size", "samples", "validate_with_format"), &RenderingDevice::framebuffer_create_empty, DEFVAL(TEXTURE_SAMPLES_1), DEFVAL(INVALID_FORMAT_ID)); |
| 7691 | ClassDB::bind_method(D_METHOD("framebuffer_get_format", "framebuffer"), &RenderingDevice::framebuffer_get_format); |
| 7692 | ClassDB::bind_method(D_METHOD("framebuffer_is_valid", "framebuffer"), &RenderingDevice::framebuffer_is_valid); |
| 7693 | |
| 7694 | ClassDB::bind_method(D_METHOD("sampler_create", "state"), &RenderingDevice::_sampler_create); |
| 7695 | ClassDB::bind_method(D_METHOD("sampler_is_format_supported_for_filter", "format", "sampler_filter"), &RenderingDevice::sampler_is_format_supported_for_filter); |
| 7696 | |
| 7697 | ClassDB::bind_method(D_METHOD("vertex_buffer_create", "size_bytes", "data", "creation_bits"), &RenderingDevice::_vertex_buffer_create, DEFVAL(Vector<uint8_t>()), DEFVAL(0)); |
| 7698 | ClassDB::bind_method(D_METHOD("vertex_format_create", "vertex_descriptions"), &RenderingDevice::_vertex_format_create); |
| 7699 | ClassDB::bind_method(D_METHOD("vertex_array_create", "vertex_count", "vertex_format", "src_buffers", "offsets"), &RenderingDevice::_vertex_array_create, DEFVAL(Vector<int64_t>())); |
| 7700 | |
| 7701 | ClassDB::bind_method(D_METHOD("index_buffer_create", "size_indices", "format", "data", "use_restart_indices", "creation_bits"), &RenderingDevice::_index_buffer_create, DEFVAL(Vector<uint8_t>()), DEFVAL(false), DEFVAL(0)); |
| 7702 | ClassDB::bind_method(D_METHOD("index_array_create", "index_buffer", "index_offset", "index_count"), &RenderingDevice::index_array_create); |
| 7703 | |
| 7704 | ClassDB::bind_method(D_METHOD("shader_compile_spirv_from_source", "shader_source", "allow_cache"), &RenderingDevice::_shader_compile_spirv_from_source, DEFVAL(true)); |
| 7705 | ClassDB::bind_method(D_METHOD("shader_compile_binary_from_spirv", "spirv_data", "name"), &RenderingDevice::_shader_compile_binary_from_spirv, DEFVAL("")); |
| 7706 | ClassDB::bind_method(D_METHOD("shader_create_from_spirv", "spirv_data", "name"), &RenderingDevice::_shader_create_from_spirv, DEFVAL("")); |
| 7707 | ClassDB::bind_method(D_METHOD("shader_create_from_bytecode", "binary_data", "placeholder_rid"), &RenderingDevice::shader_create_from_bytecode, DEFVAL(RID())); |
| 7708 | ClassDB::bind_method(D_METHOD("shader_create_placeholder"), &RenderingDevice::shader_create_placeholder); |
| 7709 | |
| 7710 | ClassDB::bind_method(D_METHOD("shader_get_vertex_input_attribute_mask", "shader"), &RenderingDevice::shader_get_vertex_input_attribute_mask); |
| 7711 | |
| 7712 | ClassDB::bind_method(D_METHOD("uniform_buffer_create", "size_bytes", "data", "creation_bits"), &RenderingDevice::_uniform_buffer_create, DEFVAL(Vector<uint8_t>()), DEFVAL(0)); |
| 7713 | ClassDB::bind_method(D_METHOD("storage_buffer_create", "size_bytes", "data", "usage", "creation_bits"), &RenderingDevice::_storage_buffer_create, DEFVAL(Vector<uint8_t>()), DEFVAL(0), DEFVAL(0)); |
| 7714 | ClassDB::bind_method(D_METHOD("texture_buffer_create", "size_bytes", "format", "data"), &RenderingDevice::_texture_buffer_create, DEFVAL(Vector<uint8_t>())); |
| 7715 | |
| 7716 | ClassDB::bind_method(D_METHOD("uniform_set_create", "uniforms", "shader", "shader_set"), &RenderingDevice::_uniform_set_create); |
| 7717 | ClassDB::bind_method(D_METHOD("uniform_set_is_valid", "uniform_set"), &RenderingDevice::uniform_set_is_valid); |
| 7718 | |
| 7719 | ClassDB::bind_method(D_METHOD("buffer_copy", "src_buffer", "dst_buffer", "src_offset", "dst_offset", "size"), &RenderingDevice::buffer_copy); |
| 7720 | ClassDB::bind_method(D_METHOD("buffer_update", "buffer", "offset", "size_bytes", "data"), &RenderingDevice::_buffer_update_bind); |
| 7721 | ClassDB::bind_method(D_METHOD("buffer_clear", "buffer", "offset", "size_bytes"), &RenderingDevice::buffer_clear); |
| 7722 | ClassDB::bind_method(D_METHOD("buffer_get_data", "buffer", "offset_bytes", "size_bytes"), &RenderingDevice::buffer_get_data, DEFVAL(0), DEFVAL(0)); |
| 7723 | ClassDB::bind_method(D_METHOD("buffer_get_data_async", "buffer", "callback", "offset_bytes", "size_bytes"), &RenderingDevice::buffer_get_data_async, DEFVAL(0), DEFVAL(0)); |
| 7724 | ClassDB::bind_method(D_METHOD("buffer_get_device_address", "buffer"), &RenderingDevice::buffer_get_device_address); |
| 7725 | |
| 7726 | ClassDB::bind_method(D_METHOD("render_pipeline_create", "shader", "framebuffer_format", "vertex_format", "primitive", "rasterization_state", "multisample_state", "stencil_state", "color_blend_state", "dynamic_state_flags", "for_render_pass", "specialization_constants"), &RenderingDevice::_render_pipeline_create, DEFVAL(0), DEFVAL(0), DEFVAL(TypedArray<RDPipelineSpecializationConstant>())); |
| 7727 | ClassDB::bind_method(D_METHOD("render_pipeline_is_valid", "render_pipeline"), &RenderingDevice::render_pipeline_is_valid); |
| 7728 | |
| 7729 | ClassDB::bind_method(D_METHOD("compute_pipeline_create", "shader", "specialization_constants"), &RenderingDevice::_compute_pipeline_create, DEFVAL(TypedArray<RDPipelineSpecializationConstant>())); |
| 7730 | ClassDB::bind_method(D_METHOD("compute_pipeline_is_valid", "compute_pipeline"), &RenderingDevice::compute_pipeline_is_valid); |
| 7731 | |
| 7732 | ClassDB::bind_method(D_METHOD("screen_get_width", "screen"), &RenderingDevice::screen_get_width, DEFVAL(DisplayServer::MAIN_WINDOW_ID)); |
| 7733 | ClassDB::bind_method(D_METHOD("screen_get_height", "screen"), &RenderingDevice::screen_get_height, DEFVAL(DisplayServer::MAIN_WINDOW_ID)); |
| 7734 | ClassDB::bind_method(D_METHOD("screen_get_framebuffer_format", "screen"), &RenderingDevice::screen_get_framebuffer_format, DEFVAL(DisplayServer::MAIN_WINDOW_ID)); |
| 7735 | |
| 7736 | ClassDB::bind_method(D_METHOD("draw_list_begin_for_screen", "screen", "clear_color"), &RenderingDevice::draw_list_begin_for_screen, DEFVAL(DisplayServer::MAIN_WINDOW_ID), DEFVAL(Color())); |
| 7737 | |
| 7738 | ClassDB::bind_method(D_METHOD("draw_list_begin", "framebuffer", "draw_flags", "clear_color_values", "clear_depth_value", "clear_stencil_value", "region", "breadcrumb"), &RenderingDevice::_draw_list_begin_bind, DEFVAL(DRAW_DEFAULT_ALL), DEFVAL(Vector<Color>()), DEFVAL(1.0), DEFVAL(0), DEFVAL(Rect2()), DEFVAL(0)); |
| 7739 | #ifndef DISABLE_DEPRECATED |
| 7740 | ClassDB::bind_method(D_METHOD("draw_list_begin_split", "framebuffer", "splits", "initial_color_action", "final_color_action", "initial_depth_action", "final_depth_action", "clear_color_values", "clear_depth", "clear_stencil", "region", "storage_textures"), &RenderingDevice::_draw_list_begin_split, DEFVAL(Vector<Color>()), DEFVAL(1.0), DEFVAL(0), DEFVAL(Rect2()), DEFVAL(TypedArray<RID>())); |
| 7741 | #endif |
| 7742 | |
| 7743 | ClassDB::bind_method(D_METHOD("draw_list_set_blend_constants", "draw_list", "color"), &RenderingDevice::draw_list_set_blend_constants); |
| 7744 | ClassDB::bind_method(D_METHOD("draw_list_bind_render_pipeline", "draw_list", "render_pipeline"), &RenderingDevice::draw_list_bind_render_pipeline); |
| 7745 | ClassDB::bind_method(D_METHOD("draw_list_bind_uniform_set", "draw_list", "uniform_set", "set_index"), &RenderingDevice::draw_list_bind_uniform_set); |
| 7746 | ClassDB::bind_method(D_METHOD("draw_list_bind_vertex_array", "draw_list", "vertex_array"), &RenderingDevice::draw_list_bind_vertex_array); |
| 7747 | ClassDB::bind_method(D_METHOD("draw_list_bind_vertex_buffers_format", "draw_list", "vertex_format", "vertex_count", "vertex_buffers", "offsets"), &RenderingDevice::_draw_list_bind_vertex_buffers_format, DEFVAL(Vector<int64_t>())); |
| 7748 | ClassDB::bind_method(D_METHOD("draw_list_bind_index_array", "draw_list", "index_array"), &RenderingDevice::draw_list_bind_index_array); |
| 7749 | ClassDB::bind_method(D_METHOD("draw_list_set_push_constant", "draw_list", "buffer", "size_bytes"), &RenderingDevice::_draw_list_set_push_constant); |
| 7750 | |
| 7751 | ClassDB::bind_method(D_METHOD("draw_list_draw", "draw_list", "use_indices", "instances", "procedural_vertex_count"), &RenderingDevice::draw_list_draw, DEFVAL(0)); |
| 7752 | ClassDB::bind_method(D_METHOD("draw_list_draw_indirect", "draw_list", "use_indices", "buffer", "offset", "draw_count", "stride"), &RenderingDevice::draw_list_draw_indirect, DEFVAL(0), DEFVAL(1), DEFVAL(0)); |
| 7753 | |
| 7754 | ClassDB::bind_method(D_METHOD("draw_list_enable_scissor", "draw_list", "rect"), &RenderingDevice::draw_list_enable_scissor, DEFVAL(Rect2())); |
| 7755 | ClassDB::bind_method(D_METHOD("draw_list_disable_scissor", "draw_list"), &RenderingDevice::draw_list_disable_scissor); |
| 7756 | |
| 7757 | ClassDB::bind_method(D_METHOD("draw_list_switch_to_next_pass"), &RenderingDevice::draw_list_switch_to_next_pass); |
| 7758 | #ifndef DISABLE_DEPRECATED |
| 7759 | ClassDB::bind_method(D_METHOD("draw_list_switch_to_next_pass_split", "splits"), &RenderingDevice::_draw_list_switch_to_next_pass_split); |
| 7760 | #endif |
| 7761 | |
| 7762 | ClassDB::bind_method(D_METHOD("draw_list_end"), &RenderingDevice::draw_list_end); |
| 7763 | |
| 7764 | ClassDB::bind_method(D_METHOD("compute_list_begin"), &RenderingDevice::compute_list_begin); |
| 7765 | ClassDB::bind_method(D_METHOD("compute_list_bind_compute_pipeline", "compute_list", "compute_pipeline"), &RenderingDevice::compute_list_bind_compute_pipeline); |
| 7766 | ClassDB::bind_method(D_METHOD("compute_list_set_push_constant", "compute_list", "buffer", "size_bytes"), &RenderingDevice::_compute_list_set_push_constant); |
| 7767 | ClassDB::bind_method(D_METHOD("compute_list_bind_uniform_set", "compute_list", "uniform_set", "set_index"), &RenderingDevice::compute_list_bind_uniform_set); |
| 7768 | ClassDB::bind_method(D_METHOD("compute_list_dispatch", "compute_list", "x_groups", "y_groups", "z_groups"), &RenderingDevice::compute_list_dispatch); |
| 7769 | ClassDB::bind_method(D_METHOD("compute_list_dispatch_indirect", "compute_list", "buffer", "offset"), &RenderingDevice::compute_list_dispatch_indirect); |
| 7770 | ClassDB::bind_method(D_METHOD("compute_list_add_barrier", "compute_list"), &RenderingDevice::compute_list_add_barrier); |
| 7771 | ClassDB::bind_method(D_METHOD("compute_list_end"), &RenderingDevice::compute_list_end); |
| 7772 | |
| 7773 | ClassDB::bind_method(D_METHOD("free_rid", "rid"), &RenderingDevice::free_rid); |
| 7774 | |
| 7775 | ClassDB::bind_method(D_METHOD("capture_timestamp", "name"), &RenderingDevice::capture_timestamp); |
| 7776 | ClassDB::bind_method(D_METHOD("get_captured_timestamps_count"), &RenderingDevice::get_captured_timestamps_count); |
| 7777 | ClassDB::bind_method(D_METHOD("get_captured_timestamps_frame"), &RenderingDevice::get_captured_timestamps_frame); |
| 7778 | ClassDB::bind_method(D_METHOD("get_captured_timestamp_gpu_time", "index"), &RenderingDevice::get_captured_timestamp_gpu_time); |
| 7779 | ClassDB::bind_method(D_METHOD("get_captured_timestamp_cpu_time", "index"), &RenderingDevice::get_captured_timestamp_cpu_time); |
| 7780 | ClassDB::bind_method(D_METHOD("get_captured_timestamp_name", "index"), &RenderingDevice::get_captured_timestamp_name); |
| 7781 | |
| 7782 | ClassDB::bind_method(D_METHOD("has_feature", "feature"), &RenderingDevice::has_feature); |
| 7783 | ClassDB::bind_method(D_METHOD("limit_get", "limit"), &RenderingDevice::limit_get); |
| 7784 | ClassDB::bind_method(D_METHOD("get_frame_delay"), &RenderingDevice::get_frame_delay); |
| 7785 | ClassDB::bind_method(D_METHOD("submit"), &RenderingDevice::submit); |
| 7786 | ClassDB::bind_method(D_METHOD("sync"), &RenderingDevice::sync); |
| 7787 | |
| 7788 | #ifndef DISABLE_DEPRECATED |
| 7789 | ClassDB::bind_method(D_METHOD("barrier", "from", "to"), &RenderingDevice::barrier, DEFVAL(BARRIER_MASK_ALL_BARRIERS), DEFVAL(BARRIER_MASK_ALL_BARRIERS)); |
| 7790 | ClassDB::bind_method(D_METHOD("full_barrier"), &RenderingDevice::full_barrier); |
| 7791 | #endif |
| 7792 | |
| 7793 | ClassDB::bind_method(D_METHOD("create_local_device"), &RenderingDevice::create_local_device); |
| 7794 | |
| 7795 | ClassDB::bind_method(D_METHOD("set_resource_name", "id", "name"), &RenderingDevice::set_resource_name); |
| 7796 | |
| 7797 | ClassDB::bind_method(D_METHOD("draw_command_begin_label", "name", "color"), &RenderingDevice::_draw_command_begin_label); |
| 7798 | #ifndef DISABLE_DEPRECATED |
| 7799 | ClassDB::bind_method(D_METHOD("draw_command_insert_label", "name", "color"), &RenderingDevice::draw_command_insert_label); |
| 7800 | #endif |
| 7801 | ClassDB::bind_method(D_METHOD("draw_command_end_label"), &RenderingDevice::draw_command_end_label); |
| 7802 | |
| 7803 | ClassDB::bind_method(D_METHOD("get_device_vendor_name"), &RenderingDevice::get_device_vendor_name); |
| 7804 | ClassDB::bind_method(D_METHOD("get_device_name"), &RenderingDevice::get_device_name); |
| 7805 | ClassDB::bind_method(D_METHOD("get_device_pipeline_cache_uuid"), &RenderingDevice::get_device_pipeline_cache_uuid); |
| 7806 | |
| 7807 | ClassDB::bind_method(D_METHOD("get_memory_usage", "type"), &RenderingDevice::get_memory_usage); |
| 7808 | |
| 7809 | ClassDB::bind_method(D_METHOD("get_driver_resource", "resource", "rid", "index"), &RenderingDevice::get_driver_resource); |
| 7810 | |
| 7811 | ClassDB::bind_method(D_METHOD("get_perf_report"), &RenderingDevice::get_perf_report); |
| 7812 | |
| 7813 | ClassDB::bind_method(D_METHOD("get_driver_and_device_memory_report"), &RenderingDevice::get_driver_and_device_memory_report); |
| 7814 | ClassDB::bind_method(D_METHOD("get_tracked_object_name", "type_index"), &RenderingDevice::get_tracked_object_name); |
| 7815 | ClassDB::bind_method(D_METHOD("get_tracked_object_type_count"), &RenderingDevice::get_tracked_object_type_count); |
| 7816 | ClassDB::bind_method(D_METHOD("get_driver_total_memory"), &RenderingDevice::get_driver_total_memory); |
| 7817 | ClassDB::bind_method(D_METHOD("get_driver_allocation_count"), &RenderingDevice::get_driver_allocation_count); |
| 7818 | ClassDB::bind_method(D_METHOD("get_driver_memory_by_object_type", "type"), &RenderingDevice::get_driver_memory_by_object_type); |
| 7819 | ClassDB::bind_method(D_METHOD("get_driver_allocs_by_object_type", "type"), &RenderingDevice::get_driver_allocs_by_object_type); |
| 7820 | ClassDB::bind_method(D_METHOD("get_device_total_memory"), &RenderingDevice::get_device_total_memory); |
| 7821 | ClassDB::bind_method(D_METHOD("get_device_allocation_count"), &RenderingDevice::get_device_allocation_count); |
| 7822 | ClassDB::bind_method(D_METHOD("get_device_memory_by_object_type", "type"), &RenderingDevice::get_device_memory_by_object_type); |
| 7823 | ClassDB::bind_method(D_METHOD("get_device_allocs_by_object_type", "type"), &RenderingDevice::get_device_allocs_by_object_type); |
| 7824 | |
| 7825 | BIND_ENUM_CONSTANT(DEVICE_TYPE_OTHER); |
| 7826 | BIND_ENUM_CONSTANT(DEVICE_TYPE_INTEGRATED_GPU); |
| 7827 | BIND_ENUM_CONSTANT(DEVICE_TYPE_DISCRETE_GPU); |
| 7828 | BIND_ENUM_CONSTANT(DEVICE_TYPE_VIRTUAL_GPU); |
| 7829 | BIND_ENUM_CONSTANT(DEVICE_TYPE_CPU); |
| 7830 | BIND_ENUM_CONSTANT(DEVICE_TYPE_MAX); |
| 7831 | |
| 7832 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_LOGICAL_DEVICE); |
| 7833 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_PHYSICAL_DEVICE); |
| 7834 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_TOPMOST_OBJECT); |
| 7835 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_COMMAND_QUEUE); |
| 7836 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_QUEUE_FAMILY); |
| 7837 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_TEXTURE); |
| 7838 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_TEXTURE_VIEW); |
| 7839 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_TEXTURE_DATA_FORMAT); |
| 7840 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_SAMPLER); |
| 7841 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_UNIFORM_SET); |
| 7842 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_BUFFER); |
| 7843 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_COMPUTE_PIPELINE); |
| 7844 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_RENDER_PIPELINE); |
| 7845 | #ifndef DISABLE_DEPRECATED |
| 7846 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_DEVICE); |
| 7847 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_PHYSICAL_DEVICE); |
| 7848 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_INSTANCE); |
| 7849 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_QUEUE); |
| 7850 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_QUEUE_FAMILY_INDEX); |
| 7851 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_IMAGE); |
| 7852 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_IMAGE_VIEW); |
| 7853 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_IMAGE_NATIVE_TEXTURE_FORMAT); |
| 7854 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_SAMPLER); |
| 7855 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_DESCRIPTOR_SET); |
| 7856 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_BUFFER); |
| 7857 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_COMPUTE_PIPELINE); |
| 7858 | BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_RENDER_PIPELINE); |
| 7859 | #endif |
| 7860 | |
| 7861 | BIND_ENUM_CONSTANT(DATA_FORMAT_R4G4_UNORM_PACK8); |
| 7862 | BIND_ENUM_CONSTANT(DATA_FORMAT_R4G4B4A4_UNORM_PACK16); |
| 7863 | BIND_ENUM_CONSTANT(DATA_FORMAT_B4G4R4A4_UNORM_PACK16); |
| 7864 | BIND_ENUM_CONSTANT(DATA_FORMAT_R5G6B5_UNORM_PACK16); |
| 7865 | BIND_ENUM_CONSTANT(DATA_FORMAT_B5G6R5_UNORM_PACK16); |
| 7866 | BIND_ENUM_CONSTANT(DATA_FORMAT_R5G5B5A1_UNORM_PACK16); |
| 7867 | BIND_ENUM_CONSTANT(DATA_FORMAT_B5G5R5A1_UNORM_PACK16); |
| 7868 | BIND_ENUM_CONSTANT(DATA_FORMAT_A1R5G5B5_UNORM_PACK16); |
| 7869 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8_UNORM); |
| 7870 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8_SNORM); |
| 7871 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8_USCALED); |
| 7872 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8_SSCALED); |
| 7873 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8_UINT); |
| 7874 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8_SINT); |
| 7875 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8_SRGB); |
| 7876 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_UNORM); |
| 7877 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_SNORM); |
| 7878 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_USCALED); |
| 7879 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_SSCALED); |
| 7880 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_UINT); |
| 7881 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_SINT); |
| 7882 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_SRGB); |
| 7883 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_UNORM); |
| 7884 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_SNORM); |
| 7885 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_USCALED); |
| 7886 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_SSCALED); |
| 7887 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_UINT); |
| 7888 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_SINT); |
| 7889 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_SRGB); |
| 7890 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_UNORM); |
| 7891 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_SNORM); |
| 7892 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_USCALED); |
| 7893 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_SSCALED); |
| 7894 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_UINT); |
| 7895 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_SINT); |
| 7896 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_SRGB); |
| 7897 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_UNORM); |
| 7898 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_SNORM); |
| 7899 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_USCALED); |
| 7900 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_SSCALED); |
| 7901 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_UINT); |
| 7902 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_SINT); |
| 7903 | BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_SRGB); |
| 7904 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_UNORM); |
| 7905 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_SNORM); |
| 7906 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_USCALED); |
| 7907 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_SSCALED); |
| 7908 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_UINT); |
| 7909 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_SINT); |
| 7910 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_SRGB); |
| 7911 | BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_UNORM_PACK32); |
| 7912 | BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_SNORM_PACK32); |
| 7913 | BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_USCALED_PACK32); |
| 7914 | BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_SSCALED_PACK32); |
| 7915 | BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_UINT_PACK32); |
| 7916 | BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_SINT_PACK32); |
| 7917 | BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_SRGB_PACK32); |
| 7918 | BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_UNORM_PACK32); |
| 7919 | BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_SNORM_PACK32); |
| 7920 | BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_USCALED_PACK32); |
| 7921 | BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_SSCALED_PACK32); |
| 7922 | BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_UINT_PACK32); |
| 7923 | BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_SINT_PACK32); |
| 7924 | BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_UNORM_PACK32); |
| 7925 | BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_SNORM_PACK32); |
| 7926 | BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_USCALED_PACK32); |
| 7927 | BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_SSCALED_PACK32); |
| 7928 | BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_UINT_PACK32); |
| 7929 | BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_SINT_PACK32); |
| 7930 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16_UNORM); |
| 7931 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16_SNORM); |
| 7932 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16_USCALED); |
| 7933 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16_SSCALED); |
| 7934 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16_UINT); |
| 7935 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16_SINT); |
| 7936 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16_SFLOAT); |
| 7937 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_UNORM); |
| 7938 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_SNORM); |
| 7939 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_USCALED); |
| 7940 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_SSCALED); |
| 7941 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_UINT); |
| 7942 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_SINT); |
| 7943 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_SFLOAT); |
| 7944 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_UNORM); |
| 7945 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_SNORM); |
| 7946 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_USCALED); |
| 7947 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_SSCALED); |
| 7948 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_UINT); |
| 7949 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_SINT); |
| 7950 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_SFLOAT); |
| 7951 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_UNORM); |
| 7952 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_SNORM); |
| 7953 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_USCALED); |
| 7954 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_SSCALED); |
| 7955 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_UINT); |
| 7956 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_SINT); |
| 7957 | BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_SFLOAT); |
| 7958 | BIND_ENUM_CONSTANT(DATA_FORMAT_R32_UINT); |
| 7959 | BIND_ENUM_CONSTANT(DATA_FORMAT_R32_SINT); |
| 7960 | BIND_ENUM_CONSTANT(DATA_FORMAT_R32_SFLOAT); |
| 7961 | BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32_UINT); |
| 7962 | BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32_SINT); |
| 7963 | BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32_SFLOAT); |
| 7964 | BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32_UINT); |
| 7965 | BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32_SINT); |
| 7966 | BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32_SFLOAT); |
| 7967 | BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32A32_UINT); |
| 7968 | BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32A32_SINT); |
| 7969 | BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32A32_SFLOAT); |
| 7970 | BIND_ENUM_CONSTANT(DATA_FORMAT_R64_UINT); |
| 7971 | BIND_ENUM_CONSTANT(DATA_FORMAT_R64_SINT); |
| 7972 | BIND_ENUM_CONSTANT(DATA_FORMAT_R64_SFLOAT); |
| 7973 | BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64_UINT); |
| 7974 | BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64_SINT); |
| 7975 | BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64_SFLOAT); |
| 7976 | BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64_UINT); |
| 7977 | BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64_SINT); |
| 7978 | BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64_SFLOAT); |
| 7979 | BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64A64_UINT); |
| 7980 | BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64A64_SINT); |
| 7981 | BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64A64_SFLOAT); |
| 7982 | BIND_ENUM_CONSTANT(DATA_FORMAT_B10G11R11_UFLOAT_PACK32); |
| 7983 | BIND_ENUM_CONSTANT(DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32); |
| 7984 | BIND_ENUM_CONSTANT(DATA_FORMAT_D16_UNORM); |
| 7985 | BIND_ENUM_CONSTANT(DATA_FORMAT_X8_D24_UNORM_PACK32); |
| 7986 | BIND_ENUM_CONSTANT(DATA_FORMAT_D32_SFLOAT); |
| 7987 | BIND_ENUM_CONSTANT(DATA_FORMAT_S8_UINT); |
| 7988 | BIND_ENUM_CONSTANT(DATA_FORMAT_D16_UNORM_S8_UINT); |
| 7989 | BIND_ENUM_CONSTANT(DATA_FORMAT_D24_UNORM_S8_UINT); |
| 7990 | BIND_ENUM_CONSTANT(DATA_FORMAT_D32_SFLOAT_S8_UINT); |
| 7991 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC1_RGB_UNORM_BLOCK); |
| 7992 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC1_RGB_SRGB_BLOCK); |
| 7993 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC1_RGBA_UNORM_BLOCK); |
| 7994 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC1_RGBA_SRGB_BLOCK); |
| 7995 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC2_UNORM_BLOCK); |
| 7996 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC2_SRGB_BLOCK); |
| 7997 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC3_UNORM_BLOCK); |
| 7998 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC3_SRGB_BLOCK); |
| 7999 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC4_UNORM_BLOCK); |
| 8000 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC4_SNORM_BLOCK); |
| 8001 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC5_UNORM_BLOCK); |
| 8002 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC5_SNORM_BLOCK); |
| 8003 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC6H_UFLOAT_BLOCK); |
| 8004 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC6H_SFLOAT_BLOCK); |
| 8005 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC7_UNORM_BLOCK); |
| 8006 | BIND_ENUM_CONSTANT(DATA_FORMAT_BC7_SRGB_BLOCK); |
| 8007 | BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK); |
| 8008 | BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK); |
| 8009 | BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK); |
| 8010 | BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK); |
| 8011 | BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK); |
| 8012 | BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK); |
| 8013 | BIND_ENUM_CONSTANT(DATA_FORMAT_EAC_R11_UNORM_BLOCK); |
| 8014 | BIND_ENUM_CONSTANT(DATA_FORMAT_EAC_R11_SNORM_BLOCK); |
| 8015 | BIND_ENUM_CONSTANT(DATA_FORMAT_EAC_R11G11_UNORM_BLOCK); |
| 8016 | BIND_ENUM_CONSTANT(DATA_FORMAT_EAC_R11G11_SNORM_BLOCK); |
| 8017 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_4x4_UNORM_BLOCK); |
| 8018 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_4x4_SRGB_BLOCK); |
| 8019 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x4_UNORM_BLOCK); |
| 8020 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x4_SRGB_BLOCK); |
| 8021 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x5_UNORM_BLOCK); |
| 8022 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x5_SRGB_BLOCK); |
| 8023 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x5_UNORM_BLOCK); |
| 8024 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x5_SRGB_BLOCK); |
| 8025 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x6_UNORM_BLOCK); |
| 8026 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x6_SRGB_BLOCK); |
| 8027 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x5_UNORM_BLOCK); |
| 8028 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x5_SRGB_BLOCK); |
| 8029 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x6_UNORM_BLOCK); |
| 8030 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x6_SRGB_BLOCK); |
| 8031 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x8_UNORM_BLOCK); |
| 8032 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x8_SRGB_BLOCK); |
| 8033 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x5_UNORM_BLOCK); |
| 8034 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x5_SRGB_BLOCK); |
| 8035 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x6_UNORM_BLOCK); |
| 8036 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x6_SRGB_BLOCK); |
| 8037 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x8_UNORM_BLOCK); |
| 8038 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x8_SRGB_BLOCK); |
| 8039 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x10_UNORM_BLOCK); |
| 8040 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x10_SRGB_BLOCK); |
| 8041 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x10_UNORM_BLOCK); |
| 8042 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x10_SRGB_BLOCK); |
| 8043 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x12_UNORM_BLOCK); |
| 8044 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x12_SRGB_BLOCK); |
| 8045 | BIND_ENUM_CONSTANT(DATA_FORMAT_G8B8G8R8_422_UNORM); |
| 8046 | BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8G8_422_UNORM); |
| 8047 | BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8_R8_3PLANE_420_UNORM); |
| 8048 | BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8R8_2PLANE_420_UNORM); |
| 8049 | BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8_R8_3PLANE_422_UNORM); |
| 8050 | BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8R8_2PLANE_422_UNORM); |
| 8051 | BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8_R8_3PLANE_444_UNORM); |
| 8052 | BIND_ENUM_CONSTANT(DATA_FORMAT_R10X6_UNORM_PACK16); |
| 8053 | BIND_ENUM_CONSTANT(DATA_FORMAT_R10X6G10X6_UNORM_2PACK16); |
| 8054 | BIND_ENUM_CONSTANT(DATA_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16); |
| 8055 | BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16); |
| 8056 | BIND_ENUM_CONSTANT(DATA_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16); |
| 8057 | BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16); |
| 8058 | BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16); |
| 8059 | BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16); |
| 8060 | BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16); |
| 8061 | BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16); |
| 8062 | BIND_ENUM_CONSTANT(DATA_FORMAT_R12X4_UNORM_PACK16); |
| 8063 | BIND_ENUM_CONSTANT(DATA_FORMAT_R12X4G12X4_UNORM_2PACK16); |
| 8064 | BIND_ENUM_CONSTANT(DATA_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16); |
| 8065 | BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16); |
| 8066 | BIND_ENUM_CONSTANT(DATA_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16); |
| 8067 | BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16); |
| 8068 | BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16); |
| 8069 | BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16); |
| 8070 | BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16); |
| 8071 | BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16); |
| 8072 | BIND_ENUM_CONSTANT(DATA_FORMAT_G16B16G16R16_422_UNORM); |
| 8073 | BIND_ENUM_CONSTANT(DATA_FORMAT_B16G16R16G16_422_UNORM); |
| 8074 | BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16_R16_3PLANE_420_UNORM); |
| 8075 | BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16R16_2PLANE_420_UNORM); |
| 8076 | BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16_R16_3PLANE_422_UNORM); |
| 8077 | BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16R16_2PLANE_422_UNORM); |
| 8078 | BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16_R16_3PLANE_444_UNORM); |
| 8079 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_4x4_SFLOAT_BLOCK); |
| 8080 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x4_SFLOAT_BLOCK); |
| 8081 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x5_SFLOAT_BLOCK); |
| 8082 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x5_SFLOAT_BLOCK); |
| 8083 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x6_SFLOAT_BLOCK); |
| 8084 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x5_SFLOAT_BLOCK); |
| 8085 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x6_SFLOAT_BLOCK); |
| 8086 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x8_SFLOAT_BLOCK); |
| 8087 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x5_SFLOAT_BLOCK); |
| 8088 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x6_SFLOAT_BLOCK); |
| 8089 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x8_SFLOAT_BLOCK); |
| 8090 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x10_SFLOAT_BLOCK); |
| 8091 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x10_SFLOAT_BLOCK); |
| 8092 | BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x12_SFLOAT_BLOCK); |
| 8093 | BIND_ENUM_CONSTANT(DATA_FORMAT_MAX); |
| 8094 | |
| 8095 | #ifndef DISABLE_DEPRECATED |
| 8096 | BIND_BITFIELD_FLAG(BARRIER_MASK_VERTEX); |
| 8097 | BIND_BITFIELD_FLAG(BARRIER_MASK_FRAGMENT); |
| 8098 | BIND_BITFIELD_FLAG(BARRIER_MASK_COMPUTE); |
| 8099 | BIND_BITFIELD_FLAG(BARRIER_MASK_TRANSFER); |
| 8100 | BIND_BITFIELD_FLAG(BARRIER_MASK_RASTER); |
| 8101 | BIND_BITFIELD_FLAG(BARRIER_MASK_ALL_BARRIERS); |
| 8102 | BIND_BITFIELD_FLAG(BARRIER_MASK_NO_BARRIER); |
| 8103 | #endif |
| 8104 | |
| 8105 | BIND_ENUM_CONSTANT(TEXTURE_TYPE_1D); |
| 8106 | BIND_ENUM_CONSTANT(TEXTURE_TYPE_2D); |
| 8107 | BIND_ENUM_CONSTANT(TEXTURE_TYPE_3D); |
| 8108 | BIND_ENUM_CONSTANT(TEXTURE_TYPE_CUBE); |
| 8109 | BIND_ENUM_CONSTANT(TEXTURE_TYPE_1D_ARRAY); |
| 8110 | BIND_ENUM_CONSTANT(TEXTURE_TYPE_2D_ARRAY); |
| 8111 | BIND_ENUM_CONSTANT(TEXTURE_TYPE_CUBE_ARRAY); |
| 8112 | BIND_ENUM_CONSTANT(TEXTURE_TYPE_MAX); |
| 8113 | |
| 8114 | BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_1); |
| 8115 | BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_2); |
| 8116 | BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_4); |
| 8117 | BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_8); |
| 8118 | BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_16); |
| 8119 | BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_32); |
| 8120 | BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_64); |
| 8121 | BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_MAX); |
| 8122 | |
| 8123 | BIND_BITFIELD_FLAG(TEXTURE_USAGE_SAMPLING_BIT); |
| 8124 | BIND_BITFIELD_FLAG(TEXTURE_USAGE_COLOR_ATTACHMENT_BIT); |
| 8125 | BIND_BITFIELD_FLAG(TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT); |
| 8126 | BIND_BITFIELD_FLAG(TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT); |
| 8127 | BIND_BITFIELD_FLAG(TEXTURE_USAGE_STORAGE_BIT); |
| 8128 | BIND_BITFIELD_FLAG(TEXTURE_USAGE_STORAGE_ATOMIC_BIT); |
| 8129 | BIND_BITFIELD_FLAG(TEXTURE_USAGE_CPU_READ_BIT); |
| 8130 | BIND_BITFIELD_FLAG(TEXTURE_USAGE_CAN_UPDATE_BIT); |
| 8131 | BIND_BITFIELD_FLAG(TEXTURE_USAGE_CAN_COPY_FROM_BIT); |
| 8132 | BIND_BITFIELD_FLAG(TEXTURE_USAGE_CAN_COPY_TO_BIT); |
| 8133 | BIND_BITFIELD_FLAG(TEXTURE_USAGE_INPUT_ATTACHMENT_BIT); |
| 8134 | |
| 8135 | BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_IDENTITY); |
| 8136 | BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_ZERO); |
| 8137 | BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_ONE); |
| 8138 | BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_R); |
| 8139 | BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_G); |
| 8140 | BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_B); |
| 8141 | BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_A); |
| 8142 | BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_MAX); |
| 8143 | |
| 8144 | BIND_ENUM_CONSTANT(TEXTURE_SLICE_2D); |
| 8145 | BIND_ENUM_CONSTANT(TEXTURE_SLICE_CUBEMAP); |
| 8146 | BIND_ENUM_CONSTANT(TEXTURE_SLICE_3D); |
| 8147 | |
| 8148 | BIND_ENUM_CONSTANT(SAMPLER_FILTER_NEAREST); |
| 8149 | BIND_ENUM_CONSTANT(SAMPLER_FILTER_LINEAR); |
| 8150 | BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_REPEAT); |
| 8151 | BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_MIRRORED_REPEAT); |
| 8152 | BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE); |
| 8153 | BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_CLAMP_TO_BORDER); |
| 8154 | BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_MIRROR_CLAMP_TO_EDGE); |
| 8155 | BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_MAX); |
| 8156 | |
| 8157 | BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK); |
| 8158 | BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_INT_TRANSPARENT_BLACK); |
| 8159 | BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_FLOAT_OPAQUE_BLACK); |
| 8160 | BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_INT_OPAQUE_BLACK); |
| 8161 | BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_FLOAT_OPAQUE_WHITE); |
| 8162 | BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_INT_OPAQUE_WHITE); |
| 8163 | BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_MAX); |
| 8164 | |
| 8165 | BIND_ENUM_CONSTANT(VERTEX_FREQUENCY_VERTEX); |
| 8166 | BIND_ENUM_CONSTANT(VERTEX_FREQUENCY_INSTANCE); |
| 8167 | |
| 8168 | BIND_ENUM_CONSTANT(INDEX_BUFFER_FORMAT_UINT16); |
| 8169 | BIND_ENUM_CONSTANT(INDEX_BUFFER_FORMAT_UINT32); |
| 8170 | |
| 8171 | BIND_BITFIELD_FLAG(STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT); |
| 8172 | |
| 8173 | BIND_BITFIELD_FLAG(BUFFER_CREATION_DEVICE_ADDRESS_BIT); |
| 8174 | BIND_BITFIELD_FLAG(BUFFER_CREATION_AS_STORAGE_BIT); |
| 8175 | // Not exposed on purpose. This flag is too dangerous to be exposed to regular GD users. |
| 8176 | //BIND_BITFIELD_FLAG(BUFFER_CREATION_DYNAMIC_PERSISTENT_BIT); |
| 8177 | |
| 8178 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_SAMPLER); //for sampling only (sampler GLSL type) |
| 8179 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_SAMPLER_WITH_TEXTURE); // for sampling only); but includes a texture); (samplerXX GLSL type)); first a sampler then a texture |
| 8180 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_TEXTURE); //only texture); (textureXX GLSL type) |
| 8181 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_IMAGE); // storage image (imageXX GLSL type)); for compute mostly |
| 8182 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_TEXTURE_BUFFER); // buffer texture (or TBO); textureBuffer type) |
| 8183 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_SAMPLER_WITH_TEXTURE_BUFFER); // buffer texture with a sampler(or TBO); samplerBuffer type) |
| 8184 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_IMAGE_BUFFER); //texel buffer); (imageBuffer type)); for compute mostly |
| 8185 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_UNIFORM_BUFFER); //regular uniform buffer (or UBO). |
| 8186 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_STORAGE_BUFFER); //storage buffer ("buffer" qualifier) like UBO); but supports storage); for compute mostly |
| 8187 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_INPUT_ATTACHMENT); //used for sub-pass read/write); for mobile mostly |
| 8188 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_UNIFORM_BUFFER_DYNAMIC); // Exposed in case a BUFFER_CREATION_DYNAMIC_PERSISTENT_BIT buffer created by C++ makes it into GD users. |
| 8189 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_STORAGE_BUFFER_DYNAMIC); // Exposed in case a BUFFER_CREATION_DYNAMIC_PERSISTENT_BIT buffer created by C++ makes it into GD users. |
| 8190 | BIND_ENUM_CONSTANT(UNIFORM_TYPE_MAX); |
| 8191 | |
| 8192 | BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_POINTS); |
| 8193 | BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_LINES); |
| 8194 | BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_LINES_WITH_ADJACENCY); |
| 8195 | BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_LINESTRIPS); |
| 8196 | BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_LINESTRIPS_WITH_ADJACENCY); |
| 8197 | BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLES); |
| 8198 | BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLES_WITH_ADJACENCY); |
| 8199 | BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLE_STRIPS); |
| 8200 | BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLE_STRIPS_WITH_AJACENCY); |
| 8201 | BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLE_STRIPS_WITH_RESTART_INDEX); |
| 8202 | BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TESSELATION_PATCH); |
| 8203 | BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_MAX); |
| 8204 | |
| 8205 | BIND_ENUM_CONSTANT(POLYGON_CULL_DISABLED); |
| 8206 | BIND_ENUM_CONSTANT(POLYGON_CULL_FRONT); |
| 8207 | BIND_ENUM_CONSTANT(POLYGON_CULL_BACK); |
| 8208 | |
| 8209 | BIND_ENUM_CONSTANT(POLYGON_FRONT_FACE_CLOCKWISE); |
| 8210 | BIND_ENUM_CONSTANT(POLYGON_FRONT_FACE_COUNTER_CLOCKWISE); |
| 8211 | |
| 8212 | BIND_ENUM_CONSTANT(STENCIL_OP_KEEP); |
| 8213 | BIND_ENUM_CONSTANT(STENCIL_OP_ZERO); |
| 8214 | BIND_ENUM_CONSTANT(STENCIL_OP_REPLACE); |
| 8215 | BIND_ENUM_CONSTANT(STENCIL_OP_INCREMENT_AND_CLAMP); |
| 8216 | BIND_ENUM_CONSTANT(STENCIL_OP_DECREMENT_AND_CLAMP); |
| 8217 | BIND_ENUM_CONSTANT(STENCIL_OP_INVERT); |
| 8218 | BIND_ENUM_CONSTANT(STENCIL_OP_INCREMENT_AND_WRAP); |
| 8219 | BIND_ENUM_CONSTANT(STENCIL_OP_DECREMENT_AND_WRAP); |
| 8220 | BIND_ENUM_CONSTANT(STENCIL_OP_MAX); //not an actual operator); just the amount of operators :D |
| 8221 | |
| 8222 | BIND_ENUM_CONSTANT(COMPARE_OP_NEVER); |
| 8223 | BIND_ENUM_CONSTANT(COMPARE_OP_LESS); |
| 8224 | BIND_ENUM_CONSTANT(COMPARE_OP_EQUAL); |
| 8225 | BIND_ENUM_CONSTANT(COMPARE_OP_LESS_OR_EQUAL); |
| 8226 | BIND_ENUM_CONSTANT(COMPARE_OP_GREATER); |
| 8227 | BIND_ENUM_CONSTANT(COMPARE_OP_NOT_EQUAL); |
| 8228 | BIND_ENUM_CONSTANT(COMPARE_OP_GREATER_OR_EQUAL); |
| 8229 | BIND_ENUM_CONSTANT(COMPARE_OP_ALWAYS); |
| 8230 | BIND_ENUM_CONSTANT(COMPARE_OP_MAX); |
| 8231 | |
| 8232 | BIND_ENUM_CONSTANT(LOGIC_OP_CLEAR); |
| 8233 | BIND_ENUM_CONSTANT(LOGIC_OP_AND); |
| 8234 | BIND_ENUM_CONSTANT(LOGIC_OP_AND_REVERSE); |
| 8235 | BIND_ENUM_CONSTANT(LOGIC_OP_COPY); |
| 8236 | BIND_ENUM_CONSTANT(LOGIC_OP_AND_INVERTED); |
| 8237 | BIND_ENUM_CONSTANT(LOGIC_OP_NO_OP); |
| 8238 | BIND_ENUM_CONSTANT(LOGIC_OP_XOR); |
| 8239 | BIND_ENUM_CONSTANT(LOGIC_OP_OR); |
| 8240 | BIND_ENUM_CONSTANT(LOGIC_OP_NOR); |
| 8241 | BIND_ENUM_CONSTANT(LOGIC_OP_EQUIVALENT); |
| 8242 | BIND_ENUM_CONSTANT(LOGIC_OP_INVERT); |
| 8243 | BIND_ENUM_CONSTANT(LOGIC_OP_OR_REVERSE); |
| 8244 | BIND_ENUM_CONSTANT(LOGIC_OP_COPY_INVERTED); |
| 8245 | BIND_ENUM_CONSTANT(LOGIC_OP_OR_INVERTED); |
| 8246 | BIND_ENUM_CONSTANT(LOGIC_OP_NAND); |
| 8247 | BIND_ENUM_CONSTANT(LOGIC_OP_SET); |
| 8248 | BIND_ENUM_CONSTANT(LOGIC_OP_MAX); //not an actual operator); just the amount of operators :D |
| 8249 | |
| 8250 | BIND_ENUM_CONSTANT(BLEND_FACTOR_ZERO); |
| 8251 | BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE); |
| 8252 | BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC_COLOR); |
| 8253 | BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_SRC_COLOR); |
| 8254 | BIND_ENUM_CONSTANT(BLEND_FACTOR_DST_COLOR); |
| 8255 | BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_DST_COLOR); |
| 8256 | BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC_ALPHA); |
| 8257 | BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_SRC_ALPHA); |
| 8258 | BIND_ENUM_CONSTANT(BLEND_FACTOR_DST_ALPHA); |
| 8259 | BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_DST_ALPHA); |
| 8260 | BIND_ENUM_CONSTANT(BLEND_FACTOR_CONSTANT_COLOR); |
| 8261 | BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR); |
| 8262 | BIND_ENUM_CONSTANT(BLEND_FACTOR_CONSTANT_ALPHA); |
| 8263 | BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA); |
| 8264 | BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC_ALPHA_SATURATE); |
| 8265 | BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC1_COLOR); |
| 8266 | BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_SRC1_COLOR); |
| 8267 | BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC1_ALPHA); |
| 8268 | BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA); |
| 8269 | BIND_ENUM_CONSTANT(BLEND_FACTOR_MAX); |
| 8270 | |
| 8271 | BIND_ENUM_CONSTANT(BLEND_OP_ADD); |
| 8272 | BIND_ENUM_CONSTANT(BLEND_OP_SUBTRACT); |
| 8273 | BIND_ENUM_CONSTANT(BLEND_OP_REVERSE_SUBTRACT); |
| 8274 | BIND_ENUM_CONSTANT(BLEND_OP_MINIMUM); |
| 8275 | BIND_ENUM_CONSTANT(BLEND_OP_MAXIMUM); |
| 8276 | BIND_ENUM_CONSTANT(BLEND_OP_MAX); |
| 8277 | |
| 8278 | BIND_BITFIELD_FLAG(DYNAMIC_STATE_LINE_WIDTH); |
| 8279 | BIND_BITFIELD_FLAG(DYNAMIC_STATE_DEPTH_BIAS); |
| 8280 | BIND_BITFIELD_FLAG(DYNAMIC_STATE_BLEND_CONSTANTS); |
| 8281 | BIND_BITFIELD_FLAG(DYNAMIC_STATE_DEPTH_BOUNDS); |
| 8282 | BIND_BITFIELD_FLAG(DYNAMIC_STATE_STENCIL_COMPARE_MASK); |
| 8283 | BIND_BITFIELD_FLAG(DYNAMIC_STATE_STENCIL_WRITE_MASK); |
| 8284 | BIND_BITFIELD_FLAG(DYNAMIC_STATE_STENCIL_REFERENCE); |
| 8285 | |
| 8286 | #ifndef DISABLE_DEPRECATED |
| 8287 | BIND_ENUM_CONSTANT(INITIAL_ACTION_LOAD); |
| 8288 | BIND_ENUM_CONSTANT(INITIAL_ACTION_CLEAR); |
| 8289 | BIND_ENUM_CONSTANT(INITIAL_ACTION_DISCARD); |
| 8290 | BIND_ENUM_CONSTANT(INITIAL_ACTION_MAX); |
| 8291 | BIND_ENUM_CONSTANT(INITIAL_ACTION_CLEAR_REGION); |
| 8292 | BIND_ENUM_CONSTANT(INITIAL_ACTION_CLEAR_REGION_CONTINUE); |
| 8293 | BIND_ENUM_CONSTANT(INITIAL_ACTION_KEEP); |
| 8294 | BIND_ENUM_CONSTANT(INITIAL_ACTION_DROP); |
| 8295 | BIND_ENUM_CONSTANT(INITIAL_ACTION_CONTINUE); |
| 8296 | |
| 8297 | BIND_ENUM_CONSTANT(FINAL_ACTION_STORE); |
| 8298 | BIND_ENUM_CONSTANT(FINAL_ACTION_DISCARD); |
| 8299 | BIND_ENUM_CONSTANT(FINAL_ACTION_MAX); |
| 8300 | BIND_ENUM_CONSTANT(FINAL_ACTION_READ); |
| 8301 | BIND_ENUM_CONSTANT(FINAL_ACTION_CONTINUE); |
| 8302 | #endif |
| 8303 | |
| 8304 | BIND_ENUM_CONSTANT(SHADER_STAGE_VERTEX); |
| 8305 | BIND_ENUM_CONSTANT(SHADER_STAGE_FRAGMENT); |
| 8306 | BIND_ENUM_CONSTANT(SHADER_STAGE_TESSELATION_CONTROL); |
| 8307 | BIND_ENUM_CONSTANT(SHADER_STAGE_TESSELATION_EVALUATION); |
| 8308 | BIND_ENUM_CONSTANT(SHADER_STAGE_COMPUTE); |
| 8309 | BIND_ENUM_CONSTANT(SHADER_STAGE_MAX); |
| 8310 | BIND_ENUM_CONSTANT(SHADER_STAGE_VERTEX_BIT); |
| 8311 | BIND_ENUM_CONSTANT(SHADER_STAGE_FRAGMENT_BIT); |
| 8312 | BIND_ENUM_CONSTANT(SHADER_STAGE_TESSELATION_CONTROL_BIT); |
| 8313 | BIND_ENUM_CONSTANT(SHADER_STAGE_TESSELATION_EVALUATION_BIT); |
| 8314 | BIND_ENUM_CONSTANT(SHADER_STAGE_COMPUTE_BIT); |
| 8315 | |
| 8316 | BIND_ENUM_CONSTANT(SHADER_LANGUAGE_GLSL); |
| 8317 | BIND_ENUM_CONSTANT(SHADER_LANGUAGE_HLSL); |
| 8318 | |
| 8319 | BIND_ENUM_CONSTANT(PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL); |
| 8320 | BIND_ENUM_CONSTANT(PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT); |
| 8321 | BIND_ENUM_CONSTANT(PIPELINE_SPECIALIZATION_CONSTANT_TYPE_FLOAT); |
| 8322 | |
| 8323 | BIND_ENUM_CONSTANT(SUPPORTS_METALFX_SPATIAL); |
| 8324 | BIND_ENUM_CONSTANT(SUPPORTS_METALFX_TEMPORAL); |
| 8325 | BIND_ENUM_CONSTANT(SUPPORTS_BUFFER_DEVICE_ADDRESS); |
| 8326 | BIND_ENUM_CONSTANT(SUPPORTS_IMAGE_ATOMIC_32_BIT); |
| 8327 | |
| 8328 | BIND_ENUM_CONSTANT(LIMIT_MAX_BOUND_UNIFORM_SETS); |
| 8329 | BIND_ENUM_CONSTANT(LIMIT_MAX_FRAMEBUFFER_COLOR_ATTACHMENTS); |
| 8330 | BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURES_PER_UNIFORM_SET); |
| 8331 | BIND_ENUM_CONSTANT(LIMIT_MAX_SAMPLERS_PER_UNIFORM_SET); |
| 8332 | BIND_ENUM_CONSTANT(LIMIT_MAX_STORAGE_BUFFERS_PER_UNIFORM_SET); |
| 8333 | BIND_ENUM_CONSTANT(LIMIT_MAX_STORAGE_IMAGES_PER_UNIFORM_SET); |
| 8334 | BIND_ENUM_CONSTANT(LIMIT_MAX_UNIFORM_BUFFERS_PER_UNIFORM_SET); |
| 8335 | BIND_ENUM_CONSTANT(LIMIT_MAX_DRAW_INDEXED_INDEX); |
| 8336 | BIND_ENUM_CONSTANT(LIMIT_MAX_FRAMEBUFFER_HEIGHT); |
| 8337 | BIND_ENUM_CONSTANT(LIMIT_MAX_FRAMEBUFFER_WIDTH); |
| 8338 | BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_ARRAY_LAYERS); |
| 8339 | BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_SIZE_1D); |
| 8340 | BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_SIZE_2D); |
| 8341 | BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_SIZE_3D); |
| 8342 | BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_SIZE_CUBE); |
| 8343 | BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURES_PER_SHADER_STAGE); |
| 8344 | BIND_ENUM_CONSTANT(LIMIT_MAX_SAMPLERS_PER_SHADER_STAGE); |
| 8345 | BIND_ENUM_CONSTANT(LIMIT_MAX_STORAGE_BUFFERS_PER_SHADER_STAGE); |
| 8346 | BIND_ENUM_CONSTANT(LIMIT_MAX_STORAGE_IMAGES_PER_SHADER_STAGE); |
| 8347 | BIND_ENUM_CONSTANT(LIMIT_MAX_UNIFORM_BUFFERS_PER_SHADER_STAGE); |
| 8348 | BIND_ENUM_CONSTANT(LIMIT_MAX_PUSH_CONSTANT_SIZE); |
| 8349 | BIND_ENUM_CONSTANT(LIMIT_MAX_UNIFORM_BUFFER_SIZE); |
| 8350 | BIND_ENUM_CONSTANT(LIMIT_MAX_VERTEX_INPUT_ATTRIBUTE_OFFSET); |
| 8351 | BIND_ENUM_CONSTANT(LIMIT_MAX_VERTEX_INPUT_ATTRIBUTES); |
| 8352 | BIND_ENUM_CONSTANT(LIMIT_MAX_VERTEX_INPUT_BINDINGS); |
| 8353 | BIND_ENUM_CONSTANT(LIMIT_MAX_VERTEX_INPUT_BINDING_STRIDE); |
| 8354 | BIND_ENUM_CONSTANT(LIMIT_MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT); |
| 8355 | BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_SHARED_MEMORY_SIZE); |
| 8356 | BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X); |
| 8357 | BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Y); |
| 8358 | BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Z); |
| 8359 | BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_INVOCATIONS); |
| 8360 | BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_X); |
| 8361 | BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_Y); |
| 8362 | BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_Z); |
| 8363 | BIND_ENUM_CONSTANT(LIMIT_MAX_VIEWPORT_DIMENSIONS_X); |
| 8364 | BIND_ENUM_CONSTANT(LIMIT_MAX_VIEWPORT_DIMENSIONS_Y); |
| 8365 | BIND_ENUM_CONSTANT(LIMIT_METALFX_TEMPORAL_SCALER_MIN_SCALE); |
| 8366 | BIND_ENUM_CONSTANT(LIMIT_METALFX_TEMPORAL_SCALER_MAX_SCALE); |
| 8367 | |
| 8368 | BIND_ENUM_CONSTANT(MEMORY_TEXTURES); |
| 8369 | BIND_ENUM_CONSTANT(MEMORY_BUFFERS); |
| 8370 | BIND_ENUM_CONSTANT(MEMORY_TOTAL); |
| 8371 | |
| 8372 | BIND_CONSTANT(INVALID_ID); |
| 8373 | BIND_CONSTANT(INVALID_FORMAT_ID); |
| 8374 | |
| 8375 | BIND_ENUM_CONSTANT(NONE); |
| 8376 | BIND_ENUM_CONSTANT(REFLECTION_PROBES); |
| 8377 | BIND_ENUM_CONSTANT(SKY_PASS); |
| 8378 | BIND_ENUM_CONSTANT(LIGHTMAPPER_PASS); |
| 8379 | BIND_ENUM_CONSTANT(SHADOW_PASS_DIRECTIONAL); |
| 8380 | BIND_ENUM_CONSTANT(SHADOW_PASS_CUBE); |
| 8381 | BIND_ENUM_CONSTANT(OPAQUE_PASS); |
| 8382 | BIND_ENUM_CONSTANT(ALPHA_PASS); |
| 8383 | BIND_ENUM_CONSTANT(TRANSPARENT_PASS); |
| 8384 | BIND_ENUM_CONSTANT(POST_PROCESSING_PASS); |
| 8385 | BIND_ENUM_CONSTANT(BLIT_PASS); |
| 8386 | BIND_ENUM_CONSTANT(UI_PASS); |
| 8387 | BIND_ENUM_CONSTANT(DEBUG_PASS); |
| 8388 | |
| 8389 | BIND_BITFIELD_FLAG(DRAW_DEFAULT_ALL); |
| 8390 | BIND_BITFIELD_FLAG(DRAW_CLEAR_COLOR_0); |
| 8391 | BIND_BITFIELD_FLAG(DRAW_CLEAR_COLOR_1); |
| 8392 | BIND_BITFIELD_FLAG(DRAW_CLEAR_COLOR_2); |
| 8393 | BIND_BITFIELD_FLAG(DRAW_CLEAR_COLOR_3); |
| 8394 | BIND_BITFIELD_FLAG(DRAW_CLEAR_COLOR_4); |
| 8395 | BIND_BITFIELD_FLAG(DRAW_CLEAR_COLOR_5); |
| 8396 | BIND_BITFIELD_FLAG(DRAW_CLEAR_COLOR_6); |
| 8397 | BIND_BITFIELD_FLAG(DRAW_CLEAR_COLOR_7); |
| 8398 | BIND_BITFIELD_FLAG(DRAW_CLEAR_COLOR_MASK); |
| 8399 | BIND_BITFIELD_FLAG(DRAW_CLEAR_COLOR_ALL); |
| 8400 | BIND_BITFIELD_FLAG(DRAW_IGNORE_COLOR_0); |
| 8401 | BIND_BITFIELD_FLAG(DRAW_IGNORE_COLOR_1); |
| 8402 | BIND_BITFIELD_FLAG(DRAW_IGNORE_COLOR_2); |
| 8403 | BIND_BITFIELD_FLAG(DRAW_IGNORE_COLOR_3); |
| 8404 | BIND_BITFIELD_FLAG(DRAW_IGNORE_COLOR_4); |
| 8405 | BIND_BITFIELD_FLAG(DRAW_IGNORE_COLOR_5); |
| 8406 | BIND_BITFIELD_FLAG(DRAW_IGNORE_COLOR_6); |
| 8407 | BIND_BITFIELD_FLAG(DRAW_IGNORE_COLOR_7); |
| 8408 | BIND_BITFIELD_FLAG(DRAW_IGNORE_COLOR_MASK); |
| 8409 | BIND_BITFIELD_FLAG(DRAW_IGNORE_COLOR_ALL); |
| 8410 | BIND_BITFIELD_FLAG(DRAW_CLEAR_DEPTH); |
| 8411 | BIND_BITFIELD_FLAG(DRAW_IGNORE_DEPTH); |
| 8412 | BIND_BITFIELD_FLAG(DRAW_CLEAR_STENCIL); |
| 8413 | BIND_BITFIELD_FLAG(DRAW_IGNORE_STENCIL); |
| 8414 | BIND_BITFIELD_FLAG(DRAW_CLEAR_ALL); |
| 8415 | BIND_BITFIELD_FLAG(DRAW_IGNORE_ALL); |
| 8416 | } |
| 8417 | |
| 8418 | void RenderingDevice::make_current() { |
| 8419 | render_thread_id = Thread::get_caller_id(); |
| 8420 | } |
| 8421 | |
| 8422 | RenderingDevice::~RenderingDevice() { |
| 8423 | finalize(); |
| 8424 | |
| 8425 | if (singleton == this) { |
| 8426 | singleton = nullptr; |
| 8427 | } |
| 8428 | } |
| 8429 | |
| 8430 | RenderingDevice::RenderingDevice() { |
| 8431 | if (singleton == nullptr) { |
| 8432 | singleton = this; |
| 8433 | } |
| 8434 | |
| 8435 | render_thread_id = Thread::get_caller_id(); |
| 8436 | } |
| 8437 | |
| 8438 | /*****************/ |
| 8439 | /**** BINDERS ****/ |
| 8440 | /*****************/ |
| 8441 | |
| 8442 | RID RenderingDevice::_texture_create(const Ref<RDTextureFormat> &p_format, const Ref<RDTextureView> &p_view, const TypedArray<PackedByteArray> &p_data) { |
| 8443 | ERR_FAIL_COND_V(p_format.is_null(), RID()); |
| 8444 | ERR_FAIL_COND_V(p_view.is_null(), RID()); |
| 8445 | Vector<Vector<uint8_t>> data; |
| 8446 | for (int i = 0; i < p_data.size(); i++) { |
| 8447 | Vector<uint8_t> byte_slice = p_data[i]; |
| 8448 | ERR_FAIL_COND_V(byte_slice.is_empty(), RID()); |
| 8449 | data.push_back(byte_slice); |
| 8450 | } |
| 8451 | return texture_create(p_format->base, p_view->base, data); |
| 8452 | } |
| 8453 | |
| 8454 | RID RenderingDevice::_texture_create_shared(const Ref<RDTextureView> &p_view, RID p_with_texture) { |
| 8455 | ERR_FAIL_COND_V(p_view.is_null(), RID()); |
| 8456 | |
| 8457 | return texture_create_shared(p_view->base, p_with_texture); |
| 8458 | } |
| 8459 | |
| 8460 | RID RenderingDevice::_texture_create_shared_from_slice(const Ref<RDTextureView> &p_view, RID p_with_texture, uint32_t p_layer, uint32_t p_mipmap, uint32_t p_mipmaps, TextureSliceType p_slice_type) { |
| 8461 | ERR_FAIL_COND_V(p_view.is_null(), RID()); |
| 8462 | |
| 8463 | return texture_create_shared_from_slice(p_view->base, p_with_texture, p_layer, p_mipmap, p_mipmaps, p_slice_type); |
| 8464 | } |
| 8465 | |
| 8466 | Ref<RDTextureFormat> RenderingDevice::_texture_get_format(RID p_rd_texture) { |
| 8467 | Ref<RDTextureFormat> rtf; |
| 8468 | rtf.instantiate(); |
| 8469 | rtf->base = texture_get_format(p_rd_texture); |
| 8470 | |
| 8471 | return rtf; |
| 8472 | } |
| 8473 | |
| 8474 | RenderingDevice::FramebufferFormatID RenderingDevice::_framebuffer_format_create(const TypedArray<RDAttachmentFormat> &p_attachments, uint32_t p_view_count) { |
| 8475 | Vector<AttachmentFormat> attachments; |
| 8476 | attachments.resize(p_attachments.size()); |
| 8477 | |
| 8478 | for (int i = 0; i < p_attachments.size(); i++) { |
| 8479 | Ref<RDAttachmentFormat> af = p_attachments[i]; |
| 8480 | ERR_FAIL_COND_V(af.is_null(), INVALID_FORMAT_ID); |
| 8481 | attachments.write[i] = af->base; |
| 8482 | } |
| 8483 | return framebuffer_format_create(attachments, p_view_count); |
| 8484 | } |
| 8485 | |
| 8486 | RenderingDevice::FramebufferFormatID RenderingDevice::_framebuffer_format_create_multipass(const TypedArray<RDAttachmentFormat> &p_attachments, const TypedArray<RDFramebufferPass> &p_passes, uint32_t p_view_count) { |
| 8487 | Vector<AttachmentFormat> attachments; |
| 8488 | attachments.resize(p_attachments.size()); |
| 8489 | |
| 8490 | for (int i = 0; i < p_attachments.size(); i++) { |
| 8491 | Ref<RDAttachmentFormat> af = p_attachments[i]; |
| 8492 | ERR_FAIL_COND_V(af.is_null(), INVALID_FORMAT_ID); |
| 8493 | attachments.write[i] = af->base; |
| 8494 | } |
| 8495 | |
| 8496 | Vector<FramebufferPass> passes; |
| 8497 | for (int i = 0; i < p_passes.size(); i++) { |
| 8498 | Ref<RDFramebufferPass> pass = p_passes[i]; |
| 8499 | ERR_CONTINUE(pass.is_null()); |
| 8500 | passes.push_back(pass->base); |
| 8501 | } |
| 8502 | |
| 8503 | return framebuffer_format_create_multipass(attachments, passes, p_view_count); |
| 8504 | } |
| 8505 | |
| 8506 | RID RenderingDevice::_framebuffer_create(const TypedArray<RID> &p_textures, FramebufferFormatID p_format_check, uint32_t p_view_count) { |
| 8507 | Vector<RID> textures = Variant(p_textures); |
| 8508 | return framebuffer_create(textures, p_format_check, p_view_count); |
| 8509 | } |
| 8510 | |
| 8511 | RID RenderingDevice::_framebuffer_create_multipass(const TypedArray<RID> &p_textures, const TypedArray<RDFramebufferPass> &p_passes, FramebufferFormatID p_format_check, uint32_t p_view_count) { |
| 8512 | Vector<RID> textures = Variant(p_textures); |
| 8513 | Vector<FramebufferPass> passes; |
| 8514 | for (int i = 0; i < p_passes.size(); i++) { |
| 8515 | Ref<RDFramebufferPass> pass = p_passes[i]; |
| 8516 | ERR_CONTINUE(pass.is_null()); |
| 8517 | passes.push_back(pass->base); |
| 8518 | } |
| 8519 | return framebuffer_create_multipass(textures, passes, p_format_check, p_view_count); |
| 8520 | } |
| 8521 | |
| 8522 | RID RenderingDevice::_sampler_create(const Ref<RDSamplerState> &p_state) { |
| 8523 | ERR_FAIL_COND_V(p_state.is_null(), RID()); |
| 8524 | |
| 8525 | return sampler_create(p_state->base); |
| 8526 | } |
| 8527 | |
| 8528 | RenderingDevice::VertexFormatID RenderingDevice::_vertex_format_create(const TypedArray<RDVertexAttribute> &p_vertex_formats) { |
| 8529 | Vector<VertexAttribute> descriptions; |
| 8530 | descriptions.resize(p_vertex_formats.size()); |
| 8531 | |
| 8532 | for (int i = 0; i < p_vertex_formats.size(); i++) { |
| 8533 | Ref<RDVertexAttribute> af = p_vertex_formats[i]; |
| 8534 | ERR_FAIL_COND_V(af.is_null(), INVALID_FORMAT_ID); |
| 8535 | descriptions.write[i] = af->base; |
| 8536 | } |
| 8537 | return vertex_format_create(descriptions); |
| 8538 | } |
| 8539 | |
| 8540 | RID RenderingDevice::_vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const TypedArray<RID> &p_src_buffers, const Vector<int64_t> &p_offsets) { |
| 8541 | Vector<RID> buffers = Variant(p_src_buffers); |
| 8542 | |
| 8543 | Vector<uint64_t> offsets; |
| 8544 | offsets.resize(p_offsets.size()); |
| 8545 | for (int i = 0; i < p_offsets.size(); i++) { |
| 8546 | offsets.write[i] = p_offsets[i]; |
| 8547 | } |
| 8548 | |
| 8549 | return vertex_array_create(p_vertex_count, p_vertex_format, buffers, offsets); |
| 8550 | } |
| 8551 | |
| 8552 | void RenderingDevice::_draw_list_bind_vertex_buffers_format(DrawListID p_list, VertexFormatID p_vertex_format, uint32_t p_vertex_count, const TypedArray<RID> &p_vertex_buffers, const Vector<int64_t> &p_offsets) { |
| 8553 | Vector<RID> buffers = Variant(p_vertex_buffers); |
| 8554 | |
| 8555 | Vector<uint64_t> offsets; |
| 8556 | offsets.resize(p_offsets.size()); |
| 8557 | for (int i = 0; i < p_offsets.size(); i++) { |
| 8558 | offsets.write[i] = p_offsets[i]; |
| 8559 | } |
| 8560 | |
| 8561 | draw_list_bind_vertex_buffers_format(p_list, p_vertex_format, p_vertex_count, buffers, offsets); |
| 8562 | } |
| 8563 | |
| 8564 | Ref<RDShaderSPIRV> RenderingDevice::_shader_compile_spirv_from_source(const Ref<RDShaderSource> &p_source, bool p_allow_cache) { |
| 8565 | ERR_FAIL_COND_V(p_source.is_null(), Ref<RDShaderSPIRV>()); |
| 8566 | |
| 8567 | Ref<RDShaderSPIRV> bytecode; |
| 8568 | bytecode.instantiate(); |
| 8569 | for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) { |
| 8570 | String error; |
| 8571 | |
| 8572 | ShaderStage stage = ShaderStage(i); |
| 8573 | String source = p_source->get_stage_source(stage); |
| 8574 | |
| 8575 | if (!source.is_empty()) { |
| 8576 | Vector<uint8_t> spirv = shader_compile_spirv_from_source(stage, source, p_source->get_language(), &error, p_allow_cache); |
| 8577 | bytecode->set_stage_bytecode(stage, spirv); |
| 8578 | bytecode->set_stage_compile_error(stage, error); |
| 8579 | } |
| 8580 | } |
| 8581 | return bytecode; |
| 8582 | } |
| 8583 | |
| 8584 | Vector<uint8_t> RenderingDevice::_shader_compile_binary_from_spirv(const Ref<RDShaderSPIRV> &p_spirv, const String &p_shader_name) { |
| 8585 | ERR_FAIL_COND_V(p_spirv.is_null(), Vector<uint8_t>()); |
| 8586 | |
| 8587 | Vector<ShaderStageSPIRVData> stage_data; |
| 8588 | for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) { |
| 8589 | ShaderStage stage = ShaderStage(i); |
| 8590 | ShaderStageSPIRVData sd; |
| 8591 | sd.shader_stage = stage; |
| 8592 | String error = p_spirv->get_stage_compile_error(stage); |
| 8593 | ERR_FAIL_COND_V_MSG(!error.is_empty(), Vector<uint8_t>(), "Can't create a shader from an errored bytecode. Check errors in source bytecode."); |
| 8594 | sd.spirv = p_spirv->get_stage_bytecode(stage); |
| 8595 | if (sd.spirv.is_empty()) { |
| 8596 | continue; |
| 8597 | } |
| 8598 | stage_data.push_back(sd); |
| 8599 | } |
| 8600 | |
| 8601 | return shader_compile_binary_from_spirv(stage_data, p_shader_name); |
| 8602 | } |
| 8603 | |
| 8604 | RID RenderingDevice::_shader_create_from_spirv(const Ref<RDShaderSPIRV> &p_spirv, const String &p_shader_name) { |
| 8605 | ERR_FAIL_COND_V(p_spirv.is_null(), RID()); |
| 8606 | |
| 8607 | Vector<ShaderStageSPIRVData> stage_data; |
| 8608 | for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) { |
| 8609 | ShaderStage stage = ShaderStage(i); |
| 8610 | ShaderStageSPIRVData sd; |
| 8611 | sd.shader_stage = stage; |
| 8612 | String error = p_spirv->get_stage_compile_error(stage); |
| 8613 | ERR_FAIL_COND_V_MSG(!error.is_empty(), RID(), "Can't create a shader from an errored bytecode. Check errors in source bytecode."); |
| 8614 | sd.spirv = p_spirv->get_stage_bytecode(stage); |
| 8615 | if (sd.spirv.is_empty()) { |
| 8616 | continue; |
| 8617 | } |
| 8618 | stage_data.push_back(sd); |
| 8619 | } |
| 8620 | return shader_create_from_spirv(stage_data); |
| 8621 | } |
| 8622 | |
| 8623 | RID RenderingDevice::_uniform_set_create(const TypedArray<RDUniform> &p_uniforms, RID p_shader, uint32_t p_shader_set) { |
| 8624 | LocalVector<Uniform> uniforms; |
| 8625 | uniforms.resize(p_uniforms.size()); |
| 8626 | for (int i = 0; i < p_uniforms.size(); i++) { |
| 8627 | Ref<RDUniform> uniform = p_uniforms[i]; |
| 8628 | ERR_FAIL_COND_V(uniform.is_null(), RID()); |
| 8629 | uniforms[i] = uniform->base; |
| 8630 | } |
| 8631 | return uniform_set_create(uniforms, p_shader, p_shader_set); |
| 8632 | } |
| 8633 | |
| 8634 | Error RenderingDevice::_buffer_update_bind(RID p_buffer, uint32_t p_offset, uint32_t p_size, const Vector<uint8_t> &p_data) { |
| 8635 | return buffer_update(p_buffer, p_offset, p_size, p_data.ptr()); |
| 8636 | } |
| 8637 | |
| 8638 | static Vector<RenderingDevice::PipelineSpecializationConstant> _get_spec_constants(const TypedArray<RDPipelineSpecializationConstant> &p_constants) { |
| 8639 | Vector<RenderingDevice::PipelineSpecializationConstant> ret; |
| 8640 | ret.resize(p_constants.size()); |
| 8641 | for (int i = 0; i < p_constants.size(); i++) { |
| 8642 | Ref<RDPipelineSpecializationConstant> c = p_constants[i]; |
| 8643 | ERR_CONTINUE(c.is_null()); |
| 8644 | RenderingDevice::PipelineSpecializationConstant &sc = ret.write[i]; |
| 8645 | Variant value = c->get_value(); |
| 8646 | switch (value.get_type()) { |
| 8647 | case Variant::BOOL: { |
| 8648 | sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL; |
| 8649 | sc.bool_value = value; |
| 8650 | } break; |
| 8651 | case Variant::INT: { |
| 8652 | sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT; |
| 8653 | sc.int_value = value; |
| 8654 | } break; |
| 8655 | case Variant::FLOAT: { |
| 8656 | sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_FLOAT; |
| 8657 | sc.float_value = value; |
| 8658 | } break; |
| 8659 | default: { |
| 8660 | } |
| 8661 | } |
| 8662 | |
| 8663 | sc.constant_id = c->get_constant_id(); |
| 8664 | } |
| 8665 | return ret; |
| 8666 | } |
| 8667 | |
| 8668 | RID RenderingDevice::_render_pipeline_create(RID p_shader, FramebufferFormatID p_framebuffer_format, VertexFormatID p_vertex_format, RenderPrimitive p_render_primitive, const Ref<RDPipelineRasterizationState> &p_rasterization_state, const Ref<RDPipelineMultisampleState> &p_multisample_state, const Ref<RDPipelineDepthStencilState> &p_depth_stencil_state, const Ref<RDPipelineColorBlendState> &p_blend_state, BitField<PipelineDynamicStateFlags> p_dynamic_state_flags, uint32_t p_for_render_pass, const TypedArray<RDPipelineSpecializationConstant> &p_specialization_constants) { |
| 8669 | PipelineRasterizationState rasterization_state; |
| 8670 | if (p_rasterization_state.is_valid()) { |
| 8671 | rasterization_state = p_rasterization_state->base; |
| 8672 | } |
| 8673 | |
| 8674 | PipelineMultisampleState multisample_state; |
| 8675 | if (p_multisample_state.is_valid()) { |
| 8676 | multisample_state = p_multisample_state->base; |
| 8677 | for (int i = 0; i < p_multisample_state->sample_masks.size(); i++) { |
| 8678 | int64_t mask = p_multisample_state->sample_masks[i]; |
| 8679 | multisample_state.sample_mask.push_back(mask); |
| 8680 | } |
| 8681 | } |
| 8682 | |
| 8683 | PipelineDepthStencilState depth_stencil_state; |
| 8684 | if (p_depth_stencil_state.is_valid()) { |
| 8685 | depth_stencil_state = p_depth_stencil_state->base; |
| 8686 | } |
| 8687 | |
| 8688 | PipelineColorBlendState color_blend_state; |
| 8689 | if (p_blend_state.is_valid()) { |
| 8690 | color_blend_state = p_blend_state->base; |
| 8691 | for (int i = 0; i < p_blend_state->attachments.size(); i++) { |
| 8692 | Ref<RDPipelineColorBlendStateAttachment> attachment = p_blend_state->attachments[i]; |
| 8693 | if (attachment.is_valid()) { |
| 8694 | color_blend_state.attachments.push_back(attachment->base); |
| 8695 | } |
| 8696 | } |
| 8697 | } |
| 8698 | |
| 8699 | return render_pipeline_create(p_shader, p_framebuffer_format, p_vertex_format, p_render_primitive, rasterization_state, multisample_state, depth_stencil_state, color_blend_state, p_dynamic_state_flags, p_for_render_pass, _get_spec_constants(p_specialization_constants)); |
| 8700 | } |
| 8701 | |
| 8702 | RID RenderingDevice::_compute_pipeline_create(RID p_shader, const TypedArray<RDPipelineSpecializationConstant> &p_specialization_constants = TypedArray<RDPipelineSpecializationConstant>()) { |
| 8703 | return compute_pipeline_create(p_shader, _get_spec_constants(p_specialization_constants)); |
| 8704 | } |
| 8705 | |
| 8706 | #ifndef DISABLE_DEPRECATED |
| 8707 | Vector<int64_t> RenderingDevice::_draw_list_begin_split(RID p_framebuffer, uint32_t p_splits, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values, float p_clear_depth, uint32_t p_clear_stencil, const Rect2 &p_region, const TypedArray<RID> &p_storage_textures) { |
| 8708 | ERR_FAIL_V_MSG(Vector<int64_t>(), "Deprecated. Split draw lists are used automatically by RenderingDevice."); |
| 8709 | } |
| 8710 | |
| 8711 | Vector<int64_t> RenderingDevice::_draw_list_switch_to_next_pass_split(uint32_t p_splits) { |
| 8712 | ERR_FAIL_V_MSG(Vector<int64_t>(), "Deprecated. Split draw lists are used automatically by RenderingDevice."); |
| 8713 | } |
| 8714 | #endif |
| 8715 | |
| 8716 | void RenderingDevice::_draw_list_set_push_constant(DrawListID p_list, const Vector<uint8_t> &p_data, uint32_t p_data_size) { |
| 8717 | ERR_FAIL_COND(p_data_size > (uint32_t)p_data.size()); |
| 8718 | draw_list_set_push_constant(p_list, p_data.ptr(), p_data_size); |
| 8719 | } |
| 8720 | |
| 8721 | void RenderingDevice::_compute_list_set_push_constant(ComputeListID p_list, const Vector<uint8_t> &p_data, uint32_t p_data_size) { |
| 8722 | ERR_FAIL_COND(p_data_size > (uint32_t)p_data.size()); |
| 8723 | compute_list_set_push_constant(p_list, p_data.ptr(), p_data_size); |
| 8724 | } |
| 8725 | |
| 8726 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_NONE, RDG::RESOURCE_USAGE_NONE)); |
| 8727 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_COPY_FROM, RDG::RESOURCE_USAGE_COPY_FROM)); |
| 8728 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_COPY_TO, RDG::RESOURCE_USAGE_COPY_TO)); |
| 8729 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_RESOLVE_FROM, RDG::RESOURCE_USAGE_RESOLVE_FROM)); |
| 8730 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_RESOLVE_TO, RDG::RESOURCE_USAGE_RESOLVE_TO)); |
| 8731 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_UNIFORM_BUFFER_READ, RDG::RESOURCE_USAGE_UNIFORM_BUFFER_READ)); |
| 8732 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_INDIRECT_BUFFER_READ, RDG::RESOURCE_USAGE_INDIRECT_BUFFER_READ)); |
| 8733 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_TEXTURE_BUFFER_READ, RDG::RESOURCE_USAGE_TEXTURE_BUFFER_READ)); |
| 8734 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE, RDG::RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE)); |
| 8735 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_STORAGE_BUFFER_READ, RDG::RESOURCE_USAGE_STORAGE_BUFFER_READ)); |
| 8736 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE, RDG::RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE)); |
| 8737 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_VERTEX_BUFFER_READ, RDG::RESOURCE_USAGE_VERTEX_BUFFER_READ)); |
| 8738 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_INDEX_BUFFER_READ, RDG::RESOURCE_USAGE_INDEX_BUFFER_READ)); |
| 8739 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_TEXTURE_SAMPLE, RDG::RESOURCE_USAGE_TEXTURE_SAMPLE)); |
| 8740 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_STORAGE_IMAGE_READ, RDG::RESOURCE_USAGE_STORAGE_IMAGE_READ)); |
| 8741 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE, RDG::RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE)); |
| 8742 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE, RDG::RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE)); |
| 8743 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE, RDG::RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE)); |
| 8744 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_GENERAL, RDG::RESOURCE_USAGE_GENERAL)); |
| 8745 | static_assert(ENUM_MEMBERS_EQUAL(RD::CALLBACK_RESOURCE_USAGE_MAX, RDG::RESOURCE_USAGE_MAX)); |