2 * state block implementation
4 * Copyright 2002 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2007 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29 #define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
31 static const DWORD pixel_states_render[] =
33 WINED3DRS_ALPHABLENDENABLE,
36 WINED3DRS_ALPHATESTENABLE,
38 WINED3DRS_COLORWRITEENABLE,
40 WINED3DRS_DITHERENABLE,
48 WINED3DRS_STENCILENABLE,
49 WINED3DRS_STENCILFAIL,
50 WINED3DRS_STENCILFUNC,
51 WINED3DRS_STENCILMASK,
52 WINED3DRS_STENCILPASS,
54 WINED3DRS_STENCILWRITEMASK,
55 WINED3DRS_STENCILZFAIL,
56 WINED3DRS_TEXTUREFACTOR,
67 WINED3DRS_ZWRITEENABLE,
70 static const DWORD pixel_states_texture[] =
76 WINED3DTSS_BUMPENVLOFFSET,
77 WINED3DTSS_BUMPENVLSCALE,
78 WINED3DTSS_BUMPENVMAT00,
79 WINED3DTSS_BUMPENVMAT01,
80 WINED3DTSS_BUMPENVMAT10,
81 WINED3DTSS_BUMPENVMAT11,
87 WINED3DTSS_TEXCOORDINDEX,
88 WINED3DTSS_TEXTURETRANSFORMFLAGS,
91 static const DWORD pixel_states_sampler[] =
96 WINED3DSAMP_BORDERCOLOR,
97 WINED3DSAMP_MAGFILTER,
98 WINED3DSAMP_MINFILTER,
99 WINED3DSAMP_MIPFILTER,
100 WINED3DSAMP_MIPMAPLODBIAS,
101 WINED3DSAMP_MAXMIPLEVEL,
102 WINED3DSAMP_MAXANISOTROPY,
103 WINED3DSAMP_SRGBTEXTURE,
104 WINED3DSAMP_ELEMENTINDEX,
107 static const DWORD vertex_states_render[] =
110 WINED3DRS_AMBIENTMATERIALSOURCE,
112 WINED3DRS_CLIPPLANEENABLE,
113 WINED3DRS_COLORVERTEX,
114 WINED3DRS_DIFFUSEMATERIALSOURCE,
115 WINED3DRS_EMISSIVEMATERIALSOURCE,
116 WINED3DRS_FOGDENSITY,
119 WINED3DRS_FOGTABLEMODE,
120 WINED3DRS_FOGVERTEXMODE,
121 WINED3DRS_INDEXEDVERTEXBLENDENABLE,
123 WINED3DRS_LOCALVIEWER,
124 WINED3DRS_MULTISAMPLEANTIALIAS,
125 WINED3DRS_MULTISAMPLEMASK,
126 WINED3DRS_NORMALIZENORMALS,
127 WINED3DRS_PATCHEDGESTYLE,
128 WINED3DRS_POINTSCALE_A,
129 WINED3DRS_POINTSCALE_B,
130 WINED3DRS_POINTSCALE_C,
131 WINED3DRS_POINTSCALEENABLE,
133 WINED3DRS_POINTSIZE_MAX,
134 WINED3DRS_POINTSIZE_MIN,
135 WINED3DRS_POINTSPRITEENABLE,
136 WINED3DRS_RANGEFOGENABLE,
137 WINED3DRS_SPECULARMATERIALSOURCE,
138 WINED3DRS_TWEENFACTOR,
139 WINED3DRS_VERTEXBLEND,
144 static const DWORD vertex_states_texture[] =
146 WINED3DTSS_TEXCOORDINDEX,
147 WINED3DTSS_TEXTURETRANSFORMFLAGS,
150 static const DWORD vertex_states_sampler[] =
152 WINED3DSAMP_DMAPOFFSET,
155 /* Allocates the correct amount of space for pixel and vertex shader constants,
156 * along with their set/changed flags on the given stateblock object
158 static HRESULT stateblock_allocate_shader_constants(IWineD3DStateBlockImpl *object)
160 IWineD3DStateBlockImpl *This = object;
162 /* Allocate space for floating point constants */
163 object->pixelShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
164 if (!object->pixelShaderConstantF) goto fail;
166 object->changed.pixelShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(pshader_constantsF));
167 if (!object->changed.pixelShaderConstantsF) goto fail;
169 object->vertexShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
170 if (!object->vertexShaderConstantF) goto fail;
172 object->changed.vertexShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(vshader_constantsF));
173 if (!object->changed.vertexShaderConstantsF) goto fail;
175 object->contained_vs_consts_f = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * GL_LIMITS(vshader_constantsF));
176 if (!object->contained_vs_consts_f) goto fail;
178 object->contained_ps_consts_f = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * GL_LIMITS(pshader_constantsF));
179 if (!object->contained_ps_consts_f) goto fail;
184 ERR("Failed to allocate memory\n");
185 HeapFree(GetProcessHeap(), 0, object->pixelShaderConstantF);
186 HeapFree(GetProcessHeap(), 0, object->changed.pixelShaderConstantsF);
187 HeapFree(GetProcessHeap(), 0, object->vertexShaderConstantF);
188 HeapFree(GetProcessHeap(), 0, object->changed.vertexShaderConstantsF);
189 HeapFree(GetProcessHeap(), 0, object->contained_vs_consts_f);
190 HeapFree(GetProcessHeap(), 0, object->contained_ps_consts_f);
191 return E_OUTOFMEMORY;
194 static inline void stateblock_set_bits(DWORD *map, UINT map_size)
196 DWORD mask = (1 << (map_size & 0x1f)) - 1;
197 memset(map, 0xff, (map_size >> 5) * sizeof(*map));
198 if (mask) map[map_size >> 5] = mask;
201 /* Set all members of a stateblock savedstate to the given value */
202 static void stateblock_savedstates_set_all(SAVEDSTATES *states, const struct wined3d_gl_info *gl_info)
207 states->primitive_type = 1;
209 states->material = 1;
210 states->viewport = 1;
211 states->vertexDecl = 1;
212 states->pixelShader = 1;
213 states->vertexShader = 1;
214 states->scissorRect = 1;
216 /* Fixed size arrays */
217 states->streamSource = 0xffff;
218 states->streamFreq = 0xffff;
219 states->textures = 0xfffff;
220 stateblock_set_bits(states->transform, HIGHEST_TRANSFORMSTATE + 1);
221 stateblock_set_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
222 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
223 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3fff;
224 states->clipplane = 0xffffffff;
225 states->pixelShaderConstantsB = 0xffff;
226 states->pixelShaderConstantsI = 0xffff;
227 states->vertexShaderConstantsB = 0xffff;
228 states->vertexShaderConstantsI = 0xffff;
230 /* Dynamically sized arrays */
231 memset(states->pixelShaderConstantsF, TRUE, sizeof(BOOL) * gl_info->max_pshader_constantsF);
232 memset(states->vertexShaderConstantsF, TRUE, sizeof(BOOL) * gl_info->max_vshader_constantsF);
235 static void stateblock_savedstates_set_pixel(SAVEDSTATES *states, const struct wined3d_gl_info *gl_info)
237 DWORD texture_mask = 0;
238 WORD sampler_mask = 0;
241 states->pixelShader = 1;
243 for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
245 DWORD rs = pixel_states_render[i];
246 states->renderState[rs >> 5] |= 1 << (rs & 0x1f);
249 for (i = 0; i < sizeof(pixel_states_texture) / sizeof(*pixel_states_texture); ++i)
250 texture_mask |= 1 << pixel_states_texture[i];
251 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
252 for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
253 sampler_mask |= 1 << pixel_states_sampler[i];
254 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
255 states->pixelShaderConstantsB = 0xffff;
256 states->pixelShaderConstantsI = 0xffff;
258 memset(states->pixelShaderConstantsF, TRUE, sizeof(BOOL) * gl_info->max_pshader_constantsF);
261 static void stateblock_savedstates_set_vertex(SAVEDSTATES *states, const struct wined3d_gl_info *gl_info)
263 DWORD texture_mask = 0;
264 WORD sampler_mask = 0;
267 states->vertexShader = 1;
269 for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
271 DWORD rs = vertex_states_render[i];
272 states->renderState[rs >> 5] |= 1 << (rs & 0x1f);
275 for (i = 0; i < sizeof(vertex_states_texture) / sizeof(*vertex_states_texture); ++i)
276 texture_mask |= 1 << vertex_states_texture[i];
277 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
278 for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
279 sampler_mask |= 1 << vertex_states_sampler[i];
280 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
281 states->vertexShaderConstantsB = 0xffff;
282 states->vertexShaderConstantsI = 0xffff;
284 memset(states->vertexShaderConstantsF, TRUE, sizeof(BOOL) * gl_info->max_vshader_constantsF);
287 static void stateblock_copy_values(IWineD3DStateBlockImpl *dst, const IWineD3DStateBlockImpl *src,
288 const struct wined3d_gl_info *gl_info)
293 dst->gl_primitive_type = src->gl_primitive_type;
294 dst->vertexDecl = src->vertexDecl;
295 dst->vertexShader = src->vertexShader;
296 dst->streamIsUP = src->streamIsUP;
297 dst->pIndexData = src->pIndexData;
298 dst->IndexFmt = src->IndexFmt;
299 dst->baseVertexIndex = src->baseVertexIndex;
300 dst->clip_status = src->clip_status;
301 dst->viewport = src->viewport;
302 dst->material = src->material;
303 dst->pixelShader = src->pixelShader;
304 dst->scissorRect = src->scissorRect;
307 memset(dst->activeLights, 0, sizeof(dst->activeLights));
308 for (l = 0; l < LIGHTMAP_SIZE; ++l)
310 struct list *e1, *e2;
311 LIST_FOR_EACH_SAFE(e1, e2, &dst->lightMap[l])
313 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
314 list_remove(&light->entry);
315 HeapFree(GetProcessHeap(), 0, light);
318 LIST_FOR_EACH(e1, &src->lightMap[l])
320 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry), *light2;
321 light2 = HeapAlloc(GetProcessHeap(), 0, sizeof(*light));
323 list_add_tail(&dst->lightMap[l], &light2->entry);
324 if (light2->glIndex != -1) dst->activeLights[light2->glIndex] = light2;
328 /* Fixed size arrays */
329 memcpy(dst->vertexShaderConstantB, src->vertexShaderConstantB, sizeof(dst->vertexShaderConstantB));
330 memcpy(dst->vertexShaderConstantI, src->vertexShaderConstantI, sizeof(dst->vertexShaderConstantI));
331 memcpy(dst->pixelShaderConstantB, src->pixelShaderConstantB, sizeof(dst->pixelShaderConstantB));
332 memcpy(dst->pixelShaderConstantI, src->pixelShaderConstantI, sizeof(dst->pixelShaderConstantI));
334 memcpy(dst->streamStride, src->streamStride, sizeof(dst->streamStride));
335 memcpy(dst->streamOffset, src->streamOffset, sizeof(dst->streamOffset));
336 memcpy(dst->streamSource, src->streamSource, sizeof(dst->streamSource));
337 memcpy(dst->streamFreq, src->streamFreq, sizeof(dst->streamFreq));
338 memcpy(dst->streamFlags, src->streamFlags, sizeof(dst->streamFlags));
339 memcpy(dst->transforms, src->transforms, sizeof(dst->transforms));
340 memcpy(dst->clipplane, src->clipplane, sizeof(dst->clipplane));
341 memcpy(dst->renderState, src->renderState, sizeof(dst->renderState));
342 memcpy(dst->textures, src->textures, sizeof(dst->textures));
343 memcpy(dst->textureState, src->textureState, sizeof(dst->textureState));
344 memcpy(dst->samplerState, src->samplerState, sizeof(dst->samplerState));
346 /* Dynamically sized arrays */
347 memcpy(dst->vertexShaderConstantF, src->vertexShaderConstantF, sizeof(float) * gl_info->max_vshader_constantsF * 4);
348 memcpy(dst->pixelShaderConstantF, src->pixelShaderConstantF, sizeof(float) * gl_info->max_pshader_constantsF * 4);
351 void stateblock_init_contained_states(IWineD3DStateBlockImpl *stateblock)
353 const struct wined3d_gl_info *gl_info = &stateblock->wineD3DDevice->adapter->gl_info;
356 for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
358 DWORD map = stateblock->changed.renderState[i];
359 for (j = 0; map; map >>= 1, ++j)
361 if (!(map & 1)) continue;
363 stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
364 ++stateblock->num_contained_render_states;
368 for (i = 0; i <= HIGHEST_TRANSFORMSTATE >> 5; ++i)
370 DWORD map = stateblock->changed.transform[i];
371 for (j = 0; map; map >>= 1, ++j)
373 if (!(map & 1)) continue;
375 stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
376 ++stateblock->num_contained_transform_states;
380 for (i = 0; i < gl_info->max_vshader_constantsF; ++i)
382 if (stateblock->changed.vertexShaderConstantsF[i])
384 stateblock->contained_vs_consts_f[stateblock->num_contained_vs_consts_f] = i;
385 ++stateblock->num_contained_vs_consts_f;
389 for (i = 0; i < MAX_CONST_I; ++i)
391 if (stateblock->changed.vertexShaderConstantsI & (1 << i))
393 stateblock->contained_vs_consts_i[stateblock->num_contained_vs_consts_i] = i;
394 ++stateblock->num_contained_vs_consts_i;
398 for (i = 0; i < MAX_CONST_B; ++i)
400 if (stateblock->changed.vertexShaderConstantsB & (1 << i))
402 stateblock->contained_vs_consts_b[stateblock->num_contained_vs_consts_b] = i;
403 ++stateblock->num_contained_vs_consts_b;
407 for (i = 0; i < gl_info->max_pshader_constantsF; ++i)
409 if (stateblock->changed.pixelShaderConstantsF[i])
411 stateblock->contained_ps_consts_f[stateblock->num_contained_ps_consts_f] = i;
412 ++stateblock->num_contained_ps_consts_f;
416 for (i = 0; i < MAX_CONST_I; ++i)
418 if (stateblock->changed.pixelShaderConstantsI & (1 << i))
420 stateblock->contained_ps_consts_i[stateblock->num_contained_ps_consts_i] = i;
421 ++stateblock->num_contained_ps_consts_i;
425 for (i = 0; i < MAX_CONST_B; ++i)
427 if (stateblock->changed.pixelShaderConstantsB & (1 << i))
429 stateblock->contained_ps_consts_b[stateblock->num_contained_ps_consts_b] = i;
430 ++stateblock->num_contained_ps_consts_b;
434 for (i = 0; i < MAX_TEXTURES; ++i)
436 DWORD map = stateblock->changed.textureState[i];
438 for(j = 0; map; map >>= 1, ++j)
440 if (!(map & 1)) continue;
442 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
443 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
444 ++stateblock->num_contained_tss_states;
448 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
450 DWORD map = stateblock->changed.samplerState[i];
452 for (j = 0; map; map >>= 1, ++j)
454 if (!(map & 1)) continue;
456 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
457 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
458 ++stateblock->num_contained_sampler_states;
463 /**********************************************************
464 * IWineD3DStateBlockImpl IUnknown parts follows
465 **********************************************************/
466 static HRESULT WINAPI IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock *iface,REFIID riid,LPVOID *ppobj)
468 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
469 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
470 if (IsEqualGUID(riid, &IID_IUnknown)
471 || IsEqualGUID(riid, &IID_IWineD3DBase)
472 || IsEqualGUID(riid, &IID_IWineD3DStateBlock)){
473 IUnknown_AddRef(iface);
478 return E_NOINTERFACE;
481 static ULONG WINAPI IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock *iface) {
482 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
483 ULONG refCount = InterlockedIncrement(&This->ref);
485 TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
489 static ULONG WINAPI IWineD3DStateBlockImpl_Release(IWineD3DStateBlock *iface) {
490 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
491 ULONG refCount = InterlockedDecrement(&This->ref);
493 TRACE("(%p) : Releasing from %d\n", This, refCount + 1);
498 if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
500 for (counter = 0; counter < MAX_COMBINED_SAMPLERS; counter++)
502 if (This->textures[counter]) IWineD3DBaseTexture_Release(This->textures[counter]);
505 for (counter = 0; counter < MAX_STREAMS; counter++) {
506 if(This->streamSource[counter]) {
507 if (IWineD3DBuffer_Release(This->streamSource[counter]))
509 TRACE("Vertex buffer still referenced by stateblock, applications has leaked Stream %u, buffer %p\n", counter, This->streamSource[counter]);
513 if(This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
514 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
515 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
517 for(counter = 0; counter < LIGHTMAP_SIZE; counter++) {
518 struct list *e1, *e2;
519 LIST_FOR_EACH_SAFE(e1, e2, &This->lightMap[counter]) {
520 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
521 list_remove(&light->entry);
522 HeapFree(GetProcessHeap(), 0, light);
526 HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
527 HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
528 HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
529 HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
530 HeapFree(GetProcessHeap(), 0, This->contained_vs_consts_f);
531 HeapFree(GetProcessHeap(), 0, This->contained_ps_consts_f);
532 HeapFree(GetProcessHeap(), 0, This);
537 /**********************************************************
538 * IWineD3DStateBlockImpl parts follows
539 **********************************************************/
540 static HRESULT WINAPI IWineD3DStateBlockImpl_GetParent(IWineD3DStateBlock *iface, IUnknown **pParent) {
541 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
542 IUnknown_AddRef(This->parent);
543 *pParent = This->parent;
547 static HRESULT WINAPI IWineD3DStateBlockImpl_GetDevice(IWineD3DStateBlock *iface, IWineD3DDevice** ppDevice){
549 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
551 *ppDevice = (IWineD3DDevice*)This->wineD3DDevice;
552 IWineD3DDevice_AddRef(*ppDevice);
557 static void record_lights(IWineD3DStateBlockImpl *This, const IWineD3DStateBlockImpl *targetStateBlock)
561 /* Lights... For a recorded state block, we just had a chain of actions to perform,
562 * so we need to walk that chain and update any actions which differ
564 for(i = 0; i < LIGHTMAP_SIZE; i++) {
566 LIST_FOR_EACH(e, &This->lightMap[i]) {
567 BOOL updated = FALSE;
568 PLIGHTINFOEL *src = LIST_ENTRY(e, PLIGHTINFOEL, entry), *realLight;
569 if (!src->changed && !src->enabledChanged) continue;
571 /* Look up the light in the destination */
572 LIST_FOR_EACH(f, &targetStateBlock->lightMap[i]) {
573 realLight = LIST_ENTRY(f, PLIGHTINFOEL, entry);
574 if(realLight->OriginalIndex == src->OriginalIndex) {
576 src->OriginalParms = realLight->OriginalParms;
578 if(src->enabledChanged) {
579 /* Need to double check because enabledChanged does not catch enabled -> disabled -> enabled
580 * or disabled -> enabled -> disabled changes
582 if(realLight->glIndex == -1 && src->glIndex != -1) {
584 This->activeLights[src->glIndex] = NULL;
585 } else if(realLight->glIndex != -1 && src->glIndex == -1){
587 This->activeLights[realLight->glIndex] = src;
589 src->glIndex = realLight->glIndex;
597 /* Found a light, all done, proceed with next hash entry */
599 } else if(src->changed) {
600 /* Otherwise assign defaul params */
601 src->OriginalParms = WINED3D_default_light;
603 /* Not enabled by default */
610 static HRESULT WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface){
612 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
613 IWineD3DStateBlockImpl *targetStateBlock = This->wineD3DDevice->stateBlock;
617 TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock, This);
619 /* If not recorded, then update can just recapture */
620 if (This->blockType == WINED3DSBT_RECORDED) {
622 /* Recorded => Only update 'changed' values */
623 if (This->changed.vertexShader && This->vertexShader != targetStateBlock->vertexShader) {
624 TRACE("Updating vertex shader from %p to %p\n", This->vertexShader, targetStateBlock->vertexShader);
626 if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
627 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
628 This->vertexShader = targetStateBlock->vertexShader;
631 /* Vertex Shader Float Constants */
632 for (j = 0; j < This->num_contained_vs_consts_f; ++j) {
633 i = This->contained_vs_consts_f[j];
634 TRACE("Setting %p from %p %u to {%f, %f, %f, %f}\n", This, targetStateBlock, i,
635 targetStateBlock->vertexShaderConstantF[i * 4],
636 targetStateBlock->vertexShaderConstantF[i * 4 + 1],
637 targetStateBlock->vertexShaderConstantF[i * 4 + 2],
638 targetStateBlock->vertexShaderConstantF[i * 4 + 3]);
640 This->vertexShaderConstantF[i * 4] = targetStateBlock->vertexShaderConstantF[i * 4];
641 This->vertexShaderConstantF[i * 4 + 1] = targetStateBlock->vertexShaderConstantF[i * 4 + 1];
642 This->vertexShaderConstantF[i * 4 + 2] = targetStateBlock->vertexShaderConstantF[i * 4 + 2];
643 This->vertexShaderConstantF[i * 4 + 3] = targetStateBlock->vertexShaderConstantF[i * 4 + 3];
646 /* Vertex Shader Integer Constants */
647 for (j = 0; j < This->num_contained_vs_consts_i; ++j) {
648 i = This->contained_vs_consts_i[j];
649 TRACE("Setting %p from %p %u to {%d, %d, %d, %d}\n", This, targetStateBlock, i,
650 targetStateBlock->vertexShaderConstantI[i * 4],
651 targetStateBlock->vertexShaderConstantI[i * 4 + 1],
652 targetStateBlock->vertexShaderConstantI[i * 4 + 2],
653 targetStateBlock->vertexShaderConstantI[i * 4 + 3]);
655 This->vertexShaderConstantI[i * 4] = targetStateBlock->vertexShaderConstantI[i * 4];
656 This->vertexShaderConstantI[i * 4 + 1] = targetStateBlock->vertexShaderConstantI[i * 4 + 1];
657 This->vertexShaderConstantI[i * 4 + 2] = targetStateBlock->vertexShaderConstantI[i * 4 + 2];
658 This->vertexShaderConstantI[i * 4 + 3] = targetStateBlock->vertexShaderConstantI[i * 4 + 3];
661 /* Vertex Shader Boolean Constants */
662 for (j = 0; j < This->num_contained_vs_consts_b; ++j) {
663 i = This->contained_vs_consts_b[j];
664 TRACE("Setting %p from %p %u to %s\n", This, targetStateBlock, i,
665 targetStateBlock->vertexShaderConstantB[i] ? "TRUE" : "FALSE");
667 This->vertexShaderConstantB[i] = targetStateBlock->vertexShaderConstantB[i];
670 /* Pixel Shader Float Constants */
671 for (j = 0; j < This->num_contained_ps_consts_f; ++j) {
672 i = This->contained_ps_consts_f[j];
673 TRACE("Setting %p from %p %u to {%f, %f, %f, %f}\n", This, targetStateBlock, i,
674 targetStateBlock->pixelShaderConstantF[i * 4],
675 targetStateBlock->pixelShaderConstantF[i * 4 + 1],
676 targetStateBlock->pixelShaderConstantF[i * 4 + 2],
677 targetStateBlock->pixelShaderConstantF[i * 4 + 3]);
679 This->pixelShaderConstantF[i * 4] = targetStateBlock->pixelShaderConstantF[i * 4];
680 This->pixelShaderConstantF[i * 4 + 1] = targetStateBlock->pixelShaderConstantF[i * 4 + 1];
681 This->pixelShaderConstantF[i * 4 + 2] = targetStateBlock->pixelShaderConstantF[i * 4 + 2];
682 This->pixelShaderConstantF[i * 4 + 3] = targetStateBlock->pixelShaderConstantF[i * 4 + 3];
685 /* Pixel Shader Integer Constants */
686 for (j = 0; j < This->num_contained_ps_consts_i; ++j) {
687 i = This->contained_ps_consts_i[j];
688 TRACE("Setting %p from %p %u to {%d, %d, %d, %d}\n", This, targetStateBlock, i,
689 targetStateBlock->pixelShaderConstantI[i * 4],
690 targetStateBlock->pixelShaderConstantI[i * 4 + 1],
691 targetStateBlock->pixelShaderConstantI[i * 4 + 2],
692 targetStateBlock->pixelShaderConstantI[i * 4 + 3]);
694 This->pixelShaderConstantI[i * 4] = targetStateBlock->pixelShaderConstantI[i * 4];
695 This->pixelShaderConstantI[i * 4 + 1] = targetStateBlock->pixelShaderConstantI[i * 4 + 1];
696 This->pixelShaderConstantI[i * 4 + 2] = targetStateBlock->pixelShaderConstantI[i * 4 + 2];
697 This->pixelShaderConstantI[i * 4 + 3] = targetStateBlock->pixelShaderConstantI[i * 4 + 3];
700 /* Pixel Shader Boolean Constants */
701 for (j = 0; j < This->num_contained_ps_consts_b; ++j) {
702 i = This->contained_ps_consts_b[j];
703 TRACE("Setting %p from %p %u to %s\n", This, targetStateBlock, i,
704 targetStateBlock->pixelShaderConstantB[i] ? "TRUE" : "FALSE");
706 This->pixelShaderConstantB[i] = targetStateBlock->pixelShaderConstantB[i];
709 /* Others + Render & Texture */
710 for (i = 0; i < This->num_contained_transform_states; i++) {
711 TRACE("Updating transform %u\n", i);
712 This->transforms[This->contained_transform_states[i]] =
713 targetStateBlock->transforms[This->contained_transform_states[i]];
716 if (This->changed.primitive_type) This->gl_primitive_type = targetStateBlock->gl_primitive_type;
718 if (This->changed.indices && ((This->pIndexData != targetStateBlock->pIndexData)
719 || (This->baseVertexIndex != targetStateBlock->baseVertexIndex)
720 || (This->IndexFmt != targetStateBlock->IndexFmt))) {
721 TRACE("Updating pIndexData to %p, baseVertexIndex to %d\n",
722 targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
723 if(targetStateBlock->pIndexData) IWineD3DBuffer_AddRef(targetStateBlock->pIndexData);
724 if(This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
725 This->pIndexData = targetStateBlock->pIndexData;
726 This->baseVertexIndex = targetStateBlock->baseVertexIndex;
727 This->IndexFmt = targetStateBlock->IndexFmt;
730 if(This->changed.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl){
731 TRACE("Updating vertex declaration from %p to %p\n", This->vertexDecl, targetStateBlock->vertexDecl);
733 if (targetStateBlock->vertexDecl) IWineD3DVertexDeclaration_AddRef(targetStateBlock->vertexDecl);
734 if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
735 This->vertexDecl = targetStateBlock->vertexDecl;
738 if (This->changed.material && memcmp(&targetStateBlock->material,
740 sizeof(WINED3DMATERIAL)) != 0) {
741 TRACE("Updating material\n");
742 This->material = targetStateBlock->material;
745 if (This->changed.viewport && memcmp(&targetStateBlock->viewport,
747 sizeof(WINED3DVIEWPORT)) != 0) {
748 TRACE("Updating viewport\n");
749 This->viewport = targetStateBlock->viewport;
752 if(This->changed.scissorRect && memcmp(&targetStateBlock->scissorRect,
754 sizeof(targetStateBlock->scissorRect)))
756 TRACE("Updating scissor rect\n");
757 targetStateBlock->scissorRect = This->scissorRect;
760 map = This->changed.streamSource;
761 for (i = 0; map; map >>= 1, ++i)
763 if (!(map & 1)) continue;
765 if (This->streamStride[i] != targetStateBlock->streamStride[i]
766 || This->streamSource[i] != targetStateBlock->streamSource[i])
768 TRACE("Updating stream source %u to %p, stride to %u\n",
769 i, targetStateBlock->streamSource[i], targetStateBlock->streamStride[i]);
770 This->streamStride[i] = targetStateBlock->streamStride[i];
771 if (targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
772 if (This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
773 This->streamSource[i] = targetStateBlock->streamSource[i];
777 map = This->changed.streamFreq;
778 for (i = 0; map; map >>= 1, ++i)
780 if (!(map & 1)) continue;
782 if (This->streamFreq[i] != targetStateBlock->streamFreq[i]
783 || This->streamFlags[i] != targetStateBlock->streamFlags[i])
785 TRACE("Updating stream frequency %u to %u flags to %#x\n",
786 i, targetStateBlock->streamFreq[i], targetStateBlock->streamFlags[i]);
787 This->streamFreq[i] = targetStateBlock->streamFreq[i];
788 This->streamFlags[i] = targetStateBlock->streamFlags[i];
792 map = This->changed.clipplane;
793 for (i = 0; map; map >>= 1, ++i)
795 if (!(map & 1)) continue;
797 if (memcmp(targetStateBlock->clipplane[i], This->clipplane[i], sizeof(*This->clipplane)))
799 TRACE("Updating clipplane %u\n", i);
800 memcpy(This->clipplane[i], targetStateBlock->clipplane[i], sizeof(*This->clipplane));
805 for (i = 0; i < This->num_contained_render_states; i++) {
806 TRACE("Updating renderState %u to %u\n", This->contained_render_states[i],
807 targetStateBlock->renderState[This->contained_render_states[i]]);
808 This->renderState[This->contained_render_states[i]] = targetStateBlock->renderState[This->contained_render_states[i]];
812 for (j = 0; j < This->num_contained_tss_states; j++) {
813 DWORD stage = This->contained_tss_states[j].stage;
814 DWORD state = This->contained_tss_states[j].state;
816 TRACE("Updating texturestage state %u, %u to %u (was %u)\n", stage, state,
817 targetStateBlock->textureState[stage][state], This->textureState[stage][state]);
818 This->textureState[stage][state] = targetStateBlock->textureState[stage][state];
822 map = This->changed.textures;
823 for (i = 0; map; map >>= 1, ++i)
825 if (!(map & 1)) continue;
827 TRACE("Updating texture %u to %p (was %p)\n", i, targetStateBlock->textures[i], This->textures[i]);
828 if (targetStateBlock->textures[i]) IWineD3DBaseTexture_AddRef(targetStateBlock->textures[i]);
829 if (This->textures[i]) IWineD3DBaseTexture_Release(This->textures[i]);
830 This->textures[i] = targetStateBlock->textures[i];
833 for (j = 0; j < This->num_contained_sampler_states; j++) {
834 DWORD stage = This->contained_sampler_states[j].stage;
835 DWORD state = This->contained_sampler_states[j].state;
836 TRACE("Updating sampler state %u, %u to %u (was %u)\n", stage, state,
837 targetStateBlock->samplerState[stage][state], This->samplerState[stage][state]);
838 This->samplerState[stage][state] = targetStateBlock->samplerState[stage][state];
840 if(This->changed.pixelShader && This->pixelShader != targetStateBlock->pixelShader) {
841 if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
842 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
843 This->pixelShader = targetStateBlock->pixelShader;
846 record_lights(This, targetStateBlock);
847 } else if(This->blockType == WINED3DSBT_ALL) {
848 memcpy(This->vertexShaderConstantB, targetStateBlock->vertexShaderConstantB, sizeof(This->vertexShaderConstantI));
849 memcpy(This->vertexShaderConstantI, targetStateBlock->vertexShaderConstantI, sizeof(This->vertexShaderConstantF));
850 memcpy(This->vertexShaderConstantF, targetStateBlock->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
851 This->gl_primitive_type = targetStateBlock->gl_primitive_type;
852 memcpy(This->streamStride, targetStateBlock->streamStride, sizeof(This->streamStride));
853 memcpy(This->streamOffset, targetStateBlock->streamOffset, sizeof(This->streamOffset));
854 memcpy(This->streamFreq, targetStateBlock->streamFreq, sizeof(This->streamFreq));
855 memcpy(This->streamFlags, targetStateBlock->streamFlags, sizeof(This->streamFlags));
856 This->baseVertexIndex = targetStateBlock->baseVertexIndex;
857 memcpy(This->transforms, targetStateBlock->transforms, sizeof(This->transforms));
858 record_lights(This, targetStateBlock);
859 memcpy(This->clipplane, targetStateBlock->clipplane, sizeof(This->clipplane));
860 This->clip_status = targetStateBlock->clip_status;
861 This->viewport = targetStateBlock->viewport;
862 This->material = targetStateBlock->material;
863 memcpy(This->pixelShaderConstantB, targetStateBlock->pixelShaderConstantB, sizeof(This->pixelShaderConstantI));
864 memcpy(This->pixelShaderConstantI, targetStateBlock->pixelShaderConstantI, sizeof(This->pixelShaderConstantF));
865 memcpy(This->pixelShaderConstantF, targetStateBlock->pixelShaderConstantF, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
866 memcpy(This->renderState, targetStateBlock->renderState, sizeof(This->renderState));
867 memcpy(This->textureState, targetStateBlock->textureState, sizeof(This->textureState));
868 memcpy(This->samplerState, targetStateBlock->samplerState, sizeof(This->samplerState));
869 This->scissorRect = targetStateBlock->scissorRect;
871 if (This->vertexDecl != targetStateBlock->vertexDecl)
873 if (targetStateBlock->vertexDecl) IWineD3DVertexDeclaration_AddRef(targetStateBlock->vertexDecl);
874 if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
875 This->vertexDecl = targetStateBlock->vertexDecl;
878 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
880 if (targetStateBlock->textures[i] != This->textures[i])
882 if (targetStateBlock->textures[i]) IWineD3DBaseTexture_AddRef(targetStateBlock->textures[i]);
883 if (This->textures[i]) IWineD3DBaseTexture_Release(This->textures[i]);
884 This->textures[i] = targetStateBlock->textures[i];
888 if(targetStateBlock->pIndexData != This->pIndexData ||
889 targetStateBlock->IndexFmt != This->IndexFmt) {
890 if (targetStateBlock->pIndexData) IWineD3DBuffer_AddRef(targetStateBlock->pIndexData);
891 if (This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
892 This->pIndexData = targetStateBlock->pIndexData;
893 This->IndexFmt = targetStateBlock->IndexFmt;
895 for(i = 0; i < MAX_STREAMS; i++) {
896 if(targetStateBlock->streamSource[i] != This->streamSource[i]) {
897 if(targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
898 if(This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
899 This->streamSource[i] = targetStateBlock->streamSource[i];
902 if(This->vertexShader != targetStateBlock->vertexShader) {
903 if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
904 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
905 This->vertexShader = targetStateBlock->vertexShader;
907 if(This->pixelShader != targetStateBlock->pixelShader) {
908 if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
909 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
910 This->pixelShader = targetStateBlock->pixelShader;
912 } else if(This->blockType == WINED3DSBT_VERTEXSTATE) {
913 memcpy(This->vertexShaderConstantB, targetStateBlock->vertexShaderConstantB, sizeof(This->vertexShaderConstantI));
914 memcpy(This->vertexShaderConstantI, targetStateBlock->vertexShaderConstantI, sizeof(This->vertexShaderConstantF));
915 memcpy(This->vertexShaderConstantF, targetStateBlock->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
916 record_lights(This, targetStateBlock);
917 for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
919 This->renderState[vertex_states_render[i]] = targetStateBlock->renderState[vertex_states_render[i]];
921 for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
922 for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
924 This->samplerState[j][vertex_states_sampler[i]] = targetStateBlock->samplerState[j][vertex_states_sampler[i]];
927 for (j = 0; j < MAX_TEXTURES; j++) {
928 for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
930 This->textureState[j][vertex_states_render[i]] = targetStateBlock->textureState[j][vertex_states_render[i]];
933 for(i = 0; i < MAX_STREAMS; i++) {
934 if(targetStateBlock->streamSource[i] != This->streamSource[i]) {
935 if (targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
936 if (This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
937 This->streamSource[i] = targetStateBlock->streamSource[i];
940 if(This->vertexShader != targetStateBlock->vertexShader) {
941 if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
942 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
943 This->vertexShader = targetStateBlock->vertexShader;
945 } else if(This->blockType == WINED3DSBT_PIXELSTATE) {
946 memcpy(This->pixelShaderConstantB, targetStateBlock->pixelShaderConstantB, sizeof(This->pixelShaderConstantI));
947 memcpy(This->pixelShaderConstantI, targetStateBlock->pixelShaderConstantI, sizeof(This->pixelShaderConstantF));
948 memcpy(This->pixelShaderConstantF, targetStateBlock->pixelShaderConstantF, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
949 for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
951 This->renderState[pixel_states_render[i]] = targetStateBlock->renderState[pixel_states_render[i]];
953 for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
954 for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
956 This->samplerState[j][pixel_states_sampler[i]] = targetStateBlock->samplerState[j][pixel_states_sampler[i]];
959 for (j = 0; j < MAX_TEXTURES; j++) {
960 for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
962 This->textureState[j][pixel_states_render[i]] = targetStateBlock->textureState[j][pixel_states_render[i]];
965 if(This->pixelShader != targetStateBlock->pixelShader) {
966 if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
967 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
968 This->pixelShader = targetStateBlock->pixelShader;
972 TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
977 static void apply_lights(IWineD3DDevice *pDevice, const IWineD3DStateBlockImpl *This)
980 for(i = 0; i < LIGHTMAP_SIZE; i++) {
983 LIST_FOR_EACH(e, &This->lightMap[i]) {
984 const PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
987 IWineD3DDevice_SetLight(pDevice, light->OriginalIndex, &light->OriginalParms);
989 if(light->enabledChanged) {
990 IWineD3DDevice_SetLightEnable(pDevice, light->OriginalIndex, light->glIndex != -1);
996 static HRESULT WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
997 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
998 IWineD3DDevice* pDevice = (IWineD3DDevice*)This->wineD3DDevice;
1000 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
1001 should really perform a delta so that only the changes get updated*/
1007 TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
1009 TRACE("Blocktype: %d\n", This->blockType);
1011 if(This->blockType == WINED3DSBT_RECORDED) {
1012 if (This->changed.vertexShader) {
1013 IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
1015 /* Vertex Shader Constants */
1016 for (i = 0; i < This->num_contained_vs_consts_f; i++) {
1017 IWineD3DDevice_SetVertexShaderConstantF(pDevice, This->contained_vs_consts_f[i],
1018 This->vertexShaderConstantF + This->contained_vs_consts_f[i] * 4, 1);
1020 for (i = 0; i < This->num_contained_vs_consts_i; i++) {
1021 IWineD3DDevice_SetVertexShaderConstantI(pDevice, This->contained_vs_consts_i[i],
1022 This->vertexShaderConstantI + This->contained_vs_consts_i[i] * 4, 1);
1024 for (i = 0; i < This->num_contained_vs_consts_b; i++) {
1025 IWineD3DDevice_SetVertexShaderConstantB(pDevice, This->contained_vs_consts_b[i],
1026 This->vertexShaderConstantB + This->contained_vs_consts_b[i], 1);
1029 apply_lights(pDevice, This);
1031 if (This->changed.pixelShader) {
1032 IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
1034 /* Pixel Shader Constants */
1035 for (i = 0; i < This->num_contained_ps_consts_f; i++) {
1036 IWineD3DDevice_SetPixelShaderConstantF(pDevice, This->contained_ps_consts_f[i],
1037 This->pixelShaderConstantF + This->contained_ps_consts_f[i] * 4, 1);
1039 for (i = 0; i < This->num_contained_ps_consts_i; i++) {
1040 IWineD3DDevice_SetPixelShaderConstantI(pDevice, This->contained_ps_consts_i[i],
1041 This->pixelShaderConstantI + This->contained_ps_consts_i[i] * 4, 1);
1043 for (i = 0; i < This->num_contained_ps_consts_b; i++) {
1044 IWineD3DDevice_SetPixelShaderConstantB(pDevice, This->contained_ps_consts_b[i],
1045 This->pixelShaderConstantB + This->contained_ps_consts_b[i], 1);
1049 for (i = 0; i <= This->num_contained_render_states; i++) {
1050 IWineD3DDevice_SetRenderState(pDevice, This->contained_render_states[i],
1051 This->renderState[This->contained_render_states[i]]);
1053 /* Texture states */
1054 for (i = 0; i < This->num_contained_tss_states; i++) {
1055 DWORD stage = This->contained_tss_states[i].stage;
1056 DWORD state = This->contained_tss_states[i].state;
1057 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[stage][state] = This->textureState[stage][state];
1058 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[stage] |= 1 << state;
1059 /* TODO: Record a display list to apply all gl states. For now apply by brute force */
1060 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(stage, state));
1062 /* Sampler states */
1063 for (i = 0; i < This->num_contained_sampler_states; i++) {
1064 DWORD stage = This->contained_sampler_states[i].stage;
1065 DWORD state = This->contained_sampler_states[i].state;
1066 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[stage][state] = This->samplerState[stage][state];
1067 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[stage] |= 1 << state;
1068 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_SAMPLER(stage));
1071 for (i = 0; i < This->num_contained_transform_states; i++) {
1072 IWineD3DDevice_SetTransform(pDevice, This->contained_transform_states[i],
1073 &This->transforms[This->contained_transform_states[i]]);
1076 if (This->changed.primitive_type)
1078 This->wineD3DDevice->updateStateBlock->changed.primitive_type = TRUE;
1079 This->wineD3DDevice->updateStateBlock->gl_primitive_type = This->gl_primitive_type;
1082 if (This->changed.indices)
1084 IWineD3DDevice_SetIndexBuffer(pDevice, This->pIndexData, This->IndexFmt);
1085 IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
1088 if (This->changed.vertexDecl) {
1089 IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
1092 if (This->changed.material ) {
1093 IWineD3DDevice_SetMaterial(pDevice, &This->material);
1096 if (This->changed.viewport) {
1097 IWineD3DDevice_SetViewport(pDevice, &This->viewport);
1100 if (This->changed.scissorRect) {
1101 IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
1104 /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
1105 map = This->changed.streamSource;
1106 for (i = 0; map; map >>= 1, ++i)
1108 if (map & 1) IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
1111 map = This->changed.streamFreq;
1112 for (i = 0; map; map >>= 1, ++i)
1114 if (map & 1) IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
1117 map = This->changed.textures;
1118 for (i = 0; map; map >>= 1, ++i)
1120 if (!(map & 1)) continue;
1122 if (i < MAX_FRAGMENT_SAMPLERS) IWineD3DDevice_SetTexture(pDevice, i, This->textures[i]);
1123 else IWineD3DDevice_SetTexture(pDevice, WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS,
1127 map = This->changed.clipplane;
1128 for (i = 0; map; map >>= 1, ++i)
1132 if (!(map & 1)) continue;
1134 clip[0] = This->clipplane[i][0];
1135 clip[1] = This->clipplane[i][1];
1136 clip[2] = This->clipplane[i][2];
1137 clip[3] = This->clipplane[i][3];
1138 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
1140 } else if(This->blockType == WINED3DSBT_VERTEXSTATE) {
1141 IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
1142 for (i = 0; i < GL_LIMITS(vshader_constantsF); i++) {
1143 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i,
1144 This->vertexShaderConstantF + i * 4, 1);
1146 for (i = 0; i < MAX_CONST_I; i++) {
1147 IWineD3DDevice_SetVertexShaderConstantI(pDevice, i,
1148 This->vertexShaderConstantI + i * 4, 1);
1150 for (i = 0; i < MAX_CONST_B; i++) {
1151 IWineD3DDevice_SetVertexShaderConstantB(pDevice, i,
1152 This->vertexShaderConstantB + i, 1);
1155 apply_lights(pDevice, This);
1157 for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
1159 IWineD3DDevice_SetRenderState(pDevice, vertex_states_render[i], This->renderState[vertex_states_render[i]]);
1161 for(j = 0; j < MAX_TEXTURES; j++) {
1162 for (i = 0; i < sizeof(vertex_states_texture) / sizeof(*vertex_states_texture); ++i)
1164 IWineD3DDevice_SetTextureStageState(pDevice, j, vertex_states_texture[i],
1165 This->textureState[j][vertex_states_texture[i]]);
1169 for(j = 0; j < MAX_FRAGMENT_SAMPLERS; j++) {
1170 for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
1172 IWineD3DDevice_SetSamplerState(pDevice, j, vertex_states_sampler[i],
1173 This->samplerState[j][vertex_states_sampler[i]]);
1176 for(j = MAX_FRAGMENT_SAMPLERS; j < MAX_COMBINED_SAMPLERS; j++) {
1177 for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
1179 IWineD3DDevice_SetSamplerState(pDevice, WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS,
1180 vertex_states_sampler[i], This->samplerState[j][vertex_states_sampler[i]]);
1183 } else if(This->blockType == WINED3DSBT_PIXELSTATE) {
1184 IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
1185 for (i = 0; i < GL_LIMITS(pshader_constantsF); i++) {
1186 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i,
1187 This->pixelShaderConstantF + i * 4, 1);
1189 for (i = 0; i < MAX_CONST_I; i++) {
1190 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i,
1191 This->pixelShaderConstantI + i * 4, 1);
1193 for (i = 0; i < MAX_CONST_B; i++) {
1194 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i,
1195 This->pixelShaderConstantB + i, 1);
1198 for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
1200 IWineD3DDevice_SetRenderState(pDevice, pixel_states_render[i], This->renderState[pixel_states_render[i]]);
1202 for(j = 0; j < MAX_TEXTURES; j++) {
1203 for (i = 0; i < sizeof(pixel_states_texture) / sizeof(*pixel_states_texture); ++i)
1205 IWineD3DDevice_SetTextureStageState(pDevice, j, pixel_states_texture[i],
1206 This->textureState[j][pixel_states_texture[i]]);
1210 for(j = 0; j < MAX_FRAGMENT_SAMPLERS; j++) {
1211 for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
1213 IWineD3DDevice_SetSamplerState(pDevice, j, pixel_states_sampler[i],
1214 This->samplerState[j][pixel_states_sampler[i]]);
1217 for(j = MAX_FRAGMENT_SAMPLERS; j < MAX_COMBINED_SAMPLERS; j++) {
1218 for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
1220 IWineD3DDevice_SetSamplerState(pDevice, WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS,
1221 pixel_states_sampler[i], This->samplerState[j][pixel_states_sampler[i]]);
1224 } else if(This->blockType == WINED3DSBT_ALL) {
1225 IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
1226 for (i = 0; i < GL_LIMITS(vshader_constantsF); i++) {
1227 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i,
1228 This->vertexShaderConstantF + i * 4, 1);
1230 for (i = 0; i < MAX_CONST_I; i++) {
1231 IWineD3DDevice_SetVertexShaderConstantI(pDevice, i,
1232 This->vertexShaderConstantI + i * 4, 1);
1234 for (i = 0; i < MAX_CONST_B; i++) {
1235 IWineD3DDevice_SetVertexShaderConstantB(pDevice, i,
1236 This->vertexShaderConstantB + i, 1);
1239 IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
1240 for (i = 0; i < GL_LIMITS(pshader_constantsF); i++) {
1241 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i,
1242 This->pixelShaderConstantF + i * 4, 1);
1244 for (i = 0; i < MAX_CONST_I; i++) {
1245 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i,
1246 This->pixelShaderConstantI + i * 4, 1);
1248 for (i = 0; i < MAX_CONST_B; i++) {
1249 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i,
1250 This->pixelShaderConstantB + i, 1);
1253 apply_lights(pDevice, This);
1255 for(i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
1256 IWineD3DDevice_SetRenderState(pDevice, i, This->renderState[i]);
1258 for(j = 0; j < MAX_TEXTURES; j++) {
1259 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
1261 IWineD3DDevice_SetTextureStageState(pDevice, j, i, This->textureState[j][i]);
1265 /* Skip unused values between TEXTURE8 and WORLD0 ? */
1266 for(i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
1267 IWineD3DDevice_SetTransform(pDevice, i, &This->transforms[i]);
1269 This->wineD3DDevice->updateStateBlock->gl_primitive_type = This->gl_primitive_type;
1270 IWineD3DDevice_SetIndexBuffer(pDevice, This->pIndexData, This->IndexFmt);
1271 IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
1272 IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
1273 IWineD3DDevice_SetMaterial(pDevice, &This->material);
1274 IWineD3DDevice_SetViewport(pDevice, &This->viewport);
1275 IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
1277 /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
1278 for (i=0; i<MAX_STREAMS; i++) {
1279 IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
1280 IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
1282 for (j = 0 ; j < MAX_COMBINED_SAMPLERS; j++){
1283 UINT sampler = j < MAX_FRAGMENT_SAMPLERS ? j : WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS;
1285 IWineD3DDevice_SetTexture(pDevice, sampler, This->textures[j]);
1286 for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; ++i)
1288 IWineD3DDevice_SetSamplerState(pDevice, sampler, i, This->samplerState[j][i]);
1291 for (i = 0; i < GL_LIMITS(clipplanes); i++) {
1294 clip[0] = This->clipplane[i][0];
1295 clip[1] = This->clipplane[i][1];
1296 clip[2] = This->clipplane[i][2];
1297 clip[3] = This->clipplane[i][3];
1298 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
1302 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = MAX_TEXTURES - 1;
1303 for(j = 0; j < MAX_TEXTURES - 1; j++) {
1304 if(((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE) {
1305 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = j;
1309 TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
1314 static HRESULT WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
1315 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
1316 IWineD3DDevice *device = (IWineD3DDevice *)This->wineD3DDevice;
1317 IWineD3DDeviceImpl *ThisDevice = (IWineD3DDeviceImpl *)device;
1319 WINED3DLINEPATTERN lp;
1327 IWineD3DSwapChain *swapchain;
1328 IWineD3DSurface *backbuffer;
1331 /* Note this may have a large overhead but it should only be executed
1332 once, in order to initialize the complete state of the device and
1333 all opengl equivalents */
1334 TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
1335 /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
1336 This->blockType = WINED3DSBT_INIT;
1338 /* Set some of the defaults for lights, transforms etc */
1339 memcpy(&This->transforms[WINED3DTS_PROJECTION], identity, sizeof(identity));
1340 memcpy(&This->transforms[WINED3DTS_VIEW], identity, sizeof(identity));
1341 for (i = 0; i < 256; ++i) {
1342 memcpy(&This->transforms[WINED3DTS_WORLDMATRIX(i)], identity, sizeof(identity));
1345 TRACE("Render states\n");
1346 /* Render states: */
1347 if (ThisDevice->auto_depth_stencil_buffer != NULL) {
1348 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, WINED3DZB_TRUE);
1350 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, WINED3DZB_FALSE);
1352 IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE, WINED3DFILL_SOLID);
1353 IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE, WINED3DSHADE_GOURAUD);
1354 lp.lp.wRepeatFactor = 0;
1355 lp.lp.wLinePattern = 0;
1356 IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN, lp.d);
1357 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE, TRUE);
1358 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE, FALSE);
1359 IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL, TRUE);
1360 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND, WINED3DBLEND_ONE);
1361 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND, WINED3DBLEND_ZERO);
1362 IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE, WINED3DCULL_CCW);
1363 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC, WINED3DCMP_LESSEQUAL);
1364 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC, WINED3DCMP_ALWAYS);
1365 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF, 0);
1366 IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE, FALSE);
1367 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
1368 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE, FALSE);
1369 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE, FALSE);
1370 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE, 0);
1371 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR, 0);
1372 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE, WINED3DFOG_NONE);
1374 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART, tmpfloat.d);
1376 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND, tmpfloat.d);
1378 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY, tmpfloat.d);
1379 IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS, FALSE);
1380 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS, 0);
1381 IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE, FALSE);
1382 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE, FALSE);
1383 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL, WINED3DSTENCILOP_KEEP);
1384 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL, WINED3DSTENCILOP_KEEP);
1385 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS, WINED3DSTENCILOP_KEEP);
1386 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF, 0);
1387 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK, 0xFFFFFFFF);
1388 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC, WINED3DCMP_ALWAYS);
1389 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
1390 IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR, 0xFFFFFFFF);
1391 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
1392 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
1393 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
1394 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
1395 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
1396 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
1397 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
1398 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
1399 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING, TRUE);
1400 IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING, TRUE);
1401 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT, 0);
1402 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE, WINED3DFOG_NONE);
1403 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX, TRUE);
1404 IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER, TRUE);
1405 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS, FALSE);
1406 IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE, WINED3DMCS_COLOR1);
1407 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE, WINED3DMCS_COLOR2);
1408 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE, WINED3DMCS_MATERIAL);
1409 IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE, WINED3DMCS_MATERIAL);
1410 IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND, WINED3DVBF_DISABLE);
1411 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE, 0);
1412 IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
1414 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE, tmpfloat.d);
1415 tmpfloat.f = ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion < 9 ? 0.0f : 1.0f;
1416 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN, tmpfloat.d);
1417 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE, FALSE);
1418 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE, FALSE);
1420 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A, tmpfloat.d);
1422 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B, tmpfloat.d);
1424 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C, tmpfloat.d);
1425 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS, TRUE);
1426 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1427 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE, WINED3DPATCHEDGE_DISCRETE);
1429 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS, tmpfloat.d);
1430 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN, 0xbaadcafe);
1431 tmpfloat.f = GL_LIMITS(pointsize);
1432 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX, tmpfloat.d);
1433 IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
1434 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE, 0x0000000F);
1436 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR, tmpfloat.d);
1437 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP, WINED3DBLENDOP_ADD);
1438 IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE, WINED3DDEGREE_CUBIC);
1439 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE, WINED3DDEGREE_LINEAR);
1440 /* states new in d3d9 */
1441 IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE, FALSE);
1442 IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS, 0);
1444 IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL, tmpfloat.d);
1445 IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL, tmpfloat.d);
1446 IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE, FALSE);
1448 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X, tmpfloat.d);
1449 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y, tmpfloat.d);
1451 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z, tmpfloat.d);
1453 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W, tmpfloat.d);
1454 IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
1455 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE, FALSE);
1456 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL, WINED3DSTENCILOP_KEEP);
1457 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL, WINED3DSTENCILOP_KEEP);
1458 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS, WINED3DSTENCILOP_KEEP);
1459 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC, WINED3DCMP_ALWAYS);
1460 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1, 0x0000000F);
1461 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2, 0x0000000F);
1462 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3, 0x0000000F);
1463 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR, 0xFFFFFFFF);
1464 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE, 0);
1465 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS, 0);
1466 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8, 0);
1467 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9, 0);
1468 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
1469 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
1470 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
1471 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
1472 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
1473 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
1474 IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1475 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA, WINED3DBLEND_ONE);
1476 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA, WINED3DBLEND_ZERO);
1477 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA, WINED3DBLENDOP_ADD);
1479 /* clipping status */
1480 This->clip_status.ClipUnion = 0;
1481 This->clip_status.ClipIntersection = 0xFFFFFFFF;
1483 /* Texture Stage States - Put directly into state block, we will call function below */
1484 for (i = 0; i < MAX_TEXTURES; i++) {
1485 TRACE("Setting up default texture states for texture Stage %d\n", i);
1486 memcpy(&This->transforms[WINED3DTS_TEXTURE0 + i], identity, sizeof(identity));
1487 This->textureState[i][WINED3DTSS_COLOROP ] = (i==0)? WINED3DTOP_MODULATE : WINED3DTOP_DISABLE;
1488 This->textureState[i][WINED3DTSS_COLORARG1 ] = WINED3DTA_TEXTURE;
1489 This->textureState[i][WINED3DTSS_COLORARG2 ] = WINED3DTA_CURRENT;
1490 This->textureState[i][WINED3DTSS_ALPHAOP ] = (i==0)? WINED3DTOP_SELECTARG1 : WINED3DTOP_DISABLE;
1491 This->textureState[i][WINED3DTSS_ALPHAARG1 ] = WINED3DTA_TEXTURE;
1492 This->textureState[i][WINED3DTSS_ALPHAARG2 ] = WINED3DTA_CURRENT;
1493 This->textureState[i][WINED3DTSS_BUMPENVMAT00 ] = 0;
1494 This->textureState[i][WINED3DTSS_BUMPENVMAT01 ] = 0;
1495 This->textureState[i][WINED3DTSS_BUMPENVMAT10 ] = 0;
1496 This->textureState[i][WINED3DTSS_BUMPENVMAT11 ] = 0;
1497 This->textureState[i][WINED3DTSS_TEXCOORDINDEX ] = i;
1498 This->textureState[i][WINED3DTSS_BUMPENVLSCALE ] = 0;
1499 This->textureState[i][WINED3DTSS_BUMPENVLOFFSET ] = 0;
1500 This->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS ] = WINED3DTTFF_DISABLE;
1501 This->textureState[i][WINED3DTSS_COLORARG0 ] = WINED3DTA_CURRENT;
1502 This->textureState[i][WINED3DTSS_ALPHAARG0 ] = WINED3DTA_CURRENT;
1503 This->textureState[i][WINED3DTSS_RESULTARG ] = WINED3DTA_CURRENT;
1505 This->lowest_disabled_stage = 1;
1508 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; i++) {
1509 TRACE("Setting up default samplers states for sampler %d\n", i);
1510 This->samplerState[i][WINED3DSAMP_ADDRESSU ] = WINED3DTADDRESS_WRAP;
1511 This->samplerState[i][WINED3DSAMP_ADDRESSV ] = WINED3DTADDRESS_WRAP;
1512 This->samplerState[i][WINED3DSAMP_ADDRESSW ] = WINED3DTADDRESS_WRAP;
1513 This->samplerState[i][WINED3DSAMP_BORDERCOLOR ] = 0x00;
1514 This->samplerState[i][WINED3DSAMP_MAGFILTER ] = WINED3DTEXF_POINT;
1515 This->samplerState[i][WINED3DSAMP_MINFILTER ] = WINED3DTEXF_POINT;
1516 This->samplerState[i][WINED3DSAMP_MIPFILTER ] = WINED3DTEXF_NONE;
1517 This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS ] = 0;
1518 This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL ] = 0;
1519 This->samplerState[i][WINED3DSAMP_MAXANISOTROPY ] = 1;
1520 This->samplerState[i][WINED3DSAMP_SRGBTEXTURE ] = 0;
1521 This->samplerState[i][WINED3DSAMP_ELEMENTINDEX ] = 0; /* TODO: Indicates which element of a multielement texture to use */
1522 This->samplerState[i][WINED3DSAMP_DMAPOFFSET ] = 0; /* TODO: Vertex offset in the presampled displacement map */
1525 for(i = 0; i < GL_LIMITS(textures); i++) {
1526 /* Note: This avoids calling SetTexture, so pretend it has been called */
1527 This->changed.textures |= 1 << i;
1528 This->textures[i] = NULL;
1531 /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1532 hr = IWineD3DDevice_GetSwapChain(device, 0, &swapchain);
1533 if( hr == WINED3D_OK && swapchain != NULL) {
1536 hr = IWineD3DSwapChain_GetBackBuffer(swapchain, 0, WINED3DBACKBUFFER_TYPE_MONO, &backbuffer);
1537 if (SUCCEEDED(hr) && backbuffer)
1539 WINED3DSURFACE_DESC desc;
1542 IWineD3DSurface_GetDesc(backbuffer, &desc);
1543 IWineD3DSurface_Release(backbuffer);
1545 /* Set the default scissor rect values */
1546 scissorrect.left = 0;
1547 scissorrect.right = desc.width;
1548 scissorrect.top = 0;
1549 scissorrect.bottom = desc.height;
1550 hr = IWineD3DDevice_SetScissorRect(device, &scissorrect);
1551 if (FAILED(hr)) ERR("This should never happen, expect rendering issues!\n");
1554 /* Set the default viewport */
1557 vp.Width = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferWidth;
1558 vp.Height = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferHeight;
1561 IWineD3DDevice_SetViewport(device, &vp);
1563 IWineD3DSwapChain_Release(swapchain);
1566 TRACE("-----------------------> Device defaults now set up...\n");
1570 /**********************************************************
1571 * IWineD3DStateBlock VTbl follows
1572 **********************************************************/
1574 static const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
1577 IWineD3DStateBlockImpl_QueryInterface,
1578 IWineD3DStateBlockImpl_AddRef,
1579 IWineD3DStateBlockImpl_Release,
1580 /* IWineD3DStateBlock */
1581 IWineD3DStateBlockImpl_GetParent,
1582 IWineD3DStateBlockImpl_GetDevice,
1583 IWineD3DStateBlockImpl_Capture,
1584 IWineD3DStateBlockImpl_Apply,
1585 IWineD3DStateBlockImpl_InitStartupStateBlock
1588 HRESULT stateblock_init(IWineD3DStateBlockImpl *stateblock, IWineD3DDeviceImpl *device,
1589 WINED3DSTATEBLOCKTYPE type, IUnknown *parent)
1591 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1595 stateblock->lpVtbl = &IWineD3DStateBlock_Vtbl;
1596 stateblock->ref = 1;
1597 stateblock->parent = parent;
1598 stateblock->wineD3DDevice = device;
1599 stateblock->blockType = type;
1601 for (i = 0; i < LIGHTMAP_SIZE; i++)
1603 list_init(&stateblock->lightMap[i]);
1606 hr = stateblock_allocate_shader_constants(stateblock);
1607 if (FAILED(hr)) return hr;
1609 /* The WINED3DSBT_INIT stateblock type is used during initialization to
1610 * produce a placeholder stateblock so other functions called can update a
1612 if (type == WINED3DSBT_INIT || type == WINED3DSBT_RECORDED) return WINED3D_OK;
1614 /* Otherwise, might as well set the whole state block to the appropriate values */
1615 if (device->stateBlock)
1618 stateblock_copy_values(stateblock, device->stateBlock, gl_info);
1622 memset(stateblock->streamFreq, 1, sizeof(stateblock->streamFreq));
1625 TRACE("Updating changed flags appropriate for type %#x.\n", type);
1627 if (type == WINED3DSBT_ALL)
1629 TRACE("ALL => Pretend everything has changed.\n");
1631 stateblock_savedstates_set_all(&stateblock->changed, gl_info);
1632 stateblock_init_contained_states(stateblock);
1634 /* Lights are not part of the changed / set structure. */
1635 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1638 LIST_FOR_EACH(e, &stateblock->lightMap[i])
1640 PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
1641 light->changed = TRUE;
1642 light->enabledChanged = TRUE;
1646 for (i = 0; i < MAX_STREAMS; ++i)
1648 if (stateblock->streamSource[i]) IWineD3DBuffer_AddRef(stateblock->streamSource[i]);
1651 if (stateblock->pIndexData) IWineD3DBuffer_AddRef(stateblock->pIndexData);
1652 if (stateblock->vertexShader) IWineD3DVertexShader_AddRef(stateblock->vertexShader);
1653 if (stateblock->pixelShader) IWineD3DPixelShader_AddRef(stateblock->pixelShader);
1655 else if (type == WINED3DSBT_PIXELSTATE)
1657 TRACE("PIXELSTATE => Pretend all pixel states have changed.\n");
1659 stateblock_savedstates_set_pixel(&stateblock->changed, gl_info);
1660 stateblock_init_contained_states(stateblock);
1662 if (stateblock->pixelShader) IWineD3DPixelShader_AddRef(stateblock->pixelShader);
1664 /* Pixel state blocks do not contain vertex buffers. Set them to NULL
1665 * to avoid wrong refcounting on them. This makes releasing the buffer
1667 for (i = 0; i < MAX_STREAMS; ++i)
1669 stateblock->streamSource[i] = NULL;
1671 stateblock->pIndexData = NULL;
1672 stateblock->vertexShader = NULL;
1674 else if (type == WINED3DSBT_VERTEXSTATE)
1676 TRACE("VERTEXSTATE => Pretend all vertex shates have changed.\n");
1678 stateblock_savedstates_set_vertex(&stateblock->changed, gl_info);
1679 stateblock_init_contained_states(stateblock);
1681 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1684 LIST_FOR_EACH(e, &stateblock->lightMap[i])
1686 PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
1687 light->changed = TRUE;
1688 light->enabledChanged = TRUE;
1692 for (i = 0; i < MAX_STREAMS; ++i)
1694 if (stateblock->streamSource[i]) IWineD3DBuffer_AddRef(stateblock->streamSource[i]);
1697 if (stateblock->vertexShader) IWineD3DVertexShader_AddRef(stateblock->vertexShader);
1699 stateblock->pIndexData = NULL;
1700 stateblock->pixelShader = NULL;
1704 FIXME("Unrecognized state block type %#x.\n", type);