wined3d: Don't free D3D buffers until the wined3d buffer is destroyed.
[wine] / dlls / ddraw / utils.c
1 /*
2  * DirectDraw helper functions
3  *
4  * Copyright (c) 1997-2000 Marcus Meissner
5  * Copyright (c) 1998 Lionel Ulmer
6  * Copyright (c) 2000 TransGaming Technologies Inc.
7  * Copyright (c) 2006 Stefan Dösinger
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25
26 #define NONAMELESSUNION
27
28 #include "ddraw_private.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
32
33 static void DDRAW_dump_pixelformat(const DDPIXELFORMAT *pf);
34
35 /*****************************************************************************
36  * PixelFormat_WineD3DtoDD
37  *
38  * Converts an WINED3DFORMAT value into a DDPIXELFORMAT structure
39  *
40  * Params:
41  *  DDPixelFormat: Address of the structure to write the pixel format to
42  *  WineD3DFormat: Source format
43  *
44  *****************************************************************************/
45 void
46 PixelFormat_WineD3DtoDD(DDPIXELFORMAT *DDPixelFormat,
47                         WINED3DFORMAT WineD3DFormat)
48 {
49     DWORD Size = DDPixelFormat->dwSize;
50     TRACE("Converting WINED3DFORMAT %d to DDRAW\n", WineD3DFormat);
51
52     if(Size==0) return;
53
54     memset(DDPixelFormat, 0x00, Size);
55     DDPixelFormat->dwSize = Size;
56     switch(WineD3DFormat)
57     {
58         case WINED3DFMT_R8G8B8:
59             DDPixelFormat->dwFlags = DDPF_RGB;
60             DDPixelFormat->dwFourCC = 0;
61             DDPixelFormat->u1.dwRGBBitCount = 24;
62             DDPixelFormat->u2.dwRBitMask = 0x00ff0000;
63             DDPixelFormat->u3.dwGBitMask = 0x0000ff00;
64             DDPixelFormat->u4.dwBBitMask = 0x000000ff;
65             DDPixelFormat->u5.dwRGBAlphaBitMask = 0x0;
66             break;
67
68         case WINED3DFMT_A8R8G8B8:
69             DDPixelFormat->dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
70             DDPixelFormat->dwFourCC = 0;
71             DDPixelFormat->u1.dwRGBBitCount = 32;
72             DDPixelFormat->u2.dwRBitMask = 0x00ff0000;
73             DDPixelFormat->u3.dwGBitMask = 0x0000ff00;
74             DDPixelFormat->u4.dwBBitMask = 0x000000ff;
75             DDPixelFormat->u5.dwRGBAlphaBitMask = 0xff000000;
76             break;
77
78         case WINED3DFMT_X8R8G8B8:
79             DDPixelFormat->dwFlags = DDPF_RGB;
80             DDPixelFormat->dwFourCC = 0;
81             DDPixelFormat->u1.dwRGBBitCount = 32;
82             DDPixelFormat->u2.dwRBitMask = 0x00ff0000;
83             DDPixelFormat->u3.dwGBitMask = 0x0000ff00;
84             DDPixelFormat->u4.dwBBitMask = 0x000000ff;
85             DDPixelFormat->u5.dwRGBAlphaBitMask = 0x0;
86             break;
87
88         case WINED3DFMT_X8B8G8R8:
89             DDPixelFormat->dwFlags = DDPF_RGB;
90             DDPixelFormat->dwFourCC = 0;
91             DDPixelFormat->u1.dwRGBBitCount = 32;
92             DDPixelFormat->u2.dwRBitMask = 0x000000ff;
93             DDPixelFormat->u3.dwGBitMask = 0x0000ff00;
94             DDPixelFormat->u4.dwBBitMask = 0x00ff0000;
95             DDPixelFormat->u5.dwRGBAlphaBitMask = 0x0;
96             break;
97
98         case WINED3DFMT_R5G6B5:
99             DDPixelFormat->dwFlags = DDPF_RGB;
100             DDPixelFormat->dwFourCC = 0;
101             DDPixelFormat->u1.dwRGBBitCount = 16;
102             DDPixelFormat->u2.dwRBitMask = 0xF800;
103             DDPixelFormat->u3.dwGBitMask = 0x07E0;
104             DDPixelFormat->u4.dwBBitMask = 0x001F;
105             DDPixelFormat->u5.dwRGBAlphaBitMask = 0x0;
106             break;
107
108         case WINED3DFMT_X1R5G5B5:
109             DDPixelFormat->dwFlags = DDPF_RGB;
110             DDPixelFormat->dwFourCC = 0;
111             DDPixelFormat->u1.dwRGBBitCount = 16;
112             DDPixelFormat->u2.dwRBitMask = 0x7C00;
113             DDPixelFormat->u3.dwGBitMask = 0x03E0;
114             DDPixelFormat->u4.dwBBitMask = 0x001F;
115             DDPixelFormat->u5.dwRGBAlphaBitMask = 0x0;
116             break;
117
118         case WINED3DFMT_A1R5G5B5:
119             DDPixelFormat->dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
120             DDPixelFormat->dwFourCC = 0;
121             DDPixelFormat->u1.dwRGBBitCount = 16;
122             DDPixelFormat->u2.dwRBitMask = 0x7C00;
123             DDPixelFormat->u3.dwGBitMask = 0x03E0;
124             DDPixelFormat->u4.dwBBitMask = 0x001F;
125             DDPixelFormat->u5.dwRGBAlphaBitMask = 0x8000;
126             break;
127
128         case WINED3DFMT_A4R4G4B4:
129             DDPixelFormat->dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
130             DDPixelFormat->dwFourCC = 0;
131             DDPixelFormat->u1.dwRGBBitCount = 16;
132             DDPixelFormat->u2.dwRBitMask = 0x0F00;
133             DDPixelFormat->u3.dwGBitMask = 0x00F0;
134             DDPixelFormat->u4.dwBBitMask = 0x000F;
135             DDPixelFormat->u5.dwRGBAlphaBitMask = 0xF000;
136             break;
137
138         case WINED3DFMT_R3G3B2:
139             DDPixelFormat->dwFlags = DDPF_RGB;
140             DDPixelFormat->dwFourCC = 0;
141             DDPixelFormat->u1.dwRGBBitCount = 8;
142             DDPixelFormat->u2.dwRBitMask = 0xE0;
143             DDPixelFormat->u3.dwGBitMask = 0x1C;
144             DDPixelFormat->u4.dwBBitMask = 0x03;
145             DDPixelFormat->u5.dwLuminanceAlphaBitMask = 0x0;
146             break;
147
148         case WINED3DFMT_P8:
149             DDPixelFormat->dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
150             DDPixelFormat->dwFourCC = 0;
151             DDPixelFormat->u1.dwRGBBitCount = 8;
152             DDPixelFormat->u2.dwRBitMask = 0x00;
153             DDPixelFormat->u3.dwGBitMask = 0x00;
154             DDPixelFormat->u4.dwBBitMask = 0x00;
155             break;
156
157         case WINED3DFMT_A8_UNORM:
158             DDPixelFormat->dwFlags = DDPF_ALPHA;
159             DDPixelFormat->dwFourCC = 0;
160             DDPixelFormat->u1.dwAlphaBitDepth = 8;
161             DDPixelFormat->u2.dwRBitMask = 0x0;
162             DDPixelFormat->u3.dwZBitMask = 0x0;
163             DDPixelFormat->u4.dwStencilBitMask = 0x0;
164             DDPixelFormat->u5.dwLuminanceAlphaBitMask = 0x0;
165             break;
166
167         case WINED3DFMT_A8R3G3B2:
168             DDPixelFormat->dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
169             DDPixelFormat->dwFourCC = 0;
170             DDPixelFormat->u1.dwRGBBitCount = 16;
171             DDPixelFormat->u2.dwRBitMask = 0x00E0;
172             DDPixelFormat->u3.dwGBitMask = 0x001C;
173             DDPixelFormat->u4.dwBBitMask = 0x0003;
174             DDPixelFormat->u5.dwRGBAlphaBitMask = 0xF000;
175             break;
176
177         case WINED3DFMT_X4R4G4B4:
178             DDPixelFormat->dwFlags = DDPF_RGB;
179             DDPixelFormat->dwFourCC = 0;
180             DDPixelFormat->u1.dwRGBBitCount = 16;
181             DDPixelFormat->u2.dwRBitMask = 0x0F00;
182             DDPixelFormat->u3.dwGBitMask = 0x00F0;
183             DDPixelFormat->u4.dwBBitMask = 0x000F;
184             DDPixelFormat->u5.dwRGBAlphaBitMask = 0x0;
185             return;
186
187         /* How are Z buffer bit depth and Stencil buffer bit depth related?
188          */
189         case WINED3DFMT_D16_UNORM:
190             DDPixelFormat->dwFlags = DDPF_ZBUFFER;
191             DDPixelFormat->dwFourCC = 0;
192             DDPixelFormat->u1.dwZBufferBitDepth = 16;
193             DDPixelFormat->u2.dwStencilBitDepth = 0;
194             DDPixelFormat->u3.dwZBitMask = 0x0000FFFF;
195             DDPixelFormat->u4.dwStencilBitMask = 0x0;
196             DDPixelFormat->u5.dwRGBZBitMask = 0x00000000;
197             break;
198
199         case WINED3DFMT_D32:
200             DDPixelFormat->dwFlags = DDPF_ZBUFFER;
201             DDPixelFormat->dwFourCC = 0;
202             DDPixelFormat->u1.dwZBufferBitDepth = 32;
203             DDPixelFormat->u2.dwStencilBitDepth = 0;
204             DDPixelFormat->u3.dwZBitMask = 0xFFFFFFFF;
205             DDPixelFormat->u4.dwStencilBitMask = 0x0;
206             DDPixelFormat->u5.dwRGBZBitMask = 0x00000000;
207             break;
208
209         case WINED3DFMT_D24X4S4:
210             DDPixelFormat->dwFlags = DDPF_ZBUFFER | DDPF_STENCILBUFFER;
211             DDPixelFormat->dwFourCC = 0;
212             /* Should I set dwZBufferBitDepth to 32 here? */
213             DDPixelFormat->u1.dwZBufferBitDepth = 32;
214             DDPixelFormat->u2.dwStencilBitDepth = 4;
215             DDPixelFormat->u3.dwZBitMask = 0x00FFFFFF;
216             DDPixelFormat->u4.dwStencilBitMask = 0x0F000000;
217             DDPixelFormat->u5.dwRGBAlphaBitMask = 0x0;
218             break;
219
220         case WINED3DFMT_D24S8:
221             DDPixelFormat->dwFlags = DDPF_ZBUFFER | DDPF_STENCILBUFFER;
222             DDPixelFormat->dwFourCC = 0;
223             /* Should I set dwZBufferBitDepth to 32 here? */
224             DDPixelFormat->u1.dwZBufferBitDepth = 32;
225             DDPixelFormat->u2.dwStencilBitDepth = 8;
226             DDPixelFormat->u3.dwZBitMask = 0x00FFFFFFFF;
227             DDPixelFormat->u4.dwStencilBitMask = 0xFF000000;
228             DDPixelFormat->u5.dwRGBAlphaBitMask = 0x0;
229             break;
230
231         case WINED3DFMT_D24X8:
232             DDPixelFormat->dwFlags = DDPF_ZBUFFER;
233             DDPixelFormat->dwFourCC = 0;
234             DDPixelFormat->u1.dwZBufferBitDepth = 32;
235             DDPixelFormat->u2.dwStencilBitDepth = 0;
236             DDPixelFormat->u3.dwZBitMask = 0x00FFFFFFFF;
237             DDPixelFormat->u4.dwStencilBitMask = 0x00000000;
238             DDPixelFormat->u5.dwRGBAlphaBitMask = 0x0;
239
240             break;
241         case WINED3DFMT_D15S1:
242             DDPixelFormat->dwFlags = DDPF_ZBUFFER | DDPF_STENCILBUFFER;
243             DDPixelFormat->dwFourCC = 0;
244             DDPixelFormat->u1.dwZBufferBitDepth = 16;
245             DDPixelFormat->u2.dwStencilBitDepth = 1;
246             DDPixelFormat->u3.dwZBitMask = 0x7fff;
247             DDPixelFormat->u4.dwStencilBitMask = 0x8000;
248             DDPixelFormat->u5.dwRGBAlphaBitMask = 0x0;
249             break;
250
251         case WINED3DFMT_UYVY:
252         case WINED3DFMT_YUY2:
253             DDPixelFormat->u1.dwYUVBitCount = 16;
254             DDPixelFormat->dwFlags = DDPF_FOURCC;
255             DDPixelFormat->dwFourCC = WineD3DFormat;
256             break;
257
258         case WINED3DFMT_YV12:
259             DDPixelFormat->u1.dwYUVBitCount = 12;
260             DDPixelFormat->dwFlags = DDPF_FOURCC;
261             DDPixelFormat->dwFourCC = WineD3DFormat;
262             break;
263
264         case WINED3DFMT_DXT1:
265         case WINED3DFMT_DXT2:
266         case WINED3DFMT_DXT3:
267         case WINED3DFMT_DXT4:
268         case WINED3DFMT_DXT5:
269         case WINED3DFMT_MULTI2_ARGB8:
270         case WINED3DFMT_G8R8_G8B8:
271         case WINED3DFMT_R8G8_B8G8:
272             DDPixelFormat->dwFlags = DDPF_FOURCC;
273             DDPixelFormat->dwFourCC = WineD3DFormat;
274             break;
275
276         /* Luminance */
277         case WINED3DFMT_L8:
278             DDPixelFormat->dwFlags = DDPF_LUMINANCE;
279             DDPixelFormat->dwFourCC = 0;
280             DDPixelFormat->u1.dwLuminanceBitCount = 8;
281             DDPixelFormat->u2.dwLuminanceBitMask = 0xff;
282             DDPixelFormat->u3.dwBumpDvBitMask = 0x0;
283             DDPixelFormat->u4.dwBumpLuminanceBitMask = 0x0;
284             DDPixelFormat->u5.dwLuminanceAlphaBitMask = 0x0;
285             break;
286
287         case WINED3DFMT_A4L4:
288             DDPixelFormat->dwFlags = DDPF_ALPHAPIXELS | DDPF_LUMINANCE;
289             DDPixelFormat->dwFourCC = 0;
290             DDPixelFormat->u1.dwLuminanceBitCount = 4;
291             DDPixelFormat->u2.dwLuminanceBitMask = 0x0f;
292             DDPixelFormat->u3.dwBumpDvBitMask = 0x0;
293             DDPixelFormat->u4.dwBumpLuminanceBitMask = 0x0;
294             DDPixelFormat->u5.dwLuminanceAlphaBitMask = 0xf0;
295             break;
296
297         case WINED3DFMT_A8L8:
298             DDPixelFormat->dwFlags = DDPF_ALPHAPIXELS | DDPF_LUMINANCE;
299             DDPixelFormat->dwFourCC = 0;
300             DDPixelFormat->u1.dwLuminanceBitCount = 16;
301             DDPixelFormat->u2.dwLuminanceBitMask = 0x00ff;
302             DDPixelFormat->u3.dwBumpDvBitMask = 0x0;
303             DDPixelFormat->u4.dwBumpLuminanceBitMask = 0x0;
304             DDPixelFormat->u5.dwLuminanceAlphaBitMask = 0xff00;
305             break;
306
307         /* Bump mapping */
308         case WINED3DFMT_R8G8_SNORM:
309             DDPixelFormat->dwFlags = DDPF_BUMPDUDV;
310             DDPixelFormat->dwFourCC = 0;
311             DDPixelFormat->u1.dwBumpBitCount = 16;
312             DDPixelFormat->u2.dwBumpDuBitMask =         0x000000ff;
313             DDPixelFormat->u3.dwBumpDvBitMask =         0x0000ff00;
314             DDPixelFormat->u4.dwBumpLuminanceBitMask =  0x00000000;
315             DDPixelFormat->u5.dwLuminanceAlphaBitMask = 0x00000000;
316             break;
317
318         case WINED3DFMT_L6V5U5:
319             DDPixelFormat->dwFlags = DDPF_BUMPDUDV;
320             DDPixelFormat->dwFourCC = 0;
321             DDPixelFormat->u1.dwBumpBitCount = 16;
322             DDPixelFormat->u2.dwBumpDuBitMask =         0x0000001f;
323             DDPixelFormat->u3.dwBumpDvBitMask =         0x000003e0;
324             DDPixelFormat->u4.dwBumpLuminanceBitMask =  0x0000fc00;
325             DDPixelFormat->u5.dwLuminanceAlphaBitMask = 0x00000000;
326             break;
327
328         case WINED3DFMT_X8L8V8U8:
329             DDPixelFormat->dwFlags = DDPF_BUMPDUDV;
330             DDPixelFormat->dwFourCC = 0;
331             DDPixelFormat->u1.dwBumpBitCount = 32;
332             DDPixelFormat->u2.dwBumpDuBitMask =         0x000000ff;
333             DDPixelFormat->u3.dwBumpDvBitMask =         0x0000ff00;
334             DDPixelFormat->u4.dwBumpLuminanceBitMask =  0x00ff0000;
335             DDPixelFormat->u5.dwLuminanceAlphaBitMask = 0x00000000;
336             break;
337
338         default:
339             ERR("Can't translate this Pixelformat %d\n", WineD3DFormat);
340     }
341
342     if(TRACE_ON(ddraw)) {
343         TRACE("Returning: ");
344         DDRAW_dump_pixelformat(DDPixelFormat);
345     }
346 }
347 /*****************************************************************************
348  * PixelFormat_DD2WineD3D
349  *
350  * Reads a DDPIXELFORMAT structure and returns the equal WINED3DFORMAT
351  *
352  * Params:
353  *  DDPixelFormat: The source format
354  *
355  * Returns:
356  *  The WINED3DFORMAT equal to the DDraw format
357  *  WINED3DFMT_UNKNOWN if a matching format wasn't found
358  *****************************************************************************/
359 WINED3DFORMAT
360 PixelFormat_DD2WineD3D(const DDPIXELFORMAT *DDPixelFormat)
361 {
362     TRACE("Convert a DirectDraw Pixelformat to a WineD3D Pixelformat\n");
363     if(TRACE_ON(ddraw))
364     {
365         DDRAW_dump_pixelformat(DDPixelFormat);
366     }
367
368     if(DDPixelFormat->dwFlags & DDPF_PALETTEINDEXED8)
369     {
370         return WINED3DFMT_P8;
371     }
372     else if(DDPixelFormat->dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 | DDPF_PALETTEINDEXED4) )
373     {
374         FIXME("DDPF_PALETTEINDEXED1 to DDPF_PALETTEINDEXED4 are not supported by WineD3D (yet). Returning WINED3DFMT_P8\n");
375         return WINED3DFMT_P8;
376     }
377     else if(DDPixelFormat->dwFlags & DDPF_RGB)
378     {
379         switch(DDPixelFormat->u1.dwRGBBitCount)
380         {
381             case 8:
382                 /* This is the only format that can match here */
383                 return WINED3DFMT_R3G3B2;
384
385             case 16:
386                 /* Read the Color masks */
387                 if( (DDPixelFormat->u2.dwRBitMask == 0xF800) &&
388                     (DDPixelFormat->u3.dwGBitMask == 0x07E0) &&
389                     (DDPixelFormat->u4.dwBBitMask == 0x001F) )
390                 {
391                     return WINED3DFMT_R5G6B5;
392                 }
393
394                 if( (DDPixelFormat->u2.dwRBitMask == 0x7C00) &&
395                     (DDPixelFormat->u3.dwGBitMask == 0x03E0) &&
396                     (DDPixelFormat->u4.dwBBitMask == 0x001F) )
397                 {
398                     if( (DDPixelFormat->dwFlags & DDPF_ALPHAPIXELS) &&
399                         (DDPixelFormat->u5.dwRGBAlphaBitMask == 0x8000))
400                         return WINED3DFMT_A1R5G5B5;
401                     else
402                         return WINED3DFMT_X1R5G5B5;
403                 }
404
405                 if( (DDPixelFormat->u2.dwRBitMask == 0x0F00) &&
406                     (DDPixelFormat->u3.dwGBitMask == 0x00F0) &&
407                     (DDPixelFormat->u4.dwBBitMask == 0x000F) )
408                 {
409                     if( (DDPixelFormat->dwFlags & DDPF_ALPHAPIXELS) &&
410                         (DDPixelFormat->u5.dwRGBAlphaBitMask == 0xF000))
411                         return WINED3DFMT_A4R4G4B4;
412                     else
413                         return WINED3DFMT_X4R4G4B4;
414                 }
415
416                 if( (DDPixelFormat->dwFlags & DDPF_ALPHAPIXELS) &&
417                     (DDPixelFormat->u5.dwRGBAlphaBitMask == 0xFF00) &&
418                     (DDPixelFormat->u2.dwRBitMask == 0x00E0) &&
419                     (DDPixelFormat->u3.dwGBitMask == 0x001C) &&
420                     (DDPixelFormat->u4.dwBBitMask == 0x0003) )
421                 {
422                     return WINED3DFMT_A8R3G3B2;
423                 }
424                 ERR("16 bit RGB Pixel format does not match\n");
425                 return WINED3DFMT_UNKNOWN;
426
427             case 24:
428                 return WINED3DFMT_R8G8B8;
429
430             case 32:
431                 /* Read the Color masks */
432                 if( (DDPixelFormat->u2.dwRBitMask == 0x00FF0000) &&
433                     (DDPixelFormat->u3.dwGBitMask == 0x0000FF00) &&
434                     (DDPixelFormat->u4.dwBBitMask == 0x000000FF) )
435                 {
436                     if( (DDPixelFormat->dwFlags & DDPF_ALPHAPIXELS) &&
437                         (DDPixelFormat->u5.dwRGBAlphaBitMask == 0xFF000000))
438                         return WINED3DFMT_A8R8G8B8;
439                     else
440                         return WINED3DFMT_X8R8G8B8;
441
442                 }
443                 ERR("32 bit RGB pixel format does not match\n");
444
445             default:
446                 ERR("Invalid dwRGBBitCount in Pixelformat structure\n");
447                 return WINED3DFMT_UNKNOWN;
448         }
449     }
450     else if( (DDPixelFormat->dwFlags & DDPF_ALPHA) )
451     {
452         /* Alpha only Pixelformat */
453         switch(DDPixelFormat->u1.dwAlphaBitDepth)
454         {
455             case 1:
456             case 2:
457             case 4:
458                 ERR("Unsupported Alpha-Only bit depth 0x%x\n", DDPixelFormat->u1.dwAlphaBitDepth);
459             case 8:
460                 return WINED3DFMT_A8_UNORM;
461
462             default:
463                 ERR("Invalid AlphaBitDepth in Alpha-Only Pixelformat\n");
464                 return WINED3DFMT_UNKNOWN;
465         }
466     }
467     else if(DDPixelFormat->dwFlags & DDPF_LUMINANCE)
468     {
469         /* Luminance-only or luminance-alpha */
470         if(DDPixelFormat->dwFlags & DDPF_ALPHAPIXELS)
471         {
472             /* Luminance with Alpha */
473             switch(DDPixelFormat->u1.dwLuminanceBitCount)
474             {
475                 case 4:
476                     if(DDPixelFormat->u1.dwAlphaBitDepth == 4)
477                         return WINED3DFMT_A4L4;
478                     ERR("Unknown Alpha / Luminance bit depth combination\n");
479                     return WINED3DFMT_UNKNOWN;
480
481                 case 6:
482                     ERR("A luminance Pixelformat shouldn't have 6 luminance bits. Returning D3DFMT_L6V5U5 for now!!\n");
483                     return WINED3DFMT_L6V5U5;
484
485                 case 8:
486                     if(DDPixelFormat->u1.dwAlphaBitDepth == 8)
487                         return WINED3DFMT_A8L8;
488                     ERR("Unknown Alpha / Lumincase bit depth combination\n");
489                     return WINED3DFMT_UNKNOWN;
490             }
491         }
492         else
493         {
494             /* Luminance-only */
495             switch(DDPixelFormat->u1.dwLuminanceBitCount)
496             {
497                 case 6:
498                     ERR("A luminance Pixelformat shouldn't have 6 luminance bits. Returning D3DFMT_L6V5U5 for now!!\n");
499                     return WINED3DFMT_L6V5U5;
500
501                 case 8:
502                     return WINED3DFMT_L8;
503
504                 default:
505                     ERR("Unknown luminance-only bit depth 0x%x\n", DDPixelFormat->u1.dwLuminanceBitCount);
506                     return WINED3DFMT_UNKNOWN;
507              }
508         }
509     }
510     else if(DDPixelFormat->dwFlags & DDPF_ZBUFFER)
511     {
512         /* Z buffer */
513         if(DDPixelFormat->dwFlags & DDPF_STENCILBUFFER)
514         {
515             switch(DDPixelFormat->u1.dwZBufferBitDepth)
516             {
517                 case 8:
518                     FIXME("8 Bits Z+Stencil buffer pixelformat is not supported. Returning WINED3DFMT_UNKNOWN\n");
519                     return WINED3DFMT_UNKNOWN;
520
521                 case 15:
522                     FIXME("15 bit depth buffer not handled yet, assuming 16 bit\n");
523                 case 16:
524                     if(DDPixelFormat->u2.dwStencilBitDepth == 1)
525                         return WINED3DFMT_D15S1;
526
527                     FIXME("Don't know how to handle a 16 bit Z buffer with %d bit stencil buffer pixelformat\n", DDPixelFormat->u2.dwStencilBitDepth);
528                     return WINED3DFMT_UNKNOWN;
529
530                 case 24:
531                     FIXME("Don't know how to handle a 24 bit depth buffer with stencil bits\n");
532                     return WINED3DFMT_D24S8;
533
534                 case 32:
535                     if(DDPixelFormat->u2.dwStencilBitDepth == 8)
536                         return WINED3DFMT_D24S8;
537                     else
538                         return WINED3DFMT_D24X4S4;
539
540                 default:
541                     ERR("Unknown Z buffer depth %d\n", DDPixelFormat->u1.dwZBufferBitDepth);
542                     return WINED3DFMT_UNKNOWN;
543             }
544         }
545         else
546         {
547             switch(DDPixelFormat->u1.dwZBufferBitDepth)
548             {
549                 case 8:
550                     ERR("8 Bit Z buffers are not supported. Trying a 16 Bit one\n");
551                     return WINED3DFMT_D16_UNORM;
552
553                 case 16:
554                     return WINED3DFMT_D16_UNORM;
555
556                 case 24:
557                     FIXME("24 Bit depth buffer, treating like a 32 bit one\n");
558                 case 32:
559                     if(DDPixelFormat->u3.dwZBitMask == 0x00FFFFFF) {
560                         return WINED3DFMT_D24X8;
561                     } else if(DDPixelFormat->u3.dwZBitMask == 0xFFFFFFFF) {
562                         return WINED3DFMT_D32;
563                     }
564                     FIXME("Unhandled 32 bit depth buffer bitmasks, returning WINED3DFMT_D24X8\n");
565                     return WINED3DFMT_D24X8; /* That's most likely to make games happy */
566
567                 default:
568                     ERR("Unsupported Z buffer depth %d\n", DDPixelFormat->u1.dwZBufferBitDepth);
569                     return WINED3DFMT_UNKNOWN;
570             }
571         }
572     }
573     else if(DDPixelFormat->dwFlags & DDPF_FOURCC)
574     {
575         if(DDPixelFormat->dwFourCC == MAKEFOURCC('U', 'Y', 'V', 'Y'))
576         {
577             return WINED3DFMT_UYVY;
578         }
579         if(DDPixelFormat->dwFourCC == MAKEFOURCC('Y', 'U', 'Y', '2'))
580         {
581             return WINED3DFMT_YUY2;
582         }
583         if(DDPixelFormat->dwFourCC == MAKEFOURCC('Y', 'V', '1', '2'))
584         {
585             return WINED3DFMT_YV12;
586         }
587         if(DDPixelFormat->dwFourCC == MAKEFOURCC('D', 'X', 'T', '1'))
588         {
589             return WINED3DFMT_DXT1;
590         }
591         if(DDPixelFormat->dwFourCC == MAKEFOURCC('D', 'X', 'T', '2'))
592         {
593             return WINED3DFMT_DXT2;
594         }
595         if(DDPixelFormat->dwFourCC == MAKEFOURCC('D', 'X', 'T', '3'))
596         {
597            return WINED3DFMT_DXT3;
598         }
599         if(DDPixelFormat->dwFourCC == MAKEFOURCC('D', 'X', 'T', '4'))
600         {
601             return WINED3DFMT_DXT4;
602         }
603         if(DDPixelFormat->dwFourCC == MAKEFOURCC('D', 'X', 'T', '5'))
604         {
605             return WINED3DFMT_DXT5;
606         }
607         if(DDPixelFormat->dwFourCC == MAKEFOURCC('G', 'R', 'G', 'B'))
608         {
609             return WINED3DFMT_G8R8_G8B8;
610         }
611         if(DDPixelFormat->dwFourCC == MAKEFOURCC('R', 'G', 'B', 'G'))
612         {
613             return WINED3DFMT_R8G8_B8G8;
614         }
615         return WINED3DFMT_UNKNOWN;  /* Abuse this as an error value */
616     }
617     else if(DDPixelFormat->dwFlags & DDPF_BUMPDUDV)
618     {
619         if( (DDPixelFormat->u1.dwBumpBitCount         == 16        ) &&
620             (DDPixelFormat->u2.dwBumpDuBitMask        == 0x000000ff) &&
621             (DDPixelFormat->u3.dwBumpDvBitMask        == 0x0000ff00) &&
622             (DDPixelFormat->u4.dwBumpLuminanceBitMask == 0x00000000) )
623         {
624             return WINED3DFMT_R8G8_SNORM;
625         }
626         else if ( (DDPixelFormat->u1.dwBumpBitCount         == 16        ) &&
627                   (DDPixelFormat->u2.dwBumpDuBitMask        == 0x0000001f) &&
628                   (DDPixelFormat->u3.dwBumpDvBitMask        == 0x000003e0) &&
629                   (DDPixelFormat->u4.dwBumpLuminanceBitMask == 0x0000fc00) )
630         {
631             return WINED3DFMT_L6V5U5;
632         }
633         else if ( (DDPixelFormat->u1.dwBumpBitCount         == 32        ) &&
634                   (DDPixelFormat->u2.dwBumpDuBitMask        == 0x000000ff) &&
635                   (DDPixelFormat->u3.dwBumpDvBitMask        == 0x0000ff00) &&
636                   (DDPixelFormat->u4.dwBumpLuminanceBitMask == 0x00ff0000) )
637         {
638             return WINED3DFMT_X8L8V8U8;
639         }
640     }
641
642     ERR("Unknown Pixelformat!\n");
643     return WINED3DFMT_UNKNOWN;
644 }
645
646 /*****************************************************************************
647  * Various dumping functions.
648  *
649  * They write the contents of a specific function to a TRACE.
650  *
651  *****************************************************************************/
652 static void
653 DDRAW_dump_DWORD(const void *in)
654 {
655     TRACE("%d\n", *((const DWORD *) in));
656 }
657 static void
658 DDRAW_dump_PTR(const void *in)
659 {
660     TRACE("%p\n", *((const void * const*) in));
661 }
662 static void
663 DDRAW_dump_DDCOLORKEY(const DDCOLORKEY *ddck)
664 {
665     TRACE("Low : %d  - High : %d\n", ddck->dwColorSpaceLowValue, ddck->dwColorSpaceHighValue);
666 }
667
668 static void DDRAW_dump_flags_nolf(DWORD flags, const flag_info* names,
669                                   size_t num_names)
670 {
671     unsigned int        i;
672
673     for (i=0; i < num_names; i++)
674         if ((flags & names[i].val) ||      /* standard flag value */
675             ((!flags) && (!names[i].val))) /* zero value only */
676             TRACE("%s ", names[i].name);
677 }
678
679 static void DDRAW_dump_flags(DWORD flags, const flag_info* names, size_t num_names)
680 {
681     DDRAW_dump_flags_nolf(flags, names, num_names);
682     TRACE("\n");
683 }
684
685 void DDRAW_dump_DDSCAPS2(const DDSCAPS2 *in)
686 {
687     static const flag_info flags[] = {
688         FE(DDSCAPS_RESERVED1),
689         FE(DDSCAPS_ALPHA),
690         FE(DDSCAPS_BACKBUFFER),
691         FE(DDSCAPS_COMPLEX),
692         FE(DDSCAPS_FLIP),
693         FE(DDSCAPS_FRONTBUFFER),
694         FE(DDSCAPS_OFFSCREENPLAIN),
695         FE(DDSCAPS_OVERLAY),
696         FE(DDSCAPS_PALETTE),
697         FE(DDSCAPS_PRIMARYSURFACE),
698         FE(DDSCAPS_PRIMARYSURFACELEFT),
699         FE(DDSCAPS_SYSTEMMEMORY),
700         FE(DDSCAPS_TEXTURE),
701         FE(DDSCAPS_3DDEVICE),
702         FE(DDSCAPS_VIDEOMEMORY),
703         FE(DDSCAPS_VISIBLE),
704         FE(DDSCAPS_WRITEONLY),
705         FE(DDSCAPS_ZBUFFER),
706         FE(DDSCAPS_OWNDC),
707         FE(DDSCAPS_LIVEVIDEO),
708         FE(DDSCAPS_HWCODEC),
709         FE(DDSCAPS_MODEX),
710         FE(DDSCAPS_MIPMAP),
711         FE(DDSCAPS_RESERVED2),
712         FE(DDSCAPS_ALLOCONLOAD),
713         FE(DDSCAPS_VIDEOPORT),
714         FE(DDSCAPS_LOCALVIDMEM),
715         FE(DDSCAPS_NONLOCALVIDMEM),
716         FE(DDSCAPS_STANDARDVGAMODE),
717         FE(DDSCAPS_OPTIMIZED)
718     };
719     static const flag_info flags2[] = {
720         FE(DDSCAPS2_HARDWAREDEINTERLACE),
721         FE(DDSCAPS2_HINTDYNAMIC),
722         FE(DDSCAPS2_HINTSTATIC),
723         FE(DDSCAPS2_TEXTUREMANAGE),
724         FE(DDSCAPS2_RESERVED1),
725         FE(DDSCAPS2_RESERVED2),
726         FE(DDSCAPS2_OPAQUE),
727         FE(DDSCAPS2_HINTANTIALIASING),
728         FE(DDSCAPS2_CUBEMAP),
729         FE(DDSCAPS2_CUBEMAP_POSITIVEX),
730         FE(DDSCAPS2_CUBEMAP_NEGATIVEX),
731         FE(DDSCAPS2_CUBEMAP_POSITIVEY),
732         FE(DDSCAPS2_CUBEMAP_NEGATIVEY),
733         FE(DDSCAPS2_CUBEMAP_POSITIVEZ),
734         FE(DDSCAPS2_CUBEMAP_NEGATIVEZ),
735         FE(DDSCAPS2_MIPMAPSUBLEVEL),
736         FE(DDSCAPS2_D3DTEXTUREMANAGE),
737         FE(DDSCAPS2_DONOTPERSIST),
738         FE(DDSCAPS2_STEREOSURFACELEFT)
739     };
740
741     DDRAW_dump_flags_nolf(in->dwCaps, flags, sizeof(flags)/sizeof(flags[0]));
742     DDRAW_dump_flags(in->dwCaps2, flags2, sizeof(flags2)/sizeof(flags2[0]));
743 }
744
745 static void
746 DDRAW_dump_DDSCAPS(const DDSCAPS *in)
747 {
748     DDSCAPS2 in_bis;
749
750     in_bis.dwCaps = in->dwCaps;
751     in_bis.dwCaps2 = 0;
752     in_bis.dwCaps3 = 0;
753     in_bis.dwCaps4 = 0;
754
755     DDRAW_dump_DDSCAPS2(&in_bis);
756 }
757
758 static void
759 DDRAW_dump_pixelformat_flag(DWORD flagmask)
760 {
761     static const flag_info flags[] =
762         {
763             FE(DDPF_ALPHAPIXELS),
764             FE(DDPF_ALPHA),
765             FE(DDPF_FOURCC),
766             FE(DDPF_PALETTEINDEXED4),
767             FE(DDPF_PALETTEINDEXEDTO8),
768             FE(DDPF_PALETTEINDEXED8),
769             FE(DDPF_RGB),
770             FE(DDPF_COMPRESSED),
771             FE(DDPF_RGBTOYUV),
772             FE(DDPF_YUV),
773             FE(DDPF_ZBUFFER),
774             FE(DDPF_PALETTEINDEXED1),
775             FE(DDPF_PALETTEINDEXED2),
776             FE(DDPF_ZPIXELS)
777     };
778
779     DDRAW_dump_flags_nolf(flagmask, flags, sizeof(flags)/sizeof(flags[0]));
780 }
781
782 static void
783 DDRAW_dump_members(DWORD flags,
784                    const void* data,
785                    const member_info* mems,
786                    size_t num_mems)
787 {
788     unsigned int i;
789
790     for (i=0; i < num_mems; i++)
791     {
792         if (mems[i].val & flags)
793         {
794             TRACE(" - %s : ", mems[i].name);
795             mems[i].func((const char *)data + mems[i].offset);
796         }
797     }
798 }
799
800 static void
801 DDRAW_dump_pixelformat(const DDPIXELFORMAT *pf)
802 {
803     TRACE("( ");
804     DDRAW_dump_pixelformat_flag(pf->dwFlags);
805     if (pf->dwFlags & DDPF_FOURCC)
806     {
807         TRACE(", dwFourCC code '%c%c%c%c' (0x%08x) - %d bits per pixel",
808                 (unsigned char)( pf->dwFourCC     &0xff),
809                 (unsigned char)((pf->dwFourCC>> 8)&0xff),
810                 (unsigned char)((pf->dwFourCC>>16)&0xff),
811                 (unsigned char)((pf->dwFourCC>>24)&0xff),
812                 pf->dwFourCC,
813                 pf->u1.dwYUVBitCount
814         );
815     }
816     if (pf->dwFlags & DDPF_RGB)
817     {
818         const char *cmd;
819         TRACE(", RGB bits: %d, ", pf->u1.dwRGBBitCount);
820         switch (pf->u1.dwRGBBitCount)
821         {
822         case 4: cmd = "%1lx"; break;
823         case 8: cmd = "%02lx"; break;
824         case 16: cmd = "%04lx"; break;
825         case 24: cmd = "%06lx"; break;
826         case 32: cmd = "%08lx"; break;
827         default: ERR("Unexpected bit depth !\n"); cmd = "%d"; break;
828         }
829         TRACE(" R "); TRACE(cmd, pf->u2.dwRBitMask);
830         TRACE(" G "); TRACE(cmd, pf->u3.dwGBitMask);
831         TRACE(" B "); TRACE(cmd, pf->u4.dwBBitMask);
832         if (pf->dwFlags & DDPF_ALPHAPIXELS)
833         {
834             TRACE(" A "); TRACE(cmd, pf->u5.dwRGBAlphaBitMask);
835         }
836         if (pf->dwFlags & DDPF_ZPIXELS)
837         {
838             TRACE(" Z "); TRACE(cmd, pf->u5.dwRGBZBitMask);
839         }
840     }
841     if (pf->dwFlags & DDPF_ZBUFFER)
842     {
843         TRACE(", Z bits : %d", pf->u1.dwZBufferBitDepth);
844     }
845     if (pf->dwFlags & DDPF_ALPHA)
846     {
847         TRACE(", Alpha bits : %d", pf->u1.dwAlphaBitDepth);
848     }
849     if (pf->dwFlags & DDPF_BUMPDUDV)
850     {
851         const char *cmd = "%08lx";
852         TRACE(", Bump bits: %d, ", pf->u1.dwBumpBitCount);
853         TRACE(" U "); TRACE(cmd, pf->u2.dwBumpDuBitMask);
854         TRACE(" V "); TRACE(cmd, pf->u3.dwBumpDvBitMask);
855         TRACE(" L "); TRACE(cmd, pf->u4.dwBumpLuminanceBitMask);
856     }
857     TRACE(")\n");
858 }
859
860 void DDRAW_dump_surface_desc(const DDSURFACEDESC2 *lpddsd)
861 {
862 #define STRUCT DDSURFACEDESC2
863     static const member_info members[] =
864         {
865             ME(DDSD_HEIGHT, DDRAW_dump_DWORD, dwHeight),
866             ME(DDSD_WIDTH, DDRAW_dump_DWORD, dwWidth),
867             ME(DDSD_PITCH, DDRAW_dump_DWORD, u1 /* lPitch */),
868             ME(DDSD_LINEARSIZE, DDRAW_dump_DWORD, u1 /* dwLinearSize */),
869             ME(DDSD_BACKBUFFERCOUNT, DDRAW_dump_DWORD, dwBackBufferCount),
870             ME(DDSD_MIPMAPCOUNT, DDRAW_dump_DWORD, u2 /* dwMipMapCount */),
871             ME(DDSD_ZBUFFERBITDEPTH, DDRAW_dump_DWORD, u2 /* dwZBufferBitDepth */), /* This is for 'old-style' D3D */
872             ME(DDSD_REFRESHRATE, DDRAW_dump_DWORD, u2 /* dwRefreshRate */),
873             ME(DDSD_ALPHABITDEPTH, DDRAW_dump_DWORD, dwAlphaBitDepth),
874             ME(DDSD_LPSURFACE, DDRAW_dump_PTR, lpSurface),
875             ME(DDSD_CKDESTOVERLAY, DDRAW_dump_DDCOLORKEY, u3 /* ddckCKDestOverlay */),
876             ME(DDSD_CKDESTBLT, DDRAW_dump_DDCOLORKEY, ddckCKDestBlt),
877             ME(DDSD_CKSRCOVERLAY, DDRAW_dump_DDCOLORKEY, ddckCKSrcOverlay),
878             ME(DDSD_CKSRCBLT, DDRAW_dump_DDCOLORKEY, ddckCKSrcBlt),
879             ME(DDSD_PIXELFORMAT, DDRAW_dump_pixelformat, u4 /* ddpfPixelFormat */)
880         };
881     static const member_info members_caps[] =
882         {
883             ME(DDSD_CAPS, DDRAW_dump_DDSCAPS, ddsCaps)
884         };
885     static const member_info members_caps2[] =
886         {
887             ME(DDSD_CAPS, DDRAW_dump_DDSCAPS2, ddsCaps)
888         };
889 #undef STRUCT
890
891     if (NULL == lpddsd)
892     {
893         TRACE("(null)\n");
894     }
895     else
896     {
897       if (lpddsd->dwSize >= sizeof(DDSURFACEDESC2))
898       {
899           DDRAW_dump_members(lpddsd->dwFlags, lpddsd, members_caps2, 1);
900       }
901       else
902       {
903           DDRAW_dump_members(lpddsd->dwFlags, lpddsd, members_caps, 1);
904       }
905       DDRAW_dump_members(lpddsd->dwFlags, lpddsd, members,
906                           sizeof(members)/sizeof(members[0]));
907     }
908 }
909
910 void
911 dump_D3DMATRIX(const D3DMATRIX *mat)
912 {
913     TRACE("  %f %f %f %f\n", mat->_11, mat->_12, mat->_13, mat->_14);
914     TRACE("  %f %f %f %f\n", mat->_21, mat->_22, mat->_23, mat->_24);
915     TRACE("  %f %f %f %f\n", mat->_31, mat->_32, mat->_33, mat->_34);
916     TRACE("  %f %f %f %f\n", mat->_41, mat->_42, mat->_43, mat->_44);
917 }
918
919 DWORD
920 get_flexible_vertex_size(DWORD d3dvtVertexType)
921 {
922     DWORD size = 0;
923     DWORD i;
924
925     if (d3dvtVertexType & D3DFVF_NORMAL) size += 3 * sizeof(D3DVALUE);
926     if (d3dvtVertexType & D3DFVF_DIFFUSE) size += sizeof(DWORD);
927     if (d3dvtVertexType & D3DFVF_SPECULAR) size += sizeof(DWORD);
928     if (d3dvtVertexType & D3DFVF_RESERVED1) size += sizeof(DWORD);
929     switch (d3dvtVertexType & D3DFVF_POSITION_MASK)
930     {
931         case D3DFVF_XYZ:    size += 3 * sizeof(D3DVALUE); break;
932         case D3DFVF_XYZRHW: size += 4 * sizeof(D3DVALUE); break;
933         case D3DFVF_XYZB1:  size += 4 * sizeof(D3DVALUE); break;
934         case D3DFVF_XYZB2:  size += 5 * sizeof(D3DVALUE); break;
935         case D3DFVF_XYZB3:  size += 6 * sizeof(D3DVALUE); break;
936         case D3DFVF_XYZB4:  size += 7 * sizeof(D3DVALUE); break;
937         case D3DFVF_XYZB5:  size += 8 * sizeof(D3DVALUE); break;
938         default: ERR("Unexpected position mask\n");
939     }
940     for (i = 0; i < GET_TEXCOUNT_FROM_FVF(d3dvtVertexType); i++)
941     {
942         size += GET_TEXCOORD_SIZE_FROM_FVF(d3dvtVertexType, i) * sizeof(D3DVALUE);
943     }
944
945     return size;
946 }
947
948 void DDRAW_Convert_DDSCAPS_1_To_2(const DDSCAPS* pIn, DDSCAPS2* pOut)
949 {
950     /* 2 adds three additional caps fields to the end. Both versions
951      * are unversioned. */
952     pOut->dwCaps = pIn->dwCaps;
953     pOut->dwCaps2 = 0;
954     pOut->dwCaps3 = 0;
955     pOut->dwCaps4 = 0;
956 }
957
958 void DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(const DDDEVICEIDENTIFIER2* pIn, DDDEVICEIDENTIFIER* pOut)
959 {
960     /* 2 adds a dwWHQLLevel field to the end. Both structures are
961      * unversioned. */
962     memcpy(pOut, pIn, sizeof(*pOut));
963 }
964
965 void DDRAW_dump_cooperativelevel(DWORD cooplevel)
966 {
967     static const flag_info flags[] =
968         {
969             FE(DDSCL_FULLSCREEN),
970             FE(DDSCL_ALLOWREBOOT),
971             FE(DDSCL_NOWINDOWCHANGES),
972             FE(DDSCL_NORMAL),
973             FE(DDSCL_ALLOWMODEX),
974             FE(DDSCL_EXCLUSIVE),
975             FE(DDSCL_SETFOCUSWINDOW),
976             FE(DDSCL_SETDEVICEWINDOW),
977             FE(DDSCL_CREATEDEVICEWINDOW)
978     };
979
980     if (TRACE_ON(ddraw))
981     {
982         TRACE(" - ");
983         DDRAW_dump_flags(cooplevel, flags, sizeof(flags)/sizeof(flags[0]));
984     }
985 }
986
987 void DDRAW_dump_DDCAPS(const DDCAPS *lpcaps)
988 {
989     static const flag_info flags1[] =
990     {
991       FE(DDCAPS_3D),
992       FE(DDCAPS_ALIGNBOUNDARYDEST),
993       FE(DDCAPS_ALIGNSIZEDEST),
994       FE(DDCAPS_ALIGNBOUNDARYSRC),
995       FE(DDCAPS_ALIGNSIZESRC),
996       FE(DDCAPS_ALIGNSTRIDE),
997       FE(DDCAPS_BLT),
998       FE(DDCAPS_BLTQUEUE),
999       FE(DDCAPS_BLTFOURCC),
1000       FE(DDCAPS_BLTSTRETCH),
1001       FE(DDCAPS_GDI),
1002       FE(DDCAPS_OVERLAY),
1003       FE(DDCAPS_OVERLAYCANTCLIP),
1004       FE(DDCAPS_OVERLAYFOURCC),
1005       FE(DDCAPS_OVERLAYSTRETCH),
1006       FE(DDCAPS_PALETTE),
1007       FE(DDCAPS_PALETTEVSYNC),
1008       FE(DDCAPS_READSCANLINE),
1009       FE(DDCAPS_STEREOVIEW),
1010       FE(DDCAPS_VBI),
1011       FE(DDCAPS_ZBLTS),
1012       FE(DDCAPS_ZOVERLAYS),
1013       FE(DDCAPS_COLORKEY),
1014       FE(DDCAPS_ALPHA),
1015       FE(DDCAPS_COLORKEYHWASSIST),
1016       FE(DDCAPS_NOHARDWARE),
1017       FE(DDCAPS_BLTCOLORFILL),
1018       FE(DDCAPS_BANKSWITCHED),
1019       FE(DDCAPS_BLTDEPTHFILL),
1020       FE(DDCAPS_CANCLIP),
1021       FE(DDCAPS_CANCLIPSTRETCHED),
1022       FE(DDCAPS_CANBLTSYSMEM)
1023     };
1024     static const flag_info flags2[] =
1025     {
1026       FE(DDCAPS2_CERTIFIED),
1027       FE(DDCAPS2_NO2DDURING3DSCENE),
1028       FE(DDCAPS2_VIDEOPORT),
1029       FE(DDCAPS2_AUTOFLIPOVERLAY),
1030       FE(DDCAPS2_CANBOBINTERLEAVED),
1031       FE(DDCAPS2_CANBOBNONINTERLEAVED),
1032       FE(DDCAPS2_COLORCONTROLOVERLAY),
1033       FE(DDCAPS2_COLORCONTROLPRIMARY),
1034       FE(DDCAPS2_CANDROPZ16BIT),
1035       FE(DDCAPS2_NONLOCALVIDMEM),
1036       FE(DDCAPS2_NONLOCALVIDMEMCAPS),
1037       FE(DDCAPS2_NOPAGELOCKREQUIRED),
1038       FE(DDCAPS2_WIDESURFACES),
1039       FE(DDCAPS2_CANFLIPODDEVEN),
1040       FE(DDCAPS2_CANBOBHARDWARE),
1041       FE(DDCAPS2_COPYFOURCC),
1042       FE(DDCAPS2_PRIMARYGAMMA),
1043       FE(DDCAPS2_CANRENDERWINDOWED),
1044       FE(DDCAPS2_CANCALIBRATEGAMMA),
1045       FE(DDCAPS2_FLIPINTERVAL),
1046       FE(DDCAPS2_FLIPNOVSYNC),
1047       FE(DDCAPS2_CANMANAGETEXTURE),
1048       FE(DDCAPS2_TEXMANINNONLOCALVIDMEM),
1049       FE(DDCAPS2_STEREO),
1050       FE(DDCAPS2_SYSTONONLOCAL_AS_SYSTOLOCAL)
1051     };
1052     static const flag_info flags3[] =
1053     {
1054       FE(DDCKEYCAPS_DESTBLT),
1055       FE(DDCKEYCAPS_DESTBLTCLRSPACE),
1056       FE(DDCKEYCAPS_DESTBLTCLRSPACEYUV),
1057       FE(DDCKEYCAPS_DESTBLTYUV),
1058       FE(DDCKEYCAPS_DESTOVERLAY),
1059       FE(DDCKEYCAPS_DESTOVERLAYCLRSPACE),
1060       FE(DDCKEYCAPS_DESTOVERLAYCLRSPACEYUV),
1061       FE(DDCKEYCAPS_DESTOVERLAYONEACTIVE),
1062       FE(DDCKEYCAPS_DESTOVERLAYYUV),
1063       FE(DDCKEYCAPS_SRCBLT),
1064       FE(DDCKEYCAPS_SRCBLTCLRSPACE),
1065       FE(DDCKEYCAPS_SRCBLTCLRSPACEYUV),
1066       FE(DDCKEYCAPS_SRCBLTYUV),
1067       FE(DDCKEYCAPS_SRCOVERLAY),
1068       FE(DDCKEYCAPS_SRCOVERLAYCLRSPACE),
1069       FE(DDCKEYCAPS_SRCOVERLAYCLRSPACEYUV),
1070       FE(DDCKEYCAPS_SRCOVERLAYONEACTIVE),
1071       FE(DDCKEYCAPS_SRCOVERLAYYUV),
1072       FE(DDCKEYCAPS_NOCOSTOVERLAY)
1073     };
1074     static const flag_info flags4[] =
1075     {
1076       FE(DDFXCAPS_BLTALPHA),
1077       FE(DDFXCAPS_OVERLAYALPHA),
1078       FE(DDFXCAPS_BLTARITHSTRETCHYN),
1079       FE(DDFXCAPS_BLTARITHSTRETCHY),
1080       FE(DDFXCAPS_BLTMIRRORLEFTRIGHT),
1081       FE(DDFXCAPS_BLTMIRRORUPDOWN),
1082       FE(DDFXCAPS_BLTROTATION),
1083       FE(DDFXCAPS_BLTROTATION90),
1084       FE(DDFXCAPS_BLTSHRINKX),
1085       FE(DDFXCAPS_BLTSHRINKXN),
1086       FE(DDFXCAPS_BLTSHRINKY),
1087       FE(DDFXCAPS_BLTSHRINKYN),
1088       FE(DDFXCAPS_BLTSTRETCHX),
1089       FE(DDFXCAPS_BLTSTRETCHXN),
1090       FE(DDFXCAPS_BLTSTRETCHY),
1091       FE(DDFXCAPS_BLTSTRETCHYN),
1092       FE(DDFXCAPS_OVERLAYARITHSTRETCHY),
1093       FE(DDFXCAPS_OVERLAYARITHSTRETCHYN),
1094       FE(DDFXCAPS_OVERLAYSHRINKX),
1095       FE(DDFXCAPS_OVERLAYSHRINKXN),
1096       FE(DDFXCAPS_OVERLAYSHRINKY),
1097       FE(DDFXCAPS_OVERLAYSHRINKYN),
1098       FE(DDFXCAPS_OVERLAYSTRETCHX),
1099       FE(DDFXCAPS_OVERLAYSTRETCHXN),
1100       FE(DDFXCAPS_OVERLAYSTRETCHY),
1101       FE(DDFXCAPS_OVERLAYSTRETCHYN),
1102       FE(DDFXCAPS_OVERLAYMIRRORLEFTRIGHT),
1103       FE(DDFXCAPS_OVERLAYMIRRORUPDOWN)
1104     };
1105     static const flag_info flags5[] =
1106     {
1107       FE(DDFXALPHACAPS_BLTALPHAEDGEBLEND),
1108       FE(DDFXALPHACAPS_BLTALPHAPIXELS),
1109       FE(DDFXALPHACAPS_BLTALPHAPIXELSNEG),
1110       FE(DDFXALPHACAPS_BLTALPHASURFACES),
1111       FE(DDFXALPHACAPS_BLTALPHASURFACESNEG),
1112       FE(DDFXALPHACAPS_OVERLAYALPHAEDGEBLEND),
1113       FE(DDFXALPHACAPS_OVERLAYALPHAPIXELS),
1114       FE(DDFXALPHACAPS_OVERLAYALPHAPIXELSNEG),
1115       FE(DDFXALPHACAPS_OVERLAYALPHASURFACES),
1116       FE(DDFXALPHACAPS_OVERLAYALPHASURFACESNEG)
1117     };
1118     static const flag_info flags6[] =
1119     {
1120       FE(DDPCAPS_4BIT),
1121       FE(DDPCAPS_8BITENTRIES),
1122       FE(DDPCAPS_8BIT),
1123       FE(DDPCAPS_INITIALIZE),
1124       FE(DDPCAPS_PRIMARYSURFACE),
1125       FE(DDPCAPS_PRIMARYSURFACELEFT),
1126       FE(DDPCAPS_ALLOW256),
1127       FE(DDPCAPS_VSYNC),
1128       FE(DDPCAPS_1BIT),
1129       FE(DDPCAPS_2BIT),
1130       FE(DDPCAPS_ALPHA),
1131     };
1132     static const flag_info flags7[] =
1133     {
1134       FE(DDSVCAPS_RESERVED1),
1135       FE(DDSVCAPS_RESERVED2),
1136       FE(DDSVCAPS_RESERVED3),
1137       FE(DDSVCAPS_RESERVED4),
1138       FE(DDSVCAPS_STEREOSEQUENTIAL),
1139     };
1140
1141     TRACE(" - dwSize : %d\n", lpcaps->dwSize);
1142     TRACE(" - dwCaps : "); DDRAW_dump_flags(lpcaps->dwCaps, flags1, sizeof(flags1)/sizeof(flags1[0]));
1143     TRACE(" - dwCaps2 : "); DDRAW_dump_flags(lpcaps->dwCaps2, flags2, sizeof(flags2)/sizeof(flags2[0]));
1144     TRACE(" - dwCKeyCaps : "); DDRAW_dump_flags(lpcaps->dwCKeyCaps, flags3, sizeof(flags3)/sizeof(flags3[0]));
1145     TRACE(" - dwFXCaps : "); DDRAW_dump_flags(lpcaps->dwFXCaps, flags4, sizeof(flags4)/sizeof(flags4[0]));
1146     TRACE(" - dwFXAlphaCaps : "); DDRAW_dump_flags(lpcaps->dwFXAlphaCaps, flags5, sizeof(flags5)/sizeof(flags5[0]));
1147     TRACE(" - dwPalCaps : "); DDRAW_dump_flags(lpcaps->dwPalCaps, flags6, sizeof(flags6)/sizeof(flags6[0]));
1148     TRACE(" - dwSVCaps : "); DDRAW_dump_flags(lpcaps->dwSVCaps, flags7, sizeof(flags7)/sizeof(flags7[0]));
1149     TRACE("...\n");
1150     TRACE(" - dwNumFourCCCodes : %d\n", lpcaps->dwNumFourCCCodes);
1151     TRACE(" - dwCurrVisibleOverlays : %d\n", lpcaps->dwCurrVisibleOverlays);
1152     TRACE(" - dwMinOverlayStretch : %d\n", lpcaps->dwMinOverlayStretch);
1153     TRACE(" - dwMaxOverlayStretch : %d\n", lpcaps->dwMaxOverlayStretch);
1154     TRACE("...\n");
1155     TRACE(" - ddsCaps : "); DDRAW_dump_DDSCAPS2(&lpcaps->ddsCaps);
1156 }
1157
1158 /*****************************************************************************
1159  * multiply_matrix
1160  *
1161  * Multiplies 2 4x4 matrices src1 and src2, and stores the result in dest.
1162  *
1163  * Params:
1164  *  dest: Pointer to the destination matrix
1165  *  src1: Pointer to the first source matrix
1166  *  src2: Pointer to the second source matrix
1167  *
1168  *****************************************************************************/
1169 void
1170 multiply_matrix(D3DMATRIX *dest,
1171                 const D3DMATRIX *src1,
1172                 const D3DMATRIX *src2)
1173 {
1174     D3DMATRIX temp;
1175
1176     /* Now do the multiplication 'by hand'.
1177        I know that all this could be optimised, but this will be done later :-) */
1178     temp._11 = (src1->_11 * src2->_11) + (src1->_21 * src2->_12) + (src1->_31 * src2->_13) + (src1->_41 * src2->_14);
1179     temp._21 = (src1->_11 * src2->_21) + (src1->_21 * src2->_22) + (src1->_31 * src2->_23) + (src1->_41 * src2->_24);
1180     temp._31 = (src1->_11 * src2->_31) + (src1->_21 * src2->_32) + (src1->_31 * src2->_33) + (src1->_41 * src2->_34);
1181     temp._41 = (src1->_11 * src2->_41) + (src1->_21 * src2->_42) + (src1->_31 * src2->_43) + (src1->_41 * src2->_44);
1182
1183     temp._12 = (src1->_12 * src2->_11) + (src1->_22 * src2->_12) + (src1->_32 * src2->_13) + (src1->_42 * src2->_14);
1184     temp._22 = (src1->_12 * src2->_21) + (src1->_22 * src2->_22) + (src1->_32 * src2->_23) + (src1->_42 * src2->_24);
1185     temp._32 = (src1->_12 * src2->_31) + (src1->_22 * src2->_32) + (src1->_32 * src2->_33) + (src1->_42 * src2->_34);
1186     temp._42 = (src1->_12 * src2->_41) + (src1->_22 * src2->_42) + (src1->_32 * src2->_43) + (src1->_42 * src2->_44);
1187
1188     temp._13 = (src1->_13 * src2->_11) + (src1->_23 * src2->_12) + (src1->_33 * src2->_13) + (src1->_43 * src2->_14);
1189     temp._23 = (src1->_13 * src2->_21) + (src1->_23 * src2->_22) + (src1->_33 * src2->_23) + (src1->_43 * src2->_24);
1190     temp._33 = (src1->_13 * src2->_31) + (src1->_23 * src2->_32) + (src1->_33 * src2->_33) + (src1->_43 * src2->_34);
1191     temp._43 = (src1->_13 * src2->_41) + (src1->_23 * src2->_42) + (src1->_33 * src2->_43) + (src1->_43 * src2->_44);
1192
1193     temp._14 = (src1->_14 * src2->_11) + (src1->_24 * src2->_12) + (src1->_34 * src2->_13) + (src1->_44 * src2->_14);
1194     temp._24 = (src1->_14 * src2->_21) + (src1->_24 * src2->_22) + (src1->_34 * src2->_23) + (src1->_44 * src2->_24);
1195     temp._34 = (src1->_14 * src2->_31) + (src1->_24 * src2->_32) + (src1->_34 * src2->_33) + (src1->_44 * src2->_34);
1196     temp._44 = (src1->_14 * src2->_41) + (src1->_24 * src2->_42) + (src1->_34 * src2->_43) + (src1->_44 * src2->_44);
1197
1198     /* And copy the new matrix in the good storage.. */
1199     memcpy(dest, &temp, 16 * sizeof(D3DVALUE));
1200 }
1201
1202 void multiply_matrix_D3D_way(D3DMATRIX* result, const D3DMATRIX *m1, const D3DMATRIX *m2)
1203 {
1204     D3DMATRIX temp;
1205
1206     temp._11 = m1->_11 * m2->_11 + m1->_12 * m2->_21 + m1->_13 * m2->_31 + m1->_14 * m2->_41;
1207     temp._12 = m1->_11 * m2->_12 + m1->_12 * m2->_22 + m1->_13 * m2->_32 + m1->_14 * m2->_42;
1208     temp._13 = m1->_11 * m2->_13 + m1->_12 * m2->_23 + m1->_13 * m2->_33 + m1->_14 * m2->_43;
1209     temp._14 = m1->_11 * m2->_14 + m1->_12 * m2->_24 + m1->_13 * m2->_34 + m1->_14 * m2->_44;
1210     temp._21 = m1->_21 * m2->_11 + m1->_22 * m2->_21 + m1->_23 * m2->_31 + m1->_24 * m2->_41;
1211     temp._22 = m1->_21 * m2->_12 + m1->_22 * m2->_22 + m1->_23 * m2->_32 + m1->_24 * m2->_42;
1212     temp._23 = m1->_21 * m2->_13 + m1->_22 * m2->_23 + m1->_23 * m2->_33 + m1->_24 * m2->_43;
1213     temp._24 = m1->_21 * m2->_14 + m1->_22 * m2->_24 + m1->_23 * m2->_34 + m1->_24 * m2->_44;
1214     temp._31 = m1->_31 * m2->_11 + m1->_32 * m2->_21 + m1->_33 * m2->_31 + m1->_34 * m2->_41;
1215     temp._32 = m1->_31 * m2->_12 + m1->_32 * m2->_22 + m1->_33 * m2->_32 + m1->_34 * m2->_42;
1216     temp._33 = m1->_31 * m2->_13 + m1->_32 * m2->_23 + m1->_33 * m2->_33 + m1->_34 * m2->_43;
1217     temp._34 = m1->_31 * m2->_14 + m1->_32 * m2->_24 + m1->_33 * m2->_34 + m1->_34 * m2->_44;
1218     temp._41 = m1->_41 * m2->_11 + m1->_42 * m2->_21 + m1->_43 * m2->_31 + m1->_44 * m2->_41;
1219     temp._42 = m1->_41 * m2->_12 + m1->_42 * m2->_22 + m1->_43 * m2->_32 + m1->_44 * m2->_42;
1220     temp._43 = m1->_41 * m2->_13 + m1->_42 * m2->_23 + m1->_43 * m2->_33 + m1->_44 * m2->_43;
1221     temp._44 = m1->_41 * m2->_14 + m1->_42 * m2->_24 + m1->_43 * m2->_34 + m1->_44 * m2->_44;
1222
1223     *result = temp;
1224
1225     return;
1226 }
1227
1228 HRESULT
1229 hr_ddraw_from_wined3d(HRESULT hr)
1230 {
1231     switch(hr)
1232     {
1233         case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1234         default: return hr;
1235     }
1236 }