CmRenderSystem.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 "CmRenderOperation.h"
  34. #include "CmRenderSystemCapabilities.h"
  35. #include "CmRenderTarget.h"
  36. #include "CmRenderTexture.h"
  37. #include "CmRenderWindow.h"
  38. #include "CmGpuProgram.h"
  39. #include "CmPlane.h"
  40. #include "CmModule.h"
  41. #include "boost/function.hpp"
  42. #include "boost/signal.hpp"
  43. namespace CamelotFramework
  44. {
  45. /** \addtogroup Core
  46. * @{
  47. */
  48. /** \addtogroup RenderSystem
  49. * @{
  50. */
  51. typedef Multimap<UINT8, RenderTarget * >::type RenderTargetPriorityMap;
  52. class TextureManager;
  53. /** Defines the functionality of a 3D API
  54. @remarks
  55. The RenderSystem class provides a base interface
  56. which abstracts the general functionality of the 3D API
  57. e.g. Direct3D or OpenGL. Whilst a few of the general
  58. methods have implementations, most of this class is
  59. abstract, requiring a subclass based on a specific API
  60. to be constructed to provide the full functionality.
  61. @author
  62. Steven Streeting
  63. @version
  64. 1.0
  65. */
  66. class CM_EXPORT RenderSystem : public Module<RenderSystem>
  67. {
  68. public:
  69. /** Default Constructor.
  70. */
  71. RenderSystem();
  72. /** Destructor.
  73. */
  74. virtual ~RenderSystem();
  75. /** Returns the name of the rendering system.
  76. */
  77. virtual const String& getName(void) const = 0;
  78. /**
  79. * @brief Gets the name of the primary shading language.
  80. */
  81. virtual const String& getShadingLanguageName() const = 0;
  82. /**
  83. * @brief Sets a sampler state for the specified texture unit.
  84. * @see SamplerState
  85. */
  86. virtual void setSamplerState(GpuProgramType gptype, UINT16 texUnit, const SamplerStatePtr& samplerState) = 0;
  87. /** Turns off a texture unit. */
  88. virtual void disableTextureUnit(GpuProgramType gptype, UINT16 texUnit);
  89. /**
  90. * @brief Sets a blend state used for all active render targets.
  91. * @see BlendState
  92. */
  93. virtual void setBlendState(const BlendStatePtr& blendState) = 0;
  94. /**
  95. * @brief Sets a state that controls various rasterizer options.
  96. * @see RasterizerState
  97. */
  98. virtual void setRasterizerState(const RasterizerStatePtr& rasterizerState) = 0;
  99. /**
  100. * @brief Sets a state that controls depth & stencil buffer options.
  101. * @see DepthStencilState
  102. */
  103. virtual void setDepthStencilState(const DepthStencilStatePtr& depthStencilState, UINT32 stencilRefValue) = 0;
  104. /**
  105. Sets the texture to bind to a given texture unit.
  106. User processes would not normally call this direct unless rendering
  107. primitives themselves.
  108. @param unit The index of the texture unit to modify. Multitexturing
  109. hardware can support multiple units (see
  110. RenderSystemCapabilites::getNumTextureUnits)
  111. @param enabled Boolean to turn the unit on/off
  112. @param texPtr Pointer to the texture to use.
  113. */
  114. virtual void setTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const TexturePtr &texPtr) = 0;
  115. /**
  116. * Signifies the beginning of a frame, i.e. the start of rendering on a single viewport. Will occur
  117. * several times per complete frame if multiple viewports exist.
  118. */
  119. virtual void beginFrame(void) = 0;
  120. /**
  121. * Ends rendering of a frame to the current viewport.
  122. */
  123. virtual void endFrame(void) = 0;
  124. /**
  125. Sets the provided viewport as the active one for future
  126. rendering operations. This viewport is aware of it's own
  127. camera and render target. Must be implemented by subclass.
  128. @param target Viewport to render to.
  129. */
  130. virtual void setViewport(ViewportPtr& vp) = 0;
  131. /** Sets the current vertex buffer for the specified source index.
  132. /** @note Set buffer to nullptr to clear the buffer at the specified index.*/
  133. virtual void setVertexBuffer(UINT32 index, const VertexBufferPtr& buffer) = 0;
  134. /**
  135. * @brief Sets an index buffer to use when drawing. Indices in an index buffer
  136. * reference vertices in the vertex buffer, which increases cache coherency
  137. * and reduces the size of vertex buffers by eliminating duplicate data.
  138. */
  139. virtual void setIndexBuffer(const IndexBufferPtr& buffer) = 0;
  140. /**
  141. * @brief Sets the vertex declaration to use when drawing. Vertex declaration
  142. * is used to decode contents of a single vertex in a vertex buffer.
  143. */
  144. virtual void setVertexDeclaration(VertexDeclarationPtr vertexDeclaration) = 0;
  145. /**
  146. * @brief Sets the draw operation that determines how to interpret the elements
  147. * of the index or vertex buffers.
  148. */
  149. virtual void setDrawOperation(DrawOperationType op) = 0;
  150. /**
  151. * @brief A helper method that provides a simple way of rendering a single object.
  152. * It will automatically set up vertex declaration, draw operation,
  153. * vertex and index buffers and draw them.
  154. */
  155. virtual void render(const RenderOperation& op);
  156. /**
  157. * @brief Draw an object based on currently set
  158. * shaders, vertex declaration index buffers.
  159. *
  160. * Draws directly from the vertex buffer without using
  161. * indices.
  162. */
  163. virtual void draw(UINT32 vertexCount) = 0;
  164. /**
  165. * @brief Draw an object based on currently set
  166. * shaders, vertex declaration and vertex
  167. * and index buffers.
  168. */
  169. virtual void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexCount) = 0;
  170. /**
  171. * @brief Swap the front and back buffer of the specified render target.
  172. */
  173. virtual void swapBuffers(RenderTargetPtr target);
  174. /** Gets the capabilities of the render system. */
  175. const RenderSystemCapabilities* getCapabilities(void) const;
  176. /** Returns the driver version.
  177. */
  178. virtual const DriverVersion& getDriverVersion(void) const;
  179. /** Binds a given GpuProgram (but not the parameters).
  180. @remarks Only one GpuProgram of each type can be bound at once, binding another
  181. one will simply replace the existing one.
  182. */
  183. virtual void bindGpuProgram(HGpuProgram prg);
  184. /** Bind Gpu program parameters.
  185. @param gptype The type of program to bind the parameters to
  186. @param params The parameters to bind
  187. */
  188. virtual void bindGpuParams(GpuProgramType gptype, BindableGpuParams& params) = 0;
  189. /** Unbinds GpuPrograms of a given GpuProgramType.
  190. @remarks
  191. This returns the pipeline to fixed-function processing for this type.
  192. */
  193. virtual void unbindGpuProgram(GpuProgramType gptype);
  194. /** Returns whether or not a Gpu program of the given type is currently bound. */
  195. virtual bool isGpuProgramBound(GpuProgramType gptype);
  196. /** Sets the user clipping region.
  197. */
  198. virtual void setClipPlanes(const PlaneList& clipPlanes);
  199. /** Add a user clipping plane. */
  200. virtual void addClipPlane (const Plane& p);
  201. /** Add a user clipping plane. */
  202. virtual void addClipPlane (float A, float B, float C, float D);
  203. /** Clears the user clipping region.
  204. */
  205. virtual void resetClipPlanes();
  206. /** Sets the 'scissor region' ie the region of the target in which rendering can take place.
  207. @remarks
  208. This method allows you to 'mask off' rendering in all but a given rectangular area
  209. as identified by the parameters to this method.
  210. @note
  211. Not all systems support this method. Check the RenderSystemCapabilities for the
  212. RSC_SCISSOR_TEST capability to see if it is supported.
  213. @param left, top, right, bottom The location of the corners of the rectangle, expressed in
  214. <i>pixels</i>.
  215. */
  216. virtual void setScissorRect(UINT32 left = 0, UINT32 top = 0, UINT32 right = 800, UINT32 bottom = 600) = 0;
  217. /** Clears the provided render target to the specified values
  218. @param renderTarget Render target to clear. Entire surface will be cleared
  219. regardless of viewport or scissor rect.
  220. @param buffers Combination of one or more elements of FrameBufferType
  221. denoting which buffers are to be cleared
  222. @param colour The colour to clear the colour buffer with, if enabled
  223. @param depth The value to initialise the depth buffer with, if enabled
  224. @param stencil The value to initialise the stencil buffer with, if enabled.
  225. */
  226. virtual void clear(RenderTargetPtr renderTarget, unsigned int buffers,
  227. const Color& color = Color::Black,
  228. float depth = 1.0f, unsigned short stencil = 0) = 0;
  229. /**
  230. * Set current render target to target, enabling its device context if needed
  231. */
  232. virtual void setRenderTarget(RenderTargetPtr target) = 0;
  233. /**
  234. * @brief Updates the resource with the specified data.
  235. */
  236. void writeSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceData& data, AsyncOp& asyncOp);
  237. /**
  238. * @brief Reads data from a resource into a pre-allocated GpuResourceData instance.
  239. */
  240. void readSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, GpuResourceData& data, AsyncOp& asyncOp);
  241. /************************************************************************/
  242. /* UTILITY METHODS */
  243. /************************************************************************/
  244. /** Get the native VertexElementType for a compact 32-bit colour value
  245. for this rendersystem.
  246. */
  247. virtual VertexElementType getColorVertexElementType(void) const = 0;
  248. /** Converts a uniform projection matrix to suitable for this render system.
  249. @remarks
  250. Because different APIs have different requirements (some incompatible) for the
  251. projection matrix, this method allows each to implement their own correctly and pass
  252. back a generic Camelot matrix for storage in the engine.
  253. */
  254. virtual void convertProjectionMatrix(const Matrix4& matrix,
  255. Matrix4& dest, bool forGpuProgram = false) = 0;
  256. /** Returns the horizontal texel offset value required for mapping
  257. texel origins to pixel origins in this rendersystem.
  258. @remarks
  259. Since rendersystems sometimes disagree on the origin of a texel,
  260. mapping from texels to pixels can sometimes be problematic to
  261. implement generically. This method allows you to retrieve the offset
  262. required to map the origin of a texel to the origin of a pixel in
  263. the horizontal direction.
  264. */
  265. virtual float getHorizontalTexelOffset(void) = 0;
  266. /** Returns the vertical texel offset value required for mapping
  267. texel origins to pixel origins in this rendersystem.
  268. @remarks
  269. Since rendersystems sometimes disagree on the origin of a texel,
  270. mapping from texels to pixels can sometimes be problematic to
  271. implement generically. This method allows you to retrieve the offset
  272. required to map the origin of a texel to the origin of a pixel in
  273. the vertical direction.
  274. */
  275. virtual float getVerticalTexelOffset(void) = 0;
  276. /** Gets the minimum (closest) depth value to be used when rendering
  277. using identity transforms.
  278. @remarks
  279. When using identity transforms you can manually set the depth
  280. of a vertex; however the input values required differ per
  281. rendersystem. This method lets you retrieve the correct value.
  282. @see Renderable::getUseIdentityView, Renderable::getUseIdentityProjection
  283. */
  284. virtual float getMinimumDepthInputValue(void) = 0;
  285. /** Gets the maximum (farthest) depth value to be used when rendering
  286. using identity transforms.
  287. @remarks
  288. When using identity transforms you can manually set the depth
  289. of a vertex; however the input values required differ per
  290. rendersystem. This method lets you retrieve the correct value.
  291. @see Renderable::getUseIdentityView, Renderable::getUseIdentityProjection
  292. */
  293. virtual float getMaximumDepthInputValue(void) = 0;
  294. /************************************************************************/
  295. /* INTERNAL DATA & METHODS */
  296. /************************************************************************/
  297. protected:
  298. friend class RenderSystemManager;
  299. /** The Active render target. */
  300. RenderTargetPtr mActiveRenderTarget;
  301. CullingMode mCullingMode;
  302. bool mInvertVertexWinding;
  303. /// Texture units from this upwards are disabled
  304. UINT16 mDisabledTexUnitsFrom;
  305. bool mVertexProgramBound;
  306. bool mGeometryProgramBound;
  307. bool mFragmentProgramBound;
  308. // Recording user clip planes
  309. PlaneList mClipPlanes;
  310. // Indicator that we need to re-set the clip planes on next render call
  311. bool mClipPlanesDirty;
  312. /// Used to store the capabilities of the graphics card
  313. RenderSystemCapabilities* mCurrentCapabilities;
  314. // TODO - Only used between initialize and initialize_internal. Handle it better?
  315. RENDER_WINDOW_DESC mPrimaryWindowDesc;
  316. /**
  317. * @brief Initializes the render system and creates a primary render window.
  318. *
  319. * @note Although I'd like otherwise, due to the nature of some render system implementations,
  320. * you cannot initialize the render system without a window.
  321. */
  322. RenderWindowPtr initialize(const RENDER_WINDOW_DESC& primaryWindowDesc);
  323. virtual void initialize_internal(AsyncOp& asyncOp);
  324. virtual void destroy_internal();
  325. /// Internal method used to set the underlying clip planes when needed
  326. virtual void setClipPlanesImpl(const PlaneList& clipPlanes) = 0;
  327. /** Query the real capabilities of the GPU and driver in the RenderSystem*/
  328. virtual RenderSystemCapabilities* createRenderSystemCapabilities() const = 0;
  329. /** Initialize the render system from the capabilities*/
  330. virtual void initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps) = 0;
  331. /** Returns a description of an error code.
  332. */
  333. virtual String getErrorDescription(long errorNumber) const = 0;
  334. DriverVersion mDriverVersion;
  335. };
  336. /** @} */
  337. /** @} */
  338. }
  339. #endif