BsRenderer.h 11 KB

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