Skip to content

Commit d1abb63

Browse files
committed
1 parent aa06e9e commit d1abb63

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

drivers/metal/rendering_device_driver_metal.mm

+19-5
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,25 @@ _FORCE_INLINE_ MTLSize mipmapLevelSizeFromSize(MTLSize p_size, NSUInteger p_leve
358358
}
359359

360360
RDD::TextureID RenderingDeviceDriverMetal::texture_create_from_extension(uint64_t p_native_texture, TextureType p_type, DataFormat p_format, uint32_t p_array_layers, bool p_depth_stencil) {
361-
id<MTLTexture> obj = (__bridge id<MTLTexture>)(void *)(uintptr_t)p_native_texture;
362-
363-
// We only need to create a RDD::TextureID for an existing, natively-provided texture.
364-
365-
return rid::make(obj);
361+
id<MTLTexture> res = (__bridge id<MTLTexture>)(void *)(uintptr_t)p_native_texture;
362+
363+
// If the requested format is different, we need to create a view.
364+
MTLPixelFormat format = pixel_formats->getMTLPixelFormat(p_format);
365+
if (res.pixelFormat != format) {
366+
MTLTextureSwizzleChannels swizzle = MTLTextureSwizzleChannelsMake(
367+
MTLTextureSwizzleRed,
368+
MTLTextureSwizzleGreen,
369+
MTLTextureSwizzleBlue,
370+
MTLTextureSwizzleAlpha);
371+
res = [res newTextureViewWithPixelFormat:format
372+
textureType:res.textureType
373+
levels:NSMakeRange(0, res.mipmapLevelCount)
374+
slices:NSMakeRange(0, p_array_layers)
375+
swizzle:swizzle];
376+
ERR_FAIL_NULL_V_MSG(res, TextureID(), "Unable to create texture view.");
377+
}
378+
379+
return rid::make(res);
366380
}
367381

368382
RDD::TextureID RenderingDeviceDriverMetal::texture_create_shared(TextureID p_original_texture, const TextureView &p_view) {

0 commit comments

Comments
 (0)