CmRenderSystem.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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 "CmCommon.h"
  32. #include "CmRenderOperation.h"
  33. #include "CmRenderSystemCapabilities.h"
  34. #include "CmRenderTarget.h"
  35. #include "CmRenderTexture.h"
  36. #include "CmGpuProgram.h"
  37. #include "CmPlane.h"
  38. #include "boost/function.hpp"
  39. #include "boost/signal.hpp"
  40. namespace CamelotEngine
  41. {
  42. /** \addtogroup Core
  43. * @{
  44. */
  45. /** \addtogroup RenderSystem
  46. * @{
  47. */
  48. typedef multimap<UINT8, RenderTarget * >::type RenderTargetPriorityMap;
  49. class TextureManager;
  50. /** Defines the functionality of a 3D API
  51. @remarks
  52. The RenderSystem class provides a base interface
  53. which abstracts the general functionality of the 3D API
  54. e.g. Direct3D or OpenGL. Whilst a few of the general
  55. methods have implementations, most of this class is
  56. abstract, requiring a subclass based on a specific API
  57. to be constructed to provide the full functionality.
  58. Note there are 2 levels to the interface - one which
  59. will be used often by the caller of the Ogre library,
  60. and one which is at a lower level and will be used by the
  61. other classes provided by Ogre. These lower level
  62. methods are prefixed with '_' to differentiate them.
  63. The advanced user of the library may use these lower
  64. level methods to access the 3D API at a more fundamental
  65. level (dealing direct with render states and rendering
  66. primitives), but still benefiting from Ogre's abstraction
  67. of exactly which 3D API is in use.
  68. @author
  69. Steven Streeting
  70. @version
  71. 1.0
  72. */
  73. class CM_EXPORT RenderSystem
  74. {
  75. public:
  76. /** Default Constructor.
  77. */
  78. RenderSystem();
  79. /** Destructor.
  80. */
  81. virtual ~RenderSystem();
  82. /** Returns the name of the rendering system.
  83. */
  84. virtual const String& getName(void) const = 0;
  85. /* @brief Start up the RenderSystem. Call before doing any operations on the render system.
  86. * Make sure all subsequent calls to the RenderSystem are done from the same thread it was started on.
  87. *
  88. * @remark If you want to access the render system from other threads, call RenderSystem::createRenderContext,
  89. * set the active context using RenderSystem::setActiveRenderContext and call the render system normally.
  90. * By default an automatically created primary render context is used.
  91. */
  92. void startUp();
  93. // TODO - Classes below (shutdown to getErrorDescription) are not yet thread safe
  94. /** Shutdown the renderer and cleanup resources.
  95. */
  96. void shutdown(void);
  97. /** Creates a new rendering window.
  98. @remarks
  99. This method creates a new rendering window as specified
  100. by the paramteters. The rendering system could be
  101. responible for only a single window (e.g. in the case
  102. of a game), or could be in charge of multiple ones (in the
  103. case of a level editor). The option to create the window
  104. as a child of another is therefore given.
  105. This method will create an appropriate subclass of
  106. RenderWindow depending on the API and platform implementation.
  107. @par
  108. After creation, this window can be retrieved using getRenderTarget().
  109. @param
  110. name The name of the window. Used in other methods
  111. later like setRenderTarget and getRenderTarget.
  112. @param
  113. width The width of the new window.
  114. @param
  115. height The height of the new window.
  116. @param
  117. fullScreen Specify true to make the window full screen
  118. without borders, title bar or menu bar.
  119. @param
  120. miscParams A NameValuePairList describing the other parameters for the new rendering window.
  121. Options are case sensitive. Unrecognised parameters will be ignored silently.
  122. These values might be platform dependent, but these are present for all platforms unless
  123. indicated otherwise:
  124. <table>
  125. <tr>
  126. <td><b>Key</b></td>
  127. <td><b>Type/Values</b></td>
  128. <td><b>Default</b></td>
  129. <td><b>Description</b></td>
  130. <td><b>Notes</b></td>
  131. </tr>
  132. <tr>
  133. <td>title</td>
  134. <td>Any string</td>
  135. <td>RenderTarget name</td>
  136. <td>The title of the window that will appear in the title bar</td>
  137. <td>&nbsp;</td>
  138. </tr>
  139. <tr>
  140. <td>colourDepth</td>
  141. <td>16, 32</td>
  142. <td>Desktop depth</td>
  143. <td>Colour depth of the resulting rendering window; only applies if fullScreen</td>
  144. <td>Win32 Specific</td>
  145. </tr>
  146. <tr>
  147. <td>left</td>
  148. <td>Positive integers</td>
  149. <td>Centred</td>
  150. <td>Screen x coordinate from left</td>
  151. <td>&nbsp;</td>
  152. </tr>
  153. <tr>
  154. <td>top</td>
  155. <td>Positive integers</td>
  156. <td>Centred</td>
  157. <td>Screen y coordinate from left</td>
  158. <td>&nbsp;</td>
  159. </tr>
  160. <tr>
  161. <td>depthBuffer</td>
  162. <td>true, false</td>
  163. <td>true</td>
  164. <td>Use depth buffer</td>
  165. <td>DirectX9 specific</td>
  166. </tr>
  167. <tr>
  168. <td>externalWindowHandle</td>
  169. <td>Win32: HWND as integer<br/>
  170. GLX: poslong:posint:poslong (display*:screen:windowHandle) or poslong:posint:poslong:poslong (display*:screen:windowHandle:XVisualInfo*)</td>
  171. <td>0 (none)</td>
  172. <td>External window handle, for embedding the OGRE render in an existing window</td>
  173. <td>&nbsp;</td>
  174. </tr>
  175. <tr>
  176. <td>externalGLControl</td>
  177. <td>true, false</td>
  178. <td>false</td>
  179. <td>Let the external window control OpenGL i.e. don't select a pixel format for the window,
  180. do not change v-sync and do not swap buffer. When set to true, the calling application
  181. is responsible of OpenGL initialization and buffer swapping. It should also create an
  182. OpenGL context for its own rendering, Ogre will create one for its use. Then the calling
  183. application must also enable Ogre OpenGL context before calling any Ogre function and
  184. restore its OpenGL context after these calls.</td>
  185. <td>OpenGL specific</td>
  186. </tr>
  187. <tr>
  188. <td>externalGLContext</td>
  189. <td>Context as Unsigned Long</td>
  190. <td>0 (create own context)</td>
  191. <td>Use an externally created GL context</td>
  192. <td>OpenGL Specific</td>
  193. </tr>
  194. <tr>
  195. <td>parentWindowHandle</td>
  196. <td>Win32: HWND as integer<br/>
  197. GLX: poslong:posint:poslong (display*:screen:windowHandle) or poslong:posint:poslong:poslong (display*:screen:windowHandle:XVisualInfo*)</td>
  198. <td>0 (none)</td>
  199. <td>Parent window handle, for embedding the engine in a child of an external window</td>
  200. <td>&nbsp;</td>
  201. </tr>
  202. <tr>
  203. <td>macAPI</td>
  204. <td>String: "cocoa" or "carbon"</td>
  205. <td>"carbon"</td>
  206. <td>Specifies the type of rendering window on the Mac Platform.</td>
  207. <td>&nbsp;</td>
  208. </tr>
  209. <tr>
  210. <td>macAPICocoaUseNSView</td>
  211. <td>bool "true" or "false"</td>
  212. <td>"false"</td>
  213. <td>On the Mac platform the most diffused method to embed engine in a custom application is to use Interface Builder
  214. and add to the interface an instance of OgreView.
  215. The pointer to this instance is then used as "externalWindowHandle".
  216. However, there are cases where you are NOT using Interface Builder and you get the Cocoa NSView* of an existing interface.
  217. For example, this is happens when you want to render into a Java/AWT interface.
  218. In short, by setting this flag to "true" the Ogre::Root::createRenderWindow interprets the "externalWindowHandle" as a NSView*
  219. instead of an OgreView*. See OgreOSXCocoaView.h/mm.
  220. </td>
  221. <td>&nbsp;</td>
  222. </tr>
  223. <tr>
  224. <td>FSAA</td>
  225. <td>Positive integer (usually 0, 2, 4, 8, 16)</td>
  226. <td>0</td>
  227. <td>Full screen antialiasing factor</td>
  228. <td>&nbsp;</td>
  229. </tr>
  230. <tr>
  231. <td>FSAAHint</td>
  232. <td>Depends on RenderSystem and hardware. Currently supports:<br/>
  233. "Quality": on systems that have an option to prefer higher AA quality over speed, use it</td>
  234. <td>Blank</td>
  235. <td>Full screen antialiasing hint</td>
  236. <td>&nbsp;</td>
  237. </tr>
  238. <tr>
  239. <td>displayFrequency</td>
  240. <td>Refresh rate in Hertz (e.g. 60, 75, 100)</td>
  241. <td>Desktop vsync rate</td>
  242. <td>Display frequency rate, for fullscreen mode</td>
  243. <td>&nbsp;</td>
  244. </tr>
  245. <tr>
  246. <td>vsync</td>
  247. <td>true, false</td>
  248. <td>false</td>
  249. <td>Synchronize buffer swaps to monitor vsync, eliminating tearing at the expense of a fixed frame rate</td>
  250. <td>&nbsp;</td>
  251. </tr>
  252. <tr>
  253. <td>vsyncInterval</td>
  254. <td>1, 2, 3, 4</td>
  255. <td>1</td>
  256. <td>If vsync is enabled, the minimum number of vertical blanks that should occur between renders.
  257. For example if vsync is enabled, the refresh rate is 60 and this is set to 2, then the
  258. frame rate will be locked at 30.</td>
  259. <td>&nbsp;</td>
  260. </tr>
  261. <tr>
  262. <td>border</td>
  263. <td>none, fixed, resize</td>
  264. <td>resize</td>
  265. <td>The type of window border (in windowed mode)</td>
  266. <td>&nbsp;</td>
  267. </tr>
  268. <tr>
  269. <td>outerDimensions</td>
  270. <td>true, false</td>
  271. <td>false</td>
  272. <td>Whether the width/height is expressed as the size of the
  273. outer window, rather than the content area</td>
  274. <td>&nbsp;</td>
  275. </tr>
  276. <tr>
  277. <td>useNVPerfHUD</td>
  278. <td>true, false</td>
  279. <td>false</td>
  280. <td>Enable the use of nVidia NVPerfHUD</td>
  281. <td>&nbsp;</td>
  282. </tr>
  283. <tr>
  284. <td>gamma</td>
  285. <td>true, false</td>
  286. <td>false</td>
  287. <td>Enable hardware conversion from linear colour space to gamma
  288. colour space on rendering to the window.</td>
  289. <td>&nbsp;</td>
  290. </tr>
  291. */
  292. RenderWindow* createRenderWindow(const String &name, unsigned int width, unsigned int height,
  293. bool fullScreen, const NameValuePairList *miscParams = 0);
  294. virtual void createRenderWindow_internal(const String &name, unsigned int width, unsigned int height,
  295. bool fullScreen, const NameValuePairList& miscParams, AsyncOp& asyncOp) = 0;
  296. /** Attaches the passed render target to the render system.
  297. */
  298. virtual void attachRenderTarget(RenderTarget &target);
  299. /** Detaches the render target from the render system.
  300. @note
  301. If the render target cannot be found, NULL is returned.
  302. */
  303. virtual void detachRenderTarget(RenderTarget& renderTarget);
  304. /** Destroys a render window */
  305. virtual void destroyRenderWindow(RenderWindow* renderWindow);
  306. /** Destroys a render texture */
  307. virtual void destroyRenderTexture(RenderTexture* renderTexture);
  308. /** Destroys a render target of any sort */
  309. virtual void destroyRenderTarget(RenderTarget* renderTarget);
  310. /** Defines whether or now fullscreen render windows wait for the vertical blank before flipping buffers.
  311. @remarks
  312. By default, all rendering windows wait for a vertical blank (when the CRT beam turns off briefly to move
  313. from the bottom right of the screen back to the top left) before flipping the screen buffers. This ensures
  314. that the image you see on the screen is steady. However it restricts the frame rate to the refresh rate of
  315. the monitor, and can slow the frame rate down. You can speed this up by not waiting for the blank, but
  316. this has the downside of introducing 'tearing' artefacts where part of the previous frame is still displayed
  317. as the buffers are switched. Speed vs quality, you choose.
  318. @note
  319. Has NO effect on windowed mode render targets. Only affects fullscreen mode.
  320. @param
  321. enabled If true, the system waits for vertical blanks - quality over speed. If false it doesn't - speed over quality.
  322. */
  323. void setWaitForVerticalBlank(bool enabled);
  324. /** Returns true if the system is synchronising frames with the monitor vertical blank.
  325. */
  326. bool getWaitForVerticalBlank(void) const;
  327. // ------------------------------------------------------------------------
  328. // Internal Rendering Access
  329. // All methods below here are normally only called by other Camelot classes
  330. // They can be called by library user if required
  331. // ------------------------------------------------------------------------
  332. /**
  333. * @brief Sets a sampler state for the specified texture unit.
  334. */
  335. virtual void setSamplerState(UINT16 texUnit, const SamplerState& samplerState) = 0;
  336. /** Turns off a texture unit. */
  337. virtual void disableTextureUnit(UINT16 texUnit);
  338. /** Disables all texture units from the given unit upwards */
  339. virtual void disableTextureUnitsFrom(UINT16 texUnit);
  340. /** Sets the size of points and how they are attenuated with distance.
  341. @remarks
  342. When performing point rendering or point sprite rendering,
  343. point size can be attenuated with distance. The equation for
  344. doing this is attenuation = 1 / (constant + linear * dist + quadratic * d^2) .
  345. @par
  346. For example, to disable distance attenuation (constant screensize)
  347. you would set constant to 1, and linear and quadratic to 0. A
  348. standard perspective attenuation would be 0, 1, 0 respectively.
  349. */
  350. virtual void setPointParameters(float size, bool attenuationEnabled,
  351. float constant, float linear, float quadratic, float minSize, float maxSize) = 0;
  352. /**
  353. Sets the texture to bind to a given texture unit.
  354. User processes would not normally call this direct unless rendering
  355. primitives themselves.
  356. @param unit The index of the texture unit to modify. Multitexturing
  357. hardware can support multiple units (see
  358. RenderSystemCapabilites::getNumTextureUnits)
  359. @param enabled Boolean to turn the unit on/off
  360. @param texPtr Pointer to the texture to use.
  361. */
  362. virtual void setTexture(UINT16 unit, bool enabled,
  363. const TexturePtr &texPtr) = 0;
  364. /** Sets the global blending factors for combining subsequent renders with the existing frame contents.
  365. The result of the blending operation is:</p>
  366. <p align="center">final = (texture * sourceFactor) + (pixel * destFactor)</p>
  367. Each of the factors is specified as one of a number of options, as specified in the SceneBlendFactor
  368. enumerated type.
  369. By changing the operation you can change addition between the source and destination pixels to a different operator.
  370. @param sourceFactor The source factor in the above calculation, i.e. multiplied by the texture colour components.
  371. @param destFactor The destination factor in the above calculation, i.e. multiplied by the pixel colour components.
  372. @param op The blend operation mode for combining pixels
  373. */
  374. virtual void setSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op = SBO_ADD) = 0;
  375. /** Sets the global blending factors for combining subsequent renders with the existing frame contents.
  376. The result of the blending operation is:</p>
  377. <p align="center">final = (texture * sourceFactor) + (pixel * destFactor)</p>
  378. Each of the factors is specified as one of a number of options, as specified in the SceneBlendFactor
  379. enumerated type.
  380. @param sourceFactor The source factor in the above calculation, i.e. multiplied by the texture colour components.
  381. @param destFactor The destination factor in the above calculation, i.e. multiplied by the pixel colour components.
  382. @param sourceFactorAlpha The source factor in the above calculation for the alpha channel, i.e. multiplied by the texture alpha components.
  383. @param destFactorAlpha The destination factor in the above calculation for the alpha channel, i.e. multiplied by the pixel alpha components.
  384. @param op The blend operation mode for combining pixels
  385. @param alphaOp The blend operation mode for combining pixel alpha values
  386. */
  387. virtual void setSeparateSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha,
  388. SceneBlendFactor destFactorAlpha, SceneBlendOperation op = SBO_ADD, SceneBlendOperation alphaOp = SBO_ADD) = 0;
  389. /** Sets the global alpha rejection approach for future renders.
  390. By default images are rendered regardless of texture alpha. This method lets you change that.
  391. @param func The comparison function which must pass for a pixel to be written.
  392. @param val The value to compare each pixels alpha value to (0-255)
  393. @param alphaToCoverage Whether to enable alpha to coverage, if supported
  394. */
  395. virtual void setAlphaRejectSettings(CompareFunction func, unsigned char value, bool alphaToCoverage) = 0;
  396. /**
  397. * Signifies the beginning of a frame, i.e. the start of rendering on a single viewport. Will occur
  398. * several times per complete frame if multiple viewports exist.
  399. */
  400. virtual void beginFrame(void) = 0;
  401. /**
  402. * Ends rendering of a frame to the current viewport.
  403. */
  404. virtual void endFrame(void) = 0;
  405. /**
  406. Sets the provided viewport as the active one for future
  407. rendering operations. This viewport is aware of it's own
  408. camera and render target. Must be implemented by subclass.
  409. @param target Viewport to render to.
  410. */
  411. virtual void setViewport(const Viewport& vp) = 0;
  412. /** Get the current active viewport for rendering. */
  413. virtual Viewport getViewport(void);
  414. /** Sets the culling mode for the render system based on the 'vertex winding'.
  415. A typical way for the rendering engine to cull triangles is based on the
  416. 'vertex winding' of triangles. Vertex winding refers to the direction in
  417. which the vertices are passed or indexed to in the rendering operation as viewed
  418. from the camera, and will wither be clockwise or anticlockwise (that's 'counterclockwise' for
  419. you Americans out there ;) The default is CULL_CLOCKWISE i.e. that only triangles whose vertices
  420. are passed/indexed in anticlockwise order are rendered - this is a common approach and is used in 3D studio models
  421. for example. You can alter this culling mode if you wish but it is not advised unless you know what you are doing.
  422. You may wish to use the CULL_NONE option for mesh data that you cull yourself where the vertex
  423. winding is uncertain.
  424. */
  425. virtual void setCullingMode(CullingMode mode) = 0;
  426. virtual CullingMode getCullingMode(void) const;
  427. /** Sets the mode of operation for depth buffer tests from this point onwards.
  428. Sometimes you may wish to alter the behaviour of the depth buffer to achieve
  429. special effects. Because it's unlikely that you'll set these options for an entire frame,
  430. but rather use them to tweak settings between rendering objects, this is an internal
  431. method (indicated by the '_' prefix) which will be used by a SceneManager implementation
  432. rather than directly from the client application.
  433. If this method is never called the settings are automatically the same as the default parameters.
  434. @param depthTest If true, the depth buffer is tested for each pixel and the frame buffer is only updated
  435. if the depth function test succeeds. If false, no test is performed and pixels are always written.
  436. @param depthWrite If true, the depth buffer is updated with the depth of the new pixel if the depth test succeeds.
  437. If false, the depth buffer is left unchanged even if a new pixel is written.
  438. @param depthFunction Sets the function required for the depth test.
  439. */
  440. virtual void setDepthBufferParams(bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL) = 0;
  441. /** Sets whether or not the depth buffer check is performed before a pixel write.
  442. @param enabled If true, the depth buffer is tested for each pixel and the frame buffer is only updated
  443. if the depth function test succeeds. If false, no test is performed and pixels are always written.
  444. */
  445. virtual void setDepthBufferCheckEnabled(bool enabled = true) = 0;
  446. /** Sets whether or not the depth buffer is updated after a pixel write.
  447. @param enabled If true, the depth buffer is updated with the depth of the new pixel if the depth test succeeds.
  448. If false, the depth buffer is left unchanged even if a new pixel is written.
  449. */
  450. virtual void setDepthBufferWriteEnabled(bool enabled = true) = 0;
  451. /** Sets the comparison function for the depth buffer check.
  452. Advanced use only - allows you to choose the function applied to compare the depth values of
  453. new and existing pixels in the depth buffer. Only an issue if the deoth buffer check is enabled
  454. (see _setDepthBufferCheckEnabled)
  455. @param func The comparison between the new depth and the existing depth which must return true
  456. for the new pixel to be written.
  457. */
  458. virtual void setDepthBufferFunction(CompareFunction func = CMPF_LESS_EQUAL) = 0;
  459. /** Sets whether or not colour buffer writing is enabled, and for which channels.
  460. @remarks
  461. For some advanced effects, you may wish to turn off the writing of certain colour
  462. channels, or even all of the colour channels so that only the depth buffer is updated
  463. in a rendering pass. However, the chances are that you really want to use this option
  464. through the Material class.
  465. @param red, green, blue, alpha Whether writing is enabled for each of the 4 colour channels. */
  466. virtual void setColorBufferWriteEnabled(bool red, bool green, bool blue, bool alpha) = 0;
  467. /** Sets the depth bias, NB you should use the Material version of this.
  468. @remarks
  469. When polygons are coplanar, you can get problems with 'depth fighting' where
  470. the pixels from the two polys compete for the same screen pixel. This is particularly
  471. a problem for decals (polys attached to another surface to represent details such as
  472. bulletholes etc.).
  473. @par
  474. A way to combat this problem is to use a depth bias to adjust the depth buffer value
  475. used for the decal such that it is slightly higher than the true value, ensuring that
  476. the decal appears on top.
  477. @note
  478. The final bias value is a combination of a constant bias and a bias proportional
  479. to the maximum depth slope of the polygon being rendered. The final bias
  480. is constantBias + slopeScaleBias * maxslope. Slope scale biasing is
  481. generally preferable but is not available on older hardware.
  482. @param constantBias The constant bias value, expressed as a value in
  483. homogeneous depth coordinates.
  484. @param slopeScaleBias The bias value which is factored by the maximum slope
  485. of the polygon, see the description above. This is not supported by all
  486. cards.
  487. */
  488. virtual void setDepthBias(float constantBias, float slopeScaleBias = 0.0f) = 0;
  489. /** Sets how to rasterise triangles, as points, wireframe or solid polys. */
  490. virtual void setPolygonMode(PolygonMode level) = 0;
  491. /** Turns stencil buffer checking on or off.
  492. @remarks
  493. Stencilling (masking off areas of the rendering target based on the stencil
  494. buffer) can be turned on or off using this method. By default, stencilling is
  495. disabled.
  496. */
  497. virtual void setStencilCheckEnabled(bool enabled) = 0;
  498. /** This method allows you to set all the stencil buffer parameters in one call.
  499. @remarks
  500. The stencil buffer is used to mask out pixels in the render target, allowing
  501. you to do effects like mirrors, cut-outs, stencil shadows and more. Each of
  502. your batches of rendering is likely to ignore the stencil buffer,
  503. update it with new values, or apply it to mask the output of the render.
  504. The stencil test is:<PRE>
  505. (Reference Value & Mask) CompareFunction (Stencil Buffer Value & Mask)</PRE>
  506. The result of this will cause one of 3 actions depending on whether the test fails,
  507. succeeds but with the depth buffer check still failing, or succeeds with the
  508. depth buffer check passing too.
  509. @par
  510. Unlike other render states, stencilling is left for the application to turn
  511. on and off when it requires. This is because you are likely to want to change
  512. parameters between batches of arbitrary objects and control the ordering yourself.
  513. In order to batch things this way, you'll want to use OGRE's separate render queue
  514. groups (see RenderQueue) and register a RenderQueueListener to get notifications
  515. between batches.
  516. @par
  517. There are individual state change methods for each of the parameters set using
  518. this method.
  519. Note that the default values in this method represent the defaults at system
  520. start up too.
  521. @param func The comparison function applied.
  522. @param refValue The reference value used in the comparison
  523. @param mask The bitmask applied to both the stencil value and the reference value
  524. before comparison
  525. @param stencilFailOp The action to perform when the stencil check fails
  526. @param depthFailOp The action to perform when the stencil check passes, but the
  527. depth buffer check still fails
  528. @param passOp The action to take when both the stencil and depth check pass.
  529. @param twoSidedOperation If set to true, then if you render both back and front faces
  530. (you'll have to turn off culling) then these parameters will apply for front faces,
  531. and the inverse of them will happen for back faces (keep remains the same).
  532. */
  533. virtual void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS,
  534. UINT32 refValue = 0, UINT32 mask = 0xFFFFFFFF,
  535. StencilOperation stencilFailOp = SOP_KEEP,
  536. StencilOperation depthFailOp = SOP_KEEP,
  537. StencilOperation passOp = SOP_KEEP,
  538. bool twoSidedOperation = false) = 0;
  539. /** Sets the current vertex declaration, ie the source of vertex data. */
  540. virtual void setVertexDeclaration(VertexDeclarationPtr decl) = 0;
  541. /** Sets the current vertex buffer binding state. */
  542. virtual void setVertexBufferBinding(VertexBufferBinding* binding) = 0;
  543. /**
  544. Render something to the active viewport.
  545. Low-level rendering interface to perform rendering
  546. operations. Unlikely to be used directly by client
  547. applications, since the SceneManager and various support
  548. classes will be responsible for calling this method.
  549. Can only be called between _beginScene and _endScene
  550. @param op A rendering operation instance, which contains
  551. details of the operation to be performed.
  552. */
  553. virtual void render(const RenderOperation& op);
  554. /** Gets the capabilities of the render system. */
  555. const RenderSystemCapabilities* getCapabilities(void) const;
  556. /** Returns the driver version.
  557. */
  558. virtual const DriverVersion& getDriverVersion(void) const;
  559. /** Binds a given GpuProgram (but not the parameters).
  560. @remarks Only one GpuProgram of each type can be bound at once, binding another
  561. one will simply replace the existing one.
  562. */
  563. virtual void bindGpuProgram(GpuProgramHandle prg);
  564. /** Bind Gpu program parameters.
  565. @param gptype The type of program to bind the parameters to
  566. @param params The parameters to bind
  567. @param variabilityMask A mask of GpuParamVariability identifying which params need binding
  568. */
  569. virtual void bindGpuProgramParameters(GpuProgramType gptype,
  570. GpuProgramParametersSharedPtr params, UINT16 variabilityMask) = 0;
  571. /** Unbinds GpuPrograms of a given GpuProgramType.
  572. @remarks
  573. This returns the pipeline to fixed-function processing for this type.
  574. */
  575. virtual void unbindGpuProgram(GpuProgramType gptype);
  576. /** Returns whether or not a Gpu program of the given type is currently bound. */
  577. virtual bool isGpuProgramBound(GpuProgramType gptype);
  578. /** Sets the user clipping region.
  579. */
  580. virtual void setClipPlanes(const PlaneList& clipPlanes);
  581. /** Add a user clipping plane. */
  582. virtual void addClipPlane (const Plane& p);
  583. /** Add a user clipping plane. */
  584. virtual void addClipPlane (float A, float B, float C, float D);
  585. /** Clears the user clipping region.
  586. */
  587. virtual void resetClipPlanes();
  588. /** Internal method for swapping all the buffers on all render targets,
  589. if _updateAllRenderTargets was called with a 'false' parameter. */
  590. virtual void swapAllRenderTargetBuffers(bool waitForVsync = true);
  591. /** Sets whether or not vertex windings set should be inverted; this can be important
  592. for rendering reflections. */
  593. virtual void setInvertVertexWinding_(bool invert);
  594. /** Indicates whether or not the vertex windings set will be inverted for the current render (e.g. reflections)
  595. @see RenderSystem::setInvertVertexWinding
  596. */
  597. virtual bool getInvertVertexWinding(void) const;
  598. /** Sets the 'scissor region' ie the region of the target in which rendering can take place.
  599. @remarks
  600. This method allows you to 'mask off' rendering in all but a given rectangular area
  601. as identified by the parameters to this method.
  602. @note
  603. Not all systems support this method. Check the RenderSystemCapabilities for the
  604. RSC_SCISSOR_TEST capability to see if it is supported.
  605. @param enabled True to enable the scissor test, false to disable it.
  606. @param left, top, right, bottom The location of the corners of the rectangle, expressed in
  607. <i>pixels</i>.
  608. */
  609. virtual void setScissorTest(bool enabled, UINT32 left = 0, UINT32 top = 0,
  610. UINT32 right = 800, UINT32 bottom = 600) = 0;
  611. /** Clears one or more frame buffers on the active render target.
  612. @param buffers Combination of one or more elements of FrameBufferType
  613. denoting which buffers are to be cleared
  614. @param colour The colour to clear the colour buffer with, if enabled
  615. @param depth The value to initialise the depth buffer with, if enabled
  616. @param stencil The value to initialise the stencil buffer with, if enabled.
  617. */
  618. virtual void clearFrameBuffer(unsigned int buffers,
  619. const Color& color = Color::Black,
  620. float depth = 1.0f, unsigned short stencil = 0) = 0;
  621. /**
  622. * Set current render target to target, enabling its device context if needed
  623. */
  624. virtual void setRenderTarget(RenderTarget *target) = 0;
  625. /************************************************************************/
  626. /* UTILITY METHODS */
  627. /************************************************************************/
  628. /** Get the native VertexElementType for a compact 32-bit colour value
  629. for this rendersystem.
  630. */
  631. virtual VertexElementType getColorVertexElementType(void) const = 0;
  632. /** Converts a uniform projection matrix to suitable for this render system.
  633. @remarks
  634. Because different APIs have different requirements (some incompatible) for the
  635. projection matrix, this method allows each to implement their own correctly and pass
  636. back a generic Camelot matrix for storage in the engine.
  637. */
  638. virtual void convertProjectionMatrix(const Matrix4& matrix,
  639. Matrix4& dest, bool forGpuProgram = false) = 0;
  640. /** Returns the horizontal texel offset value required for mapping
  641. texel origins to pixel origins in this rendersystem.
  642. @remarks
  643. Since rendersystems sometimes disagree on the origin of a texel,
  644. mapping from texels to pixels can sometimes be problematic to
  645. implement generically. This method allows you to retrieve the offset
  646. required to map the origin of a texel to the origin of a pixel in
  647. the horizontal direction.
  648. */
  649. virtual float getHorizontalTexelOffset(void) = 0;
  650. /** Returns the vertical texel offset value required for mapping
  651. texel origins to pixel origins in this rendersystem.
  652. @remarks
  653. Since rendersystems sometimes disagree on the origin of a texel,
  654. mapping from texels to pixels can sometimes be problematic to
  655. implement generically. This method allows you to retrieve the offset
  656. required to map the origin of a texel to the origin of a pixel in
  657. the vertical direction.
  658. */
  659. virtual float getVerticalTexelOffset(void) = 0;
  660. /** Gets the minimum (closest) depth value to be used when rendering
  661. using identity transforms.
  662. @remarks
  663. When using identity transforms you can manually set the depth
  664. of a vertex; however the input values required differ per
  665. rendersystem. This method lets you retrieve the correct value.
  666. @see Renderable::getUseIdentityView, Renderable::getUseIdentityProjection
  667. */
  668. virtual float getMinimumDepthInputValue(void) = 0;
  669. /** Gets the maximum (farthest) depth value to be used when rendering
  670. using identity transforms.
  671. @remarks
  672. When using identity transforms you can manually set the depth
  673. of a vertex; however the input values required differ per
  674. rendersystem. This method lets you retrieve the correct value.
  675. @see Renderable::getUseIdentityView, Renderable::getUseIdentityProjection
  676. */
  677. virtual float getMaximumDepthInputValue(void) = 0;
  678. /************************************************************************/
  679. /* INTERNAL DATA & METHODS */
  680. /************************************************************************/
  681. protected:
  682. /** The render targets. */
  683. vector<RenderTarget*>::type mRenderTargets;
  684. /** The render targets, ordered by priority. */
  685. RenderTargetPriorityMap mPrioritisedRenderTargets;
  686. /** The Active render target. */
  687. RenderTarget * mActiveRenderTarget;
  688. /** The Active GPU programs and gpu program parameters*/
  689. GpuProgramParametersSharedPtr mActiveVertexGpuProgramParameters;
  690. GpuProgramParametersSharedPtr mActiveGeometryGpuProgramParameters;
  691. GpuProgramParametersSharedPtr mActiveFragmentGpuProgramParameters;
  692. // Active viewport (dest for future rendering operations)
  693. Viewport mActiveViewport;
  694. CullingMode mCullingMode;
  695. bool mVsync;
  696. unsigned int mVSyncInterval;
  697. bool mInvertVertexWinding;
  698. /// Texture units from this upwards are disabled
  699. UINT16 mDisabledTexUnitsFrom;
  700. bool mVertexProgramBound;
  701. bool mGeometryProgramBound;
  702. bool mFragmentProgramBound;
  703. // Recording user clip planes
  704. PlaneList mClipPlanes;
  705. // Indicator that we need to re-set the clip planes on next render call
  706. bool mClipPlanesDirty;
  707. /// Used to store the capabilities of the graphics card
  708. RenderSystemCapabilities* mCurrentCapabilities;
  709. virtual void startUp_internal();
  710. virtual void shutdown_internal();
  711. /// Internal method used to set the underlying clip planes when needed
  712. virtual void setClipPlanesImpl(const PlaneList& clipPlanes) = 0;
  713. /** Query the real capabilities of the GPU and driver in the RenderSystem*/
  714. virtual RenderSystemCapabilities* createRenderSystemCapabilities() const = 0;
  715. /** Initialize the render system from the capabilities*/
  716. virtual void initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary) = 0;
  717. /** Create a MultiRenderTarget, which is a render target that renders to multiple RenderTextures
  718. at once. Surfaces can be bound and unbound at will.
  719. This fails if mCapabilities->getNumMultiRenderTargets() is smaller than 2.
  720. */
  721. virtual MultiRenderTarget * createMultiRenderTarget(const String & name) = 0;
  722. /** Returns a description of an error code.
  723. */
  724. virtual String getErrorDescription(long errorNumber) const = 0;
  725. DriverVersion mDriverVersion;
  726. /************************************************************************/
  727. /* THREADING */
  728. /************************************************************************/
  729. class RenderWorkerFunc CM_THREAD_WORKER_INHERIT
  730. {
  731. public:
  732. RenderWorkerFunc(RenderSystem* rs);
  733. void operator()();
  734. private:
  735. RenderSystem* mRS;
  736. };
  737. RenderWorkerFunc* mRenderThreadFunc;
  738. bool mRenderThreadShutdown;
  739. CM_THREAD_ID_TYPE mRenderThreadId;
  740. CM_THREAD_SYNCHRONISER(mRenderThreadStartCondition)
  741. CM_MUTEX(mRenderThreadStartMutex)
  742. CM_MUTEX(mCommandQueueMutex)
  743. CM_THREAD_SYNCHRONISER(mCommandReadyCondition)
  744. CM_MUTEX(mCommandNotifyMutex)
  745. CM_THREAD_SYNCHRONISER(mCommandCompleteCondition)
  746. #if CM_THREAD_SUPPORT
  747. CM_THREAD_TYPE* mRenderThread;
  748. #endif
  749. CommandQueue* mCommandQueue;
  750. UINT32 mMaxCommandNotifyId; // ID that will be assigned to the next command with a notifier callback
  751. vector<UINT32>::type mCommandsCompleted; // Completed commands that have notifier callbacks set up
  752. /**
  753. * @brief Initializes a separate render thread. Should only be called once.
  754. */
  755. void initRenderThread();
  756. /**
  757. * @brief Main function of the render thread. Called once thread is started.
  758. */
  759. void runRenderThread();
  760. /**
  761. * @brief Shutdowns the render thread. It will complete all ready commands
  762. * before shutdown.
  763. */
  764. void shutdownRenderThread();
  765. /**
  766. * @brief Throws an exception if current thread isn't the render thread;
  767. */
  768. void throwIfNotRenderThread() const;
  769. /**
  770. * @brief Blocks the calling thread until the command with the specified ID completes.
  771. * Make sure that the specified ID actually exists, otherwise this will block forever.
  772. */
  773. void blockUntilCommandCompleted(UINT32 commandId);
  774. /**
  775. * @brief Callback called by the command list when a specific command finishes executing.
  776. * This is only called on commands that have a special notify on complete flag set.
  777. *
  778. * @param commandId Identifier for the command.
  779. */
  780. void commandCompletedNotify(UINT32 commandId);
  781. public:
  782. /**
  783. * @brief Returns the id of the render thread. If a separate render thread
  784. * is not used, then it returns the id of the thread RenderSystem
  785. * was initialized on.
  786. */
  787. CM_THREAD_ID_TYPE getRenderThreadId() const { return mRenderThreadId; }
  788. /**
  789. * @brief Creates a new render system context that you can use for rendering on
  790. * a non-render thread. You can have as many of these as you wish, the only limitation
  791. * is that you do not use a single instance on more than one thread. Each thread
  792. * requires its own context. The context will be bound to the thread you call this method on.
  793. */
  794. DeferredRenderContextPtr createDeferredContext();
  795. /**
  796. * @brief Queues a new command that will be added to the global command queue. You are allowed to call this from any thread,
  797. * however be aware that it involves possibly slow synchronization primitives, so limit your usage.
  798. *
  799. * @param blockUntilComplete If true the thread will be blocked until the command executes. Be aware that there be many commands queued before it
  800. * and they all need to be executed in order before the current command is reached, which might take a long time.
  801. *
  802. * @see CommandQueue::queueReturn
  803. */
  804. AsyncOp queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete = false);
  805. /**
  806. * @brief Queues a new command that will be added to the global command queue.You are allowed to call this from any thread,
  807. * however be aware that it involves possibly slow synchronization primitives, so limit your usage.
  808. *
  809. * @param blockUntilComplete If true the thread will be blocked until the command executes. Be aware that there be many commands queued before it
  810. * and they all need to be executed in order before the current command is reached, which might take a long time.
  811. * @see CommandQueue::queue
  812. */
  813. void queueCommand(boost::function<void()> commandCallback, bool blockUntilComplete = false);
  814. };
  815. /** @} */
  816. /** @} */
  817. }
  818. #endif