dxt5nmSwizzle.h 3.8 KB

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