Blender V5.3
mtl_context.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#include "mtl_context.hh"
9#include "mtl_debug.hh"
10#include "mtl_framebuffer.hh"
11#include "mtl_immediate.hh"
12#include "mtl_memory.hh"
13#include "mtl_primitive.hh"
14#include "mtl_ray_tracing.hh"
15#include "mtl_shader.hh"
18#include "mtl_state.hh"
19#include "mtl_storage_buffer.hh"
20#include "mtl_uniform_buffer.hh"
21#include "mtl_vertex_buffer.hh"
22
23#include "DNA_userdef_types.h"
24
25#include "GPU_capabilities.hh"
26#include "GPU_matrix.hh"
27#include "GPU_shader.hh"
28#include "GPU_storage_buffer.hh"
29#include "GPU_texture.hh"
30#include "GPU_uniform_buffer.hh"
31#include "GPU_vertex_buffer.hh"
33
34#include "BLI_time.hh"
35
36#include <fstream>
37#include <string>
38
39using namespace blender::gpu;
40
41/* Fire off a single dispatch per encoder. Can make debugging view clearer for texture resources
42 * associated with each dispatch. */
43#define MTL_DEBUG_SINGLE_DISPATCH_PER_ENCODER 0
44
45/* Debug option to bind null buffer for missing UBOs. */
46#define DEBUG_BIND_NULL_BUFFER_FOR_MISSING_UBO 0
47
48/* Debug option to bind null buffer for missing SSBOs. NOTE: This is unsafe if replacing a
49 * write-enabled SSBO and should only be used for debugging to identify binding-related issues. */
50#define DEBUG_BIND_NULL_BUFFER_FOR_MISSING_SSBO 0
51
52/* Error or warning depending on debug flag. */
53#if DEBUG_BIND_NULL_BUFFER_FOR_MISSING_UBO == 1
54# define MTL_LOG_UBO_ERROR MTL_LOG_WARNING
55#else
56# define MTL_LOG_UBO_ERROR MTL_LOG_ERROR
57#endif
58
59#if DEBUG_BIND_NULL_BUFFER_FOR_MISSING_SSBO == 1
60# define MTL_LOG_SSBO_ERROR MTL_LOG_WARNING
61#else
62# define MTL_LOG_SSBO_ERROR MTL_LOG_ERROR
63#endif
64
65namespace blender::gpu {
66
67static CLG_LogRef LOG = {"gpu.metal"};
68
69/* Global memory manager. */
73
74/* Swap-chain and latency management. */
75std::atomic<int> MTLContext::max_drawables_in_flight = 0;
76std::atomic<int64_t> MTLContext::avg_drawable_latency_us = 0;
78
79/* -------------------------------------------------------------------- */
82
83void MTLContext::set_ghost_context(GHOST_IContext *ghostCtxHandle)
84{
85 GHOST_Context *ghost_ctx = static_cast<GHOST_Context *>(ghostCtxHandle);
86 BLI_assert(ghost_ctx != nullptr);
87
88 /* Release old MTLTexture handle */
89 if (default_fbo_mtltexture_) {
90 [default_fbo_mtltexture_ release];
91 default_fbo_mtltexture_ = nil;
92 }
93
94 /* Release Framebuffer attachments */
95 MTLFrameBuffer *mtl_front_left = static_cast<MTLFrameBuffer *>(this->front_left);
96 MTLFrameBuffer *mtl_back_left = static_cast<MTLFrameBuffer *>(this->back_left);
97 mtl_front_left->remove_all_attachments();
98 mtl_back_left->remove_all_attachments();
99
100 GHOST_ContextMTL *ghost_mtl_ctx = dynamic_cast<GHOST_ContextMTL *>(ghost_ctx);
101 if (ghost_mtl_ctx != nullptr) {
102 default_fbo_mtltexture_ = ghost_mtl_ctx->metalOverlayTexture();
103
105 "Binding GHOST context MTL %p to GPU context %p. (Device: %p, queue: %p, texture: %p)",
106 ghost_mtl_ctx,
107 this,
108 this->device,
109 this->queue,
110 default_fbo_gputexture_);
111
112 /* Check if the GHOST Context provides a default framebuffer: */
113 if (default_fbo_mtltexture_) {
114
115 /* Release old gpu::Texture handle */
116 if (default_fbo_gputexture_) {
117 GPU_texture_free(default_fbo_gputexture_);
118 default_fbo_gputexture_ = nullptr;
119 }
120
121 /* Retain handle */
122 [default_fbo_mtltexture_ retain];
123
124 /*** Create front and back-buffers ***/
125 /* Create gpu::MTLTexture objects */
126 default_fbo_gputexture_ = new gpu::MTLTexture("MTL_BACKBUFFER",
127 TextureFormat::SFLOAT_16_16_16_16,
129 default_fbo_mtltexture_);
130
131 /* Update frame-buffers with new texture attachments. */
132 mtl_front_left->add_color_attachment(default_fbo_gputexture_, 0, 0, 0);
133 mtl_back_left->add_color_attachment(default_fbo_gputexture_, 0, 0, 0);
134#ifndef NDEBUG
135 this->label = default_fbo_mtltexture_.label;
136#endif
137 }
138 else {
139
140 /* Add default texture for cases where no other framebuffer is bound */
141 if (!default_fbo_gputexture_) {
142 default_fbo_gputexture_ = static_cast<gpu::MTLTexture *>(
143 GPU_texture_create_2d(__func__,
144 16,
145 16,
146 1,
147 TextureFormat::SFLOAT_16_16_16_16,
149 nullptr));
150 }
151 mtl_back_left->add_color_attachment(default_fbo_gputexture_, 0, 0, 0);
152
154 "-- Bound context %p for GPU context: %p is offscreen and does not have a default "
155 "framebuffer",
156 ghost_mtl_ctx,
157 this);
158#ifndef NDEBUG
159 this->label = @"Offscreen Metal Context";
160#endif
161 }
162 }
163 else {
165 " Failed to bind GHOST context to MTLContext -- GHOST_ContextMTL is null "
166 "(GhostContext: %p, GhostContext_MTL: %p)",
167 ghost_ctx,
168 ghost_mtl_ctx);
169 BLI_assert(false);
170 }
171}
172
173void MTLContext::set_ghost_window(GHOST_IWindow *ghostWinHandle)
174{
175 GHOST_Window *ghostWin = static_cast<GHOST_Window *>(ghostWinHandle);
176 this->set_ghost_context(ghostWin ? ghostWin->getContext() : nullptr);
177}
178
180
181/* -------------------------------------------------------------------- */
184
185/* Placeholder functions */
187 : memory_manager(*this), main_command_buffer(*this)
188{
189 /* Init debug. */
191
192 /* Initialize Render-pass and Frame-buffer State. */
193 this->back_left = nullptr;
194
195 /* Initialize command buffer state. */
197
198 /* Frame management. */
199 is_inside_frame_ = false;
200 current_frame_index_ = 0;
201
202 /* Prepare null data buffer. */
203 null_buffer_ = nil;
204 null_attribute_buffer_ = nil;
205
206 /* Zero-initialize MTL textures. */
207 default_fbo_mtltexture_ = nil;
208 default_fbo_gputexture_ = nullptr;
209
210 ghost_window_ = ghost_window;
211 this->ghost_context_ = static_cast<GHOST_ContextMTL *>(ghost_context);
213 this->queue = (id<MTLCommandQueue>)this->ghost_context_->metalCommandQueue();
214 this->device = (id<MTLDevice>)this->ghost_context_->metalDevice();
215 BLI_assert(this->queue);
216 BLI_assert(this->device);
217 [this->queue retain];
218 [this->device retain];
219
220#pragma clang diagnostic push
221#pragma clang diagnostic ignored "-Wobjc-method-access"
222 /* Enable increased concurrent shader compiler limit.
223 * NOTE: Disable warning for missing method when building on older OS's, as compiled code will
224 * still work correctly when run on a system with the API available. */
225 if (@available(macOS 13.3, *)) {
226 [this->device setShouldMaximizeConcurrentCompilation:YES];
227 }
228#pragma clang diagnostic pop
229
230 /* Register present callback. */
231 this->ghost_context_->metalRegisterPresentCallback(&present);
232 this->ghost_context_->metalRegisterXrBlitCallback(&xr_blit);
233
234 /* Create FrameBuffer handles. */
235 MTLFrameBuffer *mtl_front_left = new MTLFrameBuffer(this, "front_left");
236 MTLFrameBuffer *mtl_back_left = new MTLFrameBuffer(this, "back_left");
237 this->front_left = mtl_front_left;
238 this->back_left = mtl_back_left;
239 this->active_fb = this->back_left;
240
241 /* Prepare platform and capabilities. (NOTE: With METAL, this needs to be done after CTX
242 * initialization). */
243 MTLBackend::platform_init(this);
244 MTLBackend::capabilities_init(this);
245
246 /* Ensure global memory manager is initialized. */
249
250 /* Initialize Metal modules. */
251 this->memory_manager.init();
252 this->state_manager = new MTLStateManager(this);
253 this->imm = new MTLImmediate(this);
254
255 /* Initialize texture read/update structures. */
256 this->get_texture_utils().init();
257
258 /* Bound Samplers struct. */
259 for (int i = 0; i < MTL_MAX_TEXTURE_SLOTS; i++) {
260 samplers_.mtl_sampler[i] = nil;
261 samplers_.mtl_sampler_flags[i] = DEFAULT_SAMPLER_STATE;
262 }
263
264 /* Initialize samplers. */
266}
267
269{
270 BLI_assert(this == MTLContext::get());
271 /* Ensure rendering is complete command encoders/command buffers are freed. */
272 if (MTLBackend::get()->is_inside_render_boundary()) {
273 this->finish();
274
275 /* End frame. */
276 if (this->get_inside_frame()) {
277 this->end_frame();
278 }
279 }
280
281 /* Wait for all GPU work to finish. */
283
284 /* Free textures and frame-buffers in base class. */
286
287 /* Release context textures. */
288 if (default_fbo_gputexture_) {
289 GPU_texture_free(default_fbo_gputexture_);
290 default_fbo_gputexture_ = nullptr;
291 }
292 if (default_fbo_mtltexture_) {
293 [default_fbo_mtltexture_ release];
294 default_fbo_mtltexture_ = nil;
295 }
296
297 /* Release Memory Manager */
299
300 /* Release update/blit shaders. */
301 this->get_texture_utils().cleanup();
302 this->get_compute_utils().cleanup();
303
304 /* Detach resource references. */
306
307 /* Unbind UBOs. */
308 for (auto &ubo_bind : this->pipeline_state.ubo_bindings) {
309 if (ubo_bind.bound && ubo_bind.ubo != nullptr) {
310 GPU_uniformbuf_unbind(ubo_bind.ubo);
311 }
312 }
313
314 /* Unbind SSBOs. */
315 for (auto &ssbo_bind : this->pipeline_state.ssbo_bindings) {
316 if (ssbo_bind.bound && ssbo_bind.ssbo != nullptr) {
317 ssbo_bind.ssbo->unbind();
318 }
319 }
320
321 /* Release Dummy resources. */
322 this->free_dummy_resources();
323
324 /* Release Sampler States. */
325 for (int extend_yz_i = 0; extend_yz_i < GPU_SAMPLER_EXTEND_MODES_COUNT; extend_yz_i++) {
326 for (int extend_x_i = 0; extend_x_i < GPU_SAMPLER_EXTEND_MODES_COUNT; extend_x_i++) {
327 for (int filtering_i = 0; filtering_i < GPU_SAMPLER_FILTERING_TYPES_COUNT; filtering_i++) {
328 if (sampler_state_cache_[extend_yz_i][extend_x_i][filtering_i] != nil) {
329 [sampler_state_cache_[extend_yz_i][extend_x_i][filtering_i] release];
330 sampler_state_cache_[extend_yz_i][extend_x_i][filtering_i] = nil;
331 }
332 }
333 }
334 }
335
336 /* Release Custom Sampler States. */
337 for (int i = 0; i < GPU_SAMPLER_CUSTOM_TYPES_COUNT; i++) {
338 if (custom_sampler_state_cache_[i] != nil) {
339 [custom_sampler_state_cache_[i] release];
340 custom_sampler_state_cache_[i] = nil;
341 }
342 }
343
344 /* Empty cached sampler argument buffers. */
345 for (auto *entry : cached_sampler_buffers_.values()) {
346 entry->free();
347 }
348 cached_sampler_buffers_.clear();
349
350 /* Free null buffers. */
351 if (null_buffer_) {
352 [null_buffer_ release];
353 }
354 if (null_attribute_buffer_) {
355 [null_attribute_buffer_ release];
356 }
357
358 /* Release memory manager reference. */
360
361 /* Free Metal objects. */
362 if (this->queue) {
363 [this->queue release];
364 }
365 if (this->device) {
366 [this->device release];
367 }
368
369 this->process_frame_timings();
370}
371
373{
374 BLI_assert(MTLBackend::get()->is_inside_render_boundary());
375 if (this->get_inside_frame()) {
376 return;
377 }
378
379 /* Begin Command buffer for next frame. */
380 is_inside_frame_ = true;
381}
382
384{
386
387 /* Ensure pre-present work is committed. */
388 this->flush();
389
390 /* Increment frame counter. */
391 is_inside_frame_ = false;
392
393 this->process_frame_timings();
394}
395
396void MTLContext::check_error(const char * /*info*/)
397{
398 /* TODO(Metal): Implement. */
399}
400
402{
403 /* Make sure no other context is already bound to this thread. */
404 BLI_assert(is_active_ == false);
405 /* Make sure the active GHOST context matches the one this GPU Context was created for. */
407
408 is_active_ = true;
409 thread_ = pthread_self();
410
411 /* Re-apply ghost window/context for resizing */
412 if (ghost_window_) {
413 this->set_ghost_window(ghost_window_);
414 }
415 else if (ghost_context_) {
416 this->set_ghost_context(ghost_context_);
417 }
418
419 /* Reset UBO bind state. */
420 for (auto &ssbo_bind : this->pipeline_state.ubo_bindings) {
421 if (ssbo_bind.bound && ssbo_bind.ubo != nullptr) {
422 ssbo_bind.bound = false;
423 ssbo_bind.ubo = nullptr;
424 }
425 }
426
427 /* Reset SSBO bind state. */
428 for (auto &ssbo_bind : this->pipeline_state.ssbo_bindings) {
429 if (ssbo_bind.bound && ssbo_bind.ssbo != nullptr) {
430 ssbo_bind.bound = false;
431 ssbo_bind.ssbo = nullptr;
432 }
433 }
434
435 /* Ensure imm active. */
436 immActivate();
437}
438
440{
442 /* Flush context on deactivate. */
443 this->flush();
444 is_active_ = false;
446}
447
449{
450 this->main_command_buffer.submit(false);
451}
452
454{
455 this->main_command_buffer.submit(true);
456}
457
458void MTLContext::memory_statistics_get(int *r_total_mem, int *r_free_mem)
459{
460 /* TODO(Metal): Implement. */
461 *r_total_mem = 0;
462 *r_free_mem = 0;
463}
464
466{
467 /* We do not yet begin the pass -- We defer beginning the pass until a draw is requested. */
468 BLI_assert(framebuffer);
469 this->active_fb = framebuffer;
470}
471
473{
474 /* Bind default framebuffer from context --
475 * We defer beginning the pass until a draw is requested. */
476 this->active_fb = this->back_left;
477}
478
480{
481 BLI_assert(this);
482
483 /* Ensure the rendering frame has started. */
484 if (!this->get_inside_frame()) {
485 this->begin_frame();
486 }
487
488 /* Check whether a framebuffer is bound. */
489 if (!this->active_fb) {
490 BLI_assert(false && "No framebuffer is bound!");
492 }
493
494 /* Ensure command buffer workload submissions are optimal --
495 * Though do not split a batch mid-IMM recording. */
497 !((MTLImmediate *)(this->imm))->imm_is_recording())
498 {
499 this->flush();
500 }
501
502 /* Begin pass or perform a pass switch if the active framebuffer has been changed, or if the
503 * framebuffer state has been modified (is_dirty). */
505 this->active_fb != this->main_command_buffer.get_active_framebuffer() ||
506 this->main_command_buffer.get_active_framebuffer()->get_dirty() ||
507 this->is_visibility_dirty())
508 {
509
510 /* Validate bound framebuffer before beginning render pass. */
511 if (!static_cast<MTLFrameBuffer *>(this->active_fb)->validate_render_pass()) {
512 MTL_LOG_WARNING("Framebuffer validation failed, falling back to default framebuffer");
513 this->framebuffer_restore();
514
515 if (!static_cast<MTLFrameBuffer *>(this->active_fb)->validate_render_pass()) {
516 MTL_LOG_ERROR("CRITICAL: DEFAULT FRAMEBUFFER FAIL VALIDATION!!");
517 }
518 }
519
520 /* Begin RenderCommandEncoder on main CommandBuffer. */
521 bool new_render_pass = false;
524 static_cast<MTLFrameBuffer *>(this->active_fb), true, &new_render_pass);
525 if (new_render_pass) {
526 /* Flag context pipeline state as dirty - dynamic pipeline state need re-applying. */
528 }
529 return new_enc;
530 }
533}
534
536{
537 MTLFrameBuffer *last_bound = static_cast<MTLFrameBuffer *>(this->active_fb);
538 return last_bound ? last_bound : this->get_default_framebuffer();
539}
540
542{
543 return static_cast<MTLFrameBuffer *>(this->back_left);
544}
545
550
552{
553 if (null_buffer_ != nil) {
554 return null_buffer_;
555 }
556
557 /* TODO(mpw_apple_gpusw): Null buffer size temporarily increased to cover
558 * maximum possible UBO size. There are a number of cases which need to be
559 * resolved in the high level where an expected UBO does not have a bound
560 * buffer. The null buffer needs to at least cover the size of these
561 * UBOs to avoid any GPU memory issues. */
562 static const int null_buffer_size = 20480;
563 null_buffer_ = [this->device newBufferWithLength:null_buffer_size
564 options:MTLResourceStorageModeManaged];
565 [null_buffer_ retain];
566 uint32_t *null_data = (uint32_t *)calloc(1, null_buffer_size);
567 memcpy([null_buffer_ contents], null_data, null_buffer_size);
568 [null_buffer_ didModifyRange:NSMakeRange(0, null_buffer_size)];
569 free(null_data);
570
571 BLI_assert(null_buffer_ != nil);
572 return null_buffer_;
573}
574
576{
577 if (null_attribute_buffer_ != nil) {
578 return null_attribute_buffer_;
579 }
580
581 /* Allocate Null buffer if it has not yet been created.
582 * Min buffer size is 256 bytes -- though we only need 64 bytes of data. */
583 static const int null_buffer_size = 256;
584 null_attribute_buffer_ = [this->device newBufferWithLength:null_buffer_size
585 options:MTLResourceStorageModeManaged];
586 BLI_assert(null_attribute_buffer_ != nil);
587 [null_attribute_buffer_ retain];
588 float data[4] = {0.0f, 0.0f, 0.0f, 1.0f};
589 memcpy([null_attribute_buffer_ contents], data, sizeof(float) * 4);
590 [null_attribute_buffer_ didModifyRange:NSMakeRange(0, null_buffer_size)];
591
592 return null_attribute_buffer_;
593}
594
596 GPUSamplerFormat sampler_format)
597{
598 /* Decrement 1 from texture type as they start from 1 and go to 32 (inclusive). Remap to 0..31 */
599 gpu::MTLTexture *dummy_tex = dummy_textures_[sampler_format][type - 1];
600 if (dummy_tex != nullptr) {
601 return dummy_tex;
602 }
603 /* Determine format for dummy texture. */
604 TextureFormat format = TextureFormat::UNORM_8_8_8_8;
605 switch (sampler_format) {
607 format = TextureFormat::UNORM_8_8_8_8;
608 break;
610 format = TextureFormat::SINT_8_8_8_8;
611 break;
613 format = TextureFormat::UINT_8_8_8_8;
614 break;
616 format = TextureFormat::SFLOAT_32_DEPTH_UINT_8;
617 break;
618 default:
620 }
621
622 /* Create dummy texture based on desired type. */
623 gpu::Texture *tex = nullptr;
625 switch (type) {
626 case GPU_TEXTURE_1D:
627 tex = GPU_texture_create_1d("Dummy 1D", 128, 1, format, usage, nullptr);
628 break;
630 tex = GPU_texture_create_1d_array("Dummy 1DArray", 128, 1, 1, format, usage, nullptr);
631 break;
632 case GPU_TEXTURE_2D:
633 tex = GPU_texture_create_2d("Dummy 2D", 128, 128, 1, format, usage, nullptr);
634 break;
636 tex = GPU_texture_create_2d_array("Dummy 2DArray", 128, 128, 1, 1, format, usage, nullptr);
637 break;
638 case GPU_TEXTURE_3D:
639 tex = GPU_texture_create_3d("Dummy 3D", 128, 128, 1, 1, format, usage, nullptr);
640 break;
641 case GPU_TEXTURE_CUBE:
642 tex = GPU_texture_create_cube("Dummy Cube", 128, 1, format, usage, nullptr);
643 break;
645 tex = GPU_texture_create_cube_array("Dummy CubeArray", 128, 1, 1, format, usage, nullptr);
646 break;
648 if (!dummy_verts_[sampler_format]) {
649 GPU_vertformat_clear(&dummy_vertformat_[sampler_format]);
650
651 VertAttrType attr_type = VertAttrType::SFLOAT_32_32_32_32;
652
653 switch (sampler_format) {
656 attr_type = VertAttrType::SFLOAT_32_32_32_32;
657 break;
659 attr_type = VertAttrType::SINT_32_32_32_32;
660 break;
662 attr_type = VertAttrType::UINT_32_32_32_32;
663 break;
664 default:
666 }
667
668 GPU_vertformat_attr_add(&dummy_vertformat_[sampler_format], "dummy", attr_type);
669 dummy_verts_[sampler_format] = GPU_vertbuf_create_with_format_ex(
670 dummy_vertformat_[sampler_format],
672 GPU_vertbuf_data_alloc(*dummy_verts_[sampler_format], 64);
673 }
674 tex = GPU_texture_create_from_vertbuf("Dummy TextureBuffer", dummy_verts_[sampler_format]);
675 break;
676 default:
677 BLI_assert_msg(false, "Unrecognised texture type");
678 return nullptr;
679 }
680 gpu::MTLTexture *metal_tex = static_cast<gpu::MTLTexture *>(reinterpret_cast<Texture *>(tex));
681 dummy_textures_[sampler_format][type - 1] = metal_tex;
682 return metal_tex;
683}
684
686{
687 for (int format = 0; format < GPU_SAMPLER_TYPE_MAX; format++) {
688 for (int tex = 0; tex < GPU_TEXTURE_BUFFER; tex++) {
689 if (dummy_textures_[format][tex]) {
690 GPU_texture_free(reinterpret_cast<gpu::Texture *>(
691 static_cast<Texture *>(dummy_textures_[format][tex])));
692 dummy_textures_[format][tex] = nullptr;
693 }
694 }
695 if (dummy_verts_[format]) {
696 GPU_vertbuf_discard(dummy_verts_[format]);
697 }
698 }
699}
700
707
709
710/* -------------------------------------------------------------------- */
713
714/* Needs to run before buffer and push constant binding. */
716 MTLTexture *gpu_tex,
717 MTLShaderInterface &shader_interface,
718 MTLShader &active_shader)
719{
721
722 std::string buf_name = texture_name + std::string("_buf_");
723 std::string metadata_name = texture_name + std::string("_metadata_");
724
725 int buf_slot = shader_interface.ssbo_get(buf_name)->binding;
726 gpu_tex->get_storagebuf()->bind(buf_slot);
727
728 int pc_loc = shader_interface.uniform_get(metadata_name)->location;
729 active_shader.uniform_int(pc_loc, 4, 1, gpu_tex->get_texture_metadata_ptr());
730}
731
732template<typename CommandEncoderT>
734 CommandEncoderT enc,
735 MTLSamplerArray &sampler_array,
736 Map<MTLSamplerArray, gpu::MTLBuffer *> &sampler_buffers_cache,
737 MTLShaderInterface &shader_interface,
738 id<MTLFunction> mtl_function,
740{
741 const int arg_buffer_idx = MTL_SAMPLER_ARGUMENT_BUFFER_SLOT;
742 BLI_assert(arg_buffer_idx < 32);
743
744 const uint64_t tex_slot_mask = shader_interface.enabled_tex_mask_;
745 const uint max_sampler_index = 63 - bitscan_reverse_uint64(tex_slot_mask);
746
747 /* Generate or Fetch argument buffer sampler configuration.
748 * NOTE(Metal): we need to base sampler counts off of the maximal texture
749 * index. This is not the most optimal, but in practice, not a use-case
750 * when argument buffers are required.
751 * This is because with explicit texture indices, the binding indices
752 * should match across draws, to allow the high-level to optimize bind-points. */
753 sampler_array.num_samplers = max_sampler_index + 1;
754
755 gpu::MTLBuffer *encoder_buf = sampler_buffers_cache.lookup_or_add_cb(sampler_array, [&]() {
756 id<MTLArgumentEncoder> argument_encoder = shader_interface.ensure_argument_encoder(
757 mtl_function);
758
759 /* Populate argument buffer with current global sampler bindings. */
760 size_t size = [argument_encoder encodedLength];
761 size_t alignment = max_uu([argument_encoder alignment], 256);
762 size_t size_align_delta = (size % alignment);
763 size_t aligned_alloc_size = ((alignment > 1) && (size_align_delta > 0)) ?
764 size + (alignment - (size % alignment)) :
765 size;
766
767 /* Allocate buffer to store encoded sampler arguments. */
768 encoder_buf = MTLContext::get_global_memory_manager()->allocate(aligned_alloc_size, true);
769
770 BLI_assert(encoder_buf);
771 BLI_assert(encoder_buf->get_metal_buffer());
772 [argument_encoder setArgumentBuffer:encoder_buf->get_metal_buffer() offset:0];
773 [argument_encoder setSamplerStates:sampler_array.mtl_sampler
774 withRange:NSMakeRange(0, max_sampler_index + 1)];
775 encoder_buf->flush();
776
777 return encoder_buf;
778 });
779
780 bindings.bind_buffer(enc, encoder_buf->get_metal_buffer(), 0, arg_buffer_idx);
781}
782
783/* Ensure texture bindings are correct and up to date for current draw call.
784 * We will iterate through all texture bindings on the context and determine if any of the
785 * active slots match those in our shader interface. If so, textures will be bound. */
786template<typename CommandEncoderT>
789 CommandEncoderT enc,
791 id<MTLFunction> mtl_function,
792 uint16_t stage_ima_mask = uint16_t(-1),
793 uint64_t stage_tex_mask = uint64_t(-1))
794{
795 MTLShaderInterface &shader_interface = shader.get_interface();
796
797 if (shader_interface.enabled_ima_mask_ == 0 && shader_interface.enabled_tex_mask_ == 0) {
798 return;
799 }
800
801 /* TODO(fclem): Dirty binding tracking optimization. */
802 uint16_t dirty_image_mask = ~uint16_t(0u);
803 uint64_t dirty_sampler_mask = ~uint64_t(0u);
804
805 uint16_t dirty_enabled_image_mask = shader_interface.enabled_ima_mask_ & dirty_image_mask;
806 uint64_t dirty_enabled_sampler_mask = shader_interface.enabled_tex_mask_ & dirty_sampler_mask;
807
808 uint16_t bind_image = dirty_enabled_image_mask & stage_ima_mask;
809 for (const uint slot : bits::iter_1_indices(bind_image)) {
810 MTLTexture *gpu_tex = ctx.pipeline_state.image_bindings[slot].texture_resource;
811
812 if (gpu_tex == nullptr) {
813 /* TODO(fclem): texture_get can also return uniforms or images. */
814 const int name_ofs = shader_interface.texture_get(slot)->name_offset;
815 MTL_LOG_ERROR("Shader %s: Missing image bind: %s slot(%d).",
816 shader.name_get().c_str(),
817 shader_interface.name_at_offset(name_ofs),
818 slot);
819 continue;
820 }
821 /* If texture resource is an image binding and has a non-default swizzle mask, we need
822 * to bind the source texture resource to retain image write access. */
823 id<MTLTexture> tex = gpu_tex->has_custom_swizzle() ? gpu_tex->get_metal_handle_base() :
824 gpu_tex->get_metal_handle();
825 bindings.bind_texture(enc, tex, MTL_IMAGE_SLOT_OFFSET + slot);
826 if (shader_interface.use_texture_atomic() && (gpu_tex->usage_get() & GPU_TEXTURE_USAGE_ATOMIC))
827 {
828 if (MTLBackend::get_capabilities().supports_texture_atomics == false) {
829 bind_atomic_workaround_buffer(shader_interface.image_name_get(slot),
830 gpu_tex,
831 shader_interface,
833 }
834 }
835 }
836
837 uint64_t bind_sampler = dirty_enabled_sampler_mask & stage_tex_mask;
838 for (const uint slot : bits::iter_1_indices(bind_sampler)) {
839 MTLTexture *gpu_tex = ctx.pipeline_state.texture_bindings[slot].texture_resource;
840 MTLSamplerBinding &sampler_state = ctx.pipeline_state.sampler_bindings[slot];
841
842 if (gpu_tex == nullptr) {
843 /* TODO(fclem): texture_get can also return uniforms or images. */
844 const int name_ofs = shader_interface.texture_get(slot)->name_offset;
845 MTL_LOG_ERROR("Shader %s: Missing texture bind: %s slot(%d).",
846 shader.name_get().c_str(),
847 shader_interface.name_at_offset(name_ofs),
848 slot);
849 continue;
850 }
851
852 id<MTLSamplerState> mtl_sampler = (sampler_state.state == DEFAULT_SAMPLER_STATE) ?
854 ctx.get_sampler_from_state(sampler_state.state);
855
856 bindings.bind_texture(enc, gpu_tex->get_metal_handle(), MTL_SAMPLER_SLOT_OFFSET + slot);
857 bindings.bind_sampler(enc,
858 ctx.get_sampler_array(),
859 mtl_sampler,
860 sampler_state.state,
861 shader_interface.use_samplers_argument_buffer(),
862 slot);
863 if (shader_interface.use_texture_atomic() && (gpu_tex->usage_get() & GPU_TEXTURE_USAGE_ATOMIC))
864 {
865 if (MTLBackend::get_capabilities().supports_texture_atomics == false) {
866 bind_atomic_workaround_buffer(shader_interface.sampler_name_get(slot),
867 gpu_tex,
868 shader_interface,
870 }
871 }
872 }
873
874 /* Construct and Bind argument buffer.
875 * NOTE(Metal): Samplers use an argument buffer when the limit of 16 samplers is exceeded. */
876 if (shader_interface.use_samplers_argument_buffer()) {
878 ctx.get_sampler_array(),
880 shader_interface,
881 mtl_function,
882 bindings);
883 }
884}
885
886template<typename CommandEncoderT>
889 CommandEncoderT enc,
891 uint32_t stage_buf_mask = uint32_t(-1))
892{
893 if ((stage_buf_mask & (1 << MTL_PUSH_CONSTANT_BUFFER_SLOT)) == 0) {
894 /* Push constant is not used by this shader. We can skip it. */
895 return;
896 }
897 MTLPushConstantBuf *pc_buf = shader.get_push_constant_buf();
898 /* Only need to rebind block if push constants have been modified -- or if no data is bound for
899 * the current RenderCommandEncoder. */
900 bindings.bind_bytes(enc,
902 pc_buf->data(),
903 pc_buf->size(),
905}
906
907/* Bind UBOs and SSBOs to an active render command encoder using the rendering state of the
908 * current context -> Active shader, Bound UBOs.
909 * NOTE: `ensure_buffer_bindings` must be called after `ensure_texture_bindings` to allow
910 * for binding of buffer-backed texture's data buffer and metadata. */
911template<typename CommandEncoderT>
914 CommandEncoderT enc,
916 uint32_t stage_buf_mask = uint32_t(-1))
917{
918 MTLShaderInterface &shader_interface = shader.get_interface();
919
920 /* TODO(fclem): Dirty binding tracking optimization. */
921 uint32_t dirty_ubo_mask = ~uint32_t(0u);
922 uint32_t dirty_ssbo_mask = ~uint32_t(0u);
923
924 uint32_t dirty_enabled_ubo_mask = shader_interface.enabled_ubo_mask_ & dirty_ubo_mask;
925 uint32_t dirty_enabled_ssbo_mask = shader_interface.enabled_ssbo_mask_ & dirty_ssbo_mask;
926
927 uint32_t bind_ubo = dirty_enabled_ubo_mask & (stage_buf_mask >> MTL_UBO_SLOT_OFFSET);
928 for (const uint slot : bits::iter_1_indices(bind_ubo)) {
930 if (bind.ubo) {
931 bindings.bind_buffer(enc, bind.ubo->get_metal_buffer(), 0, MTL_UBO_SLOT_OFFSET + slot);
932 }
933 else {
934 const int name_ofs = shader_interface.ubo_get(slot)->name_offset;
935 MTL_LOG_ERROR("Shader %s: Missing UBO bind: %s slot(%d).",
936 shader.name_get().c_str(),
937 shader_interface.name_at_offset(name_ofs),
938 slot);
939 }
940 }
941
942 uint32_t bind_ssbo = dirty_enabled_ssbo_mask & (stage_buf_mask >> MTL_SSBO_SLOT_OFFSET);
943 for (const uint slot : bits::iter_1_indices(bind_ssbo)) {
945 if (bind.ssbo) {
946 bindings.bind_buffer(enc, bind.ssbo->get_metal_buffer(), 0, MTL_SSBO_SLOT_OFFSET + slot);
947 }
948 else {
949 const int name_ofs = shader_interface.ssbo_get(slot)->name_offset;
950 MTL_LOG_ERROR("Shader %s: Missing SSBO bind: %s slot(%d).",
951 shader.name_get().c_str(),
952 shader_interface.name_at_offset(name_ofs),
953 slot);
954 }
955 }
956
957 /* Bind acceleration structures for any shader stage that declares one. */
958 for (const uint slot : bits::iter_1_indices(shader_interface.enabled_accel_mask())) {
961 if (bind.tlas) {
962 enc.set_acceleration_structure(bind.tlas->acceleration_structure(),
964 bind.tlas->make_resident(enc);
965 }
966 else {
967 MTL_LOG_ERROR("Shader %s: Missing acceleration structure bind at slot(%d).",
968 shader.name_get().c_str(),
969 slot);
970 }
971 }
972}
973
975{
977
978 /* Clear blending State. */
979 this->pipeline_state.color_write_mask = MTLColorWriteMaskRed | MTLColorWriteMaskGreen |
980 MTLColorWriteMaskBlue | MTLColorWriteMaskAlpha;
981 this->pipeline_state.blending_enabled = false;
982 this->pipeline_state.alpha_blend_op = MTLBlendOperationAdd;
983 this->pipeline_state.rgb_blend_op = MTLBlendOperationAdd;
984 this->pipeline_state.dest_alpha_blend_factor = MTLBlendFactorZero;
985 this->pipeline_state.dest_rgb_blend_factor = MTLBlendFactorZero;
986 this->pipeline_state.src_alpha_blend_factor = MTLBlendFactorOne;
987 this->pipeline_state.src_rgb_blend_factor = MTLBlendFactorOne;
988
989 /* Viewport and scissor. */
990 for (int v = 0; v < GPU_MAX_VIEWPORTS; v++) {
995 }
996 this->pipeline_state.scissor_x = 0;
997 this->pipeline_state.scissor_y = 0;
1000 this->pipeline_state.scissor_enabled = false;
1001
1002 /* Culling State. */
1003 this->pipeline_state.culling_enabled = false;
1006
1007 /* Depth State. */
1010 this->pipeline_state.depth_stencil_state.depth_function = MTLCompareFunctionAlways;
1011
1012 /* Stencil State. */
1017 this->pipeline_state.depth_stencil_state.stencil_func = MTLCompareFunctionAlways;
1019 this->pipeline_state.depth_stencil_state.stencil_op_front_depth_fail = MTLStencilOperationKeep;
1021 MTLStencilOperationKeep;
1022 this->pipeline_state.depth_stencil_state.stencil_op_back_stencil_fail = MTLStencilOperationKeep;
1023 this->pipeline_state.depth_stencil_state.stencil_op_back_depth_fail = MTLStencilOperationKeep;
1025 MTLStencilOperationKeep;
1026}
1027
1028void MTLContext::set_viewport(int origin_x, int origin_y, int width, int height)
1029{
1030 BLI_assert(this);
1031 BLI_assert(width > 0);
1032 BLI_assert(height > 0);
1033 BLI_assert(origin_x >= 0);
1034 BLI_assert(origin_y >= 0);
1035 bool changed = (this->pipeline_state.viewport_offset_x[0] != origin_x) ||
1036 (this->pipeline_state.viewport_offset_y[0] != origin_y) ||
1037 (this->pipeline_state.viewport_width[0] != width) ||
1038 (this->pipeline_state.viewport_height[0] != height) ||
1040 this->pipeline_state.viewport_offset_x[0] = origin_x;
1041 this->pipeline_state.viewport_offset_y[0] = origin_y;
1042 this->pipeline_state.viewport_width[0] = width;
1043 this->pipeline_state.viewport_height[0] = height;
1045
1046 if (changed) {
1049 }
1050}
1051
1052void MTLContext::set_viewports(int count, const int (&viewports)[GPU_MAX_VIEWPORTS][4])
1053{
1054 BLI_assert(this);
1055 bool changed = (this->pipeline_state.num_active_viewports != count);
1056 for (int v = 0; v < count; v++) {
1057 const int (&viewport_info)[4] = viewports[v];
1058
1059 BLI_assert(viewport_info[0] >= 0);
1060 BLI_assert(viewport_info[1] >= 0);
1061 BLI_assert(viewport_info[2] > 0);
1062 BLI_assert(viewport_info[3] > 0);
1063
1064 changed = changed || (this->pipeline_state.viewport_offset_x[v] != viewport_info[0]) ||
1065 (this->pipeline_state.viewport_offset_y[v] != viewport_info[1]) ||
1066 (this->pipeline_state.viewport_width[v] != viewport_info[2]) ||
1067 (this->pipeline_state.viewport_height[v] != viewport_info[3]);
1068 this->pipeline_state.viewport_offset_x[v] = viewport_info[0];
1069 this->pipeline_state.viewport_offset_y[v] = viewport_info[1];
1070 this->pipeline_state.viewport_width[v] = viewport_info[2];
1071 this->pipeline_state.viewport_height[v] = viewport_info[3];
1072 }
1074
1075 if (changed) {
1078 }
1079}
1080
1081void MTLContext::set_scissor(int scissor_x, int scissor_y, int scissor_width, int scissor_height)
1082{
1083 BLI_assert(this);
1084 bool changed = (this->pipeline_state.scissor_x != scissor_x) ||
1085 (this->pipeline_state.scissor_y != scissor_y) ||
1086 (this->pipeline_state.scissor_width != scissor_width) ||
1087 (this->pipeline_state.scissor_height != scissor_height) ||
1088 (this->pipeline_state.scissor_enabled != true);
1089 this->pipeline_state.scissor_x = scissor_x;
1090 this->pipeline_state.scissor_y = scissor_y;
1091 this->pipeline_state.scissor_width = scissor_width;
1092 this->pipeline_state.scissor_height = scissor_height;
1093 this->pipeline_state.scissor_enabled = (scissor_width > 0 && scissor_height > 0);
1094
1095 if (changed) {
1098 }
1099}
1100
1101void MTLContext::set_scissor_enabled(bool scissor_enabled)
1102{
1103 /* Only turn on Scissor if requested scissor region is valid */
1104 scissor_enabled = scissor_enabled && (this->pipeline_state.scissor_width > 0 &&
1105 this->pipeline_state.scissor_height > 0);
1106
1107 bool changed = (this->pipeline_state.scissor_enabled != scissor_enabled);
1108 this->pipeline_state.scissor_enabled = scissor_enabled;
1109 if (changed) {
1112 }
1113}
1114
1116
1117/* -------------------------------------------------------------------- */
1127
1128bool MTLContext::ensure_render_pipeline_state(MTLPrimitiveType mtl_prim_type)
1129{
1131 /* Check if an active shader is bound. */
1132 if (!shader) {
1133 MTL_LOG_WARNING("No Metal shader for bound GL shader");
1134 return false;
1135 }
1136
1137 /* Also ensure active shader is valid. */
1138 if (!shader->is_valid()) {
1140 "Bound active shader is not valid (Missing/invalid implementation for Metal).", );
1141 return false;
1142 }
1143
1144 /* Apply global state. */
1145 this->state_manager->apply_state();
1146
1147 /* Main command buffer tracks the current state of the render pass, based on bound
1148 * MTLFrameBuffer. */
1150
1151 /* Debug Check: Ensure Framebuffer instance is not dirty. */
1153
1154 MTLShaderInterface &shader_interface = shader->get_interface();
1155
1156 /* Fetch shader and bake valid PipelineStateObject (PSO) based on current
1157 * shader and state combination. This PSO represents the final GPU-executable
1158 * permutation of the shader. */
1160 this, mtl_prim_type_to_topology_class(mtl_prim_type));
1161
1162 if (!psi) {
1163 MTL_LOG_ERROR("Failed to bake Metal pipeline state for shader: %s",
1164 shader_interface.name_get());
1165 return false;
1166 }
1167
1168 if (!psi->pso) {
1169 MTL_LOG_ERROR("PSO for shader %s is null.", shader_interface.name_get());
1170 return false;
1171 }
1172
1173 /* Fetch render command encoder. A render pass should already be active.
1174 * This will be NULL if invalid. */
1176 BLI_assert(rec);
1177 if (rec == nil) {
1178 MTL_LOG_ERROR("ensure_render_pipeline_state called while render pass is not active.");
1179 return false;
1180 }
1181
1182 /* Support legacy point size state. */
1183 MTLStateManager &state_manager = *static_cast<MTLStateManager *>(this->state_manager);
1184 if (mtl_prim_type == MTLPrimitiveTypePoint && state_manager.mutable_state.point_size < 0) {
1186 }
1187
1188 /* Bind Render Pipeline State. */
1189 BLI_assert(psi->pso);
1190 if (rps.bound_pso != psi->pso) {
1191 [rec setRenderPipelineState:psi->pso];
1192 rps.bound_pso = psi->pso;
1193 }
1194
1195 bool active_shader_changed = assign_if_different(
1196 rps.last_bound_shader_state, MTLBoundShaderState{shader, psi->shader_pso_index});
1197
1199 if (active_shader_changed && pc_buf) {
1200 pc_buf->tag_dirty();
1201 }
1202
1203 if (G.debug & G_DEBUG_GPU) {
1204 [rec pushDebugGroup:@"ApplyState"];
1205 }
1206
1208 MTLVertexCommandEncoder vert_rec{rec};
1209 MTLFragmentCommandEncoder frag_rec{rec};
1211 *shader,
1212 vert_rec,
1213 rps.vertex_bindings,
1214 psi->vert,
1215 psi->used_ima_vert_mask,
1216 psi->used_tex_vert_mask);
1218 *shader,
1219 frag_rec,
1221 psi->frag,
1222 psi->used_ima_frag_mask,
1223 psi->used_tex_frag_mask);
1226 if (pc_buf && pc_buf->is_dirty()) {
1227 ensure_push_constant(*this, *shader, vert_rec, rps.vertex_bindings, psi->used_buf_vert_mask);
1229 pc_buf->tag_updated();
1230 }
1231
1232 /* Bind Null attribute buffer, if needed. */
1233 if (psi->null_attribute_buffer_index >= 0) {
1234 if (G.debug & G_DEBUG_GPU) {
1235 MTL_LOG_DEBUG("Binding null attribute buffer at index: %d",
1237 }
1239 }
1240
1242 /* State: Viewport. */
1243 if (this->pipeline_state.num_active_viewports > 1) {
1244 /* Multiple Viewports. */
1245 MTLViewport viewports[GPU_MAX_VIEWPORTS];
1246 for (int v = 0; v < this->pipeline_state.num_active_viewports; v++) {
1247 MTLViewport &viewport = viewports[v];
1248 viewport.originX = (double)this->pipeline_state.viewport_offset_x[v];
1249 viewport.originY = (double)this->pipeline_state.viewport_offset_y[v];
1250 viewport.width = (double)this->pipeline_state.viewport_width[v];
1251 viewport.height = (double)this->pipeline_state.viewport_height[v];
1252 viewport.znear = 0.0f;
1253 viewport.zfar = 1.0f;
1254 }
1255 [rec setViewports:viewports count:this->pipeline_state.num_active_viewports];
1256 }
1257 else {
1258 /* Single Viewport. */
1259 MTLViewport viewport;
1260 viewport.originX = (double)this->pipeline_state.viewport_offset_x[0];
1261 viewport.originY = (double)this->pipeline_state.viewport_offset_y[0];
1262 viewport.width = (double)this->pipeline_state.viewport_width[0];
1263 viewport.height = (double)this->pipeline_state.viewport_height[0];
1264 viewport.znear = 0.0f;
1265 viewport.zfar = 1.0f;
1266 [rec setViewport:viewport];
1267 }
1268
1269 /* State: Scissor. */
1271
1272 /* Get FrameBuffer associated with active RenderCommandEncoder. */
1274
1275 MTLScissorRect scissor;
1276 if (this->pipeline_state.scissor_enabled) {
1277 scissor.x = this->pipeline_state.scissor_x;
1278 scissor.y = this->pipeline_state.scissor_y;
1279 scissor.width = this->pipeline_state.scissor_width;
1280 scissor.height = this->pipeline_state.scissor_height;
1281
1282 /* Some scissor assignments exceed the bounds of the viewport due to implicitly added
1283 * padding to the width/height - Clamp width/height. */
1284 BLI_assert(scissor.x >= 0 && scissor.x < render_fb->get_attachment_width());
1285 BLI_assert(scissor.y >= 0 && scissor.y < render_fb->get_attachment_height());
1286 scissor.width = (uint)min_ii(
1287 scissor.width, max_ii(render_fb->get_attachment_width() - (int)(scissor.x), 0));
1288 scissor.height = (uint)min_ii(
1289 scissor.height, max_ii(render_fb->get_attachment_height() - (int)(scissor.y), 0));
1290 BLI_assert(scissor.width > 0 &&
1291 (scissor.x + scissor.width <= render_fb->get_attachment_width()));
1292 BLI_assert(scissor.height > 0 && (scissor.height <= render_fb->get_attachment_height()));
1293 }
1294 else {
1295 /* Scissor is disabled, reset to attachment size as scissor state may have been previously
1296 * assigned on this encoder.
1297 * NOTE: If an attachment-less framebuffer is used, fetch specified width/height rather
1298 * than active attachment width/height as provided by get_attachment_w/h(). */
1299 uint default_w = render_fb->get_attachment_width();
1300 uint default_h = render_fb->get_attachment_height();
1301 bool is_attachmentless = (default_w == 0) && (default_h == 0);
1302 scissor.x = 0;
1303 scissor.y = 0;
1304 scissor.width = (is_attachmentless) ? render_fb->get_width() : default_w;
1305 scissor.height = (is_attachmentless) ? render_fb->get_height() : default_h;
1306 }
1307
1308 /* Scissor state can still be flagged as changed if it is toggled on and off, without
1309 * parameters changing between draws. */
1310 if (memcmp(&scissor, &rps.last_scissor_rect, sizeof(MTLScissorRect)) != 0) {
1311 [rec setScissorRect:scissor];
1312 rps.last_scissor_rect = scissor;
1313 }
1316 }
1317
1318 /* State: Face winding. */
1320 /* We need to invert the face winding in Metal, to account for the inverted-Y coordinate
1321 * system. */
1322 MTLWinding winding = (this->pipeline_state.front_face == GPU_CLOCKWISE) ?
1323 MTLWindingClockwise :
1324 MTLWindingCounterClockwise;
1325 [rec setFrontFacingWinding:winding];
1328 }
1329
1330 /* State: cull-mode. */
1332
1333 MTLCullMode mode = MTLCullModeNone;
1334 if (this->pipeline_state.culling_enabled) {
1335 switch (this->pipeline_state.cull_mode) {
1336 case GPU_CULL_NONE:
1337 mode = MTLCullModeNone;
1338 break;
1339 case GPU_CULL_FRONT:
1340 mode = MTLCullModeFront;
1341 break;
1342 case GPU_CULL_BACK:
1343 mode = MTLCullModeBack;
1344 break;
1345 default:
1347 break;
1348 }
1349 }
1350 [rec setCullMode:mode];
1353 }
1354
1355 if (G.debug & G_DEBUG_GPU) {
1356 [rec popDebugGroup];
1357 }
1358
1359 /* Pipeline state is now good. */
1360 return true;
1361}
1362
1363/* Encode latest depth-stencil state. */
1365{
1366 /* Check if we need to update state. */
1368 return;
1369 }
1370
1371 /* Fetch render command encoder. */
1373 BLI_assert(rec);
1374
1375 /* Fetch Render Pass state. */
1377
1380 bool hasDepthTarget = fb->has_depth_attachment();
1381 bool hasStencilTarget = fb->has_stencil_attachment();
1382
1383 if (hasDepthTarget || hasStencilTarget) {
1384 /* Update FrameBuffer State. */
1386 this->pipeline_state.depth_stencil_state.has_stencil_target = hasStencilTarget;
1387
1388 /* Check if current MTLContextDepthStencilState maps to an existing state object in
1389 * the Depth-stencil state cache. */
1390 id<MTLDepthStencilState> ds_state = nil;
1391 id<MTLDepthStencilState> *depth_stencil_state_lookup =
1392 this->depth_stencil_state_cache.lookup_ptr(this->pipeline_state.depth_stencil_state);
1393
1394 /* If not, populate DepthStencil state descriptor. */
1395 if (depth_stencil_state_lookup == nullptr) {
1396
1397 MTLDepthStencilDescriptor *ds_state_desc = [[[MTLDepthStencilDescriptor alloc] init]
1398 autorelease];
1399
1400 if (hasDepthTarget) {
1401 ds_state_desc.depthWriteEnabled =
1403 ds_state_desc.depthCompareFunction =
1406 MTLCompareFunctionAlways;
1407 }
1408
1409 if (hasStencilTarget) {
1410 ds_state_desc.backFaceStencil.readMask =
1412 ds_state_desc.backFaceStencil.writeMask =
1414 ds_state_desc.backFaceStencil.stencilFailureOperation =
1416 ds_state_desc.backFaceStencil.depthFailureOperation =
1418 ds_state_desc.backFaceStencil.depthStencilPassOperation =
1420 ds_state_desc.backFaceStencil.stencilCompareFunction =
1423 MTLCompareFunctionAlways;
1424
1425 ds_state_desc.frontFaceStencil.readMask =
1427 ds_state_desc.frontFaceStencil.writeMask =
1429 ds_state_desc.frontFaceStencil.stencilFailureOperation =
1431 ds_state_desc.frontFaceStencil.depthFailureOperation =
1433 ds_state_desc.frontFaceStencil.depthStencilPassOperation =
1435 ds_state_desc.frontFaceStencil.stencilCompareFunction =
1438 MTLCompareFunctionAlways;
1439 }
1440
1441 /* Bake new DS state. */
1442 ds_state = [this->device newDepthStencilStateWithDescriptor:ds_state_desc];
1443
1444 /* Store state in cache. */
1445 BLI_assert(ds_state != nil);
1446 this->depth_stencil_state_cache.add_new(this->pipeline_state.depth_stencil_state, ds_state);
1447 }
1448 else {
1449 ds_state = *depth_stencil_state_lookup;
1450 BLI_assert(ds_state != nil);
1451 }
1452
1453 /* Bind Depth Stencil State to render command encoder. */
1454 BLI_assert(ds_state != nil);
1455 if (ds_state != nil) {
1456 if (rps.bound_ds_state != ds_state) {
1457 [rec setDepthStencilState:ds_state];
1458 rps.bound_ds_state = ds_state;
1459 }
1460 }
1461
1462 /* Apply dynamic depth-stencil state on encoder. */
1463 if (hasStencilTarget) {
1464 uint32_t stencil_ref_value =
1467 0;
1468 if (stencil_ref_value != rps.last_used_stencil_ref_value) {
1469 [rec setStencilReferenceValue:stencil_ref_value];
1470 rps.last_used_stencil_ref_value = stencil_ref_value;
1471 }
1472 }
1473
1474 if (hasDepthTarget) {
1475 [rec setDepthBias:0 slopeScale:0 clamp:0];
1476 }
1477 }
1478}
1479
1481
1482/* -------------------------------------------------------------------- */
1485
1487{
1488 /* Verify if bound shader is valid and fetch MTLComputePipelineStateInstance. */
1489 /* Check if an active shader is bound. */
1490 if (!this->pipeline_state.active_shader) {
1491 MTL_LOG_WARNING("No Metal shader bound!");
1492 return nullptr;
1493 }
1494 /* Also ensure active shader is valid. */
1495 if (!this->pipeline_state.active_shader->is_valid()) {
1497 "Bound active shader is not valid (Missing/invalid implementation for Metal).", );
1498 return nullptr;
1499 }
1500 /* Verify this is a compute shader. */
1501 MTLShader *active_shader = this->pipeline_state.active_shader;
1502
1503 /* Set descriptor to default shader constants . */
1504 MTLComputePipelineStateDescriptor compute_pipeline_descriptor(this->constants_state.values);
1505
1506 const MTLComputePipelineStateInstance *compute_pso_inst =
1507 active_shader->bake_compute_pipeline_state(this, compute_pipeline_descriptor);
1508
1509 if (compute_pso_inst == nullptr || compute_pso_inst->pso == nil) {
1510 MTL_LOG_WARNING("No valid compute PSO for compute dispatch!", );
1511 return nullptr;
1512 }
1513 return compute_pso_inst;
1514}
1515
1516void MTLContext::compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len)
1517{
1518 /* Ensure all resources required by upcoming compute submission are correctly bound to avoid
1519 * out of bounds reads/writes. */
1520 const MTLComputePipelineStateInstance *pipe_state_inst = this->ensure_compute_pipeline_state();
1521 if (pipe_state_inst == nullptr) {
1522 return;
1523 }
1524
1525#if MTL_DEBUG_SINGLE_DISPATCH_PER_ENCODER == 1
1526 GPU_flush();
1527#endif
1528
1530
1531 /* Begin compute encoder. */
1532 id<MTLComputeCommandEncoder> compute_encoder =
1534 BLI_assert(compute_encoder != nil);
1535
1537 if (pc_buf) {
1538 /* Always tag dirty since we have a new encoder for each dispatch. */
1539 pc_buf->tag_dirty();
1540 }
1541
1542 if (G.debug & G_DEBUG_GPU) {
1544 [compute_encoder pushDebugGroup:[NSString stringWithFormat:@"Dispatch(Shader:%s)",
1545 shader->name_get().c_str()]];
1546 }
1547
1548 /* Bind PSO. */
1550 cs.bind_pso(pipe_state_inst->pso);
1551
1552 if (G.debug & G_DEBUG_GPU) {
1553 [compute_encoder pushDebugGroup:@"ApplyState"];
1554 }
1555
1557 MTLComputeCommandEncoder comp_rec{compute_encoder};
1558 ensure_texture_bindings(*this, *shader, comp_rec, cs.compute_bindings, pipe_state_inst->compute);
1559 ensure_buffer_bindings(*this, *shader, comp_rec, cs.compute_bindings);
1560 if (pc_buf && pc_buf->is_dirty()) {
1561 ensure_push_constant(*this, *shader, comp_rec, cs.compute_bindings);
1562 pc_buf->tag_updated();
1563 }
1564
1565 if (G.debug & G_DEBUG_GPU) {
1566 [compute_encoder popDebugGroup];
1567 }
1568
1569 /* Dispatch compute. */
1570 const MTLComputePipelineStateCommon &compute_state_common =
1572 [compute_encoder dispatchThreadgroups:MTLSizeMake(max_ii(groups_x_len, 1),
1573 max_ii(groups_y_len, 1),
1574 max_ii(groups_z_len, 1))
1575 threadsPerThreadgroup:MTLSizeMake(compute_state_common.threadgroup_x_len,
1576 compute_state_common.threadgroup_y_len,
1577 compute_state_common.threadgroup_z_len)];
1578
1579 if (G.debug & G_DEBUG_GPU) {
1580 [compute_encoder popDebugGroup];
1581 }
1582
1583#if MTL_DEBUG_SINGLE_DISPATCH_PER_ENCODER == 1
1584 GPU_flush();
1585#endif
1586}
1587
1589{
1590#if MTL_DEBUG_SINGLE_DISPATCH_PER_ENCODER == 1
1591 GPU_flush();
1592#endif
1593
1595
1596 /* Ensure all resources required by upcoming compute submission are correctly bound. */
1597 const MTLComputePipelineStateInstance *pipe_state_inst = this->ensure_compute_pipeline_state();
1598 BLI_assert(pipe_state_inst != nullptr);
1599
1600 /* Begin compute encoder. */
1601 id<MTLComputeCommandEncoder> compute_encoder =
1603 BLI_assert(compute_encoder != nil);
1604
1606 if (pc_buf) {
1607 /* Always tag dirty since we have a new encoder for each dispatch. */
1608 pc_buf->tag_dirty();
1609 }
1610
1611 if (G.debug & G_DEBUG_GPU) {
1613 [compute_encoder pushDebugGroup:[NSString stringWithFormat:@"DispatchIndirect(Shader:%s)",
1614 shader->name_get().c_str()]];
1615 }
1616
1617 /* Bind PSO. */
1619 cs.bind_pso(pipe_state_inst->pso);
1620
1621 if (G.debug & G_DEBUG_GPU) {
1622 [compute_encoder pushDebugGroup:@"ApplyState"];
1623 }
1624
1626 MTLComputeCommandEncoder comp_rec{compute_encoder};
1627 ensure_texture_bindings(*this, *shader, comp_rec, cs.compute_bindings, pipe_state_inst->compute);
1628 ensure_buffer_bindings(*this, *shader, comp_rec, cs.compute_bindings);
1629 if (pc_buf && pc_buf->is_dirty()) {
1630 ensure_push_constant(*this, *shader, comp_rec, cs.compute_bindings);
1631 pc_buf->tag_updated();
1632 }
1633
1634 if (G.debug & G_DEBUG_GPU) {
1635 [compute_encoder popDebugGroup];
1636 }
1637
1638 /* Indirect Dispatch compute. */
1639 MTLStorageBuf *mtlssbo = static_cast<MTLStorageBuf *>(indirect_buf);
1640 id<MTLBuffer> mtl_indirect_buf = mtlssbo->get_metal_buffer();
1641 BLI_assert(mtl_indirect_buf != nil);
1642 if (mtl_indirect_buf == nil) {
1643 MTL_LOG_WARNING("Metal Indirect Compute dispatch storage buffer does not exist.");
1644 if (G.debug & G_DEBUG_GPU) {
1645 [compute_encoder popDebugGroup];
1646 }
1647 return;
1648 }
1649
1650 /* Indirect Compute dispatch. */
1651 const MTLComputePipelineStateCommon &compute_state_common =
1653 [compute_encoder
1654 dispatchThreadgroupsWithIndirectBuffer:mtl_indirect_buf
1655 indirectBufferOffset:0
1656 threadsPerThreadgroup:MTLSizeMake(compute_state_common.threadgroup_x_len,
1657 compute_state_common.threadgroup_y_len,
1658 compute_state_common.threadgroup_z_len)];
1659
1660 if (G.debug & G_DEBUG_GPU) {
1661 [compute_encoder popDebugGroup];
1662 }
1663#if MTL_DEBUG_SINGLE_DISPATCH_PER_ENCODER == 1
1664 GPU_flush();
1665#endif
1666}
1667
1669
1670/* -------------------------------------------------------------------- */
1673
1675{
1676 /* Flag visibility buffer as dirty if the buffer being used for visibility has changed --
1677 * This is required by the render pass, and we will break the pass if the results destination
1678 * buffer is modified. */
1679 if (buffer) {
1680 visibility_is_dirty_ = (buffer != visibility_buffer_) || visibility_is_dirty_;
1681 visibility_buffer_ = buffer;
1682 visibility_buffer_->debug_ensure_used();
1683 }
1684 else {
1685 /* If buffer is null, reset visibility state, mark dirty to break render pass if results are no
1686 * longer needed. */
1687 visibility_is_dirty_ = (visibility_buffer_ != nullptr) || visibility_is_dirty_;
1688 visibility_buffer_ = nullptr;
1689 }
1690}
1691
1693{
1694 return visibility_buffer_;
1695}
1696
1698{
1699 visibility_is_dirty_ = false;
1700}
1701
1703{
1704 return visibility_is_dirty_;
1705}
1706
1708
1709/* -------------------------------------------------------------------- */
1712
1713void MTLContext::texture_bind(gpu::MTLTexture *mtl_texture, uint texture_unit, bool is_image)
1714{
1715 BLI_assert(this);
1716 BLI_assert(mtl_texture);
1717
1718 if (texture_unit < 0 || texture_unit >= GPU_max_textures() ||
1719 texture_unit >= MTL_MAX_TEXTURE_SLOTS)
1720 {
1721 MTL_LOG_ERROR("Attempting to bind texture '%s' to invalid texture unit %d",
1722 mtl_texture->get_name(),
1723 texture_unit);
1724 BLI_assert(false);
1725 return;
1726 }
1727
1728 MTLTextureBinding &resource_bind = (is_image) ?
1729 this->pipeline_state.image_bindings[texture_unit] :
1730 this->pipeline_state.texture_bindings[texture_unit];
1731
1732 /* Bind new texture. */
1733 resource_bind.texture_resource = mtl_texture;
1734}
1735
1736void MTLContext::sampler_bind(MTLSamplerState sampler_state, uint sampler_unit)
1737{
1738 BLI_assert(this);
1739 if (sampler_unit < 0 || sampler_unit >= GPU_max_textures() ||
1740 sampler_unit >= MTL_MAX_SAMPLER_SLOTS)
1741 {
1742 MTL_LOG_ERROR("Attempting to bind sampler to invalid sampler unit %d", sampler_unit);
1743 BLI_assert(false);
1744 return;
1745 }
1746
1747 /* Apply binding. */
1748 this->pipeline_state.sampler_bindings[sampler_unit] = {sampler_state};
1749}
1750
1752 bool is_image,
1754{
1755 BLI_assert(mtl_texture);
1756
1757 if (is_image) {
1758 /* Iterate through textures in state and unbind. */
1759 int i = 0;
1760 for (auto &resource_bind : this->pipeline_state.image_bindings) {
1761 if (resource_bind.texture_resource == mtl_texture) {
1762 resource_bind.texture_resource = nullptr;
1764 }
1765 i++;
1766 }
1767 }
1768 else {
1769 /* Iterate through textures in state and unbind. */
1770 for (auto &resource_bind : this->pipeline_state.texture_bindings) {
1771 if (resource_bind.texture_resource == mtl_texture) {
1772 resource_bind.texture_resource = nullptr;
1773 }
1774 }
1775 }
1776}
1777
1779{
1780 /* Iterate through context's bound textures. */
1781 if (is_image) {
1782 int i = 0;
1783 for (auto &resource_bind : this->pipeline_state.image_bindings) {
1784 if (resource_bind.texture_resource) {
1785 resource_bind.texture_resource = nullptr;
1787 }
1788 i++;
1789 }
1790 }
1791 else {
1792 for (auto &resource_bind : this->pipeline_state.texture_bindings) {
1793 if (resource_bind.texture_resource) {
1794 resource_bind.texture_resource = nullptr;
1795 }
1796 }
1797 }
1798}
1799
1801{
1802 /* Internal sampler states are signal values and do not correspond to actual samplers. */
1804
1805 if (sampler_state.state.type == GPU_SAMPLER_STATE_TYPE_CUSTOM) {
1806 return custom_sampler_state_cache_[sampler_state.state.custom_type];
1807 }
1808
1809 return sampler_state_cache_[sampler_state.state.extend_yz][sampler_state.state.extend_x]
1810 [sampler_state.state.filtering];
1811}
1812
1814static inline MTLSamplerAddressMode to_mtl_type(GPUSamplerExtendMode wrap_mode)
1815{
1816 switch (wrap_mode) {
1818 return MTLSamplerAddressModeClampToEdge;
1820 return MTLSamplerAddressModeRepeat;
1822 return MTLSamplerAddressModeMirrorRepeat;
1824 return MTLSamplerAddressModeClampToBorderColor;
1825 default:
1827 return MTLSamplerAddressModeClampToEdge;
1828 }
1829}
1830
1832{
1833 for (int extend_yz_i = 0; extend_yz_i < GPU_SAMPLER_EXTEND_MODES_COUNT; extend_yz_i++) {
1834 const GPUSamplerExtendMode extend_yz = static_cast<GPUSamplerExtendMode>(extend_yz_i);
1835 const MTLSamplerAddressMode extend_t = to_mtl_type(extend_yz);
1836
1837 for (int extend_x_i = 0; extend_x_i < GPU_SAMPLER_EXTEND_MODES_COUNT; extend_x_i++) {
1838 const GPUSamplerExtendMode extend_x = static_cast<GPUSamplerExtendMode>(extend_x_i);
1839 const MTLSamplerAddressMode extend_s = to_mtl_type(extend_x);
1840
1841 for (int filtering_i = 0; filtering_i < GPU_SAMPLER_FILTERING_TYPES_COUNT; filtering_i++) {
1842 const GPUSamplerFiltering filtering = GPUSamplerFiltering(filtering_i);
1843
1844 MTLSamplerDescriptor *descriptor = [[MTLSamplerDescriptor alloc] init];
1845 descriptor.normalizedCoordinates = true;
1846 descriptor.sAddressMode = extend_s;
1847 descriptor.tAddressMode = extend_t;
1848 descriptor.rAddressMode = extend_t;
1849 descriptor.borderColor = MTLSamplerBorderColorTransparentBlack;
1850 descriptor.minFilter = (filtering & GPU_SAMPLER_FILTERING_LINEAR) ?
1851 MTLSamplerMinMagFilterLinear :
1852 MTLSamplerMinMagFilterNearest;
1853 descriptor.magFilter = (filtering & GPU_SAMPLER_FILTERING_LINEAR) ?
1854 MTLSamplerMinMagFilterLinear :
1855 MTLSamplerMinMagFilterNearest;
1856 descriptor.mipFilter = (filtering & GPU_SAMPLER_FILTERING_MIPMAP) ?
1857 MTLSamplerMipFilterLinear :
1858 MTLSamplerMipFilterNotMipmapped;
1859 descriptor.lodMinClamp = -1000;
1860 descriptor.lodMaxClamp = 1000;
1861 float aniso_filter = min_ff(float(GPU_anisotropic_samples_get(filtering)), 16.0f);
1862 descriptor.maxAnisotropy = (filtering & GPU_SAMPLER_FILTERING_MIPMAP) ? aniso_filter : 1;
1863 descriptor.compareFunction = MTLCompareFunctionAlways;
1864 descriptor.supportArgumentBuffers = true;
1865
1866 id<MTLSamplerState> state = [this->device newSamplerStateWithDescriptor:descriptor];
1867 sampler_state_cache_[extend_yz_i][extend_x_i][filtering_i] = state;
1868
1869 BLI_assert(state != nil);
1870 [descriptor autorelease];
1871 }
1872 }
1873 }
1874
1875 /* Compare sampler for depth textures. */
1876 {
1877 MTLSamplerDescriptor *descriptor = [[MTLSamplerDescriptor alloc] init];
1878 descriptor.minFilter = MTLSamplerMinMagFilterLinear;
1879 descriptor.magFilter = MTLSamplerMinMagFilterLinear;
1880 descriptor.compareFunction = MTLCompareFunctionLessEqual;
1881 descriptor.lodMinClamp = -1000;
1882 descriptor.lodMaxClamp = 1000;
1883 descriptor.supportArgumentBuffers = true;
1884
1885 id<MTLSamplerState> compare_state = [this->device newSamplerStateWithDescriptor:descriptor];
1886 custom_sampler_state_cache_[GPU_SAMPLER_CUSTOM_COMPARE] = compare_state;
1887
1888 BLI_assert(compare_state != nil);
1889 [descriptor autorelease];
1890 }
1891
1892 /* Custom sampler for icons. The icon texture is sampled within the shader using a -0.5f LOD
1893 * bias. */
1894 {
1895 MTLSamplerDescriptor *descriptor = [[MTLSamplerDescriptor alloc] init];
1896 descriptor.minFilter = MTLSamplerMinMagFilterLinear;
1897 descriptor.magFilter = MTLSamplerMinMagFilterLinear;
1898 descriptor.mipFilter = MTLSamplerMipFilterNearest;
1899 descriptor.lodMinClamp = 0;
1900 descriptor.lodMaxClamp = 1;
1901
1902 id<MTLSamplerState> icon_state = [this->device newSamplerStateWithDescriptor:descriptor];
1903 custom_sampler_state_cache_[GPU_SAMPLER_CUSTOM_ICON] = icon_state;
1904
1905 BLI_assert(icon_state != nil);
1906 [descriptor autorelease];
1907 }
1908}
1909
1911{
1912 if (default_sampler_state_ == nil) {
1913 default_sampler_state_ = this->get_sampler_from_state({GPUSamplerState::default_sampler()});
1914 }
1915 return default_sampler_state_;
1916}
1917
1919
1920/* -------------------------------------------------------------------- */
1923
1925{
1926 if (buffer_clear_pso_ != nil) {
1927 return buffer_clear_pso_;
1928 }
1929
1930 /* Fetch active context. */
1931 MTLContext *ctx = MTLContext::get();
1932 BLI_assert(ctx);
1933
1934 @autoreleasepool {
1935 /* Source as NSString. */
1936 const char *src =
1937 "\
1938 struct BufferClearParams {\
1939 uint clear_value;\
1940 };\
1941 kernel void compute_buffer_clear(constant BufferClearParams &params [[buffer(0)]],\
1942 device uint32_t* output_data [[buffer(1)]],\
1943 uint position [[thread_position_in_grid]])\
1944 {\
1945 output_data[position] = params.clear_value;\
1946 }";
1947 NSString *compute_buffer_clear_src = [NSString stringWithUTF8String:src];
1948
1949 /* Prepare shader library for buffer clearing. */
1950 MTLCompileOptions *options = [[[MTLCompileOptions alloc] init] autorelease];
1951 options.languageVersion = MTLLanguageVersion2_2;
1952
1953 NSError *error = nullptr;
1954 id<MTLLibrary> temp_lib = [[ctx->device newLibraryWithSource:compute_buffer_clear_src
1956 error:&error] autorelease];
1957 if (error) {
1958 /* Only exit out if genuine error and not warning. */
1959 if ([[error localizedDescription] rangeOfString:@"Compilation succeeded"].location ==
1960 NSNotFound)
1961 {
1962 CLOG_ERROR(&LOG,
1963 "Compile Error - Metal Shader Library error %s",
1964 [[error localizedDescription] UTF8String]);
1965 BLI_assert(false);
1966 return nil;
1967 }
1968 }
1969
1970 /* Fetch compute function. */
1971 BLI_assert(temp_lib != nil);
1972 id<MTLFunction> temp_compute_function = [[temp_lib newFunctionWithName:@"compute_buffer_clear"]
1973 autorelease];
1974 BLI_assert(temp_compute_function);
1975
1976 /* Compile compute PSO */
1977 buffer_clear_pso_ = [ctx->device newComputePipelineStateWithFunction:temp_compute_function
1978 error:&error];
1979 if (error || buffer_clear_pso_ == nil) {
1980 CLOG_ERROR(&LOG,
1981 "Failed to prepare compute_buffer_clear MTLComputePipelineState %s",
1982 [[error localizedDescription] UTF8String]);
1983 BLI_assert(false);
1984 return nil;
1985 }
1986
1987 [buffer_clear_pso_ retain];
1988 }
1989
1990 BLI_assert(buffer_clear_pso_ != nil);
1991 return buffer_clear_pso_;
1992}
1993
1995
1996/* -------------------------------------------------------------------- */
1999
2000void present(MTLRenderPassDescriptor *blit_descriptor,
2002 id<MTLTexture> swapchain_texture,
2003 id<CAMetalDrawable> drawable)
2004{
2005
2006 MTLContext *ctx = MTLContext::get();
2007 BLI_assert(ctx);
2008
2009 /* Flush any outstanding work. */
2010 ctx->flush();
2011
2012 /* Always pace CPU to maximum of 3 drawables in flight.
2013 * nextDrawable may have more in flight if backing swapchain
2014 * textures are re-allocate, such as during resize events.
2015 *
2016 * Determine frames in flight based on current latency. If
2017 * we are in a high-latency situation, limit frames in flight
2018 * to increase app responsiveness and keep GPU execution under control.
2019 * If latency improves, increase frames in flight to improve overall
2020 * performance. */
2021 int perf_max_drawables = MTL_MAX_DRAWABLES;
2023 perf_max_drawables = 1;
2024 }
2025 else if (MTLContext::avg_drawable_latency_us > 75000) {
2026 perf_max_drawables = 2;
2027 }
2028
2029 while (MTLContext::max_drawables_in_flight > min_ii(perf_max_drawables, MTL_MAX_DRAWABLES)) {
2031 }
2032
2033 /* Present is submitted in its own CMD Buffer to ensure drawable reference released as early as
2034 * possible. This command buffer is separate as it does not utilize the global state
2035 * for rendering as the main context does. */
2036 id<MTLCommandBuffer> cmdbuf = [ctx->queue commandBuffer];
2038
2039 /* Ensure freed buffers have usage tracked against active CommandBuffer submissions. Referenced
2040 * from creation until completion of the command buffer, like
2041 * #MTLCommandBufferManager::ensure_begin. */
2042 MTLSafeFreeList *cmd_free_buffer_list =
2044 BLI_assert(cmd_free_buffer_list);
2045
2046 /* Do Present Call and final Blit to MTLDrawable. */
2047 id<MTLRenderCommandEncoder> enc = [cmdbuf renderCommandEncoderWithDescriptor:blit_descriptor];
2048 [enc setRenderPipelineState:blit_pso];
2049 [enc setFragmentTexture:swapchain_texture atIndex:0];
2050 [enc drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];
2051 [enc endEncoding];
2052
2053 /* Present drawable. */
2054 BLI_assert(drawable);
2055 [cmdbuf presentDrawable:drawable];
2056
2057 /* Increment drawables in flight limiter. */
2059 std::chrono::time_point submission_time = std::chrono::high_resolution_clock::now();
2060
2061 /* Decrement free pool reference upon command buffer completion. */
2062 [cmdbuf addCompletedHandler:^(id<MTLCommandBuffer> /*cb*/) {
2063 /* Flag freed buffers associated with this CMD buffer as ready to be freed. */
2064 cmd_free_buffer_list->decrement_reference();
2065
2066 /* Decrement count */
2068
2069 MTL_LOG_DEBUG("Active command buffers: %d",
2071
2072 /* Drawable count and latency management. */
2074 std::chrono::time_point completion_time = std::chrono::high_resolution_clock::now();
2075 int64_t microseconds_per_frame = std::chrono::duration_cast<std::chrono::microseconds>(
2076 completion_time - submission_time)
2077 .count();
2078 MTLContext::latency_resolve_average(microseconds_per_frame);
2079
2080 MTL_LOG_DEBUG("Frame Latency: %f ms (Rolling avg: %f ms Drawables: %d)",
2081 ((float)microseconds_per_frame) / 1000.0f,
2082 ((float)MTLContext::avg_drawable_latency_us) / 1000.0f,
2083 perf_max_drawables);
2084 }];
2085
2086 [cmdbuf commit];
2087
2088 /* When debugging, fetch advanced command buffer errors. */
2089 if (G.debug & G_DEBUG_GPU) {
2090 [cmdbuf waitUntilCompleted];
2091 NSError *error = [cmdbuf error];
2092 if (error != nil) {
2093 CLOG_ERROR(&LOG, "Command buffer error: %s", [[error localizedDescription] UTF8String]);
2094 BLI_assert(false);
2095 }
2096 }
2097}
2098
2100
2101/* -------------------------------------------------------------------- */
2104
2105void xr_blit(id<MTLTexture> metal_xr_texture,
2106 const int ofsx,
2107 const int ofsy,
2108 const int width,
2109 const int height)
2110{
2112
2113 gpu::MTLFrameBuffer *source_framebuffer = ctx->get_current_framebuffer();
2114 MTLAttachment src_attachment = source_framebuffer->get_color_attachment(0);
2115 id<MTLTexture> src_texture = src_attachment.texture->get_metal_handle_base();
2116
2117 MTLOrigin origin = MTLOriginMake(ofsx, ofsy, 0);
2118 MTLSize size = MTLSizeMake(width, height, 1);
2119
2121 [blit_encoder copyFromTexture:src_texture
2122 sourceSlice:0
2123 sourceLevel:0
2124 sourceOrigin:origin
2125 sourceSize:size
2126 toTexture:metal_xr_texture
2127 destinationSlice:0
2128 destinationLevel:0
2129 destinationOrigin:origin];
2130}
2131
2133
2134} // namespace blender::gpu
Platform independent time functions.
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define GPU_SAMPLER_CUSTOM_TYPES_COUNT
#define GPU_SAMPLER_EXTEND_MODES_COUNT
#define BLI_assert_unreachable()
#define BLI_assert(a)
#define BLI_assert_msg(a, msg)
id< MTLTexture > metalOverlayTexture()
static GHOST_IContext * getActiveDrawingContext()
GHOST_Context * getContext()
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
Definition BLI_map.hh:620
constexpr const char * c_str() const
static MTLCapabilities & get_capabilities()
static MTLBackend * get()
void init(id< MTLDevice > device)
Definition mtl_memory.mm:40
MTLSafeFreeList * acquire_current_safe_list()
gpu::MTLBuffer * allocate(uint64_t size, bool cpu_visible)
id< MTLBuffer > get_metal_buffer() const
void prepare(bool supports_render=true)
MTLRenderPassState & get_render_pass_state()
id< MTLRenderCommandEncoder > ensure_begin_render_command_encoder(MTLFrameBuffer *ctx_framebuffer, bool force_begin, bool *r_new_pass)
id< MTLComputeCommandEncoder > ensure_begin_compute_encoder()
id< MTLBlitCommandEncoder > ensure_begin_blit_encoder()
id< MTLRenderCommandEncoder > get_active_render_command_encoder()
static volatile std::atomic< int > num_active_cmd_bufs_in_system
MTLBindingCache< gpu::MTLComputeCommandEncoder > compute_bindings
void bind_pso(id< MTLComputePipelineState > pso)
id< MTLComputePipelineState > get_buffer_clear_pso()
bool ensure_render_pipeline_state(MTLPrimitiveType prim_type)
MTLFrameBuffer * get_current_framebuffer()
MTLFrameBuffer * get_default_framebuffer()
static std::atomic< int64_t > avg_drawable_latency_us
MTLContextComputeUtils & get_compute_utils()
const MTLComputePipelineStateInstance * ensure_compute_pipeline_state()
id< MTLSamplerState > get_sampler_from_state(MTLSamplerState state)
shader::SpecializationConstants constants_state
void deactivate() override
static void latency_resolve_average(int64_t frame_latency_us)
gpu::MTLBuffer * get_visibility_buffer() const
void set_visibility_buffer(gpu::MTLBuffer *buffer)
void set_scissor_enabled(bool scissor_enabled)
void framebuffer_bind(MTLFrameBuffer *framebuffer)
id< MTLRenderCommandEncoder > ensure_begin_render_pass()
static MTLContext * get()
id< MTLBuffer > get_null_buffer()
friend class MTLRenderPassState
id< MTLBuffer > get_null_attribute_buffer()
static void global_memory_manager_release_ref()
void activate() override
MTLScratchBufferManager memory_manager
MTLContextGlobalShaderPipelineState pipeline_state
MTLSamplerArray & get_sampler_array()
static MTLBufferPool * global_memory_manager
void end_frame() override
static int global_memory_manager_refcount
MTLScratchBufferManager & get_scratch_buffer_manager()
MTLShader * get_active_shader()
void texture_unbind(gpu::MTLTexture *mtl_texture, bool is_image, StateManager *state_manager)
void set_viewport(int origin_x, int origin_y, int width, int height)
static int64_t frame_latency[MTL_FRAME_AVERAGE_COUNT]
static std::mutex global_memory_manager_reflock
void sampler_bind(MTLSamplerState, uint sampler_unit)
void texture_bind(gpu::MTLTexture *mtl_texture, uint texture_unit, bool is_image)
void specialization_constants_set(const shader::SpecializationConstants *constants_state)
void begin_frame() override
void set_viewports(int count, const int(&viewports)[GPU_MAX_VIEWPORTS][4])
void compute_dispatch_indirect(StorageBuf *indirect_buf)
static void check_error(const char *info)
void set_scissor(int scissor_x, int scissor_y, int scissor_width, int scissor_height)
MTLCommandBufferManager main_command_buffer
id< MTLSamplerState > get_default_sampler_state()
bool is_visibility_dirty() const
void texture_unbind_all(bool is_image)
static void global_memory_manager_acquire_ref()
void memory_statistics_get(int *r_total_mem, int *r_free_mem) override
Map< MTLSamplerArray, gpu::MTLBuffer * > & get_sampler_arg_buf_cache()
id< MTLCommandQueue > queue
gpu::MTLTexture * get_dummy_texture(GPUTextureType type, GPUSamplerFormat sampler_format)
void compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len)
MTLContextTextureUtils & get_texture_utils()
static MTLBufferPool * get_global_memory_manager()
MTLContext(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context)
static std::atomic< int > max_drawables_in_flight
MTLAttachment get_color_attachment(uint slot)
bool add_color_attachment(gpu::MTLTexture *texture, uint slot, int miplevel, int layer)
MTLFrameBuffer(MTLContext *ctx, const char *name)
MTLImmediate(MTLContext *ctx)
void bind_vertex_buffer(id< MTLBuffer > buffer, uint64_t buffer_offset, uint index)
MTLBindingCache< gpu::MTLVertexCommandEncoder > vertex_bindings
id< MTLDepthStencilState > bound_ds_state
MTLBindingCache< gpu::MTLFragmentCommandEncoder > fragment_bindings
id< MTLRenderPipelineState > bound_pso
MTLBoundShaderState last_bound_shader_state
const char * sampler_name_get(int slot) const
const char * image_name_get(int slot) const
id< MTLArgumentEncoder > ensure_argument_encoder(id< MTLFunction > mtl_function)
const char * name_at_offset(uint32_t offset) const
const MTLComputePipelineStateCommon & get_compute_common_state()
MTLComputePipelineStateInstance * bake_compute_pipeline_state(MTLContext *ctx, MTLComputePipelineStateDescriptor &compute_pipeline_descriptor)
void uniform_int(int location, int comp_len, int array_size, const int *data) override
MTLShaderInterface & get_interface()
MTLRenderPipelineStateInstance * bake_current_pipeline_state(MTLContext *ctx, MTLPrimitiveTopologyClass prim_type)
MTLPushConstantBuf * get_push_constant_buf()
MTLStateManager(MTLContext *ctx)
Definition mtl_state.mm:31
void bind(int slot) override
id< MTLTexture > get_metal_handle()
friend void xr_blit(id< MTLTexture > metal_xr_texture, int ofsx, int ofsy, int width, int height)
id< MTLTexture > get_metal_handle_base()
MTLStorageBuf * get_storagebuf()
const int * get_texture_metadata_ptr() const
void make_resident(Encoder &enc)
id< MTLAccelerationStructure > acceleration_structure() const
const ShaderInput * uniform_get(const StringRefNull name) const
const ShaderInput * texture_get(const int binding) const
const ShaderInput * ubo_get(const StringRefNull name) const
const ShaderInput * ssbo_get(const StringRefNull name) const
StringRefNull name_get() const
virtual void apply_state()=0
std::array< TextureWriteFormat, GPU_MAX_IMAGE > image_formats
eGPUTextureUsage usage_get() const
CCL_NAMESPACE_BEGIN struct Options options
constexpr T clamp(T, U, U) RET
const int state
#define MTL_MAX_SAMPLER_SLOTS
#define MTL_MAX_TEXTURE_SLOTS
#define MTL_MAX_DRAWABLES
Definition mtl_common.hh:14
#define MTL_FRAME_AVERAGE_COUNT
Definition mtl_common.hh:13
#define MTL_LOG_WARNING(info,...)
Definition mtl_debug.hh:39
#define MTL_LOG_DEBUG(info,...)
Definition mtl_debug.hh:46
#define MTL_LOG_ERROR(info,...)
Definition mtl_debug.hh:31
#define MTL_SAMPLER_ARGUMENT_BUFFER_SLOT
#define MTL_ACCELERATION_STRUCTURE_SLOT
#define MTL_PUSH_CONSTANT_BUFFER_SLOT
#define MTL_UBO_SLOT_OFFSET
#define MTL_IMAGE_SLOT_OFFSET
#define MTL_SAMPLER_SLOT_OFFSET
#define MTL_SSBO_SLOT_OFFSET
OneBitIteratorRange iter_1_indices(BitInt value)
void free(Subdiv *subdiv)
Definition subdiv.cc:192
void present(MTLRenderPassDescriptor *blit_descriptor, id< MTLRenderPipelineState > blit_pso, id< MTLTexture > swapchain_texture, id< CAMetalDrawable > drawable)
static void ensure_buffer_bindings(MTLContext &ctx, MTLShader &shader, CommandEncoderT enc, MTLBindingCache< CommandEncoderT > &bindings, uint32_t stage_buf_mask=uint32_t(-1))
static CLG_LogRef LOG
static void ensure_push_constant(MTLContext &ctx, MTLShader &shader, CommandEncoderT enc, MTLBindingCache< CommandEncoderT > &bindings, uint32_t stage_buf_mask=uint32_t(-1))
static void ensure_texture_bindings(MTLContext &ctx, MTLShader &shader, CommandEncoderT enc, MTLBindingCache< CommandEncoderT > &bindings, id< MTLFunction > mtl_function, uint16_t stage_ima_mask=uint16_t(-1), uint64_t stage_tex_mask=uint64_t(-1))
static void bind_atomic_workaround_buffer(StringRefNull texture_name, MTLTexture *gpu_tex, MTLShaderInterface &shader_interface, MTLShader &active_shader)
static void bind_sampler_argument_buffer(CommandEncoderT enc, MTLSamplerArray &sampler_array, Map< MTLSamplerArray, gpu::MTLBuffer * > &sampler_buffers_cache, MTLShaderInterface &shader_interface, id< MTLFunction > mtl_function, MTLBindingCache< CommandEncoderT > &bindings)
const MTLSamplerState DEFAULT_SAMPLER_STATE
@ MTL_PIPELINE_STATE_CULLMODE_FLAG
@ MTL_PIPELINE_STATE_SCISSOR_FLAG
@ MTL_PIPELINE_STATE_FRONT_FACING_FLAG
@ MTL_PIPELINE_STATE_DEPTHSTENCIL_FLAG
@ MTL_PIPELINE_STATE_VIEWPORT_FLAG
@ MTL_PIPELINE_STATE_ALL_FLAG
static MTLSamplerAddressMode to_mtl_type(GPUSamplerExtendMode wrap_mode)
void xr_blit(id< MTLTexture > metal_xr_texture, int ofsx, int ofsy, int width, int height)
MINLINE float min_ff(float a, float b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
gpu::Texture * GPU_texture_create_cube_array(const char *name, int width, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
@ GPU_CULL_FRONT
Definition GPU_state.hh:158
@ GPU_CULL_NONE
Definition GPU_state.hh:157
@ GPU_CULL_BACK
Definition GPU_state.hh:159
unsigned __int64 uint64_t
Definition BKE_text.h:91
gpu::Texture * GPU_texture_create_1d(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
__int64 int64_t
Definition BKE_text.h:90
unsigned int uint
Definition BKE_text.h:55
bool assign_if_different(T &old_value, T &&new_value)
BLI_INLINE float fb(float length, float L)
void BLI_time_sleep_ms(int ms)
Definition time.cc:91
@ GPU_SAMPLER_EXTEND_MODE_MIRRORED_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
void GPU_texture_unbind_all()
gpu::Texture * GPU_texture_create_2d_array(const char *name, int width, int height, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
@ GPU_SAMPLER_FILTERING_LINEAR
@ GPU_SAMPLER_FILTERING_MIPMAP
MINLINE uint max_uu(uint a, uint b)
MINLINE int max_ii(int a, int b)
ATTR_WARN_UNUSED_RESULT const BMVert * v
Global G
Definition blender.cc:57
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
@ GPU_TEXTURE_USAGE_GENERAL
@ GPU_TEXTURE_USAGE_ATOMIC
gpu::Texture * GPU_texture_create_from_vertbuf(const char *name, gpu::VertBuf *vertex_buf)
BMesh const char void * data
gpu::Texture * GPU_texture_create_3d(const char *name, int width, int height, int depth, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const void *data)
int GPU_max_textures()
gpu::VertBuf * GPU_vertbuf_create_with_format_ex(const GPUVertFormat &format, GPUUsageType usage)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
gpu::Texture * GPU_texture_create_cube(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
@ GPU_SAMPLER_CUSTOM_ICON
@ GPU_SAMPLER_CUSTOM_COMPARE
void GPU_flush()
Definition gpu_state.cc:284
void GPU_texture_free(gpu::Texture *texture)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
@ GPU_SAMPLER_STATE_TYPE_CUSTOM
@ GPU_SAMPLER_STATE_TYPE_INTERNAL
void GPU_vertformat_clear(GPUVertFormat *)
void immDeactivate()
void immActivate()
MINLINE unsigned int bitscan_reverse_uint64(unsigned long long a)
void GPU_uniformbuf_unbind(gpu::UniformBuf *ubo)
void GPU_vertbuf_discard(gpu::VertBuf *)
MINLINE int min_ii(int a, int b)
@ GPU_USAGE_FLAG_BUFFER_TEXTURE_ONLY
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
gpu::Texture * GPU_texture_create_1d_array(const char *name, int width, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
GPUSamplerStateType type
GPUSamplerExtendMode extend_yz
GPUSamplerFiltering filtering
GPUSamplerCustomType custom_type
GPUSamplerExtendMode extend_x
static constexpr GPUSamplerState default_sampler()
void bind_buffer(CommandEncoderT enc, id< MTLBuffer > buf, size_t offset, uint index)
void bind_texture(CommandEncoderT enc, id< MTLTexture > tex, uint index)
void bind_sampler(CommandEncoderT enc, gpu::MTLSamplerArray &sampler_array, id< MTLSamplerState > sampler_state, gpu::MTLSamplerState binding_state, bool use_samplers_argument_buffer, uint index)
void bind_bytes(CommandEncoderT enc, MTLScratchBufferManager &scratch_buffer, const void *bytes, size_t length, uint index)
MTLStencilOperation stencil_op_back_depthstencil_pass
MTLStencilOperation stencil_op_front_stencil_fail
MTLStencilOperation stencil_op_front_depthstencil_pass
std::array< MTLStorageBufferBinding, MTL_MAX_SSBO > ssbo_bindings
std::array< MTLTextureBinding, MTL_MAX_SAMPLER_SLOTS > texture_bindings
std::array< MTLAccelerationStructureBinding, 1 > acceleration_structure_bindings
std::array< MTLUniformBufferBinding, MTL_MAX_UBO > ubo_bindings
std::array< MTLSamplerBinding, MTL_MAX_SAMPLER_SLOTS > sampler_bindings
std::array< MTLTextureBinding, MTL_MAX_IMAGE_SLOTS > image_bindings
id< MTLSamplerState > mtl_sampler[MTL_MAX_TEXTURE_SLOTS]
Vector< SpecializationConstant::Value, 8 > values