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