processedShaderMaterial.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 _MATERIALS_PROCESSEDSHADERMATERIAL_H_
  23. #define _MATERIALS_PROCESSEDSHADERMATERIAL_H_
  24. #ifndef _MATERIALS_PROCESSEDMATERIAL_H_
  25. #include "processedMaterial.h"
  26. #endif
  27. #ifndef _GFXSHADER_H_
  28. #include "gfx/gfxShader.h"
  29. #endif
  30. #ifndef CUSTOMSHADERBINDINGDATA_H
  31. #include "materials/customShaderBindingData.h"
  32. #endif
  33. class GenericConstBufferLayout;
  34. class ShaderData;
  35. class LightInfo;
  36. class ShaderMaterialParameterHandle;
  37. class ShaderFeatureConstHandles;
  38. class CustomMaterial;
  39. class ShaderConstHandles
  40. {
  41. public:
  42. GFXShaderConstHandle* mDiffuseColorSC;
  43. GFXShaderConstHandle* mToneMapTexSC;
  44. GFXShaderConstHandle* mTexMatSC;
  45. GFXShaderConstHandle* mORMConfigSC;
  46. GFXShaderConstHandle* mRoughnessSC;
  47. GFXShaderConstHandle* mMetalnessSC;
  48. GFXShaderConstHandle* mGlowMulSC;
  49. GFXShaderConstHandle* mParallaxInfoSC;
  50. GFXShaderConstHandle* mAccuScaleSC;
  51. GFXShaderConstHandle* mAccuDirectionSC;
  52. GFXShaderConstHandle* mAccuStrengthSC;
  53. GFXShaderConstHandle* mAccuCoverageSC;
  54. GFXShaderConstHandle* mAccuSpecularSC;
  55. GFXShaderConstHandle* mFogDataSC;
  56. GFXShaderConstHandle* mFogColorSC;
  57. GFXShaderConstHandle* mDetailScaleSC;
  58. GFXShaderConstHandle* mVisiblitySC;
  59. GFXShaderConstHandle* mColorMultiplySC;
  60. GFXShaderConstHandle* mAlphaTestValueSC;
  61. GFXShaderConstHandle* mModelViewProjSC;
  62. GFXShaderConstHandle* mWorldViewOnlySC;
  63. GFXShaderConstHandle* mWorldToCameraSC;
  64. GFXShaderConstHandle* mCameraToWorldSC;
  65. GFXShaderConstHandle* mWorldToObjSC;
  66. GFXShaderConstHandle* mViewToObjSC;
  67. GFXShaderConstHandle* mInvCameraTransSC;
  68. GFXShaderConstHandle* mCameraToScreenSC;
  69. GFXShaderConstHandle* mScreenToCameraSC;
  70. GFXShaderConstHandle* mCubeTransSC;
  71. GFXShaderConstHandle* mCubeMipsSC;
  72. GFXShaderConstHandle* mObjTransSC;
  73. GFXShaderConstHandle* mCubeEyePosSC;
  74. GFXShaderConstHandle* mEyePosSC;
  75. GFXShaderConstHandle* mEyePosWorldSC;
  76. GFXShaderConstHandle* m_vEyeSC;
  77. GFXShaderConstHandle* mEyeMatSC;
  78. GFXShaderConstHandle* mOneOverFarplane;
  79. GFXShaderConstHandle* mAccumTimeSC;
  80. GFXShaderConstHandle* mDampnessSC;
  81. GFXShaderConstHandle* mMinnaertConstantSC;
  82. GFXShaderConstHandle* mSubSurfaceParamsSC;
  83. GFXShaderConstHandle* mDiffuseAtlasParamsSC;
  84. GFXShaderConstHandle* mBumpAtlasParamsSC;
  85. GFXShaderConstHandle* mDiffuseAtlasTileSC;
  86. GFXShaderConstHandle* mBumpAtlasTileSC;
  87. GFXShaderConstHandle *mRTSizeSC;
  88. GFXShaderConstHandle *mOneOverRTSizeSC;
  89. GFXShaderConstHandle* mDetailBumpStrength;
  90. GFXShaderConstHandle* mViewProjSC;
  91. GFXShaderConstHandle *mImposterUVs;
  92. GFXShaderConstHandle *mImposterLimits;
  93. // Deferred Shading : Material Info Flags
  94. GFXShaderConstHandle* mMatInfoFlagsSC;
  95. GFXShaderConstHandle* mTexHandlesSC[Material::MAX_TEX_PER_PASS];
  96. GFXShaderConstHandle* mRTParamsSC[GFX_TEXTURE_STAGE_COUNT];
  97. GFXShaderConstHandle* mNodeTransforms;
  98. GFXShaderConstHandle* mIsCapturingSC;
  99. struct customHandleData
  100. {
  101. StringTableEntry handleName;
  102. GFXShaderConstHandle* handle;
  103. };
  104. Vector<customHandleData> mCustomHandles;
  105. void init( GFXShader* shader, CustomMaterial* mat = NULL);
  106. };
  107. class ShaderRenderPassData : public RenderPassData
  108. {
  109. typedef RenderPassData Parent;
  110. public:
  111. virtual ~ShaderRenderPassData() { reset(); }
  112. GFXShaderRef shader;
  113. ShaderConstHandles shaderHandles;
  114. Vector<ShaderFeatureConstHandles*> featureShaderHandles;
  115. void reset() override;
  116. String describeSelf() const override;
  117. };
  118. class ProcessedShaderMaterial : public ProcessedMaterial
  119. {
  120. typedef ProcessedMaterial Parent;
  121. public:
  122. ProcessedShaderMaterial();
  123. ProcessedShaderMaterial(Material &mat);
  124. ~ProcessedShaderMaterial();
  125. // ProcessedMaterial
  126. bool init( const FeatureSet &features,
  127. const GFXVertexFormat *vertexFormat,
  128. const MatFeaturesDelegate &featuresDelegate ) override;
  129. bool setupPass(SceneRenderState *, const SceneData& sgData, U32 pass) override;
  130. void setTextureStages(SceneRenderState *, const SceneData &sgData, U32 pass ) override;
  131. void setTransforms(const MatrixSet &matrixSet, SceneRenderState *state, const U32 pass) override;
  132. void setNodeTransforms(const MatrixF *address, const U32 numTransforms, const U32 pass) override;
  133. void setCustomShaderData(Vector<CustomShaderBindingData> &shaderData, const U32 pass) override;
  134. void setSceneInfo(SceneRenderState *, const SceneData& sgData, U32 pass) override;
  135. void setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer) override;
  136. bool stepInstance() override;
  137. void dumpMaterialInfo() override;
  138. void getMaterialInfo(GuiTreeViewCtrl* tree, U32 item) override;
  139. MaterialParameters* allocMaterialParameters() override;
  140. MaterialParameters* getDefaultMaterialParameters() override { return mDefaultParameters; }
  141. MaterialParameterHandle* getMaterialParameterHandle(const String& name) override;
  142. U32 getNumStages() override;
  143. protected:
  144. Vector<GFXShaderConstDesc> mShaderConstDesc;
  145. MaterialParameters* mDefaultParameters;
  146. Vector<ShaderMaterialParameterHandle*> mParameterHandles;
  147. /// Hold the instancing state data for the material.
  148. class InstancingState
  149. {
  150. const static U32 COUNT = 200;
  151. public:
  152. InstancingState()
  153. : mInstFormat( NULL ),
  154. mBuffer( NULL ),
  155. mCount( -1 )
  156. {
  157. }
  158. ~InstancingState()
  159. {
  160. delete [] mBuffer;
  161. }
  162. void setFormat( const GFXVertexFormat *instFormat, const GFXVertexFormat *vertexFormat )
  163. {
  164. mInstFormat = instFormat;
  165. mDeclFormat.copy( *vertexFormat );
  166. mDeclFormat.append( *mInstFormat, 1 );
  167. // Let the declaration know we have instancing.
  168. mDeclFormat.enableInstancing();
  169. mDeclFormat.getDecl();
  170. delete [] mBuffer;
  171. mBuffer = new U8[ mInstFormat->getSizeInBytes() * COUNT ];
  172. mCount = -1;
  173. }
  174. bool step( U8 **outPtr )
  175. {
  176. // Are we starting a new draw call?
  177. if ( mCount < 0 )
  178. {
  179. *outPtr = mBuffer;
  180. mCount = 0;
  181. }
  182. else
  183. {
  184. // Increment to the next instance.
  185. *outPtr += mInstFormat->getSizeInBytes();
  186. mCount++;
  187. }
  188. return mCount < COUNT;
  189. }
  190. void resetStep() { mCount = -1; }
  191. U8* getBuffer() const { return mBuffer; }
  192. S32 getCount() const { return mCount; }
  193. const GFXVertexFormat* getFormat() const { return mInstFormat; }
  194. const GFXVertexFormat* getDeclFormat() const { return &mDeclFormat; }
  195. protected:
  196. GFXVertexFormat mDeclFormat;
  197. const GFXVertexFormat *mInstFormat;
  198. U8 *mBuffer;
  199. S32 mCount;
  200. };
  201. /// The instancing state if this material
  202. /// supports instancing.
  203. InstancingState *mInstancingState;
  204. /// @name Internal functions
  205. ///
  206. /// @{
  207. /// Adds a pass for the given stage.
  208. virtual bool _addPass( ShaderRenderPassData &rpd,
  209. U32 &texIndex,
  210. MaterialFeatureData &fd,
  211. U32 stageNum,
  212. const FeatureSet &features);
  213. /// Chooses a blend op for the given pass
  214. virtual void _setPassBlendOp( ShaderFeature *sf,
  215. ShaderRenderPassData &passData,
  216. U32 &texIndex,
  217. MaterialFeatureData &stageFeatures,
  218. U32 stageNum,
  219. const FeatureSet &features);
  220. /// Creates passes for the given stage
  221. virtual bool _createPasses( MaterialFeatureData &fd, U32 stageNum, const FeatureSet &features );
  222. /// Fills in the MaterialFeatureData for the given stage
  223. virtual void _determineFeatures( U32 stageNum,
  224. MaterialFeatureData &fd,
  225. const FeatureSet &features );
  226. /// Do we have a cubemap on pass?
  227. virtual bool _hasCubemap(U32 pass);
  228. /// Used by setTextureTransforms
  229. F32 _getWaveOffset( U32 stage );
  230. /// Sets texture transformation matrices for texture animations such as scale and wave
  231. virtual void _setTextureTransforms(const U32 pass);
  232. /// Sets all of the necessary shader constants for the given pass
  233. virtual void _setShaderConstants(SceneRenderState *, const SceneData &sgData, U32 pass);
  234. /// @}
  235. void _setPrimaryLightConst(const LightInfo* light, const MatrixF& objTrans, const U32 stageNum);
  236. /// This is here to deal with the differences between ProcessedCustomMaterials and ProcessedShaderMaterials.
  237. public:
  238. virtual GFXShaderConstBuffer* _getShaderConstBuffer(const U32 pass);
  239. virtual ShaderConstHandles* _getShaderConstHandles(const U32 pass);
  240. protected:
  241. ///
  242. virtual void _initMaterialParameters();
  243. ShaderRenderPassData* _getRPD(const U32 pass) { return static_cast<ShaderRenderPassData*>(mPasses[pass]); }
  244. };
  245. #endif // _MATERIALS_PROCESSEDSHADERMATERIAL_H_