renderPrePassMgr.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // Generic PrePass Render Instance Type
  41. static const RenderInstType RIT_PrePass;
  42. RenderPrePassMgr( bool gatherDepth = true,
  43. GFXFormat format = GFXFormatR16G16B16A16 );
  44. virtual ~RenderPrePassMgr();
  45. virtual void setPrePassMaterial( PrePassMatInstance *mat );
  46. // RenderBinManager interface
  47. virtual void render(SceneRenderState * state);
  48. virtual void sort();
  49. virtual void clear();
  50. virtual void addElement( RenderInst *inst );
  51. // ConsoleObject
  52. DECLARE_CONOBJECT(RenderPrePassMgr);
  53. typedef Signal<void(const SceneRenderState*, RenderPrePassMgr*, bool)> RenderSignal;
  54. static RenderSignal& getRenderSignal();
  55. static const U32 OpaqueStaticLitMask = BIT(1); ///< Stencil mask for opaque, lightmapped pixels
  56. static const U32 OpaqueDynamicLitMask = BIT(0); ///< Stencil mask for opaque, dynamic lit pixels
  57. static const GFXStateBlockDesc &getOpaqueStencilTestDesc();
  58. static const GFXStateBlockDesc &getOpaqueStenciWriteDesc(bool lightmappedGeometry = true);
  59. virtual bool setTargetSize(const Point2I &newTargetSize);
  60. inline BaseMatInstance* getPrePassMaterial( BaseMatInstance *mat );
  61. protected:
  62. /// The terrain render instance elements.
  63. Vector< MainSortElem > mTerrainElementList;
  64. /// The object render instance elements.
  65. Vector< MainSortElem > mObjectElementList;
  66. PrePassMatInstance *mPrePassMatInstance;
  67. virtual void _registerFeatures();
  68. virtual void _unregisterFeatures();
  69. virtual bool _updateTargets();
  70. virtual void _createPrePassMaterial();
  71. bool _lightManagerActivate(bool active);
  72. };
  73. //------------------------------------------------------------------------------
  74. class ProcessedPrePassMaterial : public ProcessedShaderMaterial
  75. {
  76. typedef ProcessedShaderMaterial Parent;
  77. public:
  78. ProcessedPrePassMaterial(Material& mat, const RenderPrePassMgr *prePassMgr);
  79. virtual U32 getNumStages();
  80. virtual void addStateBlockDesc(const GFXStateBlockDesc& desc);
  81. protected:
  82. virtual void _determineFeatures( U32 stageNum, MaterialFeatureData &fd, const FeatureSet &features );
  83. const RenderPrePassMgr *mPrePassMgr;
  84. bool mIsLightmappedGeometry;
  85. };
  86. //------------------------------------------------------------------------------
  87. class PrePassMatInstance : public MatInstance
  88. {
  89. typedef MatInstance Parent;
  90. public:
  91. PrePassMatInstance(MatInstance* root, const RenderPrePassMgr *prePassMgr);
  92. virtual ~PrePassMatInstance();
  93. bool init()
  94. {
  95. return init( mFeatureList, mVertexFormat );
  96. }
  97. // MatInstance
  98. virtual bool init( const FeatureSet &features,
  99. const GFXVertexFormat *vertexFormat );
  100. protected:
  101. virtual ProcessedMaterial* getShaderMaterial();
  102. const RenderPrePassMgr *mPrePassMgr;
  103. };
  104. //------------------------------------------------------------------------------
  105. class PrePassMatInstanceHook : public MatInstanceHook
  106. {
  107. public:
  108. PrePassMatInstanceHook(MatInstance *baseMatInst, const RenderPrePassMgr *prePassMgr);
  109. virtual ~PrePassMatInstanceHook();
  110. virtual PrePassMatInstance *getPrePassMatInstance() { return mHookedPrePassMatInst; }
  111. virtual const MatInstanceHookType& getType() const { return Type; }
  112. /// The type for prepass material hooks.
  113. static const MatInstanceHookType Type;
  114. protected:
  115. PrePassMatInstance *mHookedPrePassMatInst;
  116. const RenderPrePassMgr *mPrePassManager;
  117. };
  118. //------------------------------------------------------------------------------
  119. // A very simple, default depth conditioner feature
  120. class LinearEyeDepthConditioner : public ConditionerFeature
  121. {
  122. typedef ConditionerFeature Parent;
  123. public:
  124. LinearEyeDepthConditioner(const GFXFormat bufferFormat)
  125. : Parent(bufferFormat)
  126. {
  127. }
  128. virtual String getName()
  129. {
  130. return "Linear Eye-Space Depth Conditioner";
  131. }
  132. virtual void processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd );
  133. protected:
  134. virtual Var *_conditionOutput( Var *unconditionedOutput, MultiLine *meta );
  135. virtual Var *_unconditionInput( Var *conditionedInput, MultiLine *meta );
  136. virtual Var *printMethodHeader( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta );
  137. };
  138. inline BaseMatInstance* RenderPrePassMgr::getPrePassMaterial( BaseMatInstance *mat )
  139. {
  140. PrePassMatInstanceHook *hook = static_cast<PrePassMatInstanceHook*>( mat->getHook( PrePassMatInstanceHook::Type ) );
  141. if ( !hook )
  142. {
  143. hook = new PrePassMatInstanceHook( static_cast<MatInstance*>( mat ), this );
  144. mat->addHook( hook );
  145. }
  146. return hook->getPrePassMatInstance();
  147. }
  148. #endif // _PREPASS_MGR_H_