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