BsRenderBeast.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "BsRenderableElement.h"
  8. #include "BsSamplerOverrides.h"
  9. #include "BsRendererMaterial.h"
  10. #include "BsLightRendering.h"
  11. #include "BsPostProcessing.h"
  12. namespace BansheeEngine
  13. {
  14. /** @addtogroup RenderBeast
  15. * @{
  16. */
  17. class BeastRenderableElement;
  18. /** Semantics that may be used for signaling the renderer for what is a certain shader parameter used for. */
  19. static StringID RPS_GBufferA = "GBufferA";
  20. static StringID RPS_GBufferB = "GBufferB";
  21. static StringID RPS_GBufferDepth = "GBufferDepth";
  22. /** Basic shader that is used when no other is available. */
  23. class DefaultMaterial : public RendererMaterial<DefaultMaterial> { RMAT_DEF("Default.bsl"); };
  24. /** Data used by the renderer when rendering renderable handlers. */
  25. struct RenderableData
  26. {
  27. RenderableCore* renderable;
  28. Vector<BeastRenderableElement> elements;
  29. RenderableHandler* controller;
  30. };
  31. /** Data bound to the shader when rendering a specific renderable. */
  32. struct RenderableShaderData
  33. {
  34. Matrix4 worldTransform;
  35. Matrix4 invWorldTransform;
  36. Matrix4 worldNoScaleTransform;
  37. Matrix4 invWorldNoScaleTransform;
  38. float worldDeterminantSign;
  39. };
  40. /** Data bound to the shader when rendering a with a specific camera. */
  41. struct CameraShaderData
  42. {
  43. Vector3 viewDir;
  44. Vector3 viewOrigin;
  45. Matrix4 view;
  46. Matrix4 proj;
  47. Matrix4 viewProj;
  48. Matrix4 invProj;
  49. Matrix4 invViewProj;
  50. Matrix4 screenToWorld;
  51. Vector2 deviceZToWorldZ;
  52. Vector4 clipToUVScaleOffset;
  53. };
  54. /**
  55. * @copydoc RenderableElement
  56. *
  57. * Contains additional data specific to RenderBeast renderer.
  58. */
  59. class BS_BSRND_EXPORT BeastRenderableElement : public RenderableElement
  60. {
  61. public:
  62. /**
  63. * Optional overrides for material sampler states. Used when renderer wants to override certain sampling properties
  64. * on a global scale (for example filtering most commonly).
  65. */
  66. MaterialSamplerOverrides* samplerOverrides;
  67. /** Identifier of the owner renderable. */
  68. UINT32 renderableId;
  69. };
  70. /**
  71. * Default renderer for Banshee. Performs frustum culling, sorting and renders objects in custom ways determine by
  72. * renderable handlers.
  73. *
  74. * @note Sim thread unless otherwise noted.
  75. */
  76. class BS_BSRND_EXPORT RenderBeast : public Renderer
  77. {
  78. /** Render data for a single render target. */
  79. struct RenderTargetData
  80. {
  81. SPtr<RenderTargetCore> target;
  82. Vector<const CameraCore*> cameras;
  83. };
  84. /** Data used by the renderer for a camera. */
  85. struct CameraData
  86. {
  87. SPtr<RenderQueue> opaqueQueue;
  88. SPtr<RenderQueue> transparentQueue;
  89. SPtr<RenderTargets> target;
  90. PostProcessInfo postProcessInfo;
  91. };
  92. /** Data used by the renderer for lights. */
  93. struct LightData
  94. {
  95. LightCore* internal;
  96. };
  97. public:
  98. RenderBeast();
  99. ~RenderBeast() { }
  100. /** @copydoc Renderer::getName */
  101. const StringID& getName() const override;
  102. /** @copydoc Renderer::renderAll */
  103. void renderAll() override;
  104. /** Sets options used for controlling the rendering. */
  105. void setOptions(const SPtr<CoreRendererOptions>& options) override;
  106. /** Returns current set of options used for controlling the rendering. */
  107. SPtr<CoreRendererOptions> getOptions() const override;
  108. /** @copydoc Renderer::initialize */
  109. void initialize() override;
  110. /** @copydoc Renderer::destroy */
  111. void destroy() override;
  112. private:
  113. /** @copydoc Renderer::notifyCameraAdded */
  114. void notifyCameraAdded(const CameraCore* camera) override;
  115. /** @copydoc Renderer::notifyCameraUpdated */
  116. void notifyCameraUpdated(const CameraCore* camera, UINT32 updateFlag) override;
  117. /** @copydocRenderer::notifyCameraRemoved */
  118. void notifyCameraRemoved(const CameraCore* camera) override;
  119. /** @copydoc Renderer::notifyLightAdded */
  120. void notifyLightAdded(LightCore* light) override;
  121. /** @copydoc Renderer::notifyLightUpdated */
  122. void notifyLightUpdated(LightCore* light) override;
  123. /** @copydoc Renderer::notifyLightRemoved */
  124. void notifyLightRemoved(LightCore* light) override;
  125. /** @copydoc Renderer::notifyRenderableAdded */
  126. void notifyRenderableAdded(RenderableCore* renderable) override;
  127. /** @copydoc Renderer::notifyRenderableUpdated */
  128. void notifyRenderableUpdated(RenderableCore* renderable) override;
  129. /** @copydoc Renderer::notifyRenderableRemoved */
  130. void notifyRenderableRemoved(RenderableCore* renderable) override;
  131. /**
  132. * Updates (or adds) renderer specific data for the specified camera. Should be called whenever camera properties
  133. * change.
  134. *
  135. * @param[in] camera Camera whose data to update.
  136. * @param[in] forceRemove If true, the camera data will be removed instead of updated.
  137. */
  138. void updateCameraData(const CameraCore* camera, bool forceRemove = false);
  139. /**
  140. * Updates the render options on the core thread.
  141. *
  142. * @note Core thread only.
  143. */
  144. void syncRenderOptions(const RenderBeastOptions& options);
  145. /**
  146. * Performs rendering over all camera proxies.
  147. *
  148. * @param[in] time Current frame time in milliseconds.
  149. * @param[in] delta Time elapsed since the last frame.
  150. *
  151. * @note Core thread only.
  152. */
  153. void renderAllCore(float time, float delta);
  154. /**
  155. * Populates camera render queues by determining visible renderable object.
  156. *
  157. * @param[in] camera The camera to determine visibility for.
  158. */
  159. void determineVisible(const CameraCore& camera);
  160. /**
  161. * Renders all objects visible by the provided camera.
  162. *
  163. * @param[in] rtData Render target data containing the camera to render.
  164. * @param[in] camIdx Index of the camera to render.
  165. * @param[in] delta Time elapsed since the last frame.
  166. *
  167. * @note Core thread only.
  168. */
  169. void render(RenderTargetData& rtData, UINT32 camIdx, float delta);
  170. /**
  171. * Renders all overlay callbacks attached to the provided camera.
  172. *
  173. * @param[in] rtData Render target data containing the camera to render.
  174. * @param[in] camIdx Index of the camera to render.
  175. * @param[in] delta Time elapsed since the last frame.
  176. *
  177. * @note Core thread only.
  178. */
  179. void renderOverlay(RenderTargetData& rtData, UINT32 camIdx, float delta);
  180. /** Creates data used by the renderer on the core thread. */
  181. void initializeCore();
  182. /** Destroys data used by the renderer on the core thread. */
  183. void destroyCore();
  184. /**
  185. * Checks all sampler overrides in case material sampler states changed, and updates them.
  186. *
  187. * @param[in] force If true, all sampler overrides will be updated, regardless of a change in the material
  188. * was detected or not.
  189. */
  190. void refreshSamplerOverrides(bool force = false);
  191. /**
  192. * Extracts the necessary values from the projection matrix that allow you to transform device Z value into
  193. * world Z value.
  194. *
  195. * @param[in] projMatrix Projection matrix that was used to create the device Z value to transform.
  196. * @return Returns two values that can be used to transform device z to world z using this formula:
  197. * z = (deviceZ + y) * x.
  198. */
  199. static Vector2 getDeviceZTransform(const Matrix4& projMatrix);
  200. /**
  201. * Populates the provided camera shader data object with data from the provided camera. The object can then be used
  202. * for populating per-camera parameter buffers.
  203. *
  204. * @note Core thread.
  205. */
  206. static CameraShaderData getCameraShaderData(const CameraCore& camera);
  207. /**
  208. * Activates the specified pass on the pipeline.
  209. *
  210. * @param[in] pass Pass to activate.
  211. *
  212. * @note Core thread.
  213. */
  214. static void setPass(const SPtr<PassCore>& pass);
  215. /**
  216. * Sets parameters (textures, samplers, buffers) for the currently active pass.
  217. *
  218. * @param[in] passParams Structure containing parameters for all stages of the pass.
  219. * @param[in] samplerOverrides Optional samplers to use instead of the those in the pass parameters. Number of
  220. * samplers must match number in pass parameters.
  221. *
  222. * @note Core thread.
  223. */
  224. static void setPassParams(const SPtr<PassParametersCore>& passParams, const PassSamplerOverrides* samplerOverrides);
  225. // Core thread only fields
  226. Vector<RenderTargetData> mRenderTargets;
  227. UnorderedMap<const CameraCore*, CameraData> mCameraData;
  228. UnorderedMap<SPtr<MaterialCore>, MaterialSamplerOverrides*> mSamplerOverrides;
  229. Vector<RenderableData> mRenderables;
  230. Vector<RenderableShaderData> mRenderableShaderData;
  231. Vector<Bounds> mWorldBounds;
  232. Vector<LightData> mDirectionalLights;
  233. Vector<LightData> mPointLights;
  234. Vector<Sphere> mLightWorldBounds;
  235. SPtr<RenderBeastOptions> mCoreOptions;
  236. DefaultMaterial* mDefaultMaterial;
  237. PointLightInMat* mPointLightInMat;
  238. PointLightOutMat* mPointLightOutMat;
  239. DirectionalLightMat* mDirLightMat;
  240. // Sim thread only fields
  241. StaticRenderableHandler* mStaticHandler;
  242. SPtr<RenderBeastOptions> mOptions;
  243. bool mOptionsDirty;
  244. };
  245. /** @} */
  246. }