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_HLSL_RESOURCESHLSL_H_ 11 #define COMPILER_TRANSLATOR_HLSL_RESOURCESHLSL_H_ 12 13 #include "compiler/translator/hlsl/OutputHLSL.h" 14 #include "compiler/translator/hlsl/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 void allocateShaderStorageBlockRegisters( 45 const ReferencedInterfaceBlocks &referencedInterfaceBlocks); 46 TString shaderStorageBlocksHeader(const ReferencedInterfaceBlocks &referencedInterfaceBlocks); 47 48 // Used for direct index references 49 static TString InterfaceBlockInstanceString(const ImmutableString &instanceName, 50 unsigned int arrayIndex); 51 getShaderStorageBlockRegisterMap()52 const std::map<std::string, unsigned int> &getShaderStorageBlockRegisterMap() const 53 { 54 return mShaderStorageBlockRegisterMap; 55 } 56 getUniformBlockRegisterMap()57 const std::map<std::string, unsigned int> &getUniformBlockRegisterMap() const 58 { 59 return mUniformBlockRegisterMap; 60 } 61 getUniformBlockUseStructuredBufferMap()62 const std::map<std::string, bool> &getUniformBlockUseStructuredBufferMap() const 63 { 64 return mUniformBlockUseStructuredBufferMap; 65 } 66 getUniformRegisterMap()67 const std::map<std::string, unsigned int> &getUniformRegisterMap() const 68 { 69 return mUniformRegisterMap; 70 } 71 getReadonlyImage2DRegisterIndex()72 unsigned int getReadonlyImage2DRegisterIndex() const { return mReadonlyImage2DRegisterIndex; } getImage2DRegisterIndex()73 unsigned int getImage2DRegisterIndex() const { return mImage2DRegisterIndex; } hasImages()74 bool hasImages() const { return mReadonlyImageCount > 0 || mImageCount > 0; } 75 76 private: 77 TString uniformBlockString(const TInterfaceBlock &interfaceBlock, 78 const TVariable *instanceVariable, 79 unsigned int registerIndex, 80 unsigned int arrayIndex); 81 TString uniformBlockWithOneLargeArrayMemberString(const TInterfaceBlock &interfaceBlock, 82 const TVariable *instanceVariable, 83 unsigned int registerIndex, 84 unsigned int arrayIndex); 85 86 TString shaderStorageBlockString(const TInterfaceBlock &interfaceBlock, 87 const TVariable *instanceVariable, 88 unsigned int registerIndex, 89 unsigned int arrayIndex); 90 TString uniformBlockMembersString(const TInterfaceBlock &interfaceBlock, 91 TLayoutBlockStorage blockStorage); 92 TString uniformBlockStructString(const TInterfaceBlock &interfaceBlock); 93 const ShaderVariable *findUniformByName(const ImmutableString &name) const; 94 95 void outputUniform(TInfoSinkBase &out, 96 const TType &type, 97 const TVariable &variable, 98 const unsigned int registerIndex); 99 void outputAtomicCounterBuffer(TInfoSinkBase &out, 100 const int binding, 101 const unsigned int registerIndex); 102 103 // Returns the uniform's register index 104 unsigned int assignUniformRegister(const TType &type, 105 const ImmutableString &name, 106 unsigned int *outRegisterCount); 107 unsigned int assignSamplerInStructUniformRegister(const TType &type, 108 const TString &name, 109 unsigned int *outRegisterCount); 110 111 void outputHLSLSamplerUniformGroup( 112 TInfoSinkBase &out, 113 const HLSLTextureGroup textureGroup, 114 const TVector<const TVariable *> &group, 115 const TMap<const TVariable *, TString> &samplerInStructSymbolsToAPINames, 116 unsigned int *groupTextureRegisterIndex); 117 118 void outputHLSLImageUniformIndices(TInfoSinkBase &out, 119 const TVector<const TVariable *> &group, 120 unsigned int imageArrayIndex, 121 unsigned int *groupRegisterCount); 122 void outputHLSLReadonlyImageUniformGroup(TInfoSinkBase &out, 123 const HLSLTextureGroup textureGroup, 124 const TVector<const TVariable *> &group, 125 unsigned int *groupTextureRegisterIndex); 126 void outputHLSLImageUniformGroup(TInfoSinkBase &out, 127 const HLSLRWTextureGroup textureGroup, 128 const TVector<const TVariable *> &group, 129 unsigned int *groupTextureRegisterIndex); 130 131 unsigned int mUniformRegister; 132 unsigned int mUniformBlockRegister; 133 unsigned int mSRVRegister; 134 unsigned int mUAVRegister; 135 unsigned int mSamplerCount; 136 unsigned int mReadonlyImageCount = 0; 137 unsigned int mImageCount = 0; 138 StructureHLSL *mStructureHLSL; 139 ShShaderOutput mOutputType; 140 141 const std::vector<ShaderVariable> &mUniforms; 142 std::map<std::string, unsigned int> mUniformBlockRegisterMap; 143 std::map<std::string, unsigned int> mShaderStorageBlockRegisterMap; 144 std::map<std::string, unsigned int> mUniformRegisterMap; 145 std::map<std::string, bool> mUniformBlockUseStructuredBufferMap; 146 unsigned int mReadonlyImage2DRegisterIndex = 0; 147 unsigned int mImage2DRegisterIndex = 0; 148 }; 149 } // namespace sh 150 151 #endif // COMPILER_TRANSLATOR_HLSL_RESOURCESHLSL_H_ 152