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