X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=dither_effect.cpp;h=14132bd43407bda72a7d2a8ca86f785bc9e1fab1;hp=1399c8196984c728ac148ea686f4ab9de504e403;hb=9651a4eaae012cdc49c1aa38197861e04f62e91e;hpb=29072985d0a00a53e5b578a1444cee61a0c9e1f2 diff --git a/dither_effect.cpp b/dither_effect.cpp index 1399c81..14132bd 100644 --- a/dither_effect.cpp +++ b/dither_effect.cpp @@ -1,10 +1,15 @@ -#include -#include #include +#include +#include +#include #include "dither_effect.h" +#include "effect_util.h" +#include "init.h" #include "util.h" +using namespace std; + namespace { // A simple LCG (linear congruental generator) random generator. @@ -37,12 +42,14 @@ 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); @@ -50,8 +57,8 @@ void DitherEffect::update_texture(GLuint glsl_program_num, const std::string &pr // 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 = std::min(width, 128); - texture_height = std::min(height, 128); + 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_ @@ -81,10 +88,14 @@ void DitherEffect::update_texture(GLuint glsl_program_num, const std::string &pr 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; @@ -105,4 +116,9 @@ void DitherEffect::set_gl_state(GLuint glsl_program_num, const std::string &pref // we don't have to worry about it. float tc_scale[] = { float(width) / float(texture_width), float(height) / float(texture_height) }; set_uniform_vec2(glsl_program_num, prefix, "tc_scale", tc_scale); + + // Used if the shader needs to do explicit rounding. + int round_fac = (1 << num_bits) - 1; + set_uniform_float(glsl_program_num, prefix, "round_fac", round_fac); + set_uniform_float(glsl_program_num, prefix, "inv_round_fac", 1.0f / round_fac); }