Constant alacritty::renderer::rects::RECT_SHADER_F
source · const RECT_SHADER_F: &str = "#if defined(GLES2_RENDERER)\n#define float_t mediump float\n#define color_t mediump vec4\n#define FRAG_COLOR gl_FragColor\n\nvarying color_t color;\n\n#else\n#define float_t float\n#define color_t vec4\n\nout vec4 FragColor;\n#define FRAG_COLOR FragColor\n\nflat in color_t color;\n\n#endif\n\nuniform float_t cellWidth;\nuniform float_t cellHeight;\nuniform float_t paddingY;\nuniform float_t paddingX;\n\nuniform float_t underlinePosition;\nuniform float_t underlineThickness;\n\nuniform float_t undercurlPosition;\n\n#define PI 3.1415926538\n\n#if defined(DRAW_UNDERCURL)\ncolor_t draw_undercurl(float_t x, float_t y) {\n // We use `undercurlPosition` as an amplitude, since it\'s half of the descent\n // value.\n //\n // The `x` represents the left bound of pixel we should add `1/2` to it, so\n // we compute the undercurl position for the center of the pixel.\n float_t undercurl = undercurlPosition / 2. * cos((x + 0.5) * 2.\n * PI / cellWidth) + undercurlPosition - 1.;\n\n float_t undercurlTop = undercurl + max((underlineThickness - 1.), 0.) / 2.;\n float_t undercurlBottom = undercurl - max((underlineThickness - 1.), 0.) / 2.;\n\n // The distance to the curve boundary is always positive when it should\n // be used for AA. When both `y - undercurlTop` and `undercurlBottom - y`\n // expressions are negative, it means that the point is inside the curve\n // and we should just use alpha 1. To do so, we max one value with 0\n // so it\'ll use the alpha 1 in the end.\n float_t dst = max(y - undercurlTop, max(undercurlBottom - y, 0.));\n\n // Doing proper SDF is complicated for this shader, so just make AA\n // stronger by 1/x^2, which renders preserving underline thickness and\n // being bold enough.\n float_t alpha = 1. - dst * dst;\n\n // The result is an alpha mask on a rect, which leaves only curve opaque.\n return vec4(color.rgb, alpha);\n}\n#endif\n\n#if defined(DRAW_DOTTED)\n// When the dot size increases we can use AA to make spacing look even and the\n// dots rounded.\ncolor_t draw_dotted_aliased(float_t x, float_t y) {\n float_t dotNumber = floor(x / underlineThickness);\n\n float_t radius = underlineThickness / 2.;\n float_t centerY = underlinePosition - 1.;\n\n float_t leftCenter = (dotNumber - mod(dotNumber, 2.)) * underlineThickness + radius;\n float_t rightCenter = leftCenter + 2. * underlineThickness;\n\n float_t distanceLeft = sqrt(pow(x - leftCenter, 2.) + pow(y - centerY, 2.));\n float_t distanceRight = sqrt(pow(x - rightCenter, 2.) + pow(y - centerY, 2.));\n\n float_t alpha = max(1. - (min(distanceLeft, distanceRight) - radius), 0.);\n return vec4(color.rgb, alpha);\n}\n\n/// Draw dotted line when dot is just a single pixel.\ncolor_t draw_dotted(float_t x, float_t y) {\n float_t cellEven = 0.;\n\n // Since the size of the dot and its gap combined is 2px we should ensure that\n // spacing will be even. If the cellWidth is even it\'ll work since we start\n // with dot and end with gap. However if cellWidth is odd, the cell will start\n // and end with a dot, creating a dash. To resolve this issue, we invert the\n // pattern every two cells.\n if (int(mod(cellWidth, 2.)) != 0) {\n cellEven = mod((gl_FragCoord.x - paddingX) / cellWidth, 2.);\n }\n\n // Since we use the entire descent area for dotted underlines, we limit its\n // height to a single pixel so we don\'t draw bars instead of dots.\n float_t alpha = 1. - abs(floor(underlinePosition) - y);\n if (int(mod(x, 2.)) != int(cellEven)) {\n alpha = 0.;\n }\n\n return vec4(color.rgb, alpha);\n}\n#endif\n\n#if defined(DRAW_DASHED)\ncolor_t draw_dashed(float_t x) {\n // Since dashes of adjacent cells connect with each other our dash length is\n // half of the desired total length.\n float_t halfDashLen = floor(cellWidth / 4. + 0.5);\n\n float_t alpha = 1.;\n\n // Check if `x` coordinate is where we should draw gap.\n if (x > halfDashLen - 1. && x < cellWidth - halfDashLen) {\n alpha = 0.;\n }\n\n return vec4(color.rgb, alpha);\n}\n#endif\n\nvoid main() {\n float_t x = floor(mod(gl_FragCoord.x - paddingX, cellWidth));\n float_t y = floor(mod(gl_FragCoord.y - paddingY, cellHeight));\n\n#if defined(DRAW_UNDERCURL)\n FRAG_COLOR = draw_undercurl(x, y);\n#elif defined(DRAW_DOTTED)\n if (underlineThickness < 2.) {\n FRAG_COLOR = draw_dotted(x, y);\n } else {\n FRAG_COLOR = draw_dotted_aliased(x, y);\n }\n#elif defined(DRAW_DASHED)\n FRAG_COLOR = draw_dashed(x);\n#else\n FRAG_COLOR = color;\n#endif\n}\n";
Expand description
Shader sources for rect rendering program.