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