Skip to content

Commit 614188a

Browse files
GraphicsTools: added GetNativeTextureFormat and GetTextureFormatFromNative functions
1 parent d8451c5 commit 614188a

File tree

10 files changed

+352
-18
lines changed

10 files changed

+352
-18
lines changed

Graphics/GraphicsEngineVulkan/src/VulkanTypeConversions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class VkFormatToTexFormatMapper
340340
m_VkFmtToTexFmtMap[VK_FORMAT_R64G64B64A64_SINT] = TEX_FORMAT_UNKNOWN;
341341
m_VkFmtToTexFmtMap[VK_FORMAT_R64G64B64A64_SFLOAT] = TEX_FORMAT_UNKNOWN;
342342

343-
m_VkFmtToTexFmtMap[VK_FORMAT_B10G11R11_UFLOAT_PACK32] = TEX_FORMAT_UNKNOWN;
343+
m_VkFmtToTexFmtMap[VK_FORMAT_B10G11R11_UFLOAT_PACK32] = TEX_FORMAT_R11G11B10_FLOAT;
344344
m_VkFmtToTexFmtMap[VK_FORMAT_E5B9G9R9_UFLOAT_PACK32] = TEX_FORMAT_RGB9E5_SHAREDEXP;
345345
m_VkFmtToTexFmtMap[VK_FORMAT_D16_UNORM] = TEX_FORMAT_D16_UNORM;
346346
m_VkFmtToTexFmtMap[VK_FORMAT_X8_D24_UNORM_PACK32] = TEX_FORMAT_UNKNOWN;

