astcenc_color_quantize.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2011-2023 Arm Limited
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. // use this file except in compliance with the License. You may obtain a copy
  7. // of the License at:
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. // License for the specific language governing permissions and limitations
  15. // under the License.
  16. // ----------------------------------------------------------------------------
  17. #if !defined(ASTCENC_DECOMPRESS_ONLY)
  18. /**
  19. * @brief Functions for color quantization.
  20. *
  21. * The design of the color quantization functionality requires the caller to use higher level error
  22. * analysis to determine the base encoding that should be used. This earlier analysis will select
  23. * the basic type of the endpoint that should be used:
  24. *
  25. * * Mode: LDR or HDR
  26. * * Quantization level
  27. * * Channel count: L, LA, RGB, or RGBA
  28. * * Endpoint 2 type: Direct color endcode, or scaled from endpoint 1.
  29. *
  30. * However, this leaves a number of decisions about exactly how to pack the endpoints open. In
  31. * particular we need to determine if blue contraction can be used, or/and if delta encoding can be
  32. * used. If they can be applied these will allow us to maintain higher precision in the endpoints
  33. * without needing additional storage.
  34. */
  35. #include <stdio.h>
  36. #include <assert.h>
  37. #include "astcenc_internal.h"
  38. /**
  39. * @brief Compute the error of an LDR RGB or RGBA encoding.
  40. *
  41. * @param uquant0 The original endpoint 0 color.
  42. * @param uquant1 The original endpoint 1 color.
  43. * @param quant0 The unpacked quantized endpoint 0 color.
  44. * @param quant1 The unpacked quantized endpoint 1 color.
  45. *
  46. * @return The MSE of the encoding.
  47. */
  48. static float get_rgba_encoding_error(
  49. vfloat4 uquant0,
  50. vfloat4 uquant1,
  51. vint4 quant0,
  52. vint4 quant1
  53. ) {
  54. vfloat4 error0 = uquant0 - int_to_float(quant0);
  55. vfloat4 error1 = uquant1 - int_to_float(quant1);
  56. return hadd_s(error0 * error0 + error1 * error1);
  57. }
  58. /**
  59. * @brief Determine the quantized value given a quantization level.
  60. *
  61. * @param quant_level The quantization level to use.
  62. * @param value The value to convert. This must be in the 0-255 range.
  63. *
  64. * @return The unpacked quantized value, returned in 0-255 range.
  65. */
  66. static inline uint8_t quant_color(
  67. quant_method quant_level,
  68. int value
  69. ) {
  70. int index = value * 2 + 1;
  71. return color_unquant_to_uquant_tables[quant_level - QUANT_6][index];
  72. }
  73. /**
  74. * @brief Determine the quantized value given a quantization level.
  75. *
  76. * @param quant_level The quantization level to use.
  77. * @param value The value to convert. This must be in the 0-255 range.
  78. *
  79. * @return The unpacked quantized value, returned in 0-255 range.
  80. */
  81. static inline vint4 quant_color3(
  82. quant_method quant_level,
  83. vint4 value
  84. ) {
  85. vint4 index = value * 2 + 1;
  86. return vint4(
  87. color_unquant_to_uquant_tables[quant_level - QUANT_6][index.lane<0>()],
  88. color_unquant_to_uquant_tables[quant_level - QUANT_6][index.lane<1>()],
  89. color_unquant_to_uquant_tables[quant_level - QUANT_6][index.lane<2>()],
  90. 0);
  91. }
  92. /**
  93. * @brief Determine the quantized value given a quantization level and residual.
  94. *
  95. * @param quant_level The quantization level to use.
  96. * @param value The value to convert. This must be in the 0-255 range.
  97. * @param valuef The original value before rounding, used to compute a residual.
  98. *
  99. * @return The unpacked quantized value, returned in 0-255 range.
  100. */
  101. static inline uint8_t quant_color(
  102. quant_method quant_level,
  103. int value,
  104. float valuef
  105. ) {
  106. int index = value * 2;
  107. // Compute the residual to determine if we should round down or up ties.
  108. // Test should be residual >= 0, but empirical testing shows small bias helps.
  109. float residual = valuef - static_cast<float>(value);
  110. if (residual >= -0.1f)
  111. {
  112. index++;
  113. }
  114. return color_unquant_to_uquant_tables[quant_level - QUANT_6][index];
  115. }
  116. /**
  117. * @brief Determine the quantized value given a quantization level and residual.
  118. *
  119. * @param quant_level The quantization level to use.
  120. * @param value The value to convert. This must be in the 0-255 range.
  121. * @param valuef The original value before rounding, used to compute a residual.
  122. *
  123. * @return The unpacked quantized value, returned in 0-255 range.
  124. */
  125. static inline vint4 quant_color3(
  126. quant_method quant_level,
  127. vint4 value,
  128. vfloat4 valuef
  129. ) {
  130. vint4 index = value * 2;
  131. // Compute the residual to determine if we should round down or up ties.
  132. // Test should be residual >= 0, but empirical testing shows small bias helps.
  133. vfloat4 residual = valuef - int_to_float(value);
  134. vmask4 mask = residual >= vfloat4(-0.1f);
  135. index = select(index, index + 1, mask);
  136. return vint4(
  137. color_unquant_to_uquant_tables[quant_level - QUANT_6][index.lane<0>()],
  138. color_unquant_to_uquant_tables[quant_level - QUANT_6][index.lane<1>()],
  139. color_unquant_to_uquant_tables[quant_level - QUANT_6][index.lane<2>()],
  140. 0);
  141. }
  142. /**
  143. * @brief Quantize an LDR RGB color.
  144. *
  145. * Since this is a fall-back encoding, we cannot actually fail but must produce a sensible result.
  146. * For this encoding @c color0 cannot be larger than @c color1. If @c color0 is actually larger
  147. * than @c color1, @c color0 is reduced and @c color1 is increased until the constraint is met.
  148. *
  149. * @param color0 The input unquantized color0 endpoint.
  150. * @param color1 The input unquantized color1 endpoint.
  151. * @param[out] color0_out The output quantized color0 endpoint.
  152. * @param[out] color1_out The output quantized color1 endpoint.
  153. * @param quant_level The quantization level to use.
  154. */
  155. static void quantize_rgb(
  156. vfloat4 color0,
  157. vfloat4 color1,
  158. vint4& color0_out,
  159. vint4& color1_out,
  160. quant_method quant_level
  161. ) {
  162. vint4 color0i, color1i;
  163. vfloat4 nudge(0.2f);
  164. do
  165. {
  166. vint4 color0q = max(float_to_int_rtn(color0), vint4(0));
  167. color0i = quant_color3(quant_level, color0q, color0);
  168. color0 = color0 - nudge;
  169. vint4 color1q = min(float_to_int_rtn(color1), vint4(255));
  170. color1i = quant_color3(quant_level, color1q, color1);
  171. color1 = color1 + nudge;
  172. } while (hadd_rgb_s(color0i) > hadd_rgb_s(color1i));
  173. color0_out = color0i;
  174. color1_out = color1i;
  175. }
  176. /**
  177. * @brief Quantize an LDR RGBA color.
  178. *
  179. * Since this is a fall-back encoding, we cannot actually fail but must produce a sensible result.
  180. * For this encoding @c color0.rgb cannot be larger than @c color1.rgb (this indicates blue
  181. * contraction). If @c color0.rgb is actually larger than @c color1.rgb, @c color0.rgb is reduced
  182. * and @c color1.rgb is increased until the constraint is met.
  183. *
  184. * @param color0 The input unquantized color0 endpoint.
  185. * @param color1 The input unquantized color1 endpoint.
  186. * @param[out] color0_out The output quantized color0 endpoint.
  187. * @param[out] color1_out The output quantized color1 endpoint.
  188. * @param quant_level The quantization level to use.
  189. */
  190. static void quantize_rgba(
  191. vfloat4 color0,
  192. vfloat4 color1,
  193. vint4& color0_out,
  194. vint4& color1_out,
  195. quant_method quant_level
  196. ) {
  197. quantize_rgb(color0, color1, color0_out, color1_out, quant_level);
  198. float a0 = color0.lane<3>();
  199. float a1 = color1.lane<3>();
  200. color0_out.set_lane<3>(quant_color(quant_level, astc::flt2int_rtn(a0), a0));
  201. color1_out.set_lane<3>(quant_color(quant_level, astc::flt2int_rtn(a1), a1));
  202. }
  203. /**
  204. * @brief Try to quantize an LDR RGB color using blue-contraction.
  205. *
  206. * Blue-contraction is only usable if encoded color 1 is larger than color 0.
  207. *
  208. * @param color0 The input unquantized color0 endpoint.
  209. * @param color1 The input unquantized color1 endpoint.
  210. * @param[out] color0_out The output quantized color0 endpoint.
  211. * @param[out] color1_out The output quantized color1 endpoint.
  212. * @param quant_level The quantization level to use.
  213. *
  214. * @return Returns @c false on failure, @c true on success.
  215. */
  216. static bool try_quantize_rgb_blue_contract(
  217. vfloat4 color0,
  218. vfloat4 color1,
  219. vint4& color0_out,
  220. vint4& color1_out,
  221. quant_method quant_level
  222. ) {
  223. // Apply inverse blue-contraction
  224. color0 += color0 - color0.swz<2, 2, 2, 3>();
  225. color1 += color1 - color1.swz<2, 2, 2, 3>();
  226. // If anything overflows BC cannot be used
  227. vmask4 color0_error = (color0 < vfloat4(0.0f)) | (color0 > vfloat4(255.0f));
  228. vmask4 color1_error = (color1 < vfloat4(0.0f)) | (color1 > vfloat4(255.0f));
  229. if (any(color0_error | color1_error))
  230. {
  231. return false;
  232. }
  233. // Quantize the inverse blue-contracted color
  234. vint4 color0i = quant_color3(quant_level, float_to_int_rtn(color0), color0);
  235. vint4 color1i = quant_color3(quant_level, float_to_int_rtn(color1), color1);
  236. // If color #1 is not larger than color #0 then blue-contraction cannot be used
  237. // We must test afterwards because quantization can change the order
  238. if (hadd_rgb_s(color1i) <= hadd_rgb_s(color0i))
  239. {
  240. return false;
  241. }
  242. color0_out = color1i;
  243. color1_out = color0i;
  244. return true;
  245. }
  246. /**
  247. * @brief Try to quantize an LDR RGBA color using blue-contraction.
  248. *
  249. * Blue-contraction is only usable if encoded color 1 RGB is larger than color 0 RGB.
  250. *
  251. * @param color0 The input unquantized color0 endpoint.
  252. * @param color1 The input unquantized color1 endpoint.
  253. * @param[out] color0_out The output quantized color0 endpoint.
  254. * @param[out] color1_out The output quantized color1 endpoint.
  255. * @param quant_level The quantization level to use.
  256. *
  257. * @return Returns @c false on failure, @c true on success.
  258. */
  259. static bool try_quantize_rgba_blue_contract(
  260. vfloat4 color0,
  261. vfloat4 color1,
  262. vint4& color0_out,
  263. vint4& color1_out,
  264. quant_method quant_level
  265. ) {
  266. if (try_quantize_rgb_blue_contract(color0, color1, color0_out, color1_out, quant_level))
  267. {
  268. float a0 = color0.lane<3>();
  269. float a1 = color1.lane<3>();
  270. color0_out.set_lane<3>(quant_color(quant_level, astc::flt2int_rtn(a1), a1));
  271. color1_out.set_lane<3>(quant_color(quant_level, astc::flt2int_rtn(a0), a0));
  272. return true;
  273. }
  274. return false;
  275. }
  276. /**
  277. * @brief Try to quantize an LDR RGB color using delta encoding.
  278. *
  279. * At decode time we move one bit from the offset to the base and seize another bit as a sign bit;
  280. * we then unquantize both values as if they contain one extra bit. If the sum of the offsets is
  281. * non-negative, then we encode a regular delta.
  282. *
  283. * @param color0 The input unquantized color0 endpoint.
  284. * @param color1 The input unquantized color1 endpoint.
  285. * @param[out] color0_out The output quantized color0 endpoint.
  286. * @param[out] color1_out The output quantized color1 endpoint.
  287. * @param quant_level The quantization level to use.
  288. *
  289. * @return Returns @c false on failure, @c true on success.
  290. */
  291. static bool try_quantize_rgb_delta(
  292. vfloat4 color0,
  293. vfloat4 color1,
  294. vint4& color0_out,
  295. vint4& color1_out,
  296. quant_method quant_level
  297. ) {
  298. // Transform color0 to unorm9
  299. vint4 color0a = float_to_int_rtn(color0);
  300. color0.set_lane<3>(0.0f);
  301. color0a = lsl<1>(color0a);
  302. // Mask off the top bit
  303. vint4 color0b = color0a & 0xFF;
  304. // Quantize then unquantize in order to get a value that we take differences against
  305. vint4 color0be = quant_color3(quant_level, color0b);
  306. color0b = color0be | (color0a & 0x100);
  307. // Get hold of the second value
  308. vint4 color1d = float_to_int_rtn(color1);
  309. color1d = lsl<1>(color1d);
  310. // ... and take differences
  311. color1d = color1d - color0b;
  312. color1d.set_lane<3>(0);
  313. // Check if the difference is too large to be encodable
  314. if (any((color1d > vint4(63)) | (color1d < vint4(-64))))
  315. {
  316. return false;
  317. }
  318. // Insert top bit of the base into the offset
  319. color1d = color1d & 0x7F;
  320. color1d = color1d | lsr<1>(color0b & 0x100);
  321. // Then quantize and unquantize; if this causes either top two bits to flip, then encoding fails
  322. // since we have then corrupted either the top bit of the base or the sign bit of the offset
  323. vint4 color1de = quant_color3(quant_level, color1d);
  324. vint4 color_flips = (color1d ^ color1de) & 0xC0;
  325. color_flips.set_lane<3>(0);
  326. if (any(color_flips != vint4::zero()))
  327. {
  328. return false;
  329. }
  330. // If the sum of offsets triggers blue-contraction then encoding fails
  331. vint4 ep0 = color0be;
  332. vint4 ep1 = color1de;
  333. bit_transfer_signed(ep1, ep0);
  334. if (hadd_rgb_s(ep1) < 0)
  335. {
  336. return false;
  337. }
  338. // Check that the offsets produce legitimate sums as well
  339. ep0 = ep0 + ep1;
  340. if (any((ep0 < vint4(0)) | (ep0 > vint4(0xFF))))
  341. {
  342. return false;
  343. }
  344. color0_out = color0be;
  345. color1_out = color1de;
  346. return true;
  347. }
  348. /**
  349. * @brief Try to quantize an LDR RGB color using delta encoding and blue-contraction.
  350. *
  351. * Blue-contraction is only usable if encoded color 1 RGB is larger than color 0 RGB.
  352. *
  353. * @param color0 The input unquantized color0 endpoint.
  354. * @param color1 The input unquantized color1 endpoint.
  355. * @param[out] color0_out The output quantized color0 endpoint.
  356. * @param[out] color1_out The output quantized color1 endpoint.
  357. * @param quant_level The quantization level to use.
  358. *
  359. * @return Returns @c false on failure, @c true on success.
  360. */
  361. static bool try_quantize_rgb_delta_blue_contract(
  362. vfloat4 color0,
  363. vfloat4 color1,
  364. vint4& color0_out,
  365. vint4& color1_out,
  366. quant_method quant_level
  367. ) {
  368. // Note: Switch around endpoint colors already at start
  369. std::swap(color0, color1);
  370. // Apply inverse blue-contraction
  371. color0 += color0 - color0.swz<2, 2, 2, 3>();
  372. color1 += color1 - color1.swz<2, 2, 2, 3>();
  373. // If anything overflows BC cannot be used
  374. vmask4 color0_error = (color0 < vfloat4(0.0f)) | (color0 > vfloat4(255.0f));
  375. vmask4 color1_error = (color1 < vfloat4(0.0f)) | (color1 > vfloat4(255.0f));
  376. if (any(color0_error | color1_error))
  377. {
  378. return false;
  379. }
  380. // Transform color0 to unorm9
  381. vint4 color0a = float_to_int_rtn(color0);
  382. color0.set_lane<3>(0.0f);
  383. color0a = lsl<1>(color0a);
  384. // Mask off the top bit
  385. vint4 color0b = color0a & 0xFF;
  386. // Quantize then unquantize in order to get a value that we take differences against
  387. vint4 color0be = quant_color3(quant_level, color0b);
  388. color0b = color0be | (color0a & 0x100);
  389. // Get hold of the second value
  390. vint4 color1d = float_to_int_rtn(color1);
  391. color1d = lsl<1>(color1d);
  392. // ... and take differences
  393. color1d = color1d - color0b;
  394. color1d.set_lane<3>(0);
  395. // Check if the difference is too large to be encodable
  396. if (any((color1d > vint4(63)) | (color1d < vint4(-64))))
  397. {
  398. return false;
  399. }
  400. // Insert top bit of the base into the offset
  401. color1d = color1d & 0x7F;
  402. color1d = color1d | lsr<1>(color0b & 0x100);
  403. // Then quantize and unquantize; if this causes either top two bits to flip, then encoding fails
  404. // since we have then corrupted either the top bit of the base or the sign bit of the offset
  405. vint4 color1de = quant_color3(quant_level, color1d);
  406. vint4 color_flips = (color1d ^ color1de) & 0xC0;
  407. color_flips.set_lane<3>(0);
  408. if (any(color_flips != vint4::zero()))
  409. {
  410. return false;
  411. }
  412. // If the sum of offsets does not trigger blue-contraction then encoding fails
  413. vint4 ep0 = color0be;
  414. vint4 ep1 = color1de;
  415. bit_transfer_signed(ep1, ep0);
  416. if (hadd_rgb_s(ep1) >= 0)
  417. {
  418. return false;
  419. }
  420. // Check that the offsets produce legitimate sums as well
  421. ep0 = ep0 + ep1;
  422. if (any((ep0 < vint4(0)) | (ep0 > vint4(0xFF))))
  423. {
  424. return false;
  425. }
  426. color0_out = color0be;
  427. color1_out = color1de;
  428. return true;
  429. }
  430. /**
  431. * @brief Try to quantize an LDR A color using delta encoding.
  432. *
  433. * At decode time we move one bit from the offset to the base and seize another bit as a sign bit;
  434. * we then unquantize both values as if they contain one extra bit. If the sum of the offsets is
  435. * non-negative, then we encode a regular delta.
  436. *
  437. * This function only compressed the alpha - the other elements in the output array are not touched.
  438. *
  439. * @param color0 The input unquantized color0 endpoint.
  440. * @param color1 The input unquantized color1 endpoint.
  441. * @param[out] color0_out The output quantized color0 endpoint; must preserve lane 0/1/2.
  442. * @param[out] color1_out The output quantized color1 endpoint; must preserve lane 0/1/2.
  443. * @param quant_level The quantization level to use.
  444. *
  445. * @return Returns @c false on failure, @c true on success.
  446. */
  447. static bool try_quantize_alpha_delta(
  448. vfloat4 color0,
  449. vfloat4 color1,
  450. vint4& color0_out,
  451. vint4& color1_out,
  452. quant_method quant_level
  453. ) {
  454. float a0 = color0.lane<3>();
  455. float a1 = color1.lane<3>();
  456. int a0a = astc::flt2int_rtn(a0);
  457. a0a <<= 1;
  458. int a0b = a0a & 0xFF;
  459. int a0be = quant_color(quant_level, a0b);
  460. a0b = a0be;
  461. a0b |= a0a & 0x100;
  462. int a1d = astc::flt2int_rtn(a1);
  463. a1d <<= 1;
  464. a1d -= a0b;
  465. if (a1d > 63 || a1d < -64)
  466. {
  467. return false;
  468. }
  469. a1d &= 0x7F;
  470. a1d |= (a0b & 0x100) >> 1;
  471. int a1de = quant_color(quant_level, a1d);
  472. int a1du = a1de;
  473. if ((a1d ^ a1du) & 0xC0)
  474. {
  475. return false;
  476. }
  477. a1du &= 0x7F;
  478. if (a1du & 0x40)
  479. {
  480. a1du -= 0x80;
  481. }
  482. a1du += a0b;
  483. if (a1du < 0 || a1du > 0x1FF)
  484. {
  485. return false;
  486. }
  487. color0_out.set_lane<3>(a0be);
  488. color1_out.set_lane<3>(a1de);
  489. return true;
  490. }
  491. /**
  492. * @brief Try to quantize an LDR LA color using delta encoding.
  493. *
  494. * At decode time we move one bit from the offset to the base and seize another bit as a sign bit;
  495. * we then unquantize both values as if they contain one extra bit. If the sum of the offsets is
  496. * non-negative, then we encode a regular delta.
  497. *
  498. * This function only compressed the alpha - the other elements in the output array are not touched.
  499. *
  500. * @param color0 The input unquantized color0 endpoint.
  501. * @param color1 The input unquantized color1 endpoint.
  502. * @param[out] output The output endpoints, returned as (l0, l1, a0, a1).
  503. * @param quant_level The quantization level to use.
  504. *
  505. * @return Returns @c false on failure, @c true on success.
  506. */
  507. static bool try_quantize_luminance_alpha_delta(
  508. vfloat4 color0,
  509. vfloat4 color1,
  510. uint8_t output[4],
  511. quant_method quant_level
  512. ) {
  513. float l0 = hadd_rgb_s(color0) * (1.0f / 3.0f);
  514. float l1 = hadd_rgb_s(color1) * (1.0f / 3.0f);
  515. float a0 = color0.lane<3>();
  516. float a1 = color1.lane<3>();
  517. int l0a = astc::flt2int_rtn(l0);
  518. int a0a = astc::flt2int_rtn(a0);
  519. l0a <<= 1;
  520. a0a <<= 1;
  521. int l0b = l0a & 0xFF;
  522. int a0b = a0a & 0xFF;
  523. int l0be = quant_color(quant_level, l0b);
  524. int a0be = quant_color(quant_level, a0b);
  525. l0b = l0be;
  526. a0b = a0be;
  527. l0b |= l0a & 0x100;
  528. a0b |= a0a & 0x100;
  529. int l1d = astc::flt2int_rtn(l1);
  530. int a1d = astc::flt2int_rtn(a1);
  531. l1d <<= 1;
  532. a1d <<= 1;
  533. l1d -= l0b;
  534. a1d -= a0b;
  535. if (l1d > 63 || l1d < -64)
  536. {
  537. return false;
  538. }
  539. if (a1d > 63 || a1d < -64)
  540. {
  541. return false;
  542. }
  543. l1d &= 0x7F;
  544. a1d &= 0x7F;
  545. l1d |= (l0b & 0x100) >> 1;
  546. a1d |= (a0b & 0x100) >> 1;
  547. int l1de = quant_color(quant_level, l1d);
  548. int a1de = quant_color(quant_level, a1d);
  549. int l1du = l1de;
  550. int a1du = a1de;
  551. if ((l1d ^ l1du) & 0xC0)
  552. {
  553. return false;
  554. }
  555. if ((a1d ^ a1du) & 0xC0)
  556. {
  557. return false;
  558. }
  559. l1du &= 0x7F;
  560. a1du &= 0x7F;
  561. if (l1du & 0x40)
  562. {
  563. l1du -= 0x80;
  564. }
  565. if (a1du & 0x40)
  566. {
  567. a1du -= 0x80;
  568. }
  569. l1du += l0b;
  570. a1du += a0b;
  571. if (l1du < 0 || l1du > 0x1FF)
  572. {
  573. return false;
  574. }
  575. if (a1du < 0 || a1du > 0x1FF)
  576. {
  577. return false;
  578. }
  579. output[0] = static_cast<uint8_t>(l0be);
  580. output[1] = static_cast<uint8_t>(l1de);
  581. output[2] = static_cast<uint8_t>(a0be);
  582. output[3] = static_cast<uint8_t>(a1de);
  583. return true;
  584. }
  585. /**
  586. * @brief Try to quantize an LDR RGBA color using delta encoding.
  587. *
  588. * At decode time we move one bit from the offset to the base and seize another bit as a sign bit;
  589. * we then unquantize both values as if they contain one extra bit. If the sum of the offsets is
  590. * non-negative, then we encode a regular delta.
  591. *
  592. * This function only compressed the alpha - the other elements in the output array are not touched.
  593. *
  594. * @param color0 The input unquantized color0 endpoint.
  595. * @param color1 The input unquantized color1 endpoint.
  596. * @param[out] color0_out The output quantized color0 endpoint
  597. * @param[out] color1_out The output quantized color1 endpoint
  598. * @param quant_level The quantization level to use.
  599. *
  600. * @return Returns @c false on failure, @c true on success.
  601. */
  602. static bool try_quantize_rgba_delta(
  603. vfloat4 color0,
  604. vfloat4 color1,
  605. vint4& color0_out,
  606. vint4& color1_out,
  607. quant_method quant_level
  608. ) {
  609. return try_quantize_rgb_delta(color0, color1, color0_out, color1_out, quant_level) &&
  610. try_quantize_alpha_delta(color0, color1, color0_out, color1_out, quant_level);
  611. }
  612. /**
  613. * @brief Try to quantize an LDR RGBA color using delta and blue contract encoding.
  614. *
  615. * At decode time we move one bit from the offset to the base and seize another bit as a sign bit;
  616. * we then unquantize both values as if they contain one extra bit. If the sum of the offsets is
  617. * non-negative, then we encode a regular delta.
  618. *
  619. * This function only compressed the alpha - the other elements in the output array are not touched.
  620. *
  621. * @param color0 The input unquantized color0 endpoint.
  622. * @param color1 The input unquantized color1 endpoint.
  623. * @param[out] color0_out The output quantized color0 endpoint
  624. * @param[out] color1_out The output quantized color1 endpoint
  625. * @param quant_level The quantization level to use.
  626. *
  627. * @return Returns @c false on failure, @c true on success.
  628. */
  629. static bool try_quantize_rgba_delta_blue_contract(
  630. vfloat4 color0,
  631. vfloat4 color1,
  632. vint4& color0_out,
  633. vint4& color1_out,
  634. quant_method quant_level
  635. ) {
  636. // Note that we swap the color0 and color1 ordering for alpha to match RGB blue-contract
  637. return try_quantize_rgb_delta_blue_contract(color0, color1, color0_out, color1_out, quant_level) &&
  638. try_quantize_alpha_delta(color1, color0, color0_out, color1_out, quant_level);
  639. }
  640. /**
  641. * @brief Quantize an LDR RGB color using scale encoding.
  642. *
  643. * @param color The input unquantized color endpoint and scale factor.
  644. * @param[out] output The output endpoints, returned as (r0, g0, b0, s).
  645. * @param quant_level The quantization level to use.
  646. */
  647. static void quantize_rgbs(
  648. vfloat4 color,
  649. uint8_t output[4],
  650. quant_method quant_level
  651. ) {
  652. float scale = 1.0f / 257.0f;
  653. float r = astc::clamp255f(color.lane<0>() * scale);
  654. float g = astc::clamp255f(color.lane<1>() * scale);
  655. float b = astc::clamp255f(color.lane<2>() * scale);
  656. int ri = quant_color(quant_level, astc::flt2int_rtn(r), r);
  657. int gi = quant_color(quant_level, astc::flt2int_rtn(g), g);
  658. int bi = quant_color(quant_level, astc::flt2int_rtn(b), b);
  659. float oldcolorsum = hadd_rgb_s(color) * scale;
  660. float newcolorsum = static_cast<float>(ri + gi + bi);
  661. float scalea = astc::clamp1f(color.lane<3>() * (oldcolorsum + 1e-10f) / (newcolorsum + 1e-10f));
  662. int scale_idx = astc::flt2int_rtn(scalea * 256.0f);
  663. scale_idx = astc::clamp(scale_idx, 0, 255);
  664. output[0] = static_cast<uint8_t>(ri);
  665. output[1] = static_cast<uint8_t>(gi);
  666. output[2] = static_cast<uint8_t>(bi);
  667. output[3] = quant_color(quant_level, scale_idx);
  668. }
  669. /**
  670. * @brief Quantize an LDR RGBA color using scale encoding.
  671. *
  672. * @param color0 The input unquantized color0 alpha endpoint.
  673. * @param color1 The input unquantized color1 alpha endpoint.
  674. * @param color The input unquantized color endpoint and scale factor.
  675. * @param[out] output The output endpoints, returned as (r0, g0, b0, s, a0, a1).
  676. * @param quant_level The quantization level to use.
  677. */
  678. static void quantize_rgbs_alpha(
  679. vfloat4 color0,
  680. vfloat4 color1,
  681. vfloat4 color,
  682. uint8_t output[6],
  683. quant_method quant_level
  684. ) {
  685. float a0 = color0.lane<3>();
  686. float a1 = color1.lane<3>();
  687. output[4] = quant_color(quant_level, astc::flt2int_rtn(a0), a0);
  688. output[5] = quant_color(quant_level, astc::flt2int_rtn(a1), a1);
  689. quantize_rgbs(color, output, quant_level);
  690. }
  691. /**
  692. * @brief Quantize a LDR L color.
  693. *
  694. * @param color0 The input unquantized color0 endpoint.
  695. * @param color1 The input unquantized color1 endpoint.
  696. * @param[out] output The output endpoints, returned as (l0, l1).
  697. * @param quant_level The quantization level to use.
  698. */
  699. static void quantize_luminance(
  700. vfloat4 color0,
  701. vfloat4 color1,
  702. uint8_t output[2],
  703. quant_method quant_level
  704. ) {
  705. float lum0 = hadd_rgb_s(color0) * (1.0f / 3.0f);
  706. float lum1 = hadd_rgb_s(color1) * (1.0f / 3.0f);
  707. if (lum0 > lum1)
  708. {
  709. float avg = (lum0 + lum1) * 0.5f;
  710. lum0 = avg;
  711. lum1 = avg;
  712. }
  713. output[0] = quant_color(quant_level, astc::flt2int_rtn(lum0), lum0);
  714. output[1] = quant_color(quant_level, astc::flt2int_rtn(lum1), lum1);
  715. }
  716. /**
  717. * @brief Quantize a LDR LA color.
  718. *
  719. * @param color0 The input unquantized color0 endpoint.
  720. * @param color1 The input unquantized color1 endpoint.
  721. * @param[out] output The output endpoints, returned as (l0, l1, a0, a1).
  722. * @param quant_level The quantization level to use.
  723. */
  724. static void quantize_luminance_alpha(
  725. vfloat4 color0,
  726. vfloat4 color1,
  727. uint8_t output[4],
  728. quant_method quant_level
  729. ) {
  730. float lum0 = hadd_rgb_s(color0) * (1.0f / 3.0f);
  731. float lum1 = hadd_rgb_s(color1) * (1.0f / 3.0f);
  732. float a0 = color0.lane<3>();
  733. float a1 = color1.lane<3>();
  734. output[0] = quant_color(quant_level, astc::flt2int_rtn(lum0), lum0);
  735. output[1] = quant_color(quant_level, astc::flt2int_rtn(lum1), lum1);
  736. output[2] = quant_color(quant_level, astc::flt2int_rtn(a0), a0);
  737. output[3] = quant_color(quant_level, astc::flt2int_rtn(a1), a1);
  738. }
  739. /**
  740. * @brief Quantize and unquantize a value ensuring top two bits are the same.
  741. *
  742. * @param quant_level The quantization level to use.
  743. * @param value The input unquantized value.
  744. * @param[out] quant_value The quantized value.
  745. */
  746. static inline void quantize_and_unquantize_retain_top_two_bits(
  747. quant_method quant_level,
  748. uint8_t value,
  749. uint8_t& quant_value
  750. ) {
  751. int perform_loop;
  752. uint8_t quantval;
  753. do
  754. {
  755. quantval = quant_color(quant_level, value);
  756. // Perform looping if the top two bits were modified by quant/unquant
  757. perform_loop = (value & 0xC0) != (quantval & 0xC0);
  758. if ((quantval & 0xC0) > (value & 0xC0))
  759. {
  760. // Quant/unquant rounded UP so that the top two bits changed;
  761. // decrement the input in hopes that this will avoid rounding up.
  762. value--;
  763. }
  764. else if ((quantval & 0xC0) < (value & 0xC0))
  765. {
  766. // Quant/unquant rounded DOWN so that the top two bits changed;
  767. // decrement the input in hopes that this will avoid rounding down.
  768. value--;
  769. }
  770. } while (perform_loop);
  771. quant_value = quantval;
  772. }
  773. /**
  774. * @brief Quantize and unquantize a value ensuring top four bits are the same.
  775. *
  776. * @param quant_level The quantization level to use.
  777. * @param value The input unquantized value.
  778. * @param[out] quant_value The quantized value in 0-255 range.
  779. */
  780. static inline void quantize_and_unquantize_retain_top_four_bits(
  781. quant_method quant_level,
  782. uint8_t value,
  783. uint8_t& quant_value
  784. ) {
  785. uint8_t perform_loop;
  786. uint8_t quantval;
  787. do
  788. {
  789. quantval = quant_color(quant_level, value);
  790. // Perform looping if the top four bits were modified by quant/unquant
  791. perform_loop = (value & 0xF0) != (quantval & 0xF0);
  792. if ((quantval & 0xF0) > (value & 0xF0))
  793. {
  794. // Quant/unquant rounded UP so that the top four bits changed;
  795. // decrement the input value in hopes that this will avoid rounding up.
  796. value--;
  797. }
  798. else if ((quantval & 0xF0) < (value & 0xF0))
  799. {
  800. // Quant/unquant rounded DOWN so that the top four bits changed;
  801. // decrement the input value in hopes that this will avoid rounding down.
  802. value--;
  803. }
  804. } while (perform_loop);
  805. quant_value = quantval;
  806. }
  807. /**
  808. * @brief Quantize a HDR RGB color using RGB + offset.
  809. *
  810. * @param color The input unquantized color endpoint and offset.
  811. * @param[out] output The output endpoints, returned as packed RGBS with some mode bits.
  812. * @param quant_level The quantization level to use.
  813. */
  814. static void quantize_hdr_rgbo(
  815. vfloat4 color,
  816. uint8_t output[4],
  817. quant_method quant_level
  818. ) {
  819. color.set_lane<0>(color.lane<0>() + color.lane<3>());
  820. color.set_lane<1>(color.lane<1>() + color.lane<3>());
  821. color.set_lane<2>(color.lane<2>() + color.lane<3>());
  822. color = clamp(0.0f, 65535.0f, color);
  823. vfloat4 color_bak = color;
  824. int majcomp;
  825. if (color.lane<0>() > color.lane<1>() && color.lane<0>() > color.lane<2>())
  826. {
  827. majcomp = 0; // red is largest component
  828. }
  829. else if (color.lane<1>() > color.lane<2>())
  830. {
  831. majcomp = 1; // green is largest component
  832. }
  833. else
  834. {
  835. majcomp = 2; // blue is largest component
  836. }
  837. // swap around the red component and the largest component.
  838. switch (majcomp)
  839. {
  840. case 1:
  841. color = color.swz<1, 0, 2, 3>();
  842. break;
  843. case 2:
  844. color = color.swz<2, 1, 0, 3>();
  845. break;
  846. default:
  847. break;
  848. }
  849. static const int mode_bits[5][3] {
  850. {11, 5, 7},
  851. {11, 6, 5},
  852. {10, 5, 8},
  853. {9, 6, 7},
  854. {8, 7, 6}
  855. };
  856. static const float mode_cutoffs[5][2] {
  857. {1024, 4096},
  858. {2048, 1024},
  859. {2048, 16384},
  860. {8192, 16384},
  861. {32768, 16384}
  862. };
  863. static const float mode_rscales[5] {
  864. 32.0f,
  865. 32.0f,
  866. 64.0f,
  867. 128.0f,
  868. 256.0f,
  869. };
  870. static const float mode_scales[5] {
  871. 1.0f / 32.0f,
  872. 1.0f / 32.0f,
  873. 1.0f / 64.0f,
  874. 1.0f / 128.0f,
  875. 1.0f / 256.0f,
  876. };
  877. float r_base = color.lane<0>();
  878. float g_base = color.lane<0>() - color.lane<1>() ;
  879. float b_base = color.lane<0>() - color.lane<2>() ;
  880. float s_base = color.lane<3>() ;
  881. for (int mode = 0; mode < 5; mode++)
  882. {
  883. if (g_base > mode_cutoffs[mode][0] || b_base > mode_cutoffs[mode][0] || s_base > mode_cutoffs[mode][1])
  884. {
  885. continue;
  886. }
  887. // Encode the mode into a 4-bit vector
  888. int mode_enc = mode < 4 ? (mode | (majcomp << 2)) : (majcomp | 0xC);
  889. float mode_scale = mode_scales[mode];
  890. float mode_rscale = mode_rscales[mode];
  891. int gb_intcutoff = 1 << mode_bits[mode][1];
  892. int s_intcutoff = 1 << mode_bits[mode][2];
  893. // Quantize and unquantize R
  894. int r_intval = astc::flt2int_rtn(r_base * mode_scale);
  895. int r_lowbits = r_intval & 0x3f;
  896. r_lowbits |= (mode_enc & 3) << 6;
  897. uint8_t r_quantval;
  898. quantize_and_unquantize_retain_top_two_bits(
  899. quant_level, static_cast<uint8_t>(r_lowbits), r_quantval);
  900. r_intval = (r_intval & ~0x3f) | (r_quantval & 0x3f);
  901. float r_fval = static_cast<float>(r_intval) * mode_rscale;
  902. // Recompute G and B, then quantize and unquantize them
  903. float g_fval = r_fval - color.lane<1>() ;
  904. float b_fval = r_fval - color.lane<2>() ;
  905. g_fval = astc::clamp(g_fval, 0.0f, 65535.0f);
  906. b_fval = astc::clamp(b_fval, 0.0f, 65535.0f);
  907. int g_intval = astc::flt2int_rtn(g_fval * mode_scale);
  908. int b_intval = astc::flt2int_rtn(b_fval * mode_scale);
  909. if (g_intval >= gb_intcutoff || b_intval >= gb_intcutoff)
  910. {
  911. continue;
  912. }
  913. int g_lowbits = g_intval & 0x1f;
  914. int b_lowbits = b_intval & 0x1f;
  915. int bit0 = 0;
  916. int bit1 = 0;
  917. int bit2 = 0;
  918. int bit3 = 0;
  919. switch (mode)
  920. {
  921. case 0:
  922. case 2:
  923. bit0 = (r_intval >> 9) & 1;
  924. break;
  925. case 1:
  926. case 3:
  927. bit0 = (r_intval >> 8) & 1;
  928. break;
  929. case 4:
  930. case 5:
  931. bit0 = (g_intval >> 6) & 1;
  932. break;
  933. }
  934. switch (mode)
  935. {
  936. case 0:
  937. case 1:
  938. case 2:
  939. case 3:
  940. bit2 = (r_intval >> 7) & 1;
  941. break;
  942. case 4:
  943. case 5:
  944. bit2 = (b_intval >> 6) & 1;
  945. break;
  946. }
  947. switch (mode)
  948. {
  949. case 0:
  950. case 2:
  951. bit1 = (r_intval >> 8) & 1;
  952. break;
  953. case 1:
  954. case 3:
  955. case 4:
  956. case 5:
  957. bit1 = (g_intval >> 5) & 1;
  958. break;
  959. }
  960. switch (mode)
  961. {
  962. case 0:
  963. bit3 = (r_intval >> 10) & 1;
  964. break;
  965. case 2:
  966. bit3 = (r_intval >> 6) & 1;
  967. break;
  968. case 1:
  969. case 3:
  970. case 4:
  971. case 5:
  972. bit3 = (b_intval >> 5) & 1;
  973. break;
  974. }
  975. g_lowbits |= (mode_enc & 0x4) << 5;
  976. b_lowbits |= (mode_enc & 0x8) << 4;
  977. g_lowbits |= bit0 << 6;
  978. g_lowbits |= bit1 << 5;
  979. b_lowbits |= bit2 << 6;
  980. b_lowbits |= bit3 << 5;
  981. uint8_t g_quantval;
  982. uint8_t b_quantval;
  983. quantize_and_unquantize_retain_top_four_bits(
  984. quant_level, static_cast<uint8_t>(g_lowbits), g_quantval);
  985. quantize_and_unquantize_retain_top_four_bits(
  986. quant_level, static_cast<uint8_t>(b_lowbits), b_quantval);
  987. g_intval = (g_intval & ~0x1f) | (g_quantval & 0x1f);
  988. b_intval = (b_intval & ~0x1f) | (b_quantval & 0x1f);
  989. g_fval = static_cast<float>(g_intval) * mode_rscale;
  990. b_fval = static_cast<float>(b_intval) * mode_rscale;
  991. // Recompute the scale value, based on the errors introduced to red, green and blue
  992. // If the error is positive, then the R,G,B errors combined have raised the color
  993. // value overall; as such, the scale value needs to be increased.
  994. float rgb_errorsum = (r_fval - color.lane<0>() ) + (r_fval - g_fval - color.lane<1>() ) + (r_fval - b_fval - color.lane<2>() );
  995. float s_fval = s_base + rgb_errorsum * (1.0f / 3.0f);
  996. s_fval = astc::clamp(s_fval, 0.0f, 1e9f);
  997. int s_intval = astc::flt2int_rtn(s_fval * mode_scale);
  998. if (s_intval >= s_intcutoff)
  999. {
  1000. continue;
  1001. }
  1002. int s_lowbits = s_intval & 0x1f;
  1003. int bit4;
  1004. int bit5;
  1005. int bit6;
  1006. switch (mode)
  1007. {
  1008. case 1:
  1009. bit6 = (r_intval >> 9) & 1;
  1010. break;
  1011. default:
  1012. bit6 = (s_intval >> 5) & 1;
  1013. break;
  1014. }
  1015. switch (mode)
  1016. {
  1017. case 4:
  1018. bit5 = (r_intval >> 7) & 1;
  1019. break;
  1020. case 1:
  1021. bit5 = (r_intval >> 10) & 1;
  1022. break;
  1023. default:
  1024. bit5 = (s_intval >> 6) & 1;
  1025. break;
  1026. }
  1027. switch (mode)
  1028. {
  1029. case 2:
  1030. bit4 = (s_intval >> 7) & 1;
  1031. break;
  1032. default:
  1033. bit4 = (r_intval >> 6) & 1;
  1034. break;
  1035. }
  1036. s_lowbits |= bit6 << 5;
  1037. s_lowbits |= bit5 << 6;
  1038. s_lowbits |= bit4 << 7;
  1039. uint8_t s_quantval;
  1040. quantize_and_unquantize_retain_top_four_bits(
  1041. quant_level, static_cast<uint8_t>(s_lowbits), s_quantval);
  1042. output[0] = r_quantval;
  1043. output[1] = g_quantval;
  1044. output[2] = b_quantval;
  1045. output[3] = s_quantval;
  1046. return;
  1047. }
  1048. // Failed to encode any of the modes above? In that case encode using mode #5
  1049. float vals[4];
  1050. vals[0] = color_bak.lane<0>();
  1051. vals[1] = color_bak.lane<1>();
  1052. vals[2] = color_bak.lane<2>();
  1053. vals[3] = color_bak.lane<3>();
  1054. int ivals[4];
  1055. float cvals[3];
  1056. for (int i = 0; i < 3; i++)
  1057. {
  1058. vals[i] = astc::clamp(vals[i], 0.0f, 65020.0f);
  1059. ivals[i] = astc::flt2int_rtn(vals[i] * (1.0f / 512.0f));
  1060. cvals[i] = static_cast<float>(ivals[i]) * 512.0f;
  1061. }
  1062. float rgb_errorsum = (cvals[0] - vals[0]) + (cvals[1] - vals[1]) + (cvals[2] - vals[2]);
  1063. vals[3] += rgb_errorsum * (1.0f / 3.0f);
  1064. vals[3] = astc::clamp(vals[3], 0.0f, 65020.0f);
  1065. ivals[3] = astc::flt2int_rtn(vals[3] * (1.0f / 512.0f));
  1066. int encvals[4];
  1067. encvals[0] = (ivals[0] & 0x3f) | 0xC0;
  1068. encvals[1] = (ivals[1] & 0x7f) | 0x80;
  1069. encvals[2] = (ivals[2] & 0x7f) | 0x80;
  1070. encvals[3] = (ivals[3] & 0x7f) | ((ivals[0] & 0x40) << 1);
  1071. for (uint8_t i = 0; i < 4; i++)
  1072. {
  1073. quantize_and_unquantize_retain_top_four_bits(
  1074. quant_level, static_cast<uint8_t>(encvals[i]), output[i]);
  1075. }
  1076. return;
  1077. }
  1078. /**
  1079. * @brief Quantize a HDR RGB color using direct RGB encoding.
  1080. *
  1081. * @param color0 The input unquantized color0 endpoint.
  1082. * @param color1 The input unquantized color1 endpoint.
  1083. * @param[out] output The output endpoints, returned as packed RGB+RGB pairs with mode bits.
  1084. * @param quant_level The quantization level to use.
  1085. */
  1086. static void quantize_hdr_rgb(
  1087. vfloat4 color0,
  1088. vfloat4 color1,
  1089. uint8_t output[6],
  1090. quant_method quant_level
  1091. ) {
  1092. // Note: color*.lane<3> is not used so we can ignore it
  1093. color0 = clamp(0.0f, 65535.0f, color0);
  1094. color1 = clamp(0.0f, 65535.0f, color1);
  1095. vfloat4 color0_bak = color0;
  1096. vfloat4 color1_bak = color1;
  1097. int majcomp;
  1098. if (color1.lane<0>() > color1.lane<1>() && color1.lane<0>() > color1.lane<2>())
  1099. {
  1100. majcomp = 0;
  1101. }
  1102. else if (color1.lane<1>() > color1.lane<2>())
  1103. {
  1104. majcomp = 1;
  1105. }
  1106. else
  1107. {
  1108. majcomp = 2;
  1109. }
  1110. // Swizzle the components
  1111. switch (majcomp)
  1112. {
  1113. case 1: // red-green swap
  1114. color0 = color0.swz<1, 0, 2, 3>();
  1115. color1 = color1.swz<1, 0, 2, 3>();
  1116. break;
  1117. case 2: // red-blue swap
  1118. color0 = color0.swz<2, 1, 0, 3>();
  1119. color1 = color1.swz<2, 1, 0, 3>();
  1120. break;
  1121. default:
  1122. break;
  1123. }
  1124. float a_base = color1.lane<0>();
  1125. a_base = astc::clamp(a_base, 0.0f, 65535.0f);
  1126. float b0_base = a_base - color1.lane<1>();
  1127. float b1_base = a_base - color1.lane<2>();
  1128. float c_base = a_base - color0.lane<0>();
  1129. float d0_base = a_base - b0_base - c_base - color0.lane<1>();
  1130. float d1_base = a_base - b1_base - c_base - color0.lane<2>();
  1131. // Number of bits in the various fields in the various modes
  1132. static const int mode_bits[8][4] {
  1133. {9, 7, 6, 7},
  1134. {9, 8, 6, 6},
  1135. {10, 6, 7, 7},
  1136. {10, 7, 7, 6},
  1137. {11, 8, 6, 5},
  1138. {11, 6, 8, 6},
  1139. {12, 7, 7, 5},
  1140. {12, 6, 7, 6}
  1141. };
  1142. // Cutoffs to use for the computed values of a,b,c,d, assuming the
  1143. // range 0..65535 are LNS values corresponding to fp16.
  1144. static const float mode_cutoffs[8][4] {
  1145. {16384, 8192, 8192, 8}, // mode 0: 9,7,6,7
  1146. {32768, 8192, 4096, 8}, // mode 1: 9,8,6,6
  1147. {4096, 8192, 4096, 4}, // mode 2: 10,6,7,7
  1148. {8192, 8192, 2048, 4}, // mode 3: 10,7,7,6
  1149. {8192, 2048, 512, 2}, // mode 4: 11,8,6,5
  1150. {2048, 8192, 1024, 2}, // mode 5: 11,6,8,6
  1151. {2048, 2048, 256, 1}, // mode 6: 12,7,7,5
  1152. {1024, 2048, 512, 1}, // mode 7: 12,6,7,6
  1153. };
  1154. static const float mode_scales[8] {
  1155. 1.0f / 128.0f,
  1156. 1.0f / 128.0f,
  1157. 1.0f / 64.0f,
  1158. 1.0f / 64.0f,
  1159. 1.0f / 32.0f,
  1160. 1.0f / 32.0f,
  1161. 1.0f / 16.0f,
  1162. 1.0f / 16.0f,
  1163. };
  1164. // Scaling factors when going from what was encoded in the mode to 16 bits.
  1165. static const float mode_rscales[8] {
  1166. 128.0f,
  1167. 128.0f,
  1168. 64.0f,
  1169. 64.0f,
  1170. 32.0f,
  1171. 32.0f,
  1172. 16.0f,
  1173. 16.0f
  1174. };
  1175. // Try modes one by one, with the highest-precision mode first.
  1176. for (int mode = 7; mode >= 0; mode--)
  1177. {
  1178. // For each mode, test if we can in fact accommodate the computed b, c, and d values.
  1179. // If we clearly can't, then we skip to the next mode.
  1180. float b_cutoff = mode_cutoffs[mode][0];
  1181. float c_cutoff = mode_cutoffs[mode][1];
  1182. float d_cutoff = mode_cutoffs[mode][2];
  1183. if (b0_base > b_cutoff || b1_base > b_cutoff || c_base > c_cutoff || fabsf(d0_base) > d_cutoff || fabsf(d1_base) > d_cutoff)
  1184. {
  1185. continue;
  1186. }
  1187. float mode_scale = mode_scales[mode];
  1188. float mode_rscale = mode_rscales[mode];
  1189. int b_intcutoff = 1 << mode_bits[mode][1];
  1190. int c_intcutoff = 1 << mode_bits[mode][2];
  1191. int d_intcutoff = 1 << (mode_bits[mode][3] - 1);
  1192. // Quantize and unquantize A, with the assumption that its high bits can be handled safely.
  1193. int a_intval = astc::flt2int_rtn(a_base * mode_scale);
  1194. int a_lowbits = a_intval & 0xFF;
  1195. int a_quantval = quant_color(quant_level, a_lowbits);
  1196. int a_uquantval = a_quantval;
  1197. a_intval = (a_intval & ~0xFF) | a_uquantval;
  1198. float a_fval = static_cast<float>(a_intval) * mode_rscale;
  1199. // Recompute C, then quantize and unquantize it
  1200. float c_fval = a_fval - color0.lane<0>();
  1201. c_fval = astc::clamp(c_fval, 0.0f, 65535.0f);
  1202. int c_intval = astc::flt2int_rtn(c_fval * mode_scale);
  1203. if (c_intval >= c_intcutoff)
  1204. {
  1205. continue;
  1206. }
  1207. int c_lowbits = c_intval & 0x3f;
  1208. c_lowbits |= (mode & 1) << 7;
  1209. c_lowbits |= (a_intval & 0x100) >> 2;
  1210. uint8_t c_quantval;
  1211. quantize_and_unquantize_retain_top_two_bits(
  1212. quant_level, static_cast<uint8_t>(c_lowbits), c_quantval);
  1213. c_intval = (c_intval & ~0x3F) | (c_quantval & 0x3F);
  1214. c_fval = static_cast<float>(c_intval) * mode_rscale;
  1215. // Recompute B0 and B1, then quantize and unquantize them
  1216. float b0_fval = a_fval - color1.lane<1>();
  1217. float b1_fval = a_fval - color1.lane<2>();
  1218. b0_fval = astc::clamp(b0_fval, 0.0f, 65535.0f);
  1219. b1_fval = astc::clamp(b1_fval, 0.0f, 65535.0f);
  1220. int b0_intval = astc::flt2int_rtn(b0_fval * mode_scale);
  1221. int b1_intval = astc::flt2int_rtn(b1_fval * mode_scale);
  1222. if (b0_intval >= b_intcutoff || b1_intval >= b_intcutoff)
  1223. {
  1224. continue;
  1225. }
  1226. int b0_lowbits = b0_intval & 0x3f;
  1227. int b1_lowbits = b1_intval & 0x3f;
  1228. int bit0 = 0;
  1229. int bit1 = 0;
  1230. switch (mode)
  1231. {
  1232. case 0:
  1233. case 1:
  1234. case 3:
  1235. case 4:
  1236. case 6:
  1237. bit0 = (b0_intval >> 6) & 1;
  1238. break;
  1239. case 2:
  1240. case 5:
  1241. case 7:
  1242. bit0 = (a_intval >> 9) & 1;
  1243. break;
  1244. }
  1245. switch (mode)
  1246. {
  1247. case 0:
  1248. case 1:
  1249. case 3:
  1250. case 4:
  1251. case 6:
  1252. bit1 = (b1_intval >> 6) & 1;
  1253. break;
  1254. case 2:
  1255. bit1 = (c_intval >> 6) & 1;
  1256. break;
  1257. case 5:
  1258. case 7:
  1259. bit1 = (a_intval >> 10) & 1;
  1260. break;
  1261. }
  1262. b0_lowbits |= bit0 << 6;
  1263. b1_lowbits |= bit1 << 6;
  1264. b0_lowbits |= ((mode >> 1) & 1) << 7;
  1265. b1_lowbits |= ((mode >> 2) & 1) << 7;
  1266. uint8_t b0_quantval;
  1267. uint8_t b1_quantval;
  1268. quantize_and_unquantize_retain_top_two_bits(
  1269. quant_level, static_cast<uint8_t>(b0_lowbits), b0_quantval);
  1270. quantize_and_unquantize_retain_top_two_bits(
  1271. quant_level, static_cast<uint8_t>(b1_lowbits), b1_quantval);
  1272. b0_intval = (b0_intval & ~0x3f) | (b0_quantval & 0x3f);
  1273. b1_intval = (b1_intval & ~0x3f) | (b1_quantval & 0x3f);
  1274. b0_fval = static_cast<float>(b0_intval) * mode_rscale;
  1275. b1_fval = static_cast<float>(b1_intval) * mode_rscale;
  1276. // Recompute D0 and D1, then quantize and unquantize them
  1277. float d0_fval = a_fval - b0_fval - c_fval - color0.lane<1>();
  1278. float d1_fval = a_fval - b1_fval - c_fval - color0.lane<2>();
  1279. d0_fval = astc::clamp(d0_fval, -65535.0f, 65535.0f);
  1280. d1_fval = astc::clamp(d1_fval, -65535.0f, 65535.0f);
  1281. int d0_intval = astc::flt2int_rtn(d0_fval * mode_scale);
  1282. int d1_intval = astc::flt2int_rtn(d1_fval * mode_scale);
  1283. if (abs(d0_intval) >= d_intcutoff || abs(d1_intval) >= d_intcutoff)
  1284. {
  1285. continue;
  1286. }
  1287. int d0_lowbits = d0_intval & 0x1f;
  1288. int d1_lowbits = d1_intval & 0x1f;
  1289. int bit2 = 0;
  1290. int bit3 = 0;
  1291. int bit4;
  1292. int bit5;
  1293. switch (mode)
  1294. {
  1295. case 0:
  1296. case 2:
  1297. bit2 = (d0_intval >> 6) & 1;
  1298. break;
  1299. case 1:
  1300. case 4:
  1301. bit2 = (b0_intval >> 7) & 1;
  1302. break;
  1303. case 3:
  1304. bit2 = (a_intval >> 9) & 1;
  1305. break;
  1306. case 5:
  1307. bit2 = (c_intval >> 7) & 1;
  1308. break;
  1309. case 6:
  1310. case 7:
  1311. bit2 = (a_intval >> 11) & 1;
  1312. break;
  1313. }
  1314. switch (mode)
  1315. {
  1316. case 0:
  1317. case 2:
  1318. bit3 = (d1_intval >> 6) & 1;
  1319. break;
  1320. case 1:
  1321. case 4:
  1322. bit3 = (b1_intval >> 7) & 1;
  1323. break;
  1324. case 3:
  1325. case 5:
  1326. case 6:
  1327. case 7:
  1328. bit3 = (c_intval >> 6) & 1;
  1329. break;
  1330. }
  1331. switch (mode)
  1332. {
  1333. case 4:
  1334. case 6:
  1335. bit4 = (a_intval >> 9) & 1;
  1336. bit5 = (a_intval >> 10) & 1;
  1337. break;
  1338. default:
  1339. bit4 = (d0_intval >> 5) & 1;
  1340. bit5 = (d1_intval >> 5) & 1;
  1341. break;
  1342. }
  1343. d0_lowbits |= bit2 << 6;
  1344. d1_lowbits |= bit3 << 6;
  1345. d0_lowbits |= bit4 << 5;
  1346. d1_lowbits |= bit5 << 5;
  1347. d0_lowbits |= (majcomp & 1) << 7;
  1348. d1_lowbits |= ((majcomp >> 1) & 1) << 7;
  1349. uint8_t d0_quantval;
  1350. uint8_t d1_quantval;
  1351. quantize_and_unquantize_retain_top_four_bits(
  1352. quant_level, static_cast<uint8_t>(d0_lowbits), d0_quantval);
  1353. quantize_and_unquantize_retain_top_four_bits(
  1354. quant_level, static_cast<uint8_t>(d1_lowbits), d1_quantval);
  1355. output[0] = static_cast<uint8_t>(a_quantval);
  1356. output[1] = c_quantval;
  1357. output[2] = b0_quantval;
  1358. output[3] = b1_quantval;
  1359. output[4] = d0_quantval;
  1360. output[5] = d1_quantval;
  1361. return;
  1362. }
  1363. // If neither of the modes fit we will use a flat representation for storing data, using 8 bits
  1364. // for red and green, and 7 bits for blue. This gives color accuracy roughly similar to LDR
  1365. // 4:4:3 which is not at all great but usable. This representation is used if the light color is
  1366. // more than 4x the color value of the dark color.
  1367. float vals[6];
  1368. vals[0] = color0_bak.lane<0>();
  1369. vals[1] = color1_bak.lane<0>();
  1370. vals[2] = color0_bak.lane<1>();
  1371. vals[3] = color1_bak.lane<1>();
  1372. vals[4] = color0_bak.lane<2>();
  1373. vals[5] = color1_bak.lane<2>();
  1374. for (int i = 0; i < 6; i++)
  1375. {
  1376. vals[i] = astc::clamp(vals[i], 0.0f, 65020.0f);
  1377. }
  1378. for (int i = 0; i < 4; i++)
  1379. {
  1380. int idx = astc::flt2int_rtn(vals[i] * 1.0f / 256.0f);
  1381. output[i] = quant_color(quant_level, idx);
  1382. }
  1383. for (int i = 4; i < 6; i++)
  1384. {
  1385. int idx = astc::flt2int_rtn(vals[i] * 1.0f / 512.0f) + 128;
  1386. quantize_and_unquantize_retain_top_two_bits(
  1387. quant_level, static_cast<uint8_t>(idx), output[i]);
  1388. }
  1389. return;
  1390. }
  1391. /**
  1392. * @brief Quantize a HDR RGB + LDR A color using direct RGBA encoding.
  1393. *
  1394. * @param color0 The input unquantized color0 endpoint.
  1395. * @param color1 The input unquantized color1 endpoint.
  1396. * @param[out] output The output endpoints, returned as packed RGBA+RGBA pairs with mode bits.
  1397. * @param quant_level The quantization level to use.
  1398. */
  1399. static void quantize_hdr_rgb_ldr_alpha(
  1400. vfloat4 color0,
  1401. vfloat4 color1,
  1402. uint8_t output[8],
  1403. quant_method quant_level
  1404. ) {
  1405. float scale = 1.0f / 257.0f;
  1406. float a0 = astc::clamp255f(color0.lane<3>() * scale);
  1407. float a1 = astc::clamp255f(color1.lane<3>() * scale);
  1408. output[6] = quant_color(quant_level, astc::flt2int_rtn(a0), a0);
  1409. output[7] = quant_color(quant_level, astc::flt2int_rtn(a1), a1);
  1410. quantize_hdr_rgb(color0, color1, output, quant_level);
  1411. }
  1412. /**
  1413. * @brief Quantize a HDR L color using the large range encoding.
  1414. *
  1415. * @param color0 The input unquantized color0 endpoint.
  1416. * @param color1 The input unquantized color1 endpoint.
  1417. * @param[out] output The output endpoints, returned as packed (l0, l1).
  1418. * @param quant_level The quantization level to use.
  1419. */
  1420. static void quantize_hdr_luminance_large_range(
  1421. vfloat4 color0,
  1422. vfloat4 color1,
  1423. uint8_t output[2],
  1424. quant_method quant_level
  1425. ) {
  1426. float lum0 = hadd_rgb_s(color0) * (1.0f / 3.0f);
  1427. float lum1 = hadd_rgb_s(color1) * (1.0f / 3.0f);
  1428. if (lum1 < lum0)
  1429. {
  1430. float avg = (lum0 + lum1) * 0.5f;
  1431. lum0 = avg;
  1432. lum1 = avg;
  1433. }
  1434. int ilum1 = astc::flt2int_rtn(lum1);
  1435. int ilum0 = astc::flt2int_rtn(lum0);
  1436. // Find the closest encodable point in the upper half of the code-point space
  1437. int upper_v0 = (ilum0 + 128) >> 8;
  1438. int upper_v1 = (ilum1 + 128) >> 8;
  1439. upper_v0 = astc::clamp(upper_v0, 0, 255);
  1440. upper_v1 = astc::clamp(upper_v1, 0, 255);
  1441. // Find the closest encodable point in the lower half of the code-point space
  1442. int lower_v0 = (ilum1 + 256) >> 8;
  1443. int lower_v1 = ilum0 >> 8;
  1444. lower_v0 = astc::clamp(lower_v0, 0, 255);
  1445. lower_v1 = astc::clamp(lower_v1, 0, 255);
  1446. // Determine the distance between the point in code-point space and the input value
  1447. int upper0_dec = upper_v0 << 8;
  1448. int upper1_dec = upper_v1 << 8;
  1449. int lower0_dec = (lower_v1 << 8) + 128;
  1450. int lower1_dec = (lower_v0 << 8) - 128;
  1451. int upper0_diff = upper0_dec - ilum0;
  1452. int upper1_diff = upper1_dec - ilum1;
  1453. int lower0_diff = lower0_dec - ilum0;
  1454. int lower1_diff = lower1_dec - ilum1;
  1455. int upper_error = (upper0_diff * upper0_diff) + (upper1_diff * upper1_diff);
  1456. int lower_error = (lower0_diff * lower0_diff) + (lower1_diff * lower1_diff);
  1457. int v0, v1;
  1458. if (upper_error < lower_error)
  1459. {
  1460. v0 = upper_v0;
  1461. v1 = upper_v1;
  1462. }
  1463. else
  1464. {
  1465. v0 = lower_v0;
  1466. v1 = lower_v1;
  1467. }
  1468. // OK; encode
  1469. output[0] = quant_color(quant_level, v0);
  1470. output[1] = quant_color(quant_level, v1);
  1471. }
  1472. /**
  1473. * @brief Quantize a HDR L color using the small range encoding.
  1474. *
  1475. * @param color0 The input unquantized color0 endpoint.
  1476. * @param color1 The input unquantized color1 endpoint.
  1477. * @param[out] output The output endpoints, returned as packed (l0, l1) with mode bits.
  1478. * @param quant_level The quantization level to use.
  1479. *
  1480. * @return Returns @c false on failure, @c true on success.
  1481. */
  1482. static bool try_quantize_hdr_luminance_small_range(
  1483. vfloat4 color0,
  1484. vfloat4 color1,
  1485. uint8_t output[2],
  1486. quant_method quant_level
  1487. ) {
  1488. float lum0 = hadd_rgb_s(color0) * (1.0f / 3.0f);
  1489. float lum1 = hadd_rgb_s(color1) * (1.0f / 3.0f);
  1490. if (lum1 < lum0)
  1491. {
  1492. float avg = (lum0 + lum1) * 0.5f;
  1493. lum0 = avg;
  1494. lum1 = avg;
  1495. }
  1496. int ilum1 = astc::flt2int_rtn(lum1);
  1497. int ilum0 = astc::flt2int_rtn(lum0);
  1498. // Difference of more than a factor-of-2 results in immediate failure
  1499. if (ilum1 - ilum0 > 2048)
  1500. {
  1501. return false;
  1502. }
  1503. int lowval, highval, diffval;
  1504. int v0, v1;
  1505. int v0e, v1e;
  1506. int v0d, v1d;
  1507. // Try to encode the high-precision submode
  1508. lowval = (ilum0 + 16) >> 5;
  1509. highval = (ilum1 + 16) >> 5;
  1510. lowval = astc::clamp(lowval, 0, 2047);
  1511. highval = astc::clamp(highval, 0, 2047);
  1512. v0 = lowval & 0x7F;
  1513. v0e = quant_color(quant_level, v0);
  1514. v0d = v0e;
  1515. if (v0d < 0x80)
  1516. {
  1517. lowval = (lowval & ~0x7F) | v0d;
  1518. diffval = highval - lowval;
  1519. if (diffval >= 0 && diffval <= 15)
  1520. {
  1521. v1 = ((lowval >> 3) & 0xF0) | diffval;
  1522. v1e = quant_color(quant_level, v1);
  1523. v1d = v1e;
  1524. if ((v1d & 0xF0) == (v1 & 0xF0))
  1525. {
  1526. output[0] = static_cast<uint8_t>(v0e);
  1527. output[1] = static_cast<uint8_t>(v1e);
  1528. return true;
  1529. }
  1530. }
  1531. }
  1532. // Try to encode the low-precision submode
  1533. lowval = (ilum0 + 32) >> 6;
  1534. highval = (ilum1 + 32) >> 6;
  1535. lowval = astc::clamp(lowval, 0, 1023);
  1536. highval = astc::clamp(highval, 0, 1023);
  1537. v0 = (lowval & 0x7F) | 0x80;
  1538. v0e = quant_color(quant_level, v0);
  1539. v0d = v0e;
  1540. if ((v0d & 0x80) == 0)
  1541. {
  1542. return false;
  1543. }
  1544. lowval = (lowval & ~0x7F) | (v0d & 0x7F);
  1545. diffval = highval - lowval;
  1546. if (diffval < 0 || diffval > 31)
  1547. {
  1548. return false;
  1549. }
  1550. v1 = ((lowval >> 2) & 0xE0) | diffval;
  1551. v1e = quant_color(quant_level, v1);
  1552. v1d = v1e;
  1553. if ((v1d & 0xE0) != (v1 & 0xE0))
  1554. {
  1555. return false;
  1556. }
  1557. output[0] = static_cast<uint8_t>(v0e);
  1558. output[1] = static_cast<uint8_t>(v1e);
  1559. return true;
  1560. }
  1561. /**
  1562. * @brief Quantize a HDR A color using either delta or direct RGBA encoding.
  1563. *
  1564. * @param alpha0 The input unquantized color0 endpoint.
  1565. * @param alpha1 The input unquantized color1 endpoint.
  1566. * @param[out] output The output endpoints, returned as packed RGBA+RGBA pairs with mode bits.
  1567. * @param quant_level The quantization level to use.
  1568. */
  1569. static void quantize_hdr_alpha(
  1570. float alpha0,
  1571. float alpha1,
  1572. uint8_t output[2],
  1573. quant_method quant_level
  1574. ) {
  1575. alpha0 = astc::clamp(alpha0, 0.0f, 65280.0f);
  1576. alpha1 = astc::clamp(alpha1, 0.0f, 65280.0f);
  1577. int ialpha0 = astc::flt2int_rtn(alpha0);
  1578. int ialpha1 = astc::flt2int_rtn(alpha1);
  1579. int val0, val1, diffval;
  1580. int v6, v7;
  1581. int v6e, v7e;
  1582. int v6d, v7d;
  1583. // Try to encode one of the delta submodes, in decreasing-precision order
  1584. for (int i = 2; i >= 0; i--)
  1585. {
  1586. val0 = (ialpha0 + (128 >> i)) >> (8 - i);
  1587. val1 = (ialpha1 + (128 >> i)) >> (8 - i);
  1588. v6 = (val0 & 0x7F) | ((i & 1) << 7);
  1589. v6e = quant_color(quant_level, v6);
  1590. v6d = v6e;
  1591. if ((v6 ^ v6d) & 0x80)
  1592. {
  1593. continue;
  1594. }
  1595. val0 = (val0 & ~0x7f) | (v6d & 0x7f);
  1596. diffval = val1 - val0;
  1597. int cutoff = 32 >> i;
  1598. int mask = 2 * cutoff - 1;
  1599. if (diffval < -cutoff || diffval >= cutoff)
  1600. {
  1601. continue;
  1602. }
  1603. v7 = ((i & 2) << 6) | ((val0 >> 7) << (6 - i)) | (diffval & mask);
  1604. v7e = quant_color(quant_level, v7);
  1605. v7d = v7e;
  1606. static const int testbits[3] { 0xE0, 0xF0, 0xF8 };
  1607. if ((v7 ^ v7d) & testbits[i])
  1608. {
  1609. continue;
  1610. }
  1611. output[0] = static_cast<uint8_t>(v6e);
  1612. output[1] = static_cast<uint8_t>(v7e);
  1613. return;
  1614. }
  1615. // Could not encode any of the delta modes; instead encode a flat value
  1616. val0 = (ialpha0 + 256) >> 9;
  1617. val1 = (ialpha1 + 256) >> 9;
  1618. v6 = val0 | 0x80;
  1619. v7 = val1 | 0x80;
  1620. output[0] = quant_color(quant_level, v6);
  1621. output[1] = quant_color(quant_level, v7);
  1622. return;
  1623. }
  1624. /**
  1625. * @brief Quantize a HDR RGBA color using either delta or direct RGBA encoding.
  1626. *
  1627. * @param color0 The input unquantized color0 endpoint.
  1628. * @param color1 The input unquantized color1 endpoint.
  1629. * @param[out] output The output endpoints, returned as packed RGBA+RGBA pairs with mode bits.
  1630. * @param quant_level The quantization level to use.
  1631. */
  1632. static void quantize_hdr_rgb_alpha(
  1633. vfloat4 color0,
  1634. vfloat4 color1,
  1635. uint8_t output[8],
  1636. quant_method quant_level
  1637. ) {
  1638. quantize_hdr_rgb(color0, color1, output, quant_level);
  1639. quantize_hdr_alpha(color0.lane<3>(), color1.lane<3>(), output + 6, quant_level);
  1640. }
  1641. /* See header for documentation. */
  1642. uint8_t pack_color_endpoints(
  1643. vfloat4 color0,
  1644. vfloat4 color1,
  1645. vfloat4 rgbs_color,
  1646. vfloat4 rgbo_color,
  1647. int format,
  1648. uint8_t* output,
  1649. quant_method quant_level
  1650. ) {
  1651. assert(QUANT_6 <= quant_level && quant_level <= QUANT_256);
  1652. // Clamp colors to a valid LDR range
  1653. // Note that HDR has a lower max, handled in the conversion functions
  1654. color0 = clamp(0.0f, 65535.0f, color0);
  1655. color1 = clamp(0.0f, 65535.0f, color1);
  1656. // Pre-scale the LDR value we need to the 0-255 quantizable range
  1657. vfloat4 color0_ldr = color0 * (1.0f / 257.0f);
  1658. vfloat4 color1_ldr = color1 * (1.0f / 257.0f);
  1659. uint8_t retval = 0;
  1660. float best_error = ERROR_CALC_DEFAULT;
  1661. vint4 color0_out, color1_out;
  1662. vint4 color0_out2, color1_out2;
  1663. switch (format)
  1664. {
  1665. case FMT_RGB:
  1666. if (quant_level <= QUANT_160)
  1667. {
  1668. if (try_quantize_rgb_delta_blue_contract(color0_ldr, color1_ldr, color0_out, color1_out, quant_level))
  1669. {
  1670. vint4 color0_unpack;
  1671. vint4 color1_unpack;
  1672. rgba_delta_unpack(color0_out, color1_out, color0_unpack, color1_unpack);
  1673. retval = FMT_RGB_DELTA;
  1674. best_error = get_rgba_encoding_error(color0_ldr, color1_ldr, color0_unpack, color1_unpack);
  1675. }
  1676. if (try_quantize_rgb_delta(color0_ldr, color1_ldr, color0_out2, color1_out2, quant_level))
  1677. {
  1678. vint4 color0_unpack;
  1679. vint4 color1_unpack;
  1680. rgba_delta_unpack(color0_out2, color1_out2, color0_unpack, color1_unpack);
  1681. float error = get_rgba_encoding_error(color0_ldr, color1_ldr, color0_unpack, color1_unpack);
  1682. if (error < best_error)
  1683. {
  1684. retval = FMT_RGB_DELTA;
  1685. best_error = error;
  1686. color0_out = color0_out2;
  1687. color1_out = color1_out2;
  1688. }
  1689. }
  1690. }
  1691. if (quant_level < QUANT_256)
  1692. {
  1693. if (try_quantize_rgb_blue_contract(color0_ldr, color1_ldr, color0_out2, color1_out2, quant_level))
  1694. {
  1695. vint4 color0_unpack;
  1696. vint4 color1_unpack;
  1697. rgba_unpack(color0_out2, color1_out2, color0_unpack, color1_unpack);
  1698. float error = get_rgba_encoding_error(color0_ldr, color1_ldr, color0_unpack, color1_unpack);
  1699. if (error < best_error)
  1700. {
  1701. retval = FMT_RGB;
  1702. best_error = error;
  1703. color0_out = color0_out2;
  1704. color1_out = color1_out2;
  1705. }
  1706. }
  1707. }
  1708. {
  1709. quantize_rgb(color0_ldr, color1_ldr, color0_out2, color1_out2, quant_level);
  1710. vint4 color0_unpack;
  1711. vint4 color1_unpack;
  1712. rgba_unpack(color0_out2, color1_out2, color0_unpack, color1_unpack);
  1713. float error = get_rgba_encoding_error(color0_ldr, color1_ldr, color0_unpack, color1_unpack);
  1714. if (error < best_error)
  1715. {
  1716. retval = FMT_RGB;
  1717. color0_out = color0_out2;
  1718. color1_out = color1_out2;
  1719. }
  1720. }
  1721. // TODO: Can we vectorize this?
  1722. output[0] = static_cast<uint8_t>(color0_out.lane<0>());
  1723. output[1] = static_cast<uint8_t>(color1_out.lane<0>());
  1724. output[2] = static_cast<uint8_t>(color0_out.lane<1>());
  1725. output[3] = static_cast<uint8_t>(color1_out.lane<1>());
  1726. output[4] = static_cast<uint8_t>(color0_out.lane<2>());
  1727. output[5] = static_cast<uint8_t>(color1_out.lane<2>());
  1728. break;
  1729. case FMT_RGBA:
  1730. if (quant_level <= QUANT_160)
  1731. {
  1732. if (try_quantize_rgba_delta_blue_contract(color0_ldr, color1_ldr, color0_out, color1_out, quant_level))
  1733. {
  1734. vint4 color0_unpack;
  1735. vint4 color1_unpack;
  1736. rgba_delta_unpack(color0_out, color1_out, color0_unpack, color1_unpack);
  1737. retval = FMT_RGBA_DELTA;
  1738. best_error = get_rgba_encoding_error(color0_ldr, color1_ldr, color0_unpack, color1_unpack);
  1739. }
  1740. if (try_quantize_rgba_delta(color0_ldr, color1_ldr, color0_out2, color1_out2, quant_level))
  1741. {
  1742. vint4 color0_unpack;
  1743. vint4 color1_unpack;
  1744. rgba_delta_unpack(color0_out2, color1_out2, color0_unpack, color1_unpack);
  1745. float error = get_rgba_encoding_error(color0_ldr, color1_ldr, color0_unpack, color1_unpack);
  1746. if (error < best_error)
  1747. {
  1748. retval = FMT_RGBA_DELTA;
  1749. best_error = error;
  1750. color0_out = color0_out2;
  1751. color1_out = color1_out2;
  1752. }
  1753. }
  1754. }
  1755. if (quant_level < QUANT_256)
  1756. {
  1757. if (try_quantize_rgba_blue_contract(color0_ldr, color1_ldr, color0_out2, color1_out2, quant_level))
  1758. {
  1759. vint4 color0_unpack;
  1760. vint4 color1_unpack;
  1761. rgba_unpack(color0_out2, color1_out2, color0_unpack, color1_unpack);
  1762. float error = get_rgba_encoding_error(color0_ldr, color1_ldr, color0_unpack, color1_unpack);
  1763. if (error < best_error)
  1764. {
  1765. retval = FMT_RGBA;
  1766. best_error = error;
  1767. color0_out = color0_out2;
  1768. color1_out = color1_out2;
  1769. }
  1770. }
  1771. }
  1772. {
  1773. quantize_rgba(color0_ldr, color1_ldr, color0_out2, color1_out2, quant_level);
  1774. vint4 color0_unpack;
  1775. vint4 color1_unpack;
  1776. rgba_unpack(color0_out2, color1_out2, color0_unpack, color1_unpack);
  1777. float error = get_rgba_encoding_error(color0_ldr, color1_ldr, color0_unpack, color1_unpack);
  1778. if (error < best_error)
  1779. {
  1780. retval = FMT_RGBA;
  1781. color0_out = color0_out2;
  1782. color1_out = color1_out2;
  1783. }
  1784. }
  1785. // TODO: Can we vectorize this?
  1786. output[0] = static_cast<uint8_t>(color0_out.lane<0>());
  1787. output[1] = static_cast<uint8_t>(color1_out.lane<0>());
  1788. output[2] = static_cast<uint8_t>(color0_out.lane<1>());
  1789. output[3] = static_cast<uint8_t>(color1_out.lane<1>());
  1790. output[4] = static_cast<uint8_t>(color0_out.lane<2>());
  1791. output[5] = static_cast<uint8_t>(color1_out.lane<2>());
  1792. output[6] = static_cast<uint8_t>(color0_out.lane<3>());
  1793. output[7] = static_cast<uint8_t>(color1_out.lane<3>());
  1794. break;
  1795. case FMT_RGB_SCALE:
  1796. quantize_rgbs(rgbs_color, output, quant_level);
  1797. retval = FMT_RGB_SCALE;
  1798. break;
  1799. case FMT_HDR_RGB_SCALE:
  1800. quantize_hdr_rgbo(rgbo_color, output, quant_level);
  1801. retval = FMT_HDR_RGB_SCALE;
  1802. break;
  1803. case FMT_HDR_RGB:
  1804. quantize_hdr_rgb(color0, color1, output, quant_level);
  1805. retval = FMT_HDR_RGB;
  1806. break;
  1807. case FMT_RGB_SCALE_ALPHA:
  1808. quantize_rgbs_alpha(color0_ldr, color1_ldr, rgbs_color, output, quant_level);
  1809. retval = FMT_RGB_SCALE_ALPHA;
  1810. break;
  1811. case FMT_HDR_LUMINANCE_SMALL_RANGE:
  1812. case FMT_HDR_LUMINANCE_LARGE_RANGE:
  1813. if (try_quantize_hdr_luminance_small_range(color0, color1, output, quant_level))
  1814. {
  1815. retval = FMT_HDR_LUMINANCE_SMALL_RANGE;
  1816. break;
  1817. }
  1818. quantize_hdr_luminance_large_range(color0, color1, output, quant_level);
  1819. retval = FMT_HDR_LUMINANCE_LARGE_RANGE;
  1820. break;
  1821. case FMT_LUMINANCE:
  1822. quantize_luminance(color0_ldr, color1_ldr, output, quant_level);
  1823. retval = FMT_LUMINANCE;
  1824. break;
  1825. case FMT_LUMINANCE_ALPHA:
  1826. if (quant_level <= 18)
  1827. {
  1828. if (try_quantize_luminance_alpha_delta(color0_ldr, color1_ldr, output, quant_level))
  1829. {
  1830. retval = FMT_LUMINANCE_ALPHA_DELTA;
  1831. break;
  1832. }
  1833. }
  1834. quantize_luminance_alpha(color0_ldr, color1_ldr, output, quant_level);
  1835. retval = FMT_LUMINANCE_ALPHA;
  1836. break;
  1837. case FMT_HDR_RGB_LDR_ALPHA:
  1838. quantize_hdr_rgb_ldr_alpha(color0, color1, output, quant_level);
  1839. retval = FMT_HDR_RGB_LDR_ALPHA;
  1840. break;
  1841. case FMT_HDR_RGBA:
  1842. quantize_hdr_rgb_alpha(color0, color1, output, quant_level);
  1843. retval = FMT_HDR_RGBA;
  1844. break;
  1845. }
  1846. return retval;
  1847. }
  1848. #endif