BsRenderSystem.h 13 KB

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