BsRenderBeast.h 8.1 KB

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