6 #include "dither_effect.h"
7 #include "effect_util.h"
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
23 // This comes from http://en.wikipedia.org/wiki/Linear_congruential_generator.
24 unsigned lcg_rand(unsigned x)
26 return (x * 1103515245U + 12345U) & ((1U << 31) - 1);
31 DitherEffect::DitherEffect()
32 : width(1280), height(720), num_bits(8),
33 last_width(-1), last_height(-1), last_num_bits(-1)
35 register_int("output_width", &width);
36 register_int("output_height", &height);
37 register_int("num_bits", &num_bits);
39 glGenTextures(1, &texnum);
42 DitherEffect::~DitherEffect()
44 glDeleteTextures(1, &texnum);
47 string DitherEffect::output_fragment_shader()
50 sprintf(buf, "#define NEED_EXPLICIT_ROUND %d\n", (movit_num_wrongly_rounded > 0 && movit_shader_rounding_supported));
51 return buf + read_file("dither_effect.frag");
54 void DitherEffect::update_texture(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
56 float *dither_noise = new float[width * height];
57 float dither_double_amplitude = 1.0f / (1 << num_bits);
59 // We don't need a strictly nonrepeating dither; reducing the resolution
60 // to max 128x128 saves a lot of texture bandwidth, without causing any
61 // noticeable harm to the dither's performance.
62 texture_width = min(width, 128);
63 texture_height = min(height, 128);
65 // Using the resolution as a seed gives us a consistent dither from frame to frame.
66 // It also gives a different dither for e.g. different aspect ratios, which _feels_
67 // good, but probably shouldn't matter.
68 unsigned seed = (width << 16) ^ height;
69 for (int i = 0; i < texture_width * texture_height; ++i) {
70 seed = lcg_rand(seed);
71 float normalized_rand = seed * (1.0f / (1U << 31)) - 0.5; // [-0.5, 0.5>
72 dither_noise[i] = dither_double_amplitude * normalized_rand;
75 glActiveTexture(GL_TEXTURE0 + *sampler_num);
77 glBindTexture(GL_TEXTURE_2D, texnum);
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
87 glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, texture_width, texture_height, 0, GL_RED, GL_FLOAT, dither_noise);
90 delete[] dither_noise;
93 void DitherEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
95 Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
101 if (width != last_width || height != last_height || num_bits != last_num_bits) {
102 update_texture(glsl_program_num, prefix, sampler_num);
104 last_height = height;
105 last_num_bits = num_bits;
108 glActiveTexture(GL_TEXTURE0 + *sampler_num);
110 glBindTexture(GL_TEXTURE_2D, texnum);
113 set_uniform_int(glsl_program_num, prefix, "dither_tex", *sampler_num);
116 // In theory, we should adjust for the texel centers that have moved here as well,
117 // but since we use GL_NEAREST and we don't really care a lot what texel we sample,
118 // we don't have to worry about it.
119 float tc_scale[] = { float(width) / float(texture_width), float(height) / float(texture_height) };
120 set_uniform_vec2(glsl_program_num, prefix, "tc_scale", tc_scale);
122 // Used if the shader needs to do explicit rounding.
123 int round_fac = (1 << num_bits) - 1;
124 set_uniform_float(glsl_program_num, prefix, "round_fac", round_fac);
125 set_uniform_float(glsl_program_num, prefix, "inv_round_fac", 1.0f / round_fac);