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