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