Skip to content

Commit

Permalink
GraphicsTools: added GetNativeTextureFormat and GetTextureFormatFromN…
Browse files Browse the repository at this point in the history
…ative functions
  • Loading branch information
TheMostDiligent committed Dec 8, 2024
1 parent d8451c5 commit c1509f6
Show file tree
Hide file tree
Showing 10 changed files with 352 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ class VkFormatToTexFormatMapper
m_VkFmtToTexFmtMap[VK_FORMAT_R64G64B64A64_SINT] = TEX_FORMAT_UNKNOWN;
m_VkFmtToTexFmtMap[VK_FORMAT_R64G64B64A64_SFLOAT] = TEX_FORMAT_UNKNOWN;

m_VkFmtToTexFmtMap[VK_FORMAT_B10G11R11_UFLOAT_PACK32] = TEX_FORMAT_UNKNOWN;
m_VkFmtToTexFmtMap[VK_FORMAT_B10G11R11_UFLOAT_PACK32] = TEX_FORMAT_R11G11B10_FLOAT;
m_VkFmtToTexFmtMap[VK_FORMAT_E5B9G9R9_UFLOAT_PACK32] = TEX_FORMAT_RGB9E5_SHAREDEXP;
m_VkFmtToTexFmtMap[VK_FORMAT_D16_UNORM] = TEX_FORMAT_D16_UNORM;
m_VkFmtToTexFmtMap[VK_FORMAT_X8_D24_UNORM_PACK32] = TEX_FORMAT_UNKNOWN;
Expand Down
20 changes: 8 additions & 12 deletions Graphics/GraphicsTools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ set(SOURCE
src/DynamicTextureArray.cpp
src/DynamicTextureAtlas.cpp
src/GraphicsUtilities.cpp
src/GraphicsUtilitiesD3D11.cpp
src/GraphicsUtilitiesD3D12.cpp
src/GraphicsUtilitiesGL.cpp
src/GraphicsUtilitiesVk.cpp
src/GraphicsUtilitiesWebGPU.cpp
src/OffScreenSwapChain.cpp
src/ScopedQueryHelper.cpp
src/ScreenCapture.cpp
Expand Down Expand Up @@ -83,7 +78,7 @@ endif()
set(DEPENDENCIES)

if(D3D11_SUPPORTED)
list(APPEND SOURCE src/TextureUploaderD3D11.cpp)
list(APPEND SOURCE src/TextureUploaderD3D11.cpp src/GraphicsUtilitiesD3D11.cpp)
list(APPEND INTERFACE interface/TextureUploaderD3D11.hpp)
list(APPEND DEPENDENCIES Diligent-GraphicsEngineD3D11Interface)
if(DILIGENT_USE_OPENXR)
Expand All @@ -99,30 +94,31 @@ if(D3D12_SUPPORTED)
endif()

if(VULKAN_SUPPORTED)
list(APPEND DEPENDENCIES Diligent-GraphicsEngineVkInterface Vulkan::Headers)
list(APPEND SOURCE src/GraphicsUtilitiesVk.cpp)
list(APPEND DEPENDENCIES Diligent-GraphicsEngineVk-static Vulkan::Headers)
if(DILIGENT_USE_OPENXR)
list(APPEND SOURCE src/OpenXRUtilitiesVk.cpp)
endif()
endif()

if(D3D12_SUPPORTED OR VULKAN_SUPPORTED)
list(APPEND SOURCE src/TextureUploaderD3D12_Vk.cpp)
list(APPEND SOURCE src/TextureUploaderD3D12_Vk.cpp src/GraphicsUtilitiesD3D12.cpp)
list(APPEND INTERFACE interface/TextureUploaderD3D12_Vk.hpp)
endif()

if(GL_SUPPORTED OR GLES_SUPPORTED)
list(APPEND SOURCE src/TextureUploaderGL.cpp)
list(APPEND SOURCE src/TextureUploaderGL.cpp src/GraphicsUtilitiesGL.cpp)
list(APPEND INTERFACE interface/TextureUploaderGL.hpp)
list(APPEND DEPENDENCIES Diligent-GraphicsEngineOpenGLInterface)
list(APPEND DEPENDENCIES Diligent-GraphicsEngineOpenGL-static)
if(DILIGENT_USE_OPENXR)
list(APPEND SOURCE src/OpenXRUtilitiesGL.cpp)
endif()
endif()

if(WEBGPU_SUPPORTED)
list(APPEND SOURCE src/TextureUploaderWebGPU.cpp)
list(APPEND SOURCE src/TextureUploaderWebGPU.cpp src/GraphicsUtilitiesWebGPU.cpp)
list(APPEND INTERFACE interface/TextureUploaderWebGPU.hpp)
list(APPEND DEPENDENCIES Diligent-GraphicsEngineWebGPUInterface)
list(APPEND DEPENDENCIES Diligent-GraphicsEngineWebGPU-static)
if (NOT PLATFORM_EMSCRIPTEN)
list(APPEND DEPENDENCIES dawn_proc)
endif()
Expand Down
5 changes: 5 additions & 0 deletions Graphics/GraphicsTools/interface/GraphicsUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ IBufferView* DILIGENT_GLOBAL_FUNCTION(GetBufferDefaultUAV)(IObject* pBuffer);
/// For other shader types, returns null.
const char* DILIGENT_GLOBAL_FUNCTION(GetWebGPUEmulatedArrayIndexSuffix)(IShader* pShader);

/// Returns the native texture format (e.g. DXGI_FORMAT, VkFormat) for the given texture format and device type.
int64_t DILIGENT_GLOBAL_FUNCTION(GetNativeTextureFormat)(TEXTURE_FORMAT TexFormat, enum RENDER_DEVICE_TYPE DeviceType);

/// Returns the texture format for the given native format (e.g. DXGI_FORMAT, VkFormat) and device type.
TEXTURE_FORMAT DILIGENT_GLOBAL_FUNCTION(GetTextureFormatFromNative)(int64_t NativeFormat, enum RENDER_DEVICE_TYPE DeviceType);

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

Expand Down
114 changes: 114 additions & 0 deletions Graphics/GraphicsTools/src/GraphicsUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@
namespace Diligent
{

#if D3D11_SUPPORTED
int64_t GetNativeTextureFormatD3D11(TEXTURE_FORMAT TexFormat);
TEXTURE_FORMAT GetTextureFormatFromNativeD3D11(int64_t NativeFormat);
#endif

#if D3D12_SUPPORTED
int64_t GetNativeTextureFormatD3D12(TEXTURE_FORMAT TexFormat);
TEXTURE_FORMAT GetTextureFormatFromNativeD3D12(int64_t NativeFormat);
#endif

#if GL_SUPPORTED || GLES_SUPPORTED
int64_t GetNativeTextureFormatGL(TEXTURE_FORMAT TexFormat);
TEXTURE_FORMAT GetTextureFormatFromNativeGL(int64_t NativeFormat);
#endif

#if VULKAN_SUPPORTED
int64_t GetNativeTextureFormatVk(TEXTURE_FORMAT TexFormat);
TEXTURE_FORMAT GetTextureFormatFromNativeVk(int64_t NativeFormat);
#endif

#if WEBGPU_SUPPORTED
int64_t GetNativeTextureFormatWebGPU(TEXTURE_FORMAT TexFormat);
TEXTURE_FORMAT GetTextureFormatFromNativeWebGPU(int64_t NativeFormat);
#endif

void CreateUniformBuffer(IRenderDevice* pDevice,
Uint64 Size,
const Char* Name,
Expand Down Expand Up @@ -540,6 +565,85 @@ IBufferView* GetBufferDefaultUAV(IObject* pBuffer)
return GetDefaultUAV(static_cast<IBuffer*>(pBuffer));
}

#if !WEBGPU_SUPPORTED
const char* GetWebGPUEmulatedArrayIndexSuffix(IShader* pShader)
{
return nullptr;
}
#endif

int64_t GetNativeTextureFormat(TEXTURE_FORMAT TexFormat, RENDER_DEVICE_TYPE DeviceType)
{
switch (DeviceType)
{
#if D3D11_SUPPORTED
case RENDER_DEVICE_TYPE_D3D11:
return GetNativeTextureFormatD3D11(TexFormat);
#endif

#if D3D12_SUPPORTED
case RENDER_DEVICE_TYPE_D3D12:
return GetNativeTextureFormatD3D12(TexFormat);
#endif

#if GL_SUPPORTED || GLES_SUPPORTED
case RENDER_DEVICE_TYPE_GL:
case RENDER_DEVICE_TYPE_GLES:
return GetNativeTextureFormatGL(TexFormat);
#endif

#if VULKAN_SUPPORTED
case RENDER_DEVICE_TYPE_VULKAN:
return GetNativeTextureFormatVk(TexFormat);
#endif

#if WEBGPU_SUPPORTED
case RENDER_DEVICE_TYPE_WEBGPU:
return GetNativeTextureFormatWebGPU(TexFormat);
#endif

default:
UNSUPPORTED("Unsupported device type");
return 0;
}
}

TEXTURE_FORMAT GetTextureFormatFromNative(int64_t NativeFormat, RENDER_DEVICE_TYPE DeviceType)
{
switch (DeviceType)
{
#if D3D11_SUPPORTED
case RENDER_DEVICE_TYPE_D3D11:
return GetTextureFormatFromNativeD3D11(NativeFormat);
#endif

#if D3D12_SUPPORTED
case RENDER_DEVICE_TYPE_D3D12:
return GetTextureFormatFromNativeD3D12(NativeFormat);
#endif

#if GL_SUPPORTED || GLES_SUPPORTED
case RENDER_DEVICE_TYPE_GL:
case RENDER_DEVICE_TYPE_GLES:
return GetTextureFormatFromNativeGL(NativeFormat);
#endif

#if VULKAN_SUPPORTED
case RENDER_DEVICE_TYPE_VULKAN:
return GetTextureFormatFromNativeVk(NativeFormat);
#endif

#if WEBGPU_SUPPORTED
case RENDER_DEVICE_TYPE_WEBGPU:
return GetTextureFormatFromNativeWebGPU(NativeFormat);
#endif

default:
UNSUPPORTED("Unsupported device type");
return TEX_FORMAT_UNKNOWN;
}
}

} // namespace Diligent


Expand Down Expand Up @@ -616,4 +720,14 @@ extern "C"
{
return Diligent::GetWebGPUEmulatedArrayIndexSuffix(pShader);
}

int64_t Diligent_GetNativeTextureFormat(Diligent::TEXTURE_FORMAT TexFormat, Diligent::RENDER_DEVICE_TYPE DeviceType)
{
return Diligent::GetNativeTextureFormat(TexFormat, DeviceType);
}

Diligent::TEXTURE_FORMAT Diligent_GetTextureFormatFromNative(int64_t NativeFormat, Diligent::RENDER_DEVICE_TYPE DeviceType)
{
return Diligent::GetTextureFormatFromNative(NativeFormat, DeviceType);
}
}
14 changes: 13 additions & 1 deletion Graphics/GraphicsTools/src/GraphicsUtilitiesD3D11.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 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 All @@ -26,7 +26,19 @@

