ddsFile.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 _DDSFILE_H_
  23. #define _DDSFILE_H_
  24. #ifndef _GFXSTRUCTS_H_
  25. #include "gfx/gfxStructs.h"
  26. #endif
  27. #ifndef _BITSET_H_
  28. #include "core/bitSet.h"
  29. #endif
  30. #ifndef _TVECTOR_H_
  31. #include "core/util/tVector.h"
  32. #endif
  33. #ifndef __RESOURCE_H__
  34. #include "core/resource.h"
  35. #endif
  36. class Stream;
  37. class GBitmap;
  38. struct DDSFile
  39. {
  40. enum DDSFlags
  41. {
  42. ComplexFlag = BIT(0), ///< Indicates this includes a mipchain, cubemap, or
  43. /// volume texture, ie, isn't a plain old bitmap.
  44. MipMapsFlag = BIT(1), ///< Indicates we have a mipmap chain in the file.
  45. CubeMapFlag = BIT(2), ///< Indicates we are a cubemap. Requires all six faces.
  46. VolumeFlag = BIT(3), ///< Indicates we are a volume texture.
  47. PitchSizeFlag = BIT(4), ///< Cue as to how to interpret our pitchlinear value.
  48. LinearSizeFlag = BIT(5), ///< Cue as to how to interpret our pitchlinear value.
  49. RGBData = BIT(6), ///< Indicates that this is straight out RGBA data.
  50. CompressedData = BIT(7), ///< Indicates that this is compressed or otherwise
  51. /// exotic data.
  52. /// These are the flags for which cubemap
  53. /// surfaces are included in the file.
  54. CubeMap_PosX_Flag = BIT(8),
  55. CubeMap_NegX_Flag = BIT(9),
  56. CubeMap_PosY_Flag = BIT(10),
  57. CubeMap_NegY_Flag = BIT(11),
  58. CubeMap_PosZ_Flag = BIT(12),
  59. CubeMap_NegZ_Flag = BIT(13),
  60. };
  61. /// The index into mSurfaces for each
  62. /// cubemap face.
  63. enum
  64. {
  65. Cubemap_Surface_PosX,
  66. Cubemap_Surface_NegX,
  67. Cubemap_Surface_PosY,
  68. Cubemap_Surface_NegY,
  69. Cubemap_Surface_PosZ,
  70. Cubemap_Surface_NegZ,
  71. Cubemap_Surface_Count,
  72. };
  73. BitSet32 mFlags;
  74. U32 mHeight;
  75. U32 mWidth;
  76. U32 mDepth;
  77. U32 mPitchOrLinearSize;
  78. U32 mMipMapCount;
  79. GFXFormat mFormat;
  80. U32 mBytesPerPixel; ///< Ignored if we're a compressed texture.
  81. U32 mFourCC;
  82. String mCacheString;
  83. Torque::Path mSourcePath;
  84. bool mHasTransparency;
  85. // This is ugly... but it allows us to pass the number of
  86. // mips to drop into the ResourceManager loading process.
  87. static U32 smDropMipCount;
  88. struct SurfaceData
  89. {
  90. SurfaceData()
  91. {
  92. VECTOR_SET_ASSOCIATION( mMips );
  93. }
  94. ~SurfaceData()
  95. {
  96. // Free our mips!
  97. for(S32 i=0; i<mMips.size(); i++)
  98. delete[] mMips[i];
  99. }
  100. Vector<U8*> mMips;
  101. // Helper function to read in a mipchain.
  102. bool readMipChain();
  103. void dumpImage(DDSFile *dds, U32 mip, const char *file);
  104. /// Helper for reading a mip level.
  105. void readNextMip(DDSFile *dds, Stream &s, U32 height, U32 width, U32 mipLevel, bool skip);
  106. /// Helper for writing a mip level.
  107. void writeNextMip(DDSFile *dds, Stream &s, U32 height, U32 width, U32 mipLevel);
  108. };
  109. Vector<SurfaceData*> mSurfaces;
  110. /// Clear all our information; used before reading.
  111. void clear();
  112. /// Reads a DDS file from the stream.
  113. bool read(Stream &s, U32 dropMipCount);
  114. /// Called from read() to read in the DDS header.
  115. bool readHeader(Stream &s);
  116. /// Writes this DDS file to the stream.
  117. bool write(Stream &s);
  118. /// Called from write() to write the DDS header.
  119. bool writeHeader(Stream &s);
  120. /// For our current format etc., what is the size of a surface with the
  121. /// given dimensions?
  122. U32 getSurfaceSize( U32 mipLevel = 0 ) const { return getSurfaceSize( mHeight, mWidth, mipLevel ); }
  123. U32 getSurfaceSize( U32 height, U32 width, U32 mipLevel = 0 ) const;
  124. // Helper for getting the size in bytes of a compressed DDS texture.
  125. static U32 getSizeInBytes( GFXFormat format, U32 height, U32 width, U32 mipLevels );
  126. /// Returns the total video memory size of the texture
  127. /// including all mipmaps and compression settings.
  128. U32 getSizeInBytes() const;
  129. U32 getWidth( U32 mipLevel = 0 ) const { return getMax( U32(1), mWidth >> mipLevel ); }
  130. U32 getHeight( U32 mipLevel = 0 ) const { return getMax(U32(1), mHeight >> mipLevel); }
  131. U32 getDepth( U32 mipLevel = 0 ) const { return getMax(U32(1), mDepth >> mipLevel); }
  132. U32 getMipLevels() const { return mMipMapCount; }
  133. bool getHasTransparency() const { return mHasTransparency; }
  134. bool isCubemap() const { return mFlags.test( CubeMapFlag ); }
  135. GFXFormat getFormat() const { return mFormat; }
  136. U32 getSurfacePitch( U32 mipLevel = 0 ) const;
  137. const Torque::Path &getSourcePath() const { return mSourcePath; }
  138. const String &getTextureCacheString() const { return mCacheString; }
  139. static Resource<DDSFile> load( const Torque::Path &path, U32 dropMipCount );
  140. // For debugging fun!
  141. static S32 smActiveCopies;
  142. DDSFile()
  143. {
  144. VECTOR_SET_ASSOCIATION( mSurfaces );
  145. smActiveCopies++;
  146. mHasTransparency = false;
  147. }
  148. DDSFile( const DDSFile &dds );
  149. ~DDSFile()
  150. {
  151. smActiveCopies--;
  152. // Free our surfaces!
  153. for(S32 i=0; i<mSurfaces.size(); i++)
  154. delete mSurfaces[i];
  155. mSurfaces.clear();
  156. }
  157. static DDSFile *createDDSFileFromGBitmap( const GBitmap *gbmp );
  158. };
  159. #endif // _DDSFILE_H_