2
0

EtcBlock4x4Encoding_RGB8A1.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. /*
  2. * Copyright 2015 The Etc2Comp Authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. EtcBlock4x4Encoding_RGB8A1.cpp contains:
  18. Block4x4Encoding_RGB8A1
  19. Block4x4Encoding_RGB8A1_Opaque
  20. Block4x4Encoding_RGB8A1_Transparent
  21. These encoders are used when targetting file format RGB8A1.
  22. Block4x4Encoding_RGB8A1_Opaque is used when all pixels in the 4x4 block are opaque
  23. Block4x4Encoding_RGB8A1_Transparent is used when all pixels in the 4x4 block are transparent
  24. Block4x4Encoding_RGB8A1 is used when there is a mixture of alphas in the 4x4 block
  25. */
  26. #include "EtcConfig.h"
  27. #include "EtcBlock4x4Encoding_RGB8A1.h"
  28. #include "EtcBlock4x4.h"
  29. #include "EtcBlock4x4EncodingBits.h"
  30. #include "EtcBlock4x4Encoding_RGB8.h"
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. namespace Etc
  35. {
  36. // ####################################################################################################
  37. // Block4x4Encoding_RGB8A1
  38. // ####################################################################################################
  39. float Block4x4Encoding_RGB8A1::s_aafCwOpaqueUnsetTable[CW_RANGES][SELECTORS] =
  40. {
  41. { 0.0f / 255.0f, 8.0f / 255.0f, 0.0f / 255.0f, -8.0f / 255.0f },
  42. { 0.0f / 255.0f, 17.0f / 255.0f, 0.0f / 255.0f, -17.0f / 255.0f },
  43. { 0.0f / 255.0f, 29.0f / 255.0f, 0.0f / 255.0f, -29.0f / 255.0f },
  44. { 0.0f / 255.0f, 42.0f / 255.0f, 0.0f / 255.0f, -42.0f / 255.0f },
  45. { 0.0f / 255.0f, 60.0f / 255.0f, 0.0f / 255.0f, -60.0f / 255.0f },
  46. { 0.0f / 255.0f, 80.0f / 255.0f, 0.0f / 255.0f, -80.0f / 255.0f },
  47. { 0.0f / 255.0f, 106.0f / 255.0f, 0.0f / 255.0f, -106.0f / 255.0f },
  48. { 0.0f / 255.0f, 183.0f / 255.0f, 0.0f / 255.0f, -183.0f / 255.0f }
  49. };
  50. // ----------------------------------------------------------------------------------------------------
  51. //
  52. Block4x4Encoding_RGB8A1::Block4x4Encoding_RGB8A1(void)
  53. {
  54. m_pencodingbitsRGB8 = nullptr;
  55. m_boolOpaque = false;
  56. m_boolTransparent = false;
  57. m_boolPunchThroughPixels = true;
  58. }
  59. Block4x4Encoding_RGB8A1::~Block4x4Encoding_RGB8A1(void) {}
  60. // ----------------------------------------------------------------------------------------------------
  61. // initialization prior to encoding
  62. // a_pblockParent points to the block associated with this encoding
  63. // a_errormetric is used to choose the best encoding
  64. // a_pafrgbaSource points to a 4x4 block subset of the source image
  65. // a_paucEncodingBits points to the final encoding bits
  66. //
  67. void Block4x4Encoding_RGB8A1::InitFromSource(Block4x4 *a_pblockParent,
  68. ColorFloatRGBA *a_pafrgbaSource,
  69. unsigned char *a_paucEncodingBits,
  70. ErrorMetric a_errormetric)
  71. {
  72. Block4x4Encoding_RGB8::InitFromSource(a_pblockParent,
  73. a_pafrgbaSource,
  74. a_paucEncodingBits,
  75. a_errormetric);
  76. m_boolOpaque = a_pblockParent->GetSourceAlphaMix() == Block4x4::SourceAlphaMix::OPAQUE;
  77. m_boolTransparent = a_pblockParent->GetSourceAlphaMix() == Block4x4::SourceAlphaMix::TRANSPARENT;
  78. m_boolPunchThroughPixels = a_pblockParent->HasPunchThroughPixels();
  79. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  80. {
  81. if (m_pafrgbaSource[uiPixel].fA >= 0.5f)
  82. {
  83. m_afDecodedAlphas[uiPixel] = 1.0f;
  84. }
  85. else
  86. {
  87. m_afDecodedAlphas[uiPixel] = 0.0f;
  88. }
  89. }
  90. }
  91. // ----------------------------------------------------------------------------------------------------
  92. // initialization from the encoding bits of a previous encoding
  93. // a_pblockParent points to the block associated with this encoding
  94. // a_errormetric is used to choose the best encoding
  95. // a_pafrgbaSource points to a 4x4 block subset of the source image
  96. // a_paucEncodingBits points to the final encoding bits of a previous encoding
  97. //
  98. void Block4x4Encoding_RGB8A1::InitFromEncodingBits(Block4x4 *a_pblockParent,
  99. unsigned char *a_paucEncodingBits,
  100. ColorFloatRGBA *a_pafrgbaSource,
  101. ErrorMetric a_errormetric)
  102. {
  103. InitFromEncodingBits_ETC1(a_pblockParent,
  104. a_paucEncodingBits,
  105. a_pafrgbaSource,
  106. a_errormetric);
  107. m_pencodingbitsRGB8 = (Block4x4EncodingBits_RGB8 *)a_paucEncodingBits;
  108. // detect if there is a T, H or Planar mode present
  109. int iRed1 = m_pencodingbitsRGB8->differential.red1;
  110. int iDRed2 = m_pencodingbitsRGB8->differential.dred2;
  111. int iRed2 = iRed1 + iDRed2;
  112. int iGreen1 = m_pencodingbitsRGB8->differential.green1;
  113. int iDGreen2 = m_pencodingbitsRGB8->differential.dgreen2;
  114. int iGreen2 = iGreen1 + iDGreen2;
  115. int iBlue1 = m_pencodingbitsRGB8->differential.blue1;
  116. int iDBlue2 = m_pencodingbitsRGB8->differential.dblue2;
  117. int iBlue2 = iBlue1 + iDBlue2;
  118. if (iRed2 < 0 || iRed2 > 31)
  119. {
  120. InitFromEncodingBits_T();
  121. }
  122. else if (iGreen2 < 0 || iGreen2 > 31)
  123. {
  124. InitFromEncodingBits_H();
  125. }
  126. else if (iBlue2 < 0 || iBlue2 > 31)
  127. {
  128. Block4x4Encoding_RGB8::InitFromEncodingBits_Planar();
  129. }
  130. }
  131. // ----------------------------------------------------------------------------------------------------
  132. // initialization from the encoding bits of a previous encoding assuming the encoding is an ETC1 mode.
  133. // if it isn't an ETC1 mode, this will be overwritten later
  134. //
  135. void Block4x4Encoding_RGB8A1::InitFromEncodingBits_ETC1(Block4x4 *a_pblockParent,
  136. unsigned char *a_paucEncodingBits,
  137. ColorFloatRGBA *a_pafrgbaSource,
  138. ErrorMetric a_errormetric)
  139. {
  140. Block4x4Encoding::Init(a_pblockParent, a_pafrgbaSource,
  141. a_errormetric);
  142. m_pencodingbitsRGB8 = (Block4x4EncodingBits_RGB8 *)a_paucEncodingBits;
  143. m_mode = MODE_ETC1;
  144. m_boolDiff = true;
  145. m_boolFlip = m_pencodingbitsRGB8->differential.flip;
  146. m_boolOpaque = m_pencodingbitsRGB8->differential.diff;
  147. int iR2 = m_pencodingbitsRGB8->differential.red1 + m_pencodingbitsRGB8->differential.dred2;
  148. if (iR2 < 0)
  149. {
  150. iR2 = 0;
  151. }
  152. else if (iR2 > 31)
  153. {
  154. iR2 = 31;
  155. }
  156. int iG2 = m_pencodingbitsRGB8->differential.green1 + m_pencodingbitsRGB8->differential.dgreen2;
  157. if (iG2 < 0)
  158. {
  159. iG2 = 0;
  160. }
  161. else if (iG2 > 31)
  162. {
  163. iG2 = 31;
  164. }
  165. int iB2 = m_pencodingbitsRGB8->differential.blue1 + m_pencodingbitsRGB8->differential.dblue2;
  166. if (iB2 < 0)
  167. {
  168. iB2 = 0;
  169. }
  170. else if (iB2 > 31)
  171. {
  172. iB2 = 31;
  173. }
  174. m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB5(m_pencodingbitsRGB8->differential.red1, m_pencodingbitsRGB8->differential.green1, m_pencodingbitsRGB8->differential.blue1);
  175. m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB5((unsigned char)iR2, (unsigned char)iG2, (unsigned char)iB2);
  176. m_uiCW1 = m_pencodingbitsRGB8->differential.cw1;
  177. m_uiCW2 = m_pencodingbitsRGB8->differential.cw2;
  178. Block4x4Encoding_ETC1::InitFromEncodingBits_Selectors();
  179. Decode_ETC1();
  180. CalcBlockError();
  181. }
  182. // ----------------------------------------------------------------------------------------------------
  183. // initialization from the encoding bits of a previous encoding if T mode is detected
  184. //
  185. void Block4x4Encoding_RGB8A1::InitFromEncodingBits_T(void)
  186. {
  187. m_mode = MODE_T;
  188. unsigned char ucRed1 = (unsigned char)((m_pencodingbitsRGB8->t.red1a << 2) +
  189. m_pencodingbitsRGB8->t.red1b);
  190. unsigned char ucGreen1 = m_pencodingbitsRGB8->t.green1;
  191. unsigned char ucBlue1 = m_pencodingbitsRGB8->t.blue1;
  192. unsigned char ucRed2 = m_pencodingbitsRGB8->t.red2;
  193. unsigned char ucGreen2 = m_pencodingbitsRGB8->t.green2;
  194. unsigned char ucBlue2 = m_pencodingbitsRGB8->t.blue2;
  195. m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB4(ucRed1, ucGreen1, ucBlue1);
  196. m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB4(ucRed2, ucGreen2, ucBlue2);
  197. m_uiCW1 = (m_pencodingbitsRGB8->t.da << 1) + m_pencodingbitsRGB8->t.db;
  198. Block4x4Encoding_ETC1::InitFromEncodingBits_Selectors();
  199. DecodePixels_T();
  200. CalcBlockError();
  201. }
  202. // ----------------------------------------------------------------------------------------------------
  203. // initialization from the encoding bits of a previous encoding if H mode is detected
  204. //
  205. void Block4x4Encoding_RGB8A1::InitFromEncodingBits_H(void)
  206. {
  207. m_mode = MODE_H;
  208. unsigned char ucRed1 = m_pencodingbitsRGB8->h.red1;
  209. unsigned char ucGreen1 = (unsigned char)((m_pencodingbitsRGB8->h.green1a << 1) +
  210. m_pencodingbitsRGB8->h.green1b);
  211. unsigned char ucBlue1 = (unsigned char)((m_pencodingbitsRGB8->h.blue1a << 3) +
  212. (m_pencodingbitsRGB8->h.blue1b << 1) +
  213. m_pencodingbitsRGB8->h.blue1c);
  214. unsigned char ucRed2 = m_pencodingbitsRGB8->h.red2;
  215. unsigned char ucGreen2 = (unsigned char)((m_pencodingbitsRGB8->h.green2a << 1) +
  216. m_pencodingbitsRGB8->h.green2b);
  217. unsigned char ucBlue2 = m_pencodingbitsRGB8->h.blue2;
  218. m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB4(ucRed1, ucGreen1, ucBlue1);
  219. m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB4(ucRed2, ucGreen2, ucBlue2);
  220. // used to determine the LSB of the CW
  221. unsigned int uiRGB1 = (unsigned int)(((int)ucRed1 << 16) + ((int)ucGreen1 << 8) + (int)ucBlue1);
  222. unsigned int uiRGB2 = (unsigned int)(((int)ucRed2 << 16) + ((int)ucGreen2 << 8) + (int)ucBlue2);
  223. m_uiCW1 = (m_pencodingbitsRGB8->h.da << 2) + (m_pencodingbitsRGB8->h.db << 1);
  224. if (uiRGB1 >= uiRGB2)
  225. {
  226. m_uiCW1++;
  227. }
  228. Block4x4Encoding_ETC1::InitFromEncodingBits_Selectors();
  229. DecodePixels_H();
  230. CalcBlockError();
  231. }
  232. // ----------------------------------------------------------------------------------------------------
  233. // for ETC1 modes, set the decoded colors and decoded alpha based on the encoding state
  234. //
  235. void Block4x4Encoding_RGB8A1::Decode_ETC1(void)
  236. {
  237. const unsigned int *pauiPixelOrder = m_boolFlip ? s_auiPixelOrderFlip1 : s_auiPixelOrderFlip0;
  238. for (unsigned int uiPixelOrder = 0; uiPixelOrder < PIXELS; uiPixelOrder++)
  239. {
  240. ColorFloatRGBA *pfrgbaCenter = uiPixelOrder < 8 ? &m_frgbaColor1 : &m_frgbaColor2;
  241. unsigned int uiCW = uiPixelOrder < 8 ? m_uiCW1 : m_uiCW2;
  242. unsigned int uiPixel = pauiPixelOrder[uiPixelOrder];
  243. float fDelta;
  244. if (m_boolOpaque)
  245. fDelta = Block4x4Encoding_ETC1::s_aafCwTable[uiCW][m_auiSelectors[uiPixel]];
  246. else
  247. fDelta = s_aafCwOpaqueUnsetTable[uiCW][m_auiSelectors[uiPixel]];
  248. if (m_boolOpaque == false && m_auiSelectors[uiPixel] == TRANSPARENT_SELECTOR)
  249. {
  250. m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA();
  251. m_afDecodedAlphas[uiPixel] = 0.0f;
  252. }
  253. else
  254. {
  255. m_afrgbaDecodedColors[uiPixel] = (*pfrgbaCenter + fDelta).ClampRGB();
  256. m_afDecodedAlphas[uiPixel] = 1.0f;
  257. }
  258. }
  259. }
  260. // ----------------------------------------------------------------------------------------------------
  261. // for T mode, set the decoded colors and decoded alpha based on the encoding state
  262. //
  263. void Block4x4Encoding_RGB8A1::DecodePixels_T(void)
  264. {
  265. float fDistance = s_afTHDistanceTable[m_uiCW1];
  266. ColorFloatRGBA frgbaDistance(fDistance, fDistance, fDistance, 0.0f);
  267. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  268. {
  269. switch (m_auiSelectors[uiPixel])
  270. {
  271. case 0:
  272. m_afrgbaDecodedColors[uiPixel] = m_frgbaColor1;
  273. m_afDecodedAlphas[uiPixel] = 1.0f;
  274. break;
  275. case 1:
  276. m_afrgbaDecodedColors[uiPixel] = (m_frgbaColor2 + frgbaDistance).ClampRGB();
  277. m_afDecodedAlphas[uiPixel] = 1.0f;
  278. break;
  279. case 2:
  280. if (m_boolOpaque == false)
  281. {
  282. m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA();
  283. m_afDecodedAlphas[uiPixel] = 0.0f;
  284. }
  285. else
  286. {
  287. m_afrgbaDecodedColors[uiPixel] = m_frgbaColor2;
  288. m_afDecodedAlphas[uiPixel] = 1.0f;
  289. }
  290. break;
  291. case 3:
  292. m_afrgbaDecodedColors[uiPixel] = (m_frgbaColor2 - frgbaDistance).ClampRGB();
  293. m_afDecodedAlphas[uiPixel] = 1.0f;
  294. break;
  295. }
  296. }
  297. }
  298. // ----------------------------------------------------------------------------------------------------
  299. // for H mode, set the decoded colors and decoded alpha based on the encoding state
  300. //
  301. void Block4x4Encoding_RGB8A1::DecodePixels_H(void)
  302. {
  303. float fDistance = s_afTHDistanceTable[m_uiCW1];
  304. ColorFloatRGBA frgbaDistance(fDistance, fDistance, fDistance, 0.0f);
  305. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  306. {
  307. switch (m_auiSelectors[uiPixel])
  308. {
  309. case 0:
  310. m_afrgbaDecodedColors[uiPixel] = (m_frgbaColor1 + frgbaDistance).ClampRGB();
  311. m_afDecodedAlphas[uiPixel] = 1.0f;
  312. break;
  313. case 1:
  314. m_afrgbaDecodedColors[uiPixel] = (m_frgbaColor1 - frgbaDistance).ClampRGB();
  315. m_afDecodedAlphas[uiPixel] = 1.0f;
  316. break;
  317. case 2:
  318. if (m_boolOpaque == false)
  319. {
  320. m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA();
  321. m_afDecodedAlphas[uiPixel] = 0.0f;
  322. }
  323. else
  324. {
  325. m_afrgbaDecodedColors[uiPixel] = (m_frgbaColor2 + frgbaDistance).ClampRGB();
  326. m_afDecodedAlphas[uiPixel] = 1.0f;
  327. }
  328. break;
  329. case 3:
  330. m_afrgbaDecodedColors[uiPixel] = (m_frgbaColor2 - frgbaDistance).ClampRGB();
  331. m_afDecodedAlphas[uiPixel] = 1.0f;
  332. break;
  333. }
  334. }
  335. }
  336. // ----------------------------------------------------------------------------------------------------
  337. // perform a single encoding iteration
  338. // replace the encoding if a better encoding was found
  339. // subsequent iterations generally take longer for each iteration
  340. // set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
  341. //
  342. // RGB8A1 can't use individual mode
  343. // RGB8A1 with transparent pixels can't use planar mode
  344. //
  345. void Block4x4Encoding_RGB8A1::PerformIteration(float a_fEffort)
  346. {
  347. assert(!m_boolOpaque);
  348. assert(!m_boolTransparent);
  349. assert(!m_boolDone);
  350. switch (m_uiEncodingIterations)
  351. {
  352. case 0:
  353. PerformFirstIteration();
  354. break;
  355. case 1:
  356. TryDifferential(m_boolMostLikelyFlip, 1, 0, 0);
  357. break;
  358. case 2:
  359. TryDifferential(!m_boolMostLikelyFlip, 1, 0, 0);
  360. if (a_fEffort <= 39.5f)
  361. {
  362. m_boolDone = true;
  363. }
  364. break;
  365. case 3:
  366. Block4x4Encoding_RGB8::CalculateBaseColorsForTAndH();
  367. TryT(1);
  368. TryH(1);
  369. if (a_fEffort <= 49.5f)
  370. {
  371. m_boolDone = true;
  372. }
  373. break;
  374. case 4:
  375. TryDegenerates1();
  376. if (a_fEffort <= 59.5f)
  377. {
  378. m_boolDone = true;
  379. }
  380. break;
  381. case 5:
  382. TryDegenerates2();
  383. if (a_fEffort <= 69.5f)
  384. {
  385. m_boolDone = true;
  386. }
  387. break;
  388. case 6:
  389. TryDegenerates3();
  390. if (a_fEffort <= 79.5f)
  391. {
  392. m_boolDone = true;
  393. }
  394. break;
  395. case 7:
  396. TryDegenerates4();
  397. m_boolDone = true;
  398. break;
  399. default:
  400. assert(0);
  401. break;
  402. }
  403. m_uiEncodingIterations++;
  404. SetDoneIfPerfect();
  405. }
  406. // ----------------------------------------------------------------------------------------------------
  407. // find best initial encoding to ensure block has a valid encoding
  408. //
  409. void Block4x4Encoding_RGB8A1::PerformFirstIteration(void)
  410. {
  411. Block4x4Encoding_ETC1::CalculateMostLikelyFlip();
  412. m_fError = FLT_MAX;
  413. TryDifferential(m_boolMostLikelyFlip, 0, 0, 0);
  414. SetDoneIfPerfect();
  415. if (m_boolDone)
  416. {
  417. return;
  418. }
  419. TryDifferential(!m_boolMostLikelyFlip, 0, 0, 0);
  420. SetDoneIfPerfect();
  421. }
  422. // ----------------------------------------------------------------------------------------------------
  423. // mostly copied from ETC1
  424. // differences:
  425. // Block4x4Encoding_RGB8A1 encodingTry = *this;
  426. //
  427. void Block4x4Encoding_RGB8A1::TryDifferential(bool a_boolFlip, unsigned int a_uiRadius,
  428. int a_iGrayOffset1, int a_iGrayOffset2)
  429. {
  430. ColorFloatRGBA frgbaColor1;
  431. ColorFloatRGBA frgbaColor2;
  432. const unsigned int *pauiPixelMapping1;
  433. const unsigned int *pauiPixelMapping2;
  434. if (a_boolFlip)
  435. {
  436. frgbaColor1 = m_frgbaSourceAverageTop;
  437. frgbaColor2 = m_frgbaSourceAverageBottom;
  438. pauiPixelMapping1 = s_auiTopPixelMapping;
  439. pauiPixelMapping2 = s_auiBottomPixelMapping;
  440. }
  441. else
  442. {
  443. frgbaColor1 = m_frgbaSourceAverageLeft;
  444. frgbaColor2 = m_frgbaSourceAverageRight;
  445. pauiPixelMapping1 = s_auiLeftPixelMapping;
  446. pauiPixelMapping2 = s_auiRightPixelMapping;
  447. }
  448. DifferentialTrys trys(frgbaColor1, frgbaColor2, pauiPixelMapping1, pauiPixelMapping2,
  449. a_uiRadius, a_iGrayOffset1, a_iGrayOffset2);
  450. Block4x4Encoding_RGB8A1 encodingTry = *this;
  451. encodingTry.m_boolFlip = a_boolFlip;
  452. encodingTry.TryDifferentialHalf(&trys.m_half1);
  453. encodingTry.TryDifferentialHalf(&trys.m_half2);
  454. // find best halves that are within differential range
  455. DifferentialTrys::Try *ptryBest1 = nullptr;
  456. DifferentialTrys::Try *ptryBest2 = nullptr;
  457. encodingTry.m_fError = FLT_MAX;
  458. // see if the best of each half are in differential range
  459. int iDRed = trys.m_half2.m_ptryBest->m_iRed - trys.m_half1.m_ptryBest->m_iRed;
  460. int iDGreen = trys.m_half2.m_ptryBest->m_iGreen - trys.m_half1.m_ptryBest->m_iGreen;
  461. int iDBlue = trys.m_half2.m_ptryBest->m_iBlue - trys.m_half1.m_ptryBest->m_iBlue;
  462. if (iDRed >= -4 && iDRed <= 3 && iDGreen >= -4 && iDGreen <= 3 && iDBlue >= -4 && iDBlue <= 3)
  463. {
  464. ptryBest1 = trys.m_half1.m_ptryBest;
  465. ptryBest2 = trys.m_half2.m_ptryBest;
  466. encodingTry.m_fError = trys.m_half1.m_ptryBest->m_fError + trys.m_half2.m_ptryBest->m_fError;
  467. }
  468. else
  469. {
  470. // else, find the next best halves that are in differential range
  471. for (DifferentialTrys::Try *ptry1 = &trys.m_half1.m_atry[0];
  472. ptry1 < &trys.m_half1.m_atry[trys.m_half1.m_uiTrys];
  473. ptry1++)
  474. {
  475. for (DifferentialTrys::Try *ptry2 = &trys.m_half2.m_atry[0];
  476. ptry2 < &trys.m_half2.m_atry[trys.m_half2.m_uiTrys];
  477. ptry2++)
  478. {
  479. iDRed = ptry2->m_iRed - ptry1->m_iRed;
  480. bool boolValidRedDelta = iDRed <= 3 && iDRed >= -4;
  481. iDGreen = ptry2->m_iGreen - ptry1->m_iGreen;
  482. bool boolValidGreenDelta = iDGreen <= 3 && iDGreen >= -4;
  483. iDBlue = ptry2->m_iBlue - ptry1->m_iBlue;
  484. bool boolValidBlueDelta = iDBlue <= 3 && iDBlue >= -4;
  485. if (boolValidRedDelta && boolValidGreenDelta && boolValidBlueDelta)
  486. {
  487. float fError = ptry1->m_fError + ptry2->m_fError;
  488. if (fError < encodingTry.m_fError)
  489. {
  490. encodingTry.m_fError = fError;
  491. ptryBest1 = ptry1;
  492. ptryBest2 = ptry2;
  493. }
  494. }
  495. }
  496. }
  497. assert(encodingTry.m_fError < FLT_MAX);
  498. assert(ptryBest1 != nullptr);
  499. assert(ptryBest2 != nullptr);
  500. }
  501. if (encodingTry.m_fError < m_fError)
  502. {
  503. m_mode = MODE_ETC1;
  504. m_boolDiff = true;
  505. m_boolFlip = encodingTry.m_boolFlip;
  506. m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB5((unsigned char)ptryBest1->m_iRed, (unsigned char)ptryBest1->m_iGreen, (unsigned char)ptryBest1->m_iBlue);
  507. m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB5((unsigned char)ptryBest2->m_iRed, (unsigned char)ptryBest2->m_iGreen, (unsigned char)ptryBest2->m_iBlue);
  508. m_uiCW1 = ptryBest1->m_uiCW;
  509. m_uiCW2 = ptryBest2->m_uiCW;
  510. m_fError = 0.0f;
  511. for (unsigned int uiPixelOrder = 0; uiPixelOrder < PIXELS / 2; uiPixelOrder++)
  512. {
  513. unsigned int uiPixel1 = pauiPixelMapping1[uiPixelOrder];
  514. unsigned int uiPixel2 = pauiPixelMapping2[uiPixelOrder];
  515. unsigned int uiSelector1 = ptryBest1->m_auiSelectors[uiPixelOrder];
  516. unsigned int uiSelector2 = ptryBest2->m_auiSelectors[uiPixelOrder];
  517. m_auiSelectors[uiPixel1] = uiSelector1;
  518. m_auiSelectors[uiPixel2] = ptryBest2->m_auiSelectors[uiPixelOrder];
  519. if (uiSelector1 == TRANSPARENT_SELECTOR)
  520. {
  521. m_afrgbaDecodedColors[uiPixel1] = ColorFloatRGBA();
  522. m_afDecodedAlphas[uiPixel1] = 0.0f;
  523. }
  524. else
  525. {
  526. float fDeltaRGB1 = s_aafCwOpaqueUnsetTable[m_uiCW1][uiSelector1];
  527. m_afrgbaDecodedColors[uiPixel1] = (m_frgbaColor1 + fDeltaRGB1).ClampRGB();
  528. m_afDecodedAlphas[uiPixel1] = 1.0f;
  529. }
  530. if (uiSelector2 == TRANSPARENT_SELECTOR)
  531. {
  532. m_afrgbaDecodedColors[uiPixel2] = ColorFloatRGBA();
  533. m_afDecodedAlphas[uiPixel2] = 0.0f;
  534. }
  535. else
  536. {
  537. float fDeltaRGB2 = s_aafCwOpaqueUnsetTable[m_uiCW2][uiSelector2];
  538. m_afrgbaDecodedColors[uiPixel2] = (m_frgbaColor2 + fDeltaRGB2).ClampRGB();
  539. m_afDecodedAlphas[uiPixel2] = 1.0f;
  540. }
  541. float fDeltaA1 = m_afDecodedAlphas[uiPixel1] - m_pafrgbaSource[uiPixel1].fA;
  542. m_fError += fDeltaA1 * fDeltaA1;
  543. float fDeltaA2 = m_afDecodedAlphas[uiPixel2] - m_pafrgbaSource[uiPixel2].fA;
  544. m_fError += fDeltaA2 * fDeltaA2;
  545. }
  546. m_fError1 = ptryBest1->m_fError;
  547. m_fError2 = ptryBest2->m_fError;
  548. m_boolSeverelyBentDifferentialColors = trys.m_boolSeverelyBentColors;
  549. m_fError = m_fError1 + m_fError2;
  550. // sanity check
  551. {
  552. int iRed1 = m_frgbaColor1.IntRed(31.0f);
  553. int iGreen1 = m_frgbaColor1.IntGreen(31.0f);
  554. int iBlue1 = m_frgbaColor1.IntBlue(31.0f);
  555. int iRed2 = m_frgbaColor2.IntRed(31.0f);
  556. int iGreen2 = m_frgbaColor2.IntGreen(31.0f);
  557. int iBlue2 = m_frgbaColor2.IntBlue(31.0f);
  558. iDRed = iRed2 - iRed1;
  559. iDGreen = iGreen2 - iGreen1;
  560. iDBlue = iBlue2 - iBlue1;
  561. assert(iDRed >= -4 && iDRed < 4);
  562. assert(iDGreen >= -4 && iDGreen < 4);
  563. assert(iDBlue >= -4 && iDBlue < 4);
  564. }
  565. }
  566. }
  567. // ----------------------------------------------------------------------------------------------------
  568. // mostly copied from ETC1
  569. // differences:
  570. // uses s_aafCwOpaqueUnsetTable
  571. // color for selector set to 0,0,0,0
  572. //
  573. void Block4x4Encoding_RGB8A1::TryDifferentialHalf(DifferentialTrys::Half *a_phalf)
  574. {
  575. a_phalf->m_ptryBest = nullptr;
  576. float fBestTryError = FLT_MAX;
  577. a_phalf->m_uiTrys = 0;
  578. for (int iRed = a_phalf->m_iRed - (int)a_phalf->m_uiRadius;
  579. iRed <= a_phalf->m_iRed + (int)a_phalf->m_uiRadius;
  580. iRed++)
  581. {
  582. assert(iRed >= 0 && iRed <= 31);
  583. for (int iGreen = a_phalf->m_iGreen - (int)a_phalf->m_uiRadius;
  584. iGreen <= a_phalf->m_iGreen + (int)a_phalf->m_uiRadius;
  585. iGreen++)
  586. {
  587. assert(iGreen >= 0 && iGreen <= 31);
  588. for (int iBlue = a_phalf->m_iBlue - (int)a_phalf->m_uiRadius;
  589. iBlue <= a_phalf->m_iBlue + (int)a_phalf->m_uiRadius;
  590. iBlue++)
  591. {
  592. assert(iBlue >= 0 && iBlue <= 31);
  593. DifferentialTrys::Try *ptry = &a_phalf->m_atry[a_phalf->m_uiTrys];
  594. assert(ptry < &a_phalf->m_atry[DifferentialTrys::Half::MAX_TRYS]);
  595. ptry->m_iRed = iRed;
  596. ptry->m_iGreen = iGreen;
  597. ptry->m_iBlue = iBlue;
  598. ptry->m_fError = FLT_MAX;
  599. ColorFloatRGBA frgbaColor = ColorFloatRGBA::ConvertFromRGB5((unsigned char)iRed, (unsigned char)iGreen, (unsigned char)iBlue);
  600. // try each CW
  601. for (unsigned int uiCW = 0; uiCW < CW_RANGES; uiCW++)
  602. {
  603. unsigned int auiPixelSelectors[PIXELS / 2];
  604. ColorFloatRGBA afrgbaDecodedColors[PIXELS / 2];
  605. float afPixelErrors[PIXELS / 2] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
  606. FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX };
  607. // pre-compute decoded pixels for each selector
  608. ColorFloatRGBA afrgbaSelectors[SELECTORS];
  609. assert(SELECTORS == 4);
  610. afrgbaSelectors[0] = (frgbaColor + s_aafCwOpaqueUnsetTable[uiCW][0]).ClampRGB();
  611. afrgbaSelectors[1] = (frgbaColor + s_aafCwOpaqueUnsetTable[uiCW][1]).ClampRGB();
  612. afrgbaSelectors[2] = ColorFloatRGBA();
  613. afrgbaSelectors[3] = (frgbaColor + s_aafCwOpaqueUnsetTable[uiCW][3]).ClampRGB();
  614. for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
  615. {
  616. ColorFloatRGBA *pfrgbaSourcePixel = &m_pafrgbaSource[a_phalf->m_pauiPixelMapping[uiPixel]];
  617. ColorFloatRGBA frgbaDecodedPixel;
  618. for (unsigned int uiSelector = 0; uiSelector < SELECTORS; uiSelector++)
  619. {
  620. if (pfrgbaSourcePixel->fA < 0.5f)
  621. {
  622. uiSelector = TRANSPARENT_SELECTOR;
  623. }
  624. else if (uiSelector == TRANSPARENT_SELECTOR)
  625. {
  626. continue;
  627. }
  628. frgbaDecodedPixel = afrgbaSelectors[uiSelector];
  629. float fPixelError;
  630. fPixelError = CalcPixelError(frgbaDecodedPixel, m_afDecodedAlphas[a_phalf->m_pauiPixelMapping[uiPixel]],
  631. *pfrgbaSourcePixel);
  632. if (fPixelError < afPixelErrors[uiPixel])
  633. {
  634. auiPixelSelectors[uiPixel] = uiSelector;
  635. afrgbaDecodedColors[uiPixel] = frgbaDecodedPixel;
  636. afPixelErrors[uiPixel] = fPixelError;
  637. }
  638. if (uiSelector == TRANSPARENT_SELECTOR)
  639. {
  640. break;
  641. }
  642. }
  643. }
  644. // add up all pixel errors
  645. float fCWError = 0.0f;
  646. for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
  647. {
  648. fCWError += afPixelErrors[uiPixel];
  649. }
  650. // if best CW so far
  651. if (fCWError < ptry->m_fError)
  652. {
  653. ptry->m_uiCW = uiCW;
  654. for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
  655. {
  656. ptry->m_auiSelectors[uiPixel] = auiPixelSelectors[uiPixel];
  657. }
  658. ptry->m_fError = fCWError;
  659. }
  660. }
  661. if (ptry->m_fError < fBestTryError)
  662. {
  663. a_phalf->m_ptryBest = ptry;
  664. fBestTryError = ptry->m_fError;
  665. }
  666. assert(ptry->m_fError < FLT_MAX);
  667. a_phalf->m_uiTrys++;
  668. }
  669. }
  670. }
  671. }
  672. // ----------------------------------------------------------------------------------------------------
  673. // try encoding in T mode
  674. // save this encoding if it improves the error
  675. //
  676. // since pixels that use base color1 don't use the distance table, color1 and color2 can be twiddled independently
  677. // better encoding can be found if TWIDDLE_RADIUS is set to 2, but it will be much slower
  678. //
  679. void Block4x4Encoding_RGB8A1::TryT(unsigned int a_uiRadius)
  680. {
  681. Block4x4Encoding_RGB8A1 encodingTry = *this;
  682. // init "try"
  683. {
  684. encodingTry.m_mode = MODE_T;
  685. encodingTry.m_boolDiff = true;
  686. encodingTry.m_boolFlip = false;
  687. encodingTry.m_fError = FLT_MAX;
  688. }
  689. int iColor1Red = m_frgbaOriginalColor1_TAndH.IntRed(15.0f);
  690. int iColor1Green = m_frgbaOriginalColor1_TAndH.IntGreen(15.0f);
  691. int iColor1Blue = m_frgbaOriginalColor1_TAndH.IntBlue(15.0f);
  692. int iMinRed1 = iColor1Red - (int)a_uiRadius;
  693. if (iMinRed1 < 0)
  694. {
  695. iMinRed1 = 0;
  696. }
  697. int iMaxRed1 = iColor1Red + (int)a_uiRadius;
  698. if (iMaxRed1 > 15)
  699. {
  700. iMinRed1 = 15;
  701. }
  702. int iMinGreen1 = iColor1Green - (int)a_uiRadius;
  703. if (iMinGreen1 < 0)
  704. {
  705. iMinGreen1 = 0;
  706. }
  707. int iMaxGreen1 = iColor1Green + (int)a_uiRadius;
  708. if (iMaxGreen1 > 15)
  709. {
  710. iMinGreen1 = 15;
  711. }
  712. int iMinBlue1 = iColor1Blue - (int)a_uiRadius;
  713. if (iMinBlue1 < 0)
  714. {
  715. iMinBlue1 = 0;
  716. }
  717. int iMaxBlue1 = iColor1Blue + (int)a_uiRadius;
  718. if (iMaxBlue1 > 15)
  719. {
  720. iMinBlue1 = 15;
  721. }
  722. int iColor2Red = m_frgbaOriginalColor2_TAndH.IntRed(15.0f);
  723. int iColor2Green = m_frgbaOriginalColor2_TAndH.IntGreen(15.0f);
  724. int iColor2Blue = m_frgbaOriginalColor2_TAndH.IntBlue(15.0f);
  725. int iMinRed2 = iColor2Red - (int)a_uiRadius;
  726. if (iMinRed2 < 0)
  727. {
  728. iMinRed2 = 0;
  729. }
  730. int iMaxRed2 = iColor2Red + (int)a_uiRadius;
  731. if (iMaxRed2 > 15)
  732. {
  733. iMinRed2 = 15;
  734. }
  735. int iMinGreen2 = iColor2Green - (int)a_uiRadius;
  736. if (iMinGreen2 < 0)
  737. {
  738. iMinGreen2 = 0;
  739. }
  740. int iMaxGreen2 = iColor2Green + (int)a_uiRadius;
  741. if (iMaxGreen2 > 15)
  742. {
  743. iMinGreen2 = 15;
  744. }
  745. int iMinBlue2 = iColor2Blue - (int)a_uiRadius;
  746. if (iMinBlue2 < 0)
  747. {
  748. iMinBlue2 = 0;
  749. }
  750. int iMaxBlue2 = iColor2Blue + (int)a_uiRadius;
  751. if (iMaxBlue2 > 15)
  752. {
  753. iMinBlue2 = 15;
  754. }
  755. for (unsigned int uiDistance = 0; uiDistance < TH_DISTANCES; uiDistance++)
  756. {
  757. encodingTry.m_uiCW1 = uiDistance;
  758. // twiddle m_frgbaOriginalColor2_TAndH
  759. // twiddle color2 first, since it affects 3 selectors, while color1 only affects one selector
  760. //
  761. for (int iRed2 = iMinRed2; iRed2 <= iMaxRed2; iRed2++)
  762. {
  763. for (int iGreen2 = iMinGreen2; iGreen2 <= iMaxGreen2; iGreen2++)
  764. {
  765. for (int iBlue2 = iMinBlue2; iBlue2 <= iMaxBlue2; iBlue2++)
  766. {
  767. for (unsigned int uiBaseColorSwaps = 0; uiBaseColorSwaps < 2; uiBaseColorSwaps++)
  768. {
  769. if (uiBaseColorSwaps == 0)
  770. {
  771. encodingTry.m_frgbaColor1 = m_frgbaOriginalColor1_TAndH;
  772. encodingTry.m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed2, (unsigned char)iGreen2, (unsigned char)iBlue2);
  773. }
  774. else
  775. {
  776. encodingTry.m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed2, (unsigned char)iGreen2, (unsigned char)iBlue2);
  777. encodingTry.m_frgbaColor2 = m_frgbaOriginalColor1_TAndH;
  778. }
  779. encodingTry.TryT_BestSelectorCombination();
  780. if (encodingTry.m_fError < m_fError)
  781. {
  782. m_mode = encodingTry.m_mode;
  783. m_boolDiff = encodingTry.m_boolDiff;
  784. m_boolFlip = encodingTry.m_boolFlip;
  785. m_frgbaColor1 = encodingTry.m_frgbaColor1;
  786. m_frgbaColor2 = encodingTry.m_frgbaColor2;
  787. m_uiCW1 = encodingTry.m_uiCW1;
  788. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  789. {
  790. m_auiSelectors[uiPixel] = encodingTry.m_auiSelectors[uiPixel];
  791. m_afrgbaDecodedColors[uiPixel] = encodingTry.m_afrgbaDecodedColors[uiPixel];
  792. }
  793. m_fError = encodingTry.m_fError;
  794. }
  795. }
  796. }
  797. }
  798. }
  799. // twiddle m_frgbaOriginalColor1_TAndH
  800. for (int iRed1 = iMinRed1; iRed1 <= iMaxRed1; iRed1++)
  801. {
  802. for (int iGreen1 = iMinGreen1; iGreen1 <= iMaxGreen1; iGreen1++)
  803. {
  804. for (int iBlue1 = iMinBlue1; iBlue1 <= iMaxBlue1; iBlue1++)
  805. {
  806. for (unsigned int uiBaseColorSwaps = 0; uiBaseColorSwaps < 2; uiBaseColorSwaps++)
  807. {
  808. if (uiBaseColorSwaps == 0)
  809. {
  810. encodingTry.m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed1, (unsigned char)iGreen1, (unsigned char)iBlue1);
  811. encodingTry.m_frgbaColor2 = m_frgbaOriginalColor2_TAndH;
  812. }
  813. else
  814. {
  815. encodingTry.m_frgbaColor1 = m_frgbaOriginalColor2_TAndH;
  816. encodingTry.m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed1, (unsigned char)iGreen1, (unsigned char)iBlue1);
  817. }
  818. encodingTry.TryT_BestSelectorCombination();
  819. if (encodingTry.m_fError < m_fError)
  820. {
  821. m_mode = encodingTry.m_mode;
  822. m_boolDiff = encodingTry.m_boolDiff;
  823. m_boolFlip = encodingTry.m_boolFlip;
  824. m_frgbaColor1 = encodingTry.m_frgbaColor1;
  825. m_frgbaColor2 = encodingTry.m_frgbaColor2;
  826. m_uiCW1 = encodingTry.m_uiCW1;
  827. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  828. {
  829. m_auiSelectors[uiPixel] = encodingTry.m_auiSelectors[uiPixel];
  830. m_afrgbaDecodedColors[uiPixel] = encodingTry.m_afrgbaDecodedColors[uiPixel];
  831. }
  832. m_fError = encodingTry.m_fError;
  833. }
  834. }
  835. }
  836. }
  837. }
  838. }
  839. }
  840. // ----------------------------------------------------------------------------------------------------
  841. // find best selector combination for TryT
  842. // called on an encodingTry
  843. //
  844. void Block4x4Encoding_RGB8A1::TryT_BestSelectorCombination(void)
  845. {
  846. float fDistance = s_afTHDistanceTable[m_uiCW1];
  847. unsigned int auiBestPixelSelectors[PIXELS];
  848. float afBestPixelErrors[PIXELS] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
  849. FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX };
  850. ColorFloatRGBA afrgbaBestDecodedPixels[PIXELS];
  851. ColorFloatRGBA afrgbaDecodedPixel[SELECTORS];
  852. assert(SELECTORS == 4);
  853. afrgbaDecodedPixel[0] = m_frgbaColor1;
  854. afrgbaDecodedPixel[1] = (m_frgbaColor2 + fDistance).ClampRGB();
  855. afrgbaDecodedPixel[2] = ColorFloatRGBA();
  856. afrgbaDecodedPixel[3] = (m_frgbaColor2 - fDistance).ClampRGB();
  857. // try each selector
  858. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  859. {
  860. unsigned int uiMinSelector = 0;
  861. unsigned int uiMaxSelector = SELECTORS - 1;
  862. if (m_pafrgbaSource[uiPixel].fA < 0.5f)
  863. {
  864. uiMinSelector = 2;
  865. uiMaxSelector = 2;
  866. }
  867. for (unsigned int uiSelector = uiMinSelector; uiSelector <= uiMaxSelector; uiSelector++)
  868. {
  869. float fPixelError = CalcPixelError(afrgbaDecodedPixel[uiSelector], m_afDecodedAlphas[uiPixel],
  870. m_pafrgbaSource[uiPixel]);
  871. if (fPixelError < afBestPixelErrors[uiPixel])
  872. {
  873. afBestPixelErrors[uiPixel] = fPixelError;
  874. auiBestPixelSelectors[uiPixel] = uiSelector;
  875. afrgbaBestDecodedPixels[uiPixel] = afrgbaDecodedPixel[uiSelector];
  876. }
  877. }
  878. }
  879. // add up all of the pixel errors
  880. float fBlockError = 0.0f;
  881. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  882. {
  883. fBlockError += afBestPixelErrors[uiPixel];
  884. }
  885. if (fBlockError < m_fError)
  886. {
  887. m_fError = fBlockError;
  888. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  889. {
  890. m_auiSelectors[uiPixel] = auiBestPixelSelectors[uiPixel];
  891. m_afrgbaDecodedColors[uiPixel] = afrgbaBestDecodedPixels[uiPixel];
  892. }
  893. }
  894. }
  895. // ----------------------------------------------------------------------------------------------------
  896. // try encoding in H mode
  897. // save this encoding if it improves the error
  898. //
  899. // since all pixels use the distance table, color1 and color2 can NOT be twiddled independently
  900. // TWIDDLE_RADIUS of 2 is WAY too slow
  901. //
  902. void Block4x4Encoding_RGB8A1::TryH(unsigned int a_uiRadius)
  903. {
  904. Block4x4Encoding_RGB8A1 encodingTry = *this;
  905. // init "try"
  906. {
  907. encodingTry.m_mode = MODE_H;
  908. encodingTry.m_boolDiff = true;
  909. encodingTry.m_boolFlip = false;
  910. encodingTry.m_fError = FLT_MAX;
  911. }
  912. int iColor1Red = m_frgbaOriginalColor1_TAndH.IntRed(15.0f);
  913. int iColor1Green = m_frgbaOriginalColor1_TAndH.IntGreen(15.0f);
  914. int iColor1Blue = m_frgbaOriginalColor1_TAndH.IntBlue(15.0f);
  915. int iMinRed1 = iColor1Red - (int)a_uiRadius;
  916. if (iMinRed1 < 0)
  917. {
  918. iMinRed1 = 0;
  919. }
  920. int iMaxRed1 = iColor1Red + (int)a_uiRadius;
  921. if (iMaxRed1 > 15)
  922. {
  923. iMinRed1 = 15;
  924. }
  925. int iMinGreen1 = iColor1Green - (int)a_uiRadius;
  926. if (iMinGreen1 < 0)
  927. {
  928. iMinGreen1 = 0;
  929. }
  930. int iMaxGreen1 = iColor1Green + (int)a_uiRadius;
  931. if (iMaxGreen1 > 15)
  932. {
  933. iMinGreen1 = 15;
  934. }
  935. int iMinBlue1 = iColor1Blue - (int)a_uiRadius;
  936. if (iMinBlue1 < 0)
  937. {
  938. iMinBlue1 = 0;
  939. }
  940. int iMaxBlue1 = iColor1Blue + (int)a_uiRadius;
  941. if (iMaxBlue1 > 15)
  942. {
  943. iMinBlue1 = 15;
  944. }
  945. int iColor2Red = m_frgbaOriginalColor2_TAndH.IntRed(15.0f);
  946. int iColor2Green = m_frgbaOriginalColor2_TAndH.IntGreen(15.0f);
  947. int iColor2Blue = m_frgbaOriginalColor2_TAndH.IntBlue(15.0f);
  948. int iMinRed2 = iColor2Red - (int)a_uiRadius;
  949. if (iMinRed2 < 0)
  950. {
  951. iMinRed2 = 0;
  952. }
  953. int iMaxRed2 = iColor2Red + (int)a_uiRadius;
  954. if (iMaxRed2 > 15)
  955. {
  956. iMinRed2 = 15;
  957. }
  958. int iMinGreen2 = iColor2Green - (int)a_uiRadius;
  959. if (iMinGreen2 < 0)
  960. {
  961. iMinGreen2 = 0;
  962. }
  963. int iMaxGreen2 = iColor2Green + (int)a_uiRadius;
  964. if (iMaxGreen2 > 15)
  965. {
  966. iMinGreen2 = 15;
  967. }
  968. int iMinBlue2 = iColor2Blue - (int)a_uiRadius;
  969. if (iMinBlue2 < 0)
  970. {
  971. iMinBlue2 = 0;
  972. }
  973. int iMaxBlue2 = iColor2Blue + (int)a_uiRadius;
  974. if (iMaxBlue2 > 15)
  975. {
  976. iMinBlue2 = 15;
  977. }
  978. for (unsigned int uiDistance = 0; uiDistance < TH_DISTANCES; uiDistance++)
  979. {
  980. encodingTry.m_uiCW1 = uiDistance;
  981. // twiddle m_frgbaOriginalColor1_TAndH
  982. for (int iRed1 = iMinRed1; iRed1 <= iMaxRed1; iRed1++)
  983. {
  984. for (int iGreen1 = iMinGreen1; iGreen1 <= iMaxGreen1; iGreen1++)
  985. {
  986. for (int iBlue1 = iMinBlue1; iBlue1 <= iMaxBlue1; iBlue1++)
  987. {
  988. encodingTry.m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed1, (unsigned char)iGreen1, (unsigned char)iBlue1);
  989. encodingTry.m_frgbaColor2 = m_frgbaOriginalColor2_TAndH;
  990. // if color1 == color2, H encoding issues can pop up, so abort
  991. if (iRed1 == iColor2Red && iGreen1 == iColor2Green && iBlue1 == iColor2Blue)
  992. {
  993. continue;
  994. }
  995. encodingTry.TryH_BestSelectorCombination();
  996. if (encodingTry.m_fError < m_fError)
  997. {
  998. m_mode = encodingTry.m_mode;
  999. m_boolDiff = encodingTry.m_boolDiff;
  1000. m_boolFlip = encodingTry.m_boolFlip;
  1001. m_frgbaColor1 = encodingTry.m_frgbaColor1;
  1002. m_frgbaColor2 = encodingTry.m_frgbaColor2;
  1003. m_uiCW1 = encodingTry.m_uiCW1;
  1004. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  1005. {
  1006. m_auiSelectors[uiPixel] = encodingTry.m_auiSelectors[uiPixel];
  1007. m_afrgbaDecodedColors[uiPixel] = encodingTry.m_afrgbaDecodedColors[uiPixel];
  1008. }
  1009. m_fError = encodingTry.m_fError;
  1010. }
  1011. }
  1012. }
  1013. }
  1014. // twiddle m_frgbaOriginalColor2_TAndH
  1015. for (int iRed2 = iMinRed2; iRed2 <= iMaxRed2; iRed2++)
  1016. {
  1017. for (int iGreen2 = iMinGreen2; iGreen2 <= iMaxGreen2; iGreen2++)
  1018. {
  1019. for (int iBlue2 = iMinBlue2; iBlue2 <= iMaxBlue2; iBlue2++)
  1020. {
  1021. encodingTry.m_frgbaColor1 = m_frgbaOriginalColor1_TAndH;
  1022. encodingTry.m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed2, (unsigned char)iGreen2, (unsigned char)iBlue2);
  1023. // if color1 == color2, H encoding issues can pop up, so abort
  1024. if (iRed2 == iColor1Red && iGreen2 == iColor1Green && iBlue2 == iColor1Blue)
  1025. {
  1026. continue;
  1027. }
  1028. encodingTry.TryH_BestSelectorCombination();
  1029. if (encodingTry.m_fError < m_fError)
  1030. {
  1031. m_mode = encodingTry.m_mode;
  1032. m_boolDiff = encodingTry.m_boolDiff;
  1033. m_boolFlip = encodingTry.m_boolFlip;
  1034. m_frgbaColor1 = encodingTry.m_frgbaColor1;
  1035. m_frgbaColor2 = encodingTry.m_frgbaColor2;
  1036. m_uiCW1 = encodingTry.m_uiCW1;
  1037. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  1038. {
  1039. m_auiSelectors[uiPixel] = encodingTry.m_auiSelectors[uiPixel];
  1040. m_afrgbaDecodedColors[uiPixel] = encodingTry.m_afrgbaDecodedColors[uiPixel];
  1041. }
  1042. m_fError = encodingTry.m_fError;
  1043. }
  1044. }
  1045. }
  1046. }
  1047. }
  1048. }
  1049. // ----------------------------------------------------------------------------------------------------
  1050. // find best selector combination for TryH
  1051. // called on an encodingTry
  1052. //
  1053. void Block4x4Encoding_RGB8A1::TryH_BestSelectorCombination(void)
  1054. {
  1055. // abort if colors and CW will pose an encoding problem
  1056. {
  1057. unsigned int uiRed1 = (unsigned int)m_frgbaColor1.IntRed(255.0f);
  1058. unsigned int uiGreen1 = (unsigned int)m_frgbaColor1.IntGreen(255.0f);
  1059. unsigned int uiBlue1 = (unsigned int)m_frgbaColor1.IntBlue(255.0f);
  1060. unsigned int uiColorValue1 = (uiRed1 << 16) + (uiGreen1 << 8) + uiBlue1;
  1061. unsigned int uiRed2 = (unsigned int)m_frgbaColor2.IntRed(255.0f);
  1062. unsigned int uiGreen2 = (unsigned int)m_frgbaColor2.IntGreen(255.0f);
  1063. unsigned int uiBlue2 = (unsigned int)m_frgbaColor2.IntBlue(255.0f);
  1064. unsigned int uiColorValue2 = (uiRed2 << 16) + (uiGreen2 << 8) + uiBlue2;
  1065. unsigned int uiCWLsb = m_uiCW1 & 1;
  1066. if ((uiColorValue1 >= (uiColorValue2 & uiCWLsb)) == 0 ||
  1067. (uiColorValue1 < (uiColorValue2 & uiCWLsb)) == 1)
  1068. {
  1069. return;
  1070. }
  1071. }
  1072. float fDistance = s_afTHDistanceTable[m_uiCW1];
  1073. unsigned int auiBestPixelSelectors[PIXELS];
  1074. float afBestPixelErrors[PIXELS] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
  1075. FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX };
  1076. ColorFloatRGBA afrgbaBestDecodedPixels[PIXELS];
  1077. ColorFloatRGBA afrgbaDecodedPixel[SELECTORS];
  1078. assert(SELECTORS == 4);
  1079. afrgbaDecodedPixel[0] = (m_frgbaColor1 + fDistance).ClampRGB();
  1080. afrgbaDecodedPixel[1] = (m_frgbaColor1 - fDistance).ClampRGB();
  1081. afrgbaDecodedPixel[2] = ColorFloatRGBA();;
  1082. afrgbaDecodedPixel[3] = (m_frgbaColor2 - fDistance).ClampRGB();
  1083. // try each selector
  1084. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  1085. {
  1086. unsigned int uiMinSelector = 0;
  1087. unsigned int uiMaxSelector = SELECTORS - 1;
  1088. if (m_pafrgbaSource[uiPixel].fA < 0.5f)
  1089. {
  1090. uiMinSelector = 2;
  1091. uiMaxSelector = 2;
  1092. }
  1093. for (unsigned int uiSelector = uiMinSelector; uiSelector <= uiMaxSelector; uiSelector++)
  1094. {
  1095. float fPixelError = CalcPixelError(afrgbaDecodedPixel[uiSelector], m_afDecodedAlphas[uiPixel],
  1096. m_pafrgbaSource[uiPixel]);
  1097. if (fPixelError < afBestPixelErrors[uiPixel])
  1098. {
  1099. afBestPixelErrors[uiPixel] = fPixelError;
  1100. auiBestPixelSelectors[uiPixel] = uiSelector;
  1101. afrgbaBestDecodedPixels[uiPixel] = afrgbaDecodedPixel[uiSelector];
  1102. }
  1103. }
  1104. }
  1105. // add up all of the pixel errors
  1106. float fBlockError = 0.0f;
  1107. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  1108. {
  1109. fBlockError += afBestPixelErrors[uiPixel];
  1110. }
  1111. if (fBlockError < m_fError)
  1112. {
  1113. m_fError = fBlockError;
  1114. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  1115. {
  1116. m_auiSelectors[uiPixel] = auiBestPixelSelectors[uiPixel];
  1117. m_afrgbaDecodedColors[uiPixel] = afrgbaBestDecodedPixels[uiPixel];
  1118. }
  1119. }
  1120. }
  1121. // ----------------------------------------------------------------------------------------------------
  1122. // try version 1 of the degenerate search
  1123. // degenerate encodings use basecolor movement and a subset of the selectors to find useful encodings
  1124. // each subsequent version of the degenerate search uses more basecolor movement and is less likely to
  1125. // be successfull
  1126. //
  1127. void Block4x4Encoding_RGB8A1::TryDegenerates1(void)
  1128. {
  1129. TryDifferential(m_boolMostLikelyFlip, 1, -2, 0);
  1130. TryDifferential(m_boolMostLikelyFlip, 1, 2, 0);
  1131. TryDifferential(m_boolMostLikelyFlip, 1, 0, 2);
  1132. TryDifferential(m_boolMostLikelyFlip, 1, 0, -2);
  1133. }
  1134. // ----------------------------------------------------------------------------------------------------
  1135. // try version 2 of the degenerate search
  1136. // degenerate encodings use basecolor movement and a subset of the selectors to find useful encodings
  1137. // each subsequent version of the degenerate search uses more basecolor movement and is less likely to
  1138. // be successfull
  1139. //
  1140. void Block4x4Encoding_RGB8A1::TryDegenerates2(void)
  1141. {
  1142. TryDifferential(!m_boolMostLikelyFlip, 1, -2, 0);
  1143. TryDifferential(!m_boolMostLikelyFlip, 1, 2, 0);
  1144. TryDifferential(!m_boolMostLikelyFlip, 1, 0, 2);
  1145. TryDifferential(!m_boolMostLikelyFlip, 1, 0, -2);
  1146. }
  1147. // ----------------------------------------------------------------------------------------------------
  1148. // try version 3 of the degenerate search
  1149. // degenerate encodings use basecolor movement and a subset of the selectors to find useful encodings
  1150. // each subsequent version of the degenerate search uses more basecolor movement and is less likely to
  1151. // be successfull
  1152. //
  1153. void Block4x4Encoding_RGB8A1::TryDegenerates3(void)
  1154. {
  1155. TryDifferential(m_boolMostLikelyFlip, 1, -2, -2);
  1156. TryDifferential(m_boolMostLikelyFlip, 1, -2, 2);
  1157. TryDifferential(m_boolMostLikelyFlip, 1, 2, -2);
  1158. TryDifferential(m_boolMostLikelyFlip, 1, 2, 2);
  1159. }
  1160. // ----------------------------------------------------------------------------------------------------
  1161. // try version 4 of the degenerate search
  1162. // degenerate encodings use basecolor movement and a subset of the selectors to find useful encodings
  1163. // each subsequent version of the degenerate search uses more basecolor movement and is less likely to
  1164. // be successfull
  1165. //
  1166. void Block4x4Encoding_RGB8A1::TryDegenerates4(void)
  1167. {
  1168. TryDifferential(m_boolMostLikelyFlip, 1, -4, 0);
  1169. TryDifferential(m_boolMostLikelyFlip, 1, 4, 0);
  1170. TryDifferential(m_boolMostLikelyFlip, 1, 0, 4);
  1171. TryDifferential(m_boolMostLikelyFlip, 1, 0, -4);
  1172. }
  1173. // ----------------------------------------------------------------------------------------------------
  1174. // set the encoding bits based on encoding state
  1175. //
  1176. void Block4x4Encoding_RGB8A1::SetEncodingBits(void)
  1177. {
  1178. switch (m_mode)
  1179. {
  1180. case MODE_ETC1:
  1181. SetEncodingBits_ETC1();
  1182. break;
  1183. case MODE_T:
  1184. SetEncodingBits_T();
  1185. break;
  1186. case MODE_H:
  1187. SetEncodingBits_H();
  1188. break;
  1189. case MODE_PLANAR:
  1190. Block4x4Encoding_RGB8::SetEncodingBits_Planar();
  1191. break;
  1192. default:
  1193. assert(false);
  1194. }
  1195. }
  1196. // ----------------------------------------------------------------------------------------------------
  1197. // set the encoding bits based on encoding state if ETC1 mode
  1198. //
  1199. void Block4x4Encoding_RGB8A1::SetEncodingBits_ETC1(void)
  1200. {
  1201. // there is no individual mode in RGB8A1
  1202. assert(m_boolDiff);
  1203. int iRed1 = m_frgbaColor1.IntRed(31.0f);
  1204. int iGreen1 = m_frgbaColor1.IntGreen(31.0f);
  1205. int iBlue1 = m_frgbaColor1.IntBlue(31.0f);
  1206. int iRed2 = m_frgbaColor2.IntRed(31.0f);
  1207. int iGreen2 = m_frgbaColor2.IntGreen(31.0f);
  1208. int iBlue2 = m_frgbaColor2.IntBlue(31.0f);
  1209. int iDRed2 = iRed2 - iRed1;
  1210. int iDGreen2 = iGreen2 - iGreen1;
  1211. int iDBlue2 = iBlue2 - iBlue1;
  1212. assert(iDRed2 >= -4 && iDRed2 < 4);
  1213. assert(iDGreen2 >= -4 && iDGreen2 < 4);
  1214. assert(iDBlue2 >= -4 && iDBlue2 < 4);
  1215. m_pencodingbitsRGB8->differential.red1 = iRed1;
  1216. m_pencodingbitsRGB8->differential.green1 = iGreen1;
  1217. m_pencodingbitsRGB8->differential.blue1 = iBlue1;
  1218. m_pencodingbitsRGB8->differential.dred2 = iDRed2;
  1219. m_pencodingbitsRGB8->differential.dgreen2 = iDGreen2;
  1220. m_pencodingbitsRGB8->differential.dblue2 = iDBlue2;
  1221. m_pencodingbitsRGB8->individual.cw1 = m_uiCW1;
  1222. m_pencodingbitsRGB8->individual.cw2 = m_uiCW2;
  1223. SetEncodingBits_Selectors();
  1224. // in RGB8A1 encoding bits, opaque replaces differential
  1225. m_pencodingbitsRGB8->differential.diff = !m_boolPunchThroughPixels;
  1226. m_pencodingbitsRGB8->individual.flip = m_boolFlip;
  1227. }
  1228. // ----------------------------------------------------------------------------------------------------
  1229. // set the encoding bits based on encoding state if T mode
  1230. //
  1231. void Block4x4Encoding_RGB8A1::SetEncodingBits_T(void)
  1232. {
  1233. static const bool SANITY_CHECK = true;
  1234. assert(m_mode == MODE_T);
  1235. assert(m_boolDiff == true);
  1236. unsigned int uiRed1 = (unsigned int)m_frgbaColor1.IntRed(15.0f);
  1237. unsigned int uiGreen1 = (unsigned int)m_frgbaColor1.IntGreen(15.0f);
  1238. unsigned int uiBlue1 = (unsigned int)m_frgbaColor1.IntBlue(15.0f);
  1239. unsigned int uiRed2 = (unsigned int)m_frgbaColor2.IntRed(15.0f);
  1240. unsigned int uiGreen2 = (unsigned int)m_frgbaColor2.IntGreen(15.0f);
  1241. unsigned int uiBlue2 = (unsigned int)m_frgbaColor2.IntBlue(15.0f);
  1242. m_pencodingbitsRGB8->t.red1a = uiRed1 >> 2;
  1243. m_pencodingbitsRGB8->t.red1b = uiRed1;
  1244. m_pencodingbitsRGB8->t.green1 = uiGreen1;
  1245. m_pencodingbitsRGB8->t.blue1 = uiBlue1;
  1246. m_pencodingbitsRGB8->t.red2 = uiRed2;
  1247. m_pencodingbitsRGB8->t.green2 = uiGreen2;
  1248. m_pencodingbitsRGB8->t.blue2 = uiBlue2;
  1249. m_pencodingbitsRGB8->t.da = m_uiCW1 >> 1;
  1250. m_pencodingbitsRGB8->t.db = m_uiCW1;
  1251. // in RGB8A1 encoding bits, opaque replaces differential
  1252. m_pencodingbitsRGB8->differential.diff = !m_boolPunchThroughPixels;
  1253. Block4x4Encoding_ETC1::SetEncodingBits_Selectors();
  1254. // create an invalid R differential to trigger T mode
  1255. m_pencodingbitsRGB8->t.detect1 = 0;
  1256. m_pencodingbitsRGB8->t.detect2 = 0;
  1257. int iRed2 = (int)m_pencodingbitsRGB8->differential.red1 + (int)m_pencodingbitsRGB8->differential.dred2;
  1258. if (iRed2 >= 4)
  1259. {
  1260. m_pencodingbitsRGB8->t.detect1 = 7;
  1261. m_pencodingbitsRGB8->t.detect2 = 0;
  1262. }
  1263. else
  1264. {
  1265. m_pencodingbitsRGB8->t.detect1 = 0;
  1266. m_pencodingbitsRGB8->t.detect2 = 1;
  1267. }
  1268. if (SANITY_CHECK)
  1269. {
  1270. iRed2 = (int)m_pencodingbitsRGB8->differential.red1 + (int)m_pencodingbitsRGB8->differential.dred2;
  1271. // make sure red overflows
  1272. assert(iRed2 < 0 || iRed2 > 31);
  1273. }
  1274. }
  1275. // ----------------------------------------------------------------------------------------------------
  1276. // set the encoding bits based on encoding state if H mode
  1277. //
  1278. // colors and selectors may need to swap in order to generate lsb of distance index
  1279. //
  1280. void Block4x4Encoding_RGB8A1::SetEncodingBits_H(void)
  1281. {
  1282. static const bool SANITY_CHECK = true;
  1283. assert(m_mode == MODE_H);
  1284. assert(m_boolDiff == true);
  1285. unsigned int uiRed1 = (unsigned int)m_frgbaColor1.IntRed(15.0f);
  1286. unsigned int uiGreen1 = (unsigned int)m_frgbaColor1.IntGreen(15.0f);
  1287. unsigned int uiBlue1 = (unsigned int)m_frgbaColor1.IntBlue(15.0f);
  1288. unsigned int uiRed2 = (unsigned int)m_frgbaColor2.IntRed(15.0f);
  1289. unsigned int uiGreen2 = (unsigned int)m_frgbaColor2.IntGreen(15.0f);
  1290. unsigned int uiBlue2 = (unsigned int)m_frgbaColor2.IntBlue(15.0f);
  1291. unsigned int uiColor1 = (uiRed1 << 16) + (uiGreen1 << 8) + uiBlue1;
  1292. unsigned int uiColor2 = (uiRed2 << 16) + (uiGreen2 << 8) + uiBlue2;
  1293. bool boolOddDistance = m_uiCW1 & 1;
  1294. bool boolSwapColors = (uiColor1 < uiColor2) ^ !boolOddDistance;
  1295. if (boolSwapColors)
  1296. {
  1297. m_pencodingbitsRGB8->h.red1 = uiRed2;
  1298. m_pencodingbitsRGB8->h.green1a = uiGreen2 >> 1;
  1299. m_pencodingbitsRGB8->h.green1b = uiGreen2;
  1300. m_pencodingbitsRGB8->h.blue1a = uiBlue2 >> 3;
  1301. m_pencodingbitsRGB8->h.blue1b = uiBlue2 >> 1;
  1302. m_pencodingbitsRGB8->h.blue1c = uiBlue2;
  1303. m_pencodingbitsRGB8->h.red2 = uiRed1;
  1304. m_pencodingbitsRGB8->h.green2a = uiGreen1 >> 1;
  1305. m_pencodingbitsRGB8->h.green2b = uiGreen1;
  1306. m_pencodingbitsRGB8->h.blue2 = uiBlue1;
  1307. m_pencodingbitsRGB8->h.da = m_uiCW1 >> 2;
  1308. m_pencodingbitsRGB8->h.db = m_uiCW1 >> 1;
  1309. }
  1310. else
  1311. {
  1312. m_pencodingbitsRGB8->h.red1 = uiRed1;
  1313. m_pencodingbitsRGB8->h.green1a = uiGreen1 >> 1;
  1314. m_pencodingbitsRGB8->h.green1b = uiGreen1;
  1315. m_pencodingbitsRGB8->h.blue1a = uiBlue1 >> 3;
  1316. m_pencodingbitsRGB8->h.blue1b = uiBlue1 >> 1;
  1317. m_pencodingbitsRGB8->h.blue1c = uiBlue1;
  1318. m_pencodingbitsRGB8->h.red2 = uiRed2;
  1319. m_pencodingbitsRGB8->h.green2a = uiGreen2 >> 1;
  1320. m_pencodingbitsRGB8->h.green2b = uiGreen2;
  1321. m_pencodingbitsRGB8->h.blue2 = uiBlue2;
  1322. m_pencodingbitsRGB8->h.da = m_uiCW1 >> 2;
  1323. m_pencodingbitsRGB8->h.db = m_uiCW1 >> 1;
  1324. }
  1325. // in RGB8A1 encoding bits, opaque replaces differential
  1326. m_pencodingbitsRGB8->differential.diff = !m_boolPunchThroughPixels;
  1327. Block4x4Encoding_ETC1::SetEncodingBits_Selectors();
  1328. if (boolSwapColors)
  1329. {
  1330. m_pencodingbitsRGB8->h.selectors ^= 0x0000FFFF;
  1331. }
  1332. // create an invalid R differential to trigger T mode
  1333. m_pencodingbitsRGB8->h.detect1 = 0;
  1334. m_pencodingbitsRGB8->h.detect2 = 0;
  1335. m_pencodingbitsRGB8->h.detect3 = 0;
  1336. int iRed2 = (int)m_pencodingbitsRGB8->differential.red1 + (int)m_pencodingbitsRGB8->differential.dred2;
  1337. int iGreen2 = (int)m_pencodingbitsRGB8->differential.green1 + (int)m_pencodingbitsRGB8->differential.dgreen2;
  1338. if (iRed2 < 0 || iRed2 > 31)
  1339. {
  1340. m_pencodingbitsRGB8->h.detect1 = 1;
  1341. }
  1342. if (iGreen2 >= 4)
  1343. {
  1344. m_pencodingbitsRGB8->h.detect2 = 7;
  1345. m_pencodingbitsRGB8->h.detect3 = 0;
  1346. }
  1347. else
  1348. {
  1349. m_pencodingbitsRGB8->h.detect2 = 0;
  1350. m_pencodingbitsRGB8->h.detect3 = 1;
  1351. }
  1352. if (SANITY_CHECK)
  1353. {
  1354. iRed2 = (int)m_pencodingbitsRGB8->differential.red1 + (int)m_pencodingbitsRGB8->differential.dred2;
  1355. iGreen2 = (int)m_pencodingbitsRGB8->differential.green1 + (int)m_pencodingbitsRGB8->differential.dgreen2;
  1356. // make sure red doesn't overflow and green does
  1357. assert(iRed2 >= 0 && iRed2 <= 31);
  1358. assert(iGreen2 < 0 || iGreen2 > 31);
  1359. }
  1360. }
  1361. // ####################################################################################################
  1362. // Block4x4Encoding_RGB8A1_Opaque
  1363. // ####################################################################################################
  1364. // ----------------------------------------------------------------------------------------------------
  1365. // perform a single encoding iteration
  1366. // replace the encoding if a better encoding was found
  1367. // subsequent iterations generally take longer for each iteration
  1368. // set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
  1369. //
  1370. void Block4x4Encoding_RGB8A1_Opaque::PerformIteration(float a_fEffort)
  1371. {
  1372. assert(!m_boolPunchThroughPixels);
  1373. assert(!m_boolTransparent);
  1374. assert(!m_boolDone);
  1375. switch (m_uiEncodingIterations)
  1376. {
  1377. case 0:
  1378. PerformFirstIteration();
  1379. break;
  1380. case 1:
  1381. Block4x4Encoding_ETC1::TryDifferential(m_boolMostLikelyFlip, 1, 0, 0);
  1382. break;
  1383. case 2:
  1384. Block4x4Encoding_ETC1::TryDifferential(!m_boolMostLikelyFlip, 1, 0, 0);
  1385. break;
  1386. case 3:
  1387. Block4x4Encoding_RGB8::TryPlanar(1);
  1388. break;
  1389. case 4:
  1390. Block4x4Encoding_RGB8::TryTAndH(1);
  1391. if (a_fEffort <= 49.5f)
  1392. {
  1393. m_boolDone = true;
  1394. }
  1395. break;
  1396. case 5:
  1397. Block4x4Encoding_ETC1::TryDegenerates1();
  1398. if (a_fEffort <= 59.5f)
  1399. {
  1400. m_boolDone = true;
  1401. }
  1402. break;
  1403. case 6:
  1404. Block4x4Encoding_ETC1::TryDegenerates2();
  1405. if (a_fEffort <= 69.5f)
  1406. {
  1407. m_boolDone = true;
  1408. }
  1409. break;
  1410. case 7:
  1411. Block4x4Encoding_ETC1::TryDegenerates3();
  1412. if (a_fEffort <= 79.5f)
  1413. {
  1414. m_boolDone = true;
  1415. }
  1416. break;
  1417. case 8:
  1418. Block4x4Encoding_ETC1::TryDegenerates4();
  1419. m_boolDone = true;
  1420. break;
  1421. default:
  1422. assert(0);
  1423. break;
  1424. }
  1425. m_uiEncodingIterations++;
  1426. SetDoneIfPerfect();
  1427. }
  1428. // ----------------------------------------------------------------------------------------------------
  1429. // find best initial encoding to ensure block has a valid encoding
  1430. //
  1431. void Block4x4Encoding_RGB8A1_Opaque::PerformFirstIteration(void)
  1432. {
  1433. // set decoded alphas
  1434. // calculate alpha error
  1435. m_fError = 0.0f;
  1436. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  1437. {
  1438. m_afDecodedAlphas[uiPixel] = 1.0f;
  1439. float fDeltaA = 1.0f - m_pafrgbaSource[uiPixel].fA;
  1440. m_fError += fDeltaA * fDeltaA;
  1441. }
  1442. CalculateMostLikelyFlip();
  1443. m_fError = FLT_MAX;
  1444. Block4x4Encoding_ETC1::TryDifferential(m_boolMostLikelyFlip, 0, 0, 0);
  1445. SetDoneIfPerfect();
  1446. if (m_boolDone)
  1447. {
  1448. return;
  1449. }
  1450. Block4x4Encoding_ETC1::TryDifferential(!m_boolMostLikelyFlip, 0, 0, 0);
  1451. SetDoneIfPerfect();
  1452. if (m_boolDone)
  1453. {
  1454. return;
  1455. }
  1456. Block4x4Encoding_RGB8::TryPlanar(0);
  1457. SetDoneIfPerfect();
  1458. if (m_boolDone)
  1459. {
  1460. return;
  1461. }
  1462. Block4x4Encoding_RGB8::TryTAndH(0);
  1463. SetDoneIfPerfect();
  1464. }
  1465. // ####################################################################################################
  1466. // Block4x4Encoding_RGB8A1_Transparent
  1467. // ####################################################################################################
  1468. // ----------------------------------------------------------------------------------------------------
  1469. // perform a single encoding iteration
  1470. // replace the encoding if a better encoding was found
  1471. // subsequent iterations generally take longer for each iteration
  1472. // set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
  1473. //
  1474. void Block4x4Encoding_RGB8A1_Transparent::PerformIteration(float )
  1475. {
  1476. assert(!m_boolOpaque);
  1477. assert(m_boolTransparent);
  1478. assert(!m_boolDone);
  1479. assert(m_uiEncodingIterations == 0);
  1480. m_mode = MODE_ETC1;
  1481. m_boolDiff = true;
  1482. m_boolFlip = false;
  1483. m_uiCW1 = 0;
  1484. m_uiCW2 = 0;
  1485. m_frgbaColor1 = ColorFloatRGBA();
  1486. m_frgbaColor2 = ColorFloatRGBA();
  1487. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  1488. {
  1489. m_auiSelectors[uiPixel] = TRANSPARENT_SELECTOR;
  1490. m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA();
  1491. m_afDecodedAlphas[uiPixel] = 0.0f;
  1492. }
  1493. CalcBlockError();
  1494. m_boolDone = true;
  1495. m_uiEncodingIterations++;
  1496. }
  1497. // ----------------------------------------------------------------------------------------------------
  1498. //
  1499. }