@@ -405,6 +405,15 @@ _FORCE_INLINE_ MTLSize mipmapLevelSizeFromSize(MTLSize p_size, NSUInteger p_leve
405405RDD::TextureID RenderingDeviceDriverMetal::texture_create_shared (TextureID p_original_texture, const TextureView &p_view) {
406406 id <MTLTexture > src_texture = rid::get (p_original_texture);
407407
408+ NSUInteger slices = src_texture.arrayLength ;
409+ if (src_texture.textureType == MTLTextureTypeCube ) {
410+ // Metal expects Cube textures to have a slice count of 6.
411+ slices = 6 ;
412+ } else if (src_texture.textureType == MTLTextureTypeCubeArray ) {
413+ // Metal expects Cube Array textures to have 6 slices per layer.
414+ slices *= 6 ;
415+ }
416+
408417#if DEV_ENABLED
409418 if (src_texture.sampleCount > 1 ) {
410419 // TODO(sgc): is it ok to create a shared texture from a multi-sample texture?
@@ -434,7 +443,7 @@ _FORCE_INLINE_ MTLSize mipmapLevelSizeFromSize(MTLSize p_size, NSUInteger p_leve
434443 id <MTLTexture > obj = [src_texture newTextureViewWithPixelFormat: format
435444 textureType: src_texture.textureType
436445 levels: NSMakeRange (0 , src_texture.mipmapLevelCount)
437- slices: NSMakeRange (0 , src_texture.arrayLength )
446+ slices: NSMakeRange (0 , slices )
438447 swizzle: swizzle];
439448 ERR_FAIL_NULL_V_MSG (obj, TextureID (), " Unable to create shared texture" );
440449 return rid::make (obj);
@@ -566,7 +575,14 @@ _FORCE_INLINE_ MTLSize mipmapLevelSizeFromSize(MTLSize p_size, NSUInteger p_leve
566575 r_layout->size = get_image_format_required_size (format, sz.width , sz.height , sz.depth , 1 , &sbw, &sbh);
567576 r_layout->row_pitch = r_layout->size / ((sbh / bh) * sz.depth );
568577 r_layout->depth_pitch = r_layout->size / sz.depth ;
569- r_layout->layer_pitch = r_layout->size / obj.arrayLength ;
578+
579+ uint32_t array_length = obj.arrayLength ;
580+ if (obj.textureType == MTLTextureTypeCube ) {
581+ array_length = 6 ;
582+ } else if (obj.textureType == MTLTextureTypeCubeArray ) {
583+ array_length *= 6 ;
584+ }
585+ r_layout->layer_pitch = r_layout->size / array_length;
570586 } else {
571587 CRASH_NOW_MSG (" need to calculate layout for shared texture" );
572588 }
0 commit comments