blur9fast-vertical.vs 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
  1. #version 150
  2. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  3. // Copyright (C) 2014 TroggleMonkey
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. #if __VERSION__ >= 130
  23. #define COMPAT_TEXTURE texture
  24. #else
  25. #define COMPAT_TEXTURE texture2D
  26. #endif
  27. #ifdef GL_ES
  28. #define COMPAT_PRECISION mediump
  29. #else
  30. #define COMPAT_PRECISION
  31. #endif
  32. in vec4 position;
  33. in vec2 texCoord;
  34. out Vertex {
  35. vec2 vTexCoord;
  36. vec2 blur_dxdy;
  37. };
  38. uniform vec4 targetSize;
  39. uniform vec4 sourceSize[];
  40. ///////////////////////////// SETTINGS MANAGEMENT ////////////////////////////
  41. // PASS SETTINGS:
  42. // gamma-management.h needs to know what kind of pipeline we're using and
  43. // what pass this is in that pipeline. This will become obsolete if/when we
  44. // can #define things like this in the .cgp preset file.
  45. //#define GAMMA_ENCODE_EVERY_FBO
  46. //#define FIRST_PASS
  47. //#define LAST_PASS
  48. //#define SIMULATE_CRT_ON_LCD
  49. //#define SIMULATE_GBA_ON_LCD
  50. //#define SIMULATE_LCD_ON_CRT
  51. //#define SIMULATE_GBA_ON_CRT
  52. #ifndef GAMMA_MANAGEMENT_H
  53. #define GAMMA_MANAGEMENT_H
  54. /////////////////////////////// BASE CONSTANTS ///////////////////////////////
  55. // Set standard gamma constants, but allow users to override them:
  56. #ifndef OVERRIDE_STANDARD_GAMMA
  57. // Standard encoding gammas:
  58. float ntsc_gamma = 2.2; // Best to use NTSC for PAL too?
  59. float pal_gamma = 2.8; // Never actually 2.8 in practice
  60. // Typical device decoding gammas (only use for emulating devices):
  61. // CRT/LCD reference gammas are higher than NTSC and Rec.709 video standard
  62. // gammas: The standards purposely undercorrected for an analog CRT's
  63. // assumed 2.5 reference display gamma to maintain contrast in assumed
  64. // [dark] viewing conditions: http://www.poynton.com/PDFs/GammaFAQ.pdf
  65. // These unstated assumptions about display gamma and perceptual rendering
  66. // intent caused a lot of confusion, and more modern CRT's seemed to target
  67. // NTSC 2.2 gamma with circuitry. LCD displays seem to have followed suit
  68. // (they struggle near black with 2.5 gamma anyway), especially PC/laptop
  69. // displays designed to view sRGB in bright environments. (Standards are
  70. // also in flux again with BT.1886, but it's underspecified for displays.)
  71. float crt_reference_gamma_high = 2.5; // In (2.35, 2.55)
  72. float crt_reference_gamma_low = 2.35; // In (2.35, 2.55)
  73. float lcd_reference_gamma = 2.5; // To match CRT
  74. float crt_office_gamma = 2.2; // Circuitry-adjusted for NTSC
  75. float lcd_office_gamma = 2.2; // Approximates sRGB
  76. #endif // OVERRIDE_STANDARD_GAMMA
  77. // Assuming alpha == 1.0 might make it easier for users to avoid some bugs,
  78. // but only if they're aware of it.
  79. #ifndef OVERRIDE_ALPHA_ASSUMPTIONS
  80. bool assume_opaque_alpha = false;
  81. #endif
  82. /////////////////////// DERIVED CONSTANTS AS FUNCTIONS ///////////////////////
  83. // gamma-management.h should be compatible with overriding gamma values with
  84. // runtime user parameters, but we can only define other global constants in
  85. // terms of static constants, not uniform user parameters. To get around this
  86. // limitation, we need to define derived constants using functions.
  87. // Set device gamma constants, but allow users to override them:
  88. #ifdef OVERRIDE_DEVICE_GAMMA
  89. // The user promises to globally define the appropriate constants:
  90. float get_crt_gamma() { return crt_gamma; }
  91. float get_gba_gamma() { return gba_gamma; }
  92. float get_lcd_gamma() { return lcd_gamma; }
  93. #else
  94. float get_crt_gamma() { return crt_reference_gamma_high; }
  95. float get_gba_gamma() { return 3.5; } // Game Boy Advance; in (3.0, 4.0)
  96. float get_lcd_gamma() { return lcd_office_gamma; }
  97. #endif // OVERRIDE_DEVICE_GAMMA
  98. // Set decoding/encoding gammas for the first/lass passes, but allow overrides:
  99. #ifdef OVERRIDE_FINAL_GAMMA
  100. // The user promises to globally define the appropriate constants:
  101. float get_intermediate_gamma() { return intermediate_gamma; }
  102. float get_input_gamma() { return input_gamma; }
  103. float get_output_gamma() { return output_gamma; }
  104. #else
  105. // If we gamma-correct every pass, always use ntsc_gamma between passes to
  106. // ensure middle passes don't need to care if anything is being simulated:
  107. float get_intermediate_gamma() { return ntsc_gamma; }
  108. #ifdef SIMULATE_CRT_ON_LCD
  109. float get_input_gamma() { return get_crt_gamma(); }
  110. float get_output_gamma() { return get_lcd_gamma(); }
  111. #else
  112. #ifdef SIMULATE_GBA_ON_LCD
  113. float get_input_gamma() { return get_gba_gamma(); }
  114. float get_output_gamma() { return get_lcd_gamma(); }
  115. #else
  116. #ifdef SIMULATE_LCD_ON_CRT
  117. float get_input_gamma() { return get_lcd_gamma(); }
  118. float get_output_gamma() { return get_crt_gamma(); }
  119. #else
  120. #ifdef SIMULATE_GBA_ON_CRT
  121. float get_input_gamma() { return get_gba_gamma(); }
  122. float get_output_gamma() { return get_crt_gamma(); }
  123. #else // Don't simulate anything:
  124. float get_input_gamma() { return ntsc_gamma; }
  125. float get_output_gamma() { return ntsc_gamma; }
  126. #endif // SIMULATE_GBA_ON_CRT
  127. #endif // SIMULATE_LCD_ON_CRT
  128. #endif // SIMULATE_GBA_ON_LCD
  129. #endif // SIMULATE_CRT_ON_LCD
  130. #endif // OVERRIDE_FINAL_GAMMA
  131. #ifndef GAMMA_ENCODE_EVERY_FBO
  132. #ifdef FIRST_PASS
  133. bool linearize_input = true;
  134. float get_pass_input_gamma() { return get_input_gamma(); }
  135. #else
  136. bool linearize_input = false;
  137. float get_pass_input_gamma() { return 1.0; }
  138. #endif
  139. #ifdef LAST_PASS
  140. bool gamma_encode_output = true;
  141. float get_pass_output_gamma() { return get_output_gamma(); }
  142. #else
  143. bool gamma_encode_output = false;
  144. float get_pass_output_gamma() { return 1.0; }
  145. #endif
  146. #else
  147. bool linearize_input = true;
  148. bool gamma_encode_output = true;
  149. #ifdef FIRST_PASS
  150. float get_pass_input_gamma() { return get_input_gamma(); }
  151. #else
  152. float get_pass_input_gamma() { return get_intermediate_gamma(); }
  153. #endif
  154. #ifdef LAST_PASS
  155. float get_pass_output_gamma() { return get_output_gamma(); }
  156. #else
  157. float get_pass_output_gamma() { return get_intermediate_gamma(); }
  158. #endif
  159. #endif
  160. vec4 decode_input(vec4 color)
  161. {
  162. if(linearize_input = true)
  163. {
  164. if(assume_opaque_alpha = true)
  165. {
  166. return vec4(pow(color.rgb, vec3(get_pass_input_gamma())), 1.0);
  167. }
  168. else
  169. {
  170. return vec4(pow(color.rgb, vec3(get_pass_input_gamma())), color.a);
  171. }
  172. }
  173. else
  174. {
  175. return color;
  176. }
  177. }
  178. vec4 encode_output(vec4 color)
  179. {
  180. if(gamma_encode_output = true)
  181. {
  182. if(assume_opaque_alpha = true)
  183. {
  184. return vec4(pow(color.rgb, vec3(1.0/get_pass_output_gamma())), 1.0);
  185. }
  186. else
  187. {
  188. return vec4(pow(color.rgb, vec3(1.0/get_pass_output_gamma())), color.a);
  189. }
  190. }
  191. else
  192. {
  193. return color;
  194. }
  195. }
  196. #define tex2D_linearize(C, D) decode_input(vec4(COMPAT_TEXTURE(C, D)))
  197. //vec4 tex2D_linearize(sampler2D tex, vec2 tex_coords)
  198. //{ return decode_input(vec4(COMPAT_TEXTURE(tex, tex_coords))); }
  199. //#define tex2D_linearize(C, D, E) decode_input(vec4(COMPAT_TEXTURE(C, D, E)))
  200. //vec4 tex2D_linearize(sampler2D tex, vec2 tex_coords, int texel_off)
  201. //{ return decode_input(vec4(COMPAT_TEXTURE(tex, tex_coords, texel_off))); }
  202. #endif // GAMMA_MANAGEMENT_H
  203. #ifndef BLUR_FUNCTIONS_H
  204. #define BLUR_FUNCTIONS_H
  205. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  206. // Copyright (C) 2014 TroggleMonkey
  207. //
  208. // Permission is hereby granted, free of charge, to any person obtaining a copy
  209. // of this software and associated documentation files (the "Software"), to
  210. // deal in the Software without restriction, including without limitation the
  211. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  212. // sell copies of the Software, and to permit persons to whom the Software is
  213. // furnished to do so, subject to the following conditions:
  214. //
  215. // The above copyright notice and this permission notice shall be included in
  216. // all copies or substantial portions of the Software.
  217. //
  218. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  219. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  220. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  221. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  222. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  223. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  224. // IN THE SOFTWARE.
  225. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  226. // This file provides reusable one-pass and separable (two-pass) blurs.
  227. // Requires: All blurs share these requirements (dxdy requirement is split):
  228. // 1.) All requirements of gamma-management.h must be satisfied!
  229. // 2.) filter_linearN must == "true" in your .cgp preset unless
  230. // you're using tex2DblurNresize at 1x scale.
  231. // 3.) mipmap_inputN must == "true" in your .cgp preset if
  232. // IN.output_size < IN.video_size.
  233. // 4.) IN.output_size == IN.video_size / pow(2, M), where M is some
  234. // positive integer. tex2Dblur*resize can resize arbitrarily
  235. // (and the blur will be done after resizing), but arbitrary
  236. // resizes "fail" with other blurs due to the way they mix
  237. // static weights with bilinear sample exploitation.
  238. // 5.) In general, dxdy should contain the uv pixel spacing:
  239. // dxdy = (IN.video_size/IN.output_size)/IN.texture_size
  240. // 6.) For separable blurs (tex2DblurNresize and tex2DblurNfast),
  241. // zero out the dxdy component in the unblurred dimension:
  242. // dxdy = vec2(dxdy.x, 0.0) or vec2(0.0, dxdy.y)
  243. // Many blurs share these requirements:
  244. // 1.) One-pass blurs require scale_xN == scale_yN or scales > 1.0,
  245. // or they will blur more in the lower-scaled dimension.
  246. // 2.) One-pass shared sample blurs require ddx(), ddy(), and
  247. // tex2Dlod() to be supported by the current Cg profile, and
  248. // the drivers must support high-quality derivatives.
  249. // 3.) One-pass shared sample blurs require:
  250. // tex_uv.w == log2(IN.video_size/IN.output_size).y;
  251. // Non-wrapper blurs share this requirement:
  252. // 1.) sigma is the intended standard deviation of the blur
  253. // Wrapper blurs share this requirement, which is automatically
  254. // met (unless OVERRIDE_BLUR_STD_DEVS is #defined; see below):
  255. // 1.) blurN_std_dev must be global static float values
  256. // specifying standard deviations for Nx blurs in units
  257. // of destination pixels
  258. // Optional: 1.) The including file (or an earlier included file) may
  259. // optionally #define USE_BINOMIAL_BLUR_STD_DEVS to replace
  260. // default standard deviations with those matching a binomial
  261. // distribution. (See below for details/properties.)
  262. // 2.) The including file (or an earlier included file) may
  263. // optionally #define OVERRIDE_BLUR_STD_DEVS and override:
  264. // static float blur3_std_dev
  265. // static float blur4_std_dev
  266. // static float blur5_std_dev
  267. // static float blur6_std_dev
  268. // static float blur7_std_dev
  269. // static float blur8_std_dev
  270. // static float blur9_std_dev
  271. // static float blur10_std_dev
  272. // static float blur11_std_dev
  273. // static float blur12_std_dev
  274. // static float blur17_std_dev
  275. // static float blur25_std_dev
  276. // static float blur31_std_dev
  277. // static float blur43_std_dev
  278. // 3.) The including file (or an earlier included file) may
  279. // optionally #define OVERRIDE_ERROR_BLURRING and override:
  280. // static float error_blurring
  281. // This tuning value helps mitigate weighting errors from one-
  282. // pass shared-sample blurs sharing bilinear samples between
  283. // fragments. Values closer to 0.0 have "correct" blurriness
  284. // but allow more artifacts, and values closer to 1.0 blur away
  285. // artifacts by sampling closer to halfway between texels.
  286. // UPDATE 6/21/14: The above static constants may now be overridden
  287. // by non-static uniform constants. This permits exposing blur
  288. // standard deviations as runtime GUI shader parameters. However,
  289. // using them keeps weights from being statically computed, and the
  290. // speed hit depends on the blur: On my machine, uniforms kill over
  291. // 53% of the framerate with tex2Dblur12x12shared, but they only
  292. // drop the framerate by about 18% with tex2Dblur11fast.
  293. // Quality and Performance Comparisons:
  294. // For the purposes of the following discussion, "no sRGB" means
  295. // GAMMA_ENCODE_EVERY_FBO is #defined, and "sRGB" means it isn't.
  296. // 1.) tex2DblurNfast is always faster than tex2DblurNresize.
  297. // 2.) tex2DblurNresize functions are the only ones that can arbitrarily resize
  298. // well, because they're the only ones that don't exploit bilinear samples.
  299. // This also means they're the only functions which can be truly gamma-
  300. // correct without linear (or sRGB FBO) input, but only at 1x scale.
  301. // 3.) One-pass shared sample blurs only have a speed advantage without sRGB.
  302. // They also have some inaccuracies due to their shared-[bilinear-]sample
  303. // design, which grow increasingly bothersome for smaller blurs and higher-
  304. // frequency source images (relative to their resolution). I had high
  305. // hopes for them, but their most realistic use case is limited to quickly
  306. // reblurring an already blurred input at full resolution. Otherwise:
  307. // a.) If you're blurring a low-resolution source, you want a better blur.
  308. // b.) If you're blurring a lower mipmap, you want a better blur.
  309. // c.) If you're blurring a high-resolution, high-frequency source, you
  310. // want a better blur.
  311. // 4.) The one-pass blurs without shared samples grow slower for larger blurs,
  312. // but they're competitive with separable blurs at 5x5 and smaller, and
  313. // even tex2Dblur7x7 isn't bad if you're wanting to conserve passes.
  314. // Here are some framerates from a GeForce 8800GTS. The first pass resizes to
  315. // viewport size (4x in this test) and linearizes for sRGB codepaths, and the
  316. // remaining passes perform 6 full blurs. Mipmapped tests are performed at the
  317. // same scale, so they just measure the cost of mipmapping each FBO (only every
  318. // other FBO is mipmapped for separable blurs, to mimic realistic usage).
  319. // Mipmap Neither sRGB+Mipmap sRGB Function
  320. // 76.0 92.3 131.3 193.7 tex2Dblur3fast
  321. // 63.2 74.4 122.4 175.5 tex2Dblur3resize
  322. // 93.7 121.2 159.3 263.2 tex2Dblur3x3
  323. // 59.7 68.7 115.4 162.1 tex2Dblur3x3resize
  324. // 63.2 74.4 122.4 175.5 tex2Dblur5fast
  325. // 49.3 54.8 100.0 132.7 tex2Dblur5resize
  326. // 59.7 68.7 115.4 162.1 tex2Dblur5x5
  327. // 64.9 77.2 99.1 137.2 tex2Dblur6x6shared
  328. // 55.8 63.7 110.4 151.8 tex2Dblur7fast
  329. // 39.8 43.9 83.9 105.8 tex2Dblur7resize
  330. // 40.0 44.2 83.2 104.9 tex2Dblur7x7
  331. // 56.4 65.5 71.9 87.9 tex2Dblur8x8shared
  332. // 49.3 55.1 99.9 132.5 tex2Dblur9fast
  333. // 33.3 36.2 72.4 88.0 tex2Dblur9resize
  334. // 27.8 29.7 61.3 72.2 tex2Dblur9x9
  335. // 37.2 41.1 52.6 60.2 tex2Dblur10x10shared
  336. // 44.4 49.5 91.3 117.8 tex2Dblur11fast
  337. // 28.8 30.8 63.6 75.4 tex2Dblur11resize
  338. // 33.6 36.5 40.9 45.5 tex2Dblur12x12shared
  339. // TODO: Fill in benchmarks for new untested blurs.
  340. // tex2Dblur17fast
  341. // tex2Dblur25fast
  342. // tex2Dblur31fast
  343. // tex2Dblur43fast
  344. // tex2Dblur3x3resize
  345. ///////////////////////////// SETTINGS MANAGEMENT ////////////////////////////
  346. // Set static standard deviations, but allow users to override them with their
  347. // own constants (even non-static uniforms if they're okay with the speed hit):
  348. #ifndef OVERRIDE_BLUR_STD_DEVS
  349. // blurN_std_dev values are specified in terms of dxdy strides.
  350. #ifdef USE_BINOMIAL_BLUR_STD_DEVS
  351. // By request, we can define standard deviations corresponding to a
  352. // binomial distribution with p = 0.5 (related to Pascal's triangle).
  353. // This distribution works such that blurring multiple times should
  354. // have the same result as a single larger blur. These values are
  355. // larger than default for blurs up to 6x and smaller thereafter.
  356. float blur3_std_dev = 0.84931640625;
  357. float blur4_std_dev = 0.84931640625;
  358. float blur5_std_dev = 1.0595703125;
  359. float blur6_std_dev = 1.06591796875;
  360. float blur7_std_dev = 1.17041015625;
  361. float blur8_std_dev = 1.1720703125;
  362. float blur9_std_dev = 1.2259765625;
  363. float blur10_std_dev = 1.21982421875;
  364. float blur11_std_dev = 1.25361328125;
  365. float blur12_std_dev = 1.2423828125;
  366. float blur17_std_dev = 1.27783203125;
  367. float blur25_std_dev = 1.2810546875;
  368. float blur31_std_dev = 1.28125;
  369. float blur43_std_dev = 1.28125;
  370. #else
  371. // The defaults are the largest values that keep the largest unused
  372. // blur term on each side <= 1.0/256.0. (We could get away with more
  373. // or be more conservative, but this compromise is pretty reasonable.)
  374. float blur3_std_dev = 0.62666015625;
  375. float blur4_std_dev = 0.66171875;
  376. float blur5_std_dev = 0.9845703125;
  377. float blur6_std_dev = 1.02626953125;
  378. float blur7_std_dev = 1.36103515625;
  379. float blur8_std_dev = 1.4080078125;
  380. float blur9_std_dev = 1.7533203125;
  381. float blur10_std_dev = 1.80478515625;
  382. float blur11_std_dev = 2.15986328125;
  383. float blur12_std_dev = 2.215234375;
  384. float blur17_std_dev = 3.45535583496;
  385. float blur25_std_dev = 5.3409576416;
  386. float blur31_std_dev = 6.86488037109;
  387. float blur43_std_dev = 10.1852050781;
  388. #endif // USE_BINOMIAL_BLUR_STD_DEVS
  389. #endif // OVERRIDE_BLUR_STD_DEVS
  390. #ifndef OVERRIDE_ERROR_BLURRING
  391. // error_blurring should be in [0.0, 1.0]. Higher values reduce ringing
  392. // in shared-sample blurs but increase blurring and feature shifting.
  393. float error_blurring = 0.5;
  394. #endif
  395. // Make a length squared helper macro (for usage with static constants):
  396. #define LENGTH_SQ(vec) (dot(vec, vec))
  397. ////////////////////////////////// INCLUDES //////////////////////////////////
  398. // gamma-management.h relies on pass-specific settings to guide its behavior:
  399. // FIRST_PASS, LAST_PASS, GAMMA_ENCODE_EVERY_FBO, etc. See it for details.
  400. //#include "gamma-management.h"
  401. //#include "quad-pixel-communication.h"
  402. //#include "special-functions.h"
  403. #ifndef SPECIAL_FUNCTIONS_H
  404. #define SPECIAL_FUNCTIONS_H
  405. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  406. // Copyright (C) 2014 TroggleMonkey
  407. //
  408. // Permission is hereby granted, free of charge, to any person obtaining a copy
  409. // of this software and associated documentation files (the "Software"), to
  410. // deal in the Software without restriction, including without limitation the
  411. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  412. // sell copies of the Software, and to permit persons to whom the Software is
  413. // furnished to do so, subject to the following conditions:
  414. //
  415. // The above copyright notice and this permission notice shall be included in
  416. // all copies or substantial portions of the Software.
  417. //
  418. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  419. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  420. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  421. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  422. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  423. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  424. // IN THE SOFTWARE.
  425. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  426. // This file implements the following mathematical special functions:
  427. // 1.) erf() = 2/sqrt(pi) * indefinite_integral(e**(-x**2))
  428. // 2.) gamma(s), a real-numbered extension of the integer factorial function
  429. // It also implements normalized_ligamma(s, z), a normalized lower incomplete
  430. // gamma function for s < 0.5 only. Both gamma() and normalized_ligamma() can
  431. // be called with an _impl suffix to use an implementation version with a few
  432. // extra precomputed parameters (which may be useful for the caller to reuse).
  433. // See below for details.
  434. //
  435. // Design Rationale:
  436. // Pretty much every line of code in this file is duplicated four times for
  437. // different input types (vec4/vec3/vec2/float). This is unfortunate,
  438. // but Cg doesn't allow function templates. Macros would be far less verbose,
  439. // but they would make the code harder to document and read. I don't expect
  440. // these functions will require a whole lot of maintenance changes unless
  441. // someone ever has need for more robust incomplete gamma functions, so code
  442. // duplication seems to be the lesser evil in this case.
  443. /////////////////////////// GAUSSIAN ERROR FUNCTION //////////////////////////
  444. vec4 erf6(vec4 x)
  445. {
  446. // Requires: x is the standard parameter to erf().
  447. // Returns: Return an Abramowitz/Stegun approximation of erf(), where:
  448. // erf(x) = 2/sqrt(pi) * integral(e**(-x**2))
  449. // This approximation has a max absolute error of 2.5*10**-5
  450. // with solid numerical robustness and efficiency. See:
  451. // https://en.wikipedia.org/wiki/Error_function#Approximation_with_elementary_functions
  452. vec4 one = vec4(1.0);
  453. vec4 sign_x = sign(x);
  454. vec4 t = one/(one + 0.47047*abs(x));
  455. vec4 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  456. exp(-(x*x));
  457. return result * sign_x;
  458. }
  459. vec3 erf6(vec3 x)
  460. {
  461. // vec3 version:
  462. vec3 one = vec3(1.0);
  463. vec3 sign_x = sign(x);
  464. vec3 t = one/(one + 0.47047*abs(x));
  465. vec3 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  466. exp(-(x*x));
  467. return result * sign_x;
  468. }
  469. vec2 erf6(vec2 x)
  470. {
  471. // vec2 version:
  472. vec2 one = vec2(1.0);
  473. vec2 sign_x = sign(x);
  474. vec2 t = one/(one + 0.47047*abs(x));
  475. vec2 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  476. exp(-(x*x));
  477. return result * sign_x;
  478. }
  479. float erf6(float x)
  480. {
  481. // Float version:
  482. float sign_x = sign(x);
  483. float t = 1.0/(1.0 + 0.47047*abs(x));
  484. float result = 1.0 - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  485. exp(-(x*x));
  486. return result * sign_x;
  487. }
  488. vec4 erft(vec4 x)
  489. {
  490. // Requires: x is the standard parameter to erf().
  491. // Returns: Approximate erf() with the hyperbolic tangent. The error is
  492. // visually noticeable, but it's blazing fast and perceptually
  493. // close...at least on ATI hardware. See:
  494. // http://www.maplesoft.com/applications/view.aspx?SID=5525&view=html
  495. // Warning: Only use this if your hardware drivers correctly implement
  496. // tanh(): My nVidia 8800GTS returns garbage output.
  497. return tanh(1.202760580 * x);
  498. }
  499. vec3 erft(vec3 x)
  500. {
  501. // vec3 version:
  502. return tanh(1.202760580 * x);
  503. }
  504. vec2 erft(vec2 x)
  505. {
  506. // vec2 version:
  507. return tanh(1.202760580 * x);
  508. }
  509. float erft(float x)
  510. {
  511. // Float version:
  512. return tanh(1.202760580 * x);
  513. }
  514. vec4 erf(vec4 x)
  515. {
  516. // Requires: x is the standard parameter to erf().
  517. // Returns: Some approximation of erf(x), depending on user settings.
  518. #ifdef ERF_FAST_APPROXIMATION
  519. return erft(x);
  520. #else
  521. return erf6(x);
  522. #endif
  523. }
  524. vec3 erf(vec3 x)
  525. {
  526. // vec3 version:
  527. #ifdef ERF_FAST_APPROXIMATION
  528. return erft(x);
  529. #else
  530. return erf6(x);
  531. #endif
  532. }
  533. vec2 erf(vec2 x)
  534. {
  535. // vec2 version:
  536. #ifdef ERF_FAST_APPROXIMATION
  537. return erft(x);
  538. #else
  539. return erf6(x);
  540. #endif
  541. }
  542. float erf(float x)
  543. {
  544. // Float version:
  545. #ifdef ERF_FAST_APPROXIMATION
  546. return erft(x);
  547. #else
  548. return erf6(x);
  549. #endif
  550. }
  551. /////////////////////////// COMPLETE GAMMA FUNCTION //////////////////////////
  552. vec4 gamma_impl(vec4 s, vec4 s_inv)
  553. {
  554. // Requires: 1.) s is the standard parameter to the gamma function, and
  555. // it should lie in the [0, 36] range.
  556. // 2.) s_inv = 1.0/s. This implementation function requires
  557. // the caller to precompute this value, giving users the
  558. // opportunity to reuse it.
  559. // Returns: Return approximate gamma function (real-numbered factorial)
  560. // output using the Lanczos approximation with two coefficients
  561. // calculated using Paul Godfrey's method here:
  562. // http://my.fit.edu/~gabdo/gamma.txt
  563. // An optimal g value for s in [0, 36] is ~1.12906830989, with
  564. // a maximum relative error of 0.000463 for 2**16 equally
  565. // evals. We could use three coeffs (0.0000346 error) without
  566. // hurting latency, but this allows more parallelism with
  567. // outside instructions.
  568. vec4 g = vec4(1.12906830989);
  569. vec4 c0 = vec4(0.8109119309638332633713423362694399653724431);
  570. vec4 c1 = vec4(0.4808354605142681877121661197951496120000040);
  571. vec4 e = vec4(2.71828182845904523536028747135266249775724709);
  572. vec4 sph = s + vec4(0.5);
  573. vec4 lanczos_sum = c0 + c1/(s + vec4(1.0));
  574. vec4 base = (sph + g)/e; // or (s + g + vec4(0.5))/e
  575. // gamma(s + 1) = base**sph * lanczos_sum; divide by s for gamma(s).
  576. // This has less error for small s's than (s -= 1.0) at the beginning.
  577. return (pow(base, sph) * lanczos_sum) * s_inv;
  578. }
  579. vec3 gamma_impl(vec3 s, vec3 s_inv)
  580. {
  581. // vec3 version:
  582. vec3 g = vec3(1.12906830989);
  583. vec3 c0 = vec3(0.8109119309638332633713423362694399653724431);
  584. vec3 c1 = vec3(0.4808354605142681877121661197951496120000040);
  585. vec3 e = vec3(2.71828182845904523536028747135266249775724709);
  586. vec3 sph = s + vec3(0.5);
  587. vec3 lanczos_sum = c0 + c1/(s + vec3(1.0));
  588. vec3 base = (sph + g)/e;
  589. return (pow(base, sph) * lanczos_sum) * s_inv;
  590. }
  591. vec2 gamma_impl(vec2 s, vec2 s_inv)
  592. {
  593. // vec2 version:
  594. vec2 g = vec2(1.12906830989);
  595. vec2 c0 = vec2(0.8109119309638332633713423362694399653724431);
  596. vec2 c1 = vec2(0.4808354605142681877121661197951496120000040);
  597. vec2 e = vec2(2.71828182845904523536028747135266249775724709);
  598. vec2 sph = s + vec2(0.5);
  599. vec2 lanczos_sum = c0 + c1/(s + vec2(1.0));
  600. vec2 base = (sph + g)/e;
  601. return (pow(base, sph) * lanczos_sum) * s_inv;
  602. }
  603. float gamma_impl(float s, float s_inv)
  604. {
  605. // Float version:
  606. float g = 1.12906830989;
  607. float c0 = 0.8109119309638332633713423362694399653724431;
  608. float c1 = 0.4808354605142681877121661197951496120000040;
  609. float e = 2.71828182845904523536028747135266249775724709;
  610. float sph = s + 0.5;
  611. float lanczos_sum = c0 + c1/(s + 1.0);
  612. float base = (sph + g)/e;
  613. return (pow(base, sph) * lanczos_sum) * s_inv;
  614. }
  615. vec4 gamma(vec4 s)
  616. {
  617. // Requires: s is the standard parameter to the gamma function, and it
  618. // should lie in the [0, 36] range.
  619. // Returns: Return approximate gamma function output with a maximum
  620. // relative error of 0.000463. See gamma_impl for details.
  621. return gamma_impl(s, vec4(1.0)/s);
  622. }
  623. vec3 gamma(vec3 s)
  624. {
  625. // vec3 version:
  626. return gamma_impl(s, vec3(1.0)/s);
  627. }
  628. vec2 gamma(vec2 s)
  629. {
  630. // vec2 version:
  631. return gamma_impl(s, vec2(1.0)/s);
  632. }
  633. float gamma(float s)
  634. {
  635. // Float version:
  636. return gamma_impl(s, 1.0/s);
  637. }
  638. //////////////// INCOMPLETE GAMMA FUNCTIONS (RESTRICTED INPUT) ///////////////
  639. // Lower incomplete gamma function for small s and z (implementation):
  640. vec4 ligamma_small_z_impl(vec4 s, vec4 z, vec4 s_inv)
  641. {
  642. // Requires: 1.) s < ~0.5
  643. // 2.) z <= ~0.775075
  644. // 3.) s_inv = 1.0/s (precomputed for outside reuse)
  645. // Returns: A series representation for the lower incomplete gamma
  646. // function for small s and small z (4 terms).
  647. // The actual "rolled up" summation looks like:
  648. // last_sign = 1.0; last_pow = 1.0; last_factorial = 1.0;
  649. // sum = last_sign * last_pow / ((s + k) * last_factorial)
  650. // for(int i = 0; i < 4; ++i)
  651. // {
  652. // last_sign *= -1.0; last_pow *= z; last_factorial *= i;
  653. // sum += last_sign * last_pow / ((s + k) * last_factorial);
  654. // }
  655. // Unrolled, constant-unfolded and arranged for madds and parallelism:
  656. vec4 scale = pow(z, s);
  657. vec4 sum = s_inv; // Summation iteration 0 result
  658. // Summation iterations 1, 2, and 3:
  659. vec4 z_sq = z*z;
  660. vec4 denom1 = s + vec4(1.0);
  661. vec4 denom2 = 2.0*s + vec4(4.0);
  662. vec4 denom3 = 6.0*s + vec4(18.0);
  663. //vec4 denom4 = 24.0*s + vec4(96.0);
  664. sum -= z/denom1;
  665. sum += z_sq/denom2;
  666. sum -= z * z_sq/denom3;
  667. //sum += z_sq * z_sq / denom4;
  668. // Scale and return:
  669. return scale * sum;
  670. }
  671. vec3 ligamma_small_z_impl(vec3 s, vec3 z, vec3 s_inv)
  672. {
  673. // vec3 version:
  674. vec3 scale = pow(z, s);
  675. vec3 sum = s_inv;
  676. vec3 z_sq = z*z;
  677. vec3 denom1 = s + vec3(1.0);
  678. vec3 denom2 = 2.0*s + vec3(4.0);
  679. vec3 denom3 = 6.0*s + vec3(18.0);
  680. sum -= z/denom1;
  681. sum += z_sq/denom2;
  682. sum -= z * z_sq/denom3;
  683. return scale * sum;
  684. }
  685. vec2 ligamma_small_z_impl(vec2 s, vec2 z, vec2 s_inv)
  686. {
  687. // vec2 version:
  688. vec2 scale = pow(z, s);
  689. vec2 sum = s_inv;
  690. vec2 z_sq = z*z;
  691. vec2 denom1 = s + vec2(1.0);
  692. vec2 denom2 = 2.0*s + vec2(4.0);
  693. vec2 denom3 = 6.0*s + vec2(18.0);
  694. sum -= z/denom1;
  695. sum += z_sq/denom2;
  696. sum -= z * z_sq/denom3;
  697. return scale * sum;
  698. }
  699. float ligamma_small_z_impl(float s, float z, float s_inv)
  700. {
  701. // Float version:
  702. float scale = pow(z, s);
  703. float sum = s_inv;
  704. float z_sq = z*z;
  705. float denom1 = s + 1.0;
  706. float denom2 = 2.0*s + 4.0;
  707. float denom3 = 6.0*s + 18.0;
  708. sum -= z/denom1;
  709. sum += z_sq/denom2;
  710. sum -= z * z_sq/denom3;
  711. return scale * sum;
  712. }
  713. // Upper incomplete gamma function for small s and large z (implementation):
  714. vec4 uigamma_large_z_impl(vec4 s, vec4 z)
  715. {
  716. // Requires: 1.) s < ~0.5
  717. // 2.) z > ~0.775075
  718. // Returns: Gauss's continued fraction representation for the upper
  719. // incomplete gamma function (4 terms).
  720. // The "rolled up" continued fraction looks like this. The denominator
  721. // is truncated, and it's calculated "from the bottom up:"
  722. // denom = vec4('inf');
  723. // vec4 one = vec4(1.0);
  724. // for(int i = 4; i > 0; --i)
  725. // {
  726. // denom = ((i * 2.0) - one) + z - s + (i * (s - i))/denom;
  727. // }
  728. // Unrolled and constant-unfolded for madds and parallelism:
  729. vec4 numerator = pow(z, s) * exp(-z);
  730. vec4 denom = vec4(7.0) + z - s;
  731. denom = vec4(5.0) + z - s + (3.0*s - vec4(9.0))/denom;
  732. denom = vec4(3.0) + z - s + (2.0*s - vec4(4.0))/denom;
  733. denom = vec4(1.0) + z - s + (s - vec4(1.0))/denom;
  734. return numerator / denom;
  735. }
  736. vec3 uigamma_large_z_impl(vec3 s, vec3 z)
  737. {
  738. // vec3 version:
  739. vec3 numerator = pow(z, s) * exp(-z);
  740. vec3 denom = vec3(7.0) + z - s;
  741. denom = vec3(5.0) + z - s + (3.0*s - vec3(9.0))/denom;
  742. denom = vec3(3.0) + z - s + (2.0*s - vec3(4.0))/denom;
  743. denom = vec3(1.0) + z - s + (s - vec3(1.0))/denom;
  744. return numerator / denom;
  745. }
  746. vec2 uigamma_large_z_impl(vec2 s, vec2 z)
  747. {
  748. // vec2 version:
  749. vec2 numerator = pow(z, s) * exp(-z);
  750. vec2 denom = vec2(7.0) + z - s;
  751. denom = vec2(5.0) + z - s + (3.0*s - vec2(9.0))/denom;
  752. denom = vec2(3.0) + z - s + (2.0*s - vec2(4.0))/denom;
  753. denom = vec2(1.0) + z - s + (s - vec2(1.0))/denom;
  754. return numerator / denom;
  755. }
  756. float uigamma_large_z_impl(float s, float z)
  757. {
  758. // Float version:
  759. float numerator = pow(z, s) * exp(-z);
  760. float denom = 7.0 + z - s;
  761. denom = 5.0 + z - s + (3.0*s - 9.0)/denom;
  762. denom = 3.0 + z - s + (2.0*s - 4.0)/denom;
  763. denom = 1.0 + z - s + (s - 1.0)/denom;
  764. return numerator / denom;
  765. }
  766. // Normalized lower incomplete gamma function for small s (implementation):
  767. vec4 normalized_ligamma_impl(vec4 s, vec4 z,
  768. vec4 s_inv, vec4 gamma_s_inv)
  769. {
  770. // Requires: 1.) s < ~0.5
  771. // 2.) s_inv = 1/s (precomputed for outside reuse)
  772. // 3.) gamma_s_inv = 1/gamma(s) (precomputed for outside reuse)
  773. // Returns: Approximate the normalized lower incomplete gamma function
  774. // for s < 0.5. Since we only care about s < 0.5, we only need
  775. // to evaluate two branches (not four) based on z. Each branch
  776. // uses four terms, with a max relative error of ~0.00182. The
  777. // branch threshold and specifics were adapted for fewer terms
  778. // from Gil/Segura/Temme's paper here:
  779. // http://oai.cwi.nl/oai/asset/20433/20433B.pdf
  780. // Evaluate both branches: Real branches test slower even when available.
  781. vec4 thresh = vec4(0.775075);
  782. bvec4 z_is_large = greaterThan(z , thresh);
  783. vec4 z_size_check = vec4(z_is_large.x ? 1.0 : 0.0, z_is_large.y ? 1.0 : 0.0, z_is_large.z ? 1.0 : 0.0, z_is_large.w ? 1.0 : 0.0);
  784. vec4 large_z = vec4(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  785. vec4 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  786. // Combine the results from both branches:
  787. return large_z * vec4(z_size_check) + small_z * vec4(z_size_check);
  788. }
  789. vec3 normalized_ligamma_impl(vec3 s, vec3 z,
  790. vec3 s_inv, vec3 gamma_s_inv)
  791. {
  792. // vec3 version:
  793. vec3 thresh = vec3(0.775075);
  794. bvec3 z_is_large = greaterThan(z , thresh);
  795. vec3 z_size_check = vec3(z_is_large.x ? 1.0 : 0.0, z_is_large.y ? 1.0 : 0.0, z_is_large.z ? 1.0 : 0.0);
  796. vec3 large_z = vec3(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  797. vec3 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  798. return large_z * vec3(z_size_check) + small_z * vec3(z_size_check);
  799. }
  800. vec2 normalized_ligamma_impl(vec2 s, vec2 z,
  801. vec2 s_inv, vec2 gamma_s_inv)
  802. {
  803. // vec2 version:
  804. vec2 thresh = vec2(0.775075);
  805. bvec2 z_is_large = greaterThan(z , thresh);
  806. vec2 z_size_check = vec2(z_is_large.x ? 1.0 : 0.0, z_is_large.y ? 1.0 : 0.0);
  807. vec2 large_z = vec2(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  808. vec2 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  809. return large_z * vec2(z_size_check) + small_z * vec2(z_size_check);
  810. }
  811. float normalized_ligamma_impl(float s, float z,
  812. float s_inv, float gamma_s_inv)
  813. {
  814. // Float version:
  815. float thresh = 0.775075;
  816. float z_size_check = 0.0;
  817. if (z > thresh) z_size_check = 1.0;
  818. float large_z = 1.0 - uigamma_large_z_impl(s, z) * gamma_s_inv;
  819. float small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  820. return large_z * float(z_size_check) + small_z * float(z_size_check);
  821. }
  822. // Normalized lower incomplete gamma function for small s:
  823. vec4 normalized_ligamma(vec4 s, vec4 z)
  824. {
  825. // Requires: s < ~0.5
  826. // Returns: Approximate the normalized lower incomplete gamma function
  827. // for s < 0.5. See normalized_ligamma_impl() for details.
  828. vec4 s_inv = vec4(1.0)/s;
  829. vec4 gamma_s_inv = vec4(1.0)/gamma_impl(s, s_inv);
  830. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  831. }
  832. vec3 normalized_ligamma(vec3 s, vec3 z)
  833. {
  834. // vec3 version:
  835. vec3 s_inv = vec3(1.0)/s;
  836. vec3 gamma_s_inv = vec3(1.0)/gamma_impl(s, s_inv);
  837. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  838. }
  839. vec2 normalized_ligamma(vec2 s, vec2 z)
  840. {
  841. // vec2 version:
  842. vec2 s_inv = vec2(1.0)/s;
  843. vec2 gamma_s_inv = vec2(1.0)/gamma_impl(s, s_inv);
  844. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  845. }
  846. float normalized_ligamma(float s, float z)
  847. {
  848. // Float version:
  849. float s_inv = 1.0/s;
  850. float gamma_s_inv = 1.0/gamma_impl(s, s_inv);
  851. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  852. }
  853. #endif // SPECIAL_FUNCTIONS_H
  854. /////////////////////////////////// HELPERS //////////////////////////////////
  855. vec4 uv2_to_uv4(vec2 tex_uv)
  856. {
  857. // Make a vec2 uv offset safe for adding to vec4 tex2Dlod coords:
  858. return vec4(tex_uv, 0.0, 0.0);
  859. }
  860. // Make a length squared helper macro (for usage with static constants):
  861. #define LENGTH_SQ(vec) (dot(vec, vec))
  862. float get_fast_gaussian_weight_sum_inv(float sigma)
  863. {
  864. // We can use the Gaussian integral to calculate the asymptotic weight for
  865. // the center pixel. Since the unnormalized center pixel weight is 1.0,
  866. // the normalized weight is the same as the weight sum inverse. Given a
  867. // large enough blur (9+), the asymptotic weight sum is close and faster:
  868. // center_weight = 0.5 *
  869. // (erf(0.5/(sigma*sqrt(2.0))) - erf(-0.5/(sigma*sqrt(2.0))))
  870. // erf(-x) == -erf(x), so we get 0.5 * (2.0 * erf(blah blah)):
  871. // However, we can get even faster results with curve-fitting. These are
  872. // also closer than the asymptotic results, because they were constructed
  873. // from 64 blurs sizes from [3, 131) and 255 equally-spaced sigmas from
  874. // (0, blurN_std_dev), so the results for smaller sigmas are biased toward
  875. // smaller blurs. The max error is 0.0031793913.
  876. // Relative FPS: 134.3 with erf, 135.8 with curve-fitting.
  877. //static float temp = 0.5/sqrt(2.0);
  878. //return erf(temp/sigma);
  879. return min(exp(exp(0.348348412457428/
  880. (sigma - 0.0860587260734721))), 0.399334576340352/sigma);
  881. }
  882. //////////////////// ARBITRARILY RESIZABLE SEPARABLE BLURS ///////////////////
  883. vec3 tex2Dblur11resize(sampler2D tex, vec2 tex_uv,
  884. vec2 dxdy, float sigma)
  885. {
  886. // Requires: Global requirements must be met (see file description).
  887. // Returns: A 1D 11x Gaussian blurred texture lookup using a 11-tap blur.
  888. // It may be mipmapped depending on settings and dxdy.
  889. // Calculate Gaussian blur kernel weights and a normalization factor for
  890. // distances of 0-4, ignoring constant factors (since we're normalizing).
  891. float denom_inv = 0.5/(sigma*sigma);
  892. float w0 = 1.0;
  893. float w1 = exp(-1.0 * denom_inv);
  894. float w2 = exp(-4.0 * denom_inv);
  895. float w3 = exp(-9.0 * denom_inv);
  896. float w4 = exp(-16.0 * denom_inv);
  897. float w5 = exp(-25.0 * denom_inv);
  898. float weight_sum_inv = 1.0 /
  899. (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5));
  900. // Statically normalize weights, sum weighted samples, and return. Blurs are
  901. // currently optimized for dynamic weights.
  902. vec3 sum = vec3(0.0);
  903. sum += w5 * tex2D_linearize(tex, tex_uv - 5.0 * dxdy).rgb;
  904. sum += w4 * tex2D_linearize(tex, tex_uv - 4.0 * dxdy).rgb;
  905. sum += w3 * tex2D_linearize(tex, tex_uv - 3.0 * dxdy).rgb;
  906. sum += w2 * tex2D_linearize(tex, tex_uv - 2.0 * dxdy).rgb;
  907. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  908. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  909. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  910. sum += w2 * tex2D_linearize(tex, tex_uv + 2.0 * dxdy).rgb;
  911. sum += w3 * tex2D_linearize(tex, tex_uv + 3.0 * dxdy).rgb;
  912. sum += w4 * tex2D_linearize(tex, tex_uv + 4.0 * dxdy).rgb;
  913. sum += w5 * tex2D_linearize(tex, tex_uv + 5.0 * dxdy).rgb;
  914. return sum * weight_sum_inv;
  915. }
  916. vec3 tex2Dblur9resize(sampler2D tex, vec2 tex_uv,
  917. vec2 dxdy, float sigma)
  918. {
  919. // Requires: Global requirements must be met (see file description).
  920. // Returns: A 1D 9x Gaussian blurred texture lookup using a 9-tap blur.
  921. // It may be mipmapped depending on settings and dxdy.
  922. // First get the texel weights and normalization factor as above.
  923. float denom_inv = 0.5/(sigma*sigma);
  924. float w0 = 1.0;
  925. float w1 = exp(-1.0 * denom_inv);
  926. float w2 = exp(-4.0 * denom_inv);
  927. float w3 = exp(-9.0 * denom_inv);
  928. float w4 = exp(-16.0 * denom_inv);
  929. float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3 + w4));
  930. // Statically normalize weights, sum weighted samples, and return:
  931. vec3 sum = vec3(0.0);
  932. sum += w4 * tex2D_linearize(tex, tex_uv - 4.0 * dxdy).rgb;
  933. sum += w3 * tex2D_linearize(tex, tex_uv - 3.0 * dxdy).rgb;
  934. sum += w2 * tex2D_linearize(tex, tex_uv - 2.0 * dxdy).rgb;
  935. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  936. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  937. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  938. sum += w2 * tex2D_linearize(tex, tex_uv + 2.0 * dxdy).rgb;
  939. sum += w3 * tex2D_linearize(tex, tex_uv + 3.0 * dxdy).rgb;
  940. sum += w4 * tex2D_linearize(tex, tex_uv + 4.0 * dxdy).rgb;
  941. return sum * weight_sum_inv;
  942. }
  943. vec3 tex2Dblur7resize(sampler2D tex, vec2 tex_uv,
  944. vec2 dxdy, float sigma)
  945. {
  946. // Requires: Global requirements must be met (see file description).
  947. // Returns: A 1D 7x Gaussian blurred texture lookup using a 7-tap blur.
  948. // It may be mipmapped depending on settings and dxdy.
  949. // First get the texel weights and normalization factor as above.
  950. float denom_inv = 0.5/(sigma*sigma);
  951. float w0 = 1.0;
  952. float w1 = exp(-1.0 * denom_inv);
  953. float w2 = exp(-4.0 * denom_inv);
  954. float w3 = exp(-9.0 * denom_inv);
  955. float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3));
  956. // Statically normalize weights, sum weighted samples, and return:
  957. vec3 sum = vec3(0.0);
  958. sum += w3 * tex2D_linearize(tex, tex_uv - 3.0 * dxdy).rgb;
  959. sum += w2 * tex2D_linearize(tex, tex_uv - 2.0 * dxdy).rgb;
  960. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  961. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  962. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  963. sum += w2 * tex2D_linearize(tex, tex_uv + 2.0 * dxdy).rgb;
  964. sum += w3 * tex2D_linearize(tex, tex_uv + 3.0 * dxdy).rgb;
  965. return sum * weight_sum_inv;
  966. }
  967. vec3 tex2Dblur5resize(sampler2D tex, vec2 tex_uv,
  968. vec2 dxdy, float sigma)
  969. {
  970. // Requires: Global requirements must be met (see file description).
  971. // Returns: A 1D 5x Gaussian blurred texture lookup using a 5-tap blur.
  972. // It may be mipmapped depending on settings and dxdy.
  973. // First get the texel weights and normalization factor as above.
  974. float denom_inv = 0.5/(sigma*sigma);
  975. float w0 = 1.0;
  976. float w1 = exp(-1.0 * denom_inv);
  977. float w2 = exp(-4.0 * denom_inv);
  978. float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2));
  979. // Statically normalize weights, sum weighted samples, and return:
  980. vec3 sum = vec3(0.0);
  981. sum += w2 * tex2D_linearize(tex, tex_uv - 2.0 * dxdy).rgb;
  982. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  983. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  984. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  985. sum += w2 * tex2D_linearize(tex, tex_uv + 2.0 * dxdy).rgb;
  986. return sum * weight_sum_inv;
  987. }
  988. vec3 tex2Dblur3resize(sampler2D tex, vec2 tex_uv,
  989. vec2 dxdy, float sigma)
  990. {
  991. // Requires: Global requirements must be met (see file description).
  992. // Returns: A 1D 3x Gaussian blurred texture lookup using a 3-tap blur.
  993. // It may be mipmapped depending on settings and dxdy.
  994. // First get the texel weights and normalization factor as above.
  995. float denom_inv = 0.5/(sigma*sigma);
  996. float w0 = 1.0;
  997. float w1 = exp(-1.0 * denom_inv);
  998. float weight_sum_inv = 1.0 / (w0 + 2.0 * w1);
  999. // Statically normalize weights, sum weighted samples, and return:
  1000. vec3 sum = vec3(0.0);
  1001. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  1002. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  1003. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  1004. return sum * weight_sum_inv;
  1005. }
  1006. /////////////////////////// FAST SEPARABLE BLURS ///////////////////////////
  1007. vec3 tex2Dblur11fast(sampler2D tex, vec2 tex_uv,
  1008. vec2 dxdy, float sigma)
  1009. {
  1010. // Requires: 1.) Global requirements must be met (see file description).
  1011. // 2.) filter_linearN must = "true" in your .cgp file.
  1012. // 3.) For gamma-correct bilinear filtering, global
  1013. // gamma_aware_bilinear == true (from gamma-management.h)
  1014. // Returns: A 1D 11x Gaussian blurred texture lookup using 6 linear
  1015. // taps. It may be mipmapped depending on settings and dxdy.
  1016. // First get the texel weights and normalization factor as above.
  1017. float denom_inv = 0.5/(sigma*sigma);
  1018. float w0 = 1.0;
  1019. float w1 = exp(-1.0 * denom_inv);
  1020. float w2 = exp(-4.0 * denom_inv);
  1021. float w3 = exp(-9.0 * denom_inv);
  1022. float w4 = exp(-16.0 * denom_inv);
  1023. float w5 = exp(-25.0 * denom_inv);
  1024. float weight_sum_inv = 1.0 /
  1025. (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5));
  1026. // Calculate combined weights and linear sample ratios between texel pairs.
  1027. // The center texel (with weight w0) is used twice, so halve its weight.
  1028. float w01 = w0 * 0.5 + w1;
  1029. float w23 = w2 + w3;
  1030. float w45 = w4 + w5;
  1031. float w01_ratio = w1/w01;
  1032. float w23_ratio = w3/w23;
  1033. float w45_ratio = w5/w45;
  1034. // Statically normalize weights, sum weighted samples, and return:
  1035. vec3 sum = vec3(0.0);
  1036. sum += w45 * tex2D_linearize(tex, tex_uv - (4.0 + w45_ratio) * dxdy).rgb;
  1037. sum += w23 * tex2D_linearize(tex, tex_uv - (2.0 + w23_ratio) * dxdy).rgb;
  1038. sum += w01 * tex2D_linearize(tex, tex_uv - w01_ratio * dxdy).rgb;
  1039. sum += w01 * tex2D_linearize(tex, tex_uv + w01_ratio * dxdy).rgb;
  1040. sum += w23 * tex2D_linearize(tex, tex_uv + (2.0 + w23_ratio) * dxdy).rgb;
  1041. sum += w45 * tex2D_linearize(tex, tex_uv + (4.0 + w45_ratio) * dxdy).rgb;
  1042. return sum * weight_sum_inv;
  1043. }
  1044. vec3 tex2Dblur17fast(sampler2D tex, vec2 tex_uv,
  1045. vec2 dxdy, float sigma)
  1046. {
  1047. // Requires: Same as tex2Dblur11()
  1048. // Returns: A 1D 17x Gaussian blurred texture lookup using 1 nearest
  1049. // neighbor and 8 linear taps. It may be mipmapped depending
  1050. // on settings and dxdy.
  1051. // First get the texel weights and normalization factor as above.
  1052. float denom_inv = 0.5/(sigma*sigma);
  1053. float w0 = 1.0;
  1054. float w1 = exp(-1.0 * denom_inv);
  1055. float w2 = exp(-4.0 * denom_inv);
  1056. float w3 = exp(-9.0 * denom_inv);
  1057. float w4 = exp(-16.0 * denom_inv);
  1058. float w5 = exp(-25.0 * denom_inv);
  1059. float w6 = exp(-36.0 * denom_inv);
  1060. float w7 = exp(-49.0 * denom_inv);
  1061. float w8 = exp(-64.0 * denom_inv);
  1062. //float weight_sum_inv = 1.0 / (w0 + 2.0 * (
  1063. // w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8));
  1064. float weight_sum_inv = get_fast_gaussian_weight_sum_inv(sigma);
  1065. // Calculate combined weights and linear sample ratios between texel pairs.
  1066. float w1_2 = w1 + w2;
  1067. float w3_4 = w3 + w4;
  1068. float w5_6 = w5 + w6;
  1069. float w7_8 = w7 + w8;
  1070. float w1_2_ratio = w2/w1_2;
  1071. float w3_4_ratio = w4/w3_4;
  1072. float w5_6_ratio = w6/w5_6;
  1073. float w7_8_ratio = w8/w7_8;
  1074. // Statically normalize weights, sum weighted samples, and return:
  1075. vec3 sum = vec3(0.0);
  1076. sum += w7_8 * tex2D_linearize(tex, tex_uv - (7.0 + w7_8_ratio) * dxdy).rgb;
  1077. sum += w5_6 * tex2D_linearize(tex, tex_uv - (5.0 + w5_6_ratio) * dxdy).rgb;
  1078. sum += w3_4 * tex2D_linearize(tex, tex_uv - (3.0 + w3_4_ratio) * dxdy).rgb;
  1079. sum += w1_2 * tex2D_linearize(tex, tex_uv - (1.0 + w1_2_ratio) * dxdy).rgb;
  1080. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  1081. sum += w1_2 * tex2D_linearize(tex, tex_uv + (1.0 + w1_2_ratio) * dxdy).rgb;
  1082. sum += w3_4 * tex2D_linearize(tex, tex_uv + (3.0 + w3_4_ratio) * dxdy).rgb;
  1083. sum += w5_6 * tex2D_linearize(tex, tex_uv + (5.0 + w5_6_ratio) * dxdy).rgb;
  1084. sum += w7_8 * tex2D_linearize(tex, tex_uv + (7.0 + w7_8_ratio) * dxdy).rgb;
  1085. return sum * weight_sum_inv;
  1086. }
  1087. vec3 tex2Dblur25fast(sampler2D tex, vec2 tex_uv,
  1088. vec2 dxdy, float sigma)
  1089. {
  1090. // Requires: Same as tex2Dblur11()
  1091. // Returns: A 1D 25x Gaussian blurred texture lookup using 1 nearest
  1092. // neighbor and 12 linear taps. It may be mipmapped depending
  1093. // on settings and dxdy.
  1094. // First get the texel weights and normalization factor as above.
  1095. float denom_inv = 0.5/(sigma*sigma);
  1096. float w0 = 1.0;
  1097. float w1 = exp(-1.0 * denom_inv);
  1098. float w2 = exp(-4.0 * denom_inv);
  1099. float w3 = exp(-9.0 * denom_inv);
  1100. float w4 = exp(-16.0 * denom_inv);
  1101. float w5 = exp(-25.0 * denom_inv);
  1102. float w6 = exp(-36.0 * denom_inv);
  1103. float w7 = exp(-49.0 * denom_inv);
  1104. float w8 = exp(-64.0 * denom_inv);
  1105. float w9 = exp(-81.0 * denom_inv);
  1106. float w10 = exp(-100.0 * denom_inv);
  1107. float w11 = exp(-121.0 * denom_inv);
  1108. float w12 = exp(-144.0 * denom_inv);
  1109. //float weight_sum_inv = 1.0 / (w0 + 2.0 * (
  1110. // w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12));
  1111. float weight_sum_inv = get_fast_gaussian_weight_sum_inv(sigma);
  1112. // Calculate combined weights and linear sample ratios between texel pairs.
  1113. float w1_2 = w1 + w2;
  1114. float w3_4 = w3 + w4;
  1115. float w5_6 = w5 + w6;
  1116. float w7_8 = w7 + w8;
  1117. float w9_10 = w9 + w10;
  1118. float w11_12 = w11 + w12;
  1119. float w1_2_ratio = w2/w1_2;
  1120. float w3_4_ratio = w4/w3_4;
  1121. float w5_6_ratio = w6/w5_6;
  1122. float w7_8_ratio = w8/w7_8;
  1123. float w9_10_ratio = w10/w9_10;
  1124. float w11_12_ratio = w12/w11_12;
  1125. // Statically normalize weights, sum weighted samples, and return:
  1126. vec3 sum = vec3(0.0);
  1127. sum += w11_12 * tex2D_linearize(tex, tex_uv - (11.0 + w11_12_ratio) * dxdy).rgb;
  1128. sum += w9_10 * tex2D_linearize(tex, tex_uv - (9.0 + w9_10_ratio) * dxdy).rgb;
  1129. sum += w7_8 * tex2D_linearize(tex, tex_uv - (7.0 + w7_8_ratio) * dxdy).rgb;
  1130. sum += w5_6 * tex2D_linearize(tex, tex_uv - (5.0 + w5_6_ratio) * dxdy).rgb;
  1131. sum += w3_4 * tex2D_linearize(tex, tex_uv - (3.0 + w3_4_ratio) * dxdy).rgb;
  1132. sum += w1_2 * tex2D_linearize(tex, tex_uv - (1.0 + w1_2_ratio) * dxdy).rgb;
  1133. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  1134. sum += w1_2 * tex2D_linearize(tex, tex_uv + (1.0 + w1_2_ratio) * dxdy).rgb;
  1135. sum += w3_4 * tex2D_linearize(tex, tex_uv + (3.0 + w3_4_ratio) * dxdy).rgb;
  1136. sum += w5_6 * tex2D_linearize(tex, tex_uv + (5.0 + w5_6_ratio) * dxdy).rgb;
  1137. sum += w7_8 * tex2D_linearize(tex, tex_uv + (7.0 + w7_8_ratio) * dxdy).rgb;
  1138. sum += w9_10 * tex2D_linearize(tex, tex_uv + (9.0 + w9_10_ratio) * dxdy).rgb;
  1139. sum += w11_12 * tex2D_linearize(tex, tex_uv + (11.0 + w11_12_ratio) * dxdy).rgb;
  1140. return sum * weight_sum_inv;
  1141. }
  1142. vec3 tex2Dblur31fast(sampler2D tex, vec2 tex_uv,
  1143. vec2 dxdy, float sigma)
  1144. {
  1145. // Requires: Same as tex2Dblur11()
  1146. // Returns: A 1D 31x Gaussian blurred texture lookup using 16 linear
  1147. // taps. It may be mipmapped depending on settings and dxdy.
  1148. // First get the texel weights and normalization factor as above.
  1149. float denom_inv = 0.5/(sigma*sigma);
  1150. float w0 = 1.0;
  1151. float w1 = exp(-1.0 * denom_inv);
  1152. float w2 = exp(-4.0 * denom_inv);
  1153. float w3 = exp(-9.0 * denom_inv);
  1154. float w4 = exp(-16.0 * denom_inv);
  1155. float w5 = exp(-25.0 * denom_inv);
  1156. float w6 = exp(-36.0 * denom_inv);
  1157. float w7 = exp(-49.0 * denom_inv);
  1158. float w8 = exp(-64.0 * denom_inv);
  1159. float w9 = exp(-81.0 * denom_inv);
  1160. float w10 = exp(-100.0 * denom_inv);
  1161. float w11 = exp(-121.0 * denom_inv);
  1162. float w12 = exp(-144.0 * denom_inv);
  1163. float w13 = exp(-169.0 * denom_inv);
  1164. float w14 = exp(-196.0 * denom_inv);
  1165. float w15 = exp(-225.0 * denom_inv);
  1166. //float weight_sum_inv = 1.0 /
  1167. // (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 +
  1168. // w9 + w10 + w11 + w12 + w13 + w14 + w15));
  1169. float weight_sum_inv = get_fast_gaussian_weight_sum_inv(sigma);
  1170. // Calculate combined weights and linear sample ratios between texel pairs.
  1171. // The center texel (with weight w0) is used twice, so halve its weight.
  1172. float w0_1 = w0 * 0.5 + w1;
  1173. float w2_3 = w2 + w3;
  1174. float w4_5 = w4 + w5;
  1175. float w6_7 = w6 + w7;
  1176. float w8_9 = w8 + w9;
  1177. float w10_11 = w10 + w11;
  1178. float w12_13 = w12 + w13;
  1179. float w14_15 = w14 + w15;
  1180. float w0_1_ratio = w1/w0_1;
  1181. float w2_3_ratio = w3/w2_3;
  1182. float w4_5_ratio = w5/w4_5;
  1183. float w6_7_ratio = w7/w6_7;
  1184. float w8_9_ratio = w9/w8_9;
  1185. float w10_11_ratio = w11/w10_11;
  1186. float w12_13_ratio = w13/w12_13;
  1187. float w14_15_ratio = w15/w14_15;
  1188. // Statically normalize weights, sum weighted samples, and return:
  1189. vec3 sum = vec3(0.0);
  1190. sum += w14_15 * tex2D_linearize(tex, tex_uv - (14.0 + w14_15_ratio) * dxdy).rgb;
  1191. sum += w12_13 * tex2D_linearize(tex, tex_uv - (12.0 + w12_13_ratio) * dxdy).rgb;
  1192. sum += w10_11 * tex2D_linearize(tex, tex_uv - (10.0 + w10_11_ratio) * dxdy).rgb;
  1193. sum += w8_9 * tex2D_linearize(tex, tex_uv - (8.0 + w8_9_ratio) * dxdy).rgb;
  1194. sum += w6_7 * tex2D_linearize(tex, tex_uv - (6.0 + w6_7_ratio) * dxdy).rgb;
  1195. sum += w4_5 * tex2D_linearize(tex, tex_uv - (4.0 + w4_5_ratio) * dxdy).rgb;
  1196. sum += w2_3 * tex2D_linearize(tex, tex_uv - (2.0 + w2_3_ratio) * dxdy).rgb;
  1197. sum += w0_1 * tex2D_linearize(tex, tex_uv - w0_1_ratio * dxdy).rgb;
  1198. sum += w0_1 * tex2D_linearize(tex, tex_uv + w0_1_ratio * dxdy).rgb;
  1199. sum += w2_3 * tex2D_linearize(tex, tex_uv + (2.0 + w2_3_ratio) * dxdy).rgb;
  1200. sum += w4_5 * tex2D_linearize(tex, tex_uv + (4.0 + w4_5_ratio) * dxdy).rgb;
  1201. sum += w6_7 * tex2D_linearize(tex, tex_uv + (6.0 + w6_7_ratio) * dxdy).rgb;
  1202. sum += w8_9 * tex2D_linearize(tex, tex_uv + (8.0 + w8_9_ratio) * dxdy).rgb;
  1203. sum += w10_11 * tex2D_linearize(tex, tex_uv + (10.0 + w10_11_ratio) * dxdy).rgb;
  1204. sum += w12_13 * tex2D_linearize(tex, tex_uv + (12.0 + w12_13_ratio) * dxdy).rgb;
  1205. sum += w14_15 * tex2D_linearize(tex, tex_uv + (14.0 + w14_15_ratio) * dxdy).rgb;
  1206. return sum * weight_sum_inv;
  1207. }
  1208. vec3 tex2Dblur43fast(sampler2D tex, vec2 tex_uv,
  1209. vec2 dxdy, float sigma)
  1210. {
  1211. // Requires: Same as tex2Dblur11()
  1212. // Returns: A 1D 43x Gaussian blurred texture lookup using 22 linear
  1213. // taps. It may be mipmapped depending on settings and dxdy.
  1214. // First get the texel weights and normalization factor as above.
  1215. float denom_inv = 0.5/(sigma*sigma);
  1216. float w0 = 1.0;
  1217. float w1 = exp(-1.0 * denom_inv);
  1218. float w2 = exp(-4.0 * denom_inv);
  1219. float w3 = exp(-9.0 * denom_inv);
  1220. float w4 = exp(-16.0 * denom_inv);
  1221. float w5 = exp(-25.0 * denom_inv);
  1222. float w6 = exp(-36.0 * denom_inv);
  1223. float w7 = exp(-49.0 * denom_inv);
  1224. float w8 = exp(-64.0 * denom_inv);
  1225. float w9 = exp(-81.0 * denom_inv);
  1226. float w10 = exp(-100.0 * denom_inv);
  1227. float w11 = exp(-121.0 * denom_inv);
  1228. float w12 = exp(-144.0 * denom_inv);
  1229. float w13 = exp(-169.0 * denom_inv);
  1230. float w14 = exp(-196.0 * denom_inv);
  1231. float w15 = exp(-225.0 * denom_inv);
  1232. float w16 = exp(-256.0 * denom_inv);
  1233. float w17 = exp(-289.0 * denom_inv);
  1234. float w18 = exp(-324.0 * denom_inv);
  1235. float w19 = exp(-361.0 * denom_inv);
  1236. float w20 = exp(-400.0 * denom_inv);
  1237. float w21 = exp(-441.0 * denom_inv);
  1238. //float weight_sum_inv = 1.0 /
  1239. // (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 +
  1240. // w12 + w13 + w14 + w15 + w16 + w17 + w18 + w19 + w20 + w21));
  1241. float weight_sum_inv = get_fast_gaussian_weight_sum_inv(sigma);
  1242. // Calculate combined weights and linear sample ratios between texel pairs.
  1243. // The center texel (with weight w0) is used twice, so halve its weight.
  1244. float w0_1 = w0 * 0.5 + w1;
  1245. float w2_3 = w2 + w3;
  1246. float w4_5 = w4 + w5;
  1247. float w6_7 = w6 + w7;
  1248. float w8_9 = w8 + w9;
  1249. float w10_11 = w10 + w11;
  1250. float w12_13 = w12 + w13;
  1251. float w14_15 = w14 + w15;
  1252. float w16_17 = w16 + w17;
  1253. float w18_19 = w18 + w19;
  1254. float w20_21 = w20 + w21;
  1255. float w0_1_ratio = w1/w0_1;
  1256. float w2_3_ratio = w3/w2_3;
  1257. float w4_5_ratio = w5/w4_5;
  1258. float w6_7_ratio = w7/w6_7;
  1259. float w8_9_ratio = w9/w8_9;
  1260. float w10_11_ratio = w11/w10_11;
  1261. float w12_13_ratio = w13/w12_13;
  1262. float w14_15_ratio = w15/w14_15;
  1263. float w16_17_ratio = w17/w16_17;
  1264. float w18_19_ratio = w19/w18_19;
  1265. float w20_21_ratio = w21/w20_21;
  1266. // Statically normalize weights, sum weighted samples, and return:
  1267. vec3 sum = vec3(0.0);
  1268. sum += w20_21 * tex2D_linearize(tex, tex_uv - (20.0 + w20_21_ratio) * dxdy).rgb;
  1269. sum += w18_19 * tex2D_linearize(tex, tex_uv - (18.0 + w18_19_ratio) * dxdy).rgb;
  1270. sum += w16_17 * tex2D_linearize(tex, tex_uv - (16.0 + w16_17_ratio) * dxdy).rgb;
  1271. sum += w14_15 * tex2D_linearize(tex, tex_uv - (14.0 + w14_15_ratio) * dxdy).rgb;
  1272. sum += w12_13 * tex2D_linearize(tex, tex_uv - (12.0 + w12_13_ratio) * dxdy).rgb;
  1273. sum += w10_11 * tex2D_linearize(tex, tex_uv - (10.0 + w10_11_ratio) * dxdy).rgb;
  1274. sum += w8_9 * tex2D_linearize(tex, tex_uv - (8.0 + w8_9_ratio) * dxdy).rgb;
  1275. sum += w6_7 * tex2D_linearize(tex, tex_uv - (6.0 + w6_7_ratio) * dxdy).rgb;
  1276. sum += w4_5 * tex2D_linearize(tex, tex_uv - (4.0 + w4_5_ratio) * dxdy).rgb;
  1277. sum += w2_3 * tex2D_linearize(tex, tex_uv - (2.0 + w2_3_ratio) * dxdy).rgb;
  1278. sum += w0_1 * tex2D_linearize(tex, tex_uv - w0_1_ratio * dxdy).rgb;
  1279. sum += w0_1 * tex2D_linearize(tex, tex_uv + w0_1_ratio * dxdy).rgb;
  1280. sum += w2_3 * tex2D_linearize(tex, tex_uv + (2.0 + w2_3_ratio) * dxdy).rgb;
  1281. sum += w4_5 * tex2D_linearize(tex, tex_uv + (4.0 + w4_5_ratio) * dxdy).rgb;
  1282. sum += w6_7 * tex2D_linearize(tex, tex_uv + (6.0 + w6_7_ratio) * dxdy).rgb;
  1283. sum += w8_9 * tex2D_linearize(tex, tex_uv + (8.0 + w8_9_ratio) * dxdy).rgb;
  1284. sum += w10_11 * tex2D_linearize(tex, tex_uv + (10.0 + w10_11_ratio) * dxdy).rgb;
  1285. sum += w12_13 * tex2D_linearize(tex, tex_uv + (12.0 + w12_13_ratio) * dxdy).rgb;
  1286. sum += w14_15 * tex2D_linearize(tex, tex_uv + (14.0 + w14_15_ratio) * dxdy).rgb;
  1287. sum += w16_17 * tex2D_linearize(tex, tex_uv + (16.0 + w16_17_ratio) * dxdy).rgb;
  1288. sum += w18_19 * tex2D_linearize(tex, tex_uv + (18.0 + w18_19_ratio) * dxdy).rgb;
  1289. sum += w20_21 * tex2D_linearize(tex, tex_uv + (20.0 + w20_21_ratio) * dxdy).rgb;
  1290. return sum * weight_sum_inv;
  1291. }
  1292. vec3 tex2Dblur3fast(sampler2D tex, vec2 tex_uv,
  1293. vec2 dxdy, float sigma)
  1294. {
  1295. // Requires: Same as tex2Dblur11()
  1296. // Returns: A 1D 3x Gaussian blurred texture lookup using 2 linear
  1297. // taps. It may be mipmapped depending on settings and dxdy.
  1298. // First get the texel weights and normalization factor as above.
  1299. float denom_inv = 0.5/(sigma*sigma);
  1300. float w0 = 1.0;
  1301. float w1 = exp(-1.0 * denom_inv);
  1302. float weight_sum_inv = 1.0 / (w0 + 2.0 * w1);
  1303. // Calculate combined weights and linear sample ratios between texel pairs.
  1304. // The center texel (with weight w0) is used twice, so halve its weight.
  1305. float w01 = w0 * 0.5 + w1;
  1306. float w01_ratio = w1/w01;
  1307. // Weights for all samples are the same, so just average them:
  1308. return 0.5 * (
  1309. tex2D_linearize(tex, tex_uv - w01_ratio * dxdy).rgb +
  1310. tex2D_linearize(tex, tex_uv + w01_ratio * dxdy).rgb);
  1311. }
  1312. vec3 tex2Dblur5fast(sampler2D tex, vec2 tex_uv,
  1313. vec2 dxdy, float sigma)
  1314. {
  1315. // Requires: Same as tex2Dblur11()
  1316. // Returns: A 1D 5x Gaussian blurred texture lookup using 1 nearest
  1317. // neighbor and 2 linear taps. It may be mipmapped depending
  1318. // on settings and dxdy.
  1319. // First get the texel weights and normalization factor as above.
  1320. float denom_inv = 0.5/(sigma*sigma);
  1321. float w0 = 1.0;
  1322. float w1 = exp(-1.0 * denom_inv);
  1323. float w2 = exp(-4.0 * denom_inv);
  1324. float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2));
  1325. // Calculate combined weights and linear sample ratios between texel pairs.
  1326. float w12 = w1 + w2;
  1327. float w12_ratio = w2/w12;
  1328. // Statically normalize weights, sum weighted samples, and return:
  1329. vec3 sum = vec3(0.0);
  1330. sum += w12 * tex2D_linearize(tex, tex_uv - (1.0 + w12_ratio) * dxdy).rgb;
  1331. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  1332. sum += w12 * tex2D_linearize(tex, tex_uv + (1.0 + w12_ratio) * dxdy).rgb;
  1333. return sum * weight_sum_inv;
  1334. }
  1335. vec3 tex2Dblur7fast(sampler2D tex, vec2 tex_uv,
  1336. vec2 dxdy, float sigma)
  1337. {
  1338. // Requires: Same as tex2Dblur11()
  1339. // Returns: A 1D 7x Gaussian blurred texture lookup using 4 linear
  1340. // taps. It may be mipmapped depending on settings and dxdy.
  1341. // First get the texel weights and normalization factor as above.
  1342. float denom_inv = 0.5/(sigma*sigma);
  1343. float w0 = 1.0;
  1344. float w1 = exp(-1.0 * denom_inv);
  1345. float w2 = exp(-4.0 * denom_inv);
  1346. float w3 = exp(-9.0 * denom_inv);
  1347. float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3));
  1348. // Calculate combined weights and linear sample ratios between texel pairs.
  1349. // The center texel (with weight w0) is used twice, so halve its weight.
  1350. float w01 = w0 * 0.5 + w1;
  1351. float w23 = w2 + w3;
  1352. float w01_ratio = w1/w01;
  1353. float w23_ratio = w3/w23;
  1354. // Statically normalize weights, sum weighted samples, and return:
  1355. vec3 sum = vec3(0.0);
  1356. sum += w23 * tex2D_linearize(tex, tex_uv - (2.0 + w23_ratio) * dxdy).rgb;
  1357. sum += w01 * tex2D_linearize(tex, tex_uv - w01_ratio * dxdy).rgb;
  1358. sum += w01 * tex2D_linearize(tex, tex_uv + w01_ratio * dxdy).rgb;
  1359. sum += w23 * tex2D_linearize(tex, tex_uv + (2.0 + w23_ratio) * dxdy).rgb;
  1360. return sum * weight_sum_inv;
  1361. }
  1362. //////////////////// ARBITRARILY RESIZABLE ONE-PASS BLURS ////////////////////
  1363. vec3 tex2Dblur3x3resize(sampler2D tex, vec2 tex_uv,
  1364. vec2 dxdy, float sigma)
  1365. {
  1366. // Requires: Global requirements must be met (see file description).
  1367. // Returns: A 3x3 Gaussian blurred mipmapped texture lookup of the
  1368. // resized input.
  1369. // Description:
  1370. // This is the only arbitrarily resizable one-pass blur; tex2Dblur5x5resize
  1371. // would perform like tex2Dblur9x9, MUCH slower than tex2Dblur5resize.
  1372. float denom_inv = 0.5/(sigma*sigma);
  1373. // Load each sample. We need all 3x3 samples. Quad-pixel communication
  1374. // won't help either: This should perform like tex2Dblur5x5, but sharing a
  1375. // 4x4 sample field would perform more like tex2Dblur8x8shared (worse).
  1376. vec2 sample4_uv = tex_uv;
  1377. vec2 dx = vec2(dxdy.x, 0.0);
  1378. vec2 dy = vec2(0.0, dxdy.y);
  1379. vec2 sample1_uv = sample4_uv - dy;
  1380. vec2 sample7_uv = sample4_uv + dy;
  1381. vec3 sample0 = tex2D_linearize(tex, sample1_uv - dx).rgb;
  1382. vec3 sample1 = tex2D_linearize(tex, sample1_uv).rgb;
  1383. vec3 sample2 = tex2D_linearize(tex, sample1_uv + dx).rgb;
  1384. vec3 sample3 = tex2D_linearize(tex, sample4_uv - dx).rgb;
  1385. vec3 sample4 = tex2D_linearize(tex, sample4_uv).rgb;
  1386. vec3 sample5 = tex2D_linearize(tex, sample4_uv + dx).rgb;
  1387. vec3 sample6 = tex2D_linearize(tex, sample7_uv - dx).rgb;
  1388. vec3 sample7 = tex2D_linearize(tex, sample7_uv).rgb;
  1389. vec3 sample8 = tex2D_linearize(tex, sample7_uv + dx).rgb;
  1390. // Statically compute Gaussian sample weights:
  1391. float w4 = 1.0;
  1392. float w1_3_5_7 = exp(-LENGTH_SQ(vec2(1.0, 0.0)) * denom_inv);
  1393. float w0_2_6_8 = exp(-LENGTH_SQ(vec2(1.0, 1.0)) * denom_inv);
  1394. float weight_sum_inv = 1.0/(w4 + 4.0 * (w1_3_5_7 + w0_2_6_8));
  1395. // Weight and sum the samples:
  1396. vec3 sum = w4 * sample4 +
  1397. w1_3_5_7 * (sample1 + sample3 + sample5 + sample7) +
  1398. w0_2_6_8 * (sample0 + sample2 + sample6 + sample8);
  1399. return sum * weight_sum_inv;
  1400. }
  1401. // Resizable one-pass blurs:
  1402. vec3 tex2Dblur3x3resize(sampler2D texture, vec2 tex_uv,
  1403. vec2 dxdy)
  1404. {
  1405. return tex2Dblur3x3resize(texture, tex_uv, dxdy, blur3_std_dev);
  1406. }
  1407. vec3 tex2Dblur9fast(sampler2D tex, vec2 tex_uv,
  1408. vec2 dxdy, float sigma)
  1409. {
  1410. // Requires: Same as tex2Dblur11()
  1411. // Returns: A 1D 9x Gaussian blurred texture lookup using 1 nearest
  1412. // neighbor and 4 linear taps. It may be mipmapped depending
  1413. // on settings and dxdy.
  1414. // First get the texel weights and normalization factor as above.
  1415. float denom_inv = 0.5/(sigma*sigma);
  1416. float w0 = 1.0;
  1417. float w1 = exp(-1.0 * denom_inv);
  1418. float w2 = exp(-4.0 * denom_inv);
  1419. float w3 = exp(-9.0 * denom_inv);
  1420. float w4 = exp(-16.0 * denom_inv);
  1421. float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3 + w4));
  1422. // Calculate combined weights and linear sample ratios between texel pairs.
  1423. float w12 = w1 + w2;
  1424. float w34 = w3 + w4;
  1425. float w12_ratio = w2/w12;
  1426. float w34_ratio = w4/w34;
  1427. // Statically normalize weights, sum weighted samples, and return:
  1428. vec3 sum = vec3(0.0);
  1429. sum += w34 * tex2D_linearize(tex, tex_uv - (3.0 + w34_ratio) * dxdy).rgb;
  1430. sum += w12 * tex2D_linearize(tex, tex_uv - (1.0 + w12_ratio) * dxdy).rgb;
  1431. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  1432. sum += w12 * tex2D_linearize(tex, tex_uv + (1.0 + w12_ratio) * dxdy).rgb;
  1433. sum += w34 * tex2D_linearize(tex, tex_uv + (3.0 + w34_ratio) * dxdy).rgb;
  1434. return sum * weight_sum_inv;
  1435. }
  1436. vec3 tex2Dblur9x9(sampler2D tex, vec2 tex_uv,
  1437. vec2 dxdy, float sigma)
  1438. {
  1439. // Perform a 1-pass 9x9 blur with 5x5 bilinear samples.
  1440. // Requires: Same as tex2Dblur9()
  1441. // Returns: A 9x9 Gaussian blurred mipmapped texture lookup composed of
  1442. // 5x5 carefully selected bilinear samples.
  1443. // Description:
  1444. // Perform a 1-pass 9x9 blur with 5x5 bilinear samples. Adjust the
  1445. // bilinear sample location to reflect the true Gaussian weights for each
  1446. // underlying texel. The following diagram illustrates the relative
  1447. // locations of bilinear samples. Each sample with the same number has the
  1448. // same weight (notice the symmetry). The letters a, b, c, d distinguish
  1449. // quadrants, and the letters U, D, L, R, C (up, down, left, right, center)
  1450. // distinguish 1D directions along the line containing the pixel center:
  1451. // 6a 5a 2U 5b 6b
  1452. // 4a 3a 1U 3b 4b
  1453. // 2L 1L 0C 1R 2R
  1454. // 4c 3c 1D 3d 4d
  1455. // 6c 5c 2D 5d 6d
  1456. // The following diagram illustrates the underlying equally spaced texels,
  1457. // named after the sample that accesses them and subnamed by their location
  1458. // within their 2x2, 2x1, 1x2, or 1x1 texel block:
  1459. // 6a4 6a3 5a4 5a3 2U2 5b3 5b4 6b3 6b4
  1460. // 6a2 6a1 5a2 5a1 2U1 5b1 5b2 6b1 6b2
  1461. // 4a4 4a3 3a4 3a3 1U2 3b3 3b4 4b3 4b4
  1462. // 4a2 4a1 3a2 3a1 1U1 3b1 3b2 4b1 4b2
  1463. // 2L2 2L1 1L2 1L1 0C1 1R1 1R2 2R1 2R2
  1464. // 4c2 4c1 3c2 3c1 1D1 3d1 3d2 4d1 4d2
  1465. // 4c4 4c3 3c4 3c3 1D2 3d3 3d4 4d3 4d4
  1466. // 6c2 6c1 5c2 5c1 2D1 5d1 5d2 6d1 6d2
  1467. // 6c4 6c3 5c4 5c3 2D2 5d3 5d4 6d3 6d4
  1468. // Note there is only one C texel and only two texels for each U, D, L, or
  1469. // R sample. The center sample is effectively a nearest neighbor sample,
  1470. // and the U/D/L/R samples use 1D linear filtering. All other texels are
  1471. // read with bilinear samples somewhere within their 2x2 texel blocks.
  1472. // COMPUTE TEXTURE COORDS:
  1473. // Statically compute sampling offsets within each 2x2 texel block, based
  1474. // on 1D sampling ratios between texels [1, 2] and [3, 4] texels away from
  1475. // the center, and reuse them independently for both dimensions. Compute
  1476. // these offsets based on the relative 1D Gaussian weights of the texels
  1477. // in question. (w1off means "Gaussian weight for the texel 1.0 texels
  1478. // away from the pixel center," etc.).
  1479. float denom_inv = 0.5/(sigma*sigma);
  1480. float w1off = exp(-1.0 * denom_inv);
  1481. float w2off = exp(-4.0 * denom_inv);
  1482. float w3off = exp(-9.0 * denom_inv);
  1483. float w4off = exp(-16.0 * denom_inv);
  1484. float texel1to2ratio = w2off/(w1off + w2off);
  1485. float texel3to4ratio = w4off/(w3off + w4off);
  1486. // Statically compute texel offsets from the fragment center to each
  1487. // bilinear sample in the bottom-right quadrant, including x-axis-aligned:
  1488. vec2 sample1R_texel_offset = vec2(1.0, 0.0) + vec2(texel1to2ratio, 0.0);
  1489. vec2 sample2R_texel_offset = vec2(3.0, 0.0) + vec2(texel3to4ratio, 0.0);
  1490. vec2 sample3d_texel_offset = vec2(1.0, 1.0) + vec2(texel1to2ratio, texel1to2ratio);
  1491. vec2 sample4d_texel_offset = vec2(3.0, 1.0) + vec2(texel3to4ratio, texel1to2ratio);
  1492. vec2 sample5d_texel_offset = vec2(1.0, 3.0) + vec2(texel1to2ratio, texel3to4ratio);
  1493. vec2 sample6d_texel_offset = vec2(3.0, 3.0) + vec2(texel3to4ratio, texel3to4ratio);
  1494. // CALCULATE KERNEL WEIGHTS FOR ALL SAMPLES:
  1495. // Statically compute Gaussian texel weights for the bottom-right quadrant.
  1496. // Read underscores as "and."
  1497. float w1R1 = w1off;
  1498. float w1R2 = w2off;
  1499. float w2R1 = w3off;
  1500. float w2R2 = w4off;
  1501. float w3d1 = exp(-LENGTH_SQ(vec2(1.0, 1.0)) * denom_inv);
  1502. float w3d2_3d3 = exp(-LENGTH_SQ(vec2(2.0, 1.0)) * denom_inv);
  1503. float w3d4 = exp(-LENGTH_SQ(vec2(2.0, 2.0)) * denom_inv);
  1504. float w4d1_5d1 = exp(-LENGTH_SQ(vec2(3.0, 1.0)) * denom_inv);
  1505. float w4d2_5d3 = exp(-LENGTH_SQ(vec2(4.0, 1.0)) * denom_inv);
  1506. float w4d3_5d2 = exp(-LENGTH_SQ(vec2(3.0, 2.0)) * denom_inv);
  1507. float w4d4_5d4 = exp(-LENGTH_SQ(vec2(4.0, 2.0)) * denom_inv);
  1508. float w6d1 = exp(-LENGTH_SQ(vec2(3.0, 3.0)) * denom_inv);
  1509. float w6d2_6d3 = exp(-LENGTH_SQ(vec2(4.0, 3.0)) * denom_inv);
  1510. float w6d4 = exp(-LENGTH_SQ(vec2(4.0, 4.0)) * denom_inv);
  1511. // Statically add texel weights in each sample to get sample weights:
  1512. float w0 = 1.0;
  1513. float w1 = w1R1 + w1R2;
  1514. float w2 = w2R1 + w2R2;
  1515. float w3 = w3d1 + 2.0 * w3d2_3d3 + w3d4;
  1516. float w4 = w4d1_5d1 + w4d2_5d3 + w4d3_5d2 + w4d4_5d4;
  1517. float w5 = w4;
  1518. float w6 = w6d1 + 2.0 * w6d2_6d3 + w6d4;
  1519. // Get the weight sum inverse (normalization factor):
  1520. float weight_sum_inv =
  1521. 1.0/(w0 + 4.0 * (w1 + w2 + w3 + w4 + w5 + w6));
  1522. // LOAD TEXTURE SAMPLES:
  1523. // Load all 25 samples (1 nearest, 8 linear, 16 bilinear) using symmetry:
  1524. vec2 mirror_x = vec2(-1.0, 1.0);
  1525. vec2 mirror_y = vec2(1.0, -1.0);
  1526. vec2 mirror_xy = vec2(-1.0, -1.0);
  1527. vec2 dxdy_mirror_x = dxdy * mirror_x;
  1528. vec2 dxdy_mirror_y = dxdy * mirror_y;
  1529. vec2 dxdy_mirror_xy = dxdy * mirror_xy;
  1530. // Sampling order doesn't seem to affect performance, so just be clear:
  1531. vec3 sample0C = tex2D_linearize(tex, tex_uv).rgb;
  1532. vec3 sample1R = tex2D_linearize(tex, tex_uv + dxdy * sample1R_texel_offset).rgb;
  1533. vec3 sample1D = tex2D_linearize(tex, tex_uv + dxdy * sample1R_texel_offset.yx).rgb;
  1534. vec3 sample1L = tex2D_linearize(tex, tex_uv - dxdy * sample1R_texel_offset).rgb;
  1535. vec3 sample1U = tex2D_linearize(tex, tex_uv - dxdy * sample1R_texel_offset.yx).rgb;
  1536. vec3 sample2R = tex2D_linearize(tex, tex_uv + dxdy * sample2R_texel_offset).rgb;
  1537. vec3 sample2D = tex2D_linearize(tex, tex_uv + dxdy * sample2R_texel_offset.yx).rgb;
  1538. vec3 sample2L = tex2D_linearize(tex, tex_uv - dxdy * sample2R_texel_offset).rgb;
  1539. vec3 sample2U = tex2D_linearize(tex, tex_uv - dxdy * sample2R_texel_offset.yx).rgb;
  1540. vec3 sample3d = tex2D_linearize(tex, tex_uv + dxdy * sample3d_texel_offset).rgb;
  1541. vec3 sample3c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample3d_texel_offset).rgb;
  1542. vec3 sample3b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample3d_texel_offset).rgb;
  1543. vec3 sample3a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample3d_texel_offset).rgb;
  1544. vec3 sample4d = tex2D_linearize(tex, tex_uv + dxdy * sample4d_texel_offset).rgb;
  1545. vec3 sample4c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample4d_texel_offset).rgb;
  1546. vec3 sample4b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample4d_texel_offset).rgb;
  1547. vec3 sample4a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample4d_texel_offset).rgb;
  1548. vec3 sample5d = tex2D_linearize(tex, tex_uv + dxdy * sample5d_texel_offset).rgb;
  1549. vec3 sample5c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample5d_texel_offset).rgb;
  1550. vec3 sample5b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample5d_texel_offset).rgb;
  1551. vec3 sample5a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample5d_texel_offset).rgb;
  1552. vec3 sample6d = tex2D_linearize(tex, tex_uv + dxdy * sample6d_texel_offset).rgb;
  1553. vec3 sample6c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample6d_texel_offset).rgb;
  1554. vec3 sample6b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample6d_texel_offset).rgb;
  1555. vec3 sample6a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample6d_texel_offset).rgb;
  1556. // SUM WEIGHTED SAMPLES:
  1557. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  1558. vec3 sum = w0 * sample0C;
  1559. sum += w1 * (sample1R + sample1D + sample1L + sample1U);
  1560. sum += w2 * (sample2R + sample2D + sample2L + sample2U);
  1561. sum += w3 * (sample3d + sample3c + sample3b + sample3a);
  1562. sum += w4 * (sample4d + sample4c + sample4b + sample4a);
  1563. sum += w5 * (sample5d + sample5c + sample5b + sample5a);
  1564. sum += w6 * (sample6d + sample6c + sample6b + sample6a);
  1565. return sum * weight_sum_inv;
  1566. }
  1567. vec3 tex2Dblur7x7(sampler2D tex, vec2 tex_uv,
  1568. vec2 dxdy, float sigma)
  1569. {
  1570. // Perform a 1-pass 7x7 blur with 5x5 bilinear samples.
  1571. // Requires: Same as tex2Dblur9()
  1572. // Returns: A 7x7 Gaussian blurred mipmapped texture lookup composed of
  1573. // 4x4 carefully selected bilinear samples.
  1574. // Description:
  1575. // First see the descriptions for tex2Dblur9x9() and tex2Dblur7(). This
  1576. // blur mixes concepts from both. The sample layout is as follows:
  1577. // 4a 3a 3b 4b
  1578. // 2a 1a 1b 2b
  1579. // 2c 1c 1d 2d
  1580. // 4c 3c 3d 4d
  1581. // The texel layout is as follows. Note that samples 3a/3b, 1a/1b, 1c/1d,
  1582. // and 3c/3d share a vertical column of texels, and samples 2a/2c, 1a/1c,
  1583. // 1b/1d, and 2b/2d share a horizontal row of texels (all sample1's share
  1584. // the center texel):
  1585. // 4a4 4a3 3a4 3ab3 3b4 4b3 4b4
  1586. // 4a2 4a1 3a2 3ab1 3b2 4b1 4b2
  1587. // 2a4 2a3 1a4 1ab3 1b4 2b3 2b4
  1588. // 2ac2 2ac1 1ac2 1* 1bd2 2bd1 2bd2
  1589. // 2c4 2c3 1c4 1cd3 1d4 2d3 2d4
  1590. // 4c2 4c1 3c2 3cd1 3d2 4d1 4d2
  1591. // 4c4 4c3 3c4 3cd3 3d4 4d3 4d4
  1592. // COMPUTE TEXTURE COORDS:
  1593. // Statically compute bilinear sampling offsets (details in tex2Dblur9x9).
  1594. float denom_inv = 0.5/(sigma*sigma);
  1595. float w0off = 1.0;
  1596. float w1off = exp(-1.0 * denom_inv);
  1597. float w2off = exp(-4.0 * denom_inv);
  1598. float w3off = exp(-9.0 * denom_inv);
  1599. float texel0to1ratio = w1off/(w0off * 0.5 + w1off);
  1600. float texel2to3ratio = w3off/(w2off + w3off);
  1601. // Statically compute texel offsets from the fragment center to each
  1602. // bilinear sample in the bottom-right quadrant, including axis-aligned:
  1603. vec2 sample1d_texel_offset = vec2(texel0to1ratio, texel0to1ratio);
  1604. vec2 sample2d_texel_offset = vec2(2.0, 0.0) + vec2(texel2to3ratio, texel0to1ratio);
  1605. vec2 sample3d_texel_offset = vec2(0.0, 2.0) + vec2(texel0to1ratio, texel2to3ratio);
  1606. vec2 sample4d_texel_offset = vec2(2.0, 2.0) + vec2(texel2to3ratio, texel2to3ratio);
  1607. // CALCULATE KERNEL WEIGHTS FOR ALL SAMPLES:
  1608. // Statically compute Gaussian texel weights for the bottom-right quadrant.
  1609. // Read underscores as "and."
  1610. float w1abcd = 1.0;
  1611. float w1bd2_1cd3 = exp(-LENGTH_SQ(vec2(1.0, 0.0)) * denom_inv);
  1612. float w2bd1_3cd1 = exp(-LENGTH_SQ(vec2(2.0, 0.0)) * denom_inv);
  1613. float w2bd2_3cd2 = exp(-LENGTH_SQ(vec2(3.0, 0.0)) * denom_inv);
  1614. float w1d4 = exp(-LENGTH_SQ(vec2(1.0, 1.0)) * denom_inv);
  1615. float w2d3_3d2 = exp(-LENGTH_SQ(vec2(2.0, 1.0)) * denom_inv);
  1616. float w2d4_3d4 = exp(-LENGTH_SQ(vec2(3.0, 1.0)) * denom_inv);
  1617. float w4d1 = exp(-LENGTH_SQ(vec2(2.0, 2.0)) * denom_inv);
  1618. float w4d2_4d3 = exp(-LENGTH_SQ(vec2(3.0, 2.0)) * denom_inv);
  1619. float w4d4 = exp(-LENGTH_SQ(vec2(3.0, 3.0)) * denom_inv);
  1620. // Statically add texel weights in each sample to get sample weights.
  1621. // Split weights for shared texels between samples sharing them:
  1622. float w1 = w1abcd * 0.25 + w1bd2_1cd3 + w1d4;
  1623. float w2_3 = (w2bd1_3cd1 + w2bd2_3cd2) * 0.5 + w2d3_3d2 + w2d4_3d4;
  1624. float w4 = w4d1 + 2.0 * w4d2_4d3 + w4d4;
  1625. // Get the weight sum inverse (normalization factor):
  1626. float weight_sum_inv =
  1627. 1.0/(4.0 * (w1 + 2.0 * w2_3 + w4));
  1628. // LOAD TEXTURE SAMPLES:
  1629. // Load all 16 samples using symmetry:
  1630. vec2 mirror_x = vec2(-1.0, 1.0);
  1631. vec2 mirror_y = vec2(1.0, -1.0);
  1632. vec2 mirror_xy = vec2(-1.0, -1.0);
  1633. vec2 dxdy_mirror_x = dxdy * mirror_x;
  1634. vec2 dxdy_mirror_y = dxdy * mirror_y;
  1635. vec2 dxdy_mirror_xy = dxdy * mirror_xy;
  1636. vec3 sample1a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample1d_texel_offset).rgb;
  1637. vec3 sample2a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample2d_texel_offset).rgb;
  1638. vec3 sample3a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample3d_texel_offset).rgb;
  1639. vec3 sample4a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample4d_texel_offset).rgb;
  1640. vec3 sample1b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample1d_texel_offset).rgb;
  1641. vec3 sample2b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample2d_texel_offset).rgb;
  1642. vec3 sample3b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample3d_texel_offset).rgb;
  1643. vec3 sample4b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample4d_texel_offset).rgb;
  1644. vec3 sample1c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample1d_texel_offset).rgb;
  1645. vec3 sample2c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample2d_texel_offset).rgb;
  1646. vec3 sample3c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample3d_texel_offset).rgb;
  1647. vec3 sample4c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample4d_texel_offset).rgb;
  1648. vec3 sample1d = tex2D_linearize(tex, tex_uv + dxdy * sample1d_texel_offset).rgb;
  1649. vec3 sample2d = tex2D_linearize(tex, tex_uv + dxdy * sample2d_texel_offset).rgb;
  1650. vec3 sample3d = tex2D_linearize(tex, tex_uv + dxdy * sample3d_texel_offset).rgb;
  1651. vec3 sample4d = tex2D_linearize(tex, tex_uv + dxdy * sample4d_texel_offset).rgb;
  1652. // SUM WEIGHTED SAMPLES:
  1653. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  1654. vec3 sum = vec3(0.0);
  1655. sum += w1 * (sample1a + sample1b + sample1c + sample1d);
  1656. sum += w2_3 * (sample2a + sample2b + sample2c + sample2d);
  1657. sum += w2_3 * (sample3a + sample3b + sample3c + sample3d);
  1658. sum += w4 * (sample4a + sample4b + sample4c + sample4d);
  1659. return sum * weight_sum_inv;
  1660. }
  1661. vec3 tex2Dblur5x5(sampler2D tex, vec2 tex_uv,
  1662. vec2 dxdy, float sigma)
  1663. {
  1664. // Perform a 1-pass 5x5 blur with 3x3 bilinear samples.
  1665. // Requires: Same as tex2Dblur9()
  1666. // Returns: A 5x5 Gaussian blurred mipmapped texture lookup composed of
  1667. // 3x3 carefully selected bilinear samples.
  1668. // Description:
  1669. // First see the description for tex2Dblur9x9(). This blur uses the same
  1670. // concept and sample/texel locations except on a smaller scale. Samples:
  1671. // 2a 1U 2b
  1672. // 1L 0C 1R
  1673. // 2c 1D 2d
  1674. // Texels:
  1675. // 2a4 2a3 1U2 2b3 2b4
  1676. // 2a2 2a1 1U1 2b1 2b2
  1677. // 1L2 1L1 0C1 1R1 1R2
  1678. // 2c2 2c1 1D1 2d1 2d2
  1679. // 2c4 2c3 1D2 2d3 2d4
  1680. // COMPUTE TEXTURE COORDS:
  1681. // Statically compute bilinear sampling offsets (details in tex2Dblur9x9).
  1682. float denom_inv = 0.5/(sigma*sigma);
  1683. float w1off = exp(-1.0 * denom_inv);
  1684. float w2off = exp(-4.0 * denom_inv);
  1685. float texel1to2ratio = w2off/(w1off + w2off);
  1686. // Statically compute texel offsets from the fragment center to each
  1687. // bilinear sample in the bottom-right quadrant, including x-axis-aligned:
  1688. vec2 sample1R_texel_offset = vec2(1.0, 0.0) + vec2(texel1to2ratio, 0.0);
  1689. vec2 sample2d_texel_offset = vec2(1.0, 1.0) + vec2(texel1to2ratio, texel1to2ratio);
  1690. // CALCULATE KERNEL WEIGHTS FOR ALL SAMPLES:
  1691. // Statically compute Gaussian texel weights for the bottom-right quadrant.
  1692. // Read underscores as "and."
  1693. float w1R1 = w1off;
  1694. float w1R2 = w2off;
  1695. float w2d1 = exp(-LENGTH_SQ(vec2(1.0, 1.0)) * denom_inv);
  1696. float w2d2_3 = exp(-LENGTH_SQ(vec2(2.0, 1.0)) * denom_inv);
  1697. float w2d4 = exp(-LENGTH_SQ(vec2(2.0, 2.0)) * denom_inv);
  1698. // Statically add texel weights in each sample to get sample weights:
  1699. float w0 = 1.0;
  1700. float w1 = w1R1 + w1R2;
  1701. float w2 = w2d1 + 2.0 * w2d2_3 + w2d4;
  1702. // Get the weight sum inverse (normalization factor):
  1703. float weight_sum_inv = 1.0/(w0 + 4.0 * (w1 + w2));
  1704. // LOAD TEXTURE SAMPLES:
  1705. // Load all 9 samples (1 nearest, 4 linear, 4 bilinear) using symmetry:
  1706. vec2 mirror_x = vec2(-1.0, 1.0);
  1707. vec2 mirror_y = vec2(1.0, -1.0);
  1708. vec2 mirror_xy = vec2(-1.0, -1.0);
  1709. vec2 dxdy_mirror_x = dxdy * mirror_x;
  1710. vec2 dxdy_mirror_y = dxdy * mirror_y;
  1711. vec2 dxdy_mirror_xy = dxdy * mirror_xy;
  1712. vec3 sample0C = tex2D_linearize(tex, tex_uv).rgb;
  1713. vec3 sample1R = tex2D_linearize(tex, tex_uv + dxdy * sample1R_texel_offset).rgb;
  1714. vec3 sample1D = tex2D_linearize(tex, tex_uv + dxdy * sample1R_texel_offset.yx).rgb;
  1715. vec3 sample1L = tex2D_linearize(tex, tex_uv - dxdy * sample1R_texel_offset).rgb;
  1716. vec3 sample1U = tex2D_linearize(tex, tex_uv - dxdy * sample1R_texel_offset.yx).rgb;
  1717. vec3 sample2d = tex2D_linearize(tex, tex_uv + dxdy * sample2d_texel_offset).rgb;
  1718. vec3 sample2c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample2d_texel_offset).rgb;
  1719. vec3 sample2b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample2d_texel_offset).rgb;
  1720. vec3 sample2a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample2d_texel_offset).rgb;
  1721. // SUM WEIGHTED SAMPLES:
  1722. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  1723. vec3 sum = w0 * sample0C;
  1724. sum += w1 * (sample1R + sample1D + sample1L + sample1U);
  1725. sum += w2 * (sample2a + sample2b + sample2c + sample2d);
  1726. return sum * weight_sum_inv;
  1727. }
  1728. vec3 tex2Dblur3x3(sampler2D tex, vec2 tex_uv,
  1729. vec2 dxdy, float sigma)
  1730. {
  1731. // Perform a 1-pass 3x3 blur with 5x5 bilinear samples.
  1732. // Requires: Same as tex2Dblur9()
  1733. // Returns: A 3x3 Gaussian blurred mipmapped texture lookup composed of
  1734. // 2x2 carefully selected bilinear samples.
  1735. // Description:
  1736. // First see the descriptions for tex2Dblur9x9() and tex2Dblur7(). This
  1737. // blur mixes concepts from both. The sample layout is as follows:
  1738. // 0a 0b
  1739. // 0c 0d
  1740. // The texel layout is as follows. Note that samples 0a/0b and 0c/0d share
  1741. // a vertical column of texels, and samples 0a/0c and 0b/0d share a
  1742. // horizontal row of texels (all samples share the center texel):
  1743. // 0a3 0ab2 0b3
  1744. // 0ac1 0*0 0bd1
  1745. // 0c3 0cd2 0d3
  1746. // COMPUTE TEXTURE COORDS:
  1747. // Statically compute bilinear sampling offsets (details in tex2Dblur9x9).
  1748. float denom_inv = 0.5/(sigma*sigma);
  1749. float w0off = 1.0;
  1750. float w1off = exp(-1.0 * denom_inv);
  1751. float texel0to1ratio = w1off/(w0off * 0.5 + w1off);
  1752. // Statically compute texel offsets from the fragment center to each
  1753. // bilinear sample in the bottom-right quadrant, including axis-aligned:
  1754. vec2 sample0d_texel_offset = vec2(texel0to1ratio, texel0to1ratio);
  1755. // LOAD TEXTURE SAMPLES:
  1756. // Load all 4 samples using symmetry:
  1757. vec2 mirror_x = vec2(-1.0, 1.0);
  1758. vec2 mirror_y = vec2(1.0, -1.0);
  1759. vec2 mirror_xy = vec2(-1.0, -1.0);
  1760. vec2 dxdy_mirror_x = dxdy * mirror_x;
  1761. vec2 dxdy_mirror_y = dxdy * mirror_y;
  1762. vec2 dxdy_mirror_xy = dxdy * mirror_xy;
  1763. vec3 sample0a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample0d_texel_offset).rgb;
  1764. vec3 sample0b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample0d_texel_offset).rgb;
  1765. vec3 sample0c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample0d_texel_offset).rgb;
  1766. vec3 sample0d = tex2D_linearize(tex, tex_uv + dxdy * sample0d_texel_offset).rgb;
  1767. // SUM WEIGHTED SAMPLES:
  1768. // Weights for all samples are the same, so just average them:
  1769. return 0.25 * (sample0a + sample0b + sample0c + sample0d);
  1770. }
  1771. vec3 tex2Dblur9fast(sampler2D tex, vec2 tex_uv,
  1772. vec2 dxdy)
  1773. {
  1774. return tex2Dblur9fast(tex, tex_uv, dxdy, blur9_std_dev);
  1775. }
  1776. vec3 tex2Dblur17fast(sampler2D texture, vec2 tex_uv,
  1777. vec2 dxdy)
  1778. {
  1779. return tex2Dblur17fast(texture, tex_uv, dxdy, blur17_std_dev);
  1780. }
  1781. vec3 tex2Dblur25fast(sampler2D texture, vec2 tex_uv,
  1782. vec2 dxdy)
  1783. {
  1784. return tex2Dblur25fast(texture, tex_uv, dxdy, blur25_std_dev);
  1785. }
  1786. vec3 tex2Dblur43fast(sampler2D texture, vec2 tex_uv,
  1787. vec2 dxdy)
  1788. {
  1789. return tex2Dblur43fast(texture, tex_uv, dxdy, blur43_std_dev);
  1790. }
  1791. vec3 tex2Dblur31fast(sampler2D texture, vec2 tex_uv,
  1792. vec2 dxdy)
  1793. {
  1794. return tex2Dblur31fast(texture, tex_uv, dxdy, blur31_std_dev);
  1795. }
  1796. vec3 tex2Dblur3fast(sampler2D texture, vec2 tex_uv,
  1797. vec2 dxdy)
  1798. {
  1799. return tex2Dblur3fast(texture, tex_uv, dxdy, blur3_std_dev);
  1800. }
  1801. vec3 tex2Dblur3x3(sampler2D texture, vec2 tex_uv,
  1802. vec2 dxdy)
  1803. {
  1804. return tex2Dblur3x3(texture, tex_uv, dxdy, blur3_std_dev);
  1805. }
  1806. vec3 tex2Dblur5fast(sampler2D texture, vec2 tex_uv,
  1807. vec2 dxdy)
  1808. {
  1809. return tex2Dblur5fast(texture, tex_uv, dxdy, blur5_std_dev);
  1810. }
  1811. vec3 tex2Dblur5resize(sampler2D texture, vec2 tex_uv,
  1812. vec2 dxdy)
  1813. {
  1814. return tex2Dblur5resize(texture, tex_uv, dxdy, blur5_std_dev);
  1815. }
  1816. vec3 tex2Dblur3resize(sampler2D texture, vec2 tex_uv,
  1817. vec2 dxdy)
  1818. {
  1819. return tex2Dblur3resize(texture, tex_uv, dxdy, blur3_std_dev);
  1820. }
  1821. vec3 tex2Dblur5x5(sampler2D texture, vec2 tex_uv,
  1822. vec2 dxdy)
  1823. {
  1824. return tex2Dblur5x5(texture, tex_uv, dxdy, blur5_std_dev);
  1825. }
  1826. vec3 tex2Dblur7resize(sampler2D texture, vec2 tex_uv,
  1827. vec2 dxdy)
  1828. {
  1829. return tex2Dblur7resize(texture, tex_uv, dxdy, blur7_std_dev);
  1830. }
  1831. vec3 tex2Dblur7fast(sampler2D texture, vec2 tex_uv,
  1832. vec2 dxdy)
  1833. {
  1834. return tex2Dblur7fast(texture, tex_uv, dxdy, blur7_std_dev);
  1835. }
  1836. vec3 tex2Dblur7x7(sampler2D texture, vec2 tex_uv,
  1837. vec2 dxdy)
  1838. {
  1839. return tex2Dblur7x7(texture, tex_uv, dxdy, blur7_std_dev);
  1840. }
  1841. vec3 tex2Dblur9resize(sampler2D texture, vec2 tex_uv,
  1842. vec2 dxdy)
  1843. {
  1844. return tex2Dblur9resize(texture, tex_uv, dxdy, blur9_std_dev);
  1845. }
  1846. vec3 tex2Dblur9x9(sampler2D texture, vec2 tex_uv,
  1847. vec2 dxdy)
  1848. {
  1849. return tex2Dblur9x9(texture, tex_uv, dxdy, blur9_std_dev);
  1850. }
  1851. vec3 tex2Dblur11resize(sampler2D texture, vec2 tex_uv,
  1852. vec2 dxdy)
  1853. {
  1854. return tex2Dblur11resize(texture, tex_uv, dxdy, blur11_std_dev);
  1855. }
  1856. vec3 tex2Dblur11fast(sampler2D texture, vec2 tex_uv,
  1857. vec2 dxdy)
  1858. {
  1859. return tex2Dblur11fast(texture, tex_uv, dxdy, blur11_std_dev);
  1860. }
  1861. #endif // BLUR_FUNCTIONS_H
  1862. #define InputSize sourceSize[0].xy
  1863. #define TextureSize sourceSize[0].xy
  1864. #define OutputSize targetSize.xy
  1865. void main() {
  1866. gl_Position = position;
  1867. vTexCoord = texCoord;
  1868. // Get the uv sample distance between output pixels. Blurs are not generic
  1869. // Gaussian resizers, and correct blurs require:
  1870. // 1.) OutputSize == InputSize * 2^m, where m is an integer <= 0.
  1871. // 2.) mipmap_inputN = "true" for this pass in the preset if m != 0
  1872. // 3.) filter_linearN = "true" except for 1x scale nearest neighbor blurs
  1873. // Gaussian resizers would upsize using the distance between input texels
  1874. // (not output pixels), but we avoid this and consistently blur at the
  1875. // destination size. Otherwise, combining statically calculated weights
  1876. // with bilinear sample exploitation would result in terrible artifacts.
  1877. vec2 dxdy_scale = InputSize/OutputSize;
  1878. vec2 dxdy = dxdy_scale/TextureSize;
  1879. // This blur is vertical-only, so zero out the horizontal offset:
  1880. blur_dxdy = vec2(0.0, dxdy.y);
  1881. }