BsRenderBeast.h 8.2 KB

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