Advertisement
Guest User

hyllian-updated-new.tweaked.wp

a guest
Jun 26th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OpenGL Shading 25.71 KB | Source Code | 0 0
  1. #version 120
  2.  
  3. /*
  4.    Hyllian's CRT Shader
  5.  
  6.    Copyright (C) 2011-2020 Hyllian - sergiogdb@gmail.com
  7.  
  8.    Copyright (C) 2020, this file ported from Libretro's GLSL
  9.    shader crt-hyllian.glslp to DOSBox-compatible format by Tyrells.
  10.  
  11.    Permission is hereby granted, free of charge, to any person obtaining a copy
  12.    of this software and associated documentation files (the "Software"), to deal
  13.    in the Software without restriction, including without limitation the rights
  14.    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15.    copies of the Software, and to permit persons to whom the Software is
  16.    furnished to do so, subject to the following conditions:
  17.  
  18.    The above copyright notice and this permission notice shall be included in
  19.    all copies or substantial portions of the Software.
  20.  
  21.    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22.    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23.    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24.    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25.    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26.    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27.    THE SOFTWARE.
  28.  
  29. */
  30.  
  31. /*
  32.  
  33. #pragma parameter BEAM_PROFILE "BEAM PROFILE (BP)" 0.0 0.0 2.0 1.0
  34. #pragma parameter HFILTER_PROFILE "HORIZONTAL FILTER PROFILE [ HERMITE | CATMULL-ROM ]" 0.0 0.0 1.0 1.0
  35. #pragma parameter BEAM_MIN_WIDTH "Custom [If BP=0.00] MIN BEAM WIDTH" 1.0 0.0 1.0 0.01
  36. #pragma parameter BEAM_MAX_WIDTH "Custom [If BP=0.00] MAX BEAM WIDTH" 1.0 0.0 1.0 0.01
  37. #pragma parameter SCANLINES_STRENGTH "Custom [If BP=0.00] SCANLINES STRENGTH" 0.58 0.0 1.0 0.01
  38. #pragma parameter COLOR_BOOST "Custom [If BP=0.00] COLOR BOOST" 1.30 1.0 2.0 0.05
  39. #pragma parameter SHARPNESS_HACK "SHARPNESS_HACK" 1.0 1.0 4.0 1.0
  40. #pragma parameter PHOSPHOR_LAYOUT "PHOSPHOR LAYOUT" 4.0 0.0 19.0 1.0
  41. #pragma parameter MASK_INTENSITY "MASK INTENSITY" 0.7 0.0 1.0 0.1
  42. #pragma parameter CRT_ANTI_RINGING "ANTI RINGING" 1.0 0.0 1.0 0.2
  43. #pragma parameter INPUT_GAMMA "INPUT GAMMA" 2.4 0.0 5.0 0.1
  44. #pragma parameter OUTPUT_GAMMA "OUTPUT GAMMA" 2.2 0.0 5.0 0.1
  45. #pragma parameter VSCANLINES "VERTICAL SCANLINES [ OFF | ON ]" 0.0 0.0 1.0 1.0
  46. #pragma parameter BLACK_LEVEL "BLACK LEVEL" 0.0 0.0 0.005 0.0001
  47.  
  48. #pragma parameter WP_ADJUST "White Point Adjustments [ OFF | ON ]" 0.0 0.0 1.0 1.0
  49. #pragma parameter TEMPERATURE "White Point" 9311.0 1031.0 12047.0 72.0
  50. #pragma parameter LUMA_PRESERVE "Preserve Luminance [ OFF | ON ]" 1.0 0.0 1.0 1.0
  51. #pragma parameter WP_RED "Red Shift" 0.0 -1.0 1.0 0.01
  52. #pragma parameter WP_GREEN "Green Shift" 0.0 -1.0 1.0 0.01
  53. #pragma parameter WP_BLUE "Blue Shift" 0.0 -1.0 1.0 0.01
  54.  
  55. */
  56.  
  57. #define GAMMA_IN(color)   pow(color, vec4(INPUT_GAMMA, INPUT_GAMMA, INPUT_GAMMA, INPUT_GAMMA))
  58. #define GAMMA_OUT(color)  pow(color, vec4(1.0 / OUTPUT_GAMMA, 1.0 / OUTPUT_GAMMA, 1.0 / OUTPUT_GAMMA, 1.0 / OUTPUT_GAMMA))
  59.  
  60.  
  61. #define texCoord v_texCoord
  62.  
  63. #if defined(VERTEX)
  64.  
  65. #if __VERSION__ >= 130
  66. #define OUT out
  67. #define IN  in
  68. #define tex2D texture
  69. #else
  70. #define OUT varying
  71. #define IN attribute
  72. #define tex2D texture2D
  73. #endif
  74.  
  75. #ifdef GL_ES
  76. #define PRECISION mediump
  77. #else
  78. #define PRECISION
  79. #endif
  80.  
  81.  
  82. IN  vec4 a_position;
  83. IN  vec4 Color;
  84. IN  vec2 TexCoord;
  85. OUT vec4 color;
  86. OUT vec2 texCoord;
  87.  
  88. uniform PRECISION vec2 rubyOutputSize;
  89. uniform PRECISION vec2 rubyTextureSize;
  90. uniform PRECISION vec2 rubyInputSize;
  91.  
  92. void main()
  93. {
  94.     gl_Position = a_position;
  95.     v_texCoord = vec2(a_position.x + 1.0, 1.0 - a_position.y) / 2.0 * rubyInputSize / rubyTextureSize;
  96. }
  97.  
  98.  
  99. #elif defined(FRAGMENT)
  100.  
  101. #if __VERSION__ >= 130
  102. #define IN in
  103. #define tex2D texture
  104. out vec4 FragColor;
  105. #else
  106. #define IN varying
  107. #define FragColor gl_FragColor
  108. #define tex2D texture2D
  109. #endif
  110.  
  111. #ifdef GL_ES
  112. #ifdef GL_FRAGMENT_PRECISION_HIGH
  113. precision highp float;
  114. #else
  115. precision mediump float;
  116. #endif
  117. #define PRECISION mediump
  118. #else
  119. #define PRECISION
  120. #endif
  121.  
  122. uniform PRECISION vec2 rubyOutputSize;
  123. uniform PRECISION vec2 rubyTextureSize;
  124. uniform PRECISION vec2 rubyInputSize;
  125. uniform sampler2D s_p;
  126. IN vec2 texCoord;
  127.  
  128. #ifdef PARAMETER_UNIFORM
  129. uniform PRECISION float BEAM_PROFILE;
  130. uniform PRECISION float HFILTER_PROFILE;
  131. uniform PRECISION float BEAM_MIN_WIDTH;
  132. uniform PRECISION float BEAM_MAX_WIDTH;
  133. uniform PRECISION float SCANLINES_STRENGTH;
  134. uniform PRECISION float COLOR_BOOST;
  135. uniform PRECISION float SHARPNESS_HACK;
  136. uniform PRECISION float PHOSPHOR_LAYOUT;
  137. uniform PRECISION float MASK_INTENSITY;
  138. uniform PRECISION float CRT_ANTI_RINGING;
  139. uniform PRECISION float INPUT_GAMMA;
  140. uniform PRECISION float OUTPUT_GAMMA;
  141. uniform PRECISION float VSCANLINES;
  142.  
  143. uniform PRECISION float WP_ADJUST;
  144. uniform PRECISION float TEMPERATURE;
  145. uniform PRECISION float LUMA_PRESERVE;
  146. uniform PRECISION float RED_SHIFT;
  147. uniform PRECISION float GREEN_SHIFT;
  148. uniform PRECISION float BLUE_SHIFT;
  149.  
  150. #else
  151. #define BEAM_PROFILE        0.00
  152. #define HFILTER_PROFILE     0.00
  153. #define BEAM_MIN_WIDTH      0.90
  154. #define BEAM_MAX_WIDTH      1.10
  155. #define SCANLINES_STRENGTH  0.80
  156. #define COLOR_BOOST         6.00
  157. #define SHARPNESS_HACK      1.00
  158. #define PHOSPHOR_LAYOUT    11.00
  159. #define MASK_INTENSITY      0.85
  160. #define CRT_ANTI_RINGING    1.00
  161. #define INPUT_GAMMA         2.40
  162. #define OUTPUT_GAMMA        2.10
  163. #define VSCANLINES          0.00
  164. #define BLACK_LEVEL         0.0001
  165.  
  166. #define WP_ADJUST           1.00
  167. #define TEMPERATURE      9300.00
  168. #define LUMA_PRESERVE       1.00
  169. #define RED_SHIFT           0.00
  170. #define GREEN_SHIFT         0.00
  171. #define BLUE_SHIFT          0.00
  172. #endif
  173. // END PARAMETERS //
  174.  
  175.  
  176. // White Point Mapping
  177. //          ported by Dogway
  178. //
  179. // From the first comment post (sRGB primaries and linear light compensated)
  180. //      http://www.zombieprototypes.com/?p=210#comment-4695029660
  181. // Based on the Neil Bartlett's blog update
  182. //      http://www.zombieprototypes.com/?p=210
  183. // Inspired itself by Tanner Helland's work
  184. //      http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
  185.  
  186. vec3 wp_adjust(vec3 color){
  187.     float temp = TEMPERATURE / 100.;
  188.     float k = TEMPERATURE / 10000.;
  189.     float lk = log(k);
  190.  
  191.     vec3 wp = vec3(1.);
  192.  
  193.     // calculate RED
  194.     wp.r = (temp <= 65.) ? 1. : 0.32068362618584273 + (0.19668730877673762 * pow(k - 0.21298613432655075, - 1.5139012907556737)) + (- 0.013883432789258415 * lk);
  195.  
  196.     // calculate GREEN
  197.     float mg = 1.226916242502167 + (- 1.3109482654223614 * pow(k - 0.44267061967913873, 3.) * exp(- 5.089297600846147 * (k - 0.44267061967913873))) + (0.6453936305542096 * lk);
  198.     float pg = 0.4860175851734596 + (0.1802139719519286 * pow(k - 0.14573069517701578, - 1.397716496795082)) + (- 0.00803698899233844 * lk);
  199.     wp.g = (temp <= 65.5) ? ((temp <= 8.) ? 0. : mg) : pg;
  200.  
  201.     // calculate BLUE
  202.     wp.b = (temp <= 19.) ? 0. : (temp >= 66.) ? 1. : 1.677499032830161 + (- 0.02313594016938082 * pow(k - 1.1367244820333684, 3.) * exp(- 4.221279555918655 * (k - 1.1367244820333684))) + (1.6550275798913296 * lk);
  203.  
  204.     // clamp
  205.     wp.rgb = clamp(wp.rgb, vec3(0.), vec3(1.));
  206.  
  207.     // R/G/B independent manual White Point adjustment
  208.     wp.rgb += vec3(RED_SHIFT, GREEN_SHIFT, BLUE_SHIFT);
  209.  
  210.     // Linear color input
  211.     return color * wp;
  212. }
  213.  
  214. vec3 sRGB_to_XYZ(vec3 RGB){
  215.     const mat3x3 m = mat3x3(
  216.     0.4124564,  0.3575761,  0.1804375,
  217.     0.2126729,  0.7151522,  0.0721750,
  218.     0.0193339,  0.1191920,  0.9503041);
  219.     return RGB * m;
  220. }
  221.  
  222.  
  223. vec3 XYZtoYxy(vec3 XYZ){
  224.     float XYZrgb = XYZ.r+XYZ.g+XYZ.b;
  225.     float Yxyr = XYZ.g;
  226.     float Yxyg = (XYZrgb <= 0.0) ? 0.3805 : XYZ.r / XYZrgb;
  227.     float Yxyb = (XYZrgb <= 0.0) ? 0.3769 : XYZ.g / XYZrgb;
  228.     return vec3(Yxyr,Yxyg,Yxyb);
  229. }
  230.  
  231. vec3 XYZ_to_sRGB(vec3 XYZ){
  232.     const mat3x3 m = mat3x3(
  233.     3.2404542, -1.5371385, -0.4985314,
  234.    -0.9692660,  1.8760108,  0.0415560,
  235.     0.0556434, -0.2040259,  1.0572252);
  236.     return XYZ * m;
  237. }
  238.  
  239.  
  240. vec3 YxytoXYZ(vec3 Yxy) {
  241.     float Xs = Yxy.r * (Yxy.g/Yxy.b);
  242.     float Xsz = (Yxy.r <= 0.0) ? 0 : 1;
  243.     vec3 XYZ = vec3(Xsz,Xsz,Xsz) * vec3(Xs, Yxy.r, (Xs/Yxy.g)-Xs-Yxy.r);
  244.     return XYZ;
  245. }
  246.  
  247.  
  248. /*
  249.     A collection of CRT mask effects that work with LCD subpixel structures for
  250.     small details
  251.  
  252.     author: hunterk
  253.     license: public domain
  254.  
  255.     How to use it:
  256.  
  257.     Multiply your image by the vec3 output:
  258.     FragColor.rgb *= mask_weights(gl_FragCoord.xy, 1.0, 1);
  259.  
  260.     The function needs to be tiled across the screen using the physical pixels, e.g.
  261.     gl_FragCoord (the "vec2 coord" input). In the case of slang shaders, we use
  262.     (vTexCoord.st * OutputSize.xy).
  263.  
  264.     The "mask_intensity" (float value between 0.0 and 1.0) is how strong the mask
  265.     effect should be. Full-strength red, green and blue subpixels on a white pixel
  266.     are the ideal, and are achieved with an intensity of 1.0, though this darkens
  267.     the image significantly and may not always be desirable.
  268.  
  269.     The "phosphor_layout" (int value between 0 and 19) determines which phophor
  270.     layout to apply. 0 is no mask/passthru.
  271.  
  272.     Many of these mask arrays are adapted from cgwg's crt-geom-deluxe LUTs, and
  273.     those have their filenames included for easy identification
  274. */
  275. vec3 mask_weights(vec2 coord, float mask_intensity, int phosphor_layout) {
  276.     vec3 weights = vec3(1.,1.,1.);
  277.     float on = 1.;
  278.     float off = 1.-mask_intensity;
  279.     vec3 red     = vec3(on,  off, off);
  280.     vec3 green   = vec3(off, on,  off);
  281.     vec3 blue    = vec3(off, off, on );
  282.     vec3 magenta = vec3(on,  off, on );
  283.     vec3 yellow  = vec3(on,  on,  off);
  284.     vec3 cyan    = vec3(off, on,  on );
  285.     vec3 black   = vec3(off, off, off);
  286.     vec3 white   = vec3(on,  on,  on );
  287.     int w, z = 0;
  288.  
  289.     // This pattern is used by a few layouts, so we'll define it here
  290.     vec3 aperture_weights = mix(magenta, green, floor(mod(coord.x, 2.0)));
  291.  
  292.     if (phosphor_layout == 0) {
  293.         return weights;
  294.  
  295.     } else if (phosphor_layout == 1) {
  296.         // classic aperture for RGB panels; good for 1080p, too small for 4K+
  297.         // aka aperture_1_2_bgr
  298.         weights  = aperture_weights;
  299.         return weights;
  300.  
  301.     } else if (phosphor_layout == 2) {
  302.         // 2x2 shadow mask for RGB panels; good for 1080p, too small for 4K+
  303.         // aka delta_1_2x1_bgr
  304.         vec3 inverse_aperture = mix(green, magenta, floor(mod(coord.x, 2.0)));
  305.         weights               = mix(aperture_weights, inverse_aperture, floor(mod(coord.y, 2.0)));
  306.         return weights;
  307.  
  308.     } else if (phosphor_layout == 3) {
  309.         // slot mask for RGB panels; looks okay at 1080p, looks better at 4K
  310.         // {magenta, green, black,   black},
  311.         // {magenta, green, magenta, green},
  312.         // {black,   black, magenta, green}
  313.  
  314.         // GLSL can't do 2D arrays until version 430, so do this stupid thing instead for compatibility's sake:
  315.         // First lay out the horizontal pixels in arrays
  316.         vec3 slotmask_x1[4] = vec3[](magenta, green, black,   black);
  317.         vec3 slotmask_x2[4] = vec3[](magenta, green, magenta, green);
  318.         vec3 slotmask_x3[4] = vec3[](black,   black, magenta, green);
  319.  
  320.         // find the vertical index
  321.         w = int(floor(mod(coord.y, 3.0)));
  322.  
  323.         // find the horizontal index
  324.         z = int(floor(mod(coord.x, 4.0)));
  325.  
  326.         // do a big, dumb comparison in place of a 2D array
  327.         weights = (w == 1) ? slotmask_x1[z] : (w == 2) ? slotmask_x2[z] :  slotmask_x3[z];
  328.     }
  329.  
  330.     if (phosphor_layout == 4) {
  331.         // classic aperture for RBG panels; good for 1080p, too small for 4K+
  332.         weights  = mix(yellow, blue, floor(mod(coord.x, 2.0)));
  333.         return weights;
  334.  
  335.     } else if (phosphor_layout == 5) {
  336.         // 2x2 shadow mask for RBG panels; good for 1080p, too small for 4K+
  337.         vec3 inverse_aperture = mix(blue, yellow, floor(mod(coord.x, 2.0)));
  338.  
  339.         weights = mix(
  340.             mix(yellow, blue, floor(mod(coord.x, 2.0))),
  341.             inverse_aperture,
  342.             floor(mod(coord.y, 2.0))
  343.         );
  344.         return weights;
  345.  
  346.     } else if (phosphor_layout == 6) {
  347.         // aperture_1_4_rgb; good for simulating lower
  348.         vec3 ap4[4] = vec3[](red, green, blue, black);
  349.  
  350.         z = int(floor(mod(coord.x, 4.0)));
  351.  
  352.         weights = ap4[z];
  353.         return weights;
  354.  
  355.     } else if (phosphor_layout == 7) {
  356.         // aperture_2_5_bgr
  357.         vec3 ap3[5] = vec3[](red, magenta, blue, green, green);
  358.  
  359.         z = int(floor(mod(coord.x, 5.0)));
  360.  
  361.         weights = ap3[z];
  362.         return weights;
  363.  
  364.     } else if (phosphor_layout == 8){
  365.         // aperture_3_6_rgb
  366.         vec3 big_ap[7] = vec3[](red, red, yellow, green, cyan, blue, blue);
  367.  
  368.         w = int(floor(mod(coord.x, 7.)));
  369.  
  370.         weights = big_ap[w];
  371.         return weights;
  372.  
  373.     } else if (phosphor_layout == 9) {
  374.         // reduced TVL aperture for RGB panels
  375.         // aperture_2_4_rgb
  376.         vec3 big_ap_rgb[4] = vec3[](red, yellow, cyan, blue);
  377.  
  378.         w = int(floor(mod(coord.x, 4.)));
  379.  
  380.         weights = big_ap_rgb[w];
  381.         return weights;
  382.  
  383.     } else if (phosphor_layout == 10) {
  384.         // reduced TVL aperture for RBG panels
  385.         vec3 big_ap_rbg[4] = vec3[](red, magenta, cyan, green);
  386.  
  387.         w = int(floor(mod(coord.x, 4.)));
  388.  
  389.         weights = big_ap_rbg[w];
  390.         return weights;
  391.  
  392.     } else if(phosphor_layout == 11){
  393.        // delta_1_4x1_rgb; dunno why this is called 4x1 when it's obviously 4x2 /shrug
  394.        vec3 delta_1_1[4] = vec3[](red, green, blue, black);
  395.        vec3 delta_1_2[4] = vec3[](blue, black, red, green);
  396.  
  397.        w = int(floor(mod(coord.y, 2.0)));
  398.        z = int(floor(mod(coord.x, 4.0)));
  399.  
  400.        weights = (w == 1) ? delta_1_1[z] : delta_1_2[z];
  401.        return weights;
  402.  
  403.     } else if(phosphor_layout == 12){
  404.        // delta_2_4x1_rgb
  405.        vec3 delta_2_1[4] = vec3[](red, yellow, cyan, blue);
  406.        vec3 delta_2_2[4] = vec3[](cyan, blue, red, yellow);
  407.  
  408.        z = int(floor(mod(coord.x, 4.0)));
  409.  
  410.        weights = (w == 1) ? delta_2_1[z] : delta_2_2[z];
  411.        return weights;
  412.  
  413.     } else if(phosphor_layout == 13){
  414.        // delta_2_4x2_rgb
  415.        vec3 delta_1[4] = vec3[](red, yellow, cyan, blue);
  416.        vec3 delta_2[4] = vec3[](red, yellow, cyan, blue);
  417.        vec3 delta_3[4] = vec3[](cyan, blue, red, yellow);
  418.        vec3 delta_4[4] = vec3[](cyan, blue, red, yellow);
  419.  
  420.        w = int(floor(mod(coord.y, 4.0)));
  421.        z = int(floor(mod(coord.x, 4.0)));
  422.  
  423.        weights = (w == 1) ? delta_1[z] : (w == 2) ? delta_2[z] : (w == 3) ? delta_3[z] : delta_4[z];
  424.        return weights;
  425.  
  426.     } else if(phosphor_layout == 14){
  427.        // slot mask for RGB panels; too low-pitch for 1080p, looks okay at 4K, but wants 8K+
  428.        // {magenta, green, black, black,   black, black},
  429.        // {magenta, green, black, magenta, green, black},
  430.        // {black,   black, black, magenta, green, black}
  431.        vec3 slot2_1[6] = vec3[](magenta, green, black, black,   black, black);
  432.        vec3 slot2_2[6] = vec3[](magenta, green, black, magenta, green, black);
  433.        vec3 slot2_3[6] = vec3[](black,   black, black, magenta, green, black);
  434.  
  435.        w = int(floor(mod(coord.y, 3.0)));
  436.        z = int(floor(mod(coord.x, 6.0)));
  437.  
  438.        weights = (w == 1) ? slot2_1[z] : (w == 2) ? slot2_2[z] : slot2_3[z];
  439.        return weights;
  440.  
  441.     } else if(phosphor_layout == 15){
  442.        // slot_2_4x4_rgb
  443.        // {red,   yellow, cyan,  blue,  red,   yellow, cyan,  blue },
  444.        // {red,   yellow, cyan,  blue,  black, black,  black, black},
  445.        // {red,   yellow, cyan,  blue,  red,   yellow, cyan,  blue },
  446.        // {black, black,  black, black, red,   yellow, cyan,  blue }
  447.        vec3 slotmask_RBG_x1[8] = vec3[](red,   yellow, cyan,  blue,  red,   yellow, cyan,  blue );
  448.        vec3 slotmask_RBG_x2[8] = vec3[](red,   yellow, cyan,  blue,  black, black,  black, black);
  449.        vec3 slotmask_RBG_x3[8] = vec3[](red,   yellow, cyan,  blue,  red,   yellow, cyan,  blue );
  450.        vec3 slotmask_RBG_x4[8] = vec3[](black, black,  black, black, red,   yellow, cyan,  blue );
  451.  
  452.        // find the vertical index
  453.        w = int(floor(mod(coord.y, 4.0)));
  454.  
  455.        // find the horizontal index
  456.        z = int(floor(mod(coord.x, 8.0)));
  457.  
  458.        weights = (w == 1) ? slotmask_RBG_x1[z] : (w == 2) ? slotmask_RBG_x2[z] : (w == 3) ? slotmask_RBG_x3[z] : slotmask_RBG_x4[z];
  459.        return weights;
  460.  
  461.      } else if(phosphor_layout == 16){
  462.        // slot mask for RBG panels; too low-pitch for 1080p, looks okay at 4K, but wants 8K+
  463.        // {yellow, blue,  black,  black},
  464.        // {yellow, blue,  yellow, blue},
  465.        // {black,  black, yellow, blue}
  466.        vec3 slot2_1[4] = vec3[](yellow, blue,  black,  black);
  467.        vec3 slot2_2[4] = vec3[](yellow, blue,  yellow, blue);
  468.        vec3 slot2_3[4] = vec3[](black,  black, yellow, blue);
  469.  
  470.        w = int(floor(mod(coord.y, 3.0)));
  471.        z = int(floor(mod(coord.x, 4.0)));
  472.  
  473.        weights = (w == 1) ? slot2_1[z] : (w == 2) ? slot2_2[z] : slot2_3[z];
  474.        return weights;
  475.  
  476.     } else if (phosphor_layout == 17) {
  477.        // slot_2_5x4_bgr
  478.        // {red,   magenta, blue,  green, green, red,   magenta, blue,  green, green},
  479.        // {black, blue,    blue,  green, green, red,   red,     black, black, black},
  480.        // {red,   magenta, blue,  green, green, red,   magenta, blue,  green, green},
  481.        // {red,   red,     black, black, black, black, blue,    blue,  green, green}
  482.        vec3 slot_1[10] = vec3[](red,   magenta, blue,  green, green, red,   magenta, blue,  green, green);
  483.        vec3 slot_2[10] = vec3[](black, blue,    blue,  green, green, red,   red,     black, black, black);
  484.        vec3 slot_3[10] = vec3[](red,   magenta, blue,  green, green, red,   magenta, blue,  green, green);
  485.        vec3 slot_4[10] = vec3[](red,   red,     black, black, black, black, blue,    blue,  green, green);
  486.  
  487.        w = int(floor(mod(coord.y, 4.0)));
  488.        z = int(floor(mod(coord.x, 10.0)));
  489.  
  490.        weights = (w == 1) ? slot_1[z] : (w == 2) ? slot_2[z] : (w == 3) ? slot_3[z] : slot_4[z];
  491.        return weights;
  492.  
  493.     } else if (phosphor_layout == 18) {
  494.        // same as above but for RBG panels
  495.        // {red,   yellow, green, blue,  blue,  red,   yellow, green, blue,  blue },
  496.        // {black, green,  green, blue,  blue,  red,   red,    black, black, black},
  497.        // {red,   yellow, green, blue,  blue,  red,   yellow, green, blue,  blue },
  498.        // {red,   red,    black, black, black, black, green,  green, blue,  blue }
  499.        vec3 slot_1[10] = vec3[](red,   yellow, green, blue,  blue,  red,   yellow, green, blue,  blue );
  500.        vec3 slot_2[10] = vec3[](black, green,  green, blue,  blue,  red,   red,    black, black, black);
  501.        vec3 slot_3[10] = vec3[](red,   yellow, green, blue,  blue,  red,   yellow, green, blue,  blue );
  502.        vec3 slot_4[10] = vec3[](red,   red,    black, black, black, black, green,  green, blue,  blue );
  503.  
  504.        w = int(floor(mod(coord.y, 4.0)));
  505.        z = int(floor(mod(coord.x, 10.0)));
  506.  
  507.        weights = (w == 1) ? slot_1[z] : (w == 2) ? slot_2[z] : (w == 3) ? slot_3[z] : slot_4[z];
  508.        return weights;
  509.  
  510.     } else if(phosphor_layout == 19) {
  511.        // slot_3_7x6_rgb
  512.        // {red,   red,   yellow, green, cyan,  blue,  blue,  red,   red,   yellow, green,  cyan,  blue,  blue},
  513.        // {red,   red,   yellow, green, cyan,  blue,  blue,  red,   red,   yellow, green,  cyan,  blue,  blue},
  514.        // {red,   red,   yellow, green, cyan,  blue,  blue,  black, black, black,  black,  black, black, black},
  515.        // {red,   red,   yellow, green, cyan,  blue,  blue,  red,   red,   yellow, green,  cyan,  blue,  blue},
  516.        // {red,   red,   yellow, green, cyan,  blue,  blue,  red,   red,   yellow, green,  cyan,  blue,  blue},
  517.        // {black, black, black,  black, black, black, black, black, red,   red,    yellow, green, cyan,  blue}
  518.  
  519.        vec3 slot_1[14] = vec3[](red,   red,   yellow, green, cyan,  blue,  blue,  red,   red,   yellow, green,  cyan,  blue,  blue);
  520.        vec3 slot_2[14] = vec3[](red,   red,   yellow, green, cyan,  blue,  blue,  red,   red,   yellow, green,  cyan,  blue,  blue);
  521.        vec3 slot_3[14] = vec3[](red,   red,   yellow, green, cyan,  blue,  blue,  black, black, black,  black,  black, black, black);
  522.        vec3 slot_4[14] = vec3[](red,   red,   yellow, green, cyan,  blue,  blue,  red,   red,   yellow, green,  cyan,  blue,  blue);
  523.        vec3 slot_5[14] = vec3[](red,   red,   yellow, green, cyan,  blue,  blue,  red,   red,   yellow, green,  cyan,  blue,  blue);
  524.        vec3 slot_6[14] = vec3[](black, black, black,  black, black, black, black, black, red,   red,    yellow, green, cyan,  blue);
  525.  
  526.        w = int(floor(mod(coord.y, 6.0)));
  527.        z = int(floor(mod(coord.x, 14.0)));
  528.  
  529.        weights = (w == 1) ? slot_1[z] : (w == 2) ? slot_2[z] : (w == 3) ? slot_3[z] : (w == 4) ? slot_4[z] : (w == 5) ? slot_5[z] : slot_6[z];
  530.        return weights;
  531.  
  532.     } else {
  533.         return weights;
  534.     }
  535. }
  536.  
  537. // Horizontal cubic filter.
  538. //
  539. // Some known filters use these values:
  540. //
  541. //   B = 0.0, C = 0.0  =>  Hermite cubic filter.
  542. //   B = 1.0, C = 0.0  =>  Cubic B-Spline filter.
  543. //   B = 0.0, C = 0.5  =>  Catmull-Rom Spline filter. This is the default used in this shader.
  544. //   B = C = 1.0/3.0   =>  Mitchell-Netravali cubic filter.
  545. //   B = 0.3782, C = 0.3109  =>  Robidoux filter.
  546. //   B = 0.2620, C = 0.3690  =>  Robidoux Sharp filter.
  547.  
  548. // Using only Hermite and Catmull-Rom, as the others aren't useful for crt shader.
  549. // For more info, see: http://www.imagemagick.org/Usage/img_diagrams/cubic_survey.gif
  550. mat4x4 get_hfilter_profile()
  551. {
  552.     float bf = 0.0;
  553.     float cf = 0.0;
  554.  
  555.     if (HFILTER_PROFILE == 1) {
  556.         bf = 0.0;
  557.         cf = 0.5;
  558.     }
  559.  
  560.     return mat4(
  561.                    (-bf - 6.0*cf)/6.0,          (3.0*bf + 12.0*cf)/6.0, (-3.0*bf - 6.0*cf)/6.0,             bf/6.0,
  562.          (12.0 - 9.0*bf - 6.0*cf)/6.0, (-18.0 + 12.0*bf  + 6.0*cf)/6.0,                    0.0, (6.0 - 2.0*bf)/6.0,
  563.         -(12.0 - 9.0*bf - 6.0*cf)/6.0,  (18.0 - 15.0*bf - 12.0*cf)/6.0,  (3.0*bf + 6.0*cf)/6.0,             bf/6.0,
  564.                     (bf + 6.0*cf)/6.0,                        -cf,                         0.0,                0.0
  565.     );
  566. }
  567.  
  568. #define scanlines_strength (4.0 * profile.x)
  569. #define beam_min_width     profile.y
  570. #define beam_max_width     profile.z
  571. #define color_boost        profile.w
  572.  
  573. vec4 get_beam_profile()
  574. {
  575.     vec4 bp = vec4(SCANLINES_STRENGTH, BEAM_MIN_WIDTH, BEAM_MAX_WIDTH, COLOR_BOOST);
  576.  
  577.     if (BEAM_PROFILE == 1.0)  bp = vec4(0.62, 1.00, 1.00, 1.40); // Catmull-rom
  578.     if (BEAM_PROFILE == 2.0)  bp = vec4(0.72, 1.00, 1.00, 1.20); // Catmull-rom
  579.  
  580.     return bp;
  581. }
  582.  
  583. void main()
  584. {
  585.     vec4 profile = get_beam_profile();
  586.  
  587.     vec2 TextureSize = mix(
  588.         vec2(rubyTextureSize.x * SHARPNESS_HACK, rubyTextureSize.y),
  589.         vec2(rubyTextureSize.x, rubyTextureSize.y * SHARPNESS_HACK), VSCANLINES
  590.     );
  591.  
  592.     vec2 dx = mix(vec2(1.0/TextureSize.x, 0.0), vec2(0.0, 1.0/TextureSize.y), VSCANLINES);
  593.     vec2 dy = mix(vec2(0.0, 1.0/TextureSize.y), vec2(1.0/TextureSize.x, 0.0), VSCANLINES);
  594.  
  595.     vec2 pix_coord = texCoord.xy * TextureSize + vec2(-0.5, 0.5);
  596.  
  597.     vec2 tc = mix(
  598.         (floor(pix_coord) + vec2(0.5,  0.5)) / TextureSize,
  599.         (floor(pix_coord) + vec2(1.0, -0.5)) / TextureSize,
  600.         VSCANLINES
  601.     );
  602.  
  603.     vec2 fp = mix(fract(pix_coord), fract(pix_coord.yx), VSCANLINES);
  604.  
  605.     vec4 c00 = GAMMA_IN(tex2D(s_p, tc     - dx - dy).xyzw) + BLACK_LEVEL;
  606.     vec4 c01 = GAMMA_IN(tex2D(s_p, tc          - dy).xyzw) + BLACK_LEVEL;
  607.     vec4 c02 = GAMMA_IN(tex2D(s_p, tc     + dx - dy).xyzw) + BLACK_LEVEL;
  608.     vec4 c03 = GAMMA_IN(tex2D(s_p, tc + 2.0*dx - dy).xyzw) + BLACK_LEVEL;
  609.  
  610.     vec4 c10 = GAMMA_IN(tex2D(s_p, tc     - dx).xyzw) + BLACK_LEVEL;
  611.     vec4 c11 = GAMMA_IN(tex2D(s_p, tc         ).xyzw) + BLACK_LEVEL;
  612.     vec4 c12 = GAMMA_IN(tex2D(s_p, tc     + dx).xyzw) + BLACK_LEVEL;
  613.     vec4 c13 = GAMMA_IN(tex2D(s_p, tc + 2.0*dx).xyzw) + BLACK_LEVEL;
  614.  
  615.     mat4 invX = get_hfilter_profile();
  616.  
  617.     mat4 color_matrix0 = mat4(c00, c01, c02, c03);
  618.     mat4 color_matrix1 = mat4(c10, c11, c12, c13);
  619.  
  620.     vec4 invX_Px = vec4(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0) * invX;
  621.     vec4 color0  = color_matrix0 * invX_Px;
  622.     vec4 color1  = color_matrix1 * invX_Px;
  623.  
  624.     //  Get min/max samples
  625.     vec4 min_sample0 = min(c01, c02);
  626.     vec4 max_sample0 = max(c01, c02);
  627.     vec4 min_sample1 = min(c11, c12);
  628.     vec4 max_sample1 = max(c11, c12);
  629.  
  630.     // Anti-ringing
  631.     vec4 aux = color0;
  632.     color0 = clamp(color0, min_sample0, max_sample0);
  633.     color0 = mix(aux, color0, CRT_ANTI_RINGING * step(0.0, (c00-c01)*(c02-c03)));
  634.     aux = color1;
  635.     color1 = clamp(color1, min_sample1, max_sample1);
  636.     color1 = mix(aux, color1, CRT_ANTI_RINGING * step(0.0, (c10-c11)*(c12-c13)));
  637.  
  638.     float pos0 = fp.y;
  639.     float pos1 = 1.0 - fp.y;
  640.  
  641.     vec4 lum0 = mix(vec4(beam_min_width), vec4(beam_max_width), color0);
  642.     vec4 lum1 = mix(vec4(beam_min_width), vec4(beam_max_width), color1);
  643.  
  644.     vec4 d0 = scanlines_strength * pos0 / (lum0 + 0.0000001);
  645.     vec4 d1 = scanlines_strength * pos1 / (lum1 + 0.0000001);
  646.  
  647.     d0 = exp(-d0*d0);
  648.     d1 = exp(-d1*d1);
  649.  
  650.     vec4 color = color_boost * (color0*d0 + color1*d1);
  651.  
  652.     // Mask
  653.     vec2 mask_coords = gl_FragCoord.xy; //texCoord.xy * OutputSize.xy;
  654.     mask_coords = mix(mask_coords.xy, mask_coords.yx, VSCANLINES);
  655.     color.rgb *= mask_weights(mask_coords, MASK_INTENSITY, int(PHOSPHOR_LAYOUT));
  656.  
  657.     // Colour temperature
  658.     if (WP_ADJUST == 1.0) {
  659.         vec3 wp_adjusted = wp_adjust(color.rgb);
  660.         vec3 base_luma = XYZtoYxy(sRGB_to_XYZ(color.rgb));
  661.         vec3 adjusted_luma = XYZtoYxy(sRGB_to_XYZ(wp_adjusted));
  662.         wp_adjusted = (LUMA_PRESERVE == 1.0) ? adjusted_luma + (vec3(base_luma.r,0.,0.) - vec3(adjusted_luma.r,0.,0.)) : adjusted_luma;
  663.         color = vec4(XYZ_to_sRGB(YxytoXYZ(wp_adjusted)), 1.0);
  664.     }
  665.  
  666.     // Output gamma
  667.     color = clamp(GAMMA_OUT(color), 0.0, 1.0);
  668.  
  669.     FragColor = vec4(color.rgb, 1.0);
  670. }
  671. #endif
  672.  
Tags: CRT shader
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement