BsRenderer.h 10 KB

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