OSDN Git Service

Update to latest LLVM 7.0 release branch revision.
[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
21 #include <string>
22 #include <vector>
23 #include <unordered_map>
24 #include <cstdint>
25 #include <type_traits>
26 #include <spirv/unified1/spirv.hpp>
27
28 namespace sw
29 {
30         class SpirvShader
31         {
32         public:
33                 using InsnStore = std::vector<uint32_t>;
34                 InsnStore insns;
35
36                 /* Pseudo-iterator over SPIRV instructions, designed to support range-based-for. */
37                 class InsnIterator
38                 {
39                         InsnStore::const_iterator iter;
40
41                 public:
42                         spv::Op opcode() const
43                         {
44                                 return static_cast<spv::Op>(*iter & spv::OpCodeMask);
45                         }
46
47                         uint32_t wordCount() const
48                         {
49                                 return *iter >> spv::WordCountShift;
50                         }
51
52                         uint32_t word(uint32_t n) const
53                         {
54                                 ASSERT(n < wordCount());
55                                 return iter[n];
56                         }
57
58                         bool operator!=(InsnIterator const &other) const
59                         {
60                                 return iter != other.iter;
61                         }
62
63                         InsnIterator operator*() const
64                         {
65                                 return *this;
66                         }
67
68                         InsnIterator &operator++()
69                         {
70                                 iter += wordCount();
71                                 return *this;
72                         }
73
74                         InsnIterator const operator++(int)
75                         {
76                                 InsnIterator ret{*this};
77                                 iter += wordCount();
78                                 return ret;
79                         }
80
81                         InsnIterator(InsnIterator const &other) = default;
82
83                         InsnIterator() = default;
84
85                         explicit InsnIterator(InsnStore::const_iterator iter) : iter{iter}
86                         {
87                         }
88                 };
89
90                 /* range-based-for interface */
91                 InsnIterator begin() const
92                 {
93                         return InsnIterator{insns.cbegin() + 5};
94                 }
95
96                 InsnIterator end() const
97                 {
98                         return InsnIterator{insns.cend()};
99                 }
100
101                 class Object
102                 {
103                 public:
104                         InsnIterator definition;
105                         spv::StorageClass storageClass;
106                         uint32_t sizeInComponents = 0;
107                         bool isBuiltInBlock = false;
108
109                         enum class Kind
110                         {
111                                 Unknown,        /* for paranoia -- if we get left with an object in this state, the module was broken */
112                                 Type,
113                                 Variable,
114                                 Constant,
115                                 Value,
116                         } kind = Kind::Unknown;
117                 };
118
119                 int getSerialID() const
120                 {
121                         return serialID;
122                 }
123
124                 explicit SpirvShader(InsnStore const &insns);
125
126                 struct Modes
127                 {
128                         bool EarlyFragmentTests : 1;
129                         bool DepthReplacing : 1;
130                         bool DepthGreater : 1;
131                         bool DepthLess : 1;
132                         bool DepthUnchanged : 1;
133                         bool ContainsKill : 1;
134
135                         // Compute workgroup dimensions
136                         int LocalSizeX, LocalSizeY, LocalSizeZ;
137                 };
138
139                 Modes const &getModes() const
140                 {
141                         return modes;
142                 }
143
144                 enum AttribType : unsigned char
145                 {
146                         ATTRIBTYPE_FLOAT,
147                         ATTRIBTYPE_INT,
148                         ATTRIBTYPE_UINT,
149                         ATTRIBTYPE_UNUSED,
150
151                         ATTRIBTYPE_LAST = ATTRIBTYPE_UINT
152                 };
153
154                 bool hasBuiltinInput(spv::BuiltIn b) const
155                 {
156                         return inputBuiltins.find(b) != inputBuiltins.end();
157                 }
158
159                 struct Decorations
160                 {
161                         int32_t Location;
162                         int32_t Component;
163                         spv::BuiltIn BuiltIn;
164                         bool HasLocation : 1;
165                         bool HasComponent : 1;
166                         bool HasBuiltIn : 1;
167                         bool Flat : 1;
168                         bool Centroid : 1;
169                         bool NoPerspective : 1;
170                         bool Block : 1;
171                         bool BufferBlock : 1;
172
173                         Decorations()
174                                         : Location{-1}, Component{0}, BuiltIn{}, HasLocation{false}, HasComponent{false}, HasBuiltIn{false},
175                                           Flat{false},
176                                           Centroid{false}, NoPerspective{false}, Block{false},
177                                           BufferBlock{false}
178                         {
179                         }
180
181                         Decorations(Decorations const &) = default;
182
183                         void Apply(Decorations const &src);
184
185                         void Apply(spv::Decoration decoration, uint32_t arg);
186                 };
187
188                 std::unordered_map<uint32_t, Decorations> decorations;
189                 std::unordered_map<uint32_t, std::vector<Decorations>> memberDecorations;
190
191                 struct InterfaceComponent
192                 {
193                         AttribType Type;
194                         bool Flat : 1;
195                         bool Centroid : 1;
196                         bool NoPerspective : 1;
197
198                         InterfaceComponent()
199                                         : Type{ATTRIBTYPE_UNUSED}, Flat{false}, Centroid{false}, NoPerspective{false}
200                         {
201                         }
202                 };
203
204                 struct BuiltinMapping
205                 {
206                         uint32_t Id;
207                         uint32_t FirstComponent;
208                         uint32_t SizeInComponents;
209                 };
210
211                 std::vector<InterfaceComponent> inputs;
212                 std::vector<InterfaceComponent> outputs;
213
214         private:
215                 const int serialID;
216                 static volatile int serialCounter;
217                 Modes modes;
218                 std::unordered_map<uint32_t, Object> types;
219                 std::unordered_map<uint32_t, Object> defs;
220
221                 using BuiltInHash = std::hash<std::underlying_type<spv::BuiltIn>::type>;
222                 std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> inputBuiltins;
223                 std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> outputBuiltins;
224
225                 Object const &getType(uint32_t id) const
226                 {
227                         auto it = types.find(id);
228                         assert(it != types.end());
229                         return it->second;
230                 }
231
232                 void ProcessExecutionMode(InsnIterator it);
233
234                 uint32_t ComputeTypeSize(InsnIterator insn);
235
236                 void PopulateInterfaceSlot(std::vector<InterfaceComponent> *iface, Decorations const &d, AttribType type);
237
238                 int PopulateInterfaceInner(std::vector<InterfaceComponent> *iface, uint32_t id, Decorations d);
239
240                 void PopulateInterface(std::vector<InterfaceComponent> *iface, uint32_t id);
241
242                 uint32_t GetConstantInt(uint32_t id);
243
244                 void ProcessInterfaceVariable(Object const &object);
245         };
246 }
247
248 #endif  // sw_SpirvShader_hpp