#include "GraphicsUtilities.h"

#include "../../GraphicsEngineD3DBase/include/DXGITypeConversions.hpp"

namespace Diligent
{

int64_t GetNativeTextureFormatD3D11(TEXTURE_FORMAT TexFormat)
{
return static_cast<int64_t>(TexFormatToDXGI_Format(TexFormat));
}

TEXTURE_FORMAT GetTextureFormatFromNativeD3D11(int64_t NativeFormat)
{
return DXGI_FormatToTexFormat(static_cast<DXGI_FORMAT>(NativeFormat));
}

} // namespace Diligent
14 changes: 13 additions & 1 deletion Graphics/GraphicsTools/src/GraphicsUtilitiesD3D12.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 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 All @@ -26,7 +26,19 @@

#include "GraphicsUtilities.h"

#include "../../GraphicsEngineD3DBase/include/DXGITypeConversions.hpp"

namespace Diligent
{

int64_t GetNativeTextureFormatD3D12(TEXTURE_FORMAT TexFormat)
{
return static_cast<int64_t>(TexFormatToDXGI_Format(TexFormat));
}

TEXTURE_FORMAT GetTextureFormatFromNativeD3D12(int64_t NativeFormat)
{
return DXGI_FormatToTexFormat(static_cast<DXGI_FORMAT>(NativeFormat));
}

} // namespace Diligent
16 changes: 15 additions & 1 deletion Graphics/GraphicsTools/src/GraphicsUtilitiesGL.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 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 All @@ -26,7 +26,21 @@

