nvtt.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2011-2020 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause
  4. */
  5. #include "nvtt.h"
  6. #include <string.h>
  7. #include <bx/uint32_t.h>
  8. BX_PRAGMA_DIAGNOSTIC_PUSH();
  9. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4458) // warning C4458: declaration of 'x' hides class member
  10. #include "bc6h/zoh.h"
  11. #include "bc7/avpcl.h"
  12. #include "nvmath/vector.inl"
  13. BX_PRAGMA_DIAGNOSTIC_POP();
  14. BX_PRAGMA_DIAGNOSTIC_PUSH();
  15. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4702) // warning C4702: unreachable code
  16. NVCORE_API int nvAbort(const char *, const char *, int , const char *, const char *, ...)
  17. {
  18. abort();
  19. return 0;
  20. }
  21. BX_PRAGMA_DIAGNOSTIC_POP();
  22. namespace nvtt
  23. {
  24. void compressBC6H(const void* _input, uint32_t _width, uint32_t _height, uint32_t _srcStride, void* _output)
  25. {
  26. const uint8_t* src = (const uint8_t*)_input;
  27. char* dst = (char*)_output;
  28. for (uint32_t yy = 0; yy < _height; yy += ZOH::Tile::TILE_H)
  29. {
  30. for (uint32_t xx = 0; xx < _width; xx += ZOH::Tile::TILE_H)
  31. {
  32. const uint32_t bytesPerPixel = sizeof(float)*4;
  33. const nv::Vector4* srcRgba = (const nv::Vector4*)&src[yy*_srcStride + xx*bytesPerPixel];
  34. const uint32_t srcRgbaStride = _srcStride/bytesPerPixel;
  35. ZOH::Utils::FORMAT = ZOH::SIGNED_F16;
  36. ZOH::Tile zohTile(ZOH::Tile::TILE_H, ZOH::Tile::TILE_H);
  37. bx::memSet(zohTile.data, 0, sizeof(zohTile.data) );
  38. bx::memSet(zohTile.importance_map, 0, sizeof(zohTile.importance_map) );
  39. for (uint32_t blockY = 0; blockY < ZOH::Tile::TILE_H; ++blockY)
  40. {
  41. for (uint32_t blockX = 0; blockX < ZOH::Tile::TILE_W; ++blockX)
  42. {
  43. nv::Vector4 color = srcRgba[blockY*srcRgbaStride + blockX];
  44. zohTile.data[blockY][blockX].x = float(int16_t(bx::halfFromFloat(bx::max(color.x, 0.0f) ) ) );
  45. zohTile.data[blockY][blockX].y = float(int16_t(bx::halfFromFloat(bx::max(color.y, 0.0f) ) ) );
  46. zohTile.data[blockY][blockX].z = float(int16_t(bx::halfFromFloat(bx::max(color.z, 0.0f) ) ) );
  47. }
  48. }
  49. zohTile.generate_importance_map();
  50. ZOH::compress(zohTile, dst);
  51. dst += ZOH::BLOCKSIZE;
  52. }
  53. }
  54. }
  55. void compressBC7(const void* _input, uint32_t _width, uint32_t _height, uint32_t _srcStride, void* _output)
  56. {
  57. const uint8_t* src = (const uint8_t*)_input;
  58. char* dst = (char*)_output;
  59. for (uint32_t yy = 0; yy < _height; yy += 4)
  60. {
  61. for (uint32_t xx = 0; xx < _width; xx += 4)
  62. {
  63. const uint32_t bytesPerPixel = sizeof(float) * 4;
  64. const nv::Vector4* srcRgba = (const nv::Vector4*)&src[yy*_srcStride + xx*bytesPerPixel];
  65. const uint32_t srcRgbaStride = _srcStride / bytesPerPixel;
  66. AVPCL::mode_rgb = false;
  67. AVPCL::flag_premult = false;
  68. AVPCL::flag_nonuniform = false;
  69. AVPCL::flag_nonuniform_ati = false;
  70. AVPCL::Tile avpclTile(4, 4);
  71. bx::memSet(avpclTile.data, 0, sizeof(avpclTile.data) );
  72. for (uint32_t blockY = 0; blockY < 4; ++blockY)
  73. {
  74. for (uint32_t blockX = 0; blockX < 4; ++blockX)
  75. {
  76. nv::Vector4 color = srcRgba[blockY*srcRgbaStride + blockX];
  77. avpclTile.data[blockY][blockX] = color * 255.0f;
  78. avpclTile.importance_map[blockY][blockX] = 1.0f;
  79. }
  80. }
  81. AVPCL::compress(avpclTile, dst);
  82. dst += AVPCL::BLOCKSIZE;
  83. }
  84. }
  85. }
  86. } //namespace nvtt