OSDN Git Service

34c79518ff106dcf790dc7052ee210d404ed45d0
[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 "ShaderCore.hpp"
19 #include "SpirvID.hpp"
20 #include "System/Types.hpp"
21 #include "Vulkan/VkDebug.hpp"
22 #include "Vulkan/VkConfig.h"
23
24 #include <array>
25 #include <cstring>
26 #include <string>
27 #include <vector>
28 #include <unordered_map>
29 #include <cstdint>
30 #include <type_traits>
31 #include <memory>
32 #include <spirv/unified1/spirv.hpp>
33 #include <Device/Config.hpp>
34
35 namespace vk
36 {
37         class PipelineLayout;
38 } // namespace vk
39
40 namespace sw
41 {
42         // Forward declarations.
43         class SpirvRoutine;
44         class GenericValue;
45
46         // SIMD contains types that represent multiple scalars packed into a single
47         // vector data type. Types in the SIMD namespace provide a semantic hint
48         // that the data should be treated as a per-execution-lane scalar instead of
49         // a typical euclidean-style vector type.
50         namespace SIMD
51         {
52                 // Width is the number of per-lane scalars packed into each SIMD vector.
53                 static constexpr int Width = 4;
54
55                 using Float = rr::Float4;
56                 using Int = rr::Int4;
57                 using UInt = rr::UInt4;
58         }
59
60         // Incrementally constructed complex bundle of rvalues
61         // Effectively a restricted vector, supporting only:
62         // - allocation to a (runtime-known) fixed size
63         // - in-place construction of elements
64         // - const operator[]
65         class Intermediate
66         {
67         public:
68                 using Scalar = RValue<SIMD::Float>;
69
70                 Intermediate(uint32_t size) : contents(new ContentsType[size]), size(size) {
71 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
72                         memset(contents, 0, sizeof(ContentsType) * size);
73 #endif
74                 }
75
76                 ~Intermediate()
77                 {
78                         for (auto i = 0u; i < size; i++)
79                                 reinterpret_cast<Scalar *>(&contents[i])->~Scalar();
80                         delete [] contents;
81                 }
82
83                 void emplace(uint32_t n, Scalar&& value)
84                 {
85                         ASSERT(n < size);
86                         ASSERT(reinterpret_cast<Scalar const *>(&contents[n])->value == nullptr);
87                         new (&contents[n]) Scalar(value);
88                 }
89
90                 void emplace(uint32_t n, const Scalar& value)
91                 {
92                         ASSERT(n < size);
93                         ASSERT(reinterpret_cast<Scalar const *>(&contents[n])->value == nullptr);
94                         new (&contents[n]) Scalar(value);
95                 }
96
97                 // Emplace with cast helpers.
98                 void emplace(uint32_t n, const RValue<SIMD::Int>& value) { emplace(n, As<SIMD::Float>(value)); }
99                 void emplace(uint32_t n, const RValue<SIMD::UInt>& value) { emplace(n, As<SIMD::Float>(value)); }
100
101                 // Value retrieval functions.
102                 RValue<SIMD::Float> Float(uint32_t i) const
103                 {
104                         ASSERT(i < size);
105                         auto scalar = reinterpret_cast<Scalar const *>(&contents[i]);
106                         ASSERT(scalar->value != nullptr);
107                         return *scalar;
108                 }
109                 RValue<SIMD::Int> Int(uint32_t i) const { return As<SIMD::Int>(Float(i)); }
110                 RValue<SIMD::UInt> UInt(uint32_t i) const { return As<SIMD::UInt>(Float(i)); }
111
112                 // No copy/move construction or assignment
113                 Intermediate(Intermediate const &) = delete;
114                 Intermediate(Intermediate &&) = delete;
115                 Intermediate & operator=(Intermediate const &) = delete;
116                 Intermediate & operator=(Intermediate &&) = delete;
117
118         private:
119                 using ContentsType = std::aligned_storage<sizeof(Scalar), alignof(Scalar)>::type;
120
121                 ContentsType *contents;
122                 uint32_t size;
123         };
124
125         class SpirvShader
126         {
127         public:
128                 using InsnStore = std::vector<uint32_t>;
129                 InsnStore insns;
130
131                 /* Pseudo-iterator over SPIRV instructions, designed to support range-based-for. */
132                 class InsnIterator
133                 {
134                         InsnStore::const_iterator iter;
135
136                 public:
137                         spv::Op opcode() const
138                         {
139                                 return static_cast<spv::Op>(*iter & spv::OpCodeMask);
140                         }
141
142                         uint32_t wordCount() const
143                         {
144                                 return *iter >> spv::WordCountShift;
145                         }
146
147                         uint32_t word(uint32_t n) const
148                         {
149                                 ASSERT(n < wordCount());
150                                 return iter[n];
151                         }
152
153                         uint32_t const * wordPointer(uint32_t n) const
154                         {
155                                 ASSERT(n < wordCount());
156                                 return &iter[n];
157                         }
158
159                         bool operator!=(InsnIterator const &other) const
160                         {
161                                 return iter != other.iter;
162                         }
163
164                         InsnIterator operator*() const
165                         {
166                                 return *this;
167                         }
168
169                         InsnIterator &operator++()
170                         {
171                                 iter += wordCount();
172                                 return *this;
173                         }
174
175                         InsnIterator const operator++(int)
176                         {
177                                 InsnIterator ret{*this};
178                                 iter += wordCount();
179                                 return ret;
180                         }
181
182                         InsnIterator(InsnIterator const &other) = default;
183
184                         InsnIterator() = default;
185
186                         explicit InsnIterator(InsnStore::const_iterator iter) : iter{iter}
187                         {
188                         }
189                 };
190
191                 /* range-based-for interface */
192                 InsnIterator begin() const
193                 {
194                         return InsnIterator{insns.cbegin() + 5};
195                 }
196
197                 InsnIterator end() const
198                 {
199                         return InsnIterator{insns.cend()};
200                 }
201
202                 class Type
203                 {
204                 public:
205                         using ID = SpirvID<Type>;
206
207                         InsnIterator definition;
208                         spv::StorageClass storageClass = static_cast<spv::StorageClass>(-1);
209                         uint32_t sizeInComponents = 0;
210                         bool isBuiltInBlock = false;
211
212                         // Inner element type for pointers, arrays, vectors and matrices.
213                         ID element;
214                 };
215
216                 class Object
217                 {
218                 public:
219                         using ID = SpirvID<Object>;
220
221                         InsnIterator definition;
222                         Type::ID type;
223                         ID pointerBase;
224                         std::unique_ptr<uint32_t[]> constantValue = nullptr;
225
226                         enum class Kind
227                         {
228                                 Unknown,        /* for paranoia -- if we get left with an object in this state, the module was broken */
229                                 Variable,          // TODO: Document
230                                 InterfaceVariable, // TODO: Document
231                                 Constant,          // Values held by Object::constantValue
232                                 Value,             // Values held by SpirvRoutine::intermediates
233                                 PhysicalPointer,   // Pointer held by SpirvRoutine::physicalPointers
234                         } kind = Kind::Unknown;
235                 };
236
237                 // Block is an interval of SPIR-V instructions, starting with the
238                 // opening OpLabel, and ending with a termination instruction.
239                 class Block
240                 {
241                 public:
242                         using ID = SpirvID<Block>;
243
244                         Block() = default;
245                         Block(const Block& other) = default;
246                         explicit Block(InsnIterator begin, InsnIterator end) : begin_(begin), end_(end) {}
247
248                         /* range-based-for interface */
249                         inline InsnIterator begin() const { return begin_; }
250                         inline InsnIterator end() const { return end_; }
251
252                 private:
253                         InsnIterator begin_;
254                         InsnIterator end_;
255                 };
256
257                 struct TypeOrObject {}; // Dummy struct to represent a Type or Object.
258
259                 // TypeOrObjectID is an identifier that represents a Type or an Object,
260                 // and supports implicit casting to and from Type::ID or Object::ID.
261                 class TypeOrObjectID : public SpirvID<TypeOrObject>
262                 {
263                 public:
264                         using Hash = std::hash<SpirvID<TypeOrObject>>;
265
266                         inline TypeOrObjectID(uint32_t id) : SpirvID(id) {}
267                         inline TypeOrObjectID(Type::ID id) : SpirvID(id.value()) {}
268                         inline TypeOrObjectID(Object::ID id) : SpirvID(id.value()) {}
269                         inline operator Type::ID() const { return Type::ID(value()); }
270                         inline operator Object::ID() const { return Object::ID(value()); }
271                 };
272
273                 int getSerialID() const
274                 {
275                         return serialID;
276                 }
277
278                 explicit SpirvShader(InsnStore const &insns);
279
280                 struct Modes
281                 {
282                         bool EarlyFragmentTests : 1;
283                         bool DepthReplacing : 1;
284                         bool DepthGreater : 1;
285                         bool DepthLess : 1;
286                         bool DepthUnchanged : 1;
287                         bool ContainsKill : 1;
288                         bool NeedsCentroid : 1;
289
290                         // Compute workgroup dimensions
291                         int LocalSizeX, LocalSizeY, LocalSizeZ;
292                 };
293
294                 Modes const &getModes() const
295                 {
296                         return modes;
297                 }
298
299                 enum AttribType : unsigned char
300                 {
301                         ATTRIBTYPE_FLOAT,
302                         ATTRIBTYPE_INT,
303                         ATTRIBTYPE_UINT,
304                         ATTRIBTYPE_UNUSED,
305
306                         ATTRIBTYPE_LAST = ATTRIBTYPE_UINT
307                 };
308
309                 bool hasBuiltinInput(spv::BuiltIn b) const
310                 {
311                         return inputBuiltins.find(b) != inputBuiltins.end();
312                 }
313
314                 struct Decorations
315                 {
316                         int32_t Location;
317                         int32_t Component;
318                         int32_t DescriptorSet;
319                         int32_t Binding;
320                         spv::BuiltIn BuiltIn;
321                         int32_t Offset;
322                         int32_t ArrayStride;
323                         int32_t MatrixStride;
324                         bool HasLocation : 1;
325                         bool HasComponent : 1;
326                         bool HasDescriptorSet : 1;
327                         bool HasBinding : 1;
328                         bool HasBuiltIn : 1;
329                         bool Flat : 1;
330                         bool Centroid : 1;
331                         bool NoPerspective : 1;
332                         bool Block : 1;
333                         bool BufferBlock : 1;
334                         bool HasOffset : 1;
335                         bool HasArrayStride : 1;
336                         bool HasMatrixStride : 1;
337
338                         Decorations()
339                                         : Location{-1}, Component{0}, DescriptorSet{-1}, Binding{-1},
340                                           BuiltIn{static_cast<spv::BuiltIn>(-1)},
341                                           Offset{-1}, ArrayStride{-1}, MatrixStride{-1},
342                                           HasLocation{false}, HasComponent{false},
343                                           HasDescriptorSet{false}, HasBinding{false},
344                                           HasBuiltIn{false}, Flat{false}, Centroid{false},
345                                           NoPerspective{false}, Block{false}, BufferBlock{false},
346                                           HasOffset{false}, HasArrayStride{false}, HasMatrixStride{false}
347                         {
348                         }
349
350                         Decorations(Decorations const &) = default;
351
352                         void Apply(Decorations const &src);
353
354                         void Apply(spv::Decoration decoration, uint32_t arg);
355                 };
356
357                 std::unordered_map<TypeOrObjectID, Decorations, TypeOrObjectID::Hash> decorations;
358                 std::unordered_map<Type::ID, std::vector<Decorations>> memberDecorations;
359
360                 struct InterfaceComponent
361                 {
362                         AttribType Type;
363                         bool Flat : 1;
364                         bool Centroid : 1;
365                         bool NoPerspective : 1;
366
367                         InterfaceComponent()
368                                         : Type{ATTRIBTYPE_UNUSED}, Flat{false}, Centroid{false}, NoPerspective{false}
369                         {
370                         }
371                 };
372
373                 struct BuiltinMapping
374                 {
375                         Object::ID Id;
376                         uint32_t FirstComponent;
377                         uint32_t SizeInComponents;
378                 };
379
380                 std::vector<InterfaceComponent> inputs;
381                 std::vector<InterfaceComponent> outputs;
382
383                 void emitProlog(SpirvRoutine *routine) const;
384                 void emit(SpirvRoutine *routine) const;
385                 void emitEpilog(SpirvRoutine *routine) const;
386
387                 using BuiltInHash = std::hash<std::underlying_type<spv::BuiltIn>::type>;
388                 std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> inputBuiltins;
389                 std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> outputBuiltins;
390
391                 Type const &getType(Type::ID id) const
392                 {
393                         auto it = types.find(id);
394                         ASSERT(it != types.end());
395                         return it->second;
396                 }
397
398                 Object const &getObject(Object::ID id) const
399                 {
400                         auto it = defs.find(id);
401                         ASSERT(it != defs.end());
402                         return it->second;
403                 }
404
405                 Block const &getBlock(Block::ID id) const
406                 {
407                         auto it = blocks.find(id);
408                         ASSERT(it != blocks.end());
409                         return it->second;
410                 }
411
412         private:
413                 const int serialID;
414                 static volatile int serialCounter;
415                 Modes modes;
416                 HandleMap<Type> types;
417                 HandleMap<Object> defs;
418                 HandleMap<Block> blocks;
419                 Block::ID mainBlockId; // Block of the entry point function.
420
421                 void EmitBlock(SpirvRoutine *routine, Block const &block) const;
422                 void EmitInstruction(SpirvRoutine *routine, InsnIterator insn) const;
423
424                 // DeclareType creates a Type for the given OpTypeX instruction, storing
425                 // it into the types map. It is called from the analysis pass (constructor).
426                 void DeclareType(InsnIterator insn);
427
428                 void ProcessExecutionMode(InsnIterator it);
429
430                 uint32_t ComputeTypeSize(InsnIterator insn);
431                 void ApplyDecorationsForId(Decorations *d, TypeOrObjectID id) const;
432                 void ApplyDecorationsForIdMember(Decorations *d, Type::ID id, uint32_t member) const;
433
434                 // Returns true if data in the given storage class is word-interleaved
435                 // by each SIMD vector lane, otherwise data is linerally stored.
436                 //
437                 // A 'lane' is a component of a SIMD vector register.
438                 // Given 4 consecutive loads/stores of 4 SIMD vector registers:
439                 //
440                 // "StorageInterleavedByLane":
441                 //
442                 //  Ptr+0:Reg0.x | Ptr+1:Reg0.y | Ptr+2:Reg0.z | Ptr+3:Reg0.w
443                 // --------------+--------------+--------------+--------------
444                 //  Ptr+4:Reg1.x | Ptr+5:Reg1.y | Ptr+6:Reg1.z | Ptr+7:Reg1.w
445                 // --------------+--------------+--------------+--------------
446                 //  Ptr+8:Reg2.x | Ptr+9:Reg2.y | Ptr+a:Reg2.z | Ptr+b:Reg2.w
447                 // --------------+--------------+--------------+--------------
448                 //  Ptr+c:Reg3.x | Ptr+d:Reg3.y | Ptr+e:Reg3.z | Ptr+f:Reg3.w
449                 //
450                 // Not "StorageInterleavedByLane":
451                 //
452                 //  Ptr+0:Reg0.x | Ptr+0:Reg0.y | Ptr+0:Reg0.z | Ptr+0:Reg0.w
453                 // --------------+--------------+--------------+--------------
454                 //  Ptr+1:Reg1.x | Ptr+1:Reg1.y | Ptr+1:Reg1.z | Ptr+1:Reg1.w
455                 // --------------+--------------+--------------+--------------
456                 //  Ptr+2:Reg2.x | Ptr+2:Reg2.y | Ptr+2:Reg2.z | Ptr+2:Reg2.w
457                 // --------------+--------------+--------------+--------------
458                 //  Ptr+3:Reg3.x | Ptr+3:Reg3.y | Ptr+3:Reg3.z | Ptr+3:Reg3.w
459                 //
460                 static bool IsStorageInterleavedByLane(spv::StorageClass storageClass);
461
462                 template<typename F>
463                 int VisitInterfaceInner(Type::ID id, Decorations d, F f) const;
464
465                 template<typename F>
466                 void VisitInterface(Object::ID id, F f) const;
467
468                 uint32_t GetConstantInt(Object::ID id) const;
469                 Object& CreateConstant(InsnIterator it);
470
471                 void ProcessInterfaceVariable(Object &object);
472
473                 SIMD::Int WalkAccessChain(Object::ID id, uint32_t numIndexes, uint32_t const *indexIds, SpirvRoutine *routine) const;
474                 uint32_t WalkLiteralAccessChain(Type::ID id, uint32_t numIndexes, uint32_t const *indexes) const;
475
476                 // Emit pass instructions:
477                 void EmitVariable(InsnIterator insn, SpirvRoutine *routine) const;
478                 void EmitLoad(InsnIterator insn, SpirvRoutine *routine) const;
479                 void EmitStore(InsnIterator insn, SpirvRoutine *routine) const;
480                 void EmitAccessChain(InsnIterator insn, SpirvRoutine *routine) const;
481                 void EmitCompositeConstruct(InsnIterator insn, SpirvRoutine *routine) const;
482                 void EmitCompositeInsert(InsnIterator insn, SpirvRoutine *routine) const;
483                 void EmitCompositeExtract(InsnIterator insn, SpirvRoutine *routine) const;
484                 void EmitVectorShuffle(InsnIterator insn, SpirvRoutine *routine) const;
485                 void EmitVectorTimesScalar(InsnIterator insn, SpirvRoutine *routine) const;
486                 void EmitUnaryOp(InsnIterator insn, SpirvRoutine *routine) const;
487                 void EmitBinaryOp(InsnIterator insn, SpirvRoutine *routine) const;
488                 void EmitDot(InsnIterator insn, SpirvRoutine *routine) const;
489                 void EmitSelect(InsnIterator insn, SpirvRoutine *routine) const;
490                 void EmitExtendedInstruction(InsnIterator insn, SpirvRoutine *routine) const;
491                 void EmitAny(InsnIterator insn, SpirvRoutine *routine) const;
492                 void EmitAll(InsnIterator insn, SpirvRoutine *routine) const;
493
494                 // OpcodeName returns the name of the opcode op.
495                 // If NDEBUG is defined, then OpcodeName will only return the numerical code.
496                 static std::string OpcodeName(spv::Op op);
497
498                 // Helper as we often need to take dot products as part of doing other things.
499                 SIMD::Float Dot(unsigned numComponents, GenericValue const & x, GenericValue const & y) const;
500         };
501
502         class SpirvRoutine
503         {
504         public:
505                 SpirvRoutine(vk::PipelineLayout const *pipelineLayout);
506
507                 using Value = Array<SIMD::Float>;
508
509                 vk::PipelineLayout const * const pipelineLayout;
510
511                 std::unordered_map<SpirvShader::Object::ID, Value> lvalues;
512
513                 std::unordered_map<SpirvShader::Object::ID, Intermediate> intermediates;
514
515                 std::unordered_map<SpirvShader::Object::ID, Pointer<Byte> > physicalPointers;
516
517                 Value inputs = Value{MAX_INTERFACE_COMPONENTS};
518                 Value outputs = Value{MAX_INTERFACE_COMPONENTS};
519
520                 std::array<Pointer<Byte>, vk::MAX_BOUND_DESCRIPTOR_SETS> descriptorSets;
521
522                 void createLvalue(SpirvShader::Object::ID id, uint32_t size)
523                 {
524                         lvalues.emplace(id, Value(size));
525                 }
526
527                 Intermediate& createIntermediate(SpirvShader::Object::ID id, uint32_t size)
528                 {
529                         auto it = intermediates.emplace(std::piecewise_construct,
530                                         std::forward_as_tuple(id),
531                                         std::forward_as_tuple(size));
532                         return it.first->second;
533                 }
534
535                 Value& getValue(SpirvShader::Object::ID id)
536                 {
537                         auto it = lvalues.find(id);
538                         ASSERT(it != lvalues.end());
539                         return it->second;
540                 }
541
542                 Intermediate const& getIntermediate(SpirvShader::Object::ID id) const
543                 {
544                         auto it = intermediates.find(id);
545                         ASSERT(it != intermediates.end());
546                         return it->second;
547                 }
548
549                 Pointer<Byte>& getPhysicalPointer(SpirvShader::Object::ID id)
550                 {
551                         auto it = physicalPointers.find(id);
552                         assert(it != physicalPointers.end());
553                         return it->second;
554                 }
555         };
556
557         class GenericValue
558         {
559                 // Generic wrapper over either per-lane intermediate value, or a constant.
560                 // Constants are transparently widened to per-lane values in operator[].
561                 // This is appropriate in most cases -- if we're not going to do something
562                 // significantly different based on whether the value is uniform across lanes.
563
564                 SpirvShader::Object const &obj;
565                 Intermediate const *intermediate;
566
567         public:
568                 GenericValue(SpirvShader const *shader, SpirvRoutine const *routine, SpirvShader::Object::ID objId) :
569                                 obj(shader->getObject(objId)),
570                                 intermediate(obj.kind == SpirvShader::Object::Kind::Value ? &routine->getIntermediate(objId) : nullptr) {}
571
572                 RValue<SIMD::Float> Float(uint32_t i) const
573                 {
574                         if (intermediate != nullptr)
575                         {
576                                 return intermediate->Float(i);
577                         }
578                         auto constantValue = reinterpret_cast<float *>(obj.constantValue.get());
579                         return RValue<SIMD::Float>(constantValue[i]);
580                 }
581
582                 RValue<SIMD::Int> Int(uint32_t i) const
583                 {
584                         return As<SIMD::Int>(Float(i));
585                 }
586
587                 RValue<SIMD::UInt> UInt(uint32_t i) const
588                 {
589                         return As<SIMD::UInt>(Float(i));
590                 }
591         };
592
593 }
594
595 #endif  // sw_SpirvShader_hpp