*/}}

ourshader.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. subroutine vec4 MixRoutines(vec4 a, vec4 b, float fac_a);
  79. subroutine(MixRoutines) vec4 DoMixNormal(vec4 a, vec4 b, float fac_a){
  80. return mix(a,b,1-fac_a);
  81. }
  82. subroutine(MixRoutines) vec4 DoMixSpectral(vec4 a, vec4 b, float fac_a){
  83. vec4 result = vec4(0,0,0,0);
  84. result.a=mix(a.a,b.a,1-fac_a);
  85. float spec_a[10] = {0,0,0,0,0,0,0,0,0,0}; rgb_to_spectral(a.rgb, spec_a);
  86. float spec_b[10] = {0,0,0,0,0,0,0,0,0,0}; rgb_to_spectral(b.rgb, spec_b);
  87. float spectralmix[10] = {0,0,0,0,0,0,0,0,0,0};
  88. for (int i=0; i < 10; i++) { spectralmix[i] = pow(spec_a[i], fac_a) * pow(spec_b[i], 1-fac_a); }
  89. result.rgb=spectral_to_rgb(spectralmix);
  90. return result;
  91. }
  92. subroutine uniform MixRoutines uMixRoutineSelection;
  93. vec4 spectral_mix(vec4 a, vec4 b, float fac_a){
  94. return uMixRoutineSelection(a,b,fac_a);
  95. }
  96. vec4 spectral_mix_unpre(vec4 colora, vec4 colorb, float fac){
  97. vec4 ca=(colora.a==0)?colora:vec4(colora.rgb/colora.a,colora.a);
  98. vec4 cb=(colorb.a==0)?colorb:vec4(colorb.rgb/colorb.a,colorb.a);
  99. float af=colora.a*(1-fac);
  100. float aa=af/(af+fac*colorb.a+0.000001);
  101. vec4 result=spectral_mix(ca,cb,aa);
  102. result.a=mix(colora.a,colorb.a,fac);
  103. return vec4(result.rgb*result.a,result.a);
  104. }
  105. float atan2(in float y, in float x){
  106. bool s = (abs(x) > abs(y)); return mix(3.1415926535/2.0 - atan(x,y), atan(y,x), s);
  107. }
  108. vec2 rotate(vec2 v, float angle) {
  109. float s = sin(angle); float c = cos(angle);
  110. return mat2(c,-s,s,c) * v;
  111. }
  112. float brightness(vec4 color) {
  113. return color.r*0.2126+color.b*0.7152+color.g*0.0722;
  114. }
  115. vec4 mix_over(vec4 colora, vec4 colorb){
  116. vec4 a=(colora.a==0)?colora:vec4(colora.rgb/colora.a,colora.a);
  117. vec4 b=(colorb.a==0)?colorb:vec4(colorb.rgb/colorb.a,colorb.a);
  118. vec4 m=vec4(0,0,0,0); float aa=colora.a/(colora.a+(1-colora.a)*colorb.a+0.0001);
  119. m=spectral_mix(a,b,aa);
  120. m.a=colora.a+colorb.a*(1-colora.a);
  121. m=vec4(m.rgb*m.a,m.a);
  122. return m;
  123. }
  124. int dab(float d, vec4 color, float size, float hardness, float smudge, vec4 smudge_color, vec4 last_color, out vec4 final){
  125. vec4 cc=color;
  126. float fac=1-pow(d/size,1+1/(1-hardness+1e-4));
  127. cc.a=color.a*fac*(1-smudge); cc.rgb=cc.rgb*cc.a;
  128. float erasing=float(uBrushErasing);
  129. cc=cc*(1-erasing);
  130. // this looks better than the one commented out below
  131. vec4 c2=spectral_mix_unpre(last_color,smudge_color,smudge*fac*color.a);
  132. c2=mix_over(cc,c2);
  133. //vec4 c2=mix_over(cc,last_color);
  134. //c2=spectral_mix_unpre(c2,smudge_color,smudge*fac*color.a);
  135. c2=spectral_mix_unpre(c2,c2*(1-fac*color.a),erasing);
  136. final=c2;
  137. return 1;
  138. }
  139. subroutine void BrushRoutines();
  140. subroutine(BrushRoutines) void DoDabs(){
  141. ivec2 px = ivec2(gl_GlobalInvocationID.xy)+uBrushCorner;
  142. if(px.x<0||px.y<0||px.x>1024||px.y>1024) return;
  143. vec2 fpx=vec2(px);
  144. fpx=uBrushCenter+rotate(fpx-uBrushCenter,uBrushAngle);
  145. fpx.x=uBrushCenter.x+(fpx.x-uBrushCenter.x)*(1+uBrushSlender);
  146. float dd=distance(fpx,uBrushCenter); if(dd>uBrushSize) return;
  147. vec4 dabc=imageLoad(img, px);
  148. 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);
  149. vec4 final_color;
  150. dab(dd,uBrushColor,uBrushSize,uBrushHardness,uBrushSmudge,smudgec,dabc,final_color);
  151. dabc=final_color;
  152. imageStore(img, px, dabc);
  153. }
  154. subroutine(BrushRoutines) void DoSample(){
  155. ivec2 p=ivec2(gl_GlobalInvocationID.xy);
  156. int DoSample=1; vec4 color;
  157. if(p.y==0){
  158. vec2 sp=round(vec2(sin(float(p.x)),cos(float(p.x)))*uBrushSize);
  159. ivec2 px=ivec2(sp)+uBrushCorner; if(px.x<0||px.y<0||px.x>=1024||px.y>=1024){ DoSample=0; }
  160. if(DoSample!=0){
  161. 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)); }
  162. color=imageLoad(img, px);
  163. imageStore(smudge_buckets,ivec2(p.x+128,0),color);
  164. }
  165. }else{DoSample=0;}
  166. memoryBarrier();barrier(); if(DoSample==0) return;
  167. if(uBrushErasing==0 || p.x!=0) return;
  168. color=vec4(0,0,0,0); for(int i=0;i<32;i++){ color=color+imageLoad(smudge_buckets, ivec2(i+128,0)); }
  169. 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));
  170. imageStore(smudge_buckets,ivec2(1,0),uBrushErasing==2?color:oldcolor);
  171. imageStore(smudge_buckets,ivec2(0,0),color);
  172. }
  173. subroutine uniform BrushRoutines uBrushRoutineSelection;
  174. void main() {
  175. uBrushRoutineSelection();
  176. }
  177. )";
  178. const char OUR_COMPOSITION_SHADER[]=R"(#version 430
  179. layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;
  180. layout(rgba16, binding = 0) uniform image2D top;
  181. layout(rgba16, binding = 1) uniform image2D bottom;
  182. uniform int uBlendMode;
  183. uniform float uAlphaTop;
  184. uniform float uAlphaBottom;
  185. vec4 mix_over(vec4 colora, vec4 colorb){
  186. colora=colora*uAlphaTop/uAlphaBottom;
  187. vec4 c; c.a=colora.a+colorb.a*(1-colora.a);
  188. c.rgb=(colora.rgb+colorb.rgb*(1-colora.a));
  189. return c;
  190. }
  191. vec4 add_over(vec4 colora, vec4 colorb){
  192. colora=colora*uAlphaTop/uAlphaBottom;
  193. vec4 a=colora+colorb; a.a=clamp(a.a,0,1); return a;
  194. }
  195. void main() {
  196. ivec2 px=ivec2(gl_GlobalInvocationID.xy);
  197. vec4 c1=imageLoad(top,px); vec4 c2=imageLoad(bottom,px);
  198. vec4 c=(uBlendMode==0)?mix_over(c1,c2):add_over(c1,c2);
  199. imageStore(bottom,px,c);
  200. imageStore(top,px,vec4(0,0,0,0));
  201. }
  202. )";