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