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