1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOpenXRManagerD3DGraphics.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkOpenXRManagerD3DGraphics.h"
#include "vtkObjectFactory.h"
#include "vtkOpenXRManager.h"
#include "vtkWin32OpenGLDXRenderWindow.h"
#define XR_USE_GRAPHICS_API_D3D11
#include "d3d11.h" // Required headers for the XrGraphicsRequirementsD3D11KHR struct
#include <vtkOpenXRPlatform.h>
VTK_ABI_NAMESPACE_BEGIN
class vtkOpenXRManagerD3DGraphics::PIMPL
{
public:
// D3D swapchains
std::vector<SwapchainImagesD3D> ColorSwapchains;
std::vector<SwapchainImagesD3D> DepthSwapchains;
};
vtkStandardNewMacro(vtkOpenXRManagerD3DGraphics);
//------------------------------------------------------------------------------
vtkOpenXRManagerD3DGraphics::vtkOpenXRManagerD3DGraphics()
{
this->Private = new PIMPL;
}
//------------------------------------------------------------------------------
vtkOpenXRManagerD3DGraphics::~vtkOpenXRManagerD3DGraphics()
{
delete this->Private;
}
//------------------------------------------------------------------------------
void vtkOpenXRManagerD3DGraphics::SetNumberOfSwapchains(uint32_t viewCount)
{
this->Private->ColorSwapchains.resize(viewCount);
this->Private->DepthSwapchains.resize(viewCount);
}
//------------------------------------------------------------------------------
void vtkOpenXRManagerD3DGraphics::GetColorSwapchainImage(
uint32_t scIndex, uint32_t imgIndex, void* texture)
{
*(ID3D11Texture2D**)texture = this->Private->ColorSwapchains[scIndex].Images[imgIndex].texture;
}
//------------------------------------------------------------------------------
void vtkOpenXRManagerD3DGraphics::GetDepthSwapchainImage(
uint32_t scIndex, uint32_t imgIndex, void* texture)
{
*(ID3D11Texture2D**)texture = this->Private->DepthSwapchains[scIndex].Images[imgIndex].texture;
}
//------------------------------------------------------------------------------
void vtkOpenXRManagerD3DGraphics::EnumerateColorSwapchainImages(
XrSwapchain swapchain, uint32_t scIndex)
{
this->EnumerateSwapchainImages(swapchain, this->Private->ColorSwapchains[scIndex]);
}
//------------------------------------------------------------------------------
void vtkOpenXRManagerD3DGraphics::EnumerateDepthSwapchainImages(
XrSwapchain swapchain, uint32_t scIndex)
{
this->EnumerateSwapchainImages(swapchain, this->Private->DepthSwapchains[scIndex]);
}
//------------------------------------------------------------------------------
const std::vector<int64_t>& vtkOpenXRManagerD3DGraphics::GetSupportedColorFormats()
{
const static std::vector<int64_t> supportedColorFormats = { DXGI_FORMAT_R8G8B8A8_UNORM };
return supportedColorFormats;
}
//------------------------------------------------------------------------------
const std::vector<int64_t>& vtkOpenXRManagerD3DGraphics::GetSupportedDepthFormats()
{
const static std::vector<int64_t> supportedDepthFormats = { DXGI_FORMAT_D16_UNORM,
DXGI_FORMAT_D24_UNORM_S8_UINT, DXGI_FORMAT_D32_FLOAT, DXGI_FORMAT_D32_FLOAT_S8X24_UINT };
return supportedDepthFormats;
}
//------------------------------------------------------------------------------
void vtkOpenXRManagerD3DGraphics::EnumerateSwapchainImages(
XrSwapchain swapchain, SwapchainImagesD3D& swapchainImages)
{
uint32_t chainLength = this->GetChainLength(swapchain);
swapchainImages.Images.resize(chainLength, { XR_TYPE_SWAPCHAIN_IMAGE_D3D11_KHR });
vtkOpenXRManager::GetInstance().XrCheckError(
xrEnumerateSwapchainImages(swapchain, (uint32_t)swapchainImages.Images.size(), &chainLength,
reinterpret_cast<XrSwapchainImageBaseHeader*>(swapchainImages.Images.data())),
"Failed to enumerate swapchain images");
}
//------------------------------------------------------------------------------
bool vtkOpenXRManagerD3DGraphics::CreateGraphicsBinding(vtkOpenGLRenderWindow* helperWindow)
{
vtkWin32OpenGLDXRenderWindow* d3dWindow =
vtkWin32OpenGLDXRenderWindow::SafeDownCast(helperWindow);
if (d3dWindow)
{
auto graphicsBindingDXWin32 =
std::shared_ptr<XrGraphicsBindingD3D11KHR>(new XrGraphicsBindingD3D11KHR{
XR_TYPE_GRAPHICS_BINDING_D3D11_KHR, // .type
nullptr, // .next
{ 0 } // ID3D11Device* device
});
this->GraphicsBinding = graphicsBindingDXWin32;
graphicsBindingDXWin32->device = d3dWindow->GetDevice();
}
return true;
}
//------------------------------------------------------------------------------
bool vtkOpenXRManagerD3DGraphics::CheckGraphicsRequirements(XrInstance instance, XrSystemId id)
{
XrGraphicsRequirementsD3D11KHR graphicsRequirements{ XR_TYPE_GRAPHICS_REQUIREMENTS_D3D11_KHR };
xr::GraphicsExtensionDispatchTable extensions;
extensions.PopulateDispatchTable(instance);
if (!vtkOpenXRManager::GetInstance().XrCheckError(
extensions.xrGetD3D11GraphicsRequirementsKHR(instance, id, &graphicsRequirements),
"Failed to get DirectX graphics requirements!"))
{
return false;
}
// Create a list of feature levels which are both supported by the OpenXR runtime and this
// application. vtkWin32OpenGLDXRenderWindow only supports D3D11 for now.
constexpr D3D_FEATURE_LEVEL levels[] = { D3D_FEATURE_LEVEL_11_1 };
auto supportedLevel =
std::lower_bound(std::begin(levels), std::end(levels), graphicsRequirements.minFeatureLevel);
if (supportedLevel == std::end(levels))
{
vtkErrorMacro("Unsupported minimum feature level!");
return false;
};
return true;
}
//------------------------------------------------------------------------------
const char* vtkOpenXRManagerD3DGraphics::GetBackendExtensionName()
{
return XR_KHR_D3D11_ENABLE_EXTENSION_NAME;
}
VTK_ABI_NAMESPACE_END
|