BsLightRendering.h 5.4 KB

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