BsRenderBeast.h 8.4 KB

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