wined3d: Fix compilation (VBO constants).
[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(textures); counter++) {
94                 if (This->textures[counter]) {
95                     /* release our 'internal' hold on the texture */
96                     if(0 != IWineD3DBaseTexture_Release(This->textures[counter])) {
97                         TRACE("Texture still referenced by stateblock, applications has leaked Stage = %u Texture = %p\n", counter, This->textures[counter]);
98                     }
99                 }
100             }
101
102         }
103         HeapFree(GetProcessHeap(), 0, This);
104     }
105     return refCount;
106 }
107
108 /**********************************************************
109  * IWineD3DStateBlockImpl parts follows
110  **********************************************************/
111 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         /* FIXME: textures are up to MAX_SAMPLERS for d3d9? */
378         /* Texture */
379         for (j = 0; j < GL_LIMITS(textures); j++) {
380             /* TODO: move over to using memcpy */
381             for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE ; i++) {
382                 if (This->set.textureState[j][i]) {
383                     TRACE("Updating texturestagestate %d,%d to %ld (was %ld)\n", j,i, targetStateBlock->textureState[j][i],
384                     This->textureState[j][i]);
385                     This->textureState[j][i]         =  targetStateBlock->textureState[j][i];
386                 }
387             }
388
389             if (This->set.textures[j]) {
390                 TRACE("Updating texture %d to %p (was %p)\n", j, targetStateBlock->textures[j],  This->textures[j]);
391                 This->textures[j] = targetStateBlock->textures[j];
392             }
393
394         }
395
396         /* Samplers */
397         /* TODO: move over to using memcpy */
398         for (j = 0 ; j < GL_LIMITS(samplers); j++){
399             for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE ; i++){ /* States are 1 based */
400                 if (This->set.samplerState[j][i]) {
401                     TRACE("Updating sampler state %d,%d to %ld (was %ld)\n",
402                     j, i, targetStateBlock->samplerState[j][i],
403                     This->samplerState[j][i]);
404                     This->samplerState[j][i]         = targetStateBlock->samplerState[j][i];
405                 }
406             }
407         }
408     }
409
410     TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
411
412     return WINED3D_OK;
413 }
414
415 static HRESULT  WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
416     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
417     IWineD3DDevice*        pDevice     = (IWineD3DDevice*)This->wineD3DDevice;
418
419 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
420 should really perform a delta so that only the changes get updated*/
421
422
423     UINT i;
424     UINT j;
425
426     TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
427
428     /* FIXME: Only apply applicable states not all states */
429
430     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */This->blockType == WINED3DSBT_INIT || This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_VERTEXSTATE) {
431
432
433         PLIGHTINFOEL *toDo = This->lights;
434         while (toDo != NULL) {
435             if (toDo->changed)
436                   IWineD3DDevice_SetLight(pDevice, toDo->OriginalIndex, &toDo->OriginalParms);
437             if (toDo->enabledChanged)
438                   IWineD3DDevice_SetLightEnable(pDevice, toDo->OriginalIndex, toDo->lightEnabled);
439             toDo = toDo->next;
440         }
441
442         /* Vertex Shader */
443         if (This->set.vertexShader && This->changed.vertexShader) {
444             IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
445         }
446
447         /* Vertex Shader Constants */
448         for (i = 0; i < MAX_VSHADER_CONSTANTS; ++i) {
449             if (This->set.vertexShaderConstantsF[i] && This->changed.vertexShaderConstantsF[i])
450                 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i, This->vertexShaderConstantF + i * 4, 1);
451
452             if (This->set.vertexShaderConstantsI[i] && This->changed.vertexShaderConstantsI[i])
453                 IWineD3DDevice_SetVertexShaderConstantI(pDevice, i, This->vertexShaderConstantI + i * 4, 1);
454
455             if (This->set.vertexShaderConstantsB[i] && This->changed.vertexShaderConstantsB[i])
456                 IWineD3DDevice_SetVertexShaderConstantB(pDevice, i, This->vertexShaderConstantB + i, 1);
457         }
458     }
459
460     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == D3DSBT_ALL || This->blockType == D3DSBT_PIXELSTATE) {
461
462         /* Pixel Shader */
463         if (This->set.pixelShader && This->changed.pixelShader) {
464             IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
465         }
466
467         /* Pixel Shader Constants */
468         for (i = 0; i < MAX_PSHADER_CONSTANTS; ++i) {
469             if (This->set.pixelShaderConstantsF[i] && This->changed.pixelShaderConstantsF[i])
470                 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i, This->pixelShaderConstantF + i * 4, 1);
471
472             if (This->set.pixelShaderConstantsI[i] && This->changed.pixelShaderConstantsI[i])
473                 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i, This->pixelShaderConstantI + i * 4, 1);
474
475             if (This->set.pixelShaderConstantsB[i] && This->changed.pixelShaderConstantsB[i])
476                 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i, This->pixelShaderConstantB + i, 1);
477         }
478     }
479
480     if (This->set.fvf && This->changed.fvf) {
481         IWineD3DDevice_SetFVF(pDevice, This->fvf);
482     }
483
484     if (This->set.vertexDecl && This->changed.vertexDecl) {
485         IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
486     }
487
488     /* Others + Render & Texture */
489     if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_INIT) {
490         for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
491             if (This->set.transform[i] && This->changed.transform[i])
492                 IWineD3DDevice_SetTransform(pDevice, i, &This->transforms[i]);
493         }
494
495         if (This->set.indices && This->changed.indices)
496             IWineD3DDevice_SetIndices(pDevice, This->pIndexData, This->baseVertexIndex);
497
498         if (This->set.material && This->changed.material )
499             IWineD3DDevice_SetMaterial(pDevice, &This->material);
500
501         if (This->set.viewport && This->changed.viewport)
502             IWineD3DDevice_SetViewport(pDevice, &This->viewport);
503
504         /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
505         for (i=0; i<MAX_STREAMS; i++) {
506             if (This->set.streamSource[i] && This->changed.streamSource[i])
507                 IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
508
509             if (This->set.streamFreq[i] && This->changed.streamFreq[i])
510                 IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
511         }
512
513         for (i = 0; i < GL_LIMITS(clipplanes); i++) {
514             if (This->set.clipplane[i] && This->changed.clipplane[i]) {
515                 float clip[4];
516
517                 clip[0] = This->clipplane[i][0];
518                 clip[1] = This->clipplane[i][1];
519                 clip[2] = This->clipplane[i][2];
520                 clip[3] = This->clipplane[i][3];
521                 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
522             }
523         }
524
525         /* Render */
526         for (i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
527             if (This->set.renderState[i] && This->changed.renderState[i])
528                 IWineD3DDevice_SetRenderState(pDevice, i, This->renderState[i]);
529         }
530
531         /* FIXME: Texture are set against samplers... not just TextureStages */
532         /* Texture */
533         for (j = 0; j < GL_LIMITS(textures); j++) { /* Set The texture first, just in case it resets the states? */
534             if (This->set.textures[j] && This->changed.textures[j]) {
535                 IWineD3DDevice_SetTexture(pDevice, j, This->textures[j]);
536             }
537             /* TODO: move over to memcpy */
538             for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE; i++) {
539                 if (This->set.textureState[j][i] && This->changed.textureState[j][i]) { /* tb_dx9_10 failes without this test */
540                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][i]         = This->textureState[j][i];
541                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->set.textureState[j][i]     = TRUE;
542                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[j][i] = TRUE;
543                 }
544             }
545         }
546
547         /* Samplers */
548         /* TODO: move over to memcpy */
549         for (j = 0 ; j < GL_LIMITS(samplers); j++){
550             for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; i++){
551                 if (This->set.samplerState[j][i] && This->changed.samplerState[j][i]) {
552                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][i]         = This->samplerState[j][i];
553                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->set.samplerState[j][i]     = TRUE;
554                     ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[j][i] = TRUE;
555                 }
556             }
557
558         }
559
560     } else if (This->blockType == WINED3DSBT_PIXELSTATE) {
561
562         for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
563             if (This->set.renderState[SavedPixelStates_R[i]] && This->changed.renderState[SavedPixelStates_R[i]])
564                 IWineD3DDevice_SetRenderState(pDevice, SavedPixelStates_R[i], This->renderState[SavedPixelStates_R[i]]);
565
566         }
567
568         for (j = 0; j < GL_LIMITS(textures); j++) {
569             for (i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
570                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedPixelStates_T[i]] = This->textureState[j][SavedPixelStates_T[i]];
571             }
572         }
573
574         for (j = 0; j < GL_LIMITS(samplers); j++) {
575             for (i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
576                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedPixelStates_S[i]] = This->samplerState[j][SavedPixelStates_S[i]];
577             }
578         }
579
580     } else if (This->blockType == WINED3DSBT_VERTEXSTATE) {
581
582         for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
583             if ( This->set.renderState[SavedVertexStates_R[i]] && This->changed.renderState[SavedVertexStates_R[i]])
584                 IWineD3DDevice_SetRenderState(pDevice, SavedVertexStates_R[i], This->renderState[SavedVertexStates_R[i]]);
585         }
586
587         for (j = 0; j < GL_LIMITS(textures); j++) {
588             for (i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
589                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedVertexStates_T[i]] = This->textureState[j][SavedVertexStates_T[i]];
590             }
591         }
592
593         for (j = 0; j < GL_LIMITS(textures); j++) {
594             for (i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
595                 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedVertexStates_S[i]] = This->samplerState[j][SavedVertexStates_S[i]];
596             }
597         }
598
599
600     } else {
601         FIXME("Unrecognized state block type %d\n", This->blockType);
602     }
603     memcpy(&((IWineD3DDeviceImpl*)pDevice)->stateBlock->changed, &This->changed, sizeof(((IWineD3DDeviceImpl*)pDevice)->stateBlock->changed));
604     TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
605
606     return WINED3D_OK;
607 }
608
609 static HRESULT  WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
610     IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
611     IWineD3DDevice         *device = (IWineD3DDevice *)This->wineD3DDevice;
612     IWineD3DDeviceImpl     *ThisDevice = (IWineD3DDeviceImpl *)device;
613     union {
614         D3DLINEPATTERN lp;
615         DWORD d;
616     } lp;
617     union {
618         float f;
619         DWORD d;
620     } tmpfloat;
621     unsigned int i;
622
623     /* Note this may have a large overhead but it should only be executed
624        once, in order to initialize the complete state of the device and
625        all opengl equivalents                                            */
626     TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
627     /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
628     This->blockType = WINED3DSBT_INIT;
629
630     /* Set some of the defaults for lights, transforms etc */
631     memcpy(&This->transforms[D3DTS_PROJECTION], &identity, sizeof(identity));
632     memcpy(&This->transforms[D3DTS_VIEW], &identity, sizeof(identity));
633     for (i = 0; i < 256; ++i) {
634       memcpy(&This->transforms[D3DTS_WORLDMATRIX(i)], &identity, sizeof(identity));
635     }
636
637     TRACE("Render states\n");
638     /* Render states: */
639     if (ThisDevice->depthStencilBuffer != NULL) {
640        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       D3DZB_TRUE);
641     } else {
642        IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE,       D3DZB_FALSE);
643     }
644     IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE,         D3DFILL_SOLID);
645     IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE,        D3DSHADE_GOURAUD);
646     lp.lp.wRepeatFactor = 0;
647     lp.lp.wLinePattern  = 0;
648     IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN,      lp.d);
649     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE,     TRUE);
650     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE,  FALSE);
651     IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL,        TRUE);
652     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND,         D3DBLEND_ONE);
653     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND,        D3DBLEND_ZERO);
654     IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE,         D3DCULL_CCW);
655     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC,            D3DCMP_LESSEQUAL);
656     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC,        D3DCMP_ALWAYS);
657     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF,         0xff); /*??*/
658     IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE,     FALSE);
659     IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
660     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE,        FALSE);
661     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE,   FALSE);
662     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE,         0);
663     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR,         0);
664     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE,     D3DFOG_NONE);
665     tmpfloat.f = 0.0f;
666     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART,         tmpfloat.d);
667     tmpfloat.f = 1.0f;
668     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND,           tmpfloat.d);
669     tmpfloat.f = 1.0f;
670     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY,       tmpfloat.d);
671     IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS,    FALSE);
672     IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS,            0);
673     IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE,   FALSE);
674     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE,    FALSE);
675     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL,      D3DSTENCILOP_KEEP);
676     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL,     D3DSTENCILOP_KEEP);
677     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS,      D3DSTENCILOP_KEEP);
678
679     /* Setting stencil func also uses values for stencil ref/mask, so manually set defaults
680      * so only a single call performed (and ensure defaults initialized before making that call)
681      *
682      * IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF, 0);
683      * IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK, 0xFFFFFFFF);
684      */
685     This->renderState[WINED3DRS_STENCILREF] = 0;
686     This->renderState[WINED3DRS_STENCILMASK] = 0xFFFFFFFF;
687     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC,      D3DCMP_ALWAYS);
688     IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
689     IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR,    0xFFFFFFFF);
690     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
691     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
692     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
693     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
694     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
695     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
696     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
697     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
698     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING,                 TRUE);
699     IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING,                 TRUE);
700     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT,                  0);
701     IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE,            D3DFOG_NONE);
702     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX,              TRUE);
703     IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER,              TRUE);
704     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS,         FALSE);
705     IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE,    D3DMCS_COLOR1);
706     IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE,   D3DMCS_COLOR2);
707     IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE,    D3DMCS_COLOR2);
708     IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE,   D3DMCS_MATERIAL);
709     IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND,              D3DVBF_DISABLE);
710     IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE,          0);
711     IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
712     tmpfloat.f = 1.0f;
713     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE,                tmpfloat.d);
714     tmpfloat.f = 0.0f;
715     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN,            tmpfloat.d);
716     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE,        FALSE);
717     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE,         FALSE);
718     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A,             TRUE);
719     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B,             TRUE);
720     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C,             TRUE);
721     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS,     TRUE);
722     IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK,          0xFFFFFFFF);
723     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE,           D3DPATCHEDGE_DISCRETE);
724     tmpfloat.f = 1.0f;
725     IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS,            tmpfloat.d);
726     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN,        D3DDMT_DISABLE);
727     tmpfloat.f = 64.0f;
728     IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX,            tmpfloat.d);
729     IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
730     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE,         0x0000000F);
731     tmpfloat.f = 0.0f;
732     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR,              tmpfloat.d);
733     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP,                  D3DBLENDOP_ADD);
734     IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE,           WINED3DDEGREE_CUBIC);
735     IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE,             WINED3DDEGREE_LINEAR);
736     /* states new in d3d9 */
737     IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE,        FALSE);
738     IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS,      0);
739     tmpfloat.f = 1.0f;
740     IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL,     tmpfloat.d);
741     IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL,     tmpfloat.d);
742     IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE,    FALSE);
743     tmpfloat.f = 0.0f;
744     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X,           tmpfloat.d);
745     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y,           tmpfloat.d);
746     tmpfloat.f = 1.0f;
747     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z,           tmpfloat.d);
748     tmpfloat.f = 0.0f;
749     IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W,           tmpfloat.d);
750     IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
751     IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE,      FALSE);
752     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL,          D3DSTENCILOP_KEEP);
753     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL,         D3DSTENCILOP_KEEP);
754     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS,          D3DSTENCILOP_KEEP);
755     IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC,          D3DCMP_ALWAYS);
756     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1,        0x0000000F);
757     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2,        0x0000000F);
758     IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3,        0x0000000F);
759     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR,              0xFFFFFFFF);
760     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE,          0);
761     IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS,                0);
762     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8,  0);
763     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9,  0);
764     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
765     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
766     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
767     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
768     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
769     IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
770     IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
771     IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA,            D3DBLEND_ONE);
772     IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA,           D3DBLEND_ZERO);
773     IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA,             D3DBLENDOP_ADD);
774
775     /* clipping status */
776     This->clip_status.ClipUnion = 0;
777     This->clip_status.ClipIntersection = 0xFFFFFFFF;
778
779     /* Texture Stage States - Put directly into state block, we will call function below */
780     for (i = 0; i < GL_LIMITS(textures); i++) {
781         TRACE("Setting up default texture states for texture Stage %d\n", i);
782         memcpy(&This->transforms[D3DTS_TEXTURE0 + i], &identity, sizeof(identity));
783         This->textureState[i][D3DTSS_COLOROP               ] = (i==0)? D3DTOP_MODULATE :  D3DTOP_DISABLE;
784         This->textureState[i][D3DTSS_COLORARG1             ] = D3DTA_TEXTURE;
785         This->textureState[i][D3DTSS_COLORARG2             ] = D3DTA_CURRENT;
786         This->textureState[i][D3DTSS_ALPHAOP               ] = (i==0)? D3DTOP_SELECTARG1 :  D3DTOP_DISABLE;
787         This->textureState[i][D3DTSS_ALPHAARG1             ] = D3DTA_TEXTURE;
788         This->textureState[i][D3DTSS_ALPHAARG2             ] = D3DTA_CURRENT;
789         This->textureState[i][D3DTSS_BUMPENVMAT00          ] = (DWORD) 0.0;
790         This->textureState[i][D3DTSS_BUMPENVMAT01          ] = (DWORD) 0.0;
791         This->textureState[i][D3DTSS_BUMPENVMAT10          ] = (DWORD) 0.0;
792         This->textureState[i][D3DTSS_BUMPENVMAT11          ] = (DWORD) 0.0;
793         This->textureState[i][D3DTSS_TEXCOORDINDEX         ] = i;
794         This->textureState[i][D3DTSS_BUMPENVLSCALE         ] = (DWORD) 0.0;
795         This->textureState[i][D3DTSS_BUMPENVLOFFSET        ] = (DWORD) 0.0;
796         This->textureState[i][D3DTSS_TEXTURETRANSFORMFLAGS ] = D3DTTFF_DISABLE;
797         This->textureState[i][D3DTSS_ADDRESSW              ] = D3DTADDRESS_WRAP;
798         This->textureState[i][D3DTSS_COLORARG0             ] = D3DTA_CURRENT;
799         This->textureState[i][D3DTSS_ALPHAARG0             ] = D3DTA_CURRENT;
800         This->textureState[i][D3DTSS_RESULTARG             ] = D3DTA_CURRENT;
801     }
802
803         /* Sampler states*/
804     for (i = 0 ; i <  GL_LIMITS(samplers); i++) {
805         TRACE("Setting up default samplers states for sampler %d\n", i);
806         This->samplerState[i][WINED3DSAMP_ADDRESSU         ] = D3DTADDRESS_WRAP;
807         This->samplerState[i][WINED3DSAMP_ADDRESSV         ] = D3DTADDRESS_WRAP;
808         This->samplerState[i][WINED3DSAMP_ADDRESSW         ] = D3DTADDRESS_WRAP;
809         This->samplerState[i][WINED3DSAMP_BORDERCOLOR      ] = 0x00;
810         This->samplerState[i][WINED3DSAMP_MAGFILTER        ] = WINED3DTEXF_POINT;
811         This->samplerState[i][WINED3DSAMP_MINFILTER        ] = WINED3DTEXF_POINT;
812         This->samplerState[i][WINED3DSAMP_MIPFILTER        ] = WINED3DTEXF_NONE;
813         This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS    ] = 0;
814         This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL      ] = 0;
815         This->samplerState[i][WINED3DSAMP_MAXANISOTROPY    ] = 1;
816         This->samplerState[i][WINED3DSAMP_SRGBTEXTURE      ] = 0; /* TODO: Gamma correction value*/
817         This->samplerState[i][WINED3DSAMP_ELEMENTINDEX     ] = 0; /* TODO: Indicates which element of a  multielement texture to use */
818         This->samplerState[i][WINED3DSAMP_DMAPOFFSET       ] = 256; /* TODO: Vertex offset in the presampled displacement map */
819     }
820
821     /* Under DirectX you can have texture stage operations even if no texture is
822        bound, whereas opengl will only do texture operations when a valid texture is
823        bound. We emulate this by creating dummy textures and binding them to each
824        texture stage, but disable all stages by default. Hence if a stage is enabled
825        then the default texture will kick in until replaced by a SetTexture call     */
826
827     ENTER_GL();
828
829     for (i = 0; i < GL_LIMITS(textures); i++) {
830         GLubyte white = 255;
831
832         /* Note this avoids calling settexture, so pretend it has been called */
833         This->set.textures[i]     = TRUE;
834         This->changed.textures[i] = TRUE;
835         This->textures[i]         = NULL;
836
837         /* Make appropriate texture active */
838         if (GL_SUPPORT(ARB_MULTITEXTURE)) {
839             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
840             checkGLcall("glActiveTextureARB");
841         } else if (i > 0) {
842             FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
843         }
844
845         /* Generate an opengl texture name */
846         glGenTextures(1, &ThisDevice->dummyTextureName[i]);
847         checkGLcall("glGenTextures");
848         TRACE("Dummy Texture %d given name %d\n", i, ThisDevice->dummyTextureName[i]);
849
850         /* Generate a dummy 1d texture */
851         This->textureDimensions[i] = GL_TEXTURE_1D;
852         glBindTexture(GL_TEXTURE_1D, ThisDevice->dummyTextureName[i]);
853         checkGLcall("glBindTexture");
854
855         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &white);
856         checkGLcall("glTexImage1D");
857 #if 1   /* TODO: move the setting texture states off to basetexture */
858         /* Reapply all the texture state information to this texture */
859         IWineD3DDevice_SetupTextureStates(device, i, REAPPLY_ALL);
860 #endif
861     }
862
863     LEAVE_GL();
864
865     /* Defaulting palettes - Note these are device wide but reinitialized here for convenience*/
866     for (i = 0; i < MAX_PALETTES; ++i) {
867       int j;
868       for (j = 0; j < 256; ++j) {
869         This->wineD3DDevice->palettes[i][j].peRed   = 0xFF;
870         This->wineD3DDevice->palettes[i][j].peGreen = 0xFF;
871         This->wineD3DDevice->palettes[i][j].peBlue  = 0xFF;
872         This->wineD3DDevice->palettes[i][j].peFlags = 0xFF;
873       }
874     }
875     This->wineD3DDevice->currentPalette = 0;
876
877     /* Set default GLSL program ID to 0.  We won't actually create one
878      * until the app sets a vertex or pixel shader */
879     This->shaderPrgId = 0;
880
881     TRACE("-----------------------> Device defaults now set up...\n");
882     return WINED3D_OK;
883 }
884
885 /**********************************************************
886  * IWineD3DStateBlock VTbl follows
887  **********************************************************/
888
889 const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
890 {
891     /* IUnknown */
892     IWineD3DStateBlockImpl_QueryInterface,
893     IWineD3DStateBlockImpl_AddRef,
894     IWineD3DStateBlockImpl_Release,
895     /* IWineD3DStateBlock */
896     IWineD3DStateBlockImpl_GetParent,
897     IWineD3DStateBlockImpl_GetDevice,
898     IWineD3DStateBlockImpl_Capture,
899     IWineD3DStateBlockImpl_Apply,
900     IWineD3DStateBlockImpl_InitStartupStateBlock
901 };