File: MipMapDownsamplingShader.frag.txt

package info (click to toggle)
psychtoolbox-3 3.0.18.12.dfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,576 kB
  • sloc: ansic: 173,181; cpp: 20,885; objc: 5,148; sh: 2,752; python: 1,366; php: 384; makefile: 193; java: 113
file content (55 lines) | stat: -rw-r--r-- 2,188 bytes parent folder | download | duplicates (5)
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
/* MipMapDownsamplingShader.frag.txt
 *
 * Example shader for downsampling during building a OpenGL Mipmap image
 * resolution pyramid. This shader can be passed to CreateResolutionPyramid().
 *
 * This shader computes each "downfiltered" output sample from a 3-by-3 grid
 * of neighbouring input samples, weighted by a gaussian filter kernel of
 * standard deviation 1.0.
 *
 * This is a proof-of-concept shader. It demonstrates the principle, but is
 * not necessarilly perfect.
 *
 * (c) 2012 by Mario Kleiner. Licensed under MIT license.
 */

const vec3 ColorToGrayWeights = vec3(0.3, 0.59, 0.11); 
uniform sampler2D Image;
uniform vec2 srcSize;
uniform vec2 dstSize;

const mat3 kernel = mat3( 0.075113607954111,   0.123841403152974,   0.075113607954111, 0.123841403152974,   0.204179955571658,   0.123841403152974, 0.075113607954111,   0.123841403152974,   0.075113607954111);

void main()
{
    vec2 inpos = gl_TexCoord[0].st;
    float dx = 1.0 / srcSize.x;
    float dy = 1.0 / srcSize.y;

    /* Take 9 weighted samples in a 3x3 grid, to emulate sampling with a gaussian kernel: */
    vec4 incolor = vec4(0.0);
    incolor += texture2D(Image, inpos + vec2(-dx, -dy)) * kernel[0][0];
    incolor += texture2D(Image, inpos + vec2(0.0, -dy)) * kernel[1][0];
    incolor += texture2D(Image, inpos + vec2(+dx, -dy)) * kernel[2][0];

    incolor += texture2D(Image, inpos + vec2(-dx, 0.0)) * kernel[0][1];
    incolor += texture2D(Image, inpos + vec2(0.0, 0.0)) * kernel[1][1];
    incolor += texture2D(Image, inpos + vec2(+dx, 0.0)) * kernel[2][1];

    incolor += texture2D(Image, inpos + vec2(-dx, +dy)) * kernel[0][2];
    incolor += texture2D(Image, inpos + vec2(0.0, +dy)) * kernel[1][2];
    incolor += texture2D(Image, inpos + vec2(+dx, +dy)) * kernel[2][2];

    /* Optionally convert final RGB sample to grayscale: */
    if (true) {
        float luminance = dot(incolor.rgb, ColorToGrayWeights);
        gl_FragColor.rgb = vec3(luminance);

        /* Pass alpha unmodified: */
        gl_FragColor.a = incolor.a;
    }
    else {
        /* Simply pass sample unmodified: */
        gl_FragColor = incolor;
    }
}