sfricke-samsung | 7a9bdca | 2022-01-24 14:38:03 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2021-2022 The Khronos Group Inc. |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | * |
| 15 | * Author: Spencer Fricke <s.fricke@samsung.com> |
| 16 | * |
| 17 | * The Shader Module file is in charge of all things around creating and parsing an internal representation of a shader module |
| 18 | */ |
| 19 | #ifndef VULKAN_SHADER_MODULE_H |
| 20 | #define VULKAN_SHADER_MODULE_H |
| 21 | |
| 22 | #include <cassert> |
| 23 | #include <cstdlib> |
| 24 | #include <cstring> |
| 25 | #include <unordered_map> |
| 26 | #include <vector> |
| 27 | |
Jeremy Gebben | 5d97074 | 2021-05-31 16:04:14 -0600 | [diff] [blame] | 28 | #include "base_node.h" |
Jeremy Gebben | c942dfa | 2021-08-26 14:18:20 -0600 | [diff] [blame] | 29 | #include "sampler_state.h" |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 30 | #include <spirv/unified1/spirv.hpp> |
| 31 | #include "spirv-tools/optimizer.hpp" |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 32 | |
Jeremy Gebben | c942dfa | 2021-08-26 14:18:20 -0600 | [diff] [blame] | 33 | class PIPELINE_STATE; |
| 34 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 35 | // A forward iterator over spirv instructions. Provides easy access to len, opcode, and content words |
| 36 | // without the caller needing to care too much about the physical SPIRV module layout. |
sfricke-samsung | a6c1ddc | 2022-01-23 14:15:40 -0800 | [diff] [blame] | 37 | // |
| 38 | // For more information of the physical module layout to help understand this struct: |
| 39 | // https://github.com/KhronosGroup/SPIRV-Guide/blob/master/chapters/parsing_instructions.md |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 40 | struct spirv_inst_iter { |
| 41 | std::vector<uint32_t>::const_iterator zero; |
| 42 | std::vector<uint32_t>::const_iterator it; |
| 43 | |
| 44 | uint32_t len() const { |
| 45 | auto result = *it >> 16; |
| 46 | assert(result > 0); |
| 47 | return result; |
| 48 | } |
| 49 | |
| 50 | uint32_t opcode() const { return *it & 0x0ffffu; } |
| 51 | |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 52 | uint32_t const &word(uint32_t n) const { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 53 | assert(n < len()); |
| 54 | return it[n]; |
| 55 | } |
| 56 | |
| 57 | uint32_t offset() const { return (uint32_t)(it - zero); } |
| 58 | |
| 59 | spirv_inst_iter() {} |
| 60 | |
| 61 | spirv_inst_iter(std::vector<uint32_t>::const_iterator zero, std::vector<uint32_t>::const_iterator it) : zero(zero), it(it) {} |
| 62 | |
| 63 | bool operator==(spirv_inst_iter const &other) const { return it == other.it; } |
| 64 | |
| 65 | bool operator!=(spirv_inst_iter const &other) const { return it != other.it; } |
| 66 | |
sfricke-samsung | 61d50ec | 2022-02-13 17:01:25 -0800 | [diff] [blame] | 67 | bool operator!=(std::vector<uint32_t>::const_iterator other) const { return it != other; } |
| 68 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 69 | spirv_inst_iter operator++(int) { // x++ |
| 70 | spirv_inst_iter ii = *this; |
| 71 | it += len(); |
| 72 | return ii; |
| 73 | } |
| 74 | |
| 75 | spirv_inst_iter operator++() { // ++x; |
| 76 | it += len(); |
| 77 | return *this; |
| 78 | } |
| 79 | |
| 80 | // The iterator and the value are the same thing. |
| 81 | spirv_inst_iter &operator*() { return *this; } |
| 82 | spirv_inst_iter const &operator*() const { return *this; } |
| 83 | }; |
| 84 | |
Jeremy Gebben | c942dfa | 2021-08-26 14:18:20 -0600 | [diff] [blame] | 85 | struct interface_var { |
| 86 | uint32_t id; |
| 87 | uint32_t type_id; |
| 88 | uint32_t offset; |
| 89 | |
Jeremy Gebben | 1578c00 | 2021-12-02 09:20:03 -0700 | [diff] [blame] | 90 | // List of samplers that sample a given image. The index of array is index of image. |
| 91 | std::vector<layer_data::unordered_set<SamplerUsedByImage>> samplers_used_by_image; |
Jeremy Gebben | c942dfa | 2021-08-26 14:18:20 -0600 | [diff] [blame] | 92 | |
| 93 | bool is_patch; |
| 94 | bool is_block_member; |
| 95 | bool is_relaxed_precision; |
Lionel Landwerlin | bc7401b | 2021-12-07 15:43:05 +0200 | [diff] [blame] | 96 | bool is_readable; |
Jeremy Gebben | c942dfa | 2021-08-26 14:18:20 -0600 | [diff] [blame] | 97 | bool is_writable; |
| 98 | bool is_atomic_operation; |
| 99 | bool is_sampler_implicitLod_dref_proj; |
| 100 | bool is_sampler_bias_offset; |
Lionel Landwerlin | bc7401b | 2021-12-07 15:43:05 +0200 | [diff] [blame] | 101 | bool is_read_without_format; // For storage images |
| 102 | bool is_write_without_format; // For storage images |
Lionel Landwerlin | cdbe868 | 2021-12-08 15:10:37 +0200 | [diff] [blame] | 103 | bool is_dref_operation; |
Jeremy Gebben | c942dfa | 2021-08-26 14:18:20 -0600 | [diff] [blame] | 104 | // TODO: collect the name, too? Isn't required to be present. |
| 105 | |
| 106 | interface_var() |
| 107 | : id(0), |
| 108 | type_id(0), |
| 109 | offset(0), |
| 110 | is_patch(false), |
| 111 | is_block_member(false), |
| 112 | is_relaxed_precision(false), |
Lionel Landwerlin | bc7401b | 2021-12-07 15:43:05 +0200 | [diff] [blame] | 113 | is_readable(false), |
Jeremy Gebben | c942dfa | 2021-08-26 14:18:20 -0600 | [diff] [blame] | 114 | is_writable(false), |
| 115 | is_atomic_operation(false), |
| 116 | is_sampler_implicitLod_dref_proj(false), |
Lionel Landwerlin | bc7401b | 2021-12-07 15:43:05 +0200 | [diff] [blame] | 117 | is_sampler_bias_offset(false), |
| 118 | is_read_without_format(false), |
Lionel Landwerlin | cdbe868 | 2021-12-08 15:10:37 +0200 | [diff] [blame] | 119 | is_write_without_format(false), |
| 120 | is_dref_operation(false) {} |
Jeremy Gebben | c942dfa | 2021-08-26 14:18:20 -0600 | [diff] [blame] | 121 | }; |
| 122 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 123 | // Utils taking a spirv_inst_iter |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 124 | std::vector<uint32_t> FindEntrypointInterfaces(const spirv_inst_iter &entrypoint); |
| 125 | |
| 126 | enum FORMAT_TYPE { |
| 127 | FORMAT_TYPE_FLOAT = 1, // UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader |
| 128 | FORMAT_TYPE_SINT = 2, |
| 129 | FORMAT_TYPE_UINT = 4, |
| 130 | }; |
| 131 | |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 132 | typedef std::pair<uint32_t, uint32_t> location_t; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 133 | |
| 134 | struct decoration_set { |
| 135 | enum { |
| 136 | location_bit = 1 << 0, |
| 137 | patch_bit = 1 << 1, |
| 138 | relaxed_precision_bit = 1 << 2, |
| 139 | block_bit = 1 << 3, |
| 140 | buffer_block_bit = 1 << 4, |
| 141 | component_bit = 1 << 5, |
| 142 | input_attachment_index_bit = 1 << 6, |
| 143 | descriptor_set_bit = 1 << 7, |
| 144 | binding_bit = 1 << 8, |
| 145 | nonwritable_bit = 1 << 9, |
| 146 | builtin_bit = 1 << 10, |
Lionel Landwerlin | 38d2e12 | 2021-07-21 14:21:47 +0300 | [diff] [blame] | 147 | nonreadable_bit = 1 << 11, |
ziga-lunarg | 9e94e11 | 2021-09-27 00:21:10 +0200 | [diff] [blame] | 148 | per_vertex_bit = 1 << 12, |
| 149 | passthrough_bit = 1 << 13, |
sjfricke | de73431 | 2022-07-14 19:22:43 +0900 | [diff] [blame] | 150 | aliased_bit = 1 << 14, |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 151 | }; |
Nathaniel Cesario | cf69bda | 2021-06-22 13:23:42 -0600 | [diff] [blame] | 152 | static constexpr uint32_t kInvalidValue = std::numeric_limits<uint32_t>::max(); |
| 153 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 154 | uint32_t flags = 0; |
Nathaniel Cesario | cf69bda | 2021-06-22 13:23:42 -0600 | [diff] [blame] | 155 | uint32_t location = kInvalidValue; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 156 | uint32_t component = 0; |
| 157 | uint32_t input_attachment_index = 0; |
| 158 | uint32_t descriptor_set = 0; |
| 159 | uint32_t binding = 0; |
Nathaniel Cesario | cf69bda | 2021-06-22 13:23:42 -0600 | [diff] [blame] | 160 | uint32_t builtin = kInvalidValue; |
| 161 | uint32_t spec_const_id = kInvalidValue; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 162 | |
| 163 | void merge(decoration_set const &other); |
| 164 | |
| 165 | void add(uint32_t decoration, uint32_t value); |
| 166 | }; |
| 167 | |
sfricke-samsung | 58b8435 | 2021-07-31 21:41:04 -0700 | [diff] [blame] | 168 | struct atomic_instruction { |
| 169 | uint32_t storage_class; |
| 170 | uint32_t bit_width; |
| 171 | uint32_t type; // ex. OpTypeInt |
| 172 | |
| 173 | atomic_instruction() : storage_class(0), bit_width(0), type(0) {} |
| 174 | }; |
| 175 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 176 | struct function_set { |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 177 | uint32_t id; |
| 178 | uint32_t offset; |
| 179 | uint32_t length; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 180 | std::unordered_multimap<uint32_t, uint32_t> op_lists; // key: spv::Op, value: offset |
| 181 | |
| 182 | function_set() : id(0), offset(0), length(0) {} |
| 183 | }; |
| 184 | |
| 185 | struct builtin_set { |
| 186 | uint32_t offset; // offset to instruction (OpDecorate or OpMemberDecorate) |
| 187 | spv::BuiltIn builtin; |
| 188 | |
| 189 | builtin_set(uint32_t offset, spv::BuiltIn builtin) : offset(offset), builtin(builtin) {} |
| 190 | }; |
| 191 | |
sjfricke | 6a03e01 | 2022-06-23 17:54:11 +0900 | [diff] [blame] | 192 | // Contains all the details for a OpTypeStruct |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 193 | struct shader_struct_member { |
| 194 | uint32_t offset; |
| 195 | uint32_t size; // A scalar size or a struct size. Not consider array |
| 196 | std::vector<uint32_t> array_length_hierarchy; // multi-dimensional array, mat, vec. mat is combined with 2 array. |
| 197 | // e.g :array[2] -> {2}, array[2][3][4] -> {2,3,4}, mat4[2] ->{2,4,4}, |
| 198 | std::vector<uint32_t> array_block_size; // When index increases, how many data increases. |
| 199 | // e.g : array[2][3][4] -> {12,4,1}, it means if the first index increases one, the |
| 200 | // array gets 12 data. If the second index increases one, the array gets 4 data. |
sjfricke | 6a03e01 | 2022-06-23 17:54:11 +0900 | [diff] [blame] | 201 | |
| 202 | // OpTypeStruct can have OpTypeStruct inside it so need to track the struct-in-struct chain |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 203 | std::vector<shader_struct_member> struct_members; // If the data is not a struct, it's empty. |
| 204 | shader_struct_member *root; |
| 205 | |
| 206 | shader_struct_member() : offset(0), size(0), root(nullptr) {} |
| 207 | |
| 208 | bool IsUsed() const { |
| 209 | if (!root) return false; |
| 210 | return root->used_bytes.size() ? true : false; |
| 211 | } |
| 212 | |
| 213 | std::vector<uint8_t> *GetUsedbytes() const { |
| 214 | if (!root) return nullptr; |
| 215 | return &root->used_bytes; |
| 216 | } |
| 217 | |
| 218 | std::string GetLocationDesc(uint32_t index_used_bytes) const; |
| 219 | |
| 220 | private: |
| 221 | std::vector<uint8_t> used_bytes; // This only works for root. 0: not used. 1: used. The totally array * size. |
| 222 | }; |
| 223 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 224 | struct SHADER_MODULE_STATE : public BASE_NODE { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 225 | struct EntryPoint { |
| 226 | uint32_t offset; // into module to get OpEntryPoint instruction |
| 227 | VkShaderStageFlagBits stage; |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 228 | std::unordered_multimap<uint32_t, uint32_t> decorate_list; // key: spv::Op, value: offset |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 229 | std::vector<function_set> function_set_list; |
| 230 | shader_struct_member push_constant_used_in_shader; |
| 231 | }; |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 232 | |
| 233 | // Static/const data extracted from a SPIRV module. |
| 234 | struct SpirvStaticData { |
| 235 | SpirvStaticData() = default; |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 236 | SpirvStaticData(const SHADER_MODULE_STATE &module_state); |
sjfricke | dedec24 | 2022-08-25 15:18:38 +0900 | [diff] [blame] | 237 | SpirvStaticData &operator=(const SpirvStaticData &) = default; |
| 238 | SpirvStaticData(SpirvStaticData &&) = default; |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 239 | |
| 240 | // A mapping of <id> to the first word of its def. this is useful because walking type |
| 241 | // trees, constant expressions, etc requires jumping all over the instruction stream. |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 242 | layer_data::unordered_map<uint32_t, uint32_t> def_index; |
| 243 | layer_data::unordered_map<uint32_t, decoration_set> decorations; |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 244 | // <Specialization constant ID -> target ID> mapping |
| 245 | layer_data::unordered_map<uint32_t, uint32_t> spec_const_map; |
| 246 | // Find all decoration instructions to prevent relooping module later - many checks need this info |
| 247 | std::vector<spirv_inst_iter> decoration_inst; |
| 248 | std::vector<spirv_inst_iter> member_decoration_inst; |
sjfricke | 44d663c | 2022-06-01 06:42:58 +0900 | [diff] [blame] | 249 | // Find all variable instructions to prevent relookping module later |
| 250 | std::vector<spirv_inst_iter> variable_inst; |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 251 | // Execution are not tied to an entry point and are their own mapping tied to entry point function |
| 252 | // [OpEntryPoint function <id> operand] : [Execution Mode Instruction list] |
| 253 | layer_data::unordered_map<uint32_t, std::vector<spirv_inst_iter>> execution_mode_inst; |
| 254 | // both OpDecorate and OpMemberDecorate builtin instructions |
| 255 | std::vector<builtin_set> builtin_decoration_list; |
| 256 | std::unordered_map<uint32_t, atomic_instruction> atomic_inst; |
ziga-lunarg | be00373 | 2022-04-22 00:20:13 +0200 | [diff] [blame] | 257 | std::vector<spv::Capability> capability_list; |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 258 | |
sjfricke | dedec24 | 2022-08-25 15:18:38 +0900 | [diff] [blame] | 259 | bool has_group_decoration{false}; |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 260 | bool has_specialization_constants{false}; |
ziga-lunarg | e25f5f0 | 2022-04-16 15:07:35 +0200 | [diff] [blame] | 261 | bool has_invocation_repack_instruction{false}; |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 262 | |
| 263 | // entry point is not unqiue to single value so need multimap |
| 264 | std::unordered_multimap<std::string, EntryPoint> entry_points; |
| 265 | bool multiple_entry_points{false}; |
| 266 | }; |
| 267 | |
| 268 | // The spirv image itself |
| 269 | // NOTE: this _must_ be initialized first. |
| 270 | // NOTE: this may end up being an _optimized_ version of what was passed in at initialization time. |
| 271 | const std::vector<uint32_t> words; |
| 272 | |
| 273 | const SpirvStaticData static_data_; |
| 274 | |
| 275 | const bool has_valid_spirv{false}; |
| 276 | const uint32_t gpu_validation_shader_id{std::numeric_limits<uint32_t>::max()}; |
| 277 | |
| 278 | SHADER_MODULE_STATE(const uint32_t *code, std::size_t count, spv_target_env env = SPV_ENV_VULKAN_1_0) |
| 279 | : BASE_NODE(static_cast<VkShaderModule>(VK_NULL_HANDLE), kVulkanObjectTypeShaderModule), |
Nathaniel Cesario | ea997aa | 2022-07-19 10:43:57 -0600 | [diff] [blame] | 280 | words(code, code + (count / sizeof(uint32_t))), |
| 281 | static_data_(*this) { |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 282 | PreprocessShaderBinary(env); |
| 283 | } |
| 284 | |
| 285 | template <typename SpirvContainer> |
| 286 | SHADER_MODULE_STATE(const SpirvContainer &spirv) |
| 287 | : SHADER_MODULE_STATE(spirv.data(), spirv.size() * sizeof(typename SpirvContainer::value_type)) {} |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 288 | |
Nathaniel Cesario | 844ec21 | 2022-04-05 10:55:23 -0600 | [diff] [blame] | 289 | SHADER_MODULE_STATE(const VkShaderModuleCreateInfo &create_info, spv_target_env env, uint32_t unique_shader_id) |
Nathaniel Cesario | ee041d8 | 2022-02-15 16:32:03 -0700 | [diff] [blame] | 290 | : BASE_NODE(static_cast<VkShaderModule>(VK_NULL_HANDLE), kVulkanObjectTypeShaderModule), |
| 291 | words(create_info.pCode, create_info.pCode + create_info.codeSize / sizeof(uint32_t)), |
| 292 | static_data_(*this), |
| 293 | has_valid_spirv(true), |
| 294 | gpu_validation_shader_id(unique_shader_id) { |
| 295 | PreprocessShaderBinary(env); |
| 296 | } |
| 297 | |
Nathaniel Cesario | 844ec21 | 2022-04-05 10:55:23 -0600 | [diff] [blame] | 298 | SHADER_MODULE_STATE(const VkShaderModuleCreateInfo &create_info, VkShaderModule shaderModule, spv_target_env env, |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 299 | uint32_t unique_shader_id) |
Jeremy Gebben | 9efe1cf | 2021-05-15 20:05:09 -0600 | [diff] [blame] | 300 | : BASE_NODE(shaderModule, kVulkanObjectTypeShaderModule), |
Nathaniel Cesario | ee041d8 | 2022-02-15 16:32:03 -0700 | [diff] [blame] | 301 | words(create_info.pCode, create_info.pCode + create_info.codeSize / sizeof(uint32_t)), |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 302 | static_data_(*this), |
Jeremy Gebben | 9efe1cf | 2021-05-15 20:05:09 -0600 | [diff] [blame] | 303 | has_valid_spirv(true), |
Jeremy Gebben | 9efe1cf | 2021-05-15 20:05:09 -0600 | [diff] [blame] | 304 | gpu_validation_shader_id(unique_shader_id) { |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 305 | PreprocessShaderBinary(env); |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 308 | SHADER_MODULE_STATE() : BASE_NODE(static_cast<VkShaderModule>(VK_NULL_HANDLE), kVulkanObjectTypeShaderModule) {} |
| 309 | |
| 310 | const std::vector<spirv_inst_iter> &GetDecorationInstructions() const { return static_data_.decoration_inst; } |
| 311 | |
| 312 | const std::unordered_map<uint32_t, atomic_instruction> &GetAtomicInstructions() const { return static_data_.atomic_inst; } |
| 313 | |
| 314 | const layer_data::unordered_map<uint32_t, std::vector<spirv_inst_iter>> &GetExecutionModeInstructions() const { |
| 315 | return static_data_.execution_mode_inst; |
| 316 | } |
| 317 | |
| 318 | const std::vector<builtin_set> &GetBuiltinDecorationList() const { return static_data_.builtin_decoration_list; } |
| 319 | |
| 320 | const layer_data::unordered_map<uint32_t, uint32_t> &GetSpecConstMap() const { return static_data_.spec_const_map; } |
| 321 | |
| 322 | bool HasSpecConstants() const { return static_data_.has_specialization_constants; } |
| 323 | |
| 324 | const std::unordered_multimap<std::string, EntryPoint> &GetEntryPoints() const { return static_data_.entry_points; } |
| 325 | |
| 326 | bool HasMultipleEntryPoints() const { return static_data_.multiple_entry_points; } |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 327 | |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 328 | VkShaderModule vk_shader_module() const { return handle_.Cast<VkShaderModule>(); } |
| 329 | |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 330 | decoration_set get_decorations(uint32_t id) const { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 331 | // return the actual decorations for this id, or a default set. |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 332 | auto it = static_data_.decorations.find(id); |
| 333 | if (it != static_data_.decorations.end()) return it->second; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 334 | return decoration_set(); |
| 335 | } |
| 336 | |
| 337 | // Expose begin() / end() to enable range-based for |
| 338 | spirv_inst_iter begin() const { return spirv_inst_iter(words.begin(), words.begin() + 5); } // First insn |
| 339 | spirv_inst_iter end() const { return spirv_inst_iter(words.begin(), words.end()); } // Just past last insn |
| 340 | // Given an offset into the module, produce an iterator there. |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 341 | spirv_inst_iter at(uint32_t offset) const { return spirv_inst_iter(words.begin(), words.begin() + offset); } |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 342 | |
| 343 | // Gets an iterator to the definition of an id |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 344 | spirv_inst_iter get_def(uint32_t id) const { |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 345 | auto it = static_data_.def_index.find(id); |
| 346 | if (it == static_data_.def_index.end()) { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 347 | return end(); |
| 348 | } |
| 349 | return at(it->second); |
| 350 | } |
| 351 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 352 | // Used to get human readable strings for error messages |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 353 | void DescribeTypeInner(std::ostringstream &ss, uint32_t type) const; |
| 354 | std::string DescribeType(uint32_t type) const; |
sfricke-samsung | 7a9bdca | 2022-01-24 14:38:03 -0800 | [diff] [blame] | 355 | std::string DescribeInstruction(const spirv_inst_iter &insn) const; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 356 | |
| 357 | layer_data::unordered_set<uint32_t> MarkAccessibleIds(spirv_inst_iter entrypoint) const; |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 358 | layer_data::optional<VkPrimitiveTopology> GetTopology(const spirv_inst_iter &entrypoint) const; |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 359 | // TODO (https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450) |
| 360 | // Since we currently don't support multiple entry points, this is a helper to return the topology |
| 361 | // for the "first" (and for our purposes _only_) entrypoint. |
| 362 | layer_data::optional<VkPrimitiveTopology> GetTopology() const; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 363 | |
| 364 | const EntryPoint *FindEntrypointStruct(char const *name, VkShaderStageFlagBits stageBits) const; |
| 365 | spirv_inst_iter FindEntrypoint(char const *name, VkShaderStageFlagBits stageBits) const; |
| 366 | bool FindLocalSize(const spirv_inst_iter &entrypoint, uint32_t &local_size_x, uint32_t &local_size_y, |
| 367 | uint32_t &local_size_z) const; |
| 368 | |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 369 | spirv_inst_iter GetConstantDef(uint32_t id) const; |
sjfricke | 3b0cb10 | 2022-08-10 16:27:45 +0900 | [diff] [blame] | 370 | uint32_t GetConstantValue(const spirv_inst_iter &itr) const; |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 371 | uint32_t GetConstantValueById(uint32_t id) const; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 372 | int32_t GetShaderResourceDimensionality(const interface_var &resource) const; |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 373 | uint32_t GetLocationsConsumedByType(uint32_t type, bool strip_array_level) const; |
| 374 | uint32_t GetComponentsConsumedByType(uint32_t type, bool strip_array_level) const; |
| 375 | uint32_t GetFundamentalType(uint32_t type) const; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 376 | spirv_inst_iter GetStructType(spirv_inst_iter def, bool is_array_of_verts) const; |
| 377 | |
sfricke-samsung | ad55ccc | 2022-01-19 20:06:17 -0800 | [diff] [blame] | 378 | void DefineStructMember(const spirv_inst_iter &it, const std::vector<uint32_t> &member_decorate_offsets, |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 379 | shader_struct_member &data) const; |
| 380 | void RunUsedArray(uint32_t offset, std::vector<uint32_t> array_indices, uint32_t access_chain_word_index, |
| 381 | spirv_inst_iter &access_chain_it, const shader_struct_member &data) const; |
| 382 | void RunUsedStruct(uint32_t offset, uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it, |
| 383 | const shader_struct_member &data) const; |
| 384 | void SetUsedStructMember(const uint32_t variable_id, const std::vector<function_set> &function_set_list, |
| 385 | const shader_struct_member &data) const; |
| 386 | |
| 387 | // Push consants |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 388 | static void SetPushConstantUsedInShader(const SHADER_MODULE_STATE &module_state, |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 389 | std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> &entry_points); |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 390 | |
| 391 | uint32_t DescriptorTypeToReqs(uint32_t type_id) const; |
| 392 | |
| 393 | bool IsBuiltInWritten(spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) const; |
| 394 | |
| 395 | // State tracking helpers for collecting interface information |
| 396 | void IsSpecificDescriptorType(const spirv_inst_iter &id_it, bool is_storage_buffer, bool is_check_writable, |
sjfricke | eaa434d | 2022-06-22 04:55:28 +0900 | [diff] [blame] | 397 | interface_var &out_interface_var) const; |
Jeremy Gebben | 7fc88a2 | 2021-08-25 13:30:45 -0600 | [diff] [blame] | 398 | std::vector<std::pair<DescriptorSlot, interface_var>> CollectInterfaceByDescriptorSlot( |
ziga-lunarg | c2de478 | 2022-04-14 19:49:07 +0200 | [diff] [blame] | 399 | layer_data::unordered_set<uint32_t> const &accessible_ids) const; |
Jeremy Gebben | 84b838b | 2021-08-23 08:41:39 -0600 | [diff] [blame] | 400 | layer_data::unordered_set<uint32_t> CollectWritableOutputLocationinFS(const spirv_inst_iter &entrypoint) const; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 401 | bool CollectInterfaceBlockMembers(std::map<location_t, interface_var> *out, bool is_array_of_verts, uint32_t id, |
ziga-lunarg | 9e94e11 | 2021-09-27 00:21:10 +0200 | [diff] [blame] | 402 | uint32_t type_id, bool is_patch, uint32_t first_location) const; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 403 | std::map<location_t, interface_var> CollectInterfaceByLocation(spirv_inst_iter entrypoint, spv::StorageClass sinterface, |
| 404 | bool is_array_of_verts) const; |
| 405 | std::vector<uint32_t> CollectBuiltinBlockMembers(spirv_inst_iter entrypoint, uint32_t storageClass) const; |
| 406 | std::vector<std::pair<uint32_t, interface_var>> CollectInterfaceByInputAttachmentIndex( |
| 407 | layer_data::unordered_set<uint32_t> const &accessible_ids) const; |
Lionel Landwerlin | 892d6c3 | 2021-05-05 12:56:19 +0300 | [diff] [blame] | 408 | |
ziga-lunarg | 8346fe8 | 2021-08-22 17:30:50 +0200 | [diff] [blame] | 409 | uint32_t GetNumComponentsInBaseType(const spirv_inst_iter &iter) const; |
ziga-lunarg | a26b360 | 2021-08-08 15:53:00 +0200 | [diff] [blame] | 410 | uint32_t GetTypeBitsSize(const spirv_inst_iter &iter) const; |
| 411 | uint32_t GetTypeBytesSize(const spirv_inst_iter &iter) const; |
ziga-lunarg | 8346fe8 | 2021-08-22 17:30:50 +0200 | [diff] [blame] | 412 | uint32_t GetBaseType(const spirv_inst_iter &iter) const; |
sfricke-samsung | a6c1ddc | 2022-01-23 14:15:40 -0800 | [diff] [blame] | 413 | uint32_t GetTypeId(uint32_t id) const; |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 414 | |
ziga-lunarg | 143bdbf | 2022-05-04 19:04:58 +0200 | [diff] [blame] | 415 | bool WritesToGlLayer() const { |
| 416 | return std::any_of(static_data_.builtin_decoration_list.begin(), static_data_.builtin_decoration_list.end(), |
| 417 | [](const builtin_set &built_in) { return built_in.builtin == spv::BuiltInLayer; }); |
| 418 | } |
| 419 | |
| 420 | bool HasInputAttachmentCapability() const { |
| 421 | return std::any_of(static_data_.capability_list.begin(), static_data_.capability_list.end(), |
| 422 | [](const spv::Capability &capability) { return capability == spv::CapabilityInputAttachment; }); |
| 423 | } |
| 424 | |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 425 | private: |
| 426 | // Functions used for initialization only |
| 427 | // Used to populate the shader module object |
| 428 | void PreprocessShaderBinary(spv_target_env env); |
| 429 | |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 430 | static std::unordered_multimap<std::string, EntryPoint> ProcessEntryPoints(const SHADER_MODULE_STATE &module_state); |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 431 | }; |
| 432 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 433 | #endif // VULKAN_SHADER_MODULE_H |