astcenc_color_quantize.cpp 54 KB

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