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