38void gpu::MTLTexture::mtl_texture_init()
45 resource_mode_ = MTL_TEXTURE_MODE_DEFAULT;
50 mip_swizzle_view_ = nil;
53 vert_buffer_ =
nullptr;
54 vert_buffer_mtl_ = nil;
57 tex_swizzle_mask_[0] =
'r';
58 tex_swizzle_mask_[1] =
'g';
59 tex_swizzle_mask_[2] =
'b';
60 tex_swizzle_mask_[3] =
'a';
61 mtl_swizzle_mask_ = MTLTextureSwizzleChannelsMake(
62 MTLTextureSwizzleRed, MTLTextureSwizzleGreen, MTLTextureSwizzleBlue, MTLTextureSwizzleAlpha);
84 init_2D((
int)metal_texture.width, (
int)metal_texture.height, 0, 1, format);
87 texture_ = metal_texture;
95 resource_mode_ = MTL_TEXTURE_MODE_EXTERNAL;
102 if (ctx !=
nullptr) {
116void gpu::MTLTexture::bake_mip_swizzle_view()
118 if (texture_view_dirty_flags_) {
124 if (resource_mode_ != MTL_TEXTURE_MODE_TEXTURE_VIEW &&
125 texture_view_dirty_flags_ == TEXTURE_VIEW_MIP_DIRTY && mip_swizzle_view_ == nil)
128 if (mip_min_ == 0 && mipmaps_ == mtl_max_mips_) {
129 texture_view_dirty_flags_ = TEXTURE_VIEW_NOT_DIRTY;
135 if (mip_swizzle_view_ != nil) {
136 [mip_swizzle_view_ release];
137 mip_swizzle_view_ = nil;
142 const gpu::Texture *tex_view_src =
this;
143 if (resource_mode_ == MTL_TEXTURE_MODE_TEXTURE_VIEW) {
144 tex_view_src = source_texture_;
152 max_slices = tex_view_src->height_get();
156 max_slices = tex_view_src->depth_get();
165 max_slices = tex_view_src->depth_get();
176 if (texture_view_stencil_) {
177 switch (texture_view_pixel_format) {
178 case MTLPixelFormatDepth24Unorm_Stencil8:
179 texture_view_pixel_format = MTLPixelFormatX24_Stencil8;
181 case MTLPixelFormatDepth32Float_Stencil8:
182 texture_view_pixel_format = MTLPixelFormatX32_Stencil8;
185 BLI_assert_msg(
false,
"Texture format does not support stencil views.");
192 MTLTextureType texture_view_texture_type =
to_metal_type(type_);
199 (texture_view_pixel_format == texture_.pixelFormat) ||
201 "Usage Flag GPU_TEXTURE_USAGE_FORMAT_VIEW must be specified if a texture view is "
202 "created with a different format to its source texture.");
204 int range_len =
min_ii((mip_max_ - mip_min_) + 1, (
int)texture_.mipmapLevelCount - mip_min_);
206 BLI_assert(mip_min_ < texture_.mipmapLevelCount);
209 mip_swizzle_view_ = [texture_
210 newTextureViewWithPixelFormat:texture_view_pixel_format
211 textureType:texture_view_texture_type
212 levels:NSMakeRange(mip_min_, range_len)
213 slices:NSMakeRange(view_layer_start_, num_slices)
214 swizzle:mtl_swizzle_mask_];
216 "Updating texture view - MIP TEXTURE BASE LEVEL: %d, MAX LEVEL: %d (Range len: %d)",
218 min_ii(mip_max_, (
int)texture_.mipmapLevelCount),
221 mip_swizzle_view_.label = [NSString
223 @"MipSwizzleView_%s__format=%u_type=%u_baselevel=%u_numlevels=%u_swizzle='%c%c%c%c'",
224 [[texture_ label] UTF8String],
225 (
uint)texture_view_pixel_format,
226 (
uint)texture_view_texture_type,
229 tex_swizzle_mask_[0],
230 tex_swizzle_mask_[1],
231 tex_swizzle_mask_[2],
232 tex_swizzle_mask_[3]];
234 mip_swizzle_view_.label = [texture_ label];
236 texture_view_dirty_flags_ = TEXTURE_VIEW_NOT_DIRTY;
250 if (resource_mode_ == MTL_TEXTURE_MODE_VBO) {
254 if (buf != vert_buffer_mtl_) {
256 "MTLTexture '%p' using MTL_TEXTURE_MODE_VBO requires re-generation due to updated "
266 buf = vert_buffer_->get_metal_buffer();
272 BLI_assert(vert_buffer_->get_metal_buffer() == vert_buffer_mtl_);
276 this->ensure_baked();
280 if (resource_mode_ == MTL_TEXTURE_MODE_TEXTURE_VIEW) {
281 BLI_assert_msg(mip_swizzle_view_,
"Texture view should always have a valid handle.");
284 if (mip_swizzle_view_ != nil || texture_view_dirty_flags_) {
285 bake_mip_swizzle_view();
290 if (mip_swizzle_view_ != nil) {
291 return mip_swizzle_view_;
303 this->ensure_baked();
306 if (resource_mode_ == MTL_TEXTURE_MODE_TEXTURE_VIEW) {
307 BLI_assert_msg(mip_swizzle_view_,
"Texture view should always have a valid handle.");
308 if (mip_swizzle_view_ != nil || texture_view_dirty_flags_) {
309 bake_mip_swizzle_view();
312 return mip_swizzle_view_;
340 BLI_assert(width > 0 && height > 0 && depth > 0);
341 MTLSize src_size = MTLSizeMake(width, height, depth);
342 MTLOrigin src_origin = MTLOriginMake(src_x_offset, src_y_offset, src_z_offset);
343 MTLOrigin dst_origin = MTLOriginMake(dst_x_offset, dst_y_offset, dst_z_offset);
345 if (!((this->format_get() == dst->
format_get()) ||
346 (this->format_get() == TextureFormat::SRGBA_8_8_8_8 &&
347 dst->
format_get() == TextureFormat::UNORM_8_8_8_8) ||
348 (this->format_get() == TextureFormat::UNORM_8_8_8_8 &&
349 dst->
format_get() == TextureFormat::SRGBA_8_8_8_8)))
352 "gpu::MTLTexture: Cannot copy between two textures of different types using a "
353 "blit encoder. TODO: Support this operation");
359 [blit_encoder copyFromTexture:this->get_metal_handle_base()
360 sourceSlice:src_slice
362 sourceOrigin:src_origin
364 toTexture:dst->get_metal_handle_base()
365 destinationSlice:dst_slice
366 destinationLevel:dst_mip
367 destinationOrigin:dst_origin];
381 BLI_assert(this->type_get() == dst->type_get());
389 gpu::FrameBuffer *blit_target_fb = dst->get_blit_framebuffer(dst_slice, dst_mip);
397 float w = dst->width_get();
398 float h = dst->height_get();
437 if (restore_fb !=
nullptr) {
445gpu::FrameBuffer *gpu::MTLTexture::get_blit_framebuffer(
int dst_slice,
uint dst_mip)
449 bool update_attachments =
false;
451 std::string fb_name = this->name_ +
"_blit_fb";
453 update_attachments =
true;
458 if (blit_fb_slice_ != dst_slice || blit_fb_mip_ != dst_mip) {
459 update_attachments =
true;
463 if (update_attachments) {
478 blit_fb_slice_ = dst_slice;
479 blit_fb_mip_ = dst_mip;
499 const uint unpack_row_length)
506 BLI_assert(resource_mode_ != MTL_TEXTURE_MODE_TEXTURE_VIEW);
509 this->ensure_mipmaps(mip);
512 this->ensure_baked();
521 const bool do_texture_unpack = !
ELEM(unpack_row_length, 0, extent[0]);
524 std::unique_ptr<uint8_t, MEM_smart_ptr_deleter<uint8_t>> unpack_buffer =
nullptr;
525 if (do_texture_unpack) {
527 "Compressed data with unpack_row_length != 0 is not supported.");
529 "3D texture data with unpack_row_length != 0 is not supported.");
533 size_t dst_total_count = dst_row_stride *
max_ii(extent[1], 1) *
max_ii(extent[2], 1);
540 uint8_t *dst_ptr = unpack_buffer.get();
541 for (
int y = 0;
y <
max_ii(extent[1], 1); ++
y) {
542 std::memcpy(dst_ptr, src_ptr, dst_row_stride);
543 src_ptr += src_row_stride;
544 dst_ptr += dst_row_stride;
549 data = unpack_buffer.get();
552 std::unique_ptr<uint16_t, MEM_smart_ptr_deleter<uint16_t>> clamped_half_buffer =
nullptr;
555 size_t pixel_count =
max_ii(extent[0], 1) *
max_ii(extent[1], 1) *
max_ii(extent[2], 1);
558 clamped_half_buffer.reset(
static_cast<uint16_t *
>(
561 Span<float> src(
static_cast<const float *
>(
data), total_component_count);
563 total_component_count);
565 constexpr int64_t chunk_size = 4 * 1024 * 1024;
576 data = clamped_half_buffer.get();
583 if (is_depth_format) {
587 update_sub_depth_2d(mip, offset, extent, type,
data);
591 "gpu::MTLTexture::update_sub not yet supported for other depth "
603 size_t totalsize = 0;
605 size_t expected_update_w = extent[0];
615 totalsize = input_bytes_per_pixel *
max_ulul(expected_update_w, 1);
618 totalsize = input_bytes_per_pixel *
max_ulul(expected_update_w, 1) * size_t(extent[1]);
621 totalsize = input_bytes_per_pixel *
max_ulul(expected_update_w, 1) * size_t(extent[1]) *
632 if (totalsize <= 0 || extent[0] <= 0) {
634 "MTLTexture::update_sub called with extent size of zero for one or more dimensions. "
635 "(%d, %d, %d) - DimCount: %u",
656 destination_num_channels,
663 bool can_use_direct_blit =
true;
664 if (!is_compressed && (expected_dst_bytes_per_pixel != input_bytes_per_pixel ||
665 num_channels != destination_num_channels))
667 can_use_direct_blit =
false;
670 if (is_depth_format) {
674 if (extent[0] == 1 && extent[1] == 1 && extent[2] == 1 && totalsize == 4) {
675 can_use_direct_blit =
false;
680 if (
format_ == TextureFormat::SRGBA_8_8_8_8 && !can_use_direct_blit) {
682 "SRGB data upload does not work correctly using compute upload. "
692 "Special input data type must be a 1-1 mapping with destination texture as it "
693 "cannot easily be split");
697 if (!can_use_direct_blit) {
701 "Updating texture layers other than mip=0 when data is mismatched is not "
702 "possible in METAL on macOS using texture->write\n");
709 "Updating texture -- destination MTLPixelFormat '%d' does not support write "
710 "operations, and no suitable TextureView format exists.\n",
711 *(
int *)(&destination_format));
721 if (compatible_write_format == MTLPixelFormatInvalid) {
722 MTL_LOG_ERROR(
"Cannot use compute update blit with texture-view format: %d\n",
723 *((
int *)&compatible_write_format));
729 totalsize,
true,
data);
740 bool use_staging_texture =
false;
742 if (can_use_direct_blit) {
749 if ((compatible_write_format != destination_format) &&
752 use_staging_texture =
true;
763 use_staging_texture =
true;
765 if (compatible_write_format != destination_format) {
767 use_staging_texture =
true;
773 if (use_staging_texture) {
776 MTLTextureUsage original_usage = texture_descriptor_.usage;
777 texture_descriptor_.usage = original_usage | MTLTextureUsageShaderWrite |
778 MTLTextureUsagePixelFormatView;
779 staging_texture = [ctx->
device newTextureWithDescriptor:texture_descriptor_];
780 staging_texture.label =
@"Staging texture";
781 texture_descriptor_.usage = original_usage;
784 texture_handle = ((compatible_write_format == destination_format)) ?
785 [staging_texture retain] :
786 [staging_texture newTextureViewWithPixelFormat:compatible_write_format];
790 if (compatible_write_format != destination_format) {
792 texture_handle = [texture_ newTextureViewWithPixelFormat:compatible_write_format];
795 texture_handle = texture_;
796 [texture_handle retain];
805 if (can_use_direct_blit) {
807 size_t bytes_per_row = expected_dst_bytes_per_pixel * extent[0];
808 size_t bytes_per_image = bytes_per_row;
812 bytes_per_row = blocks_x * block_size;
813 bytes_per_image = bytes_per_row;
816 for (
int array_index = 0; array_index < max_array_index; array_index++) {
818 size_t buffer_array_offset = (bytes_per_image * size_t(array_index));
820 copyFromBuffer:staging_buffer
821 sourceOffset:buffer_array_offset
822 sourceBytesPerRow:bytes_per_row
823 sourceBytesPerImage:bytes_per_image
824 sourceSize:MTLSizeMake(extent[0], 1, 1)
825 toTexture:texture_handle
829 destinationOrigin:MTLOriginMake(offset[0], 0, 0)];
836 compute_specialization_kernel);
837 TextureUpdateParams params = {mip, {extent[0], 1, 1}, {offset[0], 0, 0}, extent[0]};
846 dispatchThreads:MTLSizeMake(extent[0], 1, 1)
847 threadsPerThreadgroup:MTLSizeMake(64, 1, 1)];
851 compute_specialization_kernel);
852 TextureUpdateParams params = {
853 mip, {extent[0], extent[1], 1}, {offset[0], offset[1], 0}, extent[0]};
862 dispatchThreads:MTLSizeMake(extent[0], extent[1], 1)
863 threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
871 if (can_use_direct_blit) {
873 size_t bytes_per_row = expected_dst_bytes_per_pixel * extent[0];
874 size_t bytes_per_image = bytes_per_row * extent[1];
879 bytes_per_row = blocks_x * block_size;
880 bytes_per_image = bytes_per_row * blocks_y;
883 size_t texture_array_relative_offset = 0;
887 for (
int array_slice = base_slice; array_slice < final_slice; array_slice++) {
889 if (array_slice > 0) {
894 [blit_encoder copyFromBuffer:staging_buffer
895 sourceOffset:texture_array_relative_offset
896 sourceBytesPerRow:bytes_per_row
897 sourceBytesPerImage:bytes_per_image
898 sourceSize:MTLSizeMake(extent[0], extent[1], 1)
899 toTexture:texture_handle
900 destinationSlice:array_slice
902 destinationOrigin:MTLOriginMake(offset[0], offset[1], 0)];
904 texture_array_relative_offset += bytes_per_image;
911 compute_specialization_kernel);
912 TextureUpdateParams params = {
913 mip, {extent[0], extent[1], 1}, {offset[0], offset[1], 0}, extent[0]};
922 dispatchThreads:MTLSizeMake(
923 extent[0], extent[1], 1)
924 threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
928 compute_specialization_kernel);
929 TextureUpdateParams params = {mip,
930 {extent[0], extent[1], extent[2]},
931 {offset[0], offset[1], offset[2]},
940 [compute_encoder dispatchThreads:MTLSizeMake(extent[0],
943 threadsPerThreadgroup:MTLSizeMake(4, 4, 4)];
951 if (can_use_direct_blit) {
952 size_t bytes_per_row = expected_dst_bytes_per_pixel * extent[0];
953 size_t bytes_per_image = bytes_per_row * extent[1];
954 [blit_encoder copyFromBuffer:staging_buffer
956 sourceBytesPerRow:bytes_per_row
957 sourceBytesPerImage:bytes_per_image
958 sourceSize:MTLSizeMake(extent[0], extent[1], extent[2])
959 toTexture:texture_handle
962 destinationOrigin:MTLOriginMake(offset[0], offset[1], offset[2])];
966 compute_specialization_kernel);
967 TextureUpdateParams params = {mip,
968 {extent[0], extent[1], extent[2]},
969 {offset[0], offset[1], offset[2]},
979 dispatchThreads:MTLSizeMake(
980 extent[0], extent[1], extent[2])
981 threadsPerThreadgroup:MTLSizeMake(4, 4, 4)];
987 if (can_use_direct_blit) {
988 size_t bytes_per_row = expected_dst_bytes_per_pixel * extent[0];
989 size_t bytes_per_image = bytes_per_row * extent[1];
990 size_t texture_array_relative_offset = 0;
993 for (
int i = 0; i < extent[2]; i++) {
994 int face_index = offset[2] + i;
996 [blit_encoder copyFromBuffer:staging_buffer
997 sourceOffset:texture_array_relative_offset
998 sourceBytesPerRow:bytes_per_row
999 sourceBytesPerImage:bytes_per_image
1000 sourceSize:MTLSizeMake(extent[0], extent[1], 1)
1001 toTexture:texture_handle
1002 destinationSlice:face_index
1003 destinationLevel:mip
1004 destinationOrigin:MTLOriginMake(offset[0], offset[1], 0)];
1005 texture_array_relative_offset += bytes_per_image;
1010 "TODO(Metal): Support compute texture update for GPU_TEXTURE_CUBE %d, %d, %d\n",
1018 if (can_use_direct_blit) {
1020 size_t bytes_per_row = expected_dst_bytes_per_pixel * extent[0];
1021 size_t bytes_per_image = bytes_per_row * extent[1];
1024 size_t texture_array_relative_offset = 0;
1025 for (
int i = 0; i < extent[2]; i++) {
1026 int face_index = offset[2] + i;
1027 [blit_encoder copyFromBuffer:staging_buffer
1028 sourceOffset:texture_array_relative_offset
1029 sourceBytesPerRow:bytes_per_row
1030 sourceBytesPerImage:bytes_per_image
1031 sourceSize:MTLSizeMake(extent[0], extent[1], 1)
1032 toTexture:texture_handle
1033 destinationSlice:face_index
1034 destinationLevel:mip
1035 destinationOrigin:MTLOriginMake(offset[0], offset[1], 0)];
1036 texture_array_relative_offset += bytes_per_image;
1041 "TODO(Metal): Support compute texture update for GPU_TEXTURE_CUBE_ARRAY %d, %d, "
1061 if (use_staging_texture) {
1073 for (
int array_index = base_slice; array_index < final_slice; array_index++) {
1074 [blit_encoder copyFromTexture:staging_texture
1075 sourceSlice:array_index
1077 sourceOrigin:MTLOriginMake(offset[0], 0, 0)
1078 sourceSize:MTLSizeMake(extent[0], 1, 1)
1080 destinationSlice:array_index
1081 destinationLevel:mip
1082 destinationOrigin:MTLOriginMake(offset[0], 0, 0)];
1089 for (
int array_index = base_slice; array_index < final_slice; array_index++) {
1090 [blit_encoder copyFromTexture:staging_texture
1091 sourceSlice:array_index
1093 sourceOrigin:MTLOriginMake(offset[0], offset[1], 0)
1094 sourceSize:MTLSizeMake(extent[0], extent[1], 1)
1096 destinationSlice:array_index
1097 destinationLevel:mip
1098 destinationOrigin:MTLOriginMake(offset[0], offset[1], 0)];
1102 [blit_encoder copyFromTexture:staging_texture
1105 sourceOrigin:MTLOriginMake(offset[0], offset[1], offset[2])
1106 sourceSize:MTLSizeMake(extent[0], extent[1], extent[2])
1109 destinationLevel:mip
1110 destinationOrigin:MTLOriginMake(offset[0], offset[1], offset[2])];
1115 for (
int i = 0; i < extent[2]; i++) {
1116 int face_index = offset[2] + i;
1117 [blit_encoder copyFromTexture:staging_texture
1118 sourceSlice:face_index
1120 sourceOrigin:MTLOriginMake(offset[0], offset[1], 0)
1121 sourceSize:MTLSizeMake(extent[0], extent[1], 1)
1123 destinationSlice:face_index
1124 destinationLevel:mip
1125 destinationOrigin:MTLOriginMake(offset[0], offset[1], 0)];
1134 [staging_texture release];
1138 if (can_use_direct_blit) {
1141 if (texture_.storageMode == MTLStorageModeManaged) {
1142 [blit_encoder synchronizeResource:texture_];
1144 [blit_encoder optimizeContentsForGPUAccess:texture_];
1150 if (texture_.storageMode == MTLStorageModeManaged) {
1152 [blit_encoder synchronizeResource:texture_];
1154 [blit_encoder optimizeContentsForGPUAccess:texture_];
1158 [texture_handle release];
1165 temp_allocation->
free();
1172 GPUPixelBuffer *pixbuf)
1182 if (buffer == nil) {
1187 this->ensure_baked();
1193 size_t bits_per_pixel = num_image_channels *
to_bytesize(format);
1194 size_t bytes_per_row = bits_per_pixel * extent[0];
1195 size_t bytes_per_image = bytes_per_row * extent[1];
1205 [blit_encoder copyFromBuffer:buffer
1207 sourceBytesPerRow:bytes_per_row
1208 sourceBytesPerImage:bytes_per_image
1209 sourceSize:MTLSizeMake(extent[0], extent[1], 1)
1213 destinationOrigin:MTLOriginMake(offset[0], offset[1], 0)];
1215 if (texture_.storageMode == MTLStorageModeManaged) {
1216 [blit_encoder synchronizeResource:texture_];
1218 [blit_encoder optimizeContentsForGPUAccess:texture_];
1225void gpu::MTLTexture::ensure_mipmaps(
int miplvl)
1228 BLI_assert(resource_mode_ != MTL_TEXTURE_MODE_TEXTURE_VIEW);
1233 int max_dimension = std::max({w_, effective_h, effective_d});
1234 int max_miplvl =
floor(
log2(max_dimension));
1235 miplvl =
min_ii(max_miplvl, miplvl);
1238 if (mipmaps_ < miplvl) {
1242 if (is_baked_ && mipmaps_ > mtl_max_mips_) {
1244 "Texture requires a higher mipmap level count. Please specify the required "
1247 MTL_LOG_WARNING(
"Texture requires regenerating due to increase in mip-count");
1250 this->mip_range_set(0, mipmaps_ - 1);
1266 MTL_LOG_ERROR(
"Cannot Generate mip-maps -- metal device invalid\n");
1272 this->ensure_mipmaps(mtl_max_mips_);
1275 this->ensure_baked();
1276 BLI_assert_msg(is_baked_ && texture_,
"MTLTexture is not valid");
1278 if (
mipmaps_ == 1 || mtl_max_mips_ == 1) {
1284 if (
format_ == TextureFormat::SFLOAT_32_DEPTH ||
format_ == TextureFormat::UNORM_16_DEPTH ||
1285 format_ == TextureFormat::SFLOAT_32_DEPTH_UINT_8)
1287 MTL_LOG_WARNING(
"Cannot generate mipmaps for textures using DEPTH formats");
1295 [enc insertDebugSignpost:
@"Generate MipMaps"];
1297 [enc generateMipmapsForTexture:texture_];
1298 has_generated_mips_ =
true;
1308 std::max(
d_, 1) == std::max(mt_dst->
d_, 1));
1310 (
format_ == TextureFormat::SRGBA_8_8_8_8 &&
1311 mt_dst->
format_ == TextureFormat::UNORM_8_8_8_8) ||
1312 (
format_ == TextureFormat::UNORM_8_8_8_8 &&
1313 mt_dst->
format_ == TextureFormat::SRGBA_8_8_8_8));
1321 this->ensure_baked();
1328 switch (mt_dst->
type_) {
1339 for (
int mip : mip_levels) {
1341 int extent[3] = {1, 1, 1};
1345 this->blit(blit_encoder,
1376 this->ensure_baked();
1380 if (backing_buffer_ !=
nullptr) {
1384 bool fast_buf_clear = (u32 == ((u32 << 8) | (u32 >> 24)));
1386 fast_buf_clear &= u32[i] == u32[0];
1389 if (fast_buf_clear) {
1397 [blit_encoder fillBuffer:backing_buffer_->get_metal_buffer()
1398 range:NSMakeRange(0, backing_buffer_->get_size())
1403 "Non-repeating-byte-pattern clear for buffer-backed textures not supported!");
1411 if (resource_mode_ == MTL_TEXTURE_MODE_TEXTURE_VIEW &&
source_texture_) {
1412 id<MTLTexture> clear_texture = (mip_swizzle_view_ != nil) ? mip_swizzle_view_ : texture_;
1413 if (!(clear_texture.usage & MTLTextureUsageRenderTarget)) {
1435 return MTLTextureSwizzleRed;
1438 return MTLTextureSwizzleGreen;
1441 return MTLTextureSwizzleBlue;
1444 return MTLTextureSwizzleAlpha;
1446 return MTLTextureSwizzleZero;
1448 return MTLTextureSwizzleOne;
1454 if (memcmp(tex_swizzle_mask_, swizzle_mask, 4) != 0) {
1455 memcpy(tex_swizzle_mask_, swizzle_mask, 4);
1458 MTLTextureSwizzleChannels new_swizzle_mask = MTLTextureSwizzleChannelsMake(
1464 mtl_swizzle_mask_ = new_swizzle_mask;
1465 texture_view_dirty_flags_ |= TEXTURE_VIEW_SWIZZLE_DIRTY;
1469void gpu::MTLTexture::mip_range_set(
int min,
int max)
1488 " MTLTexture of type TEXTURE_1D_ARRAY or TEXTURE_BUFFER cannot have a mipcount "
1489 "greater than 1\n");
1496 texture_view_dirty_flags_ |= TEXTURE_VIEW_MIP_DIRTY;
1506 int extent[3] = {1, 1, 1};
1514 this->read_internal(
1515 mip, 0, 0, 0, extent[0], extent[1], extent[2], type, num_channels, texture_size,
data);
1519 MTL_LOG_WARNING(
"MTLTexture::read - reading from texture with no image data");
1524void gpu::MTLTexture::read_internal(
int mip,
1532 int num_output_components,
1533 size_t debug_data_size,
1538 MTL_LOG_WARNING(
"gpu::MTLTexture::read_internal - Trying to read from a non-baked texture!");
1547 BLI_assert(num_output_components <= num_channels);
1548 size_t desired_output_bpp = num_output_components *
to_bytesize(desired_output_format);
1557 bool format_conversion_needed = (data_format != desired_output_format);
1558 bool can_use_simple_read = (desired_output_bpp == image_bpp) && (!format_conversion_needed) &&
1559 (num_output_components == image_components);
1563 if (is_depth_format) {
1564 can_use_simple_read =
false;
1566 image_components = 1;
1579 BLI_assert(format_ == TextureFormat::UFLOAT_11_11_10 ||
1580 format_ == TextureFormat::UNORM_10_10_10_2 ||
1581 format_ == TextureFormat::UINT_10_10_10_2);
1584 image_bpp =
sizeof(int);
1585 image_components = 1;
1586 desired_output_bpp =
sizeof(int);
1587 num_output_components = 1;
1590 format_conversion_needed =
false;
1591 can_use_simple_read =
true;
1595 size_t bytes_per_row = desired_output_bpp * width;
1596 size_t bytes_per_image = bytes_per_row * height;
1597 size_t total_bytes = bytes_per_image *
max_ii(depth, 1);
1599 if (can_use_simple_read) {
1603 ((num_output_components *
to_bytesize(desired_output_format)) == desired_output_bpp) &&
1604 (desired_output_bpp == image_bpp));
1612 total_bytes, 256,
true);
1615 id<MTLBuffer> destination_buffer = dest_buf->get_metal_buffer();
1617 void *destination_buffer_host_ptr = dest_buf->get_host_ptr();
1618 BLI_assert(destination_buffer_host_ptr !=
nullptr);
1621 int depth_format_mode = 0;
1622 if (is_depth_format) {
1623 depth_format_mode = 1;
1624 switch (desired_output_format) {
1626 depth_format_mode = 1;
1629 depth_format_mode = 2;
1632 depth_format_mode = 4;
1640 TextureReadRoutineSpecialization compute_specialization_kernel = {
1644 num_output_components,
1647 bool copy_successful =
false;
1655 id<MTLTexture> read_texture = texture_;
1657 if (resource_mode_ == MTL_TEXTURE_MODE_TEXTURE_VIEW) {
1658 read_texture = this->get_metal_handle();
1661 if (format_ == TextureFormat::SRGBA_8_8_8_8) {
1663 read_texture = [read_texture newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm];
1669 if (can_use_simple_read) {
1673 [enc insertDebugSignpost:
@"GPUTextureRead1D"];
1675 [enc copyFromTexture:read_texture
1678 sourceOrigin:MTLOriginMake(x_off, 0, 0)
1679 sourceSize:MTLSizeMake(width, 1, 1)
1680 toBuffer:destination_buffer
1682 destinationBytesPerRow:bytes_per_row
1683 destinationBytesPerImage:bytes_per_image];
1684 copy_successful =
true;
1689 id<MTLComputeCommandEncoder> compute_encoder =
1691 id<MTLComputePipelineState> pso = texture_read_1d_get_kernel(
1692 compute_specialization_kernel);
1693 TextureReadParams params = {
1702 cs.bind_compute_bytes(¶ms,
sizeof(params), 0);
1703 cs.bind_compute_buffer(destination_buffer, 0, 1);
1704 cs.bind_compute_texture(read_texture, 0);
1705 [compute_encoder dispatchThreads:MTLSizeMake(width, 1, 1)
1706 threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
1707 copy_successful =
true;
1712 if (can_use_simple_read) {
1716 [enc insertDebugSignpost:
@"GPUTextureRead1DArray"];
1719 int base_slice = y_off;
1720 int final_slice = base_slice + height;
1721 size_t texture_array_relative_offset = 0;
1723 for (
int array_slice = base_slice; array_slice < final_slice; array_slice++) {
1724 [enc copyFromTexture:read_texture
1725 sourceSlice:base_slice
1727 sourceOrigin:MTLOriginMake(x_off, 0, 0)
1728 sourceSize:MTLSizeMake(width, 1, 1)
1729 toBuffer:destination_buffer
1730 destinationOffset:texture_array_relative_offset
1731 destinationBytesPerRow:bytes_per_row
1732 destinationBytesPerImage:bytes_per_row];
1733 texture_array_relative_offset += bytes_per_row;
1735 copy_successful =
true;
1739 id<MTLComputeCommandEncoder> compute_encoder =
1741 id<MTLComputePipelineState> pso = texture_read_1d_array_get_kernel(
1742 compute_specialization_kernel);
1743 TextureReadParams params = {
1752 cs.bind_compute_bytes(¶ms,
sizeof(params), 0);
1753 cs.bind_compute_buffer(destination_buffer, 0, 1);
1754 cs.bind_compute_texture(read_texture, 0);
1755 [compute_encoder dispatchThreads:MTLSizeMake(width, height, 1)
1756 threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
1757 copy_successful =
true;
1762 if (can_use_simple_read) {
1766 [enc insertDebugSignpost:
@"GPUTextureRead2D"];
1768 [enc copyFromTexture:read_texture
1771 sourceOrigin:MTLOriginMake(x_off, y_off, 0)
1772 sourceSize:MTLSizeMake(width, height, 1)
1773 toBuffer:destination_buffer
1775 destinationBytesPerRow:bytes_per_row
1776 destinationBytesPerImage:bytes_per_image];
1777 copy_successful =
true;
1782 id<MTLComputeCommandEncoder> compute_encoder =
1784 id<MTLComputePipelineState> pso = texture_read_2d_get_kernel(
1785 compute_specialization_kernel);
1786 TextureReadParams params = {
1795 cs.bind_compute_bytes(¶ms,
sizeof(params), 0);
1796 cs.bind_compute_buffer(destination_buffer, 0, 1);
1797 cs.bind_compute_texture(read_texture, 0);
1798 [compute_encoder dispatchThreads:MTLSizeMake(width, height, 1)
1799 threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
1800 copy_successful =
true;
1805 if (can_use_simple_read) {
1809 [enc insertDebugSignpost:
@"GPUTextureRead2DArray"];
1811 int base_slice = z_off;
1812 int final_slice = base_slice + depth;
1813 size_t texture_array_relative_offset = 0;
1815 for (
int array_slice = base_slice; array_slice < final_slice; array_slice++) {
1816 [enc copyFromTexture:read_texture
1817 sourceSlice:array_slice
1819 sourceOrigin:MTLOriginMake(x_off, y_off, 0)
1820 sourceSize:MTLSizeMake(width, height, 1)
1821 toBuffer:destination_buffer
1822 destinationOffset:texture_array_relative_offset
1823 destinationBytesPerRow:bytes_per_row
1824 destinationBytesPerImage:bytes_per_image];
1825 texture_array_relative_offset += bytes_per_image;
1827 copy_successful =
true;
1832 id<MTLComputeCommandEncoder> compute_encoder =
1834 id<MTLComputePipelineState> pso = texture_read_2d_array_get_kernel(
1835 compute_specialization_kernel);
1836 TextureReadParams params = {
1838 {width, height, depth},
1839 {x_off, y_off, z_off},
1845 cs.bind_compute_bytes(¶ms,
sizeof(params), 0);
1846 cs.bind_compute_buffer(destination_buffer, 0, 1);
1847 cs.bind_compute_texture(read_texture, 0);
1849 dispatchThreads:MTLSizeMake(width, height, depth)
1850 threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
1851 copy_successful =
true;
1856 if (can_use_simple_read) {
1860 [enc insertDebugSignpost:
@"GPUTextureRead3D"];
1862 [enc copyFromTexture:read_texture
1865 sourceOrigin:MTLOriginMake(x_off, y_off, z_off)
1866 sourceSize:MTLSizeMake(width, height, depth)
1867 toBuffer:destination_buffer
1869 destinationBytesPerRow:bytes_per_row
1870 destinationBytesPerImage:bytes_per_image];
1871 copy_successful =
true;
1876 id<MTLComputeCommandEncoder> compute_encoder =
1878 id<MTLComputePipelineState> pso = texture_read_3d_get_kernel(
1879 compute_specialization_kernel);
1880 TextureReadParams params = {
1882 {width, height, depth},
1883 {x_off, y_off, z_off},
1889 cs.bind_compute_bytes(¶ms,
sizeof(params), 0);
1890 cs.bind_compute_buffer(destination_buffer, 0, 1);
1891 cs.bind_compute_texture(read_texture, 0);
1893 dispatchThreads:MTLSizeMake(width, height, depth)
1894 threadsPerThreadgroup:MTLSizeMake(4, 4, 4)];
1895 copy_successful =
true;
1902 "z_off > 0 is only supported by TEXTURE CUBE ARRAY reads.");
1904 "depth > 6 is only supported by TEXTURE CUBE ARRAY reads. ");
1905 if (can_use_simple_read) {
1908 [enc insertDebugSignpost:
@"GPUTextureReadCubeArray"];
1912 int base_slice = z_off;
1913 int final_slice = base_slice + depth;
1914 size_t texture_array_relative_offset = 0;
1916 for (
int array_slice = base_slice; array_slice < final_slice; array_slice++) {
1917 [enc copyFromTexture:read_texture
1918 sourceSlice:array_slice
1920 sourceOrigin:MTLOriginMake(x_off, y_off, 0)
1921 sourceSize:MTLSizeMake(width, height, 1)
1922 toBuffer:destination_buffer
1923 destinationOffset:texture_array_relative_offset
1924 destinationBytesPerRow:bytes_per_row
1925 destinationBytesPerImage:bytes_per_image];
1927 texture_array_relative_offset += bytes_per_image;
1929 MTL_LOG_DEBUG(
"Copying texture data to buffer GPU_TEXTURE_CUBE_ARRAY");
1930 copy_successful =
true;
1933 MTL_LOG_ERROR(
"TODO(Metal): unsupported compute copy of texture cube array");
1939 "gpu::MTLTexture::read_internal simple-copy not yet supported for texture "
1945 if (copy_successful) {
1948 if (dest_buf->get_resource_options() == MTLResourceStorageModeManaged) {
1951 [enc insertDebugSignpost:
@"GPUTextureRead-syncResource"];
1953 [enc synchronizeResource:destination_buffer];
1960 memcpy(r_data, destination_buffer_host_ptr, total_bytes);
1961 MTL_LOG_DEBUG(
"gpu::MTLTexture::read_internal success! %lu bytes read", total_bytes);
1965 "gpu::MTLTexture::read_internal not yet supported for this config -- data "
1966 "format different (src %lu bytes, dst %lu bytes) (src format: %d, dst format: %d), or "
1967 "varying component counts (src %d, dst %d)",
1971 (
int)desired_output_format,
1973 num_output_components);
1983 this->prepare_internal();
1998 this->mip_range_set(0, 0);
2018 size_t bytes_per_row = bytes_per_pixel *
w_;
2022 [mtl_ctx->
device minimumLinearTextureAlignmentForPixelFormat:mtl_format]);
2026 if (format->stride > bytes_per_pixel && format->attr_len > 1) {
2032 if (bytes_per_pixel * format->attr_len != format->stride) {
2034 "Cannot split attributes across multiple pixels as attribute format sizes do "
2041 bytes_per_row *= format->attr_len;
2045 w_ *= format->attr_len;
2048 "Image should contain one pixel for each attribute in every vertex.");
2052 BLI_assert(bytes_per_pixel == format->stride &&
2053 "Pixel format stride MUST match the texture format stride -- These being different "
2054 "is likely caused by Metal's VBO padding to a minimum of 4-bytes per-vertex."
2055 " If multiple attributes are used. Each attribute is to be packed into its own "
2056 "individual pixel when stride length is exceeded. ");
2061 texture_descriptor_ = [[MTLTextureDescriptor alloc]
init];
2062 texture_descriptor_.pixelFormat = mtl_format;
2063 texture_descriptor_.textureType = MTLTextureTypeTextureBuffer;
2064 texture_descriptor_.width =
w_;
2065 texture_descriptor_.height = 1;
2066 texture_descriptor_.depth = 1;
2067 texture_descriptor_.arrayLength = 1;
2068 texture_descriptor_.mipmapLevelCount = mtl_max_mips_;
2069 texture_descriptor_.usage =
2070 MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite |
2071 MTLTextureUsagePixelFormatView;
2072 texture_descriptor_.storageMode = [source_buffer storageMode];
2073 texture_descriptor_.sampleCount = 1;
2074 texture_descriptor_.cpuCacheMode = [source_buffer cpuCacheMode];
2075 texture_descriptor_.hazardTrackingMode = [source_buffer hazardTrackingMode];
2077 texture_ = [source_buffer
2078 newTextureWithDescriptor:texture_descriptor_
2081 aligned_w_ = bytes_per_row / bytes_per_pixel;
2084 texture_.label = [NSString stringWithUTF8String:this->
get_name()];
2087 resource_mode_ = MTL_TEXTURE_MODE_VBO;
2090 vert_buffer_ = mtl_vbo;
2091 vert_buffer_mtl_ = source_buffer;
2101 this->prepare_internal();
2104 resource_mode_ = MTL_TEXTURE_MODE_TEXTURE_VIEW;
2105 texture_view_dirty_flags_ |= TEXTURE_VIEW_MIP_DIRTY;
2112 mtltex->ensure_baked();
2113 texture_ = mtltex->texture_;
2122 texture_view_stencil_ =
false;
2125 texture_view_stencil_ =
true;
2129 bake_mip_swizzle_view();
2145void gpu::MTLTexture::prepare_internal()
2149 internal_gpu_image_usage_flags_ = gpu_image_usage_flags_;
2172 mtl_max_mips_ = mipmaps_;
2176void gpu::MTLTexture::ensure_baked()
2180 id<MTLTexture> previous_texture = nil;
2181 bool copy_previous_contents =
false;
2183 if (is_baked_ && is_dirty_) {
2184 copy_previous_contents =
true;
2185 previous_texture = texture_;
2186 [previous_texture retain];
2195 BLI_assert(resource_mode_ != MTL_TEXTURE_MODE_EXTERNAL);
2196 BLI_assert(resource_mode_ != MTL_TEXTURE_MODE_TEXTURE_VIEW);
2197 BLI_assert(resource_mode_ != MTL_TEXTURE_MODE_VBO);
2205 if (format_ == TextureFormat::SRGBA_8_8_8_8) {
2216 texture_descriptor_ = [[MTLTextureDescriptor alloc]
init];
2217 texture_descriptor_.pixelFormat = mtl_format;
2220 texture_descriptor_.width = w_;
2221 texture_descriptor_.height = 1;
2222 texture_descriptor_.depth = 1;
2224 texture_descriptor_.mipmapLevelCount = (mtl_max_mips_ > 0) ? mtl_max_mips_ : 1;
2226 texture_descriptor_.storageMode = MTLStorageModePrivate;
2227 texture_descriptor_.sampleCount = 1;
2228 texture_descriptor_.cpuCacheMode = MTLCPUCacheModeDefaultCache;
2229 texture_descriptor_.hazardTrackingMode = MTLHazardTrackingModeDefault;
2236 texture_descriptor_ = [[MTLTextureDescriptor alloc]
init];
2237 texture_descriptor_.pixelFormat = mtl_format;
2240 texture_descriptor_.width = w_;
2241 texture_descriptor_.height = h_;
2242 texture_descriptor_.depth = 1;
2244 texture_descriptor_.mipmapLevelCount = (mtl_max_mips_ > 0) ? mtl_max_mips_ : 1;
2246 texture_descriptor_.storageMode = MTLStorageModePrivate;
2247 texture_descriptor_.sampleCount = 1;
2248 texture_descriptor_.cpuCacheMode = MTLCPUCacheModeDefaultCache;
2249 texture_descriptor_.hazardTrackingMode = MTLHazardTrackingModeDefault;
2255 texture_descriptor_ = [[MTLTextureDescriptor alloc]
init];
2256 texture_descriptor_.pixelFormat = mtl_format;
2257 texture_descriptor_.textureType = MTLTextureType3D;
2258 texture_descriptor_.width = w_;
2259 texture_descriptor_.height = h_;
2260 texture_descriptor_.depth = d_;
2261 texture_descriptor_.arrayLength = 1;
2262 texture_descriptor_.mipmapLevelCount = (mtl_max_mips_ > 0) ? mtl_max_mips_ : 1;
2264 texture_descriptor_.storageMode = MTLStorageModePrivate;
2265 texture_descriptor_.sampleCount = 1;
2266 texture_descriptor_.cpuCacheMode = MTLCPUCacheModeDefaultCache;
2267 texture_descriptor_.hazardTrackingMode = MTLHazardTrackingModeDefault;
2276 texture_descriptor_ = [[MTLTextureDescriptor alloc]
init];
2277 texture_descriptor_.pixelFormat = mtl_format;
2279 MTLTextureTypeCubeArray :
2281 texture_descriptor_.width = w_;
2282 texture_descriptor_.height = h_;
2283 texture_descriptor_.depth = 1;
2285 texture_descriptor_.mipmapLevelCount = (mtl_max_mips_ > 0) ? mtl_max_mips_ : 1;
2287 texture_descriptor_.storageMode = MTLStorageModePrivate;
2288 texture_descriptor_.sampleCount = 1;
2289 texture_descriptor_.cpuCacheMode = MTLCPUCacheModeDefaultCache;
2290 texture_descriptor_.hazardTrackingMode = MTLHazardTrackingModeDefault;
2295 texture_descriptor_ = [[MTLTextureDescriptor alloc]
init];
2296 texture_descriptor_.pixelFormat = mtl_format;
2297 texture_descriptor_.textureType = MTLTextureTypeTextureBuffer;
2298 texture_descriptor_.width = w_;
2299 texture_descriptor_.height = 1;
2300 texture_descriptor_.depth = 1;
2301 texture_descriptor_.arrayLength = 1;
2302 texture_descriptor_.mipmapLevelCount = (mtl_max_mips_ > 0) ? mtl_max_mips_ : 1;
2304 texture_descriptor_.storageMode = MTLStorageModePrivate;
2305 texture_descriptor_.sampleCount = 1;
2306 texture_descriptor_.cpuCacheMode = MTLCPUCacheModeDefaultCache;
2307 texture_descriptor_.hazardTrackingMode = MTLHazardTrackingModeDefault;
2311 MTL_LOG_ERROR(
"[METAL] Error: Cannot create texture with unknown type: %d\n", type_);
2317 resource_mode_ = MTL_TEXTURE_MODE_DEFAULT;
2323 if (is_tile_based_arch) {
2324 texture_descriptor_.storageMode = MTLStorageModeMemoryless;
2339 "Texture atomic fallback support is only available for GPU_TEXTURE_2D, "
2340 "GPU_TEXTURE_2D_ARRAY and GPU_TEXTURE_3D.");
2349 const uint max_width = 16384;
2350 const uint max_height = 16384;
2351 const uint pixels_res = w_ * h_ * d_;
2353 uint new_w = 0, new_h = 0;
2354 if (pixels_res <= max_width) {
2360 new_h = ((pixels_res % new_w) == 0) ? (pixels_res / new_w) : ((pixels_res / new_w) + 1);
2363 texture_descriptor_.width = new_w;
2364 texture_descriptor_.height = new_h;
2368 texture_descriptor_.height <= max_height,
2369 "Atomic fallback support texture is too large.");
2374 size_t bytes_per_row = bytes_per_pixel * texture_descriptor_.width;
2375 size_t total_bytes = bytes_per_row * texture_descriptor_.height;
2382 texture_descriptor_.textureType = MTLTextureType2D;
2383 texture_descriptor_.depth = 1;
2384 texture_descriptor_.arrayLength = 1;
2388 tex_buffer_metadata_[0] = w_;
2389 tex_buffer_metadata_[1] = h_;
2390 tex_buffer_metadata_[2] = d_;
2395 [ctx->device minimumLinearTextureAlignmentForPixelFormat:mtl_format]);
2397 texture_ = [backing_buffer_->get_metal_buffer()
2398 newTextureWithDescriptor:texture_descriptor_
2400 bytesPerRow:aligned_bytes_per_row];
2402 tex_buffer_metadata_[3] = bytes_per_row / bytes_per_pixel;
2405 texture_.label = [NSString
2406 stringWithFormat:
@"AtomicBufferBackedTexture_%s", this->
get_name()];
2412 texture_ = [ctx->device newTextureWithDescriptor:texture_descriptor_];
2416 texture_.label = [NSString stringWithFormat:
@"MemorylessTexture_%s", this->
get_name()];
2419 texture_.label = [NSString stringWithFormat:
@"Texture_%s", this->
get_name()];
2430 if (copy_previous_contents) {
2432 [previous_texture release];
2436void gpu::MTLTexture::reset()
2440 if (texture_ != nil) {
2448 if (backing_buffer_ !=
nullptr) {
2449 backing_buffer_->free();
2450 backing_buffer_ =
nullptr;
2454 if (storage_buffer_ !=
nullptr) {
2455 delete storage_buffer_;
2456 storage_buffer_ =
nullptr;
2459 if (texture_no_srgb_ != nil) {
2460 [texture_no_srgb_ release];
2461 texture_no_srgb_ = nil;
2464 if (mip_swizzle_view_ != nil) {
2465 [mip_swizzle_view_ release];
2466 mip_swizzle_view_ = nil;
2476 if (texture_descriptor_ !=
nullptr) {
2477 [texture_descriptor_ release];
2478 texture_descriptor_ =
nullptr;
2482 has_generated_mips_ =
false;
2496 backing_buffer_ !=
nullptr,
2497 "Resource must have been created as a buffer backed resource to support SSBO wrapping.");
2499 this->ensure_baked();
2500 if (storage_buffer_ == nil) {
2506 return storage_buffer_;
2517 return (
format_ == TextureFormat::SRGBA_8_8_8_8);
2524 if (texture_no_srgb_ == nil) {
2525 texture_no_srgb_ = [base_tex newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm];
2527 return texture_no_srgb_;
2559 MTLResourceOptions resource_options = ([ctx->
device hasUnifiedMemory]) ?
2560 MTLResourceStorageModeShared :
2561 MTLResourceStorageModeManaged;
2563 if (buffer_ != nil) {
2568 buffer_ = new_buffer;
2574 return [buffer_ contents];
2579 if (buffer_ == nil) {
2584 if (buffer_.resourceOptions & MTLResourceStorageModeManaged) {
2585 [buffer_ didModifyRange:NSMakeRange(0,
size_)];
2596 if (![ctx->
device hasUnifiedMemory]) {
2597 return native_handle;
2605 return native_handle;
#define GPU_batch_texture_bind(batch, name, tex)
#define GPU_ATTACHMENT_NONE
#define GPU_framebuffer_ensure_config(_fb,...)
#define GPU_ATTACHMENT_TEXTURE_LAYER_MIP(_texture, _layer, _mip)
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
constexpr T * data() const
constexpr Span slice(int64_t start, int64_t size) const
constexpr const T * data() const
StateManager * state_manager
static MTLCapabilities & get_capabilities()
gpu::MTLBuffer * allocate_with_data(uint64_t size, bool cpu_visible, const void *data=nullptr)
gpu::MTLBuffer * allocate(uint64_t size, bool cpu_visible)
gpu::MTLBuffer * allocate_aligned(uint64_t size, uint alignment, bool cpu_visible)
id< MTLBuffer > get_metal_buffer() const
id< MTLComputeCommandEncoder > ensure_begin_compute_encoder()
MTLComputeState & get_compute_state()
id< MTLBlitCommandEncoder > ensure_begin_blit_encoder()
void bind_compute_bytes(const void *bytes, uint64_t length, uint index)
void bind_compute_texture(id< MTLTexture > tex, uint slot)
void bind_compute_buffer(id< MTLBuffer > buffer, uint64_t buffer_offset, uint index)
void bind_pso(id< MTLComputePipelineState > pso)
static MTLContext * get()
MTLCommandBufferManager main_command_buffer
static MTLBufferPool * get_global_memory_manager()
GPUPixelBufferNativeHandle get_native_handle() override
id< MTLBuffer > get_metal_buffer()
size_t get_size() override
MTLPixelBuffer(size_t size)
MTLStorageBuf(size_t size, GPUUsageType usage, const char *name)
void update_sub(int mip, int offset[3], int extent[3], eGPUDataFormat type, const void *data, const uint unpack_row_length) override
id< MTLTexture > get_non_srgb_handle()
id< MTLTexture > get_metal_handle()
MTLTexture(const char *name)
friend class MTLStorageBuf
MTLSamplerState get_sampler_state()
void read(int mip, eGPUDataFormat type, void *data) override
id< MTLTexture > get_metal_handle_base()
MTLStorageBuf * get_storagebuf()
void generate_mipmap() override
bool init_internal() override
void swizzle_set(const char swizzle_mask[4]) override
void clear(const double4 data) override
void copy_to(Texture *dst, IndexRange mip_levels) override
virtual void texture_unbind(Texture *tex)=0
GPUSamplerState sampler_state
Texture * source_texture_
eGPUTextureUsage gpu_image_usage_flags_
size_t read_size_get(int mip, eGPUDataFormat format) const
bool init_2D(int w, int h, int layers, int mip_len, TextureFormat format)
int dimensions_count() const
GPUAttachmentType attachment_type(int slot) const
void mip_size_get(int mip, int r_size[3]) const
TextureFormat format_get() const
Texture(const char *name)
GPUTextureFormatFlag format_flag_
CCL_NAMESPACE_BEGIN struct Options options
float length(VecOp< float, D >) RET
#define MTL_LOG_WARNING(info,...)
#define MTL_LOG_DEBUG(info,...)
#define MTL_LOG_ERROR(info,...)
std::string get_name(const VolumeGridData &grid)
size_t get_mtl_format_bytesize(MTLPixelFormat tex_format)
MTLPixelFormat gpu_texture_format_to_metal(TextureFormat tex_format)
bool is_half_float(TextureFormat format)
std::string tex_data_format_to_msl_type_str(eGPUDataFormat type)
eGPUDataFormat to_texture_data_format(TextureFormat tex_format)
std::string tex_data_format_to_msl_texture_template_type(eGPUDataFormat type)
MTLPixelFormat mtl_format_get_writeable_view_format(MTLPixelFormat format)
static MTLTextureSwizzle swizzle_to_mtl(const char swizzle)
BLI_INLINE MTLTexture * unwrap(Texture *tex)
size_t to_block_size(TextureFormat data_type)
int get_mtl_format_num_components(MTLPixelFormat tex_format)
int to_bytesize(const DataFormat format)
MTLTextureUsage mtl_usage_from_gpu(eGPUTextureUsage usage)
int to_component_len(TextureFormat format)
constexpr bool validate_data_format(TextureFormat tex_format, eGPUDataFormat data_format)
MTLTextureType to_metal_type(GPUTextureType type)
eGPUTextureUsage gpu_usage_from_mtl(MTLTextureUsage mtl_usage)
void float_to_half_make_finite_array(const float *src, uint16_t *dst, size_t length)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE unsigned long long max_ulul(unsigned long long a, unsigned long long b)
void GPU_scissor_test(bool enable)
gpu::FrameBuffer * GPU_framebuffer_active_get()
void GPU_shader_uniform_2f(gpu::Shader *sh, const char *name, float x, float y)
void GPU_framebuffer_restore()
eGPUTextureUsage GPU_texture_usage(const gpu::Texture *texture)
void GPU_framebuffer_free(gpu::FrameBuffer *fb)
GPUDepthTest GPU_depth_test_get()
BLI_INLINE float fb(float length, float L)
@ GPU_DATA_2_10_10_10_REV
@ GPU_DATA_UINT_24_8_DEPRECATED
GPUStencilTest GPU_stencil_test_get()
VecBase< uint32_t, 4 > uint4
void GPU_depth_mask(bool depth)
MINLINE int max_ii(int a, int b)
void GPU_batch_set_shader(gpu::Batch *batch, gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
const GPUVertFormat * GPU_vertbuf_get_format(const gpu::VertBuf *verts)
GPUFaceCullTest GPU_face_culling_get()
void GPU_stencil_reference_set(uint reference)
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
@ GPU_TEXTURE_USAGE_FORMAT_VIEW
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_TEXTURE_USAGE_HOST_READ
@ GPU_TEXTURE_USAGE_ATOMIC
@ GPU_TEXTURE_USAGE_ATTACHMENT
@ GPU_TEXTURE_USAGE_MEMORYLESS
BMesh const char void * data
GPUArchitectureType GPU_platform_architecture()
uint GPU_stencil_mask_get()
bool GPU_depth_mask_get()
void GPU_stencil_test(GPUStencilTest test)
void GPU_batch_draw(gpu::Batch *batch)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
gpu::FrameBuffer * GPU_framebuffer_create(const char *name)
gpu::Batch * GPU_batch_preset_quad()
MINLINE uint divide_ceil_u(uint a, uint b)
void GPU_face_culling(GPUFaceCullTest culling)
void GPU_stencil_write_mask_set(uint write_mask)
void GPU_blend(GPUBlend blend)
int GPU_max_texture_3d_size()
MINLINE int min_ii(int a, int b)
void GPU_depth_test(GPUDepthTest test)
VecBase< double, 4 > double4
bool supports_texture_atomics