d3d: Fix adapter mode enumeration and filtering.
[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 ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->wineD3DDevice)->wineD3D))->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
82     /* Fixed size arrays */
83     memcpy(dest->streamSource, source->streamSource, bsize * MAX_STREAMS);
84     memcpy(dest->streamFreq, source->streamFreq, bsize * MAX_STREAMS);
85     memcpy(dest->textures, source->textures, bsize * MAX_SAMPLERS);
86     memcpy(dest->transform, source->transform, bsize * (HIGHEST_TRANSFORMSTATE + 1));
87     memcpy(dest->renderState, source->renderState, bsize * (WINEHIGHEST_RENDER_STATE + 1));
88     memcpy(dest->textureState, source->textureState, bsize * MAX_TEXTURES * (WINED3D_HIGHEST_TEXTURE_STATE + 1));
89     memcpy(dest->samplerState, source->samplerState, bsize * MAX_SAMPLERS * (WINED3D_HIGHEST_SAMPLER_STATE + 1));
90     memcpy(dest->clipplane, source->clipplane, bsize * MAX_CLIPPLANES);
91     memcpy(dest->pixelShaderConstantsB, source->pixelShaderConstantsB, bsize * MAX_CONST_B);
92     memcpy(dest->pixelShaderConstantsI, source->pixelShaderConstantsI, bsize * MAX_CONST_I);
93     memcpy(dest->vertexShaderConstantsB, source->vertexShaderConstantsB, bsize * MAX_CONST_B);
94     memcpy(dest->vertexShaderConstantsI, source->vertexShaderConstantsI, bsize * MAX_CONST_I);
95
96     /* Dynamically sized arrays */
97     memcpy(dest->pixelShaderConstantsF, source->pixelShaderConstantsF, bsize * GL_LIMITS(pshader_constantsF));
98     memcpy(dest->vertexShaderConstantsF, source->vertexShaderConstantsF, bsize * GL_LIMITS(vshader_constantsF));
99 }
100
101 /** Set all members of a stateblock savedstate to the given value */
102 void stateblock_savedstates_set(
103     IWineD3DStateBlock* iface,
104     SAVEDSTATES* states,
105     BOOL value) {
106     
107     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
108     unsigned bsize = sizeof(BOOL);
109
110     /* Single values */
111     states->indices = value;
112     states->material = value;
113     states->fvf = value;
114     states->viewport = value;
115     states->vertexDecl = value;
116     states->pixelShader = value;
117     states->vertexShader = value;
118
119     /* Fixed size arrays */
120     memset(states->streamSource, value, bsize * MAX_STREAMS);
121     memset(states->streamFreq, value, bsize * MAX_STREAMS);
122     memset(states->textures, value, bsize * MAX_SAMPLERS);
123     memset(states->transform, value, bsize * (HIGHEST_TRANSFORMSTATE + 1));
124     memset(states->renderState, value, bsize * (WINEHIGHEST_RENDER_STATE + 1));
125     memset(states->textureState, value, bsize * MAX_TEXTURES * (WINED3D_HIGHEST_TEXTURE_STATE + 1));
126     memset(states->samplerState, value, bsize * MAX_SAMPLERS * (WINED3D_HIGHEST_SAMPLER_STATE + 1));
127     memset(states->clipplane, value, bsize * MAX_CLIPPLANES);
128     memset(states->pixelShaderConstantsB, value, bsize * MAX_CONST_B);
129     memset(states->pixelShaderConstantsI, value, bsize * MAX_CONST_I);
130     memset(states->vertexShaderConstantsB, value, bsize * MAX_CONST_B);
131     memset(states->vertexShaderConstantsI, value, bsize * MAX_CONST_I);
132
133     /* Dynamically sized arrays */
134     memset(states->pixelShaderConstantsF, value, bsize * GL_LIMITS(pshader_constantsF));
135     memset(states->vertexShaderConstantsF, value, bsize * GL_LIMITS(vshader_constantsF));
136 }
137
138 void stateblock_copy(
139     IWineD3DStateBlock* destination,
140     IWineD3DStateBlock* source) {
141
142     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)source;
143     IWineD3DStateBlockImpl *Dest = (IWineD3DStateBlockImpl *)destination;
144
145     /* IUnknown fields */
146     Dest->lpVtbl                = This->lpVtbl;
147     Dest->ref                   = This->ref;
148
149     /* IWineD3DStateBlock information */
150     Dest->parent                = This->parent;
151     Dest->wineD3DDevice         = This->wineD3DDevice;
152     Dest->blockType             = This->blockType;
153
154     /* Saved states */
155     stateblock_savedstates_copy(source, &Dest->set, &This->set);
156     stateblock_savedstates_copy(source, &Dest->changed, &This->changed);
157
158     /* Single items */
159     Dest->fvf = This->fvf;
160     Dest->vertexDecl = This->vertexDecl;
161     Dest->vertexShader = This->vertexShader;
162     Dest->streamIsUP = This->streamIsUP;
163     Dest->pIndexData = This->pIndexData;
164     Dest->baseVertexIndex = This->baseVertexIndex;
165     Dest->lights = This->lights;
166     Dest->clip_status = This->clip_status;
167     Dest->viewport = This->viewport;
168     Dest->material = This->material;
169     Dest->pixelShader = This->pixelShader;
170     Dest->glsl_program = This->glsl_program;
171
172     /* Fixed size arrays */
173     memcpy(Dest->vertexShaderConstantB, This->vertexShaderConstantB, sizeof(BOOL) * MAX_CONST_B);
174     memcpy(Dest->vertexShaderConstantI, This->vertexShaderConstantI, sizeof(INT) * MAX_CONST_I * 4);
175     memcpy(Dest->pixelShaderConstantB, This->pixelShaderConstantB, sizeof(BOOL) * MAX_CONST_B);
176     memcpy(Dest->pixelShaderConstantI, This->pixelShaderConstantI, sizeof(INT) * MAX_CONST_I * 4);
177     
178     memcpy(Dest->streamStride, This->streamStride, sizeof(UINT) * MAX_STREAMS);
179     memcpy(Dest->streamOffset, This->streamOffset, sizeof(UINT) * MAX_STREAMS);
180     memcpy(Dest->streamSource, This->streamSource, sizeof(IWineD3DVertexBuffer*) * MAX_STREAMS);
181     memcpy(Dest->streamFreq,   This->streamFreq,   sizeof(UINT) * MAX_STREAMS);
182     memcpy(Dest->streamFlags,  This->streamFlags,  sizeof(UINT) * MAX_STREAMS);
183     memcpy(Dest->transforms,   This->transforms,   sizeof(WINED3DMATRIX) * (HIGHEST_TRANSFORMSTATE + 1));
184     memcpy(Dest->clipplane,    This->clipplane,    sizeof(double) * MAX_CLIPPLANES * 4);
185     memcpy(Dest->renderState,  This->renderState,  sizeof(DWORD) * (WINEHIGHEST_RENDER_STATE + 1));
186     memcpy(Dest->textures,     This->textures,     sizeof(IWineD3DBaseTexture*) * MAX_SAMPLERS);
187     memcpy(Dest->textureDimensions, This->textureDimensions, sizeof(int) * MAX_SAMPLERS);
188     memcpy(Dest->textureState, This->textureState, sizeof(DWORD) * MAX_TEXTURES * (WINED3D_HIGHEST_TEXTURE_STATE + 1));
189     memcpy(Dest->samplerState, This->samplerState, sizeof(DWORD) * MAX_SAMPLERS * (WINED3D_HIGHEST_SAMPLER_STATE + 1));
190
191     /* Dynamically sized arrays */
192     memcpy(Dest->vertexShaderConstantF, This->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
193     memcpy(Dest->pixelShaderConstantF,  This->pixelShaderConstantF,  sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
194 }
195
196 /**********************************************************
197  * IWineD3DStateBlockImpl IUnknown parts follows
198  **********************************************************/
199 static HRESULT  WINAPI IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock *iface,REFIID riid,LPVOID *ppobj)
200 {
201     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
202     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
203     if (IsEqualGUID(riid, &IID_IUnknown)
204         || IsEqualGUID(riid, &IID_IWineD3DBase)
205         || IsEqualGUID(riid, &IID_IWineD3DStateBlock)){
206         IUnknown_AddRef(iface);
207         *ppobj = This;
208         return S_OK;
209     }
210     *ppobj = NULL;
211     return E_NOINTERFACE;
212 }
213
214 static ULONG  WINAPI IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock *iface) {
215     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
216     ULONG refCount = InterlockedIncrement(&This->ref);
217
218     TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
219     return refCount;
220 }
221
222 static ULONG  WINAPI IWineD3DStateBlockImpl_Release(IWineD3DStateBlock *iface) {
223     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
224     ULONG refCount = InterlockedDecrement(&This->ref);
225
226     TRACE("(%p) : Releasing from %d\n", This, refCount + 1);
227
228     if (!refCount) {
229         constant_entry *constant, *constant2;
230
231         /* type 0 represents the primary stateblock, so free all the resources */
232         if (This->blockType == WINED3DSBT_INIT) {
233             int counter;
234             FIXME("Releasing primary stateblock\n");
235             /* Free any streams still bound */
236             for (counter = 0 ; counter < MAX_STREAMS ; counter++) {
237                 if (This->streamSource[counter] != NULL) {
238                     IWineD3DVertexBuffer_Release(This->streamSource[counter]);
239                     This->streamSource[counter] = NULL;
240                 }
241             }
242
243             /* free any index data */
244             if (This->pIndexData) {
245                 IWineD3DIndexBuffer_Release(This->pIndexData);
246                 This->pIndexData = NULL;
247             }
248
249             if (NULL != This->pixelShader) {
250                 IWineD3DPixelShader_Release(This->pixelShader);
251             }
252
253             if (NULL != This->vertexShader) {
254                 IWineD3DVertexShader_Release(This->vertexShader);
255             }
256
257             if (NULL != This->vertexDecl) {
258                 IWineD3DVertexDeclaration_Release(This->vertexDecl);
259             }
260
261             /* NOTE: according to MSDN: The application is responsible for making sure the texture references are cleared down */
262             for (counter = 0; counter < GL_LIMITS(sampler_stages); counter++) {
263                 if (This->textures[counter]) {
264                     /* release our 'internal' hold on the texture */
265                     if(0 != IWineD3DBaseTexture_Release(This->textures[counter])) {
266                         TRACE("Texture still referenced by stateblock, applications has leaked Stage = %u Texture = %p\n", counter, This->textures[counter]);
267                     }
268                 }
269             }
270
271         }
272
273         HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
274         HeapFree(GetProcessHeap(), 0, This->set.vertexShaderConstantsF);
275         HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
276         HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
277         HeapFree(GetProcessHeap(), 0, This->set.pixelShaderConstantsF);
278         HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
279
280         LIST_FOR_EACH_ENTRY_SAFE(constant, constant2, &This->set_vconstantsF, constant_entry, entry) {
281             HeapFree(GetProcessHeap(), 0, constant);
282         }
283
284         LIST_FOR_EACH_ENTRY_SAFE(constant, constant2, &This->set_pconstantsF, constant_entry, entry) {
285             HeapFree(GetProcessHeap(), 0, constant);
286         }
287
288         HeapFree(GetProcessHeap(), 0, This);
289     }
290     return refCount;
291 }
292
293 /**********************************************************
294  * IWineD3DStateBlockImpl parts follows
295  **********************************************************/
296 static HRESULT  WINAPI IWineD3DStateBlockImpl_GetParent(IWineD3DStateBlock *iface, IUnknown **pParent) {
297     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
298     IUnknown_AddRef(This->parent);
299     *pParent = This->parent;
300     return WINED3D_OK;
301 }
302
303 static HRESULT  WINAPI IWineD3DStateBlockImpl_GetDevice(IWineD3DStateBlock *iface, IWineD3DDevice** ppDevice){
304
305     IWineD3DStateBlockImpl *This   = (IWineD3DStateBlockImpl *)iface;
306
307     *ppDevice = (IWineD3DDevice*)This->wineD3DDevice;
308     IWineD3DDevice_AddRef(*ppDevice);
309     return WINED3D_OK;
310
311 }
312
313 static HRESULT  WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface){
314
315     IWineD3DStateBlockImpl *This             = (IWineD3DStateBlockImpl *)iface;
316     IWineD3DStateBlockImpl *targetStateBlock = This->wineD3DDevice->stateBlock;
317
318     TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock, This);
319
320     /* If not recorded, then update can just recapture */
321     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED  */ 0) {
322         IWineD3DStateBlockImpl* tmpBlock;
323         PLIGHTINFOEL *tmp = This->lights;
324
325         IWineD3DDevice_CreateStateBlock((IWineD3DDevice *)This->wineD3DDevice, This->blockType, (IWineD3DStateBlock**) &tmpBlock, NULL/*parent*/);
326
327         /* Note just swap the light chains over so when deleting, the old one goes */
328         memcpy(This, tmpBlock, sizeof(IWineD3DStateBlockImpl));
329         tmpBlock->lights = tmp;
330
331         /* Delete the temporary one (which points to the old light chain though */
332         IWineD3DStateBlock_Release((IWineD3DStateBlock *)tmpBlock);
333         /*IDirect3DDevice_DeleteStateBlock(pDevice, tmpBlock);*/
334
335     } else {
336         unsigned int i, j;
337
338         PLIGHTINFOEL *src;
339
340         /* Recorded => Only update 'changed' values */
341         if (This->vertexShader != targetStateBlock->vertexShader) {
342             TRACE("Updating vertex shader from %p to %p\n", This->vertexShader, targetStateBlock->vertexShader);
343
344             if (targetStateBlock->vertexShader) {
345                 IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
346             }
347             if (This->vertexShader) {
348                 IWineD3DVertexShader_Release(This->vertexShader);
349             }
350
351             This->vertexShader = targetStateBlock->vertexShader;
352         }
353
354         /* Vertex Shader Float Constants */
355         for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
356             if (This->set.vertexShaderConstantsF[i]) {
357                 TRACE("Setting %p from %p %d to { %f, %f, %f, %f }\n", This, targetStateBlock, i,
358                     targetStateBlock->vertexShaderConstantF[i * 4],
359                     targetStateBlock->vertexShaderConstantF[i * 4 + 1],
360                     targetStateBlock->vertexShaderConstantF[i * 4 + 2],
361                     targetStateBlock->vertexShaderConstantF[i * 4 + 3]);
362
363                 This->vertexShaderConstantF[i * 4]      = targetStateBlock->vertexShaderConstantF[i * 4];
364                 This->vertexShaderConstantF[i * 4 + 1]  = targetStateBlock->vertexShaderConstantF[i * 4 + 1];
365                 This->vertexShaderConstantF[i * 4 + 2]  = targetStateBlock->vertexShaderConstantF[i * 4 + 2];
366                 This->vertexShaderConstantF[i * 4 + 3]  = targetStateBlock->vertexShaderConstantF[i * 4 + 3];
367             }
368         }
369         
370         /* Vertex Shader Integer Constants */
371         for (i = 0; i < MAX_CONST_I; ++i) {
372             if (This->set.vertexShaderConstantsI[i]) {
373                 TRACE("Setting %p from %p %d to { %d, %d, %d, %d }\n", This, targetStateBlock, i,
374                     targetStateBlock->vertexShaderConstantI[i * 4],
375                     targetStateBlock->vertexShaderConstantI[i * 4 + 1],
376                     targetStateBlock->vertexShaderConstantI[i * 4 + 2],
377                     targetStateBlock->vertexShaderConstantI[i * 4 + 3]);
378
379                 This->vertexShaderConstantI[i * 4]      = targetStateBlock->vertexShaderConstantI[i * 4];
380                 This->vertexShaderConstantI[i * 4 + 1]  = targetStateBlock->vertexShaderConstantI[i * 4 + 1];
381                 This->vertexShaderConstantI[i * 4 + 2]  = targetStateBlock->vertexShaderConstantI[i * 4 + 2];
382                 This->vertexShaderConstantI[i * 4 + 3]  = targetStateBlock->vertexShaderConstantI[i * 4 + 3];
383             }
384         }
385         
386         /* Vertex Shader Boolean Constants */
387         for (i = 0; i < MAX_CONST_B; ++i) {
388             if (This->set.vertexShaderConstantsB[i]) {
389                 TRACE("Setting %p from %p %d to %s\n", This, targetStateBlock, i,
390                     targetStateBlock->vertexShaderConstantB[i]? "TRUE":"FALSE");
391
392                 This->vertexShaderConstantB[i] =  targetStateBlock->vertexShaderConstantB[i];
393             }
394         }
395         
396         /* Lights... For a recorded state block, we just had a chain of actions to perform,
397              so we need to walk that chain and update any actions which differ */
398         src = This->lights;
399         while (src != NULL) {
400             PLIGHTINFOEL *realLight = NULL;
401
402             /* Locate the light in the live lights */
403             realLight = targetStateBlock->lights;
404             while (realLight != NULL && realLight->OriginalIndex != src->OriginalIndex) realLight = realLight->next;
405
406             /* If 'changed' then its a SetLight command. Rather than comparing to see
407                  if the OriginalParms have changed and then copy them (twice through
408                  memory) just do the copy                                              */
409             if (src->changed) {
410
411                 /* If the light exists, copy its parameters, otherwise copy the default parameters */
412                 const WINED3DLIGHT* params = realLight? &realLight->OriginalParms: &WINED3D_default_light;
413                 TRACE("Updating lights for light %d\n", src->OriginalIndex);
414                 memcpy(&src->OriginalParms, params, sizeof(*params));
415             }
416
417             /* If 'enabledchanged' then its a LightEnable command */
418             if (src->enabledChanged) {
419
420                 /* If the light exists, check if it's enabled, otherwise default is disabled state */
421                 TRACE("Updating lightEnabled for light %d\n", src->OriginalIndex);
422                 src->lightEnabled = realLight? realLight->lightEnabled: FALSE;
423             }
424
425             src = src->next;
426         }
427
428         /* Recorded => Only update 'changed' values */
429         if (This->pixelShader != targetStateBlock->pixelShader) {
430             TRACE("Updating pixel shader from %p to %p\n", This->pixelShader, targetStateBlock->pixelShader);
431
432             if (targetStateBlock->pixelShader) {
433                 IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
434             }
435             if (This->pixelShader) {
436                 IWineD3DPixelShader_Release(This->pixelShader);
437             }
438
439             This->pixelShader = targetStateBlock->pixelShader;
440         }
441
442         /* Pixel Shader Float Constants */
443         for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
444             if (This->set.pixelShaderConstantsF[i]) {
445                 TRACE("Setting %p from %p %d to { %f, %f, %f, %f }\n", This, targetStateBlock, i,
446                     targetStateBlock->pixelShaderConstantF[i * 4],
447                     targetStateBlock->pixelShaderConstantF[i * 4 + 1],
448                     targetStateBlock->pixelShaderConstantF[i * 4 + 2],
449                     targetStateBlock->pixelShaderConstantF[i * 4 + 3]);
450
451                 This->pixelShaderConstantF[i * 4]      = targetStateBlock->pixelShaderConstantF[i * 4];
452                 This->pixelShaderConstantF[i * 4 + 1]  = targetStateBlock->pixelShaderConstantF[i * 4 + 1];
453                 This->pixelShaderConstantF[i * 4 + 2]  = targetStateBlock->pixelShaderConstantF[i * 4 + 2];
454                 This->pixelShaderConstantF[i * 4 + 3]  = targetStateBlock->pixelShaderConstantF[i * 4 + 3];
455             }
456         }
457         
458         /* Pixel Shader Integer Constants */
459         for (i = 0; i < MAX_CONST_I; ++i) {
460             if (This->set.pixelShaderConstantsI[i]) {
461                 TRACE("Setting %p from %p %d to { %d, %d, %d, %d }\n", This, targetStateBlock, i,
462                     targetStateBlock->pixelShaderConstantI[i * 4],
463                     targetStateBlock->pixelShaderConstantI[i * 4 + 1],
464                     targetStateBlock->pixelShaderConstantI[i * 4 + 2],
465                     targetStateBlock->pixelShaderConstantI[i * 4 + 3]);
466
467                 This->pixelShaderConstantI[i * 4]      = targetStateBlock->pixelShaderConstantI[i * 4];
468                 This->pixelShaderConstantI[i * 4 + 1]  = targetStateBlock->pixelShaderConstantI[i * 4 + 1];
469                 This->pixelShaderConstantI[i * 4 + 2]  = targetStateBlock->pixelShaderConstantI[i * 4 + 2];
470                 This->pixelShaderConstantI[i * 4 + 3]  = targetStateBlock->pixelShaderConstantI[i * 4 + 3];
471             }
472         }
473         
474         /* Pixel Shader Boolean Constants */
475         for (i = 0; i < MAX_CONST_B; ++i) {
476             if (This->set.pixelShaderConstantsB[i]) {
477                 TRACE("Setting %p from %p %d to %s\n", This, targetStateBlock, i,
478                     targetStateBlock->pixelShaderConstantB[i]? "TRUE":"FALSE");
479
480                 This->pixelShaderConstantB[i] =  targetStateBlock->pixelShaderConstantB[i];
481             }
482         }
483
484         /* Others + Render & Texture */
485         for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
486             if (This->set.transform[i] && memcmp(&targetStateBlock->transforms[i],
487                                     &This->transforms[i],
488                                     sizeof(WINED3DMATRIX)) != 0) {
489                 TRACE("Updating transform %d\n", i);
490                 memcpy(&This->transforms[i], &targetStateBlock->transforms[i], sizeof(WINED3DMATRIX));
491             }
492         }
493
494         if (This->set.indices && ((This->pIndexData != targetStateBlock->pIndexData)
495                         || (This->baseVertexIndex != targetStateBlock->baseVertexIndex))) {
496             TRACE("Updating pindexData to %p, baseVertexIndex to %d\n",
497             targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
498             This->pIndexData = targetStateBlock->pIndexData;
499             This->baseVertexIndex = targetStateBlock->baseVertexIndex;
500         }
501
502         if(This->set.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl){
503             TRACE("Updating vertex declaration from %p to %p\n", This->vertexDecl, targetStateBlock->vertexDecl);
504
505             if (targetStateBlock->vertexDecl) {
506                 IWineD3DVertexDeclaration_AddRef(targetStateBlock->vertexDecl);
507             }
508             if (This->vertexDecl) {
509                 IWineD3DVertexDeclaration_Release(This->vertexDecl);
510             }
511
512             This->vertexDecl = targetStateBlock->vertexDecl;
513         }
514
515         if(This->set.fvf && This->fvf != targetStateBlock->fvf){
516             This->fvf = targetStateBlock->fvf;
517         }
518
519         if (This->set.material && memcmp(&targetStateBlock->material,
520                                                     &This->material,
521                                                     sizeof(WINED3DMATERIAL)) != 0) {
522             TRACE("Updating material\n");
523             memcpy(&This->material, &targetStateBlock->material, sizeof(WINED3DMATERIAL));
524         }
525
526         if (This->set.viewport && memcmp(&targetStateBlock->viewport,
527                                                     &This->viewport,
528                                                     sizeof(WINED3DVIEWPORT)) != 0) {
529             TRACE("Updating viewport\n");
530             memcpy(&This->viewport, &targetStateBlock->viewport, sizeof(WINED3DVIEWPORT));
531         }
532
533         for (i = 0; i < MAX_STREAMS; i++) {
534             if (This->set.streamSource[i] &&
535                             ((This->streamStride[i] != targetStateBlock->streamStride[i]) ||
536                             (This->streamSource[i] != targetStateBlock->streamSource[i]))) {
537                 TRACE("Updating stream source %d to %p, stride to %d\n", i, targetStateBlock->streamSource[i],
538                                                                             targetStateBlock->streamStride[i]);
539                 This->streamStride[i] = targetStateBlock->streamStride[i];
540                 This->streamSource[i] = targetStateBlock->streamSource[i];
541             }
542
543             if (This->set.streamFreq[i] &&
544             (This->streamFreq[i] != targetStateBlock->streamFreq[i]
545             || This->streamFlags[i] != targetStateBlock->streamFlags[i])){
546                     TRACE("Updating stream frequency %d to %d flags to %d\n", i ,  targetStateBlock->streamFreq[i] ,
547                                                                                    targetStateBlock->streamFlags[i]);
548                     This->streamFreq[i]  =  targetStateBlock->streamFreq[i];
549                     This->streamFlags[i] =  targetStateBlock->streamFlags[i];
550             }
551         }
552
553         for (i = 0; i < GL_LIMITS(clipplanes); i++) {
554             if (This->set.clipplane[i] && memcmp(&targetStateBlock->clipplane[i],
555                                                         &This->clipplane[i],
556                                                         sizeof(This->clipplane)) != 0) {
557
558                 TRACE("Updating clipplane %d\n", i);
559                 memcpy(&This->clipplane[i], &targetStateBlock->clipplane[i],
560                                         sizeof(This->clipplane));
561             }
562         }
563
564         /* Render */
565         for (i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
566
567             if (This->set.renderState[i] && (This->renderState[i] != targetStateBlock->renderState[i])) {
568                 TRACE("Updating renderState %d to %d\n", i, targetStateBlock->renderState[i]);
569                 This->renderState[i] = targetStateBlock->renderState[i];
570             }
571         }
572
573         /* Texture states */
574         for (j = 0; j < GL_LIMITS(texture_stages); j++) {
575             /* TODO: move over to using memcpy */
576             for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE ; i++) {
577                 if (This->set.textureState[j][i]) {
578                     TRACE("Updating texturestagestate %d,%d to %d (was %d)\n", j,i, targetStateBlock->textureState[j][i],
579                     This->textureState[j][i]);
580                     This->textureState[j][i]         =  targetStateBlock->textureState[j][i];
581                 }
582             }
583         }
584
585         /* Samplers */
586         /* TODO: move over to using memcpy */
587         for (j = 0; j < GL_LIMITS(sampler_stages); j++) {
588             if (This->set.textures[j]) {
589                 TRACE("Updating texture %d to %p (was %p)\n", j, targetStateBlock->textures[j],  This->textures[j]);
590                 This->textures[j] = targetStateBlock->textures[j];
591             }
592             for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE ; i++){ /* States are 1 based */
593                 if (This->set.samplerState[j][i]) {
594                     TRACE("Updating sampler state %d,%d to %d (was %d)\n",
595                     j, i, targetStateBlock->samplerState[j][i],
596                     This->samplerState[j][i]);
597                     This->samplerState[j][i]         = targetStateBlock->samplerState[j][i];
598                 }
599             }
600         }
601     }
602
603     TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
604
605     return WINED3D_OK;
606 }
607
608 static HRESULT  WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
609     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
610     IWineD3DDevice*        pDevice     = (IWineD3DDevice*)This->wineD3DDevice;
611
612 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
613 should really perform a delta so that only the changes get updated*/
614
615
616     UINT i;
617     UINT j;
618
619     TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
620
621     /* FIXME: Only apply applicable states not all states */
622
623     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */This->blockType == WINED3DSBT_INIT || This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_VERTEXSTATE) {
624
625
626         PLIGHTINFOEL *toDo = This->lights;
627         while (toDo != NULL) {
628             if (toDo->changed)
629                   IWineD3DDevice_SetLight(pDevice, toDo->OriginalIndex, &toDo->OriginalParms);
630             if (toDo->enabledChanged)
631                   IWineD3DDevice_SetLightEnable(pDevice, toDo->OriginalIndex, toDo->lightEnabled);
632             toDo = toDo->next;
633         }
634
635         /* Vertex Shader */
636         if (This->set.vertexShader && This->changed.vertexShader) {
637             IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
638         }
639
640         /* Vertex Shader Constants */
641         for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
642             if (This->set.vertexShaderConstantsF[i] && This->changed.vertexShaderConstantsF[i])
643                 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i, This->vertexShaderConstantF + i * 4, 1);
644         }
645         
646         for (i = 0; i < MAX_CONST_I; i++) {
647             if (This->set.vertexShaderConstantsI[i] && This->changed.vertexShaderConstantsI[i])
648                 IWineD3DDevice_SetVertexShaderConstantI(pDevice, i, This->vertexShaderConstantI + i * 4, 1);
649         }
650         
651         for (i = 0; i < MAX_CONST_B; i++) {
652             if (This->set.vertexShaderConstantsB[i] && This->changed.vertexShaderConstantsB[i])
653                 IWineD3DDevice_SetVertexShaderConstantB(pDevice, i, This->vertexShaderConstantB + i, 1);
654         }
655     }
656
657     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == D3DSBT_ALL || This->blockType == D3DSBT_PIXELSTATE) {
658
659         /* Pixel Shader */
660         if (This->set.pixelShader && This->changed.pixelShader) {
661             IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
662         }
663
664         /* Pixel Shader Constants */
665         for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
666             if (This->set.pixelShaderConstantsF[i] && This->changed.pixelShaderConstantsF[i])
667                 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i, This->pixelShaderConstantF + i * 4, 1);
668         }
669
670         for (i = 0; i < MAX_CONST_I; ++i) {
671             if (This->set.pixelShaderConstantsI[i] && This->changed.pixelShaderConstantsI[i])
672                 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i, This->pixelShaderConstantI + i * 4, 1);
673         }
674         
675         for (i = 0; i < MAX_CONST_B; ++i) {
676             if (This->set.pixelShaderConstantsB[i] && This->changed.pixelShaderConstantsB[i])
677                 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i, This->pixelShaderConstantB + i, 1);
678         }
679     }
680
681     if (This->set.fvf && This->changed.fvf) {
682         IWineD3DDevice_SetFVF(pDevice, This->fvf);
683     }
684
685     if (This->set.vertexDecl && This->changed.vertexDecl) {
686         IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
687     }
688
689     /* Others + Render & Texture */
690     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_INIT) {
691         for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
692             if (This->set.transform[i] && This->changed.transform[i])
693                 IWineD3DDevice_SetTransform(pDevice, i, &This->transforms[i]);
694         }
695
696         if (This->set.indices && This->changed.indices)
697             IWineD3DDevice_SetIndices(pDevice, This->pIndexData, This->baseVertexIndex);
698
699         if (This->set.material && This->changed.material )
700             IWineD3DDevice_SetMaterial(pDevice, &This->material);
701
702         if (This->set.viewport && This->changed.viewport)
703             IWineD3DDevice_SetViewport(pDevice, &This->viewport);
704
705         /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
706         for (i=0; i<MAX_STREAMS; i++) {
707             if (This->set.streamSource[i] && This->changed.streamSource[i])
708                 IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
709
710             if (This->set.streamFreq[i] && This->changed.streamFreq[i])
711                 IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
712         }
713
714         for (i = 0; i < GL_LIMITS(clipplanes); i++) {
715             if (This->set.clipplane[i] && This->changed.clipplane[i]) {
716                 float clip[4];
717
718                 clip[0] = This->clipplane[i][0];
719                 clip[1] = This->clipplane[i][1];
720                 clip[2] = This->clipplane[i][2];
721                 clip[3] = This->clipplane[i][3];
722                 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
723             }
724         }
725
726         /* Render */
727         for (i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
728             if (This->set.renderState[i] && This->changed.renderState[i])
729                 IWineD3DDevice_SetRenderState(pDevice, i, This->renderState[i]);
730         }
731
732         /* Texture states */
733         for (j = 0; j < GL_LIMITS(texture_stages); j++) { /* Set The texture first, just in case it resets the states? */
734             /* TODO: move over to memcpy */
735             for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE; i++) {
736                 if (This->set.textureState[j][i] && This->changed.textureState[j][i]) { /* tb_dx9_10 failes without this test */
737                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][i]         = This->textureState[j][i];
738                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->set.textureState[j][i]     = TRUE;
739                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[j][i] = TRUE;
740                     /* TODO: Record a display list to apply all gl states. For now apply by brute force */
741                     IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(j, i));
742                 }
743             }
744         }
745
746         /* Samplers */
747         /* TODO: move over to memcpy */
748         for (j = 0 ; j < GL_LIMITS(sampler_stages); j++){
749             if (This->set.textures[j] && This->changed.textures[j]) {
750                 IWineD3DDevice_SetTexture(pDevice, j, This->textures[j]);
751             }
752             for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; i++){
753                 if (This->set.samplerState[j][i] && This->changed.samplerState[j][i]) {
754                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][i]         = This->samplerState[j][i];
755                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->set.samplerState[j][i]     = TRUE;
756                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[j][i] = TRUE;
757                 }
758             }
759             /* SetTexture catches nop changes, so the above call does not assure that the sampler is updated */
760             IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_SAMPLER(j));
761         }
762
763     } else if (This->blockType == WINED3DSBT_PIXELSTATE) {
764
765         for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
766             if (This->set.renderState[SavedPixelStates_R[i]] && This->changed.renderState[SavedPixelStates_R[i]])
767                 IWineD3DDevice_SetRenderState(pDevice, SavedPixelStates_R[i], This->renderState[SavedPixelStates_R[i]]);
768
769         }
770
771         for (j = 0; j < GL_LIMITS(texture_stages); j++) {
772             for (i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
773                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedPixelStates_T[i]] = This->textureState[j][SavedPixelStates_T[i]];
774                 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(j, SavedPixelStates_T[i]));
775             }
776         }
777
778         for (j = 0; j < GL_LIMITS(sampler_stages); j++) {
779             for (i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
780                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedPixelStates_S[i]] = This->samplerState[j][SavedPixelStates_S[i]];
781             }
782         }
783
784     } else if (This->blockType == WINED3DSBT_VERTEXSTATE) {
785
786         for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
787             if ( This->set.renderState[SavedVertexStates_R[i]] && This->changed.renderState[SavedVertexStates_R[i]])
788                 IWineD3DDevice_SetRenderState(pDevice, SavedVertexStates_R[i], This->renderState[SavedVertexStates_R[i]]);
789         }
790
791         for (j = 0; j < GL_LIMITS(texture_stages); j++) {
792             for (i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
793                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedVertexStates_T[i]] = This->textureState[j][SavedVertexStates_T[i]];
794                 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(j, SavedVertexStates_T[i]));
795             }
796         }
797
798         for (j = 0; j < GL_LIMITS(sampler_stages); j++) {
799             for (i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
800                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedVertexStates_S[i]] = This->samplerState[j][SavedVertexStates_S[i]];
801             }
802         }
803
804
805     } else {
806         FIXME("Unrecognized state block type %d\n", This->blockType);
807     }
808     stateblock_savedstates_copy(iface, &((IWineD3DDeviceImpl*)pDevice)->stateBlock->changed, &This->changed);
809     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = MAX_TEXTURES - 1;
810     for(j = 0; j < MAX_TEXTURES - 1; j++) {
811         if(((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][D3DTSS_COLOROP] == WINED3DTOP_DISABLE) {
812             ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = j;
813             break;
814         }
815     }
816     TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
817
818     return WINED3D_OK;
819 }
820
821 static HRESULT  WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
822     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
823     IWineD3DDevice         *device = (IWineD3DDevice *)This->wineD3DDevice;
824     IWineD3DDeviceImpl     *ThisDevice = (IWineD3DDeviceImpl *)device;
825     union {
826         WINED3DLINEPATTERN lp;
827         DWORD d;
828     } lp;
829     union {
830         float f;
831         DWORD d;
832     } tmpfloat;
833     unsigned int i;
834
835     /* Note this may have a large overhead but it should only be executed
836        once, in order to initialize the complete state of the device and
837        all opengl equivalents                                            */
838     TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
839     /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
840     This->blockType = WINED3DSBT_INIT;
841
842     /* Set some of the defaults for lights, transforms etc */
843     memcpy(&This->transforms[WINED3DTS_PROJECTION], &identity, sizeof(identity));
844     memcpy(&This->transforms[WINED3DTS_VIEW], &identity, sizeof(identity));
845     for (i = 0; i < 256; ++i) {
846       memcpy(&This->transforms[WINED3DTS_WORLDMATRIX(i)], &identity, sizeof(identity));
847     }
848
849     TRACE("Render states\n");
850     /* Render states: */
851     if (ThisDevice->depthStencilBuffer != NULL) {
852        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       WINED3DZB_TRUE);
853     } else {
854        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       WINED3DZB_FALSE);
855     }
856     IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE,         WINED3DFILL_SOLID);
857     IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE,        WINED3DSHADE_GOURAUD);
858     lp.lp.wRepeatFactor = 0;
859     lp.lp.wLinePattern  = 0;
860     IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN,      lp.d);
861     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE,     TRUE);
862     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE,  FALSE);
863     IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL,        TRUE);
864     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND,         WINED3DBLEND_ONE);
865     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND,        WINED3DBLEND_ZERO);
866     IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE,         WINED3DCULL_CCW);
867     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC,            WINED3DCMP_LESSEQUAL);
868     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC,        WINED3DCMP_ALWAYS);
869     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF,         0);
870     IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE,     FALSE);
871     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
872     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE,        FALSE);
873     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE,   FALSE);
874     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE,         0);
875     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR,         0);
876     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE,     WINED3DFOG_NONE);
877     tmpfloat.f = 0.0f;
878     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART,         tmpfloat.d);
879     tmpfloat.f = 1.0f;
880     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND,           tmpfloat.d);
881     tmpfloat.f = 1.0f;
882     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY,       tmpfloat.d);
883     IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS,    FALSE);
884     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS,            0);
885     IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE,   FALSE);
886     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE,    FALSE);
887     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL,      WINED3DSTENCILOP_KEEP);
888     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL,     WINED3DSTENCILOP_KEEP);
889     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS,      WINED3DSTENCILOP_KEEP);
890
891     /* Setting stencil func also uses values for stencil ref/mask, so manually set defaults
892      * so only a single call performed (and ensure defaults initialized before making that call)
893      *
894      * IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF, 0);
895      * IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK, 0xFFFFFFFF);
896      */
897     This->renderState[WINED3DRS_STENCILREF] = 0;
898     This->renderState[WINED3DRS_STENCILMASK] = 0xFFFFFFFF;
899     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC,      WINED3DCMP_ALWAYS);
900     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
901     IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR,    0xFFFFFFFF);
902     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
903     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
904     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
905     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
906     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
907     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
908     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
909     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
910     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING,                 TRUE);
911     IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING,                 TRUE);
912     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT,                  0);
913     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE,            WINED3DFOG_NONE);
914     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX,              TRUE);
915     IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER,              TRUE);
916     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS,         FALSE);
917     IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE,    WINED3DMCS_COLOR1);
918     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE,   WINED3DMCS_COLOR2);
919     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE,    WINED3DMCS_MATERIAL);
920     IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE,   WINED3DMCS_MATERIAL);
921     IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND,              WINED3DVBF_DISABLE);
922     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE,          0);
923     IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
924     tmpfloat.f = 1.0f;
925     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE,                tmpfloat.d);
926     tmpfloat.f = 1.0f;
927     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN,            tmpfloat.d);
928     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE,        FALSE);
929     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE,         FALSE);
930     tmpfloat.f = 1.0f;
931     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A,             tmpfloat.d);
932     tmpfloat.f = 0.0f;
933     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B,             tmpfloat.d);
934     tmpfloat.f = 0.0f;
935     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C,             tmpfloat.d);
936     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS,     TRUE);
937     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK,          0xFFFFFFFF);
938     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE,           WINED3DPATCHEDGE_DISCRETE);
939     tmpfloat.f = 1.0f;
940     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS,            tmpfloat.d);
941     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN,        0xbaadcafe);
942     tmpfloat.f = 64.0f;
943     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX,            tmpfloat.d);
944     IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
945     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE,         0x0000000F);
946     tmpfloat.f = 0.0f;
947     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR,              tmpfloat.d);
948     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP,                  WINED3DBLENDOP_ADD);
949     IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE,           WINED3DDEGREE_CUBIC);
950     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE,             WINED3DDEGREE_LINEAR);
951     /* states new in d3d9 */
952     IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE,        FALSE);
953     IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS,      0);
954     tmpfloat.f = 1.0f;
955     IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL,     tmpfloat.d);
956     IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL,     tmpfloat.d);
957     IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE,    FALSE);
958     tmpfloat.f = 0.0f;
959     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X,           tmpfloat.d);
960     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y,           tmpfloat.d);
961     tmpfloat.f = 1.0f;
962     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z,           tmpfloat.d);
963     tmpfloat.f = 0.0f;
964     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W,           tmpfloat.d);
965     IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
966     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE,      FALSE);
967     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL,          WINED3DSTENCILOP_KEEP);
968     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL,         WINED3DSTENCILOP_KEEP);
969     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS,          WINED3DSTENCILOP_KEEP);
970     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC,          WINED3DCMP_ALWAYS);
971     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1,        0x0000000F);
972     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2,        0x0000000F);
973     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3,        0x0000000F);
974     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR,              0xFFFFFFFF);
975     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE,          0);
976     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS,                0);
977     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8,  0);
978     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9,  0);
979     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
980     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
981     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
982     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
983     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
984     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
985     IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
986     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA,            WINED3DBLEND_ONE);
987     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA,           WINED3DBLEND_ZERO);
988     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA,             WINED3DBLENDOP_ADD);
989
990     /* clipping status */
991     This->clip_status.ClipUnion = 0;
992     This->clip_status.ClipIntersection = 0xFFFFFFFF;
993
994     /* Texture Stage States - Put directly into state block, we will call function below */
995     for (i = 0; i < GL_LIMITS(texture_stages); i++) {
996         TRACE("Setting up default texture states for texture Stage %d\n", i);
997         memcpy(&This->transforms[WINED3DTS_TEXTURE0 + i], &identity, sizeof(identity));
998         This->textureState[i][WINED3DTSS_COLOROP               ] = (i==0)? WINED3DTOP_MODULATE :  WINED3DTOP_DISABLE;
999         This->textureState[i][WINED3DTSS_COLORARG1             ] = WINED3DTA_TEXTURE;
1000         This->textureState[i][WINED3DTSS_COLORARG2             ] = WINED3DTA_CURRENT;
1001         This->textureState[i][WINED3DTSS_ALPHAOP               ] = (i==0)? WINED3DTOP_SELECTARG1 :  WINED3DTOP_DISABLE;
1002         This->textureState[i][WINED3DTSS_ALPHAARG1             ] = WINED3DTA_TEXTURE;
1003         This->textureState[i][WINED3DTSS_ALPHAARG2             ] = WINED3DTA_CURRENT;
1004         This->textureState[i][WINED3DTSS_BUMPENVMAT00          ] = (DWORD) 0.0;
1005         This->textureState[i][WINED3DTSS_BUMPENVMAT01          ] = (DWORD) 0.0;
1006         This->textureState[i][WINED3DTSS_BUMPENVMAT10          ] = (DWORD) 0.0;
1007         This->textureState[i][WINED3DTSS_BUMPENVMAT11          ] = (DWORD) 0.0;
1008         This->textureState[i][WINED3DTSS_TEXCOORDINDEX         ] = i;
1009         This->textureState[i][WINED3DTSS_BUMPENVLSCALE         ] = (DWORD) 0.0;
1010         This->textureState[i][WINED3DTSS_BUMPENVLOFFSET        ] = (DWORD) 0.0;
1011         This->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS ] = WINED3DTTFF_DISABLE;
1012         This->textureState[i][WINED3DTSS_ADDRESSW              ] = WINED3DTADDRESS_WRAP;
1013         This->textureState[i][WINED3DTSS_COLORARG0             ] = WINED3DTA_CURRENT;
1014         This->textureState[i][WINED3DTSS_ALPHAARG0             ] = WINED3DTA_CURRENT;
1015         This->textureState[i][WINED3DTSS_RESULTARG             ] = WINED3DTA_CURRENT;
1016     }
1017     This->lowest_disabled_stage = 1;
1018
1019         /* Sampler states*/
1020     for (i = 0 ; i <  GL_LIMITS(sampler_stages); i++) {
1021         TRACE("Setting up default samplers states for sampler %d\n", i);
1022         This->samplerState[i][WINED3DSAMP_ADDRESSU         ] = WINED3DTADDRESS_WRAP;
1023         This->samplerState[i][WINED3DSAMP_ADDRESSV         ] = WINED3DTADDRESS_WRAP;
1024         This->samplerState[i][WINED3DSAMP_ADDRESSW         ] = WINED3DTADDRESS_WRAP;
1025         This->samplerState[i][WINED3DSAMP_BORDERCOLOR      ] = 0x00;
1026         This->samplerState[i][WINED3DSAMP_MAGFILTER        ] = WINED3DTEXF_POINT;
1027         This->samplerState[i][WINED3DSAMP_MINFILTER        ] = WINED3DTEXF_POINT;
1028         This->samplerState[i][WINED3DSAMP_MIPFILTER        ] = WINED3DTEXF_NONE;
1029         This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS    ] = 0;
1030         This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL      ] = 0;
1031         This->samplerState[i][WINED3DSAMP_MAXANISOTROPY    ] = 1;
1032         This->samplerState[i][WINED3DSAMP_SRGBTEXTURE      ] = 0; /* TODO: Gamma correction value*/
1033         This->samplerState[i][WINED3DSAMP_ELEMENTINDEX     ] = 0; /* TODO: Indicates which element of a  multielement texture to use */
1034         This->samplerState[i][WINED3DSAMP_DMAPOFFSET       ] = 0; /* TODO: Vertex offset in the presampled displacement map */
1035     }
1036
1037     /* Under DirectX you can have texture stage operations even if no texture is
1038        bound, whereas opengl will only do texture operations when a valid texture is
1039        bound. We emulate this by creating dummy textures and binding them to each
1040        texture stage, but disable all stages by default. Hence if a stage is enabled
1041        then the default texture will kick in until replaced by a SetTexture call     */
1042     ENTER_GL();
1043
1044     for (i = 0; i < GL_LIMITS(texture_stages); i++) {
1045         GLubyte white = 255;
1046
1047         /* Note this avoids calling settexture, so pretend it has been called */
1048         This->set.textures[i]     = TRUE;
1049         This->changed.textures[i] = TRUE;
1050         This->textures[i]         = NULL;
1051
1052         /* Make appropriate texture active */
1053         if (GL_SUPPORT(ARB_MULTITEXTURE)) {
1054             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1055             checkGLcall("glActiveTextureARB");
1056         } else if (i > 0) {
1057             FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
1058         }
1059
1060         /* Generate an opengl texture name */
1061         glGenTextures(1, &ThisDevice->dummyTextureName[i]);
1062         checkGLcall("glGenTextures");
1063         TRACE("Dummy Texture %d given name %d\n", i, ThisDevice->dummyTextureName[i]);
1064
1065         /* Generate a dummy 1d texture */
1066         This->textureDimensions[i] = GL_TEXTURE_1D;
1067         glBindTexture(GL_TEXTURE_1D, ThisDevice->dummyTextureName[i]);
1068         checkGLcall("glBindTexture");
1069
1070         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &white);
1071         checkGLcall("glTexImage1D");
1072 #if 1   /* TODO: move the setting texture states off to basetexture */
1073         /* Reapply all the texture state information to this texture */
1074         IWineD3DDevice_SetupTextureStates(device, i, i, REAPPLY_ALL);
1075 #endif
1076     }
1077
1078     LEAVE_GL();
1079
1080
1081     /* Defaulting palettes - Note these are device wide but reinitialized here for convenience*/
1082     for (i = 0; i < MAX_PALETTES; ++i) {
1083       int j;
1084       for (j = 0; j < 256; ++j) {
1085         This->wineD3DDevice->palettes[i][j].peRed   = 0xFF;
1086         This->wineD3DDevice->palettes[i][j].peGreen = 0xFF;
1087         This->wineD3DDevice->palettes[i][j].peBlue  = 0xFF;
1088         This->wineD3DDevice->palettes[i][j].peFlags = 0xFF;
1089       }
1090     }
1091     This->wineD3DDevice->currentPalette = 0;
1092
1093     /* Set default GLSL program to NULL.  We won't actually create one
1094      * until the app sets a vertex or pixel shader */
1095     This->glsl_program = NULL;
1096
1097     TRACE("-----------------------> Device defaults now set up...\n");
1098     return WINED3D_OK;
1099 }
1100
1101 /**********************************************************
1102  * IWineD3DStateBlock VTbl follows
1103  **********************************************************/
1104
1105 const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
1106 {
1107     /* IUnknown */
1108     IWineD3DStateBlockImpl_QueryInterface,
1109     IWineD3DStateBlockImpl_AddRef,
1110     IWineD3DStateBlockImpl_Release,
1111     /* IWineD3DStateBlock */
1112     IWineD3DStateBlockImpl_GetParent,
1113     IWineD3DStateBlockImpl_GetDevice,
1114     IWineD3DStateBlockImpl_Capture,
1115     IWineD3DStateBlockImpl_Apply,
1116     IWineD3DStateBlockImpl_InitStartupStateBlock
1117 };