wined3d: Color keying emulation.
[wine] / dlls / wined3d / stateblock.c
1 /*
2  * state block implementation
3  *
4  * Copyright 2002 Raphael Junqueira
5  * Copyright 2004 Jason Edmeades
6  * Copyright 2005 Oliver Stieber
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->wineD3DDevice)->wineD3D))->gl_info
28
29 /**********************************************************
30  * IWineD3DStateBlockImpl IUnknown parts follows
31  **********************************************************/
32 HRESULT WINAPI IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock *iface,REFIID riid,LPVOID *ppobj)
33 {
34     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
35     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
36     if (IsEqualGUID(riid, &IID_IUnknown)
37         || IsEqualGUID(riid, &IID_IWineD3DBase)
38         || IsEqualGUID(riid, &IID_IWineD3DStateBlock)){
39         IUnknown_AddRef(iface);
40         *ppobj = This;
41         return S_OK;
42     }
43     *ppobj = NULL;
44     return E_NOINTERFACE;
45 }
46
47 ULONG WINAPI IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock *iface) {
48     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
49     ULONG refCount = InterlockedIncrement(&This->ref);
50
51     TRACE("(%p) : AddRef increasing from %ld\n", This, refCount - 1);
52     return refCount;
53 }
54
55 ULONG WINAPI IWineD3DStateBlockImpl_Release(IWineD3DStateBlock *iface) {
56     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
57     ULONG refCount = InterlockedDecrement(&This->ref);
58
59     TRACE("(%p) : Releasing from %ld\n", This, refCount + 1);
60
61     if (!refCount) {
62         /* type 0 represents the primary stateblock, so free all the resources */
63         if (This->blockType == WINED3DSBT_INIT) {
64             int counter;
65             FIXME("Releasing primary stateblock\n");
66             /* Free any streams still bound */
67             for (counter = 0 ; counter < MAX_STREAMS ; counter++) {
68                 if (This->streamSource[counter] != NULL) {
69                     IWineD3DVertexBuffer_Release(This->streamSource[counter]);
70                     This->streamSource[counter] = NULL;
71                 }
72             }
73
74             /* free any index data */
75             if (This->pIndexData) {
76                 IWineD3DIndexBuffer_Release(This->pIndexData);
77                 This->pIndexData = NULL;
78             }
79
80             if (NULL != This->pixelShader) {
81                 IWineD3DPixelShader_Release(This->pixelShader);
82             }
83
84             if (NULL != This->vertexShader) {
85                 IWineD3DVertexShader_Release(This->vertexShader);
86             }
87
88             if (NULL != This->vertexDecl) {
89                 IWineD3DVertexDeclaration_Release(This->vertexDecl);
90             }
91
92             /* NOTE: according to MSDN: The applicaion is responsible for making sure the texture references are cleared down */
93             for (counter = 0; counter < GL_LIMITS(textures); counter++) {
94                 if (This->textures[counter]) {
95                     /* release our 'internal' hold on the texture */
96                     if(0 != IWineD3DBaseTexture_Release(This->textures[counter])) {
97                         TRACE("Texture still referenced by stateblock, applications has leaked Stage = %u Texture = %p\n", counter, This->textures[counter]);
98                     }
99                 }
100             }
101
102         }
103         HeapFree(GetProcessHeap(), 0, This);
104     }
105     return refCount;
106 }
107
108 /**********************************************************
109  * IWineD3DStateBlockImpl parts follows
110  **********************************************************/
111 HRESULT WINAPI IWineD3DStateBlockImpl_GetParent(IWineD3DStateBlock *iface, IUnknown **pParent) {
112     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
113     IUnknown_AddRef(This->parent);
114     *pParent = This->parent;
115     return WINED3D_OK;
116 }
117
118 HRESULT WINAPI IWineD3DStateBlockImpl_GetDevice(IWineD3DStateBlock *iface, IWineD3DDevice** ppDevice){
119
120     IWineD3DStateBlockImpl *This   = (IWineD3DStateBlockImpl *)iface;
121
122     *ppDevice = (IWineD3DDevice*)This->wineD3DDevice;
123     IWineD3DDevice_AddRef(*ppDevice);
124     return WINED3D_OK;
125
126 }
127
128 HRESULT WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface){
129
130     IWineD3DStateBlockImpl *This             = (IWineD3DStateBlockImpl *)iface;
131     IWineD3DStateBlockImpl *targetStateBlock = This->wineD3DDevice->stateBlock;
132
133     TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock, This);
134
135     /* If not recorded, then update can just recapture */
136     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED  */ 0) {
137         IWineD3DStateBlockImpl* tmpBlock;
138         PLIGHTINFOEL *tmp = This->lights;
139
140         IWineD3DDevice_CreateStateBlock((IWineD3DDevice *)This->wineD3DDevice, This->blockType, (IWineD3DStateBlock**) &tmpBlock, NULL/*parent*/);
141
142         /* Note just swap the light chains over so when deleting, the old one goes */
143         memcpy(This, tmpBlock, sizeof(IWineD3DStateBlockImpl));
144         tmpBlock->lights = tmp;
145
146         /* Delete the temporary one (which points to the old light chain though */
147         IWineD3DStateBlock_Release((IWineD3DStateBlock *)tmpBlock);
148         /*IDirect3DDevice_DeleteStateBlock(pDevice, tmpBlock);*/
149
150     } else {
151         unsigned int i, j;
152
153         PLIGHTINFOEL *src;
154
155         /* Recorded => Only update 'changed' values */
156         if (This->vertexShader != targetStateBlock->vertexShader) {
157             TRACE("Updating vertex shader from %p to %p\n", This->vertexShader, targetStateBlock->vertexShader);
158
159             if (targetStateBlock->vertexShader) {
160                 IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
161             }
162             if (This->vertexShader) {
163                 IWineD3DVertexShader_Release(This->vertexShader);
164             }
165
166             This->vertexShader = targetStateBlock->vertexShader;
167         }
168
169         /* Vertex Shader Constants */
170         for (i = 0; i < MAX_VSHADER_CONSTANTS; ++i) {
171             if (This->set.vertexShaderConstants[i]) {
172                 TRACE("Setting %p from %p %d to %f\n", This, targetStateBlock, i,  targetStateBlock->vertexShaderConstantF[i * 4 + 1]);
173                 This->vertexShaderConstantB[i] = targetStateBlock->vertexShaderConstantB[i];
174                 This->vertexShaderConstantF[i * 4]      = targetStateBlock->vertexShaderConstantF[i * 4];
175                 This->vertexShaderConstantF[i * 4 + 1]  = targetStateBlock->vertexShaderConstantF[i * 4 + 1];
176                 This->vertexShaderConstantF[i * 4 + 2]  = targetStateBlock->vertexShaderConstantF[i * 4 + 2];
177                 This->vertexShaderConstantF[i * 4 + 3]  = targetStateBlock->vertexShaderConstantF[i * 4 + 3];
178                 This->vertexShaderConstantI[i * 4]      = targetStateBlock->vertexShaderConstantI[i * 4];
179                 This->vertexShaderConstantI[i * 4 + 1]  = targetStateBlock->vertexShaderConstantI[i * 4 + 1];
180                 This->vertexShaderConstantI[i * 4 + 2]  = targetStateBlock->vertexShaderConstantI[i * 4 + 2];
181                 This->vertexShaderConstantI[i * 4 + 3]  = targetStateBlock->vertexShaderConstantI[i * 4 + 3];
182                 This->vertexShaderConstantT[i]          = targetStateBlock->vertexShaderConstantT[i];
183             }
184         }
185
186         /* Lights... For a recorded state block, we just had a chain of actions to perform,
187              so we need to walk that chain and update any actions which differ */
188         src = This->lights;
189         while (src != NULL) {
190             PLIGHTINFOEL *realLight = NULL;
191
192             /* Locate the light in the live lights */
193             realLight = targetStateBlock->lights;
194             while (realLight != NULL && realLight->OriginalIndex != src->OriginalIndex) realLight = realLight->next;
195
196             if (realLight == NULL) {
197                 FIXME("A captured light no longer exists...?\n");
198             } else {
199
200                 /* If 'changed' then its a SetLight command. Rather than comparing to see
201                      if the OriginalParms have changed and then copy them (twice through
202                      memory) just do the copy                                              */
203                 if (src->changed) {
204                     TRACE("Updating lights for light %ld\n", src->OriginalIndex);
205                     memcpy(&src->OriginalParms, &realLight->OriginalParms, sizeof(src->OriginalParms));
206                 }
207
208                 /* If 'enabledchanged' then its a LightEnable command */
209                 if (src->enabledChanged) {
210                     TRACE("Updating lightEnabled for light %ld\n", src->OriginalIndex);
211                     src->lightEnabled = realLight->lightEnabled;
212                 }
213
214             }
215
216             src = src->next;
217         }
218
219         /* Recorded => Only update 'changed' values */
220         if (This->pixelShader != targetStateBlock->pixelShader) {
221             TRACE("Updating pixel shader from %p to %p\n", This->pixelShader, targetStateBlock->pixelShader);
222
223             if (targetStateBlock->pixelShader) {
224                 IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
225             }
226             if (This->pixelShader) {
227                 IWineD3DPixelShader_Release(This->pixelShader);
228             }
229
230             This->pixelShader = targetStateBlock->pixelShader;
231         }
232
233         /* Pixel Shader Constants */
234         for (i = 0; i < MAX_PSHADER_CONSTANTS; ++i) {
235             if (This->set.pixelShaderConstants[i]) {
236                 TRACE("Setting %p from %p %d to %f\n", This, targetStateBlock, i,  targetStateBlock->pixelShaderConstantF[i * 4 + 1]);
237                 This->pixelShaderConstantB[i] = targetStateBlock->pixelShaderConstantB[i];
238                 This->pixelShaderConstantF[i * 4]      = targetStateBlock->pixelShaderConstantF[i * 4];
239                 This->pixelShaderConstantF[i * 4 + 1]  = targetStateBlock->pixelShaderConstantF[i * 4 + 1];
240                 This->pixelShaderConstantF[i * 4 + 2]  = targetStateBlock->pixelShaderConstantF[i * 4 + 2];
241                 This->pixelShaderConstantF[i * 4 + 3]  = targetStateBlock->pixelShaderConstantF[i * 4 + 3];
242                 This->pixelShaderConstantI[i * 4]      = targetStateBlock->pixelShaderConstantI[i * 4];
243                 This->pixelShaderConstantI[i * 4 + 1]  = targetStateBlock->pixelShaderConstantI[i * 4 + 1];
244                 This->pixelShaderConstantI[i * 4 + 2]  = targetStateBlock->pixelShaderConstantI[i * 4 + 2];
245                 This->pixelShaderConstantI[i * 4 + 3]  = targetStateBlock->pixelShaderConstantI[i * 4 + 3];
246                 This->pixelShaderConstantT[i]          = targetStateBlock->pixelShaderConstantT[i];
247             }
248         }
249
250         /* Others + Render & Texture */
251         for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
252             if (This->set.transform[i] && memcmp(&targetStateBlock->transforms[i],
253                                     &This->transforms[i],
254                                     sizeof(D3DMATRIX)) != 0) {
255                 TRACE("Updating transform %d\n", i);
256                 memcpy(&This->transforms[i], &targetStateBlock->transforms[i], sizeof(D3DMATRIX));
257             }
258         }
259
260         if (This->set.indices && ((This->pIndexData != targetStateBlock->pIndexData)
261                         || (This->baseVertexIndex != targetStateBlock->baseVertexIndex))) {
262             TRACE("Updating pindexData to %p, baseVertexIndex to %d\n",
263             targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
264             This->pIndexData = targetStateBlock->pIndexData;
265             This->baseVertexIndex = targetStateBlock->baseVertexIndex;
266         }
267
268         if(This->set.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl){
269             TRACE("Updating vertex declaration from %p to %p\n", This->vertexDecl, targetStateBlock->vertexDecl);
270
271             if (targetStateBlock->vertexDecl) {
272                 IWineD3DVertexDeclaration_AddRef(targetStateBlock->vertexDecl);
273             }
274             if (This->vertexDecl) {
275                 IWineD3DVertexDeclaration_Release(This->vertexDecl);
276             }
277
278             This->vertexDecl = targetStateBlock->vertexDecl;
279         }
280
281         if(This->set.fvf && This->fvf != targetStateBlock->fvf){
282             This->fvf = targetStateBlock->fvf;
283         }
284
285         if (This->set.material && memcmp(&targetStateBlock->material,
286                                                     &This->material,
287                                                     sizeof(D3DMATERIAL9)) != 0) {
288             TRACE("Updating material\n");
289             memcpy(&This->material, &targetStateBlock->material, sizeof(D3DMATERIAL9));
290         }
291
292         if (This->set.viewport && memcmp(&targetStateBlock->viewport,
293                                                     &This->viewport,
294                                                     sizeof(D3DVIEWPORT9)) != 0) {
295             TRACE("Updating viewport\n");
296             memcpy(&This->viewport, &targetStateBlock->viewport, sizeof(D3DVIEWPORT9));
297         }
298
299         for (i = 0; i < MAX_STREAMS; i++) {
300             if (This->set.streamSource[i] &&
301                             ((This->streamStride[i] != targetStateBlock->streamStride[i]) ||
302                             (This->streamSource[i] != targetStateBlock->streamSource[i]))) {
303                 TRACE("Updating stream source %d to %p, stride to %d\n", i, targetStateBlock->streamSource[i],
304                                                                             targetStateBlock->streamStride[i]);
305                 This->streamStride[i] = targetStateBlock->streamStride[i];
306                 This->streamSource[i] = targetStateBlock->streamSource[i];
307             }
308
309             if (This->set.streamFreq[i] &&
310             (This->streamFreq[i] != targetStateBlock->streamFreq[i]
311             || This->streamFlags[i] != targetStateBlock->streamFlags[i])){
312                     TRACE("Updating stream frequency %d to %d flags to %d\n", i ,  targetStateBlock->streamFreq[i] ,
313                                                                                    targetStateBlock->streamFlags[i]);
314                     This->streamFreq[i]  =  targetStateBlock->streamFreq[i];
315                     This->streamFlags[i] =  targetStateBlock->streamFlags[i];
316             }
317         }
318
319         for (i = 0; i < GL_LIMITS(clipplanes); i++) {
320             if (This->set.clipplane[i] && memcmp(&targetStateBlock->clipplane[i],
321                                                         &This->clipplane[i],
322                                                         sizeof(This->clipplane)) != 0) {
323
324                 TRACE("Updating clipplane %d\n", i);
325                 memcpy(&This->clipplane[i], &targetStateBlock->clipplane[i],
326                                         sizeof(This->clipplane));
327             }
328         }
329
330         /* Render */
331         for (i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
332
333             if (This->set.renderState[i] && (This->renderState[i] != targetStateBlock->renderState[i])) {
334                 TRACE("Updating renderState %d to %ld\n", i, targetStateBlock->renderState[i]);
335                 This->renderState[i] = targetStateBlock->renderState[i];
336             }
337         }
338
339         /* FIXME: textures are up to MAX_SAMPLERS for d3d9? */
340         /* Texture */
341         for (j = 0; j < GL_LIMITS(textures); j++) {
342             /* TODO: move over to using memcpy */
343             for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE ; i++) {
344                 if (This->set.textureState[j][i]) {
345                     TRACE("Updating texturestagestate %d,%d to %ld (was %ld)\n", j,i, targetStateBlock->textureState[j][i],
346                     This->textureState[j][i]);
347                     This->textureState[j][i]         =  targetStateBlock->textureState[j][i];
348                 }
349             }
350
351             if (This->set.textures[j]) {
352                 TRACE("Updating texture %d to %p (was %p)\n", j, targetStateBlock->textures[j],  This->textures[j]);
353                 This->textures[j] = targetStateBlock->textures[j];
354             }
355
356         }
357
358         /* Samplers */
359         /* TODO: move over to using memcpy */
360         for (j = 0 ; j < GL_LIMITS(samplers); j++){
361             for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE ; i++){ /* States are 1 based */
362                 if (This->set.samplerState[j][i]) {
363                     TRACE("Updating sampler state %d,%d to %ld (was %ld)\n",
364                     j, i, targetStateBlock->samplerState[j][i],
365                     This->samplerState[j][i]);
366                     This->samplerState[j][i]         = targetStateBlock->samplerState[j][i];
367                 }
368             }
369         }
370     }
371
372     TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
373
374     return WINED3D_OK;
375 }
376
377 HRESULT WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
378     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
379     IWineD3DDevice*        pDevice     = (IWineD3DDevice*)This->wineD3DDevice;
380
381 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
382 should really perform a delta so that only the changes get updated*/
383
384
385     UINT i;
386     UINT j;
387
388     TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
389
390     /* FIXME: Only apply applicable states not all states */
391
392     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */This->blockType == WINED3DSBT_INIT || This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_VERTEXSTATE) {
393
394
395         PLIGHTINFOEL *toDo = This->lights;
396         while (toDo != NULL) {
397             if (toDo->changed)
398                   IWineD3DDevice_SetLight(pDevice, toDo->OriginalIndex, &toDo->OriginalParms);
399             if (toDo->enabledChanged)
400                   IWineD3DDevice_SetLightEnable(pDevice, toDo->OriginalIndex, toDo->lightEnabled);
401             toDo = toDo->next;
402         }
403
404         /* Vertex Shader */
405         if (This->set.vertexShader && This->changed.vertexShader) {
406             IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
407         }
408
409         /* Vertex Shader Constants */
410         for (i = 0; i < MAX_VSHADER_CONSTANTS; ++i) {
411             if (This->set.vertexShaderConstants[i] && This->changed.vertexShaderConstants[i]) {
412                 switch (This->vertexShaderConstantT[i]) {
413                 case WINESHADERCNST_FLOAT:
414                     IWineD3DDevice_SetVertexShaderConstantF(pDevice, i, This->vertexShaderConstantF + i * 4, 1);
415                 break;
416                 case WINESHADERCNST_BOOL:
417                     IWineD3DDevice_SetVertexShaderConstantB(pDevice, i, This->vertexShaderConstantB + i, 1);
418                 break;
419                 case WINESHADERCNST_INTEGER:
420                     IWineD3DDevice_SetVertexShaderConstantI(pDevice, i, This->vertexShaderConstantI + i * 4, 1);
421                 break;
422                 case WINESHADERCNST_NONE:
423                     IWineD3DDevice_SetVertexShaderConstantN(pDevice, i, 1);
424                 break;
425                 }
426             }
427         }
428
429     }
430
431     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == D3DSBT_ALL || This->blockType == D3DSBT_PIXELSTATE) {
432
433         /* Pixel Shader */
434         if (This->set.pixelShader && This->changed.pixelShader) {
435             IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
436         }
437
438         /* Pixel Shader Constants */
439         for (i = 0; i < MAX_PSHADER_CONSTANTS; ++i) {
440             if (This->set.pixelShaderConstants[i] && This->changed.pixelShaderConstants[i]) {
441                 switch (This->pixelShaderConstantT[i]) {
442                 case WINESHADERCNST_FLOAT:
443                     IWineD3DDevice_SetPixelShaderConstantF(pDevice, i, This->pixelShaderConstantF + i * 4, 1);
444                 break;
445                 case WINESHADERCNST_BOOL:
446                     IWineD3DDevice_SetPixelShaderConstantB(pDevice, i, This->pixelShaderConstantB + i, 1);
447                 break;
448                 case WINESHADERCNST_INTEGER:
449                     IWineD3DDevice_SetPixelShaderConstantI(pDevice, i, This->pixelShaderConstantI + i * 4, 1);
450                 break;
451                 case WINESHADERCNST_NONE:
452                     IWineD3DDevice_SetPixelShaderConstantN(pDevice, i, 1);
453                 break;
454                 }
455             }
456         }
457     }
458
459     if (This->set.fvf && This->changed.fvf) {
460         IWineD3DDevice_SetFVF(pDevice, This->fvf);
461     }
462
463     if (This->set.vertexDecl && This->changed.vertexDecl) {
464         IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
465     }
466
467     /* Others + Render & Texture */
468     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_INIT) {
469         for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
470             if (This->set.transform[i] && This->changed.transform[i])
471                 IWineD3DDevice_SetTransform(pDevice, i, &This->transforms[i]);
472         }
473
474         if (This->set.indices && This->changed.indices)
475             IWineD3DDevice_SetIndices(pDevice, This->pIndexData, This->baseVertexIndex);
476
477         if (This->set.material && This->changed.material )
478             IWineD3DDevice_SetMaterial(pDevice, &This->material);
479
480         if (This->set.viewport && This->changed.viewport)
481             IWineD3DDevice_SetViewport(pDevice, &This->viewport);
482
483         /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
484         for (i=0; i<MAX_STREAMS; i++) {
485             if (This->set.streamSource[i] && This->changed.streamSource[i])
486                 IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
487
488             if (This->set.streamFreq[i] && This->changed.streamFreq[i])
489                 IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
490         }
491
492         for (i = 0; i < GL_LIMITS(clipplanes); i++) {
493             if (This->set.clipplane[i] && This->changed.clipplane[i]) {
494                 float clip[4];
495
496                 clip[0] = This->clipplane[i][0];
497                 clip[1] = This->clipplane[i][1];
498                 clip[2] = This->clipplane[i][2];
499                 clip[3] = This->clipplane[i][3];
500                 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
501             }
502         }
503
504         /* Render */
505         for (i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
506             if (This->set.renderState[i] && This->changed.renderState[i])
507                 IWineD3DDevice_SetRenderState(pDevice, i, This->renderState[i]);
508         }
509
510         /* FIXME: Texture are set against samplers... not just TextureStages */
511         /* Texture */
512         for (j = 0; j < GL_LIMITS(textures); j++) { /* Set The texture first, just in case it resets the states? */
513             if (This->set.textures[j] && This->changed.textures[j]) {
514                 IWineD3DDevice_SetTexture(pDevice, j, This->textures[j]);
515             }
516             /* TODO: move over to memcpy */
517             for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE; i++) {
518                 if (This->set.textureState[j][i] && This->changed.textureState[j][i]) { /* tb_dx9_10 failes without this test */
519                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][i]         = This->textureState[j][i];
520                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->set.textureState[j][i]     = TRUE;
521                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[j][i] = TRUE;
522                 }
523             }
524         }
525
526         /* Samplers */
527         /* TODO: move over to memcpy */
528         for (j = 0 ; j < GL_LIMITS(samplers); j++){
529             for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; i++){
530                 if (This->set.samplerState[j][i] && This->changed.samplerState[j][i]) {
531                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][i]         = This->samplerState[j][i];
532                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->set.samplerState[j][i]     = TRUE;
533                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[j][i] = TRUE;
534                 }
535             }
536
537         }
538
539     } else if (This->blockType == WINED3DSBT_PIXELSTATE) {
540
541         for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
542             if (This->set.renderState[SavedPixelStates_R[i]] && This->changed.renderState[SavedPixelStates_R[i]])
543                 IWineD3DDevice_SetRenderState(pDevice, SavedPixelStates_R[i], This->renderState[SavedPixelStates_R[i]]);
544
545         }
546
547         for (j = 0; j < GL_LIMITS(textures); j++) {
548             for (i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
549                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedPixelStates_T[i]] = This->textureState[j][SavedPixelStates_T[i]];
550             }
551         }
552
553         for (j = 0; j < GL_LIMITS(samplers); j++) {
554             for (i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
555                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedPixelStates_S[i]] = This->samplerState[j][SavedPixelStates_S[i]];
556             }
557         }
558
559     } else if (This->blockType == WINED3DSBT_VERTEXSTATE) {
560
561         for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
562             if ( This->set.renderState[SavedVertexStates_R[i]] && This->changed.renderState[SavedVertexStates_R[i]])
563                 IWineD3DDevice_SetRenderState(pDevice, SavedVertexStates_R[i], This->renderState[SavedVertexStates_R[i]]);
564         }
565
566         for (j = 0; j < GL_LIMITS(textures); j++) {
567             for (i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
568                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedVertexStates_T[i]] = This->textureState[j][SavedVertexStates_T[i]];
569             }
570         }
571
572         for (j = 0; j < GL_LIMITS(textures); j++) {
573             for (i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
574                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedVertexStates_S[i]] = This->samplerState[j][SavedVertexStates_S[i]];
575             }
576         }
577
578
579     } else {
580         FIXME("Unrecognized state block type %d\n", This->blockType);
581     }
582     memcpy(&((IWineD3DDeviceImpl*)pDevice)->stateBlock->changed, &This->changed, sizeof(((IWineD3DDeviceImpl*)pDevice)->stateBlock->changed));
583     TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
584
585     return WINED3D_OK;
586 }
587
588 HRESULT WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
589     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
590     IWineD3DDevice         *device = (IWineD3DDevice *)This->wineD3DDevice;
591     IWineD3DDeviceImpl     *ThisDevice = (IWineD3DDeviceImpl *)device;
592     union {
593         D3DLINEPATTERN lp;
594         DWORD d;
595     } lp;
596     union {
597         float f;
598         DWORD d;
599     } tmpfloat;
600     unsigned int i;
601
602     /* Note this may have a large overhead but it should only be executed
603        once, in order to initialize the complete state of the device and
604        all opengl equivalents                                            */
605     TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
606     /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
607     This->blockType = WINED3DSBT_INIT;
608
609     /* Set some of the defaults for lights, transforms etc */
610     memcpy(&This->transforms[D3DTS_PROJECTION], &identity, sizeof(identity));
611     memcpy(&This->transforms[D3DTS_VIEW], &identity, sizeof(identity));
612     for (i = 0; i < 256; ++i) {
613       memcpy(&This->transforms[D3DTS_WORLDMATRIX(i)], &identity, sizeof(identity));
614     }
615
616     TRACE("Render states\n");
617     /* Render states: */
618     if (ThisDevice->depthStencilBuffer != NULL) {
619        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       D3DZB_TRUE);
620     } else {
621        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       D3DZB_FALSE);
622     }
623     IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE,         D3DFILL_SOLID);
624     IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE,        D3DSHADE_GOURAUD);
625     lp.lp.wRepeatFactor = 0;
626     lp.lp.wLinePattern  = 0;
627     IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN,      lp.d);
628     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE,     TRUE);
629     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE,  FALSE);
630     IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL,        TRUE);
631     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND,         D3DBLEND_ONE);
632     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND,        D3DBLEND_ZERO);
633     IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE,         D3DCULL_CCW);
634     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC,            D3DCMP_LESSEQUAL);
635     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC,        D3DCMP_ALWAYS);
636     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF,         0xff); /*??*/
637     IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE,     FALSE);
638     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
639     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE,        FALSE);
640     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE,   FALSE);
641     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE,         0);
642     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR,         0);
643     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE,     D3DFOG_NONE);
644     tmpfloat.f = 0.0f;
645     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART,         tmpfloat.d);
646     tmpfloat.f = 1.0f;
647     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND,           tmpfloat.d);
648     tmpfloat.f = 1.0f;
649     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY,       tmpfloat.d);
650     IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS,    FALSE);
651     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS,            0);
652     IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE,   FALSE);
653     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE,    FALSE);
654     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL,      D3DSTENCILOP_KEEP);
655     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL,     D3DSTENCILOP_KEEP);
656     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS,      D3DSTENCILOP_KEEP);
657
658     /* Setting stencil func also uses values for stencil ref/mask, so manually set defaults
659      * so only a single call performed (and ensure defaults initialized before making that call)
660      *
661      * IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF, 0);
662      * IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK, 0xFFFFFFFF);
663      */
664     This->renderState[WINED3DRS_STENCILREF] = 0;
665     This->renderState[WINED3DRS_STENCILMASK] = 0xFFFFFFFF;
666     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC,      D3DCMP_ALWAYS);
667     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
668     IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR,    0xFFFFFFFF);
669     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
670     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
671     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
672     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
673     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
674     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
675     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
676     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
677     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING,                 TRUE);
678     IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING,                 TRUE);
679     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT,                  0);
680     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE,            D3DFOG_NONE);
681     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX,              TRUE);
682     IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER,              TRUE);
683     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS,         FALSE);
684     IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE,    D3DMCS_COLOR1);
685     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE,   D3DMCS_COLOR2);
686     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE,    D3DMCS_COLOR2);
687     IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE,   D3DMCS_MATERIAL);
688     IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND,              D3DVBF_DISABLE);
689     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE,          0);
690     IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
691     tmpfloat.f = 1.0f;
692     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE,                tmpfloat.d);
693     tmpfloat.f = 0.0f;
694     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN,            tmpfloat.d);
695     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE,        FALSE);
696     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE,         FALSE);
697     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A,             TRUE);
698     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B,             TRUE);
699     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C,             TRUE);
700     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS,     TRUE);
701     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK,          0xFFFFFFFF);
702     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE,           D3DPATCHEDGE_DISCRETE);
703     tmpfloat.f = 1.0f;
704     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS,            tmpfloat.d);
705     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN,        D3DDMT_DISABLE);
706     tmpfloat.f = 64.0f;
707     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX,            tmpfloat.d);
708     IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
709     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE,         0x0000000F);
710     tmpfloat.f = 0.0f;
711     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR,              tmpfloat.d);
712     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP,                  D3DBLENDOP_ADD);
713     IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE,           WINED3DDEGREE_CUBIC);
714     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE,             WINED3DDEGREE_LINEAR);
715     /* states new in d3d9 */
716     IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE,        FALSE);
717     IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS,      0);
718     tmpfloat.f = 1.0f;
719     IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL,     tmpfloat.d);
720     IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL,     tmpfloat.d);
721     IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE,    FALSE);
722     tmpfloat.f = 0.0f;
723     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X,           tmpfloat.d);
724     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y,           tmpfloat.d);
725     tmpfloat.f = 1.0f;
726     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z,           tmpfloat.d);
727     tmpfloat.f = 0.0f;
728     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W,           tmpfloat.d);
729     IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
730     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE,      FALSE);
731     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL,          D3DSTENCILOP_KEEP);
732     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL,         D3DSTENCILOP_KEEP);
733     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS,          D3DSTENCILOP_KEEP);
734     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC,          D3DCMP_ALWAYS);
735     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1,        0x0000000F);
736     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2,        0x0000000F);
737     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3,        0x0000000F);
738     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR,              0xFFFFFFFF);
739     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE,          0);
740     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS,                0);
741     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8,  0);
742     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9,  0);
743     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
744     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
745     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
746     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
747     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
748     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
749     IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
750     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA,            D3DBLEND_ONE);
751     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA,           D3DBLEND_ZERO);
752     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA,             D3DBLENDOP_ADD);
753
754     /* clipping status */
755     This->clip_status.ClipUnion = 0;
756     This->clip_status.ClipIntersection = 0xFFFFFFFF;
757
758     /* Texture Stage States - Put directly into state block, we will call function below */
759     for (i = 0; i < GL_LIMITS(textures); i++) {
760         TRACE("Setting up default texture states for texture Stage %d\n", i);
761         memcpy(&This->transforms[D3DTS_TEXTURE0 + i], &identity, sizeof(identity));
762         This->textureState[i][D3DTSS_COLOROP               ] = (i==0)? D3DTOP_MODULATE :  D3DTOP_DISABLE;
763         This->textureState[i][D3DTSS_COLORARG1             ] = D3DTA_TEXTURE;
764         This->textureState[i][D3DTSS_COLORARG2             ] = D3DTA_CURRENT;
765         This->textureState[i][D3DTSS_ALPHAOP               ] = (i==0)? D3DTOP_SELECTARG1 :  D3DTOP_DISABLE;
766         This->textureState[i][D3DTSS_ALPHAARG1             ] = D3DTA_TEXTURE;
767         This->textureState[i][D3DTSS_ALPHAARG2             ] = D3DTA_CURRENT;
768         This->textureState[i][D3DTSS_BUMPENVMAT00          ] = (DWORD) 0.0;
769         This->textureState[i][D3DTSS_BUMPENVMAT01          ] = (DWORD) 0.0;
770         This->textureState[i][D3DTSS_BUMPENVMAT10          ] = (DWORD) 0.0;
771         This->textureState[i][D3DTSS_BUMPENVMAT11          ] = (DWORD) 0.0;
772         This->textureState[i][D3DTSS_TEXCOORDINDEX         ] = i;
773         This->textureState[i][D3DTSS_BUMPENVLSCALE         ] = (DWORD) 0.0;
774         This->textureState[i][D3DTSS_BUMPENVLOFFSET        ] = (DWORD) 0.0;
775         This->textureState[i][D3DTSS_TEXTURETRANSFORMFLAGS ] = D3DTTFF_DISABLE;
776         This->textureState[i][D3DTSS_ADDRESSW              ] = D3DTADDRESS_WRAP;
777         This->textureState[i][D3DTSS_COLORARG0             ] = D3DTA_CURRENT;
778         This->textureState[i][D3DTSS_ALPHAARG0             ] = D3DTA_CURRENT;
779         This->textureState[i][D3DTSS_RESULTARG             ] = D3DTA_CURRENT;
780     }
781
782         /* Sampler states*/
783     for (i = 0 ; i <  GL_LIMITS(samplers); i++) {
784         TRACE("Setting up default samplers states for sampler %d\n", i);
785         This->samplerState[i][WINED3DSAMP_ADDRESSU         ] = D3DTADDRESS_WRAP;
786         This->samplerState[i][WINED3DSAMP_ADDRESSV         ] = D3DTADDRESS_WRAP;
787         This->samplerState[i][WINED3DSAMP_ADDRESSW         ] = D3DTADDRESS_WRAP;
788         This->samplerState[i][WINED3DSAMP_BORDERCOLOR      ] = 0x00;
789         This->samplerState[i][WINED3DSAMP_MAGFILTER        ] = WINED3DTEXF_POINT;
790         This->samplerState[i][WINED3DSAMP_MINFILTER        ] = WINED3DTEXF_POINT;
791         This->samplerState[i][WINED3DSAMP_MIPFILTER        ] = WINED3DTEXF_NONE;
792         This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS    ] = 0;
793         This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL      ] = 0;
794         This->samplerState[i][WINED3DSAMP_MAXANISOTROPY    ] = 1;
795         This->samplerState[i][WINED3DSAMP_SRGBTEXTURE      ] = 0; /* TODO: Gamma correction value*/
796         This->samplerState[i][WINED3DSAMP_ELEMENTINDEX     ] = 0; /* TODO: Indicates which element of a  multielement texture to use */
797         This->samplerState[i][WINED3DSAMP_DMAPOFFSET       ] = 256; /* TODO: Vertex offset in the presampled displacement map */
798     }
799
800     /* Under DirectX you can have texture stage operations even if no texture is
801        bound, whereas opengl will only do texture operations when a valid texture is
802        bound. We emulate this by creating dummy textures and binding them to each
803        texture stage, but disable all stages by default. Hence if a stage is enabled
804        then the default texture will kick in until replaced by a SetTexture call     */
805
806     ENTER_GL();
807
808     for (i = 0; i < GL_LIMITS(textures); i++) {
809         GLubyte white = 255;
810
811         /* Note this avoids calling settexture, so pretend it has been called */
812         This->set.textures[i]     = TRUE;
813         This->changed.textures[i] = TRUE;
814         This->textures[i]         = NULL;
815
816         /* Make appropriate texture active */
817         if (GL_SUPPORT(ARB_MULTITEXTURE)) {
818             GLACTIVETEXTURE(i);
819         } else if (i > 0) {
820             FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
821         }
822
823         /* Generate an opengl texture name */
824         glGenTextures(1, &ThisDevice->dummyTextureName[i]);
825         checkGLcall("glGenTextures");
826         TRACE("Dummy Texture %d given name %d\n", i, ThisDevice->dummyTextureName[i]);
827
828         /* Generate a dummy 1d texture */
829         This->textureDimensions[i] = GL_TEXTURE_1D;
830         glBindTexture(GL_TEXTURE_1D, ThisDevice->dummyTextureName[i]);
831         checkGLcall("glBindTexture");
832
833         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &white);
834         checkGLcall("glTexImage1D");
835 #if 1   /* TODO: move the setting texture states off to basetexture */
836         /* Reapply all the texture state information to this texture */
837         IWineD3DDevice_SetupTextureStates(device, i, REAPPLY_ALL);
838 #endif
839     }
840
841     LEAVE_GL();
842
843     /* Defaulting palettes - Note these are device wide but reinitialized here for convenience*/
844     for (i = 0; i < MAX_PALETTES; ++i) {
845       int j;
846       for (j = 0; j < 256; ++j) {
847         This->wineD3DDevice->palettes[i][j].peRed   = 0xFF;
848         This->wineD3DDevice->palettes[i][j].peGreen = 0xFF;
849         This->wineD3DDevice->palettes[i][j].peBlue  = 0xFF;
850         This->wineD3DDevice->palettes[i][j].peFlags = 0xFF;
851       }
852     }
853     This->wineD3DDevice->currentPalette = 0;
854
855     TRACE("-----------------------> Device defaults now set up...\n");
856     return WINED3D_OK;
857 }
858
859 /**********************************************************
860  * IWineD3DStateBlock VTbl follows
861  **********************************************************/
862
863 const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
864 {
865     /* IUnknown */
866     IWineD3DStateBlockImpl_QueryInterface,
867     IWineD3DStateBlockImpl_AddRef,
868     IWineD3DStateBlockImpl_Release,
869     /* IWineD3DStateBlock */
870     IWineD3DStateBlockImpl_GetParent,
871     IWineD3DStateBlockImpl_GetDevice,
872     IWineD3DStateBlockImpl_Capture,
873     IWineD3DStateBlockImpl_Apply,
874     IWineD3DStateBlockImpl_InitStartupStateBlock
875 };