BsRenderBeast.h 8.3 KB

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