]> git.sesse.net Git - movit/commitdiff
Add (safe) asserts around all Effect::set_* return values, and add warnings for not...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 28 Oct 2012 18:58:23 +0000 (19:58 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 28 Oct 2012 19:32:33 +0000 (20:32 +0100)
blur_effect.cpp
demo.cpp
effect.h
effect_chain.cpp
glow_effect.cpp
resample_effect.cpp
unsharp_mask_effect.cpp
util.h

index ec61aeab784f8a9f8b3574a29e48141162b6b2f7..80ccb3f2bee02769b90b487975b36ca83248ded6 100644 (file)
@@ -16,9 +16,9 @@ BlurEffect::BlurEffect()
 {
        // The first blur pass will forward resolution information to us.
        hpass = new SingleBlurPassEffect(this);
-       hpass->set_int("direction", SingleBlurPassEffect::HORIZONTAL);
+       CHECK(hpass->set_int("direction", SingleBlurPassEffect::HORIZONTAL));
        vpass = new SingleBlurPassEffect(NULL);
-       vpass->set_int("direction", SingleBlurPassEffect::VERTICAL);
+       CHECK(vpass->set_int("direction", SingleBlurPassEffect::VERTICAL));
 
        update_radius();
 }
index 2243f2dd702b90419ef491963636c6508c76b669..5bcfa4335a2670f1784878246bdc7f8460dd81c2 100644 (file)
--- a/demo.cpp
+++ b/demo.cpp
@@ -222,8 +222,8 @@ int main(int argc, char **argv)
                //vignette_effect->set_float("inner_radius", inner_radius);
                //vignette_effect->set_vec2("center", (float[]){ 0.7f, 0.5f });
 
-               diffusion_effect->set_float("radius", blur_radius);
-               diffusion_effect->set_float("blurred_mix_amount", blurred_mix_amount);
+               CHECK(diffusion_effect->set_float("radius", blur_radius));
+               CHECK(diffusion_effect->set_float("blurred_mix_amount", blurred_mix_amount));
 
                input->set_pixel_data(src_img);
                chain.render_to_screen();
index db0fa94d43397804194021c910419363e826d14e..21068effc20b7debc024923ca3e12cd504ca63fb 100644 (file)
--- a/effect.h
+++ b/effect.h
@@ -179,10 +179,10 @@ public:
 
        // Set a parameter; intended to be called from user code.
        // Neither of these take ownership of the pointer.
-       virtual bool set_int(const std::string&, int value);
-       virtual bool set_float(const std::string &key, float value);
-       virtual bool set_vec2(const std::string &key, const float *values);
-       virtual bool set_vec3(const std::string &key, const float *values);
+       virtual bool set_int(const std::string&, int value) MUST_CHECK_RESULT;
+       virtual bool set_float(const std::string &key, float value) MUST_CHECK_RESULT;
+       virtual bool set_vec2(const std::string &key, const float *values) MUST_CHECK_RESULT;
+       virtual bool set_vec3(const std::string &key, const float *values) MUST_CHECK_RESULT;
 
 protected:
        // Register a parameter. Whenever set_*() is called with the same key,
index d1654e50a05adae169fea1c9bc7955834625a734..5b5e3675fea17a81430b21eec75e225e211228c5 100644 (file)
@@ -259,7 +259,7 @@ Phase *EffectChain::compile_glsl_program(
        for (unsigned i = 0; i < effects.size(); ++i) {
                Node *node = effects[i];
                if (node->effect->num_inputs() == 0) {
-                       node->effect->set_int("needs_mipmaps", input_needs_mipmaps);
+                       CHECK(node->effect->set_int("needs_mipmaps", input_needs_mipmaps));
                }
        }
        frag_shader += std::string("#define INPUT ") + effects.back()->effect_id + "\n";
@@ -726,8 +726,8 @@ void EffectChain::fix_internal_color_spaces()
                                        continue;
                                }
                                Node *conversion = add_node(new ColorspaceConversionEffect());
-                               conversion->effect->set_int("source_space", input->output_color_space);
-                               conversion->effect->set_int("destination_space", COLORSPACE_sRGB);
+                               CHECK(conversion->effect->set_int("source_space", input->output_color_space));
+                               CHECK(conversion->effect->set_int("destination_space", COLORSPACE_sRGB));
                                conversion->output_color_space = COLORSPACE_sRGB;
                                insert_node_between(input, conversion, node);
                        }
@@ -760,8 +760,8 @@ void EffectChain::fix_output_color_space()
        Node *output = find_output_node();
        if (output->output_color_space != output_format.color_space) {
                Node *conversion = add_node(new ColorspaceConversionEffect());
-               conversion->effect->set_int("source_space", output->output_color_space);
-               conversion->effect->set_int("destination_space", output_format.color_space);
+               CHECK(conversion->effect->set_int("source_space", output->output_color_space));
+               CHECK(conversion->effect->set_int("destination_space", output_format.color_space));
                conversion->output_color_space = output_format.color_space;
                connect_nodes(output, conversion);
                propagate_gamma_and_color_space();
@@ -841,7 +841,7 @@ void EffectChain::fix_internal_gamma_by_asking_inputs(unsigned step)
                        }
 
                        for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
