astcenc_color_quantize.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  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