swizzleSpec.h

Engine/source/core/util/swizzleSpec.h

More...

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 _SWIZZLESPEC_H_
 25#define _SWIZZLESPEC_H_
 26
 27//------------------------------------------------------------------------------
 28// <U8, 4> (most common) Specialization
 29//------------------------------------------------------------------------------
 30#include "core/util/byteswap.h"
 31
 32template<>
 33inline void Swizzle<U8, 4>::InPlace( void *memory, const dsize_t size ) const
 34{
 35   AssertFatal( size % 4 == 0, "Bad buffer size for swizzle, see docs." );
 36
 37   U8 *dest = reinterpret_cast<U8 *>( memory );
 38   U8 *src = reinterpret_cast<U8 *>( memory );
 39
 40   // Fast divide by 4 since we are assured a proper size
 41   for( S32 i = 0; i < size >> 2; i++ )
 42   {
 43      BYTESWAP( *dest++, src[mMap[0]] );
 44      BYTESWAP( *dest++, src[mMap[1]] );
 45      BYTESWAP( *dest++, src[mMap[2]] );
 46      BYTESWAP( *dest++, src[mMap[3]] );
 47      
 48      src += 4;
 49   }
 50}
 51
 52template<>
 53inline void Swizzle<U8, 4>::ToBuffer( void *destination, const void *source, const dsize_t size ) const
 54{
 55   AssertFatal( size % 4 == 0, "Bad buffer size for swizzle, see docs." );
 56
 57   U8 *dest = reinterpret_cast<U8 *>( destination );
 58   const U8 *src = reinterpret_cast<const U8 *>( source );
 59
 60   // Fast divide by 4 since we are assured a proper size
 61   for( S32 i = 0; i < size >> 2; i++ )
 62   {
 63      *dest++ = src[mMap[0]];
 64      *dest++ = src[mMap[1]];
 65      *dest++ = src[mMap[2]];
 66      *dest++ = src[mMap[3]];
 67      
 68      src += 4;
 69   }
 70}
 71
 72//------------------------------------------------------------------------------
 73// <U8, 3> Specialization
 74//------------------------------------------------------------------------------
 75
 76template<>
 77inline void Swizzle<U8, 3>::InPlace( void *memory, const dsize_t size ) const
 78{
 79   AssertFatal( size % 3 == 0, "Bad buffer size for swizzle, see docs." );
 80
 81   U8 *dest = reinterpret_cast<U8 *>( memory );
 82   U8 *src = reinterpret_cast<U8 *>( memory );
 83
 84   for( S32 i = 0; i < size /3; i++ )
 85   {
 86      BYTESWAP( *dest++, src[mMap[0]] );
 87      BYTESWAP( *dest++, src[mMap[1]] );
 88      BYTESWAP( *dest++, src[mMap[2]] );
 89      
 90      src += 3;
 91   }
 92}
 93
 94template<>
 95inline void Swizzle<U8, 3>::ToBuffer( void *destination, const void *source, const dsize_t size ) const
 96{
 97   AssertFatal( size % 3 == 0, "Bad buffer size for swizzle, see docs." );
 98
 99   U8 *dest = reinterpret_cast<U8 *>( destination );
100   const U8 *src = reinterpret_cast<const U8 *>( source );
101
102   for( S32 i = 0; i < size / 3; i++ )
103   {
104      *dest++ = src[mMap[0]];
105      *dest++ = src[mMap[1]];
106      *dest++ = src[mMap[2]];
107      
108      src += 3;
109   }
110}
111
112
113#endif
114