Blender V5.3
mtl_command_buffer.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_userdef_types.h"
10
11#include "GPU_debug.hh"
12
14
15#include "mtl_backend.hh"
16#include "mtl_command_buffer.hh"
17#include "mtl_common.hh"
18#include "mtl_context.hh"
19#include "mtl_debug.hh"
20#include "mtl_framebuffer.hh"
21
22#include <fstream>
23
24using namespace blender::gpu;
25
26namespace blender::gpu {
27
28static CLG_LogRef LOG = {"gpu.metal"};
29
30/* Counter for active command buffers. */
32
33/* -------------------------------------------------------------------- */
36
37void MTLCommandBufferManager::prepare(bool /*supports_render*/)
38{
39 render_pass_state_.reset_state();
40 compute_state_.reset_state();
41}
42
43void MTLCommandBufferManager::register_encoder_counters()
44{
45 encoder_count_++;
46 empty_ = false;
47}
48
49id<MTLCommandBuffer> MTLCommandBufferManager::ensure_begin()
50{
51 if (active_command_buffer_ == nil) {
52
53 /* Verify number of active command buffers is below limit.
54 * Exceeding this limit will mean we either have a command buffer leak/GPU hang
55 * or we should increase the command buffer limit during MTLQueue creation.
56 * Excessive command buffers can also be caused by frequent GPUContext switches, which cause
57 * the GPU pipeline to flush. This is common during indirect light baking operations.
58 *
59 * NOTE: We currently stall until completion of GPU work upon ::submit if we have reached the
60 * in-flight command buffer limit. */
63
64 if (G.debug & G_DEBUG_GPU) {
65 /* Debug: Enable Advanced Errors for GPU work execution. */
66 MTLCommandBufferDescriptor *desc = [[MTLCommandBufferDescriptor alloc] init];
67 desc.errorOptions = MTLCommandBufferErrorOptionEncoderExecutionStatus;
68 desc.retainedReferences = YES;
69 BLI_assert(context_.queue != nil);
70 active_command_buffer_ = [context_.queue commandBufferWithDescriptor:desc];
71
72 std::string group_name = "CmdBuf: " + GPU_debug_get_groups_names({0, 1});
73 [active_command_buffer_ setLabel:@(group_name.c_str())];
74 [desc release];
75 }
76 else {
77 active_command_buffer_ = [context_.queue commandBuffer];
78 }
79
80 [active_command_buffer_ retain];
81 context_.main_command_buffer.inc_active_command_buffer_count();
83
84 /* Ensure we begin new Scratch Buffer if we are on a new frame. */
85 MTLScratchBufferManager &mem = context_.memory_manager;
87
88 /* Reset Command buffer heuristics. */
89 this->reset_counters();
90 }
91 BLI_assert(active_command_buffer_ != nil);
92 return active_command_buffer_;
93}
94
95/* If wait is true, CPU will stall until GPU work has completed. */
97{
98 /* If we have to wait, we must ensure to either finish the active command encoder or to wait
99 * until all previous one have completed. */
100 if (empty_ && !wait) {
101 /* Skip submission if command buffer is empty. */
102 return false;
103 }
104
105 if (active_command_buffer_ == nil) {
106 if (wait) {
107 /* Wait for any previously submitted work on this context to complete.
108 * (The wait function will yield so may need reworking if this hits a
109 * performance critical path which is sensitive to CPU<->GPU latency) */
111 }
112 return false;
113 }
114
115 /* Ensure current encoders are finished. */
117 BLI_assert(active_command_encoder_type_ == MTL_NO_COMMAND_ENCODER);
118
119 /* Flush active ScratchBuffer associated with parent MTLContext. */
120 context_.memory_manager.flush_active_scratch_buffer();
121
122 /*** Submit Command Buffer. ***/
123 /* Command buffer lifetime tracking. */
124 /* The MTLSafeFreeList referenced since the command buffer was created flags MTLBuffers freed
125 * within the command buffer lifetime as used. Lists are released in order, so this also covers
126 * buffers freed into later lists.
127 * This ensures that in-use resources are not prematurely de-referenced and returned to the
128 * available buffer pool while they are in-use by the GPU. */
129 MTLSafeFreeList *cmd_free_buffer_list = active_safe_free_list_;
130 BLI_assert(cmd_free_buffer_list);
131 active_safe_free_list_ = nullptr;
132
133 id<MTLCommandBuffer> cmd_buffer_ref = active_command_buffer_;
134 [cmd_buffer_ref retain];
135
136 [cmd_buffer_ref addCompletedHandler:^(id<MTLCommandBuffer> /*cb*/) {
137 /* Upon command buffer completion, decrement MTLSafeFreeList reference count
138 * to allow buffers no longer in use by this CommandBuffer to be freed. */
139 cmd_free_buffer_list->decrement_reference();
140
141 /* Release command buffer after completion callback handled. */
142 [cmd_buffer_ref release];
143
144 /* Decrement count. */
145 context_.main_command_buffer.dec_active_command_buffer_count();
146 }];
147
148 /* Submit command buffer to GPU. */
149 [active_command_buffer_ commit];
150
151 /* If we have too many active command buffers in flight, wait until completed to avoid running
152 * out. We can increase */
155 {
156 wait = true;
158 "Maximum number of command buffers in flight. Host will wait until GPU work has "
159 "completed. Consider increasing GHOST_ContextMTL::max_command_buffer_count or reducing "
160 "work fragmentation to better utilize system hardware. Command buffers are flushed upon "
161 "GPUContext switches, this is the most common cause of excessive command buffer "
162 "generation.");
163 }
164
165 if (wait || (G.debug & G_DEBUG_GPU)) {
166 /* Wait until current GPU work has finished executing. */
167 [active_command_buffer_ waitUntilCompleted];
168
169 /* Command buffer execution debugging can return an error message if
170 * execution has failed or encountered GPU-side errors. */
171 if (G.debug & G_DEBUG_GPU) {
172
173 NSError *error = [active_command_buffer_ error];
174 if (error != nil) {
175 CLOG_ERROR(&LOG, "Command buffer error: %s", [[error localizedDescription] UTF8String]);
176 BLI_assert(false);
177 }
178 }
179 }
180
181 /* Release previous frames command buffer and reset active cmd buffer. */
182 if (last_submitted_command_buffer_ != nil) {
183
184 BLI_assert(MTLBackend::get()->is_inside_render_boundary());
185 [last_submitted_command_buffer_ autorelease];
186 last_submitted_command_buffer_ = nil;
187 }
188 last_submitted_command_buffer_ = active_command_buffer_;
189 active_command_buffer_ = nil;
190
191 return true;
192}
193
195
196/* -------------------------------------------------------------------- */
199
200/* Fetch/query current encoder. */
202{
203 return (active_command_encoder_type_ == MTL_RENDER_COMMAND_ENCODER);
204}
205
207{
208 return (active_command_encoder_type_ == MTL_BLIT_COMMAND_ENCODER);
209}
210
212{
213 return (active_command_encoder_type_ == MTL_COMPUTE_COMMAND_ENCODER);
214}
215
217{
218 /* Calling code should check if inside render pass. Otherwise nil. */
219 return active_render_command_encoder_;
220}
221
223{
224 /* Calling code should check if inside render pass. Otherwise nil. */
225 return active_blit_command_encoder_;
226}
227
229{
230 /* Calling code should check if inside render pass. Otherwise nil. */
231 return active_compute_command_encoder_;
232}
233
235{
236 /* If outside of RenderPass, nullptr will be returned. */
237 if (this->is_inside_render_pass()) {
238 return active_frame_buffer_;
239 }
240 return nullptr;
241}
242
243/* Encoder and Pass management. */
244/* End currently active MTLCommandEncoder. */
246{
247 /* End active encoder if one is active. */
248 if (active_command_encoder_type_ == MTL_NO_COMMAND_ENCODER) {
249 /* MTL_NO_COMMAND_ENCODER. */
250 BLI_assert(active_render_command_encoder_ == nil);
251 BLI_assert(active_blit_command_encoder_ == nil);
252 BLI_assert(active_compute_command_encoder_ == nil);
253 return false;
254 }
255
256 if (G.debug & G_DEBUG_GPU) {
257 fold_remaining_debug_groups();
258 }
259
260 switch (active_command_encoder_type_) {
261 case MTL_RENDER_COMMAND_ENCODER:
262 BLI_assert(active_render_command_encoder_ != nil);
263 [active_render_command_encoder_ endEncoding];
264 [active_render_command_encoder_ release];
265 active_render_command_encoder_ = nil;
266 /* Reset associated frame-buffer flag. */
267 if (!retain_framebuffers) {
268 active_frame_buffer_ = nullptr;
269 active_pass_descriptor_ = nullptr;
270 }
271 break;
272 case MTL_BLIT_COMMAND_ENCODER:
273 BLI_assert(active_blit_command_encoder_ != nil);
274 [active_blit_command_encoder_ endEncoding];
275 [active_blit_command_encoder_ release];
276 active_blit_command_encoder_ = nil;
277 break;
278 case MTL_COMPUTE_COMMAND_ENCODER:
279 BLI_assert(active_compute_command_encoder_ != nil);
280 [active_compute_command_encoder_ endEncoding];
281 [active_compute_command_encoder_ release];
282 active_compute_command_encoder_ = nil;
283 break;
284 case MTL_ACCELERATION_STRUCTURE_COMMAND_ENCODER:
285 BLI_assert(active_acceleration_structure_command_encoder_ != nil);
286 [active_acceleration_structure_command_encoder_ endEncoding];
287 [active_acceleration_structure_command_encoder_ release];
288 active_acceleration_structure_command_encoder_ = nil;
289 break;
290 default: {
291 BLI_assert(false && "Invalid command encoder type");
292 return false;
293 }
294 }
295 active_command_encoder_type_ = MTL_NO_COMMAND_ENCODER;
296 return true;
297}
298
300 MTLFrameBuffer *ctx_framebuffer, bool force_begin, bool *r_new_pass)
301{
302 /* Ensure valid frame-buffer. */
303 BLI_assert(ctx_framebuffer != nullptr);
304
305 /* Ensure active command buffer. */
306 id<MTLCommandBuffer> cmd_buf = this->ensure_begin();
307 BLI_assert(cmd_buf);
308
309 /* Begin new command encoder if the currently active one is
310 * incompatible or requires updating. */
311 if (active_command_encoder_type_ != MTL_RENDER_COMMAND_ENCODER ||
312 active_frame_buffer_ != ctx_framebuffer || force_begin)
313 {
315
316 /* Determine if this is a re-bind of the same frame-buffer. */
317 bool is_rebind = (active_frame_buffer_ == ctx_framebuffer);
318
319 /* Generate RenderPassDescriptor from bound frame-buffer. */
320 BLI_assert(ctx_framebuffer);
321 active_frame_buffer_ = ctx_framebuffer;
322 active_pass_descriptor_ = active_frame_buffer_->bake_render_pass_descriptor(
323 is_rebind && (!active_frame_buffer_->get_pending_clear()));
324
325 /* Determine if there is a visibility buffer assigned to the context. */
326 gpu::MTLBuffer *visibility_buffer = context_.get_visibility_buffer();
327 this->active_pass_descriptor_.visibilityResultBuffer =
328 (visibility_buffer) ? visibility_buffer->get_metal_buffer() : nil;
329 context_.clear_visibility_dirty();
330
331 /* Ensure we have already cleaned up our previous render command encoder. */
332 BLI_assert(active_render_command_encoder_ == nil);
333
334 /* Create new RenderCommandEncoder based on descriptor (and begin encoding). */
335 active_render_command_encoder_ = [cmd_buf
336 renderCommandEncoderWithDescriptor:active_pass_descriptor_];
337 [active_render_command_encoder_ retain];
338 active_command_encoder_type_ = MTL_RENDER_COMMAND_ENCODER;
339
340 /* Add debug label. */
341 if (G.debug & G_DEBUG_GPU) {
342 std::string debug_name = "FrameBuf: " + std::string(active_frame_buffer_->name_get());
343 [active_render_command_encoder_ setLabel:@(debug_name.c_str())];
344 }
345
346 /* Unroll pending debug groups. */
347 if (G.debug & G_DEBUG_GPU) {
349 }
350
351 /* Update command buffer encoder heuristics. */
352 this->register_encoder_counters();
353
354 /* Apply initial state. */
355 /* Update Viewport and Scissor State */
356 active_frame_buffer_->apply_state();
357
358 /* FLAG FRAMEBUFFER AS CLEARED -- A clear only lasts as long as one has been specified.
359 * After this, resets to Load attachments to parallel GL behavior. */
360 active_frame_buffer_->mark_cleared();
361
362 /* Reset RenderPassState to ensure resource bindings are re-applied. */
363 render_pass_state_.reset_state();
364
365 /* Return true as new pass started. */
366 *r_new_pass = true;
367 }
368 else {
369 /* No new pass. */
370 *r_new_pass = false;
371 }
372
373 BLI_assert(active_render_command_encoder_ != nil);
374 return active_render_command_encoder_;
375}
376
378{
379 /* Ensure active command buffer. */
380 id<MTLCommandBuffer> cmd_buf = this->ensure_begin();
381 BLI_assert(cmd_buf);
382
383 /* Ensure no existing command encoder of a different type is active. */
384 if (active_command_encoder_type_ != MTL_BLIT_COMMAND_ENCODER) {
386 }
387
388 /* Begin new Blit Encoder. */
389 if (active_blit_command_encoder_ == nil) {
390
391 active_blit_command_encoder_ = [cmd_buf blitCommandEncoder];
392 BLI_assert(active_blit_command_encoder_ != nil);
393 [active_blit_command_encoder_ retain];
394 active_command_encoder_type_ = MTL_BLIT_COMMAND_ENCODER;
395
396 /* Add debug label. */
397 if (G.debug & G_DEBUG_GPU) {
398 std::string debug_name = GPU_debug_get_groups_names({1, 1});
399 [active_blit_command_encoder_ setLabel:@(debug_name.c_str())];
400 }
401 /* Unroll pending debug groups. */
402 if (G.debug & G_DEBUG_GPU) {
404 }
405
406 /* Update command buffer encoder heuristics. */
407 this->register_encoder_counters();
408 }
409 BLI_assert(active_blit_command_encoder_ != nil);
410 return active_blit_command_encoder_;
411}
412
414{
415 /* Ensure active command buffer. */
416 id<MTLCommandBuffer> cmd_buf = this->ensure_begin();
417 BLI_assert(cmd_buf);
418
419 /* Ensure no existing command encoder of a different type is active. */
420 if (active_command_encoder_type_ != MTL_COMPUTE_COMMAND_ENCODER) {
422 }
423
424 /* Begin new Compute Encoder. */
425 if (active_compute_command_encoder_ == nil) {
426 active_compute_command_encoder_ = [cmd_buf computeCommandEncoder];
427 BLI_assert(active_compute_command_encoder_ != nil);
428 [active_compute_command_encoder_ retain];
429 active_command_encoder_type_ = MTL_COMPUTE_COMMAND_ENCODER;
430
431 /* Add debug label. */
432 if (G.debug & G_DEBUG_GPU) {
433 std::string debug_name = GPU_debug_get_groups_names({1, 1});
434 [active_compute_command_encoder_ setLabel:@(debug_name.c_str())];
435 }
436
437 /* Unroll pending debug groups. */
438 if (G.debug & G_DEBUG_GPU) {
440 }
441
442 /* Update command buffer encoder heuristics. */
443 this->register_encoder_counters();
444
445 /* Reset RenderPassState to ensure resource bindings are re-applied. */
446 compute_state_.reset_state();
447 }
448 BLI_assert(active_compute_command_encoder_ != nil);
449 return active_compute_command_encoder_;
450}
451
454{
455 /* Ensure active command buffer. */
456 id<MTLCommandBuffer> cmd_buf = this->ensure_begin();
457 BLI_assert(cmd_buf);
458
459 /* Ensure no existing command encoder of a different type is active. */
460 if (active_command_encoder_type_ != MTL_ACCELERATION_STRUCTURE_COMMAND_ENCODER) {
462 }
463
464 /* Begin new Acceleration Structure Encoder. */
465 if (active_acceleration_structure_command_encoder_ == nil) {
466 active_acceleration_structure_command_encoder_ = [cmd_buf accelerationStructureCommandEncoder];
467 BLI_assert(active_acceleration_structure_command_encoder_ != nil);
468 [active_acceleration_structure_command_encoder_ retain];
469 active_command_encoder_type_ = MTL_ACCELERATION_STRUCTURE_COMMAND_ENCODER;
470
471 /* Add debug label. */
472 if (G.debug & G_DEBUG_GPU) {
473 std::string debug_name = GPU_debug_get_groups_names({1, 1});
474 [active_acceleration_structure_command_encoder_ setLabel:@(debug_name.c_str())];
475 }
476
477 /* Unroll pending debug groups. */
478 if (G.debug & G_DEBUG_GPU) {
480 }
481
482 /* Update command buffer encoder heuristics. */
483 this->register_encoder_counters();
484 }
485 BLI_assert(active_acceleration_structure_command_encoder_ != nil);
486 return active_acceleration_structure_command_encoder_;
487}
488
490
491/* -------------------------------------------------------------------- */
494
495/* Rendering Heuristics. */
497{
498 current_draw_call_count_++;
499 vertex_submitted_count_ += vertex_submission;
500 empty_ = false;
501}
502
503/* Reset workload counters. */
505{
506 empty_ = true;
507 current_draw_call_count_ = 0;
508 encoder_count_ = 0;
509 vertex_submitted_count_ = 0;
510}
511
512/* Workload evaluation. */
514{
515 /* Skip if no active command buffer. */
516 if (active_command_buffer_ == nil) {
517 return false;
518 }
519
520 /* Use optimized heuristic to split heavy command buffer submissions to better saturate the
521 * hardware and also reduce stalling from individual large submissions. */
524 {
525 return ((current_draw_call_count_ > 30000) || (vertex_submitted_count_ > 100000000) ||
526 (encoder_count_ > 25));
527 }
528 /* Apple Silicon is less efficient if splitting submissions. */
529 return false;
530}
531
533
534/* -------------------------------------------------------------------- */
537
538/* Debug. */
539void MTLCommandBufferManager::push_debug_group(const char *name, int /*index*/)
540{
541 /* Only perform this operation if capturing. */
542 MTLCaptureManager *capture_manager = [MTLCaptureManager sharedCaptureManager];
543 if (![capture_manager isCapturing]) {
544 return;
545 }
546
547 if (active_command_buffer_ == nil) {
548 return;
549 }
550
551 id<MTLCommandBuffer> cmd = this->ensure_begin();
552 if (cmd != nil) {
553 switch (active_command_encoder_type_) {
554 case MTL_RENDER_COMMAND_ENCODER:
555 [active_render_command_encoder_ pushDebugGroup:@(name)];
556 break;
557 case MTL_BLIT_COMMAND_ENCODER:
558 [active_blit_command_encoder_ pushDebugGroup:@(name)];
559 break;
560 case MTL_COMPUTE_COMMAND_ENCODER:
561 [active_compute_command_encoder_ pushDebugGroup:@(name)];
562 break;
563 default:
564 // [active_command_buffer_ pushDebugGroup:@(name)];
565 break;
566 }
567 }
568}
569
571{
572 /* Only perform this operation if capturing. */
573 MTLCaptureManager *capture_manager = [MTLCaptureManager sharedCaptureManager];
574 if (![capture_manager isCapturing]) {
575 return;
576 }
577
578 if (active_command_buffer_ == nil) {
579 return;
580 }
581
582 id<MTLCommandBuffer> cmd = this->ensure_begin();
583 if (cmd != nil) {
584 switch (active_command_encoder_type_) {
585 case MTL_RENDER_COMMAND_ENCODER:
586 [active_render_command_encoder_ popDebugGroup];
587 break;
588 case MTL_BLIT_COMMAND_ENCODER:
589 [active_blit_command_encoder_ popDebugGroup];
590 break;
591 case MTL_COMPUTE_COMMAND_ENCODER:
592 [active_compute_command_encoder_ popDebugGroup];
593 break;
594 default:
595 // [active_command_buffer_ popDebugGroup];
596 break;
597 }
598 }
599}
600
602{
603 /* Only perform this operation if capturing. */
604 MTLCaptureManager *capture_manager = [MTLCaptureManager sharedCaptureManager];
605 if (![capture_manager isCapturing]) {
606 return;
607 }
608
609 if (active_command_buffer_ != nil) {
611 const DebugStack &gpu_stack = ctx->debug_stack;
612 DebugStack &mtl_stack = mtl_debug_stack_;
613 /* Bottom level group is the label of the command buffer. */
614 Span<StringRef> groups = gpu_stack.as_span().drop_front(1);
615
616 /* Close closed groups. */
617 for ([[maybe_unused]] int i : mtl_stack.index_range().drop_front(groups.size())) {
619 mtl_stack.pop_last();
620 }
621 BLI_assert(mtl_stack.size() <= groups.size());
622 /* Verify all open groups are the same on both stack. */
623 int first_mismatch = 0;
624 for (int i : mtl_stack.index_range()) {
625 if (mtl_stack[i] != groups[i]) {
626 break;
627 }
628 first_mismatch++;
629 }
630 /* Discard the ones that are not. */
631 for ([[maybe_unused]] int i : mtl_stack.index_range().drop_front(first_mismatch)) {
633 mtl_stack.pop_last();
634 }
635 BLI_assert(mtl_stack.size() <= groups.size());
636 /* Add new groups not present in the metal stack. */
637 for (const StringRef &name : groups.drop_front(mtl_stack.size())) {
638 std::string s_name = name;
639 push_debug_group(s_name.c_str(), 0);
640 mtl_stack.append(name);
641 }
642 BLI_assert(mtl_stack.size() == groups.size());
643 }
644}
645
646void MTLCommandBufferManager::fold_remaining_debug_groups()
647{
648 /* Only perform this operation if capturing. */
649 MTLCaptureManager *capture_manager = [MTLCaptureManager sharedCaptureManager];
650 if (![capture_manager isCapturing]) {
651 return;
652 }
653
654 if (active_command_buffer_ != nil) {
655 DebugStack &mtl_stack = mtl_debug_stack_;
656 for ([[maybe_unused]] int i : mtl_stack.index_range()) {
658 mtl_stack.pop_last();
659 }
660 }
661}
662
663/* Workload Synchronization. */
665 GPUStageBarrierBits before_stages,
666 GPUStageBarrierBits after_stages)
667{
668 /* Apple Silicon does not support memory barriers for RenderCommandEncoder's.
669 * We do not currently need these due to implicit API guarantees. However, render->render
670 * resource dependencies are only evaluated at RenderCommandEncoder boundaries due to work
671 * execution on TBDR architecture.
672 *
673 * NOTE: Render barriers are therefore inherently expensive. Where possible, opt for local
674 * synchronization using raster order groups, or, prefer compute to avoid subsequent passes
675 * re-loading pass attachments which are not needed. */
676 const bool is_tile_based_arch = (GPU_platform_architecture() == GPU_ARCHITECTURE_TBDR);
677 if (is_tile_based_arch) {
678 if (active_command_encoder_type_ == MTL_RENDER_COMMAND_ENCODER) {
679 /* Break render pass to ensure final pass results are visible to subsequent calls. */
681 return true;
682 }
683 /* Skip all barriers for compute and blit passes as Metal will resolve these dependencies. */
684 return false;
685 }
686
687 /* Resolve scope. */
688 MTLBarrierScope scope = 0;
689 if (barrier_bits & GPU_BARRIER_SHADER_IMAGE_ACCESS || barrier_bits & GPU_BARRIER_TEXTURE_FETCH) {
690 bool is_compute = (active_command_encoder_type_ != MTL_RENDER_COMMAND_ENCODER);
691 scope |= (is_compute ? 0 : MTLBarrierScopeRenderTargets) | MTLBarrierScopeTextures;
692 }
693 if (barrier_bits & GPU_BARRIER_SHADER_STORAGE ||
694 barrier_bits & GPU_BARRIER_VERTEX_ATTRIB_ARRAY || barrier_bits & GPU_BARRIER_ELEMENT_ARRAY ||
695 barrier_bits & GPU_BARRIER_UNIFORM || barrier_bits & GPU_BARRIER_BUFFER_UPDATE)
696 {
697 scope = scope | MTLBarrierScopeBuffers;
698 }
699
700 if (scope != 0) {
701 /* Issue barrier based on encoder. */
702 switch (active_command_encoder_type_) {
703 case MTL_NO_COMMAND_ENCODER:
704 case MTL_BLIT_COMMAND_ENCODER:
705 case MTL_ACCELERATION_STRUCTURE_COMMAND_ENCODER: {
706 /* No barrier to be inserted. */
707 return false;
708 }
709
710 /* Rendering. */
711 case MTL_RENDER_COMMAND_ENCODER: {
712 /* Currently flagging both stages -- can use bits above to filter on stage type --
713 * though full barrier is safe for now. */
714 MTLRenderStages before_stage_flags = 0;
715 MTLRenderStages after_stage_flags = 0;
716 if (before_stages & GPU_BARRIER_STAGE_VERTEX &&
717 !(before_stages & GPU_BARRIER_STAGE_FRAGMENT))
718 {
719 before_stage_flags = before_stage_flags | MTLRenderStageVertex;
720 }
721 if (before_stages & GPU_BARRIER_STAGE_FRAGMENT) {
722 before_stage_flags = before_stage_flags | MTLRenderStageFragment;
723 }
724 if (after_stages & GPU_BARRIER_STAGE_VERTEX) {
725 after_stage_flags = after_stage_flags | MTLRenderStageVertex;
726 }
727 if (after_stages & GPU_BARRIER_STAGE_FRAGMENT) {
728 after_stage_flags = MTLRenderStageFragment;
729 }
730
732 BLI_assert(rec != nil);
733 [rec memoryBarrierWithScope:scope
734 afterStages:after_stage_flags
735 beforeStages:before_stage_flags];
736 return true;
737 }
738
739 /* Compute. */
740 case MTL_COMPUTE_COMMAND_ENCODER: {
742 BLI_assert(rec != nil);
743 [rec memoryBarrierWithScope:scope];
744 return true;
745 }
746 }
747 }
748 /* No barrier support. */
749 return false;
750}
751
753{
754 /* Ensure active command buffer. */
755 id<MTLCommandBuffer> cmd_buf = this->ensure_begin();
756 BLI_assert(cmd_buf);
758 [cmd_buf encodeSignalEvent:event value:signal_value];
759 register_encoder_counters();
760}
761
763{
764 /* Ensure active command buffer. */
765 id<MTLCommandBuffer> cmd_buf = this->ensure_begin();
766 BLI_assert(cmd_buf);
768 [cmd_buf encodeWaitForEvent:event value:signal_value];
769 register_encoder_counters();
770}
771
773
774/* -------------------------------------------------------------------- */
777
778/* Reset binding state when a new RenderCommandEncoder is bound, to ensure
779 * pipeline resources are re-applied to the new Encoder.
780 * NOTE: In Metal, state is only persistent within an MTLCommandEncoder,
781 * not globally. */
783{
784 /* Reset Cached pipeline state. */
785 this->bound_pso = nil;
786 this->bound_ds_state = nil;
787
788 /* Clear shader binding. */
789 this->last_bound_shader_state = {nullptr, 0};
790
791 /* Other states. */
794 this->last_scissor_rect = {0,
795 0,
796 (uint)((fb != nullptr) ? fb->get_width() : 0),
797 (uint)((fb != nullptr) ? fb->get_height() : 0)};
798
799 this->vertex_bindings = {};
800 this->fragment_bindings = {};
801}
802
804{
805 /* Reset Cached pipeline state. */
806 this->bound_pso = nil;
807
808 this->compute_bindings = {};
809}
810
812{
813 if (this->bound_pso != pso) {
815 [rec setComputePipelineState:pso];
816 this->bound_pso = pso;
817 }
818}
819
821{
822 this->compute_bindings.bind_texture(this->cmd.get_active_compute_command_encoder(), tex, slot);
823}
824
826 bool use_samplers_argument_buffer,
827 uint slot)
828{
830 this->ctx.get_sampler_array(),
831 sampler_binding.get_mtl_sampler(this->ctx),
832 sampler_binding.state,
833 use_samplers_argument_buffer,
834 slot);
835}
836
838{
839 this->compute_bindings.bind_buffer(
840 this->cmd.get_active_compute_command_encoder(), buffer, buffer_offset, index);
841}
842
844{
846 this->ctx.get_scratch_buffer_manager(),
847 bytes,
848 length,
849 index);
850}
851
853{
854 this->vertex_bindings.bind_texture(this->cmd.get_active_render_command_encoder(), tex, slot);
855}
856
858 bool use_samplers_argument_buffer,
859 uint slot)
860{
861 this->vertex_bindings.bind_sampler(this->cmd.get_active_render_command_encoder(),
862 this->ctx.get_sampler_array(),
863 sampler_binding.get_mtl_sampler(this->ctx),
864 sampler_binding.state,
865 use_samplers_argument_buffer,
866 slot);
867}
868
870 uint64_t buffer_offset,
871 uint index)
872{
873 this->vertex_bindings.bind_buffer(
874 this->cmd.get_active_render_command_encoder(), buffer, buffer_offset, index);
875}
876
878{
880 this->ctx.get_scratch_buffer_manager(),
881 bytes,
882 length,
883 index);
884}
885
890
892 bool use_samplers_argument_buffer,
893 uint slot)
894{
896 this->ctx.get_sampler_array(),
897 sampler_binding.get_mtl_sampler(this->ctx),
898 sampler_binding.state,
899 use_samplers_argument_buffer,
900 slot);
901}
902
904 uint64_t buffer_offset,
905 uint index)
906{
907 this->fragment_bindings.bind_buffer(
908 this->cmd.get_active_render_command_encoder(), buffer, buffer_offset, index);
909}
910
912{
914 this->ctx.get_scratch_buffer_manager(),
915 bytes,
916 length,
917 index);
918}
919
921
927
929{
930 [enc setBufferOffset:offset atIndex:index];
931}
933{
934 [enc setBuffer:buf offset:offset atIndex:index];
935}
936void MTLComputeCommandEncoder::set_bytes(const void *bytes, size_t length, int index)
937{
938 [enc setBytes:bytes length:length atIndex:index];
939}
941{
942 [enc setTexture:tex atIndex:index];
943}
945{
946 [enc setSamplerState:sampler_state atIndex:index];
947}
949 int index)
950{
951 [enc setAccelerationStructure:accel atBufferIndex:index];
952}
953void MTLComputeCommandEncoder::use_resource(id<MTLResource> resource, MTLResourceUsage usage)
954{
955 [enc useResource:resource usage:usage];
956}
957
959{
960 [enc setVertexBufferOffset:offset atIndex:index];
961}
963{
964 [enc setVertexBuffer:buf offset:offset atIndex:index];
965}
966void MTLVertexCommandEncoder::set_bytes(const void *bytes, size_t length, int index)
967{
968 [enc setVertexBytes:bytes length:length atIndex:index];
969}
971{
972 [enc setVertexTexture:tex atIndex:index];
973}
975{
976 [enc setVertexSamplerState:sampler_state atIndex:index];
977}
979 int index)
980{
981 [enc setVertexAccelerationStructure:accel atBufferIndex:index];
982}
983void MTLVertexCommandEncoder::use_resource(id<MTLResource> resource, MTLResourceUsage usage)
984{
985 [enc useResource:resource usage:usage stages:MTLRenderStageVertex];
986}
987
989{
990 [enc setFragmentBufferOffset:offset atIndex:index];
991}
993{
994 [enc setFragmentBuffer:buf offset:offset atIndex:index];
995}
996void MTLFragmentCommandEncoder::set_bytes(const void *bytes, size_t length, int index)
997{
998 [enc setFragmentBytes:bytes length:length atIndex:index];
999}
1001{
1002 [enc setFragmentTexture:tex atIndex:index];
1003}
1005{
1006 [enc setFragmentSamplerState:sampler_state atIndex:index];
1007}
1009 int index)
1010{
1011 [enc setFragmentAccelerationStructure:accel atBufferIndex:index];
1012}
1013void MTLFragmentCommandEncoder::use_resource(id<MTLResource> resource, MTLResourceUsage usage)
1014{
1015 [enc useResource:resource usage:usage stages:MTLRenderStageFragment];
1016}
1017
1018} // namespace blender::gpu
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define BLI_assert(a)
static const int max_command_buffer_count
constexpr Span drop_front(int64_t n) const
Definition BLI_span.hh:175
constexpr int64_t size() const
Definition BLI_span.hh:256
static MTLBackend * get()
MTLSafeFreeList * acquire_current_safe_list()
id< MTLBuffer > get_metal_buffer() const
bool end_active_command_encoder(bool retain_framebuffers=false)
void encode_signal_event(id< MTLEvent > event, uint64_t value)
id< MTLComputeCommandEncoder > get_active_compute_command_encoder()
void register_draw_counters(int vertex_submission)
void prepare(bool supports_render=true)
id< MTLRenderCommandEncoder > ensure_begin_render_command_encoder(MTLFrameBuffer *ctx_framebuffer, bool force_begin, bool *r_new_pass)
id< MTLComputeCommandEncoder > ensure_begin_compute_encoder()
bool insert_memory_barrier(GPUBarrier barrier_bits, GPUStageBarrierBits before_stages, GPUStageBarrierBits after_stages)
id< MTLBlitCommandEncoder > ensure_begin_blit_encoder()
void push_debug_group(const char *name, int index)
id< MTLAccelerationStructureCommandEncoder > ensure_begin_acceleration_structure_encoder()
id< MTLRenderCommandEncoder > get_active_render_command_encoder()
static volatile std::atomic< int > num_active_cmd_bufs_in_system
void encode_wait_for_event(id< MTLEvent > event, uint64_t value)
id< MTLBlitCommandEncoder > get_active_blit_command_encoder()
void bind_compute_bytes(const void *bytes, uint64_t length, uint index)
id< MTLComputePipelineState > bound_pso
MTLCommandBufferManager & cmd
void bind_compute_texture(id< MTLTexture > tex, uint slot)
MTLBindingCache< gpu::MTLComputeCommandEncoder > compute_bindings
void bind_compute_buffer(id< MTLBuffer > buffer, uint64_t buffer_offset, uint index)
void bind_compute_sampler(MTLSamplerBinding &sampler_binding, bool use_samplers_argument_buffer, uint slot)
void bind_pso(id< MTLComputePipelineState > pso)
id< MTLSamplerState > get_sampler_from_state(MTLSamplerState state)
static MTLContext * get()
id< MTLSamplerState > get_default_sampler_state()
id< MTLCommandQueue > queue
static MTLBufferPool * get_global_memory_manager()
MTLRenderPassDescriptor * bake_render_pass_descriptor(bool load_contents)
void bind_fragment_bytes(const void *bytes, uint64_t length, uint index)
void bind_vertex_buffer(id< MTLBuffer > buffer, uint64_t buffer_offset, uint index)
MTLBindingCache< gpu::MTLVertexCommandEncoder > vertex_bindings
void bind_vertex_texture(id< MTLTexture > tex, uint slot)
void bind_vertex_bytes(const void *bytes, uint64_t length, uint index)
id< MTLDepthStencilState > bound_ds_state
MTLCommandBufferManager & cmd
MTLBindingCache< gpu::MTLFragmentCommandEncoder > fragment_bindings
void bind_vertex_sampler(MTLSamplerBinding &sampler_binding, bool use_samplers_argument_buffer, uint slot)
id< MTLRenderPipelineState > bound_pso
void bind_fragment_sampler(MTLSamplerBinding &sampler_binding, bool use_samplers_argument_buffer, uint slot)
MTLBoundShaderState last_bound_shader_state
void bind_fragment_texture(id< MTLTexture > tex, uint slot)
void bind_fragment_buffer(id< MTLBuffer > buffer, uint64_t buffer_offset, uint index)
float length(VecOp< float, D >) RET
#define MTL_LOG_WARNING(info,...)
Definition mtl_debug.hh:39
static CLG_LogRef LOG
const MTLSamplerState DEFAULT_SAMPLER_STATE
Vector< StringRef > DebugStack
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
GPUStageBarrierBits
Definition GPU_state.hh:68
@ GPU_BARRIER_STAGE_FRAGMENT
Definition GPU_state.hh:70
@ GPU_BARRIER_STAGE_VERTEX
Definition GPU_state.hh:69
unsigned int uint
Definition BKE_text.h:55
BLI_INLINE float fb(float length, float L)
Global G
Definition blender.cc:57
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
@ GPU_ARCHITECTURE_TBDR
GPUArchitectureType GPU_platform_architecture()
@ GPU_BARRIER_VERTEX_ATTRIB_ARRAY
Definition GPU_state.hh:53
@ GPU_BARRIER_BUFFER_UPDATE
Definition GPU_state.hh:59
@ GPU_BARRIER_TEXTURE_FETCH
Definition GPU_state.hh:40
@ GPU_BARRIER_SHADER_STORAGE
Definition GPU_state.hh:51
@ GPU_BARRIER_UNIFORM
Definition GPU_state.hh:57
@ GPU_BARRIER_SHADER_IMAGE_ACCESS
Definition GPU_state.hh:38
@ GPU_BARRIER_ELEMENT_ARRAY
Definition GPU_state.hh:55
void GPU_debug_get_groups_names(int name_buf_maxncpy, char *r_name_buf)
Definition gpu_debug.cc:44
@ GPU_DEVICE_INTEL
void set_sampler(id< MTLSamplerState > sampler_state, int index)
void set_bytes(const void *bytes, size_t length, int index)
void set_buffer_offset(size_t offset, int index)
void set_buffer(id< MTLBuffer > buf, size_t offset, int index)
void set_texture(id< MTLTexture > tex, int index)
id< MTLComputeCommandEncoder > enc
void use_resource(id< MTLResource > resource, MTLResourceUsage usage)
void set_acceleration_structure(id< MTLAccelerationStructure > accel, int index)
void set_acceleration_structure(id< MTLAccelerationStructure > accel, int index)
void set_texture(id< MTLTexture > tex, int index)
void set_buffer_offset(size_t offset, int index)
void set_sampler(id< MTLSamplerState > sampler_state, int index)
void set_buffer(id< MTLBuffer > buf, size_t offset, int index)
void use_resource(id< MTLResource > resource, MTLResourceUsage usage)
void set_bytes(const void *bytes, size_t length, int index)
id< MTLSamplerState > get_mtl_sampler(MTLContext &ctx)
void set_bytes(const void *bytes, size_t length, int index)
void set_buffer_offset(size_t offset, int index)
void use_resource(id< MTLResource > resource, MTLResourceUsage usage)
void set_acceleration_structure(id< MTLAccelerationStructure > accel, int index)
void set_sampler(id< MTLSamplerState > sampler_state, int index)
void set_buffer(id< MTLBuffer > buf, size_t offset, int index)
void set_texture(id< MTLTexture > tex, int index)
int size() const
Definition vector.hh:130