BsRenderSystem.h 12 KB

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