OgreD3D11HLSLProgram.h
Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004 (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2013 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 #ifndef __D3D11HLSLProgram_H__
00029 #define __D3D11HLSLProgram_H__
00030 
00031 #include "OgreD3D11Prerequisites.h"
00032 #include "OgreHighLevelGpuProgram.h"
00033 #include "OgreHardwareUniformBuffer.h"
00034 #include "OgreD3D11VertexDeclaration.h"
00035 
00036 
00037 namespace Ogre {
00038     typedef vector<byte>::type MicroCode;
00039 
00048     class D3D11HLSLProgram : public HighLevelGpuProgram
00049     {
00050     public:
00052         class CmdEntryPoint : public ParamCommand
00053         {
00054         public:
00055             String doGet(const void* target) const;
00056             void doSet(void* target, const String& val);
00057         };
00059         class CmdTarget : public ParamCommand
00060         {
00061         public:
00062             String doGet(const void* target) const;
00063             void doSet(void* target, const String& val);
00064         };
00066         class CmdPreprocessorDefines : public ParamCommand
00067         {
00068         public:
00069             String doGet(const void* target) const;
00070             void doSet(void* target, const String& val);
00071         };
00073         class CmdColumnMajorMatrices : public ParamCommand
00074         {
00075         public:
00076             String doGet(const void* target) const;
00077             void doSet(void* target, const String& val);
00078         };
00080         class CmdEnableBackwardsCompatibility : public ParamCommand
00081         {
00082         public:
00083             String doGet(const void* target) const;
00084             void doSet(void* target, const String& val);
00085         };
00086 
00087     protected:
00088 
00089         static CmdEntryPoint msCmdEntryPoint;
00090         static CmdTarget msCmdTarget;
00091         static CmdPreprocessorDefines msCmdPreprocessorDefines;
00092         static CmdColumnMajorMatrices msCmdColumnMajorMatrices;
00093         static CmdEnableBackwardsCompatibility msCmdEnableBackwardsCompatibility;
00094         
00097         void createLowLevelImpl(void);
00099         void unloadHighLevelImpl(void);
00101         void populateParameterNames(GpuProgramParametersSharedPtr params);
00102 
00103         // Recursive utility method for populateParameterNames
00104         void processParamElement(String prefix, LPCSTR pName, ID3D11ShaderReflectionType* varRefType);
00105 
00106         void populateDef(D3D11_SHADER_TYPE_DESC& d3dDesc, GpuConstantDefinition& def) const;
00107 
00108         String mTarget;
00109         String mEntryPoint;
00110         String mPreprocessorDefines;
00111         bool mColumnMajorMatrices;
00112         bool mEnableBackwardsCompatibility;
00113 
00114         bool mErrorsInCompile;
00115         MicroCode mMicroCode;
00116         ID3D11Buffer* mConstantBuffer;
00117         
00118         D3D_SHADER_MACRO* mShaderMacros;
00119         bool shaderMacroSet;
00120 
00121         D3D11Device & mDevice;
00122 
00123         D3D11VertexDeclaration mInputVertexDeclaration;
00124 
00125         ID3D11VertexShader* mVertexShader;
00126         ID3D11PixelShader* mPixelShader;
00127         ID3D11GeometryShader* mGeometryShader;
00128         ID3D11DomainShader* mDomainShader;
00129         ID3D11HullShader* mHullShader;
00130         ID3D11ComputeShader* mComputeShader;
00131 
00132         struct ShaderVarWithPosInBuf
00133         {
00134             mutable String name;
00135             size_t size;
00136             size_t startOffset;
00137             
00138             ShaderVarWithPosInBuf& operator=(const ShaderVarWithPosInBuf& var)
00139             {
00140                 name = var.name;
00141                 size = var.size;
00142                 startOffset = var.startOffset;
00143                 return *this;
00144             }
00145         };
00146         typedef vector<ShaderVarWithPosInBuf>::type ShaderVars;
00147         typedef ShaderVars::iterator ShaderVarsIter;
00148         typedef ShaderVars::const_iterator ShaderVarsConstIter; 
00149 
00150         // A hack for cg to get the "original name" of the var in the "auto comments"
00151         // that cg adds to the hlsl 4 output. This is to solve the issue that
00152         // in some cases cg changes the name of the var to a new name.
00153         void fixVariableNameFromCg(const ShaderVarWithPosInBuf& newVar);
00154         //ShaderVars mShaderVars;
00155         
00156         // HACK: Multi-index emulation container to store constant buffer information by index and name at same time
00157         // using tips from http://www.boost.org/doc/libs/1_35_0/libs/multi_index/doc/performance.html
00158         // and http://cnx.org/content/m35767/1.2/
00159 #define INVALID_IDX (unsigned int)-1
00160         struct BufferInfo
00161         {
00162             static _StringHash mHash;
00163             unsigned int mIdx;
00164             String mName;
00165             mutable HardwareUniformBufferSharedPtr mUniformBuffer;
00166             mutable ShaderVars mShaderVars;
00167                 
00168             // Default constructor
00169             BufferInfo() : mIdx(0), mName("") { mUniformBuffer.setNull(); }
00170             BufferInfo(unsigned int index, const String& name)
00171                 : mIdx(index), mName(name)
00172             {
00173                 mUniformBuffer.setNull();
00174             }
00175             
00176             // Copy constructor
00177             BufferInfo(const BufferInfo& info) 
00178                 : mIdx(info.mIdx)
00179                 , mName(info.mName)
00180                 , mUniformBuffer(info.mUniformBuffer)
00181                 , mShaderVars(info.mShaderVars)
00182             {
00183 
00184             }
00185 
00186             // Copy operator
00187             BufferInfo& operator=(const BufferInfo& info)
00188             {
00189                 this->mIdx = info.mIdx;
00190                 this->mName = info.mName;
00191                 mUniformBuffer = info.mUniformBuffer;
00192                 mShaderVars = info.mShaderVars;
00193                 return *this;
00194             }
00195             
00196             // Constructors and operators used for search
00197             BufferInfo(unsigned int index) : mIdx(index), mName("") { }
00198             BufferInfo(const String& name) : mIdx(INVALID_IDX), mName(name) { }
00199             BufferInfo& operator=(unsigned int index) { this->mIdx = index; return *this; }
00200             BufferInfo& operator=(const String& name) { this->mName = name; return *this; } 
00201             
00202             bool operator==(const BufferInfo& other) const
00203             {
00204                 return mName == other.mName && mIdx == other.mIdx;
00205             }
00206             bool operator<(const BufferInfo& other) const
00207             {
00208                 if (mIdx == INVALID_IDX || other.mIdx == INVALID_IDX) 
00209                 {
00210                     return mName < other.mName;
00211                 }
00212                 else if (mName == "" || other.mName == "")
00213                 {
00214                     return mIdx < other.mIdx;
00215                 }
00216                 else 
00217                 {
00218                     if (mName == other.mName)
00219                     {
00220                         return mIdx < other.mIdx;
00221                     }
00222                     else
00223                     {
00224                         return mName < other.mName;
00225                     }
00226                 }
00227             }
00228         };
00229 
00230         // Make sure that objects have index and name, or some search will fail
00231         typedef std::set<BufferInfo> BufferInfoMap;
00232         typedef std::set<BufferInfo>::iterator BufferInfoIterator;
00233         BufferInfoMap mBufferInfoMap;
00234 
00235         // Map to store interface slot position. 
00236         // Number of interface slots is size of this map.
00237         typedef std::map<std::string, unsigned int> SlotMap;
00238         typedef std::map<std::string, unsigned int>::const_iterator SlotIterator;
00239         SlotMap mSlotMap;
00240 
00241         typedef vector<D3D11_SIGNATURE_PARAMETER_DESC>::type D3d11ShaderParameters;
00242         typedef D3d11ShaderParameters::iterator D3d11ShaderParametersIter; 
00243 
00244 
00245         typedef vector<D3D11_SHADER_VARIABLE_DESC>::type D3d11ShaderVariables;
00246         typedef D3d11ShaderVariables::iterator D3d11ShaderVariablesIter; 
00247 
00248         struct GpuConstantDefinitionWithName : GpuConstantDefinition
00249         {
00250             LPCSTR                  Name;          
00251         };
00252         typedef vector<GpuConstantDefinitionWithName>::type D3d11ShaderVariableSubparts;
00253         typedef D3d11ShaderVariableSubparts::iterator D3d11ShaderVariableSubpartsIter; 
00254 
00255         typedef struct MemberTypeName
00256         {
00257             LPCSTR                  Name;           
00258         };
00259 
00260         vector<String *>::type mSerStrings;
00261 
00262         typedef vector<D3D11_SHADER_BUFFER_DESC>::type D3d11ShaderBufferDescs;
00263         typedef vector<D3D11_SHADER_TYPE_DESC>::type D3d11ShaderTypeDescs;
00264         typedef vector<UINT>::type InterfaceSlots;
00265         typedef vector<MemberTypeName>::type MemberTypeNames;
00266 
00267         UINT mConstantBufferSize;
00268         UINT mConstantBufferNr;
00269         UINT mNumSlots;
00270         ShaderVars mShaderVars;
00271         D3d11ShaderParameters mD3d11ShaderInputParameters;
00272         D3d11ShaderParameters mD3d11ShaderOutputParameters;
00273         D3d11ShaderVariables mD3d11ShaderVariables;
00274         D3d11ShaderVariableSubparts mD3d11ShaderVariableSubparts;
00275         D3d11ShaderBufferDescs mD3d11ShaderBufferDescs;
00276         D3d11ShaderVariables mVarDescBuffer;
00277         D3d11ShaderVariables mVarDescPointer;
00278         D3d11ShaderTypeDescs mD3d11ShaderTypeDescs;
00279         D3d11ShaderTypeDescs mMemberTypeDesc;
00280         MemberTypeNames mMemberTypeName;
00281         InterfaceSlots mInterfaceSlots;
00282 
00283         void createConstantBuffer(const UINT ByteWidth);
00284         void analizeMicrocode();
00285         void getMicrocodeFromCache(void);
00286         void compileMicrocode(void);
00287     public:
00288         D3D11HLSLProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
00289             const String& group, bool isManual, ManualResourceLoader* loader, D3D11Device & device);
00290         ~D3D11HLSLProgram();
00291 
00293         void setEntryPoint(const String& entryPoint) { mEntryPoint = entryPoint; }
00295         const String& getEntryPoint(void) const { return mEntryPoint; }
00297         void setTarget(const String& target);
00299         const String& getTarget(void) const { return mTarget; }
00301         const String& getCompatibleTarget(void) const;
00303         void setShaderMacros(D3D_SHADER_MACRO* shaderMacros);
00304 
00306         void setPreprocessorDefines(const String& defines) { mPreprocessorDefines = defines; }
00308         const String& getPreprocessorDefines(void) const { return mPreprocessorDefines; }
00310         void setColumnMajorMatrices(bool columnMajor) { mColumnMajorMatrices = columnMajor; }
00312         bool getColumnMajorMatrices(void) const { return mColumnMajorMatrices; }
00314         void setEnableBackwardsCompatibility(bool enableBackwardsCompatibility) { mEnableBackwardsCompatibility = enableBackwardsCompatibility; }
00316         bool getEnableBackwardsCompatibility(void) const { return mEnableBackwardsCompatibility; }
00318         bool isSupported(void) const;
00320         GpuProgramParametersSharedPtr createParameters(void);
00322         const String& getLanguage(void) const;
00323 
00324         virtual void buildConstantDefinitions() const;
00325         ID3D11VertexShader* getVertexShader(void) const;
00326         ID3D11PixelShader* getPixelShader(void) const; 
00327         ID3D11GeometryShader* getGeometryShader(void) const; 
00328         ID3D11DomainShader* getDomainShader(void) const;
00329         ID3D11HullShader* getHullShader(void) const;
00330         ID3D11ComputeShader* getComputeShader(void) const;
00331         const MicroCode &  getMicroCode(void) const;  
00332 
00333         ID3D11Buffer* getConstantBuffer(GpuProgramParametersSharedPtr params, uint16 variabilityMask);
00334 
00335         void getConstantBuffers(ID3D11Buffer** buffers, unsigned int& numBuffers,
00336                                 ID3D11ClassInstance** classes, unsigned int& numInstances,
00337                                 GpuProgramParametersSharedPtr params, uint16 variabilityMask);
00338 
00339         // Get slot for a specific interface
00340         unsigned int getSubroutineSlot(const String& subroutineSlotName) const;
00341 
00342         void CreateVertexShader();
00343         void CreatePixelShader();
00344         void CreateGeometryShader();
00345         void CreateDomainShader();
00346         void CreateHullShader();
00347         void CreateComputeShader();
00348 
00351         void loadFromSource(void);
00352 
00353         D3D11VertexDeclaration & getInputVertexDeclaration() { return mInputVertexDeclaration; }
00354 
00355         void reinterpretGSForStreamOut(void);
00356         bool mReinterpretingGS;
00357         
00358         unsigned int getNumInputs(void)const;
00359         unsigned int getNumOutputs(void)const;
00360 
00361         String getNameForMicrocodeCache();
00362 
00363         const D3D11_SIGNATURE_PARAMETER_DESC & getInputParamDesc(unsigned int index) const;
00364         const D3D11_SIGNATURE_PARAMETER_DESC & getOutputParamDesc(unsigned int index) const;    
00365     };
00366 }
00367 
00368 #endif

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:41