| 1 |
/* |
| 2 |
* Mesa 3-D graphics library |
| 3 |
* Version: 5.1 |
| 4 |
* |
| 5 |
* Copyright (C) 1999-2003 Brian Paul All Rights Reserved. |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
* copy of this software and associated documentation files (the "Software"), |
| 9 |
* to deal in the Software without restriction, including without limitation |
| 10 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
* Software is furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included |
| 15 |
* in all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 |
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 21 |
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
|
| 26 |
/* |
| 27 |
* Matrix/vertex/vector transformation stuff |
| 28 |
* |
| 29 |
* |
| 30 |
* NOTES: |
| 31 |
* 1. 4x4 transformation matrices are stored in memory in column major order. |
| 32 |
* 2. Points/vertices are to be thought of as column vectors. |
| 33 |
* 3. Transformation of a point p by a matrix M is: p' = M * p |
| 34 |
*/ |
| 35 |
|
| 36 |
#include "glheader.h" |
| 37 |
#include "macros.h" |
| 38 |
|
| 39 |
#include "m_eval.h" |
| 40 |
#include "m_matrix.h" |
| 41 |
#include "m_translate.h" |
| 42 |
#include "m_xform.h" |
| 43 |
#include "mathmod.h" |
| 44 |
|
| 45 |
|
| 46 |
#ifdef DEBUG |
| 47 |
#include "m_debug.h" |
| 48 |
#endif |
| 49 |
|
| 50 |
#ifdef USE_X86_ASM |
| 51 |
#include "x86/common_x86_asm.h" |
| 52 |
#endif |
| 53 |
|
| 54 |
#ifdef USE_X86_64_ASM |
| 55 |
#include "x86-64/x86-64.h" |
| 56 |
#endif |
| 57 |
|
| 58 |
#ifdef USE_SPARC_ASM |
| 59 |
#include "sparc/sparc.h" |
| 60 |
#endif |
| 61 |
|
| 62 |
#ifdef USE_PPC_ASM |
| 63 |
#include "ppc/common_ppc_features.h" |
| 64 |
#endif |
| 65 |
|
| 66 |
clip_func _mesa_clip_tab[5]; |
| 67 |
clip_func _mesa_clip_np_tab[5]; |
| 68 |
dotprod_func _mesa_dotprod_tab[5]; |
| 69 |
vec_copy_func _mesa_copy_tab[0x10]; |
| 70 |
normal_func _mesa_normal_tab[0xf]; |
| 71 |
transform_func *_mesa_transform_tab[5]; |
| 72 |
|
| 73 |
|
| 74 |
/* Raw data format used for: |
| 75 |
* - Object-to-eye transform prior to culling, although this too |
| 76 |
* could be culled under some circumstances. |
| 77 |
* - Eye-to-clip transform (via the function above). |
| 78 |
* - Cliptesting |
| 79 |
* - And everything else too, if culling happens to be disabled. |
| 80 |
* |
| 81 |
* GH: It's used for everything now, as clipping/culling is done |
| 82 |
* elsewhere (most often by the driver itself). |
| 83 |
*/ |
| 84 |
#define TAG(x) x |
| 85 |
#define TAG2(x,y) x##y |
| 86 |
#define STRIDE_LOOP for ( i = 0 ; i < count ; i++, STRIDE_F(from, stride) ) |
| 87 |
#define LOOP for ( i = 0 ; i < n ; i++ ) |
| 88 |
#define ARGS |
| 89 |
#include "m_xform_tmp.h" |
| 90 |
#include "m_clip_tmp.h" |
| 91 |
#include "m_norm_tmp.h" |
| 92 |
#include "m_dotprod_tmp.h" |
| 93 |
#include "m_copy_tmp.h" |
| 94 |
#undef TAG |
| 95 |
#undef TAG2 |
| 96 |
#undef LOOP |
| 97 |
#undef ARGS |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
GLvector4f *_mesa_project_points( GLvector4f *proj_vec, |
| 103 |
const GLvector4f *clip_vec ) |
| 104 |
{ |
| 105 |
const GLuint stride = clip_vec->stride; |
| 106 |
const GLfloat *from = (GLfloat *)clip_vec->start; |
| 107 |
const GLuint count = clip_vec->count; |
| 108 |
GLfloat (*vProj)[4] = (GLfloat (*)[4])proj_vec->start; |
| 109 |
GLuint i; |
| 110 |
|
| 111 |
for (i = 0 ; i < count ; i++, STRIDE_F(from, stride)) |
| 112 |
{ |
| 113 |
GLfloat oow = 1.0F / from[3]; |
| 114 |
vProj[i][3] = oow; |
| 115 |
vProj[i][0] = from[0] * oow; |
| 116 |
vProj[i][1] = from[1] * oow; |
| 117 |
vProj[i][2] = from[2] * oow; |
| 118 |
} |
| 119 |
|
| 120 |
proj_vec->flags |= VEC_SIZE_4; |
| 121 |
proj_vec->size = 3; |
| 122 |
proj_vec->count = clip_vec->count; |
| 123 |
return proj_vec; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
/* |
| 132 |
* Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix. This |
| 133 |
* function is used for transforming clipping plane equations and spotlight |
| 134 |
* directions. |
| 135 |
* Mathematically, u = v * m. |
| 136 |
* Input: v - input vector |
| 137 |
* m - transformation matrix |
| 138 |
* Output: u - transformed vector |
| 139 |
*/ |
| 140 |
void _mesa_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] ) |
| 141 |
{ |
| 142 |
GLfloat v0=v[0], v1=v[1], v2=v[2], v3=v[3]; |
| 143 |
#define M(row,col) m[row + col*4] |
| 144 |
u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0); |
| 145 |
u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1); |
| 146 |
u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2); |
| 147 |
u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3); |
| 148 |
#undef M |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
/* Useful for one-off point transformations, as in clipping. |
| 153 |
* Note that because the matrix isn't analysed we do too many |
| 154 |
* multiplies, and that the result is always 4-clean. |
| 155 |
*/ |
| 156 |
void _mesa_transform_point_sz( GLfloat Q[4], const GLfloat M[16], |
| 157 |
const GLfloat P[4], GLuint sz ) |
| 158 |
{ |
| 159 |
if (Q == P) |
| 160 |
return; |
| 161 |
|
| 162 |
if (sz == 4) |
| 163 |
{ |
| 164 |
Q[0] = M[0] * P[0] + M[4] * P[1] + M[8] * P[2] + M[12] * P[3]; |
| 165 |
Q[1] = M[1] * P[0] + M[5] * P[1] + M[9] * P[2] + M[13] * P[3]; |
| 166 |
Q[2] = M[2] * P[0] + M[6] * P[1] + M[10] * P[2] + M[14] * P[3]; |
| 167 |
Q[3] = M[3] * P[0] + M[7] * P[1] + M[11] * P[2] + M[15] * P[3]; |
| 168 |
} |
| 169 |
else if (sz == 3) |
| 170 |
{ |
| 171 |
Q[0] = M[0] * P[0] + M[4] * P[1] + M[8] * P[2] + M[12]; |
| 172 |
Q[1] = M[1] * P[0] + M[5] * P[1] + M[9] * P[2] + M[13]; |
| 173 |
Q[2] = M[2] * P[0] + M[6] * P[1] + M[10] * P[2] + M[14]; |
| 174 |
Q[3] = M[3] * P[0] + M[7] * P[1] + M[11] * P[2] + M[15]; |
| 175 |
} |
| 176 |
else if (sz == 2) |
| 177 |
{ |
| 178 |
Q[0] = M[0] * P[0] + M[4] * P[1] + M[12]; |
| 179 |
Q[1] = M[1] * P[0] + M[5] * P[1] + M[13]; |
| 180 |
Q[2] = M[2] * P[0] + M[6] * P[1] + M[14]; |
| 181 |
Q[3] = M[3] * P[0] + M[7] * P[1] + M[15]; |
| 182 |
} |
| 183 |
else if (sz == 1) |
| 184 |
{ |
| 185 |
Q[0] = M[0] * P[0] + M[12]; |
| 186 |
Q[1] = M[1] * P[0] + M[13]; |
| 187 |
Q[2] = M[2] * P[0] + M[14]; |
| 188 |
Q[3] = M[3] * P[0] + M[15]; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
/* |
| 194 |
* This is called only once. It initializes several tables with pointers |
| 195 |
* to optimized transformation functions. This is where we can test for |
| 196 |
* AMD 3Dnow! capability, Intel SSE, etc. and hook in the right code. |
| 197 |
*/ |
| 198 |
void |
| 199 |
_math_init_transformation( void ) |
| 200 |
{ |
| 201 |
init_c_transformations(); |
| 202 |
init_c_norm_transform(); |
| 203 |
init_c_cliptest(); |
| 204 |
init_copy0(); |
| 205 |
init_dotprod(); |
| 206 |
|
| 207 |
#ifdef DEBUG |
| 208 |
_math_test_all_transform_functions( "default" ); |
| 209 |
_math_test_all_normal_transform_functions( "default" ); |
| 210 |
_math_test_all_cliptest_functions( "default" ); |
| 211 |
#endif |
| 212 |
|
| 213 |
#ifdef USE_X86_ASM |
| 214 |
_mesa_init_all_x86_transform_asm(); |
| 215 |
#elif defined( USE_SPARC_ASM ) |
| 216 |
_mesa_init_all_sparc_transform_asm(); |
| 217 |
#elif defined( USE_PPC_ASM ) |
| 218 |
_mesa_init_all_ppc_transform_asm(); |
| 219 |
#elif defined( USE_X86_64_ASM ) |
| 220 |
_mesa_init_all_x86_64_transform_asm(); |
| 221 |
#endif |
| 222 |
} |
| 223 |
|
| 224 |
void |
| 225 |
_math_init( void ) |
| 226 |
{ |
| 227 |
_math_init_transformation(); |
| 228 |
_math_init_translate(); |
| 229 |
_math_init_eval(); |
| 230 |
} |