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 bool operator!=(InsnIterator const &other) const
77 return iter != other.iter;
80 InsnIterator operator*() const
85 InsnIterator &operator++()
91 InsnIterator const operator++(int)
93 InsnIterator ret{*this};
98 InsnIterator(InsnIterator const &other) = default;
100 InsnIterator() = default;
102 explicit InsnIterator(InsnStore::const_iterator iter) : iter{iter}
107 /* range-based-for interface */
108 InsnIterator begin() const
110 return InsnIterator{insns.cbegin() + 5};
113 InsnIterator end() const
115 return InsnIterator{insns.cend()};
121 InsnIterator definition;
122 spv::StorageClass storageClass;
123 uint32_t sizeInComponents = 0;
124 bool isBuiltInBlock = false;
125 uint32_t pointerBase = 0;
129 Unknown, /* for paranoia -- if we get left with an object in this state, the module was broken */
135 } kind = Kind::Unknown;
138 int getSerialID() const
143 explicit SpirvShader(InsnStore const &insns);
147 bool EarlyFragmentTests : 1;
148 bool DepthReplacing : 1;
149 bool DepthGreater : 1;
151 bool DepthUnchanged : 1;
152 bool ContainsKill : 1;
153 bool NeedsCentroid : 1;
155 // Compute workgroup dimensions
156 int LocalSizeX, LocalSizeY, LocalSizeZ;
159 Modes const &getModes() const
164 enum AttribType : unsigned char
171 ATTRIBTYPE_LAST = ATTRIBTYPE_UINT
174 bool hasBuiltinInput(spv::BuiltIn b) const
176 return inputBuiltins.find(b) != inputBuiltins.end();
183 spv::BuiltIn BuiltIn;
184 bool HasLocation : 1;
185 bool HasComponent : 1;
189 bool NoPerspective : 1;
191 bool BufferBlock : 1;
194 : Location{-1}, Component{0}, BuiltIn{}, HasLocation{false}, HasComponent{false}, HasBuiltIn{false},
196 Centroid{false}, NoPerspective{false}, Block{false},
201 Decorations(Decorations const &) = default;
203 void Apply(Decorations const &src);
205 void Apply(spv::Decoration decoration, uint32_t arg);
208 std::unordered_map<uint32_t, Decorations> decorations;
209 std::unordered_map<uint32_t, std::vector<Decorations>> memberDecorations;
211 struct InterfaceComponent
216 bool NoPerspective : 1;
219 : Type{ATTRIBTYPE_UNUSED}, Flat{false}, Centroid{false}, NoPerspective{false}
224 struct BuiltinMapping
227 uint32_t FirstComponent;
228 uint32_t SizeInComponents;
231 std::vector<InterfaceComponent> inputs;
232 std::vector<InterfaceComponent> outputs;
234 void emitEarly(SpirvRoutine *routine) const;
236 void emit(SpirvRoutine *routine) const;
238 using BuiltInHash = std::hash<std::underlying_type<spv::BuiltIn>::type>;
239 std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> inputBuiltins;
240 std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> outputBuiltins;
242 Object const &getType(uint32_t id) const
244 auto it = types.find(id);
245 assert(it != types.end());
249 Object const &getObject(uint32_t id) const
251 auto it = defs.find(id);
252 assert(it != defs.end());
258 static volatile int serialCounter;
260 std::unordered_map<uint32_t, Object> types;
261 std::unordered_map<uint32_t, Object> defs;
263 void ProcessExecutionMode(InsnIterator it);
265 uint32_t ComputeTypeSize(InsnIterator insn);
266 void ApplyDecorationsForId(Decorations *d, uint32_t id) const;
267 void ApplyDecorationsForIdMember(Decorations *d, uint32_t id, uint32_t member) const;
270 int VisitInterfaceInner(uint32_t id, Decorations d, F f) const;
273 void VisitInterface(uint32_t id, F f) const;
275 uint32_t GetConstantInt(uint32_t id) const;
277 void ProcessInterfaceVariable(Object &object);
281 #endif // sw_SpirvShader_hpp