1 //
2 // Copyright (c) 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);
36     TString prePaddingString(const TType &type);
37     TString postPaddingString(const TType &type, bool useHLSLRowMajorPacking);
38 
39   private:
40     TString next();
41 
42     unsigned *mPaddingCounter;
43     int mElementIndex;
44     const std::map<TString, int> *mStructElementIndexes;
45 };
46 
47 class StructureHLSL : angle::NonCopyable
48 {
49   public:
50     StructureHLSL();
51 
52     // Returns the name of the constructor function.
53     TString addStructConstructor(const TStructure &structure);
54     TString addBuiltInConstructor(const TType &type, const TIntermSequence *parameters);
55 
56     static TString defineNameless(const TStructure &structure);
57     void ensureStructDefined(const TStructure &structure);
58 
59     std::string structsHeader() const;
60 
61     Std140PaddingHelper getPaddingHelper();
62 
63   private:
64     unsigned mUniquePaddingCounter;
65 
66     std::map<TString, int> mStd140StructElementIndexes;
67 
68     struct TStructProperties : public angle::NonCopyable
69     {
70         POOL_ALLOCATOR_NEW_DELETE();
71 
TStructPropertiesTStructProperties72         TStructProperties() {}
73 
74         // Constructor is an empty string in case the struct doesn't have a constructor yet.
75         TString constructor;
76     };
77 
78     // Map from struct name to struct properties.
79     typedef std::map<TString, TStructProperties *> DefinedStructs;
80     DefinedStructs mDefinedStructs;
81 
82     // Struct declarations need to be kept in a vector instead of having them inside mDefinedStructs
83     // since maintaining the original order is necessary for nested structs.
84     typedef std::vector<TString> StructDeclarations;
85     StructDeclarations mStructDeclarations;
86 
87     typedef std::set<TString> BuiltInConstructors;
88     BuiltInConstructors mBuiltInConstructors;
89 
90     void storeStd140ElementIndex(const TStructure &structure, bool useHLSLRowMajorPacking);
91     TString defineQualified(const TStructure &structure,
92                             bool useHLSLRowMajorPacking,
93                             bool useStd140Packing);
94     DefinedStructs::iterator defineVariants(const TStructure &structure, const TString &name);
95 };
96 }
97 
98 #endif  // COMPILER_TRANSLATOR_STRUCTUREHLSL_H_
99