1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2022 Collabora, LTD 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "vk_pipeline.h" 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci#include "vk_log.h" 27bf215546Sopenharmony_ci#include "vk_nir.h" 28bf215546Sopenharmony_ci#include "vk_shader_module.h" 29bf215546Sopenharmony_ci#include "vk_util.h" 30bf215546Sopenharmony_ci 31bf215546Sopenharmony_ci#include "nir_serialize.h" 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci#include "util/mesa-sha1.h" 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_cibool 36bf215546Sopenharmony_civk_pipeline_shader_stage_is_null(const VkPipelineShaderStageCreateInfo *info) 37bf215546Sopenharmony_ci{ 38bf215546Sopenharmony_ci if (info->module != VK_NULL_HANDLE) 39bf215546Sopenharmony_ci return false; 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_ci vk_foreach_struct_const(ext, info->pNext) { 42bf215546Sopenharmony_ci if (ext->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO || 43bf215546Sopenharmony_ci ext->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_MODULE_IDENTIFIER_CREATE_INFO_EXT) 44bf215546Sopenharmony_ci return false; 45bf215546Sopenharmony_ci } 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ci return true; 48bf215546Sopenharmony_ci} 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_cistatic uint32_t 51bf215546Sopenharmony_ciget_required_subgroup_size(const VkPipelineShaderStageCreateInfo *info) 52bf215546Sopenharmony_ci{ 53bf215546Sopenharmony_ci const VkPipelineShaderStageRequiredSubgroupSizeCreateInfo *rss_info = 54bf215546Sopenharmony_ci vk_find_struct_const(info->pNext, 55bf215546Sopenharmony_ci PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO); 56bf215546Sopenharmony_ci return rss_info != NULL ? rss_info->requiredSubgroupSize : 0; 57bf215546Sopenharmony_ci} 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ciVkResult 60bf215546Sopenharmony_civk_pipeline_shader_stage_to_nir(struct vk_device *device, 61bf215546Sopenharmony_ci const VkPipelineShaderStageCreateInfo *info, 62bf215546Sopenharmony_ci const struct spirv_to_nir_options *spirv_options, 63bf215546Sopenharmony_ci const struct nir_shader_compiler_options *nir_options, 64bf215546Sopenharmony_ci void *mem_ctx, nir_shader **nir_out) 65bf215546Sopenharmony_ci{ 66bf215546Sopenharmony_ci VK_FROM_HANDLE(vk_shader_module, module, info->module); 67bf215546Sopenharmony_ci const gl_shader_stage stage = vk_to_mesa_shader_stage(info->stage); 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_ci assert(info->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO); 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci if (module != NULL && module->nir != NULL) { 72bf215546Sopenharmony_ci assert(module->nir->info.stage == stage); 73bf215546Sopenharmony_ci assert(exec_list_length(&module->nir->functions) == 1); 74bf215546Sopenharmony_ci ASSERTED const char *nir_name = 75bf215546Sopenharmony_ci nir_shader_get_entrypoint(module->nir)->function->name; 76bf215546Sopenharmony_ci assert(strcmp(nir_name, info->pName) == 0); 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_ci nir_validate_shader(module->nir, "internal shader"); 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci nir_shader *clone = nir_shader_clone(mem_ctx, module->nir); 81bf215546Sopenharmony_ci if (clone == NULL) 82bf215546Sopenharmony_ci return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci assert(clone->options == NULL || clone->options == nir_options); 85bf215546Sopenharmony_ci clone->options = nir_options; 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ci *nir_out = clone; 88bf215546Sopenharmony_ci return VK_SUCCESS; 89bf215546Sopenharmony_ci } 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci const uint32_t *spirv_data; 92bf215546Sopenharmony_ci uint32_t spirv_size; 93bf215546Sopenharmony_ci if (module != NULL) { 94bf215546Sopenharmony_ci spirv_data = (uint32_t *)module->data; 95bf215546Sopenharmony_ci spirv_size = module->size; 96bf215546Sopenharmony_ci } else { 97bf215546Sopenharmony_ci const VkShaderModuleCreateInfo *minfo = 98bf215546Sopenharmony_ci vk_find_struct_const(info->pNext, SHADER_MODULE_CREATE_INFO); 99bf215546Sopenharmony_ci if (unlikely(minfo == NULL)) { 100bf215546Sopenharmony_ci return vk_errorf(device, VK_ERROR_UNKNOWN, 101bf215546Sopenharmony_ci "No shader module provided"); 102bf215546Sopenharmony_ci } 103bf215546Sopenharmony_ci spirv_data = minfo->pCode; 104bf215546Sopenharmony_ci spirv_size = minfo->codeSize; 105bf215546Sopenharmony_ci } 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci enum gl_subgroup_size subgroup_size; 108bf215546Sopenharmony_ci uint32_t req_subgroup_size = get_required_subgroup_size(info); 109bf215546Sopenharmony_ci if (req_subgroup_size > 0) { 110bf215546Sopenharmony_ci assert(util_is_power_of_two_nonzero(req_subgroup_size)); 111bf215546Sopenharmony_ci assert(req_subgroup_size >= 8 && req_subgroup_size <= 128); 112bf215546Sopenharmony_ci subgroup_size = req_subgroup_size; 113bf215546Sopenharmony_ci } else if (info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT || 114bf215546Sopenharmony_ci vk_spirv_version(spirv_data, spirv_size) >= 0x10600) { 115bf215546Sopenharmony_ci /* Starting with SPIR-V 1.6, varying subgroup size the default */ 116bf215546Sopenharmony_ci subgroup_size = SUBGROUP_SIZE_VARYING; 117bf215546Sopenharmony_ci } else if (info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT) { 118bf215546Sopenharmony_ci assert(stage == MESA_SHADER_COMPUTE); 119bf215546Sopenharmony_ci subgroup_size = SUBGROUP_SIZE_FULL_SUBGROUPS; 120bf215546Sopenharmony_ci } else { 121bf215546Sopenharmony_ci subgroup_size = SUBGROUP_SIZE_API_CONSTANT; 122bf215546Sopenharmony_ci } 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci nir_shader *nir = vk_spirv_to_nir(device, spirv_data, spirv_size, stage, 125bf215546Sopenharmony_ci info->pName, subgroup_size, 126bf215546Sopenharmony_ci info->pSpecializationInfo, 127bf215546Sopenharmony_ci spirv_options, nir_options, mem_ctx); 128bf215546Sopenharmony_ci if (nir == NULL) 129bf215546Sopenharmony_ci return vk_errorf(device, VK_ERROR_UNKNOWN, "spirv_to_nir failed"); 130bf215546Sopenharmony_ci 131bf215546Sopenharmony_ci *nir_out = nir; 132bf215546Sopenharmony_ci 133bf215546Sopenharmony_ci return VK_SUCCESS; 134bf215546Sopenharmony_ci} 135bf215546Sopenharmony_ci 136bf215546Sopenharmony_civoid 137bf215546Sopenharmony_civk_pipeline_hash_shader_stage(const VkPipelineShaderStageCreateInfo *info, 138bf215546Sopenharmony_ci unsigned char *stage_sha1) 139bf215546Sopenharmony_ci{ 140bf215546Sopenharmony_ci VK_FROM_HANDLE(vk_shader_module, module, info->module); 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ci if (module && module->nir) { 143bf215546Sopenharmony_ci /* Internal NIR module: serialize and hash the NIR shader. 144bf215546Sopenharmony_ci * We don't need to hash other info fields since they should match the 145bf215546Sopenharmony_ci * NIR data. 146bf215546Sopenharmony_ci */ 147bf215546Sopenharmony_ci assert(module->nir->info.stage == vk_to_mesa_shader_stage(info->stage)); 148bf215546Sopenharmony_ci ASSERTED nir_function_impl *entrypoint = nir_shader_get_entrypoint(module->nir); 149bf215546Sopenharmony_ci assert(strcmp(entrypoint->function->name, info->pName) == 0); 150bf215546Sopenharmony_ci assert(info->pSpecializationInfo == NULL); 151bf215546Sopenharmony_ci 152bf215546Sopenharmony_ci struct blob blob; 153bf215546Sopenharmony_ci 154bf215546Sopenharmony_ci blob_init(&blob); 155bf215546Sopenharmony_ci nir_serialize(&blob, module->nir, false); 156bf215546Sopenharmony_ci assert(!blob.out_of_memory); 157bf215546Sopenharmony_ci _mesa_sha1_compute(blob.data, blob.size, stage_sha1); 158bf215546Sopenharmony_ci blob_finish(&blob); 159bf215546Sopenharmony_ci return; 160bf215546Sopenharmony_ci } 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_ci const VkShaderModuleCreateInfo *minfo = 163bf215546Sopenharmony_ci vk_find_struct_const(info->pNext, SHADER_MODULE_CREATE_INFO); 164bf215546Sopenharmony_ci const VkPipelineShaderStageModuleIdentifierCreateInfoEXT *iinfo = 165bf215546Sopenharmony_ci vk_find_struct_const(info->pNext, PIPELINE_SHADER_STAGE_MODULE_IDENTIFIER_CREATE_INFO_EXT); 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ci struct mesa_sha1 ctx; 168bf215546Sopenharmony_ci 169bf215546Sopenharmony_ci _mesa_sha1_init(&ctx); 170bf215546Sopenharmony_ci 171bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, &info->flags, sizeof(info->flags)); 172bf215546Sopenharmony_ci 173bf215546Sopenharmony_ci assert(util_bitcount(info->stage) == 1); 174bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, &info->stage, sizeof(info->stage)); 175bf215546Sopenharmony_ci 176bf215546Sopenharmony_ci if (module) { 177bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, module->sha1, sizeof(module->sha1)); 178bf215546Sopenharmony_ci } else if (minfo) { 179bf215546Sopenharmony_ci unsigned char spirv_sha1[SHA1_DIGEST_LENGTH]; 180bf215546Sopenharmony_ci 181bf215546Sopenharmony_ci _mesa_sha1_compute(minfo->pCode, minfo->codeSize, spirv_sha1); 182bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, spirv_sha1, sizeof(spirv_sha1)); 183bf215546Sopenharmony_ci } else { 184bf215546Sopenharmony_ci /* It is legal to pass in arbitrary identifiers as long as they don't exceed 185bf215546Sopenharmony_ci * the limit. Shaders with bogus identifiers are more or less guaranteed to fail. */ 186bf215546Sopenharmony_ci assert(iinfo); 187bf215546Sopenharmony_ci assert(iinfo->identifierSize <= VK_MAX_SHADER_MODULE_IDENTIFIER_SIZE_EXT); 188bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, iinfo->pIdentifier, iinfo->identifierSize); 189bf215546Sopenharmony_ci } 190bf215546Sopenharmony_ci 191bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, info->pName, strlen(info->pName)); 192bf215546Sopenharmony_ci 193bf215546Sopenharmony_ci if (info->pSpecializationInfo) { 194bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, info->pSpecializationInfo->pMapEntries, 195bf215546Sopenharmony_ci info->pSpecializationInfo->mapEntryCount * 196bf215546Sopenharmony_ci sizeof(*info->pSpecializationInfo->pMapEntries)); 197bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, info->pSpecializationInfo->pData, 198bf215546Sopenharmony_ci info->pSpecializationInfo->dataSize); 199bf215546Sopenharmony_ci } 200bf215546Sopenharmony_ci 201bf215546Sopenharmony_ci uint32_t req_subgroup_size = get_required_subgroup_size(info); 202bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, &req_subgroup_size, sizeof(req_subgroup_size)); 203bf215546Sopenharmony_ci 204bf215546Sopenharmony_ci _mesa_sha1_final(&ctx, stage_sha1); 205bf215546Sopenharmony_ci} 206