Skip to content

Commit 3d9b05a

Browse files
committed
Merge pull request godotengine#104341 from stuartcarnie/103602/metal_fix_cubemaps
Renderer: Fix Metal handling of cube textures; assert equal dimensions
2 parents 8f1d481 + e206629 commit 3d9b05a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

drivers/metal/rendering_device_driver_metal.mm

+18-2
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,15 @@ _FORCE_INLINE_ MTLSize mipmapLevelSizeFromSize(MTLSize p_size, NSUInteger p_leve
405405
RDD::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
}

servers/rendering/rendering_device.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,8 @@ RID RenderingDevice::texture_create(const TextureFormat &p_format, const Texture
912912
"Number of layers must be equal or greater than 1 for arrays and cubemaps.");
913913
ERR_FAIL_COND_V_MSG((format.texture_type == TEXTURE_TYPE_CUBE_ARRAY || format.texture_type == TEXTURE_TYPE_CUBE) && (format.array_layers % 6) != 0, RID(),
914914
"Cubemap and cubemap array textures must provide a layer number that is multiple of 6");
915+
ERR_FAIL_COND_V_MSG(((format.texture_type == TEXTURE_TYPE_CUBE_ARRAY || format.texture_type == TEXTURE_TYPE_CUBE)) && (format.width != format.height), RID(),
916+
"Cubemap and cubemap array textures must have equal width and height.");
915917
ERR_FAIL_COND_V_MSG(format.array_layers > driver->limit_get(LIMIT_MAX_TEXTURE_ARRAY_LAYERS), RID(), "Number of layers exceeds device maximum.");
916918
} else {
917919
format.array_layers = 1;

0 commit comments

Comments
 (0)