BsRenderAPI.h 19 KB

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