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#include "RenderAPI/BsGpuPipelineParamInfo.h"
9#include "BsRendererLight.h"
10#include "BsRendererReflectionProbe.h"
11
12namespace bs { namespace ct
13{
14 struct SkyInfo;
15 struct SceneInfo;
16 class RendererViewGroup;
17
18 /** @addtogroup RenderBeast
19 * @{
20 */
21
22 BS_PARAM_BLOCK_BEGIN(TiledLightingParamDef)
23 BS_PARAM_BLOCK_ENTRY(Vector4I, gLightCounts)
24 BS_PARAM_BLOCK_ENTRY(Vector2I, gLightStrides)
25 BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
26 BS_PARAM_BLOCK_END
27
28 extern TiledLightingParamDef gTiledLightingParamDef;
29
30 /** Shader that performs a lighting pass over data stored in the Gbuffer. */
31 class TiledDeferredLightingMat : public RendererMaterial<TiledDeferredLightingMat>
32 {
33 RMAT_DEF_CUSTOMIZED("TiledDeferredLighting.bsl");
34
35 /** Helper method used for initializing variations of this material. */
36 template<UINT32 msaa>
37 static const ShaderVariation& getVariation()
38 {
39 static ShaderVariation variation = ShaderVariation(
40 {
41 ShaderVariation::Param("MSAA_COUNT", msaa)
42 });
43
44 return variation;
45 }
46 public:
47 TiledDeferredLightingMat();
48
49 /** Binds the material for rendering, sets up parameters and executes it. */
50 void execute(const RendererView& view, const VisibleLightData& lightData, const GBufferTextures& gbuffer,
51 const SPtr<Texture>& inputTexture, const SPtr<Texture>& lightAccumTex, const SPtr<Texture>& lightAccumTexArray,
52 const SPtr<Texture>& msaaCoverage);
53
54 /** Returns the material variation matching the provided parameters. */
55 static TiledDeferredLightingMat* getVariation(UINT32 msaaCount);
56
57 private:
58 UINT32 mSampleCount;
59 GBufferParams mGBufferParams;
60
61 GpuParamBuffer mLightBufferParam;
62 GpuParamLoadStoreTexture mOutputTextureParam;
63
64 GpuParamTexture mInColorTextureParam;
65 GpuParamTexture mMSAACoverageTexParam;
66
67 SPtr<GpuParamBlockBuffer> mParamBuffer;
68
69 static const UINT32 TILE_SIZE;
70 };
71
72 /**
73 * Moves data from a texture array into a MSAA texture. Primarily useful when needing to do unordered writes to a
74 * MSAA texture which isn't directly supported on some backends, so writes are done to a texture array instead. The
75 * array is expected to have the same number of layers as the number of samples in the MSAA texture, each layer
76 * containing a sample for that specific pixel.
77 */
78 class TextureArrayToMSAATexture : public RendererMaterial<TextureArrayToMSAATexture>
79 {
80 RMAT_DEF("TextureArrayToMSAATexture.bsl");
81
82 public:
83 TextureArrayToMSAATexture();
84
85 /** Binds the material for rendering, sets up parameters and executes it. */
86 void execute(const SPtr<Texture>& inputArray, const SPtr<Texture>& target);
87
88 private:
89 GpuParamTexture mInputParam;
90 };
91
92 BS_PARAM_BLOCK_BEGIN(ClearLoadStoreParamDef)
93 BS_PARAM_BLOCK_ENTRY(Vector2I, gSize)
94 BS_PARAM_BLOCK_ENTRY(Vector4, gFloatClearVal)
95 BS_PARAM_BLOCK_ENTRY(Vector4I, gIntClearVal)
96 BS_PARAM_BLOCK_END
97
98 extern ClearLoadStoreParamDef gClearLoadStoreParamDef;
99
100 /** Possible object types used as clear destinations by ClearLoadStoreMat. */
101 enum class ClearLoadStoreType { Texture, TextureArray, Buffer, StructuredBuffer };
102
103 /** Possible data types used in destination objects in ClearLoadStoreMat. */
104 enum class ClearLoadStoreDataType { Float, Int };
105
106 /** Clears the provided texture to zero, using a compute shader. */
107 class ClearLoadStoreMat : public RendererMaterial<ClearLoadStoreMat>
108 {
109 RMAT_DEF_CUSTOMIZED("ClearLoadStore.bsl");
110
111 public:
112 ClearLoadStoreMat();
113
114 /**
115 * Binds the material for rendering, sets up parameters and executes it. Only works on variations of
116 * this material intended for textures and texture arrays.
117 */
118 void execute(const SPtr<Texture>& target, const Color& clearValue = Color::ZERO,
119 const TextureSurface& surface = TextureSurface::COMPLETE);
120
121 /**
122 * Binds the material for rendering, sets up parameters and executes it. Only works on variations of
123 * this material intended for buffers.
124 */
125 void execute(const SPtr<GpuBuffer>& target, const Color& clearValue = Color::ZERO);
126
127 /**
128 * Returns the material variation matching the provided parameters.
129 *
130 * @param[in] objType Type of object used for clear source.
131 * @param[in] dataType Base data type stored in the clear source object.
132 * @param[in] numComponents Number of components in the source objects's data type (e.g. float2, float4).
133 * In range [1, 4].
134 * @return Material variation matching the provided values.
135 */
136 static ClearLoadStoreMat* getVariation(ClearLoadStoreType objType, ClearLoadStoreDataType dataType,
137 UINT32 numComponents);
138 private:
139 /** TILE_SIZE * TILE_SIZE is the number of pixels to process per thread. */
140 static constexpr UINT32 TILE_SIZE = 4;
141
142 /** Number of threads to launch per work group. */
143 static constexpr UINT32 NUM_THREADS = 128;
144
145 GpuParamLoadStoreTexture mOutputTextureParam;
146 GpuParamBuffer mOutputBufferParam;
147 SPtr<GpuParamBlockBuffer> mParamBuffer;
148 };
149
150 BS_PARAM_BLOCK_BEGIN(TiledImageBasedLightingParamDef)
151 BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
152 BS_PARAM_BLOCK_END
153
154 extern TiledImageBasedLightingParamDef gTiledImageBasedLightingParamDef;
155
156 /** Shader that performs a lighting pass over data stored in the Gbuffer. */
157 class TiledDeferredImageBasedLightingMat : public RendererMaterial<TiledDeferredImageBasedLightingMat>
158 {
159 RMAT_DEF_CUSTOMIZED("TiledDeferredImageBasedLighting.bsl");
160
161 /** Helper method used for initializing variations of this material. */
162 template<UINT32 msaa>
163 static const ShaderVariation& getVariation()
164 {
165 static ShaderVariation variation = ShaderVariation(
166 {
167 ShaderVariation::Param("MSAA_COUNT", msaa)
168 });
169
170 return variation;
171 }
172 public:
173 /** Container for parameters to be passed to the execute() method. */
174 struct Inputs
175 {
176 GBufferTextures gbuffer;
177 SPtr<Texture> lightAccumulation;
178 SPtr<Texture> sceneColorTex;
179 SPtr<Texture> sceneColorTexArray;
180 SPtr<Texture> preIntegratedGF;
181 SPtr<Texture> ambientOcclusion;
182 SPtr<Texture> ssr;
183 SPtr<Texture> msaaCoverage;
184 };
185
186 TiledDeferredImageBasedLightingMat();
187
188 /** Binds the material for rendering, sets up parameters and executes it. */
189 void execute(const RendererView& view, const SceneInfo& sceneInfo, const VisibleReflProbeData& probeData,
190 const Inputs& inputs);
191
192 /** Returns the material variation matching the provided parameters. */
193 static TiledDeferredImageBasedLightingMat* getVariation(UINT32 msaaCount);
194
195 private:
196 UINT32 mSampleCount;
197
198 GpuParamTexture mGBufferA;
199 GpuParamTexture mGBufferB;
200 GpuParamTexture mGBufferC;
201 GpuParamTexture mGBufferDepth;
202
203 GpuParamTexture mInColorTextureParam;
204 GpuParamTexture mMSAACoverageTexParam;
205
206 ImageBasedLightingParams mImageBasedParams;
207
208 GpuParamLoadStoreTexture mOutputTextureParam;
209
210 SPtr<GpuParamBlockBuffer> mParamBuffer;
211 ReflProbeParamBuffer mReflProbeParamBuffer;
212
213 static const UINT32 TILE_SIZE;
214 };
215
216 /** @} */
217}}
218