• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "dxil_spirv_nir.h"
25 #include "spirv_to_dxil.h"
26 #include "nir_to_dxil.h"
27 #include "dxil_nir.h"
28 #include "shader_enums.h"
29 #include "spirv/nir_spirv.h"
30 #include "util/blob.h"
31 
32 #include "git_sha1.h"
33 #include "vulkan/vulkan.h"
34 
35 static_assert(DXIL_SPIRV_SHADER_NONE == (int)MESA_SHADER_NONE, "must match");
36 static_assert(DXIL_SPIRV_SHADER_VERTEX == (int)MESA_SHADER_VERTEX, "must match");
37 static_assert(DXIL_SPIRV_SHADER_TESS_CTRL == (int)MESA_SHADER_TESS_CTRL, "must match");
38 static_assert(DXIL_SPIRV_SHADER_TESS_EVAL == (int)MESA_SHADER_TESS_EVAL, "must match");
39 static_assert(DXIL_SPIRV_SHADER_GEOMETRY == (int)MESA_SHADER_GEOMETRY, "must match");
40 static_assert(DXIL_SPIRV_SHADER_FRAGMENT == (int)MESA_SHADER_FRAGMENT, "must match");
41 static_assert(DXIL_SPIRV_SHADER_COMPUTE == (int)MESA_SHADER_COMPUTE, "must match");
42 static_assert(DXIL_SPIRV_SHADER_KERNEL == (int)MESA_SHADER_KERNEL, "must match");
43 
44 /* Logic extracted from vk_spirv_to_nir() so we have the same preparation
45  * steps for both the vulkan driver and the lib used by the WebGPU
46  * implementation.
47  * Maybe we should move those steps out of vk_spirv_to_nir() and make
48  * them vk agnosting (right, the only vk specific thing is the vk_device
49  * object that's used for the debug callback passed to spirv_to_nir()).
50  */
51 static void
spirv_to_dxil_nir_prep(nir_shader * nir)52 spirv_to_dxil_nir_prep(nir_shader *nir)
53 {
54    /* We have to lower away local constant initializers right before we
55     * inline functions.  That way they get properly initialized at the top
56     * of the function and not at the top of its caller.
57     */
58    NIR_PASS_V(nir, nir_lower_variable_initializers, nir_var_function_temp);
59    NIR_PASS_V(nir, nir_lower_returns);
60    NIR_PASS_V(nir, nir_inline_functions);
61    NIR_PASS_V(nir, nir_copy_prop);
62    NIR_PASS_V(nir, nir_opt_deref);
63 
64    /* Pick off the single entrypoint that we want */
65    foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
66       if (!func->is_entrypoint)
67          exec_node_remove(&func->node);
68    }
69    assert(exec_list_length(&nir->functions) == 1);
70 
71    /* Now that we've deleted all but the main function, we can go ahead and
72     * lower the rest of the constant initializers.  We do this here so that
73     * nir_remove_dead_variables and split_per_member_structs below see the
74     * corresponding stores.
75     */
76    NIR_PASS_V(nir, nir_lower_variable_initializers, ~0);
77 
78    /* Split member structs.  We do this before lower_io_to_temporaries so that
79     * it doesn't lower system values to temporaries by accident.
80     */
81    NIR_PASS_V(nir, nir_split_var_copies);
82    NIR_PASS_V(nir, nir_split_per_member_structs);
83 
84    NIR_PASS_V(nir, nir_remove_dead_variables,
85               nir_var_shader_in | nir_var_shader_out | nir_var_system_value |
86               nir_var_shader_call_data | nir_var_ray_hit_attrib,
87               NULL);
88 
89    NIR_PASS_V(nir, nir_propagate_invariant, false);
90 }
91 
92 bool
spirv_to_dxil(const uint32_t * words,size_t word_count,struct dxil_spirv_specialization * specializations,unsigned int num_specializations,dxil_spirv_shader_stage stage,const char * entry_point_name,const struct dxil_spirv_debug_options * dgb_opts,const struct dxil_spirv_runtime_conf * conf,struct dxil_spirv_object * out_dxil)93 spirv_to_dxil(const uint32_t *words, size_t word_count,
94               struct dxil_spirv_specialization *specializations,
95               unsigned int num_specializations, dxil_spirv_shader_stage stage,
96               const char *entry_point_name,
97               const struct dxil_spirv_debug_options *dgb_opts,
98               const struct dxil_spirv_runtime_conf *conf,
99               struct dxil_spirv_object *out_dxil)
100 {
101    if (stage == DXIL_SPIRV_SHADER_NONE || stage == DXIL_SPIRV_SHADER_KERNEL)
102       return false;
103 
104    struct spirv_to_nir_options spirv_opts = {
105       .caps = {
106          .draw_parameters = true,
107       },
108       .ubo_addr_format = nir_address_format_32bit_index_offset,
109       .ssbo_addr_format = nir_address_format_32bit_index_offset,
110       .shared_addr_format = nir_address_format_32bit_offset_as_64bit,
111 
112       // use_deref_buffer_array_length + nir_lower_explicit_io force
113       //  get_ssbo_size to take in the return from load_vulkan_descriptor
114       //  instead of vulkan_resource_index. This makes it much easier to
115       //  get the DXIL handle for the SSBO.
116       .use_deref_buffer_array_length = true
117    };
118 
119    glsl_type_singleton_init_or_ref();
120 
121    struct nir_shader_compiler_options nir_options = *dxil_get_nir_compiler_options();
122    // We will manually handle base_vertex when vertex_id and instance_id have
123    // have been already converted to zero-base.
124    nir_options.lower_base_vertex = !conf->zero_based_vertex_instance_id;
125 
126    nir_shader *nir = spirv_to_nir(
127       words, word_count, (struct nir_spirv_specialization *)specializations,
128       num_specializations, (gl_shader_stage)stage, entry_point_name,
129       &spirv_opts, &nir_options);
130    if (!nir) {
131       glsl_type_singleton_decref();
132       return false;
133    }
134 
135    nir_validate_shader(nir,
136                        "Validate before feeding NIR to the DXIL compiler");
137 
138    spirv_to_dxil_nir_prep(nir);
139 
140    bool requires_runtime_data;
141    dxil_spirv_nir_passes(nir, conf, &requires_runtime_data);
142 
143    if (dgb_opts->dump_nir)
144       nir_print_shader(nir, stderr);
145 
146    struct nir_to_dxil_options opts = {
147       .environment = DXIL_ENVIRONMENT_VULKAN,
148       .shader_model_max = SHADER_MODEL_6_2,
149       .validator_version_max = DXIL_VALIDATOR_1_4,
150    };
151    struct blob dxil_blob;
152    if (!nir_to_dxil(nir, &opts, &dxil_blob)) {
153       if (dxil_blob.allocated)
154          blob_finish(&dxil_blob);
155       ralloc_free(nir);
156       glsl_type_singleton_decref();
157       return false;
158    }
159 
160    ralloc_free(nir);
161    out_dxil->metadata.requires_runtime_data = requires_runtime_data;
162    blob_finish_get_buffer(&dxil_blob, &out_dxil->binary.buffer,
163                           &out_dxil->binary.size);
164 
165    glsl_type_singleton_decref();
166    return true;
167 }
168 
169 void
spirv_to_dxil_free(struct dxil_spirv_object * dxil)170 spirv_to_dxil_free(struct dxil_spirv_object *dxil)
171 {
172    free(dxil->binary.buffer);
173 }
174 
175 uint64_t
spirv_to_dxil_get_version()176 spirv_to_dxil_get_version()
177 {
178    const char sha1[] = MESA_GIT_SHA1;
179    const char* dash = strchr(sha1, '-');
180    if (dash) {
181       return strtoull(dash + 1, NULL, 16);
182    }
183    return 0;
184 }
185