BsRenderAPI.h 19 KB

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