BsRenderer.h 9.4 KB

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