renderDeferredMgr.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 _DEFERRED_MGR_H_
  23. #define _DEFERRED_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 DeferredMatInstance;
  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 RenderDeferredMgr : 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. static const String LightMapBufferName;
  44. // Generic Deferred Render Instance Type
  45. static const RenderInstType RIT_Deferred;
  46. RenderDeferredMgr( bool gatherDepth = true,
  47. GFXFormat format = GFXFormatR16G16B16A16 );
  48. virtual ~RenderDeferredMgr();
  49. virtual void setDeferredMaterial( DeferredMatInstance *mat );
  50. // RenderBinManager interface
  51. virtual void render(SceneRenderState * state);
  52. virtual void sort();
  53. virtual void clear();
  54. virtual void addElement( RenderInst *inst );
  55. // ConsoleObject
  56. DECLARE_CONOBJECT(RenderDeferredMgr);
  57. typedef Signal<void(const SceneRenderState*, RenderDeferredMgr*, bool)> RenderSignal;
  58. static RenderSignal& getRenderSignal();
  59. static const U32 OpaqueStaticLitMask = BIT(1); ///< Stencil mask for opaque, lightmapped pixels
  60. static const U32 OpaqueDynamicLitMask = BIT(0); ///< Stencil mask for opaque, dynamic lit pixels
  61. static const GFXStateBlockDesc &getOpaqueStencilTestDesc();
  62. static const GFXStateBlockDesc &getOpaqueStenciWriteDesc(bool lightmappedGeometry = true);
  63. virtual bool setTargetSize(const Point2I &newTargetSize);
  64. inline BaseMatInstance* getDeferredMaterial( BaseMatInstance *mat );
  65. protected:
  66. /// The terrain render instance elements.
  67. Vector< MainSortElem > mTerrainElementList;
  68. /// The object render instance elements.
  69. Vector< MainSortElem > mObjectElementList;
  70. DeferredMatInstance *mDeferredMatInstance;
  71. virtual void _registerFeatures();
  72. virtual void _unregisterFeatures();
  73. virtual bool _updateTargets();
  74. virtual void _createDeferredMaterial();
  75. bool _lightManagerActivate(bool active);
  76. // Deferred Shading
  77. GFXVertexBufferHandle<GFXVertexPC> mClearGBufferVerts;
  78. GFXShaderRef mClearGBufferShader;
  79. GFXStateBlockRef mStateblock;
  80. NamedTexTarget mColorTarget;
  81. NamedTexTarget mMatInfoTarget;
  82. NamedTexTarget mLightMapTarget;
  83. GFXTexHandle mColorTex;
  84. GFXTexHandle mMatInfoTex;
  85. GFXTexHandle mLightMapTex;
  86. GFXShaderConstBufferRef mShaderConsts;
  87. public:
  88. void clearBuffers();
  89. void _initShaders();
  90. };
  91. //------------------------------------------------------------------------------
  92. class ProcessedDeferredMaterial : public ProcessedShaderMaterial
  93. {
  94. typedef ProcessedShaderMaterial Parent;
  95. public:
  96. ProcessedDeferredMaterial(Material& mat, const RenderDeferredMgr *deferredMgr);
  97. virtual U32 getNumStages();
  98. virtual void addStateBlockDesc(const GFXStateBlockDesc& desc);
  99. protected:
  100. virtual void _determineFeatures( U32 stageNum, MaterialFeatureData &fd, const FeatureSet &features );
  101. const RenderDeferredMgr *mDeferredMgr;
  102. bool mIsLightmappedGeometry;
  103. };
  104. //------------------------------------------------------------------------------
  105. class DeferredMatInstance : public MatInstance
  106. {
  107. typedef MatInstance Parent;
  108. public:
  109. DeferredMatInstance(MatInstance* root, const RenderDeferredMgr *deferredMgr);
  110. virtual ~DeferredMatInstance();
  111. bool init()
  112. {
  113. return init( mFeatureList, mVertexFormat );
  114. }
  115. // MatInstance
  116. virtual bool init( const FeatureSet &features,
  117. const GFXVertexFormat *vertexFormat );
  118. protected:
  119. virtual ProcessedMaterial* getShaderMaterial();
  120. const RenderDeferredMgr *mDeferredMgr;
  121. };
  122. //------------------------------------------------------------------------------
  123. class DeferredMatInstanceHook : public MatInstanceHook
  124. {
  125. public:
  126. DeferredMatInstanceHook(MatInstance *baseMatInst, const RenderDeferredMgr *deferredMgr);
  127. virtual ~DeferredMatInstanceHook();
  128. virtual DeferredMatInstance *getDeferredMatInstance() { return mHookedDeferredMatInst; }
  129. virtual const MatInstanceHookType& getType() const { return Type; }
  130. /// The type for deferred material hooks.
  131. static const MatInstanceHookType Type;
  132. protected:
  133. DeferredMatInstance *mHookedDeferredMatInst;
  134. const RenderDeferredMgr *mDeferredManager;
  135. };
  136. //------------------------------------------------------------------------------
  137. // A very simple, default depth conditioner feature
  138. class LinearEyeDepthConditioner : public ConditionerFeature
  139. {
  140. typedef ConditionerFeature Parent;
  141. public:
  142. LinearEyeDepthConditioner(const GFXFormat bufferFormat)
  143. : Parent(bufferFormat)
  144. {
  145. }
  146. virtual String getName()
  147. {
  148. return "Linear Eye-Space Depth Conditioner";
  149. }
  150. virtual void processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd );
  151. protected:
  152. virtual Var *_conditionOutput( Var *unconditionedOutput, MultiLine *meta );
  153. virtual Var *_unconditionInput( Var *conditionedInput, MultiLine *meta );
  154. virtual Var *printMethodHeader( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta );
  155. };
  156. inline BaseMatInstance* RenderDeferredMgr::getDeferredMaterial( BaseMatInstance *mat )
  157. {
  158. DeferredMatInstanceHook *hook = static_cast<DeferredMatInstanceHook*>( mat->getHook( DeferredMatInstanceHook::Type ) );
  159. if ( !hook )
  160. {
  161. hook = new DeferredMatInstanceHook( static_cast<MatInstance*>( mat ), this );
  162. mat->addHook( hook );
  163. }
  164. return hook->getDeferredMatInstance();
  165. }
  166. #endif // _DEFERRED_MGR_H_