*/}}

ourshader.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Our Paint: A light weight GPU powered painting program.
  3. * Copyright (C) 2022-2023 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "ourpaint.h"
  19. const char OUR_SHADER_VERSION_430[]="#version 430\n#define WORKGROUP_SIZE 32";
  20. const char OUR_SHADER_VERSION_320ES[]="#version 320 es\n#define OUR_GLES\n#define WORKGROUP_SIZE 16";
  21. const char OUR_CANVAS_SHADER[]=R"(
  22. precision highp uimage2D;
  23. precision highp float;
  24. precision highp int;
  25. layout(local_size_x = WORKGROUP_SIZE, local_size_y = WORKGROUP_SIZE, local_size_z = 1) in;
  26. #ifdef OUR_GLES
  27. layout(r32ui, binding = 0) uniform uimage2D img;
  28. layout(r32ui, binding = 1) coherent uniform uimage2D smudge_buckets;
  29. #else
  30. layout(rgba16ui, binding = 0) uniform uimage2D img;
  31. layout(rgba16ui, binding = 1) coherent uniform uimage2D smudge_buckets;
  32. #endif
  33. uniform int uCanvasType;
  34. uniform int uCanvasRandom;
  35. uniform float uCanvasFactor;
  36. uniform ivec2 uImageOffset;
  37. uniform ivec2 uBrushCorner;
  38. uniform vec2 uBrushCenter;
  39. uniform float uBrushSize;
  40. uniform float uBrushHardness;
  41. uniform float uBrushSmudge;
  42. uniform float uBrushSlender;
  43. uniform float uBrushAngle;
  44. uniform vec2 uBrushDirection;
  45. uniform float uBrushForce;
  46. uniform float uBrushGunkyness;
  47. uniform float uBrushRecentness;
  48. uniform vec4 uBrushColor;
  49. uniform vec4 uBackgroundColor;
  50. uniform int uBrushErasing;
  51. uniform int uBrushMix;
  52. #ifdef OUR_GLES
  53. uniform int uBrushRoutineSelectionES;
  54. uniform int uMixRoutineSelectionES;
  55. vec4 cunpack(uint d){
  56. return vec4(float(d&0xFFu)/255.,float((d>>8u)&0xFFu)/255.,float((d>>16u)&0xFFu)/255.,float((d>>24u)&0xFFu)/255.);
  57. }
  58. uvec4 cpack(vec4 c){
  59. uint v= uint(uint(c.r*255.) | (uint(c.g*255.)<<8u) | (uint(c.b*255.)<<16u) | (uint(c.a*255.)<<24u));
  60. return uvec4(v,v,v,v);
  61. }
  62. #define OurImageLoad(img, p) \
  63. (cunpack(imageLoad(img,p).x))
  64. #define OurImageStore(img, p, color) \
  65. imageStore(img,p,cpack(color))
  66. #else
  67. #define OurImageLoad(img, p) \
  68. (vec4(imageLoad(img,p))/65535.)
  69. #define OurImageStore(img, p, color) \
  70. imageStore(img,p,uvec4(vec4(color)*65535.))
  71. #endif
  72. const vec4 p1_22=vec4(1.0/2.2,1.0/2.2,1.0/2.2,1.0/2.2);
  73. const vec4 p22=vec4(2.2,2.2,2.2,2.2);
  74. const float WGM_EPSILON=0.001f;
  75. const float T_MATRIX_SMALL[30] = float[30](0.026595621243689,0.049779426257903,0.022449850859496,-0.218453689278271
  76. ,-0.256894883201278,0.445881722194840,0.772365886289756,0.194498761382537
  77. ,0.014038157587820,0.007687264480513
  78. ,-0.032601672674412,-0.061021043498478,-0.052490001018404
  79. ,0.206659098273522,0.572496335158169,0.317837248815438,-0.021216624031211
  80. ,-0.019387668756117,-0.001521339050858,-0.000835181622534
  81. ,0.339475473216284,0.635401374177222,0.771520797089589,0.113222640692379
  82. ,-0.055251113343776,-0.048222578468680,-0.012966666339586
  83. ,-0.001523814504223,-0.000094718948810,-0.000051604594741);
  84. const float spectral_r_small[10] = float[10](0.009281362787953,0.009732627042016,0.011254252737167,0.015105578649573
  85. ,0.024797924177217,0.083622585502406,0.977865045723212,1.000000000000000
  86. ,0.999961046144372,0.999999992756822);
  87. const float spectral_g_small[10] = float[10](0.002854127435775,0.003917589679914,0.012132151699187,0.748259205918013
  88. ,1.000000000000000,0.865695937531795,0.037477469241101,0.022816789725717
  89. ,0.021747419446456,0.021384940572308);
  90. const float spectral_b_small[10] = float[10](0.537052150373386,0.546646402401469,0.575501819073983,0.258778829633924
  91. ,0.041709923751716,0.012662638828324,0.007485593127390,0.006766900622462
  92. ,0.006699764779016,0.006676219883241);
  93. void rgb_to_spectral (vec3 rgb, out float spectral_[10]) {
  94. float offset = 1.0 - WGM_EPSILON;
  95. float r = rgb.r * offset + WGM_EPSILON;
  96. float g = rgb.g * offset + WGM_EPSILON;
  97. float b = rgb.b * offset + WGM_EPSILON;
  98. float spec_r[10] = float[10](0.,0.,0.,0.,0.,0.,0.,0.,0.,0.); for (int i=0; i < 10; i++) {spec_r[i] = spectral_r_small[i] * r;}
  99. float spec_g[10] = float[10](0.,0.,0.,0.,0.,0.,0.,0.,0.,0.); for (int i=0; i < 10; i++) {spec_g[i] = spectral_g_small[i] * g;}
  100. float spec_b[10] = float[10](0.,0.,0.,0.,0.,0.,0.,0.,0.,0.); for (int i=0; i < 10; i++) {spec_b[i] = spectral_b_small[i] * b;}
  101. for (int i=0; i<10; i++) {spectral_[i] = spec_r[i] + spec_g[i] + spec_b[i];}
  102. }
  103. vec3 spectral_to_rgb (float spectral[10]) {
  104. float offset = 1.0 - WGM_EPSILON;
  105. // We need this tmp. array to allow auto vectorization. <-- How about on GPU?
  106. float tmp[3] = float[3](0.,0.,0.);
  107. for (int i=0; i<10; i++) {
  108. tmp[0] += T_MATRIX_SMALL[i] * spectral[i];
  109. tmp[1] += T_MATRIX_SMALL[10+i] * spectral[i];
  110. tmp[2] += T_MATRIX_SMALL[20+i] * spectral[i];
  111. }
  112. vec3 rgb_;
  113. for (int i=0; i<3; i++) {rgb_[i] = clamp((tmp[i] - WGM_EPSILON) / offset, 0.0f, 1.0f);}
  114. return rgb_;
  115. }
  116. vec2 hash( vec2 p ){
  117. p = vec2( dot(p,vec2(127.1,311.7)), dot(p,vec2(269.5,183.3)) );
  118. return -1.0 + 2.0*fract(sin(p)*43758.5453123);
  119. }
  120. float rand(vec2 co){
  121. return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
  122. }
  123. float noise(in vec2 p){ // from iq
  124. const float K1 = 0.366025404; // (sqrt(3)-1)/2;
  125. const float K2 = 0.211324865; // (3-sqrt(3))/6;
  126. vec2 i = floor( p + (p.x+p.y)*K1 );
  127. vec2 a = p - i + (i.x+i.y)*K2;
  128. float m = step(a.y,a.x);
  129. vec2 o = vec2(m,1.0-m);
  130. vec2 b = a - o + K2;
  131. vec2 c = a - 1.0 + 2.0*K2;
  132. vec3 h = max( 0.5-vec3(dot(a,a), dot(b,b), dot(c,c) ), 0.0 );
  133. vec3 n = h*h*h*h*vec3( dot(a,hash(i+0.0)), dot(b,hash(i+o)), dot(c,hash(i+1.0)));
  134. return dot( n, vec3(70.0) );
  135. }
  136. #define HEIGHT_STRAND(x,y) abs(fract(x)-.5)<.48? \
  137. (.4+.2*sin(3.14*(y+ceil(x))))* \
  138. ((max(abs(sin(3.14*x*2.)+0.2),abs(sin(3.14*x*2.)-0.2))+2.*abs(sin(3.14*x)))/2.+0.5):0.1
  139. #define PATTERN_CANVAS(x,y) \
  140. (max(HEIGHT_STRAND((x),(y)),HEIGHT_STRAND(-(y),(x))))
  141. float HEIGHT_CANVAS(float x,float y){
  142. if(uCanvasType == 1){
  143. return PATTERN_CANVAS(x,y);
  144. }else if(uCanvasType == 2){
  145. vec2 uv=vec2(x,y); float f; uv*=0.1; // from iq
  146. f = 0.2*noise( uv ); uv*=5.;
  147. f += 0.6*noise( uv ); uv*=3.;
  148. f += 0.5*noise( uv );
  149. f = 0.55 + 0.55*f;
  150. return pow(f,0.5);
  151. }
  152. return 1.;
  153. }
  154. float SampleCanvas(vec2 U, vec2 dir,float rfac, float force, float gunky){
  155. if(uCanvasType==0 || abs(gunky)<1.e-2){ return rfac; }
  156. U+=vec2(uImageOffset); U/=20.3; U.x=U.x+rand(U)/10.; U.y=U.y+rand(U)/10.;
  157. mat2 m = mat2(1.6,1.2,-1.2,1.6); vec2 _uv=U; _uv.x+=float(uCanvasRandom%65535)/174.41; _uv.y+=float(uCanvasRandom%65535)/439.87; _uv/=500.;
  158. U.x+=noise(_uv)*2.1; _uv = m*_uv; U.x+=noise(_uv)*0.71;
  159. _uv.y+=365.404;
  160. U.y+=noise(_uv)*1.9; _uv = m*_uv; U.y+=noise(_uv)*0.83;
  161. float d=0.1;
  162. float h=HEIGHT_CANVAS(U.x,U.y);
  163. float hr=HEIGHT_CANVAS(U.x+d,U.y);
  164. float hu=HEIGHT_CANVAS(U.x,U.y+d);
  165. vec3 vx=normalize(vec3(d,0,hr)-vec3(0,0,h)),vy=normalize(vec3(0,d,hu)-vec3(0,0,h)),vz=cross(vx,vy);
  166. float useforce=force*rfac;
  167. float scrape=dot(normalize(vz),vec3(-normalize(dir).xy,0))*mix(0.3,1.,useforce);
  168. float top=h-(1.-pow(useforce,1.5)*2.); float tophard=smoothstep(0.4,0.6,top);
  169. float fac=(gunky>=0.)?mix(mix(1.,top,gunky),tophard,gunky):mix(1.,1.-h,-gunky*0.8);
  170. fac=max(fac,scrape*clamp(gunky,0.,1.));
  171. fac=clamp(fac,0.,1.);
  172. fac*=rfac;
  173. return mix(rfac,fac,uCanvasFactor);
  174. }
  175. #ifndef OUR_GLES
  176. subroutine vec4 MixRoutines(vec4 a, vec4 b, float fac_a);
  177. #endif
  178. #ifndef OUR_GLES
  179. subroutine(MixRoutines)
  180. #endif
  181. vec4 DoMixNormal(vec4 a, vec4 b, float fac_a){
  182. return mix(a,b,1.0f-fac_a);
  183. }
  184. #ifndef OUR_GLES
  185. subroutine(MixRoutines)
  186. #endif
  187. vec4 DoMixSpectral(vec4 a, vec4 b, float fac_a){
  188. vec4 result = vec4(0,0,0,0);
  189. result.a=mix(a.a,b.a,1.0f-fac_a);
  190. float spec_a[10] = float[10](0.,0.,0.,0.,0.,0.,0.,0.,0.,0.); rgb_to_spectral(a.rgb, spec_a);
  191. float spec_b[10] = float[10](0.,0.,0.,0.,0.,0.,0.,0.,0.,0.); rgb_to_spectral(b.rgb, spec_b);
  192. float spectralmix[10] = float[10](0.,0.,0.,0.,0.,0.,0.,0.,0.,0.);
  193. for (int i=0; i < 10; i++) { spectralmix[i] = pow(spec_a[i], fac_a) * pow(spec_b[i], 1.0f-fac_a); }
  194. result.rgb=spectral_to_rgb(spectralmix);
  195. return result;
  196. }
  197. #ifdef OUR_GLES
  198. vec4 uMixRoutineSelection(vec4 a, vec4 b, float fac_a){
  199. if(uMixRoutineSelectionES==0){ return DoMixNormal(a,b,fac_a); }
  200. else{ return DoMixSpectral(a,b,fac_a); }
  201. }
  202. #else
  203. subroutine uniform MixRoutines uMixRoutineSelection;
  204. #endif
  205. vec4 spectral_mix(vec4 a, vec4 b, float fac_a){
  206. return uMixRoutineSelection(a,b,fac_a);
  207. }
  208. vec4 spectral_mix_unpre(vec4 colora, vec4 colorb, float fac){
  209. vec4 ca=(colora.a==0.0f)?colora:vec4(colora.rgb/colora.a,colora.a);
  210. vec4 cb=(colorb.a==0.0f)?colorb:vec4(colorb.rgb/colorb.a,colorb.a);
  211. float af=colora.a*(1.0f-fac);
  212. float aa=af/(af+fac*colorb.a+0.000001);
  213. vec4 result=spectral_mix(ca,cb,aa);
  214. result.a=mix(colora.a,colorb.a,fac);
  215. return vec4(result.rgb*result.a,result.a);
  216. }
  217. float atan2(in float y, in float x){
  218. bool s = (abs(x) > abs(y)); return mix(3.1415926535/2.0 - atan(x,y), atan(y,x), s);
  219. }
  220. vec2 rotate(vec2 v, float angle) {
  221. float s = sin(angle); float c = cos(angle);
  222. return mat2(c,-s,s,c) * v;
  223. }
  224. float brightness(vec4 color) {
  225. return color.r*0.2126+color.b*0.7152+color.g*0.0722;
  226. }
  227. vec4 mix_over(vec4 colora, vec4 colorb){
  228. vec4 a=(colora.a==0.0f)?colora:vec4(colora.rgb/colora.a,colora.a);
  229. vec4 b=(colorb.a==0.0f)?colorb:vec4(colorb.rgb/colorb.a,colorb.a);
  230. vec4 m=vec4(0,0,0,0); float aa=colora.a/(colora.a+(1.0f-colora.a)*colorb.a+0.0001);
  231. m=spectral_mix(a,b,aa);
  232. m.a=colora.a+colorb.a*(1.0f-colora.a);
  233. m=vec4(m.rgb*m.a,m.a);
  234. return m;
  235. }
  236. int dab(float d, vec2 fpx, vec4 color, float size, float hardness, float smudge, vec4 smudge_color, vec4 last_color, out vec4 final){
  237. vec4 cc=color;
  238. float fac=1.0f-pow(d/size,1.0f+1.0f/(1.0f-hardness+1e-4));
  239. float canvas=SampleCanvas(fpx,uBrushDirection,fac,uBrushForce,uBrushGunkyness);
  240. cc.a=color.a*canvas*(1.0f-smudge); cc.rgb=cc.rgb*cc.a;
  241. float erasing=float(uBrushErasing);
  242. cc=cc*(1.0f-erasing);
  243. // this looks better than the one commented out below
  244. vec4 c2=spectral_mix_unpre(last_color,smudge_color,smudge*fac*color.a*canvas);
  245. c2=mix_over(cc,c2);
  246. //vec4 c2=mix_over(cc,last_color);
  247. //c2=spectral_mix_unpre(c2,smudge_color,smudge*fac*color.a*canvas);
  248. c2=spectral_mix_unpre(c2,c2*(1.0f-fac*color.a),erasing*canvas);
  249. final=c2;
  250. return 1;
  251. }
  252. #ifndef saturate
  253. #define saturate(v) clamp(v, 0., 1.)
  254. #endif
  255. const float HCV_EPSILON = 1e-10;
  256. const float HCY_EPSILON = 1e-10;
  257. vec3 hue_to_rgb(float hue){
  258. float R = abs(hue * 6. - 3.) - 1.;
  259. float G = 2. - abs(hue * 6. - 2.);
  260. float B = 2. - abs(hue * 6. - 4.);
  261. return saturate(vec3(R,G,B));
  262. }
  263. vec3 hcy_to_rgb(vec3 hcy){
  264. const vec3 HCYwts = vec3(0.299, 0.587, 0.114);
  265. vec3 RGB = hue_to_rgb(hcy.x);
  266. float Z = dot(RGB, HCYwts);
  267. if (hcy.z < Z) { hcy.y *= hcy.z / Z; }
  268. else if (Z < 1.) { hcy.y *= (1. - hcy.z) / (1. - Z); }
  269. return (RGB - Z) * hcy.y + hcy.z;
  270. }
  271. vec3 rgb_to_hcv(vec3 rgb){
  272. // Based on work by Sam Hocevar and Emil Persson
  273. vec4 P = (rgb.g < rgb.b) ? vec4(rgb.bg, -1.0, 2.0/3.0) : vec4(rgb.gb, 0.0, -1.0/3.0);
  274. vec4 Q = (rgb.r < P.x) ? vec4(P.xyw, rgb.r) : vec4(rgb.r, P.yzx);
  275. float C = Q.x - min(Q.w, Q.y);
  276. float H = abs((Q.w - Q.y) / (6. * C + HCV_EPSILON) + Q.z);
  277. return vec3(H, C, Q.x);
  278. }
  279. vec3 rgb_to_hcy(vec3 rgb){
  280. const vec3 HCYwts = vec3(0.299, 0.587, 0.114);
  281. // Corrected by David Schaeffer
  282. vec3 HCV = rgb_to_hcv(rgb);
  283. float Y = dot(rgb, HCYwts);
  284. float Z = dot(hue_to_rgb(HCV.x), HCYwts);
  285. if (Y < Z) { HCV.y *= Z / (HCY_EPSILON + Y); }
  286. else { HCV.y *= (1. - Z) / (HCY_EPSILON + 1. - Y); }
  287. return vec3(HCV.x, HCV.y, Y);
  288. }
  289. #ifndef OUR_GLES
  290. subroutine void BrushRoutines();
  291. #endif
  292. #ifndef OUR_GLES
  293. subroutine(BrushRoutines)
  294. #endif
  295. void DoDabs(){
  296. ivec2 px = ivec2(gl_GlobalInvocationID.xy)+uBrushCorner;
  297. if(px.x<0||px.y<0||px.x>1024||px.y>1024) return;
  298. vec2 fpx=vec2(px),origfpx=fpx;
  299. fpx=uBrushCenter+rotate(fpx-uBrushCenter,uBrushAngle);
  300. fpx.x=uBrushCenter.x+(fpx.x-uBrushCenter.x)*(1.+uBrushSlender);
  301. float dd=distance(fpx,uBrushCenter); if(dd>uBrushSize) return;
  302. vec4 dabc=OurImageLoad(img, px);
  303. vec4 smudgec=pow(spectral_mix_unpre(pow(OurImageLoad(smudge_buckets,ivec2(1,0)),p1_22),pow(OurImageLoad(smudge_buckets,ivec2(0,0)),p1_22),uBrushRecentness),p22);
  304. vec4 final_color;
  305. dab(dd,origfpx,uBrushColor,uBrushSize,uBrushHardness,uBrushSmudge,smudgec,dabc,final_color);
  306. if(final_color.a>0.){
  307. if(uBrushMix==0){ dabc=final_color; }
  308. else if(uBrushMix==1){ dabc.rgb=final_color.rgb/final_color.a*dabc.a;}
  309. else if(uBrushMix==2){ vec3 xyz=rgb_to_hcy(dabc.rgb); xyz.xy=rgb_to_hcy(final_color.rgb).xy; dabc.rgb=hcy_to_rgb(xyz); }
  310. else if(uBrushMix==3){ dabc.rgb=dabc.rgb+final_color.rgb*0.01;dabc.a=dabc.a*0.99+final_color.a*0.01; }
  311. OurImageStore(img, px, dabc);
  312. }
  313. }
  314. #ifndef OUR_GLES
  315. subroutine(BrushRoutines)
  316. #endif
  317. void DoSample(){
  318. ivec2 p=ivec2(gl_GlobalInvocationID.xy);
  319. int DoSample=1; vec4 color;
  320. if(p.y==0){
  321. vec2 sp=round(vec2(sin(float(p.x)),cos(float(p.x)))*uBrushSize);
  322. ivec2 px=ivec2(sp)+uBrushCorner; if(px.x<0||px.y<0||px.x>=1024||px.y>=1024){ DoSample=0; }
  323. if(DoSample!=0){
  324. ivec2 b=uBrushCorner; if(b.x>=0&&b.y>=0&&b.x<1024&&b.y<1024){ OurImageStore(smudge_buckets,ivec2(128+32,0),OurImageLoad(img, b)); }
  325. color=OurImageLoad(img, px);
  326. OurImageStore(smudge_buckets,ivec2(p.x+128,0),color);
  327. }
  328. }else{DoSample=0;}
  329. memoryBarrier();barrier(); if(DoSample==0) return;
  330. if(uBrushErasing==0 || p.x!=0) return;
  331. color=vec4(0.,0.,0.,0.); for(int i=0;i<32;i++){ color=color+OurImageLoad(smudge_buckets, ivec2(i+128,0)); }
  332. color=spectral_mix_unpre(color/32.,OurImageLoad(smudge_buckets, ivec2(128+32,0)),0.6*(1.0f-uBrushColor.a)); vec4 oldcolor=OurImageLoad(smudge_buckets, ivec2(0,0));
  333. OurImageStore(smudge_buckets,ivec2(1,0),uBrushErasing==2?color:oldcolor);
  334. OurImageStore(smudge_buckets,ivec2(0,0),color);
  335. }
  336. #ifdef OUR_GLES
  337. void uBrushRoutineSelection(){
  338. if(uBrushRoutineSelectionES==0){ DoDabs(); }
  339. else{ DoSample(); }
  340. }
  341. #else
  342. subroutine uniform BrushRoutines uBrushRoutineSelection;
  343. #endif
  344. void main() {
  345. uBrushRoutineSelection();
  346. }
  347. )";
  348. const char OUR_COMPOSITION_SHADER[] = R"(
  349. precision highp uimage2D;
  350. precision highp float;
  351. precision highp int;
  352. layout(local_size_x = WORKGROUP_SIZE, local_size_y = WORKGROUP_SIZE, local_size_z = 1) in;
  353. #ifdef OUR_GLES
  354. layout(r32ui, binding = 0) uniform uimage2D top;
  355. layout(r32ui, binding = 1) uniform uimage2D bottom;
  356. #else
  357. layout(rgba16ui, binding = 0) uniform uimage2D top;
  358. layout(rgba16ui, binding = 1) uniform uimage2D bottom;
  359. #endif
  360. uniform int uBlendMode;
  361. uniform float uAlphaTop;
  362. uniform float uAlphaBottom;
  363. #ifdef OUR_GLES
  364. vec4 cunpack(uint d){
  365. return vec4(float(d&0xFFu)/255.,float((d>>8u)&0xFFu)/255.,float((d>>16u)&0xFFu)/255.,float((d>>24u)&0xFFu)/255.);
  366. }
  367. uvec4 cpack(vec4 c){
  368. uint v= uint(uint(c.r*255.) | (uint(c.g*255.)<<8u) | (uint(c.b*255.)<<16u) | (uint(c.a*255.)<<24u));
  369. return uvec4(v,v,v,v);
  370. }
  371. #define OurImageLoad(img, p) \
  372. (cunpack(imageLoad(img,p).x))
  373. #define OurImageStore(img, p, color) \
  374. imageStore(img,p,cpack(color))
  375. #else
  376. #define OurImageLoad(img, p) \
  377. (vec4(imageLoad(img,p))/65535.)
  378. #define OurImageStore(img, p, color) \
  379. imageStore(img,p,uvec4(vec4(color)*65535.))
  380. #endif
  381. vec4 mix_over(vec4 colora, vec4 colorb){
  382. colora=colora*uAlphaTop/uAlphaBottom;
  383. vec4 c; c.a=colora.a+colorb.a*(1.0f-colora.a);
  384. c.rgb=(colora.rgb+colorb.rgb*(1.0f-colora.a));
  385. return c;
  386. }
  387. vec4 add_over(vec4 colora, vec4 colorb){
  388. colora=colora*uAlphaTop/uAlphaBottom;
  389. vec4 a=colora+colorb; a.a=clamp(a.a,0.,1.); return a;
  390. }
  391. void main() {
  392. ivec2 px=ivec2(gl_GlobalInvocationID.xy);
  393. vec4 c1=OurImageLoad(top,px); vec4 c2=OurImageLoad(bottom,px);
  394. vec4 c=(uBlendMode==0)?mix_over(c1,c2):add_over(c1,c2);
  395. OurImageStore(bottom,px,c);
  396. OurImageStore(top,px,vec4(1.));
  397. }
  398. )";