BsRenderBeast.h 7.2 KB

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