BsRenderer.h 11 KB

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