ddsFile.h

Engine/source/gfx/bitmap/ddsFile.h

More...

Classes:

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _DDSFILE_H_
 25#define _DDSFILE_H_
 26
 27#ifndef _GFXSTRUCTS_H_
 28#include "gfx/gfxStructs.h"
 29#endif
 30#ifndef _BITSET_H_
 31#include "core/bitSet.h"
 32#endif
 33#ifndef _TVECTOR_H_
 34#include "core/util/tVector.h"
 35#endif
 36#ifndef __RESOURCE_H__
 37#include "core/resource.h"
 38#endif
 39
 40class Stream;
 41class GBitmap;
 42
 43
 44struct DDSFile
 45{
 46   enum DDSFlags
 47   {
 48      ComplexFlag = BIT(0), ///< Indicates this includes a mipchain, cubemap, or
 49                            ///  volume texture, ie, isn't a plain old bitmap.
 50      MipMapsFlag = BIT(1), ///< Indicates we have a mipmap chain in the file.
 51      CubeMapFlag = BIT(2), ///< Indicates we are a cubemap. Requires all six faces.
 52      VolumeFlag  = BIT(3), ///< Indicates we are a volume texture.
 53
 54      PitchSizeFlag = BIT(4),  ///< Cue as to how to interpret our pitchlinear value.
 55      LinearSizeFlag = BIT(5), ///< Cue as to how to interpret our pitchlinear value.
 56
 57      RGBData        = BIT(6), ///< Indicates that this is straight out RGBA data.
 58      CompressedData = BIT(7), ///< Indicates that this is compressed or otherwise
 59                               ///  exotic data.
 60
 61      /// These are the flags for which cubemap 
 62      /// surfaces are included in the file.
 63      CubeMap_PosX_Flag = BIT(8),
 64      CubeMap_NegX_Flag = BIT(9),
 65      CubeMap_PosY_Flag = BIT(10),
 66      CubeMap_NegY_Flag = BIT(11),
 67      CubeMap_PosZ_Flag = BIT(12),
 68      CubeMap_NegZ_Flag = BIT(13),
 69      CubeMap_All_Flags = CubeMapFlag</a>|<a href="/coding/class/structddsfile/#structddsfile_1a8eb961b4b9d24df0a945833c2a8c0c82a79444ea4dc7982767e59d020d6345203">CubeMap_PosX_Flag | CubeMap_NegX_Flag | CubeMap_PosY_Flag | CubeMap_NegY_Flag | CubeMap_PosZ_Flag | CubeMap_NegZ_Flag,
 70   };
 71
 72   /// The index into mSurfaces for each 
 73   /// cubemap face.
 74   enum
 75   {
 76      Cubemap_Surface_PosX,
 77      Cubemap_Surface_NegX,
 78      Cubemap_Surface_PosY,
 79      Cubemap_Surface_NegY,
 80      Cubemap_Surface_PosZ,
 81      Cubemap_Surface_NegZ,
 82      Cubemap_Surface_Count,
 83   };
 84
 85   BitSet32    mFlags;
 86   U32         mHeight;
 87   U32         mWidth;
 88   U32         mDepth;
 89   U32         mPitchOrLinearSize;
 90   U32         mMipMapCount;
 91
 92   GFXFormat   mFormat;
 93   U32         mBytesPerPixel; ///< Ignored if we're a compressed texture.
 94   U32         mFourCC;
 95   String      mCacheString;
 96   Torque::Path mSourcePath;
 97
 98   bool        mHasTransparency;
 99
100   // This is ugly... but it allows us to pass the number of
101   // mips to drop into the ResourceManager loading process.
102   static U32 smDropMipCount;
103
104   struct SurfaceData
105   {
106      SurfaceData()
107      {
108         VECTOR_SET_ASSOCIATION( mMips );
109      }
110
111      ~SurfaceData()
112      {
113         // Free our mips!
114         for(S32 i=0; i<mMips.size(); i++)
115            delete[] mMips[i];
116      }
117
118      Vector<U8*> mMips;
119
120      void dumpImage(DDSFile *dds, U32 mip, const char *file);
121      
122      /// Helper for reading a mip level.
123      void readNextMip(DDSFile *dds, Stream &s, U32 height, U32 width, U32 mipLevel, bool skip);
124      
125      /// Helper for writing a mip level.
126      void writeNextMip(DDSFile *dds, Stream &s, U32 height, U32 width, U32 mipLevel);
127   };
128   
129   Vector<SurfaceData*> mSurfaces;
130
131   /// Clear all our information; used before reading.
132   void clear();
133
134   /// Reads a DDS file from the stream.
135   bool read(Stream &s, U32 dropMipCount);
136
137   /// Called from read() to read in the DDS header.
138   bool readHeader(Stream &s);
139
140   /// Writes this DDS file to the stream.
141   bool write(Stream &s);
142
143   /// Called from write() to write the DDS header.
144   bool writeHeader(Stream &s);
145
146   /// For our current format etc., what is the size of a surface with the
147   /// given dimensions?
148   U32 getSurfaceSize( U32 mipLevel = 0 ) const { return getSurfaceSize( mHeight, mWidth, mipLevel ); }
149   U32 getSurfaceSize( U32 height, U32 width, U32 mipLevel = 0 ) const;
150
151   // Helper for getting the size in bytes of a compressed DDS texture.
152   static U32 getSizeInBytes( GFXFormat format, U32 height, U32 width, U32 mipLevels );
153
154   /// Returns the total video memory size of the texture
155   /// including all mipmaps and compression settings.
156   U32 getSizeInBytes() const;
157
158   U32 getWidth( U32 mipLevel = 0 ) const { return getMax( U32(1), mWidth >> mipLevel ); }
159   U32 getHeight( U32 mipLevel = 0 ) const { return getMax(U32(1), mHeight >> mipLevel); }
160   U32 getDepth( U32 mipLevel = 0 ) const { return getMax(U32(1), mDepth >> mipLevel); }
161
162   U32 getMipLevels() const { return mMipMapCount; }
163
164   bool getHasTransparency() const { return mHasTransparency; }
165
166   bool isCubemap() const { return mFlags.test( CubeMapFlag ); }
167
168   GFXFormat getFormat() const { return mFormat; }
169
170   U32 getSurfacePitch( U32 mipLevel = 0 ) const;
171
172   const Torque::Path &getSourcePath() const { return mSourcePath; }
173   const String &getTextureCacheString() const { return mCacheString; }
174
175   static Resource<DDSFile> load( const Torque::Path &path, U32 dropMipCount );
176
177   // For debugging fun!
178   static S32 smActiveCopies;
179
180   DDSFile():
181      mBytesPerPixel(0),
182      mHeight(0),
183      mWidth(0),
184      mDepth(0),
185      mFormat(GFXFormat_FIRST),
186      mFourCC(0),
187      mMipMapCount(0),
188      mPitchOrLinearSize(0)
189   {
190      VECTOR_SET_ASSOCIATION( mSurfaces );
191      smActiveCopies++;
192
193      mHasTransparency = false;
194   }
195
196   DDSFile( const DDSFile &dds );
197
198   ~DDSFile()
199   {
200      smActiveCopies--;
201
202      // Free our surfaces!
203      for(S32 i=0; i<mSurfaces.size(); i++)
204         delete mSurfaces[i];
205
206      mSurfaces.clear();
207   }
208
209   static DDSFile *createDDSFileFromGBitmap( const GBitmap *gbmp );
210   //Create a single cubemap texture from 6 GBitmap
211   static DDSFile *createDDSCubemapFileFromGBitmaps(GBitmap **gbmps);
212   bool decompressToGBitmap(GBitmap *dest);
213};
214
215#endif // _DDSFILE_H_
216