CmRenderTarget.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 __RenderTarget_H__
  25. #define __RenderTarget_H__
  26. #include "CmPrerequisites.h"
  27. #include "CmString.h"
  28. #include "CmPixelUtil.h"
  29. #include "CmViewport.h"
  30. /* Define the number of priority groups for the render system's render targets. */
  31. #ifndef OGRE_NUM_RENDERTARGET_GROUPS
  32. #define OGRE_NUM_RENDERTARGET_GROUPS 10
  33. #define OGRE_DEFAULT_RT_GROUP 4
  34. #define OGRE_REND_TO_TEX_RT_GROUP 2
  35. #endif
  36. namespace CamelotEngine {
  37. /** \addtogroup Core
  38. * @{
  39. */
  40. /** \addtogroup RenderSystem
  41. * @{
  42. */
  43. /** A 'canvas' which can receive the results of a rendering
  44. operation.
  45. @remarks
  46. This abstract class defines a common root to all targets of rendering operations. A
  47. render target could be a window on a screen, or another
  48. offscreen surface like a texture or bump map etc.
  49. @author
  50. Steven Streeting
  51. @version
  52. 1.0
  53. */
  54. class CM_EXPORT RenderTarget
  55. {
  56. public:
  57. enum FrameBuffer
  58. {
  59. FB_FRONT,
  60. FB_BACK,
  61. FB_AUTO
  62. };
  63. RenderTarget();
  64. virtual ~RenderTarget();
  65. /// Retrieve target's name.
  66. virtual const String& getName(void) const;
  67. /// Retrieve information about the render target.
  68. virtual void getMetrics(unsigned int& width, unsigned int& height, unsigned int& colourDepth);
  69. virtual unsigned int getWidth(void) const;
  70. virtual unsigned int getHeight(void) const;
  71. virtual unsigned int getColourDepth(void) const;
  72. /** Tells the target to update it's contents.
  73. @remarks
  74. If OGRE is not running in an automatic rendering loop
  75. (started using Root::startRendering),
  76. the user of the library is responsible for asking each render
  77. target to refresh. This is the method used to do this. It automatically
  78. re-renders the contents of the target using whatever cameras have been
  79. pointed at it (using Camera::setRenderTarget).
  80. @par
  81. This allows OGRE to be used in multi-windowed utilities
  82. and for contents to be refreshed only when required, rather than
  83. constantly as with the automatic rendering loop.
  84. @param swapBuffers For targets that support double-buffering, if set
  85. to true, the target will immediately
  86. swap it's buffers after update. Otherwise, the buffers are
  87. not swapped, and you have to call swapBuffers yourself sometime
  88. later. You might want to do this on some rendersystems which
  89. pause for queued rendering commands to complete before accepting
  90. swap buffers calls - so you could do other CPU tasks whilst the
  91. queued commands complete. Or, you might do this if you want custom
  92. control over your windows, such as for externally created windows.
  93. */
  94. virtual void update(bool swapBuffers = true);
  95. /** Swaps the frame buffers to display the next frame.
  96. @remarks
  97. For targets that are double-buffered so that no
  98. 'in-progress' versions of the scene are displayed
  99. during rendering. Once rendering has completed (to
  100. an off-screen version of the window) the buffers
  101. are swapped to display the new frame.
  102. @param
  103. waitForVSync If true, the system waits for the
  104. next vertical blank period (when the CRT beam turns off
  105. as it travels from bottom-right to top-left at the
  106. end of the pass) before flipping. If false, flipping
  107. occurs no matter what the beam position. Waiting for
  108. a vertical blank can be slower (and limits the
  109. framerate to the monitor refresh rate) but results
  110. in a steadier image with no 'tearing' (a flicker
  111. resulting from flipping buffers when the beam is
  112. in the progress of drawing the last frame).
  113. */
  114. virtual void swapBuffers(bool waitForVSync = true)
  115. { (void)waitForVSync; }
  116. /** Adds a viewport to the rendering target.
  117. @remarks
  118. A viewport is the rectangle into which rendering output is sent. This method adds
  119. a viewport to the render target, rendering from the supplied camera. The
  120. rest of the parameters are only required if you wish to add more than one viewport
  121. to a single rendering target. Note that size information passed to this method is
  122. passed as a parametric, i.e. it is relative rather than absolute. This is to allow
  123. viewports to automatically resize along with the target.
  124. @param
  125. ZOrder The relative order of the viewport with others on the target (allows overlapping
  126. viewports i.e. picture-in-picture). Higher ZOrders are on top of lower ones. The actual number
  127. is irrelevant, only the relative ZOrder matters (you can leave gaps in the numbering)
  128. @param
  129. left The relative position of the left of the viewport on the target, as a value between 0 and 1.
  130. @param
  131. top The relative position of the top of the viewport on the target, as a value between 0 and 1.
  132. @param
  133. width The relative width of the viewport on the target, as a value between 0 and 1.
  134. @param
  135. height The relative height of the viewport on the target, as a value between 0 and 1.
  136. */
  137. virtual Viewport* addViewport(int ZOrder = 0, float left = 0.0f, float top = 0.0f ,
  138. float width = 1.0f, float height = 1.0f);
  139. /** Returns the number of viewports attached to this target.*/
  140. virtual unsigned short getNumViewports(void) const;
  141. /** Retrieves a pointer to the viewport with the given index. */
  142. virtual Viewport* getViewport(unsigned short index);
  143. /** Removes a viewport at a given ZOrder.
  144. */
  145. virtual void removeViewport(int ZOrder);
  146. /** Removes all viewports on this target.
  147. */
  148. virtual void removeAllViewports(void);
  149. /** Gets a custom (maybe platform-specific) attribute.
  150. @remarks
  151. This is a nasty way of satisfying any API's need to see platform-specific details.
  152. It horrid, but D3D needs this kind of info. At least it's abstracted.
  153. @param
  154. name The name of the attribute.
  155. @param
  156. pData Pointer to memory of the right kind of structure to receive the info.
  157. */
  158. virtual void getCustomAttribute(const String& name, void* pData);
  159. /** Sets the priority of this render target in relation to the others.
  160. @remarks
  161. This can be used in order to schedule render target updates. Lower
  162. priorities will be rendered first. Note that the priority must be set
  163. at the time the render target is attached to the render system, changes
  164. afterwards will not affect the ordering.
  165. */
  166. virtual void setPriority( UINT8 priority ) { mPriority = priority; }
  167. /** Gets the priority of a render target. */
  168. virtual UINT8 getPriority() const { return mPriority; }
  169. /** Used to retrieve or set the active state of the render target.
  170. */
  171. virtual bool isActive() const;
  172. /** Used to set the active state of the render target.
  173. */
  174. virtual void setActive( bool state );
  175. /** Copies the current contents of the render target to a pixelbox.
  176. @remarks See suggestPixelFormat for a tip as to the best pixel format to
  177. extract into, although you can use whatever format you like and the
  178. results will be converted.
  179. */
  180. virtual void copyContentsToMemory(const PixelData &dst, FrameBuffer buffer = FB_AUTO) = 0;
  181. /** Suggests a pixel format to use for extracting the data in this target,
  182. when calling copyContentsToMemory.
  183. */
  184. virtual PixelFormat suggestPixelFormat() const { return PF_BYTE_RGBA; }
  185. virtual bool requiresTextureFlipping() const = 0;
  186. /** Indicates whether this target is the primary window. The
  187. primary window is special in that it is destroyed when
  188. ogre is shut down, and cannot be destroyed directly.
  189. This is the case because it holds the context for vertex,
  190. index buffers and textures.
  191. */
  192. virtual bool isPrimary(void) const;
  193. /** Indicates whether on rendering, linear colour space is converted to
  194. sRGB gamma colour space. This is the exact opposite conversion of
  195. what is indicated by Texture::isHardwareGammaEnabled, and can only
  196. be enabled on creation of the render target. For render windows, it's
  197. enabled through the 'gamma' creation misc parameter. For textures,
  198. it is enabled through the hwGamma parameter to the create call.
  199. */
  200. virtual bool isHardwareGammaEnabled() const { return mHwGamma; }
  201. /** Indicates whether multisampling is performed on rendering and at what level.
  202. */
  203. virtual UINT32 getFSAA() const { return mFSAA; }
  204. /** Gets the FSAA hint (@see Root::createRenderWindow)
  205. */
  206. virtual const String& getFSAAHint() const { return mFSAAHint; }
  207. /** RenderSystem specific interface for a RenderTarget;
  208. this should be subclassed by RenderSystems.
  209. */
  210. class Impl
  211. {
  212. protected:
  213. ~Impl() { }
  214. };
  215. /** Get rendersystem specific interface for this RenderTarget.
  216. This is used by the RenderSystem to (un)bind this target,
  217. and to get specific information like surfaces
  218. and framebuffer objects.
  219. */
  220. virtual Impl *_getImpl();
  221. /** Method for manual management of rendering : fires 'preRenderTargetUpdate'
  222. and initialises statistics etc.
  223. @remarks
  224. <ul>
  225. <li>_beginUpdate resets statistics and fires 'preRenderTargetUpdate'.</li>
  226. <li>_updateViewport renders the given viewport (even if it is not autoupdated),
  227. fires preViewportUpdate and postViewportUpdate and manages statistics.</li>
  228. <li>_updateAutoUpdatedViewports renders only viewports that are auto updated,
  229. fires preViewportUpdate and postViewportUpdate and manages statistics.</li>
  230. <li>_endUpdate() ends statistics calculation and fires postRenderTargetUpdate.</li>
  231. </ul>
  232. you can use it like this for example :
  233. <pre>
  234. renderTarget->_beginUpdate();
  235. renderTarget->_updateViewport(1); // which is not auto updated
  236. renderTarget->_updateViewport(2); // which is not auto updated
  237. renderTarget->_updateAutoUpdatedViewports();
  238. renderTarget->_endUpdate();
  239. renderTarget->swapBuffers(true);
  240. </pre>
  241. Please note that in that case, the zorder may not work as you expect,
  242. since you are responsible for calling _updateViewport in the correct order.
  243. */
  244. virtual void _beginUpdate();
  245. /** Method for manual management of rendering - renders the given
  246. viewport (even if it is not autoupdated)
  247. @remarks
  248. This also fires preViewportUpdate and postViewportUpdate, and manages statistics.
  249. You should call it between _beginUpdate() and _endUpdate().
  250. @see _beginUpdate for more details.
  251. @param zorder The zorder of the viewport to update.
  252. @param updateStatistics Whether you want to update statistics or not.
  253. */
  254. virtual void _updateViewport(int zorder, bool updateStatistics = true);
  255. /** Method for manual management of rendering - renders the given viewport (even if it is not autoupdated)
  256. @remarks
  257. This also fires preViewportUpdate and postViewportUpdate, and manages statistics
  258. if needed. You should call it between _beginUpdate() and _endUpdate().
  259. @see _beginUpdate for more details.
  260. @param viewport The viewport you want to update, it must be bound to the rendertarget.
  261. @param updateStatistics Whether you want to update statistics or not.
  262. */
  263. virtual void _updateViewport(Viewport* viewport, bool updateStatistics = true);
  264. /** Method for manual management of rendering - finishes statistics calculation
  265. and fires 'postRenderTargetUpdate'.
  266. @remarks
  267. You should call it after a _beginUpdate
  268. @see _beginUpdate for more details.
  269. */
  270. virtual void _endUpdate();
  271. protected:
  272. /// The name of this target.
  273. String mName;
  274. /// The priority of the render target.
  275. UINT8 mPriority;
  276. unsigned int mWidth;
  277. unsigned int mHeight;
  278. unsigned int mColourDepth;
  279. bool mIsDepthBuffered;
  280. bool mActive;
  281. bool mAutoUpdate;
  282. // Hardware sRGB gamma conversion done on write?
  283. bool mHwGamma;
  284. // FSAA performed?
  285. UINT32 mFSAA;
  286. String mFSAAHint;
  287. typedef map<int, Viewport*>::type ViewportList;
  288. /// List of viewports, map on Z-order
  289. ViewportList mViewportList;
  290. /// Internal implementation of update()
  291. virtual void updateImpl();
  292. };
  293. /** @} */
  294. /** @} */
  295. } // Namespace
  296. #endif