BsRenderAPI.h 18 KB

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