Skip to content

Commit

Permalink
GraphicsTools: added OpenXR utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Dec 8, 2024
1 parent 59a0d8d commit 0d022f5
Show file tree
Hide file tree
Showing 7 changed files with 450 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Graphics/GraphicsTools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(INTERFACE
interface/GraphicsUtilities.h
interface/MapHelper.hpp
interface/OffScreenSwapChain.hpp
interface/OpenXRUtilities.h
interface/ResourceRegistry.hpp
interface/ScopedDebugGroup.hpp
interface/GPUCompletionAwaitQueue.hpp
Expand Down Expand Up @@ -43,6 +44,7 @@ set(SOURCE
src/GraphicsUtilitiesVk.cpp
src/GraphicsUtilitiesWebGPU.cpp
src/OffScreenSwapChain.cpp
src/OpenXRUtilities.cpp
src/ScopedQueryHelper.cpp
src/ScreenCapture.cpp
src/ShaderSourceFactoryUtils.cpp
Expand Down Expand Up @@ -81,14 +83,23 @@ if(D3D11_SUPPORTED)
list(APPEND SOURCE src/TextureUploaderD3D11.cpp)
list(APPEND INTERFACE interface/TextureUploaderD3D11.hpp)
list(APPEND DEPENDENCIES Diligent-GraphicsEngineD3D11Interface)
if(DILIGENT_USE_OPENXR)
list(APPEND SOURCE src/OpenXRUtilitiesD3D11.cpp)
endif()
endif()

if(D3D12_SUPPORTED)
list(APPEND DEPENDENCIES Diligent-GraphicsEngineD3D12Interface)
if(DILIGENT_USE_OPENXR)
list(APPEND SOURCE src/OpenXRUtilitiesD3D12.cpp)
endif()
endif()

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

if(D3D12_SUPPORTED OR VULKAN_SUPPORTED)
Expand All @@ -100,6 +111,9 @@ if(GL_SUPPORTED OR GLES_SUPPORTED)
list(APPEND SOURCE src/TextureUploaderGL.cpp)
list(APPEND INTERFACE interface/TextureUploaderGL.hpp)
list(APPEND DEPENDENCIES Diligent-GraphicsEngineOpenGLInterface)
if(DILIGENT_USE_OPENXR)
list(APPEND SOURCE src/OpenXRUtilitiesGL.cpp)
endif()
endif()

if(WEBGPU_SUPPORTED)
Expand Down Expand Up @@ -151,6 +165,11 @@ if(RENDER_STATE_CACHE_SUPPORTED)
target_compile_definitions(Diligent-GraphicsTools PUBLIC DILIGENT_RENDER_STATE_CACHE_SUPPORTED=1)
endif()

if(DILIGENT_USE_OPENXR)
target_link_libraries(Diligent-GraphicsTools PRIVATE OpenXR::headers)
target_compile_definitions(Diligent-GraphicsTools PUBLIC DILIGENT_USE_OPENXR=1)
endif()

if(D3D11_SUPPORTED OR D3D12_SUPPORTED)
target_link_libraries(Diligent-GraphicsTools
PRIVATE
Expand Down
65 changes: 65 additions & 0 deletions Graphics/GraphicsTools/interface/OpenXRUtilities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/

#pragma once

/// \file
/// OpenXR utilities

#include "../../GraphicsEngine/interface/RenderDevice.h"
#include "../../GraphicsEngine/interface/DeviceContext.h"
#include "../../Primitives/interface/DataBlob.h"

DILIGENT_BEGIN_NAMESPACE(Diligent)

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

/// Prepares OpenXR graphics binding for the specified device and context.
///
/// \param [in] pDevice - Pointer to the render device.
/// \param [in] pContext - Pointer to the device context.
/// \param [out] ppGraphicsBinding - Address of the memory location where the pointer to the data blob
/// containing the graphics binding will be stored.
///
/// \remarks The function returns the data blob that contains the OpenXR graphics binding structure
/// (XrGraphicsBindingVulkanKHR, XrGraphicsBindingD3D11KHR, etc.).
/// The data blob should be used to create the OpenXR session, for example:
///
/// RefCntAutoPtr<IDataBlob> pGraphicsBinding;
/// GetOpenXRGraphicsBinding(m_pDevice, m_pImmediateContext, &pGraphicsBinding);
///
/// XrSessionCreateInfo sessionCI{XR_TYPE_SESSION_CREATE_INFO};
/// sessionCI.next = pGraphicsBinding->GetConstDataPtr();
/// sessionCI.systemId = m_SystemId;
/// xrCreateSession(m_xrInstance, &sessionCI, &m_xrSession);
///
void DILIGENT_GLOBAL_FUNCTION(GetOpenXRGraphicsBinding)(IRenderDevice* pDevice,
IDeviceContext* pContext,
IDataBlob** ppGraphicsBinding);

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

DILIGENT_END_NAMESPACE // namespace Diligent
131 changes: 131 additions & 0 deletions Graphics/GraphicsTools/src/OpenXRUtilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/

#include "OpenXRUtilities.h"

#include "DebugUtilities.hpp"

namespace Diligent
{

#if DILIGENT_USE_OPENXR

# if D3D11_SUPPORTED
void GetOpenXRGraphicsBindingD3D11(IRenderDevice* pDevice,
IDeviceContext* pContext,
IDataBlob** ppGraphicsBinding);
# endif

# if D3D12_SUPPORTED
void GetOpenXRGraphicsBindingD3D12(IRenderDevice* pDevice,
IDeviceContext* pContext,
IDataBlob** ppGraphicsBinding);
# endif

# if GL_SUPPORTED || GLES_SUPPORTED
void GetOpenXRGraphicsBindingGL(IRenderDevice* pDevice,
IDeviceContext* pContext,
IDataBlob** ppGraphicsBinding);
# endif

# if VULKAN_SUPPORTED
void GetOpenXRGraphicsBindingVk(IRenderDevice* pDevice,
IDeviceContext* pContext,
IDataBlob** ppGraphicsBinding);
# endif

#endif

void GetOpenXRGraphicsBinding(IRenderDevice* pDevice,
IDeviceContext* pContext,
IDataBlob** ppGraphicsBinding)
{
#if DILIGENT_USE_OPENXR
if (pDevice == nullptr)
{
UNEXPECTED("pDevice must not be null");
return;
}

if (pContext == nullptr)
{
UNEXPECTED("pContext must not be null");
return;
}

if (ppGraphicsBinding == nullptr)
{
UNEXPECTED("ppGraphicsBinding must not be null");
return;
}

RENDER_DEVICE_TYPE DevType = pDevice->GetDeviceInfo().Type;
switch (DevType)
{
# if D3D11_SUPPORTED
case RENDER_DEVICE_TYPE_D3D11:
GetOpenXRGraphicsBindingD3D11(pDevice, pContext, ppGraphicsBinding);
break;
# endif

# if D3D12_SUPPORTED
case RENDER_DEVICE_TYPE_D3D12:
GetOpenXRGraphicsBindingD3D12(pDevice, pContext, ppGraphicsBinding);
break;
# endif

# if GL_SUPPORTED || GLES_SUPPORTED
case RENDER_DEVICE_TYPE_GL:
case RENDER_DEVICE_TYPE_GLES:
GetOpenXRGraphicsBindingGL(pDevice, pContext, ppGraphicsBinding);
break;
# endif

# if VULKAN_SUPPORTED
case RENDER_DEVICE_TYPE_VULKAN:
GetOpenXRGraphicsBindingVk(pDevice, pContext, ppGraphicsBinding);
break;
# endif

default:
UNSUPPORTED("Unsupported device type");
}
#else
UNSUPPORTED("OpenXR is not supported");
#endif
}

} // namespace Diligent

extern "C"
{
void Diligent_GetOpenXRGraphicsBinding(Diligent::IRenderDevice* pDevice,
Diligent::IDeviceContext* pContext,
Diligent::IDataBlob** ppGraphicsBinding)
{
Diligent::GetOpenXRGraphicsBinding(pDevice, pContext, ppGraphicsBinding);
}
}
62 changes: 62 additions & 0 deletions Graphics/GraphicsTools/src/OpenXRUtilitiesD3D11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/

#include "OpenXRUtilities.h"

#include "DebugUtilities.hpp"
#include "DataBlobImpl.hpp"

#include "WinHPreface.h"
#include <d3d11.h>
#include <atlbase.h>
#include "WinHPostface.h"

#include "RenderDeviceD3D11.h"

#define XR_USE_GRAPHICS_API_D3D11
#include <openxr/openxr_platform.h>

namespace Diligent
{

void GetOpenXRGraphicsBindingD3D11(IRenderDevice* pDevice,
IDeviceContext* pContext,
IDataBlob** ppGraphicsBinding)
{
RefCntAutoPtr<DataBlobImpl> pDataBlob{DataBlobImpl::Create(sizeof(XrGraphicsBindingD3D11KHR))};

RefCntAutoPtr<IRenderDeviceD3D11> pDeviceD3D11{pDevice, IID_RenderDeviceD3D11};
VERIFY_EXPR(pDeviceD3D11 != nullptr);

XrGraphicsBindingD3D11KHR& Binding = *reinterpret_cast<XrGraphicsBindingD3D11KHR*>(pDataBlob->GetDataPtr());
Binding.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR;
Binding.next = nullptr;
Binding.device = pDeviceD3D11->GetD3D11Device();

*ppGraphicsBinding = pDataBlob.Detach();
}

} // namespace Diligent
67 changes: 67 additions & 0 deletions Graphics/GraphicsTools/src/OpenXRUtilitiesD3D12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/

#include "OpenXRUtilities.h"

#include "DebugUtilities.hpp"
#include "DataBlobImpl.hpp"

#include "WinHPreface.h"
#include <d3d12.h>
#include <atlbase.h>
#include "WinHPostface.h"

#include "RenderDeviceD3D12.h"
#include "CommandQueueD3D12.h"


#define XR_USE_GRAPHICS_API_D3D12
#include <openxr/openxr_platform.h>

namespace Diligent
{

void GetOpenXRGraphicsBindingD3D12(IRenderDevice* pDevice,
IDeviceContext* pContext,
IDataBlob** ppGraphicsBinding)
{
RefCntAutoPtr<DataBlobImpl> pDataBlob{DataBlobImpl::Create(sizeof(XrGraphicsBindingD3D12KHR))};

RefCntAutoPtr<IRenderDeviceD3D12> pDeviceD3D12{pDevice, IID_RenderDeviceD3D12};
VERIFY_EXPR(pDeviceD3D12 != nullptr);
RefCntAutoPtr<ICommandQueueD3D12> pQueueD3D12{pContext->LockCommandQueue(), IID_CommandQueueD3D12};
VERIFY_EXPR(pQueueD3D12 != nullptr);

XrGraphicsBindingD3D12KHR& Binding = *reinterpret_cast<XrGraphicsBindingD3D12KHR*>(pDataBlob->GetDataPtr());
Binding.type = XR_TYPE_GRAPHICS_BINDING_D3D12_KHR;
Binding.next = nullptr;
Binding.device = pDeviceD3D12->GetD3D12Device();
Binding.queue = pQueueD3D12->GetD3D12CommandQueue();

*ppGraphicsBinding = pDataBlob.Detach();
}

} // namespace Diligent
Loading

0 comments on commit 0d022f5

Please sign in to comment.