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