xref: /aosp_15_r20/external/mesa3d/src/compiler/spirv/nir_spirv.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2015 Intel 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_SPIRV_H_
25 #define _NIR_SPIRV_H_
26 
27 #include "util/disk_cache.h"
28 #include "compiler/nir/nir.h"
29 #include "compiler/shader_info.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 struct spirv_capabilities;
36 
37 struct nir_spirv_specialization {
38    uint32_t id;
39    nir_const_value value;
40    bool defined_on_module;
41 };
42 
43 enum nir_spirv_debug_level {
44    NIR_SPIRV_DEBUG_LEVEL_INVALID = -1,
45    NIR_SPIRV_DEBUG_LEVEL_INFO,
46    NIR_SPIRV_DEBUG_LEVEL_WARNING,
47    NIR_SPIRV_DEBUG_LEVEL_ERROR,
48 };
49 
50 enum nir_spirv_execution_environment {
51    NIR_SPIRV_VULKAN = 0,
52    NIR_SPIRV_OPENCL,
53    NIR_SPIRV_OPENGL,
54 };
55 
56 struct spirv_to_nir_options {
57    enum nir_spirv_execution_environment environment;
58 
59    /* Whether to keep ViewIndex as an input instead of rewriting to a sysval.
60     */
61    bool view_index_is_input;
62 
63    /* Create a nir library. */
64    bool create_library;
65 
66    /* Initial value for shader_info::float_controls_execution_mode,
67     * indicates hardware requirements rather than shader author intent
68     */
69    uint32_t float_controls_execution_mode;
70 
71    /* Initial subgroup size.  This may be overwritten for CL kernels */
72    enum gl_subgroup_size subgroup_size;
73 
74    /* True if RelaxedPrecision-decorated ALU result values should be performed
75     * with 16-bit math.
76     */
77    bool mediump_16bit_alu;
78 
79    /* When mediump_16bit_alu is set, determines whether nir_op_fddx/fddy can be
80     * performed in 16-bit math.
81     */
82    bool mediump_16bit_derivatives;
83 
84    /* These really early AMD extensions don't have capabilities */
85    bool amd_gcn_shader;
86    bool amd_shader_ballot;
87    bool amd_trinary_minmax;
88    bool amd_shader_explicit_vertex_parameter;
89 
90    /* Whether or not printf is supported */
91    bool printf;
92 
93    /* Whether or not the driver wants consume debug information (Debugging purposes). */
94    bool debug_info;
95 
96    const struct spirv_capabilities *capabilities;
97 
98    /* Address format for various kinds of pointers. */
99    nir_address_format ubo_addr_format;
100    nir_address_format ssbo_addr_format;
101    nir_address_format phys_ssbo_addr_format;
102    nir_address_format push_const_addr_format;
103    nir_address_format shared_addr_format;
104    nir_address_format task_payload_addr_format;
105    nir_address_format global_addr_format;
106    nir_address_format temp_addr_format;
107    nir_address_format constant_addr_format;
108 
109    /** Minimum UBO alignment.
110     *
111     * This should match VkPhysicalDeviceLimits::minUniformBufferOffsetAlignment
112     */
113    uint32_t min_ubo_alignment;
114 
115    /** Minimum SSBO alignment.
116     *
117     * This should match VkPhysicalDeviceLimits::minStorageBufferOffsetAlignment
118     */
119    uint32_t min_ssbo_alignment;
120 
121    const nir_shader *clc_shader;
122 
123    struct {
124       void (*func)(void *private_data,
125                    enum nir_spirv_debug_level level,
126                    size_t spirv_offset,
127                    const char *message);
128       void *private_data;
129    } debug;
130 
131    /* Force texture sampling to be non-uniform. */
132    bool force_tex_non_uniform;
133    /* Force SSBO accesses to be non-uniform. */
134    bool force_ssbo_non_uniform;
135 
136    /* In Debug Builds, instead of emitting an OS break on failure, just return NULL from
137     * spirv_to_nir().  This is useful for the unit tests that want to report a test failed
138     * but continue executing other tests.
139     */
140    bool skip_os_break_in_debug_build;
141 
142    /* Shader index provided by VkPipelineShaderStageNodeCreateInfoAMDX */
143    uint32_t shader_index;
144 };
145 
146 enum spirv_verify_result {
147    SPIRV_VERIFY_OK = 0,
148    SPIRV_VERIFY_PARSER_ERROR = 1,
149    SPIRV_VERIFY_ENTRY_POINT_NOT_FOUND = 2,
150    SPIRV_VERIFY_UNKNOWN_SPEC_INDEX = 3,
151 };
152 
153 enum spirv_verify_result spirv_verify_gl_specialization_constants(
154    const uint32_t *words, size_t word_count,
155    struct nir_spirv_specialization *spec, unsigned num_spec,
156    gl_shader_stage stage, const char *entry_point_name);
157 
158 nir_shader *spirv_to_nir(const uint32_t *words, size_t word_count,
159                          struct nir_spirv_specialization *specializations,
160                          unsigned num_specializations,
161                          gl_shader_stage stage, const char *entry_point_name,
162                          const struct spirv_to_nir_options *options,
163                          const nir_shader_compiler_options *nir_options);
164 
165 bool
166 spirv_library_to_nir_builder(FILE *fp, const uint32_t *words, size_t word_count,
167                              const struct spirv_to_nir_options *options);
168 
169 void spirv_print_asm(FILE *fp, const uint32_t *words, size_t word_count);
170 
171 #ifdef __cplusplus
172 }
173 #endif
174 
175 #endif /* _NIR_SPIRV_H_ */
176