Graphics/GraphicsTools/CMakeLists.txt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ set(SOURCE
3737
src/DynamicTextureArray.cpp
3838
src/DynamicTextureAtlas.cpp
3939
src/GraphicsUtilities.cpp
40-
src/GraphicsUtilitiesD3D11.cpp
41-
src/GraphicsUtilitiesD3D12.cpp
42-
src/GraphicsUtilitiesGL.cpp
43-
src/GraphicsUtilitiesVk.cpp
44-
src/GraphicsUtilitiesWebGPU.cpp
4540
src/OffScreenSwapChain.cpp
4641
src/ScopedQueryHelper.cpp
4742
src/ScreenCapture.cpp
@@ -83,7 +78,7 @@ endif()
8378
set(DEPENDENCIES)
8479

8580
if(D3D11_SUPPORTED)
86-
list(APPEND SOURCE src/TextureUploaderD3D11.cpp)
81+
list(APPEND SOURCE src/TextureUploaderD3D11.cpp src/GraphicsUtilitiesD3D11.cpp)
8782
list(APPEND INTERFACE interface/TextureUploaderD3D11.hpp)
8883
list(APPEND DEPENDENCIES Diligent-GraphicsEngineD3D11Interface)
8984
if(DILIGENT_USE_OPENXR)
@@ -93,13 +88,15 @@ endif()
9388

9489
if(D3D12_SUPPORTED)
9590
list(APPEND DEPENDENCIES Diligent-GraphicsEngineD3D12Interface)
91+
list(APPEND SOURCE src/GraphicsUtilitiesD3D12.cpp)
9692
if(DILIGENT_USE_OPENXR)
9793
list(APPEND SOURCE src/OpenXRUtilitiesD3D12.cpp)
9894
endif()
9995
endif()
10096

10197
if(VULKAN_SUPPORTED)
102-
list(APPEND DEPENDENCIES Diligent-GraphicsEngineVkInterface Vulkan::Headers)
98+
list(APPEND SOURCE src/GraphicsUtilitiesVk.cpp)
99+
list(APPEND DEPENDENCIES Diligent-GraphicsEngineVk-static Vulkan::Headers)
103100
if(DILIGENT_USE_OPENXR)
104101
list(APPEND SOURCE src/OpenXRUtilitiesVk.cpp)
105102
endif()
@@ -111,18 +108,18 @@ if(D3D12_SUPPORTED OR VULKAN_SUPPORTED)
111108
endif()
112109

113110
if(GL_SUPPORTED OR GLES_SUPPORTED)
114-
list(APPEND SOURCE src/TextureUploaderGL.cpp)
111+
list(APPEND SOURCE src/TextureUploaderGL.cpp src/GraphicsUtilitiesGL.cpp)
115112
list(APPEND INTERFACE interface/TextureUploaderGL.hpp)
116-
list(APPEND DEPENDENCIES Diligent-GraphicsEngineOpenGLInterface)
113+
list(APPEND DEPENDENCIES Diligent-GraphicsEngineOpenGL-static)
117114
if(DILIGENT_USE_OPENXR)
118115
list(APPEND SOURCE src/OpenXRUtilitiesGL.cpp)
119116
endif()
120117
endif()
121118

122119
if(WEBGPU_SUPPORTED)
123-
list(APPEND SOURCE src/TextureUploaderWebGPU.cpp)
120+
list(APPEND SOURCE src/TextureUploaderWebGPU.cpp src/GraphicsUtilitiesWebGPU.cpp)
124121
list(APPEND INTERFACE interface/TextureUploaderWebGPU.hpp)
125-
list(APPEND DEPENDENCIES Diligent-GraphicsEngineWebGPUInterface)
122+
list(APPEND DEPENDENCIES Diligent-GraphicsEngineWebGPU-static)
126123
if (NOT PLATFORM_EMSCRIPTEN)
127124
list(APPEND DEPENDENCIES dawn_proc)
128125
endif()

Graphics/GraphicsTools/interface/GraphicsUtilities.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ IBufferView* DILIGENT_GLOBAL_FUNCTION(GetBufferDefaultUAV)(IObject* pBuffer);
212212
/// For other shader types, returns null.
213213
const char* DILIGENT_GLOBAL_FUNCTION(GetWebGPUEmulatedArrayIndexSuffix)(IShader* pShader);
214214

215+
/// Returns the native texture format (e.g. DXGI_FORMAT, VkFormat) for the given texture format and device type.
216+
int64_t DILIGENT_GLOBAL_FUNCTION(GetNativeTextureFormat)(TEXTURE_FORMAT TexFormat, enum RENDER_DEVICE_TYPE DeviceType);
217+
218+
/// Returns the texture format for the given native format (e.g. DXGI_FORMAT, VkFormat) and device type.
219+
TEXTURE_FORMAT DILIGENT_GLOBAL_FUNCTION(GetTextureFormatFromNative)(int64_t NativeFormat, enum RENDER_DEVICE_TYPE DeviceType);
215220

216221
#include "../../../Primitives/interface/UndefRefMacro.h"
217222

Graphics/GraphicsTools/src/GraphicsUtilities.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,31 @@
4040
namespace Diligent
4141
{
4242

43+
#if D3D11_SUPPORTED
44+
int64_t GetNativeTextureFormatD3D11(TEXTURE_FORMAT TexFormat);
45+
TEXTURE_FORMAT GetTextureFormatFromNativeD3D11(int64_t NativeFormat);
46+
#endif
47+
48+
#if D3D12_SUPPORTED
49+
int64_t GetNativeTextureFormatD3D12(TEXTURE_FORMAT TexFormat);
50+
TEXTURE_FORMAT GetTextureFormatFromNativeD3D12(int64_t NativeFormat);
51+
#endif
52+
53+
#if GL_SUPPORTED || GLES_SUPPORTED
54+
int64_t GetNativeTextureFormatGL(TEXTURE_FORMAT TexFormat);
55+
TEXTURE_FORMAT GetTextureFormatFromNativeGL(int64_t NativeFormat);
56+
#endif
57+
58+
#if VULKAN_SUPPORTED
59+
int64_t GetNativeTextureFormatVk(TEXTURE_FORMAT TexFormat);
60+
TEXTURE_FORMAT GetTextureFormatFromNativeVk(int64_t NativeFormat);
61+
#endif
62+
63+
#if WEBGPU_SUPPORTED
64+
int64_t GetNativeTextureFormatWebGPU(TEXTURE_FORMAT TexFormat);
65+
TEXTURE_FORMAT GetTextureFormatFromNativeWebGPU(int64_t NativeFormat);
66+
#endif
67+
4368
void CreateUniformBuffer(IRenderDevice* pDevice,
4469
Uint64 Size,
4570
const Char* Name,
@@ -540,6 +565,85 @@ IBufferView* GetBufferDefaultUAV(IObject* pBuffer)
540565
return GetDefaultUAV(static_cast<IBuffer*>(pBuffer));
541566
}
542567

568+
#if !WEBGPU_SUPPORTED
569+
const char* GetWebGPUEmulatedArrayIndexSuffix(IShader* pShader)
570+
{
571+
return nullptr;
572+
}
573+
#endif
574+
575+
int64_t GetNativeTextureFormat(TEXTURE_FORMAT TexFormat, RENDER_DEVICE_TYPE DeviceType)
576+
{
577+
switch (DeviceType)
578+
{
579+
#if D3D11_SUPPORTED
580+
case RENDER_DEVICE_TYPE_D3D11:
581+
return GetNativeTextureFormatD3D11(TexFormat);
582+
#endif
583+
584+
#if D3D12_SUPPORTED
585+
case RENDER_DEVICE_TYPE_D3D12:
586+
return GetNativeTextureFormatD3D12(TexFormat);
587+
#endif
588+
589+
#if GL_SUPPORTED || GLES_SUPPORTED
590+
case RENDER_DEVICE_TYPE_GL:
591+
case RENDER_DEVICE_TYPE_GLES:
592+
return GetNativeTextureFormatGL(TexFormat);
593+
#endif
594+
595+
#if VULKAN_SUPPORTED
596+
case RENDER_DEVICE_TYPE_VULKAN:
597+
return GetNativeTextureFormatVk(TexFormat);
598+
#endif
599+
600+
#if WEBGPU_SUPPORTED
601+
case RENDER_DEVICE_TYPE_WEBGPU:
602+
return GetNativeTextureFormatWebGPU(TexFormat);
603+
#endif
604+
605+
default:
606+
UNSUPPORTED("Unsupported device type");
607+
return 0;
608+
}
609+
}
610+
611+
TEXTURE_FORMAT GetTextureFormatFromNative(int64_t NativeFormat, RENDER_DEVICE_TYPE DeviceType)
612+
{
613+
switch (DeviceType)
614+
{
615+
#if D3D11_SUPPORTED
616+
case RENDER_DEVICE_TYPE_D3D11:
617+
return GetTextureFormatFromNativeD3D11(NativeFormat);
618+
#endif
619+
620+
#if D3D12_SUPPORTED
621+
case RENDER_DEVICE_TYPE_D3D12:
622+
return GetTextureFormatFromNativeD3D12(NativeFormat);
623+
#endif
624+
625+
#if GL_SUPPORTED || GLES_SUPPORTED
626+
case RENDER_DEVICE_TYPE_GL:
627+
case RENDER_DEVICE_TYPE_GLES:
628+
return GetTextureFormatFromNativeGL(NativeFormat);
629+
#endif
630+
631+
#if VULKAN_SUPPORTED
632+
case RENDER_DEVICE_TYPE_VULKAN:
633+
return GetTextureFormatFromNativeVk(NativeFormat);
634+
#endif
635+
636+
#if WEBGPU_SUPPORTED
637+
case RENDER_DEVICE_TYPE_WEBGPU:
638+
return GetTextureFormatFromNativeWebGPU(NativeFormat);
639+
#endif
640+
641+
default:
642+
UNSUPPORTED("Unsupported device type");
643+
return TEX_FORMAT_UNKNOWN;
644+
}
645+
}
646+
543647
} // namespace Diligent
544648

545649

@@ -616,4 +720,14 @@ extern "C"
616720
{
617721
return Diligent::GetWebGPUEmulatedArrayIndexSuffix(pShader);
618722
}
723+
724+
int64_t Diligent_GetNativeTextureFormat(Diligent::TEXTURE_FORMAT TexFormat, Diligent::RENDER_DEVICE_TYPE DeviceType)
725+
{
726+
return Diligent::GetNativeTextureFormat(TexFormat, DeviceType);
727+
}
728+
729+
Diligent::TEXTURE_FORMAT Diligent_GetTextureFormatFromNative(int64_t NativeFormat, Diligent::RENDER_DEVICE_TYPE DeviceType)
730+
{
731+
return Diligent::GetTextureFormatFromNative(NativeFormat, DeviceType);
732+
}
619733
}

