800 if (miplevel < 0 || miplevel >= MTL_MAX_MIPMAP_COUNT) {
801 MTL_LOG_WARNING(
"Attachment specified with invalid mip level %u", miplevel);
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;
813 mtl_color_attachments_[slot].render_target_array_length = 0;
820 mtl_color_attachments_[slot].slice = 0;
821 mtl_color_attachments_[slot].depth_plane = 0;
826 MTL_LOG_WARNING(
"TODO: Support layered rendering for 1D array textures, if needed.");
829 mtl_color_attachments_[slot].slice =
layer;
830 mtl_color_attachments_[slot].depth_plane = 0;
834 mtl_color_attachments_[slot].slice =
layer;
835 mtl_color_attachments_[slot].depth_plane = 0;
837 mtl_color_attachments_[slot].slice = 0;
838 mtl_color_attachments_[slot].render_target_array_length =
texture->d_;
839 use_multilayered_rendering_ =
true;
844 mtl_color_attachments_[slot].slice = 0;
845 mtl_color_attachments_[slot].depth_plane =
layer;
847 mtl_color_attachments_[slot].depth_plane = 0;
848 mtl_color_attachments_[slot].render_target_array_length =
texture->d_;
849 use_multilayered_rendering_ =
true;
854 mtl_color_attachments_[slot].slice =
layer;
855 mtl_color_attachments_[slot].depth_plane = 0;
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;
866 mtl_color_attachments_[slot].slice =
layer;
867 mtl_color_attachments_[slot].depth_plane = 0;
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;
876 mtl_color_attachments_[slot].slice = 0;
877 mtl_color_attachments_[slot].depth_plane = 0;
880 MTL_LOG_ERROR(
"MTLFrameBuffer::add_color_attachment Unrecognized texture type %u",
886 int width_of_miplayer, height_of_miplayer;
888 width_of_miplayer =
texture->width_get();
889 height_of_miplayer =
texture->height_get();
892 width_of_miplayer =
max_ii(
texture->width_get() >> miplevel, 1);
893 height_of_miplayer =
max_ii(
texture->height_get() >> miplevel, 1);
896 if (attachment_width_ == 0 || attachment_height_ == 0) {
897 this->
size_set(width_of_miplayer, height_of_miplayer);
903 BLI_assert(attachment_width_ == width_of_miplayer);
904 BLI_assert(attachment_height_ == height_of_miplayer);
912 "Passing in null texture to MTLFrameBuffer::add_color_attachment (This could be due to "
913 "not all texture types being supported).");
923 if (miplevel < 0 || miplevel >= MTL_MAX_MIPMAP_COUNT) {
924 MTL_LOG_WARNING(
"Attachment specified with invalid mip level %u", miplevel);
929 mtl_depth_attachment_.used =
true;
930 mtl_depth_attachment_.texture =
texture;
931 mtl_depth_attachment_.mip = miplevel;
934 mtl_depth_attachment_.render_target_array_length = 0;
941 mtl_depth_attachment_.slice = 0;
942 mtl_depth_attachment_.depth_plane = 0;
947 MTL_LOG_WARNING(
"TODO: Support layered rendering for 1D array textures, if needed");
950 mtl_depth_attachment_.slice =
layer;
951 mtl_depth_attachment_.depth_plane = 0;
955 mtl_depth_attachment_.slice =
layer;
956 mtl_depth_attachment_.depth_plane = 0;
958 mtl_depth_attachment_.slice = 0;
959 mtl_depth_attachment_.render_target_array_length =
texture->d_;
960 use_multilayered_rendering_ =
true;
965 mtl_depth_attachment_.slice = 0;
966 mtl_depth_attachment_.depth_plane =
layer;
968 mtl_depth_attachment_.depth_plane = 0;
969 mtl_depth_attachment_.render_target_array_length =
texture->d_;
970 use_multilayered_rendering_ =
true;
975 mtl_depth_attachment_.slice =
layer;
976 mtl_depth_attachment_.depth_plane = 0;
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;
987 mtl_depth_attachment_.slice =
layer;
988 mtl_depth_attachment_.depth_plane = 0;
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;
997 mtl_depth_attachment_.slice = 0;
998 mtl_depth_attachment_.depth_plane = 0;
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();
1012 width_of_miplayer =
max_ii(
texture->width_get() >> miplevel, 1);
1013 height_of_miplayer =
max_ii(
texture->height_get() >> miplevel, 1);
1016 if (attachment_width_ == 0 || attachment_height_ == 0) {
1017 this->
size_set(width_of_miplayer, height_of_miplayer);
1023 BLI_assert(attachment_width_ == width_of_miplayer);
1024 BLI_assert(attachment_height_ == height_of_miplayer);
1032 "Passing in null texture to MTLFrameBuffer::addDepthAttachment (This could be due to not "
1033 "all texture types being supported).");
1043 if (miplevel < 0 || miplevel >= MTL_MAX_MIPMAP_COUNT) {
1044 MTL_LOG_WARNING(
"Attachment specified with invalid mip level %u", miplevel);
1049 mtl_stencil_attachment_.used =
true;
1050 mtl_stencil_attachment_.texture =
texture;
1051 mtl_stencil_attachment_.mip = miplevel;
1054 mtl_stencil_attachment_.render_target_array_length = 0;
1061 mtl_stencil_attachment_.slice = 0;
1062 mtl_stencil_attachment_.depth_plane = 0;
1067 MTL_LOG_WARNING(
"TODO: Support layered rendering for 1D array textures, if needed");
1070 mtl_stencil_attachment_.slice =
layer;
1071 mtl_stencil_attachment_.depth_plane = 0;
1075 mtl_stencil_attachment_.slice =
layer;
1076 mtl_stencil_attachment_.depth_plane = 0;
1078 mtl_stencil_attachment_.slice = 0;
1079 mtl_stencil_attachment_.render_target_array_length =
texture->d_;
1080 use_multilayered_rendering_ =
true;
1085 mtl_stencil_attachment_.slice = 0;
1086 mtl_stencil_attachment_.depth_plane =
layer;
1088 mtl_stencil_attachment_.depth_plane = 0;
1089 mtl_stencil_attachment_.render_target_array_length =
texture->d_;
1090 use_multilayered_rendering_ =
true;
1095 mtl_stencil_attachment_.slice =
layer;
1096 mtl_stencil_attachment_.depth_plane = 0;
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;
1107 mtl_stencil_attachment_.slice =
layer;
1108 mtl_stencil_attachment_.depth_plane = 0;
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;
1117 mtl_stencil_attachment_.slice = 0;
1118 mtl_stencil_attachment_.depth_plane = 0;
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();
1132 width_of_miplayer =
max_ii(
texture->width_get() >> miplevel, 1);
1133 height_of_miplayer =
max_ii(
texture->height_get() >> miplevel, 1);
1136 if (attachment_width_ == 0 || attachment_height_ == 0) {
1137 this->
size_set(width_of_miplayer, height_of_miplayer);
1143 BLI_assert(attachment_width_ == width_of_miplayer);
1144 BLI_assert(attachment_height_ == height_of_miplayer);
1152 "Passing in null texture to MTLFrameBuffer::addStencilAttachment (This could be due to "
1153 "not all texture types being supported).");
1487 if (load_contents) {
1500 for (
int config = 0; config < 3; config++) {
1501 descriptor_dirty_[config] =
true;
1504 else if (is_loadstore_dirty_) {
1506 descriptor_dirty_[MTL_FB_CONFIG_CLEAR] =
true;
1507 descriptor_dirty_[MTL_FB_CONFIG_CUSTOM] =
true;
1519 uint descriptor_config = (load_contents) ? MTL_FB_CONFIG_LOAD :
1521 MTL_FB_CONFIG_CUSTOM);
1527 descriptor_config = MTL_FB_CONFIG_CUSTOM;
1528 descriptor_dirty_[descriptor_config] =
true;
1531 if (descriptor_dirty_[descriptor_config] || framebuffer_descriptor_[descriptor_config] == nil) {
1534 if (framebuffer_descriptor_[descriptor_config] == nil) {
1535 framebuffer_descriptor_[descriptor_config] = [[MTLRenderPassDescriptor alloc]
init];
1539 if (use_multilayered_rendering_) {
1546 if (mtl_color_attachments_[attachment_ind].used) {
1548 len = mtl_color_attachments_[attachment_ind].render_target_array_length;
1552 (
len == mtl_color_attachments_[attachment_ind].render_target_array_length);
1557 if (mtl_depth_attachment_.used) {
1559 len = mtl_depth_attachment_.render_target_array_length;
1562 valid = valid && (
len == mtl_depth_attachment_.render_target_array_length);
1566 if (mtl_stencil_attachment_.used) {
1568 len = mtl_stencil_attachment_.render_target_array_length;
1571 valid = valid && (
len == mtl_stencil_attachment_.render_target_array_length);
1577 framebuffer_descriptor_[descriptor_config].renderTargetArrayLength =
len;
1580 framebuffer_descriptor_[descriptor_config].renderTargetArrayLength = 0;
1584 int colour_attachments = 0;
1586 MTLAttachment &attachment_config = mtl_color_attachments_[attachment_ind];
1588 if (attachment_config.
used) {
1591 MTL_LOG_ERROR(
"Attempting to assign invalid texture as attachment");
1600 !this->get_srgb_enabled())
1609 if (descriptor_config == MTL_FB_CONFIG_LOAD) {
1634 MTLRenderPassColorAttachmentDescriptor *attachment =
1635 colour_attachment_descriptors_[attachment_ind];
1638 attachment.texture = source_color_texture;
1642 MTLClearColorMake(0.0, 0.0, 0.0, 0.0);
1644 attachment.level = attachment_config.
mip;
1645 attachment.slice = attachment_config.
slice;
1646 attachment.depthPlane = attachment_config.
depth_plane;
1647 colour_attachments++;
1650 [framebuffer_descriptor_[descriptor_config].colorAttachments setObject:attachment
1651 atIndexedSubscript:attachment_ind];
1655 [framebuffer_descriptor_[descriptor_config].colorAttachments setObject:nil
1656 atIndexedSubscript:attachment_ind];
1659 BLI_assert(colour_attachments == colour_attachment_count_);
1663 if (mtl_depth_attachment_.used) {
1664 framebuffer_descriptor_[descriptor_config].depthAttachment.texture =
1665 (
id<MTLTexture>)mtl_depth_attachment_.texture->get_metal_handle_base();
1667 bool texture_is_memoryless = (mtl_depth_attachment_.texture->usage_get() &
1672 GPULoadOp load_action = mtl_depth_attachment_.load_action;
1673 if (descriptor_config == MTL_FB_CONFIG_LOAD) {
1687 GPUStoreOp store_action = mtl_depth_attachment_.store_action;
1695 framebuffer_descriptor_[descriptor_config].depthAttachment.loadAction =
1697 framebuffer_descriptor_[descriptor_config].depthAttachment.clearDepth =
1699 framebuffer_descriptor_[descriptor_config].depthAttachment.storeAction =
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;
1708 framebuffer_descriptor_[descriptor_config].depthAttachment.texture = nil;
1712 if (mtl_stencil_attachment_.used) {
1713 framebuffer_descriptor_[descriptor_config].stencilAttachment.texture =
1714 (
id<MTLTexture>)mtl_stencil_attachment_.texture->get_metal_handle_base();
1716 bool texture_is_memoryless = (mtl_stencil_attachment_.texture->usage_get() &
1721 GPULoadOp load_action = mtl_stencil_attachment_.load_action;
1722 if (descriptor_config == MTL_FB_CONFIG_LOAD) {
1736 GPUStoreOp store_action = mtl_stencil_attachment_.store_action;
1744 framebuffer_descriptor_[descriptor_config].stencilAttachment.loadAction =
1746 framebuffer_descriptor_[descriptor_config].stencilAttachment.clearStencil =
1748 framebuffer_descriptor_[descriptor_config].stencilAttachment.storeAction =
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;
1758 framebuffer_descriptor_[descriptor_config].stencilAttachment.texture = nil;
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) {
1766 framebuffer_descriptor_[descriptor_config].renderTargetWidth =
width_;
1767 framebuffer_descriptor_[descriptor_config].renderTargetHeight =
height_;
1768 framebuffer_descriptor_[descriptor_config].defaultRasterSampleCount = 1;
1771 descriptor_dirty_[descriptor_config] =
false;
1775 is_loadstore_dirty_ =
false;
1780 return framebuffer_descriptor_[descriptor_config];