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