nvtt.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "nvtt.h"
  6. #include <string.h>
  7. #include <bx/uint32_t.h>
  8. #include "bc6h/zoh.h"
  9. #include "bc7/avpcl.h"
  10. #include "nvmath/vector.inl"
  11. NVCORE_API int nvAbort(const char *, const char *, int , const char *, const char *, ...)
  12. {
  13. abort();
  14. return 0;
  15. }
  16. namespace nvtt
  17. {
  18. using namespace nv;
  19. void compressBC6H(const void* _input, uint32_t _width, uint32_t _height, uint32_t _stride, void* _output)
  20. {
  21. const uint8_t* src = (const uint8_t*)_input;
  22. char* dst = (char*)_output;
  23. for (uint32_t yy = 0; yy < _height; yy += 4)
  24. {
  25. for (uint32_t xx = 0; xx < _width; xx += 4)
  26. {
  27. const Vector4* rgba = (const Vector4*)&src[yy*_stride + xx*sizeof(float)*4];
  28. ZOH::Utils::FORMAT = ZOH::UNSIGNED_F16;
  29. ZOH::Tile zohTile(4, 4);
  30. memset(zohTile.data, 0, sizeof(zohTile.data) );
  31. memset(zohTile.importance_map, 0, sizeof(zohTile.importance_map) );
  32. for (uint32_t blockY = 0; blockY < 4; ++blockY)
  33. {
  34. for (uint32_t blockX = 0; blockX < 4; ++blockX)
  35. {
  36. Vector4 color = rgba[blockY*4 + blockX];
  37. uint16 rHalf = bx::halfFromFloat(color.x);
  38. uint16 gHalf = bx::halfFromFloat(color.y);
  39. uint16 bHalf = bx::halfFromFloat(color.z);
  40. zohTile.data[blockY][blockX].x = ZOH::Tile::half2float(rHalf);
  41. zohTile.data[blockY][blockX].y = ZOH::Tile::half2float(gHalf);
  42. zohTile.data[blockY][blockX].z = ZOH::Tile::half2float(bHalf);
  43. zohTile.importance_map[blockY][blockX] = 1.0f;
  44. }
  45. }
  46. ZOH::compress(zohTile, &dst[( (yy*_width) + xx)/4 * 16]);
  47. }
  48. }
  49. }
  50. void compressBC7(const void* _input, uint32_t _width, uint32_t _height, uint32_t _stride, void* _output)
  51. {
  52. const uint8_t* src = (const uint8_t*)_input;
  53. char* dst = (char*)_output;
  54. for (uint32_t yy = 0; yy < _height; yy += 4)
  55. {
  56. for (uint32_t xx = 0; xx < _width; xx += 4)
  57. {
  58. const Vector4* rgba = (const Vector4*)&src[yy*_stride + xx*sizeof(float)*4];
  59. AVPCL::mode_rgb = false;
  60. AVPCL::flag_premult = false;
  61. AVPCL::flag_nonuniform = false;
  62. AVPCL::flag_nonuniform_ati = false;
  63. AVPCL::Tile avpclTile(4, 4);
  64. memset(avpclTile.data, 0, sizeof(avpclTile.data) );
  65. for (uint32_t blockY = 0; blockY < 4; ++blockY)
  66. {
  67. for (uint32_t blockX = 0; blockX < 4; ++blockX)
  68. {
  69. Vector4 color = rgba[blockY*4 + blockX];
  70. avpclTile.data[blockY][blockX] = color * 255.0f;
  71. avpclTile.importance_map[blockY][blockX] = 1.0f;
  72. }
  73. }
  74. AVPCL::compress(avpclTile, &dst[( (yy*_width) + xx)/4 * 16]);
  75. }
  76. }
  77. }
  78. } //namespace nvtt