Skip to content

Commit fc5662f

Browse files
committed
[hello_xr] Use preferred depth swapchain format rather than (somewhat unsupported) D32.
1 parent 2a24f3b commit fc5662f

8 files changed

Lines changed: 31 additions & 25 deletions

src/tests/hello_xr/graphicsplugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct IGraphicsPlugin {
5555

5656
// Render to a swapchain image for a projection view.
5757
virtual void RenderView(const XrCompositionLayerProjectionView& layerView, const XrSwapchainImageBaseHeader* swapchainImage,
58-
int64_t swapchainFormat, const std::vector<Cube>& cubes) = 0;
58+
int64_t colorSwapchainFormat, int64_t depthSwapchainFormat, const std::vector<Cube>& cubes) = 0;
5959

6060
// Get recommended number of sub-data element samples in view (recommendedSwapchainSampleCount)
6161
// if supported by the graphics plugin. A supported value otherwise.

src/tests/hello_xr/graphicsplugin_d3d11.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ struct D3D11GraphicsPlugin : public IGraphicsPlugin {
273273
}
274274

275275
void RenderView(const XrCompositionLayerProjectionView& layerView, const XrSwapchainImageBaseHeader* swapchainImage,
276-
int64_t /* swapchainFormat */, const std::vector<Cube>& cubes) override {
276+
int64_t /* colorSwapchainFormat */, int64_t /* depthSwapchainFormat */, const std::vector<Cube>& cubes) override {
277277
CHECK(layerView.subImage.imageArrayIndex == 0); // Texture arrays not supported.
278278

279279
D3D11SwapchainImageData* swapchainData;

src/tests/hello_xr/graphicsplugin_d3d12.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,9 @@ struct D3D12GraphicsPlugin : public IGraphicsPlugin {
458458
return ret;
459459
}
460460

461-
ID3D12PipelineState* GetOrCreatePipelineState(DXGI_FORMAT swapchainFormat) {
462-
auto iter = m_pipelineStates.find(swapchainFormat);
461+
ID3D12PipelineState* GetOrCreatePipelineState(DXGI_FORMAT colorSwapchainFormat, DXGI_FORMAT depthSwapchainFormat) {
462+
auto swapchainFormats = std::make_pair(colorSwapchainFormat, depthSwapchainFormat);
463+
auto iter = m_pipelineStates.find(swapchainFormats);
463464
if (iter != m_pipelineStates.end()) {
464465
return iter->second.Get();
465466
}
@@ -525,8 +526,8 @@ struct D3D12GraphicsPlugin : public IGraphicsPlugin {
525526
pipelineStateDesc.IBStripCutValue = D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFF;
526527
pipelineStateDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
527528
pipelineStateDesc.NumRenderTargets = 1;
528-
pipelineStateDesc.RTVFormats[0] = swapchainFormat;
529-
pipelineStateDesc.DSVFormat = DXGI_FORMAT_D32_FLOAT;
529+
pipelineStateDesc.RTVFormats[0] = colorSwapchainFormat;
530+
pipelineStateDesc.DSVFormat = depthSwapchainFormat;
530531
pipelineStateDesc.SampleDesc = {1, 0};
531532
pipelineStateDesc.NodeMask = 0;
532533
pipelineStateDesc.CachedPSO = {nullptr, 0};
@@ -537,13 +538,13 @@ struct D3D12GraphicsPlugin : public IGraphicsPlugin {
537538
reinterpret_cast<void**>(pipelineState.ReleaseAndGetAddressOf())));
538539
ID3D12PipelineState* pipelineStateRaw = pipelineState.Get();
539540

540-
m_pipelineStates.emplace(swapchainFormat, std::move(pipelineState));
541+
m_pipelineStates.emplace(swapchainFormats, std::move(pipelineState));
541542

542543
return pipelineStateRaw;
543544
}
544545

545546
void RenderView(const XrCompositionLayerProjectionView& layerView, const XrSwapchainImageBaseHeader* swapchainImage,
546-
int64_t swapchainFormat, const std::vector<Cube>& cubes) override {
547+
int64_t colorSwapchainFormat, int64_t depthSwapchainFormat, const std::vector<Cube>& cubes) override {
547548
CHECK(layerView.subImage.imageArrayIndex == 0); // Texture arrays not supported.
548549

549550
D3D12SwapchainImageData* swapchainData;
@@ -561,7 +562,8 @@ struct D3D12GraphicsPlugin : public IGraphicsPlugin {
561562
0, D3D12_COMMAND_LIST_TYPE_DIRECT, swapchainData->GetCommandAllocator(), nullptr, __uuidof(ID3D12GraphicsCommandList),
562563
reinterpret_cast<void**>(cmdList.ReleaseAndGetAddressOf())));
563564

564-
ID3D12PipelineState* pipelineState = GetOrCreatePipelineState((DXGI_FORMAT)swapchainFormat);
565+
ID3D12PipelineState* pipelineState =
566+
GetOrCreatePipelineState((DXGI_FORMAT)colorSwapchainFormat, (DXGI_FORMAT)depthSwapchainFormat);
565567
cmdList->SetPipelineState(pipelineState);
566568
cmdList->SetGraphicsRootSignature(m_rootSignature.Get());
567569

@@ -584,7 +586,7 @@ struct D3D12GraphicsPlugin : public IGraphicsPlugin {
584586
// Create RenderTargetView with original swapchain format (swapchain is typeless).
585587
D3D12_CPU_DESCRIPTOR_HANDLE renderTargetView = m_rtvHeap->GetCPUDescriptorHandleForHeapStart();
586588
D3D12_RENDER_TARGET_VIEW_DESC renderTargetViewDesc{};
587-
renderTargetViewDesc.Format = (DXGI_FORMAT)swapchainFormat;
589+
renderTargetViewDesc.Format = (DXGI_FORMAT)colorSwapchainFormat;
588590
if (colorTextureDesc.DepthOrArraySize > 1) {
589591
if (colorTextureDesc.SampleDesc.Count > 1) {
590592
renderTargetViewDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY;
@@ -606,7 +608,7 @@ struct D3D12GraphicsPlugin : public IGraphicsPlugin {
606608
const D3D12_RESOURCE_DESC depthStencilTextureDesc = depthStencilTexture->GetDesc();
607609
D3D12_CPU_DESCRIPTOR_HANDLE depthStencilView = m_dsvHeap->GetCPUDescriptorHandleForHeapStart();
608610
D3D12_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc{};
609-
depthStencilViewDesc.Format = DXGI_FORMAT_D32_FLOAT;
611+
depthStencilViewDesc.Format = (DXGI_FORMAT)depthSwapchainFormat;
610612
if (depthStencilTextureDesc.DepthOrArraySize > 1) {
611613
if (depthStencilTextureDesc.SampleDesc.Count > 1) {
612614
depthStencilViewDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DMSARRAY;
@@ -730,7 +732,7 @@ struct D3D12GraphicsPlugin : public IGraphicsPlugin {
730732
SwapchainImageDataMap<D3D12SwapchainImageData> m_swapchainImageDataMap;
731733
XrGraphicsBindingD3D12KHR m_graphicsBinding{XR_TYPE_GRAPHICS_BINDING_D3D12_KHR};
732734
ComPtr<ID3D12RootSignature> m_rootSignature;
733-
std::map<DXGI_FORMAT, ComPtr<ID3D12PipelineState>> m_pipelineStates;
735+
std::map<std::pair<DXGI_FORMAT, DXGI_FORMAT>, ComPtr<ID3D12PipelineState>> m_pipelineStates;
734736
ComPtr<ID3D12Resource> m_cubeVertexBuffer;
735737
ComPtr<ID3D12Resource> m_cubeIndexBuffer;
736738
ComPtr<ID3D12DescriptorHeap> m_rtvHeap;

src/tests/hello_xr/graphicsplugin_metal.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,19 +308,20 @@ struct MetalGraphicsPlugin : public IGraphicsPlugin {
308308
}
309309

310310
void RenderView(const XrCompositionLayerProjectionView& layerView, const XrSwapchainImageBaseHeader* swapchainImage,
311-
int64_t swapchainFormat, const std::vector<Cube>& cubes) override {
311+
int64_t colorSwapchainFormat, int64_t depthSwapchainFormat, const std::vector<Cube>& cubes) override {
312312
auto pAutoReleasePool = NS::TransferPtr(NS::AutoreleasePool::alloc()->init());
313313

314314
SwapchainContext& swapchainContext =
315315
m_swapchainImageDataMap.GetDataAndIndexFromBasePointer(swapchainImage).first->swapchainContext;
316316

317-
auto mtlSwapchainFormat = (MTL::PixelFormat)swapchainFormat;
318-
if (mtlSwapchainFormat != m_colorAttachmentFormat) {
317+
auto mtlColorSwapchainFormat = (MTL::PixelFormat)colorSwapchainFormat;
318+
auto mtlDepthSwapchainFormat = (MTL::PixelFormat)depthSwapchainFormat;
319+
if ((mtlColorSwapchainFormat != m_colorAttachmentFormat) || (mtlDepthSwapchainFormat != m_depthAttachmentFormat)) {
319320
auto pDesc = NS::TransferPtr(MTL::RenderPipelineDescriptor::alloc()->init());
320321
pDesc->setVertexFunction(m_vertexFunction.get());
321322
pDesc->setFragmentFunction(m_fragmentFunction.get());
322-
pDesc->colorAttachments()->object(0)->setPixelFormat(mtlSwapchainFormat);
323-
pDesc->setDepthAttachmentPixelFormat(MTL::PixelFormatDepth32Float);
323+
pDesc->colorAttachments()->object(0)->setPixelFormat(mtlColorSwapchainFormat);
324+
pDesc->setDepthAttachmentPixelFormat(mtlDepthSwapchainFormat);
324325

325326
NS::Error* pError = nullptr;
326327
m_pipelineStateObject = NS::TransferPtr(m_device->newRenderPipelineState(pDesc.get(), &pError));
@@ -329,7 +330,7 @@ struct MetalGraphicsPlugin : public IGraphicsPlugin {
329330
assert(false);
330331
return;
331332
}
332-
m_colorAttachmentFormat = mtlSwapchainFormat;
333+
m_colorAttachmentFormat = mtlColorSwapchainFormat;
333334
}
334335

335336
CHECK(layerView.subImage.imageArrayIndex == 0); // Texture arrays not supported.
@@ -339,7 +340,7 @@ struct MetalGraphicsPlugin : public IGraphicsPlugin {
339340
if (!m_depthStencilTexture) {
340341
auto depthTextureDescriptor = NS::TransferPtr(MTL::TextureDescriptor::alloc()->init());
341342
depthTextureDescriptor->setTextureType(colorTexture->textureType());
342-
depthTextureDescriptor->setPixelFormat(MTL::PixelFormatDepth32Float);
343+
depthTextureDescriptor->setPixelFormat(mtlDepthSwapchainFormat);
343344
depthTextureDescriptor->setWidth(colorTexture->width());
344345
depthTextureDescriptor->setHeight(colorTexture->height());
345346
depthTextureDescriptor->setUsage(MTL::TextureUsageRenderTarget);
@@ -423,6 +424,7 @@ struct MetalGraphicsPlugin : public IGraphicsPlugin {
423424
NS::SharedPtr<MTL::RenderPipelineState> m_pipelineStateObject;
424425
NS::SharedPtr<MTL::DepthStencilState> m_depthStencilState;
425426
MTL::PixelFormat m_colorAttachmentFormat{MTL::PixelFormatInvalid};
427+
MTL::PixelFormat m_depthAttachmentFormat{MTL::PixelFormatInvalid};
426428

427429
NS::SharedPtr<MTL::Library> m_library;
428430
NS::SharedPtr<MTL::Function> m_vertexFunction;

src/tests/hello_xr/graphicsplugin_opengl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,10 @@ struct OpenGLGraphicsPlugin : public IGraphicsPlugin {
407407
}
408408

409409
void RenderView(const XrCompositionLayerProjectionView& layerView, const XrSwapchainImageBaseHeader* swapchainImage,
410-
int64_t swapchainFormat, const std::vector<Cube>& cubes) override {
410+
int64_t colorSwapchainFormat, int64_t depthSwapchainFormat, const std::vector<Cube>& cubes) override {
411411
CHECK(layerView.subImage.imageArrayIndex == 0); // Texture arrays not supported.
412-
UNUSED_PARM(swapchainFormat); // Not used in this function for now.
412+
UNUSED_PARM(colorSwapchainFormat); // Not used in this function for now.
413+
UNUSED_PARM(depthSwapchainFormat); // Not used in this function for now.
413414

414415
OpenGLSwapchainImageData* swapchainData;
415416
uint32_t imageIndex;

src/tests/hello_xr/graphicsplugin_opengles.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,10 @@ struct OpenGLESGraphicsPlugin : public IGraphicsPlugin {
336336
}
337337

338338
void RenderView(const XrCompositionLayerProjectionView& layerView, const XrSwapchainImageBaseHeader* swapchainImage,
339-
int64_t swapchainFormat, const std::vector<Cube>& cubes) override {
339+
int64_t colorSwapchainFormat, int64_t depthSwapchainFormat, const std::vector<Cube>& cubes) override {
340340
CHECK(layerView.subImage.imageArrayIndex == 0); // Texture arrays not supported.
341-
UNUSED_PARM(swapchainFormat); // Not used in this function for now.
341+
UNUSED_PARM(colorSwapchainFormat); // Not used in this function for now.
342+
UNUSED_PARM(depthSwapchainFormat); // Not used in this function for now.
342343

343344
OpenGLESSwapchainImageData* swapchainData;
344345
uint32_t imageIndex;

src/tests/hello_xr/graphicsplugin_vulkan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ struct VulkanGraphicsPlugin : public IGraphicsPlugin {
825825
}
826826

827827
void RenderView(const XrCompositionLayerProjectionView& layerView, const XrSwapchainImageBaseHeader* swapchainImage,
828-
int64_t /*swapchainFormat*/, const std::vector<Cube>& cubes) override {
828+
int64_t /*colorSwapchainFormat*/, int64_t /*depthSwapchainFormat*/, const std::vector<Cube>& cubes) override {
829829
CHECK(layerView.subImage.imageArrayIndex == 0); // Texture arrays not supported.
830830

831831
VulkanSwapchainImageData* swapchainData;

src/tests/hello_xr/openxr_program.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ struct OpenXrProgram : IOpenXrProgram {
10651065

10661066
const XrSwapchainImageBaseHeader* const swapchainImage =
10671067
m_swapchainImages[viewSwapchain.handle]->GetGenericColorImage(swapchainImageIndex);
1068-
m_graphicsPlugin->RenderView(projectionLayerViews[i], swapchainImage, m_colorSwapchainFormat, cubes);
1068+
m_graphicsPlugin->RenderView(projectionLayerViews[i], swapchainImage, m_colorSwapchainFormat, m_depthSwapchainFormat, cubes);
10691069

10701070
XrSwapchainImageReleaseInfo releaseInfo{XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO};
10711071
CHECK_XRCMD(xrReleaseSwapchainImage(viewSwapchain.handle, &releaseInfo));

0 commit comments

Comments
 (0)