BsRenderer.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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) { }
  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 the texture assigned to a skybox is changed.
  151. *
  152. * @note Core thread.
  153. */
  154. virtual void notifySkyboxTextureChanged(Skybox* skybox) { }
  155. /**
  156. * Called whenever a skybox is destroyed.
  157. *
  158. * @note Core thread.
  159. */
  160. virtual void notifySkyboxRemoved(Skybox* skybox) { }
  161. /**
  162. * Captures the scene at the specified location into a cubemap.
  163. *
  164. * @param[in] cubemap Cubemap to store the results in.
  165. * @param[in] position Position to capture the scene at.
  166. * @param[in] hdr If true scene will be captured in a format that supports high dynamic range.
  167. *
  168. * @note Core thread.
  169. */
  170. virtual void captureSceneCubeMap(const SPtr<Texture>& cubemap, const Vector3& position, bool hdr) = 0;
  171. /**
  172. * Creates a new empty renderer mesh data.
  173. *
  174. * @note Sim thread.
  175. *
  176. * @see RendererMeshData
  177. */
  178. virtual SPtr<RendererMeshData> _createMeshData(UINT32 numVertices, UINT32 numIndices, VertexLayout layout,
  179. IndexType indexType = IT_32BIT);
  180. /**
  181. * Creates a new renderer mesh data using an existing generic mesh data buffer.
  182. *
  183. * @note Sim thread.
  184. *
  185. * @see RendererMeshData
  186. */
  187. virtual SPtr<RendererMeshData> _createMeshData(const SPtr<MeshData>& meshData);
  188. /**
  189. * Registers an extension object that will be called every frame by the renderer. Allows external code to perform
  190. * custom rendering interleaved with the renderer's output.
  191. *
  192. * @note Core thread.
  193. */
  194. void addPlugin(RendererExtension* plugin) { mCallbacks.insert(plugin); }
  195. /**
  196. * Unregisters an extension registered with addPlugin().
  197. *
  198. * @note Core thread.
  199. */
  200. void removePlugin(RendererExtension* plugin) { mCallbacks.erase(plugin); }
  201. /**
  202. * Registers a new task for execution on the core thread.
  203. *
  204. * @note Thread safe.
  205. */
  206. void addTask(const SPtr<RendererTask>& task);
  207. /** Sets options used for controlling the rendering. */
  208. virtual void setOptions(const SPtr<RendererOptions>& options) { }
  209. /** Returns current set of options used for controlling the rendering. */
  210. virtual SPtr<RendererOptions> getOptions() const { return SPtr<RendererOptions>(); }
  211. protected:
  212. friend class RendererTask;
  213. /** Contains information about a render callback. */
  214. struct RenderCallbackData
  215. {
  216. bool overlay;
  217. std::function<void()> callback;
  218. };
  219. /**
  220. * Executes all renderer tasks queued for this frame.
  221. *
  222. * @param[in] forceAll If true, multi-frame tasks will be forced to execute fully within this call.
  223. *
  224. * @note Core thread.
  225. */
  226. void processTasks(bool forceAll);
  227. /**
  228. * Executes the provided renderer task.
  229. *
  230. * @param[in] task Task to execute.
  231. * @param[in] forceAll If true, multi-frame tasks will be forced to execute fully within this call.
  232. *
  233. * @note Core thread.
  234. */
  235. void processTask(RendererTask& task, bool forceAll);
  236. /** Callback to trigger when comparing the order in which renderer extensions are called. */
  237. static bool compareCallback(const RendererExtension* a, const RendererExtension* b);
  238. Set<RendererExtension*, std::function<bool(const RendererExtension*, const RendererExtension*)>> mCallbacks;
  239. Vector<SPtr<RendererTask>> mQueuedTasks; // Sim & core thread
  240. Vector<SPtr<RendererTask>> mUnresolvedTasks; // Sim thread
  241. Vector<SPtr<RendererTask>> mRemainingUnresolvedTasks; // Sim thread
  242. Vector<SPtr<RendererTask>> mRunningTasks; // Core thread
  243. Vector<SPtr<RendererTask>> mRemainingTasks; // Core thread
  244. Mutex mTaskMutex;
  245. };
  246. /** Provides easy access to Renderer. */
  247. SPtr<Renderer> BS_CORE_EXPORT gRenderer();
  248. /**
  249. * Task that represents an asynchonous operation queued for execution on the core thread. All such tasks are executed
  250. * before main rendering happens, every frame.
  251. *
  252. * @note Thread safe except where stated otherwise.
  253. */
  254. class BS_CORE_EXPORT RendererTask
  255. {
  256. struct PrivatelyConstruct {};
  257. public:
  258. RendererTask(const PrivatelyConstruct& dummy, const String& name, std::function<bool()> taskWorker);
  259. /**
  260. * Creates a new task. Task should be provided to Renderer in order for it to start.
  261. *
  262. * @param[in] name Name you can use to more easily identify the task.
  263. * @param[in] taskWorker Worker method that does all of the work in the task. Tasks can run over the course of
  264. * multiple frames, in which case this method should return false (if there's more
  265. * work to be done), or true (if the task has completed).
  266. */
  267. static SPtr<RendererTask> create(const String& name, std::function<bool()> taskWorker);
  268. /** Returns true if the task has completed. */
  269. bool isComplete() const;
  270. /** Returns true if the task has been canceled. */
  271. bool isCanceled() const;
  272. /** Blocks the current thread until the task has completed. */
  273. void wait();
  274. /** Cancels the task and removes it from the Renderer's queue. */
  275. void cancel();
  276. /**
  277. * Callback triggered on the sim thread, when the task completes. Is not triggered if the task is cancelled.
  278. *
  279. * @note Sim thread only.
  280. */
  281. Event<void()> onComplete;
  282. private:
  283. friend class Renderer;
  284. String mName;
  285. std::function<bool()> mTaskWorker;
  286. std::atomic<UINT32> mState; /**< 0 - Inactive, 1 - In progress, 2 - Completed, 3 - Canceled */
  287. };
  288. /** @} */
  289. }}