OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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-2014 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 
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;
167 
168  // Default constructor
170  BufferInfo(unsigned int index, const String& name)
171  : mIdx(index), mName(name)
172  {
174  }
175 
176  // Copy constructor
177  BufferInfo(const BufferInfo& info)
178  : mIdx(info.mIdx)
179  , mName(info.mName)
181  , mShaderVars(info.mShaderVars)
182  {
183 
184  }
185 
186  // Copy operator
188  {
189  this->mIdx = info.mIdx;
190  this->mName = info.mName;
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;
234 
235  // Map to store interface slot position.
236  // Number of interface slots is size of this map.
237  typedef std::map<String, unsigned int> SlotMap;
238  typedef std::map<String, unsigned int>::const_iterator SlotIterator;
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;
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
D3D11VertexDeclaration mInputVertexDeclaration
String doGet(const void *target) const
Command object for setting backwards compatibility.
D3d11ShaderVariableSubparts::iterator D3d11ShaderVariableSubpartsIter
void createLowLevelImpl(void)
Internal method for creating an appropriate low-level program from this high-level program...
D3d11ShaderTypeDescs mD3d11ShaderTypeDescs
D3d11ShaderVariables mVarDescBuffer
vector< D3D11_SHADER_BUFFER_DESC >::type D3d11ShaderBufferDescs
vector< ShaderVarWithPosInBuf >::type ShaderVars
unsigned int getNumInputs(void) const
D3d11ShaderVariableSubparts mD3d11ShaderVariableSubparts
void doSet(void *target, const String &val)
ID3D11Buffer * getConstantBuffer(GpuProgramParametersSharedPtr params, uint16 variabilityMask)
Shared pointer implementation used to share uniform buffers.
void loadFromSource(void)
Internal load implementation, must be implemented by subclasses.
ID3D11ComputeShader * mComputeShader
void doSet(void *target, const String &val)
vector< String * >::type mSerStrings
void compileMicrocode(void)
D3D11VertexDeclaration & getInputVertexDeclaration()
ID3D11DomainShader * getDomainShader(void) const
vector< UINT >::type InterfaceSlots
BufferInfo & operator=(const String &name)
stdext::hash_compare< _StringBase, std::less< _StringBase > > _StringHash
Definition: OgreString.h:215
const D3D11_SIGNATURE_PARAMETER_DESC & getOutputParamDesc(unsigned int index) const
#define INVALID_IDX
void doSet(void *target, const String &val)
void setEnableBackwardsCompatibility(bool enableBackwardsCompatibility)
Sets whether backwards compatibility is enabled.
vector< D3D11_SHADER_VARIABLE_DESC >::type D3d11ShaderVariables
vector< D3D11_SIGNATURE_PARAMETER_DESC >::type D3d11ShaderParameters
bool operator==(const BufferInfo &other) const
ID3D11HullShader * getHullShader(void) const
D3D11HLSLProgram(ResourceManager *creator, const String &name, ResourceHandle handle, const String &group, bool isManual, ManualResourceLoader *loader, D3D11Device &device)
Interface describing a manual resource loader.
Definition: OgreResource.h:498
void setTarget(const String &target)
Sets the shader target to compile down to, e.g.
void doSet(void *target, const String &val)
ShaderVarWithPosInBuf & operator=(const ShaderVarWithPosInBuf &var)
String doGet(const void *target) const
ID3D11GeometryShader * getGeometryShader(void) const
const String & getLanguage(void) const
Overridden from GpuProgram.
const MicroCode & getMicroCode(void) const
void unloadHighLevelImpl(void)
Internal unload implementation, must be implemented by subclasses.
ID3D11VertexShader * mVertexShader
Command object for setting macro defines.
void createConstantBuffer(const UINT ByteWidth)
D3d11ShaderParameters mD3d11ShaderInputParameters
Command object for setting entry point.
std::map< String, unsigned int > SlotMap
vector< GpuConstantDefinitionWithName >::type D3d11ShaderVariableSubparts
std::set< BufferInfo >::iterator BufferInfoIterator
vector< byte >::type MicroCode
ID3D11VertexShader * getVertexShader(void) const
D3d11ShaderVariables mD3d11ShaderVariables
Command object for setting target assembler.
static CmdEntryPoint msCmdEntryPoint
static CmdTarget msCmdTarget
ID3D11DomainShader * mDomainShader
String doGet(const void *target) const
D3d11ShaderVariables::iterator D3d11ShaderVariablesIter
ID3D11PixelShader * mPixelShader
void getMicrocodeFromCache(void)
void setNull(void)
ID3D11GeometryShader * mGeometryShader
ID3D11PixelShader * getPixelShader(void) const
GpuProgramParametersSharedPtr createParameters(void)
Overridden from GpuProgram.
ID3D11HullShader * mHullShader
D3d11ShaderBufferDescs mD3d11ShaderBufferDescs
ShaderVars::const_iterator ShaderVarsConstIter
void processParamElement(String prefix, LPCSTR pName, ID3D11ShaderReflectionType *varRefType)
D3d11ShaderParameters mD3d11ShaderOutputParameters
void populateParameterNames(GpuProgramParametersSharedPtr params)
Populate the passed parameters with name->index map, must be overridden.
void getConstantBuffers(ID3D11Buffer **buffers, unsigned int &numBuffers, ID3D11ClassInstance **classes, unsigned int &numInstances, GpuProgramParametersSharedPtr params, uint16 variabilityMask)
String doGet(const void *target) const
void doSet(void *target, const String &val)
String getNameForMicrocodeCache()
vector< D3D11_SHADER_TYPE_DESC >::type D3d11ShaderTypeDescs
BufferInfo & operator=(const BufferInfo &info)
unsigned int getSubroutineSlot(const String &subroutineSlotName) const
std::set< BufferInfo > BufferInfoMap
unsigned int getNumOutputs(void) const
ID3D11ComputeShader * getComputeShader(void) const
D3D_SHADER_MACRO * mShaderMacros
unsigned long long int ResourceHandle
Definition: OgreResource.h:39
bool isSupported(void) const
Overridden from GpuProgram.
const D3D11_SIGNATURE_PARAMETER_DESC & getInputParamDesc(unsigned int index) const
bool getColumnMajorMatrices(void) const
Gets whether matrix packed in column-major order.
void setEntryPoint(const String &entryPoint)
Sets the entry point for this program ie the first method called.
bool getEnableBackwardsCompatibility(void) const
Gets whether backwards compatibility is enabled.
static CmdColumnMajorMatrices msCmdColumnMajorMatrices
void populateDef(D3D11_SHADER_TYPE_DESC &d3dDesc, GpuConstantDefinition &def) const
void setPreprocessorDefines(const String &defines)
Sets the preprocessor defines use to compile the program.
Defines a generic resource handler.
void fixVariableNameFromCg(const ShaderVarWithPosInBuf &newVar)
std::map< String, unsigned int >::const_iterator SlotIterator
Specialisation of VertexDeclaration for D3D11.
BufferInfo(unsigned int index, const String &name)
ShaderVars::iterator ShaderVarsIter
Information about predefined program constants.
bool operator<(const BufferInfo &other) const
void setShaderMacros(D3D_SHADER_MACRO *shaderMacros)
Sets shader macros created manually.
_StringBase String
Definition: OgreCommon.h:53
void reinterpretGSForStreamOut(void)
Command object for setting matrix packing in column-major order.
HardwareUniformBufferSharedPtr mUniformBuffer
unsigned short uint16
Definition: OgrePlatform.h:421
Abstract base class representing a high-level program (a vertex or fragment program).
const String & getTarget(void) const
Gets the shader target to compile down to, e.g.
BufferInfo & operator=(unsigned int index)
static CmdEnableBackwardsCompatibility msCmdEnableBackwardsCompatibility
void setColumnMajorMatrices(bool columnMajor)
Sets whether matrix packing in column-major order.
virtual void buildConstantDefinitions() const
Build the constant definition map, must be overridden.
const String & getCompatibleTarget(void) const
Gets the shader target promoted to the first compatible, e.g.
D3d11ShaderVariables mVarDescPointer
Specialization of HighLevelGpuProgram to provide support for D3D11 High-Level Shader Language (HLSL)...
D3d11ShaderTypeDescs mMemberTypeDesc
vector< MemberTypeName >::type MemberTypeNames
D3d11ShaderParameters::iterator D3d11ShaderParametersIter
Abstract class which is command object which gets/sets parameters.
const String & getPreprocessorDefines(void) const
Sets the preprocessor defines use to compile the program.
static CmdPreprocessorDefines msCmdPreprocessorDefines
const String & getEntryPoint(void) const
Gets the entry point defined for this program.