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