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