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