Skip to content

Commit

Permalink
Enabled GetNativeTextureFormat/GetTextureFormatFromNative on Metal
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Dec 20, 2024
1 parent 96917d5 commit 5a34b0b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Graphics/GraphicsTools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ endif()

if(METAL_SUPPORTED)
list(APPEND SOURCE src/GraphicsUtilitiesMtl.mm)
list(APPEND DEPENDENCIES Diligent-GraphicsEngineMetalInterface)
list(APPEND DEPENDENCIES Diligent-GraphicsEngineMetal-static)
endif()

add_library(Diligent-GraphicsTools STATIC ${SOURCE} ${INCLUDE} ${INTERFACE})
Expand Down
15 changes: 15 additions & 0 deletions Graphics/GraphicsTools/src/GraphicsUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ int64_t GetNativeTextureFormatVk(TEXTURE_FORMAT TexFormat);
TEXTURE_FORMAT GetTextureFormatFromNativeVk(int64_t NativeFormat);
#endif

#if METAL_SUPPORTED
int64_t GetNativeTextureFormatMtl(TEXTURE_FORMAT TexFormat);
TEXTURE_FORMAT GetTextureFormatFromNativeMtl(int64_t NativeFormat);
#endif

#if WEBGPU_SUPPORTED
int64_t GetNativeTextureFormatWebGPU(TEXTURE_FORMAT TexFormat);
TEXTURE_FORMAT GetTextureFormatFromNativeWebGPU(int64_t NativeFormat);
Expand Down Expand Up @@ -597,6 +602,11 @@ int64_t GetNativeTextureFormat(TEXTURE_FORMAT TexFormat, RENDER_DEVICE_TYPE Devi
return GetNativeTextureFormatVk(TexFormat);
#endif

#if VULKAN_SUPPORTED
case RENDER_DEVICE_TYPE_METAL:
return GetNativeTextureFormatMtl(TexFormat);
#endif

#if WEBGPU_SUPPORTED
case RENDER_DEVICE_TYPE_WEBGPU:
return GetNativeTextureFormatWebGPU(TexFormat);
Expand Down Expand Up @@ -628,6 +638,11 @@ TEXTURE_FORMAT GetTextureFormatFromNative(int64_t NativeFormat, RENDER_DEVICE_TY
return GetTextureFormatFromNativeGL(NativeFormat);
#endif

#if VULKAN_SUPPORTED
case RENDER_DEVICE_TYPE_METAL:
return GetTextureFormatFromNativeMtl(NativeFormat);
#endif

#if VULKAN_SUPPORTED
case RENDER_DEVICE_TYPE_VULKAN:
return GetTextureFormatFromNativeVk(NativeFormat);
Expand Down
15 changes: 14 additions & 1 deletion Graphics/GraphicsTools/src/GraphicsUtilitiesMtl.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 Diligent Graphics LLC
* Copyright 2019-2024 Diligent Graphics LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,6 +34,9 @@
namespace Diligent
{

MTLPixelFormat TextureFormatToMTLPixelFormat(TEXTURE_FORMAT TexFormat);
TEXTURE_FORMAT MTLPixelFormatToTextureFormat(MTLPixelFormat mtlPixelFormat);

void CreateSparseTextureMtl(IRenderDevice* pDevice,
const TextureDesc& TexDesc,
IDeviceMemory* pMemory,
Expand All @@ -54,4 +57,14 @@ void CreateSparseTextureMtl(IRenderDevice* pDevice,
pDeviceMtl->CreateSparseTexture(TexDesc, pMemory, ppTexture);
}

int64_t GetNativeTextureFormatMtl(TEXTURE_FORMAT TexFormat)
{
return static_cast<int64_t>(TextureFormatToMTLPixelFormat(TexFormat));
}

TEXTURE_FORMAT GetTextureFormatFromNativeMtl(int64_t NativeFormat)
{
return MTLPixelFormatToTextureFormat(static_cast<MTLPixelFormat>(NativeFormat));
}

} // namespace Diligent
26 changes: 26 additions & 0 deletions Tests/DiligentCoreTest/src/GraphicsTools/GraphicsUtilitiesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ TEST(GraphicsUtilitiesTest, GetNativeTextureFormat_GetTextureFormatFromNative)
TEX_FORMAT_BGRX8_UNORM_SRGB,
};

const std::unordered_set<TEXTURE_FORMAT> SkipFormatsMtl{
TEX_FORMAT_RGB32_FLOAT,
TEX_FORMAT_RGB32_UINT,
TEX_FORMAT_RGB32_SINT,
TEX_FORMAT_R32_FLOAT_X8X24_TYPELESS,
TEX_FORMAT_X32_TYPELESS_G8X24_UINT,
TEX_FORMAT_R24_UNORM_X8_TYPELESS,
TEX_FORMAT_X24_TYPELESS_G8_UINT,
TEX_FORMAT_A8_UNORM,
TEX_FORMAT_R1_UNORM,
TEX_FORMAT_RG8_B8G8_UNORM,
TEX_FORMAT_G8R8_G8B8_UNORM,
TEX_FORMAT_B5G6R5_UNORM,
TEX_FORMAT_B5G5R5A1_UNORM,
TEX_FORMAT_BGRX8_UNORM,
TEX_FORMAT_R10G10B10_XR_BIAS_A2_UNORM,
TEX_FORMAT_BGRX8_UNORM_SRGB,
};

const std::unordered_set<TEXTURE_FORMAT> SkipFormatsWebGPU{
TEX_FORMAT_RGB32_FLOAT,
TEX_FORMAT_RGB32_UINT,
Expand Down Expand Up @@ -142,6 +161,13 @@ TEST(GraphicsUtilitiesTest, GetNativeTextureFormat_GetTextureFormatFromNative)
}
#endif

#if METAL_SUPPORTED
if (SkipFormatsMtl.find(Fmt) == SkipFormatsMtl.end())
{
Test(RENDER_DEVICE_TYPE_METAL);
}
#endif

#if WEBGPU_SUPPORTED
if (SkipFormatsWebGPU.find(Fmt) == SkipFormatsWebGPU.end())
{
Expand Down

0 comments on commit 5a34b0b

Please sign in to comment.