@@ -405,6 +405,15 @@ _FORCE_INLINE_ MTLSize mipmapLevelSizeFromSize(MTLSize p_size, NSUInteger p_leve
405
405
RDD::TextureID RenderingDeviceDriverMetal::texture_create_shared (TextureID p_original_texture, const TextureView &p_view) {
406
406
id <MTLTexture > src_texture = rid::get (p_original_texture);
407
407
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
+
408
417
#if DEV_ENABLED
409
418
if (src_texture.sampleCount > 1 ) {
410
419
// 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
434
443
id <MTLTexture > obj = [src_texture newTextureViewWithPixelFormat: format
435
444
textureType: src_texture.textureType
436
445
levels: NSMakeRange (0 , src_texture.mipmapLevelCount)
437
- slices: NSMakeRange (0 , src_texture.arrayLength )
446
+ slices: NSMakeRange (0 , slices )
438
447
swizzle: swizzle];
439
448
ERR_FAIL_NULL_V_MSG (obj, TextureID (), " Unable to create shared texture" );
440
449
return rid::make (obj);
@@ -566,7 +575,14 @@ _FORCE_INLINE_ MTLSize mipmapLevelSizeFromSize(MTLSize p_size, NSUInteger p_leve
566
575
r_layout->size = get_image_format_required_size (format, sz.width , sz.height , sz.depth , 1 , &sbw, &sbh);
567
576
r_layout->row_pitch = r_layout->size / ((sbh / bh) * sz.depth );
568
577
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;
570
586
} else {
571
587
CRASH_NOW_MSG (" need to calculate layout for shared texture" );
572
588
}
0 commit comments