wined3d: Only use stretch_rect_fbo() on surfaces that are FBO attachable.
[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 2002-2003, 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 "objbase.h"
41 #include "wine/wined3d.h"
42 #include "wined3d_gl.h"
43 #include "wine/list.h"
44 #include "wine/rbtree.h"
45
46 /* Texture format fixups */
47
48 enum fixup_channel_source
49 {
50     CHANNEL_SOURCE_ZERO = 0,
51     CHANNEL_SOURCE_ONE = 1,
52     CHANNEL_SOURCE_X = 2,
53     CHANNEL_SOURCE_Y = 3,
54     CHANNEL_SOURCE_Z = 4,
55     CHANNEL_SOURCE_W = 5,
56     CHANNEL_SOURCE_YUV0 = 6,
57     CHANNEL_SOURCE_YUV1 = 7,
58 };
59
60 enum yuv_fixup
61 {
62     YUV_FIXUP_YUY2 = 0,
63     YUV_FIXUP_UYVY = 1,
64     YUV_FIXUP_YV12 = 2,
65 };
66
67 #include <pshpack2.h>
68 struct color_fixup_desc
69 {
70     unsigned x_sign_fixup : 1;
71     unsigned x_source : 3;
72     unsigned y_sign_fixup : 1;
73     unsigned y_source : 3;
74     unsigned z_sign_fixup : 1;
75     unsigned z_source : 3;
76     unsigned w_sign_fixup : 1;
77     unsigned w_source : 3;
78 };
79 #include <poppack.h>
80
81 static const struct color_fixup_desc COLOR_FIXUP_IDENTITY =
82         {0, CHANNEL_SOURCE_X, 0, CHANNEL_SOURCE_Y, 0, CHANNEL_SOURCE_Z, 0, CHANNEL_SOURCE_W};
83
84 static inline struct color_fixup_desc create_color_fixup_desc(
85         int sign0, enum fixup_channel_source src0, int sign1, enum fixup_channel_source src1,
86         int sign2, enum fixup_channel_source src2, int sign3, enum fixup_channel_source src3)
87 {
88     struct color_fixup_desc fixup =
89     {
90         sign0, src0,
91         sign1, src1,
92         sign2, src2,
93         sign3, src3,
94     };
95     return fixup;
96 }
97
98 static inline struct color_fixup_desc create_yuv_fixup_desc(enum yuv_fixup yuv_fixup)
99 {
100     struct color_fixup_desc fixup =
101     {
102         0, yuv_fixup & (1 << 0) ? CHANNEL_SOURCE_YUV1 : CHANNEL_SOURCE_YUV0,
103         0, yuv_fixup & (1 << 1) ? CHANNEL_SOURCE_YUV1 : CHANNEL_SOURCE_YUV0,
104         0, yuv_fixup & (1 << 2) ? CHANNEL_SOURCE_YUV1 : CHANNEL_SOURCE_YUV0,
105         0, yuv_fixup & (1 << 3) ? CHANNEL_SOURCE_YUV1 : CHANNEL_SOURCE_YUV0,
106     };
107     return fixup;
108 }
109
110 static inline BOOL is_identity_fixup(struct color_fixup_desc fixup)
111 {
112     return !memcmp(&fixup, &COLOR_FIXUP_IDENTITY, sizeof(fixup));
113 }
114
115 static inline BOOL is_yuv_fixup(struct color_fixup_desc fixup)
116 {
117     return fixup.x_source == CHANNEL_SOURCE_YUV0 || fixup.x_source == CHANNEL_SOURCE_YUV1;
118 }
119
120 static inline enum yuv_fixup get_yuv_fixup(struct color_fixup_desc fixup)
121 {
122     enum yuv_fixup yuv_fixup = 0;
123     if (fixup.x_source == CHANNEL_SOURCE_YUV1) yuv_fixup |= (1 << 0);
124     if (fixup.y_source == CHANNEL_SOURCE_YUV1) yuv_fixup |= (1 << 1);
125     if (fixup.z_source == CHANNEL_SOURCE_YUV1) yuv_fixup |= (1 << 2);
126     if (fixup.w_source == CHANNEL_SOURCE_YUV1) yuv_fixup |= (1 << 3);
127     return yuv_fixup;
128 }
129
130 void *wined3d_rb_alloc(size_t size);
131 void *wined3d_rb_realloc(void *ptr, size_t size);
132 void wined3d_rb_free(void *ptr);
133
134 /* Device caps */
135 #define MAX_PALETTES            65536
136 #define MAX_STREAMS             16
137 #define MAX_TEXTURES            8
138 #define MAX_FRAGMENT_SAMPLERS   16
139 #define MAX_VERTEX_SAMPLERS     4
140 #define MAX_COMBINED_SAMPLERS   (MAX_FRAGMENT_SAMPLERS + MAX_VERTEX_SAMPLERS)
141 #define MAX_ACTIVE_LIGHTS       8
142 #define MAX_CLIPPLANES          WINED3DMAXUSERCLIPPLANES
143
144 /* Used for CreateStateBlock */
145 #define NUM_SAVEDPIXELSTATES_R     35
146 #define NUM_SAVEDPIXELSTATES_T     18
147 #define NUM_SAVEDPIXELSTATES_S     12
148 #define NUM_SAVEDVERTEXSTATES_R    34
149 #define NUM_SAVEDVERTEXSTATES_T    2
150 #define NUM_SAVEDVERTEXSTATES_S    1
151
152 extern const DWORD SavedPixelStates_R[NUM_SAVEDPIXELSTATES_R];
153 extern const DWORD SavedPixelStates_T[NUM_SAVEDPIXELSTATES_T];
154 extern const DWORD SavedPixelStates_S[NUM_SAVEDPIXELSTATES_S];
155 extern const DWORD SavedVertexStates_R[NUM_SAVEDVERTEXSTATES_R];
156 extern const DWORD SavedVertexStates_T[NUM_SAVEDVERTEXSTATES_T];
157 extern const DWORD SavedVertexStates_S[NUM_SAVEDVERTEXSTATES_S];
158
159 typedef enum _WINELOOKUP {
160     WINELOOKUP_WARPPARAM = 0,
161     MAX_LOOKUPS          = 1
162 } WINELOOKUP;
163
164 extern const int minLookup[MAX_LOOKUPS];
165 extern const int maxLookup[MAX_LOOKUPS];
166 extern DWORD *stateLookup[MAX_LOOKUPS];
167
168 struct min_lookup
169 {
170     GLenum mip[WINED3DTEXF_LINEAR + 1];
171 };
172
173 struct min_lookup minMipLookup[WINED3DTEXF_ANISOTROPIC + 1];
174 const struct min_lookup minMipLookup_noFilter[WINED3DTEXF_ANISOTROPIC + 1];
175 GLenum magLookup[WINED3DTEXF_ANISOTROPIC + 1];
176 const GLenum magLookup_noFilter[WINED3DTEXF_ANISOTROPIC + 1];
177
178 extern const struct filter_lookup filter_lookup_nofilter;
179 extern struct filter_lookup filter_lookup;
180
181 /* float_16_to_32() and float_32_to_16() (see implementation in
182  * surface_base.c) convert 16 bit floats in the FLOAT16 data type
183  * to standard C floats and vice versa. They do not depend on the encoding
184  * of the C float, so they are platform independent, but slow. On x86 and
185  * other IEEE 754 compliant platforms the conversion can be accelerated by
186  * bit shifting the exponent and mantissa. There are also some SSE-based
187  * assembly routines out there.
188  *
189  * See GL_NV_half_float for a reference of the FLOAT16 / GL_HALF format
190  */
191 static inline float float_16_to_32(const unsigned short *in) {
192     const unsigned short s = ((*in) & 0x8000);
193     const unsigned short e = ((*in) & 0x7C00) >> 10;
194     const unsigned short m = (*in) & 0x3FF;
195     const float sgn = (s ? -1.0 : 1.0);
196
197     if(e == 0) {
198         if(m == 0) return sgn * 0.0; /* +0.0 or -0.0 */
199         else return sgn * pow(2, -14.0) * ( (float) m / 1024.0);
200     } else if(e < 31) {
201         return sgn * pow(2, (float) e-15.0) * (1.0 + ((float) m / 1024.0));
202     } else {
203         if(m == 0) return sgn / 0.0; /* +INF / -INF */
204         else return 0.0 / 0.0; /* NAN */
205     }
206 }
207
208 /**
209  * Settings 
210  */
211 #define VS_NONE    0
212 #define VS_HW      1
213
214 #define PS_NONE    0
215 #define PS_HW      1
216
217 #define VBO_NONE   0
218 #define VBO_HW     1
219
220 #define NP2_NONE   0
221 #define NP2_REPACK 1
222 #define NP2_NATIVE 2
223
224 #define ORM_BACKBUFFER  0
225 #define ORM_PBUFFER     1
226 #define ORM_FBO         2
227
228 #define SHADER_ARB  1
229 #define SHADER_GLSL 2
230 #define SHADER_ATI  3
231 #define SHADER_NONE 4
232
233 #define RTL_DISABLE   -1
234 #define RTL_AUTO       0
235 #define RTL_READDRAW   1
236 #define RTL_READTEX    2
237 #define RTL_TEXDRAW    3
238 #define RTL_TEXTEX     4
239
240 #define PCI_VENDOR_NONE 0xffff /* e.g. 0x8086 for Intel and 0x10de for Nvidia */
241 #define PCI_DEVICE_NONE 0xffff /* e.g. 0x14f for a Geforce6200 */
242
243 /* NOTE: When adding fields to this structure, make sure to update the default
244  * values in wined3d_main.c as well. */
245 typedef struct wined3d_settings_s {
246 /* vertex and pixel shader modes */
247   int vs_mode;
248   int ps_mode;
249   int vbo_mode;
250 /* Ideally, we don't want the user to have to request GLSL.  If the hardware supports GLSL,
251     we should use it.  However, until it's fully implemented, we'll leave it as a registry
252     setting for developers. */
253   BOOL glslRequested;
254   int offscreen_rendering_mode;
255   int rendertargetlock_mode;
256   unsigned short pci_vendor_id;
257   unsigned short pci_device_id;
258 /* Memory tracking and object counting */
259   unsigned int emulated_textureram;
260   char *logo;
261   int allow_multisampling;
262 } wined3d_settings_t;
263
264 extern wined3d_settings_t wined3d_settings;
265
266 typedef enum _WINED3DSAMPLER_TEXTURE_TYPE
267 {
268     WINED3DSTT_UNKNOWN = 0,
269     WINED3DSTT_1D = 1,
270     WINED3DSTT_2D = 2,
271     WINED3DSTT_CUBE = 3,
272     WINED3DSTT_VOLUME = 4,
273 } WINED3DSAMPLER_TEXTURE_TYPE;
274
275 typedef enum _WINED3DSHADER_PARAM_REGISTER_TYPE
276 {
277     WINED3DSPR_TEMP = 0,
278     WINED3DSPR_INPUT = 1,
279     WINED3DSPR_CONST = 2,
280     WINED3DSPR_ADDR = 3,
281     WINED3DSPR_TEXTURE = 3,
282     WINED3DSPR_RASTOUT = 4,
283     WINED3DSPR_ATTROUT = 5,
284     WINED3DSPR_TEXCRDOUT = 6,
285     WINED3DSPR_OUTPUT = 6,
286     WINED3DSPR_CONSTINT = 7,
287     WINED3DSPR_COLOROUT = 8,
288     WINED3DSPR_DEPTHOUT = 9,
289     WINED3DSPR_SAMPLER = 10,
290     WINED3DSPR_CONST2 = 11,
291     WINED3DSPR_CONST3 = 12,
292     WINED3DSPR_CONST4 = 13,
293     WINED3DSPR_CONSTBOOL = 14,
294     WINED3DSPR_LOOP = 15,
295     WINED3DSPR_TEMPFLOAT16 = 16,
296     WINED3DSPR_MISCTYPE = 17,
297     WINED3DSPR_LABEL = 18,
298     WINED3DSPR_PREDICATE = 19,
299     WINED3DSPR_IMMCONST,
300 } WINED3DSHADER_PARAM_REGISTER_TYPE;
301
302 enum wined3d_immconst_type
303 {
304     WINED3D_IMMCONST_FLOAT,
305     WINED3D_IMMCONST_FLOAT4,
306 };
307
308 typedef enum _WINED3DVS_RASTOUT_OFFSETS
309 {
310     WINED3DSRO_POSITION = 0,
311     WINED3DSRO_FOG = 1,
312     WINED3DSRO_POINT_SIZE = 2,
313 } WINED3DVS_RASTOUT_OFFSETS;
314
315 #define WINED3DSP_NOSWIZZLE (0 | (1 << 2) | (2 << 4) | (3 << 6))
316
317 typedef enum _WINED3DSHADER_PARAM_SRCMOD_TYPE
318 {
319     WINED3DSPSM_NONE = 0,
320     WINED3DSPSM_NEG = 1,
321     WINED3DSPSM_BIAS = 2,
322     WINED3DSPSM_BIASNEG = 3,
323     WINED3DSPSM_SIGN = 4,
324     WINED3DSPSM_SIGNNEG = 5,
325     WINED3DSPSM_COMP = 6,
326     WINED3DSPSM_X2 = 7,
327     WINED3DSPSM_X2NEG = 8,
328     WINED3DSPSM_DZ = 9,
329     WINED3DSPSM_DW = 10,
330     WINED3DSPSM_ABS = 11,
331     WINED3DSPSM_ABSNEG = 12,
332     WINED3DSPSM_NOT = 13,
333 } WINED3DSHADER_PARAM_SRCMOD_TYPE;
334
335 #define WINED3DSP_WRITEMASK_0   0x1 /* .x r */
336 #define WINED3DSP_WRITEMASK_1   0x2 /* .y g */
337 #define WINED3DSP_WRITEMASK_2   0x4 /* .z b */
338 #define WINED3DSP_WRITEMASK_3   0x8 /* .w a */
339 #define WINED3DSP_WRITEMASK_ALL 0xf /* all */
340
341 typedef enum _WINED3DSHADER_PARAM_DSTMOD_TYPE
342 {
343     WINED3DSPDM_NONE = 0,
344     WINED3DSPDM_SATURATE = 1,
345     WINED3DSPDM_PARTIALPRECISION = 2,
346     WINED3DSPDM_MSAMPCENTROID = 4,
347 } WINED3DSHADER_PARAM_DSTMOD_TYPE;
348
349 typedef enum _WINED3DSHADER_INSTRUCTION_OPCODE_TYPE
350 {
351     WINED3DSIO_NOP = 0,
352     WINED3DSIO_MOV = 1,
353     WINED3DSIO_ADD = 2,
354     WINED3DSIO_SUB = 3,
355     WINED3DSIO_MAD = 4,
356     WINED3DSIO_MUL = 5,
357     WINED3DSIO_RCP = 6,
358     WINED3DSIO_RSQ = 7,
359     WINED3DSIO_DP3 = 8,
360     WINED3DSIO_DP4 = 9,
361     WINED3DSIO_MIN = 10,
362     WINED3DSIO_MAX = 11,
363     WINED3DSIO_SLT = 12,
364     WINED3DSIO_SGE = 13,
365     WINED3DSIO_EXP = 14,
366     WINED3DSIO_LOG = 15,
367     WINED3DSIO_LIT = 16,
368     WINED3DSIO_DST = 17,
369     WINED3DSIO_LRP = 18,
370     WINED3DSIO_FRC = 19,
371     WINED3DSIO_M4x4 = 20,
372     WINED3DSIO_M4x3 = 21,
373     WINED3DSIO_M3x4 = 22,
374     WINED3DSIO_M3x3 = 23,
375     WINED3DSIO_M3x2 = 24,
376     WINED3DSIO_CALL = 25,
377     WINED3DSIO_CALLNZ = 26,
378     WINED3DSIO_LOOP = 27,
379     WINED3DSIO_RET = 28,
380     WINED3DSIO_ENDLOOP = 29,
381     WINED3DSIO_LABEL = 30,
382     WINED3DSIO_DCL = 31,
383     WINED3DSIO_POW = 32,
384     WINED3DSIO_CRS = 33,
385     WINED3DSIO_SGN = 34,
386     WINED3DSIO_ABS = 35,
387     WINED3DSIO_NRM = 36,
388     WINED3DSIO_SINCOS = 37,
389     WINED3DSIO_REP = 38,
390     WINED3DSIO_ENDREP = 39,
391     WINED3DSIO_IF = 40,
392     WINED3DSIO_IFC = 41,
393     WINED3DSIO_ELSE = 42,
394     WINED3DSIO_ENDIF = 43,
395     WINED3DSIO_BREAK = 44,
396     WINED3DSIO_BREAKC = 45,
397     WINED3DSIO_MOVA = 46,
398     WINED3DSIO_DEFB = 47,
399     WINED3DSIO_DEFI = 48,
400
401     WINED3DSIO_TEXCOORD = 64,
402     WINED3DSIO_TEXKILL = 65,
403     WINED3DSIO_TEX = 66,
404     WINED3DSIO_TEXBEM = 67,
405     WINED3DSIO_TEXBEML = 68,
406     WINED3DSIO_TEXREG2AR = 69,
407     WINED3DSIO_TEXREG2GB = 70,
408     WINED3DSIO_TEXM3x2PAD = 71,
409     WINED3DSIO_TEXM3x2TEX = 72,
410     WINED3DSIO_TEXM3x3PAD = 73,
411     WINED3DSIO_TEXM3x3TEX = 74,
412     WINED3DSIO_TEXM3x3DIFF = 75,
413     WINED3DSIO_TEXM3x3SPEC = 76,
414     WINED3DSIO_TEXM3x3VSPEC = 77,
415     WINED3DSIO_EXPP = 78,
416     WINED3DSIO_LOGP = 79,
417     WINED3DSIO_CND = 80,
418     WINED3DSIO_DEF = 81,
419     WINED3DSIO_TEXREG2RGB = 82,
420     WINED3DSIO_TEXDP3TEX = 83,
421     WINED3DSIO_TEXM3x2DEPTH = 84,
422     WINED3DSIO_TEXDP3 = 85,
423     WINED3DSIO_TEXM3x3 = 86,
424     WINED3DSIO_TEXDEPTH = 87,
425     WINED3DSIO_CMP = 88,
426     WINED3DSIO_BEM = 89,
427     WINED3DSIO_DP2ADD = 90,
428     WINED3DSIO_DSX = 91,
429     WINED3DSIO_DSY = 92,
430     WINED3DSIO_TEXLDD = 93,
431     WINED3DSIO_SETP = 94,
432     WINED3DSIO_TEXLDL = 95,
433     WINED3DSIO_BREAKP = 96,
434
435     WINED3DSIO_PHASE = 0xfffd,
436     WINED3DSIO_COMMENT = 0xfffe,
437     WINED3DSIO_END = 0Xffff,
438 } WINED3DSHADER_INSTRUCTION_OPCODE_TYPE;
439
440 /* Undocumented opcode control to identify projective texture lookups in ps 2.0 and later */
441 #define WINED3DSI_TEXLD_PROJECT 1
442 #define WINED3DSI_TEXLD_BIAS    2
443
444 typedef enum COMPARISON_TYPE
445 {
446     COMPARISON_GT = 1,
447     COMPARISON_EQ = 2,
448     COMPARISON_GE = 3,
449     COMPARISON_LT = 4,
450     COMPARISON_NE = 5,
451     COMPARISON_LE = 6,
452 } COMPARISON_TYPE;
453
454 #define WINED3D_SM1_VS  0xfffe
455 #define WINED3D_SM1_PS  0xffff
456 #define WINED3D_SM4_PS  0x0000
457 #define WINED3D_SM4_VS  0x0001
458 #define WINED3D_SM4_GS  0x0002
459
460 /* Shader version tokens, and shader end tokens */
461 #define WINED3DPS_VERSION(major, minor) ((WINED3D_SM1_PS << 16) | ((major) << 8) | (minor))
462 #define WINED3DVS_VERSION(major, minor) ((WINED3D_SM1_VS << 16) | ((major) << 8) | (minor))
463
464 /* Shader backends */
465
466 /* TODO: Make this dynamic, based on shader limits ? */
467 #define MAX_ATTRIBS 16
468 #define MAX_REG_ADDR 1
469 #define MAX_REG_TEMP 32
470 #define MAX_REG_TEXCRD 8
471 #define MAX_REG_INPUT 12
472 #define MAX_REG_OUTPUT 12
473 #define MAX_CONST_I 16
474 #define MAX_CONST_B 16
475
476 /* FIXME: This needs to go up to 2048 for
477  * Shader model 3 according to msdn (and for software shaders) */
478 #define MAX_LABELS 16
479
480 #define SHADER_PGMSIZE 65535
481 typedef struct SHADER_BUFFER {
482     char* buffer;
483     unsigned int bsize;
484     unsigned int lineNo;
485     BOOL newline;
486 } SHADER_BUFFER;
487
488 enum WINED3D_SHADER_INSTRUCTION_HANDLER
489 {
490     WINED3DSIH_ABS,
491     WINED3DSIH_ADD,
492     WINED3DSIH_BEM,
493     WINED3DSIH_BREAK,
494     WINED3DSIH_BREAKC,
495     WINED3DSIH_BREAKP,
496     WINED3DSIH_CALL,
497     WINED3DSIH_CALLNZ,
498     WINED3DSIH_CMP,
499     WINED3DSIH_CND,
500     WINED3DSIH_CRS,
501     WINED3DSIH_DCL,
502     WINED3DSIH_DEF,
503     WINED3DSIH_DEFB,
504     WINED3DSIH_DEFI,
505     WINED3DSIH_DP2ADD,
506     WINED3DSIH_DP3,
507     WINED3DSIH_DP4,
508     WINED3DSIH_DST,
509     WINED3DSIH_DSX,
510     WINED3DSIH_DSY,
511     WINED3DSIH_ELSE,
512     WINED3DSIH_ENDIF,
513     WINED3DSIH_ENDLOOP,
514     WINED3DSIH_ENDREP,
515     WINED3DSIH_EXP,
516     WINED3DSIH_EXPP,
517     WINED3DSIH_FRC,
518     WINED3DSIH_IF,
519     WINED3DSIH_IFC,
520     WINED3DSIH_LABEL,
521     WINED3DSIH_LIT,
522     WINED3DSIH_LOG,
523     WINED3DSIH_LOGP,
524     WINED3DSIH_LOOP,
525     WINED3DSIH_LRP,
526     WINED3DSIH_M3x2,
527     WINED3DSIH_M3x3,
528     WINED3DSIH_M3x4,
529     WINED3DSIH_M4x3,
530     WINED3DSIH_M4x4,
531     WINED3DSIH_MAD,
532     WINED3DSIH_MAX,
533     WINED3DSIH_MIN,
534     WINED3DSIH_MOV,
535     WINED3DSIH_MOVA,
536     WINED3DSIH_MUL,
537     WINED3DSIH_NOP,
538     WINED3DSIH_NRM,
539     WINED3DSIH_PHASE,
540     WINED3DSIH_POW,
541     WINED3DSIH_RCP,
542     WINED3DSIH_REP,
543     WINED3DSIH_RET,
544     WINED3DSIH_RSQ,
545     WINED3DSIH_SETP,
546     WINED3DSIH_SGE,
547     WINED3DSIH_SGN,
548     WINED3DSIH_SINCOS,
549     WINED3DSIH_SLT,
550     WINED3DSIH_SUB,
551     WINED3DSIH_TEX,
552     WINED3DSIH_TEXBEM,
553     WINED3DSIH_TEXBEML,
554     WINED3DSIH_TEXCOORD,
555     WINED3DSIH_TEXDEPTH,
556     WINED3DSIH_TEXDP3,
557     WINED3DSIH_TEXDP3TEX,
558     WINED3DSIH_TEXKILL,
559     WINED3DSIH_TEXLDD,
560     WINED3DSIH_TEXLDL,
561     WINED3DSIH_TEXM3x2DEPTH,
562     WINED3DSIH_TEXM3x2PAD,
563     WINED3DSIH_TEXM3x2TEX,
564     WINED3DSIH_TEXM3x3,
565     WINED3DSIH_TEXM3x3DIFF,
566     WINED3DSIH_TEXM3x3PAD,
567     WINED3DSIH_TEXM3x3SPEC,
568     WINED3DSIH_TEXM3x3TEX,
569     WINED3DSIH_TEXM3x3VSPEC,
570     WINED3DSIH_TEXREG2AR,
571     WINED3DSIH_TEXREG2GB,
572     WINED3DSIH_TEXREG2RGB,
573     WINED3DSIH_TABLE_SIZE
574 };
575
576 enum wined3d_shader_type
577 {
578     WINED3D_SHADER_TYPE_PIXEL,
579     WINED3D_SHADER_TYPE_VERTEX,
580     WINED3D_SHADER_TYPE_GEOMETRY,
581 };
582
583 struct wined3d_shader_version
584 {
585     enum wined3d_shader_type type;
586     BYTE major;
587     BYTE minor;
588 };
589
590 #define WINED3D_SHADER_VERSION(major, minor) (((major) << 8) | (minor))
591
592 typedef struct shader_reg_maps
593 {
594     struct wined3d_shader_version shader_version;
595     char texcoord[MAX_REG_TEXCRD];          /* pixel < 3.0 */
596     char temporary[MAX_REG_TEMP];           /* pixel, vertex */
597     char address[MAX_REG_ADDR];             /* vertex */
598     char labels[MAX_LABELS];                /* pixel, vertex */
599     DWORD *constf;                          /* pixel, vertex */
600     DWORD texcoord_mask[MAX_REG_TEXCRD];    /* vertex < 3.0 */
601     WORD input_registers;                   /* max(MAX_REG_INPUT, MAX_ATTRIBS), 16 */
602     WORD output_registers;                  /* MAX_REG_OUTPUT, 12 */
603     WORD integer_constants;                 /* MAX_CONST_I, 16 */
604     WORD boolean_constants;                 /* MAX_CONST_B, 16 */
605     WORD local_bool_consts;                 /* MAX_CONST_B, 16 */
606
607     WINED3DSAMPLER_TEXTURE_TYPE sampler_type[max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS)];
608     BOOL bumpmat[MAX_TEXTURES], luminanceparams[MAX_TEXTURES];
609     char usesnrm, vpos, usesdsx, usesdsy, usestexldd, usesmova;
610     char usesrelconstF;
611
612     /* Whether or not loops are used in this shader, and nesting depth */
613     unsigned loop_depth;
614
615     /* Whether or not this shader uses fog */
616     char fog;
617
618 } shader_reg_maps;
619
620 struct wined3d_shader_context
621 {
622     IWineD3DBaseShader *shader;
623     const struct shader_reg_maps *reg_maps;
624     SHADER_BUFFER *buffer;
625     void *backend_data;
626 };
627
628 struct wined3d_shader_register
629 {
630     WINED3DSHADER_PARAM_REGISTER_TYPE type;
631     UINT idx;
632     const struct wined3d_shader_src_param *rel_addr;
633     enum wined3d_immconst_type immconst_type;
634     DWORD immconst_data[4];
635 };
636
637 struct wined3d_shader_dst_param
638 {
639     struct wined3d_shader_register reg;
640     DWORD write_mask;
641     DWORD modifiers;
642     DWORD shift;
643 };
644
645 struct wined3d_shader_src_param
646 {
647     struct wined3d_shader_register reg;
648     DWORD swizzle;
649     DWORD modifiers;
650 };
651
652 struct wined3d_shader_instruction
653 {
654     const struct wined3d_shader_context *ctx;
655     enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx;
656     DWORD flags;
657     BOOL coissue;
658     DWORD predicate;
659     UINT dst_count;
660     const struct wined3d_shader_dst_param *dst;
661     UINT src_count;
662     const struct wined3d_shader_src_param *src;
663 };
664
665 struct wined3d_shader_semantic
666 {
667     WINED3DDECLUSAGE usage;
668     UINT usage_idx;
669     WINED3DSAMPLER_TEXTURE_TYPE sampler_type;
670     struct wined3d_shader_dst_param reg;
671 };
672
673 struct wined3d_shader_attribute
674 {
675     WINED3DDECLUSAGE usage;
676     UINT usage_idx;
677 };
678
679 struct wined3d_shader_frontend
680 {
681     void *(*shader_init)(const DWORD *ptr, const struct wined3d_shader_signature *output_signature);
682     void (*shader_free)(void *data);
683     void (*shader_read_header)(void *data, const DWORD **ptr, struct wined3d_shader_version *shader_version);
684     void (*shader_read_opcode)(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins, UINT *param_size);
685     void (*shader_read_src_param)(void *data, const DWORD **ptr, struct wined3d_shader_src_param *src_param,
686             struct wined3d_shader_src_param *src_rel_addr);
687     void (*shader_read_dst_param)(void *data, const DWORD **ptr, struct wined3d_shader_dst_param *dst_param,
688             struct wined3d_shader_src_param *dst_rel_addr);
689     void (*shader_read_semantic)(const DWORD **ptr, struct wined3d_shader_semantic *semantic);
690     void (*shader_read_comment)(const DWORD **ptr, const char **comment);
691     BOOL (*shader_is_end)(void *data, const DWORD **ptr);
692 };
693
694 extern const struct wined3d_shader_frontend sm1_shader_frontend;
695 extern const struct wined3d_shader_frontend sm4_shader_frontend;
696
697 typedef void (*SHADER_HANDLER)(const struct wined3d_shader_instruction *);
698
699 struct shader_caps {
700     DWORD               VertexShaderVersion;
701     DWORD               MaxVertexShaderConst;
702
703     DWORD               PixelShaderVersion;
704     float               PixelShader1xMaxValue;
705     DWORD               MaxPixelShaderConst;
706
707     WINED3DVSHADERCAPS2_0   VS20Caps;
708     WINED3DPSHADERCAPS2_0   PS20Caps;
709
710     DWORD               MaxVShaderInstructionsExecuted;
711     DWORD               MaxPShaderInstructionsExecuted;
712     DWORD               MaxVertexShader30InstructionSlots;
713     DWORD               MaxPixelShader30InstructionSlots;
714
715     BOOL                VSClipping;
716 };
717
718 enum tex_types
719 {
720     tex_1d       = 0,
721     tex_2d       = 1,
722     tex_3d       = 2,
723     tex_cube     = 3,
724     tex_rect     = 4,
725     tex_type_count = 5,
726 };
727
728 enum vertexprocessing_mode {
729     fixedfunction,
730     vertexshader,
731     pretransformed
732 };
733
734 #define WINED3D_CONST_NUM_UNUSED ~0U
735
736 struct stb_const_desc {
737     unsigned char           texunit;
738     UINT                    const_num;
739 };
740
741 enum fogmode {
742     FOG_OFF,
743     FOG_LINEAR,
744     FOG_EXP,
745     FOG_EXP2
746 };
747
748 /* Stateblock dependent parameters which have to be hardcoded
749  * into the shader code
750  */
751 struct ps_compile_args {
752     struct color_fixup_desc     color_fixup[MAX_FRAGMENT_SAMPLERS];
753     enum vertexprocessing_mode  vp_mode;
754     enum fogmode                fog;
755     /* Projected textures(ps 1.0-1.3) */
756     /* Texture types(2D, Cube, 3D) in ps 1.x */
757     BOOL                        srgb_correction;
758     WORD                        np2_fixup;
759     /* Bitmap for NP2 texcoord fixups (16 samplers max currently).
760        D3D9 has a limit of 16 samplers and the fixup is superfluous
761        in D3D10 (unconditional NP2 support mandatory). */
762 };
763
764 enum fog_src_type {
765     VS_FOG_Z        = 0,
766     VS_FOG_COORD    = 1
767 };
768
769 struct vs_compile_args {
770     WORD                        fog_src;
771     WORD                        swizzle_map;   /* MAX_ATTRIBS, 16 */
772 };
773
774 typedef struct {
775     void (*shader_handle_instruction)(const struct wined3d_shader_instruction *);
776     void (*shader_select)(IWineD3DDevice *iface, BOOL usePS, BOOL useVS);
777     void (*shader_select_depth_blt)(IWineD3DDevice *iface, enum tex_types tex_type);
778     void (*shader_deselect_depth_blt)(IWineD3DDevice *iface);
779     void (*shader_update_float_vertex_constants)(IWineD3DDevice *iface, UINT start, UINT count);
780     void (*shader_update_float_pixel_constants)(IWineD3DDevice *iface, UINT start, UINT count);
781     void (*shader_load_constants)(IWineD3DDevice *iface, char usePS, char useVS);
782     void (*shader_load_np2fixup_constants)(IWineD3DDevice *iface, char usePS, char useVS);
783     void (*shader_destroy)(IWineD3DBaseShader *iface);
784     HRESULT (*shader_alloc_private)(IWineD3DDevice *iface);
785     void (*shader_free_private)(IWineD3DDevice *iface);
786     BOOL (*shader_dirtifyable_constants)(IWineD3DDevice *iface);
787     void (*shader_get_caps)(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *caps);
788     BOOL (*shader_color_fixup_supported)(struct color_fixup_desc fixup);
789     void (*shader_add_instruction_modifiers)(const struct wined3d_shader_instruction *ins);
790 } shader_backend_t;
791
792 extern const shader_backend_t glsl_shader_backend;
793 extern const shader_backend_t arb_program_shader_backend;
794 extern const shader_backend_t none_shader_backend;
795
796 /* X11 locking */
797
798 extern void (* CDECL wine_tsx11_lock_ptr)(void);
799 extern void (* CDECL wine_tsx11_unlock_ptr)(void);
800
801 /* As GLX relies on X, this is needed */
802 extern int num_lock;
803
804 #if 0
805 #define ENTER_GL() ++num_lock; if (num_lock > 1) FIXME("Recursive use of GL lock to: %d\n", num_lock); wine_tsx11_lock_ptr()
806 #define LEAVE_GL() if (num_lock != 1) FIXME("Recursive use of GL lock: %d\n", num_lock); --num_lock; wine_tsx11_unlock_ptr()
807 #else
808 #define ENTER_GL() wine_tsx11_lock_ptr()
809 #define LEAVE_GL() wine_tsx11_unlock_ptr()
810 #endif
811
812 /*****************************************************************************
813  * Defines
814  */
815
816 /* GL related defines */
817 /* ------------------ */
818 #define GL_SUPPORT(ExtName)           (GLINFO_LOCATION.supported[ExtName] != 0)
819 #define GL_LIMITS(ExtName)            (GLINFO_LOCATION.max_##ExtName)
820 #define GL_EXTCALL(FuncName)          (GLINFO_LOCATION.FuncName)
821 #define GL_VEND(_VendName)            (GLINFO_LOCATION.gl_vendor == VENDOR_##_VendName ? TRUE : FALSE)
822
823 #define D3DCOLOR_B_R(dw) (((dw) >> 16) & 0xFF)
824 #define D3DCOLOR_B_G(dw) (((dw) >>  8) & 0xFF)
825 #define D3DCOLOR_B_B(dw) (((dw) >>  0) & 0xFF)
826 #define D3DCOLOR_B_A(dw) (((dw) >> 24) & 0xFF)
827
828 #define D3DCOLOR_R(dw) (((float) (((dw) >> 16) & 0xFF)) / 255.0f)
829 #define D3DCOLOR_G(dw) (((float) (((dw) >>  8) & 0xFF)) / 255.0f)
830 #define D3DCOLOR_B(dw) (((float) (((dw) >>  0) & 0xFF)) / 255.0f)
831 #define D3DCOLOR_A(dw) (((float) (((dw) >> 24) & 0xFF)) / 255.0f)
832
833 #define D3DCOLORTOGLFLOAT4(dw, vec) do { \
834   (vec)[0] = D3DCOLOR_R(dw); \
835   (vec)[1] = D3DCOLOR_G(dw); \
836   (vec)[2] = D3DCOLOR_B(dw); \
837   (vec)[3] = D3DCOLOR_A(dw); \
838 } while(0)
839
840 /* DirectX Device Limits */
841 /* --------------------- */
842 #define MAX_MIP_LEVELS 32  /* Maximum number of mipmap levels. */
843 #define MAX_STREAMS  16  /* Maximum possible streams - used for fixed size arrays
844                             See MaxStreams in MSDN under GetDeviceCaps */
845 #define HIGHEST_TRANSFORMSTATE WINED3DTS_WORLDMATRIX(255) /* Highest value in WINED3DTRANSFORMSTATETYPE */
846
847 /* Checking of API calls */
848 /* --------------------- */
849 #ifndef WINE_NO_DEBUG_MSGS
850 #define checkGLcall(A)                                          \
851 do {                                                            \
852     GLint err = glGetError();                                   \
853     if (err == GL_NO_ERROR) {                                   \
854        TRACE("%s call ok %s / %d\n", A, __FILE__, __LINE__);    \
855                                                                 \
856     } else do {                                                 \
857         FIXME(">>>>>>>>>>>>>>>>> %s (%#x) from %s @ %s / %d\n", \
858             debug_glerror(err), err, A, __FILE__, __LINE__);    \
859        err = glGetError();                                      \
860     } while (err != GL_NO_ERROR);                               \
861 } while(0)
862 #else
863 #define checkGLcall(A) do {} while(0)
864 #endif
865
866 /* Trace routines / diagnostics */
867 /* ---------------------------- */
868
869 /* Dump out a matrix and copy it */
870 #define conv_mat(mat,gl_mat)                                                                \
871 do {                                                                                        \
872     TRACE("%f %f %f %f\n", (mat)->u.s._11, (mat)->u.s._12, (mat)->u.s._13, (mat)->u.s._14); \
873     TRACE("%f %f %f %f\n", (mat)->u.s._21, (mat)->u.s._22, (mat)->u.s._23, (mat)->u.s._24); \
874     TRACE("%f %f %f %f\n", (mat)->u.s._31, (mat)->u.s._32, (mat)->u.s._33, (mat)->u.s._34); \
875     TRACE("%f %f %f %f\n", (mat)->u.s._41, (mat)->u.s._42, (mat)->u.s._43, (mat)->u.s._44); \
876     memcpy(gl_mat, (mat), 16 * sizeof(float));                                              \
877 } while (0)
878
879 /* Macro to dump out the current state of the light chain */
880 #define DUMP_LIGHT_CHAIN()                    \
881 do {                                          \
882   PLIGHTINFOEL *el = This->stateBlock->lights;\
883   while (el) {                                \
884     TRACE("Light %p (glIndex %ld, d3dIndex %ld, enabled %d)\n", el, el->glIndex, el->OriginalIndex, el->lightEnabled);\
885     el = el->next;                            \
886   }                                           \
887 } while(0)
888
889 /* Trace vector and strided data information */
890 #define TRACE_VECTOR(name) TRACE( #name "=(%f, %f, %f, %f)\n", name.x, name.y, name.z, name.w);
891 #define TRACE_STRIDED(si, name) TRACE( #name "=(data:%p, stride:%d, format:%#x, vbo %d, stream %u)\n", \
892         si->elements[name].data, si->elements[name].stride, si->elements[name].format_desc->format, \
893         si->elements[name].buffer_object, si->elements[name].stream_idx);
894
895 /* Defines used for optimizations */
896
897 /*    Only reapply what is necessary */
898 #define REAPPLY_ALPHAOP  0x0001
899 #define REAPPLY_ALL      0xFFFF
900
901 /* Advance declaration of structures to satisfy compiler */
902 typedef struct IWineD3DStateBlockImpl IWineD3DStateBlockImpl;
903 typedef struct IWineD3DSurfaceImpl    IWineD3DSurfaceImpl;
904 typedef struct IWineD3DPaletteImpl    IWineD3DPaletteImpl;
905 typedef struct IWineD3DDeviceImpl     IWineD3DDeviceImpl;
906
907 /* Global variables */
908 extern const float identity[16];
909
910 /*****************************************************************************
911  * Compilable extra diagnostics
912  */
913
914 /* Trace information per-vertex: (extremely high amount of trace) */
915 #if 0 /* NOTE: Must be 0 in cvs */
916 # define VTRACE(A) TRACE A
917 #else 
918 # define VTRACE(A) 
919 #endif
920
921 /* TODO: Confirm each of these works when wined3d move completed */
922 #if 0 /* NOTE: Must be 0 in cvs */
923   /* To avoid having to get gigabytes of trace, the following can be compiled in, and at the start
924      of each frame, a check is made for the existence of C:\D3DTRACE, and if it exists d3d trace
925      is enabled, and if it doesn't exist it is disabled. */
926 # define FRAME_DEBUGGING
927   /*  Adding in the SINGLE_FRAME_DEBUGGING gives a trace of just what makes up a single frame, before
928       the file is deleted                                                                            */
929 # if 1 /* NOTE: Must be 1 in cvs, as this is mostly more useful than a trace from program start */
930 #  define SINGLE_FRAME_DEBUGGING
931 # endif  
932   /* The following, when enabled, lets you see the makeup of the frame, by drawprimitive calls.
933      It can only be enabled when FRAME_DEBUGGING is also enabled                               
934      The contents of the back buffer are written into /tmp/backbuffer_* after each primitive 
935      array is drawn.                                                                            */
936 # if 0 /* NOTE: Must be 0 in cvs, as this give a lot of ppm files when compiled in */                                                                                       
937 #  define SHOW_FRAME_MAKEUP 1
938 # endif  
939   /* The following, when enabled, lets you see the makeup of the all the textures used during each
940      of the drawprimitive calls. It can only be enabled when SHOW_FRAME_MAKEUP is also enabled.
941      The contents of the textures assigned to each stage are written into 
942      /tmp/texture_*_<Stage>.ppm after each primitive array is drawn.                            */
943 # if 0 /* NOTE: Must be 0 in cvs, as this give a lot of ppm files when compiled in */
944 #  define SHOW_TEXTURE_MAKEUP 0
945 # endif  
946 extern BOOL isOn;
947 extern BOOL isDumpingFrames;
948 extern LONG primCounter;
949 #endif
950
951 enum wined3d_ffp_idx
952 {
953     WINED3D_FFP_POSITION = 0,
954     WINED3D_FFP_BLENDWEIGHT = 1,
955     WINED3D_FFP_BLENDINDICES = 2,
956     WINED3D_FFP_NORMAL = 3,
957     WINED3D_FFP_PSIZE = 4,
958     WINED3D_FFP_DIFFUSE = 5,
959     WINED3D_FFP_SPECULAR = 6,
960     WINED3D_FFP_TEXCOORD0 = 7,
961     WINED3D_FFP_TEXCOORD1 = 8,
962     WINED3D_FFP_TEXCOORD2 = 9,
963     WINED3D_FFP_TEXCOORD3 = 10,
964     WINED3D_FFP_TEXCOORD4 = 11,
965     WINED3D_FFP_TEXCOORD5 = 12,
966     WINED3D_FFP_TEXCOORD6 = 13,
967     WINED3D_FFP_TEXCOORD7 = 14,
968 };
969
970 enum wined3d_ffp_emit_idx
971 {
972     WINED3D_FFP_EMIT_FLOAT1 = 0,
973     WINED3D_FFP_EMIT_FLOAT2 = 1,
974     WINED3D_FFP_EMIT_FLOAT3 = 2,
975     WINED3D_FFP_EMIT_FLOAT4 = 3,
976     WINED3D_FFP_EMIT_D3DCOLOR = 4,
977     WINED3D_FFP_EMIT_UBYTE4 = 5,
978     WINED3D_FFP_EMIT_SHORT2 = 6,
979     WINED3D_FFP_EMIT_SHORT4 = 7,
980     WINED3D_FFP_EMIT_UBYTE4N = 8,
981     WINED3D_FFP_EMIT_SHORT2N = 9,
982     WINED3D_FFP_EMIT_SHORT4N = 10,
983     WINED3D_FFP_EMIT_USHORT2N = 11,
984     WINED3D_FFP_EMIT_USHORT4N = 12,
985     WINED3D_FFP_EMIT_UDEC3 = 13,
986     WINED3D_FFP_EMIT_DEC3N = 14,
987     WINED3D_FFP_EMIT_FLOAT16_2 = 15,
988     WINED3D_FFP_EMIT_FLOAT16_4 = 16,
989     WINED3D_FFP_EMIT_COUNT = 17
990 };
991
992 struct wined3d_stream_info_element
993 {
994     const struct GlPixelFormatDesc *format_desc;
995     GLsizei stride;
996     const BYTE *data;
997     UINT stream_idx;
998     GLuint buffer_object;
999 };
1000
1001 struct wined3d_stream_info
1002 {
1003     struct wined3d_stream_info_element elements[MAX_ATTRIBS];
1004     BOOL position_transformed;
1005     WORD swizzle_map; /* MAX_ATTRIBS, 16 */
1006     WORD use_map; /* MAX_ATTRIBS, 16 */
1007 };
1008
1009 /*****************************************************************************
1010  * Prototypes
1011  */
1012
1013 /* Routine common to the draw primitive and draw indexed primitive routines */
1014 void drawPrimitive(IWineD3DDevice *iface, UINT index_count, UINT numberOfVertices,
1015         UINT start_idx, UINT idxBytes, const void *idxData, UINT minIndex);
1016 DWORD get_flexible_vertex_size(DWORD d3dvtVertexType);
1017
1018 typedef void (WINE_GLAPI *glAttribFunc)(const void *data);
1019 typedef void (WINE_GLAPI *glMultiTexCoordFunc)(GLenum unit, const void *data);
1020 extern glAttribFunc position_funcs[WINED3D_FFP_EMIT_COUNT];
1021 extern glAttribFunc diffuse_funcs[WINED3D_FFP_EMIT_COUNT];
1022 extern glAttribFunc specular_func_3ubv;
1023 extern glAttribFunc specular_funcs[WINED3D_FFP_EMIT_COUNT];
1024 extern glAttribFunc normal_funcs[WINED3D_FFP_EMIT_COUNT];
1025 extern glMultiTexCoordFunc multi_texcoord_funcs[WINED3D_FFP_EMIT_COUNT];
1026
1027 #define eps 1e-8
1028
1029 #define GET_TEXCOORD_SIZE_FROM_FVF(d3dvtVertexType, tex_num) \
1030     (((((d3dvtVertexType) >> (16 + (2 * (tex_num)))) + 1) & 0x03) + 1)
1031
1032 /* Routines and structures related to state management */
1033 typedef struct WineD3DContext WineD3DContext;
1034 typedef void (*APPLYSTATEFUNC)(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *ctx);
1035
1036 #define STATE_RENDER(a) (a)
1037 #define STATE_IS_RENDER(a) ((a) >= STATE_RENDER(1) && (a) <= STATE_RENDER(WINEHIGHEST_RENDER_STATE))
1038
1039 #define STATE_TEXTURESTAGE(stage, num) (STATE_RENDER(WINEHIGHEST_RENDER_STATE) + 1 + (stage) * (WINED3D_HIGHEST_TEXTURE_STATE + 1) + (num))
1040 #define STATE_IS_TEXTURESTAGE(a) ((a) >= STATE_TEXTURESTAGE(0, 1) && (a) <= STATE_TEXTURESTAGE(MAX_TEXTURES - 1, WINED3D_HIGHEST_TEXTURE_STATE))
1041
1042 /* + 1 because samplers start with 0 */
1043 #define STATE_SAMPLER(num) (STATE_TEXTURESTAGE(MAX_TEXTURES - 1, WINED3D_HIGHEST_TEXTURE_STATE) + 1 + (num))
1044 #define STATE_IS_SAMPLER(num) ((num) >= STATE_SAMPLER(0) && (num) <= STATE_SAMPLER(MAX_COMBINED_SAMPLERS - 1))
1045
1046 #define STATE_PIXELSHADER (STATE_SAMPLER(MAX_COMBINED_SAMPLERS - 1) + 1)
1047 #define STATE_IS_PIXELSHADER(a) ((a) == STATE_PIXELSHADER)
1048
1049 #define STATE_TRANSFORM(a) (STATE_PIXELSHADER + (a))
1050 #define STATE_IS_TRANSFORM(a) ((a) >= STATE_TRANSFORM(1) && (a) <= STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(255)))
1051
1052 #define STATE_STREAMSRC (STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(255)) + 1)
1053 #define STATE_IS_STREAMSRC(a) ((a) == STATE_STREAMSRC)
1054 #define STATE_INDEXBUFFER (STATE_STREAMSRC + 1)
1055 #define STATE_IS_INDEXBUFFER(a) ((a) == STATE_INDEXBUFFER)
1056
1057 #define STATE_VDECL (STATE_INDEXBUFFER + 1)
1058 #define STATE_IS_VDECL(a) ((a) == STATE_VDECL)
1059
1060 #define STATE_VSHADER (STATE_VDECL + 1)
1061 #define STATE_IS_VSHADER(a) ((a) == STATE_VSHADER)
1062
1063 #define STATE_VIEWPORT (STATE_VSHADER + 1)
1064 #define STATE_IS_VIEWPORT(a) ((a) == STATE_VIEWPORT)
1065
1066 #define STATE_VERTEXSHADERCONSTANT (STATE_VIEWPORT + 1)
1067 #define STATE_PIXELSHADERCONSTANT (STATE_VERTEXSHADERCONSTANT + 1)
1068 #define STATE_IS_VERTEXSHADERCONSTANT(a) ((a) == STATE_VERTEXSHADERCONSTANT)
1069 #define STATE_IS_PIXELSHADERCONSTANT(a) ((a) == STATE_PIXELSHADERCONSTANT)
1070
1071 #define STATE_ACTIVELIGHT(a) (STATE_PIXELSHADERCONSTANT + (a) + 1)
1072 #define STATE_IS_ACTIVELIGHT(a) ((a) >= STATE_ACTIVELIGHT(0) && (a) < STATE_ACTIVELIGHT(MAX_ACTIVE_LIGHTS))
1073
1074 #define STATE_SCISSORRECT (STATE_ACTIVELIGHT(MAX_ACTIVE_LIGHTS - 1) + 1)
1075 #define STATE_IS_SCISSORRECT(a) ((a) == STATE_SCISSORRECT)
1076
1077 #define STATE_CLIPPLANE(a) (STATE_SCISSORRECT + 1 + (a))
1078 #define STATE_IS_CLIPPLANE(a) ((a) >= STATE_CLIPPLANE(0) && (a) <= STATE_CLIPPLANE(MAX_CLIPPLANES - 1))
1079
1080 #define STATE_MATERIAL (STATE_CLIPPLANE(MAX_CLIPPLANES))
1081
1082 #define STATE_FRONTFACE (STATE_MATERIAL + 1)
1083
1084 #define STATE_HIGHEST (STATE_FRONTFACE)
1085
1086 struct StateEntry
1087 {
1088     DWORD               representative;
1089     APPLYSTATEFUNC      apply;
1090 };
1091
1092 struct StateEntryTemplate
1093 {
1094     DWORD               state;
1095     struct StateEntry   content;
1096     GL_SupportedExt     extension;
1097 };
1098
1099 struct fragment_caps {
1100     DWORD               PrimitiveMiscCaps;
1101
1102     DWORD               TextureOpCaps;
1103     DWORD               MaxTextureBlendStages;
1104     DWORD               MaxSimultaneousTextures;
1105 };
1106
1107 struct fragment_pipeline {
1108     void (*enable_extension)(IWineD3DDevice *iface, BOOL enable);
1109     void (*get_caps)(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct fragment_caps *caps);
1110     HRESULT (*alloc_private)(IWineD3DDevice *iface);
1111     void (*free_private)(IWineD3DDevice *iface);
1112     BOOL (*color_fixup_supported)(struct color_fixup_desc fixup);
1113     const struct StateEntryTemplate *states;
1114     BOOL ffp_proj_control;
1115 };
1116
1117 extern const struct StateEntryTemplate misc_state_template[];
1118 extern const struct StateEntryTemplate ffp_vertexstate_template[];
1119 extern const struct fragment_pipeline ffp_fragment_pipeline;
1120 extern const struct fragment_pipeline atifs_fragment_pipeline;
1121 extern const struct fragment_pipeline arbfp_fragment_pipeline;
1122 extern const struct fragment_pipeline nvts_fragment_pipeline;
1123 extern const struct fragment_pipeline nvrc_fragment_pipeline;
1124
1125 /* "Base" state table */
1126 HRESULT compile_state_table(struct StateEntry *StateTable, APPLYSTATEFUNC **dev_multistate_funcs,
1127         const WineD3D_GL_Info *gl_info, const struct StateEntryTemplate *vertex,
1128         const struct fragment_pipeline *fragment, const struct StateEntryTemplate *misc);
1129
1130 /* Shaders for color conversions in blits */
1131 struct blit_shader {
1132     HRESULT (*alloc_private)(IWineD3DDevice *iface);
1133     void (*free_private)(IWineD3DDevice *iface);
1134     HRESULT (*set_shader)(IWineD3DDevice *iface, const struct GlPixelFormatDesc *format_desc,
1135             GLenum textype, UINT width, UINT height);
1136     void (*unset_shader)(IWineD3DDevice *iface);
1137     BOOL (*color_fixup_supported)(struct color_fixup_desc fixup);
1138 };
1139
1140 extern const struct blit_shader ffp_blit;
1141 extern const struct blit_shader arbfp_blit;
1142
1143 enum fogsource {
1144     FOGSOURCE_FFP,
1145     FOGSOURCE_VS,
1146     FOGSOURCE_COORD,
1147 };
1148
1149 #define WINED3D_MAX_FBO_ENTRIES 64
1150
1151 /* The new context manager that should deal with onscreen and offscreen rendering */
1152 struct WineD3DContext {
1153     /* State dirtification
1154      * dirtyArray is an array that contains markers for dirty states. numDirtyEntries states are dirty, their numbers are in indices
1155      * 0...numDirtyEntries - 1. isStateDirty is a redundant copy of the dirtyArray. Technically only one of them would be needed,
1156      * 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
1157      * only numDirtyEntries array elements have to be checked, not STATE_HIGHEST states.
1158      */
1159     DWORD                   dirtyArray[STATE_HIGHEST + 1]; /* Won't get bigger than that, a state is never marked dirty 2 times */
1160     DWORD                   numDirtyEntries;
1161     DWORD                   isStateDirty[STATE_HIGHEST/32 + 1]; /* Bitmap to find out quickly if a state is dirty */
1162
1163     IWineD3DSurface         *surface;
1164     DWORD                   tid;    /* Thread ID which owns this context at the moment */
1165
1166     /* Stores some information about the context state for optimization */
1167     WORD draw_buffer_dirty : 1;
1168     WORD last_was_rhw : 1;              /* true iff last draw_primitive was in xyzrhw mode */
1169     WORD last_was_pshader : 1;
1170     WORD last_was_vshader : 1;
1171     WORD namedArraysLoaded : 1;
1172     WORD numberedArraysLoaded : 1;
1173     WORD last_was_blit : 1;
1174     WORD last_was_ckey : 1;
1175     WORD fog_coord : 1;
1176     WORD isPBuffer : 1;
1177     WORD fog_enabled : 1;
1178     WORD num_untracked_materials : 2;   /* Max value 2 */
1179     WORD padding : 3;
1180     BYTE texShaderBumpMap;              /* MAX_TEXTURES, 8 */
1181     BYTE lastWasPow2Texture;            /* MAX_TEXTURES, 8 */
1182     DWORD                   numbered_array_mask;
1183     GLenum                  tracking_parm;     /* Which source is tracking current colour         */
1184     GLenum                  untracked_materials[2];
1185     UINT                    blit_w, blit_h;
1186     enum fogsource          fog_source;
1187
1188     char                    *vshader_const_dirty, *pshader_const_dirty;
1189
1190     /* The actual opengl context */
1191     HGLRC                   glCtx;
1192     HWND                    win_handle;
1193     HDC                     hdc;
1194     HPBUFFERARB             pbuffer;
1195     GLint                   aux_buffers;
1196
1197     /* FBOs */
1198     UINT                    fbo_entry_count;
1199     struct list             fbo_list;
1200     struct fbo_entry        *current_fbo;
1201     GLuint                  src_fbo;
1202     GLuint                  dst_fbo;
1203
1204     /* Extension emulation */
1205     GLint                   gl_fog_source;
1206     GLfloat                 fog_coord_value;
1207     GLfloat                 color[4], fogstart, fogend, fogcolor[4];
1208     GLuint                  dummy_arbfp_prog;
1209 };
1210
1211 typedef enum ContextUsage {
1212     CTXUSAGE_RESOURCELOAD       = 1,    /* Only loads textures: No State is applied */
1213     CTXUSAGE_DRAWPRIM           = 2,    /* OpenGL states are set up for blitting DirectDraw surfaces */
1214     CTXUSAGE_BLIT               = 3,    /* OpenGL states are set up 3D drawing */
1215     CTXUSAGE_CLEAR              = 4,    /* Drawable and states are set up for clearing */
1216 } ContextUsage;
1217
1218 void ActivateContext(IWineD3DDeviceImpl *device, IWineD3DSurface *target, ContextUsage usage);
1219 WineD3DContext *getActiveContext(void);
1220 WineD3DContext *CreateContext(IWineD3DDeviceImpl *This, IWineD3DSurfaceImpl *target, HWND win, BOOL create_pbuffer, const WINED3DPRESENT_PARAMETERS *pPresentParms);
1221 void DestroyContext(IWineD3DDeviceImpl *This, WineD3DContext *context);
1222 void context_resource_released(IWineD3DDevice *iface, IWineD3DResource *resource, WINED3DRESOURCETYPE type);
1223 void context_bind_fbo(IWineD3DDevice *iface, GLenum target, GLuint *fbo);
1224 void context_attach_depth_stencil_fbo(IWineD3DDeviceImpl *This, GLenum fbo_target, IWineD3DSurface *depth_stencil, BOOL use_render_buffer);
1225 void context_attach_surface_fbo(IWineD3DDeviceImpl *This, GLenum fbo_target, DWORD idx, IWineD3DSurface *surface);
1226
1227 void delete_opengl_contexts(IWineD3DDevice *iface, IWineD3DSwapChain *swapchain);
1228 HRESULT create_primary_opengl_context(IWineD3DDevice *iface, IWineD3DSwapChain *swapchain);
1229
1230 /* Macros for doing basic GPU detection based on opengl capabilities */
1231 #define WINE_D3D6_CAPABLE(gl_info) (gl_info->supported[ARB_MULTITEXTURE])
1232 #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])
1233 #define WINE_D3D8_CAPABLE(gl_info) WINE_D3D7_CAPABLE(gl_info) && (gl_info->supported[ARB_MULTISAMPLE] && gl_info->supported[ARB_TEXTURE_BORDER_CLAMP])
1234 #define WINE_D3D9_CAPABLE(gl_info) WINE_D3D8_CAPABLE(gl_info) && (gl_info->supported[ARB_FRAGMENT_PROGRAM] && gl_info->supported[ARB_VERTEX_SHADER])
1235
1236 /* Default callbacks for implicit object destruction */
1237 extern ULONG WINAPI D3DCB_DefaultDestroySurface(IWineD3DSurface *pSurface);
1238
1239 extern ULONG WINAPI D3DCB_DefaultDestroyVolume(IWineD3DVolume *pSurface);
1240
1241 /*****************************************************************************
1242  * Internal representation of a light
1243  */
1244 typedef struct PLIGHTINFOEL PLIGHTINFOEL;
1245 struct PLIGHTINFOEL {
1246     WINED3DLIGHT OriginalParms; /* Note D3D8LIGHT == D3D9LIGHT */
1247     DWORD        OriginalIndex;
1248     LONG         glIndex;
1249     BOOL         changed;
1250     BOOL         enabledChanged;
1251     BOOL         enabled;
1252
1253     /* Converted parms to speed up swapping lights */
1254     float                         lightPosn[4];
1255     float                         lightDirn[4];
1256     float                         exponent;
1257     float                         cutoff;
1258
1259     struct list entry;
1260 };
1261
1262 /* The default light parameters */
1263 extern const WINED3DLIGHT WINED3D_default_light;
1264
1265 typedef struct WineD3D_PixelFormat
1266 {
1267     int iPixelFormat; /* WGL pixel format */
1268     int iPixelType; /* WGL pixel type e.g. WGL_TYPE_RGBA_ARB, WGL_TYPE_RGBA_FLOAT_ARB or WGL_TYPE_COLORINDEX_ARB */
1269     int redSize, greenSize, blueSize, alphaSize;
1270     int depthSize, stencilSize;
1271     BOOL windowDrawable;
1272     BOOL pbufferDrawable;
1273     BOOL doubleBuffer;
1274     int auxBuffers;
1275     int numSamples;
1276 } WineD3D_PixelFormat;
1277
1278 /* The adapter structure */
1279 struct WineD3DAdapter
1280 {
1281     UINT                    num;
1282     BOOL                    opengl;
1283     POINT                   monitorPoint;
1284     WineD3D_GL_Info         gl_info;
1285     const char              *driver;
1286     const char              *description;
1287     WCHAR                   DeviceName[CCHDEVICENAME]; /* DeviceName for use with e.g. ChangeDisplaySettings */
1288     int                     nCfgs;
1289     WineD3D_PixelFormat     *cfgs;
1290     BOOL                    brokenStencil; /* Set on cards which only offer mixed depth+stencil */
1291     unsigned int            TextureRam; /* Amount of texture memory both video ram + AGP/TurboCache/HyperMemory/.. */
1292     unsigned int            UsedTextureRam;
1293 };
1294
1295 extern BOOL initPixelFormats(WineD3D_GL_Info *gl_info);
1296 BOOL initPixelFormatsNoGL(WineD3D_GL_Info *gl_info);
1297 extern long WineD3DAdapterChangeGLRam(IWineD3DDeviceImpl *D3DDevice, long glram);
1298 extern void add_gl_compat_wrappers(WineD3D_GL_Info *gl_info);
1299
1300 /*****************************************************************************
1301  * High order patch management
1302  */
1303 struct WineD3DRectPatch
1304 {
1305     UINT                            Handle;
1306     float                          *mem;
1307     WineDirect3DVertexStridedData   strided;
1308     WINED3DRECTPATCH_INFO           RectPatchInfo;
1309     float                           numSegs[4];
1310     char                            has_normals, has_texcoords;
1311     struct list                     entry;
1312 };
1313
1314 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This, struct WineD3DRectPatch *patch);
1315
1316 enum projection_types
1317 {
1318     proj_none    = 0,
1319     proj_count3  = 1,
1320     proj_count4  = 2
1321 };
1322
1323 enum dst_arg
1324 {
1325     resultreg    = 0,
1326     tempreg      = 1
1327 };
1328
1329 /*****************************************************************************
1330  * Fixed function pipeline replacements
1331  */
1332 #define ARG_UNUSED          0xff
1333 struct texture_stage_op
1334 {
1335     unsigned                cop : 8;
1336     unsigned                carg1 : 8;
1337     unsigned                carg2 : 8;
1338     unsigned                carg0 : 8;
1339
1340     unsigned                aop : 8;
1341     unsigned                aarg1 : 8;
1342     unsigned                aarg2 : 8;
1343     unsigned                aarg0 : 8;
1344
1345     struct color_fixup_desc color_fixup;
1346     unsigned                tex_type : 3;
1347     unsigned                dst : 1;
1348     unsigned                projected : 2;
1349     unsigned                padding : 10;
1350 };
1351
1352 struct ffp_frag_settings {
1353     struct texture_stage_op     op[MAX_TEXTURES];
1354     enum fogmode fog;
1355     /* Use an int instead of a char to get dword alignment */
1356     unsigned int sRGB_write;
1357 };
1358
1359 struct ffp_frag_desc
1360 {
1361     struct wine_rb_entry entry;
1362     struct ffp_frag_settings    settings;
1363 };
1364
1365 extern const struct wine_rb_functions wined3d_ffp_frag_program_rb_functions;
1366
1367 void gen_ffp_frag_op(IWineD3DStateBlockImpl *stateblock, struct ffp_frag_settings *settings, BOOL ignore_textype);
1368 const struct ffp_frag_desc *find_ffp_frag_shader(const struct wine_rb_tree *fragment_shaders,
1369         const struct ffp_frag_settings *settings);
1370 void add_ffp_frag_shader(struct wine_rb_tree *shaders, struct ffp_frag_desc *desc);
1371
1372 /*****************************************************************************
1373  * IWineD3D implementation structure
1374  */
1375 typedef struct IWineD3DImpl
1376 {
1377     /* IUnknown fields */
1378     const IWineD3DVtbl     *lpVtbl;
1379     LONG                    ref;     /* Note: Ref counting not required */
1380
1381     /* WineD3D Information */
1382     IUnknown               *parent;
1383     UINT                    dxVersion;
1384
1385     UINT adapter_count;
1386     struct WineD3DAdapter adapters[1];
1387 } IWineD3DImpl;
1388
1389 extern const IWineD3DVtbl IWineD3D_Vtbl;
1390
1391 BOOL InitAdapters(IWineD3DImpl *This);
1392
1393 /* TODO: setup some flags in the registry to enable, disable pbuffer support
1394 (since it will break quite a few things until contexts are managed properly!) */
1395 extern BOOL pbuffer_support;
1396 /* allocate one pbuffer per surface */
1397 extern BOOL pbuffer_per_surface;
1398
1399 /* A helper function that dumps a resource list */
1400 void dumpResources(struct list *list);
1401
1402 /*****************************************************************************
1403  * IWineD3DDevice implementation structure
1404  */
1405 #define WINED3D_UNMAPPED_STAGE ~0U
1406
1407 /* Multithreaded flag. Removed from the public header to signal that IWineD3D::CreateDevice ignores it */
1408 #define WINED3DCREATE_MULTITHREADED 0x00000004
1409
1410 struct IWineD3DDeviceImpl
1411 {
1412     /* IUnknown fields      */
1413     const IWineD3DDeviceVtbl *lpVtbl;
1414     LONG                    ref;     /* Note: Ref counting not required */
1415
1416     /* WineD3D Information  */
1417     IUnknown               *parent;
1418     IWineD3DDeviceParent   *device_parent;
1419     IWineD3D               *wineD3D;
1420     struct WineD3DAdapter  *adapter;
1421
1422     /* Window styles to restore when switching fullscreen mode */
1423     LONG                    style;
1424     LONG                    exStyle;
1425
1426     /* X and GL Information */
1427     GLint                   maxConcurrentLights;
1428     GLenum                  offscreenBuffer;
1429
1430     /* Selected capabilities */
1431     int vs_selected_mode;
1432     int ps_selected_mode;
1433     const shader_backend_t *shader_backend;
1434     void *shader_priv;
1435     void *fragment_priv;
1436     void *blit_priv;
1437     struct StateEntry StateTable[STATE_HIGHEST + 1];
1438     /* Array of functions for states which are handled by more than one pipeline part */
1439     APPLYSTATEFUNC *multistate_funcs[STATE_HIGHEST + 1];
1440     const struct fragment_pipeline *frag_pipe;
1441     const struct blit_shader *blitter;
1442
1443     unsigned int max_ffp_textures, max_ffp_texture_stages;
1444     DWORD d3d_vshader_constantF, d3d_pshader_constantF; /* Advertised d3d caps, not GL ones */
1445     DWORD vs_clipping;
1446
1447     WORD view_ident : 1;                /* true iff view matrix is identity */
1448     WORD untransformed : 1;
1449     WORD vertexBlendUsed : 1;           /* To avoid needless setting of the blend matrices */
1450     WORD isRecordingState : 1;
1451     WORD isInDraw : 1;
1452     WORD render_offscreen : 1;
1453     WORD bCursorVisible : 1;
1454     WORD haveHardwareCursor : 1;
1455     WORD d3d_initialized : 1;
1456     WORD inScene : 1;                   /* A flag to check for proper BeginScene / EndScene call pairs */
1457     WORD softwareVertexProcessing : 1;  /* process vertex shaders using software or hardware */
1458     WORD useDrawStridedSlow : 1;
1459     WORD instancedDraw : 1;
1460     WORD padding : 3;
1461
1462     BYTE fixed_function_usage_map;      /* MAX_TEXTURES, 8 */
1463
1464 #define DDRAW_PITCH_ALIGNMENT 8
1465 #define D3D8_PITCH_ALIGNMENT 4
1466     unsigned char           surface_alignment; /* Line Alignment of surfaces                      */
1467
1468     /* State block related */
1469     IWineD3DStateBlockImpl *stateBlock;
1470     IWineD3DStateBlockImpl *updateStateBlock;
1471
1472     /* Internal use fields  */
1473     WINED3DDEVICE_CREATION_PARAMETERS createParms;
1474     UINT                            adapterNo;
1475     WINED3DDEVTYPE                  devType;
1476
1477     IWineD3DSwapChain     **swapchains;
1478     UINT                    NumberOfSwapChains;
1479
1480     struct list             resources; /* a linked list to track resources created by the device */
1481     struct list             shaders;   /* a linked list to track shaders (pixel and vertex)      */
1482     unsigned int            highest_dirty_ps_const, highest_dirty_vs_const;
1483
1484     /* Render Target Support */
1485     IWineD3DSurface       **render_targets;
1486     IWineD3DSurface        *auto_depth_stencil_buffer;
1487     IWineD3DSurface        *stencilBufferTarget;
1488
1489     /* Caches to avoid unneeded context changes */
1490     IWineD3DSurface        *lastActiveRenderTarget;
1491     IWineD3DSwapChain      *lastActiveSwapChain;
1492
1493     /* palettes texture management */
1494     UINT                    NumberOfPalettes;
1495     PALETTEENTRY            **palettes;
1496     UINT                    currentPalette;
1497     UINT                    paletteConversionShader;
1498
1499     /* For rendering to a texture using glCopyTexImage */
1500     GLenum                  *draw_buffers;
1501     GLuint                  depth_blt_texture;
1502     GLuint                  depth_blt_rb;
1503     UINT                    depth_blt_rb_w;
1504     UINT                    depth_blt_rb_h;
1505
1506     /* Cursor management */
1507     UINT                    xHotSpot;
1508     UINT                    yHotSpot;
1509     UINT                    xScreenSpace;
1510     UINT                    yScreenSpace;
1511     UINT                    cursorWidth, cursorHeight;
1512     GLuint                  cursorTexture;
1513     HCURSOR                 hardwareCursor;
1514
1515     /* The Wine logo surface */
1516     IWineD3DSurface        *logo_surface;
1517
1518     /* Textures for when no other textures are mapped */
1519     UINT                          dummyTextureName[MAX_TEXTURES];
1520
1521     /* Device state management */
1522     HRESULT                 state;
1523
1524     /* DirectDraw stuff */
1525     DWORD ddraw_width, ddraw_height;
1526     WINED3DFORMAT ddraw_format;
1527
1528     /* Final position fixup constant */
1529     float                       posFixup[4];
1530
1531     /* With register combiners we can skip junk texture stages */
1532     DWORD                     texUnitMap[MAX_COMBINED_SAMPLERS];
1533     DWORD                     rev_tex_unit_map[MAX_COMBINED_SAMPLERS];
1534
1535     /* Stream source management */
1536     struct wined3d_stream_info strided_streams;
1537     const WineDirect3DVertexStridedData *up_strided;
1538
1539     /* Context management */
1540     WineD3DContext          **contexts;                  /* Dynamic array containing pointers to context structures */
1541     WineD3DContext          *activeContext;
1542     DWORD                   lastThread;
1543     UINT                    numContexts;
1544     WineD3DContext          *pbufferContext;             /* The context that has a pbuffer as drawable */
1545     DWORD                   pbufferWidth, pbufferHeight; /* Size of the buffer drawable */
1546
1547     /* High level patch management */
1548 #define PATCHMAP_SIZE 43
1549 #define PATCHMAP_HASHFUNC(x) ((x) % PATCHMAP_SIZE) /* Primitive and simple function */
1550     struct list             patches[PATCHMAP_SIZE];
1551     struct WineD3DRectPatch *currentPatch;
1552 };
1553
1554 extern const IWineD3DDeviceVtbl IWineD3DDevice_Vtbl;
1555
1556 void device_resource_add(IWineD3DDeviceImpl *This, IWineD3DResource *resource);
1557 void device_resource_released(IWineD3DDeviceImpl *This, IWineD3DResource *resource);
1558 void device_stream_info_from_declaration(IWineD3DDeviceImpl *This,
1559         BOOL use_vshader, struct wined3d_stream_info *stream_info, BOOL *fixup);
1560 void device_stream_info_from_strided(IWineD3DDeviceImpl *This,
1561         const struct WineDirect3DVertexStridedData *strided, struct wined3d_stream_info *stream_info);
1562 HRESULT IWineD3DDeviceImpl_ClearSurface(IWineD3DDeviceImpl *This,  IWineD3DSurfaceImpl *target, DWORD Count,
1563                                         CONST WINED3DRECT* pRects, DWORD Flags, WINED3DCOLOR Color,
1564                                         float Z, DWORD Stencil);
1565 void IWineD3DDeviceImpl_FindTexUnitMap(IWineD3DDeviceImpl *This);
1566 void IWineD3DDeviceImpl_MarkStateDirty(IWineD3DDeviceImpl *This, DWORD state);
1567 static inline BOOL isStateDirty(WineD3DContext *context, DWORD state) {
1568     DWORD idx = state >> 5;
1569     BYTE shift = state & 0x1f;
1570     return context->isStateDirty[idx] & (1 << shift);
1571 }
1572
1573 /* Support for IWineD3DResource ::Set/Get/FreePrivateData. */
1574 typedef struct PrivateData
1575 {
1576     struct list entry;
1577
1578     GUID tag;
1579     DWORD flags; /* DDSPD_* */
1580
1581     union
1582     {
1583         LPVOID data;
1584         LPUNKNOWN object;
1585     } ptr;
1586
1587     DWORD size;
1588 } PrivateData;
1589
1590 /*****************************************************************************
1591  * IWineD3DResource implementation structure
1592  */
1593 typedef struct IWineD3DResourceClass
1594 {
1595     /* IUnknown fields */
1596     LONG                    ref;     /* Note: Ref counting not required */
1597
1598     /* WineD3DResource Information */
1599     IUnknown               *parent;
1600     WINED3DRESOURCETYPE     resourceType;
1601     IWineD3DDeviceImpl     *wineD3DDevice;
1602     WINED3DPOOL             pool;
1603     UINT                    size;
1604     DWORD                   usage;
1605     const struct GlPixelFormatDesc *format_desc;
1606     DWORD                   priority;
1607     BYTE                   *allocatedMemory; /* Pointer to the real data location */
1608     BYTE                   *heapMemory; /* Pointer to the HeapAlloced block of memory */
1609     struct list             privateData;
1610     struct list             resource_list_entry;
1611
1612 } IWineD3DResourceClass;
1613
1614 typedef struct IWineD3DResourceImpl
1615 {
1616     /* IUnknown & WineD3DResource Information     */
1617     const IWineD3DResourceVtbl *lpVtbl;
1618     IWineD3DResourceClass   resource;
1619 } IWineD3DResourceImpl;
1620
1621 void resource_cleanup(IWineD3DResource *iface);
1622 HRESULT resource_free_private_data(IWineD3DResource *iface, REFGUID guid);
1623 HRESULT resource_get_device(IWineD3DResource *iface, IWineD3DDevice **device);
1624 HRESULT resource_get_parent(IWineD3DResource *iface, IUnknown **parent);
1625 DWORD resource_get_priority(IWineD3DResource *iface);
1626 HRESULT resource_get_private_data(IWineD3DResource *iface, REFGUID guid,
1627         void *data, DWORD *data_size);
1628 HRESULT resource_init(IWineD3DResource *iface, WINED3DRESOURCETYPE resource_type,
1629         IWineD3DDeviceImpl *device, UINT size, DWORD usage, const struct GlPixelFormatDesc *format_desc,
1630         WINED3DPOOL pool, IUnknown *parent);
1631 WINED3DRESOURCETYPE resource_get_type(IWineD3DResource *iface);
1632 DWORD resource_set_priority(IWineD3DResource *iface, DWORD new_priority);
1633 HRESULT resource_set_private_data(IWineD3DResource *iface, REFGUID guid,
1634         const void *data, DWORD data_size, DWORD flags);
1635
1636 /* Tests show that the start address of resources is 32 byte aligned */
1637 #define RESOURCE_ALIGNMENT 32
1638
1639 /*****************************************************************************
1640  * IWineD3DBaseTexture D3D- > openGL state map lookups
1641  */
1642
1643 typedef enum winetexturestates {
1644     WINED3DTEXSTA_ADDRESSU       = 0,
1645     WINED3DTEXSTA_ADDRESSV       = 1,
1646     WINED3DTEXSTA_ADDRESSW       = 2,
1647     WINED3DTEXSTA_BORDERCOLOR    = 3,
1648     WINED3DTEXSTA_MAGFILTER      = 4,
1649     WINED3DTEXSTA_MINFILTER      = 5,
1650     WINED3DTEXSTA_MIPFILTER      = 6,
1651     WINED3DTEXSTA_MAXMIPLEVEL    = 7,
1652     WINED3DTEXSTA_MAXANISOTROPY  = 8,
1653     WINED3DTEXSTA_SRGBTEXTURE    = 9,
1654     WINED3DTEXSTA_ELEMENTINDEX   = 10,
1655     WINED3DTEXSTA_DMAPOFFSET     = 11,
1656     WINED3DTEXSTA_TSSADDRESSW    = 12,
1657     MAX_WINETEXTURESTATES        = 13,
1658 } winetexturestates;
1659
1660 enum WINED3DSRGB
1661 {
1662     SRGB_ANY                                = 0,    /* Uses the cached value(e.g. external calls) */
1663     SRGB_RGB                                = 1,    /* Loads the rgb texture */
1664     SRGB_SRGB                               = 2,    /* Loads the srgb texture */
1665     SRGB_BOTH                               = 3,    /* Loads both textures */
1666 };
1667
1668 /*****************************************************************************
1669  * IWineD3DBaseTexture implementation structure (extends IWineD3DResourceImpl)
1670  */
1671 typedef struct IWineD3DBaseTextureClass
1672 {
1673     DWORD                   states[MAX_WINETEXTURESTATES];
1674     DWORD                   srgbstates[MAX_WINETEXTURESTATES];
1675     UINT                    levels;
1676     BOOL                    dirty, srgbDirty;
1677     UINT                    textureName, srgbTextureName;
1678     float                   pow2Matrix[16];
1679     UINT                    LOD;
1680     WINED3DTEXTUREFILTERTYPE filterType;
1681     LONG                    bindCount;
1682     DWORD                   sampler;
1683     BOOL                    is_srgb;
1684     BOOL                    pow2Matrix_identity;
1685     const struct min_lookup *minMipLookup;
1686     const GLenum            *magLookup;
1687     void                    (*internal_preload)(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb);
1688 } IWineD3DBaseTextureClass;
1689
1690 void surface_internal_preload(IWineD3DSurface *iface, enum WINED3DSRGB srgb);
1691
1692 typedef struct IWineD3DBaseTextureImpl
1693 {
1694     /* IUnknown & WineD3DResource Information     */
1695     const IWineD3DBaseTextureVtbl *lpVtbl;
1696     IWineD3DResourceClass     resource;
1697     IWineD3DBaseTextureClass  baseTexture;
1698
1699 } IWineD3DBaseTextureImpl;
1700
1701 void basetexture_apply_state_changes(IWineD3DBaseTexture *iface,
1702         const DWORD texture_states[WINED3D_HIGHEST_TEXTURE_STATE + 1],
1703         const DWORD sampler_states[WINED3D_HIGHEST_SAMPLER_STATE + 1]);
1704 HRESULT basetexture_bind(IWineD3DBaseTexture *iface, BOOL srgb, BOOL *set_surface_desc);
1705 void basetexture_cleanup(IWineD3DBaseTexture *iface);
1706 void basetexture_generate_mipmaps(IWineD3DBaseTexture *iface);
1707 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTexture *iface);
1708 BOOL basetexture_get_dirty(IWineD3DBaseTexture *iface);
1709 DWORD basetexture_get_level_count(IWineD3DBaseTexture *iface);
1710 DWORD basetexture_get_lod(IWineD3DBaseTexture *iface);
1711 HRESULT basetexture_init(IWineD3DBaseTextureImpl *texture, UINT levels, WINED3DRESOURCETYPE resource_type,
1712         IWineD3DDeviceImpl *device, UINT size, DWORD usage, const struct GlPixelFormatDesc *format_desc,
1713         WINED3DPOOL pool, IUnknown *parent);
1714 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE filter_type);
1715 BOOL basetexture_set_dirty(IWineD3DBaseTexture *iface, BOOL dirty);
1716 DWORD basetexture_set_lod(IWineD3DBaseTexture *iface, DWORD new_lod);
1717 void basetexture_unload(IWineD3DBaseTexture *iface);
1718 static inline void basetexture_setsrgbcache(IWineD3DBaseTexture *iface, BOOL srgb) {
1719     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
1720     This->baseTexture.is_srgb = srgb;
1721 }
1722
1723 /*****************************************************************************
1724  * IWineD3DTexture implementation structure (extends IWineD3DBaseTextureImpl)
1725  */
1726 typedef struct IWineD3DTextureImpl
1727 {
1728     /* IUnknown & WineD3DResource/WineD3DBaseTexture Information     */
1729     const IWineD3DTextureVtbl *lpVtbl;
1730     IWineD3DResourceClass     resource;
1731     IWineD3DBaseTextureClass  baseTexture;
1732
1733     /* IWineD3DTexture */
1734     IWineD3DSurface          *surfaces[MAX_MIP_LEVELS];
1735     UINT                      target;
1736     BOOL                      cond_np2;
1737
1738 } IWineD3DTextureImpl;
1739
1740 extern const IWineD3DTextureVtbl IWineD3DTexture_Vtbl;
1741
1742 HRESULT texture_init(IWineD3DTextureImpl *texture, UINT width, UINT height, UINT levels,
1743         IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool, IUnknown *parent);
1744
1745 /*****************************************************************************
1746  * IWineD3DCubeTexture implementation structure (extends IWineD3DBaseTextureImpl)
1747  */
1748 typedef struct IWineD3DCubeTextureImpl
1749 {
1750     /* IUnknown & WineD3DResource/WineD3DBaseTexture Information     */
1751     const IWineD3DCubeTextureVtbl *lpVtbl;
1752     IWineD3DResourceClass     resource;
1753     IWineD3DBaseTextureClass  baseTexture;
1754
1755     /* IWineD3DCubeTexture */
1756     IWineD3DSurface          *surfaces[6][MAX_MIP_LEVELS];
1757 } IWineD3DCubeTextureImpl;
1758
1759 extern const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl;
1760
1761 HRESULT cubetexture_init(IWineD3DCubeTextureImpl *texture, UINT edge_length, UINT levels,
1762         IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool, IUnknown *parent);
1763
1764 typedef struct _WINED3DVOLUMET_DESC
1765 {
1766     UINT                    Width;
1767     UINT                    Height;
1768     UINT                    Depth;
1769 } WINED3DVOLUMET_DESC;
1770
1771 /*****************************************************************************
1772  * IWineD3DVolume implementation structure (extends IUnknown)
1773  */
1774 typedef struct IWineD3DVolumeImpl
1775 {
1776     /* IUnknown & WineD3DResource fields */
1777     const IWineD3DVolumeVtbl  *lpVtbl;
1778     IWineD3DResourceClass      resource;
1779
1780     /* WineD3DVolume Information */
1781     WINED3DVOLUMET_DESC      currentDesc;
1782     IWineD3DBase            *container;
1783     BOOL                    lockable;
1784     BOOL                    locked;
1785     WINED3DBOX              lockedBox;
1786     WINED3DBOX              dirtyBox;
1787     BOOL                    dirty;
1788 } IWineD3DVolumeImpl;
1789
1790 extern const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl;
1791
1792 void volume_add_dirty_box(IWineD3DVolume *iface, const WINED3DBOX *dirty_box);
1793
1794 /*****************************************************************************
1795  * IWineD3DVolumeTexture implementation structure (extends IWineD3DBaseTextureImpl)
1796  */
1797 typedef struct IWineD3DVolumeTextureImpl
1798 {
1799     /* IUnknown & WineD3DResource/WineD3DBaseTexture Information     */
1800     const IWineD3DVolumeTextureVtbl *lpVtbl;
1801     IWineD3DResourceClass     resource;
1802     IWineD3DBaseTextureClass  baseTexture;
1803
1804     /* IWineD3DVolumeTexture */
1805     IWineD3DVolume           *volumes[MAX_MIP_LEVELS];
1806 } IWineD3DVolumeTextureImpl;
1807
1808 extern const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl;
1809
1810 HRESULT volumetexture_init(IWineD3DVolumeTextureImpl *texture, UINT width, UINT height, UINT depth, UINT levels,
1811         IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool, IUnknown *parent);
1812
1813 typedef struct _WINED3DSURFACET_DESC
1814 {
1815     WINED3DMULTISAMPLE_TYPE MultiSampleType;
1816     DWORD                   MultiSampleQuality;
1817     UINT                    Width;
1818     UINT                    Height;
1819 } WINED3DSURFACET_DESC;
1820
1821 /*****************************************************************************
1822  * Structure for DIB Surfaces (GetDC and GDI surfaces)
1823  */
1824 typedef struct wineD3DSurface_DIB {
1825     HBITMAP DIBsection;
1826     void* bitmap_data;
1827     UINT bitmap_size;
1828     HGDIOBJ holdbitmap;
1829     BOOL client_memory;
1830 } wineD3DSurface_DIB;
1831
1832 typedef struct {
1833     struct list entry;
1834     GLuint id;
1835     UINT width;
1836     UINT height;
1837 } renderbuffer_entry_t;
1838
1839 struct fbo_entry
1840 {
1841     struct list entry;
1842     IWineD3DSurface **render_targets;
1843     IWineD3DSurface *depth_stencil;
1844     BOOL attached;
1845     GLuint id;
1846 };
1847
1848 /*****************************************************************************
1849  * IWineD3DClipp implementation structure
1850  */
1851 typedef struct IWineD3DClipperImpl
1852 {
1853     const IWineD3DClipperVtbl *lpVtbl;
1854     LONG ref;
1855
1856     IUnknown *Parent;
1857     HWND hWnd;
1858 } IWineD3DClipperImpl;
1859
1860
1861 /*****************************************************************************
1862  * IWineD3DSurface implementation structure
1863  */
1864 struct IWineD3DSurfaceImpl
1865 {
1866     /* IUnknown & IWineD3DResource Information     */
1867     const IWineD3DSurfaceVtbl *lpVtbl;
1868     IWineD3DResourceClass     resource;
1869
1870     /* IWineD3DSurface fields */
1871     IWineD3DBase              *container;
1872     WINED3DSURFACET_DESC      currentDesc;
1873     IWineD3DPaletteImpl       *palette; /* D3D7 style palette handling */
1874     PALETTEENTRY              *palette9; /* D3D8/9 style palette handling */
1875
1876     /* TODO: move this off into a management class(maybe!) */
1877     DWORD                      Flags;
1878
1879     UINT                      pow2Width;
1880     UINT                      pow2Height;
1881
1882     /* A method to retrieve the drawable size. Not in the Vtable to make it changeable */
1883     void (*get_drawable_size)(IWineD3DSurfaceImpl *This, UINT *width, UINT *height);
1884
1885     /* Oversized texture */
1886     RECT                      glRect;
1887
1888     /* PBO */
1889     GLuint                    pbo;
1890
1891     RECT                      lockedRect;
1892     RECT                      dirtyRect;
1893     int                       lockCount;
1894 #define MAXLOCKCOUNT          50 /* After this amount of locks do not free the sysmem copy */
1895
1896     glDescriptor              glDescription;
1897
1898     /* For GetDC */
1899     wineD3DSurface_DIB        dib;
1900     HDC                       hDC;
1901
1902     /* Color keys for DDraw */
1903     WINEDDCOLORKEY            DestBltCKey;
1904     WINEDDCOLORKEY            DestOverlayCKey;
1905     WINEDDCOLORKEY            SrcOverlayCKey;
1906     WINEDDCOLORKEY            SrcBltCKey;
1907     DWORD                     CKeyFlags;
1908
1909     WINEDDCOLORKEY            glCKey;
1910
1911     struct list               renderbuffers;
1912     renderbuffer_entry_t      *current_renderbuffer;
1913
1914     /* DirectDraw clippers */
1915     IWineD3DClipper           *clipper;
1916
1917     /* DirectDraw Overlay handling */
1918     RECT                      overlay_srcrect;
1919     RECT                      overlay_destrect;
1920     IWineD3DSurfaceImpl       *overlay_dest;
1921     struct list               overlays;
1922     struct list               overlay_entry;
1923 };
1924
1925 extern const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl;
1926 extern const IWineD3DSurfaceVtbl IWineGDISurface_Vtbl;
1927
1928 /* Predeclare the shared Surface functions */
1929 HRESULT WINAPI IWineD3DBaseSurfaceImpl_QueryInterface(IWineD3DSurface *iface, REFIID riid, LPVOID *ppobj);
1930 ULONG WINAPI IWineD3DBaseSurfaceImpl_AddRef(IWineD3DSurface *iface);
1931 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetParent(IWineD3DSurface *iface, IUnknown **pParent);
1932 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetDevice(IWineD3DSurface *iface, IWineD3DDevice** ppDevice);
1933 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetPrivateData(IWineD3DSurface *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags);
1934 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetPrivateData(IWineD3DSurface *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData);
1935 HRESULT WINAPI IWineD3DBaseSurfaceImpl_FreePrivateData(IWineD3DSurface *iface, REFGUID refguid);
1936 DWORD   WINAPI IWineD3DBaseSurfaceImpl_SetPriority(IWineD3DSurface *iface, DWORD PriorityNew);
1937 DWORD   WINAPI IWineD3DBaseSurfaceImpl_GetPriority(IWineD3DSurface *iface);
1938 WINED3DRESOURCETYPE WINAPI IWineD3DBaseSurfaceImpl_GetType(IWineD3DSurface *iface);
1939 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetContainer(IWineD3DSurface* iface, REFIID riid, void** ppContainer);
1940 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetDesc(IWineD3DSurface *iface, WINED3DSURFACE_DESC *pDesc);
1941 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetBltStatus(IWineD3DSurface *iface, DWORD Flags);
1942 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetFlipStatus(IWineD3DSurface *iface, DWORD Flags);
1943 HRESULT WINAPI IWineD3DBaseSurfaceImpl_IsLost(IWineD3DSurface *iface);
1944 HRESULT WINAPI IWineD3DBaseSurfaceImpl_Restore(IWineD3DSurface *iface);
1945 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetPalette(IWineD3DSurface *iface, IWineD3DPalette **Pal);
1946 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetPalette(IWineD3DSurface *iface, IWineD3DPalette *Pal);
1947 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetColorKey(IWineD3DSurface *iface, DWORD Flags, const WINEDDCOLORKEY *CKey);
1948 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container);
1949 DWORD WINAPI IWineD3DBaseSurfaceImpl_GetPitch(IWineD3DSurface *iface);
1950 HRESULT WINAPI IWineD3DBaseSurfaceImpl_RealizePalette(IWineD3DSurface *iface);
1951 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetOverlayPosition(IWineD3DSurface *iface, LONG X, LONG Y);
1952 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetOverlayPosition(IWineD3DSurface *iface, LONG *X, LONG *Y);
1953 HRESULT WINAPI IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder(IWineD3DSurface *iface, DWORD Flags, IWineD3DSurface *Ref);
1954 HRESULT WINAPI IWineD3DBaseSurfaceImpl_UpdateOverlay(IWineD3DSurface *iface, const RECT *SrcRect,
1955         IWineD3DSurface *DstSurface, const RECT *DstRect, DWORD Flags, const WINEDDOVERLAYFX *FX);
1956 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetClipper(IWineD3DSurface *iface, IWineD3DClipper *clipper);
1957 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetClipper(IWineD3DSurface *iface, IWineD3DClipper **clipper);
1958 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format);
1959 HRESULT IWineD3DBaseSurfaceImpl_CreateDIBSection(IWineD3DSurface *iface);
1960 HRESULT WINAPI IWineD3DBaseSurfaceImpl_Blt(IWineD3DSurface *iface, const RECT *DestRect, IWineD3DSurface *SrcSurface,
1961         const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter);
1962 HRESULT WINAPI IWineD3DBaseSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty,
1963         IWineD3DSurface *Source, const RECT *rsrc, DWORD trans);
1964 HRESULT WINAPI IWineD3DBaseSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags);
1965 void WINAPI IWineD3DBaseSurfaceImpl_BindTexture(IWineD3DSurface *iface, BOOL srgb);
1966 const void *WINAPI IWineD3DBaseSurfaceImpl_GetData(IWineD3DSurface *iface);
1967
1968 void get_drawable_size_swapchain(IWineD3DSurfaceImpl *This, UINT *width, UINT *height);
1969 void get_drawable_size_backbuffer(IWineD3DSurfaceImpl *This, UINT *width, UINT *height);
1970 void get_drawable_size_pbuffer(IWineD3DSurfaceImpl *This, UINT *width, UINT *height);
1971 void get_drawable_size_fbo(IWineD3DSurfaceImpl *This, UINT *width, UINT *height);
1972
1973 void flip_surface(IWineD3DSurfaceImpl *front, IWineD3DSurfaceImpl *back);
1974
1975 /* Surface flags: */
1976 #define SFLAG_OVERSIZE      0x00000001 /* Surface is bigger than gl size, blts only */
1977 #define SFLAG_CONVERTED     0x00000002 /* Converted for color keying or Palettized */
1978 #define SFLAG_DIBSECTION    0x00000004 /* Has a DIB section attached for GetDC */
1979 #define SFLAG_LOCKABLE      0x00000008 /* Surface can be locked */
1980 #define SFLAG_DISCARD       0x00000010 /* ??? */
1981 #define SFLAG_LOCKED        0x00000020 /* Surface is locked atm */
1982 #define SFLAG_INTEXTURE     0x00000040 /* The GL texture contains the newest surface content */
1983 #define SFLAG_INSRGBTEX     0x00000080 /* The GL srgb texture contains the newest surface content */
1984 #define SFLAG_INDRAWABLE    0x00000100 /* The gl drawable contains the most up to date data */
1985 #define SFLAG_INSYSMEM      0x00000200 /* The system memory copy is most up to date */
1986 #define SFLAG_NONPOW2       0x00000400 /* Surface sizes are not a power of 2 */
1987 #define SFLAG_DYNLOCK       0x00000800 /* Surface is often locked by the app */
1988 #define SFLAG_DCINUSE       0x00001000 /* Set between GetDC and ReleaseDC calls */
1989 #define SFLAG_LOST          0x00002000 /* Surface lost flag for DDraw */
1990 #define SFLAG_USERPTR       0x00004000 /* The application allocated the memory for this surface */
1991 #define SFLAG_GLCKEY        0x00008000 /* The gl texture was created with a color key */
1992 #define SFLAG_CLIENT        0x00010000 /* GL_APPLE_client_storage is used on that texture */
1993 #define SFLAG_ALLOCATED     0x00020000 /* A gl texture is allocated for this surface */
1994 #define SFLAG_SRGBALLOCATED 0x00040000 /* A srgb gl texture is allocated for this surface */
1995 #define SFLAG_PBO           0x00080000 /* Has a PBO attached for speeding up data transfers for dynamically locked surfaces */
1996 #define SFLAG_NORMCOORD     0x00100000 /* Set if the GL texture coords are normalized(non-texture rectangle) */
1997 #define SFLAG_DS_ONSCREEN   0x00200000 /* Is a depth stencil, last modified onscreen */
1998 #define SFLAG_DS_OFFSCREEN  0x00400000 /* Is a depth stencil, last modified offscreen */
1999 #define SFLAG_INOVERLAYDRAW 0x00800000 /* Overlay drawing is in progress. Recursion prevention */
2000 #define SFLAG_SWAPCHAIN     0x01000000 /* The surface is part of a swapchain */
2001
2002 /* In some conditions the surface memory must not be freed:
2003  * SFLAG_OVERSIZE: Not all data can be kept in GL
2004  * SFLAG_CONVERTED: Converting the data back would take too long
2005  * SFLAG_DIBSECTION: The dib code manages the memory
2006  * SFLAG_LOCKED: The app requires access to the surface data
2007  * SFLAG_DYNLOCK: Avoid freeing the data for performance
2008  * SFLAG_PBO: PBOs don't use 'normal' memory. It is either allocated by the driver or must be NULL.
2009  * SFLAG_CLIENT: OpenGL uses our memory as backup
2010  */
2011 #define SFLAG_DONOTFREE     (SFLAG_OVERSIZE   | \
2012                              SFLAG_CONVERTED  | \
2013                              SFLAG_DIBSECTION | \
2014                              SFLAG_LOCKED     | \
2015                              SFLAG_DYNLOCK    | \
2016                              SFLAG_USERPTR    | \
2017                              SFLAG_PBO        | \
2018                              SFLAG_CLIENT)
2019
2020 #define SFLAG_LOCATIONS     (SFLAG_INSYSMEM   | \
2021                              SFLAG_INTEXTURE  | \
2022                              SFLAG_INDRAWABLE | \
2023                              SFLAG_INSRGBTEX)
2024
2025 #define SFLAG_DS_LOCATIONS  (SFLAG_DS_ONSCREEN | \
2026                              SFLAG_DS_OFFSCREEN)
2027 #define SFLAG_DS_DISCARDED   SFLAG_DS_LOCATIONS
2028
2029 BOOL CalculateTexRect(IWineD3DSurfaceImpl *This, RECT *Rect, float glTexCoord[4]);
2030
2031 typedef enum {
2032     NO_CONVERSION,
2033     CONVERT_PALETTED,
2034     CONVERT_PALETTED_CK,
2035     CONVERT_CK_565,
2036     CONVERT_CK_5551,
2037     CONVERT_CK_4444,
2038     CONVERT_CK_4444_ARGB,
2039     CONVERT_CK_1555,
2040     CONVERT_555,
2041     CONVERT_CK_RGB24,
2042     CONVERT_CK_8888,
2043     CONVERT_CK_8888_ARGB,
2044     CONVERT_RGB32_888,
2045     CONVERT_V8U8,
2046     CONVERT_L6V5U5,
2047     CONVERT_X8L8V8U8,
2048     CONVERT_Q8W8V8U8,
2049     CONVERT_V16U16,
2050     CONVERT_A4L4,
2051     CONVERT_G16R16,
2052     CONVERT_R16G16F,
2053     CONVERT_R32G32F,
2054 } CONVERT_TYPES;
2055
2056 HRESULT d3dfmt_get_conv(IWineD3DSurfaceImpl *This, BOOL need_alpha_ck, BOOL use_texturing, GLenum *format, GLenum *internal, GLenum *type, CONVERT_TYPES *convert, int *target_bpp, BOOL srgb_mode);
2057
2058 BOOL palette9_changed(IWineD3DSurfaceImpl *This);
2059
2060 /*****************************************************************************
2061  * IWineD3DVertexDeclaration implementation structure
2062  */
2063 #define MAX_ATTRIBS 16
2064
2065 struct wined3d_vertex_declaration_element
2066 {
2067     const struct GlPixelFormatDesc *format_desc;
2068     BOOL ffp_valid;
2069     WORD input_slot;
2070     WORD offset;
2071     UINT output_slot;
2072     BYTE method;
2073     BYTE usage;
2074     BYTE usage_idx;
2075 };
2076
2077 typedef struct IWineD3DVertexDeclarationImpl {
2078     /* IUnknown  Information */
2079     const IWineD3DVertexDeclarationVtbl *lpVtbl;
2080     LONG                    ref;
2081
2082     IUnknown                *parent;
2083     IWineD3DDeviceImpl      *wineD3DDevice;
2084
2085     struct wined3d_vertex_declaration_element *elements;
2086     UINT element_count;
2087
2088     DWORD                   streams[MAX_STREAMS];
2089     UINT                    num_streams;
2090     BOOL                    position_transformed;
2091     BOOL                    half_float_conv_needed;
2092 } IWineD3DVertexDeclarationImpl;
2093
2094 extern const IWineD3DVertexDeclarationVtbl IWineD3DVertexDeclaration_Vtbl;
2095
2096 HRESULT vertexdeclaration_init(IWineD3DVertexDeclarationImpl *This,
2097         const WINED3DVERTEXELEMENT *elements, UINT element_count);
2098
2099 /*****************************************************************************
2100  * IWineD3DStateBlock implementation structure
2101  */
2102
2103 /* Internal state Block for Begin/End/Capture/Create/Apply info  */
2104 /*   Note: Very long winded but gl Lists are not flexible enough */
2105 /*   to resolve everything we need, so doing it manually for now */
2106 typedef struct SAVEDSTATES {
2107     DWORD transform[(HIGHEST_TRANSFORMSTATE >> 5) + 1];
2108     WORD streamSource;                          /* MAX_STREAMS, 16 */
2109     WORD streamFreq;                            /* MAX_STREAMS, 16 */
2110     DWORD renderState[(WINEHIGHEST_RENDER_STATE >> 5) + 1];
2111     DWORD textureState[MAX_TEXTURES];           /* WINED3D_HIGHEST_TEXTURE_STATE + 1, 18 */
2112     WORD samplerState[MAX_COMBINED_SAMPLERS];   /* WINED3D_HIGHEST_SAMPLER_STATE + 1, 14 */
2113     DWORD textures;                             /* MAX_COMBINED_SAMPLERS, 20 */
2114     DWORD clipplane;                            /* WINED3DMAXUSERCLIPPLANES, 32 */
2115     WORD pixelShaderConstantsB;                 /* MAX_CONST_B, 16 */
2116     WORD pixelShaderConstantsI;                 /* MAX_CONST_I, 16 */
2117     BOOL *pixelShaderConstantsF;
2118     WORD vertexShaderConstantsB;                /* MAX_CONST_B, 16 */
2119     WORD vertexShaderConstantsI;                /* MAX_CONST_I, 16 */
2120     BOOL *vertexShaderConstantsF;
2121     WORD primitive_type : 1;
2122     WORD indices : 1;
2123     WORD material : 1;
2124     WORD viewport : 1;
2125     WORD vertexDecl : 1;
2126     WORD pixelShader : 1;
2127     WORD vertexShader : 1;
2128     WORD scissorRect : 1;
2129     WORD padding : 1;
2130 } SAVEDSTATES;
2131
2132 struct StageState {
2133     DWORD stage;
2134     DWORD state;
2135 };
2136
2137 struct IWineD3DStateBlockImpl
2138 {
2139     /* IUnknown fields */
2140     const IWineD3DStateBlockVtbl *lpVtbl;
2141     LONG                      ref;     /* Note: Ref counting not required */
2142
2143     /* IWineD3DStateBlock information */
2144     IUnknown                 *parent;
2145     IWineD3DDeviceImpl       *wineD3DDevice;
2146     WINED3DSTATEBLOCKTYPE     blockType;
2147
2148     /* Array indicating whether things have been set or changed */
2149     SAVEDSTATES               changed;
2150
2151     /* Vertex Shader Declaration */
2152     IWineD3DVertexDeclaration *vertexDecl;
2153
2154     IWineD3DVertexShader      *vertexShader;
2155
2156     /* Vertex Shader Constants */
2157     BOOL                       vertexShaderConstantB[MAX_CONST_B];
2158     INT                        vertexShaderConstantI[MAX_CONST_I * 4];
2159     float                     *vertexShaderConstantF;
2160
2161     /* primitive type */
2162     GLenum gl_primitive_type;
2163
2164     /* Stream Source */
2165     BOOL                      streamIsUP;
2166     UINT                      streamStride[MAX_STREAMS];
2167     UINT                      streamOffset[MAX_STREAMS + 1 /* tesselated pseudo-stream */ ];
2168     IWineD3DBuffer           *streamSource[MAX_STREAMS];
2169     UINT                      streamFreq[MAX_STREAMS + 1];
2170     UINT                      streamFlags[MAX_STREAMS + 1];     /*0 | WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA  */
2171
2172     /* Indices */
2173     IWineD3DBuffer*           pIndexData;
2174     WINED3DFORMAT             IndexFmt;
2175     INT                       baseVertexIndex;
2176     INT                       loadBaseVertexIndex; /* non-indexed drawing needs 0 here, indexed baseVertexIndex */
2177
2178     /* Transform */
2179     WINED3DMATRIX             transforms[HIGHEST_TRANSFORMSTATE + 1];
2180
2181     /* Light hashmap . Collisions are handled using standard wine double linked lists */
2182 #define LIGHTMAP_SIZE 43 /* Use of a prime number recommended. Set to 1 for a linked list! */
2183 #define LIGHTMAP_HASHFUNC(x) ((x) % LIGHTMAP_SIZE) /* Primitive and simple function */
2184     struct list               lightMap[LIGHTMAP_SIZE]; /* Mashmap containing the lights */
2185     PLIGHTINFOEL             *activeLights[MAX_ACTIVE_LIGHTS]; /* Map of opengl lights to d3d lights */
2186
2187     /* Clipping */
2188     double                    clipplane[MAX_CLIPPLANES][4];
2189     WINED3DCLIPSTATUS         clip_status;
2190
2191     /* ViewPort */
2192     WINED3DVIEWPORT           viewport;
2193
2194     /* Material */
2195     WINED3DMATERIAL           material;
2196
2197     /* Pixel Shader */
2198     IWineD3DPixelShader      *pixelShader;
2199
2200     /* Pixel Shader Constants */
2201     BOOL                       pixelShaderConstantB[MAX_CONST_B];
2202     INT                        pixelShaderConstantI[MAX_CONST_I * 4];
2203     float                     *pixelShaderConstantF;
2204
2205     /* RenderState */
2206     DWORD                     renderState[WINEHIGHEST_RENDER_STATE + 1];
2207
2208     /* Texture */
2209     IWineD3DBaseTexture      *textures[MAX_COMBINED_SAMPLERS];
2210
2211     /* Texture State Stage */
2212     DWORD                     textureState[MAX_TEXTURES][WINED3D_HIGHEST_TEXTURE_STATE + 1];
2213     DWORD                     lowest_disabled_stage;
2214     /* Sampler States */
2215     DWORD                     samplerState[MAX_COMBINED_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1];
2216
2217     /* Scissor test rectangle */
2218     RECT                      scissorRect;
2219
2220     /* Contained state management */
2221     DWORD                     contained_render_states[WINEHIGHEST_RENDER_STATE + 1];
2222     unsigned int              num_contained_render_states;
2223     DWORD                     contained_transform_states[HIGHEST_TRANSFORMSTATE + 1];
2224     unsigned int              num_contained_transform_states;
2225     DWORD                     contained_vs_consts_i[MAX_CONST_I];
2226     unsigned int              num_contained_vs_consts_i;
2227     DWORD                     contained_vs_consts_b[MAX_CONST_B];
2228     unsigned int              num_contained_vs_consts_b;
2229     DWORD                     *contained_vs_consts_f;
2230     unsigned int              num_contained_vs_consts_f;
2231     DWORD                     contained_ps_consts_i[MAX_CONST_I];
2232     unsigned int              num_contained_ps_consts_i;
2233     DWORD                     contained_ps_consts_b[MAX_CONST_B];
2234     unsigned int              num_contained_ps_consts_b;
2235     DWORD                     *contained_ps_consts_f;
2236     unsigned int              num_contained_ps_consts_f;
2237     struct StageState         contained_tss_states[MAX_TEXTURES * (WINED3D_HIGHEST_TEXTURE_STATE + 1)];
2238     unsigned int              num_contained_tss_states;
2239     struct StageState         contained_sampler_states[MAX_COMBINED_SAMPLERS * WINED3D_HIGHEST_SAMPLER_STATE];
2240     unsigned int              num_contained_sampler_states;
2241 };
2242
2243 extern void stateblock_savedstates_set(
2244     IWineD3DStateBlock* iface,
2245     SAVEDSTATES* states,
2246     BOOL value);
2247
2248 extern void stateblock_copy(
2249     IWineD3DStateBlock* destination,
2250     IWineD3DStateBlock* source);
2251
2252 extern const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl;
2253
2254 /* Direct3D terminology with little modifications. We do not have an issued state
2255  * because only the driver knows about it, but we have a created state because d3d
2256  * allows GetData on a created issue, but opengl doesn't
2257  */
2258 enum query_state {
2259     QUERY_CREATED,
2260     QUERY_SIGNALLED,
2261     QUERY_BUILDING
2262 };
2263 /*****************************************************************************
2264  * IWineD3DQueryImpl implementation structure (extends IUnknown)
2265  */
2266 typedef struct IWineD3DQueryImpl
2267 {
2268     const IWineD3DQueryVtbl  *lpVtbl;
2269     LONG                      ref;     /* Note: Ref counting not required */
2270     
2271     IUnknown                 *parent;
2272     /*TODO: replace with iface usage */
2273 #if 0
2274     IWineD3DDevice         *wineD3DDevice;
2275 #else
2276     IWineD3DDeviceImpl       *wineD3DDevice;
2277 #endif
2278
2279     /* IWineD3DQuery fields */
2280     enum query_state         state;
2281     WINED3DQUERYTYPE         type;
2282     /* TODO: Think about using a IUnknown instead of a void* */
2283     void                     *extendedData;
2284     
2285   
2286 } IWineD3DQueryImpl;
2287
2288 extern const IWineD3DQueryVtbl IWineD3DQuery_Vtbl;
2289 extern const IWineD3DQueryVtbl IWineD3DEventQuery_Vtbl;
2290 extern const IWineD3DQueryVtbl IWineD3DOcclusionQuery_Vtbl;
2291
2292 /* Datastructures for IWineD3DQueryImpl.extendedData */
2293 typedef struct  WineQueryOcclusionData {
2294     GLuint  queryId;
2295     WineD3DContext *ctx;
2296 } WineQueryOcclusionData;
2297
2298 typedef struct  WineQueryEventData {
2299     GLuint  fenceId;
2300     WineD3DContext *ctx;
2301 } WineQueryEventData;
2302
2303 /* IWineD3DBuffer */
2304
2305 /* TODO: Add tests and support for FLOAT16_4 POSITIONT, D3DCOLOR position, other
2306  * fixed function semantics as D3DCOLOR or FLOAT16 */
2307 enum wined3d_buffer_conversion_type
2308 {
2309     CONV_NONE,
2310     CONV_D3DCOLOR,
2311     CONV_POSITIONT,
2312     CONV_FLOAT16_2, /* Also handles FLOAT16_4 */
2313 };
2314
2315 #define WINED3D_BUFFER_OPTIMIZED    0x01    /* Optimize has been called for the buffer */
2316 #define WINED3D_BUFFER_DIRTY        0x02    /* Buffer data has been modified */
2317 #define WINED3D_BUFFER_HASDESC      0x04    /* A vertex description has been found */
2318 #define WINED3D_BUFFER_CREATEBO     0x08    /* Attempt to create a buffer object next PreLoad */
2319 #define WINED3D_BUFFER_DOUBLEBUFFER 0x10    /* Use a vbo and local allocated memory */
2320
2321 struct wined3d_buffer
2322 {
2323     const struct IWineD3DBufferVtbl *vtbl;
2324     IWineD3DResourceClass resource;
2325
2326     struct wined3d_buffer_desc desc;
2327
2328     GLuint buffer_object;
2329     GLenum buffer_object_usage;
2330     GLenum buffer_type_hint;
2331     UINT buffer_object_size;
2332     LONG bind_count;
2333     DWORD flags;
2334
2335     UINT dirty_start;
2336     UINT dirty_end;
2337     LONG lock_count;
2338
2339     /* conversion stuff */
2340     UINT conversion_count;
2341     UINT draw_count;
2342     UINT stride;                                            /* 0 if no conversion */
2343     UINT conversion_stride;                                 /* 0 if no shifted conversion */
2344     enum wined3d_buffer_conversion_type *conversion_map;    /* NULL if no conversion */
2345     /* Extra load offsets, for FLOAT16 conversion */
2346     UINT *conversion_shift;                                 /* NULL if no shifted conversion */
2347 };
2348
2349 extern const IWineD3DBufferVtbl wined3d_buffer_vtbl;
2350 const BYTE *buffer_get_memory(IWineD3DBuffer *iface, UINT offset, GLuint *buffer_object);
2351 const BYTE *buffer_get_sysmem(struct wined3d_buffer *This);
2352
2353 /* IWineD3DRendertargetView */
2354 struct wined3d_rendertarget_view
2355 {
2356     const struct IWineD3DRendertargetViewVtbl *vtbl;
2357     LONG refcount;
2358
2359     IWineD3DResource *resource;
2360     IUnknown *parent;
2361 };
2362
2363 extern const IWineD3DRendertargetViewVtbl wined3d_rendertarget_view_vtbl;
2364
2365 /*****************************************************************************
2366  * IWineD3DSwapChainImpl implementation structure (extends IUnknown)
2367  */
2368
2369 typedef struct IWineD3DSwapChainImpl
2370 {
2371     /*IUnknown part*/
2372     const IWineD3DSwapChainVtbl *lpVtbl;
2373     LONG                      ref;     /* Note: Ref counting not required */
2374
2375     IUnknown                 *parent;
2376     IWineD3DDeviceImpl       *wineD3DDevice;
2377
2378     /* IWineD3DSwapChain fields */
2379     IWineD3DSurface         **backBuffer;
2380     IWineD3DSurface          *frontBuffer;
2381     WINED3DPRESENT_PARAMETERS presentParms;
2382     DWORD                     orig_width, orig_height;
2383     WINED3DFORMAT             orig_fmt;
2384     WINED3DGAMMARAMP          orig_gamma;
2385
2386     long prev_time, frames;   /* Performance tracking */
2387     unsigned int vSyncCounter;
2388
2389     WineD3DContext        **context; /* Later a array for multithreading */
2390     unsigned int            num_contexts;
2391
2392     HWND                    win_handle;
2393 } IWineD3DSwapChainImpl;
2394
2395 extern const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl;
2396 const IWineD3DSwapChainVtbl IWineGDISwapChain_Vtbl;
2397 void x11_copy_to_screen(IWineD3DSwapChainImpl *This, const RECT *rc);
2398
2399 HRESULT WINAPI IWineD3DBaseSwapChainImpl_QueryInterface(IWineD3DSwapChain *iface, REFIID riid, LPVOID *ppobj);
2400 ULONG WINAPI IWineD3DBaseSwapChainImpl_AddRef(IWineD3DSwapChain *iface);
2401 ULONG WINAPI IWineD3DBaseSwapChainImpl_Release(IWineD3DSwapChain *iface);
2402 HRESULT WINAPI IWineD3DBaseSwapChainImpl_GetParent(IWineD3DSwapChain *iface, IUnknown ** ppParent);
2403 HRESULT WINAPI IWineD3DBaseSwapChainImpl_GetFrontBufferData(IWineD3DSwapChain *iface, IWineD3DSurface *pDestSurface);
2404 HRESULT WINAPI IWineD3DBaseSwapChainImpl_GetBackBuffer(IWineD3DSwapChain *iface, UINT iBackBuffer, WINED3DBACKBUFFER_TYPE Type, IWineD3DSurface **ppBackBuffer);
2405 HRESULT WINAPI IWineD3DBaseSwapChainImpl_GetRasterStatus(IWineD3DSwapChain *iface, WINED3DRASTER_STATUS *pRasterStatus);
2406 HRESULT WINAPI IWineD3DBaseSwapChainImpl_GetDisplayMode(IWineD3DSwapChain *iface, WINED3DDISPLAYMODE*pMode);
2407 HRESULT WINAPI IWineD3DBaseSwapChainImpl_GetDevice(IWineD3DSwapChain *iface, IWineD3DDevice**ppDevice);
2408 HRESULT WINAPI IWineD3DBaseSwapChainImpl_GetPresentParameters(IWineD3DSwapChain *iface, WINED3DPRESENT_PARAMETERS *pPresentationParameters);
2409 HRESULT WINAPI IWineD3DBaseSwapChainImpl_SetGammaRamp(IWineD3DSwapChain *iface, DWORD Flags, CONST WINED3DGAMMARAMP *pRamp);
2410 HRESULT WINAPI IWineD3DBaseSwapChainImpl_GetGammaRamp(IWineD3DSwapChain *iface, WINED3DGAMMARAMP *pRamp);
2411
2412 WineD3DContext *IWineD3DSwapChainImpl_CreateContextForThread(IWineD3DSwapChain *iface);
2413
2414 /*****************************************************************************
2415  * Utility function prototypes 
2416  */
2417
2418 /* Trace routines */
2419 const char* debug_d3dformat(WINED3DFORMAT fmt);
2420 const char* debug_d3ddevicetype(WINED3DDEVTYPE devtype);
2421 const char* debug_d3dresourcetype(WINED3DRESOURCETYPE res);
2422 const char* debug_d3dusage(DWORD usage);
2423 const char* debug_d3dusagequery(DWORD usagequery);
2424 const char* debug_d3ddeclmethod(WINED3DDECLMETHOD method);
2425 const char* debug_d3ddeclusage(BYTE usage);
2426 const char* debug_d3dprimitivetype(WINED3DPRIMITIVETYPE PrimitiveType);
2427 const char* debug_d3drenderstate(DWORD state);
2428 const char* debug_d3dsamplerstate(DWORD state);
2429 const char* debug_d3dtexturefiltertype(WINED3DTEXTUREFILTERTYPE filter_type);
2430 const char* debug_d3dtexturestate(DWORD state);
2431 const char* debug_d3dtstype(WINED3DTRANSFORMSTATETYPE tstype);
2432 const char* debug_d3dpool(WINED3DPOOL pool);
2433 const char *debug_fbostatus(GLenum status);
2434 const char *debug_glerror(GLenum error);
2435 const char *debug_d3dbasis(WINED3DBASISTYPE basis);
2436 const char *debug_d3ddegree(WINED3DDEGREETYPE order);
2437 const char* debug_d3dtop(WINED3DTEXTUREOP d3dtop);
2438 void dump_color_fixup_desc(struct color_fixup_desc fixup);
2439 const char *debug_surflocation(DWORD flag);
2440
2441 /* Routines for GL <-> D3D values */
2442 GLenum StencilOp(DWORD op);
2443 GLenum CompareFunc(DWORD func);
2444 BOOL is_invalid_op(IWineD3DDeviceImpl *This, int stage, WINED3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3);
2445 void   set_tex_op_nvrc(IWineD3DDevice *iface, BOOL is_alpha, int stage, WINED3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3, INT texture_idx, DWORD dst);
2446 void   set_texture_matrix(const float *smat, DWORD flags, BOOL calculatedCoords, BOOL transformed, DWORD coordtype, BOOL ffp_can_disable_proj);
2447 void texture_activate_dimensions(DWORD stage, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context);
2448 void sampler_texdim(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context);
2449 void tex_alphaop(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context);
2450 void apply_pixelshader(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context);
2451 void state_fogcolor(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context);
2452 void state_fogdensity(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context);
2453 void state_fogstartend(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context);
2454 void state_fog_fragpart(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context);
2455
2456 void surface_add_dirty_rect(IWineD3DSurface *iface, const RECT *dirty_rect);
2457 GLenum surface_get_gl_buffer(IWineD3DSurface *iface, IWineD3DSwapChain *swapchain);
2458 void surface_load_ds_location(IWineD3DSurface *iface, DWORD location);
2459 void surface_modify_ds_location(IWineD3DSurface *iface, DWORD location);
2460 void surface_set_compatible_renderbuffer(IWineD3DSurface *iface, unsigned int width, unsigned int height);
2461 void surface_set_texture_name(IWineD3DSurface *iface, GLuint name, BOOL srgb_name);
2462 void surface_set_texture_target(IWineD3DSurface *iface, GLenum target);
2463
2464 BOOL getColorBits(const struct GlPixelFormatDesc *format_desc,
2465         short *redSize, short *greenSize, short *blueSize, short *alphaSize, short *totalSize);
2466 BOOL getDepthStencilBits(const struct GlPixelFormatDesc *format_desc, short *depthSize, short *stencilSize);
2467
2468 /* Math utils */
2469 void multiply_matrix(WINED3DMATRIX *dest, const WINED3DMATRIX *src1, const WINED3DMATRIX *src2);
2470 UINT wined3d_log2i(UINT32 x);
2471 unsigned int count_bits(unsigned int mask);
2472
2473 typedef struct local_constant {
2474     struct list entry;
2475     unsigned int idx;
2476     DWORD value[4];
2477 } local_constant;
2478
2479 typedef struct SHADER_LIMITS {
2480     unsigned int temporary;
2481     unsigned int texcoord;
2482     unsigned int sampler;
2483     unsigned int constant_int;
2484     unsigned int constant_float;
2485     unsigned int constant_bool;
2486     unsigned int address;
2487     unsigned int packed_output;
2488     unsigned int packed_input;
2489     unsigned int attributes;
2490     unsigned int label;
2491 } SHADER_LIMITS;
2492
2493 /** Keeps track of details for TEX_M#x# shader opcodes which need to 
2494     maintain state information between multiple codes */
2495 typedef struct SHADER_PARSE_STATE {
2496     unsigned int current_row;
2497     DWORD texcoord_w[2];
2498 } SHADER_PARSE_STATE;
2499
2500 #ifdef __GNUC__
2501 #define PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
2502 #else
2503 #define PRINTF_ATTR(fmt,args)
2504 #endif
2505
2506 /* Base Shader utility functions. 
2507  * (may move callers into the same file in the future) */
2508 extern int shader_addline(
2509     SHADER_BUFFER* buffer,
2510     const char* fmt, ...) PRINTF_ATTR(2,3);
2511 int shader_vaddline(SHADER_BUFFER *buffer, const char *fmt, va_list args);
2512
2513 /* Vertex shader utility functions */
2514 extern BOOL vshader_get_input(
2515     IWineD3DVertexShader* iface,
2516     BYTE usage_req, BYTE usage_idx_req,
2517     unsigned int* regnum);
2518
2519 extern HRESULT allocate_shader_constants(IWineD3DStateBlockImpl* object);
2520
2521 /*****************************************************************************
2522  * IDirect3DBaseShader implementation structure
2523  */
2524 typedef struct IWineD3DBaseShaderClass
2525 {
2526     LONG                            ref;
2527     SHADER_LIMITS                   limits;
2528     SHADER_PARSE_STATE              parse_state;
2529     DWORD                          *function;
2530     UINT                            functionLength;
2531     UINT                            cur_loop_depth, cur_loop_regno;
2532     BOOL                            load_local_constsF;
2533     const struct wined3d_shader_frontend *frontend;
2534     void *frontend_data;
2535
2536     /* Programs this shader is linked with */
2537     struct list linked_programs;
2538
2539     /* Immediate constants (override global ones) */
2540     struct list constantsB;
2541     struct list constantsF;
2542     struct list constantsI;
2543     shader_reg_maps reg_maps;
2544
2545     /* Pointer to the parent device */
2546     IWineD3DDevice *device;
2547     struct list     shader_list_entry;
2548
2549 } IWineD3DBaseShaderClass;
2550
2551 typedef struct IWineD3DBaseShaderImpl {
2552     /* IUnknown */
2553     const IWineD3DBaseShaderVtbl    *lpVtbl;
2554
2555     /* IWineD3DBaseShader */
2556     IWineD3DBaseShaderClass         baseShader;
2557 } IWineD3DBaseShaderImpl;
2558
2559 void shader_buffer_init(struct SHADER_BUFFER *buffer);
2560 void shader_buffer_free(struct SHADER_BUFFER *buffer);
2561 void shader_cleanup(IWineD3DBaseShader *iface);
2562 void shader_dump_src_param(const struct wined3d_shader_src_param *param,
2563         const struct wined3d_shader_version *shader_version);
2564 void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
2565         const struct wined3d_shader_version *shader_version);
2566 void shader_generate_main(IWineD3DBaseShader *iface, SHADER_BUFFER *buffer,
2567         const shader_reg_maps *reg_maps, const DWORD *pFunction, void *backend_ctx);
2568 HRESULT shader_get_registers_used(IWineD3DBaseShader *iface, const struct wined3d_shader_frontend *fe,
2569         struct shader_reg_maps *reg_maps, struct wined3d_shader_attribute *attributes,
2570         struct wined3d_shader_signature_element *input_signature,
2571         struct wined3d_shader_signature_element *output_signature, const DWORD *byte_code, DWORD constf_size);
2572 void shader_init(struct IWineD3DBaseShaderClass *shader, IWineD3DDevice *device);
2573 BOOL shader_match_semantic(const char *semantic_name, WINED3DDECLUSAGE usage);
2574 const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token);
2575 void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *pFunction);
2576
2577 static inline BOOL shader_is_pshader_version(enum wined3d_shader_type type)
2578 {
2579     return type == WINED3D_SHADER_TYPE_PIXEL;
2580 }
2581
2582 static inline BOOL shader_is_vshader_version(enum wined3d_shader_type type)
2583 {
2584     return type == WINED3D_SHADER_TYPE_VERTEX;
2585 }
2586
2587 static inline BOOL shader_is_scalar(const struct wined3d_shader_register *reg)
2588 {
2589     switch (reg->type)
2590     {
2591         case WINED3DSPR_RASTOUT:
2592             /* oFog & oPts */
2593             if (reg->idx != 0) return TRUE;
2594             /* oPos */
2595             return FALSE;
2596
2597         case WINED3DSPR_DEPTHOUT:   /* oDepth */
2598         case WINED3DSPR_CONSTBOOL:  /* b# */
2599         case WINED3DSPR_LOOP:       /* aL */
2600         case WINED3DSPR_PREDICATE:  /* p0 */
2601             return TRUE;
2602
2603         case WINED3DSPR_MISCTYPE:
2604             switch(reg->idx)
2605             {
2606                 case 0: /* vPos */
2607                     return FALSE;
2608                 case 1: /* vFace */
2609                     return TRUE;
2610                 default:
2611                     return FALSE;
2612             }
2613
2614         case WINED3DSPR_IMMCONST:
2615             switch(reg->immconst_type)
2616             {
2617                 case WINED3D_IMMCONST_FLOAT:
2618                     return TRUE;
2619                 default:
2620                     return FALSE;
2621             }
2622
2623         default:
2624             return FALSE;
2625     }
2626 }
2627
2628 static inline BOOL shader_constant_is_local(IWineD3DBaseShaderImpl* This, DWORD reg) {
2629     local_constant* lconst;
2630
2631     if(This->baseShader.load_local_constsF) return FALSE;
2632     LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
2633         if(lconst->idx == reg) return TRUE;
2634     }
2635     return FALSE;
2636
2637 }
2638
2639 /*****************************************************************************
2640  * IDirect3DVertexShader implementation structures
2641  */
2642 typedef struct IWineD3DVertexShaderImpl {
2643     /* IUnknown parts*/   
2644     const IWineD3DVertexShaderVtbl *lpVtbl;
2645
2646     /* IWineD3DBaseShader */
2647     IWineD3DBaseShaderClass     baseShader;
2648
2649     /* IWineD3DVertexShaderImpl */
2650     IUnknown                    *parent;
2651
2652     DWORD                       usage;
2653
2654     /* The GL shader */
2655     void                        *backend_priv;
2656
2657     /* Vertex shader input and output semantics */
2658     struct wined3d_shader_attribute attributes[MAX_ATTRIBS];
2659     struct wined3d_shader_signature_element output_signature[MAX_REG_OUTPUT];
2660
2661     UINT                       min_rel_offset, max_rel_offset;
2662     UINT                       rel_offset;
2663
2664     UINT                       recompile_count;
2665 } IWineD3DVertexShaderImpl;
2666 extern const IWineD3DVertexShaderVtbl IWineD3DVertexShader_Vtbl;
2667
2668 void find_vs_compile_args(IWineD3DVertexShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct vs_compile_args *args);
2669
2670 /*****************************************************************************
2671  * IDirect3DPixelShader implementation structure
2672  */
2673 typedef struct IWineD3DPixelShaderImpl {
2674     /* IUnknown parts */
2675     const IWineD3DPixelShaderVtbl *lpVtbl;
2676
2677     /* IWineD3DBaseShader */
2678     IWineD3DBaseShaderClass     baseShader;
2679
2680     /* IWineD3DPixelShaderImpl */
2681     IUnknown                   *parent;
2682
2683     /* Pixel shader input semantics */
2684     struct wined3d_shader_signature_element input_signature[MAX_REG_INPUT];
2685     DWORD                 input_reg_map[MAX_REG_INPUT];
2686     BOOL                  input_reg_used[MAX_REG_INPUT];
2687     int                         declared_in_count;
2688
2689     /* The GL shader */
2690     void                        *backend_priv;
2691
2692     /* Some information about the shader behavior */
2693     struct stb_const_desc       bumpenvmatconst[MAX_TEXTURES];
2694     unsigned char               numbumpenvmatconsts;
2695     struct stb_const_desc       luminanceconst[MAX_TEXTURES];
2696     char                        vpos_uniform;
2697
2698     BOOL                        color0_mov;
2699     DWORD                       color0_reg;
2700
2701 } IWineD3DPixelShaderImpl;
2702
2703 extern const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl;
2704 void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures);
2705 void find_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args);
2706
2707 /* sRGB correction constants */
2708 static const float srgb_cmp = 0.0031308;
2709 static const float srgb_mul_low = 12.92;
2710 static const float srgb_pow = 0.41666;
2711 static const float srgb_mul_high = 1.055;
2712 static const float srgb_sub_high = 0.055;
2713
2714 /*****************************************************************************
2715  * IWineD3DPalette implementation structure
2716  */
2717 struct IWineD3DPaletteImpl {
2718     /* IUnknown parts */
2719     const IWineD3DPaletteVtbl  *lpVtbl;
2720     LONG                       ref;
2721
2722     IUnknown                   *parent;
2723     IWineD3DDeviceImpl         *wineD3DDevice;
2724
2725     /* IWineD3DPalette */
2726     HPALETTE                   hpal;
2727     WORD                       palVersion;     /*|               */
2728     WORD                       palNumEntries;  /*|  LOGPALETTE   */
2729     PALETTEENTRY               palents[256];   /*|               */
2730     /* This is to store the palette in 'screen format' */
2731     int                        screen_palents[256];
2732     DWORD                      Flags;
2733 };
2734
2735 extern const IWineD3DPaletteVtbl IWineD3DPalette_Vtbl;
2736 DWORD IWineD3DPaletteImpl_Size(DWORD dwFlags);
2737
2738 /* DirectDraw utility functions */
2739 extern WINED3DFORMAT pixelformat_for_depth(DWORD depth);
2740
2741 /*****************************************************************************
2742  * Pixel format management
2743  */
2744
2745 /* WineD3D pixel format flags */
2746 #define WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING 0x1
2747 #define WINED3DFMT_FLAG_FILTERING                0x2
2748 #define WINED3DFMT_FLAG_DEPTH                    0x4
2749 #define WINED3DFMT_FLAG_STENCIL                  0x8
2750 #define WINED3DFMT_FLAG_RENDERTARGET             0x10
2751 #define WINED3DFMT_FLAG_FOURCC                   0x20
2752 #define WINED3DFMT_FLAG_FBO_ATTACHABLE           0x40
2753
2754 struct GlPixelFormatDesc
2755 {
2756     WINED3DFORMAT format;
2757     DWORD red_mask;
2758     DWORD green_mask;
2759     DWORD blue_mask;
2760     DWORD alpha_mask;
2761     UINT byte_count;
2762     WORD depth_size;
2763     WORD stencil_size;
2764
2765     enum wined3d_ffp_emit_idx emit_idx;
2766     GLint component_count;
2767     GLenum gl_vtx_type;
2768     GLint gl_vtx_format;
2769     GLboolean gl_normalized;
2770     unsigned int component_size;
2771
2772     GLint glInternal;
2773     GLint glGammaInternal;
2774     GLint rtInternal;
2775     GLint glFormat;
2776     GLint glType;
2777     unsigned int Flags;
2778     float heightscale;
2779     struct color_fixup_desc color_fixup;
2780 };
2781
2782 const struct GlPixelFormatDesc *getFormatDescEntry(WINED3DFORMAT fmt, const WineD3D_GL_Info *gl_info);
2783
2784 static inline BOOL use_vs(IWineD3DStateBlockImpl *stateblock)
2785 {
2786     return (stateblock->vertexShader
2787             && !stateblock->wineD3DDevice->strided_streams.position_transformed
2788             && stateblock->wineD3DDevice->vs_selected_mode != SHADER_NONE);
2789 }
2790
2791 static inline BOOL use_ps(IWineD3DStateBlockImpl *stateblock)
2792 {
2793     return (stateblock->pixelShader
2794             && stateblock->wineD3DDevice->ps_selected_mode != SHADER_NONE);
2795 }
2796
2797 void stretch_rect_fbo(IWineD3DDevice *iface, IWineD3DSurface *src_surface, WINED3DRECT *src_rect,
2798         IWineD3DSurface *dst_surface, WINED3DRECT *dst_rect, const WINED3DTEXTUREFILTERTYPE filter, BOOL flip);
2799
2800 /* The WNDCLASS-Name for the fake window which we use to retrieve the GL capabilities */
2801 #define WINED3D_OPENGL_WINDOW_CLASS_NAME "WineD3D_OpenGL"
2802
2803 #endif