bc6_encode_kernel.cpp 160 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433
  1. //=====================================================================
  2. // Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files(the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and / or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions :
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. //=====================================================================
  23. #include "bc6_encode_kernel.h"
  24. #ifndef ASPM_OPENCL
  25. //#define USE_NEW_SINGLE_HEADER_INTERFACES
  26. #ifdef USE_NEW_SINGLE_HEADER_INTERFACES
  27. //#define USE_CMP // Testung Betsy GPU Code on CPU
  28. //#define USE_BETSY // Testung Betsy GPU Code on CPU
  29. //#define USE_HPC // EnableCPU Definitions
  30. //#define USE_UNITY
  31. #endif
  32. #endif
  33. #include "bc6_common_encoder.h"
  34. #ifdef ASPM_GPU
  35. void memset(CGU_UINT8* srcdata, CGU_UINT8 value, CGU_INT size)
  36. {
  37. for (CGU_INT i = 0; i < size; i++)
  38. *srcdata++ = value;
  39. }
  40. void memcpy(CGU_UINT8* dstdata, CGU_UINT8* srcdata, CGU_INT size)
  41. {
  42. for (CGU_INT i = 0; i < size; i++)
  43. {
  44. *dstdata = *srcdata;
  45. srcdata++;
  46. dstdata++;
  47. }
  48. }
  49. void swap(CGU_INT A, CGU_INT B)
  50. {
  51. CGU_INT hold = A;
  52. A = B;
  53. B = hold;
  54. }
  55. #define abs fabs
  56. #define floorf floor
  57. #define sqrtf sqrt
  58. #define logf log
  59. #define ceilf ceil
  60. #endif
  61. __constant CGU_UINT8 BC6_PARTITIONS[MAX_BC6H_PARTITIONS][MAX_SUBSET_SIZE] = {
  62. {// 0
  63. 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1},
  64. {// 1
  65. 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1},
  66. {// 2
  67. 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1},
  68. {// 3
  69. 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1},
  70. {// 4
  71. 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1},
  72. {// 5
  73. 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1},
  74. {// 6
  75. 0,
  76. 0,
  77. 0,
  78. 1,
  79. 0,
  80. 0,
  81. 1,
  82. 1,
  83. 0,
  84. 1,
  85. 1,
  86. 1,
  87. 1,
  88. 1,
  89. 1,
  90. 1},
  91. {// 7
  92. 0,
  93. 0,
  94. 0,
  95. 0,
  96. 0,
  97. 0,
  98. 0,
  99. 1,
  100. 0,
  101. 0,
  102. 1,
  103. 1,
  104. 0,
  105. 1,
  106. 1,
  107. 1},
  108. {// 8
  109. 0,
  110. 0,
  111. 0,
  112. 0,
  113. 0,
  114. 0,
  115. 0,
  116. 0,
  117. 0,
  118. 0,
  119. 0,
  120. 1,
  121. 0,
  122. 0,
  123. 1,
  124. 1},
  125. {// 9
  126. 0,
  127. 0,
  128. 1,
  129. 1,
  130. 0,
  131. 1,
  132. 1,
  133. 1,
  134. 1,
  135. 1,
  136. 1,
  137. 1,
  138. 1,
  139. 1,
  140. 1,
  141. 1},
  142. {// 10
  143. 0,
  144. 0,
  145. 0,
  146. 0,
  147. 0,
  148. 0,
  149. 0,
  150. 1,
  151. 0,
  152. 1,
  153. 1,
  154. 1,
  155. 1,
  156. 1,
  157. 1,
  158. 1},
  159. {// 11
  160. 0,
  161. 0,
  162. 0,
  163. 0,
  164. 0,
  165. 0,
  166. 0,
  167. 0,
  168. 0,
  169. 0,
  170. 0,
  171. 1,
  172. 0,
  173. 1,
  174. 1,
  175. 1},
  176. {// 12
  177. 0,
  178. 0,
  179. 0,
  180. 1,
  181. 0,
  182. 1,
  183. 1,
  184. 1,
  185. 1,
  186. 1,
  187. 1,
  188. 1,
  189. 1,
  190. 1,
  191. 1,
  192. 1},
  193. {// 13
  194. 0,
  195. 0,
  196. 0,
  197. 0,
  198. 0,
  199. 0,
  200. 0,
  201. 0,
  202. 1,
  203. 1,
  204. 1,
  205. 1,
  206. 1,
  207. 1,
  208. 1,
  209. 1},
  210. {// 14
  211. 0,
  212. 0,
  213. 0,
  214. 0,
  215. 1,
  216. 1,
  217. 1,
  218. 1,
  219. 1,
  220. 1,
  221. 1,
  222. 1,
  223. 1,
  224. 1,
  225. 1,
  226. 1},
  227. {// 15
  228. 0,
  229. 0,
  230. 0,
  231. 0,
  232. 0,
  233. 0,
  234. 0,
  235. 0,
  236. 0,
  237. 0,
  238. 0,
  239. 0,
  240. 1,
  241. 1,
  242. 1,
  243. 1},
  244. {// 16
  245. 0,
  246. 0,
  247. 0,
  248. 0,
  249. 1,
  250. 0,
  251. 0,
  252. 0,
  253. 1,
  254. 1,
  255. 1,
  256. 0,
  257. 1,
  258. 1,
  259. 1,
  260. 1},
  261. {// 17
  262. 0,
  263. 1,
  264. 1,
  265. 1,
  266. 0,
  267. 0,
  268. 0,
  269. 1,
  270. 0,
  271. 0,
  272. 0,
  273. 0,
  274. 0,
  275. 0,
  276. 0,
  277. 0},
  278. {// 18
  279. 0,
  280. 0,
  281. 0,
  282. 0,
  283. 0,
  284. 0,
  285. 0,
  286. 0,
  287. 1,
  288. 0,
  289. 0,
  290. 0,
  291. 1,
  292. 1,
  293. 1,
  294. 0},
  295. {// 19
  296. 0,
  297. 1,
  298. 1,
  299. 1,
  300. 0,
  301. 0,
  302. 1,
  303. 1,
  304. 0,
  305. 0,
  306. 0,
  307. 1,
  308. 0,
  309. 0,
  310. 0,
  311. 0},
  312. {// 20
  313. 0,
  314. 0,
  315. 1,
  316. 1,
  317. 0,
  318. 0,
  319. 0,
  320. 1,
  321. 0,
  322. 0,
  323. 0,
  324. 0,
  325. 0,
  326. 0,
  327. 0,
  328. 0},
  329. {// 21
  330. 0,
  331. 0,
  332. 0,
  333. 0,
  334. 1,
  335. 0,
  336. 0,
  337. 0,
  338. 1,
  339. 1,
  340. 0,
  341. 0,
  342. 1,
  343. 1,
  344. 1,
  345. 0},
  346. {// 22
  347. 0,
  348. 0,
  349. 0,
  350. 0,
  351. 0,
  352. 0,
  353. 0,
  354. 0,
  355. 1,
  356. 0,
  357. 0,
  358. 0,
  359. 1,
  360. 1,
  361. 0,
  362. 0},
  363. {// 23
  364. 0,
  365. 1,
  366. 1,
  367. 1,
  368. 0,
  369. 0,
  370. 1,
  371. 1,
  372. 0,
  373. 0,
  374. 1,
  375. 1,
  376. 0,
  377. 0,
  378. 0,
  379. 1},
  380. {// 24
  381. 0,
  382. 0,
  383. 1,
  384. 1,
  385. 0,
  386. 0,
  387. 0,
  388. 1,
  389. 0,
  390. 0,
  391. 0,
  392. 1,
  393. 0,
  394. 0,
  395. 0,
  396. 0},
  397. {// 25
  398. 0,
  399. 0,
  400. 0,
  401. 0,
  402. 1,
  403. 0,
  404. 0,
  405. 0,
  406. 1,
  407. 0,
  408. 0,
  409. 0,
  410. 1,
  411. 1,
  412. 0,
  413. 0},
  414. {// 26
  415. 0,
  416. 1,
  417. 1,
  418. 0,
  419. 0,
  420. 1,
  421. 1,
  422. 0,
  423. 0,
  424. 1,
  425. 1,
  426. 0,
  427. 0,
  428. 1,
  429. 1,
  430. 0},
  431. {// 27
  432. 0,
  433. 0,
  434. 1,
  435. 1,
  436. 0,
  437. 1,
  438. 1,
  439. 0,
  440. 0,
  441. 1,
  442. 1,
  443. 0,
  444. 1,
  445. 1,
  446. 0,
  447. 0},
  448. {// 28
  449. 0,
  450. 0,
  451. 0,
  452. 1,
  453. 0,
  454. 1,
  455. 1,
  456. 1,
  457. 1,
  458. 1,
  459. 1,
  460. 0,
  461. 1,
  462. 0,
  463. 0,
  464. 0},
  465. {// 29
  466. 0,
  467. 0,
  468. 0,
  469. 0,
  470. 1,
  471. 1,
  472. 1,
  473. 1,
  474. 1,
  475. 1,
  476. 1,
  477. 1,
  478. 0,
  479. 0,
  480. 0,
  481. 0},
  482. {// 30
  483. 0,
  484. 1,
  485. 1,
  486. 1,
  487. 0,
  488. 0,
  489. 0,
  490. 1,
  491. 1,
  492. 0,
  493. 0,
  494. 0,
  495. 1,
  496. 1,
  497. 1,
  498. 0},
  499. {// 31
  500. 0,
  501. 0,
  502. 1,
  503. 1,
  504. 1,
  505. 0,
  506. 0,
  507. 1,
  508. 1,
  509. 0,
  510. 0,
  511. 1,
  512. 1,
  513. 1,
  514. 0,
  515. 0},
  516. };
  517. CGU_DWORD get_partition_subset(CGU_INT subset, CGU_INT partI, CGU_INT index)
  518. {
  519. if (subset)
  520. return BC6_PARTITIONS[partI][index];
  521. else
  522. return 0;
  523. }
  524. void Partition(CGU_INT shape,
  525. CGU_FLOAT in[][MAX_DIMENSION_BIG],
  526. CGU_FLOAT subsets[MAX_SUBSETS][MAX_SUBSET_SIZE][MAX_DIMENSION_BIG], //[3][16][4]
  527. CGU_INT count[MAX_SUBSETS],
  528. CGU_INT8 ShapeTableToUse,
  529. CGU_INT dimension)
  530. {
  531. int i, j;
  532. int insubset = -1, inpart = 0;
  533. // Dont use memset: this is better for now
  534. for (i = 0; i < MAX_SUBSETS; i++)
  535. count[i] = 0;
  536. switch (ShapeTableToUse)
  537. {
  538. case 0:
  539. case 1:
  540. insubset = 0;
  541. inpart = 0;
  542. break;
  543. case 2:
  544. insubset = 1;
  545. inpart = shape;
  546. break;
  547. default:
  548. break;
  549. }
  550. // Nothing to do!!: Must indicate an error to user
  551. if (insubset == -1)
  552. return; // Nothing to do!!
  553. for (i = 0; i < MAX_SUBSET_SIZE; i++)
  554. {
  555. int subset = get_partition_subset(insubset, inpart, i);
  556. for (j = 0; j < dimension; j++)
  557. {
  558. subsets[subset][count[subset]][j] = in[i][j];
  559. }
  560. if (dimension < MAX_DIMENSION_BIG)
  561. {
  562. subsets[subset][count[subset]][j] = 0.0;
  563. }
  564. count[subset]++;
  565. }
  566. }
  567. void GetEndPoints(CGU_FLOAT EndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  568. CGU_FLOAT outB[MAX_SUBSETS][MAX_SUBSET_SIZE][MAX_DIMENSION_BIG],
  569. CGU_INT max_subsets,
  570. int entryCount[MAX_SUBSETS])
  571. {
  572. // Should have some sort of error notification!
  573. if (max_subsets > MAX_SUBSETS)
  574. return;
  575. // Save Min and Max OutB points as EndPoints
  576. for (int subset = 0; subset < max_subsets; subset++)
  577. {
  578. // We now have points on direction vector(s)
  579. // find the min and max points
  580. CGU_FLOAT min = CMP_HALF_MAX;
  581. CGU_FLOAT max = 0;
  582. CGU_FLOAT val;
  583. int mini = 0;
  584. int maxi = 0;
  585. for (int i = 0; i < entryCount[subset]; i++)
  586. {
  587. val = outB[subset][i][0] + outB[subset][i][1] + outB[subset][i][2];
  588. if (val < min)
  589. {
  590. min = val;
  591. mini = i;
  592. }
  593. if (val > max)
  594. {
  595. max = val;
  596. maxi = i;
  597. }
  598. }
  599. // Is round best for this !
  600. for (int c = 0; c < MAX_DIMENSION_BIG; c++)
  601. {
  602. EndPoints[subset][0][c] = outB[subset][mini][c];
  603. }
  604. for (int c = 0; c < MAX_DIMENSION_BIG; c++)
  605. {
  606. EndPoints[subset][1][c] = outB[subset][maxi][c];
  607. }
  608. }
  609. }
  610. void covariance_d(CGU_FLOAT data[][MAX_DIMENSION_BIG], CGU_INT numEntries, CGU_FLOAT cov[MAX_DIMENSION_BIG][MAX_DIMENSION_BIG], CGU_INT dimension)
  611. {
  612. #ifdef USE_DBGTRACE
  613. DbgTrace(());
  614. #endif
  615. int i, j, k;
  616. for (i = 0; i < dimension; i++)
  617. for (j = 0; j <= i; j++)
  618. {
  619. cov[i][j] = 0;
  620. for (k = 0; k < numEntries; k++)
  621. cov[i][j] += data[k][i] * data[k][j];
  622. }
  623. for (i = 0; i < dimension; i++)
  624. for (j = i + 1; j < dimension; j++)
  625. cov[i][j] = cov[j][i];
  626. }
  627. void centerInPlace_d(CGU_FLOAT data[][MAX_DIMENSION_BIG], int numEntries, CGU_FLOAT mean[MAX_DIMENSION_BIG], CGU_INT dimension)
  628. {
  629. #ifdef USE_DBGTRACE
  630. DbgTrace(());
  631. #endif
  632. int i, k;
  633. for (i = 0; i < dimension; i++)
  634. {
  635. mean[i] = 0;
  636. for (k = 0; k < numEntries; k++)
  637. mean[i] += data[k][i];
  638. }
  639. if (!numEntries)
  640. return;
  641. for (i = 0; i < dimension; i++)
  642. {
  643. mean[i] /= numEntries;
  644. for (k = 0; k < numEntries; k++)
  645. data[k][i] -= mean[i];
  646. }
  647. }
  648. void eigenVector_d(CGU_FLOAT cov[MAX_DIMENSION_BIG][MAX_DIMENSION_BIG], CGU_FLOAT vector[MAX_DIMENSION_BIG], CGU_INT dimension)
  649. {
  650. #ifdef USE_DBGTRACE
  651. DbgTrace(());
  652. #endif
  653. // calculate an eigenvecto corresponding to a biggest eigenvalue
  654. // will work for non-zero non-negative matricies only
  655. #define EV_ITERATION_NUMBER 20
  656. #define EV_SLACK 2 /* additive for exp base 2)*/
  657. CGU_INT i, j, k, l, m, n, p, q;
  658. CGU_FLOAT c[2][MAX_DIMENSION_BIG][MAX_DIMENSION_BIG];
  659. CGU_FLOAT maxDiag;
  660. for (i = 0; i < dimension; i++)
  661. for (j = 0; j < dimension; j++)
  662. c[0][i][j] = cov[i][j];
  663. p = (int)floorf(log((BC6_FLT_MAX_EXP - EV_SLACK) / ceilf(logf((CGU_FLOAT)dimension) / logf(2.0f))) / logf(2.0f));
  664. //assert(p>0);
  665. p = p > 0 ? p : 1;
  666. q = (EV_ITERATION_NUMBER + p - 1) / p;
  667. l = 0;
  668. for (n = 0; n < q; n++)
  669. {
  670. maxDiag = 0;
  671. for (i = 0; i < dimension; i++)
  672. maxDiag = c[l][i][i] > maxDiag ? c[l][i][i] : maxDiag;
  673. if (maxDiag <= 0)
  674. {
  675. return;
  676. }
  677. //assert(maxDiag >0);
  678. for (i = 0; i < dimension; i++)
  679. for (j = 0; j < dimension; j++)
  680. c[l][i][j] /= maxDiag;
  681. for (m = 0; m < p; m++)
  682. {
  683. for (i = 0; i < dimension; i++)
  684. for (j = 0; j < dimension; j++)
  685. {
  686. CGU_FLOAT temp = 0;
  687. for (k = 0; k < dimension; k++)
  688. {
  689. // Notes:
  690. // This is the most consuming portion of the code and needs optimizing for perfromance
  691. temp += c[l][i][k] * c[l][k][j];
  692. }
  693. c[1 - l][i][j] = temp;
  694. }
  695. l = 1 - l;
  696. }
  697. }
  698. maxDiag = 0;
  699. k = 0;
  700. for (i = 0; i < dimension; i++)
  701. {
  702. k = c[l][i][i] > maxDiag ? i : k;
  703. maxDiag = c[l][i][i] > maxDiag ? c[l][i][i] : maxDiag;
  704. }
  705. CGU_FLOAT t;
  706. t = 0;
  707. for (i = 0; i < dimension; i++)
  708. {
  709. t += c[l][k][i] * c[l][k][i];
  710. vector[i] = c[l][k][i];
  711. }
  712. // normalization is really optional
  713. t = sqrtf(t);
  714. //assert(t>0);
  715. if (t <= 0)
  716. {
  717. return;
  718. }
  719. for (i = 0; i < dimension; i++)
  720. vector[i] /= t;
  721. }
  722. void project_d(CGU_FLOAT data[][MAX_DIMENSION_BIG],
  723. CGU_INT numEntries,
  724. CGU_FLOAT vector[MAX_DIMENSION_BIG],
  725. CGU_FLOAT projection[MAX_ENTRIES],
  726. CGU_INT dimension)
  727. {
  728. #ifdef USE_DBGTRACE
  729. DbgTrace(());
  730. #endif
  731. // assume that vector is normalized already
  732. int i, k;
  733. for (k = 0; k < numEntries; k++)
  734. {
  735. projection[k] = 0;
  736. for (i = 0; i < dimension; i++)
  737. {
  738. projection[k] += data[k][i] * vector[i];
  739. }
  740. }
  741. }
  742. typedef struct
  743. {
  744. CGU_FLOAT d;
  745. int i;
  746. } a;
  747. inline CGU_INT a_compare(const void* arg1, const void* arg2)
  748. {
  749. if (((a*)arg1)->d - ((a*)arg2)->d > 0)
  750. return 1;
  751. if (((a*)arg1)->d - ((a*)arg2)->d < 0)
  752. return -1;
  753. return 0;
  754. };
  755. void sortProjection(CGU_FLOAT projection[MAX_ENTRIES], CGU_INT order[MAX_ENTRIES], CGU_INT numEntries)
  756. {
  757. int i;
  758. a what[MAX_ENTRIES + MAX_PARTITIONS_TABLE];
  759. for (i = 0; i < numEntries; i++)
  760. what[what[i].i = i].d = projection[i];
  761. #ifdef USE_QSORT
  762. qsort((void*)&what, numEntries, sizeof(a), a_compare);
  763. #else
  764. {
  765. int j;
  766. int tmp;
  767. CGU_FLOAT tmp_d;
  768. for (i = 1; i < numEntries; i++)
  769. {
  770. for (j = i; j > 0; j--)
  771. {
  772. if (what[j - 1].d > what[j].d)
  773. {
  774. tmp = what[j].i;
  775. tmp_d = what[j].d;
  776. what[j].i = what[j - 1].i;
  777. what[j].d = what[j - 1].d;
  778. what[j - 1].i = tmp;
  779. what[j - 1].d = tmp_d;
  780. }
  781. }
  782. }
  783. }
  784. #endif
  785. for (i = 0; i < numEntries; i++)
  786. order[i] = what[i].i;
  787. };
  788. CGU_FLOAT totalError_d(CGU_FLOAT data[MAX_ENTRIES][MAX_DIMENSION_BIG], CGU_FLOAT data2[MAX_ENTRIES][MAX_DIMENSION_BIG], CGU_INT numEntries, CGU_INT dimension)
  789. {
  790. int i, j;
  791. CGU_FLOAT t = 0;
  792. for (i = 0; i < numEntries; i++)
  793. for (j = 0; j < dimension; j++)
  794. t += (data[i][j] - data2[i][j]) * (data[i][j] - data2[i][j]);
  795. return t;
  796. };
  797. // input:
  798. //
  799. // v_ points, might be uncentered
  800. // k - number of points in the ramp
  801. // n - number of points in v_
  802. //
  803. // output:
  804. // index, uncentered, in the range 0..k-1
  805. //
  806. void quant_AnD_Shell(CGU_FLOAT* v_, CGU_INT k, CGU_INT n, CGU_INT idx[MAX_ENTRIES])
  807. {
  808. #define MAX_BLOCK MAX_ENTRIES
  809. CGU_INT i, j;
  810. CGU_FLOAT v[MAX_BLOCK];
  811. CGU_FLOAT z[MAX_BLOCK];
  812. a d[MAX_BLOCK];
  813. CGU_FLOAT l;
  814. CGU_FLOAT mm;
  815. CGU_FLOAT r = 0;
  816. CGU_INT mi;
  817. CGU_FLOAT m, M, s, dm = 0.;
  818. m = M = v_[0];
  819. for (i = 1; i < n; i++)
  820. {
  821. m = m < v_[i] ? m : v_[i];
  822. M = M > v_[i] ? M : v_[i];
  823. }
  824. if (M == m)
  825. {
  826. for (i = 0; i < n; i++)
  827. idx[i] = 0;
  828. return;
  829. }
  830. //assert(M - m >0);
  831. s = (k - 1) / (M - m);
  832. for (i = 0; i < n; i++)
  833. {
  834. v[i] = v_[i] * s;
  835. idx[i] = (int)(z[i] = (v[i] + 0.5f /* stabilizer*/ - m * s)); //floorf(v[i] + 0.5f /* stabilizer*/ - m *s));
  836. d[i].d = v[i] - z[i] - m * s;
  837. d[i].i = i;
  838. dm += d[i].d;
  839. r += d[i].d * d[i].d;
  840. }
  841. if (n * r - dm * dm >= (CGU_FLOAT)(n - 1) / 4 /*slack*/ / 2)
  842. {
  843. dm /= (CGU_FLOAT)n;
  844. for (i = 0; i < n; i++)
  845. d[i].d -= dm;
  846. //!!! Need an OpenCL version of qsort
  847. #ifdef USE_QSORT
  848. qsort((void*)&d, n, sizeof(a), a_compare);
  849. #else
  850. {
  851. CGU_INT tmp;
  852. CGU_FLOAT tmp_d;
  853. for (i = 1; i < n; i++)
  854. {
  855. for (j = i; j > 0; j--)
  856. {
  857. if (d[j - 1].d > d[j].d)
  858. {
  859. tmp = d[j].i;
  860. tmp_d = d[j].d;
  861. d[j].i = d[j - 1].i;
  862. d[j].d = d[j - 1].d;
  863. d[j - 1].i = tmp;
  864. d[j - 1].d = tmp_d;
  865. }
  866. }
  867. }
  868. }
  869. #endif
  870. // got into fundamental simplex
  871. // move coordinate system origin to its center
  872. for (i = 0; i < n; i++)
  873. d[i].d -= (2.0f * (CGU_FLOAT)i + 1.0f - (CGU_FLOAT)n) / 2.0f / (CGU_FLOAT)n;
  874. mm = l = 0.;
  875. j = -1;
  876. for (i = 0; i < n; i++)
  877. {
  878. l += d[i].d;
  879. if (l < mm)
  880. {
  881. mm = l;
  882. j = i;
  883. }
  884. }
  885. // position which should be in 0
  886. j = j + 1;
  887. j = j % n;
  888. for (i = j; i < n; i++)
  889. idx[d[i].i]++;
  890. }
  891. // get rid of an offset in idx
  892. mi = idx[0];
  893. for (i = 1; i < n; i++)
  894. mi = mi < idx[i] ? mi : idx[i];
  895. for (i = 0; i < n; i++)
  896. idx[i] -= mi;
  897. }
  898. CGU_FLOAT optQuantAnD_d(CGU_FLOAT data[MAX_ENTRIES][MAX_DIMENSION_BIG],
  899. CGU_INT numEntries,
  900. CGU_INT numClusters,
  901. CGU_INT index[MAX_ENTRIES],
  902. CGU_FLOAT out[MAX_ENTRIES][MAX_DIMENSION_BIG],
  903. CGU_FLOAT direction[MAX_DIMENSION_BIG],
  904. CGU_FLOAT* step,
  905. CGU_INT dimension,
  906. CGU_FLOAT quality)
  907. {
  908. CGU_INT index_[MAX_ENTRIES];
  909. CGU_INT maxTry = (int)(MAX_TRY * quality);
  910. CGU_INT try_two = 50;
  911. CGU_INT i, j, k;
  912. CGU_FLOAT t, s;
  913. CGU_FLOAT centered[MAX_ENTRIES][MAX_DIMENSION_BIG];
  914. CGU_FLOAT mean[MAX_DIMENSION_BIG];
  915. CGU_FLOAT cov[MAX_DIMENSION_BIG][MAX_DIMENSION_BIG];
  916. CGU_FLOAT projected[MAX_ENTRIES];
  917. CGU_INT order_[MAX_ENTRIES];
  918. for (i = 0; i < numEntries; i++)
  919. for (j = 0; j < dimension; j++)
  920. centered[i][j] = data[i][j];
  921. centerInPlace_d(centered, numEntries, mean, dimension);
  922. covariance_d(centered, numEntries, cov, dimension);
  923. // check if they all are the same
  924. t = 0;
  925. for (j = 0; j < dimension; j++)
  926. t += cov[j][j];
  927. if (numEntries == 0)
  928. {
  929. for (i = 0; i < numEntries; i++)
  930. {
  931. index[i] = 0;
  932. for (j = 0; j < dimension; j++)
  933. out[i][j] = mean[j];
  934. }
  935. return 0.0f;
  936. }
  937. eigenVector_d(cov, direction, dimension);
  938. project_d(centered, numEntries, direction, projected, dimension);
  939. for (i = 0; i < maxTry; i++)
  940. {
  941. CGU_INT done = 0;
  942. if (i)
  943. {
  944. do
  945. {
  946. CGU_FLOAT q;
  947. q = s = t = 0;
  948. for (k = 0; k < numEntries; k++)
  949. {
  950. s += index[k];
  951. t += index[k] * index[k];
  952. }
  953. for (j = 0; j < dimension; j++)
  954. {
  955. direction[j] = 0;
  956. for (k = 0; k < numEntries; k++)
  957. direction[j] += centered[k][j] * index[k];
  958. q += direction[j] * direction[j];
  959. }
  960. s /= (CGU_FLOAT)numEntries;
  961. t = t - s * s * (CGU_FLOAT)numEntries;
  962. //assert(t != 0);
  963. t = (t == 0.0f ? 0.0f : 1.0f / t);
  964. // We need to requantize
  965. q = sqrtf(q);
  966. t *= q;
  967. if (q != 0)
  968. for (j = 0; j < dimension; j++)
  969. direction[j] /= q;
  970. // direction normalized
  971. project_d(centered, numEntries, direction, projected, dimension);
  972. sortProjection(projected, order_, numEntries);
  973. CGU_INT index__[MAX_ENTRIES];
  974. // it's projected and centered; cluster centers are (index[i]-s)*t (*dir)
  975. k = 0;
  976. for (j = 0; j < numEntries; j++)
  977. {
  978. while (projected[order_[j]] > (k + 0.5 - s) * t && k < numClusters - 1)
  979. k++;
  980. index__[order_[j]] = k;
  981. }
  982. done = 1;
  983. for (j = 0; j < numEntries; j++)
  984. {
  985. done = (done && (index__[j] == index[j]));
  986. index[j] = index__[j];
  987. }
  988. } while (!done && try_two--);
  989. if (i == 1)
  990. for (j = 0; j < numEntries; j++)
  991. index_[j] = index[j];
  992. else
  993. {
  994. done = 1;
  995. for (j = 0; j < numEntries; j++)
  996. {
  997. done = (done && (index_[j] == index[j]));
  998. index_[j] = index_[j];
  999. }
  1000. if (done)
  1001. break;
  1002. }
  1003. }
  1004. quant_AnD_Shell(projected, numClusters, numEntries, index);
  1005. }
  1006. s = t = 0;
  1007. CGU_FLOAT q = 0;
  1008. for (k = 0; k < numEntries; k++)
  1009. {
  1010. s += index[k];
  1011. t += index[k] * index[k];
  1012. }
  1013. for (j = 0; j < dimension; j++)
  1014. {
  1015. direction[j] = 0;
  1016. for (k = 0; k < numEntries; k++)
  1017. direction[j] += centered[k][j] * index[k];
  1018. q += direction[j] * direction[j];
  1019. }
  1020. s /= (CGU_FLOAT)numEntries;
  1021. t = t - s * s * (CGU_FLOAT)numEntries;
  1022. //assert(t != 0);
  1023. t = (t == 0.0 ? 0.0f : 1.0f / t);
  1024. for (i = 0; i < numEntries; i++)
  1025. for (j = 0; j < dimension; j++)
  1026. out[i][j] = mean[j] + direction[j] * t * (index[i] - s);
  1027. // normalize direction for output
  1028. q = sqrtf(q);
  1029. *step = t * q;
  1030. for (j = 0; j < dimension; j++)
  1031. direction[j] /= q;
  1032. return totalError_d(data, out, numEntries, dimension);
  1033. }
  1034. void clampF16Max(CGU_FLOAT EndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG], CGU_BOOL isSigned)
  1035. {
  1036. for (CGU_INT region = 0; region < 2; region++)
  1037. for (CGU_INT ab = 0; ab < 2; ab++)
  1038. for (CGU_INT rgb = 0; rgb < 3; rgb++)
  1039. {
  1040. if (isSigned)
  1041. {
  1042. if (EndPoints[region][ab][rgb] < -FLT16_MAX)
  1043. EndPoints[region][ab][rgb] = -FLT16_MAX;
  1044. else if (EndPoints[region][ab][rgb] > FLT16_MAX)
  1045. EndPoints[region][ab][rgb] = FLT16_MAX;
  1046. }
  1047. else
  1048. {
  1049. if (EndPoints[region][ab][rgb] < 0.0)
  1050. EndPoints[region][ab][rgb] = 0.0;
  1051. else if (EndPoints[region][ab][rgb] > FLT16_MAX)
  1052. EndPoints[region][ab][rgb] = FLT16_MAX;
  1053. }
  1054. // Zero region
  1055. // if ((EndPoints[region][ab][rgb] > -0.01) && ((EndPoints[region][ab][rgb] < 0.01))) EndPoints[region][ab][rgb] = 0.0;
  1056. }
  1057. }
  1058. //=====================================================================================================================
  1059. #define LOG_CL_BASE 2
  1060. #define BIT_BASE 5
  1061. #define LOG_CL_RANGE 5
  1062. #define BIT_RANGE 9
  1063. #define MAX_CLUSTERS_BIG 16
  1064. #ifdef USE_BC6RAMPS
  1065. int spidx(int in_data, int in_clogs, int in_bits, int in_p2, int in_o1, int in_o2, int in_i)
  1066. {
  1067. // use BC7 sp_idx
  1068. return 0;
  1069. }
  1070. float sperr(int in_data, int clogs, int bits, int p2, int o1, int o2)
  1071. {
  1072. // use BC7 sp_err
  1073. return 0, 0f;
  1074. }
  1075. #endif
  1076. __constant CGU_FLOAT rampLerpWeightsBC6[5][16] = {
  1077. {0.0}, // 0 bit index
  1078. {0.0, 1.0}, // 1 bit index
  1079. {0.0, 21.0 / 64.0, 43.0 / 64.0, 1.0}, // 2 bit index
  1080. {0.0, 9.0 / 64.0, 18.0 / 64.0, 27.0 / 64.0, 37.0 / 64.0, 46.0 / 64.0, 55.0 / 64.0, 1.0}, // 3 bit index
  1081. {0.0,
  1082. 4.0 / 64.0,
  1083. 9.0 / 64.0,
  1084. 13.0 / 64.0,
  1085. 17.0 / 64.0,
  1086. 21.0 / 64.0,
  1087. 26.0 / 64.0,
  1088. 30.0 / 64.0,
  1089. 34.0 / 64.0,
  1090. 38.0 / 64.0,
  1091. 43.0 / 64.0,
  1092. 47.0 / 64.0,
  1093. 51.0 / 64.0,
  1094. 55.0 / 64.0,
  1095. 60.0 / 64.0,
  1096. 1.0} // 4 bit index
  1097. };
  1098. CGU_FLOAT rampf(CGU_INT clogs, CGU_FLOAT p1, CGU_FLOAT p2, CGU_INT indexPos)
  1099. {
  1100. // (clogs+ LOG_CL_BASE) starts from 2 to 4
  1101. return (CGU_FLOAT)p1 + rampLerpWeightsBC6[clogs + LOG_CL_BASE][indexPos] * (p2 - p1);
  1102. }
  1103. CGU_INT all_same_d(CGU_FLOAT d[][MAX_DIMENSION_BIG], CGU_INT n, CGU_INT dimension)
  1104. {
  1105. CGU_INT i, j;
  1106. CGU_INT same = 1;
  1107. for (i = 1; i < n; i++)
  1108. for (j = 0; j < dimension; j++)
  1109. same = same && (d[0][j] == d[i][j]);
  1110. return (same);
  1111. }
  1112. // return the max index from a set of indexes
  1113. CGU_INT max_index(CGU_INT a[], CGU_INT n)
  1114. {
  1115. CGU_INT i, m = a[0];
  1116. for (i = 0; i < n; i++)
  1117. m = m > a[i] ? m : a[i];
  1118. return (m);
  1119. }
  1120. CGU_INT cluster_mean_d_d(CGU_FLOAT d[MAX_ENTRIES][MAX_DIMENSION_BIG],
  1121. CGU_FLOAT mean[MAX_ENTRIES][MAX_DIMENSION_BIG],
  1122. CGU_INT index[],
  1123. CGU_INT i_comp[],
  1124. CGU_INT i_cnt[],
  1125. CGU_INT n,
  1126. CGU_INT dimension)
  1127. {
  1128. // unused index values are underfined
  1129. CGU_INT i, j, k;
  1130. //assert(n!=0);
  1131. for (i = 0; i < n; i++)
  1132. for (j = 0; j < dimension; j++)
  1133. {
  1134. // assert(index[i]<MAX_CLUSTERS_BIG);
  1135. mean[index[i]][j] = 0;
  1136. i_cnt[index[i]] = 0;
  1137. }
  1138. k = 0;
  1139. for (i = 0; i < n; i++)
  1140. {
  1141. for (j = 0; j < dimension; j++)
  1142. mean[index[i]][j] += d[i][j];
  1143. if (i_cnt[index[i]] == 0)
  1144. i_comp[k++] = index[i];
  1145. i_cnt[index[i]]++;
  1146. }
  1147. for (i = 0; i < k; i++)
  1148. for (j = 0; j < dimension; j++)
  1149. mean[i_comp[i]][j] /= (CGU_FLOAT)i_cnt[i_comp[i]];
  1150. return k;
  1151. }
  1152. void mean_d_d(CGU_FLOAT d[][MAX_DIMENSION_BIG], CGU_FLOAT mean[MAX_DIMENSION_BIG], CGU_INT n, CGU_INT dimension)
  1153. {
  1154. CGU_INT i, j;
  1155. for (j = 0; j < dimension; j++)
  1156. mean[j] = 0;
  1157. for (i = 0; i < n; i++)
  1158. for (j = 0; j < dimension; j++)
  1159. mean[j] += d[i][j];
  1160. for (j = 0; j < dimension; j++)
  1161. mean[j] /= (CGU_FLOAT)n;
  1162. }
  1163. void index_collapse_kernel(CGU_INT index[], CGU_INT numEntries)
  1164. {
  1165. CGU_INT k;
  1166. CGU_INT d, D;
  1167. CGU_INT mi;
  1168. CGU_INT Mi;
  1169. if (numEntries == 0)
  1170. return;
  1171. mi = Mi = index[0];
  1172. for (k = 1; k < numEntries; k++)
  1173. {
  1174. mi = mi < index[k] ? mi : index[k];
  1175. Mi = Mi > index[k] ? Mi : index[k];
  1176. }
  1177. D = 1;
  1178. for (d = 2; d <= Mi - mi; d++)
  1179. {
  1180. for (k = 0; k < numEntries; k++)
  1181. if ((index[k] - mi) % d != 0)
  1182. break;
  1183. if (k >= numEntries)
  1184. D = d;
  1185. }
  1186. for (k = 0; k < numEntries; k++)
  1187. index[k] = (index[k] - mi) / D;
  1188. }
  1189. CGU_INT max_int(CGU_INT a[], CGU_INT n)
  1190. {
  1191. CGU_INT i, m = a[0];
  1192. for (i = 0; i < n; i++)
  1193. m = m > a[i] ? m : a[i];
  1194. return (m);
  1195. }
  1196. __constant CGU_INT npv_nd[2][2 * MAX_DIMENSION_BIG] = {
  1197. {1, 2, 4, 8, 16, 32, 0, 0}, //dimension = 3
  1198. {1, 2, 4, 0, 0, 0, 0, 0} //dimension = 4
  1199. };
  1200. __constant short par_vectors_nd[2][8][128][2][MAX_DIMENSION_BIG] = {
  1201. {// Dimension = 3
  1202. {{{0, 0, 0, 0}, {0, 0, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}}},
  1203. // 3*n+1 BCC 3*n+1 Cartesian 3*n //same parity
  1204. {// SAME_PAR
  1205. {{0, 0, 0}, {0, 0, 0}},
  1206. {{1, 1, 1}, {1, 1, 1}}},
  1207. // 3*n+2 BCC 3*n+1 BCC 3*n+1
  1208. {// BCC
  1209. {{0, 0, 0}, {0, 0, 0}},
  1210. {{0, 0, 0}, {1, 1, 1}},
  1211. {{1, 1, 1}, {0, 0, 0}},
  1212. {{1, 1, 1}, {1, 1, 1}}},
  1213. // 3*n+3 FCC ??? // ??????
  1214. // BCC with FCC same or inverted, symmetric
  1215. {
  1216. // BCC_SAME_FCC
  1217. {{0, 0, 0}, {0, 0, 0}},
  1218. {{1, 1, 0}, {1, 1, 0}},
  1219. {{1, 0, 1}, {1, 0, 1}},
  1220. {{0, 1, 1}, {0, 1, 1}},
  1221. {{0, 0, 0}, {1, 1, 1}},
  1222. {{1, 1, 1}, {0, 0, 0}},
  1223. {{0, 1, 0}, {0, 1, 0}}, // ??
  1224. {{1, 1, 1}, {1, 1, 1}},
  1225. },
  1226. // 3*n+4 FCC 3*n+2 FCC 3*n+2
  1227. {
  1228. {{0, 0, 0}, {0, 0, 0}},
  1229. {{1, 1, 0}, {0, 0, 0}},
  1230. {{1, 0, 1}, {0, 0, 0}},
  1231. {{0, 1, 1}, {0, 0, 0}},
  1232. {{0, 0, 0}, {1, 1, 0}},
  1233. {{1, 1, 0}, {1, 1, 0}},
  1234. {{1, 0, 1}, {1, 1, 0}},
  1235. {{0, 1, 1}, {1, 1, 0}},
  1236. {{0, 0, 0}, {1, 0, 1}},
  1237. {{1, 1, 0}, {1, 0, 1}},
  1238. {{1, 0, 1}, {1, 0, 1}},
  1239. {{0, 1, 1}, {1, 0, 1}},
  1240. {{0, 0, 0}, {0, 1, 1}},
  1241. {{1, 1, 0}, {0, 1, 1}},
  1242. {{1, 0, 1}, {0, 1, 1}},
  1243. {{0, 1, 1}, {0, 1, 1}}},
  1244. // 3*n+5 Cartesian 3*n+3 FCC 3*n+2 //D^*[6]
  1245. {
  1246. {{0, 0, 0}, {0, 0, 0}}, {{1, 1, 0}, {0, 0, 0}}, {{1, 0, 1}, {0, 0, 0}}, {{0, 1, 1}, {0, 0, 0}},
  1247. {{0, 0, 0}, {1, 1, 0}}, {{1, 1, 0}, {1, 1, 0}}, {{1, 0, 1}, {1, 1, 0}}, {{0, 1, 1}, {1, 1, 0}},
  1248. {{0, 0, 0}, {1, 0, 1}}, {{1, 1, 0}, {1, 0, 1}}, {{1, 0, 1}, {1, 0, 1}}, {{0, 1, 1}, {1, 0, 1}},
  1249. {{0, 0, 0}, {0, 1, 1}}, {{1, 1, 0}, {0, 1, 1}}, {{1, 0, 1}, {0, 1, 1}}, {{0, 1, 1}, {0, 1, 1}},
  1250. {{1, 0, 0}, {1, 1, 1}}, {{0, 1, 0}, {1, 1, 1}}, {{0, 0, 1}, {1, 1, 1}}, {{1, 1, 1}, {1, 1, 1}},
  1251. {{1, 0, 0}, {0, 0, 1}}, {{0, 1, 0}, {0, 0, 1}}, {{0, 0, 1}, {0, 0, 1}}, {{1, 1, 1}, {0, 0, 1}},
  1252. {{1, 0, 0}, {1, 0, 0}}, {{0, 1, 0}, {1, 0, 0}}, {{0, 0, 1}, {1, 0, 0}}, {{1, 1, 1}, {1, 0, 0}},
  1253. {{1, 0, 0}, {0, 1, 0}}, {{0, 1, 0}, {0, 1, 0}}, {{0, 0, 1}, {0, 1, 0}}, {{1, 1, 1}, {0, 1, 0}}}}, // Dimension = 3
  1254. {
  1255. // Dimension = 4
  1256. {{{0, 0, 0, 0}, {0, 0, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}}},
  1257. // 3*n+1 BCC 3*n+1 Cartesian 3*n //same parity
  1258. {// SAME_PAR
  1259. {{0, 0, 0, 0}, {0, 0, 0, 0}},
  1260. {{1, 1, 1, 1}, {1, 1, 1, 1}}},
  1261. // 3*n+2 BCC 3*n+1 BCC 3*n+1
  1262. {// BCC
  1263. {{0, 0, 0, 0}, {0, 0, 0, 0}},
  1264. {{0, 0, 0, 0}, {1, 1, 1, 1}},
  1265. {{1, 1, 1, 1}, {0, 0, 0, 0}},
  1266. {{1, 1, 1, 1}, {1, 1, 1, 1}}},
  1267. // 3 PBIT
  1268. {{{0, 0, 0, 0}, {0, 0, 0, 0}},
  1269. {{0, 0, 0, 0}, {0, 1, 1, 1}},
  1270. {{0, 1, 1, 1}, {0, 0, 0, 0}},
  1271. {{0, 1, 1, 1}, {0, 1, 1, 1}},
  1272. {{1, 0, 0, 0}, {1, 0, 0, 0}},
  1273. {{1, 0, 0, 0}, {1, 1, 1, 1}},
  1274. {{1, 1, 1, 1}, {1, 0, 0, 0}},
  1275. {{1, 1, 1, 1}, {1, 1, 1, 1}}},
  1276. // 4 PBIT
  1277. {
  1278. {{0, 0, 0, 0}, {0, 0, 0, 0}},
  1279. {{0, 0, 0, 0}, {0, 1, 1, 1}},
  1280. {{0, 1, 1, 1}, {0, 0, 0, 0}},
  1281. {{0, 1, 1, 1}, {0, 1, 1, 1}},
  1282. {{1, 0, 0, 0}, {1, 0, 0, 0}},
  1283. {{1, 0, 0, 0}, {1, 1, 1, 1}},
  1284. {{1, 1, 1, 1}, {1, 0, 0, 0}},
  1285. {{1, 1, 1, 1}, {1, 1, 1, 1}},
  1286. {{0, 0, 0, 0}, {0, 0, 0, 0}},
  1287. {{0, 0, 0, 0}, {0, 0, 1, 1}},
  1288. {{0, 0, 1, 1}, {0, 0, 0, 0}},
  1289. {{0, 1, 0, 1}, {0, 1, 0, 1}},
  1290. {{1, 0, 0, 0}, {1, 0, 0, 0}},
  1291. {{1, 0, 0, 0}, {1, 0, 1, 1}},
  1292. {{1, 0, 1, 1}, {1, 0, 0, 0}},
  1293. {{1, 1, 0, 1}, {1, 1, 0, 1}},
  1294. },
  1295. } // Dimension = 4
  1296. };
  1297. CGU_INT get_par_vector(CGU_INT dim1, CGU_INT dim2, CGU_INT dim3, CGU_INT dim4, CGU_INT dim5)
  1298. {
  1299. return par_vectors_nd[dim1][dim2][dim3][dim4][dim5];
  1300. }
  1301. CGU_FLOAT quant_single_point_d(CGU_FLOAT data[MAX_ENTRIES][MAX_DIMENSION_BIG],
  1302. CGU_INT numEntries,
  1303. CGU_INT index[MAX_ENTRIES],
  1304. CGU_FLOAT out[MAX_ENTRIES][MAX_DIMENSION_BIG],
  1305. CGU_INT epo_1[2][MAX_DIMENSION_BIG],
  1306. CGU_INT Mi_, // last cluster
  1307. CGU_INT type,
  1308. CGU_INT dimension)
  1309. {
  1310. if (dimension < 3)
  1311. return CMP_FLOAT_MAX;
  1312. CGU_INT i, j;
  1313. CGU_FLOAT err_0 = CMP_FLOAT_MAX;
  1314. CGU_FLOAT err_1 = CMP_FLOAT_MAX;
  1315. CGU_INT idx = 0;
  1316. CGU_INT idx_1 = 0;
  1317. CGU_INT epo_0[2][MAX_DIMENSION_BIG];
  1318. CGU_INT use_par = (type != 0);
  1319. CGU_INT clogs = 0;
  1320. i = Mi_ + 1;
  1321. while (i >>= 1)
  1322. clogs++;
  1323. // assert((1<<clogs)== Mi_+1);
  1324. CGU_INT pn;
  1325. for (pn = 0; pn < npv_nd[dimension - 3][type]; pn++)
  1326. {
  1327. //1
  1328. CGU_INT dim1 = dimension - 3;
  1329. CGU_INT dim2 = type;
  1330. CGU_INT dim3 = pn;
  1331. CGU_INT o1[2][MAX_DIMENSION_BIG]; // = { 0,2 };
  1332. CGU_INT o2[2][MAX_DIMENSION_BIG]; // = { 0,2 };
  1333. for (j = 0; j < dimension; j++)
  1334. {
  1335. //A
  1336. o2[0][j] = o1[0][j] = 0;
  1337. o2[1][j] = o1[1][j] = 2;
  1338. if (use_par)
  1339. {
  1340. if (get_par_vector(dim1, dim2, dim3, 0, j))
  1341. o1[0][j] = 1;
  1342. else
  1343. o1[1][j] = 1;
  1344. if (get_par_vector(dim1, dim2, dim3, 1, j))
  1345. o2[0][j] = 1;
  1346. else
  1347. o2[1][j] = 1;
  1348. }
  1349. } //A
  1350. CGU_INT t1, t2;
  1351. CGU_INT dr[MAX_DIMENSION_BIG];
  1352. CGU_INT dr_0[MAX_DIMENSION_BIG];
  1353. //CGU_FLOAT tr;
  1354. for (i = 0; i < (1 << clogs); i++)
  1355. {
  1356. //E
  1357. CGU_FLOAT t = 0;
  1358. CGU_INT t1o[MAX_DIMENSION_BIG], t2o[MAX_DIMENSION_BIG];
  1359. for (j = 0; j < dimension; j++)
  1360. {
  1361. // D
  1362. CGU_FLOAT t_ = CMP_FLOAT_MAX;
  1363. for (t1 = o1[0][j]; t1 < o1[1][j]; t1++)
  1364. {
  1365. // C
  1366. for (t2 = o2[0][j]; t2 < o2[1][j]; t2++)
  1367. // This is needed for non-integer mean points of "collapsed" sets
  1368. {
  1369. // B
  1370. #ifdef USE_BC6RAMPS
  1371. CGU_INT tf = (int)floorf(data[0][j]);
  1372. CGU_INT tc = (int)ceilf(data[0][j]);
  1373. // if they are not equal, the same representalbe point is used for
  1374. // both of them, as all representable points are integers in the rage
  1375. if (sperr(tf, CLT(clogs), BTT(bits[j]), t1, t2, i) > sperr(tc, CLT(clogs), BTT(bits[j]), t1, t2, i))
  1376. dr[j] = tc;
  1377. else if (sperr(tf, CLT(clogs), BTT(bits[j]), t1, t2, i) < sperr(tc, CLT(clogs), BTT(bits[j]), t1, t2, i))
  1378. dr[j] = tf;
  1379. else
  1380. #endif
  1381. dr[j] = (int)floorf(data[0][j] + 0.5f);
  1382. #ifdef USE_BC6RAMPS
  1383. tr = sperr(dr[j], CLT(clogs), BTT(bits[j]), t1, t2, i) +
  1384. 2.0f * sqrtf(sperr(dr[j], CLT(clogs), BTT(bits[j]), t1, t2, i)) * fabsf((float)dr[j] - data[0][j]) +
  1385. (dr[j] - data[0][j]) * (dr[j] - data[0][j]);
  1386. if (tr < t_)
  1387. {
  1388. t_ = tr;
  1389. #else
  1390. t_ = 0;
  1391. #endif
  1392. t1o[j] = t1;
  1393. t2o[j] = t2;
  1394. dr_0[j] = dr[j];
  1395. #ifdef USE_BC6RAMPS
  1396. if ((dr_0[j] < 0) || (dr_0[j] > 255))
  1397. {
  1398. dr_0[j] = 0; // Error!
  1399. }
  1400. }
  1401. #endif
  1402. } // B
  1403. } //C
  1404. t += t_;
  1405. } // D
  1406. if (t < err_0)
  1407. {
  1408. idx = i;
  1409. for (j = 0; j < dimension; j++)
  1410. {
  1411. #ifdef USE_BC6RAMPS
  1412. CGU_INT p1 = CLT(clogs); // < 3
  1413. CGU_INT p2 = BTT(bits[j]); // < 4
  1414. CGU_INT in_data = dr_0[j]; // < SP_ERRIDX_MAX
  1415. CGU_INT p4 = t1o[j]; // < 2
  1416. CGU_INT p5 = t2o[j]; // < 2
  1417. CGU_INT p6 = i; // < 16
  1418. // New spidx
  1419. epo_0[0][j] = spidx(in_data, p1, p2, p4, p5, p6, 0);
  1420. epo_0[1][j] = spidx(in_data, p1, p2, p4, p5, p6, 1);
  1421. if (epo_0[1][j] >= SP_ERRIDX_MAX)
  1422. {
  1423. epo_0[1][j] = 0; // Error!!
  1424. }
  1425. #else
  1426. epo_0[0][j] = 0;
  1427. epo_0[1][j] = 0;
  1428. #endif
  1429. }
  1430. err_0 = t;
  1431. }
  1432. if (err_0 == 0)
  1433. break;
  1434. } // E
  1435. if (err_0 < err_1)
  1436. {
  1437. idx_1 = idx;
  1438. for (j = 0; j < dimension; j++)
  1439. {
  1440. epo_1[0][j] = epo_0[0][j];
  1441. epo_1[1][j] = epo_0[1][j];
  1442. }
  1443. err_1 = err_0;
  1444. }
  1445. if (err_1 == 0)
  1446. break;
  1447. } //1
  1448. for (i = 0; i < numEntries; i++)
  1449. {
  1450. index[i] = idx_1;
  1451. for (j = 0; j < dimension; j++)
  1452. {
  1453. CGU_INT p1 = CLT(clogs); // < 3
  1454. CGU_INT p3 = epo_1[0][j]; // < SP_ERRIDX_MAX
  1455. CGU_INT p4 = epo_1[1][j]; // < SP_ERRIDX_MAX
  1456. CGU_INT p5 = idx_1; // < 16
  1457. #pragma warning(push)
  1458. #pragma warning(disable : 4244)
  1459. out[i][j] = (int)rampf(p1, p3, p4, p5);
  1460. #pragma warning(pop)
  1461. }
  1462. }
  1463. return err_1 * numEntries;
  1464. }
  1465. //========================================================================================================================
  1466. CGU_FLOAT ep_shaker_HD(CGU_FLOAT data[MAX_ENTRIES][MAX_DIMENSION_BIG],
  1467. CGU_INT numEntries,
  1468. CGU_INT index_[MAX_ENTRIES],
  1469. CGU_FLOAT out[MAX_ENTRIES][MAX_DIMENSION_BIG],
  1470. CGU_INT epo_code_out[2][MAX_DIMENSION_BIG],
  1471. CGU_INT Mi_, // last cluster
  1472. CGU_INT bits[3], // including parity
  1473. CGU_INT channels3or4)
  1474. {
  1475. CGU_INT i, j, k;
  1476. CGU_INT use_par = 0;
  1477. CGU_INT clogs = 0;
  1478. i = Mi_ + 1;
  1479. while (i >>= 1)
  1480. clogs++;
  1481. CGU_FLOAT mean[MAX_DIMENSION_BIG];
  1482. CGU_INT index[MAX_ENTRIES];
  1483. CGU_INT Mi;
  1484. CGU_INT maxTry = 1;
  1485. for (k = 0; k < numEntries; k++)
  1486. {
  1487. index[k] = index_[k];
  1488. }
  1489. CGU_INT done;
  1490. CGU_INT change;
  1491. CGU_INT better;
  1492. CGU_FLOAT err_o = CMP_FLOAT_MAX;
  1493. CGU_FLOAT out_2[MAX_ENTRIES][MAX_DIMENSION_BIG];
  1494. CGU_INT idx_2[MAX_ENTRIES];
  1495. CGU_INT epo_2[2][MAX_DIMENSION_BIG];
  1496. CGU_INT max_bits[MAX_DIMENSION_BIG];
  1497. CGU_INT type = bits[0] % (2 * channels3or4);
  1498. for (j = 0; j < channels3or4; j++)
  1499. max_bits[j] = (bits[0] + 2 * channels3or4 - 1) / (2 * channels3or4);
  1500. // handled below automatically
  1501. CGU_INT alls = all_same_d(data, numEntries, channels3or4);
  1502. mean_d_d(data, mean, numEntries, channels3or4);
  1503. do
  1504. {
  1505. index_collapse_kernel(index, numEntries);
  1506. Mi = max_index(index, numEntries); // index can be from requantizer
  1507. CGU_INT p, q;
  1508. CGU_INT p0 = -1, q0 = -1;
  1509. CGU_FLOAT err_2 = CMP_FLOAT_MAX;
  1510. if (Mi == 0)
  1511. {
  1512. CGU_FLOAT t;
  1513. CGU_INT epo_0[2][MAX_DIMENSION_BIG];
  1514. // either sinle point from the beginning or collapsed index
  1515. if (alls)
  1516. {
  1517. t = quant_single_point_d(data, numEntries, index, out_2, epo_0, Mi_, type, channels3or4);
  1518. }
  1519. else
  1520. {
  1521. quant_single_point_d(&mean, numEntries, index, out_2, epo_0, Mi_, type, channels3or4);
  1522. t = totalError_d(data, out_2, numEntries, channels3or4);
  1523. }
  1524. if (t < err_o)
  1525. {
  1526. for (k = 0; k < numEntries; k++)
  1527. {
  1528. index_[k] = index[k];
  1529. for (j = 0; j < channels3or4; j++)
  1530. {
  1531. out[k][j] = out_2[k][j];
  1532. epo_code_out[0][j] = epo_0[0][j];
  1533. epo_code_out[1][j] = epo_0[1][j];
  1534. }
  1535. };
  1536. err_o = t;
  1537. }
  1538. return err_o;
  1539. }
  1540. //===============================
  1541. // We have ramp colors to process
  1542. //===============================
  1543. for (q = 1; Mi != 0 && q * Mi <= Mi_; q++)
  1544. { // does not work for single point collapsed index!!!
  1545. for (p = 0; p <= Mi_ - q * Mi; p++)
  1546. {
  1547. //-------------------------------------
  1548. // set a new index data to try
  1549. //-------------------------------------
  1550. CGU_INT cidx[MAX_ENTRIES];
  1551. for (k = 0; k < numEntries; k++)
  1552. {
  1553. cidx[k] = index[k] * q + p;
  1554. }
  1555. CGU_FLOAT epa[2][MAX_DIMENSION_BIG];
  1556. //
  1557. // solve RMS problem for center
  1558. //
  1559. CGU_FLOAT im[2][2] = {{0, 0}, {0, 0}}; // matrix /inverse matrix
  1560. CGU_FLOAT rp[2][MAX_DIMENSION_BIG]; // right part for RMS fit problem
  1561. // get ideal clustr centers
  1562. CGU_FLOAT cc[MAX_CLUSTERS_BIG][MAX_DIMENSION_BIG];
  1563. CGU_INT index_cnt[MAX_CLUSTERS_BIG]; // count of index entries
  1564. CGU_INT index_comp[MAX_CLUSTERS_BIG]; // compacted index
  1565. CGU_INT index_ncl; // number of unique indexes
  1566. index_ncl = cluster_mean_d_d(data, cc, cidx, index_comp, index_cnt, numEntries, channels3or4); // unrounded
  1567. for (i = 0; i < index_ncl; i++)
  1568. for (j = 0; j < channels3or4; j++)
  1569. cc[index_comp[i]][j] = (CGU_FLOAT)floorf(cc[index_comp[i]][j] + 0.5f); // more or less ideal location
  1570. for (j = 0; j < channels3or4; j++)
  1571. {
  1572. rp[0][j] = rp[1][j] = 0;
  1573. }
  1574. // weight with cnt if runnning on compacted index
  1575. for (k = 0; k < numEntries; k++)
  1576. {
  1577. im[0][0] += (Mi_ - cidx[k]) * (Mi_ - cidx[k]);
  1578. im[0][1] += cidx[k] * (Mi_ - cidx[k]); // im is symmetric
  1579. im[1][1] += cidx[k] * cidx[k];
  1580. for (j = 0; j < channels3or4; j++)
  1581. {
  1582. rp[0][j] += (Mi_ - cidx[k]) * cc[cidx[k]][j];
  1583. rp[1][j] += cidx[k] * cc[cidx[k]][j];
  1584. }
  1585. }
  1586. CGU_FLOAT dd = im[0][0] * im[1][1] - im[0][1] * im[0][1];
  1587. //assert(dd !=0);
  1588. // dd=0 means that cidx[k] and (Mi_-cidx[k]) collinear which implies only one active index;
  1589. // taken care of separately
  1590. im[1][0] = im[0][0];
  1591. im[0][0] = im[1][1] / dd;
  1592. im[1][1] = im[1][0] / dd;
  1593. im[1][0] = im[0][1] = -im[0][1] / dd;
  1594. for (j = 0; j < channels3or4; j++)
  1595. {
  1596. epa[0][j] = (im[0][0] * rp[0][j] + im[0][1] * rp[1][j]) * Mi_;
  1597. epa[1][j] = (im[1][0] * rp[0][j] + im[1][1] * rp[1][j]) * Mi_;
  1598. }
  1599. CGU_FLOAT err_1 = CMP_FLOAT_MAX;
  1600. CGU_FLOAT out_1[MAX_ENTRIES][MAX_DIMENSION_BIG];
  1601. CGU_INT idx_1[MAX_ENTRIES];
  1602. CGU_INT epo_1[2][MAX_DIMENSION_BIG];
  1603. CGU_INT s1 = 0;
  1604. CGU_FLOAT epd[2][MAX_DIMENSION_BIG][2]; // first second, coord, begin range end range
  1605. for (j = 0; j < channels3or4; j++)
  1606. {
  1607. for (i = 0; i < 2; i++)
  1608. {
  1609. // set range
  1610. epd[i][j][0] = epd[i][j][1] = epa[i][j];
  1611. epd[i][j][1] +=
  1612. ((1 << bits[j]) - 1 - (int)epd[i][j][1] < (1 << use_par) ? (1 << bits[j]) - 1 - (int)epd[i][j][1] : (1 << use_par)) & (~use_par);
  1613. }
  1614. }
  1615. CGU_FLOAT ce[MAX_ENTRIES][MAX_CLUSTERS_BIG][MAX_DIMENSION_BIG];
  1616. CGU_FLOAT err_0 = 0;
  1617. CGU_FLOAT out_0[MAX_ENTRIES][MAX_DIMENSION_BIG];
  1618. CGU_INT idx_0[MAX_ENTRIES];
  1619. for (i = 0; i < numEntries; i++)
  1620. {
  1621. CGU_FLOAT d[4];
  1622. d[0] = data[i][0];
  1623. d[1] = data[i][1];
  1624. d[2] = data[i][2];
  1625. d[3] = data[i][3];
  1626. for (j = 0; j < (1 << clogs); j++)
  1627. for (k = 0; k < channels3or4; k++)
  1628. {
  1629. ce[i][j][k] = (rampf(CLT(clogs), epd[0][k][0], epd[1][k][0], j) - d[k]) * (rampf(CLT(clogs), epd[0][k][0], epd[1][k][0], j) - d[k]);
  1630. }
  1631. }
  1632. CGU_INT s = 0, p1, g;
  1633. CGU_INT ei0 = 0, ei1 = 0;
  1634. for (p1 = 0; p1 < 64; p1++)
  1635. {
  1636. CGU_INT j0 = 0;
  1637. // Gray code increment
  1638. g = p1 & (-p1);
  1639. err_0 = 0;
  1640. for (j = 0; j < channels3or4; j++)
  1641. {
  1642. if (((g >> (2 * j)) & 0x3) != 0)
  1643. {
  1644. j0 = j;
  1645. // new cords
  1646. ei0 = (((s ^ g) >> (2 * j)) & 0x1);
  1647. ei1 = (((s ^ g) >> (2 * j + 1)) & 0x1);
  1648. }
  1649. }
  1650. s = s ^ g;
  1651. err_0 = 0;
  1652. for (i = 0; i < numEntries; i++)
  1653. {
  1654. CGU_FLOAT d[4];
  1655. d[0] = data[i][0];
  1656. d[1] = data[i][1];
  1657. d[2] = data[i][2];
  1658. d[3] = data[i][3];
  1659. CGU_INT ci = 0;
  1660. CGU_FLOAT cmin = CMP_FLOAT_MAX;
  1661. for (j = 0; j < (1 << clogs); j++)
  1662. {
  1663. float t_ = 0.;
  1664. ce[i][j][j0] = (rampf(CLT(clogs), epd[0][j0][ei0], epd[1][j0][ei1], j) - d[j0]) *
  1665. (rampf(CLT(clogs), epd[0][j0][ei0], epd[1][j0][ei1], j) - d[j0]);
  1666. for (k = 0; k < channels3or4; k++)
  1667. {
  1668. t_ += ce[i][j][k];
  1669. }
  1670. if (t_ < cmin)
  1671. {
  1672. cmin = t_;
  1673. ci = j;
  1674. }
  1675. }
  1676. idx_0[i] = ci;
  1677. for (k = 0; k < channels3or4; k++)
  1678. {
  1679. out_0[i][k] = rampf(CLT(clogs), epd[0][k][ei0], epd[1][k][ei1], ci);
  1680. }
  1681. err_0 += cmin;
  1682. }
  1683. if (err_0 < err_1)
  1684. {
  1685. // best in the curent ep cube run
  1686. for (i = 0; i < numEntries; i++)
  1687. {
  1688. idx_1[i] = idx_0[i];
  1689. for (j = 0; j < channels3or4; j++)
  1690. out_1[i][j] = out_0[i][j];
  1691. }
  1692. err_1 = err_0;
  1693. s1 = s; // epo coding
  1694. }
  1695. }
  1696. // reconstruct epo
  1697. for (j = 0; j < channels3or4; j++)
  1698. {
  1699. {
  1700. // new cords
  1701. ei0 = ((s1 >> (2 * j)) & 0x1);
  1702. ei1 = ((s1 >> (2 * j + 1)) & 0x1);
  1703. epo_1[0][j] = (int)epd[0][j][ei0];
  1704. epo_1[1][j] = (int)epd[1][j][ei1];
  1705. }
  1706. }
  1707. if (err_1 < err_2)
  1708. {
  1709. // best in the curent ep cube run
  1710. for (i = 0; i < numEntries; i++)
  1711. {
  1712. idx_2[i] = idx_1[i];
  1713. for (j = 0; j < channels3or4; j++)
  1714. out_2[i][j] = out_1[i][j];
  1715. }
  1716. err_2 = err_1;
  1717. for (j = 0; j < channels3or4; j++)
  1718. {
  1719. epo_2[0][j] = epo_1[0][j];
  1720. epo_2[1][j] = epo_1[1][j];
  1721. }
  1722. p0 = p;
  1723. q0 = q;
  1724. }
  1725. }
  1726. }
  1727. // change/better
  1728. change = 0;
  1729. for (k = 0; k < numEntries; k++)
  1730. change = change || (index[k] * q0 + p0 != idx_2[k]);
  1731. better = err_2 < err_o;
  1732. if (better)
  1733. {
  1734. for (k = 0; k < numEntries; k++)
  1735. {
  1736. index_[k] = index[k] = idx_2[k];
  1737. for (j = 0; j < channels3or4; j++)
  1738. {
  1739. out[k][j] = out_2[k][j];
  1740. epo_code_out[0][j] = epo_2[0][j];
  1741. epo_code_out[1][j] = epo_2[1][j];
  1742. }
  1743. }
  1744. err_o = err_2;
  1745. }
  1746. done = !(change && better);
  1747. if (maxTry > 0)
  1748. maxTry--;
  1749. else
  1750. maxTry = 0;
  1751. } while (!done && maxTry);
  1752. return err_o;
  1753. }
  1754. #ifndef ASPM_GPU
  1755. static CGU_INT g_aWeights3[] = {0, 9, 18, 27, 37, 46, 55, 64}; // 3 bit color Indices
  1756. static CGU_INT g_aWeights4[] = {0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64}; // 4 bit color indices
  1757. CGU_FLOAT lerpf(CGU_FLOAT a, CGU_FLOAT b, CGU_INT i, CGU_INT denom)
  1758. {
  1759. assert(denom == 3 || denom == 7 || denom == 15);
  1760. assert(i >= 0 && i <= denom);
  1761. CGU_INT* weights = NULL;
  1762. switch (denom)
  1763. {
  1764. case 3:
  1765. denom *= 5;
  1766. i *= 5; // fall through to case 15
  1767. case 7:
  1768. weights = g_aWeights3;
  1769. break;
  1770. case 15:
  1771. weights = g_aWeights4;
  1772. break;
  1773. default:
  1774. assert(0);
  1775. }
  1776. return (a * weights[denom - i] + b * weights[i]) / 64.0f;
  1777. }
  1778. #else
  1779. CGU_FLOAT lerpf(CGU_FLOAT a, CGU_FLOAT b, CGU_INT i, CGU_INT denom)
  1780. {
  1781. CGU_INT g_aWeights3[] = {0, 9, 18, 27, 37, 46, 55, 64}; // 3 bit color Indices
  1782. CGU_INT g_aWeights4[] = {0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64}; // 4 bit color indices
  1783. switch (denom)
  1784. {
  1785. case 7:
  1786. return ((a * g_aWeights3[denom - i] + b * g_aWeights3[i]) / 64.0f);
  1787. break;
  1788. case 15:
  1789. return ((a * g_aWeights4[denom - i] + b * g_aWeights4[i]) / 64.0f);
  1790. break;
  1791. default:
  1792. case 3: // fall through to case 15
  1793. denom *= 5;
  1794. i *= 5;
  1795. return ((a * g_aWeights3[denom - i] + b * g_aWeights3[i]) / 64.0f);
  1796. break;
  1797. }
  1798. }
  1799. #endif
  1800. void palitizeEndPointsF(BC6H_Encode_local* BC6H_data, CGU_FLOAT fEndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG])
  1801. {
  1802. // scale endpoints
  1803. CGU_FLOAT Ar, Ag, Ab, Br, Bg, Bb;
  1804. // Compose index colors from end points
  1805. if (BC6H_data->region == 1)
  1806. {
  1807. Ar = fEndPoints[0][0][0];
  1808. Ag = fEndPoints[0][0][1];
  1809. Ab = fEndPoints[0][0][2];
  1810. Br = fEndPoints[0][1][0];
  1811. Bg = fEndPoints[0][1][1];
  1812. Bb = fEndPoints[0][1][2];
  1813. for (CGU_INT i = 0; i < 16; i++)
  1814. {
  1815. // Red
  1816. BC6H_data->Paletef[0][i].x = lerpf(Ar, Br, i, 15);
  1817. // Green
  1818. BC6H_data->Paletef[0][i].y = lerpf(Ag, Bg, i, 15);
  1819. // Blue
  1820. BC6H_data->Paletef[0][i].z = lerpf(Ab, Bb, i, 15);
  1821. }
  1822. }
  1823. else
  1824. { //mode.type == BC6_TWO
  1825. for (CGU_INT region = 0; region < 2; region++)
  1826. {
  1827. Ar = fEndPoints[region][0][0];
  1828. Ag = fEndPoints[region][0][1];
  1829. Ab = fEndPoints[region][0][2];
  1830. Br = fEndPoints[region][1][0];
  1831. Bg = fEndPoints[region][1][1];
  1832. Bb = fEndPoints[region][1][2];
  1833. for (CGU_INT i = 0; i < 8; i++)
  1834. {
  1835. // Red
  1836. BC6H_data->Paletef[region][i].x = lerpf(Ar, Br, i, 7);
  1837. // Greed
  1838. BC6H_data->Paletef[region][i].y = lerpf(Ag, Bg, i, 7);
  1839. // Blue
  1840. BC6H_data->Paletef[region][i].z = lerpf(Ab, Bb, i, 7);
  1841. }
  1842. }
  1843. }
  1844. }
  1845. CGU_FLOAT CalcShapeError(BC6H_Encode_local* BC6H_data, CGU_FLOAT fEndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG], CGU_BOOL SkipPallet)
  1846. {
  1847. CGU_INT maxPallet;
  1848. CGU_INT subset = 0;
  1849. CGU_FLOAT totalError = 0.0f;
  1850. CGU_INT region = (BC6H_data->region - 1);
  1851. if (region == 0)
  1852. maxPallet = 16;
  1853. else
  1854. maxPallet = 8;
  1855. if (!SkipPallet)
  1856. palitizeEndPointsF(BC6H_data, fEndPoints);
  1857. for (CGU_INT i = 0; i < MAX_SUBSET_SIZE; i++)
  1858. {
  1859. CGU_FLOAT error = 0.0f;
  1860. CGU_FLOAT bestError = 0.0f;
  1861. if (region == 0)
  1862. {
  1863. subset = 0;
  1864. }
  1865. else
  1866. {
  1867. // get the shape subset 0 or 1
  1868. subset = BC6_PARTITIONS[BC6H_data->d_shape_index][i];
  1869. }
  1870. // initialize bestError to the difference for first data
  1871. bestError = abs(BC6H_data->din[i][0] - BC6H_data->Paletef[subset][0].x) + abs(BC6H_data->din[i][1] - BC6H_data->Paletef[subset][0].y) +
  1872. abs(BC6H_data->din[i][2] - BC6H_data->Paletef[subset][0].z);
  1873. // loop through the rest of the data until find the best error
  1874. for (CGU_INT j = 1; j < maxPallet && bestError > 0; j++)
  1875. {
  1876. error = abs(BC6H_data->din[i][0] - BC6H_data->Paletef[subset][j].x) + abs(BC6H_data->din[i][1] - BC6H_data->Paletef[subset][j].y) +
  1877. abs(BC6H_data->din[i][2] - BC6H_data->Paletef[subset][j].z);
  1878. if (error <= bestError)
  1879. bestError = error;
  1880. else
  1881. break;
  1882. }
  1883. totalError += bestError;
  1884. }
  1885. return totalError;
  1886. }
  1887. CGU_FLOAT FindBestPattern(BC6H_Encode_local* BC6H_data, CGU_BOOL TwoRegionShapes, CGU_INT8 shape_pattern, CGU_FLOAT quality)
  1888. {
  1889. // Index bit size for the patterns been used.
  1890. // All two zone shapes have 3 bits per color, max index value < 8
  1891. // All one zone shapes gave 4 bits per color, max index value < 16
  1892. CGU_INT8 Index_BitSize = TwoRegionShapes ? 8 : 16;
  1893. CGU_INT8 max_subsets = TwoRegionShapes ? 2 : 1;
  1894. CGU_FLOAT direction[NCHANNELS];
  1895. CGU_FLOAT step;
  1896. BC6H_data->region = max_subsets;
  1897. BC6H_data->index = 0;
  1898. BC6H_data->d_shape_index = shape_pattern;
  1899. memset((CGU_UINT8*)BC6H_data->partition, 0, sizeof(BC6H_data->partition));
  1900. memset((CGU_UINT8*)BC6H_data->shape_indices, 0, sizeof(BC6H_data->shape_indices));
  1901. // Get the pattern to encode with
  1902. Partition(shape_pattern, // Shape pattern we want to get
  1903. BC6H_data->din, // Input data
  1904. BC6H_data->partition, // Returns the patterned shape data
  1905. BC6H_data->entryCount, // counts the number of pixel used in each subset region num of 0's amd 1's
  1906. max_subsets, // Table Shapes to use eithe one regions 1 or two regions 2
  1907. 3); // rgb no alpha always = 3
  1908. CGU_FLOAT error[MAX_SUBSETS] = {0.0, CMP_FLOAT_MAX, CMP_FLOAT_MAX};
  1909. CGU_INT BestOutB = 0;
  1910. CGU_FLOAT BestError; //the lowest error from vector direction quantization
  1911. CGU_FLOAT BestError_endpts; //the lowest error from endpoints extracted from the vector direction quantization
  1912. CGU_FLOAT outB[2][2][MAX_SUBSET_SIZE][MAX_DIMENSION_BIG];
  1913. CGU_INT shape_indicesB[2][MAX_SUBSETS][MAX_SUBSET_SIZE];
  1914. for (CGU_INT subset = 0; subset < max_subsets; subset++)
  1915. {
  1916. error[0] += optQuantAnD_d(BC6H_data->partition[subset], // input data
  1917. BC6H_data->entryCount[subset], // number of input points above (not clear about 1, better to avoid)
  1918. Index_BitSize, // number of clusters on the ramp, 8 or 16
  1919. shape_indicesB[0][subset], // output index, if not all points of the ramp used, 0 may not be assigned
  1920. outB[0][subset], // resulting quantization
  1921. direction, // direction vector of the ramp (check normalization)
  1922. &step, // step size (check normalization)
  1923. 3, // number of channels (always 3 = RGB for BC6H)
  1924. quality // Quality set number of retry to get good end points
  1925. // Max retries = MAX_TRY = 4000 when Quality is 1.0
  1926. // Min = 0 and default with quality 0.05 is 200 times
  1927. );
  1928. }
  1929. BestError = error[0];
  1930. BestOutB = 0;
  1931. // The following code is almost complete - runs very slow and not sure if % of improvement is justified..
  1932. #ifdef USE_SHAKERHD
  1933. // Valid only for 2 region shapes
  1934. if ((max_subsets > 1) && (quality > 0.80))
  1935. {
  1936. CGU_INT tempIndices[MAX_SUBSET_SIZE];
  1937. // CGU_INT temp_epo_code[2][2][MAX_DIMENSION_BIG];
  1938. CGU_INT bits[3] = {8, 8, 8}; // Channel index bit size
  1939. // CGU_FLOAT epo[2][MAX_DIMENSION_BIG];
  1940. CGU_INT epo_code[MAX_SUBSETS][2][MAX_DIMENSION_BIG];
  1941. // CGU_INT shakeSize = 8;
  1942. error[1] = 0.0;
  1943. for (CGU_INT subset = 0; subset < max_subsets; subset++)
  1944. {
  1945. for (CGU_INT k = 0; k < BC6H_data->entryCount[subset]; k++)
  1946. {
  1947. tempIndices[k] = shape_indicesB[0][subset][k];
  1948. }
  1949. error[1] += ep_shaker_HD(BC6H_data->partition[subset],
  1950. BC6H_data->entryCount[subset],
  1951. tempIndices, // output index, if not all points of the ramp used, 0 may not be assigned
  1952. outB[1][subset], // resulting quantization
  1953. epo_code[subset],
  1954. BC6H_data->entryCount[subset] - 1,
  1955. bits,
  1956. 3);
  1957. // error[1] += ep_shaker_2_d(
  1958. // BC6H_data.partition[subset],
  1959. // BC6H_data.entryCount[subset],
  1960. // tempIndices, // output index, if not all points of the ramp used, 0 may not be assigned
  1961. // outB[1][subset], // resulting quantization
  1962. // epo_code[subset],
  1963. // shakeSize,
  1964. // BC6H_data.entryCount[subset] - 1,
  1965. // bits[0],
  1966. // 3,
  1967. // epo
  1968. // );
  1969. for (CGU_INT k = 0; k < BC6H_data->entryCount[subset]; k++)
  1970. {
  1971. shape_indicesB[1][subset][k] = tempIndices[k];
  1972. }
  1973. } // subsets
  1974. if (BestError > error[1])
  1975. {
  1976. BestError = error[1];
  1977. BestOutB = 1;
  1978. for (CGU_INT subset = 0; subset < max_subsets; subset++)
  1979. {
  1980. for (CGU_INT k = 0; k < MAX_DIMENSION_BIG; k++)
  1981. {
  1982. BC6H_data->fEndPoints[subset][0][k] = (CGU_FLOAT)epo_code[subset][0][k];
  1983. BC6H_data->fEndPoints[subset][1][k] = (CGU_FLOAT)epo_code[subset][1][k];
  1984. }
  1985. }
  1986. }
  1987. }
  1988. #endif
  1989. // Save the best for BC6H data processing later
  1990. if (BestOutB == 0)
  1991. GetEndPoints(BC6H_data->fEndPoints, outB[BestOutB], max_subsets, BC6H_data->entryCount);
  1992. memcpy((CGU_UINT8*)BC6H_data->shape_indices, (CGU_UINT8*)shape_indicesB[BestOutB], sizeof(BC6H_data->shape_indices));
  1993. clampF16Max(BC6H_data->fEndPoints, BC6H_data->issigned);
  1994. BestError_endpts = CalcShapeError(BC6H_data, BC6H_data->fEndPoints, false);
  1995. return BestError_endpts;
  1996. }
  1997. #ifndef ASPM_GPU
  1998. void SaveDataBlock(BC6H_Encode_local* bc6h_format, CMP_GLOBAL CGU_UINT8 cmpout[COMPRESSED_BLOCK_SIZE])
  1999. {
  2000. BitHeader header(NULL, COMPRESSED_BLOCK_SIZE);
  2001. // Save the RGB end point values
  2002. switch (bc6h_format->m_mode)
  2003. {
  2004. case 1: //0x00
  2005. header.setvalue(0, 2, 0x00);
  2006. header.setvalue(2, 1, bc6h_format->gy, 4); // gy[4]
  2007. header.setvalue(3, 1, bc6h_format->by, 4); // by[4]
  2008. header.setvalue(4, 1, bc6h_format->bz, 4); // bz[4]
  2009. header.setvalue(5, 10, bc6h_format->rw); // 10: rw[9:0]
  2010. header.setvalue(15, 10, bc6h_format->gw); // 10: gw[9:0]
  2011. header.setvalue(25, 10, bc6h_format->bw); // 10: bw[9:0]
  2012. header.setvalue(35, 5, bc6h_format->rx); // 5: rx[4:0]
  2013. header.setvalue(40, 1, bc6h_format->gz, 4); // gz[4]
  2014. header.setvalue(41, 4, bc6h_format->gy); // 5: gy[3:0]
  2015. header.setvalue(45, 5, bc6h_format->gx); // 5: gx[4:0]
  2016. header.setvalue(50, 1, bc6h_format->bz); // 5: bz[0]
  2017. header.setvalue(51, 4, bc6h_format->gz); // 5: gz[3:0]
  2018. header.setvalue(55, 5, bc6h_format->bx); // 5: bx[4:0]
  2019. header.setvalue(60, 1, bc6h_format->bz, 1); // bz[1]
  2020. header.setvalue(61, 4, bc6h_format->by); // 5: by[3:0]
  2021. header.setvalue(65, 5, bc6h_format->ry); // 5: ry[4:0]
  2022. header.setvalue(70, 1, bc6h_format->bz, 2); // bz[2]
  2023. header.setvalue(71, 5, bc6h_format->rz); // 5: rz[4:0]
  2024. header.setvalue(76, 1, bc6h_format->bz, 3); // bz[3]
  2025. break;
  2026. case 2: // 0x01
  2027. header.setvalue(0, 2, 0x01);
  2028. header.setvalue(2, 1, bc6h_format->gy, 5); // gy[5]
  2029. header.setvalue(3, 1, bc6h_format->gz, 4); // gz[4]
  2030. header.setvalue(4, 1, bc6h_format->gz, 5); // gz[5]
  2031. header.setvalue(5, 7, bc6h_format->rw); // rw[6:0]
  2032. header.setvalue(12, 1, bc6h_format->bz); // bz[0]
  2033. header.setvalue(13, 1, bc6h_format->bz, 1); // bz[1]
  2034. header.setvalue(14, 1, bc6h_format->by, 4); // by[4]
  2035. header.setvalue(15, 7, bc6h_format->gw); // gw[6:0]
  2036. header.setvalue(22, 1, bc6h_format->by, 5); // by[5]
  2037. header.setvalue(23, 1, bc6h_format->bz, 2); // bz[2]
  2038. header.setvalue(24, 1, bc6h_format->gy, 4); // gy[4]
  2039. header.setvalue(25, 7, bc6h_format->bw); // 7: bw[6:0]
  2040. header.setvalue(32, 1, bc6h_format->bz, 3); // bz[3]
  2041. header.setvalue(33, 1, bc6h_format->bz, 5); // bz[5]
  2042. header.setvalue(34, 1, bc6h_format->bz, 4); // bz[4]
  2043. header.setvalue(35, 6, bc6h_format->rx); // 6: rx[5:0]
  2044. header.setvalue(41, 4, bc6h_format->gy); // 6: gy[3:0]
  2045. header.setvalue(45, 6, bc6h_format->gx); // 6: gx[5:0]
  2046. header.setvalue(51, 4, bc6h_format->gz); // 6: gz[3:0]
  2047. header.setvalue(55, 6, bc6h_format->bx); // 6: bx[5:0]
  2048. header.setvalue(61, 4, bc6h_format->by); // 6: by[3:0]
  2049. header.setvalue(65, 6, bc6h_format->ry); // 6: ry[5:0]
  2050. header.setvalue(71, 6, bc6h_format->rz); // 6: rz[5:0]
  2051. break;
  2052. case 3: // 0x02
  2053. header.setvalue(0, 5, 0x02);
  2054. header.setvalue(5, 10, bc6h_format->rw); // 11: rw[9:0]
  2055. header.setvalue(15, 10, bc6h_format->gw); // 11: gw[9:0]
  2056. header.setvalue(25, 10, bc6h_format->bw); // 11: bw[9:0]
  2057. header.setvalue(35, 5, bc6h_format->rx); // 5: rx[4:0]
  2058. header.setvalue(40, 1, bc6h_format->rw, 10); // rw[10]
  2059. header.setvalue(41, 4, bc6h_format->gy); // 4: gy[3:0]
  2060. header.setvalue(45, 4, bc6h_format->gx); // 4: gx[3:0]
  2061. header.setvalue(49, 1, bc6h_format->gw, 10); // gw[10]
  2062. header.setvalue(50, 1, bc6h_format->bz); // 4: bz[0]
  2063. header.setvalue(51, 4, bc6h_format->gz); // 4: gz[3:0]
  2064. header.setvalue(55, 4, bc6h_format->bx); // 4: bx[3:0]
  2065. header.setvalue(59, 1, bc6h_format->bw, 10); // bw[10]
  2066. header.setvalue(60, 1, bc6h_format->bz, 1); // bz[1]
  2067. header.setvalue(61, 4, bc6h_format->by); // 4: by[3:0]
  2068. header.setvalue(65, 5, bc6h_format->ry); // 5: ry[4:0]
  2069. header.setvalue(70, 1, bc6h_format->bz, 2); // bz[2]
  2070. header.setvalue(71, 5, bc6h_format->rz); // 5: rz[4:0]
  2071. header.setvalue(76, 1, bc6h_format->bz, 3); // bz[3]
  2072. break;
  2073. case 4: // 0x06
  2074. header.setvalue(0, 5, 0x06);
  2075. header.setvalue(5, 10, bc6h_format->rw); // 11: rw[9:0]
  2076. header.setvalue(15, 10, bc6h_format->gw); // 11: gw[9:0]
  2077. header.setvalue(25, 10, bc6h_format->bw); // 11: bw[9:0]
  2078. header.setvalue(35, 4, bc6h_format->rx); // rx[3:0]
  2079. header.setvalue(39, 1, bc6h_format->rw, 10); // rw[10]
  2080. header.setvalue(40, 1, bc6h_format->gz, 4); // gz[4]
  2081. header.setvalue(41, 4, bc6h_format->gy); // 5: gy[3:0]
  2082. header.setvalue(45, 5, bc6h_format->gx); // gx[4:0]
  2083. header.setvalue(50, 1, bc6h_format->gw, 10); // 5: gw[10]
  2084. header.setvalue(51, 4, bc6h_format->gz); // 5: gz[3:0]
  2085. header.setvalue(55, 4, bc6h_format->bx); // 4: bx[3:0]
  2086. header.setvalue(59, 1, bc6h_format->bw, 10); // bw[10]
  2087. header.setvalue(60, 1, bc6h_format->bz, 1); // bz[1]
  2088. header.setvalue(61, 4, bc6h_format->by); // 4: by[3:0]
  2089. header.setvalue(65, 4, bc6h_format->ry); // 4: ry[3:0]
  2090. header.setvalue(69, 1, bc6h_format->bz); // 4: bz[0]
  2091. header.setvalue(70, 1, bc6h_format->bz, 2); // bz[2]
  2092. header.setvalue(71, 4, bc6h_format->rz); // 4: rz[3:0]
  2093. header.setvalue(75, 1, bc6h_format->gy, 4); // gy[4]
  2094. header.setvalue(76, 1, bc6h_format->bz, 3); // bz[3]
  2095. break;
  2096. case 5: // 0x0A
  2097. header.setvalue(0, 5, 0x0A);
  2098. header.setvalue(5, 10, bc6h_format->rw); // 11: rw[9:0]
  2099. header.setvalue(15, 10, bc6h_format->gw); // 11: gw[9:0]
  2100. header.setvalue(25, 10, bc6h_format->bw); // 11: bw[9:0]
  2101. header.setvalue(35, 4, bc6h_format->rx); // 4: rx[3:0]
  2102. header.setvalue(39, 1, bc6h_format->rw, 10); // rw[10]
  2103. header.setvalue(40, 1, bc6h_format->by, 4); // by[4]
  2104. header.setvalue(41, 4, bc6h_format->gy); // 4: gy[3:0]
  2105. header.setvalue(45, 4, bc6h_format->gx); // 4: gx[3:0]
  2106. header.setvalue(49, 1, bc6h_format->gw, 10); // gw[10]
  2107. header.setvalue(50, 1, bc6h_format->bz); // 5: bz[0]
  2108. header.setvalue(51, 4, bc6h_format->gz); // 4: gz[3:0]
  2109. header.setvalue(55, 5, bc6h_format->bx); // 5: bx[4:0]
  2110. header.setvalue(60, 1, bc6h_format->bw, 10); // bw[10]
  2111. header.setvalue(61, 4, bc6h_format->by); // 5: by[3:0]
  2112. header.setvalue(65, 4, bc6h_format->ry); // 4: ry[3:0]
  2113. header.setvalue(69, 1, bc6h_format->bz, 1); // bz[1]
  2114. header.setvalue(70, 1, bc6h_format->bz, 2); // bz[2]
  2115. header.setvalue(71, 4, bc6h_format->rz); // 4: rz[3:0]
  2116. header.setvalue(75, 1, bc6h_format->bz, 4); // bz[4]
  2117. header.setvalue(76, 1, bc6h_format->bz, 3); // bz[3]
  2118. break;
  2119. case 6: // 0x0E
  2120. header.setvalue(0, 5, 0x0E);
  2121. header.setvalue(5, 9, bc6h_format->rw); // 9: rw[8:0]
  2122. header.setvalue(14, 1, bc6h_format->by, 4); // by[4]
  2123. header.setvalue(15, 9, bc6h_format->gw); // 9: gw[8:0]
  2124. header.setvalue(24, 1, bc6h_format->gy, 4); // gy[4]
  2125. header.setvalue(25, 9, bc6h_format->bw); // 9: bw[8:0]
  2126. header.setvalue(34, 1, bc6h_format->bz, 4); // bz[4]
  2127. header.setvalue(35, 5, bc6h_format->rx); // 5: rx[4:0]
  2128. header.setvalue(40, 1, bc6h_format->gz, 4); // gz[4]
  2129. header.setvalue(41, 4, bc6h_format->gy); // 5: gy[3:0]
  2130. header.setvalue(45, 5, bc6h_format->gx); // 5: gx[4:0]
  2131. header.setvalue(50, 1, bc6h_format->bz); // 5: bz[0]
  2132. header.setvalue(51, 4, bc6h_format->gz); // 5: gz[3:0]
  2133. header.setvalue(55, 5, bc6h_format->bx); // 5: bx[4:0]
  2134. header.setvalue(60, 1, bc6h_format->bz, 1); // bz[1]
  2135. header.setvalue(61, 4, bc6h_format->by); // 5: by[3:0]
  2136. header.setvalue(65, 5, bc6h_format->ry); // 5: ry[4:0]
  2137. header.setvalue(70, 1, bc6h_format->bz, 2); // bz[2]
  2138. header.setvalue(71, 5, bc6h_format->rz); // 5: rz[4:0]
  2139. header.setvalue(76, 1, bc6h_format->bz, 3); // bz[3]
  2140. break;
  2141. case 7: // 0x12
  2142. header.setvalue(0, 5, 0x12);
  2143. header.setvalue(5, 8, bc6h_format->rw); // 8: rw[7:0]
  2144. header.setvalue(13, 1, bc6h_format->gz, 4); // gz[4]
  2145. header.setvalue(14, 1, bc6h_format->by, 4); // by[4]
  2146. header.setvalue(15, 8, bc6h_format->gw); // 8: gw[7:0]
  2147. header.setvalue(23, 1, bc6h_format->bz, 2); // bz[2]
  2148. header.setvalue(24, 1, bc6h_format->gy, 4); // gy[4]
  2149. header.setvalue(25, 8, bc6h_format->bw); // 8: bw[7:0]
  2150. header.setvalue(33, 1, bc6h_format->bz, 3); // bz[3]
  2151. header.setvalue(34, 1, bc6h_format->bz, 4); // bz[4]
  2152. header.setvalue(35, 6, bc6h_format->rx); // 6: rx[5:0]
  2153. header.setvalue(41, 4, bc6h_format->gy); // 5: gy[3:0]
  2154. header.setvalue(45, 5, bc6h_format->gx); // 5: gx[4:0]
  2155. header.setvalue(50, 1, bc6h_format->bz); // 5: bz[0]
  2156. header.setvalue(51, 4, bc6h_format->gz); // 5: gz[3:0]
  2157. header.setvalue(55, 5, bc6h_format->bx); // 5: bx[4:0]
  2158. header.setvalue(60, 1, bc6h_format->bz, 1); // bz[1]
  2159. header.setvalue(61, 4, bc6h_format->by); // 5: by[3:0]
  2160. header.setvalue(65, 6, bc6h_format->ry); // 6: ry[5:0]
  2161. header.setvalue(71, 6, bc6h_format->rz); // 6: rz[5:0]
  2162. break;
  2163. case 8: // 0x16
  2164. header.setvalue(0, 5, 0x16);
  2165. header.setvalue(5, 8, bc6h_format->rw); // 8: rw[7:0]
  2166. header.setvalue(13, 1, bc6h_format->bz); // 5: bz[0]
  2167. header.setvalue(14, 1, bc6h_format->by, 4); // by[4]
  2168. header.setvalue(15, 8, bc6h_format->gw); // 8: gw[7:0]
  2169. header.setvalue(23, 1, bc6h_format->gy, 5); // gy[5]
  2170. header.setvalue(24, 1, bc6h_format->gy, 4); // gy[4]
  2171. header.setvalue(25, 8, bc6h_format->bw); // 8: bw[7:0]
  2172. header.setvalue(33, 1, bc6h_format->gz, 5); // gz[5]
  2173. header.setvalue(34, 1, bc6h_format->bz, 4); // bz[4]
  2174. header.setvalue(35, 5, bc6h_format->rx); // 5: rx[4:0]
  2175. header.setvalue(40, 1, bc6h_format->gz, 4); // gz[4]
  2176. header.setvalue(41, 4, bc6h_format->gy); // 6: gy[3:0]
  2177. header.setvalue(45, 6, bc6h_format->gx); // 6: gx[5:0]
  2178. header.setvalue(51, 4, bc6h_format->gz); // 6: gz[3:0]
  2179. header.setvalue(55, 5, bc6h_format->bx); // 5: bx[4:0]
  2180. header.setvalue(60, 1, bc6h_format->bz, 1); // bz[1]
  2181. header.setvalue(61, 4, bc6h_format->by); // 5: by[3:0]
  2182. header.setvalue(65, 5, bc6h_format->ry); // 5: ry[4:0]
  2183. header.setvalue(70, 1, bc6h_format->bz, 2); // bz[2]
  2184. header.setvalue(71, 5, bc6h_format->rz); // 5: rz[4:0]
  2185. header.setvalue(76, 1, bc6h_format->bz, 3); // bz[3]
  2186. break;
  2187. case 9: // 0x1A
  2188. header.setvalue(0, 5, 0x1A);
  2189. header.setvalue(5, 8, bc6h_format->rw); // 8: rw[7:0]
  2190. header.setvalue(13, 1, bc6h_format->bz, 1); // bz[1]
  2191. header.setvalue(14, 1, bc6h_format->by, 4); // by[4]
  2192. header.setvalue(15, 8, bc6h_format->gw); // 8: gw[7:0]
  2193. header.setvalue(23, 1, bc6h_format->by, 5); // by[5]
  2194. header.setvalue(24, 1, bc6h_format->gy, 4); // gy[4]
  2195. header.setvalue(25, 8, bc6h_format->bw); // 8: bw[7:0]
  2196. header.setvalue(33, 1, bc6h_format->bz, 5); // bz[5]
  2197. header.setvalue(34, 1, bc6h_format->bz, 4); // bz[4]
  2198. header.setvalue(35, 5, bc6h_format->rx); // 5: rx[4:0]
  2199. header.setvalue(40, 1, bc6h_format->gz, 4); // gz[4]
  2200. header.setvalue(41, 4, bc6h_format->gy); // 5: gy[3:0]
  2201. header.setvalue(45, 5, bc6h_format->gx); // 5: gx[4:0]
  2202. header.setvalue(50, 1, bc6h_format->bz); // 6: bz[0]
  2203. header.setvalue(51, 4, bc6h_format->gz); // 5: gz[3:0]
  2204. header.setvalue(55, 6, bc6h_format->bx); // 6: bx[5:0]
  2205. header.setvalue(61, 4, bc6h_format->by); // 6: by[3:0]
  2206. header.setvalue(65, 5, bc6h_format->ry); // 5: ry[4:0]
  2207. header.setvalue(70, 1, bc6h_format->bz, 2); // bz[2]
  2208. header.setvalue(71, 5, bc6h_format->rz); // 5: rz[4:0]
  2209. header.setvalue(76, 1, bc6h_format->bz, 3); // bz[3]
  2210. break;
  2211. case 10: // 0x1E
  2212. header.setvalue(0, 5, 0x1E);
  2213. header.setvalue(5, 6, bc6h_format->rw); // 6: rw[5:0]
  2214. header.setvalue(11, 1, bc6h_format->gz, 4); // gz[4]
  2215. header.setvalue(12, 1, bc6h_format->bz); // 6: bz[0]
  2216. header.setvalue(13, 1, bc6h_format->bz, 1); // bz[1]
  2217. header.setvalue(14, 1, bc6h_format->by, 4); // by[4]
  2218. header.setvalue(15, 6, bc6h_format->gw); // 6: gw[5:0]
  2219. header.setvalue(21, 1, bc6h_format->gy, 5); // gy[5]
  2220. header.setvalue(22, 1, bc6h_format->by, 5); // by[5]
  2221. header.setvalue(23, 1, bc6h_format->bz, 2); // bz[2]
  2222. header.setvalue(24, 1, bc6h_format->gy, 4); // gy[4]
  2223. header.setvalue(25, 6, bc6h_format->bw); // 6: bw[5:0]
  2224. header.setvalue(31, 1, bc6h_format->gz, 5); // gz[5]
  2225. header.setvalue(32, 1, bc6h_format->bz, 3); // bz[3]
  2226. header.setvalue(33, 1, bc6h_format->bz, 5); // bz[5]
  2227. header.setvalue(34, 1, bc6h_format->bz, 4); // bz[4]
  2228. header.setvalue(35, 6, bc6h_format->rx); // 6: rx[5:0]
  2229. header.setvalue(41, 4, bc6h_format->gy); // 6: gy[3:0]
  2230. header.setvalue(45, 6, bc6h_format->gx); // 6: gx[5:0]
  2231. header.setvalue(51, 4, bc6h_format->gz); // 6: gz[3:0]
  2232. header.setvalue(55, 6, bc6h_format->bx); // 6: bx[5:0]
  2233. header.setvalue(61, 4, bc6h_format->by); // 6: by[3:0]
  2234. header.setvalue(65, 6, bc6h_format->ry); // 6: ry[5:0]
  2235. header.setvalue(71, 6, bc6h_format->rz); // 6: rz[5:0]
  2236. break;
  2237. // Single regions Modes
  2238. case 11: // 0x03
  2239. header.setvalue(0, 5, 0x03);
  2240. header.setvalue(5, 10, bc6h_format->rw); // 10: rw[9:0]
  2241. header.setvalue(15, 10, bc6h_format->gw); // 10: gw[9:0]
  2242. header.setvalue(25, 10, bc6h_format->bw); // 10: bw[9:0]
  2243. header.setvalue(35, 10, bc6h_format->rx); // 10: rx[9:0]
  2244. header.setvalue(45, 10, bc6h_format->gx); // 10: gx[9:0]
  2245. header.setvalue(55, 10, bc6h_format->bx); // 10: bx[9:0]
  2246. break;
  2247. case 12: // 0x07
  2248. header.setvalue(0, 5, 0x07);
  2249. header.setvalue(5, 10, bc6h_format->rw); // 11: rw[9:0]
  2250. header.setvalue(15, 10, bc6h_format->gw); // 11: gw[9:0]
  2251. header.setvalue(25, 10, bc6h_format->bw); // 11: bw[9:0]
  2252. header.setvalue(35, 9, bc6h_format->rx); // 9: rx[8:0]
  2253. header.setvalue(44, 1, bc6h_format->rw, 10); // rw[10]
  2254. header.setvalue(45, 9, bc6h_format->gx); // 9: gx[8:0]
  2255. header.setvalue(54, 1, bc6h_format->gw, 10); // gw[10]
  2256. header.setvalue(55, 9, bc6h_format->bx); // 9: bx[8:0]
  2257. header.setvalue(64, 1, bc6h_format->bw, 10); // bw[10]
  2258. break;
  2259. case 13: // 0x0B
  2260. header.setvalue(0, 5, 0x0B);
  2261. header.setvalue(5, 10, bc6h_format->rw); // 12: rw[9:0]
  2262. header.setvalue(15, 10, bc6h_format->gw); // 12: gw[9:0]
  2263. header.setvalue(25, 10, bc6h_format->bw); // 12: bw[9:0]
  2264. header.setvalue(35, 8, bc6h_format->rx); // 8: rx[7:0]
  2265. header.setvalue(43, 1, bc6h_format->rw, 11); // rw[11]
  2266. header.setvalue(44, 1, bc6h_format->rw, 10); // rw[10]
  2267. header.setvalue(45, 8, bc6h_format->gx); // 8: gx[7:0]
  2268. header.setvalue(53, 1, bc6h_format->gw, 11); // gw[11]
  2269. header.setvalue(54, 1, bc6h_format->gw, 10); // gw[10]
  2270. header.setvalue(55, 8, bc6h_format->bx); // 8: bx[7:0]
  2271. header.setvalue(63, 1, bc6h_format->bw, 11); // bw[11]
  2272. header.setvalue(64, 1, bc6h_format->bw, 10); // bw[10]
  2273. break;
  2274. case 14: // 0x0F
  2275. header.setvalue(0, 5, 0x0F);
  2276. header.setvalue(5, 10, bc6h_format->rw); // 16: rw[9:0]
  2277. header.setvalue(15, 10, bc6h_format->gw); // 16: gw[9:0]
  2278. header.setvalue(25, 10, bc6h_format->bw); // 16: bw[9:0]
  2279. header.setvalue(35, 4, bc6h_format->rx); // 4: rx[3:0]
  2280. header.setvalue(39, 6, bc6h_format->rw, 10); // rw[15:10]
  2281. header.setvalue(45, 4, bc6h_format->gx); // 4: gx[3:0]
  2282. header.setvalue(49, 6, bc6h_format->gw, 10); // gw[15:10]
  2283. header.setvalue(55, 4, bc6h_format->bx); // 4: bx[3:0]
  2284. header.setvalue(59, 6, bc6h_format->bw, 10); // bw[15:10]
  2285. break;
  2286. default: // Need to indicate error!
  2287. return;
  2288. }
  2289. // Each format in the mode table can be uniquely identified by the mode bits.
  2290. // The first ten modes are used for two-region tiles, and the mode bit field
  2291. // can be either two or five bits long. These blocks also have fields for
  2292. // the compressed color endpoints (72 or 75 bits), the partition (5 bits),
  2293. // and the partition indices (46 bits).
  2294. if (bc6h_format->m_mode >= MIN_MODE_FOR_ONE_REGION)
  2295. {
  2296. CGU_INT startbit = ONE_REGION_INDEX_OFFSET;
  2297. header.setvalue(startbit, 3, bc6h_format->indices16[0]);
  2298. startbit += 3;
  2299. for (CGU_INT i = 1; i < 16; i++)
  2300. {
  2301. header.setvalue(startbit, 4, bc6h_format->indices16[i]);
  2302. startbit += 4;
  2303. }
  2304. }
  2305. else
  2306. {
  2307. header.setvalue(77, 5, bc6h_format->d_shape_index); // Shape Index
  2308. CGU_INT startbit = TWO_REGION_INDEX_OFFSET, nbits = 2;
  2309. header.setvalue(startbit, nbits, bc6h_format->indices16[0]);
  2310. for (CGU_INT i = 1; i < 16; i++)
  2311. {
  2312. startbit += nbits; // offset start bit for next index using prior nbits used
  2313. nbits = g_indexfixups[bc6h_format->d_shape_index] == i ? 2 : 3; // get new number of bit to save index with
  2314. header.setvalue(startbit, nbits, bc6h_format->indices16[i]);
  2315. }
  2316. }
  2317. // save to output buffer our new bit values
  2318. // this can be optimized if header is part of bc6h_format struct
  2319. header.transferbits(cmpout, 16);
  2320. }
  2321. #else
  2322. void SaveDataBlock(BC6H_Encode_local* bc6h_format, CMP_GLOBAL CGU_UINT8 out[COMPRESSED_BLOCK_SIZE])
  2323. {
  2324. // ToDo
  2325. }
  2326. #endif
  2327. void SwapIndices(CGU_INT32 iEndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2328. CGU_INT32 iIndices[3][MAX_SUBSET_SIZE],
  2329. CGU_INT entryCount[MAX_SUBSETS],
  2330. CGU_INT max_subsets,
  2331. CGU_INT mode,
  2332. CGU_INT shape_pattern)
  2333. {
  2334. CGU_UINT32 uNumIndices = 1 << ModePartition[mode].IndexPrec;
  2335. CGU_UINT32 uHighIndexBit = uNumIndices >> 1;
  2336. for (CGU_INT subset = 0; subset < max_subsets; ++subset)
  2337. {
  2338. // region 0 (subset = 0) The fix-up index for this subset is allways index 0
  2339. // region 1 (subset = 1) The fix-up index for this subset varies based on the shape
  2340. size_t i = subset ? g_Region2FixUp[shape_pattern] : 0;
  2341. if (iIndices[subset][i] & uHighIndexBit)
  2342. {
  2343. #ifdef ASPM_GPU
  2344. // high bit is set, swap the aEndPts and indices for this region
  2345. swap(iEndPoints[subset][0][0], iEndPoints[subset][1][0]);
  2346. swap(iEndPoints[subset][0][1], iEndPoints[subset][1][1]);
  2347. swap(iEndPoints[subset][0][2], iEndPoints[subset][1][2]);
  2348. #else
  2349. // high bit is set, swap the aEndPts and indices for this region
  2350. std::swap(iEndPoints[subset][0][0], iEndPoints[subset][1][0]);
  2351. std::swap(iEndPoints[subset][0][1], iEndPoints[subset][1][1]);
  2352. std::swap(iEndPoints[subset][0][2], iEndPoints[subset][1][2]);
  2353. #endif
  2354. for (size_t j = 0; j < (size_t)entryCount[subset]; ++j)
  2355. {
  2356. iIndices[subset][j] = uNumIndices - 1 - iIndices[subset][j];
  2357. }
  2358. }
  2359. }
  2360. }
  2361. // helper function to check transform overflow
  2362. // todo: check overflow by checking against sign
  2363. CGU_BOOL isOverflow(CGU_INT endpoint, CGU_INT nbit)
  2364. {
  2365. CGU_INT maxRange = (int)pow(2.0f, (CGU_FLOAT)nbit - 1.0f) - 1;
  2366. CGU_INT minRange = (int)-(pow(2.0f, (CGU_FLOAT)nbit - 1.0f));
  2367. //no overflow
  2368. if ((endpoint >= minRange) && (endpoint <= maxRange))
  2369. return false;
  2370. else //overflow
  2371. return true;
  2372. }
  2373. CGU_BOOL TransformEndPoints(BC6H_Encode_local* BC6H_data,
  2374. CGU_INT iEndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2375. CGU_INT oEndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2376. CGU_INT max_subsets,
  2377. CGU_INT mode)
  2378. {
  2379. CGU_INT Mask;
  2380. if (ModePartition[mode].transformed)
  2381. {
  2382. BC6H_data->istransformed = true;
  2383. for (CGU_INT i = 0; i < 3; ++i)
  2384. {
  2385. Mask = MASK(ModePartition[mode].nbits);
  2386. oEndPoints[0][0][i] = iEndPoints[0][0][i] & Mask; // [0][A]
  2387. Mask = MASK(ModePartition[mode].prec[i]);
  2388. oEndPoints[0][1][i] = iEndPoints[0][1][i] - iEndPoints[0][0][i]; // [0][B] - [0][A]
  2389. if (isOverflow(oEndPoints[0][1][i], ModePartition[mode].prec[i]))
  2390. return false;
  2391. oEndPoints[0][1][i] = (oEndPoints[0][1][i] & Mask);
  2392. //redo the check for sign overflow for one region case
  2393. if (max_subsets <= 1)
  2394. {
  2395. if (isOverflow(oEndPoints[0][1][i], ModePartition[mode].prec[i]))
  2396. return false;
  2397. }
  2398. if (max_subsets > 1)
  2399. {
  2400. oEndPoints[1][0][i] = iEndPoints[1][0][i] - iEndPoints[0][0][i]; // [1][A] - [0][A]
  2401. if (isOverflow(oEndPoints[1][0][i], ModePartition[mode].prec[i]))
  2402. return false;
  2403. oEndPoints[1][0][i] = (oEndPoints[1][0][i] & Mask);
  2404. oEndPoints[1][1][i] = iEndPoints[1][1][i] - iEndPoints[0][0][i]; // [1][B] - [0][A]
  2405. if (isOverflow(oEndPoints[1][1][i], ModePartition[mode].prec[i]))
  2406. return false;
  2407. oEndPoints[1][1][i] = (oEndPoints[1][1][i] & Mask);
  2408. }
  2409. }
  2410. }
  2411. else
  2412. {
  2413. BC6H_data->istransformed = false;
  2414. for (CGU_INT i = 0; i < 3; ++i)
  2415. {
  2416. Mask = MASK(ModePartition[mode].nbits);
  2417. oEndPoints[0][0][i] = iEndPoints[0][0][i] & Mask;
  2418. Mask = MASK(ModePartition[mode].prec[i]);
  2419. oEndPoints[0][1][i] = iEndPoints[0][1][i] & Mask;
  2420. if (max_subsets > 1)
  2421. {
  2422. oEndPoints[1][0][i] = iEndPoints[1][0][i] & Mask;
  2423. oEndPoints[1][1][i] = iEndPoints[1][1][i] & Mask;
  2424. }
  2425. }
  2426. }
  2427. return true;
  2428. }
  2429. void SaveCompressedBlockData(BC6H_Encode_local* BC6H_data,
  2430. CGU_INT oEndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2431. CGU_INT iIndices[2][MAX_SUBSET_SIZE],
  2432. CGU_INT8 max_subsets,
  2433. CGU_INT8 mode)
  2434. {
  2435. BC6H_data->m_mode = mode;
  2436. BC6H_data->index++;
  2437. // Save the data to output
  2438. BC6H_data->rw = oEndPoints[0][0][0]; // rw
  2439. BC6H_data->gw = oEndPoints[0][0][1]; // gw
  2440. BC6H_data->bw = oEndPoints[0][0][2]; // bw
  2441. BC6H_data->rx = oEndPoints[0][1][0]; // rx
  2442. BC6H_data->gx = oEndPoints[0][1][1]; // gx
  2443. BC6H_data->bx = oEndPoints[0][1][2]; // bx
  2444. if (max_subsets > 1)
  2445. {
  2446. // Save the data to output
  2447. BC6H_data->ry = oEndPoints[1][0][0]; // ry
  2448. BC6H_data->gy = oEndPoints[1][0][1]; // gy
  2449. BC6H_data->by = oEndPoints[1][0][2]; // by
  2450. BC6H_data->rz = oEndPoints[1][1][0]; // rz
  2451. BC6H_data->gz = oEndPoints[1][1][1]; // gz
  2452. BC6H_data->bz = oEndPoints[1][1][2]; // bz
  2453. }
  2454. // Map our two subset Indices for the shape to output 4x4 block
  2455. CGU_INT pos[2] = {0, 0};
  2456. CGU_INT asubset;
  2457. for (CGU_INT i = 0; i < MAX_SUBSET_SIZE; i++)
  2458. {
  2459. if (max_subsets > 1)
  2460. asubset = BC6_PARTITIONS[BC6H_data->d_shape_index][i]; // Two region shapes
  2461. else
  2462. asubset = 0; // One region shapes
  2463. BC6H_data->indices16[i] = (CGU_UINT8)iIndices[asubset][pos[asubset]];
  2464. pos[asubset]++;
  2465. }
  2466. }
  2467. CGU_FLOAT CalcOneRegionEndPtsError(BC6H_Encode_local* BC6H_data,
  2468. CGU_FLOAT fEndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2469. CGU_INT shape_indices[MAX_SUBSETS][MAX_SUBSET_SIZE])
  2470. {
  2471. CGU_FLOAT error = 0;
  2472. for (CGU_INT i = 0; i < MAX_SUBSET_SIZE; i++)
  2473. {
  2474. for (CGU_INT m = 0; m < MAX_END_POINTS; m++)
  2475. {
  2476. for (CGU_INT n = 0; n < NCHANNELS; n++)
  2477. {
  2478. CGU_FLOAT calencpts = fEndPoints[0][m][n] + (abs(fEndPoints[0][m][n] - fEndPoints[0][m][n]) * (shape_indices[0][i] / 15));
  2479. error += abs(BC6H_data->din[i][n] - calencpts);
  2480. }
  2481. }
  2482. }
  2483. return error;
  2484. }
  2485. void ReIndexShapef(BC6H_Encode_local* BC6H_data, CGU_INT shape_indices[MAX_SUBSETS][MAX_SUBSET_SIZE])
  2486. {
  2487. CGU_FLOAT error = 0;
  2488. CGU_FLOAT bestError;
  2489. CGU_INT bestIndex = 0;
  2490. CGU_INT sub0index = 0;
  2491. CGU_INT sub1index = 0;
  2492. CGU_INT MaxPallet;
  2493. CGU_INT region = (BC6H_data->region - 1);
  2494. if (region == 0)
  2495. MaxPallet = 16;
  2496. else
  2497. MaxPallet = 8;
  2498. CGU_UINT8 isSet = 0;
  2499. for (CGU_INT i = 0; i < MAX_SUBSET_SIZE; i++)
  2500. {
  2501. // subset 0 or subset 1
  2502. if (region)
  2503. isSet = BC6_PARTITIONS[BC6H_data->d_shape_index][i];
  2504. if (isSet)
  2505. {
  2506. bestError = CMP_HALF_MAX;
  2507. bestIndex = 0;
  2508. // For two shape regions max Pallet is 8
  2509. for (CGU_INT j = 0; j < MaxPallet; j++)
  2510. {
  2511. // Calculate error from original
  2512. error = abs(BC6H_data->din[i][0] - BC6H_data->Paletef[1][j].x) + abs(BC6H_data->din[i][1] - BC6H_data->Paletef[1][j].y) +
  2513. abs(BC6H_data->din[i][2] - BC6H_data->Paletef[1][j].z);
  2514. if (error < bestError)
  2515. {
  2516. bestError = error;
  2517. bestIndex = j;
  2518. }
  2519. }
  2520. shape_indices[1][sub1index] = bestIndex;
  2521. sub1index++;
  2522. }
  2523. else
  2524. {
  2525. // This is shared for one or two shape regions max Pallet either 16 or 8
  2526. bestError = CMP_FLOAT_MAX;
  2527. bestIndex = 0;
  2528. for (CGU_INT j = 0; j < MaxPallet; j++)
  2529. {
  2530. // Calculate error from original
  2531. error = abs(BC6H_data->din[i][0] - BC6H_data->Paletef[0][j].x) + abs(BC6H_data->din[i][1] - BC6H_data->Paletef[0][j].y) +
  2532. abs(BC6H_data->din[i][2] - BC6H_data->Paletef[0][j].z);
  2533. if (error < bestError)
  2534. {
  2535. bestError = error;
  2536. bestIndex = j;
  2537. }
  2538. }
  2539. shape_indices[0][sub0index] = bestIndex;
  2540. sub0index++;
  2541. }
  2542. }
  2543. }
  2544. CGU_INT Unquantize(CGU_INT comp, unsigned char uBitsPerComp, CGU_BOOL bSigned)
  2545. {
  2546. CGU_INT unq = 0, s = 0;
  2547. if (bSigned)
  2548. {
  2549. if (uBitsPerComp >= 16)
  2550. {
  2551. unq = comp;
  2552. }
  2553. else
  2554. {
  2555. if (comp < 0)
  2556. {
  2557. s = 1;
  2558. comp = -comp;
  2559. }
  2560. if (comp == 0)
  2561. unq = 0;
  2562. else if (comp >= ((1 << (uBitsPerComp - 1)) - 1))
  2563. unq = 0x7FFF;
  2564. else
  2565. unq = ((comp << 15) + 0x4000) >> (uBitsPerComp - 1);
  2566. if (s)
  2567. unq = -unq;
  2568. }
  2569. }
  2570. else
  2571. {
  2572. if (uBitsPerComp >= 15)
  2573. unq = comp;
  2574. else if (comp == 0)
  2575. unq = 0;
  2576. else if (comp == ((1 << uBitsPerComp) - 1))
  2577. unq = 0xFFFF;
  2578. else
  2579. unq = ((comp << 16) + 0x8000) >> uBitsPerComp;
  2580. }
  2581. return unq;
  2582. }
  2583. CGU_INT finish_unquantizef16(CGU_INT q, CGU_BOOL isSigned)
  2584. {
  2585. // Is it F16 Signed else F16 Unsigned
  2586. if (isSigned)
  2587. return (q < 0) ? -(((-q) * 31) >> 5) : (q * 31) >> 5; // scale the magnitude by 31/32
  2588. else
  2589. return (q * 31) >> 6; // scale the magnitude by 31/64
  2590. // Note for Undefined we should return q as is
  2591. }
  2592. // decompress endpoints
  2593. void decompress_endpoints1(BC6H_Encode_local* bc6h_format,
  2594. CGU_INT oEndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2595. CGU_FLOAT outf[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2596. CGU_INT mode)
  2597. {
  2598. CGU_INT i;
  2599. CGU_INT t;
  2600. CGU_FLOAT out[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG];
  2601. if (bc6h_format->issigned)
  2602. {
  2603. if (bc6h_format->istransformed)
  2604. {
  2605. for (i = 0; i < NCHANNELS; i++)
  2606. {
  2607. out[0][0][i] = (CGU_FLOAT)SIGN_EXTEND_TYPELESS(oEndPoints[0][0][i], ModePartition[mode].nbits);
  2608. t = SIGN_EXTEND_TYPELESS(oEndPoints[0][1][i], ModePartition[mode].prec[i]); //C_RED
  2609. t = (t + oEndPoints[0][0][i]) & MASK(ModePartition[mode].nbits);
  2610. out[0][1][i] = (CGU_FLOAT)SIGN_EXTEND_TYPELESS(t, ModePartition[mode].nbits);
  2611. // Unquantize all points to nbits
  2612. out[0][0][i] = (CGU_FLOAT)Unquantize((int)out[0][0][i], (unsigned char)ModePartition[mode].nbits, false);
  2613. out[0][1][i] = (CGU_FLOAT)Unquantize((int)out[0][1][i], (unsigned char)ModePartition[mode].nbits, false);
  2614. // F16 format
  2615. outf[0][0][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][0][i], false);
  2616. outf[0][1][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][1][i], false);
  2617. }
  2618. }
  2619. else
  2620. {
  2621. for (i = 0; i < NCHANNELS; i++)
  2622. {
  2623. out[0][0][i] = (CGU_FLOAT)SIGN_EXTEND_TYPELESS(oEndPoints[0][0][i], ModePartition[mode].nbits);
  2624. out[0][1][i] = (CGU_FLOAT)SIGN_EXTEND_TYPELESS(oEndPoints[0][1][i], ModePartition[mode].prec[i]);
  2625. // Unquantize all points to nbits
  2626. out[0][0][i] = (CGU_FLOAT)Unquantize((int)out[0][0][i], (unsigned char)ModePartition[mode].nbits, false);
  2627. out[0][1][i] = (CGU_FLOAT)Unquantize((int)out[0][1][i], (unsigned char)ModePartition[mode].nbits, false);
  2628. // F16 format
  2629. outf[0][0][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][0][i], false);
  2630. outf[0][1][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][1][i], false);
  2631. }
  2632. }
  2633. }
  2634. else
  2635. {
  2636. if (bc6h_format->istransformed)
  2637. {
  2638. for (i = 0; i < NCHANNELS; i++)
  2639. {
  2640. out[0][0][i] = (CGU_FLOAT)oEndPoints[0][0][i];
  2641. t = SIGN_EXTEND_TYPELESS(oEndPoints[0][1][i], ModePartition[mode].prec[i]);
  2642. out[0][1][i] = (CGU_FLOAT)((t + oEndPoints[0][0][i]) & MASK(ModePartition[mode].nbits));
  2643. // Unquantize all points to nbits
  2644. out[0][0][i] = (CGU_FLOAT)Unquantize((int)out[0][0][i], (unsigned char)ModePartition[mode].nbits, false);
  2645. out[0][1][i] = (CGU_FLOAT)Unquantize((int)out[0][1][i], (unsigned char)ModePartition[mode].nbits, false);
  2646. // F16 format
  2647. outf[0][0][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][0][i], false);
  2648. outf[0][1][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][1][i], false);
  2649. }
  2650. }
  2651. else
  2652. {
  2653. for (i = 0; i < NCHANNELS; i++)
  2654. {
  2655. out[0][0][i] = (CGU_FLOAT)oEndPoints[0][0][i];
  2656. out[0][1][i] = (CGU_FLOAT)oEndPoints[0][1][i];
  2657. // Unquantize all points to nbits
  2658. out[0][0][i] = (CGU_FLOAT)Unquantize((int)out[0][0][i], (unsigned char)ModePartition[mode].nbits, false);
  2659. out[0][1][i] = (CGU_FLOAT)Unquantize((int)out[0][1][i], (unsigned char)ModePartition[mode].nbits, false);
  2660. // F16 format
  2661. outf[0][0][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][0][i], false);
  2662. outf[0][1][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][1][i], false);
  2663. }
  2664. }
  2665. }
  2666. }
  2667. void decompress_endpoints2(BC6H_Encode_local* bc6h_format,
  2668. CGU_INT oEndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2669. CGU_FLOAT outf[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2670. CGU_INT mode)
  2671. {
  2672. CGU_INT i;
  2673. CGU_INT t;
  2674. CGU_FLOAT out[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG];
  2675. if (bc6h_format->issigned)
  2676. {
  2677. if (bc6h_format->istransformed)
  2678. {
  2679. for (i = 0; i < NCHANNELS; i++)
  2680. {
  2681. // get the quantized values
  2682. out[0][0][i] = (CGU_FLOAT)SIGN_EXTEND_TYPELESS(oEndPoints[0][0][i], ModePartition[mode].nbits);
  2683. t = SIGN_EXTEND_TYPELESS(oEndPoints[0][1][i], ModePartition[mode].prec[i]);
  2684. t = (t + oEndPoints[0][0][i]) & MASK(ModePartition[mode].nbits);
  2685. out[0][1][i] = (CGU_FLOAT)SIGN_EXTEND_TYPELESS(t, ModePartition[mode].nbits);
  2686. t = SIGN_EXTEND_TYPELESS(oEndPoints[1][0][i], ModePartition[mode].prec[i]);
  2687. t = (t + oEndPoints[0][0][i]) & MASK(ModePartition[mode].nbits);
  2688. out[1][0][i] = (CGU_FLOAT)SIGN_EXTEND_TYPELESS(t, ModePartition[mode].nbits);
  2689. t = SIGN_EXTEND_TYPELESS(oEndPoints[1][1][i], ModePartition[mode].prec[i]);
  2690. t = (t + oEndPoints[0][0][i]) & MASK(ModePartition[mode].nbits);
  2691. out[1][1][i] = (CGU_FLOAT)SIGN_EXTEND_TYPELESS(t, ModePartition[mode].nbits);
  2692. // Unquantize all points to nbits
  2693. out[0][0][i] = (CGU_FLOAT)Unquantize((int)out[0][0][i], (unsigned char)ModePartition[mode].nbits, true);
  2694. out[0][1][i] = (CGU_FLOAT)Unquantize((int)out[0][1][i], (unsigned char)ModePartition[mode].nbits, true);
  2695. out[1][0][i] = (CGU_FLOAT)Unquantize((int)out[1][0][i], (unsigned char)ModePartition[mode].nbits, true);
  2696. out[1][1][i] = (CGU_FLOAT)Unquantize((int)out[1][1][i], (unsigned char)ModePartition[mode].nbits, true);
  2697. // F16 format
  2698. outf[0][0][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][0][i], true);
  2699. outf[0][1][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][1][i], true);
  2700. outf[1][0][i] = (CGU_FLOAT)finish_unquantizef16((int)out[1][0][i], true);
  2701. outf[1][1][i] = (CGU_FLOAT)finish_unquantizef16((int)out[1][1][i], true);
  2702. }
  2703. }
  2704. else
  2705. {
  2706. for (i = 0; i < NCHANNELS; i++)
  2707. {
  2708. out[0][0][i] = (CGU_FLOAT)SIGN_EXTEND_TYPELESS(oEndPoints[0][0][i], ModePartition[mode].nbits);
  2709. out[0][1][i] = (CGU_FLOAT)SIGN_EXTEND_TYPELESS(oEndPoints[0][1][i], ModePartition[mode].prec[i]);
  2710. out[1][0][i] = (CGU_FLOAT)SIGN_EXTEND_TYPELESS(oEndPoints[1][0][i], ModePartition[mode].prec[i]);
  2711. out[1][1][i] = (CGU_FLOAT)SIGN_EXTEND_TYPELESS(oEndPoints[1][1][i], ModePartition[mode].prec[i]);
  2712. // Unquantize all points to nbits
  2713. out[0][0][i] = (CGU_FLOAT)Unquantize((int)out[0][0][i], (unsigned char)ModePartition[mode].nbits, false);
  2714. out[0][1][i] = (CGU_FLOAT)Unquantize((int)out[0][1][i], (unsigned char)ModePartition[mode].nbits, false);
  2715. out[1][0][i] = (CGU_FLOAT)Unquantize((int)out[1][0][i], (unsigned char)ModePartition[mode].nbits, false);
  2716. out[1][1][i] = (CGU_FLOAT)Unquantize((int)out[1][1][i], (unsigned char)ModePartition[mode].nbits, false);
  2717. // nbits to F16 format
  2718. outf[0][0][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][0][i], false);
  2719. outf[0][1][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][1][i], false);
  2720. outf[1][0][i] = (CGU_FLOAT)finish_unquantizef16((int)out[1][0][i], false);
  2721. outf[1][1][i] = (CGU_FLOAT)finish_unquantizef16((int)out[1][1][i], false);
  2722. }
  2723. }
  2724. }
  2725. else
  2726. {
  2727. if (bc6h_format->istransformed)
  2728. {
  2729. for (i = 0; i < NCHANNELS; i++)
  2730. {
  2731. out[0][0][i] = (CGU_FLOAT)oEndPoints[0][0][i];
  2732. t = SIGN_EXTEND_TYPELESS(oEndPoints[0][1][i], ModePartition[mode].prec[i]);
  2733. out[0][1][i] = (CGU_FLOAT)((t + oEndPoints[0][0][i]) & MASK(ModePartition[mode].nbits));
  2734. t = SIGN_EXTEND_TYPELESS(oEndPoints[1][0][i], ModePartition[mode].prec[i]);
  2735. out[1][0][i] = (CGU_FLOAT)((t + oEndPoints[0][0][i]) & MASK(ModePartition[mode].nbits));
  2736. t = SIGN_EXTEND_TYPELESS(oEndPoints[1][1][i], ModePartition[mode].prec[i]);
  2737. out[1][1][i] = (CGU_FLOAT)((t + oEndPoints[0][0][i]) & MASK(ModePartition[mode].nbits));
  2738. // Unquantize all points to nbits
  2739. out[0][0][i] = (CGU_FLOAT)Unquantize((int)out[0][0][i], (unsigned char)ModePartition[mode].nbits, false);
  2740. out[0][1][i] = (CGU_FLOAT)Unquantize((int)out[0][1][i], (unsigned char)ModePartition[mode].nbits, false);
  2741. out[1][0][i] = (CGU_FLOAT)Unquantize((int)out[1][0][i], (unsigned char)ModePartition[mode].nbits, false);
  2742. out[1][1][i] = (CGU_FLOAT)Unquantize((int)out[1][1][i], (unsigned char)ModePartition[mode].nbits, false);
  2743. // nbits to F16 format
  2744. outf[0][0][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][0][i], false);
  2745. outf[0][1][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][1][i], false);
  2746. outf[1][0][i] = (CGU_FLOAT)finish_unquantizef16((int)out[1][0][i], false);
  2747. outf[1][1][i] = (CGU_FLOAT)finish_unquantizef16((int)out[1][1][i], false);
  2748. }
  2749. }
  2750. else
  2751. {
  2752. for (i = 0; i < NCHANNELS; i++)
  2753. {
  2754. out[0][0][i] = (CGU_FLOAT)oEndPoints[0][0][i];
  2755. out[0][1][i] = (CGU_FLOAT)oEndPoints[0][1][i];
  2756. out[1][0][i] = (CGU_FLOAT)oEndPoints[1][0][i];
  2757. out[1][1][i] = (CGU_FLOAT)oEndPoints[1][1][i];
  2758. // Unquantize all points to nbits
  2759. out[0][0][i] = (CGU_FLOAT)Unquantize((int)out[0][0][i], (unsigned char)ModePartition[mode].nbits, false);
  2760. out[0][1][i] = (CGU_FLOAT)Unquantize((int)out[0][1][i], (unsigned char)ModePartition[mode].nbits, false);
  2761. out[1][0][i] = (CGU_FLOAT)Unquantize((int)out[1][0][i], (unsigned char)ModePartition[mode].nbits, false);
  2762. out[1][1][i] = (CGU_FLOAT)Unquantize((int)out[1][1][i], (unsigned char)ModePartition[mode].nbits, false);
  2763. // nbits to F16 format
  2764. outf[0][0][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][0][i], false);
  2765. outf[0][1][i] = (CGU_FLOAT)finish_unquantizef16((int)out[0][1][i], false);
  2766. outf[1][0][i] = (CGU_FLOAT)finish_unquantizef16((int)out[1][0][i], false);
  2767. outf[1][1][i] = (CGU_FLOAT)finish_unquantizef16((int)out[1][1][i], false);
  2768. }
  2769. }
  2770. }
  2771. }
  2772. // decompress endpoints
  2773. static void decompress_endpts(const CGU_INT in[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2774. CGU_INT out[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2775. const CGU_INT mode,
  2776. CGU_BOOL issigned)
  2777. {
  2778. if (ModePartition[mode].transformed)
  2779. {
  2780. for (CGU_INT i = 0; i < 3; ++i)
  2781. {
  2782. R_0(out) = issigned ? SIGN_EXTEND_TYPELESS(R_0(in), ModePartition[mode].IndexPrec) : R_0(in);
  2783. CGU_INT t;
  2784. t = SIGN_EXTEND_TYPELESS(R_1(in), ModePartition[mode].prec[i]);
  2785. t = (t + R_0(in)) & MASK(ModePartition[mode].nbits);
  2786. R_1(out) = issigned ? SIGN_EXTEND_TYPELESS(t, ModePartition[mode].nbits) : t;
  2787. t = SIGN_EXTEND_TYPELESS(R_2(in), ModePartition[mode].prec[i]);
  2788. t = (t + R_0(in)) & MASK(ModePartition[mode].nbits);
  2789. R_2(out) = issigned ? SIGN_EXTEND_TYPELESS(t, ModePartition[mode].nbits) : t;
  2790. t = SIGN_EXTEND_TYPELESS(R_3(in), ModePartition[mode].prec[i]);
  2791. t = (t + R_0(in)) & MASK(ModePartition[mode].nbits);
  2792. R_3(out) = issigned ? SIGN_EXTEND_TYPELESS(t, ModePartition[mode].nbits) : t;
  2793. }
  2794. }
  2795. else
  2796. {
  2797. for (CGU_INT i = 0; i < 3; ++i)
  2798. {
  2799. R_0(out) = issigned ? SIGN_EXTEND_TYPELESS(R_0(in), ModePartition[mode].nbits) : R_0(in);
  2800. R_1(out) = issigned ? SIGN_EXTEND_TYPELESS(R_1(in), ModePartition[mode].prec[i]) : R_1(in);
  2801. R_2(out) = issigned ? SIGN_EXTEND_TYPELESS(R_2(in), ModePartition[mode].prec[i]) : R_2(in);
  2802. R_3(out) = issigned ? SIGN_EXTEND_TYPELESS(R_3(in), ModePartition[mode].prec[i]) : R_3(in);
  2803. }
  2804. }
  2805. }
  2806. // endpoints fit only if the compression was lossless
  2807. static CGU_BOOL endpts_fit(const CGU_INT orig[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2808. const CGU_INT compressed[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2809. const CGU_INT mode,
  2810. CGU_INT max_subsets,
  2811. CGU_BOOL issigned)
  2812. {
  2813. CGU_INT uncompressed[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG];
  2814. decompress_endpts(compressed, uncompressed, mode, issigned);
  2815. for (CGU_INT j = 0; j < max_subsets; ++j)
  2816. for (CGU_INT i = 0; i < 3; ++i)
  2817. {
  2818. if (orig[j][0][i] != uncompressed[j][0][i])
  2819. return false;
  2820. if (orig[j][1][i] != uncompressed[j][1][i])
  2821. return false;
  2822. }
  2823. return true;
  2824. }
  2825. //todo: checkoverflow
  2826. void QuantizeEndPointToF16Prec(CGU_FLOAT EndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2827. CGU_INT iEndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG],
  2828. CGU_INT max_subsets,
  2829. CGU_INT prec,
  2830. CGU_BOOL isSigned)
  2831. {
  2832. for (CGU_INT subset = 0; subset < max_subsets; ++subset)
  2833. {
  2834. iEndPoints[subset][0][0] = cmp_QuantizeToBitSize((CGU_INT)EndPoints[subset][0][0], prec, isSigned); // A.Red
  2835. iEndPoints[subset][0][1] = cmp_QuantizeToBitSize((CGU_INT)EndPoints[subset][0][1], prec, isSigned); // A.Green
  2836. iEndPoints[subset][0][2] = cmp_QuantizeToBitSize((CGU_INT)EndPoints[subset][0][2], prec, isSigned); // A.Blue
  2837. iEndPoints[subset][1][0] = cmp_QuantizeToBitSize((CGU_INT)EndPoints[subset][1][0], prec, isSigned); // B.Red
  2838. iEndPoints[subset][1][1] = cmp_QuantizeToBitSize((CGU_INT)EndPoints[subset][1][1], prec, isSigned); // B.Green
  2839. iEndPoints[subset][1][2] = cmp_QuantizeToBitSize((CGU_INT)EndPoints[subset][1][2], prec, isSigned); // B.Blue
  2840. }
  2841. }
  2842. CGU_FLOAT EncodePattern(BC6H_Encode_local* BC6H_data, CGU_FLOAT error)
  2843. {
  2844. CGU_INT8 max_subsets = BC6H_data->region;
  2845. // now we have input colors (in), output colors (outB) mapped to a line of ends (EndPoints)
  2846. // and a set of colors on the line equally spaced (indexedcolors)
  2847. // Lets assign indices
  2848. //CGU_FLOAT SrcEndPoints[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG]; // temp endpoints used during calculations
  2849. // Quantize the EndPoints
  2850. CGU_INT F16EndPoints[MAX_BC6H_MODES + 1][MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG]; // temp endpoints used during calculations
  2851. CGU_INT quantEndPoints[MAX_BC6H_MODES + 1][MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG]; // endpoints to save for a given mode
  2852. // ModePartition[] starts from 1 to 14
  2853. // If we have a shape pattern set the loop to check modes from 1 to 10 else from 11 to 14
  2854. // of the ModePartition table
  2855. CGU_INT min_mode = (BC6H_data->region == 2) ? 1 : 11;
  2856. CGU_INT max_mode = (BC6H_data->region == 2) ? MAX_TWOREGION_MODES : MAX_BC6H_MODES;
  2857. CGU_BOOL fits[15];
  2858. memset((CGU_UINT8*)fits, 0, sizeof(fits));
  2859. CGU_INT bestFit = 0;
  2860. CGU_INT bestEndpointMode = 0;
  2861. CGU_FLOAT bestError = CMP_FLOAT_MAX;
  2862. CGU_FLOAT bestEndpointsErr = CMP_FLOAT_MAX;
  2863. CGU_FLOAT endPointErr = 0;
  2864. // Try Optimization for the Mode
  2865. CGU_FLOAT best_EndPoints[MAX_BC6H_MODES + 1][MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG];
  2866. CGU_INT best_Indices[MAX_BC6H_MODES + 1][MAX_SUBSETS][MAX_SUBSET_SIZE];
  2867. CGU_FLOAT opt_toterr[MAX_BC6H_MODES + 1] = {0};
  2868. memset((CGU_UINT8*)opt_toterr, 0, sizeof(opt_toterr));
  2869. CGU_INT numfits = 0;
  2870. //
  2871. // Notes; Only the endpoints are varying; the indices stay fixed in values!
  2872. // so to optimize which mode we need only check the endpoints error against our original to pick the mode to save
  2873. //
  2874. for (CGU_INT modes = min_mode; modes <= max_mode; ++modes)
  2875. {
  2876. memcpy((CGU_UINT8*)best_EndPoints[modes], (CGU_UINT8*)BC6H_data->fEndPoints, sizeof(BC6H_data->fEndPoints));
  2877. memcpy((CGU_UINT8*)best_Indices[modes], (CGU_UINT8*)BC6H_data->shape_indices, sizeof(BC6H_data->shape_indices));
  2878. {
  2879. QuantizeEndPointToF16Prec(best_EndPoints[modes], F16EndPoints[modes], max_subsets, ModePartition[ModeFitOrder[modes]].nbits, BC6H_data->issigned);
  2880. }
  2881. // Indices data to save for given mode
  2882. SwapIndices(F16EndPoints[modes], best_Indices[modes], BC6H_data->entryCount, max_subsets, ModeFitOrder[modes], BC6H_data->d_shape_index);
  2883. CGU_BOOL transformfit = TransformEndPoints(BC6H_data, F16EndPoints[modes], quantEndPoints[modes], max_subsets, ModeFitOrder[modes]);
  2884. fits[modes] = endpts_fit(F16EndPoints[modes], quantEndPoints[modes], ModeFitOrder[modes], max_subsets, BC6H_data->issigned);
  2885. if (fits[modes] && transformfit)
  2886. {
  2887. numfits++;
  2888. // The new compressed end points fit the mode
  2889. // recalculate the error for this mode with a new set of indices
  2890. // since we have shifted the end points from what we origially calc
  2891. // from the find_bestpattern
  2892. CGU_FLOAT uncompressed[MAX_SUBSETS][MAX_END_POINTS][MAX_DIMENSION_BIG];
  2893. if (BC6H_data->region == 1)
  2894. decompress_endpoints1(BC6H_data, quantEndPoints[modes], uncompressed, ModeFitOrder[modes]);
  2895. else
  2896. decompress_endpoints2(BC6H_data, quantEndPoints[modes], uncompressed, ModeFitOrder[modes]);
  2897. // Takes the end points and creates a pallet of colors
  2898. // based on preset weights along a vector formed by the two end points
  2899. palitizeEndPointsF(BC6H_data, uncompressed);
  2900. // Once we have the pallet - recalculate the optimal indices using the pallet
  2901. // and the original image data stored in BC6H_data.din[]
  2902. if (!BC6H_data->issigned)
  2903. ReIndexShapef(BC6H_data, best_Indices[modes]);
  2904. // Calculate the error of the new tile vs the old tile data
  2905. opt_toterr[modes] = CalcShapeError(BC6H_data, uncompressed, true);
  2906. if (BC6H_data->region == 1)
  2907. {
  2908. endPointErr = CalcOneRegionEndPtsError(BC6H_data, uncompressed, best_Indices[modes]);
  2909. if (endPointErr < bestEndpointsErr)
  2910. {
  2911. bestEndpointsErr = endPointErr;
  2912. bestEndpointMode = modes;
  2913. }
  2914. }
  2915. CGU_BOOL transformFit = true;
  2916. // Save hold this mode fit data if its better than the last one checked.
  2917. if (opt_toterr[modes] < bestError)
  2918. {
  2919. if (!BC6H_data->issigned)
  2920. {
  2921. QuantizeEndPointToF16Prec(uncompressed, F16EndPoints[modes], max_subsets, ModePartition[ModeFitOrder[modes]].nbits, BC6H_data->issigned);
  2922. SwapIndices(F16EndPoints[modes], best_Indices[modes], BC6H_data->entryCount, max_subsets, ModeFitOrder[modes], BC6H_data->d_shape_index);
  2923. transformFit = TransformEndPoints(BC6H_data, F16EndPoints[modes], quantEndPoints[modes], max_subsets, ModeFitOrder[modes]);
  2924. }
  2925. if (transformFit)
  2926. {
  2927. if (BC6H_data->region == 1)
  2928. {
  2929. bestFit = (modes == bestEndpointMode) ? modes : ((modes < bestEndpointMode) ? modes : bestEndpointMode);
  2930. }
  2931. else
  2932. {
  2933. bestFit = modes;
  2934. }
  2935. bestError = opt_toterr[bestFit];
  2936. error = bestError;
  2937. }
  2938. }
  2939. }
  2940. }
  2941. if (numfits > 0)
  2942. {
  2943. SaveCompressedBlockData(BC6H_data, quantEndPoints[bestFit], best_Indices[bestFit], max_subsets, ModeFitOrder[bestFit]);
  2944. return error;
  2945. }
  2946. // Should not get here!
  2947. return error;
  2948. }
  2949. void CompressBlockBC6_Internal(CMP_GLOBAL unsigned char* outdata,
  2950. CGU_UINT32 destIdx,
  2951. BC6H_Encode_local* BC6HEncode_local,
  2952. CMP_GLOBAL const BC6H_Encode* BC6HEncode)
  2953. {
  2954. // printf("---SRC---\n");
  2955. // CGU_UINT8 blkindex = 0;
  2956. // CGU_UINT8 srcindex = 0;
  2957. // CGU_FLOAT f[3];
  2958. // for ( CGU_INT32 j = 0; j < 16; j++) {
  2959. // printf("%04x," , CGU_UINT32(BC6HEncode_local->din[j][0])); // R
  2960. // printf("%04x," , CGU_UINT32(BC6HEncode_local->din[j][1])); // G
  2961. // printf("%04x : " , CGU_UINT32(BC6HEncode_local->din[j][2])); // B
  2962. // f[0] = HalfToFloat(BC6HEncode_local->din[j][0]); // R
  2963. // f[1] = HalfToFloat(BC6HEncode_local->din[j][1]); // G
  2964. // f[2] = HalfToFloat(BC6HEncode_local->din[j][2]); // B
  2965. // printf("%1.3f,", f[0]); // R
  2966. // printf("%1.3f,", f[1]); // G
  2967. // printf("%1.3f :",f[2]); // B
  2968. // printf("%04x, ", CGU_UINT32(FloatToHalf(f[0]))); // R
  2969. // printf("%04x," , CGU_UINT32(FloatToHalf(f[1]))); // G
  2970. // printf("%04x\n", CGU_UINT32(FloatToHalf(f[2]))); // B
  2971. // }
  2972. #ifdef USE_NEW_SINGLE_HEADER_INTERFACES
  2973. CGU_Vec3f image_src[16];
  2974. for (int i = 0; i < 16; i++)
  2975. {
  2976. image_src[i].x = HalfToFloat((CGU_UINT32)BC6HEncode_local->din[i][0]);
  2977. image_src[i].y = HalfToFloat((CGU_UINT32)BC6HEncode_local->din[i][1]);
  2978. image_src[i].z = HalfToFloat((CGU_UINT32)BC6HEncode_local->din[i][2]);
  2979. }
  2980. CGU_Vec4ui cmp = CompressBlockBC6H_UNORM(image_src, 1.0f);
  2981. outdata[destIdx + 0] = cmp.x & 0xFF;
  2982. outdata[destIdx + 1] = (cmp.x >> 8) & 0xFF;
  2983. outdata[destIdx + 2] = (cmp.x >> 16) & 0xFF;
  2984. outdata[destIdx + 3] = (cmp.x >> 24) & 0xFF;
  2985. outdata[destIdx + 4] = cmp.y & 0xFF;
  2986. outdata[destIdx + 5] = (cmp.y >> 8) & 0xFF;
  2987. outdata[destIdx + 6] = (cmp.y >> 16) & 0xFF;
  2988. outdata[destIdx + 7] = (cmp.y >> 24) & 0xFF;
  2989. outdata[destIdx + 8] = cmp.z & 0xFF;
  2990. outdata[destIdx + 9] = (cmp.z >> 8) & 0xFF;
  2991. outdata[destIdx + 10] = (cmp.z >> 16) & 0xFF;
  2992. outdata[destIdx + 11] = (cmp.z >> 24) & 0xFF;
  2993. outdata[destIdx + 12] = cmp.w & 0xFF;
  2994. outdata[destIdx + 13] = (cmp.w >> 8) & 0xFF;
  2995. outdata[destIdx + 14] = (cmp.w >> 16) & 0xFF;
  2996. outdata[destIdx + 15] = (cmp.w >> 24) & 0xFF;
  2997. return;
  2998. #else
  2999. CGU_UINT8 Cmp_Red_Block[16] = {0xc2, 0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
  3000. CGU_FLOAT bestError = CMP_FLOAT_MAX;
  3001. CGU_FLOAT error = CMP_FLOAT_MAX;
  3002. CGU_INT8 bestShape = 0;
  3003. CGU_FLOAT quality = BC6HEncode->m_quality;
  3004. BC6HEncode_local->issigned = BC6HEncode->m_isSigned;
  3005. // run through no partition first
  3006. error = FindBestPattern(BC6HEncode_local, false, 0, quality);
  3007. if (error < bestError)
  3008. {
  3009. bestError = error;
  3010. bestShape = -1;
  3011. memcpy((CGU_UINT8*)BC6HEncode_local->cur_best_shape_indices, (CGU_UINT8*)BC6HEncode_local->shape_indices, sizeof(BC6HEncode_local->shape_indices));
  3012. memcpy((CGU_UINT8*)BC6HEncode_local->cur_best_partition, (CGU_UINT8*)BC6HEncode_local->partition, sizeof(BC6HEncode_local->partition));
  3013. memcpy((CGU_UINT8*)BC6HEncode_local->cur_best_fEndPoints, (CGU_UINT8*)BC6HEncode_local->fEndPoints, sizeof(BC6HEncode_local->fEndPoints));
  3014. memcpy((CGU_UINT8*)BC6HEncode_local->cur_best_entryCount, (CGU_UINT8*)BC6HEncode_local->entryCount, sizeof(BC6HEncode_local->entryCount));
  3015. BC6HEncode_local->d_shape_index = bestShape;
  3016. }
  3017. // run through 32 possible partition set
  3018. for (CGU_INT8 shape = 0; shape < MAX_BC6H_PARTITIONS; shape++)
  3019. {
  3020. error = FindBestPattern(BC6HEncode_local, true, shape, quality);
  3021. if (error < bestError)
  3022. {
  3023. bestError = error;
  3024. bestShape = shape;
  3025. memcpy((CGU_UINT8*)BC6HEncode_local->cur_best_shape_indices, (CGU_UINT8*)BC6HEncode_local->shape_indices, sizeof(BC6HEncode_local->shape_indices));
  3026. memcpy((CGU_UINT8*)BC6HEncode_local->cur_best_partition, (CGU_UINT8*)BC6HEncode_local->partition, sizeof(BC6HEncode_local->partition));
  3027. memcpy((CGU_UINT8*)BC6HEncode_local->cur_best_fEndPoints, (CGU_UINT8*)BC6HEncode_local->fEndPoints, sizeof(BC6HEncode_local->fEndPoints));
  3028. memcpy((CGU_UINT8*)BC6HEncode_local->cur_best_entryCount, (CGU_UINT8*)BC6HEncode_local->entryCount, sizeof(BC6HEncode_local->entryCount));
  3029. BC6HEncode_local->d_shape_index = bestShape;
  3030. }
  3031. else
  3032. {
  3033. if (bestShape != -1)
  3034. {
  3035. BC6HEncode_local->d_shape_index = bestShape;
  3036. memcpy(
  3037. (CGU_UINT8*)BC6HEncode_local->shape_indices, (CGU_UINT8*)BC6HEncode_local->cur_best_shape_indices, sizeof(BC6HEncode_local->shape_indices));
  3038. memcpy((CGU_UINT8*)BC6HEncode_local->partition, (CGU_UINT8*)BC6HEncode_local->cur_best_partition, sizeof(BC6HEncode_local->partition));
  3039. memcpy((CGU_UINT8*)BC6HEncode_local->fEndPoints, (CGU_UINT8*)BC6HEncode_local->cur_best_fEndPoints, sizeof(BC6HEncode_local->fEndPoints));
  3040. memcpy((CGU_UINT8*)BC6HEncode_local->entryCount, (CGU_UINT8*)BC6HEncode_local->cur_best_entryCount, sizeof(BC6HEncode_local->entryCount));
  3041. }
  3042. }
  3043. }
  3044. bestError = EncodePattern(BC6HEncode_local, bestError);
  3045. // used for debugging modes, set the value you want to debug with
  3046. if (BC6HEncode_local->m_mode != 0)
  3047. {
  3048. // do final encoding and save to output block
  3049. SaveDataBlock(BC6HEncode_local, &outdata[destIdx]);
  3050. }
  3051. else
  3052. {
  3053. for (CGU_INT i = 0; i < 16; i++)
  3054. outdata[destIdx + i] = Cmp_Red_Block[i];
  3055. }
  3056. // for (CGU_INT i = 0; i < 16; i++)
  3057. // printf("data[%d] = %x\n", i,outdata[i]);
  3058. #endif
  3059. }
  3060. //============================================== USER INTERFACES ========================================================
  3061. #ifndef ASPM_GPU
  3062. #ifndef ASPM
  3063. //======================= DECOMPRESS =========================================
  3064. static AMD_BC6H_Format extract_format(const CGU_UINT8 in[COMPRESSED_BLOCK_SIZE])
  3065. {
  3066. AMD_BC6H_Format bc6h_format;
  3067. unsigned short decvalue;
  3068. CGU_UINT8 iData[COMPRESSED_BLOCK_SIZE];
  3069. memcpy(iData, in, COMPRESSED_BLOCK_SIZE);
  3070. memset(&bc6h_format, 0, sizeof(AMD_BC6H_Format));
  3071. // 2 bit mode has Mode bit:2 = 0 and mode bits:1 = 0 or 1
  3072. // 5 bit mode has Mode bit:2 = 1
  3073. if ((in[0] & 0x02) > 0)
  3074. {
  3075. decvalue = (in[0] & 0x1F); // first five bits
  3076. }
  3077. else
  3078. {
  3079. decvalue = (in[0] & 0x01); // first two bits
  3080. }
  3081. BitHeader header(in, 16);
  3082. switch (decvalue)
  3083. {
  3084. case 0x00:
  3085. bc6h_format.m_mode = 1; // 10:5:5:5
  3086. bc6h_format.wBits = 10;
  3087. bc6h_format.tBits[C_RED] = 5;
  3088. bc6h_format.tBits[C_GREEN] = 5;
  3089. bc6h_format.tBits[C_BLUE] = 5;
  3090. bc6h_format.rw = header.getvalue(5, 10); // 10: rw[9:0]
  3091. bc6h_format.rx = header.getvalue(35, 5); // 5: rx[4:0]
  3092. bc6h_format.ry = header.getvalue(65, 5); // 5: ry[4:0]
  3093. bc6h_format.rz = header.getvalue(71, 5); // 5: rz[4:0]
  3094. bc6h_format.gw = header.getvalue(15, 10); // 10: gw[9:0]
  3095. bc6h_format.gx = header.getvalue(45, 5); // 5: gx[4:0]
  3096. bc6h_format.gy = header.getvalue(41, 4) | // 5: gy[3:0]
  3097. (header.getvalue(2, 1) << 4); // gy[4]
  3098. bc6h_format.gz = header.getvalue(51, 4) | // 5: gz[3:0]
  3099. (header.getvalue(40, 1) << 4); // gz[4]
  3100. bc6h_format.bw = header.getvalue(25, 10); // 10: bw[9:0]
  3101. bc6h_format.bx = header.getvalue(55, 5); // 5: bx[4:0]
  3102. bc6h_format.by = header.getvalue(61, 4) | // 5: by[3:0]
  3103. (header.getvalue(3, 1) << 4); // by[4]
  3104. bc6h_format.bz = header.getvalue(50, 1) | // 5: bz[0]
  3105. (header.getvalue(60, 1) << 1) | // bz[1]
  3106. (header.getvalue(70, 1) << 2) | // bz[2]
  3107. (header.getvalue(76, 1) << 3) | // bz[3]
  3108. (header.getvalue(4, 1) << 4); // bz[4]
  3109. break;
  3110. case 0x01:
  3111. bc6h_format.m_mode = 2; // 7:6:6:6
  3112. bc6h_format.wBits = 7;
  3113. bc6h_format.tBits[C_RED] = 6;
  3114. bc6h_format.tBits[C_GREEN] = 6;
  3115. bc6h_format.tBits[C_BLUE] = 6;
  3116. bc6h_format.rw = header.getvalue(5, 7); // 7: rw[6:0]
  3117. bc6h_format.rx = header.getvalue(35, 6); // 6: rx[5:0]
  3118. bc6h_format.ry = header.getvalue(65, 6); // 6: ry[5:0]
  3119. bc6h_format.rz = header.getvalue(71, 6); // 6: rz[5:0]
  3120. bc6h_format.gw = header.getvalue(15, 7); // 7: gw[6:0]
  3121. bc6h_format.gx = header.getvalue(45, 6); // 6: gx[5:0]
  3122. bc6h_format.gy = header.getvalue(41, 4) | // 6: gy[3:0]
  3123. (header.getvalue(24, 1) << 4) | // gy[4]
  3124. (header.getvalue(2, 1) << 5); // gy[5]
  3125. bc6h_format.gz = header.getvalue(51, 4) | // 6: gz[3:0]
  3126. (header.getvalue(3, 1) << 4) | // gz[4]
  3127. (header.getvalue(4, 1) << 5); // gz[5]
  3128. bc6h_format.bw = header.getvalue(25, 7); // 7: bw[6:0]
  3129. bc6h_format.bx = header.getvalue(55, 6); // 6: bx[5:0]
  3130. bc6h_format.by = header.getvalue(61, 4) | // 6: by[3:0]
  3131. (header.getvalue(14, 1) << 4) | // by[4]
  3132. (header.getvalue(22, 1) << 5); // by[5]
  3133. bc6h_format.bz = header.getvalue(12, 1) | // 6: bz[0]
  3134. (header.getvalue(13, 1) << 1) | // bz[1]
  3135. (header.getvalue(23, 1) << 2) | // bz[2]
  3136. (header.getvalue(32, 1) << 3) | // bz[3]
  3137. (header.getvalue(34, 1) << 4) | // bz[4]
  3138. (header.getvalue(33, 1) << 5); // bz[5]
  3139. break;
  3140. case 0x02:
  3141. bc6h_format.m_mode = 3; // 11:5:4:4
  3142. bc6h_format.wBits = 11;
  3143. bc6h_format.tBits[C_RED] = 5;
  3144. bc6h_format.tBits[C_GREEN] = 4;
  3145. bc6h_format.tBits[C_BLUE] = 4;
  3146. bc6h_format.rw = header.getvalue(5, 10) | //11: rw[9:0]
  3147. (header.getvalue(40, 1) << 10); // rw[10]
  3148. bc6h_format.rx = header.getvalue(35, 5); // 5: rx[4:0]
  3149. bc6h_format.ry = header.getvalue(65, 5); // 5: ry[4:0]
  3150. bc6h_format.rz = header.getvalue(71, 5); // 5: rz[4:0]
  3151. bc6h_format.gw = header.getvalue(15, 10) | //11: gw[9:0]
  3152. (header.getvalue(49, 1) << 10); // gw[10]
  3153. bc6h_format.gx = header.getvalue(45, 4); //4: gx[3:0]
  3154. bc6h_format.gy = header.getvalue(41, 4); //4: gy[3:0]
  3155. bc6h_format.gz = header.getvalue(51, 4); //4: gz[3:0]
  3156. bc6h_format.bw = header.getvalue(25, 10) | //11: bw[9:0]
  3157. (header.getvalue(59, 1) << 10); // bw[10]
  3158. bc6h_format.bx = header.getvalue(55, 4); //4: bx[3:0]
  3159. bc6h_format.by = header.getvalue(61, 4); //4: by[3:0]
  3160. bc6h_format.bz = header.getvalue(50, 1) | //4: bz[0]
  3161. (header.getvalue(60, 1) << 1) | // bz[1]
  3162. (header.getvalue(70, 1) << 2) | // bz[2]
  3163. (header.getvalue(76, 1) << 3); // bz[3]
  3164. break;
  3165. case 0x06:
  3166. bc6h_format.m_mode = 4; // 11:4:5:4
  3167. bc6h_format.wBits = 11;
  3168. bc6h_format.tBits[C_RED] = 4;
  3169. bc6h_format.tBits[C_GREEN] = 5;
  3170. bc6h_format.tBits[C_BLUE] = 4;
  3171. bc6h_format.rw = header.getvalue(5, 10) | //11: rw[9:0]
  3172. (header.getvalue(39, 1) << 10); // rw[10]
  3173. bc6h_format.rx = header.getvalue(35, 4); //4: rx[3:0]
  3174. bc6h_format.ry = header.getvalue(65, 4); //4: ry[3:0]
  3175. bc6h_format.rz = header.getvalue(71, 4); //4: rz[3:0]
  3176. bc6h_format.gw = header.getvalue(15, 10) | //11: gw[9:0]
  3177. (header.getvalue(50, 1) << 10); // gw[10]
  3178. bc6h_format.gx = header.getvalue(45, 5); //5: gx[4:0]
  3179. bc6h_format.gy = header.getvalue(41, 4) | //5: gy[3:0]
  3180. (header.getvalue(75, 1) << 4); // gy[4]
  3181. bc6h_format.gz = header.getvalue(51, 4) | //5: gz[3:0]
  3182. (header.getvalue(40, 1) << 4); // gz[4]
  3183. bc6h_format.bw = header.getvalue(25, 10) | //11: bw[9:0]
  3184. (header.getvalue(59, 1) << 10); // bw[10]
  3185. bc6h_format.bx = header.getvalue(55, 4); //4: bx[3:0]
  3186. bc6h_format.by = header.getvalue(61, 4); //4: by[3:0]
  3187. bc6h_format.bz = header.getvalue(69, 1) | //4: bz[0]
  3188. (header.getvalue(60, 1) << 1) | // bz[1]
  3189. (header.getvalue(70, 1) << 2) | // bz[2]
  3190. (header.getvalue(76, 1) << 3); // bz[3]
  3191. break;
  3192. case 0x0A:
  3193. bc6h_format.m_mode = 5; // 11:4:4:5
  3194. bc6h_format.wBits = 11;
  3195. bc6h_format.tBits[C_RED] = 4;
  3196. bc6h_format.tBits[C_GREEN] = 4;
  3197. bc6h_format.tBits[C_BLUE] = 5;
  3198. bc6h_format.rw = header.getvalue(5, 10) | //11: rw[9:0]
  3199. (header.getvalue(39, 1) << 10); // rw[10]
  3200. bc6h_format.rx = header.getvalue(35, 4); //4: rx[3:0]
  3201. bc6h_format.ry = header.getvalue(65, 4); //4: ry[3:0]
  3202. bc6h_format.rz = header.getvalue(71, 4); //4: rz[3:0]
  3203. bc6h_format.gw = header.getvalue(15, 10) | //11: gw[9:0]
  3204. (header.getvalue(49, 1) << 10); // gw[10]
  3205. bc6h_format.gx = header.getvalue(45, 4); //4: gx[3:0]
  3206. bc6h_format.gy = header.getvalue(41, 4); //4: gy[3:0]
  3207. bc6h_format.gz = header.getvalue(51, 4); //4: gz[3:0]
  3208. bc6h_format.bw = header.getvalue(25, 10) | //11: bw[9:0]
  3209. (header.getvalue(60, 1) << 10); // bw[10]
  3210. bc6h_format.bx = header.getvalue(55, 5); //5: bx[4:0]
  3211. bc6h_format.by = header.getvalue(61, 4); //5: by[3:0]
  3212. (header.getvalue(40, 1) << 4); // by[4]
  3213. bc6h_format.bz = header.getvalue(50, 1) | //5: bz[0]
  3214. (header.getvalue(69, 1) << 1) | // bz[1]
  3215. (header.getvalue(70, 1) << 2) | // bz[2]
  3216. (header.getvalue(76, 1) << 3) | // bz[3]
  3217. (header.getvalue(75, 1) << 4); // bz[4]
  3218. break;
  3219. case 0x0E:
  3220. bc6h_format.m_mode = 6; // 9:5:5:5
  3221. bc6h_format.wBits = 9;
  3222. bc6h_format.tBits[C_RED] = 5;
  3223. bc6h_format.tBits[C_GREEN] = 5;
  3224. bc6h_format.tBits[C_BLUE] = 5;
  3225. bc6h_format.rw = header.getvalue(5, 9); //9: rw[8:0]
  3226. bc6h_format.gw = header.getvalue(15, 9); //9: gw[8:0]
  3227. bc6h_format.bw = header.getvalue(25, 9); //9: bw[8:0]
  3228. bc6h_format.rx = header.getvalue(35, 5); //5: rx[4:0]
  3229. bc6h_format.gx = header.getvalue(45, 5); //5: gx[4:0]
  3230. bc6h_format.bx = header.getvalue(55, 5); //5: bx[4:0]
  3231. bc6h_format.ry = header.getvalue(65, 5); //5: ry[4:0]
  3232. bc6h_format.gy = header.getvalue(41, 4) | //5: gy[3:0]
  3233. (header.getvalue(24, 1) << 4); // gy[4]
  3234. bc6h_format.by = header.getvalue(61, 4) | //5: by[3:0]
  3235. (header.getvalue(14, 1) << 4); // by[4]
  3236. bc6h_format.rz = header.getvalue(71, 5); //5: rz[4:0]
  3237. bc6h_format.gz = header.getvalue(51, 4) | //5: gz[3:0]
  3238. (header.getvalue(40, 1) << 4); // gz[4]
  3239. bc6h_format.bz = header.getvalue(50, 1) | //5: bz[0]
  3240. (header.getvalue(60, 1) << 1) | // bz[1]
  3241. (header.getvalue(70, 1) << 2) | // bz[2]
  3242. (header.getvalue(76, 1) << 3) | // bz[3]
  3243. (header.getvalue(34, 1) << 4); // bz[4]
  3244. break;
  3245. case 0x12:
  3246. bc6h_format.m_mode = 7; // 8:6:5:5
  3247. bc6h_format.wBits = 8;
  3248. bc6h_format.tBits[C_RED] = 6;
  3249. bc6h_format.tBits[C_GREEN] = 5;
  3250. bc6h_format.tBits[C_BLUE] = 5;
  3251. bc6h_format.rw = header.getvalue(5, 8); //8: rw[7:0]
  3252. bc6h_format.gw = header.getvalue(15, 8); //8: gw[7:0]
  3253. bc6h_format.bw = header.getvalue(25, 8); //8: bw[7:0]
  3254. bc6h_format.rx = header.getvalue(35, 6); //6: rx[5:0]
  3255. bc6h_format.gx = header.getvalue(45, 5); //5: gx[4:0]
  3256. bc6h_format.bx = header.getvalue(55, 5); //5: bx[4:0]
  3257. bc6h_format.ry = header.getvalue(65, 6); //6: ry[5:0]
  3258. bc6h_format.gy = header.getvalue(41, 4) | //5: gy[3:0]
  3259. (header.getvalue(24, 1) << 4); // gy[4]
  3260. bc6h_format.by = header.getvalue(61, 4) | //5: by[3:0]
  3261. (header.getvalue(14, 1) << 4); // by[4]
  3262. bc6h_format.rz = header.getvalue(71, 6); //6: rz[5:0]
  3263. bc6h_format.gz = header.getvalue(51, 4) | //5: gz[3:0]
  3264. (header.getvalue(13, 1) << 4); // gz[4]
  3265. bc6h_format.bz = header.getvalue(50, 1) | //5: bz[0]
  3266. (header.getvalue(60, 1) << 1) | // bz[1]
  3267. (header.getvalue(23, 1) << 2) | // bz[2]
  3268. (header.getvalue(33, 1) << 3) | // bz[3]
  3269. (header.getvalue(34, 1) << 4); // bz[4]
  3270. break;
  3271. case 0x16:
  3272. bc6h_format.m_mode = 8; // 8:5:6:5
  3273. bc6h_format.wBits = 8;
  3274. bc6h_format.tBits[C_RED] = 5;
  3275. bc6h_format.tBits[C_GREEN] = 6;
  3276. bc6h_format.tBits[C_BLUE] = 5;
  3277. bc6h_format.rw = header.getvalue(5, 8); //8: rw[7:0]
  3278. bc6h_format.gw = header.getvalue(15, 8); //8: gw[7:0]
  3279. bc6h_format.bw = header.getvalue(25, 8); //8: bw[7:0]
  3280. bc6h_format.rx = header.getvalue(35, 5); //5: rx[4:0]
  3281. bc6h_format.gx = header.getvalue(45, 6); //6: gx[5:0]
  3282. bc6h_format.bx = header.getvalue(55, 5); //5: bx[4:0]
  3283. bc6h_format.ry = header.getvalue(65, 5); //5: ry[4:0]
  3284. bc6h_format.gy = header.getvalue(41, 4) | //6: gy[3:0]
  3285. (header.getvalue(24, 1) << 4) | // gy[4]
  3286. (header.getvalue(23, 1) << 5); // gy[5]
  3287. bc6h_format.by = header.getvalue(61, 4) | //5: by[3:0]
  3288. (header.getvalue(14, 1) << 4); // by[4]
  3289. bc6h_format.rz = header.getvalue(71, 5); //5: rz[4:0]
  3290. bc6h_format.gz = header.getvalue(51, 4) | //6: gz[3:0]
  3291. (header.getvalue(40, 1) << 4) | // gz[4]
  3292. (header.getvalue(33, 1) << 5); // gz[5]
  3293. bc6h_format.bz = header.getvalue(13, 1) | //5: bz[0]
  3294. (header.getvalue(60, 1) << 1) | // bz[1]
  3295. (header.getvalue(70, 1) << 2) | // bz[2]
  3296. (header.getvalue(76, 1) << 3) | // bz[3]
  3297. (header.getvalue(34, 1) << 4); // bz[4]
  3298. break;
  3299. case 0x1A:
  3300. bc6h_format.m_mode = 9; // 8:5:5:6
  3301. bc6h_format.wBits = 8;
  3302. bc6h_format.tBits[C_RED] = 5;
  3303. bc6h_format.tBits[C_GREEN] = 5;
  3304. bc6h_format.tBits[C_BLUE] = 6;
  3305. bc6h_format.rw = header.getvalue(5, 8); //8: rw[7:0]
  3306. bc6h_format.gw = header.getvalue(15, 8); //8: gw[7:0]
  3307. bc6h_format.bw = header.getvalue(25, 8); //8: bw[7:0]
  3308. bc6h_format.rx = header.getvalue(35, 5); //5: rx[4:0]
  3309. bc6h_format.gx = header.getvalue(45, 5); //5: gx[4:0]
  3310. bc6h_format.bx = header.getvalue(55, 6); //6: bx[5:0]
  3311. bc6h_format.ry = header.getvalue(65, 5); //5: ry[4:0]
  3312. bc6h_format.gy = header.getvalue(41, 4) | //5: gy[3:0]
  3313. (header.getvalue(24, 1) << 4); // gy[4]
  3314. bc6h_format.by = header.getvalue(61, 4) | //6: by[3:0]
  3315. (header.getvalue(14, 1) << 4) | // by[4]
  3316. (header.getvalue(23, 1) << 5); // by[5]
  3317. bc6h_format.rz = header.getvalue(71, 5); //5: rz[4:0]
  3318. bc6h_format.gz = header.getvalue(51, 4) | //5: gz[3:0]
  3319. (header.getvalue(40, 1) << 4); // gz[4]
  3320. bc6h_format.bz = header.getvalue(50, 1) | //6: bz[0]
  3321. (header.getvalue(13, 1) << 1) | // bz[1]
  3322. (header.getvalue(70, 1) << 2) | // bz[2]
  3323. (header.getvalue(76, 1) << 3) | // bz[3]
  3324. (header.getvalue(34, 1) << 4) | // bz[4]
  3325. (header.getvalue(33, 1) << 5); // bz[5]
  3326. break;
  3327. case 0x1E:
  3328. bc6h_format.m_mode = 10; // 6:6:6:6
  3329. bc6h_format.istransformed = FALSE;
  3330. bc6h_format.wBits = 6;
  3331. bc6h_format.tBits[C_RED] = 6;
  3332. bc6h_format.tBits[C_GREEN] = 6;
  3333. bc6h_format.tBits[C_BLUE] = 6;
  3334. bc6h_format.rw = header.getvalue(5, 6); //6: rw[5:0]
  3335. bc6h_format.gw = header.getvalue(15, 6); //6: gw[5:0]
  3336. bc6h_format.bw = header.getvalue(25, 6); //6: bw[5:0]
  3337. bc6h_format.rx = header.getvalue(35, 6); //6: rx[5:0]
  3338. bc6h_format.gx = header.getvalue(45, 6); //6: gx[5:0]
  3339. bc6h_format.bx = header.getvalue(55, 6); //6: bx[5:0]
  3340. bc6h_format.ry = header.getvalue(65, 6); //6: ry[5:0]
  3341. bc6h_format.gy = header.getvalue(41, 4) | //6: gy[3:0]
  3342. (header.getvalue(24, 1) << 4) | // gy[4]
  3343. (header.getvalue(21, 1) << 5); // gy[5]
  3344. bc6h_format.by = header.getvalue(61, 4) | //6: by[3:0]
  3345. (header.getvalue(14, 1) << 4) | // by[4]
  3346. (header.getvalue(22, 1) << 5); // by[5]
  3347. bc6h_format.rz = header.getvalue(71, 6); //6: rz[5:0]
  3348. bc6h_format.gz = header.getvalue(51, 4) | //6: gz[3:0]
  3349. (header.getvalue(11, 1) << 4) | // gz[4]
  3350. (header.getvalue(31, 1) << 5); // gz[5]
  3351. bc6h_format.bz = header.getvalue(12, 1) | //6: bz[0]
  3352. (header.getvalue(13, 1) << 1) | // bz[1]
  3353. (header.getvalue(23, 1) << 2) | // bz[2]
  3354. (header.getvalue(32, 1) << 3) | // bz[3]
  3355. (header.getvalue(34, 1) << 4) | // bz[4]
  3356. (header.getvalue(33, 1) << 5); // bz[5]
  3357. break;
  3358. // Single region modes
  3359. case 0x03:
  3360. bc6h_format.m_mode = 11; // 10:10
  3361. bc6h_format.wBits = 10;
  3362. bc6h_format.tBits[C_RED] = 10;
  3363. bc6h_format.tBits[C_GREEN] = 10;
  3364. bc6h_format.tBits[C_BLUE] = 10;
  3365. bc6h_format.rw = header.getvalue(5, 10); // 10: rw[9:0]
  3366. bc6h_format.gw = header.getvalue(15, 10); // 10: gw[9:0]
  3367. bc6h_format.bw = header.getvalue(25, 10); // 10: bw[9:0]
  3368. bc6h_format.rx = header.getvalue(35, 10); // 10: rx[9:0]
  3369. bc6h_format.gx = header.getvalue(45, 10); // 10: gx[9:0]
  3370. bc6h_format.bx = header.getvalue(55, 10); // 10: bx[9:0]
  3371. break;
  3372. case 0x07:
  3373. bc6h_format.m_mode = 12; // 11:9
  3374. bc6h_format.wBits = 11;
  3375. bc6h_format.tBits[C_RED] = 9;
  3376. bc6h_format.tBits[C_GREEN] = 9;
  3377. bc6h_format.tBits[C_BLUE] = 9;
  3378. bc6h_format.rw = header.getvalue(5, 10) | // 10: rw[9:0]
  3379. (header.getvalue(44, 1) << 10); // rw[10]
  3380. bc6h_format.gw = header.getvalue(15, 10) | // 10: gw[9:0]
  3381. (header.getvalue(54, 1) << 10); // gw[10]
  3382. bc6h_format.bw = header.getvalue(25, 10) | // 10: bw[9:0]
  3383. (header.getvalue(64, 1) << 10); // bw[10]
  3384. bc6h_format.rx = header.getvalue(35, 9); // 9: rx[8:0]
  3385. bc6h_format.gx = header.getvalue(45, 9); // 9: gx[8:0]
  3386. bc6h_format.bx = header.getvalue(55, 9); // 9: bx[8:0]
  3387. break;
  3388. case 0x0B:
  3389. bc6h_format.m_mode = 13; // 12:8
  3390. bc6h_format.wBits = 12;
  3391. bc6h_format.tBits[C_RED] = 8;
  3392. bc6h_format.tBits[C_GREEN] = 8;
  3393. bc6h_format.tBits[C_BLUE] = 8;
  3394. bc6h_format.rw = header.getvalue(5, 10) | // 12: rw[9:0]
  3395. (header.getvalue(43, 1) << 11) | // rw[11]
  3396. (header.getvalue(44, 1) << 10); // rw[10]
  3397. bc6h_format.gw = header.getvalue(15, 10) | // 12: gw[9:0]
  3398. (header.getvalue(53, 1) << 11) | // gw[11]
  3399. (header.getvalue(54, 1) << 10); // gw[10]
  3400. bc6h_format.bw = header.getvalue(25, 10) | // 12: bw[9:0]
  3401. (header.getvalue(63, 1) << 11) | // bw[11]
  3402. (header.getvalue(64, 1) << 10); // bw[10]
  3403. bc6h_format.rx = header.getvalue(35, 8); // 8: rx[7:0]
  3404. bc6h_format.gx = header.getvalue(45, 8); // 8: gx[7:0]
  3405. bc6h_format.bx = header.getvalue(55, 8); // 8: bx[7:0]
  3406. break;
  3407. case 0x0F:
  3408. bc6h_format.m_mode = 14; // 16:4
  3409. bc6h_format.wBits = 16;
  3410. bc6h_format.tBits[C_RED] = 4;
  3411. bc6h_format.tBits[C_GREEN] = 4;
  3412. bc6h_format.tBits[C_BLUE] = 4;
  3413. bc6h_format.rw = header.getvalue(5, 10) | // 16: rw[9:0]
  3414. (header.getvalue(39, 1) << 15) | // rw[15]
  3415. (header.getvalue(40, 1) << 14) | // rw[14]
  3416. (header.getvalue(41, 1) << 13) | // rw[13]
  3417. (header.getvalue(42, 1) << 12) | // rw[12]
  3418. (header.getvalue(43, 1) << 11) | // rw[11]
  3419. (header.getvalue(44, 1) << 10); // rw[10]
  3420. bc6h_format.gw = header.getvalue(15, 10) | // 16: gw[9:0]
  3421. (header.getvalue(49, 1) << 15) | // gw[15]
  3422. (header.getvalue(50, 1) << 14) | // gw[14]
  3423. (header.getvalue(51, 1) << 13) | // gw[13]
  3424. (header.getvalue(52, 1) << 12) | // gw[12]
  3425. (header.getvalue(53, 1) << 11) | // gw[11]
  3426. (header.getvalue(54, 1) << 10); // gw[10]
  3427. bc6h_format.bw = header.getvalue(25, 10) | // 16: bw[9:0]
  3428. (header.getvalue(59, 1) << 15) | // bw[15]
  3429. (header.getvalue(60, 1) << 14) | // bw[14]
  3430. (header.getvalue(61, 1) << 13) | // bw[13]
  3431. (header.getvalue(62, 1) << 12) | // bw[12]
  3432. (header.getvalue(63, 1) << 11) | // bw[11]
  3433. (header.getvalue(64, 1) << 10); // bw[10]
  3434. bc6h_format.rx = header.getvalue(35, 4); // 4: rx[3:0]
  3435. bc6h_format.gx = header.getvalue(45, 4); // 4: gx[3:0]
  3436. bc6h_format.bx = header.getvalue(55, 4); // 4: bx[3:0]
  3437. break;
  3438. default:
  3439. bc6h_format.m_mode = 0;
  3440. return bc6h_format;
  3441. }
  3442. // Each format in the mode table can be uniquely identified by the mode bits.
  3443. // The first ten modes are used for two-region tiles, and the mode bit field
  3444. // can be either two or five bits long. These blocks also have fields for
  3445. // the compressed color endpoints (72 or 75 bits), the partition (5 bits),
  3446. // and the partition indices (46 bits).
  3447. if (bc6h_format.m_mode <= 10)
  3448. {
  3449. bc6h_format.region = BC6_TWO;
  3450. // Get the shape index bits 77 to 81
  3451. bc6h_format.d_shape_index = (unsigned short)header.getvalue(77, 5);
  3452. bc6h_format.istransformed = (bc6h_format.m_mode < 10) ? TRUE : FALSE;
  3453. }
  3454. else
  3455. {
  3456. bc6h_format.region = BC6_ONE;
  3457. bc6h_format.d_shape_index = 0;
  3458. bc6h_format.istransformed = (bc6h_format.m_mode > 11) ? TRUE : FALSE;
  3459. }
  3460. // Save the points in a form easy to compute with
  3461. bc6h_format.EC[0].A[0] = (CGU_FLOAT)bc6h_format.rw;
  3462. bc6h_format.EC[0].B[0] = (CGU_FLOAT)bc6h_format.rx;
  3463. bc6h_format.EC[1].A[0] = (CGU_FLOAT)bc6h_format.ry;
  3464. bc6h_format.EC[1].B[0] = (CGU_FLOAT)bc6h_format.rz;
  3465. bc6h_format.EC[0].A[1] = (CGU_FLOAT)bc6h_format.gw;
  3466. bc6h_format.EC[0].B[1] = (CGU_FLOAT)bc6h_format.gx;
  3467. bc6h_format.EC[1].A[1] = (CGU_FLOAT)bc6h_format.gy;
  3468. bc6h_format.EC[1].B[1] = (CGU_FLOAT)bc6h_format.gz;
  3469. bc6h_format.EC[0].A[2] = (CGU_FLOAT)bc6h_format.bw;
  3470. bc6h_format.EC[0].B[2] = (CGU_FLOAT)bc6h_format.bx;
  3471. bc6h_format.EC[1].A[2] = (CGU_FLOAT)bc6h_format.by;
  3472. bc6h_format.EC[1].B[2] = (CGU_FLOAT)bc6h_format.bz;
  3473. if (bc6h_format.region == BC6_ONE)
  3474. {
  3475. int startbits = ONE_REGION_INDEX_OFFSET;
  3476. bc6h_format.indices16[0] = (CGU_UINT8)header.getvalue(startbits, 3);
  3477. startbits += 3;
  3478. for (int i = 1; i < 16; i++)
  3479. {
  3480. bc6h_format.indices16[i] = (CGU_UINT8)header.getvalue(startbits, 4);
  3481. startbits += 4;
  3482. }
  3483. }
  3484. else
  3485. {
  3486. int startbit = TWO_REGION_INDEX_OFFSET, nbits = 2;
  3487. bc6h_format.indices16[0] = (CGU_UINT8)header.getvalue(startbit, 2);
  3488. for (int i = 1; i < 16; i++)
  3489. {
  3490. startbit += nbits; // offset start bit for next index using prior nbits used
  3491. nbits = g_indexfixups[bc6h_format.d_shape_index] == i ? 2 : 3; // get new number of bit to save index with
  3492. bc6h_format.indices16[i] = (CGU_UINT8)header.getvalue(startbit, nbits);
  3493. }
  3494. }
  3495. return bc6h_format;
  3496. }
  3497. static void extract_compressed_endpoints(AMD_BC6H_Format& bc6h_format)
  3498. {
  3499. int i;
  3500. int t;
  3501. if (bc6h_format.issigned)
  3502. {
  3503. if (bc6h_format.istransformed)
  3504. {
  3505. for (i = 0; i < NCHANNELS; i++)
  3506. {
  3507. bc6h_format.E[0].A[i] = (CGU_FLOAT)SIGN_EXTEND(bc6h_format.EC[0].A[i], bc6h_format.wBits);
  3508. t = SIGN_EXTEND(bc6h_format.EC[0].B[i], bc6h_format.tBits[i]); //C_RED
  3509. t = int(t + bc6h_format.EC[0].A[i]) & MASK(bc6h_format.wBits);
  3510. bc6h_format.E[0].B[i] = (CGU_FLOAT)SIGN_EXTEND(t, bc6h_format.wBits);
  3511. }
  3512. }
  3513. else
  3514. {
  3515. for (i = 0; i < NCHANNELS; i++)
  3516. {
  3517. bc6h_format.E[0].A[i] = (CGU_FLOAT)SIGN_EXTEND(bc6h_format.EC[0].A[i], bc6h_format.wBits);
  3518. bc6h_format.E[0].B[i] = (CGU_FLOAT)SIGN_EXTEND(bc6h_format.EC[0].B[i], bc6h_format.tBits[i]); //C_RED
  3519. }
  3520. }
  3521. }
  3522. else
  3523. {
  3524. if (bc6h_format.istransformed)
  3525. {
  3526. for (i = 0; i < NCHANNELS; i++)
  3527. {
  3528. bc6h_format.E[0].A[i] = bc6h_format.EC[0].A[i];
  3529. t = SIGN_EXTEND(bc6h_format.EC[0].B[i], bc6h_format.tBits[i]); //C_RED
  3530. bc6h_format.E[0].B[i] = CGU_FLOAT(CGU_INT(t + bc6h_format.EC[0].A[i]) & MASK(bc6h_format.wBits));
  3531. }
  3532. }
  3533. else
  3534. {
  3535. for (i = 0; i < NCHANNELS; i++)
  3536. {
  3537. bc6h_format.E[0].A[i] = bc6h_format.EC[0].A[i];
  3538. bc6h_format.E[0].B[i] = bc6h_format.EC[0].B[i];
  3539. }
  3540. }
  3541. }
  3542. }
  3543. // NV code: Used with modifcations
  3544. static int unquantize(AMD_BC6H_Format& bc6h_format, int q, int prec)
  3545. {
  3546. int unq = 0, s;
  3547. switch (bc6h_format.format)
  3548. {
  3549. // modify this case to move the multiplication by 31 after interpolation.
  3550. // Need to use finish_unquantize.
  3551. // since we have 16 bits available, let's unquantize this to 16 bits unsigned
  3552. // thus the scale factor is [0-7c00)/[0-10000) = 31/64
  3553. case UNSIGNED_F16:
  3554. if (prec >= 15)
  3555. unq = q;
  3556. else if (q == 0)
  3557. unq = 0;
  3558. else if (q == ((1 << prec) - 1))
  3559. unq = U16MAX;
  3560. else
  3561. unq = (q * (U16MAX + 1) + (U16MAX + 1) / 2) >> prec;
  3562. break;
  3563. // here, let's stick with S16 (no apparent quality benefit from going to S17)
  3564. // range is (-7c00..7c00)/(-8000..8000) = 31/32
  3565. case SIGNED_F16:
  3566. // don't remove this test even though it appears equivalent to the code below
  3567. // as it isn't -- the code below can overflow for prec = 16
  3568. if (prec >= 16)
  3569. unq = q;
  3570. else
  3571. {
  3572. if (q < 0)
  3573. {
  3574. s = 1;
  3575. q = -q;
  3576. }
  3577. else
  3578. s = 0;
  3579. if (q == 0)
  3580. unq = 0;
  3581. else if (q >= ((1 << (prec - 1)) - 1))
  3582. unq = s ? -S16MAX : S16MAX;
  3583. else
  3584. {
  3585. unq = (q * (S16MAX + 1) + (S16MAX + 1) / 2) >> (prec - 1);
  3586. if (s)
  3587. unq = -unq;
  3588. }
  3589. }
  3590. break;
  3591. }
  3592. return unq;
  3593. }
  3594. static int lerp(int a, int b, int i, int denom)
  3595. {
  3596. assert(denom == 3 || denom == 7 || denom == 15);
  3597. assert(i >= 0 && i <= denom);
  3598. int shift = 6, *weights = NULL;
  3599. switch (denom)
  3600. {
  3601. case 3:
  3602. denom *= 5;
  3603. i *= 5; // fall through to case 15
  3604. case 15:
  3605. weights = g_aWeights4;
  3606. break;
  3607. case 7:
  3608. weights = g_aWeights3;
  3609. break;
  3610. default:
  3611. assert(0);
  3612. }
  3613. #pragma warning(disable : 4244)
  3614. // no need to round these as this is an exact division
  3615. return (int)(a * weights[denom - i] + b * weights[i]) / float(1 << shift);
  3616. }
  3617. static int finish_unquantize(AMD_BC6H_Format bc6h_format, int q)
  3618. {
  3619. if (bc6h_format.format == UNSIGNED_F16)
  3620. return (q * 31) >> 6; // scale the magnitude by 31/64
  3621. else if (bc6h_format.format == SIGNED_F16)
  3622. return (q < 0) ? -(((-q) * 31) >> 5) : (q * 31) >> 5; // scale the magnitude by 31/32
  3623. else
  3624. return q;
  3625. }
  3626. static void generate_palette_quantized(int max, AMD_BC6H_Format& bc6h_format, int region)
  3627. {
  3628. // scale endpoints
  3629. int a, b, c; // really need a IntVec3...
  3630. a = unquantize(bc6h_format, bc6h_format.E[region].A[0], bc6h_format.wBits);
  3631. b = unquantize(bc6h_format, bc6h_format.E[region].B[0], bc6h_format.wBits);
  3632. // interpolate : This part of code is used for debuging data
  3633. for (int i = 0; i < max; i++)
  3634. {
  3635. c = finish_unquantize(bc6h_format, lerp(a, b, i, max - 1));
  3636. bc6h_format.Palete[region][i].x = c;
  3637. }
  3638. a = unquantize(bc6h_format, bc6h_format.E[region].A[1], bc6h_format.wBits);
  3639. b = unquantize(bc6h_format, bc6h_format.E[region].B[1], bc6h_format.wBits);
  3640. // interpolate
  3641. for (int i = 0; i < max; i++)
  3642. bc6h_format.Palete[region][i].y = finish_unquantize(bc6h_format, lerp(a, b, i, max - 1));
  3643. a = unquantize(bc6h_format, bc6h_format.E[region].A[2], bc6h_format.wBits);
  3644. b = unquantize(bc6h_format, bc6h_format.E[region].B[2], bc6h_format.wBits);
  3645. // interpolate
  3646. for (int i = 0; i < max; i++)
  3647. bc6h_format.Palete[region][i].z = finish_unquantize(bc6h_format, lerp(a, b, i, max - 1));
  3648. }
  3649. // NV code : used with modifications
  3650. static void extract_compressed_endpoints2(AMD_BC6H_Format& bc6h_format)
  3651. {
  3652. int i;
  3653. int t;
  3654. if (bc6h_format.issigned)
  3655. {
  3656. if (bc6h_format.istransformed)
  3657. {
  3658. for (i = 0; i < NCHANNELS; i++)
  3659. {
  3660. bc6h_format.E[0].A[i] = SIGN_EXTEND(bc6h_format.EC[0].A[i], bc6h_format.wBits);
  3661. t = SIGN_EXTEND(bc6h_format.EC[0].B[i], bc6h_format.tBits[i]); // C_RED
  3662. t = int(t + bc6h_format.EC[0].A[i]) & MASK(bc6h_format.wBits);
  3663. bc6h_format.E[0].B[i] = SIGN_EXTEND(t, bc6h_format.wBits);
  3664. t = SIGN_EXTEND(bc6h_format.EC[1].A[i], bc6h_format.tBits[i]); //C_GREEN
  3665. t = int(t + bc6h_format.EC[0].A[i]) & MASK(bc6h_format.wBits);
  3666. bc6h_format.E[1].A[i] = SIGN_EXTEND(t, bc6h_format.wBits);
  3667. t = SIGN_EXTEND(bc6h_format.EC[1].B[i], bc6h_format.tBits[i]); //C_BLUE
  3668. t = int(t + bc6h_format.EC[0].A[i]) & MASK(bc6h_format.wBits);
  3669. bc6h_format.E[1].B[i] = SIGN_EXTEND(t, bc6h_format.wBits);
  3670. }
  3671. }
  3672. else
  3673. {
  3674. for (i = 0; i < NCHANNELS; i++)
  3675. {
  3676. bc6h_format.E[0].A[i] = SIGN_EXTEND(bc6h_format.EC[0].A[i], bc6h_format.wBits);
  3677. bc6h_format.E[0].B[i] = SIGN_EXTEND(bc6h_format.EC[0].B[i], bc6h_format.tBits[i]); //C_RED
  3678. bc6h_format.E[1].A[i] = SIGN_EXTEND(bc6h_format.EC[1].A[i], bc6h_format.tBits[i]); //C_GREEN
  3679. bc6h_format.E[1].B[i] = SIGN_EXTEND(bc6h_format.EC[1].B[i], bc6h_format.tBits[i]); //C_BLUE
  3680. }
  3681. }
  3682. }
  3683. else
  3684. {
  3685. if (bc6h_format.istransformed)
  3686. {
  3687. for (i = 0; i < NCHANNELS; i++)
  3688. {
  3689. bc6h_format.E[0].A[i] = bc6h_format.EC[0].A[i];
  3690. t = SIGN_EXTEND(bc6h_format.EC[0].B[i], bc6h_format.tBits[i]); // C_RED
  3691. bc6h_format.E[0].B[i] = int(t + bc6h_format.EC[0].A[i]) & MASK(bc6h_format.wBits);
  3692. t = SIGN_EXTEND(bc6h_format.EC[1].A[i], bc6h_format.tBits[i]); // C_GREEN
  3693. bc6h_format.E[1].A[i] = int(t + bc6h_format.EC[0].A[i]) & MASK(bc6h_format.wBits);
  3694. t = SIGN_EXTEND(bc6h_format.EC[1].B[i], bc6h_format.tBits[i]); //C_BLUE
  3695. bc6h_format.E[1].B[i] = int(t + bc6h_format.EC[0].A[i]) & MASK(bc6h_format.wBits);
  3696. }
  3697. }
  3698. else
  3699. {
  3700. for (i = 0; i < NCHANNELS; i++)
  3701. {
  3702. bc6h_format.E[0].A[i] = bc6h_format.EC[0].A[i];
  3703. bc6h_format.E[0].B[i] = bc6h_format.EC[0].B[i];
  3704. bc6h_format.E[1].A[i] = bc6h_format.EC[1].A[i];
  3705. bc6h_format.E[1].B[i] = bc6h_format.EC[1].B[i];
  3706. }
  3707. }
  3708. }
  3709. }
  3710. void DecompressBC6_Internal(CGU_UINT16 rgbBlock[48], const CGU_UINT8 compressedBlock[16], const BC6H_Encode* BC6HEncode)
  3711. {
  3712. if (BC6HEncode)
  3713. {
  3714. }
  3715. CGU_BOOL m_bc6signed = false;
  3716. // now determine the mode type and extract the coded endpoints data
  3717. AMD_BC6H_Format bc6h_format = extract_format(compressedBlock);
  3718. if (!m_bc6signed)
  3719. bc6h_format.format = UNSIGNED_F16;
  3720. else
  3721. bc6h_format.format = SIGNED_F16;
  3722. if (bc6h_format.region == BC6_ONE)
  3723. {
  3724. extract_compressed_endpoints(bc6h_format);
  3725. generate_palette_quantized(16, bc6h_format, 0);
  3726. }
  3727. else
  3728. { //mode.type == BC6_TWO
  3729. extract_compressed_endpoints2(bc6h_format);
  3730. for (int r = 0; r < 2; r++)
  3731. {
  3732. generate_palette_quantized(8, bc6h_format, r);
  3733. }
  3734. }
  3735. BC6H_Vec3 data;
  3736. int indexPos = 0;
  3737. int rgbPos = 0;
  3738. // Note first 32 BC6H_PARTIONS is shared with BC6H
  3739. // Partitioning is always arranged such that index 0 is always in subset 0 of BC6H_PARTIONS array
  3740. // Partition order goes from top-left to bottom-right, moving left to right and then top to bottom.
  3741. for (int block_row = 0; block_row < 4; block_row++)
  3742. for (int block_col = 0; block_col < 4; block_col++)
  3743. {
  3744. // Need to check region logic
  3745. // gets the region (0 or 1) in the partition set
  3746. //int region = bc6h_format.region == BC6_ONE?0:REGION(block_col,block_row,bc6h_format.d_shape_index);
  3747. // for a one region partitions : its always return 0 so there is room for performance improvement
  3748. // by seperating the condition into another looped call.
  3749. //int region = bc6h_format.region == BC6_ONE?0:BC6H_PARTITIONS[1][bc6h_format.d_shape_index][indexPos];
  3750. int region = bc6h_format.region == BC6_ONE ? 0 : BC6_PARTITIONS[bc6h_format.d_shape_index][indexPos];
  3751. // Index is validated as ok
  3752. int paleteIndex = bc6h_format.indices[block_row][block_col];
  3753. // this result is validated ok for region = BC6_ONE , BC6_TWO To be determined
  3754. data = bc6h_format.Palete[region][paleteIndex];
  3755. rgbBlock[rgbPos++] = data.x;
  3756. rgbBlock[rgbPos++] = data.y;
  3757. rgbBlock[rgbPos++] = data.z;
  3758. indexPos++;
  3759. }
  3760. }
  3761. //======================= END OF DECOMPRESS CODE =========================================
  3762. int CMP_CDECL CreateOptionsBC6(void** options)
  3763. {
  3764. (*options) = new BC6H_Encode;
  3765. if (!options)
  3766. return CGU_CORE_ERR_NEWMEM;
  3767. SetDefaultBC6Options((BC6H_Encode*)(*options));
  3768. return CGU_CORE_OK;
  3769. }
  3770. int CMP_CDECL DestroyOptionsBC6(void* options)
  3771. {
  3772. if (!options)
  3773. return CGU_CORE_ERR_INVALIDPTR;
  3774. BC6H_Encode* BCOptions = reinterpret_cast<BC6H_Encode*>(options);
  3775. delete BCOptions;
  3776. return CGU_CORE_OK;
  3777. }
  3778. int CMP_CDECL SetQualityBC6(void* options, CGU_FLOAT fquality)
  3779. {
  3780. if (!options)
  3781. return CGU_CORE_ERR_INVALIDPTR;
  3782. BC6H_Encode* BC6optionsDefault = (BC6H_Encode*)options;
  3783. if (fquality < 0.0f)
  3784. fquality = 0.0f;
  3785. else if (fquality > 1.0f)
  3786. fquality = 1.0f;
  3787. BC6optionsDefault->m_quality = fquality;
  3788. BC6optionsDefault->m_partitionSearchSize = (BC6optionsDefault->m_quality * 2.0F) / qFAST_THRESHOLD;
  3789. if (BC6optionsDefault->m_partitionSearchSize < (1.0F / 16.0F))
  3790. BC6optionsDefault->m_partitionSearchSize = (1.0F / 16.0F);
  3791. return CGU_CORE_OK;
  3792. }
  3793. int CMP_CDECL SetMaskBC6(void* options, CGU_UINT32 mask)
  3794. {
  3795. if (!options)
  3796. return CGU_CORE_ERR_INVALIDPTR;
  3797. BC6H_Encode* BC6options = (BC6H_Encode*)options;
  3798. BC6options->m_validModeMask = mask;
  3799. return CGU_CORE_OK;
  3800. }
  3801. int CMP_CDECL SetSignedBC6(void* options, CGU_BOOL sf16)
  3802. {
  3803. if (!options)
  3804. return CGU_CORE_ERR_INVALIDPTR;
  3805. BC6H_Encode* BC6options = (BC6H_Encode*)options;
  3806. BC6options->m_isSigned = sf16;
  3807. return CGU_CORE_OK;
  3808. }
  3809. int CMP_CDECL CompressBlockBC6(const CGU_UINT16* srcBlock,
  3810. unsigned int srcStrideInShorts,
  3811. CMP_GLOBAL CGU_UINT8 cmpBlock[16],
  3812. const CMP_GLOBAL void* options = NULL)
  3813. {
  3814. CGU_UINT16 inBlock[48];
  3815. //----------------------------------
  3816. // Fill the inBlock with source data
  3817. //----------------------------------
  3818. CGU_INT srcpos = 0;
  3819. CGU_INT dstptr = 0;
  3820. for (CGU_UINT8 row = 0; row < 4; row++)
  3821. {
  3822. srcpos = row * srcStrideInShorts;
  3823. for (CGU_UINT8 col = 0; col < 4; col++)
  3824. {
  3825. inBlock[dstptr++] = CGU_UINT16(srcBlock[srcpos++]);
  3826. inBlock[dstptr++] = CGU_UINT16(srcBlock[srcpos++]);
  3827. inBlock[dstptr++] = CGU_UINT16(srcBlock[srcpos++]);
  3828. }
  3829. }
  3830. BC6H_Encode* BC6HEncode = (BC6H_Encode*)options;
  3831. BC6H_Encode BC6HEncodeDefault;
  3832. if (BC6HEncode == NULL)
  3833. {
  3834. BC6HEncode = &BC6HEncodeDefault;
  3835. SetDefaultBC6Options(BC6HEncode);
  3836. }
  3837. BC6H_Encode_local BC6HEncode_local;
  3838. memset((CGU_UINT8*)&BC6HEncode_local, 0, sizeof(BC6H_Encode_local));
  3839. CGU_UINT8 blkindex = 0;
  3840. for (CGU_INT32 j = 0; j < 16; j++)
  3841. {
  3842. BC6HEncode_local.din[j][0] = inBlock[blkindex++]; // R
  3843. BC6HEncode_local.din[j][1] = inBlock[blkindex++]; // G
  3844. BC6HEncode_local.din[j][2] = inBlock[blkindex++]; // B
  3845. BC6HEncode_local.din[j][3] = 0; // A
  3846. }
  3847. CompressBlockBC6_Internal(cmpBlock, 0, &BC6HEncode_local, BC6HEncode);
  3848. return CGU_CORE_OK;
  3849. }
  3850. int CMP_CDECL DecompressBlockBC6(const unsigned char cmpBlock[16], CGU_UINT16 srcBlock[48], const void* options = NULL)
  3851. {
  3852. BC6H_Encode* BC6HEncode = (BC6H_Encode*)options;
  3853. BC6H_Encode BC6HEncodeDefault;
  3854. if (BC6HEncode == NULL)
  3855. {
  3856. BC6HEncode = &BC6HEncodeDefault;
  3857. SetDefaultBC6Options(BC6HEncode);
  3858. }
  3859. DecompressBC6_Internal(srcBlock, cmpBlock, BC6HEncode);
  3860. return CGU_CORE_OK;
  3861. }
  3862. #endif // !ASPM
  3863. #endif // !ASPM_GPU
  3864. //============================================== OpenCL USER INTERFACE ====================================================
  3865. #ifdef ASPM_OPENCL
  3866. CMP_STATIC CMP_KERNEL void CMP_GPUEncoder(CMP_GLOBAL CGU_UINT8* p_source_pixels,
  3867. CMP_GLOBAL CGU_UINT8* p_encoded_blocks,
  3868. CMP_GLOBAL Source_Info* SourceInfo,
  3869. CMP_GLOBAL BC6H_Encode* BC6HEncode)
  3870. {
  3871. CGU_UINT32 x = get_global_id(0);
  3872. CGU_UINT32 y = get_global_id(1);
  3873. if (x >= (SourceInfo->m_src_width / BYTEPP))
  3874. return;
  3875. if (y >= (SourceInfo->m_src_height / BYTEPP))
  3876. return;
  3877. BC6H_Encode_local BC6HEncode_local;
  3878. memset((CGU_UINT8*)&BC6HEncode_local, 0, sizeof(BC6H_Encode_local));
  3879. CGU_UINT32 stride = SourceInfo->m_src_width * BYTEPP;
  3880. CGU_UINT32 srcOffset = (x * BlockX * BYTEPP) + (y * stride * BYTEPP);
  3881. CGU_UINT32 destI = (x * COMPRESSED_BLOCK_SIZE) + (y * (SourceInfo->m_src_width / BlockX) * COMPRESSED_BLOCK_SIZE);
  3882. CGU_UINT32 srcidx;
  3883. //CGU_FLOAT block4x4[16][4];
  3884. for (CGU_INT i = 0; i < BlockX; i++)
  3885. {
  3886. srcidx = i * stride;
  3887. for (CGU_INT j = 0; j < BlockY; j++)
  3888. {
  3889. BC6HEncode_local.din[i * BlockX + j][0] = (CGU_UINT16)(p_source_pixels[srcOffset + srcidx++]);
  3890. if (BC6HEncode_local.din[i * BlockX + j][0] < 0.00001 || cmp_isnan(BC6HEncode_local.din[i * BlockX + j][0]))
  3891. {
  3892. if (BC6HEncode->m_isSigned)
  3893. {
  3894. BC6HEncode_local.din[i * BlockX + j][0] =
  3895. (cmp_isnan(BC6HEncode_local.din[i * BlockX + j][0])) ? F16NEGPREC_LIMIT_VAL : -BC6HEncode_local.din[i * BlockX + j][0];
  3896. if (BC6HEncode_local.din[i * BlockX + j][0] < F16NEGPREC_LIMIT_VAL)
  3897. {
  3898. BC6HEncode_local.din[i * BlockX + j][0] = F16NEGPREC_LIMIT_VAL;
  3899. }
  3900. }
  3901. else
  3902. BC6HEncode_local.din[i * BlockX + j][0] = 0.0;
  3903. }
  3904. BC6HEncode_local.din[i * BlockX + j][1] = (CGU_UINT16)(p_source_pixels[srcOffset + srcidx++]);
  3905. if (BC6HEncode_local.din[i * BlockX + j][1] < 0.00001 || cmp_isnan(BC6HEncode_local.din[i * BlockX + j][1]))
  3906. {
  3907. if (BC6HEncode->m_isSigned)
  3908. {
  3909. if (BC6HEncode_local.din[i * BlockX + j][1] < 0.00001 || cmp_isnan(BC6HEncode_local.din[i * BlockX + j][1]))
  3910. (isnan(BC6HEncode_local.din[i * BlockX + j][1])) ? F16NEGPREC_LIMIT_VAL : -BC6HEncode_local.din[i * BlockX + j][1];
  3911. BC6HEncode_local.din[i * BlockX + j][1] =
  3912. (cmp_isnan(BC6HEncode_local.din[i * BlockX + j][1])) ? F16NEGPREC_LIMIT_VAL : -BC6HEncode_local.din[i * BlockX + j][1];
  3913. if (BC6HEncode_local.din[i * BlockX + j][1] < F16NEGPREC_LIMIT_VAL)
  3914. {
  3915. BC6HEncode_local.din[i * BlockX + j][1] = F16NEGPREC_LIMIT_VAL;
  3916. }
  3917. }
  3918. else
  3919. BC6HEncode_local.din[i * BlockX + j][1] = 0.0;
  3920. }
  3921. BC6HEncode_local.din[i * BlockX + j][2] = (CGU_UINT16)(p_source_pixels[srcOffset + srcidx++]);
  3922. if (BC6HEncode_local.din[i * BlockX + j][2] < 0.00001 || isnan(BC6HEncode_local.din[i * BlockX + j][2]))
  3923. {
  3924. if (BC6HEncode->m_isSigned)
  3925. {
  3926. BC6HEncode_local.din[i * BlockX + j][2] =
  3927. (isnan(BC6HEncode_local.din[i * BlockX + j][2])) ? F16NEGPREC_LIMIT_VAL : -BC6HEncode_local.din[i * BlockX + j][2];
  3928. if (BC6HEncode_local.din[i * BlockX + j][2] < F16NEGPREC_LIMIT_VAL)
  3929. {
  3930. BC6HEncode_local.din[i * BlockX + j][2] = F16NEGPREC_LIMIT_VAL;
  3931. }
  3932. }
  3933. else
  3934. BC6HEncode_local.din[i * BlockX + j][2] = 0.0;
  3935. }
  3936. BC6HEncode_local.din[i * BlockX + j][3] = 0.0f;
  3937. //printf("Ori---src image %d, --%02x", x, (p_source_pixels[srcOffset + srcidx++]) & 0x0000ff); //for debug
  3938. }
  3939. }
  3940. // printf(" X %3d Y %3d Quality %2.2f", x, y, BC6HEncode->m_quality);
  3941. CompressBlockBC6_Internal(p_encoded_blocks, destI, &BC6HEncode_local, BC6HEncode);
  3942. }
  3943. #endif