CmRenderSystem.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __RenderSystem_H_
  25. #define __RenderSystem_H_
  26. // Precompiler options
  27. #include "CmPrerequisites.h"
  28. #include <memory>
  29. #include "CmString.h"
  30. #include "CmSamplerState.h"
  31. #include "CmCommonEnums.h"
  32. #include "CmCommandQueue.h"
  33. #include "CmDrawOps.h"
  34. #include "CmRenderSystemCapabilities.h"
  35. #include "CmRenderTarget.h"
  36. #include "CmRenderTexture.h"
  37. #include "CmRenderWindow.h"
  38. #include "CmGpuProgram.h"
  39. #include "CmVertexDeclaration.h"
  40. #include "CmPlane.h"
  41. #include "CmModule.h"
  42. #include "boost/function.hpp"
  43. #include "boost/signal.hpp"
  44. namespace CamelotFramework
  45. {
  46. /** \addtogroup Core
  47. * @{
  48. */
  49. /** \addtogroup RenderSystem
  50. * @{
  51. */
  52. typedef Multimap<UINT8, RenderTarget * >::type RenderTargetPriorityMap;
  53. class TextureManager;
  54. /** Defines the functionality of a 3D API
  55. @remarks
  56. The RenderSystem class provides a base interface
  57. which abstracts the general functionality of the 3D API
  58. e.g. Direct3D or OpenGL. Whilst a few of the general
  59. methods have implementations, most of this class is
  60. abstract, requiring a subclass based on a specific API
  61. to be constructed to provide the full functionality.
  62. @author
  63. Steven Streeting
  64. @version
  65. 1.0
  66. */
  67. class CM_EXPORT RenderSystem : public Module<RenderSystem>
  68. {
  69. public:
  70. /** Default Constructor.
  71. */
  72. RenderSystem();
  73. /** Destructor.
  74. */
  75. virtual ~RenderSystem();
  76. /** Returns the name of the rendering system.
  77. */
  78. virtual const String& getName(void) const = 0;
  79. /**
  80. * @brief Gets the name of the primary shading language.
  81. */
  82. virtual const String& getShadingLanguageName() const = 0;
  83. /**
  84. * @brief Sets a sampler state for the specified texture unit.
  85. * @see SamplerState
  86. */
  87. virtual void setSamplerState(GpuProgramType gptype, UINT16 texUnit, const SamplerStatePtr& samplerState) = 0;
  88. /** Turns off a texture unit. */
  89. virtual void disableTextureUnit(GpuProgramType gptype, UINT16 texUnit);
  90. /**
  91. * @brief Sets a blend state used for all active render targets.
  92. * @see BlendState
  93. */
  94. virtual void setBlendState(const BlendStatePtr& blendState) = 0;
  95. /**
  96. * @brief Sets a state that controls various rasterizer options.
  97. * @see RasterizerState
  98. */
  99. virtual void setRasterizerState(const RasterizerStatePtr& rasterizerState) = 0;
  100. /**
  101. * @brief Sets a state that controls depth & stencil buffer options.
  102. * @see DepthStencilState
  103. */
  104. virtual void setDepthStencilState(const DepthStencilStatePtr& depthStencilState, UINT32 stencilRefValue) = 0;
  105. /**
  106. Sets the texture to bind to a given texture unit.
  107. User processes would not normally call this direct unless rendering
  108. primitives themselves.
  109. @param unit The index of the texture unit to modify. Multitexturing
  110. hardware can support multiple units (see
  111. RenderSystemCapabilites::getNumTextureUnits)
  112. @param enabled Boolean to turn the unit on/off
  113. @param texPtr Pointer to the texture to use.
  114. */
  115. virtual void setTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const TexturePtr &texPtr) = 0;
  116. /**
  117. * Signifies the beginning of a frame, i.e. the start of rendering on a single viewport. Will occur
  118. * several times per complete frame if multiple viewports exist.
  119. */
  120. virtual void beginFrame(void) = 0;
  121. /**
  122. * Ends rendering of a frame to the current viewport.
  123. */
  124. virtual void endFrame(void) = 0;
  125. /**
  126. Sets the provided viewport as the active one for future
  127. rendering operations. This viewport is aware of it's own
  128. camera and render target. Must be implemented by subclass.
  129. @param target Viewport to render to.
  130. */
  131. virtual void setViewport(const ViewportPtr& vp) = 0;
  132. /** Sets the provided vertex buffers starting at the specified source index.
  133. /** @note Set buffer to nullptr to clear the buffer at the specified index.*/
  134. virtual void setVertexBuffers(UINT32 index, VertexBufferPtr* buffers, UINT32 numBuffers) = 0;
  135. /**
  136. * @brief Sets an index buffer to use when drawing. Indices in an index buffer
  137. * reference vertices in the vertex buffer, which increases cache coherency
  138. * and reduces the size of vertex buffers by eliminating duplicate data.
  139. */
  140. virtual void setIndexBuffer(const IndexBufferPtr& buffer) = 0;
  141. /**
  142. * @brief Sets the vertex declaration to use when drawing. Vertex declaration
  143. * is used to decode contents of a single vertex in a vertex buffer.
  144. */
  145. virtual void setVertexDeclaration(VertexDeclarationPtr vertexDeclaration) = 0;
  146. /**
  147. * @brief Sets the draw operation that determines how to interpret the elements
  148. * of the index or vertex buffers.
  149. */
  150. virtual void setDrawOperation(DrawOperationType op) = 0;
  151. /**
  152. * @brief A helper method that provides a simple way of rendering a single object. It will
  153. * automatically set up vertex declaration, draw operation, vertex and index buffers and
  154. * draw them.
  155. *
  156. * @param mesh The mesh.
  157. * @param indexOffset (optional) Offset into the mesh buffer to start drawing from.
  158. * @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.
  159. * @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.
  160. * @param drawOp (optional) Draw operation to use when rendering.
  161. */
  162. virtual void render(const MeshBasePtr& mesh, UINT32 indexOffset = 0, UINT32 indexCount = 0, bool useIndices = true, DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  163. /**
  164. * @brief Draw an object based on currently set
  165. * shaders, vertex declaration index buffers.
  166. *
  167. * Draws directly from the vertex buffer without using
  168. * indices.
  169. */
  170. virtual void draw(UINT32 vertexOffset, UINT32 vertexCount) = 0;
  171. /**
  172. * @brief Draw an object based on currently set
  173. * shaders, vertex declaration and vertex
  174. * and index buffers.
  175. */
  176. virtual void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount) = 0;
  177. /**
  178. * @brief Swap the front and back buffer of the specified render target.
  179. */
  180. virtual void swapBuffers(RenderTargetPtr target);
  181. /**
  182. * @brief Gets the capabilities of the render system.
  183. *
  184. * @note Thread safe.
  185. */
  186. const RenderSystemCapabilities* getCapabilities() const;
  187. /** Returns the driver version.
  188. */
  189. virtual const DriverVersion& getDriverVersion() const;
  190. /** Binds a given GpuProgram (but not the parameters).
  191. @remarks Only one GpuProgram of each type can be bound at once, binding another
  192. one will simply replace the existing one.
  193. */
  194. virtual void bindGpuProgram(HGpuProgram prg);
  195. /** Bind Gpu program parameters.
  196. @param gptype The type of program to bind the parameters to
  197. @param params The parameters to bind
  198. */
  199. virtual void bindGpuParams(GpuProgramType gptype, BindableGpuParams& params) = 0;
  200. /** Unbinds GpuPrograms of a given GpuProgramType.
  201. @remarks
  202. This returns the pipeline to fixed-function processing for this type.
  203. */
  204. virtual void unbindGpuProgram(GpuProgramType gptype);
  205. /** Returns whether or not a Gpu program of the given type is currently bound. */
  206. virtual bool isGpuProgramBound(GpuProgramType gptype);
  207. /** Sets the user clipping region.
  208. */
  209. virtual void setClipPlanes(const PlaneList& clipPlanes);
  210. /** Add a user clipping plane. */
  211. virtual void addClipPlane (const Plane& p);
  212. /** Add a user clipping plane. */
  213. virtual void addClipPlane (float A, float B, float C, float D);
  214. /** Clears the user clipping region.
  215. */
  216. virtual void resetClipPlanes();
  217. /** Sets the 'scissor region' ie the region of the target in which rendering can take place.
  218. @remarks
  219. This method allows you to 'mask off' rendering in all but a given rectangular area
  220. as identified by the parameters to this method.
  221. @note
  222. Not all systems support this method. Check the RenderSystemCapabilities for the
  223. RSC_SCISSOR_TEST capability to see if it is supported.
  224. @param left, top, right, bottom The location of the corners of the rectangle, expressed in
  225. <i>pixels</i>.
  226. */
  227. virtual void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom) = 0;
  228. /**
  229. * @brief Clears the currently active render target.
  230. *
  231. * @param buffers Combination of one or more elements of FrameBufferType
  232. * denoting which buffers are to be cleared.
  233. * @param color (optional) The color to clear the color buffer with, if enabled.
  234. * @param depth (optional) The value to initialise the depth buffer with, if enabled.
  235. * @param stencil (optional) The value to initialise the stencil buffer with, if enabled.
  236. */
  237. virtual void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0) = 0;
  238. /**
  239. * @brief Clears the currently active viewport (i.e. it clears just a sub-area of a render-target that is covered by the viewport,
  240. * as opposed to clearRenderTarget which always clears the entire render target).
  241. *
  242. * @param buffers Combination of one or more elements of FrameBufferType
  243. * denoting which buffers are to be cleared.
  244. * @param color (optional) The color to clear the color buffer with, if enabled.
  245. * @param depth (optional) The value to initialise the depth buffer with, if enabled.
  246. * @param stencil (optional) The value to initialise the stencil buffer with, if enabled.
  247. */
  248. virtual void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0) = 0;
  249. /**
  250. * Set current render target to target, enabling its device context if needed
  251. */
  252. virtual void setRenderTarget(RenderTargetPtr target) = 0;
  253. /**
  254. * @brief Updates the resource with the specified data.
  255. */
  256. void writeSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceDataPtr& data, bool discardEntireBuffer, AsyncOp& asyncOp);
  257. /**
  258. * @brief Reads data from a resource into a pre-allocated GpuResourceData instance.
  259. */
  260. void readSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, GpuResourceDataPtr& data, AsyncOp& asyncOp);
  261. /************************************************************************/
  262. /* UTILITY METHODS */
  263. /************************************************************************/
  264. /** Get the native VertexElementType for a compact 32-bit colour value
  265. for this rendersystem.
  266. */
  267. virtual VertexElementType getColorVertexElementType(void) const = 0;
  268. /** Converts a uniform projection matrix to suitable for this render system.
  269. @remarks
  270. Because different APIs have different requirements (some incompatible) for the
  271. projection matrix, this method allows each to implement their own correctly and pass
  272. back a generic Camelot matrix for storage in the engine.
  273. */
  274. virtual void convertProjectionMatrix(const Matrix4& matrix,
  275. Matrix4& dest, bool forGpuProgram = false) = 0;
  276. /** Returns the horizontal texel offset value required for mapping
  277. texel origins to pixel origins in this rendersystem.
  278. @remarks
  279. Since rendersystems sometimes disagree on the origin of a texel,
  280. mapping from texels to pixels can sometimes be problematic to
  281. implement generically. This method allows you to retrieve the offset
  282. required to map the origin of a texel to the origin of a pixel in
  283. the horizontal direction.
  284. */
  285. virtual float getHorizontalTexelOffset(void) = 0;
  286. /** Returns the vertical texel offset value required for mapping
  287. texel origins to pixel origins in this rendersystem.
  288. @remarks
  289. Since rendersystems sometimes disagree on the origin of a texel,
  290. mapping from texels to pixels can sometimes be problematic to
  291. implement generically. This method allows you to retrieve the offset
  292. required to map the origin of a texel to the origin of a pixel in
  293. the vertical direction.
  294. */
  295. virtual float getVerticalTexelOffset(void) = 0;
  296. /** Gets the minimum (closest) depth value to be used when rendering
  297. using identity transforms.
  298. @remarks
  299. When using identity transforms you can manually set the depth
  300. of a vertex; however the input values required differ per
  301. rendersystem. This method lets you retrieve the correct value.
  302. @see Renderable::getUseIdentityView, Renderable::getUseIdentityProjection
  303. */
  304. virtual float getMinimumDepthInputValue(void) = 0;
  305. /** Gets the maximum (farthest) depth value to be used when rendering
  306. using identity transforms.
  307. @remarks
  308. When using identity transforms you can manually set the depth
  309. of a vertex; however the input values required differ per
  310. rendersystem. This method lets you retrieve the correct value.
  311. @see Renderable::getUseIdentityView, Renderable::getUseIdentityProjection
  312. */
  313. virtual float getMaximumDepthInputValue(void) = 0;
  314. /************************************************************************/
  315. /* INTERNAL DATA & METHODS */
  316. /************************************************************************/
  317. protected:
  318. friend class RenderSystemManager;
  319. /** The Active render target. */
  320. RenderTargetPtr mActiveRenderTarget;
  321. CullingMode mCullingMode;
  322. /// Texture units from this upwards are disabled
  323. UINT16 mDisabledTexUnitsFrom;
  324. bool mVertexProgramBound;
  325. bool mGeometryProgramBound;
  326. bool mFragmentProgramBound;
  327. // Recording user clip planes
  328. PlaneList mClipPlanes;
  329. // Indicator that we need to re-set the clip planes on next render call
  330. bool mClipPlanesDirty;
  331. /// Used to store the capabilities of the graphics card
  332. RenderSystemCapabilities* mCurrentCapabilities;
  333. // TODO - Only used between initialize and initialize_internal. Handle it better?
  334. RENDER_WINDOW_DESC mPrimaryWindowDesc;
  335. /**
  336. * @brief Initializes the render system and creates a primary render window.
  337. *
  338. * @note Although I'd like otherwise, due to the nature of some render system implementations,
  339. * you cannot initialize the render system without a window.
  340. */
  341. RenderWindowPtr initialize(const RENDER_WINDOW_DESC& primaryWindowDesc);
  342. virtual void initialize_internal(AsyncOp& asyncOp);
  343. virtual void destroy_internal();
  344. /// Internal method used to set the underlying clip planes when needed
  345. virtual void setClipPlanesImpl(const PlaneList& clipPlanes) = 0;
  346. /** Query the real capabilities of the GPU and driver in the RenderSystem*/
  347. virtual RenderSystemCapabilities* createRenderSystemCapabilities() const = 0;
  348. /** Initialize the render system from the capabilities*/
  349. virtual void initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps) = 0;
  350. /** Returns a description of an error code.
  351. */
  352. virtual String getErrorDescription(long errorNumber) const = 0;
  353. DriverVersion mDriverVersion;
  354. };
  355. /** @} */
  356. /** @} */
  357. }
  358. #endif