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