oleaut32: Use wire sizes for marshaled safe arrays of bstrs and variants; fixes a...
[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(IWineD3DStateBlockImpl *object)
195 {
196     IWineD3DDeviceImpl *device = object->device;
197
198     /* Allocate space for floating point constants */
199     object->pixelShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
200             sizeof(float) * device->d3d_pshader_constantF * 4);
201     if (!object->pixelShaderConstantF) 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->vertexShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
208             sizeof(float) * device->d3d_vshader_constantF * 4);
209     if (!object->vertexShaderConstantF) 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->pixelShaderConstantF);
228     HeapFree(GetProcessHeap(), 0, object->changed.pixelShaderConstantsF);
229     HeapFree(GetProcessHeap(), 0, object->vertexShaderConstantF);
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] = 0x3fff;
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(IWineD3DStateBlockImpl *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(IWineD3DStateBlockImpl *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->lightMap[i], &dst_light->entry);
456         }
457     }
458 }
459
460 /**********************************************************
461  * IWineD3DStateBlockImpl IUnknown parts follows
462  **********************************************************/
463 static HRESULT  WINAPI IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock *iface,REFIID riid,LPVOID *ppobj)
464 {
465     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
466     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
467     if (IsEqualGUID(riid, &IID_IUnknown)
468         || IsEqualGUID(riid, &IID_IWineD3DBase)
469         || IsEqualGUID(riid, &IID_IWineD3DStateBlock)){
470         IUnknown_AddRef(iface);
471         *ppobj = This;
472         return S_OK;
473     }
474     *ppobj = NULL;
475     return E_NOINTERFACE;
476 }
477
478 static ULONG  WINAPI IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock *iface) {
479     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
480     ULONG refCount = InterlockedIncrement(&This->ref);
481
482     TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
483     return refCount;
484 }
485
486 static ULONG  WINAPI IWineD3DStateBlockImpl_Release(IWineD3DStateBlock *iface) {
487     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
488     ULONG refCount = InterlockedDecrement(&This->ref);
489
490     TRACE("(%p) : Releasing from %d\n", This, refCount + 1);
491
492     if (!refCount) {
493         int counter;
494
495         if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
496
497         for (counter = 0; counter < MAX_COMBINED_SAMPLERS; counter++)
498         {
499             if (This->textures[counter]) IWineD3DBaseTexture_Release(This->textures[counter]);
500         }
501
502         for (counter = 0; counter < MAX_STREAMS; counter++) {
503             if(This->streamSource[counter]) {
504                 if (IWineD3DBuffer_Release(This->streamSource[counter]))
505                 {
506                     TRACE("Vertex buffer still referenced by stateblock, applications has leaked Stream %u, buffer %p\n", counter, This->streamSource[counter]);
507                 }
508             }
509         }
510         if(This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
511         if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
512         if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
513
514         for(counter = 0; counter < LIGHTMAP_SIZE; counter++) {
515             struct list *e1, *e2;
516             LIST_FOR_EACH_SAFE(e1, e2, &This->lightMap[counter])
517             {
518                 struct wined3d_light_info *light = LIST_ENTRY(e1, struct wined3d_light_info, entry);
519                 list_remove(&light->entry);
520                 HeapFree(GetProcessHeap(), 0, light);
521             }
522         }
523
524         HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
525         HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
526         HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
527         HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
528         HeapFree(GetProcessHeap(), 0, This->contained_vs_consts_f);
529         HeapFree(GetProcessHeap(), 0, This->contained_ps_consts_f);
530         HeapFree(GetProcessHeap(), 0, This);
531     }
532     return refCount;
533 }
534
535 /**********************************************************
536  * IWineD3DStateBlockImpl parts follows
537  **********************************************************/
538 static void record_lights(IWineD3DStateBlockImpl *This, const IWineD3DStateBlockImpl *targetStateBlock)
539 {
540     UINT i;
541
542     /* Lights... For a recorded state block, we just had a chain of actions to perform,
543      * so we need to walk that chain and update any actions which differ
544      */
545     for(i = 0; i < LIGHTMAP_SIZE; i++) {
546         struct list *e, *f;
547         LIST_FOR_EACH(e, &This->lightMap[i]) {
548             BOOL updated = FALSE;
549             struct wined3d_light_info *src = LIST_ENTRY(e, struct wined3d_light_info, entry), *realLight;
550
551             /* Look up the light in the destination */
552             LIST_FOR_EACH(f, &targetStateBlock->lightMap[i]) {
553                 realLight = LIST_ENTRY(f, struct wined3d_light_info, entry);
554                 if (realLight->OriginalIndex == src->OriginalIndex)
555                 {
556                     src->OriginalParms = realLight->OriginalParms;
557
558                     if (realLight->glIndex == -1 && src->glIndex != -1)
559                     {
560                         /* Light disabled */
561                         This->activeLights[src->glIndex] = NULL;
562                     }
563                     else if (realLight->glIndex != -1 && src->glIndex == -1)
564                     {
565                         /* Light enabled */
566                         This->activeLights[realLight->glIndex] = src;
567                     }
568                     src->glIndex = realLight->glIndex;
569                     updated = TRUE;
570                     break;
571                 }
572             }
573
574             if (!updated)
575             {
576                 ERR("Light %u in stateblock %p does not exist in device stateblock %p.\n",
577                         src->OriginalIndex, This, targetStateBlock);
578             }
579         }
580     }
581 }
582
583 static HRESULT WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface)
584 {
585     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
586     IWineD3DStateBlockImpl *targetStateBlock = This->device->stateBlock;
587     unsigned int i;
588     DWORD map;
589
590     TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock, This);
591
592     if (This->changed.vertexShader && This->vertexShader != targetStateBlock->vertexShader)
593     {
594         TRACE("Updating vertex shader from %p to %p\n", This->vertexShader, targetStateBlock->vertexShader);
595
596         if (targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
597         if (This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
598         This->vertexShader = targetStateBlock->vertexShader;
599     }
600
601     /* Vertex Shader Float Constants */
602     for (i = 0; i < This->num_contained_vs_consts_f; ++i)
603     {
604         unsigned int idx = This->contained_vs_consts_f[i];
605
606         TRACE("Setting %p from %p %u to {%.8e, %.8e, %.8e, %.8e}.\n",
607                 This, targetStateBlock, idx,
608                 targetStateBlock->vertexShaderConstantF[idx * 4 + 0],
609                 targetStateBlock->vertexShaderConstantF[idx * 4 + 1],
610                 targetStateBlock->vertexShaderConstantF[idx * 4 + 2],
611                 targetStateBlock->vertexShaderConstantF[idx * 4 + 3]);
612
613         This->vertexShaderConstantF[idx * 4 + 0] = targetStateBlock->vertexShaderConstantF[idx * 4 + 0];
614         This->vertexShaderConstantF[idx * 4 + 1] = targetStateBlock->vertexShaderConstantF[idx * 4 + 1];
615         This->vertexShaderConstantF[idx * 4 + 2] = targetStateBlock->vertexShaderConstantF[idx * 4 + 2];
616         This->vertexShaderConstantF[idx * 4 + 3] = targetStateBlock->vertexShaderConstantF[idx * 4 + 3];
617     }
618
619     /* Vertex Shader Integer Constants */
620     for (i = 0; i < This->num_contained_vs_consts_i; ++i)
621     {
622         unsigned int idx = This->contained_vs_consts_i[i];
623
624         TRACE("Setting %p from %p %u to {%d, %d, %d, %d}.\n",
625                 This, targetStateBlock, idx,
626                 targetStateBlock->vertexShaderConstantI[idx * 4 + 0],
627                 targetStateBlock->vertexShaderConstantI[idx * 4 + 1],
628                 targetStateBlock->vertexShaderConstantI[idx * 4 + 2],
629                 targetStateBlock->vertexShaderConstantI[idx * 4 + 3]);
630
631         This->vertexShaderConstantI[idx * 4 + 0] = targetStateBlock->vertexShaderConstantI[idx * 4 + 0];
632         This->vertexShaderConstantI[idx * 4 + 1] = targetStateBlock->vertexShaderConstantI[idx * 4 + 1];
633         This->vertexShaderConstantI[idx * 4 + 2] = targetStateBlock->vertexShaderConstantI[idx * 4 + 2];
634         This->vertexShaderConstantI[idx * 4 + 3] = targetStateBlock->vertexShaderConstantI[idx * 4 + 3];
635     }
636
637     /* Vertex Shader Boolean Constants */
638     for (i = 0; i < This->num_contained_vs_consts_b; ++i)
639     {
640         unsigned int idx = This->contained_vs_consts_b[i];
641
642         TRACE("Setting %p from %p %u to %s.\n",
643                 This, targetStateBlock, idx,
644                 targetStateBlock->vertexShaderConstantB[idx] ? "TRUE" : "FALSE");
645
646         This->vertexShaderConstantB[idx] = targetStateBlock->vertexShaderConstantB[idx];
647     }
648
649     /* Pixel Shader Float Constants */
650     for (i = 0; i < This->num_contained_ps_consts_f; ++i)
651     {
652         unsigned int idx = This->contained_ps_consts_f[i];
653
654         TRACE("Setting %p from %p %u to {%.8e, %.8e, %.8e, %.8e}.\n",
655                 This, targetStateBlock, idx,
656                 targetStateBlock->pixelShaderConstantF[idx * 4 + 0],
657                 targetStateBlock->pixelShaderConstantF[idx * 4 + 1],
658                 targetStateBlock->pixelShaderConstantF[idx * 4 + 2],
659                 targetStateBlock->pixelShaderConstantF[idx * 4 + 3]);
660
661         This->pixelShaderConstantF[idx * 4 + 0] = targetStateBlock->pixelShaderConstantF[idx * 4 + 0];
662         This->pixelShaderConstantF[idx * 4 + 1] = targetStateBlock->pixelShaderConstantF[idx * 4 + 1];
663         This->pixelShaderConstantF[idx * 4 + 2] = targetStateBlock->pixelShaderConstantF[idx * 4 + 2];
664         This->pixelShaderConstantF[idx * 4 + 3] = targetStateBlock->pixelShaderConstantF[idx * 4 + 3];
665     }
666
667     /* Pixel Shader Integer Constants */
668     for (i = 0; i < This->num_contained_ps_consts_i; ++i)
669     {
670         unsigned int idx = This->contained_ps_consts_i[i];
671         TRACE("Setting %p from %p %u to {%d, %d, %d, %d}.\n",
672                 This, targetStateBlock, idx,
673                 targetStateBlock->pixelShaderConstantI[idx * 4 + 0],
674                 targetStateBlock->pixelShaderConstantI[idx * 4 + 1],
675                 targetStateBlock->pixelShaderConstantI[idx * 4 + 2],
676                 targetStateBlock->pixelShaderConstantI[idx * 4 + 3]);
677
678         This->pixelShaderConstantI[idx * 4 + 0] = targetStateBlock->pixelShaderConstantI[idx * 4 + 0];
679         This->pixelShaderConstantI[idx * 4 + 1] = targetStateBlock->pixelShaderConstantI[idx * 4 + 1];
680         This->pixelShaderConstantI[idx * 4 + 2] = targetStateBlock->pixelShaderConstantI[idx * 4 + 2];
681         This->pixelShaderConstantI[idx * 4 + 3] = targetStateBlock->pixelShaderConstantI[idx * 4 + 3];
682     }
683
684     /* Pixel Shader Boolean Constants */
685     for (i = 0; i < This->num_contained_ps_consts_b; ++i)
686     {
687         unsigned int idx = This->contained_ps_consts_b[i];
688         TRACE("Setting %p from %p %u to %s.\n", This, targetStateBlock, idx,
689                 targetStateBlock->pixelShaderConstantB[idx] ? "TRUE" : "FALSE");
690
691         This->pixelShaderConstantB[idx] = targetStateBlock->pixelShaderConstantB[idx];
692     }
693
694     /* Others + Render & Texture */
695     for (i = 0; i < This->num_contained_transform_states; ++i)
696     {
697         WINED3DTRANSFORMSTATETYPE transform = This->contained_transform_states[i];
698
699         TRACE("Updating transform %#x.\n", transform);
700
701         This->transforms[transform] = targetStateBlock->transforms[transform];
702     }
703
704     if (This->changed.primitive_type) This->gl_primitive_type = targetStateBlock->gl_primitive_type;
705
706     if (This->changed.indices
707             && ((This->pIndexData != targetStateBlock->pIndexData)
708                 || (This->baseVertexIndex != targetStateBlock->baseVertexIndex)
709                 || (This->IndexFmt != targetStateBlock->IndexFmt)))
710     {
711         TRACE("Updating pIndexData to %p, baseVertexIndex to %d.\n",
712                 targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
713
714         if (targetStateBlock->pIndexData) IWineD3DBuffer_AddRef(targetStateBlock->pIndexData);
715         if (This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
716         This->pIndexData = targetStateBlock->pIndexData;
717         This->baseVertexIndex = targetStateBlock->baseVertexIndex;
718         This->IndexFmt = targetStateBlock->IndexFmt;
719     }
720
721     if (This->changed.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl
722             && (This->blockType != WINED3DSBT_RECORDED
723             || ((IWineD3DImpl *)This->device->wined3d)->dxVersion != 9))
724     {
725         TRACE("Updating vertex declaration from %p to %p.\n", This->vertexDecl, targetStateBlock->vertexDecl);
726
727         if (targetStateBlock->vertexDecl) IWineD3DVertexDeclaration_AddRef(targetStateBlock->vertexDecl);
728         if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
729         This->vertexDecl = targetStateBlock->vertexDecl;
730     }
731
732     if (This->changed.material
733             && memcmp(&targetStateBlock->material, &This->material, sizeof(This->material)))
734     {
735         TRACE("Updating material.\n");
736
737         This->material = targetStateBlock->material;
738     }
739
740     if (This->changed.viewport
741             && memcmp(&targetStateBlock->viewport, &This->viewport, sizeof(This->viewport)))
742     {
743         TRACE("Updating viewport.\n");
744
745         This->viewport = targetStateBlock->viewport;
746     }
747
748     if(This->changed.scissorRect
749             && memcmp(&targetStateBlock->scissorRect, &This->scissorRect, sizeof(This->scissorRect)))
750     {
751         TRACE("Updating scissor rect.\n");
752
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
767             This->streamStride[i] = targetStateBlock->streamStride[i];
768             if (targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
769             if (This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
770             This->streamSource[i] = targetStateBlock->streamSource[i];
771         }
772     }
773
774     map = This->changed.streamFreq;
775     for (i = 0; map; map >>= 1, ++i)
776     {
777         if (!(map & 1)) continue;
778
779         if (This->streamFreq[i] != targetStateBlock->streamFreq[i]
780                 || This->streamFlags[i] != targetStateBlock->streamFlags[i])
781         {
782             TRACE("Updating stream frequency %u to %u flags to %#x.\n",
783                     i, targetStateBlock->streamFreq[i], targetStateBlock->streamFlags[i]);
784
785             This->streamFreq[i] = targetStateBlock->streamFreq[i];
786             This->streamFlags[i] = targetStateBlock->streamFlags[i];
787         }
788     }
789
790     map = This->changed.clipplane;
791     for (i = 0; map; map >>= 1, ++i)
792     {
793         if (!(map & 1)) continue;
794
795         if (memcmp(targetStateBlock->clipplane[i], This->clipplane[i], sizeof(*This->clipplane)))
796         {
797             TRACE("Updating clipplane %u.\n", i);
798             memcpy(This->clipplane[i], targetStateBlock->clipplane[i], sizeof(*This->clipplane));
799         }
800     }
801
802     /* Render */
803     for (i = 0; i < This->num_contained_render_states; ++i)
804     {
805         WINED3DRENDERSTATETYPE rs = This->contained_render_states[i];
806
807         TRACE("Updating renderState %#x to %u.\n", rs, targetStateBlock->renderState[rs]);
808
809         This->renderState[rs] = targetStateBlock->renderState[rs];
810     }
811
812     /* Texture states */
813     for (i = 0; i < This->num_contained_tss_states; ++i)
814     {
815         DWORD stage = This->contained_tss_states[i].stage;
816         DWORD state = This->contained_tss_states[i].state;
817
818         TRACE("Updating texturestage state %u, %u to %u (was %u).\n", stage, state,
819                 targetStateBlock->textureState[stage][state], This->textureState[stage][state]);
820
821         This->textureState[stage][state] = targetStateBlock->textureState[stage][state];
822     }
823
824     /* Samplers */
825     map = This->changed.textures;
826     for (i = 0; map; map >>= 1, ++i)
827     {
828         if (!(map & 1)) continue;
829
830         TRACE("Updating texture %u to %p (was %p).\n", i, targetStateBlock->textures[i], This->textures[i]);
831
832         if (targetStateBlock->textures[i]) IWineD3DBaseTexture_AddRef(targetStateBlock->textures[i]);
833         if (This->textures[i]) IWineD3DBaseTexture_Release(This->textures[i]);
834         This->textures[i] = targetStateBlock->textures[i];
835     }
836
837     for (i = 0; i < This->num_contained_sampler_states; ++i)
838     {
839         DWORD stage = This->contained_sampler_states[i].stage;
840         DWORD state = This->contained_sampler_states[i].state;
841
842         TRACE("Updating sampler state %u, %u to %u (was %u).\n", stage, state,
843                 targetStateBlock->samplerState[stage][state], This->samplerState[stage][state]);
844
845         This->samplerState[stage][state] = targetStateBlock->samplerState[stage][state];
846     }
847
848     if (This->changed.pixelShader && This->pixelShader != targetStateBlock->pixelShader)
849     {
850         if (targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
851         if (This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
852         This->pixelShader = targetStateBlock->pixelShader;
853     }
854
855     record_lights(This, targetStateBlock);
856
857     TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
858
859     return WINED3D_OK;
860 }
861
862 static void apply_lights(IWineD3DDevice *pDevice, const IWineD3DStateBlockImpl *This)
863 {
864     UINT i;
865     for(i = 0; i < LIGHTMAP_SIZE; i++) {
866         struct list *e;
867
868         LIST_FOR_EACH(e, &This->lightMap[i])
869         {
870             const struct wined3d_light_info *light = LIST_ENTRY(e, struct wined3d_light_info, entry);
871
872             IWineD3DDevice_SetLight(pDevice, light->OriginalIndex, &light->OriginalParms);
873             IWineD3DDevice_SetLightEnable(pDevice, light->OriginalIndex, light->glIndex != -1);
874         }
875     }
876 }
877
878 static HRESULT WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface)
879 {
880     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
881     IWineD3DDevice *pDevice = (IWineD3DDevice *)This->device;
882     unsigned int i;
883     DWORD map;
884
885     TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
886
887     TRACE("Blocktype: %d\n", This->blockType);
888
889     if (This->changed.vertexShader) IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
890
891     /* Vertex Shader Constants */
892     for (i = 0; i < This->num_contained_vs_consts_f; ++i)
893     {
894         IWineD3DDevice_SetVertexShaderConstantF(pDevice, This->contained_vs_consts_f[i],
895                 This->vertexShaderConstantF + This->contained_vs_consts_f[i] * 4, 1);
896     }
897     for (i = 0; i < This->num_contained_vs_consts_i; ++i)
898     {
899         IWineD3DDevice_SetVertexShaderConstantI(pDevice, This->contained_vs_consts_i[i],
900                 This->vertexShaderConstantI + This->contained_vs_consts_i[i] * 4, 1);
901     }
902     for (i = 0; i < This->num_contained_vs_consts_b; ++i)
903     {
904         IWineD3DDevice_SetVertexShaderConstantB(pDevice, This->contained_vs_consts_b[i],
905                 This->vertexShaderConstantB + This->contained_vs_consts_b[i], 1);
906     }
907
908     apply_lights(pDevice, This);
909
910     if (This->changed.pixelShader) IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
911
912     /* Pixel Shader Constants */
913     for (i = 0; i < This->num_contained_ps_consts_f; ++i)
914     {
915         IWineD3DDevice_SetPixelShaderConstantF(pDevice, This->contained_ps_consts_f[i],
916                 This->pixelShaderConstantF + This->contained_ps_consts_f[i] * 4, 1);
917     }
918     for (i = 0; i < This->num_contained_ps_consts_i; ++i)
919     {
920         IWineD3DDevice_SetPixelShaderConstantI(pDevice, This->contained_ps_consts_i[i],
921                 This->pixelShaderConstantI + This->contained_ps_consts_i[i] * 4, 1);
922     }
923     for (i = 0; i < This->num_contained_ps_consts_b; ++i)
924     {
925         IWineD3DDevice_SetPixelShaderConstantB(pDevice, This->contained_ps_consts_b[i],
926                 This->pixelShaderConstantB + This->contained_ps_consts_b[i], 1);
927     }
928
929     /* Render */
930     for (i = 0; i < This->num_contained_render_states; ++i)
931     {
932         IWineD3DDevice_SetRenderState(pDevice, This->contained_render_states[i],
933                 This->renderState[This->contained_render_states[i]]);
934     }
935
936     /* Texture states */
937     for (i = 0; i < This->num_contained_tss_states; ++i)
938     {
939         DWORD stage = This->contained_tss_states[i].stage;
940         DWORD state = This->contained_tss_states[i].state;
941
942         IWineD3DDevice_SetTextureStageState(pDevice, stage, state, This->textureState[stage][state]);
943     }
944
945     /* Sampler states */
946     for (i = 0; i < This->num_contained_sampler_states; ++i)
947     {
948         DWORD stage = This->contained_sampler_states[i].stage;
949         DWORD state = This->contained_sampler_states[i].state;
950         DWORD value = This->samplerState[stage][state];
951
952         if (stage >= MAX_FRAGMENT_SAMPLERS) stage += WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS;
953         IWineD3DDevice_SetSamplerState(pDevice, stage, state, value);
954     }
955
956     for (i = 0; i < This->num_contained_transform_states; ++i)
957     {
958         IWineD3DDevice_SetTransform(pDevice, This->contained_transform_states[i],
959                 &This->transforms[This->contained_transform_states[i]]);
960     }
961
962     if (This->changed.primitive_type)
963     {
964         This->device->updateStateBlock->changed.primitive_type = TRUE;
965         This->device->updateStateBlock->gl_primitive_type = This->gl_primitive_type;
966     }
967
968     if (This->changed.indices)
969     {
970         IWineD3DDevice_SetIndexBuffer(pDevice, This->pIndexData, This->IndexFmt);
971         IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
972     }
973
974     if (This->changed.vertexDecl)
975     {
976         IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
977     }
978
979     if (This->changed.material)
980     {
981         IWineD3DDevice_SetMaterial(pDevice, &This->material);
982     }
983
984     if (This->changed.viewport)
985     {
986         IWineD3DDevice_SetViewport(pDevice, &This->viewport);
987     }
988
989     if (This->changed.scissorRect)
990     {
991         IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
992     }
993
994     map = This->changed.streamSource;
995     for (i = 0; map; map >>= 1, ++i)
996     {
997         if (map & 1) IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
998     }
999
1000     map = This->changed.streamFreq;
1001     for (i = 0; map; map >>= 1, ++i)
1002     {
1003         if (map & 1) IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
1004     }
1005
1006     map = This->changed.textures;
1007     for (i = 0; map; map >>= 1, ++i)
1008     {
1009         DWORD stage;
1010
1011         if (!(map & 1)) continue;
1012
1013         stage = i < MAX_FRAGMENT_SAMPLERS ? i : WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS;
1014         IWineD3DDevice_SetTexture(pDevice, stage, This->textures[i]);
1015     }
1016
1017     map = This->changed.clipplane;
1018     for (i = 0; map; map >>= 1, ++i)
1019     {
1020         float clip[4];
1021
1022         if (!(map & 1)) continue;
1023
1024         clip[0] = This->clipplane[i][0];
1025         clip[1] = This->clipplane[i][1];
1026         clip[2] = This->clipplane[i][2];
1027         clip[3] = This->clipplane[i][3];
1028         IWineD3DDevice_SetClipPlane(pDevice, i, clip);
1029     }
1030
1031     This->device->stateBlock->lowest_disabled_stage = MAX_TEXTURES - 1;
1032     for (i = 0; i < MAX_TEXTURES - 1; ++i)
1033     {
1034         if (This->device->stateBlock->textureState[i][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE)
1035         {
1036             This->device->stateBlock->lowest_disabled_stage = i;
1037             break;
1038         }
1039     }
1040     TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
1041
1042     return WINED3D_OK;
1043 }
1044
1045 static HRESULT  WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
1046     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
1047     IWineD3DDevice *device = (IWineD3DDevice *)This->device;
1048     IWineD3DDeviceImpl     *ThisDevice = (IWineD3DDeviceImpl *)device;
1049     const struct wined3d_gl_info *gl_info = &ThisDevice->adapter->gl_info;
1050     union {
1051         WINED3DLINEPATTERN lp;
1052         DWORD d;
1053     } lp;
1054     union {
1055         float f;
1056         DWORD d;
1057     } tmpfloat;
1058     unsigned int i;
1059     IWineD3DSwapChain *swapchain;
1060     IWineD3DSurface *backbuffer;
1061     HRESULT hr;
1062
1063     /* Note this may have a large overhead but it should only be executed
1064        once, in order to initialize the complete state of the device and
1065        all opengl equivalents                                            */
1066     TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, ThisDevice);
1067     /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
1068     This->blockType = WINED3DSBT_INIT;
1069
1070     /* Set some of the defaults for lights, transforms etc */
1071     memcpy(&This->transforms[WINED3DTS_PROJECTION], identity, sizeof(identity));
1072     memcpy(&This->transforms[WINED3DTS_VIEW], identity, sizeof(identity));
1073     for (i = 0; i < 256; ++i) {
1074       memcpy(&This->transforms[WINED3DTS_WORLDMATRIX(i)], identity, sizeof(identity));
1075     }
1076
1077     TRACE("Render states\n");
1078     /* Render states: */
1079     if (ThisDevice->auto_depth_stencil_buffer != NULL) {
1080        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       WINED3DZB_TRUE);
1081     } else {
1082        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       WINED3DZB_FALSE);
1083     }
1084     IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE,         WINED3DFILL_SOLID);
1085     IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE,        WINED3DSHADE_GOURAUD);
1086     lp.lp.wRepeatFactor = 0;
1087     lp.lp.wLinePattern  = 0;
1088     IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN,      lp.d);
1089     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE,     TRUE);
1090     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE,  FALSE);
1091     IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL,        TRUE);
1092     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND,         WINED3DBLEND_ONE);
1093     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND,        WINED3DBLEND_ZERO);
1094     IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE,         WINED3DCULL_CCW);
1095     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC,            WINED3DCMP_LESSEQUAL);
1096     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC,        WINED3DCMP_ALWAYS);
1097     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF,         0);
1098     IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE,     FALSE);
1099     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
1100     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE,        FALSE);
1101     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE,   FALSE);
1102     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE,         0);
1103     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR,         0);
1104     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE,     WINED3DFOG_NONE);
1105     tmpfloat.f = 0.0f;
1106     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART,         tmpfloat.d);
1107     tmpfloat.f = 1.0f;
1108     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND,           tmpfloat.d);
1109     tmpfloat.f = 1.0f;
1110     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY,       tmpfloat.d);
1111     IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS,    FALSE);
1112     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS,            0);
1113     IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE,   FALSE);
1114     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE,    FALSE);
1115     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL,      WINED3DSTENCILOP_KEEP);
1116     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL,     WINED3DSTENCILOP_KEEP);
1117     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS,      WINED3DSTENCILOP_KEEP);
1118     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF,       0);
1119     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK,      0xFFFFFFFF);
1120     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC,      WINED3DCMP_ALWAYS);
1121     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
1122     IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR,    0xFFFFFFFF);
1123     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
1124     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
1125     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
1126     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
1127     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
1128     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
1129     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
1130     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
1131     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING,                 TRUE);
1132     IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING,                 TRUE);
1133     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT,                  0);
1134     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE,            WINED3DFOG_NONE);
1135     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX,              TRUE);
1136     IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER,              TRUE);
1137     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS,         FALSE);
1138     IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE,    WINED3DMCS_COLOR1);
1139     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE,   WINED3DMCS_COLOR2);
1140     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE,    WINED3DMCS_MATERIAL);
1141     IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE,   WINED3DMCS_MATERIAL);
1142     IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND,              WINED3DVBF_DISABLE);
1143     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE,          0);
1144     IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
1145     tmpfloat.f = 1.0f;
1146     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE,                tmpfloat.d);
1147     tmpfloat.f = 1.0f;
1148     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN,            tmpfloat.d);
1149     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE,        FALSE);
1150     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE,         FALSE);
1151     tmpfloat.f = 1.0f;
1152     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A,             tmpfloat.d);
1153     tmpfloat.f = 0.0f;
1154     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B,             tmpfloat.d);
1155     tmpfloat.f = 0.0f;
1156     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C,             tmpfloat.d);
1157     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS,     TRUE);
1158     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK,          0xFFFFFFFF);
1159     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE,           WINED3DPATCHEDGE_DISCRETE);
1160     tmpfloat.f = 1.0f;
1161     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS,            tmpfloat.d);
1162     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN,        0xbaadcafe);
1163     tmpfloat.f = gl_info->limits.pointsize_max;
1164     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX,            tmpfloat.d);
1165     IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
1166     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE,         0x0000000F);
1167     tmpfloat.f = 0.0f;
1168     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR,              tmpfloat.d);
1169     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP,                  WINED3DBLENDOP_ADD);
1170     IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE,           WINED3DDEGREE_CUBIC);
1171     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE,             WINED3DDEGREE_LINEAR);
1172     /* states new in d3d9 */
1173     IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE,        FALSE);
1174     IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS,      0);
1175     tmpfloat.f = 1.0f;
1176     IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL,     tmpfloat.d);
1177     IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL,     tmpfloat.d);
1178     IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE,    FALSE);
1179     tmpfloat.f = 0.0f;
1180     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X,           tmpfloat.d);
1181     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y,           tmpfloat.d);
1182     tmpfloat.f = 1.0f;
1183     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z,           tmpfloat.d);
1184     tmpfloat.f = 0.0f;
1185     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W,           tmpfloat.d);
1186     IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
1187     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE,      FALSE);
1188     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL,          WINED3DSTENCILOP_KEEP);
1189     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL,         WINED3DSTENCILOP_KEEP);
1190     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS,          WINED3DSTENCILOP_KEEP);
1191     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC,          WINED3DCMP_ALWAYS);
1192     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1,        0x0000000F);
1193     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2,        0x0000000F);
1194     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3,        0x0000000F);
1195     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR,              0xFFFFFFFF);
1196     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE,          0);
1197     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS,                0);
1198     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8,  0);
1199     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9,  0);
1200     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
1201     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
1202     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
1203     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
1204     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
1205     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
1206     IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1207     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA,            WINED3DBLEND_ONE);
1208     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA,           WINED3DBLEND_ZERO);
1209     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA,             WINED3DBLENDOP_ADD);
1210
1211     /* clipping status */
1212     This->clip_status.ClipUnion = 0;
1213     This->clip_status.ClipIntersection = 0xFFFFFFFF;
1214
1215     /* Texture Stage States - Put directly into state block, we will call function below */
1216     for (i = 0; i < MAX_TEXTURES; i++) {
1217         TRACE("Setting up default texture states for texture Stage %d\n", i);
1218         memcpy(&This->transforms[WINED3DTS_TEXTURE0 + i], identity, sizeof(identity));
1219         This->textureState[i][WINED3DTSS_COLOROP               ] = (i==0)? WINED3DTOP_MODULATE :  WINED3DTOP_DISABLE;
1220         This->textureState[i][WINED3DTSS_COLORARG1             ] = WINED3DTA_TEXTURE;
1221         This->textureState[i][WINED3DTSS_COLORARG2             ] = WINED3DTA_CURRENT;
1222         This->textureState[i][WINED3DTSS_ALPHAOP               ] = (i==0)? WINED3DTOP_SELECTARG1 :  WINED3DTOP_DISABLE;
1223         This->textureState[i][WINED3DTSS_ALPHAARG1             ] = WINED3DTA_TEXTURE;
1224         This->textureState[i][WINED3DTSS_ALPHAARG2             ] = WINED3DTA_CURRENT;
1225         This->textureState[i][WINED3DTSS_BUMPENVMAT00          ] = 0;
1226         This->textureState[i][WINED3DTSS_BUMPENVMAT01          ] = 0;
1227         This->textureState[i][WINED3DTSS_BUMPENVMAT10          ] = 0;
1228         This->textureState[i][WINED3DTSS_BUMPENVMAT11          ] = 0;
1229         This->textureState[i][WINED3DTSS_TEXCOORDINDEX         ] = i;
1230         This->textureState[i][WINED3DTSS_BUMPENVLSCALE         ] = 0;
1231         This->textureState[i][WINED3DTSS_BUMPENVLOFFSET        ] = 0;
1232         This->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS ] = WINED3DTTFF_DISABLE;
1233         This->textureState[i][WINED3DTSS_COLORARG0             ] = WINED3DTA_CURRENT;
1234         This->textureState[i][WINED3DTSS_ALPHAARG0             ] = WINED3DTA_CURRENT;
1235         This->textureState[i][WINED3DTSS_RESULTARG             ] = WINED3DTA_CURRENT;
1236     }
1237     This->lowest_disabled_stage = 1;
1238
1239         /* Sampler states*/
1240     for (i = 0 ; i <  MAX_COMBINED_SAMPLERS; i++) {
1241         TRACE("Setting up default samplers states for sampler %d\n", i);
1242         This->samplerState[i][WINED3DSAMP_ADDRESSU         ] = WINED3DTADDRESS_WRAP;
1243         This->samplerState[i][WINED3DSAMP_ADDRESSV         ] = WINED3DTADDRESS_WRAP;
1244         This->samplerState[i][WINED3DSAMP_ADDRESSW         ] = WINED3DTADDRESS_WRAP;
1245         This->samplerState[i][WINED3DSAMP_BORDERCOLOR      ] = 0x00;
1246         This->samplerState[i][WINED3DSAMP_MAGFILTER        ] = WINED3DTEXF_POINT;
1247         This->samplerState[i][WINED3DSAMP_MINFILTER        ] = WINED3DTEXF_POINT;
1248         This->samplerState[i][WINED3DSAMP_MIPFILTER        ] = WINED3DTEXF_NONE;
1249         This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS    ] = 0;
1250         This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL      ] = 0;
1251         This->samplerState[i][WINED3DSAMP_MAXANISOTROPY    ] = 1;
1252         This->samplerState[i][WINED3DSAMP_SRGBTEXTURE      ] = 0;
1253         This->samplerState[i][WINED3DSAMP_ELEMENTINDEX     ] = 0; /* TODO: Indicates which element of a  multielement texture to use */
1254         This->samplerState[i][WINED3DSAMP_DMAPOFFSET       ] = 0; /* TODO: Vertex offset in the presampled displacement map */
1255     }
1256
1257     for (i = 0; i < gl_info->limits.textures; ++i)
1258     {
1259         /* Note: This avoids calling SetTexture, so pretend it has been called */
1260         This->changed.textures |= 1 << i;
1261         This->textures[i]         = NULL;
1262     }
1263
1264     /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1265     hr = IWineD3DDevice_GetSwapChain(device, 0, &swapchain);
1266     if( hr == WINED3D_OK && swapchain != NULL) {
1267         WINED3DVIEWPORT vp;
1268
1269         hr = IWineD3DSwapChain_GetBackBuffer(swapchain, 0, WINED3DBACKBUFFER_TYPE_MONO, &backbuffer);
1270         if (SUCCEEDED(hr) && backbuffer)
1271         {
1272             WINED3DSURFACE_DESC desc;
1273             RECT scissorrect;
1274
1275             IWineD3DSurface_GetDesc(backbuffer, &desc);
1276             IWineD3DSurface_Release(backbuffer);
1277
1278             /* Set the default scissor rect values */
1279             scissorrect.left = 0;
1280             scissorrect.right = desc.width;
1281             scissorrect.top = 0;
1282             scissorrect.bottom = desc.height;
1283             hr = IWineD3DDevice_SetScissorRect(device, &scissorrect);
1284             if (FAILED(hr)) ERR("This should never happen, expect rendering issues!\n");
1285         }
1286
1287         /* Set the default viewport */
1288         vp.X      = 0;
1289         vp.Y      = 0;
1290         vp.Width  = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferWidth;
1291         vp.Height = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferHeight;
1292         vp.MinZ   = 0.0f;
1293         vp.MaxZ   = 1.0f;
1294         IWineD3DDevice_SetViewport(device, &vp);
1295
1296         IWineD3DSwapChain_Release(swapchain);
1297     }
1298
1299     TRACE("-----------------------> Device defaults now set up...\n");
1300     return WINED3D_OK;
1301 }
1302
1303 /**********************************************************
1304  * IWineD3DStateBlock VTbl follows
1305  **********************************************************/
1306
1307 static const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
1308 {
1309     /* IUnknown */
1310     IWineD3DStateBlockImpl_QueryInterface,
1311     IWineD3DStateBlockImpl_AddRef,
1312     IWineD3DStateBlockImpl_Release,
1313     /* IWineD3DStateBlock */
1314     IWineD3DStateBlockImpl_Capture,
1315     IWineD3DStateBlockImpl_Apply,
1316     IWineD3DStateBlockImpl_InitStartupStateBlock
1317 };
1318
1319 HRESULT stateblock_init(IWineD3DStateBlockImpl *stateblock, IWineD3DDeviceImpl *device, WINED3DSTATEBLOCKTYPE type)
1320 {
1321     unsigned int i;
1322     HRESULT hr;
1323
1324     stateblock->lpVtbl = &IWineD3DStateBlock_Vtbl;
1325     stateblock->ref = 1;
1326     stateblock->device = device;
1327     stateblock->blockType = type;
1328
1329     for (i = 0; i < LIGHTMAP_SIZE; i++)
1330     {
1331         list_init(&stateblock->lightMap[i]);
1332     }
1333
1334     hr = stateblock_allocate_shader_constants(stateblock);
1335     if (FAILED(hr)) return hr;
1336
1337     /* The WINED3DSBT_INIT stateblock type is used during initialization to
1338      * produce a placeholder stateblock so other functions called can update a
1339      * state block. */
1340     if (type == WINED3DSBT_INIT || type == WINED3DSBT_RECORDED) return WINED3D_OK;
1341
1342     TRACE("Updating changed flags appropriate for type %#x.\n", type);
1343
1344     switch (type)
1345     {
1346         case WINED3DSBT_ALL:
1347             stateblock_init_lights(stateblock, device->stateBlock->lightMap);
1348             stateblock_savedstates_set_all(&stateblock->changed, device->d3d_vshader_constantF,
1349                                            device->d3d_pshader_constantF);
1350             break;
1351
1352         case WINED3DSBT_PIXELSTATE:
1353             stateblock_savedstates_set_pixel(&stateblock->changed, device->d3d_pshader_constantF);
1354             break;
1355
1356         case WINED3DSBT_VERTEXSTATE:
1357             stateblock_init_lights(stateblock, device->stateBlock->lightMap);
1358             stateblock_savedstates_set_vertex(&stateblock->changed, device->d3d_vshader_constantF);
1359             break;
1360
1361         default:
1362             FIXME("Unrecognized state block type %#x.\n", type);
1363             break;
1364     }
1365
1366     stateblock_init_contained_states(stateblock);
1367     IWineD3DStateBlockImpl_Capture((IWineD3DStateBlock *)stateblock);
1368
1369     return WINED3D_OK;
1370 }