/* * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include #include "tvgGlShaderSrc.h" #define TVG_COMPOSE_SHADER(shader) #shader const char* COLOR_VERT_SHADER = TVG_COMPOSE_SHADER( layout(location = 0) in vec3 aLocation; \n layout(std140) uniform Matrix { \n mat4 transform; \n } uMatrix; \n out float vOpacity; \n void main() \n { \n gl_Position = \n uMatrix.transform * vec4(aLocation.xy, 0.0, 1.0); \n vOpacity = aLocation.z; \n }); const char* COLOR_FRAG_SHADER = TVG_COMPOSE_SHADER( layout(std140) uniform ColorInfo { \n vec4 solidColor; \n } uColorInfo; \n in float vOpacity; \n out vec4 FragColor; \n void main() \n { \n vec4 uColor = uColorInfo.solidColor; \n FragColor = vec4(uColor.xyz, uColor.w*vOpacity); \n }); const char* GRADIENT_VERT_SHADER = TVG_COMPOSE_SHADER( layout(location = 0) in vec3 aLocation; \n out float vOpacity; \n out vec2 vPos; \n layout(std140) uniform Matrix { \n mat4 transform; \n } uMatrix; \n \n void main() \n { \n gl_Position = uMatrix.transform * vec4(aLocation.xy, 0.0, 1.0); \n vOpacity = aLocation.z; \n vPos = aLocation.xy; \n }); std::string STR_GRADIENT_FRAG_COMMON_VARIABLES = TVG_COMPOSE_SHADER( const int MAX_STOP_COUNT = 16; \n in vec2 vPos; \n in float vOpacity; \n ); std::string STR_GRADIENT_FRAG_COMMON_FUNCTIONS = TVG_COMPOSE_SHADER( float gradientStep(float edge0, float edge1, float x) \n { \n // linear \n x = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0); \n return x; \n } \n \n float gradientStop(int index) \n { \n if (index >= MAX_STOP_COUNT) index = MAX_STOP_COUNT - 1; \n int i = index / 4; \n int j = index % 4; \n return uGradientInfo.stopPoints[i][j]; \n } \n \n float gradientWrap(float d) \n { \n int i = 1; \n while (d > 1.0) { \n d = d - 1.0; \n i *= -1; \n } \n \n if (i == 1) \n return smoothstep(0.0, 1.0, d); \n else \n return smoothstep(1.0, 0.0, d); \n } \n \n vec4 gradient(float t) \n { \n vec4 col = vec4(0.0); \n int i = 0; \n int count = int(uGradientInfo.nStops[0]); \n if (t <= gradientStop(0)) \n { \n col += uGradientInfo.stopColors[0]; \n } \n else if (t >= gradientStop(count - 1)) \n { \n col += uGradientInfo.stopColors[count - 1]; \n } \n else \n { \n for (i = 0; i < count - 1; ++i) \n { \n float stopi = gradientStop(i); \n float stopi1 = gradientStop(i + 1); \n if (t > stopi && t 0.0) { \n FragColor = vec4(srcColor.rgb, srcColor.a * da * vOpacity); \n } else { \n FragColor = vec4(maskColor.rgb, maskColor.a * (-da) * vOpacity);\n } \n } \n );