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