BsRenderSystem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include <memory>
  4. #include "BsString.h"
  5. #include "BsSamplerState.h"
  6. #include "BsRenderStats.h"
  7. #include "BsCommandQueue.h"
  8. #include "BsDrawOps.h"
  9. #include "BsRenderSystemCapabilities.h"
  10. #include "BsRenderTarget.h"
  11. #include "BsRenderTexture.h"
  12. #include "BsRenderWindow.h"
  13. #include "BsGpuProgram.h"
  14. #include "BsVertexDeclaration.h"
  15. #include "BsPlane.h"
  16. #include "BsModule.h"
  17. #include "BsEvent.h"
  18. namespace BansheeEngine
  19. {
  20. /**
  21. * @brief Render system provides base functionality for a rendering API like
  22. * DirectX or OpenGL. Most of the class is abstract and specific
  23. * subclass for each rendering API needs to be implemented.
  24. *
  25. * @note Core thread only unless specifically noted otherwise on per-method basis.
  26. */
  27. class BS_CORE_EXPORT RenderSystem : public Module<RenderSystem>
  28. {
  29. public:
  30. RenderSystem();
  31. virtual ~RenderSystem();
  32. /**
  33. * @brief Returns the name of the rendering system.
  34. *
  35. * @note Thread safe.
  36. */
  37. virtual const String& getName() const = 0;
  38. /**
  39. * @brief Gets the name of the primary shading language
  40. * used by the rendering system.
  41. *
  42. * @note Thread safe.
  43. */
  44. virtual const String& getShadingLanguageName() const = 0;
  45. /**
  46. * @brief Sets a sampler state for the specified texture unit.
  47. *
  48. * @see SamplerState
  49. */
  50. virtual void setSamplerState(GpuProgramType gptype, UINT16 texUnit, const SamplerStatePtr& samplerState) = 0;
  51. /**
  52. * @brief Sets a blend state used for all active render targets.
  53. *
  54. * @see BlendState
  55. */
  56. virtual void setBlendState(const BlendStatePtr& blendState) = 0;
  57. /**
  58. * @brief Sets a state that controls various rasterizer options.
  59. *
  60. * @see RasterizerState
  61. */
  62. virtual void setRasterizerState(const RasterizerStatePtr& rasterizerState) = 0;
  63. /**
  64. * @brief Sets a state that controls depth & stencil buffer options.
  65. *
  66. * @see DepthStencilState
  67. */
  68. virtual void setDepthStencilState(const DepthStencilStatePtr& depthStencilState, UINT32 stencilRefValue) = 0;
  69. /**
  70. * @brief Binds a texture to the pipeline for the specified GPU program type at the specified slot.
  71. * If the slot matches the one configured in the GPU program the program will be able to access
  72. * this texture on the GPU.
  73. */
  74. virtual void setTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const TexturePtr& texPtr) = 0;
  75. /**
  76. * @brief Turns off a texture unit.
  77. */
  78. virtual void disableTextureUnit(GpuProgramType gptype, UINT16 texUnit);
  79. /**
  80. * @brief Signals that rendering for a specific viewport has started. Any draw calls
  81. * need to be called between beginFrame and endFrame. You may not switch render targets
  82. * until you call endFrame.
  83. */
  84. virtual void beginFrame() = 0;
  85. /**
  86. * @brief Ends that rendering to a specific viewport has ended.
  87. */
  88. virtual void endFrame() = 0;
  89. /**
  90. * @brief Sets the active viewport that will be used for all render operations.
  91. * Viewport will change active render target if needed.
  92. */
  93. virtual void setViewport(Viewport vp) = 0;
  94. /**
  95. * @brief Sets the provided vertex buffers starting at the specified source index.
  96. * Set buffer to nullptr to clear the buffer at the specified index.
  97. */
  98. virtual void setVertexBuffers(UINT32 index, VertexBufferPtr* buffers, UINT32 numBuffers) = 0;
  99. /**
  100. * @brief Sets an index buffer to use when drawing. Indices in an index buffer
  101. * reference vertices in the vertex buffer, which increases cache coherency
  102. * and reduces the size of vertex buffers by eliminating duplicate data.
  103. */
  104. virtual void setIndexBuffer(const IndexBufferPtr& buffer) = 0;
  105. /**
  106. * @brief Sets the vertex declaration to use when drawing. Vertex declaration
  107. * is used to decode contents of a single vertex in a vertex buffer.
  108. */
  109. virtual void setVertexDeclaration(VertexDeclarationPtr vertexDeclaration) = 0;
  110. /**
  111. * @brief Sets the draw operation that determines how to interpret the elements
  112. * of the index or vertex buffers.
  113. */
  114. virtual void setDrawOperation(DrawOperationType op) = 0;
  115. /**
  116. * @brief A helper method that provides a simple way of rendering a single object. It will
  117. * automatically set up vertex declaration, draw operation, vertex and index buffers and
  118. * draw them.
  119. *
  120. * @param mesh The mesh.
  121. * @param indexOffset (optional) Offset into the mesh buffer to start drawing from.
  122. * @param indexCount (optional) Number of indexes to draw, starting at the offset. Ignored if "drawIndexed" is false. If 0 all indices in the mesh will be drawn.
  123. * @param useIndices (optional) If true, drawing is done using the index buffer on the mesh and the provided offset and size, otherwise all mesh vertices are drawn sequentially.
  124. * @param drawOp (optional) Draw operation to use when rendering.
  125. */
  126. virtual void render(const MeshBasePtr& mesh, UINT32 indexOffset = 0, UINT32 indexCount = 0, bool useIndices = true, DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  127. /**
  128. * @brief Draw an object based on currently bound GPU programs, vertex declaration and vertex buffers.
  129. *
  130. * Draws directly from the vertex buffer without using indices.
  131. */
  132. virtual void draw(UINT32 vertexOffset, UINT32 vertexCount) = 0;
  133. /**
  134. * @brief Draw an object based on currently bound GPU programs, vertex declaration, vertex
  135. * and index buffers.
  136. */
  137. virtual void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount) = 0;
  138. /**
  139. * @brief Swap the front and back buffer of the specified render target.
  140. */
  141. virtual void swapBuffers(RenderTargetPtr target);
  142. /**
  143. * @brief Gets the capabilities of the render system.
  144. *
  145. * @note Thread safe.
  146. */
  147. const RenderSystemCapabilities* getCapabilities() const;
  148. /**
  149. * @brief Returns information about the driver version.
  150. */
  151. virtual const DriverVersion& getDriverVersion() const;
  152. /**
  153. * @brief Binds the provided GPU program to the pipeline. Any following
  154. * draw operations will use this program.
  155. *
  156. * @note You need to bind at least a vertex and a fragment program in order to draw something.
  157. */
  158. virtual void bindGpuProgram(HGpuProgram prg);
  159. /**
  160. * @brief Binds GPU program parameters. Caller must ensure these match the previously
  161. * bound GPU program.
  162. */
  163. virtual void bindGpuParams(GpuProgramType gptype, GpuParamsPtr params) = 0;
  164. /**
  165. * @brief Unbinds a program of a given type.
  166. */
  167. virtual void unbindGpuProgram(GpuProgramType gptype);
  168. /**
  169. * @brief Query if a GPU program of a given type is currently bound.
  170. */
  171. virtual bool isGpuProgramBound(GpuProgramType gptype);
  172. /**
  173. * @brief Sets up clip planes that will clip drawn geometry on the negative side of the planes.
  174. */
  175. virtual void setClipPlanes(const PlaneList& clipPlanes);
  176. /**
  177. * @brief Adds a new clip plane. All drawn geometry will be clipped to this plane.
  178. */
  179. virtual void addClipPlane(const Plane& p);
  180. /**
  181. * @brief Clears all clip planes.
  182. */
  183. virtual void resetClipPlanes();
  184. /**
  185. * @brief Allows you to set up a region in which rendering can take place. Coordinates are in pixels.
  186. * No rendering will be done to render target pixels outside of the provided region.
  187. */
  188. virtual void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom) = 0;
  189. /**
  190. * @brief Clears the currently active render target.
  191. *
  192. * @param buffers Combination of one or more elements of FrameBufferType
  193. * denoting which buffers are to be cleared.
  194. * @param color (optional) The color to clear the color buffer with, if enabled.
  195. * @param depth (optional) The value to initialise the depth buffer with, if enabled.
  196. * @param stencil (optional) The value to initialise the stencil buffer with, if enabled.
  197. */
  198. virtual void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0) = 0;
  199. /**
  200. * @brief Clears the currently active viewport (i.e. it clears just a sub-area of a render-target that is covered by the viewport,
  201. * as opposed to clearRenderTarget which always clears the entire render target).
  202. *
  203. * @param buffers Combination of one or more elements of FrameBufferType
  204. * denoting which buffers are to be cleared.
  205. * @param color (optional) The color to clear the color buffer with, if enabled.
  206. * @param depth (optional) The value to initialise the depth buffer with, if enabled.
  207. * @param stencil (optional) The value to initialise the stencil buffer with, if enabled.
  208. */
  209. virtual void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0) = 0;
  210. /**
  211. * @brief Change the render target into which we want to draw.
  212. */
  213. virtual void setRenderTarget(RenderTargetPtr target) = 0;
  214. /**
  215. * @brief Updates the resource with the specified data.
  216. *
  217. * @note It is assumed GpuResourceData has been locked before being passed here. Data will be unlocked
  218. * when this method finishes.
  219. */
  220. void writeSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceDataPtr& data, bool discardEntireBuffer, AsyncOp& asyncOp);
  221. /**
  222. * @brief Reads data from a resource into a pre-allocated GpuResourceData instance.
  223. *
  224. * @note It is assumed GpuResourceData has been locked before being passed here. Data will be unlocked
  225. * when this method finishes.
  226. */
  227. void readSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, GpuResourceDataPtr& data, AsyncOp& asyncOp);
  228. /**
  229. * @brief Returns information about available output devices and their video modes.
  230. *
  231. * @note Thread safe.
  232. */
  233. const VideoModeInfo& getVideoModeInfo() const { return *mVideoModeInfo; }
  234. /************************************************************************/
  235. /* UTILITY METHODS */
  236. /************************************************************************/
  237. /**
  238. * @brief Gets the native type used for vertex colors.
  239. *
  240. * @note Thread safe.
  241. */
  242. virtual VertexElementType getColorVertexElementType() const = 0;
  243. /**
  244. * @brief Contains a default matrix into a matrix suitable for use
  245. * by this specific render system.
  246. *
  247. * @note Thread safe.
  248. */
  249. virtual void convertProjectionMatrix(const Matrix4& matrix,
  250. Matrix4& dest, bool forGpuProgram = false) = 0;
  251. /**
  252. * @brief Gets horizontal texel offset used for mapping texels to pixels
  253. * in this render system.
  254. *
  255. * @note Thread safe.
  256. */
  257. virtual float getHorizontalTexelOffset() = 0;
  258. /**
  259. * @brief Gets vertical texel offset used for mapping texels to pixels
  260. * in this render system.
  261. *
  262. * @note Thread safe.
  263. */
  264. virtual float getVerticalTexelOffset() = 0;
  265. /**
  266. * @brief Gets the minimum (closest) depth value used by this
  267. * render system.
  268. *
  269. * @note Thread safe.
  270. */
  271. virtual float getMinimumDepthInputValue() = 0;
  272. /**
  273. * @brief Gets the maximum (farthest) depth value used by this
  274. * render system.
  275. *
  276. * @note Thread safe.
  277. */
  278. virtual float getMaximumDepthInputValue() = 0;
  279. /**
  280. * @brief Returns an object containing various rendering statistics.
  281. *
  282. * @note Do not modify the returned state unless you know what you are doing, it will
  283. * change the actual internal object.
  284. */
  285. RenderStats& getRenderStats() { return mRenderStats; }
  286. /************************************************************************/
  287. /* INTERNAL METHODS */
  288. /************************************************************************/
  289. protected:
  290. /**
  291. * @brief Initializes the render system and creates a primary render window.
  292. *
  293. * @note Although I'd like otherwise, due to the nature of some render system implementations,
  294. * you cannot initialize the render system without a window.
  295. *
  296. * Sim thread.
  297. */
  298. RenderWindowPtr initialize(const RENDER_WINDOW_DESC& primaryWindowDesc);
  299. /**
  300. * @brief Performs second part of the initialization (on Core thread), first part
  301. * being in initialize().
  302. */
  303. virtual void initialize_internal(AsyncOp& asyncOp);
  304. /**
  305. * @brief Shuts down the render system and cleans up all resources.
  306. *
  307. * @note Sim thread.
  308. */
  309. void destroy();
  310. /**
  311. * @brief Performs second part of render system shutdown on the core thread.
  312. */
  313. virtual void destroy_internal();
  314. /**
  315. * @copydoc setClipPlanes.
  316. */
  317. virtual void setClipPlanesImpl(const PlaneList& clipPlanes) = 0;
  318. /************************************************************************/
  319. /* INTERNAL DATA */
  320. /************************************************************************/
  321. protected:
  322. friend class RenderSystemManager;
  323. RenderTargetPtr mActiveRenderTarget;
  324. RenderStats mRenderStats;
  325. DriverVersion mDriverVersion;
  326. CullingMode mCullingMode;
  327. UINT16 mDisabledTexUnitsFrom;
  328. bool mVertexProgramBound;
  329. bool mGeometryProgramBound;
  330. bool mFragmentProgramBound;
  331. bool mDomainProgramBound;
  332. bool mHullProgramBound;
  333. bool mComputeProgramBound;
  334. PlaneList mClipPlanes;
  335. bool mClipPlanesDirty;
  336. RenderSystemCapabilities* mCurrentCapabilities;
  337. VideoModeInfoPtr mVideoModeInfo;
  338. // TODO - Only used between initialize and initialize_internal. Handle it better?
  339. RENDER_WINDOW_DESC mPrimaryWindowDesc;
  340. };
  341. }