renderDeferredMgr.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. ///
  72. Vector< MainSortElem > mProbeElementList;
  73. virtual void _registerFeatures();
  74. virtual void _unregisterFeatures();
  75. virtual bool _updateTargets();
  76. virtual void _createDeferredMaterial();
  77. bool _lightManagerActivate(bool active);
  78. // Deferred Shading
  79. GFXVertexBufferHandle<GFXVertexPC> mClearGBufferVerts;
  80. GFXShaderRef mClearGBufferShader;
  81. GFXStateBlockRef mStateblock;
  82. NamedTexTarget mColorTarget;
  83. NamedTexTarget mMatInfoTarget;
  84. NamedTexTarget mLightMapTarget;
  85. GFXTexHandle mColorTex;
  86. GFXTexHandle mMatInfoTex;
  87. GFXTexHandle mLightMapTex;
  88. GFXShaderConstBufferRef mShaderConsts;
  89. public:
  90. void clearBuffers();
  91. void _initShaders();
  92. };
  93. //------------------------------------------------------------------------------
  94. class ProcessedDeferredMaterial : public ProcessedShaderMaterial
  95. {
  96. typedef ProcessedShaderMaterial Parent;
  97. public:
  98. ProcessedDeferredMaterial(Material& mat, const RenderDeferredMgr *deferredMgr);
  99. virtual U32 getNumStages();
  100. virtual void addStateBlockDesc(const GFXStateBlockDesc& desc);
  101. protected:
  102. virtual void _determineFeatures( U32 stageNum, MaterialFeatureData &fd, const FeatureSet &features );
  103. const RenderDeferredMgr *mDeferredMgr;
  104. bool mIsLightmappedGeometry;
  105. };
  106. //------------------------------------------------------------------------------
  107. class DeferredMatInstance : public MatInstance
  108. {
  109. typedef MatInstance Parent;
  110. public:
  111. DeferredMatInstance(MatInstance* root, const RenderDeferredMgr *deferredMgr);
  112. virtual ~DeferredMatInstance();
  113. bool init()
  114. {
  115. return init( mFeatureList, mVertexFormat );
  116. }
  117. // MatInstance
  118. virtual bool init( const FeatureSet &features,
  119. const GFXVertexFormat *vertexFormat );
  120. protected:
  121. virtual ProcessedMaterial* getShaderMaterial();
  122. const RenderDeferredMgr *mDeferredMgr;
  123. };
  124. //------------------------------------------------------------------------------
  125. class DeferredMatInstanceHook : public MatInstanceHook
  126. {
  127. public:
  128. DeferredMatInstanceHook(MatInstance *baseMatInst, const RenderDeferredMgr *deferredMgr);
  129. virtual ~DeferredMatInstanceHook();
  130. virtual DeferredMatInstance *getDeferredMatInstance() { return mHookedDeferredMatInst; }
  131. virtual const MatInstanceHookType& getType() const { return Type; }
  132. /// The type for deferred material hooks.
  133. static const MatInstanceHookType Type;
  134. protected:
  135. DeferredMatInstance *mHookedDeferredMatInst;
  136. const RenderDeferredMgr *mDeferredManager;
  137. };
  138. //------------------------------------------------------------------------------
  139. // A very simple, default depth conditioner feature
  140. class LinearEyeDepthConditioner : public ConditionerFeature
  141. {
  142. typedef ConditionerFeature Parent;
  143. public:
  144. LinearEyeDepthConditioner(const GFXFormat bufferFormat)
  145. : Parent(bufferFormat)
  146. {
  147. }
  148. virtual String getName()
  149. {
  150. return "Linear Eye-Space Depth Conditioner";
  151. }
  152. virtual void processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd );
  153. protected:
  154. virtual Var *_conditionOutput( Var *unconditionedOutput, MultiLine *meta );
  155. virtual Var *_unconditionInput( Var *conditionedInput, MultiLine *meta );
  156. virtual Var *printMethodHeader( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta );
  157. };
  158. inline BaseMatInstance* RenderDeferredMgr::getDeferredMaterial( BaseMatInstance *mat )
  159. {
  160. DeferredMatInstanceHook *hook = static_cast<DeferredMatInstanceHook*>( mat->getHook( DeferredMatInstanceHook::Type ) );
  161. if ( !hook )
  162. {
  163. hook = new DeferredMatInstanceHook( static_cast<MatInstance*>( mat ), this );
  164. mat->addHook( hook );
  165. }
  166. return hook->getDeferredMatInstance();
  167. }
  168. #endif // _DEFERRED_MGR_H_