#include "GraphicsUtilities.h"

typedef unsigned int GLenum;

namespace Diligent
{
GLenum TexFormatToGLInternalTexFormat(TEXTURE_FORMAT TexFormat, Uint32 BindFlags = 0);
TEXTURE_FORMAT GLInternalTexFormatToTexFormat(GLenum GlFormat);

int64_t GetNativeTextureFormatGL(TEXTURE_FORMAT TexFormat)
{
return static_cast<int64_t>(TexFormatToGLInternalTexFormat(TexFormat));
}

TEXTURE_FORMAT GetTextureFormatFromNativeGL(int64_t NativeFormat)
{
return GLInternalTexFormatToTexFormat(static_cast<GLenum>(NativeFormat));
}

} // namespace Diligent
15 changes: 14 additions & 1 deletion Graphics/GraphicsTools/src/GraphicsUtilitiesVk.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 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 All @@ -25,12 +25,25 @@
*/

#include "GraphicsUtilities.h"
#include "DeviceContext.h"

#if VULKAN_SUPPORTED
# include "../../GraphicsEngineVulkan/include/VulkanUtilities/VulkanHeaders.h"
#endif

#include "../../GraphicsEngineVulkan/include/VulkanTypeConversions.hpp"

namespace Diligent
{

int64_t GetNativeTextureFormatVk(TEXTURE_FORMAT TexFormat)
{
return static_cast<int64_t>(TexFormatToVkFormat(TexFormat));
}

TEXTURE_FORMAT GetTextureFormatFromNativeVk(int64_t NativeFormat)
{
return VkFormatToTexFormat(static_cast<VkFormat>(NativeFormat));
}

} // namespace Diligent
17 changes: 15 additions & 2 deletions Graphics/GraphicsTools/src/GraphicsUtilitiesWebGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,31 @@

#include "RefCntAutoPtr.hpp"

enum WGPUTextureFormat : int;

namespace Diligent
{

WGPUTextureFormat TextureFormatToWGPUFormat(TEXTURE_FORMAT TexFmt);
TEXTURE_FORMAT WGPUFormatToTextureFormat(WGPUTextureFormat TexFmt);

const char* GetWebGPUEmulatedArrayIndexSuffix(IShader* pShader)
{
#if WEBGPU_SUPPORTED
if (RefCntAutoPtr<IShaderWebGPU> pShaderWGPU{pShader, IID_ShaderWebGPU})
{
return pShaderWGPU->GetEmulatedArrayIndexSuffix();
}
#endif
return nullptr;
}

int64_t GetNativeTextureFormatWebGPU(TEXTURE_FORMAT TexFormat)
{
return static_cast<int64_t>(TextureFormatToWGPUFormat(TexFormat));
}

TEXTURE_FORMAT GetTextureFormatFromNativeWebGPU(int64_t NativeFormat)
{
return WGPUFormatToTextureFormat(static_cast<WGPUTextureFormat>(NativeFormat));
}

} // namespace Diligent
Loading

0 comments on commit c1509f6

Please sign in to comment.