CmRenderSystem.h 48 KB

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