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