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