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