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 ShCompileOptions compileOptions, 28 const std::vector<ShaderVariable> &uniforms, 29 unsigned int firstUniformRegister); 30 31 void reserveUniformRegisters(unsigned int registerCount); 32 void reserveUniformBlockRegisters(unsigned int registerCount); 33 void uniformsHeader(TInfoSinkBase &out, 34 ShShaderOutput outputType, 35 const ReferencedVariables &referencedUniforms, 36 TSymbolTable *symbolTable); 37 38 // Must be called after uniformsHeader 39 void samplerMetadataUniforms(TInfoSinkBase &out, unsigned int regIndex); getSamplerCount()40 unsigned int getSamplerCount() const { return mSamplerCount; } 41 void imageMetadataUniforms(TInfoSinkBase &out, unsigned int regIndex); 42 TString uniformBlocksHeader(const ReferencedInterfaceBlocks &referencedInterfaceBlocks); 43 TString shaderStorageBlocksHeader(const ReferencedInterfaceBlocks &referencedInterfaceBlocks); 44 45 // Used for direct index references 46 static TString InterfaceBlockInstanceString(const ImmutableString &instanceName, 47 unsigned int arrayIndex); 48 getShaderStorageBlockRegisterMap()49 const std::map<std::string, unsigned int> &getShaderStorageBlockRegisterMap() const 50 { 51 return mShaderStorageBlockRegisterMap; 52 } 53 getUniformBlockRegisterMap()54 const std::map<std::string, unsigned int> &getUniformBlockRegisterMap() const 55 { 56 return mUniformBlockRegisterMap; 57 } 58 getUniformBlockUseStructuredBufferMap()59 const std::map<std::string, bool> &getUniformBlockUseStructuredBufferMap() const 60 { 61 return mUniformBlockUseStructuredBufferMap; 62 } 63 getUniformRegisterMap()64 const std::map<std::string, unsigned int> &getUniformRegisterMap() const 65 { 66 return mUniformRegisterMap; 67 } 68 getReadonlyImage2DRegisterIndex()69 unsigned int getReadonlyImage2DRegisterIndex() const { return mReadonlyImage2DRegisterIndex; } getImage2DRegisterIndex()70 unsigned int getImage2DRegisterIndex() const { return mImage2DRegisterIndex; } 71 bool shouldTranslateUniformBlockToStructuredBuffer(const TInterfaceBlock &interfaceBlock); 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 ShCompileOptions mCompileOptions; 142 143 const std::vector<ShaderVariable> &mUniforms; 144 std::map<std::string, unsigned int> mUniformBlockRegisterMap; 145 std::map<std::string, unsigned int> mShaderStorageBlockRegisterMap; 146 std::map<std::string, unsigned int> mUniformRegisterMap; 147 std::map<std::string, bool> mUniformBlockUseStructuredBufferMap; 148 unsigned int mReadonlyImage2DRegisterIndex; 149 unsigned int mImage2DRegisterIndex; 150 }; 151 } // namespace sh 152 153 #endif // COMPILER_TRANSLATOR_RESOURCESHLSL_H_ 154