Torque3D Documentation / _generateds / dxt5nmSwizzle.h

dxt5nmSwizzle.h

Engine/source/core/util/dxt5nmSwizzle.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 _DXT5nm_SWIZZLE_H_
 25#define _DXT5nm_SWIZZLE_H_
 26
 27#include "core/util/swizzle.h"
 28#include "core/util/byteswap.h"
 29
 30class DXT5nmSwizzle : public Swizzle<U8, 4>
 31{
 32public:
 33   DXT5nmSwizzle() : Swizzle<U8, 4>( NULL ) {};
 34
 35   virtual void InPlace( void *memory, const dsize_t size ) const
 36   {
 37      AssertFatal( size % 4 == 0, "Bad buffer size for DXT5nm Swizzle" );
 38
 39      volatile U8 *u8Mem = reinterpret_cast<U8 *>( memory );
 40
 41      for( S32 i = 0; i < size >> 2; i++ )
 42      {
 43         // g = garbage byte
 44         // Input: [X|Y|Z|g]     (rgba)
 45         // Output: [g|Y|0xFF|X] (bgra)
 46         BYTESWAP( u8Mem[0], u8Mem[3] ); // Store X in Alpha
 47         *u8Mem ^= *u8Mem;               // 0 the garbage bit
 48         u8Mem[2] |= 0xFF;               // Set Red to 1.0
 49
 50         u8Mem += 4;
 51      }
 52   }
 53
 54   virtual void ToBuffer( void *destination, const void *source, const dsize_t size ) const
 55   {
 56      AssertFatal( size % 4 == 0, "Bad buffer size for DXT5nm Swizzle" );
 57
 58      volatile const U8 *srcU8 = reinterpret_cast<const U8 *>( source );
 59      volatile U8 *dstU8 = reinterpret_cast<U8 *>( destination );
 60
 61      for( S32 i = 0; i < size >> 2; i++ )
 62      {
 63         // g = garbage byte
 64         // Input: [X|Y|Z|g]     (rgba)
 65         // Output: [g|Y|0xFF|X] (bgra)
 66         *dstU8++ = 0;                   // 0 garbage bit
 67         *dstU8++ = srcU8[1];            // Copy Y into G
 68         *dstU8++ |= 0xFF;               // Set Red to 1.0
 69         *dstU8++ = srcU8[0];            // Copy X into Alpha
 70
 71         srcU8 += 4;
 72      }
 73   }
 74};
 75
 76class DXT5nmSwizzleUp24t32 : public Swizzle<U8, 3>
 77{
 78public:
 79   DXT5nmSwizzleUp24t32() : Swizzle<U8, 3>( NULL ) {};
 80
 81   virtual void InPlace( void *memory, const dsize_t size ) const
 82   {
 83      AssertISV( false, "Cannot swizzle in place a 24->32 bit swizzle." );
 84   }
 85
 86   virtual void ToBuffer( void *destination, const void *source, const dsize_t size ) const
 87   {
 88      AssertFatal( size % 3 == 0, "Bad buffer size for DXT5nm Swizzle" );
 89      const S32 pixels = size / 3;
 90
 91      volatile const U8 *srcU8 = reinterpret_cast<const U8 *>( source );
 92      volatile U8 *dstU8 = reinterpret_cast<U8 *>( destination );
 93
 94      // destination better damn well be the right size
 95      for( S32 i = 0; i < pixels; i++ )
 96      {
 97         // g = garbage byte
 98         // Input: [X|Y|Z|g]     (rgba)
 99         // Output: [g|Y|0xFF|X] (bgra)
100         *dstU8++ = 0;                   // 0 garbage bit
101         *dstU8++ = srcU8[1];            // Copy Y into G
102         *dstU8++ |= 0xFF;               // Set Red to 1.0
103         *dstU8++ = srcU8[0];            // Copy X into Alpha
104
105         srcU8 += 3;
106      }
107   }
108};
109#endif
110