wined3d: Apply the half pixel correction for shaders too.
[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                      textureName;
864     UINT                      bytesPerPixel;
865
866     /* TODO: move this off into a management class(maybe!) */
867     DWORD                      Flags;
868
869     UINT                      pow2Width;
870     UINT                      pow2Height;
871     UINT                      pow2Size;
872
873     /* Oversized texture */
874     RECT                      glRect;
875
876 #if 0
877     /* precalculated x and y scalings for texture coords */
878     float                     pow2scalingFactorX; /* =  (Width  / pow2Width ) */
879     float                     pow2scalingFactorY; /* =  (Height / pow2Height) */
880 #endif
881
882     RECT                      lockedRect;
883     RECT                      dirtyRect;
884
885     glDescriptor              glDescription;
886
887     /* For GetDC */
888     wineD3DSurface_DIB        dib;
889     HDC                       hDC;
890
891     /* Color keys for DDraw */
892     DDCOLORKEY                DestBltCKey;
893     DDCOLORKEY                DestOverlayCKey;
894     DDCOLORKEY                SrcOverlayCKey;
895     DDCOLORKEY                SrcBltCKey;
896     DWORD                     CKeyFlags;
897
898     DDCOLORKEY                glCKey;
899 };
900
901 extern const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl;
902 extern const IWineD3DSurfaceVtbl IWineGDISurface_Vtbl;
903
904 /* Predeclare the shared Surface functions */
905 HRESULT WINAPI IWineD3DSurfaceImpl_QueryInterface(IWineD3DSurface *iface, REFIID riid, LPVOID *ppobj);
906 ULONG WINAPI IWineD3DSurfaceImpl_AddRef(IWineD3DSurface *iface);
907 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface);
908 HRESULT WINAPI IWineD3DSurfaceImpl_GetParent(IWineD3DSurface *iface, IUnknown **pParent);
909 HRESULT WINAPI IWineD3DSurfaceImpl_GetDevice(IWineD3DSurface *iface, IWineD3DDevice** ppDevice);
910 HRESULT WINAPI IWineD3DSurfaceImpl_SetPrivateData(IWineD3DSurface *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags);
911 HRESULT WINAPI IWineD3DSurfaceImpl_GetPrivateData(IWineD3DSurface *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData);
912 HRESULT WINAPI IWineD3DSurfaceImpl_FreePrivateData(IWineD3DSurface *iface, REFGUID refguid);
913 DWORD   WINAPI IWineD3DSurfaceImpl_SetPriority(IWineD3DSurface *iface, DWORD PriorityNew);
914 DWORD   WINAPI IWineD3DSurfaceImpl_GetPriority(IWineD3DSurface *iface);
915 void    WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface);
916 WINED3DRESOURCETYPE WINAPI IWineD3DSurfaceImpl_GetType(IWineD3DSurface *iface);
917 HRESULT WINAPI IWineD3DSurfaceImpl_GetContainerParent(IWineD3DSurface* iface, IUnknown **ppContainerParent);
918 HRESULT WINAPI IWineD3DSurfaceImpl_GetContainer(IWineD3DSurface* iface, REFIID riid, void** ppContainer);
919 HRESULT WINAPI IWineD3DSurfaceImpl_GetDesc(IWineD3DSurface *iface, WINED3DSURFACE_DESC *pDesc);
920 HRESULT WINAPI IWineD3DSurfaceImpl_GetBltStatus(IWineD3DSurface *iface, DWORD Flags);
921 HRESULT WINAPI IWineD3DSurfaceImpl_GetFlipStatus(IWineD3DSurface *iface, DWORD Flags);
922 HRESULT WINAPI IWineD3DSurfaceImpl_IsLost(IWineD3DSurface *iface);
923 HRESULT WINAPI IWineD3DSurfaceImpl_Restore(IWineD3DSurface *iface);
924 HRESULT WINAPI IWineD3DSurfaceImpl_SetPixelFormat(IWineD3DSurface *iface, WINED3DFORMAT Format, BYTE *Surface, DWORD Size);
925 HRESULT WINAPI IWineD3DSurfaceImpl_GetPalette(IWineD3DSurface *iface, IWineD3DPalette **Pal);
926 HRESULT WINAPI IWineD3DSurfaceImpl_SetPalette(IWineD3DSurface *iface, IWineD3DPalette *Pal);
927 HRESULT WINAPI IWineD3DSurfaceImpl_SetColorKey(IWineD3DSurface *iface, DWORD Flags, DDCOLORKEY *CKey);
928 HRESULT WINAPI IWineD3DSurfaceImpl_CleanDirtyRect(IWineD3DSurface *iface);
929 extern HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect);
930 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container);
931 HRESULT WINAPI IWineD3DSurfaceImpl_SetPBufferState(IWineD3DSurface *iface, BOOL inPBuffer, BOOL  inTexture);
932 void WINAPI IWineD3DSurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target);
933 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription);
934 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface);
935 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format);
936 HRESULT WINAPI IWineGDISurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx);
937 HRESULT WINAPI IWineGDISurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans);
938 HRESULT WINAPI IWineD3DSurfaceImpl_SetPalette(IWineD3DSurface *iface, IWineD3DPalette *Pal);
939 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC);
940 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC);
941 DWORD WINAPI IWineD3DSurfaceImpl_GetPitch(IWineD3DSurface *iface);
942 HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface);
943 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem);
944
945 /* Surface flags: */
946 #define SFLAG_OVERSIZE    0x00000001 /* Surface is bigger than gl size, blts only */
947 #define SFLAG_CONVERTED   0x00000002 /* Converted for color keying or Palettized */
948 #define SFLAG_DIBSECTION  0x00000004 /* Has a DIB section attached for getdc */
949 #define SFLAG_DIRTY       0x00000008 /* Surface was locked by the app */
950 #define SFLAG_LOCKABLE    0x00000010 /* Surface can be locked */
951 #define SFLAG_DISCARD     0x00000020 /* ??? */
952 #define SFLAG_LOCKED      0x00000040 /* Surface is locked atm */
953 #define SFLAG_ACTIVELOCK  0x00000080 /* Not locked, but surface memory is needed */
954 #define SFLAG_INTEXTURE   0x00000100 /* ??? */
955 #define SFLAG_INPBUFFER   0x00000200 /* ??? */
956 #define SFLAG_NONPOW2     0x00000400 /* Surface sizes are not a power of 2 */
957 #define SFLAG_DYNLOCK     0x00000800 /* Surface is often locked by the app */
958 #define SFLAG_DYNCHANGE   0x00001800 /* Surface contents are changed very often, implies DYNLOCK */
959 #define SFLAG_DCINUSE     0x00002000 /* Set between GetDC and ReleaseDC calls */
960 #define SFLAG_GLDIRTY     0x00004000 /* The opengl texture is more up to date than the surface mem */
961 #define SFLAG_LOST        0x00008000 /* Surface lost flag for DDraw */
962 #define SFLAG_FORCELOAD   0x00010000 /* To force PreLoading of a scratch cursor */
963 #define SFLAG_USERPTR     0x00020000 /* The application allocated the memory for this surface */
964 #define SFLAG_GLCKEY      0x00040000 /* The gl texture was created with a color key */
965
966 /* In some conditions the surface memory must not be freed:
967  * SFLAG_OVERSIZE: Not all data can be kept in GL
968  * SFLAG_CONVERTED: Converting the data back would take too long
969  * SFLAG_DIBSECTION: The dib code manages the memory
970  * SFLAG_DIRTY: GL surface isn't up to date
971  * SFLAG_LOCKED: The app requires access to the surface data
972  * SFLAG_ACTIVELOCK: Some wined3d code needs the memory
973  * SFLAG_DYNLOCK: Avoid freeing the data for performance
974  * SFLAG_DYNCHANGE: Same reason as DYNLOCK
975  */
976 #define SFLAG_DONOTFREE  (SFLAG_OVERSIZE   | \
977                           SFLAG_CONVERTED  | \
978                           SFLAG_DIBSECTION | \
979                           SFLAG_DIRTY      | \
980                           SFLAG_LOCKED     | \
981                           SFLAG_ACTIVELOCK | \
982                           SFLAG_DYNLOCK    | \
983                           SFLAG_DYNCHANGE  | \
984                           SFLAG_USERPTR)
985
986 BOOL CalculateTexRect(IWineD3DSurfaceImpl *This, RECT *Rect, float glTexCoord[4]);
987
988 /*****************************************************************************
989  * IWineD3DVertexDeclaration implementation structure
990  */
991 typedef struct IWineD3DVertexDeclarationImpl {
992  /* IUnknown  Information     */
993   const IWineD3DVertexDeclarationVtbl *lpVtbl;
994   LONG                    ref;     /* Note: Ref counting not required */
995
996   IUnknown               *parent;
997   /** precomputed fvf if simple declaration */
998   IWineD3DDeviceImpl     *wineD3DDevice;
999   DWORD   fvf[MAX_STREAMS];
1000   DWORD   allFVF;
1001
1002   /** dx8 compatible Declaration fields */
1003   DWORD*  pDeclaration8;
1004   DWORD   declaration8Length;
1005
1006   /** dx9+ */
1007   D3DVERTEXELEMENT9 *pDeclaration9;
1008   UINT               declaration9NumElements;
1009
1010   WINED3DVERTEXELEMENT  *pDeclarationWine;
1011   UINT                   declarationWNumElements;
1012   
1013   float                 *constants;
1014   
1015 } IWineD3DVertexDeclarationImpl;
1016
1017 extern const IWineD3DVertexDeclarationVtbl IWineD3DVertexDeclaration_Vtbl;
1018
1019 /*****************************************************************************
1020  * IWineD3DStateBlock implementation structure
1021  */
1022
1023 /* Internal state Block for Begin/End/Capture/Create/Apply info  */
1024 /*   Note: Very long winded but gl Lists are not flexible enough */
1025 /*   to resolve everything we need, so doing it manually for now */
1026 typedef struct SAVEDSTATES {
1027         BOOL                      indices;
1028         BOOL                      material;
1029         BOOL                      fvf;
1030         BOOL                      streamSource[MAX_STREAMS];
1031         BOOL                      streamFreq[MAX_STREAMS];
1032         BOOL                      textures[MAX_SAMPLERS];
1033         BOOL                      transform[HIGHEST_TRANSFORMSTATE + 1];
1034         BOOL                      viewport;
1035         BOOL                      renderState[WINEHIGHEST_RENDER_STATE + 1];
1036         BOOL                      textureState[MAX_TEXTURES][WINED3D_HIGHEST_TEXTURE_STATE + 1];
1037         BOOL                      samplerState[MAX_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1];
1038         BOOL                      clipplane[MAX_CLIPPLANES];
1039         BOOL                      vertexDecl;
1040         BOOL                      pixelShader;
1041         BOOL                      pixelShaderConstantsB[MAX_CONST_B];
1042         BOOL                      pixelShaderConstantsI[MAX_CONST_I];
1043         BOOL                     *pixelShaderConstantsF;
1044         BOOL                      vertexShader;
1045         BOOL                      vertexShaderConstantsB[MAX_CONST_B];
1046         BOOL                      vertexShaderConstantsI[MAX_CONST_I];
1047         BOOL                     *vertexShaderConstantsF;
1048 } SAVEDSTATES;
1049
1050 typedef struct {
1051     struct list entry;
1052     int idx;
1053 } constant_entry;
1054
1055 struct IWineD3DStateBlockImpl
1056 {
1057     /* IUnknown fields */
1058     const IWineD3DStateBlockVtbl *lpVtbl;
1059     LONG                      ref;     /* Note: Ref counting not required */
1060
1061     /* IWineD3DStateBlock information */
1062     IUnknown                 *parent;
1063     IWineD3DDeviceImpl       *wineD3DDevice;
1064     WINED3DSTATEBLOCKTYPE     blockType;
1065
1066     /* Array indicating whether things have been set or changed */
1067     SAVEDSTATES               changed;
1068     SAVEDSTATES               set;
1069     struct list               set_vconstantsF;
1070     struct list               set_pconstantsF;
1071
1072     /* Drawing - Vertex Shader or FVF related */
1073     DWORD                     fvf;
1074     /* Vertex Shader Declaration */
1075     IWineD3DVertexDeclaration *vertexDecl;
1076
1077     IWineD3DVertexShader      *vertexShader;
1078
1079     /* Vertex Shader Constants */
1080     BOOL                       vertexShaderConstantB[MAX_CONST_B];
1081     INT                        vertexShaderConstantI[MAX_CONST_I * 4];
1082     float                     *vertexShaderConstantF;
1083
1084     /* Stream Source */
1085     BOOL                      streamIsUP;
1086     UINT                      streamStride[MAX_STREAMS];
1087     UINT                      streamOffset[MAX_STREAMS];
1088     IWineD3DVertexBuffer     *streamSource[MAX_STREAMS];
1089     UINT                      streamFreq[MAX_STREAMS];
1090     UINT                      streamFlags[MAX_STREAMS];     /*0 | D3DSTREAMSOURCE_INSTANCEDATA | D3DSTREAMSOURCE_INDEXEDDATA  */
1091
1092     /* Indices */
1093     IWineD3DIndexBuffer*      pIndexData;
1094     UINT                      baseVertexIndex; /* Note: only used for d3d8 */
1095
1096     /* Transform */
1097     D3DMATRIX                 transforms[HIGHEST_TRANSFORMSTATE + 1];
1098
1099     /* Lights */
1100     PLIGHTINFOEL             *lights; /* NOTE: active GL lights must be front of the chain */
1101
1102     /* Clipping */
1103     double                    clipplane[MAX_CLIPPLANES][4];
1104     WINED3DCLIPSTATUS         clip_status;
1105
1106     /* ViewPort */
1107     WINED3DVIEWPORT           viewport;
1108
1109     /* Material */
1110     WINED3DMATERIAL           material;
1111
1112     /* Pixel Shader */
1113     IWineD3DPixelShader      *pixelShader;
1114
1115     /* Pixel Shader Constants */
1116     BOOL                       pixelShaderConstantB[MAX_CONST_B];
1117     INT                        pixelShaderConstantI[MAX_CONST_I * 4];
1118     float                     *pixelShaderConstantF;
1119
1120     /* Indexed Vertex Blending */
1121     D3DVERTEXBLENDFLAGS       vertex_blend;
1122     FLOAT                     tween_factor;
1123
1124     /* RenderState */
1125     DWORD                     renderState[WINEHIGHEST_RENDER_STATE + 1];
1126
1127     /* Texture */
1128     IWineD3DBaseTexture      *textures[MAX_SAMPLERS];
1129     int                       textureDimensions[MAX_SAMPLERS];
1130
1131     /* Texture State Stage */
1132     DWORD                     textureState[MAX_TEXTURES][WINED3D_HIGHEST_TEXTURE_STATE + 1];
1133     /* Sampler States */
1134     DWORD                     samplerState[MAX_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1];
1135
1136     /* Current GLSL Shader Program */
1137     struct glsl_shader_prog_link *glsl_program;
1138 };
1139
1140 extern void stateblock_savedstates_set(
1141     IWineD3DStateBlock* iface,
1142     SAVEDSTATES* states,
1143     BOOL value);
1144
1145 extern void stateblock_savedstates_copy(
1146     IWineD3DStateBlock* iface,
1147     SAVEDSTATES* dest,
1148     SAVEDSTATES* source);
1149
1150 extern void stateblock_copy(
1151     IWineD3DStateBlock* destination,
1152     IWineD3DStateBlock* source);
1153
1154 extern const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl;
1155
1156 /*****************************************************************************
1157  * IWineD3DQueryImpl implementation structure (extends IUnknown)
1158  */
1159 typedef struct IWineD3DQueryImpl
1160 {
1161     const IWineD3DQueryVtbl  *lpVtbl;
1162     LONG                      ref;     /* Note: Ref counting not required */
1163     
1164     IUnknown                 *parent;
1165     /*TODO: replace with iface usage */
1166 #if 0
1167     IWineD3DDevice         *wineD3DDevice;
1168 #else
1169     IWineD3DDeviceImpl       *wineD3DDevice;
1170 #endif
1171     /* IWineD3DQuery fields */
1172
1173     D3DQUERYTYPE              type;
1174     /* TODO: Think about using a IUnknown instead of a void* */
1175     void                     *extendedData;
1176     
1177   
1178 } IWineD3DQueryImpl;
1179
1180 extern const IWineD3DQueryVtbl IWineD3DQuery_Vtbl;
1181
1182 /* Datastructures for IWineD3DQueryImpl.extendedData */
1183 typedef struct  WineQueryOcclusionData {
1184     GLuint  queryId;
1185 } WineQueryOcclusionData;
1186
1187
1188 /*****************************************************************************
1189  * IWineD3DSwapChainImpl implementation structure (extends IUnknown)
1190  */
1191
1192 typedef struct IWineD3DSwapChainImpl
1193 {
1194     /*IUnknown part*/
1195     IWineD3DSwapChainVtbl    *lpVtbl;
1196     LONG                      ref;     /* Note: Ref counting not required */
1197
1198     IUnknown                 *parent;
1199     IWineD3DDeviceImpl       *wineD3DDevice;
1200
1201     /* IWineD3DSwapChain fields */
1202     IWineD3DSurface         **backBuffer;
1203     IWineD3DSurface          *frontBuffer;
1204     BOOL                      wantsDepthStencilBuffer;
1205     D3DPRESENT_PARAMETERS     presentParms;
1206
1207     /* TODO: move everything up to drawable off into a context manager
1208       and store the 'data' in the contextManagerData interface.
1209     IUnknown                  *contextManagerData;
1210     */
1211
1212     HWND                    win_handle;
1213     Window                  win;
1214     Display                *display;
1215
1216     GLXContext              glCtx;
1217     XVisualInfo            *visInfo;
1218     GLXContext              render_ctx;
1219     /* This has been left in device for now, but needs moving off into a rendertarget management class and separated out from swapchains and devices. */
1220     Drawable                drawable;
1221 } IWineD3DSwapChainImpl;
1222
1223 extern IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl;
1224
1225 /*****************************************************************************
1226  * Utility function prototypes 
1227  */
1228
1229 /* Trace routines */
1230 const char* debug_d3dformat(WINED3DFORMAT fmt);
1231 const char* debug_d3ddevicetype(D3DDEVTYPE devtype);
1232 const char* debug_d3dresourcetype(WINED3DRESOURCETYPE res);
1233 const char* debug_d3dusage(DWORD usage);
1234 const char* debug_d3dusagequery(DWORD usagequery);
1235 const char* debug_d3ddeclmethod(WINED3DDECLMETHOD method);
1236 const char* debug_d3ddecltype(WINED3DDECLTYPE type);
1237 const char* debug_d3ddeclusage(BYTE usage);
1238 const char* debug_d3dprimitivetype(D3DPRIMITIVETYPE PrimitiveType);
1239 const char* debug_d3drenderstate(DWORD state);
1240 const char* debug_d3dsamplerstate(DWORD state);
1241 const char* debug_d3dtexturestate(DWORD state);
1242 const char* debug_d3dtstype(WINED3DTRANSFORMSTATETYPE tstype);
1243 const char* debug_d3dpool(WINED3DPOOL pool);
1244
1245 /* Routines for GL <-> D3D values */
1246 GLenum StencilOp(DWORD op);
1247 GLenum CompareFunc(DWORD func);
1248 void   set_tex_op(IWineD3DDevice *iface, BOOL isAlpha, int Stage, D3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3);
1249 void   set_tex_op_nvrc(IWineD3DDevice *iface, BOOL is_alpha, int stage, D3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3, INT texture_idx);
1250 void   set_texture_matrix(const float *smat, DWORD flags, BOOL calculatedCoords);
1251
1252 int D3DFmtMakeGlCfg(D3DFORMAT BackBufferFormat, D3DFORMAT StencilBufferFormat, int *attribs, int* nAttribs, BOOL alternate);
1253
1254 /* Math utils */
1255 void multiply_matrix(D3DMATRIX *dest, D3DMATRIX *src1, D3DMATRIX *src2);
1256
1257 /*****************************************************************************
1258  * To enable calling of inherited functions, requires prototypes 
1259  *
1260  * Note: Only require classes which are subclassed, ie resource, basetexture, 
1261  */
1262     /*** IUnknown methods ***/
1263     extern HRESULT WINAPI IWineD3DResourceImpl_QueryInterface(IWineD3DResource *iface, REFIID riid, void** ppvObject);
1264     extern ULONG WINAPI IWineD3DResourceImpl_AddRef(IWineD3DResource *iface);
1265     extern ULONG WINAPI IWineD3DResourceImpl_Release(IWineD3DResource *iface);
1266     /*** IWineD3DResource methods ***/
1267     extern HRESULT WINAPI IWineD3DResourceImpl_GetParent(IWineD3DResource *iface, IUnknown **pParent);
1268     extern HRESULT WINAPI IWineD3DResourceImpl_GetDevice(IWineD3DResource *iface, IWineD3DDevice ** ppDevice);
1269     extern HRESULT WINAPI IWineD3DResourceImpl_SetPrivateData(IWineD3DResource *iface, REFGUID  refguid, CONST void * pData, DWORD  SizeOfData, DWORD  Flags);
1270     extern HRESULT WINAPI IWineD3DResourceImpl_GetPrivateData(IWineD3DResource *iface, REFGUID  refguid, void * pData, DWORD * pSizeOfData);
1271     extern HRESULT WINAPI IWineD3DResourceImpl_FreePrivateData(IWineD3DResource *iface, REFGUID  refguid);
1272     extern DWORD WINAPI IWineD3DResourceImpl_SetPriority(IWineD3DResource *iface, DWORD  PriorityNew);
1273     extern DWORD WINAPI IWineD3DResourceImpl_GetPriority(IWineD3DResource *iface);
1274     extern void WINAPI IWineD3DResourceImpl_PreLoad(IWineD3DResource *iface);
1275     extern WINED3DRESOURCETYPE WINAPI IWineD3DResourceImpl_GetType(IWineD3DResource *iface);
1276     /*** class static members ***/
1277     void IWineD3DResourceImpl_CleanUp(IWineD3DResource *iface);
1278
1279     /*** IUnknown methods ***/
1280     extern HRESULT WINAPI IWineD3DBaseTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, void** ppvObject);
1281     extern ULONG WINAPI IWineD3DBaseTextureImpl_AddRef(IWineD3DBaseTexture *iface);
1282     extern ULONG WINAPI IWineD3DBaseTextureImpl_Release(IWineD3DBaseTexture *iface);
1283     /*** IWineD3DResource methods ***/
1284     extern HRESULT WINAPI IWineD3DBaseTextureImpl_GetParent(IWineD3DBaseTexture *iface, IUnknown **pParent);
1285     extern HRESULT WINAPI IWineD3DBaseTextureImpl_GetDevice(IWineD3DBaseTexture *iface, IWineD3DDevice ** ppDevice);
1286     extern HRESULT WINAPI IWineD3DBaseTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface, REFGUID  refguid, CONST void * pData, DWORD  SizeOfData, DWORD  Flags);
1287     extern HRESULT WINAPI IWineD3DBaseTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface, REFGUID  refguid, void * pData, DWORD * pSizeOfData);
1288     extern HRESULT WINAPI IWineD3DBaseTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID  refguid);
1289     extern DWORD WINAPI IWineD3DBaseTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD  PriorityNew);
1290     extern DWORD WINAPI IWineD3DBaseTextureImpl_GetPriority(IWineD3DBaseTexture *iface);
1291     extern void WINAPI IWineD3DBaseTextureImpl_PreLoad(IWineD3DBaseTexture *iface);
1292     extern WINED3DRESOURCETYPE WINAPI IWineD3DBaseTextureImpl_GetType(IWineD3DBaseTexture *iface);
1293     /*** IWineD3DBaseTexture methods ***/
1294     extern DWORD WINAPI IWineD3DBaseTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew);
1295     extern DWORD WINAPI IWineD3DBaseTextureImpl_GetLOD(IWineD3DBaseTexture *iface);
1296     extern DWORD WINAPI IWineD3DBaseTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface);
1297     extern HRESULT WINAPI IWineD3DBaseTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType);
1298     extern WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DBaseTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface);
1299     extern void WINAPI IWineD3DBaseTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface);
1300     extern BOOL WINAPI IWineD3DBaseTextureImpl_SetDirty(IWineD3DBaseTexture *iface, BOOL);
1301     extern BOOL WINAPI IWineD3DBaseTextureImpl_GetDirty(IWineD3DBaseTexture *iface);
1302
1303     extern BYTE* WINAPI IWineD3DVertexBufferImpl_GetMemory(IWineD3DVertexBuffer* iface, DWORD iOffset, GLint *vbo);
1304     extern HRESULT WINAPI IWineD3DVertexBufferImpl_ReleaseMemory(IWineD3DVertexBuffer* iface);
1305     extern HRESULT WINAPI IWineD3DBaseTextureImpl_BindTexture(IWineD3DBaseTexture *iface);
1306     extern HRESULT WINAPI IWineD3DBaseTextureImpl_UnBindTexture(IWineD3DBaseTexture *iface);
1307     extern void WINAPI IWineD3DBaseTextureImpl_ApplyStateChanges(IWineD3DBaseTexture *iface, const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1], const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]);
1308     /*** class static members ***/
1309     void IWineD3DBaseTextureImpl_CleanUp(IWineD3DBaseTexture *iface);
1310
1311 struct SHADER_OPCODE_ARG;
1312 typedef void (*shader_fct_t)();
1313 typedef void (*SHADER_HANDLER) (struct SHADER_OPCODE_ARG*);
1314
1315 /* Struct to maintain a list of GLSL shader programs and their associated pixel and
1316  * vertex shaders.  A list of this type is maintained on the DeviceImpl, and is only
1317  * used if the user is using GLSL shaders. */
1318 struct glsl_shader_prog_link {
1319     struct list             entry;
1320     GLhandleARB             programId;
1321     GLhandleARB             *vuniformF_locations;
1322     GLhandleARB             *puniformF_locations;
1323     IWineD3DVertexShader*   vertexShader;
1324     IWineD3DPixelShader*    pixelShader;
1325 };
1326
1327 /* TODO: Make this dynamic, based on shader limits ? */
1328 #define MAX_REG_ADDR 1
1329 #define MAX_REG_TEMP 32
1330 #define MAX_REG_TEXCRD 8
1331 #define MAX_REG_INPUT 12
1332 #define MAX_REG_OUTPUT 12
1333 #define MAX_ATTRIBS 16
1334 #define MAX_CONST_I 16
1335 #define MAX_CONST_B 16
1336
1337 /* FIXME: This needs to go up to 2048 for
1338  * Shader model 3 according to msdn (and for software shaders) */
1339 #define MAX_LABELS 16
1340
1341 typedef struct semantic {
1342     DWORD usage;
1343     DWORD reg;
1344 } semantic;
1345
1346 typedef struct local_constant {
1347     struct list entry;
1348     unsigned int idx;
1349     DWORD value[4];
1350 } local_constant;
1351
1352 typedef struct shader_reg_maps {
1353
1354     char texcoord[MAX_REG_TEXCRD];          /* pixel < 3.0 */
1355     char temporary[MAX_REG_TEMP];           /* pixel, vertex */
1356     char address[MAX_REG_ADDR];             /* vertex */
1357     char packed_input[MAX_REG_INPUT];       /* pshader >= 3.0 */
1358     char packed_output[MAX_REG_OUTPUT];     /* vertex >= 3.0 */
1359     char attributes[MAX_ATTRIBS];           /* vertex */
1360     char labels[MAX_LABELS];                /* pixel, vertex */
1361
1362     /* Sampler usage tokens 
1363      * Use 0 as default (bit 31 is always 1 on a valid token) */
1364     DWORD samplers[MAX_SAMPLERS];
1365
1366     /* Whether or not a loop is used in this shader */
1367     char loop;
1368
1369     /* Whether or not this shader uses fog */
1370     char fog;
1371
1372 } shader_reg_maps;
1373
1374 #define SHADER_PGMSIZE 65535
1375 typedef struct SHADER_BUFFER {
1376     char* buffer;
1377     unsigned int bsize;
1378     unsigned int lineNo;
1379 } SHADER_BUFFER;
1380
1381 /* Undocumented opcode controls */
1382 #define INST_CONTROLS_SHIFT 16
1383 #define INST_CONTROLS_MASK 0x00ff0000
1384
1385 typedef enum COMPARISON_TYPE {
1386     COMPARISON_GT = 1,
1387     COMPARISON_EQ = 2,
1388     COMPARISON_GE = 3,
1389     COMPARISON_LT = 4,
1390     COMPARISON_NE = 5,
1391     COMPARISON_LE = 6
1392 } COMPARISON_TYPE;
1393
1394 typedef struct SHADER_OPCODE {
1395     unsigned int  opcode;
1396     const char*   name;
1397     const char*   glname;
1398     char          dst_token;
1399     CONST UINT    num_params;
1400     shader_fct_t  soft_fct;
1401     SHADER_HANDLER hw_fct;
1402     SHADER_HANDLER hw_glsl_fct;
1403     DWORD         min_version;
1404     DWORD         max_version;
1405 } SHADER_OPCODE;
1406
1407 typedef struct SHADER_OPCODE_ARG {
1408     IWineD3DBaseShader* shader;
1409     shader_reg_maps* reg_maps;
1410     CONST SHADER_OPCODE* opcode;
1411     DWORD opcode_token;
1412     DWORD dst;
1413     DWORD dst_addr;
1414     DWORD predicate;
1415     DWORD src[4];
1416     DWORD src_addr[4];
1417     SHADER_BUFFER* buffer;
1418 } SHADER_OPCODE_ARG;
1419
1420 typedef struct SHADER_LIMITS {
1421     unsigned int temporary;
1422     unsigned int texcoord;
1423     unsigned int sampler;
1424     unsigned int constant_int;
1425     unsigned int constant_float;
1426     unsigned int constant_bool;
1427     unsigned int address;
1428     unsigned int packed_output;
1429     unsigned int packed_input;
1430     unsigned int attributes;
1431     unsigned int label;
1432 } SHADER_LIMITS;
1433
1434 /** Keeps track of details for TEX_M#x# shader opcodes which need to 
1435     maintain state information between multiple codes */
1436 typedef struct SHADER_PARSE_STATE {
1437     unsigned int current_row;
1438     DWORD texcoord_w[2];
1439 } SHADER_PARSE_STATE;
1440
1441 /* Base Shader utility functions. 
1442  * (may move callers into the same file in the future) */
1443 extern int shader_addline(
1444     SHADER_BUFFER* buffer,
1445     const char* fmt, ...);
1446
1447 extern const SHADER_OPCODE* shader_get_opcode(
1448     IWineD3DBaseShader *iface, 
1449     const DWORD code);
1450
1451 extern void shader_delete_constant_list(
1452     struct list* clist);
1453
1454 /* Vertex shader utility functions */
1455 extern BOOL vshader_get_input(
1456     IWineD3DVertexShader* iface,
1457     BYTE usage_req, BYTE usage_idx_req,
1458     unsigned int* regnum);
1459
1460 extern BOOL vshader_input_is_color(
1461     IWineD3DVertexShader* iface,
1462     unsigned int regnum);
1463
1464 extern HRESULT allocate_shader_constants(IWineD3DStateBlockImpl* object);
1465
1466 /* ARB_[vertex/fragment]_program helper functions */
1467 extern void shader_arb_load_constants(
1468     IWineD3DStateBlock* iface,
1469     char usePixelShader,
1470     char useVertexShader);
1471
1472 /* ARB shader program Prototypes */
1473 extern void shader_hw_def(SHADER_OPCODE_ARG *arg);
1474
1475 /* ARB pixel shader prototypes */
1476 extern void pshader_hw_cnd(SHADER_OPCODE_ARG* arg);
1477 extern void pshader_hw_cmp(SHADER_OPCODE_ARG* arg);
1478 extern void pshader_hw_map2gl(SHADER_OPCODE_ARG* arg);
1479 extern void pshader_hw_tex(SHADER_OPCODE_ARG* arg);
1480 extern void pshader_hw_texcoord(SHADER_OPCODE_ARG* arg);
1481 extern void pshader_hw_texreg2ar(SHADER_OPCODE_ARG* arg);
1482 extern void pshader_hw_texreg2gb(SHADER_OPCODE_ARG* arg);
1483 extern void pshader_hw_texbem(SHADER_OPCODE_ARG* arg);
1484 extern void pshader_hw_texm3x2pad(SHADER_OPCODE_ARG* arg);
1485 extern void pshader_hw_texm3x2tex(SHADER_OPCODE_ARG* arg);
1486 extern void pshader_hw_texm3x3pad(SHADER_OPCODE_ARG* arg);
1487 extern void pshader_hw_texm3x3tex(SHADER_OPCODE_ARG* arg);
1488 extern void pshader_hw_texm3x3spec(SHADER_OPCODE_ARG* arg);
1489 extern void pshader_hw_texm3x3vspec(SHADER_OPCODE_ARG* arg);
1490
1491 /* ARB vertex shader prototypes */
1492 extern void vshader_hw_map2gl(SHADER_OPCODE_ARG* arg);
1493 extern void vshader_hw_mnxn(SHADER_OPCODE_ARG* arg);
1494
1495 /* GLSL helper functions */
1496 extern void set_glsl_shader_program(IWineD3DDevice *iface);
1497 extern void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG *arg);
1498 extern void shader_glsl_load_constants(
1499     IWineD3DStateBlock* iface,
1500     char usePixelShader,
1501     char useVertexShader);
1502
1503 /** The following translate DirectX pixel/vertex shader opcodes to GLSL lines */
1504 extern void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg);
1505 extern void shader_glsl_arith(SHADER_OPCODE_ARG* arg);
1506 extern void shader_glsl_mov(SHADER_OPCODE_ARG* arg);
1507 extern void shader_glsl_mad(SHADER_OPCODE_ARG* arg);
1508 extern void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg);
1509 extern void shader_glsl_lrp(SHADER_OPCODE_ARG* arg);
1510 extern void shader_glsl_dot(SHADER_OPCODE_ARG* arg);
1511 extern void shader_glsl_rcp(SHADER_OPCODE_ARG* arg);
1512 extern void shader_glsl_cnd(SHADER_OPCODE_ARG* arg);
1513 extern void shader_glsl_compare(SHADER_OPCODE_ARG* arg);
1514 extern void shader_glsl_def(SHADER_OPCODE_ARG* arg);
1515 extern void shader_glsl_defi(SHADER_OPCODE_ARG* arg);
1516 extern void shader_glsl_defb(SHADER_OPCODE_ARG* arg);
1517 extern void shader_glsl_expp(SHADER_OPCODE_ARG* arg);
1518 extern void shader_glsl_cmp(SHADER_OPCODE_ARG* arg);
1519 extern void shader_glsl_lit(SHADER_OPCODE_ARG* arg);
1520 extern void shader_glsl_dst(SHADER_OPCODE_ARG* arg);
1521 extern void shader_glsl_sincos(SHADER_OPCODE_ARG* arg);
1522 extern void shader_glsl_loop(SHADER_OPCODE_ARG* arg);
1523 extern void shader_glsl_end(SHADER_OPCODE_ARG* arg);
1524 extern void shader_glsl_if(SHADER_OPCODE_ARG* arg);
1525 extern void shader_glsl_ifc(SHADER_OPCODE_ARG* arg);
1526 extern void shader_glsl_else(SHADER_OPCODE_ARG* arg);
1527 extern void shader_glsl_break(SHADER_OPCODE_ARG* arg);
1528 extern void shader_glsl_breakc(SHADER_OPCODE_ARG* arg);
1529 extern void shader_glsl_rep(SHADER_OPCODE_ARG* arg);
1530 extern void shader_glsl_call(SHADER_OPCODE_ARG* arg);
1531 extern void shader_glsl_callnz(SHADER_OPCODE_ARG* arg);
1532 extern void shader_glsl_label(SHADER_OPCODE_ARG* arg);
1533
1534 /** GLSL Pixel Shader Prototypes */
1535 extern void pshader_glsl_tex(SHADER_OPCODE_ARG* arg);
1536 extern void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg);
1537 extern void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg);
1538 extern void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg);
1539 extern void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg);
1540 extern void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg);
1541 extern void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg);
1542 extern void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg);
1543 extern void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg);
1544 extern void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg);
1545 extern void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg);
1546 extern void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg);
1547 extern void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg);
1548 extern void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg);
1549 extern void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg);
1550 extern void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg);
1551 extern void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg);
1552 extern void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg);
1553 extern void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg);
1554 extern void pshader_glsl_input_pack(
1555    SHADER_BUFFER* buffer,
1556    semantic* semantics_out);
1557
1558 /** GLSL Vertex Shader Prototypes */
1559 extern void vshader_glsl_output_unpack(
1560    SHADER_BUFFER* buffer,
1561    semantic* semantics_out);
1562
1563 /*****************************************************************************
1564  * IDirect3DBaseShader implementation structure
1565  */
1566 typedef struct IWineD3DBaseShaderClass
1567 {
1568     DWORD                           hex_version;
1569     SHADER_LIMITS                   limits;
1570     SHADER_PARSE_STATE              parse_state;
1571     CONST SHADER_OPCODE             *shader_ins;
1572     CONST DWORD                     *function;
1573     UINT                            functionLength;
1574     GLuint                          prgId;
1575     BOOL                            is_compiled;
1576
1577     /* Type of shader backend */
1578     int shader_mode;
1579
1580     /* Immediate constants (override global ones) */
1581     struct list constantsB;
1582     struct list constantsF;
1583     struct list constantsI;
1584     shader_reg_maps reg_maps;
1585
1586 } IWineD3DBaseShaderClass;
1587
1588 typedef struct IWineD3DBaseShaderImpl {
1589     /* IUnknown */
1590     const IWineD3DBaseShaderVtbl    *lpVtbl;
1591     LONG                            ref;
1592
1593     /* IWineD3DBaseShader */
1594     IWineD3DBaseShaderClass         baseShader;
1595 } IWineD3DBaseShaderImpl;
1596
1597 extern HRESULT shader_get_registers_used(
1598     IWineD3DBaseShader *iface,
1599     shader_reg_maps* reg_maps,
1600     semantic* semantics_in,
1601     semantic* semantics_out,
1602     CONST DWORD* pToken,
1603     IWineD3DStateBlockImpl *stateBlock);
1604
1605 extern void shader_generate_glsl_declarations(
1606     IWineD3DBaseShader *iface,
1607     shader_reg_maps* reg_maps,
1608     SHADER_BUFFER* buffer,
1609     WineD3D_GL_Info* gl_info);
1610
1611 extern void shader_generate_arb_declarations(
1612     IWineD3DBaseShader *iface,
1613     shader_reg_maps* reg_maps,
1614     SHADER_BUFFER* buffer,
1615     WineD3D_GL_Info* gl_info);
1616
1617 extern void shader_generate_main(
1618     IWineD3DBaseShader *iface,
1619     SHADER_BUFFER* buffer,
1620     shader_reg_maps* reg_maps,
1621     CONST DWORD* pFunction);
1622
1623 extern void shader_dump_ins_modifiers(
1624     const DWORD output);
1625
1626 extern void shader_dump_param(
1627     IWineD3DBaseShader *iface,
1628     const DWORD param,
1629     const DWORD addr_token,
1630     int input);
1631
1632 extern void shader_trace_init(
1633     IWineD3DBaseShader *iface,
1634     const DWORD* pFunction);
1635
1636 extern int shader_get_param(
1637     IWineD3DBaseShader* iface,
1638     const DWORD* pToken,
1639     DWORD* param,
1640     DWORD* addr_token);
1641
1642 extern int shader_skip_unrecognized(
1643     IWineD3DBaseShader* iface,
1644     const DWORD* pToken);
1645
1646 extern void print_glsl_info_log(
1647     WineD3D_GL_Info *gl_info,
1648     GLhandleARB obj);
1649
1650 inline static int shader_get_regtype(const DWORD param) {
1651     return (((param & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT) |
1652             ((param & D3DSP_REGTYPE_MASK2) >> D3DSP_REGTYPE_SHIFT2));
1653 }
1654
1655 extern unsigned int shader_get_float_offset(const DWORD reg);
1656
1657 inline static BOOL shader_is_pshader_version(DWORD token) {
1658     return 0xFFFF0000 == (token & 0xFFFF0000);
1659 }
1660
1661 inline static BOOL shader_is_vshader_version(DWORD token) {
1662     return 0xFFFE0000 == (token & 0xFFFF0000);
1663 }
1664
1665 inline static BOOL shader_is_comment(DWORD token) {
1666     return D3DSIO_COMMENT == (token & D3DSI_OPCODE_MASK);
1667 }
1668
1669 /*****************************************************************************
1670  * IDirect3DVertexShader implementation structure
1671  */
1672 typedef struct IWineD3DVertexShaderImpl {
1673     /* IUnknown parts*/   
1674     const IWineD3DVertexShaderVtbl *lpVtbl;
1675     LONG                        ref;     /* Note: Ref counting not required */
1676
1677     /* IWineD3DBaseShader */
1678     IWineD3DBaseShaderClass     baseShader;
1679
1680     /* IWineD3DVertexShaderImpl */
1681     IUnknown                    *parent;
1682     IWineD3DDeviceImpl          *wineD3DDevice;
1683
1684     char                        usesFog;
1685     DWORD                       usage;
1686
1687     /* Vertex shader input and output semantics */
1688     semantic semantics_in [MAX_ATTRIBS];
1689     semantic semantics_out [MAX_REG_OUTPUT];
1690
1691     /* run time datas...  */
1692     VSHADERDATA                *data;
1693     IWineD3DVertexDeclaration  *vertexDeclaration;
1694 #if 0 /* needs reworking */
1695     /* run time datas */
1696     VSHADERINPUTDATA input;
1697     VSHADEROUTPUTDATA output;
1698 #endif
1699 } IWineD3DVertexShaderImpl;
1700 extern const SHADER_OPCODE IWineD3DVertexShaderImpl_shader_ins[];
1701 extern const IWineD3DVertexShaderVtbl IWineD3DVertexShader_Vtbl;
1702
1703 /*****************************************************************************
1704  * IDirect3DPixelShader implementation structure
1705  */
1706 typedef struct IWineD3DPixelShaderImpl {
1707     /* IUnknown parts */
1708     const IWineD3DPixelShaderVtbl *lpVtbl;
1709     LONG                        ref;     /* Note: Ref counting not required */
1710
1711     /* IWineD3DBaseShader */
1712     IWineD3DBaseShaderClass     baseShader;
1713
1714     /* IWineD3DPixelShaderImpl */
1715     IUnknown                   *parent;
1716     IWineD3DDeviceImpl         *wineD3DDevice;
1717
1718     /* Pixel shader input semantics */
1719     semantic semantics_in [MAX_REG_INPUT];
1720
1721     /* run time data */
1722     PSHADERDATA                *data;
1723
1724 #if 0 /* needs reworking */
1725     PSHADERINPUTDATA input;
1726     PSHADEROUTPUTDATA output;
1727 #endif
1728 } IWineD3DPixelShaderImpl;
1729
1730 extern const SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins[];
1731 extern const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl;
1732
1733 /*****************************************************************************
1734  * IWineD3DPalette implementation structure
1735  */
1736 struct IWineD3DPaletteImpl {
1737     /* IUnknown parts */
1738     const IWineD3DPaletteVtbl  *lpVtbl;
1739     LONG                       ref;
1740
1741     IUnknown                   *parent;
1742     IWineD3DDeviceImpl         *wineD3DDevice;
1743
1744     /* IWineD3DPalette */
1745     HPALETTE                   hpal;
1746     WORD                       palVersion;     /*|               */
1747     WORD                       palNumEntries;  /*|  LOGPALETTE   */
1748     PALETTEENTRY               palents[256];   /*|               */
1749     /* This is to store the palette in 'screen format' */
1750     int                        screen_palents[256];
1751     DWORD                      Flags;
1752 };
1753
1754 extern const IWineD3DPaletteVtbl IWineD3DPalette_Vtbl;
1755 DWORD IWineD3DPaletteImpl_Size(DWORD dwFlags);
1756
1757 /* DirectDraw utility functions */
1758 extern WINED3DFORMAT pixelformat_for_depth(DWORD depth);
1759
1760 /*****************************************************************************
1761  * Pixel format management
1762  */
1763 typedef struct {
1764     WINED3DFORMAT           format;
1765     DWORD                   alphaMask, redMask, greenMask, blueMask;
1766     UINT                    bpp;
1767     BOOL                    isFourcc;
1768     GLint                   glInternal, glFormat, glType;
1769 } PixelFormatDesc;
1770
1771 const PixelFormatDesc *getFormatDescEntry(WINED3DFORMAT fmt);
1772 #endif