OSDN Git Service

Add SpirvRoutine::getValue helper
[android-x86/external-swiftshader.git] / src / Pipeline / SpirvShader.hpp
1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
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 #ifndef sw_SpirvShader_hpp
16 #define sw_SpirvShader_hpp
17
18 #include "System/Types.hpp"
19 #include "Vulkan/VkDebug.hpp"
20 #include "ShaderCore.hpp"
21
22 #include <string>
23 #include <vector>
24 #include <unordered_map>
25 #include <cstdint>
26 #include <type_traits>
27 #include <memory>
28 #include <spirv/unified1/spirv.hpp>
29 #include <Device/Config.hpp>
30
31 namespace sw
32 {
33         class SpirvRoutine
34         {
35         public:
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));
40
41                 void createLvalue(uint32_t id, uint32_t size)
42                 {
43                         lvalues.emplace(id, std::unique_ptr<Value>(new Value(size)));
44                 }
45
46                 Value& getValue(uint32_t id)
47                 {
48                         auto it = lvalues.find(id);
49                         assert(it != lvalues.end());
50                         return *it->second;
51                 }
52         };
53
54         class SpirvShader
55         {
56         public:
57                 using InsnStore = std::vector<uint32_t>;
58                 InsnStore insns;
59
60                 /* Pseudo-iterator over SPIRV instructions, designed to support range-based-for. */
61                 class InsnIterator
62                 {
63                         InsnStore::const_iterator iter;
64
65                 public:
66                         spv::Op opcode() const
67                         {
68                                 return static_cast<spv::Op>(*iter & spv::OpCodeMask);
69                         }
70
71                         uint32_t wordCount() const
72                         {
73                                 return *iter >> spv::WordCountShift;
74                         }
75
76                         uint32_t word(uint32_t n) const
77                         {
78                                 ASSERT(n < wordCount());
79                                 return iter[n];
80                         }
81
82                         uint32_t const * wordPointer(uint32_t n) const
83                         {
84                                 ASSERT(n < wordCount());
85                                 return &iter[n];
86                         }
87
88                         bool operator!=(InsnIterator const &other) const
89                         {
90                                 return iter != other.iter;
91                         }
92
93                         InsnIterator operator*() const
94                         {
95                                 return *this;
96                         }
97
98                         InsnIterator &operator++()
99                         {
100                                 iter += wordCount();
101                                 return *this;
102                         }
103
104                         InsnIterator const operator++(int)
105                         {
106                                 InsnIterator ret{*this};
107                                 iter += wordCount();
108                                 return ret;
109                         }
110
111                         InsnIterator(InsnIterator const &other) = default;
112
113                         InsnIterator() = default;
114
115                         explicit InsnIterator(InsnStore::const_iterator iter) : iter{iter}
116                         {
117                         }
118                 };
119
120                 /* range-based-for interface */
121                 InsnIterator begin() const
122                 {
123                         return InsnIterator{insns.cbegin() + 5};
124                 }
125
126                 InsnIterator end() const
127                 {
128                         return InsnIterator{insns.cend()};
129                 }
130
131                 class Object
132                 {
133                 public:
134                         InsnIterator definition;
135                         spv::StorageClass storageClass;
136                         uint32_t sizeInComponents = 0;
137                         bool isBuiltInBlock = false;
138                         uint32_t pointerBase = 0;
139
140                         enum class Kind
141                         {
142                                 Unknown,        /* for paranoia -- if we get left with an object in this state, the module was broken */
143                                 Type,
144                                 Variable,
145                                 InterfaceVariable,
146                                 Constant,
147                                 Value,
148                         } kind = Kind::Unknown;
149                 };
150
151                 int getSerialID() const
152                 {
153                         return serialID;
154                 }
155
156                 explicit SpirvShader(InsnStore const &insns);
157
158                 struct Modes
159                 {
160                         bool EarlyFragmentTests : 1;
161                         bool DepthReplacing : 1;
162                         bool DepthGreater : 1;
163                         bool DepthLess : 1;
164                         bool DepthUnchanged : 1;
165                         bool ContainsKill : 1;
166                         bool NeedsCentroid : 1;
167
168                         // Compute workgroup dimensions
169                         int LocalSizeX, LocalSizeY, LocalSizeZ;
170                 };
171
172                 Modes const &getModes() const
173                 {
174                         return modes;
175                 }
176
177                 enum AttribType : unsigned char
178                 {
179                         ATTRIBTYPE_FLOAT,
180                         ATTRIBTYPE_INT,
181                         ATTRIBTYPE_UINT,
182                         ATTRIBTYPE_UNUSED,
183
184                         ATTRIBTYPE_LAST = ATTRIBTYPE_UINT
185                 };
186
187                 bool hasBuiltinInput(spv::BuiltIn b) const
188                 {
189                         return inputBuiltins.find(b) != inputBuiltins.end();
190                 }
191
192                 struct Decorations
193                 {
194                         int32_t Location;
195                         int32_t Component;
196                         spv::BuiltIn BuiltIn;
197                         bool HasLocation : 1;
198                         bool HasComponent : 1;
199                         bool HasBuiltIn : 1;
200                         bool Flat : 1;
201                         bool Centroid : 1;
202                         bool NoPerspective : 1;
203                         bool Block : 1;
204                         bool BufferBlock : 1;
205
206                         Decorations()
207                                         : Location{-1}, Component{0}, BuiltIn{}, HasLocation{false}, HasComponent{false}, HasBuiltIn{false},
208                                           Flat{false},
209                                           Centroid{false}, NoPerspective{false}, Block{false},
210                                           BufferBlock{false}
211                         {
212                         }
213
214                         Decorations(Decorations const &) = default;
215
216                         void Apply(Decorations const &src);
217
218                         void Apply(spv::Decoration decoration, uint32_t arg);
219                 };
220
221                 std::unordered_map<uint32_t, Decorations> decorations;
222                 std::unordered_map<uint32_t, std::vector<Decorations>> memberDecorations;
223
224                 struct InterfaceComponent
225                 {
226                         AttribType Type;
227                         bool Flat : 1;
228                         bool Centroid : 1;
229                         bool NoPerspective : 1;
230
231                         InterfaceComponent()
232                                         : Type{ATTRIBTYPE_UNUSED}, Flat{false}, Centroid{false}, NoPerspective{false}
233                         {
234                         }
235                 };
236
237                 struct BuiltinMapping
238                 {
239                         uint32_t Id;
240                         uint32_t FirstComponent;
241                         uint32_t SizeInComponents;
242                 };
243
244                 std::vector<InterfaceComponent> inputs;
245                 std::vector<InterfaceComponent> outputs;
246
247                 void emitEarly(SpirvRoutine *routine) const;
248
249                 void emit(SpirvRoutine *routine) const;
250
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;
254
255                 Object const &getType(uint32_t id) const
256                 {
257                         auto it = types.find(id);
258                         assert(it != types.end());
259                         return it->second;
260                 }
261
262                 Object const &getObject(uint32_t id) const
263                 {
264                         auto it = defs.find(id);
265                         assert(it != defs.end());
266                         return it->second;
267                 }
268
269         private:
270                 const int serialID;
271                 static volatile int serialCounter;
272                 Modes modes;
273                 std::unordered_map<uint32_t, Object> types;
274                 std::unordered_map<uint32_t, Object> defs;
275
276                 void ProcessExecutionMode(InsnIterator it);
277
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;
281
282                 template<typename F>
283                 int VisitInterfaceInner(uint32_t id, Decorations d, F f) const;
284
285                 template<typename F>
286                 void VisitInterface(uint32_t id, F f) const;
287
288                 uint32_t GetConstantInt(uint32_t id) const;
289
290                 void ProcessInterfaceVariable(Object &object);
291
292                 Int4 WalkAccessChain(uint32_t id, uint32_t numIndexes, uint32_t const *indexIds, SpirvRoutine *routine) const;
293         };
294 }
295
296 #endif  // sw_SpirvShader_hpp