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