astcenc_color_quantize.cpp 54 KB

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