matrix: Adds experimental cogl_matrix_{transform,project}_points
This add two new function that allows us to transform or project an array of points instead of only transforming one point at a time. Recent benchmarking has shown cogl_matrix_transform_point to be a bottleneck sometimes, so this should allow us to reduce the overhead when transforming lots of vertices at the same time, and also reduce the cost of 3 component, non-projective transforms. For now they are marked as experimental (you have to define COGL_ENABLE_EXPERIMENTAL_API) because there is some concern that it introduces some inconsistent naming. cogl_matrix_transform_point would have to be renamed cogl_matrix_project_point to be consistent, but that would be an API break.
This commit is contained in:
@ -24,6 +24,10 @@
|
||||
* Robert Bragg <robert@linux.intel.com>
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#define USE_MESA_MATRIX_API
|
||||
|
||||
#include <cogl.h>
|
||||
@ -391,4 +395,346 @@ cogl_matrix_transform_point (const CoglMatrix *matrix,
|
||||
*w = matrix->wx * _x + matrix->wy * _y + matrix->wz * _z + matrix->ww * _w;
|
||||
}
|
||||
|
||||
typedef struct _Point2f
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
} Point2f;
|
||||
|
||||
typedef struct _Point3f
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
} Point3f;
|
||||
|
||||
typedef struct _Point4f
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float w;
|
||||
} Point4f;
|
||||
|
||||
static void
|
||||
_cogl_matrix_transform_points_f2_packed (const CoglMatrix *matrix,
|
||||
void *points_in,
|
||||
void *points_out,
|
||||
int n_points)
|
||||
{
|
||||
Point2f *p = points_in;
|
||||
Point3f *o = points_out;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_points; i++)
|
||||
{
|
||||
o[i].x = matrix->xx * p[i].x + matrix->xy * p[i].y +
|
||||
matrix->xw;
|
||||
o[i].y = matrix->yx * p[i].x + matrix->yy * p[i].y +
|
||||
matrix->yw;
|
||||
o[i].z = matrix->zx * p[i].x + matrix->zy * p[i].y +
|
||||
matrix->zw;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_cogl_matrix_transform_points_f2_strided (const CoglMatrix *matrix,
|
||||
size_t stride_in,
|
||||
void *points_in,
|
||||
size_t stride_out,
|
||||
void *points_out,
|
||||
int n_points)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_points; i++)
|
||||
{
|
||||
Point2f *p = (Point2f *)((guint8 *)points_in + i * stride_in);
|
||||
Point3f *o = (Point3f *)((guint8 *)points_out + i * stride_out);
|
||||
|
||||
o->x = matrix->xx * p->x + matrix->xy * p->y + matrix->xw;
|
||||
o->y = matrix->yx * p->x + matrix->yy * p->y + matrix->yw;
|
||||
o->z = matrix->zx * p->x + matrix->zy * p->y + matrix->zw;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_cogl_matrix_project_points_f2_packed (const CoglMatrix *matrix,
|
||||
void *points_in,
|
||||
void *points_out,
|
||||
int n_points)
|
||||
{
|
||||
Point2f *p = points_in;
|
||||
Point4f *o = points_out;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_points; i++)
|
||||
{
|
||||
o[i].x = matrix->xx * p[i].x + matrix->xy * p[i].y +
|
||||
matrix->xw;
|
||||
o[i].y = matrix->yx * p[i].x + matrix->yy * p[i].y +
|
||||
matrix->yw;
|
||||
o[i].z = matrix->zx * p[i].x + matrix->zy * p[i].y +
|
||||
matrix->zw;
|
||||
o[i].w = matrix->wx * p[i].x + matrix->wy * p[i].y +
|
||||
matrix->ww;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_cogl_matrix_project_points_f2_strided (const CoglMatrix *matrix,
|
||||
size_t stride_in,
|
||||
void *points_in,
|
||||
size_t stride_out,
|
||||
void *points_out,
|
||||
int n_points)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_points; i++)
|
||||
{
|
||||
Point2f *p = (Point2f *)((guint8 *)points_in + i * stride_in);
|
||||
Point4f *o = (Point4f *)((guint8 *)points_out + i * stride_out);
|
||||
|
||||
o->x = matrix->xx * p->x + matrix->xy * p->y + matrix->xw;
|
||||
o->y = matrix->yx * p->x + matrix->yy * p->y + matrix->yw;
|
||||
o->z = matrix->zx * p->x + matrix->zy * p->y + matrix->zw;
|
||||
o->w = matrix->wx * p->x + matrix->wy * p->y + matrix->ww;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_cogl_matrix_transform_points_f3_packed (const CoglMatrix *matrix,
|
||||
void *points_in,
|
||||
void *points_out,
|
||||
int n_points)
|
||||
{
|
||||
Point3f *p = points_in;
|
||||
Point3f *o = points_out;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_points; i++)
|
||||
{
|
||||
o[i].x = matrix->xx * p[i].x + matrix->xy * p[i].y +
|
||||
matrix->xz * p[i].z + matrix->xw;
|
||||
o[i].y = matrix->yx * p[i].x + matrix->yy * p[i].y +
|
||||
matrix->yz * p[i].z + matrix->yw;
|
||||
o[i].z = matrix->zx * p[i].x + matrix->zy * p[i].y +
|
||||
matrix->zz * p[i].z + matrix->zw;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_cogl_matrix_transform_points_f3_strided (const CoglMatrix *matrix,
|
||||
size_t stride_in,
|
||||
void *points_in,
|
||||
size_t stride_out,
|
||||
void *points_out,
|
||||
int n_points)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_points; i++)
|
||||
{
|
||||
Point3f *p = (Point3f *)((guint8 *)points_in + i * stride_in);
|
||||
Point3f *o = (Point3f *)((guint8 *)points_out + i * stride_out);
|
||||
|
||||
o->x = matrix->xx * p->x + matrix->xy * p->y +
|
||||
matrix->xz * p->z + matrix->xw;
|
||||
o->y = matrix->yx * p->x + matrix->yy * p->y +
|
||||
matrix->yz * p->z + matrix->yw;
|
||||
o->z = matrix->zx * p->x + matrix->zy * p->y +
|
||||
matrix->zz * p->z + matrix->zw;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_cogl_matrix_project_points_f3_packed (const CoglMatrix *matrix,
|
||||
void *points_in,
|
||||
void *points_out,
|
||||
int n_points)
|
||||
{
|
||||
Point3f *p = points_in;
|
||||
Point4f *o = points_out;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_points; i++)
|
||||
{
|
||||
o[i].x = matrix->xx * p[i].x + matrix->xy * p[i].y +
|
||||
matrix->xz * p[i].z + matrix->xw;
|
||||
o[i].y = matrix->yx * p[i].x + matrix->yy * p[i].y +
|
||||
matrix->yz * p[i].z + matrix->yw;
|
||||
o[i].z = matrix->zx * p[i].x + matrix->zy * p[i].y +
|
||||
matrix->zz * p[i].z + matrix->zw;
|
||||
o[i].w = matrix->wx * p[i].x + matrix->wy * p[i].y +
|
||||
matrix->wz * p[i].z + matrix->ww;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_cogl_matrix_project_points_f3_strided (const CoglMatrix *matrix,
|
||||
size_t stride_in,
|
||||
void *points_in,
|
||||
size_t stride_out,
|
||||
void *points_out,
|
||||
int n_points)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_points; i++)
|
||||
{
|
||||
Point3f *p = (Point3f *)((guint8 *)points_in + i * stride_in);
|
||||
Point4f *o = (Point4f *)((guint8 *)points_out + i * stride_out);
|
||||
|
||||
o->x = matrix->xx * p->x + matrix->xy * p->y +
|
||||
matrix->xz * p->z + matrix->xw;
|
||||
o->y = matrix->yx * p->x + matrix->yy * p->y +
|
||||
matrix->yz * p->z + matrix->yw;
|
||||
o->z = matrix->zx * p->x + matrix->zy * p->y +
|
||||
matrix->zz * p->z + matrix->zw;
|
||||
o->w = matrix->wx * p->x + matrix->wy * p->y +
|
||||
matrix->wz * p->z + matrix->ww;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_cogl_matrix_project_points_f4_packed (const CoglMatrix *matrix,
|
||||
void *points_in,
|
||||
void *points_out,
|
||||
int n_points)
|
||||
{
|
||||
Point4f *p = points_in;
|
||||
Point4f *o = points_out;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_points; i++)
|
||||
{
|
||||
o[i].x = matrix->xx * p[i].x + matrix->xy * p[i].y +
|
||||
matrix->xz * p[i].z + matrix->xw * p[i].w;
|
||||
o[i].y = matrix->yx * p[i].x + matrix->yy * p[i].y +
|
||||
matrix->yz * p[i].z + matrix->yw * p[i].w;
|
||||
o[i].z = matrix->zx * p[i].x + matrix->zy * p[i].y +
|
||||
matrix->zz * p[i].z + matrix->zw * p[i].w;
|
||||
o[i].w = matrix->wx * p[i].x + matrix->wy * p[i].y +
|
||||
matrix->wz * p[i].z + matrix->ww * p[i].w;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_cogl_matrix_project_points_f4_strided (const CoglMatrix *matrix,
|
||||
size_t stride_in,
|
||||
void *points_in,
|
||||
size_t stride_out,
|
||||
void *points_out,
|
||||
int n_points)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_points; i++)
|
||||
{
|
||||
Point4f *p = (Point4f *)((guint8 *)points_in + i * stride_in);
|
||||
Point4f *o = (Point4f *)((guint8 *)points_out + i * stride_out);
|
||||
|
||||
o->x = matrix->xx * p->x + matrix->xy * p->y +
|
||||
matrix->xz * p->z + matrix->xw * p->w;
|
||||
o->y = matrix->yx * p->x + matrix->yy * p->y +
|
||||
matrix->yz * p->z + matrix->yw * p->w;
|
||||
o->z = matrix->zx * p->x + matrix->zy * p->y +
|
||||
matrix->zz * p->z + matrix->zw * p->w;
|
||||
o->w = matrix->wx * p->x + matrix->wy * p->y +
|
||||
matrix->wz * p->z + matrix->ww * p->w;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cogl_matrix_transform_points (const CoglMatrix *matrix,
|
||||
int n_components,
|
||||
size_t stride_in,
|
||||
void *points_in,
|
||||
size_t stride_out,
|
||||
void *points_out,
|
||||
int n_points)
|
||||
{
|
||||
/* The results of transforming always have three components... */
|
||||
g_return_if_fail (stride_out >= sizeof (Point3f));
|
||||
|
||||
if (n_components == 2)
|
||||
{
|
||||
if (stride_in == sizeof (Point2f) &&
|
||||
stride_out == sizeof (Point3f))
|
||||
_cogl_matrix_transform_points_f2_packed (matrix,
|
||||
points_in, points_out,
|
||||
n_points);
|
||||
else
|
||||
_cogl_matrix_transform_points_f2_strided (matrix,
|
||||
stride_in, points_in,
|
||||
stride_out, points_out,
|
||||
n_points);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_return_if_fail (n_components == 3);
|
||||
|
||||
if (stride_in == sizeof (Point3f) && stride_out == stride_in)
|
||||
_cogl_matrix_transform_points_f3_packed (matrix,
|
||||
points_in, points_out,
|
||||
n_points);
|
||||
else
|
||||
_cogl_matrix_transform_points_f3_strided (matrix,
|
||||
stride_in, points_in,
|
||||
stride_out, points_out,
|
||||
n_points);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cogl_matrix_project_points (const CoglMatrix *matrix,
|
||||
int n_components,
|
||||
size_t stride_in,
|
||||
void *points_in,
|
||||
size_t stride_out,
|
||||
void *points_out,
|
||||
int n_points)
|
||||
{
|
||||
if (n_components == 2)
|
||||
{
|
||||
if (stride_in == sizeof (Point2f) &&
|
||||
stride_out == sizeof (Point4f))
|
||||
_cogl_matrix_project_points_f2_packed (matrix,
|
||||
points_in, points_out,
|
||||
n_points);
|
||||
else
|
||||
_cogl_matrix_project_points_f2_strided (matrix,
|
||||
stride_in, points_in,
|
||||
stride_out, points_out,
|
||||
n_points);
|
||||
}
|
||||
else if (n_components == 3)
|
||||
{
|
||||
if (stride_in == sizeof (Point3f) &&
|
||||
stride_out == sizeof (Point4f))
|
||||
_cogl_matrix_project_points_f3_packed (matrix,
|
||||
points_in, points_out,
|
||||
n_points);
|
||||
else
|
||||
_cogl_matrix_project_points_f3_strided (matrix,
|
||||
stride_in, points_in,
|
||||
stride_out, points_out,
|
||||
n_points);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_return_if_fail (n_components == 4);
|
||||
|
||||
if (stride_in == sizeof (Point4f) && stride_out == stride_in)
|
||||
_cogl_matrix_project_points_f4_packed (matrix,
|
||||
points_in, points_out,
|
||||
n_points);
|
||||
else
|
||||
_cogl_matrix_project_points_f4_strided (matrix,
|
||||
stride_in, points_in,
|
||||
stride_out, points_out,
|
||||
n_points);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user