BsRenderer.h 9.4 KB

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