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) {
60 LPDIRECT3DDEVICE8 iface = (LPDIRECT3DDEVICE8) This;
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;
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));
76 if (This->PresentParms.EnableAutoDepthStencil) {
77 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZENABLE, D3DZB_TRUE);
79 IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_ZENABLE, D3DZB_FALSE);
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);
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)
114 * IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILREF, 0);
115 * IDirect3DDevice8Impl_SetRenderState(iface, D3DRS_STENCILMASK, 0xFFFFFFFF);
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);
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;
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 */
204 for (i = 0; i < GL_LIMITS(textures); i++) {
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;
212 /* Make appropriate texture active */
213 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
214 #if defined(GL_VERSION_1_3)
215 glActiveTexture(GL_TEXTURE0 + i);
217 glActiveTextureARB(GL_TEXTURE0_ARB + i);
219 checkGLcall("glActiveTextureARB");
221 FIXME("Program using multiple concurrent textures which this opengl implementation doesnt support\n");
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]);
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");
234 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &white);
235 checkGLcall("glTexImage1D");
237 /* Reapply all the texture state information to this texture */
238 setupTextureStates(iface, i, REAPPLY_ALL);
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;
252 This->currentPalette = 0;
254 TRACE("-----------------------> Device defaults now set up...\n");
261 HRESULT WINAPI IDirect3DDeviceImpl_CreateStateBlock(IDirect3DDevice8Impl* This, D3DSTATEBLOCKTYPE Type, IDirect3DStateBlockImpl** ppStateBlock) {
262 IDirect3DStateBlockImpl* object;
265 TRACE("(%p) : Type(%d)\n", This, Type);
267 /* Allocate Storage */
268 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlockImpl));
270 if (NULL == This->StateBlock) { /** if it the main stateblock only do init and returns */
271 /*object->lpVtbl = &Direct3DStateBlock9_Vtbl;*/
272 object->device = This;
274 object->blockType = Type;
275 This->StateBlock = object;
276 /* don't forget to init it calling InitStartupStateBlock */
279 memcpy(object, This->StateBlock, sizeof(IDirect3DStateBlockImpl));
281 *ppStateBlock = (IDirect3DStateBlockImpl*) 0xFFFFFFFF;
282 return E_OUTOFMEMORY;
284 /*object->lpVtbl = &Direct3DStateBlock9_Vtbl;*/
285 object->device = This;
287 object->blockType = Type;
289 TRACE("Updating changed flags appropriate for type %d\n", Type);
291 if (Type == D3DSBT_ALL) {
292 TRACE("ALL => Pretend everything has changed\n");
293 memset(&object->Changed, TRUE, sizeof(This->StateBlock->Changed));
295 } else if (Type == D3DSBT_PIXELSTATE) {
297 memset(&object->Changed, FALSE, sizeof(This->StateBlock->Changed));
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;
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;
310 } else if (Type == D3DSBT_VERTEXSTATE) {
312 memset(&object->Changed, FALSE, sizeof(This->StateBlock->Changed));
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;
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;
324 for (i = 0; i < GL_LIMITS(lights); i++) {
325 object->Changed.lightEnable[i] = TRUE;
326 object->Changed.lights[i] = TRUE;
330 FIXME("Unrecognized state block type %d\n", Type);
332 TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, object);
334 *ppStateBlock = object;
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);
345 HRESULT WINAPI IDirect3DDeviceImpl_BeginStateBlock(IDirect3DDevice8Impl* This) {
346 IDirect3DStateBlockImpl* object;
348 TRACE("(%p)\n", This);
350 if (This->isRecordingState) {
351 TRACE("(%p) already recording! returning error\n", This);
352 return D3DERR_INVALIDCALL;
355 /* Allocate Storage */
356 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlockImpl));
359 return E_OUTOFMEMORY;
361 /*object->lpVtbl = &Direct3DVextexShaderDeclaration8_Vtbl;*/
362 object->device = This;
365 This->isRecordingState = TRUE;
366 This->UpdateStateBlock = object;
371 HRESULT WINAPI IDirect3DDeviceImpl_EndStateBlock(IDirect3DDevice8Impl* This, IDirect3DStateBlockImpl** ppStateBlock) {
372 TRACE("(%p)\n", This);
374 if (!This->isRecordingState) {
375 TRACE("(%p) not recording! returning error\n", This);
376 *ppStateBlock = NULL;
377 return D3DERR_INVALIDCALL;
380 This->UpdateStateBlock->blockType = D3DSBT_RECORDED;
381 *ppStateBlock = This->UpdateStateBlock; /* FIXME: AddRef() */
382 This->isRecordingState = FALSE;
383 This->UpdateStateBlock = This->StateBlock;
385 TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, *ppStateBlock);
389 HRESULT WINAPI IDirect3DDeviceImpl_ApplyStateBlock(IDirect3DDevice8Impl* This, IDirect3DStateBlockImpl* pSB) {
392 LPDIRECT3DDEVICE8 iface = (LPDIRECT3DDEVICE8) This;
394 TRACE("(%p) : Applying state block %p ------------------v\n", This, pSB);
396 /* FIXME: Only apply applicable states not all states */
398 if (pSB->blockType == D3DSBT_RECORDED || pSB->blockType == D3DSBT_ALL || pSB->blockType == D3DSBT_VERTEXSTATE) {
400 for (i = 0; i < GL_LIMITS(lights); i++) {
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]);
408 if (pSB->Set.vertexShader && pSB->Changed.vertexShader)
409 IDirect3DDevice8Impl_SetVertexShader(iface, pSB->VertexShader);
411 /* TODO: Vertex Shader Constants */
414 if (pSB->blockType == D3DSBT_RECORDED || pSB->blockType == D3DSBT_ALL || pSB->blockType == D3DSBT_PIXELSTATE) {
416 if (pSB->Set.pixelShader && pSB->Changed.pixelShader)
417 IDirect3DDevice8Impl_SetPixelShader(iface, pSB->PixelShader);
419 /* TODO: Pixel Shader Constants */
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]);
429 if (pSB->Set.Indices && pSB->Changed.Indices)
430 IDirect3DDevice8Impl_SetIndices(iface, pSB->pIndexData, pSB->baseVertexIndex);
432 if (pSB->Set.material && pSB->Changed.material)
433 IDirect3DDevice8Impl_SetMaterial(iface, &pSB->material);
435 if (pSB->Set.viewport && pSB->Changed.viewport)
436 IDirect3DDevice8Impl_SetViewport(iface, &pSB->viewport);
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]);
443 for (i = 0; i < GL_LIMITS(clipplanes); i++) {
444 if (pSB->Set.clipplane[i] && pSB->Changed.clipplane[i]) {
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);
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]);
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]);
468 if (pSB->Set.textures[j] && pSB->Changed.textures[j]) {
469 IDirect3DDevice8Impl_SetTexture(iface, j, pSB->textures[j]);
474 } else if (pSB->blockType == D3DSBT_PIXELSTATE) {
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]]);
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]]);
490 } else if (pSB->blockType == D3DSBT_VERTEXSTATE) {
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]]);
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]]);
507 FIXME("Unrecognized state block type %d\n", pSB->blockType);
509 memcpy(&This->StateBlock->Changed, &pSB->Changed, sizeof(This->StateBlock->Changed));
510 TRACE("(%p) : Applied state block %p ------------------^\n", This, pSB);
515 HRESULT WINAPI IDirect3DDeviceImpl_CaptureStateBlock(IDirect3DDevice8Impl* This, IDirect3DStateBlockImpl* updateBlock) {
516 LPDIRECT3DDEVICE8 iface = (LPDIRECT3DDEVICE8) This;
518 TRACE("(%p) : Updating state block %p ------------------v \n", This, updateBlock);
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);
527 /* FIXME: This will record states of new lights! May need to have and save set_lights
528 across this action */
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);
539 /* TODO: Vertex Shader Constants */
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];
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));
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);
561 /* TODO: Pixel Shader Constants */
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));
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;
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));
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));
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];
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) {
611 TRACE("Updating clipplane %d\n", i);
612 memcpy(&updateBlock->clipplane[i], &This->StateBlock->clipplane[i],
613 sizeof(updateBlock->clipplane));
618 for (i = 0; i < HIGHEST_RENDER_STATE; i++) {
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];
628 for (j = 0; j < GL_LIMITS(textures); j++) {
629 for (i = 0; i < HIGHEST_TEXTURE_STATE; i++) {
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];
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];
647 TRACE("(%p) : Updated state block %p ------------------^\n", This, updateBlock);
652 DWORD SavedPixelStates_R[NUM_SAVEDPIXELSTATES_R] = {
653 D3DRS_ALPHABLENDENABLE ,
656 D3DRS_ALPHATESTENABLE ,
658 D3DRS_COLORWRITEENABLE ,
661 D3DRS_EDGEANTIALIAS ,
670 D3DRS_STENCILENABLE ,
676 D3DRS_STENCILWRITEMASK ,
678 D3DRS_TEXTUREFACTOR ,
693 DWORD SavedPixelStates_T[NUM_SAVEDPIXELSTATES_T] = {
702 D3DTSS_BUMPENVLOFFSET ,
703 D3DTSS_BUMPENVLSCALE ,
704 D3DTSS_BUMPENVMAT00 ,
705 D3DTSS_BUMPENVMAT01 ,
706 D3DTSS_BUMPENVMAT10 ,
707 D3DTSS_BUMPENVMAT11 ,
713 D3DTSS_MAXANISOTROPY ,
717 D3DTSS_MIPMAPLODBIAS ,
719 D3DTSS_TEXCOORDINDEX ,
720 D3DTSS_TEXTURETRANSFORMFLAGS
723 DWORD SavedVertexStates_R[NUM_SAVEDVERTEXSTATES_R] = {
725 D3DRS_AMBIENTMATERIALSOURCE ,
727 D3DRS_CLIPPLANEENABLE ,
729 D3DRS_DIFFUSEMATERIALSOURCE ,
730 D3DRS_EMISSIVEMATERIALSOURCE ,
735 D3DRS_FOGVERTEXMODE ,
736 D3DRS_INDEXEDVERTEXBLENDENABLE ,
739 D3DRS_MULTISAMPLEANTIALIAS ,
740 D3DRS_MULTISAMPLEMASK ,
741 D3DRS_NORMALIZENORMALS ,
742 D3DRS_PATCHEDGESTYLE ,
743 D3DRS_PATCHSEGMENTS ,
747 D3DRS_POINTSCALEENABLE ,
749 D3DRS_POINTSIZE_MAX ,
750 D3DRS_POINTSIZE_MIN ,
751 D3DRS_POINTSPRITEENABLE ,
752 D3DRS_RANGEFOGENABLE ,
753 D3DRS_SOFTWAREVERTEXPROCESSING ,
754 D3DRS_SPECULARMATERIALSOURCE ,
759 DWORD SavedVertexStates_T[NUM_SAVEDVERTEXSTATES_T] = {
760 D3DTSS_TEXCOORDINDEX ,
761 D3DTSS_TEXTURETRANSFORMFLAGS