BsRenderer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "String/BsStringID.h"
  6. #include "Renderer/BsRendererMeshData.h"
  7. #include "Material/BsShaderVariation.h"
  8. namespace bs
  9. {
  10. class RendererExtension;
  11. class LightProbeVolume;
  12. struct RenderSettings;
  13. namespace ct
  14. {
  15. class RendererTask;
  16. class LightProbeVolume;
  17. /** @addtogroup Renderer-Internal
  18. * @{
  19. */
  20. /** Common shader variations. */
  21. /** Returns a specific vertex input shader variation. */
  22. template<bool skinned, bool morph>
  23. static const ShaderVariation& getVertexInputVariation()
  24. {
  25. static ShaderVariation variation = ShaderVariation({
  26. ShaderVariation::Param("SKINNED", skinned),
  27. ShaderVariation::Param("MORPH", morph),
  28. });
  29. return variation;
  30. }
  31. /** Returns a specific forward rendering shader variation. */
  32. template<bool skinned, bool morph, bool clustered>
  33. static const ShaderVariation& getForwardRenderingVariation()
  34. {
  35. static ShaderVariation variation = ShaderVariation({
  36. ShaderVariation::Param("SKINNED", skinned),
  37. ShaderVariation::Param("MORPH", morph),
  38. ShaderVariation::Param("CLUSTERED", clustered),
  39. });
  40. return variation;
  41. }
  42. /** Technique tags. */
  43. static StringID RTag_Skinned = "Skinned";
  44. static StringID RTag_Morph = "Morph";
  45. static StringID RTag_SkinnedMorph = "SkinnedMorph";
  46. /** Set of options that can be used for controlling the renderer. */
  47. struct BS_CORE_EXPORT RendererOptions
  48. {
  49. virtual ~RendererOptions() { }
  50. };
  51. /** Settings that control renderer scene capture. */
  52. struct CaptureSettings
  53. {
  54. /** If true scene will be captured in a format that supports high dynamic range. */
  55. bool hdr = true;
  56. /**
  57. * When enabled the alpha channel of the final render target will be populated with an encoded depth value.
  58. * Parameters @p depthEncodeNear and @p depthEncodeFar control which range of the depth buffer to encode.
  59. */
  60. bool encodeDepth = false;
  61. /**
  62. * Controls at which position to start encoding depth, in view space. Only relevant with @p encodeDepth is enabled.
  63. * Depth will be linearly interpolated between this value and @p depthEncodeFar.
  64. */
  65. float depthEncodeNear = 0.0f;
  66. /**
  67. * Controls at which position to stop encoding depth, in view space. Only relevant with @p encodeDepth is enabled.
  68. * Depth will be linearly interpolated between @p depthEncodeNear and this value.
  69. */
  70. float depthEncodeFar = 0.0f;
  71. };
  72. /**
  73. * Primarily rendering class that allows you to specify how to render objects that exist in the scene graph. You need
  74. * to provide your own implementation of your class.
  75. *
  76. * @note
  77. * Normally you would iterate over all cameras, find visible objects for each camera and render those objects in some
  78. * way.
  79. */
  80. class BS_CORE_EXPORT Renderer
  81. {
  82. public:
  83. Renderer();
  84. virtual ~Renderer() { }
  85. /** Initializes the renderer. Must be called before using the renderer. */
  86. virtual void initialize() { }
  87. /** Called every frame. Triggers render task callbacks. */
  88. void update();
  89. /** Cleans up the renderer. Must be called before the renderer is deleted. */
  90. virtual void destroy() { }
  91. /** Name of the renderer. Used by materials to find an appropriate technique for this renderer. */
  92. virtual const StringID& getName() const = 0;
  93. /** Called in order to render all currently active cameras. */
  94. virtual void renderAll() = 0;
  95. /**
  96. * Called whenever a new camera is created.
  97. *
  98. * @note Core thread.
  99. */
  100. virtual void notifyCameraAdded(Camera* camera) { }
  101. /**
  102. * Called whenever a camera's position or rotation is updated.
  103. *
  104. * @param[in] camera Camera that was updated.
  105. * @param[in] updateFlag Optional flag that allows the camera to signal to the renderer exactly what was updated.
  106. *
  107. * @note Core thread.
  108. */
  109. virtual void notifyCameraUpdated(Camera* camera, UINT32 updateFlag) { }
  110. /**
  111. * Called whenever a camera is destroyed.
  112. *
  113. * @note Core thread.
  114. */
  115. virtual void notifyCameraRemoved(Camera* camera) { }
  116. /**
  117. * Called whenever a new renderable is created.
  118. *
  119. * @note Core thread.
  120. */
  121. virtual void notifyRenderableAdded(Renderable* renderable) { }
  122. /**
  123. * Called whenever a renderable is updated.
  124. *
  125. * @note Core thread.
  126. */
  127. virtual void notifyRenderableUpdated(Renderable* renderable) { }
  128. /**
  129. * Called whenever a renderable is destroyed.
  130. *
  131. * @note Core thread.
  132. */
  133. virtual void notifyRenderableRemoved(Renderable* renderable) { }
  134. /**
  135. * Called whenever a new light is created.
  136. *
  137. * @note Core thread.
  138. */
  139. virtual void notifyLightAdded(Light* light) { }
  140. /**
  141. * Called whenever a light is updated.
  142. *
  143. * @note Core thread.
  144. */
  145. virtual void notifyLightUpdated(Light* light) { }
  146. /**
  147. * Called whenever a light is destroyed.
  148. *
  149. * @note Core thread.
  150. */
  151. virtual void notifyLightRemoved(Light* light) { }
  152. /**
  153. * Called whenever a new reflection probe is created.
  154. *
  155. * @note Core thread.
  156. */
  157. virtual void notifyReflectionProbeAdded(ReflectionProbe* probe) { }
  158. /**
  159. * Called whenever a reflection probe is updated.
  160. *
  161. * @note Core thread.
  162. */
  163. virtual void notifyReflectionProbeUpdated(ReflectionProbe* probe, bool texture) { }
  164. /**
  165. * Called whenever a reflection probe is destroyed.
  166. *
  167. * @note Core thread.
  168. */
  169. virtual void notifyReflectionProbeRemoved(ReflectionProbe* probe) { }
  170. /**
  171. * Called whenever a new light probe volume is created.
  172. *
  173. * @note Core thread.
  174. */
  175. virtual void notifyLightProbeVolumeAdded(LightProbeVolume* volume) { }
  176. /**
  177. * Called whenever a light probe volume is updated.
  178. *
  179. * @note Core thread.
  180. */
  181. virtual void notifyLightProbeVolumeUpdated(LightProbeVolume* volume) { }
  182. /**
  183. * Called whenever a light probe volume is destroyed.
  184. *
  185. * @note Core thread.
  186. */
  187. virtual void notifyLightProbeVolumeRemoved(LightProbeVolume* volume) { }
  188. /**
  189. * Called whenever a skybox is created.
  190. *
  191. * @note Core thread.
  192. */
  193. virtual void notifySkyboxAdded(Skybox* skybox) { }
  194. /**
  195. * Called whenever a skybox is destroyed.
  196. *
  197. * @note Core thread.
  198. */
  199. virtual void notifySkyboxRemoved(Skybox* skybox) { }
  200. /**
  201. * Captures the scene at the specified location into a cubemap.
  202. *
  203. * @param[in] cubemap Cubemap to store the results in.
  204. * @param[in] position Position to capture the scene at.
  205. * @param[in] settings Settings that allow you to customize the capture.
  206. *
  207. * @note Core thread.
  208. */
  209. virtual void captureSceneCubeMap(const SPtr<Texture>& cubemap, const Vector3& position,
  210. const CaptureSettings& settings) = 0;
  211. /**
  212. * Creates a new empty renderer mesh data.
  213. *
  214. * @note Sim thread.
  215. *
  216. * @see RendererMeshData
  217. */
  218. virtual SPtr<RendererMeshData> _createMeshData(UINT32 numVertices, UINT32 numIndices, VertexLayout layout,
  219. IndexType indexType = IT_32BIT);
  220. /**
  221. * Creates a new renderer mesh data using an existing generic mesh data buffer.
  222. *
  223. * @note Sim thread.
  224. *
  225. * @see RendererMeshData
  226. */
  227. virtual SPtr<RendererMeshData> _createMeshData(const SPtr<MeshData>& meshData);
  228. /**
  229. * Registers an extension object that will be called every frame by the renderer. Allows external code to perform
  230. * custom rendering interleaved with the renderer's output.
  231. *
  232. * @note Core thread.
  233. */
  234. void addPlugin(RendererExtension* plugin) { mCallbacks.insert(plugin); }
  235. /**
  236. * Unregisters an extension registered with addPlugin().
  237. *
  238. * @note Core thread.
  239. */
  240. void removePlugin(RendererExtension* plugin) { mCallbacks.erase(plugin); }
  241. /**
  242. * Registers a new task for execution on the core thread.
  243. *
  244. * @note Thread safe.
  245. */
  246. void addTask(const SPtr<RendererTask>& task);
  247. /** Sets options used for controlling the rendering. */
  248. virtual void setOptions(const SPtr<RendererOptions>& options) { }
  249. /** Returns current set of options used for controlling the rendering. */
  250. virtual SPtr<RendererOptions> getOptions() const { return SPtr<RendererOptions>(); }
  251. protected:
  252. friend class RendererTask;
  253. /** Contains information about a render callback. */
  254. struct RenderCallbackData
  255. {
  256. bool overlay;
  257. std::function<void()> callback;
  258. };
  259. /**
  260. * Executes all renderer tasks queued for this frame.
  261. *
  262. * @param[in] forceAll If true, multi-frame tasks will be forced to execute fully within this call.
  263. *
  264. * @note Core thread.
  265. */
  266. void processTasks(bool forceAll);
  267. /**
  268. * Executes the provided renderer task.
  269. *
  270. * @param[in] task Task to execute.
  271. * @param[in] forceAll If true, multi-frame tasks will be forced to execute fully within this call.
  272. *
  273. * @note Core thread.
  274. */
  275. void processTask(RendererTask& task, bool forceAll);
  276. /** Callback to trigger when comparing the order in which renderer extensions are called. */
  277. static bool compareCallback(const RendererExtension* a, const RendererExtension* b);
  278. Set<RendererExtension*, std::function<bool(const RendererExtension*, const RendererExtension*)>> mCallbacks;
  279. Vector<SPtr<RendererTask>> mQueuedTasks; // Sim & core thread
  280. Vector<SPtr<RendererTask>> mUnresolvedTasks; // Sim thread
  281. Vector<SPtr<RendererTask>> mRemainingUnresolvedTasks; // Sim thread
  282. Vector<SPtr<RendererTask>> mRunningTasks; // Core thread
  283. Vector<SPtr<RendererTask>> mRemainingTasks; // Core thread
  284. Mutex mTaskMutex;
  285. };
  286. /** Provides easy access to Renderer. */
  287. SPtr<Renderer> BS_CORE_EXPORT gRenderer();
  288. /**
  289. * Task that represents an asynchonous operation queued for execution on the core thread. All such tasks are executed
  290. * before main rendering happens, every frame.
  291. *
  292. * @note Thread safe except where stated otherwise.
  293. */
  294. class BS_CORE_EXPORT RendererTask
  295. {
  296. struct PrivatelyConstruct {};
  297. public:
  298. RendererTask(const PrivatelyConstruct& dummy, const String& name, std::function<bool()> taskWorker);
  299. /**
  300. * Creates a new task. Task should be provided to Renderer in order for it to start.
  301. *
  302. * @param[in] name Name you can use to more easily identify the task.
  303. * @param[in] taskWorker Worker method that does all of the work in the task. Tasks can run over the course of
  304. * multiple frames, in which case this method should return false (if there's more
  305. * work to be done), or true (if the task has completed).
  306. */
  307. static SPtr<RendererTask> create(const String& name, std::function<bool()> taskWorker);
  308. /** Returns true if the task has completed. */
  309. bool isComplete() const;
  310. /** Returns true if the task has been canceled. */
  311. bool isCanceled() const;
  312. /** Blocks the current thread until the task has completed. */
  313. void wait();
  314. /** Cancels the task and removes it from the Renderer's queue. */
  315. void cancel();
  316. /**
  317. * Callback triggered on the sim thread, when the task completes. Is not triggered if the task is cancelled.
  318. *
  319. * @note Sim thread only.
  320. */
  321. Event<void()> onComplete;
  322. private:
  323. friend class Renderer;
  324. String mName;
  325. std::function<bool()> mTaskWorker;
  326. std::atomic<UINT32> mState; /**< 0 - Inactive, 1 - In progress, 2 - Completed, 3 - Canceled */
  327. };
  328. /** @} */
  329. }}