1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // StructureHLSL.h:
7 //   HLSL translation of GLSL constructors and structures.
8 //
9 
10 #ifndef COMPILER_TRANSLATOR_STRUCTUREHLSL_H_
11 #define COMPILER_TRANSLATOR_STRUCTUREHLSL_H_
12 
13 #include "compiler/translator/Common.h"
14 #include "compiler/translator/IntermNode.h"
15 
16 #include <set>
17 
18 class TInfoSinkBase;
19 class TScopeBracket;
20 
21 namespace sh
22 {
23 
24 // This helper class assists structure and interface block definitions in determining
25 // how to pack std140 structs within HLSL's packing rules.
26 class Std140PaddingHelper
27 {
28   public:
29     explicit Std140PaddingHelper(const std::map<TString, int> &structElementIndexes,
30                                  unsigned int *uniqueCounter);
31     Std140PaddingHelper(const Std140PaddingHelper &other);
32     Std140PaddingHelper &operator=(const Std140PaddingHelper &other);
33 
elementIndex()34     int elementIndex() const { return mElementIndex; }
35     int prePadding(const TType &type, bool forcePadding);
36     TString prePaddingString(const TType &type, bool forcePadding);
37     TString postPaddingString(const TType &type,
38                               bool useHLSLRowMajorPacking,
39                               bool isLastElement,
40                               bool forcePadding);
41 
42   private:
43     TString next();
44 
45     unsigned *mPaddingCounter;
46     int mElementIndex;
47     const std::map<TString, int> *mStructElementIndexes;
48 };
49 
50 class StructureHLSL : angle::NonCopyable
51 {
52   public:
53     StructureHLSL();
54 
55     // Returns the name of the constructor function.
56     TString addStructConstructor(const TStructure &structure);
57     TString addBuiltInConstructor(const TType &type, const TIntermSequence *parameters);
58 
59     static TString defineNameless(const TStructure &structure);
60     void ensureStructDefined(const TStructure &structure);
61 
62     std::string structsHeader() const;
63 
64     Std140PaddingHelper getPaddingHelper();
65 
66   private:
67     unsigned mUniquePaddingCounter;
68 
69     std::map<TString, int> mStd140StructElementIndexes;
70 
71     struct TStructProperties : public angle::NonCopyable
72     {
73         POOL_ALLOCATOR_NEW_DELETE
74 
TStructPropertiesTStructProperties75         TStructProperties() {}
76 
77         // Constructor is an empty string in case the struct doesn't have a constructor yet.
78         TString constructor;
79     };
80 
81     // Map from struct name to struct properties.
82     typedef std::map<TString, TStructProperties *> DefinedStructs;
83     DefinedStructs mDefinedStructs;
84 
85     // Struct declarations need to be kept in a vector instead of having them inside mDefinedStructs
86     // since maintaining the original order is necessary for nested structs.
87     typedef std::vector<TString> StructDeclarations;
88     StructDeclarations mStructDeclarations;
89 
90     typedef std::set<TString> BuiltInConstructors;
91     BuiltInConstructors mBuiltInConstructors;
92 
93     void storeStd140ElementIndex(const TStructure &structure, bool useHLSLRowMajorPacking);
94     TString defineQualified(const TStructure &structure,
95                             bool useHLSLRowMajorPacking,
96                             bool useStd140Packing,
97                             bool forcePackingEnd);
98     DefinedStructs::iterator defineVariants(const TStructure &structure, const TString &name);
99 };
100 }  // namespace sh
101 
102 #endif  // COMPILER_TRANSLATOR_STRUCTUREHLSL_H_
103