BsRenderBeast.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "BsRenderer.h"
  6. #include "BsBounds.h"
  7. #include "BsSamplerOverrides.h"
  8. #include "BsRendererMaterial.h"
  9. #include "BsLightRendering.h"
  10. #include "BsObjectRendering.h"
  11. #include "BsPostProcessing.h"
  12. #include "BsRendererCamera.h"
  13. #include "BsRendererObject.h"
  14. namespace bs
  15. {
  16. struct RendererAnimationData;
  17. namespace ct
  18. {
  19. class LightGrid;
  20. /** @addtogroup RenderBeast
  21. * @{
  22. */
  23. /** Semantics that may be used for signaling the renderer for what is a certain shader parameter used for. */
  24. static StringID RPS_GBufferA = "GBufferA";
  25. static StringID RPS_GBufferB = "GBufferB";
  26. static StringID RPS_GBufferC = "GBufferC";
  27. static StringID RPS_GBufferDepth = "GBufferDepth";
  28. static StringID RPS_BoneMatrices = "BoneMatrices";
  29. /**
  30. * Default renderer for Banshee. Performs frustum culling, sorting and renders all scene objects while applying
  31. * lighting, shadowing, special effects and post-processing.
  32. *
  33. * @note Sim thread unless otherwise noted.
  34. */
  35. class RenderBeast : public Renderer
  36. {
  37. /** Renderer information specific to a single render target. */
  38. struct RendererRenderTarget
  39. {
  40. SPtr<RenderTarget> target;
  41. Vector<const Camera*> cameras;
  42. };
  43. /** Renderer information for a single material. */
  44. struct RendererMaterial
  45. {
  46. Vector<SPtr<GpuParamsSet>> params;
  47. UINT32 matVersion;
  48. };
  49. /** Contains information global to an entire frame. */
  50. struct FrameInfo
  51. {
  52. FrameInfo(float timeDelta, const RendererAnimationData& animData)
  53. :timeDelta(timeDelta), animData(animData)
  54. { }
  55. float timeDelta;
  56. const RendererAnimationData& animData;
  57. };
  58. /** Information about an active reflection probe. */
  59. struct ReflProbeInfo
  60. {
  61. ReflectionProbe* probe;
  62. UINT32 arrayIdx;
  63. SPtr<Texture> texture;
  64. bool customTexture : 1;
  65. bool textureDirty : 1;
  66. bool arrayDirty : 1;
  67. bool errorFlagged : 1;
  68. };
  69. public:
  70. RenderBeast();
  71. ~RenderBeast() { }
  72. /** @copydoc Renderer::getName */
  73. const StringID& getName() const override;
  74. /** @copydoc Renderer::renderAll */
  75. void renderAll() override;
  76. /** Sets options used for controlling the rendering. */
  77. void setOptions(const SPtr<RendererOptions>& options) override;
  78. /** Returns current set of options used for controlling the rendering. */
  79. SPtr<RendererOptions> getOptions() const override;
  80. /** @copydoc Renderer::initialize */
  81. void initialize() override;
  82. /** @copydoc Renderer::destroy */
  83. void destroy() override;
  84. /** @copydoc Renderer::createPostProcessSettings */
  85. SPtr<PostProcessSettings> createPostProcessSettings() const override;
  86. private:
  87. /** @copydoc Renderer::notifyCameraAdded */
  88. void notifyCameraAdded(const Camera* camera) override;
  89. /** @copydoc Renderer::notifyCameraUpdated */
  90. void notifyCameraUpdated(const Camera* camera, UINT32 updateFlag) override;
  91. /** @copydocRenderer::notifyCameraRemoved */
  92. void notifyCameraRemoved(const Camera* camera) override;
  93. /** @copydoc Renderer::notifyLightAdded */
  94. void notifyLightAdded(Light* light) override;
  95. /** @copydoc Renderer::notifyLightUpdated */
  96. void notifyLightUpdated(Light* light) override;
  97. /** @copydoc Renderer::notifyLightRemoved */
  98. void notifyLightRemoved(Light* light) override;
  99. /** @copydoc Renderer::notifyRenderableAdded */
  100. void notifyRenderableAdded(Renderable* renderable) override;
  101. /** @copydoc Renderer::notifyRenderableUpdated */
  102. void notifyRenderableUpdated(Renderable* renderable) override;
  103. /** @copydoc Renderer::notifyRenderableRemoved */
  104. void notifyRenderableRemoved(Renderable* renderable) override;
  105. /** @copydoc Renderer::notifyReflectionProbeAdded */
  106. void notifyReflectionProbeAdded(ReflectionProbe* probe) override;
  107. /** @copydoc Renderer::notifyReflectionProbeUpdated */
  108. void notifyReflectionProbeUpdated(ReflectionProbe* probe) override;
  109. /** @copydoc Renderer::notifyReflectionProbeRemoved */
  110. void notifyReflectionProbeRemoved(ReflectionProbe* probe) override;
  111. /**
  112. * Updates (or adds) renderer specific data for the specified camera. Should be called whenever camera properties
  113. * change.
  114. *
  115. * @param[in] camera Camera whose data to update.
  116. * @param[in] forceRemove If true, the camera data will be removed instead of updated.
  117. * @return Renderer camera object that represents the camera. Null if camera was removed.
  118. */
  119. RendererCamera* updateCameraData(const Camera* camera, bool forceRemove = false);
  120. /**
  121. * Updates the render options on the core thread.
  122. *
  123. * @note Core thread only.
  124. */
  125. void syncOptions(const RenderBeastOptions& options);
  126. /**
  127. * Performs rendering over all camera proxies.
  128. *
  129. * @param[in] time Current frame time in milliseconds.
  130. * @param[in] delta Time elapsed since the last frame.
  131. *
  132. * @note Core thread only.
  133. */
  134. void renderAllCore(float time, float delta);
  135. /**
  136. * Renders all provided views.
  137. *
  138. * @note Core thread only.
  139. */
  140. void renderViews(RendererCamera** views, UINT32 numViews, const FrameInfo& frameInfo);
  141. /**
  142. * Renders all objects visible by the provided view.
  143. *
  144. * @note Core thread only.
  145. */
  146. void renderView(RendererCamera* viewInfo, float frameDelta);
  147. /**
  148. * Renders all overlay callbacks of the provided view.
  149. *
  150. * @note Core thread only.
  151. */
  152. void renderOverlay(RendererCamera* viewInfo);
  153. /**
  154. * Renders a single element of a renderable object.
  155. *
  156. * @param[in] element Element to render.
  157. * @param[in] passIdx Index of the material pass to render the element with.
  158. * @param[in] bindPass If true the material pass will be bound for rendering, if false it is assumed it is
  159. * already bound.
  160. * @param[in] viewProj View projection matrix of the camera the element is being rendered with.
  161. */
  162. void renderElement(const BeastRenderableElement& element, UINT32 passIdx, bool bindPass, const Matrix4& viewProj);
  163. /**
  164. * Captures the scene at the specified location into a cubemap.
  165. *
  166. * @param[in] cubemap Cubemap to store the results in.
  167. * @param[in] position Position to capture the scene at.
  168. * @param[in] hdr If true scene will be captured in a format that supports high dynamic range.
  169. * @param[in] frameInfo Global information about the the frame currently being rendered.
  170. */
  171. void captureSceneCubeMap(const SPtr<Texture>& cubemap, const Vector3& position, bool hdr, const FrameInfo& frameInfo);
  172. /** Creates data used by the renderer on the core thread. */
  173. void initializeCore();
  174. /** Destroys data used by the renderer on the core thread. */
  175. void destroyCore();
  176. /** Updates reflection probes, rendering ones that are dirty and updating the global probe cubemap array. */
  177. void updateReflectionProbes(const FrameInfo& frameInfo);
  178. /**
  179. * Checks all sampler overrides in case material sampler states changed, and updates them.
  180. *
  181. * @param[in] force If true, all sampler overrides will be updated, regardless of a change in the material
  182. * was detected or not.
  183. */
  184. void refreshSamplerOverrides(bool force = false);
  185. // Core thread only fields
  186. Vector<RendererRenderTarget> mRenderTargets;
  187. UnorderedMap<const Camera*, RendererCamera*> mCameras;
  188. UnorderedMap<SamplerOverrideKey, MaterialSamplerOverrides*> mSamplerOverrides;
  189. Vector<RendererObject*> mRenderables;
  190. Vector<CullInfo> mRenderableCullInfos;
  191. Vector<bool> mRenderableVisibility; // Transient
  192. Vector<RendererLight> mDirectionalLights;
  193. Vector<RendererLight> mRadialLights;
  194. Vector<RendererLight> mSpotLights;
  195. Vector<Sphere> mPointLightWorldBounds;
  196. Vector<Sphere> mSpotLightWorldBounds;
  197. Vector<ReflProbeInfo> mReflProbes;
  198. Vector<Sphere> mReflProbeWorldBounds;
  199. Vector<bool> mCubemapArrayUsedSlots;
  200. SPtr<Texture> mCubemapArrayTex;
  201. SPtr<RenderBeastOptions> mCoreOptions;
  202. DefaultMaterial* mDefaultMaterial;
  203. ITiledDeferredLightingMat* mTiledDeferredLightingMats[4];
  204. FlatFramebufferToTextureMat* mFlatFramebufferToTextureMat;
  205. SkyboxMat<false>* mSkyboxMat;
  206. SkyboxMat<true>* mSkyboxSolidColorMat;
  207. GPULightData* mGPULightData;
  208. LightGrid* mLightGrid;
  209. ObjectRenderer* mObjectRenderer;
  210. // Sim thread only fields
  211. SPtr<RenderBeastOptions> mOptions;
  212. bool mOptionsDirty;
  213. // Helpers to avoid memory allocations
  214. Vector<LightData> mLightDataTemp;
  215. Vector<bool> mLightVisibilityTemp;
  216. };
  217. /** @} */
  218. }}