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 if ((xsub & XRT_SWAPCHAIN_USAGE_UNORDERED_ACCESS) != 0) {
62 state |= D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
63 }
64 if ((xsub & XRT_SWAPCHAIN_USAGE_COLOR) != 0) {
65 // since we are treating these as mutually exclusive
66 assert((xsub & XRT_SWAPCHAIN_USAGE_DEPTH_STENCIL) == 0);
67 state |= D3D12_RESOURCE_STATE_RENDER_TARGET;
68 }
69 if ((xsub & XRT_SWAPCHAIN_USAGE_DEPTH_STENCIL) != 0) {
70 state |= D3D12_RESOURCE_STATE_DEPTH_WRITE;
71 }
72 return state;
73}
74
75/**
76 * Given swapchain usage bits, determine the resource usage state expected by the compositor
77 *
78 * @param xsub
79 * @return D3D12_RESOURCE_STATES
80 */
81static inline D3D12_RESOURCE_STATES
82d3d_convert_usage_bits_to_d3d12_compositor_resource_state(enum xrt_swapchain_usage_bits xsub)
83{
84 D3D12_RESOURCE_STATES state = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
85
86 if ((xsub & XRT_SWAPCHAIN_USAGE_UNORDERED_ACCESS) != 0) {
87 state |= D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
88 }
89 // if ((xsub & XRT_SWAPCHAIN_USAGE_COLOR) != 0) {
90 // // since we are treating these as mutually exclusive
91 // assert((xsub & XRT_SWAPCHAIN_USAGE_DEPTH_STENCIL) == 0);
92 // state |= D3D12_RESOURCE_STATE_RENDER_TARGET;
93 // }
94 if ((xsub & XRT_SWAPCHAIN_USAGE_DEPTH_STENCIL) != 0) {
95 state |= D3D12_RESOURCE_STATE_DEPTH_READ;
96 }
97 return state;
98}
99
100#ifdef __cplusplus
101}
102#endif
103
104#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.