2 * state block implementation
4 * Copyright 2002 Raphael Junqueira
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.
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.
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
30 #include "wine/debug.h"
32 #include "d3d8_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
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
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] = {
56 HRESULT WINAPI IDirect3DDeviceImpl_InitStartupStateBlock(IDirect3DDevice8Impl* This) {
67 LPDIRECT3DDEVICE8 iface = (LPDIRECT3DDEVICE8) This;
69 /* Note this may have a large overhead but it should only be executed
70 once, in order to initialize the complete state of the device and
71 all opengl equivalents */
72 TRACE("-----------------------> Setting up device defaults...\n");
73 This->StateBlock->blockType = D3DSBT_ALL;
75 /* FIXME: Set some of the defaults for lights, transforms etc */
76 memcpy(&This->StateBlock->transforms[D3DTS_PROJECTION], &idmatrix, sizeof(idmatrix));
77 memcpy(&This->StateBlock->transforms[D3DTS_VIEW], &idmatrix, sizeof(idmatrix));
78 for (i = 0; i < 256; ++i) {
79 memcpy(&This->StateBlock->transforms[D3DTS_WORLDMATRIX(i)], &idmatrix, sizeof(idmatrix));
83 if (This->PresentParms.EnableAutoDepthStencil) {
84 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZENABLE, D3DZB_TRUE);
86 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZENABLE, D3DZB_FALSE);
88 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FILLMODE, D3DFILL_SOLID);
89 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
91 lp.lp.wRepeatFactor = 0; lp.lp.wLinePattern = 0;
92 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_LINEPATTERN, lp.d);
93 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZWRITEENABLE, TRUE);
94 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ALPHATESTENABLE, FALSE);
95 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_LASTPIXEL, TRUE);
96 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_SRCBLEND, D3DBLEND_ONE);
97 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_DESTBLEND, D3DBLEND_ZERO);
98 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_CULLMODE, D3DCULL_CCW);
99 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
100 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ALPHAFUNC, D3DCMP_ALWAYS);
101 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ALPHAREF, 0xff); /*??*/
102 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_DITHERENABLE, FALSE);
103 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ALPHABLENDENABLE, FALSE);
104 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGENABLE, FALSE);
105 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_SPECULARENABLE, FALSE);
106 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZVISIBLE, 0);
107 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGCOLOR, 0);
108 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGTABLEMODE, D3DFOG_NONE);
110 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGSTART, tmpfloat.d);
112 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGEND, tmpfloat.d);
114 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGDENSITY, tmpfloat.d);
115 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_EDGEANTIALIAS, FALSE);
116 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZBIAS, 0);
117 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_RANGEFOGENABLE, FALSE);
118 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILENABLE, FALSE);
119 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
120 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
121 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);
123 /* Setting stencil func also uses values for stencil ref/mask, so manually set defaults
124 * so only a single call performed (and ensure defaults initialized before making that call)
126 * IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILREF, 0);
127 * IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILMASK, 0xFFFFFFFF);
129 This->StateBlock->renderstate[D3DRS_STENCILREF] = 0;
130 This->StateBlock->renderstate[D3DRS_STENCILMASK] = 0xFFFFFFFF;
131 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
132 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
133 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_TEXTUREFACTOR, 0xFFFFFFFF);
134 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP0, 0);
135 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP1, 0);
136 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP2, 0);
137 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP3, 0);
138 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP4, 0);
139 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP5, 0);
140 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP6, 0);
141 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_WRAP7, 0);
142 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_CLIPPING, TRUE);
143 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_LIGHTING, TRUE);
144 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_AMBIENT, 0);
145 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
146 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_COLORVERTEX, TRUE);
147 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_LOCALVIEWER, TRUE);
148 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_NORMALIZENORMALS, FALSE);
149 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
150 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_SPECULARMATERIALSOURCE, D3DMCS_COLOR2);
151 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_COLOR2);
152 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
153 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_VERTEXBLEND, D3DVBF_DISABLE);
154 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_CLIPPLANEENABLE, 0);
155 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
157 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSIZE, tmpfloat.d);
159 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSIZE_MIN, tmpfloat.d);
160 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSPRITEENABLE, FALSE);
161 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSCALEENABLE, FALSE);
162 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSCALE_A, TRUE);
163 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSCALE_B, TRUE);
164 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSCALE_C, TRUE);
165 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_MULTISAMPLEANTIALIAS, TRUE);
166 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
167 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_PATCHEDGESTYLE, D3DPATCHEDGE_DISCRETE);
169 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_PATCHSEGMENTS, tmpfloat.d);
170 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_DEBUGMONITORTOKEN, D3DDMT_DISABLE);
172 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POINTSIZE_MAX, tmpfloat.d);
173 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
174 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_COLORWRITEENABLE, 0x0000000F);
176 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_TWEENFACTOR, tmpfloat.d);
177 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_BLENDOP, D3DBLENDOP_ADD);
178 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_POSITIONORDER, D3DORDER_CUBIC);
179 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_NORMALORDER, D3DORDER_LINEAR);
181 /* Texture Stage States - Put directly into state block, we will call function below */
182 for (i = 0; i < GL_LIMITS(textures); i++) {
183 memcpy(&This->StateBlock->transforms[D3DTS_TEXTURE0 + i], &idmatrix, sizeof(idmatrix));
184 This->StateBlock->texture_state[i][D3DTSS_COLOROP ] = (i==0)? D3DTOP_MODULATE : D3DTOP_DISABLE;
185 This->StateBlock->texture_state[i][D3DTSS_COLORARG1 ] = D3DTA_TEXTURE;
186 This->StateBlock->texture_state[i][D3DTSS_COLORARG2 ] = D3DTA_CURRENT;
187 This->StateBlock->texture_state[i][D3DTSS_ALPHAOP ] = (i==0)? D3DTOP_SELECTARG1 : D3DTOP_DISABLE;
188 This->StateBlock->texture_state[i][D3DTSS_ALPHAARG1 ] = D3DTA_TEXTURE;
189 This->StateBlock->texture_state[i][D3DTSS_ALPHAARG2 ] = D3DTA_CURRENT;
190 This->StateBlock->texture_state[i][D3DTSS_BUMPENVMAT00 ] = (DWORD) 0.0;
191 This->StateBlock->texture_state[i][D3DTSS_BUMPENVMAT01 ] = (DWORD) 0.0;
192 This->StateBlock->texture_state[i][D3DTSS_BUMPENVMAT10 ] = (DWORD) 0.0;
193 This->StateBlock->texture_state[i][D3DTSS_BUMPENVMAT11 ] = (DWORD) 0.0;
194 This->StateBlock->texture_state[i][D3DTSS_TEXCOORDINDEX ] = i;
195 This->StateBlock->texture_state[i][D3DTSS_ADDRESSU ] = D3DTADDRESS_WRAP;
196 This->StateBlock->texture_state[i][D3DTSS_ADDRESSV ] = D3DTADDRESS_WRAP;
197 This->StateBlock->texture_state[i][D3DTSS_BORDERCOLOR ] = 0x00;
198 This->StateBlock->texture_state[i][D3DTSS_MAGFILTER ] = D3DTEXF_POINT;
199 This->StateBlock->texture_state[i][D3DTSS_MINFILTER ] = D3DTEXF_POINT;
200 This->StateBlock->texture_state[i][D3DTSS_MIPFILTER ] = D3DTEXF_NONE;
201 This->StateBlock->texture_state[i][D3DTSS_MIPMAPLODBIAS ] = 0;
202 This->StateBlock->texture_state[i][D3DTSS_MAXMIPLEVEL ] = 0;
203 This->StateBlock->texture_state[i][D3DTSS_MAXANISOTROPY ] = 1;
204 This->StateBlock->texture_state[i][D3DTSS_BUMPENVLSCALE ] = (DWORD) 0.0;
205 This->StateBlock->texture_state[i][D3DTSS_BUMPENVLOFFSET ] = (DWORD) 0.0;
206 This->StateBlock->texture_state[i][D3DTSS_TEXTURETRANSFORMFLAGS ] = D3DTTFF_DISABLE;
207 This->StateBlock->texture_state[i][D3DTSS_ADDRESSW ] = D3DTADDRESS_WRAP;
208 This->StateBlock->texture_state[i][D3DTSS_COLORARG0 ] = D3DTA_CURRENT;
209 This->StateBlock->texture_state[i][D3DTSS_ALPHAARG0 ] = D3DTA_CURRENT;
210 This->StateBlock->texture_state[i][D3DTSS_RESULTARG ] = D3DTA_CURRENT;
213 /* Under DirectX you can have texture stage operations even if no texture is
214 bound, whereas opengl will only do texture operations when a valid texture is
215 bound. We emulate this by creating dummy textures and binding them to each
216 texture stage, but disable all stages by default. Hence if a stage is enabled
217 then the default texture will kick in until replaced by a SetTexture call */
221 for (i = 0; i < GL_LIMITS(textures); i++) {
224 /* Note this avoids calling settexture, so pretend it has been called */
225 This->StateBlock->Set.textures[i] = TRUE;
226 This->StateBlock->Changed.textures[i] = TRUE;
227 This->StateBlock->textures[i] = NULL;
229 /* Make appropriate texture active */
230 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
231 #if defined(GL_VERSION_1_3)
232 glActiveTexture(GL_TEXTURE0 + i);
234 glActiveTextureARB(GL_TEXTURE0_ARB + i);
236 checkGLcall("glActiveTextureARB");
238 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
241 /* Generate an opengl texture name */
242 glGenTextures(1, &This->dummyTextureName[i]);
243 checkGLcall("glGenTextures");
244 TRACE("Dummy Texture %d given name %d\n", i, This->dummyTextureName[i]);
246 /* Generate a dummy 1d texture */
247 This->StateBlock->textureDimensions[i] = GL_TEXTURE_1D;
248 glBindTexture(GL_TEXTURE_1D, This->dummyTextureName[i]);
249 checkGLcall("glBindTexture");
251 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &white);
252 checkGLcall("glTexImage1D");
254 /* Reapply all the texture state information to this texture */
255 setupTextureStates(iface, i, REAPPLY_ALL);
260 /* defaulting palettes */
261 for (i = 0; i < MAX_PALETTES; ++i) {
262 for (j = 0; j < 256; ++j) {
263 This->palettes[i][j].peRed = 0xFF;
264 This->palettes[i][j].peGreen = 0xFF;
265 This->palettes[i][j].peBlue = 0xFF;
266 This->palettes[i][j].peFlags = 0xFF;
269 This->currentPalette = 0;
271 TRACE("-----------------------> Device defaults now set up...\n");
278 HRESULT WINAPI IDirect3DDeviceImpl_CreateStateBlock(IDirect3DDevice8Impl* This, D3DSTATEBLOCKTYPE Type, IDirect3DStateBlockImpl** ppStateBlock) {
279 IDirect3DStateBlockImpl* object;
282 TRACE("(%p) : Type(%d)\n", This, Type);
284 /* Allocate Storage */
285 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlockImpl));
287 if (NULL == This->StateBlock) { /** if it the main stateblock only do init and returns */
288 /*object->lpVtbl = &Direct3DStateBlock9_Vtbl;*/
289 object->device = This;
291 object->blockType = Type;
292 This->StateBlock = object;
293 /* don't forget to init it calling InitStartupStateBlock */
296 memcpy(object, This->StateBlock, sizeof(IDirect3DStateBlockImpl));
298 *ppStateBlock = (IDirect3DStateBlockImpl*) 0xFFFFFFFF;
299 return E_OUTOFMEMORY;
301 /*object->lpVtbl = &Direct3DStateBlock9_Vtbl;*/
302 object->device = This;
304 object->blockType = Type;
306 TRACE("Updating changed flags appropriate for type %d\n", Type);
308 if (Type == D3DSBT_ALL) {
309 TRACE("ALL => Pretend everything has changed\n");
310 memset(&object->Changed, TRUE, sizeof(This->StateBlock->Changed));
312 } else if (Type == D3DSBT_PIXELSTATE) {
314 memset(&object->Changed, FALSE, sizeof(This->StateBlock->Changed));
316 /* TODO: Pixel Shader Constants */
317 object->Changed.pixelShader = TRUE;
318 for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
319 object->Changed.renderstate[SavedPixelStates_R[i]] = TRUE;
321 for (j = 0; j < GL_LIMITS(textures); i++) {
322 for (i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
323 object->Changed.texture_state[j][SavedPixelStates_T[i]] = TRUE;
327 } else if (Type == D3DSBT_VERTEXSTATE) {
329 memset(&object->Changed, FALSE, sizeof(This->StateBlock->Changed));
331 /* TODO: Vertex Shader Constants */
332 object->Changed.vertexShader = TRUE;
333 for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
334 object->Changed.renderstate[SavedVertexStates_R[i]] = TRUE;
336 for (j = 0; j < GL_LIMITS(textures); i++) {
337 for (i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
338 object->Changed.texture_state[j][SavedVertexStates_T[i]] = TRUE;
342 /* Duplicate light chain */
344 PLIGHTINFOEL *src = NULL;
345 PLIGHTINFOEL *dst = NULL;
346 PLIGHTINFOEL *newEl = NULL;
348 src = This->StateBlock->lights;
349 object->lights = NULL;
352 newEl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PLIGHTINFOEL));
353 if (newEl == NULL) return D3DERR_OUTOFVIDEOMEMORY;
354 memcpy(newEl, src, sizeof(PLIGHTINFOEL));
356 newEl->changed = TRUE;
357 newEl->enabledChanged = TRUE;
359 object->lights = newEl;
369 FIXME("Unrecognized state block type %d\n", Type);
371 TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, object);
373 *ppStateBlock = object;
377 /** yakkk temporary waiting for Release */
378 HRESULT WINAPI IDirect3DDeviceImpl_DeleteStateBlock(IDirect3DDevice8Impl* This, IDirect3DStateBlockImpl* pSB) {
381 TRACE("(%p) : freeing StateBlock %p\n", This, pSB);
383 if (tmp) tmp = tmp->next;
384 while (tmp != NULL) {
385 HeapFree(GetProcessHeap(), 0, (void *)(tmp->prev));
388 if (tmp) HeapFree(GetProcessHeap(), 0, (void *)tmp);
389 HeapFree(GetProcessHeap(), 0, (void *)pSB);
393 HRESULT WINAPI IDirect3DDeviceImpl_BeginStateBlock(IDirect3DDevice8Impl* This) {
394 IDirect3DStateBlockImpl* object;
396 TRACE("(%p)\n", This);
398 if (This->isRecordingState) {
399 TRACE("(%p) already recording! returning error\n", This);
400 return D3DERR_INVALIDCALL;
403 /* Allocate Storage */
404 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlockImpl));
407 return E_OUTOFMEMORY;
409 /*object->lpVtbl = &Direct3DVextexShaderDeclaration8_Vtbl;*/
410 object->device = This;
413 This->isRecordingState = TRUE;
414 This->UpdateStateBlock = object;
419 HRESULT WINAPI IDirect3DDeviceImpl_EndStateBlock(IDirect3DDevice8Impl* This, IDirect3DStateBlockImpl** ppStateBlock) {
420 TRACE("(%p)\n", This);
422 if (!This->isRecordingState) {
423 TRACE("(%p) not recording! returning error\n", This);
424 *ppStateBlock = NULL;
425 return D3DERR_INVALIDCALL;
428 This->UpdateStateBlock->blockType = D3DSBT_RECORDED;
429 *ppStateBlock = This->UpdateStateBlock; /* FIXME: AddRef() */
430 This->isRecordingState = FALSE;
431 This->UpdateStateBlock = This->StateBlock;
433 TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, *ppStateBlock);
437 HRESULT WINAPI IDirect3DDeviceImpl_ApplyStateBlock(IDirect3DDevice8Impl* This, IDirect3DStateBlockImpl* pSB) {
440 LPDIRECT3DDEVICE8 iface = (LPDIRECT3DDEVICE8) This;
442 TRACE("(%p) : Applying state block %p ------------------v\n", This, pSB);
444 /* FIXME: Only apply applicable states not all states */
446 if (pSB->blockType == D3DSBT_RECORDED || pSB->blockType == D3DSBT_ALL || pSB->blockType == D3DSBT_VERTEXSTATE) {
448 PLIGHTINFOEL *toDo = pSB->lights;
449 while (toDo != NULL) {
451 IDirect3DDevice8Impl_SetLight(iface, toDo->OriginalIndex, &toDo->OriginalParms);
452 if (toDo->enabledChanged)
453 IDirect3DDevice8Impl_LightEnable(iface, toDo->OriginalIndex, toDo->lightEnabled);
457 if (pSB->Set.vertexShader && pSB->Changed.vertexShader)
458 IDirect3DDevice8Impl_SetVertexShader(iface, pSB->VertexShader);
460 /* TODO: Vertex Shader Constants */
463 if (pSB->blockType == D3DSBT_RECORDED || pSB->blockType == D3DSBT_ALL || pSB->blockType == D3DSBT_PIXELSTATE) {
465 if (pSB->Set.pixelShader && pSB->Changed.pixelShader)
466 IDirect3DDevice8Impl_SetPixelShader(iface, pSB->PixelShader);
468 /* TODO: Pixel Shader Constants */
471 /* Others + Render & Texture */
472 if (pSB->blockType == D3DSBT_RECORDED || pSB->blockType == D3DSBT_ALL) {
473 for (i = 0; i < HIGHEST_TRANSFORMSTATE; i++) {
474 if (pSB->Set.transform[i] && pSB->Changed.transform[i])
475 IDirect3DDevice8Impl_SetTransform(iface, i, &pSB->transforms[i]);
478 if (pSB->Set.Indices && pSB->Changed.Indices)
479 IDirect3DDevice8Impl_SetIndices(iface, pSB->pIndexData, pSB->baseVertexIndex);
481 if (pSB->Set.material && pSB->Changed.material)
482 IDirect3DDevice8Impl_SetMaterial(iface, &pSB->material);
484 if (pSB->Set.viewport && pSB->Changed.viewport)
485 IDirect3DDevice8Impl_SetViewport(iface, &pSB->viewport);
487 for (i=0; i<MAX_STREAMS; i++) {
488 if (pSB->Set.stream_source[i] && pSB->Changed.stream_source[i])
489 IDirect3DDevice8Impl_SetStreamSource(iface, i, pSB->stream_source[i], pSB->stream_stride[i]);
492 for (i = 0; i < GL_LIMITS(clipplanes); i++) {
493 if (pSB->Set.clipplane[i] && pSB->Changed.clipplane[i]) {
496 clip[0] = pSB->clipplane[i][0];
497 clip[1] = pSB->clipplane[i][1];
498 clip[2] = pSB->clipplane[i][2];
499 clip[3] = pSB->clipplane[i][3];
500 IDirect3DDevice8Impl_SetClipPlane(iface, i, clip);
505 for (i = 0; i < HIGHEST_RENDER_STATE; i++) {
506 if (pSB->Set.renderstate[i] && pSB->Changed.renderstate[i])
507 IDirect3DDevice8Impl_SetRenderState(iface, i, pSB->renderstate[i]);
511 for (j = 0; j < GL_LIMITS(textures); j++) {
512 for (i = 0; i < HIGHEST_TEXTURE_STATE; i++) {
513 if (pSB->Set.texture_state[j][i] && pSB->Changed.texture_state[j][i]) {
514 IDirect3DDevice8Impl_SetTextureStageState(iface, j, i, pSB->texture_state[j][i]);
517 if (pSB->Set.textures[j] && pSB->Changed.textures[j]) {
518 IDirect3DDevice8Impl_SetTexture(iface, j, pSB->textures[j]);
523 } else if (pSB->blockType == D3DSBT_PIXELSTATE) {
525 for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
526 if (pSB->Set.renderstate[SavedPixelStates_R[i]] && pSB->Changed.renderstate[SavedPixelStates_R[i]])
527 IDirect3DDevice8Impl_SetRenderState(iface, SavedPixelStates_R[i], pSB->renderstate[SavedPixelStates_R[i]]);
531 for (j = 0; j < GL_LIMITS(textures); i++) {
532 for (i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
533 if (pSB->Set.texture_state[j][SavedPixelStates_T[i]] &&
534 pSB->Changed.texture_state[j][SavedPixelStates_T[i]])
535 IDirect3DDevice8Impl_SetTextureStageState(iface, j, SavedPixelStates_T[i], pSB->texture_state[j][SavedPixelStates_T[i]]);
539 } else if (pSB->blockType == D3DSBT_VERTEXSTATE) {
541 for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
542 if (pSB->Set.renderstate[SavedVertexStates_R[i]] && pSB->Changed.renderstate[SavedVertexStates_R[i]])
543 IDirect3DDevice8Impl_SetRenderState(iface, SavedVertexStates_R[i], pSB->renderstate[SavedVertexStates_R[i]]);
546 for (j = 0; j < GL_LIMITS(textures); i++) {
547 for (i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
548 if (pSB->Set.texture_state[j][SavedVertexStates_T[i]] &&
549 pSB->Changed.texture_state[j][SavedVertexStates_T[i]])
550 IDirect3DDevice8Impl_SetTextureStageState(iface, j, SavedVertexStates_T[i], pSB->texture_state[j][SavedVertexStates_T[i]]);
556 FIXME("Unrecognized state block type %d\n", pSB->blockType);
558 memcpy(&This->StateBlock->Changed, &pSB->Changed, sizeof(This->StateBlock->Changed));
559 TRACE("(%p) : Applied state block %p ------------------^\n", This, pSB);
564 HRESULT WINAPI IDirect3DDeviceImpl_CaptureStateBlock(IDirect3DDevice8Impl* This, IDirect3DStateBlockImpl* updateBlock) {
565 LPDIRECT3DDEVICE8 iface = (LPDIRECT3DDEVICE8) This;
568 TRACE("(%p) : Updating state block %p ------------------v \n", This, updateBlock);
570 /* If not recorded, then update can just recapture */
571 if (updateBlock->blockType != D3DSBT_RECORDED) {
572 IDirect3DStateBlockImpl* tmpBlock;
573 IDirect3DDeviceImpl_CreateStateBlock(This, updateBlock->blockType, &tmpBlock);
575 /* Note just swap the light chains over so when deleting, the old one goes */
576 tmp = updateBlock->lights;
577 memcpy(updateBlock, tmpBlock, sizeof(IDirect3DStateBlockImpl));
578 tmpBlock->lights = tmp;
580 /* Delete the temporary one (which points to the old light chain though */
581 IDirect3DDeviceImpl_DeleteStateBlock(This, tmpBlock);
587 /* Recorded => Only update 'changed' values */
588 if (updateBlock->Set.vertexShader && updateBlock->VertexShader != This->StateBlock->VertexShader) {
589 updateBlock->VertexShader = This->StateBlock->VertexShader;
590 TRACE("Updating vertex shader to %ld\n", This->StateBlock->VertexShader);
593 /* TODO: Vertex Shader Constants */
595 /* Lights... For a recorded state block, we just had a chain of actions to perform,
596 so we need to walk that chain and update any actions which differ */
597 src = updateBlock->lights;
598 while (src != NULL) {
599 PLIGHTINFOEL *realLight = NULL;
601 /* Locate the light in the live lights */
602 realLight = This->StateBlock->lights;
603 while (realLight != NULL && realLight->OriginalIndex != src->OriginalIndex) realLight = realLight->next;
605 if (realLight == NULL) {
606 FIXME("A captured light no longer exists...?\n");
609 /* If 'changed' then its a SetLight command. Rather than comparing to see
610 if the OriginalParms have changed and then copy them (twice through
611 memory) just do the copy */
613 TRACE("Updating lights for light %ld\n", src->OriginalIndex);
614 memcpy(&src->OriginalParms, &realLight->OriginalParms, sizeof(PLIGHTINFOEL));
617 /* If 'enabledChanged' then its a LightEnable command */
618 if (src->enabledChanged) {
619 TRACE("Updating lightEnabled for light %ld\n", src->OriginalIndex);
620 src->lightEnabled = realLight->lightEnabled;
629 if (updateBlock->Set.pixelShader && updateBlock->PixelShader != This->StateBlock->PixelShader) {
630 TRACE("Updating pixel shader to %ld\n", This->StateBlock->PixelShader);
631 IDirect3DDevice8Impl_SetVertexShader(iface, updateBlock->PixelShader);
634 /* TODO: Pixel Shader Constants */
636 /* Others + Render & Texture */
637 for (i = 0; i < HIGHEST_TRANSFORMSTATE; i++) {
638 if (updateBlock->Set.transform[i] && memcmp(&This->StateBlock->transforms[i],
639 &updateBlock->transforms[i],
640 sizeof(D3DMATRIX)) != 0) {
641 TRACE("Updating transform %d\n", i);
642 memcpy(&updateBlock->transforms[i], &This->StateBlock->transforms[i], sizeof(D3DMATRIX));
646 if (updateBlock->Set.Indices && ((updateBlock->pIndexData != This->StateBlock->pIndexData)
647 || (updateBlock->baseVertexIndex != This->StateBlock->baseVertexIndex))) {
648 TRACE("Updating pindexData to %p, baseVertexIndex to %d\n",
649 This->StateBlock->pIndexData, This->StateBlock->baseVertexIndex);
650 updateBlock->pIndexData = This->StateBlock->pIndexData;
651 updateBlock->baseVertexIndex = This->StateBlock->baseVertexIndex;
654 if (updateBlock->Set.material && memcmp(&This->StateBlock->material,
655 &updateBlock->material,
656 sizeof(D3DMATERIAL8)) != 0) {
657 TRACE("Updating material\n");
658 memcpy(&updateBlock->material, &This->StateBlock->material, sizeof(D3DMATERIAL8));
661 if (updateBlock->Set.viewport && memcmp(&This->StateBlock->viewport,
662 &updateBlock->viewport,
663 sizeof(D3DVIEWPORT8)) != 0) {
664 TRACE("Updating viewport\n");
665 memcpy(&updateBlock->viewport, &This->StateBlock->viewport, sizeof(D3DVIEWPORT8));
668 for (i = 0; i < MAX_STREAMS; i++) {
669 if (updateBlock->Set.stream_source[i] &&
670 ((updateBlock->stream_stride[i] != This->StateBlock->stream_stride[i]) ||
671 (updateBlock->stream_source[i] != This->StateBlock->stream_source[i]))) {
672 TRACE("Updating stream source %d to %p, stride to %d\n", i, This->StateBlock->stream_source[i],
673 This->StateBlock->stream_stride[i]);
674 updateBlock->stream_stride[i] = This->StateBlock->stream_stride[i];
675 updateBlock->stream_source[i] = This->StateBlock->stream_source[i];
679 for (i = 0; i < GL_LIMITS(clipplanes); i++) {
680 if (updateBlock->Set.clipplane[i] && memcmp(&This->StateBlock->clipplane[i],
681 &updateBlock->clipplane[i],
682 sizeof(updateBlock->clipplane)) != 0) {
684 TRACE("Updating clipplane %d\n", i);
685 memcpy(&updateBlock->clipplane[i], &This->StateBlock->clipplane[i],
686 sizeof(updateBlock->clipplane));
691 for (i = 0; i < HIGHEST_RENDER_STATE; i++) {
693 if (updateBlock->Set.renderstate[i] && (updateBlock->renderstate[i] !=
694 This->StateBlock->renderstate[i])) {
695 TRACE("Updating renderstate %d to %ld\n", i, This->StateBlock->renderstate[i]);
696 updateBlock->renderstate[i] = This->StateBlock->renderstate[i];
701 for (j = 0; j < GL_LIMITS(textures); j++) {
702 for (i = 0; i < HIGHEST_TEXTURE_STATE; i++) {
704 if (updateBlock->Set.texture_state[j][i] && (updateBlock->texture_state[j][i] !=
705 This->StateBlock->texture_state[j][i])) {
706 TRACE("Updating texturestagestate %d,%d to %ld (was %ld)\n", j,i, This->StateBlock->texture_state[j][i],
707 updateBlock->texture_state[j][i]);
708 updateBlock->texture_state[j][i] = This->StateBlock->texture_state[j][i];
711 if (updateBlock->Set.textures[j] && (updateBlock->textures[j] != This->StateBlock->textures[j])) {
712 TRACE("Updating texture %d to %p (was %p)\n", j, This->StateBlock->textures[j], updateBlock->textures[j]);
713 updateBlock->textures[j] = This->StateBlock->textures[j];
720 TRACE("(%p) : Updated state block %p ------------------^\n", This, updateBlock);
725 DWORD SavedPixelStates_R[NUM_SAVEDPIXELSTATES_R] = {
726 D3DRS_ALPHABLENDENABLE ,
729 D3DRS_ALPHATESTENABLE ,
731 D3DRS_COLORWRITEENABLE ,
734 D3DRS_EDGEANTIALIAS ,
743 D3DRS_STENCILENABLE ,
749 D3DRS_STENCILWRITEMASK ,
751 D3DRS_TEXTUREFACTOR ,
766 DWORD SavedPixelStates_T[NUM_SAVEDPIXELSTATES_T] = {
775 D3DTSS_BUMPENVLOFFSET ,
776 D3DTSS_BUMPENVLSCALE ,
777 D3DTSS_BUMPENVMAT00 ,
778 D3DTSS_BUMPENVMAT01 ,
779 D3DTSS_BUMPENVMAT10 ,
780 D3DTSS_BUMPENVMAT11 ,
786 D3DTSS_MAXANISOTROPY ,
790 D3DTSS_MIPMAPLODBIAS ,
792 D3DTSS_TEXCOORDINDEX ,
793 D3DTSS_TEXTURETRANSFORMFLAGS
796 DWORD SavedVertexStates_R[NUM_SAVEDVERTEXSTATES_R] = {
798 D3DRS_AMBIENTMATERIALSOURCE ,
800 D3DRS_CLIPPLANEENABLE ,
802 D3DRS_DIFFUSEMATERIALSOURCE ,
803 D3DRS_EMISSIVEMATERIALSOURCE ,
808 D3DRS_FOGVERTEXMODE ,
809 D3DRS_INDEXEDVERTEXBLENDENABLE ,
812 D3DRS_MULTISAMPLEANTIALIAS ,
813 D3DRS_MULTISAMPLEMASK ,
814 D3DRS_NORMALIZENORMALS ,
815 D3DRS_PATCHEDGESTYLE ,
816 D3DRS_PATCHSEGMENTS ,
820 D3DRS_POINTSCALEENABLE ,
822 D3DRS_POINTSIZE_MAX ,
823 D3DRS_POINTSIZE_MIN ,
824 D3DRS_POINTSPRITEENABLE ,
825 D3DRS_RANGEFOGENABLE ,
826 D3DRS_SOFTWAREVERTEXPROCESSING ,
827 D3DRS_SPECULARMATERIALSOURCE ,
832 DWORD SavedVertexStates_T[NUM_SAVEDVERTEXSTATES_T] = {
833 D3DTSS_TEXCOORDINDEX ,
834 D3DTSS_TEXTURETRANSFORMFLAGS