*/}}
1
0

la_tns_shaders.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * LaGUI: A graphical application framework.
  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 "la_5.h"
  19. extern "C" const char* TNS_SHADER_COLOR_COMMON=R"(
  20. #define M_PI 3.1415926535897932384626433832795
  21. float cbrt( float x ){
  22. return sign(x)*pow(abs(x),1.0f/3.0f);
  23. }
  24. float srgb_transfer_function(float a){
  25. return .0031308f >= a ? 12.92f * a : 1.055f * pow(a, .4166666666666667f) - .055f;
  26. }
  27. float srgb_transfer_function_inv(float a){
  28. return .04045f < a ? pow((a + .055f) / 1.055f, 2.4f) : a / 12.92f;
  29. }
  30. vec3 to_log_srgb(vec3 color){
  31. return vec3(srgb_transfer_function(color.r),srgb_transfer_function(color.g),srgb_transfer_function(color.b));
  32. }
  33. vec3 to_linear_srgb(vec3 color){
  34. return vec3(srgb_transfer_function_inv(color.r),srgb_transfer_function_inv(color.g),srgb_transfer_function_inv(color.b));
  35. }
  36. vec3 to_linear_clay(vec3 color){
  37. return vec3(pow(color.r,2.19921875),pow(color.g,2.19921875),pow(color.b,2.19921875));
  38. }
  39. vec3 to_log_clay(vec3 color){
  40. return vec3(pow(color.r,1.0/2.19921875),pow(color.g,1.0/2.19921875),pow(color.b,1.0/2.19921875));
  41. }
  42. vec3 linear_srgb_to_oklab(vec3 c){
  43. float l = 0.4122214708f * c.r + 0.5363325363f * c.g + 0.0514459929f * c.b;
  44. float m = 0.2119034982f * c.r + 0.6806995451f * c.g + 0.1073969566f * c.b;
  45. float s = 0.0883024619f * c.r + 0.2817188376f * c.g + 0.6299787005f * c.b;
  46. float l_ = cbrt(l);
  47. float m_ = cbrt(m);
  48. float s_ = cbrt(s);
  49. return vec3(
  50. 0.2104542553f * l_ + 0.7936177850f * m_ - 0.0040720468f * s_,
  51. 1.9779984951f * l_ - 2.4285922050f * m_ + 0.4505937099f * s_,
  52. 0.0259040371f * l_ + 0.7827717662f * m_ - 0.8086757660f * s_
  53. );
  54. }
  55. vec3 oklab_to_linear_srgb(vec3 c){
  56. float l_ = c.x + 0.3963377774f * c.y + 0.2158037573f * c.z;
  57. float m_ = c.x - 0.1055613458f * c.y - 0.0638541728f * c.z;
  58. float s_ = c.x - 0.0894841775f * c.y - 1.2914855480f * c.z;
  59. float l = l_ * l_ * l_;
  60. float m = m_ * m_ * m_;
  61. float s = s_ * s_ * s_;
  62. return vec3(
  63. +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s,
  64. -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s,
  65. -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s
  66. );
  67. }
  68. vec3 oklab_to_xyz(vec3 c){
  69. float l_ = c.x + 0.3963377774f * c.y + 0.2158037573f * c.z;
  70. float m_ = c.x - 0.1055613458f * c.y - 0.0638541728f * c.z;
  71. float s_ = c.x - 0.0894841775f * c.y - 1.2914855480f * c.z;
  72. float l = l_ * l_ * l_;
  73. float m = m_ * m_ * m_;
  74. float s = s_ * s_ * s_;
  75. mat3 mat=inverse(mat3(vec3(+0.8189330101,+0.0329845436,+0.0482003018),
  76. vec3(+0.3618667424,+0.9293118715,+0.2643662691),
  77. vec3(-0.1288597137,+0.0361456387,+0.6338517070)));
  78. return mat*vec3(l,m,s);
  79. }
  80. float compute_max_saturation(float a, float b){ float k0, k1, k2, k3, k4, wl, wm, ws;
  81. if (-1.88170328f * a - 0.80936493f * b > 1.f){ k0 = +1.19086277f; k1 = +1.76576728f; k2 = +0.59662641f; k3 = +0.75515197f; k4 = +0.56771245f;
  82. wl = +4.0767416621f; wm = -3.3077115913f; ws = +0.2309699292f;
  83. }
  84. else if (1.81444104f * a - 1.19445276f * b > 1.f){ k0 = +0.73956515f; k1 = -0.45954404f; k2 = +0.08285427f; k3 = +0.12541070f; k4 = +0.14503204f;
  85. wl = -1.2684380046f; wm = +2.6097574011f; ws = -0.3413193965f;
  86. }
  87. else{ k0 = +1.35733652f; k1 = -0.00915799f; k2 = -1.15130210f; k3 = -0.50559606f; k4 = +0.00692167f;
  88. wl = -0.0041960863f; wm = -0.7034186147f; ws = +1.7076147010f;
  89. } float S = k0 + k1 * a + k2 * b + k3 * a * a + k4 * a * b;
  90. float k_l = +0.3963377774f * a + 0.2158037573f * b;
  91. float k_m = -0.1055613458f * a - 0.0638541728f * b;
  92. float k_s = -0.0894841775f * a - 1.2914855480f * b;{
  93. float l_ = 1.f + S * k_l;
  94. float m_ = 1.f + S * k_m;
  95. float s_ = 1.f + S * k_s;
  96. float l = l_ * l_ * l_;
  97. float m = m_ * m_ * m_;
  98. float s = s_ * s_ * s_;
  99. float l_dS = 3.f * k_l * l_ * l_;
  100. float m_dS = 3.f * k_m * m_ * m_;
  101. float s_dS = 3.f * k_s * s_ * s_;
  102. float l_dS2 = 6.f * k_l * k_l * l_;
  103. float m_dS2 = 6.f * k_m * k_m * m_;
  104. float s_dS2 = 6.f * k_s * k_s * s_;
  105. float f = wl * l + wm * m + ws * s;
  106. float f1 = wl * l_dS + wm * m_dS + ws * s_dS;
  107. float f2 = wl * l_dS2 + wm * m_dS2 + ws * s_dS2;
  108. S = S - f * f1 / (f1 * f1 - 0.5f * f * f2);
  109. }
  110. return S;
  111. }
  112. vec2 find_cusp(float a, float b){ float S_cusp = compute_max_saturation(a, b); vec3 rgb_at_max = oklab_to_linear_srgb(vec3( 1, S_cusp * a, S_cusp * b ));
  113. float L_cusp = cbrt(1.f / max(max(rgb_at_max.r, rgb_at_max.g), rgb_at_max.b));
  114. float C_cusp = L_cusp * S_cusp;
  115. return vec2( L_cusp , C_cusp );
  116. }
  117. float find_gamut_intersection(float a, float b, float L1, float C1, float L0, vec2 cusp){ float t;
  118. if (((L1 - L0) * cusp.y - (cusp.x - L0) * C1) <= 0.f){
  119. t = cusp.y * L0 / (C1 * cusp.x + cusp.y * (L0 - L1));
  120. }
  121. else{ t = cusp.y * (L0 - 1.f) / (C1 * (cusp.x - 1.f) + cusp.y * (L0 - L1)); {
  122. float dL = L1 - L0;
  123. float dC = C1;
  124. float k_l = +0.3963377774f * a + 0.2158037573f * b;
  125. float k_m = -0.1055613458f * a - 0.0638541728f * b;
  126. float k_s = -0.0894841775f * a - 1.2914855480f * b;
  127. float l_dt = dL + dC * k_l;
  128. float m_dt = dL + dC * k_m;
  129. float s_dt = dL + dC * k_s; {
  130. float L = L0 * (1.f - t) + t * L1;
  131. float C = t * C1;
  132. float l_ = L + C * k_l;
  133. float m_ = L + C * k_m;
  134. float s_ = L + C * k_s;
  135. float l = l_ * l_ * l_;
  136. float m = m_ * m_ * m_;
  137. float s = s_ * s_ * s_;
  138. float ldt = 3.f * l_dt * l_ * l_;
  139. float mdt = 3.f * m_dt * m_ * m_;
  140. float sdt = 3.f * s_dt * s_ * s_;
  141. float ldt2 = 6.f * l_dt * l_dt * l_;
  142. float mdt2 = 6.f * m_dt * m_dt * m_;
  143. float sdt2 = 6.f * s_dt * s_dt * s_;
  144. float r = 4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s - 1.f;
  145. float r1 = 4.0767416621f * ldt - 3.3077115913f * mdt + 0.2309699292f * sdt;
  146. float r2 = 4.0767416621f * ldt2 - 3.3077115913f * mdt2 + 0.2309699292f * sdt2;
  147. float u_r = r1 / (r1 * r1 - 0.5f * r * r2);
  148. float t_r = -r * u_r;
  149. float g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s - 1.f;
  150. float g1 = -1.2684380046f * ldt + 2.6097574011f * mdt - 0.3413193965f * sdt;
  151. float g2 = -1.2684380046f * ldt2 + 2.6097574011f * mdt2 - 0.3413193965f * sdt2;
  152. float u_g = g1 / (g1 * g1 - 0.5f * g * g2);
  153. float t_g = -g * u_g;
  154. float b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s - 1.f;
  155. float b1 = -0.0041960863f * ldt - 0.7034186147f * mdt + 1.7076147010f * sdt;
  156. float b2 = -0.0041960863f * ldt2 - 0.7034186147f * mdt2 + 1.7076147010f * sdt2;
  157. float u_b = b1 / (b1 * b1 - 0.5f * b * b2);
  158. float t_b = -b * u_b;
  159. t_r = u_r >= 0.f ? t_r : 10000.f;
  160. t_g = u_g >= 0.f ? t_g : 10000.f;
  161. t_b = u_b >= 0.f ? t_b : 10000.f;
  162. t += min(t_r, min(t_g, t_b));
  163. }
  164. }
  165. }
  166. return t;
  167. }
  168. float find_gamut_intersection(float a, float b, float L1, float C1, float L0){ vec2 cusp = find_cusp(a, b);
  169. return find_gamut_intersection(a, b, L1, C1, L0, cusp);
  170. }
  171. vec3 gamut_clip_preserve_chroma(vec3 rgb){
  172. if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
  173. return rgb;
  174. vec3 lab = linear_srgb_to_oklab(rgb);
  175. float L = lab.x;
  176. float eps = 0.00001f;
  177. float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
  178. float a_ = lab.y / C;
  179. float b_ = lab.z / C;
  180. float L0 = clamp(L, 0.f, 1.f);
  181. float t = find_gamut_intersection(a_, b_, L, C, L0);
  182. float L_clipped = L0 * (1.f - t) + t * L;
  183. float C_clipped = t * C;
  184. return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
  185. }
  186. vec3 gamut_clip_project_to_0_5(vec3 rgb){
  187. if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
  188. return rgb;
  189. vec3 lab = linear_srgb_to_oklab(rgb);
  190. float L = lab.x;
  191. float eps = 0.00001f;
  192. float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
  193. float a_ = lab.y / C;
  194. float b_ = lab.z / C;
  195. float L0 = 0.5;
  196. float t = find_gamut_intersection(a_, b_, L, C, L0);
  197. float L_clipped = L0 * (1.f - t) + t * L;
  198. float C_clipped = t * C;
  199. return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
  200. }
  201. vec3 gamut_clip_project_to_L_cusp(vec3 rgb){
  202. if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
  203. return rgb;
  204. vec3 lab = linear_srgb_to_oklab(rgb);
  205. float L = lab.x;
  206. float eps = 0.00001f;
  207. float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
  208. float a_ = lab.y / C;
  209. float b_ = lab.z / C; vec2 cusp = find_cusp(a_, b_);
  210. float L0 = cusp.x;
  211. float t = find_gamut_intersection(a_, b_, L, C, L0);
  212. float L_clipped = L0 * (1.f - t) + t * L;
  213. float C_clipped = t * C;
  214. return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
  215. }
  216. vec3 gamut_clip_adaptive_L0_0_5(vec3 rgb, float alpha){
  217. if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
  218. return rgb;
  219. vec3 lab = linear_srgb_to_oklab(rgb);
  220. float L = lab.x;
  221. float eps = 0.00001f;
  222. float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
  223. float a_ = lab.y / C;
  224. float b_ = lab.z / C;
  225. float Ld = L - 0.5f;
  226. float e1 = 0.5f + abs(Ld) + alpha * C;
  227. float L0 = 0.5f * (1.f + sign(Ld) * (e1 - sqrt(e1 * e1 - 2.f * abs(Ld))));
  228. float t = find_gamut_intersection(a_, b_, L, C, L0);
  229. float L_clipped = L0 * (1.f - t) + t * L;
  230. float C_clipped = t * C;
  231. return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
  232. }
  233. vec3 gamut_clip_adaptive_L0_L_cusp(vec3 rgb, float alpha){
  234. if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
  235. return rgb;
  236. vec3 lab = linear_srgb_to_oklab(rgb);
  237. float L = lab.x;
  238. float eps = 0.00001f;
  239. float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
  240. float a_ = lab.y / C;
  241. float b_ = lab.z / C; vec2 cusp = find_cusp(a_, b_);
  242. float Ld = L - cusp.x;
  243. float k = 2.f * (Ld > 0.f ? 1.f - cusp.x : cusp.x);
  244. float e1 = 0.5f * k + abs(Ld) + alpha * C / k;
  245. float L0 = cusp.x + 0.5f * (sign(Ld) * (e1 - sqrt(e1 * e1 - 2.f * k * abs(Ld))));
  246. float t = find_gamut_intersection(a_, b_, L, C, L0);
  247. float L_clipped = L0 * (1.f - t) + t * L;
  248. float C_clipped = t * C;
  249. return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
  250. }
  251. float toe(float x){
  252. float k_1 = 0.206f;
  253. float k_2 = 0.03f;
  254. float k_3 = (1.f + k_1) / (1.f + k_2);
  255. return 0.5f * (k_3 * x - k_1 + sqrt((k_3 * x - k_1) * (k_3 * x - k_1) + 4.f * k_2 * k_3 * x));
  256. }
  257. float toe_inv(float x){
  258. float k_1 = 0.206f;
  259. float k_2 = 0.03f;
  260. float k_3 = (1.f + k_1) / (1.f + k_2);
  261. return (x * x + k_1 * x) / (k_3 * (x + k_2));
  262. }
  263. vec2 to_ST(vec2 cusp){
  264. float L = cusp.x;
  265. float C = cusp.y;
  266. return vec2( C / L, C / (1.f - L) );
  267. }
  268. vec2 get_ST_mid(float a_, float b_){
  269. float S = 0.11516993f + 1.f / (
  270. +7.44778970f + 4.15901240f * b_
  271. + a_ * (-2.19557347f + 1.75198401f * b_
  272. + a_ * (-2.13704948f - 10.02301043f * b_
  273. + a_ * (-4.24894561f + 5.38770819f * b_ + 4.69891013f * a_
  274. )))
  275. );
  276. float T = 0.11239642f + 1.f / (
  277. +1.61320320f - 0.68124379f * b_
  278. + a_ * (+0.40370612f + 0.90148123f * b_
  279. + a_ * (-0.27087943f + 0.61223990f * b_
  280. + a_ * (+0.00299215f - 0.45399568f * b_ - 0.14661872f * a_
  281. )))
  282. );
  283. return vec2( S, T );
  284. }
  285. vec3 get_Cs(float L, float a_, float b_){
  286. vec2 cusp = find_cusp(a_, b_);
  287. float C_max = find_gamut_intersection(a_, b_, L, 1.f, L, cusp);
  288. vec2 ST_max = to_ST(cusp); float k = C_max / min((L * ST_max.x), (1.f - L) * ST_max.y);
  289. float C_mid;{
  290. vec2 ST_mid = get_ST_mid(a_, b_); float C_a = L * ST_mid.x;
  291. float C_b = (1.f - L) * ST_mid.y;
  292. C_mid = 0.9f * k * sqrt(sqrt(1.f / (1.f / (C_a * C_a * C_a * C_a) + 1.f / (C_b * C_b * C_b * C_b))));
  293. }
  294. float C_0;{ float C_a = L * 0.4f;
  295. float C_b = (1.f - L) * 0.8f; C_0 = sqrt(1.f / (1.f / (C_a * C_a) + 1.f / (C_b * C_b)));
  296. }
  297. return vec3( C_0, C_mid, C_max );
  298. }
  299. vec3 okhsl_to_srgb(vec3 hsl){
  300. float h = hsl.x;
  301. float s = hsl.y;
  302. float l = hsl.z;
  303. if (l == 1.0f){
  304. return vec3( 1.f, 1.f, 1.f );
  305. }
  306. else if (l == 0.f){
  307. return vec3( 0.f, 0.f, 0.f );
  308. }
  309. float a_ = cos(2.f * M_PI * h);
  310. float b_ = sin(2.f * M_PI * h);
  311. float L = toe_inv(l);
  312. vec3 cs = get_Cs(L, a_, b_);
  313. float C_0 = cs.x;
  314. float C_mid = cs.y;
  315. float C_max = cs.z;
  316. float mid = 0.8f;
  317. float mid_inv = 1.25f;
  318. float C, t, k_0, k_1, k_2;
  319. if (s < mid){
  320. t = mid_inv * s;
  321. k_1 = mid * C_0;
  322. k_2 = (1.f - k_1 / C_mid);
  323. C = t * k_1 / (1.f - k_2 * t);
  324. }
  325. else{
  326. t = (s - mid)/ (1.f - mid);
  327. k_0 = C_mid;
  328. k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
  329. k_2 = (1.f - (k_1) / (C_max - C_mid));
  330. C = k_0 + t * k_1 / (1.f - k_2 * t);
  331. }
  332. vec3 rgb = oklab_to_linear_srgb(vec3( L, C * a_, C * b_ ));
  333. return vec3(
  334. srgb_transfer_function(rgb.r),
  335. srgb_transfer_function(rgb.g),
  336. srgb_transfer_function(rgb.b)
  337. );
  338. }
  339. vec3 okhsl_to_linear_srgb(vec3 hsl){
  340. float h = hsl.x;
  341. float s = hsl.y;
  342. float l = hsl.z;
  343. if (l == 1.0f){
  344. return vec3( 1.f, 1.f, 1.f );
  345. }
  346. else if (l == 0.f){
  347. return vec3( 0.f, 0.f, 0.f );
  348. }
  349. float a_ = cos(2.f * M_PI * h);
  350. float b_ = sin(2.f * M_PI * h);
  351. float L = toe_inv(l);
  352. vec3 cs = get_Cs(L, a_, b_);
  353. float C_0 = cs.x;
  354. float C_mid = cs.y;
  355. float C_max = cs.z;
  356. float mid = 0.8f;
  357. float mid_inv = 1.25f;
  358. float C, t, k_0, k_1, k_2;
  359. if (s < mid){
  360. t = mid_inv * s;
  361. k_1 = mid * C_0;
  362. k_2 = (1.f - k_1 / C_mid);
  363. C = t * k_1 / (1.f - k_2 * t);
  364. }
  365. else{
  366. t = (s - mid)/ (1.f - mid);
  367. k_0 = C_mid;
  368. k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
  369. k_2 = (1.f - (k_1) / (C_max - C_mid));
  370. C = k_0 + t * k_1 / (1.f - k_2 * t);
  371. }
  372. return oklab_to_linear_srgb(vec3( L, C * a_, C * b_ ));
  373. }
  374. vec3 okhsl_to_xyz(vec3 hsl){
  375. float h = hsl.x;
  376. float s = hsl.y;
  377. float l = hsl.z;
  378. if (l == 1.0f){
  379. return vec3( 1.f, 1.f, 1.f );
  380. }
  381. else if (l == 0.f){
  382. return vec3( 0.f, 0.f, 0.f );
  383. }
  384. float a_ = cos(2.f * M_PI * h);
  385. float b_ = sin(2.f * M_PI * h);
  386. float L = toe_inv(l);
  387. vec3 cs = get_Cs(L, a_, b_);
  388. float C_0 = cs.x;
  389. float C_mid = cs.y;
  390. float C_max = cs.z;
  391. float mid = 0.8f;
  392. float mid_inv = 1.25f;
  393. float C, t, k_0, k_1, k_2;
  394. if (s < mid){
  395. t = mid_inv * s;
  396. k_1 = mid * C_0;
  397. k_2 = (1.f - k_1 / C_mid);
  398. C = t * k_1 / (1.f - k_2 * t);
  399. }
  400. else{
  401. t = (s - mid)/ (1.f - mid);
  402. k_0 = C_mid;
  403. k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
  404. k_2 = (1.f - (k_1) / (C_max - C_mid));
  405. C = k_0 + t * k_1 / (1.f - k_2 * t);
  406. }
  407. return oklab_to_xyz(vec3( L, C * a_, C * b_ ));
  408. }
  409. vec3 srgb_to_okhsl(vec3 rgb){
  410. vec3 lab = linear_srgb_to_oklab(vec3(
  411. srgb_transfer_function_inv(rgb.r),
  412. srgb_transfer_function_inv(rgb.g),
  413. srgb_transfer_function_inv(rgb.b)
  414. ));
  415. float C = sqrt(lab.y * lab.y + lab.z * lab.z);
  416. float a_ = lab.y / C;
  417. float b_ = lab.z / C;
  418. float L = lab.x;
  419. float h = 0.5f + 0.5f * atan(-lab.z, -lab.y) / M_PI;
  420. vec3 cs = get_Cs(L, a_, b_);
  421. float C_0 = cs.x;
  422. float C_mid = cs.y;
  423. float C_max = cs.z;
  424. float mid = 0.8f;
  425. float mid_inv = 1.25f;
  426. float s;
  427. if (C < C_mid){
  428. float k_1 = mid * C_0;
  429. float k_2 = (1.f - k_1 / C_mid);
  430. float t = C / (k_1 + k_2 * C);
  431. s = t * mid;
  432. }
  433. else{
  434. float k_0 = C_mid;
  435. float k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
  436. float k_2 = (1.f - (k_1) / (C_max - C_mid));
  437. float t = (C - k_0) / (k_1 + k_2 * (C - k_0));
  438. s = mid + (1.f - mid) * t;
  439. }
  440. float l = toe(L);
  441. return vec3( h, s, l );
  442. }
  443. vec3 okhsv_to_srgb(vec3 hsv){
  444. float h = hsv.x;
  445. float s = hsv.y;
  446. float v = hsv.z;
  447. float a_ = cos(2.f * M_PI * h);
  448. float b_ = sin(2.f * M_PI * h);
  449. vec2 cusp = find_cusp(a_, b_);
  450. vec2 ST_max = to_ST(cusp);
  451. float S_max = ST_max.x;
  452. float T_max = ST_max.y;
  453. float S_0 = 0.5f;
  454. float k = 1.f- S_0 / S_max; float L_v = 1.f - s * S_0 / (S_0 + T_max - T_max * k * s);
  455. float C_v = s * T_max * S_0 / (S_0 + T_max - T_max * k * s);
  456. float L = v * L_v;
  457. float C = v * C_v; float L_vt = toe_inv(L_v);
  458. float C_vt = C_v * L_vt / L_v;
  459. float L_new = toe_inv(L);
  460. C = C * L_new / L;
  461. L = L_new;
  462. vec3 rgb_scale = oklab_to_linear_srgb(vec3( L_vt, a_ * C_vt, b_ * C_vt ));
  463. float scale_L = cbrt(1.f / max(max(rgb_scale.r, rgb_scale.g), max(rgb_scale.b, 0.f)));
  464. L = L * scale_L;
  465. C = C * scale_L;
  466. vec3 rgb = oklab_to_linear_srgb(vec3( L, C * a_, C * b_ ));
  467. return vec3(
  468. srgb_transfer_function(rgb.r),
  469. srgb_transfer_function(rgb.g),
  470. srgb_transfer_function(rgb.b)
  471. );
  472. })" R"(
  473. vec3 srgb_to_okhsv(vec3 rgb){
  474. vec3 lab = linear_srgb_to_oklab(vec3(
  475. srgb_transfer_function_inv(rgb.r),
  476. srgb_transfer_function_inv(rgb.g),
  477. srgb_transfer_function_inv(rgb.b)
  478. ));
  479. float C = sqrt(lab.y * lab.y + lab.z * lab.z);
  480. float a_ = lab.y / C;
  481. float b_ = lab.z / C;
  482. float L = lab.x;
  483. float h = 0.5f + 0.5f * atan(-lab.z, -lab.y) / M_PI;
  484. vec2 cusp = find_cusp(a_, b_);
  485. vec2 ST_max = to_ST(cusp);
  486. float S_max = ST_max.x;
  487. float T_max = ST_max.y;
  488. float S_0 = 0.5f;
  489. float k = 1.f - S_0 / S_max;
  490. float t = T_max / (C + L * T_max);
  491. float L_v = t * L;
  492. float C_v = t * C;
  493. float L_vt = toe_inv(L_v);
  494. float C_vt = C_v * L_vt / L_v; vec3 rgb_scale = oklab_to_linear_srgb(vec3( L_vt, a_ * C_vt, b_ * C_vt ));
  495. float scale_L = cbrt(1.f / max(max(rgb_scale.r, rgb_scale.g), max(rgb_scale.b, 0.f)));
  496. L = L / scale_L;
  497. C = C / scale_L;
  498. C = C * toe(L) / L;
  499. L = toe(L);
  500. float v = L / L_v;
  501. float s = (S_0 + T_max) * C_v / ((T_max * S_0) + T_max * k * C_v);
  502. return vec3 (h, s, v );
  503. }
  504. vec3 sRGB2XYZ(vec3 color){
  505. mat3 mat=mat3(vec3(0.4124564,0.3575761,0.1804375),
  506. vec3(0.2126729,0.7151522,0.0721750),
  507. vec3(0.0193339,0.1191920,0.9503041));
  508. return color*mat;
  509. }
  510. vec3 Clay2XYZ(vec3 color){
  511. mat3 mat=mat3(vec3(0.5767309,0.1855540,0.1881852),
  512. vec3(0.2973769,0.6273491,0.0752741),
  513. vec3(0.0270343,0.0706872,0.9911085));
  514. return color*mat;
  515. }
  516. vec3 XYZ2sRGB(vec3 xyz){
  517. mat3 mat=mat3(vec3(3.2404542,-1.5371385,-0.4985314),
  518. vec3(-0.9692660,1.8760108,0.0415560),
  519. vec3(0.0556434,-0.2040259,1.0572252));
  520. return xyz*mat;
  521. }
  522. vec3 XYZ2Clay(vec3 xyz){
  523. mat3 mat=mat3(vec3(2.0413690,-0.5649464,-0.3446944),
  524. vec3(-0.9692660,1.8760108,0.0415560),
  525. vec3(0.0134474,-0.1183897,1.0154096));
  526. return xyz*mat;
  527. }
  528. )";
  529. extern "C" const char* TNS_VERTEX_SIMPLE_MATCAP = R"(#version 330
  530. uniform mat4 mProjection;
  531. uniform mat4 mModel;
  532. uniform mat4 mView;
  533. in vec4 vVertex;
  534. in vec3 vNormal;
  535. smooth out vec3 fNormal;
  536. void main(){
  537. gl_Position = mProjection * mView * mModel * vVertex;
  538. vec3 N = ( mView * mModel * vec4(vNormal,0)).xyz;
  539. fNormal = normalize(N);
  540. })";
  541. extern "C" const char* TNS_FRAGMENT_SIMPLE_MATCAP = R"(#version 330
  542. smooth in vec3 fNormal;
  543. float Interpolate(float between1,float between2,float value1,float value2,float key){
  544. float i = (key-between1)/(between2-between1);
  545. return value1*(1-i)+value2*i;
  546. }
  547. void main(){
  548. float value = dot(vec3(0,0,1),fNormal);
  549. if(value<0.65) value=0.15;
  550. else if(value>=0.65 && value<0.85) value=Interpolate(0.65,0.85,0.15,0.75,value);
  551. else if(value>=0.85 && value<0.95) value=0.75;
  552. else if(value>=0.95) value=0.9;
  553. gl_FragColor = vec4(vec3(0.84, 0.41, 0.16)*value,1);
  554. })";
  555. extern "C" const char* TNS_VERTEX_GRID = R"(#version 330
  556. uniform mat4 mProjection;
  557. uniform mat4 mModel;
  558. uniform mat4 mView;
  559. in vec4 vVertex;
  560. in vec4 vColor;
  561. in vec2 vUV;
  562. out vec4 fColor;
  563. out vec2 uv;
  564. void main(){
  565. vec4 pos = mProjection * mView * mModel * vVertex;
  566. gl_Position = pos;
  567. fColor = vColor;
  568. uv = vUV;
  569. })";
  570. extern "C" const char* TNS_FRAGMENT_TRANSPARNT_GRID = R"(#version 330
  571. in vec4 fColor;
  572. in vec2 uv;
  573. void main(){
  574. vec4 c = fColor;
  575. c.a = sin(uv.x)*sin(uv.y)>0?c.a:0;
  576. gl_FragColor = c;
  577. })";
  578. extern "C" const char* LA_IMM_VERTEX_SHADER = R"(#version 330
  579. uniform mat4 mProjection;\
  580. uniform mat4 mModel;\
  581. uniform mat4 mView;\
  582. in vec4 vVertex;\
  583. in vec4 vColor;\
  584. in vec3 vNormal;\
  585. in vec2 vUV;\
  586. out vec4 fColor;\
  587. out vec2 fUV;\
  588. flat out vec3 fNormal;\
  589. out vec3 fGPos;\
  590. void main(){\
  591. gl_Position = mProjection * mView * mModel * vVertex;\
  592. fColor = vColor;\
  593. fUV=vUV;\
  594. fGPos=vec3((mModel * vVertex).xyz);\
  595. fNormal= (mModel * vec4(vNormal,0.)).xyz;\
  596. })";
  597. extern "C" const char* LA_IMM_FRAGMENT_SHADER = R"(#version 330
  598. uniform sampler2D TexColor;\
  599. uniform sampler2DMS TexColorMS;\
  600. uniform int TextureMode;
  601. uniform int ColorMode;
  602. uniform int MultiplyColor;
  603. uniform int SampleAmount;
  604. uniform int UseNormal;
  605. uniform int InputColorSpace;
  606. uniform int OutputColorSpace;
  607. uniform int ShowStripes;
  608. uniform float HCYGamma;
  609. uniform vec3 uViewPos;
  610. in vec4 fColor;
  611. in vec2 fUV;
  612. flat in vec3 fNormal;
  613. in vec3 fGPos;
  614. layout(location = 0) out vec4 outColor;
  615. layout(location = 1) out vec3 outNormal;
  616. layout(location = 2) out vec3 outGPos;
  617. #with TNS_SHADER_COLOR_COMMON
  618. vec3 ConvertColorSpace(vec3 color){
  619. if(InputColorSpace!=OutputColorSpace){
  620. if(ColorMode==0){
  621. if(InputColorSpace==0) color=to_linear_srgb(color);
  622. else if(InputColorSpace==1) color=to_linear_clay(color);
  623. }
  624. vec3 xyz; if(ColorMode==1){ color.y=pow(color.y,max(HCYGamma,1)); color=okhsl_to_linear_srgb(color); }
  625. if(InputColorSpace==1){ xyz=Clay2XYZ(color); }
  626. if(InputColorSpace==0){ xyz=sRGB2XYZ(color); }
  627. if(OutputColorSpace==0){ color=to_log_srgb(XYZ2sRGB(xyz)); }
  628. if(OutputColorSpace==1){ color=to_log_clay(XYZ2Clay(xyz)); }
  629. }else{
  630. if(ColorMode==1){ color.y=pow(color.y,max(HCYGamma,1)); color=okhsl_to_srgb(color); }
  631. else if(ColorMode==0){ color=color; }
  632. else{
  633. if(OutputColorSpace==0){ color=to_log_srgb(color); }
  634. if(OutputColorSpace==1){ color=to_log_clay(color); }
  635. }
  636. }
  637. if(ShowStripes!=0){
  638. if(color.r>1.00001||color.g>1.00001||color.b>1.00001||color.r<0||color.g<0||color.b<0){ color=mix(color,vec3(0.5,0.5,0.5),(sin((gl_FragCoord.x+gl_FragCoord.y)/2)>0)?1:0.5); }
  639. }
  640. return color;
  641. }
  642. void main(){
  643. vec4 color=vec4(1,0,1,1);
  644. if(TextureMode==0){ color = fColor;}
  645. else if(TextureMode==1){color = vec4(fColor.rgb,fColor.a*texture2D(TexColor,fUV.st).r);}
  646. else if(TextureMode==2){
  647. color=texture2D(TexColor,fUV.st);
  648. if(MultiplyColor!=0){color*=fColor;}
  649. }else if(TextureMode==3){
  650. color=vec4(0,0,0,0);
  651. ivec2 texSize = textureSize(TexColorMS);
  652. for(int i=0;i<SampleAmount;i++) color+=texelFetch(TexColorMS, ivec2(fUV * texSize),i);
  653. color/=SampleAmount;
  654. if(MultiplyColor!=0){color*=fColor;}
  655. }
  656. if(UseNormal!=0){
  657. float light_factor=dot(fNormal,vec3(0,0,1));
  658. float view=dot(fNormal,fGPos-uViewPos);
  659. float factor=abs(light_factor);
  660. if(light_factor*view>0){ factor=0; }
  661. color=vec4(color.rgb*mix(0.2,1.,factor),color.a);
  662. vec3 oNormal=fNormal; if(view<0){ oNormal=-fNormal; }
  663. outNormal = oNormal;
  664. }
  665. color=vec4(ConvertColorSpace(color.rgb),color.a); color.a=clamp(color.a,0,1);
  666. outColor = color;
  667. outGPos = fGPos;
  668. })";
  669. extern "C" const char* LA_RAY_VERTEX_SHADER = R"(#version 330
  670. in vec3 vUV;
  671. in vec4 vVertex;
  672. out vec3 fViewDir;
  673. void main(){
  674. gl_Position=vVertex;
  675. fViewDir = vUV;
  676. })";
  677. extern "C" const char* LA_SHADER_LIB_FXAA = R"(
  678. #define DIFF_LUM_ABS_HOLD 0.0833
  679. #define DIFF_LUM_RES_HOLD 0.166
  680. float luminance(vec3 col) {
  681. return dot(col, vec3(0.2126729f, 0.7151522f, 0.0721750f));
  682. }
  683. vec4 fxaa(in sampler2D tex, vec2 uv, vec2 texsize) {
  684. vec3 e = vec3(-1., 1., 0.);
  685. vec2 offuv = uv;
  686. vec3 colnw = texture(tex, uv + e.xy / texsize).rgb;
  687. vec3 coln = texture(tex, uv + e.zy / texsize).rgb;
  688. vec3 colne = texture(tex, uv + e.yy / texsize).rgb;
  689. vec3 colw = texture(tex, uv + e.xz / texsize).rgb;
  690. vec4 colm4 = texture(tex, uv + e.zz / texsize);
  691. vec3 colm = colm4.rgb;
  692. vec3 cole = texture(tex, uv + e.yz / texsize).rgb;
  693. vec3 colsw = texture(tex, uv + e.xx / texsize).rgb;
  694. vec3 cols = texture(tex, uv + e.zx / texsize).rgb;
  695. vec3 colse = texture(tex, uv + e.yx / texsize).rgb;
  696. float lnw = luminance(colnw), ln = luminance(coln), lne = luminance(colne),
  697. lw = luminance(colw), lm = luminance(colm), le = luminance(cole),
  698. lsw = luminance(colsw), ls = luminance(cols), lse = luminance(colse);
  699. float maxl = max(ln, max(ls, max(lw, max(le, lm))));
  700. float minl = min(ln, min(ls, min(lw, min(le, lm))));
  701. float diff = maxl - minl;
  702. if (diff < max(DIFF_LUM_ABS_HOLD, DIFF_LUM_RES_HOLD * maxl)) return colm4;
  703. float filterfactor = 0.;
  704. filterfactor += 2. * (ln + lw + ls + le) + lnw + lne + lsw + lse;
  705. filterfactor /= 12.;
  706. filterfactor = abs(filterfactor - lm);
  707. filterfactor = clamp(filterfactor / diff, 0., 1.);
  708. float blend = smoothstep(0., 1., filterfactor);
  709. blend *= blend;
  710. float hedge = 2.*(ln + ls - 2.*lm) + (lne + lse - 2.*le) + (lnw + lsw - 2.*lw);
  711. float vedge = 2.*(le + lw - 2.*lm) + (lne + lnw - 2.*ln) + (lse + lsw - 2.*ls);
  712. float ish = step(vedge, hedge);
  713. float psoff = ish >= 1.0 ? 1./texsize.y : 1./texsize.x;
  714. float pleft = ish >= 1.0 ? ln : le;
  715. float pright = ish >= 1.0 ? ls : lw;
  716. if (abs(pleft - lm) < abs(pright - lm)) psoff = -psoff;
  717. if (ish >= 1.0) { offuv.y += psoff * blend; }else{ offuv.x += psoff * blend; }
  718. return vec4(texture(tex, offuv).rgb,colm4.a);
  719. })";
  720. extern "C" const char* LA_RAY_FRAGMENT_SHADER = R"(#version 330
  721. uniform vec3 uViewDir;
  722. uniform vec3 uViewPos;
  723. uniform float uFOV;
  724. in vec3 fViewDir;
  725. uniform sampler2D TexColor;
  726. uniform sampler2D TexNormal;
  727. uniform sampler2D TexGPos;
  728. #with LA_SHADER_LIB_FXAA
  729. void main(){
  730. float d=dot(uViewDir,normalize(fViewDir));
  731. float target=cos(uFOV/2.);
  732. vec4 color=vec4(1.,1.,1.,1.); float mul=0.;
  733. //if(d<(target+0.005)&&d>target) mul=1.0;
  734. vec2 uv=gl_FragCoord.xy/textureSize(TexColor,0);
  735. vec4 buffer_color=fxaa(TexColor,uv,textureSize(TexColor,0));
  736. //vec4 buffer_color=texture2D(TexColor,uv);
  737. gl_FragColor = mul*color+buffer_color;
  738. })";
  739. extern "C" const char* LA_SCENE_VERTEX_SHADER = R"(#version 330
  740. uniform mat4 mProjection;
  741. uniform mat4 mModel;
  742. uniform mat4 mView;
  743. uniform mat4 mShadow;
  744. in vec4 vVertex;
  745. in vec4 vColor;
  746. in vec4 vNormal;
  747. in vec2 vUV;
  748. out vec4 fColor;
  749. //out vec4 fNormal;
  750. out vec2 fUV;
  751. out vec4 fGPos;
  752. void main(){
  753. gl_Position= mProjection * mView * mModel * vVertex;
  754. fUV=vUV;
  755. //fNormal=vNormal;
  756. fColor=vColor;
  757. fGPos= mShadow * mModel * vVertex;\
  758. })";
  759. extern "C" const char* LA_SCENE_FRAGMENT_SHADER = R"(#version 330
  760. uniform sampler2D TexColor;
  761. uniform sampler2DMS TexColorMS;\
  762. uniform int TextureMode;
  763. uniform int SampleAmount;
  764. uniform int MultiplyColor;
  765. in vec4 fColor;
  766. //in vec4 fNormal;
  767. in vec2 fUV;
  768. in vec4 fGPos;
  769. vec4 GetTexture(vec2 uv){
  770. vec4 color=vec4(1,0,1,1);
  771. if(TextureMode==1 || TextureMode==2){ return texture2D(TexColor,uv); }
  772. else if(TextureMode==3){
  773. ivec2 texSize = textureSize(TexColorMS);
  774. for(int i=0;i<SampleAmount;i++) color+=texelFetch(TexColorMS, ivec2(fUV * texSize),i);
  775. color/=SampleAmount;
  776. if(MultiplyColor!=0){color*=fColor;}
  777. return color;
  778. }
  779. else return vec4(1,0,1,1);
  780. }
  781. float GetShadow(vec4 GPos){
  782. vec3 projCoords = GPos.xyz / GPos.w;
  783. projCoords = projCoords * 0.5 + 0.5;
  784. float closestDepth = GetTexture(projCoords.xy).r;
  785. float currentDepth = projCoords.z;
  786. float shadow = currentDepth > (closestDepth+0.001) ? 0.5 : 1.0;
  787. return shadow;
  788. }
  789. void main(){
  790. gl_FragColor=GetShadow(fGPos)*fColor;
  791. })";
  792. extern "C" const char* LA_CASCADE_SHADOW_VERTEX_SHADER = R"(#version 330
  793. in vec4 vVertex;
  794. uniform mat4 mModel;
  795. uniform mat4 mShadow;
  796. void main(){
  797. gl_Position=mShadow*mModel*vVertex;
  798. })";
  799. extern "C" const char* LA_CASCADE_SHADOW_FRAGMENT_SHADER = "#version 330\nvoid main(){gl_FragDepth = gl_FragCoord.z;}";
  800. extern "C" const char* LA_SELECTION_VERTEX_SHADER = R"(#version 330
  801. in vec4 vVertex;
  802. in vec3 vColor;
  803. uniform mat4 mProjection;
  804. uniform mat4 mModel;
  805. uniform mat4 mView;
  806. flat out vec3 fIdColor;
  807. void main(){
  808. gl_Position = mProjection * mView * mModel * vVertex;
  809. fIdColor = vColor;
  810. })";
  811. extern "C" const char* LA_SELECTION_FRAGMENT_SHADER = R"(#version 330
  812. flat in vec3 fIdColor;
  813. void main(){
  814. gl_FragColor=vec4(fIdColor,1.);
  815. })";