1 /* Copyright (c) 2021 The Khronos Group Inc.
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 
28 #include "base_node.h"
29 #include "sampler_state.h"
30 #include <spirv/unified1/spirv.hpp>
31 #include "spirv-tools/optimizer.hpp"
32 
33 class PIPELINE_STATE;
34 
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.
37 struct spirv_inst_iter {
38     std::vector<uint32_t>::const_iterator zero;
39     std::vector<uint32_t>::const_iterator it;
40 
lenspirv_inst_iter41     uint32_t len() const {
42         auto result = *it >> 16;
43         assert(result > 0);
44         return result;
45     }
46 
opcodespirv_inst_iter47     uint32_t opcode() const { return *it & 0x0ffffu; }
48 
wordspirv_inst_iter49     uint32_t const &word(unsigned n) const {
50         assert(n < len());
51         return it[n];
52     }
53 
offsetspirv_inst_iter54     uint32_t offset() const { return (uint32_t)(it - zero); }
55 
spirv_inst_iterspirv_inst_iter56     spirv_inst_iter() {}
57 
spirv_inst_iterspirv_inst_iter58     spirv_inst_iter(std::vector<uint32_t>::const_iterator zero, std::vector<uint32_t>::const_iterator it) : zero(zero), it(it) {}
59 
60     bool operator==(spirv_inst_iter const &other) const { return it == other.it; }
61 
62     bool operator!=(spirv_inst_iter const &other) const { return it != other.it; }
63 
64     spirv_inst_iter operator++(int) {  // x++
65         spirv_inst_iter ii = *this;
66         it += len();
67         return ii;
68     }
69 
70     spirv_inst_iter operator++() {  // ++x;
71         it += len();
72         return *this;
73     }
74 
75     // The iterator and the value are the same thing.
76     spirv_inst_iter &operator*() { return *this; }
77     spirv_inst_iter const &operator*() const { return *this; }
78 };
79 
80 struct interface_var {
81     uint32_t id;
82     uint32_t type_id;
83     uint32_t offset;
84 
85     // List of samplers that sample a given image. The index of array is index of image.
86     std::vector<layer_data::unordered_set<SamplerUsedByImage>> samplers_used_by_image;
87 
88     bool is_patch;
89     bool is_block_member;
90     bool is_relaxed_precision;
91     bool is_writable;
92     bool is_atomic_operation;
93     bool is_sampler_implicitLod_dref_proj;
94     bool is_sampler_bias_offset;
95     // TODO: collect the name, too? Isn't required to be present.
96 
interface_varinterface_var97     interface_var()
98         : id(0),
99           type_id(0),
100           offset(0),
101           is_patch(false),
102           is_block_member(false),
103           is_relaxed_precision(false),
104           is_writable(false),
105           is_atomic_operation(false),
106           is_sampler_implicitLod_dref_proj(false),
107           is_sampler_bias_offset(false) {}
108 };
109 
110 // Utils taking a spirv_inst_iter
111 uint32_t GetConstantValue(const spirv_inst_iter &itr);
112 std::vector<uint32_t> FindEntrypointInterfaces(const spirv_inst_iter &entrypoint);
113 
114 enum FORMAT_TYPE {
115     FORMAT_TYPE_FLOAT = 1,  // UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader
116     FORMAT_TYPE_SINT = 2,
117     FORMAT_TYPE_UINT = 4,
118 };
119 
120 typedef std::pair<unsigned, unsigned> location_t;
121 
122 struct decoration_set {
123     enum {
124         location_bit = 1 << 0,
125         patch_bit = 1 << 1,
126         relaxed_precision_bit = 1 << 2,
127         block_bit = 1 << 3,
128         buffer_block_bit = 1 << 4,
129         component_bit = 1 << 5,
130         input_attachment_index_bit = 1 << 6,
131         descriptor_set_bit = 1 << 7,
132         binding_bit = 1 << 8,
133         nonwritable_bit = 1 << 9,
134         builtin_bit = 1 << 10,
135         nonreadable_bit = 1 << 11,
136         per_vertex_bit = 1 << 12,
137         passthrough_bit = 1 << 13,
138     };
139     static constexpr uint32_t kInvalidValue = std::numeric_limits<uint32_t>::max();
140 
141     uint32_t flags = 0;
142     uint32_t location = kInvalidValue;
143     uint32_t component = 0;
144     uint32_t input_attachment_index = 0;
145     uint32_t descriptor_set = 0;
146     uint32_t binding = 0;
147     uint32_t builtin = kInvalidValue;
148     uint32_t spec_const_id = kInvalidValue;
149 
150     void merge(decoration_set const &other);
151 
152     void add(uint32_t decoration, uint32_t value);
153 };
154 
155 struct atomic_instruction {
156     uint32_t storage_class;
157     uint32_t bit_width;
158     uint32_t type;  // ex. OpTypeInt
159 
atomic_instructionatomic_instruction160     atomic_instruction() : storage_class(0), bit_width(0), type(0) {}
161 };
162 
163 struct function_set {
164     unsigned id;
165     unsigned offset;
166     unsigned length;
167     std::unordered_multimap<uint32_t, uint32_t> op_lists;  // key: spv::Op,  value: offset
168 
function_setfunction_set169     function_set() : id(0), offset(0), length(0) {}
170 };
171 
172 struct builtin_set {
173     uint32_t offset;  // offset to instruction (OpDecorate or OpMemberDecorate)
174     spv::BuiltIn builtin;
175 
builtin_setbuiltin_set176     builtin_set(uint32_t offset, spv::BuiltIn builtin) : offset(offset), builtin(builtin) {}
177 };
178 
179 struct shader_struct_member {
180     uint32_t offset;
181     uint32_t size;                                 // A scalar size or a struct size. Not consider array
182     std::vector<uint32_t> array_length_hierarchy;  // multi-dimensional array, mat, vec. mat is combined with 2 array.
183                                                    // e.g :array[2] -> {2}, array[2][3][4] -> {2,3,4}, mat4[2] ->{2,4,4},
184     std::vector<uint32_t> array_block_size;        // When index increases, how many data increases.
185                                              // e.g : array[2][3][4] -> {12,4,1}, it means if the first index increases one, the
186                                              // array gets 12 data. If the second index increases one, the array gets 4 data.
187     std::vector<shader_struct_member> struct_members;  // If the data is not a struct, it's empty.
188     shader_struct_member *root;
189 
shader_struct_membershader_struct_member190     shader_struct_member() : offset(0), size(0), root(nullptr) {}
191 
IsUsedshader_struct_member192     bool IsUsed() const {
193         if (!root) return false;
194         return root->used_bytes.size() ? true : false;
195     }
196 
GetUsedbytesshader_struct_member197     std::vector<uint8_t> *GetUsedbytes() const {
198         if (!root) return nullptr;
199         return &root->used_bytes;
200     }
201 
202     std::string GetLocationDesc(uint32_t index_used_bytes) const;
203 
204   private:
205     std::vector<uint8_t> used_bytes;  // This only works for root. 0: not used. 1: used. The totally array * size.
206 };
207 
208 struct shader_module_used_operators;
209 
210 struct SHADER_MODULE_STATE : public BASE_NODE {
211     struct EntryPoint {
212         uint32_t offset;  // into module to get OpEntryPoint instruction
213         VkShaderStageFlagBits stage;
214         std::unordered_multimap<unsigned, unsigned> decorate_list;  // key: spv::Op,  value: offset
215         std::vector<function_set> function_set_list;
216         shader_struct_member push_constant_used_in_shader;
217     };
218 
219     // Static/const data extracted from a SPIRV module.
220     struct SpirvStaticData {
221         SpirvStaticData() = default;
222         SpirvStaticData(const SHADER_MODULE_STATE &mod);
223 
224         // A mapping of <id> to the first word of its def. this is useful because walking type
225         // trees, constant expressions, etc requires jumping all over the instruction stream.
226         layer_data::unordered_map<unsigned, unsigned> def_index;
227         layer_data::unordered_map<unsigned, decoration_set> decorations;
228         // <Specialization constant ID -> target ID> mapping
229         layer_data::unordered_map<uint32_t, uint32_t> spec_const_map;
230         // Find all decoration instructions to prevent relooping module later - many checks need this info
231         std::vector<spirv_inst_iter> decoration_inst;
232         std::vector<spirv_inst_iter> member_decoration_inst;
233         // Execution are not tied to an entry point and are their own mapping tied to entry point function
234         // [OpEntryPoint function <id> operand] : [Execution Mode Instruction list]
235         layer_data::unordered_map<uint32_t, std::vector<spirv_inst_iter>> execution_mode_inst;
236         // both OpDecorate and OpMemberDecorate builtin instructions
237         std::vector<builtin_set> builtin_decoration_list;
238         std::unordered_map<uint32_t, atomic_instruction> atomic_inst;
239 
240         bool has_group_decoration = false;
241         bool has_specialization_constants{false};
242 
243         // entry point is not unqiue to single value so need multimap
244         std::unordered_multimap<std::string, EntryPoint> entry_points;
245         bool multiple_entry_points{false};
246     };
247 
248     // The spirv image itself
249     // NOTE: this _must_ be initialized first.
250     // NOTE: this may end up being an _optimized_ version of what was passed in at initialization time.
251     const std::vector<uint32_t> words;
252 
253     const SpirvStaticData static_data_;
254 
255     const bool has_valid_spirv{false};
256     const uint32_t gpu_validation_shader_id{std::numeric_limits<uint32_t>::max()};
257 
258     SHADER_MODULE_STATE(const uint32_t *code, std::size_t count, spv_target_env env = SPV_ENV_VULKAN_1_0)
BASE_NODESHADER_MODULE_STATE259         : BASE_NODE(static_cast<VkShaderModule>(VK_NULL_HANDLE), kVulkanObjectTypeShaderModule),
260           words(code, code + (count / sizeof(uint32_t))) {
261         PreprocessShaderBinary(env);
262     }
263 
264     template <typename SpirvContainer>
SHADER_MODULE_STATESHADER_MODULE_STATE265     SHADER_MODULE_STATE(const SpirvContainer &spirv)
266         : SHADER_MODULE_STATE(spirv.data(), spirv.size() * sizeof(typename SpirvContainer::value_type)) {}
267 
SHADER_MODULE_STATESHADER_MODULE_STATE268     SHADER_MODULE_STATE(VkShaderModuleCreateInfo const *pCreateInfo, VkShaderModule shaderModule, spv_target_env env,
269                         uint32_t unique_shader_id)
270         : BASE_NODE(shaderModule, kVulkanObjectTypeShaderModule),
271           words(pCreateInfo->pCode, pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
272           static_data_(*this),
273           has_valid_spirv(true),
274           gpu_validation_shader_id(unique_shader_id) {
275         PreprocessShaderBinary(env);
276     }
277 
SHADER_MODULE_STATESHADER_MODULE_STATE278     SHADER_MODULE_STATE() : BASE_NODE(static_cast<VkShaderModule>(VK_NULL_HANDLE), kVulkanObjectTypeShaderModule) {}
279 
GetDecorationInstructionsSHADER_MODULE_STATE280     const std::vector<spirv_inst_iter> &GetDecorationInstructions() const { return static_data_.decoration_inst; }
281 
GetAtomicInstructionsSHADER_MODULE_STATE282     const std::unordered_map<uint32_t, atomic_instruction> &GetAtomicInstructions() const { return static_data_.atomic_inst; }
283 
GetExecutionModeInstructionsSHADER_MODULE_STATE284     const layer_data::unordered_map<uint32_t, std::vector<spirv_inst_iter>> &GetExecutionModeInstructions() const {
285         return static_data_.execution_mode_inst;
286     }
287 
GetBuiltinDecorationListSHADER_MODULE_STATE288     const std::vector<builtin_set> &GetBuiltinDecorationList() const { return static_data_.builtin_decoration_list; }
289 
GetSpecConstMapSHADER_MODULE_STATE290     const layer_data::unordered_map<uint32_t, uint32_t> &GetSpecConstMap() const { return static_data_.spec_const_map; }
291 
HasSpecConstantsSHADER_MODULE_STATE292     bool HasSpecConstants() const { return static_data_.has_specialization_constants; }
293 
GetEntryPointsSHADER_MODULE_STATE294     const std::unordered_multimap<std::string, EntryPoint> &GetEntryPoints() const { return static_data_.entry_points; }
295 
HasMultipleEntryPointsSHADER_MODULE_STATE296     bool HasMultipleEntryPoints() const { return static_data_.multiple_entry_points; }
297 
vk_shader_moduleSHADER_MODULE_STATE298     VkShaderModule vk_shader_module() const { return handle_.Cast<VkShaderModule>(); }
299 
get_decorationsSHADER_MODULE_STATE300     decoration_set get_decorations(unsigned id) const {
301         // return the actual decorations for this id, or a default set.
302         auto it = static_data_.decorations.find(id);
303         if (it != static_data_.decorations.end()) return it->second;
304         return decoration_set();
305     }
306 
307     // Expose begin() / end() to enable range-based for
beginSHADER_MODULE_STATE308     spirv_inst_iter begin() const { return spirv_inst_iter(words.begin(), words.begin() + 5); }  // First insn
endSHADER_MODULE_STATE309     spirv_inst_iter end() const { return spirv_inst_iter(words.begin(), words.end()); }          // Just past last insn
310     // Given an offset into the module, produce an iterator there.
atSHADER_MODULE_STATE311     spirv_inst_iter at(unsigned offset) const { return spirv_inst_iter(words.begin(), words.begin() + offset); }
312 
313     // Gets an iterator to the definition of an id
get_defSHADER_MODULE_STATE314     spirv_inst_iter get_def(unsigned id) const {
315         auto it = static_data_.def_index.find(id);
316         if (it == static_data_.def_index.end()) {
317             return end();
318         }
319         return at(it->second);
320     }
321 
322     // Used to get human readable strings for error messages
323     void DescribeTypeInner(std::ostringstream &ss, unsigned type) const;
324     std::string DescribeType(unsigned type) const;
325 
326     layer_data::unordered_set<uint32_t> MarkAccessibleIds(spirv_inst_iter entrypoint) const;
327     layer_data::optional<VkPrimitiveTopology> GetTopology(const spirv_inst_iter &entrypoint) const;
328 
329     const EntryPoint *FindEntrypointStruct(char const *name, VkShaderStageFlagBits stageBits) const;
330     spirv_inst_iter FindEntrypoint(char const *name, VkShaderStageFlagBits stageBits) const;
331     bool FindLocalSize(const spirv_inst_iter &entrypoint, uint32_t &local_size_x, uint32_t &local_size_y,
332                        uint32_t &local_size_z) const;
333 
334     spirv_inst_iter GetConstantDef(unsigned id) const;
335     uint32_t GetConstantValueById(unsigned id) const;
336     int32_t GetShaderResourceDimensionality(const interface_var &resource) const;
337     unsigned GetLocationsConsumedByType(unsigned type, bool strip_array_level) const;
338     unsigned GetComponentsConsumedByType(unsigned type, bool strip_array_level) const;
339     unsigned GetFundamentalType(unsigned type) const;
340     spirv_inst_iter GetStructType(spirv_inst_iter def, bool is_array_of_verts) const;
341 
342     void DefineStructMember(const spirv_inst_iter &it, const std::vector<uint32_t> &memberDecorate_offsets,
343                             shader_struct_member &data) const;
344     void RunUsedArray(uint32_t offset, std::vector<uint32_t> array_indices, uint32_t access_chain_word_index,
345                       spirv_inst_iter &access_chain_it, const shader_struct_member &data) const;
346     void RunUsedStruct(uint32_t offset, uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it,
347                        const shader_struct_member &data) const;
348     void SetUsedStructMember(const uint32_t variable_id, const std::vector<function_set> &function_set_list,
349                              const shader_struct_member &data) const;
350 
351     // Push consants
352     static void SetPushConstantUsedInShader(const SHADER_MODULE_STATE &mod,
353                                             std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> &entry_points);
354 
355     uint32_t DescriptorTypeToReqs(uint32_t type_id) const;
356 
357     bool IsBuiltInWritten(spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) const;
358 
359     // State tracking helpers for collecting interface information
360     void IsSpecificDescriptorType(const spirv_inst_iter &id_it, bool is_storage_buffer, bool is_check_writable,
361                                   interface_var &out_interface_var, shader_module_used_operators &used_operators) const;
362     std::vector<std::pair<DescriptorSlot, interface_var>> CollectInterfaceByDescriptorSlot(
363         layer_data::unordered_set<uint32_t> const &accessible_ids) const;
364     layer_data::unordered_set<uint32_t> CollectWritableOutputLocationinFS(const spirv_inst_iter &entrypoint) const;
365     bool CollectInterfaceBlockMembers(std::map<location_t, interface_var> *out, bool is_array_of_verts, uint32_t id,
366                                       uint32_t type_id, bool is_patch, uint32_t first_location) const;
367     std::map<location_t, interface_var> CollectInterfaceByLocation(spirv_inst_iter entrypoint, spv::StorageClass sinterface,
368                                                                    bool is_array_of_verts) const;
369     std::vector<uint32_t> CollectBuiltinBlockMembers(spirv_inst_iter entrypoint, uint32_t storageClass) const;
370     std::vector<std::pair<uint32_t, interface_var>> CollectInterfaceByInputAttachmentIndex(
371         layer_data::unordered_set<uint32_t> const &accessible_ids) const;
372 
373     // Get the image type from a variable id or load operation that reference an image
374     spirv_inst_iter GetImageFormatInst(uint32_t id) const;
375 
376     uint32_t GetNumComponentsInBaseType(const spirv_inst_iter &iter) const;
377     std::array<uint32_t, 3> GetWorkgroupSize(VkPipelineShaderStageCreateInfo const *pStage,
378                                              const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map) const;
379     uint32_t GetTypeBitsSize(const spirv_inst_iter &iter) const;
380     uint32_t GetTypeBytesSize(const spirv_inst_iter &iter) const;
381     uint32_t GetBaseType(const spirv_inst_iter &iter) const;
382     uint32_t CalcComputeSharedMemory(VkShaderStageFlagBits stage,
383                                      const spirv_inst_iter &insn) const;
384 
385   private:
386     // Functions used for initialization only
387     // Used to populate the shader module object
388     void PreprocessShaderBinary(spv_target_env env);
389 
390     static std::unordered_multimap<std::string, EntryPoint> ProcessEntryPoints(const SHADER_MODULE_STATE &mod);
391 };
392 
393 // String helpers functions to give better error messages
394 char const *StorageClassName(unsigned sc);
395 
396 #endif  // VULKAN_SHADER_MODULE_H
397