1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef sw_SpirvShader_hpp
16 #define sw_SpirvShader_hpp
18 #include "System/Types.hpp"
19 #include "Vulkan/VkDebug.hpp"
20 #include "ShaderCore.hpp"
24 #include <unordered_map>
26 #include <type_traits>
28 #include <spirv/unified1/spirv.hpp>
29 #include <Device/Config.hpp>
36 using Value = Array<Float4>;
37 std::unordered_map<uint32_t, std::unique_ptr<Value>> lvalues;
38 std::unique_ptr<Value> inputs = std::unique_ptr<Value>(new Value(MAX_INTERFACE_COMPONENTS));
39 std::unique_ptr<Value> outputs = std::unique_ptr<Value>(new Value(MAX_INTERFACE_COMPONENTS));
41 void createLvalue(uint32_t id, uint32_t size)
43 lvalues.emplace(id, std::unique_ptr<Value>(new Value(size)));
46 Value& getValue(uint32_t id)
48 auto it = lvalues.find(id);
49 assert(it != lvalues.end());
57 using InsnStore = std::vector<uint32_t>;
60 /* Pseudo-iterator over SPIRV instructions, designed to support range-based-for. */
63 InsnStore::const_iterator iter;
66 spv::Op opcode() const
68 return static_cast<spv::Op>(*iter & spv::OpCodeMask);
71 uint32_t wordCount() const
73 return *iter >> spv::WordCountShift;
76 uint32_t word(uint32_t n) const
78 ASSERT(n < wordCount());
82 uint32_t const * wordPointer(uint32_t n) const
84 ASSERT(n < wordCount());
88 bool operator!=(InsnIterator const &other) const
90 return iter != other.iter;
93 InsnIterator operator*() const
98 InsnIterator &operator++()
104 InsnIterator const operator++(int)
106 InsnIterator ret{*this};
111 InsnIterator(InsnIterator const &other) = default;
113 InsnIterator() = default;
115 explicit InsnIterator(InsnStore::const_iterator iter) : iter{iter}
120 /* range-based-for interface */
121 InsnIterator begin() const
123 return InsnIterator{insns.cbegin() + 5};
126 InsnIterator end() const
128 return InsnIterator{insns.cend()};
134 InsnIterator definition;
135 spv::StorageClass storageClass;
136 uint32_t sizeInComponents = 0;
137 bool isBuiltInBlock = false;
138 uint32_t pointerBase = 0;
142 Unknown, /* for paranoia -- if we get left with an object in this state, the module was broken */
148 } kind = Kind::Unknown;
151 int getSerialID() const
156 explicit SpirvShader(InsnStore const &insns);
160 bool EarlyFragmentTests : 1;
161 bool DepthReplacing : 1;
162 bool DepthGreater : 1;
164 bool DepthUnchanged : 1;
165 bool ContainsKill : 1;
166 bool NeedsCentroid : 1;
168 // Compute workgroup dimensions
169 int LocalSizeX, LocalSizeY, LocalSizeZ;
172 Modes const &getModes() const
177 enum AttribType : unsigned char
184 ATTRIBTYPE_LAST = ATTRIBTYPE_UINT
187 bool hasBuiltinInput(spv::BuiltIn b) const
189 return inputBuiltins.find(b) != inputBuiltins.end();
196 spv::BuiltIn BuiltIn;
197 bool HasLocation : 1;
198 bool HasComponent : 1;
202 bool NoPerspective : 1;
204 bool BufferBlock : 1;
207 : Location{-1}, Component{0}, BuiltIn{}, HasLocation{false}, HasComponent{false}, HasBuiltIn{false},
209 Centroid{false}, NoPerspective{false}, Block{false},
214 Decorations(Decorations const &) = default;
216 void Apply(Decorations const &src);
218 void Apply(spv::Decoration decoration, uint32_t arg);
221 std::unordered_map<uint32_t, Decorations> decorations;
222 std::unordered_map<uint32_t, std::vector<Decorations>> memberDecorations;
224 struct InterfaceComponent
229 bool NoPerspective : 1;
232 : Type{ATTRIBTYPE_UNUSED}, Flat{false}, Centroid{false}, NoPerspective{false}
237 struct BuiltinMapping
240 uint32_t FirstComponent;
241 uint32_t SizeInComponents;
244 std::vector<InterfaceComponent> inputs;
245 std::vector<InterfaceComponent> outputs;
247 void emitEarly(SpirvRoutine *routine) const;
249 void emit(SpirvRoutine *routine) const;
251 using BuiltInHash = std::hash<std::underlying_type<spv::BuiltIn>::type>;
252 std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> inputBuiltins;
253 std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> outputBuiltins;
255 Object const &getType(uint32_t id) const
257 auto it = types.find(id);
258 assert(it != types.end());
262 Object const &getObject(uint32_t id) const
264 auto it = defs.find(id);
265 assert(it != defs.end());
271 static volatile int serialCounter;
273 std::unordered_map<uint32_t, Object> types;
274 std::unordered_map<uint32_t, Object> defs;
276 void ProcessExecutionMode(InsnIterator it);
278 uint32_t ComputeTypeSize(InsnIterator insn);
279 void ApplyDecorationsForId(Decorations *d, uint32_t id) const;
280 void ApplyDecorationsForIdMember(Decorations *d, uint32_t id, uint32_t member) const;
283 int VisitInterfaceInner(uint32_t id, Decorations d, F f) const;
286 void VisitInterface(uint32_t id, F f) const;
288 uint32_t GetConstantInt(uint32_t id) const;
290 void ProcessInterfaceVariable(Object &object);
292 Int4 WalkAccessChain(uint32_t id, uint32_t numIndexes, uint32_t const *indexIds, SpirvRoutine *routine) const;
296 #endif // sw_SpirvShader_hpp