1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2015 Intel Corporation 3bf215546Sopenharmony_ci * Copyright © 2022 Collabora, LTD 4bf215546Sopenharmony_ci * 5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 11bf215546Sopenharmony_ci * 12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 14bf215546Sopenharmony_ci * Software. 15bf215546Sopenharmony_ci * 16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22bf215546Sopenharmony_ci * IN THE SOFTWARE. 23bf215546Sopenharmony_ci */ 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ci#include "vk_nir.h" 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include "compiler/nir/nir_xfb_info.h" 28bf215546Sopenharmony_ci#include "compiler/spirv/nir_spirv.h" 29bf215546Sopenharmony_ci#include "vk_log.h" 30bf215546Sopenharmony_ci#include "vk_util.h" 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci#define SPIR_V_MAGIC_NUMBER 0x07230203 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ciuint32_t 35bf215546Sopenharmony_civk_spirv_version(const uint32_t *spirv_data, size_t spirv_size_B) 36bf215546Sopenharmony_ci{ 37bf215546Sopenharmony_ci assert(spirv_size_B >= 8); 38bf215546Sopenharmony_ci assert(spirv_data[0] == SPIR_V_MAGIC_NUMBER); 39bf215546Sopenharmony_ci return spirv_data[1]; 40bf215546Sopenharmony_ci} 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_cistatic void 43bf215546Sopenharmony_cispirv_nir_debug(void *private_data, 44bf215546Sopenharmony_ci enum nir_spirv_debug_level level, 45bf215546Sopenharmony_ci size_t spirv_offset, 46bf215546Sopenharmony_ci const char *message) 47bf215546Sopenharmony_ci{ 48bf215546Sopenharmony_ci const struct vk_object_base *log_obj = private_data; 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci switch (level) { 51bf215546Sopenharmony_ci case NIR_SPIRV_DEBUG_LEVEL_INFO: 52bf215546Sopenharmony_ci //vk_logi(VK_LOG_OBJS(log_obj), "SPIR-V offset %lu: %s", 53bf215546Sopenharmony_ci // (unsigned long) spirv_offset, message); 54bf215546Sopenharmony_ci break; 55bf215546Sopenharmony_ci case NIR_SPIRV_DEBUG_LEVEL_WARNING: 56bf215546Sopenharmony_ci vk_logw(VK_LOG_OBJS(log_obj), "SPIR-V offset %lu: %s", 57bf215546Sopenharmony_ci (unsigned long) spirv_offset, message); 58bf215546Sopenharmony_ci break; 59bf215546Sopenharmony_ci case NIR_SPIRV_DEBUG_LEVEL_ERROR: 60bf215546Sopenharmony_ci vk_loge(VK_LOG_OBJS(log_obj), "SPIR-V offset %lu: %s", 61bf215546Sopenharmony_ci (unsigned long) spirv_offset, message); 62bf215546Sopenharmony_ci break; 63bf215546Sopenharmony_ci default: 64bf215546Sopenharmony_ci break; 65bf215546Sopenharmony_ci } 66bf215546Sopenharmony_ci} 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_cistatic bool 69bf215546Sopenharmony_ciis_not_xfb_output(nir_variable *var, void *data) 70bf215546Sopenharmony_ci{ 71bf215546Sopenharmony_ci if (var->data.mode != nir_var_shader_out) 72bf215546Sopenharmony_ci return true; 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci return !var->data.explicit_xfb_buffer && 75bf215546Sopenharmony_ci !var->data.explicit_xfb_stride; 76bf215546Sopenharmony_ci} 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_cinir_shader * 79bf215546Sopenharmony_civk_spirv_to_nir(struct vk_device *device, 80bf215546Sopenharmony_ci const uint32_t *spirv_data, size_t spirv_size_B, 81bf215546Sopenharmony_ci gl_shader_stage stage, const char *entrypoint_name, 82bf215546Sopenharmony_ci enum gl_subgroup_size subgroup_size, 83bf215546Sopenharmony_ci const VkSpecializationInfo *spec_info, 84bf215546Sopenharmony_ci const struct spirv_to_nir_options *spirv_options, 85bf215546Sopenharmony_ci const struct nir_shader_compiler_options *nir_options, 86bf215546Sopenharmony_ci void *mem_ctx) 87bf215546Sopenharmony_ci{ 88bf215546Sopenharmony_ci assert(spirv_size_B >= 4 && spirv_size_B % 4 == 0); 89bf215546Sopenharmony_ci assert(spirv_data[0] == SPIR_V_MAGIC_NUMBER); 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci struct spirv_to_nir_options spirv_options_local = *spirv_options; 92bf215546Sopenharmony_ci spirv_options_local.debug.func = spirv_nir_debug; 93bf215546Sopenharmony_ci spirv_options_local.debug.private_data = (void *)device; 94bf215546Sopenharmony_ci spirv_options_local.subgroup_size = subgroup_size; 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_ci uint32_t num_spec_entries = 0; 97bf215546Sopenharmony_ci struct nir_spirv_specialization *spec_entries = 98bf215546Sopenharmony_ci vk_spec_info_to_nir_spirv(spec_info, &num_spec_entries); 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci nir_shader *nir = spirv_to_nir(spirv_data, spirv_size_B / 4, 101bf215546Sopenharmony_ci spec_entries, num_spec_entries, 102bf215546Sopenharmony_ci stage, entrypoint_name, 103bf215546Sopenharmony_ci &spirv_options_local, nir_options); 104bf215546Sopenharmony_ci free(spec_entries); 105bf215546Sopenharmony_ci 106bf215546Sopenharmony_ci if (nir == NULL) 107bf215546Sopenharmony_ci return NULL; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci assert(nir->info.stage == stage); 110bf215546Sopenharmony_ci nir_validate_shader(nir, "after spirv_to_nir"); 111bf215546Sopenharmony_ci nir_validate_ssa_dominance(nir, "after spirv_to_nir"); 112bf215546Sopenharmony_ci if (mem_ctx != NULL) 113bf215546Sopenharmony_ci ralloc_steal(mem_ctx, nir); 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci /* We have to lower away local constant initializers right before we 116bf215546Sopenharmony_ci * inline functions. That way they get properly initialized at the top 117bf215546Sopenharmony_ci * of the function and not at the top of its caller. 118bf215546Sopenharmony_ci */ 119bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_lower_variable_initializers, nir_var_function_temp); 120bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_lower_returns); 121bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_inline_functions); 122bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_copy_prop); 123bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_opt_deref); 124bf215546Sopenharmony_ci 125bf215546Sopenharmony_ci /* Pick off the single entrypoint that we want */ 126bf215546Sopenharmony_ci nir_remove_non_entrypoints(nir); 127bf215546Sopenharmony_ci 128bf215546Sopenharmony_ci /* Now that we've deleted all but the main function, we can go ahead and 129bf215546Sopenharmony_ci * lower the rest of the constant initializers. We do this here so that 130bf215546Sopenharmony_ci * nir_remove_dead_variables and split_per_member_structs below see the 131bf215546Sopenharmony_ci * corresponding stores. 132bf215546Sopenharmony_ci */ 133bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_lower_variable_initializers, ~0); 134bf215546Sopenharmony_ci 135bf215546Sopenharmony_ci /* Split member structs. We do this before lower_io_to_temporaries so that 136bf215546Sopenharmony_ci * it doesn't lower system values to temporaries by accident. 137bf215546Sopenharmony_ci */ 138bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_split_var_copies); 139bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_split_per_member_structs); 140bf215546Sopenharmony_ci 141bf215546Sopenharmony_ci nir_remove_dead_variables_options dead_vars_opts = { 142bf215546Sopenharmony_ci .can_remove_var = is_not_xfb_output, 143bf215546Sopenharmony_ci }; 144bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_remove_dead_variables, 145bf215546Sopenharmony_ci nir_var_shader_in | nir_var_shader_out | nir_var_system_value | 146bf215546Sopenharmony_ci nir_var_shader_call_data | nir_var_ray_hit_attrib, 147bf215546Sopenharmony_ci &dead_vars_opts); 148bf215546Sopenharmony_ci 149bf215546Sopenharmony_ci /* This needs to happen after remove_dead_vars because GLSLang likes to 150bf215546Sopenharmony_ci * insert dead clip/cull vars and we don't want to clip/cull based on 151bf215546Sopenharmony_ci * uninitialized garbage. 152bf215546Sopenharmony_ci */ 153bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_lower_clip_cull_distance_arrays); 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ci if (nir->info.stage == MESA_SHADER_VERTEX || 156bf215546Sopenharmony_ci nir->info.stage == MESA_SHADER_TESS_EVAL || 157bf215546Sopenharmony_ci nir->info.stage == MESA_SHADER_GEOMETRY) 158bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_shader_gather_xfb_info); 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_propagate_invariant, false); 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_ci return nir; 163bf215546Sopenharmony_ci} 164