Torque3D Documentation / _generateds / gfxD3D9Shader.h

gfxD3D9Shader.h

Engine/source/gfx/D3D9/gfxD3D9Shader.h

More...

Classes:

class

The D3D9 implementation of a shader.

class

The D3D9 implementation of a shader constant buffer.

class

The D3D9 implementation of a shader constant handle.

Public Typedefs

_gfxD3DXIncludeRef 

Detailed Description

Public Typedefs

typedef StrongRefPtr< _gfxD3DXInclude > _gfxD3DXIncludeRef 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _GFXD3D9SHADER_H_
 25#define _GFXD3D9SHADER_H_
 26
 27#ifndef _PATH_H_
 28#include "core/util/path.h"
 29#endif
 30#ifndef _TDICTIONARY_H_
 31#include "core/util/tDictionary.h"
 32#endif
 33#ifndef _GFXSHADER_H_
 34#include "gfx/gfxShader.h"
 35#endif
 36#ifndef _GFXRESOURCE_H_
 37#include "gfx/gfxResource.h"
 38#endif
 39#ifndef _GENERICCONSTBUFFER_H_
 40#include "gfx/genericConstBuffer.h"
 41#endif
 42
 43
 44class GFXD3D9Shader;
 45struct IDirect3DVertexShader9;
 46struct IDirect3DPixelShader9;
 47struct IDirect3DDevice9;
 48struct ID3DXConstantTable;
 49struct ID3DXBuffer;
 50struct _D3DXMACRO;
 51
 52
 53class GFXD3D9ShaderBufferLayout : public GenericConstBufferLayout
 54{
 55protected:
 56   /// Set a matrix, given a base pointer
 57   virtual bool setMatrix(const ParamDesc& pd, const GFXShaderConstType constType, const U32 size, const void* data, U8* basePointer);
 58};
 59
 60
 61/// The D3D9 implementation of a shader constant handle.
 62class GFXD3D9ShaderConstHandle : public GFXShaderConstHandle
 63{
 64public:   
 65
 66   // GFXShaderConstHandle
 67   const String& getName() const;
 68   GFXShaderConstType getType() const;
 69   U32 getArraySize() const;
 70
 71   WeakRefPtr<GFXD3D9Shader> mShader;
 72
 73   bool mVertexConstant;
 74   GenericConstBufferLayout::ParamDesc mVertexHandle;
 75   bool mPixelConstant;
 76   GenericConstBufferLayout::ParamDesc mPixelHandle;
 77   
 78   /// Is true if this constant is for hardware mesh instancing.
 79   ///
 80   /// Note: We currently store its settings in mPixelHandle.
 81   ///
 82   bool mInstancingConstant;
 83
 84   void setValid( bool valid ) { mValid = valid; }
 85   S32 getSamplerRegister() const;
 86
 87   // Returns true if this is a handle to a sampler register.
 88   bool isSampler() const 
 89   {
 90      return ( mPixelConstant && mPixelHandle.constType >= GFXSCT_Sampler ) ||
 91             ( mVertexConstant && mVertexHandle.constType >= GFXSCT_Sampler );
 92   }
 93
 94   /// Restore to uninitialized state.
 95   void clear()
 96   {
 97      mShader = NULL;
 98      mVertexConstant = false;
 99      mPixelConstant = false;
100      mInstancingConstant = false;
101      mVertexHandle.clear();
102      mPixelHandle.clear();
103      mValid = false;
104   }
105
106   GFXD3D9ShaderConstHandle();
107};
108
109
110/// The D3D9 implementation of a shader constant buffer.
111class GFXD3D9ShaderConstBuffer : public GFXShaderConstBuffer
112{
113   friend class GFXD3D9Shader;
114
115public:
116
117   GFXD3D9ShaderConstBuffer( GFXD3D9Shader* shader,
118                             GFXD3D9ShaderBufferLayout* vertexLayoutF, 
119                             GFXD3D9ShaderBufferLayout* vertexLayoutI,
120                             GFXD3D9ShaderBufferLayout* pixelLayoutF, 
121                             GFXD3D9ShaderBufferLayout* pixelLayoutI );
122   virtual ~GFXD3D9ShaderConstBuffer();   
123
124   /// Called by GFXD3D9Device to activate this buffer.
125   /// @param mPrevShaderBuffer The previously active buffer
126   void activate( GFXD3D9ShaderConstBuffer *prevShaderBuffer );
127   
128   /// Used internally by GXD3D9ShaderConstBuffer to determine if it's dirty.
129   bool isDirty();
130
131   /// Called from GFXD3D9Shader when constants have changed and need
132   /// to be the shader this buffer references is reloaded.
133   void onShaderReload( GFXD3D9Shader *shader );
134
135   // GFXShaderConstBuffer
136   virtual GFXShader* getShader();
137   virtual void set(GFXShaderConstHandle* handle, const F32 fv);
138   virtual void set(GFXShaderConstHandle* handle, const Point2F& fv);
139   virtual void set(GFXShaderConstHandle* handle, const Point3F& fv);
140   virtual void set(GFXShaderConstHandle* handle, const Point4F& fv);
141   virtual void set(GFXShaderConstHandle* handle, const PlaneF& fv);
142   virtual void set(GFXShaderConstHandle* handle, const ColorF& fv);   
143   virtual void set(GFXShaderConstHandle* handle, const S32 f);
144   virtual void set(GFXShaderConstHandle* handle, const Point2I& fv);
145   virtual void set(GFXShaderConstHandle* handle, const Point3I& fv);
146   virtual void set(GFXShaderConstHandle* handle, const Point4I& fv);
147   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<F32>& fv);
148   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point2F>& fv);
149   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point3F>& fv);
150   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point4F>& fv);   
151   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<S32>& fv);
152   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point2I>& fv);
153   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point3I>& fv);
154   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point4I>& fv);
155   virtual void set(GFXShaderConstHandle* handle, const MatrixF& mat, const GFXShaderConstType matType = GFXSCT_Float4x4);
156   virtual void set(GFXShaderConstHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType = GFXSCT_Float4x4);
157   
158   // GFXResource
159   virtual const String describeSelf() const;
160   virtual void zombify();
161   virtual void resurrect();
162
163protected:
164
165   template<class T>
166   inline void SET_CONSTANT(  GFXShaderConstHandle* handle, 
167                              const T& fv, 
168                              GenericConstBuffer *vBuffer, 
169                              GenericConstBuffer *pBuffer );
170
171   /// A cached direct pointer to the device.
172   IDirect3DDevice9 *mDevice;
173
174   /// We keep a weak reference to the shader 
175   /// because it will often be deleted.
176   WeakRefPtr<GFXD3D9Shader> mShader;
177   
178   GFXD3D9ShaderBufferLayout* mVertexConstBufferLayoutF;
179   GenericConstBuffer* mVertexConstBufferF;
180   GFXD3D9ShaderBufferLayout* mPixelConstBufferLayoutF;
181   GenericConstBuffer* mPixelConstBufferF;   
182   GFXD3D9ShaderBufferLayout* mVertexConstBufferLayoutI;
183   GenericConstBuffer* mVertexConstBufferI;
184   GFXD3D9ShaderBufferLayout* mPixelConstBufferLayoutI;
185   GenericConstBuffer* mPixelConstBufferI;   
186};
187
188
189class _gfxD3DXInclude;
190typedef StrongRefPtr<_gfxD3DXInclude> _gfxD3DXIncludeRef;
191
192/// The D3D9 implementation of a shader.
193class GFXD3D9Shader : public GFXShader
194{
195   friend class GFXD3D9Device;
196   friend class GFX360Device;
197   friend class GFXD3D9ShaderConstBuffer;
198   friend class GFX360ShaderConstBuffer;
199public:
200   typedef Map<String, GFXD3D9ShaderConstHandle*> HandleMap;
201
202   GFXD3D9Shader();
203   virtual ~GFXD3D9Shader();   
204
205   // GFXShader
206   virtual GFXShaderConstBufferRef allocConstBuffer();
207   virtual const Vector<GFXShaderConstDesc>& getShaderConstDesc() const;
208   virtual GFXShaderConstHandle* getShaderConstHandle(const String& name); 
209   virtual GFXShaderConstHandle* findShaderConstHandle(const String& name);
210   virtual U32 getAlignmentValue(const GFXShaderConstType constType) const;
211   virtual bool getDisassembly( String &outStr ) const;
212
213   // GFXResource
214   virtual void zombify();
215   virtual void resurrect();
216
217protected:
218
219   virtual bool _init();   
220
221   static const U32 smCompiledShaderTag;
222
223   IDirect3DDevice9 *mD3D9Device;
224
225   IDirect3DVertexShader9 *mVertShader;
226   IDirect3DPixelShader9 *mPixShader;
227
228   GFXD3D9ShaderBufferLayout* mVertexConstBufferLayoutF;   
229   GFXD3D9ShaderBufferLayout* mPixelConstBufferLayoutF;
230   GFXD3D9ShaderBufferLayout* mVertexConstBufferLayoutI;   
231   GFXD3D9ShaderBufferLayout* mPixelConstBufferLayoutI;
232
233   static _gfxD3DXIncludeRef smD3DXInclude;
234
235   HandleMap mHandles;
236
237   /// The shader disassembly from DX when this shader is compiled.
238   /// We only store this data in non-release builds.
239   String mDissasembly;
240
241   /// Vector of sampler type descriptions consolidated from _compileShader.
242   Vector<GFXShaderConstDesc> mSamplerDescriptions;
243
244   /// Vector of descriptions (consolidated for the getShaderConstDesc call)
245   Vector<GFXShaderConstDesc> mShaderConsts;
246   
247   // These two functions are used when compiling shaders from hlsl
248   virtual bool _compileShader( const Torque::Path &filePath, 
249                                const String &target, 
250                                const _D3DXMACRO *defines, 
251                                GenericConstBufferLayout *bufferLayoutF, 
252                                GenericConstBufferLayout *bufferLayoutI,
253                                Vector<GFXShaderConstDesc> &samplerDescriptions );
254
255   void _getShaderConstants( ID3DXConstantTable* table, 
256                             GenericConstBufferLayout *bufferLayoutF, 
257                             GenericConstBufferLayout *bufferLayoutI,
258                             Vector<GFXShaderConstDesc> &samplerDescriptions );
259
260   bool _saveCompiledOutput( const Torque::Path &filePath, 
261                             ID3DXBuffer *buffer, 
262                             GenericConstBufferLayout *bufferLayoutF, 
263                             GenericConstBufferLayout *bufferLayoutI,
264                             Vector<GFXShaderConstDesc> &samplerDescriptions );
265
266   // Loads precompiled shaders
267   bool _loadCompiledOutput( const Torque::Path &filePath, 
268                             const String &target, 
269                             GenericConstBufferLayout *bufferLayoutF, 
270                             GenericConstBufferLayout *bufferLayoutI,
271                             Vector<GFXShaderConstDesc> &samplerDescriptions );
272
273   // This is used in both cases
274   virtual void _buildShaderConstantHandles( GenericConstBufferLayout *layout, bool vertexConst );
275   
276   virtual void _buildSamplerShaderConstantHandles( Vector<GFXShaderConstDesc> &samplerDescriptions );
277
278   /// Used to build the instancing shader constants from 
279   /// the instancing vertex format.
280   void _buildInstancingShaderConstantHandles();
281};
282
283inline bool GFXD3D9Shader::getDisassembly( String &outStr ) const
284{
285   outStr = mDissasembly;
286   return ( outStr.isNotEmpty() );
287}
288
289#endif // _GFXD3D9SHADER_H_
290