Fixed header dependencies to be fully compatible with the Windows
[wine] / dlls / d3d8 / stateblock.c
1 /*
2  * state block implementation
3  *
4  * Copyright 2002 Raphael Junqueira
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <math.h>
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "wingdi.h"
30 #include "wine/debug.h"
31
32 #include "d3d8_private.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
35
36 /* Used for CreateStateBlock */
37 #define NUM_SAVEDPIXELSTATES_R     38
38 #define NUM_SAVEDPIXELSTATES_T     27
39 #define NUM_SAVEDVERTEXSTATES_R    33
40 #define NUM_SAVEDVERTEXSTATES_T    2
41
42 /*
43  * Globals
44  */
45 extern DWORD SavedPixelStates_R[NUM_SAVEDPIXELSTATES_R];
46 extern DWORD SavedPixelStates_T[NUM_SAVEDPIXELSTATES_T];
47 extern DWORD SavedVertexStates_R[NUM_SAVEDVERTEXSTATES_R];
48 extern DWORD SavedVertexStates_T[NUM_SAVEDVERTEXSTATES_T];
49 static const float idmatrix[16] = {
50   1.0, 0.0, 0.0, 0.0,
51   0.0, 1.0, 0.0, 0.0,
52   0.0, 0.0, 1.0, 0.0,
53   0.0, 0.0, 0.0, 1.0
54 };
55
56 HRESULT WINAPI IDirect3DDeviceImpl_InitStartupStateBlock(IDirect3DDevice8Impl* This) {
57     D3DLINEPATTERN lp;
58     int i;
59     int j;
60     LPDIRECT3DDEVICE8 iface = (LPDIRECT3DDEVICE8) This;
61
62     /* Note this may have a large overhead but it should only be executed
63        once, in order to initialize the complete state of the device and 
64        all opengl equivalents                                            */
65     TRACE("-----------------------> Setting up device defaults...\n");
66     This->StateBlock->blockType = D3DSBT_ALL;
67
68     /* FIXME: Set some of the defaults for lights, transforms etc */
69     memcpy(&This->StateBlock->transforms[D3DTS_PROJECTION], &idmatrix, sizeof(idmatrix));
70     memcpy(&This->StateBlock->transforms[D3DTS_VIEW], &idmatrix, sizeof(idmatrix));
71     for (i = 0; i < 256; ++i) {
72       memcpy(&This->StateBlock->transforms[D3DTS_WORLDMATRIX(i)], &idmatrix, sizeof(idmatrix));
73     }
74  
75     /* Render states: */
76     if (This->PresentParms.EnableAutoDepthStencil) {
77        IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZENABLE, D3DZB_TRUE);
78     } else {
79        IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZENABLE, D3DZB_FALSE);
80     }
81     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FILLMODE, D3DFILL_SOLID);
82     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
83     lp.wRepeatFactor = 0; lp.wLinePattern = 0; IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_LINEPATTERN, (DWORD) &lp);
84     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZWRITEENABLE, TRUE);
85     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ALPHATESTENABLE, FALSE);
86     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_LASTPIXEL, TRUE);
87     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_SRCBLEND, D3DBLEND_ONE);
88     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_DESTBLEND, D3DBLEND_ZERO);
89     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_CULLMODE, D3DCULL_CCW);
90     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
91     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ALPHAFUNC, D3DCMP_ALWAYS);
92     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ALPHAREF, 0xff); /*??*/
93     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_DITHERENABLE, FALSE);
94     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ALPHABLENDENABLE, FALSE);
95     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGENABLE, FALSE);
96     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_SPECULARENABLE, FALSE);
97     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZVISIBLE, 0);
98     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGCOLOR, 0);
99     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGTABLEMODE, D3DFOG_NONE);
100     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGSTART, 0.0f);
101     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGEND, 1.0f);
102     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGDENSITY, 1.0f);
103     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_EDGEANTIALIAS, FALSE);
104     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZBIAS, 0);
105     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_RANGEFOGENABLE, FALSE);
106     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILENABLE, FALSE);
107     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
108     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
109     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);
110
111     /* Setting stencil func also uses values for stencil ref/mask, so manually set defaults
112      * so only a single call performed (and ensure defaults initialized before making that call)    
113      *
114      * IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILREF, 0);
115      * IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILMASK, 0xFFFFFFFF);
116      */
117     This->StateBlock->renderstate[D3DRS_STENCILREF] = 0;
118     This->StateBlock->renderstate[D3DRS_STENCILMASK] = 0xFFFFFFFF;
119     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
120     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
121     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_TEXTUREFACTOR, 0xFFFFFFFF);
122     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP0, 0);
123     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP1, 0);
124     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP2, 0);
125     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP3, 0);
126     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP4, 0);
127     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP5, 0);
128     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP6, 0);
129     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP7, 0);
130     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_CLIPPING, TRUE);
131     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_LIGHTING, TRUE);
132     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_AMBIENT, 0);
133     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
134     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_COLORVERTEX, TRUE);
135     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_LOCALVIEWER, TRUE);
136     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_NORMALIZENORMALS, FALSE);
137     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
138     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_SPECULARMATERIALSOURCE, D3DMCS_COLOR2);
139     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_COLOR2);
140     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
141     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_VERTEXBLEND, D3DVBF_DISABLE);
142     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_CLIPPLANEENABLE, 0);
143     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
144     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSIZE, 1.0f);
145     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSIZE_MIN, 0.0f);
146     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSPRITEENABLE, FALSE);
147     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSCALEENABLE, FALSE);
148     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSCALE_A, TRUE);
149     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSCALE_B, TRUE);
150     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSCALE_C, TRUE);
151     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_MULTISAMPLEANTIALIAS, TRUE);
152     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
153     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_PATCHEDGESTYLE, D3DPATCHEDGE_DISCRETE);
154     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_PATCHSEGMENTS, 1.0f);
155     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_DEBUGMONITORTOKEN, D3DDMT_DISABLE);
156     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSIZE_MAX, (DWORD) 64.0f);
157     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
158     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_COLORWRITEENABLE, 0x0000000F);
159     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_TWEENFACTOR, (DWORD) 0.0f);
160     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_BLENDOP, D3DBLENDOP_ADD);
161     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POSITIONORDER, D3DORDER_CUBIC);
162     IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_NORMALORDER, D3DORDER_LINEAR);
163
164     /* Texture Stage States - Put directly into state block, we will call function below */
165     for (i = 0; i < GL_LIMITS(textures); i++) {
166         memcpy(&This->StateBlock->transforms[D3DTS_TEXTURE0 + i], &idmatrix, sizeof(idmatrix));
167         This->StateBlock->texture_state[i][D3DTSS_COLOROP               ] = (i==0)? D3DTOP_MODULATE :  D3DTOP_DISABLE;
168         This->StateBlock->texture_state[i][D3DTSS_COLORARG1             ] = D3DTA_TEXTURE;
169         This->StateBlock->texture_state[i][D3DTSS_COLORARG2             ] = D3DTA_CURRENT;
170         This->StateBlock->texture_state[i][D3DTSS_ALPHAOP               ] = (i==0)? D3DTOP_SELECTARG1 :  D3DTOP_DISABLE;
171         This->StateBlock->texture_state[i][D3DTSS_ALPHAARG1             ] = D3DTA_TEXTURE;
172         This->StateBlock->texture_state[i][D3DTSS_ALPHAARG2             ] = D3DTA_CURRENT;
173         This->StateBlock->texture_state[i][D3DTSS_BUMPENVMAT00          ] = (DWORD) 0.0;
174         This->StateBlock->texture_state[i][D3DTSS_BUMPENVMAT01          ] = (DWORD) 0.0;
175         This->StateBlock->texture_state[i][D3DTSS_BUMPENVMAT10          ] = (DWORD) 0.0;
176         This->StateBlock->texture_state[i][D3DTSS_BUMPENVMAT11          ] = (DWORD) 0.0;
177         This->StateBlock->texture_state[i][D3DTSS_TEXCOORDINDEX         ] = i;
178         This->StateBlock->texture_state[i][D3DTSS_ADDRESSU              ] = D3DTADDRESS_WRAP;
179         This->StateBlock->texture_state[i][D3DTSS_ADDRESSV              ] = D3DTADDRESS_WRAP;
180         This->StateBlock->texture_state[i][D3DTSS_BORDERCOLOR           ] = 0x00;
181         This->StateBlock->texture_state[i][D3DTSS_MAGFILTER             ] = D3DTEXF_POINT;
182         This->StateBlock->texture_state[i][D3DTSS_MINFILTER             ] = D3DTEXF_POINT;
183         This->StateBlock->texture_state[i][D3DTSS_MIPFILTER             ] = D3DTEXF_NONE;
184         This->StateBlock->texture_state[i][D3DTSS_MIPMAPLODBIAS         ] = 0;
185         This->StateBlock->texture_state[i][D3DTSS_MAXMIPLEVEL           ] = 0;
186         This->StateBlock->texture_state[i][D3DTSS_MAXANISOTROPY         ] = 1;
187         This->StateBlock->texture_state[i][D3DTSS_BUMPENVLSCALE         ] = (DWORD) 0.0;
188         This->StateBlock->texture_state[i][D3DTSS_BUMPENVLOFFSET        ] = (DWORD) 0.0;
189         This->StateBlock->texture_state[i][D3DTSS_TEXTURETRANSFORMFLAGS ] = D3DTTFF_DISABLE;
190         This->StateBlock->texture_state[i][D3DTSS_ADDRESSW              ] = D3DTADDRESS_WRAP;
191         This->StateBlock->texture_state[i][D3DTSS_COLORARG0             ] = D3DTA_CURRENT;
192         This->StateBlock->texture_state[i][D3DTSS_ALPHAARG0             ] = D3DTA_CURRENT;
193         This->StateBlock->texture_state[i][D3DTSS_RESULTARG             ] = D3DTA_CURRENT;
194     }
195
196     /* Under DirectX you can have texture stage operations even if no texture is
197        bound, whereas opengl will only do texture operations when a valid texture is
198        bound. We emulate this by creating dummy textures and binding them to each
199        texture stage, but disable all stages by default. Hence if a stage is enabled
200        then the default texture will kick in until replaced by a SetTexture call     */
201
202     ENTER_GL();
203
204     for (i = 0; i < GL_LIMITS(textures); i++) {
205         GLubyte white = 255;
206
207         /* Note this avoids calling settexture, so pretend it has been called */
208         This->StateBlock->Set.textures[i] = TRUE;
209         This->StateBlock->Changed.textures[i] = TRUE;
210         This->StateBlock->textures[i] = NULL;
211
212         /* Make appropriate texture active */
213         if (GL_SUPPORT(ARB_MULTITEXTURE)) {
214 #if defined(GL_VERSION_1_3)
215             glActiveTexture(GL_TEXTURE0 + i);
216 #else
217             glActiveTextureARB(GL_TEXTURE0_ARB + i);
218 #endif
219             checkGLcall("glActiveTextureARB");
220         } else if (i > 0) {
221             FIXME("Program using multiple concurrent textures which this opengl implementation doesnt support\n");
222         }
223
224         /* Generate an opengl texture name */
225         glGenTextures(1, &This->dummyTextureName[i]);
226         checkGLcall("glGenTextures");
227         TRACE("Dummy Texture %d given name %d\n", i, This->dummyTextureName[i]);
228
229         /* Generate a dummy 1d texture */
230         This->StateBlock->textureDimensions[i] = GL_TEXTURE_1D;
231         glBindTexture(GL_TEXTURE_1D, This->dummyTextureName[i]);
232         checkGLcall("glBindTexture");
233
234         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &white); 
235         checkGLcall("glTexImage1D");
236
237         /* Reapply all the texture state information to this texture */
238         setupTextureStates(iface, i, REAPPLY_ALL);
239     }
240
241     LEAVE_GL();
242
243     /* defaulting palettes */
244     for (i = 0; i < MAX_PALETTES; ++i) {
245       for (j = 0; j < 256; ++j) {
246         This->palettes[i][j].peRed   = 0xFF;
247         This->palettes[i][j].peGreen = 0xFF;
248         This->palettes[i][j].peBlue  = 0xFF;
249         This->palettes[i][j].peFlags = 0xFF;
250       }
251     }
252     This->currentPalette = 0;
253
254     TRACE("-----------------------> Device defaults now set up...\n");
255
256     return D3D_OK;
257 }
258
259
260
261 HRESULT WINAPI IDirect3DDeviceImpl_CreateStateBlock(IDirect3DDevice8Impl* This, D3DSTATEBLOCKTYPE Type, IDirect3DStateBlockImpl** ppStateBlock) {
262   IDirect3DStateBlockImpl* object;
263   UINT i, j;
264
265   TRACE("(%p) : Type(%d)\n", This, Type);
266
267   /* Allocate Storage */
268   object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlockImpl));
269   if (object) {
270     if (NULL == This->StateBlock) { /** if it the main stateblock only do init and returns */
271       /*object->lpVtbl = &Direct3DStateBlock9_Vtbl;*/
272       object->device = This;
273       object->ref = 1;
274       object->blockType = Type;
275       This->StateBlock = object;
276       /* don't forget to init it calling InitStartupStateBlock */
277       return D3D_OK;
278     }
279     memcpy(object, This->StateBlock, sizeof(IDirect3DStateBlockImpl));
280   } else {
281     *ppStateBlock = (IDirect3DStateBlockImpl*) 0xFFFFFFFF;
282     return E_OUTOFMEMORY;
283   }
284   /*object->lpVtbl = &Direct3DStateBlock9_Vtbl;*/
285   object->device = This;
286   object->ref = 1;
287   object->blockType = Type;
288
289   TRACE("Updating changed flags appropriate for type %d\n", Type);
290
291   if (Type == D3DSBT_ALL) {
292     TRACE("ALL => Pretend everything has changed\n");
293     memset(&object->Changed, TRUE, sizeof(This->StateBlock->Changed));
294     
295   } else if (Type == D3DSBT_PIXELSTATE) {
296     
297     memset(&object->Changed, FALSE, sizeof(This->StateBlock->Changed));
298     
299     /* TODO: Pixel Shader Constants */
300     object->Changed.pixelShader = TRUE;
301     for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
302       object->Changed.renderstate[SavedPixelStates_R[i]] = TRUE;
303     }
304     for (j = 0; j < GL_LIMITS(textures); i++) {
305       for (i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
306         object->Changed.texture_state[j][SavedPixelStates_T[i]] = TRUE;
307       }
308     }
309     
310   } else if (Type == D3DSBT_VERTEXSTATE) {
311     
312     memset(&object->Changed, FALSE, sizeof(This->StateBlock->Changed));
313     
314     /* TODO: Vertex Shader Constants */
315     object->Changed.vertexShader = TRUE;
316     for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
317       object->Changed.renderstate[SavedVertexStates_R[i]] = TRUE;
318     }
319     for (j = 0; j < GL_LIMITS(textures); i++) {
320       for (i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
321         object->Changed.texture_state[j][SavedVertexStates_T[i]] = TRUE;
322       }
323     }
324     for (i = 0; i < GL_LIMITS(lights); i++) {
325       object->Changed.lightEnable[i] = TRUE;
326       object->Changed.lights[i] = TRUE;
327     }
328     
329   } else {
330     FIXME("Unrecognized state block type %d\n", Type);
331   }
332   TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, object);
333   
334   *ppStateBlock = object;
335   return D3D_OK;
336 }
337
338 /** yakkk temporary waiting for Release */
339 HRESULT WINAPI IDirect3DDeviceImpl_DeleteStateBlock(IDirect3DDevice8Impl* This, IDirect3DStateBlockImpl* pSB) {
340   TRACE("(%p) : freeing StateBlock %p\n", This, pSB);
341   HeapFree(GetProcessHeap(), 0, (void *)pSB);
342   return D3D_OK;
343 }
344
345 HRESULT WINAPI IDirect3DDeviceImpl_BeginStateBlock(IDirect3DDevice8Impl* This) {
346   IDirect3DStateBlockImpl* object;
347   
348   TRACE("(%p)\n", This);
349   
350   if (This->isRecordingState) {
351     TRACE("(%p) already recording! returning error\n", This);
352     return D3DERR_INVALIDCALL;
353   }
354
355   /* Allocate Storage */
356   object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlockImpl));
357   if (object) {
358   } else {
359     return E_OUTOFMEMORY;
360   }
361   /*object->lpVtbl = &Direct3DVextexShaderDeclaration8_Vtbl;*/
362   object->device = This;
363   object->ref = 1;
364   
365   This->isRecordingState = TRUE;
366   This->UpdateStateBlock = object;
367   
368   return D3D_OK;
369 }
370
371 HRESULT WINAPI IDirect3DDeviceImpl_EndStateBlock(IDirect3DDevice8Impl* This, IDirect3DStateBlockImpl** ppStateBlock) {
372   TRACE("(%p)\n", This);
373   
374   if (!This->isRecordingState) {
375     TRACE("(%p) not recording! returning error\n", This);
376     *ppStateBlock = NULL;
377     return D3DERR_INVALIDCALL;
378   }
379
380   This->UpdateStateBlock->blockType = D3DSBT_RECORDED;
381   *ppStateBlock = This->UpdateStateBlock;  /* FIXME: AddRef() */
382   This->isRecordingState = FALSE;
383   This->UpdateStateBlock = This->StateBlock;
384   
385   TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, *ppStateBlock);
386   return D3D_OK;
387 }
388
389 HRESULT  WINAPI  IDirect3DDeviceImpl_ApplyStateBlock(IDirect3DDevice8Impl* This, IDirect3DStateBlockImpl* pSB) {
390     UINT i;
391     UINT j;
392     LPDIRECT3DDEVICE8 iface = (LPDIRECT3DDEVICE8) This;
393
394     TRACE("(%p) : Applying state block %p ------------------v\n", This, pSB);
395
396     /* FIXME: Only apply applicable states not all states */
397
398     if (pSB->blockType == D3DSBT_RECORDED || pSB->blockType == D3DSBT_ALL || pSB->blockType == D3DSBT_VERTEXSTATE) {
399
400         for (i = 0; i < GL_LIMITS(lights); i++) {
401
402             if (pSB->Set.lightEnable[i] && pSB->Changed.lightEnable[i])
403                 IDirect3DDevice8Impl_LightEnable(iface, i, pSB->lightEnable[i]);
404             if (pSB->Set.lights[i] && pSB->Changed.lights[i])
405                 IDirect3DDevice8Impl_SetLight(iface, i, &pSB->lights[i]);
406         }
407
408         if (pSB->Set.vertexShader && pSB->Changed.vertexShader)
409             IDirect3DDevice8Impl_SetVertexShader(iface, pSB->VertexShader);
410
411         /* TODO: Vertex Shader Constants */
412     }
413
414     if (pSB->blockType == D3DSBT_RECORDED || pSB->blockType == D3DSBT_ALL || pSB->blockType == D3DSBT_PIXELSTATE) {
415
416         if (pSB->Set.pixelShader && pSB->Changed.pixelShader)
417             IDirect3DDevice8Impl_SetPixelShader(iface, pSB->PixelShader);
418
419         /* TODO: Pixel Shader Constants */
420     }
421
422     /* Others + Render & Texture */
423     if (pSB->blockType == D3DSBT_RECORDED || pSB->blockType == D3DSBT_ALL) {
424         for (i = 0; i < HIGHEST_TRANSFORMSTATE; i++) {
425             if (pSB->Set.transform[i] && pSB->Changed.transform[i])
426                 IDirect3DDevice8Impl_SetTransform(iface, i, &pSB->transforms[i]);
427         }
428
429         if (pSB->Set.Indices && pSB->Changed.Indices)
430             IDirect3DDevice8Impl_SetIndices(iface, pSB->pIndexData, pSB->baseVertexIndex);
431
432         if (pSB->Set.material && pSB->Changed.material)
433             IDirect3DDevice8Impl_SetMaterial(iface, &pSB->material);
434
435         if (pSB->Set.viewport && pSB->Changed.viewport)
436             IDirect3DDevice8Impl_SetViewport(iface, &pSB->viewport);
437
438         for (i=0; i<MAX_STREAMS; i++) {
439             if (pSB->Set.stream_source[i] && pSB->Changed.stream_source[i])
440                 IDirect3DDevice8Impl_SetStreamSource(iface, i, pSB->stream_source[i], pSB->stream_stride[i]);
441         }
442
443         for (i = 0; i < GL_LIMITS(clipplanes); i++) {
444             if (pSB->Set.clipplane[i] && pSB->Changed.clipplane[i]) {
445                 float clip[4];
446
447                 clip[0] = pSB->clipplane[i][0];
448                 clip[1] = pSB->clipplane[i][1];
449                 clip[2] = pSB->clipplane[i][2];
450                 clip[3] = pSB->clipplane[i][3];
451                 IDirect3DDevice8Impl_SetClipPlane(iface, i, clip);
452             }
453         }
454
455         /* Render */
456         for (i = 0; i < HIGHEST_RENDER_STATE; i++) {
457             if (pSB->Set.renderstate[i] && pSB->Changed.renderstate[i])
458                 IDirect3DDevice8Impl_SetRenderState(iface, i, pSB->renderstate[i]);
459         }
460
461         /* Texture */
462         for (j = 0; j < GL_LIMITS(textures); j++) {
463           for (i = 0; i < HIGHEST_TEXTURE_STATE; i++) {
464             if (pSB->Set.texture_state[j][i] && pSB->Changed.texture_state[j][i]) {
465               IDirect3DDevice8Impl_SetTextureStageState(iface, j, i, pSB->texture_state[j][i]);
466             }
467           } 
468           if (pSB->Set.textures[j] && pSB->Changed.textures[j]) {
469             IDirect3DDevice8Impl_SetTexture(iface, j, pSB->textures[j]);
470           } 
471         }
472
473
474     } else if (pSB->blockType == D3DSBT_PIXELSTATE) {
475
476         for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
477             if (pSB->Set.renderstate[SavedPixelStates_R[i]] && pSB->Changed.renderstate[SavedPixelStates_R[i]])
478                 IDirect3DDevice8Impl_SetRenderState(iface, SavedPixelStates_R[i], pSB->renderstate[SavedPixelStates_R[i]]);
479
480         }
481
482         for (j = 0; j < GL_LIMITS(textures); i++) {
483             for (i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
484                 if (pSB->Set.texture_state[j][SavedPixelStates_T[i]] &&
485                     pSB->Changed.texture_state[j][SavedPixelStates_T[i]])
486                     IDirect3DDevice8Impl_SetTextureStageState(iface, j, SavedPixelStates_T[i], pSB->texture_state[j][SavedPixelStates_T[i]]);
487             }
488         }
489
490     } else if (pSB->blockType == D3DSBT_VERTEXSTATE) {
491
492         for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
493             if (pSB->Set.renderstate[SavedVertexStates_R[i]] && pSB->Changed.renderstate[SavedVertexStates_R[i]])
494                 IDirect3DDevice8Impl_SetRenderState(iface, SavedVertexStates_R[i], pSB->renderstate[SavedVertexStates_R[i]]);
495         }
496
497         for (j = 0; j < GL_LIMITS(textures); i++) {
498             for (i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
499                 if (pSB->Set.texture_state[j][SavedVertexStates_T[i]] &&
500                     pSB->Changed.texture_state[j][SavedVertexStates_T[i]])
501                     IDirect3DDevice8Impl_SetTextureStageState(iface, j, SavedVertexStates_T[i], pSB->texture_state[j][SavedVertexStates_T[i]]);
502             }
503         }
504
505
506     } else {
507         FIXME("Unrecognized state block type %d\n", pSB->blockType);
508     }
509     memcpy(&This->StateBlock->Changed, &pSB->Changed, sizeof(This->StateBlock->Changed));
510     TRACE("(%p) : Applied state block %p ------------------^\n", This, pSB);
511
512     return D3D_OK;
513 }
514
515 HRESULT WINAPI IDirect3DDeviceImpl_CaptureStateBlock(IDirect3DDevice8Impl* This, IDirect3DStateBlockImpl* updateBlock) {
516     LPDIRECT3DDEVICE8 iface = (LPDIRECT3DDEVICE8) This;
517
518     TRACE("(%p) : Updating state block %p ------------------v \n", This, updateBlock);
519
520     /* If not recorded, then update can just recapture */
521     if (updateBlock->blockType != D3DSBT_RECORDED) {
522         IDirect3DStateBlockImpl* tmpBlock;
523         IDirect3DDeviceImpl_CreateStateBlock(This, updateBlock->blockType, &tmpBlock);
524         memcpy(updateBlock, tmpBlock, sizeof(IDirect3DStateBlockImpl));
525         IDirect3DDeviceImpl_DeleteStateBlock(This, tmpBlock);
526
527         /* FIXME: This will record states of new lights! May need to have and save set_lights
528            across this action */
529
530     } else {
531         int i, j;
532
533         /* Recorded => Only update 'changed' values */
534         if (updateBlock->Set.vertexShader && updateBlock->VertexShader != This->StateBlock->VertexShader) {
535             updateBlock->VertexShader = This->StateBlock->VertexShader;
536             TRACE("Updating vertex shader to %ld\n", This->StateBlock->VertexShader);
537         }
538
539         /* TODO: Vertex Shader Constants */
540
541         for (i = 0; i < GL_LIMITS(lights); i++) {
542           if (updateBlock->Set.lightEnable[i] && This->StateBlock->lightEnable[i] != updateBlock->lightEnable[i]) {
543               TRACE("Updating light enable for light %d to %d\n", i, This->StateBlock->lightEnable[i]);
544               updateBlock->lightEnable[i] = This->StateBlock->lightEnable[i];
545           }
546
547           if (updateBlock->Set.lights[i] && memcmp(&This->StateBlock->lights[i], 
548                                                    &updateBlock->lights[i], 
549                                                    sizeof(D3DLIGHT8)) != 0) {
550               TRACE("Updating lights for light %d\n", i);
551               memcpy(&updateBlock->lights[i], &This->StateBlock->lights[i], sizeof(D3DLIGHT8));
552           }
553         }
554
555         if (updateBlock->Set.pixelShader && updateBlock->PixelShader != This->StateBlock->PixelShader) {
556             TRACE("Updating pixel shader to %ld\n", This->StateBlock->PixelShader);
557             updateBlock->lights[i] = This->StateBlock->lights[i];
558             IDirect3DDevice8Impl_SetVertexShader(iface, updateBlock->PixelShader);
559         }
560
561         /* TODO: Pixel Shader Constants */
562
563         /* Others + Render & Texture */
564         for (i = 0; i < HIGHEST_TRANSFORMSTATE; i++) {
565           if (updateBlock->Set.transform[i] && memcmp(&This->StateBlock->transforms[i], 
566                                                       &updateBlock->transforms[i], 
567                                                       sizeof(D3DMATRIX)) != 0) {
568             TRACE("Updating transform %d\n", i);
569             memcpy(&updateBlock->transforms[i], &This->StateBlock->transforms[i], sizeof(D3DMATRIX));
570           }
571         }
572
573         if (updateBlock->Set.Indices && ((updateBlock->pIndexData != This->StateBlock->pIndexData)
574                                          || (updateBlock->baseVertexIndex != This->StateBlock->baseVertexIndex))) {
575           TRACE("Updating pindexData to %p, baseVertexIndex to %d\n", 
576                 This->StateBlock->pIndexData, This->StateBlock->baseVertexIndex);
577           updateBlock->pIndexData = This->StateBlock->pIndexData;
578           updateBlock->baseVertexIndex = This->StateBlock->baseVertexIndex;
579         }
580
581        if (updateBlock->Set.material && memcmp(&This->StateBlock->material, 
582                                                    &updateBlock->material, 
583                                                    sizeof(D3DMATERIAL8)) != 0) {
584                 TRACE("Updating material\n");
585                 memcpy(&updateBlock->material, &This->StateBlock->material, sizeof(D3DMATERIAL8));
586        }
587            
588        if (updateBlock->Set.viewport && memcmp(&This->StateBlock->viewport, 
589                                                    &updateBlock->viewport, 
590                                                    sizeof(D3DVIEWPORT8)) != 0) {
591                 TRACE("Updating viewport\n");
592                 memcpy(&updateBlock->viewport, &This->StateBlock->viewport, sizeof(D3DVIEWPORT8));
593        }
594
595        for (i = 0; i < MAX_STREAMS; i++) {
596            if (updateBlock->Set.stream_source[i] && 
597                            ((updateBlock->stream_stride[i] != This->StateBlock->stream_stride[i]) ||
598                            (updateBlock->stream_source[i] != This->StateBlock->stream_source[i]))) {
599                TRACE("Updating stream source %d to %p, stride to %d\n", i, This->StateBlock->stream_source[i], 
600                                                                         This->StateBlock->stream_stride[i]);
601                updateBlock->stream_stride[i] = This->StateBlock->stream_stride[i];
602                updateBlock->stream_source[i] = This->StateBlock->stream_source[i];
603            }
604        }
605
606        for (i = 0; i < GL_LIMITS(clipplanes); i++) {
607            if (updateBlock->Set.clipplane[i] && memcmp(&This->StateBlock->clipplane[i], 
608                                                        &updateBlock->clipplane[i], 
609                                                        sizeof(updateBlock->clipplane)) != 0) {
610
611                TRACE("Updating clipplane %d\n", i);
612                memcpy(&updateBlock->clipplane[i], &This->StateBlock->clipplane[i], 
613                                        sizeof(updateBlock->clipplane));
614            }
615        }
616
617        /* Render */
618        for (i = 0; i < HIGHEST_RENDER_STATE; i++) {
619
620            if (updateBlock->Set.renderstate[i] && (updateBlock->renderstate[i] != 
621                                                        This->StateBlock->renderstate[i])) {
622                TRACE("Updating renderstate %d to %ld\n", i, This->StateBlock->renderstate[i]);
623                updateBlock->renderstate[i] = This->StateBlock->renderstate[i];
624            }
625        }
626
627        /* Texture */
628        for (j = 0; j < GL_LIMITS(textures); j++) {
629            for (i = 0; i < HIGHEST_TEXTURE_STATE; i++) {
630
631                if (updateBlock->Set.texture_state[j][i] && (updateBlock->texture_state[j][i] != 
632                                                                 This->StateBlock->texture_state[j][i])) {
633                    TRACE("Updating texturestagestate %d,%d to %ld (was %ld)\n", j,i, This->StateBlock->texture_state[j][i], 
634                                updateBlock->texture_state[j][i]);
635                    updateBlock->texture_state[j][i] =  This->StateBlock->texture_state[j][i];
636                }
637
638                if (updateBlock->Set.textures[j] && (updateBlock->textures[j] != This->StateBlock->textures[j])) {
639                    TRACE("Updating texture %d to %p (was %p)\n", j, This->StateBlock->textures[j],  updateBlock->textures[j]);
640                    updateBlock->textures[j] =  This->StateBlock->textures[j];
641                }
642            }
643
644        }
645     }
646
647     TRACE("(%p) : Updated state block %p ------------------^\n", This, updateBlock);
648
649     return D3D_OK;
650 }
651
652 DWORD SavedPixelStates_R[NUM_SAVEDPIXELSTATES_R] = {
653     D3DRS_ALPHABLENDENABLE   ,
654     D3DRS_ALPHAFUNC          ,
655     D3DRS_ALPHAREF           ,
656     D3DRS_ALPHATESTENABLE    ,
657     D3DRS_BLENDOP            ,
658     D3DRS_COLORWRITEENABLE   ,
659     D3DRS_DESTBLEND          ,
660     D3DRS_DITHERENABLE       ,
661     D3DRS_EDGEANTIALIAS      ,
662     D3DRS_FILLMODE           ,
663     D3DRS_FOGDENSITY         ,
664     D3DRS_FOGEND             ,
665     D3DRS_FOGSTART           ,
666     D3DRS_LASTPIXEL          ,
667     D3DRS_LINEPATTERN        ,
668     D3DRS_SHADEMODE          ,
669     D3DRS_SRCBLEND           ,
670     D3DRS_STENCILENABLE      ,
671     D3DRS_STENCILFAIL        ,
672     D3DRS_STENCILFUNC        ,
673     D3DRS_STENCILMASK        ,
674     D3DRS_STENCILPASS        ,
675     D3DRS_STENCILREF         ,
676     D3DRS_STENCILWRITEMASK   ,
677     D3DRS_STENCILZFAIL       ,
678     D3DRS_TEXTUREFACTOR      ,
679     D3DRS_WRAP0              ,
680     D3DRS_WRAP1              ,
681     D3DRS_WRAP2              ,
682     D3DRS_WRAP3              ,
683     D3DRS_WRAP4              ,
684     D3DRS_WRAP5              ,
685     D3DRS_WRAP6              ,
686     D3DRS_WRAP7              ,
687     D3DRS_ZBIAS              ,
688     D3DRS_ZENABLE            ,
689     D3DRS_ZFUNC              ,
690     D3DRS_ZWRITEENABLE
691 };
692
693 DWORD SavedPixelStates_T[NUM_SAVEDPIXELSTATES_T] = {
694     D3DTSS_ADDRESSU              ,
695     D3DTSS_ADDRESSV              ,
696     D3DTSS_ADDRESSW              ,
697     D3DTSS_ALPHAARG0             ,
698     D3DTSS_ALPHAARG1             ,
699     D3DTSS_ALPHAARG2             ,
700     D3DTSS_ALPHAOP               ,
701     D3DTSS_BORDERCOLOR           ,
702     D3DTSS_BUMPENVLOFFSET        ,
703     D3DTSS_BUMPENVLSCALE         ,
704     D3DTSS_BUMPENVMAT00          ,
705     D3DTSS_BUMPENVMAT01          ,
706     D3DTSS_BUMPENVMAT10          ,
707     D3DTSS_BUMPENVMAT11          ,
708     D3DTSS_COLORARG0             ,
709     D3DTSS_COLORARG1             ,
710     D3DTSS_COLORARG2             ,
711     D3DTSS_COLOROP               ,
712     D3DTSS_MAGFILTER             ,
713     D3DTSS_MAXANISOTROPY         ,
714     D3DTSS_MAXMIPLEVEL           ,
715     D3DTSS_MINFILTER             ,
716     D3DTSS_MIPFILTER             ,
717     D3DTSS_MIPMAPLODBIAS         ,
718     D3DTSS_RESULTARG             ,
719     D3DTSS_TEXCOORDINDEX         ,
720     D3DTSS_TEXTURETRANSFORMFLAGS
721 };
722
723 DWORD SavedVertexStates_R[NUM_SAVEDVERTEXSTATES_R] = {
724     D3DRS_AMBIENT                       ,
725     D3DRS_AMBIENTMATERIALSOURCE         ,
726     D3DRS_CLIPPING                      ,
727     D3DRS_CLIPPLANEENABLE               ,
728     D3DRS_COLORVERTEX                   ,
729     D3DRS_DIFFUSEMATERIALSOURCE         ,
730     D3DRS_EMISSIVEMATERIALSOURCE        ,
731     D3DRS_FOGDENSITY                    ,
732     D3DRS_FOGEND                        ,
733     D3DRS_FOGSTART                      ,
734     D3DRS_FOGTABLEMODE                  ,
735     D3DRS_FOGVERTEXMODE                 ,
736     D3DRS_INDEXEDVERTEXBLENDENABLE      ,
737     D3DRS_LIGHTING                      ,
738     D3DRS_LOCALVIEWER                   ,
739     D3DRS_MULTISAMPLEANTIALIAS          ,
740     D3DRS_MULTISAMPLEMASK               ,
741     D3DRS_NORMALIZENORMALS              ,
742     D3DRS_PATCHEDGESTYLE                ,
743     D3DRS_PATCHSEGMENTS                 ,
744     D3DRS_POINTSCALE_A                  ,
745     D3DRS_POINTSCALE_B                  ,
746     D3DRS_POINTSCALE_C                  ,
747     D3DRS_POINTSCALEENABLE              ,
748     D3DRS_POINTSIZE                     ,
749     D3DRS_POINTSIZE_MAX                 ,
750     D3DRS_POINTSIZE_MIN                 ,
751     D3DRS_POINTSPRITEENABLE             ,
752     D3DRS_RANGEFOGENABLE                ,
753     D3DRS_SOFTWAREVERTEXPROCESSING      ,
754     D3DRS_SPECULARMATERIALSOURCE        ,
755     D3DRS_TWEENFACTOR                   ,
756     D3DRS_VERTEXBLEND
757 };
758
759 DWORD SavedVertexStates_T[NUM_SAVEDVERTEXSTATES_T] = {
760     D3DTSS_TEXCOORDINDEX         ,
761     D3DTSS_TEXTURETRANSFORMFLAGS
762 };