BsRenderBeast.h 7.8 KB

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