mutter/fixed-to-float-patches/clutter-alpha.c.0.patch
Robert Bragg e8915fcb12 First cut at a fixed point to floating point conversion script + patches
This commit doesn't actually include any direct changes to source; you
have to run ./fixed-to-float.sh. Note: the script will make a number of
commits itself to your git repository a various stages of the script.
You will need to reset these if you want to re-run the script.

* NB: Be carefull about how you reset your tree, if you are making changes
to the script and patches, so you don't loose your changes *

This aims to remove all use of fixed point within Clutter and Cogl. It aims to
not break the Clutter API, including maintaining the CLUTTER_FIXED macros,
(though they now handle floats not 16.16 fixed)

It maintains cogl-fixed.[ch] as a utility API that can be used by applications
(and potentially for focused internal optimisations), but all Cogl interfaces
now accept floats in place of CoglFixed.

Note: the choice to to use single precision floats, not doubles is very
intentional. GPUs are basically all single precision; only this year have high
end cards started adding double precision - aimed mostly at the GPGPU market.
This means if you pass doubles into any GL[ES] driver, you can expect those
numbers to be cast to a float. (Certainly this is true of Mesa wich casts
most things to floats internally) It can be a noteable performance issue to
cast from double->float frequently, and if we were to have an api defined in
terms of doubles, that would imply a *lot* of unneeded casting. One of the
noteable issues with fixed point was the amount of casting required, so I
don't want to overshoot the mark and require just as much casting still. Double
precision arithmatic is also slower, so it usually makes sense to minimize its
use if the extra precision isn't needed. In the same way that the fast/low
precision fixed API can be used sparingly for optimisations; if needs be in
certain situations we can promote to doubles internally for higher precision.

E.g.
quoting Brian Paul (talking about performance optimisations for GL programmers):
"Avoid double precision valued functions
    Mesa does all internal floating point computations in single precision
    floating point. API functions which take double precision floating point
    values must convert them to single precision. This can be expensive in the
    case of glVertex, glNormal, etc. "
2008-12-22 16:59:00 +00:00

264 lines
7.6 KiB
Diff

