ConvectionKernels.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Convection Texture Tools
  3. Copyright (c) 2018 Eric Lasota
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject
  10. to the following conditions:
  11. The above copyright notice and this permission notice shall be included
  12. in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  17. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  18. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  19. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #pragma once
  22. #ifndef __CVTT_CONVECTION_KERNELS__
  23. #define __CVTT_CONVECTION_KERNELS__
  24. #include <stdint.h>
  25. namespace cvtt
  26. {
  27. namespace Flags
  28. {
  29. // Enable partitioned modes in BC7 encoding (slower, better quality)
  30. const uint32_t BC7_EnablePartitioning = 0x001;
  31. // Enable 3-partition modes in BC7 encoding (slower, better quality, requires BC7_EnablePartitioning)
  32. const uint32_t BC7_Enable3Subsets = 0x002;
  33. // Enable dual-plane modes in BC7 encoding (slower, better quality)
  34. const uint32_t BC7_EnableDualPlane = 0x004;
  35. // Use fast indexing in BC7 encoding (about 2x faster, slightly worse quality)
  36. const uint32_t BC7_FastIndexing = 0x008;
  37. // Try precomputed single-color lookups where applicable (slightly slower, small quality increase on specific blocks)
  38. const uint32_t BC7_TrySingleColor = 0x010;
  39. // Don't allow non-zero or non-max alpha values in blocks that only contain one or the other
  40. const uint32_t BC7_RespectPunchThrough = 0x020;
  41. // Use fast indexing in HDR formats (faster, worse quality)
  42. const uint32_t BC6H_FastIndexing = 0x040;
  43. // Exhaustive search RGB orderings when encoding BC1-BC3 (much slower, better quality)
  44. const uint32_t S3TC_Exhaustive = 0x080;
  45. // Penalize distant endpoints, improving quality on inaccurate GPU decoders
  46. const uint32_t S3TC_Paranoid = 0x100;
  47. // Uniform color channel importance
  48. const uint32_t Uniform = 0x200;
  49. // Misc useful default flag combinations
  50. const uint32_t Fastest = (BC6H_FastIndexing | S3TC_Paranoid);
  51. const uint32_t Faster = (BC7_EnableDualPlane | BC6H_FastIndexing | S3TC_Paranoid);
  52. const uint32_t Fast = (BC7_EnablePartitioning | BC7_EnableDualPlane | BC7_FastIndexing | S3TC_Paranoid);
  53. const uint32_t Default = (BC7_EnablePartitioning | BC7_EnableDualPlane | BC7_Enable3Subsets | BC7_FastIndexing | S3TC_Paranoid);
  54. const uint32_t Better = (BC7_EnablePartitioning | BC7_EnableDualPlane | BC7_Enable3Subsets | S3TC_Paranoid | S3TC_Exhaustive);
  55. const uint32_t Ultra = (BC7_EnablePartitioning | BC7_EnableDualPlane | BC7_Enable3Subsets | BC7_TrySingleColor | S3TC_Paranoid | S3TC_Exhaustive);
  56. }
  57. const unsigned int NumParallelBlocks = 8;
  58. struct Options
  59. {
  60. uint32_t flags; // Bitmask of cvtt::Flags values
  61. float threshold; // Alpha test threshold for BC1
  62. float redWeight; // Red channel importance
  63. float greenWeight; // Green channel importance
  64. float blueWeight; // Blue channel importance
  65. float alphaWeight; // Alpha channel importance
  66. int refineRoundsBC7; // Number of refine rounds for BC7
  67. int refineRoundsBC6H; // Number of refine rounds for BC6H (max 3)
  68. int refineRoundsIIC; // Number of refine rounds for independent interpolated channels (BC3 alpha, BC4, BC5)
  69. int refineRoundsS3TC; // Number of refine rounds for S3TC RGB
  70. int seedPoints; // Number of seed points (min 1, max 4)
  71. Options()
  72. : flags(Flags::Default)
  73. , threshold(0.5f)
  74. , redWeight(0.2125f / 0.7154f)
  75. , greenWeight(1.0f)
  76. , blueWeight(0.0721f / 0.7154f)
  77. , alphaWeight(1.0f)
  78. , refineRoundsBC7(2)
  79. , refineRoundsBC6H(3)
  80. , refineRoundsIIC(8)
  81. , refineRoundsS3TC(2)
  82. , seedPoints(4)
  83. {
  84. }
  85. };
  86. // RGBA input block for unsigned 8-bit formats
  87. struct PixelBlockU8
  88. {
  89. uint8_t m_pixels[16][4];
  90. };
  91. // RGBA input block for signed 8-bit formats
  92. struct PixelBlockS8
  93. {
  94. int8_t m_pixels[16][4];
  95. };
  96. // RGBA input block for half-precision float formats (bit-cast to int16_t)
  97. struct PixelBlockF16
  98. {
  99. int16_t m_pixels[16][4];
  100. };
  101. namespace Kernels
  102. {
  103. // NOTE: All functions accept and output NumParallelBlocks blocks at once
  104. void EncodeBC1(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options);
  105. void EncodeBC2(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options);
  106. void EncodeBC3(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options);
  107. void EncodeBC4U(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options);
  108. void EncodeBC4S(uint8_t *pBC, const PixelBlockS8 *pBlocks, const Options &options);
  109. void EncodeBC5U(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options);
  110. void EncodeBC5S(uint8_t *pBC, const PixelBlockS8 *pBlocks, const Options &options);
  111. void EncodeBC6HU(uint8_t *pBC, const PixelBlockF16 *pBlocks, const Options &options);
  112. void EncodeBC6HS(uint8_t *pBC, const PixelBlockF16 *pBlocks, const Options &options);
  113. void EncodeBC7(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options);
  114. void DecodeBC6HU(PixelBlockF16 *pBlocks, const uint8_t *pBC);
  115. void DecodeBC6HS(PixelBlockF16 *pBlocks, const uint8_t *pBC);
  116. void DecodeBC7(PixelBlockU8 *pBlocks, const uint8_t *pBC);
  117. }
  118. }
  119. #endif