OSDN Git Service

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