BsRenderAPI.h 27 KB

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