gfxTarget.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GFXTARGET_H_
  23. #define _GFXTARGET_H_
  24. #ifndef _REFBASE_H_
  25. #include "core/util/refBase.h"
  26. #endif
  27. #ifndef _GFXENUMS_H_
  28. #include "gfx/gfxEnums.h"
  29. #endif
  30. #ifndef _GFXRESOURCE_H_
  31. #include "gfx/gfxResource.h"
  32. #endif
  33. #ifndef _MPOINT3_H_
  34. #include "math/mPoint3.h"
  35. #endif
  36. class Point2I;
  37. class PlatformWindow;
  38. class GFXCubemap;
  39. class GFXTextureObject;
  40. /// Base class for a target to which GFX can render.
  41. ///
  42. /// Most modern graphics hardware supports selecting render targets. However,
  43. /// there may be multiple types of render target, with wildly varying
  44. /// device-level implementations, resource requirements, and so forth.
  45. ///
  46. /// This base class is used to represent a render target; it might be a context
  47. /// tied to a window, or a set of surfaces or textures.
  48. class GFXTarget : public StrongRefBase, public GFXResource
  49. {
  50. private:
  51. S32 mChangeToken;
  52. S32 mLastAppliedChange;
  53. protected:
  54. /// Called whenever a change is made to this target.
  55. inline void invalidateState()
  56. {
  57. mChangeToken++;
  58. }
  59. /// Called when the device has applied pending state.
  60. inline void stateApplied()
  61. {
  62. mLastAppliedChange = mChangeToken;
  63. }
  64. public:
  65. /// Constructor to initialize the state tracking logic.
  66. GFXTarget() : mChangeToken( 0 ),
  67. mLastAppliedChange( 0 )
  68. {
  69. }
  70. virtual ~GFXTarget() {}
  71. /// Called to check if we have pending state for the device to apply.
  72. inline const bool isPendingState() const
  73. {
  74. return (mChangeToken != mLastAppliedChange);
  75. }
  76. /// Returns the size in pixels of the render target.
  77. virtual const Point2I getSize()=0;
  78. /// Returns the texture format of the render target.
  79. virtual GFXFormat getFormat()=0;
  80. // GFXResourceInterface
  81. /// The resource should put a description of itself (number of vertices, size/width of texture, etc.) in buffer
  82. virtual const String describeSelf() const;
  83. /// This is called to set the render target.
  84. virtual void activate() { }
  85. /// This is called when the target is not being used anymore.
  86. virtual void deactivate() { }
  87. /// This tells the target that the contents of this target should be restored
  88. /// when activate() is next called.
  89. virtual void preserve() { }
  90. /// Copy this surface to the passed GFXTextureObject.
  91. /// @param tex The GFXTextureObject to copy to.
  92. virtual void resolveTo( GFXTextureObject *tex ) { }
  93. };
  94. /// A render target associated with an OS window.
  95. ///
  96. /// Various API/OS combinations will implement their own GFXTargets for
  97. /// rendering to a window. However, they are all subclasses of GFXWindowTarget.
  98. ///
  99. /// This allows platform-neutral code to safely distinguish between various
  100. /// types of render targets (using dynamic_cast<>), as well as letting it
  101. /// gain access to useful things like the corresponding PlatformWindow.
  102. class GFXWindowTarget : public GFXTarget
  103. {
  104. protected:
  105. PlatformWindow *mWindow;
  106. public:
  107. GFXWindowTarget() : mWindow(NULL){};
  108. GFXWindowTarget( PlatformWindow *windowObject )
  109. {
  110. mWindow = windowObject;
  111. }
  112. virtual ~GFXWindowTarget() {}
  113. /// Returns a pointer to the window this target is bound to.
  114. inline PlatformWindow *getWindow() { return mWindow; };
  115. /// Present latest buffer, if buffer swapping is in effect.
  116. virtual bool present()=0;
  117. /// Notify the target that the video mode on the window has changed.
  118. virtual void resetMode()=0;
  119. };
  120. /// A render target associated with one or more textures.
  121. ///
  122. /// Although some APIs allow directly selecting any texture or surfaces, in
  123. /// some cases it is necessary to allocate helper resources to enable RTT
  124. /// operations.
  125. ///
  126. /// @note A GFXTextureTarget will retain references to textures that are
  127. /// attached to it, so be sure to clear them out when you're done!
  128. ///
  129. /// @note Different APIs have different restrictions on what they can support
  130. /// here. Be aware when mixing cubemaps vs. non-cubemaps, or targets of
  131. /// different resolutions. The devices will attempt to limit behavior
  132. /// to things that are safely portable, but they cannot catch every
  133. /// possible situation for all drivers and API - so make sure to
  134. /// actually test things!
  135. class GFXTextureTarget : public GFXTarget
  136. {
  137. public:
  138. enum RenderSlot
  139. {
  140. DepthStencil,
  141. Color0, Color1, Color2, Color3, Color4, Color5,
  142. MaxRenderSlotId,
  143. };
  144. static GFXTextureObject *sDefaultDepthStencil;
  145. virtual ~GFXTextureTarget() {}
  146. /// Attach a surface to a given slot as part of this render target.
  147. ///
  148. /// @param slot What slot is used for multiple render target (MRT) effects.
  149. /// Most of the time you'll use Color0.
  150. /// @param tex A texture and miplevel to bind for rendering, or else NULL/0
  151. /// to clear a slot.
  152. /// @param mipLevel What level of this texture are we rendering to?
  153. /// @param zOffset If this is a depth texture, what z level are we
  154. /// rendering to?
  155. virtual void attachTexture(RenderSlot slot, GFXTextureObject *tex, U32 mipLevel=0, U32 zOffset = 0) = 0;
  156. /// Support binding to cubemaps.
  157. ///
  158. /// @param slot What slot is used for multiple render target (MRT) effects.
  159. /// Most of the time you'll use Color0.
  160. /// @param tex What cubemap will we be rendering to?
  161. /// @param face A face identifier.
  162. /// @param mipLevel What level of this texture are we rendering to?
  163. virtual void attachTexture(RenderSlot slot, GFXCubemap *tex, U32 face, U32 mipLevel=0) = 0;
  164. /// Resolve the current render target data to the associated textures. This method
  165. /// will get called automatically when a rendertarget is changed, before new geometry
  166. /// is drawn to a different rendertarget. This method can also be called to
  167. /// gather render target data without switching targets.
  168. ///
  169. /// By default, this method will resolve all color targets.
  170. virtual void resolve()=0;
  171. /// Returns true if the automatic generation of mip maps is enabled
  172. inline bool isGenMipsEnabled() const { return mGenMips; }
  173. protected:
  174. bool mGenMips;
  175. };
  176. typedef StrongRefPtr<GFXTarget> GFXTargetRef;
  177. typedef StrongRefPtr<GFXWindowTarget> GFXWindowTargetRef;
  178. typedef StrongRefPtr<GFXTextureTarget> GFXTextureTargetRef;
  179. #endif // _GFXTARGET_H_