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 "BsRendererLight.h"
7#include "BsRendererView.h"
8#include "BsRendererParticles.h"
9#include "Shading/BsLightProbes.h"
10#include "Utility/BsSamplerOverrides.h"
11
12namespace bs
13{
14 struct EvaluatedAnimationData;
15 struct ParticlePerFrameData;
16
17 namespace ct
18 {
19 struct RendererDecal;
20 class Decal;
21 struct FrameInfo;
22
23 /** @addtogroup RenderBeast
24 * @{
25 */
26
27 // Limited by max number of array elements in texture for DX11 hardware
28 constexpr UINT32 MaxReflectionCubemaps = 2048 / 6;
29
30 /** Contains most scene objects relevant to the renderer. */
31 struct SceneInfo
32 {
33 // Cameras and render targets
34 Vector<RendererRenderTarget> renderTargets;
35 Vector<RendererView*> views;
36 UnorderedMap<const Camera*, UINT32> cameraToView;
37
38 // Renderables
39 Vector<RendererRenderable*> renderables;
40 Vector<CullInfo> renderableCullInfos;
41
42 // Lights
43 Vector<RendererLight> directionalLights;
44 Vector<RendererLight> radialLights;
45 Vector<RendererLight> spotLights;
46 Vector<Sphere> radialLightWorldBounds;
47 Vector<Sphere> spotLightWorldBounds;
48
49 // Reflection probes
50 Vector<RendererReflectionProbe> reflProbes;
51 Vector<Sphere> reflProbeWorldBounds;
52 Vector<bool> reflProbeCubemapArrayUsedSlots;
53 SPtr<Texture> reflProbeCubemapsTex;
54
55 // Light probes (indirect lighting)
56 LightProbes lightProbes;
57
58 // Particles
59 Vector<RendererParticles> particleSystems;
60 Vector<CullInfo> particleSystemCullInfos;
61
62 // Decals
63 Vector<RendererDecal> decals;
64 Vector<CullInfo> decalCullInfos;
65
66 // Sky
67 Skybox* skybox = nullptr;
68
69 // Buffers for various transient data that gets rebuilt every frame
70 //// Rebuilt every frame
71 mutable Vector<bool> renderableReady;
72 };
73
74 /** Contains information about the scene (e.g. renderables, lights, cameras) required by the renderer. */
75 class RendererScene
76 {
77 public:
78 RendererScene(const SPtr<RenderBeastOptions>& options);
79 ~RendererScene();
80
81 /** Registers a new camera in the scene. */
82 void registerCamera(Camera* camera);
83
84 /** Updates information about a previously registered camera. */
85 void updateCamera(Camera* camera, UINT32 updateFlag);
86
87 /** Removes a camera from the scene. */
88 void unregisterCamera(Camera* camera);
89
90 /** Registers a new light in the scene. */
91 void registerLight(Light* light);
92
93 /** Updates information about a previously registered light. */
94 void updateLight(Light* light);
95
96 /** Removes a light from the scene. */
97 void unregisterLight(Light* light);
98
99 /** Registers a new renderable object in the scene. */
100 void registerRenderable(Renderable* renderable);
101
102 /** Updates information about a previously registered renderable object. */
103 void updateRenderable(Renderable* renderable);
104
105 /** Removes a renderable object from the scene. */
106 void unregisterRenderable(Renderable* renderable);
107
108 /** Registers a new reflection probe in the scene. */
109 void registerReflectionProbe(ReflectionProbe* probe);
110
111 /** Updates information about a previously registered reflection probe. */
112 void updateReflectionProbe(ReflectionProbe* probe, bool texture);
113
114 /** Removes a reflection probe from the scene. */
115 void unregisterReflectionProbe(ReflectionProbe* probe);
116
117 /** Updates the index at which the reflection probe's texture is stored at, in the global array. */
118 void setReflectionProbeArrayIndex(UINT32 probeIdx, UINT32 arrayIdx, bool markAsClean);
119
120 /** Registers a new light probe volume in the scene. */
121 void registerLightProbeVolume(LightProbeVolume* volume);
122
123 /** Updates information about a previously registered light probe volume. */
124 void updateLightProbeVolume(LightProbeVolume* volume);
125
126 /** Removes a light probe volume from the scene. */
127 void unregisterLightProbeVolume(LightProbeVolume* volume);
128
129 /**
130 * Rebuilds any light probe related information. Should be called once immediately before rendering. If no change
131 * is detected since the last call, the call does nothing.
132 */
133 void updateLightProbes();
134
135 /** Registers a new sky texture in the scene. */
136 void registerSkybox(Skybox* skybox);
137
138 /** Removes a skybox from the scene. */
139 void unregisterSkybox(Skybox* skybox);
140
141 /** Registers a new particle system in the scene. */
142 void registerParticleSystem(ParticleSystem* particleSystem);
143
144 /** Updates information about a previously registered particle system. */
145 void updateParticleSystem(ParticleSystem* particleSystem, bool tfrmOnly);
146
147 /** Removes a particle system from the scene. */
148 void unregisterParticleSystem(ParticleSystem* particleSystem);
149
150 /** Registers a new decal object in the scene. */
151 void registerDecal(Decal* decal);
152
153 /** Updates information about a previously registered decal object. */
154 void updateDecal(Decal* decal);
155
156 /** Removes a decal object from the scene. */
157 void unregisterDecal(Decal* decal);
158
159 /** Returns a container with all relevant scene objects. */
160 const SceneInfo& getSceneInfo() const { return mInfo; }
161
162 /** Updates scene according to the newly provided renderer options. */
163 void setOptions(const SPtr<RenderBeastOptions>& options);
164
165 /**
166 * Checks all sampler overrides in case material sampler states changed, and updates them.
167 *
168 * @param[in] force If true, all sampler overrides will be updated, regardless of a change in the material
169 * was detected or not.
170 */
171 void refreshSamplerOverrides(bool force = false);
172
173 /** Updates global per frame parameter buffers with new values. To be called at the start of every frame. */
174 void setParamFrameParams(float time);
175
176 /**
177 * Performs necessary steps to make a renderable ready for rendering. This must be called at least once every frame
178 * for every renderable that will be drawn. Multiple calls for the same renderable during a single frame will result
179 * in a no-op.
180 *
181 * @param[in] idx Index of the renderable to prepare.
182 * @param[in] frameInfo Global information describing the current frame.
183 */
184 void prepareRenderable(UINT32 idx, const FrameInfo& frameInfo);
185
186 /**
187 * Performs necessary steps to make a particle system ready for rendering. This must be called at least once every
188 * frame for every particle system that will be drawn.
189 *
190 * @param[in] idx Index of the particle system to prepare.
191 * @param[in] frameInfo Global information describing the current frame.
192 */
193 void prepareParticleSystem(UINT32 idx, const FrameInfo& frameInfo);
194
195 /**
196 * Performs necessary steps to make a decal ready for rendering. This must be called at least once every frame
197 * for every decal that will be drawn.
198 *
199 * @param[in] idx Index of the decal to prepare.
200 * @param[in] frameInfo Global information describing the current frame.
201 */
202 void prepareDecal(UINT32 idx, const FrameInfo& frameInfo);
203
204 /** Updates the bounds for all the particle systems from the provided object. */
205 void updateParticleSystemBounds(const ParticlePerFrameData* particleRenderData);
206
207 /** Returns a modifiable version of SceneInfo. Only to be used by friends who know what they are doing. */
208 SceneInfo& _getSceneInfo() { return mInfo; }
209 private:
210 /** Creates a renderer view descriptor for the particular camera. */
211 RENDERER_VIEW_DESC createViewDesc(Camera* camera) const;
212
213 /**
214 * Find the render target the camera belongs to and adds it to the relevant list. If the camera was previously
215 * registered with some other render target it will be removed from it and added to the new target.
216 */
217 void updateCameraRenderTargets(Camera* camera, bool remove = false);
218
219 /**
220 * Allocates (or returns existing) set of sampler state overrides that can be used for the provided render
221 * element.
222 */
223 MaterialSamplerOverrides* allocSamplerStateOverrides(RenderElement& elem);
224
225 /** Frees sampler state overrides previously allocated with allocSamplerStateOverrides(). */
226 void freeSamplerStateOverrides(RenderElement& elem);
227
228 SceneInfo mInfo;
229 SPtr<GpuParamBlockBuffer> mPerFrameParamBuffer;
230 UnorderedMap<SamplerOverrideKey, MaterialSamplerOverrides*> mSamplerOverrides;
231
232 SPtr<RenderBeastOptions> mOptions;
233 };
234
235 BS_PARAM_BLOCK_BEGIN(PerFrameParamDef)
236 BS_PARAM_BLOCK_ENTRY(float, gTime)
237 BS_PARAM_BLOCK_END
238
239 extern PerFrameParamDef gPerFrameParamDef;
240
241 /** Basic shader that is used when no other is available. */
242 class DefaultMaterial : public RendererMaterial<DefaultMaterial> { RMAT_DEF("Default.bsl"); };
243
244 /** @} */
245}}
246