ProjectArcade
54 строки · 1.6 Кб
1// Bicubic (Catmull-Rom) upscaling shader.
2
3#ifdef GL_ES
4precision mediump float;
5precision mediump int;
6#endif
7
8uniform sampler2D sampler0;
9varying vec2 v_position;
10
11uniform vec2 u_texelDelta;
12uniform vec2 u_pixelDelta;
13
14const vec2 HALF_PIXEL = vec2(0.5, 0.5);
15
16vec3 rgb(int inputX, int inputY)
17{
18return texture2D(sampler0, (vec2(inputX, inputY) + HALF_PIXEL) * u_texelDelta).xyz;
19}
20
21// Catmull-Rom coefficients, multiplied by 2.
22float A(float x) {return x*((2.0-x)*x-1.0);}
23float B(float x) {return x*x*(3.0*x-5.0)+2.0;}
24float C(float x) {return x*((4.0-3.0*x)*x+1.0);}
25float D(float x) {return x*x*(x-1.0);}
26
27vec3 interpolateHorizontally(vec2 inputPos, ivec2 inputPosFloor, int dy) {
28vec3 ret = vec3(0.0);
29float x = inputPos.x - float(inputPosFloor.x);
30
31ret += A(x) * rgb(inputPosFloor.x - 1, inputPosFloor.y + dy);
32ret += B(x) * rgb(inputPosFloor.x , inputPosFloor.y + dy);
33ret += C(x) * rgb(inputPosFloor.x + 1, inputPosFloor.y + dy);
34ret += D(x) * rgb(inputPosFloor.x + 2, inputPosFloor.y + dy);
35return ret;
36}
37
38vec4 process(vec2 outputPos) {
39vec2 inputPos = outputPos / u_texelDelta - HALF_PIXEL;
40ivec2 inputPosFloor = ivec2(inputPos);
41
42vec3 ret = vec3(0.0);
43float y = inputPos.y - float(inputPosFloor.y);
44ret += A(y) * interpolateHorizontally(inputPos, inputPosFloor, -1);
45ret += B(y) * interpolateHorizontally(inputPos, inputPosFloor, 0);
46ret += C(y) * interpolateHorizontally(inputPos, inputPosFloor, +1);
47ret += D(y) * interpolateHorizontally(inputPos, inputPosFloor, +2);
48return vec4(0.25 * ret, 1.0);
49}
50
51void main()
52{
53gl_FragColor.rgba = process(v_position);
54}
55