BsRenderAPI.h 17 KB

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