X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=dither_effect.cpp;h=3fa6aebc3fa8f674674ca37e37ac6932be78c6fe;hp=cdfb157e675900a08ed3a859889641e01df7ba4a;hb=34776d3ed2565ee834405e575bf3bfc7f7933e36;hpb=ff9e68a3f5abb179bd7bf9fb84df48327f148583 diff --git a/dither_effect.cpp b/dither_effect.cpp index cdfb157..3fa6aeb 100644 --- a/dither_effect.cpp +++ b/dither_effect.cpp @@ -1,9 +1,16 @@ -#include +#include #include +#include +#include #include "dither_effect.h" +#include "effect_util.h" +#include "init.h" #include "util.h" -#include "opengl.h" + +using namespace std; + +namespace movit { namespace { @@ -28,6 +35,10 @@ DitherEffect::DitherEffect() register_int("output_width", &width); register_int("output_height", &height); register_int("num_bits", &num_bits); + register_uniform_float("round_fac", &uniform_round_fac); + register_uniform_float("inv_round_fac", &uniform_inv_round_fac); + register_uniform_vec2("tc_scale", uniform_tc_scale); + register_uniform_sampler2d("dither_tex", &uniform_dither_tex); glGenTextures(1, &texnum); } @@ -37,21 +48,29 @@ DitherEffect::~DitherEffect() glDeleteTextures(1, &texnum); } -std::string DitherEffect::output_fragment_shader() +string DitherEffect::output_fragment_shader() { - return read_file("dither_effect.frag"); + char buf[256]; + sprintf(buf, "#define NEED_EXPLICIT_ROUND %d\n", (movit_num_wrongly_rounded > 0)); + return buf + read_file("dither_effect.frag"); } -void DitherEffect::update_texture(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) +void DitherEffect::update_texture(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num) { float *dither_noise = new float[width * height]; float dither_double_amplitude = 1.0f / (1 << num_bits); + // We don't need a strictly nonrepeating dither; reducing the resolution + // to max 128x128 saves a lot of texture bandwidth, without causing any + // noticeable harm to the dither's performance. + texture_width = min(width, 128); + texture_height = min(height, 128); + // Using the resolution as a seed gives us a consistent dither from frame to frame. // It also gives a different dither for e.g. different aspect ratios, which _feels_ // good, but probably shouldn't matter. unsigned seed = (width << 16) ^ height; - for (int i = 0; i < width * height; ++i) { + for (int i = 0; i < texture_width * texture_height; ++i) { seed = lcg_rand(seed); float normalized_rand = seed * (1.0f / (1U << 31)) - 0.5; // [-0.5, 0.5> dither_noise[i] = dither_double_amplitude * normalized_rand; @@ -63,20 +82,26 @@ void DitherEffect::update_texture(GLuint glsl_program_num, const std::string &pr check_error(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); check_error(); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + check_error(); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); check_error(); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); check_error(); - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16F_ARB, width, height, 0, GL_LUMINANCE, GL_FLOAT, dither_noise); + glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, texture_width, texture_height, 0, GL_RED, GL_FLOAT, dither_noise); check_error(); delete[] dither_noise; } -void DitherEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) +void DitherEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num) { Effect::set_gl_state(glsl_program_num, prefix, sampler_num); + assert(width > 0); + assert(height > 0); + assert(num_bits > 0); + if (width != last_width || height != last_height || num_bits != last_num_bits) { update_texture(glsl_program_num, prefix, sampler_num); last_width = width; @@ -89,6 +114,19 @@ void DitherEffect::set_gl_state(GLuint glsl_program_num, const std::string &pref glBindTexture(GL_TEXTURE_2D, texnum); check_error(); - set_uniform_int(glsl_program_num, prefix, "dither_tex", *sampler_num); - ++sampler_num; + uniform_dither_tex = *sampler_num; + ++*sampler_num; + + // In theory, we should adjust for the texel centers that have moved here as well, + // but since we use GL_NEAREST and we don't really care a lot what texel we sample, + // we don't have to worry about it. + uniform_tc_scale[0] = float(width) / float(texture_width); + uniform_tc_scale[1] = float(height) / float(texture_height); + + // Used if the shader needs to do explicit rounding. + int round_fac = (1 << num_bits) - 1; + uniform_round_fac = round_fac; + uniform_inv_round_fac = 1.0f / round_fac; } + +} // namespace movit