renderPrePassMgr.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 _PREPASS_MGR_H_
  23. #define _PREPASS_MGR_H_
  24. #include "renderInstance/renderTexTargetBinManager.h"
  25. #include "materials/matInstance.h"
  26. #include "materials/processedShaderMaterial.h"
  27. #include "shaderGen/conditionerFeature.h"
  28. #include "core/util/autoPtr.h"
  29. // Forward declare
  30. class PrePassMatInstance;
  31. // This render manager renders opaque objects to the z-buffer as a z-fill pass.
  32. // It can optionally accumulate data from this opaque render pass into a render
  33. // target for later use.
  34. class RenderPrePassMgr : public RenderTexTargetBinManager
  35. {
  36. typedef RenderTexTargetBinManager Parent;
  37. public:
  38. // registered buffer name
  39. static const String BufferName;
  40. // andremwac: Deferred Rendering
  41. static const String ColorBufferName;
  42. static const String MatInfoBufferName;
  43. // Generic PrePass Render Instance Type
  44. static const RenderInstType RIT_PrePass;
  45. RenderPrePassMgr( bool gatherDepth = true,
  46. GFXFormat format = GFXFormatR16G16B16A16 );
  47. virtual ~RenderPrePassMgr();
  48. virtual void setPrePassMaterial( PrePassMatInstance *mat );
  49. // RenderBinManager interface
  50. virtual void render(SceneRenderState * state);
  51. virtual void sort();
  52. virtual void clear();
  53. virtual void addElement( RenderInst *inst );
  54. // ConsoleObject
  55. DECLARE_CONOBJECT(RenderPrePassMgr);
  56. typedef Signal<void(const SceneRenderState*, RenderPrePassMgr*, bool)> RenderSignal;
  57. static RenderSignal& getRenderSignal();
  58. static const U32 OpaqueStaticLitMask = BIT(1); ///< Stencil mask for opaque, lightmapped pixels
  59. static const U32 OpaqueDynamicLitMask = BIT(0); ///< Stencil mask for opaque, dynamic lit pixels
  60. static const GFXStateBlockDesc &getOpaqueStencilTestDesc();
  61. static const GFXStateBlockDesc &getOpaqueStenciWriteDesc(bool lightmappedGeometry = true);
  62. virtual bool setTargetSize(const Point2I &newTargetSize);
  63. inline BaseMatInstance* getPrePassMaterial( BaseMatInstance *mat );
  64. protected:
  65. /// The terrain render instance elements.
  66. Vector< MainSortElem > mTerrainElementList;
  67. /// The object render instance elements.
  68. Vector< MainSortElem > mObjectElementList;
  69. PrePassMatInstance *mPrePassMatInstance;
  70. virtual void _registerFeatures();
  71. virtual void _unregisterFeatures();
  72. virtual bool _updateTargets();
  73. virtual void _createPrePassMaterial();
  74. bool _lightManagerActivate(bool active);
  75. // Deferred Shading
  76. GFXVertexBufferHandle<GFXVertexPC> mClearGBufferVerts;
  77. GFXShaderRef mClearGBufferShader;
  78. GFXStateBlockRef mStateblock;
  79. NamedTexTarget mColorTarget;
  80. NamedTexTarget mMatInfoTarget;
  81. GFXTexHandle mColorTex;
  82. GFXTexHandle mMatInfoTex;
  83. GFXShaderConstBufferRef mShaderConsts;
  84. GFXShaderConstHandle *mSpecularStrengthSC;
  85. GFXShaderConstHandle *mSpecularPowerSC;
  86. public:
  87. void clearBuffers();
  88. void _initShaders();
  89. };
  90. //------------------------------------------------------------------------------
  91. class ProcessedPrePassMaterial : public ProcessedShaderMaterial
  92. {
  93. typedef ProcessedShaderMaterial Parent;
  94. public:
  95. ProcessedPrePassMaterial(Material& mat, const RenderPrePassMgr *prePassMgr);
  96. virtual U32 getNumStages();
  97. virtual void addStateBlockDesc(const GFXStateBlockDesc& desc);
  98. protected:
  99. virtual void _determineFeatures( U32 stageNum, MaterialFeatureData &fd, const FeatureSet &features );
  100. const RenderPrePassMgr *mPrePassMgr;
  101. bool mIsLightmappedGeometry;
  102. };
  103. //------------------------------------------------------------------------------
  104. class PrePassMatInstance : public MatInstance
  105. {
  106. typedef MatInstance Parent;
  107. public:
  108. PrePassMatInstance(MatInstance* root, const RenderPrePassMgr *prePassMgr);
  109. virtual ~PrePassMatInstance();
  110. bool init()
  111. {
  112. return init( mFeatureList, mVertexFormat );
  113. }
  114. // MatInstance
  115. virtual bool init( const FeatureSet &features,
  116. const GFXVertexFormat *vertexFormat );
  117. protected:
  118. virtual ProcessedMaterial* getShaderMaterial();
  119. const RenderPrePassMgr *mPrePassMgr;
  120. };
  121. //------------------------------------------------------------------------------
  122. class PrePassMatInstanceHook : public MatInstanceHook
  123. {
  124. public:
  125. PrePassMatInstanceHook(MatInstance *baseMatInst, const RenderPrePassMgr *prePassMgr);
  126. virtual ~PrePassMatInstanceHook();
  127. virtual PrePassMatInstance *getPrePassMatInstance() { return mHookedPrePassMatInst; }
  128. virtual const MatInstanceHookType& getType() const { return Type; }
  129. /// The type for prepass material hooks.
  130. static const MatInstanceHookType Type;
  131. protected:
  132. PrePassMatInstance *mHookedPrePassMatInst;
  133. const RenderPrePassMgr *mPrePassManager;
  134. };
  135. //------------------------------------------------------------------------------
  136. // A very simple, default depth conditioner feature
  137. class LinearEyeDepthConditioner : public ConditionerFeature
  138. {
  139. typedef ConditionerFeature Parent;
  140. public:
  141. LinearEyeDepthConditioner(const GFXFormat bufferFormat)
  142. : Parent(bufferFormat)
  143. {
  144. }
  145. virtual String getName()
  146. {
  147. return "Linear Eye-Space Depth Conditioner";
  148. }
  149. virtual void processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd );
  150. protected:
  151. virtual Var *_conditionOutput( Var *unconditionedOutput, MultiLine *meta );
  152. virtual Var *_unconditionInput( Var *conditionedInput, MultiLine *meta );
  153. virtual Var *printMethodHeader( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta );
  154. };
  155. inline BaseMatInstance* RenderPrePassMgr::getPrePassMaterial( BaseMatInstance *mat )
  156. {
  157. PrePassMatInstanceHook *hook = static_cast<PrePassMatInstanceHook*>( mat->getHook( PrePassMatInstanceHook::Type ) );
  158. if ( !hook )
  159. {
  160. hook = new PrePassMatInstanceHook( static_cast<MatInstance*>( mat ), this );
  161. mat->addHook( hook );
  162. }
  163. return hook->getPrePassMatInstance();
  164. }
  165. #endif // _PREPASS_MGR_H_