Graphics/GraphicsTools/src/GraphicsUtilitiesD3D11.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,19 @@
2626

2727
#include "GraphicsUtilities.h"
2828

29+
#include "../../GraphicsEngineD3DBase/include/DXGITypeConversions.hpp"
30+
2931
namespace Diligent
3032
{
3133

34+
int64_t GetNativeTextureFormatD3D11(TEXTURE_FORMAT TexFormat)
35+
{
36+
return static_cast<int64_t>(TexFormatToDXGI_Format(TexFormat));
37+
}
38+
39+
TEXTURE_FORMAT GetTextureFormatFromNativeD3D11(int64_t NativeFormat)
40+
{
41+
return DXGI_FormatToTexFormat(static_cast<DXGI_FORMAT>(NativeFormat));
3242
}
43+
44+
} // namespace Diligent

Graphics/GraphicsTools/src/GraphicsUtilitiesD3D12.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,19 @@
2626

2727
#include "GraphicsUtilities.h"
2828

29+
#include "../../GraphicsEngineD3DBase/include/DXGITypeConversions.hpp"
30+
2931
namespace Diligent
3032
{
3133

34+
int64_t GetNativeTextureFormatD3D12(TEXTURE_FORMAT TexFormat)
35+
{
36+
return static_cast<int64_t>(TexFormatToDXGI_Format(TexFormat));
37+
}
38+
39+
TEXTURE_FORMAT GetTextureFormatFromNativeD3D12(int64_t NativeFormat)
40+
{
41+
return DXGI_FormatToTexFormat(static_cast<DXGI_FORMAT>(NativeFormat));
42+
}
43+
3244
} // namespace Diligent

