BsRenderAPI.h 28 KB

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