d3dx9_36: Implementation of D3DXSHMultiply3.
[wine] / dlls / d3dx9_36 / math.c
1 /*
2  * Mathematical operations specific to D3DX9.
3  *
4  * Copyright (C) 2008 David Adam
5  * Copyright (C) 2008 Luis Busquets
6  * Copyright (C) 2008 Jérôme Gardou
7  * Copyright (C) 2008 Philip Nilsson
8  * Copyright (C) 2008 Henri Verbeet
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #define NONAMELESSUNION
26
27 #include "config.h"
28 #include "wine/port.h"
29
30 #include "windef.h"
31 #include "wingdi.h"
32 #include "d3dx9_36_private.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
37
38 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl;
39
40 typedef struct ID3DXMatrixStackImpl
41 {
42   ID3DXMatrixStack ID3DXMatrixStack_iface;
43   LONG ref;
44
45   unsigned int current;
46   unsigned int stack_size;
47   D3DXMATRIX *stack;
48 } ID3DXMatrixStackImpl;
49
50
51 /*_________________D3DXColor____________________*/
52
53 D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
54 {
55     pout->r = 0.5f + s * (pc->r - 0.5f);
56     pout->g = 0.5f + s * (pc->g - 0.5f);
57     pout->b = 0.5f + s * (pc->b - 0.5f);
58     pout->a = pc->a;
59     return pout;
60 }
61
62 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
63 {
64     FLOAT grey;
65
66     grey = pc->r * 0.2125f + pc->g * 0.7154f + pc->b * 0.0721f;
67     pout->r = grey + s * (pc->r - grey);
68     pout->g = grey + s * (pc->g - grey);
69     pout->b = grey + s * (pc->b - grey);
70     pout->a = pc->a;
71     return pout;
72 }
73
74 /*_________________Misc__________________________*/
75
76 FLOAT WINAPI D3DXFresnelTerm(FLOAT costheta, FLOAT refractionindex)
77 {
78     FLOAT a, d, g, result;
79
80     g = sqrt(refractionindex * refractionindex + costheta * costheta - 1.0f);
81     a = g + costheta;
82     d = g - costheta;
83     result = ( costheta * a - 1.0f ) * ( costheta * a - 1.0f ) / ( ( costheta * d + 1.0f ) * ( costheta * d + 1.0f ) ) + 1.0f;
84     result = result * 0.5f * d * d / ( a * a );
85     return result;
86 }
87
88 /*_________________D3DXMatrix____________________*/
89
90 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR3 *rotationcenter, CONST D3DXQUATERNION *rotation, CONST D3DXVECTOR3 *translation)
91 {
92     D3DXMATRIX m1, m2, m3, m4, m5;
93
94     D3DXMatrixScaling(&m1, scaling, scaling, scaling);
95
96     if ( !rotationcenter )
97     {
98         D3DXMatrixIdentity(&m2);
99         D3DXMatrixIdentity(&m4);
100     }
101     else
102     {
103         D3DXMatrixTranslation(&m2, -rotationcenter->x, -rotationcenter->y, -rotationcenter->z);
104         D3DXMatrixTranslation(&m4, rotationcenter->x, rotationcenter->y, rotationcenter->z);
105     }
106
107     if ( !rotation ) D3DXMatrixIdentity(&m3);
108     else D3DXMatrixRotationQuaternion(&m3, rotation);
109
110     if ( !translation ) D3DXMatrixIdentity(&m5);
111     else D3DXMatrixTranslation(&m5, translation->x, translation->y, translation->z);
112
113     D3DXMatrixMultiply(&m1, &m1, &m2);
114     D3DXMatrixMultiply(&m1, &m1, &m3);
115     D3DXMatrixMultiply(&m1, &m1, &m4);
116     D3DXMatrixMultiply(pout, &m1, &m5);
117     return pout;
118 }
119
120 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation2D(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
121 {
122     D3DXMATRIX m1, m2, m3, m4, m5;
123     D3DXQUATERNION rot;
124     D3DXVECTOR3 rot_center, trans;
125
126     rot.w=cos(rotation/2.0f);
127     rot.x=0.0f;
128     rot.y=0.0f;
129     rot.z=sin(rotation/2.0f);
130
131     if ( protationcenter )
132     {
133         rot_center.x=protationcenter->x;
134         rot_center.y=protationcenter->y;
135         rot_center.z=0.0f;
136     }
137     else
138     {
139         rot_center.x=0.0f;
140         rot_center.y=0.0f;
141         rot_center.z=0.0f;
142     }
143
144     if ( ptranslation )
145     {
146         trans.x=ptranslation->x;
147         trans.y=ptranslation->y;
148         trans.z=0.0f;
149     }
150     else
151     {
152         trans.x=0.0f;
153         trans.y=0.0f;
154         trans.z=0.0f;
155     }
156
157     D3DXMatrixScaling(&m1, scaling, scaling, 1.0f);
158     D3DXMatrixTranslation(&m2, -rot_center.x, -rot_center.y, -rot_center.z);
159     D3DXMatrixTranslation(&m4, rot_center.x, rot_center.y, rot_center.z);
160     D3DXMatrixRotationQuaternion(&m3, &rot);
161     D3DXMatrixTranslation(&m5, trans.x, trans.y, trans.z);
162
163     D3DXMatrixMultiply(&m1, &m1, &m2);
164     D3DXMatrixMultiply(&m1, &m1, &m3);
165     D3DXMatrixMultiply(&m1, &m1, &m4);
166     D3DXMatrixMultiply(pout, &m1, &m5);
167
168     return pout;
169 }
170
171 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, CONST D3DXMATRIX *pm)
172 {
173     D3DXMATRIX normalized;
174     D3DXVECTOR3 vec;
175
176     /*Compute the scaling part.*/
177     vec.x=pm->u.m[0][0];
178     vec.y=pm->u.m[0][1];
179     vec.z=pm->u.m[0][2];
180     poutscale->x=D3DXVec3Length(&vec);
181
182     vec.x=pm->u.m[1][0];
183     vec.y=pm->u.m[1][1];
184     vec.z=pm->u.m[1][2];
185     poutscale->y=D3DXVec3Length(&vec);
186
187     vec.x=pm->u.m[2][0];
188     vec.y=pm->u.m[2][1];
189     vec.z=pm->u.m[2][2];
190     poutscale->z=D3DXVec3Length(&vec);
191
192     /*Compute the translation part.*/
193     pouttranslation->x=pm->u.m[3][0];
194     pouttranslation->y=pm->u.m[3][1];
195     pouttranslation->z=pm->u.m[3][2];
196
197     /*Let's calculate the rotation now*/
198     if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) ) return D3DERR_INVALIDCALL;
199
200     normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
201     normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
202     normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
203     normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
204     normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
205     normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
206     normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
207     normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
208     normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
209
210     D3DXQuaternionRotationMatrix(poutrotation,&normalized);
211     return S_OK;
212 }
213
214 FLOAT WINAPI D3DXMatrixDeterminant(CONST D3DXMATRIX *pm)
215 {
216     D3DXVECTOR4 minor, v1, v2, v3;
217     FLOAT det;
218
219     v1.x = pm->u.m[0][0]; v1.y = pm->u.m[1][0]; v1.z = pm->u.m[2][0]; v1.w = pm->u.m[3][0];
220     v2.x = pm->u.m[0][1]; v2.y = pm->u.m[1][1]; v2.z = pm->u.m[2][1]; v2.w = pm->u.m[3][1];
221     v3.x = pm->u.m[0][2]; v3.y = pm->u.m[1][2]; v3.z = pm->u.m[2][2]; v3.w = pm->u.m[3][2];
222     D3DXVec4Cross(&minor, &v1, &v2, &v3);
223     det =  - (pm->u.m[0][3] * minor.x + pm->u.m[1][3] * minor.y + pm->u.m[2][3] * minor.z + pm->u.m[3][3] * minor.w);
224     return det;
225 }
226
227 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, CONST D3DXMATRIX *pm)
228 {
229     int a, i, j;
230     D3DXMATRIX out;
231     D3DXVECTOR4 v, vec[3];
232     FLOAT det;
233
234     det = D3DXMatrixDeterminant(pm);
235     if ( !det ) return NULL;
236     if ( pdeterminant ) *pdeterminant = det;
237     for (i=0; i<4; i++)
238     {
239         for (j=0; j<4; j++)
240         {
241             if (j != i )
242             {
243                 a = j;
244                 if ( j > i ) a = a-1;
245                 vec[a].x = pm->u.m[j][0];
246                 vec[a].y = pm->u.m[j][1];
247                 vec[a].z = pm->u.m[j][2];
248                 vec[a].w = pm->u.m[j][3];
249             }
250         }
251     D3DXVec4Cross(&v, &vec[0], &vec[1], &vec[2]);
252     out.u.m[0][i] = pow(-1.0f, i) * v.x / det;
253     out.u.m[1][i] = pow(-1.0f, i) * v.y / det;
254     out.u.m[2][i] = pow(-1.0f, i) * v.z / det;
255     out.u.m[3][i] = pow(-1.0f, i) * v.w / det;
256    }
257
258    *pout = out;
259    return pout;
260 }
261
262 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
263 {
264     D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
265
266     D3DXVec3Subtract(&vec2, pat, peye);
267     D3DXVec3Normalize(&vec, &vec2);
268     D3DXVec3Cross(&right, pup, &vec);
269     D3DXVec3Cross(&up, &vec, &right);
270     D3DXVec3Normalize(&rightn, &right);
271     D3DXVec3Normalize(&upn, &up);
272     pout->u.m[0][0] = rightn.x;
273     pout->u.m[1][0] = rightn.y;
274     pout->u.m[2][0] = rightn.z;
275     pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
276     pout->u.m[0][1] = upn.x;
277     pout->u.m[1][1] = upn.y;
278     pout->u.m[2][1] = upn.z;
279     pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
280     pout->u.m[0][2] = vec.x;
281     pout->u.m[1][2] = vec.y;
282     pout->u.m[2][2] = vec.z;
283     pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
284     pout->u.m[0][3] = 0.0f;
285     pout->u.m[1][3] = 0.0f;
286     pout->u.m[2][3] = 0.0f;
287     pout->u.m[3][3] = 1.0f;
288     return pout;
289 }
290
291 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
292 {
293     D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
294
295     D3DXVec3Subtract(&vec2, pat, peye);
296     D3DXVec3Normalize(&vec, &vec2);
297     D3DXVec3Cross(&right, pup, &vec);
298     D3DXVec3Cross(&up, &vec, &right);
299     D3DXVec3Normalize(&rightn, &right);
300     D3DXVec3Normalize(&upn, &up);
301     pout->u.m[0][0] = -rightn.x;
302     pout->u.m[1][0] = -rightn.y;
303     pout->u.m[2][0] = -rightn.z;
304     pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
305     pout->u.m[0][1] = upn.x;
306     pout->u.m[1][1] = upn.y;
307     pout->u.m[2][1] = upn.z;
308     pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
309     pout->u.m[0][2] = -vec.x;
310     pout->u.m[1][2] = -vec.y;
311     pout->u.m[2][2] = -vec.z;
312     pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
313     pout->u.m[0][3] = 0.0f;
314     pout->u.m[1][3] = 0.0f;
315     pout->u.m[2][3] = 0.0f;
316     pout->u.m[3][3] = 1.0f;
317     return pout;
318 }
319
320 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
321 {
322     D3DXMATRIX out;
323     int i,j;
324
325     for (i=0; i<4; i++)
326     {
327         for (j=0; j<4; j++)
328         {
329             out.u.m[i][j] = pm1->u.m[i][0] * pm2->u.m[0][j] + pm1->u.m[i][1] * pm2->u.m[1][j] + pm1->u.m[i][2] * pm2->u.m[2][j] + pm1->u.m[i][3] * pm2->u.m[3][j];
330         }
331     }
332
333     *pout = out;
334     return pout;
335 }
336
337 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
338 {
339     D3DXMatrixMultiply(pout, pm1, pm2);
340     D3DXMatrixTranspose(pout, pout);
341     return pout;
342 }
343
344 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
345 {
346     D3DXMatrixIdentity(pout);
347     pout->u.m[0][0] = 2.0f / w;
348     pout->u.m[1][1] = 2.0f / h;
349     pout->u.m[2][2] = 1.0f / (zf - zn);
350     pout->u.m[3][2] = zn / (zn - zf);
351     return pout;
352 }
353
354 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
355 {
356     D3DXMatrixIdentity(pout);
357     pout->u.m[0][0] = 2.0f / (r - l);
358     pout->u.m[1][1] = 2.0f / (t - b);
359     pout->u.m[2][2] = 1.0f / (zf -zn);
360     pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
361     pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
362     pout->u.m[3][2] = zn / (zn -zf);
363     return pout;
364 }
365
366 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
367 {
368     D3DXMatrixIdentity(pout);
369     pout->u.m[0][0] = 2.0f / (r - l);
370     pout->u.m[1][1] = 2.0f / (t - b);
371     pout->u.m[2][2] = 1.0f / (zn -zf);
372     pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
373     pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
374     pout->u.m[3][2] = zn / (zn -zf);
375     return pout;
376 }
377
378 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
379 {
380     D3DXMatrixIdentity(pout);
381     pout->u.m[0][0] = 2.0f / w;
382     pout->u.m[1][1] = 2.0f / h;
383     pout->u.m[2][2] = 1.0f / (zn - zf);
384     pout->u.m[3][2] = zn / (zn - zf);
385     return pout;
386 }
387
388 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
389 {
390     D3DXMatrixIdentity(pout);
391     pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
392     pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
393     pout->u.m[2][2] = zf / (zf - zn);
394     pout->u.m[2][3] = 1.0f;
395     pout->u.m[3][2] = (zf * zn) / (zn - zf);
396     pout->u.m[3][3] = 0.0f;
397     return pout;
398 }
399
400 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
401 {
402     D3DXMatrixIdentity(pout);
403     pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
404     pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
405     pout->u.m[2][2] = zf / (zn - zf);
406     pout->u.m[2][3] = -1.0f;
407     pout->u.m[3][2] = (zf * zn) / (zn - zf);
408     pout->u.m[3][3] = 0.0f;
409     return pout;
410 }
411
412 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
413 {
414     D3DXMatrixIdentity(pout);
415     pout->u.m[0][0] = 2.0f * zn / w;
416     pout->u.m[1][1] = 2.0f * zn / h;
417     pout->u.m[2][2] = zf / (zf - zn);
418     pout->u.m[3][2] = (zn * zf) / (zn - zf);
419     pout->u.m[2][3] = 1.0f;
420     pout->u.m[3][3] = 0.0f;
421     return pout;
422 }
423
424 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
425 {
426     D3DXMatrixIdentity(pout);
427     pout->u.m[0][0] = 2.0f * zn / (r - l);
428     pout->u.m[1][1] = -2.0f * zn / (b - t);
429     pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
430     pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
431     pout->u.m[2][2] = - zf / (zn - zf);
432     pout->u.m[3][2] = (zn * zf) / (zn -zf);
433     pout->u.m[2][3] = 1.0f;
434     pout->u.m[3][3] = 0.0f;
435     return pout;
436 }
437
438 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
439 {
440     D3DXMatrixIdentity(pout);
441     pout->u.m[0][0] = 2.0f * zn / (r - l);
442     pout->u.m[1][1] = -2.0f * zn / (b - t);
443     pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
444     pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
445     pout->u.m[2][2] = zf / (zn - zf);
446     pout->u.m[3][2] = (zn * zf) / (zn -zf);
447     pout->u.m[2][3] = -1.0f;
448     pout->u.m[3][3] = 0.0f;
449     return pout;
450 }
451
452 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
453 {
454     D3DXMatrixIdentity(pout);
455     pout->u.m[0][0] = 2.0f * zn / w;
456     pout->u.m[1][1] = 2.0f * zn / h;
457     pout->u.m[2][2] = zf / (zn - zf);
458     pout->u.m[3][2] = (zn * zf) / (zn - zf);
459     pout->u.m[2][3] = -1.0f;
460     pout->u.m[3][3] = 0.0f;
461     return pout;
462 }
463
464 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, CONST D3DXPLANE *pplane)
465 {
466     D3DXPLANE Nplane;
467
468     D3DXPlaneNormalize(&Nplane, pplane);
469     D3DXMatrixIdentity(pout);
470     pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
471     pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
472     pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
473     pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
474     pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
475     pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
476     pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
477     pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
478     pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
479     pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
480     pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
481     pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
482     return pout;
483 }
484
485 D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
486 {
487     D3DXVECTOR3 v;
488
489     D3DXVec3Normalize(&v,pv);
490     D3DXMatrixIdentity(pout);
491     pout->u.m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
492     pout->u.m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
493     pout->u.m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
494     pout->u.m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
495     pout->u.m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
496     pout->u.m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
497     pout->u.m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
498     pout->u.m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
499     pout->u.m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
500     return pout;
501 }
502
503 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
504 {
505     D3DXMatrixIdentity(pout);
506     pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
507     pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
508     pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
509     pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
510     pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
511     pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
512     pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
513     pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
514     pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
515     return pout;
516 }
517
518 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
519 {
520     D3DXMatrixIdentity(pout);
521     pout->u.m[1][1] = cos(angle);
522     pout->u.m[2][2] = cos(angle);
523     pout->u.m[1][2] = sin(angle);
524     pout->u.m[2][1] = -sin(angle);
525     return pout;
526 }
527
528 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
529 {
530     D3DXMatrixIdentity(pout);
531     pout->u.m[0][0] = cos(angle);
532     pout->u.m[2][2] = cos(angle);
533     pout->u.m[0][2] = -sin(angle);
534     pout->u.m[2][0] = sin(angle);
535     return pout;
536 }
537
538 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
539 {
540     D3DXMATRIX m;
541
542     D3DXMatrixIdentity(pout);
543     D3DXMatrixRotationZ(&m, roll);
544     D3DXMatrixMultiply(pout, pout, &m);
545     D3DXMatrixRotationX(&m, pitch);
546     D3DXMatrixMultiply(pout, pout, &m);
547     D3DXMatrixRotationY(&m, yaw);
548     D3DXMatrixMultiply(pout, pout, &m);
549     return pout;
550 }
551 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
552 {
553     D3DXMatrixIdentity(pout);
554     pout->u.m[0][0] = cos(angle);
555     pout->u.m[1][1] = cos(angle);
556     pout->u.m[0][1] = sin(angle);
557     pout->u.m[1][0] = -sin(angle);
558     return pout;
559 }
560
561 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
562 {
563     D3DXMatrixIdentity(pout);
564     pout->u.m[0][0] = sx;
565     pout->u.m[1][1] = sy;
566     pout->u.m[2][2] = sz;
567     return pout;
568 }
569
570 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, CONST D3DXVECTOR4 *plight, CONST D3DXPLANE *pplane)
571 {
572     D3DXPLANE Nplane;
573     FLOAT dot;
574
575     D3DXPlaneNormalize(&Nplane, pplane);
576     dot = D3DXPlaneDot(&Nplane, plight);
577     pout->u.m[0][0] = dot - Nplane.a * plight->x;
578     pout->u.m[0][1] = -Nplane.a * plight->y;
579     pout->u.m[0][2] = -Nplane.a * plight->z;
580     pout->u.m[0][3] = -Nplane.a * plight->w;
581     pout->u.m[1][0] = -Nplane.b * plight->x;
582     pout->u.m[1][1] = dot - Nplane.b * plight->y;
583     pout->u.m[1][2] = -Nplane.b * plight->z;
584     pout->u.m[1][3] = -Nplane.b * plight->w;
585     pout->u.m[2][0] = -Nplane.c * plight->x;
586     pout->u.m[2][1] = -Nplane.c * plight->y;
587     pout->u.m[2][2] = dot - Nplane.c * plight->z;
588     pout->u.m[2][3] = -Nplane.c * plight->w;
589     pout->u.m[3][0] = -Nplane.d * plight->x;
590     pout->u.m[3][1] = -Nplane.d * plight->y;
591     pout->u.m[3][2] = -Nplane.d * plight->z;
592     pout->u.m[3][3] = dot - Nplane.d * plight->w;
593     return pout;
594 }
595
596 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pscalingcenter, CONST D3DXQUATERNION *pscalingrotation, CONST D3DXVECTOR3 *pscaling, CONST D3DXVECTOR3 *protationcenter, CONST D3DXQUATERNION *protation, CONST D3DXVECTOR3 *ptranslation)
597 {
598     D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
599     D3DXQUATERNION prc;
600     D3DXVECTOR3 psc, pt;
601
602     if ( !pscalingcenter )
603     {
604         psc.x = 0.0f;
605         psc.y = 0.0f;
606         psc.z = 0.0f;
607     }
608     else
609     {
610         psc.x = pscalingcenter->x;
611         psc.y = pscalingcenter->y;
612         psc.z = pscalingcenter->z;
613     }
614
615     if ( !protationcenter )
616     {
617         prc.x = 0.0f;
618         prc.y = 0.0f;
619         prc.z = 0.0f;
620     }
621     else
622     {
623         prc.x = protationcenter->x;
624         prc.y = protationcenter->y;
625         prc.z = protationcenter->z;
626     }
627
628     if ( !ptranslation )
629     {
630         pt.x = 0.0f;
631         pt.y = 0.0f;
632         pt.z = 0.0f;
633     }
634     else
635     {
636         pt.x = ptranslation->x;
637         pt.y = ptranslation->y;
638         pt.z = ptranslation->z;
639     }
640
641     D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
642
643     if ( !pscalingrotation )
644     {
645         D3DXMatrixIdentity(&m2);
646         D3DXMatrixIdentity(&m4);
647     }
648     else
649     {
650         D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
651         D3DXMatrixInverse(&m2, NULL, &m4);
652     }
653
654     if ( !pscaling ) D3DXMatrixIdentity(&m3);
655     else D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
656
657     if ( !protation ) D3DXMatrixIdentity(&m6);
658     else D3DXMatrixRotationQuaternion(&m6, protation);
659
660     D3DXMatrixTranslation(&m5, psc.x - prc.x,  psc.y - prc.y,  psc.z - prc.z);
661     D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
662     D3DXMatrixMultiply(&m1, &m1, &m2);
663     D3DXMatrixMultiply(&m1, &m1, &m3);
664     D3DXMatrixMultiply(&m1, &m1, &m4);
665     D3DXMatrixMultiply(&m1, &m1, &m5);
666     D3DXMatrixMultiply(&m1, &m1, &m6);
667     D3DXMatrixMultiply(pout, &m1, &m7);
668     return pout;
669 }
670 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(D3DXMATRIX *pout, CONST D3DXVECTOR2 *pscalingcenter, FLOAT scalingrotation, CONST D3DXVECTOR2 *pscaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
671 {
672     D3DXQUATERNION rot, sca_rot;
673     D3DXVECTOR3 rot_center, sca, sca_center, trans;
674
675     if ( pscalingcenter )
676     {
677         sca_center.x=pscalingcenter->x;
678         sca_center.y=pscalingcenter->y;
679         sca_center.z=0.0f;
680     }
681     else
682     {
683         sca_center.x=0.0f;
684         sca_center.y=0.0f;
685         sca_center.z=0.0f;
686     }
687
688     if ( pscaling )
689     {
690         sca.x=pscaling->x;
691         sca.y=pscaling->y;
692         sca.z=1.0f;
693     }
694     else
695     {
696         sca.x=1.0f;
697         sca.y=1.0f;
698         sca.z=1.0f;
699     }
700
701     if ( protationcenter )
702     {
703         rot_center.x=protationcenter->x;
704         rot_center.y=protationcenter->y;
705         rot_center.z=0.0f;
706     }
707     else
708     {
709         rot_center.x=0.0f;
710         rot_center.y=0.0f;
711         rot_center.z=0.0f;
712     }
713
714     if ( ptranslation )
715     {
716         trans.x=ptranslation->x;
717         trans.y=ptranslation->y;
718         trans.z=0.0f;
719     }
720     else
721     {
722         trans.x=0.0f;
723         trans.y=0.0f;
724         trans.z=0.0f;
725     }
726
727     rot.w=cos(rotation/2.0f);
728     rot.x=0.0f;
729     rot.y=0.0f;
730     rot.z=sin(rotation/2.0f);
731
732     sca_rot.w=cos(scalingrotation/2.0f);
733     sca_rot.x=0.0f;
734     sca_rot.y=0.0f;
735     sca_rot.z=sin(scalingrotation/2.0f);
736
737     D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
738
739     return pout;
740 }
741
742 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
743 {
744     D3DXMatrixIdentity(pout);
745     pout->u.m[3][0] = x;
746     pout->u.m[3][1] = y;
747     pout->u.m[3][2] = z;
748     return pout;
749 }
750
751 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
752 {
753     CONST D3DXMATRIX m = *pm;
754     int i,j;
755
756     for (i=0; i<4; i++)
757         for (j=0; j<4; j++) pout->u.m[i][j] = m.u.m[j][i];
758
759     return pout;
760 }
761
762 /*_________________D3DXMatrixStack____________________*/
763
764 static const unsigned int INITIAL_STACK_SIZE = 32;
765
766 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, LPD3DXMATRIXSTACK* ppstack)
767 {
768     ID3DXMatrixStackImpl* object;
769
770     TRACE("flags %#x, ppstack %p\n", flags, ppstack);
771
772     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXMatrixStackImpl));
773     if ( object == NULL )
774     {
775      *ppstack = NULL;
776      return E_OUTOFMEMORY;
777     }
778     object->ID3DXMatrixStack_iface.lpVtbl = &ID3DXMatrixStack_Vtbl;
779     object->ref = 1;
780
781     object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(D3DXMATRIX));
782     if (!object->stack)
783     {
784         HeapFree(GetProcessHeap(), 0, object);
785         *ppstack = NULL;
786         return E_OUTOFMEMORY;
787     }
788
789     object->current = 0;
790     object->stack_size = INITIAL_STACK_SIZE;
791     D3DXMatrixIdentity(&object->stack[0]);
792
793     TRACE("Created matrix stack %p\n", object);
794
795     *ppstack = &object->ID3DXMatrixStack_iface;
796     return D3D_OK;
797 }
798
799 static inline ID3DXMatrixStackImpl *impl_from_ID3DXMatrixStack(ID3DXMatrixStack *iface)
800 {
801   return CONTAINING_RECORD(iface, ID3DXMatrixStackImpl, ID3DXMatrixStack_iface);
802 }
803
804 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **out)
805 {
806     TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
807
808     if (IsEqualGUID(riid, &IID_ID3DXMatrixStack)
809             || IsEqualGUID(riid, &IID_IUnknown))
810     {
811         ID3DXMatrixStack_AddRef(iface);
812         *out = iface;
813         return S_OK;
814     }
815
816     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
817
818     *out = NULL;
819     return E_NOINTERFACE;
820 }
821
822 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
823 {
824     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
825     ULONG ref = InterlockedIncrement(&This->ref);
826     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
827     return ref;
828 }
829
830 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack* iface)
831 {
832     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
833     ULONG ref = InterlockedDecrement(&This->ref);
834     if (!ref)
835     {
836         HeapFree(GetProcessHeap(), 0, This->stack);
837         HeapFree(GetProcessHeap(), 0, This);
838     }
839     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
840     return ref;
841 }
842
843 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
844 {
845     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
846
847     TRACE("iface %p\n", iface);
848
849     return &This->stack[This->current];
850 }
851
852 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
853 {
854     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
855
856     TRACE("iface %p\n", iface);
857
858     D3DXMatrixIdentity(&This->stack[This->current]);
859
860     return D3D_OK;
861 }
862
863 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
864 {
865     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
866
867     TRACE("iface %p\n", iface);
868
869     This->stack[This->current] = *pm;
870
871     return D3D_OK;
872 }
873
874 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
875 {
876     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
877
878     TRACE("iface %p\n", iface);
879
880     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
881
882     return D3D_OK;
883 }
884
885 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
886 {
887     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
888
889     TRACE("iface %p\n", iface);
890
891     D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
892
893     return D3D_OK;
894 }
895
896 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
897 {
898     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
899
900     TRACE("iface %p\n", iface);
901
902     /* Popping the last element on the stack returns D3D_OK, but does nothing. */
903     if (!This->current) return D3D_OK;
904
905     if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
906     {
907         unsigned int new_size;
908         D3DXMATRIX *new_stack;
909
910         new_size = This->stack_size / 2;
911         new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(D3DXMATRIX));
912         if (new_stack)
913         {
914             This->stack_size = new_size;
915             This->stack = new_stack;
916         }
917     }
918
919     --This->current;
920
921     return D3D_OK;
922 }
923
924 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
925 {
926     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
927
928     TRACE("iface %p\n", iface);
929
930     if (This->current == This->stack_size - 1)
931     {
932         unsigned int new_size;
933         D3DXMATRIX *new_stack;
934
935         if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
936
937         new_size = This->stack_size * 2;
938         new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(D3DXMATRIX));
939         if (!new_stack) return E_OUTOFMEMORY;
940
941         This->stack_size = new_size;
942         This->stack = new_stack;
943     }
944
945     ++This->current;
946     This->stack[This->current] = This->stack[This->current - 1];
947
948     return D3D_OK;
949 }
950
951 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
952 {
953     D3DXMATRIX temp;
954     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
955
956     TRACE("iface %p\n", iface);
957
958     D3DXMatrixRotationAxis(&temp, pv, angle);
959     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
960
961     return D3D_OK;
962 }
963
964 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
965 {
966     D3DXMATRIX temp;
967     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
968
969     TRACE("iface %p\n", iface);
970
971     D3DXMatrixRotationAxis(&temp, pv, angle);
972     D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
973
974     return D3D_OK;
975 }
976
977 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
978 {
979     D3DXMATRIX temp;
980     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
981
982     TRACE("iface %p\n", iface);
983
984     D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
985     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
986
987     return D3D_OK;
988 }
989
990 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
991 {
992     D3DXMATRIX temp;
993     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
994
995     TRACE("iface %p\n", iface);
996
997     D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
998     D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
999
1000     return D3D_OK;
1001 }
1002
1003 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1004 {
1005     D3DXMATRIX temp;
1006     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1007
1008     TRACE("iface %p\n", iface);
1009
1010     D3DXMatrixScaling(&temp, x, y, z);
1011     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1012
1013     return D3D_OK;
1014 }
1015
1016 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1017 {
1018     D3DXMATRIX temp;
1019     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1020
1021     TRACE("iface %p\n", iface);
1022
1023     D3DXMatrixScaling(&temp, x, y, z);
1024     D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1025
1026     return D3D_OK;
1027 }
1028
1029 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1030 {
1031     D3DXMATRIX temp;
1032     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1033
1034     TRACE("iface %p\n", iface);
1035
1036     D3DXMatrixTranslation(&temp, x, y, z);
1037     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1038
1039     return D3D_OK;
1040 }
1041
1042 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1043 {
1044     D3DXMATRIX temp;
1045     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1046
1047     TRACE("iface %p\n", iface);
1048
1049     D3DXMatrixTranslation(&temp, x, y, z);
1050     D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
1051
1052     return D3D_OK;
1053 }
1054
1055 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
1056 {
1057     ID3DXMatrixStackImpl_QueryInterface,
1058     ID3DXMatrixStackImpl_AddRef,
1059     ID3DXMatrixStackImpl_Release,
1060     ID3DXMatrixStackImpl_Pop,
1061     ID3DXMatrixStackImpl_Push,
1062     ID3DXMatrixStackImpl_LoadIdentity,
1063     ID3DXMatrixStackImpl_LoadMatrix,
1064     ID3DXMatrixStackImpl_MultMatrix,
1065     ID3DXMatrixStackImpl_MultMatrixLocal,
1066     ID3DXMatrixStackImpl_RotateAxis,
1067     ID3DXMatrixStackImpl_RotateAxisLocal,
1068     ID3DXMatrixStackImpl_RotateYawPitchRoll,
1069     ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
1070     ID3DXMatrixStackImpl_Scale,
1071     ID3DXMatrixStackImpl_ScaleLocal,
1072     ID3DXMatrixStackImpl_Translate,
1073     ID3DXMatrixStackImpl_TranslateLocal,
1074     ID3DXMatrixStackImpl_GetTop
1075 };
1076
1077 /*_________________D3DXPLANE________________*/
1078
1079 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, CONST D3DXVECTOR3 *pvpoint, CONST D3DXVECTOR3 *pvnormal)
1080 {
1081     pout->a = pvnormal->x;
1082     pout->b = pvnormal->y;
1083     pout->c = pvnormal->z;
1084     pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
1085     return pout;
1086 }
1087
1088 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3)
1089 {
1090     D3DXVECTOR3 edge1, edge2, normal, Nnormal;
1091
1092     edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
1093     edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
1094     D3DXVec3Subtract(&edge1, pv2, pv1);
1095     D3DXVec3Subtract(&edge2, pv3, pv1);
1096     D3DXVec3Cross(&normal, &edge1, &edge2);
1097     D3DXVec3Normalize(&Nnormal, &normal);
1098     D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
1099     return pout;
1100 }
1101
1102 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, CONST D3DXPLANE *pp, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
1103 {
1104     D3DXVECTOR3 direction, normal;
1105     FLOAT dot, temp;
1106
1107     normal.x = pp->a;
1108     normal.y = pp->b;
1109     normal.z = pp->c;
1110     direction.x = pv2->x - pv1->x;
1111     direction.y = pv2->y - pv1->y;
1112     direction.z = pv2->z - pv1->z;
1113     dot = D3DXVec3Dot(&normal, &direction);
1114     if ( !dot ) return NULL;
1115     temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
1116     pout->x = pv1->x - temp * direction.x;
1117     pout->y = pv1->y - temp * direction.y;
1118     pout->z = pv1->z - temp * direction.z;
1119     return pout;
1120 }
1121
1122 D3DXPLANE* WINAPI D3DXPlaneNormalize(D3DXPLANE *pout, CONST D3DXPLANE *pp)
1123 {
1124     D3DXPLANE out;
1125     FLOAT norm;
1126
1127     norm = sqrt(pp->a * pp->a + pp->b * pp->b + pp->c * pp->c);
1128     if ( norm )
1129     {
1130      out.a = pp->a / norm;
1131      out.b = pp->b / norm;
1132      out.c = pp->c / norm;
1133      out.d = pp->d / norm;
1134     }
1135     else
1136     {
1137      out.a = 0.0f;
1138      out.b = 0.0f;
1139      out.c = 0.0f;
1140      out.d = 0.0f;
1141     }
1142     *pout = out;
1143     return pout;
1144 }
1145
1146 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, CONST D3DXPLANE *pplane, CONST D3DXMATRIX *pm)
1147 {
1148     CONST D3DXPLANE plane = *pplane;
1149     pout->a = pm->u.m[0][0] * plane.a + pm->u.m[1][0] * plane.b + pm->u.m[2][0] * plane.c + pm->u.m[3][0] * plane.d;
1150     pout->b = pm->u.m[0][1] * plane.a + pm->u.m[1][1] * plane.b + pm->u.m[2][1] * plane.c + pm->u.m[3][1] * plane.d;
1151     pout->c = pm->u.m[0][2] * plane.a + pm->u.m[1][2] * plane.b + pm->u.m[2][2] * plane.c + pm->u.m[3][2] * plane.d;
1152     pout->d = pm->u.m[0][3] * plane.a + pm->u.m[1][3] * plane.b + pm->u.m[2][3] * plane.c + pm->u.m[3][3] * plane.d;
1153     return pout;
1154 }
1155
1156 D3DXPLANE* WINAPI D3DXPlaneTransformArray(D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1157 {
1158     UINT i;
1159
1160     for (i = 0; i < elements; ++i) {
1161         D3DXPlaneTransform(
1162             (D3DXPLANE*)((char*)out + outstride * i),
1163             (CONST D3DXPLANE*)((const char*)in + instride * i),
1164             matrix);
1165     }
1166     return out;
1167 }
1168
1169 /*_________________D3DXQUATERNION________________*/
1170
1171 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
1172 {
1173     D3DXQUATERNION temp1, temp2;
1174     D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
1175     return pout;
1176 }
1177
1178 D3DXQUATERNION* WINAPI D3DXQuaternionExp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1179 {
1180     FLOAT norm;
1181
1182     norm = sqrt(pq->x * pq->x + pq->y * pq->y + pq->z * pq->z);
1183     if (norm )
1184     {
1185      pout->x = sin(norm) * pq->x / norm;
1186      pout->y = sin(norm) * pq->y / norm;
1187      pout->z = sin(norm) * pq->z / norm;
1188      pout->w = cos(norm);
1189     }
1190     else
1191     {
1192      pout->x = 0.0f;
1193      pout->y = 0.0f;
1194      pout->z = 0.0f;
1195      pout->w = 1.0f;
1196     }
1197     return pout;
1198 }
1199
1200 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1201 {
1202     D3DXQUATERNION out;
1203     FLOAT norm;
1204
1205     norm = D3DXQuaternionLengthSq(pq);
1206
1207     out.x = -pq->x / norm;
1208     out.y = -pq->y / norm;
1209     out.z = -pq->z / norm;
1210     out.w = pq->w / norm;
1211
1212     *pout =out;
1213     return pout;
1214 }
1215
1216 D3DXQUATERNION* WINAPI D3DXQuaternionLn(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1217 {
1218     FLOAT t;
1219
1220     TRACE("(%p, %p)\n", pout, pq);
1221
1222     if ( (pq->w >= 1.0f) || (pq->w == -1.0f) )
1223         t = 1.0f;
1224     else
1225         t = acos( pq->w ) / sqrt( 1.0f - pq->w * pq->w );
1226
1227     pout->x = t * pq->x;
1228     pout->y = t * pq->y;
1229     pout->z = t * pq->z;
1230     pout->w = 0.0f;
1231
1232     return pout;
1233 }
1234
1235 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
1236 {
1237     D3DXQUATERNION out;
1238     out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1239     out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1240     out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1241     out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1242     *pout = out;
1243     return pout;
1244 }
1245
1246 D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1247 {
1248     D3DXQUATERNION out;
1249     FLOAT norm;
1250
1251     norm = D3DXQuaternionLength(pq);
1252
1253     out.x = pq->x / norm;
1254     out.y = pq->y / norm;
1255     out.z = pq->z / norm;
1256     out.w = pq->w / norm;
1257
1258     *pout=out;
1259
1260     return pout;
1261 }
1262
1263 D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
1264 {
1265     D3DXVECTOR3 temp;
1266
1267     D3DXVec3Normalize(&temp, pv);
1268     pout->x = sin( angle / 2.0f ) * temp.x;
1269     pout->y = sin( angle / 2.0f ) * temp.y;
1270     pout->z = sin( angle / 2.0f ) * temp.z;
1271     pout->w = cos( angle / 2.0f );
1272     return pout;
1273 }
1274
1275 D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, CONST D3DXMATRIX *pm)
1276 {
1277     int i, maxi;
1278     FLOAT maxdiag, S, trace;
1279
1280     trace = pm->u.m[0][0] + pm->u.m[1][1] + pm->u.m[2][2] + 1.0f;
1281     if ( trace > 1.0f)
1282     {
1283      pout->x = ( pm->u.m[1][2] - pm->u.m[2][1] ) / ( 2.0f * sqrt(trace) );
1284      pout->y = ( pm->u.m[2][0] - pm->u.m[0][2] ) / ( 2.0f * sqrt(trace) );
1285      pout->z = ( pm->u.m[0][1] - pm->u.m[1][0] ) / ( 2.0f * sqrt(trace) );
1286      pout->w = sqrt(trace) / 2.0f;
1287      return pout;
1288      }
1289     maxi = 0;
1290     maxdiag = pm->u.m[0][0];
1291     for (i=1; i<3; i++)
1292     {
1293      if ( pm->u.m[i][i] > maxdiag )
1294      {
1295       maxi = i;
1296       maxdiag = pm->u.m[i][i];
1297      }
1298     }
1299     switch( maxi )
1300     {
1301      case 0:
1302        S = 2.0f * sqrt(1.0f + pm->u.m[0][0] - pm->u.m[1][1] - pm->u.m[2][2]);
1303        pout->x = 0.25f * S;
1304        pout->y = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1305        pout->z = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1306        pout->w = ( pm->u.m[1][2] - pm->u.m[2][1] ) / S;
1307      break;
1308      case 1:
1309        S = 2.0f * sqrt(1.0f + pm->u.m[1][1] - pm->u.m[0][0] - pm->u.m[2][2]);
1310        pout->x = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1311        pout->y = 0.25f * S;
1312        pout->z = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1313        pout->w = ( pm->u.m[2][0] - pm->u.m[0][2] ) / S;
1314      break;
1315      case 2:
1316        S = 2.0f * sqrt(1.0f + pm->u.m[2][2] - pm->u.m[0][0] - pm->u.m[1][1]);
1317        pout->x = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1318        pout->y = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1319        pout->z = 0.25f * S;
1320        pout->w = ( pm->u.m[0][1] - pm->u.m[1][0] ) / S;
1321      break;
1322     }
1323     return pout;
1324 }
1325
1326 D3DXQUATERNION* WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
1327 {
1328     pout->x = sin( yaw / 2.0f) * cos(pitch / 2.0f) * sin(roll / 2.0f) + cos(yaw / 2.0f) * sin(pitch / 2.0f) * cos(roll / 2.0f);
1329     pout->y = sin( yaw / 2.0f) * cos(pitch / 2.0f) * cos(roll / 2.0f) - cos(yaw / 2.0f) * sin(pitch / 2.0f) * sin(roll / 2.0f);
1330     pout->z = cos(yaw / 2.0f) * cos(pitch / 2.0f) * sin(roll / 2.0f) - sin( yaw / 2.0f) * sin(pitch / 2.0f) * cos(roll / 2.0f);
1331     pout->w = cos( yaw / 2.0f) * cos(pitch / 2.0f) * cos(roll / 2.0f) + sin(yaw / 2.0f) * sin(pitch / 2.0f) * sin(roll / 2.0f);
1332     return pout;
1333 }
1334
1335 D3DXQUATERNION* WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, FLOAT t)
1336 {
1337     FLOAT dot, epsilon, temp, theta, u;
1338
1339     epsilon = 1.0f;
1340     temp = 1.0f - t;
1341     u = t;
1342     dot = D3DXQuaternionDot(pq1, pq2);
1343     if ( dot < 0.0f )
1344     {
1345         epsilon = -1.0f;
1346         dot = -dot;
1347     }
1348     if( 1.0f - dot > 0.001f )
1349     {
1350         theta = acos(dot);
1351         temp  = sin(theta * temp) / sin(theta);
1352         u = sin(theta * u) / sin(theta);
1353     }
1354     pout->x = temp * pq1->x + epsilon * u * pq2->x;
1355     pout->y = temp * pq1->y + epsilon * u * pq2->y;
1356     pout->z = temp * pq1->z + epsilon * u * pq2->z;
1357     pout->w = temp * pq1->w + epsilon * u * pq2->w;
1358     return pout;
1359 }
1360
1361 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, CONST D3DXQUATERNION *pq4, FLOAT t)
1362 {
1363     D3DXQUATERNION temp1, temp2;
1364
1365     D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1366     return pout;
1367 }
1368
1369 static D3DXQUATERNION add_diff(CONST D3DXQUATERNION *q1, CONST D3DXQUATERNION *q2, CONST FLOAT add)
1370 {
1371     D3DXQUATERNION temp;
1372
1373     temp.x = q1->x + add * q2->x;
1374     temp.y = q1->y + add * q2->y;
1375     temp.z = q1->z + add * q2->z;
1376     temp.w = q1->w + add * q2->w;
1377
1378     return temp;
1379 }
1380
1381 void WINAPI D3DXQuaternionSquadSetup(D3DXQUATERNION *paout, D3DXQUATERNION *pbout, D3DXQUATERNION *pcout, CONST D3DXQUATERNION *pq0, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3)
1382 {
1383     D3DXQUATERNION q, temp1, temp2, temp3, zero;
1384
1385     TRACE("(%p, %p, %p, %p, %p, %p, %p)\n", paout, pbout, pcout, pq0, pq1, pq2, pq3);
1386
1387     zero.x = 0.0f;
1388     zero.y = 0.0f;
1389     zero.z = 0.0f;
1390     zero.w = 0.0f;
1391
1392     if ( D3DXQuaternionDot(pq0, pq1) <  0.0f )
1393         temp2 = add_diff(&zero, pq0, -1.0f);
1394     else
1395         temp2 = *pq0;
1396
1397     if ( D3DXQuaternionDot(pq1, pq2) < 0.0f )
1398         *pcout = add_diff(&zero, pq2, -1.0f);
1399     else
1400         *pcout = *pq2;
1401
1402     if ( D3DXQuaternionDot(pcout, pq3) < 0.0f )
1403         temp3 = add_diff(&zero, pq3, -1.0f);
1404     else
1405         temp3 = *pq3;
1406
1407     D3DXQuaternionInverse(&temp1, pq1);
1408     D3DXQuaternionMultiply(&temp2, &temp1, &temp2);
1409     D3DXQuaternionLn(&temp2, &temp2);
1410     D3DXQuaternionMultiply(&q, &temp1, pcout);
1411     D3DXQuaternionLn(&q, &q);
1412     temp1 = add_diff(&temp2, &q, 1.0f);
1413     temp1.x *= -0.25f;
1414     temp1.y *= -0.25f;
1415     temp1.z *= -0.25f;
1416     temp1.w *= -0.25f;
1417     D3DXQuaternionExp(&temp1, &temp1);
1418     D3DXQuaternionMultiply(paout, pq1, &temp1);
1419
1420     D3DXQuaternionInverse(&temp1, pcout);
1421     D3DXQuaternionMultiply(&temp2, &temp1, pq1);
1422     D3DXQuaternionLn(&temp2, &temp2);
1423     D3DXQuaternionMultiply(&q, &temp1, &temp3);
1424     D3DXQuaternionLn(&q, &q);
1425     temp1 = add_diff(&temp2, &q, 1.0f);
1426     temp1.x *= -0.25f;
1427     temp1.y *= -0.25f;
1428     temp1.z *= -0.25f;
1429     temp1.w *= -0.25f;
1430     D3DXQuaternionExp(&temp1, &temp1);
1431     D3DXQuaternionMultiply(pbout, pcout, &temp1);
1432
1433     return;
1434 }
1435
1436 void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1437 {
1438     paxis->x = pq->x;
1439     paxis->y = pq->y;
1440     paxis->z = pq->z;
1441     *pangle = 2.0f * acos(pq->w);
1442 }
1443
1444 /*_________________D3DXVec2_____________________*/
1445
1446 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1447 {
1448     pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1449     pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1450     return pout;
1451 }
1452
1453 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
1454 {
1455     pout->x = 0.5f * (2.0f * pv1->x + (pv2->x - pv0->x) *s + (2.0f *pv0->x - 5.0f * pv1->x + 4.0f * pv2->x - pv3->x) * s * s + (pv3->x -3.0f * pv2->x + 3.0f * pv1->x - pv0->x) * s * s * s);
1456     pout->y = 0.5f * (2.0f * pv1->y + (pv2->y - pv0->y) *s + (2.0f *pv0->y - 5.0f * pv1->y + 4.0f * pv2->y - pv3->y) * s * s + (pv3->y -3.0f * pv2->y + 3.0f * pv1->y - pv0->y) * s * s * s);
1457     return pout;
1458 }
1459
1460 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
1461 {
1462     FLOAT h1, h2, h3, h4;
1463
1464     h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1465     h2 = s * s * s - 2.0f * s * s + s;
1466     h3 = -2.0f * s * s * s + 3.0f * s * s;
1467     h4 = s * s * s - s * s;
1468
1469     pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1470     pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1471     return pout;
1472 }
1473
1474 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
1475 {
1476     D3DXVECTOR2 out;
1477     FLOAT norm;
1478
1479     norm = D3DXVec2Length(pv);
1480     if ( !norm )
1481     {
1482      out.x = 0.0f;
1483      out.y = 0.0f;
1484     }
1485     else
1486     {
1487      out.x = pv->x / norm;
1488      out.y = pv->y / norm;
1489     }
1490     *pout=out;
1491     return pout;
1492 }
1493
1494 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1495 {
1496     pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y  + pm->u.m[3][0];
1497     pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y  + pm->u.m[3][1];
1498     pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y  + pm->u.m[3][2];
1499     pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y  + pm->u.m[3][3];
1500     return pout;
1501 }
1502
1503 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1504 {
1505     UINT i;
1506
1507     for (i = 0; i < elements; ++i) {
1508         D3DXVec2Transform(
1509             (D3DXVECTOR4*)((char*)out + outstride * i),
1510             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1511             matrix);
1512     }
1513     return out;
1514 }
1515
1516 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1517 {
1518     D3DXVECTOR2 v;
1519     FLOAT norm;
1520
1521     v = *pv;
1522     norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1523
1524     pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1525     pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1526
1527     return pout;
1528 }
1529
1530 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1531 {
1532     UINT i;
1533
1534     for (i = 0; i < elements; ++i) {
1535         D3DXVec2TransformCoord(
1536             (D3DXVECTOR2*)((char*)out + outstride * i),
1537             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1538             matrix);
1539     }
1540     return out;
1541 }
1542
1543 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1544 {
1545     CONST D3DXVECTOR2 v = *pv;
1546     pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1547     pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1548     return pout;
1549 }
1550
1551 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride, CONST D3DXMATRIX *matrix, UINT elements)
1552 {
1553     UINT i;
1554
1555     for (i = 0; i < elements; ++i) {
1556         D3DXVec2TransformNormal(
1557             (D3DXVECTOR2*)((char*)out + outstride * i),
1558             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1559             matrix);
1560     }
1561     return out;
1562 }
1563
1564 /*_________________D3DXVec3_____________________*/
1565
1566 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1567 {
1568     pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1569     pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1570     pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1571     return pout;
1572 }
1573
1574 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
1575 {
1576     pout->x = 0.5f * (2.0f * pv1->x + (pv2->x - pv0->x) *s + (2.0f *pv0->x - 5.0f * pv1->x + 4.0f * pv2->x - pv3->x) * s * s + (pv3->x -3.0f * pv2->x + 3.0f * pv1->x - pv0->x) * s * s * s);
1577     pout->y = 0.5f * (2.0f * pv1->y + (pv2->y - pv0->y) *s + (2.0f *pv0->y - 5.0f * pv1->y + 4.0f * pv2->y - pv3->y) * s * s + (pv3->y -3.0f * pv2->y + 3.0f * pv1->y - pv0->y) * s * s * s);
1578     pout->z = 0.5f * (2.0f * pv1->z + (pv2->z - pv0->z) *s + (2.0f *pv0->z - 5.0f * pv1->z + 4.0f * pv2->z - pv3->z) * s * s + (pv3->z -3.0f * pv2->z + 3.0f * pv1->z - pv0->z) * s * s * s);
1579     return pout;
1580 }
1581
1582 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
1583 {
1584     FLOAT h1, h2, h3, h4;
1585
1586     h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1587     h2 = s * s * s - 2.0f * s * s + s;
1588     h3 = -2.0f * s * s * s + 3.0f * s * s;
1589     h4 = s * s * s - s * s;
1590
1591     pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1592     pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1593     pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1594     return pout;
1595 }
1596
1597 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
1598 {
1599     D3DXVECTOR3 out;
1600     FLOAT norm;
1601
1602     norm = D3DXVec3Length(pv);
1603     if ( !norm )
1604     {
1605      out.x = 0.0f;
1606      out.y = 0.0f;
1607      out.z = 0.0f;
1608     }
1609     else
1610     {
1611      out.x = pv->x / norm;
1612      out.y = pv->y / norm;
1613      out.z = pv->z / norm;
1614     }
1615     *pout = out;
1616     return pout;
1617 }
1618
1619 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1620 {
1621     D3DXMATRIX m;
1622     D3DXVECTOR3 out;
1623
1624     D3DXMatrixMultiply(&m, pworld, pview);
1625     D3DXMatrixMultiply(&m, &m, pprojection);
1626     D3DXVec3TransformCoord(&out, pv, &m);
1627     out.x = pviewport->X +  ( 1.0f + out.x ) * pviewport->Width / 2.0f;
1628     out.y = pviewport->Y +  ( 1.0f - out.y ) * pviewport->Height / 2.0f;
1629     out.z = pviewport->MinZ + out.z * ( pviewport->MaxZ - pviewport->MinZ );
1630     *pout = out;
1631     return pout;
1632 }
1633
1634 D3DXVECTOR3* WINAPI D3DXVec3ProjectArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection, CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
1635 {
1636     UINT i;
1637
1638     for (i = 0; i < elements; ++i) {
1639         D3DXVec3Project(
1640             (D3DXVECTOR3*)((char*)out + outstride * i),
1641             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1642             viewport, projection, view, world);
1643     }
1644     return out;
1645 }
1646
1647 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1648 {
1649     pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0];
1650     pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1];
1651     pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2];
1652     pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] * pv->z + pm->u.m[3][3];
1653     return pout;
1654 }
1655
1656 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1657 {
1658     UINT i;
1659
1660     for (i = 0; i < elements; ++i) {
1661         D3DXVec3Transform(
1662             (D3DXVECTOR4*)((char*)out + outstride * i),
1663             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1664             matrix);
1665     }
1666     return out;
1667 }
1668
1669 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1670 {
1671     D3DXVECTOR3 out;
1672     FLOAT norm;
1673
1674     norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] *pv->z + pm->u.m[3][3];
1675
1676     out.x = (pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0]) / norm;
1677     out.y = (pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1]) / norm;
1678     out.z = (pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2]) / norm;
1679
1680     *pout = out;
1681
1682     return pout;
1683 }
1684
1685 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1686 {
1687     UINT i;
1688
1689     for (i = 0; i < elements; ++i) {
1690         D3DXVec3TransformCoord(
1691             (D3DXVECTOR3*)((char*)out + outstride * i),
1692             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1693             matrix);
1694     }
1695     return out;
1696 }
1697
1698 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1699 {
1700     CONST D3DXVECTOR3 v = *pv;
1701     pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1702     pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1703     pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1704     return pout;
1705
1706 }
1707
1708 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1709 {
1710     UINT i;
1711
1712     for (i = 0; i < elements; ++i) {
1713         D3DXVec3TransformNormal(
1714             (D3DXVECTOR3*)((char*)out + outstride * i),
1715             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1716             matrix);
1717     }
1718     return out;
1719 }
1720
1721 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1722 {
1723     D3DXMATRIX m;
1724     D3DXVECTOR3 out;
1725
1726     if (pworld) {
1727         D3DXMatrixMultiply(&m, pworld, pview);
1728         D3DXMatrixMultiply(&m, &m, pprojection);
1729     } else {
1730         D3DXMatrixMultiply(&m, pview, pprojection);
1731     }
1732     D3DXMatrixInverse(&m, NULL, &m);
1733     out.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f;
1734     out.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height;
1735     out.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1736     D3DXVec3TransformCoord(&out, &out, &m);
1737     *pout = out;
1738     return pout;
1739 }
1740
1741 D3DXVECTOR3* WINAPI D3DXVec3UnprojectArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection, CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
1742 {
1743     UINT i;
1744
1745     for (i = 0; i < elements; ++i) {
1746         D3DXVec3Unproject(
1747             (D3DXVECTOR3*)((char*)out + outstride * i),
1748             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1749             viewport, projection, view, world);
1750     }
1751     return out;
1752 }
1753
1754 /*_________________D3DXVec4_____________________*/
1755
1756 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
1757 {
1758     pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1759     pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1760     pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1761     pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
1762     return pout;
1763 }
1764
1765 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
1766 {
1767     pout->x = 0.5f * (2.0f * pv1->x + (pv2->x - pv0->x) *s + (2.0f *pv0->x - 5.0f * pv1->x + 4.0f * pv2->x - pv3->x) * s * s + (pv3->x -3.0f * pv2->x + 3.0f * pv1->x - pv0->x) * s * s * s);
1768     pout->y = 0.5f * (2.0f * pv1->y + (pv2->y - pv0->y) *s + (2.0f *pv0->y - 5.0f * pv1->y + 4.0f * pv2->y - pv3->y) * s * s + (pv3->y -3.0f * pv2->y + 3.0f * pv1->y - pv0->y) * s * s * s);
1769     pout->z = 0.5f * (2.0f * pv1->z + (pv2->z - pv0->z) *s + (2.0f *pv0->z - 5.0f * pv1->z + 4.0f * pv2->z - pv3->z) * s * s + (pv3->z -3.0f * pv2->z + 3.0f * pv1->z - pv0->z) * s * s * s);
1770     pout->w = 0.5f * (2.0f * pv1->w + (pv2->w - pv0->w) *s + (2.0f *pv0->w - 5.0f * pv1->w + 4.0f * pv2->w - pv3->w) * s * s + (pv3->w -3.0f * pv2->w + 3.0f * pv1->w - pv0->w) * s * s * s);
1771     return pout;
1772 }
1773
1774 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
1775 {
1776     D3DXVECTOR4 out;
1777     out.x = pv1->y * (pv2->z * pv3->w - pv3->z * pv2->w) - pv1->z * (pv2->y * pv3->w - pv3->y * pv2->w) + pv1->w * (pv2->y * pv3->z - pv2->z *pv3->y);
1778     out.y = -(pv1->x * (pv2->z * pv3->w - pv3->z * pv2->w) - pv1->z * (pv2->x * pv3->w - pv3->x * pv2->w) + pv1->w * (pv2->x * pv3->z - pv3->x * pv2->z));
1779     out.z = pv1->x * (pv2->y * pv3->w - pv3->y * pv2->w) - pv1->y * (pv2->x *pv3->w - pv3->x * pv2->w) + pv1->w * (pv2->x * pv3->y - pv3->x * pv2->y);
1780     out.w = -(pv1->x * (pv2->y * pv3->z - pv3->y * pv2->z) - pv1->y * (pv2->x * pv3->z - pv3->x *pv2->z) + pv1->z * (pv2->x * pv3->y - pv3->x * pv2->y));
1781     *pout = out;
1782     return pout;
1783 }
1784
1785 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
1786 {
1787     FLOAT h1, h2, h3, h4;
1788
1789     h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1790     h2 = s * s * s - 2.0f * s * s + s;
1791     h3 = -2.0f * s * s * s + 3.0f * s * s;
1792     h4 = s * s * s - s * s;
1793
1794     pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1795     pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1796     pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1797     pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
1798     return pout;
1799 }
1800
1801 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
1802 {
1803     D3DXVECTOR4 out;
1804     FLOAT norm;
1805
1806     norm = D3DXVec4Length(pv);
1807
1808     out.x = pv->x / norm;
1809     out.y = pv->y / norm;
1810     out.z = pv->z / norm;
1811     out.w = pv->w / norm;
1812
1813     *pout = out;
1814     return pout;
1815 }
1816
1817 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
1818 {
1819     D3DXVECTOR4 out;
1820     out.x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0] * pv->w;
1821     out.y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1] * pv->w;
1822     out.z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2] * pv->w;
1823     out.w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] * pv->z + pm->u.m[3][3] * pv->w;
1824     *pout = out;
1825     return pout;
1826 }
1827
1828 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1829 {
1830     UINT i;
1831
1832     for (i = 0; i < elements; ++i) {
1833         D3DXVec4Transform(
1834             (D3DXVECTOR4*)((char*)out + outstride * i),
1835             (CONST D3DXVECTOR4*)((const char*)in + instride * i),
1836             matrix);
1837     }
1838     return out;
1839 }
1840
1841 static inline unsigned short float_32_to_16(const float in)
1842 {
1843     int exp = 0, origexp;
1844     float tmp = fabs(in);
1845     int sign = (copysignf(1, in) < 0);
1846     unsigned int mantissa;
1847     unsigned short ret;
1848
1849     /* Deal with special numbers */
1850     if (isinf(in)) return (sign ? 0xffff : 0x7fff);
1851     if (isnan(in)) return (sign ? 0xffff : 0x7fff);
1852     if (in == 0.0f) return (sign ? 0x8000 : 0x0000);
1853
1854     if (tmp < powf(2, 10))
1855     {
1856         do
1857         {
1858             tmp *= 2.0f;
1859             exp--;
1860         } while (tmp < powf(2, 10));
1861     }
1862     else if (tmp >= powf(2, 11))
1863     {
1864         do
1865         {
1866             tmp /= 2.0f;
1867             exp++;
1868         } while (tmp >= powf(2, 11));
1869     }
1870
1871     exp += 10;  /* Normalize the mantissa */
1872     exp += 15;  /* Exponent is encoded with excess 15 */
1873
1874     origexp = exp;
1875
1876     mantissa = (unsigned int) tmp;
1877     if ((tmp - mantissa == 0.5f && mantissa % 2 == 1) || /* round half to even */
1878         (tmp - mantissa > 0.5f))
1879     {
1880         mantissa++; /* round to nearest, away from zero */
1881     }
1882     if (mantissa == 2048)
1883     {
1884         mantissa = 1024;
1885         exp++;
1886     }
1887
1888     if (exp > 31)
1889     {
1890         /* too big */
1891         ret = 0x7fff; /* INF */
1892     }
1893     else if (exp <= 0)
1894     {
1895         unsigned int rounding = 0;
1896
1897         /* Denormalized half float */
1898
1899         /* return 0x0000 (=0.0) for numbers too small to represent in half floats */
1900         if (exp < -11)
1901             return (sign ? 0x8000 : 0x0000);
1902
1903         exp = origexp;
1904
1905         /* the 13 extra bits from single precision are used for rounding */
1906         mantissa = (unsigned int)(tmp * powf(2, 13));
1907         mantissa >>= 1 - exp; /* denormalize */
1908
1909         mantissa -= ~(mantissa >> 13) & 1; /* round half to even */
1910         /* remove 13 least significant bits to get half float precision */
1911         mantissa >>= 12;
1912         rounding = mantissa & 1;
1913         mantissa >>= 1;
1914
1915         ret = mantissa + rounding;
1916     }
1917     else
1918     {
1919         ret = (exp << 10) | (mantissa & 0x3ff);
1920     }
1921
1922     ret |= ((sign ? 1 : 0) << 15); /* Add the sign */
1923     return ret;
1924 }
1925
1926 D3DXFLOAT16 *WINAPI D3DXFloat32To16Array(D3DXFLOAT16 *pout, CONST FLOAT *pin, UINT n)
1927 {
1928     unsigned int i;
1929
1930     for (i = 0; i < n; ++i)
1931     {
1932         pout[i].value = float_32_to_16(pin[i]);
1933     }
1934
1935     return pout;
1936 }
1937
1938 /* Native d3dx9's D3DXFloat16to32Array lacks support for NaN and Inf. Specifically, e = 16 is treated as a
1939  * regular number - e.g., 0x7fff is converted to 131008.0 and 0xffff to -131008.0. */
1940 static inline float float_16_to_32(const unsigned short in)
1941 {
1942     const unsigned short s = (in & 0x8000);
1943     const unsigned short e = (in & 0x7C00) >> 10;
1944     const unsigned short m = in & 0x3FF;
1945     const float sgn = (s ? -1.0f : 1.0f);
1946
1947     if (e == 0)
1948     {
1949         if (m == 0) return sgn * 0.0f; /* +0.0 or -0.0 */
1950         else return sgn * powf(2, -14.0f) * (m / 1024.0f);
1951     }
1952     else
1953     {
1954         return sgn * powf(2, e - 15.0f) * (1.0f + (m / 1024.0f));
1955     }
1956 }
1957
1958 FLOAT *WINAPI D3DXFloat16To32Array(FLOAT *pout, CONST D3DXFLOAT16 *pin, UINT n)
1959 {
1960     unsigned int i;
1961
1962     for (i = 0; i < n; ++i)
1963     {
1964         pout[i] = float_16_to_32(pin[i].value);
1965     }
1966
1967     return pout;
1968 }
1969
1970 /*_________________D3DXSH________________*/
1971
1972 FLOAT* WINAPI D3DXSHAdd(FLOAT *out, UINT order, const FLOAT *a, const FLOAT *b)
1973 {
1974     UINT i;
1975
1976     TRACE("out %p, order %u, a %p, b %p\n", out, order, a, b);
1977
1978     for (i = 0; i < order * order; i++)
1979         out[i] = a[i] + b[i];
1980
1981     return out;
1982 }
1983
1984 FLOAT* WINAPI D3DXSHMultiply3(FLOAT *out, CONST FLOAT *a, CONST FLOAT *b)
1985 {
1986     FLOAT t, ta, tb;
1987
1988     TRACE("(%p, %p, %p)\n", out, a, b);
1989
1990     out[0]= 0.28209479f * a[0] * b[0];
1991
1992     ta = 0.28209479f * a[0] - 0.12615662f * a[6] - 0.21850968f * a[8];
1993     tb = 0.28209479f * b[0] - 0.12615662f * b[6] - 0.21850968f * b[8];
1994     out[1] = ta * b[1] + tb * a[1];
1995     t = a[1] * b[1];
1996     out[0] += 0.28209479f * t;
1997     out[6] = -0.12615662f * t;
1998     out[8] = -0.21850968f * t;
1999
2000     ta = 0.21850968f * a[5];
2001     tb = 0.21850968f * b[5];
2002     out[1] += ta * b[2] + tb * a[2];
2003     out[2] = ta * b[1] + tb * a[1];
2004     t = a[1] * b[2] +a[2] * b[1];
2005     out[5] = 0.21850968f * t;
2006
2007     ta = 0.21850968f * a[4];
2008     tb = 0.21850968f * b[4];
2009     out[1] += ta * b[3] + tb * a[3];
2010     out[3]  = ta * b[1] + tb * a[1];
2011     t = a[1] * b[3] + a[3] * b[1];
2012     out[4] = 0.21850968f * t;
2013
2014     ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2015     tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2016     out[2] += ta * b[2] + tb * a[2];
2017     t = a[2] * b[2];
2018     out[0] += 0.28209480f * t;
2019     out[6] += 0.25231326f * t;
2020
2021     ta = 0.21850969f * a[7];
2022     tb = 0.21850969f * b[7];
2023     out[2] += ta * b[3] + tb * a[3];
2024     out[3] += ta * b[2] + tb * a[2];
2025     t = a[2] * b[3] + a[3] * b[2];
2026     out[7] = 0.21850969f * t;
2027
2028     ta = 0.28209479f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2029     tb = 0.28209479f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2030     out[3] += ta * b[3] + tb * a[3];
2031     t = a[3] * b[3];
2032     out[0] += 0.28209479f * t;
2033     out[6] -= 0.12615663f * t;
2034     out[8] += 0.21850969f * t;
2035
2036     ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2037     tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2038     out[4] += ta * b[4] + tb * a[4];
2039     t = a[4] * b[4];
2040     out[0] += 0.28209479f * t;
2041     out[6] -= 0.18022375f * t;
2042
2043     ta = 0.15607835f * a[7];
2044     tb = 0.15607835f * b[7];
2045     out[4] += ta * b[5] + tb * a[5];
2046     out[5] += ta * b[4] + tb * a[4];
2047     t = a[4] * b[5] + a[5] * b[4];
2048     out[7] += 0.15607834f * t;
2049
2050     ta = 0.28209479f * a[0] + 0.09011186 * a[6] - 0.15607835f * a[8];
2051     tb = 0.28209479f * b[0] + 0.09011186 * b[6] - 0.15607835f * b[8];
2052     out[5] += ta * b[5] + tb * a[5];
2053     t = a[5] * b[5];
2054     out[0] += 0.28209479f * t;
2055     out[6] += 0.09011186f * t;
2056     out[8] -= 0.15607835f * t;
2057
2058     ta = 0.28209480f * a[0];
2059     tb = 0.28209480f * b[0];
2060     out[6] += ta * b[6] + tb * a[6];
2061     t = a[6] * b[6];
2062     out[0] += 0.28209480f * t;
2063     out[6] += 0.18022376f * t;
2064
2065     ta = 0.28209479f * a[0] + 0.09011186 * a[6] + 0.15607835f * a[8];
2066     tb = 0.28209479f * b[0] + 0.09011186 * b[6] + 0.15607835f * b[8];
2067     out[7] += ta * b[7] + tb * a[7];
2068     t = a[7] * b[7];
2069     out[0] += 0.28209479f * t;
2070     out[6] += 0.09011186f * t;
2071     out[8] += 0.15607835f * t;
2072
2073     ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2074     tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2075     out[8] += ta * b[8] + tb * a[8];
2076     t = a[8] * b[8];
2077     out[0] += 0.28209479f * t;
2078     out[6] -= 0.18022375f * t;
2079
2080     return out;
2081 }