gfxD3D11Shader.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2015 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 _GFXD3D11SHADER_H_
  23. #define _GFXD3D11SHADER_H_
  24. #include <d3dcompiler.h>
  25. #include "core/util/path.h"
  26. #include "core/util/tDictionary.h"
  27. #include "gfx/gfxShader.h"
  28. #include "gfx/gfxResource.h"
  29. #include "gfx/D3D11/gfxD3D11Device.h"
  30. class GFXD3D11Shader;
  31. typedef CompoundKey<U32, U32> BufferKey;
  32. struct BufferRange
  33. {
  34. U32 mBufMin = U32_MAX;
  35. U32 mBufMax = 0;
  36. inline void addSlot(U32 slot)
  37. {
  38. mBufMin = getMin(mBufMin, slot);
  39. mBufMax = getMax(mBufMax, slot);
  40. }
  41. inline bool isValid() const { return mBufMin <= mBufMax; }
  42. };
  43. struct ConstantBuffer
  44. {
  45. U8* data;
  46. U32 size;
  47. bool isDirty;
  48. };
  49. class GFXD3D11ShaderConstHandle : public GFXShaderConstHandle
  50. {
  51. friend class GFXD3D11Shader;
  52. public:
  53. typedef Map<GFXShaderStage, GFXShaderConstDesc> DescMap;
  54. GFXD3D11ShaderConstHandle(GFXD3D11Shader* shader);
  55. GFXD3D11ShaderConstHandle(GFXD3D11Shader* shader,
  56. const GFXShaderConstDesc& desc);
  57. virtual ~GFXD3D11ShaderConstHandle();
  58. void addDesc(GFXShaderStage stage, const GFXShaderConstDesc& desc);
  59. const GFXShaderConstDesc getDesc(GFXShaderStage stage);
  60. const String& getName() const override { return mDesc.name; }
  61. GFXShaderConstType getType() const override { return mDesc.constType; }
  62. U32 getArraySize() const override { return mDesc.arraySize; }
  63. U32 getSize() const { return mDesc.size; }
  64. void setValid(bool valid) { mValid = valid; }
  65. /// @warning This will always return the value assigned when the shader was
  66. /// initialized. If the value is later changed this method won't reflect that.
  67. S32 getSamplerRegister() const override { return (!isSampler() || !mValid) ? -1 : mDesc.samplerReg; }
  68. // Returns true if this is a handle to a sampler register.
  69. bool isSampler() const
  70. {
  71. return (getType() >= GFXSCT_Sampler);
  72. }
  73. /// Restore to uninitialized state.
  74. void clear()
  75. {
  76. mShader = NULL;
  77. mInstancingConstant = false;
  78. mValid = false;
  79. }
  80. GFXShaderConstDesc mDesc;
  81. GFXD3D11Shader* mShader;
  82. DescMap mDescMap;
  83. U32 mStageFlags;
  84. bool mInstancingConstant;
  85. };
  86. /// The D3D11 implementation of a shader constant buffer.
  87. class GFXD3D11ShaderConstBuffer : public GFXShaderConstBuffer
  88. {
  89. // Cache device context
  90. ID3D11DeviceContext* mDeviceContext;
  91. public:
  92. typedef Map<BufferKey, ConstantBuffer> BufferMap;
  93. GFXD3D11ShaderConstBuffer(GFXD3D11Shader* shader);
  94. virtual ~GFXD3D11ShaderConstBuffer();
  95. /// Called by GFXD3D11Device to activate this buffer.
  96. /// @param mPrevShaderBuffer The previously active buffer
  97. void activate(GFXD3D11ShaderConstBuffer *prevShaderBuffer);
  98. void addBuffer(const GFXShaderConstDesc desc);
  99. /// Called from GFXD3D11Shader when constants have changed and need
  100. /// to be the shader this buffer references is reloaded.
  101. void onShaderReload(GFXD3D11Shader *shader);
  102. // GFXShaderConstBuffer
  103. GFXShader* getShader() override;
  104. void set(GFXShaderConstHandle* handle, const F32 fv) override;
  105. void set(GFXShaderConstHandle* handle, const Point2F& fv) override;
  106. void set(GFXShaderConstHandle* handle, const Point3F& fv) override;
  107. void set(GFXShaderConstHandle* handle, const Point4F& fv) override;
  108. void set(GFXShaderConstHandle* handle, const PlaneF& fv) override;
  109. void set(GFXShaderConstHandle* handle, const LinearColorF& fv) override;
  110. void set(GFXShaderConstHandle* handle, const S32 f) override;
  111. void set(GFXShaderConstHandle* handle, const Point2I& fv) override;
  112. void set(GFXShaderConstHandle* handle, const Point3I& fv) override;
  113. void set(GFXShaderConstHandle* handle, const Point4I& fv) override;
  114. void set(GFXShaderConstHandle* handle, const AlignedArray<F32>& fv) override;
  115. void set(GFXShaderConstHandle* handle, const AlignedArray<Point2F>& fv) override;
  116. void set(GFXShaderConstHandle* handle, const AlignedArray<Point3F>& fv) override;
  117. void set(GFXShaderConstHandle* handle, const AlignedArray<Point4F>& fv) override;
  118. void set(GFXShaderConstHandle* handle, const AlignedArray<S32>& fv) override;
  119. void set(GFXShaderConstHandle* handle, const AlignedArray<Point2I>& fv) override;
  120. void set(GFXShaderConstHandle* handle, const AlignedArray<Point3I>& fv) override;
  121. void set(GFXShaderConstHandle* handle, const AlignedArray<Point4I>& fv) override;
  122. void set(GFXShaderConstHandle* handle, const MatrixF& mat, const GFXShaderConstType matType = GFXSCT_Float4x4) override;
  123. void set(GFXShaderConstHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType = GFXSCT_Float4x4) override;
  124. // GFXResource
  125. const String describeSelf() const override;
  126. void zombify() override {}
  127. void resurrect() override {}
  128. protected:
  129. friend class GFXD3D11Shader;
  130. /// We keep a weak reference to the shader
  131. /// because it will often be deleted.
  132. WeakRefPtr<GFXD3D11Shader> mShader;
  133. BufferMap mBufferMap;
  134. void setMatrix(const GFXShaderConstDesc& handle, const U32 inSize, const void* data, U8* basePointer);
  135. void internalSet(GFXShaderConstHandle* handle, const U32 inSize, const void* data);
  136. ID3D11Buffer* mBoundBuffers[6][16];
  137. };
  138. class gfxD3D11Include;
  139. typedef StrongRefPtr<gfxD3D11Include> gfxD3DIncludeRef;
  140. /////////////////// GFXShader implementation /////////////////////////////
  141. class GFXD3D11Shader : public GFXShader
  142. {
  143. friend class GFXD3D11Device;
  144. friend class GFXD3D11ShaderConstBuffer;
  145. public:
  146. typedef Map<String, GFXD3D11ShaderConstHandle*> HandleMap;
  147. typedef Map<String, GFXShaderConstDesc> BufferMap;
  148. GFXD3D11Shader();
  149. virtual ~GFXD3D11Shader();
  150. // GFXShader
  151. GFXShaderConstBufferRef allocConstBuffer() override;
  152. const Vector<GFXShaderConstDesc>& getShaderConstDesc() const override;
  153. GFXShaderConstHandle* getShaderConstHandle(const String& name) override;
  154. GFXShaderConstHandle* findShaderConstHandle(const String& name) override;
  155. U32 getAlignmentValue(const GFXShaderConstType constType) const override;
  156. // GFXResource
  157. void zombify() override;
  158. void resurrect() override;
  159. protected:
  160. bool _init() override;
  161. ID3D11VertexShader *mVertShader;
  162. ID3D11PixelShader *mPixShader;
  163. ID3D11GeometryShader *mGeoShader;
  164. static gfxD3DIncludeRef smD3DInclude;
  165. HandleMap mHandles;
  166. BufferMap mBuffers;
  167. /// Vector of descriptions (consolidated for the getShaderConstDesc call)
  168. Vector<GFXShaderConstDesc> mShaderConsts;
  169. Vector<GFXShaderConstDesc> mSamplerDescriptions;
  170. // These two functions are used when compiling shaders from hlsl
  171. virtual bool _compileShader( const Torque::Path &filePath,
  172. GFXShaderStage shaderStage,
  173. const D3D_SHADER_MACRO *defines);
  174. void _getShaderConstants( ID3D11ShaderReflection* refTable,
  175. GFXShaderStage shaderStage);
  176. // This is used in both cases
  177. virtual void _buildShaderConstantHandles();
  178. void _buildInstancingShaderConstantHandles();
  179. GFXShaderConstType convertConstType(D3D11_SHADER_TYPE_DESC typeDesc);
  180. };
  181. #endif