BsRenderer.h 11 KB

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