BsLightGrid.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include "BsVectorNI.h"
  8. namespace bs { namespace ct
  9. {
  10. /** @addtogroup RenderBeast
  11. * @{
  12. */
  13. BS_PARAM_BLOCK_BEGIN(LightGridParamDef)
  14. BS_PARAM_BLOCK_ENTRY(Vector3I, gLightOffsets)
  15. BS_PARAM_BLOCK_ENTRY(INT32, gNumCells)
  16. BS_PARAM_BLOCK_ENTRY(Vector3I, gGridSize)
  17. BS_PARAM_BLOCK_ENTRY(INT32, gMaxNumLightsPerCell)
  18. BS_PARAM_BLOCK_END
  19. extern LightGridParamDef gLightGridParamDefDef;
  20. /** Shader that creates a linked list for each light grid cell, containing which light affects each cell. */
  21. class LightGridLLCreationMat : public RendererMaterial<LightGridLLCreationMat>
  22. {
  23. RMAT_DEF("LightGridLLCreation.bsl");
  24. public:
  25. LightGridLLCreationMat();
  26. /** Binds parameter buffers and prepares any internal buffers. Must be called before execute(). */
  27. void setParams(const Vector3I& gridSize, const SPtr<GpuParamBlockBuffer>& gridParams,
  28. const SPtr<GpuBuffer>& lightsBuffer);
  29. /** Binds the material for rendering, sets up per-camera parameters and executes it. */
  30. void execute(const RendererCamera& view);
  31. /** Returns the buffers generated by execute(). */
  32. void getOutputs(SPtr<GpuBuffer>& linkedListHeads, SPtr<GpuBuffer>& linkedList) const;
  33. private:
  34. GpuParamBuffer mLightBufferParam;
  35. GpuParamBuffer mLinkedListCounterParam;
  36. GpuParamBuffer mLinkedListHeadsParam;
  37. GpuParamBuffer mLinkedListParam;
  38. SPtr<GpuBuffer> mLinkedListCounter;
  39. SPtr<GpuBuffer> mLinkedListHeads;
  40. SPtr<GpuBuffer> mLinkedList;
  41. UINT32 mBufferNumCells;
  42. Vector3I mGridSize;
  43. };
  44. /** Shader that reduces the linked list created by LightGridLLCreationMat into a sequential array. */
  45. class LightGridLLReductionMat : public RendererMaterial<LightGridLLReductionMat>
  46. {
  47. RMAT_DEF("LightGridLLReduction.bsl");
  48. public:
  49. LightGridLLReductionMat();
  50. /** Binds parameter buffers and prepares any internal buffers. Must be called before execute(). */
  51. void setParams(const Vector3I& gridSize, const SPtr<GpuParamBlockBuffer>& gridParams,
  52. SPtr<GpuBuffer>& linkedListHeads, SPtr<GpuBuffer>& linkedList);
  53. /** Binds the material for renderingand executes it. */
  54. void execute(const RendererCamera& view);
  55. /** Returns the buffers generated by execute(). */
  56. void getOutputs(SPtr<GpuBuffer>& gridOffsetsAndSize, SPtr<GpuBuffer>& gridLightIndices) const;
  57. private:
  58. GpuParamBuffer mLinkedListHeadsParam;
  59. GpuParamBuffer mLinkedListParam;
  60. GpuParamBuffer mGridDataCounterParam;
  61. GpuParamBuffer mGridLightOffsetAndSizeParam;
  62. GpuParamBuffer mGridLightIndicesParam;
  63. SPtr<GpuBuffer> mGridDataCounter;
  64. SPtr<GpuBuffer> mGridLightOffsetAndSize;
  65. SPtr<GpuBuffer> mGridLightIndices;
  66. UINT32 mBufferNumCells;
  67. Vector3I mGridSize;
  68. };
  69. /**
  70. * Helper class that is used for generating a grid in view space, whose cells contain information about lights
  71. * affecting them. Used for forward rendering.
  72. */
  73. class LightGrid
  74. {
  75. public:
  76. LightGrid();
  77. /** Updates the light grid from the provided view. */
  78. void updateGrid(const RendererCamera& view, const SPtr<GpuBuffer>& lightsBuffer, UINT32 numDirLights,
  79. UINT32 numRadialLights, UINT32 numSpotLights);
  80. /**
  81. * Returns the buffers containing light indices per grid cell.
  82. *
  83. * @param[out] gridOffsetsAndSize Flattened array of grid cells, where each entry contains the number of lights
  84. * affecting that cell, and a index into the @p gridLightIndices buffer.
  85. * @param[out] gridLightIndices A list of light indices. Each cell's indices start at specific position and
  86. * are placed sequentially one after another. Lookup into this array is done
  87. * through offset & size provided by @p gridOffsetsAndSize.
  88. */
  89. void getOutputs(SPtr<GpuBuffer>& gridOffsetsAndSize, SPtr<GpuBuffer>& gridLightIndices) const;
  90. private:
  91. LightGridLLCreationMat mLLCreationMat;
  92. LightGridLLReductionMat mLLReductionMat;
  93. SPtr<GpuParamBlockBuffer> mGridParamBuffer;
  94. };
  95. /** @} */
  96. }}