wined3d: Only call activate_dimensions() if the texture is used.
[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  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
28
29 /***************************************
30  * Stateblock helper functions follow
31  **************************************/
32
33 /** Allocates the correct amount of space for pixel and vertex shader constants, 
34  * along with their set/changed flags on the given stateblock object
35  */
36 HRESULT allocate_shader_constants(IWineD3DStateBlockImpl* object) {
37     
38     IWineD3DStateBlockImpl *This = object;
39
40 #define WINED3D_MEMCHECK(_object) if (NULL == _object) { FIXME("Out of memory!\n"); return E_OUTOFMEMORY; }
41
42     /* Allocate space for floating point constants */
43     object->pixelShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
44     WINED3D_MEMCHECK(object->pixelShaderConstantF);
45     object->set.pixelShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(pshader_constantsF) );
46     WINED3D_MEMCHECK(object->set.pixelShaderConstantsF);
47     object->changed.pixelShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(pshader_constantsF));
48     WINED3D_MEMCHECK(object->changed.pixelShaderConstantsF);
49     object->vertexShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
50     WINED3D_MEMCHECK(object->vertexShaderConstantF);
51     object->set.vertexShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(vshader_constantsF));
52     WINED3D_MEMCHECK(object->set.vertexShaderConstantsF);
53     object->changed.vertexShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(vshader_constantsF));
54     WINED3D_MEMCHECK(object->changed.vertexShaderConstantsF);
55
56     list_init(&object->set_vconstantsF);
57     list_init(&object->set_pconstantsF);
58
59 #undef WINED3D_MEMCHECK
60
61     return WINED3D_OK;
62 }
63
64 /** Copy all members of one stateblock to another */
65 void stateblock_savedstates_copy(
66     IWineD3DStateBlock* iface,
67     SAVEDSTATES* dest,
68     SAVEDSTATES* source) {
69     
70     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
71     unsigned bsize = sizeof(BOOL);
72
73     /* Single values */
74     dest->indices = source->indices;
75     dest->material = source->material;
76     dest->fvf = source->fvf;
77     dest->viewport = source->viewport;
78     dest->vertexDecl = source->vertexDecl;
79     dest->pixelShader = source->pixelShader;
80     dest->vertexShader = source->vertexShader;
81     dest->scissorRect = dest->scissorRect;
82
83     /* Fixed size arrays */
84     memcpy(dest->streamSource, source->streamSource, bsize * MAX_STREAMS);
85     memcpy(dest->streamFreq, source->streamFreq, bsize * MAX_STREAMS);
86     memcpy(dest->textures, source->textures, bsize * MAX_COMBINED_SAMPLERS);
87     memcpy(dest->transform, source->transform, bsize * (HIGHEST_TRANSFORMSTATE + 1));
88     memcpy(dest->renderState, source->renderState, bsize * (WINEHIGHEST_RENDER_STATE + 1));
89     memcpy(dest->textureState, source->textureState, bsize * MAX_TEXTURES * (WINED3D_HIGHEST_TEXTURE_STATE + 1));
90     memcpy(dest->samplerState, source->samplerState, bsize * MAX_COMBINED_SAMPLERS * (WINED3D_HIGHEST_SAMPLER_STATE + 1));
91     memcpy(dest->clipplane, source->clipplane, bsize * MAX_CLIPPLANES);
92     memcpy(dest->pixelShaderConstantsB, source->pixelShaderConstantsB, bsize * MAX_CONST_B);
93     memcpy(dest->pixelShaderConstantsI, source->pixelShaderConstantsI, bsize * MAX_CONST_I);
94     memcpy(dest->vertexShaderConstantsB, source->vertexShaderConstantsB, bsize * MAX_CONST_B);
95     memcpy(dest->vertexShaderConstantsI, source->vertexShaderConstantsI, bsize * MAX_CONST_I);
96
97     /* Dynamically sized arrays */
98     memcpy(dest->pixelShaderConstantsF, source->pixelShaderConstantsF, bsize * GL_LIMITS(pshader_constantsF));
99     memcpy(dest->vertexShaderConstantsF, source->vertexShaderConstantsF, bsize * GL_LIMITS(vshader_constantsF));
100 }
101
102 /** Set all members of a stateblock savedstate to the given value */
103 void stateblock_savedstates_set(
104     IWineD3DStateBlock* iface,
105     SAVEDSTATES* states,
106     BOOL value) {
107     
108     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
109     unsigned bsize = sizeof(BOOL);
110
111     /* Single values */
112     states->indices = value;
113     states->material = value;
114     states->fvf = value;
115     states->viewport = value;
116     states->vertexDecl = value;
117     states->pixelShader = value;
118     states->vertexShader = value;
119     states->scissorRect = value;
120
121     /* Fixed size arrays */
122     memset(states->streamSource, value, bsize * MAX_STREAMS);
123     memset(states->streamFreq, value, bsize * MAX_STREAMS);
124     memset(states->textures, value, bsize * MAX_COMBINED_SAMPLERS);
125     memset(states->transform, value, bsize * (HIGHEST_TRANSFORMSTATE + 1));
126     memset(states->renderState, value, bsize * (WINEHIGHEST_RENDER_STATE + 1));
127     memset(states->textureState, value, bsize * MAX_TEXTURES * (WINED3D_HIGHEST_TEXTURE_STATE + 1));
128     memset(states->samplerState, value, bsize * MAX_COMBINED_SAMPLERS * (WINED3D_HIGHEST_SAMPLER_STATE + 1));
129     memset(states->clipplane, value, bsize * MAX_CLIPPLANES);
130     memset(states->pixelShaderConstantsB, value, bsize * MAX_CONST_B);
131     memset(states->pixelShaderConstantsI, value, bsize * MAX_CONST_I);
132     memset(states->vertexShaderConstantsB, value, bsize * MAX_CONST_B);
133     memset(states->vertexShaderConstantsI, value, bsize * MAX_CONST_I);
134
135     /* Dynamically sized arrays */
136     memset(states->pixelShaderConstantsF, value, bsize * GL_LIMITS(pshader_constantsF));
137     memset(states->vertexShaderConstantsF, value, bsize * GL_LIMITS(vshader_constantsF));
138 }
139
140 void stateblock_copy(
141     IWineD3DStateBlock* destination,
142     IWineD3DStateBlock* source) {
143     int l;
144
145     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)source;
146     IWineD3DStateBlockImpl *Dest = (IWineD3DStateBlockImpl *)destination;
147
148     /* IUnknown fields */
149     Dest->lpVtbl                = This->lpVtbl;
150     Dest->ref                   = This->ref;
151
152     /* IWineD3DStateBlock information */
153     Dest->parent                = This->parent;
154     Dest->wineD3DDevice         = This->wineD3DDevice;
155     Dest->blockType             = This->blockType;
156
157     /* Saved states */
158     stateblock_savedstates_copy(source, &Dest->set, &This->set);
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     memcpy(&Dest->scissorRect, &This->scissorRect, sizeof(Dest->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             memcpy(light2, light, sizeof(*light));
190             list_add_tail(&This->lightMap[l], &light2->entry);
191             if(light2->glIndex != -1) This->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             FIXME("Releasing primary stateblock\n");
258
259             /* NOTE: according to MSDN: The application is responsible for making sure the texture references are cleared down */
260             for (counter = 0; counter < MAX_COMBINED_SAMPLERS; counter++) {
261                 if (This->textures[counter]) {
262                     /* release our 'internal' hold on the texture */
263                     if(0 != IWineD3DBaseTexture_Release(This->textures[counter])) {
264                         TRACE("Texture still referenced by stateblock, applications has leaked Stage = %u Texture = %p\n", counter, This->textures[counter]);
265                     }
266                 }
267             }
268
269         }
270
271         for(counter = 0; counter < LIGHTMAP_SIZE; counter++) {
272             struct list *e1, *e2;
273             LIST_FOR_EACH_SAFE(e1, e2, &This->lightMap[counter]) {
274                 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
275                 list_remove(&light->entry);
276                 HeapFree(GetProcessHeap(), 0, light);
277             }
278         }
279
280         HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
281         HeapFree(GetProcessHeap(), 0, This->set.vertexShaderConstantsF);
282         HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
283         HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
284         HeapFree(GetProcessHeap(), 0, This->set.pixelShaderConstantsF);
285         HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
286
287         LIST_FOR_EACH_ENTRY_SAFE(constant, constant2, &This->set_vconstantsF, constants_entry, entry) {
288             HeapFree(GetProcessHeap(), 0, constant);
289         }
290
291         LIST_FOR_EACH_ENTRY_SAFE(constant, constant2, &This->set_pconstantsF, constants_entry, entry) {
292             HeapFree(GetProcessHeap(), 0, constant);
293         }
294
295         HeapFree(GetProcessHeap(), 0, This);
296     }
297     return refCount;
298 }
299
300 /**********************************************************
301  * IWineD3DStateBlockImpl parts follows
302  **********************************************************/
303 static HRESULT  WINAPI IWineD3DStateBlockImpl_GetParent(IWineD3DStateBlock *iface, IUnknown **pParent) {
304     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
305     IUnknown_AddRef(This->parent);
306     *pParent = This->parent;
307     return WINED3D_OK;
308 }
309
310 static HRESULT  WINAPI IWineD3DStateBlockImpl_GetDevice(IWineD3DStateBlock *iface, IWineD3DDevice** ppDevice){
311
312     IWineD3DStateBlockImpl *This   = (IWineD3DStateBlockImpl *)iface;
313
314     *ppDevice = (IWineD3DDevice*)This->wineD3DDevice;
315     IWineD3DDevice_AddRef(*ppDevice);
316     return WINED3D_OK;
317
318 }
319
320 static HRESULT  WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface){
321
322     IWineD3DStateBlockImpl *This             = (IWineD3DStateBlockImpl *)iface;
323     IWineD3DStateBlockImpl *targetStateBlock = This->wineD3DDevice->stateBlock;
324
325     TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock, This);
326
327     /* If not recorded, then update can just recapture */
328     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED  */ 0) {
329         IWineD3DStateBlockImpl* tmpBlock;
330         int i;
331
332         IWineD3DDevice_CreateStateBlock((IWineD3DDevice *)This->wineD3DDevice, This->blockType, (IWineD3DStateBlock**) &tmpBlock, NULL/*parent*/);
333
334         memcpy(This, tmpBlock, sizeof(IWineD3DStateBlockImpl));
335
336         /* Move the light elements from the tmpBlock to This. No need to duplicate them, but they have to be removed from tmpBlock
337          * and the pointers updated for the base element in This.
338          *
339          * No need to update This->activeLights because the lights objects are untouched(same address)
340          */
341         for(i = 0; i < LIGHTMAP_SIZE; i++) {
342             list_init(&This->lightMap[i]); /* This element contains rubish due to the memcpy */
343             list_move_tail(&This->lightMap[i], &tmpBlock->lightMap[i]); /* Cleans the list entry in tmpBlock */
344         }
345
346         IWineD3DStateBlock_Release((IWineD3DStateBlock *)tmpBlock);
347
348     } else {
349         unsigned int i, j;
350
351         /* Recorded => Only update 'changed' values */
352         if (This->vertexShader != targetStateBlock->vertexShader) {
353             TRACE("Updating vertex shader from %p to %p\n", This->vertexShader, targetStateBlock->vertexShader);
354
355             This->vertexShader = targetStateBlock->vertexShader;
356         }
357
358         /* Vertex Shader Float Constants */
359         for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
360             if (This->set.vertexShaderConstantsF[i]) {
361                 TRACE("Setting %p from %p %d to { %f, %f, %f, %f }\n", This, targetStateBlock, i,
362                     targetStateBlock->vertexShaderConstantF[i * 4],
363                     targetStateBlock->vertexShaderConstantF[i * 4 + 1],
364                     targetStateBlock->vertexShaderConstantF[i * 4 + 2],
365                     targetStateBlock->vertexShaderConstantF[i * 4 + 3]);
366
367                 This->vertexShaderConstantF[i * 4]      = targetStateBlock->vertexShaderConstantF[i * 4];
368                 This->vertexShaderConstantF[i * 4 + 1]  = targetStateBlock->vertexShaderConstantF[i * 4 + 1];
369                 This->vertexShaderConstantF[i * 4 + 2]  = targetStateBlock->vertexShaderConstantF[i * 4 + 2];
370                 This->vertexShaderConstantF[i * 4 + 3]  = targetStateBlock->vertexShaderConstantF[i * 4 + 3];
371             }
372         }
373         
374         /* Vertex Shader Integer Constants */
375         for (i = 0; i < MAX_CONST_I; ++i) {
376             if (This->set.vertexShaderConstantsI[i]) {
377                 TRACE("Setting %p from %p %d to { %d, %d, %d, %d }\n", This, targetStateBlock, i,
378                     targetStateBlock->vertexShaderConstantI[i * 4],
379                     targetStateBlock->vertexShaderConstantI[i * 4 + 1],
380                     targetStateBlock->vertexShaderConstantI[i * 4 + 2],
381                     targetStateBlock->vertexShaderConstantI[i * 4 + 3]);
382
383                 This->vertexShaderConstantI[i * 4]      = targetStateBlock->vertexShaderConstantI[i * 4];
384                 This->vertexShaderConstantI[i * 4 + 1]  = targetStateBlock->vertexShaderConstantI[i * 4 + 1];
385                 This->vertexShaderConstantI[i * 4 + 2]  = targetStateBlock->vertexShaderConstantI[i * 4 + 2];
386                 This->vertexShaderConstantI[i * 4 + 3]  = targetStateBlock->vertexShaderConstantI[i * 4 + 3];
387             }
388         }
389         
390         /* Vertex Shader Boolean Constants */
391         for (i = 0; i < MAX_CONST_B; ++i) {
392             if (This->set.vertexShaderConstantsB[i]) {
393                 TRACE("Setting %p from %p %d to %s\n", This, targetStateBlock, i,
394                     targetStateBlock->vertexShaderConstantB[i]? "TRUE":"FALSE");
395
396                 This->vertexShaderConstantB[i] =  targetStateBlock->vertexShaderConstantB[i];
397             }
398         }
399
400         /* Lights... For a recorded state block, we just had a chain of actions to perform,
401              so we need to walk that chain and update any actions which differ */
402         for(i = 0; i < LIGHTMAP_SIZE; i++) {
403             struct list *e, *f;
404             LIST_FOR_EACH(e, &This->lightMap[i]) {
405                 BOOL updated = FALSE;
406                 PLIGHTINFOEL *src = LIST_ENTRY(e, PLIGHTINFOEL, entry), *realLight;
407                 if(!src->changed || !src->enabledChanged) continue;
408
409                 /* Look up the light in the destination */
410                 LIST_FOR_EACH(f, &targetStateBlock->lightMap[i]) {
411                     realLight = LIST_ENTRY(f, PLIGHTINFOEL, entry);
412                     if(realLight->OriginalIndex == src->OriginalIndex) {
413                         if(src->changed) {
414                             memcpy(&src->OriginalParms, &realLight->OriginalParms, sizeof(src->OriginalParms));
415                         }
416                         if(src->enabledChanged) {
417                             /* Need to double check because enabledChanged does not catch enabled -> disabled -> enabled
418                              * or disabled -> enabled -> disabled changes
419                              */
420                             if(realLight->glIndex == -1 && src->glIndex != -1) {
421                                 /* Light disabled */
422                                 This->activeLights[src->glIndex] = NULL;
423                             } else if(realLight->glIndex != -1 && src->glIndex == -1){
424                                 /* Light enabled */
425                                 This->activeLights[realLight->glIndex] = src;
426                             }
427                             src->glIndex = realLight->glIndex;
428                         }
429                         updated = TRUE;
430                         break;
431                     }
432                 }
433
434                 if(updated) {
435                     /* Found a light, all done, proceed with next hash entry */
436                     continue;
437                 } else if(src->changed) {
438                     /* Otherwise assign defaul params */
439                     memcpy(&src->OriginalParms, &WINED3D_default_light, sizeof(src->OriginalParms));
440                 } else {
441                     /* Not enabled by default */
442                     src->glIndex = -1;
443                 }
444             }
445         }
446
447         /* Recorded => Only update 'changed' values */
448         if (This->pixelShader != targetStateBlock->pixelShader) {
449             TRACE("Updating pixel shader from %p to %p\n", This->pixelShader, targetStateBlock->pixelShader);
450
451             This->pixelShader = targetStateBlock->pixelShader;
452         }
453
454         /* Pixel Shader Float Constants */
455         for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
456             if (This->set.pixelShaderConstantsF[i]) {
457                 TRACE("Setting %p from %p %d to { %f, %f, %f, %f }\n", This, targetStateBlock, i,
458                     targetStateBlock->pixelShaderConstantF[i * 4],
459                     targetStateBlock->pixelShaderConstantF[i * 4 + 1],
460                     targetStateBlock->pixelShaderConstantF[i * 4 + 2],
461                     targetStateBlock->pixelShaderConstantF[i * 4 + 3]);
462
463                 This->pixelShaderConstantF[i * 4]      = targetStateBlock->pixelShaderConstantF[i * 4];
464                 This->pixelShaderConstantF[i * 4 + 1]  = targetStateBlock->pixelShaderConstantF[i * 4 + 1];
465                 This->pixelShaderConstantF[i * 4 + 2]  = targetStateBlock->pixelShaderConstantF[i * 4 + 2];
466                 This->pixelShaderConstantF[i * 4 + 3]  = targetStateBlock->pixelShaderConstantF[i * 4 + 3];
467             }
468         }
469         
470         /* Pixel Shader Integer Constants */
471         for (i = 0; i < MAX_CONST_I; ++i) {
472             if (This->set.pixelShaderConstantsI[i]) {
473                 TRACE("Setting %p from %p %d to { %d, %d, %d, %d }\n", This, targetStateBlock, i,
474                     targetStateBlock->pixelShaderConstantI[i * 4],
475                     targetStateBlock->pixelShaderConstantI[i * 4 + 1],
476                     targetStateBlock->pixelShaderConstantI[i * 4 + 2],
477                     targetStateBlock->pixelShaderConstantI[i * 4 + 3]);
478
479                 This->pixelShaderConstantI[i * 4]      = targetStateBlock->pixelShaderConstantI[i * 4];
480                 This->pixelShaderConstantI[i * 4 + 1]  = targetStateBlock->pixelShaderConstantI[i * 4 + 1];
481                 This->pixelShaderConstantI[i * 4 + 2]  = targetStateBlock->pixelShaderConstantI[i * 4 + 2];
482                 This->pixelShaderConstantI[i * 4 + 3]  = targetStateBlock->pixelShaderConstantI[i * 4 + 3];
483             }
484         }
485         
486         /* Pixel Shader Boolean Constants */
487         for (i = 0; i < MAX_CONST_B; ++i) {
488             if (This->set.pixelShaderConstantsB[i]) {
489                 TRACE("Setting %p from %p %d to %s\n", This, targetStateBlock, i,
490                     targetStateBlock->pixelShaderConstantB[i]? "TRUE":"FALSE");
491
492                 This->pixelShaderConstantB[i] =  targetStateBlock->pixelShaderConstantB[i];
493             }
494         }
495
496         /* Others + Render & Texture */
497         for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
498             if (This->set.transform[i] && memcmp(&targetStateBlock->transforms[i],
499                                     &This->transforms[i],
500                                     sizeof(WINED3DMATRIX)) != 0) {
501                 TRACE("Updating transform %d\n", i);
502                 memcpy(&This->transforms[i], &targetStateBlock->transforms[i], sizeof(WINED3DMATRIX));
503             }
504         }
505
506         if (This->set.indices && ((This->pIndexData != targetStateBlock->pIndexData)
507                         || (This->baseVertexIndex != targetStateBlock->baseVertexIndex))) {
508             TRACE("Updating pindexData to %p, baseVertexIndex to %d\n",
509             targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
510             This->pIndexData = targetStateBlock->pIndexData;
511             This->baseVertexIndex = targetStateBlock->baseVertexIndex;
512         }
513
514         if(This->set.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl){
515             TRACE("Updating vertex declaration from %p to %p\n", This->vertexDecl, targetStateBlock->vertexDecl);
516
517             This->vertexDecl = targetStateBlock->vertexDecl;
518         }
519
520         if(This->set.fvf && This->fvf != targetStateBlock->fvf){
521             This->fvf = targetStateBlock->fvf;
522         }
523
524         if (This->set.material && memcmp(&targetStateBlock->material,
525                                                     &This->material,
526                                                     sizeof(WINED3DMATERIAL)) != 0) {
527             TRACE("Updating material\n");
528             memcpy(&This->material, &targetStateBlock->material, sizeof(WINED3DMATERIAL));
529         }
530
531         if (This->set.viewport && memcmp(&targetStateBlock->viewport,
532                                                     &This->viewport,
533                                                     sizeof(WINED3DVIEWPORT)) != 0) {
534             TRACE("Updating viewport\n");
535             memcpy(&This->viewport, &targetStateBlock->viewport, sizeof(WINED3DVIEWPORT));
536         }
537
538         if(This->set.scissorRect && memcmp(&targetStateBlock->scissorRect,
539                                            &This->scissorRect,
540                                            sizeof(targetStateBlock->scissorRect)))
541         {
542             TRACE("Updating scissor rect\n");
543             memcpy(&targetStateBlock->scissorRect, &This->scissorRect, sizeof(targetStateBlock->scissorRect));
544         }
545
546         for (i = 0; i < MAX_STREAMS; i++) {
547             if (This->set.streamSource[i] &&
548                             ((This->streamStride[i] != targetStateBlock->streamStride[i]) ||
549                             (This->streamSource[i] != targetStateBlock->streamSource[i]))) {
550                 TRACE("Updating stream source %d to %p, stride to %d\n", i, targetStateBlock->streamSource[i],
551                                                                             targetStateBlock->streamStride[i]);
552                 This->streamStride[i] = targetStateBlock->streamStride[i];
553                 This->streamSource[i] = targetStateBlock->streamSource[i];
554             }
555
556             if (This->set.streamFreq[i] &&
557             (This->streamFreq[i] != targetStateBlock->streamFreq[i]
558             || This->streamFlags[i] != targetStateBlock->streamFlags[i])){
559                     TRACE("Updating stream frequency %d to %d flags to %d\n", i ,  targetStateBlock->streamFreq[i] ,
560                                                                                    targetStateBlock->streamFlags[i]);
561                     This->streamFreq[i]  =  targetStateBlock->streamFreq[i];
562                     This->streamFlags[i] =  targetStateBlock->streamFlags[i];
563             }
564         }
565
566         for (i = 0; i < GL_LIMITS(clipplanes); i++) {
567             if (This->set.clipplane[i] && memcmp(&targetStateBlock->clipplane[i],
568                                                         &This->clipplane[i],
569                                                         sizeof(This->clipplane)) != 0) {
570
571                 TRACE("Updating clipplane %d\n", i);
572                 memcpy(&This->clipplane[i], &targetStateBlock->clipplane[i],
573                                         sizeof(This->clipplane));
574             }
575         }
576
577         /* Render */
578         for (i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
579
580             if (This->set.renderState[i] && (This->renderState[i] != targetStateBlock->renderState[i])) {
581                 TRACE("Updating renderState %d to %d\n", i, targetStateBlock->renderState[i]);
582                 This->renderState[i] = targetStateBlock->renderState[i];
583             }
584         }
585
586         /* Texture states */
587         for (j = 0; j < MAX_TEXTURES; j++) {
588             /* TODO: move over to using memcpy */
589             for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE ; i++) {
590                 if (This->set.textureState[j][i]) {
591                     TRACE("Updating texturestagestate %d,%d to %d (was %d)\n", j,i, targetStateBlock->textureState[j][i],
592                     This->textureState[j][i]);
593                     This->textureState[j][i]         =  targetStateBlock->textureState[j][i];
594                 }
595             }
596         }
597
598         /* Samplers */
599         /* TODO: move over to using memcpy */
600         for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
601             if (This->set.textures[j]) {
602                 TRACE("Updating texture %d to %p (was %p)\n", j, targetStateBlock->textures[j],  This->textures[j]);
603                 This->textures[j] = targetStateBlock->textures[j];
604             }
605             for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE ; i++){ /* States are 1 based */
606                 if (This->set.samplerState[j][i]) {
607                     TRACE("Updating sampler state %d,%d to %d (was %d)\n",
608                     j, i, targetStateBlock->samplerState[j][i],
609                     This->samplerState[j][i]);
610                     This->samplerState[j][i]         = targetStateBlock->samplerState[j][i];
611                 }
612             }
613         }
614     }
615
616     TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
617
618     return WINED3D_OK;
619 }
620
621 static HRESULT  WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
622     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
623     IWineD3DDevice*        pDevice     = (IWineD3DDevice*)This->wineD3DDevice;
624
625 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
626 should really perform a delta so that only the changes get updated*/
627
628
629     UINT i;
630     UINT j;
631
632     TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
633
634     /* FIXME: Only apply applicable states not all states */
635
636     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */This->blockType == WINED3DSBT_INIT || This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_VERTEXSTATE) {
637
638         for(i = 0; i < LIGHTMAP_SIZE; i++) {
639             struct list *e;
640
641             LIST_FOR_EACH(e, &This->lightMap[i]) {
642                 PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
643
644                 if(light->changed) {
645                     IWineD3DDevice_SetLight(pDevice, light->OriginalIndex, &light->OriginalParms);
646                 }
647                 if(light->enabledChanged) {
648                     IWineD3DDevice_SetLightEnable(pDevice, light->OriginalIndex, light->glIndex != -1);
649                 }
650             }
651         }
652
653         /* Vertex Shader */
654         if (This->set.vertexShader && This->changed.vertexShader) {
655             IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
656         }
657
658         /* Vertex Shader Constants */
659         for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
660             if (This->set.vertexShaderConstantsF[i] && This->changed.vertexShaderConstantsF[i])
661                 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i, This->vertexShaderConstantF + i * 4, 1);
662         }
663         
664         for (i = 0; i < MAX_CONST_I; i++) {
665             if (This->set.vertexShaderConstantsI[i] && This->changed.vertexShaderConstantsI[i])
666                 IWineD3DDevice_SetVertexShaderConstantI(pDevice, i, This->vertexShaderConstantI + i * 4, 1);
667         }
668         
669         for (i = 0; i < MAX_CONST_B; i++) {
670             if (This->set.vertexShaderConstantsB[i] && This->changed.vertexShaderConstantsB[i])
671                 IWineD3DDevice_SetVertexShaderConstantB(pDevice, i, This->vertexShaderConstantB + i, 1);
672         }
673     }
674
675     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_PIXELSTATE) {
676
677         /* Pixel Shader */
678         if (This->set.pixelShader && This->changed.pixelShader) {
679             IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
680         }
681
682         /* Pixel Shader Constants */
683         for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
684             if (This->set.pixelShaderConstantsF[i] && This->changed.pixelShaderConstantsF[i])
685                 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i, This->pixelShaderConstantF + i * 4, 1);
686         }
687
688         for (i = 0; i < MAX_CONST_I; ++i) {
689             if (This->set.pixelShaderConstantsI[i] && This->changed.pixelShaderConstantsI[i])
690                 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i, This->pixelShaderConstantI + i * 4, 1);
691         }
692         
693         for (i = 0; i < MAX_CONST_B; ++i) {
694             if (This->set.pixelShaderConstantsB[i] && This->changed.pixelShaderConstantsB[i])
695                 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i, This->pixelShaderConstantB + i, 1);
696         }
697     }
698
699     if (This->set.fvf && This->changed.fvf) {
700         IWineD3DDevice_SetFVF(pDevice, This->fvf);
701     }
702
703     if (This->set.vertexDecl && This->changed.vertexDecl) {
704         IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
705     }
706
707     /* Others + Render & Texture */
708     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_INIT) {
709         for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
710             if (This->set.transform[i] && This->changed.transform[i])
711                 IWineD3DDevice_SetTransform(pDevice, i, &This->transforms[i]);
712         }
713
714         if (This->set.indices && This->changed.indices) {
715             IWineD3DDevice_SetIndices(pDevice, This->pIndexData);
716             IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
717         }
718
719         if (This->set.material && This->changed.material )
720             IWineD3DDevice_SetMaterial(pDevice, &This->material);
721
722         if (This->set.viewport && This->changed.viewport)
723             IWineD3DDevice_SetViewport(pDevice, &This->viewport);
724
725         if (This->set.scissorRect && This->changed.scissorRect)
726             IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
727
728         /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
729         for (i=0; i<MAX_STREAMS; i++) {
730             if (This->set.streamSource[i] && This->changed.streamSource[i])
731                 IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
732
733             if (This->set.streamFreq[i] && This->changed.streamFreq[i])
734                 IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
735         }
736
737         for (i = 0; i < GL_LIMITS(clipplanes); i++) {
738             if (This->set.clipplane[i] && This->changed.clipplane[i]) {
739                 float clip[4];
740
741                 clip[0] = This->clipplane[i][0];
742                 clip[1] = This->clipplane[i][1];
743                 clip[2] = This->clipplane[i][2];
744                 clip[3] = This->clipplane[i][3];
745                 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
746             }
747         }
748
749         /* Render */
750         for (i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
751             if (This->set.renderState[i] && This->changed.renderState[i])
752                 IWineD3DDevice_SetRenderState(pDevice, i, This->renderState[i]);
753         }
754
755         /* Texture states */
756         for (j = 0; j < MAX_TEXTURES; j++) { /* Set The texture first, just in case it resets the states? */
757             /* TODO: move over to memcpy */
758             for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE; i++) {
759                 if (This->set.textureState[j][i] && This->changed.textureState[j][i]) { /* tb_dx9_10 failes without this test */
760                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][i]         = This->textureState[j][i];
761                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->set.textureState[j][i]     = TRUE;
762                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[j][i] = TRUE;
763                     /* TODO: Record a display list to apply all gl states. For now apply by brute force */
764                     IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(j, i));
765                 }
766             }
767         }
768
769         /* Samplers */
770         /* TODO: move over to memcpy */
771         for (j = 0 ; j < MAX_COMBINED_SAMPLERS; j++){
772             if (This->set.textures[j] && This->changed.textures[j]) {
773                 if (j < MAX_FRAGMENT_SAMPLERS) {
774                     IWineD3DDevice_SetTexture(pDevice, j, This->textures[j]);
775                 } else {
776                     IWineD3DDevice_SetTexture(pDevice, WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS, This->textures[j]);
777                 }
778             }
779             for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; i++){
780                 if (This->set.samplerState[j][i] && This->changed.samplerState[j][i]) {
781                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][i]         = This->samplerState[j][i];
782                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->set.samplerState[j][i]     = TRUE;
783                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[j][i] = TRUE;
784                 }
785             }
786             /* SetTexture catches nop changes, so the above call does not assure that the sampler is updated */
787             IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_SAMPLER(j));
788         }
789
790     } else if (This->blockType == WINED3DSBT_PIXELSTATE) {
791
792         for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
793             if (This->set.renderState[SavedPixelStates_R[i]] && This->changed.renderState[SavedPixelStates_R[i]])
794                 IWineD3DDevice_SetRenderState(pDevice, SavedPixelStates_R[i], This->renderState[SavedPixelStates_R[i]]);
795
796         }
797
798         for (j = 0; j < MAX_TEXTURES; j++) {
799             for (i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
800                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedPixelStates_T[i]] = This->textureState[j][SavedPixelStates_T[i]];
801                 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(j, SavedPixelStates_T[i]));
802             }
803         }
804
805         for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
806             for (i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
807                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedPixelStates_S[i]] = This->samplerState[j][SavedPixelStates_S[i]];
808             }
809         }
810
811     } else if (This->blockType == WINED3DSBT_VERTEXSTATE) {
812
813         for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
814             if ( This->set.renderState[SavedVertexStates_R[i]] && This->changed.renderState[SavedVertexStates_R[i]])
815                 IWineD3DDevice_SetRenderState(pDevice, SavedVertexStates_R[i], This->renderState[SavedVertexStates_R[i]]);
816         }
817
818         for (j = 0; j < MAX_TEXTURES; j++) {
819             for (i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
820                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedVertexStates_T[i]] = This->textureState[j][SavedVertexStates_T[i]];
821                 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(j, SavedVertexStates_T[i]));
822             }
823         }
824
825         for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
826             for (i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
827                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedVertexStates_S[i]] = This->samplerState[j][SavedVertexStates_S[i]];
828             }
829         }
830
831
832     } else {
833         FIXME("Unrecognized state block type %d\n", This->blockType);
834     }
835     stateblock_savedstates_copy(iface, &((IWineD3DDeviceImpl*)pDevice)->stateBlock->changed, &This->changed);
836     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = MAX_TEXTURES - 1;
837     for(j = 0; j < MAX_TEXTURES - 1; j++) {
838         if(((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE) {
839             ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = j;
840             break;
841         }
842     }
843     TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
844
845     return WINED3D_OK;
846 }
847
848 static HRESULT  WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
849     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
850     IWineD3DDevice         *device = (IWineD3DDevice *)This->wineD3DDevice;
851     IWineD3DDeviceImpl     *ThisDevice = (IWineD3DDeviceImpl *)device;
852     union {
853         WINED3DLINEPATTERN lp;
854         DWORD d;
855     } lp;
856     union {
857         float f;
858         DWORD d;
859     } tmpfloat;
860     unsigned int i;
861
862     /* Note this may have a large overhead but it should only be executed
863        once, in order to initialize the complete state of the device and
864        all opengl equivalents                                            */
865     TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
866     /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
867     This->blockType = WINED3DSBT_INIT;
868
869     /* Set some of the defaults for lights, transforms etc */
870     memcpy(&This->transforms[WINED3DTS_PROJECTION], &identity, sizeof(identity));
871     memcpy(&This->transforms[WINED3DTS_VIEW], &identity, sizeof(identity));
872     for (i = 0; i < 256; ++i) {
873       memcpy(&This->transforms[WINED3DTS_WORLDMATRIX(i)], &identity, sizeof(identity));
874     }
875
876     TRACE("Render states\n");
877     /* Render states: */
878     if (ThisDevice->depthStencilBuffer != NULL) {
879        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       WINED3DZB_TRUE);
880     } else {
881        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       WINED3DZB_FALSE);
882     }
883     IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE,         WINED3DFILL_SOLID);
884     IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE,        WINED3DSHADE_GOURAUD);
885     lp.lp.wRepeatFactor = 0;
886     lp.lp.wLinePattern  = 0;
887     IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN,      lp.d);
888     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE,     TRUE);
889     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE,  FALSE);
890     IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL,        TRUE);
891     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND,         WINED3DBLEND_ONE);
892     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND,        WINED3DBLEND_ZERO);
893     IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE,         WINED3DCULL_CCW);
894     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC,            WINED3DCMP_LESSEQUAL);
895     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC,        WINED3DCMP_ALWAYS);
896     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF,         0);
897     IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE,     FALSE);
898     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
899     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE,        FALSE);
900     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE,   FALSE);
901     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE,         0);
902     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR,         0);
903     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE,     WINED3DFOG_NONE);
904     tmpfloat.f = 0.0f;
905     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART,         tmpfloat.d);
906     tmpfloat.f = 1.0f;
907     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND,           tmpfloat.d);
908     tmpfloat.f = 1.0f;
909     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY,       tmpfloat.d);
910     IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS,    FALSE);
911     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS,            0);
912     IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE,   FALSE);
913     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE,    FALSE);
914     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL,      WINED3DSTENCILOP_KEEP);
915     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL,     WINED3DSTENCILOP_KEEP);
916     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS,      WINED3DSTENCILOP_KEEP);
917
918     /* Setting stencil func also uses values for stencil ref/mask, so manually set defaults
919      * so only a single call performed (and ensure defaults initialized before making that call)
920      *
921      * IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF, 0);
922      * IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK, 0xFFFFFFFF);
923      */
924     This->renderState[WINED3DRS_STENCILREF] = 0;
925     This->renderState[WINED3DRS_STENCILMASK] = 0xFFFFFFFF;
926     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC,      WINED3DCMP_ALWAYS);
927     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
928     IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR,    0xFFFFFFFF);
929     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
930     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
931     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
932     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
933     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
934     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
935     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
936     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
937     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING,                 TRUE);
938     IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING,                 TRUE);
939     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT,                  0);
940     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE,            WINED3DFOG_NONE);
941     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX,              TRUE);
942     IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER,              TRUE);
943     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS,         FALSE);
944     IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE,    WINED3DMCS_COLOR1);
945     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE,   WINED3DMCS_COLOR2);
946     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE,    WINED3DMCS_MATERIAL);
947     IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE,   WINED3DMCS_MATERIAL);
948     IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND,              WINED3DVBF_DISABLE);
949     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE,          0);
950     IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
951     tmpfloat.f = 1.0f;
952     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE,                tmpfloat.d);
953     tmpfloat.f = 1.0f;
954     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN,            tmpfloat.d);
955     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE,        FALSE);
956     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE,         FALSE);
957     tmpfloat.f = 1.0f;
958     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A,             tmpfloat.d);
959     tmpfloat.f = 0.0f;
960     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B,             tmpfloat.d);
961     tmpfloat.f = 0.0f;
962     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C,             tmpfloat.d);
963     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS,     TRUE);
964     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK,          0xFFFFFFFF);
965     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE,           WINED3DPATCHEDGE_DISCRETE);
966     tmpfloat.f = 1.0f;
967     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS,            tmpfloat.d);
968     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN,        0xbaadcafe);
969     tmpfloat.f = 64.0f;
970     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX,            tmpfloat.d);
971     IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
972     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE,         0x0000000F);
973     tmpfloat.f = 0.0f;
974     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR,              tmpfloat.d);
975     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP,                  WINED3DBLENDOP_ADD);
976     IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE,           WINED3DDEGREE_CUBIC);
977     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE,             WINED3DDEGREE_LINEAR);
978     /* states new in d3d9 */
979     IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE,        FALSE);
980     IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS,      0);
981     tmpfloat.f = 1.0f;
982     IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL,     tmpfloat.d);
983     IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL,     tmpfloat.d);
984     IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE,    FALSE);
985     tmpfloat.f = 0.0f;
986     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X,           tmpfloat.d);
987     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y,           tmpfloat.d);
988     tmpfloat.f = 1.0f;
989     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z,           tmpfloat.d);
990     tmpfloat.f = 0.0f;
991     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W,           tmpfloat.d);
992     IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
993     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE,      FALSE);
994     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL,          WINED3DSTENCILOP_KEEP);
995     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL,         WINED3DSTENCILOP_KEEP);
996     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS,          WINED3DSTENCILOP_KEEP);
997     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC,          WINED3DCMP_ALWAYS);
998     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1,        0x0000000F);
999     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2,        0x0000000F);
1000     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3,        0x0000000F);
1001     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR,              0xFFFFFFFF);
1002     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE,          0);
1003     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS,                0);
1004     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8,  0);
1005     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9,  0);
1006     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
1007     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
1008     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
1009     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
1010     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
1011     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
1012     IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1013     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA,            WINED3DBLEND_ONE);
1014     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA,           WINED3DBLEND_ZERO);
1015     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA,             WINED3DBLENDOP_ADD);
1016
1017     /* clipping status */
1018     This->clip_status.ClipUnion = 0;
1019     This->clip_status.ClipIntersection = 0xFFFFFFFF;
1020
1021     /* Texture Stage States - Put directly into state block, we will call function below */
1022     for (i = 0; i < MAX_TEXTURES; i++) {
1023         TRACE("Setting up default texture states for texture Stage %d\n", i);
1024         memcpy(&This->transforms[WINED3DTS_TEXTURE0 + i], &identity, sizeof(identity));
1025         This->textureState[i][WINED3DTSS_COLOROP               ] = (i==0)? WINED3DTOP_MODULATE :  WINED3DTOP_DISABLE;
1026         This->textureState[i][WINED3DTSS_COLORARG1             ] = WINED3DTA_TEXTURE;
1027         This->textureState[i][WINED3DTSS_COLORARG2             ] = WINED3DTA_CURRENT;
1028         This->textureState[i][WINED3DTSS_ALPHAOP               ] = (i==0)? WINED3DTOP_SELECTARG1 :  WINED3DTOP_DISABLE;
1029         This->textureState[i][WINED3DTSS_ALPHAARG1             ] = WINED3DTA_TEXTURE;
1030         This->textureState[i][WINED3DTSS_ALPHAARG2             ] = WINED3DTA_CURRENT;
1031         This->textureState[i][WINED3DTSS_BUMPENVMAT00          ] = (DWORD) 0.0;
1032         This->textureState[i][WINED3DTSS_BUMPENVMAT01          ] = (DWORD) 0.0;
1033         This->textureState[i][WINED3DTSS_BUMPENVMAT10          ] = (DWORD) 0.0;
1034         This->textureState[i][WINED3DTSS_BUMPENVMAT11          ] = (DWORD) 0.0;
1035         This->textureState[i][WINED3DTSS_TEXCOORDINDEX         ] = i;
1036         This->textureState[i][WINED3DTSS_BUMPENVLSCALE         ] = (DWORD) 0.0;
1037         This->textureState[i][WINED3DTSS_BUMPENVLOFFSET        ] = (DWORD) 0.0;
1038         This->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS ] = WINED3DTTFF_DISABLE;
1039         This->textureState[i][WINED3DTSS_ADDRESSW              ] = WINED3DTADDRESS_WRAP;
1040         This->textureState[i][WINED3DTSS_COLORARG0             ] = WINED3DTA_CURRENT;
1041         This->textureState[i][WINED3DTSS_ALPHAARG0             ] = WINED3DTA_CURRENT;
1042         This->textureState[i][WINED3DTSS_RESULTARG             ] = WINED3DTA_CURRENT;
1043     }
1044     This->lowest_disabled_stage = 1;
1045
1046         /* Sampler states*/
1047     for (i = 0 ; i <  MAX_COMBINED_SAMPLERS; i++) {
1048         TRACE("Setting up default samplers states for sampler %d\n", i);
1049         This->samplerState[i][WINED3DSAMP_ADDRESSU         ] = WINED3DTADDRESS_WRAP;
1050         This->samplerState[i][WINED3DSAMP_ADDRESSV         ] = WINED3DTADDRESS_WRAP;
1051         This->samplerState[i][WINED3DSAMP_ADDRESSW         ] = WINED3DTADDRESS_WRAP;
1052         This->samplerState[i][WINED3DSAMP_BORDERCOLOR      ] = 0x00;
1053         This->samplerState[i][WINED3DSAMP_MAGFILTER        ] = WINED3DTEXF_POINT;
1054         This->samplerState[i][WINED3DSAMP_MINFILTER        ] = WINED3DTEXF_POINT;
1055         This->samplerState[i][WINED3DSAMP_MIPFILTER        ] = WINED3DTEXF_NONE;
1056         This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS    ] = 0;
1057         This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL      ] = 0;
1058         This->samplerState[i][WINED3DSAMP_MAXANISOTROPY    ] = 1;
1059         This->samplerState[i][WINED3DSAMP_SRGBTEXTURE      ] = 0;
1060         This->samplerState[i][WINED3DSAMP_ELEMENTINDEX     ] = 0; /* TODO: Indicates which element of a  multielement texture to use */
1061         This->samplerState[i][WINED3DSAMP_DMAPOFFSET       ] = 0; /* TODO: Vertex offset in the presampled displacement map */
1062     }
1063
1064     /* Under DirectX you can have texture stage operations even if no texture is
1065        bound, whereas opengl will only do texture operations when a valid texture is
1066        bound. We emulate this by creating dummy textures and binding them to each
1067        texture stage, but disable all stages by default. Hence if a stage is enabled
1068        then the default texture will kick in until replaced by a SetTexture call     */
1069     ENTER_GL();
1070
1071     if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
1072         /* The dummy texture does not have client storage backing */
1073         glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
1074         checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1075     }
1076     for (i = 0; i < GL_LIMITS(textures); i++) {
1077         GLubyte white = 255;
1078
1079         /* Note this avoids calling settexture, so pretend it has been called */
1080         This->set.textures[i]     = TRUE;
1081         This->changed.textures[i] = TRUE;
1082         This->textures[i]         = NULL;
1083
1084         /* Make appropriate texture active */
1085         if (GL_SUPPORT(ARB_MULTITEXTURE)) {
1086             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1087             checkGLcall("glActiveTextureARB");
1088         } else if (i > 0) {
1089             FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
1090         }
1091
1092         /* Generate an opengl texture name */
1093         glGenTextures(1, &ThisDevice->dummyTextureName[i]);
1094         checkGLcall("glGenTextures");
1095         TRACE("Dummy Texture %d given name %d\n", i, ThisDevice->dummyTextureName[i]);
1096
1097         /* Generate a dummy 2d texture (not using 1d because they cause many
1098          * DRI drivers fall back to sw) */
1099         This->textureDimensions[i] = GL_TEXTURE_2D;
1100         glBindTexture(GL_TEXTURE_2D, ThisDevice->dummyTextureName[i]);
1101         checkGLcall("glBindTexture");
1102
1103         glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 1, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &white);
1104         checkGLcall("glTexImage2D");
1105     }
1106     if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
1107         /* Reenable because if supported it is enabled by default */
1108         glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1109         checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1110     }
1111
1112     LEAVE_GL();
1113
1114
1115     /* Defaulting palettes - Note these are device wide but reinitialized here for convenience*/
1116     for (i = 0; i < MAX_PALETTES; ++i) {
1117       int j;
1118       for (j = 0; j < 256; ++j) {
1119         This->wineD3DDevice->palettes[i][j].peRed   = 0xFF;
1120         This->wineD3DDevice->palettes[i][j].peGreen = 0xFF;
1121         This->wineD3DDevice->palettes[i][j].peBlue  = 0xFF;
1122         This->wineD3DDevice->palettes[i][j].peFlags = 0xFF;
1123       }
1124     }
1125     This->wineD3DDevice->currentPalette = 0;
1126
1127     /* Set default GLSL program to NULL.  We won't actually create one
1128      * until the app sets a vertex or pixel shader */
1129     This->glsl_program = NULL;
1130
1131     TRACE("-----------------------> Device defaults now set up...\n");
1132     return WINED3D_OK;
1133 }
1134
1135 /**********************************************************
1136  * IWineD3DStateBlock VTbl follows
1137  **********************************************************/
1138
1139 const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
1140 {
1141     /* IUnknown */
1142     IWineD3DStateBlockImpl_QueryInterface,
1143     IWineD3DStateBlockImpl_AddRef,
1144     IWineD3DStateBlockImpl_Release,
1145     /* IWineD3DStateBlock */
1146     IWineD3DStateBlockImpl_GetParent,
1147     IWineD3DStateBlockImpl_GetDevice,
1148     IWineD3DStateBlockImpl_Capture,
1149     IWineD3DStateBlockImpl_Apply,
1150     IWineD3DStateBlockImpl_InitStartupStateBlock
1151 };