msxml3: Reorganize parse_buffer function (make use of libxml2 parserInternals.h).
[wine] / dlls / wined3d / utils.c
1 /*
2  * Utility functions for the WineD3D Library
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2003-2004 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  * Copyright 2005 Oliver Stieber
8  * Copyright 2006-2007 Henri Verbeet
9  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25
26 #include "config.h"
27 #include "wined3d_private.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30
31 /*****************************************************************************
32  * Pixel format array
33  *
34  * For the formats WINED3DFMT_A32B32G32R32F, WINED3DFMT_A16B16G16R16F,
35  * and WINED3DFMT_A16B16G16R16 do not have correct alpha masks, because the
36  * high masks do not fit into the 32 bit values needed for ddraw. It is only
37  * used for ddraw mostly, and to figure out if the format has alpha at all, so
38  * setting a mask like 0x1 for those surfaces is correct. The 64 and 128 bit
39  * formats are not usable in 2D rendering because ddraw doesn't support them.
40  */
41 static const StaticPixelFormatDesc formats[] = {
42   /*{WINED3DFORMAT          ,alphamask  ,redmask    ,greenmask  ,bluemask   ,bpp    ,depth  ,stencil,    isFourcc*/
43     {WINED3DFMT_UNKNOWN     ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,0      ,0          ,FALSE },
44     /* FourCC formats, kept here to have WINED3DFMT_R8G8B8(=20) at position 20 */
45     {WINED3DFMT_UYVY        ,0x0        ,0x0        ,0x0        ,0x0        ,1/*?*/ ,0      ,0          ,TRUE  },
46     {WINED3DFMT_YUY2        ,0x0        ,0x0        ,0x0        ,0x0        ,1/*?*/ ,0      ,0          ,TRUE  },
47     {WINED3DFMT_YV12        ,0x0        ,0x0        ,0x0        ,0x0        ,1/*?*/ ,0      ,0          ,TRUE  },
48     {WINED3DFMT_DXT1        ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,0      ,0          ,TRUE  },
49     {WINED3DFMT_DXT2        ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,0      ,0          ,TRUE  },
50     {WINED3DFMT_DXT3        ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,0      ,0          ,TRUE  },
51     {WINED3DFMT_DXT4        ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,0      ,0          ,TRUE  },
52     {WINED3DFMT_DXT5        ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,0      ,0          ,TRUE  },
53     {WINED3DFMT_MULTI2_ARGB8,0x0        ,0x0        ,0x0        ,0x0        ,1/*?*/ ,0      ,0          ,TRUE  },
54     {WINED3DFMT_G8R8_G8B8   ,0x0        ,0x0        ,0x0        ,0x0        ,1/*?*/ ,0      ,0          ,TRUE  },
55     {WINED3DFMT_R8G8_B8G8   ,0x0        ,0x0        ,0x0        ,0x0        ,1/*?*/ ,0      ,0          ,TRUE  },
56     /* IEEE formats */
57     {WINED3DFMT_R32F        ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,0      ,0          ,FALSE },
58     {WINED3DFMT_G32R32F     ,0x0        ,0x0        ,0x0        ,0x0        ,8      ,0      ,0          ,FALSE },
59     {WINED3DFMT_A32B32G32R32F,0x1       ,0x0        ,0x0        ,0x0        ,16     ,0      ,0          ,FALSE },
60     /* Hmm? */
61     {WINED3DFMT_CxV8U8      ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,0      ,0          ,FALSE },
62     /* Float */
63     {WINED3DFMT_R16F        ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,0      ,0          ,FALSE },
64     {WINED3DFMT_G16R16F     ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,0      ,0          ,FALSE },
65     {WINED3DFMT_A16B16G16R16F,0x1       ,0x0        ,0x0        ,0x0        ,8      ,0      ,0          ,FALSE },
66     /* Palettized formats */
67     {WINED3DFMT_A8P8        ,0x0000ff00 ,0x0        ,0x0        ,0x0        ,2      ,0      ,0          ,FALSE },
68     {WINED3DFMT_P8          ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,0      ,0          ,FALSE },
69     /* Standard ARGB formats. Keep WINED3DFMT_R8G8B8(=20) at position 20 */
70     {WINED3DFMT_R8G8B8      ,0x0        ,0x00ff0000 ,0x0000ff00 ,0x000000ff ,3      ,0      ,0          ,FALSE },
71     {WINED3DFMT_A8R8G8B8    ,0xff000000 ,0x00ff0000 ,0x0000ff00 ,0x000000ff ,4      ,0      ,0          ,FALSE },
72     {WINED3DFMT_X8R8G8B8    ,0x0        ,0x00ff0000 ,0x0000ff00 ,0x000000ff ,4      ,0      ,0          ,FALSE },
73     {WINED3DFMT_R5G6B5      ,0x0        ,0x0000F800 ,0x000007e0 ,0x0000001f ,2      ,0      ,0          ,FALSE },
74     {WINED3DFMT_X1R5G5B5    ,0x0        ,0x00007c00 ,0x000003e0 ,0x0000001f ,2      ,0      ,0          ,FALSE },
75     {WINED3DFMT_A1R5G5B5    ,0x00008000 ,0x00007c00 ,0x000003e0 ,0x0000001f ,2      ,0      ,0          ,FALSE },
76     {WINED3DFMT_A4R4G4B4    ,0x0000f000 ,0x00000f00 ,0x000000f0 ,0x0000000f ,2      ,0      ,0          ,FALSE },
77     {WINED3DFMT_R3G3B2      ,0x0        ,0x000000e0 ,0x0000001c ,0x00000003 ,1      ,0      ,0          ,FALSE },
78     {WINED3DFMT_A8          ,0x000000ff ,0x0        ,0x0        ,0x0        ,1      ,0      ,0          ,FALSE },
79     {WINED3DFMT_A8R3G3B2    ,0x0000ff00 ,0x000000e0 ,0x0000001c ,0x00000003 ,2      ,0      ,0          ,FALSE },
80     {WINED3DFMT_X4R4G4B4    ,0x0        ,0x00000f00 ,0x000000f0 ,0x0000000f ,2      ,0      ,0          ,FALSE },
81     {WINED3DFMT_A2B10G10R10 ,0xb0000000 ,0x000003ff ,0x000ffc00 ,0x3ff00000 ,4      ,0      ,0          ,FALSE },
82     {WINED3DFMT_A8B8G8R8    ,0xff000000 ,0x000000ff ,0x0000ff00 ,0x00ff0000 ,4      ,0      ,0          ,FALSE },
83     {WINED3DFMT_X8B8G8R8    ,0x0        ,0x000000ff ,0x0000ff00 ,0x00ff0000 ,4      ,0      ,0          ,FALSE },
84     {WINED3DFMT_G16R16      ,0x0        ,0x0000ffff ,0xffff0000 ,0x0        ,4      ,0      ,0          ,FALSE },
85     {WINED3DFMT_A2R10G10B10 ,0xb0000000 ,0x3ff00000 ,0x000ffc00 ,0x000003ff ,4      ,0      ,0          ,FALSE },
86     {WINED3DFMT_A16B16G16R16,0x1        ,0x0000ffff ,0xffff0000 ,0x0        ,8      ,0      ,0          ,FALSE },
87     /* Luminance */
88     {WINED3DFMT_L8          ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,0      ,0          ,FALSE },
89     {WINED3DFMT_A8L8        ,0x0000ff00 ,0x0        ,0x0        ,0x0        ,2      ,0      ,0          ,FALSE },
90     {WINED3DFMT_A4L4        ,0x000000f0 ,0x0        ,0x0        ,0x0        ,1      ,0      ,0          ,FALSE },
91     /* Bump mapping stuff */
92     {WINED3DFMT_V8U8        ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,0      ,0          ,FALSE },
93     {WINED3DFMT_L6V5U5      ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,0      ,0          ,FALSE },
94     {WINED3DFMT_X8L8V8U8    ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,0      ,0          ,FALSE },
95     {WINED3DFMT_Q8W8V8U8    ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,0      ,0          ,FALSE },
96     {WINED3DFMT_V16U16      ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,0      ,0          ,FALSE },
97     {WINED3DFMT_W11V11U10   ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,0      ,0          ,FALSE },
98     {WINED3DFMT_A2W10V10U10 ,0xb0000000 ,0x0        ,0x0        ,0x0        ,4      ,0      ,0          ,FALSE },
99     /* Depth stencil formats */
100     {WINED3DFMT_D16_LOCKABLE,0x0        ,0x0        ,0x0        ,0x0        ,2      ,16     ,0          ,FALSE },
101     {WINED3DFMT_D32         ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,32     ,0          ,FALSE },
102     {WINED3DFMT_D15S1       ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,15     ,1          ,FALSE },
103     {WINED3DFMT_D24S8       ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,24     ,8          ,FALSE },
104     {WINED3DFMT_D24X8       ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,24     ,0          ,FALSE },
105     {WINED3DFMT_D24X4S4     ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,24     ,4          ,FALSE },
106     {WINED3DFMT_D16         ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,16     ,0          ,FALSE },
107     {WINED3DFMT_L16         ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,16      ,0          ,FALSE },
108     {WINED3DFMT_D32F_LOCKABLE,0x0       ,0x0        ,0x0        ,0x0        ,4      ,32     ,0          ,FALSE },
109     {WINED3DFMT_D24FS8      ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,24     ,8          ,FALSE },
110     /* Is this a vertex buffer? */
111     {WINED3DFMT_VERTEXDATA  ,0x0        ,0x0        ,0x0        ,0x0        ,0      ,0      ,0          ,FALSE },
112     {WINED3DFMT_INDEX16     ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,0      ,0          ,FALSE },
113     {WINED3DFMT_INDEX32     ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,0      ,0          ,FALSE },
114     {WINED3DFMT_Q16W16V16U16,0x0        ,0x0        ,0x0        ,0x0        ,8      ,0      ,0          ,FALSE },
115     /* Vendor-specific formats */
116     {WINED3DFMT_ATI2N       ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,0      ,0          ,TRUE  },
117 };
118
119 typedef struct {
120     WINED3DFORMAT           fmt;
121     GLint                   glInternal, glGammaInternal, rtInternal, glFormat, glType;
122     unsigned int            Flags;
123 } GlPixelFormatDescTemplate;
124
125 /*****************************************************************************
126  * OpenGL format template. Contains unexciting formats which do not need
127  * extension checks. The order in this table is independent of the order in
128  * the table StaticPixelFormatDesc above. Not all formats have to be in this
129  * table.
130  */
131 static const GlPixelFormatDescTemplate gl_formats_template[] = {
132   /*{                           internal                         ,srgbInternal                           , rtInternal,  format                    ,type \
133         ,Flags }*/
134     {WINED3DFMT_UNKNOWN        ,0                                ,0                                      , 0,           0                         ,0
135         ,0 },
136     /* FourCC formats */
137     {WINED3DFMT_UYVY           ,0                                ,0                                      , 0,           0                         ,0
138         ,0 },
139     {WINED3DFMT_YUY2           ,0                                ,0                                      , 0,           0                         ,0
140         ,0 },
141     {WINED3DFMT_DXT1           ,GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ,GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT , 0,           GL_RGBA                   ,GL_UNSIGNED_BYTE
142         ,WINED3DFMT_FLAG_FILTERING },
143     {WINED3DFMT_DXT2           ,GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ,GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT , 0,           GL_RGBA                   ,GL_UNSIGNED_BYTE
144         ,WINED3DFMT_FLAG_FILTERING },
145     {WINED3DFMT_DXT3           ,GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ,GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT , 0,           GL_RGBA                   ,GL_UNSIGNED_BYTE
146         ,WINED3DFMT_FLAG_FILTERING },
147     {WINED3DFMT_DXT4           ,GL_COMPRESSED_RGBA_S3TC_DXT5_EXT ,GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT , 0,           GL_RGBA                   ,GL_UNSIGNED_BYTE
148         ,WINED3DFMT_FLAG_FILTERING },
149     {WINED3DFMT_DXT5           ,GL_COMPRESSED_RGBA_S3TC_DXT5_EXT ,GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT , 0,           GL_RGBA                   ,GL_UNSIGNED_BYTE
150         ,WINED3DFMT_FLAG_FILTERING },
151     {WINED3DFMT_MULTI2_ARGB8   ,0                                ,0                                      , 0,           0                         ,0
152         ,0 },
153     {WINED3DFMT_G8R8_G8B8      ,0                                ,0                                      , 0,           0                         ,0
154         ,0 },
155     {WINED3DFMT_R8G8_B8G8      ,0                                ,0                                      , 0,           0                         ,0
156         ,0 },
157     /* IEEE formats */
158     {WINED3DFMT_R32F           ,GL_RGB32F_ARB                    ,GL_RGB32F_ARB                          , 0,           GL_RED                    ,GL_FLOAT
159         ,WINED3DFMT_FLAG_RENDERTARGET },
160     {WINED3DFMT_G32R32F        ,0                                ,0                                      , 0,           0                         ,0
161         ,WINED3DFMT_FLAG_RENDERTARGET },
162     {WINED3DFMT_A32B32G32R32F  ,GL_RGBA32F_ARB                   ,GL_RGBA32F_ARB                         , 0,           GL_RGBA                   ,GL_FLOAT
163         ,WINED3DFMT_FLAG_RENDERTARGET },
164     /* Hmm? */
165     {WINED3DFMT_CxV8U8         ,0                                ,0                                      , 0,           0                         ,0
166         ,0 },
167     /* Float */
168     {WINED3DFMT_R16F           ,GL_RGB16F_ARB                    ,GL_RGB16F_ARB                          , 0,           GL_RED                    ,GL_HALF_FLOAT_ARB
169         ,WINED3DFMT_FLAG_FILTERING | WINED3DFMT_FLAG_RENDERTARGET },
170     {WINED3DFMT_G16R16F        ,0                                ,0                                      , 0,           0                         ,0
171         ,WINED3DFMT_FLAG_FILTERING | WINED3DFMT_FLAG_RENDERTARGET },
172     {WINED3DFMT_A16B16G16R16F  ,GL_RGBA16F_ARB                   ,GL_RGBA16F_ARB                         , 0,           GL_RGBA                   ,GL_HALF_FLOAT_ARB
173         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING | WINED3DFMT_FLAG_RENDERTARGET },
174     /* Palettized formats */
175     {WINED3DFMT_A8P8,           0                                ,0                                      , 0,           0                         ,0
176         ,0 },
177     {WINED3DFMT_P8,             GL_COLOR_INDEX8_EXT              ,GL_COLOR_INDEX8_EXT                    , 0,           GL_COLOR_INDEX            ,GL_UNSIGNED_BYTE
178         ,0 },
179     /* Standard ARGB formats */
180     {WINED3DFMT_R8G8B8         ,GL_RGB8                          ,GL_RGB8                                , 0,           GL_BGR                    ,GL_UNSIGNED_BYTE
181         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING | WINED3DFMT_FLAG_RENDERTARGET },
182     {WINED3DFMT_A8R8G8B8       ,GL_RGBA8                         ,GL_SRGB8_ALPHA8_EXT                    , 0,           GL_BGRA                   ,GL_UNSIGNED_INT_8_8_8_8_REV
183         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING | WINED3DFMT_FLAG_RENDERTARGET },
184     {WINED3DFMT_X8R8G8B8       ,GL_RGB8                          ,GL_SRGB8_EXT                           , 0,           GL_BGRA                   ,GL_UNSIGNED_INT_8_8_8_8_REV
185         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING | WINED3DFMT_FLAG_RENDERTARGET },
186     {WINED3DFMT_R5G6B5         ,GL_RGB5                          ,GL_RGB5                                , GL_RGB8,     GL_RGB                    ,GL_UNSIGNED_SHORT_5_6_5
187         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING | WINED3DFMT_FLAG_RENDERTARGET },
188     {WINED3DFMT_X1R5G5B5       ,GL_RGB5                          ,GL_RGB5_A1                             , 0,           GL_BGRA                   ,GL_UNSIGNED_SHORT_1_5_5_5_REV
189         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING },
190     {WINED3DFMT_A1R5G5B5       ,GL_RGB5_A1                       ,GL_RGB5_A1                             , 0,           GL_BGRA                   ,GL_UNSIGNED_SHORT_1_5_5_5_REV
191         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING },
192     {WINED3DFMT_A4R4G4B4       ,GL_RGBA4                         ,GL_SRGB8_ALPHA8_EXT                    , 0,           GL_BGRA                   ,GL_UNSIGNED_SHORT_4_4_4_4_REV
193         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING },
194     {WINED3DFMT_R3G3B2         ,GL_R3_G3_B2                      ,GL_R3_G3_B2                            , 0,           GL_RGB                    ,GL_UNSIGNED_BYTE_3_3_2
195         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING },
196     {WINED3DFMT_A8             ,GL_ALPHA8                        ,GL_ALPHA8                              , 0,           GL_ALPHA                  ,GL_UNSIGNED_BYTE
197         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING },
198     {WINED3DFMT_A8R3G3B2       ,0                                ,0                                      , 0,           0                         ,0
199         ,0 },
200     {WINED3DFMT_X4R4G4B4       ,GL_RGB4                          ,GL_RGB4                                , 0,           GL_BGRA                   ,GL_UNSIGNED_SHORT_4_4_4_4_REV
201         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING},
202     {WINED3DFMT_A2B10G10R10    ,GL_RGBA                          ,GL_RGBA                                , 0,           GL_RGBA                   ,GL_UNSIGNED_INT_2_10_10_10_REV
203         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING},
204     {WINED3DFMT_A8B8G8R8       ,GL_RGBA8                         ,GL_RGBA8                               , 0,           GL_RGBA                   ,GL_UNSIGNED_INT_8_8_8_8_REV
205         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING},
206     {WINED3DFMT_X8B8G8R8       ,GL_RGB8                          ,GL_RGB8                                , 0,           GL_RGBA                   ,GL_UNSIGNED_INT_8_8_8_8_REV
207         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING},
208     {WINED3DFMT_G16R16         ,GL_RGB16_EXT                     ,GL_RGB16_EXT                           , 0,           GL_RGB                    ,GL_UNSIGNED_SHORT
209         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING },
210     {WINED3DFMT_A2R10G10B10    ,GL_RGBA                          ,GL_RGBA                                , 0,           GL_BGRA                   ,GL_UNSIGNED_INT_2_10_10_10_REV
211         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING },
212     {WINED3DFMT_A16B16G16R16   ,GL_RGBA16_EXT                    ,GL_RGBA16_EXT                          , 0,           GL_RGBA                   ,GL_UNSIGNED_SHORT
213         ,WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING | WINED3DFMT_FLAG_RENDERTARGET },
214     /* Luminance */
215     {WINED3DFMT_L8             ,GL_LUMINANCE8                    ,GL_SLUMINANCE8_EXT                     , 0,           GL_LUMINANCE              ,GL_UNSIGNED_BYTE
216         ,WINED3DFMT_FLAG_FILTERING },
217     {WINED3DFMT_A8L8           ,GL_LUMINANCE8_ALPHA8             ,GL_SLUMINANCE8_ALPHA8_EXT              , 0,           GL_LUMINANCE_ALPHA        ,GL_UNSIGNED_BYTE
218         ,WINED3DFMT_FLAG_FILTERING },
219     {WINED3DFMT_A4L4           ,GL_LUMINANCE4_ALPHA4             ,GL_LUMINANCE4_ALPHA4                   , 0,           GL_LUMINANCE_ALPHA        ,GL_UNSIGNED_BYTE
220         ,0 },
221     /* Bump mapping stuff */
222     {WINED3DFMT_V8U8           ,GL_DSDT8_NV                      ,GL_DSDT8_NV                            , 0,           GL_DSDT_NV                ,GL_BYTE
223         ,WINED3DFMT_FLAG_FILTERING },
224     {WINED3DFMT_L6V5U5         ,GL_DSDT8_MAG8_NV                 ,GL_DSDT8_MAG8_NV                       , 0,           GL_DSDT_MAG_NV            ,GL_BYTE
225         ,WINED3DFMT_FLAG_FILTERING },
226     {WINED3DFMT_X8L8V8U8       ,GL_DSDT8_MAG8_INTENSITY8_NV      ,GL_DSDT8_MAG8_INTENSITY8_NV            , 0,           GL_DSDT_MAG_VIB_NV        ,GL_UNSIGNED_INT_8_8_S8_S8_REV_NV
227         ,WINED3DFMT_FLAG_FILTERING },
228     {WINED3DFMT_Q8W8V8U8       ,GL_SIGNED_RGBA8_NV               ,GL_SIGNED_RGBA8_NV                     , 0,           GL_RGBA                   ,GL_BYTE
229         ,WINED3DFMT_FLAG_FILTERING },
230     {WINED3DFMT_V16U16         ,GL_SIGNED_HILO16_NV              ,GL_SIGNED_HILO16_NV                    , 0,           GL_HILO_NV                ,GL_SHORT
231         ,WINED3DFMT_FLAG_FILTERING },
232     {WINED3DFMT_W11V11U10      ,0                                ,0                                      , 0,           0                         ,0
233         ,0 },
234     {WINED3DFMT_A2W10V10U10    ,0                                ,0                                      , 0,           0                         ,0
235         ,0 },
236     /* Depth stencil formats */
237     {WINED3DFMT_D16_LOCKABLE   ,GL_DEPTH_COMPONENT24_ARB         ,GL_DEPTH_COMPONENT24_ARB               , 0,           GL_DEPTH_COMPONENT        ,GL_UNSIGNED_SHORT
238         ,WINED3DFMT_FLAG_DEPTH },
239     {WINED3DFMT_D32            ,GL_DEPTH_COMPONENT32_ARB         ,GL_DEPTH_COMPONENT32_ARB               , 0,           GL_DEPTH_COMPONENT        ,GL_UNSIGNED_INT
240         ,WINED3DFMT_FLAG_DEPTH },
241     {WINED3DFMT_D15S1          ,GL_DEPTH_COMPONENT24_ARB         ,GL_DEPTH_COMPONENT24_ARB               , 0,           GL_DEPTH_COMPONENT        ,GL_UNSIGNED_SHORT
242         ,WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL },
243     {WINED3DFMT_D24S8          ,GL_DEPTH_COMPONENT24_ARB         ,GL_DEPTH_COMPONENT24_ARB               , 0,           GL_DEPTH_COMPONENT        ,GL_UNSIGNED_INT
244         ,WINED3DFMT_FLAG_FILTERING | WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL },
245     {WINED3DFMT_D24X8          ,GL_DEPTH_COMPONENT24_ARB         ,GL_DEPTH_COMPONENT24_ARB               , 0,           GL_DEPTH_COMPONENT        ,GL_UNSIGNED_INT
246         ,WINED3DFMT_FLAG_FILTERING | WINED3DFMT_FLAG_DEPTH },
247     {WINED3DFMT_D24X4S4        ,GL_DEPTH_COMPONENT24_ARB         ,GL_DEPTH_COMPONENT24_ARB               , 0,           GL_DEPTH_COMPONENT        ,GL_UNSIGNED_INT
248         ,WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL },
249     {WINED3DFMT_D16            ,GL_DEPTH_COMPONENT24_ARB         ,GL_DEPTH_COMPONENT24_ARB               , 0,           GL_DEPTH_COMPONENT        ,GL_UNSIGNED_SHORT
250         ,WINED3DFMT_FLAG_FILTERING | WINED3DFMT_FLAG_DEPTH },
251     {WINED3DFMT_L16            ,GL_LUMINANCE16_EXT               ,GL_LUMINANCE16_EXT                     , 0,           GL_LUMINANCE              ,GL_UNSIGNED_SHORT
252         ,WINED3DFMT_FLAG_FILTERING },
253     {WINED3DFMT_D32F_LOCKABLE  ,GL_DEPTH_COMPONENT32_ARB         ,GL_DEPTH_COMPONENT32_ARB               , 0,           GL_DEPTH_COMPONENT        ,GL_FLOAT
254         ,WINED3DFMT_FLAG_DEPTH },
255     {WINED3DFMT_D24FS8         ,GL_DEPTH_COMPONENT24_ARB         ,GL_DEPTH_COMPONENT24_ARB               , 0,           GL_DEPTH_COMPONENT        ,GL_FLOAT
256         ,WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL},
257     /* Is this a vertex buffer? */
258     {WINED3DFMT_VERTEXDATA     ,0                                ,0                                      , 0,           0                         ,0
259         ,0 },
260     {WINED3DFMT_INDEX16        ,0                                ,0                                      , 0,           0                         ,0
261         ,0 },
262     {WINED3DFMT_INDEX32        ,0                                ,0                                      , 0,           0                         ,0
263         ,0 },
264     {WINED3DFMT_Q16W16V16U16   ,GL_COLOR_INDEX                   ,GL_COLOR_INDEX                         , 0,           GL_COLOR_INDEX            ,GL_UNSIGNED_SHORT
265         ,0 },
266     /* Vendor-specific formats */
267     {WINED3DFMT_ATI2N          ,0                                ,0                                      , 0,           GL_LUMINANCE_ALPHA        ,GL_UNSIGNED_BYTE
268         ,0 }
269 };
270
271 static inline int getFmtIdx(WINED3DFORMAT fmt) {
272     /* First check if the format is at the position of its value.
273      * This will catch the argb formats before the loop is entered
274      */
275     if(fmt < (sizeof(formats) / sizeof(formats[0])) && formats[fmt].format == fmt) {
276         return fmt;
277     } else {
278         unsigned int i;
279         for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
280             if(formats[i].format == fmt) {
281                 return i;
282             }
283         }
284     }
285     return -1;
286 }
287
288 #define GLINFO_LOCATION (*gl_info)
289 BOOL initPixelFormats(WineD3D_GL_Info *gl_info)
290 {
291     unsigned int src;
292     int dst;
293
294     gl_info->gl_formats = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
295                                     sizeof(formats) / sizeof(formats[0]) * sizeof(gl_info->gl_formats[0]));
296     if(!gl_info->gl_formats) return FALSE;
297
298     /* If a format depends on some extensions, remove them from the table above and initialize them
299      * after this loop
300      */
301     for(src = 0; src < sizeof(gl_formats_template) / sizeof(gl_formats_template[0]); src++) {
302         dst = getFmtIdx(gl_formats_template[src].fmt);
303         gl_info->gl_formats[dst].glInternal      = gl_formats_template[src].glInternal;
304         gl_info->gl_formats[dst].glGammaInternal = gl_formats_template[src].glGammaInternal;
305         gl_info->gl_formats[dst].glFormat        = gl_formats_template[src].glFormat;
306         gl_info->gl_formats[dst].glType          = gl_formats_template[src].glType;
307         gl_info->gl_formats[dst].conversion_group= WINED3DFMT_UNKNOWN;
308         gl_info->gl_formats[dst].Flags           = gl_formats_template[src].Flags;
309
310         if(wined3d_settings.offscreen_rendering_mode == ORM_FBO &&
311            gl_formats_template[src].rtInternal != 0) {
312             GLuint tex, fb;
313             GLenum status;
314
315             /* Check if the default internal format is supported as a frame buffer target, otherwise
316              * fall back to the render target internal.
317              *
318              * Try to stick to the standard format if possible, this limits precision differences
319              */
320             while(glGetError());
321             glGenTextures(1, &tex);
322             glBindTexture(GL_TEXTURE_2D, tex);
323             glTexImage2D(GL_TEXTURE_2D, 0, gl_formats_template[src].glInternal, 16, 16, 0,
324                          GL_RGBA, GL_UNSIGNED_BYTE, NULL);
325
326             GL_EXTCALL(glGenFramebuffersEXT(1, &fb));
327             GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb));
328             GL_EXTCALL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
329                                                  GL_TEXTURE_2D, tex, 0));
330
331             status = GL_EXTCALL(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
332             GL_EXTCALL(glDeleteFramebuffersEXT(1, &fb));
333             glDeleteTextures(1, &tex);
334
335             checkGLcall("Framebuffer format check");
336
337             if(status != GL_FRAMEBUFFER_COMPLETE_EXT) {
338                 TRACE("Internal format of %s not supported as frame buffer target, using render target internal instead\n",
339                     debug_d3dformat(gl_formats_template[src].fmt));
340                 gl_info->gl_formats[dst].rtInternal = gl_formats_template[src].rtInternal;
341             } else {
342                 TRACE("Format %s is supported as fbo target\n", debug_d3dformat(gl_formats_template[src].fmt));
343                 gl_info->gl_formats[dst].rtInternal = gl_formats_template[src].glInternal;
344             }
345
346         } else {
347             gl_info->gl_formats[dst].rtInternal = gl_formats_template[src].glInternal;
348         }
349     }
350
351     /* V8U8 is supported natively by GL_ATI_envmap_bumpmap and GL_NV_texture_shader.
352      * V16U16 is only supported by GL_NV_texture_shader. The formats need fixup if
353      * their extensions are not available.
354      *
355      * In theory, V8U8 and V16U16 need a fixup of the undefined blue channel. OpenGL
356      * returns 0.0 when sampling from it, DirectX 1.0. This is disabled until we find
357      * an application that needs this because it causes performance problems due to
358      * shader recompiling in some games.
359      */
360     if(!GL_SUPPORT(ATI_ENVMAP_BUMPMAP) && !GL_SUPPORT(NV_TEXTURE_SHADER2)) {
361         /* signed -> unsigned fixup */
362         dst = getFmtIdx(WINED3DFMT_V8U8);
363         gl_info->gl_formats[dst].conversion_group = WINED3DFMT_V8U8;
364         dst = getFmtIdx(WINED3DFMT_V16U16);
365         gl_info->gl_formats[dst].conversion_group = WINED3DFMT_V8U8;
366     } else if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
367         /* signed -> unsigned fixup */
368         dst = getFmtIdx(WINED3DFMT_V16U16);
369         gl_info->gl_formats[dst].conversion_group = WINED3DFMT_V16U16;
370     } else {
371         /* Blue = 1.0 fixup, disabled for now */
372 #if 0
373         dst = getFmtIdx(WINED3DFMT_V8U8);
374         gl_info->gl_formats[dst].conversion_group = WINED3DFMT_V8U8;
375         dst = getFmtIdx(WINED3DFMT_V16U16);
376         gl_info->gl_formats[dst].conversion_group = WINED3DFMT_V8U8;
377 #endif
378     }
379
380     if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
381         /* If GL_NV_texture_shader is not supported, those formats are converted, incompatibly
382          * with each other
383          */
384         dst = getFmtIdx(WINED3DFMT_L6V5U5);
385         gl_info->gl_formats[dst].conversion_group = WINED3DFMT_L6V5U5;
386         dst = getFmtIdx(WINED3DFMT_X8L8V8U8);
387         gl_info->gl_formats[dst].conversion_group = WINED3DFMT_X8L8V8U8;
388         dst = getFmtIdx(WINED3DFMT_Q8W8V8U8);
389         gl_info->gl_formats[dst].conversion_group = WINED3DFMT_Q8W8V8U8;
390     } else {
391         /* If GL_NV_texture_shader is supported, WINED3DFMT_L6V5U5 and WINED3DFMT_X8L8V8U8
392          * are converted at surface loading time, but they do not need any modification in
393          * the shader, thus they are compatible with all WINED3DFMT_UNKNOWN group formats.
394          * WINED3DFMT_Q8W8V8U8 doesn't even need load-time conversion
395          */
396     }
397
398     if(GL_SUPPORT(EXT_TEXTURE_COMPRESSION_RGTC)) {
399         dst = getFmtIdx(WINED3DFMT_ATI2N);
400         gl_info->gl_formats[dst].glInternal = GL_COMPRESSED_RED_GREEN_RGTC2_EXT;
401         gl_info->gl_formats[dst].glGammaInternal = GL_COMPRESSED_RED_GREEN_RGTC2_EXT;
402         gl_info->gl_formats[dst].conversion_group= WINED3DFMT_ATI2N;
403     } else if(GL_SUPPORT(ATI_TEXTURE_COMPRESSION_3DC)) {
404         dst = getFmtIdx(WINED3DFMT_ATI2N);
405         gl_info->gl_formats[dst].glInternal = GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI;
406         gl_info->gl_formats[dst].glGammaInternal = GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI;
407         gl_info->gl_formats[dst].conversion_group= WINED3DFMT_ATI2N;
408     }
409
410     return TRUE;
411 }
412
413 /* NOTE: Make sure these are in the correct numerical order. (see /include/wined3d_types.h) */
414 static WINED3DGLTYPE const glTypeLookupTemplate[WINED3DDECLTYPE_UNUSED] = {
415                                   {WINED3DDECLTYPE_FLOAT1,    1, GL_FLOAT           , GL_FALSE ,sizeof(float)},
416                                   {WINED3DDECLTYPE_FLOAT2,    2, GL_FLOAT           , GL_FALSE ,sizeof(float)},
417                                   {WINED3DDECLTYPE_FLOAT3,    3, GL_FLOAT           , GL_FALSE ,sizeof(float)},
418                                   {WINED3DDECLTYPE_FLOAT4,    4, GL_FLOAT           , GL_FALSE ,sizeof(float)},
419                                   {WINED3DDECLTYPE_D3DCOLOR,  4, GL_UNSIGNED_BYTE   , GL_TRUE  ,sizeof(BYTE)},
420                                   {WINED3DDECLTYPE_UBYTE4,    4, GL_UNSIGNED_BYTE   , GL_FALSE ,sizeof(BYTE)},
421                                   {WINED3DDECLTYPE_SHORT2,    2, GL_SHORT           , GL_FALSE ,sizeof(short int)},
422                                   {WINED3DDECLTYPE_SHORT4,    4, GL_SHORT           , GL_FALSE ,sizeof(short int)},
423                                   {WINED3DDECLTYPE_UBYTE4N,   4, GL_UNSIGNED_BYTE   , GL_TRUE  ,sizeof(BYTE)},
424                                   {WINED3DDECLTYPE_SHORT2N,   2, GL_SHORT           , GL_TRUE  ,sizeof(short int)},
425                                   {WINED3DDECLTYPE_SHORT4N,   4, GL_SHORT           , GL_TRUE  ,sizeof(short int)},
426                                   {WINED3DDECLTYPE_USHORT2N,  2, GL_UNSIGNED_SHORT  , GL_TRUE  ,sizeof(short int)},
427                                   {WINED3DDECLTYPE_USHORT4N,  4, GL_UNSIGNED_SHORT  , GL_TRUE  ,sizeof(short int)},
428                                   {WINED3DDECLTYPE_UDEC3,     3, GL_UNSIGNED_SHORT  , GL_FALSE ,sizeof(short int)},
429                                   {WINED3DDECLTYPE_DEC3N,     3, GL_SHORT           , GL_TRUE  ,sizeof(short int)},
430                                   {WINED3DDECLTYPE_FLOAT16_2, 2, GL_HALF_FLOAT_NV   , GL_FALSE ,sizeof(GLhalfNV)},
431                                   {WINED3DDECLTYPE_FLOAT16_4, 4, GL_HALF_FLOAT_NV   , GL_FALSE ,sizeof(GLhalfNV)}};
432
433 void init_type_lookup(WineD3D_GL_Info *gl_info) {
434     memcpy(gl_info->glTypeLookup, glTypeLookupTemplate, sizeof(glTypeLookupTemplate));
435     if(!GL_SUPPORT(NV_HALF_FLOAT)) {
436         /* Do not change the size of the type, it is CPU side. We have to change the GPU-side information though.
437          * It is the job of the vertex buffer code to make sure that the vbos have the right format
438          */
439         gl_info->glTypeLookup[WINED3DDECLTYPE_FLOAT16_2].glType = GL_FLOAT;
440         gl_info->glTypeLookup[WINED3DDECLTYPE_FLOAT16_4].glType = GL_FLOAT;
441     }
442 }
443
444 #undef GLINFO_LOCATION
445
446 #define GLINFO_LOCATION This->adapter->gl_info
447
448 const StaticPixelFormatDesc *getFormatDescEntry(WINED3DFORMAT fmt, WineD3D_GL_Info *gl_info, const GlPixelFormatDesc **glDesc)
449 {
450     int idx = getFmtIdx(fmt);
451
452     if(idx == -1) {
453         FIXME("Can't find format %s(%d) in the format lookup table\n", debug_d3dformat(fmt), fmt);
454         /* Get the caller a valid pointer */
455         idx = getFmtIdx(WINED3DFMT_UNKNOWN);
456     }
457     if(glDesc) {
458         if(!gl_info->gl_formats) {
459             /* If we do not have gl format information, provide a dummy NULL format. This is an easy way to make
460              * all gl caps check return "unsupported" than catching the lack of gl all over the code. ANSI C requires
461              * static variables to be initialized to 0.
462              */
463             static const GlPixelFormatDesc dummyFmt;
464             *glDesc = &dummyFmt;
465         } else {
466             *glDesc = &gl_info->gl_formats[idx];
467         }
468     }
469     return &formats[idx];
470 }
471
472 /*****************************************************************************
473  * Trace formatting of useful values
474  */
475 const char* debug_d3dformat(WINED3DFORMAT fmt) {
476   switch (fmt) {
477 #define FMT_TO_STR(fmt) case fmt: return #fmt
478     FMT_TO_STR(WINED3DFMT_UNKNOWN);
479     FMT_TO_STR(WINED3DFMT_R8G8B8);
480     FMT_TO_STR(WINED3DFMT_A8R8G8B8);
481     FMT_TO_STR(WINED3DFMT_X8R8G8B8);
482     FMT_TO_STR(WINED3DFMT_R5G6B5);
483     FMT_TO_STR(WINED3DFMT_X1R5G5B5);
484     FMT_TO_STR(WINED3DFMT_A1R5G5B5);
485     FMT_TO_STR(WINED3DFMT_A4R4G4B4);
486     FMT_TO_STR(WINED3DFMT_R3G3B2);
487     FMT_TO_STR(WINED3DFMT_A8);
488     FMT_TO_STR(WINED3DFMT_A8R3G3B2);
489     FMT_TO_STR(WINED3DFMT_X4R4G4B4);
490     FMT_TO_STR(WINED3DFMT_A2B10G10R10);
491     FMT_TO_STR(WINED3DFMT_A8B8G8R8);
492     FMT_TO_STR(WINED3DFMT_X8B8G8R8);
493     FMT_TO_STR(WINED3DFMT_G16R16);
494     FMT_TO_STR(WINED3DFMT_A2R10G10B10);
495     FMT_TO_STR(WINED3DFMT_A16B16G16R16);
496     FMT_TO_STR(WINED3DFMT_A8P8);
497     FMT_TO_STR(WINED3DFMT_P8);
498     FMT_TO_STR(WINED3DFMT_L8);
499     FMT_TO_STR(WINED3DFMT_A8L8);
500     FMT_TO_STR(WINED3DFMT_A4L4);
501     FMT_TO_STR(WINED3DFMT_V8U8);
502     FMT_TO_STR(WINED3DFMT_L6V5U5);
503     FMT_TO_STR(WINED3DFMT_X8L8V8U8);
504     FMT_TO_STR(WINED3DFMT_Q8W8V8U8);
505     FMT_TO_STR(WINED3DFMT_V16U16);
506     FMT_TO_STR(WINED3DFMT_W11V11U10);
507     FMT_TO_STR(WINED3DFMT_A2W10V10U10);
508     FMT_TO_STR(WINED3DFMT_UYVY);
509     FMT_TO_STR(WINED3DFMT_YUY2);
510     FMT_TO_STR(WINED3DFMT_DXT1);
511     FMT_TO_STR(WINED3DFMT_DXT2);
512     FMT_TO_STR(WINED3DFMT_DXT3);
513     FMT_TO_STR(WINED3DFMT_DXT4);
514     FMT_TO_STR(WINED3DFMT_DXT5);
515     FMT_TO_STR(WINED3DFMT_MULTI2_ARGB8);
516     FMT_TO_STR(WINED3DFMT_G8R8_G8B8);
517     FMT_TO_STR(WINED3DFMT_R8G8_B8G8);
518     FMT_TO_STR(WINED3DFMT_D16_LOCKABLE);
519     FMT_TO_STR(WINED3DFMT_D32);
520     FMT_TO_STR(WINED3DFMT_D15S1);
521     FMT_TO_STR(WINED3DFMT_D24S8);
522     FMT_TO_STR(WINED3DFMT_D24X8);
523     FMT_TO_STR(WINED3DFMT_D24X4S4);
524     FMT_TO_STR(WINED3DFMT_D16);
525     FMT_TO_STR(WINED3DFMT_L16);
526     FMT_TO_STR(WINED3DFMT_D32F_LOCKABLE);
527     FMT_TO_STR(WINED3DFMT_D24FS8);
528     FMT_TO_STR(WINED3DFMT_VERTEXDATA);
529     FMT_TO_STR(WINED3DFMT_INDEX16);
530     FMT_TO_STR(WINED3DFMT_INDEX32);
531     FMT_TO_STR(WINED3DFMT_Q16W16V16U16);
532     FMT_TO_STR(WINED3DFMT_R16F);
533     FMT_TO_STR(WINED3DFMT_G16R16F);
534     FMT_TO_STR(WINED3DFMT_A16B16G16R16F);
535     FMT_TO_STR(WINED3DFMT_R32F);
536     FMT_TO_STR(WINED3DFMT_G32R32F);
537     FMT_TO_STR(WINED3DFMT_A32B32G32R32F);
538     FMT_TO_STR(WINED3DFMT_CxV8U8);
539     FMT_TO_STR(WINED3DFMT_ATI2N);
540 #undef FMT_TO_STR
541   default:
542     {
543       char fourcc[5];
544       fourcc[0] = (char)(fmt);
545       fourcc[1] = (char)(fmt >> 8);
546       fourcc[2] = (char)(fmt >> 16);
547       fourcc[3] = (char)(fmt >> 24);
548       fourcc[4] = 0;
549       if( isprint(fourcc[0]) && isprint(fourcc[1]) && isprint(fourcc[2]) && isprint(fourcc[3]) )
550         FIXME("Unrecognized %u (as fourcc: %s) WINED3DFORMAT!\n", fmt, fourcc);
551       else
552         FIXME("Unrecognized %u WINED3DFORMAT!\n", fmt);
553     }
554     return "unrecognized";
555   }
556 }
557
558 const char* debug_d3ddevicetype(WINED3DDEVTYPE devtype) {
559   switch (devtype) {
560 #define DEVTYPE_TO_STR(dev) case dev: return #dev
561     DEVTYPE_TO_STR(WINED3DDEVTYPE_HAL);
562     DEVTYPE_TO_STR(WINED3DDEVTYPE_REF);
563     DEVTYPE_TO_STR(WINED3DDEVTYPE_SW);
564 #undef DEVTYPE_TO_STR
565   default:
566     FIXME("Unrecognized %u WINED3DDEVTYPE!\n", devtype);
567     return "unrecognized";
568   }
569 }
570
571 const char* debug_d3dusage(DWORD usage) {
572   switch (usage & WINED3DUSAGE_MASK) {
573 #define WINED3DUSAGE_TO_STR(u) case u: return #u
574     WINED3DUSAGE_TO_STR(WINED3DUSAGE_RENDERTARGET);
575     WINED3DUSAGE_TO_STR(WINED3DUSAGE_DEPTHSTENCIL);
576     WINED3DUSAGE_TO_STR(WINED3DUSAGE_WRITEONLY);
577     WINED3DUSAGE_TO_STR(WINED3DUSAGE_SOFTWAREPROCESSING);
578     WINED3DUSAGE_TO_STR(WINED3DUSAGE_DONOTCLIP);
579     WINED3DUSAGE_TO_STR(WINED3DUSAGE_POINTS);
580     WINED3DUSAGE_TO_STR(WINED3DUSAGE_RTPATCHES);
581     WINED3DUSAGE_TO_STR(WINED3DUSAGE_NPATCHES);
582     WINED3DUSAGE_TO_STR(WINED3DUSAGE_DYNAMIC);
583     WINED3DUSAGE_TO_STR(WINED3DUSAGE_AUTOGENMIPMAP);
584     WINED3DUSAGE_TO_STR(WINED3DUSAGE_DMAP);
585 #undef WINED3DUSAGE_TO_STR
586   case 0: return "none";
587   default:
588     FIXME("Unrecognized %u Usage!\n", usage);
589     return "unrecognized";
590   }
591 }
592
593 const char* debug_d3dusagequery(DWORD usagequery) {
594   switch (usagequery & WINED3DUSAGE_QUERY_MASK) {
595 #define WINED3DUSAGEQUERY_TO_STR(u) case u: return #u
596     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_FILTER);
597     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_LEGACYBUMPMAP);
598     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING);
599     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_SRGBREAD);
600     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_SRGBWRITE);
601     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_VERTEXTEXTURE);
602     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_WRAPANDMIP);
603 #undef WINED3DUSAGEQUERY_TO_STR
604   case 0: return "none";
605   default:
606     FIXME("Unrecognized %u Usage Query!\n", usagequery);
607     return "unrecognized";
608   }
609 }
610
611 const char* debug_d3ddeclmethod(WINED3DDECLMETHOD method) {
612     switch (method) {
613 #define WINED3DDECLMETHOD_TO_STR(u) case u: return #u
614         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_DEFAULT);
615         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_PARTIALU);
616         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_PARTIALV);
617         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_CROSSUV);
618         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_UV);
619         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_LOOKUP);
620         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_LOOKUPPRESAMPLED);
621 #undef WINED3DDECLMETHOD_TO_STR
622         default:
623             FIXME("Unrecognized %u declaration method!\n", method);
624             return "unrecognized";
625     }
626 }
627
628 const char* debug_d3ddecltype(WINED3DDECLTYPE type) {
629     switch (type) {
630 #define WINED3DDECLTYPE_TO_STR(u) case u: return #u
631         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_FLOAT1);
632         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_FLOAT2);
633         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_FLOAT3);
634         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_FLOAT4);
635         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_D3DCOLOR);
636         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_UBYTE4);
637         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_SHORT2);
638         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_SHORT4);
639         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_UBYTE4N);
640         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_SHORT2N);
641         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_SHORT4N);
642         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_USHORT2N);
643         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_USHORT4N);
644         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_UDEC3);
645         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_DEC3N);
646         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_FLOAT16_2);
647         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_FLOAT16_4);
648         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_UNUSED);
649 #undef WINED3DDECLTYPE_TO_STR
650         default:
651             FIXME("Unrecognized %u declaration type!\n", type);
652             return "unrecognized";
653     }
654 }
655
656 const char* debug_d3ddeclusage(BYTE usage) {
657     switch (usage) {
658 #define WINED3DDECLUSAGE_TO_STR(u) case u: return #u
659         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_POSITION);
660         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_BLENDWEIGHT);
661         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_BLENDINDICES);
662         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_NORMAL);
663         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_PSIZE);
664         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_TEXCOORD);
665         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_TANGENT);
666         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_BINORMAL);
667         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_TESSFACTOR);
668         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_POSITIONT);
669         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_COLOR);
670         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_FOG);
671         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_DEPTH);
672         WINED3DDECLUSAGE_TO_STR(WINED3DDECLUSAGE_SAMPLE);
673 #undef WINED3DDECLUSAGE_TO_STR
674         default:
675             FIXME("Unrecognized %u declaration usage!\n", usage);
676             return "unrecognized";
677     }
678 }
679
680 const char* debug_d3dresourcetype(WINED3DRESOURCETYPE res) {
681   switch (res) {
682 #define RES_TO_STR(res) case res: return #res;
683     RES_TO_STR(WINED3DRTYPE_SURFACE);
684     RES_TO_STR(WINED3DRTYPE_VOLUME);
685     RES_TO_STR(WINED3DRTYPE_TEXTURE);
686     RES_TO_STR(WINED3DRTYPE_VOLUMETEXTURE);
687     RES_TO_STR(WINED3DRTYPE_CUBETEXTURE);
688     RES_TO_STR(WINED3DRTYPE_VERTEXBUFFER);
689     RES_TO_STR(WINED3DRTYPE_INDEXBUFFER);
690 #undef  RES_TO_STR
691   default:
692     FIXME("Unrecognized %u WINED3DRESOURCETYPE!\n", res);
693     return "unrecognized";
694   }
695 }
696
697 const char* debug_d3dprimitivetype(WINED3DPRIMITIVETYPE PrimitiveType) {
698   switch (PrimitiveType) {
699 #define PRIM_TO_STR(prim) case prim: return #prim;
700     PRIM_TO_STR(WINED3DPT_POINTLIST);
701     PRIM_TO_STR(WINED3DPT_LINELIST);
702     PRIM_TO_STR(WINED3DPT_LINESTRIP);
703     PRIM_TO_STR(WINED3DPT_TRIANGLELIST);
704     PRIM_TO_STR(WINED3DPT_TRIANGLESTRIP);
705     PRIM_TO_STR(WINED3DPT_TRIANGLEFAN);
706 #undef  PRIM_TO_STR
707   default:
708     FIXME("Unrecognized %u WINED3DPRIMITIVETYPE!\n", PrimitiveType);
709     return "unrecognized";
710   }
711 }
712
713 const char* debug_d3drenderstate(DWORD state) {
714   switch (state) {
715 #define D3DSTATE_TO_STR(u) case u: return #u
716     D3DSTATE_TO_STR(WINED3DRS_TEXTUREHANDLE             );
717     D3DSTATE_TO_STR(WINED3DRS_ANTIALIAS                 );
718     D3DSTATE_TO_STR(WINED3DRS_TEXTUREADDRESS            );
719     D3DSTATE_TO_STR(WINED3DRS_TEXTUREPERSPECTIVE        );
720     D3DSTATE_TO_STR(WINED3DRS_WRAPU                     );
721     D3DSTATE_TO_STR(WINED3DRS_WRAPV                     );
722     D3DSTATE_TO_STR(WINED3DRS_ZENABLE                   );
723     D3DSTATE_TO_STR(WINED3DRS_FILLMODE                  );
724     D3DSTATE_TO_STR(WINED3DRS_SHADEMODE                 );
725     D3DSTATE_TO_STR(WINED3DRS_LINEPATTERN               );
726     D3DSTATE_TO_STR(WINED3DRS_MONOENABLE                );
727     D3DSTATE_TO_STR(WINED3DRS_ROP2                      );
728     D3DSTATE_TO_STR(WINED3DRS_PLANEMASK                 );
729     D3DSTATE_TO_STR(WINED3DRS_ZWRITEENABLE              );
730     D3DSTATE_TO_STR(WINED3DRS_ALPHATESTENABLE           );
731     D3DSTATE_TO_STR(WINED3DRS_LASTPIXEL                 );
732     D3DSTATE_TO_STR(WINED3DRS_TEXTUREMAG                );
733     D3DSTATE_TO_STR(WINED3DRS_TEXTUREMIN                );
734     D3DSTATE_TO_STR(WINED3DRS_SRCBLEND                  );
735     D3DSTATE_TO_STR(WINED3DRS_DESTBLEND                 );
736     D3DSTATE_TO_STR(WINED3DRS_TEXTUREMAPBLEND           );
737     D3DSTATE_TO_STR(WINED3DRS_CULLMODE                  );
738     D3DSTATE_TO_STR(WINED3DRS_ZFUNC                     );
739     D3DSTATE_TO_STR(WINED3DRS_ALPHAREF                  );
740     D3DSTATE_TO_STR(WINED3DRS_ALPHAFUNC                 );
741     D3DSTATE_TO_STR(WINED3DRS_DITHERENABLE              );
742     D3DSTATE_TO_STR(WINED3DRS_ALPHABLENDENABLE          );
743     D3DSTATE_TO_STR(WINED3DRS_FOGENABLE                 );
744     D3DSTATE_TO_STR(WINED3DRS_SPECULARENABLE            );
745     D3DSTATE_TO_STR(WINED3DRS_ZVISIBLE                  );
746     D3DSTATE_TO_STR(WINED3DRS_SUBPIXEL                  );
747     D3DSTATE_TO_STR(WINED3DRS_SUBPIXELX                 );
748     D3DSTATE_TO_STR(WINED3DRS_STIPPLEDALPHA             );
749     D3DSTATE_TO_STR(WINED3DRS_FOGCOLOR                  );
750     D3DSTATE_TO_STR(WINED3DRS_FOGTABLEMODE              );
751     D3DSTATE_TO_STR(WINED3DRS_FOGSTART                  );
752     D3DSTATE_TO_STR(WINED3DRS_FOGEND                    );
753     D3DSTATE_TO_STR(WINED3DRS_FOGDENSITY                );
754     D3DSTATE_TO_STR(WINED3DRS_STIPPLEENABLE             );
755     D3DSTATE_TO_STR(WINED3DRS_EDGEANTIALIAS             );
756     D3DSTATE_TO_STR(WINED3DRS_COLORKEYENABLE            );
757     D3DSTATE_TO_STR(WINED3DRS_BORDERCOLOR               );
758     D3DSTATE_TO_STR(WINED3DRS_TEXTUREADDRESSU           );
759     D3DSTATE_TO_STR(WINED3DRS_TEXTUREADDRESSV           );
760     D3DSTATE_TO_STR(WINED3DRS_MIPMAPLODBIAS             );
761     D3DSTATE_TO_STR(WINED3DRS_ZBIAS                     );
762     D3DSTATE_TO_STR(WINED3DRS_RANGEFOGENABLE            );
763     D3DSTATE_TO_STR(WINED3DRS_ANISOTROPY                );
764     D3DSTATE_TO_STR(WINED3DRS_FLUSHBATCH                );
765     D3DSTATE_TO_STR(WINED3DRS_TRANSLUCENTSORTINDEPENDENT);
766     D3DSTATE_TO_STR(WINED3DRS_STENCILENABLE             );
767     D3DSTATE_TO_STR(WINED3DRS_STENCILFAIL               );
768     D3DSTATE_TO_STR(WINED3DRS_STENCILZFAIL              );
769     D3DSTATE_TO_STR(WINED3DRS_STENCILPASS               );
770     D3DSTATE_TO_STR(WINED3DRS_STENCILFUNC               );
771     D3DSTATE_TO_STR(WINED3DRS_STENCILREF                );
772     D3DSTATE_TO_STR(WINED3DRS_STENCILMASK               );
773     D3DSTATE_TO_STR(WINED3DRS_STENCILWRITEMASK          );
774     D3DSTATE_TO_STR(WINED3DRS_TEXTUREFACTOR             );
775     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN00          );
776     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN01          );
777     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN02          );
778     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN03          );
779     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN04          );
780     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN05          );
781     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN06          );
782     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN07          );
783     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN08          );
784     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN09          );
785     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN10          );
786     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN11          );
787     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN12          );
788     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN13          );
789     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN14          );
790     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN15          );
791     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN16          );
792     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN17          );
793     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN18          );
794     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN19          );
795     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN20          );
796     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN21          );
797     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN22          );
798     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN23          );
799     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN24          );
800     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN25          );
801     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN26          );
802     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN27          );
803     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN28          );
804     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN29          );
805     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN30          );
806     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN31          );
807     D3DSTATE_TO_STR(WINED3DRS_WRAP0                     );
808     D3DSTATE_TO_STR(WINED3DRS_WRAP1                     );
809     D3DSTATE_TO_STR(WINED3DRS_WRAP2                     );
810     D3DSTATE_TO_STR(WINED3DRS_WRAP3                     );
811     D3DSTATE_TO_STR(WINED3DRS_WRAP4                     );
812     D3DSTATE_TO_STR(WINED3DRS_WRAP5                     );
813     D3DSTATE_TO_STR(WINED3DRS_WRAP6                     );
814     D3DSTATE_TO_STR(WINED3DRS_WRAP7                     );
815     D3DSTATE_TO_STR(WINED3DRS_CLIPPING                  );
816     D3DSTATE_TO_STR(WINED3DRS_LIGHTING                  );
817     D3DSTATE_TO_STR(WINED3DRS_EXTENTS                   );
818     D3DSTATE_TO_STR(WINED3DRS_AMBIENT                   );
819     D3DSTATE_TO_STR(WINED3DRS_FOGVERTEXMODE             );
820     D3DSTATE_TO_STR(WINED3DRS_COLORVERTEX               );
821     D3DSTATE_TO_STR(WINED3DRS_LOCALVIEWER               );
822     D3DSTATE_TO_STR(WINED3DRS_NORMALIZENORMALS          );
823     D3DSTATE_TO_STR(WINED3DRS_COLORKEYBLENDENABLE       );
824     D3DSTATE_TO_STR(WINED3DRS_DIFFUSEMATERIALSOURCE     );
825     D3DSTATE_TO_STR(WINED3DRS_SPECULARMATERIALSOURCE    );
826     D3DSTATE_TO_STR(WINED3DRS_AMBIENTMATERIALSOURCE     );
827     D3DSTATE_TO_STR(WINED3DRS_EMISSIVEMATERIALSOURCE    );
828     D3DSTATE_TO_STR(WINED3DRS_VERTEXBLEND               );
829     D3DSTATE_TO_STR(WINED3DRS_CLIPPLANEENABLE           );
830     D3DSTATE_TO_STR(WINED3DRS_SOFTWAREVERTEXPROCESSING  );
831     D3DSTATE_TO_STR(WINED3DRS_POINTSIZE                 );
832     D3DSTATE_TO_STR(WINED3DRS_POINTSIZE_MIN             );
833     D3DSTATE_TO_STR(WINED3DRS_POINTSPRITEENABLE         );
834     D3DSTATE_TO_STR(WINED3DRS_POINTSCALEENABLE          );
835     D3DSTATE_TO_STR(WINED3DRS_POINTSCALE_A              );
836     D3DSTATE_TO_STR(WINED3DRS_POINTSCALE_B              );
837     D3DSTATE_TO_STR(WINED3DRS_POINTSCALE_C              );
838     D3DSTATE_TO_STR(WINED3DRS_MULTISAMPLEANTIALIAS      );
839     D3DSTATE_TO_STR(WINED3DRS_MULTISAMPLEMASK           );
840     D3DSTATE_TO_STR(WINED3DRS_PATCHEDGESTYLE            );
841     D3DSTATE_TO_STR(WINED3DRS_PATCHSEGMENTS             );
842     D3DSTATE_TO_STR(WINED3DRS_DEBUGMONITORTOKEN         );
843     D3DSTATE_TO_STR(WINED3DRS_POINTSIZE_MAX             );
844     D3DSTATE_TO_STR(WINED3DRS_INDEXEDVERTEXBLENDENABLE  );
845     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE          );
846     D3DSTATE_TO_STR(WINED3DRS_TWEENFACTOR               );
847     D3DSTATE_TO_STR(WINED3DRS_BLENDOP                   );
848     D3DSTATE_TO_STR(WINED3DRS_POSITIONDEGREE            );
849     D3DSTATE_TO_STR(WINED3DRS_NORMALDEGREE              );
850     D3DSTATE_TO_STR(WINED3DRS_SCISSORTESTENABLE         );
851     D3DSTATE_TO_STR(WINED3DRS_SLOPESCALEDEPTHBIAS       );
852     D3DSTATE_TO_STR(WINED3DRS_ANTIALIASEDLINEENABLE     );
853     D3DSTATE_TO_STR(WINED3DRS_MINTESSELLATIONLEVEL      );
854     D3DSTATE_TO_STR(WINED3DRS_MAXTESSELLATIONLEVEL      );
855     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_X            );
856     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_Y            );
857     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_Z            );
858     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_W            );
859     D3DSTATE_TO_STR(WINED3DRS_ENABLEADAPTIVETESSELLATION);
860     D3DSTATE_TO_STR(WINED3DRS_TWOSIDEDSTENCILMODE       );
861     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILFAIL           );
862     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILZFAIL          );
863     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILPASS           );
864     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILFUNC           );
865     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE1         );
866     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE2         );
867     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE3         );
868     D3DSTATE_TO_STR(WINED3DRS_BLENDFACTOR               );
869     D3DSTATE_TO_STR(WINED3DRS_SRGBWRITEENABLE           );
870     D3DSTATE_TO_STR(WINED3DRS_DEPTHBIAS                 );
871     D3DSTATE_TO_STR(WINED3DRS_WRAP8                     );
872     D3DSTATE_TO_STR(WINED3DRS_WRAP9                     );
873     D3DSTATE_TO_STR(WINED3DRS_WRAP10                    );
874     D3DSTATE_TO_STR(WINED3DRS_WRAP11                    );
875     D3DSTATE_TO_STR(WINED3DRS_WRAP12                    );
876     D3DSTATE_TO_STR(WINED3DRS_WRAP13                    );
877     D3DSTATE_TO_STR(WINED3DRS_WRAP14                    );
878     D3DSTATE_TO_STR(WINED3DRS_WRAP15                    );
879     D3DSTATE_TO_STR(WINED3DRS_SEPARATEALPHABLENDENABLE  );
880     D3DSTATE_TO_STR(WINED3DRS_SRCBLENDALPHA             );
881     D3DSTATE_TO_STR(WINED3DRS_DESTBLENDALPHA            );
882     D3DSTATE_TO_STR(WINED3DRS_BLENDOPALPHA              );
883 #undef D3DSTATE_TO_STR
884   default:
885     FIXME("Unrecognized %u render state!\n", state);
886     return "unrecognized";
887   }
888 }
889
890 const char* debug_d3dsamplerstate(DWORD state) {
891   switch (state) {
892 #define D3DSTATE_TO_STR(u) case u: return #u
893     D3DSTATE_TO_STR(WINED3DSAMP_BORDERCOLOR  );
894     D3DSTATE_TO_STR(WINED3DSAMP_ADDRESSU     );
895     D3DSTATE_TO_STR(WINED3DSAMP_ADDRESSV     );
896     D3DSTATE_TO_STR(WINED3DSAMP_ADDRESSW     );
897     D3DSTATE_TO_STR(WINED3DSAMP_MAGFILTER    );
898     D3DSTATE_TO_STR(WINED3DSAMP_MINFILTER    );
899     D3DSTATE_TO_STR(WINED3DSAMP_MIPFILTER    );
900     D3DSTATE_TO_STR(WINED3DSAMP_MIPMAPLODBIAS);
901     D3DSTATE_TO_STR(WINED3DSAMP_MAXMIPLEVEL  );
902     D3DSTATE_TO_STR(WINED3DSAMP_MAXANISOTROPY);
903     D3DSTATE_TO_STR(WINED3DSAMP_SRGBTEXTURE  );
904     D3DSTATE_TO_STR(WINED3DSAMP_ELEMENTINDEX );
905     D3DSTATE_TO_STR(WINED3DSAMP_DMAPOFFSET   );
906 #undef D3DSTATE_TO_STR
907   default:
908     FIXME("Unrecognized %u sampler state!\n", state);
909     return "unrecognized";
910   }
911 }
912
913 const char *debug_d3dtexturefiltertype(WINED3DTEXTUREFILTERTYPE filter_type) {
914     switch (filter_type) {
915 #define D3DTEXTUREFILTERTYPE_TO_STR(u) case u: return #u
916         D3DTEXTUREFILTERTYPE_TO_STR(WINED3DTEXF_NONE);
917         D3DTEXTUREFILTERTYPE_TO_STR(WINED3DTEXF_POINT);
918         D3DTEXTUREFILTERTYPE_TO_STR(WINED3DTEXF_LINEAR);
919         D3DTEXTUREFILTERTYPE_TO_STR(WINED3DTEXF_ANISOTROPIC);
920         D3DTEXTUREFILTERTYPE_TO_STR(WINED3DTEXF_FLATCUBIC);
921         D3DTEXTUREFILTERTYPE_TO_STR(WINED3DTEXF_GAUSSIANCUBIC);
922         D3DTEXTUREFILTERTYPE_TO_STR(WINED3DTEXF_PYRAMIDALQUAD);
923         D3DTEXTUREFILTERTYPE_TO_STR(WINED3DTEXF_GAUSSIANQUAD);
924 #undef D3DTEXTUREFILTERTYPE_TO_STR
925         default:
926             FIXME("Unrecognied texture filter type 0x%08x\n", filter_type);
927             return "unrecognized";
928     }
929 }
930
931 const char* debug_d3dtexturestate(DWORD state) {
932   switch (state) {
933 #define D3DSTATE_TO_STR(u) case u: return #u
934     D3DSTATE_TO_STR(WINED3DTSS_COLOROP               );
935     D3DSTATE_TO_STR(WINED3DTSS_COLORARG1             );
936     D3DSTATE_TO_STR(WINED3DTSS_COLORARG2             );
937     D3DSTATE_TO_STR(WINED3DTSS_ALPHAOP               );
938     D3DSTATE_TO_STR(WINED3DTSS_ALPHAARG1             );
939     D3DSTATE_TO_STR(WINED3DTSS_ALPHAARG2             );
940     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT00          );
941     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT01          );
942     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT10          );
943     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT11          );
944     D3DSTATE_TO_STR(WINED3DTSS_TEXCOORDINDEX         );
945     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVLSCALE         );
946     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVLOFFSET        );
947     D3DSTATE_TO_STR(WINED3DTSS_TEXTURETRANSFORMFLAGS );
948     D3DSTATE_TO_STR(WINED3DTSS_ADDRESSW              );
949     D3DSTATE_TO_STR(WINED3DTSS_COLORARG0             );
950     D3DSTATE_TO_STR(WINED3DTSS_ALPHAARG0             );
951     D3DSTATE_TO_STR(WINED3DTSS_RESULTARG             );
952     D3DSTATE_TO_STR(WINED3DTSS_CONSTANT              );
953 #undef D3DSTATE_TO_STR
954   case 12:
955     /* Note WINED3DTSS are not consecutive, so skip these */
956     return "unused";
957   default:
958     FIXME("Unrecognized %u texture state!\n", state);
959     return "unrecognized";
960   }
961 }
962
963 const char* debug_d3dtop(WINED3DTEXTUREOP d3dtop) {
964     switch (d3dtop) {
965 #define D3DTOP_TO_STR(u) case u: return #u
966         D3DTOP_TO_STR(WINED3DTOP_DISABLE);
967         D3DTOP_TO_STR(WINED3DTOP_SELECTARG1);
968         D3DTOP_TO_STR(WINED3DTOP_SELECTARG2);
969         D3DTOP_TO_STR(WINED3DTOP_MODULATE);
970         D3DTOP_TO_STR(WINED3DTOP_MODULATE2X);
971         D3DTOP_TO_STR(WINED3DTOP_MODULATE4X);
972         D3DTOP_TO_STR(WINED3DTOP_ADD);
973         D3DTOP_TO_STR(WINED3DTOP_ADDSIGNED);
974         D3DTOP_TO_STR(WINED3DTOP_ADDSIGNED2X);
975         D3DTOP_TO_STR(WINED3DTOP_SUBTRACT);
976         D3DTOP_TO_STR(WINED3DTOP_ADDSMOOTH);
977         D3DTOP_TO_STR(WINED3DTOP_BLENDDIFFUSEALPHA);
978         D3DTOP_TO_STR(WINED3DTOP_BLENDTEXTUREALPHA);
979         D3DTOP_TO_STR(WINED3DTOP_BLENDFACTORALPHA);
980         D3DTOP_TO_STR(WINED3DTOP_BLENDTEXTUREALPHAPM);
981         D3DTOP_TO_STR(WINED3DTOP_BLENDCURRENTALPHA);
982         D3DTOP_TO_STR(WINED3DTOP_PREMODULATE);
983         D3DTOP_TO_STR(WINED3DTOP_MODULATEALPHA_ADDCOLOR);
984         D3DTOP_TO_STR(WINED3DTOP_MODULATECOLOR_ADDALPHA);
985         D3DTOP_TO_STR(WINED3DTOP_MODULATEINVALPHA_ADDCOLOR);
986         D3DTOP_TO_STR(WINED3DTOP_MODULATEINVCOLOR_ADDALPHA);
987         D3DTOP_TO_STR(WINED3DTOP_BUMPENVMAP);
988         D3DTOP_TO_STR(WINED3DTOP_BUMPENVMAPLUMINANCE);
989         D3DTOP_TO_STR(WINED3DTOP_DOTPRODUCT3);
990         D3DTOP_TO_STR(WINED3DTOP_MULTIPLYADD);
991         D3DTOP_TO_STR(WINED3DTOP_LERP);
992 #undef D3DTOP_TO_STR
993         default:
994             FIXME("Unrecognized %u WINED3DTOP\n", d3dtop);
995             return "unrecognized";
996     }
997 }
998
999 const char* debug_d3dtstype(WINED3DTRANSFORMSTATETYPE tstype) {
1000     switch (tstype) {
1001 #define TSTYPE_TO_STR(tstype) case tstype: return #tstype
1002     TSTYPE_TO_STR(WINED3DTS_VIEW);
1003     TSTYPE_TO_STR(WINED3DTS_PROJECTION);
1004     TSTYPE_TO_STR(WINED3DTS_TEXTURE0);
1005     TSTYPE_TO_STR(WINED3DTS_TEXTURE1);
1006     TSTYPE_TO_STR(WINED3DTS_TEXTURE2);
1007     TSTYPE_TO_STR(WINED3DTS_TEXTURE3);
1008     TSTYPE_TO_STR(WINED3DTS_TEXTURE4);
1009     TSTYPE_TO_STR(WINED3DTS_TEXTURE5);
1010     TSTYPE_TO_STR(WINED3DTS_TEXTURE6);
1011     TSTYPE_TO_STR(WINED3DTS_TEXTURE7);
1012     TSTYPE_TO_STR(WINED3DTS_WORLDMATRIX(0));
1013 #undef TSTYPE_TO_STR
1014     default:
1015         if (tstype > 256 && tstype < 512) {
1016             FIXME("WINED3DTS_WORLDMATRIX(%u). 1..255 not currently supported\n", tstype);
1017             return ("WINED3DTS_WORLDMATRIX > 0");
1018         }
1019         FIXME("Unrecognized %u WINED3DTS\n", tstype);
1020         return "unrecognized";
1021     }
1022 }
1023
1024 const char* debug_d3dpool(WINED3DPOOL Pool) {
1025   switch (Pool) {
1026 #define POOL_TO_STR(p) case p: return #p;
1027     POOL_TO_STR(WINED3DPOOL_DEFAULT);
1028     POOL_TO_STR(WINED3DPOOL_MANAGED);
1029     POOL_TO_STR(WINED3DPOOL_SYSTEMMEM);
1030     POOL_TO_STR(WINED3DPOOL_SCRATCH);
1031 #undef  POOL_TO_STR
1032   default:
1033     FIXME("Unrecognized %u WINED3DPOOL!\n", Pool);
1034     return "unrecognized";
1035   }
1036 }
1037
1038 const char *debug_fbostatus(GLenum status) {
1039     switch(status) {
1040 #define FBOSTATUS_TO_STR(u) case u: return #u
1041         FBOSTATUS_TO_STR(GL_FRAMEBUFFER_COMPLETE_EXT);
1042         FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT);
1043         FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT);
1044         FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT);
1045         FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT);
1046         FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT);
1047         FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT);
1048         FBOSTATUS_TO_STR(GL_FRAMEBUFFER_UNSUPPORTED_EXT);
1049 #undef FBOSTATUS_TO_STR
1050         default:
1051             FIXME("Unrecognied FBO status 0x%08x\n", status);
1052             return "unrecognized";
1053     }
1054 }
1055
1056 const char *debug_glerror(GLenum error) {
1057     switch(error) {
1058 #define GLERROR_TO_STR(u) case u: return #u
1059         GLERROR_TO_STR(GL_NO_ERROR);
1060         GLERROR_TO_STR(GL_INVALID_ENUM);
1061         GLERROR_TO_STR(GL_INVALID_VALUE);
1062         GLERROR_TO_STR(GL_INVALID_OPERATION);
1063         GLERROR_TO_STR(GL_STACK_OVERFLOW);
1064         GLERROR_TO_STR(GL_STACK_UNDERFLOW);
1065         GLERROR_TO_STR(GL_OUT_OF_MEMORY);
1066         GLERROR_TO_STR(GL_INVALID_FRAMEBUFFER_OPERATION_EXT);
1067 #undef GLERROR_TO_STR
1068         default:
1069             FIXME("Unrecognied GL error 0x%08x\n", error);
1070             return "unrecognized";
1071     }
1072 }
1073
1074 const char *debug_d3dbasis(WINED3DBASISTYPE basis) {
1075     switch(basis) {
1076         case WINED3DBASIS_BEZIER:       return "WINED3DBASIS_BEZIER";
1077         case WINED3DBASIS_BSPLINE:      return "WINED3DBASIS_BSPLINE";
1078         case WINED3DBASIS_INTERPOLATE:  return "WINED3DBASIS_INTERPOLATE";
1079         default:                        return "unrecognized";
1080     }
1081 }
1082
1083 const char *debug_d3ddegree(WINED3DDEGREETYPE degree) {
1084     switch(degree) {
1085         case WINED3DDEGREE_LINEAR:      return "WINED3DDEGREE_LINEAR";
1086         case WINED3DDEGREE_QUADRATIC:   return "WINED3DDEGREE_QUADRATIC";
1087         case WINED3DDEGREE_CUBIC:       return "WINED3DDEGREE_CUBIC";
1088         case WINED3DDEGREE_QUINTIC:     return "WINED3DDEGREE_QUINTIC";
1089         default:                        return "unrecognized";
1090     }
1091 }
1092
1093 /*****************************************************************************
1094  * Useful functions mapping GL <-> D3D values
1095  */
1096 GLenum StencilOp(DWORD op) {
1097     switch(op) {
1098     case WINED3DSTENCILOP_KEEP    : return GL_KEEP;
1099     case WINED3DSTENCILOP_ZERO    : return GL_ZERO;
1100     case WINED3DSTENCILOP_REPLACE : return GL_REPLACE;
1101     case WINED3DSTENCILOP_INCRSAT : return GL_INCR;
1102     case WINED3DSTENCILOP_DECRSAT : return GL_DECR;
1103     case WINED3DSTENCILOP_INVERT  : return GL_INVERT;
1104     case WINED3DSTENCILOP_INCR    : return GL_INCR_WRAP_EXT;
1105     case WINED3DSTENCILOP_DECR    : return GL_DECR_WRAP_EXT;
1106     default:
1107         FIXME("Unrecognized stencil op %d\n", op);
1108         return GL_KEEP;
1109     }
1110 }
1111
1112 GLenum CompareFunc(DWORD func) {
1113     switch ((WINED3DCMPFUNC)func) {
1114     case WINED3DCMP_NEVER        : return GL_NEVER;
1115     case WINED3DCMP_LESS         : return GL_LESS;
1116     case WINED3DCMP_EQUAL        : return GL_EQUAL;
1117     case WINED3DCMP_LESSEQUAL    : return GL_LEQUAL;
1118     case WINED3DCMP_GREATER      : return GL_GREATER;
1119     case WINED3DCMP_NOTEQUAL     : return GL_NOTEQUAL;
1120     case WINED3DCMP_GREATEREQUAL : return GL_GEQUAL;
1121     case WINED3DCMP_ALWAYS       : return GL_ALWAYS;
1122     default:
1123         FIXME("Unrecognized WINED3DCMPFUNC value %d\n", func);
1124         return 0;
1125     }
1126 }
1127
1128 BOOL is_invalid_op(IWineD3DDeviceImpl *This, int stage, WINED3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3) {
1129     if (op == WINED3DTOP_DISABLE) return FALSE;
1130     if (This->stateBlock->textures[stage]) return FALSE;
1131
1132     if ((arg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
1133             && op != WINED3DTOP_SELECTARG2) return TRUE;
1134     if ((arg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
1135             && op != WINED3DTOP_SELECTARG1) return TRUE;
1136     if ((arg3 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
1137             && (op == WINED3DTOP_MULTIPLYADD || op == WINED3DTOP_LERP)) return TRUE;
1138
1139     return FALSE;
1140 }
1141
1142 /* Setup this textures matrix according to the texture flags*/
1143 void set_texture_matrix(const float *smat, DWORD flags, BOOL calculatedCoords, BOOL transformed, DWORD coordtype)
1144 {
1145     float mat[16];
1146
1147     glMatrixMode(GL_TEXTURE);
1148     checkGLcall("glMatrixMode(GL_TEXTURE)");
1149
1150     if (flags == WINED3DTTFF_DISABLE || flags == WINED3DTTFF_COUNT1 || transformed) {
1151         glLoadIdentity();
1152         checkGLcall("glLoadIdentity()");
1153         return;
1154     }
1155
1156     if (flags == (WINED3DTTFF_COUNT1|WINED3DTTFF_PROJECTED)) {
1157         ERR("Invalid texture transform flags: WINED3DTTFF_COUNT1|WINED3DTTFF_PROJECTED\n");
1158         return;
1159     }
1160
1161     memcpy(mat, smat, 16 * sizeof(float));
1162
1163     if (flags & WINED3DTTFF_PROJECTED) {
1164         switch (flags & ~WINED3DTTFF_PROJECTED) {
1165         case WINED3DTTFF_COUNT2:
1166             mat[3] = mat[1], mat[7] = mat[5], mat[11] = mat[9], mat[15] = mat[13];
1167             mat[1] = mat[5] = mat[9] = mat[13] = 0;
1168             break;
1169         case WINED3DTTFF_COUNT3:
1170             mat[3] = mat[2], mat[7] = mat[6], mat[11] = mat[10], mat[15] = mat[14];
1171             mat[2] = mat[6] = mat[10] = mat[14] = 0;
1172             break;
1173         }
1174     } else { /* under directx the R/Z coord can be used for translation, under opengl we use the Q coord instead */
1175         if(!calculatedCoords) {
1176             switch(coordtype) {
1177                 case WINED3DDECLTYPE_FLOAT1:
1178                     /* Direct3D passes the default 1.0 in the 2nd coord, while gl passes it in the 4th.
1179                      * swap 2nd and 4th coord. No need to store the value of mat[12] in mat[4] because
1180                      * the input value to the transformation will be 0, so the matrix value is irrelevant
1181                      */
1182                     mat[12] = mat[4];
1183                     mat[13] = mat[5];
1184                     mat[14] = mat[6];
1185                     mat[15] = mat[7];
1186                     break;
1187                 case WINED3DDECLTYPE_FLOAT2:
1188                     /* See above, just 3rd and 4th coord
1189                     */
1190                     mat[12] = mat[8];
1191                     mat[13] = mat[9];
1192                     mat[14] = mat[10];
1193                     mat[15] = mat[11];
1194                     break;
1195                 case WINED3DDECLTYPE_FLOAT3: /* Opengl defaults match dx defaults */
1196                 case WINED3DDECLTYPE_FLOAT4: /* No defaults apply, all app defined */
1197
1198                 /* This is to prevent swapping the matrix lines and put the default 4th coord = 1.0
1199                  * into a bad place. The division elimination below will apply to make sure the
1200                  * 1.0 doesn't do anything bad. The caller will set this value if the stride is 0
1201                  */
1202                 case WINED3DDECLTYPE_UNUSED: /* No texture coords, 0/0/0/1 defaults are passed */
1203                     break;
1204                 default:
1205                     FIXME("Unexpected fixed function texture coord input\n");
1206             }
1207         }
1208         switch (flags & ~WINED3DTTFF_PROJECTED) {
1209             /* case WINED3DTTFF_COUNT1: Won't ever get here */
1210             case WINED3DTTFF_COUNT2: mat[2] = mat[6] = mat[10] = mat[14] = 0;
1211             /* OpenGL divides the first 3 vertex coord by the 4th by default,
1212              * which is essentially the same as D3DTTFF_PROJECTED. Make sure that
1213              * the 4th coord evaluates to 1.0 to eliminate that.
1214              *
1215              * If the fixed function pipeline is used, the 4th value remains unused,
1216              * so there is no danger in doing this. With vertex shaders we have a
1217              * problem. Should an app hit that problem, the code here would have to
1218              * check for pixel shaders, and the shader has to undo the default gl divide.
1219              *
1220              * A more serious problem occurs if the app passes 4 coordinates in, and the
1221              * 4th is != 1.0(opengl default). This would have to be fixed in drawStridedSlow
1222              * or a replacement shader
1223              */
1224             default: mat[3] = mat[7] = mat[11] = 0; mat[15] = 1;
1225         }
1226     }
1227
1228     glLoadMatrixf(mat);
1229     checkGLcall("glLoadMatrixf(mat)");
1230 }
1231 #undef GLINFO_LOCATION
1232
1233 #define GLINFO_LOCATION ((IWineD3DImpl *)(This->wineD3D))->gl_info
1234
1235 /* This small helper function is used to convert a bitmask into the number of masked bits */
1236 unsigned int count_bits(unsigned int mask)
1237 {
1238     unsigned int count;
1239     for (count = 0; mask; ++count)
1240     {
1241         mask &= mask - 1;
1242     }
1243     return count;
1244 }
1245
1246 /* Helper function for retrieving color info for ChoosePixelFormat and wglChoosePixelFormatARB.
1247  * The later function requires individual color components. */
1248 BOOL getColorBits(WINED3DFORMAT fmt, short *redSize, short *greenSize, short *blueSize, short *alphaSize, short *totalSize)
1249 {
1250     const StaticPixelFormatDesc *desc;
1251
1252     TRACE("fmt: %s\n", debug_d3dformat(fmt));
1253     switch(fmt)
1254     {
1255         case WINED3DFMT_X8R8G8B8:
1256         case WINED3DFMT_R8G8B8:
1257         case WINED3DFMT_A8R8G8B8:
1258         case WINED3DFMT_A2R10G10B10:
1259         case WINED3DFMT_X1R5G5B5:
1260         case WINED3DFMT_A1R5G5B5:
1261         case WINED3DFMT_R5G6B5:
1262         case WINED3DFMT_X4R4G4B4:
1263         case WINED3DFMT_A4R4G4B4:
1264         case WINED3DFMT_R3G3B2:
1265         case WINED3DFMT_A8P8:
1266         case WINED3DFMT_P8:
1267             break;
1268         default:
1269             ERR("Unsupported format: %s\n", debug_d3dformat(fmt));
1270             return FALSE;
1271     }
1272
1273     desc = getFormatDescEntry(fmt, NULL, NULL);
1274     if(!desc)
1275     {
1276         ERR("Unable to look up format: 0x%x\n", fmt);
1277         return FALSE;
1278     }
1279     *redSize = count_bits(desc->redMask);
1280     *greenSize = count_bits(desc->greenMask);
1281     *blueSize = count_bits(desc->blueMask);
1282     *alphaSize = count_bits(desc->alphaMask);
1283     *totalSize = *redSize + *greenSize + *blueSize + *alphaSize;
1284
1285     TRACE("Returning red:  %d, green: %d, blue: %d, alpha: %d, total: %d for fmt=%s\n", *redSize, *greenSize, *blueSize, *alphaSize, *totalSize, debug_d3dformat(fmt));
1286     return TRUE;
1287 }
1288
1289 /* Helper function for retrieving depth/stencil info for ChoosePixelFormat and wglChoosePixelFormatARB */
1290 BOOL getDepthStencilBits(WINED3DFORMAT fmt, short *depthSize, short *stencilSize)
1291 {
1292     const StaticPixelFormatDesc *desc;
1293
1294     TRACE("fmt: %s\n", debug_d3dformat(fmt));
1295     switch(fmt)
1296     {
1297         case WINED3DFMT_D16_LOCKABLE:
1298         case WINED3DFMT_D16:
1299         case WINED3DFMT_D15S1:
1300         case WINED3DFMT_D24X8:
1301         case WINED3DFMT_D24X4S4:
1302         case WINED3DFMT_D24S8:
1303         case WINED3DFMT_D24FS8:
1304         case WINED3DFMT_D32:
1305         case WINED3DFMT_D32F_LOCKABLE:
1306             break;
1307         default:
1308             FIXME("Unsupported stencil format: %s\n", debug_d3dformat(fmt));
1309             return FALSE;
1310     }
1311
1312     desc = getFormatDescEntry(fmt, NULL, NULL);
1313     if(!desc)
1314     {
1315         ERR("Unable to look up format: 0x%x\n", fmt);
1316         return FALSE;
1317     }
1318     *depthSize = desc->depthSize;
1319     *stencilSize = desc->stencilSize;
1320
1321     TRACE("Returning depthSize: %d and stencilSize: %d for fmt=%s\n", *depthSize, *stencilSize, debug_d3dformat(fmt));
1322     return TRUE;
1323 }
1324
1325 #undef GLINFO_LOCATION
1326
1327 /* DirectDraw stuff */
1328 WINED3DFORMAT pixelformat_for_depth(DWORD depth) {
1329     switch(depth) {
1330         case 8:  return WINED3DFMT_P8;
1331         case 15: return WINED3DFMT_X1R5G5B5;
1332         case 16: return WINED3DFMT_R5G6B5;
1333         case 24: return WINED3DFMT_X8R8G8B8; /* Robots needs 24bit to be X8R8G8B8 */
1334         case 32: return WINED3DFMT_X8R8G8B8; /* EVE online and the Fur demo need 32bit AdapterDisplayMode to return X8R8G8B8 */
1335         default: return WINED3DFMT_UNKNOWN;
1336     }
1337 }
1338
1339 void multiply_matrix(WINED3DMATRIX *dest, const WINED3DMATRIX *src1, const WINED3DMATRIX *src2) {
1340     WINED3DMATRIX temp;
1341
1342     /* Now do the multiplication 'by hand'.
1343        I know that all this could be optimised, but this will be done later :-) */
1344     temp.u.s._11 = (src1->u.s._11 * src2->u.s._11) + (src1->u.s._21 * src2->u.s._12) + (src1->u.s._31 * src2->u.s._13) + (src1->u.s._41 * src2->u.s._14);
1345     temp.u.s._21 = (src1->u.s._11 * src2->u.s._21) + (src1->u.s._21 * src2->u.s._22) + (src1->u.s._31 * src2->u.s._23) + (src1->u.s._41 * src2->u.s._24);
1346     temp.u.s._31 = (src1->u.s._11 * src2->u.s._31) + (src1->u.s._21 * src2->u.s._32) + (src1->u.s._31 * src2->u.s._33) + (src1->u.s._41 * src2->u.s._34);
1347     temp.u.s._41 = (src1->u.s._11 * src2->u.s._41) + (src1->u.s._21 * src2->u.s._42) + (src1->u.s._31 * src2->u.s._43) + (src1->u.s._41 * src2->u.s._44);
1348
1349     temp.u.s._12 = (src1->u.s._12 * src2->u.s._11) + (src1->u.s._22 * src2->u.s._12) + (src1->u.s._32 * src2->u.s._13) + (src1->u.s._42 * src2->u.s._14);
1350     temp.u.s._22 = (src1->u.s._12 * src2->u.s._21) + (src1->u.s._22 * src2->u.s._22) + (src1->u.s._32 * src2->u.s._23) + (src1->u.s._42 * src2->u.s._24);
1351     temp.u.s._32 = (src1->u.s._12 * src2->u.s._31) + (src1->u.s._22 * src2->u.s._32) + (src1->u.s._32 * src2->u.s._33) + (src1->u.s._42 * src2->u.s._34);
1352     temp.u.s._42 = (src1->u.s._12 * src2->u.s._41) + (src1->u.s._22 * src2->u.s._42) + (src1->u.s._32 * src2->u.s._43) + (src1->u.s._42 * src2->u.s._44);
1353
1354     temp.u.s._13 = (src1->u.s._13 * src2->u.s._11) + (src1->u.s._23 * src2->u.s._12) + (src1->u.s._33 * src2->u.s._13) + (src1->u.s._43 * src2->u.s._14);
1355     temp.u.s._23 = (src1->u.s._13 * src2->u.s._21) + (src1->u.s._23 * src2->u.s._22) + (src1->u.s._33 * src2->u.s._23) + (src1->u.s._43 * src2->u.s._24);
1356     temp.u.s._33 = (src1->u.s._13 * src2->u.s._31) + (src1->u.s._23 * src2->u.s._32) + (src1->u.s._33 * src2->u.s._33) + (src1->u.s._43 * src2->u.s._34);
1357     temp.u.s._43 = (src1->u.s._13 * src2->u.s._41) + (src1->u.s._23 * src2->u.s._42) + (src1->u.s._33 * src2->u.s._43) + (src1->u.s._43 * src2->u.s._44);
1358
1359     temp.u.s._14 = (src1->u.s._14 * src2->u.s._11) + (src1->u.s._24 * src2->u.s._12) + (src1->u.s._34 * src2->u.s._13) + (src1->u.s._44 * src2->u.s._14);
1360     temp.u.s._24 = (src1->u.s._14 * src2->u.s._21) + (src1->u.s._24 * src2->u.s._22) + (src1->u.s._34 * src2->u.s._23) + (src1->u.s._44 * src2->u.s._24);
1361     temp.u.s._34 = (src1->u.s._14 * src2->u.s._31) + (src1->u.s._24 * src2->u.s._32) + (src1->u.s._34 * src2->u.s._33) + (src1->u.s._44 * src2->u.s._34);
1362     temp.u.s._44 = (src1->u.s._14 * src2->u.s._41) + (src1->u.s._24 * src2->u.s._42) + (src1->u.s._34 * src2->u.s._43) + (src1->u.s._44 * src2->u.s._44);
1363
1364     /* And copy the new matrix in the good storage.. */
1365     memcpy(dest, &temp, 16 * sizeof(float));
1366 }
1367
1368 DWORD get_flexible_vertex_size(DWORD d3dvtVertexType) {
1369     DWORD size = 0;
1370     int i;
1371     int numTextures = (d3dvtVertexType & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
1372
1373     if (d3dvtVertexType & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
1374     if (d3dvtVertexType & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
1375     if (d3dvtVertexType & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
1376     if (d3dvtVertexType & WINED3DFVF_PSIZE) size += sizeof(DWORD);
1377     switch (d3dvtVertexType & WINED3DFVF_POSITION_MASK) {
1378         case WINED3DFVF_XYZ:    size += 3 * sizeof(float); break;
1379         case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
1380         case WINED3DFVF_XYZB1:  size += 4 * sizeof(float); break;
1381         case WINED3DFVF_XYZB2:  size += 5 * sizeof(float); break;
1382         case WINED3DFVF_XYZB3:  size += 6 * sizeof(float); break;
1383         case WINED3DFVF_XYZB4:  size += 7 * sizeof(float); break;
1384         case WINED3DFVF_XYZB5:  size += 8 * sizeof(float); break;
1385         default: ERR("Unexpected position mask\n");
1386     }
1387     for (i = 0; i < numTextures; i++) {
1388         size += GET_TEXCOORD_SIZE_FROM_FVF(d3dvtVertexType, i) * sizeof(float);
1389     }
1390
1391     return size;
1392 }
1393
1394 /***********************************************************************
1395  * CalculateTexRect
1396  *
1397  * Calculates the dimensions of the opengl texture used for blits.
1398  * Handled oversized opengl textures and updates the source rectangle
1399  * accordingly
1400  *
1401  * Params:
1402  *  This: Surface to operate on
1403  *  Rect: Requested rectangle
1404  *
1405  * Returns:
1406  *  TRUE if the texture part can be loaded,
1407  *  FALSE otherwise
1408  *
1409  *********************************************************************/
1410 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
1411
1412 BOOL CalculateTexRect(IWineD3DSurfaceImpl *This, RECT *Rect, float glTexCoord[4]) {
1413     int x1 = Rect->left, x2 = Rect->right;
1414     int y1 = Rect->top, y2 = Rect->bottom;
1415     GLint maxSize = GL_LIMITS(texture_size);
1416
1417     TRACE("(%p)->(%d,%d)-(%d,%d)\n", This,
1418           Rect->left, Rect->top, Rect->right, Rect->bottom);
1419
1420     /* The sizes might be reversed */
1421     if(Rect->left > Rect->right) {
1422         x1 = Rect->right;
1423         x2 = Rect->left;
1424     }
1425     if(Rect->top > Rect->bottom) {
1426         y1 = Rect->bottom;
1427         y2 = Rect->top;
1428     }
1429
1430     /* No oversized texture? This is easy */
1431     if(!(This->Flags & SFLAG_OVERSIZE)) {
1432         /* Which rect from the texture do I need? */
1433         if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB) {
1434             glTexCoord[0] = (float) Rect->left;
1435             glTexCoord[2] = (float) Rect->top;
1436             glTexCoord[1] = (float) Rect->right;
1437             glTexCoord[3] = (float) Rect->bottom;
1438         } else {
1439             glTexCoord[0] = (float) Rect->left / (float) This->pow2Width;
1440             glTexCoord[2] = (float) Rect->top / (float) This->pow2Height;
1441             glTexCoord[1] = (float) Rect->right / (float) This->pow2Width;
1442             glTexCoord[3] = (float) Rect->bottom / (float) This->pow2Height;
1443         }
1444
1445         return TRUE;
1446     } else {
1447         /* Check if we can succeed at all */
1448         if( (x2 - x1) > maxSize ||
1449             (y2 - y1) > maxSize ) {
1450             TRACE("Requested rectangle is too large for gl\n");
1451             return FALSE;
1452         }
1453
1454         /* A part of the texture has to be picked. First, check if
1455          * some texture part is loaded already, if yes try to re-use it.
1456          * If the texture is dirty, or the part can't be used,
1457          * re-position the part to load
1458          */
1459         if(This->Flags & SFLAG_INTEXTURE) {
1460             if(This->glRect.left <= x1 && This->glRect.right >= x2 &&
1461                This->glRect.top <= y1 && This->glRect.bottom >= x2 ) {
1462                 /* Ok, the rectangle is ok, re-use it */
1463                 TRACE("Using existing gl Texture\n");
1464             } else {
1465                 /* Rectangle is not ok, dirtify the texture to reload it */
1466                 TRACE("Dirtifying texture to force reload\n");
1467                 This->Flags &= ~SFLAG_INTEXTURE;
1468             }
1469         }
1470
1471         /* Now if we are dirty(no else if!) */
1472         if(!(This->Flags & SFLAG_INTEXTURE)) {
1473             /* Set the new rectangle. Use the following strategy:
1474              * 1) Use as big textures as possible.
1475              * 2) Place the texture part in the way that the requested
1476              *    part is in the middle of the texture(well, almost)
1477              * 3) If the texture is moved over the edges of the
1478              *    surface, replace it nicely
1479              * 4) If the coord is not limiting the texture size,
1480              *    use the whole size
1481              */
1482             if((This->pow2Width) > maxSize) {
1483                 This->glRect.left = x1 - maxSize / 2;
1484                 if(This->glRect.left < 0) {
1485                     This->glRect.left = 0;
1486                 }
1487                 This->glRect.right = This->glRect.left + maxSize;
1488                 if(This->glRect.right > This->currentDesc.Width) {
1489                     This->glRect.right = This->currentDesc.Width;
1490                     This->glRect.left = This->glRect.right - maxSize;
1491                 }
1492             } else {
1493                 This->glRect.left = 0;
1494                 This->glRect.right = This->pow2Width;
1495             }
1496
1497             if(This->pow2Height > maxSize) {
1498                 This->glRect.top = x1 - GL_LIMITS(texture_size) / 2;
1499                 if(This->glRect.top < 0) This->glRect.top = 0;
1500                 This->glRect.bottom = This->glRect.left + maxSize;
1501                 if(This->glRect.bottom > This->currentDesc.Height) {
1502                     This->glRect.bottom = This->currentDesc.Height;
1503                     This->glRect.top = This->glRect.bottom - maxSize;
1504                 }
1505             } else {
1506                 This->glRect.top = 0;
1507                 This->glRect.bottom = This->pow2Height;
1508             }
1509             TRACE("(%p): Using rect (%d,%d)-(%d,%d)\n", This,
1510                    This->glRect.left, This->glRect.top, This->glRect.right, This->glRect.bottom);
1511         }
1512
1513         /* Re-calculate the rect to draw */
1514         Rect->left -= This->glRect.left;
1515         Rect->right -= This->glRect.left;
1516         Rect->top -= This->glRect.top;
1517         Rect->bottom -= This->glRect.top;
1518
1519         /* Get the gl coordinates. The gl rectangle is a power of 2, eigher the max size,
1520          * or the pow2Width / pow2Height of the surface.
1521          *
1522          * Can never be GL_TEXTURE_RECTANGLE_ARB because oversized surfaces are always set up
1523          * as regular GL_TEXTURE_2D.
1524          */
1525         glTexCoord[0] = (float) Rect->left / (float) (This->glRect.right - This->glRect.left);
1526         glTexCoord[2] = (float) Rect->top / (float) (This->glRect.bottom - This->glRect.top);
1527         glTexCoord[1] = (float) Rect->right / (float) (This->glRect.right - This->glRect.left);
1528         glTexCoord[3] = (float) Rect->bottom / (float) (This->glRect.bottom - This->glRect.top);
1529     }
1530     return TRUE;
1531 }
1532 #undef GLINFO_LOCATION
1533
1534 /* Hash table functions */
1535
1536 hash_table_t *hash_table_create(hash_function_t *hash_function, compare_function_t *compare_function)
1537 {
1538     hash_table_t *table;
1539     unsigned int initial_size = 8;
1540
1541     table = HeapAlloc(GetProcessHeap(), 0, sizeof(hash_table_t) + (initial_size * sizeof(struct list)));
1542     if (!table)
1543     {
1544         ERR("Failed to allocate table, returning NULL.\n");
1545         return NULL;
1546     }
1547
1548     table->hash_function = hash_function;
1549     table->compare_function = compare_function;
1550
1551     table->grow_size = initial_size - (initial_size >> 2);
1552     table->shrink_size = 0;
1553
1554     table->buckets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, initial_size * sizeof(struct list));
1555     if (!table->buckets)
1556     {
1557         ERR("Failed to allocate table buckets, returning NULL.\n");
1558         HeapFree(GetProcessHeap(), 0, table);
1559         return NULL;
1560     }
1561     table->bucket_count = initial_size;
1562
1563     table->entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, table->grow_size * sizeof(hash_table_entry_t));
1564     if (!table->entries)
1565     {
1566         ERR("Failed to allocate table entries, returning NULL.\n");
1567         HeapFree(GetProcessHeap(), 0, table->buckets);
1568         HeapFree(GetProcessHeap(), 0, table);
1569         return NULL;
1570     }
1571     table->entry_count = 0;
1572
1573     list_init(&table->free_entries);
1574     table->count = 0;
1575
1576     return table;
1577 }
1578
1579 void hash_table_destroy(hash_table_t *table, void (*free_value)(void *value, void *cb), void *cb)
1580 {
1581     unsigned int i = 0;
1582
1583     for (i = 0; i < table->entry_count; ++i)
1584     {
1585         if(free_value) {
1586             free_value(table->entries[i].value, cb);
1587         }
1588         HeapFree(GetProcessHeap(), 0, table->entries[i].key);
1589     }
1590
1591     HeapFree(GetProcessHeap(), 0, table->entries);
1592     HeapFree(GetProcessHeap(), 0, table->buckets);
1593     HeapFree(GetProcessHeap(), 0, table);
1594 }
1595
1596 static inline hash_table_entry_t *hash_table_get_by_idx(hash_table_t *table, void *key, unsigned int idx)
1597 {
1598     hash_table_entry_t *entry;
1599
1600     if (table->buckets[idx].next)
1601         LIST_FOR_EACH_ENTRY(entry, &(table->buckets[idx]), hash_table_entry_t, entry)
1602             if (table->compare_function(entry->key, key)) return entry;
1603
1604     return NULL;
1605 }
1606
1607 static BOOL hash_table_resize(hash_table_t *table, unsigned int new_bucket_count)
1608 {
1609     unsigned int new_entry_count = 0;
1610     hash_table_entry_t *new_entries;
1611     struct list *new_buckets;
1612     unsigned int grow_size = new_bucket_count - (new_bucket_count >> 2);
1613     unsigned int i;
1614
1615     new_buckets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, new_bucket_count * sizeof(struct list));
1616     if (!new_buckets)
1617     {
1618         ERR("Failed to allocate new buckets, returning FALSE.\n");
1619         return FALSE;
1620     }
1621
1622     new_entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, grow_size * sizeof(hash_table_entry_t));
1623     if (!new_entries)
1624     {
1625         ERR("Failed to allocate new entries, returning FALSE.\n");
1626         HeapFree(GetProcessHeap(), 0, new_buckets);
1627         return FALSE;
1628     }
1629
1630     for (i = 0; i < table->bucket_count; ++i)
1631     {
1632         if (table->buckets[i].next)
1633         {
1634             hash_table_entry_t *entry, *entry2;
1635
1636             LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &table->buckets[i], hash_table_entry_t, entry)
1637             {
1638                 int j;
1639                 hash_table_entry_t *new_entry = new_entries + (new_entry_count++);
1640                 *new_entry = *entry;
1641
1642                 j = new_entry->hash & (new_bucket_count - 1);
1643
1644                 if (!new_buckets[j].next) list_init(&new_buckets[j]);
1645                 list_add_head(&new_buckets[j], &new_entry->entry);
1646             }
1647         }
1648     }
1649
1650     HeapFree(GetProcessHeap(), 0, table->buckets);
1651     table->buckets = new_buckets;
1652
1653     HeapFree(GetProcessHeap(), 0, table->entries);
1654     table->entries = new_entries;
1655
1656     table->entry_count = new_entry_count;
1657     list_init(&table->free_entries);
1658
1659     table->bucket_count = new_bucket_count;
1660     table->grow_size = grow_size;
1661     table->shrink_size = new_bucket_count > 8 ? new_bucket_count >> 2 : 0;
1662
1663     return TRUE;
1664 }
1665
1666 void hash_table_put(hash_table_t *table, void *key, void *value)
1667 {
1668     unsigned int idx;
1669     unsigned int hash;
1670     hash_table_entry_t *entry;
1671
1672     hash = table->hash_function(key);
1673     idx = hash & (table->bucket_count - 1);
1674     entry = hash_table_get_by_idx(table, key, idx);
1675
1676     if (entry)
1677     {
1678         HeapFree(GetProcessHeap(), 0, key);
1679         entry->value = value;
1680
1681         if (!value)
1682         {
1683             HeapFree(GetProcessHeap(), 0, entry->key);
1684             entry->key = NULL;
1685
1686             /* Remove the entry */
1687             list_remove(&entry->entry);
1688             list_add_head(&table->free_entries, &entry->entry);
1689
1690             --table->count;
1691
1692             /* Shrink if necessary */
1693             if (table->count < table->shrink_size) {
1694                 if (!hash_table_resize(table, table->bucket_count >> 1))
1695                 {
1696                     ERR("Failed to shrink the table...\n");
1697                 }
1698             }
1699         }
1700
1701         return;
1702     }
1703
1704     if (!value) return;
1705
1706     /* Grow if necessary */
1707     if (table->count >= table->grow_size)
1708     {
1709         if (!hash_table_resize(table, table->bucket_count << 1))
1710         {
1711             ERR("Failed to grow the table, returning.\n");
1712             return;
1713         }
1714
1715         idx = hash & (table->bucket_count - 1);
1716     }
1717
1718     /* Find an entry to insert */
1719     if (!list_empty(&table->free_entries))
1720     {
1721         struct list *elem = list_head(&table->free_entries);
1722
1723         list_remove(elem);
1724         entry = LIST_ENTRY(elem, hash_table_entry_t, entry);
1725     } else {
1726         entry = table->entries + (table->entry_count++);
1727     }
1728
1729     /* Insert the entry */
1730     entry->key = key;
1731     entry->value = value;
1732     entry->hash = hash;
1733     if (!table->buckets[idx].next) list_init(&table->buckets[idx]);
1734     list_add_head(&table->buckets[idx], &entry->entry);
1735
1736     ++table->count;
1737 }
1738
1739 void hash_table_remove(hash_table_t *table, void *key)
1740 {
1741     hash_table_put(table, key, NULL);
1742 }
1743
1744 void *hash_table_get(hash_table_t *table, void *key)
1745 {
1746     unsigned int idx;
1747     hash_table_entry_t *entry;
1748
1749     idx = table->hash_function(key) & (table->bucket_count - 1);
1750     entry = hash_table_get_by_idx(table, key, idx);
1751
1752     return entry ? entry->value : NULL;
1753 }
1754
1755 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
1756 void gen_ffp_op(IWineD3DStateBlockImpl *stateblock, struct ffp_settings *settings, BOOL ignore_textype) {
1757 #define ARG1 0x01
1758 #define ARG2 0x02
1759 #define ARG0 0x04
1760     static const unsigned char args[WINED3DTOP_LERP + 1] = {
1761         /* undefined                        */  0,
1762         /* D3DTOP_DISABLE                   */  0,
1763         /* D3DTOP_SELECTARG1                */  ARG1,
1764         /* D3DTOP_SELECTARG2                */  ARG2,
1765         /* D3DTOP_MODULATE                  */  ARG1 | ARG2,
1766         /* D3DTOP_MODULATE2X                */  ARG1 | ARG2,
1767         /* D3DTOP_MODULATE4X                */  ARG1 | ARG2,
1768         /* D3DTOP_ADD                       */  ARG1 | ARG2,
1769         /* D3DTOP_ADDSIGNED                 */  ARG1 | ARG2,
1770         /* D3DTOP_ADDSIGNED2X               */  ARG1 | ARG2,
1771         /* D3DTOP_SUBTRACT                  */  ARG1 | ARG2,
1772         /* D3DTOP_ADDSMOOTH                 */  ARG1 | ARG2,
1773         /* D3DTOP_BLENDDIFFUSEALPHA         */  ARG1 | ARG2,
1774         /* D3DTOP_BLENDTEXTUREALPHA         */  ARG1 | ARG2,
1775         /* D3DTOP_BLENDFACTORALPHA          */  ARG1 | ARG2,
1776         /* D3DTOP_BLENDTEXTUREALPHAPM       */  ARG1 | ARG2,
1777         /* D3DTOP_BLENDCURRENTALPHA         */  ARG1 | ARG2,
1778         /* D3DTOP_PREMODULATE               */  ARG1 | ARG2,
1779         /* D3DTOP_MODULATEALPHA_ADDCOLOR    */  ARG1 | ARG2,
1780         /* D3DTOP_MODULATECOLOR_ADDALPHA    */  ARG1 | ARG2,
1781         /* D3DTOP_MODULATEINVALPHA_ADDCOLOR */  ARG1 | ARG2,
1782         /* D3DTOP_MODULATEINVCOLOR_ADDALPHA */  ARG1 | ARG2,
1783         /* D3DTOP_BUMPENVMAP                */  ARG1 | ARG2,
1784         /* D3DTOP_BUMPENVMAPLUMINANCE       */  ARG1 | ARG2,
1785         /* D3DTOP_DOTPRODUCT3               */  ARG1 | ARG2,
1786         /* D3DTOP_MULTIPLYADD               */  ARG1 | ARG2 | ARG0,
1787         /* D3DTOP_LERP                      */  ARG1 | ARG2 | ARG0
1788     };
1789     unsigned int i;
1790     DWORD ttff;
1791     DWORD cop, aop, carg0, carg1, carg2, aarg0, aarg1, aarg2;
1792
1793     for(i = 0; i < GL_LIMITS(texture_stages); i++) {
1794         IWineD3DBaseTextureImpl *texture;
1795         if(stateblock->textureState[i][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE) {
1796             settings->op[i].cop = WINED3DTOP_DISABLE;
1797             settings->op[i].aop = WINED3DTOP_DISABLE;
1798             /* 0x3F: set all 6 bits of the args to 1 */
1799             settings->op[i].carg0 = settings->op[i].carg1 = settings->op[i].carg2 = 0x3F;
1800             settings->op[i].aarg0 = settings->op[i].aarg1 = settings->op[i].aarg2 = 0x3F;
1801             settings->op[i].color_correction = WINED3DFMT_UNKNOWN;
1802             settings->op[i].dst = resultreg;
1803             i++;
1804             break;
1805         }
1806
1807         texture = (IWineD3DBaseTextureImpl *) stateblock->textures[i];
1808         if(texture) {
1809             settings->op[i].color_correction = texture->baseTexture.shader_conversion_group;
1810             if(ignore_textype) {
1811                 settings->op[i].tex_type = tex_1d;
1812             } else {
1813                 switch(stateblock->textureDimensions[i]) {
1814                     case GL_TEXTURE_1D:
1815                         settings->op[i].tex_type = tex_1d;
1816                         break;
1817                     case GL_TEXTURE_2D:
1818                         settings->op[i].tex_type = tex_2d;
1819                         break;
1820                     case GL_TEXTURE_3D:
1821                         settings->op[i].tex_type = tex_3d;
1822                         break;
1823                     case GL_TEXTURE_CUBE_MAP_ARB:
1824                         settings->op[i].tex_type = tex_cube;
1825                         break;
1826                     case GL_TEXTURE_RECTANGLE_ARB:
1827                         settings->op[i].tex_type = tex_rect;
1828                         break;
1829                 }
1830             }
1831         } else {
1832             settings->op[i].color_correction = WINED3DFMT_UNKNOWN;
1833             settings->op[i].tex_type = tex_1d;
1834         }
1835
1836         cop = stateblock->textureState[i][WINED3DTSS_COLOROP];
1837         aop = stateblock->textureState[i][WINED3DTSS_ALPHAOP];
1838
1839         carg1 = (args[cop] & ARG1) ? stateblock->textureState[i][WINED3DTSS_COLORARG1] : 0xffffffff;
1840         carg2 = (args[cop] & ARG2) ? stateblock->textureState[i][WINED3DTSS_COLORARG2] : 0xffffffff;
1841         carg0 = (args[cop] & ARG0) ? stateblock->textureState[i][WINED3DTSS_COLORARG0] : 0xffffffff;
1842
1843         if(is_invalid_op(stateblock->wineD3DDevice, i, cop,
1844                          carg1, carg2, carg0)) {
1845             carg0 = 0xffffffff;
1846             carg2 = 0xffffffff;
1847             carg1 = WINED3DTA_CURRENT;
1848             cop = WINED3DTOP_SELECTARG1;
1849         }
1850
1851         aarg1 = (args[aop] & ARG1) ? stateblock->textureState[i][WINED3DTSS_ALPHAARG1] : 0xffffffff;
1852         aarg2 = (args[aop] & ARG2) ? stateblock->textureState[i][WINED3DTSS_ALPHAARG2] : 0xffffffff;
1853         aarg0 = (args[aop] & ARG0) ? stateblock->textureState[i][WINED3DTSS_ALPHAARG0] : 0xffffffff;
1854
1855         if(i == 0 && stateblock->textures[0] &&
1856                   stateblock->renderState[WINED3DRS_COLORKEYENABLE] &&
1857                  (stateblock->textureDimensions[0] == GL_TEXTURE_2D ||
1858                   stateblock->textureDimensions[0] == GL_TEXTURE_RECTANGLE_ARB)) {
1859             IWineD3DSurfaceImpl *surf = (IWineD3DSurfaceImpl *) ((IWineD3DTextureImpl *) stateblock->textures[0])->surfaces[0];
1860
1861             if(surf->CKeyFlags & WINEDDSD_CKSRCBLT &&
1862                getFormatDescEntry(surf->resource.format, NULL, NULL)->alphaMask == 0x00000000) {
1863
1864                 if(aop == WINED3DTOP_DISABLE) {
1865                    aarg1 = WINED3DTA_TEXTURE;
1866                    aop = WINED3DTOP_SELECTARG1;
1867                 }
1868                 else if(aop == WINED3DTOP_SELECTARG1 && aarg1 != WINED3DTA_TEXTURE) {
1869                     if (stateblock->renderState[WINED3DRS_ALPHABLENDENABLE]) {
1870                         aarg2 = WINED3DTA_TEXTURE;
1871                         aop = WINED3DTOP_MODULATE;
1872                     }
1873                     else aarg1 = WINED3DTA_TEXTURE;
1874                 }
1875                 else if(aop == WINED3DTOP_SELECTARG2 && aarg2 != WINED3DTA_TEXTURE) {
1876                     if (stateblock->renderState[WINED3DRS_ALPHABLENDENABLE]) {
1877                         aarg1 = WINED3DTA_TEXTURE;
1878                         aop = WINED3DTOP_MODULATE;
1879                     }
1880                     else aarg2 = WINED3DTA_TEXTURE;
1881                 }
1882             }
1883         }
1884
1885         if(is_invalid_op(stateblock->wineD3DDevice, i, aop,
1886            aarg1, aarg2, aarg0)) {
1887                aarg0 = 0xffffffff;
1888                aarg2 = 0xffffffff;
1889                aarg1 = WINED3DTA_CURRENT;
1890                aop = WINED3DTOP_SELECTARG1;
1891         }
1892
1893         if(carg1 == WINED3DTA_TEXTURE || carg2 == WINED3DTA_TEXTURE || carg0 == WINED3DTA_TEXTURE ||
1894            aarg1 == WINED3DTA_TEXTURE || aarg2 == WINED3DTA_TEXTURE || aarg0 == WINED3DTA_TEXTURE) {
1895             ttff = stateblock->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1896             if(ttff == (WINED3DTTFF_PROJECTED | WINED3DTTFF_COUNT3)) {
1897                 settings->op[i].projected = proj_count3;
1898             } else if(ttff == (WINED3DTTFF_PROJECTED | WINED3DTTFF_COUNT4)) {
1899                 settings->op[i].projected = proj_count4;
1900             } else {
1901                 settings->op[i].projected = proj_none;
1902             }
1903         } else {
1904             settings->op[i].projected = proj_none;
1905         }
1906
1907         settings->op[i].cop = cop;
1908         settings->op[i].aop = aop;
1909         settings->op[i].carg0 = carg0;
1910         settings->op[i].carg1 = carg1;
1911         settings->op[i].carg2 = carg2;
1912         settings->op[i].aarg0 = aarg0;
1913         settings->op[i].aarg1 = aarg1;
1914         settings->op[i].aarg2 = aarg2;
1915
1916         if(stateblock->textureState[i][WINED3DTSS_RESULTARG] == WINED3DTA_TEMP) {
1917             settings->op[i].dst = tempreg;
1918         } else {
1919             settings->op[i].dst = resultreg;
1920         }
1921     }
1922
1923     /* Clear unsupported stages */
1924     for(; i < MAX_TEXTURES; i++) {
1925         memset(&settings->op[i], 0xff, sizeof(settings->op[i]));
1926     }
1927
1928     if(stateblock->renderState[WINED3DRS_FOGENABLE] == FALSE) {
1929         settings->fog = FOG_OFF;
1930     } else if(stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
1931         switch(stateblock->renderState[WINED3DRS_FOGVERTEXMODE]) {
1932             case WINED3DFOG_NONE:
1933             case WINED3DFOG_LINEAR:
1934                 settings->fog = FOG_LINEAR;
1935                 break;
1936             case WINED3DFOG_EXP:
1937                 settings->fog = FOG_EXP;
1938                 break;
1939             case WINED3DFOG_EXP2:
1940                 settings->fog = FOG_EXP2;
1941                 break;
1942         }
1943     } else {
1944         switch(stateblock->renderState[WINED3DRS_FOGTABLEMODE]) {
1945             case WINED3DFOG_LINEAR:
1946                 settings->fog = FOG_LINEAR;
1947                 break;
1948             case WINED3DFOG_EXP:
1949                 settings->fog = FOG_EXP;
1950                 break;
1951             case WINED3DFOG_EXP2:
1952                 settings->fog = FOG_EXP2;
1953                 break;
1954         }
1955     }
1956 }
1957 #undef GLINFO_LOCATION
1958
1959 struct ffp_desc *find_ffp_shader(hash_table_t *fragment_shaders, struct ffp_settings *settings)
1960 {
1961     return (struct ffp_desc *)hash_table_get(fragment_shaders, settings);}
1962
1963 void add_ffp_shader(hash_table_t *shaders, struct ffp_desc *desc) {
1964     struct ffp_settings *key = HeapAlloc(GetProcessHeap(), 0, sizeof(*key));
1965     /* Note that the key is the implementation independent part of the ffp_desc structure,
1966      * whereas desc points to an extended structure with implementation specific parts.
1967      * Make a copy of the key because hash_table_put takes ownership of it
1968      */
1969     *key = desc->settings;
1970     hash_table_put(shaders, key, desc);
1971 }
1972
1973 /* Activates the texture dimension according to the bound D3D texture.
1974  * Does not care for the colorop or correct gl texture unit(when using nvrc)
1975  * Requires the caller to activate the correct unit before
1976  */
1977 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
1978 void texture_activate_dimensions(DWORD stage, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1979     if(stateblock->textures[stage]) {
1980         switch(stateblock->textureDimensions[stage]) {
1981             case GL_TEXTURE_2D:
1982                 glDisable(GL_TEXTURE_3D);
1983                 checkGLcall("glDisable(GL_TEXTURE_3D)");
1984                 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
1985                     glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1986                     checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
1987                 }
1988                 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
1989                     glDisable(GL_TEXTURE_RECTANGLE_ARB);
1990                     checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
1991                 }
1992                 glEnable(GL_TEXTURE_2D);
1993                 checkGLcall("glEnable(GL_TEXTURE_2D)");
1994                 break;
1995             case GL_TEXTURE_RECTANGLE_ARB:
1996                 glDisable(GL_TEXTURE_2D);
1997                 checkGLcall("glDisable(GL_TEXTURE_2D)");
1998                 glDisable(GL_TEXTURE_3D);
1999                 checkGLcall("glDisable(GL_TEXTURE_3D)");
2000                 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
2001                     glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2002                     checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
2003                 }
2004                 glEnable(GL_TEXTURE_RECTANGLE_ARB);
2005                 checkGLcall("glEnable(GL_TEXTURE_RECTANGLE_ARB)");
2006                 break;
2007             case GL_TEXTURE_3D:
2008                 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
2009                     glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2010                     checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
2011                 }
2012                 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
2013                     glDisable(GL_TEXTURE_RECTANGLE_ARB);
2014                     checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
2015                 }
2016                 glDisable(GL_TEXTURE_2D);
2017                 checkGLcall("glDisable(GL_TEXTURE_2D)");
2018                 glEnable(GL_TEXTURE_3D);
2019                 checkGLcall("glEnable(GL_TEXTURE_3D)");
2020                 break;
2021             case GL_TEXTURE_CUBE_MAP_ARB:
2022                 glDisable(GL_TEXTURE_2D);
2023                 checkGLcall("glDisable(GL_TEXTURE_2D)");
2024                 glDisable(GL_TEXTURE_3D);
2025                 checkGLcall("glDisable(GL_TEXTURE_3D)");
2026                 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
2027                     glDisable(GL_TEXTURE_RECTANGLE_ARB);
2028                     checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
2029                 }
2030                 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
2031                 checkGLcall("glEnable(GL_TEXTURE_CUBE_MAP_ARB)");
2032               break;
2033         }
2034     } else {
2035         glEnable(GL_TEXTURE_2D);
2036         checkGLcall("glEnable(GL_TEXTURE_2D)");
2037         glDisable(GL_TEXTURE_3D);
2038         checkGLcall("glDisable(GL_TEXTURE_3D)");
2039         if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
2040             glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2041             checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
2042         }
2043         if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
2044             glDisable(GL_TEXTURE_RECTANGLE_ARB);
2045             checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
2046         }
2047         /* Binding textures is done by samplers. A dummy texture will be bound */
2048     }
2049 }
2050
2051 void sampler_texdim(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2052     DWORD sampler = state - STATE_SAMPLER(0);
2053     DWORD mapped_stage = stateblock->wineD3DDevice->texUnitMap[sampler];
2054
2055     /* No need to enable / disable anything here for unused samplers. The tex_colorop
2056     * handler takes care. Also no action is needed with pixel shaders, or if tex_colorop
2057     * will take care of this business
2058     */
2059     if(mapped_stage == -1 || mapped_stage >= GL_LIMITS(textures)) return;
2060     if(sampler >= stateblock->lowest_disabled_stage) return;
2061     if(use_ps(stateblock->wineD3DDevice)) return;
2062     if(isStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP))) return;
2063
2064     texture_activate_dimensions(sampler, stateblock, context);
2065 }
2066 #undef GLINFO_LOCATION
2067
2068 unsigned int ffp_program_key_hash(void *key) {
2069     struct ffp_settings *k = (struct ffp_settings *)key;
2070     unsigned int hash = 0, i;
2071     DWORD *blob;
2072
2073     /* This takes the texture op settings of stage 0 and 1 into account.
2074      * how exactly depends on the memory laybout of the compiler, but it
2075      * should not matter too much. Stages > 1 are used rarely, so there's
2076      * no need to process them. Even if they're used it is likely that
2077      * the ffp setup has distinct stage 0 and 1 settings.
2078      */
2079     for(i = 0; i < 2; i++) {
2080         blob = (DWORD *) &k->op[i];
2081         hash ^= blob[0] ^ blob[1];
2082     }
2083
2084     hash += ~(hash << 15);
2085     hash ^=  (hash >> 10);
2086     hash +=  (hash << 3);
2087     hash ^=  (hash >> 6);
2088     hash += ~(hash << 11);
2089     hash ^=  (hash >> 16);
2090
2091     return hash;
2092 }
2093
2094 BOOL ffp_program_key_compare(void *keya, void *keyb) {
2095     struct ffp_settings *ka = (struct ffp_settings *)keya;
2096     struct ffp_settings *kb = (struct ffp_settings *)keyb;
2097
2098     return memcmp(ka, kb, sizeof(*ka)) == 0;
2099 }