gfxTextureObject.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 _GFXTEXTUREOBJECT_H_
  23. #define _GFXTEXTUREOBJECT_H_
  24. #ifndef _REFBASE_H_
  25. #include "core/util/refBase.h"
  26. #endif
  27. #ifndef _MPOINT3_H_
  28. #include "math/mPoint3.h"
  29. #endif
  30. #ifndef _GFXENUMS_H_
  31. #include "gfx/gfxEnums.h"
  32. #endif
  33. #ifndef _GFXTEXTUREPROFILE_H_
  34. #include "gfx/gfxTextureProfile.h"
  35. #endif
  36. #ifndef _GFXRESOURCE_H_
  37. #include "gfx/gfxResource.h"
  38. #endif
  39. class Point2I;
  40. class GFXDevice;
  41. class GFXTextureProfile;
  42. class GBitmap;
  43. struct DDSFile;
  44. class RectI;
  45. /// Contains information on a locked region of a texture.
  46. ///
  47. /// In general, to access a given pixel in a locked rectangle, use this
  48. /// equation:
  49. ///
  50. /// @code
  51. /// U8 *pixelAtXY = bits + x * pitch + y * pixelSizeInBytes;
  52. /// @endcode
  53. ///
  54. /// @note D3DLOCKED_RECT and this structure match up. If you change this
  55. /// assumption, be sure to revisit the D3D GFX implementation.
  56. ///
  57. /// @see GFXTextureObject::lock()
  58. struct GFXLockedRect
  59. {
  60. /// Pitch of the lock. This is the spacing in bytes of the start
  61. /// of each row of the locked region.
  62. S32 pitch;
  63. /// Pointer to the start of locked rectangle.
  64. U8* bits;
  65. };
  66. class GFXTextureObject : public StrongRefBase, public GFXResource
  67. {
  68. public:
  69. #ifdef TORQUE_DEBUG
  70. // In debug builds we provide a TOC leak tracking system.
  71. static U32 smActiveTOCount;
  72. static GFXTextureObject *smHead;
  73. static U32 dumpActiveTOs();
  74. String mDebugCreationPath;
  75. String mDebugDescription;
  76. GFXTextureObject *mDebugNext;
  77. GFXTextureObject *mDebugPrev;
  78. #endif
  79. /// The path to the texture file if the
  80. /// content was loaded from a resource.
  81. String mPath;
  82. bool mDead;
  83. /// The device this texture belongs to.
  84. GFXDevice *mDevice;
  85. /// The next texture in the linked list.
  86. /// @see GFXTextureManager::mListHead
  87. GFXTextureObject *mNext;
  88. /// The previous texture in the linked list.
  89. /// @see GFXTextureManager::mListHead
  90. GFXTextureObject *mPrev;
  91. /// The siblings in the cache hash table.
  92. /// @see GFXTextureManager::mHashTable
  93. GFXTextureObject *mHashNext;
  94. /// This is the file name or other unique string used
  95. /// to hash this texture object.
  96. String mTextureLookupName;
  97. /// The time at which all references to this
  98. /// texture were removed.
  99. U32 mDeleteTime;
  100. Point3I mBitmapSize;
  101. Point3I mTextureSize;
  102. U32 mMipLevels;
  103. // TODO: This looks unused in the engine... not even sure
  104. // what it means. We should investigate and remove it.
  105. S32 mAntialiasLevel;
  106. bool mHasTransparency;
  107. // These two should be removed, and replaced by a reference to a resource
  108. // object, or data buffer. Something more generic. -patw
  109. GBitmap *mBitmap; ///< GBitmap we are backed by.
  110. DDSFile *mDDS; ///< DDSFile we're backed by.
  111. U32 getFormatByteSize() const { return GFXFormat_getByteSize( mFormat ); }
  112. GFXTextureProfile *mProfile;
  113. GFXFormat mFormat;
  114. GFXTextureObject(GFXDevice * aDevice, GFXTextureProfile *profile);
  115. virtual ~GFXTextureObject();
  116. GBitmap *getBitmap();
  117. DDSFile *getDDS();
  118. U32 getWidth() const { return mTextureSize.x; }
  119. U32 getHeight() const { return mTextureSize.y; }
  120. const Point3I& getSize() const { return mTextureSize; }
  121. U32 getDepth() const { return mTextureSize.z; }
  122. U32 getMipLevels() const { return mMipLevels; }
  123. U32 getBitmapWidth() const { return mBitmapSize.x; }
  124. U32 getBitmapHeight() const { return mBitmapSize.y; }
  125. U32 getBitmapDepth() const { return mBitmapSize.z; }
  126. GFXFormat getFormat() const { return mFormat; }
  127. /// Returns true if this texture is a render target.
  128. bool isRenderTarget() const { return mProfile->isRenderTarget(); }
  129. /// Returns the file path to the texture if
  130. /// it was loaded from disk.
  131. const String& getPath() const { return mPath; }
  132. virtual F32 getMaxUCoord() const;
  133. virtual F32 getMaxVCoord() const;
  134. /// Returns the estimated video memory usage
  135. /// in bytes including mipmaps.
  136. U32 getEstimatedSizeInBytes() const;
  137. /// Acquire a lock on part of the texture. The GFXLockedRect returned
  138. /// is managed by the GFXTextureObject and does not need to be freed.
  139. virtual GFXLockedRect * lock( U32 mipLevel = 0, RectI *inRect = NULL ) = 0;
  140. /// Releases a lock previously acquired. Note that the mipLevel parameter
  141. /// must match the corresponding lock!
  142. virtual void unlock( U32 mipLevel = 0) = 0;
  143. // copy the texture data into the specified bitmap.
  144. // - this texture object must be a render target. the function will assert if this is not the case.
  145. // - you must have called allocateBitmap() on the input bitmap first. the bitmap should have the
  146. // same dimensions as this texture. the bitmap format can be RGB or RGBA (in the latter case
  147. // the alpha values from the texture are copied too)
  148. // - returns true if successful, false otherwise
  149. // - this process is not fast.
  150. virtual bool copyToBmp(GBitmap* bmp) = 0;
  151. #ifdef TORQUE_DEBUG
  152. // It is important for any derived objects to define this method
  153. // and also call 'kill' from their destructors. If you fail to
  154. // do either, you will get a pure virtual function call crash
  155. // in debug mode. This is a precaution to make sure you don't
  156. // forget to add 'kill' to your destructor.
  157. virtual void pureVirtualCrash() = 0;
  158. #endif
  159. virtual void kill();
  160. /// Debug helper function for writing the texture to disk.
  161. bool dumpToDisk( const String &bmType, const String &path );
  162. // GFXResource interface
  163. /// The resource should put a description of itself (number of vertices, size/width of texture, etc.) in buffer
  164. virtual const String describeSelf() const;
  165. // StrongRefBase
  166. virtual void destroySelf();
  167. };
  168. //-----------------------------------------------------------------------------
  169. inline GBitmap *GFXTextureObject::getBitmap()
  170. {
  171. AssertFatal( mProfile->doStoreBitmap(), avar("GFXTextureObject::getBitmap - Cannot access bitmap for a '%s' texture.", mProfile->getName().c_str()) );
  172. return mBitmap;
  173. }
  174. inline DDSFile *GFXTextureObject::getDDS()
  175. {
  176. AssertFatal( mProfile->doStoreBitmap(), avar("GFXTextureObject::getDDS - Cannot access bitmap for a '%s' texture.", mProfile->getName().c_str()) );
  177. return mDDS;
  178. }
  179. #endif // _GFXTEXTUREOBJECT_H_