basisu_comp.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. // basisu_comp.cpp
  2. // Copyright (C) 2019 Binomial LLC. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "basisu_comp.h"
  16. #include "basisu_enc.h"
  17. #include <unordered_set>
  18. #define BASISU_USE_STB_IMAGE_RESIZE_FOR_MIPMAP_GEN 0
  19. #define DEBUG_CROP_TEXTURE_TO_64x64 (0)
  20. #define DEBUG_RESIZE_TEXTURE (0)
  21. #define DEBUG_EXTRACT_SINGLE_BLOCK (0)
  22. namespace basisu
  23. {
  24. basis_compressor::basis_compressor() :
  25. m_total_blocks(0),
  26. m_auto_global_sel_pal(false),
  27. m_basis_file_size(0),
  28. m_basis_bits_per_texel(0),
  29. m_any_source_image_has_alpha(false)
  30. {
  31. debug_printf("basis_compressor::basis_compressor\n");
  32. }
  33. bool basis_compressor::init(const basis_compressor_params &params)
  34. {
  35. debug_printf("basis_compressor::init\n");
  36. m_params = params;
  37. if (m_params.m_debug)
  38. {
  39. debug_printf("basis_compressor::init:\n");
  40. #define PRINT_BOOL_VALUE(v) debug_printf("%s: %u %u\n", BASISU_STRINGIZE2(v), static_cast<int>(m_params.v), m_params.v.was_changed());
  41. #define PRINT_INT_VALUE(v) debug_printf("%s: %i %u\n", BASISU_STRINGIZE2(v), static_cast<int>(m_params.v), m_params.v.was_changed());
  42. #define PRINT_UINT_VALUE(v) debug_printf("%s: %u %u\n", BASISU_STRINGIZE2(v), static_cast<uint32_t>(m_params.v), m_params.v.was_changed());
  43. #define PRINT_FLOAT_VALUE(v) debug_printf("%s: %f %u\n", BASISU_STRINGIZE2(v), static_cast<float>(m_params.v), m_params.v.was_changed());
  44. debug_printf("Has global selector codebook: %i\n", m_params.m_pSel_codebook != nullptr);
  45. debug_printf("Source images: %u, source filenames: %u, source alpha filenames: %i\n",
  46. (uint32_t)m_params.m_source_images.size(), (uint32_t)m_params.m_source_filenames.size(), (uint32_t)m_params.m_source_alpha_filenames.size());
  47. PRINT_BOOL_VALUE(m_y_flip);
  48. PRINT_BOOL_VALUE(m_debug);
  49. PRINT_BOOL_VALUE(m_debug_images);
  50. PRINT_BOOL_VALUE(m_global_sel_pal);
  51. PRINT_BOOL_VALUE(m_no_auto_global_sel_pal);
  52. PRINT_BOOL_VALUE(m_compression_level);
  53. PRINT_BOOL_VALUE(m_no_hybrid_sel_cb);
  54. PRINT_BOOL_VALUE(m_perceptual);
  55. PRINT_BOOL_VALUE(m_no_endpoint_rdo);
  56. PRINT_BOOL_VALUE(m_no_selector_rdo);
  57. PRINT_BOOL_VALUE(m_read_source_images);
  58. PRINT_BOOL_VALUE(m_write_output_basis_files);
  59. PRINT_BOOL_VALUE(m_compute_stats);
  60. PRINT_BOOL_VALUE(m_check_for_alpha)
  61. PRINT_BOOL_VALUE(m_force_alpha)
  62. PRINT_BOOL_VALUE(m_seperate_rg_to_color_alpha);
  63. PRINT_BOOL_VALUE(m_multithreading);
  64. PRINT_BOOL_VALUE(m_disable_hierarchical_endpoint_codebooks);
  65. PRINT_FLOAT_VALUE(m_hybrid_sel_cb_quality_thresh);
  66. PRINT_INT_VALUE(m_global_pal_bits);
  67. PRINT_INT_VALUE(m_global_mod_bits);
  68. PRINT_FLOAT_VALUE(m_endpoint_rdo_thresh);
  69. PRINT_FLOAT_VALUE(m_selector_rdo_thresh);
  70. PRINT_BOOL_VALUE(m_mip_gen);
  71. PRINT_BOOL_VALUE(m_mip_renormalize);
  72. PRINT_BOOL_VALUE(m_mip_wrapping);
  73. PRINT_BOOL_VALUE(m_mip_srgb);
  74. PRINT_FLOAT_VALUE(m_mip_premultiplied);
  75. PRINT_FLOAT_VALUE(m_mip_scale);
  76. PRINT_INT_VALUE(m_mip_smallest_dimension);
  77. debug_printf("m_mip_filter: %s\n", m_params.m_mip_filter.c_str());
  78. debug_printf("m_max_endpoint_clusters: %u\n", m_params.m_max_endpoint_clusters);
  79. debug_printf("m_max_selector_clusters: %u\n", m_params.m_max_selector_clusters);
  80. debug_printf("m_quality_level: %i\n", m_params.m_quality_level);
  81. debug_printf("m_tex_type: %u\n", m_params.m_tex_type);
  82. debug_printf("m_userdata0: 0x%X, m_userdata1: 0x%X\n", m_params.m_userdata0, m_params.m_userdata1);
  83. debug_printf("m_us_per_frame: %i (%f fps)\n", m_params.m_us_per_frame, m_params.m_us_per_frame ? 1.0f / (m_params.m_us_per_frame / 1000000.0f) : 0);
  84. #undef PRINT_BOOL_VALUE
  85. #undef PRINT_INT_VALUE
  86. #undef PRINT_UINT_VALUE
  87. #undef PRINT_FLOAT_VALUE
  88. }
  89. if ((m_params.m_read_source_images) && (!m_params.m_source_filenames.size()))
  90. {
  91. assert(0);
  92. return false;
  93. }
  94. return true;
  95. }
  96. basis_compressor::error_code basis_compressor::process()
  97. {
  98. debug_printf("basis_compressor::process\n");
  99. if (!read_source_images())
  100. return cECFailedReadingSourceImages;
  101. if (!validate_texture_type_constraints())
  102. return cECFailedValidating;
  103. if (!process_frontend())
  104. return cECFailedFrontEnd;
  105. if (!extract_frontend_texture_data())
  106. return cECFailedFontendExtract;
  107. if (!process_backend())
  108. return cECFailedBackend;
  109. if (!create_basis_file_and_transcode())
  110. return cECFailedCreateBasisFile;
  111. if (!write_output_files_and_compute_stats())
  112. return cECFailedWritingOutput;
  113. return cECSuccess;
  114. }
  115. bool basis_compressor::generate_mipmaps(const image &img, std::vector<image> &mips, bool has_alpha)
  116. {
  117. debug_printf("basis_compressor::generate_mipmaps\n");
  118. uint32_t total_levels = 1;
  119. uint32_t w = img.get_width(), h = img.get_height();
  120. while (maximum<uint32_t>(w, h) > (uint32_t)m_params.m_mip_smallest_dimension)
  121. {
  122. w = maximum(w >> 1U, 1U);
  123. h = maximum(h >> 1U, 1U);
  124. total_levels++;
  125. }
  126. #if BASISU_USE_STB_IMAGE_RESIZE_FOR_MIPMAP_GEN
  127. // Requires stb_image_resize
  128. stbir_filter filter = STBIR_FILTER_DEFAULT;
  129. if (m_params.m_mip_filter == "box")
  130. filter = STBIR_FILTER_BOX;
  131. else if (m_params.m_mip_filter == "triangle")
  132. filter = STBIR_FILTER_TRIANGLE;
  133. else if (m_params.m_mip_filter == "cubic")
  134. filter = STBIR_FILTER_CUBICBSPLINE;
  135. else if (m_params.m_mip_filter == "catmull")
  136. filter = STBIR_FILTER_CATMULLROM;
  137. else if (m_params.m_mip_filter == "mitchell")
  138. filter = STBIR_FILTER_MITCHELL;
  139. for (uint32_t level = 1; level < total_levels; level++)
  140. {
  141. const uint32_t level_width = maximum<uint32_t>(1, img.get_width() >> level);
  142. const uint32_t level_height = maximum<uint32_t>(1, img.get_height() >> level);
  143. image &level_img = *enlarge_vector(mips, 1);
  144. level_img.resize(level_width, level_height);
  145. int result = stbir_resize_uint8_generic(
  146. (const uint8_t *)img.get_ptr(), img.get_width(), img.get_height(), img.get_pitch() * sizeof(color_rgba),
  147. (uint8_t *)level_img.get_ptr(), level_img.get_width(), level_img.get_height(), level_img.get_pitch() * sizeof(color_rgba),
  148. has_alpha ? 4 : 3, has_alpha ? 3 : STBIR_ALPHA_CHANNEL_NONE, m_params.m_mip_premultiplied ? STBIR_FLAG_ALPHA_PREMULTIPLIED : 0,
  149. m_params.m_mip_wrapping ? STBIR_EDGE_WRAP : STBIR_EDGE_CLAMP, filter, m_params.m_mip_srgb ? STBIR_COLORSPACE_SRGB : STBIR_COLORSPACE_LINEAR,
  150. nullptr);
  151. if (result == 0)
  152. {
  153. error_printf("basis_compressor::generate_mipmaps: stbir_resize_uint8_generic() failed!\n");
  154. return false;
  155. }
  156. if (m_params.m_mip_renormalize)
  157. level_img.renormalize_normal_map();
  158. }
  159. #else
  160. for (uint32_t level = 1; level < total_levels; level++)
  161. {
  162. const uint32_t level_width = maximum<uint32_t>(1, img.get_width() >> level);
  163. const uint32_t level_height = maximum<uint32_t>(1, img.get_height() >> level);
  164. image &level_img = *enlarge_vector(mips, 1);
  165. level_img.resize(level_width, level_height);
  166. bool status = image_resample(img, level_img, m_params.m_mip_srgb, m_params.m_mip_filter.c_str(), m_params.m_mip_scale, m_params.m_mip_wrapping, 0, has_alpha ? 4 : 3);
  167. if (!status)
  168. {
  169. error_printf("basis_compressor::generate_mipmaps: image_resample() failed!\n");
  170. return false;
  171. }
  172. if (m_params.m_mip_renormalize)
  173. level_img.renormalize_normal_map();
  174. }
  175. #endif
  176. return true;
  177. }
  178. bool basis_compressor::read_source_images()
  179. {
  180. debug_printf("basis_compressor::read_source_images\n");
  181. const uint32_t total_source_files = m_params.m_read_source_images ? (uint32_t)m_params.m_source_filenames.size() : (uint32_t)m_params.m_source_images.size();
  182. if (!total_source_files)
  183. return false;
  184. m_stats.resize(0);
  185. m_slice_descs.resize(0);
  186. m_slice_images.resize(0);
  187. m_total_blocks = 0;
  188. uint32_t total_macroblocks = 0;
  189. m_any_source_image_has_alpha = false;
  190. std::vector<image> source_images;
  191. std::vector<std::string> source_filenames;
  192. // First load all source images, and determine if any have an alpha channel.
  193. for (uint32_t source_file_index = 0; source_file_index < total_source_files; source_file_index++)
  194. {
  195. const char *pSource_filename = "";
  196. image file_image;
  197. if (m_params.m_read_source_images)
  198. {
  199. pSource_filename = m_params.m_source_filenames[source_file_index].c_str();
  200. // Load the source image
  201. if (!load_png(pSource_filename, file_image))
  202. {
  203. error_printf("Failed reading source image: %s\n", pSource_filename);
  204. return false;
  205. }
  206. printf("Read source image \"%s\", %ux%u\n", pSource_filename, file_image.get_width(), file_image.get_height());
  207. // Optionally load another image and put a grayscale version of it into the alpha channel.
  208. if ((source_file_index < m_params.m_source_alpha_filenames.size()) && (m_params.m_source_alpha_filenames[source_file_index].size()))
  209. {
  210. const char *pSource_alpha_image = m_params.m_source_alpha_filenames[source_file_index].c_str();
  211. image alpha_data;
  212. if (!load_png(pSource_alpha_image, alpha_data))
  213. {
  214. error_printf("Failed reading source image: %s\n", pSource_alpha_image);
  215. return false;
  216. }
  217. printf("Read source alpha image \"%s\", %ux%u\n", pSource_alpha_image, alpha_data.get_width(), alpha_data.get_height());
  218. alpha_data.crop(file_image.get_width(), file_image.get_height());
  219. for (uint32_t y = 0; y < file_image.get_height(); y++)
  220. for (uint32_t x = 0; x < file_image.get_width(); x++)
  221. file_image(x, y).a = (uint8_t)alpha_data(x, y).get_709_luma();
  222. }
  223. }
  224. else
  225. {
  226. file_image = m_params.m_source_images[source_file_index];
  227. }
  228. if (m_params.m_seperate_rg_to_color_alpha)
  229. {
  230. // Used for XY normal maps in RG - puts X in color, Y in alpha
  231. for (uint32_t y = 0; y < file_image.get_height(); y++)
  232. for (uint32_t x = 0; x < file_image.get_width(); x++)
  233. {
  234. const color_rgba &c = file_image(x, y);
  235. file_image(x, y).set_noclamp_rgba(c.r, c.r, c.r, c.g);
  236. }
  237. }
  238. bool has_alpha = false;
  239. if ((m_params.m_force_alpha) || (m_params.m_seperate_rg_to_color_alpha))
  240. has_alpha = true;
  241. else if (!m_params.m_check_for_alpha)
  242. file_image.set_alpha(255);
  243. else if (file_image.has_alpha())
  244. has_alpha = true;
  245. if (has_alpha)
  246. m_any_source_image_has_alpha = true;
  247. debug_printf("Source image index %u filename %s %ux%u has alpha: %u\n", source_file_index, pSource_filename, file_image.get_width(), file_image.get_height(), has_alpha);
  248. if (m_params.m_y_flip)
  249. file_image.flip_y();
  250. #if DEBUG_EXTRACT_SINGLE_BLOCK
  251. image block_image(4, 4);
  252. const uint32_t block_x = 0;
  253. const uint32_t block_y = 0;
  254. block_image.blit(block_x * 4, block_y * 4, 4, 4, 0, 0, file_image, 0);
  255. file_image = block_image;
  256. #endif
  257. #if DEBUG_CROP_TEXTURE_TO_64x64
  258. file_image.resize(64, 64);
  259. #endif
  260. #if DEBUG_RESIZE_TEXTURE
  261. image temp_img((file_image.get_width() + 1) / 2, (file_image.get_height() + 1) / 2);
  262. image_resample(file_image, temp_img, m_params.m_perceptual, "kaiser");
  263. temp_img.swap(file_image);
  264. #endif
  265. if ((!file_image.get_width()) || (!file_image.get_height()))
  266. {
  267. error_printf("basis_compressor::read_source_images: Source image has a zero width and/or height!\n");
  268. return false;
  269. }
  270. if ((file_image.get_width() > BASISU_MAX_SUPPORTED_TEXTURE_DIMENSION) || (file_image.get_height() > BASISU_MAX_SUPPORTED_TEXTURE_DIMENSION))
  271. {
  272. error_printf("basis_compressor::read_source_images: Source image is too large!\n");
  273. return false;
  274. }
  275. source_images.push_back(file_image);
  276. source_filenames.push_back(pSource_filename);
  277. }
  278. debug_printf("Any source image has alpha: %u\n", m_any_source_image_has_alpha);
  279. for (uint32_t source_file_index = 0; source_file_index < total_source_files; source_file_index++)
  280. {
  281. image &file_image = source_images[source_file_index];
  282. const std::string &source_filename = source_filenames[source_file_index];
  283. std::vector<image> slices;
  284. slices.reserve(32);
  285. slices.push_back(file_image);
  286. if (m_params.m_mip_gen)
  287. {
  288. if (!generate_mipmaps(file_image, slices, m_any_source_image_has_alpha))
  289. return false;
  290. }
  291. uint_vec mip_indices(slices.size());
  292. for (uint32_t i = 0; i < slices.size(); i++)
  293. mip_indices[i] = i;
  294. if (m_any_source_image_has_alpha)
  295. {
  296. // If source has alpha, then even mips will have RGB, and odd mips will have alpha in RGB.
  297. std::vector<image> alpha_slices;
  298. uint_vec new_mip_indices;
  299. alpha_slices.reserve(slices.size() * 2);
  300. for (uint32_t i = 0; i < slices.size(); i++)
  301. {
  302. image lvl_rgb(slices[i]);
  303. image lvl_a(lvl_rgb);
  304. for (uint32_t y = 0; y < lvl_a.get_height(); y++)
  305. {
  306. for (uint32_t x = 0; x < lvl_a.get_width(); x++)
  307. {
  308. uint8_t a = lvl_a(x, y).a;
  309. lvl_a(x, y).set_noclamp_rgba(a, a, a, 255);
  310. }
  311. }
  312. lvl_rgb.set_alpha(255);
  313. alpha_slices.push_back(lvl_rgb);
  314. new_mip_indices.push_back(i);
  315. alpha_slices.push_back(lvl_a);
  316. new_mip_indices.push_back(i);
  317. }
  318. slices.swap(alpha_slices);
  319. mip_indices.swap(new_mip_indices);
  320. }
  321. assert(slices.size() == mip_indices.size());
  322. for (uint32_t slice_index = 0; slice_index < slices.size(); slice_index++)
  323. {
  324. const bool is_alpha_slice = m_any_source_image_has_alpha && ((slice_index & 1) != 0);
  325. image &slice_image = slices[slice_index];
  326. const uint32_t orig_width = slice_image.get_width();
  327. const uint32_t orig_height = slice_image.get_height();
  328. // Enlarge the source image to 4x4 block boundaries, duplicating edge pixels if necessary to avoid introducing extra colors into blocks.
  329. slice_image.crop_dup_borders(slice_image.get_block_width(4) * 4, slice_image.get_block_height(4) * 4);
  330. if (m_params.m_debug_images)
  331. {
  332. save_png(string_format("basis_debug_source_image_%u_%u.png", source_file_index, slice_index).c_str(), slice_image);
  333. }
  334. enlarge_vector(m_stats, 1);
  335. enlarge_vector(m_slice_images, 1);
  336. enlarge_vector(m_slice_descs, 1);
  337. const uint32_t dest_image_index = (uint32_t)m_stats.size() - 1;
  338. m_stats[dest_image_index].m_filename = source_filename.c_str();
  339. m_stats[dest_image_index].m_width = orig_width;
  340. m_stats[dest_image_index].m_height = orig_height;
  341. m_slice_images[dest_image_index] = slice_image;
  342. debug_printf("****** Slice %u: mip %u, alpha_slice: %u, filename: \"%s\", original: %ux%u actual: %ux%u\n", m_slice_descs.size() - 1, mip_indices[slice_index], is_alpha_slice, source_filename.c_str(), orig_width, orig_height, slice_image.get_width(), slice_image.get_height());
  343. basisu_backend_slice_desc &slice_desc = m_slice_descs[dest_image_index];
  344. slice_desc.m_first_block_index = m_total_blocks;
  345. slice_desc.m_orig_width = orig_width;
  346. slice_desc.m_orig_height = orig_height;
  347. slice_desc.m_width = slice_image.get_width();
  348. slice_desc.m_height = slice_image.get_height();
  349. slice_desc.m_num_blocks_x = slice_image.get_block_width(4);
  350. slice_desc.m_num_blocks_y = slice_image.get_block_height(4);
  351. slice_desc.m_num_macroblocks_x = (slice_desc.m_num_blocks_x + 1) >> 1;
  352. slice_desc.m_num_macroblocks_y = (slice_desc.m_num_blocks_y + 1) >> 1;
  353. slice_desc.m_source_file_index = source_file_index;
  354. slice_desc.m_mip_index = mip_indices[slice_index];
  355. slice_desc.m_alpha = is_alpha_slice;
  356. slice_desc.m_iframe = false;
  357. if (m_params.m_tex_type == basist::cBASISTexTypeVideoFrames)
  358. {
  359. slice_desc.m_iframe = (source_file_index == 0);
  360. }
  361. m_total_blocks += slice_desc.m_num_blocks_x * slice_desc.m_num_blocks_y;
  362. total_macroblocks += slice_desc.m_num_macroblocks_x * slice_desc.m_num_macroblocks_y;
  363. } // slice_index
  364. } // source_file_index
  365. debug_printf("Total blocks: %u, Total macroblocks: %u\n", m_total_blocks, total_macroblocks);
  366. // Make sure we don't have too many slices
  367. if (m_slice_descs.size() > BASISU_MAX_SLICES)
  368. {
  369. error_printf("Too many slices!\n");
  370. return false;
  371. }
  372. // Basic sanity check on the slices
  373. for (uint32_t i = 1; i < m_slice_descs.size(); i++)
  374. {
  375. const basisu_backend_slice_desc &prev_slice_desc = m_slice_descs[i - 1];
  376. const basisu_backend_slice_desc &slice_desc = m_slice_descs[i];
  377. // Make sure images are in order
  378. int image_delta = (int)slice_desc.m_source_file_index - (int)prev_slice_desc.m_source_file_index;
  379. if (image_delta > 1)
  380. return false;
  381. // Make sure mipmap levels are in order
  382. if (!image_delta)
  383. {
  384. int level_delta = (int)slice_desc.m_mip_index - (int)prev_slice_desc.m_mip_index;
  385. if (level_delta > 1)
  386. return false;
  387. }
  388. }
  389. printf("Total basis file slices: %u\n", (uint32_t)m_slice_descs.size());
  390. for (uint32_t i = 0; i < m_slice_descs.size(); i++)
  391. {
  392. const basisu_backend_slice_desc &slice_desc = m_slice_descs[i];
  393. printf("Slice: %u, alpha: %u, orig width/height: %ux%u, width/height: %ux%u, first_block: %u, image_index: %u, mip_level: %u, iframe: %u\n",
  394. i, slice_desc.m_alpha, slice_desc.m_orig_width, slice_desc.m_orig_height, slice_desc.m_width, slice_desc.m_height, slice_desc.m_first_block_index, slice_desc.m_source_file_index, slice_desc.m_mip_index, slice_desc.m_iframe);
  395. if (m_any_source_image_has_alpha)
  396. {
  397. // Alpha slices must be at odd slice indices
  398. if (slice_desc.m_alpha)
  399. {
  400. if ((i & 1) == 0)
  401. return false;
  402. const basisu_backend_slice_desc &prev_slice_desc = m_slice_descs[i - 1];
  403. // Make sure previous slice has this image's color data
  404. if (prev_slice_desc.m_source_file_index != slice_desc.m_source_file_index)
  405. return false;
  406. if (prev_slice_desc.m_alpha)
  407. return false;
  408. if (prev_slice_desc.m_mip_index != slice_desc.m_mip_index)
  409. return false;
  410. if (prev_slice_desc.m_num_blocks_x != slice_desc.m_num_blocks_x)
  411. return false;
  412. if (prev_slice_desc.m_num_blocks_y != slice_desc.m_num_blocks_y)
  413. return false;
  414. }
  415. else if (i & 1)
  416. return false;
  417. }
  418. else if (slice_desc.m_alpha)
  419. {
  420. return false;
  421. }
  422. if ((slice_desc.m_orig_width > slice_desc.m_width) || (slice_desc.m_orig_height > slice_desc.m_height))
  423. return false;
  424. if ((slice_desc.m_source_file_index == 0) && (m_params.m_tex_type == basist::cBASISTexTypeVideoFrames))
  425. {
  426. if (!slice_desc.m_iframe)
  427. return false;
  428. }
  429. }
  430. return true;
  431. }
  432. // Do some basic validation for 2D arrays, cubemaps, video, and volumes.
  433. bool basis_compressor::validate_texture_type_constraints()
  434. {
  435. debug_printf("basis_compressor::validate_texture_type_constraints\n");
  436. // In 2D mode anything goes (each image may have a different resolution and # of mipmap levels).
  437. if (m_params.m_tex_type == basist::cBASISTexType2D)
  438. return true;
  439. uint32_t total_basis_images = 0;
  440. for (uint32_t slice_index = 0; slice_index < m_slice_images.size(); slice_index++)
  441. {
  442. const basisu_backend_slice_desc &slice_desc = m_slice_descs[slice_index];
  443. total_basis_images = maximum<uint32_t>(total_basis_images, slice_desc.m_source_file_index + 1);
  444. }
  445. if (m_params.m_tex_type == basist::cBASISTexTypeCubemapArray)
  446. {
  447. // For cubemaps, validate that the total # of Basis images is a multiple of 6.
  448. if ((total_basis_images % 6) != 0)
  449. {
  450. error_printf("basis_compressor::validate_texture_type_constraints: For cubemaps the total number of input images is not a multiple of 6!\n");
  451. return false;
  452. }
  453. }
  454. // Now validate that all the mip0's have the same dimensions, and that each image has the same # of mipmap levels.
  455. uint_vec image_mipmap_levels(total_basis_images);
  456. int width = -1, height = -1;
  457. for (uint32_t slice_index = 0; slice_index < m_slice_images.size(); slice_index++)
  458. {
  459. const basisu_backend_slice_desc &slice_desc = m_slice_descs[slice_index];
  460. image_mipmap_levels[slice_desc.m_source_file_index] = maximum(image_mipmap_levels[slice_desc.m_source_file_index], slice_desc.m_mip_index + 1);
  461. if (slice_desc.m_mip_index != 0)
  462. continue;
  463. if (width < 0)
  464. {
  465. width = slice_desc.m_orig_width;
  466. height = slice_desc.m_orig_height;
  467. }
  468. else if ((width != (int)slice_desc.m_orig_width) || (height != (int)slice_desc.m_orig_height))
  469. {
  470. error_printf("basis_compressor::validate_texture_type_constraints: The source image resolutions are not all equal!\n");
  471. return false;
  472. }
  473. }
  474. for (size_t i = 1; i < image_mipmap_levels.size(); i++)
  475. {
  476. if (image_mipmap_levels[0] != image_mipmap_levels[i])
  477. {
  478. error_printf("basis_compressor::validate_texture_type_constraints: Each image must have the same number of mipmap levels!\n");
  479. return false;
  480. }
  481. }
  482. return true;
  483. }
  484. bool basis_compressor::process_frontend()
  485. {
  486. debug_printf("basis_compressor::process_frontend\n");
  487. m_source_blocks.resize(m_total_blocks);
  488. for (uint32_t slice_index = 0; slice_index < m_slice_images.size(); slice_index++)
  489. {
  490. const basisu_backend_slice_desc &slice_desc = m_slice_descs[slice_index];
  491. const uint32_t num_blocks_x = slice_desc.m_num_blocks_x;
  492. const uint32_t num_blocks_y = slice_desc.m_num_blocks_y;
  493. const image &source_image = m_slice_images[slice_index];
  494. for (uint32_t block_y = 0; block_y < num_blocks_y; block_y++)
  495. for (uint32_t block_x = 0; block_x < num_blocks_x; block_x++)
  496. source_image.extract_block_clamped(m_source_blocks[slice_desc.m_first_block_index + block_x + block_y * num_blocks_x].get_ptr(), block_x * 4, block_y * 4, 4, 4);
  497. }
  498. #if 0
  499. // TODO
  500. basis_etc1_pack_params pack_params;
  501. pack_params.m_quality = cETCQualityMedium;
  502. pack_params.m_perceptual = m_params.m_perceptual;
  503. pack_params.m_use_color4 = false;
  504. pack_etc1_block_context pack_context;
  505. std::unordered_set<uint64_t> endpoint_hash;
  506. std::unordered_set<uint32_t> selector_hash;
  507. for (uint32_t i = 0; i < m_source_blocks.size(); i++)
  508. {
  509. etc_block blk;
  510. pack_etc1_block(blk, m_source_blocks[i].get_ptr(), pack_params, pack_context);
  511. const color_rgba c0(blk.get_block_color(0, false));
  512. endpoint_hash.insert((c0.r | (c0.g << 5) | (c0.b << 10)) | (blk.get_inten_table(0) << 16));
  513. const color_rgba c1(blk.get_block_color(1, false));
  514. endpoint_hash.insert((c1.r | (c1.g << 5) | (c1.b << 10)) | (blk.get_inten_table(1) << 16));
  515. selector_hash.insert(blk.get_raw_selector_bits());
  516. }
  517. const uint32_t total_unique_endpoints = (uint32_t)endpoint_hash.size();
  518. const uint32_t total_unique_selectors = (uint32_t)selector_hash.size();
  519. if (m_params.m_debug)
  520. {
  521. debug_printf("Unique endpoints: %u, unique selectors: %u\n", total_unique_endpoints, total_unique_selectors);
  522. }
  523. #endif
  524. const double total_texels = m_total_blocks * 16.0f;
  525. int endpoint_clusters = m_params.m_max_endpoint_clusters;
  526. int selector_clusters = m_params.m_max_selector_clusters;
  527. if (endpoint_clusters > basisu_frontend::cMaxEndpointClusters)
  528. {
  529. error_printf("Too many endpoint clusters! (%u but max is %u)\n", endpoint_clusters, basisu_frontend::cMaxEndpointClusters);
  530. return false;
  531. }
  532. if (selector_clusters > basisu_frontend::cMaxSelectorClusters)
  533. {
  534. error_printf("Too many selector clusters! (%u but max is %u)\n", selector_clusters, basisu_frontend::cMaxSelectorClusters);
  535. return false;
  536. }
  537. if (m_params.m_quality_level != -1)
  538. {
  539. const float quality = saturate(m_params.m_quality_level / 255.0f);
  540. const float bits_per_endpoint_cluster = 14.0f;
  541. const float max_desired_endpoint_cluster_bits_per_texel = 1.0f; // .15f
  542. int max_endpoints = static_cast<int>((max_desired_endpoint_cluster_bits_per_texel * total_texels) / bits_per_endpoint_cluster);
  543. const float mid = 128.0f / 255.0f;
  544. float color_endpoint_quality = quality;
  545. const float endpoint_split_point = 0.5f;
  546. if (color_endpoint_quality <= mid)
  547. {
  548. color_endpoint_quality = lerp(0.0f, endpoint_split_point, powf(color_endpoint_quality / mid, .65f));
  549. max_endpoints = clamp<int>(max_endpoints, 256, 3072);
  550. max_endpoints = minimum<uint32_t>(max_endpoints, m_total_blocks);
  551. if (max_endpoints < 64)
  552. max_endpoints = 64;
  553. endpoint_clusters = clamp<uint32_t>((uint32_t)(.5f + lerp<float>(32, static_cast<float>(max_endpoints), color_endpoint_quality)), 32, basisu_frontend::cMaxEndpointClusters);
  554. }
  555. else
  556. {
  557. color_endpoint_quality = powf((color_endpoint_quality - mid) / (1.0f - mid), 1.6f);
  558. max_endpoints = clamp<int>(max_endpoints, 256, 8192);
  559. max_endpoints = minimum<uint32_t>(max_endpoints, m_total_blocks);
  560. if (max_endpoints < 3072)
  561. max_endpoints = 3072;
  562. endpoint_clusters = clamp<uint32_t>((uint32_t)(.5f + lerp<float>(3072, static_cast<float>(max_endpoints), color_endpoint_quality)), 32, basisu_frontend::cMaxEndpointClusters);
  563. }
  564. float bits_per_selector_cluster = m_params.m_global_sel_pal ? 21.0f : 14.0f;
  565. const float max_desired_selector_cluster_bits_per_texel = 1.0f; // .15f
  566. int max_selectors = static_cast<int>((max_desired_selector_cluster_bits_per_texel * total_texels) / bits_per_selector_cluster);
  567. max_selectors = clamp<int>(max_selectors, 256, basisu_frontend::cMaxSelectorClusters);
  568. max_selectors = minimum<uint32_t>(max_selectors, m_total_blocks);
  569. float color_selector_quality = quality;
  570. //color_selector_quality = powf(color_selector_quality, 1.65f);
  571. color_selector_quality = powf(color_selector_quality, 2.62f);
  572. if (max_selectors < 96)
  573. max_selectors = 96;
  574. selector_clusters = clamp<uint32_t>((uint32_t)(.5f + lerp<float>(96, static_cast<float>(max_selectors), color_selector_quality)), 8, basisu_frontend::cMaxSelectorClusters);
  575. debug_printf("Max endpoints: %u, max selectors: %u\n", endpoint_clusters, selector_clusters);
  576. if (m_params.m_quality_level >= 223)
  577. {
  578. if (!m_params.m_selector_rdo_thresh.was_changed())
  579. {
  580. if (!m_params.m_endpoint_rdo_thresh.was_changed())
  581. m_params.m_endpoint_rdo_thresh *= .25f;
  582. if (!m_params.m_selector_rdo_thresh.was_changed())
  583. m_params.m_selector_rdo_thresh *= .25f;
  584. }
  585. }
  586. else if (m_params.m_quality_level >= 192)
  587. {
  588. if (!m_params.m_endpoint_rdo_thresh.was_changed())
  589. m_params.m_endpoint_rdo_thresh *= .5f;
  590. if (!m_params.m_selector_rdo_thresh.was_changed())
  591. m_params.m_selector_rdo_thresh *= .5f;
  592. }
  593. else if (m_params.m_quality_level >= 160)
  594. {
  595. if (!m_params.m_endpoint_rdo_thresh.was_changed())
  596. m_params.m_endpoint_rdo_thresh *= .75f;
  597. if (!m_params.m_selector_rdo_thresh.was_changed())
  598. m_params.m_selector_rdo_thresh *= .75f;
  599. }
  600. else if (m_params.m_quality_level >= 129)
  601. {
  602. float l = (quality - 129 / 255.0f) / ((160 - 129) / 255.0f);
  603. if (!m_params.m_endpoint_rdo_thresh.was_changed())
  604. m_params.m_endpoint_rdo_thresh *= lerp<float>(1.0f, .75f, l);
  605. if (!m_params.m_selector_rdo_thresh.was_changed())
  606. m_params.m_selector_rdo_thresh *= lerp<float>(1.0f, .75f, l);
  607. }
  608. }
  609. m_auto_global_sel_pal = false;
  610. if (!m_params.m_global_sel_pal && !m_params.m_no_auto_global_sel_pal)
  611. {
  612. const float bits_per_selector_cluster = 31.0f;
  613. double selector_codebook_bpp_est = (bits_per_selector_cluster * selector_clusters) / total_texels;
  614. debug_printf("selector_codebook_bpp_est: %f\n", selector_codebook_bpp_est);
  615. const float force_global_sel_pal_bpp_threshold = .15f;
  616. if ((total_texels <= 128.0f*128.0f) && (selector_codebook_bpp_est > force_global_sel_pal_bpp_threshold))
  617. {
  618. m_auto_global_sel_pal = true;
  619. debug_printf("Auto global selector palette enabled\n");
  620. }
  621. }
  622. basisu_frontend::params p;
  623. p.m_num_source_blocks = m_total_blocks;
  624. p.m_pSource_blocks = &m_source_blocks[0];
  625. p.m_max_endpoint_clusters = endpoint_clusters;
  626. p.m_max_selector_clusters = selector_clusters;
  627. p.m_perceptual = m_params.m_perceptual;
  628. p.m_debug_stats = m_params.m_debug;
  629. p.m_debug_images = m_params.m_debug_images;
  630. p.m_compression_level = m_params.m_compression_level;
  631. p.m_tex_type = m_params.m_tex_type;
  632. p.m_multithreaded = m_params.m_multithreading;
  633. p.m_disable_hierarchical_endpoint_codebooks = m_params.m_disable_hierarchical_endpoint_codebooks;
  634. p.m_pJob_pool = m_params.m_pJob_pool;
  635. if ((m_params.m_global_sel_pal) || (m_auto_global_sel_pal))
  636. {
  637. p.m_pGlobal_sel_codebook = m_params.m_pSel_codebook;
  638. p.m_num_global_sel_codebook_pal_bits = m_params.m_global_pal_bits;
  639. p.m_num_global_sel_codebook_mod_bits = m_params.m_global_mod_bits;
  640. p.m_use_hybrid_selector_codebooks = !m_params.m_no_hybrid_sel_cb;
  641. p.m_hybrid_codebook_quality_thresh = m_params.m_hybrid_sel_cb_quality_thresh;
  642. }
  643. if (!m_frontend.init(p))
  644. {
  645. error_printf("basisu_frontend::init() failed!\n");
  646. return false;
  647. }
  648. m_frontend.compress();
  649. if (m_params.m_debug_images)
  650. {
  651. for (uint32_t i = 0; i < m_slice_descs.size(); i++)
  652. {
  653. char filename[1024];
  654. #ifdef _WIN32
  655. sprintf_s(filename, sizeof(filename), "rdo_frontend_output_output_blocks_%u.png", i);
  656. #else
  657. snprintf(filename, sizeof(filename), "rdo_frontend_output_output_blocks_%u.png", i);
  658. #endif
  659. m_frontend.dump_debug_image(filename, m_slice_descs[i].m_first_block_index, m_slice_descs[i].m_num_blocks_x, m_slice_descs[i].m_num_blocks_y, true);
  660. #ifdef _WIN32
  661. sprintf_s(filename, sizeof(filename), "rdo_frontend_output_api_%u.png", i);
  662. #else
  663. snprintf(filename, sizeof(filename), "rdo_frontend_output_api_%u.png", i);
  664. #endif
  665. m_frontend.dump_debug_image(filename, m_slice_descs[i].m_first_block_index, m_slice_descs[i].m_num_blocks_x, m_slice_descs[i].m_num_blocks_y, false);
  666. }
  667. }
  668. return true;
  669. }
  670. bool basis_compressor::extract_frontend_texture_data()
  671. {
  672. debug_printf("basis_compressor::extract_frontend_texture_data\n");
  673. m_frontend_output_textures.resize(m_slice_descs.size());
  674. m_best_etc1s_images.resize(m_slice_descs.size());
  675. m_best_etc1s_images_unpacked.resize(m_slice_descs.size());
  676. for (uint32_t i = 0; i < m_slice_descs.size(); i++)
  677. {
  678. const basisu_backend_slice_desc &slice_desc = m_slice_descs[i];
  679. const uint32_t num_blocks_x = slice_desc.m_num_blocks_x;
  680. const uint32_t num_blocks_y = slice_desc.m_num_blocks_y;
  681. const uint32_t width = num_blocks_x * 4;
  682. const uint32_t height = num_blocks_y * 4;
  683. m_frontend_output_textures[i].init(cETC1, width, height);
  684. for (uint32_t block_y = 0; block_y < num_blocks_y; block_y++)
  685. for (uint32_t block_x = 0; block_x < num_blocks_x; block_x++)
  686. memcpy(m_frontend_output_textures[i].get_block_ptr(block_x, block_y, 0), &m_frontend.get_output_block(slice_desc.m_first_block_index + block_x + block_y * num_blocks_x), sizeof(etc_block));
  687. #if 0
  688. if (m_params.m_debug_images)
  689. {
  690. char filename[1024];
  691. sprintf_s(filename, sizeof(filename), "rdo_etc_frontend_%u_", i);
  692. write_etc1_vis_images(m_frontend_output_textures[i], filename);
  693. }
  694. #endif
  695. m_best_etc1s_images[i].init(cETC1, width, height);
  696. for (uint32_t block_y = 0; block_y < num_blocks_y; block_y++)
  697. for (uint32_t block_x = 0; block_x < num_blocks_x; block_x++)
  698. memcpy(m_best_etc1s_images[i].get_block_ptr(block_x, block_y, 0), &m_frontend.get_etc1s_block(slice_desc.m_first_block_index + block_x + block_y * num_blocks_x), sizeof(etc_block));
  699. m_best_etc1s_images[i].unpack(m_best_etc1s_images_unpacked[i]);
  700. }
  701. return true;
  702. }
  703. bool basis_compressor::process_backend()
  704. {
  705. debug_printf("basis_compressor::process_backend\n");
  706. basisu_backend_params backend_params;
  707. backend_params.m_debug = m_params.m_debug;
  708. backend_params.m_debug_images = m_params.m_debug_images;
  709. backend_params.m_etc1s = true;
  710. backend_params.m_compression_level = m_params.m_compression_level;
  711. if (!m_params.m_no_endpoint_rdo)
  712. backend_params.m_endpoint_rdo_quality_thresh = m_params.m_endpoint_rdo_thresh;
  713. if (!m_params.m_no_selector_rdo)
  714. backend_params.m_selector_rdo_quality_thresh = m_params.m_selector_rdo_thresh;
  715. backend_params.m_use_global_sel_codebook = (m_frontend.get_params().m_pGlobal_sel_codebook != NULL);
  716. backend_params.m_global_sel_codebook_pal_bits = m_frontend.get_params().m_num_global_sel_codebook_pal_bits;
  717. backend_params.m_global_sel_codebook_mod_bits = m_frontend.get_params().m_num_global_sel_codebook_mod_bits;
  718. backend_params.m_use_hybrid_sel_codebooks = m_frontend.get_params().m_use_hybrid_selector_codebooks;
  719. m_backend.init(&m_frontend, backend_params, m_slice_descs, m_params.m_pSel_codebook);
  720. uint32_t total_packed_bytes = m_backend.encode();
  721. if (!total_packed_bytes)
  722. {
  723. error_printf("basis_compressor::encode() failed!\n");
  724. return false;
  725. }
  726. debug_printf("Total packed bytes (estimated): %u\n", total_packed_bytes);
  727. return true;
  728. }
  729. bool basis_compressor::create_basis_file_and_transcode()
  730. {
  731. debug_printf("basis_compressor::create_basis_file_and_transcode\n");
  732. const basisu_backend_output &encoded_output = m_backend.get_output();
  733. if (!m_basis_file.init(encoded_output, m_params.m_tex_type, m_params.m_userdata0, m_params.m_userdata1, m_params.m_y_flip, m_params.m_us_per_frame))
  734. {
  735. error_printf("basis_compressor::write_output_files_and_compute_stats: basisu_backend:init() failed!\n");
  736. return false;
  737. }
  738. const uint8_vec &comp_data = m_basis_file.get_compressed_data();
  739. m_output_basis_file = comp_data;
  740. // Verify the compressed data by transcoding it to ETC1/BC1 and validating the CRC's.
  741. basist::basisu_transcoder decoder(m_params.m_pSel_codebook);
  742. if (!decoder.validate_file_checksums(&comp_data[0], (uint32_t)comp_data.size(), true))
  743. {
  744. error_printf("decoder.validate_file_checksums() failed!\n");
  745. return false;
  746. }
  747. m_decoded_output_textures.resize(m_slice_descs.size());
  748. m_decoded_output_textures_unpacked.resize(m_slice_descs.size());
  749. m_decoded_output_textures_bc1.resize(m_slice_descs.size());
  750. m_decoded_output_textures_unpacked_bc1.resize(m_slice_descs.size());
  751. interval_timer tm;
  752. tm.start();
  753. if (!decoder.start_transcoding(&comp_data[0], (uint32_t)comp_data.size()))
  754. {
  755. error_printf("decoder.start_transcoding() failed!\n");
  756. return false;
  757. }
  758. debug_printf("basisu_comppressor::start_transcoding() took %3.3fms\n", tm.get_elapsed_ms());
  759. uint32_t total_orig_pixels = 0;
  760. uint32_t total_texels = 0;
  761. double total_time_etc1 = 0;
  762. for (uint32_t i = 0; i < m_slice_descs.size(); i++)
  763. {
  764. gpu_image decoded_texture;
  765. decoded_texture.init(cETC1, m_slice_descs[i].m_width, m_slice_descs[i].m_height);
  766. tm.start();
  767. if (!decoder.transcode_slice(&comp_data[0], (uint32_t)comp_data.size(), i,
  768. reinterpret_cast<etc_block *>(decoded_texture.get_ptr()), m_slice_descs[i].m_num_blocks_x * m_slice_descs[i].m_num_blocks_y, basist::cETC1, 8))
  769. {
  770. error_printf("Transcoding failed to ETC1 on slice %u!\n", i);
  771. return false;
  772. }
  773. total_time_etc1 += tm.get_elapsed_secs();
  774. uint32_t image_crc16 = basist::crc16(decoded_texture.get_ptr(), decoded_texture.get_size_in_bytes(), 0);
  775. if (image_crc16 != m_backend.get_output().m_slice_image_crcs[i])
  776. {
  777. error_printf("Decoded image data CRC check failed on slice %u!\n", i);
  778. return false;
  779. }
  780. debug_printf("Decoded image data CRC check succeeded on slice %i\n", i);
  781. m_decoded_output_textures[i] = decoded_texture;
  782. total_orig_pixels += m_slice_descs[i].m_orig_width * m_slice_descs[i].m_orig_height;
  783. total_texels += m_slice_descs[i].m_width * m_slice_descs[i].m_height;
  784. }
  785. tm.start();
  786. basist::basisu_transcoder_init();
  787. debug_printf("basist::basisu_transcoder_init: Took %f ms\n", tm.get_elapsed_ms());
  788. double total_time_bc1 = 0;
  789. for (uint32_t i = 0; i < m_slice_descs.size(); i++)
  790. {
  791. gpu_image decoded_texture;
  792. decoded_texture.init(cBC1, m_slice_descs[i].m_width, m_slice_descs[i].m_height);
  793. tm.start();
  794. if (!decoder.transcode_slice(&comp_data[0], (uint32_t)comp_data.size(), i,
  795. reinterpret_cast<etc_block *>(decoded_texture.get_ptr()), m_slice_descs[i].m_num_blocks_x * m_slice_descs[i].m_num_blocks_y, basist::cBC1, 8))
  796. {
  797. error_printf("Transcoding failed to BC1 on slice %u!\n", i);
  798. return false;
  799. }
  800. total_time_bc1 += tm.get_elapsed_secs();
  801. m_decoded_output_textures_bc1[i] = decoded_texture;
  802. }
  803. for (uint32_t i = 0; i < m_slice_descs.size(); i++)
  804. {
  805. m_decoded_output_textures[i].unpack(m_decoded_output_textures_unpacked[i]);
  806. m_decoded_output_textures_bc1[i].unpack(m_decoded_output_textures_unpacked_bc1[i]);
  807. }
  808. debug_printf("Transcoded to ETC1 in %3.3fms, %f texels/sec\n", total_time_etc1 * 1000.0f, total_orig_pixels / total_time_etc1);
  809. debug_printf("Transcoded to BC1 in %3.3fms, %f texels/sec\n", total_time_bc1 * 1000.0f, total_orig_pixels / total_time_bc1);
  810. debug_printf("Total .basis output file size: %u, %3.3f bits/texel\n", comp_data.size(), comp_data.size() * 8.0f / total_orig_pixels);
  811. m_output_blocks.resize(0);
  812. uint32_t total_orig_texels = 0;
  813. for (uint32_t slice_index = 0; slice_index < m_slice_descs.size(); slice_index++)
  814. {
  815. const basisu_backend_slice_desc &slice_desc = m_slice_descs[slice_index];
  816. total_orig_texels += slice_desc.m_orig_width * slice_desc.m_orig_height;
  817. const uint32_t total_blocks = slice_desc.m_num_blocks_x * slice_desc.m_num_blocks_y;
  818. assert(m_decoded_output_textures[slice_index].get_total_blocks() == total_blocks);
  819. memcpy(enlarge_vector(m_output_blocks, total_blocks), m_decoded_output_textures[slice_index].get_ptr(), sizeof(etc_block) * total_blocks);
  820. }
  821. m_basis_file_size = (uint32_t)comp_data.size();
  822. m_basis_bits_per_texel = (comp_data.size() * 8.0f) / total_orig_texels;
  823. return true;
  824. }
  825. bool basis_compressor::write_output_files_and_compute_stats()
  826. {
  827. debug_printf("basis_compressor::write_output_files_and_compute_stats\n");
  828. if (m_params.m_write_output_basis_files)
  829. {
  830. const uint8_vec &comp_data = m_basis_file.get_compressed_data();
  831. std::string basis_filename(m_params.m_out_filename);
  832. string_remove_extension(basis_filename);
  833. basis_filename += ".basis";
  834. if (!write_vec_to_file(basis_filename.c_str(), comp_data))
  835. {
  836. error_printf("Failed writing output data to file \"%s\"\n", basis_filename.c_str());
  837. return false;
  838. }
  839. printf("Wrote output .basis file \"%s\"\n", basis_filename.c_str());
  840. }
  841. m_stats.resize(m_slice_descs.size());
  842. uint32_t total_orig_texels = 0;
  843. for (uint32_t slice_index = 0; slice_index < m_slice_descs.size(); slice_index++)
  844. {
  845. const basisu_backend_slice_desc &slice_desc = m_slice_descs[slice_index];
  846. total_orig_texels += slice_desc.m_orig_width * slice_desc.m_orig_height;
  847. if (m_params.m_compute_stats)
  848. {
  849. printf("Slice: %u\n", slice_index);
  850. image_stats &s = m_stats[slice_index];
  851. // TODO: We used to output SSIM (during heavy encoder development), but this slowed down compression too much. We'll be adding it back.
  852. image_metrics em;
  853. // ---- .basis ETC1S stats
  854. em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 0, 0);
  855. em.print(".basis ETC1S 709 Luma: ");
  856. s.m_basis_etc1s_luma_709_psnr = static_cast<float>(em.m_psnr);
  857. s.m_basis_etc1s_luma_709_ssim = static_cast<float>(em.m_ssim);
  858. em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 0, 0, true, true);
  859. em.print(".basis ETC1S 601 Luma: ");
  860. s.m_basis_etc1s_luma_601_psnr = static_cast<float>(em.m_psnr);
  861. em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 0, 3);
  862. em.print(".basis ETC1S RGB Avg: ");
  863. s.m_basis_etc1s_rgb_avg_psnr = em.m_psnr;
  864. if (m_slice_descs.size() == 1)
  865. {
  866. debug_printf(".basis Luma 709 PSNR per bit/texel*10000: %3.3f\n", 10000.0f * s.m_basis_etc1s_luma_709_psnr / ((m_backend.get_output().get_output_size_estimate() * 8.0f) / (slice_desc.m_orig_width * slice_desc.m_orig_height)));
  867. }
  868. // ---- .basis BC1 stats
  869. em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc1[slice_index], 0, 0);
  870. em.print(".basis BC1 709 Luma: ");
  871. s.m_basis_bc1_luma_709_psnr = static_cast<float>(em.m_psnr);
  872. s.m_basis_bc1_luma_709_ssim = static_cast<float>(em.m_ssim);
  873. em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc1[slice_index], 0, 0, true, true);
  874. em.print(".basis BC1 601 Luma: ");
  875. s.m_basis_bc1_luma_601_psnr = static_cast<float>(em.m_psnr);
  876. em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc1[slice_index], 0, 3);
  877. em.print(".basis BC1 RGB Avg: ");
  878. s.m_basis_bc1_rgb_avg_psnr = static_cast<float>(em.m_psnr);
  879. // ---- Nearly best possible ETC1S stats
  880. em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 0);
  881. em.print("Unquantized ETC1S 709 Luma: ");
  882. s.m_best_luma_709_psnr = static_cast<float>(em.m_psnr);
  883. s.m_best_luma_709_ssim = static_cast<float>(em.m_ssim);
  884. em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 0, true, true);
  885. em.print("Unquantized ETC1S 601 Luma: ");
  886. s.m_best_luma_601_psnr = static_cast<float>(em.m_psnr);
  887. em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 3);
  888. em.print("Unquantized ETC1S RGB Avg: ");
  889. s.m_best_rgb_avg_psnr = static_cast<float>(em.m_psnr);
  890. }
  891. if (m_frontend.get_params().m_debug_images)
  892. {
  893. std::string out_basename;
  894. if (m_params.m_out_filename.size())
  895. string_get_filename(m_params.m_out_filename.c_str(), out_basename);
  896. else if (m_params.m_source_filenames.size())
  897. string_get_filename(m_params.m_source_filenames[slice_desc.m_source_file_index].c_str(), out_basename);
  898. string_remove_extension(out_basename);
  899. out_basename = "basis_debug_" + out_basename + string_format("_slice_%u", slice_index);
  900. // Write "best" ETC1S debug images
  901. {
  902. gpu_image best_etc1s_gpu_image(m_best_etc1s_images[slice_index]);
  903. best_etc1s_gpu_image.override_dimensions(slice_desc.m_orig_width, slice_desc.m_orig_height);
  904. write_compressed_texture_file((out_basename + "_best_etc1s.ktx").c_str(), best_etc1s_gpu_image);
  905. image best_etc1s_unpacked;
  906. best_etc1s_gpu_image.unpack(best_etc1s_unpacked);
  907. save_png(out_basename + "_best_etc1s.png", best_etc1s_unpacked);
  908. }
  909. // Write decoded ETC1S debug images
  910. {
  911. gpu_image decoded_etc1s(m_decoded_output_textures[slice_index]);
  912. decoded_etc1s.override_dimensions(slice_desc.m_orig_width, slice_desc.m_orig_height);
  913. write_compressed_texture_file((out_basename + "_decoded_etc1s.ktx").c_str(), decoded_etc1s);
  914. image temp(m_decoded_output_textures_unpacked[slice_index]);
  915. temp.crop(slice_desc.m_orig_width, slice_desc.m_orig_height);
  916. save_png(out_basename + "_decoded_etc1s.png", temp);
  917. }
  918. // Write decoded BC1 debug images
  919. {
  920. gpu_image decoded_bc1(m_decoded_output_textures_bc1[slice_index]);
  921. decoded_bc1.override_dimensions(slice_desc.m_orig_width, slice_desc.m_orig_height);
  922. write_compressed_texture_file((out_basename + "_decoded_bc1.ktx").c_str(), decoded_bc1);
  923. image temp(m_decoded_output_textures_unpacked_bc1[slice_index]);
  924. temp.crop(slice_desc.m_orig_width, slice_desc.m_orig_height);
  925. save_png(out_basename + "_decoded_bc1.png", temp);
  926. }
  927. }
  928. }
  929. return true;
  930. }
  931. } // namespace basisu