*/}}

ourshader.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 ivec2 uBrushCorner;
  24. uniform vec2 uBrushCenter;
  25. uniform float uBrushSize;
  26. uniform float uBrushHardness;
  27. uniform float uBrushSmudge;
  28. uniform float uBrushSlender;
  29. uniform float uBrushAngle;
  30. uniform float uBrushRecentness;
  31. uniform vec4 uBrushColor;
  32. uniform vec4 uBackgroundColor;
  33. uniform int uBrushErasing;
  34. const vec4 p1_22=vec4(1.0/2.2,1.0/2.2,1.0/2.2,1.0/2.2);
  35. const vec4 p22=vec4(2.2,2.2,2.2,2.2);
  36. const float WGM_EPSILON=0.001f;
  37. const float T_MATRIX_SMALL[3][10] = {{0.026595621243689,0.049779426257903,0.022449850859496,-0.218453689278271
  38. ,-0.256894883201278,0.445881722194840,0.772365886289756,0.194498761382537
  39. ,0.014038157587820,0.007687264480513}
  40. ,{-0.032601672674412,-0.061021043498478,-0.052490001018404
  41. ,0.206659098273522,0.572496335158169,0.317837248815438,-0.021216624031211
  42. ,-0.019387668756117,-0.001521339050858,-0.000835181622534}
  43. ,{0.339475473216284,0.635401374177222,0.771520797089589,0.113222640692379
  44. ,-0.055251113343776,-0.048222578468680,-0.012966666339586
  45. ,-0.001523814504223,-0.000094718948810,-0.000051604594741}};
  46. const float spectral_r_small[10] = {0.009281362787953,0.009732627042016,0.011254252737167,0.015105578649573
  47. ,0.024797924177217,0.083622585502406,0.977865045723212,1.000000000000000
  48. ,0.999961046144372,0.999999992756822};
  49. const float spectral_g_small[10] = {0.002854127435775,0.003917589679914,0.012132151699187,0.748259205918013
  50. ,1.000000000000000,0.865695937531795,0.037477469241101,0.022816789725717
  51. ,0.021747419446456,0.021384940572308};
  52. const float spectral_b_small[10] = {0.537052150373386,0.546646402401469,0.575501819073983,0.258778829633924
  53. ,0.041709923751716,0.012662638828324,0.007485593127390,0.006766900622462
  54. ,0.006699764779016,0.006676219883241};
  55. void rgb_to_spectral (vec3 rgb, out float spectral_[10]) {
  56. float offset = 1.0 - WGM_EPSILON;
  57. float r = rgb.r * offset + WGM_EPSILON;
  58. float g = rgb.g * offset + WGM_EPSILON;
  59. float b = rgb.b * offset + WGM_EPSILON;
  60. float spec_r[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;}
  61. float spec_g[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;}
  62. float spec_b[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;}
  63. for (int i=0; i<10; i++) {spectral_[i] = spec_r[i] + spec_g[i] + spec_b[i];}
  64. }
  65. vec3 spectral_to_rgb (float spectral[10]) {
  66. float offset = 1.0 - WGM_EPSILON;
  67. // We need this tmp. array to allow auto vectorization. <-- How about on GPU?
  68. float tmp[3] = {0,0,0};
  69. for (int i=0; i<10; i++) {
  70. tmp[0] += T_MATRIX_SMALL[0][i] * spectral[i];
  71. tmp[1] += T_MATRIX_SMALL[1][i] * spectral[i];
  72. tmp[2] += T_MATRIX_SMALL[2][i] * spectral[i];
  73. }
  74. vec3 rgb_;
  75. for (int i=0; i<3; i++) {rgb_[i] = clamp((tmp[i] - WGM_EPSILON) / offset, 0.0f, 1.0f);}
  76. return rgb_;
  77. }
  78. vec4 spectral_mix(vec4 a, vec4 b, float fac_a){
  79. vec4 result = vec4(0,0,0,0);
  80. result.a=mix(a.a,b.a,1-fac_a);
  81. float spec_a[10] = {0,0,0,0,0,0,0,0,0,0}; rgb_to_spectral(a.rgb, spec_a);
  82. float spec_b[10] = {0,0,0,0,0,0,0,0,0,0}; rgb_to_spectral(b.rgb, spec_b);
  83. float spectralmix[10] = {0,0,0,0,0,0,0,0,0,0};
  84. for (int i=0; i < 10; i++) { spectralmix[i] = pow(spec_a[i], fac_a) * pow(spec_b[i], 1-fac_a); }
  85. result.rgb=spectral_to_rgb(spectralmix);
  86. return result;
  87. }
  88. vec4 spectral_mix_unpre(vec4 colora, vec4 colorb, float fac){
  89. vec4 ca=(colora.a==0)?colora:vec4(colora.rgb/colora.a,colora.a);
  90. vec4 cb=(colorb.a==0)?colorb:vec4(colorb.rgb/colorb.a,colorb.a);
  91. float af=colora.a*(1-fac);
  92. float aa=af/(af+(1-af)*colorb.a+0.000001);
  93. vec4 result=spectral_mix(ca,cb,aa);
  94. result.a=mix(colora.a,colorb.a,fac);
  95. return vec4(result.rgb*result.a,result.a);
  96. }
  97. float atan2(in float y, in float x){
  98. bool s = (abs(x) > abs(y)); return mix(3.1415926535/2.0 - atan(x,y), atan(y,x), s);
  99. }
  100. vec2 rotate(vec2 v, float angle) {
  101. float s = sin(angle); float c = cos(angle);
  102. return mat2(c,-s,s,c) * v;
  103. }
  104. float brightness(vec4 color) {
  105. return color.r*0.2126+color.b*0.7152+color.g*0.0722;
  106. }
  107. vec4 mix_over(vec4 colora, vec4 colorb){
  108. vec4 a=(colora.a==0)?colora:vec4(colora.rgb/colora.a,colora.a);
  109. vec4 b=(colorb.a==0)?colorb:vec4(colorb.rgb/colorb.a,colorb.a);
  110. vec4 m=vec4(0,0,0,0); float aa=colora.a/(colora.a+(1-colora.a)*colorb.a+0.0001);
  111. m=spectral_mix(a,b,aa);
  112. m.a=colora.a+colorb.a*(1-colora.a);
  113. m=vec4(m.rgb*m.a,m.a);
  114. return m;
  115. }
  116. int dab(float d, vec4 color, float size, float hardness, float smudge, vec4 smudge_color, vec4 last_color, out vec4 final){
  117. vec4 cc=color;
  118. float fac=1-pow(d/size,1+1/(1-hardness+1e-4));
  119. cc.a=color.a*fac*(1-smudge); cc.rgb=cc.rgb*cc.a;
  120. float erasing=float(uBrushErasing);
  121. cc=cc*(1-erasing);
  122. // this looks better than the one commented out below
  123. vec4 c2=spectral_mix_unpre(last_color,smudge_color,smudge*fac*color.a);
  124. c2=mix_over(cc,c2);
  125. //vec4 c2=mix_over(cc,last_color);
  126. //c2=spectral_mix_unpre(c2,smudge_color,smudge*fac*color.a);
  127. c2=spectral_mix_unpre(c2,c2*(1-fac*color.a),erasing);
  128. final=c2;
  129. return 1;
  130. }
  131. subroutine void BrushRoutines();
  132. subroutine(BrushRoutines) void DoDabs(){
  133. ivec2 px = ivec2(gl_GlobalInvocationID.xy)+uBrushCorner;
  134. if(px.x<0||px.y<0||px.x>1024||px.y>1024) return;
  135. vec2 fpx=vec2(px);
  136. fpx=uBrushCenter+rotate(fpx-uBrushCenter,uBrushAngle);
  137. fpx.x=uBrushCenter.x+(fpx.x-uBrushCenter.x)*(1+uBrushSlender);
  138. float dd=distance(fpx,uBrushCenter); if(dd>uBrushSize) return;
  139. vec4 dabc=imageLoad(img, px);
  140. 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);
  141. vec4 final_color;
  142. dab(dd,uBrushColor,uBrushSize,uBrushHardness,uBrushSmudge,smudgec,dabc,final_color);
  143. dabc=final_color;
  144. imageStore(img, px, dabc);
  145. }
  146. subroutine(BrushRoutines) void DoSample(){
  147. ivec2 p=ivec2(gl_GlobalInvocationID.xy);
  148. int DoSample=1; vec4 color;
  149. if(p.y==0){
  150. vec2 sp=round(vec2(sin(float(p.x)),cos(float(p.x)))*uBrushSize);
  151. ivec2 px=ivec2(sp)+uBrushCorner; if(px.x<0||px.y<0||px.x>=1024||px.y>=1024){ DoSample=0; }
  152. if(DoSample!=0){
  153. 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)); }
  154. color=imageLoad(img, px);
  155. imageStore(smudge_buckets,ivec2(p.x+128,0),color);
  156. }
  157. }else{DoSample=0;}
  158. memoryBarrier();barrier(); if(DoSample==0) return;
  159. if(uBrushErasing==0 || p.x!=0) return;
  160. color=vec4(0,0,0,0); for(int i=0;i<32;i++){ color=color+imageLoad(smudge_buckets, ivec2(i+128,0)); }
  161. 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));
  162. imageStore(smudge_buckets,ivec2(1,0),uBrushErasing==2?color:oldcolor);
  163. imageStore(smudge_buckets,ivec2(0,0),color);
  164. }
  165. subroutine uniform BrushRoutines uBrushRoutineSelection;
  166. void main() {
  167. uBrushRoutineSelection();
  168. }
  169. )";
  170. const char OUR_COMPOSITION_SHADER[]=R"(#version 430
  171. layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;
  172. layout(rgba16, binding = 0) uniform image2D top;
  173. layout(rgba16, binding = 1) uniform image2D bottom;
  174. uniform int uBlendMode;
  175. uniform float uAlphaTop;
  176. uniform float uAlphaBottom;
  177. vec4 mix_over(vec4 colora, vec4 colorb){
  178. colora=colora*uAlphaTop/uAlphaBottom;
  179. vec4 c; c.a=colora.a+colorb.a*(1-colora.a);
  180. c.rgb=(colora.rgb+colorb.rgb*(1-colora.a));
  181. return c;
  182. }
  183. vec4 add_over(vec4 colora, vec4 colorb){
  184. colora=colora*uAlphaTop/uAlphaBottom;
  185. vec4 a=colora+colorb; a.a=clamp(a.a,0,1); return a;
  186. }
  187. void main() {
  188. ivec2 px=ivec2(gl_GlobalInvocationID.xy);
  189. vec4 c1=imageLoad(top,px); vec4 c2=imageLoad(bottom,px);
  190. vec4 c=(uBlendMode==0)?mix_over(c1,c2):add_over(c1,c2);
  191. imageStore(bottom,px,c);
  192. imageStore(top,px,vec4(0,0,0,0));
  193. }
  194. )";