BsRenderAPI.h 32 KB

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