squishOmp.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* -----------------------------------------------------------------------------
  2. Copyright (c) 2006 Simon Brown [email protected]
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  15. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  16. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  17. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  18. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. -------------------------------------------------------------------------- */
  20. #include <squish.h>
  21. //#define ENABLE_OPEN_MP
  22. #ifndef ENABLE_OPEN_MP
  23. void squish::CompressImageOMP(u8 const* rgba, int width, int height, void* blocks, int flags)
  24. {
  25. squish::CompressImage( rgba, width, height, blocks, flags );
  26. }
  27. #else
  28. // OMP implementation
  29. #include <omp.h>
  30. // OpenMP implementation of squish::CompressImage
  31. //
  32. // If you have any fixes, improvements, suggestions of: "L2OMP n00b"
  33. // please send them to:
  34. // Pat Wilson
  35. // [email protected]
  36. namespace squish {
  37. struct _blk_row
  38. {
  39. unsigned int pxl[4];
  40. };
  41. void CompressImageOMP( u8 const* rgba, int width, int height, void* blocks, int flags )
  42. {
  43. // fix any bad flags
  44. flags = FixFlags( flags );
  45. // Should really assert here or something
  46. if( width % 4 || height % 4 )
  47. return;
  48. // initialize the block output
  49. u8 *const targetBlock = reinterpret_cast<u8 *>( blocks );
  50. const int bytesPerBlock = ( ( flags & kDxt1 ) != 0 ) ? 8 : 16;
  51. const int blockHeight = height >> 2;
  52. const int blockWidth = width >> 2;
  53. #pragma omp parallel
  54. { // begin omp block
  55. // loop over blocks
  56. #pragma omp for
  57. for( int by = 0; by < blockHeight; by++ )
  58. {
  59. const int y = by * 4;
  60. for( int bx = 0; bx < blockWidth; bx++ )
  61. {
  62. const int x = bx * 4;
  63. // build the 4x4 block of pixels
  64. u8 sourceRgba[16 * 4];
  65. #define _load_row(r) (reinterpret_cast<_blk_row *>(sourceRgba))[r] = (*reinterpret_cast<const _blk_row *>(rgba + 4 * ( width * (y + r) + x )))
  66. _load_row(0);
  67. _load_row(1);
  68. _load_row(2);
  69. _load_row(3);
  70. #undef _load_row
  71. // compress it into the output
  72. const int blockIdx = by * blockWidth + bx;
  73. Compress( sourceRgba, &targetBlock[blockIdx * bytesPerBlock], flags );
  74. }
  75. }
  76. } // end omp block
  77. }
  78. } // namespace
  79. #endif