EtcBlock4x4Encoding_RGB8A1.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
  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. }