wined3d: Remove COM from the device implementation.
[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
30 static const DWORD pixel_states_render[] =
31 {
32     WINED3DRS_ALPHABLENDENABLE,
33     WINED3DRS_ALPHAFUNC,
34     WINED3DRS_ALPHAREF,
35     WINED3DRS_ALPHATESTENABLE,
36     WINED3DRS_ANTIALIASEDLINEENABLE,
37     WINED3DRS_BLENDFACTOR,
38     WINED3DRS_BLENDOP,
39     WINED3DRS_BLENDOPALPHA,
40     WINED3DRS_CCW_STENCILFAIL,
41     WINED3DRS_CCW_STENCILPASS,
42     WINED3DRS_CCW_STENCILZFAIL,
43     WINED3DRS_COLORWRITEENABLE,
44     WINED3DRS_COLORWRITEENABLE1,
45     WINED3DRS_COLORWRITEENABLE2,
46     WINED3DRS_COLORWRITEENABLE3,
47     WINED3DRS_DEPTHBIAS,
48     WINED3DRS_DESTBLEND,
49     WINED3DRS_DESTBLENDALPHA,
50     WINED3DRS_DITHERENABLE,
51     WINED3DRS_FILLMODE,
52     WINED3DRS_FOGDENSITY,
53     WINED3DRS_FOGEND,
54     WINED3DRS_FOGSTART,
55     WINED3DRS_LASTPIXEL,
56     WINED3DRS_SCISSORTESTENABLE,
57     WINED3DRS_SEPARATEALPHABLENDENABLE,
58     WINED3DRS_SHADEMODE,
59     WINED3DRS_SLOPESCALEDEPTHBIAS,
60     WINED3DRS_SRCBLEND,
61     WINED3DRS_SRCBLENDALPHA,
62     WINED3DRS_SRGBWRITEENABLE,
63     WINED3DRS_STENCILENABLE,
64     WINED3DRS_STENCILFAIL,
65     WINED3DRS_STENCILFUNC,
66     WINED3DRS_STENCILMASK,
67     WINED3DRS_STENCILPASS,
68     WINED3DRS_STENCILREF,
69     WINED3DRS_STENCILWRITEMASK,
70     WINED3DRS_STENCILZFAIL,
71     WINED3DRS_TEXTUREFACTOR,
72     WINED3DRS_TWOSIDEDSTENCILMODE,
73     WINED3DRS_WRAP0,
74     WINED3DRS_WRAP1,
75     WINED3DRS_WRAP10,
76     WINED3DRS_WRAP11,
77     WINED3DRS_WRAP12,
78     WINED3DRS_WRAP13,
79     WINED3DRS_WRAP14,
80     WINED3DRS_WRAP15,
81     WINED3DRS_WRAP2,
82     WINED3DRS_WRAP3,
83     WINED3DRS_WRAP4,
84     WINED3DRS_WRAP5,
85     WINED3DRS_WRAP6,
86     WINED3DRS_WRAP7,
87     WINED3DRS_WRAP8,
88     WINED3DRS_WRAP9,
89     WINED3DRS_ZENABLE,
90     WINED3DRS_ZFUNC,
91     WINED3DRS_ZWRITEENABLE,
92 };
93
94 static const DWORD pixel_states_texture[] =
95 {
96     WINED3DTSS_ALPHAARG0,
97     WINED3DTSS_ALPHAARG1,
98     WINED3DTSS_ALPHAARG2,
99     WINED3DTSS_ALPHAOP,
100     WINED3DTSS_BUMPENVLOFFSET,
101     WINED3DTSS_BUMPENVLSCALE,
102     WINED3DTSS_BUMPENVMAT00,
103     WINED3DTSS_BUMPENVMAT01,
104     WINED3DTSS_BUMPENVMAT10,
105     WINED3DTSS_BUMPENVMAT11,
106     WINED3DTSS_COLORARG0,
107     WINED3DTSS_COLORARG1,
108     WINED3DTSS_COLORARG2,
109     WINED3DTSS_COLOROP,
110     WINED3DTSS_RESULTARG,
111     WINED3DTSS_TEXCOORDINDEX,
112     WINED3DTSS_TEXTURETRANSFORMFLAGS,
113 };
114
115 static const DWORD pixel_states_sampler[] =
116 {
117     WINED3DSAMP_ADDRESSU,
118     WINED3DSAMP_ADDRESSV,
119     WINED3DSAMP_ADDRESSW,
120     WINED3DSAMP_BORDERCOLOR,
121     WINED3DSAMP_MAGFILTER,
122     WINED3DSAMP_MINFILTER,
123     WINED3DSAMP_MIPFILTER,
124     WINED3DSAMP_MIPMAPLODBIAS,
125     WINED3DSAMP_MAXMIPLEVEL,
126     WINED3DSAMP_MAXANISOTROPY,
127     WINED3DSAMP_SRGBTEXTURE,
128     WINED3DSAMP_ELEMENTINDEX,
129 };
130
131 static const DWORD vertex_states_render[] =
132 {
133     WINED3DRS_ADAPTIVETESS_W,
134     WINED3DRS_ADAPTIVETESS_X,
135     WINED3DRS_ADAPTIVETESS_Y,
136     WINED3DRS_ADAPTIVETESS_Z,
137     WINED3DRS_AMBIENT,
138     WINED3DRS_AMBIENTMATERIALSOURCE,
139     WINED3DRS_CLIPPING,
140     WINED3DRS_CLIPPLANEENABLE,
141     WINED3DRS_COLORVERTEX,
142     WINED3DRS_CULLMODE,
143     WINED3DRS_DIFFUSEMATERIALSOURCE,
144     WINED3DRS_EMISSIVEMATERIALSOURCE,
145     WINED3DRS_ENABLEADAPTIVETESSELLATION,
146     WINED3DRS_FOGCOLOR,
147     WINED3DRS_FOGDENSITY,
148     WINED3DRS_FOGENABLE,
149     WINED3DRS_FOGEND,
150     WINED3DRS_FOGSTART,
151     WINED3DRS_FOGTABLEMODE,
152     WINED3DRS_FOGVERTEXMODE,
153     WINED3DRS_INDEXEDVERTEXBLENDENABLE,
154     WINED3DRS_LIGHTING,
155     WINED3DRS_LOCALVIEWER,
156     WINED3DRS_MAXTESSELLATIONLEVEL,
157     WINED3DRS_MINTESSELLATIONLEVEL,
158     WINED3DRS_MULTISAMPLEANTIALIAS,
159     WINED3DRS_MULTISAMPLEMASK,
160     WINED3DRS_NORMALDEGREE,
161     WINED3DRS_NORMALIZENORMALS,
162     WINED3DRS_PATCHEDGESTYLE,
163     WINED3DRS_POINTSCALE_A,
164     WINED3DRS_POINTSCALE_B,
165     WINED3DRS_POINTSCALE_C,
166     WINED3DRS_POINTSCALEENABLE,
167     WINED3DRS_POINTSIZE,
168     WINED3DRS_POINTSIZE_MAX,
169     WINED3DRS_POINTSIZE_MIN,
170     WINED3DRS_POINTSPRITEENABLE,
171     WINED3DRS_POSITIONDEGREE,
172     WINED3DRS_RANGEFOGENABLE,
173     WINED3DRS_SHADEMODE,
174     WINED3DRS_SPECULARENABLE,
175     WINED3DRS_SPECULARMATERIALSOURCE,
176     WINED3DRS_TWEENFACTOR,
177     WINED3DRS_VERTEXBLEND,
178 };
179
180 static const DWORD vertex_states_texture[] =
181 {
182     WINED3DTSS_TEXCOORDINDEX,
183     WINED3DTSS_TEXTURETRANSFORMFLAGS,
184 };
185
186 static const DWORD vertex_states_sampler[] =
187 {
188     WINED3DSAMP_DMAPOFFSET,
189 };
190
191 /* Allocates the correct amount of space for pixel and vertex shader constants,
192  * along with their set/changed flags on the given stateblock object
193  */
194 static HRESULT stateblock_allocate_shader_constants(struct wined3d_stateblock *object)
195 {
196     IWineD3DDeviceImpl *device = object->device;
197
198     /* Allocate space for floating point constants */
199     object->state.ps_consts_f = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
200             sizeof(float) * device->d3d_pshader_constantF * 4);
201     if (!object->state.ps_consts_f) goto fail;
202
203     object->changed.pixelShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
204             sizeof(BOOL) * device->d3d_pshader_constantF);
205     if (!object->changed.pixelShaderConstantsF) goto fail;
206
207     object->state.vs_consts_f = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
208             sizeof(float) * device->d3d_vshader_constantF * 4);
209     if (!object->state.vs_consts_f) goto fail;
210
211     object->changed.vertexShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
212             sizeof(BOOL) * device->d3d_vshader_constantF);
213     if (!object->changed.vertexShaderConstantsF) goto fail;
214
215     object->contained_vs_consts_f = HeapAlloc(GetProcessHeap(), 0,
216             sizeof(DWORD) * device->d3d_vshader_constantF);
217     if (!object->contained_vs_consts_f) goto fail;
218
219     object->contained_ps_consts_f = HeapAlloc(GetProcessHeap(), 0,
220             sizeof(DWORD) * device->d3d_pshader_constantF);
221     if (!object->contained_ps_consts_f) goto fail;
222
223     return WINED3D_OK;
224
225 fail:
226     ERR("Failed to allocate memory\n");
227     HeapFree(GetProcessHeap(), 0, object->state.ps_consts_f);
228     HeapFree(GetProcessHeap(), 0, object->changed.pixelShaderConstantsF);
229     HeapFree(GetProcessHeap(), 0, object->state.vs_consts_f);
230     HeapFree(GetProcessHeap(), 0, object->changed.vertexShaderConstantsF);
231     HeapFree(GetProcessHeap(), 0, object->contained_vs_consts_f);
232     HeapFree(GetProcessHeap(), 0, object->contained_ps_consts_f);
233     return E_OUTOFMEMORY;
234 }
235
236 static inline void stateblock_set_bits(DWORD *map, UINT map_size)
237 {
238     DWORD mask = (1 << (map_size & 0x1f)) - 1;
239     memset(map, 0xff, (map_size >> 5) * sizeof(*map));
240     if (mask) map[map_size >> 5] = mask;
241 }
242
243 /* Set all members of a stateblock savedstate to the given value */
244 static void stateblock_savedstates_set_all(SAVEDSTATES *states, DWORD vs_consts, DWORD ps_consts)
245 {
246     unsigned int i;
247
248     /* Single values */
249     states->primitive_type = 1;
250     states->indices = 1;
251     states->material = 1;
252     states->viewport = 1;
253     states->vertexDecl = 1;
254     states->pixelShader = 1;
255     states->vertexShader = 1;
256     states->scissorRect = 1;
257
258     /* Fixed size arrays */
259     states->streamSource = 0xffff;
260     states->streamFreq = 0xffff;
261     states->textures = 0xfffff;
262     stateblock_set_bits(states->transform, HIGHEST_TRANSFORMSTATE + 1);
263     stateblock_set_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
264     for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
265     for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3ffe;
266     states->clipplane = 0xffffffff;
267     states->pixelShaderConstantsB = 0xffff;
268     states->pixelShaderConstantsI = 0xffff;
269     states->vertexShaderConstantsB = 0xffff;
270     states->vertexShaderConstantsI = 0xffff;
271
272     /* Dynamically sized arrays */
273     memset(states->pixelShaderConstantsF, TRUE, sizeof(BOOL) * ps_consts);
274     memset(states->vertexShaderConstantsF, TRUE, sizeof(BOOL) * vs_consts);
275 }
276
277 static void stateblock_savedstates_set_pixel(SAVEDSTATES *states, const DWORD num_constants)
278 {
279     DWORD texture_mask = 0;
280     WORD sampler_mask = 0;
281     unsigned int i;
282
283     states->pixelShader = 1;
284
285     for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
286     {
287         DWORD rs = pixel_states_render[i];
288         states->renderState[rs >> 5] |= 1 << (rs & 0x1f);
289     }
290
291     for (i = 0; i < sizeof(pixel_states_texture) / sizeof(*pixel_states_texture); ++i)
292         texture_mask |= 1 << pixel_states_texture[i];
293     for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
294     for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
295         sampler_mask |= 1 << pixel_states_sampler[i];
296     for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
297     states->pixelShaderConstantsB = 0xffff;
298     states->pixelShaderConstantsI = 0xffff;
299
300     memset(states->pixelShaderConstantsF, TRUE, sizeof(BOOL) * num_constants);
301 }
302
303 static void stateblock_savedstates_set_vertex(SAVEDSTATES *states, const DWORD num_constants)
304 {
305     DWORD texture_mask = 0;
306     WORD sampler_mask = 0;
307     unsigned int i;
308
309     states->vertexDecl = 1;
310     states->vertexShader = 1;
311
312     for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
313     {
314         DWORD rs = vertex_states_render[i];
315         states->renderState[rs >> 5] |= 1 << (rs & 0x1f);
316     }
317
318     for (i = 0; i < sizeof(vertex_states_texture) / sizeof(*vertex_states_texture); ++i)
319         texture_mask |= 1 << vertex_states_texture[i];
320     for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
321     for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
322         sampler_mask |= 1 << vertex_states_sampler[i];
323     for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
324     states->vertexShaderConstantsB = 0xffff;
325     states->vertexShaderConstantsI = 0xffff;
326
327     memset(states->vertexShaderConstantsF, TRUE, sizeof(BOOL) * num_constants);
328 }
329
330 void stateblock_init_contained_states(struct wined3d_stateblock *stateblock)
331 {
332     IWineD3DDeviceImpl *device = stateblock->device;
333     unsigned int i, j;
334
335     for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
336     {
337         DWORD map = stateblock->changed.renderState[i];
338         for (j = 0; map; map >>= 1, ++j)
339         {
340             if (!(map & 1)) continue;
341
342             stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
343             ++stateblock->num_contained_render_states;
344         }
345     }
346
347     for (i = 0; i <= HIGHEST_TRANSFORMSTATE >> 5; ++i)
348     {
349         DWORD map = stateblock->changed.transform[i];
350         for (j = 0; map; map >>= 1, ++j)
351         {
352             if (!(map & 1)) continue;
353
354             stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
355             ++stateblock->num_contained_transform_states;
356         }
357     }
358
359     for (i = 0; i < device->d3d_vshader_constantF; ++i)
360     {
361         if (stateblock->changed.vertexShaderConstantsF[i])
362         {
363             stateblock->contained_vs_consts_f[stateblock->num_contained_vs_consts_f] = i;
364             ++stateblock->num_contained_vs_consts_f;
365         }
366     }
367
368     for (i = 0; i < MAX_CONST_I; ++i)
369     {
370         if (stateblock->changed.vertexShaderConstantsI & (1 << i))
371         {
372             stateblock->contained_vs_consts_i[stateblock->num_contained_vs_consts_i] = i;
373             ++stateblock->num_contained_vs_consts_i;
374         }
375     }
376
377     for (i = 0; i < MAX_CONST_B; ++i)
378     {
379         if (stateblock->changed.vertexShaderConstantsB & (1 << i))
380         {
381             stateblock->contained_vs_consts_b[stateblock->num_contained_vs_consts_b] = i;
382             ++stateblock->num_contained_vs_consts_b;
383         }
384     }
385
386     for (i = 0; i < device->d3d_pshader_constantF; ++i)
387     {
388         if (stateblock->changed.pixelShaderConstantsF[i])
389         {
390             stateblock->contained_ps_consts_f[stateblock->num_contained_ps_consts_f] = i;
391             ++stateblock->num_contained_ps_consts_f;
392         }
393     }
394
395     for (i = 0; i < MAX_CONST_I; ++i)
396     {
397         if (stateblock->changed.pixelShaderConstantsI & (1 << i))
398         {
399             stateblock->contained_ps_consts_i[stateblock->num_contained_ps_consts_i] = i;
400             ++stateblock->num_contained_ps_consts_i;
401         }
402     }
403
404     for (i = 0; i < MAX_CONST_B; ++i)
405     {
406         if (stateblock->changed.pixelShaderConstantsB & (1 << i))
407         {
408             stateblock->contained_ps_consts_b[stateblock->num_contained_ps_consts_b] = i;
409             ++stateblock->num_contained_ps_consts_b;
410         }
411     }
412
413     for (i = 0; i < MAX_TEXTURES; ++i)
414     {
415         DWORD map = stateblock->changed.textureState[i];
416
417         for(j = 0; map; map >>= 1, ++j)
418         {
419             if (!(map & 1)) continue;
420
421             stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
422             stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
423             ++stateblock->num_contained_tss_states;
424         }
425     }
426
427     for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
428     {
429         DWORD map = stateblock->changed.samplerState[i];
430
431         for (j = 0; map; map >>= 1, ++j)
432         {
433             if (!(map & 1)) continue;
434
435             stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
436             stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
437             ++stateblock->num_contained_sampler_states;
438         }
439     }
440 }
441
442 static void stateblock_init_lights(struct wined3d_stateblock *stateblock, struct list *light_map)
443 {
444     unsigned int i;
445
446     for (i = 0; i < LIGHTMAP_SIZE; ++i)
447     {
448         const struct wined3d_light_info *src_light;
449
450         LIST_FOR_EACH_ENTRY(src_light, &light_map[i], struct wined3d_light_info, entry)
451         {
452             struct wined3d_light_info *dst_light = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_light));
453
454             *dst_light = *src_light;
455             list_add_tail(&stateblock->state.light_map[i], &dst_light->entry);
456         }
457     }
458 }
459
460 ULONG CDECL wined3d_stateblock_incref(struct wined3d_stateblock *stateblock)
461 {
462     ULONG refcount = InterlockedIncrement(&stateblock->ref);
463
464     TRACE("%p increasing refcount to %u.\n", stateblock, refcount);
465
466     return refcount;
467 }
468
469 ULONG CDECL wined3d_stateblock_decref(struct wined3d_stateblock *stateblock)
470 {
471     ULONG refcount = InterlockedDecrement(&stateblock->ref);
472
473     TRACE("%p decreasing refcount to %u\n", stateblock, refcount);
474
475     if (!refcount)
476     {
477         struct wined3d_buffer *buffer;
478         int counter;
479
480         if (stateblock->state.vertex_declaration)
481             wined3d_vertex_declaration_decref(stateblock->state.vertex_declaration);
482
483         for (counter = 0; counter < MAX_COMBINED_SAMPLERS; counter++)
484         {
485             struct wined3d_texture *texture = stateblock->state.textures[counter];
486             if (texture)
487             {
488                 stateblock->state.textures[counter] = NULL;
489                 wined3d_texture_decref(texture);
490             }
491         }
492
493         for (counter = 0; counter < MAX_STREAMS; ++counter)
494         {
495             buffer = stateblock->state.streams[counter].buffer;
496             if (buffer)
497             {
498                 stateblock->state.streams[counter].buffer = NULL;
499                 if (wined3d_buffer_decref(buffer))
500                 {
501                     WARN("Buffer %p still referenced by stateblock, stream %u.\n", buffer, counter);
502                 }
503             }
504         }
505
506         buffer = stateblock->state.index_buffer;
507         if (buffer)
508         {
509             stateblock->state.index_buffer = NULL;
510             wined3d_buffer_decref(buffer);
511         }
512
513         if (stateblock->state.vertex_shader)
514             wined3d_shader_decref(stateblock->state.vertex_shader);
515         if (stateblock->state.pixel_shader)
516             wined3d_shader_decref(stateblock->state.pixel_shader);
517
518         for (counter = 0; counter < LIGHTMAP_SIZE; ++counter)
519         {
520             struct list *e1, *e2;
521             LIST_FOR_EACH_SAFE(e1, e2, &stateblock->state.light_map[counter])
522             {
523                 struct wined3d_light_info *light = LIST_ENTRY(e1, struct wined3d_light_info, entry);
524                 list_remove(&light->entry);
525                 HeapFree(GetProcessHeap(), 0, light);
526             }
527         }
528
529         HeapFree(GetProcessHeap(), 0, stateblock->state.vs_consts_f);
530         HeapFree(GetProcessHeap(), 0, stateblock->changed.vertexShaderConstantsF);
531         HeapFree(GetProcessHeap(), 0, stateblock->state.ps_consts_f);
532         HeapFree(GetProcessHeap(), 0, stateblock->changed.pixelShaderConstantsF);
533         HeapFree(GetProcessHeap(), 0, stateblock->contained_vs_consts_f);
534         HeapFree(GetProcessHeap(), 0, stateblock->contained_ps_consts_f);
535         HeapFree(GetProcessHeap(), 0, stateblock);
536     }
537
538     return refcount;
539 }
540
541 static void wined3d_state_record_lights(struct wined3d_state *dst_state, const struct wined3d_state *src_state)
542 {
543     UINT i;
544
545     /* Lights... For a recorded state block, we just had a chain of actions
546      * to perform, so we need to walk that chain and update any actions which
547      * differ. */
548     for (i = 0; i < LIGHTMAP_SIZE; ++i)
549     {
550         struct list *e, *f;
551         LIST_FOR_EACH(e, &dst_state->light_map[i])
552         {
553             BOOL updated = FALSE;
554             struct wined3d_light_info *src = LIST_ENTRY(e, struct wined3d_light_info, entry), *realLight;
555
556             /* Look up the light in the destination */
557             LIST_FOR_EACH(f, &src_state->light_map[i])
558             {
559                 realLight = LIST_ENTRY(f, struct wined3d_light_info, entry);
560                 if (realLight->OriginalIndex == src->OriginalIndex)
561                 {
562                     src->OriginalParms = realLight->OriginalParms;
563
564                     if (realLight->glIndex == -1 && src->glIndex != -1)
565                     {
566                         /* Light disabled */
567                         dst_state->lights[src->glIndex] = NULL;
568                     }
569                     else if (realLight->glIndex != -1 && src->glIndex == -1)
570                     {
571                         /* Light enabled */
572                         dst_state->lights[realLight->glIndex] = src;
573                     }
574                     src->glIndex = realLight->glIndex;
575                     updated = TRUE;
576                     break;
577                 }
578             }
579
580             if (!updated)
581             {
582                 /* This can happen if the light was originally created as a
583                  * default light for SetLightEnable() while recording. */
584                 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
585                         src->OriginalIndex, dst_state, src_state);
586
587                 src->OriginalParms = WINED3D_default_light;
588                 if (src->glIndex != -1)
589                 {
590                     dst_state->lights[src->glIndex] = NULL;
591                     src->glIndex = -1;
592                 }
593             }
594         }
595     }
596 }
597
598 HRESULT CDECL wined3d_stateblock_capture(struct wined3d_stateblock *stateblock)
599 {
600     const struct wined3d_state *src_state = &stateblock->device->stateBlock->state;
601     unsigned int i;
602     DWORD map;
603
604     TRACE("stateblock %p.\n", stateblock);
605
606     TRACE("Capturing state %p.\n", src_state);
607
608     if (stateblock->changed.vertexShader && stateblock->state.vertex_shader != src_state->vertex_shader)
609     {
610         TRACE("Updating vertex shader from %p to %p\n",
611                 stateblock->state.vertex_shader, src_state->vertex_shader);
612
613         if (src_state->vertex_shader)
614             wined3d_shader_incref(src_state->vertex_shader);
615         if (stateblock->state.vertex_shader)
616             wined3d_shader_decref(stateblock->state.vertex_shader);
617         stateblock->state.vertex_shader = src_state->vertex_shader;
618     }
619
620     /* Vertex shader float constants. */
621     for (i = 0; i < stateblock->num_contained_vs_consts_f; ++i)
622     {
623         unsigned int idx = stateblock->contained_vs_consts_f[i];
624
625         TRACE("Setting vs_consts_f[%u] to {%.8e, %.8e, %.8e, %.8e}.\n", idx,
626                 src_state->vs_consts_f[idx * 4 + 0],
627                 src_state->vs_consts_f[idx * 4 + 1],
628                 src_state->vs_consts_f[idx * 4 + 2],
629                 src_state->vs_consts_f[idx * 4 + 3]);
630
631         stateblock->state.vs_consts_f[idx * 4 + 0] = src_state->vs_consts_f[idx * 4 + 0];
632         stateblock->state.vs_consts_f[idx * 4 + 1] = src_state->vs_consts_f[idx * 4 + 1];
633         stateblock->state.vs_consts_f[idx * 4 + 2] = src_state->vs_consts_f[idx * 4 + 2];
634         stateblock->state.vs_consts_f[idx * 4 + 3] = src_state->vs_consts_f[idx * 4 + 3];
635     }
636
637     /* Vertex shader integer constants. */
638     for (i = 0; i < stateblock->num_contained_vs_consts_i; ++i)
639     {
640         unsigned int idx = stateblock->contained_vs_consts_i[i];
641
642         TRACE("Setting vs_consts[%u] to {%d, %d, %d, %d}.\n", idx,
643                 src_state->vs_consts_i[idx * 4 + 0],
644                 src_state->vs_consts_i[idx * 4 + 1],
645                 src_state->vs_consts_i[idx * 4 + 2],
646                 src_state->vs_consts_i[idx * 4 + 3]);
647
648         stateblock->state.vs_consts_i[idx * 4 + 0] = src_state->vs_consts_i[idx * 4 + 0];
649         stateblock->state.vs_consts_i[idx * 4 + 1] = src_state->vs_consts_i[idx * 4 + 1];
650         stateblock->state.vs_consts_i[idx * 4 + 2] = src_state->vs_consts_i[idx * 4 + 2];
651         stateblock->state.vs_consts_i[idx * 4 + 3] = src_state->vs_consts_i[idx * 4 + 3];
652     }
653
654     /* Vertex shader boolean constants. */
655     for (i = 0; i < stateblock->num_contained_vs_consts_b; ++i)
656     {
657         unsigned int idx = stateblock->contained_vs_consts_b[i];
658
659         TRACE("Setting vs_consts_b[%u] to %s.\n",
660                 idx, src_state->vs_consts_b[idx] ? "TRUE" : "FALSE");
661
662         stateblock->state.vs_consts_b[idx] = src_state->vs_consts_b[idx];
663     }
664
665     /* Pixel shader float constants. */
666     for (i = 0; i < stateblock->num_contained_ps_consts_f; ++i)
667     {
668         unsigned int idx = stateblock->contained_ps_consts_f[i];
669
670         TRACE("Setting ps_consts_f[%u] to {%.8e, %.8e, %.8e, %.8e}.\n", idx,
671                 src_state->ps_consts_f[idx * 4 + 0],
672                 src_state->ps_consts_f[idx * 4 + 1],
673                 src_state->ps_consts_f[idx * 4 + 2],
674                 src_state->ps_consts_f[idx * 4 + 3]);
675
676         stateblock->state.ps_consts_f[idx * 4 + 0] = src_state->ps_consts_f[idx * 4 + 0];
677         stateblock->state.ps_consts_f[idx * 4 + 1] = src_state->ps_consts_f[idx * 4 + 1];
678         stateblock->state.ps_consts_f[idx * 4 + 2] = src_state->ps_consts_f[idx * 4 + 2];
679         stateblock->state.ps_consts_f[idx * 4 + 3] = src_state->ps_consts_f[idx * 4 + 3];
680     }
681
682     /* Pixel shader integer constants. */
683     for (i = 0; i < stateblock->num_contained_ps_consts_i; ++i)
684     {
685         unsigned int idx = stateblock->contained_ps_consts_i[i];
686         TRACE("Setting ps_consts_i[%u] to {%d, %d, %d, %d}.\n", idx,
687                 src_state->ps_consts_i[idx * 4 + 0],
688                 src_state->ps_consts_i[idx * 4 + 1],
689                 src_state->ps_consts_i[idx * 4 + 2],
690                 src_state->ps_consts_i[idx * 4 + 3]);
691
692         stateblock->state.ps_consts_i[idx * 4 + 0] = src_state->ps_consts_i[idx * 4 + 0];
693         stateblock->state.ps_consts_i[idx * 4 + 1] = src_state->ps_consts_i[idx * 4 + 1];
694         stateblock->state.ps_consts_i[idx * 4 + 2] = src_state->ps_consts_i[idx * 4 + 2];
695         stateblock->state.ps_consts_i[idx * 4 + 3] = src_state->ps_consts_i[idx * 4 + 3];
696     }
697
698     /* Pixel shader boolean constants. */
699     for (i = 0; i < stateblock->num_contained_ps_consts_b; ++i)
700     {
701         unsigned int idx = stateblock->contained_ps_consts_b[i];
702         TRACE("Setting ps_consts_b[%u] to %s.\n",
703                 idx, src_state->ps_consts_b[idx] ? "TRUE" : "FALSE");
704
705         stateblock->state.ps_consts_b[idx] = src_state->ps_consts_b[idx];
706     }
707
708     /* Others + Render & Texture */
709     for (i = 0; i < stateblock->num_contained_transform_states; ++i)
710     {
711         WINED3DTRANSFORMSTATETYPE transform = stateblock->contained_transform_states[i];
712
713         TRACE("Updating transform %#x.\n", transform);
714
715         stateblock->state.transforms[transform] = src_state->transforms[transform];
716     }
717
718     if (stateblock->changed.primitive_type)
719         stateblock->state.gl_primitive_type = src_state->gl_primitive_type;
720
721     if (stateblock->changed.indices
722             && ((stateblock->state.index_buffer != src_state->index_buffer)
723                 || (stateblock->state.base_vertex_index != src_state->base_vertex_index)
724                 || (stateblock->state.index_format != src_state->index_format)))
725     {
726         TRACE("Updating index buffer to %p, base vertex index to %d.\n",
727                 src_state->index_buffer, src_state->base_vertex_index);
728
729         if (src_state->index_buffer)
730             wined3d_buffer_incref(src_state->index_buffer);
731         if (stateblock->state.index_buffer)
732             wined3d_buffer_decref(stateblock->state.index_buffer);
733         stateblock->state.index_buffer = src_state->index_buffer;
734         stateblock->state.base_vertex_index = src_state->base_vertex_index;
735         stateblock->state.index_format = src_state->index_format;
736     }
737
738     if (stateblock->changed.vertexDecl && stateblock->state.vertex_declaration != src_state->vertex_declaration)
739     {
740         TRACE("Updating vertex declaration from %p to %p.\n",
741                 stateblock->state.vertex_declaration, src_state->vertex_declaration);
742
743         if (src_state->vertex_declaration)
744                 wined3d_vertex_declaration_incref(src_state->vertex_declaration);
745         if (stateblock->state.vertex_declaration)
746                 wined3d_vertex_declaration_decref(stateblock->state.vertex_declaration);
747         stateblock->state.vertex_declaration = src_state->vertex_declaration;
748     }
749
750     if (stateblock->changed.material
751             && memcmp(&src_state->material, &stateblock->state.material, sizeof(stateblock->state.material)))
752     {
753         TRACE("Updating material.\n");
754
755         stateblock->state.material = src_state->material;
756     }
757
758     if (stateblock->changed.viewport
759             && memcmp(&src_state->viewport, &stateblock->state.viewport, sizeof(stateblock->state.viewport)))
760     {
761         TRACE("Updating viewport.\n");
762
763         stateblock->state.viewport = src_state->viewport;
764     }
765
766     if (stateblock->changed.scissorRect && memcmp(&src_state->scissor_rect,
767             &stateblock->state.scissor_rect, sizeof(stateblock->state.scissor_rect)))
768     {
769         TRACE("Updating scissor rect.\n");
770
771         stateblock->state.scissor_rect = src_state->scissor_rect;
772     }
773
774     map = stateblock->changed.streamSource;
775     for (i = 0; map; map >>= 1, ++i)
776     {
777         if (!(map & 1)) continue;
778
779         if (stateblock->state.streams[i].stride != src_state->streams[i].stride
780                 || stateblock->state.streams[i].buffer != src_state->streams[i].buffer)
781         {
782             TRACE("Updating stream source %u to %p, stride to %u.\n",
783                     i, src_state->streams[i].buffer,
784                     src_state->streams[i].stride);
785
786             stateblock->state.streams[i].stride = src_state->streams[i].stride;
787             if (src_state->streams[i].buffer)
788                     wined3d_buffer_incref(src_state->streams[i].buffer);
789             if (stateblock->state.streams[i].buffer)
790                     wined3d_buffer_decref(stateblock->state.streams[i].buffer);
791             stateblock->state.streams[i].buffer = src_state->streams[i].buffer;
792         }
793     }
794
795     map = stateblock->changed.streamFreq;
796     for (i = 0; map; map >>= 1, ++i)
797     {
798         if (!(map & 1)) continue;
799
800         if (stateblock->state.streams[i].frequency != src_state->streams[i].frequency
801                 || stateblock->state.streams[i].flags != src_state->streams[i].flags)
802         {
803             TRACE("Updating stream frequency %u to %u flags to %#x.\n",
804                     i, src_state->streams[i].frequency, src_state->streams[i].flags);
805
806             stateblock->state.streams[i].frequency = src_state->streams[i].frequency;
807             stateblock->state.streams[i].flags = src_state->streams[i].flags;
808         }
809     }
810
811     map = stateblock->changed.clipplane;
812     for (i = 0; map; map >>= 1, ++i)
813     {
814         if (!(map & 1)) continue;
815
816         if (memcmp(src_state->clip_planes[i], stateblock->state.clip_planes[i], sizeof(*stateblock->state.clip_planes)))
817         {
818             TRACE("Updating clipplane %u.\n", i);
819             memcpy(stateblock->state.clip_planes[i], src_state->clip_planes[i], sizeof(*stateblock->state.clip_planes));
820         }
821     }
822
823     /* Render */
824     for (i = 0; i < stateblock->num_contained_render_states; ++i)
825     {
826         WINED3DRENDERSTATETYPE rs = stateblock->contained_render_states[i];
827
828         TRACE("Updating render state %#x to %u.\n", rs, src_state->render_states[rs]);
829
830         stateblock->state.render_states[rs] = src_state->render_states[rs];
831     }
832
833     /* Texture states */
834     for (i = 0; i < stateblock->num_contained_tss_states; ++i)
835     {
836         DWORD stage = stateblock->contained_tss_states[i].stage;
837         DWORD state = stateblock->contained_tss_states[i].state;
838
839         TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage, state,
840                 src_state->texture_states[stage][state], stateblock->state.texture_states[stage][state]);
841
842         stateblock->state.texture_states[stage][state] = src_state->texture_states[stage][state];
843     }
844
845     /* Samplers */
846     map = stateblock->changed.textures;
847     for (i = 0; map; map >>= 1, ++i)
848     {
849         if (!(map & 1)) continue;
850
851         TRACE("Updating texture %u to %p (was %p).\n",
852                 i, src_state->textures[i], stateblock->state.textures[i]);
853
854         if (src_state->textures[i])
855             wined3d_texture_incref(src_state->textures[i]);
856         if (stateblock->state.textures[i])
857             wined3d_texture_decref(stateblock->state.textures[i]);
858         stateblock->state.textures[i] = src_state->textures[i];
859     }
860
861     for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
862     {
863         DWORD stage = stateblock->contained_sampler_states[i].stage;
864         DWORD state = stateblock->contained_sampler_states[i].state;
865
866         TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage, state,
867                 src_state->sampler_states[stage][state], stateblock->state.sampler_states[stage][state]);
868
869         stateblock->state.sampler_states[stage][state] = src_state->sampler_states[stage][state];
870     }
871
872     if (stateblock->changed.pixelShader && stateblock->state.pixel_shader != src_state->pixel_shader)
873     {
874         if (src_state->pixel_shader)
875             wined3d_shader_incref(src_state->pixel_shader);
876         if (stateblock->state.pixel_shader)
877             wined3d_shader_decref(stateblock->state.pixel_shader);
878         stateblock->state.pixel_shader = src_state->pixel_shader;
879     }
880
881     wined3d_state_record_lights(&stateblock->state, src_state);
882
883     TRACE("Captue done.\n");
884
885     return WINED3D_OK;
886 }
887
888 static void apply_lights(IWineD3DDevice *device, const struct wined3d_state *state)
889 {
890     UINT i;
891
892     for (i = 0; i < LIGHTMAP_SIZE; ++i)
893     {
894         struct list *e;
895
896         LIST_FOR_EACH(e, &state->light_map[i])
897         {
898             const struct wined3d_light_info *light = LIST_ENTRY(e, struct wined3d_light_info, entry);
899
900             wined3d_device_set_light(device, light->OriginalIndex, &light->OriginalParms);
901             wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
902         }
903     }
904 }
905
906 HRESULT CDECL wined3d_stateblock_apply(const struct wined3d_stateblock *stateblock)
907 {
908     IWineD3DDevice *device = (IWineD3DDevice *)stateblock->device;
909     unsigned int i;
910     DWORD map;
911
912     TRACE("Applying stateblock %p to device %p.\n", stateblock, device);
913     TRACE("Blocktype: %#x.\n", stateblock->blockType);
914
915     if (stateblock->changed.vertexShader)
916         wined3d_device_set_vertex_shader(device, stateblock->state.vertex_shader);
917
918     /* Vertex Shader Constants. */
919     for (i = 0; i < stateblock->num_contained_vs_consts_f; ++i)
920     {
921         wined3d_device_set_vs_consts_f(device, stateblock->contained_vs_consts_f[i],
922                 stateblock->state.vs_consts_f + stateblock->contained_vs_consts_f[i] * 4, 1);
923     }
924     for (i = 0; i < stateblock->num_contained_vs_consts_i; ++i)
925     {
926         wined3d_device_set_vs_consts_i(device, stateblock->contained_vs_consts_i[i],
927                 stateblock->state.vs_consts_i + stateblock->contained_vs_consts_i[i] * 4, 1);
928     }
929     for (i = 0; i < stateblock->num_contained_vs_consts_b; ++i)
930     {
931         wined3d_device_set_vs_consts_b(device, stateblock->contained_vs_consts_b[i],
932                 stateblock->state.vs_consts_b + stateblock->contained_vs_consts_b[i], 1);
933     }
934
935     apply_lights(device, &stateblock->state);
936
937     if (stateblock->changed.pixelShader)
938         wined3d_device_set_pixel_shader(device, stateblock->state.pixel_shader);
939
940     /* Pixel Shader Constants. */
941     for (i = 0; i < stateblock->num_contained_ps_consts_f; ++i)
942     {
943         wined3d_device_set_ps_consts_f(device, stateblock->contained_ps_consts_f[i],
944                 stateblock->state.ps_consts_f + stateblock->contained_ps_consts_f[i] * 4, 1);
945     }
946     for (i = 0; i < stateblock->num_contained_ps_consts_i; ++i)
947     {
948         wined3d_device_set_ps_consts_i(device, stateblock->contained_ps_consts_i[i],
949                 stateblock->state.ps_consts_i + stateblock->contained_ps_consts_i[i] * 4, 1);
950     }
951     for (i = 0; i < stateblock->num_contained_ps_consts_b; ++i)
952     {
953         wined3d_device_set_ps_consts_b(device, stateblock->contained_ps_consts_b[i],
954                 stateblock->state.ps_consts_b + stateblock->contained_ps_consts_b[i], 1);
955     }
956
957     /* Render states. */
958     for (i = 0; i < stateblock->num_contained_render_states; ++i)
959     {
960         wined3d_device_set_render_state(device, stateblock->contained_render_states[i],
961                 stateblock->state.render_states[stateblock->contained_render_states[i]]);
962     }
963
964     /* Texture states. */
965     for (i = 0; i < stateblock->num_contained_tss_states; ++i)
966     {
967         DWORD stage = stateblock->contained_tss_states[i].stage;
968         DWORD state = stateblock->contained_tss_states[i].state;
969
970         wined3d_device_set_texture_stage_state(device, stage, state, stateblock->state.texture_states[stage][state]);
971     }
972
973     /* Sampler states. */
974     for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
975     {
976         DWORD stage = stateblock->contained_sampler_states[i].stage;
977         DWORD state = stateblock->contained_sampler_states[i].state;
978         DWORD value = stateblock->state.sampler_states[stage][state];
979
980         if (stage >= MAX_FRAGMENT_SAMPLERS) stage += WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS;
981         wined3d_device_set_sampler_state(device, stage, state, value);
982     }
983
984     /* Transform states. */
985     for (i = 0; i < stateblock->num_contained_transform_states; ++i)
986     {
987         wined3d_device_set_transform(device, stateblock->contained_transform_states[i],
988                 &stateblock->state.transforms[stateblock->contained_transform_states[i]]);
989     }
990
991     if (stateblock->changed.primitive_type)
992     {
993         stateblock->device->updateStateBlock->changed.primitive_type = TRUE;
994         stateblock->device->updateStateBlock->state.gl_primitive_type = stateblock->state.gl_primitive_type;
995     }
996
997     if (stateblock->changed.indices)
998     {
999         wined3d_device_set_index_buffer(device, stateblock->state.index_buffer, stateblock->state.index_format);
1000         wined3d_device_set_base_vertex_index(device, stateblock->state.base_vertex_index);
1001     }
1002
1003     if (stateblock->changed.vertexDecl && stateblock->state.vertex_declaration)
1004         wined3d_device_set_vertex_declaration(device, stateblock->state.vertex_declaration);
1005
1006     if (stateblock->changed.material)
1007         wined3d_device_set_material(device, &stateblock->state.material);
1008
1009     if (stateblock->changed.viewport)
1010         wined3d_device_set_viewport(device, &stateblock->state.viewport);
1011
1012     if (stateblock->changed.scissorRect)
1013         wined3d_device_set_scissor_rect(device, &stateblock->state.scissor_rect);
1014
1015     map = stateblock->changed.streamSource;
1016     for (i = 0; map; map >>= 1, ++i)
1017     {
1018         if (map & 1)
1019             wined3d_device_set_stream_source(device, i,
1020                     stateblock->state.streams[i].buffer,
1021                     0, stateblock->state.streams[i].stride);
1022     }
1023
1024     map = stateblock->changed.streamFreq;
1025     for (i = 0; map; map >>= 1, ++i)
1026     {
1027         if (map & 1)
1028             wined3d_device_set_stream_source_freq(device, i,
1029                     stateblock->state.streams[i].frequency | stateblock->state.streams[i].flags);
1030     }
1031
1032     map = stateblock->changed.textures;
1033     for (i = 0; map; map >>= 1, ++i)
1034     {
1035         DWORD stage;
1036
1037         if (!(map & 1)) continue;
1038
1039         stage = i < MAX_FRAGMENT_SAMPLERS ? i : WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS;
1040         wined3d_device_set_texture(device, stage, stateblock->state.textures[i]);
1041     }
1042
1043     map = stateblock->changed.clipplane;
1044     for (i = 0; map; map >>= 1, ++i)
1045     {
1046         float clip[4];
1047
1048         if (!(map & 1)) continue;
1049
1050         clip[0] = (float) stateblock->state.clip_planes[i][0];
1051         clip[1] = (float) stateblock->state.clip_planes[i][1];
1052         clip[2] = (float) stateblock->state.clip_planes[i][2];
1053         clip[3] = (float) stateblock->state.clip_planes[i][3];
1054         wined3d_device_set_clip_plane(device, i, clip);
1055     }
1056
1057     stateblock->device->stateBlock->state.lowest_disabled_stage = MAX_TEXTURES - 1;
1058     for (i = 0; i < MAX_TEXTURES - 1; ++i)
1059     {
1060         if (stateblock->device->stateBlock->state.texture_states[i][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE)
1061         {
1062             stateblock->device->stateBlock->state.lowest_disabled_stage = i;
1063             break;
1064         }
1065     }
1066
1067     TRACE("Applied stateblock %p.\n", stateblock);
1068
1069     return WINED3D_OK;
1070 }
1071
1072 void stateblock_init_default_state(struct wined3d_stateblock *stateblock)
1073 {
1074     IWineD3DDeviceImpl *device = stateblock->device;
1075     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1076     struct wined3d_state *state = &stateblock->state;
1077     union {
1078         WINED3DLINEPATTERN lp;
1079         DWORD d;
1080     } lp;
1081     union {
1082         float f;
1083         DWORD d;
1084     } tmpfloat;
1085     unsigned int i;
1086     struct wined3d_swapchain *swapchain;
1087     struct wined3d_surface *backbuffer;
1088     HRESULT hr;
1089
1090     TRACE("stateblock %p.\n", stateblock);
1091
1092     stateblock->blockType = WINED3DSBT_INIT;
1093
1094     /* Set some of the defaults for lights, transforms etc */
1095     memcpy(&state->transforms[WINED3DTS_PROJECTION], identity, sizeof(identity));
1096     memcpy(&state->transforms[WINED3DTS_VIEW], identity, sizeof(identity));
1097     for (i = 0; i < 256; ++i)
1098     {
1099         memcpy(&state->transforms[WINED3DTS_WORLDMATRIX(i)], identity, sizeof(identity));
1100     }
1101
1102     TRACE("Render states\n");
1103     /* Render states: */
1104     if (device->auto_depth_stencil)
1105        state->render_states[WINED3DRS_ZENABLE] = WINED3DZB_TRUE;
1106     else
1107        state->render_states[WINED3DRS_ZENABLE] = WINED3DZB_FALSE;
1108     state->render_states[WINED3DRS_FILLMODE] = WINED3DFILL_SOLID;
1109     state->render_states[WINED3DRS_SHADEMODE] = WINED3DSHADE_GOURAUD;
1110     lp.lp.wRepeatFactor = 0;
1111     lp.lp.wLinePattern = 0;
1112     state->render_states[WINED3DRS_LINEPATTERN] = lp.d;
1113     state->render_states[WINED3DRS_ZWRITEENABLE] = TRUE;
1114     state->render_states[WINED3DRS_ALPHATESTENABLE] = FALSE;
1115     state->render_states[WINED3DRS_LASTPIXEL] = TRUE;
1116     state->render_states[WINED3DRS_SRCBLEND] = WINED3DBLEND_ONE;
1117     state->render_states[WINED3DRS_DESTBLEND] = WINED3DBLEND_ZERO;
1118     state->render_states[WINED3DRS_CULLMODE] = WINED3DCULL_CCW;
1119     state->render_states[WINED3DRS_ZFUNC] = WINED3DCMP_LESSEQUAL;
1120     state->render_states[WINED3DRS_ALPHAFUNC] = WINED3DCMP_ALWAYS;
1121     state->render_states[WINED3DRS_ALPHAREF] = 0;
1122     state->render_states[WINED3DRS_DITHERENABLE] = FALSE;
1123     state->render_states[WINED3DRS_ALPHABLENDENABLE] = FALSE;
1124     state->render_states[WINED3DRS_FOGENABLE] = FALSE;
1125     state->render_states[WINED3DRS_SPECULARENABLE] = FALSE;
1126     state->render_states[WINED3DRS_ZVISIBLE] = 0;
1127     state->render_states[WINED3DRS_FOGCOLOR] = 0;
1128     state->render_states[WINED3DRS_FOGTABLEMODE] = WINED3DFOG_NONE;
1129     tmpfloat.f = 0.0f;
1130     state->render_states[WINED3DRS_FOGSTART] = tmpfloat.d;
1131     tmpfloat.f = 1.0f;
1132     state->render_states[WINED3DRS_FOGEND] = tmpfloat.d;
1133     tmpfloat.f = 1.0f;
1134     state->render_states[WINED3DRS_FOGDENSITY] = tmpfloat.d;
1135     state->render_states[WINED3DRS_EDGEANTIALIAS] = FALSE;
1136     state->render_states[WINED3DRS_RANGEFOGENABLE] = FALSE;
1137     state->render_states[WINED3DRS_STENCILENABLE] = FALSE;
1138     state->render_states[WINED3DRS_STENCILFAIL] = WINED3DSTENCILOP_KEEP;
1139     state->render_states[WINED3DRS_STENCILZFAIL] = WINED3DSTENCILOP_KEEP;
1140     state->render_states[WINED3DRS_STENCILPASS] = WINED3DSTENCILOP_KEEP;
1141     state->render_states[WINED3DRS_STENCILREF] = 0;
1142     state->render_states[WINED3DRS_STENCILMASK] = 0xffffffff;
1143     state->render_states[WINED3DRS_STENCILFUNC] = WINED3DCMP_ALWAYS;
1144     state->render_states[WINED3DRS_STENCILWRITEMASK] = 0xffffffff;
1145     state->render_states[WINED3DRS_TEXTUREFACTOR] = 0xffffffff;
1146     state->render_states[WINED3DRS_WRAP0] = 0;
1147     state->render_states[WINED3DRS_WRAP1] = 0;
1148     state->render_states[WINED3DRS_WRAP2] = 0;
1149     state->render_states[WINED3DRS_WRAP3] = 0;
1150     state->render_states[WINED3DRS_WRAP4] = 0;
1151     state->render_states[WINED3DRS_WRAP5] = 0;
1152     state->render_states[WINED3DRS_WRAP6] = 0;
1153     state->render_states[WINED3DRS_WRAP7] = 0;
1154     state->render_states[WINED3DRS_CLIPPING] = TRUE;
1155     state->render_states[WINED3DRS_LIGHTING] = TRUE;
1156     state->render_states[WINED3DRS_AMBIENT] = 0;
1157     state->render_states[WINED3DRS_FOGVERTEXMODE] = WINED3DFOG_NONE;
1158     state->render_states[WINED3DRS_COLORVERTEX] = TRUE;
1159     state->render_states[WINED3DRS_LOCALVIEWER] = TRUE;
1160     state->render_states[WINED3DRS_NORMALIZENORMALS] = FALSE;
1161     state->render_states[WINED3DRS_DIFFUSEMATERIALSOURCE] = WINED3DMCS_COLOR1;
1162     state->render_states[WINED3DRS_SPECULARMATERIALSOURCE] = WINED3DMCS_COLOR2;
1163     state->render_states[WINED3DRS_AMBIENTMATERIALSOURCE] = WINED3DMCS_MATERIAL;
1164     state->render_states[WINED3DRS_EMISSIVEMATERIALSOURCE] = WINED3DMCS_MATERIAL;
1165     state->render_states[WINED3DRS_VERTEXBLEND] = WINED3DVBF_DISABLE;
1166     state->render_states[WINED3DRS_CLIPPLANEENABLE] = 0;
1167     state->render_states[WINED3DRS_SOFTWAREVERTEXPROCESSING] = FALSE;
1168     tmpfloat.f = 1.0f;
1169     state->render_states[WINED3DRS_POINTSIZE] = tmpfloat.d;
1170     tmpfloat.f = 1.0f;
1171     state->render_states[WINED3DRS_POINTSIZE_MIN] = tmpfloat.d;
1172     state->render_states[WINED3DRS_POINTSPRITEENABLE] = FALSE;
1173     state->render_states[WINED3DRS_POINTSCALEENABLE] = FALSE;
1174     tmpfloat.f = 1.0f;
1175     state->render_states[WINED3DRS_POINTSCALE_A] = tmpfloat.d;
1176     tmpfloat.f = 0.0f;
1177     state->render_states[WINED3DRS_POINTSCALE_B] = tmpfloat.d;
1178     tmpfloat.f = 0.0f;
1179     state->render_states[WINED3DRS_POINTSCALE_C] = tmpfloat.d;
1180     state->render_states[WINED3DRS_MULTISAMPLEANTIALIAS] = TRUE;
1181     state->render_states[WINED3DRS_MULTISAMPLEMASK] = 0xffffffff;
1182     state->render_states[WINED3DRS_PATCHEDGESTYLE] = WINED3DPATCHEDGE_DISCRETE;
1183     tmpfloat.f = 1.0f;
1184     state->render_states[WINED3DRS_PATCHSEGMENTS] = tmpfloat.d;
1185     state->render_states[WINED3DRS_DEBUGMONITORTOKEN] = 0xbaadcafe;
1186     tmpfloat.f = gl_info->limits.pointsize_max;
1187     state->render_states[WINED3DRS_POINTSIZE_MAX] = tmpfloat.d;
1188     state->render_states[WINED3DRS_INDEXEDVERTEXBLENDENABLE] = FALSE;
1189     state->render_states[WINED3DRS_COLORWRITEENABLE] = 0x0000000f;
1190     tmpfloat.f = 0.0f;
1191     state->render_states[WINED3DRS_TWEENFACTOR] = tmpfloat.d;
1192     state->render_states[WINED3DRS_BLENDOP] = WINED3DBLENDOP_ADD;
1193     state->render_states[WINED3DRS_POSITIONDEGREE] = WINED3DDEGREE_CUBIC;
1194     state->render_states[WINED3DRS_NORMALDEGREE] = WINED3DDEGREE_LINEAR;
1195     /* states new in d3d9 */
1196     state->render_states[WINED3DRS_SCISSORTESTENABLE] = FALSE;
1197     state->render_states[WINED3DRS_SLOPESCALEDEPTHBIAS] = 0;
1198     tmpfloat.f = 1.0f;
1199     state->render_states[WINED3DRS_MINTESSELLATIONLEVEL] = tmpfloat.d;
1200     state->render_states[WINED3DRS_MAXTESSELLATIONLEVEL] = tmpfloat.d;
1201     state->render_states[WINED3DRS_ANTIALIASEDLINEENABLE] = FALSE;
1202     tmpfloat.f = 0.0f;
1203     state->render_states[WINED3DRS_ADAPTIVETESS_X] = tmpfloat.d;
1204     state->render_states[WINED3DRS_ADAPTIVETESS_Y] = tmpfloat.d;
1205     tmpfloat.f = 1.0f;
1206     state->render_states[WINED3DRS_ADAPTIVETESS_Z] = tmpfloat.d;
1207     tmpfloat.f = 0.0f;
1208     state->render_states[WINED3DRS_ADAPTIVETESS_W] = tmpfloat.d;
1209     state->render_states[WINED3DRS_ENABLEADAPTIVETESSELLATION] = FALSE;
1210     state->render_states[WINED3DRS_TWOSIDEDSTENCILMODE] = FALSE;
1211     state->render_states[WINED3DRS_CCW_STENCILFAIL] = WINED3DSTENCILOP_KEEP;
1212     state->render_states[WINED3DRS_CCW_STENCILZFAIL] = WINED3DSTENCILOP_KEEP;
1213     state->render_states[WINED3DRS_CCW_STENCILPASS] = WINED3DSTENCILOP_KEEP;
1214     state->render_states[WINED3DRS_CCW_STENCILFUNC] = WINED3DCMP_ALWAYS;
1215     state->render_states[WINED3DRS_COLORWRITEENABLE1] = 0x0000000f;
1216     state->render_states[WINED3DRS_COLORWRITEENABLE2] = 0x0000000f;
1217     state->render_states[WINED3DRS_COLORWRITEENABLE3] = 0x0000000f;
1218     state->render_states[WINED3DRS_BLENDFACTOR] = 0xFFFFFFFF;
1219     state->render_states[WINED3DRS_SRGBWRITEENABLE] = 0;
1220     state->render_states[WINED3DRS_DEPTHBIAS] = 0;
1221     state->render_states[WINED3DRS_WRAP8] = 0;
1222     state->render_states[WINED3DRS_WRAP9] = 0;
1223     state->render_states[WINED3DRS_WRAP10] = 0;
1224     state->render_states[WINED3DRS_WRAP11] = 0;
1225     state->render_states[WINED3DRS_WRAP12] = 0;
1226     state->render_states[WINED3DRS_WRAP13] = 0;
1227     state->render_states[WINED3DRS_WRAP14] = 0;
1228     state->render_states[WINED3DRS_WRAP15] = 0;
1229     state->render_states[WINED3DRS_SEPARATEALPHABLENDENABLE] = FALSE;
1230     state->render_states[WINED3DRS_SRCBLENDALPHA] = WINED3DBLEND_ONE;
1231     state->render_states[WINED3DRS_DESTBLENDALPHA] = WINED3DBLEND_ZERO;
1232     state->render_states[WINED3DRS_BLENDOPALPHA] = WINED3DBLENDOP_ADD;
1233
1234     /* clipping status */
1235     state->clip_status.ClipUnion = 0;
1236     state->clip_status.ClipIntersection = 0xFFFFFFFF;
1237
1238     /* Texture Stage States - Put directly into state block, we will call function below */
1239     for (i = 0; i < MAX_TEXTURES; ++i)
1240     {
1241         TRACE("Setting up default texture states for texture Stage %u.\n", i);
1242         memcpy(&state->transforms[WINED3DTS_TEXTURE0 + i], identity, sizeof(identity));
1243         state->texture_states[i][WINED3DTSS_COLOROP] = i ? WINED3DTOP_DISABLE : WINED3DTOP_MODULATE;
1244         state->texture_states[i][WINED3DTSS_COLORARG1] = WINED3DTA_TEXTURE;
1245         state->texture_states[i][WINED3DTSS_COLORARG2] = WINED3DTA_CURRENT;
1246         state->texture_states[i][WINED3DTSS_ALPHAOP] = i ? WINED3DTOP_DISABLE : WINED3DTOP_SELECTARG1;
1247         state->texture_states[i][WINED3DTSS_ALPHAARG1] = WINED3DTA_TEXTURE;
1248         state->texture_states[i][WINED3DTSS_ALPHAARG2] = WINED3DTA_CURRENT;
1249         state->texture_states[i][WINED3DTSS_BUMPENVMAT00] = 0;
1250         state->texture_states[i][WINED3DTSS_BUMPENVMAT01] = 0;
1251         state->texture_states[i][WINED3DTSS_BUMPENVMAT10] = 0;
1252         state->texture_states[i][WINED3DTSS_BUMPENVMAT11] = 0;
1253         state->texture_states[i][WINED3DTSS_TEXCOORDINDEX] = i;
1254         state->texture_states[i][WINED3DTSS_BUMPENVLSCALE] = 0;
1255         state->texture_states[i][WINED3DTSS_BUMPENVLOFFSET] = 0;
1256         state->texture_states[i][WINED3DTSS_TEXTURETRANSFORMFLAGS] = WINED3DTTFF_DISABLE;
1257         state->texture_states[i][WINED3DTSS_COLORARG0] = WINED3DTA_CURRENT;
1258         state->texture_states[i][WINED3DTSS_ALPHAARG0] = WINED3DTA_CURRENT;
1259         state->texture_states[i][WINED3DTSS_RESULTARG] = WINED3DTA_CURRENT;
1260     }
1261     state->lowest_disabled_stage = 1;
1262
1263         /* Sampler states*/
1264     for (i = 0 ; i <  MAX_COMBINED_SAMPLERS; ++i)
1265     {
1266         TRACE("Setting up default samplers states for sampler %u.\n", i);
1267         state->sampler_states[i][WINED3DSAMP_ADDRESSU] = WINED3DTADDRESS_WRAP;
1268         state->sampler_states[i][WINED3DSAMP_ADDRESSV] = WINED3DTADDRESS_WRAP;
1269         state->sampler_states[i][WINED3DSAMP_ADDRESSW] = WINED3DTADDRESS_WRAP;
1270         state->sampler_states[i][WINED3DSAMP_BORDERCOLOR] = 0;
1271         state->sampler_states[i][WINED3DSAMP_MAGFILTER] = WINED3DTEXF_POINT;
1272         state->sampler_states[i][WINED3DSAMP_MINFILTER] = WINED3DTEXF_POINT;
1273         state->sampler_states[i][WINED3DSAMP_MIPFILTER] = WINED3DTEXF_NONE;
1274         state->sampler_states[i][WINED3DSAMP_MIPMAPLODBIAS] = 0;
1275         state->sampler_states[i][WINED3DSAMP_MAXMIPLEVEL] = 0;
1276         state->sampler_states[i][WINED3DSAMP_MAXANISOTROPY] = 1;
1277         state->sampler_states[i][WINED3DSAMP_SRGBTEXTURE] = 0;
1278         /* TODO: Indicates which element of a multielement texture to use. */
1279         state->sampler_states[i][WINED3DSAMP_ELEMENTINDEX] = 0;
1280         /* TODO: Vertex offset in the presampled displacement map. */
1281         state->sampler_states[i][WINED3DSAMP_DMAPOFFSET] = 0;
1282     }
1283
1284     for (i = 0; i < gl_info->limits.textures; ++i)
1285     {
1286         state->textures[i] = NULL;
1287     }
1288
1289     /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1290     hr = wined3d_device_get_swapchain(device, 0, &swapchain);
1291     if (SUCCEEDED(hr) && swapchain)
1292     {
1293         hr = wined3d_swapchain_get_back_buffer(swapchain, 0, WINED3DBACKBUFFER_TYPE_MONO, &backbuffer);
1294         if (SUCCEEDED(hr) && backbuffer)
1295         {
1296             struct wined3d_resource_desc desc;
1297
1298             wined3d_resource_get_desc(&backbuffer->resource, &desc);
1299             wined3d_surface_decref(backbuffer);
1300
1301             /* Set the default scissor rect values */
1302             state->scissor_rect.left = 0;
1303             state->scissor_rect.right = desc.width;
1304             state->scissor_rect.top = 0;
1305             state->scissor_rect.bottom = desc.height;
1306         }
1307
1308         /* Set the default viewport */
1309         state->viewport.X = 0;
1310         state->viewport.Y = 0;
1311         state->viewport.Width = swapchain->presentParms.BackBufferWidth;
1312         state->viewport.Height = swapchain->presentParms.BackBufferHeight;
1313         state->viewport.MinZ = 0.0f;
1314         state->viewport.MaxZ = 1.0f;
1315
1316         wined3d_swapchain_decref(swapchain);
1317     }
1318
1319     TRACE("Done.\n");
1320 }
1321
1322 static HRESULT stateblock_init(struct wined3d_stateblock *stateblock,
1323         IWineD3DDeviceImpl *device, WINED3DSTATEBLOCKTYPE type)
1324 {
1325     unsigned int i;
1326     HRESULT hr;
1327
1328     stateblock->ref = 1;
1329     stateblock->device = device;
1330     stateblock->blockType = type;
1331
1332     for (i = 0; i < LIGHTMAP_SIZE; i++)
1333     {
1334         list_init(&stateblock->state.light_map[i]);
1335     }
1336
1337     hr = stateblock_allocate_shader_constants(stateblock);
1338     if (FAILED(hr)) return hr;
1339
1340     /* The WINED3DSBT_INIT stateblock type is used during initialization to
1341      * produce a placeholder stateblock so other functions called can update a
1342      * state block. */
1343     if (type == WINED3DSBT_INIT || type == WINED3DSBT_RECORDED) return WINED3D_OK;
1344
1345     TRACE("Updating changed flags appropriate for type %#x.\n", type);
1346
1347     switch (type)
1348     {
1349         case WINED3DSBT_ALL:
1350             stateblock_init_lights(stateblock, device->stateBlock->state.light_map);
1351             stateblock_savedstates_set_all(&stateblock->changed, device->d3d_vshader_constantF,
1352                                            device->d3d_pshader_constantF);
1353             break;
1354
1355         case WINED3DSBT_PIXELSTATE:
1356             stateblock_savedstates_set_pixel(&stateblock->changed, device->d3d_pshader_constantF);
1357             break;
1358
1359         case WINED3DSBT_VERTEXSTATE:
1360             stateblock_init_lights(stateblock, device->stateBlock->state.light_map);
1361             stateblock_savedstates_set_vertex(&stateblock->changed, device->d3d_vshader_constantF);
1362             break;
1363
1364         default:
1365             FIXME("Unrecognized state block type %#x.\n", type);
1366             break;
1367     }
1368
1369     stateblock_init_contained_states(stateblock);
1370     wined3d_stateblock_capture(stateblock);
1371
1372     return WINED3D_OK;
1373 }
1374
1375 HRESULT CDECL wined3d_stateblock_create(IWineD3DDevice *iface,
1376         WINED3DSTATEBLOCKTYPE type, struct wined3d_stateblock **stateblock)
1377 {
1378     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)iface;
1379     struct wined3d_stateblock *object;
1380     HRESULT hr;
1381
1382     TRACE("device %p, type %#x, stateblock %p.\n",
1383             device, type, stateblock);
1384
1385     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1386     if (!object)
1387     {
1388         ERR("Failed to allocate stateblock memory.\n");
1389         return E_OUTOFMEMORY;
1390     }
1391
1392     hr = stateblock_init(object, device, type);
1393     if (FAILED(hr))
1394     {
1395         WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1396         HeapFree(GetProcessHeap(), 0, object);
1397         return hr;
1398     }
1399
1400     TRACE("Created stateblock %p.\n", object);
1401     *stateblock = object;
1402
1403     return WINED3D_OK;
1404 }