xref: /aosp_15_r20/external/mesa3d/src/microsoft/compiler/nir_to_dxil.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © Microsoft Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #ifndef NIR_TO_DXIL_H
25 #define NIR_TO_DXIL_H
26 
27 #include <stdbool.h>
28 
29 #include "nir.h"
30 #include "dxil_versions.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 struct blob;
37 
38 /* Controls how resource decls/accesses are handled. Common to all:
39  *   Images, textures, and samplers map to D3D UAV, SRV, and sampler types
40  *   Shared is lowered to explicit I/O and then to a DXIL-specific intrinsic for 4-byte indices instead of byte addressing
41  *   Input/output are lowered to dedicated intrinsics
42  */
43 enum dxil_environment {
44    /* In the GL environment:
45     *   Samplers/textures are lowered, vars/intrinsics use binding to refer to them; dynamic array indexing supported with offset srcs
46     *     The lowering done by mesa/st assigns bindings from 0 -> N
47     *   All other resource variables have driver_location set instead, assigned from 0 -> N
48     *   UBOs may or may not have interface variables, and are declared from ubo_binding_offset -> num_ubos
49     *   SSBOs may or may not have interface variables, and are declared from from 0 -> num_ssbos
50     *   Images are *not* lowered, so that dynamic indexing can deterministically get a base binding via the deref chain
51     *     TODO: Maybe support lowering and use nir_intrinsic_range_base to get the base
52     *   No immediate constant buffer, or scratch
53     */
54    DXIL_ENVIRONMENT_GL,
55    /* In the CL environment:
56     *   Shader kind is always KERNEL
57     *   All resources use binding for identification
58     *   Samplers/textures/images are lowered; dynamic indexing not supported by spec
59     *   UBOs are arrays of uints in the NIR
60     *   SSBOs are implicitly declared via num_kernel_globals
61     *   Variables of shader_temp are used to declare an immediate constant buffer, with load_ptr_dxil intrinsics to access it
62     *   Scratch is supported and lowered to DXIL-specific intrinsics for scalar 32-bit access
63     */
64    DXIL_ENVIRONMENT_CL,
65    /* In the Vulkan environment:
66     *   All resources use binding / descriptor_set for identification
67     *   Samplers/textures/images support two modes:
68     *     1. Derefs: deref chains are walked to emit the DXIL handle to the resource; dynamic indexing supported
69     *     2. Bindless: the resource source is assumed as an index into a descriptor heap
70     *   UBOs/SSBOs are struct variables in the NIR, accessed via vulkan_resource_index/load_vulkan_descriptor; dynamic indexing supported
71     *     If load_vulkan_descriptor gets an index that didn't come from vulkan_resource_index, it is assumed to be an index into a descriptor heap
72     *   Read-only SSBOs, as declared in the SPIR-V, are bound as raw buffer SRVs instead of UAVs, unless they're lowered to bindless
73     *   No immediate constant buffer or scratch
74     */
75    DXIL_ENVIRONMENT_VULKAN,
76 };
77 
78 struct nir_to_dxil_options {
79    bool interpolate_at_vertex;
80    bool lower_int16;
81    bool disable_math_refactoring;
82    bool last_ubo_is_not_arrayed;
83    unsigned provoking_vertex;
84    unsigned num_kernel_globals;
85    unsigned input_clip_size;
86    enum dxil_environment environment;
87    enum dxil_shader_model shader_model_max;
88    enum dxil_validator_version validator_version_max;
89 };
90 
91 typedef void (*dxil_msg_callback)(void *priv, const char *msg);
92 
93 struct dxil_logger {
94    void *priv;
95    dxil_msg_callback log;
96 };
97 
98 bool
99 nir_to_dxil(struct nir_shader *s, const struct nir_to_dxil_options *opts,
100             const struct dxil_logger *logger, struct blob *blob);
101 
102 const nir_shader_compiler_options*
103 dxil_get_base_nir_compiler_options(void);
104 
105 void
106 dxil_get_nir_compiler_options(nir_shader_compiler_options *options,
107                               enum dxil_shader_model shader_model_max,
108                               unsigned supported_int_sizes,
109                               unsigned supported_float_sizes);
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif
116