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