1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #pragma once |
4 | |
5 | #include "BsRenderBeastPrerequisites.h" |
6 | #include "Renderer/BsRendererMaterial.h" |
7 | #include "Renderer/BsParamBlocks.h" |
8 | |
9 | namespace bs { namespace ct |
10 | { |
11 | class VisibleReflProbeData; |
12 | class VisibleLightData; |
13 | |
14 | /** @addtogroup RenderBeast |
15 | * @{ |
16 | */ |
17 | |
18 | BS_PARAM_BLOCK_BEGIN(LightGridParamDef) |
19 | BS_PARAM_BLOCK_ENTRY(Vector4I, gLightCounts) |
20 | BS_PARAM_BLOCK_ENTRY(Vector2I, gLightStrides) |
21 | BS_PARAM_BLOCK_ENTRY(INT32, gNumReflProbes) |
22 | BS_PARAM_BLOCK_ENTRY(INT32, gNumCells) |
23 | BS_PARAM_BLOCK_ENTRY(Vector3I, gGridSize) |
24 | BS_PARAM_BLOCK_ENTRY(INT32, gMaxNumLightsPerCell) |
25 | BS_PARAM_BLOCK_ENTRY(Vector2I, gGridPixelSize) |
26 | BS_PARAM_BLOCK_END |
27 | |
28 | extern LightGridParamDef gLightGridParamDefDef; |
29 | |
30 | /** A set of buffers containing outputs from LightGrid. */ |
31 | struct LightGridOutputs |
32 | { |
33 | /** Parameter block of LightGridParamDef. */ |
34 | SPtr<GpuParamBlockBuffer> gridParams; |
35 | |
36 | /** |
37 | * Flattened array of grid cells, where each entry contains the number of lights affecting that cell, and a index |
38 | * into the @p gridLightIndices buffer. |
39 | */ |
40 | SPtr<GpuBuffer> gridLightOffsetsAndSize; |
41 | |
42 | /** |
43 | * A list of light indices. Each cell's indices start at specific position and are placed sequentially one after |
44 | * another. Lookup into this array is done through offset & size provided by @p gridLightOffsetsAndSize. |
45 | */ |
46 | SPtr<GpuBuffer> gridLightIndices; |
47 | |
48 | /** |
49 | * Flattened array of grid cells, where each entry contains the number of reflection probes affecting that cell, |
50 | * and a index into the @p gridProbeIndices buffer. |
51 | */ |
52 | SPtr<GpuBuffer> gridProbeOffsetsAndSize; |
53 | |
54 | /** |
55 | * A list of reflection probe indices. Each cell's indices start at specific position and are placed sequentially |
56 | * one after another. Lookup into this array is done through offset & size provided by @p gridProbeOffsetsAndSize. |
57 | */ |
58 | SPtr<GpuBuffer> gridProbeIndices; |
59 | }; |
60 | |
61 | /** |
62 | * Shader that creates a linked list for each light grid cell, containing which lights and reflection probes affects |
63 | * each cell. |
64 | */ |
65 | class LightGridLLCreationMat : public RendererMaterial<LightGridLLCreationMat> |
66 | { |
67 | RMAT_DEF_CUSTOMIZED("LightGridLLCreation.bsl" ); |
68 | |
69 | public: |
70 | LightGridLLCreationMat(); |
71 | |
72 | /** Binds parameter buffers and prepares any internal buffers. Must be called before execute(). */ |
73 | void setParams(const Vector3I& gridSize, const SPtr<GpuParamBlockBuffer>& gridParams, |
74 | const SPtr<GpuBuffer>& lightsBuffer, const SPtr<GpuBuffer>& probesBuffer); |
75 | |
76 | /** Binds the material for rendering, sets up per-camera parameters and executes it. */ |
77 | void execute(const RendererView& view); |
78 | |
79 | /** Returns the buffers generated by execute(). */ |
80 | void getOutputs(SPtr<GpuBuffer>& lightsLLHeads, SPtr<GpuBuffer>& lightsLL, SPtr<GpuBuffer>& probesLLHeads, |
81 | SPtr<GpuBuffer>& probesLL) const; |
82 | private: |
83 | GpuParamBuffer mLightBufferParam; |
84 | GpuParamBuffer mLightsCounterParam; |
85 | GpuParamBuffer mLightsLLHeadsParam; |
86 | GpuParamBuffer mLightsLLParam; |
87 | |
88 | GpuParamBuffer mProbesBufferParam; |
89 | GpuParamBuffer mProbesCounterParam; |
90 | GpuParamBuffer mProbesLLHeadsParam; |
91 | GpuParamBuffer mProbesLLParam; |
92 | |
93 | SPtr<GpuBuffer> mLightsCounter; |
94 | SPtr<GpuBuffer> mLightsLLHeads; |
95 | SPtr<GpuBuffer> mLightsLL; |
96 | |
97 | SPtr<GpuBuffer> mProbesCounter; |
98 | SPtr<GpuBuffer> mProbesLLHeads; |
99 | SPtr<GpuBuffer> mProbesLL; |
100 | |
101 | UINT32 mBufferNumCells = 0; |
102 | Vector3I mGridSize; |
103 | }; |
104 | |
105 | /** Shader that reduces the linked list created by LightGridLLCreationMat into a sequential array. */ |
106 | class LightGridLLReductionMat : public RendererMaterial<LightGridLLReductionMat> |
107 | { |
108 | RMAT_DEF_CUSTOMIZED("LightGridLLReduction.bsl" ); |
109 | |
110 | public: |
111 | LightGridLLReductionMat(); |
112 | |
113 | /** Binds parameter buffers and prepares any internal buffers. Must be called before execute(). */ |
114 | void setParams(const Vector3I& gridSize, const SPtr<GpuParamBlockBuffer>& gridParams, |
115 | const SPtr<GpuBuffer>& lightLLHeads, const SPtr<GpuBuffer>& lightLL, |
116 | const SPtr<GpuBuffer>& probeLLHeads, const SPtr<GpuBuffer>& probeLL); |
117 | |
118 | /** Binds the material for renderingand executes it. */ |
119 | void execute(const RendererView& view); |
120 | |
121 | /** Returns the buffers generated by execute(). */ |
122 | void getOutputs(SPtr<GpuBuffer>& gridLightOffsetsAndSize, SPtr<GpuBuffer>& gridLightIndices, |
123 | SPtr<GpuBuffer>& gridProbeOffsetsAndSize, SPtr<GpuBuffer>& gridProbeIndices) const; |
124 | private: |
125 | GpuParamBuffer mLightsLLHeadsParam; |
126 | GpuParamBuffer mLightsLLParam; |
127 | |
128 | GpuParamBuffer mProbesLLHeadsParam; |
129 | GpuParamBuffer mProbesLLParam; |
130 | |
131 | GpuParamBuffer mGridDataCounterParam; |
132 | |
133 | GpuParamBuffer mGridLightOffsetAndSizeParam; |
134 | GpuParamBuffer mGridLightIndicesParam; |
135 | |
136 | GpuParamBuffer mGridProbeOffsetAndSizeParam; |
137 | GpuParamBuffer mGridProbeIndicesParam; |
138 | |
139 | SPtr<GpuBuffer> mGridDataCounter; |
140 | |
141 | SPtr<GpuBuffer> mGridLightOffsetAndSize; |
142 | SPtr<GpuBuffer> mGridLightIndices; |
143 | |
144 | SPtr<GpuBuffer> mGridProbeOffsetAndSize; |
145 | SPtr<GpuBuffer> mGridProbeIndices; |
146 | |
147 | UINT32 mBufferNumCells; |
148 | Vector3I mGridSize; |
149 | }; |
150 | |
151 | /** |
152 | * Helper class that is used for generating a grid in view space, whose cells contain information about lights |
153 | * affecting them. Used for forward rendering. |
154 | */ |
155 | class LightGrid |
156 | { |
157 | public: |
158 | LightGrid(); |
159 | |
160 | /** Updates the light grid from the provided view. */ |
161 | void updateGrid(const RendererView& view, const VisibleLightData& lightData, const VisibleReflProbeData& probeData, |
162 | bool noLighting); |
163 | |
164 | /** |
165 | * Returns the buffers containing light indices per grid cell and global grid parameters. This data gets Updated on |
166 | * every call to updateGrid(). |
167 | */ |
168 | LightGridOutputs getOutputs() const; |
169 | |
170 | private: |
171 | SPtr<GpuParamBlockBuffer> mGridParamBuffer; |
172 | }; |
173 | |
174 | /** @} */ |
175 | }} |
176 | |