1 | /* |
2 | * Copyright 2015-2021 Arm Limited |
3 | * SPDX-License-Identifier: Apache-2.0 OR MIT |
4 | * |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | * you may not use this file except in compliance with the License. |
7 | * You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, software |
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | * See the License for the specific language governing permissions and |
15 | * limitations under the License. |
16 | */ |
17 | |
18 | /* |
19 | * At your option, you may choose to accept this material under either: |
20 | * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or |
21 | * 2. The MIT License, found at <http://opensource.org/licenses/MIT>. |
22 | */ |
23 | |
24 | #ifndef SPIRV_CROSS_HPP |
25 | #define SPIRV_CROSS_HPP |
26 | |
27 | #ifndef SPV_ENABLE_UTILITY_CODE |
28 | #define SPV_ENABLE_UTILITY_CODE |
29 | #endif |
30 | #include "spirv.hpp" |
31 | #include "spirv_cfg.hpp" |
32 | #include "spirv_cross_parsed_ir.hpp" |
33 | |
34 | namespace SPIRV_CROSS_NAMESPACE |
35 | { |
36 | struct Resource |
37 | { |
38 | // Resources are identified with their SPIR-V ID. |
39 | // This is the ID of the OpVariable. |
40 | ID id; |
41 | |
42 | // The type ID of the variable which includes arrays and all type modifications. |
43 | // This type ID is not suitable for parsing OpMemberDecoration of a struct and other decorations in general |
44 | // since these modifications typically happen on the base_type_id. |
45 | TypeID type_id; |
46 | |
47 | // The base type of the declared resource. |
48 | // This type is the base type which ignores pointers and arrays of the type_id. |
49 | // This is mostly useful to parse decorations of the underlying type. |
50 | // base_type_id can also be obtained with get_type(get_type(type_id).self). |
51 | TypeID base_type_id; |
52 | |
53 | // The declared name (OpName) of the resource. |
54 | // For Buffer blocks, the name actually reflects the externally |
55 | // visible Block name. |
56 | // |
57 | // This name can be retrieved again by using either |
58 | // get_name(id) or get_name(base_type_id) depending if it's a buffer block or not. |
59 | // |
60 | // This name can be an empty string in which case get_fallback_name(id) can be |
61 | // used which obtains a suitable fallback identifier for an ID. |
62 | std::string name; |
63 | }; |
64 | |
65 | struct BuiltInResource |
66 | { |
67 | // This is mostly here to support reflection of builtins such as Position/PointSize/CullDistance/ClipDistance. |
68 | // This needs to be different from Resource since we can collect builtins from blocks. |
69 | // A builtin present here does not necessarily mean it's considered an active builtin, |
70 | // since variable ID "activeness" is only tracked on OpVariable level, not Block members. |
71 | // For that, update_active_builtins() -> has_active_builtin() can be used to further refine the reflection. |
72 | spv::BuiltIn builtin; |
73 | |
74 | // This is the actual value type of the builtin. |
75 | // Typically float4, float, array<float, N> for the gl_PerVertex builtins. |
76 | // If the builtin is a control point, the control point array type will be stripped away here as appropriate. |
77 | TypeID value_type_id; |
78 | |
79 | // This refers to the base resource which contains the builtin. |
80 | // If resource is a Block, it can hold multiple builtins, or it might not be a block. |
81 | // For advanced reflection scenarios, all information in builtin/value_type_id can be deduced, |
82 | // it's just more convenient this way. |
83 | Resource resource; |
84 | }; |
85 | |
86 | struct ShaderResources |
87 | { |
88 | SmallVector<Resource> uniform_buffers; |
89 | SmallVector<Resource> storage_buffers; |
90 | SmallVector<Resource> stage_inputs; |
91 | SmallVector<Resource> stage_outputs; |
92 | SmallVector<Resource> subpass_inputs; |
93 | SmallVector<Resource> storage_images; |
94 | SmallVector<Resource> sampled_images; |
95 | SmallVector<Resource> atomic_counters; |
96 | SmallVector<Resource> acceleration_structures; |
97 | |
98 | // There can only be one push constant block, |
99 | // but keep the vector in case this restriction is lifted in the future. |
100 | SmallVector<Resource> push_constant_buffers; |
101 | |
102 | // For Vulkan GLSL and HLSL source, |
103 | // these correspond to separate texture2D and samplers respectively. |
104 | SmallVector<Resource> separate_images; |
105 | SmallVector<Resource> separate_samplers; |
106 | |
107 | SmallVector<BuiltInResource> builtin_inputs; |
108 | SmallVector<BuiltInResource> builtin_outputs; |
109 | }; |
110 | |
111 | struct CombinedImageSampler |
112 | { |
113 | // The ID of the sampler2D variable. |
114 | VariableID combined_id; |
115 | // The ID of the texture2D variable. |
116 | VariableID image_id; |
117 | // The ID of the sampler variable. |
118 | VariableID sampler_id; |
119 | }; |
120 | |
121 | struct SpecializationConstant |
122 | { |
123 | // The ID of the specialization constant. |
124 | ConstantID id; |
125 | // The constant ID of the constant, used in Vulkan during pipeline creation. |
126 | uint32_t constant_id; |
127 | }; |
128 | |
129 | struct BufferRange |
130 | { |
131 | unsigned index; |
132 | size_t offset; |
133 | size_t range; |
134 | }; |
135 | |
136 | enum BufferPackingStandard |
137 | { |
138 | BufferPackingStd140, |
139 | BufferPackingStd430, |
140 | BufferPackingStd140EnhancedLayout, |
141 | BufferPackingStd430EnhancedLayout, |
142 | BufferPackingHLSLCbuffer, |
143 | BufferPackingHLSLCbufferPackOffset, |
144 | BufferPackingScalar, |
145 | BufferPackingScalarEnhancedLayout |
146 | }; |
147 | |
148 | struct EntryPoint |
149 | { |
150 | std::string name; |
151 | spv::ExecutionModel execution_model; |
152 | }; |
153 | |
154 | class Compiler |
155 | { |
156 | public: |
157 | friend class CFG; |
158 | friend class DominatorBuilder; |
159 | |
160 | // The constructor takes a buffer of SPIR-V words and parses it. |
161 | // It will create its own parser, parse the SPIR-V and move the parsed IR |
162 | // as if you had called the constructors taking ParsedIR directly. |
163 | explicit Compiler(std::vector<uint32_t> ir); |
164 | Compiler(const uint32_t *ir, size_t word_count); |
165 | |
166 | // This is more modular. We can also consume a ParsedIR structure directly, either as a move, or copy. |
167 | // With copy, we can reuse the same parsed IR for multiple Compiler instances. |
168 | explicit Compiler(const ParsedIR &ir); |
169 | explicit Compiler(ParsedIR &&ir); |
170 | |
171 | virtual ~Compiler() = default; |
172 | |
173 | // After parsing, API users can modify the SPIR-V via reflection and call this |
174 | // to disassemble the SPIR-V into the desired langauage. |
175 | // Sub-classes actually implement this. |
176 | virtual std::string compile(); |
177 | |
178 | // Gets the identifier (OpName) of an ID. If not defined, an empty string will be returned. |
179 | const std::string &get_name(ID id) const; |
180 | |
181 | // Applies a decoration to an ID. Effectively injects OpDecorate. |
182 | void set_decoration(ID id, spv::Decoration decoration, uint32_t argument = 0); |
183 | void set_decoration_string(ID id, spv::Decoration decoration, const std::string &argument); |
184 | |
185 | // Overrides the identifier OpName of an ID. |
186 | // Identifiers beginning with underscores or identifiers which contain double underscores |
187 | // are reserved by the implementation. |
188 | void set_name(ID id, const std::string &name); |
189 | |
190 | // Gets a bitmask for the decorations which are applied to ID. |
191 | // I.e. (1ull << spv::DecorationFoo) | (1ull << spv::DecorationBar) |
192 | const Bitset &get_decoration_bitset(ID id) const; |
193 | |
194 | // Returns whether the decoration has been applied to the ID. |
195 | bool has_decoration(ID id, spv::Decoration decoration) const; |
196 | |
197 | // Gets the value for decorations which take arguments. |
198 | // If the decoration is a boolean (i.e. spv::DecorationNonWritable), |
199 | // 1 will be returned. |
200 | // If decoration doesn't exist or decoration is not recognized, |
201 | // 0 will be returned. |
202 | uint32_t get_decoration(ID id, spv::Decoration decoration) const; |
203 | const std::string &get_decoration_string(ID id, spv::Decoration decoration) const; |
204 | |
205 | // Removes the decoration for an ID. |
206 | void unset_decoration(ID id, spv::Decoration decoration); |
207 | |
208 | // Gets the SPIR-V type associated with ID. |
209 | // Mostly used with Resource::type_id and Resource::base_type_id to parse the underlying type of a resource. |
210 | const SPIRType &get_type(TypeID id) const; |
211 | |
212 | // Gets the SPIR-V type of a variable. |
213 | const SPIRType &get_type_from_variable(VariableID id) const; |
214 | |
215 | // Gets the underlying storage class for an OpVariable. |
216 | spv::StorageClass get_storage_class(VariableID id) const; |
217 | |
218 | // If get_name() is an empty string, get the fallback name which will be used |
219 | // instead in the disassembled source. |
220 | virtual const std::string get_fallback_name(ID id) const; |
221 | |
222 | // If get_name() of a Block struct is an empty string, get the fallback name. |
223 | // This needs to be per-variable as multiple variables can use the same block type. |
224 | virtual const std::string get_block_fallback_name(VariableID id) const; |
225 | |
226 | // Given an OpTypeStruct in ID, obtain the identifier for member number "index". |
227 | // This may be an empty string. |
228 | const std::string &get_member_name(TypeID id, uint32_t index) const; |
229 | |
230 | // Given an OpTypeStruct in ID, obtain the OpMemberDecoration for member number "index". |
231 | uint32_t get_member_decoration(TypeID id, uint32_t index, spv::Decoration decoration) const; |
232 | const std::string &get_member_decoration_string(TypeID id, uint32_t index, spv::Decoration decoration) const; |
233 | |
234 | // Sets the member identifier for OpTypeStruct ID, member number "index". |
235 | void set_member_name(TypeID id, uint32_t index, const std::string &name); |
236 | |
237 | // Returns the qualified member identifier for OpTypeStruct ID, member number "index", |
238 | // or an empty string if no qualified alias exists |
239 | const std::string &get_member_qualified_name(TypeID type_id, uint32_t index) const; |
240 | |
241 | // Gets the decoration mask for a member of a struct, similar to get_decoration_mask. |
242 | const Bitset &get_member_decoration_bitset(TypeID id, uint32_t index) const; |
243 | |
244 | // Returns whether the decoration has been applied to a member of a struct. |
245 | bool has_member_decoration(TypeID id, uint32_t index, spv::Decoration decoration) const; |
246 | |
247 | // Similar to set_decoration, but for struct members. |
248 | void set_member_decoration(TypeID id, uint32_t index, spv::Decoration decoration, uint32_t argument = 0); |
249 | void set_member_decoration_string(TypeID id, uint32_t index, spv::Decoration decoration, |
250 | const std::string &argument); |
251 | |
252 | // Unsets a member decoration, similar to unset_decoration. |
253 | void unset_member_decoration(TypeID id, uint32_t index, spv::Decoration decoration); |
254 | |
255 | // Gets the fallback name for a member, similar to get_fallback_name. |
256 | virtual const std::string get_fallback_member_name(uint32_t index) const |
257 | { |
258 | return join(ts: "_" , ts&: index); |
259 | } |
260 | |
261 | // Returns a vector of which members of a struct are potentially in use by a |
262 | // SPIR-V shader. The granularity of this analysis is per-member of a struct. |
263 | // This can be used for Buffer (UBO), BufferBlock/StorageBuffer (SSBO) and PushConstant blocks. |
264 | // ID is the Resource::id obtained from get_shader_resources(). |
265 | SmallVector<BufferRange> get_active_buffer_ranges(VariableID id) const; |
266 | |
267 | // Returns the effective size of a buffer block. |
268 | size_t get_declared_struct_size(const SPIRType &struct_type) const; |
269 | |
270 | // Returns the effective size of a buffer block, with a given array size |
271 | // for a runtime array. |
272 | // SSBOs are typically declared as runtime arrays. get_declared_struct_size() will return 0 for the size. |
273 | // This is not very helpful for applications which might need to know the array stride of its last member. |
274 | // This can be done through the API, but it is not very intuitive how to accomplish this, so here we provide a helper function |
275 | // to query the size of the buffer, assuming that the last member has a certain size. |
276 | // If the buffer does not contain a runtime array, array_size is ignored, and the function will behave as |
277 | // get_declared_struct_size(). |
278 | // To get the array stride of the last member, something like: |
279 | // get_declared_struct_size_runtime_array(type, 1) - get_declared_struct_size_runtime_array(type, 0) will work. |
280 | size_t get_declared_struct_size_runtime_array(const SPIRType &struct_type, size_t array_size) const; |
281 | |
282 | // Returns the effective size of a buffer block struct member. |
283 | size_t get_declared_struct_member_size(const SPIRType &struct_type, uint32_t index) const; |
284 | |
285 | // Returns a set of all global variables which are statically accessed |
286 | // by the control flow graph from the current entry point. |
287 | // Only variables which change the interface for a shader are returned, that is, |
288 | // variables with storage class of Input, Output, Uniform, UniformConstant, PushConstant and AtomicCounter |
289 | // storage classes are returned. |
290 | // |
291 | // To use the returned set as the filter for which variables are used during compilation, |
292 | // this set can be moved to set_enabled_interface_variables(). |
293 | std::unordered_set<VariableID> get_active_interface_variables() const; |
294 | |
295 | // Sets the interface variables which are used during compilation. |
296 | // By default, all variables are used. |
297 | // Once set, compile() will only consider the set in active_variables. |
298 | void set_enabled_interface_variables(std::unordered_set<VariableID> active_variables); |
299 | |
300 | // Query shader resources, use ids with reflection interface to modify or query binding points, etc. |
301 | ShaderResources get_shader_resources() const; |
302 | |
303 | // Query shader resources, but only return the variables which are part of active_variables. |
304 | // E.g.: get_shader_resources(get_active_variables()) to only return the variables which are statically |
305 | // accessed. |
306 | ShaderResources get_shader_resources(const std::unordered_set<VariableID> &active_variables) const; |
307 | |
308 | // Remapped variables are considered built-in variables and a backend will |
309 | // not emit a declaration for this variable. |
310 | // This is mostly useful for making use of builtins which are dependent on extensions. |
311 | void set_remapped_variable_state(VariableID id, bool remap_enable); |
312 | bool get_remapped_variable_state(VariableID id) const; |
313 | |
314 | // For subpassInput variables which are remapped to plain variables, |
315 | // the number of components in the remapped |
316 | // variable must be specified as the backing type of subpass inputs are opaque. |
317 | void set_subpass_input_remapped_components(VariableID id, uint32_t components); |
318 | uint32_t get_subpass_input_remapped_components(VariableID id) const; |
319 | |
320 | // All operations work on the current entry point. |
321 | // Entry points can be swapped out with set_entry_point(). |
322 | // Entry points should be set right after the constructor completes as some reflection functions traverse the graph from the entry point. |
323 | // Resource reflection also depends on the entry point. |
324 | // By default, the current entry point is set to the first OpEntryPoint which appears in the SPIR-V module. |
325 | |
326 | // Some shader languages restrict the names that can be given to entry points, and the |
327 | // corresponding backend will automatically rename an entry point name, during the call |
328 | // to compile() if it is illegal. For example, the common entry point name main() is |
329 | // illegal in MSL, and is renamed to an alternate name by the MSL backend. |
330 | // Given the original entry point name contained in the SPIR-V, this function returns |
331 | // the name, as updated by the backend during the call to compile(). If the name is not |
332 | // illegal, and has not been renamed, or if this function is called before compile(), |
333 | // this function will simply return the same name. |
334 | |
335 | // New variants of entry point query and reflection. |
336 | // Names for entry points in the SPIR-V module may alias if they belong to different execution models. |
337 | // To disambiguate, we must pass along with the entry point names the execution model. |
338 | SmallVector<EntryPoint> get_entry_points_and_stages() const; |
339 | void set_entry_point(const std::string &entry, spv::ExecutionModel execution_model); |
340 | |
341 | // Renames an entry point from old_name to new_name. |
342 | // If old_name is currently selected as the current entry point, it will continue to be the current entry point, |
343 | // albeit with a new name. |
344 | // get_entry_points() is essentially invalidated at this point. |
345 | void rename_entry_point(const std::string &old_name, const std::string &new_name, |
346 | spv::ExecutionModel execution_model); |
347 | const SPIREntryPoint &get_entry_point(const std::string &name, spv::ExecutionModel execution_model) const; |
348 | SPIREntryPoint &get_entry_point(const std::string &name, spv::ExecutionModel execution_model); |
349 | const std::string &get_cleansed_entry_point_name(const std::string &name, |
350 | spv::ExecutionModel execution_model) const; |
351 | |
352 | // Traverses all reachable opcodes and sets active_builtins to a bitmask of all builtin variables which are accessed in the shader. |
353 | void update_active_builtins(); |
354 | bool has_active_builtin(spv::BuiltIn builtin, spv::StorageClass storage) const; |
355 | |
356 | // Query and modify OpExecutionMode. |
357 | const Bitset &get_execution_mode_bitset() const; |
358 | |
359 | void unset_execution_mode(spv::ExecutionMode mode); |
360 | void set_execution_mode(spv::ExecutionMode mode, uint32_t arg0 = 0, uint32_t arg1 = 0, uint32_t arg2 = 0); |
361 | |
362 | // Gets argument for an execution mode (LocalSize, Invocations, OutputVertices). |
363 | // For LocalSize or LocalSizeId, the index argument is used to select the dimension (X = 0, Y = 1, Z = 2). |
364 | // For execution modes which do not have arguments, 0 is returned. |
365 | // LocalSizeId query returns an ID. If LocalSizeId execution mode is not used, it returns 0. |
366 | // LocalSize always returns a literal. If execution mode is LocalSizeId, |
367 | // the literal (spec constant or not) is still returned. |
368 | uint32_t get_execution_mode_argument(spv::ExecutionMode mode, uint32_t index = 0) const; |
369 | spv::ExecutionModel get_execution_model() const; |
370 | |
371 | bool is_tessellation_shader() const; |
372 | |
373 | // In SPIR-V, the compute work group size can be represented by a constant vector, in which case |
374 | // the LocalSize execution mode is ignored. |
375 | // |
376 | // This constant vector can be a constant vector, specialization constant vector, or partly specialized constant vector. |
377 | // To modify and query work group dimensions which are specialization constants, SPIRConstant values must be modified |
378 | // directly via get_constant() rather than using LocalSize directly. This function will return which constants should be modified. |
379 | // |
380 | // To modify dimensions which are *not* specialization constants, set_execution_mode should be used directly. |
381 | // Arguments to set_execution_mode which are specialization constants are effectively ignored during compilation. |
382 | // NOTE: This is somewhat different from how SPIR-V works. In SPIR-V, the constant vector will completely replace LocalSize, |
383 | // while in this interface, LocalSize is only ignored for specialization constants. |
384 | // |
385 | // The specialization constant will be written to x, y and z arguments. |
386 | // If the component is not a specialization constant, a zeroed out struct will be written. |
387 | // The return value is the constant ID of the builtin WorkGroupSize, but this is not expected to be useful |
388 | // for most use cases. |
389 | // If LocalSizeId is used, there is no uvec3 value representing the workgroup size, so the return value is 0, |
390 | // but x, y and z are written as normal if the components are specialization constants. |
391 | uint32_t get_work_group_size_specialization_constants(SpecializationConstant &x, SpecializationConstant &y, |
392 | SpecializationConstant &z) const; |
393 | |
394 | // Analyzes all OpImageFetch (texelFetch) opcodes and checks if there are instances where |
395 | // said instruction is used without a combined image sampler. |
396 | // GLSL targets do not support the use of texelFetch without a sampler. |
397 | // To workaround this, we must inject a dummy sampler which can be used to form a sampler2D at the call-site of |
398 | // texelFetch as necessary. |
399 | // |
400 | // This must be called before build_combined_image_samplers(). |
401 | // build_combined_image_samplers() may refer to the ID returned by this method if the returned ID is non-zero. |
402 | // The return value will be the ID of a sampler object if a dummy sampler is necessary, or 0 if no sampler object |
403 | // is required. |
404 | // |
405 | // If the returned ID is non-zero, it can be decorated with set/bindings as desired before calling compile(). |
406 | // Calling this function also invalidates get_active_interface_variables(), so this should be called |
407 | // before that function. |
408 | VariableID build_dummy_sampler_for_combined_images(); |
409 | |
410 | // Analyzes all separate image and samplers used from the currently selected entry point, |
411 | // and re-routes them all to a combined image sampler instead. |
412 | // This is required to "support" separate image samplers in targets which do not natively support |
413 | // this feature, like GLSL/ESSL. |
414 | // |
415 | // This must be called before compile() if such remapping is desired. |
416 | // This call will add new sampled images to the SPIR-V, |
417 | // so it will appear in reflection if get_shader_resources() is called after build_combined_image_samplers. |
418 | // |
419 | // If any image/sampler remapping was found, no separate image/samplers will appear in the decompiled output, |
420 | // but will still appear in reflection. |
421 | // |
422 | // The resulting samplers will be void of any decorations like name, descriptor sets and binding points, |
423 | // so this can be added before compile() if desired. |
424 | // |
425 | // Combined image samplers originating from this set are always considered active variables. |
426 | // Arrays of separate samplers are not supported, but arrays of separate images are supported. |
427 | // Array of images + sampler -> Array of combined image samplers. |
428 | void build_combined_image_samplers(); |
429 | |
430 | // Gets a remapping for the combined image samplers. |
431 | const SmallVector<CombinedImageSampler> &get_combined_image_samplers() const |
432 | { |
433 | return combined_image_samplers; |
434 | } |
435 | |
436 | // Set a new variable type remap callback. |
437 | // The type remapping is designed to allow global interface variable to assume more special types. |
438 | // A typical example here is to remap sampler2D into samplerExternalOES, which currently isn't supported |
439 | // directly by SPIR-V. |
440 | // |
441 | // In compile() while emitting code, |
442 | // for every variable that is declared, including function parameters, the callback will be called |
443 | // and the API user has a chance to change the textual representation of the type used to declare the variable. |
444 | // The API user can detect special patterns in names to guide the remapping. |
445 | void set_variable_type_remap_callback(VariableTypeRemapCallback cb) |
446 | { |
447 | variable_remap_callback = std::move(cb); |
448 | } |
449 | |
450 | // API for querying which specialization constants exist. |
451 | // To modify a specialization constant before compile(), use get_constant(constant.id), |
452 | // then update constants directly in the SPIRConstant data structure. |
453 | // For composite types, the subconstants can be iterated over and modified. |
454 | // constant_type is the SPIRType for the specialization constant, |
455 | // which can be queried to determine which fields in the unions should be poked at. |
456 | SmallVector<SpecializationConstant> get_specialization_constants() const; |
457 | SPIRConstant &get_constant(ConstantID id); |
458 | const SPIRConstant &get_constant(ConstantID id) const; |
459 | |
460 | uint32_t get_current_id_bound() const |
461 | { |
462 | return uint32_t(ir.ids.size()); |
463 | } |
464 | |
465 | // API for querying buffer objects. |
466 | // The type passed in here should be the base type of a resource, i.e. |
467 | // get_type(resource.base_type_id) |
468 | // as decorations are set in the basic Block type. |
469 | // The type passed in here must have these decorations set, or an exception is raised. |
470 | // Only UBOs and SSBOs or sub-structs which are part of these buffer types will have these decorations set. |
471 | uint32_t type_struct_member_offset(const SPIRType &type, uint32_t index) const; |
472 | uint32_t type_struct_member_array_stride(const SPIRType &type, uint32_t index) const; |
473 | uint32_t type_struct_member_matrix_stride(const SPIRType &type, uint32_t index) const; |
474 | |
475 | // Gets the offset in SPIR-V words (uint32_t) for a decoration which was originally declared in the SPIR-V binary. |
476 | // The offset will point to one or more uint32_t literals which can be modified in-place before using the SPIR-V binary. |
477 | // Note that adding or removing decorations using the reflection API will not change the behavior of this function. |
478 | // If the decoration was declared, sets the word_offset to an offset into the provided SPIR-V binary buffer and returns true, |
479 | // otherwise, returns false. |
480 | // If the decoration does not have any value attached to it (e.g. DecorationRelaxedPrecision), this function will also return false. |
481 | bool get_binary_offset_for_decoration(VariableID id, spv::Decoration decoration, uint32_t &word_offset) const; |
482 | |
483 | // HLSL counter buffer reflection interface. |
484 | // Append/Consume/Increment/Decrement in HLSL is implemented as two "neighbor" buffer objects where |
485 | // one buffer implements the storage, and a single buffer containing just a lone "int" implements the counter. |
486 | // To SPIR-V these will be exposed as two separate buffers, but glslang HLSL frontend emits a special indentifier |
487 | // which lets us link the two buffers together. |
488 | |
489 | // Queries if a variable ID is a counter buffer which "belongs" to a regular buffer object. |
490 | |
491 | // If SPV_GOOGLE_hlsl_functionality1 is used, this can be used even with a stripped SPIR-V module. |
492 | // Otherwise, this query is purely based on OpName identifiers as found in the SPIR-V module, and will |
493 | // only return true if OpSource was reported HLSL. |
494 | // To rely on this functionality, ensure that the SPIR-V module is not stripped. |
495 | |
496 | bool buffer_is_hlsl_counter_buffer(VariableID id) const; |
497 | |
498 | // Queries if a buffer object has a neighbor "counter" buffer. |
499 | // If so, the ID of that counter buffer will be returned in counter_id. |
500 | // If SPV_GOOGLE_hlsl_functionality1 is used, this can be used even with a stripped SPIR-V module. |
501 | // Otherwise, this query is purely based on OpName identifiers as found in the SPIR-V module, and will |
502 | // only return true if OpSource was reported HLSL. |
503 | // To rely on this functionality, ensure that the SPIR-V module is not stripped. |
504 | bool buffer_get_hlsl_counter_buffer(VariableID id, uint32_t &counter_id) const; |
505 | |
506 | // Gets the list of all SPIR-V Capabilities which were declared in the SPIR-V module. |
507 | const SmallVector<spv::Capability> &get_declared_capabilities() const; |
508 | |
509 | // Gets the list of all SPIR-V extensions which were declared in the SPIR-V module. |
510 | const SmallVector<std::string> &get_declared_extensions() const; |
511 | |
512 | // When declaring buffer blocks in GLSL, the name declared in the GLSL source |
513 | // might not be the same as the name declared in the SPIR-V module due to naming conflicts. |
514 | // In this case, SPIRV-Cross needs to find a fallback-name, and it might only |
515 | // be possible to know this name after compiling to GLSL. |
516 | // This is particularly important for HLSL input and UAVs which tends to reuse the same block type |
517 | // for multiple distinct blocks. For these cases it is not possible to modify the name of the type itself |
518 | // because it might be unique. Instead, you can use this interface to check after compilation which |
519 | // name was actually used if your input SPIR-V tends to have this problem. |
520 | // For other names like remapped names for variables, etc, it's generally enough to query the name of the variables |
521 | // after compiling, block names are an exception to this rule. |
522 | // ID is the name of a variable as returned by Resource::id, and must be a variable with a Block-like type. |
523 | // |
524 | // This also applies to HLSL cbuffers. |
525 | std::string get_remapped_declared_block_name(VariableID id) const; |
526 | |
527 | // For buffer block variables, get the decorations for that variable. |
528 | // Sometimes, decorations for buffer blocks are found in member decorations instead |
529 | // of direct decorations on the variable itself. |
530 | // The most common use here is to check if a buffer is readonly or writeonly. |
531 | Bitset get_buffer_block_flags(VariableID id) const; |
532 | |
533 | // Returns whether the position output is invariant |
534 | bool is_position_invariant() const |
535 | { |
536 | return position_invariant; |
537 | } |
538 | |
539 | protected: |
540 | const uint32_t *stream(const Instruction &instr) const |
541 | { |
542 | // If we're not going to use any arguments, just return nullptr. |
543 | // We want to avoid case where we return an out of range pointer |
544 | // that trips debug assertions on some platforms. |
545 | if (!instr.length) |
546 | return nullptr; |
547 | |
548 | if (instr.is_embedded()) |
549 | { |
550 | auto &embedded = static_cast<const EmbeddedInstruction &>(instr); |
551 | assert(embedded.ops.size() == instr.length); |
552 | return embedded.ops.data(); |
553 | } |
554 | else |
555 | { |
556 | if (instr.offset + instr.length > ir.spirv.size()) |
557 | SPIRV_CROSS_THROW("Compiler::stream() out of range." ); |
558 | return &ir.spirv[instr.offset]; |
559 | } |
560 | } |
561 | |
562 | uint32_t *stream_mutable(const Instruction &instr) const |
563 | { |
564 | return const_cast<uint32_t *>(stream(instr)); |
565 | } |
566 | |
567 | ParsedIR ir; |
568 | // Marks variables which have global scope and variables which can alias with other variables |
569 | // (SSBO, image load store, etc) |
570 | SmallVector<uint32_t> global_variables; |
571 | SmallVector<uint32_t> aliased_variables; |
572 | |
573 | SPIRFunction *current_function = nullptr; |
574 | SPIRBlock *current_block = nullptr; |
575 | uint32_t current_loop_level = 0; |
576 | std::unordered_set<VariableID> active_interface_variables; |
577 | bool check_active_interface_variables = false; |
578 | |
579 | void add_loop_level(); |
580 | |
581 | void set_initializers(SPIRExpression &e) |
582 | { |
583 | e.emitted_loop_level = current_loop_level; |
584 | } |
585 | |
586 | template <typename T> |
587 | void set_initializers(const T &) |
588 | { |
589 | } |
590 | |
591 | // If our IDs are out of range here as part of opcodes, throw instead of |
592 | // undefined behavior. |
593 | template <typename T, typename... P> |
594 | T &set(uint32_t id, P &&... args) |
595 | { |
596 | ir.add_typed_id(type: static_cast<Types>(T::type), id); |
597 | auto &var = variant_set<T>(ir.ids[id], std::forward<P>(args)...); |
598 | var.self = id; |
599 | set_initializers(var); |
600 | return var; |
601 | } |
602 | |
603 | template <typename T> |
604 | T &get(uint32_t id) |
605 | { |
606 | return variant_get<T>(ir.ids[id]); |
607 | } |
608 | |
609 | template <typename T> |
610 | T *maybe_get(uint32_t id) |
611 | { |
612 | if (id >= ir.ids.size()) |
613 | return nullptr; |
614 | else if (ir.ids[id].get_type() == static_cast<Types>(T::type)) |
615 | return &get<T>(id); |
616 | else |
617 | return nullptr; |
618 | } |
619 | |
620 | template <typename T> |
621 | const T &get(uint32_t id) const |
622 | { |
623 | return variant_get<T>(ir.ids[id]); |
624 | } |
625 | |
626 | template <typename T> |
627 | const T *maybe_get(uint32_t id) const |
628 | { |
629 | if (id >= ir.ids.size()) |
630 | return nullptr; |
631 | else if (ir.ids[id].get_type() == static_cast<Types>(T::type)) |
632 | return &get<T>(id); |
633 | else |
634 | return nullptr; |
635 | } |
636 | |
637 | // Gets the id of SPIR-V type underlying the given type_id, which might be a pointer. |
638 | uint32_t get_pointee_type_id(uint32_t type_id) const; |
639 | |
640 | // Gets the SPIR-V type underlying the given type, which might be a pointer. |
641 | const SPIRType &get_pointee_type(const SPIRType &type) const; |
642 | |
643 | // Gets the SPIR-V type underlying the given type_id, which might be a pointer. |
644 | const SPIRType &get_pointee_type(uint32_t type_id) const; |
645 | |
646 | // Gets the ID of the SPIR-V type underlying a variable. |
647 | uint32_t get_variable_data_type_id(const SPIRVariable &var) const; |
648 | |
649 | // Gets the SPIR-V type underlying a variable. |
650 | SPIRType &get_variable_data_type(const SPIRVariable &var); |
651 | |
652 | // Gets the SPIR-V type underlying a variable. |
653 | const SPIRType &get_variable_data_type(const SPIRVariable &var) const; |
654 | |
655 | // Gets the SPIR-V element type underlying an array variable. |
656 | SPIRType &get_variable_element_type(const SPIRVariable &var); |
657 | |
658 | // Gets the SPIR-V element type underlying an array variable. |
659 | const SPIRType &get_variable_element_type(const SPIRVariable &var) const; |
660 | |
661 | // Sets the qualified member identifier for OpTypeStruct ID, member number "index". |
662 | void set_member_qualified_name(uint32_t type_id, uint32_t index, const std::string &name); |
663 | void set_qualified_name(uint32_t id, const std::string &name); |
664 | |
665 | // Returns if the given type refers to a sampled image. |
666 | bool is_sampled_image_type(const SPIRType &type); |
667 | |
668 | const SPIREntryPoint &get_entry_point() const; |
669 | SPIREntryPoint &get_entry_point(); |
670 | static bool is_tessellation_shader(spv::ExecutionModel model); |
671 | |
672 | virtual std::string to_name(uint32_t id, bool allow_alias = true) const; |
673 | bool is_builtin_variable(const SPIRVariable &var) const; |
674 | bool is_builtin_type(const SPIRType &type) const; |
675 | bool is_hidden_variable(const SPIRVariable &var, bool include_builtins = false) const; |
676 | bool is_immutable(uint32_t id) const; |
677 | bool is_member_builtin(const SPIRType &type, uint32_t index, spv::BuiltIn *builtin) const; |
678 | bool is_scalar(const SPIRType &type) const; |
679 | bool is_vector(const SPIRType &type) const; |
680 | bool is_matrix(const SPIRType &type) const; |
681 | bool is_array(const SPIRType &type) const; |
682 | uint32_t expression_type_id(uint32_t id) const; |
683 | const SPIRType &expression_type(uint32_t id) const; |
684 | bool expression_is_lvalue(uint32_t id) const; |
685 | bool variable_storage_is_aliased(const SPIRVariable &var); |
686 | SPIRVariable *maybe_get_backing_variable(uint32_t chain); |
687 | |
688 | void register_read(uint32_t expr, uint32_t chain, bool forwarded); |
689 | void register_write(uint32_t chain); |
690 | |
691 | inline bool is_continue(uint32_t next) const |
692 | { |
693 | return (ir.block_meta[next] & ParsedIR::BLOCK_META_CONTINUE_BIT) != 0; |
694 | } |
695 | |
696 | inline bool is_single_block_loop(uint32_t next) const |
697 | { |
698 | auto &block = get<SPIRBlock>(id: next); |
699 | return block.merge == SPIRBlock::MergeLoop && block.continue_block == ID(next); |
700 | } |
701 | |
702 | inline bool is_break(uint32_t next) const |
703 | { |
704 | return (ir.block_meta[next] & |
705 | (ParsedIR::BLOCK_META_LOOP_MERGE_BIT | ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT)) != 0; |
706 | } |
707 | |
708 | inline bool is_loop_break(uint32_t next) const |
709 | { |
710 | return (ir.block_meta[next] & ParsedIR::BLOCK_META_LOOP_MERGE_BIT) != 0; |
711 | } |
712 | |
713 | inline bool is_conditional(uint32_t next) const |
714 | { |
715 | return (ir.block_meta[next] & |
716 | (ParsedIR::BLOCK_META_SELECTION_MERGE_BIT | ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT)) != 0; |
717 | } |
718 | |
719 | // Dependency tracking for temporaries read from variables. |
720 | void flush_dependees(SPIRVariable &var); |
721 | void flush_all_active_variables(); |
722 | void flush_control_dependent_expressions(uint32_t block); |
723 | void flush_all_atomic_capable_variables(); |
724 | void flush_all_aliased_variables(); |
725 | void register_global_read_dependencies(const SPIRBlock &func, uint32_t id); |
726 | void register_global_read_dependencies(const SPIRFunction &func, uint32_t id); |
727 | std::unordered_set<uint32_t> invalid_expressions; |
728 | |
729 | void update_name_cache(std::unordered_set<std::string> &cache, std::string &name); |
730 | |
731 | // A variant which takes two sets of names. The secondary is only used to verify there are no collisions, |
732 | // but the set is not updated when we have found a new name. |
733 | // Used primarily when adding block interface names. |
734 | void update_name_cache(std::unordered_set<std::string> &cache_primary, |
735 | const std::unordered_set<std::string> &cache_secondary, std::string &name); |
736 | |
737 | bool function_is_pure(const SPIRFunction &func); |
738 | bool block_is_pure(const SPIRBlock &block); |
739 | |
740 | bool execution_is_branchless(const SPIRBlock &from, const SPIRBlock &to) const; |
741 | bool execution_is_direct_branch(const SPIRBlock &from, const SPIRBlock &to) const; |
742 | bool execution_is_noop(const SPIRBlock &from, const SPIRBlock &to) const; |
743 | SPIRBlock::ContinueBlockType continue_block_type(const SPIRBlock &continue_block) const; |
744 | |
745 | void force_recompile(); |
746 | void force_recompile_guarantee_forward_progress(); |
747 | void clear_force_recompile(); |
748 | bool is_forcing_recompilation() const; |
749 | bool is_force_recompile = false; |
750 | bool is_force_recompile_forward_progress = false; |
751 | |
752 | bool block_is_loop_candidate(const SPIRBlock &block, SPIRBlock::Method method) const; |
753 | |
754 | bool types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const; |
755 | void inherit_expression_dependencies(uint32_t dst, uint32_t source); |
756 | void add_implied_read_expression(SPIRExpression &e, uint32_t source); |
757 | void add_implied_read_expression(SPIRAccessChain &e, uint32_t source); |
758 | |
759 | // For proper multiple entry point support, allow querying if an Input or Output |
760 | // variable is part of that entry points interface. |
761 | bool interface_variable_exists_in_entry_point(uint32_t id) const; |
762 | |
763 | SmallVector<CombinedImageSampler> combined_image_samplers; |
764 | |
765 | void remap_variable_type_name(const SPIRType &type, const std::string &var_name, std::string &type_name) const |
766 | { |
767 | if (variable_remap_callback) |
768 | variable_remap_callback(type, var_name, type_name); |
769 | } |
770 | |
771 | void set_ir(const ParsedIR &parsed); |
772 | void set_ir(ParsedIR &&parsed); |
773 | void parse_fixup(); |
774 | |
775 | // Used internally to implement various traversals for queries. |
776 | struct OpcodeHandler |
777 | { |
778 | virtual ~OpcodeHandler() = default; |
779 | |
780 | // Return true if traversal should continue. |
781 | // If false, traversal will end immediately. |
782 | virtual bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) = 0; |
783 | virtual bool handle_terminator(const SPIRBlock &) |
784 | { |
785 | return true; |
786 | } |
787 | |
788 | virtual bool follow_function_call(const SPIRFunction &) |
789 | { |
790 | return true; |
791 | } |
792 | |
793 | virtual void set_current_block(const SPIRBlock &) |
794 | { |
795 | } |
796 | |
797 | // Called after returning from a function or when entering a block, |
798 | // can be called multiple times per block, |
799 | // while set_current_block is only called on block entry. |
800 | virtual void rearm_current_block(const SPIRBlock &) |
801 | { |
802 | } |
803 | |
804 | virtual bool begin_function_scope(const uint32_t *, uint32_t) |
805 | { |
806 | return true; |
807 | } |
808 | |
809 | virtual bool end_function_scope(const uint32_t *, uint32_t) |
810 | { |
811 | return true; |
812 | } |
813 | }; |
814 | |
815 | struct BufferAccessHandler : OpcodeHandler |
816 | { |
817 | BufferAccessHandler(const Compiler &compiler_, SmallVector<BufferRange> &ranges_, uint32_t id_) |
818 | : compiler(compiler_) |
819 | , ranges(ranges_) |
820 | , id(id_) |
821 | { |
822 | } |
823 | |
824 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
825 | |
826 | const Compiler &compiler; |
827 | SmallVector<BufferRange> &ranges; |
828 | uint32_t id; |
829 | |
830 | std::unordered_set<uint32_t> seen; |
831 | }; |
832 | |
833 | struct InterfaceVariableAccessHandler : OpcodeHandler |
834 | { |
835 | InterfaceVariableAccessHandler(const Compiler &compiler_, std::unordered_set<VariableID> &variables_) |
836 | : compiler(compiler_) |
837 | , variables(variables_) |
838 | { |
839 | } |
840 | |
841 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
842 | |
843 | const Compiler &compiler; |
844 | std::unordered_set<VariableID> &variables; |
845 | }; |
846 | |
847 | struct CombinedImageSamplerHandler : OpcodeHandler |
848 | { |
849 | CombinedImageSamplerHandler(Compiler &compiler_) |
850 | : compiler(compiler_) |
851 | { |
852 | } |
853 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
854 | bool begin_function_scope(const uint32_t *args, uint32_t length) override; |
855 | bool end_function_scope(const uint32_t *args, uint32_t length) override; |
856 | |
857 | Compiler &compiler; |
858 | |
859 | // Each function in the call stack needs its own remapping for parameters so we can deduce which global variable each texture/sampler the parameter is statically bound to. |
860 | std::stack<std::unordered_map<uint32_t, uint32_t>> parameter_remapping; |
861 | std::stack<SPIRFunction *> functions; |
862 | |
863 | uint32_t remap_parameter(uint32_t id); |
864 | void push_remap_parameters(const SPIRFunction &func, const uint32_t *args, uint32_t length); |
865 | void pop_remap_parameters(); |
866 | void register_combined_image_sampler(SPIRFunction &caller, VariableID combined_id, VariableID texture_id, |
867 | VariableID sampler_id, bool depth); |
868 | }; |
869 | |
870 | struct DummySamplerForCombinedImageHandler : OpcodeHandler |
871 | { |
872 | DummySamplerForCombinedImageHandler(Compiler &compiler_) |
873 | : compiler(compiler_) |
874 | { |
875 | } |
876 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
877 | |
878 | Compiler &compiler; |
879 | bool need_dummy_sampler = false; |
880 | }; |
881 | |
882 | struct ActiveBuiltinHandler : OpcodeHandler |
883 | { |
884 | ActiveBuiltinHandler(Compiler &compiler_) |
885 | : compiler(compiler_) |
886 | { |
887 | } |
888 | |
889 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
890 | Compiler &compiler; |
891 | |
892 | void handle_builtin(const SPIRType &type, spv::BuiltIn builtin, const Bitset &decoration_flags); |
893 | void add_if_builtin(uint32_t id); |
894 | void add_if_builtin_or_block(uint32_t id); |
895 | void add_if_builtin(uint32_t id, bool allow_blocks); |
896 | }; |
897 | |
898 | bool traverse_all_reachable_opcodes(const SPIRBlock &block, OpcodeHandler &handler) const; |
899 | bool traverse_all_reachable_opcodes(const SPIRFunction &block, OpcodeHandler &handler) const; |
900 | // This must be an ordered data structure so we always pick the same type aliases. |
901 | SmallVector<uint32_t> global_struct_cache; |
902 | |
903 | ShaderResources get_shader_resources(const std::unordered_set<VariableID> *active_variables) const; |
904 | |
905 | VariableTypeRemapCallback variable_remap_callback; |
906 | |
907 | bool get_common_basic_type(const SPIRType &type, SPIRType::BaseType &base_type); |
908 | |
909 | std::unordered_set<uint32_t> forced_temporaries; |
910 | std::unordered_set<uint32_t> forwarded_temporaries; |
911 | std::unordered_set<uint32_t> suppressed_usage_tracking; |
912 | std::unordered_set<uint32_t> hoisted_temporaries; |
913 | std::unordered_set<uint32_t> forced_invariant_temporaries; |
914 | |
915 | Bitset active_input_builtins; |
916 | Bitset active_output_builtins; |
917 | uint32_t clip_distance_count = 0; |
918 | uint32_t cull_distance_count = 0; |
919 | bool position_invariant = false; |
920 | |
921 | void analyze_parameter_preservation( |
922 | SPIRFunction &entry, const CFG &cfg, |
923 | const std::unordered_map<uint32_t, std::unordered_set<uint32_t>> &variable_to_blocks, |
924 | const std::unordered_map<uint32_t, std::unordered_set<uint32_t>> &complete_write_blocks); |
925 | |
926 | // If a variable ID or parameter ID is found in this set, a sampler is actually a shadow/comparison sampler. |
927 | // SPIR-V does not support this distinction, so we must keep track of this information outside the type system. |
928 | // There might be unrelated IDs found in this set which do not correspond to actual variables. |
929 | // This set should only be queried for the existence of samplers which are already known to be variables or parameter IDs. |
930 | // Similar is implemented for images, as well as if subpass inputs are needed. |
931 | std::unordered_set<uint32_t> comparison_ids; |
932 | bool need_subpass_input = false; |
933 | |
934 | // In certain backends, we will need to use a dummy sampler to be able to emit code. |
935 | // GLSL does not support texelFetch on texture2D objects, but SPIR-V does, |
936 | // so we need to workaround by having the application inject a dummy sampler. |
937 | uint32_t dummy_sampler_id = 0; |
938 | |
939 | void analyze_image_and_sampler_usage(); |
940 | |
941 | struct CombinedImageSamplerDrefHandler : OpcodeHandler |
942 | { |
943 | CombinedImageSamplerDrefHandler(Compiler &compiler_) |
944 | : compiler(compiler_) |
945 | { |
946 | } |
947 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
948 | |
949 | Compiler &compiler; |
950 | std::unordered_set<uint32_t> dref_combined_samplers; |
951 | }; |
952 | |
953 | struct CombinedImageSamplerUsageHandler : OpcodeHandler |
954 | { |
955 | CombinedImageSamplerUsageHandler(Compiler &compiler_, |
956 | const std::unordered_set<uint32_t> &dref_combined_samplers_) |
957 | : compiler(compiler_) |
958 | , dref_combined_samplers(dref_combined_samplers_) |
959 | { |
960 | } |
961 | |
962 | bool begin_function_scope(const uint32_t *args, uint32_t length) override; |
963 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
964 | Compiler &compiler; |
965 | const std::unordered_set<uint32_t> &dref_combined_samplers; |
966 | |
967 | std::unordered_map<uint32_t, std::unordered_set<uint32_t>> dependency_hierarchy; |
968 | std::unordered_set<uint32_t> comparison_ids; |
969 | |
970 | void add_hierarchy_to_comparison_ids(uint32_t ids); |
971 | bool need_subpass_input = false; |
972 | void add_dependency(uint32_t dst, uint32_t src); |
973 | }; |
974 | |
975 | void build_function_control_flow_graphs_and_analyze(); |
976 | std::unordered_map<uint32_t, std::unique_ptr<CFG>> function_cfgs; |
977 | const CFG &get_cfg_for_current_function() const; |
978 | const CFG &get_cfg_for_function(uint32_t id) const; |
979 | |
980 | struct CFGBuilder : OpcodeHandler |
981 | { |
982 | explicit CFGBuilder(Compiler &compiler_); |
983 | |
984 | bool follow_function_call(const SPIRFunction &func) override; |
985 | bool handle(spv::Op op, const uint32_t *args, uint32_t length) override; |
986 | Compiler &compiler; |
987 | std::unordered_map<uint32_t, std::unique_ptr<CFG>> function_cfgs; |
988 | }; |
989 | |
990 | struct AnalyzeVariableScopeAccessHandler : OpcodeHandler |
991 | { |
992 | AnalyzeVariableScopeAccessHandler(Compiler &compiler_, SPIRFunction &entry_); |
993 | |
994 | bool follow_function_call(const SPIRFunction &) override; |
995 | void set_current_block(const SPIRBlock &block) override; |
996 | |
997 | void notify_variable_access(uint32_t id, uint32_t block); |
998 | bool id_is_phi_variable(uint32_t id) const; |
999 | bool id_is_potential_temporary(uint32_t id) const; |
1000 | bool handle(spv::Op op, const uint32_t *args, uint32_t length) override; |
1001 | bool handle_terminator(const SPIRBlock &block) override; |
1002 | |
1003 | Compiler &compiler; |
1004 | SPIRFunction &entry; |
1005 | std::unordered_map<uint32_t, std::unordered_set<uint32_t>> accessed_variables_to_block; |
1006 | std::unordered_map<uint32_t, std::unordered_set<uint32_t>> accessed_temporaries_to_block; |
1007 | std::unordered_map<uint32_t, uint32_t> result_id_to_type; |
1008 | std::unordered_map<uint32_t, std::unordered_set<uint32_t>> complete_write_variables_to_block; |
1009 | std::unordered_map<uint32_t, std::unordered_set<uint32_t>> partial_write_variables_to_block; |
1010 | std::unordered_set<uint32_t> access_chain_expressions; |
1011 | // Access chains used in multiple blocks mean hoisting all the variables used to construct the access chain as not all backends can use pointers. |
1012 | std::unordered_map<uint32_t, std::unordered_set<uint32_t>> access_chain_children; |
1013 | const SPIRBlock *current_block = nullptr; |
1014 | }; |
1015 | |
1016 | struct StaticExpressionAccessHandler : OpcodeHandler |
1017 | { |
1018 | StaticExpressionAccessHandler(Compiler &compiler_, uint32_t variable_id_); |
1019 | bool follow_function_call(const SPIRFunction &) override; |
1020 | bool handle(spv::Op op, const uint32_t *args, uint32_t length) override; |
1021 | |
1022 | Compiler &compiler; |
1023 | uint32_t variable_id; |
1024 | uint32_t static_expression = 0; |
1025 | uint32_t write_count = 0; |
1026 | }; |
1027 | |
1028 | struct PhysicalBlockMeta |
1029 | { |
1030 | uint32_t alignment = 0; |
1031 | }; |
1032 | |
1033 | struct PhysicalStorageBufferPointerHandler : OpcodeHandler |
1034 | { |
1035 | explicit PhysicalStorageBufferPointerHandler(Compiler &compiler_); |
1036 | bool handle(spv::Op op, const uint32_t *args, uint32_t length) override; |
1037 | Compiler &compiler; |
1038 | |
1039 | std::unordered_set<uint32_t> non_block_types; |
1040 | std::unordered_map<uint32_t, PhysicalBlockMeta> physical_block_type_meta; |
1041 | std::unordered_map<uint32_t, PhysicalBlockMeta *> access_chain_to_physical_block; |
1042 | |
1043 | void mark_aligned_access(uint32_t id, const uint32_t *args, uint32_t length); |
1044 | PhysicalBlockMeta *find_block_meta(uint32_t id) const; |
1045 | bool type_is_bda_block_entry(uint32_t type_id) const; |
1046 | void setup_meta_chain(uint32_t type_id, uint32_t var_id); |
1047 | uint32_t get_minimum_scalar_alignment(const SPIRType &type) const; |
1048 | void analyze_non_block_types_from_block(const SPIRType &type); |
1049 | uint32_t get_base_non_block_type_id(uint32_t type_id) const; |
1050 | }; |
1051 | void analyze_non_block_pointer_types(); |
1052 | SmallVector<uint32_t> physical_storage_non_block_pointer_types; |
1053 | std::unordered_map<uint32_t, PhysicalBlockMeta> physical_storage_type_to_alignment; |
1054 | |
1055 | void analyze_variable_scope(SPIRFunction &function, AnalyzeVariableScopeAccessHandler &handler); |
1056 | void find_function_local_luts(SPIRFunction &function, const AnalyzeVariableScopeAccessHandler &handler, |
1057 | bool single_function); |
1058 | bool may_read_undefined_variable_in_block(const SPIRBlock &block, uint32_t var); |
1059 | |
1060 | // Finds all resources that are written to from inside the critical section, if present. |
1061 | // The critical section is delimited by OpBeginInvocationInterlockEXT and |
1062 | // OpEndInvocationInterlockEXT instructions. In MSL and HLSL, any resources written |
1063 | // while inside the critical section must be placed in a raster order group. |
1064 | struct InterlockedResourceAccessHandler : OpcodeHandler |
1065 | { |
1066 | InterlockedResourceAccessHandler(Compiler &compiler_, uint32_t entry_point_id) |
1067 | : compiler(compiler_) |
1068 | { |
1069 | call_stack.push_back(t: entry_point_id); |
1070 | } |
1071 | |
1072 | bool handle(spv::Op op, const uint32_t *args, uint32_t length) override; |
1073 | bool begin_function_scope(const uint32_t *args, uint32_t length) override; |
1074 | bool end_function_scope(const uint32_t *args, uint32_t length) override; |
1075 | |
1076 | Compiler &compiler; |
1077 | bool in_crit_sec = false; |
1078 | |
1079 | uint32_t interlock_function_id = 0; |
1080 | bool split_function_case = false; |
1081 | bool control_flow_interlock = false; |
1082 | bool use_critical_section = false; |
1083 | bool call_stack_is_interlocked = false; |
1084 | SmallVector<uint32_t> call_stack; |
1085 | |
1086 | void access_potential_resource(uint32_t id); |
1087 | }; |
1088 | |
1089 | struct InterlockedResourceAccessPrepassHandler : OpcodeHandler |
1090 | { |
1091 | InterlockedResourceAccessPrepassHandler(Compiler &compiler_, uint32_t entry_point_id) |
1092 | : compiler(compiler_) |
1093 | { |
1094 | call_stack.push_back(t: entry_point_id); |
1095 | } |
1096 | |
1097 | void rearm_current_block(const SPIRBlock &block) override; |
1098 | bool handle(spv::Op op, const uint32_t *args, uint32_t length) override; |
1099 | bool begin_function_scope(const uint32_t *args, uint32_t length) override; |
1100 | bool end_function_scope(const uint32_t *args, uint32_t length) override; |
1101 | |
1102 | Compiler &compiler; |
1103 | uint32_t interlock_function_id = 0; |
1104 | uint32_t current_block_id = 0; |
1105 | bool split_function_case = false; |
1106 | bool control_flow_interlock = false; |
1107 | SmallVector<uint32_t> call_stack; |
1108 | }; |
1109 | |
1110 | void analyze_interlocked_resource_usage(); |
1111 | // The set of all resources written while inside the critical section, if present. |
1112 | std::unordered_set<uint32_t> interlocked_resources; |
1113 | bool interlocked_is_complex = false; |
1114 | |
1115 | void make_constant_null(uint32_t id, uint32_t type); |
1116 | |
1117 | std::unordered_map<uint32_t, std::string> declared_block_names; |
1118 | |
1119 | bool instruction_to_result_type(uint32_t &result_type, uint32_t &result_id, spv::Op op, const uint32_t *args, |
1120 | uint32_t length); |
1121 | |
1122 | Bitset combined_decoration_for_member(const SPIRType &type, uint32_t index) const; |
1123 | static bool is_desktop_only_format(spv::ImageFormat format); |
1124 | |
1125 | bool is_depth_image(const SPIRType &type, uint32_t id) const; |
1126 | |
1127 | void set_extended_decoration(uint32_t id, ExtendedDecorations decoration, uint32_t value = 0); |
1128 | uint32_t get_extended_decoration(uint32_t id, ExtendedDecorations decoration) const; |
1129 | bool has_extended_decoration(uint32_t id, ExtendedDecorations decoration) const; |
1130 | void unset_extended_decoration(uint32_t id, ExtendedDecorations decoration); |
1131 | |
1132 | void set_extended_member_decoration(uint32_t type, uint32_t index, ExtendedDecorations decoration, |
1133 | uint32_t value = 0); |
1134 | uint32_t get_extended_member_decoration(uint32_t type, uint32_t index, ExtendedDecorations decoration) const; |
1135 | bool has_extended_member_decoration(uint32_t type, uint32_t index, ExtendedDecorations decoration) const; |
1136 | void unset_extended_member_decoration(uint32_t type, uint32_t index, ExtendedDecorations decoration); |
1137 | |
1138 | bool type_is_array_of_pointers(const SPIRType &type) const; |
1139 | bool type_is_top_level_physical_pointer(const SPIRType &type) const; |
1140 | bool type_is_block_like(const SPIRType &type) const; |
1141 | bool type_is_opaque_value(const SPIRType &type) const; |
1142 | |
1143 | bool reflection_ssbo_instance_name_is_significant() const; |
1144 | std::string get_remapped_declared_block_name(uint32_t id, bool fallback_prefer_instance_name) const; |
1145 | |
1146 | bool flush_phi_required(BlockID from, BlockID to) const; |
1147 | |
1148 | uint32_t evaluate_spec_constant_u32(const SPIRConstantOp &spec) const; |
1149 | uint32_t evaluate_constant_u32(uint32_t id) const; |
1150 | |
1151 | bool is_vertex_like_shader() const; |
1152 | |
1153 | // Get the correct case list for the OpSwitch, since it can be either a |
1154 | // 32 bit wide condition or a 64 bit, but the type is not embedded in the |
1155 | // instruction itself. |
1156 | const SmallVector<SPIRBlock::Case> &get_case_list(const SPIRBlock &block) const; |
1157 | |
1158 | private: |
1159 | // Used only to implement the old deprecated get_entry_point() interface. |
1160 | const SPIREntryPoint &get_first_entry_point(const std::string &name) const; |
1161 | SPIREntryPoint &get_first_entry_point(const std::string &name); |
1162 | }; |
1163 | } // namespace SPIRV_CROSS_NAMESPACE |
1164 | |
1165 | #endif |
1166 | |