wined3d: Rework shader mode selection.
[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 static 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 static 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 static 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 application is responsible for making sure the texture references are cleared down */
93             for (counter = 0; counter < GL_LIMITS(sampler_stages); 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 static 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 static 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 static 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
172             if (This->set.vertexShaderConstantsF[i]) {
173                 TRACE("Setting %p from %p %d to { %f, %f, %f, %f }\n", This, targetStateBlock, i,
174                     targetStateBlock->vertexShaderConstantF[i * 4],
175                     targetStateBlock->vertexShaderConstantF[i * 4 + 1],
176                     targetStateBlock->vertexShaderConstantF[i * 4 + 2],
177                     targetStateBlock->vertexShaderConstantF[i * 4 + 3]);
178
179                 This->vertexShaderConstantF[i * 4]      = targetStateBlock->vertexShaderConstantF[i * 4];
180                 This->vertexShaderConstantF[i * 4 + 1]  = targetStateBlock->vertexShaderConstantF[i * 4 + 1];
181                 This->vertexShaderConstantF[i * 4 + 2]  = targetStateBlock->vertexShaderConstantF[i * 4 + 2];
182                 This->vertexShaderConstantF[i * 4 + 3]  = targetStateBlock->vertexShaderConstantF[i * 4 + 3];
183             }
184
185             if (This->set.vertexShaderConstantsI[i]) {
186                 TRACE("Setting %p from %p %d to { %d, %d, %d, %d }\n", This, targetStateBlock, i,
187                     targetStateBlock->vertexShaderConstantI[i * 4],
188                     targetStateBlock->vertexShaderConstantI[i * 4 + 1],
189                     targetStateBlock->vertexShaderConstantI[i * 4 + 2],
190                     targetStateBlock->vertexShaderConstantI[i * 4 + 3]);
191
192                 This->vertexShaderConstantI[i * 4]      = targetStateBlock->vertexShaderConstantI[i * 4];
193                 This->vertexShaderConstantI[i * 4 + 1]  = targetStateBlock->vertexShaderConstantI[i * 4 + 1];
194                 This->vertexShaderConstantI[i * 4 + 2]  = targetStateBlock->vertexShaderConstantI[i * 4 + 2];
195                 This->vertexShaderConstantI[i * 4 + 3]  = targetStateBlock->vertexShaderConstantI[i * 4 + 3];
196             }
197
198             if (This->set.vertexShaderConstantsB[i]) {
199                 TRACE("Setting %p from %p %d to %s\n", This, targetStateBlock, i,
200                     targetStateBlock->vertexShaderConstantB[i]? "TRUE":"FALSE");
201
202                 This->vertexShaderConstantB[i] =  targetStateBlock->vertexShaderConstantB[i];
203             }
204         }
205
206         /* Lights... For a recorded state block, we just had a chain of actions to perform,
207              so we need to walk that chain and update any actions which differ */
208         src = This->lights;
209         while (src != NULL) {
210             PLIGHTINFOEL *realLight = NULL;
211
212             /* Locate the light in the live lights */
213             realLight = targetStateBlock->lights;
214             while (realLight != NULL && realLight->OriginalIndex != src->OriginalIndex) realLight = realLight->next;
215
216             /* If 'changed' then its a SetLight command. Rather than comparing to see
217                  if the OriginalParms have changed and then copy them (twice through
218                  memory) just do the copy                                              */
219             if (src->changed) {
220
221                 /* If the light exists, copy its parameters, otherwise copy the default parameters */
222                 const WINED3DLIGHT* params = realLight? &realLight->OriginalParms: &WINED3D_default_light;
223                 TRACE("Updating lights for light %ld\n", src->OriginalIndex);
224                 memcpy(&src->OriginalParms, params, sizeof(*params));
225             }
226
227             /* If 'enabledchanged' then its a LightEnable command */
228             if (src->enabledChanged) {
229
230                 /* If the light exists, check if it's enabled, otherwise default is disabled state */
231                 TRACE("Updating lightEnabled for light %ld\n", src->OriginalIndex);
232                 src->lightEnabled = realLight? realLight->lightEnabled: FALSE;
233             }
234
235             src = src->next;
236         }
237
238         /* Recorded => Only update 'changed' values */
239         if (This->pixelShader != targetStateBlock->pixelShader) {
240             TRACE("Updating pixel shader from %p to %p\n", This->pixelShader, targetStateBlock->pixelShader);
241
242             if (targetStateBlock->pixelShader) {
243                 IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
244             }
245             if (This->pixelShader) {
246                 IWineD3DPixelShader_Release(This->pixelShader);
247             }
248
249             This->pixelShader = targetStateBlock->pixelShader;
250         }
251
252         for (i = 0; i < MAX_PSHADER_CONSTANTS; ++i) {
253
254             if (This->set.pixelShaderConstantsF[i]) {
255                 TRACE("Setting %p from %p %d to { %f, %f, %f, %f }\n", This, targetStateBlock, i,
256                     targetStateBlock->pixelShaderConstantF[i * 4],
257                     targetStateBlock->pixelShaderConstantF[i * 4 + 1],
258                     targetStateBlock->pixelShaderConstantF[i * 4 + 2],
259                     targetStateBlock->pixelShaderConstantF[i * 4 + 3]);
260
261                 This->pixelShaderConstantF[i * 4]      = targetStateBlock->pixelShaderConstantF[i * 4];
262                 This->pixelShaderConstantF[i * 4 + 1]  = targetStateBlock->pixelShaderConstantF[i * 4 + 1];
263                 This->pixelShaderConstantF[i * 4 + 2]  = targetStateBlock->pixelShaderConstantF[i * 4 + 2];
264                 This->pixelShaderConstantF[i * 4 + 3]  = targetStateBlock->pixelShaderConstantF[i * 4 + 3];
265             }
266
267             if (This->set.pixelShaderConstantsI[i]) {
268                 TRACE("Setting %p from %p %d to { %d, %d, %d, %d }\n", This, targetStateBlock, i,
269                     targetStateBlock->pixelShaderConstantI[i * 4],
270                     targetStateBlock->pixelShaderConstantI[i * 4 + 1],
271                     targetStateBlock->pixelShaderConstantI[i * 4 + 2],
272                     targetStateBlock->pixelShaderConstantI[i * 4 + 3]);
273
274                 This->pixelShaderConstantI[i * 4]      = targetStateBlock->pixelShaderConstantI[i * 4];
275                 This->pixelShaderConstantI[i * 4 + 1]  = targetStateBlock->pixelShaderConstantI[i * 4 + 1];
276                 This->pixelShaderConstantI[i * 4 + 2]  = targetStateBlock->pixelShaderConstantI[i * 4 + 2];
277                 This->pixelShaderConstantI[i * 4 + 3]  = targetStateBlock->pixelShaderConstantI[i * 4 + 3];
278             }
279
280             if (This->set.pixelShaderConstantsB[i]) {
281                 TRACE("Setting %p from %p %d to %s\n", This, targetStateBlock, i,
282                     targetStateBlock->pixelShaderConstantB[i]? "TRUE":"FALSE");
283
284                 This->pixelShaderConstantB[i] =  targetStateBlock->pixelShaderConstantB[i];
285             }
286         }
287
288         /* Others + Render & Texture */
289         for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
290             if (This->set.transform[i] && memcmp(&targetStateBlock->transforms[i],
291                                     &This->transforms[i],
292                                     sizeof(D3DMATRIX)) != 0) {
293                 TRACE("Updating transform %d\n", i);
294                 memcpy(&This->transforms[i], &targetStateBlock->transforms[i], sizeof(D3DMATRIX));
295             }
296         }
297
298         if (This->set.indices && ((This->pIndexData != targetStateBlock->pIndexData)
299                         || (This->baseVertexIndex != targetStateBlock->baseVertexIndex))) {
300             TRACE("Updating pindexData to %p, baseVertexIndex to %d\n",
301             targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
302             This->pIndexData = targetStateBlock->pIndexData;
303             This->baseVertexIndex = targetStateBlock->baseVertexIndex;
304         }
305
306         if(This->set.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl){
307             TRACE("Updating vertex declaration from %p to %p\n", This->vertexDecl, targetStateBlock->vertexDecl);
308
309             if (targetStateBlock->vertexDecl) {
310                 IWineD3DVertexDeclaration_AddRef(targetStateBlock->vertexDecl);
311             }
312             if (This->vertexDecl) {
313                 IWineD3DVertexDeclaration_Release(This->vertexDecl);
314             }
315
316             This->vertexDecl = targetStateBlock->vertexDecl;
317         }
318
319         if(This->set.fvf && This->fvf != targetStateBlock->fvf){
320             This->fvf = targetStateBlock->fvf;
321         }
322
323         if (This->set.material && memcmp(&targetStateBlock->material,
324                                                     &This->material,
325                                                     sizeof(D3DMATERIAL9)) != 0) {
326             TRACE("Updating material\n");
327             memcpy(&This->material, &targetStateBlock->material, sizeof(D3DMATERIAL9));
328         }
329
330         if (This->set.viewport && memcmp(&targetStateBlock->viewport,
331                                                     &This->viewport,
332                                                     sizeof(D3DVIEWPORT9)) != 0) {
333             TRACE("Updating viewport\n");
334             memcpy(&This->viewport, &targetStateBlock->viewport, sizeof(D3DVIEWPORT9));
335         }
336
337         for (i = 0; i < MAX_STREAMS; i++) {
338             if (This->set.streamSource[i] &&
339                             ((This->streamStride[i] != targetStateBlock->streamStride[i]) ||
340                             (This->streamSource[i] != targetStateBlock->streamSource[i]))) {
341                 TRACE("Updating stream source %d to %p, stride to %d\n", i, targetStateBlock->streamSource[i],
342                                                                             targetStateBlock->streamStride[i]);
343                 This->streamStride[i] = targetStateBlock->streamStride[i];
344                 This->streamSource[i] = targetStateBlock->streamSource[i];
345             }
346
347             if (This->set.streamFreq[i] &&
348             (This->streamFreq[i] != targetStateBlock->streamFreq[i]
349             || This->streamFlags[i] != targetStateBlock->streamFlags[i])){
350                     TRACE("Updating stream frequency %d to %d flags to %d\n", i ,  targetStateBlock->streamFreq[i] ,
351                                                                                    targetStateBlock->streamFlags[i]);
352                     This->streamFreq[i]  =  targetStateBlock->streamFreq[i];
353                     This->streamFlags[i] =  targetStateBlock->streamFlags[i];
354             }
355         }
356
357         for (i = 0; i < GL_LIMITS(clipplanes); i++) {
358             if (This->set.clipplane[i] && memcmp(&targetStateBlock->clipplane[i],
359                                                         &This->clipplane[i],
360                                                         sizeof(This->clipplane)) != 0) {
361
362                 TRACE("Updating clipplane %d\n", i);
363                 memcpy(&This->clipplane[i], &targetStateBlock->clipplane[i],
364                                         sizeof(This->clipplane));
365             }
366         }
367
368         /* Render */
369         for (i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
370
371             if (This->set.renderState[i] && (This->renderState[i] != targetStateBlock->renderState[i])) {
372                 TRACE("Updating renderState %d to %ld\n", i, targetStateBlock->renderState[i]);
373                 This->renderState[i] = targetStateBlock->renderState[i];
374             }
375         }
376
377         /* Texture states */
378         for (j = 0; j < GL_LIMITS(texture_stages); j++) {
379             /* TODO: move over to using memcpy */
380             for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE ; i++) {
381                 if (This->set.textureState[j][i]) {
382                     TRACE("Updating texturestagestate %d,%d to %ld (was %ld)\n", j,i, targetStateBlock->textureState[j][i],
383                     This->textureState[j][i]);
384                     This->textureState[j][i]         =  targetStateBlock->textureState[j][i];
385                 }
386             }
387         }
388
389         /* Samplers */
390         /* TODO: move over to using memcpy */
391         for (j = 0; j < GL_LIMITS(sampler_stages); j++) {
392             if (This->set.textures[j]) {
393                 TRACE("Updating texture %d to %p (was %p)\n", j, targetStateBlock->textures[j],  This->textures[j]);
394                 This->textures[j] = targetStateBlock->textures[j];
395             }
396             for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE ; i++){ /* States are 1 based */
397                 if (This->set.samplerState[j][i]) {
398                     TRACE("Updating sampler state %d,%d to %ld (was %ld)\n",
399                     j, i, targetStateBlock->samplerState[j][i],
400                     This->samplerState[j][i]);
401                     This->samplerState[j][i]         = targetStateBlock->samplerState[j][i];
402                 }
403             }
404         }
405     }
406
407     TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
408
409     return WINED3D_OK;
410 }
411
412 static HRESULT  WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
413     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
414     IWineD3DDevice*        pDevice     = (IWineD3DDevice*)This->wineD3DDevice;
415
416 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
417 should really perform a delta so that only the changes get updated*/
418
419
420     UINT i;
421     UINT j;
422
423     TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
424
425     /* FIXME: Only apply applicable states not all states */
426
427     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */This->blockType == WINED3DSBT_INIT || This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_VERTEXSTATE) {
428
429
430         PLIGHTINFOEL *toDo = This->lights;
431         while (toDo != NULL) {
432             if (toDo->changed)
433                   IWineD3DDevice_SetLight(pDevice, toDo->OriginalIndex, &toDo->OriginalParms);
434             if (toDo->enabledChanged)
435                   IWineD3DDevice_SetLightEnable(pDevice, toDo->OriginalIndex, toDo->lightEnabled);
436             toDo = toDo->next;
437         }
438
439         /* Vertex Shader */
440         if (This->set.vertexShader && This->changed.vertexShader) {
441             IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
442         }
443
444         /* Vertex Shader Constants */
445         for (i = 0; i < MAX_VSHADER_CONSTANTS; ++i) {
446             if (This->set.vertexShaderConstantsF[i] && This->changed.vertexShaderConstantsF[i])
447                 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i, This->vertexShaderConstantF + i * 4, 1);
448
449             if (This->set.vertexShaderConstantsI[i] && This->changed.vertexShaderConstantsI[i])
450                 IWineD3DDevice_SetVertexShaderConstantI(pDevice, i, This->vertexShaderConstantI + i * 4, 1);
451
452             if (This->set.vertexShaderConstantsB[i] && This->changed.vertexShaderConstantsB[i])
453                 IWineD3DDevice_SetVertexShaderConstantB(pDevice, i, This->vertexShaderConstantB + i, 1);
454         }
455     }
456
457     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == D3DSBT_ALL || This->blockType == D3DSBT_PIXELSTATE) {
458
459         /* Pixel Shader */
460         if (This->set.pixelShader && This->changed.pixelShader) {
461             IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
462         }
463
464         /* Pixel Shader Constants */
465         for (i = 0; i < MAX_PSHADER_CONSTANTS; ++i) {
466             if (This->set.pixelShaderConstantsF[i] && This->changed.pixelShaderConstantsF[i])
467                 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i, This->pixelShaderConstantF + i * 4, 1);
468
469             if (This->set.pixelShaderConstantsI[i] && This->changed.pixelShaderConstantsI[i])
470                 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i, This->pixelShaderConstantI + i * 4, 1);
471
472             if (This->set.pixelShaderConstantsB[i] && This->changed.pixelShaderConstantsB[i])
473                 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i, This->pixelShaderConstantB + i, 1);
474         }
475     }
476
477     if (This->set.fvf && This->changed.fvf) {
478         IWineD3DDevice_SetFVF(pDevice, This->fvf);
479     }
480
481     if (This->set.vertexDecl && This->changed.vertexDecl) {
482         IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
483     }
484
485     /* Others + Render & Texture */
486     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_INIT) {
487         for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
488             if (This->set.transform[i] && This->changed.transform[i])
489                 IWineD3DDevice_SetTransform(pDevice, i, &This->transforms[i]);
490         }
491
492         if (This->set.indices && This->changed.indices)
493             IWineD3DDevice_SetIndices(pDevice, This->pIndexData, This->baseVertexIndex);
494
495         if (This->set.material && This->changed.material )
496             IWineD3DDevice_SetMaterial(pDevice, &This->material);
497
498         if (This->set.viewport && This->changed.viewport)
499             IWineD3DDevice_SetViewport(pDevice, &This->viewport);
500
501         /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
502         for (i=0; i<MAX_STREAMS; i++) {
503             if (This->set.streamSource[i] && This->changed.streamSource[i])
504                 IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
505
506             if (This->set.streamFreq[i] && This->changed.streamFreq[i])
507                 IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
508         }
509
510         for (i = 0; i < GL_LIMITS(clipplanes); i++) {
511             if (This->set.clipplane[i] && This->changed.clipplane[i]) {
512                 float clip[4];
513
514                 clip[0] = This->clipplane[i][0];
515                 clip[1] = This->clipplane[i][1];
516                 clip[2] = This->clipplane[i][2];
517                 clip[3] = This->clipplane[i][3];
518                 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
519             }
520         }
521
522         /* Render */
523         for (i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
524             if (This->set.renderState[i] && This->changed.renderState[i])
525                 IWineD3DDevice_SetRenderState(pDevice, i, This->renderState[i]);
526         }
527
528         /* Texture states */
529         for (j = 0; j < GL_LIMITS(texture_stages); j++) { /* Set The texture first, just in case it resets the states? */
530             /* TODO: move over to memcpy */
531             for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE; i++) {
532                 if (This->set.textureState[j][i] && This->changed.textureState[j][i]) { /* tb_dx9_10 failes without this test */
533                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][i]         = This->textureState[j][i];
534                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->set.textureState[j][i]     = TRUE;
535                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[j][i] = TRUE;
536                 }
537             }
538         }
539
540         /* Samplers */
541         /* TODO: move over to memcpy */
542         for (j = 0 ; j < GL_LIMITS(sampler_stages); j++){
543             if (This->set.textures[j] && This->changed.textures[j]) {
544                 IWineD3DDevice_SetTexture(pDevice, j, This->textures[j]);
545             }
546             for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; i++){
547                 if (This->set.samplerState[j][i] && This->changed.samplerState[j][i]) {
548                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][i]         = This->samplerState[j][i];
549                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->set.samplerState[j][i]     = TRUE;
550                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[j][i] = TRUE;
551                 }
552             }
553
554         }
555
556     } else if (This->blockType == WINED3DSBT_PIXELSTATE) {
557
558         for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
559             if (This->set.renderState[SavedPixelStates_R[i]] && This->changed.renderState[SavedPixelStates_R[i]])
560                 IWineD3DDevice_SetRenderState(pDevice, SavedPixelStates_R[i], This->renderState[SavedPixelStates_R[i]]);
561
562         }
563
564         for (j = 0; j < GL_LIMITS(texture_stages); j++) {
565             for (i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
566                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedPixelStates_T[i]] = This->textureState[j][SavedPixelStates_T[i]];
567             }
568         }
569
570         for (j = 0; j < GL_LIMITS(sampler_stages); j++) {
571             for (i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
572                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedPixelStates_S[i]] = This->samplerState[j][SavedPixelStates_S[i]];
573             }
574         }
575
576     } else if (This->blockType == WINED3DSBT_VERTEXSTATE) {
577
578         for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
579             if ( This->set.renderState[SavedVertexStates_R[i]] && This->changed.renderState[SavedVertexStates_R[i]])
580                 IWineD3DDevice_SetRenderState(pDevice, SavedVertexStates_R[i], This->renderState[SavedVertexStates_R[i]]);
581         }
582
583         for (j = 0; j < GL_LIMITS(texture_stages); j++) {
584             for (i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
585                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedVertexStates_T[i]] = This->textureState[j][SavedVertexStates_T[i]];
586             }
587         }
588
589         for (j = 0; j < GL_LIMITS(sampler_stages); j++) {
590             for (i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
591                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedVertexStates_S[i]] = This->samplerState[j][SavedVertexStates_S[i]];
592             }
593         }
594
595
596     } else {
597         FIXME("Unrecognized state block type %d\n", This->blockType);
598     }
599     memcpy(&((IWineD3DDeviceImpl*)pDevice)->stateBlock->changed, &This->changed, sizeof(((IWineD3DDeviceImpl*)pDevice)->stateBlock->changed));
600     TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
601
602     return WINED3D_OK;
603 }
604
605 static HRESULT  WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
606     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
607     IWineD3DDevice         *device = (IWineD3DDevice *)This->wineD3DDevice;
608     IWineD3DDeviceImpl     *ThisDevice = (IWineD3DDeviceImpl *)device;
609     union {
610         D3DLINEPATTERN lp;
611         DWORD d;
612     } lp;
613     union {
614         float f;
615         DWORD d;
616     } tmpfloat;
617     unsigned int i;
618
619     /* Note this may have a large overhead but it should only be executed
620        once, in order to initialize the complete state of the device and
621        all opengl equivalents                                            */
622     TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
623     /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
624     This->blockType = WINED3DSBT_INIT;
625
626     /* Set some of the defaults for lights, transforms etc */
627     memcpy(&This->transforms[D3DTS_PROJECTION], &identity, sizeof(identity));
628     memcpy(&This->transforms[D3DTS_VIEW], &identity, sizeof(identity));
629     for (i = 0; i < 256; ++i) {
630       memcpy(&This->transforms[D3DTS_WORLDMATRIX(i)], &identity, sizeof(identity));
631     }
632
633     TRACE("Render states\n");
634     /* Render states: */
635     if (ThisDevice->depthStencilBuffer != NULL) {
636        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       D3DZB_TRUE);
637     } else {
638        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       D3DZB_FALSE);
639     }
640     IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE,         D3DFILL_SOLID);
641     IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE,        D3DSHADE_GOURAUD);
642     lp.lp.wRepeatFactor = 0;
643     lp.lp.wLinePattern  = 0;
644     IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN,      lp.d);
645     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE,     TRUE);
646     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE,  FALSE);
647     IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL,        TRUE);
648     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND,         D3DBLEND_ONE);
649     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND,        D3DBLEND_ZERO);
650     IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE,         D3DCULL_CCW);
651     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC,            D3DCMP_LESSEQUAL);
652     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC,        D3DCMP_ALWAYS);
653     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF,         0xff); /*??*/
654     IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE,     FALSE);
655     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
656     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE,        FALSE);
657     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE,   FALSE);
658     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE,         0);
659     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR,         0);
660     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE,     D3DFOG_NONE);
661     tmpfloat.f = 0.0f;
662     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART,         tmpfloat.d);
663     tmpfloat.f = 1.0f;
664     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND,           tmpfloat.d);
665     tmpfloat.f = 1.0f;
666     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY,       tmpfloat.d);
667     IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS,    FALSE);
668     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS,            0);
669     IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE,   FALSE);
670     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE,    FALSE);
671     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL,      D3DSTENCILOP_KEEP);
672     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL,     D3DSTENCILOP_KEEP);
673     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS,      D3DSTENCILOP_KEEP);
674
675     /* Setting stencil func also uses values for stencil ref/mask, so manually set defaults
676      * so only a single call performed (and ensure defaults initialized before making that call)
677      *
678      * IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF, 0);
679      * IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK, 0xFFFFFFFF);
680      */
681     This->renderState[WINED3DRS_STENCILREF] = 0;
682     This->renderState[WINED3DRS_STENCILMASK] = 0xFFFFFFFF;
683     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC,      D3DCMP_ALWAYS);
684     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
685     IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR,    0xFFFFFFFF);
686     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
687     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
688     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
689     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
690     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
691     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
692     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
693     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
694     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING,                 TRUE);
695     IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING,                 TRUE);
696     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT,                  0);
697     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE,            D3DFOG_NONE);
698     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX,              TRUE);
699     IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER,              TRUE);
700     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS,         FALSE);
701     IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE,    D3DMCS_COLOR1);
702     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE,   D3DMCS_COLOR2);
703     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE,    D3DMCS_COLOR2);
704     IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE,   D3DMCS_MATERIAL);
705     IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND,              D3DVBF_DISABLE);
706     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE,          0);
707     IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
708     tmpfloat.f = 1.0f;
709     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE,                tmpfloat.d);
710     tmpfloat.f = 0.0f;
711     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN,            tmpfloat.d);
712     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE,        FALSE);
713     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE,         FALSE);
714     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A,             TRUE);
715     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B,             TRUE);
716     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C,             TRUE);
717     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS,     TRUE);
718     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK,          0xFFFFFFFF);
719     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE,           D3DPATCHEDGE_DISCRETE);
720     tmpfloat.f = 1.0f;
721     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS,            tmpfloat.d);
722     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN,        D3DDMT_DISABLE);
723     tmpfloat.f = 64.0f;
724     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX,            tmpfloat.d);
725     IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
726     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE,         0x0000000F);
727     tmpfloat.f = 0.0f;
728     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR,              tmpfloat.d);
729     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP,                  D3DBLENDOP_ADD);
730     IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE,           WINED3DDEGREE_CUBIC);
731     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE,             WINED3DDEGREE_LINEAR);
732     /* states new in d3d9 */
733     IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE,        FALSE);
734     IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS,      0);
735     tmpfloat.f = 1.0f;
736     IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL,     tmpfloat.d);
737     IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL,     tmpfloat.d);
738     IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE,    FALSE);
739     tmpfloat.f = 0.0f;
740     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X,           tmpfloat.d);
741     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y,           tmpfloat.d);
742     tmpfloat.f = 1.0f;
743     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z,           tmpfloat.d);
744     tmpfloat.f = 0.0f;
745     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W,           tmpfloat.d);
746     IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
747     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE,      FALSE);
748     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL,          D3DSTENCILOP_KEEP);
749     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL,         D3DSTENCILOP_KEEP);
750     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS,          D3DSTENCILOP_KEEP);
751     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC,          D3DCMP_ALWAYS);
752     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1,        0x0000000F);
753     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2,        0x0000000F);
754     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3,        0x0000000F);
755     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR,              0xFFFFFFFF);
756     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE,          0);
757     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS,                0);
758     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8,  0);
759     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9,  0);
760     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
761     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
762     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
763     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
764     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
765     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
766     IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
767     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA,            D3DBLEND_ONE);
768     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA,           D3DBLEND_ZERO);
769     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA,             D3DBLENDOP_ADD);
770
771     /* clipping status */
772     This->clip_status.ClipUnion = 0;
773     This->clip_status.ClipIntersection = 0xFFFFFFFF;
774
775     /* Texture Stage States - Put directly into state block, we will call function below */
776     for (i = 0; i < GL_LIMITS(texture_stages); i++) {
777         TRACE("Setting up default texture states for texture Stage %d\n", i);
778         memcpy(&This->transforms[D3DTS_TEXTURE0 + i], &identity, sizeof(identity));
779         This->textureState[i][D3DTSS_COLOROP               ] = (i==0)? D3DTOP_MODULATE :  D3DTOP_DISABLE;
780         This->textureState[i][D3DTSS_COLORARG1             ] = D3DTA_TEXTURE;
781         This->textureState[i][D3DTSS_COLORARG2             ] = D3DTA_CURRENT;
782         This->textureState[i][D3DTSS_ALPHAOP               ] = (i==0)? D3DTOP_SELECTARG1 :  D3DTOP_DISABLE;
783         This->textureState[i][D3DTSS_ALPHAARG1             ] = D3DTA_TEXTURE;
784         This->textureState[i][D3DTSS_ALPHAARG2             ] = D3DTA_CURRENT;
785         This->textureState[i][D3DTSS_BUMPENVMAT00          ] = (DWORD) 0.0;
786         This->textureState[i][D3DTSS_BUMPENVMAT01          ] = (DWORD) 0.0;
787         This->textureState[i][D3DTSS_BUMPENVMAT10          ] = (DWORD) 0.0;
788         This->textureState[i][D3DTSS_BUMPENVMAT11          ] = (DWORD) 0.0;
789         This->textureState[i][D3DTSS_TEXCOORDINDEX         ] = i;
790         This->textureState[i][D3DTSS_BUMPENVLSCALE         ] = (DWORD) 0.0;
791         This->textureState[i][D3DTSS_BUMPENVLOFFSET        ] = (DWORD) 0.0;
792         This->textureState[i][D3DTSS_TEXTURETRANSFORMFLAGS ] = D3DTTFF_DISABLE;
793         This->textureState[i][D3DTSS_ADDRESSW              ] = D3DTADDRESS_WRAP;
794         This->textureState[i][D3DTSS_COLORARG0             ] = D3DTA_CURRENT;
795         This->textureState[i][D3DTSS_ALPHAARG0             ] = D3DTA_CURRENT;
796         This->textureState[i][D3DTSS_RESULTARG             ] = D3DTA_CURRENT;
797     }
798
799         /* Sampler states*/
800     for (i = 0 ; i <  GL_LIMITS(sampler_stages); i++) {
801         TRACE("Setting up default samplers states for sampler %d\n", i);
802         This->samplerState[i][WINED3DSAMP_ADDRESSU         ] = D3DTADDRESS_WRAP;
803         This->samplerState[i][WINED3DSAMP_ADDRESSV         ] = D3DTADDRESS_WRAP;
804         This->samplerState[i][WINED3DSAMP_ADDRESSW         ] = D3DTADDRESS_WRAP;
805         This->samplerState[i][WINED3DSAMP_BORDERCOLOR      ] = 0x00;
806         This->samplerState[i][WINED3DSAMP_MAGFILTER        ] = WINED3DTEXF_POINT;
807         This->samplerState[i][WINED3DSAMP_MINFILTER        ] = WINED3DTEXF_POINT;
808         This->samplerState[i][WINED3DSAMP_MIPFILTER        ] = WINED3DTEXF_NONE;
809         This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS    ] = 0;
810         This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL      ] = 0;
811         This->samplerState[i][WINED3DSAMP_MAXANISOTROPY    ] = 1;
812         This->samplerState[i][WINED3DSAMP_SRGBTEXTURE      ] = 0; /* TODO: Gamma correction value*/
813         This->samplerState[i][WINED3DSAMP_ELEMENTINDEX     ] = 0; /* TODO: Indicates which element of a  multielement texture to use */
814         This->samplerState[i][WINED3DSAMP_DMAPOFFSET       ] = 256; /* TODO: Vertex offset in the presampled displacement map */
815     }
816
817     /* Under DirectX you can have texture stage operations even if no texture is
818        bound, whereas opengl will only do texture operations when a valid texture is
819        bound. We emulate this by creating dummy textures and binding them to each
820        texture stage, but disable all stages by default. Hence if a stage is enabled
821        then the default texture will kick in until replaced by a SetTexture call     */
822     if (!GL_SUPPORT(NV_REGISTER_COMBINERS)) {
823         ENTER_GL();
824
825         for (i = 0; i < GL_LIMITS(texture_stages); i++) {
826             GLubyte white = 255;
827
828             /* Note this avoids calling settexture, so pretend it has been called */
829             This->set.textures[i]     = TRUE;
830             This->changed.textures[i] = TRUE;
831             This->textures[i]         = NULL;
832
833             /* Make appropriate texture active */
834             if (GL_SUPPORT(ARB_MULTITEXTURE)) {
835                 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
836                 checkGLcall("glActiveTextureARB");
837             } else if (i > 0) {
838                 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
839             }
840
841             /* Generate an opengl texture name */
842             glGenTextures(1, &ThisDevice->dummyTextureName[i]);
843             checkGLcall("glGenTextures");
844             TRACE("Dummy Texture %d given name %d\n", i, ThisDevice->dummyTextureName[i]);
845
846             /* Generate a dummy 1d texture */
847             This->textureDimensions[i] = GL_TEXTURE_1D;
848             glBindTexture(GL_TEXTURE_1D, ThisDevice->dummyTextureName[i]);
849             checkGLcall("glBindTexture");
850
851             glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &white);
852             checkGLcall("glTexImage1D");
853 #if 1   /* TODO: move the setting texture states off to basetexture */
854             /* Reapply all the texture state information to this texture */
855             IWineD3DDevice_SetupTextureStates(device, i, i, REAPPLY_ALL);
856 #endif
857         }
858
859         LEAVE_GL();
860     }
861
862     /* Defaulting palettes - Note these are device wide but reinitialized here for convenience*/
863     for (i = 0; i < MAX_PALETTES; ++i) {
864       int j;
865       for (j = 0; j < 256; ++j) {
866         This->wineD3DDevice->palettes[i][j].peRed   = 0xFF;
867         This->wineD3DDevice->palettes[i][j].peGreen = 0xFF;
868         This->wineD3DDevice->palettes[i][j].peBlue  = 0xFF;
869         This->wineD3DDevice->palettes[i][j].peFlags = 0xFF;
870       }
871     }
872     This->wineD3DDevice->currentPalette = 0;
873
874     /* Set default GLSL program ID to 0.  We won't actually create one
875      * until the app sets a vertex or pixel shader */
876     This->shaderPrgId = 0;
877
878     TRACE("-----------------------> Device defaults now set up...\n");
879     return WINED3D_OK;
880 }
881
882 /**********************************************************
883  * IWineD3DStateBlock VTbl follows
884  **********************************************************/
885
886 const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
887 {
888     /* IUnknown */
889     IWineD3DStateBlockImpl_QueryInterface,
890     IWineD3DStateBlockImpl_AddRef,
891     IWineD3DStateBlockImpl_Release,
892     /* IWineD3DStateBlock */
893     IWineD3DStateBlockImpl_GetParent,
894     IWineD3DStateBlockImpl_GetDevice,
895     IWineD3DStateBlockImpl_Capture,
896     IWineD3DStateBlockImpl_Apply,
897     IWineD3DStateBlockImpl_InitStartupStateBlock
898 };