-                               nonlinear_inputs[i]->effect->set_int("output_linear_gamma", 1);
+                               CHECK(nonlinear_inputs[i]->effect->set_int("output_linear_gamma", 1));
                                nonlinear_inputs[i]->output_gamma_curve = GAMMA_LINEAR;
                        }
 
@@ -879,7 +879,7 @@ void EffectChain::fix_internal_gamma_by_inserting_nodes(unsigned step)
                        if (node->incoming_links.empty()) {
                                assert(node->outgoing_links.empty());
                                Node *conversion = add_node(new GammaExpansionEffect());
-                               conversion->effect->set_int("source_curve", node->output_gamma_curve);
+                               CHECK(conversion->effect->set_int("source_curve", node->output_gamma_curve));
                                conversion->output_gamma_curve = GAMMA_LINEAR;
                                connect_nodes(node, conversion);
                        }
@@ -893,7 +893,7 @@ void EffectChain::fix_internal_gamma_by_inserting_nodes(unsigned step)
                                        continue;
                                }
                                Node *conversion = add_node(new GammaExpansionEffect());
-                               conversion->effect->set_int("source_curve", input->output_gamma_curve);
+                               CHECK(conversion->effect->set_int("source_curve", input->output_gamma_curve));
                                conversion->output_gamma_curve = GAMMA_LINEAR;
                                insert_node_between(input, conversion, node);
                        }
@@ -928,7 +928,7 @@ void EffectChain::fix_output_gamma()
        Node *output = find_output_node();
        if (output->output_gamma_curve != output_format.gamma_curve) {
                Node *conversion = add_node(new GammaCompressionEffect());
-               conversion->effect->set_int("destination_curve", output_format.gamma_curve);
+               CHECK(conversion->effect->set_int("destination_curve", output_format.gamma_curve));
                conversion->output_gamma_curve = output_format.gamma_curve;
                connect_nodes(output, conversion);
        }
index a4775052d4503dbf7b6c0fc7f5b5d53ad584f811..064cdac99950e561d9817f4d1115f252451e067b 100644 (file)
@@ -12,10 +12,10 @@ GlowEffect::GlowEffect()
          cutoff(new HighlightCutoffEffect),
          mix(new MixEffect)
 {
-       blur->set_float("radius", 20.0f);
-       mix->set_float("strength_first", 1.0f);
-       mix->set_float("strength_second", 1.0f);
-       cutoff->set_float("cutoff", 0.2f);
+       CHECK(blur->set_float("radius", 20.0f));
+       CHECK(mix->set_float("strength_first", 1.0f));
+       CHECK(mix->set_float("strength_second", 1.0f));
+       CHECK(cutoff->set_float("cutoff", 0.2f));
 }
 
 void GlowEffect::rewrite_graph(EffectChain *graph, Node *self)
index c0afe0b1964ec06c2ab757883327c6a13570276a..e1e6199ec5b5b857add0df623d6fd05a25d4b1e0 100644 (file)
@@ -103,9 +103,9 @@ ResampleEffect::ResampleEffect()
 
        // The first blur pass will forward resolution information to us.
        hpass = new SingleResamplePassEffect(this);
-       hpass->set_int("direction", SingleResamplePassEffect::HORIZONTAL);
+       CHECK(hpass->set_int("direction", SingleResamplePassEffect::HORIZONTAL));
        vpass = new SingleResamplePassEffect(NULL);
-       vpass->set_int("direction", SingleResamplePassEffect::VERTICAL);
+       CHECK(vpass->set_int("direction", SingleResamplePassEffect::VERTICAL));
 
        update_size();
 }
index 545910f2af7519636ec91e32cfb1d586aa1694df..0ca22eea5d36543601a9e3277754266cff3f6b7d 100644 (file)
@@ -11,8 +11,8 @@ UnsharpMaskEffect::UnsharpMaskEffect()
        : blur(new BlurEffect),
          mix(new MixEffect)
 {
-       mix->set_float("strength_first", 1.0f);
-       mix->set_float("strength_second", -0.3f);
+       CHECK(mix->set_float("strength_first", 1.0f));
+       CHECK(mix->set_float("strength_second", -0.3f));
 }
 
 void UnsharpMaskEffect::rewrite_graph(EffectChain *graph, Node *self)
diff --git a/util.h b/util.h
index 082d1146a7f4c86c2d935ffeaa62307f6575b884..b01d62c00f00588a21b2587e6387f649b6b3bdc1 100644 (file)
--- a/util.h
+++ b/util.h
@@ -49,4 +49,17 @@ void combine_two_samples(float w1, float w2, float *offset, float *total_weight,
 #define check_error() { int err = glGetError(); if (err != GL_NO_ERROR) { printf("GL error 0x%x at %s:%d\n", err, __FILE__, __LINE__); exit(1); } }
 #endif
 
+#ifdef __GNUC__
+#define MUST_CHECK_RESULT __attribute__((warn_unused_result))
+#else
+#define MUST_CHECK_RESULT 
+#endif
+
+// CHECK() is like assert(), but retains any side effects no matter the compilation mode.
+#ifdef NDEBUG
+#define CHECK(x) (void)(x)
+#else
+#define CHECK(x) do { bool ok = x; if (!ok) { fprintf(stderr, "%s:%d: %s: Assertion `%s' failed.\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #x); abort(); } } while (false)
+#endif
+
 #endif // !defined(_UTIL_H)