swizzle.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 _SWIZZLE_H_
  23. #define _SWIZZLE_H_
  24. #include "platform/platform.h"
  25. #include "core/frameAllocator.h"
  26. /// This class will swizzle 'sizeof( T )' length chunks of memory into different
  27. /// patterns which are user described. The pattern is described by an instance
  28. /// of Swizzle and this swizzle can then be executed on buffers. The following
  29. /// must be true of the buffer size:
  30. /// size % ( sizeof( T ) * mapLength ) == 0
  31. template<class T, dsize_t mapLength>
  32. class Swizzle
  33. {
  34. private:
  35. /// This is an array from 0..n. Each entry in the array is a dsize_t with values
  36. /// in the range 0..n. Each value in the range 0..n can occur any number of times.
  37. ///
  38. /// For example:
  39. /// This is our data set, an array of characters with 4 elements
  40. /// { 'a', 'b', 'c', 'd' }
  41. ///
  42. /// If the map { 3, 2, 1, 0 } was applied to this set, the result would be:
  43. /// { 'd', 'c', 'b', 'a' }
  44. ///
  45. /// If the map { 3, 0, 2, 2 } was applied to the set, the result would be:
  46. /// { 'd', 'a', 'c', 'c' }
  47. dsize_t mMap[mapLength];
  48. public:
  49. /// Construct a swizzle
  50. /// @see Swizzle::mMap
  51. Swizzle( const dsize_t *map );
  52. virtual ~Swizzle(){}
  53. /// This method will, in the general case, use the ToBuffer method to swizzle
  54. /// the memory specified into a temporary buffer, allocated by FrameTemp, and
  55. /// then copy the temporary memory into the source memory.
  56. ///
  57. /// @param memory Pointer to the start of the buffer to swizzle
  58. /// @param size Size of the buffer
  59. virtual void InPlace( void *memory, const dsize_t size ) const;
  60. /// This method copies the data from source to destination while applying the
  61. /// re-ordering. This method is, in the non-specalized case, O(N^2) where N
  62. /// is sizeof( T ) / size; the number of instances of type 'T' in the buffer
  63. ///
  64. /// @param destination The destination of the swizzled data
  65. /// @param source The source data to be swizzled
  66. /// @param size Size of the source and destination buffers.
  67. virtual void ToBuffer( void *destination, const void *source, const dsize_t size ) const;
  68. };
  69. // Null swizzle
  70. template<class T, dsize_t mapLength>
  71. class NullSwizzle : public Swizzle<T, mapLength>
  72. {
  73. public:
  74. NullSwizzle( const dsize_t *map = NULL ) : Swizzle<T, mapLength>( map ) {};
  75. virtual void InPlace( void *memory, const dsize_t size ) const {}
  76. virtual void ToBuffer( void *destination, const void *source, const dsize_t size ) const
  77. {
  78. dMemcpy( destination, source, size );
  79. }
  80. };
  81. //------------------------------------------------------------------------------
  82. // Common Swizzles
  83. namespace Swizzles
  84. {
  85. extern Swizzle<U8, 4> bgra;
  86. extern Swizzle<U8, 4> argb;
  87. extern Swizzle<U8, 4> rgba;
  88. extern Swizzle<U8, 4> abgr;
  89. extern Swizzle<U8, 3> bgr;
  90. extern Swizzle<U8, 3> rgb;
  91. extern NullSwizzle<U8, 4> null;
  92. };
  93. //------------------------------------------------------------------------------
  94. template<class T, dsize_t mapLength>
  95. Swizzle<T, mapLength>::Swizzle( const dsize_t *map )
  96. {
  97. if( map != NULL )
  98. dMemcpy( mMap, map, sizeof( dsize_t ) * mapLength );
  99. }
  100. //------------------------------------------------------------------------------
  101. template<class T, dsize_t mapLength>
  102. inline void Swizzle<T, mapLength>::ToBuffer( void *destination, const void *source, const dsize_t size ) const
  103. {
  104. // TODO: OpenMP?
  105. AssertFatal( size % ( sizeof( T ) * mapLength ) == 0, "Bad buffer size for swizzle, see docs." );
  106. if (!destination || !source) return;
  107. T *dest = reinterpret_cast<T *>( destination );
  108. const T *src = reinterpret_cast<const T *>( source );
  109. for( S32 i = 0; i < size / ( mapLength * sizeof( T ) ); i++ )
  110. {
  111. for( S32 j = 0; j < mapLength; j++ )
  112. *dest++ = src[mMap[j]];
  113. src += mapLength;
  114. }
  115. }
  116. //------------------------------------------------------------------------------
  117. template<class T, dsize_t mapLength>
  118. inline void Swizzle<T, mapLength>::InPlace( void *memory, const dsize_t size ) const
  119. {
  120. // Just in case the inliner messes up the FrameTemp scoping (not sure if it would) -patw
  121. {
  122. // FrameTemp should work because the PNG loading code uses the FrameAllocator, so
  123. // it should only get used on an image w/ that size as max -patw
  124. FrameTemp<U8> buffer( size );
  125. dMemcpy( ~buffer, memory, size );
  126. ToBuffer( memory, ~buffer, size );
  127. }
  128. }
  129. //------------------------------------------------------------------------------
  130. // Template specializations for certain swizzles
  131. //#include "core/util/swizzleSpec.h"
  132. #endif