actxprxy: Add proxies for comcat.idl.
[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  * Copyright 2007 Stefan Dösinger for CodeWeavers
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 #define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
29
30 /***************************************
31  * Stateblock helper functions follow
32  **************************************/
33
34 /* Allocates the correct amount of space for pixel and vertex shader constants,
35  * along with their set/changed flags on the given stateblock object
36  */
37 HRESULT allocate_shader_constants(IWineD3DStateBlockImpl* object)
38 {
39     IWineD3DStateBlockImpl *This = object;
40
41     /* Allocate space for floating point constants */
42     object->pixelShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
43     if (!object->pixelShaderConstantF) goto fail;
44
45     object->changed.pixelShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(pshader_constantsF));
46     if (!object->changed.pixelShaderConstantsF) goto fail;
47
48     object->vertexShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
49     if (!object->vertexShaderConstantF) goto fail;
50
51     object->changed.vertexShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(vshader_constantsF));
52     if (!object->changed.vertexShaderConstantsF) goto fail;
53
54     object->contained_vs_consts_f = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * GL_LIMITS(vshader_constantsF));
55     if (!object->contained_vs_consts_f) goto fail;
56
57     object->contained_ps_consts_f = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * GL_LIMITS(pshader_constantsF));
58     if (!object->contained_ps_consts_f) goto fail;
59
60     return WINED3D_OK;
61
62 fail:
63     ERR("Failed to allocate memory\n");
64     HeapFree(GetProcessHeap(), 0, object->pixelShaderConstantF);
65     HeapFree(GetProcessHeap(), 0, object->changed.pixelShaderConstantsF);
66     HeapFree(GetProcessHeap(), 0, object->vertexShaderConstantF);
67     HeapFree(GetProcessHeap(), 0, object->changed.vertexShaderConstantsF);
68     HeapFree(GetProcessHeap(), 0, object->contained_vs_consts_f);
69     HeapFree(GetProcessHeap(), 0, object->contained_ps_consts_f);
70     return E_OUTOFMEMORY;
71 }
72
73 /** Copy all members of one stateblock to another */
74 static void stateblock_savedstates_copy(IWineD3DStateBlock* iface, SAVEDSTATES *dest, const SAVEDSTATES *source)
75 {
76     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
77     unsigned bsize = sizeof(BOOL);
78
79     /* Single values */
80     dest->primitive_type = source->primitive_type;
81     dest->indices = source->indices;
82     dest->material = source->material;
83     dest->viewport = source->viewport;
84     dest->vertexDecl = source->vertexDecl;
85     dest->pixelShader = source->pixelShader;
86     dest->vertexShader = source->vertexShader;
87     dest->scissorRect = dest->scissorRect;
88
89     /* Fixed size arrays */
90     dest->streamSource = source->streamSource;
91     dest->streamFreq = source->streamFreq;
92     dest->textures = source->textures;
93     memcpy(dest->transform, source->transform, sizeof(source->transform));
94     memcpy(dest->renderState, source->renderState, sizeof(source->renderState));
95     memcpy(dest->textureState, source->textureState, sizeof(source->textureState));
96     memcpy(dest->samplerState, source->samplerState, sizeof(source->samplerState));
97     dest->clipplane = source->clipplane;
98     dest->pixelShaderConstantsB = source->pixelShaderConstantsB;
99     dest->pixelShaderConstantsI = source->pixelShaderConstantsI;
100     dest->vertexShaderConstantsB = source->vertexShaderConstantsB;
101     dest->vertexShaderConstantsI = source->vertexShaderConstantsI;
102
103     /* Dynamically sized arrays */
104     memcpy(dest->pixelShaderConstantsF, source->pixelShaderConstantsF, bsize * GL_LIMITS(pshader_constantsF));
105     memcpy(dest->vertexShaderConstantsF, source->vertexShaderConstantsF, bsize * GL_LIMITS(vshader_constantsF));
106 }
107
108 static inline void stateblock_set_bits(DWORD *map, UINT map_size)
109 {
110     DWORD mask = (1 << (map_size & 0x1f)) - 1;
111     memset(map, 0xff, (map_size >> 5) * sizeof(*map));
112     if (mask) map[map_size >> 5] = mask;
113 }
114
115 /** Set all members of a stateblock savedstate to the given value */
116 void stateblock_savedstates_set(IWineD3DStateBlock *iface, SAVEDSTATES *states, BOOL value)
117 {
118     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
119     unsigned bsize = sizeof(BOOL);
120
121     /* Single values */
122     states->primitive_type = value;
123     states->indices = value;
124     states->material = value;
125     states->viewport = value;
126     states->vertexDecl = value;
127     states->pixelShader = value;
128     states->vertexShader = value;
129     states->scissorRect = value;
130
131     /* Fixed size arrays */
132     if (value)
133     {
134         int i;
135         states->streamSource = 0xffff;
136         states->streamFreq = 0xffff;
137         states->textures = 0xfffff;
138         stateblock_set_bits(states->transform, HIGHEST_TRANSFORMSTATE + 1);
139         stateblock_set_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
140         for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
141         for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3fff;
142         states->clipplane = 0xffffffff;
143         states->pixelShaderConstantsB = 0xffff;
144         states->pixelShaderConstantsI = 0xffff;
145         states->vertexShaderConstantsB = 0xffff;
146         states->vertexShaderConstantsI = 0xffff;
147     }
148     else
149     {
150         states->streamSource = 0;
151         states->streamFreq = 0;
152         states->textures = 0;
153         memset(states->transform, 0, sizeof(states->transform));
154         memset(states->renderState, 0, sizeof(states->renderState));
155         memset(states->textureState, 0, sizeof(states->textureState));
156         memset(states->samplerState, 0, sizeof(states->samplerState));
157         states->clipplane = 0;
158         states->pixelShaderConstantsB = 0;
159         states->pixelShaderConstantsI = 0;
160         states->vertexShaderConstantsB = 0;
161         states->vertexShaderConstantsI = 0;
162     }
163
164     /* Dynamically sized arrays */
165     memset(states->pixelShaderConstantsF, value, bsize * GL_LIMITS(pshader_constantsF));
166     memset(states->vertexShaderConstantsF, value, bsize * GL_LIMITS(vshader_constantsF));
167 }
168
169 void stateblock_copy(
170     IWineD3DStateBlock* destination,
171     IWineD3DStateBlock* source) {
172     int l;
173
174     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)source;
175     IWineD3DStateBlockImpl *Dest = (IWineD3DStateBlockImpl *)destination;
176
177     /* IUnknown fields */
178     Dest->lpVtbl                = This->lpVtbl;
179     Dest->ref                   = This->ref;
180
181     /* IWineD3DStateBlock information */
182     Dest->parent                = This->parent;
183     Dest->wineD3DDevice         = This->wineD3DDevice;
184     Dest->blockType             = This->blockType;
185
186     /* Saved states */
187     stateblock_savedstates_copy(source, &Dest->changed, &This->changed);
188
189     /* Single items */
190     Dest->gl_primitive_type = This->gl_primitive_type;
191     Dest->vertexDecl = This->vertexDecl;
192     Dest->vertexShader = This->vertexShader;
193     Dest->streamIsUP = This->streamIsUP;
194     Dest->pIndexData = This->pIndexData;
195     Dest->IndexFmt = This->IndexFmt;
196     Dest->baseVertexIndex = This->baseVertexIndex;
197     /* Dest->lights = This->lights; */
198     Dest->clip_status = This->clip_status;
199     Dest->viewport = This->viewport;
200     Dest->material = This->material;
201     Dest->pixelShader = This->pixelShader;
202     Dest->scissorRect = This->scissorRect;
203
204     /* Lights */
205     memset(This->activeLights, 0, sizeof(This->activeLights));
206     for(l = 0; l < LIGHTMAP_SIZE; l++) {
207         struct list *e1, *e2;
208         LIST_FOR_EACH_SAFE(e1, e2, &Dest->lightMap[l]) {
209             PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
210             list_remove(&light->entry);
211             HeapFree(GetProcessHeap(), 0, light);
212         }
213
214         LIST_FOR_EACH(e1, &This->lightMap[l]) {
215             PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry), *light2;
216             light2 = HeapAlloc(GetProcessHeap(), 0, sizeof(*light));
217             *light2 = *light;
218             list_add_tail(&Dest->lightMap[l], &light2->entry);
219             if(light2->glIndex != -1) Dest->activeLights[light2->glIndex] = light2;
220         }
221     }
222
223     /* Fixed size arrays */
224     memcpy(Dest->vertexShaderConstantB, This->vertexShaderConstantB, sizeof(BOOL) * MAX_CONST_B);
225     memcpy(Dest->vertexShaderConstantI, This->vertexShaderConstantI, sizeof(INT) * MAX_CONST_I * 4);
226     memcpy(Dest->pixelShaderConstantB, This->pixelShaderConstantB, sizeof(BOOL) * MAX_CONST_B);
227     memcpy(Dest->pixelShaderConstantI, This->pixelShaderConstantI, sizeof(INT) * MAX_CONST_I * 4);
228
229     memcpy(Dest->streamStride, This->streamStride, sizeof(UINT) * MAX_STREAMS);
230     memcpy(Dest->streamOffset, This->streamOffset, sizeof(UINT) * MAX_STREAMS);
231     memcpy(Dest->streamSource, This->streamSource, sizeof(IWineD3DBuffer *) * MAX_STREAMS);
232     memcpy(Dest->streamFreq,   This->streamFreq,   sizeof(UINT) * MAX_STREAMS);
233     memcpy(Dest->streamFlags,  This->streamFlags,  sizeof(UINT) * MAX_STREAMS);
234     memcpy(Dest->transforms,   This->transforms,   sizeof(WINED3DMATRIX) * (HIGHEST_TRANSFORMSTATE + 1));
235     memcpy(Dest->clipplane,    This->clipplane,    sizeof(double) * MAX_CLIPPLANES * 4);
236     memcpy(Dest->renderState,  This->renderState,  sizeof(DWORD) * (WINEHIGHEST_RENDER_STATE + 1));
237     memcpy(Dest->textures,     This->textures,     sizeof(IWineD3DBaseTexture*) * MAX_COMBINED_SAMPLERS);
238     memcpy(Dest->textureState, This->textureState, sizeof(DWORD) * MAX_TEXTURES * (WINED3D_HIGHEST_TEXTURE_STATE + 1));
239     memcpy(Dest->samplerState, This->samplerState, sizeof(DWORD) * MAX_COMBINED_SAMPLERS * (WINED3D_HIGHEST_SAMPLER_STATE + 1));
240
241     /* Dynamically sized arrays */
242     memcpy(Dest->vertexShaderConstantF, This->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
243     memcpy(Dest->pixelShaderConstantF,  This->pixelShaderConstantF,  sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
244 }
245
246 /**********************************************************
247  * IWineD3DStateBlockImpl IUnknown parts follows
248  **********************************************************/
249 static HRESULT  WINAPI IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock *iface,REFIID riid,LPVOID *ppobj)
250 {
251     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
252     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
253     if (IsEqualGUID(riid, &IID_IUnknown)
254         || IsEqualGUID(riid, &IID_IWineD3DBase)
255         || IsEqualGUID(riid, &IID_IWineD3DStateBlock)){
256         IUnknown_AddRef(iface);
257         *ppobj = This;
258         return S_OK;
259     }
260     *ppobj = NULL;
261     return E_NOINTERFACE;
262 }
263
264 static ULONG  WINAPI IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock *iface) {
265     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
266     ULONG refCount = InterlockedIncrement(&This->ref);
267
268     TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
269     return refCount;
270 }
271
272 static ULONG  WINAPI IWineD3DStateBlockImpl_Release(IWineD3DStateBlock *iface) {
273     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
274     ULONG refCount = InterlockedDecrement(&This->ref);
275
276     TRACE("(%p) : Releasing from %d\n", This, refCount + 1);
277
278     if (!refCount) {
279         int counter;
280
281         /* type 0 represents the primary stateblock, so free all the resources */
282         if (This->blockType == WINED3DSBT_INIT) {
283             /* NOTE: according to MSDN: The application is responsible for making sure the texture references are cleared down */
284             for (counter = 0; counter < MAX_COMBINED_SAMPLERS; counter++) {
285                 if (This->textures[counter]) {
286                     /* release our 'internal' hold on the texture */
287                     if(0 != IWineD3DBaseTexture_Release(This->textures[counter])) {
288                         TRACE("Texture still referenced by stateblock, applications has leaked Stage = %u Texture = %p\n", counter, This->textures[counter]);
289                     }
290                 }
291             }
292         }
293
294         for (counter = 0; counter < MAX_STREAMS; counter++) {
295             if(This->streamSource[counter]) {
296                 if (IWineD3DBuffer_Release(This->streamSource[counter]))
297                 {
298                     TRACE("Vertex buffer still referenced by stateblock, applications has leaked Stream %u, buffer %p\n", counter, This->streamSource[counter]);
299                 }
300             }
301         }
302         if(This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
303         if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
304         if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
305
306         for(counter = 0; counter < LIGHTMAP_SIZE; counter++) {
307             struct list *e1, *e2;
308             LIST_FOR_EACH_SAFE(e1, e2, &This->lightMap[counter]) {
309                 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
310                 list_remove(&light->entry);
311                 HeapFree(GetProcessHeap(), 0, light);
312             }
313         }
314
315         HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
316         HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
317         HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
318         HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
319         HeapFree(GetProcessHeap(), 0, This->contained_vs_consts_f);
320         HeapFree(GetProcessHeap(), 0, This->contained_ps_consts_f);
321         HeapFree(GetProcessHeap(), 0, This);
322     }
323     return refCount;
324 }
325
326 /**********************************************************
327  * IWineD3DStateBlockImpl parts follows
328  **********************************************************/
329 static HRESULT  WINAPI IWineD3DStateBlockImpl_GetParent(IWineD3DStateBlock *iface, IUnknown **pParent) {
330     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
331     IUnknown_AddRef(This->parent);
332     *pParent = This->parent;
333     return WINED3D_OK;
334 }
335
336 static HRESULT  WINAPI IWineD3DStateBlockImpl_GetDevice(IWineD3DStateBlock *iface, IWineD3DDevice** ppDevice){
337
338     IWineD3DStateBlockImpl *This   = (IWineD3DStateBlockImpl *)iface;
339
340     *ppDevice = (IWineD3DDevice*)This->wineD3DDevice;
341     IWineD3DDevice_AddRef(*ppDevice);
342     return WINED3D_OK;
343
344 }
345
346 static inline void record_lights(IWineD3DStateBlockImpl *This, IWineD3DStateBlockImpl *targetStateBlock) {
347     UINT i;
348
349     /* Lights... For a recorded state block, we just had a chain of actions to perform,
350      * so we need to walk that chain and update any actions which differ
351      */
352     for(i = 0; i < LIGHTMAP_SIZE; i++) {
353         struct list *e, *f;
354         LIST_FOR_EACH(e, &This->lightMap[i]) {
355             BOOL updated = FALSE;
356             PLIGHTINFOEL *src = LIST_ENTRY(e, PLIGHTINFOEL, entry), *realLight;
357             if (!src->changed && !src->enabledChanged) continue;
358
359             /* Look up the light in the destination */
360             LIST_FOR_EACH(f, &targetStateBlock->lightMap[i]) {
361                 realLight = LIST_ENTRY(f, PLIGHTINFOEL, entry);
362                 if(realLight->OriginalIndex == src->OriginalIndex) {
363                     if(src->changed) {
364                         src->OriginalParms = realLight->OriginalParms;
365                     }
366                     if(src->enabledChanged) {
367                             /* Need to double check because enabledChanged does not catch enabled -> disabled -> enabled
368                         * or disabled -> enabled -> disabled changes
369                             */
370                         if(realLight->glIndex == -1 && src->glIndex != -1) {
371                             /* Light disabled */
372                             This->activeLights[src->glIndex] = NULL;
373                         } else if(realLight->glIndex != -1 && src->glIndex == -1){
374                             /* Light enabled */
375                             This->activeLights[realLight->glIndex] = src;
376                         }
377                         src->glIndex = realLight->glIndex;
378                     }
379                     updated = TRUE;
380                     break;
381                 }
382             }
383
384             if(updated) {
385                 /* Found a light, all done, proceed with next hash entry */
386                 continue;
387             } else if(src->changed) {
388                 /* Otherwise assign defaul params */
389                 src->OriginalParms = WINED3D_default_light;
390             } else {
391                 /* Not enabled by default */
392                 src->glIndex = -1;
393             }
394         }
395     }
396 }
397
398 static HRESULT  WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface){
399
400     IWineD3DStateBlockImpl *This             = (IWineD3DStateBlockImpl *)iface;
401     IWineD3DStateBlockImpl *targetStateBlock = This->wineD3DDevice->stateBlock;
402     unsigned int i, j;
403     DWORD map;
404
405     TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock, This);
406
407     /* If not recorded, then update can just recapture */
408     if (This->blockType == WINED3DSBT_RECORDED) {
409
410         /* Recorded => Only update 'changed' values */
411         if (This->changed.vertexShader && This->vertexShader != targetStateBlock->vertexShader) {
412             TRACE("Updating vertex shader from %p to %p\n", This->vertexShader, targetStateBlock->vertexShader);
413
414             if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
415             if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
416             This->vertexShader = targetStateBlock->vertexShader;
417         }
418
419         /* Vertex Shader Float Constants */
420         for (j = 0; j < This->num_contained_vs_consts_f; ++j) {
421             i = This->contained_vs_consts_f[j];
422             TRACE("Setting %p from %p %u to {%f, %f, %f, %f}\n", This, targetStateBlock, i,
423                     targetStateBlock->vertexShaderConstantF[i * 4],
424                     targetStateBlock->vertexShaderConstantF[i * 4 + 1],
425                     targetStateBlock->vertexShaderConstantF[i * 4 + 2],
426                     targetStateBlock->vertexShaderConstantF[i * 4 + 3]);
427
428             This->vertexShaderConstantF[i * 4]      = targetStateBlock->vertexShaderConstantF[i * 4];
429             This->vertexShaderConstantF[i * 4 + 1]  = targetStateBlock->vertexShaderConstantF[i * 4 + 1];
430             This->vertexShaderConstantF[i * 4 + 2]  = targetStateBlock->vertexShaderConstantF[i * 4 + 2];
431             This->vertexShaderConstantF[i * 4 + 3]  = targetStateBlock->vertexShaderConstantF[i * 4 + 3];
432         }
433
434         /* Vertex Shader Integer Constants */
435         for (j = 0; j < This->num_contained_vs_consts_i; ++j) {
436             i = This->contained_vs_consts_i[j];
437             TRACE("Setting %p from %p %u to {%d, %d, %d, %d}\n", This, targetStateBlock, i,
438                     targetStateBlock->vertexShaderConstantI[i * 4],
439                     targetStateBlock->vertexShaderConstantI[i * 4 + 1],
440                     targetStateBlock->vertexShaderConstantI[i * 4 + 2],
441                     targetStateBlock->vertexShaderConstantI[i * 4 + 3]);
442
443             This->vertexShaderConstantI[i * 4]      = targetStateBlock->vertexShaderConstantI[i * 4];
444             This->vertexShaderConstantI[i * 4 + 1]  = targetStateBlock->vertexShaderConstantI[i * 4 + 1];
445             This->vertexShaderConstantI[i * 4 + 2]  = targetStateBlock->vertexShaderConstantI[i * 4 + 2];
446             This->vertexShaderConstantI[i * 4 + 3]  = targetStateBlock->vertexShaderConstantI[i * 4 + 3];
447         }
448
449         /* Vertex Shader Boolean Constants */
450         for (j = 0; j < This->num_contained_vs_consts_b; ++j) {
451             i = This->contained_vs_consts_b[j];
452             TRACE("Setting %p from %p %u to %s\n", This, targetStateBlock, i,
453                     targetStateBlock->vertexShaderConstantB[i] ? "TRUE" : "FALSE");
454
455             This->vertexShaderConstantB[i] =  targetStateBlock->vertexShaderConstantB[i];
456         }
457
458         /* Pixel Shader Float Constants */
459         for (j = 0; j < This->num_contained_ps_consts_f; ++j) {
460             i = This->contained_ps_consts_f[j];
461             TRACE("Setting %p from %p %u to {%f, %f, %f, %f}\n", This, targetStateBlock, i,
462                     targetStateBlock->pixelShaderConstantF[i * 4],
463                     targetStateBlock->pixelShaderConstantF[i * 4 + 1],
464                     targetStateBlock->pixelShaderConstantF[i * 4 + 2],
465                     targetStateBlock->pixelShaderConstantF[i * 4 + 3]);
466
467             This->pixelShaderConstantF[i * 4]      = targetStateBlock->pixelShaderConstantF[i * 4];
468             This->pixelShaderConstantF[i * 4 + 1]  = targetStateBlock->pixelShaderConstantF[i * 4 + 1];
469             This->pixelShaderConstantF[i * 4 + 2]  = targetStateBlock->pixelShaderConstantF[i * 4 + 2];
470             This->pixelShaderConstantF[i * 4 + 3]  = targetStateBlock->pixelShaderConstantF[i * 4 + 3];
471         }
472
473         /* Pixel Shader Integer Constants */
474         for (j = 0; j < This->num_contained_ps_consts_i; ++j) {
475             i = This->contained_ps_consts_i[j];
476             TRACE("Setting %p from %p %u to {%d, %d, %d, %d}\n", This, targetStateBlock, i,
477                     targetStateBlock->pixelShaderConstantI[i * 4],
478                     targetStateBlock->pixelShaderConstantI[i * 4 + 1],
479                     targetStateBlock->pixelShaderConstantI[i * 4 + 2],
480                     targetStateBlock->pixelShaderConstantI[i * 4 + 3]);
481
482             This->pixelShaderConstantI[i * 4]      = targetStateBlock->pixelShaderConstantI[i * 4];
483             This->pixelShaderConstantI[i * 4 + 1]  = targetStateBlock->pixelShaderConstantI[i * 4 + 1];
484             This->pixelShaderConstantI[i * 4 + 2]  = targetStateBlock->pixelShaderConstantI[i * 4 + 2];
485             This->pixelShaderConstantI[i * 4 + 3]  = targetStateBlock->pixelShaderConstantI[i * 4 + 3];
486         }
487
488         /* Pixel Shader Boolean Constants */
489         for (j = 0; j < This->num_contained_ps_consts_b; ++j) {
490             i = This->contained_ps_consts_b[j];
491             TRACE("Setting %p from %p %u to %s\n", This, targetStateBlock, i,
492                     targetStateBlock->pixelShaderConstantB[i] ? "TRUE" : "FALSE");
493
494             This->pixelShaderConstantB[i] =  targetStateBlock->pixelShaderConstantB[i];
495         }
496
497         /* Others + Render & Texture */
498         for (i = 0; i < This->num_contained_transform_states; i++) {
499             TRACE("Updating transform %u\n", i);
500             This->transforms[This->contained_transform_states[i]] =
501                 targetStateBlock->transforms[This->contained_transform_states[i]];
502         }
503
504         if (This->changed.primitive_type) This->gl_primitive_type = targetStateBlock->gl_primitive_type;
505
506         if (This->changed.indices && ((This->pIndexData != targetStateBlock->pIndexData)
507                         || (This->baseVertexIndex != targetStateBlock->baseVertexIndex)
508                         || (This->IndexFmt != targetStateBlock->IndexFmt))) {
509             TRACE("Updating pIndexData to %p, baseVertexIndex to %d\n",
510                     targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
511             if(targetStateBlock->pIndexData) IWineD3DBuffer_AddRef(targetStateBlock->pIndexData);
512             if(This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
513             This->pIndexData = targetStateBlock->pIndexData;
514             This->baseVertexIndex = targetStateBlock->baseVertexIndex;
515             This->IndexFmt = targetStateBlock->IndexFmt;
516         }
517
518         if(This->changed.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl){
519             TRACE("Updating vertex declaration from %p to %p\n", This->vertexDecl, targetStateBlock->vertexDecl);
520
521             This->vertexDecl = targetStateBlock->vertexDecl;
522         }
523
524         if (This->changed.material && memcmp(&targetStateBlock->material,
525                                                     &This->material,
526                                                     sizeof(WINED3DMATERIAL)) != 0) {
527             TRACE("Updating material\n");
528             This->material = targetStateBlock->material;
529         }
530
531         if (This->changed.viewport && memcmp(&targetStateBlock->viewport,
532                                                     &This->viewport,
533                                                     sizeof(WINED3DVIEWPORT)) != 0) {
534             TRACE("Updating viewport\n");
535             This->viewport = targetStateBlock->viewport;
536         }
537
538         if(This->changed.scissorRect && memcmp(&targetStateBlock->scissorRect,
539                                            &This->scissorRect,
540                                            sizeof(targetStateBlock->scissorRect)))
541         {
542             TRACE("Updating scissor rect\n");
543             targetStateBlock->scissorRect = This->scissorRect;
544         }
545
546         map = This->changed.streamSource;
547         for (i = 0; map; map >>= 1, ++i)
548         {
549             if (!(map & 1)) continue;
550
551             if (This->streamStride[i] != targetStateBlock->streamStride[i]
552                     || This->streamSource[i] != targetStateBlock->streamSource[i])
553             {
554                 TRACE("Updating stream source %u to %p, stride to %u\n",
555                         i, targetStateBlock->streamSource[i], targetStateBlock->streamStride[i]);
556                 This->streamStride[i] = targetStateBlock->streamStride[i];
557                 if (targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
558                 if (This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
559                 This->streamSource[i] = targetStateBlock->streamSource[i];
560             }
561         }
562
563         map = This->changed.streamFreq;
564         for (i = 0; map; map >>= 1, ++i)
565         {
566             if (!(map & 1)) continue;
567
568             if (This->streamFreq[i] != targetStateBlock->streamFreq[i]
569                     || This->streamFlags[i] != targetStateBlock->streamFlags[i])
570             {
571                 TRACE("Updating stream frequency %u to %u flags to %#x\n",
572                         i, targetStateBlock->streamFreq[i], targetStateBlock->streamFlags[i]);
573                 This->streamFreq[i] = targetStateBlock->streamFreq[i];
574                 This->streamFlags[i] = targetStateBlock->streamFlags[i];
575             }
576         }
577
578         map = This->changed.clipplane;
579         for (i = 0; map; map >>= 1, ++i)
580         {
581             if (!(map & 1)) continue;
582
583             if (memcmp(targetStateBlock->clipplane[i], This->clipplane[i], sizeof(*This->clipplane)))
584             {
585                 TRACE("Updating clipplane %u\n", i);
586                 memcpy(This->clipplane[i], targetStateBlock->clipplane[i], sizeof(*This->clipplane));
587             }
588         }
589
590         /* Render */
591         for (i = 0; i < This->num_contained_render_states; i++) {
592             TRACE("Updating renderState %u to %u\n", This->contained_render_states[i],
593                     targetStateBlock->renderState[This->contained_render_states[i]]);
594             This->renderState[This->contained_render_states[i]] = targetStateBlock->renderState[This->contained_render_states[i]];
595         }
596
597         /* Texture states */
598         for (j = 0; j < This->num_contained_tss_states; j++) {
599             DWORD stage = This->contained_tss_states[j].stage;
600             DWORD state = This->contained_tss_states[j].state;
601
602             TRACE("Updating texturestage state %u, %u to %u (was %u)\n", stage, state,
603                     targetStateBlock->textureState[stage][state], This->textureState[stage][state]);
604             This->textureState[stage][state] = targetStateBlock->textureState[stage][state];
605         }
606
607         /* Samplers */
608         /* TODO: move over to using memcpy */
609         map = This->changed.textures;
610         for (i = 0; map; map >>= 1, ++i)
611         {
612             if (!(map & 1)) continue;
613
614             TRACE("Updating texture %u to %p (was %p)\n", i, targetStateBlock->textures[i], This->textures[i]);
615             This->textures[i] = targetStateBlock->textures[i];
616         }
617
618         for (j = 0; j < This->num_contained_sampler_states; j++) {
619             DWORD stage = This->contained_sampler_states[j].stage;
620             DWORD state = This->contained_sampler_states[j].state;
621             TRACE("Updating sampler state %u, %u to %u (was %u)\n", stage, state,
622                     targetStateBlock->samplerState[stage][state], This->samplerState[stage][state]);
623             This->samplerState[stage][state] = targetStateBlock->samplerState[stage][state];
624         }
625         if(This->changed.pixelShader && This->pixelShader != targetStateBlock->pixelShader) {
626             if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
627             if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
628             This->pixelShader = targetStateBlock->pixelShader;
629         }
630
631         record_lights(This, targetStateBlock);
632     } else if(This->blockType == WINED3DSBT_ALL) {
633         This->vertexDecl = targetStateBlock->vertexDecl;
634         memcpy(This->vertexShaderConstantB, targetStateBlock->vertexShaderConstantB, sizeof(This->vertexShaderConstantI));
635         memcpy(This->vertexShaderConstantI, targetStateBlock->vertexShaderConstantI, sizeof(This->vertexShaderConstantF));
636         memcpy(This->vertexShaderConstantF, targetStateBlock->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
637         This->gl_primitive_type = targetStateBlock->gl_primitive_type;
638         memcpy(This->streamStride, targetStateBlock->streamStride, sizeof(This->streamStride));
639         memcpy(This->streamOffset, targetStateBlock->streamOffset, sizeof(This->streamOffset));
640         memcpy(This->streamFreq, targetStateBlock->streamFreq, sizeof(This->streamFreq));
641         memcpy(This->streamFlags, targetStateBlock->streamFlags, sizeof(This->streamFlags));
642         This->baseVertexIndex = targetStateBlock->baseVertexIndex;
643         memcpy(This->transforms, targetStateBlock->transforms, sizeof(This->transforms));
644         record_lights(This, targetStateBlock);
645         memcpy(This->clipplane, targetStateBlock->clipplane, sizeof(This->clipplane));
646         This->clip_status = targetStateBlock->clip_status;
647         This->viewport = targetStateBlock->viewport;
648         This->material = targetStateBlock->material;
649         memcpy(This->pixelShaderConstantB, targetStateBlock->pixelShaderConstantB, sizeof(This->pixelShaderConstantI));
650         memcpy(This->pixelShaderConstantI, targetStateBlock->pixelShaderConstantI, sizeof(This->pixelShaderConstantF));
651         memcpy(This->pixelShaderConstantF, targetStateBlock->pixelShaderConstantF, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
652         memcpy(This->renderState, targetStateBlock->renderState, sizeof(This->renderState));
653         memcpy(This->textures, targetStateBlock->textures, sizeof(This->textures));
654         memcpy(This->textureState, targetStateBlock->textureState, sizeof(This->textureState));
655         memcpy(This->samplerState, targetStateBlock->samplerState, sizeof(This->samplerState));
656         This->scissorRect = targetStateBlock->scissorRect;
657
658         if(targetStateBlock->pIndexData != This->pIndexData ||
659            targetStateBlock->IndexFmt != This->IndexFmt) {
660             if (targetStateBlock->pIndexData) IWineD3DBuffer_AddRef(targetStateBlock->pIndexData);
661             if (This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
662             This->pIndexData = targetStateBlock->pIndexData;
663             This->IndexFmt = targetStateBlock->IndexFmt;
664         }
665         for(i = 0; i < MAX_STREAMS; i++) {
666             if(targetStateBlock->streamSource[i] != This->streamSource[i]) {
667                 if(targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
668                 if(This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
669                 This->streamSource[i] = targetStateBlock->streamSource[i];
670             }
671         }
672         if(This->vertexShader != targetStateBlock->vertexShader) {
673             if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
674             if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
675             This->vertexShader = targetStateBlock->vertexShader;
676         }
677         if(This->pixelShader != targetStateBlock->pixelShader) {
678             if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
679             if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
680             This->pixelShader = targetStateBlock->pixelShader;
681         }
682     } else if(This->blockType == WINED3DSBT_VERTEXSTATE) {
683         memcpy(This->vertexShaderConstantB, targetStateBlock->vertexShaderConstantB, sizeof(This->vertexShaderConstantI));
684         memcpy(This->vertexShaderConstantI, targetStateBlock->vertexShaderConstantI, sizeof(This->vertexShaderConstantF));
685         memcpy(This->vertexShaderConstantF, targetStateBlock->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
686         record_lights(This, targetStateBlock);
687         for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
688             This->renderState[SavedVertexStates_R[i]] = targetStateBlock->renderState[SavedVertexStates_R[i]];
689         }
690         for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
691             for (i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
692                 This->samplerState[j][SavedVertexStates_S[i]] = targetStateBlock->samplerState[j][SavedVertexStates_S[i]];
693             }
694         }
695         for (j = 0; j < MAX_TEXTURES; j++) {
696             for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
697                 This->textureState[j][SavedVertexStates_R[i]] = targetStateBlock->textureState[j][SavedVertexStates_R[i]];
698             }
699         }
700         for(i = 0; i < MAX_STREAMS; i++) {
701             if(targetStateBlock->streamSource[i] != This->streamSource[i]) {
702                 if (targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
703                 if (This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
704                 This->streamSource[i] = targetStateBlock->streamSource[i];
705             }
706         }
707         if(This->vertexShader != targetStateBlock->vertexShader) {
708             if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
709             if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
710             This->vertexShader = targetStateBlock->vertexShader;
711         }
712     } else if(This->blockType == WINED3DSBT_PIXELSTATE) {
713         memcpy(This->pixelShaderConstantB, targetStateBlock->pixelShaderConstantB, sizeof(This->pixelShaderConstantI));
714         memcpy(This->pixelShaderConstantI, targetStateBlock->pixelShaderConstantI, sizeof(This->pixelShaderConstantF));
715         memcpy(This->pixelShaderConstantF, targetStateBlock->pixelShaderConstantF, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
716         for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
717             This->renderState[SavedPixelStates_R[i]] = targetStateBlock->renderState[SavedPixelStates_R[i]];
718         }
719         for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
720             for (i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
721                 This->samplerState[j][SavedPixelStates_S[i]] = targetStateBlock->samplerState[j][SavedPixelStates_S[i]];
722             }
723         }
724         for (j = 0; j < MAX_TEXTURES; j++) {
725             for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
726                 This->textureState[j][SavedPixelStates_R[i]] = targetStateBlock->textureState[j][SavedPixelStates_R[i]];
727             }
728         }
729         if(This->pixelShader != targetStateBlock->pixelShader) {
730             if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
731             if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
732             This->pixelShader = targetStateBlock->pixelShader;
733         }
734     }
735
736     TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
737
738     return WINED3D_OK;
739 }
740
741 static inline void apply_lights(IWineD3DDevice *pDevice, IWineD3DStateBlockImpl *This) {
742     UINT i;
743     for(i = 0; i < LIGHTMAP_SIZE; i++) {
744         struct list *e;
745
746         LIST_FOR_EACH(e, &This->lightMap[i]) {
747             const PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
748
749             if(light->changed) {
750                 IWineD3DDevice_SetLight(pDevice, light->OriginalIndex, &light->OriginalParms);
751             }
752             if(light->enabledChanged) {
753                 IWineD3DDevice_SetLightEnable(pDevice, light->OriginalIndex, light->glIndex != -1);
754             }
755         }
756     }
757 }
758
759 static HRESULT  WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
760     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
761     IWineD3DDevice*        pDevice     = (IWineD3DDevice*)This->wineD3DDevice;
762
763 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
764 should really perform a delta so that only the changes get updated*/
765
766     UINT i;
767     UINT j;
768     DWORD map;
769
770     TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
771
772     TRACE("Blocktype: %d\n", This->blockType);
773
774     if(This->blockType == WINED3DSBT_RECORDED) {
775         if (This->changed.vertexShader) {
776             IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
777         }
778         /* Vertex Shader Constants */
779         for (i = 0; i < This->num_contained_vs_consts_f; i++) {
780             IWineD3DDevice_SetVertexShaderConstantF(pDevice, This->contained_vs_consts_f[i],
781                     This->vertexShaderConstantF + This->contained_vs_consts_f[i] * 4, 1);
782         }
783         for (i = 0; i < This->num_contained_vs_consts_i; i++) {
784             IWineD3DDevice_SetVertexShaderConstantI(pDevice, This->contained_vs_consts_i[i],
785                     This->vertexShaderConstantI + This->contained_vs_consts_i[i] * 4, 1);
786         }
787         for (i = 0; i < This->num_contained_vs_consts_b; i++) {
788             IWineD3DDevice_SetVertexShaderConstantB(pDevice, This->contained_vs_consts_b[i],
789                     This->vertexShaderConstantB + This->contained_vs_consts_b[i], 1);
790         }
791
792         apply_lights(pDevice, This);
793
794         if (This->changed.pixelShader) {
795             IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
796         }
797         /* Pixel Shader Constants */
798         for (i = 0; i < This->num_contained_ps_consts_f; i++) {
799             IWineD3DDevice_SetPixelShaderConstantF(pDevice, This->contained_ps_consts_f[i],
800                     This->pixelShaderConstantF + This->contained_ps_consts_f[i] * 4, 1);
801         }
802         for (i = 0; i < This->num_contained_ps_consts_i; i++) {
803             IWineD3DDevice_SetPixelShaderConstantI(pDevice, This->contained_ps_consts_i[i],
804                     This->pixelShaderConstantI + This->contained_ps_consts_i[i] * 4, 1);
805         }
806         for (i = 0; i < This->num_contained_ps_consts_b; i++) {
807             IWineD3DDevice_SetPixelShaderConstantB(pDevice, This->contained_ps_consts_b[i],
808                     This->pixelShaderConstantB + This->contained_ps_consts_b[i], 1);
809         }
810
811         /* Render */
812         for (i = 0; i <= This->num_contained_render_states; i++) {
813             IWineD3DDevice_SetRenderState(pDevice, This->contained_render_states[i],
814                                           This->renderState[This->contained_render_states[i]]);
815         }
816         /* Texture states */
817         for (i = 0; i < This->num_contained_tss_states; i++) {
818             DWORD stage = This->contained_tss_states[i].stage;
819             DWORD state = This->contained_tss_states[i].state;
820             ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[stage][state]         = This->textureState[stage][state];
821             ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[stage] |= 1 << state;
822             /* TODO: Record a display list to apply all gl states. For now apply by brute force */
823             IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(stage, state));
824         }
825         /* Sampler states */
826         for (i = 0; i < This->num_contained_sampler_states; i++) {
827             DWORD stage = This->contained_sampler_states[i].stage;
828             DWORD state = This->contained_sampler_states[i].state;
829             ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[stage][state]         = This->samplerState[stage][state];
830             ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[stage] |= 1 << state;
831             IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_SAMPLER(stage));
832         }
833
834         for (i = 0; i < This->num_contained_transform_states; i++) {
835             IWineD3DDevice_SetTransform(pDevice, This->contained_transform_states[i],
836                                         &This->transforms[This->contained_transform_states[i]]);
837         }
838
839         if (This->changed.primitive_type)
840         {
841                 This->wineD3DDevice->updateStateBlock->changed.primitive_type = TRUE;
842                 This->wineD3DDevice->updateStateBlock->gl_primitive_type = This->gl_primitive_type;
843         }
844
845         if (This->changed.indices) {
846             IWineD3DDevice_SetIndices(pDevice, This->pIndexData, This->IndexFmt);
847             IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
848         }
849
850         if (This->changed.vertexDecl) {
851             IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
852         }
853
854         if (This->changed.material ) {
855             IWineD3DDevice_SetMaterial(pDevice, &This->material);
856         }
857
858         if (This->changed.viewport) {
859             IWineD3DDevice_SetViewport(pDevice, &This->viewport);
860         }
861
862         if (This->changed.scissorRect) {
863             IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
864         }
865
866         /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
867         map = This->changed.streamSource;
868         for (i = 0; map; map >>= 1, ++i)
869         {
870             if (map & 1) IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
871         }
872
873         map = This->changed.streamFreq;
874         for (i = 0; map; map >>= 1, ++i)
875         {
876             if (map & 1) IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
877         }
878
879         map = This->changed.textures;
880         for (i = 0; map; map >>= 1, ++i)
881         {
882             if (!(map & 1)) continue;
883
884             if (i < MAX_FRAGMENT_SAMPLERS) IWineD3DDevice_SetTexture(pDevice, i, This->textures[i]);
885             else IWineD3DDevice_SetTexture(pDevice, WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS,
886                     This->textures[i]);
887         }
888
889         map = This->changed.clipplane;
890         for (i = 0; map; map >>= 1, ++i)
891         {
892             float clip[4];
893
894             if (!(map & 1)) continue;
895
896             clip[0] = This->clipplane[i][0];
897             clip[1] = This->clipplane[i][1];
898             clip[2] = This->clipplane[i][2];
899             clip[3] = This->clipplane[i][3];
900             IWineD3DDevice_SetClipPlane(pDevice, i, clip);
901         }
902     } else if(This->blockType == WINED3DSBT_VERTEXSTATE) {
903         IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
904         for (i = 0; i < GL_LIMITS(vshader_constantsF); i++) {
905             IWineD3DDevice_SetVertexShaderConstantF(pDevice, i,
906                     This->vertexShaderConstantF + i * 4, 1);
907         }
908         for (i = 0; i < MAX_CONST_I; i++) {
909             IWineD3DDevice_SetVertexShaderConstantI(pDevice, i,
910                     This->vertexShaderConstantI + i * 4, 1);
911         }
912         for (i = 0; i < MAX_CONST_B; i++) {
913             IWineD3DDevice_SetVertexShaderConstantB(pDevice, i,
914                     This->vertexShaderConstantB + i, 1);
915         }
916
917         apply_lights(pDevice, This);
918
919         for(i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
920             IWineD3DDevice_SetRenderState(pDevice, SavedVertexStates_R[i], This->renderState[SavedVertexStates_R[i]]);
921         }
922         for(j = 0; j < MAX_TEXTURES; j++) {
923             for(i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
924                 IWineD3DDevice_SetTextureStageState(pDevice, j, SavedVertexStates_T[i],
925                         This->textureState[j][SavedVertexStates_T[i]]);
926             }
927         }
928
929         for(j = 0; j < MAX_FRAGMENT_SAMPLERS; j++) {
930             for(i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
931                 IWineD3DDevice_SetSamplerState(pDevice, j, SavedVertexStates_S[i],
932                                                This->samplerState[j][SavedVertexStates_S[i]]);
933             }
934         }
935         for(j = MAX_FRAGMENT_SAMPLERS; j < MAX_COMBINED_SAMPLERS; j++) {
936             for(i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
937                 IWineD3DDevice_SetSamplerState(pDevice,
938                                                WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS,
939                                                SavedVertexStates_S[i],
940                                                This->samplerState[j][SavedVertexStates_S[i]]);
941             }
942         }
943     } else if(This->blockType == WINED3DSBT_PIXELSTATE) {
944         IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
945         for (i = 0; i < GL_LIMITS(pshader_constantsF); i++) {
946             IWineD3DDevice_SetPixelShaderConstantF(pDevice, i,
947                     This->pixelShaderConstantF + i * 4, 1);
948         }
949         for (i = 0; i < MAX_CONST_I; i++) {
950             IWineD3DDevice_SetPixelShaderConstantI(pDevice, i,
951                     This->pixelShaderConstantI + i * 4, 1);
952         }
953         for (i = 0; i < MAX_CONST_B; i++) {
954             IWineD3DDevice_SetPixelShaderConstantB(pDevice, i,
955                     This->pixelShaderConstantB + i, 1);
956         }
957
958         for(i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
959             IWineD3DDevice_SetRenderState(pDevice, SavedPixelStates_R[i], This->renderState[SavedPixelStates_R[i]]);
960         }
961         for(j = 0; j < MAX_TEXTURES; j++) {
962             for(i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
963                 IWineD3DDevice_SetTextureStageState(pDevice, j, SavedPixelStates_T[i],
964                         This->textureState[j][SavedPixelStates_T[i]]);
965             }
966         }
967
968         for(j = 0; j < MAX_FRAGMENT_SAMPLERS; j++) {
969             for(i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
970                 IWineD3DDevice_SetSamplerState(pDevice, j, SavedPixelStates_S[i],
971                                                This->samplerState[j][SavedPixelStates_S[i]]);
972             }
973         }
974         for(j = MAX_FRAGMENT_SAMPLERS; j < MAX_COMBINED_SAMPLERS; j++) {
975             for(i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
976                 IWineD3DDevice_SetSamplerState(pDevice,
977                                                WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS,
978                                                SavedPixelStates_S[i],
979                                                This->samplerState[j][SavedPixelStates_S[i]]);
980             }
981         }
982     } else if(This->blockType == WINED3DSBT_ALL) {
983         IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
984         for (i = 0; i < GL_LIMITS(vshader_constantsF); i++) {
985             IWineD3DDevice_SetVertexShaderConstantF(pDevice, i,
986                     This->vertexShaderConstantF + i * 4, 1);
987         }
988         for (i = 0; i < MAX_CONST_I; i++) {
989             IWineD3DDevice_SetVertexShaderConstantI(pDevice, i,
990                     This->vertexShaderConstantI + i * 4, 1);
991         }
992         for (i = 0; i < MAX_CONST_B; i++) {
993             IWineD3DDevice_SetVertexShaderConstantB(pDevice, i,
994                     This->vertexShaderConstantB + i, 1);
995         }
996
997         IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
998         for (i = 0; i < GL_LIMITS(pshader_constantsF); i++) {
999             IWineD3DDevice_SetPixelShaderConstantF(pDevice, i,
1000                     This->pixelShaderConstantF + i * 4, 1);
1001         }
1002         for (i = 0; i < MAX_CONST_I; i++) {
1003             IWineD3DDevice_SetPixelShaderConstantI(pDevice, i,
1004                     This->pixelShaderConstantI + i * 4, 1);
1005         }
1006         for (i = 0; i < MAX_CONST_B; i++) {
1007             IWineD3DDevice_SetPixelShaderConstantB(pDevice, i,
1008                     This->pixelShaderConstantB + i, 1);
1009         }
1010
1011         apply_lights(pDevice, This);
1012
1013         for(i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
1014             IWineD3DDevice_SetRenderState(pDevice, i, This->renderState[i]);
1015         }
1016         for(j = 0; j < MAX_TEXTURES; j++) {
1017             for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
1018             {
1019                 IWineD3DDevice_SetTextureStageState(pDevice, j, i, This->textureState[j][i]);
1020             }
1021         }
1022
1023         /* Skip unused values between TEXTURE8 and WORLD0 ? */
1024         for(i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
1025             IWineD3DDevice_SetTransform(pDevice, i, &This->transforms[i]);
1026         }
1027         This->wineD3DDevice->updateStateBlock->gl_primitive_type = This->gl_primitive_type;
1028         IWineD3DDevice_SetIndices(pDevice, This->pIndexData, This->IndexFmt);
1029         IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
1030         IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
1031         IWineD3DDevice_SetMaterial(pDevice, &This->material);
1032         IWineD3DDevice_SetViewport(pDevice, &This->viewport);
1033         IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
1034
1035         /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
1036         for (i=0; i<MAX_STREAMS; i++) {
1037             IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
1038             IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
1039         }
1040         for (j = 0 ; j < MAX_COMBINED_SAMPLERS; j++){
1041             UINT sampler = j < MAX_FRAGMENT_SAMPLERS ? j : WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS;
1042
1043             IWineD3DDevice_SetTexture(pDevice, sampler, This->textures[j]);
1044             for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; ++i)
1045             {
1046                 IWineD3DDevice_SetSamplerState(pDevice, sampler, i, This->samplerState[j][i]);
1047             }
1048         }
1049         for (i = 0; i < GL_LIMITS(clipplanes); i++) {
1050             float clip[4];
1051
1052             clip[0] = This->clipplane[i][0];
1053             clip[1] = This->clipplane[i][1];
1054             clip[2] = This->clipplane[i][2];
1055             clip[3] = This->clipplane[i][3];
1056             IWineD3DDevice_SetClipPlane(pDevice, i, clip);
1057         }
1058     }
1059
1060     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = MAX_TEXTURES - 1;
1061     for(j = 0; j < MAX_TEXTURES - 1; j++) {
1062         if(((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE) {
1063             ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = j;
1064             break;
1065         }
1066     }
1067     TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
1068
1069     return WINED3D_OK;
1070 }
1071
1072 static HRESULT  WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
1073     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
1074     IWineD3DDevice         *device = (IWineD3DDevice *)This->wineD3DDevice;
1075     IWineD3DDeviceImpl     *ThisDevice = (IWineD3DDeviceImpl *)device;
1076     union {
1077         WINED3DLINEPATTERN lp;
1078         DWORD d;
1079     } lp;
1080     union {
1081         float f;
1082         DWORD d;
1083     } tmpfloat;
1084     unsigned int i;
1085     IWineD3DSwapChain *swapchain;
1086     IWineD3DSurface *backbuffer;
1087     HRESULT hr;
1088
1089     /* Note this may have a large overhead but it should only be executed
1090        once, in order to initialize the complete state of the device and
1091        all opengl equivalents                                            */
1092     TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
1093     /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
1094     This->blockType = WINED3DSBT_INIT;
1095
1096     /* Set some of the defaults for lights, transforms etc */
1097     memcpy(&This->transforms[WINED3DTS_PROJECTION], identity, sizeof(identity));
1098     memcpy(&This->transforms[WINED3DTS_VIEW], identity, sizeof(identity));
1099     for (i = 0; i < 256; ++i) {
1100       memcpy(&This->transforms[WINED3DTS_WORLDMATRIX(i)], identity, sizeof(identity));
1101     }
1102
1103     TRACE("Render states\n");
1104     /* Render states: */
1105     if (ThisDevice->auto_depth_stencil_buffer != NULL) {
1106        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       WINED3DZB_TRUE);
1107     } else {
1108        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       WINED3DZB_FALSE);
1109     }
1110     IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE,         WINED3DFILL_SOLID);
1111     IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE,        WINED3DSHADE_GOURAUD);
1112     lp.lp.wRepeatFactor = 0;
1113     lp.lp.wLinePattern  = 0;
1114     IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN,      lp.d);
1115     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE,     TRUE);
1116     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE,  FALSE);
1117     IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL,        TRUE);
1118     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND,         WINED3DBLEND_ONE);
1119     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND,        WINED3DBLEND_ZERO);
1120     IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE,         WINED3DCULL_CCW);
1121     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC,            WINED3DCMP_LESSEQUAL);
1122     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC,        WINED3DCMP_ALWAYS);
1123     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF,         0);
1124     IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE,     FALSE);
1125     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
1126     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE,        FALSE);
1127     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE,   FALSE);
1128     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE,         0);
1129     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR,         0);
1130     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE,     WINED3DFOG_NONE);
1131     tmpfloat.f = 0.0f;
1132     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART,         tmpfloat.d);
1133     tmpfloat.f = 1.0f;
1134     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND,           tmpfloat.d);
1135     tmpfloat.f = 1.0f;
1136     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY,       tmpfloat.d);
1137     IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS,    FALSE);
1138     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS,            0);
1139     IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE,   FALSE);
1140     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE,    FALSE);
1141     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL,      WINED3DSTENCILOP_KEEP);
1142     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL,     WINED3DSTENCILOP_KEEP);
1143     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS,      WINED3DSTENCILOP_KEEP);
1144     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF,       0);
1145     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK,      0xFFFFFFFF);
1146     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC,      WINED3DCMP_ALWAYS);
1147     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
1148     IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR,    0xFFFFFFFF);
1149     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
1150     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
1151     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
1152     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
1153     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
1154     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
1155     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
1156     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
1157     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING,                 TRUE);
1158     IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING,                 TRUE);
1159     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT,                  0);
1160     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE,            WINED3DFOG_NONE);
1161     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX,              TRUE);
1162     IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER,              TRUE);
1163     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS,         FALSE);
1164     IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE,    WINED3DMCS_COLOR1);
1165     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE,   WINED3DMCS_COLOR2);
1166     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE,    WINED3DMCS_MATERIAL);
1167     IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE,   WINED3DMCS_MATERIAL);
1168     IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND,              WINED3DVBF_DISABLE);
1169     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE,          0);
1170     IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
1171     tmpfloat.f = 1.0f;
1172     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE,                tmpfloat.d);
1173     tmpfloat.f = ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion < 9 ? 0.0f : 1.0f;
1174     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN,            tmpfloat.d);
1175     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE,        FALSE);
1176     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE,         FALSE);
1177     tmpfloat.f = 1.0f;
1178     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A,             tmpfloat.d);
1179     tmpfloat.f = 0.0f;
1180     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B,             tmpfloat.d);
1181     tmpfloat.f = 0.0f;
1182     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C,             tmpfloat.d);
1183     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS,     TRUE);
1184     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK,          0xFFFFFFFF);
1185     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE,           WINED3DPATCHEDGE_DISCRETE);
1186     tmpfloat.f = 1.0f;
1187     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS,            tmpfloat.d);
1188     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN,        0xbaadcafe);
1189     tmpfloat.f = GL_LIMITS(pointsize);
1190     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX,            tmpfloat.d);
1191     IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
1192     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE,         0x0000000F);
1193     tmpfloat.f = 0.0f;
1194     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR,              tmpfloat.d);
1195     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP,                  WINED3DBLENDOP_ADD);
1196     IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE,           WINED3DDEGREE_CUBIC);
1197     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE,             WINED3DDEGREE_LINEAR);
1198     /* states new in d3d9 */
1199     IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE,        FALSE);
1200     IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS,      0);
1201     tmpfloat.f = 1.0f;
1202     IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL,     tmpfloat.d);
1203     IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL,     tmpfloat.d);
1204     IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE,    FALSE);
1205     tmpfloat.f = 0.0f;
1206     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X,           tmpfloat.d);
1207     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y,           tmpfloat.d);
1208     tmpfloat.f = 1.0f;
1209     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z,           tmpfloat.d);
1210     tmpfloat.f = 0.0f;
1211     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W,           tmpfloat.d);
1212     IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
1213     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE,      FALSE);
1214     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL,          WINED3DSTENCILOP_KEEP);
1215     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL,         WINED3DSTENCILOP_KEEP);
1216     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS,          WINED3DSTENCILOP_KEEP);
1217     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC,          WINED3DCMP_ALWAYS);
1218     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1,        0x0000000F);
1219     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2,        0x0000000F);
1220     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3,        0x0000000F);
1221     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR,              0xFFFFFFFF);
1222     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE,          0);
1223     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS,                0);
1224     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8,  0);
1225     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9,  0);
1226     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
1227     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
1228     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
1229     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
1230     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
1231     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
1232     IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1233     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA,            WINED3DBLEND_ONE);
1234     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA,           WINED3DBLEND_ZERO);
1235     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA,             WINED3DBLENDOP_ADD);
1236
1237     /* clipping status */
1238     This->clip_status.ClipUnion = 0;
1239     This->clip_status.ClipIntersection = 0xFFFFFFFF;
1240
1241     /* Texture Stage States - Put directly into state block, we will call function below */
1242     for (i = 0; i < MAX_TEXTURES; i++) {
1243         TRACE("Setting up default texture states for texture Stage %d\n", i);
1244         memcpy(&This->transforms[WINED3DTS_TEXTURE0 + i], identity, sizeof(identity));
1245         This->textureState[i][WINED3DTSS_COLOROP               ] = (i==0)? WINED3DTOP_MODULATE :  WINED3DTOP_DISABLE;
1246         This->textureState[i][WINED3DTSS_COLORARG1             ] = WINED3DTA_TEXTURE;
1247         This->textureState[i][WINED3DTSS_COLORARG2             ] = WINED3DTA_CURRENT;
1248         This->textureState[i][WINED3DTSS_ALPHAOP               ] = (i==0)? WINED3DTOP_SELECTARG1 :  WINED3DTOP_DISABLE;
1249         This->textureState[i][WINED3DTSS_ALPHAARG1             ] = WINED3DTA_TEXTURE;
1250         This->textureState[i][WINED3DTSS_ALPHAARG2             ] = WINED3DTA_CURRENT;
1251         This->textureState[i][WINED3DTSS_BUMPENVMAT00          ] = 0;
1252         This->textureState[i][WINED3DTSS_BUMPENVMAT01          ] = 0;
1253         This->textureState[i][WINED3DTSS_BUMPENVMAT10          ] = 0;
1254         This->textureState[i][WINED3DTSS_BUMPENVMAT11          ] = 0;
1255         This->textureState[i][WINED3DTSS_TEXCOORDINDEX         ] = i;
1256         This->textureState[i][WINED3DTSS_BUMPENVLSCALE         ] = 0;
1257         This->textureState[i][WINED3DTSS_BUMPENVLOFFSET        ] = 0;
1258         This->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS ] = WINED3DTTFF_DISABLE;
1259         This->textureState[i][WINED3DTSS_COLORARG0             ] = WINED3DTA_CURRENT;
1260         This->textureState[i][WINED3DTSS_ALPHAARG0             ] = WINED3DTA_CURRENT;
1261         This->textureState[i][WINED3DTSS_RESULTARG             ] = WINED3DTA_CURRENT;
1262     }
1263     This->lowest_disabled_stage = 1;
1264
1265         /* Sampler states*/
1266     for (i = 0 ; i <  MAX_COMBINED_SAMPLERS; i++) {
1267         TRACE("Setting up default samplers states for sampler %d\n", i);
1268         This->samplerState[i][WINED3DSAMP_ADDRESSU         ] = WINED3DTADDRESS_WRAP;
1269         This->samplerState[i][WINED3DSAMP_ADDRESSV         ] = WINED3DTADDRESS_WRAP;
1270         This->samplerState[i][WINED3DSAMP_ADDRESSW         ] = WINED3DTADDRESS_WRAP;
1271         This->samplerState[i][WINED3DSAMP_BORDERCOLOR      ] = 0x00;
1272         This->samplerState[i][WINED3DSAMP_MAGFILTER        ] = WINED3DTEXF_POINT;
1273         This->samplerState[i][WINED3DSAMP_MINFILTER        ] = WINED3DTEXF_POINT;
1274         This->samplerState[i][WINED3DSAMP_MIPFILTER        ] = WINED3DTEXF_NONE;
1275         This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS    ] = 0;
1276         This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL      ] = 0;
1277         This->samplerState[i][WINED3DSAMP_MAXANISOTROPY    ] = 1;
1278         This->samplerState[i][WINED3DSAMP_SRGBTEXTURE      ] = 0;
1279         This->samplerState[i][WINED3DSAMP_ELEMENTINDEX     ] = 0; /* TODO: Indicates which element of a  multielement texture to use */
1280         This->samplerState[i][WINED3DSAMP_DMAPOFFSET       ] = 0; /* TODO: Vertex offset in the presampled displacement map */
1281     }
1282
1283     for(i = 0; i < GL_LIMITS(textures); i++) {
1284         /* Note: This avoids calling SetTexture, so pretend it has been called */
1285         This->changed.textures |= 1 << i;
1286         This->textures[i]         = NULL;
1287     }
1288
1289     /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1290     hr = IWineD3DDevice_GetSwapChain(device, 0, &swapchain);
1291     if( hr == WINED3D_OK && swapchain != NULL) {
1292         WINED3DVIEWPORT vp;
1293
1294         hr = IWineD3DSwapChain_GetBackBuffer(swapchain, 0, WINED3DBACKBUFFER_TYPE_MONO, &backbuffer);
1295         if (SUCCEEDED(hr) && backbuffer)
1296         {
1297             WINED3DSURFACE_DESC desc;
1298             RECT scissorrect;
1299
1300             IWineD3DSurface_GetDesc(backbuffer, &desc);
1301             IWineD3DSurface_Release(backbuffer);
1302
1303             /* Set the default scissor rect values */
1304             scissorrect.left = 0;
1305             scissorrect.right = desc.width;
1306             scissorrect.top = 0;
1307             scissorrect.bottom = desc.height;
1308             hr = IWineD3DDevice_SetScissorRect(device, &scissorrect);
1309             if (FAILED(hr)) ERR("This should never happen, expect rendering issues!\n");
1310         }
1311
1312         /* Set the default viewport */
1313         vp.X      = 0;
1314         vp.Y      = 0;
1315         vp.Width  = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferWidth;
1316         vp.Height = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferHeight;
1317         vp.MinZ   = 0.0f;
1318         vp.MaxZ   = 1.0f;
1319         IWineD3DDevice_SetViewport(device, &vp);
1320
1321         IWineD3DSwapChain_Release(swapchain);
1322     }
1323
1324     TRACE("-----------------------> Device defaults now set up...\n");
1325     return WINED3D_OK;
1326 }
1327
1328 /**********************************************************
1329  * IWineD3DStateBlock VTbl follows
1330  **********************************************************/
1331
1332 const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
1333 {
1334     /* IUnknown */
1335     IWineD3DStateBlockImpl_QueryInterface,
1336     IWineD3DStateBlockImpl_AddRef,
1337     IWineD3DStateBlockImpl_Release,
1338     /* IWineD3DStateBlock */
1339     IWineD3DStateBlockImpl_GetParent,
1340     IWineD3DStateBlockImpl_GetDevice,
1341     IWineD3DStateBlockImpl_Capture,
1342     IWineD3DStateBlockImpl_Apply,
1343     IWineD3DStateBlockImpl_InitStartupStateBlock
1344 };