ETC_Compress.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /******************************************************************************
  2. Have to keep this as a separate file, so it won't be linked if unused.
  3. Because it's linked separately, its name can't include spaces (due to Android building toolchain).
  4. /******************************************************************************/
  5. #include "stdafx.h"
  6. /******************************************************************************/
  7. #define ETC_LIB_ISPC 1 // only ETC1 compression, quality is good and comparable to ETC_LIB_ETCPACK with quality=0, but much faster
  8. #define ETC_LIB_RG 2 // only ETC1
  9. #define ETC_LIB_ETCPACK 3
  10. #define ETC1_ENC ETC_LIB_ISPC
  11. #define ETC2_ENC ETC_LIB_ETCPACK
  12. #include "../../../../ThirdPartyLibs/begin.h"
  13. #if ETC1_ENC==ETC_LIB_ISPC
  14. #ifndef ISPC_C__ESENTHEL_THIRDPARTYLIBS_BC7_ISPC_TEXCOMP_KERNEL_ISPC_H // this helps on Android where this header could have been already included due to files packed to one CPP file
  15. #include "../../../../ThirdPartyLibs/BC7/ispc_texcomp/ispc_texcomp.h"
  16. #endif
  17. #endif
  18. #if ETC1_ENC==ETC_LIB_RG
  19. #include "../../../../ThirdPartyLibs/RG-ETC1/rg_etc1.cpp"
  20. #endif
  21. #if ETC1_ENC==ETC_LIB_ETCPACK || ETC2_ENC==ETC_LIB_ETCPACK
  22. namespace ETCPACK
  23. {
  24. #define EXHAUSTIVE_CODE_ACTIVE 0 // disable to reduce code size and because this mode is too slow
  25. #define printf(x,...)
  26. #define exit(x)
  27. #pragma warning(push)
  28. #pragma warning(disable:4390) // ';': empty controlled statement found; is this the intent?
  29. #pragma runtime_checks("", off)
  30. #include "../../../../ThirdPartyLibs/ETCPack/source/etcdec.cxx"
  31. #undef exit
  32. #include "../../../../ThirdPartyLibs/ETCPack/source/etcpack.cxx"
  33. #pragma runtime_checks("", restore)
  34. #pragma warning(pop)
  35. #undef R
  36. #undef G
  37. #undef B
  38. #undef RED
  39. #undef GREEN
  40. #undef BLUE
  41. #undef SHIFT
  42. #undef MASK
  43. #if ETC2_ENC==ETC_LIB_ETCPACK
  44. static struct ETCInit
  45. {
  46. ETCInit() {setupAlphaTable();}
  47. }EI;
  48. #endif
  49. }
  50. #endif
  51. #include "../../../../ThirdPartyLibs/end.h"
  52. /******************************************************************************/
  53. namespace EE{
  54. /******************************************************************************/
  55. Bool _CompressETC(C Image &src, Image &dest, Int quality, Bool perceptual)
  56. {
  57. Bool ok=false, etc1=(dest.hwType()==IMAGE_ETC1);
  58. if(src.d()==dest.d())
  59. if(etc1 || dest.hwType()==IMAGE_ETC2 || dest.hwType()==IMAGE_ETC2_A1 || dest.hwType()==IMAGE_ETC2_A8)
  60. {
  61. Image temp; C Image *s=&src;
  62. if(etc1 && ETC1_ENC==ETC_LIB_ISPC)
  63. {
  64. if(s->hwType()!=IMAGE_R8G8B8A8 || s->w()!=dest.hwW() || s->h()!=dest.hwH()) // ISPC requires R8G8B8A8 format
  65. {
  66. if(s->copyTry(temp, dest.hwW(), dest.hwH(), -1, IMAGE_R8G8B8A8, IMAGE_SOFT, 1, FILTER_NO_STRETCH, true))s=&temp;else return false; // we need to cover the area for entire HW size, to process partial blocks too
  67. }
  68. }
  69. if(s->lockRead())
  70. {
  71. if(dest.lock(LOCK_WRITE))
  72. {
  73. ok=true;
  74. Int x_blocks=dest.hwW()/4, // operate on HW size to process partial and Pow2Padded blocks too
  75. y_blocks=dest.hwH()/4;
  76. #if ETC1_ENC==ETC_LIB_ISPC // ISPC
  77. Bool fast;
  78. etc_enc_settings settings;
  79. rgba_surface surf;
  80. #endif
  81. #if ETC1_ENC==ETC_LIB_RG // RG
  82. rg_etc1::etc1_pack_params pack;
  83. #endif
  84. #if ETC1_ENC==ETC_LIB_ETCPACK || ETC2_ENC==ETC_LIB_ETCPACK // ETCPACK
  85. Int block_size;
  86. #endif
  87. #if ETC1_ENC==ETC_LIB_ISPC // ISPC
  88. if(etc1 && ETC1_ENC==ETC_LIB_ISPC)
  89. {
  90. fast=(dest.pitch()==x_blocks*8);
  91. GetProfile_etc_slow(&settings);
  92. surf.width =s->w ();
  93. surf.height=s->h ();
  94. surf.stride=s->pitch();
  95. }else
  96. #endif
  97. #if ETC1_ENC==ETC_LIB_RG // RG
  98. if(etc1 && ETC1_ENC==ETC_LIB_RG)
  99. {
  100. rg_etc1::pack_etc1_block_init();
  101. switch(quality)
  102. {
  103. case 0: pack.m_quality=rg_etc1::cLowQuality ; break; // makes gradients look bad
  104. default: case 1: pack.m_quality=rg_etc1::cMediumQuality; break; // much faster than high and almost as good
  105. case 2: pack.m_quality=rg_etc1::cHighQuality ; break; // very slow
  106. }
  107. pack.m_dithering=false; // dithering here is actually very bad
  108. }else
  109. #endif
  110. #if ETC1_ENC==ETC_LIB_ETCPACK || ETC2_ENC==ETC_LIB_ETCPACK // ETCPACK
  111. if(etc1 ? ETC1_ENC==ETC_LIB_ETCPACK : ETC2_ENC==ETC_LIB_ETCPACK)
  112. {
  113. if(quality<0)quality=0; // default to 0 as 1 is just too slow
  114. quality=Mid(quality, 0, EXHAUSTIVE_CODE_ACTIVE)*2+Mid(perceptual, 0, 1);
  115. block_size=ImageTI[dest.hwType()].bit_pp*2;
  116. }else
  117. #endif
  118. {}
  119. REPD(z, dest.d())
  120. {
  121. // compress
  122. #if ETC1_ENC==ETC_LIB_ISPC // ISPC
  123. if(etc1 && ETC1_ENC==ETC_LIB_ISPC)
  124. {
  125. surf.ptr=ConstCast(s->data() + z*s->pitch2());
  126. if(fast)CompressBlocksETC1(&surf, dest.data()+z*dest.pitch2(), &settings);else
  127. {
  128. Byte *d=dest.data() + z*dest.pitch2();
  129. surf.height=4;
  130. REP(y_blocks)
  131. {
  132. CompressBlocksETC1(&surf, d, &settings);
  133. surf.ptr+=s ->pitch()*4;
  134. d +=dest.pitch();
  135. }
  136. }
  137. }else
  138. #endif
  139. #if ETC1_ENC==ETC_LIB_RG // RG
  140. if(etc1 && ETC1_ENC==ETC_LIB_RG)
  141. {
  142. REPD(by, y_blocks)
  143. {
  144. Int py=by*4, yo[4]; REPAO(yo)=Min(py+i, src.h()-1); // use clamping to avoid black borders
  145. Byte *dest_data=dest.data() + by*dest.pitch() + z*dest.pitch2();
  146. REPD(bx, x_blocks)
  147. {
  148. Int px=bx*4, xo[4]; REPAO(xo)=Min(px+i, src.w()-1); // use clamping to avoid black borders
  149. Color rgba[4][4]; src.gather(&rgba[0][0], xo, Elms(xo), yo, Elms(yo), &z, 1);
  150. rg_etc1::pack_etc1_block((UInt*)(dest_data + bx*8), (UInt*)rgba, pack);
  151. }
  152. }
  153. }else
  154. #endif
  155. #if ETC1_ENC==ETC_LIB_ETCPACK || ETC2_ENC==ETC_LIB_ETCPACK // ETCPACK
  156. if(etc1 ? ETC1_ENC==ETC_LIB_ETCPACK : ETC2_ENC==ETC_LIB_ETCPACK)
  157. {
  158. REPD(by, y_blocks)
  159. {
  160. Int py=by*4, yo[4]; REPAO(yo)=Min(py+i, src.h()-1); // use clamping to avoid black borders
  161. Byte *dest_data=dest.data() + by*dest.pitch() + z*dest.pitch2();
  162. REPD(bx, x_blocks)
  163. {
  164. Int px=bx*4, xo[4]; REPAO(xo)=Min(px+i, src.w()-1); // use clamping to avoid black borders
  165. VecB rgb [4][4];
  166. Color rgba[4][4];
  167. Byte a[4][4];
  168. UInt *d=(UInt*)(dest_data + bx*block_size);
  169. Color temp[4][4];
  170. switch(dest.hwType())
  171. {
  172. case IMAGE_ETC1: src.gather(&rgb[0][0], xo, Elms(xo), yo, Elms(yo), &z, 1); switch(quality)
  173. {
  174. case 0: ETCPACK::compressBlockDiffFlipFast (rgb[0][0].c, temp[0][0].c, 4, 4, 0, 0, d[0], d[1]); break;
  175. case 1: ETCPACK::compressBlockDiffFlipFastPerceptual (rgb[0][0].c, temp[0][0].c, 4, 4, 0, 0, d[0], d[1]); break;
  176. #if EXHAUSTIVE_CODE_ACTIVE
  177. case 2: ETCPACK::compressBlockETC1Exhaustive (rgb[0][0].c, temp[0][0].c, 4, 4, 0, 0, d[0], d[1]); break;
  178. case 3: ETCPACK::compressBlockETC1ExhaustivePerceptual(rgb[0][0].c, temp[0][0].c, 4, 4, 0, 0, d[0], d[1]); break;
  179. #endif
  180. }break;
  181. case IMAGE_ETC2: src.gather(&rgb[0][0], xo, Elms(xo), yo, Elms(yo), &z, 1); switch(quality)
  182. {
  183. case 0: ETCPACK::compressBlockETC2Fast (rgb[0][0].c, null, temp[0][0].c, 4, 4, 0, 0, d[0], d[1], ETCPACK::ETC2PACKAGE_RGB_NO_MIPMAPS); break;
  184. case 1: ETCPACK::compressBlockETC2FastPerceptual (rgb[0][0].c, temp[0][0].c, 4, 4, 0, 0, d[0], d[1]); break;
  185. #if EXHAUSTIVE_CODE_ACTIVE
  186. case 2: ETCPACK::compressBlockETC2Exhaustive (rgb[0][0].c, temp[0][0].c, 4, 4, 0, 0, d[0], d[1]); break;
  187. case 3: ETCPACK::compressBlockETC2ExhaustivePerceptual(rgb[0][0].c, temp[0][0].c, 4, 4, 0, 0, d[0], d[1]); break;
  188. #endif
  189. }break;
  190. case IMAGE_ETC2_A1:
  191. {
  192. src.gather(&rgba[0][0], xo, Elms(xo), yo, Elms(yo), &z, 1);
  193. REPD(y, 4)
  194. REPD(x, 4)
  195. {
  196. rgb[y][x]= rgba[y][x].v3;
  197. a [y][x]=((rgba[y][x].a>=128) ? 255 : 0); // manually adjust because compressor has issues
  198. }
  199. ETCPACK::compressBlockETC2Fast(rgb[0][0].c, &a[0][0], temp[0][0].c, 4, 4, 0, 0, d[0], d[1], ETCPACK::ETC2PACKAGE_RGBA1_NO_MIPMAPS);
  200. }break;
  201. case IMAGE_ETC2_A8:
  202. {
  203. src.gather(&rgba[0][0], xo, Elms(xo), yo, Elms(yo), &z, 1);
  204. REPD(y, 4)
  205. REPD(x, 4)
  206. {
  207. rgb[y][x]=rgba[y][x].v3;
  208. a [y][x]=rgba[y][x].a;
  209. }
  210. switch(quality)
  211. {
  212. case 0: case 1: ETCPACK::compressBlockAlphaFast(&a[0][0], 0, 0, 4, 4, (Byte*)d); break;
  213. case 2: case 3: ETCPACK::compressBlockAlphaSlow(&a[0][0], 0, 0, 4, 4, (Byte*)d); break;
  214. }
  215. d+=2;
  216. switch(quality)
  217. {
  218. case 0: ETCPACK::compressBlockETC2Fast (rgb[0][0].c, null, temp[0][0].c, 4, 4, 0, 0, d[0], d[1], ETCPACK::ETC2PACKAGE_RGB_NO_MIPMAPS); break;
  219. case 1: ETCPACK::compressBlockETC2FastPerceptual (rgb[0][0].c, temp[0][0].c, 4, 4, 0, 0, d[0], d[1]); break;
  220. #if EXHAUSTIVE_CODE_ACTIVE
  221. case 2: ETCPACK::compressBlockETC2Exhaustive (rgb[0][0].c, temp[0][0].c, 4, 4, 0, 0, d[0], d[1]); break;
  222. case 3: ETCPACK::compressBlockETC2ExhaustivePerceptual(rgb[0][0].c, temp[0][0].c, 4, 4, 0, 0, d[0], d[1]); break;
  223. #endif
  224. }
  225. }break;
  226. }
  227. SwapEndian(d[0]);
  228. SwapEndian(d[1]);
  229. }
  230. }
  231. }else
  232. #endif
  233. {}
  234. if(!ok)break;
  235. }
  236. dest.unlock();
  237. }
  238. s->unlock();
  239. }
  240. }
  241. return ok;
  242. }
  243. /******************************************************************************/
  244. }
  245. /******************************************************************************/