gfxTarget.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. friend class GFXD3D9Device;
  51. friend class GFX360Device;
  52. private:
  53. S32 mChangeToken;
  54. S32 mLastAppliedChange;
  55. protected:
  56. /// Called whenever a change is made to this target.
  57. inline void invalidateState()
  58. {
  59. mChangeToken++;
  60. }
  61. /// Called when the device has applied pending state.
  62. inline void stateApplied()
  63. {
  64. mLastAppliedChange = mChangeToken;
  65. }
  66. public:
  67. /// Constructor to initialize the state tracking logic.
  68. GFXTarget() : mChangeToken( 0 ),
  69. mLastAppliedChange( 0 )
  70. {
  71. }
  72. virtual ~GFXTarget() {}
  73. /// Called to check if we have pending state for the device to apply.
  74. inline const bool isPendingState() const
  75. {
  76. return (mChangeToken != mLastAppliedChange);
  77. }
  78. /// Returns the size in pixels of the render target.
  79. virtual const Point2I getSize()=0;
  80. /// Returns the texture format of the render target.
  81. virtual GFXFormat getFormat()=0;
  82. // GFXResourceInterface
  83. /// The resource should put a description of itself (number of vertices, size/width of texture, etc.) in buffer
  84. virtual const String describeSelf() const;
  85. /// This is called to set the render target.
  86. virtual void activate() { }
  87. /// This is called when the target is not being used anymore.
  88. virtual void deactivate() { }
  89. /// This tells the target that the contents of this target should be restored
  90. /// when activate() is next called.
  91. virtual void preserve() { }
  92. /// Copy this surface to the passed GFXTextureObject.
  93. /// @param tex The GFXTextureObject to copy to.
  94. virtual void resolveTo( GFXTextureObject *tex ) { }
  95. };
  96. /// A render target associated with an OS window.
  97. ///
  98. /// Various API/OS combinations will implement their own GFXTargets for
  99. /// rendering to a window. However, they are all subclasses of GFXWindowTarget.
  100. ///
  101. /// This allows platform-neutral code to safely distinguish between various
  102. /// types of render targets (using dynamic_cast<>), as well as letting it
  103. /// gain access to useful things like the corresponding PlatformWindow.
  104. class GFXWindowTarget : public GFXTarget
  105. {
  106. protected:
  107. PlatformWindow *mWindow;
  108. public:
  109. GFXWindowTarget() : mWindow(NULL){};
  110. GFXWindowTarget( PlatformWindow *windowObject )
  111. {
  112. mWindow = windowObject;
  113. }
  114. virtual ~GFXWindowTarget() {}
  115. /// Returns a pointer to the window this target is bound to.
  116. inline PlatformWindow *getWindow() { return mWindow; };
  117. /// Present latest buffer, if buffer swapping is in effect.
  118. virtual bool present()=0;
  119. /// Notify the target that the video mode on the window has changed.
  120. virtual void resetMode()=0;
  121. };
  122. /// A render target associated with one or more textures.
  123. ///
  124. /// Although some APIs allow directly selecting any texture or surfaces, in
  125. /// some cases it is necessary to allocate helper resources to enable RTT
  126. /// operations.
  127. ///
  128. /// @note A GFXTextureTarget will retain references to textures that are
  129. /// attached to it, so be sure to clear them out when you're done!
  130. ///
  131. /// @note Different APIs have different restrictions on what they can support
  132. /// here. Be aware when mixing cubemaps vs. non-cubemaps, or targets of
  133. /// different resolutions. The devices will attempt to limit behavior
  134. /// to things that are safely portable, but they cannot catch every
  135. /// possible situation for all drivers and API - so make sure to
  136. /// actually test things!
  137. class GFXTextureTarget : public GFXTarget
  138. {
  139. public:
  140. enum RenderSlot
  141. {
  142. DepthStencil,
  143. Color0, Color1, Color2, Color3, Color4,
  144. MaxRenderSlotId,
  145. };
  146. static GFXTextureObject *sDefaultDepthStencil;
  147. virtual ~GFXTextureTarget() {}
  148. /// Attach a surface to a given slot as part of this render target.
  149. ///
  150. /// @param slot What slot is used for multiple render target (MRT) effects.
  151. /// Most of the time you'll use Color0.
  152. /// @param tex A texture and miplevel to bind for rendering, or else NULL/0
  153. /// to clear a slot.
  154. /// @param mipLevel What level of this texture are we rendering to?
  155. /// @param zOffset If this is a depth texture, what z level are we
  156. /// rendering to?
  157. virtual void attachTexture(RenderSlot slot, GFXTextureObject *tex, U32 mipLevel=0, U32 zOffset = 0) = 0;
  158. /// Support binding to cubemaps.
  159. ///
  160. /// @param slot What slot is used for multiple render target (MRT) effects.
  161. /// Most of the time you'll use Color0.
  162. /// @param tex What cubemap will we be rendering to?
  163. /// @param face A face identifier.
  164. /// @param mipLevel What level of this texture are we rendering to?
  165. virtual void attachTexture(RenderSlot slot, GFXCubemap *tex, U32 face, U32 mipLevel=0) = 0;
  166. /// Resolve the current render target data to the associated textures. This method
  167. /// will get called automatically when a rendertarget is changed, before new geometry
  168. /// is drawn to a different rendertarget. This method can also be called to
  169. /// gather render target data without switching targets.
  170. ///
  171. /// By default, this method will resolve all color targets.
  172. virtual void resolve()=0;
  173. };
  174. typedef StrongRefPtr<GFXTarget> GFXTargetRef;
  175. typedef StrongRefPtr<GFXWindowTarget> GFXWindowTargetRef;
  176. typedef StrongRefPtr<GFXTextureTarget> GFXTextureTargetRef;
  177. #endif // _GFXTARGET_H_