BsRenderAPI.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. * Gets the name of the primary shading language used by the rendering system.
  247. *
  248. * @note Thread safe.
  249. */
  250. virtual const String& getShadingLanguageName() const = 0;
  251. /**
  252. * Applies a set of parameters that control execution of all currently bound GPU programs. These are the uniforms
  253. * like textures, samplers, or uniform buffers. Caller is expected to ensure the provided parameters actually
  254. * match the currently bound programs.
  255. */
  256. virtual void setGpuParams(const SPtr<GpuParams>& gpuParams,
  257. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  258. /**
  259. * Sets a pipeline state that controls how will subsequent draw commands render primitives.
  260. *
  261. * @param[in] pipelineState Pipeline state to bind, or null to unbind.
  262. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  263. * is executed immediately. Otherwise it is executed when executeCommands() is
  264. * called. Buffer must support graphics operations.
  265. *
  266. * @see GraphicsPipelineState
  267. */
  268. virtual void setGraphicsPipeline(const SPtr<GraphicsPipelineState>& pipelineState,
  269. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  270. /**
  271. * Sets a pipeline state that controls how will subsequent dispatch commands execute.
  272. *
  273. * @param[in] pipelineState Pipeline state to bind, or null to unbind.
  274. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  275. * is executed immediately. Otherwise it is executed when executeCommands() is
  276. * called. Buffer must support graphics operations.
  277. */
  278. virtual void setComputePipeline(const SPtr<ComputePipelineState>& pipelineState,
  279. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  280. /**
  281. * Sets the active viewport that will be used for all render operations.
  282. *
  283. * @param[in] area Area of the viewport, in normalized ([0,1] range) coordinates.
  284. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  285. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  286. * Buffer must support graphics operations.
  287. */
  288. virtual void setViewport(const Rect2& area, const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  289. /**
  290. * Allows you to set up a region in which rendering can take place. Coordinates are in pixels. No rendering will be
  291. * done to render target pixels outside of the provided region.
  292. *
  293. * @param[in] left Left border of the scissor rectangle, in pixels.
  294. * @param[in] top Top border of the scissor rectangle, in pixels.
  295. * @param[in] right Right border of the scissor rectangle, in pixels.
  296. * @param[in] bottom Bottom border of the scissor rectangle, in pixels.
  297. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  298. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  299. * Buffer must support graphics operations.
  300. */
  301. virtual void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom,
  302. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  303. /**
  304. * Sets a reference value that will be used for stencil compare operations.
  305. *
  306. * @param[in] value Reference value to set.
  307. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  308. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  309. * Buffer must support graphics operations.
  310. */
  311. virtual void setStencilRef(UINT32 value, const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  312. /**
  313. * Sets the provided vertex buffers starting at the specified source index. Set buffer to nullptr to clear the
  314. * buffer at the specified index.
  315. *
  316. * @param[in] index Index at which to start binding the vertex buffers.
  317. * @param[in] buffers A list of buffers to bind to the pipeline.
  318. * @param[in] numBuffers Number of buffers in the @p buffers list.
  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 setVertexBuffers(UINT32 index, SPtr<VertexBuffer>* buffers, UINT32 numBuffers,
  324. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  325. /**
  326. * Sets an index buffer to use when drawing. Indices in an index buffer reference vertices in the vertex buffer,
  327. * which increases cache coherency and reduces the size of vertex buffers by eliminating duplicate data.
  328. *
  329. * @param[in] buffer Index buffer to bind, null to unbind.
  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 called.
  332. * Buffer must support graphics operations.
  333. */
  334. virtual void setIndexBuffer(const SPtr<IndexBuffer>& buffer,
  335. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  336. /**
  337. * Sets the vertex declaration to use when drawing. Vertex declaration is used to decode contents of a single
  338. * vertex in a vertex buffer.
  339. *
  340. * @param[in] vertexDeclaration Vertex declaration to bind.
  341. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  342. * is executed immediately. Otherwise it is executed when executeCommands() is
  343. * called. Buffer must support graphics operations.
  344. */
  345. virtual void setVertexDeclaration(const SPtr<VertexDeclaration>& vertexDeclaration,
  346. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  347. /**
  348. * Sets the draw operation that determines how to interpret the elements of the index or vertex buffers.
  349. *
  350. * @param[in] op Draw operation to enable.
  351. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  352. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  353. * Buffer must support graphics operations.
  354. */
  355. virtual void setDrawOperation(DrawOperationType op,
  356. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  357. /**
  358. * Draw an object based on currently bound GPU programs, vertex declaration and vertex buffers. Draws directly from
  359. * the vertex buffer without using indices.
  360. *
  361. * @param[in] vertexOffset Offset into the currently bound vertex buffer to start drawing from.
  362. * @param[in] vertexCount Number of vertices to draw.
  363. * @param[in] instanceCount Number of times to draw the provided geometry, each time with an (optionally)
  364. * separate per-instance data.
  365. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  366. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  367. * Buffer must support graphics operations.
  368. */
  369. virtual void draw(UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount = 0,
  370. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  371. /**
  372. * Draw an object based on currently bound GPU programs, vertex declaration, vertex and index buffers.
  373. *
  374. * @param[in] startIndex Offset into the currently bound index buffer to start drawing from.
  375. * @param[in] indexCount Number of indices to draw.
  376. * @param[in] vertexOffset Offset to apply to each vertex index.
  377. * @param[in] vertexCount Number of vertices to draw.
  378. * @param[in] instanceCount Number of times to draw the provided geometry, each time with an (optionally)
  379. * separate per-instance data.
  380. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  381. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  382. * Buffer must support graphics operations.
  383. */
  384. virtual void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount,
  385. UINT32 instanceCount = 0, const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  386. /**
  387. * Executes the currently bound compute shader.
  388. *
  389. * @param[in] numGroupsX Number of groups to start in the X direction. Must be in range [1, 65535].
  390. * @param[in] numGroupsY Number of groups to start in the Y direction. Must be in range [1, 65535].
  391. * @param[in] numGroupsZ Number of groups to start in the Z direction. Must be in range [1, 64].
  392. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  393. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  394. * Buffer must support compute or graphics operations.
  395. */
  396. virtual void dispatchCompute(UINT32 numGroupsX, UINT32 numGroupsY = 1, UINT32 numGroupsZ = 1,
  397. const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  398. /**
  399. * Swap the front and back buffer of the specified render target.
  400. *
  401. * @param[in] target Render target to perform the buffer swap on.
  402. * @param[in] syncMask Optional synchronization mask that determines for which queues should the system wait
  403. * before performing the swap buffer operation. By default the system waits for all queues.
  404. * However if certain queues are performing non-rendering operations, or operations not
  405. * related to the provided render target, you can exclude them from the sync mask for
  406. * potentially better performance. You can use CommandSyncMask to generate a valid sync mask.
  407. */
  408. virtual void swapBuffers(const SPtr<RenderTarget>& target, UINT32 syncMask = 0xFFFFFFFF) = 0;
  409. /**
  410. * Change the render target into which we want to draw.
  411. *
  412. * @param[in] target Render target to draw to.
  413. * @param[in] readOnlyFlags Combination of one or more elements of FrameBufferType denoting which buffers
  414. * will be bound for read-only operations. This is useful for depth or stencil
  415. * buffers which need to be bound both for depth/stencil tests, as well as
  416. * shader reads.
  417. * @param[in] loadMask Determines which render target surfaces will have their current contents
  418. * preserved. By default when a render target is bound its contents will be
  419. * lost. You might need to preserve contents if you need to perform blending
  420. * or similar operations with the existing contents of the render target.
  421. *
  422. * Use the mask to select exactly which surfaces of the render target need
  423. * their contents preserved.
  424. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  425. * is executed immediately. Otherwise it is executed when executeCommands() is
  426. * called. Buffer must support graphics operations.
  427. */
  428. virtual void setRenderTarget(const SPtr<RenderTarget>& target, UINT32 readOnlyFlags = 0,
  429. RenderSurfaceMask loadMask = RT_NONE, const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  430. /**
  431. * Clears the currently active render target.
  432. *
  433. * @param[in] buffers Combination of one or more elements of FrameBufferType denoting which buffers are
  434. * to be cleared.
  435. * @param[in] color The color to clear the color buffer with, if enabled.
  436. * @param[in] depth The value to initialize the depth buffer with, if enabled.
  437. * @param[in] stencil The value to initialize the stencil buffer with, if enabled.
  438. * @param[in] targetMask In case multiple render targets are bound, this allows you to control which ones to
  439. * clear (0x01 first, 0x02 second, 0x04 third, etc., and combinations).
  440. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  441. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  442. * Buffer must support graphics operations.
  443. */
  444. virtual void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f,
  445. UINT16 stencil = 0, UINT8 targetMask = 0xFF, const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  446. /**
  447. * Clears the currently active viewport (meaning it clears just a sub-area of a render-target that is covered by the
  448. * viewport, as opposed to clearRenderTarget() which always clears the entire render target).
  449. *
  450. * @param[in] buffers Combination of one or more elements of FrameBufferType denoting which buffers are to
  451. * be cleared.
  452. * @param[in] color The color to clear the color buffer with, if enabled.
  453. * @param[in] depth The value to initialize the depth buffer with, if enabled.
  454. * @param[in] stencil The value to initialize the stencil buffer with, if enabled.
  455. * @param[in] targetMask In case multiple render targets are bound, this allows you to control which ones to
  456. * clear (0x01 first, 0x02 second, 0x04 third, etc., and combinations).
  457. * @param[in] commandBuffer Optional command buffer to queue the operation on. If not provided operation
  458. * is executed immediately. Otherwise it is executed when executeCommands() is called.
  459. * Buffer must support graphics operations.
  460. */
  461. virtual void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f,
  462. UINT16 stencil = 0, UINT8 targetMask = 0xFF, const SPtr<CommandBuffer>& commandBuffer = nullptr) = 0;
  463. /** Appends all commands from the provided secondary command buffer into the primary command buffer. */
  464. virtual void addCommands(const SPtr<CommandBuffer>& commandBuffer, const SPtr<CommandBuffer>& secondary) = 0;
  465. /**
  466. * Executes all commands in the provided command buffer. Command buffer cannot be secondary.
  467. *
  468. * @param[in] commandBuffer Command buffer whose commands to execute. Set to null to submit the main command
  469. * buffer.
  470. * @param[in] syncMask Optional synchronization mask that determines if the submitted command buffer
  471. * depends on any other command buffers. Use the CommandSyncMask class to generate
  472. * a mask using existing command buffers.
  473. *
  474. * This mask is only relevant if your command buffers are executing on different
  475. * hardware queues, and are somehow dependant. If they are executing on the same queue
  476. * (default) then they will execute sequentially in the order they are submitted.
  477. * Otherwise, if there is a dependency, you must make state it explicitly here.
  478. *
  479. * @note Core thread only.
  480. */
  481. virtual void submitCommandBuffer(const SPtr<CommandBuffer>& commandBuffer, UINT32 syncMask = 0xFFFFFFFF) = 0;
  482. /**
  483. * Gets the capabilities of a specific GPU.
  484. *
  485. * @param[in] deviceIdx Index of the device to get the capabilities for.
  486. *
  487. * @note Thread safe.
  488. */
  489. const RenderAPICapabilities& getCapabilities(UINT32 deviceIdx) const;
  490. /** Returns the number of devices supported by this render API. */
  491. UINT32 getNumDevices() const { return mNumDevices; }
  492. /**
  493. * Returns information about available output devices and their video modes.
  494. *
  495. * @note Thread safe.
  496. */
  497. const VideoModeInfo& getVideoModeInfo() const { return *mVideoModeInfo; }
  498. /************************************************************************/
  499. /* UTILITY METHODS */
  500. /************************************************************************/
  501. /**
  502. * Contains a default matrix into a matrix suitable for use by this specific render system.
  503. *
  504. * @note Thread safe.
  505. */
  506. virtual void convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest) = 0;
  507. /**
  508. * Returns information about the specific API implementation.
  509. *
  510. * @note Thread safe.
  511. */
  512. virtual const RenderAPIInfo& getAPIInfo() const = 0;
  513. /**
  514. * Generates a parameter block description and calculates per-parameter offsets for the provided gpu data
  515. * parameters. The offsets are render API specific and correspond to std140 layout for OpenGL, and the default
  516. * layout in DirectX.
  517. *
  518. * @param[in] name Name to assign the parameter block.
  519. * @param[in] params List of parameters in the parameter block. Only name, type and array size fields need to be
  520. * populated, the rest will be populated when the method returns. If a parameter is a struct
  521. * then the elementSize field needs to be populated with the size of the struct in bytes.
  522. * @return Descriptor for the parameter block holding the provided parameters as laid out by the
  523. * default render API layout.
  524. */
  525. virtual GpuParamBlockDesc generateParamBlockDesc(const String& name, Vector<GpuParamDataDesc>& params) = 0;
  526. /************************************************************************/
  527. /* INTERNAL METHODS */
  528. /************************************************************************/
  529. protected:
  530. /**
  531. * Initializes the render API system and creates a primary render window.
  532. *
  533. * @note Sim thread only.
  534. */
  535. SPtr<bs::RenderWindow> initialize(const RENDER_WINDOW_DESC& primaryWindowDesc);
  536. /** Initializes the render API system. Called before the primary render window is created. */
  537. virtual void initialize();
  538. /**
  539. * Performs (optional) secondary initialization of the render API system. Called after the render window is
  540. * created.
  541. */
  542. virtual void initializeWithWindow(const SPtr<RenderWindow>& primaryWindow);
  543. /**
  544. * Shuts down the render API system and cleans up all resources.
  545. *
  546. * @note Sim thread.
  547. */
  548. void destroy();
  549. /** Performs render API system shutdown on the core thread. */
  550. virtual void destroyCore();
  551. /** Converts the number of vertices to number of primitives based on the specified draw operation. */
  552. UINT32 vertexCountToPrimCount(DrawOperationType type, UINT32 elementCount);
  553. /************************************************************************/
  554. /* INTERNAL DATA */
  555. /************************************************************************/
  556. protected:
  557. friend class bs::RenderAPIManager;
  558. SPtr<RenderTarget> mActiveRenderTarget;
  559. RenderAPICapabilities* mCurrentCapabilities;
  560. UINT32 mNumDevices;
  561. SPtr<VideoModeInfo> mVideoModeInfo;
  562. };
  563. /** @} */
  564. }
  565. }