BsRenderAPI.h 30 KB

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