BsRenderBeast.h 8.0 KB

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