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