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