BsRenderAPI.h 21 KB

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