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 // UniformHLSL.h:
7 //   Methods for GLSL to HLSL translation for uniforms and uniform blocks.
8 //
9 
10 #ifndef COMPILER_TRANSLATOR_UNIFORMHLSL_H_
11 #define COMPILER_TRANSLATOR_UNIFORMHLSL_H_
12 
13 #include "compiler/translator/OutputHLSL.h"
14 #include "compiler/translator/UtilsHLSL.h"
15 
16 namespace sh
17 {
18 class StructureHLSL;
19 class TSymbolTable;
20 
21 class UniformHLSL : angle::NonCopyable
22 {
23   public:
24     UniformHLSL(sh::GLenum shaderType,
25                 StructureHLSL *structureHLSL,
26                 ShShaderOutput outputType,
27                 const std::vector<Uniform> &uniforms);
28 
29     void reserveUniformRegisters(unsigned int registerCount);
30     void reserveUniformBlockRegisters(unsigned int registerCount);
31     void uniformsHeader(TInfoSinkBase &out,
32                         ShShaderOutput outputType,
33                         const ReferencedSymbols &referencedUniforms,
34                         TSymbolTable *symbolTable);
35 
36     // Must be called after uniformsHeader
37     void samplerMetadataUniforms(TInfoSinkBase &out, const char *reg);
38 
39     TString uniformBlocksHeader(const ReferencedSymbols &referencedInterfaceBlocks);
40 
41     // Used for direct index references
42     static TString uniformBlockInstanceString(const TInterfaceBlock &interfaceBlock,
43                                               unsigned int arrayIndex);
44 
getUniformBlockRegisterMap()45     const std::map<std::string, unsigned int> &getUniformBlockRegisterMap() const
46     {
47         return mUniformBlockRegisterMap;
48     }
getUniformRegisterMap()49     const std::map<std::string, unsigned int> &getUniformRegisterMap() const
50     {
51         return mUniformRegisterMap;
52     }
53 
54   private:
55     TString uniformBlockString(const TInterfaceBlock &interfaceBlock,
56                                unsigned int registerIndex,
57                                unsigned int arrayIndex);
58     TString uniformBlockMembersString(const TInterfaceBlock &interfaceBlock,
59                                       TLayoutBlockStorage blockStorage);
60     TString uniformBlockStructString(const TInterfaceBlock &interfaceBlock);
61     const Uniform *findUniformByName(const TString &name) const;
62 
63     void outputHLSL4_0_FL9_3Sampler(TInfoSinkBase &out,
64                                     const TType &type,
65                                     const TName &name,
66                                     const unsigned int registerIndex);
67     void outputHLSL4_1_FL11Texture(TInfoSinkBase &out,
68                                    const TType &type,
69                                    const TName &name,
70                                    const unsigned int registerIndex);
71     void outputHLSL4_1_FL11RWTexture(TInfoSinkBase &out,
72                                      const TType &type,
73                                      const TName &name,
74                                      const unsigned int registerIndex);
75     void outputUniform(TInfoSinkBase &out,
76                        const TType &type,
77                        const TName &name,
78                        const unsigned int registerIndex);
79 
80     // Returns the uniform's register index
81     unsigned int assignUniformRegister(const TType &type,
82                                        const TString &name,
83                                        unsigned int *outRegisterCount);
84     unsigned int assignSamplerInStructUniformRegister(const TType &type,
85                                                       const TString &name,
86                                                       unsigned int *outRegisterCount);
87 
88     void outputHLSLSamplerUniformGroup(
89         TInfoSinkBase &out,
90         const HLSLTextureGroup textureGroup,
91         const TVector<const TIntermSymbol *> &group,
92         const TMap<const TIntermSymbol *, TString> &samplerInStructSymbolsToAPINames,
93         unsigned int *groupTextureRegisterIndex);
94 
95     unsigned int mUniformRegister;
96     unsigned int mUniformBlockRegister;
97     unsigned int mTextureRegister;
98     unsigned int mRWTextureRegister;
99     unsigned int mSamplerCount;
100     sh::GLenum mShaderType;
101     StructureHLSL *mStructureHLSL;
102     ShShaderOutput mOutputType;
103 
104     const std::vector<Uniform> &mUniforms;
105     std::map<std::string, unsigned int> mUniformBlockRegisterMap;
106     std::map<std::string, unsigned int> mUniformRegisterMap;
107 };
108 }
109 
110 #endif  // COMPILER_TRANSLATOR_UNIFORMHLSL_H_
111