gfxD3D9Shader.h 11 KB

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