summaryrefslogtreecommitdiff
path: root/render/fx_renderer/gles2/shaders/tex.frag
blob: 1377c00c6a9f6c59f7651f7a6e65970dfce7d0cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#define SOURCE_TEXTURE_RGBA 1
#define SOURCE_TEXTURE_RGBX 2
#define SOURCE_TEXTURE_EXTERNAL 3

#if !defined(SOURCE)
#error "Missing shader preamble"
#endif

#if SOURCE == SOURCE_TEXTURE_EXTERNAL
#extension GL_OES_EGL_image_external : require
#endif

#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif

varying vec2 v_texcoord;

#if SOURCE == SOURCE_TEXTURE_EXTERNAL
uniform samplerExternalOES tex;
#elif SOURCE == SOURCE_TEXTURE_RGBA || SOURCE == SOURCE_TEXTURE_RGBX
uniform sampler2D tex;
#endif

uniform float alpha;

uniform vec2 half_size;
uniform vec2 position;
uniform float radius;
uniform bool has_titlebar;
uniform bool discard_transparent;
uniform float dim;
uniform vec4 dim_color;

vec4 sample_texture() {
#if SOURCE == SOURCE_TEXTURE_RGBA || SOURCE == SOURCE_TEXTURE_EXTERNAL
	return texture2D(tex, v_texcoord);
#elif SOURCE == SOURCE_TEXTURE_RGBX
	return vec4(texture2D(tex, v_texcoord).rgb, 1.0);
#endif
}

float roundRectSDF() {
	vec2 q = abs(gl_FragCoord.xy - position - half_size) - half_size + radius;
	return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - radius;
}

void main() {
	gl_FragColor = mix(sample_texture(), dim_color, dim) * alpha;

	if (discard_transparent && gl_FragColor.a == 0.0) {
		discard;
		return;
	}

	if (!has_titlebar || gl_FragCoord.y - position.y > radius) {
		float alpha = smoothstep(-1.0, 1.0, roundRectSDF());
		gl_FragColor = mix(gl_FragColor, vec4(0.0), alpha);
	}
}