Graphics/GraphicsTools/src/GraphicsUtilitiesGL.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,21 @@
2626

2727
#include "GraphicsUtilities.h"
2828

29+
typedef unsigned int GLenum;
30+
2931
namespace Diligent
3032
{
33+
GLenum TexFormatToGLInternalTexFormat(TEXTURE_FORMAT TexFormat, Uint32 BindFlags = 0);
34+
TEXTURE_FORMAT GLInternalTexFormatToTexFormat(GLenum GlFormat);
35+
36+
int64_t GetNativeTextureFormatGL(TEXTURE_FORMAT TexFormat)
37+
{
38+
return static_cast<int64_t>(TexFormatToGLInternalTexFormat(TexFormat));
39+
}
40+
41+
TEXTURE_FORMAT GetTextureFormatFromNativeGL(int64_t NativeFormat)
42+
{
43+
return GLInternalTexFormatToTexFormat(static_cast<GLenum>(NativeFormat));
44+
}
3145

3246
} // namespace Diligent

Graphics/GraphicsTools/src/GraphicsUtilitiesVk.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,12 +25,25 @@
2525
*/
2626

2727
#include "GraphicsUtilities.h"
28+
#include "DeviceContext.h"
2829

2930
#if VULKAN_SUPPORTED
3031
# include "../../GraphicsEngineVulkan/include/VulkanUtilities/VulkanHeaders.h"
3132
#endif
3233

34+
#include "../../GraphicsEngineVulkan/include/VulkanTypeConversions.hpp"
35+
3336
namespace Diligent
3437
{
3538

39+
int64_t GetNativeTextureFormatVk(TEXTURE_FORMAT TexFormat)
40+
{
41+
return static_cast<int64_t>(TexFormatToVkFormat(TexFormat));
42+
}
43+
44+
TEXTURE_FORMAT GetTextureFormatFromNativeVk(int64_t NativeFormat)
45+
{
46+
return VkFormatToTexFormat(static_cast<VkFormat>(NativeFormat));
47+
}
48+
3649
} // namespace Diligent

Graphics/GraphicsTools/src/GraphicsUtilitiesWebGPU.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,31 @@
3232

3333
#include "RefCntAutoPtr.hpp"
3434

35+
enum WGPUTextureFormat : int;
36+
3537
namespace Diligent
3638
{
3739

40+
WGPUTextureFormat TextureFormatToWGPUFormat(TEXTURE_FORMAT TexFmt);
41+
TEXTURE_FORMAT WGPUFormatToTextureFormat(WGPUTextureFormat TexFmt);
42+
3843
const char* GetWebGPUEmulatedArrayIndexSuffix(IShader* pShader)
3944
{
40-
#if WEBGPU_SUPPORTED
4145
if (RefCntAutoPtr<IShaderWebGPU> pShaderWGPU{pShader, IID_ShaderWebGPU})
4246
{
4347
return pShaderWGPU->GetEmulatedArrayIndexSuffix();
4448
}
45-
#endif
4649
return nullptr;
4750
}
4851

52+
int64_t GetNativeTextureFormatWebGPU(TEXTURE_FORMAT TexFormat)
53+
{
54+
return static_cast<int64_t>(TextureFormatToWGPUFormat(TexFormat));
55+
}
56+
57+
TEXTURE_FORMAT GetTextureFormatFromNativeWebGPU(int64_t NativeFormat)
58+
{
59+
return WGPUFormatToTextureFormat(static_cast<WGPUTextureFormat>(NativeFormat));
60+
}
61+
4962
} // namespace Diligent

0 commit comments

Comments
 (0)