diff --git a/clutter/clutter-alpha.c b/clutter/clutter-alpha.c
index 3e4df4d..74a5c7a 100644
--- a/clutter/clutter-alpha.c
+++ b/clutter/clutter-alpha.c
@@ -694,6 +694,11 @@ clutter_ramp_func (ClutterAlpha *alpha,
}
}
+#if 0
+/*
+ * The following three functions are left in place for reference
+ * purposes.
+ */
static guint32
sincx1024_func (ClutterAlpha *alpha,
ClutterAngle angle,
@@ -713,7 +718,7 @@ sincx1024_func (ClutterAlpha *alpha,
x -= (512 * 512 / angle);
- sine = ((sinf (x) + offset) / 2)
+ sine = ((cogl_angle_sin (x) + offset) / 2)
* CLUTTER_ALPHA_MAX_ALPHA;
sine = sine >> COGL_FIXED_Q;
@@ -721,11 +726,6 @@ sincx1024_func (ClutterAlpha *alpha,
return sine;
}
-#if 0
-/*
- * The following two functions are left in place for reference
- * purposes.
- */
static guint32
sincx_func (ClutterAlpha *alpha,
ClutterFixed angle,
@@ -744,7 +744,7 @@ sincx_func (ClutterAlpha *alpha,
x = CLUTTER_FIXED_MUL (x, CFX_PI)
- CLUTTER_FIXED_DIV (CFX_PI, angle);
- sine = (sinf (x) + offset) / 2;
+ sine = (cogl_angle_sin (x) + offset) / 2;
CLUTTER_NOTE (ALPHA, "sine: %2f\n", CLUTTER_FIXED_TO_DOUBLE (sine));
@@ -803,9 +803,25 @@ guint32
clutter_sine_func (ClutterAlpha *alpha,
gpointer dummy)
{
-#if 0
+#if 1
+ ClutterTimeline *timeline;
+ gint current_frame_num, n_frames;
+ float radians, sine;
+
+ timeline = clutter_alpha_get_timeline (alpha);
+
+ current_frame_num = clutter_timeline_get_current_frame (timeline);
+ n_frames = clutter_timeline_get_n_frames (timeline);
+
+ radians = ((float)current_frame_num / n_frames) * (2.0 * G_PI);
+ sine = sinf (radians);
+
+ CLUTTER_NOTE (ALPHA, "sine: %2f\n", sine);
+
+ return COGL_FLOAT_TO_INT ((sine * CLUTTER_ALPHA_MAX_ALPHA));
+#elif 0
return sinc_func (alpha, 2.0, 1.0);
-#else
+#elif 0
/* 2.0 above represents full circle */
return sincx1024_func (alpha, 1024, 1.0);
#endif
@@ -839,18 +855,17 @@ clutter_sine_inc_func (ClutterAlpha *alpha,
ClutterTimeline * timeline;
gint frame;
gint n_frames;
- ClutterAngle x;
- ClutterFixed sine;
+ float radians;
+ float sine;
timeline = clutter_alpha_get_timeline (alpha);
frame = clutter_timeline_get_current_frame (timeline);
n_frames = clutter_timeline_get_n_frames (timeline);
- x = 256 * frame / n_frames;
-
- sine = sinf (x) * CLUTTER_ALPHA_MAX_ALPHA;
+ radians = ((float)frame / n_frames) * (G_PI / 2);
+ sine = sinf (radians);
- return ((guint32) sine) >> COGL_FIXED_Q;
+ return (guint32) (sine * CLUTTER_ALPHA_MAX_ALPHA);
}
/**
@@ -881,18 +896,17 @@ clutter_sine_dec_func (ClutterAlpha *alpha,
ClutterTimeline * timeline;
gint frame;
gint n_frames;
- ClutterAngle x;
- ClutterFixed sine;
+ float radians;
+ float sine;
timeline = clutter_alpha_get_timeline (alpha);
frame = clutter_timeline_get_current_frame (timeline);
n_frames = clutter_timeline_get_n_frames (timeline);
- x = 256 * frame / n_frames + 256;
-
- sine = sinf (x) * CLUTTER_ALPHA_MAX_ALPHA;
+ radians = ((float)frame / n_frames) * (G_PI / 2);
+ sine = sinf (radians + (G_PI / 2));
- return ((guint32) sine) >> COGL_FIXED_Q;
+ return (guint32) (sine * CLUTTER_ALPHA_MAX_ALPHA);
}
/**
@@ -923,18 +937,17 @@ clutter_sine_half_func (ClutterAlpha *alpha,
ClutterTimeline *timeline;
gint frame;
gint n_frames;
- ClutterAngle x;
- ClutterFixed sine;
+ float radians;
+ float sine;
timeline = clutter_alpha_get_timeline (alpha);
frame = clutter_timeline_get_current_frame (timeline);
n_frames = clutter_timeline_get_n_frames (timeline);
- x = 512 * frame / n_frames;
+ radians = ((float)frame / n_frames) * G_PI;
+ sine = sinf (radians);
- sine = sinf (x) * CLUTTER_ALPHA_MAX_ALPHA;
-
- return ((guint32) sine) >> COGL_FIXED_Q;
+ return (guint32) (sine * CLUTTER_ALPHA_MAX_ALPHA);
}
/**
@@ -959,19 +972,17 @@ clutter_sine_in_func (ClutterAlpha *alpha,
ClutterTimeline *timeline;
gint frame;
gint n_frames;
- ClutterAngle x;
- ClutterFixed sine;
+ float radians;
+ float sine;
timeline = clutter_alpha_get_timeline (alpha);
frame = clutter_timeline_get_current_frame (timeline);
n_frames = clutter_timeline_get_n_frames (timeline);
- /* XXX- if we use 768 we overflow */
- x = 256 * frame / n_frames + 767;
-
- sine = (sinf (x) + 1) * CLUTTER_ALPHA_MAX_ALPHA;
+ radians = ((float)frame / n_frames) * (G_PI / 2);
+ sine = sinf (radians - (G_PI / 2)) + 1.0;
- return ((guint32) sine) >> COGL_FIXED_Q;
+ return (guint32) (sine * CLUTTER_ALPHA_MAX_ALPHA);
}
/**
@@ -995,18 +1006,17 @@ clutter_sine_out_func (ClutterAlpha *alpha,
ClutterTimeline *timeline;
gint frame;
gint n_frames;
- ClutterAngle x;
- ClutterFixed sine;
+ float radians;
+ float sine;
timeline = clutter_alpha_get_timeline (alpha);
frame = clutter_timeline_get_current_frame (timeline);
n_frames = clutter_timeline_get_n_frames (timeline);
- x = 256 * frame / n_frames;
-
- sine = sinf (x) * CLUTTER_ALPHA_MAX_ALPHA;
+ radians = ((float)frame / n_frames) * (G_PI / 2);
+ sine = sinf (radians);
- return ((guint32) sine) >> COGL_FIXED_Q;
+ return (guint32) (sine * CLUTTER_ALPHA_MAX_ALPHA);
}
/**
@@ -1031,18 +1041,17 @@ clutter_sine_in_out_func (ClutterAlpha *alpha,
ClutterTimeline *timeline;
gint frame;
gint n_frames;
- ClutterAngle x;
- ClutterFixed sine;
+ float radians;
+ float sine;
timeline = clutter_alpha_get_timeline (alpha);
frame = clutter_timeline_get_current_frame (timeline);
n_frames = clutter_timeline_get_n_frames (timeline);
- x = -256 * frame / n_frames + 256;
+ radians = ((float)frame / n_frames) * G_PI;
+ sine = (sinf (radians - (G_PI / 2)) + 1.0) / 2.0;
- sine = (sinf (x) + 1) / 2 * CLUTTER_ALPHA_MAX_ALPHA;
-
- return ((guint32) sine) >> COGL_FIXED_Q;
+ return (guint32) (sine * CLUTTER_ALPHA_MAX_ALPHA);
}
/**
@@ -1201,9 +1210,9 @@ clutter_exp_inc_func (ClutterAlpha *alpha,
*
* (2^x_alpha_max) - 1 == CLUTTER_ALPHA_MAX_ALPHA
*/
-#if CLUTTER_ALPHA_MAX_ALPHA != 0xffff
-#error Adjust x_alpha_max to match CLUTTER_ALPHA_MAX_ALPHA
-#endif
+ /* XXX: If this fails:
+ * Adjust x_alpha_max to match CLUTTER_ALPHA_MAX_ALPHA */
+ g_assert (CLUTTER_ALPHA_MAX_ALPHA == 65535.0);
timeline = clutter_alpha_get_timeline (alpha);
frame = clutter_timeline_get_current_frame (timeline);
@@ -1211,7 +1220,7 @@ clutter_exp_inc_func (ClutterAlpha *alpha,
x = x_alpha_max * frame / n_frames;
- result = CLAMP (pow2f (x) - 1, 0, CLUTTER_ALPHA_MAX_ALPHA);
+ result = CLAMP (powf (2, x) - 1, 0, CLUTTER_ALPHA_MAX_ALPHA);
return result;
}
@@ -1252,9 +1261,9 @@ clutter_exp_dec_func (ClutterAlpha *alpha,
*
* (2^x_alpha_max) - 1 == CLUTTER_ALPHA_MAX_ALPHA
*/
-#if CLUTTER_ALPHA_MAX_ALPHA != 0xffff
-#error Adjust x_alpha_max to match CLUTTER_ALPHA_MAX_ALPHA
-#endif
+ /* XXX: If this fails:
+ * Adjust x_alpha_max to match CLUTTER_ALPHA_MAX_ALPHA */
+ g_assert (CLUTTER_ALPHA_MAX_ALPHA == 65535.0);
timeline = clutter_alpha_get_timeline (alpha);
frame = clutter_timeline_get_current_frame (timeline);
@@ -1262,7 +1271,7 @@ clutter_exp_dec_func (ClutterAlpha *alpha,
x = (x_alpha_max * (n_frames - frame)) / n_frames;
- result = CLAMP (pow2f (x) - 1, 0, CLUTTER_ALPHA_MAX_ALPHA);
+ result = CLAMP (powf (2, x) - 1, 0, CLUTTER_ALPHA_MAX_ALPHA);
return result;
}