renderDeferredMgr.h 7.1 KB

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