1#version 450
2
3/*
4   Hyllian's biquad Shader
5
6   Copyright (C) 2011-2015 Hyllian/Jararaca - sergiogdb@gmail.com
7
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU General Public License
10   as published by the Free Software Foundation; either version 2
11   of the License, or (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
22*/
23
24layout(push_constant) uniform Push
25{
26	vec4 SourceSize;
27	vec4 OriginalSize;
28	vec4 OutputSize;
29	uint FrameCount;
30	float K;
31} params;
32
33
34#pragma parameter K "Blurring Param" 0.8 0.0 1.0 0.01
35
36#define K params.K
37
38layout(std140, set = 0, binding = 0) uniform UBO
39{
40	mat4 MVP;
41} global;
42
43#define saturate(c) clamp(c, 0.0, 1.0)
44#define lerp(c) mix(c)
45#define mul(a,b) (b*a)
46#define fmod(c) mod(c)
47#define frac(c) fract(c)
48#define tex2D(c,d) texture(c,d)
49#define float2 vec2
50#define float3 vec3
51#define float4 vec4
52#define int2 ivec2
53#define int3 ivec3
54#define int4 ivec4
55#define bool2 bvec2
56#define bool3 bvec3
57#define bool4 bvec4
58#define float2x2 mat2x2
59#define float3x3 mat3x3
60#define float4x4 mat4x4
61#define s_p Source
62
63// Calculates the distance between two points
64float d(float2 pt1, float2 pt2)
65{
66  float2 v = pt2 - pt1;
67  return sqrt(dot(v,v));
68}
69
70float3 resampler(float3 x)
71    {
72      float3 res;
73
74      res = lessThanEqual(x,float3(0.5, 0.5, 0.5)) == bvec3(true) ?  (-2*K*x*x + 0.5*(K+1))  :  (lessThanEqual(x,float3(1.5, 1.5, 1.5)) == bvec3(true)  ?  (K*x*x + (-2*K - 0.5)*x + 0.75*(K+1))  :  float3(0.00001, 0.00001, 0.00001));
75
76      return res;
77    }
78
79#pragma stage vertex
80layout(location = 0) in vec4 Position;
81layout(location = 1) in vec2 TexCoord;
82layout(location = 0) out vec2 vTexCoord;
83
84void main()
85{
86   gl_Position = global.MVP * Position;
87   vTexCoord = TexCoord;
88}
89
90#pragma stage fragment
91layout(location = 0) in vec2 vTexCoord;
92layout(location = 0) out vec4 FragColor;
93layout(set = 0, binding = 2) uniform sampler2D Source;
94
95void main()
96{
97      float3 color;
98      float3x3 weights;
99
100      float2 dx = float2(1.0, 0.0);
101      float2 dy = float2(0.0, 1.0);
102
103      float2 pc = vTexCoord*params.SourceSize.xy;
104
105      float2 tc = (floor(pc)+float2(0.5,0.5));
106
107      weights[0] = resampler(float3(d(pc, tc    -dx    -dy), d(pc, tc           -dy), d(pc, tc    +dx    -dy)));
108      weights[1] = resampler(float3(d(pc, tc    -dx       ), d(pc, tc              ), d(pc, tc    +dx       )));
109      weights[2] = resampler(float3(d(pc, tc    -dx    +dy), d(pc, tc           +dy), d(pc, tc    +dx    +dy)));
110
111      dx = dx * params.SourceSize.zw;
112      dy = dy * params.SourceSize.zw;
113      tc = tc * params.SourceSize.zw;
114
115     // reading the texels
116      float3 c00 = tex2D(s_p, tc    -dx    -dy).xyz;
117      float3 c10 = tex2D(s_p, tc           -dy).xyz;
118      float3 c20 = tex2D(s_p, tc    +dx    -dy).xyz;
119      float3 c01 = tex2D(s_p, tc    -dx       ).xyz;
120      float3 c11 = tex2D(s_p, tc              ).xyz;
121      float3 c21 = tex2D(s_p, tc    +dx       ).xyz;
122      float3 c02 = tex2D(s_p, tc    -dx    +dy).xyz;
123      float3 c12 = tex2D(s_p, tc           +dy).xyz;
124      float3 c22 = tex2D(s_p, tc    +dx    +dy).xyz;
125
126      color = mul(weights[0], float3x3(c00, c10, c20));
127      color+= mul(weights[1], float3x3(c01, c11, c21));
128      color+= mul(weights[2], float3x3(c02, c12, c22));
129      color = color/(dot(mul(weights, float3(1.0)), float3(1.0)));
130
131      // final sum and weight normalization
132   FragColor = vec4(color, 1.0);
133}