blob: 1e3e9281a528c287550d7f3b874329042d1645be [file] [log] [blame]
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001/* Copyright (c) 2021-2022 The Khronos Group Inc.
sfricke-samsung962cad92021-04-13 00:46:29 -07002 *
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 Gebben5d970742021-05-31 16:04:14 -060028#include "base_node.h"
Jeremy Gebbenc942dfa2021-08-26 14:18:20 -060029#include "sampler_state.h"
sfricke-samsung962cad92021-04-13 00:46:29 -070030#include <spirv/unified1/spirv.hpp>
31#include "spirv-tools/optimizer.hpp"
sfricke-samsung962cad92021-04-13 00:46:29 -070032
Jeremy Gebbenc942dfa2021-08-26 14:18:20 -060033class PIPELINE_STATE;
34
sfricke-samsung962cad92021-04-13 00:46:29 -070035// 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-samsunga6c1ddc2022-01-23 14:15:40 -080037//
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-samsung962cad92021-04-13 00:46:29 -070040struct 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-samsung7fac88a2022-01-26 11:44:22 -080052 uint32_t const &word(uint32_t n) const {
sfricke-samsung962cad92021-04-13 00:46:29 -070053 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-samsung61d50ec2022-02-13 17:01:25 -080067 bool operator!=(std::vector<uint32_t>::const_iterator other) const { return it != other; }
68
sfricke-samsung962cad92021-04-13 00:46:29 -070069 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 Gebbenc942dfa2021-08-26 14:18:20 -060085struct interface_var {
86 uint32_t id;
87 uint32_t type_id;
88 uint32_t offset;
89
Jeremy Gebben1578c002021-12-02 09:20:03 -070090 // 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 Gebbenc942dfa2021-08-26 14:18:20 -060092
93 bool is_patch;
94 bool is_block_member;
95 bool is_relaxed_precision;
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +020096 bool is_readable;
Jeremy Gebbenc942dfa2021-08-26 14:18:20 -060097 bool is_writable;
98 bool is_atomic_operation;
99 bool is_sampler_implicitLod_dref_proj;
100 bool is_sampler_bias_offset;
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +0200101 bool is_read_without_format; // For storage images
102 bool is_write_without_format; // For storage images
Lionel Landwerlincdbe8682021-12-08 15:10:37 +0200103 bool is_dref_operation;
Jeremy Gebbenc942dfa2021-08-26 14:18:20 -0600104 // 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 Landwerlinbc7401b2021-12-07 15:43:05 +0200113 is_readable(false),
Jeremy Gebbenc942dfa2021-08-26 14:18:20 -0600114 is_writable(false),
115 is_atomic_operation(false),
116 is_sampler_implicitLod_dref_proj(false),
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +0200117 is_sampler_bias_offset(false),
118 is_read_without_format(false),
Lionel Landwerlincdbe8682021-12-08 15:10:37 +0200119 is_write_without_format(false),
120 is_dref_operation(false) {}
Jeremy Gebbenc942dfa2021-08-26 14:18:20 -0600121};
122
sfricke-samsung962cad92021-04-13 00:46:29 -0700123// Utils taking a spirv_inst_iter
sfricke-samsung962cad92021-04-13 00:46:29 -0700124std::vector<uint32_t> FindEntrypointInterfaces(const spirv_inst_iter &entrypoint);
125
126enum 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-samsung7fac88a2022-01-26 11:44:22 -0800132typedef std::pair<uint32_t, uint32_t> location_t;
sfricke-samsung962cad92021-04-13 00:46:29 -0700133
134struct 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 Landwerlin38d2e122021-07-21 14:21:47 +0300147 nonreadable_bit = 1 << 11,
ziga-lunarg9e94e112021-09-27 00:21:10 +0200148 per_vertex_bit = 1 << 12,
149 passthrough_bit = 1 << 13,
sjfrickede734312022-07-14 19:22:43 +0900150 aliased_bit = 1 << 14,
sfricke-samsung962cad92021-04-13 00:46:29 -0700151 };
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -0600152 static constexpr uint32_t kInvalidValue = std::numeric_limits<uint32_t>::max();
153
sfricke-samsung962cad92021-04-13 00:46:29 -0700154 uint32_t flags = 0;
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -0600155 uint32_t location = kInvalidValue;
sfricke-samsung962cad92021-04-13 00:46:29 -0700156 uint32_t component = 0;
157 uint32_t input_attachment_index = 0;
158 uint32_t descriptor_set = 0;
159 uint32_t binding = 0;
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -0600160 uint32_t builtin = kInvalidValue;
161 uint32_t spec_const_id = kInvalidValue;
sfricke-samsung962cad92021-04-13 00:46:29 -0700162
163 void merge(decoration_set const &other);
164
165 void add(uint32_t decoration, uint32_t value);
166};
167
sfricke-samsung58b84352021-07-31 21:41:04 -0700168struct 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-samsung962cad92021-04-13 00:46:29 -0700176struct function_set {
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800177 uint32_t id;
178 uint32_t offset;
179 uint32_t length;
sfricke-samsung962cad92021-04-13 00:46:29 -0700180 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
185struct 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
sjfricke6a03e012022-06-23 17:54:11 +0900192// Contains all the details for a OpTypeStruct
sfricke-samsung962cad92021-04-13 00:46:29 -0700193struct 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.
sjfricke6a03e012022-06-23 17:54:11 +0900201
202 // OpTypeStruct can have OpTypeStruct inside it so need to track the struct-in-struct chain
sfricke-samsung962cad92021-04-13 00:46:29 -0700203 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-samsung962cad92021-04-13 00:46:29 -0700224struct SHADER_MODULE_STATE : public BASE_NODE {
sfricke-samsung962cad92021-04-13 00:46:29 -0700225 struct EntryPoint {
226 uint32_t offset; // into module to get OpEntryPoint instruction
227 VkShaderStageFlagBits stage;
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800228 std::unordered_multimap<uint32_t, uint32_t> decorate_list; // key: spv::Op, value: offset
sfricke-samsung962cad92021-04-13 00:46:29 -0700229 std::vector<function_set> function_set_list;
230 shader_struct_member push_constant_used_in_shader;
231 };
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600232
233 // Static/const data extracted from a SPIRV module.
234 struct SpirvStaticData {
235 SpirvStaticData() = default;
sfricke-samsungef15e482022-01-26 11:32:49 -0800236 SpirvStaticData(const SHADER_MODULE_STATE &module_state);
sjfrickededec242022-08-25 15:18:38 +0900237 SpirvStaticData &operator=(const SpirvStaticData &) = default;
238 SpirvStaticData(SpirvStaticData &&) = default;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600239
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-samsung7fac88a2022-01-26 11:44:22 -0800242 layer_data::unordered_map<uint32_t, uint32_t> def_index;
243 layer_data::unordered_map<uint32_t, decoration_set> decorations;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600244 // <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;
sjfricke44d663c2022-06-01 06:42:58 +0900249 // Find all variable instructions to prevent relookping module later
250 std::vector<spirv_inst_iter> variable_inst;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600251 // 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-lunargbe003732022-04-22 00:20:13 +0200257 std::vector<spv::Capability> capability_list;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600258
sjfrickededec242022-08-25 15:18:38 +0900259 bool has_group_decoration{false};
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600260 bool has_specialization_constants{false};
ziga-lunarge25f5f02022-04-16 15:07:35 +0200261 bool has_invocation_repack_instruction{false};
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600262
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 Cesarioea997aa2022-07-19 10:43:57 -0600280 words(code, code + (count / sizeof(uint32_t))),
281 static_data_(*this) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600282 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-samsung962cad92021-04-13 00:46:29 -0700288
Nathaniel Cesario844ec212022-04-05 10:55:23 -0600289 SHADER_MODULE_STATE(const VkShaderModuleCreateInfo &create_info, spv_target_env env, uint32_t unique_shader_id)
Nathaniel Cesarioee041d82022-02-15 16:32:03 -0700290 : 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 Cesario844ec212022-04-05 10:55:23 -0600298 SHADER_MODULE_STATE(const VkShaderModuleCreateInfo &create_info, VkShaderModule shaderModule, spv_target_env env,
sfricke-samsung962cad92021-04-13 00:46:29 -0700299 uint32_t unique_shader_id)
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -0600300 : BASE_NODE(shaderModule, kVulkanObjectTypeShaderModule),
Nathaniel Cesarioee041d82022-02-15 16:32:03 -0700301 words(create_info.pCode, create_info.pCode + create_info.codeSize / sizeof(uint32_t)),
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600302 static_data_(*this),
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -0600303 has_valid_spirv(true),
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -0600304 gpu_validation_shader_id(unique_shader_id) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600305 PreprocessShaderBinary(env);
sfricke-samsung962cad92021-04-13 00:46:29 -0700306 }
307
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600308 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-samsung962cad92021-04-13 00:46:29 -0700327
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600328 VkShaderModule vk_shader_module() const { return handle_.Cast<VkShaderModule>(); }
329
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800330 decoration_set get_decorations(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700331 // return the actual decorations for this id, or a default set.
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600332 auto it = static_data_.decorations.find(id);
333 if (it != static_data_.decorations.end()) return it->second;
sfricke-samsung962cad92021-04-13 00:46:29 -0700334 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-samsung7fac88a2022-01-26 11:44:22 -0800341 spirv_inst_iter at(uint32_t offset) const { return spirv_inst_iter(words.begin(), words.begin() + offset); }
sfricke-samsung962cad92021-04-13 00:46:29 -0700342
343 // Gets an iterator to the definition of an id
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800344 spirv_inst_iter get_def(uint32_t id) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600345 auto it = static_data_.def_index.find(id);
346 if (it == static_data_.def_index.end()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700347 return end();
348 }
349 return at(it->second);
350 }
351
sfricke-samsung962cad92021-04-13 00:46:29 -0700352 // Used to get human readable strings for error messages
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800353 void DescribeTypeInner(std::ostringstream &ss, uint32_t type) const;
354 std::string DescribeType(uint32_t type) const;
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800355 std::string DescribeInstruction(const spirv_inst_iter &insn) const;
sfricke-samsung962cad92021-04-13 00:46:29 -0700356
357 layer_data::unordered_set<uint32_t> MarkAccessibleIds(spirv_inst_iter entrypoint) const;
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600358 layer_data::optional<VkPrimitiveTopology> GetTopology(const spirv_inst_iter &entrypoint) const;
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700359 // 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-samsung962cad92021-04-13 00:46:29 -0700363
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-samsung7fac88a2022-01-26 11:44:22 -0800369 spirv_inst_iter GetConstantDef(uint32_t id) const;
sjfricke3b0cb102022-08-10 16:27:45 +0900370 uint32_t GetConstantValue(const spirv_inst_iter &itr) const;
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800371 uint32_t GetConstantValueById(uint32_t id) const;
sfricke-samsung962cad92021-04-13 00:46:29 -0700372 int32_t GetShaderResourceDimensionality(const interface_var &resource) const;
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800373 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-samsung962cad92021-04-13 00:46:29 -0700376 spirv_inst_iter GetStructType(spirv_inst_iter def, bool is_array_of_verts) const;
377
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800378 void DefineStructMember(const spirv_inst_iter &it, const std::vector<uint32_t> &member_decorate_offsets,
sfricke-samsung962cad92021-04-13 00:46:29 -0700379 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-samsungef15e482022-01-26 11:32:49 -0800388 static void SetPushConstantUsedInShader(const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600389 std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> &entry_points);
sfricke-samsung962cad92021-04-13 00:46:29 -0700390
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,
sjfrickeeaa434d2022-06-22 04:55:28 +0900397 interface_var &out_interface_var) const;
Jeremy Gebben7fc88a22021-08-25 13:30:45 -0600398 std::vector<std::pair<DescriptorSlot, interface_var>> CollectInterfaceByDescriptorSlot(
ziga-lunargc2de4782022-04-14 19:49:07 +0200399 layer_data::unordered_set<uint32_t> const &accessible_ids) const;
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600400 layer_data::unordered_set<uint32_t> CollectWritableOutputLocationinFS(const spirv_inst_iter &entrypoint) const;
sfricke-samsung962cad92021-04-13 00:46:29 -0700401 bool CollectInterfaceBlockMembers(std::map<location_t, interface_var> *out, bool is_array_of_verts, uint32_t id,
ziga-lunarg9e94e112021-09-27 00:21:10 +0200402 uint32_t type_id, bool is_patch, uint32_t first_location) const;
sfricke-samsung962cad92021-04-13 00:46:29 -0700403 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 Landwerlin892d6c32021-05-05 12:56:19 +0300408
ziga-lunarg8346fe82021-08-22 17:30:50 +0200409 uint32_t GetNumComponentsInBaseType(const spirv_inst_iter &iter) const;
ziga-lunarga26b3602021-08-08 15:53:00 +0200410 uint32_t GetTypeBitsSize(const spirv_inst_iter &iter) const;
411 uint32_t GetTypeBytesSize(const spirv_inst_iter &iter) const;
ziga-lunarg8346fe82021-08-22 17:30:50 +0200412 uint32_t GetBaseType(const spirv_inst_iter &iter) const;
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -0800413 uint32_t GetTypeId(uint32_t id) const;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600414
ziga-lunarg143bdbf2022-05-04 19:04:58 +0200415 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 Cesario77cd59b2021-10-11 23:52:24 -0600425 private:
426 // Functions used for initialization only
427 // Used to populate the shader module object
428 void PreprocessShaderBinary(spv_target_env env);
429
sfricke-samsungef15e482022-01-26 11:32:49 -0800430 static std::unordered_multimap<std::string, EntryPoint> ProcessEntryPoints(const SHADER_MODULE_STATE &module_state);
sfricke-samsung962cad92021-04-13 00:46:29 -0700431};
432
sfricke-samsung962cad92021-04-13 00:46:29 -0700433#endif // VULKAN_SHADER_MODULE_H