BsLightGrid.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "Renderer/BsRendererMaterial.h"
  6. #include "Renderer/BsParamBlocks.h"
  7. #include "Math/BsVectorNI.h"
  8. namespace bs { namespace ct
  9. {
  10. class VisibleReflProbeData;
  11. class VisibleLightData;
  12. /** @addtogroup RenderBeast
  13. * @{
  14. */
  15. BS_PARAM_BLOCK_BEGIN(LightGridParamDef)
  16. BS_PARAM_BLOCK_ENTRY(Vector4I, gLightCounts)
  17. BS_PARAM_BLOCK_ENTRY(Vector2I, gLightStrides)
  18. BS_PARAM_BLOCK_ENTRY(INT32, gNumReflProbes)
  19. BS_PARAM_BLOCK_ENTRY(INT32, gNumCells)
  20. BS_PARAM_BLOCK_ENTRY(Vector3I, gGridSize)
  21. BS_PARAM_BLOCK_ENTRY(INT32, gMaxNumLightsPerCell)
  22. BS_PARAM_BLOCK_ENTRY(Vector2I, gGridPixelSize)
  23. BS_PARAM_BLOCK_END
  24. extern LightGridParamDef gLightGridParamDefDef;
  25. /**
  26. * Shader that creates a linked list for each light grid cell, containing which lights and reflection probes affects
  27. * each cell.
  28. */
  29. class LightGridLLCreationMat : public RendererMaterial<LightGridLLCreationMat>
  30. {
  31. RMAT_DEF("LightGridLLCreation.bsl");
  32. public:
  33. LightGridLLCreationMat();
  34. /** Binds parameter buffers and prepares any internal buffers. Must be called before execute(). */
  35. void setParams(const Vector3I& gridSize, const SPtr<GpuParamBlockBuffer>& gridParams,
  36. const SPtr<GpuBuffer>& lightsBuffer, const SPtr<GpuBuffer>& probesBuffer);
  37. /** Binds the material for rendering, sets up per-camera parameters and executes it. */
  38. void execute(const RendererView& view);
  39. /** Returns the buffers generated by execute(). */
  40. void getOutputs(SPtr<GpuBuffer>& lightsLLHeads, SPtr<GpuBuffer>& lightsLL, SPtr<GpuBuffer>& probesLLHeads,
  41. SPtr<GpuBuffer>& probesLL) const;
  42. private:
  43. GpuParamBuffer mLightBufferParam;
  44. GpuParamBuffer mLightsCounterParam;
  45. GpuParamBuffer mLightsLLHeadsParam;
  46. GpuParamBuffer mLightsLLParam;
  47. GpuParamBuffer mProbesBufferParam;
  48. GpuParamBuffer mProbesCounterParam;
  49. GpuParamBuffer mProbesLLHeadsParam;
  50. GpuParamBuffer mProbesLLParam;
  51. SPtr<GpuBuffer> mLightsCounter;
  52. SPtr<GpuBuffer> mLightsLLHeads;
  53. SPtr<GpuBuffer> mLightsLL;
  54. SPtr<GpuBuffer> mProbesCounter;
  55. SPtr<GpuBuffer> mProbesLLHeads;
  56. SPtr<GpuBuffer> mProbesLL;
  57. UINT32 mBufferNumCells;
  58. Vector3I mGridSize;
  59. };
  60. /** Shader that reduces the linked list created by LightGridLLCreationMat into a sequential array. */
  61. class LightGridLLReductionMat : public RendererMaterial<LightGridLLReductionMat>
  62. {
  63. RMAT_DEF("LightGridLLReduction.bsl");
  64. public:
  65. LightGridLLReductionMat();
  66. /** Binds parameter buffers and prepares any internal buffers. Must be called before execute(). */
  67. void setParams(const Vector3I& gridSize, const SPtr<GpuParamBlockBuffer>& gridParams,
  68. const SPtr<GpuBuffer>& lightLLHeads, const SPtr<GpuBuffer>& lightLL,
  69. const SPtr<GpuBuffer>& probeLLHeads, const SPtr<GpuBuffer>& probeLL);
  70. /** Binds the material for renderingand executes it. */
  71. void execute(const RendererView& view);
  72. /** Returns the buffers generated by execute(). */
  73. void getOutputs(SPtr<GpuBuffer>& gridLightOffsetsAndSize, SPtr<GpuBuffer>& gridLightIndices,
  74. SPtr<GpuBuffer>& gridProbeOffsetsAndSize, SPtr<GpuBuffer>& gridProbeIndices) const;
  75. private:
  76. GpuParamBuffer mLightsLLHeadsParam;
  77. GpuParamBuffer mLightsLLParam;
  78. GpuParamBuffer mProbesLLHeadsParam;
  79. GpuParamBuffer mProbesLLParam;
  80. GpuParamBuffer mGridDataCounterParam;
  81. GpuParamBuffer mGridLightOffsetAndSizeParam;
  82. GpuParamBuffer mGridLightIndicesParam;
  83. GpuParamBuffer mGridProbeOffsetAndSizeParam;
  84. GpuParamBuffer mGridProbeIndicesParam;
  85. SPtr<GpuBuffer> mGridDataCounter;
  86. SPtr<GpuBuffer> mGridLightOffsetAndSize;
  87. SPtr<GpuBuffer> mGridLightIndices;
  88. SPtr<GpuBuffer> mGridProbeOffsetAndSize;
  89. SPtr<GpuBuffer> mGridProbeIndices;
  90. UINT32 mBufferNumCells;
  91. Vector3I mGridSize;
  92. };
  93. /**
  94. * Helper class that is used for generating a grid in view space, whose cells contain information about lights
  95. * affecting them. Used for forward rendering.
  96. */
  97. class LightGrid
  98. {
  99. public:
  100. LightGrid();
  101. /** Updates the light grid from the provided view. */
  102. void updateGrid(const RendererView& view, const VisibleLightData& lightData, const VisibleReflProbeData& probeData,
  103. bool noLighting);
  104. /**
  105. * Returns the buffers containing light indices per grid cell and global grid parameters.
  106. *
  107. * @param[out] gridLightOffsetsAndSize Flattened array of grid cells, where each entry contains the number of
  108. * lights affecting that cell, and a index into the @p gridLightIndices buffer.
  109. * @param[out] gridLightIndices A list of light indices. Each cell's indices start at specific position and
  110. * are placed sequentially one after another. Lookup into this array is done
  111. * through offset & size provided by @p gridLightOffsetsAndSize.
  112. * @param[out] gridProbeOffsetsAndSize Flattened array of grid cells, where each entry contains the number of
  113. * reflection probes affecting that cell, and a index into the
  114. * @p gridProbeIndices buffer.
  115. * @param[out] gridProbeIndices A list of reflection probe indices. Each cell's indices start at specific
  116. * position and are placed sequentially one after another. Lookup into this
  117. * array is done through offset & size provided by @p gridProbeOffsetsAndSize.
  118. * @param[out] gridParams Global grid parameter block.
  119. */
  120. void getOutputs(SPtr<GpuBuffer>& gridLightOffsetsAndSize, SPtr<GpuBuffer>& gridLightIndices,
  121. SPtr<GpuBuffer>& gridProbeOffsetsAndSize, SPtr<GpuBuffer>& gridProbeIndices,
  122. SPtr<GpuParamBlockBuffer>& gridParams) const;
  123. private:
  124. SPtr<GpuParamBlockBuffer> mGridParamBuffer;
  125. };
  126. /** @} */
  127. }}