0.2.0-dev Download ZIP
670 lines ·24.8 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* metal3_objects.h */ |
| 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 | #pragma once |
| 32 | |
| 33 | /**************************************************************************/ |
| 34 | /* */ |
| 35 | /* Portions of this code were derived from MoltenVK. */ |
| 36 | /* */ |
| 37 | /* Copyright (c) 2015-2023 The Brenwill Workshop Ltd. */ |
| 38 | /* (http://www.brenwill.com) */ |
| 39 | /* */ |
| 40 | /* Licensed under the Apache License, Version 2.0 (the "License"); */ |
| 41 | /* you may not use this file except in compliance with the License. */ |
| 42 | /* You may obtain a copy of the License at */ |
| 43 | /* */ |
| 44 | /* http://www.apache.org/licenses/LICENSE-2.0 */ |
| 45 | /* */ |
| 46 | /* Unless required by applicable law or agreed to in writing, software */ |
| 47 | /* distributed under the License is distributed on an "AS IS" BASIS, */ |
| 48 | /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */ |
| 49 | /* implied. See the License for the specific language governing */ |
| 50 | /* permissions and limitations under the License. */ |
| 51 | /**************************************************************************/ |
| 52 | |
| 53 | #include "drivers/metal/metal_objects_shared.h" |
| 54 | #include "drivers/metal/rendering_device_driver_metal.h" |
| 55 | #include "servers/rendering/rendering_device_driver.h" |
| 56 | |
| 57 | #include <Metal/Metal.hpp> |
| 58 | |
| 59 | #include <initializer_list> |
| 60 | #include <optional> |
| 61 | |
| 62 | namespace MTL3 { |
| 63 | |
| 64 | // These types are defined in the global namespace (metal_objects_shared.h / rendering_device_driver_metal.h) |
| 65 | using ::MDAttachment; |
| 66 | using ::MDAttachmentType; |
| 67 | using ::MDCommandBufferBase; |
| 68 | using ::MDCommandBufferStateType; |
| 69 | using ::MDFrameBuffer; |
| 70 | using ::MDRenderPass; |
| 71 | using ::MDRingBuffer; |
| 72 | using ::MDSubpass; |
| 73 | using ::RenderStateBase; |
| 74 | |
| 75 | using ::DynamicOffsetLayout; |
| 76 | using ::MDComputePipeline; |
| 77 | using ::MDComputeShader; |
| 78 | using ::MDLibrary; |
| 79 | using ::MDPipeline; |
| 80 | using ::MDPipelineType; |
| 81 | using ::MDRenderPipeline; |
| 82 | using ::MDRenderShader; |
| 83 | using ::MDShader; |
| 84 | using ::MDUniformSet; |
| 85 | using ::ShaderCacheEntry; |
| 86 | using ::ShaderLoadStrategy; |
| 87 | using ::UniformInfo; |
| 88 | using ::UniformSet; |
| 89 | |
| 90 | using RDM = ::RenderingDeviceDriverMetal; |
| 91 | |
| 92 | struct ResourceUsageEntry { |
| 93 | StageResourceUsage usage = ResourceUnused; |
| 94 | uint32_t unused = 0; |
| 95 | |
| 96 | ResourceUsageEntry() {} |
| 97 | ResourceUsageEntry(StageResourceUsage p_usage) : |
| 98 | usage(p_usage) {} |
| 99 | }; |
| 100 | |
| 101 | } // namespace MTL3 |
| 102 | |
| 103 | template <> |
| 104 | struct is_zero_constructible<MTL3::ResourceUsageEntry> : std::true_type {}; |
| 105 | |
| 106 | namespace MTL3 { |
| 107 | |
| 108 | /*! Track the cumulative usage for a resource during a render or compute pass */ |
| 109 | typedef HashMap<MTL::Resource *, ResourceUsageEntry> ResourceToStageUsage; |
| 110 | |
| 111 | /*! Track resource and ensure they are resident prior to dispatch or draw commands. |
| 112 | * |
| 113 | * The primary purpose of this data structure is to track all the resources that must be made resident prior |
| 114 | * to issuing the next dispatch or draw command. It aggregates all resources used from argument buffers. |
| 115 | * |
| 116 | * As an optimization, this data structure also tracks previous usage for resources, so that |
| 117 | * it may avoid binding them again in later commands if the resource is already resident and its usage flagged. |
| 118 | */ |
| 119 | struct API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) ResourceTracker { |
| 120 | // A constant specifying how many iterations a resource can remain in |
| 121 | // the _previous HashSet before it will be removed permanently. |
| 122 | // |
| 123 | // Keeping them in the _previous HashMap reduces churn if resources are regularly |
| 124 | // bound. 256 is arbitrary, but if an object remains unused for 256 encoders, |
| 125 | // it will be released. |
| 126 | static constexpr uint32_t RESOURCE_UNUSED_CLEANUP_COUNT = 256; |
| 127 | |
| 128 | // Used as a scratch buffer to periodically clean up resources from _previous. |
| 129 | ResourceVector _scratch; |
| 130 | // Tracks all resources and their prior usage for the duration of the encoder. |
| 131 | ResourceToStageUsage _previous; |
| 132 | // Tracks resources for the current command that must be made resident |
| 133 | ResourceUsageMap _current; |
| 134 | |
| 135 | void merge_from(const ::ResourceUsageMap &p_from); |
| 136 | void encode(MTL::RenderCommandEncoder *p_enc); |
| 137 | void encode(MTL::ComputeCommandEncoder *p_enc); |
| 138 | void reset(); |
| 139 | }; |
| 140 | |
| 141 | struct BindingCache { |
| 142 | struct BufferBinding { |
| 143 | MTL::Buffer *buffer; |
| 144 | NS::UInteger offset; |
| 145 | |
| 146 | bool operator!=(const BufferBinding &p_other) const { |
| 147 | return buffer != p_other.buffer || offset != p_other.offset; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | static_assert(std::is_trivially_constructible_v<BufferBinding>); |
| 152 | |
| 153 | LocalVector<MTL::Texture *> textures; |
| 154 | LocalVector<MTL::SamplerState *> samplers; |
| 155 | LocalVector<BufferBinding> buffers; |
| 156 | |
| 157 | _FORCE_INLINE_ void clear() { |
| 158 | textures.clear(); |
| 159 | samplers.clear(); |
| 160 | buffers.clear(); |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | // Returns true if p_vec was resized, and therefore new elements were initialized. |
| 165 | template <typename T> |
| 166 | _FORCE_INLINE_ bool ensure_size(LocalVector<T> &p_vec, uint32_t p_required) { |
| 167 | if (p_vec.size() < p_required) { |
| 168 | p_vec.resize_initialized(p_required); |
| 169 | return true; |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | public: |
| 175 | _FORCE_INLINE_ bool update(NS::Range p_range, MTL::Texture *const *p_values) { |
| 176 | if (p_range.length == 0) { |
| 177 | return false; |
| 178 | } |
| 179 | uint32_t required = (uint32_t)(p_range.location + p_range.length); |
| 180 | ensure_size(textures, required); |
| 181 | bool changed = false; |
| 182 | for (NS::UInteger i = 0; i < p_range.length; ++i) { |
| 183 | uint32_t slot = (uint32_t)(p_range.location + i); |
| 184 | MTL::Texture *value = p_values[i]; |
| 185 | if (textures[slot] != value) { |
| 186 | textures[slot] = value; |
| 187 | changed = true; |
| 188 | } |
| 189 | } |
| 190 | return changed; |
| 191 | } |
| 192 | |
| 193 | _FORCE_INLINE_ bool update(NS::Range p_range, MTL::SamplerState *const *p_values) { |
| 194 | if (p_range.length == 0) { |
| 195 | return false; |
| 196 | } |
| 197 | uint32_t required = (uint32_t)(p_range.location + p_range.length); |
| 198 | ensure_size(samplers, required); |
| 199 | bool changed = false; |
| 200 | for (NS::UInteger i = 0; i < p_range.length; ++i) { |
| 201 | uint32_t slot = (uint32_t)(p_range.location + i); |
| 202 | MTL::SamplerState *value = p_values[i]; |
| 203 | if (samplers[slot] != value) { |
| 204 | samplers[slot] = value; |
| 205 | changed = true; |
| 206 | } |
| 207 | } |
| 208 | return changed; |
| 209 | } |
| 210 | |
| 211 | _FORCE_INLINE_ bool update(NS::Range p_range, MTL::Buffer *const *p_values, const NS::UInteger *p_offsets) { |
| 212 | if (p_range.length == 0) { |
| 213 | return false; |
| 214 | } |
| 215 | uint32_t required = (uint32_t)(p_range.location + p_range.length); |
| 216 | ensure_size(buffers, required); |
| 217 | BufferBinding *buffers_ptr = buffers.ptr() + p_range.location; |
| 218 | bool changed = false; |
| 219 | for (NS::UInteger i = 0; i < p_range.length; ++i) { |
| 220 | BufferBinding &binding = *buffers_ptr; |
| 221 | BufferBinding new_binding = { |
| 222 | .buffer = p_values[i], |
| 223 | .offset = p_offsets[i], |
| 224 | }; |
| 225 | if (binding != new_binding) { |
| 226 | binding = new_binding; |
| 227 | changed = true; |
| 228 | } |
| 229 | ++buffers_ptr; |
| 230 | } |
| 231 | return changed; |
| 232 | } |
| 233 | |
| 234 | _FORCE_INLINE_ bool update(MTL::Buffer *p_buffer, NS::UInteger p_offset, uint32_t p_index) { |
| 235 | uint32_t required = p_index + 1; |
| 236 | ensure_size(buffers, required); |
| 237 | BufferBinding &binding = buffers.ptr()[p_index]; |
| 238 | BufferBinding new_binding = { |
| 239 | .buffer = p_buffer, |
| 240 | .offset = p_offset, |
| 241 | }; |
| 242 | if (binding != new_binding) { |
| 243 | binding = new_binding; |
| 244 | return true; |
| 245 | } |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | _FORCE_INLINE_ void invalidate_buffer(uint32_t p_index) { |
| 250 | if (!ensure_size(buffers, p_index + 1)) { |
| 251 | buffers.ptr()[p_index] = { |
| 252 | .buffer = nullptr, |
| 253 | .offset = 0, |
| 254 | }; |
| 255 | } |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | // A type used to encode resources directly to a MTLCommandEncoder |
| 260 | struct DirectEncoder { |
| 261 | MTL::CommandEncoder *encoder; |
| 262 | BindingCache &cache; |
| 263 | enum Mode { |
| 264 | RENDER, |
| 265 | COMPUTE |
| 266 | }; |
| 267 | Mode mode; |
| 268 | |
| 269 | void set(MTL::Buffer **p_buffers, const NS::UInteger *p_offsets, NS::Range p_range); |
| 270 | void set(MTL::Buffer *p_buffer, NS::UInteger p_offset, uint32_t p_index); |
| 271 | void set(MTL::Texture **p_textures, NS::Range p_range); |
| 272 | void set(MTL::SamplerState **p_samplers, NS::Range p_range); |
| 273 | |
| 274 | DirectEncoder(MTL::CommandEncoder *p_encoder, BindingCache &p_cache, Mode p_mode) : |
| 275 | encoder(p_encoder), cache(p_cache), mode(p_mode) {} |
| 276 | }; |
| 277 | |
| 278 | // A pool of GPU timestamps, backed by a timestamp counter sample buffer. |
| 279 | // |
| 280 | // The sample buffer uses shared storage, so results are resolved on the CPU and |
| 281 | // no blit pass is required to make them readable. |
| 282 | class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) QueryPool { |
| 283 | NS::SharedPtr<MTL::CounterSampleBuffer> sample_buffer; |
| 284 | // A one byte private buffer, filled by the timestamp encoder so it always |
| 285 | // carries at least one command. |
| 286 | NS::SharedPtr<MTL::Buffer> dummy_buffer; |
| 287 | // The counter sample buffer timestamp domain is not queryable, so it is |
| 288 | // correlated against the CPU clock. queryTimestampFrequency() describes the |
| 289 | // Metal 4 counter heap domain instead, and is roughly 33x off here. |
| 290 | MTL::Device *device = nullptr; |
| 291 | MTL::Timestamp cpu_base = 0; |
| 292 | MTL::Timestamp gpu_base = 0; |
| 293 | double timestamp_to_nano = 0.0; |
| 294 | uint32_t count = 0; |
| 295 | |
| 296 | QueryPool() = default; |
| 297 | |
| 298 | // Resolves the GPU to CPU tick ratio on first use, once enough time has |
| 299 | // elapsed since creation for the correlation to be meaningful. |
| 300 | double _resolve_timestamp_to_nano(); |
| 301 | |
| 302 | public: |
| 303 | // Returns nullptr if the device does not expose the timestamp counter set. |
| 304 | static QueryPool *create(MTL::Device *p_device, uint32_t p_count); |
| 305 | |
| 306 | MTL::CounterSampleBuffer *get_sample_buffer() const { return sample_buffer.get(); } |
| 307 | MTL::Buffer *get_dummy_buffer() const { return dummy_buffer.get(); } |
| 308 | uint32_t get_count() const { return count; } |
| 309 | |
| 310 | void get_results(uint32_t p_count, uint64_t *r_results); |
| 311 | }; |
| 312 | |
| 313 | class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDCommandBuffer : public MDCommandBufferBase { |
| 314 | friend class MDUniformSet; |
| 315 | |
| 316 | private: |
| 317 | #pragma mark - Common State |
| 318 | |
| 319 | BindingCache binding_cache; |
| 320 | |
| 321 | #pragma mark - Argument Buffer Ring Allocator |
| 322 | |
| 323 | using Alloc = MDRingBuffer::Allocation; |
| 324 | |
| 325 | // Used for argument buffers that contain dynamic uniforms. |
| 326 | MDRingBuffer _scratch; |
| 327 | |
| 328 | /// Allocates from the ring buffer for dynamic argument buffers. |
| 329 | Alloc allocate_arg_buffer(uint32_t p_size); |
| 330 | |
| 331 | // Heaps made resident on this command buffer's encoders (barriers mode only). |
| 332 | LocalVector<MTL::Heap *> _resident_heaps; |
| 333 | uint64_t _resident_heaps_generation = 0; |
| 334 | |
| 335 | void _encode_residency(MTL::RenderCommandEncoder *p_enc); |
| 336 | void _encode_residency(MTL::ComputeCommandEncoder *p_enc); |
| 337 | |
| 338 | #pragma mark - Synchronization |
| 339 | |
| 340 | RDM::SyncMode sync_mode = RDM::SyncMode::Barriers; |
| 341 | |
| 342 | void _fence_wait(MTL::RenderCommandEncoder *p_enc); |
| 343 | void _fence_wait(MTL::ComputeCommandEncoder *p_enc); |
| 344 | void _fence_wait(MTL::BlitCommandEncoder *p_enc); |
| 345 | void _fence_update(MTL::RenderCommandEncoder *p_enc); |
| 346 | void _fence_update(MTL::ComputeCommandEncoder *p_enc); |
| 347 | void _fence_update(MTL::BlitCommandEncoder *p_enc); |
| 348 | |
| 349 | void reset(); |
| 350 | |
| 351 | MTL::CommandQueue *queue = nullptr; |
| 352 | NS::SharedPtr<MTL::CommandBuffer> commandBuffer; |
| 353 | bool state_begin = false; |
| 354 | |
| 355 | MTL::CommandBuffer *command_buffer(); |
| 356 | |
| 357 | void _end_compute_dispatch(); |
| 358 | void _end_inline_render(); |
| 359 | void _end_blit(); |
| 360 | void _pop_active_encoder_labels(); |
| 361 | void _set_inline_render_encoder(MTL::RenderCommandEncoder *p_encoder); |
| 362 | MTL::BlitCommandEncoder *_ensure_blit_encoder(); |
| 363 | |
| 364 | enum class CopySource { |
| 365 | Buffer, |
| 366 | Texture, |
| 367 | }; |
| 368 | void _copy_texture_buffer(CopySource p_source, |
| 369 | RDD::TextureID p_texture, |
| 370 | RDD::BufferID p_buffer, |
| 371 | VectorView<RDD::BufferTextureCopyRegion> p_regions); |
| 372 | |
| 373 | #pragma mark - Render |
| 374 | |
| 375 | void _render_set_dirty_state(); |
| 376 | void _render_bind_uniform_sets(); |
| 377 | void _set_arg_buffer(MTL::RenderCommandEncoder *p_enc, const UniformSet &p_shader_set, MTL::Buffer *p_buffer, uint32_t p_offset, uint32_t p_set_index); |
| 378 | void _bind_uniforms_argument_buffers(MDUniformSet *p_set, MDShader *p_shader, uint32_t p_set_index, uint32_t p_dynamic_offsets); |
| 379 | void _bind_uniforms_direct(MDUniformSet *p_set, MDShader *p_shader, DirectEncoder p_enc, uint32_t p_set_index, uint32_t p_dynamic_offsets); |
| 380 | |
| 381 | #pragma mark - Compute |
| 382 | |
| 383 | void _compute_set_dirty_state(); |
| 384 | void _compute_bind_uniform_sets(); |
| 385 | void _bind_uniforms_argument_buffers_compute(MDUniformSet *p_set, MDShader *p_shader, uint32_t p_set_index, uint32_t p_dynamic_offsets); |
| 386 | |
| 387 | protected: |
| 388 | void mark_push_constants_dirty() override; |
| 389 | RenderStateBase &get_render_state_base() override { return render; } |
| 390 | uint32_t get_current_view_count() const override { return render.get_subpass().view_count; } |
| 391 | MDRenderPass *get_render_pass() const override { return render.pass; } |
| 392 | MDFrameBuffer *get_frame_buffer() const override { return render.frameBuffer; } |
| 393 | const MDSubpass &get_current_subpass() const override { return render.get_subpass(); } |
| 394 | LocalVector<RDD::RenderPassClearValue> &get_clear_values() override { return render.clear_values; } |
| 395 | const Rect2i &get_render_area() const override { return render.render_area; } |
| 396 | void end_render_encoding() override { |
| 397 | _fence_update(render.encoder.get()); |
| 398 | render.end_encoding(); |
| 399 | } |
| 400 | |
| 401 | public: |
| 402 | struct RenderState : public RenderStateBase { |
| 403 | MDRenderPass *pass = nullptr; |
| 404 | MDFrameBuffer *frameBuffer = nullptr; |
| 405 | MDRenderPipeline *pipeline = nullptr; |
| 406 | LocalVector<RDD::RenderPassClearValue> clear_values; |
| 407 | MDSubpass *current_subpass = nullptr; |
| 408 | Rect2i render_area; |
| 409 | bool is_rendering_entire_area = false; |
| 410 | NS::SharedPtr<MTL::RenderPassDescriptor> desc; |
| 411 | NS::SharedPtr<MTL::RenderCommandEncoder> encoder; |
| 412 | MTL::Buffer *index_buffer = nullptr; // Buffer is owned by RDD. |
| 413 | MTL::IndexType index_type = MTL::IndexTypeUInt16; |
| 414 | uint32_t index_offset = 0; |
| 415 | LocalVector<MTL::Buffer *> vertex_buffers; |
| 416 | LocalVector<NS::UInteger> vertex_offsets; |
| 417 | ResourceTracker resource_tracker; |
| 418 | |
| 419 | LocalVector<MDUniformSet *> uniform_sets; |
| 420 | uint32_t dynamic_offsets = 0; |
| 421 | // Bit mask of the uniform sets that are dirty, to prevent redundant binding. |
| 422 | uint64_t uniform_set_mask = 0; |
| 423 | |
| 424 | enum ArgBufferStage { |
| 425 | ARG_BUFFER_STAGE_VERTEX, |
| 426 | ARG_BUFFER_STAGE_FRAGMENT, |
| 427 | ARG_BUFFER_STAGE_MAX, |
| 428 | }; |
| 429 | BindingCache arg_buffer_cache[ARG_BUFFER_STAGE_MAX]; |
| 430 | |
| 431 | MDRenderPipeline::RasterState encoder_raster; |
| 432 | MDRenderPipeline::RasterState raster_state; |
| 433 | |
| 434 | _FORCE_INLINE_ void clear_arg_buffer_cache() { |
| 435 | for (BindingCache &cache : arg_buffer_cache) { |
| 436 | cache.clear(); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | _FORCE_INLINE_ void reset(); |
| 441 | void end_encoding(); |
| 442 | |
| 443 | _ALWAYS_INLINE_ const MDSubpass &get_subpass() const { |
| 444 | DEV_ASSERT(current_subpass != nullptr); |
| 445 | return *current_subpass; |
| 446 | } |
| 447 | |
| 448 | _FORCE_INLINE_ void mark_viewport_dirty() { |
| 449 | if (viewports.is_empty()) { |
| 450 | return; |
| 451 | } |
| 452 | dirty.set_flag(DirtyFlag::DIRTY_VIEWPORT); |
| 453 | } |
| 454 | |
| 455 | _FORCE_INLINE_ void mark_scissors_dirty() { |
| 456 | if (scissors.is_empty()) { |
| 457 | return; |
| 458 | } |
| 459 | dirty.set_flag(DirtyFlag::DIRTY_SCISSOR); |
| 460 | } |
| 461 | |
| 462 | _FORCE_INLINE_ void mark_vertex_dirty() { |
| 463 | if (vertex_buffers.is_empty()) { |
| 464 | return; |
| 465 | } |
| 466 | dirty.set_flag(DirtyFlag::DIRTY_VERTEX); |
| 467 | } |
| 468 | |
| 469 | _FORCE_INLINE_ void mark_uniforms_dirty(std::initializer_list<uint32_t> l) { |
| 470 | if (uniform_sets.is_empty()) { |
| 471 | return; |
| 472 | } |
| 473 | for (uint32_t i : l) { |
| 474 | if (i < uniform_sets.size() && uniform_sets[i] != nullptr) { |
| 475 | uniform_set_mask |= 1 << i; |
| 476 | } |
| 477 | } |
| 478 | dirty.set_flag(DirtyFlag::DIRTY_UNIFORMS); |
| 479 | } |
| 480 | |
| 481 | _FORCE_INLINE_ void mark_uniforms_dirty() { |
| 482 | if (uniform_sets.is_empty()) { |
| 483 | return; |
| 484 | } |
| 485 | for (uint32_t i = 0; i < uniform_sets.size(); i++) { |
| 486 | if (uniform_sets[i] != nullptr) { |
| 487 | uniform_set_mask |= 1 << i; |
| 488 | } |
| 489 | } |
| 490 | dirty.set_flag(DirtyFlag::DIRTY_UNIFORMS); |
| 491 | } |
| 492 | |
| 493 | MTL::ScissorRect clip_to_render_area(MTL::ScissorRect p_rect) const { |
| 494 | uint32_t raLeft = render_area.position.x; |
| 495 | uint32_t raRight = raLeft + render_area.size.width; |
| 496 | uint32_t raBottom = render_area.position.y; |
| 497 | uint32_t raTop = raBottom + render_area.size.height; |
| 498 | |
| 499 | p_rect.x = CLAMP(p_rect.x, raLeft, MAX(raRight - 1, raLeft)); |
| 500 | p_rect.y = CLAMP(p_rect.y, raBottom, MAX(raTop - 1, raBottom)); |
| 501 | p_rect.width = MIN(p_rect.width, raRight - p_rect.x); |
| 502 | p_rect.height = MIN(p_rect.height, raTop - p_rect.y); |
| 503 | |
| 504 | return p_rect; |
| 505 | } |
| 506 | |
| 507 | Rect2i clip_to_render_area(Rect2i p_rect) const { |
| 508 | int32_t raLeft = render_area.position.x; |
| 509 | int32_t raRight = raLeft + render_area.size.width; |
| 510 | int32_t raBottom = render_area.position.y; |
| 511 | int32_t raTop = raBottom + render_area.size.height; |
| 512 | |
| 513 | p_rect.position.x = CLAMP(p_rect.position.x, raLeft, MAX(raRight - 1, raLeft)); |
| 514 | p_rect.position.y = CLAMP(p_rect.position.y, raBottom, MAX(raTop - 1, raBottom)); |
| 515 | p_rect.size.width = MIN(p_rect.size.width, raRight - p_rect.position.x); |
| 516 | p_rect.size.height = MIN(p_rect.size.height, raTop - p_rect.position.y); |
| 517 | |
| 518 | return p_rect; |
| 519 | } |
| 520 | |
| 521 | } render; |
| 522 | |
| 523 | // State specific for a compute pass. |
| 524 | struct ComputeState { |
| 525 | MDComputePipeline *pipeline = nullptr; |
| 526 | NS::SharedPtr<MTL::ComputeCommandEncoder> encoder; |
| 527 | ResourceTracker resource_tracker; |
| 528 | // clang-format off |
| 529 | enum DirtyFlag: uint16_t { |
| 530 | DIRTY_NONE = 0, |
| 531 | DIRTY_PIPELINE = 1 << 0, //! pipeline state |
| 532 | DIRTY_UNIFORMS = 1 << 1, //! uniform sets |
| 533 | DIRTY_PUSH = 1 << 2, //! push constants |
| 534 | DIRTY_ALL = (1 << 3) - 1, |
| 535 | }; |
| 536 | // clang-format on |
| 537 | BitField<DirtyFlag> dirty = DIRTY_NONE; |
| 538 | |
| 539 | LocalVector<MDUniformSet *> uniform_sets; |
| 540 | uint32_t dynamic_offsets = 0; |
| 541 | // Bit mask of the uniform sets that are dirty, to prevent redundant binding. |
| 542 | uint64_t uniform_set_mask = 0; |
| 543 | |
| 544 | _FORCE_INLINE_ void reset(); |
| 545 | void end_encoding(); |
| 546 | |
| 547 | _FORCE_INLINE_ void mark_uniforms_dirty() { |
| 548 | if (uniform_sets.is_empty()) { |
| 549 | return; |
| 550 | } |
| 551 | for (uint32_t i = 0; i < uniform_sets.size(); i++) { |
| 552 | if (uniform_sets[i] != nullptr) { |
| 553 | uniform_set_mask |= 1 << i; |
| 554 | } |
| 555 | } |
| 556 | dirty.set_flag(DirtyFlag::DIRTY_UNIFORMS); |
| 557 | } |
| 558 | } compute; |
| 559 | |
| 560 | struct { |
| 561 | NS::SharedPtr<MTL::RenderCommandEncoder> encoder; |
| 562 | |
| 563 | _FORCE_INLINE_ void reset() { |
| 564 | encoder.reset(); |
| 565 | } |
| 566 | } inline_render; |
| 567 | |
| 568 | // State specific to a blit pass. |
| 569 | struct { |
| 570 | NS::SharedPtr<MTL::BlitCommandEncoder> encoder; |
| 571 | _FORCE_INLINE_ void reset() { |
| 572 | encoder.reset(); |
| 573 | } |
| 574 | } blit; |
| 575 | |
| 576 | _FORCE_INLINE_ MTL::CommandBuffer *get_command_buffer() const { |
| 577 | return commandBuffer.get(); |
| 578 | } |
| 579 | |
| 580 | void _begin() override; |
| 581 | void _commit() override; |
| 582 | void _end() override; |
| 583 | |
| 584 | void bind_pipeline(RDD::PipelineID p_pipeline) override; |
| 585 | |
| 586 | #pragma mark - Render Commands |
| 587 | |
| 588 | void render_bind_uniform_sets(VectorView<RDD::UniformSetID> p_uniform_sets, RDD::ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count, uint32_t p_dynamic_offsets) override; |
| 589 | void render_clear_attachments(VectorView<RDD::AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) override; |
| 590 | void render_begin_pass(RDD::RenderPassID p_render_pass, |
| 591 | RDD::FramebufferID p_frameBuffer, |
| 592 | RDD::CommandBufferType p_cmd_buffer_type, |
| 593 | const Rect2i &p_rect, |
| 594 | VectorView<RDD::RenderPassClearValue> p_clear_values) override; |
| 595 | void render_next_subpass() override; |
| 596 | void render_draw(uint32_t p_vertex_count, |
| 597 | uint32_t p_instance_count, |
| 598 | uint32_t p_base_vertex, |
| 599 | uint32_t p_first_instance) override; |
| 600 | void render_bind_vertex_buffers(uint32_t p_binding_count, const RDD::BufferID *p_buffers, const uint64_t *p_offsets, uint64_t p_dynamic_offsets) override; |
| 601 | void render_bind_index_buffer(RDD::BufferID p_buffer, RDD::IndexBufferFormat p_format, uint64_t p_offset) override; |
| 602 | |
| 603 | void render_draw_indexed(uint32_t p_index_count, |
| 604 | uint32_t p_instance_count, |
| 605 | uint32_t p_first_index, |
| 606 | int32_t p_vertex_offset, |
| 607 | uint32_t p_first_instance) override; |
| 608 | |
| 609 | void render_draw_indexed_indirect(RDD::BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) override; |
| 610 | void render_draw_indexed_indirect_count(RDD::BufferID p_indirect_buffer, uint64_t p_offset, RDD::BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) override; |
| 611 | void render_draw_indirect(RDD::BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) override; |
| 612 | void render_draw_indirect_count(RDD::BufferID p_indirect_buffer, uint64_t p_offset, RDD::BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) override; |
| 613 | |
| 614 | void render_end_pass() override; |
| 615 | |
| 616 | #pragma mark - Compute Commands |
| 617 | |
| 618 | void compute_begin_pass() override; |
| 619 | void compute_end_pass() override; |
| 620 | void compute_bind_uniform_sets(VectorView<RDD::UniformSetID> p_uniform_sets, RDD::ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count, uint32_t p_dynamic_offsets) override; |
| 621 | void compute_dispatch(uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) override; |
| 622 | void compute_dispatch_indirect(RDD::BufferID p_indirect_buffer, uint64_t p_offset) override; |
| 623 | |
| 624 | #pragma mark - Transfer |
| 625 | |
| 626 | private: |
| 627 | MTL::RenderCommandEncoder *get_new_render_encoder_with_descriptor(MTL::RenderPassDescriptor *p_desc); |
| 628 | |
| 629 | public: |
| 630 | void resolve_texture(RDD::TextureID p_src_texture, RDD::TextureLayout p_src_texture_layout, uint32_t p_src_layer, uint32_t p_src_mipmap, RDD::TextureID p_dst_texture, RDD::TextureLayout p_dst_texture_layout, uint32_t p_dst_layer, uint32_t p_dst_mipmap) override; |
| 631 | void clear_color_texture(RDD::TextureID p_texture, RDD::TextureLayout p_texture_layout, const Color &p_color, const RDD::TextureSubresourceRange &p_subresources) override; |
| 632 | void clear_depth_stencil_texture(RDD::TextureID p_texture, RDD::TextureLayout p_texture_layout, float p_depth, uint8_t p_stencil, const RDD::TextureSubresourceRange &p_subresources) override; |
| 633 | void clear_buffer(RDD::BufferID p_buffer, uint64_t p_offset, uint64_t p_size) override; |
| 634 | void copy_buffer(RDD::BufferID p_src_buffer, RDD::BufferID p_dst_buffer, VectorView<RDD::BufferCopyRegion> p_regions) override; |
| 635 | void copy_texture(RDD::TextureID p_src_texture, RDD::TextureID p_dst_texture, VectorView<RDD::TextureCopyRegion> p_regions) override; |
| 636 | void copy_buffer_to_texture(RDD::BufferID p_src_buffer, RDD::TextureID p_dst_texture, VectorView<RDD::BufferTextureCopyRegion> p_regions) override; |
| 637 | void copy_texture_to_buffer(RDD::TextureID p_src_texture, RDD::BufferID p_dst_buffer, VectorView<RDD::BufferTextureCopyRegion> p_regions) override; |
| 638 | |
| 639 | #pragma mark - Timestamp |
| 640 | |
| 641 | void timestamp_write(QueryPool *p_pool, uint32_t p_index); |
| 642 | |
| 643 | #pragma mark - Synchronization |
| 644 | |
| 645 | void pipeline_barrier(BitField<RDD::PipelineStageBits> p_src_stages, |
| 646 | BitField<RDD::PipelineStageBits> p_dst_stages, |
| 647 | VectorView<RDD::MemoryAccessBarrier> p_memory_barriers, |
| 648 | VectorView<RDD::BufferBarrier> p_buffer_barriers, |
| 649 | VectorView<RDD::TextureBarrier> p_texture_barriers, |
| 650 | VectorView<RDD::AccelerationStructureBarrier> p_acceleration_structure_barriers) override; |
| 651 | |
| 652 | #pragma mark - Debugging |
| 653 | |
| 654 | void begin_label(const char *p_label_name, const Color &p_color) override; |
| 655 | void end_label() override; |
| 656 | |
| 657 | MDCommandBuffer(MTL::CommandQueue *p_queue, ::RenderingDeviceDriverMetal *p_device_driver); |
| 658 | MDCommandBuffer() = default; |
| 659 | }; |
| 660 | |
| 661 | } // namespace MTL3 |
| 662 | |
| 663 | // C++ helper to get mipmap level size from texture |
| 664 | _FORCE_INLINE_ static MTL::Size mipmapLevelSizeFromTexture(MTL::Texture *p_tex, NS::UInteger p_level) { |
| 665 | MTL::Size lvlSize; |
| 666 | lvlSize.width = MAX(p_tex->width() >> p_level, 1UL); |
| 667 | lvlSize.height = MAX(p_tex->height() >> p_level, 1UL); |
| 668 | lvlSize.depth = MAX(p_tex->depth() >> p_level, 1UL); |
| 669 | return lvlSize; |
| 670 | } |