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