BsLightRendering.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 radius;
  17. Vector3 direction;
  18. float intensity;
  19. Vector3 spotAngles;
  20. float radiusSqrdInv;
  21. Vector3 color;
  22. };
  23. /** Renderer information specific to a single light. */
  24. class RendererLight
  25. {
  26. public:
  27. RendererLight(Light* light);
  28. /** Populates the structure with light parameters. */
  29. void getParameters(LightData& output) const;
  30. /** Gets the internal light representation. */
  31. Light* getInternal() const { return mInternal; }
  32. private:
  33. Light* mInternal;
  34. };
  35. /** Contains GPU buffers used by the renderer to manipulate lights. */
  36. class GPULightData
  37. {
  38. public:
  39. GPULightData();
  40. /** Updates the internal buffers with a new set of lights. */
  41. void setLights(const Vector<LightData>& lightData, UINT32 numDirLights, UINT32 numRadialLights,
  42. UINT32 numSpotLights);
  43. /** Returns a GPU bindable buffer containing information about every light. */
  44. SPtr<GpuBuffer> getLightBuffer() const { return mLightBuffer; }
  45. /** Returns the number of directional lights in the lights buffer. */
  46. UINT32 getNumDirLights() const { return mNumLights[0]; }
  47. /** Returns the number of radial point lights in the lights buffer. */
  48. UINT32 getNumRadialLights() const { return mNumLights[1]; }
  49. /** Returns the number of spot point lights in the lights buffer. */
  50. UINT32 getNumSpotLights() const { return mNumLights[2]; }
  51. private:
  52. SPtr<GpuBuffer> mLightBuffer;
  53. UINT32 mNumLights[3];
  54. };
  55. BS_PARAM_BLOCK_BEGIN(TiledLightingParamDef)
  56. BS_PARAM_BLOCK_ENTRY(Vector3I, gLightOffsets)
  57. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  58. BS_PARAM_BLOCK_END
  59. extern TiledLightingParamDef gTiledLightingParamDef;
  60. /** Functionality common to all versions of TiledDeferredLightingMat<T>. */
  61. class TiledDeferredLighting
  62. {
  63. public:
  64. TiledDeferredLighting(const SPtr<Material>& material, const SPtr<GpuParamsSet>& paramsSet, UINT32 sampleCount);
  65. /** Binds the material for rendering, sets up parameters and executes it. */
  66. void execute(const SPtr<RenderTargets>& gbuffer, const SPtr<GpuParamBlockBuffer>& perCamera, bool noLighting);
  67. /** Binds all the active lights. */
  68. void setLights(const GPULightData& lightData);
  69. /**
  70. * Generates a 2D 2-channel texture containing a pre-integrated G and F factors of the microfactet BRDF. This is an
  71. * approximation used for image based lighting, so we can avoid sampling environment maps for each light. Works in
  72. * tandem with the importance sampled reflection cubemaps.
  73. *
  74. * (u, v) = (NoV, roughness)
  75. * (r, g) = (scale, bias)
  76. */
  77. static SPtr<Texture> generatePreintegratedEnvBRDF();
  78. static const UINT32 TILE_SIZE;
  79. private:
  80. UINT32 mSampleCount;
  81. SPtr<Material> mMaterial;
  82. SPtr<GpuParamsSet> mParamsSet;
  83. GpuParamTexture mGBufferA;
  84. GpuParamTexture mGBufferB;
  85. GpuParamTexture mGBufferC;
  86. GpuParamTexture mGBufferDepth;
  87. Vector3I mLightOffsets;
  88. GpuParamBuffer mLightBufferParam;
  89. GpuParamLoadStoreTexture mOutputTextureParam;
  90. GpuParamBuffer mOutputBufferParam;
  91. SPtr<GpuParamBlockBuffer> mParamBuffer;
  92. };
  93. /** Interface implemented by all versions of TTiledDeferredLightingMat<T>. */
  94. class ITiledDeferredLightingMat
  95. {
  96. public:
  97. virtual ~ITiledDeferredLightingMat() {}
  98. /** Binds the material for rendering, sets up parameters and executes it. */
  99. virtual void execute(const SPtr<RenderTargets>& gbuffer, const SPtr<GpuParamBlockBuffer>& perCamera, bool noLighting) = 0;
  100. /** Binds all the active lights. */
  101. virtual void setLights(const GPULightData& lightData) = 0;
  102. };
  103. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  104. template<int MSAA_COUNT>
  105. class TTiledDeferredLightingMat : public ITiledDeferredLightingMat, public RendererMaterial<TTiledDeferredLightingMat<MSAA_COUNT>>
  106. {
  107. RMAT_DEF("TiledDeferredLighting.bsl");
  108. public:
  109. TTiledDeferredLightingMat();
  110. /** Binds the material for rendering, sets up parameters and executes it. */
  111. void execute(const SPtr<RenderTargets>& gbuffer, const SPtr<GpuParamBlockBuffer>& perCamera, bool noLighting) override;
  112. /** Binds all the active lights. */
  113. void setLights(const GPULightData& lightData) override;
  114. private:
  115. TiledDeferredLighting mInternal;
  116. };
  117. BS_PARAM_BLOCK_BEGIN(FlatFramebufferToTextureParamDef)
  118. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  119. BS_PARAM_BLOCK_ENTRY(INT32, gSampleCount)
  120. BS_PARAM_BLOCK_END
  121. /** Shader that copies a flattened framebuffer into a multisampled texture. */
  122. class FlatFramebufferToTextureMat : public RendererMaterial<FlatFramebufferToTextureMat>
  123. {
  124. RMAT_DEF("FlatFramebufferToTexture.bsl");
  125. public:
  126. FlatFramebufferToTextureMat();
  127. /** Binds the material for rendering, sets up parameters and executes it. */
  128. void execute(const SPtr<GpuBuffer>& flatFramebuffer, const SPtr<Texture>& target);
  129. private:
  130. GpuParamBuffer mInputParam;
  131. SPtr<GpuParamBlockBuffer> mParamBuffer;
  132. };
  133. /** @} */
  134. }}