ddsFile.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. CubeMap_All_Flags = CubeMapFlag|CubeMap_PosX_Flag | CubeMap_NegX_Flag | CubeMap_PosY_Flag | CubeMap_NegY_Flag | CubeMap_PosZ_Flag | CubeMap_NegZ_Flag,
  61. };
  62. /// The index into mSurfaces for each
  63. /// cubemap face.
  64. enum
  65. {
  66. Cubemap_Surface_PosX,
  67. Cubemap_Surface_NegX,
  68. Cubemap_Surface_PosY,
  69. Cubemap_Surface_NegY,
  70. Cubemap_Surface_PosZ,
  71. Cubemap_Surface_NegZ,
  72. Cubemap_Surface_Count,
  73. };
  74. BitSet32 mFlags;
  75. U32 mHeight;
  76. U32 mWidth;
  77. U32 mDepth;
  78. U32 mPitchOrLinearSize;
  79. U32 mMipMapCount;
  80. GFXFormat mFormat;
  81. U32 mBytesPerPixel; ///< Ignored if we're a compressed texture.
  82. U32 mFourCC;
  83. String mCacheString;
  84. Torque::Path mSourcePath;
  85. bool mHasTransparency;
  86. // This is ugly... but it allows us to pass the number of
  87. // mips to drop into the ResourceManager loading process.
  88. static U32 smDropMipCount;
  89. struct SurfaceData
  90. {
  91. SurfaceData()
  92. {
  93. VECTOR_SET_ASSOCIATION( mMips );
  94. }
  95. ~SurfaceData()
  96. {
  97. // Free our mips!
  98. for(S32 i=0; i<mMips.size(); i++)
  99. delete[] mMips[i];
  100. }
  101. Vector<U8*> mMips;
  102. void dumpImage(DDSFile *dds, U32 mip, const char *file);
  103. /// Helper for reading a mip level.
  104. void readNextMip(DDSFile *dds, Stream &s, U32 height, U32 width, U32 mipLevel, bool skip);
  105. /// Helper for writing a mip level.
  106. void writeNextMip(DDSFile *dds, Stream &s, U32 height, U32 width, U32 mipLevel);
  107. };
  108. Vector<SurfaceData*> mSurfaces;
  109. /// Clear all our information; used before reading.
  110. void clear();
  111. /// Reads a DDS file from the stream.
  112. bool read(Stream &s, U32 dropMipCount);
  113. /// Called from read() to read in the DDS header.
  114. bool readHeader(Stream &s);
  115. /// Writes this DDS file to the stream.
  116. bool write(Stream &s);
  117. /// Called from write() to write the DDS header.
  118. bool writeHeader(Stream &s);
  119. /// For our current format etc., what is the size of a surface with the
  120. /// given dimensions?
  121. U32 getSurfaceSize( U32 mipLevel = 0 ) const { return getSurfaceSize( mHeight, mWidth, mipLevel ); }
  122. U32 getSurfaceSize( U32 height, U32 width, U32 mipLevel = 0 ) const;
  123. // Helper for getting the size in bytes of a compressed DDS texture.
  124. static U32 getSizeInBytes( GFXFormat format, U32 height, U32 width, U32 mipLevels );
  125. /// Returns the total video memory size of the texture
  126. /// including all mipmaps and compression settings.
  127. U32 getSizeInBytes() const;
  128. U32 getWidth( U32 mipLevel = 0 ) const { return getMax( U32(1), mWidth >> mipLevel ); }
  129. U32 getHeight( U32 mipLevel = 0 ) const { return getMax(U32(1), mHeight >> mipLevel); }
  130. U32 getDepth( U32 mipLevel = 0 ) const { return getMax(U32(1), mDepth >> mipLevel); }
  131. U32 getMipLevels() const { return mMipMapCount; }
  132. bool getHasTransparency() const { return mHasTransparency; }
  133. bool isCubemap() const { return mFlags.test( CubeMapFlag ); }
  134. GFXFormat getFormat() const { return mFormat; }
  135. U32 getSurfacePitch( U32 mipLevel = 0 ) const;
  136. const Torque::Path &getSourcePath() const { return mSourcePath; }
  137. const String &getTextureCacheString() const { return mCacheString; }
  138. static Resource<DDSFile> load( const Torque::Path &path, U32 dropMipCount );
  139. // For debugging fun!
  140. static S32 smActiveCopies;
  141. DDSFile():
  142. mBytesPerPixel(0),
  143. mHeight(0),
  144. mWidth(0),
  145. mDepth(0),
  146. mFormat(GFXFormat_FIRST),
  147. mFourCC(0),
  148. mMipMapCount(0),
  149. mPitchOrLinearSize(0)
  150. {
  151. VECTOR_SET_ASSOCIATION( mSurfaces );
  152. smActiveCopies++;
  153. mHasTransparency = false;
  154. }
  155. DDSFile( const DDSFile &dds );
  156. ~DDSFile()
  157. {
  158. smActiveCopies--;
  159. // Free our surfaces!
  160. for(S32 i=0; i<mSurfaces.size(); i++)
  161. delete mSurfaces[i];
  162. mSurfaces.clear();
  163. }
  164. static DDSFile *createDDSFileFromGBitmap( const GBitmap *gbmp );
  165. //Create a single cubemap texture from 6 GBitmap
  166. static DDSFile *createDDSCubemapFileFromGBitmaps(GBitmap **gbmps);
  167. bool decompressToGBitmap(GBitmap *dest);
  168. };
  169. #endif // _DDSFILE_H_