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