Apply filter settings from bsnes v083 shaders.
[bsnes:xml-shaders.git] / shaders / OpenGL / v1.0 / Pixellate.shader
1 <?xml version="1.0" encoding="UTF-8"?>
2 <shader language="GLSL">
3     <vertex><![CDATA[
4         void main()
5         {
6                 gl_Position    = ftransform();
7                 gl_TexCoord[0] = gl_MultiTexCoord0;
8         }
9     ]]></vertex>
10
11     <fragment filter="nearest"><![CDATA[
12         uniform sampler2D rubyTexture;
13         uniform vec2      rubyTextureSize;
14
15         #define round(x) floor( (x) + 0.5 )
16
17         void main()
18         {
19                 vec2 texelSize = 1.0 / rubyTextureSize;
20
21                 vec2 range;
22                 range.x = dFdx(gl_TexCoord[0].x) / 2.0 * 0.99;
23                 range.y = dFdy(gl_TexCoord[0].y) / 2.0 * 0.99;
24
25                 float left   = gl_TexCoord[0].x - range.x;
26                 float top    = gl_TexCoord[0].y + range.y;
27                 float right  = gl_TexCoord[0].x + range.x;
28                 float bottom = gl_TexCoord[0].y - range.y;
29
30                 vec4 topLeftColor     = texture2D(rubyTexture, (floor(vec2(left, top)     / texelSize) + 0.5) * texelSize);
31                 vec4 bottomRightColor = texture2D(rubyTexture, (floor(vec2(right, bottom) / texelSize) + 0.5) * texelSize);
32                 vec4 bottomLeftColor  = texture2D(rubyTexture, (floor(vec2(left, bottom)  / texelSize) + 0.5) * texelSize);
33                 vec4 topRightColor    = texture2D(rubyTexture, (floor(vec2(right, top)    / texelSize) + 0.5) * texelSize);
34
35                 vec2 border = clamp(
36                         round(gl_TexCoord[0].st / texelSize) * texelSize,
37                         vec2(left, bottom),
38                         vec2(right, top)
39                     );
40
41                 float totalArea = 4.0 * range.x * range.y;
42
43                 vec4 averageColor;
44                 averageColor  = ((border.x - left)  * (top - border.y)    / totalArea) * topLeftColor;
45                 averageColor += ((right - border.x) * (border.y - bottom) / totalArea) * bottomRightColor;
46                 averageColor += ((border.x - left)  * (border.y - bottom) / totalArea) * bottomLeftColor;
47                 averageColor += ((right - border.x) * (top - border.y)    / totalArea) * topRightColor;
48
49                 gl_FragColor = averageColor;
50         }
51     ]]></fragment>
52 </shader>