BsRenderBeast.h 8.4 KB

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