1
0
Fork 0

util: optimize _clutter_util_fully_transform_vertices

Instead of unconditionally combining the modelview and projection
matrices and then iterating each of the vertices to call
cogl_matrix_transform_point for each one in turn we now only combine the
matrices if there are more than 4 vertices (with less than 4 vertices
its less work to transform them separately) and we use the new
cogl_vertex_{transform,project}_points APIs which can hopefully
vectorize the transformations.

Finally the perspective divide and viewport scale is done in a separate
loop at the end and we don't do the spurious perspective divide and
viewport scale for the z component.
This commit is contained in:
Robert Bragg 2011-02-08 10:37:15 +00:00
parent 3b88029f38
commit 9130dc1311
3 changed files with 57 additions and 27 deletions

View file

@ -81,6 +81,14 @@ _clutter_gettext (const gchar *str)
#define MTX_GL_SCALE_Y(y,w,v1,v2) ((v1) - (((((y) / (w)) + 1.0f) / 2.0f) * (v1)) + (v2)) #define MTX_GL_SCALE_Y(y,w,v1,v2) ((v1) - (((((y) / (w)) + 1.0f) / 2.0f) * (v1)) + (v2))
#define MTX_GL_SCALE_Z(z,w,v1,v2) (MTX_GL_SCALE_X ((z), (w), (v1), (v2))) #define MTX_GL_SCALE_Z(z,w,v1,v2) (MTX_GL_SCALE_X ((z), (w), (v1), (v2)))
typedef struct _ClutterVertex4
{
float x;
float y;
float z;
float w;
} ClutterVertex4;
void void
_clutter_util_fully_transform_vertices (const CoglMatrix *modelview, _clutter_util_fully_transform_vertices (const CoglMatrix *modelview,
const CoglMatrix *projection, const CoglMatrix *projection,
@ -90,31 +98,53 @@ _clutter_util_fully_transform_vertices (const CoglMatrix *modelview,
int n_vertices) int n_vertices)
{ {
CoglMatrix modelview_projection; CoglMatrix modelview_projection;
ClutterVertex4 *vertices_tmp;
int i; int i;
/* XXX: we should find a way to cache this per actor */ vertices_tmp = g_alloca (sizeof (ClutterVertex4) * n_vertices);
cogl_matrix_multiply (&modelview_projection,
projection, if (n_vertices >= 4)
modelview); {
/* XXX: we should find a way to cache this per actor */
cogl_matrix_multiply (&modelview_projection,
projection,
modelview);
cogl_matrix_project_points (&modelview_projection,
3,
sizeof (ClutterVertex),
vertices_in,
sizeof (ClutterVertex4),
vertices_tmp,
n_vertices);
}
else
{
cogl_matrix_transform_points (modelview,
3,
sizeof (ClutterVertex),
vertices_in,
sizeof (ClutterVertex4),
vertices_tmp,
n_vertices);
cogl_matrix_project_points (projection,
3,
sizeof (ClutterVertex4),
vertices_tmp,
sizeof (ClutterVertex4),
vertices_tmp,
n_vertices);
}
for (i = 0; i < n_vertices; i++) for (i = 0; i < n_vertices; i++)
{ {
const ClutterVertex *vertex_in = &vertices_in[i]; ClutterVertex4 vertex_tmp = vertices_tmp[i];
ClutterVertex *vertex_out = &vertices_out[i]; ClutterVertex *vertex_out = &vertices_out[i];
gfloat x, y, z, w;
x = vertex_in->x;
y = vertex_in->y;
z = vertex_in->z;
w = 1.0;
/* Transform the point using the modelview matrix */
cogl_matrix_transform_point (&modelview_projection, &x, &y, &z, &w);
/* Finally translate from OpenGL coords to window coords */ /* Finally translate from OpenGL coords to window coords */
vertex_out->x = MTX_GL_SCALE_X (x, w, viewport[2], viewport[0]); vertex_out->x = MTX_GL_SCALE_X (vertex_tmp.x, vertex_tmp.w,
vertex_out->y = MTX_GL_SCALE_Y (y, w, viewport[3], viewport[1]); viewport[2], viewport[0]);
vertex_out->z = MTX_GL_SCALE_Z (z, w, viewport[2], viewport[0]); vertex_out->y = MTX_GL_SCALE_Y (vertex_tmp.y, vertex_tmp.w,
viewport[3], viewport[1]);
} }
} }

View file

@ -498,7 +498,7 @@ typedef struct _Point4f
static void static void
_cogl_matrix_transform_points_f2 (const CoglMatrix *matrix, _cogl_matrix_transform_points_f2 (const CoglMatrix *matrix,
size_t stride_in, size_t stride_in,
void *points_in, const void *points_in,
size_t stride_out, size_t stride_out,
void *points_out, void *points_out,
int n_points) int n_points)
@ -519,7 +519,7 @@ _cogl_matrix_transform_points_f2 (const CoglMatrix *matrix,
static void static void
_cogl_matrix_project_points_f2 (const CoglMatrix *matrix, _cogl_matrix_project_points_f2 (const CoglMatrix *matrix,
size_t stride_in, size_t stride_in,
void *points_in, const void *points_in,
size_t stride_out, size_t stride_out,
void *points_out, void *points_out,
int n_points) int n_points)
@ -541,7 +541,7 @@ _cogl_matrix_project_points_f2 (const CoglMatrix *matrix,
static void static void
_cogl_matrix_transform_points_f3 (const CoglMatrix *matrix, _cogl_matrix_transform_points_f3 (const CoglMatrix *matrix,
size_t stride_in, size_t stride_in,
void *points_in, const void *points_in,
size_t stride_out, size_t stride_out,
void *points_out, void *points_out,
int n_points) int n_points)
@ -565,7 +565,7 @@ _cogl_matrix_transform_points_f3 (const CoglMatrix *matrix,
static void static void
_cogl_matrix_project_points_f3 (const CoglMatrix *matrix, _cogl_matrix_project_points_f3 (const CoglMatrix *matrix,
size_t stride_in, size_t stride_in,
void *points_in, const void *points_in,
size_t stride_out, size_t stride_out,
void *points_out, void *points_out,
int n_points) int n_points)
@ -591,7 +591,7 @@ _cogl_matrix_project_points_f3 (const CoglMatrix *matrix,
static void static void
_cogl_matrix_project_points_f4 (const CoglMatrix *matrix, _cogl_matrix_project_points_f4 (const CoglMatrix *matrix,
size_t stride_in, size_t stride_in,
void *points_in, const void *points_in,
size_t stride_out, size_t stride_out,
void *points_out, void *points_out,
int n_points) int n_points)
@ -618,7 +618,7 @@ void
cogl_matrix_transform_points (const CoglMatrix *matrix, cogl_matrix_transform_points (const CoglMatrix *matrix,
int n_components, int n_components,
size_t stride_in, size_t stride_in,
void *points_in, const void *points_in,
size_t stride_out, size_t stride_out,
void *points_out, void *points_out,
int n_points) int n_points)
@ -646,7 +646,7 @@ void
cogl_matrix_project_points (const CoglMatrix *matrix, cogl_matrix_project_points (const CoglMatrix *matrix,
int n_components, int n_components,
size_t stride_in, size_t stride_in,
void *points_in, const void *points_in,
size_t stride_out, size_t stride_out,
void *points_out, void *points_out,
int n_points) int n_points)

View file

@ -505,7 +505,7 @@ void
cogl_matrix_transform_points (const CoglMatrix *matrix, cogl_matrix_transform_points (const CoglMatrix *matrix,
int n_components, int n_components,
size_t stride_in, size_t stride_in,
void *points_in, const void *points_in,
size_t stride_out, size_t stride_out,
void *points_out, void *points_out,
int n_points); int n_points);
@ -560,7 +560,7 @@ void
cogl_matrix_project_points (const CoglMatrix *matrix, cogl_matrix_project_points (const CoglMatrix *matrix,
int n_components, int n_components,
size_t stride_in, size_t stride_in,
void *points_in, const void *points_in,
size_t stride_out, size_t stride_out,
void *points_out, void *points_out,
int n_points); int n_points);