2
0

BsRenderAPI.h 19 KB

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