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 // ResourcesHLSL.h:
7 //   Methods for GLSL to HLSL translation for uniforms and interface blocks.
8 //
9 
10 #ifndef COMPILER_TRANSLATOR_RESOURCESHLSL_H_
11 #define COMPILER_TRANSLATOR_RESOURCESHLSL_H_
12 
13 #include "compiler/translator/OutputHLSL.h"
14 #include "compiler/translator/UtilsHLSL.h"
15 
16 namespace sh
17 {
18 class ImmutableString;
19 class StructureHLSL;
20 class TSymbolTable;
21 
22 class ResourcesHLSL : angle::NonCopyable
23 {
24   public:
25     ResourcesHLSL(StructureHLSL *structureHLSL,
26                   ShShaderOutput outputType,
27                   const std::vector<ShaderVariable> &uniforms,
28                   unsigned int firstUniformRegister);
29 
30     void reserveUniformRegisters(unsigned int registerCount);
31     void reserveUniformBlockRegisters(unsigned int registerCount);
32     void uniformsHeader(TInfoSinkBase &out,
33                         ShShaderOutput outputType,
34                         const ReferencedVariables &referencedUniforms,
35                         TSymbolTable *symbolTable);
36 
37     // Must be called after uniformsHeader
38     void samplerMetadataUniforms(TInfoSinkBase &out, unsigned int regIndex);
getSamplerCount()39     unsigned int getSamplerCount() const { return mSamplerCount; }
40     void imageMetadataUniforms(TInfoSinkBase &out, unsigned int regIndex);
41     TString uniformBlocksHeader(
42         const ReferencedInterfaceBlocks &referencedInterfaceBlocks,
43         const std::map<int, const TInterfaceBlock *> &uniformBlockOptimizedMap);
44     TString shaderStorageBlocksHeader(const ReferencedInterfaceBlocks &referencedInterfaceBlocks);
45 
46     // Used for direct index references
47     static TString InterfaceBlockInstanceString(const ImmutableString &instanceName,
48                                                 unsigned int arrayIndex);
49 
getShaderStorageBlockRegisterMap()50     const std::map<std::string, unsigned int> &getShaderStorageBlockRegisterMap() const
51     {
52         return mShaderStorageBlockRegisterMap;
53     }
54 
getUniformBlockRegisterMap()55     const std::map<std::string, unsigned int> &getUniformBlockRegisterMap() const
56     {
57         return mUniformBlockRegisterMap;
58     }
59 
getUniformBlockUseStructuredBufferMap()60     const std::map<std::string, bool> &getUniformBlockUseStructuredBufferMap() const
61     {
62         return mUniformBlockUseStructuredBufferMap;
63     }
64 
getUniformRegisterMap()65     const std::map<std::string, unsigned int> &getUniformRegisterMap() const
66     {
67         return mUniformRegisterMap;
68     }
69 
getReadonlyImage2DRegisterIndex()70     unsigned int getReadonlyImage2DRegisterIndex() const { return mReadonlyImage2DRegisterIndex; }
getImage2DRegisterIndex()71     unsigned int getImage2DRegisterIndex() const { return mImage2DRegisterIndex; }
72 
73   private:
74     TString uniformBlockString(const TInterfaceBlock &interfaceBlock,
75                                const TVariable *instanceVariable,
76                                unsigned int registerIndex,
77                                unsigned int arrayIndex);
78     TString uniformBlockWithOneLargeArrayMemberString(const TInterfaceBlock &interfaceBlock,
79                                                       const TVariable *instanceVariable,
80                                                       unsigned int registerIndex,
81                                                       unsigned int arrayIndex);
82 
83     TString shaderStorageBlockString(const TInterfaceBlock &interfaceBlock,
84                                      const TVariable *instanceVariable,
85                                      unsigned int registerIndex,
86                                      unsigned int arrayIndex);
87     TString uniformBlockMembersString(const TInterfaceBlock &interfaceBlock,
88                                       TLayoutBlockStorage blockStorage);
89     TString uniformBlockStructString(const TInterfaceBlock &interfaceBlock);
90     const ShaderVariable *findUniformByName(const ImmutableString &name) const;
91 
92     void outputHLSL4_0_FL9_3Sampler(TInfoSinkBase &out,
93                                     const TType &type,
94                                     const TVariable &variable,
95                                     const unsigned int registerIndex);
96     void outputUniform(TInfoSinkBase &out,
97                        const TType &type,
98                        const TVariable &variable,
99                        const unsigned int registerIndex);
100     void outputAtomicCounterBuffer(TInfoSinkBase &out,
101                                    const int binding,
102                                    const unsigned int registerIndex);
103 
104     // Returns the uniform's register index
105     unsigned int assignUniformRegister(const TType &type,
106                                        const ImmutableString &name,
107                                        unsigned int *outRegisterCount);
108     unsigned int assignSamplerInStructUniformRegister(const TType &type,
109                                                       const TString &name,
110                                                       unsigned int *outRegisterCount);
111 
112     void outputHLSLSamplerUniformGroup(
113         TInfoSinkBase &out,
114         const HLSLTextureGroup textureGroup,
115         const TVector<const TVariable *> &group,
116         const TMap<const TVariable *, TString> &samplerInStructSymbolsToAPINames,
117         unsigned int *groupTextureRegisterIndex);
118 
119     void outputHLSLImageUniformIndices(TInfoSinkBase &out,
120                                        const TVector<const TVariable *> &group,
121                                        unsigned int imageArrayIndex,
122                                        unsigned int *groupRegisterCount);
123     void outputHLSLReadonlyImageUniformGroup(TInfoSinkBase &out,
124                                              const HLSLTextureGroup textureGroup,
125                                              const TVector<const TVariable *> &group,
126                                              unsigned int *groupTextureRegisterIndex);
127     void outputHLSLImageUniformGroup(TInfoSinkBase &out,
128                                      const HLSLRWTextureGroup textureGroup,
129                                      const TVector<const TVariable *> &group,
130                                      unsigned int *groupTextureRegisterIndex);
131 
132     unsigned int mUniformRegister;
133     unsigned int mUniformBlockRegister;
134     unsigned int mSRVRegister;
135     unsigned int mUAVRegister;
136     unsigned int mSamplerCount;
137     unsigned int mReadonlyImageCount;
138     unsigned int mImageCount;
139     StructureHLSL *mStructureHLSL;
140     ShShaderOutput mOutputType;
141 
142     const std::vector<ShaderVariable> &mUniforms;
143     std::map<std::string, unsigned int> mUniformBlockRegisterMap;
144     std::map<std::string, unsigned int> mShaderStorageBlockRegisterMap;
145     std::map<std::string, unsigned int> mUniformRegisterMap;
146     std::map<std::string, bool> mUniformBlockUseStructuredBufferMap;
147     unsigned int mReadonlyImage2DRegisterIndex;
148     unsigned int mImage2DRegisterIndex;
149 };
150 }  // namespace sh
151 
152 #endif  // COMPILER_TRANSLATOR_RESOURCESHLSL_H_
153