BsLightGrid.h 4.3 KB

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