rpcrt4: Write the function header into the procedure format string.
[wine.git] / dlls / wined3d / stateblock.c
blob0b2ce50ecd7d144b60e13dde4b4128b1e0f167fb
1 /*
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
25 #include "config.h"
26 #include "wine/port.h"
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
31 static const DWORD pixel_states_render[] =
33 WINED3D_RS_ALPHABLENDENABLE,
34 WINED3D_RS_ALPHAFUNC,
35 WINED3D_RS_ALPHAREF,
36 WINED3D_RS_ALPHATESTENABLE,
37 WINED3D_RS_ANTIALIASEDLINEENABLE,
38 WINED3D_RS_BLENDOP,
39 WINED3D_RS_BLENDOPALPHA,
40 WINED3D_RS_BACK_STENCILFAIL,
41 WINED3D_RS_BACK_STENCILPASS,
42 WINED3D_RS_BACK_STENCILZFAIL,
43 WINED3D_RS_COLORWRITEENABLE,
44 WINED3D_RS_COLORWRITEENABLE1,
45 WINED3D_RS_COLORWRITEENABLE2,
46 WINED3D_RS_COLORWRITEENABLE3,
47 WINED3D_RS_DEPTHBIAS,
48 WINED3D_RS_DESTBLEND,
49 WINED3D_RS_DESTBLENDALPHA,
50 WINED3D_RS_DITHERENABLE,
51 WINED3D_RS_FILLMODE,
52 WINED3D_RS_FOGDENSITY,
53 WINED3D_RS_FOGEND,
54 WINED3D_RS_FOGSTART,
55 WINED3D_RS_LASTPIXEL,
56 WINED3D_RS_SCISSORTESTENABLE,
57 WINED3D_RS_SEPARATEALPHABLENDENABLE,
58 WINED3D_RS_SHADEMODE,
59 WINED3D_RS_SLOPESCALEDEPTHBIAS,
60 WINED3D_RS_SRCBLEND,
61 WINED3D_RS_SRCBLENDALPHA,
62 WINED3D_RS_SRGBWRITEENABLE,
63 WINED3D_RS_STENCILENABLE,
64 WINED3D_RS_STENCILFAIL,
65 WINED3D_RS_STENCILFUNC,
66 WINED3D_RS_STENCILMASK,
67 WINED3D_RS_STENCILPASS,
68 WINED3D_RS_STENCILREF,
69 WINED3D_RS_STENCILWRITEMASK,
70 WINED3D_RS_STENCILZFAIL,
71 WINED3D_RS_TEXTUREFACTOR,
72 WINED3D_RS_TWOSIDEDSTENCILMODE,
73 WINED3D_RS_WRAP0,
74 WINED3D_RS_WRAP1,
75 WINED3D_RS_WRAP10,
76 WINED3D_RS_WRAP11,
77 WINED3D_RS_WRAP12,
78 WINED3D_RS_WRAP13,
79 WINED3D_RS_WRAP14,
80 WINED3D_RS_WRAP15,
81 WINED3D_RS_WRAP2,
82 WINED3D_RS_WRAP3,
83 WINED3D_RS_WRAP4,
84 WINED3D_RS_WRAP5,
85 WINED3D_RS_WRAP6,
86 WINED3D_RS_WRAP7,
87 WINED3D_RS_WRAP8,
88 WINED3D_RS_WRAP9,
89 WINED3D_RS_ZENABLE,
90 WINED3D_RS_ZFUNC,
91 WINED3D_RS_ZWRITEENABLE,
94 static const DWORD pixel_states_texture[] =
96 WINED3D_TSS_ALPHA_ARG0,
97 WINED3D_TSS_ALPHA_ARG1,
98 WINED3D_TSS_ALPHA_ARG2,
99 WINED3D_TSS_ALPHA_OP,
100 WINED3D_TSS_BUMPENV_LOFFSET,
101 WINED3D_TSS_BUMPENV_LSCALE,
102 WINED3D_TSS_BUMPENV_MAT00,
103 WINED3D_TSS_BUMPENV_MAT01,
104 WINED3D_TSS_BUMPENV_MAT10,
105 WINED3D_TSS_BUMPENV_MAT11,
106 WINED3D_TSS_COLOR_ARG0,
107 WINED3D_TSS_COLOR_ARG1,
108 WINED3D_TSS_COLOR_ARG2,
109 WINED3D_TSS_COLOR_OP,
110 WINED3D_TSS_RESULT_ARG,
111 WINED3D_TSS_TEXCOORD_INDEX,
112 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,
115 static const DWORD pixel_states_sampler[] =
117 WINED3D_SAMP_ADDRESS_U,
118 WINED3D_SAMP_ADDRESS_V,
119 WINED3D_SAMP_ADDRESS_W,
120 WINED3D_SAMP_BORDER_COLOR,
121 WINED3D_SAMP_MAG_FILTER,
122 WINED3D_SAMP_MIN_FILTER,
123 WINED3D_SAMP_MIP_FILTER,
124 WINED3D_SAMP_MIPMAP_LOD_BIAS,
125 WINED3D_SAMP_MAX_MIP_LEVEL,
126 WINED3D_SAMP_MAX_ANISOTROPY,
127 WINED3D_SAMP_SRGB_TEXTURE,
128 WINED3D_SAMP_ELEMENT_INDEX,
131 static const DWORD vertex_states_render[] =
133 WINED3D_RS_ADAPTIVETESS_W,
134 WINED3D_RS_ADAPTIVETESS_X,
135 WINED3D_RS_ADAPTIVETESS_Y,
136 WINED3D_RS_ADAPTIVETESS_Z,
137 WINED3D_RS_AMBIENT,
138 WINED3D_RS_AMBIENTMATERIALSOURCE,
139 WINED3D_RS_CLIPPING,
140 WINED3D_RS_CLIPPLANEENABLE,
141 WINED3D_RS_COLORVERTEX,
142 WINED3D_RS_CULLMODE,
143 WINED3D_RS_DIFFUSEMATERIALSOURCE,
144 WINED3D_RS_EMISSIVEMATERIALSOURCE,
145 WINED3D_RS_ENABLEADAPTIVETESSELLATION,
146 WINED3D_RS_FOGCOLOR,
147 WINED3D_RS_FOGDENSITY,
148 WINED3D_RS_FOGENABLE,
149 WINED3D_RS_FOGEND,
150 WINED3D_RS_FOGSTART,
151 WINED3D_RS_FOGTABLEMODE,
152 WINED3D_RS_FOGVERTEXMODE,
153 WINED3D_RS_INDEXEDVERTEXBLENDENABLE,
154 WINED3D_RS_LIGHTING,
155 WINED3D_RS_LOCALVIEWER,
156 WINED3D_RS_MAXTESSELLATIONLEVEL,
157 WINED3D_RS_MINTESSELLATIONLEVEL,
158 WINED3D_RS_MULTISAMPLEANTIALIAS,
159 WINED3D_RS_MULTISAMPLEMASK,
160 WINED3D_RS_NORMALDEGREE,
161 WINED3D_RS_NORMALIZENORMALS,
162 WINED3D_RS_PATCHEDGESTYLE,
163 WINED3D_RS_POINTSCALE_A,
164 WINED3D_RS_POINTSCALE_B,
165 WINED3D_RS_POINTSCALE_C,
166 WINED3D_RS_POINTSCALEENABLE,
167 WINED3D_RS_POINTSIZE,
168 WINED3D_RS_POINTSIZE_MAX,
169 WINED3D_RS_POINTSIZE_MIN,
170 WINED3D_RS_POINTSPRITEENABLE,
171 WINED3D_RS_POSITIONDEGREE,
172 WINED3D_RS_RANGEFOGENABLE,
173 WINED3D_RS_SHADEMODE,
174 WINED3D_RS_SPECULARENABLE,
175 WINED3D_RS_SPECULARMATERIALSOURCE,
176 WINED3D_RS_TWEENFACTOR,
177 WINED3D_RS_VERTEXBLEND,
180 static const DWORD vertex_states_texture[] =
182 WINED3D_TSS_TEXCOORD_INDEX,
183 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,
186 static const DWORD vertex_states_sampler[] =
188 WINED3D_SAMP_DMAP_OFFSET,
191 static inline void stateblock_set_bits(DWORD *map, UINT map_size)
193 DWORD mask = (1u << (map_size & 0x1f)) - 1;
194 memset(map, 0xff, (map_size >> 5) * sizeof(*map));
195 if (mask) map[map_size >> 5] = mask;
198 /* Set all members of a stateblock savedstate to the given value */
199 static void stateblock_savedstates_set_all(struct wined3d_saved_states *states, DWORD vs_consts, DWORD ps_consts)
201 unsigned int i;
203 /* Single values */
204 states->indices = 1;
205 states->material = 1;
206 states->viewport = 1;
207 states->vertexDecl = 1;
208 states->pixelShader = 1;
209 states->vertexShader = 1;
210 states->scissorRect = 1;
211 states->blend_state = 1;
213 /* Fixed size arrays */
214 states->streamSource = 0xffff;
215 states->streamFreq = 0xffff;
216 states->textures = 0xfffff;
217 stateblock_set_bits(states->transform, HIGHEST_TRANSFORMSTATE + 1);
218 stateblock_set_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
219 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
220 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3ffe;
221 states->clipplane = (1u << MAX_CLIP_DISTANCES) - 1;
222 states->pixelShaderConstantsB = 0xffff;
223 states->pixelShaderConstantsI = 0xffff;
224 states->vertexShaderConstantsB = 0xffff;
225 states->vertexShaderConstantsI = 0xffff;
227 /* Dynamically sized arrays */
228 memset(states->ps_consts_f, TRUE, sizeof(BOOL) * ps_consts);
229 memset(states->vs_consts_f, TRUE, sizeof(BOOL) * vs_consts);
232 static void stateblock_savedstates_set_pixel(struct wined3d_saved_states *states, const DWORD num_constants)
234 DWORD texture_mask = 0;
235 WORD sampler_mask = 0;
236 unsigned int i;
238 states->pixelShader = 1;
239 states->blend_state = 1;
241 for (i = 0; i < ARRAY_SIZE(pixel_states_render); ++i)
243 DWORD rs = pixel_states_render[i];
244 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
247 for (i = 0; i < ARRAY_SIZE(pixel_states_texture); ++i)
248 texture_mask |= 1u << pixel_states_texture[i];
249 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
250 for (i = 0; i < ARRAY_SIZE(pixel_states_sampler); ++i)
251 sampler_mask |= 1u << pixel_states_sampler[i];
252 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
253 states->pixelShaderConstantsB = 0xffff;
254 states->pixelShaderConstantsI = 0xffff;
256 memset(states->ps_consts_f, TRUE, sizeof(BOOL) * num_constants);
259 static void stateblock_savedstates_set_vertex(struct wined3d_saved_states *states, const DWORD num_constants)
261 DWORD texture_mask = 0;
262 WORD sampler_mask = 0;
263 unsigned int i;
265 states->vertexDecl = 1;
266 states->vertexShader = 1;
268 for (i = 0; i < ARRAY_SIZE(vertex_states_render); ++i)
270 DWORD rs = vertex_states_render[i];
271 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
274 for (i = 0; i < ARRAY_SIZE(vertex_states_texture); ++i)
275 texture_mask |= 1u << vertex_states_texture[i];
276 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
277 for (i = 0; i < ARRAY_SIZE(vertex_states_sampler); ++i)
278 sampler_mask |= 1u << vertex_states_sampler[i];
279 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
280 states->vertexShaderConstantsB = 0xffff;
281 states->vertexShaderConstantsI = 0xffff;
283 memset(states->vs_consts_f, TRUE, sizeof(BOOL) * num_constants);
286 void stateblock_init_contained_states(struct wined3d_stateblock *stateblock)
288 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
289 unsigned int i, j;
291 for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
293 DWORD map = stateblock->changed.renderState[i];
294 for (j = 0; map; map >>= 1, ++j)
296 if (!(map & 1)) continue;
298 stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
299 ++stateblock->num_contained_render_states;
303 for (i = 0; i <= HIGHEST_TRANSFORMSTATE >> 5; ++i)
305 DWORD map = stateblock->changed.transform[i];
306 for (j = 0; map; map >>= 1, ++j)
308 if (!(map & 1)) continue;
310 stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
311 ++stateblock->num_contained_transform_states;
315 for (i = 0; i < d3d_info->limits.vs_uniform_count; ++i)
317 if (stateblock->changed.vs_consts_f[i])
319 stateblock->contained_vs_consts_f[stateblock->num_contained_vs_consts_f] = i;
320 ++stateblock->num_contained_vs_consts_f;
324 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
326 if (stateblock->changed.vertexShaderConstantsI & (1u << i))
328 stateblock->contained_vs_consts_i[stateblock->num_contained_vs_consts_i] = i;
329 ++stateblock->num_contained_vs_consts_i;
333 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
335 if (stateblock->changed.vertexShaderConstantsB & (1u << i))
337 stateblock->contained_vs_consts_b[stateblock->num_contained_vs_consts_b] = i;
338 ++stateblock->num_contained_vs_consts_b;
342 for (i = 0; i < d3d_info->limits.ps_uniform_count; ++i)
344 if (stateblock->changed.ps_consts_f[i])
346 stateblock->contained_ps_consts_f[stateblock->num_contained_ps_consts_f] = i;
347 ++stateblock->num_contained_ps_consts_f;
351 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
353 if (stateblock->changed.pixelShaderConstantsI & (1u << i))
355 stateblock->contained_ps_consts_i[stateblock->num_contained_ps_consts_i] = i;
356 ++stateblock->num_contained_ps_consts_i;
360 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
362 if (stateblock->changed.pixelShaderConstantsB & (1u << i))
364 stateblock->contained_ps_consts_b[stateblock->num_contained_ps_consts_b] = i;
365 ++stateblock->num_contained_ps_consts_b;
369 for (i = 0; i < MAX_TEXTURES; ++i)
371 DWORD map = stateblock->changed.textureState[i];
373 for(j = 0; map; map >>= 1, ++j)
375 if (!(map & 1)) continue;
377 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
378 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
379 ++stateblock->num_contained_tss_states;
383 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
385 DWORD map = stateblock->changed.samplerState[i];
387 for (j = 0; map; map >>= 1, ++j)
389 if (!(map & 1)) continue;
391 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
392 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
393 ++stateblock->num_contained_sampler_states;
398 static void stateblock_init_lights(struct wined3d_stateblock *stateblock, struct list *light_map)
400 unsigned int i;
402 for (i = 0; i < LIGHTMAP_SIZE; ++i)
404 const struct wined3d_light_info *src_light;
406 LIST_FOR_EACH_ENTRY(src_light, &light_map[i], struct wined3d_light_info, entry)
408 struct wined3d_light_info *dst_light = heap_alloc(sizeof(*dst_light));
410 *dst_light = *src_light;
411 list_add_tail(&stateblock->state.light_map[i], &dst_light->entry);
416 ULONG CDECL wined3d_stateblock_incref(struct wined3d_stateblock *stateblock)
418 ULONG refcount = InterlockedIncrement(&stateblock->ref);
420 TRACE("%p increasing refcount to %u.\n", stateblock, refcount);
422 return refcount;
425 void state_unbind_resources(struct wined3d_state *state)
427 struct wined3d_unordered_access_view *uav;
428 struct wined3d_shader_resource_view *srv;
429 struct wined3d_vertex_declaration *decl;
430 struct wined3d_sampler *sampler;
431 struct wined3d_texture *texture;
432 struct wined3d_buffer *buffer;
433 struct wined3d_shader *shader;
434 unsigned int i, j;
436 if ((decl = state->vertex_declaration))
438 state->vertex_declaration = NULL;
439 wined3d_vertex_declaration_decref(decl);
442 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
444 if ((texture = state->textures[i]))
446 state->textures[i] = NULL;
447 wined3d_texture_decref(texture);
451 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
453 if ((buffer = state->stream_output[i].buffer))
455 state->stream_output[i].buffer = NULL;
456 wined3d_buffer_decref(buffer);
460 for (i = 0; i < MAX_STREAMS; ++i)
462 if ((buffer = state->streams[i].buffer))
464 state->streams[i].buffer = NULL;
465 wined3d_buffer_decref(buffer);
469 if ((buffer = state->index_buffer))
471 state->index_buffer = NULL;
472 wined3d_buffer_decref(buffer);
475 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
477 if ((shader = state->shader[i]))
479 state->shader[i] = NULL;
480 wined3d_shader_decref(shader);
483 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
485 if ((buffer = state->cb[i][j]))
487 state->cb[i][j] = NULL;
488 wined3d_buffer_decref(buffer);
492 for (j = 0; j < MAX_SAMPLER_OBJECTS; ++j)
494 if ((sampler = state->sampler[i][j]))
496 state->sampler[i][j] = NULL;
497 wined3d_sampler_decref(sampler);
501 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
503 if ((srv = state->shader_resource_view[i][j]))
505 state->shader_resource_view[i][j] = NULL;
506 wined3d_shader_resource_view_decref(srv);
511 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
513 for (j = 0; j < MAX_UNORDERED_ACCESS_VIEWS; ++j)
515 if ((uav = state->unordered_access_view[i][j]))
517 state->unordered_access_view[i][j] = NULL;
518 wined3d_unordered_access_view_decref(uav);
524 void state_cleanup(struct wined3d_state *state)
526 unsigned int counter;
528 if (!(state->flags & WINED3D_STATE_NO_REF))
529 state_unbind_resources(state);
531 for (counter = 0; counter < MAX_ACTIVE_LIGHTS; ++counter)
533 state->lights[counter] = NULL;
536 for (counter = 0; counter < LIGHTMAP_SIZE; ++counter)
538 struct list *e1, *e2;
539 LIST_FOR_EACH_SAFE(e1, e2, &state->light_map[counter])
541 struct wined3d_light_info *light = LIST_ENTRY(e1, struct wined3d_light_info, entry);
542 list_remove(&light->entry);
543 heap_free(light);
548 ULONG CDECL wined3d_stateblock_decref(struct wined3d_stateblock *stateblock)
550 ULONG refcount = InterlockedDecrement(&stateblock->ref);
552 TRACE("%p decreasing refcount to %u\n", stateblock, refcount);
554 if (!refcount)
556 state_cleanup(&stateblock->state);
557 heap_free(stateblock);
560 return refcount;
563 struct wined3d_light_info *wined3d_state_get_light(const struct wined3d_state *state, unsigned int idx)
565 struct wined3d_light_info *light_info;
566 unsigned int hash_idx;
568 hash_idx = LIGHTMAP_HASHFUNC(idx);
569 LIST_FOR_EACH_ENTRY(light_info, &state->light_map[hash_idx], struct wined3d_light_info, entry)
571 if (light_info->OriginalIndex == idx)
572 return light_info;
575 return NULL;
578 void wined3d_state_enable_light(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info,
579 struct wined3d_light_info *light_info, BOOL enable)
581 unsigned int light_count, i;
583 if (!(light_info->enabled = enable))
585 if (light_info->glIndex == -1)
587 TRACE("Light already disabled, nothing to do.\n");
588 return;
591 state->lights[light_info->glIndex] = NULL;
592 light_info->glIndex = -1;
593 return;
596 if (light_info->glIndex != -1)
598 TRACE("Light already enabled, nothing to do.\n");
599 return;
602 /* Find a free light. */
603 light_count = d3d_info->limits.active_light_count;
604 for (i = 0; i < light_count; ++i)
606 if (state->lights[i])
607 continue;
609 state->lights[i] = light_info;
610 light_info->glIndex = i;
611 return;
614 /* Our tests show that Windows returns D3D_OK in this situation, even with
615 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices.
616 * This is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns
617 * TRUE * as well for those lights.
619 * TODO: Test how this affects rendering. */
620 WARN("Too many concurrently active lights.\n");
623 static void wined3d_state_record_lights(struct wined3d_state *dst_state, const struct wined3d_state *src_state)
625 UINT i;
627 /* Lights... For a recorded state block, we just had a chain of actions
628 * to perform, so we need to walk that chain and update any actions which
629 * differ. */
630 for (i = 0; i < LIGHTMAP_SIZE; ++i)
632 struct list *e, *f;
633 LIST_FOR_EACH(e, &dst_state->light_map[i])
635 BOOL updated = FALSE;
636 struct wined3d_light_info *src = LIST_ENTRY(e, struct wined3d_light_info, entry), *realLight;
638 /* Look up the light in the destination */
639 LIST_FOR_EACH(f, &src_state->light_map[i])
641 realLight = LIST_ENTRY(f, struct wined3d_light_info, entry);
642 if (realLight->OriginalIndex == src->OriginalIndex)
644 src->OriginalParms = realLight->OriginalParms;
646 if (realLight->glIndex == -1 && src->glIndex != -1)
648 /* Light disabled */
649 dst_state->lights[src->glIndex] = NULL;
651 else if (realLight->glIndex != -1 && src->glIndex == -1)
653 /* Light enabled */
654 dst_state->lights[realLight->glIndex] = src;
656 src->glIndex = realLight->glIndex;
657 updated = TRUE;
658 break;
662 if (!updated)
664 /* This can happen if the light was originally created as a
665 * default light for SetLightEnable() while recording. */
666 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
667 src->OriginalIndex, dst_state, src_state);
669 src->OriginalParms = WINED3D_default_light;
670 if (src->glIndex != -1)
672 dst_state->lights[src->glIndex] = NULL;
673 src->glIndex = -1;
680 void CDECL wined3d_stateblock_capture(struct wined3d_stateblock *stateblock)
682 const struct wined3d_state *src_state = &stateblock->device->state;
683 unsigned int i;
684 DWORD map;
686 TRACE("stateblock %p.\n", stateblock);
688 TRACE("Capturing state %p.\n", src_state);
690 if (stateblock->changed.vertexShader && stateblock->state.shader[WINED3D_SHADER_TYPE_VERTEX]
691 != src_state->shader[WINED3D_SHADER_TYPE_VERTEX])
693 TRACE("Updating vertex shader from %p to %p\n",
694 stateblock->state.shader[WINED3D_SHADER_TYPE_VERTEX],
695 src_state->shader[WINED3D_SHADER_TYPE_VERTEX]);
697 if (src_state->shader[WINED3D_SHADER_TYPE_VERTEX])
698 wined3d_shader_incref(src_state->shader[WINED3D_SHADER_TYPE_VERTEX]);
699 if (stateblock->state.shader[WINED3D_SHADER_TYPE_VERTEX])
700 wined3d_shader_decref(stateblock->state.shader[WINED3D_SHADER_TYPE_VERTEX]);
701 stateblock->state.shader[WINED3D_SHADER_TYPE_VERTEX] = src_state->shader[WINED3D_SHADER_TYPE_VERTEX];
704 /* Vertex shader float constants. */
705 for (i = 0; i < stateblock->num_contained_vs_consts_f; ++i)
707 unsigned int idx = stateblock->contained_vs_consts_f[i];
709 TRACE("Setting vs_consts_f[%u] to %s.\n", idx, debug_vec4(&src_state->vs_consts_f[idx]));
711 stateblock->state.vs_consts_f[idx] = src_state->vs_consts_f[idx];
714 /* Vertex shader integer constants. */
715 for (i = 0; i < stateblock->num_contained_vs_consts_i; ++i)
717 unsigned int idx = stateblock->contained_vs_consts_i[i];
719 TRACE("Setting vs_consts[%u] to %s.\n", idx, debug_ivec4(&src_state->vs_consts_i[idx]));
721 stateblock->state.vs_consts_i[idx] = src_state->vs_consts_i[idx];
724 /* Vertex shader boolean constants. */
725 for (i = 0; i < stateblock->num_contained_vs_consts_b; ++i)
727 unsigned int idx = stateblock->contained_vs_consts_b[i];
729 TRACE("Setting vs_consts_b[%u] to %s.\n",
730 idx, src_state->vs_consts_b[idx] ? "TRUE" : "FALSE");
732 stateblock->state.vs_consts_b[idx] = src_state->vs_consts_b[idx];
735 /* Pixel shader float constants. */
736 for (i = 0; i < stateblock->num_contained_ps_consts_f; ++i)
738 unsigned int idx = stateblock->contained_ps_consts_f[i];
740 TRACE("Setting ps_consts_f[%u] to %s.\n", idx, debug_vec4(&src_state->ps_consts_f[idx]));
742 stateblock->state.ps_consts_f[idx] = src_state->ps_consts_f[idx];
745 /* Pixel shader integer constants. */
746 for (i = 0; i < stateblock->num_contained_ps_consts_i; ++i)
748 unsigned int idx = stateblock->contained_ps_consts_i[i];
750 TRACE("Setting ps_consts_i[%u] to %s.\n", idx, debug_ivec4(&src_state->ps_consts_i[idx]));
752 stateblock->state.ps_consts_i[idx] = src_state->ps_consts_i[idx];
755 /* Pixel shader boolean constants. */
756 for (i = 0; i < stateblock->num_contained_ps_consts_b; ++i)
758 unsigned int idx = stateblock->contained_ps_consts_b[i];
759 TRACE("Setting ps_consts_b[%u] to %s.\n",
760 idx, src_state->ps_consts_b[idx] ? "TRUE" : "FALSE");
762 stateblock->state.ps_consts_b[idx] = src_state->ps_consts_b[idx];
765 /* Others + Render & Texture */
766 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
768 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
770 TRACE("Updating transform %#x.\n", transform);
772 stateblock->state.transforms[transform] = src_state->transforms[transform];
775 if (stateblock->changed.indices
776 && ((stateblock->state.index_buffer != src_state->index_buffer)
777 || (stateblock->state.base_vertex_index != src_state->base_vertex_index)
778 || (stateblock->state.index_format != src_state->index_format)
779 || (stateblock->state.index_offset != src_state->index_offset)))
781 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
782 src_state->index_buffer, src_state->base_vertex_index);
784 if (src_state->index_buffer)
785 wined3d_buffer_incref(src_state->index_buffer);
786 if (stateblock->state.index_buffer)
787 wined3d_buffer_decref(stateblock->state.index_buffer);
788 stateblock->state.index_buffer = src_state->index_buffer;
789 stateblock->state.base_vertex_index = src_state->base_vertex_index;
790 stateblock->state.index_format = src_state->index_format;
791 stateblock->state.index_offset = src_state->index_offset;
794 if (stateblock->changed.vertexDecl && stateblock->state.vertex_declaration != src_state->vertex_declaration)
796 TRACE("Updating vertex declaration from %p to %p.\n",
797 stateblock->state.vertex_declaration, src_state->vertex_declaration);
799 if (src_state->vertex_declaration)
800 wined3d_vertex_declaration_incref(src_state->vertex_declaration);
801 if (stateblock->state.vertex_declaration)
802 wined3d_vertex_declaration_decref(stateblock->state.vertex_declaration);
803 stateblock->state.vertex_declaration = src_state->vertex_declaration;
806 if (stateblock->changed.material
807 && memcmp(&src_state->material, &stateblock->state.material, sizeof(stateblock->state.material)))
809 TRACE("Updating material.\n");
811 stateblock->state.material = src_state->material;
814 assert(src_state->viewport_count <= 1);
816 if (stateblock->changed.viewport
817 && (src_state->viewport_count != stateblock->state.viewport_count
818 || memcmp(src_state->viewports, stateblock->state.viewports,
819 src_state->viewport_count * sizeof(*stateblock->state.viewports))))
821 TRACE("Updating viewports.\n");
823 if ((stateblock->state.viewport_count = src_state->viewport_count))
824 memcpy(stateblock->state.viewports, src_state->viewports, sizeof(src_state->viewports));
825 else
826 memset(stateblock->state.viewports, 0, sizeof(*stateblock->state.viewports));
829 if (stateblock->changed.scissorRect
830 && (src_state->scissor_rect_count != stateblock->state.scissor_rect_count
831 || memcmp(src_state->scissor_rects, stateblock->state.scissor_rects,
832 src_state->scissor_rect_count * sizeof(*stateblock->state.scissor_rects))))
834 TRACE("Updating scissor rects.\n");
836 if ((stateblock->state.scissor_rect_count = src_state->scissor_rect_count))
837 memcpy(stateblock->state.scissor_rects, src_state->scissor_rects,
838 src_state->scissor_rect_count * sizeof(*src_state->scissor_rects));
839 else
840 SetRectEmpty(stateblock->state.scissor_rects);
843 if (stateblock->changed.blend_state
844 && (src_state->blend_state != stateblock->state.blend_state
845 || memcmp(&src_state->blend_factor, &stateblock->state.blend_factor,
846 sizeof(stateblock->state.blend_factor))))
848 TRACE("Updating blend state.\n");
850 if (src_state->blend_state)
851 wined3d_blend_state_incref(src_state->blend_state);
852 if (stateblock->state.blend_state)
853 wined3d_blend_state_decref(stateblock->state.blend_state);
854 stateblock->state.blend_state = src_state->blend_state;
855 stateblock->state.blend_factor = src_state->blend_factor;
858 map = stateblock->changed.streamSource;
859 for (i = 0; map; map >>= 1, ++i)
861 if (!(map & 1)) continue;
863 if (stateblock->state.streams[i].stride != src_state->streams[i].stride
864 || stateblock->state.streams[i].buffer != src_state->streams[i].buffer)
866 TRACE("Updating stream source %u to %p, stride to %u.\n",
867 i, src_state->streams[i].buffer,
868 src_state->streams[i].stride);
870 stateblock->state.streams[i].stride = src_state->streams[i].stride;
871 if (src_state->streams[i].buffer)
872 wined3d_buffer_incref(src_state->streams[i].buffer);
873 if (stateblock->state.streams[i].buffer)
874 wined3d_buffer_decref(stateblock->state.streams[i].buffer);
875 stateblock->state.streams[i].buffer = src_state->streams[i].buffer;
879 map = stateblock->changed.streamFreq;
880 for (i = 0; map; map >>= 1, ++i)
882 if (!(map & 1)) continue;
884 if (stateblock->state.streams[i].frequency != src_state->streams[i].frequency
885 || stateblock->state.streams[i].flags != src_state->streams[i].flags)
887 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
888 i, src_state->streams[i].frequency, src_state->streams[i].flags);
890 stateblock->state.streams[i].frequency = src_state->streams[i].frequency;
891 stateblock->state.streams[i].flags = src_state->streams[i].flags;
895 map = stateblock->changed.clipplane;
896 for (i = 0; map; map >>= 1, ++i)
898 if (!(map & 1)) continue;
900 if (memcmp(&stateblock->state.clip_planes[i], &src_state->clip_planes[i], sizeof(src_state->clip_planes[i])))
902 TRACE("Updating clipplane %u.\n", i);
903 stateblock->state.clip_planes[i] = src_state->clip_planes[i];
907 /* Render */
908 for (i = 0; i < stateblock->num_contained_render_states; ++i)
910 enum wined3d_render_state rs = stateblock->contained_render_states[i];
912 TRACE("Updating render state %#x to %u.\n", rs, src_state->render_states[rs]);
914 stateblock->state.render_states[rs] = src_state->render_states[rs];
917 /* Texture states */
918 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
920 DWORD stage = stateblock->contained_tss_states[i].stage;
921 DWORD state = stateblock->contained_tss_states[i].state;
923 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage, state,
924 src_state->texture_states[stage][state], stateblock->state.texture_states[stage][state]);
926 stateblock->state.texture_states[stage][state] = src_state->texture_states[stage][state];
929 /* Samplers */
930 map = stateblock->changed.textures;
931 for (i = 0; map; map >>= 1, ++i)
933 if (!(map & 1)) continue;
935 TRACE("Updating texture %u to %p (was %p).\n",
936 i, src_state->textures[i], stateblock->state.textures[i]);
938 if (src_state->textures[i])
939 wined3d_texture_incref(src_state->textures[i]);
940 if (stateblock->state.textures[i])
941 wined3d_texture_decref(stateblock->state.textures[i]);
942 stateblock->state.textures[i] = src_state->textures[i];
945 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
947 DWORD stage = stateblock->contained_sampler_states[i].stage;
948 DWORD state = stateblock->contained_sampler_states[i].state;
950 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage, state,
951 src_state->sampler_states[stage][state], stateblock->state.sampler_states[stage][state]);
953 stateblock->state.sampler_states[stage][state] = src_state->sampler_states[stage][state];
956 if (stateblock->changed.pixelShader && stateblock->state.shader[WINED3D_SHADER_TYPE_PIXEL]
957 != src_state->shader[WINED3D_SHADER_TYPE_PIXEL])
959 if (src_state->shader[WINED3D_SHADER_TYPE_PIXEL])
960 wined3d_shader_incref(src_state->shader[WINED3D_SHADER_TYPE_PIXEL]);
961 if (stateblock->state.shader[WINED3D_SHADER_TYPE_PIXEL])
962 wined3d_shader_decref(stateblock->state.shader[WINED3D_SHADER_TYPE_PIXEL]);
963 stateblock->state.shader[WINED3D_SHADER_TYPE_PIXEL] = src_state->shader[WINED3D_SHADER_TYPE_PIXEL];
966 wined3d_state_record_lights(&stateblock->state, src_state);
968 TRACE("Capture done.\n");
971 static void apply_lights(struct wined3d_device *device, const struct wined3d_state *state)
973 UINT i;
975 for (i = 0; i < LIGHTMAP_SIZE; ++i)
977 struct list *e;
979 LIST_FOR_EACH(e, &state->light_map[i])
981 const struct wined3d_light_info *light = LIST_ENTRY(e, struct wined3d_light_info, entry);
983 wined3d_device_set_light(device, light->OriginalIndex, &light->OriginalParms);
984 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
989 void CDECL wined3d_stateblock_apply(const struct wined3d_stateblock *stateblock)
991 struct wined3d_device *device = stateblock->device;
992 unsigned int i;
993 DWORD map;
995 TRACE("Applying stateblock %p to device %p.\n", stateblock, device);
997 if (stateblock->changed.vertexShader)
998 wined3d_device_set_vertex_shader(device, stateblock->state.shader[WINED3D_SHADER_TYPE_VERTEX]);
1000 /* Vertex Shader Constants. */
1001 for (i = 0; i < stateblock->num_contained_vs_consts_f; ++i)
1003 wined3d_device_set_vs_consts_f(device, stateblock->contained_vs_consts_f[i],
1004 1, &stateblock->state.vs_consts_f[stateblock->contained_vs_consts_f[i]]);
1006 for (i = 0; i < stateblock->num_contained_vs_consts_i; ++i)
1008 wined3d_device_set_vs_consts_i(device, stateblock->contained_vs_consts_i[i],
1009 1, &stateblock->state.vs_consts_i[stateblock->contained_vs_consts_i[i]]);
1011 for (i = 0; i < stateblock->num_contained_vs_consts_b; ++i)
1013 wined3d_device_set_vs_consts_b(device, stateblock->contained_vs_consts_b[i],
1014 1, &stateblock->state.vs_consts_b[stateblock->contained_vs_consts_b[i]]);
1017 apply_lights(device, &stateblock->state);
1019 if (stateblock->changed.pixelShader)
1020 wined3d_device_set_pixel_shader(device, stateblock->state.shader[WINED3D_SHADER_TYPE_PIXEL]);
1022 /* Pixel Shader Constants. */
1023 for (i = 0; i < stateblock->num_contained_ps_consts_f; ++i)
1025 wined3d_device_set_ps_consts_f(device, stateblock->contained_ps_consts_f[i],
1026 1, &stateblock->state.ps_consts_f[stateblock->contained_ps_consts_f[i]]);
1028 for (i = 0; i < stateblock->num_contained_ps_consts_i; ++i)
1030 wined3d_device_set_ps_consts_i(device, stateblock->contained_ps_consts_i[i],
1031 1, &stateblock->state.ps_consts_i[stateblock->contained_ps_consts_i[i]]);
1033 for (i = 0; i < stateblock->num_contained_ps_consts_b; ++i)
1035 wined3d_device_set_ps_consts_b(device, stateblock->contained_ps_consts_b[i],
1036 1, &stateblock->state.ps_consts_b[stateblock->contained_ps_consts_b[i]]);
1039 /* Render states. */
1040 for (i = 0; i < stateblock->num_contained_render_states; ++i)
1042 wined3d_device_set_render_state(device, stateblock->contained_render_states[i],
1043 stateblock->state.render_states[stateblock->contained_render_states[i]]);
1046 /* Texture states. */
1047 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
1049 DWORD stage = stateblock->contained_tss_states[i].stage;
1050 DWORD state = stateblock->contained_tss_states[i].state;
1052 wined3d_device_set_texture_stage_state(device, stage, state, stateblock->state.texture_states[stage][state]);
1055 /* Sampler states. */
1056 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
1058 DWORD stage = stateblock->contained_sampler_states[i].stage;
1059 DWORD state = stateblock->contained_sampler_states[i].state;
1060 DWORD value = stateblock->state.sampler_states[stage][state];
1062 if (stage >= MAX_FRAGMENT_SAMPLERS) stage += WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS;
1063 wined3d_device_set_sampler_state(device, stage, state, value);
1066 /* Transform states. */
1067 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
1069 wined3d_device_set_transform(device, stateblock->contained_transform_states[i],
1070 &stateblock->state.transforms[stateblock->contained_transform_states[i]]);
1073 if (stateblock->changed.indices)
1075 wined3d_device_set_index_buffer(device, stateblock->state.index_buffer,
1076 stateblock->state.index_format, stateblock->state.index_offset);
1077 wined3d_device_set_base_vertex_index(device, stateblock->state.base_vertex_index);
1080 if (stateblock->changed.vertexDecl && stateblock->state.vertex_declaration)
1081 wined3d_device_set_vertex_declaration(device, stateblock->state.vertex_declaration);
1083 if (stateblock->changed.material)
1084 wined3d_device_set_material(device, &stateblock->state.material);
1086 if (stateblock->changed.viewport)
1087 wined3d_device_set_viewports(device, stateblock->state.viewport_count, stateblock->state.viewports);
1089 if (stateblock->changed.scissorRect)
1090 wined3d_device_set_scissor_rects(device, stateblock->state.scissor_rect_count,
1091 stateblock->state.scissor_rects);
1093 if (stateblock->changed.blend_state)
1094 wined3d_device_set_blend_state(device, stateblock->state.blend_state, &stateblock->state.blend_factor);
1096 map = stateblock->changed.streamSource;
1097 for (i = 0; map; map >>= 1, ++i)
1099 if (map & 1)
1100 wined3d_device_set_stream_source(device, i,
1101 stateblock->state.streams[i].buffer,
1102 0, stateblock->state.streams[i].stride);
1105 map = stateblock->changed.streamFreq;
1106 for (i = 0; map; map >>= 1, ++i)
1108 if (map & 1)
1109 wined3d_device_set_stream_source_freq(device, i,
1110 stateblock->state.streams[i].frequency | stateblock->state.streams[i].flags);
1113 map = stateblock->changed.textures;
1114 for (i = 0; map; map >>= 1, ++i)
1116 DWORD stage;
1118 if (!(map & 1)) continue;
1120 stage = i < MAX_FRAGMENT_SAMPLERS ? i : WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS;
1121 wined3d_device_set_texture(device, stage, stateblock->state.textures[i]);
1124 map = stateblock->changed.clipplane;
1125 for (i = 0; map; map >>= 1, ++i)
1127 if (!(map & 1)) continue;
1129 wined3d_device_set_clip_plane(device, i, &stateblock->state.clip_planes[i]);
1132 TRACE("Applied stateblock %p.\n", stateblock);
1135 static void state_init_default(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info)
1137 union
1139 struct wined3d_line_pattern lp;
1140 DWORD d;
1141 } lp;
1142 union
1144 float f;
1145 DWORD d;
1146 } tmpfloat;
1147 unsigned int i;
1148 struct wined3d_matrix identity;
1150 TRACE("state %p, d3d_info %p.\n", state, d3d_info);
1152 get_identity_matrix(&identity);
1153 state->gl_primitive_type = ~0u;
1154 state->gl_patch_vertices = 0;
1156 /* Set some of the defaults for lights, transforms etc */
1157 state->transforms[WINED3D_TS_PROJECTION] = identity;
1158 state->transforms[WINED3D_TS_VIEW] = identity;
1159 for (i = 0; i < 256; ++i)
1161 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
1164 TRACE("Render states\n");
1165 /* Render states: */
1166 state->render_states[WINED3D_RS_ZENABLE] = WINED3D_ZB_TRUE;
1167 state->render_states[WINED3D_RS_FILLMODE] = WINED3D_FILL_SOLID;
1168 state->render_states[WINED3D_RS_SHADEMODE] = WINED3D_SHADE_GOURAUD;
1169 lp.lp.repeat_factor = 0;
1170 lp.lp.line_pattern = 0;
1171 state->render_states[WINED3D_RS_LINEPATTERN] = lp.d;
1172 state->render_states[WINED3D_RS_ZWRITEENABLE] = TRUE;
1173 state->render_states[WINED3D_RS_ALPHATESTENABLE] = FALSE;
1174 state->render_states[WINED3D_RS_LASTPIXEL] = TRUE;
1175 state->render_states[WINED3D_RS_SRCBLEND] = WINED3D_BLEND_ONE;
1176 state->render_states[WINED3D_RS_DESTBLEND] = WINED3D_BLEND_ZERO;
1177 state->render_states[WINED3D_RS_CULLMODE] = WINED3D_CULL_BACK;
1178 state->render_states[WINED3D_RS_ZFUNC] = WINED3D_CMP_LESSEQUAL;
1179 state->render_states[WINED3D_RS_ALPHAFUNC] = WINED3D_CMP_ALWAYS;
1180 state->render_states[WINED3D_RS_ALPHAREF] = 0;
1181 state->render_states[WINED3D_RS_DITHERENABLE] = FALSE;
1182 state->render_states[WINED3D_RS_ALPHABLENDENABLE] = FALSE;
1183 state->render_states[WINED3D_RS_FOGENABLE] = FALSE;
1184 state->render_states[WINED3D_RS_SPECULARENABLE] = FALSE;
1185 state->render_states[WINED3D_RS_ZVISIBLE] = 0;
1186 state->render_states[WINED3D_RS_FOGCOLOR] = 0;
1187 state->render_states[WINED3D_RS_FOGTABLEMODE] = WINED3D_FOG_NONE;
1188 tmpfloat.f = 0.0f;
1189 state->render_states[WINED3D_RS_FOGSTART] = tmpfloat.d;
1190 tmpfloat.f = 1.0f;
1191 state->render_states[WINED3D_RS_FOGEND] = tmpfloat.d;
1192 tmpfloat.f = 1.0f;
1193 state->render_states[WINED3D_RS_FOGDENSITY] = tmpfloat.d;
1194 state->render_states[WINED3D_RS_EDGEANTIALIAS] = FALSE;
1195 state->render_states[WINED3D_RS_RANGEFOGENABLE] = FALSE;
1196 state->render_states[WINED3D_RS_STENCILENABLE] = FALSE;
1197 state->render_states[WINED3D_RS_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1198 state->render_states[WINED3D_RS_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1199 state->render_states[WINED3D_RS_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1200 state->render_states[WINED3D_RS_STENCILREF] = 0;
1201 state->render_states[WINED3D_RS_STENCILMASK] = 0xffffffff;
1202 state->render_states[WINED3D_RS_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1203 state->render_states[WINED3D_RS_STENCILWRITEMASK] = 0xffffffff;
1204 state->render_states[WINED3D_RS_TEXTUREFACTOR] = 0xffffffff;
1205 state->render_states[WINED3D_RS_WRAP0] = 0;
1206 state->render_states[WINED3D_RS_WRAP1] = 0;
1207 state->render_states[WINED3D_RS_WRAP2] = 0;
1208 state->render_states[WINED3D_RS_WRAP3] = 0;
1209 state->render_states[WINED3D_RS_WRAP4] = 0;
1210 state->render_states[WINED3D_RS_WRAP5] = 0;
1211 state->render_states[WINED3D_RS_WRAP6] = 0;
1212 state->render_states[WINED3D_RS_WRAP7] = 0;
1213 state->render_states[WINED3D_RS_CLIPPING] = TRUE;
1214 state->render_states[WINED3D_RS_LIGHTING] = TRUE;
1215 state->render_states[WINED3D_RS_AMBIENT] = 0;
1216 state->render_states[WINED3D_RS_FOGVERTEXMODE] = WINED3D_FOG_NONE;
1217 state->render_states[WINED3D_RS_COLORVERTEX] = TRUE;
1218 state->render_states[WINED3D_RS_LOCALVIEWER] = TRUE;
1219 state->render_states[WINED3D_RS_NORMALIZENORMALS] = FALSE;
1220 state->render_states[WINED3D_RS_DIFFUSEMATERIALSOURCE] = WINED3D_MCS_COLOR1;
1221 state->render_states[WINED3D_RS_SPECULARMATERIALSOURCE] = WINED3D_MCS_COLOR2;
1222 state->render_states[WINED3D_RS_AMBIENTMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1223 state->render_states[WINED3D_RS_EMISSIVEMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1224 state->render_states[WINED3D_RS_VERTEXBLEND] = WINED3D_VBF_DISABLE;
1225 state->render_states[WINED3D_RS_CLIPPLANEENABLE] = 0;
1226 state->render_states[WINED3D_RS_SOFTWAREVERTEXPROCESSING] = FALSE;
1227 tmpfloat.f = 1.0f;
1228 state->render_states[WINED3D_RS_POINTSIZE] = tmpfloat.d;
1229 tmpfloat.f = 1.0f;
1230 state->render_states[WINED3D_RS_POINTSIZE_MIN] = tmpfloat.d;
1231 state->render_states[WINED3D_RS_POINTSPRITEENABLE] = FALSE;
1232 state->render_states[WINED3D_RS_POINTSCALEENABLE] = FALSE;
1233 tmpfloat.f = 1.0f;
1234 state->render_states[WINED3D_RS_POINTSCALE_A] = tmpfloat.d;
1235 tmpfloat.f = 0.0f;
1236 state->render_states[WINED3D_RS_POINTSCALE_B] = tmpfloat.d;
1237 tmpfloat.f = 0.0f;
1238 state->render_states[WINED3D_RS_POINTSCALE_C] = tmpfloat.d;
1239 state->render_states[WINED3D_RS_MULTISAMPLEANTIALIAS] = TRUE;
1240 state->render_states[WINED3D_RS_MULTISAMPLEMASK] = 0xffffffff;
1241 state->render_states[WINED3D_RS_PATCHEDGESTYLE] = WINED3D_PATCH_EDGE_DISCRETE;
1242 tmpfloat.f = 1.0f;
1243 state->render_states[WINED3D_RS_PATCHSEGMENTS] = tmpfloat.d;
1244 state->render_states[WINED3D_RS_DEBUGMONITORTOKEN] = 0xbaadcafe;
1245 tmpfloat.f = d3d_info->limits.pointsize_max;
1246 state->render_states[WINED3D_RS_POINTSIZE_MAX] = tmpfloat.d;
1247 state->render_states[WINED3D_RS_INDEXEDVERTEXBLENDENABLE] = FALSE;
1248 state->render_states[WINED3D_RS_COLORWRITEENABLE] = 0x0000000f;
1249 tmpfloat.f = 0.0f;
1250 state->render_states[WINED3D_RS_TWEENFACTOR] = tmpfloat.d;
1251 state->render_states[WINED3D_RS_BLENDOP] = WINED3D_BLEND_OP_ADD;
1252 state->render_states[WINED3D_RS_POSITIONDEGREE] = WINED3D_DEGREE_CUBIC;
1253 state->render_states[WINED3D_RS_NORMALDEGREE] = WINED3D_DEGREE_LINEAR;
1254 /* states new in d3d9 */
1255 state->render_states[WINED3D_RS_SCISSORTESTENABLE] = FALSE;
1256 state->render_states[WINED3D_RS_SLOPESCALEDEPTHBIAS] = 0;
1257 tmpfloat.f = 1.0f;
1258 state->render_states[WINED3D_RS_MINTESSELLATIONLEVEL] = tmpfloat.d;
1259 state->render_states[WINED3D_RS_MAXTESSELLATIONLEVEL] = tmpfloat.d;
1260 state->render_states[WINED3D_RS_ANTIALIASEDLINEENABLE] = FALSE;
1261 tmpfloat.f = 0.0f;
1262 state->render_states[WINED3D_RS_ADAPTIVETESS_X] = tmpfloat.d;
1263 state->render_states[WINED3D_RS_ADAPTIVETESS_Y] = tmpfloat.d;
1264 tmpfloat.f = 1.0f;
1265 state->render_states[WINED3D_RS_ADAPTIVETESS_Z] = tmpfloat.d;
1266 tmpfloat.f = 0.0f;
1267 state->render_states[WINED3D_RS_ADAPTIVETESS_W] = tmpfloat.d;
1268 state->render_states[WINED3D_RS_ENABLEADAPTIVETESSELLATION] = FALSE;
1269 state->render_states[WINED3D_RS_TWOSIDEDSTENCILMODE] = FALSE;
1270 state->render_states[WINED3D_RS_BACK_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1271 state->render_states[WINED3D_RS_BACK_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1272 state->render_states[WINED3D_RS_BACK_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1273 state->render_states[WINED3D_RS_BACK_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1274 state->render_states[WINED3D_RS_COLORWRITEENABLE1] = 0x0000000f;
1275 state->render_states[WINED3D_RS_COLORWRITEENABLE2] = 0x0000000f;
1276 state->render_states[WINED3D_RS_COLORWRITEENABLE3] = 0x0000000f;
1277 state->render_states[WINED3D_RS_SRGBWRITEENABLE] = 0;
1278 state->render_states[WINED3D_RS_DEPTHBIAS] = 0;
1279 state->render_states[WINED3D_RS_WRAP8] = 0;
1280 state->render_states[WINED3D_RS_WRAP9] = 0;
1281 state->render_states[WINED3D_RS_WRAP10] = 0;
1282 state->render_states[WINED3D_RS_WRAP11] = 0;
1283 state->render_states[WINED3D_RS_WRAP12] = 0;
1284 state->render_states[WINED3D_RS_WRAP13] = 0;
1285 state->render_states[WINED3D_RS_WRAP14] = 0;
1286 state->render_states[WINED3D_RS_WRAP15] = 0;
1287 state->render_states[WINED3D_RS_SEPARATEALPHABLENDENABLE] = FALSE;
1288 state->render_states[WINED3D_RS_SRCBLENDALPHA] = WINED3D_BLEND_ONE;
1289 state->render_states[WINED3D_RS_DESTBLENDALPHA] = WINED3D_BLEND_ZERO;
1290 state->render_states[WINED3D_RS_BLENDOPALPHA] = WINED3D_BLEND_OP_ADD;
1292 /* Texture Stage States - Put directly into state block, we will call function below */
1293 for (i = 0; i < MAX_TEXTURES; ++i)
1295 TRACE("Setting up default texture states for texture Stage %u.\n", i);
1296 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
1297 state->texture_states[i][WINED3D_TSS_COLOR_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_MODULATE;
1298 state->texture_states[i][WINED3D_TSS_COLOR_ARG1] = WINED3DTA_TEXTURE;
1299 state->texture_states[i][WINED3D_TSS_COLOR_ARG2] = WINED3DTA_CURRENT;
1300 state->texture_states[i][WINED3D_TSS_ALPHA_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_SELECT_ARG1;
1301 state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] = WINED3DTA_TEXTURE;
1302 state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] = WINED3DTA_CURRENT;
1303 state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00] = 0;
1304 state->texture_states[i][WINED3D_TSS_BUMPENV_MAT01] = 0;
1305 state->texture_states[i][WINED3D_TSS_BUMPENV_MAT10] = 0;
1306 state->texture_states[i][WINED3D_TSS_BUMPENV_MAT11] = 0;
1307 state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] = i;
1308 state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE] = 0;
1309 state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET] = 0;
1310 state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS] = WINED3D_TTFF_DISABLE;
1311 state->texture_states[i][WINED3D_TSS_COLOR_ARG0] = WINED3DTA_CURRENT;
1312 state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] = WINED3DTA_CURRENT;
1313 state->texture_states[i][WINED3D_TSS_RESULT_ARG] = WINED3DTA_CURRENT;
1316 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; ++i)
1318 TRACE("Setting up default samplers states for sampler %u.\n", i);
1319 state->sampler_states[i][WINED3D_SAMP_ADDRESS_U] = WINED3D_TADDRESS_WRAP;
1320 state->sampler_states[i][WINED3D_SAMP_ADDRESS_V] = WINED3D_TADDRESS_WRAP;
1321 state->sampler_states[i][WINED3D_SAMP_ADDRESS_W] = WINED3D_TADDRESS_WRAP;
1322 state->sampler_states[i][WINED3D_SAMP_BORDER_COLOR] = 0;
1323 state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] = WINED3D_TEXF_POINT;
1324 state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] = WINED3D_TEXF_POINT;
1325 state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] = WINED3D_TEXF_NONE;
1326 state->sampler_states[i][WINED3D_SAMP_MIPMAP_LOD_BIAS] = 0;
1327 state->sampler_states[i][WINED3D_SAMP_MAX_MIP_LEVEL] = 0;
1328 state->sampler_states[i][WINED3D_SAMP_MAX_ANISOTROPY] = 1;
1329 state->sampler_states[i][WINED3D_SAMP_SRGB_TEXTURE] = 0;
1330 /* TODO: Indicates which element of a multielement texture to use. */
1331 state->sampler_states[i][WINED3D_SAMP_ELEMENT_INDEX] = 0;
1332 /* TODO: Vertex offset in the presampled displacement map. */
1333 state->sampler_states[i][WINED3D_SAMP_DMAP_OFFSET] = 0;
1336 state->blend_factor.r = 1.0f;
1337 state->blend_factor.g = 1.0f;
1338 state->blend_factor.b = 1.0f;
1339 state->blend_factor.a = 1.0f;
1342 void state_init(struct wined3d_state *state, struct wined3d_fb_state *fb,
1343 const struct wined3d_d3d_info *d3d_info, DWORD flags)
1345 unsigned int i;
1347 state->flags = flags;
1348 state->fb = fb;
1350 for (i = 0; i < LIGHTMAP_SIZE; i++)
1352 list_init(&state->light_map[i]);
1355 if (flags & WINED3D_STATE_INIT_DEFAULT)
1356 state_init_default(state, d3d_info);
1359 static HRESULT stateblock_init(struct wined3d_stateblock *stateblock,
1360 struct wined3d_device *device, enum wined3d_stateblock_type type)
1362 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
1364 stateblock->ref = 1;
1365 stateblock->device = device;
1366 state_init(&stateblock->state, NULL, d3d_info, 0);
1368 if (type == WINED3D_SBT_RECORDED)
1369 return WINED3D_OK;
1371 TRACE("Updating changed flags appropriate for type %#x.\n", type);
1373 switch (type)
1375 case WINED3D_SBT_ALL:
1376 stateblock_init_lights(stateblock, device->state.light_map);
1377 stateblock_savedstates_set_all(&stateblock->changed,
1378 d3d_info->limits.vs_uniform_count, d3d_info->limits.ps_uniform_count);
1379 break;
1381 case WINED3D_SBT_PIXEL_STATE:
1382 stateblock_savedstates_set_pixel(&stateblock->changed,
1383 d3d_info->limits.ps_uniform_count);
1384 break;
1386 case WINED3D_SBT_VERTEX_STATE:
1387 stateblock_init_lights(stateblock, device->state.light_map);
1388 stateblock_savedstates_set_vertex(&stateblock->changed,
1389 d3d_info->limits.vs_uniform_count);
1390 break;
1392 default:
1393 FIXME("Unrecognized state block type %#x.\n", type);
1394 break;
1397 stateblock_init_contained_states(stateblock);
1398 wined3d_stateblock_capture(stateblock);
1400 return WINED3D_OK;
1403 HRESULT CDECL wined3d_stateblock_create(struct wined3d_device *device,
1404 enum wined3d_stateblock_type type, struct wined3d_stateblock **stateblock)
1406 struct wined3d_stateblock *object;
1407 HRESULT hr;
1409 TRACE("device %p, type %#x, stateblock %p.\n",
1410 device, type, stateblock);
1412 if (!(object = heap_alloc_zero(sizeof(*object))))
1413 return E_OUTOFMEMORY;
1415 hr = stateblock_init(object, device, type);
1416 if (FAILED(hr))
1418 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1419 heap_free(object);
1420 return hr;
1423 TRACE("Created stateblock %p.\n", object);
1424 *stateblock = object;
1426 return WINED3D_OK;