wined3d: Select the right shader backend when creating the device.
[wine] / dlls / wined3d / wined3d_private.h
1 /*
2  * Direct3D wine internal private include file
3  *
4  * Copyright 2002-2003 The wine-d3d team
5  * Copyright 2002-2003 Raphael Junqueira
6  * Copyright 2004 Jason Edmeades
7  * Copyright 2005 Oliver Stieber
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #ifndef __WINE_WINED3D_PRIVATE_H
25 #define __WINE_WINED3D_PRIVATE_H
26
27 #include <stdarg.h>
28 #include <math.h>
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #define COBJMACROS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39
40 #include "d3d9.h"
41 #include "d3d9types.h"
42 #include "wined3d_private_types.h"
43 #include "ddraw.h"
44 #include "wine/wined3d_interface.h"
45 #include "wine/wined3d_gl.h"
46 #include "wine/list.h"
47
48 /* Device caps */
49 #define MAX_PALETTES      256
50 #define MAX_STREAMS       16
51 #define MAX_TEXTURES      8
52 #define MAX_SAMPLERS      16
53 #define MAX_ACTIVE_LIGHTS 8
54 #define MAX_CLIPPLANES    WINED3DMAXUSERCLIPPLANES
55 #define MAX_LEVELS        256
56
57 #define MAX_CONST_I 16
58 #define MAX_CONST_B 16
59
60 /* Used for CreateStateBlock */
61 #define NUM_SAVEDPIXELSTATES_R     35
62 #define NUM_SAVEDPIXELSTATES_T     18
63 #define NUM_SAVEDPIXELSTATES_S     12
64 #define NUM_SAVEDVERTEXSTATES_R    31
65 #define NUM_SAVEDVERTEXSTATES_T    2
66 #define NUM_SAVEDVERTEXSTATES_S    1
67
68 extern const DWORD SavedPixelStates_R[NUM_SAVEDPIXELSTATES_R];
69 extern const DWORD SavedPixelStates_T[NUM_SAVEDPIXELSTATES_T];
70 extern const DWORD SavedPixelStates_S[NUM_SAVEDPIXELSTATES_S];
71 extern const DWORD SavedVertexStates_R[NUM_SAVEDVERTEXSTATES_R];
72 extern const DWORD SavedVertexStates_T[NUM_SAVEDVERTEXSTATES_T];
73 extern const DWORD SavedVertexStates_S[NUM_SAVEDVERTEXSTATES_S];
74
75 typedef enum _WINELOOKUP {
76     WINELOOKUP_WARPPARAM = 0,
77     WINELOOKUP_MAGFILTER = 1,
78     MAX_LOOKUPS          = 2
79 } WINELOOKUP;
80
81 extern int minLookup[MAX_LOOKUPS];
82 extern int maxLookup[MAX_LOOKUPS];
83 extern DWORD *stateLookup[MAX_LOOKUPS];
84
85 extern DWORD minMipLookup[WINED3DTEXF_ANISOTROPIC + 1][WINED3DTEXF_LINEAR + 1];
86
87 typedef struct _WINED3DGLTYPE {
88     int         d3dType;
89     GLint       size;
90     GLenum      glType;
91     GLboolean   normalized;
92     int         typesize;
93 } WINED3DGLTYPE;
94
95 /* NOTE: Make sure these are in the correct numerical order. (see /include/wined3d_types.h) */
96 static WINED3DGLTYPE const glTypeLookup[WINED3DDECLTYPE_UNUSED] = {
97                                   {WINED3DDECLTYPE_FLOAT1,    1, GL_FLOAT           , GL_FALSE ,sizeof(float)},
98                                   {WINED3DDECLTYPE_FLOAT2,    2, GL_FLOAT           , GL_FALSE ,sizeof(float)},
99                                   {WINED3DDECLTYPE_FLOAT3,    3, GL_FLOAT           , GL_FALSE ,sizeof(float)},
100                                   {WINED3DDECLTYPE_FLOAT4,    4, GL_FLOAT           , GL_FALSE ,sizeof(float)},
101                                   {WINED3DDECLTYPE_D3DCOLOR,  4, GL_UNSIGNED_BYTE   , GL_TRUE  ,sizeof(BYTE)},
102                                   {WINED3DDECLTYPE_UBYTE4,    4, GL_UNSIGNED_BYTE   , GL_FALSE ,sizeof(BYTE)},
103                                   {WINED3DDECLTYPE_SHORT2,    2, GL_SHORT           , GL_FALSE ,sizeof(short int)},
104                                   {WINED3DDECLTYPE_SHORT4,    4, GL_SHORT           , GL_FALSE ,sizeof(short int)},
105                                   {WINED3DDECLTYPE_UBYTE4N,   4, GL_UNSIGNED_BYTE   , GL_FALSE ,sizeof(BYTE)},
106                                   {WINED3DDECLTYPE_SHORT2N,   2, GL_SHORT           , GL_FALSE ,sizeof(short int)},
107                                   {WINED3DDECLTYPE_SHORT4N,   4, GL_SHORT           , GL_FALSE ,sizeof(short int)},
108                                   {WINED3DDECLTYPE_USHORT2N,  2, GL_UNSIGNED_SHORT  , GL_FALSE ,sizeof(short int)},
109                                   {WINED3DDECLTYPE_USHORT4N,  4, GL_UNSIGNED_SHORT  , GL_FALSE ,sizeof(short int)},
110                                   {WINED3DDECLTYPE_UDEC3,     3, GL_UNSIGNED_SHORT  , GL_FALSE ,sizeof(short int)},
111                                   {WINED3DDECLTYPE_DEC3N,     3, GL_SHORT           , GL_FALSE ,sizeof(short int)},
112                                   {WINED3DDECLTYPE_FLOAT16_2, 2, GL_FLOAT           , GL_FALSE ,sizeof(short int)},
113                                   {WINED3DDECLTYPE_FLOAT16_4, 4, GL_FLOAT           , GL_FALSE ,sizeof(short int)}};
114
115 #define WINED3D_ATR_TYPE(type)          glTypeLookup[type].d3dType
116 #define WINED3D_ATR_SIZE(type)          glTypeLookup[type].size
117 #define WINED3D_ATR_GLTYPE(type)        glTypeLookup[type].glType
118 #define WINED3D_ATR_NORMALIZED(type)    glTypeLookup[type].normalized
119 #define WINED3D_ATR_TYPESIZE(type)      glTypeLookup[type].typesize
120
121 /**
122  * Settings 
123  */
124 #define VS_NONE    0
125 #define VS_HW      1
126 #define VS_SW      2
127
128 #define PS_NONE    0
129 #define PS_HW      1
130
131 #define VBO_NONE   0
132 #define VBO_HW     1
133
134 #define NP2_NONE   0
135 #define NP2_REPACK 1
136 #define NP2_NATIVE 2
137
138 #define ORM_BACKBUFFER  0
139 #define ORM_PBUFFER     1
140 #define ORM_FBO         2
141
142 #define SHADER_SW   0
143 #define SHADER_ARB  1
144 #define SHADER_GLSL 2
145 #define SHADER_NONE 3
146
147 #define RTL_DISABLE   -1
148 #define RTL_AUTO       0
149 #define RTL_READDRAW   1
150 #define RTL_READTEX    2
151 #define RTL_TEXDRAW    3
152 #define RTL_TEXTEX     4
153
154 /* NOTE: When adding fields to this structure, make sure to update the default
155  * values in wined3d_main.c as well. */
156 typedef struct wined3d_settings_s {
157 /* vertex and pixel shader modes */
158   int vs_mode;
159   int ps_mode;
160   int vbo_mode;
161 /* Ideally, we don't want the user to have to request GLSL.  If the hardware supports GLSL,
162     we should use it.  However, until it's fully implemented, we'll leave it as a registry
163     setting for developers. */
164   BOOL glslRequested;
165 /* nonpower 2 function */
166   int nonpower2_mode;
167   int offscreen_rendering_mode;
168   int rendertargetlock_mode;
169 /* Memory tracking and object counting */
170   unsigned int emulated_textureram;
171 } wined3d_settings_t;
172
173 extern wined3d_settings_t wined3d_settings;
174
175 /* Shader backends */
176
177 typedef struct {
178     void (*shader_select)(IWineD3DDevice *iface, BOOL usePS, BOOL useVS);
179     void (*shader_select_depth_blt)(IWineD3DDevice *iface);
180     void (*shader_load_constants)(IWineD3DDevice *iface, char usePS, char useVS);
181     void (*shader_cleanup)(BOOL usePS, BOOL useVS);
182 } shader_backend_t;
183
184 extern const shader_backend_t glsl_shader_backend;
185 extern const shader_backend_t arb_program_shader_backend;
186 extern const shader_backend_t none_shader_backend;
187
188 /* X11 locking */
189
190 extern void (*wine_tsx11_lock_ptr)(void);
191 extern void (*wine_tsx11_unlock_ptr)(void);
192
193 /* As GLX relies on X, this is needed */
194 extern int num_lock;
195
196 #if 0
197 #define ENTER_GL() ++num_lock; if (num_lock > 1) FIXME("Recursive use of GL lock to: %d\n", num_lock); wine_tsx11_lock_ptr()
198 #define LEAVE_GL() if (num_lock != 1) FIXME("Recursive use of GL lock: %d\n", num_lock); --num_lock; wine_tsx11_unlock_ptr()
199 #else
200 #define ENTER_GL() wine_tsx11_lock_ptr()
201 #define LEAVE_GL() wine_tsx11_unlock_ptr()
202 #endif
203
204 /*****************************************************************************
205  * Defines
206  */
207
208 /* GL related defines */
209 /* ------------------ */
210 #define GL_SUPPORT(ExtName)           (GLINFO_LOCATION.supported[ExtName] != 0)
211 #define GL_LIMITS(ExtName)            (GLINFO_LOCATION.max_##ExtName)
212 #define GL_EXTCALL(FuncName)          (GLINFO_LOCATION.FuncName)
213 #define GL_VEND(_VendName)            (GLINFO_LOCATION.gl_vendor == VENDOR_##_VendName ? TRUE : FALSE)
214
215 #define D3DCOLOR_B_R(dw) (((dw) >> 16) & 0xFF)
216 #define D3DCOLOR_B_G(dw) (((dw) >>  8) & 0xFF)
217 #define D3DCOLOR_B_B(dw) (((dw) >>  0) & 0xFF)
218 #define D3DCOLOR_B_A(dw) (((dw) >> 24) & 0xFF)
219
220 #define D3DCOLOR_R(dw) (((float) (((dw) >> 16) & 0xFF)) / 255.0f)
221 #define D3DCOLOR_G(dw) (((float) (((dw) >>  8) & 0xFF)) / 255.0f)
222 #define D3DCOLOR_B(dw) (((float) (((dw) >>  0) & 0xFF)) / 255.0f)
223 #define D3DCOLOR_A(dw) (((float) (((dw) >> 24) & 0xFF)) / 255.0f)
224
225 #define D3DCOLORTOGLFLOAT4(dw, vec) \
226   (vec)[0] = D3DCOLOR_R(dw); \
227   (vec)[1] = D3DCOLOR_G(dw); \
228   (vec)[2] = D3DCOLOR_B(dw); \
229   (vec)[3] = D3DCOLOR_A(dw);
230
231 /* DirectX Device Limits */
232 /* --------------------- */
233 #define MAX_LEVELS  256  /* Maximum number of mipmap levels. Guessed at 256 */
234
235 #define MAX_STREAMS  16  /* Maximum possible streams - used for fixed size arrays
236                             See MaxStreams in MSDN under GetDeviceCaps */
237                          /* Maximum number of constants provided to the shaders */
238 #define HIGHEST_TRANSFORMSTATE 512 
239                          /* Highest value in WINED3DTRANSFORMSTATETYPE */
240 #define MAX_PALETTES      256
241
242 /* Checking of API calls */
243 /* --------------------- */
244 #define checkGLcall(A)                                          \
245 {                                                               \
246     GLint err = glGetError();                                   \
247     if (err == GL_NO_ERROR) {                                   \
248        TRACE("%s call ok %s / %d\n", A, __FILE__, __LINE__);    \
249                                                                 \
250     } else do {                                                 \
251        FIXME(">>>>>>>>>>>>>>>>> %x from %s @ %s / %d\n",        \
252                 err, A, __FILE__, __LINE__);                    \
253        err = glGetError();                                      \
254     } while (err != GL_NO_ERROR);                               \
255
256
257 /* Trace routines / diagnostics */
258 /* ---------------------------- */
259
260 /* Dump out a matrix and copy it */
261 #define conv_mat(mat,gl_mat)                                                                \
262 do {                                                                                        \
263     TRACE("%f %f %f %f\n", (mat)->u.s._11, (mat)->u.s._12, (mat)->u.s._13, (mat)->u.s._14); \
264     TRACE("%f %f %f %f\n", (mat)->u.s._21, (mat)->u.s._22, (mat)->u.s._23, (mat)->u.s._24); \
265     TRACE("%f %f %f %f\n", (mat)->u.s._31, (mat)->u.s._32, (mat)->u.s._33, (mat)->u.s._34); \
266     TRACE("%f %f %f %f\n", (mat)->u.s._41, (mat)->u.s._42, (mat)->u.s._43, (mat)->u.s._44); \
267     memcpy(gl_mat, (mat), 16 * sizeof(float));                                              \
268 } while (0)
269
270 /* Macro to dump out the current state of the light chain */
271 #define DUMP_LIGHT_CHAIN()                    \
272 {                                             \
273   PLIGHTINFOEL *el = This->stateBlock->lights;\
274   while (el) {                                \
275     TRACE("Light %p (glIndex %ld, d3dIndex %ld, enabled %d)\n", el, el->glIndex, el->OriginalIndex, el->lightEnabled);\
276     el = el->next;                            \
277   }                                           \
278 }
279
280 /* Trace vector and strided data information */
281 #define TRACE_VECTOR(name) TRACE( #name "=(%f, %f, %f, %f)\n", name.x, name.y, name.z, name.w);
282 #define TRACE_STRIDED(sd,name) TRACE( #name "=(data:%p, stride:%d, type:%d)\n", sd->u.s.name.lpData, sd->u.s.name.dwStride, sd->u.s.name.dwType);
283
284 /* Defines used for optimizations */
285
286 /*    Only reapply what is necessary */
287 #define REAPPLY_ALPHAOP  0x0001
288 #define REAPPLY_ALL      0xFFFF
289
290 /* Advance declaration of structures to satisfy compiler */
291 typedef struct IWineD3DStateBlockImpl IWineD3DStateBlockImpl;
292 typedef struct IWineD3DSurfaceImpl    IWineD3DSurfaceImpl;
293 typedef struct IWineD3DPaletteImpl    IWineD3DPaletteImpl;
294
295 /* Tracking */
296
297 /* TODO: Move some of this to the device */
298 long globalChangeGlRam(long glram);
299
300 /* Memory and object tracking */
301
302 /*Structure for holding information on all direct3d objects
303 useful for making sure tracking is ok and when release is called on a device!
304 and probably quite handy for debugging and dumping states out
305 */
306 typedef struct WineD3DGlobalStatistics {
307     int glsurfaceram; /* The aproximate amount of glTexture memory allocated for textures */
308 } WineD3DGlobalStatistics;
309
310 extern WineD3DGlobalStatistics* wineD3DGlobalStatistics;
311
312 /* Global variables */
313 extern const float identity[16];
314
315 /*****************************************************************************
316  * Compilable extra diagnostics
317  */
318
319 /* Trace information per-vertex: (extremely high amount of trace) */
320 #if 0 /* NOTE: Must be 0 in cvs */
321 # define VTRACE(A) TRACE A
322 #else 
323 # define VTRACE(A) 
324 #endif
325
326 /* Checking of per-vertex related GL calls */
327 /* --------------------- */
328 #define vcheckGLcall(A)                                         \
329 {                                                               \
330     GLint err = glGetError();                                   \
331     if (err == GL_NO_ERROR) {                                   \
332        VTRACE(("%s call ok %s / %d\n", A, __FILE__, __LINE__)); \
333                                                                 \
334     } else do {                                                 \
335        FIXME(">>>>>>>>>>>>>>>>> %x from %s @ %s / %d\n",        \
336                 err, A, __FILE__, __LINE__);                    \
337        err = glGetError();                                      \
338     } while (err != GL_NO_ERROR);                               \
339 }
340
341 /* TODO: Confirm each of these works when wined3d move completed */
342 #if 0 /* NOTE: Must be 0 in cvs */
343   /* To avoid having to get gigabytes of trace, the following can be compiled in, and at the start
344      of each frame, a check is made for the existence of C:\D3DTRACE, and if it exists d3d trace
345      is enabled, and if it doesn't exist it is disabled. */
346 # define FRAME_DEBUGGING
347   /*  Adding in the SINGLE_FRAME_DEBUGGING gives a trace of just what makes up a single frame, before
348       the file is deleted                                                                            */
349 # if 1 /* NOTE: Must be 1 in cvs, as this is mostly more useful than a trace from program start */
350 #  define SINGLE_FRAME_DEBUGGING
351 # endif  
352   /* The following, when enabled, lets you see the makeup of the frame, by drawprimitive calls.
353      It can only be enabled when FRAME_DEBUGGING is also enabled                               
354      The contents of the back buffer are written into /tmp/backbuffer_* after each primitive 
355      array is drawn.                                                                            */
356 # if 0 /* NOTE: Must be 0 in cvs, as this give a lot of ppm files when compiled in */                                                                                       
357 #  define SHOW_FRAME_MAKEUP 1
358 # endif  
359   /* The following, when enabled, lets you see the makeup of the all the textures used during each
360      of the drawprimitive calls. It can only be enabled when SHOW_FRAME_MAKEUP is also enabled.
361      The contents of the textures assigned to each stage are written into 
362      /tmp/texture_*_<Stage>.ppm after each primitive array is drawn.                            */
363 # if 0 /* NOTE: Must be 0 in cvs, as this give a lot of ppm files when compiled in */
364 #  define SHOW_TEXTURE_MAKEUP 0
365 # endif  
366 extern BOOL isOn;
367 extern BOOL isDumpingFrames;
368 extern LONG primCounter;
369 #endif
370
371 /*****************************************************************************
372  * Prototypes
373  */
374
375 /* Routine common to the draw primitive and draw indexed primitive routines */
376 void drawPrimitive(IWineD3DDevice *iface,
377                     int PrimitiveType,
378                     long NumPrimitives,
379                     /* for Indexed: */
380                     long  StartVertexIndex,
381                     UINT  numberOfVertices,
382                     long  StartIdx,
383                     short idxBytes,
384                     const void *idxData,
385                     int   minIndex,
386                     WineDirect3DVertexStridedData *DrawPrimStrideData);
387
388 void primitiveConvertToStridedData(IWineD3DDevice *iface, WineDirect3DVertexStridedData *strided, LONG BaseVertexIndex, BOOL *fixup);
389
390 void primitiveDeclarationConvertToStridedData(
391      IWineD3DDevice *iface,
392      BOOL useVertexShaderFunction,
393      WineDirect3DVertexStridedData *strided,
394      LONG BaseVertexIndex, 
395      BOOL *fixup);
396
397 void primitiveConvertFVFtoOffset(DWORD thisFVF,
398                                  DWORD stride,
399                                  BYTE *data,
400                                  WineDirect3DVertexStridedData *strided,
401                                  GLint streamVBO);
402
403 DWORD get_flexible_vertex_size(DWORD d3dvtVertexType);
404
405 #define eps 1e-8
406
407 #define GET_TEXCOORD_SIZE_FROM_FVF(d3dvtVertexType, tex_num) \
408     (((((d3dvtVertexType) >> (16 + (2 * (tex_num)))) + 1) & 0x03) + 1)
409
410 /* Routine to fill gl caps for swapchains and IWineD3D */
411 BOOL IWineD3DImpl_FillGLCaps(IWineD3D *iface, Display* display);
412
413 /* Macros for doing basic GPU detection based on opengl capabilities */
414 #define WINE_D3D6_CAPABLE(gl_info) (gl_info->supported[ARB_MULTITEXTURE])
415 #define WINE_D3D7_CAPABLE(gl_info) (gl_info->supported[ARB_TEXTURE_COMPRESSION] && gl_info->supported[ARB_TEXTURE_CUBE_MAP] && gl_info->supported[ARB_TEXTURE_ENV_DOT3])
416 #define WINE_D3D8_CAPABLE(gl_info) WINE_D3D7_CAPABLE(gl_info) && (gl_info->supported[ARB_MULTISAMPLE] && gl_info->supported[ARB_TEXTURE_BORDER_CLAMP])
417 #define WINE_D3D9_CAPABLE(gl_info) WINE_D3D8_CAPABLE(gl_info) && (gl_info->supported[ARB_FRAGMENT_PROGRAM] && gl_info->supported[ARB_VERTEX_SHADER])
418
419 /*****************************************************************************
420  * Internal representation of a light
421  */
422 typedef struct PLIGHTINFOEL PLIGHTINFOEL;
423 struct PLIGHTINFOEL {
424     WINED3DLIGHT OriginalParms; /* Note D3D8LIGHT == D3D9LIGHT */
425     DWORD        OriginalIndex;
426     LONG         glIndex;
427     BOOL         lightEnabled;
428     BOOL         changed;
429     BOOL         enabledChanged;
430
431     /* Converted parms to speed up swapping lights */
432     float                         lightPosn[4];
433     float                         lightDirn[4];
434     float                         exponent;
435     float                         cutoff;
436
437     PLIGHTINFOEL *next;
438     PLIGHTINFOEL *prev;
439 };
440
441 /* The default light parameters */
442 extern const WINED3DLIGHT WINED3D_default_light;
443
444 /*****************************************************************************
445  * IWineD3D implementation structure
446  */
447 typedef struct IWineD3DImpl
448 {
449     /* IUnknown fields */
450     const IWineD3DVtbl     *lpVtbl;
451     LONG                    ref;     /* Note: Ref counting not required */
452
453     /* WineD3D Information */
454     IUnknown               *parent;
455     UINT                    dxVersion;
456
457     /* GL Information */
458     BOOL                    isGLInfoValid;
459     WineD3D_GL_Info         gl_info;
460 } IWineD3DImpl;
461
462 extern const IWineD3DVtbl IWineD3D_Vtbl;
463
464 /** Hacked out start of a context manager!! **/
465 typedef struct glContext {
466     int Width;
467     int Height;
468     int usedcount;
469     GLXContext context;
470
471     Drawable drawable;
472     IWineD3DSurface *pSurface;
473 #if 0 /* TODO: someway to represent the state of the context */
474     IWineD3DStateBlock *pStateBlock;
475 #endif
476 /* a few other things like format */
477 } glContext;
478
479 /* TODO: setup some flags in the regestry to enable, disable pbuffer support
480 (since it will break quite a few things until contexts are managed properly!) */
481 extern BOOL pbuffer_support;
482 /* allocate one pbuffer per surface */
483 extern BOOL pbuffer_per_surface;
484
485 /* Maximum number of contexts/pbuffers to keep in cache,
486 set to 100 because ATI's drivers don't support deleting pBuffers properly
487 this needs to be migrated to a list and some option availalbe for controle the cache size.
488 */
489 #define CONTEXT_CACHE 100
490
491 typedef struct ResourceList {
492     IWineD3DResource         *resource;
493     struct ResourceList      *next;
494 } ResourceList;
495
496 /* A helper function that dumps a resource list */
497 void dumpResources(ResourceList *resources);
498
499 /*****************************************************************************
500  * IWineD3DDevice implementation structure
501  */
502 typedef struct IWineD3DDeviceImpl
503 {
504     /* IUnknown fields      */
505     const IWineD3DDeviceVtbl *lpVtbl;
506     LONG                    ref;     /* Note: Ref counting not required */
507
508     /* WineD3D Information  */
509     IUnknown               *parent;
510     IWineD3D               *wineD3D;
511
512     /* X and GL Information */
513     GLint                   maxConcurrentLights;
514
515     /* Selected capabilities */
516     int vs_selected_mode;
517     int ps_selected_mode;
518     const shader_backend_t *shader_backend;
519
520     /* Optimization */
521     BOOL                    modelview_valid;
522     BOOL                    proj_valid;
523     BOOL                    view_ident;        /* true iff view matrix is identity                */
524     BOOL                    last_was_rhw;      /* true iff last draw_primitive was in xyzrhw mode */
525     BOOL                    viewport_changed;  /* Was the viewport changed since the last draw?   */
526     GLenum                  tracking_parm;     /* Which source is tracking current colour         */
527     LONG                    tracking_color;    /* used iff GL_COLOR_MATERIAL was enabled          */
528 #define                         DISABLED_TRACKING  0  /* Disabled                                 */
529 #define                         IS_TRACKING        1  /* tracking_parm is tracking diffuse color  */
530 #define                         NEEDS_TRACKING     2  /* Tracking needs to be enabled when needed */
531 #define                         NEEDS_DISABLE      3  /* Tracking needs to be disabled when needed*/
532     UINT                    srcBlend;
533     UINT                    dstBlend;
534     UINT                    alphafunc;
535     BOOL                    texture_shader_active;  /* TODO: Confirm use is correct */
536     BOOL                    last_was_notclipped;
537     BOOL                    untransformed;
538
539     /* State block related */
540     BOOL                    isRecordingState;
541     IWineD3DStateBlockImpl *stateBlock;
542     IWineD3DStateBlockImpl *updateStateBlock;
543
544     /* Internal use fields  */
545     WINED3DDEVICE_CREATION_PARAMETERS createParms;
546     UINT                            adapterNo;
547     WINED3DDEVTYPE                  devType;
548
549     IWineD3DSwapChain     **swapchains;
550     uint                    NumberOfSwapChains;
551
552     ResourceList           *resources; /* a linked list to track resources created by the device */
553
554     /* Render Target Support */
555     IWineD3DSurface        *depthStencilBuffer;
556
557     IWineD3DSurface        *renderTarget;
558     IWineD3DSurface        *stencilBufferTarget;
559
560     /* palettes texture management */
561     PALETTEENTRY            palettes[MAX_PALETTES][256];
562     UINT                    currentPalette;
563
564     /* For rendering to a texture using glCopyTexImage */
565     BOOL                    render_offscreen;
566     WINED3D_DEPTHCOPYSTATE  depth_copy_state;
567     GLuint                  fbo;
568
569     /* Cursor management */
570     BOOL                    bCursorVisible;
571     UINT                    xHotSpot;
572     UINT                    yHotSpot;
573     UINT                    xScreenSpace;
574     UINT                    yScreenSpace;
575     UINT                    cursorWidth, cursorHeight;
576     GLuint                  cursorTexture;
577
578     /* Textures for when no other textures are mapped */
579     UINT                          dummyTextureName[MAX_TEXTURES];
580
581     /* Debug stream management */
582     BOOL                     debug;
583
584     /* Device state management */
585     HRESULT                 state;
586     BOOL                    d3d_initialized;
587
588     /* Screen buffer resources */
589     glContext contextCache[CONTEXT_CACHE];
590
591     /* A flag to check if endscene has been called before changing the render tartet */
592     BOOL sceneEnded;
593
594     /* process vertex shaders using software or hardware */
595     BOOL softwareVertexProcessing;
596
597     /* DirectDraw stuff */
598     HWND ddraw_window;
599     IWineD3DSurface *ddraw_primary;
600     DWORD ddraw_width, ddraw_height;
601     WINED3DFORMAT ddraw_format;
602     BOOL ddraw_fullscreen;
603
604     /* List of GLSL shader programs and their associated vertex & pixel shaders */
605     struct list glsl_shader_progs;
606
607
608     /* Final position fixup constant */
609     float                       posFixup[4];
610 } IWineD3DDeviceImpl;
611
612 extern const IWineD3DDeviceVtbl IWineD3DDevice_Vtbl;
613
614 /* Support for IWineD3DResource ::Set/Get/FreePrivateData. I don't think
615  * anybody uses it for much so a good implementation is optional. */
616 typedef struct PrivateData
617 {
618     struct PrivateData* next;
619
620     GUID tag;
621     DWORD flags; /* DDSPD_* */
622     DWORD uniqueness_value;
623
624     union
625     {
626         LPVOID data;
627         LPUNKNOWN object;
628     } ptr;
629
630     DWORD size;
631 } PrivateData;
632
633 void d3ddevice_set_ortho(IWineD3DDeviceImpl *This);
634
635 /*****************************************************************************
636  * IWineD3DResource implementation structure
637  */
638 typedef struct IWineD3DResourceClass
639 {
640     /* IUnknown fields */
641     LONG                    ref;     /* Note: Ref counting not required */
642
643     /* WineD3DResource Information */
644     IUnknown               *parent;
645     WINED3DRESOURCETYPE     resourceType;
646     IWineD3DDeviceImpl     *wineD3DDevice;
647     WINED3DPOOL             pool;
648     UINT                    size;
649     DWORD                   usage;
650     WINED3DFORMAT           format;
651     BYTE                   *allocatedMemory;
652     PrivateData            *privateData;
653
654 } IWineD3DResourceClass;
655
656 typedef struct IWineD3DResourceImpl
657 {
658     /* IUnknown & WineD3DResource Information     */
659     const IWineD3DResourceVtbl *lpVtbl;
660     IWineD3DResourceClass   resource;
661 } IWineD3DResourceImpl;
662
663
664 /*****************************************************************************
665  * IWineD3DVertexBuffer implementation structure (extends IWineD3DResourceImpl)
666  */
667 typedef struct IWineD3DVertexBufferImpl
668 {
669     /* IUnknown & WineD3DResource Information     */
670     const IWineD3DVertexBufferVtbl *lpVtbl;
671     IWineD3DResourceClass     resource;
672
673     /* WineD3DVertexBuffer specifics */
674     DWORD                     fvf;
675
676     /* Vertex buffer object support */
677     GLuint                    vbo;
678     BYTE                      Flags;
679     UINT                      stream;
680
681     UINT                      dirtystart, dirtyend;
682     LONG                      lockcount;
683
684     LONG                      declChanges, draws;
685     /* Last description of the buffer */
686     WineDirect3DVertexStridedData strided;
687 } IWineD3DVertexBufferImpl;
688
689 extern const IWineD3DVertexBufferVtbl IWineD3DVertexBuffer_Vtbl;
690
691 #define VBFLAG_LOAD           0x01    /* Data is written from allocatedMemory to the VBO */
692 #define VBFLAG_OPTIMIZED      0x02    /* Optimize has been called for the VB */
693 #define VBFLAG_DIRTY          0x04    /* Buffer data has been modified */
694 #define VBFLAG_STREAM         0x08    /* The vertex buffer is in a stream */
695 #define VBFLAG_HASDESC        0x10    /* A vertex description has been found */
696 #define VBFLAG_VBOCREATEFAIL  0x20    /* An attempt to create a vbo has failed */
697
698 /*****************************************************************************
699  * IWineD3DIndexBuffer implementation structure (extends IWineD3DResourceImpl)
700  */
701 typedef struct IWineD3DIndexBufferImpl
702 {
703     /* IUnknown & WineD3DResource Information     */
704     const IWineD3DIndexBufferVtbl *lpVtbl;
705     IWineD3DResourceClass     resource;
706
707     /* WineD3DVertexBuffer specifics */
708 } IWineD3DIndexBufferImpl;
709
710 extern const IWineD3DIndexBufferVtbl IWineD3DIndexBuffer_Vtbl;
711
712 /*****************************************************************************
713  * IWineD3DBaseTexture D3D- > openGL state map lookups
714  */
715 #define WINED3DFUNC_NOTSUPPORTED  -2
716 #define WINED3DFUNC_UNIMPLEMENTED -1
717
718 typedef enum winetexturestates {
719     WINED3DTEXSTA_ADDRESSU       = 0,
720     WINED3DTEXSTA_ADDRESSV       = 1,
721     WINED3DTEXSTA_ADDRESSW       = 2,
722     WINED3DTEXSTA_BORDERCOLOR    = 3,
723     WINED3DTEXSTA_MAGFILTER      = 4,
724     WINED3DTEXSTA_MINFILTER      = 5,
725     WINED3DTEXSTA_MIPFILTER      = 6,
726     WINED3DTEXSTA_MAXMIPLEVEL    = 7,
727     WINED3DTEXSTA_MAXANISOTROPY  = 8,
728     WINED3DTEXSTA_SRGBTEXTURE    = 9,
729     WINED3DTEXSTA_ELEMENTINDEX   = 10,
730     WINED3DTEXSTA_DMAPOFFSET     = 11,
731     WINED3DTEXSTA_TSSADDRESSW    = 12,
732     MAX_WINETEXTURESTATES        = 13,
733 } winetexturestates;
734
735 typedef struct Wined3dTextureStateMap {
736     CONST int state;
737     int function;
738 } Wined3dTextureStateMap;
739
740 /*****************************************************************************
741  * IWineD3DBaseTexture implementation structure (extends IWineD3DResourceImpl)
742  */
743 typedef struct IWineD3DBaseTextureClass
744 {
745     UINT                    levels;
746     BOOL                    dirty;
747     WINED3DFORMAT           format;
748     DWORD                   usage;
749     UINT                    textureName;
750     UINT                    LOD;
751     WINED3DTEXTUREFILTERTYPE filterType;
752     DWORD                   states[MAX_WINETEXTURESTATES];
753
754 } IWineD3DBaseTextureClass;
755
756 typedef struct IWineD3DBaseTextureImpl
757 {
758     /* IUnknown & WineD3DResource Information     */
759     const IWineD3DBaseTextureVtbl *lpVtbl;
760     IWineD3DResourceClass     resource;
761     IWineD3DBaseTextureClass  baseTexture;
762
763 } IWineD3DBaseTextureImpl;
764
765 /*****************************************************************************
766  * IWineD3DTexture implementation structure (extends IWineD3DBaseTextureImpl)
767  */
768 typedef struct IWineD3DTextureImpl
769 {
770     /* IUnknown & WineD3DResource/WineD3DBaseTexture Information     */
771     const IWineD3DTextureVtbl *lpVtbl;
772     IWineD3DResourceClass     resource;
773     IWineD3DBaseTextureClass  baseTexture;
774
775     /* IWineD3DTexture */
776     IWineD3DSurface          *surfaces[MAX_LEVELS];
777     
778     UINT                      width;
779     UINT                      height;
780     float                     pow2scalingFactorX;
781     float                     pow2scalingFactorY;
782
783 } IWineD3DTextureImpl;
784
785 extern const IWineD3DTextureVtbl IWineD3DTexture_Vtbl;
786
787 /*****************************************************************************
788  * IWineD3DCubeTexture implementation structure (extends IWineD3DBaseTextureImpl)
789  */
790 typedef struct IWineD3DCubeTextureImpl
791 {
792     /* IUnknown & WineD3DResource/WineD3DBaseTexture Information     */
793     const IWineD3DCubeTextureVtbl *lpVtbl;
794     IWineD3DResourceClass     resource;
795     IWineD3DBaseTextureClass  baseTexture;
796
797     /* IWineD3DCubeTexture */
798     IWineD3DSurface          *surfaces[6][MAX_LEVELS];
799
800     UINT                      edgeLength;
801     float                     pow2scalingFactor;
802
803 } IWineD3DCubeTextureImpl;
804
805 extern const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl;
806
807 typedef struct _WINED3DVOLUMET_DESC
808 {
809     UINT                    Width;
810     UINT                    Height;
811     UINT                    Depth;
812 } WINED3DVOLUMET_DESC;
813
814 /*****************************************************************************
815  * IWineD3DVolume implementation structure (extends IUnknown)
816  */
817 typedef struct IWineD3DVolumeImpl
818 {
819     /* IUnknown & WineD3DResource fields */
820     const IWineD3DVolumeVtbl  *lpVtbl;
821     IWineD3DResourceClass      resource;
822
823     /* WineD3DVolume Information */
824     WINED3DVOLUMET_DESC      currentDesc;
825     IWineD3DBase            *container;
826     UINT                    bytesPerPixel;
827
828     BOOL                    lockable;
829     BOOL                    locked;
830     WINED3DBOX              lockedBox;
831     WINED3DBOX              dirtyBox;
832     BOOL                    dirty;
833
834
835 } IWineD3DVolumeImpl;
836
837 extern const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl;
838
839 /*****************************************************************************
840  * IWineD3DVolumeTexture implementation structure (extends IWineD3DBaseTextureImpl)
841  */
842 typedef struct IWineD3DVolumeTextureImpl
843 {
844     /* IUnknown & WineD3DResource/WineD3DBaseTexture Information     */
845     const IWineD3DVolumeTextureVtbl *lpVtbl;
846     IWineD3DResourceClass     resource;
847     IWineD3DBaseTextureClass  baseTexture;
848
849     /* IWineD3DVolumeTexture */
850     IWineD3DVolume           *volumes[MAX_LEVELS];
851
852     UINT                      width;
853     UINT                      height;
854     UINT                      depth;
855 } IWineD3DVolumeTextureImpl;
856
857 extern const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl;
858
859 typedef struct _WINED3DSURFACET_DESC
860 {
861     WINED3DMULTISAMPLE_TYPE MultiSampleType;
862     DWORD                   MultiSampleQuality;
863     UINT                    Width;
864     UINT                    Height;
865 } WINED3DSURFACET_DESC;
866
867 /*****************************************************************************
868  * Structure for DIB Surfaces (GetDC and GDI surfaces)
869  */
870 typedef struct wineD3DSurface_DIB {
871     HBITMAP DIBsection;
872     void* bitmap_data;
873     HGDIOBJ holdbitmap;
874     BOOL client_memory;
875 } wineD3DSurface_DIB;
876
877 /*****************************************************************************
878  * IWineD3DSurface implementation structure
879  */
880 struct IWineD3DSurfaceImpl
881 {
882     /* IUnknown & IWineD3DResource Information     */
883     const IWineD3DSurfaceVtbl *lpVtbl;
884     IWineD3DResourceClass     resource;
885
886     /* IWineD3DSurface fields */
887     IWineD3DBase              *container;
888     WINED3DSURFACET_DESC      currentDesc;
889     IWineD3DPaletteImpl      *palette;
890
891     UINT                      bytesPerPixel;
892
893     /* TODO: move this off into a management class(maybe!) */
894     DWORD                      Flags;
895
896     UINT                      pow2Width;
897     UINT                      pow2Height;
898     UINT                      pow2Size;
899
900     /* Oversized texture */
901     RECT                      glRect;
902
903 #if 0
904     /* precalculated x and y scalings for texture coords */
905     float                     pow2scalingFactorX; /* =  (Width  / pow2Width ) */
906     float                     pow2scalingFactorY; /* =  (Height / pow2Height) */
907 #endif
908
909     RECT                      lockedRect;
910     RECT                      dirtyRect;
911
912     glDescriptor              glDescription;
913
914     /* For GetDC */
915     wineD3DSurface_DIB        dib;
916     HDC                       hDC;
917
918     /* Color keys for DDraw */
919     DDCOLORKEY                DestBltCKey;
920     DDCOLORKEY                DestOverlayCKey;
921     DDCOLORKEY                SrcOverlayCKey;
922     DDCOLORKEY                SrcBltCKey;
923     DWORD                     CKeyFlags;
924
925     DDCOLORKEY                glCKey;
926 };
927
928 extern const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl;
929 extern const IWineD3DSurfaceVtbl IWineGDISurface_Vtbl;
930
931 /* Predeclare the shared Surface functions */
932 HRESULT WINAPI IWineD3DSurfaceImpl_QueryInterface(IWineD3DSurface *iface, REFIID riid, LPVOID *ppobj);
933 ULONG WINAPI IWineD3DSurfaceImpl_AddRef(IWineD3DSurface *iface);
934 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface);
935 HRESULT WINAPI IWineD3DSurfaceImpl_GetParent(IWineD3DSurface *iface, IUnknown **pParent);
936 HRESULT WINAPI IWineD3DSurfaceImpl_GetDevice(IWineD3DSurface *iface, IWineD3DDevice** ppDevice);
937 HRESULT WINAPI IWineD3DSurfaceImpl_SetPrivateData(IWineD3DSurface *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags);
938 HRESULT WINAPI IWineD3DSurfaceImpl_GetPrivateData(IWineD3DSurface *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData);
939 HRESULT WINAPI IWineD3DSurfaceImpl_FreePrivateData(IWineD3DSurface *iface, REFGUID refguid);
940 DWORD   WINAPI IWineD3DSurfaceImpl_SetPriority(IWineD3DSurface *iface, DWORD PriorityNew);
941 DWORD   WINAPI IWineD3DSurfaceImpl_GetPriority(IWineD3DSurface *iface);
942 void    WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface);
943 WINED3DRESOURCETYPE WINAPI IWineD3DSurfaceImpl_GetType(IWineD3DSurface *iface);
944 HRESULT WINAPI IWineD3DSurfaceImpl_GetContainerParent(IWineD3DSurface* iface, IUnknown **ppContainerParent);
945 HRESULT WINAPI IWineD3DSurfaceImpl_GetContainer(IWineD3DSurface* iface, REFIID riid, void** ppContainer);
946 HRESULT WINAPI IWineD3DSurfaceImpl_GetDesc(IWineD3DSurface *iface, WINED3DSURFACE_DESC *pDesc);
947 HRESULT WINAPI IWineD3DSurfaceImpl_GetBltStatus(IWineD3DSurface *iface, DWORD Flags);
948 HRESULT WINAPI IWineD3DSurfaceImpl_GetFlipStatus(IWineD3DSurface *iface, DWORD Flags);
949 HRESULT WINAPI IWineD3DSurfaceImpl_IsLost(IWineD3DSurface *iface);
950 HRESULT WINAPI IWineD3DSurfaceImpl_Restore(IWineD3DSurface *iface);
951 HRESULT WINAPI IWineD3DSurfaceImpl_SetPixelFormat(IWineD3DSurface *iface, WINED3DFORMAT Format, BYTE *Surface, DWORD Size);
952 HRESULT WINAPI IWineD3DSurfaceImpl_GetPalette(IWineD3DSurface *iface, IWineD3DPalette **Pal);
953 HRESULT WINAPI IWineD3DSurfaceImpl_SetPalette(IWineD3DSurface *iface, IWineD3DPalette *Pal);
954 HRESULT WINAPI IWineD3DSurfaceImpl_SetColorKey(IWineD3DSurface *iface, DWORD Flags, DDCOLORKEY *CKey);
955 HRESULT WINAPI IWineD3DSurfaceImpl_CleanDirtyRect(IWineD3DSurface *iface);
956 extern HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect);
957 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container);
958 HRESULT WINAPI IWineD3DSurfaceImpl_SetPBufferState(IWineD3DSurface *iface, BOOL inPBuffer, BOOL  inTexture);
959 void WINAPI IWineD3DSurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target);
960 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription);
961 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface);
962 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format);
963 HRESULT WINAPI IWineGDISurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx);
964 HRESULT WINAPI IWineGDISurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans);
965 HRESULT WINAPI IWineD3DSurfaceImpl_SetPalette(IWineD3DSurface *iface, IWineD3DPalette *Pal);
966 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC);
967 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC);
968 DWORD WINAPI IWineD3DSurfaceImpl_GetPitch(IWineD3DSurface *iface);
969 HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface);
970 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem);
971 HRESULT WINAPI IWineD3DSurfaceImpl_SetOverlayPosition(IWineD3DSurface *iface, LONG X, LONG Y);
972 HRESULT WINAPI IWineD3DSurfaceImpl_GetOverlayPosition(IWineD3DSurface *iface, LONG *X, LONG *Y);
973 HRESULT WINAPI IWineD3DSurfaceImpl_UpdateOverlayZOrder(IWineD3DSurface *iface, DWORD Flags, IWineD3DSurface *Ref);
974 HRESULT WINAPI IWineD3DSurfaceImpl_UpdateOverlay(IWineD3DSurface *iface, RECT *SrcRect, IWineD3DSurface *DstSurface, RECT *DstRect, DWORD Flags, WINEDDOVERLAYFX *FX);
975
976 /* Surface flags: */
977 #define SFLAG_OVERSIZE    0x00000001 /* Surface is bigger than gl size, blts only */
978 #define SFLAG_CONVERTED   0x00000002 /* Converted for color keying or Palettized */
979 #define SFLAG_DIBSECTION  0x00000004 /* Has a DIB section attached for getdc */
980 #define SFLAG_DIRTY       0x00000008 /* Surface was locked by the app */
981 #define SFLAG_LOCKABLE    0x00000010 /* Surface can be locked */
982 #define SFLAG_DISCARD     0x00000020 /* ??? */
983 #define SFLAG_LOCKED      0x00000040 /* Surface is locked atm */
984 #define SFLAG_ACTIVELOCK  0x00000080 /* Not locked, but surface memory is needed */
985 #define SFLAG_INTEXTURE   0x00000100 /* ??? */
986 #define SFLAG_INPBUFFER   0x00000200 /* ??? */
987 #define SFLAG_NONPOW2     0x00000400 /* Surface sizes are not a power of 2 */
988 #define SFLAG_DYNLOCK     0x00000800 /* Surface is often locked by the app */
989 #define SFLAG_DYNCHANGE   0x00001800 /* Surface contents are changed very often, implies DYNLOCK */
990 #define SFLAG_DCINUSE     0x00002000 /* Set between GetDC and ReleaseDC calls */
991 #define SFLAG_GLDIRTY     0x00004000 /* The opengl texture is more up to date than the surface mem */
992 #define SFLAG_LOST        0x00008000 /* Surface lost flag for DDraw */
993 #define SFLAG_FORCELOAD   0x00010000 /* To force PreLoading of a scratch cursor */
994 #define SFLAG_USERPTR     0x00020000 /* The application allocated the memory for this surface */
995 #define SFLAG_GLCKEY      0x00040000 /* The gl texture was created with a color key */
996
997 /* In some conditions the surface memory must not be freed:
998  * SFLAG_OVERSIZE: Not all data can be kept in GL
999  * SFLAG_CONVERTED: Converting the data back would take too long
1000  * SFLAG_DIBSECTION: The dib code manages the memory
1001  * SFLAG_DIRTY: GL surface isn't up to date
1002  * SFLAG_LOCKED: The app requires access to the surface data
1003  * SFLAG_ACTIVELOCK: Some wined3d code needs the memory
1004  * SFLAG_DYNLOCK: Avoid freeing the data for performance
1005  * SFLAG_DYNCHANGE: Same reason as DYNLOCK
1006  */
1007 #define SFLAG_DONOTFREE  (SFLAG_OVERSIZE   | \
1008                           SFLAG_CONVERTED  | \
1009                           SFLAG_DIBSECTION | \
1010                           SFLAG_DIRTY      | \
1011                           SFLAG_LOCKED     | \
1012                           SFLAG_ACTIVELOCK | \
1013                           SFLAG_DYNLOCK    | \
1014                           SFLAG_DYNCHANGE  | \
1015                           SFLAG_USERPTR)
1016
1017 BOOL CalculateTexRect(IWineD3DSurfaceImpl *This, RECT *Rect, float glTexCoord[4]);
1018
1019 /* Alignment of the pitch */
1020 #define SURFACE_ALIGNMENT 4
1021
1022 /*****************************************************************************
1023  * IWineD3DVertexDeclaration implementation structure
1024  */
1025 typedef struct IWineD3DVertexDeclarationImpl {
1026  /* IUnknown  Information     */
1027   const IWineD3DVertexDeclarationVtbl *lpVtbl;
1028   LONG                    ref;     /* Note: Ref counting not required */
1029
1030   IUnknown               *parent;
1031   /** precomputed fvf if simple declaration */
1032   IWineD3DDeviceImpl     *wineD3DDevice;
1033   DWORD   fvf[MAX_STREAMS];
1034   DWORD   allFVF;
1035
1036   /** dx8 compatible Declaration fields */
1037   DWORD*  pDeclaration8;
1038   DWORD   declaration8Length;
1039
1040   /** dx9+ */
1041   D3DVERTEXELEMENT9 *pDeclaration9;
1042   UINT               declaration9NumElements;
1043
1044   WINED3DVERTEXELEMENT  *pDeclarationWine;
1045   UINT                   declarationWNumElements;
1046   
1047   float                 *constants;
1048   
1049 } IWineD3DVertexDeclarationImpl;
1050
1051 extern const IWineD3DVertexDeclarationVtbl IWineD3DVertexDeclaration_Vtbl;
1052
1053 /*****************************************************************************
1054  * IWineD3DStateBlock implementation structure
1055  */
1056
1057 /* Internal state Block for Begin/End/Capture/Create/Apply info  */
1058 /*   Note: Very long winded but gl Lists are not flexible enough */
1059 /*   to resolve everything we need, so doing it manually for now */
1060 typedef struct SAVEDSTATES {
1061         BOOL                      indices;
1062         BOOL                      material;
1063         BOOL                      fvf;
1064         BOOL                      streamSource[MAX_STREAMS];
1065         BOOL                      streamFreq[MAX_STREAMS];
1066         BOOL                      textures[MAX_SAMPLERS];
1067         BOOL                      transform[HIGHEST_TRANSFORMSTATE + 1];
1068         BOOL                      viewport;
1069         BOOL                      renderState[WINEHIGHEST_RENDER_STATE + 1];
1070         BOOL                      textureState[MAX_TEXTURES][WINED3D_HIGHEST_TEXTURE_STATE + 1];
1071         BOOL                      samplerState[MAX_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1];
1072         BOOL                      clipplane[MAX_CLIPPLANES];
1073         BOOL                      vertexDecl;
1074         BOOL                      pixelShader;
1075         BOOL                      pixelShaderConstantsB[MAX_CONST_B];
1076         BOOL                      pixelShaderConstantsI[MAX_CONST_I];
1077         BOOL                     *pixelShaderConstantsF;
1078         BOOL                      vertexShader;
1079         BOOL                      vertexShaderConstantsB[MAX_CONST_B];
1080         BOOL                      vertexShaderConstantsI[MAX_CONST_I];
1081         BOOL                     *vertexShaderConstantsF;
1082 } SAVEDSTATES;
1083
1084 typedef struct {
1085     struct list entry;
1086     int idx;
1087 } constant_entry;
1088
1089 struct IWineD3DStateBlockImpl
1090 {
1091     /* IUnknown fields */
1092     const IWineD3DStateBlockVtbl *lpVtbl;
1093     LONG                      ref;     /* Note: Ref counting not required */
1094
1095     /* IWineD3DStateBlock information */
1096     IUnknown                 *parent;
1097     IWineD3DDeviceImpl       *wineD3DDevice;
1098     WINED3DSTATEBLOCKTYPE     blockType;
1099
1100     /* Array indicating whether things have been set or changed */
1101     SAVEDSTATES               changed;
1102     SAVEDSTATES               set;
1103     struct list               set_vconstantsF;
1104     struct list               set_pconstantsF;
1105
1106     /* Drawing - Vertex Shader or FVF related */
1107     DWORD                     fvf;
1108     /* Vertex Shader Declaration */
1109     IWineD3DVertexDeclaration *vertexDecl;
1110
1111     IWineD3DVertexShader      *vertexShader;
1112
1113     /* Vertex Shader Constants */
1114     BOOL                       vertexShaderConstantB[MAX_CONST_B];
1115     INT                        vertexShaderConstantI[MAX_CONST_I * 4];
1116     float                     *vertexShaderConstantF;
1117
1118     /* Stream Source */
1119     BOOL                      streamIsUP;
1120     UINT                      streamStride[MAX_STREAMS];
1121     UINT                      streamOffset[MAX_STREAMS];
1122     IWineD3DVertexBuffer     *streamSource[MAX_STREAMS];
1123     UINT                      streamFreq[MAX_STREAMS];
1124     UINT                      streamFlags[MAX_STREAMS];     /*0 | WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA  */
1125
1126     /* Indices */
1127     IWineD3DIndexBuffer*      pIndexData;
1128     UINT                      baseVertexIndex; /* Note: only used for d3d8 */
1129
1130     /* Transform */
1131     WINED3DMATRIX             transforms[HIGHEST_TRANSFORMSTATE + 1];
1132
1133     /* Lights */
1134     PLIGHTINFOEL             *lights; /* NOTE: active GL lights must be front of the chain */
1135
1136     /* Clipping */
1137     double                    clipplane[MAX_CLIPPLANES][4];
1138     WINED3DCLIPSTATUS         clip_status;
1139
1140     /* ViewPort */
1141     WINED3DVIEWPORT           viewport;
1142
1143     /* Material */
1144     WINED3DMATERIAL           material;
1145
1146     /* Pixel Shader */
1147     IWineD3DPixelShader      *pixelShader;
1148
1149     /* Pixel Shader Constants */
1150     BOOL                       pixelShaderConstantB[MAX_CONST_B];
1151     INT                        pixelShaderConstantI[MAX_CONST_I * 4];
1152     float                     *pixelShaderConstantF;
1153
1154     /* Indexed Vertex Blending */
1155     WINED3DVERTEXBLENDFLAGS   vertex_blend;
1156     FLOAT                     tween_factor;
1157
1158     /* RenderState */
1159     DWORD                     renderState[WINEHIGHEST_RENDER_STATE + 1];
1160
1161     /* Texture */
1162     IWineD3DBaseTexture      *textures[MAX_SAMPLERS];
1163     int                       textureDimensions[MAX_SAMPLERS];
1164
1165     /* Texture State Stage */
1166     DWORD                     textureState[MAX_TEXTURES][WINED3D_HIGHEST_TEXTURE_STATE + 1];
1167     /* Sampler States */
1168     DWORD                     samplerState[MAX_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1];
1169
1170     /* Current GLSL Shader Program */
1171     struct glsl_shader_prog_link *glsl_program;
1172 };
1173
1174 extern void stateblock_savedstates_set(
1175     IWineD3DStateBlock* iface,
1176     SAVEDSTATES* states,
1177     BOOL value);
1178
1179 extern void stateblock_savedstates_copy(
1180     IWineD3DStateBlock* iface,
1181     SAVEDSTATES* dest,
1182     SAVEDSTATES* source);
1183
1184 extern void stateblock_copy(
1185     IWineD3DStateBlock* destination,
1186     IWineD3DStateBlock* source);
1187
1188 extern const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl;
1189
1190 /*****************************************************************************
1191  * IWineD3DQueryImpl implementation structure (extends IUnknown)
1192  */
1193 typedef struct IWineD3DQueryImpl
1194 {
1195     const IWineD3DQueryVtbl  *lpVtbl;
1196     LONG                      ref;     /* Note: Ref counting not required */
1197     
1198     IUnknown                 *parent;
1199     /*TODO: replace with iface usage */
1200 #if 0
1201     IWineD3DDevice         *wineD3DDevice;
1202 #else
1203     IWineD3DDeviceImpl       *wineD3DDevice;
1204 #endif
1205
1206     /* IWineD3DQuery fields */
1207     WINED3DQUERYTYPE         type;
1208     /* TODO: Think about using a IUnknown instead of a void* */
1209     void                     *extendedData;
1210     
1211   
1212 } IWineD3DQueryImpl;
1213
1214 extern const IWineD3DQueryVtbl IWineD3DQuery_Vtbl;
1215
1216 /* Datastructures for IWineD3DQueryImpl.extendedData */
1217 typedef struct  WineQueryOcclusionData {
1218     GLuint  queryId;
1219 } WineQueryOcclusionData;
1220
1221
1222 /*****************************************************************************
1223  * IWineD3DSwapChainImpl implementation structure (extends IUnknown)
1224  */
1225
1226 typedef struct IWineD3DSwapChainImpl
1227 {
1228     /*IUnknown part*/
1229     IWineD3DSwapChainVtbl    *lpVtbl;
1230     LONG                      ref;     /* Note: Ref counting not required */
1231
1232     IUnknown                 *parent;
1233     IWineD3DDeviceImpl       *wineD3DDevice;
1234
1235     /* IWineD3DSwapChain fields */
1236     IWineD3DSurface         **backBuffer;
1237     IWineD3DSurface          *frontBuffer;
1238     BOOL                      wantsDepthStencilBuffer;
1239     D3DPRESENT_PARAMETERS     presentParms;
1240
1241     /* TODO: move everything up to drawable off into a context manager
1242       and store the 'data' in the contextManagerData interface.
1243     IUnknown                  *contextManagerData;
1244     */
1245
1246     HWND                    win_handle;
1247     Window                  win;
1248     Display                *display;
1249
1250     GLXContext              glCtx;
1251     XVisualInfo            *visInfo;
1252     GLXContext              render_ctx;
1253     /* This has been left in device for now, but needs moving off into a rendertarget management class and separated out from swapchains and devices. */
1254     Drawable                drawable;
1255 } IWineD3DSwapChainImpl;
1256
1257 extern IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl;
1258
1259 /*****************************************************************************
1260  * Utility function prototypes 
1261  */
1262
1263 /* Trace routines */
1264 const char* debug_d3dformat(WINED3DFORMAT fmt);
1265 const char* debug_d3ddevicetype(WINED3DDEVTYPE devtype);
1266 const char* debug_d3dresourcetype(WINED3DRESOURCETYPE res);
1267 const char* debug_d3dusage(DWORD usage);
1268 const char* debug_d3dusagequery(DWORD usagequery);
1269 const char* debug_d3ddeclmethod(WINED3DDECLMETHOD method);
1270 const char* debug_d3ddecltype(WINED3DDECLTYPE type);
1271 const char* debug_d3ddeclusage(BYTE usage);
1272 const char* debug_d3dprimitivetype(WINED3DPRIMITIVETYPE PrimitiveType);
1273 const char* debug_d3drenderstate(DWORD state);
1274 const char* debug_d3dsamplerstate(DWORD state);
1275 const char* debug_d3dtexturestate(DWORD state);
1276 const char* debug_d3dtstype(WINED3DTRANSFORMSTATETYPE tstype);
1277 const char* debug_d3dpool(WINED3DPOOL pool);
1278
1279 /* Routines for GL <-> D3D values */
1280 GLenum StencilOp(DWORD op);
1281 GLenum CompareFunc(DWORD func);
1282 void   set_tex_op(IWineD3DDevice *iface, BOOL isAlpha, int Stage, WINED3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3);
1283 void   set_tex_op_nvrc(IWineD3DDevice *iface, BOOL is_alpha, int stage, WINED3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3, INT texture_idx);
1284 void   set_texture_matrix(const float *smat, DWORD flags, BOOL calculatedCoords);
1285
1286 int D3DFmtMakeGlCfg(WINED3DFORMAT BackBufferFormat, WINED3DFORMAT StencilBufferFormat, int *attribs, int* nAttribs, BOOL alternate);
1287
1288 /* Math utils */
1289 void multiply_matrix(WINED3DMATRIX *dest, const WINED3DMATRIX *src1, const WINED3DMATRIX *src2);
1290
1291 /*****************************************************************************
1292  * To enable calling of inherited functions, requires prototypes 
1293  *
1294  * Note: Only require classes which are subclassed, ie resource, basetexture, 
1295  */
1296     /*** IUnknown methods ***/
1297     extern HRESULT WINAPI IWineD3DResourceImpl_QueryInterface(IWineD3DResource *iface, REFIID riid, void** ppvObject);
1298     extern ULONG WINAPI IWineD3DResourceImpl_AddRef(IWineD3DResource *iface);
1299     extern ULONG WINAPI IWineD3DResourceImpl_Release(IWineD3DResource *iface);
1300     /*** IWineD3DResource methods ***/
1301     extern HRESULT WINAPI IWineD3DResourceImpl_GetParent(IWineD3DResource *iface, IUnknown **pParent);
1302     extern HRESULT WINAPI IWineD3DResourceImpl_GetDevice(IWineD3DResource *iface, IWineD3DDevice ** ppDevice);
1303     extern HRESULT WINAPI IWineD3DResourceImpl_SetPrivateData(IWineD3DResource *iface, REFGUID  refguid, CONST void * pData, DWORD  SizeOfData, DWORD  Flags);
1304     extern HRESULT WINAPI IWineD3DResourceImpl_GetPrivateData(IWineD3DResource *iface, REFGUID  refguid, void * pData, DWORD * pSizeOfData);
1305     extern HRESULT WINAPI IWineD3DResourceImpl_FreePrivateData(IWineD3DResource *iface, REFGUID  refguid);
1306     extern DWORD WINAPI IWineD3DResourceImpl_SetPriority(IWineD3DResource *iface, DWORD  PriorityNew);
1307     extern DWORD WINAPI IWineD3DResourceImpl_GetPriority(IWineD3DResource *iface);
1308     extern void WINAPI IWineD3DResourceImpl_PreLoad(IWineD3DResource *iface);
1309     extern WINED3DRESOURCETYPE WINAPI IWineD3DResourceImpl_GetType(IWineD3DResource *iface);
1310     /*** class static members ***/
1311     void IWineD3DResourceImpl_CleanUp(IWineD3DResource *iface);
1312
1313     /*** IUnknown methods ***/
1314     extern HRESULT WINAPI IWineD3DBaseTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, void** ppvObject);
1315     extern ULONG WINAPI IWineD3DBaseTextureImpl_AddRef(IWineD3DBaseTexture *iface);
1316     extern ULONG WINAPI IWineD3DBaseTextureImpl_Release(IWineD3DBaseTexture *iface);
1317     /*** IWineD3DResource methods ***/
1318     extern HRESULT WINAPI IWineD3DBaseTextureImpl_GetParent(IWineD3DBaseTexture *iface, IUnknown **pParent);
1319     extern HRESULT WINAPI IWineD3DBaseTextureImpl_GetDevice(IWineD3DBaseTexture *iface, IWineD3DDevice ** ppDevice);
1320     extern HRESULT WINAPI IWineD3DBaseTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface, REFGUID  refguid, CONST void * pData, DWORD  SizeOfData, DWORD  Flags);
1321     extern HRESULT WINAPI IWineD3DBaseTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface, REFGUID  refguid, void * pData, DWORD * pSizeOfData);
1322     extern HRESULT WINAPI IWineD3DBaseTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID  refguid);
1323     extern DWORD WINAPI IWineD3DBaseTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD  PriorityNew);
1324     extern DWORD WINAPI IWineD3DBaseTextureImpl_GetPriority(IWineD3DBaseTexture *iface);
1325     extern void WINAPI IWineD3DBaseTextureImpl_PreLoad(IWineD3DBaseTexture *iface);
1326     extern WINED3DRESOURCETYPE WINAPI IWineD3DBaseTextureImpl_GetType(IWineD3DBaseTexture *iface);
1327     /*** IWineD3DBaseTexture methods ***/
1328     extern DWORD WINAPI IWineD3DBaseTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew);
1329     extern DWORD WINAPI IWineD3DBaseTextureImpl_GetLOD(IWineD3DBaseTexture *iface);
1330     extern DWORD WINAPI IWineD3DBaseTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface);
1331     extern HRESULT WINAPI IWineD3DBaseTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType);
1332     extern WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DBaseTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface);
1333     extern void WINAPI IWineD3DBaseTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface);
1334     extern BOOL WINAPI IWineD3DBaseTextureImpl_SetDirty(IWineD3DBaseTexture *iface, BOOL);
1335     extern BOOL WINAPI IWineD3DBaseTextureImpl_GetDirty(IWineD3DBaseTexture *iface);
1336
1337     extern BYTE* WINAPI IWineD3DVertexBufferImpl_GetMemory(IWineD3DVertexBuffer* iface, DWORD iOffset, GLint *vbo);
1338     extern HRESULT WINAPI IWineD3DVertexBufferImpl_ReleaseMemory(IWineD3DVertexBuffer* iface);
1339     extern HRESULT WINAPI IWineD3DBaseTextureImpl_BindTexture(IWineD3DBaseTexture *iface);
1340     extern HRESULT WINAPI IWineD3DBaseTextureImpl_UnBindTexture(IWineD3DBaseTexture *iface);
1341     extern void WINAPI IWineD3DBaseTextureImpl_ApplyStateChanges(IWineD3DBaseTexture *iface, const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1], const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]);
1342     /*** class static members ***/
1343     void IWineD3DBaseTextureImpl_CleanUp(IWineD3DBaseTexture *iface);
1344
1345 struct SHADER_OPCODE_ARG;
1346 typedef void (*shader_fct_t)();
1347 typedef void (*SHADER_HANDLER) (struct SHADER_OPCODE_ARG*);
1348
1349 /* Struct to maintain a list of GLSL shader programs and their associated pixel and
1350  * vertex shaders.  A list of this type is maintained on the DeviceImpl, and is only
1351  * used if the user is using GLSL shaders. */
1352 struct glsl_shader_prog_link {
1353     struct list             entry;
1354     GLhandleARB             programId;
1355     GLhandleARB             *vuniformF_locations;
1356     GLhandleARB             *puniformF_locations;
1357     IWineD3DVertexShader*   vertexShader;
1358     IWineD3DPixelShader*    pixelShader;
1359 };
1360
1361 /* TODO: Make this dynamic, based on shader limits ? */
1362 #define MAX_REG_ADDR 1
1363 #define MAX_REG_TEMP 32
1364 #define MAX_REG_TEXCRD 8
1365 #define MAX_REG_INPUT 12
1366 #define MAX_REG_OUTPUT 12
1367 #define MAX_ATTRIBS 16
1368 #define MAX_CONST_I 16
1369 #define MAX_CONST_B 16
1370
1371 /* FIXME: This needs to go up to 2048 for
1372  * Shader model 3 according to msdn (and for software shaders) */
1373 #define MAX_LABELS 16
1374
1375 typedef struct semantic {
1376     DWORD usage;
1377     DWORD reg;
1378 } semantic;
1379
1380 typedef struct local_constant {
1381     struct list entry;
1382     unsigned int idx;
1383     DWORD value[4];
1384 } local_constant;
1385
1386 typedef struct shader_reg_maps {
1387
1388     char texcoord[MAX_REG_TEXCRD];          /* pixel < 3.0 */
1389     char temporary[MAX_REG_TEMP];           /* pixel, vertex */
1390     char address[MAX_REG_ADDR];             /* vertex */
1391     char packed_input[MAX_REG_INPUT];       /* pshader >= 3.0 */
1392     char packed_output[MAX_REG_OUTPUT];     /* vertex >= 3.0 */
1393     char attributes[MAX_ATTRIBS];           /* vertex */
1394     char labels[MAX_LABELS];                /* pixel, vertex */
1395
1396     /* Sampler usage tokens 
1397      * Use 0 as default (bit 31 is always 1 on a valid token) */
1398     DWORD samplers[MAX_SAMPLERS];
1399
1400     /* Whether or not a loop is used in this shader */
1401     char loop;
1402
1403     /* Whether or not this shader uses fog */
1404     char fog;
1405
1406 } shader_reg_maps;
1407
1408 #define SHADER_PGMSIZE 65535
1409 typedef struct SHADER_BUFFER {
1410     char* buffer;
1411     unsigned int bsize;
1412     unsigned int lineNo;
1413 } SHADER_BUFFER;
1414
1415 /* Undocumented opcode controls */
1416 #define INST_CONTROLS_SHIFT 16
1417 #define INST_CONTROLS_MASK 0x00ff0000
1418
1419 typedef enum COMPARISON_TYPE {
1420     COMPARISON_GT = 1,
1421     COMPARISON_EQ = 2,
1422     COMPARISON_GE = 3,
1423     COMPARISON_LT = 4,
1424     COMPARISON_NE = 5,
1425     COMPARISON_LE = 6
1426 } COMPARISON_TYPE;
1427
1428 typedef struct SHADER_OPCODE {
1429     unsigned int  opcode;
1430     const char*   name;
1431     const char*   glname;
1432     char          dst_token;
1433     CONST UINT    num_params;
1434     shader_fct_t  soft_fct;
1435     SHADER_HANDLER hw_fct;
1436     SHADER_HANDLER hw_glsl_fct;
1437     DWORD         min_version;
1438     DWORD         max_version;
1439 } SHADER_OPCODE;
1440
1441 typedef struct SHADER_OPCODE_ARG {
1442     IWineD3DBaseShader* shader;
1443     shader_reg_maps* reg_maps;
1444     CONST SHADER_OPCODE* opcode;
1445     DWORD opcode_token;
1446     DWORD dst;
1447     DWORD dst_addr;
1448     DWORD predicate;
1449     DWORD src[4];
1450     DWORD src_addr[4];
1451     SHADER_BUFFER* buffer;
1452 } SHADER_OPCODE_ARG;
1453
1454 typedef struct SHADER_LIMITS {
1455     unsigned int temporary;
1456     unsigned int texcoord;
1457     unsigned int sampler;
1458     unsigned int constant_int;
1459     unsigned int constant_float;
1460     unsigned int constant_bool;
1461     unsigned int address;
1462     unsigned int packed_output;
1463     unsigned int packed_input;
1464     unsigned int attributes;
1465     unsigned int label;
1466 } SHADER_LIMITS;
1467
1468 /** Keeps track of details for TEX_M#x# shader opcodes which need to 
1469     maintain state information between multiple codes */
1470 typedef struct SHADER_PARSE_STATE {
1471     unsigned int current_row;
1472     DWORD texcoord_w[2];
1473 } SHADER_PARSE_STATE;
1474
1475 /* Base Shader utility functions. 
1476  * (may move callers into the same file in the future) */
1477 extern int shader_addline(
1478     SHADER_BUFFER* buffer,
1479     const char* fmt, ...);
1480
1481 extern const SHADER_OPCODE* shader_get_opcode(
1482     IWineD3DBaseShader *iface, 
1483     const DWORD code);
1484
1485 extern void shader_delete_constant_list(
1486     struct list* clist);
1487
1488 /* Vertex shader utility functions */
1489 extern BOOL vshader_get_input(
1490     IWineD3DVertexShader* iface,
1491     BYTE usage_req, BYTE usage_idx_req,
1492     unsigned int* regnum);
1493
1494 extern BOOL vshader_input_is_color(
1495     IWineD3DVertexShader* iface,
1496     unsigned int regnum);
1497
1498 extern HRESULT allocate_shader_constants(IWineD3DStateBlockImpl* object);
1499
1500 /* ARB_[vertex/fragment]_program helper functions */
1501 extern void shader_arb_load_constants(
1502     IWineD3DDevice* device,
1503     char usePixelShader,
1504     char useVertexShader);
1505
1506 /* ARB shader program Prototypes */
1507 extern void shader_hw_def(SHADER_OPCODE_ARG *arg);
1508
1509 /* ARB pixel shader prototypes */
1510 extern void pshader_hw_cnd(SHADER_OPCODE_ARG* arg);
1511 extern void pshader_hw_cmp(SHADER_OPCODE_ARG* arg);
1512 extern void pshader_hw_map2gl(SHADER_OPCODE_ARG* arg);
1513 extern void pshader_hw_tex(SHADER_OPCODE_ARG* arg);
1514 extern void pshader_hw_texcoord(SHADER_OPCODE_ARG* arg);
1515 extern void pshader_hw_texreg2ar(SHADER_OPCODE_ARG* arg);
1516 extern void pshader_hw_texreg2gb(SHADER_OPCODE_ARG* arg);
1517 extern void pshader_hw_texbem(SHADER_OPCODE_ARG* arg);
1518 extern void pshader_hw_texm3x2pad(SHADER_OPCODE_ARG* arg);
1519 extern void pshader_hw_texm3x2tex(SHADER_OPCODE_ARG* arg);
1520 extern void pshader_hw_texm3x3pad(SHADER_OPCODE_ARG* arg);
1521 extern void pshader_hw_texm3x3tex(SHADER_OPCODE_ARG* arg);
1522 extern void pshader_hw_texm3x3spec(SHADER_OPCODE_ARG* arg);
1523 extern void pshader_hw_texm3x3vspec(SHADER_OPCODE_ARG* arg);
1524
1525 /* ARB vertex shader prototypes */
1526 extern void vshader_hw_map2gl(SHADER_OPCODE_ARG* arg);
1527 extern void vshader_hw_mnxn(SHADER_OPCODE_ARG* arg);
1528
1529 /* GLSL helper functions */
1530 extern void set_glsl_shader_program(IWineD3DDevice *iface);
1531 extern void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG *arg);
1532 extern void shader_glsl_load_constants(
1533     IWineD3DDevice* device,
1534     char usePixelShader,
1535     char useVertexShader);
1536
1537 /** The following translate DirectX pixel/vertex shader opcodes to GLSL lines */
1538 extern void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg);
1539 extern void shader_glsl_arith(SHADER_OPCODE_ARG* arg);
1540 extern void shader_glsl_mov(SHADER_OPCODE_ARG* arg);
1541 extern void shader_glsl_mad(SHADER_OPCODE_ARG* arg);
1542 extern void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg);
1543 extern void shader_glsl_lrp(SHADER_OPCODE_ARG* arg);
1544 extern void shader_glsl_dot(SHADER_OPCODE_ARG* arg);
1545 extern void shader_glsl_rcp(SHADER_OPCODE_ARG* arg);
1546 extern void shader_glsl_cnd(SHADER_OPCODE_ARG* arg);
1547 extern void shader_glsl_compare(SHADER_OPCODE_ARG* arg);
1548 extern void shader_glsl_def(SHADER_OPCODE_ARG* arg);
1549 extern void shader_glsl_defi(SHADER_OPCODE_ARG* arg);
1550 extern void shader_glsl_defb(SHADER_OPCODE_ARG* arg);
1551 extern void shader_glsl_expp(SHADER_OPCODE_ARG* arg);
1552 extern void shader_glsl_cmp(SHADER_OPCODE_ARG* arg);
1553 extern void shader_glsl_lit(SHADER_OPCODE_ARG* arg);
1554 extern void shader_glsl_dst(SHADER_OPCODE_ARG* arg);
1555 extern void shader_glsl_sincos(SHADER_OPCODE_ARG* arg);
1556 extern void shader_glsl_loop(SHADER_OPCODE_ARG* arg);
1557 extern void shader_glsl_end(SHADER_OPCODE_ARG* arg);
1558 extern void shader_glsl_if(SHADER_OPCODE_ARG* arg);
1559 extern void shader_glsl_ifc(SHADER_OPCODE_ARG* arg);
1560 extern void shader_glsl_else(SHADER_OPCODE_ARG* arg);
1561 extern void shader_glsl_break(SHADER_OPCODE_ARG* arg);
1562 extern void shader_glsl_breakc(SHADER_OPCODE_ARG* arg);
1563 extern void shader_glsl_rep(SHADER_OPCODE_ARG* arg);
1564 extern void shader_glsl_call(SHADER_OPCODE_ARG* arg);
1565 extern void shader_glsl_callnz(SHADER_OPCODE_ARG* arg);
1566 extern void shader_glsl_label(SHADER_OPCODE_ARG* arg);
1567
1568 /** GLSL Pixel Shader Prototypes */
1569 extern void pshader_glsl_tex(SHADER_OPCODE_ARG* arg);
1570 extern void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg);
1571 extern void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg);
1572 extern void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg);
1573 extern void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg);
1574 extern void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg);
1575 extern void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg);
1576 extern void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg);
1577 extern void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg);
1578 extern void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg);
1579 extern void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg);
1580 extern void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg);
1581 extern void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg);
1582 extern void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg);
1583 extern void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg);
1584 extern void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg);
1585 extern void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg);
1586 extern void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg);
1587 extern void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg);
1588 extern void pshader_glsl_input_pack(
1589    SHADER_BUFFER* buffer,
1590    semantic* semantics_out);
1591
1592 /** GLSL Vertex Shader Prototypes */
1593 extern void vshader_glsl_output_unpack(
1594    SHADER_BUFFER* buffer,
1595    semantic* semantics_out);
1596
1597 /*****************************************************************************
1598  * IDirect3DBaseShader implementation structure
1599  */
1600 typedef struct IWineD3DBaseShaderClass
1601 {
1602     DWORD                           hex_version;
1603     SHADER_LIMITS                   limits;
1604     SHADER_PARSE_STATE              parse_state;
1605     CONST SHADER_OPCODE             *shader_ins;
1606     CONST DWORD                     *function;
1607     UINT                            functionLength;
1608     GLuint                          prgId;
1609     BOOL                            is_compiled;
1610
1611     /* Type of shader backend */
1612     int shader_mode;
1613
1614     /* Immediate constants (override global ones) */
1615     struct list constantsB;
1616     struct list constantsF;
1617     struct list constantsI;
1618     shader_reg_maps reg_maps;
1619
1620     /* Pointer to the parent device */
1621     IWineD3DDevice *device;
1622
1623 } IWineD3DBaseShaderClass;
1624
1625 typedef struct IWineD3DBaseShaderImpl {
1626     /* IUnknown */
1627     const IWineD3DBaseShaderVtbl    *lpVtbl;
1628     LONG                            ref;
1629
1630     /* IWineD3DBaseShader */
1631     IWineD3DBaseShaderClass         baseShader;
1632 } IWineD3DBaseShaderImpl;
1633
1634 extern HRESULT shader_get_registers_used(
1635     IWineD3DBaseShader *iface,
1636     shader_reg_maps* reg_maps,
1637     semantic* semantics_in,
1638     semantic* semantics_out,
1639     CONST DWORD* pToken,
1640     IWineD3DStateBlockImpl *stateBlock);
1641
1642 extern void shader_generate_glsl_declarations(
1643     IWineD3DBaseShader *iface,
1644     shader_reg_maps* reg_maps,
1645     SHADER_BUFFER* buffer,
1646     WineD3D_GL_Info* gl_info);
1647
1648 extern void shader_generate_arb_declarations(
1649     IWineD3DBaseShader *iface,
1650     shader_reg_maps* reg_maps,
1651     SHADER_BUFFER* buffer,
1652     WineD3D_GL_Info* gl_info);
1653
1654 extern void shader_generate_main(
1655     IWineD3DBaseShader *iface,
1656     SHADER_BUFFER* buffer,
1657     shader_reg_maps* reg_maps,
1658     CONST DWORD* pFunction);
1659
1660 extern void shader_dump_ins_modifiers(
1661     const DWORD output);
1662
1663 extern void shader_dump_param(
1664     IWineD3DBaseShader *iface,
1665     const DWORD param,
1666     const DWORD addr_token,
1667     int input);
1668
1669 extern void shader_trace_init(
1670     IWineD3DBaseShader *iface,
1671     const DWORD* pFunction);
1672
1673 extern int shader_get_param(
1674     IWineD3DBaseShader* iface,
1675     const DWORD* pToken,
1676     DWORD* param,
1677     DWORD* addr_token);
1678
1679 extern int shader_skip_unrecognized(
1680     IWineD3DBaseShader* iface,
1681     const DWORD* pToken);
1682
1683 extern void print_glsl_info_log(
1684     WineD3D_GL_Info *gl_info,
1685     GLhandleARB obj);
1686
1687 inline static int shader_get_regtype(const DWORD param) {
1688     return (((param & WINED3DSP_REGTYPE_MASK) >> WINED3DSP_REGTYPE_SHIFT) |
1689             ((param & WINED3DSP_REGTYPE_MASK2) >> WINED3DSP_REGTYPE_SHIFT2));
1690 }
1691
1692 extern unsigned int shader_get_float_offset(const DWORD reg);
1693
1694 inline static BOOL shader_is_pshader_version(DWORD token) {
1695     return 0xFFFF0000 == (token & 0xFFFF0000);
1696 }
1697
1698 inline static BOOL shader_is_vshader_version(DWORD token) {
1699     return 0xFFFE0000 == (token & 0xFFFF0000);
1700 }
1701
1702 inline static BOOL shader_is_comment(DWORD token) {
1703     return WINED3DSIO_COMMENT == (token & WINED3DSI_OPCODE_MASK);
1704 }
1705
1706 /* Internally used shader constants. Applications can use constants 0 to GL_LIMITS(vshader_constantsF) - 1,
1707  * so upload them above that
1708  */
1709 #define ARB_SHADER_PRIVCONST_BASE GL_LIMITS(vshader_constantsF)
1710 #define ARB_SHADER_PRIVCONST_POS ARB_SHADER_PRIVCONST_BASE + 0
1711
1712 /*****************************************************************************
1713  * IDirect3DVertexShader implementation structure
1714  */
1715 typedef struct IWineD3DVertexShaderImpl {
1716     /* IUnknown parts*/   
1717     const IWineD3DVertexShaderVtbl *lpVtbl;
1718     LONG                        ref;     /* Note: Ref counting not required */
1719
1720     /* IWineD3DBaseShader */
1721     IWineD3DBaseShaderClass     baseShader;
1722
1723     /* IWineD3DVertexShaderImpl */
1724     IUnknown                    *parent;
1725
1726     char                        usesFog;
1727     DWORD                       usage;
1728
1729     /* Vertex shader input and output semantics */
1730     semantic semantics_in [MAX_ATTRIBS];
1731     semantic semantics_out [MAX_REG_OUTPUT];
1732
1733     /* run time datas...  */
1734     VSHADERDATA                *data;
1735     IWineD3DVertexDeclaration  *vertexDeclaration;
1736 #if 0 /* needs reworking */
1737     /* run time datas */
1738     VSHADERINPUTDATA input;
1739     VSHADEROUTPUTDATA output;
1740 #endif
1741 } IWineD3DVertexShaderImpl;
1742 extern const SHADER_OPCODE IWineD3DVertexShaderImpl_shader_ins[];
1743 extern const IWineD3DVertexShaderVtbl IWineD3DVertexShader_Vtbl;
1744
1745 /*****************************************************************************
1746  * IDirect3DPixelShader implementation structure
1747  */
1748 typedef struct IWineD3DPixelShaderImpl {
1749     /* IUnknown parts */
1750     const IWineD3DPixelShaderVtbl *lpVtbl;
1751     LONG                        ref;     /* Note: Ref counting not required */
1752
1753     /* IWineD3DBaseShader */
1754     IWineD3DBaseShaderClass     baseShader;
1755
1756     /* IWineD3DPixelShaderImpl */
1757     IUnknown                   *parent;
1758
1759     /* Pixel shader input semantics */
1760     semantic semantics_in [MAX_REG_INPUT];
1761
1762     /* run time data */
1763     PSHADERDATA                *data;
1764
1765 #if 0 /* needs reworking */
1766     PSHADERINPUTDATA input;
1767     PSHADEROUTPUTDATA output;
1768 #endif
1769 } IWineD3DPixelShaderImpl;
1770
1771 extern const SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins[];
1772 extern const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl;
1773
1774 /*****************************************************************************
1775  * IWineD3DPalette implementation structure
1776  */
1777 struct IWineD3DPaletteImpl {
1778     /* IUnknown parts */
1779     const IWineD3DPaletteVtbl  *lpVtbl;
1780     LONG                       ref;
1781
1782     IUnknown                   *parent;
1783     IWineD3DDeviceImpl         *wineD3DDevice;
1784
1785     /* IWineD3DPalette */
1786     HPALETTE                   hpal;
1787     WORD                       palVersion;     /*|               */
1788     WORD                       palNumEntries;  /*|  LOGPALETTE   */
1789     PALETTEENTRY               palents[256];   /*|               */
1790     /* This is to store the palette in 'screen format' */
1791     int                        screen_palents[256];
1792     DWORD                      Flags;
1793 };
1794
1795 extern const IWineD3DPaletteVtbl IWineD3DPalette_Vtbl;
1796 DWORD IWineD3DPaletteImpl_Size(DWORD dwFlags);
1797
1798 /* DirectDraw utility functions */
1799 extern WINED3DFORMAT pixelformat_for_depth(DWORD depth);
1800
1801 /*****************************************************************************
1802  * Pixel format management
1803  */
1804 typedef struct {
1805     WINED3DFORMAT           format;
1806     DWORD                   alphaMask, redMask, greenMask, blueMask;
1807     UINT                    bpp;
1808     BOOL                    isFourcc;
1809     GLint                   glInternal, glFormat, glType;
1810 } PixelFormatDesc;
1811
1812 const PixelFormatDesc *getFormatDescEntry(WINED3DFORMAT fmt);
1813 #endif