848 MTL_LOG_WARNING(
"Attachment specified with invalid mip level %u", miplevel);
854 colour_attachment_count_ += (!mtl_color_attachments_[slot].used) ? 1 : 0;
855 mtl_color_attachments_[slot].used =
true;
856 mtl_color_attachments_[slot].texture =
texture;
857 mtl_color_attachments_[slot].mip = miplevel;
860 mtl_color_attachments_[slot].render_target_array_length = 0;
867 mtl_color_attachments_[slot].slice = 0;
868 mtl_color_attachments_[slot].depth_plane = 0;
873 MTL_LOG_WARNING(
"TODO: Support layered rendering for 1D array textures, if needed.");
876 mtl_color_attachments_[slot].slice = layer;
877 mtl_color_attachments_[slot].depth_plane = 0;
881 mtl_color_attachments_[slot].slice = layer;
882 mtl_color_attachments_[slot].depth_plane = 0;
884 mtl_color_attachments_[slot].slice = 0;
885 mtl_color_attachments_[slot].render_target_array_length =
texture->d_;
886 use_multilayered_rendering_ =
true;
891 mtl_color_attachments_[slot].slice = 0;
892 mtl_color_attachments_[slot].depth_plane = layer;
894 mtl_color_attachments_[slot].depth_plane = 0;
895 mtl_color_attachments_[slot].render_target_array_length =
texture->d_;
896 use_multilayered_rendering_ =
true;
901 mtl_color_attachments_[slot].slice = layer;
902 mtl_color_attachments_[slot].depth_plane = 0;
904 mtl_color_attachments_[slot].slice = 0;
905 mtl_color_attachments_[slot].depth_plane = 0;
906 mtl_color_attachments_[slot].render_target_array_length = 6;
907 use_multilayered_rendering_ =
true;
913 mtl_color_attachments_[slot].slice = layer;
914 mtl_color_attachments_[slot].depth_plane = 0;
916 mtl_color_attachments_[slot].slice = 0;
917 mtl_color_attachments_[slot].depth_plane = 0;
918 mtl_color_attachments_[slot].render_target_array_length =
texture->d_;
919 use_multilayered_rendering_ =
true;
923 mtl_color_attachments_[slot].slice = 0;
924 mtl_color_attachments_[slot].depth_plane = 0;
927 MTL_LOG_ERROR(
"MTLFrameBuffer::add_color_attachment Unrecognized texture type %u",
933 int width_of_miplayer, height_of_miplayer;
935 width_of_miplayer =
texture->width_get();
936 height_of_miplayer =
texture->height_get();
939 width_of_miplayer =
max_ii(
texture->width_get() >> miplevel, 1);
940 height_of_miplayer =
max_ii(
texture->height_get() >> miplevel, 1);
943 if (default_width_ == 0 || default_height_ == 0) {
949 BLI_assert(default_width_ == width_of_miplayer);
950 BLI_assert(default_height_ == height_of_miplayer);
958 "Passing in null texture to MTLFrameBuffer::add_color_attachment (This could be due to "
959 "not all texture types being supported).");
970 MTL_LOG_WARNING(
"Attachment specified with invalid mip level %u", miplevel);
975 mtl_depth_attachment_.used =
true;
976 mtl_depth_attachment_.texture =
texture;
977 mtl_depth_attachment_.mip = miplevel;
980 mtl_depth_attachment_.render_target_array_length = 0;
987 mtl_depth_attachment_.slice = 0;
988 mtl_depth_attachment_.depth_plane = 0;
993 MTL_LOG_WARNING(
"TODO: Support layered rendering for 1D array textures, if needed");
996 mtl_depth_attachment_.slice = layer;
997 mtl_depth_attachment_.depth_plane = 0;
1001 mtl_depth_attachment_.slice = layer;
1002 mtl_depth_attachment_.depth_plane = 0;
1004 mtl_depth_attachment_.slice = 0;
1005 mtl_depth_attachment_.render_target_array_length =
texture->d_;
1006 use_multilayered_rendering_ =
true;
1011 mtl_depth_attachment_.slice = 0;
1012 mtl_depth_attachment_.depth_plane = layer;
1014 mtl_depth_attachment_.depth_plane = 0;
1015 mtl_depth_attachment_.render_target_array_length =
texture->d_;
1016 use_multilayered_rendering_ =
true;
1021 mtl_depth_attachment_.slice = layer;
1022 mtl_depth_attachment_.depth_plane = 0;
1024 mtl_depth_attachment_.slice = 0;
1025 mtl_depth_attachment_.depth_plane = 0;
1026 mtl_depth_attachment_.render_target_array_length = 6;
1027 use_multilayered_rendering_ =
true;
1033 mtl_depth_attachment_.slice = layer;
1034 mtl_depth_attachment_.depth_plane = 0;
1036 mtl_depth_attachment_.slice = 0;
1037 mtl_depth_attachment_.depth_plane = 0;
1038 mtl_depth_attachment_.render_target_array_length =
texture->d_;
1039 use_multilayered_rendering_ =
true;
1043 mtl_depth_attachment_.slice = 0;
1044 mtl_depth_attachment_.depth_plane = 0;
1052 int width_of_miplayer, height_of_miplayer;
1053 if (miplevel <= 0) {
1054 width_of_miplayer =
texture->width_get();
1055 height_of_miplayer =
texture->height_get();
1058 width_of_miplayer =
max_ii(
texture->width_get() >> miplevel, 1);
1059 height_of_miplayer =
max_ii(
texture->height_get() >> miplevel, 1);
1062 if (default_width_ == 0 || default_height_ == 0) {
1068 BLI_assert(default_width_ == width_of_miplayer);
1069 BLI_assert(default_height_ == height_of_miplayer);
1077 "Passing in null texture to MTLFrameBuffer::addDepthAttachment (This could be due to not "
1078 "all texture types being supported).");
1089 MTL_LOG_WARNING(
"Attachment specified with invalid mip level %u", miplevel);
1094 mtl_stencil_attachment_.used =
true;
1095 mtl_stencil_attachment_.texture =
texture;
1096 mtl_stencil_attachment_.mip = miplevel;
1099 mtl_stencil_attachment_.render_target_array_length = 0;
1106 mtl_stencil_attachment_.slice = 0;
1107 mtl_stencil_attachment_.depth_plane = 0;
1112 MTL_LOG_WARNING(
"TODO: Support layered rendering for 1D array textures, if needed");
1115 mtl_stencil_attachment_.slice = layer;
1116 mtl_stencil_attachment_.depth_plane = 0;
1120 mtl_stencil_attachment_.slice = layer;
1121 mtl_stencil_attachment_.depth_plane = 0;
1123 mtl_stencil_attachment_.slice = 0;
1124 mtl_stencil_attachment_.render_target_array_length =
texture->d_;
1125 use_multilayered_rendering_ =
true;
1130 mtl_stencil_attachment_.slice = 0;
1131 mtl_stencil_attachment_.depth_plane = layer;
1133 mtl_stencil_attachment_.depth_plane = 0;
1134 mtl_stencil_attachment_.render_target_array_length =
texture->d_;
1135 use_multilayered_rendering_ =
true;
1140 mtl_stencil_attachment_.slice = layer;
1141 mtl_stencil_attachment_.depth_plane = 0;
1143 mtl_stencil_attachment_.slice = 0;
1144 mtl_stencil_attachment_.depth_plane = 0;
1145 mtl_stencil_attachment_.render_target_array_length = 6;
1146 use_multilayered_rendering_ =
true;
1152 mtl_stencil_attachment_.slice = layer;
1153 mtl_stencil_attachment_.depth_plane = 0;
1155 mtl_stencil_attachment_.slice = 0;
1156 mtl_stencil_attachment_.depth_plane = 0;
1157 mtl_stencil_attachment_.render_target_array_length =
texture->d_;
1158 use_multilayered_rendering_ =
true;
1162 mtl_stencil_attachment_.slice = 0;
1163 mtl_stencil_attachment_.depth_plane = 0;
1171 int width_of_miplayer, height_of_miplayer;
1172 if (miplevel <= 0) {
1173 width_of_miplayer =
texture->width_get();
1174 height_of_miplayer =
texture->height_get();
1177 width_of_miplayer =
max_ii(
texture->width_get() >> miplevel, 1);
1178 height_of_miplayer =
max_ii(
texture->height_get() >> miplevel, 1);
1181 if (default_width_ == 0 || default_height_ == 0) {
1187 BLI_assert(default_width_ == width_of_miplayer);
1188 BLI_assert(default_height_ == height_of_miplayer);
1196 "Passing in null texture to MTLFrameBuffer::addStencilAttachment (This could be due to "
1197 "not all texture types being supported).");
1540 if (load_contents) {
1553 for (
int config = 0; config < 3; config++) {
1554 descriptor_dirty_[config] =
true;
1557 else if (is_loadstore_dirty_) {
1559 descriptor_dirty_[MTL_FB_CONFIG_CLEAR] =
true;
1560 descriptor_dirty_[MTL_FB_CONFIG_CUSTOM] =
true;
1572 uint descriptor_config = (load_contents) ? MTL_FB_CONFIG_LOAD :
1574 MTL_FB_CONFIG_CUSTOM);
1580 descriptor_config = MTL_FB_CONFIG_CUSTOM;
1581 descriptor_dirty_[descriptor_config] =
true;
1584 if (descriptor_dirty_[descriptor_config] || framebuffer_descriptor_[descriptor_config] == nil) {
1587 if (framebuffer_descriptor_[descriptor_config] == nil) {
1588 framebuffer_descriptor_[descriptor_config] = [[MTLRenderPassDescriptor alloc]
init];
1592 if (use_multilayered_rendering_) {
1599 if (mtl_color_attachments_[attachment_ind].used) {
1601 len = mtl_color_attachments_[attachment_ind].render_target_array_length;
1605 (
len == mtl_color_attachments_[attachment_ind].render_target_array_length);
1610 if (mtl_depth_attachment_.used) {
1612 len = mtl_depth_attachment_.render_target_array_length;
1615 valid = valid && (
len == mtl_depth_attachment_.render_target_array_length);
1619 if (mtl_stencil_attachment_.used) {
1621 len = mtl_stencil_attachment_.render_target_array_length;
1624 valid = valid && (
len == mtl_stencil_attachment_.render_target_array_length);
1630 framebuffer_descriptor_[descriptor_config].renderTargetArrayLength =
len;
1633 framebuffer_descriptor_[descriptor_config].renderTargetArrayLength = 0;
1637 int colour_attachments = 0;
1639 MTLAttachment &attachment_config = mtl_color_attachments_[attachment_ind];
1641 if (attachment_config.
used) {
1642 id<MTLTexture>
texture = attachment_config.
texture->get_metal_handle_base();
1644 MTL_LOG_ERROR(
"Attempting to assign invalid texture as attachment");
1651 id<MTLTexture> source_color_texture =
texture;
1653 !this->get_srgb_enabled())
1655 source_color_texture = attachment_config.
texture->get_non_srgb_handle();
1662 if (descriptor_config == MTL_FB_CONFIG_LOAD) {
1687 MTLRenderPassColorAttachmentDescriptor *attachment =
1688 colour_attachment_descriptors_[attachment_ind];
1691 attachment.texture = source_color_texture;
1696 MTLClearColorMake(0.0, 0.0, 0.0, 0.0);
1698 attachment.level = attachment_config.
mip;
1699 attachment.slice = attachment_config.
slice;
1700 attachment.depthPlane = attachment_config.
depth_plane;
1701 colour_attachments++;
1704 [framebuffer_descriptor_[descriptor_config].colorAttachments setObject:attachment
1705 atIndexedSubscript:attachment_ind];
1709 [framebuffer_descriptor_[descriptor_config].colorAttachments setObject:nil
1710 atIndexedSubscript:attachment_ind];
1713 BLI_assert(colour_attachments == colour_attachment_count_);
1717 if (mtl_depth_attachment_.used) {
1718 framebuffer_descriptor_[descriptor_config].depthAttachment.texture =
1719 (id<MTLTexture>)mtl_depth_attachment_.texture->get_metal_handle_base();
1721 bool texture_is_memoryless = (mtl_depth_attachment_.texture->usage_get() &
1726 GPULoadOp load_action = mtl_depth_attachment_.load_action;
1727 if (descriptor_config == MTL_FB_CONFIG_LOAD) {
1741 GPUStoreOp store_action = mtl_depth_attachment_.store_action;
1749 framebuffer_descriptor_[descriptor_config].depthAttachment.loadAction =
1751 framebuffer_descriptor_[descriptor_config].depthAttachment.clearDepth =
1753 framebuffer_descriptor_[descriptor_config].depthAttachment.storeAction =
1755 framebuffer_descriptor_[descriptor_config].depthAttachment.level = mtl_depth_attachment_.mip;
1756 framebuffer_descriptor_[descriptor_config].depthAttachment.slice =
1757 mtl_depth_attachment_.slice;
1758 framebuffer_descriptor_[descriptor_config].depthAttachment.depthPlane =
1759 mtl_depth_attachment_.depth_plane;
1762 framebuffer_descriptor_[descriptor_config].depthAttachment.texture = nil;
1766 if (mtl_stencil_attachment_.used) {
1767 framebuffer_descriptor_[descriptor_config].stencilAttachment.texture =
1768 (id<MTLTexture>)mtl_stencil_attachment_.texture->get_metal_handle_base();
1770 bool texture_is_memoryless = (mtl_stencil_attachment_.texture->usage_get() &
1775 GPULoadOp load_action = mtl_stencil_attachment_.load_action;
1776 if (descriptor_config == MTL_FB_CONFIG_LOAD) {
1790 GPUStoreOp store_action = mtl_stencil_attachment_.store_action;
1798 framebuffer_descriptor_[descriptor_config].stencilAttachment.loadAction =
1800 framebuffer_descriptor_[descriptor_config].stencilAttachment.clearStencil =
1802 framebuffer_descriptor_[descriptor_config].stencilAttachment.storeAction =
1804 framebuffer_descriptor_[descriptor_config].stencilAttachment.level =
1805 mtl_stencil_attachment_.mip;
1806 framebuffer_descriptor_[descriptor_config].stencilAttachment.slice =
1807 mtl_stencil_attachment_.slice;
1808 framebuffer_descriptor_[descriptor_config].stencilAttachment.depthPlane =
1809 mtl_stencil_attachment_.depth_plane;
1812 framebuffer_descriptor_[descriptor_config].stencilAttachment.texture = nil;
1816 int total_num_attachments = colour_attachment_count_ + (mtl_depth_attachment_.used ? 1 : 0) +
1817 (mtl_stencil_attachment_.used ? 1 : 0);
1818 if (total_num_attachments == 0) {
1820 framebuffer_descriptor_[descriptor_config].renderTargetWidth =
width_;
1821 framebuffer_descriptor_[descriptor_config].renderTargetHeight =
height_;
1822 framebuffer_descriptor_[descriptor_config].defaultRasterSampleCount = 1;
1825 descriptor_dirty_[descriptor_config] =
false;
1829 is_loadstore_dirty_ =
false;
1834 return framebuffer_descriptor_[descriptor_config];
void GPU_texture_get_mipmap_size(blender::gpu::Texture *texture, int mip_level, int *r_size)
blender::gpu::TextureFormat GPU_texture_format(const blender::gpu::Texture *texture)