BsRenderAPI.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 "RenderAPI/BsSamplerState.h"
  6. #include "CoreThread/BsCommandQueue.h"
  7. #include "RenderAPI/BsRenderAPICapabilities.h"
  8. #include "RenderAPI/BsRenderTarget.h"
  9. #include "RenderAPI/BsRenderTexture.h"
  10. #include "RenderAPI/BsRenderWindow.h"
  11. #include "RenderAPI/BsGpuProgram.h"
  12. #include "RenderAPI/BsVertexDeclaration.h"
  13. #include "Math/BsPlane.h"
  14. #include "Utility/BsModule.h"
  15. #include "Utility/BsEvent.h"
  16. namespace bs
  17. {
  18. class RenderAPIInfo;
  19. class RenderAPIManager;
  20. /** @addtogroup RenderAPI
  21. * @{
  22. */
  23. /**
  24. * Provides access to ct::RenderAPI from the simulation thread. All the commands get queued on the core thread queue
  25. * for the calling thread.
  26. *
  27. * @see ct::RenderAPI
  28. *
  29. * @note Sim thread only.
  30. */
  31. class BS_CORE_EXPORT RenderAPI
  32. {
  33. public:
  34. /**
  35. * @see ct::RenderAPI::setGpuParams()
  36. *
  37. * @note This is an @ref asyncMethod "asynchronous method".
  38. */
  39. static void setGpuParams(const SPtr<GpuParams>& gpuParams);
  40. /**
  41. * @see ct::RenderAPI::setGraphicsPipeline()
  42. *
  43. * @note This is an @ref asyncMethod "asynchronous method".
  44. */
  45. static void setGraphicsPipeline(const SPtr<GraphicsPipelineState>& pipelineState);
  46. /**
  47. * @see ct::RenderAPI::setComputePipeline()
  48. *
  49. * @note This is an @ref asyncMethod "asynchronous method".
  50. */
  51. static void setComputePipeline(const SPtr<ComputePipelineState>& pipelineState);
  52. /**
  53. * @see ct::RenderAPI::setVertexBuffers()
  54. *
  55. * @note This is an @ref asyncMethod "asynchronous method".
  56. */
  57. static void setVertexBuffers(UINT32 index, const Vector<SPtr<VertexBuffer>>& buffers);
  58. /**
  59. * @see ct::RenderAPI::setIndexBuffer()
  60. *
  61. * @note This is an @ref asyncMethod "asynchronous method".
  62. */
  63. static void setIndexBuffer(const SPtr<IndexBuffer>& buffer);
  64. /**
  65. * @see ct::RenderAPI::setVertexDeclaration()
  66. *
  67. * @note This is an @ref asyncMethod "asynchronous method".
  68. */
  69. static void setVertexDeclaration(const SPtr<VertexDeclaration>& vertexDeclaration);
  70. /**
  71. * @see ct::RenderAPI::setViewport()
  72. *
  73. * @note This is an @ref asyncMethod "asynchronous method".
  74. */
  75. static void setViewport(const Rect2& area);
  76. /**
  77. * @see ct::RenderAPI::setViewport()
  78. *
  79. * @note This is an @ref asyncMethod "asynchronous method".
  80. */
  81. static void setStencilRef(UINT32 value);
  82. /**
  83. * @see ct::RenderAPI::setDrawOperation()
  84. *
  85. * @note This is an @ref asyncMethod "asynchronous method".
  86. */
  87. static void setDrawOperation(DrawOperationType op);
  88. /**
  89. * @see ct::RenderAPI::setScissorRect()
  90. *
  91. * @note This is an @ref asyncMethod "asynchronous method".
  92. */
  93. static void setScissorRect(UINT32 left = 0, UINT32 top = 0, UINT32 right = 800, UINT32 bottom = 600);
  94. /**
  95. * @see ct::RenderAPI::setRenderTarget()
  96. *
  97. * @note This is an @ref asyncMethod "asynchronous method".
  98. */
  99. static void setRenderTarget(const SPtr<RenderTarget>& target, UINT32 readOnlyFlags = 0,
  100. RenderSurfaceMask loadMask = RT_NONE);
  101. /**
  102. * @see ct::RenderAPI::clearRenderTarget()
  103. *
  104. * @note This is an @ref asyncMethod "asynchronous method".
  105. */
  106. static void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f,
  107. UINT16 stencil = 0, UINT8 targetMask = 0xFF);
  108. /**
  109. * @see ct::RenderAPI::clearViewport()
  110. *
  111. * @note This is an @ref asyncMethod "asynchronous method".
  112. */
  113. static void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0,
  114. UINT8 targetMask = 0xFF);
  115. /**
  116. * @see ct::RenderAPI::swapBuffers()
  117. *
  118. * @note This is an @ref asyncMethod "asynchronous method".
  119. */
  120. static void swapBuffers(const SPtr<RenderTarget>& target);
  121. /**
  122. * @see ct::RenderAPI::draw()
  123. *
  124. * @note This is an @ref asyncMethod "asynchronous method".
  125. */
  126. static void draw(UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount = 0);
  127. /**
  128. * @see ct::RenderAPI::drawIndexed()
  129. *
  130. * @note This is an @ref asyncMethod "asynchronous method".
  131. */
  132. static void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount,
  133. UINT32 instanceCount = 0);
  134. /**
  135. * @see ct::RenderAPI::dispatchCompute()
  136. *
  137. * @note This is an @ref asyncMethod "asynchronous method".
  138. */
  139. static void dispatchCompute(UINT32 numGroupsX, UINT32 numGroupsY = 1, UINT32 numGroupsZ = 1);
  140. /** @copydoc ct::RenderAPI::getVideoModeInfo */
  141. static const VideoModeInfo& getVideoModeInfo();
  142. /** @copydoc ct::RenderAPI::convertProjectionMatrix */
  143. static void convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest);
  144. /** @copydoc ct::RenderAPI::getAPIInfo */
  145. static const RenderAPIInfo& getAPIInfo();
  146. };
  147. /** Feature flags that describe which render API specific features are enabled. */
  148. enum class RenderAPIFeatureFlag
  149. {
  150. /** If set, vertex color order will be reversed before being sent to the shader. */
  151. VertexColorFlip = 1 << 0,
  152. /**
  153. * If set, the Y axis in texture (UV) coordinates is assumed to be pointing up, instead of down (which is the
  154. * default).
  155. */
  156. UVYAxisUp = 1 << 1,
  157. /**
  158. * If set, the Y axis in normalized device coordinates (NDC) is assumed to be pointing down, instead of up (which
  159. * is the default).
  160. */
  161. NDCYAxisDown = 1 << 2,
  162. /**
  163. * If set, the matrices used by shaders are in column major order, instead of in row major (which is the default).
  164. */
  165. ColumnMajorMatrices = 1 << 3,
  166. /**
  167. * If set, the render API has native support for multi-threaded command buffer generation. Otherwise it is
  168. * emulated and using command buffers might not be beneficial.
  169. */
  170. MultiThreadedCB = 1 << 4,
  171. /** If set, the render API supports unordered stores to a texture with more than one sample. */
  172. MSAAImageStores = 1 << 5,
  173. /**
  174. * If set, the render API supports binds of parts of a texture (e.g. a single mip level, or a single element of a
  175. * texture array). This only applies to texture reads and individual mip levels or layers can still be bound as
  176. * render texture targets regardless of this flag.
  177. */
  178. TextureViews = 1 << 6,
  179. /** If set, the render API supports compute shaders. */
  180. Compute = 1 << 7,
  181. /** If set, the render API supports load-store textures or buffers (AKA unordered access (UAV). */
  182. LoadStore = 1 << 8
  183. };
  184. typedef Flags<RenderAPIFeatureFlag> RenderAPIFeatures;
  185. BS_FLAGS_OPERATORS(RenderAPIFeatureFlag)
  186. /** Contains properties specific to a render API implementation. */
  187. class RenderAPIInfo
  188. {
  189. public:
  190. RenderAPIInfo(float horzTexelOffset, float vertTexelOffset, float minDepth, float maxDepth,
  191. VertexElementType vertexColorType, RenderAPIFeatures featureFlags)
  192. : mHorizontalTexelOffset(horzTexelOffset), mVerticalTexelOffset(vertTexelOffset), mMinDepth(minDepth)
  193. , mMaxDepth(maxDepth), mVertexColorType(vertexColorType), mFeatureFlags(featureFlags)
  194. { }
  195. /** Gets the native type used for vertex colors. */
  196. VertexElementType getColorVertexElementType() const { return mVertexColorType; }
  197. /** Gets horizontal texel offset used for mapping texels to pixels in this render system. */
  198. float getHorizontalTexelOffset() const { return mHorizontalTexelOffset; }
  199. /** Gets vertical texel offset used for mapping texels to pixels in this render system. */
  200. float getVerticalTexelOffset() const { return mVerticalTexelOffset; }
  201. /** Gets the minimum (closest) depth value used by this render system. */
  202. float getMinimumDepthInputValue() const { return mMinDepth; }
  203. /** Gets the maximum (farthest) depth value used by this render system. */
  204. float getMaximumDepthInputValue() const { return mMaxDepth; }
  205. /** Checks is a specific feature flag enabled. */
  206. bool isFlagSet(RenderAPIFeatureFlag flag) const { return mFeatureFlags.isSet(flag); }
  207. private:
  208. float mHorizontalTexelOffset = 0.0f;
  209. float mVerticalTexelOffset = 0.0f;
  210. float mMinDepth = 0.0f;
  211. float mMaxDepth = 1.0f;
  212. VertexElementType mVertexColorType = VET_COLOR_ABGR;
  213. RenderAPIFeatures mFeatureFlags;
  214. };
  215. /** @} */
  216. namespace ct
  217. {
  218. /** @addtogroup RenderAPI-Internal
  219. * @{
  220. */
  221. /**
  222. * Provides low-level API access to rendering commands (internally wrapping DirectX/OpenGL/Vulkan or similar).
  223. *
  224. * Methods that accept a CommandBuffer parameter get queued in the provided command buffer, and don't get executed until
  225. * executeCommands() method is called. User is allowed to populate command buffers from non-core threads, but they all
  226. * must get executed from the core thread.
  227. *
  228. * If a command buffer is not provivided to such methods, they execute immediately. Without a command buffer the methods
  229. * are only allowed to be called from the core thread.
  230. *
  231. * @note Accessible on any thread for methods accepting a CommandBuffer. Otherwise core thread unless specifically
  232. * noted otherwise on per-method basis.
  233. */
  234. class BS_CORE_EXPORT RenderAPI : public Module<RenderAPI>
  235. {
  236. public:
  237. RenderAPI();
  238. virtual ~RenderAPI();
  239. /**
  240. * Returns the name of the rendering system.
  241. *
  242. * @note Thread safe.
  243. */
  244. virtual const StringID& getName() const = 0;
  245. /**
  246. * Applies a set of parameters that control execution of all currently bound GPU programs. These are the uniforms
  247. * like textures, samplers, or uniform buffers. Caller is expected to ensure the provided parameters actually
  248. * match the currently bound programs.
  249. */
  250. virtual void setGpuParams(const SPtr<GpuParams>& gpuParams,
  251. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  252. /**
  253. * Sets a pipeline state that controls how will subsequent draw commands render primitives.
  254. *
  255. * @param[in] pipelineState Pipeline state to bind, or null to unbind.
  256. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  257. * is executed immediately. Otherwise it is executed when executeCommands() is
  258. * called. Buffer must support graphics operations.
  259. *
  260. * @see GraphicsPipelineState
  261. */
  262. virtual void setGraphicsPipeline(const SPtr<GraphicsPipelineState>& pipelineState,
  263. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  264. /**
  265. * Sets a pipeline state that controls how will subsequent dispatch commands execute.
  266. *
  267. * @param[in] pipelineState Pipeline state to bind, or null to unbind.
  268. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  269. * is executed immediately. Otherwise it is executed when executeCommands() is
  270. * called. Buffer must support graphics operations.
  271. */
  272. virtual void setComputePipeline(const SPtr<ComputePipelineState>& pipelineState,
  273. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  274. /**
  275. * Sets the active viewport that will be used for all render operations.
  276. *
  277. * @param[in] area Area of the viewport, in normalized ([0,1] range) coordinates.
  278. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  279. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  280. * Buffer must support graphics operations.
  281. */
  282. virtual void setViewport(const Rect2& area, const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  283. /**
  284. * Allows you to set up a region in which rendering can take place. Coordinates are in pixels. No rendering will be
  285. * done to render target pixels outside of the provided region.
  286. *
  287. * @param[in] left Left border of the scissor rectangle, in pixels.
  288. * @param[in] top Top border of the scissor rectangle, in pixels.
  289. * @param[in] right Right border of the scissor rectangle, in pixels.
  290. * @param[in] bottom Bottom border of the scissor rectangle, in pixels.
  291. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  292. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  293. * Buffer must support graphics operations.
  294. */
  295. virtual void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom,
  296. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  297. /**
  298. * Sets a reference value that will be used for stencil compare operations.
  299. *
  300. * @param[in] value Reference value to set.
  301. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  302. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  303. * Buffer must support graphics operations.
  304. */
  305. virtual void setStencilRef(UINT32 value, const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  306. /**
  307. * Sets the provided vertex buffers starting at the specified source index. Set buffer to nullptr to clear the
  308. * buffer at the specified index.
  309. *
  310. * @param[in] index Index at which to start binding the vertex buffers.
  311. * @param[in] buffers A list of buffers to bind to the pipeline.
  312. * @param[in] numBuffers Number of buffers in the @p buffers list.
  313. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  314. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  315. * Buffer must support graphics operations.
  316. */
  317. virtual void setVertexBuffers(UINT32 index, SPtr<VertexBuffer>* buffers, UINT32 numBuffers,
  318. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  319. /**
  320. * Sets an index buffer to use when drawing. Indices in an index buffer reference vertices in the vertex buffer,
  321. * which increases cache coherency and reduces the size of vertex buffers by eliminating duplicate data.
  322. *
  323. * @param[in] buffer Index buffer to bind, null to unbind.
  324. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  325. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  326. * Buffer must support graphics operations.
  327. */
  328. virtual void setIndexBuffer(const SPtr<IndexBuffer>& buffer,
  329. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  330. /**
  331. * Sets the vertex declaration to use when drawing. Vertex declaration is used to decode contents of a single
  332. * vertex in a vertex buffer.
  333. *
  334. * @param[in] vertexDeclaration Vertex declaration to bind.
  335. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  336. * is executed immediately. Otherwise it is executed when executeCommands() is
  337. * called. Buffer must support graphics operations.
  338. */
  339. virtual void setVertexDeclaration(const SPtr<VertexDeclaration>& vertexDeclaration,
  340. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  341. /**
  342. * Sets the draw operation that determines how to interpret the elements of the index or vertex buffers.
  343. *
  344. * @param[in] op Draw operation to enable.
  345. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  346. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  347. * Buffer must support graphics operations.
  348. */
  349. virtual void setDrawOperation(DrawOperationType op,
  350. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  351. /**
  352. * Draw an object based on currently bound GPU programs, vertex declaration and vertex buffers. Draws directly from
  353. * the vertex buffer without using indices.
  354. *
  355. * @param[in] vertexOffset Offset into the currently bound vertex buffer to start drawing from.
  356. * @param[in] vertexCount Number of vertices to draw.
  357. * @param[in] instanceCount Number of times to draw the provided geometry, each time with an (optionally)
  358. * separate per-instance data.
  359. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  360. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  361. * Buffer must support graphics operations.
  362. */
  363. virtual void draw(UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount = 0,
  364. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  365. /**
  366. * Draw an object based on currently bound GPU programs, vertex declaration, vertex and index buffers.
  367. *
  368. * @param[in] startIndex Offset into the currently bound index buffer to start drawing from.
  369. * @param[in] indexCount Number of indices to draw.
  370. * @param[in] vertexOffset Offset to apply to each vertex index.
  371. * @param[in] vertexCount Number of vertices to draw.
  372. * @param[in] instanceCount Number of times to draw the provided geometry, each time with an (optionally)
  373. * separate per-instance data.
  374. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  375. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  376. * Buffer must support graphics operations.
  377. */
  378. virtual void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount,
  379. UINT32 instanceCount = 0, const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  380. /**
  381. * Executes the currently bound compute shader.
  382. *
  383. * @param[in] numGroupsX Number of groups to start in the X direction. Must be in range [1, 65535].
  384. * @param[in] numGroupsY Number of groups to start in the Y direction. Must be in range [1, 65535].
  385. * @param[in] numGroupsZ Number of groups to start in the Z direction. Must be in range [1, 64].
  386. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  387. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  388. * Buffer must support compute or graphics operations.
  389. */
  390. virtual void dispatchCompute(UINT32 numGroupsX, UINT32 numGroupsY = 1, UINT32 numGroupsZ = 1,
  391. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  392. /**
  393. * Swap the front and back buffer of the specified render target.
  394. *
  395. * @param[in] target Render target to perform the buffer swap on.
  396. * @param[in] syncMask Optional synchronization mask that determines for which queues should the system wait
  397. * before performing the swap buffer operation. By default the system waits for all queues.
  398. * However if certain queues are performing non-rendering operations, or operations not
  399. * related to the provided render target, you can exclude them from the sync mask for
  400. * potentially better performance. You can use CommandSyncMask to generate a valid sync mask.
  401. */
  402. virtual void swapBuffers(const SPtr<RenderTarget>& target, UINT32 syncMask = 0xFFFFFFFF) = 0;
  403. /**
  404. * Change the render target into which we want to draw.
  405. *
  406. * @param[in] target Render target to draw to.
  407. * @param[in] readOnlyFlags Combination of one or more elements of FrameBufferType denoting which buffers
  408. * will be bound for read-only operations. This is useful for depth or stencil
  409. * buffers which need to be bound both for depth/stencil tests, as well as
  410. * shader reads.
  411. * @param[in] loadMask Determines which render target surfaces will have their current contents
  412. * preserved. By default when a render target is bound its contents will be
  413. * lost. You might need to preserve contents if you need to perform blending
  414. * or similar operations with the existing contents of the render target.
  415. *
  416. * Use the mask to select exactly which surfaces of the render target need
  417. * their contents preserved.
  418. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  419. * is executed immediately. Otherwise it is executed when executeCommands() is
  420. * called. Buffer must support graphics operations.
  421. */
  422. virtual void setRenderTarget(const SPtr<RenderTarget>& target, UINT32 readOnlyFlags = 0,
  423. RenderSurfaceMask loadMask = RT_NONE, const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  424. /**
  425. * Clears the currently active render target.
  426. *
  427. * @param[in] buffers Combination of one or more elements of FrameBufferType denoting which buffers are
  428. * to be cleared.
  429. * @param[in] color The color to clear the color buffer with, if enabled.
  430. * @param[in] depth The value to initialize the depth buffer with, if enabled.
  431. * @param[in] stencil The value to initialize the stencil buffer with, if enabled.
  432. * @param[in] targetMask In case multiple render targets are bound, this allows you to control which ones to
  433. * clear (0x01 first, 0x02 second, 0x04 third, etc., and combinations).
  434. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  435. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  436. * Buffer must support graphics operations.
  437. */
  438. virtual void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f,
  439. UINT16 stencil = 0, UINT8 targetMask = 0xFF, const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  440. /**
  441. * Clears the currently active viewport (meaning it clears just a sub-area of a render-target that is covered by the
  442. * viewport, as opposed to clearRenderTarget() which always clears the entire render target).
  443. *
  444. * @param[in] buffers Combination of one or more elements of FrameBufferType denoting which buffers are to
  445. * be cleared.
  446. * @param[in] color The color to clear the color buffer with, if enabled.
  447. * @param[in] depth The value to initialize the depth buffer with, if enabled.
  448. * @param[in] stencil The value to initialize the stencil buffer with, if enabled.
  449. * @param[in] targetMask In case multiple render targets are bound, this allows you to control which ones to
  450. * clear (0x01 first, 0x02 second, 0x04 third, etc., and combinations).
  451. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  452. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  453. * Buffer must support graphics operations.
  454. */
  455. virtual void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f,
  456. UINT16 stencil = 0, UINT8 targetMask = 0xFF, const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  457. /** Appends all commands from the provided secondary command buffer into the primary command buffer. */
  458. virtual void addCommands(const SPtr<CommandBuffer>& commandBuffer, const SPtr<CommandBuffer>& secondary) = 0;
  459. /**
  460. * Executes all commands in the provided command buffer. Command buffer cannot be secondary.
  461. *
  462. * @param[in] commandBuffer Command buffer whose commands to execute. Set to null to submit the main command
  463. * buffer.
  464. * @param[in] syncMask Optional synchronization mask that determines if the submitted command buffer
  465. * depends on any other command buffers. Use the CommandSyncMask class to generate
  466. * a mask using existing command buffers.
  467. *
  468. * This mask is only relevant if your command buffers are executing on different
  469. * hardware queues, and are somehow dependant. If they are executing on the same queue
  470. * (default) then they will execute sequentially in the order they are submitted.
  471. * Otherwise, if there is a dependency, you must make state it explicitly here.
  472. *
  473. * @note Core thread only.
  474. */
  475. virtual void submitCommandBuffer(const SPtr<CommandBuffer>& commandBuffer, UINT32 syncMask = 0xFFFFFFFF) = 0;
  476. /**
  477. * Gets the capabilities of a specific GPU.
  478. *
  479. * @param[in] deviceIdx Index of the device to get the capabilities for.
  480. *
  481. * @note Thread safe.
  482. */
  483. const RenderAPICapabilities& getCapabilities(UINT32 deviceIdx) const;
  484. /** Returns the number of devices supported by this render API. */
  485. UINT32 getNumDevices() const { return mNumDevices; }
  486. /**
  487. * Returns information about available output devices and their video modes.
  488. *
  489. * @note Thread safe.
  490. */
  491. const VideoModeInfo& getVideoModeInfo() const { return *mVideoModeInfo; }
  492. /************************************************************************/
  493. /* UTILITY METHODS */
  494. /************************************************************************/
  495. /**
  496. * Contains a default matrix into a matrix suitable for use by this specific render system.
  497. *
  498. * @note Thread safe.
  499. */
  500. virtual void convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest) = 0;
  501. /**
  502. * Returns information about the specific API implementation.
  503. *
  504. * @note Thread safe.
  505. */
  506. virtual const RenderAPIInfo& getAPIInfo() const = 0;
  507. /**
  508. * Generates a parameter block description and calculates per-parameter offsets for the provided gpu data
  509. * parameters. The offsets are render API specific and correspond to std140 layout for OpenGL, and the default
  510. * layout in DirectX.
  511. *
  512. * @param[in] name Name to assign the parameter block.
  513. * @param[in] params List of parameters in the parameter block. Only name, type and array size fields need to be
  514. * populated, the rest will be populated when the method returns. If a parameter is a struct
  515. * then the elementSize field needs to be populated with the size of the struct in bytes.
  516. * @return Descriptor for the parameter block holding the provided parameters as laid out by the
  517. * default render API layout.
  518. */
  519. virtual GpuParamBlockDesc generateParamBlockDesc(const String& name, Vector<GpuParamDataDesc>& params) = 0;
  520. /************************************************************************/
  521. /* INTERNAL METHODS */
  522. /************************************************************************/
  523. protected:
  524. /**
  525. * Initializes the render API system and creates a primary render window.
  526. *
  527. * @note Sim thread only.
  528. */
  529. SPtr<bs::RenderWindow> initialize(const RENDER_WINDOW_DESC& primaryWindowDesc);
  530. /** Initializes the render API system. Called before the primary render window is created. */
  531. virtual void initialize();
  532. /**
  533. * Performs (optional) secondary initialization of the render API system. Called after the render window is
  534. * created.
  535. */
  536. virtual void initializeWithWindow(const SPtr<RenderWindow>& primaryWindow);
  537. /**
  538. * Shuts down the render API system and cleans up all resources.
  539. *
  540. * @note Sim thread.
  541. */
  542. void destroy();
  543. /** Performs render API system shutdown on the core thread. */
  544. virtual void destroyCore();
  545. /** Converts the number of vertices to number of primitives based on the specified draw operation. */
  546. UINT32 vertexCountToPrimCount(DrawOperationType type, UINT32 elementCount);
  547. /************************************************************************/
  548. /* INTERNAL DATA */
  549. /************************************************************************/
  550. protected:
  551. friend class bs::RenderAPIManager;
  552. SPtr<RenderTarget> mActiveRenderTarget;
  553. RenderAPICapabilities* mCurrentCapabilities;
  554. UINT32 mNumDevices;
  555. SPtr<VideoModeInfo> mVideoModeInfo;
  556. };
  557. /** @} */
  558. }
  559. }