BsRenderAPI.h 32 KB

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