mf/tests: Test input type for WMA decoder DMO.
[wine.git] / dlls / wined3d / stateblock.c
blob3d6edbfa7f3bb0c67021d25dd04c4463ff57e651
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 "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(winediag);
30 static const DWORD pixel_states_render[] =
32 WINED3D_RS_ALPHABLENDENABLE,
33 WINED3D_RS_ALPHAFUNC,
34 WINED3D_RS_ALPHAREF,
35 WINED3D_RS_ALPHATESTENABLE,
36 WINED3D_RS_ANTIALIASEDLINEENABLE,
37 WINED3D_RS_BLENDFACTOR,
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_all_bits(uint32_t *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 states->indices = 1;
204 states->material = 1;
205 states->viewport = 1;
206 states->vertexDecl = 1;
207 states->pixelShader = 1;
208 states->vertexShader = 1;
209 states->scissorRect = 1;
210 states->alpha_to_coverage = 1;
211 states->lights = 1;
212 states->transforms = 1;
214 states->streamSource = 0xffff;
215 states->streamFreq = 0xffff;
216 states->textures = 0xfffff;
217 stateblock_set_all_bits(states->transform, WINED3D_HIGHEST_TRANSFORM_STATE + 1);
218 stateblock_set_all_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
219 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
220 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3ffe;
221 states->clipplane = wined3d_mask_from_size(WINED3D_MAX_CLIP_DISTANCES);
222 states->pixelShaderConstantsB = 0xffff;
223 states->pixelShaderConstantsI = 0xffff;
224 states->vertexShaderConstantsB = 0xffff;
225 states->vertexShaderConstantsI = 0xffff;
227 memset(states->ps_consts_f, 0xffu, sizeof(states->ps_consts_f));
228 memset(states->vs_consts_f, 0xffu, sizeof(states->vs_consts_f));
231 static void stateblock_savedstates_set_pixel(struct wined3d_saved_states *states, const DWORD num_constants)
233 DWORD texture_mask = 0;
234 WORD sampler_mask = 0;
235 unsigned int i;
237 states->pixelShader = 1;
239 for (i = 0; i < ARRAY_SIZE(pixel_states_render); ++i)
241 DWORD rs = pixel_states_render[i];
242 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
245 for (i = 0; i < ARRAY_SIZE(pixel_states_texture); ++i)
246 texture_mask |= 1u << pixel_states_texture[i];
247 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
248 for (i = 0; i < ARRAY_SIZE(pixel_states_sampler); ++i)
249 sampler_mask |= 1u << pixel_states_sampler[i];
250 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
251 states->pixelShaderConstantsB = 0xffff;
252 states->pixelShaderConstantsI = 0xffff;
254 memset(states->ps_consts_f, 0xffu, sizeof(states->ps_consts_f));
257 static void stateblock_savedstates_set_vertex(struct wined3d_saved_states *states, const DWORD num_constants)
259 DWORD texture_mask = 0;
260 WORD sampler_mask = 0;
261 unsigned int i;
263 states->vertexDecl = 1;
264 states->vertexShader = 1;
265 states->alpha_to_coverage = 1;
266 states->lights = 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 < WINED3D_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 < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
280 states->vertexShaderConstantsB = 0xffff;
281 states->vertexShaderConstantsI = 0xffff;
283 memset(states->vs_consts_f, 0xffu, sizeof(states->vs_consts_f));
286 void CDECL wined3d_stateblock_init_contained_states(struct wined3d_stateblock *stateblock)
288 unsigned int i, j;
290 for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
292 DWORD map = stateblock->changed.renderState[i];
293 for (j = 0; map; map >>= 1, ++j)
295 if (!(map & 1)) continue;
297 stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
298 ++stateblock->num_contained_render_states;
302 for (i = 0; i <= WINED3D_HIGHEST_TRANSFORM_STATE >> 5; ++i)
304 DWORD map = stateblock->changed.transform[i];
305 for (j = 0; map; map >>= 1, ++j)
307 if (!(map & 1)) continue;
309 stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
310 ++stateblock->num_contained_transform_states;
314 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
316 DWORD map = stateblock->changed.textureState[i];
318 for(j = 0; map; map >>= 1, ++j)
320 if (!(map & 1)) continue;
322 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
323 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
324 ++stateblock->num_contained_tss_states;
328 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
330 DWORD map = stateblock->changed.samplerState[i];
332 for (j = 0; map; map >>= 1, ++j)
334 if (!(map & 1)) continue;
336 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
337 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
338 ++stateblock->num_contained_sampler_states;
343 static void stateblock_init_lights(struct wined3d_stateblock *stateblock, const struct rb_tree *src_tree)
345 struct rb_tree *dst_tree = &stateblock->stateblock_state.light_state->lights_tree;
346 struct wined3d_light_info *src_light;
348 RB_FOR_EACH_ENTRY(src_light, src_tree, struct wined3d_light_info, entry)
350 struct wined3d_light_info *dst_light = heap_alloc(sizeof(*dst_light));
352 *dst_light = *src_light;
353 rb_put(dst_tree, (void *)(ULONG_PTR)dst_light->OriginalIndex, &dst_light->entry);
354 dst_light->changed = true;
355 list_add_tail(&stateblock->changed.changed_lights, &dst_light->changed_entry);
359 ULONG CDECL wined3d_stateblock_incref(struct wined3d_stateblock *stateblock)
361 unsigned int refcount = InterlockedIncrement(&stateblock->ref);
363 TRACE("%p increasing refcount to %u.\n", stateblock, refcount);
365 return refcount;
368 void state_unbind_resources(struct wined3d_state *state)
370 struct wined3d_unordered_access_view *uav;
371 struct wined3d_shader_resource_view *srv;
372 struct wined3d_vertex_declaration *decl;
373 struct wined3d_blend_state *blend_state;
374 struct wined3d_rendertarget_view *rtv;
375 struct wined3d_sampler *sampler;
376 struct wined3d_texture *texture;
377 struct wined3d_buffer *buffer;
378 struct wined3d_shader *shader;
379 unsigned int i, j;
381 if ((decl = state->vertex_declaration))
383 state->vertex_declaration = NULL;
384 wined3d_vertex_declaration_decref(decl);
387 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
389 if ((texture = state->textures[i]))
391 state->textures[i] = NULL;
392 wined3d_texture_decref(texture);
396 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
398 if ((buffer = state->stream_output[i].buffer))
400 state->stream_output[i].buffer = NULL;
401 wined3d_buffer_decref(buffer);
405 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
407 if ((buffer = state->streams[i].buffer))
409 state->streams[i].buffer = NULL;
410 wined3d_buffer_decref(buffer);
414 if ((buffer = state->index_buffer))
416 state->index_buffer = NULL;
417 wined3d_buffer_decref(buffer);
420 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
422 if ((shader = state->shader[i]))
424 state->shader[i] = NULL;
425 wined3d_shader_decref(shader);
428 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
430 if ((buffer = state->cb[i][j].buffer))
432 state->cb[i][j].buffer = NULL;
433 wined3d_buffer_decref(buffer);
437 for (j = 0; j < MAX_SAMPLER_OBJECTS; ++j)
439 if ((sampler = state->sampler[i][j]))
441 state->sampler[i][j] = NULL;
442 wined3d_sampler_decref(sampler);
446 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
448 if ((srv = state->shader_resource_view[i][j]))
450 state->shader_resource_view[i][j] = NULL;
451 wined3d_srv_bind_count_dec(srv);
452 wined3d_shader_resource_view_decref(srv);
457 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
459 for (j = 0; j < MAX_UNORDERED_ACCESS_VIEWS; ++j)
461 if ((uav = state->unordered_access_view[i][j]))
463 state->unordered_access_view[i][j] = NULL;
464 wined3d_unordered_access_view_decref(uav);
469 if ((blend_state = state->blend_state))
471 state->blend_state = NULL;
472 wined3d_blend_state_decref(blend_state);
475 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
477 if ((rtv = state->fb.render_targets[i]))
479 state->fb.render_targets[i] = NULL;
480 wined3d_rendertarget_view_decref(rtv);
484 if ((rtv = state->fb.depth_stencil))
486 state->fb.depth_stencil = NULL;
487 wined3d_rendertarget_view_decref(rtv);
491 void wined3d_stateblock_state_cleanup(struct wined3d_stateblock_state *state)
493 struct wined3d_light_info *light, *cursor;
494 struct wined3d_vertex_declaration *decl;
495 struct wined3d_texture *texture;
496 struct wined3d_buffer *buffer;
497 struct wined3d_shader *shader;
498 unsigned int i;
500 if ((decl = state->vertex_declaration))
502 state->vertex_declaration = NULL;
503 wined3d_vertex_declaration_decref(decl);
506 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
508 if ((buffer = state->streams[i].buffer))
510 state->streams[i].buffer = NULL;
511 wined3d_buffer_decref(buffer);
515 if ((buffer = state->index_buffer))
517 state->index_buffer = NULL;
518 wined3d_buffer_decref(buffer);
521 if ((shader = state->vs))
523 state->vs = NULL;
524 wined3d_shader_decref(shader);
527 if ((shader = state->ps))
529 state->ps = NULL;
530 wined3d_shader_decref(shader);
533 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
535 if ((texture = state->textures[i]))
537 state->textures[i] = NULL;
538 wined3d_texture_decref(texture);
542 RB_FOR_EACH_ENTRY_DESTRUCTOR(light, cursor, &state->light_state->lights_tree, struct wined3d_light_info, entry)
544 if (light->changed)
545 list_remove(&light->changed_entry);
546 rb_remove(&state->light_state->lights_tree, &light->entry);
547 heap_free(light);
551 void state_cleanup(struct wined3d_state *state)
553 struct wined3d_light_info *light, *cursor;
554 unsigned int i;
556 if (!(state->flags & WINED3D_STATE_NO_REF))
557 state_unbind_resources(state);
559 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
561 state->light_state.lights[i] = NULL;
564 RB_FOR_EACH_ENTRY_DESTRUCTOR(light, cursor, &state->light_state.lights_tree, struct wined3d_light_info, entry)
566 if (light->changed)
567 list_remove(&light->changed_entry);
568 rb_remove(&state->light_state.lights_tree, &light->entry);
569 heap_free(light);
573 ULONG CDECL wined3d_stateblock_decref(struct wined3d_stateblock *stateblock)
575 unsigned int refcount = InterlockedDecrement(&stateblock->ref);
577 TRACE("%p decreasing refcount to %u\n", stateblock, refcount);
579 if (!refcount)
581 wined3d_mutex_lock();
582 wined3d_stateblock_state_cleanup(&stateblock->stateblock_state);
583 heap_free(stateblock);
584 wined3d_mutex_unlock();
587 return refcount;
590 struct wined3d_light_info *wined3d_light_state_get_light(const struct wined3d_light_state *state, unsigned int idx)
592 struct rb_entry *entry;
594 if (!(entry = rb_get(&state->lights_tree, (void *)(ULONG_PTR)idx)))
595 return NULL;
597 return RB_ENTRY_VALUE(entry, struct wined3d_light_info, entry);
600 static void set_light_changed(struct wined3d_stateblock *stateblock, struct wined3d_light_info *light_info)
602 if (!light_info->changed)
604 list_add_tail(&stateblock->changed.changed_lights, &light_info->changed_entry);
605 light_info->changed = true;
607 stateblock->changed.lights = 1;
610 HRESULT wined3d_light_state_set_light(struct wined3d_light_state *state, DWORD light_idx,
611 const struct wined3d_light *params, struct wined3d_light_info **light_info)
613 struct wined3d_light_info *object;
615 if (!(object = wined3d_light_state_get_light(state, light_idx)))
617 TRACE("Adding new light.\n");
618 if (!(object = heap_alloc_zero(sizeof(*object))))
620 ERR("Failed to allocate light info.\n");
621 return E_OUTOFMEMORY;
624 object->glIndex = -1;
625 object->OriginalIndex = light_idx;
626 rb_put(&state->lights_tree, (void *)(ULONG_PTR)light_idx, &object->entry);
629 object->OriginalParms = *params;
631 *light_info = object;
632 return WINED3D_OK;
635 bool wined3d_light_state_enable_light(struct wined3d_light_state *state, const struct wined3d_d3d_info *d3d_info,
636 struct wined3d_light_info *light_info, BOOL enable)
638 unsigned int light_count, i;
640 if (!(light_info->enabled = enable))
642 if (light_info->glIndex == -1)
644 TRACE("Light already disabled, nothing to do.\n");
645 return false;
648 state->lights[light_info->glIndex] = NULL;
649 light_info->glIndex = -1;
650 return true;
653 if (light_info->glIndex != -1)
655 TRACE("Light already enabled, nothing to do.\n");
656 return false;
659 /* Find a free light. */
660 light_count = d3d_info->limits.active_light_count;
661 for (i = 0; i < light_count; ++i)
663 if (state->lights[i])
664 continue;
666 state->lights[i] = light_info;
667 light_info->glIndex = i;
668 return true;
671 /* Our tests show that Windows returns D3D_OK in this situation, even with
672 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices.
673 * This is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns
674 * TRUE * as well for those lights.
676 * TODO: Test how this affects rendering. */
677 WARN("Too many concurrently active lights.\n");
678 return false;
681 static void wined3d_state_record_lights(struct wined3d_light_state *dst_state,
682 const struct wined3d_light_state *src_state)
684 const struct wined3d_light_info *src;
685 struct wined3d_light_info *dst;
687 /* Lights... For a recorded state block, we just had a chain of actions
688 * to perform, so we need to walk that chain and update any actions which
689 * differ. */
690 RB_FOR_EACH_ENTRY(dst, &dst_state->lights_tree, struct wined3d_light_info, entry)
692 if ((src = wined3d_light_state_get_light(src_state, dst->OriginalIndex)))
694 dst->OriginalParms = src->OriginalParms;
696 if (src->glIndex == -1 && dst->glIndex != -1)
698 /* Light disabled. */
699 dst_state->lights[dst->glIndex] = NULL;
701 else if (src->glIndex != -1 && dst->glIndex == -1)
703 /* Light enabled. */
704 dst_state->lights[src->glIndex] = dst;
706 dst->glIndex = src->glIndex;
708 else
710 /* This can happen if the light was originally created as a
711 * default light for SetLightEnable() while recording. */
712 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
713 dst->OriginalIndex, dst_state, src_state);
715 dst->OriginalParms = WINED3D_default_light;
716 if (dst->glIndex != -1)
718 dst_state->lights[dst->glIndex] = NULL;
719 dst->glIndex = -1;
725 void CDECL wined3d_stateblock_capture(struct wined3d_stateblock *stateblock,
726 const struct wined3d_stateblock *device_state)
728 const struct wined3d_stateblock_state *state = &device_state->stateblock_state;
729 struct wined3d_range range;
730 unsigned int i, start;
731 uint32_t map;
733 TRACE("stateblock %p, device_state %p.\n", stateblock, device_state);
735 if (stateblock->changed.vertexShader && stateblock->stateblock_state.vs != state->vs)
737 TRACE("Updating vertex shader from %p to %p.\n", stateblock->stateblock_state.vs, state->vs);
739 if (state->vs)
740 wined3d_shader_incref(state->vs);
741 if (stateblock->stateblock_state.vs)
742 wined3d_shader_decref(stateblock->stateblock_state.vs);
743 stateblock->stateblock_state.vs = state->vs;
746 for (start = 0; ; start = range.offset + range.size)
748 if (!wined3d_bitmap_get_range(stateblock->changed.vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
749 break;
751 memcpy(&stateblock->stateblock_state.vs_consts_f[range.offset], &state->vs_consts_f[range.offset],
752 sizeof(*state->vs_consts_f) * range.size);
754 map = stateblock->changed.vertexShaderConstantsI;
755 for (start = 0; ; start = range.offset + range.size)
757 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
758 break;
760 memcpy(&stateblock->stateblock_state.vs_consts_i[range.offset], &state->vs_consts_i[range.offset],
761 sizeof(*state->vs_consts_i) * range.size);
763 map = stateblock->changed.vertexShaderConstantsB;
764 for (start = 0; ; start = range.offset + range.size)
766 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
767 break;
769 memcpy(&stateblock->stateblock_state.vs_consts_b[range.offset], &state->vs_consts_b[range.offset],
770 sizeof(*state->vs_consts_b) * range.size);
773 for (start = 0; ; start = range.offset + range.size)
775 if (!wined3d_bitmap_get_range(stateblock->changed.ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
776 break;
778 memcpy(&stateblock->stateblock_state.ps_consts_f[range.offset], &state->ps_consts_f[range.offset],
779 sizeof(*state->ps_consts_f) * range.size);
781 map = stateblock->changed.pixelShaderConstantsI;
782 for (start = 0; ; start = range.offset + range.size)
784 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
785 break;
787 memcpy(&stateblock->stateblock_state.ps_consts_i[range.offset], &state->ps_consts_i[range.offset],
788 sizeof(*state->ps_consts_i) * range.size);
790 map = stateblock->changed.pixelShaderConstantsB;
791 for (start = 0; ; start = range.offset + range.size)
793 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
794 break;
796 memcpy(&stateblock->stateblock_state.ps_consts_b[range.offset], &state->ps_consts_b[range.offset],
797 sizeof(*state->ps_consts_b) * range.size);
800 if (stateblock->changed.transforms)
802 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
804 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
806 TRACE("Updating transform %#x.\n", transform);
808 stateblock->stateblock_state.transforms[transform] = state->transforms[transform];
812 if (stateblock->changed.indices
813 && ((stateblock->stateblock_state.index_buffer != state->index_buffer)
814 || (stateblock->stateblock_state.base_vertex_index != state->base_vertex_index)
815 || (stateblock->stateblock_state.index_format != state->index_format)))
817 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
818 state->index_buffer, state->base_vertex_index);
820 if (state->index_buffer)
821 wined3d_buffer_incref(state->index_buffer);
822 if (stateblock->stateblock_state.index_buffer)
823 wined3d_buffer_decref(stateblock->stateblock_state.index_buffer);
824 stateblock->stateblock_state.index_buffer = state->index_buffer;
825 stateblock->stateblock_state.base_vertex_index = state->base_vertex_index;
826 stateblock->stateblock_state.index_format = state->index_format;
829 if (stateblock->changed.vertexDecl && stateblock->stateblock_state.vertex_declaration != state->vertex_declaration)
831 TRACE("Updating vertex declaration from %p to %p.\n",
832 stateblock->stateblock_state.vertex_declaration, state->vertex_declaration);
834 if (state->vertex_declaration)
835 wined3d_vertex_declaration_incref(state->vertex_declaration);
836 if (stateblock->stateblock_state.vertex_declaration)
837 wined3d_vertex_declaration_decref(stateblock->stateblock_state.vertex_declaration);
838 stateblock->stateblock_state.vertex_declaration = state->vertex_declaration;
841 if (stateblock->changed.material
842 && memcmp(&state->material, &stateblock->stateblock_state.material,
843 sizeof(stateblock->stateblock_state.material)))
845 TRACE("Updating material.\n");
847 stateblock->stateblock_state.material = state->material;
850 if (stateblock->changed.viewport
851 && memcmp(&state->viewport, &stateblock->stateblock_state.viewport, sizeof(state->viewport)))
853 TRACE("Updating viewport.\n");
855 stateblock->stateblock_state.viewport = state->viewport;
858 if (stateblock->changed.scissorRect
859 && memcmp(&state->scissor_rect, &stateblock->stateblock_state.scissor_rect, sizeof(state->scissor_rect)))
861 TRACE("Updating scissor rect.\n");
863 stateblock->stateblock_state.scissor_rect = state->scissor_rect;
866 map = stateblock->changed.streamSource;
867 while (map)
869 i = wined3d_bit_scan(&map);
871 if (stateblock->stateblock_state.streams[i].stride != state->streams[i].stride
872 || stateblock->stateblock_state.streams[i].offset != state->streams[i].offset
873 || stateblock->stateblock_state.streams[i].buffer != state->streams[i].buffer)
875 TRACE("stateblock %p, stream source %u, buffer %p, stride %u, offset %u.\n",
876 stateblock, i, state->streams[i].buffer, state->streams[i].stride,
877 state->streams[i].offset);
879 stateblock->stateblock_state.streams[i].stride = state->streams[i].stride;
880 if (stateblock->changed.store_stream_offset)
881 stateblock->stateblock_state.streams[i].offset = state->streams[i].offset;
883 if (state->streams[i].buffer)
884 wined3d_buffer_incref(state->streams[i].buffer);
885 if (stateblock->stateblock_state.streams[i].buffer)
886 wined3d_buffer_decref(stateblock->stateblock_state.streams[i].buffer);
887 stateblock->stateblock_state.streams[i].buffer = state->streams[i].buffer;
891 map = stateblock->changed.streamFreq;
892 while (map)
894 i = wined3d_bit_scan(&map);
896 if (stateblock->stateblock_state.streams[i].frequency != state->streams[i].frequency
897 || stateblock->stateblock_state.streams[i].flags != state->streams[i].flags)
899 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
900 i, state->streams[i].frequency, state->streams[i].flags);
902 stateblock->stateblock_state.streams[i].frequency = state->streams[i].frequency;
903 stateblock->stateblock_state.streams[i].flags = state->streams[i].flags;
907 map = stateblock->changed.clipplane;
908 while (map)
910 i = wined3d_bit_scan(&map);
912 if (memcmp(&stateblock->stateblock_state.clip_planes[i], &state->clip_planes[i], sizeof(state->clip_planes[i])))
914 TRACE("Updating clipplane %u.\n", i);
915 stateblock->stateblock_state.clip_planes[i] = state->clip_planes[i];
919 /* Render */
920 for (i = 0; i < stateblock->num_contained_render_states; ++i)
922 enum wined3d_render_state rs = stateblock->contained_render_states[i];
924 TRACE("Updating render state %#x to %u.\n", rs, state->rs[rs]);
926 stateblock->stateblock_state.rs[rs] = state->rs[rs];
929 /* Texture states */
930 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
932 unsigned int stage = stateblock->contained_tss_states[i].stage;
933 unsigned int texture_state = stateblock->contained_tss_states[i].state;
935 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage, texture_state,
936 state->texture_states[stage][texture_state],
937 stateblock->stateblock_state.texture_states[stage][texture_state]);
939 stateblock->stateblock_state.texture_states[stage][texture_state] = state->texture_states[stage][texture_state];
942 /* Samplers */
943 map = stateblock->changed.textures;
944 while (map)
946 i = wined3d_bit_scan(&map);
948 TRACE("Updating texture %u to %p (was %p).\n",
949 i, state->textures[i], stateblock->stateblock_state.textures[i]);
951 if (state->textures[i])
952 wined3d_texture_incref(state->textures[i]);
953 if (stateblock->stateblock_state.textures[i])
954 wined3d_texture_decref(stateblock->stateblock_state.textures[i]);
955 stateblock->stateblock_state.textures[i] = state->textures[i];
958 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
960 unsigned int stage = stateblock->contained_sampler_states[i].stage;
961 unsigned int sampler_state = stateblock->contained_sampler_states[i].state;
963 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage, sampler_state,
964 state->sampler_states[stage][sampler_state],
965 stateblock->stateblock_state.sampler_states[stage][sampler_state]);
967 stateblock->stateblock_state.sampler_states[stage][sampler_state] = state->sampler_states[stage][sampler_state];
970 if (stateblock->changed.pixelShader && stateblock->stateblock_state.ps != state->ps)
972 if (state->ps)
973 wined3d_shader_incref(state->ps);
974 if (stateblock->stateblock_state.ps)
975 wined3d_shader_decref(stateblock->stateblock_state.ps);
976 stateblock->stateblock_state.ps = state->ps;
979 if (stateblock->changed.lights)
980 wined3d_state_record_lights(stateblock->stateblock_state.light_state, state->light_state);
982 if (stateblock->changed.alpha_to_coverage)
983 stateblock->stateblock_state.alpha_to_coverage = state->alpha_to_coverage;
985 TRACE("Capture done.\n");
988 void CDECL wined3d_stateblock_apply(const struct wined3d_stateblock *stateblock,
989 struct wined3d_stateblock *device_state)
991 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
992 struct wined3d_range range;
993 unsigned int i, start;
994 uint32_t map;
996 TRACE("stateblock %p, device_state %p.\n", stateblock, device_state);
998 if (stateblock->changed.vertexShader)
999 wined3d_stateblock_set_vertex_shader(device_state, state->vs);
1000 if (stateblock->changed.pixelShader)
1001 wined3d_stateblock_set_pixel_shader(device_state, state->ps);
1003 for (start = 0; ; start = range.offset + range.size)
1005 if (!wined3d_bitmap_get_range(stateblock->changed.vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
1006 break;
1007 wined3d_stateblock_set_vs_consts_f(device_state, range.offset, range.size, &state->vs_consts_f[range.offset]);
1009 map = stateblock->changed.vertexShaderConstantsI;
1010 for (start = 0; ; start = range.offset + range.size)
1012 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
1013 break;
1014 wined3d_stateblock_set_vs_consts_i(device_state, range.offset, range.size, &state->vs_consts_i[range.offset]);
1016 map = stateblock->changed.vertexShaderConstantsB;
1017 for (start = 0; ; start = range.offset + range.size)
1019 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
1020 break;
1021 wined3d_stateblock_set_vs_consts_b(device_state, range.offset, range.size, &state->vs_consts_b[range.offset]);
1024 for (start = 0; ; start = range.offset + range.size)
1026 if (!wined3d_bitmap_get_range(stateblock->changed.ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
1027 break;
1028 wined3d_stateblock_set_ps_consts_f(device_state, range.offset, range.size, &state->ps_consts_f[range.offset]);
1030 map = stateblock->changed.pixelShaderConstantsI;
1031 for (start = 0; ; start = range.offset + range.size)
1033 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
1034 break;
1035 wined3d_stateblock_set_ps_consts_i(device_state, range.offset, range.size, &state->ps_consts_i[range.offset]);
1037 map = stateblock->changed.pixelShaderConstantsB;
1038 for (start = 0; ; start = range.offset + range.size)
1040 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
1041 break;
1042 wined3d_stateblock_set_ps_consts_b(device_state, range.offset, range.size, &state->ps_consts_b[range.offset]);
1045 if (stateblock->changed.transforms)
1047 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
1049 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
1051 wined3d_stateblock_set_transform(device_state, transform, &state->transforms[transform]);
1055 if (stateblock->changed.lights)
1057 const struct wined3d_light_info *light;
1059 LIST_FOR_EACH_ENTRY(light, &stateblock->changed.changed_lights, struct wined3d_light_info, changed_entry)
1061 wined3d_stateblock_set_light(device_state, light->OriginalIndex, &light->OriginalParms);
1062 wined3d_stateblock_set_light_enable(device_state, light->OriginalIndex, light->glIndex != -1);
1066 if (stateblock->changed.alpha_to_coverage)
1068 device_state->stateblock_state.alpha_to_coverage = state->alpha_to_coverage;
1069 device_state->changed.alpha_to_coverage = 1;
1072 /* Render states. */
1073 for (i = 0; i < stateblock->num_contained_render_states; ++i)
1075 enum wined3d_render_state rs = stateblock->contained_render_states[i];
1077 wined3d_stateblock_set_render_state(device_state, rs, state->rs[rs]);
1080 /* Texture states. */
1081 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
1083 DWORD stage = stateblock->contained_tss_states[i].stage;
1084 DWORD texture_state = stateblock->contained_tss_states[i].state;
1086 wined3d_stateblock_set_texture_stage_state(device_state, stage, texture_state,
1087 state->texture_states[stage][texture_state]);
1090 /* Sampler states. */
1091 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
1093 DWORD stage = stateblock->contained_sampler_states[i].stage;
1094 DWORD sampler_state = stateblock->contained_sampler_states[i].state;
1096 wined3d_stateblock_set_sampler_state(device_state, stage, sampler_state,
1097 state->sampler_states[stage][sampler_state]);
1100 if (stateblock->changed.indices)
1102 wined3d_stateblock_set_index_buffer(device_state, state->index_buffer, state->index_format);
1103 wined3d_stateblock_set_base_vertex_index(device_state, state->base_vertex_index);
1106 if (stateblock->changed.vertexDecl && state->vertex_declaration)
1107 wined3d_stateblock_set_vertex_declaration(device_state, state->vertex_declaration);
1109 if (stateblock->changed.material)
1110 wined3d_stateblock_set_material(device_state, &state->material);
1112 if (stateblock->changed.viewport)
1113 wined3d_stateblock_set_viewport(device_state, &state->viewport);
1115 if (stateblock->changed.scissorRect)
1116 wined3d_stateblock_set_scissor_rect(device_state, &state->scissor_rect);
1118 map = stateblock->changed.streamSource;
1119 while (map)
1121 i = wined3d_bit_scan(&map);
1122 wined3d_stateblock_set_stream_source(device_state, i, state->streams[i].buffer,
1123 state->streams[i].offset, state->streams[i].stride);
1126 map = stateblock->changed.streamFreq;
1127 while (map)
1129 i = wined3d_bit_scan(&map);
1130 wined3d_stateblock_set_stream_source_freq(device_state, i,
1131 state->streams[i].frequency | state->streams[i].flags);
1134 map = stateblock->changed.textures;
1135 while (map)
1137 i = wined3d_bit_scan(&map);
1138 wined3d_stateblock_set_texture(device_state, i, state->textures[i]);
1141 map = stateblock->changed.clipplane;
1142 while (map)
1144 i = wined3d_bit_scan(&map);
1145 wined3d_stateblock_set_clip_plane(device_state, i, &state->clip_planes[i]);
1148 TRACE("Applied stateblock %p.\n", stateblock);
1151 void CDECL wined3d_stateblock_set_vertex_shader(struct wined3d_stateblock *stateblock, struct wined3d_shader *shader)
1153 TRACE("stateblock %p, shader %p.\n", stateblock, shader);
1155 if (shader)
1156 wined3d_shader_incref(shader);
1157 if (stateblock->stateblock_state.vs)
1158 wined3d_shader_decref(stateblock->stateblock_state.vs);
1159 stateblock->stateblock_state.vs = shader;
1160 stateblock->changed.vertexShader = TRUE;
1163 static void wined3d_bitmap_set_bits(uint32_t *bitmap, unsigned int start, unsigned int count)
1165 const unsigned int word_bit_count = sizeof(*bitmap) * CHAR_BIT;
1166 const unsigned int shift = start % word_bit_count;
1167 uint32_t mask, last_mask;
1168 unsigned int mask_size;
1170 bitmap += start / word_bit_count;
1171 mask = ~0u << shift;
1172 mask_size = word_bit_count - shift;
1173 last_mask = (1u << (start + count) % word_bit_count) - 1;
1174 if (mask_size <= count)
1176 *bitmap |= mask;
1177 ++bitmap;
1178 count -= mask_size;
1179 mask = ~0u;
1181 if (count >= word_bit_count)
1183 memset(bitmap, 0xffu, count / word_bit_count * sizeof(*bitmap));
1184 bitmap += count / word_bit_count;
1185 count = count % word_bit_count;
1187 if (count)
1188 *bitmap |= mask & last_mask;
1191 HRESULT CDECL wined3d_stateblock_set_vs_consts_f(struct wined3d_stateblock *stateblock,
1192 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
1194 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
1196 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1197 stateblock, start_idx, count, constants);
1199 if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.vs_uniform_count))
1200 return WINED3DERR_INVALIDCALL;
1202 memcpy(&stateblock->stateblock_state.vs_consts_f[start_idx], constants, count * sizeof(*constants));
1203 wined3d_bitmap_set_bits(stateblock->changed.vs_consts_f, start_idx, count);
1204 return WINED3D_OK;
1207 HRESULT CDECL wined3d_stateblock_set_vs_consts_i(struct wined3d_stateblock *stateblock,
1208 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
1210 unsigned int i;
1212 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1213 stateblock, start_idx, count, constants);
1215 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1216 return WINED3DERR_INVALIDCALL;
1218 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1219 count = WINED3D_MAX_CONSTS_I - start_idx;
1221 memcpy(&stateblock->stateblock_state.vs_consts_i[start_idx], constants, count * sizeof(*constants));
1222 for (i = start_idx; i < count + start_idx; ++i)
1223 stateblock->changed.vertexShaderConstantsI |= (1u << i);
1224 return WINED3D_OK;
1227 HRESULT CDECL wined3d_stateblock_set_vs_consts_b(struct wined3d_stateblock *stateblock,
1228 unsigned int start_idx, unsigned int count, const BOOL *constants)
1230 unsigned int i;
1232 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1233 stateblock, start_idx, count, constants);
1235 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1236 return WINED3DERR_INVALIDCALL;
1238 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1239 count = WINED3D_MAX_CONSTS_B - start_idx;
1241 memcpy(&stateblock->stateblock_state.vs_consts_b[start_idx], constants, count * sizeof(*constants));
1242 for (i = start_idx; i < count + start_idx; ++i)
1243 stateblock->changed.vertexShaderConstantsB |= (1u << i);
1244 return WINED3D_OK;
1247 void CDECL wined3d_stateblock_set_pixel_shader(struct wined3d_stateblock *stateblock, struct wined3d_shader *shader)
1249 TRACE("stateblock %p, shader %p.\n", stateblock, shader);
1251 if (shader)
1252 wined3d_shader_incref(shader);
1253 if (stateblock->stateblock_state.ps)
1254 wined3d_shader_decref(stateblock->stateblock_state.ps);
1255 stateblock->stateblock_state.ps = shader;
1256 stateblock->changed.pixelShader = TRUE;
1259 HRESULT CDECL wined3d_stateblock_set_ps_consts_f(struct wined3d_stateblock *stateblock,
1260 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
1262 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
1264 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1265 stateblock, start_idx, count, constants);
1267 if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.ps_uniform_count))
1268 return WINED3DERR_INVALIDCALL;
1270 memcpy(&stateblock->stateblock_state.ps_consts_f[start_idx], constants, count * sizeof(*constants));
1271 wined3d_bitmap_set_bits(stateblock->changed.ps_consts_f, start_idx, count);
1272 return WINED3D_OK;
1275 HRESULT CDECL wined3d_stateblock_set_ps_consts_i(struct wined3d_stateblock *stateblock,
1276 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
1278 unsigned int i;
1280 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1281 stateblock, start_idx, count, constants);
1283 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1284 return WINED3DERR_INVALIDCALL;
1286 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1287 count = WINED3D_MAX_CONSTS_I - start_idx;
1289 memcpy(&stateblock->stateblock_state.ps_consts_i[start_idx], constants, count * sizeof(*constants));
1290 for (i = start_idx; i < count + start_idx; ++i)
1291 stateblock->changed.pixelShaderConstantsI |= (1u << i);
1292 return WINED3D_OK;
1295 HRESULT CDECL wined3d_stateblock_set_ps_consts_b(struct wined3d_stateblock *stateblock,
1296 unsigned int start_idx, unsigned int count, const BOOL *constants)
1298 unsigned int i;
1300 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1301 stateblock, start_idx, count, constants);
1303 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1304 return WINED3DERR_INVALIDCALL;
1306 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1307 count = WINED3D_MAX_CONSTS_B - start_idx;
1309 memcpy(&stateblock->stateblock_state.ps_consts_b[start_idx], constants, count * sizeof(*constants));
1310 for (i = start_idx; i < count + start_idx; ++i)
1311 stateblock->changed.pixelShaderConstantsB |= (1u << i);
1312 return WINED3D_OK;
1315 void CDECL wined3d_stateblock_set_vertex_declaration(struct wined3d_stateblock *stateblock,
1316 struct wined3d_vertex_declaration *declaration)
1318 TRACE("stateblock %p, declaration %p.\n", stateblock, declaration);
1320 if (declaration)
1321 wined3d_vertex_declaration_incref(declaration);
1322 if (stateblock->stateblock_state.vertex_declaration)
1323 wined3d_vertex_declaration_decref(stateblock->stateblock_state.vertex_declaration);
1324 stateblock->stateblock_state.vertex_declaration = declaration;
1325 stateblock->changed.vertexDecl = TRUE;
1328 void CDECL wined3d_stateblock_set_render_state(struct wined3d_stateblock *stateblock,
1329 enum wined3d_render_state state, unsigned int value)
1331 TRACE("stateblock %p, state %s (%#x), value %#x.\n", stateblock, debug_d3drenderstate(state), state, value);
1333 if (state > WINEHIGHEST_RENDER_STATE)
1335 WARN("Unhandled render state %#x.\n", state);
1336 return;
1339 stateblock->stateblock_state.rs[state] = value;
1340 stateblock->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
1342 if (state == WINED3D_RS_POINTSIZE
1343 && (value == WINED3D_ALPHA_TO_COVERAGE_ENABLE || value == WINED3D_ALPHA_TO_COVERAGE_DISABLE))
1345 stateblock->changed.alpha_to_coverage = 1;
1346 stateblock->stateblock_state.alpha_to_coverage = (value == WINED3D_ALPHA_TO_COVERAGE_ENABLE);
1350 void CDECL wined3d_stateblock_set_sampler_state(struct wined3d_stateblock *stateblock,
1351 UINT sampler_idx, enum wined3d_sampler_state state, unsigned int value)
1353 TRACE("stateblock %p, sampler_idx %u, state %s, value %#x.\n",
1354 stateblock, sampler_idx, debug_d3dsamplerstate(state), value);
1356 if (sampler_idx >= ARRAY_SIZE(stateblock->stateblock_state.sampler_states))
1358 WARN("Invalid sampler %u.\n", sampler_idx);
1359 return;
1362 stateblock->stateblock_state.sampler_states[sampler_idx][state] = value;
1363 stateblock->changed.samplerState[sampler_idx] |= 1u << state;
1366 void CDECL wined3d_stateblock_set_texture_stage_state(struct wined3d_stateblock *stateblock,
1367 UINT stage, enum wined3d_texture_stage_state state, unsigned int value)
1369 TRACE("stateblock %p, stage %u, state %s, value %#x.\n",
1370 stateblock, stage, debug_d3dtexturestate(state), value);
1372 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
1374 WARN("Invalid state %#x passed.\n", state);
1375 return;
1378 if (stage >= WINED3D_MAX_TEXTURES)
1380 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
1381 stage, WINED3D_MAX_TEXTURES - 1);
1382 return;
1385 stateblock->stateblock_state.texture_states[stage][state] = value;
1386 stateblock->changed.textureState[stage] |= 1u << state;
1389 void CDECL wined3d_stateblock_set_texture(struct wined3d_stateblock *stateblock,
1390 UINT stage, struct wined3d_texture *texture)
1392 TRACE("stateblock %p, stage %u, texture %p.\n", stateblock, stage, texture);
1394 if (stage >= ARRAY_SIZE(stateblock->stateblock_state.textures))
1396 WARN("Ignoring invalid stage %u.\n", stage);
1397 return;
1400 if (texture)
1401 wined3d_texture_incref(texture);
1402 if (stateblock->stateblock_state.textures[stage])
1403 wined3d_texture_decref(stateblock->stateblock_state.textures[stage]);
1404 stateblock->stateblock_state.textures[stage] = texture;
1405 stateblock->changed.textures |= 1u << stage;
1408 void CDECL wined3d_stateblock_set_transform(struct wined3d_stateblock *stateblock,
1409 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1411 TRACE("stateblock %p, state %s, matrix %p.\n", stateblock, debug_d3dtstype(d3dts), matrix);
1412 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1413 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1414 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1415 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1417 stateblock->stateblock_state.transforms[d3dts] = *matrix;
1418 stateblock->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1419 stateblock->changed.transforms = 1;
1422 void CDECL wined3d_stateblock_multiply_transform(struct wined3d_stateblock *stateblock,
1423 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1425 struct wined3d_matrix *mat = &stateblock->stateblock_state.transforms[d3dts];
1427 TRACE("stateblock %p, state %s, matrix %p.\n", stateblock, debug_d3dtstype(d3dts), matrix);
1428 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1429 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1430 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1431 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1433 multiply_matrix(mat, mat, matrix);
1434 stateblock->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1435 stateblock->changed.transforms = 1;
1438 HRESULT CDECL wined3d_stateblock_set_clip_plane(struct wined3d_stateblock *stateblock,
1439 UINT plane_idx, const struct wined3d_vec4 *plane)
1441 TRACE("stateblock %p, plane_idx %u, plane %p.\n", stateblock, plane_idx, plane);
1443 if (plane_idx >= stateblock->device->adapter->d3d_info.limits.max_clip_distances)
1445 TRACE("Application has requested clipplane this device doesn't support.\n");
1446 return WINED3DERR_INVALIDCALL;
1449 stateblock->stateblock_state.clip_planes[plane_idx] = *plane;
1450 stateblock->changed.clipplane |= 1u << plane_idx;
1451 return S_OK;
1454 void CDECL wined3d_stateblock_set_material(struct wined3d_stateblock *stateblock,
1455 const struct wined3d_material *material)
1457 TRACE("stateblock %p, material %p.\n", stateblock, material);
1459 stateblock->stateblock_state.material = *material;
1460 stateblock->changed.material = TRUE;
1463 void CDECL wined3d_stateblock_set_viewport(struct wined3d_stateblock *stateblock,
1464 const struct wined3d_viewport *viewport)
1466 TRACE("stateblock %p, viewport %p.\n", stateblock, viewport);
1468 stateblock->stateblock_state.viewport = *viewport;
1469 stateblock->changed.viewport = TRUE;
1472 void CDECL wined3d_stateblock_set_scissor_rect(struct wined3d_stateblock *stateblock, const RECT *rect)
1474 TRACE("stateblock %p, rect %s.\n", stateblock, wine_dbgstr_rect(rect));
1476 stateblock->stateblock_state.scissor_rect = *rect;
1477 stateblock->changed.scissorRect = TRUE;
1480 void CDECL wined3d_stateblock_set_index_buffer(struct wined3d_stateblock *stateblock,
1481 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1483 TRACE("stateblock %p, buffer %p, format %s.\n", stateblock, buffer, debug_d3dformat(format_id));
1485 if (buffer)
1486 wined3d_buffer_incref(buffer);
1487 if (stateblock->stateblock_state.index_buffer)
1488 wined3d_buffer_decref(stateblock->stateblock_state.index_buffer);
1489 stateblock->stateblock_state.index_buffer = buffer;
1490 stateblock->stateblock_state.index_format = format_id;
1491 stateblock->changed.indices = TRUE;
1494 void CDECL wined3d_stateblock_set_base_vertex_index(struct wined3d_stateblock *stateblock, INT base_index)
1496 TRACE("stateblock %p, base_index %d.\n", stateblock, base_index);
1498 stateblock->stateblock_state.base_vertex_index = base_index;
1501 HRESULT CDECL wined3d_stateblock_set_stream_source(struct wined3d_stateblock *stateblock,
1502 UINT stream_idx, struct wined3d_buffer *buffer, UINT offset, UINT stride)
1504 struct wined3d_stream_state *stream;
1506 TRACE("stateblock %p, stream_idx %u, buffer %p, stride %u.\n",
1507 stateblock, stream_idx, buffer, stride);
1509 if (stream_idx >= WINED3D_MAX_STREAMS)
1511 WARN("Stream index %u out of range.\n", stream_idx);
1512 return WINED3DERR_INVALIDCALL;
1515 stream = &stateblock->stateblock_state.streams[stream_idx];
1517 if (buffer)
1518 wined3d_buffer_incref(buffer);
1519 if (stream->buffer)
1520 wined3d_buffer_decref(stream->buffer);
1521 stream->buffer = buffer;
1522 stream->stride = stride;
1523 stream->offset = offset;
1524 stateblock->changed.streamSource |= 1u << stream_idx;
1525 return WINED3D_OK;
1528 HRESULT CDECL wined3d_stateblock_set_stream_source_freq(struct wined3d_stateblock *stateblock,
1529 UINT stream_idx, UINT divider)
1531 struct wined3d_stream_state *stream;
1533 TRACE("stateblock %p, stream_idx %u, divider %#x.\n", stateblock, stream_idx, divider);
1535 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1537 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1538 return WINED3DERR_INVALIDCALL;
1540 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1542 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1543 return WINED3DERR_INVALIDCALL;
1545 if (!divider)
1547 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1548 return WINED3DERR_INVALIDCALL;
1551 stream = &stateblock->stateblock_state.streams[stream_idx];
1552 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1553 stream->frequency = divider & 0x7fffff;
1554 stateblock->changed.streamFreq |= 1u << stream_idx;
1555 return WINED3D_OK;
1558 HRESULT CDECL wined3d_stateblock_set_light(struct wined3d_stateblock *stateblock,
1559 UINT light_idx, const struct wined3d_light *light)
1561 struct wined3d_light_info *object = NULL;
1562 HRESULT hr;
1564 TRACE("stateblock %p, light_idx %u, light %p.\n", stateblock, light_idx, light);
1566 /* Check the parameter range. Need for speed most wanted sets junk lights
1567 * which confuse the GL driver. */
1568 if (!light)
1569 return WINED3DERR_INVALIDCALL;
1571 switch (light->type)
1573 case WINED3D_LIGHT_POINT:
1574 case WINED3D_LIGHT_SPOT:
1575 case WINED3D_LIGHT_GLSPOT:
1576 /* Incorrect attenuation values can cause the gl driver to crash.
1577 * Happens with Need for speed most wanted. */
1578 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1580 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1581 return WINED3DERR_INVALIDCALL;
1583 break;
1585 case WINED3D_LIGHT_DIRECTIONAL:
1586 case WINED3D_LIGHT_PARALLELPOINT:
1587 /* Ignores attenuation */
1588 break;
1590 default:
1591 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL.\n");
1592 return WINED3DERR_INVALIDCALL;
1595 if (SUCCEEDED(hr = wined3d_light_state_set_light(stateblock->stateblock_state.light_state, light_idx, light, &object)))
1596 set_light_changed(stateblock, object);
1597 return hr;
1600 HRESULT CDECL wined3d_stateblock_set_light_enable(struct wined3d_stateblock *stateblock, UINT light_idx, BOOL enable)
1602 struct wined3d_light_state *light_state = stateblock->stateblock_state.light_state;
1603 struct wined3d_light_info *light_info;
1604 HRESULT hr;
1606 TRACE("stateblock %p, light_idx %u, enable %#x.\n", stateblock, light_idx, enable);
1608 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1610 if (FAILED(hr = wined3d_light_state_set_light(light_state, light_idx, &WINED3D_default_light, &light_info)))
1611 return hr;
1612 set_light_changed(stateblock, light_info);
1615 if (wined3d_light_state_enable_light(light_state, &stateblock->device->adapter->d3d_info, light_info, enable))
1616 set_light_changed(stateblock, light_info);
1618 return S_OK;
1621 const struct wined3d_stateblock_state * CDECL wined3d_stateblock_get_state(const struct wined3d_stateblock *stateblock)
1623 return &stateblock->stateblock_state;
1626 HRESULT CDECL wined3d_stateblock_get_light(const struct wined3d_stateblock *stateblock,
1627 UINT light_idx, struct wined3d_light *light, BOOL *enabled)
1629 struct wined3d_light_info *light_info;
1631 if (!(light_info = wined3d_light_state_get_light(&stateblock->light_state, light_idx)))
1633 TRACE("Light %u is not defined.\n", light_idx);
1634 return WINED3DERR_INVALIDCALL;
1636 *light = light_info->OriginalParms;
1637 *enabled = light_info->enabled ? 128 : 0;
1638 return WINED3D_OK;
1641 static void init_default_render_states(unsigned int rs[WINEHIGHEST_RENDER_STATE + 1], const struct wined3d_d3d_info *d3d_info)
1643 union
1645 struct wined3d_line_pattern lp;
1646 DWORD d;
1647 } lp;
1648 union
1650 float f;
1651 DWORD d;
1652 } tmpfloat;
1654 rs[WINED3D_RS_ZENABLE] = WINED3D_ZB_TRUE;
1655 rs[WINED3D_RS_FILLMODE] = WINED3D_FILL_SOLID;
1656 rs[WINED3D_RS_SHADEMODE] = WINED3D_SHADE_GOURAUD;
1657 lp.lp.repeat_factor = 0;
1658 lp.lp.line_pattern = 0;
1659 rs[WINED3D_RS_LINEPATTERN] = lp.d;
1660 rs[WINED3D_RS_ZWRITEENABLE] = TRUE;
1661 rs[WINED3D_RS_ALPHATESTENABLE] = FALSE;
1662 rs[WINED3D_RS_LASTPIXEL] = TRUE;
1663 rs[WINED3D_RS_SRCBLEND] = WINED3D_BLEND_ONE;
1664 rs[WINED3D_RS_DESTBLEND] = WINED3D_BLEND_ZERO;
1665 rs[WINED3D_RS_CULLMODE] = WINED3D_CULL_BACK;
1666 rs[WINED3D_RS_ZFUNC] = WINED3D_CMP_LESSEQUAL;
1667 rs[WINED3D_RS_ALPHAFUNC] = WINED3D_CMP_ALWAYS;
1668 rs[WINED3D_RS_ALPHAREF] = 0;
1669 rs[WINED3D_RS_DITHERENABLE] = FALSE;
1670 rs[WINED3D_RS_ALPHABLENDENABLE] = FALSE;
1671 rs[WINED3D_RS_FOGENABLE] = FALSE;
1672 rs[WINED3D_RS_SPECULARENABLE] = FALSE;
1673 rs[WINED3D_RS_ZVISIBLE] = 0;
1674 rs[WINED3D_RS_FOGCOLOR] = 0;
1675 rs[WINED3D_RS_FOGTABLEMODE] = WINED3D_FOG_NONE;
1676 tmpfloat.f = 0.0f;
1677 rs[WINED3D_RS_FOGSTART] = tmpfloat.d;
1678 tmpfloat.f = 1.0f;
1679 rs[WINED3D_RS_FOGEND] = tmpfloat.d;
1680 tmpfloat.f = 1.0f;
1681 rs[WINED3D_RS_FOGDENSITY] = tmpfloat.d;
1682 rs[WINED3D_RS_RANGEFOGENABLE] = FALSE;
1683 rs[WINED3D_RS_STENCILENABLE] = FALSE;
1684 rs[WINED3D_RS_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1685 rs[WINED3D_RS_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1686 rs[WINED3D_RS_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1687 rs[WINED3D_RS_STENCILREF] = 0;
1688 rs[WINED3D_RS_STENCILMASK] = 0xffffffff;
1689 rs[WINED3D_RS_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1690 rs[WINED3D_RS_STENCILWRITEMASK] = 0xffffffff;
1691 rs[WINED3D_RS_TEXTUREFACTOR] = 0xffffffff;
1692 rs[WINED3D_RS_WRAP0] = 0;
1693 rs[WINED3D_RS_WRAP1] = 0;
1694 rs[WINED3D_RS_WRAP2] = 0;
1695 rs[WINED3D_RS_WRAP3] = 0;
1696 rs[WINED3D_RS_WRAP4] = 0;
1697 rs[WINED3D_RS_WRAP5] = 0;
1698 rs[WINED3D_RS_WRAP6] = 0;
1699 rs[WINED3D_RS_WRAP7] = 0;
1700 rs[WINED3D_RS_CLIPPING] = TRUE;
1701 rs[WINED3D_RS_LIGHTING] = TRUE;
1702 rs[WINED3D_RS_AMBIENT] = 0;
1703 rs[WINED3D_RS_FOGVERTEXMODE] = WINED3D_FOG_NONE;
1704 rs[WINED3D_RS_COLORVERTEX] = TRUE;
1705 rs[WINED3D_RS_LOCALVIEWER] = TRUE;
1706 rs[WINED3D_RS_NORMALIZENORMALS] = FALSE;
1707 rs[WINED3D_RS_DIFFUSEMATERIALSOURCE] = WINED3D_MCS_COLOR1;
1708 rs[WINED3D_RS_SPECULARMATERIALSOURCE] = WINED3D_MCS_COLOR2;
1709 rs[WINED3D_RS_AMBIENTMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1710 rs[WINED3D_RS_EMISSIVEMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1711 rs[WINED3D_RS_VERTEXBLEND] = WINED3D_VBF_DISABLE;
1712 rs[WINED3D_RS_CLIPPLANEENABLE] = 0;
1713 rs[WINED3D_RS_SOFTWAREVERTEXPROCESSING] = FALSE;
1714 tmpfloat.f = 1.0f;
1715 rs[WINED3D_RS_POINTSIZE] = tmpfloat.d;
1716 tmpfloat.f = 1.0f;
1717 rs[WINED3D_RS_POINTSIZE_MIN] = tmpfloat.d;
1718 rs[WINED3D_RS_POINTSPRITEENABLE] = FALSE;
1719 rs[WINED3D_RS_POINTSCALEENABLE] = FALSE;
1720 tmpfloat.f = 1.0f;
1721 rs[WINED3D_RS_POINTSCALE_A] = tmpfloat.d;
1722 tmpfloat.f = 0.0f;
1723 rs[WINED3D_RS_POINTSCALE_B] = tmpfloat.d;
1724 tmpfloat.f = 0.0f;
1725 rs[WINED3D_RS_POINTSCALE_C] = tmpfloat.d;
1726 rs[WINED3D_RS_MULTISAMPLEANTIALIAS] = TRUE;
1727 rs[WINED3D_RS_MULTISAMPLEMASK] = 0xffffffff;
1728 rs[WINED3D_RS_PATCHEDGESTYLE] = WINED3D_PATCH_EDGE_DISCRETE;
1729 tmpfloat.f = 1.0f;
1730 rs[WINED3D_RS_PATCHSEGMENTS] = tmpfloat.d;
1731 rs[WINED3D_RS_DEBUGMONITORTOKEN] = 0xbaadcafe;
1732 tmpfloat.f = d3d_info->limits.pointsize_max;
1733 rs[WINED3D_RS_POINTSIZE_MAX] = tmpfloat.d;
1734 rs[WINED3D_RS_INDEXEDVERTEXBLENDENABLE] = FALSE;
1735 rs[WINED3D_RS_COLORWRITEENABLE] = 0x0000000f;
1736 tmpfloat.f = 0.0f;
1737 rs[WINED3D_RS_TWEENFACTOR] = tmpfloat.d;
1738 rs[WINED3D_RS_BLENDOP] = WINED3D_BLEND_OP_ADD;
1739 rs[WINED3D_RS_POSITIONDEGREE] = WINED3D_DEGREE_CUBIC;
1740 rs[WINED3D_RS_NORMALDEGREE] = WINED3D_DEGREE_LINEAR;
1741 /* states new in d3d9 */
1742 rs[WINED3D_RS_SCISSORTESTENABLE] = FALSE;
1743 rs[WINED3D_RS_SLOPESCALEDEPTHBIAS] = 0;
1744 tmpfloat.f = 1.0f;
1745 rs[WINED3D_RS_MINTESSELLATIONLEVEL] = tmpfloat.d;
1746 rs[WINED3D_RS_MAXTESSELLATIONLEVEL] = tmpfloat.d;
1747 rs[WINED3D_RS_ANTIALIASEDLINEENABLE] = FALSE;
1748 tmpfloat.f = 0.0f;
1749 rs[WINED3D_RS_ADAPTIVETESS_X] = tmpfloat.d;
1750 rs[WINED3D_RS_ADAPTIVETESS_Y] = tmpfloat.d;
1751 tmpfloat.f = 1.0f;
1752 rs[WINED3D_RS_ADAPTIVETESS_Z] = tmpfloat.d;
1753 tmpfloat.f = 0.0f;
1754 rs[WINED3D_RS_ADAPTIVETESS_W] = tmpfloat.d;
1755 rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION] = FALSE;
1756 rs[WINED3D_RS_TWOSIDEDSTENCILMODE] = FALSE;
1757 rs[WINED3D_RS_BACK_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1758 rs[WINED3D_RS_BACK_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1759 rs[WINED3D_RS_BACK_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1760 rs[WINED3D_RS_BACK_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1761 rs[WINED3D_RS_COLORWRITEENABLE1] = 0x0000000f;
1762 rs[WINED3D_RS_COLORWRITEENABLE2] = 0x0000000f;
1763 rs[WINED3D_RS_COLORWRITEENABLE3] = 0x0000000f;
1764 rs[WINED3D_RS_BLENDFACTOR] = 0xffffffff;
1765 rs[WINED3D_RS_SRGBWRITEENABLE] = 0;
1766 rs[WINED3D_RS_DEPTHBIAS] = 0;
1767 rs[WINED3D_RS_WRAP8] = 0;
1768 rs[WINED3D_RS_WRAP9] = 0;
1769 rs[WINED3D_RS_WRAP10] = 0;
1770 rs[WINED3D_RS_WRAP11] = 0;
1771 rs[WINED3D_RS_WRAP12] = 0;
1772 rs[WINED3D_RS_WRAP13] = 0;
1773 rs[WINED3D_RS_WRAP14] = 0;
1774 rs[WINED3D_RS_WRAP15] = 0;
1775 rs[WINED3D_RS_SEPARATEALPHABLENDENABLE] = FALSE;
1776 rs[WINED3D_RS_SRCBLENDALPHA] = WINED3D_BLEND_ONE;
1777 rs[WINED3D_RS_DESTBLENDALPHA] = WINED3D_BLEND_ZERO;
1778 rs[WINED3D_RS_BLENDOPALPHA] = WINED3D_BLEND_OP_ADD;
1781 static void init_default_texture_state(unsigned int i, uint32_t stage[WINED3D_HIGHEST_TEXTURE_STATE + 1])
1783 stage[WINED3D_TSS_COLOR_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_MODULATE;
1784 stage[WINED3D_TSS_COLOR_ARG1] = WINED3DTA_TEXTURE;
1785 stage[WINED3D_TSS_COLOR_ARG2] = WINED3DTA_CURRENT;
1786 stage[WINED3D_TSS_ALPHA_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_SELECT_ARG1;
1787 stage[WINED3D_TSS_ALPHA_ARG1] = WINED3DTA_TEXTURE;
1788 stage[WINED3D_TSS_ALPHA_ARG2] = WINED3DTA_CURRENT;
1789 stage[WINED3D_TSS_BUMPENV_MAT00] = 0;
1790 stage[WINED3D_TSS_BUMPENV_MAT01] = 0;
1791 stage[WINED3D_TSS_BUMPENV_MAT10] = 0;
1792 stage[WINED3D_TSS_BUMPENV_MAT11] = 0;
1793 stage[WINED3D_TSS_TEXCOORD_INDEX] = i;
1794 stage[WINED3D_TSS_BUMPENV_LSCALE] = 0;
1795 stage[WINED3D_TSS_BUMPENV_LOFFSET] = 0;
1796 stage[WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS] = WINED3D_TTFF_DISABLE;
1797 stage[WINED3D_TSS_COLOR_ARG0] = WINED3DTA_CURRENT;
1798 stage[WINED3D_TSS_ALPHA_ARG0] = WINED3DTA_CURRENT;
1799 stage[WINED3D_TSS_RESULT_ARG] = WINED3DTA_CURRENT;
1802 static void init_default_sampler_states(uint32_t states[WINED3D_MAX_COMBINED_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1])
1804 unsigned int i;
1806 for (i = 0 ; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1808 TRACE("Setting up default samplers states for sampler %u.\n", i);
1809 states[i][WINED3D_SAMP_ADDRESS_U] = WINED3D_TADDRESS_WRAP;
1810 states[i][WINED3D_SAMP_ADDRESS_V] = WINED3D_TADDRESS_WRAP;
1811 states[i][WINED3D_SAMP_ADDRESS_W] = WINED3D_TADDRESS_WRAP;
1812 states[i][WINED3D_SAMP_BORDER_COLOR] = 0;
1813 states[i][WINED3D_SAMP_MAG_FILTER] = WINED3D_TEXF_POINT;
1814 states[i][WINED3D_SAMP_MIN_FILTER] = WINED3D_TEXF_POINT;
1815 states[i][WINED3D_SAMP_MIP_FILTER] = WINED3D_TEXF_NONE;
1816 states[i][WINED3D_SAMP_MIPMAP_LOD_BIAS] = 0;
1817 states[i][WINED3D_SAMP_MAX_MIP_LEVEL] = 0;
1818 states[i][WINED3D_SAMP_MAX_ANISOTROPY] = 1;
1819 states[i][WINED3D_SAMP_SRGB_TEXTURE] = 0;
1820 /* TODO: Indicates which element of a multielement texture to use. */
1821 states[i][WINED3D_SAMP_ELEMENT_INDEX] = 0;
1822 /* TODO: Vertex offset in the presampled displacement map. */
1823 states[i][WINED3D_SAMP_DMAP_OFFSET] = 0;
1827 static void state_init_default(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info)
1829 struct wined3d_matrix identity;
1830 unsigned int i, j;
1832 TRACE("state %p, d3d_info %p.\n", state, d3d_info);
1834 get_identity_matrix(&identity);
1835 state->primitive_type = WINED3D_PT_UNDEFINED;
1836 state->patch_vertex_count = 0;
1838 /* Set some of the defaults for lights, transforms etc */
1839 state->transforms[WINED3D_TS_PROJECTION] = identity;
1840 state->transforms[WINED3D_TS_VIEW] = identity;
1841 for (i = 0; i < 256; ++i)
1843 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
1846 init_default_render_states(state->render_states, d3d_info);
1848 /* Texture Stage States - Put directly into state block, we will call function below */
1849 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1851 TRACE("Setting up default texture states for texture Stage %u.\n", i);
1852 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
1853 init_default_texture_state(i, state->texture_states[i]);
1856 init_default_sampler_states(state->sampler_states);
1858 state->blend_factor.r = 1.0f;
1859 state->blend_factor.g = 1.0f;
1860 state->blend_factor.b = 1.0f;
1861 state->blend_factor.a = 1.0f;
1863 state->sample_mask = 0xffffffff;
1865 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1866 state->streams[i].frequency = 1;
1868 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1870 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
1871 state->cb[i][j].size = WINED3D_MAX_CONSTANT_BUFFER_SIZE * 16;
1875 static int lights_compare(const void *key, const struct rb_entry *entry)
1877 const struct wined3d_light_info *light = RB_ENTRY_VALUE(entry, struct wined3d_light_info, entry);
1878 unsigned int original_index = (ULONG_PTR)key;
1880 return wined3d_uint32_compare(light->OriginalIndex, original_index);
1883 void state_init(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info,
1884 uint32_t flags, enum wined3d_feature_level feature_level)
1886 state->feature_level = feature_level;
1887 state->flags = flags;
1889 rb_init(&state->light_state.lights_tree, lights_compare);
1891 if (flags & WINED3D_STATE_INIT_DEFAULT)
1892 state_init_default(state, d3d_info);
1895 static bool wined3d_select_feature_level(const struct wined3d_adapter *adapter,
1896 const enum wined3d_feature_level *levels, unsigned int level_count,
1897 enum wined3d_feature_level *selected_level)
1899 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
1900 unsigned int i;
1902 for (i = 0; i < level_count; ++i)
1904 if (levels[i] && d3d_info->feature_level >= levels[i])
1906 *selected_level = levels[i];
1907 return true;
1911 FIXME_(winediag)("None of the requested D3D feature levels is supported on this GPU "
1912 "with the current shader backend.\n");
1913 return false;
1916 HRESULT CDECL wined3d_state_create(struct wined3d_device *device,
1917 const enum wined3d_feature_level *levels, unsigned int level_count, struct wined3d_state **state)
1919 enum wined3d_feature_level feature_level;
1920 struct wined3d_state *object;
1922 TRACE("device %p, levels %p, level_count %u, state %p.\n", device, levels, level_count, state);
1924 if (!wined3d_select_feature_level(device->adapter, levels, level_count, &feature_level))
1925 return E_FAIL;
1927 TRACE("Selected feature level %s.\n", wined3d_debug_feature_level(feature_level));
1929 if (!(object = heap_alloc_zero(sizeof(*object))))
1930 return E_OUTOFMEMORY;
1931 state_init(object, &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT, feature_level);
1933 *state = object;
1934 return S_OK;
1937 enum wined3d_feature_level CDECL wined3d_state_get_feature_level(const struct wined3d_state *state)
1939 TRACE("state %p.\n", state);
1941 return state->feature_level;
1944 void CDECL wined3d_state_destroy(struct wined3d_state *state)
1946 TRACE("state %p.\n", state);
1948 state_cleanup(state);
1949 heap_free(state);
1952 static void stateblock_state_init_default(struct wined3d_stateblock_state *state,
1953 const struct wined3d_d3d_info *d3d_info)
1955 struct wined3d_matrix identity;
1956 unsigned int i;
1958 get_identity_matrix(&identity);
1960 state->transforms[WINED3D_TS_PROJECTION] = identity;
1961 state->transforms[WINED3D_TS_VIEW] = identity;
1962 for (i = 0; i < 256; ++i)
1964 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
1967 init_default_render_states(state->rs, d3d_info);
1969 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1971 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
1972 init_default_texture_state(i, state->texture_states[i]);
1975 init_default_sampler_states(state->sampler_states);
1977 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1978 state->streams[i].frequency = 1;
1981 void wined3d_stateblock_state_init(struct wined3d_stateblock_state *state,
1982 const struct wined3d_device *device, uint32_t flags)
1984 rb_init(&state->light_state->lights_tree, lights_compare);
1986 if (flags & WINED3D_STATE_INIT_DEFAULT)
1987 stateblock_state_init_default(state, &device->adapter->d3d_info);
1991 static HRESULT stateblock_init(struct wined3d_stateblock *stateblock, const struct wined3d_stateblock *device_state,
1992 struct wined3d_device *device, enum wined3d_stateblock_type type)
1994 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
1996 stateblock->ref = 1;
1997 stateblock->device = device;
1998 stateblock->stateblock_state.light_state = &stateblock->light_state;
1999 wined3d_stateblock_state_init(&stateblock->stateblock_state, device,
2000 type == WINED3D_SBT_PRIMARY ? WINED3D_STATE_INIT_DEFAULT : 0);
2002 stateblock->changed.store_stream_offset = 1;
2003 list_init(&stateblock->changed.changed_lights);
2005 if (type == WINED3D_SBT_RECORDED || type == WINED3D_SBT_PRIMARY)
2006 return WINED3D_OK;
2008 TRACE("Updating changed flags appropriate for type %#x.\n", type);
2010 switch (type)
2012 case WINED3D_SBT_ALL:
2013 stateblock_init_lights(stateblock, &device_state->stateblock_state.light_state->lights_tree);
2014 stateblock_savedstates_set_all(&stateblock->changed,
2015 d3d_info->limits.vs_uniform_count, d3d_info->limits.ps_uniform_count);
2016 break;
2018 case WINED3D_SBT_PIXEL_STATE:
2019 stateblock_savedstates_set_pixel(&stateblock->changed,
2020 d3d_info->limits.ps_uniform_count);
2021 break;
2023 case WINED3D_SBT_VERTEX_STATE:
2024 stateblock_init_lights(stateblock, &device_state->stateblock_state.light_state->lights_tree);
2025 stateblock_savedstates_set_vertex(&stateblock->changed,
2026 d3d_info->limits.vs_uniform_count);
2027 break;
2029 default:
2030 FIXME("Unrecognized state block type %#x.\n", type);
2031 break;
2034 wined3d_stateblock_init_contained_states(stateblock);
2035 wined3d_stateblock_capture(stateblock, device_state);
2037 /* According to the tests, stream offset is not updated in the captured state if
2038 * the state was captured on state block creation. This is not the case for
2039 * state blocks initialized with BeginStateBlock / EndStateBlock, multiple
2040 * captures get stream offsets updated. */
2041 stateblock->changed.store_stream_offset = 0;
2043 return WINED3D_OK;
2046 HRESULT CDECL wined3d_stateblock_create(struct wined3d_device *device, const struct wined3d_stateblock *device_state,
2047 enum wined3d_stateblock_type type, struct wined3d_stateblock **stateblock)
2049 struct wined3d_stateblock *object;
2050 HRESULT hr;
2052 TRACE("device %p, device_state %p, type %#x, stateblock %p.\n",
2053 device, device_state, type, stateblock);
2055 if (!(object = heap_alloc_zero(sizeof(*object))))
2056 return E_OUTOFMEMORY;
2058 hr = stateblock_init(object, device_state, device, type);
2059 if (FAILED(hr))
2061 WARN("Failed to initialize stateblock, hr %#lx.\n", hr);
2062 heap_free(object);
2063 return hr;
2066 TRACE("Created stateblock %p.\n", object);
2067 *stateblock = object;
2069 return WINED3D_OK;
2072 void CDECL wined3d_stateblock_reset(struct wined3d_stateblock *stateblock)
2074 TRACE("stateblock %p.\n", stateblock);
2076 wined3d_stateblock_state_cleanup(&stateblock->stateblock_state);
2077 memset(&stateblock->stateblock_state, 0, sizeof(stateblock->stateblock_state));
2078 stateblock->stateblock_state.light_state = &stateblock->light_state;
2079 wined3d_stateblock_state_init(&stateblock->stateblock_state, stateblock->device, WINED3D_STATE_INIT_DEFAULT);