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(struct wined3d_stateblock *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(struct wined3d_stateblock *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(struct wined3d_stateblock *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 ULONG CDECL wined3d_stateblock_incref(struct wined3d_stateblock *stateblock)
462 ULONG refcount = InterlockedIncrement(&stateblock->ref);
464 TRACE("%p increasing refcount to %u.\n", stateblock, refcount);
469 ULONG CDECL wined3d_stateblock_decref(struct wined3d_stateblock *stateblock)
471 ULONG refcount = InterlockedDecrement(&stateblock->ref);
473 TRACE("%p decreasing refcount to %u\n", stateblock, refcount);
479 if (stateblock->state.vertex_declaration)
480 IWineD3DVertexDeclaration_Release((IWineD3DVertexDeclaration *)stateblock->state.vertex_declaration);
482 for (counter = 0; counter < MAX_COMBINED_SAMPLERS; counter++)
484 if (stateblock->state.textures[counter])
485 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *)stateblock->state.textures[counter]);
488 for (counter = 0; counter < MAX_STREAMS; ++counter)
490 struct wined3d_buffer *buffer = stateblock->state.streams[counter].buffer;
493 if (IWineD3DBuffer_Release((IWineD3DBuffer *)buffer))
495 WARN("Buffer %p still referenced by stateblock, stream %u.\n", buffer, counter);
499 if (stateblock->state.index_buffer)
500 IWineD3DBuffer_Release((IWineD3DBuffer *)stateblock->state.index_buffer);
501 if (stateblock->state.vertex_shader)
502 IWineD3DVertexShader_Release((IWineD3DVertexShader *)stateblock->state.vertex_shader);
503 if (stateblock->state.pixel_shader)
504 IWineD3DPixelShader_Release((IWineD3DPixelShader *)stateblock->state.pixel_shader);
506 for (counter = 0; counter < LIGHTMAP_SIZE; ++counter)
508 struct list *e1, *e2;
509 LIST_FOR_EACH_SAFE(e1, e2, &stateblock->state.light_map[counter])
511 struct wined3d_light_info *light = LIST_ENTRY(e1, struct wined3d_light_info, entry);
512 list_remove(&light->entry);
513 HeapFree(GetProcessHeap(), 0, light);
517 HeapFree(GetProcessHeap(), 0, stateblock->state.vs_consts_f);
518 HeapFree(GetProcessHeap(), 0, stateblock->changed.vertexShaderConstantsF);
519 HeapFree(GetProcessHeap(), 0, stateblock->state.ps_consts_f);
520 HeapFree(GetProcessHeap(), 0, stateblock->changed.pixelShaderConstantsF);
521 HeapFree(GetProcessHeap(), 0, stateblock->contained_vs_consts_f);
522 HeapFree(GetProcessHeap(), 0, stateblock->contained_ps_consts_f);
523 HeapFree(GetProcessHeap(), 0, stateblock);
529 static void wined3d_state_record_lights(struct wined3d_state *dst_state, const struct wined3d_state *src_state)
533 /* Lights... For a recorded state block, we just had a chain of actions
534 * to perform, so we need to walk that chain and update any actions which
536 for (i = 0; i < LIGHTMAP_SIZE; ++i)
539 LIST_FOR_EACH(e, &dst_state->light_map[i])
541 BOOL updated = FALSE;
542 struct wined3d_light_info *src = LIST_ENTRY(e, struct wined3d_light_info, entry), *realLight;
544 /* Look up the light in the destination */
545 LIST_FOR_EACH(f, &src_state->light_map[i])
547 realLight = LIST_ENTRY(f, struct wined3d_light_info, entry);
548 if (realLight->OriginalIndex == src->OriginalIndex)
550 src->OriginalParms = realLight->OriginalParms;
552 if (realLight->glIndex == -1 && src->glIndex != -1)
555 dst_state->lights[src->glIndex] = NULL;
557 else if (realLight->glIndex != -1 && src->glIndex == -1)
560 dst_state->lights[realLight->glIndex] = src;
562 src->glIndex = realLight->glIndex;
570 /* This can happen if the light was originally created as a
571 * default light for SetLightEnable() while recording. */
572 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
573 src->OriginalIndex, dst_state, src_state);
575 src->OriginalParms = WINED3D_default_light;
576 if (src->glIndex != -1)
578 dst_state->lights[src->glIndex] = NULL;
586 HRESULT CDECL wined3d_stateblock_capture(struct wined3d_stateblock *stateblock)
588 const struct wined3d_state *src_state = &stateblock->device->stateBlock->state;
592 TRACE("stateblock %p.\n", stateblock);
594 TRACE("Capturing state %p.\n", src_state);
596 if (stateblock->changed.vertexShader && stateblock->state.vertex_shader != src_state->vertex_shader)
598 TRACE("Updating vertex shader from %p to %p\n",
599 stateblock->state.vertex_shader, src_state->vertex_shader);
601 if (src_state->vertex_shader)
602 IWineD3DVertexShader_AddRef((IWineD3DVertexShader *)src_state->vertex_shader);
603 if (stateblock->state.vertex_shader)
604 IWineD3DVertexShader_Release((IWineD3DVertexShader *)stateblock->state.vertex_shader);
605 stateblock->state.vertex_shader = src_state->vertex_shader;
608 /* Vertex shader float constants. */
609 for (i = 0; i < stateblock->num_contained_vs_consts_f; ++i)
611 unsigned int idx = stateblock->contained_vs_consts_f[i];
613 TRACE("Setting vs_consts_f[%u] to {%.8e, %.8e, %.8e, %.8e}.\n", idx,
614 src_state->vs_consts_f[idx * 4 + 0],
615 src_state->vs_consts_f[idx * 4 + 1],
616 src_state->vs_consts_f[idx * 4 + 2],
617 src_state->vs_consts_f[idx * 4 + 3]);
619 stateblock->state.vs_consts_f[idx * 4 + 0] = src_state->vs_consts_f[idx * 4 + 0];
620 stateblock->state.vs_consts_f[idx * 4 + 1] = src_state->vs_consts_f[idx * 4 + 1];
621 stateblock->state.vs_consts_f[idx * 4 + 2] = src_state->vs_consts_f[idx * 4 + 2];
622 stateblock->state.vs_consts_f[idx * 4 + 3] = src_state->vs_consts_f[idx * 4 + 3];
625 /* Vertex shader integer constants. */
626 for (i = 0; i < stateblock->num_contained_vs_consts_i; ++i)
628 unsigned int idx = stateblock->contained_vs_consts_i[i];
630 TRACE("Setting vs_consts[%u] to {%d, %d, %d, %d}.\n", idx,
631 src_state->vs_consts_i[idx * 4 + 0],
632 src_state->vs_consts_i[idx * 4 + 1],
633 src_state->vs_consts_i[idx * 4 + 2],
634 src_state->vs_consts_i[idx * 4 + 3]);
636 stateblock->state.vs_consts_i[idx * 4 + 0] = src_state->vs_consts_i[idx * 4 + 0];
637 stateblock->state.vs_consts_i[idx * 4 + 1] = src_state->vs_consts_i[idx * 4 + 1];
638 stateblock->state.vs_consts_i[idx * 4 + 2] = src_state->vs_consts_i[idx * 4 + 2];
639 stateblock->state.vs_consts_i[idx * 4 + 3] = src_state->vs_consts_i[idx * 4 + 3];
642 /* Vertex shader boolean constants. */
643 for (i = 0; i < stateblock->num_contained_vs_consts_b; ++i)
645 unsigned int idx = stateblock->contained_vs_consts_b[i];
647 TRACE("Setting vs_consts_b[%u] to %s.\n",
648 idx, src_state->vs_consts_b[idx] ? "TRUE" : "FALSE");
650 stateblock->state.vs_consts_b[idx] = src_state->vs_consts_b[idx];
653 /* Pixel shader float constants. */
654 for (i = 0; i < stateblock->num_contained_ps_consts_f; ++i)
656 unsigned int idx = stateblock->contained_ps_consts_f[i];
658 TRACE("Setting ps_consts_f[%u] to {%.8e, %.8e, %.8e, %.8e}.\n", idx,
659 src_state->ps_consts_f[idx * 4 + 0],
660 src_state->ps_consts_f[idx * 4 + 1],
661 src_state->ps_consts_f[idx * 4 + 2],
662 src_state->ps_consts_f[idx * 4 + 3]);
664 stateblock->state.ps_consts_f[idx * 4 + 0] = src_state->ps_consts_f[idx * 4 + 0];
665 stateblock->state.ps_consts_f[idx * 4 + 1] = src_state->ps_consts_f[idx * 4 + 1];
666 stateblock->state.ps_consts_f[idx * 4 + 2] = src_state->ps_consts_f[idx * 4 + 2];
667 stateblock->state.ps_consts_f[idx * 4 + 3] = src_state->ps_consts_f[idx * 4 + 3];
670 /* Pixel shader integer constants. */
671 for (i = 0; i < stateblock->num_contained_ps_consts_i; ++i)
673 unsigned int idx = stateblock->contained_ps_consts_i[i];
674 TRACE("Setting ps_consts_i[%u] to {%d, %d, %d, %d}.\n", idx,
675 src_state->ps_consts_i[idx * 4 + 0],
676 src_state->ps_consts_i[idx * 4 + 1],
677 src_state->ps_consts_i[idx * 4 + 2],
678 src_state->ps_consts_i[idx * 4 + 3]);
680 stateblock->state.ps_consts_i[idx * 4 + 0] = src_state->ps_consts_i[idx * 4 + 0];
681 stateblock->state.ps_consts_i[idx * 4 + 1] = src_state->ps_consts_i[idx * 4 + 1];
682 stateblock->state.ps_consts_i[idx * 4 + 2] = src_state->ps_consts_i[idx * 4 + 2];
683 stateblock->state.ps_consts_i[idx * 4 + 3] = src_state->ps_consts_i[idx * 4 + 3];
686 /* Pixel shader boolean constants. */
687 for (i = 0; i < stateblock->num_contained_ps_consts_b; ++i)
689 unsigned int idx = stateblock->contained_ps_consts_b[i];
690 TRACE("Setting ps_consts_b[%u] to %s.\n",
691 idx, src_state->ps_consts_b[idx] ? "TRUE" : "FALSE");
693 stateblock->state.ps_consts_b[idx] = src_state->ps_consts_b[idx];
696 /* Others + Render & Texture */
697 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
699 WINED3DTRANSFORMSTATETYPE transform = stateblock->contained_transform_states[i];
701 TRACE("Updating transform %#x.\n", transform);
703 stateblock->state.transforms[transform] = src_state->transforms[transform];
706 if (stateblock->changed.primitive_type)
707 stateblock->state.gl_primitive_type = src_state->gl_primitive_type;
709 if (stateblock->changed.indices
710 && ((stateblock->state.index_buffer != src_state->index_buffer)
711 || (stateblock->state.base_vertex_index != src_state->base_vertex_index)
712 || (stateblock->state.index_format != src_state->index_format)))
714 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
715 src_state->index_buffer, src_state->base_vertex_index);
717 if (src_state->index_buffer)
718 IWineD3DBuffer_AddRef((IWineD3DBuffer *)src_state->index_buffer);
719 if (stateblock->state.index_buffer)
720 IWineD3DBuffer_Release((IWineD3DBuffer *)stateblock->state.index_buffer);
721 stateblock->state.index_buffer = src_state->index_buffer;
722 stateblock->state.base_vertex_index = src_state->base_vertex_index;
723 stateblock->state.index_format = src_state->index_format;
726 if (stateblock->changed.vertexDecl && stateblock->state.vertex_declaration != src_state->vertex_declaration)
728 TRACE("Updating vertex declaration from %p to %p.\n",
729 stateblock->state.vertex_declaration, src_state->vertex_declaration);
731 if (src_state->vertex_declaration)
732 IWineD3DVertexDeclaration_AddRef((IWineD3DVertexDeclaration *)src_state->vertex_declaration);
733 if (stateblock->state.vertex_declaration)
734 IWineD3DVertexDeclaration_Release((IWineD3DVertexDeclaration *)stateblock->state.vertex_declaration);
735 stateblock->state.vertex_declaration = src_state->vertex_declaration;
738 if (stateblock->changed.material
739 && memcmp(&src_state->material, &stateblock->state.material, sizeof(stateblock->state.material)))
741 TRACE("Updating material.\n");
743 stateblock->state.material = src_state->material;
746 if (stateblock->changed.viewport
747 && memcmp(&src_state->viewport, &stateblock->state.viewport, sizeof(stateblock->state.viewport)))
749 TRACE("Updating viewport.\n");
751 stateblock->state.viewport = src_state->viewport;
754 if (stateblock->changed.scissorRect && memcmp(&src_state->scissor_rect,
755 &stateblock->state.scissor_rect, sizeof(stateblock->state.scissor_rect)))
757 TRACE("Updating scissor rect.\n");
759 stateblock->state.scissor_rect = src_state->scissor_rect;
762 map = stateblock->changed.streamSource;
763 for (i = 0; map; map >>= 1, ++i)
765 if (!(map & 1)) continue;
767 if (stateblock->state.streams[i].stride != src_state->streams[i].stride
768 || stateblock->state.streams[i].buffer != src_state->streams[i].buffer)
770 TRACE("Updating stream source %u to %p, stride to %u.\n",
771 i, src_state->streams[i].buffer,
772 src_state->streams[i].stride);
774 stateblock->state.streams[i].stride = src_state->streams[i].stride;
775 if (src_state->streams[i].buffer)
776 IWineD3DBuffer_AddRef((IWineD3DBuffer *)src_state->streams[i].buffer);
777 if (stateblock->state.streams[i].buffer)
778 IWineD3DBuffer_Release((IWineD3DBuffer *)stateblock->state.streams[i].buffer);
779 stateblock->state.streams[i].buffer = src_state->streams[i].buffer;
783 map = stateblock->changed.streamFreq;
784 for (i = 0; map; map >>= 1, ++i)
786 if (!(map & 1)) continue;
788 if (stateblock->state.streams[i].frequency != src_state->streams[i].frequency
789 || stateblock->state.streams[i].flags != src_state->streams[i].flags)
791 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
792 i, src_state->streams[i].frequency, src_state->streams[i].flags);
794 stateblock->state.streams[i].frequency = src_state->streams[i].frequency;
795 stateblock->state.streams[i].flags = src_state->streams[i].flags;
799 map = stateblock->changed.clipplane;
800 for (i = 0; map; map >>= 1, ++i)
802 if (!(map & 1)) continue;
804 if (memcmp(src_state->clip_planes[i], stateblock->state.clip_planes[i], sizeof(*stateblock->state.clip_planes)))
806 TRACE("Updating clipplane %u.\n", i);
807 memcpy(stateblock->state.clip_planes[i], src_state->clip_planes[i], sizeof(*stateblock->state.clip_planes));
812 for (i = 0; i < stateblock->num_contained_render_states; ++i)
814 WINED3DRENDERSTATETYPE rs = stateblock->contained_render_states[i];
816 TRACE("Updating render state %#x to %u.\n", rs, src_state->render_states[rs]);
818 stateblock->state.render_states[rs] = src_state->render_states[rs];
822 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
824 DWORD stage = stateblock->contained_tss_states[i].stage;
825 DWORD state = stateblock->contained_tss_states[i].state;
827 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage, state,
828 src_state->texture_states[stage][state], stateblock->state.texture_states[stage][state]);
830 stateblock->state.texture_states[stage][state] = src_state->texture_states[stage][state];
834 map = stateblock->changed.textures;
835 for (i = 0; map; map >>= 1, ++i)
837 if (!(map & 1)) continue;
839 TRACE("Updating texture %u to %p (was %p).\n",
840 i, src_state->textures[i], stateblock->state.textures[i]);
842 if (src_state->textures[i])
843 IWineD3DBaseTexture_AddRef((IWineD3DBaseTexture *)src_state->textures[i]);
844 if (stateblock->state.textures[i])
845 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *)stateblock->state.textures[i]);
846 stateblock->state.textures[i] = src_state->textures[i];
849 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
851 DWORD stage = stateblock->contained_sampler_states[i].stage;
852 DWORD state = stateblock->contained_sampler_states[i].state;
854 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage, state,
855 src_state->sampler_states[stage][state], stateblock->state.sampler_states[stage][state]);
857 stateblock->state.sampler_states[stage][state] = src_state->sampler_states[stage][state];
860 if (stateblock->changed.pixelShader && stateblock->state.pixel_shader != src_state->pixel_shader)
862 if (src_state->pixel_shader)
863 IWineD3DPixelShader_AddRef((IWineD3DPixelShader *)src_state->pixel_shader);
864 if (stateblock->state.pixel_shader)
865 IWineD3DPixelShader_Release((IWineD3DPixelShader *)stateblock->state.pixel_shader);
866 stateblock->state.pixel_shader = src_state->pixel_shader;
869 wined3d_state_record_lights(&stateblock->state, src_state);
871 TRACE("Captue done.\n");
876 static void apply_lights(IWineD3DDevice *device, const struct wined3d_state *state)
880 for (i = 0; i < LIGHTMAP_SIZE; ++i)
884 LIST_FOR_EACH(e, &state->light_map[i])
886 const struct wined3d_light_info *light = LIST_ENTRY(e, struct wined3d_light_info, entry);
888 IWineD3DDevice_SetLight(device, light->OriginalIndex, &light->OriginalParms);
889 IWineD3DDevice_SetLightEnable(device, light->OriginalIndex, light->glIndex != -1);
894 HRESULT CDECL wined3d_stateblock_apply(const struct wined3d_stateblock *stateblock)
896 IWineD3DDevice *device = (IWineD3DDevice *)stateblock->device;
900 TRACE("Applying stateblock %p to device %p.\n", stateblock, device);
901 TRACE("Blocktype: %#x.\n", stateblock->blockType);
903 if (stateblock->changed.vertexShader)
904 IWineD3DDevice_SetVertexShader(device, (IWineD3DVertexShader *)stateblock->state.vertex_shader);
906 /* Vertex Shader Constants. */
907 for (i = 0; i < stateblock->num_contained_vs_consts_f; ++i)
909 IWineD3DDevice_SetVertexShaderConstantF(device, stateblock->contained_vs_consts_f[i],
910 stateblock->state.vs_consts_f + stateblock->contained_vs_consts_f[i] * 4, 1);
912 for (i = 0; i < stateblock->num_contained_vs_consts_i; ++i)
914 IWineD3DDevice_SetVertexShaderConstantI(device, stateblock->contained_vs_consts_i[i],
915 stateblock->state.vs_consts_i + stateblock->contained_vs_consts_i[i] * 4, 1);
917 for (i = 0; i < stateblock->num_contained_vs_consts_b; ++i)
919 IWineD3DDevice_SetVertexShaderConstantB(device, stateblock->contained_vs_consts_b[i],
920 stateblock->state.vs_consts_b + stateblock->contained_vs_consts_b[i], 1);
923 apply_lights(device, &stateblock->state);
925 if (stateblock->changed.pixelShader)
926 IWineD3DDevice_SetPixelShader(device, (IWineD3DPixelShader *)stateblock->state.pixel_shader);
928 /* Pixel Shader Constants. */
929 for (i = 0; i < stateblock->num_contained_ps_consts_f; ++i)
931 IWineD3DDevice_SetPixelShaderConstantF(device, stateblock->contained_ps_consts_f[i],
932 stateblock->state.ps_consts_f + stateblock->contained_ps_consts_f[i] * 4, 1);
934 for (i = 0; i < stateblock->num_contained_ps_consts_i; ++i)
936 IWineD3DDevice_SetPixelShaderConstantI(device, stateblock->contained_ps_consts_i[i],
937 stateblock->state.ps_consts_i + stateblock->contained_ps_consts_i[i] * 4, 1);
939 for (i = 0; i < stateblock->num_contained_ps_consts_b; ++i)
941 IWineD3DDevice_SetPixelShaderConstantB(device, stateblock->contained_ps_consts_b[i],
942 stateblock->state.ps_consts_b + stateblock->contained_ps_consts_b[i], 1);
946 for (i = 0; i < stateblock->num_contained_render_states; ++i)
948 IWineD3DDevice_SetRenderState(device, stateblock->contained_render_states[i],
949 stateblock->state.render_states[stateblock->contained_render_states[i]]);
952 /* Texture states. */
953 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
955 DWORD stage = stateblock->contained_tss_states[i].stage;
956 DWORD state = stateblock->contained_tss_states[i].state;
958 IWineD3DDevice_SetTextureStageState(device, stage, state, stateblock->state.texture_states[stage][state]);
961 /* Sampler states. */
962 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
964 DWORD stage = stateblock->contained_sampler_states[i].stage;
965 DWORD state = stateblock->contained_sampler_states[i].state;
966 DWORD value = stateblock->state.sampler_states[stage][state];
968 if (stage >= MAX_FRAGMENT_SAMPLERS) stage += WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS;
969 IWineD3DDevice_SetSamplerState(device, stage, state, value);
972 /* Transform states. */
973 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
975 IWineD3DDevice_SetTransform(device, stateblock->contained_transform_states[i],
976 &stateblock->state.transforms[stateblock->contained_transform_states[i]]);
979 if (stateblock->changed.primitive_type)
981 stateblock->device->updateStateBlock->changed.primitive_type = TRUE;
982 stateblock->device->updateStateBlock->state.gl_primitive_type = stateblock->state.gl_primitive_type;
985 if (stateblock->changed.indices)
987 IWineD3DDevice_SetIndexBuffer(device, (IWineD3DBuffer *)stateblock->state.index_buffer,
988 stateblock->state.index_format);
989 IWineD3DDevice_SetBaseVertexIndex(device, stateblock->state.base_vertex_index);
992 if (stateblock->changed.vertexDecl && stateblock->state.vertex_declaration)
994 IWineD3DDevice_SetVertexDeclaration(device, (IWineD3DVertexDeclaration *)stateblock->state.vertex_declaration);
997 if (stateblock->changed.material)
999 IWineD3DDevice_SetMaterial(device, &stateblock->state.material);
1002 if (stateblock->changed.viewport)
1004 IWineD3DDevice_SetViewport(device, &stateblock->state.viewport);
1007 if (stateblock->changed.scissorRect)
1009 IWineD3DDevice_SetScissorRect(device, &stateblock->state.scissor_rect);
1012 map = stateblock->changed.streamSource;
1013 for (i = 0; map; map >>= 1, ++i)
1016 IWineD3DDevice_SetStreamSource(device, i,
1017 (IWineD3DBuffer *)stateblock->state.streams[i].buffer,
1018 0, stateblock->state.streams[i].stride);
1021 map = stateblock->changed.streamFreq;
1022 for (i = 0; map; map >>= 1, ++i)
1025 IWineD3DDevice_SetStreamSourceFreq(device, i,
1026 stateblock->state.streams[i].frequency | stateblock->state.streams[i].flags);
1029 map = stateblock->changed.textures;
1030 for (i = 0; map; map >>= 1, ++i)
1034 if (!(map & 1)) continue;
1036 stage = i < MAX_FRAGMENT_SAMPLERS ? i : WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS;
1037 IWineD3DDevice_SetTexture(device, stage, (IWineD3DBaseTexture *)stateblock->state.textures[i]);
1040 map = stateblock->changed.clipplane;
1041 for (i = 0; map; map >>= 1, ++i)
1045 if (!(map & 1)) continue;
1047 clip[0] = stateblock->state.clip_planes[i][0];
1048 clip[1] = stateblock->state.clip_planes[i][1];
1049 clip[2] = stateblock->state.clip_planes[i][2];
1050 clip[3] = stateblock->state.clip_planes[i][3];
1051 IWineD3DDevice_SetClipPlane(device, i, clip);
1054 stateblock->device->stateBlock->state.lowest_disabled_stage = MAX_TEXTURES - 1;
1055 for (i = 0; i < MAX_TEXTURES - 1; ++i)
1057 if (stateblock->device->stateBlock->state.texture_states[i][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE)
1059 stateblock->device->stateBlock->state.lowest_disabled_stage = i;
1064 TRACE("Applied stateblock %p.\n", stateblock);
1069 void stateblock_init_default_state(struct wined3d_stateblock *stateblock)
1071 IWineD3DDeviceImpl *device = stateblock->device;
1072 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1073 struct wined3d_state *state = &stateblock->state;
1075 WINED3DLINEPATTERN lp;
1083 IWineD3DSwapChain *swapchain;
1084 IWineD3DSurface *backbuffer;
1087 TRACE("stateblock %p.\n", stateblock);
1089 stateblock->blockType = WINED3DSBT_INIT;
1091 /* Set some of the defaults for lights, transforms etc */
1092 memcpy(&state->transforms[WINED3DTS_PROJECTION], identity, sizeof(identity));
1093 memcpy(&state->transforms[WINED3DTS_VIEW], identity, sizeof(identity));
1094 for (i = 0; i < 256; ++i)
1096 memcpy(&state->transforms[WINED3DTS_WORLDMATRIX(i)], identity, sizeof(identity));
1099 TRACE("Render states\n");
1100 /* Render states: */
1101 if (device->auto_depth_stencil)
1102 state->render_states[WINED3DRS_ZENABLE] = WINED3DZB_TRUE;
1104 state->render_states[WINED3DRS_ZENABLE] = WINED3DZB_FALSE;
1105 state->render_states[WINED3DRS_FILLMODE] = WINED3DFILL_SOLID;
1106 state->render_states[WINED3DRS_SHADEMODE] = WINED3DSHADE_GOURAUD;
1107 lp.lp.wRepeatFactor = 0;
1108 lp.lp.wLinePattern = 0;
1109 state->render_states[WINED3DRS_LINEPATTERN] = lp.d;
1110 state->render_states[WINED3DRS_ZWRITEENABLE] = TRUE;
1111 state->render_states[WINED3DRS_ALPHATESTENABLE] = FALSE;
1112 state->render_states[WINED3DRS_LASTPIXEL] = TRUE;
1113 state->render_states[WINED3DRS_SRCBLEND] = WINED3DBLEND_ONE;
1114 state->render_states[WINED3DRS_DESTBLEND] = WINED3DBLEND_ZERO;
1115 state->render_states[WINED3DRS_CULLMODE] = WINED3DCULL_CCW;
1116 state->render_states[WINED3DRS_ZFUNC] = WINED3DCMP_LESSEQUAL;
1117 state->render_states[WINED3DRS_ALPHAFUNC] = WINED3DCMP_ALWAYS;
1118 state->render_states[WINED3DRS_ALPHAREF] = 0;
1119 state->render_states[WINED3DRS_DITHERENABLE] = FALSE;
1120 state->render_states[WINED3DRS_ALPHABLENDENABLE] = FALSE;
1121 state->render_states[WINED3DRS_FOGENABLE] = FALSE;
1122 state->render_states[WINED3DRS_SPECULARENABLE] = FALSE;
1123 state->render_states[WINED3DRS_ZVISIBLE] = 0;
1124 state->render_states[WINED3DRS_FOGCOLOR] = 0;
1125 state->render_states[WINED3DRS_FOGTABLEMODE] = WINED3DFOG_NONE;
1127 state->render_states[WINED3DRS_FOGSTART] = tmpfloat.d;
1129 state->render_states[WINED3DRS_FOGEND] = tmpfloat.d;
1131 state->render_states[WINED3DRS_FOGDENSITY] = tmpfloat.d;
1132 state->render_states[WINED3DRS_EDGEANTIALIAS] = FALSE;
1133 state->render_states[WINED3DRS_ZBIAS] = 0;
1134 state->render_states[WINED3DRS_RANGEFOGENABLE] = FALSE;
1135 state->render_states[WINED3DRS_STENCILENABLE] = FALSE;
1136 state->render_states[WINED3DRS_STENCILFAIL] = WINED3DSTENCILOP_KEEP;
1137 state->render_states[WINED3DRS_STENCILZFAIL] = WINED3DSTENCILOP_KEEP;
1138 state->render_states[WINED3DRS_STENCILPASS] = WINED3DSTENCILOP_KEEP;
1139 state->render_states[WINED3DRS_STENCILREF] = 0;
1140 state->render_states[WINED3DRS_STENCILMASK] = 0xffffffff;
1141 state->render_states[WINED3DRS_STENCILFUNC] = WINED3DCMP_ALWAYS;
1142 state->render_states[WINED3DRS_STENCILWRITEMASK] = 0xffffffff;
1143 state->render_states[WINED3DRS_TEXTUREFACTOR] = 0xffffffff;
1144 state->render_states[WINED3DRS_WRAP0] = 0;
1145 state->render_states[WINED3DRS_WRAP1] = 0;
1146 state->render_states[WINED3DRS_WRAP2] = 0;
1147 state->render_states[WINED3DRS_WRAP3] = 0;
1148 state->render_states[WINED3DRS_WRAP4] = 0;
1149 state->render_states[WINED3DRS_WRAP5] = 0;
1150 state->render_states[WINED3DRS_WRAP6] = 0;
1151 state->render_states[WINED3DRS_WRAP7] = 0;
1152 state->render_states[WINED3DRS_CLIPPING] = TRUE;
1153 state->render_states[WINED3DRS_LIGHTING] = TRUE;
1154 state->render_states[WINED3DRS_AMBIENT] = 0;
1155 state->render_states[WINED3DRS_FOGVERTEXMODE] = WINED3DFOG_NONE;
1156 state->render_states[WINED3DRS_COLORVERTEX] = TRUE;
1157 state->render_states[WINED3DRS_LOCALVIEWER] = TRUE;
1158 state->render_states[WINED3DRS_NORMALIZENORMALS] = FALSE;
1159 state->render_states[WINED3DRS_DIFFUSEMATERIALSOURCE] = WINED3DMCS_COLOR1;
1160 state->render_states[WINED3DRS_SPECULARMATERIALSOURCE] = WINED3DMCS_COLOR2;
1161 state->render_states[WINED3DRS_AMBIENTMATERIALSOURCE] = WINED3DMCS_MATERIAL;
1162 state->render_states[WINED3DRS_EMISSIVEMATERIALSOURCE] = WINED3DMCS_MATERIAL;
1163 state->render_states[WINED3DRS_VERTEXBLEND] = WINED3DVBF_DISABLE;
1164 state->render_states[WINED3DRS_CLIPPLANEENABLE] = 0;
1165 state->render_states[WINED3DRS_SOFTWAREVERTEXPROCESSING] = FALSE;
1167 state->render_states[WINED3DRS_POINTSIZE] = tmpfloat.d;
1169 state->render_states[WINED3DRS_POINTSIZE_MIN] = tmpfloat.d;
1170 state->render_states[WINED3DRS_POINTSPRITEENABLE] = FALSE;
1171 state->render_states[WINED3DRS_POINTSCALEENABLE] = FALSE;
1173 state->render_states[WINED3DRS_POINTSCALE_A] = tmpfloat.d;
1175 state->render_states[WINED3DRS_POINTSCALE_B] = tmpfloat.d;
1177 state->render_states[WINED3DRS_POINTSCALE_C] = tmpfloat.d;
1178 state->render_states[WINED3DRS_MULTISAMPLEANTIALIAS] = TRUE;
1179 state->render_states[WINED3DRS_MULTISAMPLEMASK] = 0xffffffff;
1180 state->render_states[WINED3DRS_PATCHEDGESTYLE] = WINED3DPATCHEDGE_DISCRETE;
1182 state->render_states[WINED3DRS_PATCHSEGMENTS] = tmpfloat.d;
1183 state->render_states[WINED3DRS_DEBUGMONITORTOKEN] = 0xbaadcafe;
1184 tmpfloat.f = gl_info->limits.pointsize_max;
1185 state->render_states[WINED3DRS_POINTSIZE_MAX] = tmpfloat.d;
1186 state->render_states[WINED3DRS_INDEXEDVERTEXBLENDENABLE] = FALSE;
1187 state->render_states[WINED3DRS_COLORWRITEENABLE] = 0x0000000f;
1189 state->render_states[WINED3DRS_TWEENFACTOR] = tmpfloat.d;
1190 state->render_states[WINED3DRS_BLENDOP] = WINED3DBLENDOP_ADD;
1191 state->render_states[WINED3DRS_POSITIONDEGREE] = WINED3DDEGREE_CUBIC;
1192 state->render_states[WINED3DRS_NORMALDEGREE] = WINED3DDEGREE_LINEAR;
1193 /* states new in d3d9 */
1194 state->render_states[WINED3DRS_SCISSORTESTENABLE] = FALSE;
1195 state->render_states[WINED3DRS_SLOPESCALEDEPTHBIAS] = 0;
1197 state->render_states[WINED3DRS_MINTESSELLATIONLEVEL] = tmpfloat.d;
1198 state->render_states[WINED3DRS_MAXTESSELLATIONLEVEL] = tmpfloat.d;
1199 state->render_states[WINED3DRS_ANTIALIASEDLINEENABLE] = FALSE;
1201 state->render_states[WINED3DRS_ADAPTIVETESS_X] = tmpfloat.d;
1202 state->render_states[WINED3DRS_ADAPTIVETESS_Y] = tmpfloat.d;
1204 state->render_states[WINED3DRS_ADAPTIVETESS_Z] = tmpfloat.d;
1206 state->render_states[WINED3DRS_ADAPTIVETESS_W] = tmpfloat.d;
1207 state->render_states[WINED3DRS_ENABLEADAPTIVETESSELLATION] = FALSE;
1208 state->render_states[WINED3DRS_TWOSIDEDSTENCILMODE] = FALSE;
1209 state->render_states[WINED3DRS_CCW_STENCILFAIL] = WINED3DSTENCILOP_KEEP;
1210 state->render_states[WINED3DRS_CCW_STENCILZFAIL] = WINED3DSTENCILOP_KEEP;
1211 state->render_states[WINED3DRS_CCW_STENCILPASS] = WINED3DSTENCILOP_KEEP;
1212 state->render_states[WINED3DRS_CCW_STENCILFUNC] = WINED3DCMP_ALWAYS;
1213 state->render_states[WINED3DRS_COLORWRITEENABLE1] = 0x0000000f;
1214 state->render_states[WINED3DRS_COLORWRITEENABLE2] = 0x0000000f;
1215 state->render_states[WINED3DRS_COLORWRITEENABLE3] = 0x0000000f;
1216 state->render_states[WINED3DRS_BLENDFACTOR] = 0xFFFFFFFF;
1217 state->render_states[WINED3DRS_SRGBWRITEENABLE] = 0;
1218 state->render_states[WINED3DRS_DEPTHBIAS] = 0;
1219 state->render_states[WINED3DRS_WRAP8] = 0;
1220 state->render_states[WINED3DRS_WRAP9] = 0;
1221 state->render_states[WINED3DRS_WRAP10] = 0;
1222 state->render_states[WINED3DRS_WRAP11] = 0;
1223 state->render_states[WINED3DRS_WRAP12] = 0;
1224 state->render_states[WINED3DRS_WRAP13] = 0;
1225 state->render_states[WINED3DRS_WRAP14] = 0;
1226 state->render_states[WINED3DRS_WRAP15] = 0;
1227 state->render_states[WINED3DRS_SEPARATEALPHABLENDENABLE] = FALSE;
1228 state->render_states[WINED3DRS_SRCBLENDALPHA] = WINED3DBLEND_ONE;
1229 state->render_states[WINED3DRS_DESTBLENDALPHA] = WINED3DBLEND_ZERO;
1230 state->render_states[WINED3DRS_BLENDOPALPHA] = WINED3DBLENDOP_ADD;
1232 /* clipping status */
1233 state->clip_status.ClipUnion = 0;
1234 state->clip_status.ClipIntersection = 0xFFFFFFFF;
1236 /* Texture Stage States - Put directly into state block, we will call function below */
1237 for (i = 0; i < MAX_TEXTURES; ++i)
1239 TRACE("Setting up default texture states for texture Stage %u.\n", i);
1240 memcpy(&state->transforms[WINED3DTS_TEXTURE0 + i], identity, sizeof(identity));
1241 state->texture_states[i][WINED3DTSS_COLOROP] = i ? WINED3DTOP_DISABLE : WINED3DTOP_MODULATE;
1242 state->texture_states[i][WINED3DTSS_COLORARG1] = WINED3DTA_TEXTURE;
1243 state->texture_states[i][WINED3DTSS_COLORARG2] = WINED3DTA_CURRENT;
1244 state->texture_states[i][WINED3DTSS_ALPHAOP] = i ? WINED3DTOP_DISABLE : WINED3DTOP_SELECTARG1;
1245 state->texture_states[i][WINED3DTSS_ALPHAARG1] = WINED3DTA_TEXTURE;
1246 state->texture_states[i][WINED3DTSS_ALPHAARG2] = WINED3DTA_CURRENT;
1247 state->texture_states[i][WINED3DTSS_BUMPENVMAT00] = 0;
1248 state->texture_states[i][WINED3DTSS_BUMPENVMAT01] = 0;
1249 state->texture_states[i][WINED3DTSS_BUMPENVMAT10] = 0;
1250 state->texture_states[i][WINED3DTSS_BUMPENVMAT11] = 0;
1251 state->texture_states[i][WINED3DTSS_TEXCOORDINDEX] = i;
1252 state->texture_states[i][WINED3DTSS_BUMPENVLSCALE] = 0;
1253 state->texture_states[i][WINED3DTSS_BUMPENVLOFFSET] = 0;
1254 state->texture_states[i][WINED3DTSS_TEXTURETRANSFORMFLAGS] = WINED3DTTFF_DISABLE;
1255 state->texture_states[i][WINED3DTSS_COLORARG0] = WINED3DTA_CURRENT;
1256 state->texture_states[i][WINED3DTSS_ALPHAARG0] = WINED3DTA_CURRENT;
1257 state->texture_states[i][WINED3DTSS_RESULTARG] = WINED3DTA_CURRENT;
1259 state->lowest_disabled_stage = 1;
1262 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; ++i)
1264 TRACE("Setting up default samplers states for sampler %u.\n", i);
1265 state->sampler_states[i][WINED3DSAMP_ADDRESSU] = WINED3DTADDRESS_WRAP;
1266 state->sampler_states[i][WINED3DSAMP_ADDRESSV] = WINED3DTADDRESS_WRAP;
1267 state->sampler_states[i][WINED3DSAMP_ADDRESSW] = WINED3DTADDRESS_WRAP;
1268 state->sampler_states[i][WINED3DSAMP_BORDERCOLOR] = 0;
1269 state->sampler_states[i][WINED3DSAMP_MAGFILTER] = WINED3DTEXF_POINT;
1270 state->sampler_states[i][WINED3DSAMP_MINFILTER] = WINED3DTEXF_POINT;
1271 state->sampler_states[i][WINED3DSAMP_MIPFILTER] = WINED3DTEXF_NONE;
1272 state->sampler_states[i][WINED3DSAMP_MIPMAPLODBIAS] = 0;
1273 state->sampler_states[i][WINED3DSAMP_MAXMIPLEVEL] = 0;
1274 state->sampler_states[i][WINED3DSAMP_MAXANISOTROPY] = 1;
1275 state->sampler_states[i][WINED3DSAMP_SRGBTEXTURE] = 0;
1276 /* TODO: Indicates which element of a multielement texture to use. */
1277 state->sampler_states[i][WINED3DSAMP_ELEMENTINDEX] = 0;
1278 /* TODO: Vertex offset in the presampled displacement map. */
1279 state->sampler_states[i][WINED3DSAMP_DMAPOFFSET] = 0;
1282 for (i = 0; i < gl_info->limits.textures; ++i)
1284 state->textures[i] = NULL;
1287 /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1288 hr = IWineD3DDevice_GetSwapChain((IWineD3DDevice *)device, 0, &swapchain);
1289 if (SUCCEEDED(hr) && swapchain)
1291 hr = IWineD3DSwapChain_GetBackBuffer(swapchain, 0, WINED3DBACKBUFFER_TYPE_MONO, &backbuffer);
1292 if (SUCCEEDED(hr) && backbuffer)
1294 WINED3DSURFACE_DESC desc;
1296 IWineD3DSurface_GetDesc(backbuffer, &desc);
1297 IWineD3DSurface_Release(backbuffer);
1299 /* Set the default scissor rect values */
1300 state->scissor_rect.left = 0;
1301 state->scissor_rect.right = desc.width;
1302 state->scissor_rect.top = 0;
1303 state->scissor_rect.bottom = desc.height;
1306 /* Set the default viewport */
1307 state->viewport.X = 0;
1308 state->viewport.Y = 0;
1309 state->viewport.Width = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferWidth;
1310 state->viewport.Height = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferHeight;
1311 state->viewport.MinZ = 0.0f;
1312 state->viewport.MaxZ = 1.0f;
1314 IWineD3DSwapChain_Release(swapchain);
1320 HRESULT stateblock_init(struct wined3d_stateblock *stateblock, IWineD3DDeviceImpl *device, WINED3DSTATEBLOCKTYPE type)
1325 stateblock->ref = 1;
1326 stateblock->device = device;
1327 stateblock->blockType = type;
1329 for (i = 0; i < LIGHTMAP_SIZE; i++)
1331 list_init(&stateblock->state.light_map[i]);
1334 hr = stateblock_allocate_shader_constants(stateblock);
1335 if (FAILED(hr)) return hr;
1337 /* The WINED3DSBT_INIT stateblock type is used during initialization to
1338 * produce a placeholder stateblock so other functions called can update a
1340 if (type == WINED3DSBT_INIT || type == WINED3DSBT_RECORDED) return WINED3D_OK;
1342 TRACE("Updating changed flags appropriate for type %#x.\n", type);
1346 case WINED3DSBT_ALL:
1347 stateblock_init_lights(stateblock, device->stateBlock->state.light_map);
1348 stateblock_savedstates_set_all(&stateblock->changed, device->d3d_vshader_constantF,
1349 device->d3d_pshader_constantF);
1352 case WINED3DSBT_PIXELSTATE:
1353 stateblock_savedstates_set_pixel(&stateblock->changed, device->d3d_pshader_constantF);
1356 case WINED3DSBT_VERTEXSTATE:
1357 stateblock_init_lights(stateblock, device->stateBlock->state.light_map);
1358 stateblock_savedstates_set_vertex(&stateblock->changed, device->d3d_vshader_constantF);
1362 FIXME("Unrecognized state block type %#x.\n", type);
1366 stateblock_init_contained_states(stateblock);
1367 wined3d_stateblock_capture(stateblock);