Blender V5.3
mtl_framebuffer.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 "BLI_string.hh"
10
11#include "BKE_global.hh"
12
13#include "mtl_context.hh"
14#include "mtl_debug.hh"
15#include "mtl_framebuffer.hh"
16#include "mtl_texture.hh"
17#import <Availability.h>
18
19namespace blender::gpu {
20
21/* -------------------------------------------------------------------- */
24
26{
27
28 context_ = ctx;
29 is_dirty_ = true;
30 is_loadstore_dirty_ = true;
31 dirty_state_ctx_ = nullptr;
32 has_pending_clear_ = false;
33 colour_attachment_count_ = 0;
34 enabled_srgb_ = false;
35 srgb_ = false;
36
37 for (int i = 0; i < GPU_FB_MAX_COLOR_ATTACHMENT; i++) {
38 mtl_color_attachments_[i].used = false;
39 }
40 mtl_depth_attachment_.used = false;
41 mtl_stencil_attachment_.used = false;
42
43 for (int i = 0; i < MTL_FB_CONFIG_MAX; i++) {
44 framebuffer_descriptor_[i] = [[MTLRenderPassDescriptor alloc] init];
45 descriptor_dirty_[i] = true;
46 }
47
48 for (int i = 0; i < GPU_FB_MAX_COLOR_ATTACHMENT; i++) {
49 colour_attachment_descriptors_[i] = [[MTLRenderPassColorAttachmentDescriptor alloc] init];
50 }
51
52 /* Initial state. */
53 this->size_set(0, 0);
54 this->viewport_reset();
55 this->scissor_reset();
56}
57
59{
60 /* If FrameBuffer is associated with a currently open RenderPass, end. */
61 if (context_->main_command_buffer.get_active_framebuffer() == this) {
62 context_->main_command_buffer.end_active_command_encoder();
63 }
64
65 /* Restore default frame-buffer if this frame-buffer was bound. */
66 if (context_->active_fb == this && context_->back_left != this) {
67 /* If this assert triggers it means the frame-buffer is being freed while in use by another
68 * context which, by the way, is TOTALLY UNSAFE!!! (Copy from GL behavior). */
69 BLI_assert(context_ == MTLContext::get());
71 }
72
73 /* Free Render Pass Descriptors. */
74 for (int config = 0; config < MTL_FB_CONFIG_MAX; config++) {
75 if (framebuffer_descriptor_[config] != nil) {
76 [framebuffer_descriptor_[config] release];
77 framebuffer_descriptor_[config] = nil;
78 }
79 }
80
81 /* Free color attachment descriptors. */
82 for (int i = 0; i < GPU_FB_MAX_COLOR_ATTACHMENT; i++) {
83 if (colour_attachment_descriptors_[i] != nil) {
84 [colour_attachment_descriptors_[i] release];
85 colour_attachment_descriptors_[i] = nil;
86 }
87 }
88
89 /* Remove attachments - release FB texture references. */
91
92 if (context_ == nullptr) {
93 return;
94 }
95}
96
98{
99 /* Ensure local MTLAttachment data is up to date.
100 * NOTE: We only refresh viewport/scissor region when attachments are updated during bind.
101 * This is to ensure state is consistent with the OpenGL backend. */
102 if (dirty_attachments_) {
103 this->update_attachments(true);
104 this->viewport_reset();
105 this->scissor_reset();
106 }
107}
108
109void MTLFrameBuffer::bind(bool enabled_srgb)
110{
111
112 /* Verify Context is valid. */
113 if (context_ != MTLContext::get()) {
114 BLI_assert_msg(false, "Trying to use the same frame-buffer in multiple context's.");
115 return;
116 }
117
118 /* Ensure local MTLAttachment data is up to date.
119 * NOTE: We only refresh viewport/scissor region when attachments are updated during bind.
120 * This is to ensure state is consistent with the OpenGL backend. */
122
123 /* Ensure SRGB state is up-to-date and valid. */
124 bool srgb_state_changed = enabled_srgb_ != enabled_srgb;
125 if (context_->active_fb != this || srgb_state_changed) {
126 if (srgb_state_changed) {
127 this->mark_dirty();
128 }
129 enabled_srgb_ = enabled_srgb;
130 Shader::set_framebuffer_srgb_target(enabled_srgb && srgb_);
131 }
132
133 /* Reset clear state on bind -- Clears and load/store ops are set after binding. */
134 this->reset_clear_state();
135
136 /* Bind to active context. */
137 MTLContext *mtl_context = MTLContext::get();
138 if (mtl_context) {
139 mtl_context->framebuffer_bind(this);
140 dirty_state_ = true;
141 }
142 else {
143 MTL_LOG_WARNING("Attempting to bind FrameBuffer, but no context is active");
144 }
145}
146
147bool MTLFrameBuffer::check(char err_out[256])
148{
149 /* Ensure local MTLAttachment data is up to date.
150 * NOTE: We only refresh viewport/scissor region when attachments are updated during bind.
151 * This is to ensure state is consistent with the OpenGL backend. */
153
154 /* Ensure there is at least one attachment. */
155 bool valid = (this->get_attachment_count() > 0 || this->has_depth_attachment() ||
156 this->has_stencil_attachment());
157 if (!valid) {
158 const char *format = "Framebuffer %s does not have any attachments.\n";
159 if (err_out) {
160 BLI_snprintf(err_out, 256, format, name_);
161 }
162 else {
163 MTL_LOG_ERROR(format, name_);
164 }
165 return false;
166 }
167
168 /* Ensure all attachments have identical dimensions. */
169 /* Ensure all attachments are render-targets. */
170 bool first = true;
171 uint dim_x = 0;
172 uint dim_y = 0;
173 for (int col_att = 0; col_att < this->get_attachment_count(); col_att++) {
174 MTLAttachment att = this->get_color_attachment(col_att);
175 if (att.used) {
176 if (att.texture->internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_ATTACHMENT) {
177 if (first) {
178 dim_x = att.texture->width_get();
179 dim_y = att.texture->height_get();
180 first = false;
181 }
182 else {
183 if (dim_x != att.texture->width_get() || dim_y != att.texture->height_get()) {
184 const char *format =
185 "Framebuffer %s: Color attachment dimensions do not match those of previous "
186 "attachment\n";
187 if (err_out) {
188 BLI_snprintf(err_out, 256, format, name_);
189 }
190 else {
191 MTL_LOG_ERROR(format, name_);
192 }
193 return false;
194 }
195 }
196 }
197 else {
198 const char *format =
199 "Framebuffer %s: Color attachment texture does not have usage flag "
200 "'GPU_TEXTURE_USAGE_ATTACHMENT'\n";
201 if (err_out) {
202 BLI_snprintf(err_out, 256, format, name_);
203 }
204 else {
205 MTL_LOG_ERROR(format, name_);
206 }
207 return false;
208 }
209 }
210 }
211 MTLAttachment depth_att = this->get_depth_attachment();
212 MTLAttachment stencil_att = this->get_stencil_attachment();
213 if (depth_att.used) {
214 if (first) {
215 dim_x = depth_att.texture->width_get();
216 dim_y = depth_att.texture->height_get();
217 first = false;
218 valid = (depth_att.texture->internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_ATTACHMENT);
219
220 if (!valid) {
221 const char *format =
222 "Framebuffer %s: Depth attachment does not have usage "
223 "'GPU_TEXTURE_USAGE_ATTACHMENT'\n";
224 if (err_out) {
225 BLI_snprintf(err_out, 256, format, name_);
226 }
227 else {
228 MTL_LOG_ERROR(format, name_);
229 }
230 return false;
231 }
232 }
233 else {
234 if (dim_x != depth_att.texture->width_get() || dim_y != depth_att.texture->height_get()) {
235 const char *format =
236 "Framebuffer %s: Depth attachment dimensions do not match that of previous "
237 "attachment\n";
238 if (err_out) {
239 BLI_snprintf(err_out, 256, format, name_);
240 }
241 else {
242 MTL_LOG_ERROR(format, name_);
243 }
244 return false;
245 }
246 }
247 }
248 if (stencil_att.used) {
249 if (first) {
250 dim_x = stencil_att.texture->width_get();
251 dim_y = stencil_att.texture->height_get();
252 first = false;
253 valid = (stencil_att.texture->internal_gpu_image_usage_flags_ &
255 if (!valid) {
256 const char *format =
257 "Framebuffer %s: Stencil attachment does not have usage "
258 "'GPU_TEXTURE_USAGE_ATTACHMENT'\n";
259 if (err_out) {
260 BLI_snprintf(err_out, 256, format, name_);
261 }
262 else {
263 MTL_LOG_ERROR(format, name_);
264 }
265 return false;
266 }
267 }
268 else {
269 if (dim_x != stencil_att.texture->width_get() || dim_y != stencil_att.texture->height_get())
270 {
271 const char *format =
272 "Framebuffer %s: Stencil attachment dimensions do not match that of previous "
273 "attachment";
274 if (err_out) {
275 BLI_snprintf(err_out, 256, format, name_);
276 }
277 else {
278 MTL_LOG_ERROR(format, name_);
279 }
280 return false;
281 }
282 }
283 }
284
285 BLI_assert(valid);
286 return valid;
287}
288
289void MTLFrameBuffer::force_clear()
290{
291 /* Perform clear by ending current and starting a new render pass. */
292 MTLContext *mtl_context = MTLContext::get();
293 MTLFrameBuffer *current_framebuffer = mtl_context->get_current_framebuffer();
294 if (current_framebuffer) {
295 BLI_assert(current_framebuffer == this);
296 /* End current render-pass. */
297 if (mtl_context->main_command_buffer.is_inside_render_pass()) {
299 }
300 mtl_context->ensure_begin_render_pass();
301 BLI_assert(has_pending_clear_ == false);
302 }
303}
304
306 const double4 clear_col,
307 float clear_depth,
308 uint clear_stencil)
309{
310
311 BLI_assert(MTLContext::get() == context_);
312 BLI_assert(context_->active_fb == this);
313
314 /* Ensure attachments are up to date. */
315 this->update_attachments(true);
316
317 /* If we had no previous clear pending, reset clear state. */
318 if (!has_pending_clear_) {
319 this->reset_clear_state();
320 }
321
322 /* Ensure we only clear if attachments exist for given buffer bits. */
323 bool do_clear = false;
324 if (buffers & GPU_COLOR_BIT) {
325 for (int i = 0; i < colour_attachment_count_; i++) {
326 this->set_color_attachment_clear_color(i, clear_col);
327 do_clear = true;
328 }
329 }
330
331 if (buffers & GPU_DEPTH_BIT) {
332 this->set_depth_attachment_clear_value(clear_depth);
333 do_clear = do_clear || this->has_depth_attachment();
334 }
335 if (buffers & GPU_STENCIL_BIT) {
336 this->set_stencil_attachment_clear_value(clear_stencil);
337 do_clear = do_clear || this->has_stencil_attachment();
338 }
339
340 if (do_clear) {
341 has_pending_clear_ = true;
342
343 /* Apply state before clear. */
344 this->apply_state();
345
346 /* TODO(Metal): Optimize - Currently force-clear always used. Consider moving clear state to
347 * MTLTexture instead. */
348 /* Force clear if RP is not yet active -- not the most efficient, but there is no distinction
349 * between clears where no draws occur. Can optimize at the high-level by using explicit
350 * load-store flags. */
351 this->force_clear();
352 }
353}
354
356{
357 /* If we had no previous clear pending, reset clear state. */
358 if (!has_pending_clear_) {
359 this->reset_clear_state();
360 }
361
362 bool do_clear = false;
363 for (int i = 0; i < this->get_attachment_limit(); i++) {
364 if (this->has_attachment_at_slot(i)) {
365 this->set_color_attachment_clear_color(i, clear_cols[i]);
366 do_clear = true;
367 }
368 }
369
370 if (do_clear) {
371 has_pending_clear_ = true;
372
373 /* Apply state before clear. */
374 this->apply_state();
375
376 /* TODO(Metal): Optimize - Currently force-clear always used. Consider moving clear state to
377 * MTLTexture instead. */
378 /* Force clear if RP is not yet active -- not the most efficient, but there is no distinction
379 * between clears where no draws occur. Can optimize at the high-level by using explicit
380 * load-store flags. */
381 this->force_clear();
382 }
383}
384
386{
387 BLI_assert(MTLContext::get() == context_);
388 BLI_assert(context_->active_fb == this);
389
390 /* If we had no previous clear pending, reset clear state. */
391 if (!has_pending_clear_) {
392 this->reset_clear_state();
393 }
394
395 bool do_clear = false;
396
397 if (type == GPU_FB_DEPTH_ATTACHMENT) {
398 if (this->has_depth_attachment()) {
399 this->set_depth_attachment_clear_value(clear_value.x);
400 do_clear = true;
401 }
402 }
403 else {
404 int slot = type - GPU_FB_COLOR_ATTACHMENT0;
405 if (this->has_attachment_at_slot(slot)) {
406 this->set_color_attachment_clear_color(slot, clear_value);
407 do_clear = true;
408 }
409 }
410
411 if (do_clear) {
412 has_pending_clear_ = true;
413
414 /* Apply state before clear. */
415 this->apply_state();
416
417 /* TODO(Metal): Optimize - Currently force-clear always used. Consider moving clear state to
418 * MTLTexture instead. */
419 /* Force clear if RP is not yet active -- not the most efficient, but there is no distinction
420 * between clears where no draws occur. Can optimize at the high-level by using explicit
421 * load-store flags. */
422 this->force_clear();
423 }
424}
426 Span<GPUAttachmentState> color_attachment_states)
427{
428 if (!MTLBackend::capabilities.supports_native_tile_inputs) {
429 /* Break render-pass if tile memory is unsupported to ensure current frame-buffer results are
430 * stored. */
431 context_->main_command_buffer.end_active_command_encoder(true);
432
433 /* Bind frame-buffer attachments as textures.
434 * NOTE: Follows behavior of gl_framebuffer. However, shaders utilizing subpass_in will
435 * need to avoid bind-point collisions for image/texture resources. */
436 for (int i : color_attachment_states.index_range()) {
438 gpu::Texture *attach_tex = this->attachments_[type].tex;
439 if (color_attachment_states[i] == GPU_ATTACHMENT_READ) {
440 GPU_texture_image_bind(attach_tex, i);
441 }
442 }
443 }
444
445 for (int i : color_attachment_states.index_range()) {
446 /* The ignored state is baked into the PSO as color mask. */
447 mtl_color_attachments_[i].ignored = (color_attachment_states[i] == GPU_ATTACHMENT_IGNORE);
448 }
449}
450
452 eGPUDataFormat format,
453 const int area[4],
454 int channel_len,
455 int slot,
456 void *r_data)
457{
458
459 BLI_assert((planes & GPU_STENCIL_BIT) == 0);
460 BLI_assert(area[2] > 0);
461 BLI_assert(area[3] > 0);
462
463 /* Early exit if requested read region area is zero. */
464 if (area[2] <= 0 || area[3] <= 0) {
465 return;
466 }
467
468 switch (planes) {
469 case GPU_DEPTH_BIT: {
470 if (this->has_depth_attachment()) {
471 MTLAttachment depth = this->get_depth_attachment();
472 gpu::MTLTexture *tex = depth.texture;
473 if (tex) {
474 size_t sample_len = area[2] * area[3];
475 size_t sample_size = to_bytesize(tex->format_, format);
476 size_t debug_data_size = sample_len * sample_size;
477 tex->read_internal(0,
478 area[0],
479 area[1],
480 0,
481 area[2],
482 area[3],
483 1,
484 format,
485 channel_len,
486 debug_data_size,
487 r_data);
488 }
489 }
490 else {
492 "Attempting to read depth from a framebuffer which does not have a depth "
493 "attachment!");
494 }
495 }
496 return;
497
498 case GPU_COLOR_BIT: {
499 if (this->has_attachment_at_slot(slot)) {
501 gpu::MTLTexture *tex = color.texture;
502 if (tex) {
503 size_t sample_len = area[2] * area[3];
504 size_t sample_size = to_bytesize(tex->format_, format);
505 size_t debug_data_size = sample_len * sample_size * channel_len;
506 tex->read_internal(0,
507 area[0],
508 area[1],
509 0,
510 area[2],
511 area[3],
512 1,
513 format,
514 channel_len,
515 debug_data_size,
516 r_data);
517 }
518 }
519 }
520 return;
521
522 case GPU_STENCIL_BIT:
523 MTL_LOG_ERROR("Framebuffer: Trying to read stencil bit. Unsupported.");
524 return;
525 }
526}
527
529 int src_slot,
530 FrameBuffer *dst,
531 int dst_slot,
532 int dst_offset_x,
533 int dst_offset_y)
534{
536 static_cast<MTLFrameBuffer *>(dst)->ensure_attachments_and_viewport();
537
538 BLI_assert(planes != 0);
539
540 MTLFrameBuffer *metal_fb_write = static_cast<MTLFrameBuffer *>(dst);
541
542 BLI_assert(this);
543 BLI_assert(metal_fb_write);
544
545 /* Get width/height from attachment. */
546 MTLAttachment src_attachment;
547 const bool do_color = (planes & GPU_COLOR_BIT);
548 const bool do_depth = (planes & GPU_DEPTH_BIT);
549 const bool do_stencil = (planes & GPU_STENCIL_BIT);
550
551 if (do_color) {
552 BLI_assert(!do_depth && !do_stencil);
553 src_attachment = this->get_color_attachment(src_slot);
554 }
555 else if (do_depth) {
556 BLI_assert(!do_color && !do_stencil);
557 src_attachment = this->get_depth_attachment();
558 }
559 else if (do_stencil) {
560 BLI_assert(!do_color && !do_depth);
561 src_attachment = this->get_stencil_attachment();
562 }
563
564 BLI_assert(src_attachment.used);
565 this->blit(src_slot,
566 0,
567 0,
568 metal_fb_write,
569 dst_slot,
570 dst_offset_x,
571 dst_offset_y,
572 src_attachment.texture->width_get(),
573 src_attachment.texture->height_get(),
574 planes);
575}
576
578
579/* -------------------------------------------------------------------- */
582
584{
585 is_dirty_ = true;
586 is_loadstore_dirty_ = true;
587}
588
590{
591 is_loadstore_dirty_ = true;
592}
593
595{
596 has_pending_clear_ = false;
597}
598
600{
601 has_pending_clear_ = true;
602}
603
604void MTLFrameBuffer::update_attachments(bool /*update_viewport*/)
605{
606 if (!dirty_attachments_) {
607 return;
608 }
609 /* Clear current attachments state. */
611
612 /* Reset framebuffer options. */
613 use_multilayered_rendering_ = false;
614
615 /* Track first attachment for SRGB property extraction. */
616 GPUAttachmentType first_attachment = GPU_FB_MAX_ATTACHMENT;
617 MTLAttachment first_attachment_mtl;
618
619 /* Scan through changes to attachments and populate local structures. */
620 bool depth_added = false;
621 for (GPUAttachmentType type = GPU_FB_MAX_ATTACHMENT - 1; type >= 0; --type) {
622 GPUAttachment &attach = attachments_[type];
623
624 switch (type) {
627 /* If one of the DEPTH types has added a texture, we avoid running this again, as it would
628 * only remove the target. */
629 if (depth_added) {
630 break;
631 }
632 if (attach.tex) {
633 /* If we already had a depth attachment, preserve load/clear-state parameters,
634 * but remove existing and add new attachment. */
635 if (this->has_depth_attachment()) {
636 MTLAttachment depth_attachment_prev = this->get_depth_attachment();
639 static_cast<gpu::MTLTexture *>(attach.tex), attach.mip, attach.layer);
640 this->set_depth_attachment_clear_value(depth_attachment_prev.clear_value.x);
641 this->set_depth_loadstore_op(depth_attachment_prev.load_action,
642 depth_attachment_prev.store_action);
643 }
644 else {
646 static_cast<gpu::MTLTexture *>(attach.tex), attach.mip, attach.layer);
647 }
648
649 /* Check stencil component -- if supplied texture format supports stencil. */
650 TextureFormat format = GPU_texture_format(attach.tex);
651 bool use_stencil = (type == GPU_FB_DEPTH_STENCIL_ATTACHMENT) &&
652 (format == TextureFormat::SFLOAT_32_DEPTH_UINT_8);
653 if (use_stencil) {
654 if (this->has_stencil_attachment()) {
655 MTLAttachment stencil_attachment_prev = this->get_stencil_attachment();
658 static_cast<gpu::MTLTexture *>(attach.tex), attach.mip, attach.layer);
659 this->set_stencil_attachment_clear_value(stencil_attachment_prev.clear_value.x);
660 this->set_stencil_loadstore_op(stencil_attachment_prev.load_action,
661 stencil_attachment_prev.store_action);
662 }
663 else {
665 static_cast<gpu::MTLTexture *>(attach.tex), attach.mip, attach.layer);
666 }
667 }
668
669 /* Flag depth as added -- mirrors the behavior in gl_framebuffer.cc to exit the for-loop
670 * after GPU_FB_DEPTH_STENCIL_ATTACHMENT has executed. */
671 depth_added = true;
672
673 if (first_attachment == GPU_FB_MAX_ATTACHMENT) {
674 /* Only use depth texture to get information if there is no color attachment. */
675 first_attachment = type;
676 first_attachment_mtl = this->get_depth_attachment();
677 }
678 }
679 else {
683 }
684 }
685 } break;
694 int color_slot_ind = type - GPU_FB_COLOR_ATTACHMENT0;
695 if (attach.tex) {
696 /* If we already had a color attachment, preserve load/clear-state parameters,
697 * but remove existing and add new attachment. */
698 if (this->has_attachment_at_slot(color_slot_ind)) {
699 MTLAttachment color_attachment_prev = this->get_color_attachment(color_slot_ind);
700
701 this->remove_color_attachment(color_slot_ind);
702 this->add_color_attachment(static_cast<gpu::MTLTexture *>(attach.tex),
703 color_slot_ind,
704 attach.mip,
705 attach.layer);
706 this->set_color_attachment_clear_color(color_slot_ind,
707 color_attachment_prev.clear_value);
708 this->set_color_loadstore_op(color_slot_ind,
709 color_attachment_prev.load_action,
710 color_attachment_prev.store_action);
711 }
712 else {
713 this->add_color_attachment(static_cast<gpu::MTLTexture *>(attach.tex),
714 color_slot_ind,
715 attach.mip,
716 attach.layer);
717 }
718 first_attachment = type;
719 first_attachment_mtl = this->get_color_attachment(color_slot_ind);
720 }
721 else {
722 this->remove_color_attachment(color_slot_ind);
723 }
724 } break;
725 default:
726 /* Non-attachment parameters. */
727 break;
728 }
729 }
730
731 /* Extract attachment size and determine if framebuffer is SRGB. */
732 if (first_attachment != GPU_FB_MAX_ATTACHMENT) {
733 /* Ensure size is correctly assigned. */
734 GPUAttachment &attach = attachments_[first_attachment];
735 int size[3];
736 GPU_texture_get_mipmap_size(attach.tex, attach.mip, size);
737 this->size_set(size[0], size[1]);
738 srgb_ = (GPU_texture_format(attach.tex) == TextureFormat::SRGBA_8_8_8_8);
739 }
740
741 /* We have now updated our internal structures. */
742 dirty_attachments_ = false;
743}
744
746{
747 MTLContext *mtl_ctx = MTLContext::get();
748 BLI_assert(mtl_ctx);
749 if (mtl_ctx->active_fb == this) {
750 if (dirty_state_ == false && dirty_state_ctx_ == mtl_ctx) {
751 return;
752 }
753
754 /* Ensure viewport has been set. NOTE: This should no longer happen, but kept for safety to
755 * track bugs. If viewport size is zero, use framebuffer size. */
756 int viewport_w = viewport_[0][2];
757 int viewport_h = viewport_[0][3];
758 if (viewport_w == 0 || viewport_h == 0) {
759 MTL_LOG_WARNING("Viewport had width and height of (0,0) -- Updating -- DEBUG Safety check");
760 viewport_w = attachment_width_;
761 viewport_h = attachment_height_;
762 }
763
764 /* Update Context State. */
765 if (multi_viewport_) {
766 mtl_ctx->set_viewports(GPU_MAX_VIEWPORTS, viewport_);
767 }
768 else {
769 mtl_ctx->set_viewport(viewport_[0][0], viewport_[0][1], viewport_w, viewport_h);
770 }
771 mtl_ctx->set_scissor(scissor_[0], scissor_[1], scissor_[2], scissor_[3]);
773
774 dirty_state_ = false;
775 dirty_state_ctx_ = mtl_ctx;
776 }
777 else {
779 "Attempting to set FrameBuffer State (VIEWPORT, SCISSOR), But FrameBuffer is not bound to "
780 "current Context.");
781 }
782}
783
785
786/* -------------------------------------------------------------------- */
789
791 uint slot,
792 int miplevel,
793 int layer)
794{
795 BLI_assert(this);
796 BLI_assert(slot >= 0 && slot < this->get_attachment_limit());
798
799 if (texture) {
800 if (miplevel < 0 || miplevel >= MTL_MAX_MIPMAP_COUNT) {
801 MTL_LOG_WARNING("Attachment specified with invalid mip level %u", miplevel);
802 miplevel = 0;
803 }
804
805 /* Check if slot is in-use. */
806 /* Assume attachment load by default. */
807 colour_attachment_count_ += (!mtl_color_attachments_[slot].used) ? 1 : 0;
808 mtl_color_attachments_[slot].used = true;
809 mtl_color_attachments_[slot].texture = texture;
810 mtl_color_attachments_[slot].mip = miplevel;
811 mtl_color_attachments_[slot].load_action = GPU_LOADACTION_LOAD;
812 mtl_color_attachments_[slot].store_action = GPU_STOREACTION_STORE;
813 mtl_color_attachments_[slot].render_target_array_length = 0;
814
815 /* Determine whether array slice or depth plane based on texture type. */
816 switch (texture->type_) {
817 case GPU_TEXTURE_1D:
818 case GPU_TEXTURE_2D:
819 BLI_assert(layer <= 0);
820 mtl_color_attachments_[slot].slice = 0;
821 mtl_color_attachments_[slot].depth_plane = 0;
822 break;
824 if (layer < 0) {
825 layer = 0;
826 MTL_LOG_WARNING("TODO: Support layered rendering for 1D array textures, if needed.");
827 }
829 mtl_color_attachments_[slot].slice = layer;
830 mtl_color_attachments_[slot].depth_plane = 0;
831 break;
834 mtl_color_attachments_[slot].slice = layer;
835 mtl_color_attachments_[slot].depth_plane = 0;
836 if (layer == -1) {
837 mtl_color_attachments_[slot].slice = 0;
838 mtl_color_attachments_[slot].render_target_array_length = texture->d_;
839 use_multilayered_rendering_ = true;
840 }
841 break;
842 case GPU_TEXTURE_3D:
844 mtl_color_attachments_[slot].slice = 0;
845 mtl_color_attachments_[slot].depth_plane = layer;
846 if (layer == -1) {
847 mtl_color_attachments_[slot].depth_plane = 0;
848 mtl_color_attachments_[slot].render_target_array_length = texture->d_;
849 use_multilayered_rendering_ = true;
850 }
851 break;
852 case GPU_TEXTURE_CUBE:
853 BLI_assert(layer < 6);
854 mtl_color_attachments_[slot].slice = layer;
855 mtl_color_attachments_[slot].depth_plane = 0;
856 if (layer == -1) {
857 mtl_color_attachments_[slot].slice = 0;
858 mtl_color_attachments_[slot].depth_plane = 0;
859 mtl_color_attachments_[slot].render_target_array_length = 6;
860 use_multilayered_rendering_ = true;
861 }
862 break;
865 /* TODO(Metal): Verify multilayered rendering for Cube arrays. */
866 mtl_color_attachments_[slot].slice = layer;
867 mtl_color_attachments_[slot].depth_plane = 0;
868 if (layer == -1) {
869 mtl_color_attachments_[slot].slice = 0;
870 mtl_color_attachments_[slot].depth_plane = 0;
871 mtl_color_attachments_[slot].render_target_array_length = texture->d_;
872 use_multilayered_rendering_ = true;
873 }
874 break;
876 mtl_color_attachments_[slot].slice = 0;
877 mtl_color_attachments_[slot].depth_plane = 0;
878 break;
879 default:
880 MTL_LOG_ERROR("MTLFrameBuffer::add_color_attachment Unrecognized texture type %u",
881 texture->type_);
882 break;
883 }
884
885 /* Update attachment size and ensure future attachments match the same size. */
886 int width_of_miplayer, height_of_miplayer;
887 if (miplevel <= 0) {
888 width_of_miplayer = texture->width_get();
889 height_of_miplayer = texture->height_get();
890 }
891 else {
892 width_of_miplayer = max_ii(texture->width_get() >> miplevel, 1);
893 height_of_miplayer = max_ii(texture->height_get() >> miplevel, 1);
894 }
895
896 if (attachment_width_ == 0 || attachment_height_ == 0) {
897 this->size_set(width_of_miplayer, height_of_miplayer);
898 this->attachment_size_set(width_of_miplayer, height_of_miplayer);
899 BLI_assert(attachment_width_ > 0);
900 BLI_assert(attachment_height_ > 0);
901 }
902 else {
903 BLI_assert(attachment_width_ == width_of_miplayer);
904 BLI_assert(attachment_height_ == height_of_miplayer);
905 }
906
907 /* Flag as dirty. */
908 this->mark_dirty();
909 }
910 else {
912 "Passing in null texture to MTLFrameBuffer::add_color_attachment (This could be due to "
913 "not all texture types being supported).");
914 }
915 return true;
916}
917
919{
920 BLI_assert(this);
921
922 if (texture) {
923 if (miplevel < 0 || miplevel >= MTL_MAX_MIPMAP_COUNT) {
924 MTL_LOG_WARNING("Attachment specified with invalid mip level %u", miplevel);
925 miplevel = 0;
926 }
927
928 /* Assume attachment load by default. */
929 mtl_depth_attachment_.used = true;
930 mtl_depth_attachment_.texture = texture;
931 mtl_depth_attachment_.mip = miplevel;
932 mtl_depth_attachment_.load_action = GPU_LOADACTION_LOAD;
933 mtl_depth_attachment_.store_action = GPU_STOREACTION_STORE;
934 mtl_depth_attachment_.render_target_array_length = 0;
935
936 /* Determine whether array slice or depth plane based on texture type. */
937 switch (texture->type_) {
938 case GPU_TEXTURE_1D:
939 case GPU_TEXTURE_2D:
940 BLI_assert(layer <= 0);
941 mtl_depth_attachment_.slice = 0;
942 mtl_depth_attachment_.depth_plane = 0;
943 break;
945 if (layer < 0) {
946 layer = 0;
947 MTL_LOG_WARNING("TODO: Support layered rendering for 1D array textures, if needed");
948 }
950 mtl_depth_attachment_.slice = layer;
951 mtl_depth_attachment_.depth_plane = 0;
952 break;
955 mtl_depth_attachment_.slice = layer;
956 mtl_depth_attachment_.depth_plane = 0;
957 if (layer == -1) {
958 mtl_depth_attachment_.slice = 0;
959 mtl_depth_attachment_.render_target_array_length = texture->d_;
960 use_multilayered_rendering_ = true;
961 }
962 break;
963 case GPU_TEXTURE_3D:
965 mtl_depth_attachment_.slice = 0;
966 mtl_depth_attachment_.depth_plane = layer;
967 if (layer == -1) {
968 mtl_depth_attachment_.depth_plane = 0;
969 mtl_depth_attachment_.render_target_array_length = texture->d_;
970 use_multilayered_rendering_ = true;
971 }
972 break;
973 case GPU_TEXTURE_CUBE:
974 BLI_assert(layer < 6);
975 mtl_depth_attachment_.slice = layer;
976 mtl_depth_attachment_.depth_plane = 0;
977 if (layer == -1) {
978 mtl_depth_attachment_.slice = 0;
979 mtl_depth_attachment_.depth_plane = 0;
980 mtl_depth_attachment_.render_target_array_length = 6;
981 use_multilayered_rendering_ = true;
982 }
983 break;
985 /* TODO(Metal): Verify multilayered rendering for Cube arrays. */
987 mtl_depth_attachment_.slice = layer;
988 mtl_depth_attachment_.depth_plane = 0;
989 if (layer == -1) {
990 mtl_depth_attachment_.slice = 0;
991 mtl_depth_attachment_.depth_plane = 0;
992 mtl_depth_attachment_.render_target_array_length = texture->d_;
993 use_multilayered_rendering_ = true;
994 }
995 break;
997 mtl_depth_attachment_.slice = 0;
998 mtl_depth_attachment_.depth_plane = 0;
999 break;
1000 default:
1001 BLI_assert_msg(false, "Unrecognized texture type");
1002 break;
1003 }
1004
1005 /* Update attachment size and ensure future attachments match the same size. */
1006 int width_of_miplayer, height_of_miplayer;
1007 if (miplevel <= 0) {
1008 width_of_miplayer = texture->width_get();
1009 height_of_miplayer = texture->height_get();
1010 }
1011 else {
1012 width_of_miplayer = max_ii(texture->width_get() >> miplevel, 1);
1013 height_of_miplayer = max_ii(texture->height_get() >> miplevel, 1);
1014 }
1015
1016 if (attachment_width_ == 0 || attachment_height_ == 0) {
1017 this->size_set(width_of_miplayer, height_of_miplayer);
1018 this->attachment_size_set(width_of_miplayer, height_of_miplayer);
1019 BLI_assert(attachment_width_ > 0);
1020 BLI_assert(attachment_height_ > 0);
1021 }
1022 else {
1023 BLI_assert(attachment_width_ == width_of_miplayer);
1024 BLI_assert(attachment_height_ == height_of_miplayer);
1025 }
1026
1027 /* Flag as dirty after attachments changed. */
1028 this->mark_dirty();
1029 }
1030 else {
1032 "Passing in null texture to MTLFrameBuffer::addDepthAttachment (This could be due to not "
1033 "all texture types being supported).");
1034 }
1035 return true;
1036}
1037
1039{
1040 BLI_assert(this);
1041
1042 if (texture) {
1043 if (miplevel < 0 || miplevel >= MTL_MAX_MIPMAP_COUNT) {
1044 MTL_LOG_WARNING("Attachment specified with invalid mip level %u", miplevel);
1045 miplevel = 0;
1046 }
1047
1048 /* Assume attachment load by default. */
1049 mtl_stencil_attachment_.used = true;
1050 mtl_stencil_attachment_.texture = texture;
1051 mtl_stencil_attachment_.mip = miplevel;
1052 mtl_stencil_attachment_.load_action = GPU_LOADACTION_LOAD;
1053 mtl_stencil_attachment_.store_action = GPU_STOREACTION_STORE;
1054 mtl_stencil_attachment_.render_target_array_length = 0;
1055
1056 /* Determine whether array slice or depth plane based on texture type. */
1057 switch (texture->type_) {
1058 case GPU_TEXTURE_1D:
1059 case GPU_TEXTURE_2D:
1060 BLI_assert(layer <= 0);
1061 mtl_stencil_attachment_.slice = 0;
1062 mtl_stencil_attachment_.depth_plane = 0;
1063 break;
1065 if (layer < 0) {
1066 layer = 0;
1067 MTL_LOG_WARNING("TODO: Support layered rendering for 1D array textures, if needed");
1068 }
1070 mtl_stencil_attachment_.slice = layer;
1071 mtl_stencil_attachment_.depth_plane = 0;
1072 break;
1075 mtl_stencil_attachment_.slice = layer;
1076 mtl_stencil_attachment_.depth_plane = 0;
1077 if (layer == -1) {
1078 mtl_stencil_attachment_.slice = 0;
1079 mtl_stencil_attachment_.render_target_array_length = texture->d_;
1080 use_multilayered_rendering_ = true;
1081 }
1082 break;
1083 case GPU_TEXTURE_3D:
1085 mtl_stencil_attachment_.slice = 0;
1086 mtl_stencil_attachment_.depth_plane = layer;
1087 if (layer == -1) {
1088 mtl_stencil_attachment_.depth_plane = 0;
1089 mtl_stencil_attachment_.render_target_array_length = texture->d_;
1090 use_multilayered_rendering_ = true;
1091 }
1092 break;
1093 case GPU_TEXTURE_CUBE:
1094 BLI_assert(layer < 6);
1095 mtl_stencil_attachment_.slice = layer;
1096 mtl_stencil_attachment_.depth_plane = 0;
1097 if (layer == -1) {
1098 mtl_stencil_attachment_.slice = 0;
1099 mtl_stencil_attachment_.depth_plane = 0;
1100 mtl_stencil_attachment_.render_target_array_length = 6;
1101 use_multilayered_rendering_ = true;
1102 }
1103 break;
1105 /* TODO(Metal): Verify multilayered rendering for Cube arrays. */
1107 mtl_stencil_attachment_.slice = layer;
1108 mtl_stencil_attachment_.depth_plane = 0;
1109 if (layer == -1) {
1110 mtl_stencil_attachment_.slice = 0;
1111 mtl_stencil_attachment_.depth_plane = 0;
1112 mtl_stencil_attachment_.render_target_array_length = texture->d_;
1113 use_multilayered_rendering_ = true;
1114 }
1115 break;
1116 case GPU_TEXTURE_BUFFER:
1117 mtl_stencil_attachment_.slice = 0;
1118 mtl_stencil_attachment_.depth_plane = 0;
1119 break;
1120 default:
1121 BLI_assert_msg(false, "Unrecognized texture type");
1122 break;
1123 }
1124
1125 /* Update attachment size and ensure future attachments match the same size. */
1126 int width_of_miplayer, height_of_miplayer;
1127 if (miplevel <= 0) {
1128 width_of_miplayer = texture->width_get();
1129 height_of_miplayer = texture->height_get();
1130 }
1131 else {
1132 width_of_miplayer = max_ii(texture->width_get() >> miplevel, 1);
1133 height_of_miplayer = max_ii(texture->height_get() >> miplevel, 1);
1134 }
1135
1136 if (attachment_width_ == 0 || attachment_height_ == 0) {
1137 this->size_set(width_of_miplayer, height_of_miplayer);
1138 this->attachment_size_set(width_of_miplayer, height_of_miplayer);
1139 BLI_assert(attachment_width_ > 0);
1140 BLI_assert(attachment_height_ > 0);
1141 }
1142 else {
1143 BLI_assert(attachment_width_ == width_of_miplayer);
1144 BLI_assert(attachment_height_ == height_of_miplayer);
1145 }
1146
1147 /* Flag as dirty after attachments changed. */
1148 this->mark_dirty();
1149 }
1150 else {
1152 "Passing in null texture to MTLFrameBuffer::addStencilAttachment (This could be due to "
1153 "not all texture types being supported).");
1154 }
1155 return true;
1156}
1157
1159{
1160 BLI_assert(this);
1161 BLI_assert(slot >= 0 && slot < this->get_attachment_limit());
1163
1164 if (this->has_attachment_at_slot(slot)) {
1165 colour_attachment_count_ -= (mtl_color_attachments_[slot].used) ? 1 : 0;
1166 mtl_color_attachments_[slot].used = false;
1168 this->mark_dirty();
1169 return true;
1170 }
1171
1172 return false;
1173}
1174
1176{
1177 BLI_assert(this);
1178
1179 mtl_depth_attachment_.used = false;
1180 mtl_depth_attachment_.texture = nullptr;
1182 this->mark_dirty();
1183
1184 return true;
1185}
1186
1188{
1189 BLI_assert(this);
1190
1191 mtl_stencil_attachment_.used = false;
1192 mtl_stencil_attachment_.texture = nullptr;
1194 this->mark_dirty();
1195
1196 return true;
1197}
1198
1200{
1201 BLI_assert(this);
1202
1203 for (int attachment = 0; attachment < GPU_FB_MAX_COLOR_ATTACHMENT; attachment++) {
1204 this->remove_color_attachment(attachment);
1205 }
1208 colour_attachment_count_ = 0;
1209 this->mark_dirty();
1210
1211 /* Verify height. */
1213
1214 /* Flag attachments as no longer being dirty. */
1215 dirty_attachments_ = false;
1216}
1217
1219{
1220 /* If we have no attachments, reset width and height to zero. */
1221 if (colour_attachment_count_ == 0 && !this->has_depth_attachment() &&
1222 !this->has_stencil_attachment())
1223 {
1224 /* Reset attachment size for empty framebuffer, but preserving the size of the framebuffer
1225 * (default_size) that is used for scissor testing. */
1226 this->attachment_size_set(0, 0);
1227 }
1228}
1229
1231
1232/* -------------------------------------------------------------------- */
1235
1237{
1238 if (type >= GPU_FB_COLOR_ATTACHMENT0) {
1239 int slot = type - GPU_FB_COLOR_ATTACHMENT0;
1242 }
1244 }
1245 else if (type == GPU_FB_DEPTH_STENCIL_ATTACHMENT) {
1248 }
1251 }
1252 else if (type == GPU_FB_DEPTH_ATTACHMENT) {
1255 }
1257 }
1258}
1259
1261{
1262 BLI_assert(this);
1263 BLI_assert(slot >= 0 && slot < this->get_attachment_limit());
1264
1265 /* Only mark as dirty if values have changed. */
1266 bool changed = mtl_color_attachments_[slot].load_action != GPU_LOADACTION_CLEAR;
1267 changed |= assign_if_different(mtl_color_attachments_[slot].clear_value, clear_color);
1268 mtl_color_attachments_[slot].load_action = GPU_LOADACTION_CLEAR;
1269
1270 if (changed) {
1271 this->mark_loadstore_dirty();
1272 }
1273 return true;
1274}
1275
1277{
1278 BLI_assert(this);
1279
1280 if (mtl_depth_attachment_.clear_value.x != depth_clear ||
1281 mtl_depth_attachment_.load_action != GPU_LOADACTION_CLEAR)
1282 {
1283 mtl_depth_attachment_.clear_value.x = depth_clear;
1284 mtl_depth_attachment_.load_action = GPU_LOADACTION_CLEAR;
1285 this->mark_loadstore_dirty();
1286 }
1287 return true;
1288}
1289
1291{
1292 BLI_assert(this);
1293
1294 if (mtl_stencil_attachment_.clear_value.x != stencil_clear ||
1295 mtl_stencil_attachment_.load_action != GPU_LOADACTION_CLEAR)
1296 {
1297 mtl_stencil_attachment_.clear_value.x = stencil_clear;
1298 mtl_stencil_attachment_.load_action = GPU_LOADACTION_CLEAR;
1299 this->mark_loadstore_dirty();
1300 }
1301 return true;
1302}
1303
1305 GPULoadOp load_action,
1306 GPUStoreOp store_action)
1307{
1308 BLI_assert(this);
1309 GPULoadOp prev_load_action = mtl_color_attachments_[slot].load_action;
1310 GPUStoreOp prev_store_action = mtl_color_attachments_[slot].store_action;
1311 mtl_color_attachments_[slot].load_action = load_action;
1312 mtl_color_attachments_[slot].store_action = store_action;
1313
1314 bool changed = (mtl_color_attachments_[slot].load_action != prev_load_action ||
1315 mtl_color_attachments_[slot].store_action != prev_store_action);
1316 if (changed) {
1317 this->mark_loadstore_dirty();
1318 }
1319
1320 return changed;
1321}
1322
1324{
1325 BLI_assert(this);
1326 GPULoadOp prev_load_action = mtl_depth_attachment_.load_action;
1327 GPUStoreOp prev_store_action = mtl_depth_attachment_.store_action;
1328 mtl_depth_attachment_.load_action = load_action;
1329 mtl_depth_attachment_.store_action = store_action;
1330
1331 bool changed = (mtl_depth_attachment_.load_action != prev_load_action ||
1332 mtl_depth_attachment_.store_action != prev_store_action);
1333 if (changed) {
1334 this->mark_loadstore_dirty();
1335 }
1336
1337 return changed;
1338}
1339
1341{
1342 BLI_assert(this);
1343 GPULoadOp prev_load_action = mtl_stencil_attachment_.load_action;
1344 GPUStoreOp prev_store_action = mtl_stencil_attachment_.store_action;
1345 mtl_stencil_attachment_.load_action = load_action;
1346 mtl_stencil_attachment_.store_action = store_action;
1347
1348 bool changed = (mtl_stencil_attachment_.load_action != prev_load_action ||
1349 mtl_stencil_attachment_.store_action != prev_store_action);
1350 if (changed) {
1351 this->mark_loadstore_dirty();
1352 }
1353
1354 return changed;
1355}
1356
1358{
1359 for (int slot = 0; slot < colour_attachment_count_; slot++) {
1361 }
1364 return true;
1365}
1366
1368
1369/* -------------------------------------------------------------------- */
1372
1374{
1375 BLI_assert(this);
1376
1377 if (slot >= 0 && slot < this->get_attachment_limit()) {
1378 return mtl_color_attachments_[slot].used;
1379 }
1380 return false;
1381}
1382
1384{
1385 BLI_assert(this);
1386
1387 for (int attachment = 0; attachment < this->get_attachment_limit(); attachment++) {
1388 if (mtl_color_attachments_[attachment].used &&
1389 mtl_color_attachments_[attachment].texture == texture)
1390 {
1391 return true;
1392 }
1393 }
1394 return false;
1395}
1396
1398{
1399 BLI_assert(this);
1400 return mtl_depth_attachment_.used;
1401}
1402
1404{
1405 BLI_assert(this);
1406 return mtl_stencil_attachment_.used;
1407}
1408
1410{
1411 BLI_assert(this);
1413
1414 for (int attachment = 0; attachment < this->get_attachment_limit(); attachment++) {
1415 if (mtl_color_attachments_[attachment].used &&
1416 (mtl_color_attachments_[attachment].texture == texture))
1417 {
1418 return attachment;
1419 }
1420 }
1421 return -1;
1422}
1423
1425{
1426 BLI_assert(this);
1427 return colour_attachment_count_;
1428}
1429
1431{
1432 BLI_assert(this);
1433 if (slot >= 0 && slot < GPU_FB_MAX_COLOR_ATTACHMENT) {
1434 return mtl_color_attachments_[slot];
1435 }
1436 MTLAttachment null_attachment;
1437 null_attachment.used = false;
1438 return null_attachment;
1439}
1440
1442{
1443 BLI_assert(this);
1444 return mtl_depth_attachment_;
1445}
1446
1448{
1449 BLI_assert(this);
1450 return mtl_stencil_attachment_;
1451}
1452
1454
1455/* -------------------------------------------------------------------- */
1459{
1460 BLI_assert(this);
1461
1462 /* First update attachments if dirty. */
1463 if (dirty_attachments_) {
1464 this->update_attachments(true);
1465 }
1466
1467 /* NOTE: Attachment-less render targets now supported so we do not need to validate attachment
1468 * counts. Keeping this function in place if other parameters need to be validate. */
1469 return true;
1470}
1471
1473{
1474 return (action == GPU_LOADACTION_LOAD) ?
1475 MTLLoadActionLoad :
1476 ((action == GPU_LOADACTION_CLEAR) ? MTLLoadActionClear : MTLLoadActionDontCare);
1477}
1478
1480{
1481 return (action == GPU_STOREACTION_STORE) ? MTLStoreActionStore : MTLStoreActionDontCare;
1482}
1483
1484MTLRenderPassDescriptor *MTLFrameBuffer::bake_render_pass_descriptor(bool load_contents)
1485{
1486 BLI_assert(this);
1487 if (load_contents) {
1488 /* Only force-load contents if there is no clear pending. */
1489 BLI_assert(!has_pending_clear_);
1490 }
1491
1492 /* Ensure we are inside a frame boundary. */
1493 MTLContext *metal_ctx = MTLContext::get();
1494 BLI_assert(metal_ctx && metal_ctx->get_inside_frame());
1495 UNUSED_VARS_NDEBUG(metal_ctx);
1496
1497 /* If Frame-buffer has been modified, regenerate descriptor. */
1498 if (is_dirty_) {
1499 /* Clear all configurations. */
1500 for (int config = 0; config < 3; config++) {
1501 descriptor_dirty_[config] = true;
1502 }
1503 }
1504 else if (is_loadstore_dirty_) {
1505 /* Load config always has load ops, so we only need to re-generate custom and clear state. */
1506 descriptor_dirty_[MTL_FB_CONFIG_CLEAR] = true;
1507 descriptor_dirty_[MTL_FB_CONFIG_CUSTOM] = true;
1508 }
1509
1510 /* If we need to populate descriptor */
1511 /* Select config based on FrameBuffer state:
1512 * [0] {MTL_FB_CONFIG_CLEAR} = Clear config -- we have a pending clear so should perform our
1513 * configured clear.
1514 * [1] {MTL_FB_CONFIG_LOAD} = Load config -- We need to re-load ALL attachments,
1515 * used for re-binding/pass-breaks.
1516 * [2] {MTL_FB_CONFIG_CUSTOM} = Custom config -- Use this when a custom binding config is
1517 * specified.
1518 */
1519 uint descriptor_config = (load_contents) ? MTL_FB_CONFIG_LOAD :
1520 ((this->get_pending_clear()) ? MTL_FB_CONFIG_CLEAR :
1521 MTL_FB_CONFIG_CUSTOM);
1522
1523 /* NOTE: If `GPU_framebuffer_bind_loadstore` is used, the `use_explicit_load_store_` flag will be
1524 * set in which case, we should always use the custom configuration. Calls with this flag will
1525 * only happen for the first render pass after binding. */
1527 descriptor_config = MTL_FB_CONFIG_CUSTOM;
1528 descriptor_dirty_[descriptor_config] = true;
1529 }
1530
1531 if (descriptor_dirty_[descriptor_config] || framebuffer_descriptor_[descriptor_config] == nil) {
1532
1533 /* Create descriptor if it does not exist. */
1534 if (framebuffer_descriptor_[descriptor_config] == nil) {
1535 framebuffer_descriptor_[descriptor_config] = [[MTLRenderPassDescriptor alloc] init];
1536 }
1537
1538 /* Configure multilayered rendering. */
1539 if (use_multilayered_rendering_) {
1540 /* Ensure all targets have the same length. */
1541 int len = 0;
1542 bool valid = true;
1543
1544 for (int attachment_ind = 0; attachment_ind < GPU_FB_MAX_COLOR_ATTACHMENT; attachment_ind++)
1545 {
1546 if (mtl_color_attachments_[attachment_ind].used) {
1547 if (len == 0) {
1548 len = mtl_color_attachments_[attachment_ind].render_target_array_length;
1549 }
1550 else {
1551 valid = valid &&
1552 (len == mtl_color_attachments_[attachment_ind].render_target_array_length);
1553 }
1554 }
1555 }
1556
1557 if (mtl_depth_attachment_.used) {
1558 if (len == 0) {
1559 len = mtl_depth_attachment_.render_target_array_length;
1560 }
1561 else {
1562 valid = valid && (len == mtl_depth_attachment_.render_target_array_length);
1563 }
1564 }
1565
1566 if (mtl_stencil_attachment_.used) {
1567 if (len == 0) {
1568 len = mtl_stencil_attachment_.render_target_array_length;
1569 }
1570 else {
1571 valid = valid && (len == mtl_stencil_attachment_.render_target_array_length);
1572 }
1573 }
1574
1575 BLI_assert(len > 0);
1576 BLI_assert(valid);
1577 framebuffer_descriptor_[descriptor_config].renderTargetArrayLength = len;
1578 }
1579 else {
1580 framebuffer_descriptor_[descriptor_config].renderTargetArrayLength = 0;
1581 }
1582
1583 /* Color attachments. */
1584 int colour_attachments = 0;
1585 for (int attachment_ind = 0; attachment_ind < GPU_FB_MAX_COLOR_ATTACHMENT; attachment_ind++) {
1586 MTLAttachment &attachment_config = mtl_color_attachments_[attachment_ind];
1587
1588 if (attachment_config.used) {
1589 id<MTLTexture> texture = attachment_config.texture->get_metal_handle_base();
1590 if (texture == nil) {
1591 MTL_LOG_ERROR("Attempting to assign invalid texture as attachment");
1592 }
1593
1594 bool texture_is_memoryless = (attachment_config.texture->usage_get() &
1596
1597 /* IF SRGB is enabled, but we are rendering with SRGB disabled, sample texture view. */
1598 id<MTLTexture> source_color_texture = texture;
1599 if (this->get_is_srgb() && attachment_config.texture->is_format_srgb() &&
1600 !this->get_srgb_enabled())
1601 {
1602 source_color_texture = attachment_config.texture->get_non_srgb_handle();
1603 BLI_assert(source_color_texture != nil);
1604 }
1605
1606 /* Resolve appropriate load action -- IF force load, perform load.
1607 * If clear but framebuffer has no pending clear, also load. */
1608 GPULoadOp load_action = attachment_config.load_action;
1609 if (descriptor_config == MTL_FB_CONFIG_LOAD) {
1610 /* MTL_FB_CONFIG_LOAD must always load. */
1611 load_action = GPU_LOADACTION_LOAD;
1612 }
1613 else if (descriptor_config == MTL_FB_CONFIG_CUSTOM && load_action == GPU_LOADACTION_CLEAR)
1614 {
1615 /* If Custom load config is used, fallback to loading if explicit bind state flag is
1616 * unset. This is to ensure attachments are loaded by default in the case a framebuffer
1617 * is unbound and rebound, or, if a render pass breaks mid-pass for compute or blit
1618 * operations. */
1619 if (!use_explicit_load_store_ && !texture_is_memoryless) {
1620 load_action = GPU_LOADACTION_LOAD;
1621 }
1622 }
1623
1624 /* Ensure memoryless attachment cannot load or store results. */
1625 GPUStoreOp store_action = attachment_config.store_action;
1626 if (texture_is_memoryless && load_action == GPU_LOADACTION_LOAD) {
1627 load_action = GPU_LOADACTION_DONT_CARE;
1628 }
1629 if (texture_is_memoryless && store_action == GPU_STOREACTION_STORE) {
1630 store_action = GPU_STOREACTION_DONT_CARE;
1631 }
1632
1633 /* Create attachment descriptor. */
1634 MTLRenderPassColorAttachmentDescriptor *attachment =
1635 colour_attachment_descriptors_[attachment_ind];
1636 BLI_assert(attachment != nil);
1637
1638 attachment.texture = source_color_texture;
1639 attachment.loadAction = mtl_load_action_from_gpu(load_action);
1640 attachment.clearColor = (load_action == GPU_LOADACTION_CLEAR) ?
1641 MTLClearColorMake(UNPACK4(attachment_config.clear_value)) :
1642 MTLClearColorMake(0.0, 0.0, 0.0, 0.0);
1643 attachment.storeAction = mtl_store_action_from_gpu(store_action);
1644 attachment.level = attachment_config.mip;
1645 attachment.slice = attachment_config.slice;
1646 attachment.depthPlane = attachment_config.depth_plane;
1647 colour_attachments++;
1648
1649 /* Copy attachment info back in. */
1650 [framebuffer_descriptor_[descriptor_config].colorAttachments setObject:attachment
1651 atIndexedSubscript:attachment_ind];
1652 }
1653 else {
1654 /* Disable color attachment. */
1655 [framebuffer_descriptor_[descriptor_config].colorAttachments setObject:nil
1656 atIndexedSubscript:attachment_ind];
1657 }
1658 }
1659 BLI_assert(colour_attachments == colour_attachment_count_);
1660 UNUSED_VARS_NDEBUG(colour_attachments);
1661
1662 /* Depth attachment. */
1663 if (mtl_depth_attachment_.used) {
1664 framebuffer_descriptor_[descriptor_config].depthAttachment.texture =
1665 (id<MTLTexture>)mtl_depth_attachment_.texture->get_metal_handle_base();
1666
1667 bool texture_is_memoryless = (mtl_depth_attachment_.texture->usage_get() &
1669
1670 /* Resolve appropriate load action -- IF force load, perform load.
1671 * If clear but framebuffer has no pending clear, also load. */
1672 GPULoadOp load_action = mtl_depth_attachment_.load_action;
1673 if (descriptor_config == MTL_FB_CONFIG_LOAD) {
1674 /* MTL_FB_CONFIG_LOAD must always load. */
1675 load_action = GPU_LOADACTION_LOAD;
1676 }
1677 else if (descriptor_config == MTL_FB_CONFIG_CUSTOM && load_action == GPU_LOADACTION_CLEAR) {
1678 /* If Custom load config is used, fallback to loading if explicit bind state flag is unset.
1679 * This is to ensure attachments are loaded by default in the case a framebuffer is unbound
1680 * and rebound, or, if a render pass breaks mid-pass for compute or blit operations. */
1681 if (!use_explicit_load_store_ && !texture_is_memoryless) {
1682 load_action = GPU_LOADACTION_LOAD;
1683 }
1684 }
1685
1686 /* Ensure memoryless attachment cannot load or store results. */
1687 GPUStoreOp store_action = mtl_depth_attachment_.store_action;
1688 if (texture_is_memoryless && load_action == GPU_LOADACTION_LOAD) {
1689 load_action = GPU_LOADACTION_DONT_CARE;
1690 }
1691 if (texture_is_memoryless && store_action == GPU_STOREACTION_STORE) {
1692 store_action = GPU_STOREACTION_DONT_CARE;
1693 }
1694
1695 framebuffer_descriptor_[descriptor_config].depthAttachment.loadAction =
1696 mtl_load_action_from_gpu(load_action);
1697 framebuffer_descriptor_[descriptor_config].depthAttachment.clearDepth =
1698 (load_action == GPU_LOADACTION_CLEAR) ? mtl_depth_attachment_.clear_value.x : 0;
1699 framebuffer_descriptor_[descriptor_config].depthAttachment.storeAction =
1700 mtl_store_action_from_gpu(store_action);
1701 framebuffer_descriptor_[descriptor_config].depthAttachment.level = mtl_depth_attachment_.mip;
1702 framebuffer_descriptor_[descriptor_config].depthAttachment.slice =
1703 mtl_depth_attachment_.slice;
1704 framebuffer_descriptor_[descriptor_config].depthAttachment.depthPlane =
1705 mtl_depth_attachment_.depth_plane;
1706 }
1707 else {
1708 framebuffer_descriptor_[descriptor_config].depthAttachment.texture = nil;
1709 }
1710
1711 /* Stencil attachment. */
1712 if (mtl_stencil_attachment_.used) {
1713 framebuffer_descriptor_[descriptor_config].stencilAttachment.texture =
1714 (id<MTLTexture>)mtl_stencil_attachment_.texture->get_metal_handle_base();
1715
1716 bool texture_is_memoryless = (mtl_stencil_attachment_.texture->usage_get() &
1718
1719 /* Resolve appropriate load action -- IF force load, perform load.
1720 * If clear but framebuffer has no pending clear, also load. */
1721 GPULoadOp load_action = mtl_stencil_attachment_.load_action;
1722 if (descriptor_config == MTL_FB_CONFIG_LOAD) {
1723 /* MTL_FB_CONFIG_LOAD must always load. */
1724 load_action = GPU_LOADACTION_LOAD;
1725 }
1726 else if (descriptor_config == MTL_FB_CONFIG_CUSTOM && load_action == GPU_LOADACTION_CLEAR) {
1727 /* If Custom load config is used, fallback to loading if explicit bind state flag is unset.
1728 * This is to ensure attachments are loaded by default in the case a framebuffer is unbound
1729 * and rebound, or, if a render pass breaks mid-pass for compute or blit operations. */
1730 if (!use_explicit_load_store_ && !texture_is_memoryless) {
1731 load_action = GPU_LOADACTION_LOAD;
1732 }
1733 }
1734
1735 /* Ensure memoryless attachment cannot load or store results. */
1736 GPUStoreOp store_action = mtl_stencil_attachment_.store_action;
1737 if (texture_is_memoryless && load_action == GPU_LOADACTION_LOAD) {
1738 load_action = GPU_LOADACTION_DONT_CARE;
1739 }
1740 if (texture_is_memoryless && store_action == GPU_STOREACTION_STORE) {
1741 store_action = GPU_STOREACTION_DONT_CARE;
1742 }
1743
1744 framebuffer_descriptor_[descriptor_config].stencilAttachment.loadAction =
1745 mtl_load_action_from_gpu(load_action);
1746 framebuffer_descriptor_[descriptor_config].stencilAttachment.clearStencil =
1747 (load_action == GPU_LOADACTION_CLEAR) ? mtl_stencil_attachment_.clear_value.x : 0;
1748 framebuffer_descriptor_[descriptor_config].stencilAttachment.storeAction =
1749 mtl_store_action_from_gpu(store_action);
1750 framebuffer_descriptor_[descriptor_config].stencilAttachment.level =
1751 mtl_stencil_attachment_.mip;
1752 framebuffer_descriptor_[descriptor_config].stencilAttachment.slice =
1753 mtl_stencil_attachment_.slice;
1754 framebuffer_descriptor_[descriptor_config].stencilAttachment.depthPlane =
1755 mtl_stencil_attachment_.depth_plane;
1756 }
1757 else {
1758 framebuffer_descriptor_[descriptor_config].stencilAttachment.texture = nil;
1759 }
1760
1761 /* Attachmentless render support. */
1762 int total_num_attachments = colour_attachment_count_ + (mtl_depth_attachment_.used ? 1 : 0) +
1763 (mtl_stencil_attachment_.used ? 1 : 0);
1764 if (total_num_attachments == 0) {
1765 BLI_assert(width_ > 0 && height_ > 0);
1766 framebuffer_descriptor_[descriptor_config].renderTargetWidth = width_;
1767 framebuffer_descriptor_[descriptor_config].renderTargetHeight = height_;
1768 framebuffer_descriptor_[descriptor_config].defaultRasterSampleCount = 1;
1769 }
1770
1771 descriptor_dirty_[descriptor_config] = false;
1772 }
1773 /* Clear dirty state flags. */
1774 is_dirty_ = false;
1775 is_loadstore_dirty_ = false;
1776
1777 /* Clear explicit bind flag. */
1779
1780 return framebuffer_descriptor_[descriptor_config];
1781}
1782
1784
1785/* -------------------------------------------------------------------- */
1788
1790 uint src_x_offset,
1791 uint src_y_offset,
1792 MTLFrameBuffer *metal_fb_write,
1793 uint write_slot,
1794 uint dst_x_offset,
1795 uint dst_y_offset,
1796 uint width,
1797 uint height,
1798 GPUFrameBufferBits blit_buffers)
1799{
1800 BLI_assert(metal_fb_write);
1801 if (!metal_fb_write) {
1802 return;
1803 }
1804 MTLContext *mtl_context = MTLContext::get();
1805
1806 const bool do_color = (blit_buffers & GPU_COLOR_BIT);
1807 const bool do_depth = (blit_buffers & GPU_DEPTH_BIT);
1808 const bool do_stencil = (blit_buffers & GPU_STENCIL_BIT);
1809
1810 /* Early exit if there is no blit to do. */
1811 if (!(do_color || do_depth || do_stencil)) {
1812 MTL_LOG_WARNING("FrameBuffer: requested blit but no color, depth or stencil flag was set");
1813 return;
1814 }
1815
1816 id<MTLBlitCommandEncoder> blit_encoder = nil;
1817
1818 /* If the color format is not the same, we cannot use the BlitCommandEncoder, and instead use
1819 * a Graphics-based blit. */
1820 if (do_color && (this->get_color_attachment(read_slot).texture->format_get() !=
1821 metal_fb_write->get_color_attachment(read_slot).texture->format_get()))
1822 {
1823
1824 MTLAttachment src_attachment = this->get_color_attachment(read_slot);
1825 MTLAttachment dst_attachment = metal_fb_write->get_color_attachment(write_slot);
1826 assert(src_attachment.slice == 0 &&
1827 "currently only supporting slice 0 for graphics framebuffer blit");
1828
1829 src_attachment.texture->blit(dst_attachment.texture,
1830 src_x_offset,
1831 src_y_offset,
1832 dst_x_offset,
1833 dst_y_offset,
1834 src_attachment.mip,
1835 dst_attachment.mip,
1836 dst_attachment.slice,
1837 width,
1838 height);
1839 }
1840 else {
1841
1842 /* Setup blit encoder. */
1843 blit_encoder = mtl_context->main_command_buffer.ensure_begin_blit_encoder();
1844
1845 if (do_color) {
1846 MTLAttachment src_attachment = this->get_color_attachment(read_slot);
1847 MTLAttachment dst_attachment = metal_fb_write->get_color_attachment(write_slot);
1848
1849 if (src_attachment.used && dst_attachment.used) {
1850
1851 /* TODO(Metal): Support depth(z) offset in blit if needed. */
1852 src_attachment.texture->blit(blit_encoder,
1853 src_x_offset,
1854 src_y_offset,
1855 0,
1856 src_attachment.slice,
1857 src_attachment.mip,
1858 dst_attachment.texture,
1859 dst_x_offset,
1860 dst_y_offset,
1861 0,
1862 dst_attachment.slice,
1863 dst_attachment.mip,
1864 width,
1865 height,
1866 1);
1867 }
1868 else {
1869 MTL_LOG_ERROR("Failed performing colour blit");
1870 }
1871 }
1872 }
1873 if ((do_depth || do_stencil) && blit_encoder == nil) {
1874 blit_encoder = mtl_context->main_command_buffer.ensure_begin_blit_encoder();
1875 }
1876
1877 if (do_depth) {
1878 MTLAttachment src_attachment = this->get_depth_attachment();
1879 MTLAttachment dst_attachment = metal_fb_write->get_depth_attachment();
1880
1881 if (src_attachment.used && dst_attachment.used) {
1882
1883 /* TODO(Metal): Support depth(z) offset in blit if needed. */
1884 src_attachment.texture->blit(blit_encoder,
1885 src_x_offset,
1886 src_y_offset,
1887 0,
1888 src_attachment.slice,
1889 src_attachment.mip,
1890 dst_attachment.texture,
1891 dst_x_offset,
1892 dst_y_offset,
1893 0,
1894 dst_attachment.slice,
1895 dst_attachment.mip,
1896 width,
1897 height,
1898 1);
1899 }
1900 else {
1901 MTL_LOG_ERROR("Failed performing depth blit");
1902 }
1903 }
1904
1905 /* Stencil attachment blit. */
1906 if (do_stencil) {
1907 MTLAttachment src_attachment = this->get_stencil_attachment();
1908 MTLAttachment dst_attachment = metal_fb_write->get_stencil_attachment();
1909
1910 if (src_attachment.used && dst_attachment.used) {
1911
1912 /* TODO(Metal): Support depth(z) offset in blit if needed. */
1913 src_attachment.texture->blit(blit_encoder,
1914 src_x_offset,
1915 src_y_offset,
1916 0,
1917 src_attachment.slice,
1918 src_attachment.mip,
1919 dst_attachment.texture,
1920 dst_x_offset,
1921 dst_y_offset,
1922 0,
1923 dst_attachment.slice,
1924 dst_attachment.mip,
1925 width,
1926 height,
1927 1);
1928 }
1929 else {
1930 MTL_LOG_ERROR("Failed performing Stencil blit");
1931 }
1932 }
1933}
1934
1936{
1937 return width_;
1938}
1940{
1941 return height_;
1942}
1943
1945{
1946 attachment_width_ = w;
1947 attachment_height_ = h;
1948}
1949
1951{
1952 return attachment_width_;
1953}
1955{
1956 return attachment_height_;
1957}
1958
1960
1961} // namespace blender::gpu
#define UNPACK4(a)
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void size_set(int width, int height)
void set_color_attachment_bit(GPUAttachmentType type, bool value)
FrameBuffer(const char *name)
GPUAttachment attachments_[GPU_FB_MAX_ATTACHMENT]
int viewport_[GPU_MAX_VIEWPORTS][4]
static MTLCapabilities capabilities
bool end_active_command_encoder(bool retain_framebuffers=false)
id< MTLBlitCommandEncoder > ensure_begin_blit_encoder()
MTLFrameBuffer * get_current_framebuffer()
void set_scissor_enabled(bool scissor_enabled)
void framebuffer_bind(MTLFrameBuffer *framebuffer)
id< MTLRenderCommandEncoder > ensure_begin_render_pass()
static MTLContext * get()
void set_viewport(int origin_x, int origin_y, int width, int height)
void set_viewports(int count, const int(&viewports)[GPU_MAX_VIEWPORTS][4])
void set_scissor(int scissor_x, int scissor_y, int scissor_width, int scissor_height)
MTLCommandBufferManager main_command_buffer
MTLAttachment get_color_attachment(uint slot)
void attachment_size_set(int w, int h)
void blit(uint read_slot, uint src_x_offset, uint src_y_offset, MTLFrameBuffer *metal_fb_write, uint write_slot, uint dst_x_offset, uint dst_y_offset, uint width, uint height, GPUFrameBufferBits blit_buffers)
bool set_stencil_loadstore_op(GPULoadOp load_action, GPUStoreOp store_action)
bool set_depth_loadstore_op(GPULoadOp load_action, GPUStoreOp store_action)
int get_color_attachment_slot_from_texture(gpu::MTLTexture *texture)
bool check(char err_out[256]) override
void read(GPUFrameBufferBits planes, eGPUDataFormat format, const int area[4], int channel_len, int slot, void *r_data) override
bool add_color_attachment(gpu::MTLTexture *texture, uint slot, int miplevel, int layer)
void clear_multi(Span< double4 > clear_cols) override
void clear(GPUFrameBufferBits buffers, const double4 clear_col, float clear_depth, uint clear_stencil) override
MTLFrameBuffer(MTLContext *ctx, const char *name)
bool set_depth_attachment_clear_value(float depth_clear)
void update_attachments(bool update_viewport)
MTLRenderPassDescriptor * bake_render_pass_descriptor(bool load_contents)
bool has_color_attachment_with_texture(gpu::MTLTexture *texture)
bool set_color_loadstore_op(uint slot, GPULoadOp load_action, GPUStoreOp store_action)
void blit_to(GPUFrameBufferBits planes, int src_slot, FrameBuffer *dst, int dst_slot, int dst_offset_x, int dst_offset_y) override
bool add_depth_attachment(gpu::MTLTexture *texture, int miplevel, int layer)
void attachment_set_loadstore_op(GPUAttachmentType type, GPULoadStore ls) override
bool set_stencil_attachment_clear_value(uint stencil_clear)
bool add_stencil_attachment(gpu::MTLTexture *texture, int miplevel, int layer)
void clear_attachment(GPUAttachmentType type, const double4 clear_value) override
bool set_color_attachment_clear_color(uint slot, const double4 clear_color)
void bind(bool enabled_srgb) override
void subpass_transition_impl(const GPUAttachmentState, Span< GPUAttachmentState > color_attachment_states) override
id< MTLTexture > get_non_srgb_handle()
id< MTLTexture > get_metal_handle_base()
static void set_framebuffer_srgb_target(int use_srgb_to_linear)
eGPUTextureUsage usage_get() const
TextureFormat format_get() const
#define GPU_FB_MAX_COLOR_ATTACHMENT
#define layer
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define MTL_LOG_WARNING(info,...)
Definition mtl_debug.hh:39
#define MTL_LOG_ERROR(info,...)
Definition mtl_debug.hh:31
MTLLoadAction mtl_load_action_from_gpu(GPULoadOp action)
MTLStoreAction mtl_store_action_from_gpu(GPUStoreOp action)
int to_bytesize(const DataFormat format)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
gpu::TextureFormat GPU_texture_format(const gpu::Texture *texture)
void GPU_framebuffer_restore()
unsigned int uint
Definition BKE_text.h:55
bool assign_if_different(T &old_value, T &&new_value)
@ GPU_LOADACTION_DONT_CARE
MINLINE int max_ii(int a, int b)
@ GPU_STOREACTION_DONT_CARE
@ GPU_TEXTURE_USAGE_ATTACHMENT
@ GPU_TEXTURE_USAGE_MEMORYLESS
void GPU_texture_image_bind(gpu::Texture *texture, int unit)
VecBase< double, 4 > double4
void GPU_texture_get_mipmap_size(gpu::Texture *texture, int mip_level, int *r_size)