*/}}

ourshader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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_CANVAS_SHADER[]=R"(#version 430
  20. layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;
  21. layout(rgba16, binding = 0) uniform image2D img;
  22. layout(rgba16, binding = 1) coherent uniform image2D smudge_buckets;
  23. uniform int uCanvasType;
  24. uniform int uCanvasRandom;
  25. uniform float uCanvasFactor;
  26. uniform ivec2 uImageOffset;
  27. uniform ivec2 uBrushCorner;
  28. uniform vec2 uBrushCenter;
  29. uniform float uBrushSize;
  30. uniform float uBrushHardness;
  31. uniform float uBrushSmudge;
  32. uniform float uBrushSlender;
  33. uniform float uBrushAngle;
  34. uniform vec2 uBrushDirection;
  35. uniform float uBrushForce;
  36. uniform float uBrushGunkyness;
  37. uniform float uBrushRecentness;
  38. uniform vec4 uBrushColor;
  39. uniform vec4 uBackgroundColor;
  40. uniform int uBrushErasing;
  41. const vec4 p1_22=vec4(1.0/2.2,1.0/2.2,1.0/2.2,1.0/2.2);
  42. const vec4 p22=vec4(2.2,2.2,2.2,2.2);
  43. const float WGM_EPSILON=0.001f;
  44. const float T_MATRIX_SMALL[30] = float[30](0.026595621243689,0.049779426257903,0.022449850859496,-0.218453689278271
  45. ,-0.256894883201278,0.445881722194840,0.772365886289756,0.194498761382537
  46. ,0.014038157587820,0.007687264480513
  47. ,-0.032601672674412,-0.061021043498478,-0.052490001018404
  48. ,0.206659098273522,0.572496335158169,0.317837248815438,-0.021216624031211
  49. ,-0.019387668756117,-0.001521339050858,-0.000835181622534
  50. ,0.339475473216284,0.635401374177222,0.771520797089589,0.113222640692379
  51. ,-0.055251113343776,-0.048222578468680,-0.012966666339586
  52. ,-0.001523814504223,-0.000094718948810,-0.000051604594741);
  53. const float spectral_r_small[10] = float[10](0.009281362787953,0.009732627042016,0.011254252737167,0.015105578649573
  54. ,0.024797924177217,0.083622585502406,0.977865045723212,1.000000000000000
  55. ,0.999961046144372,0.999999992756822);
  56. const float spectral_g_small[10] = float[10](0.002854127435775,0.003917589679914,0.012132151699187,0.748259205918013
  57. ,1.000000000000000,0.865695937531795,0.037477469241101,0.022816789725717
  58. ,0.021747419446456,0.021384940572308);
  59. const float spectral_b_small[10] = float[10](0.537052150373386,0.546646402401469,0.575501819073983,0.258778829633924
  60. ,0.041709923751716,0.012662638828324,0.007485593127390,0.006766900622462
  61. ,0.006699764779016,0.006676219883241);
  62. void rgb_to_spectral (vec3 rgb, out float spectral_[10]) {
  63. float offset = 1.0 - WGM_EPSILON;
  64. float r = rgb.r * offset + WGM_EPSILON;
  65. float g = rgb.g * offset + WGM_EPSILON;
  66. float b = rgb.b * offset + WGM_EPSILON;
  67. 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;}
  68. 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;}
  69. 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;}
  70. for (int i=0; i<10; i++) {spectral_[i] = spec_r[i] + spec_g[i] + spec_b[i];}
  71. }
  72. vec3 spectral_to_rgb (float spectral[10]) {
  73. float offset = 1.0 - WGM_EPSILON;
  74. // We need this tmp. array to allow auto vectorization. <-- How about on GPU?
  75. float tmp[3] = float[3](0,0,0);
  76. for (int i=0; i<10; i++) {
  77. tmp[0] += T_MATRIX_SMALL[i] * spectral[i];
  78. tmp[1] += T_MATRIX_SMALL[10+i] * spectral[i];
  79. tmp[2] += T_MATRIX_SMALL[20+i] * spectral[i];
  80. }
  81. vec3 rgb_;
  82. for (int i=0; i<3; i++) {rgb_[i] = clamp((tmp[i] - WGM_EPSILON) / offset, 0.0f, 1.0f);}
  83. return rgb_;
  84. }
  85. vec2 hash( vec2 p ){
  86. p = vec2( dot(p,vec2(127.1,311.7)), dot(p,vec2(269.5,183.3)) );
  87. return -1.0 + 2.0*fract(sin(p)*43758.5453123);
  88. }
  89. float rand(vec2 co){
  90. return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
  91. }
  92. float noise(in vec2 p){ // from iq
  93. const float K1 = 0.366025404; // (sqrt(3)-1)/2;
  94. const float K2 = 0.211324865; // (3-sqrt(3))/6;
  95. vec2 i = floor( p + (p.x+p.y)*K1 );
  96. vec2 a = p - i + (i.x+i.y)*K2;
  97. float m = step(a.y,a.x);
  98. vec2 o = vec2(m,1.0-m);
  99. vec2 b = a - o + K2;
  100. vec2 c = a - 1.0 + 2.0*K2;
  101. vec3 h = max( 0.5-vec3(dot(a,a), dot(b,b), dot(c,c) ), 0.0 );
  102. vec3 n = h*h*h*h*vec3( dot(a,hash(i+0.0)), dot(b,hash(i+o)), dot(c,hash(i+1.0)));
  103. return dot( n, vec3(70.0) );
  104. }
  105. #define HEIGHT_STRAND(x,y) abs(fract(x)-.5)<.48? \
  106. (.4+.2*sin(3.14*(y+ceil(x))))* \
  107. ((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
  108. #define PATTERN_CANVAS(x,y) \
  109. (max(HEIGHT_STRAND((x),(y)),HEIGHT_STRAND(-(y),(x))))
  110. float HEIGHT_CANVAS(float x,float y){
  111. if(uCanvasType == 1){
  112. return PATTERN_CANVAS(x,y);
  113. }else if(uCanvasType == 2){
  114. vec2 uv=vec2(x,y); float f; uv*=0.1; // from iq
  115. f = 0.2*noise( uv ); uv*=5.;
  116. f += 0.6*noise( uv ); uv*=3.;
  117. f += 0.5*noise( uv );
  118. f = 0.55 + 0.55*f;
  119. return pow(f,0.5);
  120. }
  121. return 1.;
  122. }
  123. float SampleCanvas(vec2 U, vec2 dir,float rfac, float force, float gunky){
  124. if(uCanvasType==0 || abs(gunky-0.)<1.e-2){ return rfac; }
  125. U+=vec2(uImageOffset); U/=20.3; U.x=U.x+rand(U)/10.; U.y=U.y+rand(U)/10.;
  126. 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.;
  127. U.x+=noise(_uv)*2.1; _uv = m*_uv; U.x+=noise(_uv)*0.71;
  128. _uv.y+=365.404;
  129. U.y+=noise(_uv)*1.9; _uv = m*_uv; U.y+=noise(_uv)*0.83;
  130. float d=0.1;
  131. float h=HEIGHT_CANVAS(U.x,U.y);
  132. float hr=HEIGHT_CANVAS(U.x+d,U.y);
  133. float hu=HEIGHT_CANVAS(U.x,U.y+d);
  134. 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);
  135. float useforce=force*rfac;
  136. float scrape=dot(normalize(vz),vec3(-normalize(dir).xy,0))*mix(0.3,1.,useforce);
  137. float top=h-(1.-pow(useforce,1.5)*2.); float tophard=smoothstep(0.4,0.6,top);
  138. float fac=(gunky>=0.)?mix(mix(1.,top,gunky),tophard,gunky):mix(1.,1.-h,-gunky*0.8);
  139. fac=max(fac,scrape*clamp(gunky,0,1));
  140. fac=clamp(fac,0.,1.);
  141. fac*=rfac;
  142. return mix(rfac,fac,uCanvasFactor);
  143. }
  144. subroutine vec4 MixRoutines(vec4 a, vec4 b, float fac_a);
  145. subroutine(MixRoutines) vec4 DoMixNormal(vec4 a, vec4 b, float fac_a){
  146. return mix(a,b,1-fac_a);
  147. }
  148. subroutine(MixRoutines) vec4 DoMixSpectral(vec4 a, vec4 b, float fac_a){
  149. vec4 result = vec4(0,0,0,0);
  150. result.a=mix(a.a,b.a,1-fac_a);
  151. float spec_a[10] = {0,0,0,0,0,0,0,0,0,0}; rgb_to_spectral(a.rgb, spec_a);
  152. float spec_b[10] = {0,0,0,0,0,0,0,0,0,0}; rgb_to_spectral(b.rgb, spec_b);
  153. float spectralmix[10] = {0,0,0,0,0,0,0,0,0,0};
  154. for (int i=0; i < 10; i++) { spectralmix[i] = pow(spec_a[i], fac_a) * pow(spec_b[i], 1-fac_a); }
  155. result.rgb=spectral_to_rgb(spectralmix);
  156. return result;
  157. }
  158. subroutine uniform MixRoutines uMixRoutineSelection;
  159. vec4 spectral_mix(vec4 a, vec4 b, float fac_a){
  160. return uMixRoutineSelection(a,b,fac_a);
  161. }
  162. vec4 spectral_mix_unpre(vec4 colora, vec4 colorb, float fac){
  163. vec4 ca=(colora.a==0)?colora:vec4(colora.rgb/colora.a,colora.a);
  164. vec4 cb=(colorb.a==0)?colorb:vec4(colorb.rgb/colorb.a,colorb.a);
  165. float af=colora.a*(1-fac);
  166. float aa=af/(af+fac*colorb.a+0.000001);
  167. vec4 result=spectral_mix(ca,cb,aa);
  168. result.a=mix(colora.a,colorb.a,fac);
  169. return vec4(result.rgb*result.a,result.a);
  170. }
  171. float atan2(in float y, in float x){
  172. bool s = (abs(x) > abs(y)); return mix(3.1415926535/2.0 - atan(x,y), atan(y,x), s);
  173. }
  174. vec2 rotate(vec2 v, float angle) {
  175. float s = sin(angle); float c = cos(angle);
  176. return mat2(c,-s,s,c) * v;
  177. }
  178. float brightness(vec4 color) {
  179. return color.r*0.2126+color.b*0.7152+color.g*0.0722;
  180. }
  181. vec4 mix_over(vec4 colora, vec4 colorb){
  182. vec4 a=(colora.a==0)?colora:vec4(colora.rgb/colora.a,colora.a);
  183. vec4 b=(colorb.a==0)?colorb:vec4(colorb.rgb/colorb.a,colorb.a);
  184. vec4 m=vec4(0,0,0,0); float aa=colora.a/(colora.a+(1-colora.a)*colorb.a+0.0001);
  185. m=spectral_mix(a,b,aa);
  186. m.a=colora.a+colorb.a*(1-colora.a);
  187. m=vec4(m.rgb*m.a,m.a);
  188. return m;
  189. }
  190. int dab(float d, vec2 fpx, vec4 color, float size, float hardness, float smudge, vec4 smudge_color, vec4 last_color, out vec4 final){
  191. vec4 cc=color;
  192. float fac=1-pow(d/size,1+1/(1-hardness+1e-4));
  193. float canvas=SampleCanvas(fpx,uBrushDirection,fac,uBrushForce,uBrushGunkyness);
  194. cc.a=color.a*canvas*(1-smudge); cc.rgb=cc.rgb*cc.a;
  195. float erasing=float(uBrushErasing);
  196. cc=cc*(1-erasing);
  197. // this looks better than the one commented out below
  198. vec4 c2=spectral_mix_unpre(last_color,smudge_color,smudge*fac*color.a*canvas);
  199. c2=mix_over(cc,c2);
  200. //vec4 c2=mix_over(cc,last_color);
  201. //c2=spectral_mix_unpre(c2,smudge_color,smudge*fac*color.a*canvas);
  202. c2=spectral_mix_unpre(c2,c2*(1-fac*color.a),erasing*canvas);
  203. final=c2;
  204. return 1;
  205. }
  206. subroutine void BrushRoutines();
  207. subroutine(BrushRoutines) void DoDabs(){
  208. ivec2 px = ivec2(gl_GlobalInvocationID.xy)+uBrushCorner;
  209. if(px.x<0||px.y<0||px.x>1024||px.y>1024) return;
  210. vec2 fpx=vec2(px),origfpx=fpx;
  211. fpx=uBrushCenter+rotate(fpx-uBrushCenter,uBrushAngle);
  212. fpx.x=uBrushCenter.x+(fpx.x-uBrushCenter.x)*(1+uBrushSlender);
  213. float dd=distance(fpx,uBrushCenter); if(dd>uBrushSize) return;
  214. vec4 dabc=imageLoad(img, px);
  215. vec4 smudgec=pow(spectral_mix_unpre(pow(imageLoad(smudge_buckets,ivec2(1,0)),p1_22),pow(imageLoad(smudge_buckets,ivec2(0,0)),p1_22),uBrushRecentness),p22);
  216. vec4 final_color;
  217. dab(dd,origfpx,uBrushColor,uBrushSize,uBrushHardness,uBrushSmudge,smudgec,dabc,final_color);
  218. dabc=final_color;
  219. imageStore(img, px, dabc);
  220. }
  221. subroutine(BrushRoutines) void DoSample(){
  222. ivec2 p=ivec2(gl_GlobalInvocationID.xy);
  223. int DoSample=1; vec4 color;
  224. if(p.y==0){
  225. vec2 sp=round(vec2(sin(float(p.x)),cos(float(p.x)))*uBrushSize);
  226. ivec2 px=ivec2(sp)+uBrushCorner; if(px.x<0||px.y<0||px.x>=1024||px.y>=1024){ DoSample=0; }
  227. if(DoSample!=0){
  228. ivec2 b=uBrushCorner; if(b.x>=0&&b.y>=0&&b.x<1024&&b.y<1024){ imageStore(smudge_buckets,ivec2(128+32,0),imageLoad(img, b)); }
  229. color=imageLoad(img, px);
  230. imageStore(smudge_buckets,ivec2(p.x+128,0),color);
  231. }
  232. }else{DoSample=0;}
  233. memoryBarrier();barrier(); if(DoSample==0) return;
  234. if(uBrushErasing==0 || p.x!=0) return;
  235. color=vec4(0,0,0,0); for(int i=0;i<32;i++){ color=color+imageLoad(smudge_buckets, ivec2(i+128,0)); }
  236. color=spectral_mix_unpre(color/32,imageLoad(smudge_buckets, ivec2(128+32,0)),0.6*(1-uBrushColor.a)); vec4 oldcolor=imageLoad(smudge_buckets, ivec2(0,0));
  237. imageStore(smudge_buckets,ivec2(1,0),uBrushErasing==2?color:oldcolor);
  238. imageStore(smudge_buckets,ivec2(0,0),color);
  239. }
  240. subroutine uniform BrushRoutines uBrushRoutineSelection;
  241. void main() {
  242. uBrushRoutineSelection();
  243. }
  244. )";
  245. const char OUR_COMPOSITION_SHADER[]=R"(#version 430
  246. layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;
  247. layout(rgba16, binding = 0) uniform image2D top;
  248. layout(rgba16, binding = 1) uniform image2D bottom;
  249. uniform int uBlendMode;
  250. uniform float uAlphaTop;
  251. uniform float uAlphaBottom;
  252. vec4 mix_over(vec4 colora, vec4 colorb){
  253. colora=colora*uAlphaTop/uAlphaBottom;
  254. vec4 c; c.a=colora.a+colorb.a*(1-colora.a);
  255. c.rgb=(colora.rgb+colorb.rgb*(1-colora.a));
  256. return c;
  257. }
  258. vec4 add_over(vec4 colora, vec4 colorb){
  259. colora=colora*uAlphaTop/uAlphaBottom;
  260. vec4 a=colora+colorb; a.a=clamp(a.a,0,1); return a;
  261. }
  262. void main() {
  263. ivec2 px=ivec2(gl_GlobalInvocationID.xy);
  264. vec4 c1=imageLoad(top,px); vec4 c2=imageLoad(bottom,px);
  265. vec4 c=(uBlendMode==0)?mix_over(c1,c2):add_over(c1,c2);
  266. imageStore(bottom,px,c);
  267. imageStore(top,px,vec4(0,0,0,0));
  268. }
  269. )";