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