swizzleSpec.h 3.7 KB

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