OgreD3D11HLSLProgram.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __D3D11HLSLProgram_H__
29 #define __D3D11HLSLProgram_H__
30 
31 #include "OgreD3D11Prerequisites.h"
35 
36 
37 namespace Ogre {
39 
49  {
50  public:
52  class CmdEntryPoint : public ParamCommand
53  {
54  public:
55  String doGet(const void* target) const;
56  void doSet(void* target, const String& val);
57  };
59  class CmdTarget : public ParamCommand
60  {
61  public:
62  String doGet(const void* target) const;
63  void doSet(void* target, const String& val);
64  };
67  {
68  public:
69  String doGet(const void* target) const;
70  void doSet(void* target, const String& val);
71  };
74  {
75  public:
76  String doGet(const void* target) const;
77  void doSet(void* target, const String& val);
78  };
81  {
82  public:
83  String doGet(const void* target) const;
84  void doSet(void* target, const String& val);
85  };
86 
87  protected:
88 
94 
97  void createLowLevelImpl(void);
99  void unloadHighLevelImpl(void);
102 
103  // Recursive utility method for populateParameterNames
104  void processParamElement(String prefix, LPCSTR pName, ID3D11ShaderReflectionType* varRefType);
105 
106  void populateDef(D3D11_SHADER_TYPE_DESC& d3dDesc, GpuConstantDefinition& def) const;
107 
113 
115  MicroCode mMicroCode;
116  ID3D11Buffer* mConstantBuffer;
117 
118  D3D_SHADER_MACRO* mShaderMacros;
120 
122 
124 
125  ID3D11VertexShader* mVertexShader;
126  ID3D11PixelShader* mPixelShader;
127  ID3D11GeometryShader* mGeometryShader;
128  ID3D11DomainShader* mDomainShader;
129  ID3D11HullShader* mHullShader;
130  ID3D11ComputeShader* mComputeShader;
131 
133  {
134  mutable String name;
135  size_t size;
136  size_t startOffset;
137 
139  {
140  name = var.name;
141  size = var.size;
142  startOffset = var.startOffset;
143  return *this;
144  }
145  };
147  typedef ShaderVars::iterator ShaderVarsIter;
148  typedef ShaderVars::const_iterator ShaderVarsConstIter;
149 
150  // A hack for cg to get the "original name" of the var in the "auto comments"
151  // that cg adds to the hlsl 4 output. This is to solve the issue that
152  // in some cases cg changes the name of the var to a new name.
153  void fixVariableNameFromCg(const ShaderVarWithPosInBuf& newVar);
154  //ShaderVars mShaderVars;
155 
156  // HACK: Multi-index emulation container to store constant buffer information by index and name at same time
157  // using tips from http://www.boost.org/doc/libs/1_35_0/libs/multi_index/doc/performance.html
158  // and http://cnx.org/content/m35767/1.2/
159 #define INVALID_IDX (unsigned int)-1
160  struct BufferInfo
161  {
163  unsigned int mIdx;
166  mutable ShaderVars mShaderVars;
167 
168  // Default constructor
169  BufferInfo() : mIdx(0), mName("") { mUniformBuffer.setNull(); }
170  BufferInfo(unsigned int index, const String& name)
171  : mIdx(index), mName(name)
172  {
173  mUniformBuffer.setNull();
174  }
175 
176  // Copy constructor
177  BufferInfo(const BufferInfo& info)
178  : mIdx(info.mIdx)
179  , mName(info.mName)
180  , mUniformBuffer(info.mUniformBuffer)
181  , mShaderVars(info.mShaderVars)
182  {
183 
184  }
185 
186  // Copy operator
188  {
189  this->mIdx = info.mIdx;
190  this->mName = info.mName;
191  mUniformBuffer = info.mUniformBuffer;
192  mShaderVars = info.mShaderVars;
193  return *this;
194  }
195 
196  // Constructors and operators used for search
197  BufferInfo(unsigned int index) : mIdx(index), mName("") { }
198  BufferInfo(const String& name) : mIdx(INVALID_IDX), mName(name) { }
199  BufferInfo& operator=(unsigned int index) { this->mIdx = index; return *this; }
200  BufferInfo& operator=(const String& name) { this->mName = name; return *this; }
201 
202  bool operator==(const BufferInfo& other) const
203  {
204  return mName == other.mName && mIdx == other.mIdx;
205  }
206  bool operator<(const BufferInfo& other) const
207  {
208  if (mIdx == INVALID_IDX || other.mIdx == INVALID_IDX)
209  {
210  return mName < other.mName;
211  }
212  else if (mName == "" || other.mName == "")
213  {
214  return mIdx < other.mIdx;
215  }
216  else
217  {
218  if (mName == other.mName)
219  {
220  return mIdx < other.mIdx;
221  }
222  else
223  {
224  return mName < other.mName;
225  }
226  }
227  }
228  };
229 
230  // Make sure that objects have index and name, or some search will fail
231  typedef std::set<BufferInfo> BufferInfoMap;
232  typedef std::set<BufferInfo>::iterator BufferInfoIterator;
233  BufferInfoMap mBufferInfoMap;
234 
235  // Map to store interface slot position.
236  // Number of interface slots is size of this map.
237  typedef std::map<std::string, unsigned int> SlotMap;
238  typedef std::map<std::string, unsigned int>::const_iterator SlotIterator;
239  SlotMap mSlotMap;
240 
242  typedef D3d11ShaderParameters::iterator D3d11ShaderParametersIter;
243 
244 
246  typedef D3d11ShaderVariables::iterator D3d11ShaderVariablesIter;
247 
249  {
250  LPCSTR Name;
251  };
253  typedef D3d11ShaderVariableSubparts::iterator D3d11ShaderVariableSubpartsIter;
254 
255  typedef struct MemberTypeName
256  {
257  LPCSTR Name;
258  };
259 
261 
266 
269  UINT mNumSlots;
270  ShaderVars mShaderVars;
271  D3d11ShaderParameters mD3d11ShaderInputParameters;
272  D3d11ShaderParameters mD3d11ShaderOutputParameters;
273  D3d11ShaderVariables mD3d11ShaderVariables;
274  D3d11ShaderVariableSubparts mD3d11ShaderVariableSubparts;
275  D3d11ShaderBufferDescs mD3d11ShaderBufferDescs;
276  D3d11ShaderVariables mVarDescBuffer;
277  D3d11ShaderVariables mVarDescPointer;
278  D3d11ShaderTypeDescs mD3d11ShaderTypeDescs;
279  D3d11ShaderTypeDescs mMemberTypeDesc;
280  MemberTypeNames mMemberTypeName;
281  InterfaceSlots mInterfaceSlots;
282 
283  void createConstantBuffer(const UINT ByteWidth);
284  void analizeMicrocode();
285  void getMicrocodeFromCache(void);
286  void compileMicrocode(void);
287  public:
288  D3D11HLSLProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
289  const String& group, bool isManual, ManualResourceLoader* loader, D3D11Device & device);
291 
293  void setEntryPoint(const String& entryPoint) { mEntryPoint = entryPoint; }
295  const String& getEntryPoint(void) const { return mEntryPoint; }
297  void setTarget(const String& target);
299  const String& getTarget(void) const { return mTarget; }
301  const String& getCompatibleTarget(void) const;
303  void setShaderMacros(D3D_SHADER_MACRO* shaderMacros);
304 
306  void setPreprocessorDefines(const String& defines) { mPreprocessorDefines = defines; }
308  const String& getPreprocessorDefines(void) const { return mPreprocessorDefines; }
310  void setColumnMajorMatrices(bool columnMajor) { mColumnMajorMatrices = columnMajor; }
312  bool getColumnMajorMatrices(void) const { return mColumnMajorMatrices; }
314  void setEnableBackwardsCompatibility(bool enableBackwardsCompatibility) { mEnableBackwardsCompatibility = enableBackwardsCompatibility; }
318  bool isSupported(void) const;
322  const String& getLanguage(void) const;
323 
324  virtual void buildConstantDefinitions() const;
325  ID3D11VertexShader* getVertexShader(void) const;
326  ID3D11PixelShader* getPixelShader(void) const;
327  ID3D11GeometryShader* getGeometryShader(void) const;
328  ID3D11DomainShader* getDomainShader(void) const;
329  ID3D11HullShader* getHullShader(void) const;
330  ID3D11ComputeShader* getComputeShader(void) const;
331  const MicroCode & getMicroCode(void) const;
332 
333  ID3D11Buffer* getConstantBuffer(GpuProgramParametersSharedPtr params, uint16 variabilityMask);
334 
335  void getConstantBuffers(ID3D11Buffer** buffers, unsigned int& numBuffers,
336  ID3D11ClassInstance** classes, unsigned int& numInstances,
337  GpuProgramParametersSharedPtr params, uint16 variabilityMask);
338 
339  // Get slot for a specific interface
340  unsigned int getSubroutineSlot(const String& subroutineSlotName) const;
341 
342  void CreateVertexShader();
343  void CreatePixelShader();
344  void CreateGeometryShader();
345  void CreateDomainShader();
346  void CreateHullShader();
347  void CreateComputeShader();
348 
351  void loadFromSource(void);
352 
354 
355  void reinterpretGSForStreamOut(void);
357 
358  unsigned int getNumInputs(void)const;
359  unsigned int getNumOutputs(void)const;
360 
362 
363  const D3D11_SIGNATURE_PARAMETER_DESC & getInputParamDesc(unsigned int index) const;
364  const D3D11_SIGNATURE_PARAMETER_DESC & getOutputParamDesc(unsigned int index) const;
365  };
366 }
367 
368 #endif
const String & getCompatibleTarget(void) const
Gets the shader target promoted to the first compatible, e.g.
void populateParameterNames(GpuProgramParametersSharedPtr params)
Populate the passed parameters with name->index map, must be overridden.
ID3D11Buffer * getConstantBuffer(GpuProgramParametersSharedPtr params, uint16 variabilityMask)
void getMicrocodeFromCache(void)
ShaderVars::iterator ShaderVarsIter
String getNameForMicrocodeCache()
const String & getEntryPoint(void) const
Gets the entry point defined for this program.
const String & getLanguage(void) const
Overridden from GpuProgram.
D3d11ShaderVariables::iterator D3d11ShaderVariablesIter
void getConstantBuffers(ID3D11Buffer **buffers, unsigned int &numBuffers, ID3D11ClassInstance **classes, unsigned int &numInstances, GpuProgramParametersSharedPtr params, uint16 variabilityMask)
Command object for setting macro defines.
void reinterpretGSForStreamOut(void)
void setTarget(const String &target)
Sets the shader target to compile down to, e.g.
D3d11ShaderTypeDescs mMemberTypeDesc
std::vector< T, A > type
void fixVariableNameFromCg(const ShaderVarWithPosInBuf &newVar)
vector< UINT >::type InterfaceSlots
void populateDef(D3D11_SHADER_TYPE_DESC &d3dDesc, GpuConstantDefinition &def) const
Command object for setting entry point.
void setEnableBackwardsCompatibility(bool enableBackwardsCompatibility)
Sets whether backwards compatibility is enabled.
vector< MemberTypeName >::type MemberTypeNames
static CmdEnableBackwardsCompatibility msCmdEnableBackwardsCompatibility
#define INVALID_IDX
D3D11HLSLProgram(ResourceManager *creator, const String &name, ResourceHandle handle, const String &group, bool isManual, ManualResourceLoader *loader, D3D11Device &device)
const String & getPreprocessorDefines(void) const
Sets the preprocessor defines use to compile the program.
ID3D11DomainShader * mDomainShader
void setShaderMacros(D3D_SHADER_MACRO *shaderMacros)
Sets shader macros created manually.
Abstract base class representing a high-level program (a vertex or fragment program).
vector< D3D11_SHADER_TYPE_DESC >::type D3d11ShaderTypeDescs
Interface describing a manual resource loader.
Definition: OgreResource.h:514
void setColumnMajorMatrices(bool columnMajor)
Sets whether matrix packing in column-major order.
Command object for setting target assembler.
ID3D11DomainShader * getDomainShader(void) const
stdext::hash_compare< _StringBase, std::less< _StringBase > > _StringHash
Definition: OgreString.h:215
unsigned long long int ResourceHandle
Definition: OgreResource.h:41
void setPreprocessorDefines(const String &defines)
Sets the preprocessor defines use to compile the program.
void setNull(void)
D3d11ShaderBufferDescs mD3d11ShaderBufferDescs
D3d11ShaderParameters mD3d11ShaderInputParameters
ID3D11VertexShader * mVertexShader
Command object for setting backwards compatibility.
static CmdTarget msCmdTarget
unsigned int getSubroutineSlot(const String &subroutineSlotName) const
const D3D11_SIGNATURE_PARAMETER_DESC & getInputParamDesc(unsigned int index) const
void processParamElement(String prefix, LPCSTR pName, ID3D11ShaderReflectionType *varRefType)
HardwareUniformBufferSharedPtr mUniformBuffer
bool getEnableBackwardsCompatibility(void) const
Gets whether backwards compatibility is enabled.
vector< ShaderVarWithPosInBuf >::type ShaderVars
Specialisation of VertexDeclaration for D3D11.
D3d11ShaderVariables mVarDescBuffer
std::map< std::string, unsigned int > SlotMap
void createLowLevelImpl(void)
Internal method for creating an appropriate low-level program from this high-level program...
std::map< std::string, unsigned int >::const_iterator SlotIterator
bool operator==(const BufferInfo &other) const
void unloadHighLevelImpl(void)
Internal unload implementation, must be implemented by subclasses.
BufferInfo(unsigned int index, const String &name)
static CmdPreprocessorDefines msCmdPreprocessorDefines
static CmdEntryPoint msCmdEntryPoint
Specialization of HighLevelGpuProgram to provide support for D3D11 High-Level Shader Language (HLSL)...
Command object for setting matrix packing in column-major order.
void setEntryPoint(const String &entryPoint)
Sets the entry point for this program ie the first method called.
void compileMicrocode(void)
const MicroCode & getMicroCode(void) const
ID3D11VertexShader * getVertexShader(void) const
D3d11ShaderParameters mD3d11ShaderOutputParameters
static CmdColumnMajorMatrices msCmdColumnMajorMatrices
_StringBase String
unsigned int getNumOutputs(void) const
BufferInfo & operator=(unsigned int index)
ID3D11GeometryShader * mGeometryShader
unsigned int getNumInputs(void) const
BufferInfo & operator=(const String &name)
D3d11ShaderVariables mD3d11ShaderVariables
D3D11VertexDeclaration mInputVertexDeclaration
String doGet(const void *target) const
D3d11ShaderVariableSubparts mD3d11ShaderVariableSubparts
D3d11ShaderParameters::iterator D3d11ShaderParametersIter
vector< D3D11_SHADER_BUFFER_DESC >::type D3d11ShaderBufferDescs
D3d11ShaderVariableSubparts::iterator D3d11ShaderVariableSubpartsIter
D3d11ShaderTypeDescs mD3d11ShaderTypeDescs
ID3D11ComputeShader * mComputeShader
void createConstantBuffer(const UINT ByteWidth)
void loadFromSource(void)
Internal load implementation, must be implemented by subclasses.
Defines a generic resource handler.
vector< GpuConstantDefinitionWithName >::type D3d11ShaderVariableSubparts
ID3D11HullShader * mHullShader
ID3D11ComputeShader * getComputeShader(void) const
This source file is part of OGRE (Object-oriented Graphics Rendering Engine) For the latest info...
ShaderVarWithPosInBuf & operator=(const ShaderVarWithPosInBuf &var)
D3d11ShaderVariables mVarDescPointer
ID3D11PixelShader * mPixelShader
ID3D11HullShader * getHullShader(void) const
vector< byte >::type MicroCode
ID3D11GeometryShader * getGeometryShader(void) const
bool isSupported(void) const
Overridden from GpuProgram.
ID3D11PixelShader * getPixelShader(void) const
bool getColumnMajorMatrices(void) const
Gets whether matrix packed in column-major order.
const D3D11_SIGNATURE_PARAMETER_DESC & getOutputParamDesc(unsigned int index) const
const String & getTarget(void) const
Gets the shader target to compile down to, e.g.
GpuProgramParametersSharedPtr createParameters(void)
Overridden from GpuProgram.
Abstract class which is command object which gets/sets parameters.
ShaderVars::const_iterator ShaderVarsConstIter
Information about predefined program constants.
D3D11VertexDeclaration & getInputVertexDeclaration()
vector< String * >::type mSerStrings
void doSet(void *target, const String &val)
vector< D3D11_SHADER_VARIABLE_DESC >::type D3d11ShaderVariables
BufferInfo & operator=(const BufferInfo &info)
bool operator<(const BufferInfo &other) const
vector< D3D11_SIGNATURE_PARAMETER_DESC >::type D3d11ShaderParameters
Shared pointer implementation used to share uniform buffers.
std::set< BufferInfo >::iterator BufferInfoIterator
unsigned short uint16
Definition: OgrePlatform.h:360
virtual void buildConstantDefinitions() const
Build the constant definition map, must be overridden.
std::set< BufferInfo > BufferInfoMap
D3D_SHADER_MACRO * mShaderMacros

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 Sun Oct 22 2017 04:04:15