-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathgraphicsplugin_d3d11.cpp
More file actions
365 lines (291 loc) · 18.4 KB
/
graphicsplugin_d3d11.cpp
File metadata and controls
365 lines (291 loc) · 18.4 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright (c) 2017-2026 The Khronos Group Inc.
//
// SPDX-License-Identifier: Apache-2.0
#include "pch.h"
#include "common.h"
#include "geometry.h"
#include "graphicsplugin.h"
#include "graphics_plugin_impl_helpers.h"
#include "options.h"
#if defined(XR_USE_GRAPHICS_API_D3D11)
#include <common/xr_linear.h>
#include <DirectXColors.h>
#include <D3Dcompiler.h>
#include "d3d_common.h"
using namespace Microsoft::WRL;
using namespace DirectX;
#define XRC_CHECK_THROW_HRCMD CHECK_HRCMD
namespace {
void InitializeD3D11DeviceForAdapter(IDXGIAdapter1* adapter, const std::vector<D3D_FEATURE_LEVEL>& featureLevels,
ID3D11Device** device, ID3D11DeviceContext** deviceContext) {
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if !defined(NDEBUG)
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
// Create the Direct3D 11 API device object and a corresponding context.
D3D_DRIVER_TYPE driverType = ((adapter == nullptr) ? D3D_DRIVER_TYPE_HARDWARE : D3D_DRIVER_TYPE_UNKNOWN);
TryAgain:
HRESULT hr = D3D11CreateDevice(adapter, driverType, 0, creationFlags, featureLevels.data(), (UINT)featureLevels.size(),
D3D11_SDK_VERSION, device, nullptr, deviceContext);
if (FAILED(hr)) {
// If initialization failed, it may be because device debugging isn't supported, so retry without that.
if ((creationFlags & D3D11_CREATE_DEVICE_DEBUG) && (hr == DXGI_ERROR_SDK_COMPONENT_MISSING)) {
creationFlags &= ~D3D11_CREATE_DEVICE_DEBUG;
goto TryAgain;
}
// If the initialization still fails, fall back to the WARP device.
// For more information on WARP, see: http://go.microsoft.com/fwlink/?LinkId=286690
if (driverType != D3D_DRIVER_TYPE_WARP) {
driverType = D3D_DRIVER_TYPE_WARP;
goto TryAgain;
}
}
}
struct D3D11GraphicsPlugin : public IGraphicsPlugin {
D3D11GraphicsPlugin(const std::shared_ptr<Options>& options, std::shared_ptr<IPlatformPlugin>)
: m_clearColor(options->GetBackgroundClearColor()) {}
std::vector<std::string> GetInstanceExtensions() const override { return {XR_KHR_D3D11_ENABLE_EXTENSION_NAME}; }
void InitializeDevice(XrInstance instance, XrSystemId systemId) override {
PFN_xrGetD3D11GraphicsRequirementsKHR pfnGetD3D11GraphicsRequirementsKHR = nullptr;
CHECK_XRCMD(xrGetInstanceProcAddr(instance, "xrGetD3D11GraphicsRequirementsKHR",
reinterpret_cast<PFN_xrVoidFunction*>(&pfnGetD3D11GraphicsRequirementsKHR)));
// Create the D3D11 device for the adapter associated with the system.
XrGraphicsRequirementsD3D11KHR graphicsRequirements{XR_TYPE_GRAPHICS_REQUIREMENTS_D3D11_KHR};
CHECK_XRCMD(pfnGetD3D11GraphicsRequirementsKHR(instance, systemId, &graphicsRequirements));
const ComPtr<IDXGIAdapter1> adapter = GetAdapter(graphicsRequirements.adapterLuid);
// Create a list of feature levels which are both supported by the OpenXR runtime and this application.
std::vector<D3D_FEATURE_LEVEL> featureLevels = {D3D_FEATURE_LEVEL_12_1, D3D_FEATURE_LEVEL_12_0, D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0};
featureLevels.erase(std::remove_if(featureLevels.begin(), featureLevels.end(),
[&](D3D_FEATURE_LEVEL fl) { return fl < graphicsRequirements.minFeatureLevel; }),
featureLevels.end());
CHECK_MSG(featureLevels.size() != 0, "Unsupported minimum feature level!");
InitializeD3D11DeviceForAdapter(adapter.Get(), featureLevels, m_device.ReleaseAndGetAddressOf(),
m_deviceContext.ReleaseAndGetAddressOf());
InitializeResources();
m_graphicsBinding.device = m_device.Get();
}
void InitializeResources() {
const ComPtr<ID3DBlob> vertexShaderBytes = CompileShader(ShaderHlsl, "MainVS", "vs_5_0");
CHECK_HRCMD(m_device->CreateVertexShader(vertexShaderBytes->GetBufferPointer(), vertexShaderBytes->GetBufferSize(), nullptr,
m_vertexShader.ReleaseAndGetAddressOf()));
const ComPtr<ID3DBlob> pixelShaderBytes = CompileShader(ShaderHlsl, "MainPS", "ps_5_0");
CHECK_HRCMD(m_device->CreatePixelShader(pixelShaderBytes->GetBufferPointer(), pixelShaderBytes->GetBufferSize(), nullptr,
m_pixelShader.ReleaseAndGetAddressOf()));
const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
CHECK_HRCMD(m_device->CreateInputLayout(vertexDesc, (UINT)ArraySize(vertexDesc), vertexShaderBytes->GetBufferPointer(),
vertexShaderBytes->GetBufferSize(), &m_inputLayout));
const CD3D11_BUFFER_DESC modelConstantBufferDesc(sizeof(ModelConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
CHECK_HRCMD(m_device->CreateBuffer(&modelConstantBufferDesc, nullptr, m_modelCBuffer.ReleaseAndGetAddressOf()));
const CD3D11_BUFFER_DESC viewProjectionConstantBufferDesc(sizeof(ViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
CHECK_HRCMD(
m_device->CreateBuffer(&viewProjectionConstantBufferDesc, nullptr, m_viewProjectionCBuffer.ReleaseAndGetAddressOf()));
const D3D11_SUBRESOURCE_DATA vertexBufferData{Geometry::c_cubeVertices};
const CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(Geometry::c_cubeVertices), D3D11_BIND_VERTEX_BUFFER);
CHECK_HRCMD(m_device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, m_cubeVertexBuffer.ReleaseAndGetAddressOf()));
const D3D11_SUBRESOURCE_DATA indexBufferData{Geometry::c_cubeIndices};
const CD3D11_BUFFER_DESC indexBufferDesc(sizeof(Geometry::c_cubeIndices), D3D11_BIND_INDEX_BUFFER);
CHECK_HRCMD(m_device->CreateBuffer(&indexBufferDesc, &indexBufferData, m_cubeIndexBuffer.ReleaseAndGetAddressOf()));
}
// Select the preferred swapchain format from the list of available formats.
int64_t SelectColorSwapchainFormat(bool throwIfNotFound, span<const int64_t> imageFormatArray) const override {
// List of supported color swapchain formats.
return SelectSwapchainFormat( //
throwIfNotFound, imageFormatArray,
{
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
DXGI_FORMAT_B8G8R8A8_UNORM_SRGB,
DXGI_FORMAT_R8G8B8A8_UNORM,
DXGI_FORMAT_B8G8R8A8_UNORM,
});
}
// Select the preferred swapchain format from the list of available formats.
int64_t SelectDepthSwapchainFormat(bool throwIfNotFound, span<const int64_t> imageFormatArray) const override {
// List of supported depth swapchain formats.
return SelectSwapchainFormat( //
throwIfNotFound, imageFormatArray,
{
DXGI_FORMAT_D32_FLOAT,
DXGI_FORMAT_D24_UNORM_S8_UINT,
DXGI_FORMAT_D16_UNORM,
DXGI_FORMAT_D32_FLOAT_S8X24_UINT,
});
}
const XrBaseInStructure* GetGraphicsBinding() const override {
return reinterpret_cast<const XrBaseInStructure*>(&m_graphicsBinding);
}
struct D3D11FallbackDepthTexture {
public:
D3D11FallbackDepthTexture() = default;
void Reset() {
m_texture = nullptr;
m_xrImage.texture = nullptr;
}
bool Allocated() const { return m_texture != nullptr; }
void Allocate(ID3D11Device* d3d11Device, UINT width, UINT height, UINT arraySize) {
Reset();
D3D11_TEXTURE2D_DESC depthDesc{};
depthDesc.Width = width;
depthDesc.Height = height;
depthDesc.ArraySize = arraySize;
depthDesc.MipLevels = 1;
depthDesc.Format = DXGI_FORMAT_R32_TYPELESS;
depthDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_DEPTH_STENCIL;
depthDesc.SampleDesc.Count = 1;
XRC_CHECK_THROW_HRCMD(d3d11Device->CreateTexture2D(&depthDesc, nullptr, m_texture.ReleaseAndGetAddressOf()));
m_xrImage.texture = m_texture.Get();
}
const XrSwapchainImageD3D11KHR& GetTexture() const { return m_xrImage; }
private:
ComPtr<ID3D11Texture2D> m_texture{};
XrSwapchainImageD3D11KHR m_xrImage{XR_TYPE_SWAPCHAIN_IMAGE_D3D11_KHR};
};
class D3D11SwapchainImageData : public SwapchainImageDataBase<XrSwapchainImageD3D11KHR> {
public:
D3D11SwapchainImageData(ComPtr<ID3D11Device> device, uint32_t capacity, const XrSwapchainCreateInfo& createInfo,
XrSwapchain depthSwapchain, const XrSwapchainCreateInfo& depthCreateInfo)
: SwapchainImageDataBase(XR_TYPE_SWAPCHAIN_IMAGE_D3D11_KHR, capacity, createInfo, depthSwapchain, depthCreateInfo),
m_device(std::move(device))
{}
D3D11SwapchainImageData(ComPtr<ID3D11Device> device, uint32_t capacity, const XrSwapchainCreateInfo& createInfo)
: SwapchainImageDataBase(XR_TYPE_SWAPCHAIN_IMAGE_D3D11_KHR, capacity, createInfo),
m_device(std::move(device)),
m_internalDepthTextures(capacity) {}
void Reset() override {
m_internalDepthTextures.clear();
m_device = nullptr;
SwapchainImageDataBase::Reset();
}
const XrSwapchainImageD3D11KHR& GetFallbackDepthSwapchainImage(uint32_t i) override {
if (!m_internalDepthTextures[i].Allocated()) {
m_internalDepthTextures[i].Allocate(m_device.Get(), this->Width(), this->Height(), this->ArraySize());
}
return m_internalDepthTextures[i].GetTexture();
}
private:
ComPtr<ID3D11Device> m_device;
std::vector<D3D11FallbackDepthTexture> m_internalDepthTextures;
};
ISwapchainImageData* AllocateSwapchainImageData(size_t size, const XrSwapchainCreateInfo& swapchainCreateInfo) override {
auto d3d11Device = m_device;
auto typedResult = std::make_unique<D3D11SwapchainImageData>(d3d11Device, uint32_t(size), swapchainCreateInfo);
// Cast our derived type to the caller-expected type.
auto ret = static_cast<ISwapchainImageData*>(typedResult.get());
m_swapchainImageDataMap.Adopt(std::move(typedResult));
return ret;
}
inline ISwapchainImageData* AllocateSwapchainImageDataWithDepthSwapchain(
size_t size, const XrSwapchainCreateInfo& colorSwapchainCreateInfo, XrSwapchain depthSwapchain,
const XrSwapchainCreateInfo& depthSwapchainCreateInfo) override {
auto d3d11Device = m_device;
auto typedResult = std::make_unique<D3D11SwapchainImageData>(d3d11Device, uint32_t(size), colorSwapchainCreateInfo,
depthSwapchain, depthSwapchainCreateInfo);
// Cast our derived type to the caller-expected type.
auto ret = static_cast<ISwapchainImageData*>(typedResult.get());
m_swapchainImageDataMap.Adopt(std::move(typedResult));
return ret;
}
ComPtr<ID3D11RenderTargetView> CreateRenderTargetView(D3D11SwapchainImageData& swapchainData, uint32_t imageIndex,
uint32_t imageArrayIndex) const {
auto d3d11Device = m_device;
// Create RenderTargetView with original swapchain format (swapchain is typeless).
ComPtr<ID3D11RenderTargetView> renderTargetView;
const CD3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc(
(swapchainData.SampleCount() > 1) ? D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY : D3D11_RTV_DIMENSION_TEXTURE2DARRAY,
(DXGI_FORMAT)swapchainData.GetCreateInfo().format, 0 /* mipSlice */, imageArrayIndex, 1 /* arraySize */);
ID3D11Texture2D* const colorTexture = swapchainData.GetTypedImage(imageIndex).texture;
XRC_CHECK_THROW_HRCMD(
d3d11Device->CreateRenderTargetView(colorTexture, &renderTargetViewDesc, renderTargetView.ReleaseAndGetAddressOf()));
return renderTargetView;
}
ComPtr<ID3D11DepthStencilView> CreateDepthStencilView(D3D11SwapchainImageData& swapchainData, uint32_t imageIndex,
uint32_t imageArrayIndex) const {
auto d3d11Device = m_device;
// Clear depth buffer.
const ComPtr<ID3D11Texture2D> depthStencilTexture = swapchainData.GetDepthImageForColorIndex(imageIndex).texture;
ComPtr<ID3D11DepthStencilView> depthStencilView;
const XrSwapchainCreateInfo* depthCreateInfo = swapchainData.GetDepthCreateInfo();
DXGI_FORMAT depthSwapchainFormatDX = (DXGI_FORMAT)depthCreateInfo->format;
CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(
(swapchainData.DepthSampleCount() > 1) ? D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY : D3D11_DSV_DIMENSION_TEXTURE2DARRAY,
depthSwapchainFormatDX, 0 /* mipSlice */, imageArrayIndex, 1 /* arraySize */);
XRC_CHECK_THROW_HRCMD(
d3d11Device->CreateDepthStencilView(depthStencilTexture.Get(), &depthStencilViewDesc, depthStencilView.GetAddressOf()));
return depthStencilView;
}
void RenderView(const XrCompositionLayerProjectionView& layerView, const XrSwapchainImageBaseHeader* swapchainImage,
int64_t /* colorSwapchainFormat */, int64_t /* depthSwapchainFormat */,
const std::vector<Cube>& cubes) override {
CHECK(layerView.subImage.imageArrayIndex == 0); // Texture arrays not supported.
D3D11SwapchainImageData* swapchainData;
uint32_t imageIndex;
std::tie(swapchainData, imageIndex) = m_swapchainImageDataMap.GetDataAndIndexFromBasePointer(swapchainImage);
CD3D11_VIEWPORT viewport((float)layerView.subImage.imageRect.offset.x, (float)layerView.subImage.imageRect.offset.y,
(float)layerView.subImage.imageRect.extent.width,
(float)layerView.subImage.imageRect.extent.height);
m_deviceContext->RSSetViewports(1, &viewport);
// Create RenderTargetView with original swapchain format (swapchain is typeless).
ComPtr<ID3D11RenderTargetView> renderTargetView =
CreateRenderTargetView(*swapchainData, imageIndex, layerView.subImage.imageArrayIndex);
ComPtr<ID3D11DepthStencilView> depthStencilView =
CreateDepthStencilView(*swapchainData, imageIndex, layerView.subImage.imageArrayIndex);
// Clear swapchain and depth buffer. NOTE: This will clear the entire render target view, not just the specified view.
m_deviceContext->ClearRenderTargetView(renderTargetView.Get(), static_cast<const FLOAT*>(m_clearColor.data()));
m_deviceContext->ClearDepthStencilView(depthStencilView.Get(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
ID3D11RenderTargetView* renderTargets[] = {renderTargetView.Get()};
m_deviceContext->OMSetRenderTargets((UINT)ArraySize(renderTargets), renderTargets, depthStencilView.Get());
const XMMATRIX spaceToView = XMMatrixInverse(nullptr, LoadXrPose(layerView.pose));
XrMatrix4x4f projectionMatrix;
XrMatrix4x4f_CreateProjectionFov(&projectionMatrix, GRAPHICS_D3D, layerView.fov, 0.05f, 100.0f);
// Set shaders and constant buffers.
ViewProjectionConstantBuffer viewProjection;
XMStoreFloat4x4(&viewProjection.ViewProjection, XMMatrixTranspose(spaceToView * LoadXrMatrix(projectionMatrix)));
m_deviceContext->UpdateSubresource(m_viewProjectionCBuffer.Get(), 0, nullptr, &viewProjection, 0, 0);
ID3D11Buffer* const constantBuffers[] = {m_modelCBuffer.Get(), m_viewProjectionCBuffer.Get()};
m_deviceContext->VSSetConstantBuffers(0, (UINT)ArraySize(constantBuffers), constantBuffers);
m_deviceContext->VSSetShader(m_vertexShader.Get(), nullptr, 0);
m_deviceContext->PSSetShader(m_pixelShader.Get(), nullptr, 0);
// Set cube primitive data.
const UINT strides[] = {sizeof(Geometry::Vertex)};
const UINT offsets[] = {0};
ID3D11Buffer* vertexBuffers[] = {m_cubeVertexBuffer.Get()};
m_deviceContext->IASetVertexBuffers(0, (UINT)ArraySize(vertexBuffers), vertexBuffers, strides, offsets);
m_deviceContext->IASetIndexBuffer(m_cubeIndexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0);
m_deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
m_deviceContext->IASetInputLayout(m_inputLayout.Get());
// Render each cube
for (const Cube& cube : cubes) {
// Compute and update the model transform.
ModelConstantBuffer model;
XMStoreFloat4x4(&model.Model,
XMMatrixTranspose(XMMatrixScaling(cube.Scale.x, cube.Scale.y, cube.Scale.z) * LoadXrPose(cube.Pose)));
m_deviceContext->UpdateSubresource(m_modelCBuffer.Get(), 0, nullptr, &model, 0, 0);
// Draw the cube.
m_deviceContext->DrawIndexed((UINT)ArraySize(Geometry::c_cubeIndices), 0, 0);
}
}
uint32_t GetSupportedSwapchainSampleCount(const XrViewConfigurationView&) override { return 1; }
void UpdateOptions(const std::shared_ptr<Options>& options) override { m_clearColor = options->GetBackgroundClearColor(); }
private:
ComPtr<ID3D11Device> m_device;
ComPtr<ID3D11DeviceContext> m_deviceContext;
XrGraphicsBindingD3D11KHR m_graphicsBinding{XR_TYPE_GRAPHICS_BINDING_D3D11_KHR};
SwapchainImageDataMap<D3D11SwapchainImageData> m_swapchainImageDataMap;
ComPtr<ID3D11VertexShader> m_vertexShader;
ComPtr<ID3D11PixelShader> m_pixelShader;
ComPtr<ID3D11InputLayout> m_inputLayout;
ComPtr<ID3D11Buffer> m_modelCBuffer;
ComPtr<ID3D11Buffer> m_viewProjectionCBuffer;
ComPtr<ID3D11Buffer> m_cubeVertexBuffer;
ComPtr<ID3D11Buffer> m_cubeIndexBuffer;
std::array<float, 4> m_clearColor;
};
} // namespace
std::shared_ptr<IGraphicsPlugin> CreateGraphicsPlugin_D3D11(const std::shared_ptr<Options>& options,
std::shared_ptr<IPlatformPlugin> platformPlugin) {
return std::make_shared<D3D11GraphicsPlugin>(options, platformPlugin);
}
#endif