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)));
50 using InsnStore = std::vector<uint32_t>;
53 /* Pseudo-iterator over SPIRV instructions, designed to support range-based-for. */
56 InsnStore::const_iterator iter;
59 spv::Op opcode() const
61 return static_cast<spv::Op>(*iter & spv::OpCodeMask);
64 uint32_t wordCount() const
66 return *iter >> spv::WordCountShift;
69 uint32_t word(uint32_t n) const
71 ASSERT(n < wordCount());
75 uint32_t const * wordPointer(uint32_t n) const
77 ASSERT(n < wordCount());
81 bool operator!=(InsnIterator const &other) const
83 return iter != other.iter;
86 InsnIterator operator*() const
91 InsnIterator &operator++()
97 InsnIterator const operator++(int)
99 InsnIterator ret{*this};
104 InsnIterator(InsnIterator const &other) = default;
106 InsnIterator() = default;
108 explicit InsnIterator(InsnStore::const_iterator iter) : iter{iter}
113 /* range-based-for interface */
114 InsnIterator begin() const
116 return InsnIterator{insns.cbegin() + 5};
119 InsnIterator end() const
121 return InsnIterator{insns.cend()};
127 InsnIterator definition;
128 spv::StorageClass storageClass;
129 uint32_t sizeInComponents = 0;
130 bool isBuiltInBlock = false;
131 uint32_t pointerBase = 0;
135 Unknown, /* for paranoia -- if we get left with an object in this state, the module was broken */
141 } kind = Kind::Unknown;
144 int getSerialID() const
149 explicit SpirvShader(InsnStore const &insns);
153 bool EarlyFragmentTests : 1;
154 bool DepthReplacing : 1;
155 bool DepthGreater : 1;
157 bool DepthUnchanged : 1;
158 bool ContainsKill : 1;
159 bool NeedsCentroid : 1;
161 // Compute workgroup dimensions
162 int LocalSizeX, LocalSizeY, LocalSizeZ;
165 Modes const &getModes() const
170 enum AttribType : unsigned char
177 ATTRIBTYPE_LAST = ATTRIBTYPE_UINT
180 bool hasBuiltinInput(spv::BuiltIn b) const
182 return inputBuiltins.find(b) != inputBuiltins.end();
189 spv::BuiltIn BuiltIn;
190 bool HasLocation : 1;
191 bool HasComponent : 1;
195 bool NoPerspective : 1;
197 bool BufferBlock : 1;
200 : Location{-1}, Component{0}, BuiltIn{}, HasLocation{false}, HasComponent{false}, HasBuiltIn{false},
202 Centroid{false}, NoPerspective{false}, Block{false},
207 Decorations(Decorations const &) = default;
209 void Apply(Decorations const &src);
211 void Apply(spv::Decoration decoration, uint32_t arg);
214 std::unordered_map<uint32_t, Decorations> decorations;
215 std::unordered_map<uint32_t, std::vector<Decorations>> memberDecorations;
217 struct InterfaceComponent
222 bool NoPerspective : 1;
225 : Type{ATTRIBTYPE_UNUSED}, Flat{false}, Centroid{false}, NoPerspective{false}
230 struct BuiltinMapping
233 uint32_t FirstComponent;
234 uint32_t SizeInComponents;
237 std::vector<InterfaceComponent> inputs;
238 std::vector<InterfaceComponent> outputs;
240 void emitEarly(SpirvRoutine *routine) const;
242 void emit(SpirvRoutine *routine) const;
244 using BuiltInHash = std::hash<std::underlying_type<spv::BuiltIn>::type>;
245 std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> inputBuiltins;
246 std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> outputBuiltins;
248 Object const &getType(uint32_t id) const
250 auto it = types.find(id);
251 assert(it != types.end());
255 Object const &getObject(uint32_t id) const
257 auto it = defs.find(id);
258 assert(it != defs.end());
264 static volatile int serialCounter;
266 std::unordered_map<uint32_t, Object> types;
267 std::unordered_map<uint32_t, Object> defs;
269 void ProcessExecutionMode(InsnIterator it);
271 uint32_t ComputeTypeSize(InsnIterator insn);
272 void ApplyDecorationsForId(Decorations *d, uint32_t id) const;
273 void ApplyDecorationsForIdMember(Decorations *d, uint32_t id, uint32_t member) const;
276 int VisitInterfaceInner(uint32_t id, Decorations d, F f) const;
279 void VisitInterface(uint32_t id, F f) const;
281 uint32_t GetConstantInt(uint32_t id) const;
283 void ProcessInterfaceVariable(Object &object);
285 Int4 WalkAccessChain(uint32_t id, uint32_t numIndexes, uint32_t const *indexIds, SpirvRoutine *routine) const;
289 #endif // sw_SpirvShader_hpp