Monado OpenXR Runtime
d3d_d3d12_bits.h
Go to the documentation of this file.
1// Copyright 2020-2022, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Usage bits for D3D12.
6 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
7 * @ingroup aux_d3d
8 */
9
10#pragma once
11
12#include "xrt/xrt_compositor.h"
13#include "xrt/xrt_windows.h"
14#include "xrt/xrt_config_have.h"
15
16#include "d3d12.h"
17
18#include <assert.h>
19
20#ifdef XRT_HAVE_D3D12
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26static inline D3D12_RESOURCE_FLAGS
27d3d_convert_usage_bits_to_d3d12_resource_flags(enum xrt_swapchain_usage_bits xsub)
28{
29 int ret = 0;
30 if ((xsub & XRT_SWAPCHAIN_USAGE_COLOR) != 0) {
31 ret |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
32 }
33 if ((xsub & XRT_SWAPCHAIN_USAGE_DEPTH_STENCIL) != 0) {
34 ret |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
35 }
36 if ((xsub & XRT_SWAPCHAIN_USAGE_UNORDERED_ACCESS) != 0) {
37 ret |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
38 }
39 // if this is omitted, D3D12 asks to add a bit to **deny** shader access.
40 // it's a bold api design strategy Cotton, let's see if it pays off for them. ;)
41 if ((xsub & XRT_SWAPCHAIN_USAGE_SAMPLED) == 0) {
42 // per https://docs.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_resource_flags
43 // this also depends on depth stencil
44 assert((xsub & XRT_SWAPCHAIN_USAGE_DEPTH_STENCIL) != 0);
45 ret |= D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;
46 }
47 return (D3D12_RESOURCE_FLAGS)ret;
48}
49
50/**
51 * Given swapchain usage bits, determine the resource usage state expected by the app
52 *
53 * @param xsub
54 * @return D3D12_RESOURCE_STATES
55 */
56static inline D3D12_RESOURCE_STATES
57d3d_convert_usage_bits_to_d3d12_app_resource_state(enum xrt_swapchain_usage_bits xsub)
58{
59 D3D12_RESOURCE_STATES state = D3D12_RESOURCE_STATES(0);
60
61 bool use_color = (xsub & XRT_SWAPCHAIN_USAGE_COLOR) != 0;
62 bool use_unordered = (xsub & XRT_SWAPCHAIN_USAGE_UNORDERED_ACCESS) != 0;
63 bool use_depth = (xsub & XRT_SWAPCHAIN_USAGE_DEPTH_STENCIL) != 0;
64 bool use_transfer_src = (xsub & XRT_SWAPCHAIN_USAGE_TRANSFER_SRC) != 0;
65 bool use_transfer_dst = (xsub & XRT_SWAPCHAIN_USAGE_TRANSFER_DST) != 0;
66 bool use_mutable = (xsub & XRT_SWAPCHAIN_USAGE_MUTABLE_FORMAT) != 0;
67 bool use_input_attachment = (xsub & XRT_SWAPCHAIN_USAGE_INPUT_ATTACHMENT) != 0;
68
69 /*
70 * When an application acquires a swapchain image by calling xrAcquireSwapchainImage in a session create using
71 * XrGraphicsBindingD3D12KHR, the OpenXR runtime must guarantee that:
72 *
73 * The color rendering target image has a resource state match with D3D12_RESOURCE_STATE_RENDER_TARGET
74 *
75 * The depth rendering target image has a resource state match with D3D12_RESOURCE_STATE_DEPTH_WRITE
76 */
77
78 if (use_color) {
79 // since we are treating these as mutually exclusive
80 assert(!use_depth);
81 state = D3D12_RESOURCE_STATE_RENDER_TARGET;
82 } else if (use_depth) {
83 state = D3D12_RESOURCE_STATE_DEPTH_WRITE;
84 } else {
85 //! @todo If neither color nor depth use is specified, we set an initial state that is not clearly
86 //! defined in the spec, but most likely expected by the application. Note that the order here
87 //! translates to precedence if multiple are set.
88 if (use_unordered) {
89 state = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
90 } else if (use_transfer_src) {
91 state = D3D12_RESOURCE_STATE_COPY_SOURCE;
92 } else if (use_transfer_dst) {
93 state = D3D12_RESOURCE_STATE_COPY_DEST;
94 } else if (use_mutable) {
95 state = D3D12_RESOURCE_STATE_COMMON;
96 } else if (use_input_attachment) {
97 state = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
98 } else {
99 //! @todo unspecified fallback
100 state = D3D12_RESOURCE_STATE_COMMON;
101 }
102 }
103
104 return state;
105}
106
107/**
108 * Given swapchain usage bits, determine the resource usage state expected by the compositor
109 *
110 * @param xsub
111 * @return D3D12_RESOURCE_STATES
112 */
113static inline D3D12_RESOURCE_STATES
114d3d_convert_usage_bits_to_d3d12_compositor_resource_state(enum xrt_swapchain_usage_bits xsub)
115{
116 D3D12_RESOURCE_STATES state = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
117
118 if ((xsub & XRT_SWAPCHAIN_USAGE_UNORDERED_ACCESS) != 0) {
119 state |= D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
120 }
121 // if ((xsub & XRT_SWAPCHAIN_USAGE_COLOR) != 0) {
122 // // since we are treating these as mutually exclusive
123 // assert((xsub & XRT_SWAPCHAIN_USAGE_DEPTH_STENCIL) == 0);
124 // state |= D3D12_RESOURCE_STATE_RENDER_TARGET;
125 // }
126 if ((xsub & XRT_SWAPCHAIN_USAGE_DEPTH_STENCIL) != 0) {
127 state |= D3D12_RESOURCE_STATE_DEPTH_READ;
128 }
129 return state;
130}
131
132#ifdef __cplusplus
133}
134#endif
135
136#endif // XRT_HAVE_D3D12
xrt_swapchain_usage_bits
Usage of the swapchain images.
Definition: xrt_compositor.h:506
Header declaring XRT graphics interfaces.
A minimal way to include Windows.h.