FsrSharpening.glsl 238 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904
  1. // Sharpening
  2. #version 430 core
  3. layout (local_size_x = 64) in;
  4. layout( rgba8, binding = 0, set = 3) uniform image2D imgOutput;
  5. layout( binding = 2 ) uniform invResolution
  6. {
  7. vec2 invResolution_data;
  8. };
  9. layout( binding = 3 ) uniform outvResolution
  10. {
  11. vec2 outvResolution_data;
  12. };
  13. layout( binding = 1, set = 2) uniform sampler2D source;
  14. layout( binding = 4 ) uniform sharpening
  15. {
  16. float sharpening_data;
  17. };
  18. #define A_GPU 1
  19. #define A_GLSL 1
  20. //==============================================================================================================================
  21. //
  22. // [A] SHADER PORTABILITY 1.20210629
  23. //
  24. //==============================================================================================================================
  25. // FidelityFX Super Resolution Sample
  26. //
  27. // Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
  28. // Permission is hereby granted, free of charge, to any person obtaining a copy
  29. // of this software and associated documentation files(the "Software"), to deal
  30. // in the Software without restriction, including without limitation the rights
  31. // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
  32. // copies of the Software, and to permit persons to whom the Software is
  33. // furnished to do so, subject to the following conditions :
  34. // The above copyright notice and this permission notice shall be included in
  35. // all copies or substantial portions of the Software.
  36. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  37. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  38. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  39. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  40. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  41. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  42. // THE SOFTWARE.
  43. //------------------------------------------------------------------------------------------------------------------------------
  44. // MIT LICENSE
  45. // ===========
  46. // Copyright (c) 2014 Michal Drobot (for concepts used in "FLOAT APPROXIMATIONS").
  47. // -----------
  48. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
  49. // files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
  50. // modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  51. // Software is furnished to do so, subject to the following conditions:
  52. // -----------
  53. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  54. // Software.
  55. // -----------
  56. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  57. // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  58. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  59. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  60. //------------------------------------------------------------------------------------------------------------------------------
  61. // ABOUT
  62. // =====
  63. // Common central point for high-level shading language and C portability for various shader headers.
  64. //------------------------------------------------------------------------------------------------------------------------------
  65. // DEFINES
  66. // =======
  67. // A_CPU ..... Include the CPU related code.
  68. // A_GPU ..... Include the GPU related code.
  69. // A_GLSL .... Using GLSL.
  70. // A_HLSL .... Using HLSL.
  71. // A_HLSL_6_2 Using HLSL 6.2 with new 'uint16_t' and related types (requires '-enable-16bit-types').
  72. // A_NO_16_BIT_CAST Don't use instructions that are not availabe in SPIR-V (needed for running A_HLSL_6_2 on Vulkan)
  73. // A_GCC ..... Using a GCC compatible compiler (else assume MSVC compatible compiler by default).
  74. // =======
  75. // A_BYTE .... Support 8-bit integer.
  76. // A_HALF .... Support 16-bit integer and floating point.
  77. // A_LONG .... Support 64-bit integer.
  78. // A_DUBL .... Support 64-bit floating point.
  79. // =======
  80. // A_WAVE .... Support wave-wide operations.
  81. //------------------------------------------------------------------------------------------------------------------------------
  82. // To get #include "ffx_a.h" working in GLSL use '#extension GL_GOOGLE_include_directive:require'.
  83. //------------------------------------------------------------------------------------------------------------------------------
  84. // SIMPLIFIED TYPE SYSTEM
  85. // ======================
  86. // - All ints will be unsigned with exception of when signed is required.
  87. // - Type naming simplified and shortened "A<type><#components>",
  88. // - H = 16-bit float (half)
  89. // - F = 32-bit float (float)
  90. // - D = 64-bit float (double)
  91. // - P = 1-bit integer (predicate, not using bool because 'B' is used for byte)
  92. // - B = 8-bit integer (byte)
  93. // - W = 16-bit integer (word)
  94. // - U = 32-bit integer (unsigned)
  95. // - L = 64-bit integer (long)
  96. // - Using "AS<type><#components>" for signed when required.
  97. //------------------------------------------------------------------------------------------------------------------------------
  98. // TODO
  99. // ====
  100. // - Make sure 'ALerp*(a,b,m)' does 'b*m+(-a*m+a)' (2 ops).
  101. //------------------------------------------------------------------------------------------------------------------------------
  102. // CHANGE LOG
  103. // ==========
  104. // 20200914 - Expanded wave ops and prx code.
  105. // 20200713 - Added [ZOL] section, fixed serious bugs in sRGB and Rec.709 color conversion code, etc.
  106. //==============================================================================================================================
  107. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  108. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  109. //_____________________________________________________________/\_______________________________________________________________
  110. //==============================================================================================================================
  111. // COMMON
  112. //==============================================================================================================================
  113. #define A_2PI 6.28318530718
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  115. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  116. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  117. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  118. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  119. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  120. //_____________________________________________________________/\_______________________________________________________________
  121. //==============================================================================================================================
  122. //
  123. //
  124. // CPU
  125. //
  126. //
  127. //==============================================================================================================================
  128. #ifdef A_CPU
  129. // Supporting user defined overrides.
  130. #ifndef A_RESTRICT
  131. #define A_RESTRICT __restrict
  132. #endif
  133. //------------------------------------------------------------------------------------------------------------------------------
  134. #ifndef A_STATIC
  135. #define A_STATIC static
  136. #endif
  137. //------------------------------------------------------------------------------------------------------------------------------
  138. // Same types across CPU and GPU.
  139. // Predicate uses 32-bit integer (C friendly bool).
  140. typedef uint32_t AP1;
  141. typedef float AF1;
  142. typedef double AD1;
  143. typedef uint8_t AB1;
  144. typedef uint16_t AW1;
  145. typedef uint32_t AU1;
  146. typedef uint64_t AL1;
  147. typedef int8_t ASB1;
  148. typedef int16_t ASW1;
  149. typedef int32_t ASU1;
  150. typedef int64_t ASL1;
  151. //------------------------------------------------------------------------------------------------------------------------------
  152. #define AD1_(a) ((AD1)(a))
  153. #define AF1_(a) ((AF1)(a))
  154. #define AL1_(a) ((AL1)(a))
  155. #define AU1_(a) ((AU1)(a))
  156. //------------------------------------------------------------------------------------------------------------------------------
  157. #define ASL1_(a) ((ASL1)(a))
  158. #define ASU1_(a) ((ASU1)(a))
  159. //------------------------------------------------------------------------------------------------------------------------------
  160. A_STATIC AU1 AU1_AF1(AF1 a){union{AF1 f;AU1 u;}bits;bits.f=a;return bits.u;}
  161. //------------------------------------------------------------------------------------------------------------------------------
  162. #define A_TRUE 1
  163. #define A_FALSE 0
  164. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  165. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  166. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  167. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  168. //_____________________________________________________________/\_______________________________________________________________
  169. //==============================================================================================================================
  170. //
  171. // CPU/GPU PORTING
  172. //
  173. //------------------------------------------------------------------------------------------------------------------------------
  174. // Get CPU and GPU to share all setup code, without duplicate code paths.
  175. // This uses a lower-case prefix for special vector constructs.
  176. // - In C restrict pointers are used.
  177. // - In the shading language, in/inout/out arguments are used.
  178. // This depends on the ability to access a vector value in both languages via array syntax (aka color[2]).
  179. //==============================================================================================================================
  180. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  181. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  182. //_____________________________________________________________/\_______________________________________________________________
  183. //==============================================================================================================================
  184. // VECTOR ARGUMENT/RETURN/INITIALIZATION PORTABILITY
  185. //==============================================================================================================================
  186. #define retAD2 AD1 *A_RESTRICT
  187. #define retAD3 AD1 *A_RESTRICT
  188. #define retAD4 AD1 *A_RESTRICT
  189. #define retAF2 AF1 *A_RESTRICT
  190. #define retAF3 AF1 *A_RESTRICT
  191. #define retAF4 AF1 *A_RESTRICT
  192. #define retAL2 AL1 *A_RESTRICT
  193. #define retAL3 AL1 *A_RESTRICT
  194. #define retAL4 AL1 *A_RESTRICT
  195. #define retAU2 AU1 *A_RESTRICT
  196. #define retAU3 AU1 *A_RESTRICT
  197. #define retAU4 AU1 *A_RESTRICT
  198. //------------------------------------------------------------------------------------------------------------------------------
  199. #define inAD2 AD1 *A_RESTRICT
  200. #define inAD3 AD1 *A_RESTRICT
  201. #define inAD4 AD1 *A_RESTRICT
  202. #define inAF2 AF1 *A_RESTRICT
  203. #define inAF3 AF1 *A_RESTRICT
  204. #define inAF4 AF1 *A_RESTRICT
  205. #define inAL2 AL1 *A_RESTRICT
  206. #define inAL3 AL1 *A_RESTRICT
  207. #define inAL4 AL1 *A_RESTRICT
  208. #define inAU2 AU1 *A_RESTRICT
  209. #define inAU3 AU1 *A_RESTRICT
  210. #define inAU4 AU1 *A_RESTRICT
  211. //------------------------------------------------------------------------------------------------------------------------------
  212. #define inoutAD2 AD1 *A_RESTRICT
  213. #define inoutAD3 AD1 *A_RESTRICT
  214. #define inoutAD4 AD1 *A_RESTRICT
  215. #define inoutAF2 AF1 *A_RESTRICT
  216. #define inoutAF3 AF1 *A_RESTRICT
  217. #define inoutAF4 AF1 *A_RESTRICT
  218. #define inoutAL2 AL1 *A_RESTRICT
  219. #define inoutAL3 AL1 *A_RESTRICT
  220. #define inoutAL4 AL1 *A_RESTRICT
  221. #define inoutAU2 AU1 *A_RESTRICT
  222. #define inoutAU3 AU1 *A_RESTRICT
  223. #define inoutAU4 AU1 *A_RESTRICT
  224. //------------------------------------------------------------------------------------------------------------------------------
  225. #define outAD2 AD1 *A_RESTRICT
  226. #define outAD3 AD1 *A_RESTRICT
  227. #define outAD4 AD1 *A_RESTRICT
  228. #define outAF2 AF1 *A_RESTRICT
  229. #define outAF3 AF1 *A_RESTRICT
  230. #define outAF4 AF1 *A_RESTRICT
  231. #define outAL2 AL1 *A_RESTRICT
  232. #define outAL3 AL1 *A_RESTRICT
  233. #define outAL4 AL1 *A_RESTRICT
  234. #define outAU2 AU1 *A_RESTRICT
  235. #define outAU3 AU1 *A_RESTRICT
  236. #define outAU4 AU1 *A_RESTRICT
  237. //------------------------------------------------------------------------------------------------------------------------------
  238. #define varAD2(x) AD1 x[2]
  239. #define varAD3(x) AD1 x[3]
  240. #define varAD4(x) AD1 x[4]
  241. #define varAF2(x) AF1 x[2]
  242. #define varAF3(x) AF1 x[3]
  243. #define varAF4(x) AF1 x[4]
  244. #define varAL2(x) AL1 x[2]
  245. #define varAL3(x) AL1 x[3]
  246. #define varAL4(x) AL1 x[4]
  247. #define varAU2(x) AU1 x[2]
  248. #define varAU3(x) AU1 x[3]
  249. #define varAU4(x) AU1 x[4]
  250. //------------------------------------------------------------------------------------------------------------------------------
  251. #define initAD2(x,y) {x,y}
  252. #define initAD3(x,y,z) {x,y,z}
  253. #define initAD4(x,y,z,w) {x,y,z,w}
  254. #define initAF2(x,y) {x,y}
  255. #define initAF3(x,y,z) {x,y,z}
  256. #define initAF4(x,y,z,w) {x,y,z,w}
  257. #define initAL2(x,y) {x,y}
  258. #define initAL3(x,y,z) {x,y,z}
  259. #define initAL4(x,y,z,w) {x,y,z,w}
  260. #define initAU2(x,y) {x,y}
  261. #define initAU3(x,y,z) {x,y,z}
  262. #define initAU4(x,y,z,w) {x,y,z,w}
  263. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  264. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  265. //_____________________________________________________________/\_______________________________________________________________
  266. //==============================================================================================================================
  267. // SCALAR RETURN OPS
  268. //------------------------------------------------------------------------------------------------------------------------------
  269. // TODO
  270. // ====
  271. // - Replace transcendentals with manual versions.
  272. //==============================================================================================================================
  273. #ifdef A_GCC
  274. A_STATIC AD1 AAbsD1(AD1 a){return __builtin_fabs(a);}
  275. A_STATIC AF1 AAbsF1(AF1 a){return __builtin_fabsf(a);}
  276. A_STATIC AU1 AAbsSU1(AU1 a){return AU1_(__builtin_abs(ASU1_(a)));}
  277. A_STATIC AL1 AAbsSL1(AL1 a){return AL1_(__builtin_llabs(ASL1_(a)));}
  278. #else
  279. A_STATIC AD1 AAbsD1(AD1 a){return fabs(a);}
  280. A_STATIC AF1 AAbsF1(AF1 a){return fabsf(a);}
  281. A_STATIC AU1 AAbsSU1(AU1 a){return AU1_(abs(ASU1_(a)));}
  282. A_STATIC AL1 AAbsSL1(AL1 a){return AL1_(labs((long)ASL1_(a)));}
  283. #endif
  284. //------------------------------------------------------------------------------------------------------------------------------
  285. #ifdef A_GCC
  286. A_STATIC AD1 ACosD1(AD1 a){return __builtin_cos(a);}
  287. A_STATIC AF1 ACosF1(AF1 a){return __builtin_cosf(a);}
  288. #else
  289. A_STATIC AD1 ACosD1(AD1 a){return cos(a);}
  290. A_STATIC AF1 ACosF1(AF1 a){return cosf(a);}
  291. #endif
  292. //------------------------------------------------------------------------------------------------------------------------------
  293. A_STATIC AD1 ADotD2(inAD2 a,inAD2 b){return a[0]*b[0]+a[1]*b[1];}
  294. A_STATIC AD1 ADotD3(inAD3 a,inAD3 b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2];}
  295. A_STATIC AD1 ADotD4(inAD4 a,inAD4 b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]+a[3]*b[3];}
  296. A_STATIC AF1 ADotF2(inAF2 a,inAF2 b){return a[0]*b[0]+a[1]*b[1];}
  297. A_STATIC AF1 ADotF3(inAF3 a,inAF3 b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2];}
  298. A_STATIC AF1 ADotF4(inAF4 a,inAF4 b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]+a[3]*b[3];}
  299. //------------------------------------------------------------------------------------------------------------------------------
  300. #ifdef A_GCC
  301. A_STATIC AD1 AExp2D1(AD1 a){return __builtin_exp2(a);}
  302. A_STATIC AF1 AExp2F1(AF1 a){return __builtin_exp2f(a);}
  303. #else
  304. A_STATIC AD1 AExp2D1(AD1 a){return exp2(a);}
  305. A_STATIC AF1 AExp2F1(AF1 a){return exp2f(a);}
  306. #endif
  307. //------------------------------------------------------------------------------------------------------------------------------
  308. #ifdef A_GCC
  309. A_STATIC AD1 AFloorD1(AD1 a){return __builtin_floor(a);}
  310. A_STATIC AF1 AFloorF1(AF1 a){return __builtin_floorf(a);}
  311. #else
  312. A_STATIC AD1 AFloorD1(AD1 a){return floor(a);}
  313. A_STATIC AF1 AFloorF1(AF1 a){return floorf(a);}
  314. #endif
  315. //------------------------------------------------------------------------------------------------------------------------------
  316. A_STATIC AD1 ALerpD1(AD1 a,AD1 b,AD1 c){return b*c+(-a*c+a);}
  317. A_STATIC AF1 ALerpF1(AF1 a,AF1 b,AF1 c){return b*c+(-a*c+a);}
  318. //------------------------------------------------------------------------------------------------------------------------------
  319. #ifdef A_GCC
  320. A_STATIC AD1 ALog2D1(AD1 a){return __builtin_log2(a);}
  321. A_STATIC AF1 ALog2F1(AF1 a){return __builtin_log2f(a);}
  322. #else
  323. A_STATIC AD1 ALog2D1(AD1 a){return log2(a);}
  324. A_STATIC AF1 ALog2F1(AF1 a){return log2f(a);}
  325. #endif
  326. //------------------------------------------------------------------------------------------------------------------------------
  327. A_STATIC AD1 AMaxD1(AD1 a,AD1 b){return a>b?a:b;}
  328. A_STATIC AF1 AMaxF1(AF1 a,AF1 b){return a>b?a:b;}
  329. A_STATIC AL1 AMaxL1(AL1 a,AL1 b){return a>b?a:b;}
  330. A_STATIC AU1 AMaxU1(AU1 a,AU1 b){return a>b?a:b;}
  331. //------------------------------------------------------------------------------------------------------------------------------
  332. // These follow the convention that A integer types don't have signage, until they are operated on.
  333. A_STATIC AL1 AMaxSL1(AL1 a,AL1 b){return (ASL1_(a)>ASL1_(b))?a:b;}
  334. A_STATIC AU1 AMaxSU1(AU1 a,AU1 b){return (ASU1_(a)>ASU1_(b))?a:b;}
  335. //------------------------------------------------------------------------------------------------------------------------------
  336. A_STATIC AD1 AMinD1(AD1 a,AD1 b){return a<b?a:b;}
  337. A_STATIC AF1 AMinF1(AF1 a,AF1 b){return a<b?a:b;}
  338. A_STATIC AL1 AMinL1(AL1 a,AL1 b){return a<b?a:b;}
  339. A_STATIC AU1 AMinU1(AU1 a,AU1 b){return a<b?a:b;}
  340. //------------------------------------------------------------------------------------------------------------------------------
  341. A_STATIC AL1 AMinSL1(AL1 a,AL1 b){return (ASL1_(a)<ASL1_(b))?a:b;}
  342. A_STATIC AU1 AMinSU1(AU1 a,AU1 b){return (ASU1_(a)<ASU1_(b))?a:b;}
  343. //------------------------------------------------------------------------------------------------------------------------------
  344. A_STATIC AD1 ARcpD1(AD1 a){return 1.0/a;}
  345. A_STATIC AF1 ARcpF1(AF1 a){return 1.0f/a;}
  346. //------------------------------------------------------------------------------------------------------------------------------
  347. A_STATIC AL1 AShrSL1(AL1 a,AL1 b){return AL1_(ASL1_(a)>>ASL1_(b));}
  348. A_STATIC AU1 AShrSU1(AU1 a,AU1 b){return AU1_(ASU1_(a)>>ASU1_(b));}
  349. //------------------------------------------------------------------------------------------------------------------------------
  350. #ifdef A_GCC
  351. A_STATIC AD1 ASinD1(AD1 a){return __builtin_sin(a);}
  352. A_STATIC AF1 ASinF1(AF1 a){return __builtin_sinf(a);}
  353. #else
  354. A_STATIC AD1 ASinD1(AD1 a){return sin(a);}
  355. A_STATIC AF1 ASinF1(AF1 a){return sinf(a);}
  356. #endif
  357. //------------------------------------------------------------------------------------------------------------------------------
  358. #ifdef A_GCC
  359. A_STATIC AD1 ASqrtD1(AD1 a){return __builtin_sqrt(a);}
  360. A_STATIC AF1 ASqrtF1(AF1 a){return __builtin_sqrtf(a);}
  361. #else
  362. A_STATIC AD1 ASqrtD1(AD1 a){return sqrt(a);}
  363. A_STATIC AF1 ASqrtF1(AF1 a){return sqrtf(a);}
  364. #endif
  365. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  366. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  367. //_____________________________________________________________/\_______________________________________________________________
  368. //==============================================================================================================================
  369. // SCALAR RETURN OPS - DEPENDENT
  370. //==============================================================================================================================
  371. A_STATIC AD1 AClampD1(AD1 x,AD1 n,AD1 m){return AMaxD1(n,AMinD1(x,m));}
  372. A_STATIC AF1 AClampF1(AF1 x,AF1 n,AF1 m){return AMaxF1(n,AMinF1(x,m));}
  373. //------------------------------------------------------------------------------------------------------------------------------
  374. A_STATIC AD1 AFractD1(AD1 a){return a-AFloorD1(a);}
  375. A_STATIC AF1 AFractF1(AF1 a){return a-AFloorF1(a);}
  376. //------------------------------------------------------------------------------------------------------------------------------
  377. A_STATIC AD1 APowD1(AD1 a,AD1 b){return AExp2D1(b*ALog2D1(a));}
  378. A_STATIC AF1 APowF1(AF1 a,AF1 b){return AExp2F1(b*ALog2F1(a));}
  379. //------------------------------------------------------------------------------------------------------------------------------
  380. A_STATIC AD1 ARsqD1(AD1 a){return ARcpD1(ASqrtD1(a));}
  381. A_STATIC AF1 ARsqF1(AF1 a){return ARcpF1(ASqrtF1(a));}
  382. //------------------------------------------------------------------------------------------------------------------------------
  383. A_STATIC AD1 ASatD1(AD1 a){return AMinD1(1.0,AMaxD1(0.0,a));}
  384. A_STATIC AF1 ASatF1(AF1 a){return AMinF1(1.0f,AMaxF1(0.0f,a));}
  385. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  386. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  387. //_____________________________________________________________/\_______________________________________________________________
  388. //==============================================================================================================================
  389. // VECTOR OPS
  390. //------------------------------------------------------------------------------------------------------------------------------
  391. // These are added as needed for production or prototyping, so not necessarily a complete set.
  392. // They follow a convention of taking in a destination and also returning the destination value to increase utility.
  393. //==============================================================================================================================
  394. A_STATIC retAD2 opAAbsD2(outAD2 d,inAD2 a){d[0]=AAbsD1(a[0]);d[1]=AAbsD1(a[1]);return d;}
  395. A_STATIC retAD3 opAAbsD3(outAD3 d,inAD3 a){d[0]=AAbsD1(a[0]);d[1]=AAbsD1(a[1]);d[2]=AAbsD1(a[2]);return d;}
  396. A_STATIC retAD4 opAAbsD4(outAD4 d,inAD4 a){d[0]=AAbsD1(a[0]);d[1]=AAbsD1(a[1]);d[2]=AAbsD1(a[2]);d[3]=AAbsD1(a[3]);return d;}
  397. //------------------------------------------------------------------------------------------------------------------------------
  398. A_STATIC retAF2 opAAbsF2(outAF2 d,inAF2 a){d[0]=AAbsF1(a[0]);d[1]=AAbsF1(a[1]);return d;}
  399. A_STATIC retAF3 opAAbsF3(outAF3 d,inAF3 a){d[0]=AAbsF1(a[0]);d[1]=AAbsF1(a[1]);d[2]=AAbsF1(a[2]);return d;}
  400. A_STATIC retAF4 opAAbsF4(outAF4 d,inAF4 a){d[0]=AAbsF1(a[0]);d[1]=AAbsF1(a[1]);d[2]=AAbsF1(a[2]);d[3]=AAbsF1(a[3]);return d;}
  401. //==============================================================================================================================
  402. A_STATIC retAD2 opAAddD2(outAD2 d,inAD2 a,inAD2 b){d[0]=a[0]+b[0];d[1]=a[1]+b[1];return d;}
  403. A_STATIC retAD3 opAAddD3(outAD3 d,inAD3 a,inAD3 b){d[0]=a[0]+b[0];d[1]=a[1]+b[1];d[2]=a[2]+b[2];return d;}
  404. A_STATIC retAD4 opAAddD4(outAD4 d,inAD4 a,inAD4 b){d[0]=a[0]+b[0];d[1]=a[1]+b[1];d[2]=a[2]+b[2];d[3]=a[3]+b[3];return d;}
  405. //------------------------------------------------------------------------------------------------------------------------------
  406. A_STATIC retAF2 opAAddF2(outAF2 d,inAF2 a,inAF2 b){d[0]=a[0]+b[0];d[1]=a[1]+b[1];return d;}
  407. A_STATIC retAF3 opAAddF3(outAF3 d,inAF3 a,inAF3 b){d[0]=a[0]+b[0];d[1]=a[1]+b[1];d[2]=a[2]+b[2];return d;}
  408. A_STATIC retAF4 opAAddF4(outAF4 d,inAF4 a,inAF4 b){d[0]=a[0]+b[0];d[1]=a[1]+b[1];d[2]=a[2]+b[2];d[3]=a[3]+b[3];return d;}
  409. //==============================================================================================================================
  410. A_STATIC retAD2 opAAddOneD2(outAD2 d,inAD2 a,AD1 b){d[0]=a[0]+b;d[1]=a[1]+b;return d;}
  411. A_STATIC retAD3 opAAddOneD3(outAD3 d,inAD3 a,AD1 b){d[0]=a[0]+b;d[1]=a[1]+b;d[2]=a[2]+b;return d;}
  412. A_STATIC retAD4 opAAddOneD4(outAD4 d,inAD4 a,AD1 b){d[0]=a[0]+b;d[1]=a[1]+b;d[2]=a[2]+b;d[3]=a[3]+b;return d;}
  413. //------------------------------------------------------------------------------------------------------------------------------
  414. A_STATIC retAF2 opAAddOneF2(outAF2 d,inAF2 a,AF1 b){d[0]=a[0]+b;d[1]=a[1]+b;return d;}
  415. A_STATIC retAF3 opAAddOneF3(outAF3 d,inAF3 a,AF1 b){d[0]=a[0]+b;d[1]=a[1]+b;d[2]=a[2]+b;return d;}
  416. A_STATIC retAF4 opAAddOneF4(outAF4 d,inAF4 a,AF1 b){d[0]=a[0]+b;d[1]=a[1]+b;d[2]=a[2]+b;d[3]=a[3]+b;return d;}
  417. //==============================================================================================================================
  418. A_STATIC retAD2 opACpyD2(outAD2 d,inAD2 a){d[0]=a[0];d[1]=a[1];return d;}
  419. A_STATIC retAD3 opACpyD3(outAD3 d,inAD3 a){d[0]=a[0];d[1]=a[1];d[2]=a[2];return d;}
  420. A_STATIC retAD4 opACpyD4(outAD4 d,inAD4 a){d[0]=a[0];d[1]=a[1];d[2]=a[2];d[3]=a[3];return d;}
  421. //------------------------------------------------------------------------------------------------------------------------------
  422. A_STATIC retAF2 opACpyF2(outAF2 d,inAF2 a){d[0]=a[0];d[1]=a[1];return d;}
  423. A_STATIC retAF3 opACpyF3(outAF3 d,inAF3 a){d[0]=a[0];d[1]=a[1];d[2]=a[2];return d;}
  424. A_STATIC retAF4 opACpyF4(outAF4 d,inAF4 a){d[0]=a[0];d[1]=a[1];d[2]=a[2];d[3]=a[3];return d;}
  425. //==============================================================================================================================
  426. A_STATIC retAD2 opALerpD2(outAD2 d,inAD2 a,inAD2 b,inAD2 c){d[0]=ALerpD1(a[0],b[0],c[0]);d[1]=ALerpD1(a[1],b[1],c[1]);return d;}
  427. A_STATIC retAD3 opALerpD3(outAD3 d,inAD3 a,inAD3 b,inAD3 c){d[0]=ALerpD1(a[0],b[0],c[0]);d[1]=ALerpD1(a[1],b[1],c[1]);d[2]=ALerpD1(a[2],b[2],c[2]);return d;}
  428. A_STATIC retAD4 opALerpD4(outAD4 d,inAD4 a,inAD4 b,inAD4 c){d[0]=ALerpD1(a[0],b[0],c[0]);d[1]=ALerpD1(a[1],b[1],c[1]);d[2]=ALerpD1(a[2],b[2],c[2]);d[3]=ALerpD1(a[3],b[3],c[3]);return d;}
  429. //------------------------------------------------------------------------------------------------------------------------------
  430. A_STATIC retAF2 opALerpF2(outAF2 d,inAF2 a,inAF2 b,inAF2 c){d[0]=ALerpF1(a[0],b[0],c[0]);d[1]=ALerpF1(a[1],b[1],c[1]);return d;}
  431. A_STATIC retAF3 opALerpF3(outAF3 d,inAF3 a,inAF3 b,inAF3 c){d[0]=ALerpF1(a[0],b[0],c[0]);d[1]=ALerpF1(a[1],b[1],c[1]);d[2]=ALerpF1(a[2],b[2],c[2]);return d;}
  432. A_STATIC retAF4 opALerpF4(outAF4 d,inAF4 a,inAF4 b,inAF4 c){d[0]=ALerpF1(a[0],b[0],c[0]);d[1]=ALerpF1(a[1],b[1],c[1]);d[2]=ALerpF1(a[2],b[2],c[2]);d[3]=ALerpF1(a[3],b[3],c[3]);return d;}
  433. //==============================================================================================================================
  434. A_STATIC retAD2 opALerpOneD2(outAD2 d,inAD2 a,inAD2 b,AD1 c){d[0]=ALerpD1(a[0],b[0],c);d[1]=ALerpD1(a[1],b[1],c);return d;}
  435. A_STATIC retAD3 opALerpOneD3(outAD3 d,inAD3 a,inAD3 b,AD1 c){d[0]=ALerpD1(a[0],b[0],c);d[1]=ALerpD1(a[1],b[1],c);d[2]=ALerpD1(a[2],b[2],c);return d;}
  436. A_STATIC retAD4 opALerpOneD4(outAD4 d,inAD4 a,inAD4 b,AD1 c){d[0]=ALerpD1(a[0],b[0],c);d[1]=ALerpD1(a[1],b[1],c);d[2]=ALerpD1(a[2],b[2],c);d[3]=ALerpD1(a[3],b[3],c);return d;}
  437. //------------------------------------------------------------------------------------------------------------------------------
  438. A_STATIC retAF2 opALerpOneF2(outAF2 d,inAF2 a,inAF2 b,AF1 c){d[0]=ALerpF1(a[0],b[0],c);d[1]=ALerpF1(a[1],b[1],c);return d;}
  439. A_STATIC retAF3 opALerpOneF3(outAF3 d,inAF3 a,inAF3 b,AF1 c){d[0]=ALerpF1(a[0],b[0],c);d[1]=ALerpF1(a[1],b[1],c);d[2]=ALerpF1(a[2],b[2],c);return d;}
  440. A_STATIC retAF4 opALerpOneF4(outAF4 d,inAF4 a,inAF4 b,AF1 c){d[0]=ALerpF1(a[0],b[0],c);d[1]=ALerpF1(a[1],b[1],c);d[2]=ALerpF1(a[2],b[2],c);d[3]=ALerpF1(a[3],b[3],c);return d;}
  441. //==============================================================================================================================
  442. A_STATIC retAD2 opAMaxD2(outAD2 d,inAD2 a,inAD2 b){d[0]=AMaxD1(a[0],b[0]);d[1]=AMaxD1(a[1],b[1]);return d;}
  443. A_STATIC retAD3 opAMaxD3(outAD3 d,inAD3 a,inAD3 b){d[0]=AMaxD1(a[0],b[0]);d[1]=AMaxD1(a[1],b[1]);d[2]=AMaxD1(a[2],b[2]);return d;}
  444. A_STATIC retAD4 opAMaxD4(outAD4 d,inAD4 a,inAD4 b){d[0]=AMaxD1(a[0],b[0]);d[1]=AMaxD1(a[1],b[1]);d[2]=AMaxD1(a[2],b[2]);d[3]=AMaxD1(a[3],b[3]);return d;}
  445. //------------------------------------------------------------------------------------------------------------------------------
  446. A_STATIC retAF2 opAMaxF2(outAF2 d,inAF2 a,inAF2 b){d[0]=AMaxF1(a[0],b[0]);d[1]=AMaxF1(a[1],b[1]);return d;}
  447. A_STATIC retAF3 opAMaxF3(outAF3 d,inAF3 a,inAF3 b){d[0]=AMaxF1(a[0],b[0]);d[1]=AMaxF1(a[1],b[1]);d[2]=AMaxF1(a[2],b[2]);return d;}
  448. A_STATIC retAF4 opAMaxF4(outAF4 d,inAF4 a,inAF4 b){d[0]=AMaxF1(a[0],b[0]);d[1]=AMaxF1(a[1],b[1]);d[2]=AMaxF1(a[2],b[2]);d[3]=AMaxF1(a[3],b[3]);return d;}
  449. //==============================================================================================================================
  450. A_STATIC retAD2 opAMinD2(outAD2 d,inAD2 a,inAD2 b){d[0]=AMinD1(a[0],b[0]);d[1]=AMinD1(a[1],b[1]);return d;}
  451. A_STATIC retAD3 opAMinD3(outAD3 d,inAD3 a,inAD3 b){d[0]=AMinD1(a[0],b[0]);d[1]=AMinD1(a[1],b[1]);d[2]=AMinD1(a[2],b[2]);return d;}
  452. A_STATIC retAD4 opAMinD4(outAD4 d,inAD4 a,inAD4 b){d[0]=AMinD1(a[0],b[0]);d[1]=AMinD1(a[1],b[1]);d[2]=AMinD1(a[2],b[2]);d[3]=AMinD1(a[3],b[3]);return d;}
  453. //------------------------------------------------------------------------------------------------------------------------------
  454. A_STATIC retAF2 opAMinF2(outAF2 d,inAF2 a,inAF2 b){d[0]=AMinF1(a[0],b[0]);d[1]=AMinF1(a[1],b[1]);return d;}
  455. A_STATIC retAF3 opAMinF3(outAF3 d,inAF3 a,inAF3 b){d[0]=AMinF1(a[0],b[0]);d[1]=AMinF1(a[1],b[1]);d[2]=AMinF1(a[2],b[2]);return d;}
  456. A_STATIC retAF4 opAMinF4(outAF4 d,inAF4 a,inAF4 b){d[0]=AMinF1(a[0],b[0]);d[1]=AMinF1(a[1],b[1]);d[2]=AMinF1(a[2],b[2]);d[3]=AMinF1(a[3],b[3]);return d;}
  457. //==============================================================================================================================
  458. A_STATIC retAD2 opAMulD2(outAD2 d,inAD2 a,inAD2 b){d[0]=a[0]*b[0];d[1]=a[1]*b[1];return d;}
  459. A_STATIC retAD3 opAMulD3(outAD3 d,inAD3 a,inAD3 b){d[0]=a[0]*b[0];d[1]=a[1]*b[1];d[2]=a[2]*b[2];return d;}
  460. A_STATIC retAD4 opAMulD4(outAD4 d,inAD4 a,inAD4 b){d[0]=a[0]*b[0];d[1]=a[1]*b[1];d[2]=a[2]*b[2];d[3]=a[3]*b[3];return d;}
  461. //------------------------------------------------------------------------------------------------------------------------------
  462. A_STATIC retAF2 opAMulF2(outAF2 d,inAF2 a,inAF2 b){d[0]=a[0]*b[0];d[1]=a[1]*b[1];return d;}
  463. A_STATIC retAF3 opAMulF3(outAF3 d,inAF3 a,inAF3 b){d[0]=a[0]*b[0];d[1]=a[1]*b[1];d[2]=a[2]*b[2];return d;}
  464. A_STATIC retAF4 opAMulF4(outAF4 d,inAF4 a,inAF4 b){d[0]=a[0]*b[0];d[1]=a[1]*b[1];d[2]=a[2]*b[2];d[3]=a[3]*b[3];return d;}
  465. //==============================================================================================================================
  466. A_STATIC retAD2 opAMulOneD2(outAD2 d,inAD2 a,AD1 b){d[0]=a[0]*b;d[1]=a[1]*b;return d;}
  467. A_STATIC retAD3 opAMulOneD3(outAD3 d,inAD3 a,AD1 b){d[0]=a[0]*b;d[1]=a[1]*b;d[2]=a[2]*b;return d;}
  468. A_STATIC retAD4 opAMulOneD4(outAD4 d,inAD4 a,AD1 b){d[0]=a[0]*b;d[1]=a[1]*b;d[2]=a[2]*b;d[3]=a[3]*b;return d;}
  469. //------------------------------------------------------------------------------------------------------------------------------
  470. A_STATIC retAF2 opAMulOneF2(outAF2 d,inAF2 a,AF1 b){d[0]=a[0]*b;d[1]=a[1]*b;return d;}
  471. A_STATIC retAF3 opAMulOneF3(outAF3 d,inAF3 a,AF1 b){d[0]=a[0]*b;d[1]=a[1]*b;d[2]=a[2]*b;return d;}
  472. A_STATIC retAF4 opAMulOneF4(outAF4 d,inAF4 a,AF1 b){d[0]=a[0]*b;d[1]=a[1]*b;d[2]=a[2]*b;d[3]=a[3]*b;return d;}
  473. //==============================================================================================================================
  474. A_STATIC retAD2 opANegD2(outAD2 d,inAD2 a){d[0]=-a[0];d[1]=-a[1];return d;}
  475. A_STATIC retAD3 opANegD3(outAD3 d,inAD3 a){d[0]=-a[0];d[1]=-a[1];d[2]=-a[2];return d;}
  476. A_STATIC retAD4 opANegD4(outAD4 d,inAD4 a){d[0]=-a[0];d[1]=-a[1];d[2]=-a[2];d[3]=-a[3];return d;}
  477. //------------------------------------------------------------------------------------------------------------------------------
  478. A_STATIC retAF2 opANegF2(outAF2 d,inAF2 a){d[0]=-a[0];d[1]=-a[1];return d;}
  479. A_STATIC retAF3 opANegF3(outAF3 d,inAF3 a){d[0]=-a[0];d[1]=-a[1];d[2]=-a[2];return d;}
  480. A_STATIC retAF4 opANegF4(outAF4 d,inAF4 a){d[0]=-a[0];d[1]=-a[1];d[2]=-a[2];d[3]=-a[3];return d;}
  481. //==============================================================================================================================
  482. A_STATIC retAD2 opARcpD2(outAD2 d,inAD2 a){d[0]=ARcpD1(a[0]);d[1]=ARcpD1(a[1]);return d;}
  483. A_STATIC retAD3 opARcpD3(outAD3 d,inAD3 a){d[0]=ARcpD1(a[0]);d[1]=ARcpD1(a[1]);d[2]=ARcpD1(a[2]);return d;}
  484. A_STATIC retAD4 opARcpD4(outAD4 d,inAD4 a){d[0]=ARcpD1(a[0]);d[1]=ARcpD1(a[1]);d[2]=ARcpD1(a[2]);d[3]=ARcpD1(a[3]);return d;}
  485. //------------------------------------------------------------------------------------------------------------------------------
  486. A_STATIC retAF2 opARcpF2(outAF2 d,inAF2 a){d[0]=ARcpF1(a[0]);d[1]=ARcpF1(a[1]);return d;}
  487. A_STATIC retAF3 opARcpF3(outAF3 d,inAF3 a){d[0]=ARcpF1(a[0]);d[1]=ARcpF1(a[1]);d[2]=ARcpF1(a[2]);return d;}
  488. A_STATIC retAF4 opARcpF4(outAF4 d,inAF4 a){d[0]=ARcpF1(a[0]);d[1]=ARcpF1(a[1]);d[2]=ARcpF1(a[2]);d[3]=ARcpF1(a[3]);return d;}
  489. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  490. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  491. //_____________________________________________________________/\_______________________________________________________________
  492. //==============================================================================================================================
  493. // HALF FLOAT PACKING
  494. //==============================================================================================================================
  495. // Convert float to half (in lower 16-bits of output).
  496. // Same fast technique as documented here: ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf
  497. // Supports denormals.
  498. // Conversion rules are to make computations possibly "safer" on the GPU,
  499. // -INF & -NaN -> -65504
  500. // +INF & +NaN -> +65504
  501. A_STATIC AU1 AU1_AH1_AF1(AF1 f){
  502. static AW1 base[512]={
  503. 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  504. 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  505. 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  506. 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  507. 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  508. 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  509. 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0001,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080,0x0100,
  510. 0x0200,0x0400,0x0800,0x0c00,0x1000,0x1400,0x1800,0x1c00,0x2000,0x2400,0x2800,0x2c00,0x3000,0x3400,0x3800,0x3c00,
  511. 0x4000,0x4400,0x4800,0x4c00,0x5000,0x5400,0x5800,0x5c00,0x6000,0x6400,0x6800,0x6c00,0x7000,0x7400,0x7800,0x7bff,
  512. 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
  513. 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
  514. 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
  515. 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
  516. 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
  517. 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
  518. 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
  519. 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
  520. 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
  521. 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
  522. 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
  523. 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
  524. 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
  525. 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8001,0x8002,0x8004,0x8008,0x8010,0x8020,0x8040,0x8080,0x8100,
  526. 0x8200,0x8400,0x8800,0x8c00,0x9000,0x9400,0x9800,0x9c00,0xa000,0xa400,0xa800,0xac00,0xb000,0xb400,0xb800,0xbc00,
  527. 0xc000,0xc400,0xc800,0xcc00,0xd000,0xd400,0xd800,0xdc00,0xe000,0xe400,0xe800,0xec00,0xf000,0xf400,0xf800,0xfbff,
  528. 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,
  529. 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,
  530. 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,
  531. 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,
  532. 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,
  533. 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,
  534. 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff};
  535. static AB1 shift[512]={
  536. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  537. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  538. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  539. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  540. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  541. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  542. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x17,0x16,0x15,0x14,0x13,0x12,0x11,0x10,0x0f,
  543. 0x0e,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,
  544. 0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x18,
  545. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  546. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  547. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  548. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  549. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  550. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  551. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  552. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  553. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  554. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  555. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  556. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  557. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  558. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x17,0x16,0x15,0x14,0x13,0x12,0x11,0x10,0x0f,
  559. 0x0e,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,
  560. 0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x18,
  561. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  562. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  563. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  564. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  565. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  566. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
  567. 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18};
  568. union{AF1 f;AU1 u;}bits;bits.f=f;AU1 u=bits.u;AU1 i=u>>23;return (AU1)(base[i])+((u&0x7fffff)>>shift[i]);}
  569. //------------------------------------------------------------------------------------------------------------------------------
  570. // Used to output packed constant.
  571. A_STATIC AU1 AU1_AH2_AF2(inAF2 a){return AU1_AH1_AF1(a[0])+(AU1_AH1_AF1(a[1])<<16);}
  572. #endif
  573. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  574. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  575. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  576. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  577. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  578. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  579. //_____________________________________________________________/\_______________________________________________________________
  580. //==============================================================================================================================
  581. //
  582. //
  583. // GLSL
  584. //
  585. //
  586. //==============================================================================================================================
  587. #if defined(A_GLSL) && defined(A_GPU)
  588. #ifndef A_SKIP_EXT
  589. #ifdef A_HALF
  590. #extension GL_EXT_shader_16bit_storage:require
  591. #extension GL_EXT_shader_explicit_arithmetic_types:require
  592. #endif
  593. //------------------------------------------------------------------------------------------------------------------------------
  594. #ifdef A_LONG
  595. #extension GL_ARB_gpu_shader_int64:require
  596. #extension GL_NV_shader_atomic_int64:require
  597. #endif
  598. //------------------------------------------------------------------------------------------------------------------------------
  599. #ifdef A_WAVE
  600. #extension GL_KHR_shader_subgroup_arithmetic:require
  601. #extension GL_KHR_shader_subgroup_ballot:require
  602. #extension GL_KHR_shader_subgroup_quad:require
  603. #extension GL_KHR_shader_subgroup_shuffle:require
  604. #endif
  605. #endif
  606. //==============================================================================================================================
  607. #define AP1 bool
  608. #define AP2 bvec2
  609. #define AP3 bvec3
  610. #define AP4 bvec4
  611. //------------------------------------------------------------------------------------------------------------------------------
  612. #define AF1 float
  613. #define AF2 vec2
  614. #define AF3 vec3
  615. #define AF4 vec4
  616. //------------------------------------------------------------------------------------------------------------------------------
  617. #define AU1 uint
  618. #define AU2 uvec2
  619. #define AU3 uvec3
  620. #define AU4 uvec4
  621. //------------------------------------------------------------------------------------------------------------------------------
  622. #define ASU1 int
  623. #define ASU2 ivec2
  624. #define ASU3 ivec3
  625. #define ASU4 ivec4
  626. //==============================================================================================================================
  627. #define AF1_AU1(x) uintBitsToFloat(AU1(x))
  628. #define AF2_AU2(x) uintBitsToFloat(AU2(x))
  629. #define AF3_AU3(x) uintBitsToFloat(AU3(x))
  630. #define AF4_AU4(x) uintBitsToFloat(AU4(x))
  631. //------------------------------------------------------------------------------------------------------------------------------
  632. #define AU1_AF1(x) floatBitsToUint(AF1(x))
  633. #define AU2_AF2(x) floatBitsToUint(AF2(x))
  634. #define AU3_AF3(x) floatBitsToUint(AF3(x))
  635. #define AU4_AF4(x) floatBitsToUint(AF4(x))
  636. //------------------------------------------------------------------------------------------------------------------------------
  637. AU1 AU1_AH1_AF1_x(AF1 a){return packHalf2x16(AF2(a,0.0));}
  638. #define AU1_AH1_AF1(a) AU1_AH1_AF1_x(AF1(a))
  639. //------------------------------------------------------------------------------------------------------------------------------
  640. #define AU1_AH2_AF2 packHalf2x16
  641. #define AU1_AW2Unorm_AF2 packUnorm2x16
  642. #define AU1_AB4Unorm_AF4 packUnorm4x8
  643. //------------------------------------------------------------------------------------------------------------------------------
  644. #define AF2_AH2_AU1 unpackHalf2x16
  645. #define AF2_AW2Unorm_AU1 unpackUnorm2x16
  646. #define AF4_AB4Unorm_AU1 unpackUnorm4x8
  647. //==============================================================================================================================
  648. AF1 AF1_x(AF1 a){return AF1(a);}
  649. AF2 AF2_x(AF1 a){return AF2(a,a);}
  650. AF3 AF3_x(AF1 a){return AF3(a,a,a);}
  651. AF4 AF4_x(AF1 a){return AF4(a,a,a,a);}
  652. #define AF1_(a) AF1_x(AF1(a))
  653. #define AF2_(a) AF2_x(AF1(a))
  654. #define AF3_(a) AF3_x(AF1(a))
  655. #define AF4_(a) AF4_x(AF1(a))
  656. //------------------------------------------------------------------------------------------------------------------------------
  657. AU1 AU1_x(AU1 a){return AU1(a);}
  658. AU2 AU2_x(AU1 a){return AU2(a,a);}
  659. AU3 AU3_x(AU1 a){return AU3(a,a,a);}
  660. AU4 AU4_x(AU1 a){return AU4(a,a,a,a);}
  661. #define AU1_(a) AU1_x(AU1(a))
  662. #define AU2_(a) AU2_x(AU1(a))
  663. #define AU3_(a) AU3_x(AU1(a))
  664. #define AU4_(a) AU4_x(AU1(a))
  665. //==============================================================================================================================
  666. AU1 AAbsSU1(AU1 a){return AU1(abs(ASU1(a)));}
  667. AU2 AAbsSU2(AU2 a){return AU2(abs(ASU2(a)));}
  668. AU3 AAbsSU3(AU3 a){return AU3(abs(ASU3(a)));}
  669. AU4 AAbsSU4(AU4 a){return AU4(abs(ASU4(a)));}
  670. //------------------------------------------------------------------------------------------------------------------------------
  671. AU1 ABfe(AU1 src,AU1 off,AU1 bits){return bitfieldExtract(src,ASU1(off),ASU1(bits));}
  672. AU1 ABfi(AU1 src,AU1 ins,AU1 mask){return (ins&mask)|(src&(~mask));}
  673. // Proxy for V_BFI_B32 where the 'mask' is set as 'bits', 'mask=(1<<bits)-1', and 'bits' needs to be an immediate.
  674. AU1 ABfiM(AU1 src,AU1 ins,AU1 bits){return bitfieldInsert(src,ins,0,ASU1(bits));}
  675. //------------------------------------------------------------------------------------------------------------------------------
  676. // V_MED3_F32.
  677. AF1 AClampF1(AF1 x,AF1 n,AF1 m){return clamp(x,n,m);}
  678. AF2 AClampF2(AF2 x,AF2 n,AF2 m){return clamp(x,n,m);}
  679. AF3 AClampF3(AF3 x,AF3 n,AF3 m){return clamp(x,n,m);}
  680. AF4 AClampF4(AF4 x,AF4 n,AF4 m){return clamp(x,n,m);}
  681. //------------------------------------------------------------------------------------------------------------------------------
  682. // V_FRACT_F32 (note DX frac() is different).
  683. AF1 AFractF1(AF1 x){return fract(x);}
  684. AF2 AFractF2(AF2 x){return fract(x);}
  685. AF3 AFractF3(AF3 x){return fract(x);}
  686. AF4 AFractF4(AF4 x){return fract(x);}
  687. //------------------------------------------------------------------------------------------------------------------------------
  688. AF1 ALerpF1(AF1 x,AF1 y,AF1 a){return mix(x,y,a);}
  689. AF2 ALerpF2(AF2 x,AF2 y,AF2 a){return mix(x,y,a);}
  690. AF3 ALerpF3(AF3 x,AF3 y,AF3 a){return mix(x,y,a);}
  691. AF4 ALerpF4(AF4 x,AF4 y,AF4 a){return mix(x,y,a);}
  692. //------------------------------------------------------------------------------------------------------------------------------
  693. // V_MAX3_F32.
  694. AF1 AMax3F1(AF1 x,AF1 y,AF1 z){return max(x,max(y,z));}
  695. AF2 AMax3F2(AF2 x,AF2 y,AF2 z){return max(x,max(y,z));}
  696. AF3 AMax3F3(AF3 x,AF3 y,AF3 z){return max(x,max(y,z));}
  697. AF4 AMax3F4(AF4 x,AF4 y,AF4 z){return max(x,max(y,z));}
  698. //------------------------------------------------------------------------------------------------------------------------------
  699. AU1 AMax3SU1(AU1 x,AU1 y,AU1 z){return AU1(max(ASU1(x),max(ASU1(y),ASU1(z))));}
  700. AU2 AMax3SU2(AU2 x,AU2 y,AU2 z){return AU2(max(ASU2(x),max(ASU2(y),ASU2(z))));}
  701. AU3 AMax3SU3(AU3 x,AU3 y,AU3 z){return AU3(max(ASU3(x),max(ASU3(y),ASU3(z))));}
  702. AU4 AMax3SU4(AU4 x,AU4 y,AU4 z){return AU4(max(ASU4(x),max(ASU4(y),ASU4(z))));}
  703. //------------------------------------------------------------------------------------------------------------------------------
  704. AU1 AMax3U1(AU1 x,AU1 y,AU1 z){return max(x,max(y,z));}
  705. AU2 AMax3U2(AU2 x,AU2 y,AU2 z){return max(x,max(y,z));}
  706. AU3 AMax3U3(AU3 x,AU3 y,AU3 z){return max(x,max(y,z));}
  707. AU4 AMax3U4(AU4 x,AU4 y,AU4 z){return max(x,max(y,z));}
  708. //------------------------------------------------------------------------------------------------------------------------------
  709. AU1 AMaxSU1(AU1 a,AU1 b){return AU1(max(ASU1(a),ASU1(b)));}
  710. AU2 AMaxSU2(AU2 a,AU2 b){return AU2(max(ASU2(a),ASU2(b)));}
  711. AU3 AMaxSU3(AU3 a,AU3 b){return AU3(max(ASU3(a),ASU3(b)));}
  712. AU4 AMaxSU4(AU4 a,AU4 b){return AU4(max(ASU4(a),ASU4(b)));}
  713. //------------------------------------------------------------------------------------------------------------------------------
  714. // Clamp has an easier pattern match for med3 when some ordering is known.
  715. // V_MED3_F32.
  716. AF1 AMed3F1(AF1 x,AF1 y,AF1 z){return max(min(x,y),min(max(x,y),z));}
  717. AF2 AMed3F2(AF2 x,AF2 y,AF2 z){return max(min(x,y),min(max(x,y),z));}
  718. AF3 AMed3F3(AF3 x,AF3 y,AF3 z){return max(min(x,y),min(max(x,y),z));}
  719. AF4 AMed3F4(AF4 x,AF4 y,AF4 z){return max(min(x,y),min(max(x,y),z));}
  720. //------------------------------------------------------------------------------------------------------------------------------
  721. // V_MIN3_F32.
  722. AF1 AMin3F1(AF1 x,AF1 y,AF1 z){return min(x,min(y,z));}
  723. AF2 AMin3F2(AF2 x,AF2 y,AF2 z){return min(x,min(y,z));}
  724. AF3 AMin3F3(AF3 x,AF3 y,AF3 z){return min(x,min(y,z));}
  725. AF4 AMin3F4(AF4 x,AF4 y,AF4 z){return min(x,min(y,z));}
  726. //------------------------------------------------------------------------------------------------------------------------------
  727. AU1 AMin3SU1(AU1 x,AU1 y,AU1 z){return AU1(min(ASU1(x),min(ASU1(y),ASU1(z))));}
  728. AU2 AMin3SU2(AU2 x,AU2 y,AU2 z){return AU2(min(ASU2(x),min(ASU2(y),ASU2(z))));}
  729. AU3 AMin3SU3(AU3 x,AU3 y,AU3 z){return AU3(min(ASU3(x),min(ASU3(y),ASU3(z))));}
  730. AU4 AMin3SU4(AU4 x,AU4 y,AU4 z){return AU4(min(ASU4(x),min(ASU4(y),ASU4(z))));}
  731. //------------------------------------------------------------------------------------------------------------------------------
  732. AU1 AMin3U1(AU1 x,AU1 y,AU1 z){return min(x,min(y,z));}
  733. AU2 AMin3U2(AU2 x,AU2 y,AU2 z){return min(x,min(y,z));}
  734. AU3 AMin3U3(AU3 x,AU3 y,AU3 z){return min(x,min(y,z));}
  735. AU4 AMin3U4(AU4 x,AU4 y,AU4 z){return min(x,min(y,z));}
  736. //------------------------------------------------------------------------------------------------------------------------------
  737. AU1 AMinSU1(AU1 a,AU1 b){return AU1(min(ASU1(a),ASU1(b)));}
  738. AU2 AMinSU2(AU2 a,AU2 b){return AU2(min(ASU2(a),ASU2(b)));}
  739. AU3 AMinSU3(AU3 a,AU3 b){return AU3(min(ASU3(a),ASU3(b)));}
  740. AU4 AMinSU4(AU4 a,AU4 b){return AU4(min(ASU4(a),ASU4(b)));}
  741. //------------------------------------------------------------------------------------------------------------------------------
  742. // Normalized trig. Valid input domain is {-256 to +256}. No GLSL compiler intrinsic exists to map to this currently.
  743. // V_COS_F32.
  744. AF1 ANCosF1(AF1 x){return cos(x*AF1_(A_2PI));}
  745. AF2 ANCosF2(AF2 x){return cos(x*AF2_(A_2PI));}
  746. AF3 ANCosF3(AF3 x){return cos(x*AF3_(A_2PI));}
  747. AF4 ANCosF4(AF4 x){return cos(x*AF4_(A_2PI));}
  748. //------------------------------------------------------------------------------------------------------------------------------
  749. // Normalized trig. Valid input domain is {-256 to +256}. No GLSL compiler intrinsic exists to map to this currently.
  750. // V_SIN_F32.
  751. AF1 ANSinF1(AF1 x){return sin(x*AF1_(A_2PI));}
  752. AF2 ANSinF2(AF2 x){return sin(x*AF2_(A_2PI));}
  753. AF3 ANSinF3(AF3 x){return sin(x*AF3_(A_2PI));}
  754. AF4 ANSinF4(AF4 x){return sin(x*AF4_(A_2PI));}
  755. //------------------------------------------------------------------------------------------------------------------------------
  756. AF1 ARcpF1(AF1 x){return AF1_(1.0)/x;}
  757. AF2 ARcpF2(AF2 x){return AF2_(1.0)/x;}
  758. AF3 ARcpF3(AF3 x){return AF3_(1.0)/x;}
  759. AF4 ARcpF4(AF4 x){return AF4_(1.0)/x;}
  760. //------------------------------------------------------------------------------------------------------------------------------
  761. AF1 ARsqF1(AF1 x){return AF1_(1.0)/sqrt(x);}
  762. AF2 ARsqF2(AF2 x){return AF2_(1.0)/sqrt(x);}
  763. AF3 ARsqF3(AF3 x){return AF3_(1.0)/sqrt(x);}
  764. AF4 ARsqF4(AF4 x){return AF4_(1.0)/sqrt(x);}
  765. //------------------------------------------------------------------------------------------------------------------------------
  766. AF1 ASatF1(AF1 x){return clamp(x,AF1_(0.0),AF1_(1.0));}
  767. AF2 ASatF2(AF2 x){return clamp(x,AF2_(0.0),AF2_(1.0));}
  768. AF3 ASatF3(AF3 x){return clamp(x,AF3_(0.0),AF3_(1.0));}
  769. AF4 ASatF4(AF4 x){return clamp(x,AF4_(0.0),AF4_(1.0));}
  770. //------------------------------------------------------------------------------------------------------------------------------
  771. AU1 AShrSU1(AU1 a,AU1 b){return AU1(ASU1(a)>>ASU1(b));}
  772. AU2 AShrSU2(AU2 a,AU2 b){return AU2(ASU2(a)>>ASU2(b));}
  773. AU3 AShrSU3(AU3 a,AU3 b){return AU3(ASU3(a)>>ASU3(b));}
  774. AU4 AShrSU4(AU4 a,AU4 b){return AU4(ASU4(a)>>ASU4(b));}
  775. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  776. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  777. //_____________________________________________________________/\_______________________________________________________________
  778. //==============================================================================================================================
  779. // GLSL BYTE
  780. //==============================================================================================================================
  781. #ifdef A_BYTE
  782. #define AB1 uint8_t
  783. #define AB2 u8vec2
  784. #define AB3 u8vec3
  785. #define AB4 u8vec4
  786. //------------------------------------------------------------------------------------------------------------------------------
  787. #define ASB1 int8_t
  788. #define ASB2 i8vec2
  789. #define ASB3 i8vec3
  790. #define ASB4 i8vec4
  791. //------------------------------------------------------------------------------------------------------------------------------
  792. AB1 AB1_x(AB1 a){return AB1(a);}
  793. AB2 AB2_x(AB1 a){return AB2(a,a);}
  794. AB3 AB3_x(AB1 a){return AB3(a,a,a);}
  795. AB4 AB4_x(AB1 a){return AB4(a,a,a,a);}
  796. #define AB1_(a) AB1_x(AB1(a))
  797. #define AB2_(a) AB2_x(AB1(a))
  798. #define AB3_(a) AB3_x(AB1(a))
  799. #define AB4_(a) AB4_x(AB1(a))
  800. #endif
  801. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  802. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  803. //_____________________________________________________________/\_______________________________________________________________
  804. //==============================================================================================================================
  805. // GLSL HALF
  806. //==============================================================================================================================
  807. #ifdef A_HALF
  808. #define AH1 float16_t
  809. #define AH2 f16vec2
  810. #define AH3 f16vec3
  811. #define AH4 f16vec4
  812. //------------------------------------------------------------------------------------------------------------------------------
  813. #define AW1 uint16_t
  814. #define AW2 u16vec2
  815. #define AW3 u16vec3
  816. #define AW4 u16vec4
  817. //------------------------------------------------------------------------------------------------------------------------------
  818. #define ASW1 int16_t
  819. #define ASW2 i16vec2
  820. #define ASW3 i16vec3
  821. #define ASW4 i16vec4
  822. //==============================================================================================================================
  823. #define AH2_AU1(x) unpackFloat2x16(AU1(x))
  824. AH4 AH4_AU2_x(AU2 x){return AH4(unpackFloat2x16(x.x),unpackFloat2x16(x.y));}
  825. #define AH4_AU2(x) AH4_AU2_x(AU2(x))
  826. #define AW2_AU1(x) unpackUint2x16(AU1(x))
  827. #define AW4_AU2(x) unpackUint4x16(pack64(AU2(x)))
  828. //------------------------------------------------------------------------------------------------------------------------------
  829. #define AU1_AH2(x) packFloat2x16(AH2(x))
  830. AU2 AU2_AH4_x(AH4 x){return AU2(packFloat2x16(x.xy),packFloat2x16(x.zw));}
  831. #define AU2_AH4(x) AU2_AH4_x(AH4(x))
  832. #define AU1_AW2(x) packUint2x16(AW2(x))
  833. #define AU2_AW4(x) unpack32(packUint4x16(AW4(x)))
  834. //==============================================================================================================================
  835. #define AW1_AH1(x) halfBitsToUint16(AH1(x))
  836. #define AW2_AH2(x) halfBitsToUint16(AH2(x))
  837. #define AW3_AH3(x) halfBitsToUint16(AH3(x))
  838. #define AW4_AH4(x) halfBitsToUint16(AH4(x))
  839. //------------------------------------------------------------------------------------------------------------------------------
  840. #define AH1_AW1(x) uint16BitsToHalf(AW1(x))
  841. #define AH2_AW2(x) uint16BitsToHalf(AW2(x))
  842. #define AH3_AW3(x) uint16BitsToHalf(AW3(x))
  843. #define AH4_AW4(x) uint16BitsToHalf(AW4(x))
  844. //==============================================================================================================================
  845. AH1 AH1_x(AH1 a){return AH1(a);}
  846. AH2 AH2_x(AH1 a){return AH2(a,a);}
  847. AH3 AH3_x(AH1 a){return AH3(a,a,a);}
  848. AH4 AH4_x(AH1 a){return AH4(a,a,a,a);}
  849. #define AH1_(a) AH1_x(AH1(a))
  850. #define AH2_(a) AH2_x(AH1(a))
  851. #define AH3_(a) AH3_x(AH1(a))
  852. #define AH4_(a) AH4_x(AH1(a))
  853. //------------------------------------------------------------------------------------------------------------------------------
  854. AW1 AW1_x(AW1 a){return AW1(a);}
  855. AW2 AW2_x(AW1 a){return AW2(a,a);}
  856. AW3 AW3_x(AW1 a){return AW3(a,a,a);}
  857. AW4 AW4_x(AW1 a){return AW4(a,a,a,a);}
  858. #define AW1_(a) AW1_x(AW1(a))
  859. #define AW2_(a) AW2_x(AW1(a))
  860. #define AW3_(a) AW3_x(AW1(a))
  861. #define AW4_(a) AW4_x(AW1(a))
  862. //==============================================================================================================================
  863. AW1 AAbsSW1(AW1 a){return AW1(abs(ASW1(a)));}
  864. AW2 AAbsSW2(AW2 a){return AW2(abs(ASW2(a)));}
  865. AW3 AAbsSW3(AW3 a){return AW3(abs(ASW3(a)));}
  866. AW4 AAbsSW4(AW4 a){return AW4(abs(ASW4(a)));}
  867. //------------------------------------------------------------------------------------------------------------------------------
  868. AH1 AClampH1(AH1 x,AH1 n,AH1 m){return clamp(x,n,m);}
  869. AH2 AClampH2(AH2 x,AH2 n,AH2 m){return clamp(x,n,m);}
  870. AH3 AClampH3(AH3 x,AH3 n,AH3 m){return clamp(x,n,m);}
  871. AH4 AClampH4(AH4 x,AH4 n,AH4 m){return clamp(x,n,m);}
  872. //------------------------------------------------------------------------------------------------------------------------------
  873. AH1 AFractH1(AH1 x){return fract(x);}
  874. AH2 AFractH2(AH2 x){return fract(x);}
  875. AH3 AFractH3(AH3 x){return fract(x);}
  876. AH4 AFractH4(AH4 x){return fract(x);}
  877. //------------------------------------------------------------------------------------------------------------------------------
  878. AH1 ALerpH1(AH1 x,AH1 y,AH1 a){return mix(x,y,a);}
  879. AH2 ALerpH2(AH2 x,AH2 y,AH2 a){return mix(x,y,a);}
  880. AH3 ALerpH3(AH3 x,AH3 y,AH3 a){return mix(x,y,a);}
  881. AH4 ALerpH4(AH4 x,AH4 y,AH4 a){return mix(x,y,a);}
  882. //------------------------------------------------------------------------------------------------------------------------------
  883. // No packed version of max3.
  884. AH1 AMax3H1(AH1 x,AH1 y,AH1 z){return max(x,max(y,z));}
  885. AH2 AMax3H2(AH2 x,AH2 y,AH2 z){return max(x,max(y,z));}
  886. AH3 AMax3H3(AH3 x,AH3 y,AH3 z){return max(x,max(y,z));}
  887. AH4 AMax3H4(AH4 x,AH4 y,AH4 z){return max(x,max(y,z));}
  888. //------------------------------------------------------------------------------------------------------------------------------
  889. AW1 AMaxSW1(AW1 a,AW1 b){return AW1(max(ASU1(a),ASU1(b)));}
  890. AW2 AMaxSW2(AW2 a,AW2 b){return AW2(max(ASU2(a),ASU2(b)));}
  891. AW3 AMaxSW3(AW3 a,AW3 b){return AW3(max(ASU3(a),ASU3(b)));}
  892. AW4 AMaxSW4(AW4 a,AW4 b){return AW4(max(ASU4(a),ASU4(b)));}
  893. //------------------------------------------------------------------------------------------------------------------------------
  894. // No packed version of min3.
  895. AH1 AMin3H1(AH1 x,AH1 y,AH1 z){return min(x,min(y,z));}
  896. AH2 AMin3H2(AH2 x,AH2 y,AH2 z){return min(x,min(y,z));}
  897. AH3 AMin3H3(AH3 x,AH3 y,AH3 z){return min(x,min(y,z));}
  898. AH4 AMin3H4(AH4 x,AH4 y,AH4 z){return min(x,min(y,z));}
  899. //------------------------------------------------------------------------------------------------------------------------------
  900. AW1 AMinSW1(AW1 a,AW1 b){return AW1(min(ASU1(a),ASU1(b)));}
  901. AW2 AMinSW2(AW2 a,AW2 b){return AW2(min(ASU2(a),ASU2(b)));}
  902. AW3 AMinSW3(AW3 a,AW3 b){return AW3(min(ASU3(a),ASU3(b)));}
  903. AW4 AMinSW4(AW4 a,AW4 b){return AW4(min(ASU4(a),ASU4(b)));}
  904. //------------------------------------------------------------------------------------------------------------------------------
  905. AH1 ARcpH1(AH1 x){return AH1_(1.0)/x;}
  906. AH2 ARcpH2(AH2 x){return AH2_(1.0)/x;}
  907. AH3 ARcpH3(AH3 x){return AH3_(1.0)/x;}
  908. AH4 ARcpH4(AH4 x){return AH4_(1.0)/x;}
  909. //------------------------------------------------------------------------------------------------------------------------------
  910. AH1 ARsqH1(AH1 x){return AH1_(1.0)/sqrt(x);}
  911. AH2 ARsqH2(AH2 x){return AH2_(1.0)/sqrt(x);}
  912. AH3 ARsqH3(AH3 x){return AH3_(1.0)/sqrt(x);}
  913. AH4 ARsqH4(AH4 x){return AH4_(1.0)/sqrt(x);}
  914. //------------------------------------------------------------------------------------------------------------------------------
  915. AH1 ASatH1(AH1 x){return clamp(x,AH1_(0.0),AH1_(1.0));}
  916. AH2 ASatH2(AH2 x){return clamp(x,AH2_(0.0),AH2_(1.0));}
  917. AH3 ASatH3(AH3 x){return clamp(x,AH3_(0.0),AH3_(1.0));}
  918. AH4 ASatH4(AH4 x){return clamp(x,AH4_(0.0),AH4_(1.0));}
  919. //------------------------------------------------------------------------------------------------------------------------------
  920. AW1 AShrSW1(AW1 a,AW1 b){return AW1(ASW1(a)>>ASW1(b));}
  921. AW2 AShrSW2(AW2 a,AW2 b){return AW2(ASW2(a)>>ASW2(b));}
  922. AW3 AShrSW3(AW3 a,AW3 b){return AW3(ASW3(a)>>ASW3(b));}
  923. AW4 AShrSW4(AW4 a,AW4 b){return AW4(ASW4(a)>>ASW4(b));}
  924. #endif
  925. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  926. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  927. //_____________________________________________________________/\_______________________________________________________________
  928. //==============================================================================================================================
  929. // GLSL DOUBLE
  930. //==============================================================================================================================
  931. #ifdef A_DUBL
  932. #define AD1 double
  933. #define AD2 dvec2
  934. #define AD3 dvec3
  935. #define AD4 dvec4
  936. //------------------------------------------------------------------------------------------------------------------------------
  937. AD1 AD1_x(AD1 a){return AD1(a);}
  938. AD2 AD2_x(AD1 a){return AD2(a,a);}
  939. AD3 AD3_x(AD1 a){return AD3(a,a,a);}
  940. AD4 AD4_x(AD1 a){return AD4(a,a,a,a);}
  941. #define AD1_(a) AD1_x(AD1(a))
  942. #define AD2_(a) AD2_x(AD1(a))
  943. #define AD3_(a) AD3_x(AD1(a))
  944. #define AD4_(a) AD4_x(AD1(a))
  945. //==============================================================================================================================
  946. AD1 AFractD1(AD1 x){return fract(x);}
  947. AD2 AFractD2(AD2 x){return fract(x);}
  948. AD3 AFractD3(AD3 x){return fract(x);}
  949. AD4 AFractD4(AD4 x){return fract(x);}
  950. //------------------------------------------------------------------------------------------------------------------------------
  951. AD1 ALerpD1(AD1 x,AD1 y,AD1 a){return mix(x,y,a);}
  952. AD2 ALerpD2(AD2 x,AD2 y,AD2 a){return mix(x,y,a);}
  953. AD3 ALerpD3(AD3 x,AD3 y,AD3 a){return mix(x,y,a);}
  954. AD4 ALerpD4(AD4 x,AD4 y,AD4 a){return mix(x,y,a);}
  955. //------------------------------------------------------------------------------------------------------------------------------
  956. AD1 ARcpD1(AD1 x){return AD1_(1.0)/x;}
  957. AD2 ARcpD2(AD2 x){return AD2_(1.0)/x;}
  958. AD3 ARcpD3(AD3 x){return AD3_(1.0)/x;}
  959. AD4 ARcpD4(AD4 x){return AD4_(1.0)/x;}
  960. //------------------------------------------------------------------------------------------------------------------------------
  961. AD1 ARsqD1(AD1 x){return AD1_(1.0)/sqrt(x);}
  962. AD2 ARsqD2(AD2 x){return AD2_(1.0)/sqrt(x);}
  963. AD3 ARsqD3(AD3 x){return AD3_(1.0)/sqrt(x);}
  964. AD4 ARsqD4(AD4 x){return AD4_(1.0)/sqrt(x);}
  965. //------------------------------------------------------------------------------------------------------------------------------
  966. AD1 ASatD1(AD1 x){return clamp(x,AD1_(0.0),AD1_(1.0));}
  967. AD2 ASatD2(AD2 x){return clamp(x,AD2_(0.0),AD2_(1.0));}
  968. AD3 ASatD3(AD3 x){return clamp(x,AD3_(0.0),AD3_(1.0));}
  969. AD4 ASatD4(AD4 x){return clamp(x,AD4_(0.0),AD4_(1.0));}
  970. #endif
  971. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  972. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  973. //_____________________________________________________________/\_______________________________________________________________
  974. //==============================================================================================================================
  975. // GLSL LONG
  976. //==============================================================================================================================
  977. #ifdef A_LONG
  978. #define AL1 uint64_t
  979. #define AL2 u64vec2
  980. #define AL3 u64vec3
  981. #define AL4 u64vec4
  982. //------------------------------------------------------------------------------------------------------------------------------
  983. #define ASL1 int64_t
  984. #define ASL2 i64vec2
  985. #define ASL3 i64vec3
  986. #define ASL4 i64vec4
  987. //------------------------------------------------------------------------------------------------------------------------------
  988. #define AL1_AU2(x) packUint2x32(AU2(x))
  989. #define AU2_AL1(x) unpackUint2x32(AL1(x))
  990. //------------------------------------------------------------------------------------------------------------------------------
  991. AL1 AL1_x(AL1 a){return AL1(a);}
  992. AL2 AL2_x(AL1 a){return AL2(a,a);}
  993. AL3 AL3_x(AL1 a){return AL3(a,a,a);}
  994. AL4 AL4_x(AL1 a){return AL4(a,a,a,a);}
  995. #define AL1_(a) AL1_x(AL1(a))
  996. #define AL2_(a) AL2_x(AL1(a))
  997. #define AL3_(a) AL3_x(AL1(a))
  998. #define AL4_(a) AL4_x(AL1(a))
  999. //==============================================================================================================================
  1000. AL1 AAbsSL1(AL1 a){return AL1(abs(ASL1(a)));}
  1001. AL2 AAbsSL2(AL2 a){return AL2(abs(ASL2(a)));}
  1002. AL3 AAbsSL3(AL3 a){return AL3(abs(ASL3(a)));}
  1003. AL4 AAbsSL4(AL4 a){return AL4(abs(ASL4(a)));}
  1004. //------------------------------------------------------------------------------------------------------------------------------
  1005. AL1 AMaxSL1(AL1 a,AL1 b){return AL1(max(ASU1(a),ASU1(b)));}
  1006. AL2 AMaxSL2(AL2 a,AL2 b){return AL2(max(ASU2(a),ASU2(b)));}
  1007. AL3 AMaxSL3(AL3 a,AL3 b){return AL3(max(ASU3(a),ASU3(b)));}
  1008. AL4 AMaxSL4(AL4 a,AL4 b){return AL4(max(ASU4(a),ASU4(b)));}
  1009. //------------------------------------------------------------------------------------------------------------------------------
  1010. AL1 AMinSL1(AL1 a,AL1 b){return AL1(min(ASU1(a),ASU1(b)));}
  1011. AL2 AMinSL2(AL2 a,AL2 b){return AL2(min(ASU2(a),ASU2(b)));}
  1012. AL3 AMinSL3(AL3 a,AL3 b){return AL3(min(ASU3(a),ASU3(b)));}
  1013. AL4 AMinSL4(AL4 a,AL4 b){return AL4(min(ASU4(a),ASU4(b)));}
  1014. #endif
  1015. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1016. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1017. //_____________________________________________________________/\_______________________________________________________________
  1018. //==============================================================================================================================
  1019. // WAVE OPERATIONS
  1020. //==============================================================================================================================
  1021. #ifdef A_WAVE
  1022. // Where 'x' must be a compile time literal.
  1023. AF1 AWaveXorF1(AF1 v,AU1 x){return subgroupShuffleXor(v,x);}
  1024. AF2 AWaveXorF2(AF2 v,AU1 x){return subgroupShuffleXor(v,x);}
  1025. AF3 AWaveXorF3(AF3 v,AU1 x){return subgroupShuffleXor(v,x);}
  1026. AF4 AWaveXorF4(AF4 v,AU1 x){return subgroupShuffleXor(v,x);}
  1027. AU1 AWaveXorU1(AU1 v,AU1 x){return subgroupShuffleXor(v,x);}
  1028. AU2 AWaveXorU2(AU2 v,AU1 x){return subgroupShuffleXor(v,x);}
  1029. AU3 AWaveXorU3(AU3 v,AU1 x){return subgroupShuffleXor(v,x);}
  1030. AU4 AWaveXorU4(AU4 v,AU1 x){return subgroupShuffleXor(v,x);}
  1031. //------------------------------------------------------------------------------------------------------------------------------
  1032. #ifdef A_HALF
  1033. AH2 AWaveXorH2(AH2 v,AU1 x){return AH2_AU1(subgroupShuffleXor(AU1_AH2(v),x));}
  1034. AH4 AWaveXorH4(AH4 v,AU1 x){return AH4_AU2(subgroupShuffleXor(AU2_AH4(v),x));}
  1035. AW2 AWaveXorW2(AW2 v,AU1 x){return AW2_AU1(subgroupShuffleXor(AU1_AW2(v),x));}
  1036. AW4 AWaveXorW4(AW4 v,AU1 x){return AW4_AU2(subgroupShuffleXor(AU2_AW4(v),x));}
  1037. #endif
  1038. #endif
  1039. //==============================================================================================================================
  1040. #endif
  1041. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1042. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1043. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1044. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1045. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1046. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1047. //_____________________________________________________________/\_______________________________________________________________
  1048. //==============================================================================================================================
  1049. //
  1050. //
  1051. // HLSL
  1052. //
  1053. //
  1054. //==============================================================================================================================
  1055. #if defined(A_HLSL) && defined(A_GPU)
  1056. #ifdef A_HLSL_6_2
  1057. #define AP1 bool
  1058. #define AP2 bool2
  1059. #define AP3 bool3
  1060. #define AP4 bool4
  1061. //------------------------------------------------------------------------------------------------------------------------------
  1062. #define AF1 float32_t
  1063. #define AF2 float32_t2
  1064. #define AF3 float32_t3
  1065. #define AF4 float32_t4
  1066. //------------------------------------------------------------------------------------------------------------------------------
  1067. #define AU1 uint32_t
  1068. #define AU2 uint32_t2
  1069. #define AU3 uint32_t3
  1070. #define AU4 uint32_t4
  1071. //------------------------------------------------------------------------------------------------------------------------------
  1072. #define ASU1 int32_t
  1073. #define ASU2 int32_t2
  1074. #define ASU3 int32_t3
  1075. #define ASU4 int32_t4
  1076. #else
  1077. #define AP1 bool
  1078. #define AP2 bool2
  1079. #define AP3 bool3
  1080. #define AP4 bool4
  1081. //------------------------------------------------------------------------------------------------------------------------------
  1082. #define AF1 float
  1083. #define AF2 float2
  1084. #define AF3 float3
  1085. #define AF4 float4
  1086. //------------------------------------------------------------------------------------------------------------------------------
  1087. #define AU1 uint
  1088. #define AU2 uint2
  1089. #define AU3 uint3
  1090. #define AU4 uint4
  1091. //------------------------------------------------------------------------------------------------------------------------------
  1092. #define ASU1 int
  1093. #define ASU2 int2
  1094. #define ASU3 int3
  1095. #define ASU4 int4
  1096. #endif
  1097. //==============================================================================================================================
  1098. #define AF1_AU1(x) asfloat(AU1(x))
  1099. #define AF2_AU2(x) asfloat(AU2(x))
  1100. #define AF3_AU3(x) asfloat(AU3(x))
  1101. #define AF4_AU4(x) asfloat(AU4(x))
  1102. //------------------------------------------------------------------------------------------------------------------------------
  1103. #define AU1_AF1(x) asuint(AF1(x))
  1104. #define AU2_AF2(x) asuint(AF2(x))
  1105. #define AU3_AF3(x) asuint(AF3(x))
  1106. #define AU4_AF4(x) asuint(AF4(x))
  1107. //------------------------------------------------------------------------------------------------------------------------------
  1108. AU1 AU1_AH1_AF1_x(AF1 a){return f32tof16(a);}
  1109. #define AU1_AH1_AF1(a) AU1_AH1_AF1_x(AF1(a))
  1110. //------------------------------------------------------------------------------------------------------------------------------
  1111. AU1 AU1_AH2_AF2_x(AF2 a){return f32tof16(a.x)|(f32tof16(a.y)<<16);}
  1112. #define AU1_AH2_AF2(a) AU1_AH2_AF2_x(AF2(a))
  1113. #define AU1_AB4Unorm_AF4(x) D3DCOLORtoUBYTE4(AF4(x))
  1114. //------------------------------------------------------------------------------------------------------------------------------
  1115. AF2 AF2_AH2_AU1_x(AU1 x){return AF2(f16tof32(x&0xFFFF),f16tof32(x>>16));}
  1116. #define AF2_AH2_AU1(x) AF2_AH2_AU1_x(AU1(x))
  1117. //==============================================================================================================================
  1118. AF1 AF1_x(AF1 a){return AF1(a);}
  1119. AF2 AF2_x(AF1 a){return AF2(a,a);}
  1120. AF3 AF3_x(AF1 a){return AF3(a,a,a);}
  1121. AF4 AF4_x(AF1 a){return AF4(a,a,a,a);}
  1122. #define AF1_(a) AF1_x(AF1(a))
  1123. #define AF2_(a) AF2_x(AF1(a))
  1124. #define AF3_(a) AF3_x(AF1(a))
  1125. #define AF4_(a) AF4_x(AF1(a))
  1126. //------------------------------------------------------------------------------------------------------------------------------
  1127. AU1 AU1_x(AU1 a){return AU1(a);}
  1128. AU2 AU2_x(AU1 a){return AU2(a,a);}
  1129. AU3 AU3_x(AU1 a){return AU3(a,a,a);}
  1130. AU4 AU4_x(AU1 a){return AU4(a,a,a,a);}
  1131. #define AU1_(a) AU1_x(AU1(a))
  1132. #define AU2_(a) AU2_x(AU1(a))
  1133. #define AU3_(a) AU3_x(AU1(a))
  1134. #define AU4_(a) AU4_x(AU1(a))
  1135. //==============================================================================================================================
  1136. AU1 AAbsSU1(AU1 a){return AU1(abs(ASU1(a)));}
  1137. AU2 AAbsSU2(AU2 a){return AU2(abs(ASU2(a)));}
  1138. AU3 AAbsSU3(AU3 a){return AU3(abs(ASU3(a)));}
  1139. AU4 AAbsSU4(AU4 a){return AU4(abs(ASU4(a)));}
  1140. //------------------------------------------------------------------------------------------------------------------------------
  1141. AU1 ABfe(AU1 src,AU1 off,AU1 bits){AU1 mask=(1u<<bits)-1;return (src>>off)&mask;}
  1142. AU1 ABfi(AU1 src,AU1 ins,AU1 mask){return (ins&mask)|(src&(~mask));}
  1143. AU1 ABfiM(AU1 src,AU1 ins,AU1 bits){AU1 mask=(1u<<bits)-1;return (ins&mask)|(src&(~mask));}
  1144. //------------------------------------------------------------------------------------------------------------------------------
  1145. AF1 AClampF1(AF1 x,AF1 n,AF1 m){return max(n,min(x,m));}
  1146. AF2 AClampF2(AF2 x,AF2 n,AF2 m){return max(n,min(x,m));}
  1147. AF3 AClampF3(AF3 x,AF3 n,AF3 m){return max(n,min(x,m));}
  1148. AF4 AClampF4(AF4 x,AF4 n,AF4 m){return max(n,min(x,m));}
  1149. //------------------------------------------------------------------------------------------------------------------------------
  1150. AF1 AFractF1(AF1 x){return x-floor(x);}
  1151. AF2 AFractF2(AF2 x){return x-floor(x);}
  1152. AF3 AFractF3(AF3 x){return x-floor(x);}
  1153. AF4 AFractF4(AF4 x){return x-floor(x);}
  1154. //------------------------------------------------------------------------------------------------------------------------------
  1155. AF1 ALerpF1(AF1 x,AF1 y,AF1 a){return lerp(x,y,a);}
  1156. AF2 ALerpF2(AF2 x,AF2 y,AF2 a){return lerp(x,y,a);}
  1157. AF3 ALerpF3(AF3 x,AF3 y,AF3 a){return lerp(x,y,a);}
  1158. AF4 ALerpF4(AF4 x,AF4 y,AF4 a){return lerp(x,y,a);}
  1159. //------------------------------------------------------------------------------------------------------------------------------
  1160. AF1 AMax3F1(AF1 x,AF1 y,AF1 z){return max(x,max(y,z));}
  1161. AF2 AMax3F2(AF2 x,AF2 y,AF2 z){return max(x,max(y,z));}
  1162. AF3 AMax3F3(AF3 x,AF3 y,AF3 z){return max(x,max(y,z));}
  1163. AF4 AMax3F4(AF4 x,AF4 y,AF4 z){return max(x,max(y,z));}
  1164. //------------------------------------------------------------------------------------------------------------------------------
  1165. AU1 AMax3SU1(AU1 x,AU1 y,AU1 z){return AU1(max(ASU1(x),max(ASU1(y),ASU1(z))));}
  1166. AU2 AMax3SU2(AU2 x,AU2 y,AU2 z){return AU2(max(ASU2(x),max(ASU2(y),ASU2(z))));}
  1167. AU3 AMax3SU3(AU3 x,AU3 y,AU3 z){return AU3(max(ASU3(x),max(ASU3(y),ASU3(z))));}
  1168. AU4 AMax3SU4(AU4 x,AU4 y,AU4 z){return AU4(max(ASU4(x),max(ASU4(y),ASU4(z))));}
  1169. //------------------------------------------------------------------------------------------------------------------------------
  1170. AU1 AMax3U1(AU1 x,AU1 y,AU1 z){return max(x,max(y,z));}
  1171. AU2 AMax3U2(AU2 x,AU2 y,AU2 z){return max(x,max(y,z));}
  1172. AU3 AMax3U3(AU3 x,AU3 y,AU3 z){return max(x,max(y,z));}
  1173. AU4 AMax3U4(AU4 x,AU4 y,AU4 z){return max(x,max(y,z));}
  1174. //------------------------------------------------------------------------------------------------------------------------------
  1175. AU1 AMaxSU1(AU1 a,AU1 b){return AU1(max(ASU1(a),ASU1(b)));}
  1176. AU2 AMaxSU2(AU2 a,AU2 b){return AU2(max(ASU2(a),ASU2(b)));}
  1177. AU3 AMaxSU3(AU3 a,AU3 b){return AU3(max(ASU3(a),ASU3(b)));}
  1178. AU4 AMaxSU4(AU4 a,AU4 b){return AU4(max(ASU4(a),ASU4(b)));}
  1179. //------------------------------------------------------------------------------------------------------------------------------
  1180. AF1 AMed3F1(AF1 x,AF1 y,AF1 z){return max(min(x,y),min(max(x,y),z));}
  1181. AF2 AMed3F2(AF2 x,AF2 y,AF2 z){return max(min(x,y),min(max(x,y),z));}
  1182. AF3 AMed3F3(AF3 x,AF3 y,AF3 z){return max(min(x,y),min(max(x,y),z));}
  1183. AF4 AMed3F4(AF4 x,AF4 y,AF4 z){return max(min(x,y),min(max(x,y),z));}
  1184. //------------------------------------------------------------------------------------------------------------------------------
  1185. AF1 AMin3F1(AF1 x,AF1 y,AF1 z){return min(x,min(y,z));}
  1186. AF2 AMin3F2(AF2 x,AF2 y,AF2 z){return min(x,min(y,z));}
  1187. AF3 AMin3F3(AF3 x,AF3 y,AF3 z){return min(x,min(y,z));}
  1188. AF4 AMin3F4(AF4 x,AF4 y,AF4 z){return min(x,min(y,z));}
  1189. //------------------------------------------------------------------------------------------------------------------------------
  1190. AU1 AMin3SU1(AU1 x,AU1 y,AU1 z){return AU1(min(ASU1(x),min(ASU1(y),ASU1(z))));}
  1191. AU2 AMin3SU2(AU2 x,AU2 y,AU2 z){return AU2(min(ASU2(x),min(ASU2(y),ASU2(z))));}
  1192. AU3 AMin3SU3(AU3 x,AU3 y,AU3 z){return AU3(min(ASU3(x),min(ASU3(y),ASU3(z))));}
  1193. AU4 AMin3SU4(AU4 x,AU4 y,AU4 z){return AU4(min(ASU4(x),min(ASU4(y),ASU4(z))));}
  1194. //------------------------------------------------------------------------------------------------------------------------------
  1195. AU1 AMin3U1(AU1 x,AU1 y,AU1 z){return min(x,min(y,z));}
  1196. AU2 AMin3U2(AU2 x,AU2 y,AU2 z){return min(x,min(y,z));}
  1197. AU3 AMin3U3(AU3 x,AU3 y,AU3 z){return min(x,min(y,z));}
  1198. AU4 AMin3U4(AU4 x,AU4 y,AU4 z){return min(x,min(y,z));}
  1199. //------------------------------------------------------------------------------------------------------------------------------
  1200. AU1 AMinSU1(AU1 a,AU1 b){return AU1(min(ASU1(a),ASU1(b)));}
  1201. AU2 AMinSU2(AU2 a,AU2 b){return AU2(min(ASU2(a),ASU2(b)));}
  1202. AU3 AMinSU3(AU3 a,AU3 b){return AU3(min(ASU3(a),ASU3(b)));}
  1203. AU4 AMinSU4(AU4 a,AU4 b){return AU4(min(ASU4(a),ASU4(b)));}
  1204. //------------------------------------------------------------------------------------------------------------------------------
  1205. AF1 ANCosF1(AF1 x){return cos(x*AF1_(A_2PI));}
  1206. AF2 ANCosF2(AF2 x){return cos(x*AF2_(A_2PI));}
  1207. AF3 ANCosF3(AF3 x){return cos(x*AF3_(A_2PI));}
  1208. AF4 ANCosF4(AF4 x){return cos(x*AF4_(A_2PI));}
  1209. //------------------------------------------------------------------------------------------------------------------------------
  1210. AF1 ANSinF1(AF1 x){return sin(x*AF1_(A_2PI));}
  1211. AF2 ANSinF2(AF2 x){return sin(x*AF2_(A_2PI));}
  1212. AF3 ANSinF3(AF3 x){return sin(x*AF3_(A_2PI));}
  1213. AF4 ANSinF4(AF4 x){return sin(x*AF4_(A_2PI));}
  1214. //------------------------------------------------------------------------------------------------------------------------------
  1215. AF1 ARcpF1(AF1 x){return rcp(x);}
  1216. AF2 ARcpF2(AF2 x){return rcp(x);}
  1217. AF3 ARcpF3(AF3 x){return rcp(x);}
  1218. AF4 ARcpF4(AF4 x){return rcp(x);}
  1219. //------------------------------------------------------------------------------------------------------------------------------
  1220. AF1 ARsqF1(AF1 x){return rsqrt(x);}
  1221. AF2 ARsqF2(AF2 x){return rsqrt(x);}
  1222. AF3 ARsqF3(AF3 x){return rsqrt(x);}
  1223. AF4 ARsqF4(AF4 x){return rsqrt(x);}
  1224. //------------------------------------------------------------------------------------------------------------------------------
  1225. AF1 ASatF1(AF1 x){return saturate(x);}
  1226. AF2 ASatF2(AF2 x){return saturate(x);}
  1227. AF3 ASatF3(AF3 x){return saturate(x);}
  1228. AF4 ASatF4(AF4 x){return saturate(x);}
  1229. //------------------------------------------------------------------------------------------------------------------------------
  1230. AU1 AShrSU1(AU1 a,AU1 b){return AU1(ASU1(a)>>ASU1(b));}
  1231. AU2 AShrSU2(AU2 a,AU2 b){return AU2(ASU2(a)>>ASU2(b));}
  1232. AU3 AShrSU3(AU3 a,AU3 b){return AU3(ASU3(a)>>ASU3(b));}
  1233. AU4 AShrSU4(AU4 a,AU4 b){return AU4(ASU4(a)>>ASU4(b));}
  1234. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1235. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1236. //_____________________________________________________________/\_______________________________________________________________
  1237. //==============================================================================================================================
  1238. // HLSL BYTE
  1239. //==============================================================================================================================
  1240. #ifdef A_BYTE
  1241. #endif
  1242. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1243. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1244. //_____________________________________________________________/\_______________________________________________________________
  1245. //==============================================================================================================================
  1246. // HLSL HALF
  1247. //==============================================================================================================================
  1248. #ifdef A_HALF
  1249. #ifdef A_HLSL_6_2
  1250. #define AH1 float16_t
  1251. #define AH2 float16_t2
  1252. #define AH3 float16_t3
  1253. #define AH4 float16_t4
  1254. //------------------------------------------------------------------------------------------------------------------------------
  1255. #define AW1 uint16_t
  1256. #define AW2 uint16_t2
  1257. #define AW3 uint16_t3
  1258. #define AW4 uint16_t4
  1259. //------------------------------------------------------------------------------------------------------------------------------
  1260. #define ASW1 int16_t
  1261. #define ASW2 int16_t2
  1262. #define ASW3 int16_t3
  1263. #define ASW4 int16_t4
  1264. #else
  1265. #define AH1 min16float
  1266. #define AH2 min16float2
  1267. #define AH3 min16float3
  1268. #define AH4 min16float4
  1269. //------------------------------------------------------------------------------------------------------------------------------
  1270. #define AW1 min16uint
  1271. #define AW2 min16uint2
  1272. #define AW3 min16uint3
  1273. #define AW4 min16uint4
  1274. //------------------------------------------------------------------------------------------------------------------------------
  1275. #define ASW1 min16int
  1276. #define ASW2 min16int2
  1277. #define ASW3 min16int3
  1278. #define ASW4 min16int4
  1279. #endif
  1280. //==============================================================================================================================
  1281. // Need to use manual unpack to get optimal execution (don't use packed types in buffers directly).
  1282. // Unpack requires this pattern: https://gpuopen.com/first-steps-implementing-fp16/
  1283. AH2 AH2_AU1_x(AU1 x){AF2 t=f16tof32(AU2(x&0xFFFF,x>>16));return AH2(t);}
  1284. AH4 AH4_AU2_x(AU2 x){return AH4(AH2_AU1_x(x.x),AH2_AU1_x(x.y));}
  1285. AW2 AW2_AU1_x(AU1 x){AU2 t=AU2(x&0xFFFF,x>>16);return AW2(t);}
  1286. AW4 AW4_AU2_x(AU2 x){return AW4(AW2_AU1_x(x.x),AW2_AU1_x(x.y));}
  1287. #define AH2_AU1(x) AH2_AU1_x(AU1(x))
  1288. #define AH4_AU2(x) AH4_AU2_x(AU2(x))
  1289. #define AW2_AU1(x) AW2_AU1_x(AU1(x))
  1290. #define AW4_AU2(x) AW4_AU2_x(AU2(x))
  1291. //------------------------------------------------------------------------------------------------------------------------------
  1292. AU1 AU1_AH2_x(AH2 x){return f32tof16(x.x)+(f32tof16(x.y)<<16);}
  1293. AU2 AU2_AH4_x(AH4 x){return AU2(AU1_AH2_x(x.xy),AU1_AH2_x(x.zw));}
  1294. AU1 AU1_AW2_x(AW2 x){return AU1(x.x)+(AU1(x.y)<<16);}
  1295. AU2 AU2_AW4_x(AW4 x){return AU2(AU1_AW2_x(x.xy),AU1_AW2_x(x.zw));}
  1296. #define AU1_AH2(x) AU1_AH2_x(AH2(x))
  1297. #define AU2_AH4(x) AU2_AH4_x(AH4(x))
  1298. #define AU1_AW2(x) AU1_AW2_x(AW2(x))
  1299. #define AU2_AW4(x) AU2_AW4_x(AW4(x))
  1300. //==============================================================================================================================
  1301. #if defined(A_HLSL_6_2) && !defined(A_NO_16_BIT_CAST)
  1302. #define AW1_AH1(x) asuint16(x)
  1303. #define AW2_AH2(x) asuint16(x)
  1304. #define AW3_AH3(x) asuint16(x)
  1305. #define AW4_AH4(x) asuint16(x)
  1306. #else
  1307. #define AW1_AH1(a) AW1(f32tof16(AF1(a)))
  1308. #define AW2_AH2(a) AW2(AW1_AH1((a).x),AW1_AH1((a).y))
  1309. #define AW3_AH3(a) AW3(AW1_AH1((a).x),AW1_AH1((a).y),AW1_AH1((a).z))
  1310. #define AW4_AH4(a) AW4(AW1_AH1((a).x),AW1_AH1((a).y),AW1_AH1((a).z),AW1_AH1((a).w))
  1311. #endif
  1312. //------------------------------------------------------------------------------------------------------------------------------
  1313. #if defined(A_HLSL_6_2) && !defined(A_NO_16_BIT_CAST)
  1314. #define AH1_AW1(x) asfloat16(x)
  1315. #define AH2_AW2(x) asfloat16(x)
  1316. #define AH3_AW3(x) asfloat16(x)
  1317. #define AH4_AW4(x) asfloat16(x)
  1318. #else
  1319. #define AH1_AW1(a) AH1(f16tof32(AU1(a)))
  1320. #define AH2_AW2(a) AH2(AH1_AW1((a).x),AH1_AW1((a).y))
  1321. #define AH3_AW3(a) AH3(AH1_AW1((a).x),AH1_AW1((a).y),AH1_AW1((a).z))
  1322. #define AH4_AW4(a) AH4(AH1_AW1((a).x),AH1_AW1((a).y),AH1_AW1((a).z),AH1_AW1((a).w))
  1323. #endif
  1324. //==============================================================================================================================
  1325. AH1 AH1_x(AH1 a){return AH1(a);}
  1326. AH2 AH2_x(AH1 a){return AH2(a,a);}
  1327. AH3 AH3_x(AH1 a){return AH3(a,a,a);}
  1328. AH4 AH4_x(AH1 a){return AH4(a,a,a,a);}
  1329. #define AH1_(a) AH1_x(AH1(a))
  1330. #define AH2_(a) AH2_x(AH1(a))
  1331. #define AH3_(a) AH3_x(AH1(a))
  1332. #define AH4_(a) AH4_x(AH1(a))
  1333. //------------------------------------------------------------------------------------------------------------------------------
  1334. AW1 AW1_x(AW1 a){return AW1(a);}
  1335. AW2 AW2_x(AW1 a){return AW2(a,a);}
  1336. AW3 AW3_x(AW1 a){return AW3(a,a,a);}
  1337. AW4 AW4_x(AW1 a){return AW4(a,a,a,a);}
  1338. #define AW1_(a) AW1_x(AW1(a))
  1339. #define AW2_(a) AW2_x(AW1(a))
  1340. #define AW3_(a) AW3_x(AW1(a))
  1341. #define AW4_(a) AW4_x(AW1(a))
  1342. //==============================================================================================================================
  1343. AW1 AAbsSW1(AW1 a){return AW1(abs(ASW1(a)));}
  1344. AW2 AAbsSW2(AW2 a){return AW2(abs(ASW2(a)));}
  1345. AW3 AAbsSW3(AW3 a){return AW3(abs(ASW3(a)));}
  1346. AW4 AAbsSW4(AW4 a){return AW4(abs(ASW4(a)));}
  1347. //------------------------------------------------------------------------------------------------------------------------------
  1348. AH1 AClampH1(AH1 x,AH1 n,AH1 m){return max(n,min(x,m));}
  1349. AH2 AClampH2(AH2 x,AH2 n,AH2 m){return max(n,min(x,m));}
  1350. AH3 AClampH3(AH3 x,AH3 n,AH3 m){return max(n,min(x,m));}
  1351. AH4 AClampH4(AH4 x,AH4 n,AH4 m){return max(n,min(x,m));}
  1352. //------------------------------------------------------------------------------------------------------------------------------
  1353. // V_FRACT_F16 (note DX frac() is different).
  1354. AH1 AFractH1(AH1 x){return x-floor(x);}
  1355. AH2 AFractH2(AH2 x){return x-floor(x);}
  1356. AH3 AFractH3(AH3 x){return x-floor(x);}
  1357. AH4 AFractH4(AH4 x){return x-floor(x);}
  1358. //------------------------------------------------------------------------------------------------------------------------------
  1359. AH1 ALerpH1(AH1 x,AH1 y,AH1 a){return lerp(x,y,a);}
  1360. AH2 ALerpH2(AH2 x,AH2 y,AH2 a){return lerp(x,y,a);}
  1361. AH3 ALerpH3(AH3 x,AH3 y,AH3 a){return lerp(x,y,a);}
  1362. AH4 ALerpH4(AH4 x,AH4 y,AH4 a){return lerp(x,y,a);}
  1363. //------------------------------------------------------------------------------------------------------------------------------
  1364. AH1 AMax3H1(AH1 x,AH1 y,AH1 z){return max(x,max(y,z));}
  1365. AH2 AMax3H2(AH2 x,AH2 y,AH2 z){return max(x,max(y,z));}
  1366. AH3 AMax3H3(AH3 x,AH3 y,AH3 z){return max(x,max(y,z));}
  1367. AH4 AMax3H4(AH4 x,AH4 y,AH4 z){return max(x,max(y,z));}
  1368. //------------------------------------------------------------------------------------------------------------------------------
  1369. AW1 AMaxSW1(AW1 a,AW1 b){return AW1(max(ASU1(a),ASU1(b)));}
  1370. AW2 AMaxSW2(AW2 a,AW2 b){return AW2(max(ASU2(a),ASU2(b)));}
  1371. AW3 AMaxSW3(AW3 a,AW3 b){return AW3(max(ASU3(a),ASU3(b)));}
  1372. AW4 AMaxSW4(AW4 a,AW4 b){return AW4(max(ASU4(a),ASU4(b)));}
  1373. //------------------------------------------------------------------------------------------------------------------------------
  1374. AH1 AMin3H1(AH1 x,AH1 y,AH1 z){return min(x,min(y,z));}
  1375. AH2 AMin3H2(AH2 x,AH2 y,AH2 z){return min(x,min(y,z));}
  1376. AH3 AMin3H3(AH3 x,AH3 y,AH3 z){return min(x,min(y,z));}
  1377. AH4 AMin3H4(AH4 x,AH4 y,AH4 z){return min(x,min(y,z));}
  1378. //------------------------------------------------------------------------------------------------------------------------------
  1379. AW1 AMinSW1(AW1 a,AW1 b){return AW1(min(ASU1(a),ASU1(b)));}
  1380. AW2 AMinSW2(AW2 a,AW2 b){return AW2(min(ASU2(a),ASU2(b)));}
  1381. AW3 AMinSW3(AW3 a,AW3 b){return AW3(min(ASU3(a),ASU3(b)));}
  1382. AW4 AMinSW4(AW4 a,AW4 b){return AW4(min(ASU4(a),ASU4(b)));}
  1383. //------------------------------------------------------------------------------------------------------------------------------
  1384. AH1 ARcpH1(AH1 x){return rcp(x);}
  1385. AH2 ARcpH2(AH2 x){return rcp(x);}
  1386. AH3 ARcpH3(AH3 x){return rcp(x);}
  1387. AH4 ARcpH4(AH4 x){return rcp(x);}
  1388. //------------------------------------------------------------------------------------------------------------------------------
  1389. AH1 ARsqH1(AH1 x){return rsqrt(x);}
  1390. AH2 ARsqH2(AH2 x){return rsqrt(x);}
  1391. AH3 ARsqH3(AH3 x){return rsqrt(x);}
  1392. AH4 ARsqH4(AH4 x){return rsqrt(x);}
  1393. //------------------------------------------------------------------------------------------------------------------------------
  1394. AH1 ASatH1(AH1 x){return saturate(x);}
  1395. AH2 ASatH2(AH2 x){return saturate(x);}
  1396. AH3 ASatH3(AH3 x){return saturate(x);}
  1397. AH4 ASatH4(AH4 x){return saturate(x);}
  1398. //------------------------------------------------------------------------------------------------------------------------------
  1399. AW1 AShrSW1(AW1 a,AW1 b){return AW1(ASW1(a)>>ASW1(b));}
  1400. AW2 AShrSW2(AW2 a,AW2 b){return AW2(ASW2(a)>>ASW2(b));}
  1401. AW3 AShrSW3(AW3 a,AW3 b){return AW3(ASW3(a)>>ASW3(b));}
  1402. AW4 AShrSW4(AW4 a,AW4 b){return AW4(ASW4(a)>>ASW4(b));}
  1403. #endif
  1404. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1405. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1406. //_____________________________________________________________/\_______________________________________________________________
  1407. //==============================================================================================================================
  1408. // HLSL DOUBLE
  1409. //==============================================================================================================================
  1410. #ifdef A_DUBL
  1411. #ifdef A_HLSL_6_2
  1412. #define AD1 float64_t
  1413. #define AD2 float64_t2
  1414. #define AD3 float64_t3
  1415. #define AD4 float64_t4
  1416. #else
  1417. #define AD1 double
  1418. #define AD2 double2
  1419. #define AD3 double3
  1420. #define AD4 double4
  1421. #endif
  1422. //------------------------------------------------------------------------------------------------------------------------------
  1423. AD1 AD1_x(AD1 a){return AD1(a);}
  1424. AD2 AD2_x(AD1 a){return AD2(a,a);}
  1425. AD3 AD3_x(AD1 a){return AD3(a,a,a);}
  1426. AD4 AD4_x(AD1 a){return AD4(a,a,a,a);}
  1427. #define AD1_(a) AD1_x(AD1(a))
  1428. #define AD2_(a) AD2_x(AD1(a))
  1429. #define AD3_(a) AD3_x(AD1(a))
  1430. #define AD4_(a) AD4_x(AD1(a))
  1431. //==============================================================================================================================
  1432. AD1 AFractD1(AD1 a){return a-floor(a);}
  1433. AD2 AFractD2(AD2 a){return a-floor(a);}
  1434. AD3 AFractD3(AD3 a){return a-floor(a);}
  1435. AD4 AFractD4(AD4 a){return a-floor(a);}
  1436. //------------------------------------------------------------------------------------------------------------------------------
  1437. AD1 ALerpD1(AD1 x,AD1 y,AD1 a){return lerp(x,y,a);}
  1438. AD2 ALerpD2(AD2 x,AD2 y,AD2 a){return lerp(x,y,a);}
  1439. AD3 ALerpD3(AD3 x,AD3 y,AD3 a){return lerp(x,y,a);}
  1440. AD4 ALerpD4(AD4 x,AD4 y,AD4 a){return lerp(x,y,a);}
  1441. //------------------------------------------------------------------------------------------------------------------------------
  1442. AD1 ARcpD1(AD1 x){return rcp(x);}
  1443. AD2 ARcpD2(AD2 x){return rcp(x);}
  1444. AD3 ARcpD3(AD3 x){return rcp(x);}
  1445. AD4 ARcpD4(AD4 x){return rcp(x);}
  1446. //------------------------------------------------------------------------------------------------------------------------------
  1447. AD1 ARsqD1(AD1 x){return rsqrt(x);}
  1448. AD2 ARsqD2(AD2 x){return rsqrt(x);}
  1449. AD3 ARsqD3(AD3 x){return rsqrt(x);}
  1450. AD4 ARsqD4(AD4 x){return rsqrt(x);}
  1451. //------------------------------------------------------------------------------------------------------------------------------
  1452. AD1 ASatD1(AD1 x){return saturate(x);}
  1453. AD2 ASatD2(AD2 x){return saturate(x);}
  1454. AD3 ASatD3(AD3 x){return saturate(x);}
  1455. AD4 ASatD4(AD4 x){return saturate(x);}
  1456. #endif
  1457. //==============================================================================================================================
  1458. // HLSL WAVE
  1459. //==============================================================================================================================
  1460. #ifdef A_WAVE
  1461. // Where 'x' must be a compile time literal.
  1462. AF1 AWaveXorF1(AF1 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
  1463. AF2 AWaveXorF2(AF2 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
  1464. AF3 AWaveXorF3(AF3 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
  1465. AF4 AWaveXorF4(AF4 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
  1466. AU1 AWaveXorU1(AU1 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
  1467. AU2 AWaveXorU1(AU2 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
  1468. AU3 AWaveXorU1(AU3 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
  1469. AU4 AWaveXorU1(AU4 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
  1470. //------------------------------------------------------------------------------------------------------------------------------
  1471. #ifdef A_HALF
  1472. AH2 AWaveXorH2(AH2 v,AU1 x){return AH2_AU1(WaveReadLaneAt(AU1_AH2(v),WaveGetLaneIndex()^x));}
  1473. AH4 AWaveXorH4(AH4 v,AU1 x){return AH4_AU2(WaveReadLaneAt(AU2_AH4(v),WaveGetLaneIndex()^x));}
  1474. AW2 AWaveXorW2(AW2 v,AU1 x){return AW2_AU1(WaveReadLaneAt(AU1_AW2(v),WaveGetLaneIndex()^x));}
  1475. AW4 AWaveXorW4(AW4 v,AU1 x){return AW4_AU1(WaveReadLaneAt(AU1_AW4(v),WaveGetLaneIndex()^x));}
  1476. #endif
  1477. #endif
  1478. //==============================================================================================================================
  1479. #endif
  1480. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1481. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1482. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1483. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1484. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1485. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1486. //_____________________________________________________________/\_______________________________________________________________
  1487. //==============================================================================================================================
  1488. //
  1489. //
  1490. // GPU COMMON
  1491. //
  1492. //
  1493. //==============================================================================================================================
  1494. #ifdef A_GPU
  1495. // Negative and positive infinity.
  1496. #define A_INFP_F AF1_AU1(0x7f800000u)
  1497. #define A_INFN_F AF1_AU1(0xff800000u)
  1498. //------------------------------------------------------------------------------------------------------------------------------
  1499. // Copy sign from 's' to positive 'd'.
  1500. AF1 ACpySgnF1(AF1 d,AF1 s){return AF1_AU1(AU1_AF1(d)|(AU1_AF1(s)&AU1_(0x80000000u)));}
  1501. AF2 ACpySgnF2(AF2 d,AF2 s){return AF2_AU2(AU2_AF2(d)|(AU2_AF2(s)&AU2_(0x80000000u)));}
  1502. AF3 ACpySgnF3(AF3 d,AF3 s){return AF3_AU3(AU3_AF3(d)|(AU3_AF3(s)&AU3_(0x80000000u)));}
  1503. AF4 ACpySgnF4(AF4 d,AF4 s){return AF4_AU4(AU4_AF4(d)|(AU4_AF4(s)&AU4_(0x80000000u)));}
  1504. //------------------------------------------------------------------------------------------------------------------------------
  1505. // Single operation to return (useful to create a mask to use in lerp for branch free logic),
  1506. // m=NaN := 0
  1507. // m>=0 := 0
  1508. // m<0 := 1
  1509. // Uses the following useful floating point logic,
  1510. // saturate(+a*(-INF)==-INF) := 0
  1511. // saturate( 0*(-INF)== NaN) := 0
  1512. // saturate(-a*(-INF)==+INF) := 1
  1513. AF1 ASignedF1(AF1 m){return ASatF1(m*AF1_(A_INFN_F));}
  1514. AF2 ASignedF2(AF2 m){return ASatF2(m*AF2_(A_INFN_F));}
  1515. AF3 ASignedF3(AF3 m){return ASatF3(m*AF3_(A_INFN_F));}
  1516. AF4 ASignedF4(AF4 m){return ASatF4(m*AF4_(A_INFN_F));}
  1517. //------------------------------------------------------------------------------------------------------------------------------
  1518. AF1 AGtZeroF1(AF1 m){return ASatF1(m*AF1_(A_INFP_F));}
  1519. AF2 AGtZeroF2(AF2 m){return ASatF2(m*AF2_(A_INFP_F));}
  1520. AF3 AGtZeroF3(AF3 m){return ASatF3(m*AF3_(A_INFP_F));}
  1521. AF4 AGtZeroF4(AF4 m){return ASatF4(m*AF4_(A_INFP_F));}
  1522. //==============================================================================================================================
  1523. #ifdef A_HALF
  1524. #ifdef A_HLSL_6_2
  1525. #define A_INFP_H AH1_AW1((uint16_t)0x7c00u)
  1526. #define A_INFN_H AH1_AW1((uint16_t)0xfc00u)
  1527. #else
  1528. #define A_INFP_H AH1_AW1(0x7c00u)
  1529. #define A_INFN_H AH1_AW1(0xfc00u)
  1530. #endif
  1531. //------------------------------------------------------------------------------------------------------------------------------
  1532. AH1 ACpySgnH1(AH1 d,AH1 s){return AH1_AW1(AW1_AH1(d)|(AW1_AH1(s)&AW1_(0x8000u)));}
  1533. AH2 ACpySgnH2(AH2 d,AH2 s){return AH2_AW2(AW2_AH2(d)|(AW2_AH2(s)&AW2_(0x8000u)));}
  1534. AH3 ACpySgnH3(AH3 d,AH3 s){return AH3_AW3(AW3_AH3(d)|(AW3_AH3(s)&AW3_(0x8000u)));}
  1535. AH4 ACpySgnH4(AH4 d,AH4 s){return AH4_AW4(AW4_AH4(d)|(AW4_AH4(s)&AW4_(0x8000u)));}
  1536. //------------------------------------------------------------------------------------------------------------------------------
  1537. AH1 ASignedH1(AH1 m){return ASatH1(m*AH1_(A_INFN_H));}
  1538. AH2 ASignedH2(AH2 m){return ASatH2(m*AH2_(A_INFN_H));}
  1539. AH3 ASignedH3(AH3 m){return ASatH3(m*AH3_(A_INFN_H));}
  1540. AH4 ASignedH4(AH4 m){return ASatH4(m*AH4_(A_INFN_H));}
  1541. //------------------------------------------------------------------------------------------------------------------------------
  1542. AH1 AGtZeroH1(AH1 m){return ASatH1(m*AH1_(A_INFP_H));}
  1543. AH2 AGtZeroH2(AH2 m){return ASatH2(m*AH2_(A_INFP_H));}
  1544. AH3 AGtZeroH3(AH3 m){return ASatH3(m*AH3_(A_INFP_H));}
  1545. AH4 AGtZeroH4(AH4 m){return ASatH4(m*AH4_(A_INFP_H));}
  1546. #endif
  1547. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1548. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1549. //_____________________________________________________________/\_______________________________________________________________
  1550. //==============================================================================================================================
  1551. // [FIS] FLOAT INTEGER SORTABLE
  1552. //------------------------------------------------------------------------------------------------------------------------------
  1553. // Float to integer sortable.
  1554. // - If sign bit=0, flip the sign bit (positives).
  1555. // - If sign bit=1, flip all bits (negatives).
  1556. // Integer sortable to float.
  1557. // - If sign bit=1, flip the sign bit (positives).
  1558. // - If sign bit=0, flip all bits (negatives).
  1559. // Has nice side effects.
  1560. // - Larger integers are more positive values.
  1561. // - Float zero is mapped to center of integers (so clear to integer zero is a nice default for atomic max usage).
  1562. // Burns 3 ops for conversion {shift,or,xor}.
  1563. //==============================================================================================================================
  1564. AU1 AFisToU1(AU1 x){return x^(( AShrSU1(x,AU1_(31)))|AU1_(0x80000000));}
  1565. AU1 AFisFromU1(AU1 x){return x^((~AShrSU1(x,AU1_(31)))|AU1_(0x80000000));}
  1566. //------------------------------------------------------------------------------------------------------------------------------
  1567. // Just adjust high 16-bit value (useful when upper part of 32-bit word is a 16-bit float value).
  1568. AU1 AFisToHiU1(AU1 x){return x^(( AShrSU1(x,AU1_(15)))|AU1_(0x80000000));}
  1569. AU1 AFisFromHiU1(AU1 x){return x^((~AShrSU1(x,AU1_(15)))|AU1_(0x80000000));}
  1570. //------------------------------------------------------------------------------------------------------------------------------
  1571. #ifdef A_HALF
  1572. AW1 AFisToW1(AW1 x){return x^(( AShrSW1(x,AW1_(15)))|AW1_(0x8000));}
  1573. AW1 AFisFromW1(AW1 x){return x^((~AShrSW1(x,AW1_(15)))|AW1_(0x8000));}
  1574. //------------------------------------------------------------------------------------------------------------------------------
  1575. AW2 AFisToW2(AW2 x){return x^(( AShrSW2(x,AW2_(15)))|AW2_(0x8000));}
  1576. AW2 AFisFromW2(AW2 x){return x^((~AShrSW2(x,AW2_(15)))|AW2_(0x8000));}
  1577. #endif
  1578. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1579. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1580. //_____________________________________________________________/\_______________________________________________________________
  1581. //==============================================================================================================================
  1582. // [PERM] V_PERM_B32
  1583. //------------------------------------------------------------------------------------------------------------------------------
  1584. // Support for V_PERM_B32 started in the 3rd generation of GCN.
  1585. //------------------------------------------------------------------------------------------------------------------------------
  1586. // yyyyxxxx - The 'i' input.
  1587. // 76543210
  1588. // ========
  1589. // HGFEDCBA - Naming on permutation.
  1590. //------------------------------------------------------------------------------------------------------------------------------
  1591. // TODO
  1592. // ====
  1593. // - Make sure compiler optimizes this.
  1594. //==============================================================================================================================
  1595. #ifdef A_HALF
  1596. AU1 APerm0E0A(AU2 i){return((i.x )&0xffu)|((i.y<<16)&0xff0000u);}
  1597. AU1 APerm0F0B(AU2 i){return((i.x>> 8)&0xffu)|((i.y<< 8)&0xff0000u);}
  1598. AU1 APerm0G0C(AU2 i){return((i.x>>16)&0xffu)|((i.y )&0xff0000u);}
  1599. AU1 APerm0H0D(AU2 i){return((i.x>>24)&0xffu)|((i.y>> 8)&0xff0000u);}
  1600. //------------------------------------------------------------------------------------------------------------------------------
  1601. AU1 APermHGFA(AU2 i){return((i.x )&0x000000ffu)|(i.y&0xffffff00u);}
  1602. AU1 APermHGFC(AU2 i){return((i.x>>16)&0x000000ffu)|(i.y&0xffffff00u);}
  1603. AU1 APermHGAE(AU2 i){return((i.x<< 8)&0x0000ff00u)|(i.y&0xffff00ffu);}
  1604. AU1 APermHGCE(AU2 i){return((i.x>> 8)&0x0000ff00u)|(i.y&0xffff00ffu);}
  1605. AU1 APermHAFE(AU2 i){return((i.x<<16)&0x00ff0000u)|(i.y&0xff00ffffu);}
  1606. AU1 APermHCFE(AU2 i){return((i.x )&0x00ff0000u)|(i.y&0xff00ffffu);}
  1607. AU1 APermAGFE(AU2 i){return((i.x<<24)&0xff000000u)|(i.y&0x00ffffffu);}
  1608. AU1 APermCGFE(AU2 i){return((i.x<< 8)&0xff000000u)|(i.y&0x00ffffffu);}
  1609. //------------------------------------------------------------------------------------------------------------------------------
  1610. AU1 APermGCEA(AU2 i){return((i.x)&0x00ff00ffu)|((i.y<<8)&0xff00ff00u);}
  1611. AU1 APermGECA(AU2 i){return(((i.x)&0xffu)|((i.x>>8)&0xff00u)|((i.y<<16)&0xff0000u)|((i.y<<8)&0xff000000u));}
  1612. #endif
  1613. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1614. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1615. //_____________________________________________________________/\_______________________________________________________________
  1616. //==============================================================================================================================
  1617. // [BUC] BYTE UNSIGNED CONVERSION
  1618. //------------------------------------------------------------------------------------------------------------------------------
  1619. // Designed to use the optimal conversion, enables the scaling to possibly be factored into other computation.
  1620. // Works on a range of {0 to A_BUC_<32,16>}, for <32-bit, and 16-bit> respectively.
  1621. //------------------------------------------------------------------------------------------------------------------------------
  1622. // OPCODE NOTES
  1623. // ============
  1624. // GCN does not do UNORM or SNORM for bytes in opcodes.
  1625. // - V_CVT_F32_UBYTE{0,1,2,3} - Unsigned byte to float.
  1626. // - V_CVT_PKACC_U8_F32 - Float to unsigned byte (does bit-field insert into 32-bit integer).
  1627. // V_PERM_B32 does byte packing with ability to zero fill bytes as well.
  1628. // - Can pull out byte values from two sources, and zero fill upper 8-bits of packed hi and lo.
  1629. //------------------------------------------------------------------------------------------------------------------------------
  1630. // BYTE : FLOAT - ABuc{0,1,2,3}{To,From}U1() - Designed for V_CVT_F32_UBYTE* and V_CVT_PKACCUM_U8_F32 ops.
  1631. // ==== =====
  1632. // 0 : 0
  1633. // 1 : 1
  1634. // ...
  1635. // 255 : 255
  1636. // : 256 (just outside the encoding range)
  1637. //------------------------------------------------------------------------------------------------------------------------------
  1638. // BYTE : FLOAT - ABuc{0,1,2,3}{To,From}U2() - Designed for 16-bit denormal tricks and V_PERM_B32.
  1639. // ==== =====
  1640. // 0 : 0
  1641. // 1 : 1/512
  1642. // 2 : 1/256
  1643. // ...
  1644. // 64 : 1/8
  1645. // 128 : 1/4
  1646. // 255 : 255/512
  1647. // : 1/2 (just outside the encoding range)
  1648. //------------------------------------------------------------------------------------------------------------------------------
  1649. // OPTIMAL IMPLEMENTATIONS ON AMD ARCHITECTURES
  1650. // ============================================
  1651. // r=ABuc0FromU1(i)
  1652. // V_CVT_F32_UBYTE0 r,i
  1653. // --------------------------------------------
  1654. // r=ABuc0ToU1(d,i)
  1655. // V_CVT_PKACCUM_U8_F32 r,i,0,d
  1656. // --------------------------------------------
  1657. // d=ABuc0FromU2(i)
  1658. // Where 'k0' is an SGPR with 0x0E0A
  1659. // Where 'k1' is an SGPR with {32768.0} packed into the lower 16-bits
  1660. // V_PERM_B32 d,i.x,i.y,k0
  1661. // V_PK_FMA_F16 d,d,k1.x,0
  1662. // --------------------------------------------
  1663. // r=ABuc0ToU2(d,i)
  1664. // Where 'k0' is an SGPR with {1.0/32768.0} packed into the lower 16-bits
  1665. // Where 'k1' is an SGPR with 0x????
  1666. // Where 'k2' is an SGPR with 0x????
  1667. // V_PK_FMA_F16 i,i,k0.x,0
  1668. // V_PERM_B32 r.x,i,i,k1
  1669. // V_PERM_B32 r.y,i,i,k2
  1670. //==============================================================================================================================
  1671. // Peak range for 32-bit and 16-bit operations.
  1672. #define A_BUC_32 (255.0)
  1673. #define A_BUC_16 (255.0/512.0)
  1674. //==============================================================================================================================
  1675. #if 1
  1676. // Designed to be one V_CVT_PKACCUM_U8_F32.
  1677. // The extra min is required to pattern match to V_CVT_PKACCUM_U8_F32.
  1678. AU1 ABuc0ToU1(AU1 d,AF1 i){return (d&0xffffff00u)|((min(AU1(i),255u) )&(0x000000ffu));}
  1679. AU1 ABuc1ToU1(AU1 d,AF1 i){return (d&0xffff00ffu)|((min(AU1(i),255u)<< 8)&(0x0000ff00u));}
  1680. AU1 ABuc2ToU1(AU1 d,AF1 i){return (d&0xff00ffffu)|((min(AU1(i),255u)<<16)&(0x00ff0000u));}
  1681. AU1 ABuc3ToU1(AU1 d,AF1 i){return (d&0x00ffffffu)|((min(AU1(i),255u)<<24)&(0xff000000u));}
  1682. //------------------------------------------------------------------------------------------------------------------------------
  1683. // Designed to be one V_CVT_F32_UBYTE*.
  1684. AF1 ABuc0FromU1(AU1 i){return AF1((i )&255u);}
  1685. AF1 ABuc1FromU1(AU1 i){return AF1((i>> 8)&255u);}
  1686. AF1 ABuc2FromU1(AU1 i){return AF1((i>>16)&255u);}
  1687. AF1 ABuc3FromU1(AU1 i){return AF1((i>>24)&255u);}
  1688. #endif
  1689. //==============================================================================================================================
  1690. #ifdef A_HALF
  1691. // Takes {x0,x1} and {y0,y1} and builds {{x0,y0},{x1,y1}}.
  1692. AW2 ABuc01ToW2(AH2 x,AH2 y){x*=AH2_(1.0/32768.0);y*=AH2_(1.0/32768.0);
  1693. return AW2_AU1(APermGCEA(AU2(AU1_AW2(AW2_AH2(x)),AU1_AW2(AW2_AH2(y)))));}
  1694. //------------------------------------------------------------------------------------------------------------------------------
  1695. // Designed for 3 ops to do SOA to AOS and conversion.
  1696. AU2 ABuc0ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)));
  1697. return AU2(APermHGFA(AU2(d.x,b)),APermHGFC(AU2(d.y,b)));}
  1698. AU2 ABuc1ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)));
  1699. return AU2(APermHGAE(AU2(d.x,b)),APermHGCE(AU2(d.y,b)));}
  1700. AU2 ABuc2ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)));
  1701. return AU2(APermHAFE(AU2(d.x,b)),APermHCFE(AU2(d.y,b)));}
  1702. AU2 ABuc3ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)));
  1703. return AU2(APermAGFE(AU2(d.x,b)),APermCGFE(AU2(d.y,b)));}
  1704. //------------------------------------------------------------------------------------------------------------------------------
  1705. // Designed for 2 ops to do both AOS to SOA, and conversion.
  1706. AH2 ABuc0FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0E0A(i)))*AH2_(32768.0);}
  1707. AH2 ABuc1FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0F0B(i)))*AH2_(32768.0);}
  1708. AH2 ABuc2FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0G0C(i)))*AH2_(32768.0);}
  1709. AH2 ABuc3FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0H0D(i)))*AH2_(32768.0);}
  1710. #endif
  1711. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1712. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1713. //_____________________________________________________________/\_______________________________________________________________
  1714. //==============================================================================================================================
  1715. // [BSC] BYTE SIGNED CONVERSION
  1716. //------------------------------------------------------------------------------------------------------------------------------
  1717. // Similar to [BUC].
  1718. // Works on a range of {-/+ A_BSC_<32,16>}, for <32-bit, and 16-bit> respectively.
  1719. //------------------------------------------------------------------------------------------------------------------------------
  1720. // ENCODING (without zero-based encoding)
  1721. // ========
  1722. // 0 = unused (can be used to mean something else)
  1723. // 1 = lowest value
  1724. // 128 = exact zero center (zero based encoding
  1725. // 255 = highest value
  1726. //------------------------------------------------------------------------------------------------------------------------------
  1727. // Zero-based [Zb] flips the MSB bit of the byte (making 128 "exact zero" actually zero).
  1728. // This is useful if there is a desire for cleared values to decode as zero.
  1729. //------------------------------------------------------------------------------------------------------------------------------
  1730. // BYTE : FLOAT - ABsc{0,1,2,3}{To,From}U2() - Designed for 16-bit denormal tricks and V_PERM_B32.
  1731. // ==== =====
  1732. // 0 : -127/512 (unused)
  1733. // 1 : -126/512
  1734. // 2 : -125/512
  1735. // ...
  1736. // 128 : 0
  1737. // ...
  1738. // 255 : 127/512
  1739. // : 1/4 (just outside the encoding range)
  1740. //==============================================================================================================================
  1741. // Peak range for 32-bit and 16-bit operations.
  1742. #define A_BSC_32 (127.0)
  1743. #define A_BSC_16 (127.0/512.0)
  1744. //==============================================================================================================================
  1745. #if 1
  1746. AU1 ABsc0ToU1(AU1 d,AF1 i){return (d&0xffffff00u)|((min(AU1(i+128.0),255u) )&(0x000000ffu));}
  1747. AU1 ABsc1ToU1(AU1 d,AF1 i){return (d&0xffff00ffu)|((min(AU1(i+128.0),255u)<< 8)&(0x0000ff00u));}
  1748. AU1 ABsc2ToU1(AU1 d,AF1 i){return (d&0xff00ffffu)|((min(AU1(i+128.0),255u)<<16)&(0x00ff0000u));}
  1749. AU1 ABsc3ToU1(AU1 d,AF1 i){return (d&0x00ffffffu)|((min(AU1(i+128.0),255u)<<24)&(0xff000000u));}
  1750. //------------------------------------------------------------------------------------------------------------------------------
  1751. AU1 ABsc0ToZbU1(AU1 d,AF1 i){return ((d&0xffffff00u)|((min(AU1(trunc(i)+128.0),255u) )&(0x000000ffu)))^0x00000080u;}
  1752. AU1 ABsc1ToZbU1(AU1 d,AF1 i){return ((d&0xffff00ffu)|((min(AU1(trunc(i)+128.0),255u)<< 8)&(0x0000ff00u)))^0x00008000u;}
  1753. AU1 ABsc2ToZbU1(AU1 d,AF1 i){return ((d&0xff00ffffu)|((min(AU1(trunc(i)+128.0),255u)<<16)&(0x00ff0000u)))^0x00800000u;}
  1754. AU1 ABsc3ToZbU1(AU1 d,AF1 i){return ((d&0x00ffffffu)|((min(AU1(trunc(i)+128.0),255u)<<24)&(0xff000000u)))^0x80000000u;}
  1755. //------------------------------------------------------------------------------------------------------------------------------
  1756. AF1 ABsc0FromU1(AU1 i){return AF1((i )&255u)-128.0;}
  1757. AF1 ABsc1FromU1(AU1 i){return AF1((i>> 8)&255u)-128.0;}
  1758. AF1 ABsc2FromU1(AU1 i){return AF1((i>>16)&255u)-128.0;}
  1759. AF1 ABsc3FromU1(AU1 i){return AF1((i>>24)&255u)-128.0;}
  1760. //------------------------------------------------------------------------------------------------------------------------------
  1761. AF1 ABsc0FromZbU1(AU1 i){return AF1(((i )&255u)^0x80u)-128.0;}
  1762. AF1 ABsc1FromZbU1(AU1 i){return AF1(((i>> 8)&255u)^0x80u)-128.0;}
  1763. AF1 ABsc2FromZbU1(AU1 i){return AF1(((i>>16)&255u)^0x80u)-128.0;}
  1764. AF1 ABsc3FromZbU1(AU1 i){return AF1(((i>>24)&255u)^0x80u)-128.0;}
  1765. #endif
  1766. //==============================================================================================================================
  1767. #ifdef A_HALF
  1768. // Takes {x0,x1} and {y0,y1} and builds {{x0,y0},{x1,y1}}.
  1769. AW2 ABsc01ToW2(AH2 x,AH2 y){x=x*AH2_(1.0/32768.0)+AH2_(0.25/32768.0);y=y*AH2_(1.0/32768.0)+AH2_(0.25/32768.0);
  1770. return AW2_AU1(APermGCEA(AU2(AU1_AW2(AW2_AH2(x)),AU1_AW2(AW2_AH2(y)))));}
  1771. //------------------------------------------------------------------------------------------------------------------------------
  1772. AU2 ABsc0ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)));
  1773. return AU2(APermHGFA(AU2(d.x,b)),APermHGFC(AU2(d.y,b)));}
  1774. AU2 ABsc1ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)));
  1775. return AU2(APermHGAE(AU2(d.x,b)),APermHGCE(AU2(d.y,b)));}
  1776. AU2 ABsc2ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)));
  1777. return AU2(APermHAFE(AU2(d.x,b)),APermHCFE(AU2(d.y,b)));}
  1778. AU2 ABsc3ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)));
  1779. return AU2(APermAGFE(AU2(d.x,b)),APermCGFE(AU2(d.y,b)));}
  1780. //------------------------------------------------------------------------------------------------------------------------------
  1781. AU2 ABsc0ToZbU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)))^0x00800080u;
  1782. return AU2(APermHGFA(AU2(d.x,b)),APermHGFC(AU2(d.y,b)));}
  1783. AU2 ABsc1ToZbU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)))^0x00800080u;
  1784. return AU2(APermHGAE(AU2(d.x,b)),APermHGCE(AU2(d.y,b)));}
  1785. AU2 ABsc2ToZbU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)))^0x00800080u;
  1786. return AU2(APermHAFE(AU2(d.x,b)),APermHCFE(AU2(d.y,b)));}
  1787. AU2 ABsc3ToZbU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)))^0x00800080u;
  1788. return AU2(APermAGFE(AU2(d.x,b)),APermCGFE(AU2(d.y,b)));}
  1789. //------------------------------------------------------------------------------------------------------------------------------
  1790. AH2 ABsc0FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0E0A(i)))*AH2_(32768.0)-AH2_(0.25);}
  1791. AH2 ABsc1FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0F0B(i)))*AH2_(32768.0)-AH2_(0.25);}
  1792. AH2 ABsc2FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0G0C(i)))*AH2_(32768.0)-AH2_(0.25);}
  1793. AH2 ABsc3FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0H0D(i)))*AH2_(32768.0)-AH2_(0.25);}
  1794. //------------------------------------------------------------------------------------------------------------------------------
  1795. AH2 ABsc0FromZbU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0E0A(i)^0x00800080u))*AH2_(32768.0)-AH2_(0.25);}
  1796. AH2 ABsc1FromZbU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0F0B(i)^0x00800080u))*AH2_(32768.0)-AH2_(0.25);}
  1797. AH2 ABsc2FromZbU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0G0C(i)^0x00800080u))*AH2_(32768.0)-AH2_(0.25);}
  1798. AH2 ABsc3FromZbU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0H0D(i)^0x00800080u))*AH2_(32768.0)-AH2_(0.25);}
  1799. #endif
  1800. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1801. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1802. //_____________________________________________________________/\_______________________________________________________________
  1803. //==============================================================================================================================
  1804. // HALF APPROXIMATIONS
  1805. //------------------------------------------------------------------------------------------------------------------------------
  1806. // These support only positive inputs.
  1807. // Did not see value yet in specialization for range.
  1808. // Using quick testing, ended up mostly getting the same "best" approximation for various ranges.
  1809. // With hardware that can co-execute transcendentals, the value in approximations could be less than expected.
  1810. // However from a latency perspective, if execution of a transcendental is 4 clk, with no packed support, -> 8 clk total.
  1811. // And co-execution would require a compiler interleaving a lot of independent work for packed usage.
  1812. //------------------------------------------------------------------------------------------------------------------------------
  1813. // The one Newton Raphson iteration form of rsq() was skipped (requires 6 ops total).
  1814. // Same with sqrt(), as this could be x*rsq() (7 ops).
  1815. //==============================================================================================================================
  1816. #ifdef A_HALF
  1817. // Minimize squared error across full positive range, 2 ops.
  1818. // The 0x1de2 based approximation maps {0 to 1} input maps to < 1 output.
  1819. AH1 APrxLoSqrtH1(AH1 a){return AH1_AW1((AW1_AH1(a)>>AW1_(1))+AW1_(0x1de2));}
  1820. AH2 APrxLoSqrtH2(AH2 a){return AH2_AW2((AW2_AH2(a)>>AW2_(1))+AW2_(0x1de2));}
  1821. AH3 APrxLoSqrtH3(AH3 a){return AH3_AW3((AW3_AH3(a)>>AW3_(1))+AW3_(0x1de2));}
  1822. AH4 APrxLoSqrtH4(AH4 a){return AH4_AW4((AW4_AH4(a)>>AW4_(1))+AW4_(0x1de2));}
  1823. //------------------------------------------------------------------------------------------------------------------------------
  1824. // Lower precision estimation, 1 op.
  1825. // Minimize squared error across {smallest normal to 16384.0}.
  1826. AH1 APrxLoRcpH1(AH1 a){return AH1_AW1(AW1_(0x7784)-AW1_AH1(a));}
  1827. AH2 APrxLoRcpH2(AH2 a){return AH2_AW2(AW2_(0x7784)-AW2_AH2(a));}
  1828. AH3 APrxLoRcpH3(AH3 a){return AH3_AW3(AW3_(0x7784)-AW3_AH3(a));}
  1829. AH4 APrxLoRcpH4(AH4 a){return AH4_AW4(AW4_(0x7784)-AW4_AH4(a));}
  1830. //------------------------------------------------------------------------------------------------------------------------------
  1831. // Medium precision estimation, one Newton Raphson iteration, 3 ops.
  1832. AH1 APrxMedRcpH1(AH1 a){AH1 b=AH1_AW1(AW1_(0x778d)-AW1_AH1(a));return b*(-b*a+AH1_(2.0));}
  1833. AH2 APrxMedRcpH2(AH2 a){AH2 b=AH2_AW2(AW2_(0x778d)-AW2_AH2(a));return b*(-b*a+AH2_(2.0));}
  1834. AH3 APrxMedRcpH3(AH3 a){AH3 b=AH3_AW3(AW3_(0x778d)-AW3_AH3(a));return b*(-b*a+AH3_(2.0));}
  1835. AH4 APrxMedRcpH4(AH4 a){AH4 b=AH4_AW4(AW4_(0x778d)-AW4_AH4(a));return b*(-b*a+AH4_(2.0));}
  1836. //------------------------------------------------------------------------------------------------------------------------------
  1837. // Minimize squared error across {smallest normal to 16384.0}, 2 ops.
  1838. AH1 APrxLoRsqH1(AH1 a){return AH1_AW1(AW1_(0x59a3)-(AW1_AH1(a)>>AW1_(1)));}
  1839. AH2 APrxLoRsqH2(AH2 a){return AH2_AW2(AW2_(0x59a3)-(AW2_AH2(a)>>AW2_(1)));}
  1840. AH3 APrxLoRsqH3(AH3 a){return AH3_AW3(AW3_(0x59a3)-(AW3_AH3(a)>>AW3_(1)));}
  1841. AH4 APrxLoRsqH4(AH4 a){return AH4_AW4(AW4_(0x59a3)-(AW4_AH4(a)>>AW4_(1)));}
  1842. #endif
  1843. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1844. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1845. //_____________________________________________________________/\_______________________________________________________________
  1846. //==============================================================================================================================
  1847. // FLOAT APPROXIMATIONS
  1848. //------------------------------------------------------------------------------------------------------------------------------
  1849. // Michal Drobot has an excellent presentation on these: "Low Level Optimizations For GCN",
  1850. // - Idea dates back to SGI, then to Quake 3, etc.
  1851. // - https://michaldrobot.files.wordpress.com/2014/05/gcn_alu_opt_digitaldragons2014.pdf
  1852. // - sqrt(x)=rsqrt(x)*x
  1853. // - rcp(x)=rsqrt(x)*rsqrt(x) for positive x
  1854. // - https://github.com/michaldrobot/ShaderFastLibs/blob/master/ShaderFastMathLib.h
  1855. //------------------------------------------------------------------------------------------------------------------------------
  1856. // These below are from perhaps less complete searching for optimal.
  1857. // Used FP16 normal range for testing with +4096 32-bit step size for sampling error.
  1858. // So these match up well with the half approximations.
  1859. //==============================================================================================================================
  1860. AF1 APrxLoSqrtF1(AF1 a){return AF1_AU1((AU1_AF1(a)>>AU1_(1))+AU1_(0x1fbc4639));}
  1861. AF1 APrxLoRcpF1(AF1 a){return AF1_AU1(AU1_(0x7ef07ebb)-AU1_AF1(a));}
  1862. AF1 APrxMedRcpF1(AF1 a){AF1 b=AF1_AU1(AU1_(0x7ef19fff)-AU1_AF1(a));return b*(-b*a+AF1_(2.0));}
  1863. AF1 APrxLoRsqF1(AF1 a){return AF1_AU1(AU1_(0x5f347d74)-(AU1_AF1(a)>>AU1_(1)));}
  1864. //------------------------------------------------------------------------------------------------------------------------------
  1865. AF2 APrxLoSqrtF2(AF2 a){return AF2_AU2((AU2_AF2(a)>>AU2_(1))+AU2_(0x1fbc4639));}
  1866. AF2 APrxLoRcpF2(AF2 a){return AF2_AU2(AU2_(0x7ef07ebb)-AU2_AF2(a));}
  1867. AF2 APrxMedRcpF2(AF2 a){AF2 b=AF2_AU2(AU2_(0x7ef19fff)-AU2_AF2(a));return b*(-b*a+AF2_(2.0));}
  1868. AF2 APrxLoRsqF2(AF2 a){return AF2_AU2(AU2_(0x5f347d74)-(AU2_AF2(a)>>AU2_(1)));}
  1869. //------------------------------------------------------------------------------------------------------------------------------
  1870. AF3 APrxLoSqrtF3(AF3 a){return AF3_AU3((AU3_AF3(a)>>AU3_(1))+AU3_(0x1fbc4639));}
  1871. AF3 APrxLoRcpF3(AF3 a){return AF3_AU3(AU3_(0x7ef07ebb)-AU3_AF3(a));}
  1872. AF3 APrxMedRcpF3(AF3 a){AF3 b=AF3_AU3(AU3_(0x7ef19fff)-AU3_AF3(a));return b*(-b*a+AF3_(2.0));}
  1873. AF3 APrxLoRsqF3(AF3 a){return AF3_AU3(AU3_(0x5f347d74)-(AU3_AF3(a)>>AU3_(1)));}
  1874. //------------------------------------------------------------------------------------------------------------------------------
  1875. AF4 APrxLoSqrtF4(AF4 a){return AF4_AU4((AU4_AF4(a)>>AU4_(1))+AU4_(0x1fbc4639));}
  1876. AF4 APrxLoRcpF4(AF4 a){return AF4_AU4(AU4_(0x7ef07ebb)-AU4_AF4(a));}
  1877. AF4 APrxMedRcpF4(AF4 a){AF4 b=AF4_AU4(AU4_(0x7ef19fff)-AU4_AF4(a));return b*(-b*a+AF4_(2.0));}
  1878. AF4 APrxLoRsqF4(AF4 a){return AF4_AU4(AU4_(0x5f347d74)-(AU4_AF4(a)>>AU4_(1)));}
  1879. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1880. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1881. //_____________________________________________________________/\_______________________________________________________________
  1882. //==============================================================================================================================
  1883. // PQ APPROXIMATIONS
  1884. //------------------------------------------------------------------------------------------------------------------------------
  1885. // PQ is very close to x^(1/8). The functions below Use the fast float approximation method to do
  1886. // PQ<~>Gamma2 (4th power and fast 4th root) and PQ<~>Linear (8th power and fast 8th root). Maximum error is ~0.2%.
  1887. //==============================================================================================================================
  1888. // Helpers
  1889. AF1 Quart(AF1 a) { a = a * a; return a * a;}
  1890. AF1 Oct(AF1 a) { a = a * a; a = a * a; return a * a; }
  1891. AF2 Quart(AF2 a) { a = a * a; return a * a; }
  1892. AF2 Oct(AF2 a) { a = a * a; a = a * a; return a * a; }
  1893. AF3 Quart(AF3 a) { a = a * a; return a * a; }
  1894. AF3 Oct(AF3 a) { a = a * a; a = a * a; return a * a; }
  1895. AF4 Quart(AF4 a) { a = a * a; return a * a; }
  1896. AF4 Oct(AF4 a) { a = a * a; a = a * a; return a * a; }
  1897. //------------------------------------------------------------------------------------------------------------------------------
  1898. AF1 APrxPQToGamma2(AF1 a) { return Quart(a); }
  1899. AF1 APrxPQToLinear(AF1 a) { return Oct(a); }
  1900. AF1 APrxLoGamma2ToPQ(AF1 a) { return AF1_AU1((AU1_AF1(a) >> AU1_(2)) + AU1_(0x2F9A4E46)); }
  1901. AF1 APrxMedGamma2ToPQ(AF1 a) { AF1 b = AF1_AU1((AU1_AF1(a) >> AU1_(2)) + AU1_(0x2F9A4E46)); AF1 b4 = Quart(b); return b - b * (b4 - a) / (AF1_(4.0) * b4); }
  1902. AF1 APrxHighGamma2ToPQ(AF1 a) { return sqrt(sqrt(a)); }
  1903. AF1 APrxLoLinearToPQ(AF1 a) { return AF1_AU1((AU1_AF1(a) >> AU1_(3)) + AU1_(0x378D8723)); }
  1904. AF1 APrxMedLinearToPQ(AF1 a) { AF1 b = AF1_AU1((AU1_AF1(a) >> AU1_(3)) + AU1_(0x378D8723)); AF1 b8 = Oct(b); return b - b * (b8 - a) / (AF1_(8.0) * b8); }
  1905. AF1 APrxHighLinearToPQ(AF1 a) { return sqrt(sqrt(sqrt(a))); }
  1906. //------------------------------------------------------------------------------------------------------------------------------
  1907. AF2 APrxPQToGamma2(AF2 a) { return Quart(a); }
  1908. AF2 APrxPQToLinear(AF2 a) { return Oct(a); }
  1909. AF2 APrxLoGamma2ToPQ(AF2 a) { return AF2_AU2((AU2_AF2(a) >> AU2_(2)) + AU2_(0x2F9A4E46)); }
  1910. AF2 APrxMedGamma2ToPQ(AF2 a) { AF2 b = AF2_AU2((AU2_AF2(a) >> AU2_(2)) + AU2_(0x2F9A4E46)); AF2 b4 = Quart(b); return b - b * (b4 - a) / (AF1_(4.0) * b4); }
  1911. AF2 APrxHighGamma2ToPQ(AF2 a) { return sqrt(sqrt(a)); }
  1912. AF2 APrxLoLinearToPQ(AF2 a) { return AF2_AU2((AU2_AF2(a) >> AU2_(3)) + AU2_(0x378D8723)); }
  1913. AF2 APrxMedLinearToPQ(AF2 a) { AF2 b = AF2_AU2((AU2_AF2(a) >> AU2_(3)) + AU2_(0x378D8723)); AF2 b8 = Oct(b); return b - b * (b8 - a) / (AF1_(8.0) * b8); }
  1914. AF2 APrxHighLinearToPQ(AF2 a) { return sqrt(sqrt(sqrt(a))); }
  1915. //------------------------------------------------------------------------------------------------------------------------------
  1916. AF3 APrxPQToGamma2(AF3 a) { return Quart(a); }
  1917. AF3 APrxPQToLinear(AF3 a) { return Oct(a); }
  1918. AF3 APrxLoGamma2ToPQ(AF3 a) { return AF3_AU3((AU3_AF3(a) >> AU3_(2)) + AU3_(0x2F9A4E46)); }
  1919. AF3 APrxMedGamma2ToPQ(AF3 a) { AF3 b = AF3_AU3((AU3_AF3(a) >> AU3_(2)) + AU3_(0x2F9A4E46)); AF3 b4 = Quart(b); return b - b * (b4 - a) / (AF1_(4.0) * b4); }
  1920. AF3 APrxHighGamma2ToPQ(AF3 a) { return sqrt(sqrt(a)); }
  1921. AF3 APrxLoLinearToPQ(AF3 a) { return AF3_AU3((AU3_AF3(a) >> AU3_(3)) + AU3_(0x378D8723)); }
  1922. AF3 APrxMedLinearToPQ(AF3 a) { AF3 b = AF3_AU3((AU3_AF3(a) >> AU3_(3)) + AU3_(0x378D8723)); AF3 b8 = Oct(b); return b - b * (b8 - a) / (AF1_(8.0) * b8); }
  1923. AF3 APrxHighLinearToPQ(AF3 a) { return sqrt(sqrt(sqrt(a))); }
  1924. //------------------------------------------------------------------------------------------------------------------------------
  1925. AF4 APrxPQToGamma2(AF4 a) { return Quart(a); }
  1926. AF4 APrxPQToLinear(AF4 a) { return Oct(a); }
  1927. AF4 APrxLoGamma2ToPQ(AF4 a) { return AF4_AU4((AU4_AF4(a) >> AU4_(2)) + AU4_(0x2F9A4E46)); }
  1928. AF4 APrxMedGamma2ToPQ(AF4 a) { AF4 b = AF4_AU4((AU4_AF4(a) >> AU4_(2)) + AU4_(0x2F9A4E46)); AF4 b4 = Quart(b); return b - b * (b4 - a) / (AF1_(4.0) * b4); }
  1929. AF4 APrxHighGamma2ToPQ(AF4 a) { return sqrt(sqrt(a)); }
  1930. AF4 APrxLoLinearToPQ(AF4 a) { return AF4_AU4((AU4_AF4(a) >> AU4_(3)) + AU4_(0x378D8723)); }
  1931. AF4 APrxMedLinearToPQ(AF4 a) { AF4 b = AF4_AU4((AU4_AF4(a) >> AU4_(3)) + AU4_(0x378D8723)); AF4 b8 = Oct(b); return b - b * (b8 - a) / (AF1_(8.0) * b8); }
  1932. AF4 APrxHighLinearToPQ(AF4 a) { return sqrt(sqrt(sqrt(a))); }
  1933. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1934. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1935. //_____________________________________________________________/\_______________________________________________________________
  1936. //==============================================================================================================================
  1937. // PARABOLIC SIN & COS
  1938. //------------------------------------------------------------------------------------------------------------------------------
  1939. // Approximate answers to transcendental questions.
  1940. //------------------------------------------------------------------------------------------------------------------------------
  1941. //==============================================================================================================================
  1942. #if 1
  1943. // Valid input range is {-1 to 1} representing {0 to 2 pi}.
  1944. // Output range is {-1/4 to 1/4} representing {-1 to 1}.
  1945. AF1 APSinF1(AF1 x){return x*abs(x)-x;} // MAD.
  1946. AF2 APSinF2(AF2 x){return x*abs(x)-x;}
  1947. AF1 APCosF1(AF1 x){x=AFractF1(x*AF1_(0.5)+AF1_(0.75));x=x*AF1_(2.0)-AF1_(1.0);return APSinF1(x);} // 3x MAD, FRACT
  1948. AF2 APCosF2(AF2 x){x=AFractF2(x*AF2_(0.5)+AF2_(0.75));x=x*AF2_(2.0)-AF2_(1.0);return APSinF2(x);}
  1949. AF2 APSinCosF1(AF1 x){AF1 y=AFractF1(x*AF1_(0.5)+AF1_(0.75));y=y*AF1_(2.0)-AF1_(1.0);return APSinF2(AF2(x,y));}
  1950. #endif
  1951. //------------------------------------------------------------------------------------------------------------------------------
  1952. #ifdef A_HALF
  1953. // For a packed {sin,cos} pair,
  1954. // - Native takes 16 clocks and 4 issue slots (no packed transcendentals).
  1955. // - Parabolic takes 8 clocks and 8 issue slots (only fract is non-packed).
  1956. AH1 APSinH1(AH1 x){return x*abs(x)-x;}
  1957. AH2 APSinH2(AH2 x){return x*abs(x)-x;} // AND,FMA
  1958. AH1 APCosH1(AH1 x){x=AFractH1(x*AH1_(0.5)+AH1_(0.75));x=x*AH1_(2.0)-AH1_(1.0);return APSinH1(x);}
  1959. AH2 APCosH2(AH2 x){x=AFractH2(x*AH2_(0.5)+AH2_(0.75));x=x*AH2_(2.0)-AH2_(1.0);return APSinH2(x);} // 3x FMA, 2xFRACT, AND
  1960. AH2 APSinCosH1(AH1 x){AH1 y=AFractH1(x*AH1_(0.5)+AH1_(0.75));y=y*AH1_(2.0)-AH1_(1.0);return APSinH2(AH2(x,y));}
  1961. #endif
  1962. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1963. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1964. //_____________________________________________________________/\_______________________________________________________________
  1965. //==============================================================================================================================
  1966. // [ZOL] ZERO ONE LOGIC
  1967. //------------------------------------------------------------------------------------------------------------------------------
  1968. // Conditional free logic designed for easy 16-bit packing, and backwards porting to 32-bit.
  1969. //------------------------------------------------------------------------------------------------------------------------------
  1970. // 0 := false
  1971. // 1 := true
  1972. //------------------------------------------------------------------------------------------------------------------------------
  1973. // AndNot(x,y) -> !(x&y) .... One op.
  1974. // AndOr(x,y,z) -> (x&y)|z ... One op.
  1975. // GtZero(x) -> x>0.0 ..... One op.
  1976. // Sel(x,y,z) -> x?y:z ..... Two ops, has no precision loss.
  1977. // Signed(x) -> x<0.0 ..... One op.
  1978. // ZeroPass(x,y) -> x?0:y ..... Two ops, 'y' is a pass through safe for aliasing as integer.
  1979. //------------------------------------------------------------------------------------------------------------------------------
  1980. // OPTIMIZATION NOTES
  1981. // ==================
  1982. // - On Vega to use 2 constants in a packed op, pass in as one AW2 or one AH2 'k.xy' and use as 'k.xx' and 'k.yy'.
  1983. // For example 'a.xy*k.xx+k.yy'.
  1984. //==============================================================================================================================
  1985. #if 1
  1986. AU1 AZolAndU1(AU1 x,AU1 y){return min(x,y);}
  1987. AU2 AZolAndU2(AU2 x,AU2 y){return min(x,y);}
  1988. AU3 AZolAndU3(AU3 x,AU3 y){return min(x,y);}
  1989. AU4 AZolAndU4(AU4 x,AU4 y){return min(x,y);}
  1990. //------------------------------------------------------------------------------------------------------------------------------
  1991. AU1 AZolNotU1(AU1 x){return x^AU1_(1);}
  1992. AU2 AZolNotU2(AU2 x){return x^AU2_(1);}
  1993. AU3 AZolNotU3(AU3 x){return x^AU3_(1);}
  1994. AU4 AZolNotU4(AU4 x){return x^AU4_(1);}
  1995. //------------------------------------------------------------------------------------------------------------------------------
  1996. AU1 AZolOrU1(AU1 x,AU1 y){return max(x,y);}
  1997. AU2 AZolOrU2(AU2 x,AU2 y){return max(x,y);}
  1998. AU3 AZolOrU3(AU3 x,AU3 y){return max(x,y);}
  1999. AU4 AZolOrU4(AU4 x,AU4 y){return max(x,y);}
  2000. //==============================================================================================================================
  2001. AU1 AZolF1ToU1(AF1 x){return AU1(x);}
  2002. AU2 AZolF2ToU2(AF2 x){return AU2(x);}
  2003. AU3 AZolF3ToU3(AF3 x){return AU3(x);}
  2004. AU4 AZolF4ToU4(AF4 x){return AU4(x);}
  2005. //------------------------------------------------------------------------------------------------------------------------------
  2006. // 2 ops, denormals don't work in 32-bit on PC (and if they are enabled, OMOD is disabled).
  2007. AU1 AZolNotF1ToU1(AF1 x){return AU1(AF1_(1.0)-x);}
  2008. AU2 AZolNotF2ToU2(AF2 x){return AU2(AF2_(1.0)-x);}
  2009. AU3 AZolNotF3ToU3(AF3 x){return AU3(AF3_(1.0)-x);}
  2010. AU4 AZolNotF4ToU4(AF4 x){return AU4(AF4_(1.0)-x);}
  2011. //------------------------------------------------------------------------------------------------------------------------------
  2012. AF1 AZolU1ToF1(AU1 x){return AF1(x);}
  2013. AF2 AZolU2ToF2(AU2 x){return AF2(x);}
  2014. AF3 AZolU3ToF3(AU3 x){return AF3(x);}
  2015. AF4 AZolU4ToF4(AU4 x){return AF4(x);}
  2016. //==============================================================================================================================
  2017. AF1 AZolAndF1(AF1 x,AF1 y){return min(x,y);}
  2018. AF2 AZolAndF2(AF2 x,AF2 y){return min(x,y);}
  2019. AF3 AZolAndF3(AF3 x,AF3 y){return min(x,y);}
  2020. AF4 AZolAndF4(AF4 x,AF4 y){return min(x,y);}
  2021. //------------------------------------------------------------------------------------------------------------------------------
  2022. AF1 ASolAndNotF1(AF1 x,AF1 y){return (-x)*y+AF1_(1.0);}
  2023. AF2 ASolAndNotF2(AF2 x,AF2 y){return (-x)*y+AF2_(1.0);}
  2024. AF3 ASolAndNotF3(AF3 x,AF3 y){return (-x)*y+AF3_(1.0);}
  2025. AF4 ASolAndNotF4(AF4 x,AF4 y){return (-x)*y+AF4_(1.0);}
  2026. //------------------------------------------------------------------------------------------------------------------------------
  2027. AF1 AZolAndOrF1(AF1 x,AF1 y,AF1 z){return ASatF1(x*y+z);}
  2028. AF2 AZolAndOrF2(AF2 x,AF2 y,AF2 z){return ASatF2(x*y+z);}
  2029. AF3 AZolAndOrF3(AF3 x,AF3 y,AF3 z){return ASatF3(x*y+z);}
  2030. AF4 AZolAndOrF4(AF4 x,AF4 y,AF4 z){return ASatF4(x*y+z);}
  2031. //------------------------------------------------------------------------------------------------------------------------------
  2032. AF1 AZolGtZeroF1(AF1 x){return ASatF1(x*AF1_(A_INFP_F));}
  2033. AF2 AZolGtZeroF2(AF2 x){return ASatF2(x*AF2_(A_INFP_F));}
  2034. AF3 AZolGtZeroF3(AF3 x){return ASatF3(x*AF3_(A_INFP_F));}
  2035. AF4 AZolGtZeroF4(AF4 x){return ASatF4(x*AF4_(A_INFP_F));}
  2036. //------------------------------------------------------------------------------------------------------------------------------
  2037. AF1 AZolNotF1(AF1 x){return AF1_(1.0)-x;}
  2038. AF2 AZolNotF2(AF2 x){return AF2_(1.0)-x;}
  2039. AF3 AZolNotF3(AF3 x){return AF3_(1.0)-x;}
  2040. AF4 AZolNotF4(AF4 x){return AF4_(1.0)-x;}
  2041. //------------------------------------------------------------------------------------------------------------------------------
  2042. AF1 AZolOrF1(AF1 x,AF1 y){return max(x,y);}
  2043. AF2 AZolOrF2(AF2 x,AF2 y){return max(x,y);}
  2044. AF3 AZolOrF3(AF3 x,AF3 y){return max(x,y);}
  2045. AF4 AZolOrF4(AF4 x,AF4 y){return max(x,y);}
  2046. //------------------------------------------------------------------------------------------------------------------------------
  2047. AF1 AZolSelF1(AF1 x,AF1 y,AF1 z){AF1 r=(-x)*z+z;return x*y+r;}
  2048. AF2 AZolSelF2(AF2 x,AF2 y,AF2 z){AF2 r=(-x)*z+z;return x*y+r;}
  2049. AF3 AZolSelF3(AF3 x,AF3 y,AF3 z){AF3 r=(-x)*z+z;return x*y+r;}
  2050. AF4 AZolSelF4(AF4 x,AF4 y,AF4 z){AF4 r=(-x)*z+z;return x*y+r;}
  2051. //------------------------------------------------------------------------------------------------------------------------------
  2052. AF1 AZolSignedF1(AF1 x){return ASatF1(x*AF1_(A_INFN_F));}
  2053. AF2 AZolSignedF2(AF2 x){return ASatF2(x*AF2_(A_INFN_F));}
  2054. AF3 AZolSignedF3(AF3 x){return ASatF3(x*AF3_(A_INFN_F));}
  2055. AF4 AZolSignedF4(AF4 x){return ASatF4(x*AF4_(A_INFN_F));}
  2056. //------------------------------------------------------------------------------------------------------------------------------
  2057. AF1 AZolZeroPassF1(AF1 x,AF1 y){return AF1_AU1((AU1_AF1(x)!=AU1_(0))?AU1_(0):AU1_AF1(y));}
  2058. AF2 AZolZeroPassF2(AF2 x,AF2 y){return AF2_AU2((AU2_AF2(x)!=AU2_(0))?AU2_(0):AU2_AF2(y));}
  2059. AF3 AZolZeroPassF3(AF3 x,AF3 y){return AF3_AU3((AU3_AF3(x)!=AU3_(0))?AU3_(0):AU3_AF3(y));}
  2060. AF4 AZolZeroPassF4(AF4 x,AF4 y){return AF4_AU4((AU4_AF4(x)!=AU4_(0))?AU4_(0):AU4_AF4(y));}
  2061. #endif
  2062. //==============================================================================================================================
  2063. #ifdef A_HALF
  2064. AW1 AZolAndW1(AW1 x,AW1 y){return min(x,y);}
  2065. AW2 AZolAndW2(AW2 x,AW2 y){return min(x,y);}
  2066. AW3 AZolAndW3(AW3 x,AW3 y){return min(x,y);}
  2067. AW4 AZolAndW4(AW4 x,AW4 y){return min(x,y);}
  2068. //------------------------------------------------------------------------------------------------------------------------------
  2069. AW1 AZolNotW1(AW1 x){return x^AW1_(1);}
  2070. AW2 AZolNotW2(AW2 x){return x^AW2_(1);}
  2071. AW3 AZolNotW3(AW3 x){return x^AW3_(1);}
  2072. AW4 AZolNotW4(AW4 x){return x^AW4_(1);}
  2073. //------------------------------------------------------------------------------------------------------------------------------
  2074. AW1 AZolOrW1(AW1 x,AW1 y){return max(x,y);}
  2075. AW2 AZolOrW2(AW2 x,AW2 y){return max(x,y);}
  2076. AW3 AZolOrW3(AW3 x,AW3 y){return max(x,y);}
  2077. AW4 AZolOrW4(AW4 x,AW4 y){return max(x,y);}
  2078. //==============================================================================================================================
  2079. // Uses denormal trick.
  2080. AW1 AZolH1ToW1(AH1 x){return AW1_AH1(x*AH1_AW1(AW1_(1)));}
  2081. AW2 AZolH2ToW2(AH2 x){return AW2_AH2(x*AH2_AW2(AW2_(1)));}
  2082. AW3 AZolH3ToW3(AH3 x){return AW3_AH3(x*AH3_AW3(AW3_(1)));}
  2083. AW4 AZolH4ToW4(AH4 x){return AW4_AH4(x*AH4_AW4(AW4_(1)));}
  2084. //------------------------------------------------------------------------------------------------------------------------------
  2085. // AMD arch lacks a packed conversion opcode.
  2086. AH1 AZolW1ToH1(AW1 x){return AH1_AW1(x*AW1_AH1(AH1_(1.0)));}
  2087. AH2 AZolW2ToH2(AW2 x){return AH2_AW2(x*AW2_AH2(AH2_(1.0)));}
  2088. AH3 AZolW1ToH3(AW3 x){return AH3_AW3(x*AW3_AH3(AH3_(1.0)));}
  2089. AH4 AZolW2ToH4(AW4 x){return AH4_AW4(x*AW4_AH4(AH4_(1.0)));}
  2090. //==============================================================================================================================
  2091. AH1 AZolAndH1(AH1 x,AH1 y){return min(x,y);}
  2092. AH2 AZolAndH2(AH2 x,AH2 y){return min(x,y);}
  2093. AH3 AZolAndH3(AH3 x,AH3 y){return min(x,y);}
  2094. AH4 AZolAndH4(AH4 x,AH4 y){return min(x,y);}
  2095. //------------------------------------------------------------------------------------------------------------------------------
  2096. AH1 ASolAndNotH1(AH1 x,AH1 y){return (-x)*y+AH1_(1.0);}
  2097. AH2 ASolAndNotH2(AH2 x,AH2 y){return (-x)*y+AH2_(1.0);}
  2098. AH3 ASolAndNotH3(AH3 x,AH3 y){return (-x)*y+AH3_(1.0);}
  2099. AH4 ASolAndNotH4(AH4 x,AH4 y){return (-x)*y+AH4_(1.0);}
  2100. //------------------------------------------------------------------------------------------------------------------------------
  2101. AH1 AZolAndOrH1(AH1 x,AH1 y,AH1 z){return ASatH1(x*y+z);}
  2102. AH2 AZolAndOrH2(AH2 x,AH2 y,AH2 z){return ASatH2(x*y+z);}
  2103. AH3 AZolAndOrH3(AH3 x,AH3 y,AH3 z){return ASatH3(x*y+z);}
  2104. AH4 AZolAndOrH4(AH4 x,AH4 y,AH4 z){return ASatH4(x*y+z);}
  2105. //------------------------------------------------------------------------------------------------------------------------------
  2106. AH1 AZolGtZeroH1(AH1 x){return ASatH1(x*AH1_(A_INFP_H));}
  2107. AH2 AZolGtZeroH2(AH2 x){return ASatH2(x*AH2_(A_INFP_H));}
  2108. AH3 AZolGtZeroH3(AH3 x){return ASatH3(x*AH3_(A_INFP_H));}
  2109. AH4 AZolGtZeroH4(AH4 x){return ASatH4(x*AH4_(A_INFP_H));}
  2110. //------------------------------------------------------------------------------------------------------------------------------
  2111. AH1 AZolNotH1(AH1 x){return AH1_(1.0)-x;}
  2112. AH2 AZolNotH2(AH2 x){return AH2_(1.0)-x;}
  2113. AH3 AZolNotH3(AH3 x){return AH3_(1.0)-x;}
  2114. AH4 AZolNotH4(AH4 x){return AH4_(1.0)-x;}
  2115. //------------------------------------------------------------------------------------------------------------------------------
  2116. AH1 AZolOrH1(AH1 x,AH1 y){return max(x,y);}
  2117. AH2 AZolOrH2(AH2 x,AH2 y){return max(x,y);}
  2118. AH3 AZolOrH3(AH3 x,AH3 y){return max(x,y);}
  2119. AH4 AZolOrH4(AH4 x,AH4 y){return max(x,y);}
  2120. //------------------------------------------------------------------------------------------------------------------------------
  2121. AH1 AZolSelH1(AH1 x,AH1 y,AH1 z){AH1 r=(-x)*z+z;return x*y+r;}
  2122. AH2 AZolSelH2(AH2 x,AH2 y,AH2 z){AH2 r=(-x)*z+z;return x*y+r;}
  2123. AH3 AZolSelH3(AH3 x,AH3 y,AH3 z){AH3 r=(-x)*z+z;return x*y+r;}
  2124. AH4 AZolSelH4(AH4 x,AH4 y,AH4 z){AH4 r=(-x)*z+z;return x*y+r;}
  2125. //------------------------------------------------------------------------------------------------------------------------------
  2126. AH1 AZolSignedH1(AH1 x){return ASatH1(x*AH1_(A_INFN_H));}
  2127. AH2 AZolSignedH2(AH2 x){return ASatH2(x*AH2_(A_INFN_H));}
  2128. AH3 AZolSignedH3(AH3 x){return ASatH3(x*AH3_(A_INFN_H));}
  2129. AH4 AZolSignedH4(AH4 x){return ASatH4(x*AH4_(A_INFN_H));}
  2130. #endif
  2131. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2132. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2133. //_____________________________________________________________/\_______________________________________________________________
  2134. //==============================================================================================================================
  2135. // COLOR CONVERSIONS
  2136. //------------------------------------------------------------------------------------------------------------------------------
  2137. // These are all linear to/from some other space (where 'linear' has been shortened out of the function name).
  2138. // So 'ToGamma' is 'LinearToGamma', and 'FromGamma' is 'LinearFromGamma'.
  2139. // These are branch free implementations.
  2140. // The AToSrgbF1() function is useful for stores for compute shaders for GPUs without hardware linear->sRGB store conversion.
  2141. //------------------------------------------------------------------------------------------------------------------------------
  2142. // TRANSFER FUNCTIONS
  2143. // ==================
  2144. // 709 ..... Rec709 used for some HDTVs
  2145. // Gamma ... Typically 2.2 for some PC displays, or 2.4-2.5 for CRTs, or 2.2 FreeSync2 native
  2146. // Pq ...... PQ native for HDR10
  2147. // Srgb .... The sRGB output, typical of PC displays, useful for 10-bit output, or storing to 8-bit UNORM without SRGB type
  2148. // Two ..... Gamma 2.0, fastest conversion (useful for intermediate pass approximations)
  2149. // Three ... Gamma 3.0, less fast, but good for HDR.
  2150. //------------------------------------------------------------------------------------------------------------------------------
  2151. // KEEPING TO SPEC
  2152. // ===============
  2153. // Both Rec.709 and sRGB have a linear segment which as spec'ed would intersect the curved segment 2 times.
  2154. // (a.) For 8-bit sRGB, steps {0 to 10.3} are in the linear region (4% of the encoding range).
  2155. // (b.) For 8-bit 709, steps {0 to 20.7} are in the linear region (8% of the encoding range).
  2156. // Also there is a slight step in the transition regions.
  2157. // Precision of the coefficients in the spec being the likely cause.
  2158. // Main usage case of the sRGB code is to do the linear->sRGB converstion in a compute shader before store.
  2159. // This is to work around lack of hardware (typically only ROP does the conversion for free).
  2160. // To "correct" the linear segment, would be to introduce error, because hardware decode of sRGB->linear is fixed (and free).
  2161. // So this header keeps with the spec.
  2162. // For linear->sRGB transforms, the linear segment in some respects reduces error, because rounding in that region is linear.
  2163. // Rounding in the curved region in hardware (and fast software code) introduces error due to rounding in non-linear.
  2164. //------------------------------------------------------------------------------------------------------------------------------
  2165. // FOR PQ
  2166. // ======
  2167. // Both input and output is {0.0-1.0}, and where output 1.0 represents 10000.0 cd/m^2.
  2168. // All constants are only specified to FP32 precision.
  2169. // External PQ source reference,
  2170. // - https://github.com/ampas/aces-dev/blob/master/transforms/ctl/utilities/ACESlib.Utilities_Color.a1.0.1.ctl
  2171. //------------------------------------------------------------------------------------------------------------------------------
  2172. // PACKED VERSIONS
  2173. // ===============
  2174. // These are the A*H2() functions.
  2175. // There is no PQ functions as FP16 seemed to not have enough precision for the conversion.
  2176. // The remaining functions are "good enough" for 8-bit, and maybe 10-bit if not concerned about a few 1-bit errors.
  2177. // Precision is lowest in the 709 conversion, higher in sRGB, higher still in Two and Gamma (when using 2.2 at least).
  2178. //------------------------------------------------------------------------------------------------------------------------------
  2179. // NOTES
  2180. // =====
  2181. // Could be faster for PQ conversions to be in ALU or a texture lookup depending on usage case.
  2182. //==============================================================================================================================
  2183. #if 1
  2184. AF1 ATo709F1(AF1 c){AF3 j=AF3(0.018*4.5,4.5,0.45);AF2 k=AF2(1.099,-0.099);
  2185. return clamp(j.x ,c*j.y ,pow(c,j.z )*k.x +k.y );}
  2186. AF2 ATo709F2(AF2 c){AF3 j=AF3(0.018*4.5,4.5,0.45);AF2 k=AF2(1.099,-0.099);
  2187. return clamp(j.xx ,c*j.yy ,pow(c,j.zz )*k.xx +k.yy );}
  2188. AF3 ATo709F3(AF3 c){AF3 j=AF3(0.018*4.5,4.5,0.45);AF2 k=AF2(1.099,-0.099);
  2189. return clamp(j.xxx,c*j.yyy,pow(c,j.zzz)*k.xxx+k.yyy);}
  2190. //------------------------------------------------------------------------------------------------------------------------------
  2191. // Note 'rcpX' is '1/x', where the 'x' is what would be used in AFromGamma().
  2192. AF1 AToGammaF1(AF1 c,AF1 rcpX){return pow(c,AF1_(rcpX));}
  2193. AF2 AToGammaF2(AF2 c,AF1 rcpX){return pow(c,AF2_(rcpX));}
  2194. AF3 AToGammaF3(AF3 c,AF1 rcpX){return pow(c,AF3_(rcpX));}
  2195. //------------------------------------------------------------------------------------------------------------------------------
  2196. AF1 AToPqF1(AF1 x){AF1 p=pow(x,AF1_(0.159302));
  2197. return pow((AF1_(0.835938)+AF1_(18.8516)*p)/(AF1_(1.0)+AF1_(18.6875)*p),AF1_(78.8438));}
  2198. AF2 AToPqF1(AF2 x){AF2 p=pow(x,AF2_(0.159302));
  2199. return pow((AF2_(0.835938)+AF2_(18.8516)*p)/(AF2_(1.0)+AF2_(18.6875)*p),AF2_(78.8438));}
  2200. AF3 AToPqF1(AF3 x){AF3 p=pow(x,AF3_(0.159302));
  2201. return pow((AF3_(0.835938)+AF3_(18.8516)*p)/(AF3_(1.0)+AF3_(18.6875)*p),AF3_(78.8438));}
  2202. //------------------------------------------------------------------------------------------------------------------------------
  2203. AF1 AToSrgbF1(AF1 c){AF3 j=AF3(0.0031308*12.92,12.92,1.0/2.4);AF2 k=AF2(1.055,-0.055);
  2204. return clamp(j.x ,c*j.y ,pow(c,j.z )*k.x +k.y );}
  2205. AF2 AToSrgbF2(AF2 c){AF3 j=AF3(0.0031308*12.92,12.92,1.0/2.4);AF2 k=AF2(1.055,-0.055);
  2206. return clamp(j.xx ,c*j.yy ,pow(c,j.zz )*k.xx +k.yy );}
  2207. AF3 AToSrgbF3(AF3 c){AF3 j=AF3(0.0031308*12.92,12.92,1.0/2.4);AF2 k=AF2(1.055,-0.055);
  2208. return clamp(j.xxx,c*j.yyy,pow(c,j.zzz)*k.xxx+k.yyy);}
  2209. //------------------------------------------------------------------------------------------------------------------------------
  2210. AF1 AToTwoF1(AF1 c){return sqrt(c);}
  2211. AF2 AToTwoF2(AF2 c){return sqrt(c);}
  2212. AF3 AToTwoF3(AF3 c){return sqrt(c);}
  2213. //------------------------------------------------------------------------------------------------------------------------------
  2214. AF1 AToThreeF1(AF1 c){return pow(c,AF1_(1.0/3.0));}
  2215. AF2 AToThreeF2(AF2 c){return pow(c,AF2_(1.0/3.0));}
  2216. AF3 AToThreeF3(AF3 c){return pow(c,AF3_(1.0/3.0));}
  2217. #endif
  2218. //==============================================================================================================================
  2219. #if 1
  2220. // Unfortunately median won't work here.
  2221. AF1 AFrom709F1(AF1 c){AF3 j=AF3(0.081/4.5,1.0/4.5,1.0/0.45);AF2 k=AF2(1.0/1.099,0.099/1.099);
  2222. return AZolSelF1(AZolSignedF1(c-j.x ),c*j.y ,pow(c*k.x +k.y ,j.z ));}
  2223. AF2 AFrom709F2(AF2 c){AF3 j=AF3(0.081/4.5,1.0/4.5,1.0/0.45);AF2 k=AF2(1.0/1.099,0.099/1.099);
  2224. return AZolSelF2(AZolSignedF2(c-j.xx ),c*j.yy ,pow(c*k.xx +k.yy ,j.zz ));}
  2225. AF3 AFrom709F3(AF3 c){AF3 j=AF3(0.081/4.5,1.0/4.5,1.0/0.45);AF2 k=AF2(1.0/1.099,0.099/1.099);
  2226. return AZolSelF3(AZolSignedF3(c-j.xxx),c*j.yyy,pow(c*k.xxx+k.yyy,j.zzz));}
  2227. //------------------------------------------------------------------------------------------------------------------------------
  2228. AF1 AFromGammaF1(AF1 c,AF1 x){return pow(c,AF1_(x));}
  2229. AF2 AFromGammaF2(AF2 c,AF1 x){return pow(c,AF2_(x));}
  2230. AF3 AFromGammaF3(AF3 c,AF1 x){return pow(c,AF3_(x));}
  2231. //------------------------------------------------------------------------------------------------------------------------------
  2232. AF1 AFromPqF1(AF1 x){AF1 p=pow(x,AF1_(0.0126833));
  2233. return pow(ASatF1(p-AF1_(0.835938))/(AF1_(18.8516)-AF1_(18.6875)*p),AF1_(6.27739));}
  2234. AF2 AFromPqF1(AF2 x){AF2 p=pow(x,AF2_(0.0126833));
  2235. return pow(ASatF2(p-AF2_(0.835938))/(AF2_(18.8516)-AF2_(18.6875)*p),AF2_(6.27739));}
  2236. AF3 AFromPqF1(AF3 x){AF3 p=pow(x,AF3_(0.0126833));
  2237. return pow(ASatF3(p-AF3_(0.835938))/(AF3_(18.8516)-AF3_(18.6875)*p),AF3_(6.27739));}
  2238. //------------------------------------------------------------------------------------------------------------------------------
  2239. // Unfortunately median won't work here.
  2240. AF1 AFromSrgbF1(AF1 c){AF3 j=AF3(0.04045/12.92,1.0/12.92,2.4);AF2 k=AF2(1.0/1.055,0.055/1.055);
  2241. return AZolSelF1(AZolSignedF1(c-j.x ),c*j.y ,pow(c*k.x +k.y ,j.z ));}
  2242. AF2 AFromSrgbF2(AF2 c){AF3 j=AF3(0.04045/12.92,1.0/12.92,2.4);AF2 k=AF2(1.0/1.055,0.055/1.055);
  2243. return AZolSelF2(AZolSignedF2(c-j.xx ),c*j.yy ,pow(c*k.xx +k.yy ,j.zz ));}
  2244. AF3 AFromSrgbF3(AF3 c){AF3 j=AF3(0.04045/12.92,1.0/12.92,2.4);AF2 k=AF2(1.0/1.055,0.055/1.055);
  2245. return AZolSelF3(AZolSignedF3(c-j.xxx),c*j.yyy,pow(c*k.xxx+k.yyy,j.zzz));}
  2246. //------------------------------------------------------------------------------------------------------------------------------
  2247. AF1 AFromTwoF1(AF1 c){return c*c;}
  2248. AF2 AFromTwoF2(AF2 c){return c*c;}
  2249. AF3 AFromTwoF3(AF3 c){return c*c;}
  2250. //------------------------------------------------------------------------------------------------------------------------------
  2251. AF1 AFromThreeF1(AF1 c){return c*c*c;}
  2252. AF2 AFromThreeF2(AF2 c){return c*c*c;}
  2253. AF3 AFromThreeF3(AF3 c){return c*c*c;}
  2254. #endif
  2255. //==============================================================================================================================
  2256. #ifdef A_HALF
  2257. AH1 ATo709H1(AH1 c){AH3 j=AH3(0.018*4.5,4.5,0.45);AH2 k=AH2(1.099,-0.099);
  2258. return clamp(j.x ,c*j.y ,pow(c,j.z )*k.x +k.y );}
  2259. AH2 ATo709H2(AH2 c){AH3 j=AH3(0.018*4.5,4.5,0.45);AH2 k=AH2(1.099,-0.099);
  2260. return clamp(j.xx ,c*j.yy ,pow(c,j.zz )*k.xx +k.yy );}
  2261. AH3 ATo709H3(AH3 c){AH3 j=AH3(0.018*4.5,4.5,0.45);AH2 k=AH2(1.099,-0.099);
  2262. return clamp(j.xxx,c*j.yyy,pow(c,j.zzz)*k.xxx+k.yyy);}
  2263. //------------------------------------------------------------------------------------------------------------------------------
  2264. AH1 AToGammaH1(AH1 c,AH1 rcpX){return pow(c,AH1_(rcpX));}
  2265. AH2 AToGammaH2(AH2 c,AH1 rcpX){return pow(c,AH2_(rcpX));}
  2266. AH3 AToGammaH3(AH3 c,AH1 rcpX){return pow(c,AH3_(rcpX));}
  2267. //------------------------------------------------------------------------------------------------------------------------------
  2268. AH1 AToSrgbH1(AH1 c){AH3 j=AH3(0.0031308*12.92,12.92,1.0/2.4);AH2 k=AH2(1.055,-0.055);
  2269. return clamp(j.x ,c*j.y ,pow(c,j.z )*k.x +k.y );}
  2270. AH2 AToSrgbH2(AH2 c){AH3 j=AH3(0.0031308*12.92,12.92,1.0/2.4);AH2 k=AH2(1.055,-0.055);
  2271. return clamp(j.xx ,c*j.yy ,pow(c,j.zz )*k.xx +k.yy );}
  2272. AH3 AToSrgbH3(AH3 c){AH3 j=AH3(0.0031308*12.92,12.92,1.0/2.4);AH2 k=AH2(1.055,-0.055);
  2273. return clamp(j.xxx,c*j.yyy,pow(c,j.zzz)*k.xxx+k.yyy);}
  2274. //------------------------------------------------------------------------------------------------------------------------------
  2275. AH1 AToTwoH1(AH1 c){return sqrt(c);}
  2276. AH2 AToTwoH2(AH2 c){return sqrt(c);}
  2277. AH3 AToTwoH3(AH3 c){return sqrt(c);}
  2278. //------------------------------------------------------------------------------------------------------------------------------
  2279. AH1 AToThreeF1(AH1 c){return pow(c,AH1_(1.0/3.0));}
  2280. AH2 AToThreeF2(AH2 c){return pow(c,AH2_(1.0/3.0));}
  2281. AH3 AToThreeF3(AH3 c){return pow(c,AH3_(1.0/3.0));}
  2282. #endif
  2283. //==============================================================================================================================
  2284. #ifdef A_HALF
  2285. AH1 AFrom709H1(AH1 c){AH3 j=AH3(0.081/4.5,1.0/4.5,1.0/0.45);AH2 k=AH2(1.0/1.099,0.099/1.099);
  2286. return AZolSelH1(AZolSignedH1(c-j.x ),c*j.y ,pow(c*k.x +k.y ,j.z ));}
  2287. AH2 AFrom709H2(AH2 c){AH3 j=AH3(0.081/4.5,1.0/4.5,1.0/0.45);AH2 k=AH2(1.0/1.099,0.099/1.099);
  2288. return AZolSelH2(AZolSignedH2(c-j.xx ),c*j.yy ,pow(c*k.xx +k.yy ,j.zz ));}
  2289. AH3 AFrom709H3(AH3 c){AH3 j=AH3(0.081/4.5,1.0/4.5,1.0/0.45);AH2 k=AH2(1.0/1.099,0.099/1.099);
  2290. return AZolSelH3(AZolSignedH3(c-j.xxx),c*j.yyy,pow(c*k.xxx+k.yyy,j.zzz));}
  2291. //------------------------------------------------------------------------------------------------------------------------------
  2292. AH1 AFromGammaH1(AH1 c,AH1 x){return pow(c,AH1_(x));}
  2293. AH2 AFromGammaH2(AH2 c,AH1 x){return pow(c,AH2_(x));}
  2294. AH3 AFromGammaH3(AH3 c,AH1 x){return pow(c,AH3_(x));}
  2295. //------------------------------------------------------------------------------------------------------------------------------
  2296. AH1 AHromSrgbF1(AH1 c){AH3 j=AH3(0.04045/12.92,1.0/12.92,2.4);AH2 k=AH2(1.0/1.055,0.055/1.055);
  2297. return AZolSelH1(AZolSignedH1(c-j.x ),c*j.y ,pow(c*k.x +k.y ,j.z ));}
  2298. AH2 AHromSrgbF2(AH2 c){AH3 j=AH3(0.04045/12.92,1.0/12.92,2.4);AH2 k=AH2(1.0/1.055,0.055/1.055);
  2299. return AZolSelH2(AZolSignedH2(c-j.xx ),c*j.yy ,pow(c*k.xx +k.yy ,j.zz ));}
  2300. AH3 AHromSrgbF3(AH3 c){AH3 j=AH3(0.04045/12.92,1.0/12.92,2.4);AH2 k=AH2(1.0/1.055,0.055/1.055);
  2301. return AZolSelH3(AZolSignedH3(c-j.xxx),c*j.yyy,pow(c*k.xxx+k.yyy,j.zzz));}
  2302. //------------------------------------------------------------------------------------------------------------------------------
  2303. AH1 AFromTwoH1(AH1 c){return c*c;}
  2304. AH2 AFromTwoH2(AH2 c){return c*c;}
  2305. AH3 AFromTwoH3(AH3 c){return c*c;}
  2306. //------------------------------------------------------------------------------------------------------------------------------
  2307. AH1 AFromThreeH1(AH1 c){return c*c*c;}
  2308. AH2 AFromThreeH2(AH2 c){return c*c*c;}
  2309. AH3 AFromThreeH3(AH3 c){return c*c*c;}
  2310. #endif
  2311. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2312. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2313. //_____________________________________________________________/\_______________________________________________________________
  2314. //==============================================================================================================================
  2315. // CS REMAP
  2316. //==============================================================================================================================
  2317. // Simple remap 64x1 to 8x8 with rotated 2x2 pixel quads in quad linear.
  2318. // 543210
  2319. // ======
  2320. // ..xxx.
  2321. // yy...y
  2322. AU2 ARmp8x8(AU1 a){return AU2(ABfe(a,1u,3u),ABfiM(ABfe(a,3u,3u),a,1u));}
  2323. //==============================================================================================================================
  2324. // More complex remap 64x1 to 8x8 which is necessary for 2D wave reductions.
  2325. // 543210
  2326. // ======
  2327. // .xx..x
  2328. // y..yy.
  2329. // Details,
  2330. // LANE TO 8x8 MAPPING
  2331. // ===================
  2332. // 00 01 08 09 10 11 18 19
  2333. // 02 03 0a 0b 12 13 1a 1b
  2334. // 04 05 0c 0d 14 15 1c 1d
  2335. // 06 07 0e 0f 16 17 1e 1f
  2336. // 20 21 28 29 30 31 38 39
  2337. // 22 23 2a 2b 32 33 3a 3b
  2338. // 24 25 2c 2d 34 35 3c 3d
  2339. // 26 27 2e 2f 36 37 3e 3f
  2340. AU2 ARmpRed8x8(AU1 a){return AU2(ABfiM(ABfe(a,2u,3u),a,1u),ABfiM(ABfe(a,3u,3u),ABfe(a,1u,2u),2u));}
  2341. //==============================================================================================================================
  2342. #ifdef A_HALF
  2343. AW2 ARmp8x8H(AU1 a){return AW2(ABfe(a,1u,3u),ABfiM(ABfe(a,3u,3u),a,1u));}
  2344. AW2 ARmpRed8x8H(AU1 a){return AW2(ABfiM(ABfe(a,2u,3u),a,1u),ABfiM(ABfe(a,3u,3u),ABfe(a,1u,2u),2u));}
  2345. #endif
  2346. #endif
  2347. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2348. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2349. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2350. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2351. //_____________________________________________________________/\_______________________________________________________________
  2352. //==============================================================================================================================
  2353. //
  2354. // REFERENCE
  2355. //
  2356. //------------------------------------------------------------------------------------------------------------------------------
  2357. // IEEE FLOAT RULES
  2358. // ================
  2359. // - saturate(NaN)=0, saturate(-INF)=0, saturate(+INF)=1
  2360. // - {+/-}0 * {+/-}INF = NaN
  2361. // - -INF + (+INF) = NaN
  2362. // - {+/-}0 / {+/-}0 = NaN
  2363. // - {+/-}INF / {+/-}INF = NaN
  2364. // - a<(-0) := sqrt(a) = NaN (a=-0.0 won't NaN)
  2365. // - 0 == -0
  2366. // - 4/0 = +INF
  2367. // - 4/-0 = -INF
  2368. // - 4+INF = +INF
  2369. // - 4-INF = -INF
  2370. // - 4*(+INF) = +INF
  2371. // - 4*(-INF) = -INF
  2372. // - -4*(+INF) = -INF
  2373. // - sqrt(+INF) = +INF
  2374. //------------------------------------------------------------------------------------------------------------------------------
  2375. // FP16 ENCODING
  2376. // =============
  2377. // fedcba9876543210
  2378. // ----------------
  2379. // ......mmmmmmmmmm 10-bit mantissa (encodes 11-bit 0.5 to 1.0 except for denormals)
  2380. // .eeeee.......... 5-bit exponent
  2381. // .00000.......... denormals
  2382. // .00001.......... -14 exponent
  2383. // .11110.......... 15 exponent
  2384. // .111110000000000 infinity
  2385. // .11111nnnnnnnnnn NaN with n!=0
  2386. // s............... sign
  2387. //------------------------------------------------------------------------------------------------------------------------------
  2388. // FP16/INT16 ALIASING DENORMAL
  2389. // ============================
  2390. // 11-bit unsigned integers alias with half float denormal/normal values,
  2391. // 1 = 2^(-24) = 1/16777216 ....................... first denormal value
  2392. // 2 = 2^(-23)
  2393. // ...
  2394. // 1023 = 2^(-14)*(1-2^(-10)) = 2^(-14)*(1-1/1024) ... last denormal value
  2395. // 1024 = 2^(-14) = 1/16384 .......................... first normal value that still maps to integers
  2396. // 2047 .............................................. last normal value that still maps to integers
  2397. // Scaling limits,
  2398. // 2^15 = 32768 ...................................... largest power of 2 scaling
  2399. // Largest pow2 conversion mapping is at *32768,
  2400. // 1 : 2^(-9) = 1/512
  2401. // 2 : 1/256
  2402. // 4 : 1/128
  2403. // 8 : 1/64
  2404. // 16 : 1/32
  2405. // 32 : 1/16
  2406. // 64 : 1/8
  2407. // 128 : 1/4
  2408. // 256 : 1/2
  2409. // 512 : 1
  2410. // 1024 : 2
  2411. // 2047 : a little less than 4
  2412. //==============================================================================================================================
  2413. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2414. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2415. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2416. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2417. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2418. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2419. //_____________________________________________________________/\_______________________________________________________________
  2420. //==============================================================================================================================
  2421. //
  2422. //
  2423. // GPU/CPU PORTABILITY
  2424. //
  2425. //
  2426. //------------------------------------------------------------------------------------------------------------------------------
  2427. // This is the GPU implementation.
  2428. // See the CPU implementation for docs.
  2429. //==============================================================================================================================
  2430. #ifdef A_GPU
  2431. #define A_TRUE true
  2432. #define A_FALSE false
  2433. #define A_STATIC
  2434. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2435. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2436. //_____________________________________________________________/\_______________________________________________________________
  2437. //==============================================================================================================================
  2438. // VECTOR ARGUMENT/RETURN/INITIALIZATION PORTABILITY
  2439. //==============================================================================================================================
  2440. #define retAD2 AD2
  2441. #define retAD3 AD3
  2442. #define retAD4 AD4
  2443. #define retAF2 AF2
  2444. #define retAF3 AF3
  2445. #define retAF4 AF4
  2446. #define retAL2 AL2
  2447. #define retAL3 AL3
  2448. #define retAL4 AL4
  2449. #define retAU2 AU2
  2450. #define retAU3 AU3
  2451. #define retAU4 AU4
  2452. //------------------------------------------------------------------------------------------------------------------------------
  2453. #define inAD2 in AD2
  2454. #define inAD3 in AD3
  2455. #define inAD4 in AD4
  2456. #define inAF2 in AF2
  2457. #define inAF3 in AF3
  2458. #define inAF4 in AF4
  2459. #define inAL2 in AL2
  2460. #define inAL3 in AL3
  2461. #define inAL4 in AL4
  2462. #define inAU2 in AU2
  2463. #define inAU3 in AU3
  2464. #define inAU4 in AU4
  2465. //------------------------------------------------------------------------------------------------------------------------------
  2466. #define inoutAD2 inout AD2
  2467. #define inoutAD3 inout AD3
  2468. #define inoutAD4 inout AD4
  2469. #define inoutAF2 inout AF2
  2470. #define inoutAF3 inout AF3
  2471. #define inoutAF4 inout AF4
  2472. #define inoutAL2 inout AL2
  2473. #define inoutAL3 inout AL3
  2474. #define inoutAL4 inout AL4
  2475. #define inoutAU2 inout AU2
  2476. #define inoutAU3 inout AU3
  2477. #define inoutAU4 inout AU4
  2478. //------------------------------------------------------------------------------------------------------------------------------
  2479. #define outAD2 out AD2
  2480. #define outAD3 out AD3
  2481. #define outAD4 out AD4
  2482. #define outAF2 out AF2
  2483. #define outAF3 out AF3
  2484. #define outAF4 out AF4
  2485. #define outAL2 out AL2
  2486. #define outAL3 out AL3
  2487. #define outAL4 out AL4
  2488. #define outAU2 out AU2
  2489. #define outAU3 out AU3
  2490. #define outAU4 out AU4
  2491. //------------------------------------------------------------------------------------------------------------------------------
  2492. #define varAD2(x) AD2 x
  2493. #define varAD3(x) AD3 x
  2494. #define varAD4(x) AD4 x
  2495. #define varAF2(x) AF2 x
  2496. #define varAF3(x) AF3 x
  2497. #define varAF4(x) AF4 x
  2498. #define varAL2(x) AL2 x
  2499. #define varAL3(x) AL3 x
  2500. #define varAL4(x) AL4 x
  2501. #define varAU2(x) AU2 x
  2502. #define varAU3(x) AU3 x
  2503. #define varAU4(x) AU4 x
  2504. //------------------------------------------------------------------------------------------------------------------------------
  2505. #define initAD2(x,y) AD2(x,y)
  2506. #define initAD3(x,y,z) AD3(x,y,z)
  2507. #define initAD4(x,y,z,w) AD4(x,y,z,w)
  2508. #define initAF2(x,y) AF2(x,y)
  2509. #define initAF3(x,y,z) AF3(x,y,z)
  2510. #define initAF4(x,y,z,w) AF4(x,y,z,w)
  2511. #define initAL2(x,y) AL2(x,y)
  2512. #define initAL3(x,y,z) AL3(x,y,z)
  2513. #define initAL4(x,y,z,w) AL4(x,y,z,w)
  2514. #define initAU2(x,y) AU2(x,y)
  2515. #define initAU3(x,y,z) AU3(x,y,z)
  2516. #define initAU4(x,y,z,w) AU4(x,y,z,w)
  2517. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2518. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2519. //_____________________________________________________________/\_______________________________________________________________
  2520. //==============================================================================================================================
  2521. // SCALAR RETURN OPS
  2522. //==============================================================================================================================
  2523. #define AAbsD1(a) abs(AD1(a))
  2524. #define AAbsF1(a) abs(AF1(a))
  2525. //------------------------------------------------------------------------------------------------------------------------------
  2526. #define ACosD1(a) cos(AD1(a))
  2527. #define ACosF1(a) cos(AF1(a))
  2528. //------------------------------------------------------------------------------------------------------------------------------
  2529. #define ADotD2(a,b) dot(AD2(a),AD2(b))
  2530. #define ADotD3(a,b) dot(AD3(a),AD3(b))
  2531. #define ADotD4(a,b) dot(AD4(a),AD4(b))
  2532. #define ADotF2(a,b) dot(AF2(a),AF2(b))
  2533. #define ADotF3(a,b) dot(AF3(a),AF3(b))
  2534. #define ADotF4(a,b) dot(AF4(a),AF4(b))
  2535. //------------------------------------------------------------------------------------------------------------------------------
  2536. #define AExp2D1(a) exp2(AD1(a))
  2537. #define AExp2F1(a) exp2(AF1(a))
  2538. //------------------------------------------------------------------------------------------------------------------------------
  2539. #define AFloorD1(a) floor(AD1(a))
  2540. #define AFloorF1(a) floor(AF1(a))
  2541. //------------------------------------------------------------------------------------------------------------------------------
  2542. #define ALog2D1(a) log2(AD1(a))
  2543. #define ALog2F1(a) log2(AF1(a))
  2544. //------------------------------------------------------------------------------------------------------------------------------
  2545. #define AMaxD1(a,b) max(a,b)
  2546. #define AMaxF1(a,b) max(a,b)
  2547. #define AMaxL1(a,b) max(a,b)
  2548. #define AMaxU1(a,b) max(a,b)
  2549. //------------------------------------------------------------------------------------------------------------------------------
  2550. #define AMinD1(a,b) min(a,b)
  2551. #define AMinF1(a,b) min(a,b)
  2552. #define AMinL1(a,b) min(a,b)
  2553. #define AMinU1(a,b) min(a,b)
  2554. //------------------------------------------------------------------------------------------------------------------------------
  2555. #define ASinD1(a) sin(AD1(a))
  2556. #define ASinF1(a) sin(AF1(a))
  2557. //------------------------------------------------------------------------------------------------------------------------------
  2558. #define ASqrtD1(a) sqrt(AD1(a))
  2559. #define ASqrtF1(a) sqrt(AF1(a))
  2560. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2561. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2562. //_____________________________________________________________/\_______________________________________________________________
  2563. //==============================================================================================================================
  2564. // SCALAR RETURN OPS - DEPENDENT
  2565. //==============================================================================================================================
  2566. #define APowD1(a,b) pow(AD1(a),AF1(b))
  2567. #define APowF1(a,b) pow(AF1(a),AF1(b))
  2568. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2569. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2570. //_____________________________________________________________/\_______________________________________________________________
  2571. //==============================================================================================================================
  2572. // VECTOR OPS
  2573. //------------------------------------------------------------------------------------------------------------------------------
  2574. // These are added as needed for production or prototyping, so not necessarily a complete set.
  2575. // They follow a convention of taking in a destination and also returning the destination value to increase utility.
  2576. //==============================================================================================================================
  2577. #ifdef A_DUBL
  2578. AD2 opAAbsD2(outAD2 d,inAD2 a){d=abs(a);return d;}
  2579. AD3 opAAbsD3(outAD3 d,inAD3 a){d=abs(a);return d;}
  2580. AD4 opAAbsD4(outAD4 d,inAD4 a){d=abs(a);return d;}
  2581. //------------------------------------------------------------------------------------------------------------------------------
  2582. AD2 opAAddD2(outAD2 d,inAD2 a,inAD2 b){d=a+b;return d;}
  2583. AD3 opAAddD3(outAD3 d,inAD3 a,inAD3 b){d=a+b;return d;}
  2584. AD4 opAAddD4(outAD4 d,inAD4 a,inAD4 b){d=a+b;return d;}
  2585. //------------------------------------------------------------------------------------------------------------------------------
  2586. AD2 opAAddOneD2(outAD2 d,inAD2 a,AD1 b){d=a+AD2_(b);return d;}
  2587. AD3 opAAddOneD3(outAD3 d,inAD3 a,AD1 b){d=a+AD3_(b);return d;}
  2588. AD4 opAAddOneD4(outAD4 d,inAD4 a,AD1 b){d=a+AD4_(b);return d;}
  2589. //------------------------------------------------------------------------------------------------------------------------------
  2590. AD2 opACpyD2(outAD2 d,inAD2 a){d=a;return d;}
  2591. AD3 opACpyD3(outAD3 d,inAD3 a){d=a;return d;}
  2592. AD4 opACpyD4(outAD4 d,inAD4 a){d=a;return d;}
  2593. //------------------------------------------------------------------------------------------------------------------------------
  2594. AD2 opALerpD2(outAD2 d,inAD2 a,inAD2 b,inAD2 c){d=ALerpD2(a,b,c);return d;}
  2595. AD3 opALerpD3(outAD3 d,inAD3 a,inAD3 b,inAD3 c){d=ALerpD3(a,b,c);return d;}
  2596. AD4 opALerpD4(outAD4 d,inAD4 a,inAD4 b,inAD4 c){d=ALerpD4(a,b,c);return d;}
  2597. //------------------------------------------------------------------------------------------------------------------------------
  2598. AD2 opALerpOneD2(outAD2 d,inAD2 a,inAD2 b,AD1 c){d=ALerpD2(a,b,AD2_(c));return d;}
  2599. AD3 opALerpOneD3(outAD3 d,inAD3 a,inAD3 b,AD1 c){d=ALerpD3(a,b,AD3_(c));return d;}
  2600. AD4 opALerpOneD4(outAD4 d,inAD4 a,inAD4 b,AD1 c){d=ALerpD4(a,b,AD4_(c));return d;}
  2601. //------------------------------------------------------------------------------------------------------------------------------
  2602. AD2 opAMaxD2(outAD2 d,inAD2 a,inAD2 b){d=max(a,b);return d;}
  2603. AD3 opAMaxD3(outAD3 d,inAD3 a,inAD3 b){d=max(a,b);return d;}
  2604. AD4 opAMaxD4(outAD4 d,inAD4 a,inAD4 b){d=max(a,b);return d;}
  2605. //------------------------------------------------------------------------------------------------------------------------------
  2606. AD2 opAMinD2(outAD2 d,inAD2 a,inAD2 b){d=min(a,b);return d;}
  2607. AD3 opAMinD3(outAD3 d,inAD3 a,inAD3 b){d=min(a,b);return d;}
  2608. AD4 opAMinD4(outAD4 d,inAD4 a,inAD4 b){d=min(a,b);return d;}
  2609. //------------------------------------------------------------------------------------------------------------------------------
  2610. AD2 opAMulD2(outAD2 d,inAD2 a,inAD2 b){d=a*b;return d;}
  2611. AD3 opAMulD3(outAD3 d,inAD3 a,inAD3 b){d=a*b;return d;}
  2612. AD4 opAMulD4(outAD4 d,inAD4 a,inAD4 b){d=a*b;return d;}
  2613. //------------------------------------------------------------------------------------------------------------------------------
  2614. AD2 opAMulOneD2(outAD2 d,inAD2 a,AD1 b){d=a*AD2_(b);return d;}
  2615. AD3 opAMulOneD3(outAD3 d,inAD3 a,AD1 b){d=a*AD3_(b);return d;}
  2616. AD4 opAMulOneD4(outAD4 d,inAD4 a,AD1 b){d=a*AD4_(b);return d;}
  2617. //------------------------------------------------------------------------------------------------------------------------------
  2618. AD2 opANegD2(outAD2 d,inAD2 a){d=-a;return d;}
  2619. AD3 opANegD3(outAD3 d,inAD3 a){d=-a;return d;}
  2620. AD4 opANegD4(outAD4 d,inAD4 a){d=-a;return d;}
  2621. //------------------------------------------------------------------------------------------------------------------------------
  2622. AD2 opARcpD2(outAD2 d,inAD2 a){d=ARcpD2(a);return d;}
  2623. AD3 opARcpD3(outAD3 d,inAD3 a){d=ARcpD3(a);return d;}
  2624. AD4 opARcpD4(outAD4 d,inAD4 a){d=ARcpD4(a);return d;}
  2625. #endif
  2626. //==============================================================================================================================
  2627. AF2 opAAbsF2(outAF2 d,inAF2 a){d=abs(a);return d;}
  2628. AF3 opAAbsF3(outAF3 d,inAF3 a){d=abs(a);return d;}
  2629. AF4 opAAbsF4(outAF4 d,inAF4 a){d=abs(a);return d;}
  2630. //------------------------------------------------------------------------------------------------------------------------------
  2631. AF2 opAAddF2(outAF2 d,inAF2 a,inAF2 b){d=a+b;return d;}
  2632. AF3 opAAddF3(outAF3 d,inAF3 a,inAF3 b){d=a+b;return d;}
  2633. AF4 opAAddF4(outAF4 d,inAF4 a,inAF4 b){d=a+b;return d;}
  2634. //------------------------------------------------------------------------------------------------------------------------------
  2635. AF2 opAAddOneF2(outAF2 d,inAF2 a,AF1 b){d=a+AF2_(b);return d;}
  2636. AF3 opAAddOneF3(outAF3 d,inAF3 a,AF1 b){d=a+AF3_(b);return d;}
  2637. AF4 opAAddOneF4(outAF4 d,inAF4 a,AF1 b){d=a+AF4_(b);return d;}
  2638. //------------------------------------------------------------------------------------------------------------------------------
  2639. AF2 opACpyF2(outAF2 d,inAF2 a){d=a;return d;}
  2640. AF3 opACpyF3(outAF3 d,inAF3 a){d=a;return d;}
  2641. AF4 opACpyF4(outAF4 d,inAF4 a){d=a;return d;}
  2642. //------------------------------------------------------------------------------------------------------------------------------
  2643. AF2 opALerpF2(outAF2 d,inAF2 a,inAF2 b,inAF2 c){d=ALerpF2(a,b,c);return d;}
  2644. AF3 opALerpF3(outAF3 d,inAF3 a,inAF3 b,inAF3 c){d=ALerpF3(a,b,c);return d;}
  2645. AF4 opALerpF4(outAF4 d,inAF4 a,inAF4 b,inAF4 c){d=ALerpF4(a,b,c);return d;}
  2646. //------------------------------------------------------------------------------------------------------------------------------
  2647. AF2 opALerpOneF2(outAF2 d,inAF2 a,inAF2 b,AF1 c){d=ALerpF2(a,b,AF2_(c));return d;}
  2648. AF3 opALerpOneF3(outAF3 d,inAF3 a,inAF3 b,AF1 c){d=ALerpF3(a,b,AF3_(c));return d;}
  2649. AF4 opALerpOneF4(outAF4 d,inAF4 a,inAF4 b,AF1 c){d=ALerpF4(a,b,AF4_(c));return d;}
  2650. //------------------------------------------------------------------------------------------------------------------------------
  2651. AF2 opAMaxF2(outAF2 d,inAF2 a,inAF2 b){d=max(a,b);return d;}
  2652. AF3 opAMaxF3(outAF3 d,inAF3 a,inAF3 b){d=max(a,b);return d;}
  2653. AF4 opAMaxF4(outAF4 d,inAF4 a,inAF4 b){d=max(a,b);return d;}
  2654. //------------------------------------------------------------------------------------------------------------------------------
  2655. AF2 opAMinF2(outAF2 d,inAF2 a,inAF2 b){d=min(a,b);return d;}
  2656. AF3 opAMinF3(outAF3 d,inAF3 a,inAF3 b){d=min(a,b);return d;}
  2657. AF4 opAMinF4(outAF4 d,inAF4 a,inAF4 b){d=min(a,b);return d;}
  2658. //------------------------------------------------------------------------------------------------------------------------------
  2659. AF2 opAMulF2(outAF2 d,inAF2 a,inAF2 b){d=a*b;return d;}
  2660. AF3 opAMulF3(outAF3 d,inAF3 a,inAF3 b){d=a*b;return d;}
  2661. AF4 opAMulF4(outAF4 d,inAF4 a,inAF4 b){d=a*b;return d;}
  2662. //------------------------------------------------------------------------------------------------------------------------------
  2663. AF2 opAMulOneF2(outAF2 d,inAF2 a,AF1 b){d=a*AF2_(b);return d;}
  2664. AF3 opAMulOneF3(outAF3 d,inAF3 a,AF1 b){d=a*AF3_(b);return d;}
  2665. AF4 opAMulOneF4(outAF4 d,inAF4 a,AF1 b){d=a*AF4_(b);return d;}
  2666. //------------------------------------------------------------------------------------------------------------------------------
  2667. AF2 opANegF2(outAF2 d,inAF2 a){d=-a;return d;}
  2668. AF3 opANegF3(outAF3 d,inAF3 a){d=-a;return d;}
  2669. AF4 opANegF4(outAF4 d,inAF4 a){d=-a;return d;}
  2670. //------------------------------------------------------------------------------------------------------------------------------
  2671. AF2 opARcpF2(outAF2 d,inAF2 a){d=ARcpF2(a);return d;}
  2672. AF3 opARcpF3(outAF3 d,inAF3 a){d=ARcpF3(a);return d;}
  2673. AF4 opARcpF4(outAF4 d,inAF4 a){d=ARcpF4(a);return d;}
  2674. #endif
  2675. #define FSR_RCAS_F 1
  2676. AU4 con0;
  2677. AF4 FsrRcasLoadF(ASU2 p) { return AF4(texelFetch(source, p, 0)); }
  2678. void FsrRcasInputF(inout AF1 r, inout AF1 g, inout AF1 b) {}
  2679. //_____________________________________________________________/\_______________________________________________________________
  2680. //==============================================================================================================================
  2681. //
  2682. //
  2683. // AMD FidelityFX SUPER RESOLUTION [FSR 1] ::: SPATIAL SCALING & EXTRAS - v1.20210629
  2684. //
  2685. //
  2686. //------------------------------------------------------------------------------------------------------------------------------
  2687. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2688. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2689. //------------------------------------------------------------------------------------------------------------------------------
  2690. // FidelityFX Super Resolution Sample
  2691. //
  2692. // Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
  2693. // Permission is hereby granted, free of charge, to any person obtaining a copy
  2694. // of this software and associated documentation files(the "Software"), to deal
  2695. // in the Software without restriction, including without limitation the rights
  2696. // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
  2697. // copies of the Software, and to permit persons to whom the Software is
  2698. // furnished to do so, subject to the following conditions :
  2699. // The above copyright notice and this permission notice shall be included in
  2700. // all copies or substantial portions of the Software.
  2701. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2702. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2703. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  2704. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2705. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  2706. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  2707. // THE SOFTWARE.
  2708. //------------------------------------------------------------------------------------------------------------------------------
  2709. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2710. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2711. //------------------------------------------------------------------------------------------------------------------------------
  2712. // ABOUT
  2713. // =====
  2714. // FSR is a collection of algorithms relating to generating a higher resolution image.
  2715. // This specific header focuses on single-image non-temporal image scaling, and related tools.
  2716. //
  2717. // The core functions are EASU and RCAS:
  2718. // [EASU] Edge Adaptive Spatial Upsampling ....... 1x to 4x area range spatial scaling, clamped adaptive elliptical filter.
  2719. // [RCAS] Robust Contrast Adaptive Sharpening .... A non-scaling variation on CAS.
  2720. // RCAS needs to be applied after EASU as a separate pass.
  2721. //
  2722. // Optional utility functions are:
  2723. // [LFGA] Linear Film Grain Applicator ........... Tool to apply film grain after scaling.
  2724. // [SRTM] Simple Reversible Tone-Mapper .......... Linear HDR {0 to FP16_MAX} to {0 to 1} and back.
  2725. // [TEPD] Temporal Energy Preserving Dither ...... Temporally energy preserving dithered {0 to 1} linear to gamma 2.0 conversion.
  2726. // See each individual sub-section for inline documentation.
  2727. //------------------------------------------------------------------------------------------------------------------------------
  2728. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2729. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2730. //------------------------------------------------------------------------------------------------------------------------------
  2731. // FUNCTION PERMUTATIONS
  2732. // =====================
  2733. // *F() ..... Single item computation with 32-bit.
  2734. // *H() ..... Single item computation with 16-bit, with packing (aka two 16-bit ops in parallel) when possible.
  2735. // *Hx2() ... Processing two items in parallel with 16-bit, easier packing.
  2736. // Not all interfaces in this file have a *Hx2() form.
  2737. //==============================================================================================================================
  2738. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2739. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2740. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2741. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2742. //_____________________________________________________________/\_______________________________________________________________
  2743. //==============================================================================================================================
  2744. //
  2745. // FSR - [EASU] EDGE ADAPTIVE SPATIAL UPSAMPLING
  2746. //
  2747. //------------------------------------------------------------------------------------------------------------------------------
  2748. // EASU provides a high quality spatial-only scaling at relatively low cost.
  2749. // Meaning EASU is appropiate for laptops and other low-end GPUs.
  2750. // Quality from 1x to 4x area scaling is good.
  2751. //------------------------------------------------------------------------------------------------------------------------------
  2752. // The scalar uses a modified fast approximation to the standard lanczos(size=2) kernel.
  2753. // EASU runs in a single pass, so it applies a directionally and anisotropically adaptive radial lanczos.
  2754. // This is also kept as simple as possible to have minimum runtime.
  2755. //------------------------------------------------------------------------------------------------------------------------------
  2756. // The lanzcos filter has negative lobes, so by itself it will introduce ringing.
  2757. // To remove all ringing, the algorithm uses the nearest 2x2 input texels as a neighborhood,
  2758. // and limits output to the minimum and maximum of that neighborhood.
  2759. //------------------------------------------------------------------------------------------------------------------------------
  2760. // Input image requirements:
  2761. //
  2762. // Color needs to be encoded as 3 channel[red, green, blue](e.g.XYZ not supported)
  2763. // Each channel needs to be in the range[0, 1]
  2764. // Any color primaries are supported
  2765. // Display / tonemapping curve needs to be as if presenting to sRGB display or similar(e.g.Gamma 2.0)
  2766. // There should be no banding in the input
  2767. // There should be no high amplitude noise in the input
  2768. // There should be no noise in the input that is not at input pixel granularity
  2769. // For performance purposes, use 32bpp formats
  2770. //------------------------------------------------------------------------------------------------------------------------------
  2771. // Best to apply EASU at the end of the frame after tonemapping
  2772. // but before film grain or composite of the UI.
  2773. //------------------------------------------------------------------------------------------------------------------------------
  2774. // Example of including this header for D3D HLSL :
  2775. //
  2776. // #define A_GPU 1
  2777. // #define A_HLSL 1
  2778. // #define A_HALF 1
  2779. // #include "ffx_a.h"
  2780. // #define FSR_EASU_H 1
  2781. // #define FSR_RCAS_H 1
  2782. // //declare input callbacks
  2783. // #include "ffx_fsr1.h"
  2784. //
  2785. // Example of including this header for Vulkan GLSL :
  2786. //
  2787. // #define A_GPU 1
  2788. // #define A_GLSL 1
  2789. // #define A_HALF 1
  2790. // #include "ffx_a.h"
  2791. // #define FSR_EASU_H 1
  2792. // #define FSR_RCAS_H 1
  2793. // //declare input callbacks
  2794. // #include "ffx_fsr1.h"
  2795. //
  2796. // Example of including this header for Vulkan HLSL :
  2797. //
  2798. // #define A_GPU 1
  2799. // #define A_HLSL 1
  2800. // #define A_HLSL_6_2 1
  2801. // #define A_NO_16_BIT_CAST 1
  2802. // #define A_HALF 1
  2803. // #include "ffx_a.h"
  2804. // #define FSR_EASU_H 1
  2805. // #define FSR_RCAS_H 1
  2806. // //declare input callbacks
  2807. // #include "ffx_fsr1.h"
  2808. //
  2809. // Example of declaring the required input callbacks for GLSL :
  2810. // The callbacks need to gather4 for each color channel using the specified texture coordinate 'p'.
  2811. // EASU uses gather4 to reduce position computation logic and for free Arrays of Structures to Structures of Arrays conversion.
  2812. //
  2813. // AH4 FsrEasuRH(AF2 p){return AH4(textureGather(sampler2D(tex,sam),p,0));}
  2814. // AH4 FsrEasuGH(AF2 p){return AH4(textureGather(sampler2D(tex,sam),p,1));}
  2815. // AH4 FsrEasuBH(AF2 p){return AH4(textureGather(sampler2D(tex,sam),p,2));}
  2816. // ...
  2817. // The FsrEasuCon function needs to be called from the CPU or GPU to set up constants.
  2818. // The difference in viewport and input image size is there to support Dynamic Resolution Scaling.
  2819. // To use FsrEasuCon() on the CPU, define A_CPU before including ffx_a and ffx_fsr1.
  2820. // Including a GPU example here, the 'con0' through 'con3' values would be stored out to a constant buffer.
  2821. // AU4 con0,con1,con2,con3;
  2822. // FsrEasuCon(con0,con1,con2,con3,
  2823. // 1920.0,1080.0, // Viewport size (top left aligned) in the input image which is to be scaled.
  2824. // 3840.0,2160.0, // The size of the input image.
  2825. // 2560.0,1440.0); // The output resolution.
  2826. //==============================================================================================================================
  2827. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2828. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2829. //_____________________________________________________________/\_______________________________________________________________
  2830. //==============================================================================================================================
  2831. // CONSTANT SETUP
  2832. //==============================================================================================================================
  2833. // Call to setup required constant values (works on CPU or GPU).
  2834. A_STATIC void FsrEasuCon(
  2835. outAU4 con0,
  2836. outAU4 con1,
  2837. outAU4 con2,
  2838. outAU4 con3,
  2839. // This the rendered image resolution being upscaled
  2840. AF1 inputViewportInPixelsX,
  2841. AF1 inputViewportInPixelsY,
  2842. // This is the resolution of the resource containing the input image (useful for dynamic resolution)
  2843. AF1 inputSizeInPixelsX,
  2844. AF1 inputSizeInPixelsY,
  2845. // This is the display resolution which the input image gets upscaled to
  2846. AF1 outputSizeInPixelsX,
  2847. AF1 outputSizeInPixelsY){
  2848. // Output integer position to a pixel position in viewport.
  2849. con0[0]=AU1_AF1(inputViewportInPixelsX*ARcpF1(outputSizeInPixelsX));
  2850. con0[1]=AU1_AF1(inputViewportInPixelsY*ARcpF1(outputSizeInPixelsY));
  2851. con0[2]=AU1_AF1(AF1_(0.5)*inputViewportInPixelsX*ARcpF1(outputSizeInPixelsX)-AF1_(0.5));
  2852. con0[3]=AU1_AF1(AF1_(0.5)*inputViewportInPixelsY*ARcpF1(outputSizeInPixelsY)-AF1_(0.5));
  2853. // Viewport pixel position to normalized image space.
  2854. // This is used to get upper-left of 'F' tap.
  2855. con1[0]=AU1_AF1(ARcpF1(inputSizeInPixelsX));
  2856. con1[1]=AU1_AF1(ARcpF1(inputSizeInPixelsY));
  2857. // Centers of gather4, first offset from upper-left of 'F'.
  2858. // +---+---+
  2859. // | | |
  2860. // +--(0)--+
  2861. // | b | c |
  2862. // +---F---+---+---+
  2863. // | e | f | g | h |
  2864. // +--(1)--+--(2)--+
  2865. // | i | j | k | l |
  2866. // +---+---+---+---+
  2867. // | n | o |
  2868. // +--(3)--+
  2869. // | | |
  2870. // +---+---+
  2871. con1[2]=AU1_AF1(AF1_( 1.0)*ARcpF1(inputSizeInPixelsX));
  2872. con1[3]=AU1_AF1(AF1_(-1.0)*ARcpF1(inputSizeInPixelsY));
  2873. // These are from (0) instead of 'F'.
  2874. con2[0]=AU1_AF1(AF1_(-1.0)*ARcpF1(inputSizeInPixelsX));
  2875. con2[1]=AU1_AF1(AF1_( 2.0)*ARcpF1(inputSizeInPixelsY));
  2876. con2[2]=AU1_AF1(AF1_( 1.0)*ARcpF1(inputSizeInPixelsX));
  2877. con2[3]=AU1_AF1(AF1_( 2.0)*ARcpF1(inputSizeInPixelsY));
  2878. con3[0]=AU1_AF1(AF1_( 0.0)*ARcpF1(inputSizeInPixelsX));
  2879. con3[1]=AU1_AF1(AF1_( 4.0)*ARcpF1(inputSizeInPixelsY));
  2880. con3[2]=con3[3]=0;}
  2881. //If the an offset into the input image resource
  2882. A_STATIC void FsrEasuConOffset(
  2883. outAU4 con0,
  2884. outAU4 con1,
  2885. outAU4 con2,
  2886. outAU4 con3,
  2887. // This the rendered image resolution being upscaled
  2888. AF1 inputViewportInPixelsX,
  2889. AF1 inputViewportInPixelsY,
  2890. // This is the resolution of the resource containing the input image (useful for dynamic resolution)
  2891. AF1 inputSizeInPixelsX,
  2892. AF1 inputSizeInPixelsY,
  2893. // This is the display resolution which the input image gets upscaled to
  2894. AF1 outputSizeInPixelsX,
  2895. AF1 outputSizeInPixelsY,
  2896. // This is the input image offset into the resource containing it (useful for dynamic resolution)
  2897. AF1 inputOffsetInPixelsX,
  2898. AF1 inputOffsetInPixelsY) {
  2899. FsrEasuCon(con0, con1, con2, con3, inputViewportInPixelsX, inputViewportInPixelsY, inputSizeInPixelsX, inputSizeInPixelsY, outputSizeInPixelsX, outputSizeInPixelsY);
  2900. con0[2] = AU1_AF1(AF1_(0.5) * inputViewportInPixelsX * ARcpF1(outputSizeInPixelsX) - AF1_(0.5) + inputOffsetInPixelsX);
  2901. con0[3] = AU1_AF1(AF1_(0.5) * inputViewportInPixelsY * ARcpF1(outputSizeInPixelsY) - AF1_(0.5) + inputOffsetInPixelsY);
  2902. }
  2903. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2904. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2905. //_____________________________________________________________/\_______________________________________________________________
  2906. //==============================================================================================================================
  2907. // NON-PACKED 32-BIT VERSION
  2908. //==============================================================================================================================
  2909. #if defined(A_GPU)&&defined(FSR_EASU_F)
  2910. // Input callback prototypes, need to be implemented by calling shader
  2911. AF4 FsrEasuRF(AF2 p);
  2912. AF4 FsrEasuGF(AF2 p);
  2913. AF4 FsrEasuBF(AF2 p);
  2914. //------------------------------------------------------------------------------------------------------------------------------
  2915. // Filtering for a given tap for the scalar.
  2916. void FsrEasuTapF(
  2917. inout AF3 aC, // Accumulated color, with negative lobe.
  2918. inout AF1 aW, // Accumulated weight.
  2919. AF2 off, // Pixel offset from resolve position to tap.
  2920. AF2 dir, // Gradient direction.
  2921. AF2 len, // Length.
  2922. AF1 lob, // Negative lobe strength.
  2923. AF1 clp, // Clipping point.
  2924. AF3 c){ // Tap color.
  2925. // Rotate offset by direction.
  2926. AF2 v;
  2927. v.x=(off.x*( dir.x))+(off.y*dir.y);
  2928. v.y=(off.x*(-dir.y))+(off.y*dir.x);
  2929. // Anisotropy.
  2930. v*=len;
  2931. // Compute distance^2.
  2932. AF1 d2=v.x*v.x+v.y*v.y;
  2933. // Limit to the window as at corner, 2 taps can easily be outside.
  2934. d2=min(d2,clp);
  2935. // Approximation of lancos2 without sin() or rcp(), or sqrt() to get x.
  2936. // (25/16 * (2/5 * x^2 - 1)^2 - (25/16 - 1)) * (1/4 * x^2 - 1)^2
  2937. // |_______________________________________| |_______________|
  2938. // base window
  2939. // The general form of the 'base' is,
  2940. // (a*(b*x^2-1)^2-(a-1))
  2941. // Where 'a=1/(2*b-b^2)' and 'b' moves around the negative lobe.
  2942. AF1 wB=AF1_(2.0/5.0)*d2+AF1_(-1.0);
  2943. AF1 wA=lob*d2+AF1_(-1.0);
  2944. wB*=wB;
  2945. wA*=wA;
  2946. wB=AF1_(25.0/16.0)*wB+AF1_(-(25.0/16.0-1.0));
  2947. AF1 w=wB*wA;
  2948. // Do weighted average.
  2949. aC+=c*w;aW+=w;}
  2950. //------------------------------------------------------------------------------------------------------------------------------
  2951. // Accumulate direction and length.
  2952. void FsrEasuSetF(
  2953. inout AF2 dir,
  2954. inout AF1 len,
  2955. AF2 pp,
  2956. AP1 biS,AP1 biT,AP1 biU,AP1 biV,
  2957. AF1 lA,AF1 lB,AF1 lC,AF1 lD,AF1 lE){
  2958. // Compute bilinear weight, branches factor out as predicates are compiler time immediates.
  2959. // s t
  2960. // u v
  2961. AF1 w = AF1_(0.0);
  2962. if(biS)w=(AF1_(1.0)-pp.x)*(AF1_(1.0)-pp.y);
  2963. if(biT)w= pp.x *(AF1_(1.0)-pp.y);
  2964. if(biU)w=(AF1_(1.0)-pp.x)* pp.y ;
  2965. if(biV)w= pp.x * pp.y ;
  2966. // Direction is the '+' diff.
  2967. // a
  2968. // b c d
  2969. // e
  2970. // Then takes magnitude from abs average of both sides of 'c'.
  2971. // Length converts gradient reversal to 0, smoothly to non-reversal at 1, shaped, then adding horz and vert terms.
  2972. AF1 dc=lD-lC;
  2973. AF1 cb=lC-lB;
  2974. AF1 lenX=max(abs(dc),abs(cb));
  2975. lenX=APrxLoRcpF1(lenX);
  2976. AF1 dirX=lD-lB;
  2977. dir.x+=dirX*w;
  2978. lenX=ASatF1(abs(dirX)*lenX);
  2979. lenX*=lenX;
  2980. len+=lenX*w;
  2981. // Repeat for the y axis.
  2982. AF1 ec=lE-lC;
  2983. AF1 ca=lC-lA;
  2984. AF1 lenY=max(abs(ec),abs(ca));
  2985. lenY=APrxLoRcpF1(lenY);
  2986. AF1 dirY=lE-lA;
  2987. dir.y+=dirY*w;
  2988. lenY=ASatF1(abs(dirY)*lenY);
  2989. lenY*=lenY;
  2990. len+=lenY*w;}
  2991. //------------------------------------------------------------------------------------------------------------------------------
  2992. void FsrEasuF(
  2993. out AF3 pix,
  2994. AU2 ip, // Integer pixel position in output.
  2995. AU4 con0, // Constants generated by FsrEasuCon().
  2996. AU4 con1,
  2997. AU4 con2,
  2998. AU4 con3){
  2999. //------------------------------------------------------------------------------------------------------------------------------
  3000. // Get position of 'f'.
  3001. AF2 pp=AF2(ip)*AF2_AU2(con0.xy)+AF2_AU2(con0.zw);
  3002. AF2 fp=floor(pp);
  3003. pp-=fp;
  3004. //------------------------------------------------------------------------------------------------------------------------------
  3005. // 12-tap kernel.
  3006. // b c
  3007. // e f g h
  3008. // i j k l
  3009. // n o
  3010. // Gather 4 ordering.
  3011. // a b
  3012. // r g
  3013. // For packed FP16, need either {rg} or {ab} so using the following setup for gather in all versions,
  3014. // a b <- unused (z)
  3015. // r g
  3016. // a b a b
  3017. // r g r g
  3018. // a b
  3019. // r g <- unused (z)
  3020. // Allowing dead-code removal to remove the 'z's.
  3021. AF2 p0=fp*AF2_AU2(con1.xy)+AF2_AU2(con1.zw);
  3022. // These are from p0 to avoid pulling two constants on pre-Navi hardware.
  3023. AF2 p1=p0+AF2_AU2(con2.xy);
  3024. AF2 p2=p0+AF2_AU2(con2.zw);
  3025. AF2 p3=p0+AF2_AU2(con3.xy);
  3026. AF4 bczzR=FsrEasuRF(p0);
  3027. AF4 bczzG=FsrEasuGF(p0);
  3028. AF4 bczzB=FsrEasuBF(p0);
  3029. AF4 ijfeR=FsrEasuRF(p1);
  3030. AF4 ijfeG=FsrEasuGF(p1);
  3031. AF4 ijfeB=FsrEasuBF(p1);
  3032. AF4 klhgR=FsrEasuRF(p2);
  3033. AF4 klhgG=FsrEasuGF(p2);
  3034. AF4 klhgB=FsrEasuBF(p2);
  3035. AF4 zzonR=FsrEasuRF(p3);
  3036. AF4 zzonG=FsrEasuGF(p3);
  3037. AF4 zzonB=FsrEasuBF(p3);
  3038. //------------------------------------------------------------------------------------------------------------------------------
  3039. // Simplest multi-channel approximate luma possible (luma times 2, in 2 FMA/MAD).
  3040. AF4 bczzL=bczzB*AF4_(0.5)+(bczzR*AF4_(0.5)+bczzG);
  3041. AF4 ijfeL=ijfeB*AF4_(0.5)+(ijfeR*AF4_(0.5)+ijfeG);
  3042. AF4 klhgL=klhgB*AF4_(0.5)+(klhgR*AF4_(0.5)+klhgG);
  3043. AF4 zzonL=zzonB*AF4_(0.5)+(zzonR*AF4_(0.5)+zzonG);
  3044. // Rename.
  3045. AF1 bL=bczzL.x;
  3046. AF1 cL=bczzL.y;
  3047. AF1 iL=ijfeL.x;
  3048. AF1 jL=ijfeL.y;
  3049. AF1 fL=ijfeL.z;
  3050. AF1 eL=ijfeL.w;
  3051. AF1 kL=klhgL.x;
  3052. AF1 lL=klhgL.y;
  3053. AF1 hL=klhgL.z;
  3054. AF1 gL=klhgL.w;
  3055. AF1 oL=zzonL.z;
  3056. AF1 nL=zzonL.w;
  3057. // Accumulate for bilinear interpolation.
  3058. AF2 dir=AF2_(0.0);
  3059. AF1 len=AF1_(0.0);
  3060. FsrEasuSetF(dir,len,pp,true, false,false,false,bL,eL,fL,gL,jL);
  3061. FsrEasuSetF(dir,len,pp,false,true ,false,false,cL,fL,gL,hL,kL);
  3062. FsrEasuSetF(dir,len,pp,false,false,true ,false,fL,iL,jL,kL,nL);
  3063. FsrEasuSetF(dir,len,pp,false,false,false,true ,gL,jL,kL,lL,oL);
  3064. //------------------------------------------------------------------------------------------------------------------------------
  3065. // Normalize with approximation, and cleanup close to zero.
  3066. AF2 dir2=dir*dir;
  3067. AF1 dirR=dir2.x+dir2.y;
  3068. AP1 zro=dirR<AF1_(1.0/32768.0);
  3069. dirR=APrxLoRsqF1(dirR);
  3070. dirR=zro?AF1_(1.0):dirR;
  3071. dir.x=zro?AF1_(1.0):dir.x;
  3072. dir*=AF2_(dirR);
  3073. // Transform from {0 to 2} to {0 to 1} range, and shape with square.
  3074. len=len*AF1_(0.5);
  3075. len*=len;
  3076. // Stretch kernel {1.0 vert|horz, to sqrt(2.0) on diagonal}.
  3077. AF1 stretch=(dir.x*dir.x+dir.y*dir.y)*APrxLoRcpF1(max(abs(dir.x),abs(dir.y)));
  3078. // Anisotropic length after rotation,
  3079. // x := 1.0 lerp to 'stretch' on edges
  3080. // y := 1.0 lerp to 2x on edges
  3081. AF2 len2=AF2(AF1_(1.0)+(stretch-AF1_(1.0))*len,AF1_(1.0)+AF1_(-0.5)*len);
  3082. // Based on the amount of 'edge',
  3083. // the window shifts from +/-{sqrt(2.0) to slightly beyond 2.0}.
  3084. AF1 lob=AF1_(0.5)+AF1_((1.0/4.0-0.04)-0.5)*len;
  3085. // Set distance^2 clipping point to the end of the adjustable window.
  3086. AF1 clp=APrxLoRcpF1(lob);
  3087. //------------------------------------------------------------------------------------------------------------------------------
  3088. // Accumulation mixed with min/max of 4 nearest.
  3089. // b c
  3090. // e f g h
  3091. // i j k l
  3092. // n o
  3093. AF3 min4=min(AMin3F3(AF3(ijfeR.z,ijfeG.z,ijfeB.z),AF3(klhgR.w,klhgG.w,klhgB.w),AF3(ijfeR.y,ijfeG.y,ijfeB.y)),
  3094. AF3(klhgR.x,klhgG.x,klhgB.x));
  3095. AF3 max4=max(AMax3F3(AF3(ijfeR.z,ijfeG.z,ijfeB.z),AF3(klhgR.w,klhgG.w,klhgB.w),AF3(ijfeR.y,ijfeG.y,ijfeB.y)),
  3096. AF3(klhgR.x,klhgG.x,klhgB.x));
  3097. // Accumulation.
  3098. AF3 aC=AF3_(0.0);
  3099. AF1 aW=AF1_(0.0);
  3100. FsrEasuTapF(aC,aW,AF2( 0.0,-1.0)-pp,dir,len2,lob,clp,AF3(bczzR.x,bczzG.x,bczzB.x)); // b
  3101. FsrEasuTapF(aC,aW,AF2( 1.0,-1.0)-pp,dir,len2,lob,clp,AF3(bczzR.y,bczzG.y,bczzB.y)); // c
  3102. FsrEasuTapF(aC,aW,AF2(-1.0, 1.0)-pp,dir,len2,lob,clp,AF3(ijfeR.x,ijfeG.x,ijfeB.x)); // i
  3103. FsrEasuTapF(aC,aW,AF2( 0.0, 1.0)-pp,dir,len2,lob,clp,AF3(ijfeR.y,ijfeG.y,ijfeB.y)); // j
  3104. FsrEasuTapF(aC,aW,AF2( 0.0, 0.0)-pp,dir,len2,lob,clp,AF3(ijfeR.z,ijfeG.z,ijfeB.z)); // f
  3105. FsrEasuTapF(aC,aW,AF2(-1.0, 0.0)-pp,dir,len2,lob,clp,AF3(ijfeR.w,ijfeG.w,ijfeB.w)); // e
  3106. FsrEasuTapF(aC,aW,AF2( 1.0, 1.0)-pp,dir,len2,lob,clp,AF3(klhgR.x,klhgG.x,klhgB.x)); // k
  3107. FsrEasuTapF(aC,aW,AF2( 2.0, 1.0)-pp,dir,len2,lob,clp,AF3(klhgR.y,klhgG.y,klhgB.y)); // l
  3108. FsrEasuTapF(aC,aW,AF2( 2.0, 0.0)-pp,dir,len2,lob,clp,AF3(klhgR.z,klhgG.z,klhgB.z)); // h
  3109. FsrEasuTapF(aC,aW,AF2( 1.0, 0.0)-pp,dir,len2,lob,clp,AF3(klhgR.w,klhgG.w,klhgB.w)); // g
  3110. FsrEasuTapF(aC,aW,AF2( 1.0, 2.0)-pp,dir,len2,lob,clp,AF3(zzonR.z,zzonG.z,zzonB.z)); // o
  3111. FsrEasuTapF(aC,aW,AF2( 0.0, 2.0)-pp,dir,len2,lob,clp,AF3(zzonR.w,zzonG.w,zzonB.w)); // n
  3112. //------------------------------------------------------------------------------------------------------------------------------
  3113. // Normalize and dering.
  3114. pix=min(max4,max(min4,aC*AF3_(ARcpF1(aW))));}
  3115. #endif
  3116. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3117. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3118. //_____________________________________________________________/\_______________________________________________________________
  3119. //==============================================================================================================================
  3120. // PACKED 16-BIT VERSION
  3121. //==============================================================================================================================
  3122. #if defined(A_GPU)&&defined(A_HALF)&&defined(FSR_EASU_H)
  3123. // Input callback prototypes, need to be implemented by calling shader
  3124. AH4 FsrEasuRH(AF2 p);
  3125. AH4 FsrEasuGH(AF2 p);
  3126. AH4 FsrEasuBH(AF2 p);
  3127. //------------------------------------------------------------------------------------------------------------------------------
  3128. // This runs 2 taps in parallel.
  3129. void FsrEasuTapH(
  3130. inout AH2 aCR,inout AH2 aCG,inout AH2 aCB,
  3131. inout AH2 aW,
  3132. AH2 offX,AH2 offY,
  3133. AH2 dir,
  3134. AH2 len,
  3135. AH1 lob,
  3136. AH1 clp,
  3137. AH2 cR,AH2 cG,AH2 cB){
  3138. AH2 vX,vY;
  3139. vX=offX* dir.xx +offY*dir.yy;
  3140. vY=offX*(-dir.yy)+offY*dir.xx;
  3141. vX*=len.x;vY*=len.y;
  3142. AH2 d2=vX*vX+vY*vY;
  3143. d2=min(d2,AH2_(clp));
  3144. AH2 wB=AH2_(2.0/5.0)*d2+AH2_(-1.0);
  3145. AH2 wA=AH2_(lob)*d2+AH2_(-1.0);
  3146. wB*=wB;
  3147. wA*=wA;
  3148. wB=AH2_(25.0/16.0)*wB+AH2_(-(25.0/16.0-1.0));
  3149. AH2 w=wB*wA;
  3150. aCR+=cR*w;aCG+=cG*w;aCB+=cB*w;aW+=w;}
  3151. //------------------------------------------------------------------------------------------------------------------------------
  3152. // This runs 2 taps in parallel.
  3153. void FsrEasuSetH(
  3154. inout AH2 dirPX,inout AH2 dirPY,
  3155. inout AH2 lenP,
  3156. AH2 pp,
  3157. AP1 biST,AP1 biUV,
  3158. AH2 lA,AH2 lB,AH2 lC,AH2 lD,AH2 lE){
  3159. AH2 w = AH2_(0.0);
  3160. if(biST)w=(AH2(1.0,0.0)+AH2(-pp.x,pp.x))*AH2_(AH1_(1.0)-pp.y);
  3161. if(biUV)w=(AH2(1.0,0.0)+AH2(-pp.x,pp.x))*AH2_( pp.y);
  3162. // ABS is not free in the packed FP16 path.
  3163. AH2 dc=lD-lC;
  3164. AH2 cb=lC-lB;
  3165. AH2 lenX=max(abs(dc),abs(cb));
  3166. lenX=ARcpH2(lenX);
  3167. AH2 dirX=lD-lB;
  3168. dirPX+=dirX*w;
  3169. lenX=ASatH2(abs(dirX)*lenX);
  3170. lenX*=lenX;
  3171. lenP+=lenX*w;
  3172. AH2 ec=lE-lC;
  3173. AH2 ca=lC-lA;
  3174. AH2 lenY=max(abs(ec),abs(ca));
  3175. lenY=ARcpH2(lenY);
  3176. AH2 dirY=lE-lA;
  3177. dirPY+=dirY*w;
  3178. lenY=ASatH2(abs(dirY)*lenY);
  3179. lenY*=lenY;
  3180. lenP+=lenY*w;}
  3181. //------------------------------------------------------------------------------------------------------------------------------
  3182. void FsrEasuH(
  3183. out AH3 pix,
  3184. AU2 ip,
  3185. AU4 con0,
  3186. AU4 con1,
  3187. AU4 con2,
  3188. AU4 con3){
  3189. //------------------------------------------------------------------------------------------------------------------------------
  3190. AF2 pp=AF2(ip)*AF2_AU2(con0.xy)+AF2_AU2(con0.zw);
  3191. AF2 fp=floor(pp);
  3192. pp-=fp;
  3193. AH2 ppp=AH2(pp);
  3194. //------------------------------------------------------------------------------------------------------------------------------
  3195. AF2 p0=fp*AF2_AU2(con1.xy)+AF2_AU2(con1.zw);
  3196. AF2 p1=p0+AF2_AU2(con2.xy);
  3197. AF2 p2=p0+AF2_AU2(con2.zw);
  3198. AF2 p3=p0+AF2_AU2(con3.xy);
  3199. AH4 bczzR=FsrEasuRH(p0);
  3200. AH4 bczzG=FsrEasuGH(p0);
  3201. AH4 bczzB=FsrEasuBH(p0);
  3202. AH4 ijfeR=FsrEasuRH(p1);
  3203. AH4 ijfeG=FsrEasuGH(p1);
  3204. AH4 ijfeB=FsrEasuBH(p1);
  3205. AH4 klhgR=FsrEasuRH(p2);
  3206. AH4 klhgG=FsrEasuGH(p2);
  3207. AH4 klhgB=FsrEasuBH(p2);
  3208. AH4 zzonR=FsrEasuRH(p3);
  3209. AH4 zzonG=FsrEasuGH(p3);
  3210. AH4 zzonB=FsrEasuBH(p3);
  3211. //------------------------------------------------------------------------------------------------------------------------------
  3212. AH4 bczzL=bczzB*AH4_(0.5)+(bczzR*AH4_(0.5)+bczzG);
  3213. AH4 ijfeL=ijfeB*AH4_(0.5)+(ijfeR*AH4_(0.5)+ijfeG);
  3214. AH4 klhgL=klhgB*AH4_(0.5)+(klhgR*AH4_(0.5)+klhgG);
  3215. AH4 zzonL=zzonB*AH4_(0.5)+(zzonR*AH4_(0.5)+zzonG);
  3216. AH1 bL=bczzL.x;
  3217. AH1 cL=bczzL.y;
  3218. AH1 iL=ijfeL.x;
  3219. AH1 jL=ijfeL.y;
  3220. AH1 fL=ijfeL.z;
  3221. AH1 eL=ijfeL.w;
  3222. AH1 kL=klhgL.x;
  3223. AH1 lL=klhgL.y;
  3224. AH1 hL=klhgL.z;
  3225. AH1 gL=klhgL.w;
  3226. AH1 oL=zzonL.z;
  3227. AH1 nL=zzonL.w;
  3228. // This part is different, accumulating 2 taps in parallel.
  3229. AH2 dirPX=AH2_(0.0);
  3230. AH2 dirPY=AH2_(0.0);
  3231. AH2 lenP=AH2_(0.0);
  3232. FsrEasuSetH(dirPX,dirPY,lenP,ppp,true, false,AH2(bL,cL),AH2(eL,fL),AH2(fL,gL),AH2(gL,hL),AH2(jL,kL));
  3233. FsrEasuSetH(dirPX,dirPY,lenP,ppp,false,true ,AH2(fL,gL),AH2(iL,jL),AH2(jL,kL),AH2(kL,lL),AH2(nL,oL));
  3234. AH2 dir=AH2(dirPX.r+dirPX.g,dirPY.r+dirPY.g);
  3235. AH1 len=lenP.r+lenP.g;
  3236. //------------------------------------------------------------------------------------------------------------------------------
  3237. AH2 dir2=dir*dir;
  3238. AH1 dirR=dir2.x+dir2.y;
  3239. AP1 zro=dirR<AH1_(1.0/32768.0);
  3240. dirR=APrxLoRsqH1(dirR);
  3241. dirR=zro?AH1_(1.0):dirR;
  3242. dir.x=zro?AH1_(1.0):dir.x;
  3243. dir*=AH2_(dirR);
  3244. len=len*AH1_(0.5);
  3245. len*=len;
  3246. AH1 stretch=(dir.x*dir.x+dir.y*dir.y)*APrxLoRcpH1(max(abs(dir.x),abs(dir.y)));
  3247. AH2 len2=AH2(AH1_(1.0)+(stretch-AH1_(1.0))*len,AH1_(1.0)+AH1_(-0.5)*len);
  3248. AH1 lob=AH1_(0.5)+AH1_((1.0/4.0-0.04)-0.5)*len;
  3249. AH1 clp=APrxLoRcpH1(lob);
  3250. //------------------------------------------------------------------------------------------------------------------------------
  3251. // FP16 is different, using packed trick to do min and max in same operation.
  3252. AH2 bothR=max(max(AH2(-ijfeR.z,ijfeR.z),AH2(-klhgR.w,klhgR.w)),max(AH2(-ijfeR.y,ijfeR.y),AH2(-klhgR.x,klhgR.x)));
  3253. AH2 bothG=max(max(AH2(-ijfeG.z,ijfeG.z),AH2(-klhgG.w,klhgG.w)),max(AH2(-ijfeG.y,ijfeG.y),AH2(-klhgG.x,klhgG.x)));
  3254. AH2 bothB=max(max(AH2(-ijfeB.z,ijfeB.z),AH2(-klhgB.w,klhgB.w)),max(AH2(-ijfeB.y,ijfeB.y),AH2(-klhgB.x,klhgB.x)));
  3255. // This part is different for FP16, working pairs of taps at a time.
  3256. AH2 pR=AH2_(0.0);
  3257. AH2 pG=AH2_(0.0);
  3258. AH2 pB=AH2_(0.0);
  3259. AH2 pW=AH2_(0.0);
  3260. FsrEasuTapH(pR,pG,pB,pW,AH2( 0.0, 1.0)-ppp.xx,AH2(-1.0,-1.0)-ppp.yy,dir,len2,lob,clp,bczzR.xy,bczzG.xy,bczzB.xy);
  3261. FsrEasuTapH(pR,pG,pB,pW,AH2(-1.0, 0.0)-ppp.xx,AH2( 1.0, 1.0)-ppp.yy,dir,len2,lob,clp,ijfeR.xy,ijfeG.xy,ijfeB.xy);
  3262. FsrEasuTapH(pR,pG,pB,pW,AH2( 0.0,-1.0)-ppp.xx,AH2( 0.0, 0.0)-ppp.yy,dir,len2,lob,clp,ijfeR.zw,ijfeG.zw,ijfeB.zw);
  3263. FsrEasuTapH(pR,pG,pB,pW,AH2( 1.0, 2.0)-ppp.xx,AH2( 1.0, 1.0)-ppp.yy,dir,len2,lob,clp,klhgR.xy,klhgG.xy,klhgB.xy);
  3264. FsrEasuTapH(pR,pG,pB,pW,AH2( 2.0, 1.0)-ppp.xx,AH2( 0.0, 0.0)-ppp.yy,dir,len2,lob,clp,klhgR.zw,klhgG.zw,klhgB.zw);
  3265. FsrEasuTapH(pR,pG,pB,pW,AH2( 1.0, 0.0)-ppp.xx,AH2( 2.0, 2.0)-ppp.yy,dir,len2,lob,clp,zzonR.zw,zzonG.zw,zzonB.zw);
  3266. AH3 aC=AH3(pR.x+pR.y,pG.x+pG.y,pB.x+pB.y);
  3267. AH1 aW=pW.x+pW.y;
  3268. //------------------------------------------------------------------------------------------------------------------------------
  3269. // Slightly different for FP16 version due to combined min and max.
  3270. pix=min(AH3(bothR.y,bothG.y,bothB.y),max(-AH3(bothR.x,bothG.x,bothB.x),aC*AH3_(ARcpH1(aW))));}
  3271. #endif
  3272. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3273. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3274. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3275. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3276. //_____________________________________________________________/\_______________________________________________________________
  3277. //==============================================================================================================================
  3278. //
  3279. // FSR - [RCAS] ROBUST CONTRAST ADAPTIVE SHARPENING
  3280. //
  3281. //------------------------------------------------------------------------------------------------------------------------------
  3282. // CAS uses a simplified mechanism to convert local contrast into a variable amount of sharpness.
  3283. // RCAS uses a more exact mechanism, solving for the maximum local sharpness possible before clipping.
  3284. // RCAS also has a built in process to limit sharpening of what it detects as possible noise.
  3285. // RCAS sharper does not support scaling, as it should be applied after EASU scaling.
  3286. // Pass EASU output straight into RCAS, no color conversions necessary.
  3287. //------------------------------------------------------------------------------------------------------------------------------
  3288. // RCAS is based on the following logic.
  3289. // RCAS uses a 5 tap filter in a cross pattern (same as CAS),
  3290. // w n
  3291. // w 1 w for taps w m e
  3292. // w s
  3293. // Where 'w' is the negative lobe weight.
  3294. // output = (w*(n+e+w+s)+m)/(4*w+1)
  3295. // RCAS solves for 'w' by seeing where the signal might clip out of the {0 to 1} input range,
  3296. // 0 == (w*(n+e+w+s)+m)/(4*w+1) -> w = -m/(n+e+w+s)
  3297. // 1 == (w*(n+e+w+s)+m)/(4*w+1) -> w = (1-m)/(n+e+w+s-4*1)
  3298. // Then chooses the 'w' which results in no clipping, limits 'w', and multiplies by the 'sharp' amount.
  3299. // This solution above has issues with MSAA input as the steps along the gradient cause edge detection issues.
  3300. // So RCAS uses 4x the maximum and 4x the minimum (depending on equation)in place of the individual taps.
  3301. // As well as switching from 'm' to either the minimum or maximum (depending on side), to help in energy conservation.
  3302. // This stabilizes RCAS.
  3303. // RCAS does a simple highpass which is normalized against the local contrast then shaped,
  3304. // 0.25
  3305. // 0.25 -1 0.25
  3306. // 0.25
  3307. // This is used as a noise detection filter, to reduce the effect of RCAS on grain, and focus on real edges.
  3308. //
  3309. // GLSL example for the required callbacks :
  3310. //
  3311. // AH4 FsrRcasLoadH(ASW2 p){return AH4(imageLoad(imgSrc,ASU2(p)));}
  3312. // void FsrRcasInputH(inout AH1 r,inout AH1 g,inout AH1 b)
  3313. // {
  3314. // //do any simple input color conversions here or leave empty if none needed
  3315. // }
  3316. //
  3317. // FsrRcasCon need to be called from the CPU or GPU to set up constants.
  3318. // Including a GPU example here, the 'con' value would be stored out to a constant buffer.
  3319. //
  3320. // AU4 con;
  3321. // FsrRcasCon(con,
  3322. // 0.0); // The scale is {0.0 := maximum sharpness, to N>0, where N is the number of stops (halving) of the reduction of sharpness}.
  3323. // ---------------
  3324. // RCAS sharpening supports a CAS-like pass-through alpha via,
  3325. // #define FSR_RCAS_PASSTHROUGH_ALPHA 1
  3326. // RCAS also supports a define to enable a more expensive path to avoid some sharpening of noise.
  3327. // Would suggest it is better to apply film grain after RCAS sharpening (and after scaling) instead of using this define,
  3328. // #define FSR_RCAS_DENOISE 1
  3329. //==============================================================================================================================
  3330. // This is set at the limit of providing unnatural results for sharpening.
  3331. #define FSR_RCAS_LIMIT (0.25-(1.0/16.0))
  3332. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3333. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3334. //_____________________________________________________________/\_______________________________________________________________
  3335. //==============================================================================================================================
  3336. // CONSTANT SETUP
  3337. //==============================================================================================================================
  3338. // Call to setup required constant values (works on CPU or GPU).
  3339. A_STATIC void FsrRcasCon(
  3340. outAU4 con,
  3341. // The scale is {0.0 := maximum, to N>0, where N is the number of stops (halving) of the reduction of sharpness}.
  3342. AF1 sharpness){
  3343. // Transform from stops to linear value.
  3344. sharpness=AExp2F1(-sharpness);
  3345. varAF2(hSharp)=initAF2(sharpness,sharpness);
  3346. con[0]=AU1_AF1(sharpness);
  3347. con[1]=AU1_AH2_AF2(hSharp);
  3348. con[2]=0;
  3349. con[3]=0;}
  3350. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3351. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3352. //_____________________________________________________________/\_______________________________________________________________
  3353. //==============================================================================================================================
  3354. // NON-PACKED 32-BIT VERSION
  3355. //==============================================================================================================================
  3356. #if defined(A_GPU)&&defined(FSR_RCAS_F)
  3357. // Input callback prototypes that need to be implemented by calling shader
  3358. AF4 FsrRcasLoadF(ASU2 p);
  3359. void FsrRcasInputF(inout AF1 r,inout AF1 g,inout AF1 b);
  3360. //------------------------------------------------------------------------------------------------------------------------------
  3361. void FsrRcasF(
  3362. out AF1 pixR, // Output values, non-vector so port between RcasFilter() and RcasFilterH() is easy.
  3363. out AF1 pixG,
  3364. out AF1 pixB,
  3365. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  3366. out AF1 pixA,
  3367. #endif
  3368. AU2 ip, // Integer pixel position in output.
  3369. AU4 con){ // Constant generated by RcasSetup().
  3370. // Algorithm uses minimal 3x3 pixel neighborhood.
  3371. // b
  3372. // d e f
  3373. // h
  3374. ASU2 sp=ASU2(ip);
  3375. AF3 b=FsrRcasLoadF(sp+ASU2( 0,-1)).rgb;
  3376. AF3 d=FsrRcasLoadF(sp+ASU2(-1, 0)).rgb;
  3377. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  3378. AF4 ee=FsrRcasLoadF(sp);
  3379. AF3 e=ee.rgb;pixA=ee.a;
  3380. #else
  3381. AF3 e=FsrRcasLoadF(sp).rgb;
  3382. #endif
  3383. AF3 f=FsrRcasLoadF(sp+ASU2( 1, 0)).rgb;
  3384. AF3 h=FsrRcasLoadF(sp+ASU2( 0, 1)).rgb;
  3385. // Rename (32-bit) or regroup (16-bit).
  3386. AF1 bR=b.r;
  3387. AF1 bG=b.g;
  3388. AF1 bB=b.b;
  3389. AF1 dR=d.r;
  3390. AF1 dG=d.g;
  3391. AF1 dB=d.b;
  3392. AF1 eR=e.r;
  3393. AF1 eG=e.g;
  3394. AF1 eB=e.b;
  3395. AF1 fR=f.r;
  3396. AF1 fG=f.g;
  3397. AF1 fB=f.b;
  3398. AF1 hR=h.r;
  3399. AF1 hG=h.g;
  3400. AF1 hB=h.b;
  3401. // Run optional input transform.
  3402. FsrRcasInputF(bR,bG,bB);
  3403. FsrRcasInputF(dR,dG,dB);
  3404. FsrRcasInputF(eR,eG,eB);
  3405. FsrRcasInputF(fR,fG,fB);
  3406. FsrRcasInputF(hR,hG,hB);
  3407. // Luma times 2.
  3408. AF1 bL=bB*AF1_(0.5)+(bR*AF1_(0.5)+bG);
  3409. AF1 dL=dB*AF1_(0.5)+(dR*AF1_(0.5)+dG);
  3410. AF1 eL=eB*AF1_(0.5)+(eR*AF1_(0.5)+eG);
  3411. AF1 fL=fB*AF1_(0.5)+(fR*AF1_(0.5)+fG);
  3412. AF1 hL=hB*AF1_(0.5)+(hR*AF1_(0.5)+hG);
  3413. // Noise detection.
  3414. AF1 nz=AF1_(0.25)*bL+AF1_(0.25)*dL+AF1_(0.25)*fL+AF1_(0.25)*hL-eL;
  3415. nz=ASatF1(abs(nz)*APrxMedRcpF1(AMax3F1(AMax3F1(bL,dL,eL),fL,hL)-AMin3F1(AMin3F1(bL,dL,eL),fL,hL)));
  3416. nz=AF1_(-0.5)*nz+AF1_(1.0);
  3417. // Min and max of ring.
  3418. AF1 mn4R=min(AMin3F1(bR,dR,fR),hR);
  3419. AF1 mn4G=min(AMin3F1(bG,dG,fG),hG);
  3420. AF1 mn4B=min(AMin3F1(bB,dB,fB),hB);
  3421. AF1 mx4R=max(AMax3F1(bR,dR,fR),hR);
  3422. AF1 mx4G=max(AMax3F1(bG,dG,fG),hG);
  3423. AF1 mx4B=max(AMax3F1(bB,dB,fB),hB);
  3424. // Immediate constants for peak range.
  3425. AF2 peakC=AF2(1.0,-1.0*4.0);
  3426. // Limiters, these need to be high precision RCPs.
  3427. AF1 hitMinR=min(mn4R,eR)*ARcpF1(AF1_(4.0)*mx4R);
  3428. AF1 hitMinG=min(mn4G,eG)*ARcpF1(AF1_(4.0)*mx4G);
  3429. AF1 hitMinB=min(mn4B,eB)*ARcpF1(AF1_(4.0)*mx4B);
  3430. AF1 hitMaxR=(peakC.x-max(mx4R,eR))*ARcpF1(AF1_(4.0)*mn4R+peakC.y);
  3431. AF1 hitMaxG=(peakC.x-max(mx4G,eG))*ARcpF1(AF1_(4.0)*mn4G+peakC.y);
  3432. AF1 hitMaxB=(peakC.x-max(mx4B,eB))*ARcpF1(AF1_(4.0)*mn4B+peakC.y);
  3433. AF1 lobeR=max(-hitMinR,hitMaxR);
  3434. AF1 lobeG=max(-hitMinG,hitMaxG);
  3435. AF1 lobeB=max(-hitMinB,hitMaxB);
  3436. AF1 lobe=max(AF1_(-FSR_RCAS_LIMIT),min(AMax3F1(lobeR,lobeG,lobeB),AF1_(0.0)))*AF1_AU1(con.x);
  3437. // Apply noise removal.
  3438. #ifdef FSR_RCAS_DENOISE
  3439. lobe*=nz;
  3440. #endif
  3441. // Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.
  3442. AF1 rcpL=APrxMedRcpF1(AF1_(4.0)*lobe+AF1_(1.0));
  3443. pixR=(lobe*bR+lobe*dR+lobe*hR+lobe*fR+eR)*rcpL;
  3444. pixG=(lobe*bG+lobe*dG+lobe*hG+lobe*fG+eG)*rcpL;
  3445. pixB=(lobe*bB+lobe*dB+lobe*hB+lobe*fB+eB)*rcpL;
  3446. return;}
  3447. #endif
  3448. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3449. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3450. //_____________________________________________________________/\_______________________________________________________________
  3451. //==============================================================================================================================
  3452. // NON-PACKED 16-BIT VERSION
  3453. //==============================================================================================================================
  3454. #if defined(A_GPU)&&defined(A_HALF)&&defined(FSR_RCAS_H)
  3455. // Input callback prototypes that need to be implemented by calling shader
  3456. AH4 FsrRcasLoadH(ASW2 p);
  3457. void FsrRcasInputH(inout AH1 r,inout AH1 g,inout AH1 b);
  3458. //------------------------------------------------------------------------------------------------------------------------------
  3459. void FsrRcasH(
  3460. out AH1 pixR, // Output values, non-vector so port between RcasFilter() and RcasFilterH() is easy.
  3461. out AH1 pixG,
  3462. out AH1 pixB,
  3463. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  3464. out AH1 pixA,
  3465. #endif
  3466. AU2 ip, // Integer pixel position in output.
  3467. AU4 con){ // Constant generated by RcasSetup().
  3468. // Sharpening algorithm uses minimal 3x3 pixel neighborhood.
  3469. // b
  3470. // d e f
  3471. // h
  3472. ASW2 sp=ASW2(ip);
  3473. AH3 b=FsrRcasLoadH(sp+ASW2( 0,-1)).rgb;
  3474. AH3 d=FsrRcasLoadH(sp+ASW2(-1, 0)).rgb;
  3475. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  3476. AH4 ee=FsrRcasLoadH(sp);
  3477. AH3 e=ee.rgb;pixA=ee.a;
  3478. #else
  3479. AH3 e=FsrRcasLoadH(sp).rgb;
  3480. #endif
  3481. AH3 f=FsrRcasLoadH(sp+ASW2( 1, 0)).rgb;
  3482. AH3 h=FsrRcasLoadH(sp+ASW2( 0, 1)).rgb;
  3483. // Rename (32-bit) or regroup (16-bit).
  3484. AH1 bR=b.r;
  3485. AH1 bG=b.g;
  3486. AH1 bB=b.b;
  3487. AH1 dR=d.r;
  3488. AH1 dG=d.g;
  3489. AH1 dB=d.b;
  3490. AH1 eR=e.r;
  3491. AH1 eG=e.g;
  3492. AH1 eB=e.b;
  3493. AH1 fR=f.r;
  3494. AH1 fG=f.g;
  3495. AH1 fB=f.b;
  3496. AH1 hR=h.r;
  3497. AH1 hG=h.g;
  3498. AH1 hB=h.b;
  3499. // Run optional input transform.
  3500. FsrRcasInputH(bR,bG,bB);
  3501. FsrRcasInputH(dR,dG,dB);
  3502. FsrRcasInputH(eR,eG,eB);
  3503. FsrRcasInputH(fR,fG,fB);
  3504. FsrRcasInputH(hR,hG,hB);
  3505. // Luma times 2.
  3506. AH1 bL=bB*AH1_(0.5)+(bR*AH1_(0.5)+bG);
  3507. AH1 dL=dB*AH1_(0.5)+(dR*AH1_(0.5)+dG);
  3508. AH1 eL=eB*AH1_(0.5)+(eR*AH1_(0.5)+eG);
  3509. AH1 fL=fB*AH1_(0.5)+(fR*AH1_(0.5)+fG);
  3510. AH1 hL=hB*AH1_(0.5)+(hR*AH1_(0.5)+hG);
  3511. // Noise detection.
  3512. AH1 nz=AH1_(0.25)*bL+AH1_(0.25)*dL+AH1_(0.25)*fL+AH1_(0.25)*hL-eL;
  3513. nz=ASatH1(abs(nz)*APrxMedRcpH1(AMax3H1(AMax3H1(bL,dL,eL),fL,hL)-AMin3H1(AMin3H1(bL,dL,eL),fL,hL)));
  3514. nz=AH1_(-0.5)*nz+AH1_(1.0);
  3515. // Min and max of ring.
  3516. AH1 mn4R=min(AMin3H1(bR,dR,fR),hR);
  3517. AH1 mn4G=min(AMin3H1(bG,dG,fG),hG);
  3518. AH1 mn4B=min(AMin3H1(bB,dB,fB),hB);
  3519. AH1 mx4R=max(AMax3H1(bR,dR,fR),hR);
  3520. AH1 mx4G=max(AMax3H1(bG,dG,fG),hG);
  3521. AH1 mx4B=max(AMax3H1(bB,dB,fB),hB);
  3522. // Immediate constants for peak range.
  3523. AH2 peakC=AH2(1.0,-1.0*4.0);
  3524. // Limiters, these need to be high precision RCPs.
  3525. AH1 hitMinR=min(mn4R,eR)*ARcpH1(AH1_(4.0)*mx4R);
  3526. AH1 hitMinG=min(mn4G,eG)*ARcpH1(AH1_(4.0)*mx4G);
  3527. AH1 hitMinB=min(mn4B,eB)*ARcpH1(AH1_(4.0)*mx4B);
  3528. AH1 hitMaxR=(peakC.x-max(mx4R,eR))*ARcpH1(AH1_(4.0)*mn4R+peakC.y);
  3529. AH1 hitMaxG=(peakC.x-max(mx4G,eG))*ARcpH1(AH1_(4.0)*mn4G+peakC.y);
  3530. AH1 hitMaxB=(peakC.x-max(mx4B,eB))*ARcpH1(AH1_(4.0)*mn4B+peakC.y);
  3531. AH1 lobeR=max(-hitMinR,hitMaxR);
  3532. AH1 lobeG=max(-hitMinG,hitMaxG);
  3533. AH1 lobeB=max(-hitMinB,hitMaxB);
  3534. AH1 lobe=max(AH1_(-FSR_RCAS_LIMIT),min(AMax3H1(lobeR,lobeG,lobeB),AH1_(0.0)))*AH2_AU1(con.y).x;
  3535. // Apply noise removal.
  3536. #ifdef FSR_RCAS_DENOISE
  3537. lobe*=nz;
  3538. #endif
  3539. // Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.
  3540. AH1 rcpL=APrxMedRcpH1(AH1_(4.0)*lobe+AH1_(1.0));
  3541. pixR=(lobe*bR+lobe*dR+lobe*hR+lobe*fR+eR)*rcpL;
  3542. pixG=(lobe*bG+lobe*dG+lobe*hG+lobe*fG+eG)*rcpL;
  3543. pixB=(lobe*bB+lobe*dB+lobe*hB+lobe*fB+eB)*rcpL;}
  3544. #endif
  3545. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3546. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3547. //_____________________________________________________________/\_______________________________________________________________
  3548. //==============================================================================================================================
  3549. // PACKED 16-BIT VERSION
  3550. //==============================================================================================================================
  3551. #if defined(A_GPU)&&defined(A_HALF)&&defined(FSR_RCAS_HX2)
  3552. // Input callback prototypes that need to be implemented by the calling shader
  3553. AH4 FsrRcasLoadHx2(ASW2 p);
  3554. void FsrRcasInputHx2(inout AH2 r,inout AH2 g,inout AH2 b);
  3555. //------------------------------------------------------------------------------------------------------------------------------
  3556. // Can be used to convert from packed Structures of Arrays to Arrays of Structures for store.
  3557. void FsrRcasDepackHx2(out AH4 pix0,out AH4 pix1,AH2 pixR,AH2 pixG,AH2 pixB){
  3558. #ifdef A_HLSL
  3559. // Invoke a slower path for DX only, since it won't allow uninitialized values.
  3560. pix0.a=pix1.a=0.0;
  3561. #endif
  3562. pix0.rgb=AH3(pixR.x,pixG.x,pixB.x);
  3563. pix1.rgb=AH3(pixR.y,pixG.y,pixB.y);}
  3564. //------------------------------------------------------------------------------------------------------------------------------
  3565. void FsrRcasHx2(
  3566. // Output values are for 2 8x8 tiles in a 16x8 region.
  3567. // pix<R,G,B>.x = left 8x8 tile
  3568. // pix<R,G,B>.y = right 8x8 tile
  3569. // This enables later processing to easily be packed as well.
  3570. out AH2 pixR,
  3571. out AH2 pixG,
  3572. out AH2 pixB,
  3573. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  3574. out AH2 pixA,
  3575. #endif
  3576. AU2 ip, // Integer pixel position in output.
  3577. AU4 con){ // Constant generated by RcasSetup().
  3578. // No scaling algorithm uses minimal 3x3 pixel neighborhood.
  3579. ASW2 sp0=ASW2(ip);
  3580. AH3 b0=FsrRcasLoadHx2(sp0+ASW2( 0,-1)).rgb;
  3581. AH3 d0=FsrRcasLoadHx2(sp0+ASW2(-1, 0)).rgb;
  3582. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  3583. AH4 ee0=FsrRcasLoadHx2(sp0);
  3584. AH3 e0=ee0.rgb;pixA.r=ee0.a;
  3585. #else
  3586. AH3 e0=FsrRcasLoadHx2(sp0).rgb;
  3587. #endif
  3588. AH3 f0=FsrRcasLoadHx2(sp0+ASW2( 1, 0)).rgb;
  3589. AH3 h0=FsrRcasLoadHx2(sp0+ASW2( 0, 1)).rgb;
  3590. ASW2 sp1=sp0+ASW2(8,0);
  3591. AH3 b1=FsrRcasLoadHx2(sp1+ASW2( 0,-1)).rgb;
  3592. AH3 d1=FsrRcasLoadHx2(sp1+ASW2(-1, 0)).rgb;
  3593. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  3594. AH4 ee1=FsrRcasLoadHx2(sp1);
  3595. AH3 e1=ee1.rgb;pixA.g=ee1.a;
  3596. #else
  3597. AH3 e1=FsrRcasLoadHx2(sp1).rgb;
  3598. #endif
  3599. AH3 f1=FsrRcasLoadHx2(sp1+ASW2( 1, 0)).rgb;
  3600. AH3 h1=FsrRcasLoadHx2(sp1+ASW2( 0, 1)).rgb;
  3601. // Arrays of Structures to Structures of Arrays conversion.
  3602. AH2 bR=AH2(b0.r,b1.r);
  3603. AH2 bG=AH2(b0.g,b1.g);
  3604. AH2 bB=AH2(b0.b,b1.b);
  3605. AH2 dR=AH2(d0.r,d1.r);
  3606. AH2 dG=AH2(d0.g,d1.g);
  3607. AH2 dB=AH2(d0.b,d1.b);
  3608. AH2 eR=AH2(e0.r,e1.r);
  3609. AH2 eG=AH2(e0.g,e1.g);
  3610. AH2 eB=AH2(e0.b,e1.b);
  3611. AH2 fR=AH2(f0.r,f1.r);
  3612. AH2 fG=AH2(f0.g,f1.g);
  3613. AH2 fB=AH2(f0.b,f1.b);
  3614. AH2 hR=AH2(h0.r,h1.r);
  3615. AH2 hG=AH2(h0.g,h1.g);
  3616. AH2 hB=AH2(h0.b,h1.b);
  3617. // Run optional input transform.
  3618. FsrRcasInputHx2(bR,bG,bB);
  3619. FsrRcasInputHx2(dR,dG,dB);
  3620. FsrRcasInputHx2(eR,eG,eB);
  3621. FsrRcasInputHx2(fR,fG,fB);
  3622. FsrRcasInputHx2(hR,hG,hB);
  3623. // Luma times 2.
  3624. AH2 bL=bB*AH2_(0.5)+(bR*AH2_(0.5)+bG);
  3625. AH2 dL=dB*AH2_(0.5)+(dR*AH2_(0.5)+dG);
  3626. AH2 eL=eB*AH2_(0.5)+(eR*AH2_(0.5)+eG);
  3627. AH2 fL=fB*AH2_(0.5)+(fR*AH2_(0.5)+fG);
  3628. AH2 hL=hB*AH2_(0.5)+(hR*AH2_(0.5)+hG);
  3629. // Noise detection.
  3630. AH2 nz=AH2_(0.25)*bL+AH2_(0.25)*dL+AH2_(0.25)*fL+AH2_(0.25)*hL-eL;
  3631. nz=ASatH2(abs(nz)*APrxMedRcpH2(AMax3H2(AMax3H2(bL,dL,eL),fL,hL)-AMin3H2(AMin3H2(bL,dL,eL),fL,hL)));
  3632. nz=AH2_(-0.5)*nz+AH2_(1.0);
  3633. // Min and max of ring.
  3634. AH2 mn4R=min(AMin3H2(bR,dR,fR),hR);
  3635. AH2 mn4G=min(AMin3H2(bG,dG,fG),hG);
  3636. AH2 mn4B=min(AMin3H2(bB,dB,fB),hB);
  3637. AH2 mx4R=max(AMax3H2(bR,dR,fR),hR);
  3638. AH2 mx4G=max(AMax3H2(bG,dG,fG),hG);
  3639. AH2 mx4B=max(AMax3H2(bB,dB,fB),hB);
  3640. // Immediate constants for peak range.
  3641. AH2 peakC=AH2(1.0,-1.0*4.0);
  3642. // Limiters, these need to be high precision RCPs.
  3643. AH2 hitMinR=min(mn4R,eR)*ARcpH2(AH2_(4.0)*mx4R);
  3644. AH2 hitMinG=min(mn4G,eG)*ARcpH2(AH2_(4.0)*mx4G);
  3645. AH2 hitMinB=min(mn4B,eB)*ARcpH2(AH2_(4.0)*mx4B);
  3646. AH2 hitMaxR=(peakC.x-max(mx4R,eR))*ARcpH2(AH2_(4.0)*mn4R+peakC.y);
  3647. AH2 hitMaxG=(peakC.x-max(mx4G,eG))*ARcpH2(AH2_(4.0)*mn4G+peakC.y);
  3648. AH2 hitMaxB=(peakC.x-max(mx4B,eB))*ARcpH2(AH2_(4.0)*mn4B+peakC.y);
  3649. AH2 lobeR=max(-hitMinR,hitMaxR);
  3650. AH2 lobeG=max(-hitMinG,hitMaxG);
  3651. AH2 lobeB=max(-hitMinB,hitMaxB);
  3652. AH2 lobe=max(AH2_(-FSR_RCAS_LIMIT),min(AMax3H2(lobeR,lobeG,lobeB),AH2_(0.0)))*AH2_(AH2_AU1(con.y).x);
  3653. // Apply noise removal.
  3654. #ifdef FSR_RCAS_DENOISE
  3655. lobe*=nz;
  3656. #endif
  3657. // Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.
  3658. AH2 rcpL=APrxMedRcpH2(AH2_(4.0)*lobe+AH2_(1.0));
  3659. pixR=(lobe*bR+lobe*dR+lobe*hR+lobe*fR+eR)*rcpL;
  3660. pixG=(lobe*bG+lobe*dG+lobe*hG+lobe*fG+eG)*rcpL;
  3661. pixB=(lobe*bB+lobe*dB+lobe*hB+lobe*fB+eB)*rcpL;}
  3662. #endif
  3663. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3664. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3665. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3666. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3667. //_____________________________________________________________/\_______________________________________________________________
  3668. //==============================================================================================================================
  3669. //
  3670. // FSR - [LFGA] LINEAR FILM GRAIN APPLICATOR
  3671. //
  3672. //------------------------------------------------------------------------------------------------------------------------------
  3673. // Adding output-resolution film grain after scaling is a good way to mask both rendering and scaling artifacts.
  3674. // Suggest using tiled blue noise as film grain input, with peak noise frequency set for a specific look and feel.
  3675. // The 'Lfga*()' functions provide a convenient way to introduce grain.
  3676. // These functions limit grain based on distance to signal limits.
  3677. // This is done so that the grain is temporally energy preserving, and thus won't modify image tonality.
  3678. // Grain application should be done in a linear colorspace.
  3679. // The grain should be temporally changing, but have a temporal sum per pixel that adds to zero (non-biased).
  3680. //------------------------------------------------------------------------------------------------------------------------------
  3681. // Usage,
  3682. // FsrLfga*(
  3683. // color, // In/out linear colorspace color {0 to 1} ranged.
  3684. // grain, // Per pixel grain texture value {-0.5 to 0.5} ranged, input is 3-channel to support colored grain.
  3685. // amount); // Amount of grain (0 to 1} ranged.
  3686. //------------------------------------------------------------------------------------------------------------------------------
  3687. // Example if grain texture is monochrome: 'FsrLfgaF(color,AF3_(grain),amount)'
  3688. //==============================================================================================================================
  3689. #if defined(A_GPU)
  3690. // Maximum grain is the minimum distance to the signal limit.
  3691. void FsrLfgaF(inout AF3 c,AF3 t,AF1 a){c+=(t*AF3_(a))*min(AF3_(1.0)-c,c);}
  3692. #endif
  3693. //==============================================================================================================================
  3694. #if defined(A_GPU)&&defined(A_HALF)
  3695. // Half precision version (slower).
  3696. void FsrLfgaH(inout AH3 c,AH3 t,AH1 a){c+=(t*AH3_(a))*min(AH3_(1.0)-c,c);}
  3697. //------------------------------------------------------------------------------------------------------------------------------
  3698. // Packed half precision version (faster).
  3699. void FsrLfgaHx2(inout AH2 cR,inout AH2 cG,inout AH2 cB,AH2 tR,AH2 tG,AH2 tB,AH1 a){
  3700. cR+=(tR*AH2_(a))*min(AH2_(1.0)-cR,cR);cG+=(tG*AH2_(a))*min(AH2_(1.0)-cG,cG);cB+=(tB*AH2_(a))*min(AH2_(1.0)-cB,cB);}
  3701. #endif
  3702. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3703. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3704. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3705. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3706. //_____________________________________________________________/\_______________________________________________________________
  3707. //==============================================================================================================================
  3708. //
  3709. // FSR - [SRTM] SIMPLE REVERSIBLE TONE-MAPPER
  3710. //
  3711. //------------------------------------------------------------------------------------------------------------------------------
  3712. // This provides a way to take linear HDR color {0 to FP16_MAX} and convert it into a temporary {0 to 1} ranged post-tonemapped linear.
  3713. // The tonemapper preserves RGB ratio, which helps maintain HDR color bleed during filtering.
  3714. //------------------------------------------------------------------------------------------------------------------------------
  3715. // Reversible tonemapper usage,
  3716. // FsrSrtm*(color); // {0 to FP16_MAX} converted to {0 to 1}.
  3717. // FsrSrtmInv*(color); // {0 to 1} converted into {0 to 32768, output peak safe for FP16}.
  3718. //==============================================================================================================================
  3719. #if defined(A_GPU)
  3720. void FsrSrtmF(inout AF3 c){c*=AF3_(ARcpF1(AMax3F1(c.r,c.g,c.b)+AF1_(1.0)));}
  3721. // The extra max solves the c=1.0 case (which is a /0).
  3722. void FsrSrtmInvF(inout AF3 c){c*=AF3_(ARcpF1(max(AF1_(1.0/32768.0),AF1_(1.0)-AMax3F1(c.r,c.g,c.b))));}
  3723. #endif
  3724. //==============================================================================================================================
  3725. #if defined(A_GPU)&&defined(A_HALF)
  3726. void FsrSrtmH(inout AH3 c){c*=AH3_(ARcpH1(AMax3H1(c.r,c.g,c.b)+AH1_(1.0)));}
  3727. void FsrSrtmInvH(inout AH3 c){c*=AH3_(ARcpH1(max(AH1_(1.0/32768.0),AH1_(1.0)-AMax3H1(c.r,c.g,c.b))));}
  3728. //------------------------------------------------------------------------------------------------------------------------------
  3729. void FsrSrtmHx2(inout AH2 cR,inout AH2 cG,inout AH2 cB){
  3730. AH2 rcp=ARcpH2(AMax3H2(cR,cG,cB)+AH2_(1.0));cR*=rcp;cG*=rcp;cB*=rcp;}
  3731. void FsrSrtmInvHx2(inout AH2 cR,inout AH2 cG,inout AH2 cB){
  3732. AH2 rcp=ARcpH2(max(AH2_(1.0/32768.0),AH2_(1.0)-AMax3H2(cR,cG,cB)));cR*=rcp;cG*=rcp;cB*=rcp;}
  3733. #endif
  3734. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3735. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3736. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3737. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3738. //_____________________________________________________________/\_______________________________________________________________
  3739. //==============================================================================================================================
  3740. //
  3741. // FSR - [TEPD] TEMPORAL ENERGY PRESERVING DITHER
  3742. //
  3743. //------------------------------------------------------------------------------------------------------------------------------
  3744. // Temporally energy preserving dithered {0 to 1} linear to gamma 2.0 conversion.
  3745. // Gamma 2.0 is used so that the conversion back to linear is just to square the color.
  3746. // The conversion comes in 8-bit and 10-bit modes, designed for output to 8-bit UNORM or 10:10:10:2 respectively.
  3747. // Given good non-biased temporal blue noise as dither input,
  3748. // the output dither will temporally conserve energy.
  3749. // This is done by choosing the linear nearest step point instead of perceptual nearest.
  3750. // See code below for details.
  3751. //------------------------------------------------------------------------------------------------------------------------------
  3752. // DX SPEC RULES FOR FLOAT->UNORM 8-BIT CONVERSION
  3753. // ===============================================
  3754. // - Output is 'uint(floor(saturate(n)*255.0+0.5))'.
  3755. // - Thus rounding is to nearest.
  3756. // - NaN gets converted to zero.
  3757. // - INF is clamped to {0.0 to 1.0}.
  3758. //==============================================================================================================================
  3759. #if defined(A_GPU)
  3760. // Hand tuned integer position to dither value, with more values than simple checkerboard.
  3761. // Only 32-bit has enough precision for this compddation.
  3762. // Output is {0 to <1}.
  3763. AF1 FsrTepdDitF(AU2 p,AU1 f){
  3764. AF1 x=AF1_(p.x+f);
  3765. AF1 y=AF1_(p.y);
  3766. // The 1.61803 golden ratio.
  3767. AF1 a=AF1_((1.0+sqrt(5.0))/2.0);
  3768. // Number designed to provide a good visual pattern.
  3769. AF1 b=AF1_(1.0/3.69);
  3770. x=x*a+(y*b);
  3771. return AFractF1(x);}
  3772. //------------------------------------------------------------------------------------------------------------------------------
  3773. // This version is 8-bit gamma 2.0.
  3774. // The 'c' input is {0 to 1}.
  3775. // Output is {0 to 1} ready for image store.
  3776. void FsrTepdC8F(inout AF3 c,AF1 dit){
  3777. AF3 n=sqrt(c);
  3778. n=floor(n*AF3_(255.0))*AF3_(1.0/255.0);
  3779. AF3 a=n*n;
  3780. AF3 b=n+AF3_(1.0/255.0);b=b*b;
  3781. // Ratio of 'a' to 'b' required to produce 'c'.
  3782. // APrxLoRcpF1() won't work here (at least for very high dynamic ranges).
  3783. // APrxMedRcpF1() is an IADD,FMA,MUL.
  3784. AF3 r=(c-b)*APrxMedRcpF3(a-b);
  3785. // Use the ratio as a cutoff to choose 'a' or 'b'.
  3786. // AGtZeroF1() is a MUL.
  3787. c=ASatF3(n+AGtZeroF3(AF3_(dit)-r)*AF3_(1.0/255.0));}
  3788. //------------------------------------------------------------------------------------------------------------------------------
  3789. // This version is 10-bit gamma 2.0.
  3790. // The 'c' input is {0 to 1}.
  3791. // Output is {0 to 1} ready for image store.
  3792. void FsrTepdC10F(inout AF3 c,AF1 dit){
  3793. AF3 n=sqrt(c);
  3794. n=floor(n*AF3_(1023.0))*AF3_(1.0/1023.0);
  3795. AF3 a=n*n;
  3796. AF3 b=n+AF3_(1.0/1023.0);b=b*b;
  3797. AF3 r=(c-b)*APrxMedRcpF3(a-b);
  3798. c=ASatF3(n+AGtZeroF3(AF3_(dit)-r)*AF3_(1.0/1023.0));}
  3799. #endif
  3800. //==============================================================================================================================
  3801. #if defined(A_GPU)&&defined(A_HALF)
  3802. AH1 FsrTepdDitH(AU2 p,AU1 f){
  3803. AF1 x=AF1_(p.x+f);
  3804. AF1 y=AF1_(p.y);
  3805. AF1 a=AF1_((1.0+sqrt(5.0))/2.0);
  3806. AF1 b=AF1_(1.0/3.69);
  3807. x=x*a+(y*b);
  3808. return AH1(AFractF1(x));}
  3809. //------------------------------------------------------------------------------------------------------------------------------
  3810. void FsrTepdC8H(inout AH3 c,AH1 dit){
  3811. AH3 n=sqrt(c);
  3812. n=floor(n*AH3_(255.0))*AH3_(1.0/255.0);
  3813. AH3 a=n*n;
  3814. AH3 b=n+AH3_(1.0/255.0);b=b*b;
  3815. AH3 r=(c-b)*APrxMedRcpH3(a-b);
  3816. c=ASatH3(n+AGtZeroH3(AH3_(dit)-r)*AH3_(1.0/255.0));}
  3817. //------------------------------------------------------------------------------------------------------------------------------
  3818. void FsrTepdC10H(inout AH3 c,AH1 dit){
  3819. AH3 n=sqrt(c);
  3820. n=floor(n*AH3_(1023.0))*AH3_(1.0/1023.0);
  3821. AH3 a=n*n;
  3822. AH3 b=n+AH3_(1.0/1023.0);b=b*b;
  3823. AH3 r=(c-b)*APrxMedRcpH3(a-b);
  3824. c=ASatH3(n+AGtZeroH3(AH3_(dit)-r)*AH3_(1.0/1023.0));}
  3825. //==============================================================================================================================
  3826. // This computes dither for positions 'p' and 'p+{8,0}'.
  3827. AH2 FsrTepdDitHx2(AU2 p,AU1 f){
  3828. AF2 x;
  3829. x.x=AF1_(p.x+f);
  3830. x.y=x.x+AF1_(8.0);
  3831. AF1 y=AF1_(p.y);
  3832. AF1 a=AF1_((1.0+sqrt(5.0))/2.0);
  3833. AF1 b=AF1_(1.0/3.69);
  3834. x=x*AF2_(a)+AF2_(y*b);
  3835. return AH2(AFractF2(x));}
  3836. //------------------------------------------------------------------------------------------------------------------------------
  3837. void FsrTepdC8Hx2(inout AH2 cR,inout AH2 cG,inout AH2 cB,AH2 dit){
  3838. AH2 nR=sqrt(cR);
  3839. AH2 nG=sqrt(cG);
  3840. AH2 nB=sqrt(cB);
  3841. nR=floor(nR*AH2_(255.0))*AH2_(1.0/255.0);
  3842. nG=floor(nG*AH2_(255.0))*AH2_(1.0/255.0);
  3843. nB=floor(nB*AH2_(255.0))*AH2_(1.0/255.0);
  3844. AH2 aR=nR*nR;
  3845. AH2 aG=nG*nG;
  3846. AH2 aB=nB*nB;
  3847. AH2 bR=nR+AH2_(1.0/255.0);bR=bR*bR;
  3848. AH2 bG=nG+AH2_(1.0/255.0);bG=bG*bG;
  3849. AH2 bB=nB+AH2_(1.0/255.0);bB=bB*bB;
  3850. AH2 rR=(cR-bR)*APrxMedRcpH2(aR-bR);
  3851. AH2 rG=(cG-bG)*APrxMedRcpH2(aG-bG);
  3852. AH2 rB=(cB-bB)*APrxMedRcpH2(aB-bB);
  3853. cR=ASatH2(nR+AGtZeroH2(dit-rR)*AH2_(1.0/255.0));
  3854. cG=ASatH2(nG+AGtZeroH2(dit-rG)*AH2_(1.0/255.0));
  3855. cB=ASatH2(nB+AGtZeroH2(dit-rB)*AH2_(1.0/255.0));}
  3856. //------------------------------------------------------------------------------------------------------------------------------
  3857. void FsrTepdC10Hx2(inout AH2 cR,inout AH2 cG,inout AH2 cB,AH2 dit){
  3858. AH2 nR=sqrt(cR);
  3859. AH2 nG=sqrt(cG);
  3860. AH2 nB=sqrt(cB);
  3861. nR=floor(nR*AH2_(1023.0))*AH2_(1.0/1023.0);
  3862. nG=floor(nG*AH2_(1023.0))*AH2_(1.0/1023.0);
  3863. nB=floor(nB*AH2_(1023.0))*AH2_(1.0/1023.0);
  3864. AH2 aR=nR*nR;
  3865. AH2 aG=nG*nG;
  3866. AH2 aB=nB*nB;
  3867. AH2 bR=nR+AH2_(1.0/1023.0);bR=bR*bR;
  3868. AH2 bG=nG+AH2_(1.0/1023.0);bG=bG*bG;
  3869. AH2 bB=nB+AH2_(1.0/1023.0);bB=bB*bB;
  3870. AH2 rR=(cR-bR)*APrxMedRcpH2(aR-bR);
  3871. AH2 rG=(cG-bG)*APrxMedRcpH2(aG-bG);
  3872. AH2 rB=(cB-bB)*APrxMedRcpH2(aB-bB);
  3873. cR=ASatH2(nR+AGtZeroH2(dit-rR)*AH2_(1.0/1023.0));
  3874. cG=ASatH2(nG+AGtZeroH2(dit-rG)*AH2_(1.0/1023.0));
  3875. cB=ASatH2(nB+AGtZeroH2(dit-rB)*AH2_(1.0/1023.0));}
  3876. #endif
  3877. void CurrFilter(AU2 pos)
  3878. {
  3879. AF3 c;
  3880. FsrRcasF(c.r, c.g, c.b, pos, con0);
  3881. imageStore(imgOutput, ASU2(pos), AF4(c, 1));
  3882. }
  3883. void main() {
  3884. FsrRcasCon(con0, sharpening_data);
  3885. AU2 gxy = ARmp8x8(gl_LocalInvocationID.x) + AU2(gl_WorkGroupID.x << 4u, gl_WorkGroupID.y << 4u);
  3886. CurrFilter(gxy);
  3887. gxy.x += 8u;
  3888. CurrFilter(gxy);
  3889. gxy.y += 8u;
  3890. CurrFilter(gxy);
  3891. gxy.x -= 8u;
  3892. CurrFilter(gxy);
  3893. }