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