]> git.sesse.net Git - movit/blob - dither_effect.cpp
Loosen up a boundary in DitherEffectTest by 10%.
[movit] / dither_effect.cpp
1 #include <math.h>
2 #include <assert.h>
3 #include <GL/glew.h>
4
5 #include "dither_effect.h"
6 #include "util.h"
7
8 namespace {
9
10 // A simple LCG (linear congruental generator) random generator.
11 // We implement our own so we can be deterministic from frame to frame
12 // and run to run; we don't have special needs for speed or quality,
13 // as long as the period is reasonably long. The output is in range
14 // [0, 2^31>.
15 //
16 // This comes from http://en.wikipedia.org/wiki/Linear_congruential_generator.
17 unsigned lcg_rand(unsigned x)
18 {
19         return (x * 1103515245U + 12345U) & ((1U << 31) - 1);
20
21
22 }  // namespace
23
24 DitherEffect::DitherEffect()
25         : width(1280), height(720), num_bits(8),
26           last_width(-1), last_height(-1), last_num_bits(-1)
27 {
28         register_int("output_width", &width);
29         register_int("output_height", &height);
30         register_int("num_bits", &num_bits);
31
32         glGenTextures(1, &texnum);
33 }
34
35 DitherEffect::~DitherEffect()
36 {
37         glDeleteTextures(1, &texnum);
38 }
39
40 std::string DitherEffect::output_fragment_shader()
41 {
42         return read_file("dither_effect.frag");
43 }
44
45 void DitherEffect::update_texture(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
46 {
47         float *dither_noise = new float[width * height];
48         float dither_double_amplitude = 1.0f / (1 << num_bits);
49
50         // We don't need a strictly nonrepeating dither; reducing the resolution
51         // to max 128x128 saves a lot of texture bandwidth, without causing any
52         // noticeable harm to the dither's performance.
53         texture_width = std::min(width, 128);
54         texture_height = std::min(height, 128);
55
56         // Using the resolution as a seed gives us a consistent dither from frame to frame.
57         // It also gives a different dither for e.g. different aspect ratios, which _feels_
58         // good, but probably shouldn't matter.
59         unsigned seed = (width << 16) ^ height;
60         for (int i = 0; i < texture_width * texture_height; ++i) {
61                 seed = lcg_rand(seed);
62                 float normalized_rand = seed * (1.0f / (1U << 31)) - 0.5;  // [-0.5, 0.5>
63                 dither_noise[i] = dither_double_amplitude * normalized_rand;
64         }
65
66         glActiveTexture(GL_TEXTURE0 + *sampler_num);
67         check_error();
68         glBindTexture(GL_TEXTURE_2D, texnum);
69         check_error();
70         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
71         check_error();
72         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
73         check_error();
74         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
75         check_error();
76         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
77         check_error();
78         glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16F_ARB, texture_width, texture_height, 0, GL_LUMINANCE, GL_FLOAT, dither_noise);
79         check_error();
80
81         delete[] dither_noise;
82 }
83
84 void DitherEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
85 {
86         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
87
88         assert(width > 0);
89         assert(height > 0);
90         assert(num_bits > 0);
91
92         if (width != last_width || height != last_height || num_bits != last_num_bits) {
93                 update_texture(glsl_program_num, prefix, sampler_num);
94                 last_width = width;
95                 last_height = height;
96                 last_num_bits = num_bits;
97         }
98
99         glActiveTexture(GL_TEXTURE0 + *sampler_num);
100         check_error();
101         glBindTexture(GL_TEXTURE_2D, texnum);
102         check_error();
103
104         set_uniform_int(glsl_program_num, prefix, "dither_tex", *sampler_num);
105         ++sampler_num;
106
107         // In theory, we should adjust for the texel centers that have moved here as well,
108         // but since we use GL_NEAREST and we don't really care a lot what texel we sample,
109         // we don't have to worry about it.     
110         float tc_scale[] = { float(width) / float(texture_width), float(height) / float(texture_height) };
111         set_uniform_vec2(glsl_program_num, prefix, "tc_scale", tc_scale);
112 }