BsRenderBeast.h 8.5 KB

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