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