d3d9: Test viewports that are bigger than the surface.
[wine] / dlls / wined3d / stateblock.c
1 /*
2  * state block implementation
3  *
4  * Copyright 2002 Raphael Junqueira
5  * Copyright 2004 Jason Edmeades
6  * Copyright 2005 Oliver Stieber
7  * Copyright 2007 Stefan Dösinger for CodeWeavers
8  * Copyright 2009 Henri Verbeet for CodeWeavers
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wined3d_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29 #define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
30
31 static const DWORD pixel_states_render[] =
32 {
33     WINED3DRS_ALPHABLENDENABLE,
34     WINED3DRS_ALPHAFUNC,
35     WINED3DRS_ALPHAREF,
36     WINED3DRS_ALPHATESTENABLE,
37     WINED3DRS_BLENDOP,
38     WINED3DRS_COLORWRITEENABLE,
39     WINED3DRS_DESTBLEND,
40     WINED3DRS_DITHERENABLE,
41     WINED3DRS_FILLMODE,
42     WINED3DRS_FOGDENSITY,
43     WINED3DRS_FOGEND,
44     WINED3DRS_FOGSTART,
45     WINED3DRS_LASTPIXEL,
46     WINED3DRS_SHADEMODE,
47     WINED3DRS_SRCBLEND,
48     WINED3DRS_STENCILENABLE,
49     WINED3DRS_STENCILFAIL,
50     WINED3DRS_STENCILFUNC,
51     WINED3DRS_STENCILMASK,
52     WINED3DRS_STENCILPASS,
53     WINED3DRS_STENCILREF,
54     WINED3DRS_STENCILWRITEMASK,
55     WINED3DRS_STENCILZFAIL,
56     WINED3DRS_TEXTUREFACTOR,
57     WINED3DRS_WRAP0,
58     WINED3DRS_WRAP1,
59     WINED3DRS_WRAP2,
60     WINED3DRS_WRAP3,
61     WINED3DRS_WRAP4,
62     WINED3DRS_WRAP5,
63     WINED3DRS_WRAP6,
64     WINED3DRS_WRAP7,
65     WINED3DRS_ZENABLE,
66     WINED3DRS_ZFUNC,
67     WINED3DRS_ZWRITEENABLE,
68 };
69
70 static const DWORD pixel_states_texture[] =
71 {
72     WINED3DTSS_ALPHAARG0,
73     WINED3DTSS_ALPHAARG1,
74     WINED3DTSS_ALPHAARG2,
75     WINED3DTSS_ALPHAOP,
76     WINED3DTSS_BUMPENVLOFFSET,
77     WINED3DTSS_BUMPENVLSCALE,
78     WINED3DTSS_BUMPENVMAT00,
79     WINED3DTSS_BUMPENVMAT01,
80     WINED3DTSS_BUMPENVMAT10,
81     WINED3DTSS_BUMPENVMAT11,
82     WINED3DTSS_COLORARG0,
83     WINED3DTSS_COLORARG1,
84     WINED3DTSS_COLORARG2,
85     WINED3DTSS_COLOROP,
86     WINED3DTSS_RESULTARG,
87     WINED3DTSS_TEXCOORDINDEX,
88     WINED3DTSS_TEXTURETRANSFORMFLAGS,
89 };
90
91 static const DWORD pixel_states_sampler[] =
92 {
93     WINED3DSAMP_ADDRESSU,
94     WINED3DSAMP_ADDRESSV,
95     WINED3DSAMP_ADDRESSW,
96     WINED3DSAMP_BORDERCOLOR,
97     WINED3DSAMP_MAGFILTER,
98     WINED3DSAMP_MINFILTER,
99     WINED3DSAMP_MIPFILTER,
100     WINED3DSAMP_MIPMAPLODBIAS,
101     WINED3DSAMP_MAXMIPLEVEL,
102     WINED3DSAMP_MAXANISOTROPY,
103     WINED3DSAMP_SRGBTEXTURE,
104     WINED3DSAMP_ELEMENTINDEX,
105 };
106
107 static const DWORD vertex_states_render[] =
108 {
109     WINED3DRS_AMBIENT,
110     WINED3DRS_AMBIENTMATERIALSOURCE,
111     WINED3DRS_CLIPPING,
112     WINED3DRS_CLIPPLANEENABLE,
113     WINED3DRS_COLORVERTEX,
114     WINED3DRS_DIFFUSEMATERIALSOURCE,
115     WINED3DRS_EMISSIVEMATERIALSOURCE,
116     WINED3DRS_FOGDENSITY,
117     WINED3DRS_FOGEND,
118     WINED3DRS_FOGSTART,
119     WINED3DRS_FOGTABLEMODE,
120     WINED3DRS_FOGVERTEXMODE,
121     WINED3DRS_INDEXEDVERTEXBLENDENABLE,
122     WINED3DRS_LIGHTING,
123     WINED3DRS_LOCALVIEWER,
124     WINED3DRS_MULTISAMPLEANTIALIAS,
125     WINED3DRS_MULTISAMPLEMASK,
126     WINED3DRS_NORMALIZENORMALS,
127     WINED3DRS_PATCHEDGESTYLE,
128     WINED3DRS_POINTSCALE_A,
129     WINED3DRS_POINTSCALE_B,
130     WINED3DRS_POINTSCALE_C,
131     WINED3DRS_POINTSCALEENABLE,
132     WINED3DRS_POINTSIZE,
133     WINED3DRS_POINTSIZE_MAX,
134     WINED3DRS_POINTSIZE_MIN,
135     WINED3DRS_POINTSPRITEENABLE,
136     WINED3DRS_RANGEFOGENABLE,
137     WINED3DRS_SPECULARMATERIALSOURCE,
138     WINED3DRS_TWEENFACTOR,
139     WINED3DRS_VERTEXBLEND,
140     WINED3DRS_CULLMODE,
141     WINED3DRS_FOGCOLOR,
142 };
143
144 static const DWORD vertex_states_texture[] =
145 {
146     WINED3DTSS_TEXCOORDINDEX,
147     WINED3DTSS_TEXTURETRANSFORMFLAGS,
148 };
149
150 static const DWORD vertex_states_sampler[] =
151 {
152     WINED3DSAMP_DMAPOFFSET,
153 };
154
155 /* Allocates the correct amount of space for pixel and vertex shader constants,
156  * along with their set/changed flags on the given stateblock object
157  */
158 static HRESULT stateblock_allocate_shader_constants(IWineD3DStateBlockImpl *object)
159 {
160     IWineD3DStateBlockImpl *This = object;
161
162     /* Allocate space for floating point constants */
163     object->pixelShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
164     if (!object->pixelShaderConstantF) goto fail;
165
166     object->changed.pixelShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(pshader_constantsF));
167     if (!object->changed.pixelShaderConstantsF) goto fail;
168
169     object->vertexShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
170     if (!object->vertexShaderConstantF) goto fail;
171
172     object->changed.vertexShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(vshader_constantsF));
173     if (!object->changed.vertexShaderConstantsF) goto fail;
174
175     object->contained_vs_consts_f = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * GL_LIMITS(vshader_constantsF));
176     if (!object->contained_vs_consts_f) goto fail;
177
178     object->contained_ps_consts_f = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * GL_LIMITS(pshader_constantsF));
179     if (!object->contained_ps_consts_f) goto fail;
180
181     return WINED3D_OK;
182
183 fail:
184     ERR("Failed to allocate memory\n");
185     HeapFree(GetProcessHeap(), 0, object->pixelShaderConstantF);
186     HeapFree(GetProcessHeap(), 0, object->changed.pixelShaderConstantsF);
187     HeapFree(GetProcessHeap(), 0, object->vertexShaderConstantF);
188     HeapFree(GetProcessHeap(), 0, object->changed.vertexShaderConstantsF);
189     HeapFree(GetProcessHeap(), 0, object->contained_vs_consts_f);
190     HeapFree(GetProcessHeap(), 0, object->contained_ps_consts_f);
191     return E_OUTOFMEMORY;
192 }
193
194 static inline void stateblock_set_bits(DWORD *map, UINT map_size)
195 {
196     DWORD mask = (1 << (map_size & 0x1f)) - 1;
197     memset(map, 0xff, (map_size >> 5) * sizeof(*map));
198     if (mask) map[map_size >> 5] = mask;
199 }
200
201 /* Set all members of a stateblock savedstate to the given value */
202 static void stateblock_savedstates_set_all(SAVEDSTATES *states, const struct wined3d_gl_info *gl_info)
203 {
204     unsigned int i;
205
206     /* Single values */
207     states->primitive_type = 1;
208     states->indices = 1;
209     states->material = 1;
210     states->viewport = 1;
211     states->vertexDecl = 1;
212     states->pixelShader = 1;
213     states->vertexShader = 1;
214     states->scissorRect = 1;
215
216     /* Fixed size arrays */
217     states->streamSource = 0xffff;
218     states->streamFreq = 0xffff;
219     states->textures = 0xfffff;
220     stateblock_set_bits(states->transform, HIGHEST_TRANSFORMSTATE + 1);
221     stateblock_set_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
222     for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
223     for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3fff;
224     states->clipplane = 0xffffffff;
225     states->pixelShaderConstantsB = 0xffff;
226     states->pixelShaderConstantsI = 0xffff;
227     states->vertexShaderConstantsB = 0xffff;
228     states->vertexShaderConstantsI = 0xffff;
229
230     /* Dynamically sized arrays */
231     memset(states->pixelShaderConstantsF, TRUE, sizeof(BOOL) * gl_info->max_pshader_constantsF);
232     memset(states->vertexShaderConstantsF, TRUE, sizeof(BOOL) * gl_info->max_vshader_constantsF);
233 }
234
235 static void stateblock_savedstates_set_pixel(SAVEDSTATES *states, const struct wined3d_gl_info *gl_info)
236 {
237     DWORD texture_mask = 0;
238     WORD sampler_mask = 0;
239     unsigned int i;
240
241     states->pixelShader = 1;
242
243     for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
244     {
245         DWORD rs = pixel_states_render[i];
246         states->renderState[rs >> 5] |= 1 << (rs & 0x1f);
247     }
248
249     for (i = 0; i < sizeof(pixel_states_texture) / sizeof(*pixel_states_texture); ++i)
250         texture_mask |= 1 << pixel_states_texture[i];
251     for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
252     for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
253         sampler_mask |= 1 << pixel_states_sampler[i];
254     for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
255     states->pixelShaderConstantsB = 0xffff;
256     states->pixelShaderConstantsI = 0xffff;
257
258     memset(states->pixelShaderConstantsF, TRUE, sizeof(BOOL) * gl_info->max_pshader_constantsF);
259 }
260
261 static void stateblock_savedstates_set_vertex(SAVEDSTATES *states, const struct wined3d_gl_info *gl_info)
262 {
263     DWORD texture_mask = 0;
264     WORD sampler_mask = 0;
265     unsigned int i;
266
267     states->vertexShader = 1;
268
269     for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
270     {
271         DWORD rs = vertex_states_render[i];
272         states->renderState[rs >> 5] |= 1 << (rs & 0x1f);
273     }
274
275     for (i = 0; i < sizeof(vertex_states_texture) / sizeof(*vertex_states_texture); ++i)
276         texture_mask |= 1 << vertex_states_texture[i];
277     for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
278     for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
279         sampler_mask |= 1 << vertex_states_sampler[i];
280     for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
281     states->vertexShaderConstantsB = 0xffff;
282     states->vertexShaderConstantsI = 0xffff;
283
284     memset(states->vertexShaderConstantsF, TRUE, sizeof(BOOL) * gl_info->max_vshader_constantsF);
285 }
286
287 static void stateblock_copy_values(IWineD3DStateBlockImpl *dst, const IWineD3DStateBlockImpl *src,
288         const struct wined3d_gl_info *gl_info)
289 {
290     unsigned int l;
291
292     /* Single items */
293     dst->gl_primitive_type = src->gl_primitive_type;
294     dst->vertexDecl = src->vertexDecl;
295     dst->vertexShader = src->vertexShader;
296     dst->streamIsUP = src->streamIsUP;
297     dst->pIndexData = src->pIndexData;
298     dst->IndexFmt = src->IndexFmt;
299     dst->baseVertexIndex = src->baseVertexIndex;
300     dst->clip_status = src->clip_status;
301     dst->viewport = src->viewport;
302     dst->material = src->material;
303     dst->pixelShader = src->pixelShader;
304     dst->scissorRect = src->scissorRect;
305
306     /* Lights */
307     memset(dst->activeLights, 0, sizeof(dst->activeLights));
308     for (l = 0; l < LIGHTMAP_SIZE; ++l)
309     {
310         struct list *e1, *e2;
311         LIST_FOR_EACH_SAFE(e1, e2, &dst->lightMap[l])
312         {
313             PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
314             list_remove(&light->entry);
315             HeapFree(GetProcessHeap(), 0, light);
316         }
317
318         LIST_FOR_EACH(e1, &src->lightMap[l])
319         {
320             PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry), *light2;
321             light2 = HeapAlloc(GetProcessHeap(), 0, sizeof(*light));
322             *light2 = *light;
323             list_add_tail(&dst->lightMap[l], &light2->entry);
324             if (light2->glIndex != -1) dst->activeLights[light2->glIndex] = light2;
325         }
326     }
327
328     /* Fixed size arrays */
329     memcpy(dst->vertexShaderConstantB, src->vertexShaderConstantB, sizeof(dst->vertexShaderConstantB));
330     memcpy(dst->vertexShaderConstantI, src->vertexShaderConstantI, sizeof(dst->vertexShaderConstantI));
331     memcpy(dst->pixelShaderConstantB, src->pixelShaderConstantB, sizeof(dst->pixelShaderConstantB));
332     memcpy(dst->pixelShaderConstantI, src->pixelShaderConstantI, sizeof(dst->pixelShaderConstantI));
333
334     memcpy(dst->streamStride, src->streamStride, sizeof(dst->streamStride));
335     memcpy(dst->streamOffset, src->streamOffset, sizeof(dst->streamOffset));
336     memcpy(dst->streamSource, src->streamSource, sizeof(dst->streamSource));
337     memcpy(dst->streamFreq, src->streamFreq, sizeof(dst->streamFreq));
338     memcpy(dst->streamFlags, src->streamFlags, sizeof(dst->streamFlags));
339     memcpy(dst->transforms, src->transforms, sizeof(dst->transforms));
340     memcpy(dst->clipplane, src->clipplane, sizeof(dst->clipplane));
341     memcpy(dst->renderState, src->renderState, sizeof(dst->renderState));
342     memcpy(dst->textures, src->textures, sizeof(dst->textures));
343     memcpy(dst->textureState, src->textureState, sizeof(dst->textureState));
344     memcpy(dst->samplerState, src->samplerState, sizeof(dst->samplerState));
345
346     /* Dynamically sized arrays */
347     memcpy(dst->vertexShaderConstantF, src->vertexShaderConstantF, sizeof(float) * gl_info->max_vshader_constantsF * 4);
348     memcpy(dst->pixelShaderConstantF,  src->pixelShaderConstantF,  sizeof(float) * gl_info->max_pshader_constantsF * 4);
349 }
350
351 void stateblock_init_contained_states(IWineD3DStateBlockImpl *stateblock)
352 {
353     const struct wined3d_gl_info *gl_info = &stateblock->wineD3DDevice->adapter->gl_info;
354     unsigned int i, j;
355
356     for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
357     {
358         DWORD map = stateblock->changed.renderState[i];
359         for (j = 0; map; map >>= 1, ++j)
360         {
361             if (!(map & 1)) continue;
362
363             stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
364             ++stateblock->num_contained_render_states;
365         }
366     }
367
368     for (i = 0; i <= HIGHEST_TRANSFORMSTATE >> 5; ++i)
369     {
370         DWORD map = stateblock->changed.transform[i];
371         for (j = 0; map; map >>= 1, ++j)
372         {
373             if (!(map & 1)) continue;
374
375             stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
376             ++stateblock->num_contained_transform_states;
377         }
378     }
379
380     for (i = 0; i < gl_info->max_vshader_constantsF; ++i)
381     {
382         if (stateblock->changed.vertexShaderConstantsF[i])
383         {
384             stateblock->contained_vs_consts_f[stateblock->num_contained_vs_consts_f] = i;
385             ++stateblock->num_contained_vs_consts_f;
386         }
387     }
388
389     for (i = 0; i < MAX_CONST_I; ++i)
390     {
391         if (stateblock->changed.vertexShaderConstantsI & (1 << i))
392         {
393             stateblock->contained_vs_consts_i[stateblock->num_contained_vs_consts_i] = i;
394             ++stateblock->num_contained_vs_consts_i;
395         }
396     }
397
398     for (i = 0; i < MAX_CONST_B; ++i)
399     {
400         if (stateblock->changed.vertexShaderConstantsB & (1 << i))
401         {
402             stateblock->contained_vs_consts_b[stateblock->num_contained_vs_consts_b] = i;
403             ++stateblock->num_contained_vs_consts_b;
404         }
405     }
406
407     for (i = 0; i < gl_info->max_pshader_constantsF; ++i)
408     {
409         if (stateblock->changed.pixelShaderConstantsF[i])
410         {
411             stateblock->contained_ps_consts_f[stateblock->num_contained_ps_consts_f] = i;
412             ++stateblock->num_contained_ps_consts_f;
413         }
414     }
415
416     for (i = 0; i < MAX_CONST_I; ++i)
417     {
418         if (stateblock->changed.pixelShaderConstantsI & (1 << i))
419         {
420             stateblock->contained_ps_consts_i[stateblock->num_contained_ps_consts_i] = i;
421             ++stateblock->num_contained_ps_consts_i;
422         }
423     }
424
425     for (i = 0; i < MAX_CONST_B; ++i)
426     {
427         if (stateblock->changed.pixelShaderConstantsB & (1 << i))
428         {
429             stateblock->contained_ps_consts_b[stateblock->num_contained_ps_consts_b] = i;
430             ++stateblock->num_contained_ps_consts_b;
431         }
432     }
433
434     for (i = 0; i < MAX_TEXTURES; ++i)
435     {
436         DWORD map = stateblock->changed.textureState[i];
437
438         for(j = 0; map; map >>= 1, ++j)
439         {
440             if (!(map & 1)) continue;
441
442             stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
443             stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
444             ++stateblock->num_contained_tss_states;
445         }
446     }
447
448     for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
449     {
450         DWORD map = stateblock->changed.samplerState[i];
451
452         for (j = 0; map; map >>= 1, ++j)
453         {
454             if (!(map & 1)) continue;
455
456             stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
457             stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
458             ++stateblock->num_contained_sampler_states;
459         }
460     }
461 }
462
463 /**********************************************************
464  * IWineD3DStateBlockImpl IUnknown parts follows
465  **********************************************************/
466 static HRESULT  WINAPI IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock *iface,REFIID riid,LPVOID *ppobj)
467 {
468     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
469     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
470     if (IsEqualGUID(riid, &IID_IUnknown)
471         || IsEqualGUID(riid, &IID_IWineD3DBase)
472         || IsEqualGUID(riid, &IID_IWineD3DStateBlock)){
473         IUnknown_AddRef(iface);
474         *ppobj = This;
475         return S_OK;
476     }
477     *ppobj = NULL;
478     return E_NOINTERFACE;
479 }
480
481 static ULONG  WINAPI IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock *iface) {
482     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
483     ULONG refCount = InterlockedIncrement(&This->ref);
484
485     TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
486     return refCount;
487 }
488
489 static ULONG  WINAPI IWineD3DStateBlockImpl_Release(IWineD3DStateBlock *iface) {
490     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
491     ULONG refCount = InterlockedDecrement(&This->ref);
492
493     TRACE("(%p) : Releasing from %d\n", This, refCount + 1);
494
495     if (!refCount) {
496         int counter;
497
498         if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
499
500         for (counter = 0; counter < MAX_COMBINED_SAMPLERS; counter++)
501         {
502             if (This->textures[counter]) IWineD3DBaseTexture_Release(This->textures[counter]);
503         }
504
505         for (counter = 0; counter < MAX_STREAMS; counter++) {
506             if(This->streamSource[counter]) {
507                 if (IWineD3DBuffer_Release(This->streamSource[counter]))
508                 {
509                     TRACE("Vertex buffer still referenced by stateblock, applications has leaked Stream %u, buffer %p\n", counter, This->streamSource[counter]);
510                 }
511             }
512         }
513         if(This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
514         if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
515         if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
516
517         for(counter = 0; counter < LIGHTMAP_SIZE; counter++) {
518             struct list *e1, *e2;
519             LIST_FOR_EACH_SAFE(e1, e2, &This->lightMap[counter]) {
520                 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
521                 list_remove(&light->entry);
522                 HeapFree(GetProcessHeap(), 0, light);
523             }
524         }
525
526         HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
527         HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
528         HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
529         HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
530         HeapFree(GetProcessHeap(), 0, This->contained_vs_consts_f);
531         HeapFree(GetProcessHeap(), 0, This->contained_ps_consts_f);
532         HeapFree(GetProcessHeap(), 0, This);
533     }
534     return refCount;
535 }
536
537 /**********************************************************
538  * IWineD3DStateBlockImpl parts follows
539  **********************************************************/
540 static HRESULT  WINAPI IWineD3DStateBlockImpl_GetParent(IWineD3DStateBlock *iface, IUnknown **pParent) {
541     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
542     IUnknown_AddRef(This->parent);
543     *pParent = This->parent;
544     return WINED3D_OK;
545 }
546
547 static HRESULT  WINAPI IWineD3DStateBlockImpl_GetDevice(IWineD3DStateBlock *iface, IWineD3DDevice** ppDevice){
548
549     IWineD3DStateBlockImpl *This   = (IWineD3DStateBlockImpl *)iface;
550
551     *ppDevice = (IWineD3DDevice*)This->wineD3DDevice;
552     IWineD3DDevice_AddRef(*ppDevice);
553     return WINED3D_OK;
554
555 }
556
557 static void record_lights(IWineD3DStateBlockImpl *This, const IWineD3DStateBlockImpl *targetStateBlock)
558 {
559     UINT i;
560
561     /* Lights... For a recorded state block, we just had a chain of actions to perform,
562      * so we need to walk that chain and update any actions which differ
563      */
564     for(i = 0; i < LIGHTMAP_SIZE; i++) {
565         struct list *e, *f;
566         LIST_FOR_EACH(e, &This->lightMap[i]) {
567             BOOL updated = FALSE;
568             PLIGHTINFOEL *src = LIST_ENTRY(e, PLIGHTINFOEL, entry), *realLight;
569             if (!src->changed && !src->enabledChanged) continue;
570
571             /* Look up the light in the destination */
572             LIST_FOR_EACH(f, &targetStateBlock->lightMap[i]) {
573                 realLight = LIST_ENTRY(f, PLIGHTINFOEL, entry);
574                 if(realLight->OriginalIndex == src->OriginalIndex) {
575                     if(src->changed) {
576                         src->OriginalParms = realLight->OriginalParms;
577                     }
578                     if(src->enabledChanged) {
579                             /* Need to double check because enabledChanged does not catch enabled -> disabled -> enabled
580                         * or disabled -> enabled -> disabled changes
581                             */
582                         if(realLight->glIndex == -1 && src->glIndex != -1) {
583                             /* Light disabled */
584                             This->activeLights[src->glIndex] = NULL;
585                         } else if(realLight->glIndex != -1 && src->glIndex == -1){
586                             /* Light enabled */
587                             This->activeLights[realLight->glIndex] = src;
588                         }
589                         src->glIndex = realLight->glIndex;
590                     }
591                     updated = TRUE;
592                     break;
593                 }
594             }
595
596             if(updated) {
597                 /* Found a light, all done, proceed with next hash entry */
598                 continue;
599             } else if(src->changed) {
600                 /* Otherwise assign defaul params */
601                 src->OriginalParms = WINED3D_default_light;
602             } else {
603                 /* Not enabled by default */
604                 src->glIndex = -1;
605             }
606         }
607     }
608 }
609
610 static HRESULT  WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface){
611
612     IWineD3DStateBlockImpl *This             = (IWineD3DStateBlockImpl *)iface;
613     IWineD3DStateBlockImpl *targetStateBlock = This->wineD3DDevice->stateBlock;
614     unsigned int i, j;
615     DWORD map;
616
617     TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock, This);
618
619     /* If not recorded, then update can just recapture */
620     if (This->blockType == WINED3DSBT_RECORDED) {
621
622         /* Recorded => Only update 'changed' values */
623         if (This->changed.vertexShader && This->vertexShader != targetStateBlock->vertexShader) {
624             TRACE("Updating vertex shader from %p to %p\n", This->vertexShader, targetStateBlock->vertexShader);
625
626             if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
627             if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
628             This->vertexShader = targetStateBlock->vertexShader;
629         }
630
631         /* Vertex Shader Float Constants */
632         for (j = 0; j < This->num_contained_vs_consts_f; ++j) {
633             i = This->contained_vs_consts_f[j];
634             TRACE("Setting %p from %p %u to {%f, %f, %f, %f}\n", This, targetStateBlock, i,
635                     targetStateBlock->vertexShaderConstantF[i * 4],
636                     targetStateBlock->vertexShaderConstantF[i * 4 + 1],
637                     targetStateBlock->vertexShaderConstantF[i * 4 + 2],
638                     targetStateBlock->vertexShaderConstantF[i * 4 + 3]);
639
640             This->vertexShaderConstantF[i * 4]      = targetStateBlock->vertexShaderConstantF[i * 4];
641             This->vertexShaderConstantF[i * 4 + 1]  = targetStateBlock->vertexShaderConstantF[i * 4 + 1];
642             This->vertexShaderConstantF[i * 4 + 2]  = targetStateBlock->vertexShaderConstantF[i * 4 + 2];
643             This->vertexShaderConstantF[i * 4 + 3]  = targetStateBlock->vertexShaderConstantF[i * 4 + 3];
644         }
645
646         /* Vertex Shader Integer Constants */
647         for (j = 0; j < This->num_contained_vs_consts_i; ++j) {
648             i = This->contained_vs_consts_i[j];
649             TRACE("Setting %p from %p %u to {%d, %d, %d, %d}\n", This, targetStateBlock, i,
650                     targetStateBlock->vertexShaderConstantI[i * 4],
651                     targetStateBlock->vertexShaderConstantI[i * 4 + 1],
652                     targetStateBlock->vertexShaderConstantI[i * 4 + 2],
653                     targetStateBlock->vertexShaderConstantI[i * 4 + 3]);
654
655             This->vertexShaderConstantI[i * 4]      = targetStateBlock->vertexShaderConstantI[i * 4];
656             This->vertexShaderConstantI[i * 4 + 1]  = targetStateBlock->vertexShaderConstantI[i * 4 + 1];
657             This->vertexShaderConstantI[i * 4 + 2]  = targetStateBlock->vertexShaderConstantI[i * 4 + 2];
658             This->vertexShaderConstantI[i * 4 + 3]  = targetStateBlock->vertexShaderConstantI[i * 4 + 3];
659         }
660
661         /* Vertex Shader Boolean Constants */
662         for (j = 0; j < This->num_contained_vs_consts_b; ++j) {
663             i = This->contained_vs_consts_b[j];
664             TRACE("Setting %p from %p %u to %s\n", This, targetStateBlock, i,
665                     targetStateBlock->vertexShaderConstantB[i] ? "TRUE" : "FALSE");
666
667             This->vertexShaderConstantB[i] =  targetStateBlock->vertexShaderConstantB[i];
668         }
669
670         /* Pixel Shader Float Constants */
671         for (j = 0; j < This->num_contained_ps_consts_f; ++j) {
672             i = This->contained_ps_consts_f[j];
673             TRACE("Setting %p from %p %u to {%f, %f, %f, %f}\n", This, targetStateBlock, i,
674                     targetStateBlock->pixelShaderConstantF[i * 4],
675                     targetStateBlock->pixelShaderConstantF[i * 4 + 1],
676                     targetStateBlock->pixelShaderConstantF[i * 4 + 2],
677                     targetStateBlock->pixelShaderConstantF[i * 4 + 3]);
678
679             This->pixelShaderConstantF[i * 4]      = targetStateBlock->pixelShaderConstantF[i * 4];
680             This->pixelShaderConstantF[i * 4 + 1]  = targetStateBlock->pixelShaderConstantF[i * 4 + 1];
681             This->pixelShaderConstantF[i * 4 + 2]  = targetStateBlock->pixelShaderConstantF[i * 4 + 2];
682             This->pixelShaderConstantF[i * 4 + 3]  = targetStateBlock->pixelShaderConstantF[i * 4 + 3];
683         }
684
685         /* Pixel Shader Integer Constants */
686         for (j = 0; j < This->num_contained_ps_consts_i; ++j) {
687             i = This->contained_ps_consts_i[j];
688             TRACE("Setting %p from %p %u to {%d, %d, %d, %d}\n", This, targetStateBlock, i,
689                     targetStateBlock->pixelShaderConstantI[i * 4],
690                     targetStateBlock->pixelShaderConstantI[i * 4 + 1],
691                     targetStateBlock->pixelShaderConstantI[i * 4 + 2],
692                     targetStateBlock->pixelShaderConstantI[i * 4 + 3]);
693
694             This->pixelShaderConstantI[i * 4]      = targetStateBlock->pixelShaderConstantI[i * 4];
695             This->pixelShaderConstantI[i * 4 + 1]  = targetStateBlock->pixelShaderConstantI[i * 4 + 1];
696             This->pixelShaderConstantI[i * 4 + 2]  = targetStateBlock->pixelShaderConstantI[i * 4 + 2];
697             This->pixelShaderConstantI[i * 4 + 3]  = targetStateBlock->pixelShaderConstantI[i * 4 + 3];
698         }
699
700         /* Pixel Shader Boolean Constants */
701         for (j = 0; j < This->num_contained_ps_consts_b; ++j) {
702             i = This->contained_ps_consts_b[j];
703             TRACE("Setting %p from %p %u to %s\n", This, targetStateBlock, i,
704                     targetStateBlock->pixelShaderConstantB[i] ? "TRUE" : "FALSE");
705
706             This->pixelShaderConstantB[i] =  targetStateBlock->pixelShaderConstantB[i];
707         }
708
709         /* Others + Render & Texture */
710         for (i = 0; i < This->num_contained_transform_states; i++) {
711             TRACE("Updating transform %u\n", i);
712             This->transforms[This->contained_transform_states[i]] =
713                 targetStateBlock->transforms[This->contained_transform_states[i]];
714         }
715
716         if (This->changed.primitive_type) This->gl_primitive_type = targetStateBlock->gl_primitive_type;
717
718         if (This->changed.indices && ((This->pIndexData != targetStateBlock->pIndexData)
719                         || (This->baseVertexIndex != targetStateBlock->baseVertexIndex)
720                         || (This->IndexFmt != targetStateBlock->IndexFmt))) {
721             TRACE("Updating pIndexData to %p, baseVertexIndex to %d\n",
722                     targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
723             if(targetStateBlock->pIndexData) IWineD3DBuffer_AddRef(targetStateBlock->pIndexData);
724             if(This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
725             This->pIndexData = targetStateBlock->pIndexData;
726             This->baseVertexIndex = targetStateBlock->baseVertexIndex;
727             This->IndexFmt = targetStateBlock->IndexFmt;
728         }
729
730         if(This->changed.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl){
731             TRACE("Updating vertex declaration from %p to %p\n", This->vertexDecl, targetStateBlock->vertexDecl);
732
733             if (targetStateBlock->vertexDecl) IWineD3DVertexDeclaration_AddRef(targetStateBlock->vertexDecl);
734             if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
735             This->vertexDecl = targetStateBlock->vertexDecl;
736         }
737
738         if (This->changed.material && memcmp(&targetStateBlock->material,
739                                                     &This->material,
740                                                     sizeof(WINED3DMATERIAL)) != 0) {
741             TRACE("Updating material\n");
742             This->material = targetStateBlock->material;
743         }
744
745         if (This->changed.viewport && memcmp(&targetStateBlock->viewport,
746                                                     &This->viewport,
747                                                     sizeof(WINED3DVIEWPORT)) != 0) {
748             TRACE("Updating viewport\n");
749             This->viewport = targetStateBlock->viewport;
750         }
751
752         if(This->changed.scissorRect && memcmp(&targetStateBlock->scissorRect,
753                                            &This->scissorRect,
754                                            sizeof(targetStateBlock->scissorRect)))
755         {
756             TRACE("Updating scissor rect\n");
757             targetStateBlock->scissorRect = This->scissorRect;
758         }
759
760         map = This->changed.streamSource;
761         for (i = 0; map; map >>= 1, ++i)
762         {
763             if (!(map & 1)) continue;
764
765             if (This->streamStride[i] != targetStateBlock->streamStride[i]
766                     || This->streamSource[i] != targetStateBlock->streamSource[i])
767             {
768                 TRACE("Updating stream source %u to %p, stride to %u\n",
769                         i, targetStateBlock->streamSource[i], targetStateBlock->streamStride[i]);
770                 This->streamStride[i] = targetStateBlock->streamStride[i];
771                 if (targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
772                 if (This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
773                 This->streamSource[i] = targetStateBlock->streamSource[i];
774             }
775         }
776
777         map = This->changed.streamFreq;
778         for (i = 0; map; map >>= 1, ++i)
779         {
780             if (!(map & 1)) continue;
781
782             if (This->streamFreq[i] != targetStateBlock->streamFreq[i]
783                     || This->streamFlags[i] != targetStateBlock->streamFlags[i])
784             {
785                 TRACE("Updating stream frequency %u to %u flags to %#x\n",
786                         i, targetStateBlock->streamFreq[i], targetStateBlock->streamFlags[i]);
787                 This->streamFreq[i] = targetStateBlock->streamFreq[i];
788                 This->streamFlags[i] = targetStateBlock->streamFlags[i];
789             }
790         }
791
792         map = This->changed.clipplane;
793         for (i = 0; map; map >>= 1, ++i)
794         {
795             if (!(map & 1)) continue;
796
797             if (memcmp(targetStateBlock->clipplane[i], This->clipplane[i], sizeof(*This->clipplane)))
798             {
799                 TRACE("Updating clipplane %u\n", i);
800                 memcpy(This->clipplane[i], targetStateBlock->clipplane[i], sizeof(*This->clipplane));
801             }
802         }
803
804         /* Render */
805         for (i = 0; i < This->num_contained_render_states; i++) {
806             TRACE("Updating renderState %u to %u\n", This->contained_render_states[i],
807                     targetStateBlock->renderState[This->contained_render_states[i]]);
808             This->renderState[This->contained_render_states[i]] = targetStateBlock->renderState[This->contained_render_states[i]];
809         }
810
811         /* Texture states */
812         for (j = 0; j < This->num_contained_tss_states; j++) {
813             DWORD stage = This->contained_tss_states[j].stage;
814             DWORD state = This->contained_tss_states[j].state;
815
816             TRACE("Updating texturestage state %u, %u to %u (was %u)\n", stage, state,
817                     targetStateBlock->textureState[stage][state], This->textureState[stage][state]);
818             This->textureState[stage][state] = targetStateBlock->textureState[stage][state];
819         }
820
821         /* Samplers */
822         map = This->changed.textures;
823         for (i = 0; map; map >>= 1, ++i)
824         {
825             if (!(map & 1)) continue;
826
827             TRACE("Updating texture %u to %p (was %p)\n", i, targetStateBlock->textures[i], This->textures[i]);
828             if (targetStateBlock->textures[i]) IWineD3DBaseTexture_AddRef(targetStateBlock->textures[i]);
829             if (This->textures[i]) IWineD3DBaseTexture_Release(This->textures[i]);
830             This->textures[i] = targetStateBlock->textures[i];
831         }
832
833         for (j = 0; j < This->num_contained_sampler_states; j++) {
834             DWORD stage = This->contained_sampler_states[j].stage;
835             DWORD state = This->contained_sampler_states[j].state;
836             TRACE("Updating sampler state %u, %u to %u (was %u)\n", stage, state,
837                     targetStateBlock->samplerState[stage][state], This->samplerState[stage][state]);
838             This->samplerState[stage][state] = targetStateBlock->samplerState[stage][state];
839         }
840         if(This->changed.pixelShader && This->pixelShader != targetStateBlock->pixelShader) {
841             if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
842             if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
843             This->pixelShader = targetStateBlock->pixelShader;
844         }
845
846         record_lights(This, targetStateBlock);
847     } else if(This->blockType == WINED3DSBT_ALL) {
848         memcpy(This->vertexShaderConstantB, targetStateBlock->vertexShaderConstantB, sizeof(This->vertexShaderConstantI));
849         memcpy(This->vertexShaderConstantI, targetStateBlock->vertexShaderConstantI, sizeof(This->vertexShaderConstantF));
850         memcpy(This->vertexShaderConstantF, targetStateBlock->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
851         This->gl_primitive_type = targetStateBlock->gl_primitive_type;
852         memcpy(This->streamStride, targetStateBlock->streamStride, sizeof(This->streamStride));
853         memcpy(This->streamOffset, targetStateBlock->streamOffset, sizeof(This->streamOffset));
854         memcpy(This->streamFreq, targetStateBlock->streamFreq, sizeof(This->streamFreq));
855         memcpy(This->streamFlags, targetStateBlock->streamFlags, sizeof(This->streamFlags));
856         This->baseVertexIndex = targetStateBlock->baseVertexIndex;
857         memcpy(This->transforms, targetStateBlock->transforms, sizeof(This->transforms));
858         record_lights(This, targetStateBlock);
859         memcpy(This->clipplane, targetStateBlock->clipplane, sizeof(This->clipplane));
860         This->clip_status = targetStateBlock->clip_status;
861         This->viewport = targetStateBlock->viewport;
862         This->material = targetStateBlock->material;
863         memcpy(This->pixelShaderConstantB, targetStateBlock->pixelShaderConstantB, sizeof(This->pixelShaderConstantI));
864         memcpy(This->pixelShaderConstantI, targetStateBlock->pixelShaderConstantI, sizeof(This->pixelShaderConstantF));
865         memcpy(This->pixelShaderConstantF, targetStateBlock->pixelShaderConstantF, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
866         memcpy(This->renderState, targetStateBlock->renderState, sizeof(This->renderState));
867         memcpy(This->textureState, targetStateBlock->textureState, sizeof(This->textureState));
868         memcpy(This->samplerState, targetStateBlock->samplerState, sizeof(This->samplerState));
869         This->scissorRect = targetStateBlock->scissorRect;
870
871         if (This->vertexDecl != targetStateBlock->vertexDecl)
872         {
873             if (targetStateBlock->vertexDecl) IWineD3DVertexDeclaration_AddRef(targetStateBlock->vertexDecl);
874             if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
875             This->vertexDecl = targetStateBlock->vertexDecl;
876         }
877
878         for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
879         {
880             if (targetStateBlock->textures[i] != This->textures[i])
881             {
882                 if (targetStateBlock->textures[i]) IWineD3DBaseTexture_AddRef(targetStateBlock->textures[i]);
883                 if (This->textures[i]) IWineD3DBaseTexture_Release(This->textures[i]);
884                 This->textures[i] = targetStateBlock->textures[i];
885             }
886         }
887
888         if(targetStateBlock->pIndexData != This->pIndexData ||
889            targetStateBlock->IndexFmt != This->IndexFmt) {
890             if (targetStateBlock->pIndexData) IWineD3DBuffer_AddRef(targetStateBlock->pIndexData);
891             if (This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
892             This->pIndexData = targetStateBlock->pIndexData;
893             This->IndexFmt = targetStateBlock->IndexFmt;
894         }
895         for(i = 0; i < MAX_STREAMS; i++) {
896             if(targetStateBlock->streamSource[i] != This->streamSource[i]) {
897                 if(targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
898                 if(This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
899                 This->streamSource[i] = targetStateBlock->streamSource[i];
900             }
901         }
902         if(This->vertexShader != targetStateBlock->vertexShader) {
903             if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
904             if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
905             This->vertexShader = targetStateBlock->vertexShader;
906         }
907         if(This->pixelShader != targetStateBlock->pixelShader) {
908             if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
909             if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
910             This->pixelShader = targetStateBlock->pixelShader;
911         }
912     } else if(This->blockType == WINED3DSBT_VERTEXSTATE) {
913         memcpy(This->vertexShaderConstantB, targetStateBlock->vertexShaderConstantB, sizeof(This->vertexShaderConstantI));
914         memcpy(This->vertexShaderConstantI, targetStateBlock->vertexShaderConstantI, sizeof(This->vertexShaderConstantF));
915         memcpy(This->vertexShaderConstantF, targetStateBlock->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
916         record_lights(This, targetStateBlock);
917         for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
918         {
919             This->renderState[vertex_states_render[i]] = targetStateBlock->renderState[vertex_states_render[i]];
920         }
921         for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
922             for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
923             {
924                 This->samplerState[j][vertex_states_sampler[i]] = targetStateBlock->samplerState[j][vertex_states_sampler[i]];
925             }
926         }
927         for (j = 0; j < MAX_TEXTURES; j++) {
928             for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
929             {
930                 This->textureState[j][vertex_states_render[i]] = targetStateBlock->textureState[j][vertex_states_render[i]];
931             }
932         }
933         for(i = 0; i < MAX_STREAMS; i++) {
934             if(targetStateBlock->streamSource[i] != This->streamSource[i]) {
935                 if (targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
936                 if (This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
937                 This->streamSource[i] = targetStateBlock->streamSource[i];
938             }
939         }
940         if(This->vertexShader != targetStateBlock->vertexShader) {
941             if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
942             if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
943             This->vertexShader = targetStateBlock->vertexShader;
944         }
945     } else if(This->blockType == WINED3DSBT_PIXELSTATE) {
946         memcpy(This->pixelShaderConstantB, targetStateBlock->pixelShaderConstantB, sizeof(This->pixelShaderConstantI));
947         memcpy(This->pixelShaderConstantI, targetStateBlock->pixelShaderConstantI, sizeof(This->pixelShaderConstantF));
948         memcpy(This->pixelShaderConstantF, targetStateBlock->pixelShaderConstantF, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
949         for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
950         {
951             This->renderState[pixel_states_render[i]] = targetStateBlock->renderState[pixel_states_render[i]];
952         }
953         for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
954             for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
955             {
956                 This->samplerState[j][pixel_states_sampler[i]] = targetStateBlock->samplerState[j][pixel_states_sampler[i]];
957             }
958         }
959         for (j = 0; j < MAX_TEXTURES; j++) {
960             for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
961             {
962                 This->textureState[j][pixel_states_render[i]] = targetStateBlock->textureState[j][pixel_states_render[i]];
963             }
964         }
965         if(This->pixelShader != targetStateBlock->pixelShader) {
966             if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
967             if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
968             This->pixelShader = targetStateBlock->pixelShader;
969         }
970     }
971
972     TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
973
974     return WINED3D_OK;
975 }
976
977 static void apply_lights(IWineD3DDevice *pDevice, const IWineD3DStateBlockImpl *This)
978 {
979     UINT i;
980     for(i = 0; i < LIGHTMAP_SIZE; i++) {
981         struct list *e;
982
983         LIST_FOR_EACH(e, &This->lightMap[i]) {
984             const PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
985
986             if(light->changed) {
987                 IWineD3DDevice_SetLight(pDevice, light->OriginalIndex, &light->OriginalParms);
988             }
989             if(light->enabledChanged) {
990                 IWineD3DDevice_SetLightEnable(pDevice, light->OriginalIndex, light->glIndex != -1);
991             }
992         }
993     }
994 }
995
996 static HRESULT  WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
997     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
998     IWineD3DDevice*        pDevice     = (IWineD3DDevice*)This->wineD3DDevice;
999
1000 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
1001 should really perform a delta so that only the changes get updated*/
1002
1003     UINT i;
1004     UINT j;
1005     DWORD map;
1006
1007     TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
1008
1009     TRACE("Blocktype: %d\n", This->blockType);
1010
1011     if(This->blockType == WINED3DSBT_RECORDED) {
1012         if (This->changed.vertexShader) {
1013             IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
1014         }
1015         /* Vertex Shader Constants */
1016         for (i = 0; i < This->num_contained_vs_consts_f; i++) {
1017             IWineD3DDevice_SetVertexShaderConstantF(pDevice, This->contained_vs_consts_f[i],
1018                     This->vertexShaderConstantF + This->contained_vs_consts_f[i] * 4, 1);
1019         }
1020         for (i = 0; i < This->num_contained_vs_consts_i; i++) {
1021             IWineD3DDevice_SetVertexShaderConstantI(pDevice, This->contained_vs_consts_i[i],
1022                     This->vertexShaderConstantI + This->contained_vs_consts_i[i] * 4, 1);
1023         }
1024         for (i = 0; i < This->num_contained_vs_consts_b; i++) {
1025             IWineD3DDevice_SetVertexShaderConstantB(pDevice, This->contained_vs_consts_b[i],
1026                     This->vertexShaderConstantB + This->contained_vs_consts_b[i], 1);
1027         }
1028
1029         apply_lights(pDevice, This);
1030
1031         if (This->changed.pixelShader) {
1032             IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
1033         }
1034         /* Pixel Shader Constants */
1035         for (i = 0; i < This->num_contained_ps_consts_f; i++) {
1036             IWineD3DDevice_SetPixelShaderConstantF(pDevice, This->contained_ps_consts_f[i],
1037                     This->pixelShaderConstantF + This->contained_ps_consts_f[i] * 4, 1);
1038         }
1039         for (i = 0; i < This->num_contained_ps_consts_i; i++) {
1040             IWineD3DDevice_SetPixelShaderConstantI(pDevice, This->contained_ps_consts_i[i],
1041                     This->pixelShaderConstantI + This->contained_ps_consts_i[i] * 4, 1);
1042         }
1043         for (i = 0; i < This->num_contained_ps_consts_b; i++) {
1044             IWineD3DDevice_SetPixelShaderConstantB(pDevice, This->contained_ps_consts_b[i],
1045                     This->pixelShaderConstantB + This->contained_ps_consts_b[i], 1);
1046         }
1047
1048         /* Render */
1049         for (i = 0; i <= This->num_contained_render_states; i++) {
1050             IWineD3DDevice_SetRenderState(pDevice, This->contained_render_states[i],
1051                                           This->renderState[This->contained_render_states[i]]);
1052         }
1053         /* Texture states */
1054         for (i = 0; i < This->num_contained_tss_states; i++) {
1055             DWORD stage = This->contained_tss_states[i].stage;
1056             DWORD state = This->contained_tss_states[i].state;
1057             ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[stage][state]         = This->textureState[stage][state];
1058             ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[stage] |= 1 << state;
1059             /* TODO: Record a display list to apply all gl states. For now apply by brute force */
1060             IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(stage, state));
1061         }
1062         /* Sampler states */
1063         for (i = 0; i < This->num_contained_sampler_states; i++) {
1064             DWORD stage = This->contained_sampler_states[i].stage;
1065             DWORD state = This->contained_sampler_states[i].state;
1066             ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[stage][state]         = This->samplerState[stage][state];
1067             ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[stage] |= 1 << state;
1068             IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_SAMPLER(stage));
1069         }
1070
1071         for (i = 0; i < This->num_contained_transform_states; i++) {
1072             IWineD3DDevice_SetTransform(pDevice, This->contained_transform_states[i],
1073                                         &This->transforms[This->contained_transform_states[i]]);
1074         }
1075
1076         if (This->changed.primitive_type)
1077         {
1078                 This->wineD3DDevice->updateStateBlock->changed.primitive_type = TRUE;
1079                 This->wineD3DDevice->updateStateBlock->gl_primitive_type = This->gl_primitive_type;
1080         }
1081
1082         if (This->changed.indices)
1083         {
1084             IWineD3DDevice_SetIndexBuffer(pDevice, This->pIndexData, This->IndexFmt);
1085             IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
1086         }
1087
1088         if (This->changed.vertexDecl) {
1089             IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
1090         }
1091
1092         if (This->changed.material ) {
1093             IWineD3DDevice_SetMaterial(pDevice, &This->material);
1094         }
1095
1096         if (This->changed.viewport) {
1097             IWineD3DDevice_SetViewport(pDevice, &This->viewport);
1098         }
1099
1100         if (This->changed.scissorRect) {
1101             IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
1102         }
1103
1104         /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
1105         map = This->changed.streamSource;
1106         for (i = 0; map; map >>= 1, ++i)
1107         {
1108             if (map & 1) IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
1109         }
1110
1111         map = This->changed.streamFreq;
1112         for (i = 0; map; map >>= 1, ++i)
1113         {
1114             if (map & 1) IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
1115         }
1116
1117         map = This->changed.textures;
1118         for (i = 0; map; map >>= 1, ++i)
1119         {
1120             if (!(map & 1)) continue;
1121
1122             if (i < MAX_FRAGMENT_SAMPLERS) IWineD3DDevice_SetTexture(pDevice, i, This->textures[i]);
1123             else IWineD3DDevice_SetTexture(pDevice, WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS,
1124                     This->textures[i]);
1125         }
1126
1127         map = This->changed.clipplane;
1128         for (i = 0; map; map >>= 1, ++i)
1129         {
1130             float clip[4];
1131
1132             if (!(map & 1)) continue;
1133
1134             clip[0] = This->clipplane[i][0];
1135             clip[1] = This->clipplane[i][1];
1136             clip[2] = This->clipplane[i][2];
1137             clip[3] = This->clipplane[i][3];
1138             IWineD3DDevice_SetClipPlane(pDevice, i, clip);
1139         }
1140     } else if(This->blockType == WINED3DSBT_VERTEXSTATE) {
1141         IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
1142         for (i = 0; i < GL_LIMITS(vshader_constantsF); i++) {
1143             IWineD3DDevice_SetVertexShaderConstantF(pDevice, i,
1144                     This->vertexShaderConstantF + i * 4, 1);
1145         }
1146         for (i = 0; i < MAX_CONST_I; i++) {
1147             IWineD3DDevice_SetVertexShaderConstantI(pDevice, i,
1148                     This->vertexShaderConstantI + i * 4, 1);
1149         }
1150         for (i = 0; i < MAX_CONST_B; i++) {
1151             IWineD3DDevice_SetVertexShaderConstantB(pDevice, i,
1152                     This->vertexShaderConstantB + i, 1);
1153         }
1154
1155         apply_lights(pDevice, This);
1156
1157         for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
1158         {
1159             IWineD3DDevice_SetRenderState(pDevice, vertex_states_render[i], This->renderState[vertex_states_render[i]]);
1160         }
1161         for(j = 0; j < MAX_TEXTURES; j++) {
1162             for (i = 0; i < sizeof(vertex_states_texture) / sizeof(*vertex_states_texture); ++i)
1163             {
1164                 IWineD3DDevice_SetTextureStageState(pDevice, j, vertex_states_texture[i],
1165                         This->textureState[j][vertex_states_texture[i]]);
1166             }
1167         }
1168
1169         for(j = 0; j < MAX_FRAGMENT_SAMPLERS; j++) {
1170             for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
1171             {
1172                 IWineD3DDevice_SetSamplerState(pDevice, j, vertex_states_sampler[i],
1173                         This->samplerState[j][vertex_states_sampler[i]]);
1174             }
1175         }
1176         for(j = MAX_FRAGMENT_SAMPLERS; j < MAX_COMBINED_SAMPLERS; j++) {
1177             for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
1178             {
1179                 IWineD3DDevice_SetSamplerState(pDevice, WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS,
1180                         vertex_states_sampler[i], This->samplerState[j][vertex_states_sampler[i]]);
1181             }
1182         }
1183     } else if(This->blockType == WINED3DSBT_PIXELSTATE) {
1184         IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
1185         for (i = 0; i < GL_LIMITS(pshader_constantsF); i++) {
1186             IWineD3DDevice_SetPixelShaderConstantF(pDevice, i,
1187                     This->pixelShaderConstantF + i * 4, 1);
1188         }
1189         for (i = 0; i < MAX_CONST_I; i++) {
1190             IWineD3DDevice_SetPixelShaderConstantI(pDevice, i,
1191                     This->pixelShaderConstantI + i * 4, 1);
1192         }
1193         for (i = 0; i < MAX_CONST_B; i++) {
1194             IWineD3DDevice_SetPixelShaderConstantB(pDevice, i,
1195                     This->pixelShaderConstantB + i, 1);
1196         }
1197
1198         for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
1199         {
1200             IWineD3DDevice_SetRenderState(pDevice, pixel_states_render[i], This->renderState[pixel_states_render[i]]);
1201         }
1202         for(j = 0; j < MAX_TEXTURES; j++) {
1203             for (i = 0; i < sizeof(pixel_states_texture) / sizeof(*pixel_states_texture); ++i)
1204             {
1205                 IWineD3DDevice_SetTextureStageState(pDevice, j, pixel_states_texture[i],
1206                         This->textureState[j][pixel_states_texture[i]]);
1207             }
1208         }
1209
1210         for(j = 0; j < MAX_FRAGMENT_SAMPLERS; j++) {
1211             for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
1212             {
1213                 IWineD3DDevice_SetSamplerState(pDevice, j, pixel_states_sampler[i],
1214                         This->samplerState[j][pixel_states_sampler[i]]);
1215             }
1216         }
1217         for(j = MAX_FRAGMENT_SAMPLERS; j < MAX_COMBINED_SAMPLERS; j++) {
1218             for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
1219             {
1220                 IWineD3DDevice_SetSamplerState(pDevice, WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS,
1221                         pixel_states_sampler[i], This->samplerState[j][pixel_states_sampler[i]]);
1222             }
1223         }
1224     } else if(This->blockType == WINED3DSBT_ALL) {
1225         IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
1226         for (i = 0; i < GL_LIMITS(vshader_constantsF); i++) {
1227             IWineD3DDevice_SetVertexShaderConstantF(pDevice, i,
1228                     This->vertexShaderConstantF + i * 4, 1);
1229         }
1230         for (i = 0; i < MAX_CONST_I; i++) {
1231             IWineD3DDevice_SetVertexShaderConstantI(pDevice, i,
1232                     This->vertexShaderConstantI + i * 4, 1);
1233         }
1234         for (i = 0; i < MAX_CONST_B; i++) {
1235             IWineD3DDevice_SetVertexShaderConstantB(pDevice, i,
1236                     This->vertexShaderConstantB + i, 1);
1237         }
1238
1239         IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
1240         for (i = 0; i < GL_LIMITS(pshader_constantsF); i++) {
1241             IWineD3DDevice_SetPixelShaderConstantF(pDevice, i,
1242                     This->pixelShaderConstantF + i * 4, 1);
1243         }
1244         for (i = 0; i < MAX_CONST_I; i++) {
1245             IWineD3DDevice_SetPixelShaderConstantI(pDevice, i,
1246                     This->pixelShaderConstantI + i * 4, 1);
1247         }
1248         for (i = 0; i < MAX_CONST_B; i++) {
1249             IWineD3DDevice_SetPixelShaderConstantB(pDevice, i,
1250                     This->pixelShaderConstantB + i, 1);
1251         }
1252
1253         apply_lights(pDevice, This);
1254
1255         for(i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
1256             IWineD3DDevice_SetRenderState(pDevice, i, This->renderState[i]);
1257         }
1258         for(j = 0; j < MAX_TEXTURES; j++) {
1259             for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
1260             {
1261                 IWineD3DDevice_SetTextureStageState(pDevice, j, i, This->textureState[j][i]);
1262             }
1263         }
1264
1265         /* Skip unused values between TEXTURE8 and WORLD0 ? */
1266         for(i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
1267             IWineD3DDevice_SetTransform(pDevice, i, &This->transforms[i]);
1268         }
1269         This->wineD3DDevice->updateStateBlock->gl_primitive_type = This->gl_primitive_type;
1270         IWineD3DDevice_SetIndexBuffer(pDevice, This->pIndexData, This->IndexFmt);
1271         IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
1272         IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
1273         IWineD3DDevice_SetMaterial(pDevice, &This->material);
1274         IWineD3DDevice_SetViewport(pDevice, &This->viewport);
1275         IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
1276
1277         /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
1278         for (i=0; i<MAX_STREAMS; i++) {
1279             IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
1280             IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
1281         }
1282         for (j = 0 ; j < MAX_COMBINED_SAMPLERS; j++){
1283             UINT sampler = j < MAX_FRAGMENT_SAMPLERS ? j : WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS;
1284
1285             IWineD3DDevice_SetTexture(pDevice, sampler, This->textures[j]);
1286             for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; ++i)
1287             {
1288                 IWineD3DDevice_SetSamplerState(pDevice, sampler, i, This->samplerState[j][i]);
1289             }
1290         }
1291         for (i = 0; i < GL_LIMITS(clipplanes); i++) {
1292             float clip[4];
1293
1294             clip[0] = This->clipplane[i][0];
1295             clip[1] = This->clipplane[i][1];
1296             clip[2] = This->clipplane[i][2];
1297             clip[3] = This->clipplane[i][3];
1298             IWineD3DDevice_SetClipPlane(pDevice, i, clip);
1299         }
1300     }
1301
1302     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = MAX_TEXTURES - 1;
1303     for(j = 0; j < MAX_TEXTURES - 1; j++) {
1304         if(((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE) {
1305             ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = j;
1306             break;
1307         }
1308     }
1309     TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
1310
1311     return WINED3D_OK;
1312 }
1313
1314 static HRESULT  WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
1315     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
1316     IWineD3DDevice         *device = (IWineD3DDevice *)This->wineD3DDevice;
1317     IWineD3DDeviceImpl     *ThisDevice = (IWineD3DDeviceImpl *)device;
1318     union {
1319         WINED3DLINEPATTERN lp;
1320         DWORD d;
1321     } lp;
1322     union {
1323         float f;
1324         DWORD d;
1325     } tmpfloat;
1326     unsigned int i;
1327     IWineD3DSwapChain *swapchain;
1328     IWineD3DSurface *backbuffer;
1329     HRESULT hr;
1330
1331     /* Note this may have a large overhead but it should only be executed
1332        once, in order to initialize the complete state of the device and
1333        all opengl equivalents                                            */
1334     TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
1335     /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
1336     This->blockType = WINED3DSBT_INIT;
1337
1338     /* Set some of the defaults for lights, transforms etc */
1339     memcpy(&This->transforms[WINED3DTS_PROJECTION], identity, sizeof(identity));
1340     memcpy(&This->transforms[WINED3DTS_VIEW], identity, sizeof(identity));
1341     for (i = 0; i < 256; ++i) {
1342       memcpy(&This->transforms[WINED3DTS_WORLDMATRIX(i)], identity, sizeof(identity));
1343     }
1344
1345     TRACE("Render states\n");
1346     /* Render states: */
1347     if (ThisDevice->auto_depth_stencil_buffer != NULL) {
1348        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       WINED3DZB_TRUE);
1349     } else {
1350        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       WINED3DZB_FALSE);
1351     }
1352     IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE,         WINED3DFILL_SOLID);
1353     IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE,        WINED3DSHADE_GOURAUD);
1354     lp.lp.wRepeatFactor = 0;
1355     lp.lp.wLinePattern  = 0;
1356     IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN,      lp.d);
1357     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE,     TRUE);
1358     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE,  FALSE);
1359     IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL,        TRUE);
1360     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND,         WINED3DBLEND_ONE);
1361     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND,        WINED3DBLEND_ZERO);
1362     IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE,         WINED3DCULL_CCW);
1363     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC,            WINED3DCMP_LESSEQUAL);
1364     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC,        WINED3DCMP_ALWAYS);
1365     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF,         0);
1366     IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE,     FALSE);
1367     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
1368     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE,        FALSE);
1369     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE,   FALSE);
1370     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE,         0);
1371     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR,         0);
1372     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE,     WINED3DFOG_NONE);
1373     tmpfloat.f = 0.0f;
1374     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART,         tmpfloat.d);
1375     tmpfloat.f = 1.0f;
1376     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND,           tmpfloat.d);
1377     tmpfloat.f = 1.0f;
1378     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY,       tmpfloat.d);
1379     IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS,    FALSE);
1380     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS,            0);
1381     IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE,   FALSE);
1382     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE,    FALSE);
1383     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL,      WINED3DSTENCILOP_KEEP);
1384     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL,     WINED3DSTENCILOP_KEEP);
1385     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS,      WINED3DSTENCILOP_KEEP);
1386     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF,       0);
1387     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK,      0xFFFFFFFF);
1388     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC,      WINED3DCMP_ALWAYS);
1389     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
1390     IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR,    0xFFFFFFFF);
1391     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
1392     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
1393     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
1394     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
1395     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
1396     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
1397     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
1398     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
1399     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING,                 TRUE);
1400     IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING,                 TRUE);
1401     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT,                  0);
1402     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE,            WINED3DFOG_NONE);
1403     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX,              TRUE);
1404     IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER,              TRUE);
1405     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS,         FALSE);
1406     IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE,    WINED3DMCS_COLOR1);
1407     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE,   WINED3DMCS_COLOR2);
1408     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE,    WINED3DMCS_MATERIAL);
1409     IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE,   WINED3DMCS_MATERIAL);
1410     IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND,              WINED3DVBF_DISABLE);
1411     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE,          0);
1412     IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
1413     tmpfloat.f = 1.0f;
1414     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE,                tmpfloat.d);
1415     tmpfloat.f = ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion < 9 ? 0.0f : 1.0f;
1416     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN,            tmpfloat.d);
1417     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE,        FALSE);
1418     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE,         FALSE);
1419     tmpfloat.f = 1.0f;
1420     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A,             tmpfloat.d);
1421     tmpfloat.f = 0.0f;
1422     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B,             tmpfloat.d);
1423     tmpfloat.f = 0.0f;
1424     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C,             tmpfloat.d);
1425     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS,     TRUE);
1426     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK,          0xFFFFFFFF);
1427     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE,           WINED3DPATCHEDGE_DISCRETE);
1428     tmpfloat.f = 1.0f;
1429     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS,            tmpfloat.d);
1430     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN,        0xbaadcafe);
1431     tmpfloat.f = GL_LIMITS(pointsize);
1432     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX,            tmpfloat.d);
1433     IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
1434     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE,         0x0000000F);
1435     tmpfloat.f = 0.0f;
1436     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR,              tmpfloat.d);
1437     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP,                  WINED3DBLENDOP_ADD);
1438     IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE,           WINED3DDEGREE_CUBIC);
1439     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE,             WINED3DDEGREE_LINEAR);
1440     /* states new in d3d9 */
1441     IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE,        FALSE);
1442     IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS,      0);
1443     tmpfloat.f = 1.0f;
1444     IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL,     tmpfloat.d);
1445     IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL,     tmpfloat.d);
1446     IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE,    FALSE);
1447     tmpfloat.f = 0.0f;
1448     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X,           tmpfloat.d);
1449     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y,           tmpfloat.d);
1450     tmpfloat.f = 1.0f;
1451     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z,           tmpfloat.d);
1452     tmpfloat.f = 0.0f;
1453     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W,           tmpfloat.d);
1454     IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
1455     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE,      FALSE);
1456     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL,          WINED3DSTENCILOP_KEEP);
1457     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL,         WINED3DSTENCILOP_KEEP);
1458     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS,          WINED3DSTENCILOP_KEEP);
1459     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC,          WINED3DCMP_ALWAYS);
1460     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1,        0x0000000F);
1461     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2,        0x0000000F);
1462     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3,        0x0000000F);
1463     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR,              0xFFFFFFFF);
1464     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE,          0);
1465     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS,                0);
1466     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8,  0);
1467     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9,  0);
1468     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
1469     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
1470     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
1471     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
1472     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
1473     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
1474     IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1475     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA,            WINED3DBLEND_ONE);
1476     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA,           WINED3DBLEND_ZERO);
1477     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA,             WINED3DBLENDOP_ADD);
1478
1479     /* clipping status */
1480     This->clip_status.ClipUnion = 0;
1481     This->clip_status.ClipIntersection = 0xFFFFFFFF;
1482
1483     /* Texture Stage States - Put directly into state block, we will call function below */
1484     for (i = 0; i < MAX_TEXTURES; i++) {
1485         TRACE("Setting up default texture states for texture Stage %d\n", i);
1486         memcpy(&This->transforms[WINED3DTS_TEXTURE0 + i], identity, sizeof(identity));
1487         This->textureState[i][WINED3DTSS_COLOROP               ] = (i==0)? WINED3DTOP_MODULATE :  WINED3DTOP_DISABLE;
1488         This->textureState[i][WINED3DTSS_COLORARG1             ] = WINED3DTA_TEXTURE;
1489         This->textureState[i][WINED3DTSS_COLORARG2             ] = WINED3DTA_CURRENT;
1490         This->textureState[i][WINED3DTSS_ALPHAOP               ] = (i==0)? WINED3DTOP_SELECTARG1 :  WINED3DTOP_DISABLE;
1491         This->textureState[i][WINED3DTSS_ALPHAARG1             ] = WINED3DTA_TEXTURE;
1492         This->textureState[i][WINED3DTSS_ALPHAARG2             ] = WINED3DTA_CURRENT;
1493         This->textureState[i][WINED3DTSS_BUMPENVMAT00          ] = 0;
1494         This->textureState[i][WINED3DTSS_BUMPENVMAT01          ] = 0;
1495         This->textureState[i][WINED3DTSS_BUMPENVMAT10          ] = 0;
1496         This->textureState[i][WINED3DTSS_BUMPENVMAT11          ] = 0;
1497         This->textureState[i][WINED3DTSS_TEXCOORDINDEX         ] = i;
1498         This->textureState[i][WINED3DTSS_BUMPENVLSCALE         ] = 0;
1499         This->textureState[i][WINED3DTSS_BUMPENVLOFFSET        ] = 0;
1500         This->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS ] = WINED3DTTFF_DISABLE;
1501         This->textureState[i][WINED3DTSS_COLORARG0             ] = WINED3DTA_CURRENT;
1502         This->textureState[i][WINED3DTSS_ALPHAARG0             ] = WINED3DTA_CURRENT;
1503         This->textureState[i][WINED3DTSS_RESULTARG             ] = WINED3DTA_CURRENT;
1504     }
1505     This->lowest_disabled_stage = 1;
1506
1507         /* Sampler states*/
1508     for (i = 0 ; i <  MAX_COMBINED_SAMPLERS; i++) {
1509         TRACE("Setting up default samplers states for sampler %d\n", i);
1510         This->samplerState[i][WINED3DSAMP_ADDRESSU         ] = WINED3DTADDRESS_WRAP;
1511         This->samplerState[i][WINED3DSAMP_ADDRESSV         ] = WINED3DTADDRESS_WRAP;
1512         This->samplerState[i][WINED3DSAMP_ADDRESSW         ] = WINED3DTADDRESS_WRAP;
1513         This->samplerState[i][WINED3DSAMP_BORDERCOLOR      ] = 0x00;
1514         This->samplerState[i][WINED3DSAMP_MAGFILTER        ] = WINED3DTEXF_POINT;
1515         This->samplerState[i][WINED3DSAMP_MINFILTER        ] = WINED3DTEXF_POINT;
1516         This->samplerState[i][WINED3DSAMP_MIPFILTER        ] = WINED3DTEXF_NONE;
1517         This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS    ] = 0;
1518         This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL      ] = 0;
1519         This->samplerState[i][WINED3DSAMP_MAXANISOTROPY    ] = 1;
1520         This->samplerState[i][WINED3DSAMP_SRGBTEXTURE      ] = 0;
1521         This->samplerState[i][WINED3DSAMP_ELEMENTINDEX     ] = 0; /* TODO: Indicates which element of a  multielement texture to use */
1522         This->samplerState[i][WINED3DSAMP_DMAPOFFSET       ] = 0; /* TODO: Vertex offset in the presampled displacement map */
1523     }
1524
1525     for(i = 0; i < GL_LIMITS(textures); i++) {
1526         /* Note: This avoids calling SetTexture, so pretend it has been called */
1527         This->changed.textures |= 1 << i;
1528         This->textures[i]         = NULL;
1529     }
1530
1531     /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1532     hr = IWineD3DDevice_GetSwapChain(device, 0, &swapchain);
1533     if( hr == WINED3D_OK && swapchain != NULL) {
1534         WINED3DVIEWPORT vp;
1535
1536         hr = IWineD3DSwapChain_GetBackBuffer(swapchain, 0, WINED3DBACKBUFFER_TYPE_MONO, &backbuffer);
1537         if (SUCCEEDED(hr) && backbuffer)
1538         {
1539             WINED3DSURFACE_DESC desc;
1540             RECT scissorrect;
1541
1542             IWineD3DSurface_GetDesc(backbuffer, &desc);
1543             IWineD3DSurface_Release(backbuffer);
1544
1545             /* Set the default scissor rect values */
1546             scissorrect.left = 0;
1547             scissorrect.right = desc.width;
1548             scissorrect.top = 0;
1549             scissorrect.bottom = desc.height;
1550             hr = IWineD3DDevice_SetScissorRect(device, &scissorrect);
1551             if (FAILED(hr)) ERR("This should never happen, expect rendering issues!\n");
1552         }
1553
1554         /* Set the default viewport */
1555         vp.X      = 0;
1556         vp.Y      = 0;
1557         vp.Width  = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferWidth;
1558         vp.Height = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferHeight;
1559         vp.MinZ   = 0.0f;
1560         vp.MaxZ   = 1.0f;
1561         IWineD3DDevice_SetViewport(device, &vp);
1562
1563         IWineD3DSwapChain_Release(swapchain);
1564     }
1565
1566     TRACE("-----------------------> Device defaults now set up...\n");
1567     return WINED3D_OK;
1568 }
1569
1570 /**********************************************************
1571  * IWineD3DStateBlock VTbl follows
1572  **********************************************************/
1573
1574 static const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
1575 {
1576     /* IUnknown */
1577     IWineD3DStateBlockImpl_QueryInterface,
1578     IWineD3DStateBlockImpl_AddRef,
1579     IWineD3DStateBlockImpl_Release,
1580     /* IWineD3DStateBlock */
1581     IWineD3DStateBlockImpl_GetParent,
1582     IWineD3DStateBlockImpl_GetDevice,
1583     IWineD3DStateBlockImpl_Capture,
1584     IWineD3DStateBlockImpl_Apply,
1585     IWineD3DStateBlockImpl_InitStartupStateBlock
1586 };
1587
1588 HRESULT stateblock_init(IWineD3DStateBlockImpl *stateblock, IWineD3DDeviceImpl *device,
1589         WINED3DSTATEBLOCKTYPE type, IUnknown *parent)
1590 {
1591     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1592     unsigned int i;
1593     HRESULT hr;
1594
1595     stateblock->lpVtbl = &IWineD3DStateBlock_Vtbl;
1596     stateblock->ref = 1;
1597     stateblock->parent = parent;
1598     stateblock->wineD3DDevice = device;
1599     stateblock->blockType = type;
1600
1601     for (i = 0; i < LIGHTMAP_SIZE; i++)
1602     {
1603         list_init(&stateblock->lightMap[i]);
1604     }
1605
1606     hr = stateblock_allocate_shader_constants(stateblock);
1607     if (FAILED(hr)) return hr;
1608
1609     /* The WINED3DSBT_INIT stateblock type is used during initialization to
1610      * produce a placeholder stateblock so other functions called can update a
1611      * state block. */
1612     if (type == WINED3DSBT_INIT || type == WINED3DSBT_RECORDED) return WINED3D_OK;
1613
1614     /* Otherwise, might as well set the whole state block to the appropriate values  */
1615     if (device->stateBlock)
1616     {
1617         /* Saved values */
1618         stateblock_copy_values(stateblock, device->stateBlock, gl_info);
1619     }
1620     else
1621     {
1622         memset(stateblock->streamFreq, 1, sizeof(stateblock->streamFreq));
1623     }
1624
1625     TRACE("Updating changed flags appropriate for type %#x.\n", type);
1626
1627     if (type == WINED3DSBT_ALL)
1628     {
1629         TRACE("ALL => Pretend everything has changed.\n");
1630
1631         stateblock_savedstates_set_all(&stateblock->changed, gl_info);
1632         stateblock_init_contained_states(stateblock);
1633
1634         /* Lights are not part of the changed / set structure. */
1635         for (i = 0; i < LIGHTMAP_SIZE; ++i)
1636         {
1637             struct list *e;
1638             LIST_FOR_EACH(e, &stateblock->lightMap[i])
1639             {
1640                 PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
1641                 light->changed = TRUE;
1642                 light->enabledChanged = TRUE;
1643             }
1644         }
1645
1646         for (i = 0; i < MAX_STREAMS; ++i)
1647         {
1648             if (stateblock->streamSource[i]) IWineD3DBuffer_AddRef(stateblock->streamSource[i]);
1649         }
1650
1651         if (stateblock->pIndexData) IWineD3DBuffer_AddRef(stateblock->pIndexData);
1652         if (stateblock->vertexShader) IWineD3DVertexShader_AddRef(stateblock->vertexShader);
1653         if (stateblock->pixelShader) IWineD3DPixelShader_AddRef(stateblock->pixelShader);
1654     }
1655     else if (type == WINED3DSBT_PIXELSTATE)
1656     {
1657         TRACE("PIXELSTATE => Pretend all pixel states have changed.\n");
1658
1659         stateblock_savedstates_set_pixel(&stateblock->changed, gl_info);
1660         stateblock_init_contained_states(stateblock);
1661
1662         if (stateblock->pixelShader) IWineD3DPixelShader_AddRef(stateblock->pixelShader);
1663
1664         /* Pixel state blocks do not contain vertex buffers. Set them to NULL
1665          * to avoid wrong refcounting on them. This makes releasing the buffer
1666          * easier. */
1667         for (i = 0; i < MAX_STREAMS; ++i)
1668         {
1669             stateblock->streamSource[i] = NULL;
1670         }
1671         stateblock->pIndexData = NULL;
1672         stateblock->vertexShader = NULL;
1673     }
1674     else if (type == WINED3DSBT_VERTEXSTATE)
1675     {
1676         TRACE("VERTEXSTATE => Pretend all vertex shates have changed.\n");
1677
1678         stateblock_savedstates_set_vertex(&stateblock->changed, gl_info);
1679         stateblock_init_contained_states(stateblock);
1680
1681         for (i = 0; i < LIGHTMAP_SIZE; ++i)
1682         {
1683             struct list *e;
1684             LIST_FOR_EACH(e, &stateblock->lightMap[i])
1685             {
1686                 PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
1687                 light->changed = TRUE;
1688                 light->enabledChanged = TRUE;
1689             }
1690         }
1691
1692         for (i = 0; i < MAX_STREAMS; ++i)
1693         {
1694             if (stateblock->streamSource[i]) IWineD3DBuffer_AddRef(stateblock->streamSource[i]);
1695         }
1696
1697         if (stateblock->vertexShader) IWineD3DVertexShader_AddRef(stateblock->vertexShader);
1698
1699         stateblock->pIndexData = NULL;
1700         stateblock->pixelShader = NULL;
1701     }
1702     else
1703     {
1704         FIXME("Unrecognized state block type %#x.\n", type);
1705     }
1706
1707     return WINED3D_OK;
1708 }