BsRenderAPI.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. /**
  23. * Version of the render API interface usable from the sim thread. All the commands get queued on the accessor provided
  24. * to each method and will be executed on the core thread later.
  25. *
  26. * @see RenderAPICore
  27. *
  28. * @note Sim thread only.
  29. */
  30. class BS_CORE_EXPORT RenderAPI
  31. {
  32. public:
  33. /** @copydoc RenderAPICore::disableTextureUnit() */
  34. static void disableTextureUnit(CoreAccessor& accessor, GpuProgramType gptype, UINT16 texUnit);
  35. /** @copydoc RenderAPICore::setTexture() */
  36. static void setTexture(CoreAccessor& accessor, GpuProgramType gptype, UINT16 unit, bool enabled, const TexturePtr &texPtr);
  37. /** @copydoc RenderAPICore::setLoadStoreTexture() */
  38. static void setLoadStoreTexture(CoreAccessor& accessor, GpuProgramType gptype, UINT16 unit, bool enabled, const TexturePtr& texPtr,
  39. const TextureSurface& surface);
  40. /** @copydoc RenderAPICore::setSamplerState() */
  41. static void setSamplerState(CoreAccessor& accessor, GpuProgramType gptype, UINT16 texUnit, const SamplerStatePtr& samplerState);
  42. /** @copydoc RenderAPICore::setBlendState() */
  43. static void setBlendState(CoreAccessor& accessor, const BlendStatePtr& blendState);
  44. /** @copydoc RenderAPICore::setRasterizerState() */
  45. static void setRasterizerState(CoreAccessor& accessor, const RasterizerStatePtr& rasterizerState);
  46. /** @copydoc RenderAPICore::setRasterizerState() */
  47. static void setDepthStencilState(CoreAccessor& accessor, const DepthStencilStatePtr& depthStencilState, UINT32 stencilRefValue);
  48. /** @copydoc RenderAPICore::setVertexBuffers() */
  49. static void setVertexBuffers(CoreAccessor& accessor, UINT32 index, const Vector<VertexBufferPtr>& buffers);
  50. /** @copydoc RenderAPICore::setIndexBuffer() */
  51. static void setIndexBuffer(CoreAccessor& accessor, const IndexBufferPtr& buffer);
  52. /** @copydoc RenderAPICore::setVertexDeclaration() */
  53. static void setVertexDeclaration(CoreAccessor& accessor, const VertexDeclarationPtr& vertexDeclaration);
  54. /** @copydoc RenderAPICore::setViewport() */
  55. static void setViewport(CoreAccessor& accessor, const Rect2& vp);
  56. /** @copydoc RenderAPICore::setDrawOperation() */
  57. static void setDrawOperation(CoreAccessor& accessor, DrawOperationType op);
  58. /** @copydoc RenderAPICore::setClipPlanes() */
  59. static void setClipPlanes(CoreAccessor& accessor, const PlaneList& clipPlanes);
  60. /** @copydoc RenderAPICore::addClipPlane(const Plane&) */
  61. static void addClipPlane(CoreAccessor& accessor, const Plane& p);
  62. /** @copydoc RenderAPICore::resetClipPlanes() */
  63. static void resetClipPlanes(CoreAccessor& accessor);
  64. /** @copydoc RenderAPICore::setScissorTest() */
  65. static void setScissorTest(CoreAccessor& accessor, UINT32 left = 0, UINT32 top = 0, UINT32 right = 800, UINT32 bottom = 600);
  66. /** @copydoc RenderAPICore::setRenderTarget() */
  67. static void setRenderTarget(CoreAccessor& accessor, const RenderTargetPtr& target, bool readOnlyDepthStencil = false);
  68. /** @copydoc RenderAPICore::bindGpuProgram() */
  69. static void bindGpuProgram(CoreAccessor& accessor, const GpuProgramPtr& prg);
  70. /** @copydoc RenderAPICore::unbindGpuProgram() */
  71. static void unbindGpuProgram(CoreAccessor& accessor, GpuProgramType gptype);
  72. /** @copydoc RenderAPICore::setConstantBuffers() */
  73. static void setConstantBuffers(CoreAccessor& accessor, GpuProgramType gptype, const GpuParamsPtr& params);
  74. /** @copydoc RenderAPICore::setGpuParams() */
  75. static void setGpuParams(CoreAccessor& accessor, GpuProgramType gptype, const GpuParamsPtr& params);
  76. /** @copydoc RenderAPICore::beginFrame() */
  77. static void beginRender(CoreAccessor& accessor);
  78. /** @copydoc RenderAPICore::endFrame() */
  79. static void endRender(CoreAccessor& accessor);
  80. /** @copydoc RenderAPICore::clearRenderTarget() */
  81. static void clearRenderTarget(CoreAccessor& accessor, UINT32 buffers,
  82. const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0, UINT8 targetMask = 0xFF);
  83. /** @copydoc RenderAPICore::clearViewport() */
  84. static void clearViewport(CoreAccessor& accessor, UINT32 buffers, const Color& color = Color::Black,
  85. float depth = 1.0f, UINT16 stencil = 0, UINT8 targetMask = 0xFF);
  86. /** @copydoc RenderAPICore::swapBuffers() */
  87. static void swapBuffers(CoreAccessor& accessor, const RenderTargetPtr& target);
  88. /** @copydoc RenderAPICore::draw() */
  89. static void draw(CoreAccessor& accessor, UINT32 vertexOffset, UINT32 vertexCount);
  90. /** @copydoc RenderAPICore::drawIndexed() */
  91. static void drawIndexed(CoreAccessor& accessor, UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount);
  92. /** @copydoc RenderAPICore::dispatchCompute() */
  93. static void dispatchCompute(CoreAccessor& accessor, UINT32 numGroupsX, UINT32 numGroupsY = 1, UINT32 numGroupsZ = 1);
  94. /** @copydoc RenderAPICore::getVideoModeInfo */
  95. static const VideoModeInfo& getVideoModeInfo();
  96. /** @copydoc RenderAPICore::getColorVertexElementType */
  97. static VertexElementType getColorVertexElementType();
  98. /** @copydoc RenderAPICore::convertProjectionMatrix */
  99. static void convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest);
  100. /** @copydoc RenderAPICore::getHorizontalTexelOffset */
  101. static float getHorizontalTexelOffset();
  102. /** @copydoc RenderAPICore::getVerticalTexelOffset */
  103. static float getVerticalTexelOffset();
  104. /** @copydoc RenderAPICore::getMinimumDepthInputValue */
  105. static float getMinimumDepthInputValue();
  106. /** @copydoc RenderAPICore::getMaximumDepthInputValue */
  107. static float getMaximumDepthInputValue();
  108. /** @copydoc RenderAPICore::getVertexColorFlipRequired */
  109. static bool getVertexColorFlipRequired();
  110. };
  111. /** @cond INTERNAL */
  112. /**
  113. * Render system provides base functionality for a rendering API like DirectX or OpenGL. Most of the class is abstract
  114. * and specific subclass for each rendering API needs to be implemented.
  115. *
  116. * @note Core thread only unless specifically noted otherwise on per-method basis.
  117. */
  118. class BS_CORE_EXPORT RenderAPICore : public Module<RenderAPICore>
  119. {
  120. public:
  121. RenderAPICore();
  122. virtual ~RenderAPICore();
  123. /**
  124. * Returns the name of the rendering system.
  125. *
  126. * @note Thread safe.
  127. */
  128. virtual const StringID& getName() const = 0;
  129. /**
  130. * Gets the name of the primary shading language used by the rendering system.
  131. *
  132. * @note Thread safe.
  133. */
  134. virtual const String& getShadingLanguageName() const = 0;
  135. /**
  136. * Sets a sampler state for the specified texture unit.
  137. *
  138. * @see SamplerState
  139. */
  140. virtual void setSamplerState(GpuProgramType gptype, UINT16 texUnit, const SPtr<SamplerStateCore>& samplerState) = 0;
  141. /**
  142. * Sets a blend state used for all active render targets.
  143. *
  144. * @see BlendState
  145. */
  146. virtual void setBlendState(const SPtr<BlendStateCore>& blendState) = 0;
  147. /**
  148. * Sets a state that controls various rasterizer options.
  149. *
  150. * @see RasterizerState
  151. */
  152. virtual void setRasterizerState(const SPtr<RasterizerStateCore>& rasterizerState) = 0;
  153. /**
  154. * Sets a state that controls depth & stencil buffer options.
  155. *
  156. * @see DepthStencilState
  157. */
  158. virtual void setDepthStencilState(const SPtr<DepthStencilStateCore>& depthStencilState, UINT32 stencilRefValue) = 0;
  159. /**
  160. * Binds a texture to the pipeline for the specified GPU program type at the specified slot. If the slot matches
  161. * the one configured in the GPU program the program will be able to access this texture on the GPU.
  162. */
  163. virtual void setTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const SPtr<TextureCore>& texPtr) = 0;
  164. /** Turns off a texture unit. */
  165. virtual void disableTextureUnit(GpuProgramType gptype, UINT16 texUnit);
  166. /** Binds a texture that can be used for random load/store operations from a GPU program. */
  167. virtual void setLoadStoreTexture(GpuProgramType gptype, UINT16 unit, bool enabled,
  168. const SPtr<TextureCore>& texPtr, const TextureSurface& surface) = 0;
  169. /**
  170. * Signals that rendering for a specific viewport has started. Any draw calls need to be called between beginFrame()
  171. * and endFrame().
  172. */
  173. virtual void beginFrame() = 0;
  174. /** Ends that rendering to a specific viewport has ended. */
  175. virtual void endFrame() = 0;
  176. /**
  177. * Sets the active viewport that will be used for all render operations.
  178. *
  179. * @param[in] area Area of the viewport, in normalized ([0,1] range) coordinates.
  180. */
  181. virtual void setViewport(const Rect2& area) = 0;
  182. /**
  183. * Sets the provided vertex buffers starting at the specified source index. Set buffer to nullptr to clear the
  184. * buffer at the specified index.
  185. */
  186. virtual void setVertexBuffers(UINT32 index, SPtr<VertexBufferCore>* buffers, UINT32 numBuffers) = 0;
  187. /**
  188. * Sets an index buffer to use when drawing. Indices in an index buffer reference vertices in the vertex buffer,
  189. * which increases cache coherency and reduces the size of vertex buffers by eliminating duplicate data.
  190. */
  191. virtual void setIndexBuffer(const SPtr<IndexBufferCore>& buffer) = 0;
  192. /**
  193. * Sets the vertex declaration to use when drawing. Vertex declaration is used to decode contents of a single
  194. * vertex in a vertex buffer.
  195. */
  196. virtual void setVertexDeclaration(const SPtr<VertexDeclarationCore>& vertexDeclaration) = 0;
  197. /** Sets the draw operation that determines how to interpret the elements of the index or vertex buffers. */
  198. virtual void setDrawOperation(DrawOperationType op) = 0;
  199. /**
  200. * Draw an object based on currently bound GPU programs, vertex declaration and vertex buffers. Draws directly from
  201. * the vertex buffer without using indices.
  202. */
  203. virtual void draw(UINT32 vertexOffset, UINT32 vertexCount) = 0;
  204. /** Draw an object based on currently bound GPU programs, vertex declaration, vertex and index buffers. */
  205. virtual void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount) = 0;
  206. /**
  207. * Executes the currently bound compute shader.
  208. *
  209. * @param[in] numGroupsX Number of groups to start in the X direction. Must be in range [1, 65535].
  210. * @param[in] numGroupsY Number of groups to start in the Y direction. Must be in range [1, 65535].
  211. * @param[in] numGroupsZ Number of groups to start in the Z direction. Must be in range [1, 64].
  212. */
  213. virtual void dispatchCompute(UINT32 numGroupsX, UINT32 numGroupsY = 1, UINT32 numGroupsZ = 1) = 0;
  214. /** Swap the front and back buffer of the specified render target. */
  215. virtual void swapBuffers(const SPtr<RenderTargetCore>& target);
  216. /**
  217. * Gets the capabilities of the render system.
  218. *
  219. * @note Thread safe.
  220. */
  221. const RenderAPICapabilities* getCapabilities() const;
  222. /** Returns information about the driver version. */
  223. virtual const DriverVersion& getDriverVersion() const;
  224. /**
  225. * Binds the provided GPU program to the pipeline. Any following draw operations will use this program.
  226. *
  227. * @note You need to bind at least a vertex and a fragment program in order to draw something.
  228. */
  229. virtual void bindGpuProgram(const SPtr<GpuProgramCore>& prg);
  230. /**
  231. * Binds constant(uniform) GPU program parameters. Caller must ensure these match the previously bound GPU program.
  232. */
  233. virtual void setConstantBuffers(GpuProgramType gptype, const SPtr<GpuParamsCore>& params) = 0;
  234. /** Binds all specified GPU program parameters. Caller must ensure these match the previously bound GPU program. */
  235. virtual void setGpuParams(GpuProgramType gptype, const SPtr<GpuParamsCore>& params);
  236. /** Unbinds a program of a given type. */
  237. virtual void unbindGpuProgram(GpuProgramType gptype);
  238. /** Query if a GPU program of a given type is currently bound. */
  239. virtual bool isGpuProgramBound(GpuProgramType gptype);
  240. /** Sets up clip planes that will clip drawn geometry on the negative side of the planes. */
  241. virtual void setClipPlanes(const PlaneList& clipPlanes);
  242. /** Adds a new clip plane. All drawn geometry will be clipped to this plane. */
  243. virtual void addClipPlane(const Plane& p);
  244. /** Clears all clip planes. */
  245. virtual void resetClipPlanes();
  246. /**
  247. * Allows you to set up a region in which rendering can take place. Coordinates are in pixels. No rendering will be
  248. * done to render target pixels outside of the provided region.
  249. */
  250. virtual void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom) = 0;
  251. /**
  252. * Clears the currently active render target.
  253. *
  254. * @param[in] buffers Combination of one or more elements of FrameBufferType denoting which buffers are
  255. * to be cleared.
  256. * @param[in] color (optional) The color to clear the color buffer with, if enabled.
  257. * @param[in] depth (optional) The value to initialize the depth buffer with, if enabled.
  258. * @param[in] stencil (optional) The value to initialize the stencil buffer with, if enabled.
  259. * @param[in] targetMask (optional) In case multiple render targets are bound, this allows you to control
  260. * which ones to clear (0x01 first, 0x02 second, 0x04 third, etc., and combinations).
  261. */
  262. virtual void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f,
  263. UINT16 stencil = 0, UINT8 targetMask = 0xFF) = 0;
  264. /**
  265. * Clears the currently active viewport (i.e. it clears just a sub-area of a render-target that is covered by the
  266. * viewport, as opposed to clearRenderTarget() which always clears the entire render target).
  267. *
  268. * @param[in] buffers Combination of one or more elements of FrameBufferType denoting which buffers are to be
  269. * cleared.
  270. * @param[in] color (optional) The color to clear the color buffer with, if enabled.
  271. * @param[in] depth (optional) The value to initialize the depth buffer with, if enabled.
  272. * @param[in] stencil (optional) The value to initialize the stencil buffer with, if enabled.
  273. * @param[in] targetMask (optional) In case multiple render targets are bound, this allows you to control
  274. * which ones to clear (0x01 first, 0x02 second, 0x04 third, etc., and combinations).
  275. */
  276. virtual void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f,
  277. UINT16 stencil = 0, UINT8 targetMask = 0xFF) = 0;
  278. /**
  279. * Change the render target into which we want to draw.
  280. *
  281. * @param[in] target Render target to draw to.
  282. * @param[in] readOnlyDepthStencil If true the caller guarantees he won't write to the depth/stencil buffer
  283. * (if any was provided). This allows the depth buffer to be bound for depth
  284. * testing, as well as reading in a shader, at the same time.
  285. */
  286. virtual void setRenderTarget(const SPtr<RenderTargetCore>& target, bool readOnlyDepthStencil = false) = 0;
  287. /**
  288. * Returns information about available output devices and their video modes.
  289. *
  290. * @note Thread safe.
  291. */
  292. const VideoModeInfo& getVideoModeInfo() const { return *mVideoModeInfo; }
  293. /************************************************************************/
  294. /* UTILITY METHODS */
  295. /************************************************************************/
  296. /**
  297. * Gets the native type used for vertex colors.
  298. *
  299. * @note Thread safe.
  300. */
  301. virtual VertexElementType getColorVertexElementType() const = 0;
  302. /**
  303. * Contains a default matrix into a matrix suitable for use by this specific render system.
  304. *
  305. * @note Thread safe.
  306. */
  307. virtual void convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest) = 0;
  308. /**
  309. * Gets horizontal texel offset used for mapping texels to pixels in this render system.
  310. *
  311. * @note Thread safe.
  312. */
  313. virtual float getHorizontalTexelOffset() = 0;
  314. /**
  315. * Gets vertical texel offset used for mapping texels to pixels in this render system.
  316. *
  317. * @note Thread safe.
  318. */
  319. virtual float getVerticalTexelOffset() = 0;
  320. /**
  321. * Gets the minimum (closest) depth value used by this render system.
  322. *
  323. * @note Thread safe.
  324. */
  325. virtual float getMinimumDepthInputValue() = 0;
  326. /**
  327. * Gets the maximum (farthest) depth value used by this render system.
  328. *
  329. * @note Thread safe.
  330. */
  331. virtual float getMaximumDepthInputValue() = 0;
  332. /**
  333. * Checks if vertex color needs to be flipped before sent to the shader.
  334. *
  335. * @note Thread safe.
  336. */
  337. virtual bool getVertexColorFlipRequired() const { return false; }
  338. /** Checks whether GPU programs expect matrices in column major format. */
  339. virtual bool getGpuProgramHasColumnMajorMatrices() const { return false; }
  340. /**
  341. * Generates a parameter block description and calculates per-parameter offsets for the provided gpu data
  342. * parameters. The offsets are render API specific and correspond to std140 layout for OpenGL, and the default
  343. * layout in DirectX.
  344. *
  345. * @param[in] name Name to assign the parameter block.
  346. * @param[in] params List of parameters in the parameter block. Only name, type and array size fields need to be
  347. * populated, the rest will be populated when the method returns.
  348. * @return Descriptor for the parameter block holding the provided parameters as laid out by the
  349. * default render API layout.
  350. */
  351. virtual GpuParamBlockDesc generateParamBlockDesc(const String& name, Vector<GpuParamDataDesc>& params) = 0;
  352. /************************************************************************/
  353. /* INTERNAL METHODS */
  354. /************************************************************************/
  355. protected:
  356. /**
  357. * Initializes the render API system and creates a primary render window.
  358. *
  359. * @note
  360. * Although I'd like otherwise, due to the nature of some render API implementations, you cannot initialize the
  361. * render system without a window.
  362. * @note
  363. * Sim thread.
  364. */
  365. RenderWindowPtr initialize(const RENDER_WINDOW_DESC& primaryWindowDesc);
  366. /**
  367. * Prepares the initialization of the render API system on the core thread. After the system is prepared a render
  368. * window can be created and initialization finalized.
  369. */
  370. virtual void initializePrepare();
  371. /**
  372. * Finalizes the initialization of the render API system on the core thread. Should be called after the primary
  373. * render window is created.
  374. */
  375. virtual void initializeFinalize(const SPtr<RenderWindowCore>& primaryWindow);
  376. /**
  377. * Shuts down the render API system and cleans up all resources.
  378. *
  379. * @note Sim thread.
  380. */
  381. void destroy();
  382. /** Performs render API system shutdown on the core thread. */
  383. virtual void destroyCore();
  384. /** @copydoc setClipPlanes */
  385. virtual void setClipPlanesImpl(const PlaneList& clipPlanes) = 0;
  386. /************************************************************************/
  387. /* INTERNAL DATA */
  388. /************************************************************************/
  389. protected:
  390. friend class RenderAPIManager;
  391. SPtr<RenderTargetCore> mActiveRenderTarget;
  392. DriverVersion mDriverVersion;
  393. CullingMode mCullingMode;
  394. UINT16 mDisabledTexUnitsFrom;
  395. bool mVertexProgramBound;
  396. bool mGeometryProgramBound;
  397. bool mFragmentProgramBound;
  398. bool mDomainProgramBound;
  399. bool mHullProgramBound;
  400. bool mComputeProgramBound;
  401. PlaneList mClipPlanes;
  402. bool mClipPlanesDirty;
  403. RenderAPICapabilities* mCurrentCapabilities;
  404. VideoModeInfoPtr mVideoModeInfo;
  405. };
  406. /** @endcond */
  407. /** @} */
  408. }