From 1d54ecb8a1fa22b11d23498085ce6404e7baf391 Mon Sep 17 00:00:00 2001 From: Neil Roberts Date: Wed, 17 Feb 2010 15:58:32 +0000 Subject: [PATCH] cogl-clip-stack: Round the coords when clipping to a window rect The size and position of the window rectangle for clipping in try_pushing_rect_as_window_rect is calculated by projecting the rectangle coordinates. Due to rounding errors, this can end up with slightly off numbers like 34.999999. These were then being cast directly to an integer so it could end up off by one. This uses a new macro called COGL_UTIL_NEARBYINT which is a replacement for the C99 nearbyint function. --- cogl/cogl-clip-stack.c | 6 +++++- cogl/cogl-util.h | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/cogl/cogl-clip-stack.c b/cogl/cogl-clip-stack.c index 3f64047a0..8e849c723 100644 --- a/cogl/cogl-clip-stack.c +++ b/cogl/cogl-clip-stack.c @@ -37,6 +37,7 @@ #include "cogl-internal.h" #include "cogl-framebuffer-private.h" #include "cogl-journal-private.h" +#include "cogl-util.h" void _cogl_add_path_to_stencil_buffer (floatVec2 nodes_min, floatVec2 nodes_max, @@ -483,7 +484,10 @@ try_pushing_rect_as_window_rect (float x_1, SWAP (y_1, y_2); #undef SWAP - cogl_clip_push_window_rectangle (x_1, y_1, x_2 - x_1, y_2 - y_1); + cogl_clip_push_window_rectangle (COGL_UTIL_NEARBYINT (x_1), + COGL_UTIL_NEARBYINT (y_1), + COGL_UTIL_NEARBYINT (x_2 - x_1), + COGL_UTIL_NEARBYINT (y_2 - y_1)); return TRUE; } diff --git a/cogl/cogl-util.h b/cogl/cogl-util.h index a6602a5f5..0e0e095d9 100644 --- a/cogl/cogl-util.h +++ b/cogl/cogl-util.h @@ -52,4 +52,12 @@ cogl_util_float_signbit (float x) } #endif +/* This is a replacement for the nearbyint function which always + rounds to the nearest integer. nearbyint is apparently a C99 + function so it might not always be available but also it seems in + glibc it is defined as a function call so this macro could end up + faster anyway. We can't just add 0.5f because it will break for + negative numbers. */ +#define COGL_UTIL_NEARBYINT(x) ((int) ((x) < 0.0f ? (x) - 0.5f : (x) + 0.5f)) + #endif /* __COGL_UTIL_H */