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