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);
30 static const DWORD pixel_states_render[] =
32 WINED3DRS_ALPHABLENDENABLE,
35 WINED3DRS_ALPHATESTENABLE,
36 WINED3DRS_ANTIALIASEDLINEENABLE,
37 WINED3DRS_BLENDFACTOR,
39 WINED3DRS_BLENDOPALPHA,
40 WINED3DRS_CCW_STENCILFAIL,
41 WINED3DRS_CCW_STENCILPASS,
42 WINED3DRS_CCW_STENCILZFAIL,
43 WINED3DRS_COLORWRITEENABLE,
44 WINED3DRS_COLORWRITEENABLE1,
45 WINED3DRS_COLORWRITEENABLE2,
46 WINED3DRS_COLORWRITEENABLE3,
49 WINED3DRS_DESTBLENDALPHA,
50 WINED3DRS_DITHERENABLE,
56 WINED3DRS_SCISSORTESTENABLE,
57 WINED3DRS_SEPARATEALPHABLENDENABLE,
59 WINED3DRS_SLOPESCALEDEPTHBIAS,
61 WINED3DRS_SRCBLENDALPHA,
62 WINED3DRS_SRGBWRITEENABLE,
63 WINED3DRS_STENCILENABLE,
64 WINED3DRS_STENCILFAIL,
65 WINED3DRS_STENCILFUNC,
66 WINED3DRS_STENCILMASK,
67 WINED3DRS_STENCILPASS,
69 WINED3DRS_STENCILWRITEMASK,
70 WINED3DRS_STENCILZFAIL,
71 WINED3DRS_TEXTUREFACTOR,
72 WINED3DRS_TWOSIDEDSTENCILMODE,
91 WINED3DRS_ZWRITEENABLE,
94 static const DWORD pixel_states_texture[] =
100 WINED3DTSS_BUMPENVLOFFSET,
101 WINED3DTSS_BUMPENVLSCALE,
102 WINED3DTSS_BUMPENVMAT00,
103 WINED3DTSS_BUMPENVMAT01,
104 WINED3DTSS_BUMPENVMAT10,
105 WINED3DTSS_BUMPENVMAT11,
106 WINED3DTSS_COLORARG0,
107 WINED3DTSS_COLORARG1,
108 WINED3DTSS_COLORARG2,
110 WINED3DTSS_RESULTARG,
111 WINED3DTSS_TEXCOORDINDEX,
112 WINED3DTSS_TEXTURETRANSFORMFLAGS,
115 static const DWORD pixel_states_sampler[] =
117 WINED3DSAMP_ADDRESSU,
118 WINED3DSAMP_ADDRESSV,
119 WINED3DSAMP_ADDRESSW,
120 WINED3DSAMP_BORDERCOLOR,
121 WINED3DSAMP_MAGFILTER,
122 WINED3DSAMP_MINFILTER,
123 WINED3DSAMP_MIPFILTER,
124 WINED3DSAMP_MIPMAPLODBIAS,
125 WINED3DSAMP_MAXMIPLEVEL,
126 WINED3DSAMP_MAXANISOTROPY,
127 WINED3DSAMP_SRGBTEXTURE,
128 WINED3DSAMP_ELEMENTINDEX,
131 static const DWORD vertex_states_render[] =
133 WINED3DRS_ADAPTIVETESS_W,
134 WINED3DRS_ADAPTIVETESS_X,
135 WINED3DRS_ADAPTIVETESS_Y,
136 WINED3DRS_ADAPTIVETESS_Z,
138 WINED3DRS_AMBIENTMATERIALSOURCE,
140 WINED3DRS_CLIPPLANEENABLE,
141 WINED3DRS_COLORVERTEX,
143 WINED3DRS_DIFFUSEMATERIALSOURCE,
144 WINED3DRS_EMISSIVEMATERIALSOURCE,
145 WINED3DRS_ENABLEADAPTIVETESSELLATION,
147 WINED3DRS_FOGDENSITY,
151 WINED3DRS_FOGTABLEMODE,
152 WINED3DRS_FOGVERTEXMODE,
153 WINED3DRS_INDEXEDVERTEXBLENDENABLE,
155 WINED3DRS_LOCALVIEWER,
156 WINED3DRS_MAXTESSELLATIONLEVEL,
157 WINED3DRS_MINTESSELLATIONLEVEL,
158 WINED3DRS_MULTISAMPLEANTIALIAS,
159 WINED3DRS_MULTISAMPLEMASK,
160 WINED3DRS_NORMALDEGREE,
161 WINED3DRS_NORMALIZENORMALS,
162 WINED3DRS_PATCHEDGESTYLE,
163 WINED3DRS_POINTSCALE_A,
164 WINED3DRS_POINTSCALE_B,
165 WINED3DRS_POINTSCALE_C,
166 WINED3DRS_POINTSCALEENABLE,
168 WINED3DRS_POINTSIZE_MAX,
169 WINED3DRS_POINTSIZE_MIN,
170 WINED3DRS_POINTSPRITEENABLE,
171 WINED3DRS_POSITIONDEGREE,
172 WINED3DRS_RANGEFOGENABLE,
174 WINED3DRS_SPECULARENABLE,
175 WINED3DRS_SPECULARMATERIALSOURCE,
176 WINED3DRS_TWEENFACTOR,
177 WINED3DRS_VERTEXBLEND,
180 static const DWORD vertex_states_texture[] =
182 WINED3DTSS_TEXCOORDINDEX,
183 WINED3DTSS_TEXTURETRANSFORMFLAGS,
186 static const DWORD vertex_states_sampler[] =
188 WINED3DSAMP_DMAPOFFSET,
191 /* Allocates the correct amount of space for pixel and vertex shader constants,
192 * along with their set/changed flags on the given stateblock object
194 static HRESULT stateblock_allocate_shader_constants(IWineD3DStateBlockImpl *object)
196 IWineD3DDeviceImpl *device = object->device;
198 /* Allocate space for floating point constants */
199 object->state.ps_consts_f = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
200 sizeof(float) * device->d3d_pshader_constantF * 4);
201 if (!object->state.ps_consts_f) goto fail;
203 object->changed.pixelShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
204 sizeof(BOOL) * device->d3d_pshader_constantF);
205 if (!object->changed.pixelShaderConstantsF) goto fail;
207 object->state.vs_consts_f = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
208 sizeof(float) * device->d3d_vshader_constantF * 4);
209 if (!object->state.vs_consts_f) goto fail;
211 object->changed.vertexShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
212 sizeof(BOOL) * device->d3d_vshader_constantF);
213 if (!object->changed.vertexShaderConstantsF) goto fail;
215 object->contained_vs_consts_f = HeapAlloc(GetProcessHeap(), 0,
216 sizeof(DWORD) * device->d3d_vshader_constantF);
217 if (!object->contained_vs_consts_f) goto fail;
219 object->contained_ps_consts_f = HeapAlloc(GetProcessHeap(), 0,
220 sizeof(DWORD) * device->d3d_pshader_constantF);
221 if (!object->contained_ps_consts_f) goto fail;
226 ERR("Failed to allocate memory\n");
227 HeapFree(GetProcessHeap(), 0, object->state.ps_consts_f);
228 HeapFree(GetProcessHeap(), 0, object->changed.pixelShaderConstantsF);
229 HeapFree(GetProcessHeap(), 0, object->state.vs_consts_f);
230 HeapFree(GetProcessHeap(), 0, object->changed.vertexShaderConstantsF);
231 HeapFree(GetProcessHeap(), 0, object->contained_vs_consts_f);
232 HeapFree(GetProcessHeap(), 0, object->contained_ps_consts_f);
233 return E_OUTOFMEMORY;
236 static inline void stateblock_set_bits(DWORD *map, UINT map_size)
238 DWORD mask = (1 << (map_size & 0x1f)) - 1;
239 memset(map, 0xff, (map_size >> 5) * sizeof(*map));
240 if (mask) map[map_size >> 5] = mask;
243 /* Set all members of a stateblock savedstate to the given value */
244 static void stateblock_savedstates_set_all(SAVEDSTATES *states, DWORD vs_consts, DWORD ps_consts)
249 states->primitive_type = 1;
251 states->material = 1;
252 states->viewport = 1;
253 states->vertexDecl = 1;
254 states->pixelShader = 1;
255 states->vertexShader = 1;
256 states->scissorRect = 1;
258 /* Fixed size arrays */
259 states->streamSource = 0xffff;
260 states->streamFreq = 0xffff;
261 states->textures = 0xfffff;
262 stateblock_set_bits(states->transform, HIGHEST_TRANSFORMSTATE + 1);
263 stateblock_set_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
264 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
265 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3ffe;
266 states->clipplane = 0xffffffff;
267 states->pixelShaderConstantsB = 0xffff;
268 states->pixelShaderConstantsI = 0xffff;
269 states->vertexShaderConstantsB = 0xffff;
270 states->vertexShaderConstantsI = 0xffff;
272 /* Dynamically sized arrays */
273 memset(states->pixelShaderConstantsF, TRUE, sizeof(BOOL) * ps_consts);
274 memset(states->vertexShaderConstantsF, TRUE, sizeof(BOOL) * vs_consts);
277 static void stateblock_savedstates_set_pixel(SAVEDSTATES *states, const DWORD num_constants)
279 DWORD texture_mask = 0;
280 WORD sampler_mask = 0;
283 states->pixelShader = 1;
285 for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
287 DWORD rs = pixel_states_render[i];
288 states->renderState[rs >> 5] |= 1 << (rs & 0x1f);
291 for (i = 0; i < sizeof(pixel_states_texture) / sizeof(*pixel_states_texture); ++i)
292 texture_mask |= 1 << pixel_states_texture[i];
293 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
294 for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
295 sampler_mask |= 1 << pixel_states_sampler[i];
296 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
297 states->pixelShaderConstantsB = 0xffff;
298 states->pixelShaderConstantsI = 0xffff;
300 memset(states->pixelShaderConstantsF, TRUE, sizeof(BOOL) * num_constants);
303 static void stateblock_savedstates_set_vertex(SAVEDSTATES *states, const DWORD num_constants)
305 DWORD texture_mask = 0;
306 WORD sampler_mask = 0;
309 states->vertexDecl = 1;
310 states->vertexShader = 1;
312 for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
314 DWORD rs = vertex_states_render[i];
315 states->renderState[rs >> 5] |= 1 << (rs & 0x1f);
318 for (i = 0; i < sizeof(vertex_states_texture) / sizeof(*vertex_states_texture); ++i)
319 texture_mask |= 1 << vertex_states_texture[i];
320 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
321 for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
322 sampler_mask |= 1 << vertex_states_sampler[i];
323 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
324 states->vertexShaderConstantsB = 0xffff;
325 states->vertexShaderConstantsI = 0xffff;
327 memset(states->vertexShaderConstantsF, TRUE, sizeof(BOOL) * num_constants);
330 void stateblock_init_contained_states(IWineD3DStateBlockImpl *stateblock)
332 IWineD3DDeviceImpl *device = stateblock->device;
335 for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
337 DWORD map = stateblock->changed.renderState[i];
338 for (j = 0; map; map >>= 1, ++j)
340 if (!(map & 1)) continue;
342 stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
343 ++stateblock->num_contained_render_states;
347 for (i = 0; i <= HIGHEST_TRANSFORMSTATE >> 5; ++i)
349 DWORD map = stateblock->changed.transform[i];
350 for (j = 0; map; map >>= 1, ++j)
352 if (!(map & 1)) continue;
354 stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
355 ++stateblock->num_contained_transform_states;
359 for (i = 0; i < device->d3d_vshader_constantF; ++i)
361 if (stateblock->changed.vertexShaderConstantsF[i])
363 stateblock->contained_vs_consts_f[stateblock->num_contained_vs_consts_f] = i;
364 ++stateblock->num_contained_vs_consts_f;
368 for (i = 0; i < MAX_CONST_I; ++i)
370 if (stateblock->changed.vertexShaderConstantsI & (1 << i))
372 stateblock->contained_vs_consts_i[stateblock->num_contained_vs_consts_i] = i;
373 ++stateblock->num_contained_vs_consts_i;
377 for (i = 0; i < MAX_CONST_B; ++i)
379 if (stateblock->changed.vertexShaderConstantsB & (1 << i))
381 stateblock->contained_vs_consts_b[stateblock->num_contained_vs_consts_b] = i;
382 ++stateblock->num_contained_vs_consts_b;
386 for (i = 0; i < device->d3d_pshader_constantF; ++i)
388 if (stateblock->changed.pixelShaderConstantsF[i])
390 stateblock->contained_ps_consts_f[stateblock->num_contained_ps_consts_f] = i;
391 ++stateblock->num_contained_ps_consts_f;
395 for (i = 0; i < MAX_CONST_I; ++i)
397 if (stateblock->changed.pixelShaderConstantsI & (1 << i))
399 stateblock->contained_ps_consts_i[stateblock->num_contained_ps_consts_i] = i;
400 ++stateblock->num_contained_ps_consts_i;
404 for (i = 0; i < MAX_CONST_B; ++i)
406 if (stateblock->changed.pixelShaderConstantsB & (1 << i))
408 stateblock->contained_ps_consts_b[stateblock->num_contained_ps_consts_b] = i;
409 ++stateblock->num_contained_ps_consts_b;
413 for (i = 0; i < MAX_TEXTURES; ++i)
415 DWORD map = stateblock->changed.textureState[i];
417 for(j = 0; map; map >>= 1, ++j)
419 if (!(map & 1)) continue;
421 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
422 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
423 ++stateblock->num_contained_tss_states;
427 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
429 DWORD map = stateblock->changed.samplerState[i];
431 for (j = 0; map; map >>= 1, ++j)
433 if (!(map & 1)) continue;
435 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
436 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
437 ++stateblock->num_contained_sampler_states;
442 static void stateblock_init_lights(IWineD3DStateBlockImpl *stateblock, struct list *light_map)
446 for (i = 0; i < LIGHTMAP_SIZE; ++i)
448 const struct wined3d_light_info *src_light;
450 LIST_FOR_EACH_ENTRY(src_light, &light_map[i], struct wined3d_light_info, entry)
452 struct wined3d_light_info *dst_light = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_light));
454 *dst_light = *src_light;
455 list_add_tail(&stateblock->state.light_map[i], &dst_light->entry);
460 /**********************************************************
461 * IWineD3DStateBlockImpl IUnknown parts follows
462 **********************************************************/
463 static HRESULT WINAPI IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock *iface,REFIID riid,LPVOID *ppobj)
465 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
466 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
467 if (IsEqualGUID(riid, &IID_IUnknown)
468 || IsEqualGUID(riid, &IID_IWineD3DStateBlock))
470 IUnknown_AddRef(iface);
475 return E_NOINTERFACE;
478 static ULONG WINAPI IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock *iface) {
479 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
480 ULONG refCount = InterlockedIncrement(&This->ref);
482 TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
486 static ULONG WINAPI IWineD3DStateBlockImpl_Release(IWineD3DStateBlock *iface) {
487 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
488 ULONG refCount = InterlockedDecrement(&This->ref);
490 TRACE("(%p) : Releasing from %d\n", This, refCount + 1);
495 if (This->state.vertex_declaration)
496 IWineD3DVertexDeclaration_Release((IWineD3DVertexDeclaration *)This->state.vertex_declaration);
498 for (counter = 0; counter < MAX_COMBINED_SAMPLERS; counter++)
500 if (This->state.textures[counter])
501 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *)This->state.textures[counter]);
504 for (counter = 0; counter < MAX_STREAMS; ++counter)
506 struct wined3d_buffer *buffer = This->state.streams[counter].buffer;
509 if (IWineD3DBuffer_Release((IWineD3DBuffer *)buffer))
511 WARN("Buffer %p still referenced by stateblock, stream %u.\n", buffer, counter);
515 if (This->state.index_buffer) IWineD3DBuffer_Release((IWineD3DBuffer *)This->state.index_buffer);
516 if (This->state.vertex_shader) IWineD3DVertexShader_Release((IWineD3DVertexShader *)This->state.vertex_shader);
517 if (This->state.pixel_shader) IWineD3DPixelShader_Release((IWineD3DPixelShader *)This->state.pixel_shader);
519 for(counter = 0; counter < LIGHTMAP_SIZE; counter++) {
520 struct list *e1, *e2;
521 LIST_FOR_EACH_SAFE(e1, e2, &This->state.light_map[counter])
523 struct wined3d_light_info *light = LIST_ENTRY(e1, struct wined3d_light_info, entry);
524 list_remove(&light->entry);
525 HeapFree(GetProcessHeap(), 0, light);
529 HeapFree(GetProcessHeap(), 0, This->state.vs_consts_f);
530 HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
531 HeapFree(GetProcessHeap(), 0, This->state.ps_consts_f);
532 HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
533 HeapFree(GetProcessHeap(), 0, This->contained_vs_consts_f);
534 HeapFree(GetProcessHeap(), 0, This->contained_ps_consts_f);
535 HeapFree(GetProcessHeap(), 0, This);
540 static void wined3d_state_record_lights(struct wined3d_state *dst_state, const struct wined3d_state *src_state)
544 /* Lights... For a recorded state block, we just had a chain of actions
545 * to perform, so we need to walk that chain and update any actions which
547 for (i = 0; i < LIGHTMAP_SIZE; ++i)
550 LIST_FOR_EACH(e, &dst_state->light_map[i])
552 BOOL updated = FALSE;
553 struct wined3d_light_info *src = LIST_ENTRY(e, struct wined3d_light_info, entry), *realLight;
555 /* Look up the light in the destination */
556 LIST_FOR_EACH(f, &src_state->light_map[i])
558 realLight = LIST_ENTRY(f, struct wined3d_light_info, entry);
559 if (realLight->OriginalIndex == src->OriginalIndex)
561 src->OriginalParms = realLight->OriginalParms;
563 if (realLight->glIndex == -1 && src->glIndex != -1)
566 dst_state->lights[src->glIndex] = NULL;
568 else if (realLight->glIndex != -1 && src->glIndex == -1)
571 dst_state->lights[realLight->glIndex] = src;
573 src->glIndex = realLight->glIndex;
581 /* This can happen if the light was originally created as a
582 * default light for SetLightEnable() while recording. */
583 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
584 src->OriginalIndex, dst_state, src_state);
586 src->OriginalParms = WINED3D_default_light;
587 if (src->glIndex != -1)
589 dst_state->lights[src->glIndex] = NULL;
597 static HRESULT WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface)
599 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
600 const struct wined3d_state *src_state = &This->device->stateBlock->state;
604 TRACE("iface %p.\n", iface);
606 TRACE("Capturing state %p.\n", src_state);
608 if (This->changed.vertexShader && This->state.vertex_shader != src_state->vertex_shader)
610 TRACE("Updating vertex shader from %p to %p\n",
611 This->state.vertex_shader, src_state->vertex_shader);
613 if (src_state->vertex_shader)
614 IWineD3DVertexShader_AddRef((IWineD3DVertexShader *)src_state->vertex_shader);
615 if (This->state.vertex_shader)
616 IWineD3DVertexShader_Release((IWineD3DVertexShader *)This->state.vertex_shader);
617 This->state.vertex_shader = src_state->vertex_shader;
620 /* Vertex Shader Float Constants */
621 for (i = 0; i < This->num_contained_vs_consts_f; ++i)
623 unsigned int idx = This->contained_vs_consts_f[i];
625 TRACE("Setting vs_consts_f[%u] to {%.8e, %.8e, %.8e, %.8e}.\n", idx,
626 src_state->vs_consts_f[idx * 4 + 0],
627 src_state->vs_consts_f[idx * 4 + 1],
628 src_state->vs_consts_f[idx * 4 + 2],
629 src_state->vs_consts_f[idx * 4 + 3]);
631 This->state.vs_consts_f[idx * 4 + 0] = src_state->vs_consts_f[idx * 4 + 0];
632 This->state.vs_consts_f[idx * 4 + 1] = src_state->vs_consts_f[idx * 4 + 1];
633 This->state.vs_consts_f[idx * 4 + 2] = src_state->vs_consts_f[idx * 4 + 2];
634 This->state.vs_consts_f[idx * 4 + 3] = src_state->vs_consts_f[idx * 4 + 3];
637 /* Vertex Shader Integer Constants */
638 for (i = 0; i < This->num_contained_vs_consts_i; ++i)
640 unsigned int idx = This->contained_vs_consts_i[i];
642 TRACE("Setting vs_consts[%u] to {%d, %d, %d, %d}.\n", idx,
643 src_state->vs_consts_i[idx * 4 + 0],
644 src_state->vs_consts_i[idx * 4 + 1],
645 src_state->vs_consts_i[idx * 4 + 2],
646 src_state->vs_consts_i[idx * 4 + 3]);
648 This->state.vs_consts_i[idx * 4 + 0] = src_state->vs_consts_i[idx * 4 + 0];
649 This->state.vs_consts_i[idx * 4 + 1] = src_state->vs_consts_i[idx * 4 + 1];
650 This->state.vs_consts_i[idx * 4 + 2] = src_state->vs_consts_i[idx * 4 + 2];
651 This->state.vs_consts_i[idx * 4 + 3] = src_state->vs_consts_i[idx * 4 + 3];
654 /* Vertex Shader Boolean Constants */
655 for (i = 0; i < This->num_contained_vs_consts_b; ++i)
657 unsigned int idx = This->contained_vs_consts_b[i];
659 TRACE("Setting vs_consts_b[%u] to %s.\n",
660 idx, src_state->vs_consts_b[idx] ? "TRUE" : "FALSE");
662 This->state.vs_consts_b[idx] = src_state->vs_consts_b[idx];
665 /* Pixel Shader Float Constants */
666 for (i = 0; i < This->num_contained_ps_consts_f; ++i)
668 unsigned int idx = This->contained_ps_consts_f[i];
670 TRACE("Setting ps_consts_f[%u] to {%.8e, %.8e, %.8e, %.8e}.\n", idx,
671 src_state->ps_consts_f[idx * 4 + 0],
672 src_state->ps_consts_f[idx * 4 + 1],
673 src_state->ps_consts_f[idx * 4 + 2],
674 src_state->ps_consts_f[idx * 4 + 3]);
676 This->state.ps_consts_f[idx * 4 + 0] = src_state->ps_consts_f[idx * 4 + 0];
677 This->state.ps_consts_f[idx * 4 + 1] = src_state->ps_consts_f[idx * 4 + 1];
678 This->state.ps_consts_f[idx * 4 + 2] = src_state->ps_consts_f[idx * 4 + 2];
679 This->state.ps_consts_f[idx * 4 + 3] = src_state->ps_consts_f[idx * 4 + 3];
682 /* Pixel Shader Integer Constants */
683 for (i = 0; i < This->num_contained_ps_consts_i; ++i)
685 unsigned int idx = This->contained_ps_consts_i[i];
686 TRACE("Setting ps_consts_i[%u] to {%d, %d, %d, %d}.\n", idx,
687 src_state->ps_consts_i[idx * 4 + 0],
688 src_state->ps_consts_i[idx * 4 + 1],
689 src_state->ps_consts_i[idx * 4 + 2],
690 src_state->ps_consts_i[idx * 4 + 3]);
692 This->state.ps_consts_i[idx * 4 + 0] = src_state->ps_consts_i[idx * 4 + 0];
693 This->state.ps_consts_i[idx * 4 + 1] = src_state->ps_consts_i[idx * 4 + 1];
694 This->state.ps_consts_i[idx * 4 + 2] = src_state->ps_consts_i[idx * 4 + 2];
695 This->state.ps_consts_i[idx * 4 + 3] = src_state->ps_consts_i[idx * 4 + 3];
698 /* Pixel Shader Boolean Constants */
699 for (i = 0; i < This->num_contained_ps_consts_b; ++i)
701 unsigned int idx = This->contained_ps_consts_b[i];
702 TRACE("Setting ps_consts_b[%u] to %s.\n",
703 idx, src_state->ps_consts_b[idx] ? "TRUE" : "FALSE");
705 This->state.ps_consts_b[idx] = src_state->ps_consts_b[idx];
708 /* Others + Render & Texture */
709 for (i = 0; i < This->num_contained_transform_states; ++i)
711 WINED3DTRANSFORMSTATETYPE transform = This->contained_transform_states[i];
713 TRACE("Updating transform %#x.\n", transform);
715 This->state.transforms[transform] = src_state->transforms[transform];
718 if (This->changed.primitive_type)
719 This->state.gl_primitive_type = src_state->gl_primitive_type;
721 if (This->changed.indices
722 && ((This->state.index_buffer != src_state->index_buffer)
723 || (This->state.base_vertex_index != src_state->base_vertex_index)
724 || (This->state.index_format != src_state->index_format)))
726 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
727 src_state->index_buffer, src_state->base_vertex_index);
729 if (src_state->index_buffer)
730 IWineD3DBuffer_AddRef((IWineD3DBuffer *)src_state->index_buffer);
731 if (This->state.index_buffer)
732 IWineD3DBuffer_Release((IWineD3DBuffer *)This->state.index_buffer);
733 This->state.index_buffer = src_state->index_buffer;
734 This->state.base_vertex_index = src_state->base_vertex_index;
735 This->state.index_format = src_state->index_format;
738 if (This->changed.vertexDecl && This->state.vertex_declaration != src_state->vertex_declaration)
740 TRACE("Updating vertex declaration from %p to %p.\n",
741 This->state.vertex_declaration, src_state->vertex_declaration);
743 if (src_state->vertex_declaration)
744 IWineD3DVertexDeclaration_AddRef((IWineD3DVertexDeclaration *)src_state->vertex_declaration);
745 if (This->state.vertex_declaration)
746 IWineD3DVertexDeclaration_Release((IWineD3DVertexDeclaration *)This->state.vertex_declaration);
747 This->state.vertex_declaration = src_state->vertex_declaration;
750 if (This->changed.material && memcmp(&src_state->material, &This->state.material, sizeof(This->state.material)))
752 TRACE("Updating material.\n");
754 This->state.material = src_state->material;
757 if (This->changed.viewport && memcmp(&src_state->viewport, &This->state.viewport, sizeof(This->state.viewport)))
759 TRACE("Updating viewport.\n");
761 This->state.viewport = src_state->viewport;
764 if (This->changed.scissorRect && memcmp(&src_state->scissor_rect,
765 &This->state.scissor_rect, sizeof(This->state.scissor_rect)))
767 TRACE("Updating scissor rect.\n");
769 This->state.scissor_rect = src_state->scissor_rect;
772 map = This->changed.streamSource;
773 for (i = 0; map; map >>= 1, ++i)
775 if (!(map & 1)) continue;
777 if (This->state.streams[i].stride != src_state->streams[i].stride
778 || This->state.streams[i].buffer != src_state->streams[i].buffer)
780 TRACE("Updating stream source %u to %p, stride to %u.\n",
781 i, src_state->streams[i].buffer,
782 src_state->streams[i].stride);
784 This->state.streams[i].stride = src_state->streams[i].stride;
785 if (src_state->streams[i].buffer)
786 IWineD3DBuffer_AddRef((IWineD3DBuffer *)src_state->streams[i].buffer);
787 if (This->state.streams[i].buffer)
788 IWineD3DBuffer_Release((IWineD3DBuffer *)This->state.streams[i].buffer);
789 This->state.streams[i].buffer = src_state->streams[i].buffer;
793 map = This->changed.streamFreq;
794 for (i = 0; map; map >>= 1, ++i)
796 if (!(map & 1)) continue;
798 if (This->state.streams[i].frequency != src_state->streams[i].frequency
799 || This->state.streams[i].flags != src_state->streams[i].flags)
801 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
802 i, src_state->streams[i].frequency, src_state->streams[i].flags);
804 This->state.streams[i].frequency = src_state->streams[i].frequency;
805 This->state.streams[i].flags = src_state->streams[i].flags;
809 map = This->changed.clipplane;
810 for (i = 0; map; map >>= 1, ++i)
812 if (!(map & 1)) continue;
814 if (memcmp(src_state->clip_planes[i], This->state.clip_planes[i], sizeof(*This->state.clip_planes)))
816 TRACE("Updating clipplane %u.\n", i);
817 memcpy(This->state.clip_planes[i], src_state->clip_planes[i], sizeof(*This->state.clip_planes));
822 for (i = 0; i < This->num_contained_render_states; ++i)
824 WINED3DRENDERSTATETYPE rs = This->contained_render_states[i];
826 TRACE("Updating render state %#x to %u.\n", rs, src_state->render_states[rs]);
828 This->state.render_states[rs] = src_state->render_states[rs];
832 for (i = 0; i < This->num_contained_tss_states; ++i)
834 DWORD stage = This->contained_tss_states[i].stage;
835 DWORD state = This->contained_tss_states[i].state;
837 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage, state,
838 src_state->texture_states[stage][state], This->state.texture_states[stage][state]);
840 This->state.texture_states[stage][state] = src_state->texture_states[stage][state];
844 map = This->changed.textures;
845 for (i = 0; map; map >>= 1, ++i)
847 if (!(map & 1)) continue;
849 TRACE("Updating texture %u to %p (was %p).\n",
850 i, src_state->textures[i], This->state.textures[i]);
852 if (src_state->textures[i])
853 IWineD3DBaseTexture_AddRef((IWineD3DBaseTexture *)src_state->textures[i]);
854 if (This->state.textures[i])
855 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *)This->state.textures[i]);
856 This->state.textures[i] = src_state->textures[i];
859 for (i = 0; i < This->num_contained_sampler_states; ++i)
861 DWORD stage = This->contained_sampler_states[i].stage;
862 DWORD state = This->contained_sampler_states[i].state;
864 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage, state,
865 src_state->sampler_states[stage][state], This->state.sampler_states[stage][state]);
867 This->state.sampler_states[stage][state] = src_state->sampler_states[stage][state];
870 if (This->changed.pixelShader && This->state.pixel_shader != src_state->pixel_shader)
872 if (src_state->pixel_shader)
873 IWineD3DPixelShader_AddRef((IWineD3DPixelShader *)src_state->pixel_shader);
874 if (This->state.pixel_shader)
875 IWineD3DPixelShader_Release((IWineD3DPixelShader *)This->state.pixel_shader);
876 This->state.pixel_shader = src_state->pixel_shader;
879 wined3d_state_record_lights(&This->state, src_state);
881 TRACE("Captue done.\n");
886 static void apply_lights(IWineD3DDevice *device, const struct wined3d_state *state)
890 for (i = 0; i < LIGHTMAP_SIZE; ++i)
894 LIST_FOR_EACH(e, &state->light_map[i])
896 const struct wined3d_light_info *light = LIST_ENTRY(e, struct wined3d_light_info, entry);
898 IWineD3DDevice_SetLight(device, light->OriginalIndex, &light->OriginalParms);
899 IWineD3DDevice_SetLightEnable(device, light->OriginalIndex, light->glIndex != -1);
904 static HRESULT WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface)
906 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
907 IWineD3DDevice *device = (IWineD3DDevice *)This->device;
911 TRACE("(%p) : Applying state block %p ------------------v\n", This, device);
913 TRACE("Blocktype: %d\n", This->blockType);
915 if (This->changed.vertexShader)
916 IWineD3DDevice_SetVertexShader(device, (IWineD3DVertexShader *)This->state.vertex_shader);
918 /* Vertex Shader Constants */
919 for (i = 0; i < This->num_contained_vs_consts_f; ++i)
921 IWineD3DDevice_SetVertexShaderConstantF(device, This->contained_vs_consts_f[i],
922 This->state.vs_consts_f + This->contained_vs_consts_f[i] * 4, 1);
924 for (i = 0; i < This->num_contained_vs_consts_i; ++i)
926 IWineD3DDevice_SetVertexShaderConstantI(device, This->contained_vs_consts_i[i],
927 This->state.vs_consts_i + This->contained_vs_consts_i[i] * 4, 1);
929 for (i = 0; i < This->num_contained_vs_consts_b; ++i)
931 IWineD3DDevice_SetVertexShaderConstantB(device, This->contained_vs_consts_b[i],
932 This->state.vs_consts_b + This->contained_vs_consts_b[i], 1);
935 apply_lights(device, &This->state);
937 if (This->changed.pixelShader)
938 IWineD3DDevice_SetPixelShader(device, (IWineD3DPixelShader *)This->state.pixel_shader);
940 /* Pixel Shader Constants */
941 for (i = 0; i < This->num_contained_ps_consts_f; ++i)
943 IWineD3DDevice_SetPixelShaderConstantF(device, This->contained_ps_consts_f[i],
944 This->state.ps_consts_f + This->contained_ps_consts_f[i] * 4, 1);
946 for (i = 0; i < This->num_contained_ps_consts_i; ++i)
948 IWineD3DDevice_SetPixelShaderConstantI(device, This->contained_ps_consts_i[i],
949 This->state.ps_consts_i + This->contained_ps_consts_i[i] * 4, 1);
951 for (i = 0; i < This->num_contained_ps_consts_b; ++i)
953 IWineD3DDevice_SetPixelShaderConstantB(device, This->contained_ps_consts_b[i],
954 This->state.ps_consts_b + This->contained_ps_consts_b[i], 1);
958 for (i = 0; i < This->num_contained_render_states; ++i)
960 IWineD3DDevice_SetRenderState(device, This->contained_render_states[i],
961 This->state.render_states[This->contained_render_states[i]]);
965 for (i = 0; i < This->num_contained_tss_states; ++i)
967 DWORD stage = This->contained_tss_states[i].stage;
968 DWORD state = This->contained_tss_states[i].state;
970 IWineD3DDevice_SetTextureStageState(device, stage, state, This->state.texture_states[stage][state]);
974 for (i = 0; i < This->num_contained_sampler_states; ++i)
976 DWORD stage = This->contained_sampler_states[i].stage;
977 DWORD state = This->contained_sampler_states[i].state;
978 DWORD value = This->state.sampler_states[stage][state];
980 if (stage >= MAX_FRAGMENT_SAMPLERS) stage += WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS;
981 IWineD3DDevice_SetSamplerState(device, stage, state, value);
984 for (i = 0; i < This->num_contained_transform_states; ++i)
986 IWineD3DDevice_SetTransform(device, This->contained_transform_states[i],
987 &This->state.transforms[This->contained_transform_states[i]]);
990 if (This->changed.primitive_type)
992 This->device->updateStateBlock->changed.primitive_type = TRUE;
993 This->device->updateStateBlock->state.gl_primitive_type = This->state.gl_primitive_type;
996 if (This->changed.indices)
998 IWineD3DDevice_SetIndexBuffer(device, (IWineD3DBuffer *)This->state.index_buffer, This->state.index_format);
999 IWineD3DDevice_SetBaseVertexIndex(device, This->state.base_vertex_index);
1002 if (This->changed.vertexDecl && This->state.vertex_declaration)
1004 IWineD3DDevice_SetVertexDeclaration(device, (IWineD3DVertexDeclaration *)This->state.vertex_declaration);
1007 if (This->changed.material)
1009 IWineD3DDevice_SetMaterial(device, &This->state.material);
1012 if (This->changed.viewport)
1014 IWineD3DDevice_SetViewport(device, &This->state.viewport);
1017 if (This->changed.scissorRect)
1019 IWineD3DDevice_SetScissorRect(device, &This->state.scissor_rect);
1022 map = This->changed.streamSource;
1023 for (i = 0; map; map >>= 1, ++i)
1026 IWineD3DDevice_SetStreamSource(device, i,
1027 (IWineD3DBuffer *)This->state.streams[i].buffer,
1028 0, This->state.streams[i].stride);
1031 map = This->changed.streamFreq;
1032 for (i = 0; map; map >>= 1, ++i)
1035 IWineD3DDevice_SetStreamSourceFreq(device, i,
1036 This->state.streams[i].frequency | This->state.streams[i].flags);
1039 map = This->changed.textures;
1040 for (i = 0; map; map >>= 1, ++i)
1044 if (!(map & 1)) continue;
1046 stage = i < MAX_FRAGMENT_SAMPLERS ? i : WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS;
1047 IWineD3DDevice_SetTexture(device, stage, (IWineD3DBaseTexture *)This->state.textures[i]);
1050 map = This->changed.clipplane;
1051 for (i = 0; map; map >>= 1, ++i)
1055 if (!(map & 1)) continue;
1057 clip[0] = This->state.clip_planes[i][0];
1058 clip[1] = This->state.clip_planes[i][1];
1059 clip[2] = This->state.clip_planes[i][2];
1060 clip[3] = This->state.clip_planes[i][3];
1061 IWineD3DDevice_SetClipPlane(device, i, clip);
1064 This->device->stateBlock->state.lowest_disabled_stage = MAX_TEXTURES - 1;
1065 for (i = 0; i < MAX_TEXTURES - 1; ++i)
1067 if (This->device->stateBlock->state.texture_states[i][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE)
1069 This->device->stateBlock->state.lowest_disabled_stage = i;
1073 TRACE("(%p) : Applied state block %p ------------------^\n", This, device);
1078 void stateblock_init_default_state(IWineD3DStateBlockImpl *stateblock)
1080 IWineD3DDeviceImpl *device = stateblock->device;
1081 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1082 struct wined3d_state *state = &stateblock->state;
1084 WINED3DLINEPATTERN lp;
1092 IWineD3DSwapChain *swapchain;
1093 IWineD3DSurface *backbuffer;
1096 TRACE("stateblock %p.\n", stateblock);
1098 stateblock->blockType = WINED3DSBT_INIT;
1100 /* Set some of the defaults for lights, transforms etc */
1101 memcpy(&state->transforms[WINED3DTS_PROJECTION], identity, sizeof(identity));
1102 memcpy(&state->transforms[WINED3DTS_VIEW], identity, sizeof(identity));
1103 for (i = 0; i < 256; ++i)
1105 memcpy(&state->transforms[WINED3DTS_WORLDMATRIX(i)], identity, sizeof(identity));
1108 TRACE("Render states\n");
1109 /* Render states: */
1110 if (device->auto_depth_stencil)
1111 state->render_states[WINED3DRS_ZENABLE] = WINED3DZB_TRUE;
1113 state->render_states[WINED3DRS_ZENABLE] = WINED3DZB_FALSE;
1114 state->render_states[WINED3DRS_FILLMODE] = WINED3DFILL_SOLID;
1115 state->render_states[WINED3DRS_SHADEMODE] = WINED3DSHADE_GOURAUD;
1116 lp.lp.wRepeatFactor = 0;
1117 lp.lp.wLinePattern = 0;
1118 state->render_states[WINED3DRS_LINEPATTERN] = lp.d;
1119 state->render_states[WINED3DRS_ZWRITEENABLE] = TRUE;
1120 state->render_states[WINED3DRS_ALPHATESTENABLE] = FALSE;
1121 state->render_states[WINED3DRS_LASTPIXEL] = TRUE;
1122 state->render_states[WINED3DRS_SRCBLEND] = WINED3DBLEND_ONE;
1123 state->render_states[WINED3DRS_DESTBLEND] = WINED3DBLEND_ZERO;
1124 state->render_states[WINED3DRS_CULLMODE] = WINED3DCULL_CCW;
1125 state->render_states[WINED3DRS_ZFUNC] = WINED3DCMP_LESSEQUAL;
1126 state->render_states[WINED3DRS_ALPHAFUNC] = WINED3DCMP_ALWAYS;
1127 state->render_states[WINED3DRS_ALPHAREF] = 0;
1128 state->render_states[WINED3DRS_DITHERENABLE] = FALSE;
1129 state->render_states[WINED3DRS_ALPHABLENDENABLE] = FALSE;
1130 state->render_states[WINED3DRS_FOGENABLE] = FALSE;
1131 state->render_states[WINED3DRS_SPECULARENABLE] = FALSE;
1132 state->render_states[WINED3DRS_ZVISIBLE] = 0;
1133 state->render_states[WINED3DRS_FOGCOLOR] = 0;
1134 state->render_states[WINED3DRS_FOGTABLEMODE] = WINED3DFOG_NONE;
1136 state->render_states[WINED3DRS_FOGSTART] = tmpfloat.d;
1138 state->render_states[WINED3DRS_FOGEND] = tmpfloat.d;
1140 state->render_states[WINED3DRS_FOGDENSITY] = tmpfloat.d;
1141 state->render_states[WINED3DRS_EDGEANTIALIAS] = FALSE;
1142 state->render_states[WINED3DRS_ZBIAS] = 0;
1143 state->render_states[WINED3DRS_RANGEFOGENABLE] = FALSE;
1144 state->render_states[WINED3DRS_STENCILENABLE] = FALSE;
1145 state->render_states[WINED3DRS_STENCILFAIL] = WINED3DSTENCILOP_KEEP;
1146 state->render_states[WINED3DRS_STENCILZFAIL] = WINED3DSTENCILOP_KEEP;
1147 state->render_states[WINED3DRS_STENCILPASS] = WINED3DSTENCILOP_KEEP;
1148 state->render_states[WINED3DRS_STENCILREF] = 0;
1149 state->render_states[WINED3DRS_STENCILMASK] = 0xffffffff;
1150 state->render_states[WINED3DRS_STENCILFUNC] = WINED3DCMP_ALWAYS;
1151 state->render_states[WINED3DRS_STENCILWRITEMASK] = 0xffffffff;
1152 state->render_states[WINED3DRS_TEXTUREFACTOR] = 0xffffffff;
1153 state->render_states[WINED3DRS_WRAP0] = 0;
1154 state->render_states[WINED3DRS_WRAP1] = 0;
1155 state->render_states[WINED3DRS_WRAP2] = 0;
1156 state->render_states[WINED3DRS_WRAP3] = 0;
1157 state->render_states[WINED3DRS_WRAP4] = 0;
1158 state->render_states[WINED3DRS_WRAP5] = 0;
1159 state->render_states[WINED3DRS_WRAP6] = 0;
1160 state->render_states[WINED3DRS_WRAP7] = 0;
1161 state->render_states[WINED3DRS_CLIPPING] = TRUE;
1162 state->render_states[WINED3DRS_LIGHTING] = TRUE;
1163 state->render_states[WINED3DRS_AMBIENT] = 0;
1164 state->render_states[WINED3DRS_FOGVERTEXMODE] = WINED3DFOG_NONE;
1165 state->render_states[WINED3DRS_COLORVERTEX] = TRUE;
1166 state->render_states[WINED3DRS_LOCALVIEWER] = TRUE;
1167 state->render_states[WINED3DRS_NORMALIZENORMALS] = FALSE;
1168 state->render_states[WINED3DRS_DIFFUSEMATERIALSOURCE] = WINED3DMCS_COLOR1;
1169 state->render_states[WINED3DRS_SPECULARMATERIALSOURCE] = WINED3DMCS_COLOR2;
1170 state->render_states[WINED3DRS_AMBIENTMATERIALSOURCE] = WINED3DMCS_MATERIAL;
1171 state->render_states[WINED3DRS_EMISSIVEMATERIALSOURCE] = WINED3DMCS_MATERIAL;
1172 state->render_states[WINED3DRS_VERTEXBLEND] = WINED3DVBF_DISABLE;
1173 state->render_states[WINED3DRS_CLIPPLANEENABLE] = 0;
1174 state->render_states[WINED3DRS_SOFTWAREVERTEXPROCESSING] = FALSE;
1176 state->render_states[WINED3DRS_POINTSIZE] = tmpfloat.d;
1178 state->render_states[WINED3DRS_POINTSIZE_MIN] = tmpfloat.d;
1179 state->render_states[WINED3DRS_POINTSPRITEENABLE] = FALSE;
1180 state->render_states[WINED3DRS_POINTSCALEENABLE] = FALSE;
1182 state->render_states[WINED3DRS_POINTSCALE_A] = tmpfloat.d;
1184 state->render_states[WINED3DRS_POINTSCALE_B] = tmpfloat.d;
1186 state->render_states[WINED3DRS_POINTSCALE_C] = tmpfloat.d;
1187 state->render_states[WINED3DRS_MULTISAMPLEANTIALIAS] = TRUE;
1188 state->render_states[WINED3DRS_MULTISAMPLEMASK] = 0xffffffff;
1189 state->render_states[WINED3DRS_PATCHEDGESTYLE] = WINED3DPATCHEDGE_DISCRETE;
1191 state->render_states[WINED3DRS_PATCHSEGMENTS] = tmpfloat.d;
1192 state->render_states[WINED3DRS_DEBUGMONITORTOKEN] = 0xbaadcafe;
1193 tmpfloat.f = gl_info->limits.pointsize_max;
1194 state->render_states[WINED3DRS_POINTSIZE_MAX] = tmpfloat.d;
1195 state->render_states[WINED3DRS_INDEXEDVERTEXBLENDENABLE] = FALSE;
1196 state->render_states[WINED3DRS_COLORWRITEENABLE] = 0x0000000f;
1198 state->render_states[WINED3DRS_TWEENFACTOR] = tmpfloat.d;
1199 state->render_states[WINED3DRS_BLENDOP] = WINED3DBLENDOP_ADD;
1200 state->render_states[WINED3DRS_POSITIONDEGREE] = WINED3DDEGREE_CUBIC;
1201 state->render_states[WINED3DRS_NORMALDEGREE] = WINED3DDEGREE_LINEAR;
1202 /* states new in d3d9 */
1203 state->render_states[WINED3DRS_SCISSORTESTENABLE] = FALSE;
1204 state->render_states[WINED3DRS_SLOPESCALEDEPTHBIAS] = 0;
1206 state->render_states[WINED3DRS_MINTESSELLATIONLEVEL] = tmpfloat.d;
1207 state->render_states[WINED3DRS_MAXTESSELLATIONLEVEL] = tmpfloat.d;
1208 state->render_states[WINED3DRS_ANTIALIASEDLINEENABLE] = FALSE;
1210 state->render_states[WINED3DRS_ADAPTIVETESS_X] = tmpfloat.d;
1211 state->render_states[WINED3DRS_ADAPTIVETESS_Y] = tmpfloat.d;
1213 state->render_states[WINED3DRS_ADAPTIVETESS_Z] = tmpfloat.d;
1215 state->render_states[WINED3DRS_ADAPTIVETESS_W] = tmpfloat.d;
1216 state->render_states[WINED3DRS_ENABLEADAPTIVETESSELLATION] = FALSE;
1217 state->render_states[WINED3DRS_TWOSIDEDSTENCILMODE] = FALSE;
1218 state->render_states[WINED3DRS_CCW_STENCILFAIL] = WINED3DSTENCILOP_KEEP;
1219 state->render_states[WINED3DRS_CCW_STENCILZFAIL] = WINED3DSTENCILOP_KEEP;
1220 state->render_states[WINED3DRS_CCW_STENCILPASS] = WINED3DSTENCILOP_KEEP;
1221 state->render_states[WINED3DRS_CCW_STENCILFUNC] = WINED3DCMP_ALWAYS;
1222 state->render_states[WINED3DRS_COLORWRITEENABLE1] = 0x0000000f;
1223 state->render_states[WINED3DRS_COLORWRITEENABLE2] = 0x0000000f;
1224 state->render_states[WINED3DRS_COLORWRITEENABLE3] = 0x0000000f;
1225 state->render_states[WINED3DRS_BLENDFACTOR] = 0xFFFFFFFF;
1226 state->render_states[WINED3DRS_SRGBWRITEENABLE] = 0;
1227 state->render_states[WINED3DRS_DEPTHBIAS] = 0;
1228 state->render_states[WINED3DRS_WRAP8] = 0;
1229 state->render_states[WINED3DRS_WRAP9] = 0;
1230 state->render_states[WINED3DRS_WRAP10] = 0;
1231 state->render_states[WINED3DRS_WRAP11] = 0;
1232 state->render_states[WINED3DRS_WRAP12] = 0;
1233 state->render_states[WINED3DRS_WRAP13] = 0;
1234 state->render_states[WINED3DRS_WRAP14] = 0;
1235 state->render_states[WINED3DRS_WRAP15] = 0;
1236 state->render_states[WINED3DRS_SEPARATEALPHABLENDENABLE] = FALSE;
1237 state->render_states[WINED3DRS_SRCBLENDALPHA] = WINED3DBLEND_ONE;
1238 state->render_states[WINED3DRS_DESTBLENDALPHA] = WINED3DBLEND_ZERO;
1239 state->render_states[WINED3DRS_BLENDOPALPHA] = WINED3DBLENDOP_ADD;
1241 /* clipping status */
1242 state->clip_status.ClipUnion = 0;
1243 state->clip_status.ClipIntersection = 0xFFFFFFFF;
1245 /* Texture Stage States - Put directly into state block, we will call function below */
1246 for (i = 0; i < MAX_TEXTURES; ++i)
1248 TRACE("Setting up default texture states for texture Stage %u.\n", i);
1249 memcpy(&state->transforms[WINED3DTS_TEXTURE0 + i], identity, sizeof(identity));
1250 state->texture_states[i][WINED3DTSS_COLOROP] = i ? WINED3DTOP_DISABLE : WINED3DTOP_MODULATE;
1251 state->texture_states[i][WINED3DTSS_COLORARG1] = WINED3DTA_TEXTURE;
1252 state->texture_states[i][WINED3DTSS_COLORARG2] = WINED3DTA_CURRENT;
1253 state->texture_states[i][WINED3DTSS_ALPHAOP] = i ? WINED3DTOP_DISABLE : WINED3DTOP_SELECTARG1;
1254 state->texture_states[i][WINED3DTSS_ALPHAARG1] = WINED3DTA_TEXTURE;
1255 state->texture_states[i][WINED3DTSS_ALPHAARG2] = WINED3DTA_CURRENT;
1256 state->texture_states[i][WINED3DTSS_BUMPENVMAT00] = 0;
1257 state->texture_states[i][WINED3DTSS_BUMPENVMAT01] = 0;
1258 state->texture_states[i][WINED3DTSS_BUMPENVMAT10] = 0;
1259 state->texture_states[i][WINED3DTSS_BUMPENVMAT11] = 0;
1260 state->texture_states[i][WINED3DTSS_TEXCOORDINDEX] = i;
1261 state->texture_states[i][WINED3DTSS_BUMPENVLSCALE] = 0;
1262 state->texture_states[i][WINED3DTSS_BUMPENVLOFFSET] = 0;
1263 state->texture_states[i][WINED3DTSS_TEXTURETRANSFORMFLAGS] = WINED3DTTFF_DISABLE;
1264 state->texture_states[i][WINED3DTSS_COLORARG0] = WINED3DTA_CURRENT;
1265 state->texture_states[i][WINED3DTSS_ALPHAARG0] = WINED3DTA_CURRENT;
1266 state->texture_states[i][WINED3DTSS_RESULTARG] = WINED3DTA_CURRENT;
1268 state->lowest_disabled_stage = 1;
1271 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; ++i)
1273 TRACE("Setting up default samplers states for sampler %u.\n", i);
1274 state->sampler_states[i][WINED3DSAMP_ADDRESSU] = WINED3DTADDRESS_WRAP;
1275 state->sampler_states[i][WINED3DSAMP_ADDRESSV] = WINED3DTADDRESS_WRAP;
1276 state->sampler_states[i][WINED3DSAMP_ADDRESSW] = WINED3DTADDRESS_WRAP;
1277 state->sampler_states[i][WINED3DSAMP_BORDERCOLOR] = 0;
1278 state->sampler_states[i][WINED3DSAMP_MAGFILTER] = WINED3DTEXF_POINT;
1279 state->sampler_states[i][WINED3DSAMP_MINFILTER] = WINED3DTEXF_POINT;
1280 state->sampler_states[i][WINED3DSAMP_MIPFILTER] = WINED3DTEXF_NONE;
1281 state->sampler_states[i][WINED3DSAMP_MIPMAPLODBIAS] = 0;
1282 state->sampler_states[i][WINED3DSAMP_MAXMIPLEVEL] = 0;
1283 state->sampler_states[i][WINED3DSAMP_MAXANISOTROPY] = 1;
1284 state->sampler_states[i][WINED3DSAMP_SRGBTEXTURE] = 0;
1285 /* TODO: Indicates which element of a multielement texture to use. */
1286 state->sampler_states[i][WINED3DSAMP_ELEMENTINDEX] = 0;
1287 /* TODO: Vertex offset in the presampled displacement map. */
1288 state->sampler_states[i][WINED3DSAMP_DMAPOFFSET] = 0;
1291 for (i = 0; i < gl_info->limits.textures; ++i)
1293 state->textures[i] = NULL;
1296 /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1297 hr = IWineD3DDevice_GetSwapChain((IWineD3DDevice *)device, 0, &swapchain);
1298 if (SUCCEEDED(hr) && swapchain)
1300 hr = IWineD3DSwapChain_GetBackBuffer(swapchain, 0, WINED3DBACKBUFFER_TYPE_MONO, &backbuffer);
1301 if (SUCCEEDED(hr) && backbuffer)
1303 WINED3DSURFACE_DESC desc;
1305 IWineD3DSurface_GetDesc(backbuffer, &desc);
1306 IWineD3DSurface_Release(backbuffer);
1308 /* Set the default scissor rect values */
1309 state->scissor_rect.left = 0;
1310 state->scissor_rect.right = desc.width;
1311 state->scissor_rect.top = 0;
1312 state->scissor_rect.bottom = desc.height;
1315 /* Set the default viewport */
1316 state->viewport.X = 0;
1317 state->viewport.Y = 0;
1318 state->viewport.Width = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferWidth;
1319 state->viewport.Height = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferHeight;
1320 state->viewport.MinZ = 0.0f;
1321 state->viewport.MaxZ = 1.0f;
1323 IWineD3DSwapChain_Release(swapchain);
1329 /**********************************************************
1330 * IWineD3DStateBlock VTbl follows
1331 **********************************************************/
1333 static const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
1336 IWineD3DStateBlockImpl_QueryInterface,
1337 IWineD3DStateBlockImpl_AddRef,
1338 IWineD3DStateBlockImpl_Release,
1339 /* IWineD3DStateBlock */
1340 IWineD3DStateBlockImpl_Capture,
1341 IWineD3DStateBlockImpl_Apply,
1344 HRESULT stateblock_init(IWineD3DStateBlockImpl *stateblock, IWineD3DDeviceImpl *device, WINED3DSTATEBLOCKTYPE type)
1349 stateblock->lpVtbl = &IWineD3DStateBlock_Vtbl;
1350 stateblock->ref = 1;
1351 stateblock->device = device;
1352 stateblock->blockType = type;
1354 for (i = 0; i < LIGHTMAP_SIZE; i++)
1356 list_init(&stateblock->state.light_map[i]);
1359 hr = stateblock_allocate_shader_constants(stateblock);
1360 if (FAILED(hr)) return hr;
1362 /* The WINED3DSBT_INIT stateblock type is used during initialization to
1363 * produce a placeholder stateblock so other functions called can update a
1365 if (type == WINED3DSBT_INIT || type == WINED3DSBT_RECORDED) return WINED3D_OK;
1367 TRACE("Updating changed flags appropriate for type %#x.\n", type);
1371 case WINED3DSBT_ALL:
1372 stateblock_init_lights(stateblock, device->stateBlock->state.light_map);
1373 stateblock_savedstates_set_all(&stateblock->changed, device->d3d_vshader_constantF,
1374 device->d3d_pshader_constantF);
1377 case WINED3DSBT_PIXELSTATE:
1378 stateblock_savedstates_set_pixel(&stateblock->changed, device->d3d_pshader_constantF);
1381 case WINED3DSBT_VERTEXSTATE:
1382 stateblock_init_lights(stateblock, device->stateBlock->state.light_map);
1383 stateblock_savedstates_set_vertex(&stateblock->changed, device->d3d_vshader_constantF);
1387 FIXME("Unrecognized state block type %#x.\n", type);
1391 stateblock_init_contained_states(stateblock);
1392 IWineD3DStateBlockImpl_Capture((IWineD3DStateBlock *)stateblock);