wined3d: Remove unused field: Surface.textureName.
[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 "ddraw.h"
43 #include "wine/wined3d_interface.h"
44 #include "wine/wined3d_gl.h"
45 #include "wine/list.h"
46
47 /* Device caps */
48 #define MAX_PALETTES      256
49 #define MAX_STREAMS       16
50 #define MAX_TEXTURES      8
51 #define MAX_SAMPLERS      16
52 #define MAX_ACTIVE_LIGHTS 8
53 #define MAX_CLIPPLANES    D3DMAXUSERCLIPPLANES
54 #define MAX_LEVELS        256
55
56 #define MAX_CONST_I 16
57 #define MAX_CONST_B 16
58
59 /* Used for CreateStateBlock */
60 #define NUM_SAVEDPIXELSTATES_R     35
61 #define NUM_SAVEDPIXELSTATES_T     18
62 #define NUM_SAVEDPIXELSTATES_S     12
63 #define NUM_SAVEDVERTEXSTATES_R    31
64 #define NUM_SAVEDVERTEXSTATES_T    2
65 #define NUM_SAVEDVERTEXSTATES_S    1
66
67 extern const DWORD SavedPixelStates_R[NUM_SAVEDPIXELSTATES_R];
68 extern const DWORD SavedPixelStates_T[NUM_SAVEDPIXELSTATES_T];
69 extern const DWORD SavedPixelStates_S[NUM_SAVEDPIXELSTATES_S];
70 extern const DWORD SavedVertexStates_R[NUM_SAVEDVERTEXSTATES_R];
71 extern const DWORD SavedVertexStates_T[NUM_SAVEDVERTEXSTATES_T];
72 extern const DWORD SavedVertexStates_S[NUM_SAVEDVERTEXSTATES_S];
73
74 typedef enum _WINELOOKUP {
75     WINELOOKUP_WARPPARAM = 0,
76     WINELOOKUP_MAGFILTER = 1,
77     MAX_LOOKUPS          = 2
78 } WINELOOKUP;
79
80 extern int minLookup[MAX_LOOKUPS];
81 extern int maxLookup[MAX_LOOKUPS];
82 extern DWORD *stateLookup[MAX_LOOKUPS];
83
84 extern DWORD minMipLookup[D3DTEXF_ANISOTROPIC + 1][D3DTEXF_LINEAR + 1];
85
86 typedef struct _WINED3DGLTYPE {
87     int         d3dType;
88     GLint       size;
89     GLenum      glType;
90     GLboolean   normalized;
91     int         typesize;
92 } WINED3DGLTYPE;
93
94 /* NOTE: Make sure these are in the correct numerical order. (see /include/d3d9types.h typedef enum _D3DDECLTYPE) */
95 static WINED3DGLTYPE const glTypeLookup[D3DDECLTYPE_UNUSED] = {
96                                   {D3DDECLTYPE_FLOAT1,    1, GL_FLOAT           , GL_FALSE ,sizeof(float)},
97                                   {D3DDECLTYPE_FLOAT2,    2, GL_FLOAT           , GL_FALSE ,sizeof(float)},
98                                   {D3DDECLTYPE_FLOAT3,    3, GL_FLOAT           , GL_FALSE ,sizeof(float)},
99                                   {D3DDECLTYPE_FLOAT4,    4, GL_FLOAT           , GL_FALSE ,sizeof(float)},
100                                   {D3DDECLTYPE_D3DCOLOR,  4, GL_UNSIGNED_BYTE   , GL_TRUE  ,sizeof(BYTE)},
101                                   {D3DDECLTYPE_UBYTE4,    4, GL_UNSIGNED_BYTE   , GL_FALSE ,sizeof(BYTE)},
102                                   {D3DDECLTYPE_SHORT2,    2, GL_SHORT           , GL_FALSE ,sizeof(short int)},
103                                   {D3DDECLTYPE_SHORT4,    4, GL_SHORT           , GL_FALSE ,sizeof(short int)},
104                                   {D3DDECLTYPE_UBYTE4N,   4, GL_UNSIGNED_BYTE   , GL_FALSE ,sizeof(BYTE)},
105                                   {D3DDECLTYPE_SHORT2N,   2, GL_SHORT           , GL_FALSE ,sizeof(short int)},
106                                   {D3DDECLTYPE_SHORT4N,   4, GL_SHORT           , GL_FALSE ,sizeof(short int)},
107                                   {D3DDECLTYPE_USHORT2N,  2, GL_UNSIGNED_SHORT  , GL_FALSE ,sizeof(short int)},
108                                   {D3DDECLTYPE_USHORT4N,  4, GL_UNSIGNED_SHORT  , GL_FALSE ,sizeof(short int)},
109                                   {D3DDECLTYPE_UDEC3,     3, GL_UNSIGNED_SHORT  , GL_FALSE ,sizeof(short int)},
110                                   {D3DDECLTYPE_DEC3N,     3, GL_SHORT           , GL_FALSE ,sizeof(short int)},
111                                   {D3DDECLTYPE_FLOAT16_2, 2, GL_FLOAT           , GL_FALSE ,sizeof(short int)},
112                                   {D3DDECLTYPE_FLOAT16_4, 4, GL_FLOAT           , GL_FALSE ,sizeof(short int)}};
113
114 #define WINED3D_ATR_TYPE(type)          glTypeLookup[type].d3dType
115 #define WINED3D_ATR_SIZE(type)          glTypeLookup[type].size
116 #define WINED3D_ATR_GLTYPE(type)        glTypeLookup[type].glType
117 #define WINED3D_ATR_NORMALIZED(type)    glTypeLookup[type].normalized
118 #define WINED3D_ATR_TYPESIZE(type)      glTypeLookup[type].typesize
119
120 /**
121  * Settings 
122  */
123 #define VS_NONE    0
124 #define VS_HW      1
125 #define VS_SW      2
126
127 #define PS_NONE    0
128 #define PS_HW      1
129
130 #define VBO_NONE   0
131 #define VBO_HW     1
132
133 #define NP2_NONE   0
134 #define NP2_REPACK 1
135
136 #define SHADER_SW   0
137 #define SHADER_ARB  1
138 #define SHADER_GLSL 2
139 #define SHADER_NONE 3
140
141 #define RTL_DISABLE   -1
142 #define RTL_AUTO       0
143 #define RTL_READDRAW   1
144 #define RTL_READTEX    2
145 #define RTL_TEXDRAW    3
146 #define RTL_TEXTEX     4
147
148 typedef struct wined3d_settings_s {
149 /* vertex and pixel shader modes */
150   int vs_mode;
151   int ps_mode;
152   int vbo_mode;
153 /* Ideally, we don't want the user to have to request GLSL.  If the hardware supports GLSL,
154     we should use it.  However, until it's fully implemented, we'll leave it as a registry
155     setting for developers. */
156   BOOL glslRequested;
157   int vs_selected_mode;
158   int ps_selected_mode;
159 /* nonpower 2 function */
160   int nonpower2_mode;
161   int rendertargetlock_mode;
162 /* Memory tracking and object counting */
163   unsigned int emulated_textureram;
164 } wined3d_settings_t;
165
166 extern wined3d_settings_t wined3d_settings;
167
168 /* X11 locking */
169
170 extern void (*wine_tsx11_lock_ptr)(void);
171 extern void (*wine_tsx11_unlock_ptr)(void);
172
173 /* As GLX relies on X, this is needed */
174 extern int num_lock;
175
176 #if 0
177 #define ENTER_GL() ++num_lock; if (num_lock > 1) FIXME("Recursive use of GL lock to: %d\n", num_lock); wine_tsx11_lock_ptr()
178 #define LEAVE_GL() if (num_lock != 1) FIXME("Recursive use of GL lock: %d\n", num_lock); --num_lock; wine_tsx11_unlock_ptr()
179 #else
180 #define ENTER_GL() wine_tsx11_lock_ptr()
181 #define LEAVE_GL() wine_tsx11_unlock_ptr()
182 #endif
183
184 /*****************************************************************************
185  * Defines
186  */
187
188 /* GL related defines */
189 /* ------------------ */
190 #define GL_SUPPORT(ExtName)           (GLINFO_LOCATION.supported[ExtName] != 0)
191 #define GL_LIMITS(ExtName)            (GLINFO_LOCATION.max_##ExtName)
192 #define GL_EXTCALL(FuncName)          (GLINFO_LOCATION.FuncName)
193 #define GL_VEND(_VendName)            (GLINFO_LOCATION.gl_vendor == VENDOR_##_VendName ? TRUE : FALSE)
194
195 #define D3DCOLOR_B_R(dw) (((dw) >> 16) & 0xFF)
196 #define D3DCOLOR_B_G(dw) (((dw) >>  8) & 0xFF)
197 #define D3DCOLOR_B_B(dw) (((dw) >>  0) & 0xFF)
198 #define D3DCOLOR_B_A(dw) (((dw) >> 24) & 0xFF)
199
200 #define D3DCOLOR_R(dw) (((float) (((dw) >> 16) & 0xFF)) / 255.0f)
201 #define D3DCOLOR_G(dw) (((float) (((dw) >>  8) & 0xFF)) / 255.0f)
202 #define D3DCOLOR_B(dw) (((float) (((dw) >>  0) & 0xFF)) / 255.0f)
203 #define D3DCOLOR_A(dw) (((float) (((dw) >> 24) & 0xFF)) / 255.0f)
204
205 #define D3DCOLORTOGLFLOAT4(dw, vec) \
206   (vec)[0] = D3DCOLOR_R(dw); \
207   (vec)[1] = D3DCOLOR_G(dw); \
208   (vec)[2] = D3DCOLOR_B(dw); \
209   (vec)[3] = D3DCOLOR_A(dw);
210
211 #define GLTEXTURECUBEMAP GL_TEXTURE_CUBE_MAP_ARB
212
213 /* DirectX Device Limits */
214 /* --------------------- */
215 #define MAX_LEVELS  256  /* Maximum number of mipmap levels. Guessed at 256 */
216
217 #define MAX_STREAMS  16  /* Maximum possible streams - used for fixed size arrays
218                             See MaxStreams in MSDN under GetDeviceCaps */
219                          /* Maximum number of constants provided to the shaders */
220 #define HIGHEST_TRANSFORMSTATE 512 
221                          /* Highest value in D3DTRANSFORMSTATETYPE */
222 #define MAX_CLIPPLANES  D3DMAXUSERCLIPPLANES
223
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:%ld, type:%ld)\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 if exists d3d trace
329      is enabled, and if it doesn't exists 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     /* Optimization */
500     BOOL                    modelview_valid;
501     BOOL                    proj_valid;
502     BOOL                    view_ident;        /* true iff view matrix is identity                */
503     BOOL                    last_was_rhw;      /* true iff last draw_primitive was in xyzrhw mode */
504     BOOL                    viewport_changed;  /* Was the viewport changed since the last draw?   */
505     GLenum                  tracking_parm;     /* Which source is tracking current colour         */
506     LONG                    tracking_color;    /* used iff GL_COLOR_MATERIAL was enabled          */
507 #define                         DISABLED_TRACKING  0  /* Disabled                                 */
508 #define                         IS_TRACKING        1  /* tracking_parm is tracking diffuse color  */
509 #define                         NEEDS_TRACKING     2  /* Tracking needs to be enabled when needed */
510 #define                         NEEDS_DISABLE      3  /* Tracking needs to be disabled when needed*/
511     UINT                    srcBlend;
512     UINT                    dstBlend;
513     UINT                    alphafunc;
514     BOOL                    texture_shader_active;  /* TODO: Confirm use is correct */
515     BOOL                    last_was_notclipped;
516     BOOL                    untransformed;
517
518     /* State block related */
519     BOOL                    isRecordingState;
520     IWineD3DStateBlockImpl *stateBlock;
521     IWineD3DStateBlockImpl *updateStateBlock;
522
523     /* Internal use fields  */
524     WINED3DDEVICE_CREATION_PARAMETERS createParms;
525     UINT                            adapterNo;
526     D3DDEVTYPE                      devType;
527
528     IWineD3DSwapChain     **swapchains;
529     uint                    NumberOfSwapChains;
530
531     ResourceList           *resources; /* a linked list to track resources created by the device */
532
533     /* Render Target Support */
534     IWineD3DSurface        *depthStencilBuffer;
535
536     IWineD3DSurface        *renderTarget;
537     IWineD3DSurface        *stencilBufferTarget;
538
539     /* palettes texture management */
540     PALETTEENTRY            palettes[MAX_PALETTES][256];
541     UINT                    currentPalette;
542
543     /* For rendering to a texture using glCopyTexImage */
544     BOOL                    renderUpsideDown;
545
546     /* Cursor management */
547     BOOL                    bCursorVisible;
548     UINT                    xHotSpot;
549     UINT                    yHotSpot;
550     UINT                    xScreenSpace;
551     UINT                    yScreenSpace;
552     UINT                    cursorWidth, cursorHeight;
553     GLuint                  cursorTexture;
554
555     /* Textures for when no other textures are mapped */
556     UINT                          dummyTextureName[MAX_TEXTURES];
557
558     /* Debug stream management */
559     BOOL                     debug;
560
561     /* Device state management */
562     HRESULT                 state;
563     BOOL                    d3d_initialized;
564
565     /* Screen buffer resources */
566     glContext contextCache[CONTEXT_CACHE];
567
568     /* A flag to check if endscene has been called before changing the render tartet */
569     BOOL sceneEnded;
570
571     /* process vertex shaders using software or hardware */
572     BOOL softwareVertexProcessing;
573
574     /* DirectDraw stuff */
575     HWND ddraw_window;
576     IWineD3DSurface *ddraw_primary;
577     DWORD ddraw_width, ddraw_height;
578     WINED3DFORMAT ddraw_format;
579
580     /* List of GLSL shader programs and their associated vertex & pixel shaders */
581     struct list glsl_shader_progs;
582
583 } IWineD3DDeviceImpl;
584
585 extern const IWineD3DDeviceVtbl IWineD3DDevice_Vtbl;
586
587 /* Support for IWineD3DResource ::Set/Get/FreePrivateData. I don't think
588  * anybody uses it for much so a good implementation is optional. */
589 typedef struct PrivateData
590 {
591     struct PrivateData* next;
592
593     GUID tag;
594     DWORD flags; /* DDSPD_* */
595     DWORD uniqueness_value;
596
597     union
598     {
599         LPVOID data;
600         LPUNKNOWN object;
601     } ptr;
602
603     DWORD size;
604 } PrivateData;
605
606 void d3ddevice_set_ortho(IWineD3DDeviceImpl *This);
607
608 /*****************************************************************************
609  * IWineD3DResource implementation structure
610  */
611 typedef struct IWineD3DResourceClass
612 {
613     /* IUnknown fields */
614     LONG                    ref;     /* Note: Ref counting not required */
615
616     /* WineD3DResource Information */
617     IUnknown               *parent;
618     WINED3DRESOURCETYPE     resourceType;
619     IWineD3DDeviceImpl     *wineD3DDevice;
620     WINED3DPOOL             pool;
621     UINT                    size;
622     DWORD                   usage;
623     WINED3DFORMAT           format;
624     BYTE                   *allocatedMemory;
625     PrivateData            *privateData;
626
627 } IWineD3DResourceClass;
628
629 typedef struct IWineD3DResourceImpl
630 {
631     /* IUnknown & WineD3DResource Information     */
632     const IWineD3DResourceVtbl *lpVtbl;
633     IWineD3DResourceClass   resource;
634 } IWineD3DResourceImpl;
635
636
637 /*****************************************************************************
638  * IWineD3DVertexBuffer implementation structure (extends IWineD3DResourceImpl)
639  */
640 typedef struct IWineD3DVertexBufferImpl
641 {
642     /* IUnknown & WineD3DResource Information     */
643     const IWineD3DVertexBufferVtbl *lpVtbl;
644     IWineD3DResourceClass     resource;
645
646     /* WineD3DVertexBuffer specifics */
647     DWORD                     fvf;
648
649     /* Vertex buffer object support */
650     GLuint                    vbo;
651     BYTE                      Flags;
652     UINT                      stream;
653
654     UINT                      dirtystart, dirtyend;
655     LONG                      lockcount;
656
657     /* Last description of the buffer */
658     WineDirect3DVertexStridedData strided;
659 } IWineD3DVertexBufferImpl;
660
661 extern const IWineD3DVertexBufferVtbl IWineD3DVertexBuffer_Vtbl;
662
663 #define VBFLAG_LOAD           0x01    /* Data is written from allocatedMemory to the VBO */
664 #define VBFLAG_OPTIMIZED      0x02    /* Optimize has been called for the VB */
665 #define VBFLAG_DIRTY          0x04    /* Buffer data has been modified */
666 #define VBFLAG_STREAM         0x08    /* The vertex buffer is in a stream */
667 #define VBFLAG_HASDESC        0x10    /* A vertex description has been found */
668 #define VBFLAG_VBOCREATEFAIL  0x20    /* An attempt to create a vbo has failed */
669
670 /*****************************************************************************
671  * IWineD3DIndexBuffer implementation structure (extends IWineD3DResourceImpl)
672  */
673 typedef struct IWineD3DIndexBufferImpl
674 {
675     /* IUnknown & WineD3DResource Information     */
676     const IWineD3DIndexBufferVtbl *lpVtbl;
677     IWineD3DResourceClass     resource;
678
679     /* WineD3DVertexBuffer specifics */
680 } IWineD3DIndexBufferImpl;
681
682 extern const IWineD3DIndexBufferVtbl IWineD3DIndexBuffer_Vtbl;
683
684 /*****************************************************************************
685  * IWineD3DBaseTexture D3D- > openGL state map lookups
686  */
687 #define WINED3DFUNC_NOTSUPPORTED  -2
688 #define WINED3DFUNC_UNIMPLEMENTED -1
689
690 typedef enum winetexturestates {
691     WINED3DTEXSTA_ADDRESSU       = 0,
692     WINED3DTEXSTA_ADDRESSV       = 1,
693     WINED3DTEXSTA_ADDRESSW       = 2,
694     WINED3DTEXSTA_BORDERCOLOR    = 3,
695     WINED3DTEXSTA_MAGFILTER      = 4,
696     WINED3DTEXSTA_MINFILTER      = 5,
697     WINED3DTEXSTA_MIPFILTER      = 6,
698     WINED3DTEXSTA_MAXMIPLEVEL    = 7,
699     WINED3DTEXSTA_MAXANISOTROPY  = 8,
700     WINED3DTEXSTA_SRGBTEXTURE    = 9,
701     WINED3DTEXSTA_ELEMENTINDEX   = 10,
702     WINED3DTEXSTA_DMAPOFFSET     = 11,
703     WINED3DTEXSTA_TSSADDRESSW    = 12,
704     MAX_WINETEXTURESTATES        = 13,
705 } winetexturestates;
706
707 typedef struct Wined3dTextureStateMap {
708     CONST int state;
709     int function;
710 } Wined3dTextureStateMap;
711
712 /*****************************************************************************
713  * IWineD3DBaseTexture implementation structure (extends IWineD3DResourceImpl)
714  */
715 typedef struct IWineD3DBaseTextureClass
716 {
717     UINT                    levels;
718     BOOL                    dirty;
719     D3DFORMAT               format;
720     DWORD                   usage;
721     UINT                    textureName;
722     UINT                    LOD;
723     WINED3DTEXTUREFILTERTYPE filterType;
724     DWORD                   states[MAX_WINETEXTURESTATES];
725
726 } IWineD3DBaseTextureClass;
727
728 typedef struct IWineD3DBaseTextureImpl
729 {
730     /* IUnknown & WineD3DResource Information     */
731     const IWineD3DBaseTextureVtbl *lpVtbl;
732     IWineD3DResourceClass     resource;
733     IWineD3DBaseTextureClass  baseTexture;
734
735 } IWineD3DBaseTextureImpl;
736
737 /*****************************************************************************
738  * IWineD3DTexture implementation structure (extends IWineD3DBaseTextureImpl)
739  */
740 typedef struct IWineD3DTextureImpl
741 {
742     /* IUnknown & WineD3DResource/WineD3DBaseTexture Information     */
743     const IWineD3DTextureVtbl *lpVtbl;
744     IWineD3DResourceClass     resource;
745     IWineD3DBaseTextureClass  baseTexture;
746
747     /* IWineD3DTexture */
748     IWineD3DSurface          *surfaces[MAX_LEVELS];
749     
750     UINT                      width;
751     UINT                      height;
752     float                     pow2scalingFactorX;
753     float                     pow2scalingFactorY;
754
755 } IWineD3DTextureImpl;
756
757 extern const IWineD3DTextureVtbl IWineD3DTexture_Vtbl;
758
759 /*****************************************************************************
760  * IWineD3DCubeTexture implementation structure (extends IWineD3DBaseTextureImpl)
761  */
762 typedef struct IWineD3DCubeTextureImpl
763 {
764     /* IUnknown & WineD3DResource/WineD3DBaseTexture Information     */
765     const IWineD3DCubeTextureVtbl *lpVtbl;
766     IWineD3DResourceClass     resource;
767     IWineD3DBaseTextureClass  baseTexture;
768
769     /* IWineD3DCubeTexture */
770     IWineD3DSurface          *surfaces[6][MAX_LEVELS];
771
772     UINT                      edgeLength;
773     float                     pow2scalingFactor;
774
775 } IWineD3DCubeTextureImpl;
776
777 extern const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl;
778
779 typedef struct _WINED3DVOLUMET_DESC
780 {
781     UINT                    Width;
782     UINT                    Height;
783     UINT                    Depth;
784 } WINED3DVOLUMET_DESC;
785
786 /*****************************************************************************
787  * IWineD3DVolume implementation structure (extends IUnknown)
788  */
789 typedef struct IWineD3DVolumeImpl
790 {
791     /* IUnknown & WineD3DResource fields */
792     const IWineD3DVolumeVtbl  *lpVtbl;
793     IWineD3DResourceClass      resource;
794
795     /* WineD3DVolume Information */
796     WINED3DVOLUMET_DESC      currentDesc;
797     IWineD3DBase            *container;
798     UINT                    bytesPerPixel;
799
800     BOOL                    lockable;
801     BOOL                    locked;
802     WINED3DBOX              lockedBox;
803     WINED3DBOX              dirtyBox;
804     BOOL                    dirty;
805
806
807 } IWineD3DVolumeImpl;
808
809 extern const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl;
810
811 /*****************************************************************************
812  * IWineD3DVolumeTexture implementation structure (extends IWineD3DBaseTextureImpl)
813  */
814 typedef struct IWineD3DVolumeTextureImpl
815 {
816     /* IUnknown & WineD3DResource/WineD3DBaseTexture Information     */
817     const IWineD3DVolumeTextureVtbl *lpVtbl;
818     IWineD3DResourceClass     resource;
819     IWineD3DBaseTextureClass  baseTexture;
820
821     /* IWineD3DVolumeTexture */
822     IWineD3DVolume           *volumes[MAX_LEVELS];
823
824     UINT                      width;
825     UINT                      height;
826     UINT                      depth;
827 } IWineD3DVolumeTextureImpl;
828
829 extern const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl;
830
831 typedef struct _WINED3DSURFACET_DESC
832 {
833     WINED3DMULTISAMPLE_TYPE MultiSampleType;
834     DWORD                   MultiSampleQuality;
835     UINT                    Width;
836     UINT                    Height;
837 } WINED3DSURFACET_DESC;
838
839 /*****************************************************************************
840  * Structure for DIB Surfaces (GetDC and GDI surfaces)
841  */
842 typedef struct wineD3DSurface_DIB {
843     HBITMAP DIBsection;
844     void* bitmap_data;
845     HGDIOBJ holdbitmap;
846     BOOL client_memory;
847 } wineD3DSurface_DIB;
848
849 /*****************************************************************************
850  * IWineD3DSurface implementation structure
851  */
852 struct IWineD3DSurfaceImpl
853 {
854     /* IUnknown & IWineD3DResource Information     */
855     const IWineD3DSurfaceVtbl *lpVtbl;
856     IWineD3DResourceClass     resource;
857
858     /* IWineD3DSurface fields */
859     IWineD3DBase              *container;
860     WINED3DSURFACET_DESC      currentDesc;
861     IWineD3DPaletteImpl      *palette;
862
863     UINT                      bytesPerPixel;
864
865     /* TODO: move this off into a management class(maybe!) */
866     DWORD                      Flags;
867
868     UINT                      pow2Width;
869     UINT                      pow2Height;
870     UINT                      pow2Size;
871
872     /* Oversized texture */
873     RECT                      glRect;
874
875 #if 0
876     /* precalculated x and y scalings for texture coords */
877     float                     pow2scalingFactorX; /* =  (Width  / pow2Width ) */
878     float                     pow2scalingFactorY; /* =  (Height / pow2Height) */
879 #endif
880
881     RECT                      lockedRect;
882     RECT                      dirtyRect;
883
884     glDescriptor              glDescription;
885
886     /* For GetDC */
887     wineD3DSurface_DIB        dib;
888     HDC                       hDC;
889
890     /* Color keys for DDraw */
891     DDCOLORKEY                DestBltCKey;
892     DDCOLORKEY                DestOverlayCKey;
893     DDCOLORKEY                SrcOverlayCKey;
894     DDCOLORKEY                SrcBltCKey;
895     DWORD                     CKeyFlags;
896
897     DDCOLORKEY                glCKey;
898 };
899
900 extern const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl;
901 extern const IWineD3DSurfaceVtbl IWineGDISurface_Vtbl;
902
903 /* Predeclare the shared Surface functions */
904 HRESULT WINAPI IWineD3DSurfaceImpl_QueryInterface(IWineD3DSurface *iface, REFIID riid, LPVOID *ppobj);
905 ULONG WINAPI IWineD3DSurfaceImpl_AddRef(IWineD3DSurface *iface);
906 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface);
907 HRESULT WINAPI IWineD3DSurfaceImpl_GetParent(IWineD3DSurface *iface, IUnknown **pParent);
908 HRESULT WINAPI IWineD3DSurfaceImpl_GetDevice(IWineD3DSurface *iface, IWineD3DDevice** ppDevice);
909 HRESULT WINAPI IWineD3DSurfaceImpl_SetPrivateData(IWineD3DSurface *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags);
910 HRESULT WINAPI IWineD3DSurfaceImpl_GetPrivateData(IWineD3DSurface *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData);
911 HRESULT WINAPI IWineD3DSurfaceImpl_FreePrivateData(IWineD3DSurface *iface, REFGUID refguid);
912 DWORD   WINAPI IWineD3DSurfaceImpl_SetPriority(IWineD3DSurface *iface, DWORD PriorityNew);
913 DWORD   WINAPI IWineD3DSurfaceImpl_GetPriority(IWineD3DSurface *iface);
914 void    WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface);
915 WINED3DRESOURCETYPE WINAPI IWineD3DSurfaceImpl_GetType(IWineD3DSurface *iface);
916 HRESULT WINAPI IWineD3DSurfaceImpl_GetContainerParent(IWineD3DSurface* iface, IUnknown **ppContainerParent);
917 HRESULT WINAPI IWineD3DSurfaceImpl_GetContainer(IWineD3DSurface* iface, REFIID riid, void** ppContainer);
918 HRESULT WINAPI IWineD3DSurfaceImpl_GetDesc(IWineD3DSurface *iface, WINED3DSURFACE_DESC *pDesc);
919 HRESULT WINAPI IWineD3DSurfaceImpl_GetBltStatus(IWineD3DSurface *iface, DWORD Flags);
920 HRESULT WINAPI IWineD3DSurfaceImpl_GetFlipStatus(IWineD3DSurface *iface, DWORD Flags);
921 HRESULT WINAPI IWineD3DSurfaceImpl_IsLost(IWineD3DSurface *iface);
922 HRESULT WINAPI IWineD3DSurfaceImpl_Restore(IWineD3DSurface *iface);
923 HRESULT WINAPI IWineD3DSurfaceImpl_SetPixelFormat(IWineD3DSurface *iface, WINED3DFORMAT Format, BYTE *Surface, DWORD Size);
924 HRESULT WINAPI IWineD3DSurfaceImpl_GetPalette(IWineD3DSurface *iface, IWineD3DPalette **Pal);
925 HRESULT WINAPI IWineD3DSurfaceImpl_SetPalette(IWineD3DSurface *iface, IWineD3DPalette *Pal);
926 HRESULT WINAPI IWineD3DSurfaceImpl_SetColorKey(IWineD3DSurface *iface, DWORD Flags, DDCOLORKEY *CKey);
927 HRESULT WINAPI IWineD3DSurfaceImpl_CleanDirtyRect(IWineD3DSurface *iface);
928 extern HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect);
929 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container);
930 HRESULT WINAPI IWineD3DSurfaceImpl_SetPBufferState(IWineD3DSurface *iface, BOOL inPBuffer, BOOL  inTexture);
931 void WINAPI IWineD3DSurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target);
932 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription);
933 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface);
934 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format);
935 HRESULT WINAPI IWineGDISurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx);
936 HRESULT WINAPI IWineGDISurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans);
937 HRESULT WINAPI IWineD3DSurfaceImpl_SetPalette(IWineD3DSurface *iface, IWineD3DPalette *Pal);
938 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC);
939 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC);
940 DWORD WINAPI IWineD3DSurfaceImpl_GetPitch(IWineD3DSurface *iface);
941 HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface);
942 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem);
943
944 /* Surface flags: */
945 #define SFLAG_OVERSIZE    0x00000001 /* Surface is bigger than gl size, blts only */
946 #define SFLAG_CONVERTED   0x00000002 /* Converted for color keying or Palettized */
947 #define SFLAG_DIBSECTION  0x00000004 /* Has a DIB section attached for getdc */
948 #define SFLAG_DIRTY       0x00000008 /* Surface was locked by the app */
949 #define SFLAG_LOCKABLE    0x00000010 /* Surface can be locked */
950 #define SFLAG_DISCARD     0x00000020 /* ??? */
951 #define SFLAG_LOCKED      0x00000040 /* Surface is locked atm */
952 #define SFLAG_ACTIVELOCK  0x00000080 /* Not locked, but surface memory is needed */
953 #define SFLAG_INTEXTURE   0x00000100 /* ??? */
954 #define SFLAG_INPBUFFER   0x00000200 /* ??? */
955 #define SFLAG_NONPOW2     0x00000400 /* Surface sizes are not a power of 2 */
956 #define SFLAG_DYNLOCK     0x00000800 /* Surface is often locked by the app */
957 #define SFLAG_DYNCHANGE   0x00001800 /* Surface contents are changed very often, implies DYNLOCK */
958 #define SFLAG_DCINUSE     0x00002000 /* Set between GetDC and ReleaseDC calls */
959 #define SFLAG_GLDIRTY     0x00004000 /* The opengl texture is more up to date than the surface mem */
960 #define SFLAG_LOST        0x00008000 /* Surface lost flag for DDraw */
961 #define SFLAG_FORCELOAD   0x00010000 /* To force PreLoading of a scratch cursor */
962 #define SFLAG_USERPTR     0x00020000 /* The application allocated the memory for this surface */
963 #define SFLAG_GLCKEY      0x00040000 /* The gl texture was created with a color key */
964
965 /* In some conditions the surface memory must not be freed:
966  * SFLAG_OVERSIZE: Not all data can be kept in GL
967  * SFLAG_CONVERTED: Converting the data back would take too long
968  * SFLAG_DIBSECTION: The dib code manages the memory
969  * SFLAG_DIRTY: GL surface isn't up to date
970  * SFLAG_LOCKED: The app requires access to the surface data
971  * SFLAG_ACTIVELOCK: Some wined3d code needs the memory
972  * SFLAG_DYNLOCK: Avoid freeing the data for performance
973  * SFLAG_DYNCHANGE: Same reason as DYNLOCK
974  */
975 #define SFLAG_DONOTFREE  (SFLAG_OVERSIZE   | \
976                           SFLAG_CONVERTED  | \
977                           SFLAG_DIBSECTION | \
978                           SFLAG_DIRTY      | \
979                           SFLAG_LOCKED     | \
980                           SFLAG_ACTIVELOCK | \
981                           SFLAG_DYNLOCK    | \
982                           SFLAG_DYNCHANGE  | \
983                           SFLAG_USERPTR)
984
985 BOOL CalculateTexRect(IWineD3DSurfaceImpl *This, RECT *Rect, float glTexCoord[4]);
986
987 /*****************************************************************************
988  * IWineD3DVertexDeclaration implementation structure
989  */
990 typedef struct IWineD3DVertexDeclarationImpl {
991  /* IUnknown  Information     */
992   const IWineD3DVertexDeclarationVtbl *lpVtbl;
993   LONG                    ref;     /* Note: Ref counting not required */
994
995   IUnknown               *parent;
996   /** precomputed fvf if simple declaration */
997   IWineD3DDeviceImpl     *wineD3DDevice;
998   DWORD   fvf[MAX_STREAMS];
999   DWORD   allFVF;
1000
1001   /** dx8 compatible Declaration fields */
1002   DWORD*  pDeclaration8;
1003   DWORD   declaration8Length;
1004
1005   /** dx9+ */
1006   D3DVERTEXELEMENT9 *pDeclaration9;
1007   UINT               declaration9NumElements;
1008
1009   WINED3DVERTEXELEMENT  *pDeclarationWine;
1010   UINT                   declarationWNumElements;
1011   
1012   float                 *constants;
1013   
1014 } IWineD3DVertexDeclarationImpl;
1015
1016 extern const IWineD3DVertexDeclarationVtbl IWineD3DVertexDeclaration_Vtbl;
1017
1018 /*****************************************************************************
1019  * IWineD3DStateBlock implementation structure
1020  */
1021
1022 /* Internal state Block for Begin/End/Capture/Create/Apply info  */
1023 /*   Note: Very long winded but gl Lists are not flexible enough */
1024 /*   to resolve everything we need, so doing it manually for now */
1025 typedef struct SAVEDSTATES {
1026         BOOL                      indices;
1027         BOOL                      material;
1028         BOOL                      fvf;
1029         BOOL                      streamSource[MAX_STREAMS];
1030         BOOL                      streamFreq[MAX_STREAMS];
1031         BOOL                      textures[MAX_SAMPLERS];
1032         BOOL                      transform[HIGHEST_TRANSFORMSTATE + 1];
1033         BOOL                      viewport;
1034         BOOL                      renderState[WINEHIGHEST_RENDER_STATE + 1];
1035         BOOL                      textureState[MAX_TEXTURES][WINED3D_HIGHEST_TEXTURE_STATE + 1];
1036         BOOL                      samplerState[MAX_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1];
1037         BOOL                      clipplane[MAX_CLIPPLANES];
1038         BOOL                      vertexDecl;
1039         BOOL                      pixelShader;
1040         BOOL                      pixelShaderConstantsB[MAX_CONST_B];
1041         BOOL                      pixelShaderConstantsI[MAX_CONST_I];
1042         BOOL                     *pixelShaderConstantsF;
1043         BOOL                      vertexShader;
1044         BOOL                      vertexShaderConstantsB[MAX_CONST_B];
1045         BOOL                      vertexShaderConstantsI[MAX_CONST_I];
1046         BOOL                     *vertexShaderConstantsF;
1047 } SAVEDSTATES;
1048
1049 typedef struct {
1050     struct list entry;
1051     int idx;
1052 } constant_entry;
1053
1054 struct IWineD3DStateBlockImpl
1055 {
1056     /* IUnknown fields */
1057     const IWineD3DStateBlockVtbl *lpVtbl;
1058     LONG                      ref;     /* Note: Ref counting not required */
1059
1060     /* IWineD3DStateBlock information */
1061     IUnknown                 *parent;
1062     IWineD3DDeviceImpl       *wineD3DDevice;
1063     WINED3DSTATEBLOCKTYPE     blockType;
1064
1065     /* Array indicating whether things have been set or changed */
1066     SAVEDSTATES               changed;
1067     SAVEDSTATES               set;
1068     struct list               set_vconstantsF;
1069     struct list               set_pconstantsF;
1070
1071     /* Drawing - Vertex Shader or FVF related */
1072     DWORD                     fvf;
1073     /* Vertex Shader Declaration */
1074     IWineD3DVertexDeclaration *vertexDecl;
1075
1076     IWineD3DVertexShader      *vertexShader;
1077
1078     /* Vertex Shader Constants */
1079     BOOL                       vertexShaderConstantB[MAX_CONST_B];
1080     INT                        vertexShaderConstantI[MAX_CONST_I * 4];
1081     float                     *vertexShaderConstantF;
1082
1083     /* Stream Source */
1084     BOOL                      streamIsUP;
1085     UINT                      streamStride[MAX_STREAMS];
1086     UINT                      streamOffset[MAX_STREAMS];
1087     IWineD3DVertexBuffer     *streamSource[MAX_STREAMS];
1088     UINT                      streamFreq[MAX_STREAMS];
1089     UINT                      streamFlags[MAX_STREAMS];     /*0 | D3DSTREAMSOURCE_INSTANCEDATA | D3DSTREAMSOURCE_INDEXEDDATA  */
1090
1091     /* Indices */
1092     IWineD3DIndexBuffer*      pIndexData;
1093     UINT                      baseVertexIndex; /* Note: only used for d3d8 */
1094
1095     /* Transform */
1096     D3DMATRIX                 transforms[HIGHEST_TRANSFORMSTATE + 1];
1097
1098     /* Lights */
1099     PLIGHTINFOEL             *lights; /* NOTE: active GL lights must be front of the chain */
1100
1101     /* Clipping */
1102     double                    clipplane[MAX_CLIPPLANES][4];
1103     WINED3DCLIPSTATUS         clip_status;
1104
1105     /* ViewPort */
1106     WINED3DVIEWPORT           viewport;
1107
1108     /* Material */
1109     WINED3DMATERIAL           material;
1110
1111     /* Pixel Shader */
1112     IWineD3DPixelShader      *pixelShader;
1113
1114     /* Pixel Shader Constants */
1115     BOOL                       pixelShaderConstantB[MAX_CONST_B];
1116     INT                        pixelShaderConstantI[MAX_CONST_I * 4];
1117     float                     *pixelShaderConstantF;
1118
1119     /* Indexed Vertex Blending */
1120     D3DVERTEXBLENDFLAGS       vertex_blend;
1121     FLOAT                     tween_factor;
1122
1123     /* RenderState */
1124     DWORD                     renderState[WINEHIGHEST_RENDER_STATE + 1];
1125
1126     /* Texture */
1127     IWineD3DBaseTexture      *textures[MAX_SAMPLERS];
1128     int                       textureDimensions[MAX_SAMPLERS];
1129
1130     /* Texture State Stage */
1131     DWORD                     textureState[MAX_TEXTURES][WINED3D_HIGHEST_TEXTURE_STATE + 1];
1132     /* Sampler States */
1133     DWORD                     samplerState[MAX_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1];
1134
1135     /* Current GLSL Shader Program */
1136     struct glsl_shader_prog_link *glsl_program;
1137 };
1138
1139 extern void stateblock_savedstates_set(
1140     IWineD3DStateBlock* iface,
1141     SAVEDSTATES* states,
1142     BOOL value);
1143
1144 extern void stateblock_savedstates_copy(
1145     IWineD3DStateBlock* iface,
1146     SAVEDSTATES* dest,
1147     SAVEDSTATES* source);
1148
1149 extern void stateblock_copy(
1150     IWineD3DStateBlock* destination,
1151     IWineD3DStateBlock* source);
1152
1153 extern const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl;
1154
1155 /*****************************************************************************
1156  * IWineD3DQueryImpl implementation structure (extends IUnknown)
1157  */
1158 typedef struct IWineD3DQueryImpl
1159 {
1160     const IWineD3DQueryVtbl  *lpVtbl;
1161     LONG                      ref;     /* Note: Ref counting not required */
1162     
1163     IUnknown                 *parent;
1164     /*TODO: replace with iface usage */
1165 #if 0
1166     IWineD3DDevice         *wineD3DDevice;
1167 #else
1168     IWineD3DDeviceImpl       *wineD3DDevice;
1169 #endif
1170     /* IWineD3DQuery fields */
1171
1172     D3DQUERYTYPE              type;
1173     /* TODO: Think about using a IUnknown instead of a void* */
1174     void                     *extendedData;
1175     
1176   
1177 } IWineD3DQueryImpl;
1178
1179 extern const IWineD3DQueryVtbl IWineD3DQuery_Vtbl;
1180
1181 /* Datastructures for IWineD3DQueryImpl.extendedData */
1182 typedef struct  WineQueryOcclusionData {
1183     GLuint  queryId;
1184 } WineQueryOcclusionData;
1185
1186
1187 /*****************************************************************************
1188  * IWineD3DSwapChainImpl implementation structure (extends IUnknown)
1189  */
1190
1191 typedef struct IWineD3DSwapChainImpl
1192 {
1193     /*IUnknown part*/
1194     IWineD3DSwapChainVtbl    *lpVtbl;
1195     LONG                      ref;     /* Note: Ref counting not required */
1196
1197     IUnknown                 *parent;
1198     IWineD3DDeviceImpl       *wineD3DDevice;
1199
1200     /* IWineD3DSwapChain fields */
1201     IWineD3DSurface         **backBuffer;
1202     IWineD3DSurface          *frontBuffer;
1203     BOOL                      wantsDepthStencilBuffer;
1204     D3DPRESENT_PARAMETERS     presentParms;
1205
1206     /* TODO: move everything up to drawable off into a context manager
1207       and store the 'data' in the contextManagerData interface.
1208     IUnknown                  *contextManagerData;
1209     */
1210
1211     HWND                    win_handle;
1212     Window                  win;
1213     Display                *display;
1214
1215     GLXContext              glCtx;
1216     XVisualInfo            *visInfo;
1217     GLXContext              render_ctx;
1218     /* This has been left in device for now, but needs moving off into a rendertarget management class and separated out from swapchains and devices. */
1219     Drawable                drawable;
1220 } IWineD3DSwapChainImpl;
1221
1222 extern IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl;
1223
1224 /*****************************************************************************
1225  * Utility function prototypes 
1226  */
1227
1228 /* Trace routines */
1229 const char* debug_d3dformat(WINED3DFORMAT fmt);
1230 const char* debug_d3ddevicetype(D3DDEVTYPE devtype);
1231 const char* debug_d3dresourcetype(WINED3DRESOURCETYPE res);
1232 const char* debug_d3dusage(DWORD usage);
1233 const char* debug_d3dusagequery(DWORD usagequery);
1234 const char* debug_d3ddeclmethod(WINED3DDECLMETHOD method);
1235 const char* debug_d3ddecltype(WINED3DDECLTYPE type);
1236 const char* debug_d3ddeclusage(BYTE usage);
1237 const char* debug_d3dprimitivetype(D3DPRIMITIVETYPE PrimitiveType);
1238 const char* debug_d3drenderstate(DWORD state);
1239 const char* debug_d3dsamplerstate(DWORD state);
1240 const char* debug_d3dtexturestate(DWORD state);
1241 const char* debug_d3dtstype(WINED3DTRANSFORMSTATETYPE tstype);
1242 const char* debug_d3dpool(WINED3DPOOL pool);
1243
1244 /* Routines for GL <-> D3D values */
1245 GLenum StencilOp(DWORD op);
1246 GLenum CompareFunc(DWORD func);
1247 void   set_tex_op(IWineD3DDevice *iface, BOOL isAlpha, int Stage, D3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3);
1248 void   set_tex_op_nvrc(IWineD3DDevice *iface, BOOL is_alpha, int stage, D3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3, INT texture_idx);
1249 void   set_texture_matrix(const float *smat, DWORD flags, BOOL calculatedCoords);
1250
1251 int D3DFmtMakeGlCfg(D3DFORMAT BackBufferFormat, D3DFORMAT StencilBufferFormat, int *attribs, int* nAttribs, BOOL alternate);
1252
1253 /* Math utils */
1254 void multiply_matrix(D3DMATRIX *dest, D3DMATRIX *src1, D3DMATRIX *src2);
1255
1256 /*****************************************************************************
1257  * To enable calling of inherited functions, requires prototypes 
1258  *
1259  * Note: Only require classes which are subclassed, ie resource, basetexture, 
1260  */
1261     /*** IUnknown methods ***/
1262     extern HRESULT WINAPI IWineD3DResourceImpl_QueryInterface(IWineD3DResource *iface, REFIID riid, void** ppvObject);
1263     extern ULONG WINAPI IWineD3DResourceImpl_AddRef(IWineD3DResource *iface);
1264     extern ULONG WINAPI IWineD3DResourceImpl_Release(IWineD3DResource *iface);
1265     /*** IWineD3DResource methods ***/
1266     extern HRESULT WINAPI IWineD3DResourceImpl_GetParent(IWineD3DResource *iface, IUnknown **pParent);
1267     extern HRESULT WINAPI IWineD3DResourceImpl_GetDevice(IWineD3DResource *iface, IWineD3DDevice ** ppDevice);
1268     extern HRESULT WINAPI IWineD3DResourceImpl_SetPrivateData(IWineD3DResource *iface, REFGUID  refguid, CONST void * pData, DWORD  SizeOfData, DWORD  Flags);
1269     extern HRESULT WINAPI IWineD3DResourceImpl_GetPrivateData(IWineD3DResource *iface, REFGUID  refguid, void * pData, DWORD * pSizeOfData);
1270     extern HRESULT WINAPI IWineD3DResourceImpl_FreePrivateData(IWineD3DResource *iface, REFGUID  refguid);
1271     extern DWORD WINAPI IWineD3DResourceImpl_SetPriority(IWineD3DResource *iface, DWORD  PriorityNew);
1272     extern DWORD WINAPI IWineD3DResourceImpl_GetPriority(IWineD3DResource *iface);
1273     extern void WINAPI IWineD3DResourceImpl_PreLoad(IWineD3DResource *iface);
1274     extern WINED3DRESOURCETYPE WINAPI IWineD3DResourceImpl_GetType(IWineD3DResource *iface);
1275     /*** class static members ***/
1276     void IWineD3DResourceImpl_CleanUp(IWineD3DResource *iface);
1277
1278     /*** IUnknown methods ***/
1279     extern HRESULT WINAPI IWineD3DBaseTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, void** ppvObject);
1280     extern ULONG WINAPI IWineD3DBaseTextureImpl_AddRef(IWineD3DBaseTexture *iface);
1281     extern ULONG WINAPI IWineD3DBaseTextureImpl_Release(IWineD3DBaseTexture *iface);
1282     /*** IWineD3DResource methods ***/
1283     extern HRESULT WINAPI IWineD3DBaseTextureImpl_GetParent(IWineD3DBaseTexture *iface, IUnknown **pParent);
1284     extern HRESULT WINAPI IWineD3DBaseTextureImpl_GetDevice(IWineD3DBaseTexture *iface, IWineD3DDevice ** ppDevice);
1285     extern HRESULT WINAPI IWineD3DBaseTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface, REFGUID  refguid, CONST void * pData, DWORD  SizeOfData, DWORD  Flags);
1286     extern HRESULT WINAPI IWineD3DBaseTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface, REFGUID  refguid, void * pData, DWORD * pSizeOfData);
1287     extern HRESULT WINAPI IWineD3DBaseTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID  refguid);
1288     extern DWORD WINAPI IWineD3DBaseTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD  PriorityNew);
1289     extern DWORD WINAPI IWineD3DBaseTextureImpl_GetPriority(IWineD3DBaseTexture *iface);
1290     extern void WINAPI IWineD3DBaseTextureImpl_PreLoad(IWineD3DBaseTexture *iface);
1291     extern WINED3DRESOURCETYPE WINAPI IWineD3DBaseTextureImpl_GetType(IWineD3DBaseTexture *iface);
1292     /*** IWineD3DBaseTexture methods ***/
1293     extern DWORD WINAPI IWineD3DBaseTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew);
1294     extern DWORD WINAPI IWineD3DBaseTextureImpl_GetLOD(IWineD3DBaseTexture *iface);
1295     extern DWORD WINAPI IWineD3DBaseTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface);
1296     extern HRESULT WINAPI IWineD3DBaseTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType);
1297     extern WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DBaseTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface);
1298     extern void WINAPI IWineD3DBaseTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface);
1299     extern BOOL WINAPI IWineD3DBaseTextureImpl_SetDirty(IWineD3DBaseTexture *iface, BOOL);
1300     extern BOOL WINAPI IWineD3DBaseTextureImpl_GetDirty(IWineD3DBaseTexture *iface);
1301
1302     extern BYTE* WINAPI IWineD3DVertexBufferImpl_GetMemory(IWineD3DVertexBuffer* iface, DWORD iOffset, GLint *vbo);
1303     extern HRESULT WINAPI IWineD3DVertexBufferImpl_ReleaseMemory(IWineD3DVertexBuffer* iface);
1304     extern HRESULT WINAPI IWineD3DBaseTextureImpl_BindTexture(IWineD3DBaseTexture *iface);
1305     extern HRESULT WINAPI IWineD3DBaseTextureImpl_UnBindTexture(IWineD3DBaseTexture *iface);
1306     extern void WINAPI IWineD3DBaseTextureImpl_ApplyStateChanges(IWineD3DBaseTexture *iface, const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1], const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]);
1307     /*** class static members ***/
1308     void IWineD3DBaseTextureImpl_CleanUp(IWineD3DBaseTexture *iface);
1309
1310 struct SHADER_OPCODE_ARG;
1311 typedef void (*shader_fct_t)();
1312 typedef void (*SHADER_HANDLER) (struct SHADER_OPCODE_ARG*);
1313
1314 /* Struct to maintain a list of GLSL shader programs and their associated pixel and
1315  * vertex shaders.  A list of this type is maintained on the DeviceImpl, and is only
1316  * used if the user is using GLSL shaders. */
1317 struct glsl_shader_prog_link {
1318     struct list             entry;
1319     GLhandleARB             programId;
1320     GLhandleARB             *vuniformF_locations;
1321     GLhandleARB             *puniformF_locations;
1322     IWineD3DVertexShader*   vertexShader;
1323     IWineD3DPixelShader*    pixelShader;
1324 };
1325
1326 /* TODO: Make this dynamic, based on shader limits ? */
1327 #define MAX_REG_ADDR 1
1328 #define MAX_REG_TEMP 32
1329 #define MAX_REG_TEXCRD 8
1330 #define MAX_REG_INPUT 12
1331 #define MAX_REG_OUTPUT 12
1332 #define MAX_ATTRIBS 16
1333 #define MAX_CONST_I 16
1334 #define MAX_CONST_B 16
1335
1336 /* FIXME: This needs to go up to 2048 for
1337  * Shader model 3 according to msdn (and for software shaders) */
1338 #define MAX_LABELS 16
1339
1340 typedef struct semantic {
1341     DWORD usage;
1342     DWORD reg;
1343 } semantic;
1344
1345 typedef struct local_constant {
1346     struct list entry;
1347     unsigned int idx;
1348     DWORD value[4];
1349 } local_constant;
1350
1351 typedef struct shader_reg_maps {
1352
1353     char texcoord[MAX_REG_TEXCRD];          /* pixel < 3.0 */
1354     char temporary[MAX_REG_TEMP];           /* pixel, vertex */
1355     char address[MAX_REG_ADDR];             /* vertex */
1356     char packed_input[MAX_REG_INPUT];       /* pshader >= 3.0 */
1357     char packed_output[MAX_REG_OUTPUT];     /* vertex >= 3.0 */
1358     char attributes[MAX_ATTRIBS];           /* vertex */
1359     char labels[MAX_LABELS];                /* pixel, vertex */
1360
1361     /* Sampler usage tokens 
1362      * Use 0 as default (bit 31 is always 1 on a valid token) */
1363     DWORD samplers[MAX_SAMPLERS];
1364
1365     /* Whether or not a loop is used in this shader */
1366     char loop;
1367
1368     /* Whether or not this shader uses fog */
1369     char fog;
1370
1371 } shader_reg_maps;
1372
1373 #define SHADER_PGMSIZE 65535
1374 typedef struct SHADER_BUFFER {
1375     char* buffer;
1376     unsigned int bsize;
1377     unsigned int lineNo;
1378 } SHADER_BUFFER;
1379
1380 /* Undocumented opcode controls */
1381 #define INST_CONTROLS_SHIFT 16
1382 #define INST_CONTROLS_MASK 0x00ff0000
1383
1384 typedef enum COMPARISON_TYPE {
1385     COMPARISON_GT = 1,
1386     COMPARISON_EQ = 2,
1387     COMPARISON_GE = 3,
1388     COMPARISON_LT = 4,
1389     COMPARISON_NE = 5,
1390     COMPARISON_LE = 6
1391 } COMPARISON_TYPE;
1392
1393 typedef struct SHADER_OPCODE {
1394     unsigned int  opcode;
1395     const char*   name;
1396     const char*   glname;
1397     char          dst_token;
1398     CONST UINT    num_params;
1399     shader_fct_t  soft_fct;
1400     SHADER_HANDLER hw_fct;
1401     SHADER_HANDLER hw_glsl_fct;
1402     DWORD         min_version;
1403     DWORD         max_version;
1404 } SHADER_OPCODE;
1405
1406 typedef struct SHADER_OPCODE_ARG {
1407     IWineD3DBaseShader* shader;
1408     shader_reg_maps* reg_maps;
1409     CONST SHADER_OPCODE* opcode;
1410     DWORD opcode_token;
1411     DWORD dst;
1412     DWORD dst_addr;
1413     DWORD predicate;
1414     DWORD src[4];
1415     DWORD src_addr[4];
1416     SHADER_BUFFER* buffer;
1417 } SHADER_OPCODE_ARG;
1418
1419 typedef struct SHADER_LIMITS {
1420     unsigned int temporary;
1421     unsigned int texcoord;
1422     unsigned int sampler;
1423     unsigned int constant_int;
1424     unsigned int constant_float;
1425     unsigned int constant_bool;
1426     unsigned int address;
1427     unsigned int packed_output;
1428     unsigned int packed_input;
1429     unsigned int attributes;
1430     unsigned int label;
1431 } SHADER_LIMITS;
1432
1433 /** Keeps track of details for TEX_M#x# shader opcodes which need to 
1434     maintain state information between multiple codes */
1435 typedef struct SHADER_PARSE_STATE {
1436     unsigned int current_row;
1437     DWORD texcoord_w[2];
1438 } SHADER_PARSE_STATE;
1439
1440 /* Base Shader utility functions. 
1441  * (may move callers into the same file in the future) */
1442 extern int shader_addline(
1443     SHADER_BUFFER* buffer,
1444     const char* fmt, ...);
1445
1446 extern const SHADER_OPCODE* shader_get_opcode(
1447     IWineD3DBaseShader *iface, 
1448     const DWORD code);
1449
1450 extern void shader_delete_constant_list(
1451     struct list* clist);
1452
1453 /* Vertex shader utility functions */
1454 extern BOOL vshader_get_input(
1455     IWineD3DVertexShader* iface,
1456     BYTE usage_req, BYTE usage_idx_req,
1457     unsigned int* regnum);
1458
1459 extern BOOL vshader_input_is_color(
1460     IWineD3DVertexShader* iface,
1461     unsigned int regnum);
1462
1463 extern HRESULT allocate_shader_constants(IWineD3DStateBlockImpl* object);
1464
1465 /* ARB_[vertex/fragment]_program helper functions */
1466 extern void shader_arb_load_constants(
1467     IWineD3DStateBlock* iface,
1468     char usePixelShader,
1469     char useVertexShader);
1470
1471 /* ARB shader program Prototypes */
1472 extern void shader_hw_def(SHADER_OPCODE_ARG *arg);
1473
1474 /* ARB pixel shader prototypes */
1475 extern void pshader_hw_cnd(SHADER_OPCODE_ARG* arg);
1476 extern void pshader_hw_cmp(SHADER_OPCODE_ARG* arg);
1477 extern void pshader_hw_map2gl(SHADER_OPCODE_ARG* arg);
1478 extern void pshader_hw_tex(SHADER_OPCODE_ARG* arg);
1479 extern void pshader_hw_texcoord(SHADER_OPCODE_ARG* arg);
1480 extern void pshader_hw_texreg2ar(SHADER_OPCODE_ARG* arg);
1481 extern void pshader_hw_texreg2gb(SHADER_OPCODE_ARG* arg);
1482 extern void pshader_hw_texbem(SHADER_OPCODE_ARG* arg);
1483 extern void pshader_hw_texm3x2pad(SHADER_OPCODE_ARG* arg);
1484 extern void pshader_hw_texm3x2tex(SHADER_OPCODE_ARG* arg);
1485 extern void pshader_hw_texm3x3pad(SHADER_OPCODE_ARG* arg);
1486 extern void pshader_hw_texm3x3tex(SHADER_OPCODE_ARG* arg);
1487 extern void pshader_hw_texm3x3spec(SHADER_OPCODE_ARG* arg);
1488 extern void pshader_hw_texm3x3vspec(SHADER_OPCODE_ARG* arg);
1489
1490 /* ARB vertex shader prototypes */
1491 extern void vshader_hw_map2gl(SHADER_OPCODE_ARG* arg);
1492 extern void vshader_hw_mnxn(SHADER_OPCODE_ARG* arg);
1493
1494 /* GLSL helper functions */
1495 extern void set_glsl_shader_program(IWineD3DDevice *iface);
1496 extern void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG *arg);
1497 extern void shader_glsl_load_constants(
1498     IWineD3DStateBlock* iface,
1499     char usePixelShader,
1500     char useVertexShader);
1501
1502 /** The following translate DirectX pixel/vertex shader opcodes to GLSL lines */
1503 extern void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg);
1504 extern void shader_glsl_arith(SHADER_OPCODE_ARG* arg);
1505 extern void shader_glsl_mov(SHADER_OPCODE_ARG* arg);
1506 extern void shader_glsl_mad(SHADER_OPCODE_ARG* arg);
1507 extern void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg);
1508 extern void shader_glsl_lrp(SHADER_OPCODE_ARG* arg);
1509 extern void shader_glsl_dot(SHADER_OPCODE_ARG* arg);
1510 extern void shader_glsl_rcp(SHADER_OPCODE_ARG* arg);
1511 extern void shader_glsl_cnd(SHADER_OPCODE_ARG* arg);
1512 extern void shader_glsl_compare(SHADER_OPCODE_ARG* arg);
1513 extern void shader_glsl_def(SHADER_OPCODE_ARG* arg);
1514 extern void shader_glsl_defi(SHADER_OPCODE_ARG* arg);
1515 extern void shader_glsl_defb(SHADER_OPCODE_ARG* arg);
1516 extern void shader_glsl_expp(SHADER_OPCODE_ARG* arg);
1517 extern void shader_glsl_cmp(SHADER_OPCODE_ARG* arg);
1518 extern void shader_glsl_lit(SHADER_OPCODE_ARG* arg);
1519 extern void shader_glsl_dst(SHADER_OPCODE_ARG* arg);
1520 extern void shader_glsl_sincos(SHADER_OPCODE_ARG* arg);
1521 extern void shader_glsl_loop(SHADER_OPCODE_ARG* arg);
1522 extern void shader_glsl_end(SHADER_OPCODE_ARG* arg);
1523 extern void shader_glsl_if(SHADER_OPCODE_ARG* arg);
1524 extern void shader_glsl_ifc(SHADER_OPCODE_ARG* arg);
1525 extern void shader_glsl_else(SHADER_OPCODE_ARG* arg);
1526 extern void shader_glsl_break(SHADER_OPCODE_ARG* arg);
1527 extern void shader_glsl_breakc(SHADER_OPCODE_ARG* arg);
1528 extern void shader_glsl_rep(SHADER_OPCODE_ARG* arg);
1529 extern void shader_glsl_call(SHADER_OPCODE_ARG* arg);
1530 extern void shader_glsl_callnz(SHADER_OPCODE_ARG* arg);
1531 extern void shader_glsl_label(SHADER_OPCODE_ARG* arg);
1532
1533 /** GLSL Pixel Shader Prototypes */
1534 extern void pshader_glsl_tex(SHADER_OPCODE_ARG* arg);
1535 extern void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg);
1536 extern void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg);
1537 extern void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg);
1538 extern void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg);
1539 extern void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg);
1540 extern void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg);
1541 extern void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg);
1542 extern void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg);
1543 extern void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg);
1544 extern void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg);
1545 extern void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg);
1546 extern void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg);
1547 extern void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg);
1548 extern void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg);
1549 extern void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg);
1550 extern void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg);
1551 extern void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg);
1552 extern void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg);
1553 extern void pshader_glsl_input_pack(
1554    SHADER_BUFFER* buffer,
1555    semantic* semantics_out);
1556
1557 /** GLSL Vertex Shader Prototypes */
1558 extern void vshader_glsl_output_unpack(
1559    SHADER_BUFFER* buffer,
1560    semantic* semantics_out);
1561
1562 /*****************************************************************************
1563  * IDirect3DBaseShader implementation structure
1564  */
1565 typedef struct IWineD3DBaseShaderClass
1566 {
1567     DWORD                           hex_version;
1568     SHADER_LIMITS                   limits;
1569     SHADER_PARSE_STATE              parse_state;
1570     CONST SHADER_OPCODE             *shader_ins;
1571     CONST DWORD                     *function;
1572     UINT                            functionLength;
1573     GLuint                          prgId;
1574     BOOL                            is_compiled;
1575
1576     /* Type of shader backend */
1577     int shader_mode;
1578
1579     /* Immediate constants (override global ones) */
1580     struct list constantsB;
1581     struct list constantsF;
1582     struct list constantsI;
1583     shader_reg_maps reg_maps;
1584
1585 } IWineD3DBaseShaderClass;
1586
1587 typedef struct IWineD3DBaseShaderImpl {
1588     /* IUnknown */
1589     const IWineD3DBaseShaderVtbl    *lpVtbl;
1590     LONG                            ref;
1591
1592     /* IWineD3DBaseShader */
1593     IWineD3DBaseShaderClass         baseShader;
1594 } IWineD3DBaseShaderImpl;
1595
1596 extern HRESULT shader_get_registers_used(
1597     IWineD3DBaseShader *iface,
1598     shader_reg_maps* reg_maps,
1599     semantic* semantics_in,
1600     semantic* semantics_out,
1601     CONST DWORD* pToken,
1602     IWineD3DStateBlockImpl *stateBlock);
1603
1604 extern void shader_generate_glsl_declarations(
1605     IWineD3DBaseShader *iface,
1606     shader_reg_maps* reg_maps,
1607     SHADER_BUFFER* buffer,
1608     WineD3D_GL_Info* gl_info);
1609
1610 extern void shader_generate_arb_declarations(
1611     IWineD3DBaseShader *iface,
1612     shader_reg_maps* reg_maps,
1613     SHADER_BUFFER* buffer,
1614     WineD3D_GL_Info* gl_info);
1615
1616 extern void shader_generate_main(
1617     IWineD3DBaseShader *iface,
1618     SHADER_BUFFER* buffer,
1619     shader_reg_maps* reg_maps,
1620     CONST DWORD* pFunction);
1621
1622 extern void shader_dump_ins_modifiers(
1623     const DWORD output);
1624
1625 extern void shader_dump_param(
1626     IWineD3DBaseShader *iface,
1627     const DWORD param,
1628     const DWORD addr_token,
1629     int input);
1630
1631 extern void shader_trace_init(
1632     IWineD3DBaseShader *iface,
1633     const DWORD* pFunction);
1634
1635 extern int shader_get_param(
1636     IWineD3DBaseShader* iface,
1637     const DWORD* pToken,
1638     DWORD* param,
1639     DWORD* addr_token);
1640
1641 extern int shader_skip_unrecognized(
1642     IWineD3DBaseShader* iface,
1643     const DWORD* pToken);
1644
1645 extern void print_glsl_info_log(
1646     WineD3D_GL_Info *gl_info,
1647     GLhandleARB obj);
1648
1649 inline static int shader_get_regtype(const DWORD param) {
1650     return (((param & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT) |
1651             ((param & D3DSP_REGTYPE_MASK2) >> D3DSP_REGTYPE_SHIFT2));
1652 }
1653
1654 extern unsigned int shader_get_float_offset(const DWORD reg);
1655
1656 inline static BOOL shader_is_pshader_version(DWORD token) {
1657     return 0xFFFF0000 == (token & 0xFFFF0000);
1658 }
1659
1660 inline static BOOL shader_is_vshader_version(DWORD token) {
1661     return 0xFFFE0000 == (token & 0xFFFF0000);
1662 }
1663
1664 inline static BOOL shader_is_comment(DWORD token) {
1665     return D3DSIO_COMMENT == (token & D3DSI_OPCODE_MASK);
1666 }
1667
1668 /*****************************************************************************
1669  * IDirect3DVertexShader implementation structure
1670  */
1671 typedef struct IWineD3DVertexShaderImpl {
1672     /* IUnknown parts*/   
1673     const IWineD3DVertexShaderVtbl *lpVtbl;
1674     LONG                        ref;     /* Note: Ref counting not required */
1675
1676     /* IWineD3DBaseShader */
1677     IWineD3DBaseShaderClass     baseShader;
1678
1679     /* IWineD3DVertexShaderImpl */
1680     IUnknown                    *parent;
1681     IWineD3DDeviceImpl          *wineD3DDevice;
1682
1683     char                        usesFog;
1684     DWORD                       usage;
1685
1686     /* Vertex shader input and output semantics */
1687     semantic semantics_in [MAX_ATTRIBS];
1688     semantic semantics_out [MAX_REG_OUTPUT];
1689
1690     /* run time datas...  */
1691     VSHADERDATA                *data;
1692     IWineD3DVertexDeclaration  *vertexDeclaration;
1693 #if 0 /* needs reworking */
1694     /* run time datas */
1695     VSHADERINPUTDATA input;
1696     VSHADEROUTPUTDATA output;
1697 #endif
1698 } IWineD3DVertexShaderImpl;
1699 extern const SHADER_OPCODE IWineD3DVertexShaderImpl_shader_ins[];
1700 extern const IWineD3DVertexShaderVtbl IWineD3DVertexShader_Vtbl;
1701
1702 /*****************************************************************************
1703  * IDirect3DPixelShader implementation structure
1704  */
1705 typedef struct IWineD3DPixelShaderImpl {
1706     /* IUnknown parts */
1707     const IWineD3DPixelShaderVtbl *lpVtbl;
1708     LONG                        ref;     /* Note: Ref counting not required */
1709
1710     /* IWineD3DBaseShader */
1711     IWineD3DBaseShaderClass     baseShader;
1712
1713     /* IWineD3DPixelShaderImpl */
1714     IUnknown                   *parent;
1715     IWineD3DDeviceImpl         *wineD3DDevice;
1716
1717     /* Pixel shader input semantics */
1718     semantic semantics_in [MAX_REG_INPUT];
1719
1720     /* run time data */
1721     PSHADERDATA                *data;
1722
1723 #if 0 /* needs reworking */
1724     PSHADERINPUTDATA input;
1725     PSHADEROUTPUTDATA output;
1726 #endif
1727 } IWineD3DPixelShaderImpl;
1728
1729 extern const SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins[];
1730 extern const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl;
1731
1732 /*****************************************************************************
1733  * IWineD3DPalette implementation structure
1734  */
1735 struct IWineD3DPaletteImpl {
1736     /* IUnknown parts */
1737     const IWineD3DPaletteVtbl  *lpVtbl;
1738     LONG                       ref;
1739
1740     IUnknown                   *parent;
1741     IWineD3DDeviceImpl         *wineD3DDevice;
1742
1743     /* IWineD3DPalette */
1744     HPALETTE                   hpal;
1745     WORD                       palVersion;     /*|               */
1746     WORD                       palNumEntries;  /*|  LOGPALETTE   */
1747     PALETTEENTRY               palents[256];   /*|               */
1748     /* This is to store the palette in 'screen format' */
1749     int                        screen_palents[256];
1750     DWORD                      Flags;
1751 };
1752
1753 extern const IWineD3DPaletteVtbl IWineD3DPalette_Vtbl;
1754 DWORD IWineD3DPaletteImpl_Size(DWORD dwFlags);
1755
1756 /* DirectDraw utility functions */
1757 extern WINED3DFORMAT pixelformat_for_depth(DWORD depth);
1758
1759 /*****************************************************************************
1760  * Pixel format management
1761  */
1762 typedef struct {
1763     WINED3DFORMAT           format;
1764     DWORD                   alphaMask, redMask, greenMask, blueMask;
1765     UINT                    bpp;
1766     BOOL                    isFourcc;
1767     GLint                   glInternal, glFormat, glType;
1768 } PixelFormatDesc;
1769
1770 const PixelFormatDesc *getFormatDescEntry(WINED3DFORMAT fmt);
1771 #endif