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 
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<std::string, unsigned int> SlotMap;
238  typedef std::map<std::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
void populateParameterNames(GpuProgramParametersSharedPtr params)
Populate the passed parameters with name->index map, must be overridden.
bool getColumnMajorMatrices(void) const
Gets whether matrix packed in column-major order.
ID3D11Buffer * getConstantBuffer(GpuProgramParametersSharedPtr params, uint16 variabilityMask)
void getMicrocodeFromCache(void)
ShaderVars::iterator ShaderVarsIter
String getNameForMicrocodeCache()
D3d11ShaderVariables::iterator D3d11ShaderVariablesIter
void getConstantBuffers(ID3D11Buffer **buffers, unsigned int &numBuffers, ID3D11ClassInstance **classes, unsigned int &numInstances, GpuProgramParametersSharedPtr params, uint16 variabilityMask)
void doSet(void *target, const String &val)
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
void fixVariableNameFromCg(const ShaderVarWithPosInBuf &newVar)
unsigned int getNumOutputs(void) const
const String & getPreprocessorDefines(void) const
Sets the preprocessor defines use to compile the program.
vector< UINT >::type InterfaceSlots
Command object for setting entry point.
void setEnableBackwardsCompatibility(bool enableBackwardsCompatibility)
Sets whether backwards compatibility is enabled.
vector< MemberTypeName >::type MemberTypeNames
unsigned int getNumInputs(void) const
static CmdEnableBackwardsCompatibility msCmdEnableBackwardsCompatibility
#define INVALID_IDX
D3D11HLSLProgram(ResourceManager *creator, const String &name, ResourceHandle handle, const String &group, bool isManual, ManualResourceLoader *loader, D3D11Device &device)
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.
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
bool isSupported(void) const
Overridden from GpuProgram.
void processParamElement(String prefix, LPCSTR pName, ID3D11ShaderReflectionType *varRefType)
bool getEnableBackwardsCompatibility(void) const
Gets whether backwards compatibility is enabled.
HardwareUniformBufferSharedPtr mUniformBuffer
const D3D11_SIGNATURE_PARAMETER_DESC & getOutputParamDesc(unsigned int index) const
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...
String doGet(const void *target) const
std::map< std::string, unsigned int >::const_iterator SlotIterator
bool operator==(const BufferInfo &other) const
ID3D11GeometryShader * getGeometryShader(void) const
const String & getCompatibleTarget(void) const
Gets the shader target promoted to the first compatible, e.g.
void unloadHighLevelImpl(void)
Internal unload implementation, must be implemented by subclasses.
const String & getLanguage(void) const
Overridden from GpuProgram.
BufferInfo(unsigned int index, const String &name)
const String & getEntryPoint(void) const
Gets the entry point defined for this program.
static CmdPreprocessorDefines msCmdPreprocessorDefines
ID3D11PixelShader * getPixelShader(void) const
String doGet(const void *target) const
static CmdEntryPoint msCmdEntryPoint
Specialization of HighLevelGpuProgram to provide support for D3D11 High-Level Shader Language (HLSL)...
const String & getTarget(void) const
Gets the shader target to compile down to, e.g.
Command object for setting matrix packing in column-major order.
ID3D11HullShader * getHullShader(void) const
void setEntryPoint(const String &entryPoint)
Sets the entry point for this program ie the first method called.
void compileMicrocode(void)
D3d11ShaderParameters mD3d11ShaderOutputParameters
static CmdColumnMajorMatrices msCmdColumnMajorMatrices
_StringBase String
BufferInfo & operator=(unsigned int index)
ID3D11GeometryShader * mGeometryShader
BufferInfo & operator=(const String &name)
String doGet(const void *target) const
D3d11ShaderVariables mD3d11ShaderVariables
D3D11VertexDeclaration mInputVertexDeclaration
D3d11ShaderVariableSubparts mD3d11ShaderVariableSubparts
D3d11ShaderParameters::iterator D3d11ShaderParametersIter
vector< D3D11_SHADER_BUFFER_DESC >::type D3d11ShaderBufferDescs
D3d11ShaderVariableSubparts::iterator D3d11ShaderVariableSubpartsIter
D3d11ShaderTypeDescs mD3d11ShaderTypeDescs
ID3D11ComputeShader * mComputeShader
void doSet(void *target, const String &val)
void createConstantBuffer(const UINT ByteWidth)
bool operator<(const BufferInfo &other) const
void loadFromSource(void)
Internal load implementation, must be implemented by subclasses.
unsigned int getSubroutineSlot(const String &subroutineSlotName) const
Defines a generic resource handler.
vector< GpuConstantDefinitionWithName >::type D3d11ShaderVariableSubparts
ID3D11VertexShader * getVertexShader(void) const
ID3D11HullShader * mHullShader
ID3D11ComputeShader * getComputeShader(void) const
const D3D11_SIGNATURE_PARAMETER_DESC & getInputParamDesc(unsigned int index) const
ShaderVarWithPosInBuf & operator=(const ShaderVarWithPosInBuf &var)
D3d11ShaderVariables mVarDescPointer
ID3D11PixelShader * mPixelShader
virtual void buildConstantDefinitions() const
Build the constant definition map, must be overridden.
vector< byte >::type MicroCode
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
ID3D11DomainShader * getDomainShader(void) const
void doSet(void *target, const String &val)
String doGet(const void *target) const
vector< D3D11_SHADER_VARIABLE_DESC >::type D3d11ShaderVariables
BufferInfo & operator=(const BufferInfo &info)
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
void populateDef(D3D11_SHADER_TYPE_DESC &d3dDesc, GpuConstantDefinition &def) const
const MicroCode & getMicroCode(void) const
std::set< BufferInfo > BufferInfoMap
void doSet(void *target, const String &val)
void doSet(void *target, const String &val)
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 Tue Mar 18 2014 19:15:24