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