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