BsLightRendering.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsRenderBeastPrerequisites.h"
  5. #include "BsRendererMaterial.h"
  6. #include "BsParamBlocks.h"
  7. namespace bs { namespace ct
  8. {
  9. /** @addtogroup RenderBeast
  10. * @{
  11. */
  12. /** Information about a single light, as seen by the lighting shader. */
  13. struct LightData
  14. {
  15. Vector3 position;
  16. float attRadius;
  17. Vector3 direction;
  18. float luminance;
  19. Vector3 spotAngles;
  20. float attRadiusSqrdInv;
  21. Vector3 color;
  22. float srcRadius;
  23. Vector3 shiftedLightPosition;
  24. float padding;
  25. };
  26. /** Renderer information specific to a single light. */
  27. class RendererLight
  28. {
  29. public:
  30. RendererLight(Light* light);
  31. /** Populates the structure with light parameters. */
  32. void getParameters(LightData& output) const;
  33. /** Gets the internal light representation. */
  34. Light* getInternal() const { return mInternal; }
  35. private:
  36. Light* mInternal;
  37. };
  38. /** Contains GPU buffers used by the renderer to manipulate lights. */
  39. class GPULightData
  40. {
  41. public:
  42. GPULightData();
  43. /** Updates the internal buffers with a new set of lights. */
  44. void setLights(const Vector<LightData>& lightData, UINT32 numDirLights, UINT32 numRadialLights,
  45. UINT32 numSpotLights);
  46. /** Returns a GPU bindable buffer containing information about every light. */
  47. SPtr<GpuBuffer> getLightBuffer() const { return mLightBuffer; }
  48. /** Returns the number of directional lights in the lights buffer. */
  49. UINT32 getNumDirLights() const { return mNumLights[0]; }
  50. /** Returns the number of radial point lights in the lights buffer. */
  51. UINT32 getNumRadialLights() const { return mNumLights[1]; }
  52. /** Returns the number of spot point lights in the lights buffer. */
  53. UINT32 getNumSpotLights() const { return mNumLights[2]; }
  54. private:
  55. SPtr<GpuBuffer> mLightBuffer;
  56. UINT32 mNumLights[3];
  57. };
  58. BS_PARAM_BLOCK_BEGIN(TiledLightingParamDef)
  59. BS_PARAM_BLOCK_ENTRY(Vector3I, gLightOffsets)
  60. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  61. BS_PARAM_BLOCK_END
  62. extern TiledLightingParamDef gTiledLightingParamDef;
  63. /** Functionality common to all versions of TiledDeferredLightingMat<T>. */
  64. class TiledDeferredLighting
  65. {
  66. public:
  67. TiledDeferredLighting(const SPtr<Material>& material, const SPtr<GpuParamsSet>& paramsSet, UINT32 sampleCount);
  68. /** Binds the material for rendering, sets up parameters and executes it. */
  69. void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera, bool noLighting);
  70. /** Binds all the active lights. */
  71. void setLights(const GPULightData& lightData);
  72. static const UINT32 TILE_SIZE;
  73. private:
  74. UINT32 mSampleCount;
  75. SPtr<Material> mMaterial;
  76. SPtr<GpuParamsSet> mParamsSet;
  77. GpuParamTexture mGBufferA;
  78. GpuParamTexture mGBufferB;
  79. GpuParamTexture mGBufferC;
  80. GpuParamTexture mGBufferDepth;
  81. Vector3I mLightOffsets;
  82. GpuParamBuffer mLightBufferParam;
  83. GpuParamLoadStoreTexture mOutputTextureParam;
  84. GpuParamBuffer mOutputBufferParam;
  85. SPtr<GpuParamBlockBuffer> mParamBuffer;
  86. };
  87. /** Interface implemented by all versions of TTiledDeferredLightingMat<T>. */
  88. class ITiledDeferredLightingMat
  89. {
  90. public:
  91. virtual ~ITiledDeferredLightingMat() {}
  92. /** @copydoc TiledDeferredLighting::execute() */
  93. virtual void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  94. bool noLighting) = 0;
  95. /** @copydoc TiledDeferredLighting::setLights() */
  96. virtual void setLights(const GPULightData& lightData) = 0;
  97. };
  98. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  99. template<int MSAA_COUNT>
  100. class TTiledDeferredLightingMat : public ITiledDeferredLightingMat, public RendererMaterial<TTiledDeferredLightingMat<MSAA_COUNT>>
  101. {
  102. RMAT_DEF("TiledDeferredLighting.bsl");
  103. public:
  104. TTiledDeferredLightingMat();
  105. /** @copydoc ITiledDeferredLightingMat::execute() */
  106. void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  107. bool noLighting) override;
  108. /** @copydoc ITiledDeferredLightingMat::setLights() */
  109. void setLights(const GPULightData& lightData) override;
  110. private:
  111. TiledDeferredLighting mInternal;
  112. };
  113. /** Contains instances for all types of tile deferred lighting materials. */
  114. class TiledDeferredLightingMaterials
  115. {
  116. public:
  117. TiledDeferredLightingMaterials();
  118. ~TiledDeferredLightingMaterials();
  119. /**
  120. * Returns a version of the tile-deferred lighting material that matches the parameters.
  121. *
  122. * @param[in] msaa Number of samples per pixel.
  123. */
  124. ITiledDeferredLightingMat* get(UINT32 msaa);
  125. private:
  126. ITiledDeferredLightingMat* mInstances[4];
  127. };
  128. BS_PARAM_BLOCK_BEGIN(FlatFramebufferToTextureParamDef)
  129. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  130. BS_PARAM_BLOCK_ENTRY(INT32, gSampleCount)
  131. BS_PARAM_BLOCK_END
  132. /** Shader that copies a flattened framebuffer into a multisampled texture. */
  133. class FlatFramebufferToTextureMat : public RendererMaterial<FlatFramebufferToTextureMat>
  134. {
  135. RMAT_DEF("FlatFramebufferToTexture.bsl");
  136. public:
  137. FlatFramebufferToTextureMat();
  138. /** Binds the material for rendering, sets up parameters and executes it. */
  139. void execute(const SPtr<GpuBuffer>& flatFramebuffer, const SPtr<Texture>& target);
  140. private:
  141. GpuParamBuffer mInputParam;
  142. SPtr<GpuParamBlockBuffer> mParamBuffer;
  143. };
  144. /** @} */
  145. }}