OSDN Git Service

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