dmband: Rewrite band lbin list parsing.
[wine.git] / dlls / wined3d / stateblock.c
blob2dfc53a83596a4e6d45218752968cb00e4f0ac08
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
9 * Copyright 2019,2020,2022 Zebediah Figura for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag);
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_BLENDFACTOR,
39 WINED3D_RS_BLENDOP,
40 WINED3D_RS_BLENDOPALPHA,
41 WINED3D_RS_BACK_STENCILFAIL,
42 WINED3D_RS_BACK_STENCILPASS,
43 WINED3D_RS_BACK_STENCILZFAIL,
44 WINED3D_RS_COLORWRITEENABLE,
45 WINED3D_RS_COLORWRITEENABLE1,
46 WINED3D_RS_COLORWRITEENABLE2,
47 WINED3D_RS_COLORWRITEENABLE3,
48 WINED3D_RS_DEPTHBIAS,
49 WINED3D_RS_DESTBLEND,
50 WINED3D_RS_DESTBLENDALPHA,
51 WINED3D_RS_DITHERENABLE,
52 WINED3D_RS_FILLMODE,
53 WINED3D_RS_FOGDENSITY,
54 WINED3D_RS_FOGEND,
55 WINED3D_RS_FOGSTART,
56 WINED3D_RS_LASTPIXEL,
57 WINED3D_RS_SCISSORTESTENABLE,
58 WINED3D_RS_SEPARATEALPHABLENDENABLE,
59 WINED3D_RS_SHADEMODE,
60 WINED3D_RS_SLOPESCALEDEPTHBIAS,
61 WINED3D_RS_SRCBLEND,
62 WINED3D_RS_SRCBLENDALPHA,
63 WINED3D_RS_SRGBWRITEENABLE,
64 WINED3D_RS_STENCILENABLE,
65 WINED3D_RS_STENCILFAIL,
66 WINED3D_RS_STENCILFUNC,
67 WINED3D_RS_STENCILMASK,
68 WINED3D_RS_STENCILPASS,
69 WINED3D_RS_STENCILREF,
70 WINED3D_RS_STENCILWRITEMASK,
71 WINED3D_RS_STENCILZFAIL,
72 WINED3D_RS_TEXTUREFACTOR,
73 WINED3D_RS_TWOSIDEDSTENCILMODE,
74 WINED3D_RS_WRAP0,
75 WINED3D_RS_WRAP1,
76 WINED3D_RS_WRAP10,
77 WINED3D_RS_WRAP11,
78 WINED3D_RS_WRAP12,
79 WINED3D_RS_WRAP13,
80 WINED3D_RS_WRAP14,
81 WINED3D_RS_WRAP15,
82 WINED3D_RS_WRAP2,
83 WINED3D_RS_WRAP3,
84 WINED3D_RS_WRAP4,
85 WINED3D_RS_WRAP5,
86 WINED3D_RS_WRAP6,
87 WINED3D_RS_WRAP7,
88 WINED3D_RS_WRAP8,
89 WINED3D_RS_WRAP9,
90 WINED3D_RS_ZENABLE,
91 WINED3D_RS_ZFUNC,
92 WINED3D_RS_ZWRITEENABLE,
95 static const DWORD pixel_states_texture[] =
97 WINED3D_TSS_ALPHA_ARG0,
98 WINED3D_TSS_ALPHA_ARG1,
99 WINED3D_TSS_ALPHA_ARG2,
100 WINED3D_TSS_ALPHA_OP,
101 WINED3D_TSS_BUMPENV_LOFFSET,
102 WINED3D_TSS_BUMPENV_LSCALE,
103 WINED3D_TSS_BUMPENV_MAT00,
104 WINED3D_TSS_BUMPENV_MAT01,
105 WINED3D_TSS_BUMPENV_MAT10,
106 WINED3D_TSS_BUMPENV_MAT11,
107 WINED3D_TSS_COLOR_ARG0,
108 WINED3D_TSS_COLOR_ARG1,
109 WINED3D_TSS_COLOR_ARG2,
110 WINED3D_TSS_COLOR_OP,
111 WINED3D_TSS_RESULT_ARG,
112 WINED3D_TSS_TEXCOORD_INDEX,
113 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,
116 static const DWORD pixel_states_sampler[] =
118 WINED3D_SAMP_ADDRESS_U,
119 WINED3D_SAMP_ADDRESS_V,
120 WINED3D_SAMP_ADDRESS_W,
121 WINED3D_SAMP_BORDER_COLOR,
122 WINED3D_SAMP_MAG_FILTER,
123 WINED3D_SAMP_MIN_FILTER,
124 WINED3D_SAMP_MIP_FILTER,
125 WINED3D_SAMP_MIPMAP_LOD_BIAS,
126 WINED3D_SAMP_MAX_MIP_LEVEL,
127 WINED3D_SAMP_MAX_ANISOTROPY,
128 WINED3D_SAMP_SRGB_TEXTURE,
129 WINED3D_SAMP_ELEMENT_INDEX,
132 static const DWORD vertex_states_render[] =
134 WINED3D_RS_ADAPTIVETESS_W,
135 WINED3D_RS_ADAPTIVETESS_X,
136 WINED3D_RS_ADAPTIVETESS_Y,
137 WINED3D_RS_ADAPTIVETESS_Z,
138 WINED3D_RS_AMBIENT,
139 WINED3D_RS_AMBIENTMATERIALSOURCE,
140 WINED3D_RS_CLIPPING,
141 WINED3D_RS_CLIPPLANEENABLE,
142 WINED3D_RS_COLORVERTEX,
143 WINED3D_RS_CULLMODE,
144 WINED3D_RS_DIFFUSEMATERIALSOURCE,
145 WINED3D_RS_EMISSIVEMATERIALSOURCE,
146 WINED3D_RS_ENABLEADAPTIVETESSELLATION,
147 WINED3D_RS_FOGCOLOR,
148 WINED3D_RS_FOGDENSITY,
149 WINED3D_RS_FOGENABLE,
150 WINED3D_RS_FOGEND,
151 WINED3D_RS_FOGSTART,
152 WINED3D_RS_FOGTABLEMODE,
153 WINED3D_RS_FOGVERTEXMODE,
154 WINED3D_RS_INDEXEDVERTEXBLENDENABLE,
155 WINED3D_RS_LIGHTING,
156 WINED3D_RS_LOCALVIEWER,
157 WINED3D_RS_MAXTESSELLATIONLEVEL,
158 WINED3D_RS_MINTESSELLATIONLEVEL,
159 WINED3D_RS_MULTISAMPLEANTIALIAS,
160 WINED3D_RS_MULTISAMPLEMASK,
161 WINED3D_RS_NORMALDEGREE,
162 WINED3D_RS_NORMALIZENORMALS,
163 WINED3D_RS_PATCHEDGESTYLE,
164 WINED3D_RS_POINTSCALE_A,
165 WINED3D_RS_POINTSCALE_B,
166 WINED3D_RS_POINTSCALE_C,
167 WINED3D_RS_POINTSCALEENABLE,
168 WINED3D_RS_POINTSIZE,
169 WINED3D_RS_POINTSIZE_MAX,
170 WINED3D_RS_POINTSIZE_MIN,
171 WINED3D_RS_POINTSPRITEENABLE,
172 WINED3D_RS_POSITIONDEGREE,
173 WINED3D_RS_RANGEFOGENABLE,
174 WINED3D_RS_SHADEMODE,
175 WINED3D_RS_SPECULARENABLE,
176 WINED3D_RS_SPECULARMATERIALSOURCE,
177 WINED3D_RS_TWEENFACTOR,
178 WINED3D_RS_VERTEXBLEND,
181 static const DWORD vertex_states_texture[] =
183 WINED3D_TSS_TEXCOORD_INDEX,
184 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,
187 static const DWORD vertex_states_sampler[] =
189 WINED3D_SAMP_DMAP_OFFSET,
192 static inline void stateblock_set_all_bits(uint32_t *map, UINT map_size)
194 DWORD mask = (1u << (map_size & 0x1f)) - 1;
195 memset(map, 0xff, (map_size >> 5) * sizeof(*map));
196 if (mask) map[map_size >> 5] = mask;
199 /* Set all members of a stateblock savedstate to the given value */
200 static void stateblock_savedstates_set_all(struct wined3d_saved_states *states, DWORD vs_consts, DWORD ps_consts)
202 unsigned int i;
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->alpha_to_coverage = 1;
212 states->lights = 1;
213 states->transforms = 1;
215 states->streamSource = 0xffff;
216 states->streamFreq = 0xffff;
217 states->textures = 0xfffff;
218 stateblock_set_all_bits(states->transform, WINED3D_HIGHEST_TRANSFORM_STATE + 1);
219 stateblock_set_all_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
220 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
221 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3ffe;
222 states->clipplane = wined3d_mask_from_size(WINED3D_MAX_CLIP_DISTANCES);
223 states->pixelShaderConstantsB = 0xffff;
224 states->pixelShaderConstantsI = 0xffff;
225 states->vertexShaderConstantsB = 0xffff;
226 states->vertexShaderConstantsI = 0xffff;
228 memset(states->ps_consts_f, 0xffu, sizeof(states->ps_consts_f));
229 memset(states->vs_consts_f, 0xffu, sizeof(states->vs_consts_f));
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;
240 for (i = 0; i < ARRAY_SIZE(pixel_states_render); ++i)
242 DWORD rs = pixel_states_render[i];
243 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
246 for (i = 0; i < ARRAY_SIZE(pixel_states_texture); ++i)
247 texture_mask |= 1u << pixel_states_texture[i];
248 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
249 for (i = 0; i < ARRAY_SIZE(pixel_states_sampler); ++i)
250 sampler_mask |= 1u << pixel_states_sampler[i];
251 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
252 states->pixelShaderConstantsB = 0xffff;
253 states->pixelShaderConstantsI = 0xffff;
255 memset(states->ps_consts_f, 0xffu, sizeof(states->ps_consts_f));
258 static void stateblock_savedstates_set_vertex(struct wined3d_saved_states *states, const DWORD num_constants)
260 DWORD texture_mask = 0;
261 WORD sampler_mask = 0;
262 unsigned int i;
264 states->vertexDecl = 1;
265 states->vertexShader = 1;
266 states->alpha_to_coverage = 1;
267 states->lights = 1;
269 for (i = 0; i < ARRAY_SIZE(vertex_states_render); ++i)
271 DWORD rs = vertex_states_render[i];
272 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
275 for (i = 0; i < ARRAY_SIZE(vertex_states_texture); ++i)
276 texture_mask |= 1u << vertex_states_texture[i];
277 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
278 for (i = 0; i < ARRAY_SIZE(vertex_states_sampler); ++i)
279 sampler_mask |= 1u << vertex_states_sampler[i];
280 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
281 states->vertexShaderConstantsB = 0xffff;
282 states->vertexShaderConstantsI = 0xffff;
284 memset(states->vs_consts_f, 0xffu, sizeof(states->vs_consts_f));
287 void CDECL wined3d_stateblock_init_contained_states(struct wined3d_stateblock *stateblock)
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 <= WINED3D_HIGHEST_TRANSFORM_STATE >> 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 < WINED3D_MAX_TEXTURES; ++i)
317 DWORD map = stateblock->changed.textureState[i];
319 for(j = 0; map; map >>= 1, ++j)
321 if (!(map & 1)) continue;
323 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
324 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
325 ++stateblock->num_contained_tss_states;
329 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
331 DWORD map = stateblock->changed.samplerState[i];
333 for (j = 0; map; map >>= 1, ++j)
335 if (!(map & 1)) continue;
337 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
338 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
339 ++stateblock->num_contained_sampler_states;
344 static void stateblock_init_lights(struct wined3d_stateblock *stateblock, const struct rb_tree *src_tree)
346 struct rb_tree *dst_tree = &stateblock->stateblock_state.light_state->lights_tree;
347 struct wined3d_light_info *src_light;
349 RB_FOR_EACH_ENTRY(src_light, src_tree, struct wined3d_light_info, entry)
351 struct wined3d_light_info *dst_light = heap_alloc(sizeof(*dst_light));
353 *dst_light = *src_light;
354 rb_put(dst_tree, (void *)(ULONG_PTR)dst_light->OriginalIndex, &dst_light->entry);
355 dst_light->changed = true;
356 list_add_tail(&stateblock->changed.changed_lights, &dst_light->changed_entry);
360 ULONG CDECL wined3d_stateblock_incref(struct wined3d_stateblock *stateblock)
362 unsigned int refcount = InterlockedIncrement(&stateblock->ref);
364 TRACE("%p increasing refcount to %u.\n", stateblock, refcount);
366 return refcount;
369 void state_unbind_resources(struct wined3d_state *state)
371 struct wined3d_unordered_access_view *uav;
372 struct wined3d_shader_resource_view *srv;
373 struct wined3d_vertex_declaration *decl;
374 struct wined3d_blend_state *blend_state;
375 struct wined3d_rendertarget_view *rtv;
376 struct wined3d_sampler *sampler;
377 struct wined3d_texture *texture;
378 struct wined3d_buffer *buffer;
379 struct wined3d_shader *shader;
380 unsigned int i, j;
382 if ((decl = state->vertex_declaration))
384 state->vertex_declaration = NULL;
385 wined3d_vertex_declaration_decref(decl);
388 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
390 if ((texture = state->textures[i]))
392 state->textures[i] = NULL;
393 wined3d_texture_decref(texture);
397 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
399 if ((buffer = state->stream_output[i].buffer))
401 state->stream_output[i].buffer = NULL;
402 wined3d_buffer_decref(buffer);
406 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
408 if ((buffer = state->streams[i].buffer))
410 state->streams[i].buffer = NULL;
411 wined3d_buffer_decref(buffer);
415 if ((buffer = state->index_buffer))
417 state->index_buffer = NULL;
418 wined3d_buffer_decref(buffer);
421 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
423 if ((shader = state->shader[i]))
425 state->shader[i] = NULL;
426 wined3d_shader_decref(shader);
429 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
431 if ((buffer = state->cb[i][j].buffer))
433 state->cb[i][j].buffer = NULL;
434 wined3d_buffer_decref(buffer);
438 for (j = 0; j < MAX_SAMPLER_OBJECTS; ++j)
440 if ((sampler = state->sampler[i][j]))
442 state->sampler[i][j] = NULL;
443 wined3d_sampler_decref(sampler);
447 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
449 if ((srv = state->shader_resource_view[i][j]))
451 state->shader_resource_view[i][j] = NULL;
452 wined3d_srv_bind_count_dec(srv);
453 wined3d_shader_resource_view_decref(srv);
458 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
460 for (j = 0; j < MAX_UNORDERED_ACCESS_VIEWS; ++j)
462 if ((uav = state->unordered_access_view[i][j]))
464 state->unordered_access_view[i][j] = NULL;
465 wined3d_unordered_access_view_decref(uav);
470 if ((blend_state = state->blend_state))
472 state->blend_state = NULL;
473 wined3d_blend_state_decref(blend_state);
476 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
478 if ((rtv = state->fb.render_targets[i]))
480 state->fb.render_targets[i] = NULL;
481 wined3d_rendertarget_view_decref(rtv);
485 if ((rtv = state->fb.depth_stencil))
487 state->fb.depth_stencil = NULL;
488 wined3d_rendertarget_view_decref(rtv);
492 void wined3d_stateblock_state_cleanup(struct wined3d_stateblock_state *state)
494 struct wined3d_light_info *light, *cursor;
495 struct wined3d_vertex_declaration *decl;
496 struct wined3d_texture *texture;
497 struct wined3d_buffer *buffer;
498 struct wined3d_shader *shader;
499 unsigned int i;
501 if ((decl = state->vertex_declaration))
503 state->vertex_declaration = NULL;
504 wined3d_vertex_declaration_decref(decl);
507 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
509 if ((buffer = state->streams[i].buffer))
511 state->streams[i].buffer = NULL;
512 wined3d_buffer_decref(buffer);
516 if ((buffer = state->index_buffer))
518 state->index_buffer = NULL;
519 wined3d_buffer_decref(buffer);
522 if ((shader = state->vs))
524 state->vs = NULL;
525 wined3d_shader_decref(shader);
528 if ((shader = state->ps))
530 state->ps = NULL;
531 wined3d_shader_decref(shader);
534 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
536 if ((texture = state->textures[i]))
538 state->textures[i] = NULL;
539 wined3d_texture_decref(texture);
543 RB_FOR_EACH_ENTRY_DESTRUCTOR(light, cursor, &state->light_state->lights_tree, struct wined3d_light_info, entry)
545 if (light->changed)
546 list_remove(&light->changed_entry);
547 rb_remove(&state->light_state->lights_tree, &light->entry);
548 heap_free(light);
552 void state_cleanup(struct wined3d_state *state)
554 struct wined3d_light_info *light, *cursor;
555 unsigned int i;
557 if (!(state->flags & WINED3D_STATE_NO_REF))
558 state_unbind_resources(state);
560 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
562 state->light_state.lights[i] = NULL;
565 RB_FOR_EACH_ENTRY_DESTRUCTOR(light, cursor, &state->light_state.lights_tree, struct wined3d_light_info, entry)
567 if (light->changed)
568 list_remove(&light->changed_entry);
569 rb_remove(&state->light_state.lights_tree, &light->entry);
570 heap_free(light);
574 ULONG CDECL wined3d_stateblock_decref(struct wined3d_stateblock *stateblock)
576 unsigned int refcount = InterlockedDecrement(&stateblock->ref);
578 TRACE("%p decreasing refcount to %u\n", stateblock, refcount);
580 if (!refcount)
582 wined3d_mutex_lock();
583 wined3d_stateblock_state_cleanup(&stateblock->stateblock_state);
584 heap_free(stateblock);
585 wined3d_mutex_unlock();
588 return refcount;
591 struct wined3d_light_info *wined3d_light_state_get_light(const struct wined3d_light_state *state, unsigned int idx)
593 struct rb_entry *entry;
595 if (!(entry = rb_get(&state->lights_tree, (void *)(ULONG_PTR)idx)))
596 return NULL;
598 return RB_ENTRY_VALUE(entry, struct wined3d_light_info, entry);
601 static void set_light_changed(struct wined3d_stateblock *stateblock, struct wined3d_light_info *light_info)
603 if (!light_info->changed)
605 list_add_tail(&stateblock->changed.changed_lights, &light_info->changed_entry);
606 light_info->changed = true;
608 stateblock->changed.lights = 1;
611 HRESULT wined3d_light_state_set_light(struct wined3d_light_state *state, DWORD light_idx,
612 const struct wined3d_light *params, struct wined3d_light_info **light_info)
614 struct wined3d_light_info *object;
616 if (!(object = wined3d_light_state_get_light(state, light_idx)))
618 TRACE("Adding new light.\n");
619 if (!(object = heap_alloc_zero(sizeof(*object))))
621 ERR("Failed to allocate light info.\n");
622 return E_OUTOFMEMORY;
625 object->glIndex = -1;
626 object->OriginalIndex = light_idx;
627 rb_put(&state->lights_tree, (void *)(ULONG_PTR)light_idx, &object->entry);
630 object->OriginalParms = *params;
632 *light_info = object;
633 return WINED3D_OK;
636 bool wined3d_light_state_enable_light(struct wined3d_light_state *state, const struct wined3d_d3d_info *d3d_info,
637 struct wined3d_light_info *light_info, BOOL enable)
639 unsigned int light_count, i;
641 if (!(light_info->enabled = enable))
643 if (light_info->glIndex == -1)
645 TRACE("Light already disabled, nothing to do.\n");
646 return false;
649 state->lights[light_info->glIndex] = NULL;
650 light_info->glIndex = -1;
651 return true;
654 if (light_info->glIndex != -1)
656 TRACE("Light already enabled, nothing to do.\n");
657 return false;
660 /* Find a free light. */
661 light_count = d3d_info->limits.active_light_count;
662 for (i = 0; i < light_count; ++i)
664 if (state->lights[i])
665 continue;
667 state->lights[i] = light_info;
668 light_info->glIndex = i;
669 return true;
672 /* Our tests show that Windows returns D3D_OK in this situation, even with
673 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices.
674 * This is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns
675 * TRUE * as well for those lights.
677 * TODO: Test how this affects rendering. */
678 WARN("Too many concurrently active lights.\n");
679 return false;
682 static void wined3d_state_record_lights(struct wined3d_light_state *dst_state,
683 const struct wined3d_light_state *src_state)
685 const struct wined3d_light_info *src;
686 struct wined3d_light_info *dst;
688 /* Lights... For a recorded state block, we just had a chain of actions
689 * to perform, so we need to walk that chain and update any actions which
690 * differ. */
691 RB_FOR_EACH_ENTRY(dst, &dst_state->lights_tree, struct wined3d_light_info, entry)
693 if ((src = wined3d_light_state_get_light(src_state, dst->OriginalIndex)))
695 dst->OriginalParms = src->OriginalParms;
697 if (src->glIndex == -1 && dst->glIndex != -1)
699 /* Light disabled. */
700 dst_state->lights[dst->glIndex] = NULL;
702 else if (src->glIndex != -1 && dst->glIndex == -1)
704 /* Light enabled. */
705 dst_state->lights[src->glIndex] = dst;
707 dst->glIndex = src->glIndex;
709 else
711 /* This can happen if the light was originally created as a
712 * default light for SetLightEnable() while recording. */
713 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
714 dst->OriginalIndex, dst_state, src_state);
716 dst->OriginalParms = WINED3D_default_light;
717 if (dst->glIndex != -1)
719 dst_state->lights[dst->glIndex] = NULL;
720 dst->glIndex = -1;
726 void CDECL wined3d_stateblock_capture(struct wined3d_stateblock *stateblock,
727 const struct wined3d_stateblock *device_state)
729 const struct wined3d_stateblock_state *state = &device_state->stateblock_state;
730 struct wined3d_range range;
731 unsigned int i, start;
732 uint32_t map;
734 TRACE("stateblock %p, device_state %p.\n", stateblock, device_state);
736 if (stateblock->changed.vertexShader && stateblock->stateblock_state.vs != state->vs)
738 TRACE("Updating vertex shader from %p to %p.\n", stateblock->stateblock_state.vs, state->vs);
740 if (state->vs)
741 wined3d_shader_incref(state->vs);
742 if (stateblock->stateblock_state.vs)
743 wined3d_shader_decref(stateblock->stateblock_state.vs);
744 stateblock->stateblock_state.vs = state->vs;
747 for (start = 0; ; start = range.offset + range.size)
749 if (!wined3d_bitmap_get_range(stateblock->changed.vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
750 break;
752 memcpy(&stateblock->stateblock_state.vs_consts_f[range.offset], &state->vs_consts_f[range.offset],
753 sizeof(*state->vs_consts_f) * range.size);
755 map = stateblock->changed.vertexShaderConstantsI;
756 for (start = 0; ; start = range.offset + range.size)
758 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
759 break;
761 memcpy(&stateblock->stateblock_state.vs_consts_i[range.offset], &state->vs_consts_i[range.offset],
762 sizeof(*state->vs_consts_i) * range.size);
764 map = stateblock->changed.vertexShaderConstantsB;
765 for (start = 0; ; start = range.offset + range.size)
767 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
768 break;
770 memcpy(&stateblock->stateblock_state.vs_consts_b[range.offset], &state->vs_consts_b[range.offset],
771 sizeof(*state->vs_consts_b) * range.size);
774 for (start = 0; ; start = range.offset + range.size)
776 if (!wined3d_bitmap_get_range(stateblock->changed.ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
777 break;
779 memcpy(&stateblock->stateblock_state.ps_consts_f[range.offset], &state->ps_consts_f[range.offset],
780 sizeof(*state->ps_consts_f) * range.size);
782 map = stateblock->changed.pixelShaderConstantsI;
783 for (start = 0; ; start = range.offset + range.size)
785 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
786 break;
788 memcpy(&stateblock->stateblock_state.ps_consts_i[range.offset], &state->ps_consts_i[range.offset],
789 sizeof(*state->ps_consts_i) * range.size);
791 map = stateblock->changed.pixelShaderConstantsB;
792 for (start = 0; ; start = range.offset + range.size)
794 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
795 break;
797 memcpy(&stateblock->stateblock_state.ps_consts_b[range.offset], &state->ps_consts_b[range.offset],
798 sizeof(*state->ps_consts_b) * range.size);
801 if (stateblock->changed.transforms)
803 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
805 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
807 TRACE("Updating transform %#x.\n", transform);
809 stateblock->stateblock_state.transforms[transform] = state->transforms[transform];
813 if (stateblock->changed.indices
814 && ((stateblock->stateblock_state.index_buffer != state->index_buffer)
815 || (stateblock->stateblock_state.base_vertex_index != state->base_vertex_index)
816 || (stateblock->stateblock_state.index_format != state->index_format)))
818 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
819 state->index_buffer, state->base_vertex_index);
821 if (state->index_buffer)
822 wined3d_buffer_incref(state->index_buffer);
823 if (stateblock->stateblock_state.index_buffer)
824 wined3d_buffer_decref(stateblock->stateblock_state.index_buffer);
825 stateblock->stateblock_state.index_buffer = state->index_buffer;
826 stateblock->stateblock_state.base_vertex_index = state->base_vertex_index;
827 stateblock->stateblock_state.index_format = state->index_format;
830 if (stateblock->changed.vertexDecl && stateblock->stateblock_state.vertex_declaration != state->vertex_declaration)
832 TRACE("Updating vertex declaration from %p to %p.\n",
833 stateblock->stateblock_state.vertex_declaration, state->vertex_declaration);
835 if (state->vertex_declaration)
836 wined3d_vertex_declaration_incref(state->vertex_declaration);
837 if (stateblock->stateblock_state.vertex_declaration)
838 wined3d_vertex_declaration_decref(stateblock->stateblock_state.vertex_declaration);
839 stateblock->stateblock_state.vertex_declaration = state->vertex_declaration;
842 if (stateblock->changed.material
843 && memcmp(&state->material, &stateblock->stateblock_state.material,
844 sizeof(stateblock->stateblock_state.material)))
846 TRACE("Updating material.\n");
848 stateblock->stateblock_state.material = state->material;
851 if (stateblock->changed.viewport
852 && memcmp(&state->viewport, &stateblock->stateblock_state.viewport, sizeof(state->viewport)))
854 TRACE("Updating viewport.\n");
856 stateblock->stateblock_state.viewport = state->viewport;
859 if (stateblock->changed.scissorRect
860 && memcmp(&state->scissor_rect, &stateblock->stateblock_state.scissor_rect, sizeof(state->scissor_rect)))
862 TRACE("Updating scissor rect.\n");
864 stateblock->stateblock_state.scissor_rect = state->scissor_rect;
867 map = stateblock->changed.streamSource;
868 while (map)
870 i = wined3d_bit_scan(&map);
872 if (stateblock->stateblock_state.streams[i].stride != state->streams[i].stride
873 || stateblock->stateblock_state.streams[i].offset != state->streams[i].offset
874 || stateblock->stateblock_state.streams[i].buffer != state->streams[i].buffer)
876 TRACE("stateblock %p, stream source %u, buffer %p, stride %u, offset %u.\n",
877 stateblock, i, state->streams[i].buffer, state->streams[i].stride,
878 state->streams[i].offset);
880 stateblock->stateblock_state.streams[i].stride = state->streams[i].stride;
881 if (stateblock->changed.store_stream_offset)
882 stateblock->stateblock_state.streams[i].offset = state->streams[i].offset;
884 if (state->streams[i].buffer)
885 wined3d_buffer_incref(state->streams[i].buffer);
886 if (stateblock->stateblock_state.streams[i].buffer)
887 wined3d_buffer_decref(stateblock->stateblock_state.streams[i].buffer);
888 stateblock->stateblock_state.streams[i].buffer = state->streams[i].buffer;
892 map = stateblock->changed.streamFreq;
893 while (map)
895 i = wined3d_bit_scan(&map);
897 if (stateblock->stateblock_state.streams[i].frequency != state->streams[i].frequency
898 || stateblock->stateblock_state.streams[i].flags != state->streams[i].flags)
900 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
901 i, state->streams[i].frequency, state->streams[i].flags);
903 stateblock->stateblock_state.streams[i].frequency = state->streams[i].frequency;
904 stateblock->stateblock_state.streams[i].flags = state->streams[i].flags;
908 map = stateblock->changed.clipplane;
909 while (map)
911 i = wined3d_bit_scan(&map);
913 if (memcmp(&stateblock->stateblock_state.clip_planes[i], &state->clip_planes[i], sizeof(state->clip_planes[i])))
915 TRACE("Updating clipplane %u.\n", i);
916 stateblock->stateblock_state.clip_planes[i] = state->clip_planes[i];
920 /* Render */
921 for (i = 0; i < stateblock->num_contained_render_states; ++i)
923 enum wined3d_render_state rs = stateblock->contained_render_states[i];
925 TRACE("Updating render state %#x to %u.\n", rs, state->rs[rs]);
927 stateblock->stateblock_state.rs[rs] = state->rs[rs];
930 /* Texture states */
931 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
933 unsigned int stage = stateblock->contained_tss_states[i].stage;
934 unsigned int texture_state = stateblock->contained_tss_states[i].state;
936 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage, texture_state,
937 state->texture_states[stage][texture_state],
938 stateblock->stateblock_state.texture_states[stage][texture_state]);
940 stateblock->stateblock_state.texture_states[stage][texture_state] = state->texture_states[stage][texture_state];
943 /* Samplers */
944 map = stateblock->changed.textures;
945 while (map)
947 i = wined3d_bit_scan(&map);
949 TRACE("Updating texture %u to %p (was %p).\n",
950 i, state->textures[i], stateblock->stateblock_state.textures[i]);
952 if (state->textures[i])
953 wined3d_texture_incref(state->textures[i]);
954 if (stateblock->stateblock_state.textures[i])
955 wined3d_texture_decref(stateblock->stateblock_state.textures[i]);
956 stateblock->stateblock_state.textures[i] = state->textures[i];
959 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
961 unsigned int stage = stateblock->contained_sampler_states[i].stage;
962 unsigned int sampler_state = stateblock->contained_sampler_states[i].state;
964 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage, sampler_state,
965 state->sampler_states[stage][sampler_state],
966 stateblock->stateblock_state.sampler_states[stage][sampler_state]);
968 stateblock->stateblock_state.sampler_states[stage][sampler_state] = state->sampler_states[stage][sampler_state];
971 if (stateblock->changed.pixelShader && stateblock->stateblock_state.ps != state->ps)
973 if (state->ps)
974 wined3d_shader_incref(state->ps);
975 if (stateblock->stateblock_state.ps)
976 wined3d_shader_decref(stateblock->stateblock_state.ps);
977 stateblock->stateblock_state.ps = state->ps;
980 if (stateblock->changed.lights)
981 wined3d_state_record_lights(stateblock->stateblock_state.light_state, state->light_state);
983 if (stateblock->changed.alpha_to_coverage)
984 stateblock->stateblock_state.alpha_to_coverage = state->alpha_to_coverage;
986 TRACE("Capture done.\n");
989 void CDECL wined3d_stateblock_apply(const struct wined3d_stateblock *stateblock,
990 struct wined3d_stateblock *device_state)
992 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
993 struct wined3d_range range;
994 unsigned int i, start;
995 uint32_t map;
997 TRACE("stateblock %p, device_state %p.\n", stateblock, device_state);
999 if (stateblock->changed.vertexShader)
1000 wined3d_stateblock_set_vertex_shader(device_state, state->vs);
1001 if (stateblock->changed.pixelShader)
1002 wined3d_stateblock_set_pixel_shader(device_state, state->ps);
1004 for (start = 0; ; start = range.offset + range.size)
1006 if (!wined3d_bitmap_get_range(stateblock->changed.vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
1007 break;
1008 wined3d_stateblock_set_vs_consts_f(device_state, range.offset, range.size, &state->vs_consts_f[range.offset]);
1010 map = stateblock->changed.vertexShaderConstantsI;
1011 for (start = 0; ; start = range.offset + range.size)
1013 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
1014 break;
1015 wined3d_stateblock_set_vs_consts_i(device_state, range.offset, range.size, &state->vs_consts_i[range.offset]);
1017 map = stateblock->changed.vertexShaderConstantsB;
1018 for (start = 0; ; start = range.offset + range.size)
1020 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
1021 break;
1022 wined3d_stateblock_set_vs_consts_b(device_state, range.offset, range.size, &state->vs_consts_b[range.offset]);
1025 for (start = 0; ; start = range.offset + range.size)
1027 if (!wined3d_bitmap_get_range(stateblock->changed.ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
1028 break;
1029 wined3d_stateblock_set_ps_consts_f(device_state, range.offset, range.size, &state->ps_consts_f[range.offset]);
1031 map = stateblock->changed.pixelShaderConstantsI;
1032 for (start = 0; ; start = range.offset + range.size)
1034 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
1035 break;
1036 wined3d_stateblock_set_ps_consts_i(device_state, range.offset, range.size, &state->ps_consts_i[range.offset]);
1038 map = stateblock->changed.pixelShaderConstantsB;
1039 for (start = 0; ; start = range.offset + range.size)
1041 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
1042 break;
1043 wined3d_stateblock_set_ps_consts_b(device_state, range.offset, range.size, &state->ps_consts_b[range.offset]);
1046 if (stateblock->changed.transforms)
1048 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
1050 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
1052 wined3d_stateblock_set_transform(device_state, transform, &state->transforms[transform]);
1056 if (stateblock->changed.lights)
1058 const struct wined3d_light_info *light;
1060 LIST_FOR_EACH_ENTRY(light, &stateblock->changed.changed_lights, struct wined3d_light_info, changed_entry)
1062 wined3d_stateblock_set_light(device_state, light->OriginalIndex, &light->OriginalParms);
1063 wined3d_stateblock_set_light_enable(device_state, light->OriginalIndex, light->glIndex != -1);
1067 if (stateblock->changed.alpha_to_coverage)
1069 device_state->stateblock_state.alpha_to_coverage = state->alpha_to_coverage;
1070 device_state->changed.alpha_to_coverage = 1;
1073 /* Render states. */
1074 for (i = 0; i < stateblock->num_contained_render_states; ++i)
1076 enum wined3d_render_state rs = stateblock->contained_render_states[i];
1078 wined3d_stateblock_set_render_state(device_state, rs, state->rs[rs]);
1081 /* Texture states. */
1082 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
1084 DWORD stage = stateblock->contained_tss_states[i].stage;
1085 DWORD texture_state = stateblock->contained_tss_states[i].state;
1087 wined3d_stateblock_set_texture_stage_state(device_state, stage, texture_state,
1088 state->texture_states[stage][texture_state]);
1091 /* Sampler states. */
1092 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
1094 DWORD stage = stateblock->contained_sampler_states[i].stage;
1095 DWORD sampler_state = stateblock->contained_sampler_states[i].state;
1097 wined3d_stateblock_set_sampler_state(device_state, stage, sampler_state,
1098 state->sampler_states[stage][sampler_state]);
1101 if (stateblock->changed.indices)
1103 wined3d_stateblock_set_index_buffer(device_state, state->index_buffer, state->index_format);
1104 wined3d_stateblock_set_base_vertex_index(device_state, state->base_vertex_index);
1107 if (stateblock->changed.vertexDecl && state->vertex_declaration)
1108 wined3d_stateblock_set_vertex_declaration(device_state, state->vertex_declaration);
1110 if (stateblock->changed.material)
1111 wined3d_stateblock_set_material(device_state, &state->material);
1113 if (stateblock->changed.viewport)
1114 wined3d_stateblock_set_viewport(device_state, &state->viewport);
1116 if (stateblock->changed.scissorRect)
1117 wined3d_stateblock_set_scissor_rect(device_state, &state->scissor_rect);
1119 map = stateblock->changed.streamSource;
1120 while (map)
1122 i = wined3d_bit_scan(&map);
1123 wined3d_stateblock_set_stream_source(device_state, i, state->streams[i].buffer,
1124 state->streams[i].offset, state->streams[i].stride);
1127 map = stateblock->changed.streamFreq;
1128 while (map)
1130 i = wined3d_bit_scan(&map);
1131 wined3d_stateblock_set_stream_source_freq(device_state, i,
1132 state->streams[i].frequency | state->streams[i].flags);
1135 map = stateblock->changed.textures;
1136 while (map)
1138 i = wined3d_bit_scan(&map);
1139 wined3d_stateblock_set_texture(device_state, i, state->textures[i]);
1142 map = stateblock->changed.clipplane;
1143 while (map)
1145 i = wined3d_bit_scan(&map);
1146 wined3d_stateblock_set_clip_plane(device_state, i, &state->clip_planes[i]);
1149 TRACE("Applied stateblock %p.\n", stateblock);
1152 void CDECL wined3d_stateblock_set_vertex_shader(struct wined3d_stateblock *stateblock, struct wined3d_shader *shader)
1154 TRACE("stateblock %p, shader %p.\n", stateblock, shader);
1156 if (shader)
1157 wined3d_shader_incref(shader);
1158 if (stateblock->stateblock_state.vs)
1159 wined3d_shader_decref(stateblock->stateblock_state.vs);
1160 stateblock->stateblock_state.vs = shader;
1161 stateblock->changed.vertexShader = TRUE;
1164 static void wined3d_bitmap_set_bits(uint32_t *bitmap, unsigned int start, unsigned int count)
1166 const unsigned int word_bit_count = sizeof(*bitmap) * CHAR_BIT;
1167 const unsigned int shift = start % word_bit_count;
1168 uint32_t mask, last_mask;
1169 unsigned int mask_size;
1171 bitmap += start / word_bit_count;
1172 mask = ~0u << shift;
1173 mask_size = word_bit_count - shift;
1174 last_mask = (1u << (start + count) % word_bit_count) - 1;
1175 if (mask_size <= count)
1177 *bitmap |= mask;
1178 ++bitmap;
1179 count -= mask_size;
1180 mask = ~0u;
1182 if (count >= word_bit_count)
1184 memset(bitmap, 0xffu, count / word_bit_count * sizeof(*bitmap));
1185 bitmap += count / word_bit_count;
1186 count = count % word_bit_count;
1188 if (count)
1189 *bitmap |= mask & last_mask;
1192 HRESULT CDECL wined3d_stateblock_set_vs_consts_f(struct wined3d_stateblock *stateblock,
1193 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
1195 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
1197 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1198 stateblock, start_idx, count, constants);
1200 if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.vs_uniform_count))
1201 return WINED3DERR_INVALIDCALL;
1203 memcpy(&stateblock->stateblock_state.vs_consts_f[start_idx], constants, count * sizeof(*constants));
1204 wined3d_bitmap_set_bits(stateblock->changed.vs_consts_f, start_idx, count);
1205 return WINED3D_OK;
1208 HRESULT CDECL wined3d_stateblock_set_vs_consts_i(struct wined3d_stateblock *stateblock,
1209 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
1211 unsigned int i;
1213 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1214 stateblock, start_idx, count, constants);
1216 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1217 return WINED3DERR_INVALIDCALL;
1219 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1220 count = WINED3D_MAX_CONSTS_I - start_idx;
1222 memcpy(&stateblock->stateblock_state.vs_consts_i[start_idx], constants, count * sizeof(*constants));
1223 for (i = start_idx; i < count + start_idx; ++i)
1224 stateblock->changed.vertexShaderConstantsI |= (1u << i);
1225 return WINED3D_OK;
1228 HRESULT CDECL wined3d_stateblock_set_vs_consts_b(struct wined3d_stateblock *stateblock,
1229 unsigned int start_idx, unsigned int count, const BOOL *constants)
1231 unsigned int i;
1233 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1234 stateblock, start_idx, count, constants);
1236 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1237 return WINED3DERR_INVALIDCALL;
1239 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1240 count = WINED3D_MAX_CONSTS_B - start_idx;
1242 memcpy(&stateblock->stateblock_state.vs_consts_b[start_idx], constants, count * sizeof(*constants));
1243 for (i = start_idx; i < count + start_idx; ++i)
1244 stateblock->changed.vertexShaderConstantsB |= (1u << i);
1245 return WINED3D_OK;
1248 HRESULT CDECL wined3d_stateblock_get_vs_consts_f(struct wined3d_stateblock *stateblock,
1249 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
1251 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
1253 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", stateblock, start_idx, count, constants);
1255 if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.vs_uniform_count))
1256 return WINED3DERR_INVALIDCALL;
1258 memcpy(constants, &stateblock->stateblock_state.vs_consts_f[start_idx], count * sizeof(*constants));
1259 return WINED3D_OK;
1262 HRESULT CDECL wined3d_stateblock_get_vs_consts_i(struct wined3d_stateblock *stateblock,
1263 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
1265 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", stateblock, start_idx, count, constants);
1267 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1268 return WINED3DERR_INVALIDCALL;
1270 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1271 count = WINED3D_MAX_CONSTS_I - start_idx;
1273 memcpy(constants, &stateblock->stateblock_state.vs_consts_i[start_idx], count * sizeof(*constants));
1274 return WINED3D_OK;
1277 HRESULT CDECL wined3d_stateblock_get_vs_consts_b(struct wined3d_stateblock *stateblock,
1278 unsigned int start_idx, unsigned int count, BOOL *constants)
1280 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", stateblock, start_idx, count, constants);
1282 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1283 return WINED3DERR_INVALIDCALL;
1285 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1286 count = WINED3D_MAX_CONSTS_B - start_idx;
1288 memcpy(constants, &stateblock->stateblock_state.vs_consts_b[start_idx], count * sizeof(*constants));
1289 return WINED3D_OK;
1292 void CDECL wined3d_stateblock_set_pixel_shader(struct wined3d_stateblock *stateblock, struct wined3d_shader *shader)
1294 TRACE("stateblock %p, shader %p.\n", stateblock, shader);
1296 if (shader)
1297 wined3d_shader_incref(shader);
1298 if (stateblock->stateblock_state.ps)
1299 wined3d_shader_decref(stateblock->stateblock_state.ps);
1300 stateblock->stateblock_state.ps = shader;
1301 stateblock->changed.pixelShader = TRUE;
1304 HRESULT CDECL wined3d_stateblock_set_ps_consts_f(struct wined3d_stateblock *stateblock,
1305 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
1307 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
1309 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1310 stateblock, start_idx, count, constants);
1312 if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.ps_uniform_count))
1313 return WINED3DERR_INVALIDCALL;
1315 memcpy(&stateblock->stateblock_state.ps_consts_f[start_idx], constants, count * sizeof(*constants));
1316 wined3d_bitmap_set_bits(stateblock->changed.ps_consts_f, start_idx, count);
1317 return WINED3D_OK;
1320 HRESULT CDECL wined3d_stateblock_set_ps_consts_i(struct wined3d_stateblock *stateblock,
1321 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
1323 unsigned int i;
1325 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1326 stateblock, start_idx, count, constants);
1328 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1329 return WINED3DERR_INVALIDCALL;
1331 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1332 count = WINED3D_MAX_CONSTS_I - start_idx;
1334 memcpy(&stateblock->stateblock_state.ps_consts_i[start_idx], constants, count * sizeof(*constants));
1335 for (i = start_idx; i < count + start_idx; ++i)
1336 stateblock->changed.pixelShaderConstantsI |= (1u << i);
1337 return WINED3D_OK;
1340 HRESULT CDECL wined3d_stateblock_set_ps_consts_b(struct wined3d_stateblock *stateblock,
1341 unsigned int start_idx, unsigned int count, const BOOL *constants)
1343 unsigned int i;
1345 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1346 stateblock, start_idx, count, constants);
1348 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1349 return WINED3DERR_INVALIDCALL;
1351 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1352 count = WINED3D_MAX_CONSTS_B - start_idx;
1354 memcpy(&stateblock->stateblock_state.ps_consts_b[start_idx], constants, count * sizeof(*constants));
1355 for (i = start_idx; i < count + start_idx; ++i)
1356 stateblock->changed.pixelShaderConstantsB |= (1u << i);
1357 return WINED3D_OK;
1360 HRESULT CDECL wined3d_stateblock_get_ps_consts_f(struct wined3d_stateblock *stateblock,
1361 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
1363 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", stateblock, start_idx, count, constants);
1365 if (!constants || !wined3d_bound_range(start_idx, count, WINED3D_MAX_PS_CONSTS_F))
1366 return WINED3DERR_INVALIDCALL;
1368 memcpy(constants, &stateblock->stateblock_state.ps_consts_f[start_idx], count * sizeof(*constants));
1369 return WINED3D_OK;
1372 HRESULT CDECL wined3d_stateblock_get_ps_consts_i(struct wined3d_stateblock *stateblock,
1373 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
1375 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", stateblock, start_idx, count, constants);
1377 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1378 return WINED3DERR_INVALIDCALL;
1380 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1381 count = WINED3D_MAX_CONSTS_I - start_idx;
1383 memcpy(constants, &stateblock->stateblock_state.ps_consts_i[start_idx], count * sizeof(*constants));
1384 return WINED3D_OK;
1387 HRESULT CDECL wined3d_stateblock_get_ps_consts_b(struct wined3d_stateblock *stateblock,
1388 unsigned int start_idx, unsigned int count, BOOL *constants)
1390 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", stateblock, start_idx, count, constants);
1392 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1393 return WINED3DERR_INVALIDCALL;
1395 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1396 count = WINED3D_MAX_CONSTS_B - start_idx;
1398 memcpy(constants, &stateblock->stateblock_state.ps_consts_b[start_idx], count * sizeof(*constants));
1399 return WINED3D_OK;
1402 void CDECL wined3d_stateblock_set_vertex_declaration(struct wined3d_stateblock *stateblock,
1403 struct wined3d_vertex_declaration *declaration)
1405 TRACE("stateblock %p, declaration %p.\n", stateblock, declaration);
1407 if (declaration)
1408 wined3d_vertex_declaration_incref(declaration);
1409 if (stateblock->stateblock_state.vertex_declaration)
1410 wined3d_vertex_declaration_decref(stateblock->stateblock_state.vertex_declaration);
1411 stateblock->stateblock_state.vertex_declaration = declaration;
1412 stateblock->changed.vertexDecl = TRUE;
1415 void CDECL wined3d_stateblock_set_render_state(struct wined3d_stateblock *stateblock,
1416 enum wined3d_render_state state, unsigned int value)
1418 TRACE("stateblock %p, state %s (%#x), value %#x.\n", stateblock, debug_d3drenderstate(state), state, value);
1420 if (state > WINEHIGHEST_RENDER_STATE)
1422 WARN("Unhandled render state %#x.\n", state);
1423 return;
1426 stateblock->stateblock_state.rs[state] = value;
1427 stateblock->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
1429 if (state == WINED3D_RS_POINTSIZE
1430 && (value == WINED3D_ALPHA_TO_COVERAGE_ENABLE || value == WINED3D_ALPHA_TO_COVERAGE_DISABLE))
1432 stateblock->changed.alpha_to_coverage = 1;
1433 stateblock->stateblock_state.alpha_to_coverage = (value == WINED3D_ALPHA_TO_COVERAGE_ENABLE);
1437 void CDECL wined3d_stateblock_set_sampler_state(struct wined3d_stateblock *stateblock,
1438 UINT sampler_idx, enum wined3d_sampler_state state, unsigned int value)
1440 TRACE("stateblock %p, sampler_idx %u, state %s, value %#x.\n",
1441 stateblock, sampler_idx, debug_d3dsamplerstate(state), value);
1443 if (sampler_idx >= ARRAY_SIZE(stateblock->stateblock_state.sampler_states))
1445 WARN("Invalid sampler %u.\n", sampler_idx);
1446 return;
1449 stateblock->stateblock_state.sampler_states[sampler_idx][state] = value;
1450 stateblock->changed.samplerState[sampler_idx] |= 1u << state;
1453 void CDECL wined3d_stateblock_set_texture_stage_state(struct wined3d_stateblock *stateblock,
1454 UINT stage, enum wined3d_texture_stage_state state, unsigned int value)
1456 TRACE("stateblock %p, stage %u, state %s, value %#x.\n",
1457 stateblock, stage, debug_d3dtexturestate(state), value);
1459 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
1461 WARN("Invalid state %#x passed.\n", state);
1462 return;
1465 if (stage >= WINED3D_MAX_TEXTURES)
1467 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
1468 stage, WINED3D_MAX_TEXTURES - 1);
1469 return;
1472 stateblock->stateblock_state.texture_states[stage][state] = value;
1473 stateblock->changed.textureState[stage] |= 1u << state;
1476 void CDECL wined3d_stateblock_set_texture(struct wined3d_stateblock *stateblock,
1477 UINT stage, struct wined3d_texture *texture)
1479 TRACE("stateblock %p, stage %u, texture %p.\n", stateblock, stage, texture);
1481 if (stage >= ARRAY_SIZE(stateblock->stateblock_state.textures))
1483 WARN("Ignoring invalid stage %u.\n", stage);
1484 return;
1487 if (texture)
1488 wined3d_texture_incref(texture);
1489 if (stateblock->stateblock_state.textures[stage])
1490 wined3d_texture_decref(stateblock->stateblock_state.textures[stage]);
1491 stateblock->stateblock_state.textures[stage] = texture;
1492 stateblock->changed.textures |= 1u << stage;
1495 void CDECL wined3d_stateblock_set_transform(struct wined3d_stateblock *stateblock,
1496 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1498 TRACE("stateblock %p, state %s, matrix %p.\n", stateblock, debug_d3dtstype(d3dts), matrix);
1499 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1500 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1501 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1502 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1504 stateblock->stateblock_state.transforms[d3dts] = *matrix;
1505 stateblock->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1506 stateblock->changed.transforms = 1;
1509 void CDECL wined3d_stateblock_multiply_transform(struct wined3d_stateblock *stateblock,
1510 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1512 struct wined3d_matrix *mat = &stateblock->stateblock_state.transforms[d3dts];
1514 TRACE("stateblock %p, state %s, matrix %p.\n", stateblock, debug_d3dtstype(d3dts), matrix);
1515 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1516 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1517 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1518 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1520 multiply_matrix(mat, mat, matrix);
1521 stateblock->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1522 stateblock->changed.transforms = 1;
1525 HRESULT CDECL wined3d_stateblock_set_clip_plane(struct wined3d_stateblock *stateblock,
1526 UINT plane_idx, const struct wined3d_vec4 *plane)
1528 TRACE("stateblock %p, plane_idx %u, plane %p.\n", stateblock, plane_idx, plane);
1530 if (plane_idx >= stateblock->device->adapter->d3d_info.limits.max_clip_distances)
1532 TRACE("Application has requested clipplane this device doesn't support.\n");
1533 return WINED3DERR_INVALIDCALL;
1536 stateblock->stateblock_state.clip_planes[plane_idx] = *plane;
1537 stateblock->changed.clipplane |= 1u << plane_idx;
1538 return S_OK;
1541 void CDECL wined3d_stateblock_set_material(struct wined3d_stateblock *stateblock,
1542 const struct wined3d_material *material)
1544 TRACE("stateblock %p, material %p.\n", stateblock, material);
1546 stateblock->stateblock_state.material = *material;
1547 stateblock->changed.material = TRUE;
1550 void CDECL wined3d_stateblock_set_viewport(struct wined3d_stateblock *stateblock,
1551 const struct wined3d_viewport *viewport)
1553 TRACE("stateblock %p, viewport %p.\n", stateblock, viewport);
1555 stateblock->stateblock_state.viewport = *viewport;
1556 stateblock->changed.viewport = TRUE;
1559 void CDECL wined3d_stateblock_set_scissor_rect(struct wined3d_stateblock *stateblock, const RECT *rect)
1561 TRACE("stateblock %p, rect %s.\n", stateblock, wine_dbgstr_rect(rect));
1563 stateblock->stateblock_state.scissor_rect = *rect;
1564 stateblock->changed.scissorRect = TRUE;
1567 void CDECL wined3d_stateblock_set_index_buffer(struct wined3d_stateblock *stateblock,
1568 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1570 TRACE("stateblock %p, buffer %p, format %s.\n", stateblock, buffer, debug_d3dformat(format_id));
1572 if (buffer)
1573 wined3d_buffer_incref(buffer);
1574 if (stateblock->stateblock_state.index_buffer)
1575 wined3d_buffer_decref(stateblock->stateblock_state.index_buffer);
1576 stateblock->stateblock_state.index_buffer = buffer;
1577 stateblock->stateblock_state.index_format = format_id;
1578 stateblock->changed.indices = TRUE;
1581 void CDECL wined3d_stateblock_set_base_vertex_index(struct wined3d_stateblock *stateblock, INT base_index)
1583 TRACE("stateblock %p, base_index %d.\n", stateblock, base_index);
1585 stateblock->stateblock_state.base_vertex_index = base_index;
1588 HRESULT CDECL wined3d_stateblock_set_stream_source(struct wined3d_stateblock *stateblock,
1589 UINT stream_idx, struct wined3d_buffer *buffer, UINT offset, UINT stride)
1591 struct wined3d_stream_state *stream;
1593 TRACE("stateblock %p, stream_idx %u, buffer %p, stride %u.\n",
1594 stateblock, stream_idx, buffer, stride);
1596 if (stream_idx >= WINED3D_MAX_STREAMS)
1598 WARN("Stream index %u out of range.\n", stream_idx);
1599 return WINED3DERR_INVALIDCALL;
1602 stream = &stateblock->stateblock_state.streams[stream_idx];
1604 if (buffer)
1605 wined3d_buffer_incref(buffer);
1606 if (stream->buffer)
1607 wined3d_buffer_decref(stream->buffer);
1608 stream->buffer = buffer;
1609 stream->stride = stride;
1610 stream->offset = offset;
1611 stateblock->changed.streamSource |= 1u << stream_idx;
1612 return WINED3D_OK;
1615 HRESULT CDECL wined3d_stateblock_set_stream_source_freq(struct wined3d_stateblock *stateblock,
1616 UINT stream_idx, UINT divider)
1618 struct wined3d_stream_state *stream;
1620 TRACE("stateblock %p, stream_idx %u, divider %#x.\n", stateblock, stream_idx, divider);
1622 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1624 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1625 return WINED3DERR_INVALIDCALL;
1627 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1629 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1630 return WINED3DERR_INVALIDCALL;
1632 if (!divider)
1634 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1635 return WINED3DERR_INVALIDCALL;
1638 stream = &stateblock->stateblock_state.streams[stream_idx];
1639 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1640 stream->frequency = divider & 0x7fffff;
1641 stateblock->changed.streamFreq |= 1u << stream_idx;
1642 return WINED3D_OK;
1645 HRESULT CDECL wined3d_stateblock_set_light(struct wined3d_stateblock *stateblock,
1646 UINT light_idx, const struct wined3d_light *light)
1648 struct wined3d_light_info *object = NULL;
1649 HRESULT hr;
1651 TRACE("stateblock %p, light_idx %u, light %p.\n", stateblock, light_idx, light);
1653 /* Check the parameter range. Need for speed most wanted sets junk lights
1654 * which confuse the GL driver. */
1655 if (!light)
1656 return WINED3DERR_INVALIDCALL;
1658 switch (light->type)
1660 case WINED3D_LIGHT_POINT:
1661 case WINED3D_LIGHT_SPOT:
1662 case WINED3D_LIGHT_GLSPOT:
1663 /* Incorrect attenuation values can cause the gl driver to crash.
1664 * Happens with Need for speed most wanted. */
1665 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1667 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1668 return WINED3DERR_INVALIDCALL;
1670 break;
1672 case WINED3D_LIGHT_DIRECTIONAL:
1673 case WINED3D_LIGHT_PARALLELPOINT:
1674 /* Ignores attenuation */
1675 break;
1677 default:
1678 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL.\n");
1679 return WINED3DERR_INVALIDCALL;
1682 if (SUCCEEDED(hr = wined3d_light_state_set_light(stateblock->stateblock_state.light_state, light_idx, light, &object)))
1683 set_light_changed(stateblock, object);
1684 return hr;
1687 HRESULT CDECL wined3d_stateblock_set_light_enable(struct wined3d_stateblock *stateblock, UINT light_idx, BOOL enable)
1689 struct wined3d_light_state *light_state = stateblock->stateblock_state.light_state;
1690 struct wined3d_light_info *light_info;
1691 HRESULT hr;
1693 TRACE("stateblock %p, light_idx %u, enable %#x.\n", stateblock, light_idx, enable);
1695 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1697 if (FAILED(hr = wined3d_light_state_set_light(light_state, light_idx, &WINED3D_default_light, &light_info)))
1698 return hr;
1699 set_light_changed(stateblock, light_info);
1702 if (wined3d_light_state_enable_light(light_state, &stateblock->device->adapter->d3d_info, light_info, enable))
1703 set_light_changed(stateblock, light_info);
1705 return S_OK;
1708 const struct wined3d_stateblock_state * CDECL wined3d_stateblock_get_state(const struct wined3d_stateblock *stateblock)
1710 return &stateblock->stateblock_state;
1713 HRESULT CDECL wined3d_stateblock_get_light(const struct wined3d_stateblock *stateblock,
1714 UINT light_idx, struct wined3d_light *light, BOOL *enabled)
1716 struct wined3d_light_info *light_info;
1718 if (!(light_info = wined3d_light_state_get_light(&stateblock->light_state, light_idx)))
1720 TRACE("Light %u is not defined.\n", light_idx);
1721 return WINED3DERR_INVALIDCALL;
1723 *light = light_info->OriginalParms;
1724 *enabled = light_info->enabled ? 128 : 0;
1725 return WINED3D_OK;
1728 static void init_default_render_states(unsigned int rs[WINEHIGHEST_RENDER_STATE + 1], const struct wined3d_d3d_info *d3d_info)
1730 union
1732 struct wined3d_line_pattern lp;
1733 DWORD d;
1734 } lp;
1735 union
1737 float f;
1738 DWORD d;
1739 } tmpfloat;
1741 rs[WINED3D_RS_ZENABLE] = WINED3D_ZB_TRUE;
1742 rs[WINED3D_RS_FILLMODE] = WINED3D_FILL_SOLID;
1743 rs[WINED3D_RS_SHADEMODE] = WINED3D_SHADE_GOURAUD;
1744 lp.lp.repeat_factor = 0;
1745 lp.lp.line_pattern = 0;
1746 rs[WINED3D_RS_LINEPATTERN] = lp.d;
1747 rs[WINED3D_RS_ZWRITEENABLE] = TRUE;
1748 rs[WINED3D_RS_ALPHATESTENABLE] = FALSE;
1749 rs[WINED3D_RS_LASTPIXEL] = TRUE;
1750 rs[WINED3D_RS_SRCBLEND] = WINED3D_BLEND_ONE;
1751 rs[WINED3D_RS_DESTBLEND] = WINED3D_BLEND_ZERO;
1752 rs[WINED3D_RS_CULLMODE] = WINED3D_CULL_BACK;
1753 rs[WINED3D_RS_ZFUNC] = WINED3D_CMP_LESSEQUAL;
1754 rs[WINED3D_RS_ALPHAFUNC] = WINED3D_CMP_ALWAYS;
1755 rs[WINED3D_RS_ALPHAREF] = 0;
1756 rs[WINED3D_RS_DITHERENABLE] = FALSE;
1757 rs[WINED3D_RS_ALPHABLENDENABLE] = FALSE;
1758 rs[WINED3D_RS_FOGENABLE] = FALSE;
1759 rs[WINED3D_RS_SPECULARENABLE] = FALSE;
1760 rs[WINED3D_RS_ZVISIBLE] = 0;
1761 rs[WINED3D_RS_FOGCOLOR] = 0;
1762 rs[WINED3D_RS_FOGTABLEMODE] = WINED3D_FOG_NONE;
1763 tmpfloat.f = 0.0f;
1764 rs[WINED3D_RS_FOGSTART] = tmpfloat.d;
1765 tmpfloat.f = 1.0f;
1766 rs[WINED3D_RS_FOGEND] = tmpfloat.d;
1767 tmpfloat.f = 1.0f;
1768 rs[WINED3D_RS_FOGDENSITY] = tmpfloat.d;
1769 rs[WINED3D_RS_RANGEFOGENABLE] = FALSE;
1770 rs[WINED3D_RS_STENCILENABLE] = FALSE;
1771 rs[WINED3D_RS_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1772 rs[WINED3D_RS_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1773 rs[WINED3D_RS_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1774 rs[WINED3D_RS_STENCILREF] = 0;
1775 rs[WINED3D_RS_STENCILMASK] = 0xffffffff;
1776 rs[WINED3D_RS_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1777 rs[WINED3D_RS_STENCILWRITEMASK] = 0xffffffff;
1778 rs[WINED3D_RS_TEXTUREFACTOR] = 0xffffffff;
1779 rs[WINED3D_RS_WRAP0] = 0;
1780 rs[WINED3D_RS_WRAP1] = 0;
1781 rs[WINED3D_RS_WRAP2] = 0;
1782 rs[WINED3D_RS_WRAP3] = 0;
1783 rs[WINED3D_RS_WRAP4] = 0;
1784 rs[WINED3D_RS_WRAP5] = 0;
1785 rs[WINED3D_RS_WRAP6] = 0;
1786 rs[WINED3D_RS_WRAP7] = 0;
1787 rs[WINED3D_RS_CLIPPING] = TRUE;
1788 rs[WINED3D_RS_LIGHTING] = TRUE;
1789 rs[WINED3D_RS_AMBIENT] = 0;
1790 rs[WINED3D_RS_FOGVERTEXMODE] = WINED3D_FOG_NONE;
1791 rs[WINED3D_RS_COLORVERTEX] = TRUE;
1792 rs[WINED3D_RS_LOCALVIEWER] = TRUE;
1793 rs[WINED3D_RS_NORMALIZENORMALS] = FALSE;
1794 rs[WINED3D_RS_DIFFUSEMATERIALSOURCE] = WINED3D_MCS_COLOR1;
1795 rs[WINED3D_RS_SPECULARMATERIALSOURCE] = WINED3D_MCS_COLOR2;
1796 rs[WINED3D_RS_AMBIENTMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1797 rs[WINED3D_RS_EMISSIVEMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1798 rs[WINED3D_RS_VERTEXBLEND] = WINED3D_VBF_DISABLE;
1799 rs[WINED3D_RS_CLIPPLANEENABLE] = 0;
1800 rs[WINED3D_RS_SOFTWAREVERTEXPROCESSING] = FALSE;
1801 tmpfloat.f = 1.0f;
1802 rs[WINED3D_RS_POINTSIZE] = tmpfloat.d;
1803 tmpfloat.f = 1.0f;
1804 rs[WINED3D_RS_POINTSIZE_MIN] = tmpfloat.d;
1805 rs[WINED3D_RS_POINTSPRITEENABLE] = FALSE;
1806 rs[WINED3D_RS_POINTSCALEENABLE] = FALSE;
1807 tmpfloat.f = 1.0f;
1808 rs[WINED3D_RS_POINTSCALE_A] = tmpfloat.d;
1809 tmpfloat.f = 0.0f;
1810 rs[WINED3D_RS_POINTSCALE_B] = tmpfloat.d;
1811 tmpfloat.f = 0.0f;
1812 rs[WINED3D_RS_POINTSCALE_C] = tmpfloat.d;
1813 rs[WINED3D_RS_MULTISAMPLEANTIALIAS] = TRUE;
1814 rs[WINED3D_RS_MULTISAMPLEMASK] = 0xffffffff;
1815 rs[WINED3D_RS_PATCHEDGESTYLE] = WINED3D_PATCH_EDGE_DISCRETE;
1816 tmpfloat.f = 1.0f;
1817 rs[WINED3D_RS_PATCHSEGMENTS] = tmpfloat.d;
1818 rs[WINED3D_RS_DEBUGMONITORTOKEN] = 0xbaadcafe;
1819 tmpfloat.f = d3d_info->limits.pointsize_max;
1820 rs[WINED3D_RS_POINTSIZE_MAX] = tmpfloat.d;
1821 rs[WINED3D_RS_INDEXEDVERTEXBLENDENABLE] = FALSE;
1822 rs[WINED3D_RS_COLORWRITEENABLE] = 0x0000000f;
1823 tmpfloat.f = 0.0f;
1824 rs[WINED3D_RS_TWEENFACTOR] = tmpfloat.d;
1825 rs[WINED3D_RS_BLENDOP] = WINED3D_BLEND_OP_ADD;
1826 rs[WINED3D_RS_POSITIONDEGREE] = WINED3D_DEGREE_CUBIC;
1827 rs[WINED3D_RS_NORMALDEGREE] = WINED3D_DEGREE_LINEAR;
1828 /* states new in d3d9 */
1829 rs[WINED3D_RS_SCISSORTESTENABLE] = FALSE;
1830 rs[WINED3D_RS_SLOPESCALEDEPTHBIAS] = 0;
1831 tmpfloat.f = 1.0f;
1832 rs[WINED3D_RS_MINTESSELLATIONLEVEL] = tmpfloat.d;
1833 rs[WINED3D_RS_MAXTESSELLATIONLEVEL] = tmpfloat.d;
1834 rs[WINED3D_RS_ANTIALIASEDLINEENABLE] = FALSE;
1835 tmpfloat.f = 0.0f;
1836 rs[WINED3D_RS_ADAPTIVETESS_X] = tmpfloat.d;
1837 rs[WINED3D_RS_ADAPTIVETESS_Y] = tmpfloat.d;
1838 tmpfloat.f = 1.0f;
1839 rs[WINED3D_RS_ADAPTIVETESS_Z] = tmpfloat.d;
1840 tmpfloat.f = 0.0f;
1841 rs[WINED3D_RS_ADAPTIVETESS_W] = tmpfloat.d;
1842 rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION] = FALSE;
1843 rs[WINED3D_RS_TWOSIDEDSTENCILMODE] = FALSE;
1844 rs[WINED3D_RS_BACK_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1845 rs[WINED3D_RS_BACK_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1846 rs[WINED3D_RS_BACK_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1847 rs[WINED3D_RS_BACK_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1848 rs[WINED3D_RS_COLORWRITEENABLE1] = 0x0000000f;
1849 rs[WINED3D_RS_COLORWRITEENABLE2] = 0x0000000f;
1850 rs[WINED3D_RS_COLORWRITEENABLE3] = 0x0000000f;
1851 rs[WINED3D_RS_BLENDFACTOR] = 0xffffffff;
1852 rs[WINED3D_RS_SRGBWRITEENABLE] = 0;
1853 rs[WINED3D_RS_DEPTHBIAS] = 0;
1854 rs[WINED3D_RS_WRAP8] = 0;
1855 rs[WINED3D_RS_WRAP9] = 0;
1856 rs[WINED3D_RS_WRAP10] = 0;
1857 rs[WINED3D_RS_WRAP11] = 0;
1858 rs[WINED3D_RS_WRAP12] = 0;
1859 rs[WINED3D_RS_WRAP13] = 0;
1860 rs[WINED3D_RS_WRAP14] = 0;
1861 rs[WINED3D_RS_WRAP15] = 0;
1862 rs[WINED3D_RS_SEPARATEALPHABLENDENABLE] = FALSE;
1863 rs[WINED3D_RS_SRCBLENDALPHA] = WINED3D_BLEND_ONE;
1864 rs[WINED3D_RS_DESTBLENDALPHA] = WINED3D_BLEND_ZERO;
1865 rs[WINED3D_RS_BLENDOPALPHA] = WINED3D_BLEND_OP_ADD;
1868 static void init_default_texture_state(unsigned int i, uint32_t stage[WINED3D_HIGHEST_TEXTURE_STATE + 1])
1870 stage[WINED3D_TSS_COLOR_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_MODULATE;
1871 stage[WINED3D_TSS_COLOR_ARG1] = WINED3DTA_TEXTURE;
1872 stage[WINED3D_TSS_COLOR_ARG2] = WINED3DTA_CURRENT;
1873 stage[WINED3D_TSS_ALPHA_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_SELECT_ARG1;
1874 stage[WINED3D_TSS_ALPHA_ARG1] = WINED3DTA_TEXTURE;
1875 stage[WINED3D_TSS_ALPHA_ARG2] = WINED3DTA_CURRENT;
1876 stage[WINED3D_TSS_BUMPENV_MAT00] = 0;
1877 stage[WINED3D_TSS_BUMPENV_MAT01] = 0;
1878 stage[WINED3D_TSS_BUMPENV_MAT10] = 0;
1879 stage[WINED3D_TSS_BUMPENV_MAT11] = 0;
1880 stage[WINED3D_TSS_TEXCOORD_INDEX] = i;
1881 stage[WINED3D_TSS_BUMPENV_LSCALE] = 0;
1882 stage[WINED3D_TSS_BUMPENV_LOFFSET] = 0;
1883 stage[WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS] = WINED3D_TTFF_DISABLE;
1884 stage[WINED3D_TSS_COLOR_ARG0] = WINED3DTA_CURRENT;
1885 stage[WINED3D_TSS_ALPHA_ARG0] = WINED3DTA_CURRENT;
1886 stage[WINED3D_TSS_RESULT_ARG] = WINED3DTA_CURRENT;
1889 static void init_default_sampler_states(uint32_t states[WINED3D_MAX_COMBINED_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1])
1891 unsigned int i;
1893 for (i = 0 ; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1895 TRACE("Setting up default samplers states for sampler %u.\n", i);
1896 states[i][WINED3D_SAMP_ADDRESS_U] = WINED3D_TADDRESS_WRAP;
1897 states[i][WINED3D_SAMP_ADDRESS_V] = WINED3D_TADDRESS_WRAP;
1898 states[i][WINED3D_SAMP_ADDRESS_W] = WINED3D_TADDRESS_WRAP;
1899 states[i][WINED3D_SAMP_BORDER_COLOR] = 0;
1900 states[i][WINED3D_SAMP_MAG_FILTER] = WINED3D_TEXF_POINT;
1901 states[i][WINED3D_SAMP_MIN_FILTER] = WINED3D_TEXF_POINT;
1902 states[i][WINED3D_SAMP_MIP_FILTER] = WINED3D_TEXF_NONE;
1903 states[i][WINED3D_SAMP_MIPMAP_LOD_BIAS] = 0;
1904 states[i][WINED3D_SAMP_MAX_MIP_LEVEL] = 0;
1905 states[i][WINED3D_SAMP_MAX_ANISOTROPY] = 1;
1906 states[i][WINED3D_SAMP_SRGB_TEXTURE] = 0;
1907 /* TODO: Indicates which element of a multielement texture to use. */
1908 states[i][WINED3D_SAMP_ELEMENT_INDEX] = 0;
1909 /* TODO: Vertex offset in the presampled displacement map. */
1910 states[i][WINED3D_SAMP_DMAP_OFFSET] = 0;
1914 static void state_init_default(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info)
1916 struct wined3d_matrix identity;
1917 unsigned int i, j;
1919 TRACE("state %p, d3d_info %p.\n", state, d3d_info);
1921 get_identity_matrix(&identity);
1922 state->primitive_type = WINED3D_PT_UNDEFINED;
1923 state->patch_vertex_count = 0;
1925 /* Set some of the defaults for lights, transforms etc */
1926 state->transforms[WINED3D_TS_PROJECTION] = identity;
1927 state->transforms[WINED3D_TS_VIEW] = identity;
1928 for (i = 0; i < 256; ++i)
1930 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
1933 init_default_render_states(state->render_states, d3d_info);
1935 /* Texture Stage States - Put directly into state block, we will call function below */
1936 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1938 TRACE("Setting up default texture states for texture Stage %u.\n", i);
1939 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
1940 init_default_texture_state(i, state->texture_states[i]);
1943 init_default_sampler_states(state->sampler_states);
1945 state->blend_factor.r = 1.0f;
1946 state->blend_factor.g = 1.0f;
1947 state->blend_factor.b = 1.0f;
1948 state->blend_factor.a = 1.0f;
1950 state->sample_mask = 0xffffffff;
1952 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1953 state->streams[i].frequency = 1;
1955 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1957 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
1958 state->cb[i][j].size = WINED3D_MAX_CONSTANT_BUFFER_SIZE * 16;
1962 static int lights_compare(const void *key, const struct rb_entry *entry)
1964 const struct wined3d_light_info *light = RB_ENTRY_VALUE(entry, struct wined3d_light_info, entry);
1965 unsigned int original_index = (ULONG_PTR)key;
1967 return wined3d_uint32_compare(light->OriginalIndex, original_index);
1970 void state_init(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info,
1971 uint32_t flags, enum wined3d_feature_level feature_level)
1973 state->feature_level = feature_level;
1974 state->flags = flags;
1976 rb_init(&state->light_state.lights_tree, lights_compare);
1978 if (flags & WINED3D_STATE_INIT_DEFAULT)
1979 state_init_default(state, d3d_info);
1982 static bool wined3d_select_feature_level(const struct wined3d_adapter *adapter,
1983 const enum wined3d_feature_level *levels, unsigned int level_count,
1984 enum wined3d_feature_level *selected_level)
1986 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
1987 unsigned int i;
1989 for (i = 0; i < level_count; ++i)
1991 if (levels[i] && d3d_info->feature_level >= levels[i])
1993 *selected_level = levels[i];
1994 return true;
1998 FIXME_(winediag)("None of the requested D3D feature levels is supported on this GPU "
1999 "with the current shader backend.\n");
2000 return false;
2003 HRESULT CDECL wined3d_state_create(struct wined3d_device *device,
2004 const enum wined3d_feature_level *levels, unsigned int level_count, struct wined3d_state **state)
2006 enum wined3d_feature_level feature_level;
2007 struct wined3d_state *object;
2009 TRACE("device %p, levels %p, level_count %u, state %p.\n", device, levels, level_count, state);
2011 if (!wined3d_select_feature_level(device->adapter, levels, level_count, &feature_level))
2012 return E_FAIL;
2014 TRACE("Selected feature level %s.\n", wined3d_debug_feature_level(feature_level));
2016 if (!(object = heap_alloc_zero(sizeof(*object))))
2017 return E_OUTOFMEMORY;
2018 state_init(object, &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT, feature_level);
2020 *state = object;
2021 return S_OK;
2024 enum wined3d_feature_level CDECL wined3d_state_get_feature_level(const struct wined3d_state *state)
2026 TRACE("state %p.\n", state);
2028 return state->feature_level;
2031 void CDECL wined3d_state_destroy(struct wined3d_state *state)
2033 TRACE("state %p.\n", state);
2035 state_cleanup(state);
2036 heap_free(state);
2039 static void stateblock_state_init_default(struct wined3d_stateblock_state *state,
2040 const struct wined3d_d3d_info *d3d_info)
2042 struct wined3d_matrix identity;
2043 unsigned int i;
2045 get_identity_matrix(&identity);
2047 state->transforms[WINED3D_TS_PROJECTION] = identity;
2048 state->transforms[WINED3D_TS_VIEW] = identity;
2049 for (i = 0; i < 256; ++i)
2051 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
2054 init_default_render_states(state->rs, d3d_info);
2056 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
2058 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
2059 init_default_texture_state(i, state->texture_states[i]);
2062 init_default_sampler_states(state->sampler_states);
2064 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
2065 state->streams[i].frequency = 1;
2068 void wined3d_stateblock_state_init(struct wined3d_stateblock_state *state,
2069 const struct wined3d_device *device, uint32_t flags)
2071 rb_init(&state->light_state->lights_tree, lights_compare);
2073 if (flags & WINED3D_STATE_INIT_DEFAULT)
2074 stateblock_state_init_default(state, &device->adapter->d3d_info);
2078 static HRESULT stateblock_init(struct wined3d_stateblock *stateblock, const struct wined3d_stateblock *device_state,
2079 struct wined3d_device *device, enum wined3d_stateblock_type type)
2081 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2083 stateblock->ref = 1;
2084 stateblock->device = device;
2085 stateblock->stateblock_state.light_state = &stateblock->light_state;
2086 wined3d_stateblock_state_init(&stateblock->stateblock_state, device,
2087 type == WINED3D_SBT_PRIMARY ? WINED3D_STATE_INIT_DEFAULT : 0);
2089 stateblock->changed.store_stream_offset = 1;
2090 list_init(&stateblock->changed.changed_lights);
2092 if (type == WINED3D_SBT_RECORDED || type == WINED3D_SBT_PRIMARY)
2093 return WINED3D_OK;
2095 TRACE("Updating changed flags appropriate for type %#x.\n", type);
2097 switch (type)
2099 case WINED3D_SBT_ALL:
2100 stateblock_init_lights(stateblock, &device_state->stateblock_state.light_state->lights_tree);
2101 stateblock_savedstates_set_all(&stateblock->changed,
2102 d3d_info->limits.vs_uniform_count, d3d_info->limits.ps_uniform_count);
2103 break;
2105 case WINED3D_SBT_PIXEL_STATE:
2106 stateblock_savedstates_set_pixel(&stateblock->changed,
2107 d3d_info->limits.ps_uniform_count);
2108 break;
2110 case WINED3D_SBT_VERTEX_STATE:
2111 stateblock_init_lights(stateblock, &device_state->stateblock_state.light_state->lights_tree);
2112 stateblock_savedstates_set_vertex(&stateblock->changed,
2113 d3d_info->limits.vs_uniform_count);
2114 break;
2116 default:
2117 FIXME("Unrecognized state block type %#x.\n", type);
2118 break;
2121 wined3d_stateblock_init_contained_states(stateblock);
2122 wined3d_stateblock_capture(stateblock, device_state);
2124 /* According to the tests, stream offset is not updated in the captured state if
2125 * the state was captured on state block creation. This is not the case for
2126 * state blocks initialized with BeginStateBlock / EndStateBlock, multiple
2127 * captures get stream offsets updated. */
2128 stateblock->changed.store_stream_offset = 0;
2130 return WINED3D_OK;
2133 HRESULT CDECL wined3d_stateblock_create(struct wined3d_device *device, const struct wined3d_stateblock *device_state,
2134 enum wined3d_stateblock_type type, struct wined3d_stateblock **stateblock)
2136 struct wined3d_stateblock *object;
2137 HRESULT hr;
2139 TRACE("device %p, device_state %p, type %#x, stateblock %p.\n",
2140 device, device_state, type, stateblock);
2142 if (!(object = heap_alloc_zero(sizeof(*object))))
2143 return E_OUTOFMEMORY;
2145 hr = stateblock_init(object, device_state, device, type);
2146 if (FAILED(hr))
2148 WARN("Failed to initialize stateblock, hr %#lx.\n", hr);
2149 heap_free(object);
2150 return hr;
2153 TRACE("Created stateblock %p.\n", object);
2154 *stateblock = object;
2156 return WINED3D_OK;
2159 void CDECL wined3d_stateblock_reset(struct wined3d_stateblock *stateblock)
2161 TRACE("stateblock %p.\n", stateblock);
2163 wined3d_stateblock_state_cleanup(&stateblock->stateblock_state);
2164 memset(&stateblock->stateblock_state, 0, sizeof(stateblock->stateblock_state));
2165 stateblock->stateblock_state.light_state = &stateblock->light_state;
2166 wined3d_stateblock_state_init(&stateblock->stateblock_state, stateblock->device, WINED3D_STATE_INIT_DEFAULT);
2169 static void wined3d_device_set_base_vertex_index(struct wined3d_device *device, int base_index)
2171 TRACE("device %p, base_index %d.\n", device, base_index);
2173 device->cs->c.state->base_vertex_index = base_index;
2176 static void wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2177 unsigned int start_idx, unsigned int count, const BOOL *constants)
2179 unsigned int i;
2181 TRACE("device %p, start_idx %u, count %u, constants %p.\n", device, start_idx, count, constants);
2183 if (TRACE_ON(d3d))
2185 for (i = 0; i < count; ++i)
2186 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2189 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2192 static void wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2193 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2195 unsigned int i;
2197 TRACE("device %p, start_idx %u, count %u, constants %p.\n", device, start_idx, count, constants);
2199 if (TRACE_ON(d3d))
2201 for (i = 0; i < count; ++i)
2202 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2205 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2208 static void wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2209 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2211 unsigned int i;
2213 TRACE("device %p, start_idx %u, count %u, constants %p.\n", device, start_idx, count, constants);
2215 if (TRACE_ON(d3d))
2217 for (i = 0; i < count; ++i)
2218 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2221 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2224 static void wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2225 unsigned int start_idx, unsigned int count, const BOOL *constants)
2227 unsigned int i;
2229 TRACE("device %p, start_idx %u, count %u, constants %p.\n", device, start_idx, count, constants);
2231 if (TRACE_ON(d3d))
2233 for (i = 0; i < count; ++i)
2234 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2237 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2240 static void wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2241 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2243 unsigned int i;
2245 TRACE("device %p, start_idx %u, count %u, constants %p.\n", device, start_idx, count, constants);
2247 if (TRACE_ON(d3d))
2249 for (i = 0; i < count; ++i)
2250 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2253 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2256 static void wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2257 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2259 unsigned int i;
2261 TRACE("device %p, start_idx %u, count %u, constants %p.\n", device, start_idx, count, constants);
2263 if (TRACE_ON(d3d))
2265 for (i = 0; i < count; ++i)
2266 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2269 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2272 /* Note lights are real special cases. Although the device caps state only
2273 * e.g. 8 are supported, you can reference any indexes you want as long as
2274 * that number max are enabled at any one point in time. Therefore since the
2275 * indices can be anything, we need a hashmap of them. However, this causes
2276 * stateblock problems. When capturing the state block, I duplicate the
2277 * hashmap, but when recording, just build a chain pretty much of commands to
2278 * be replayed. */
2279 static void wined3d_device_context_set_light(struct wined3d_device_context *context,
2280 unsigned int light_idx, const struct wined3d_light *light)
2282 struct wined3d_light_info *object = NULL;
2283 float rho;
2285 if (FAILED(wined3d_light_state_set_light(&context->state->light_state, light_idx, light, &object)))
2286 return;
2288 /* Initialize the object. */
2289 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
2290 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
2291 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
2292 light_idx, light->type, debug_color(&light->diffuse),
2293 debug_color(&light->specular), debug_color(&light->ambient),
2294 light->position.x, light->position.y, light->position.z,
2295 light->direction.x, light->direction.y, light->direction.z,
2296 light->range, light->falloff, light->theta, light->phi);
2298 switch (light->type)
2300 case WINED3D_LIGHT_POINT:
2301 /* Position */
2302 object->position.x = light->position.x;
2303 object->position.y = light->position.y;
2304 object->position.z = light->position.z;
2305 object->position.w = 1.0f;
2306 object->cutoff = 180.0f;
2307 /* FIXME: Range */
2308 break;
2310 case WINED3D_LIGHT_DIRECTIONAL:
2311 /* Direction */
2312 object->direction.x = -light->direction.x;
2313 object->direction.y = -light->direction.y;
2314 object->direction.z = -light->direction.z;
2315 object->direction.w = 0.0f;
2316 object->exponent = 0.0f;
2317 object->cutoff = 180.0f;
2318 break;
2320 case WINED3D_LIGHT_SPOT:
2321 /* Position */
2322 object->position.x = light->position.x;
2323 object->position.y = light->position.y;
2324 object->position.z = light->position.z;
2325 object->position.w = 1.0f;
2327 /* Direction */
2328 object->direction.x = light->direction.x;
2329 object->direction.y = light->direction.y;
2330 object->direction.z = light->direction.z;
2331 object->direction.w = 0.0f;
2333 /* opengl-ish and d3d-ish spot lights use too different models
2334 * for the light "intensity" as a function of the angle towards
2335 * the main light direction, so we only can approximate very
2336 * roughly. However, spot lights are rather rarely used in games
2337 * (if ever used at all). Furthermore if still used, probably
2338 * nobody pays attention to such details. */
2339 if (!light->falloff)
2341 /* Falloff = 0 is easy, because d3d's and opengl's spot light
2342 * equations have the falloff resp. exponent parameter as an
2343 * exponent, so the spot light lighting will always be 1.0 for
2344 * both of them, and we don't have to care for the rest of the
2345 * rather complex calculation. */
2346 object->exponent = 0.0f;
2348 else
2350 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
2351 if (rho < 0.0001f)
2352 rho = 0.0001f;
2353 object->exponent = -0.3f / logf(cosf(rho / 2));
2356 if (object->exponent > 128.0f)
2357 object->exponent = 128.0f;
2359 object->cutoff = (float)(light->phi * 90 / M_PI);
2360 /* FIXME: Range */
2361 break;
2363 case WINED3D_LIGHT_PARALLELPOINT:
2364 object->position.x = light->position.x;
2365 object->position.y = light->position.y;
2366 object->position.z = light->position.z;
2367 object->position.w = 1.0f;
2368 break;
2370 default:
2371 FIXME("Unrecognized light type %#x.\n", light->type);
2374 wined3d_device_context_emit_set_light(context, object);
2377 static void wined3d_device_set_light_enable(struct wined3d_device *device, unsigned int light_idx, bool enable)
2379 struct wined3d_light_state *light_state = &device->cs->c.state->light_state;
2380 struct wined3d_light_info *light_info;
2382 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
2384 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
2385 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
2387 TRACE("Light enabled requested but light not defined, so defining one!\n");
2388 wined3d_device_context_set_light(&device->cs->c, light_idx, &WINED3D_default_light);
2390 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
2392 ERR("Adding default lights has failed dismally.\n");
2393 return;
2397 if (wined3d_light_state_enable_light(light_state, &device->adapter->d3d_info, light_info, enable))
2398 wined3d_device_context_emit_set_light_enable(&device->cs->c, light_idx, enable);
2401 static HRESULT wined3d_device_set_clip_plane(struct wined3d_device *device,
2402 unsigned int plane_idx, const struct wined3d_vec4 *plane)
2404 struct wined3d_vec4 *clip_planes = device->cs->c.state->clip_planes;
2406 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2408 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
2410 TRACE("Application has requested clipplane this device doesn't support.\n");
2411 return WINED3DERR_INVALIDCALL;
2414 if (!memcmp(&clip_planes[plane_idx], plane, sizeof(*plane)))
2416 TRACE("Application is setting old values over, nothing to do.\n");
2417 return WINED3D_OK;
2420 clip_planes[plane_idx] = *plane;
2422 wined3d_device_context_emit_set_clip_plane(&device->cs->c, plane_idx, plane);
2424 return WINED3D_OK;
2427 static void resolve_depth_buffer(struct wined3d_device *device)
2429 const struct wined3d_state *state = device->cs->c.state;
2430 struct wined3d_rendertarget_view *src_view;
2431 struct wined3d_resource *dst_resource;
2432 struct wined3d_texture *dst_texture;
2434 if (!(dst_texture = state->textures[0]))
2435 return;
2436 dst_resource = &dst_texture->resource;
2437 if (!dst_resource->format->depth_size)
2438 return;
2439 if (!(src_view = state->fb.depth_stencil))
2440 return;
2442 wined3d_device_context_resolve_sub_resource(&device->cs->c, dst_resource, 0,
2443 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
2446 static void wined3d_device_set_render_state(struct wined3d_device *device,
2447 enum wined3d_render_state state, unsigned int value)
2449 if (state > WINEHIGHEST_RENDER_STATE)
2451 WARN("Unhandled render state %#x.\n", state);
2452 return;
2455 if (value == device->cs->c.state->render_states[state])
2457 TRACE("Application is setting the old value over, nothing to do.\n");
2459 else
2461 device->cs->c.state->render_states[state] = value;
2462 wined3d_device_context_emit_set_render_state(&device->cs->c, state, value);
2465 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2467 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2468 resolve_depth_buffer(device);
2472 static void wined3d_device_set_texture_stage_state(struct wined3d_device *device,
2473 unsigned int stage, enum wined3d_texture_stage_state state, uint32_t value)
2475 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2477 TRACE("device %p, stage %u, state %s, value %#x.\n",
2478 device, stage, debug_d3dtexturestate(state), value);
2480 if (stage >= d3d_info->limits.ffp_blend_stages)
2482 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
2483 stage, d3d_info->limits.ffp_blend_stages - 1);
2484 return;
2487 if (value == device->cs->c.state->texture_states[stage][state])
2489 TRACE("Application is setting the old value over, nothing to do.\n");
2490 return;
2493 device->cs->c.state->texture_states[stage][state] = value;
2495 wined3d_device_context_emit_set_texture_state(&device->cs->c, stage, state, value);
2498 static void wined3d_device_set_sampler_state(struct wined3d_device *device,
2499 unsigned int sampler_idx, enum wined3d_sampler_state state, unsigned int value)
2501 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2502 device, sampler_idx, debug_d3dsamplerstate(state), value);
2504 if (value == device->cs->c.state->sampler_states[sampler_idx][state])
2506 TRACE("Application is setting the old value over, nothing to do.\n");
2507 return;
2510 device->cs->c.state->sampler_states[sampler_idx][state] = value;
2511 wined3d_device_context_emit_set_sampler_state(&device->cs->c, sampler_idx, state, value);
2514 static void wined3d_device_set_texture(struct wined3d_device *device,
2515 unsigned int stage, struct wined3d_texture *texture)
2517 struct wined3d_state *state = device->cs->c.state;
2518 struct wined3d_texture *prev;
2520 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
2522 if (stage >= ARRAY_SIZE(state->textures))
2524 WARN("Ignoring invalid stage %u.\n", stage);
2525 return;
2528 prev = state->textures[stage];
2529 TRACE("Previous texture %p.\n", prev);
2531 if (texture == prev)
2533 TRACE("App is setting the same texture again, nothing to do.\n");
2534 return;
2537 TRACE("Setting new texture to %p.\n", texture);
2538 state->textures[stage] = texture;
2540 if (texture)
2541 wined3d_texture_incref(texture);
2542 wined3d_device_context_emit_set_texture(&device->cs->c, stage, texture);
2543 if (prev)
2544 wined3d_texture_decref(prev);
2546 return;
2549 static void wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
2551 TRACE("device %p, material %p.\n", device, material);
2553 device->cs->c.state->material = *material;
2554 wined3d_device_context_emit_set_material(&device->cs->c, material);
2557 static void wined3d_device_set_transform(struct wined3d_device *device,
2558 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
2560 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
2561 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
2562 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
2563 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
2564 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
2566 /* If the new matrix is the same as the current one,
2567 * we cut off any further processing. this seems to be a reasonable
2568 * optimization because as was noticed, some apps (warcraft3 for example)
2569 * tend towards setting the same matrix repeatedly for some reason.
2571 * From here on we assume that the new matrix is different, wherever it matters. */
2572 if (!memcmp(&device->cs->c.state->transforms[state], matrix, sizeof(*matrix)))
2574 TRACE("The application is setting the same matrix over again.\n");
2575 return;
2578 device->cs->c.state->transforms[state] = *matrix;
2579 wined3d_device_context_emit_set_transform(&device->cs->c, state, matrix);
2582 void CDECL wined3d_device_apply_stateblock(struct wined3d_device *device,
2583 struct wined3d_stateblock *stateblock)
2585 bool set_blend_state = false, set_depth_stencil_state = false, set_rasterizer_state = false;
2586 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
2587 const struct wined3d_saved_states *changed = &stateblock->changed;
2588 const unsigned int word_bit_count = sizeof(DWORD) * CHAR_BIT;
2589 struct wined3d_device_context *context = &device->cs->c;
2590 unsigned int i, j, start, idx;
2591 bool set_depth_bounds = false;
2592 struct wined3d_range range;
2593 uint32_t map;
2595 TRACE("device %p, stateblock %p.\n", device, stateblock);
2597 if (changed->vertexShader)
2598 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_VERTEX, state->vs);
2599 if (changed->pixelShader)
2600 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_PIXEL, state->ps);
2602 for (start = 0; ; start = range.offset + range.size)
2604 if (!wined3d_bitmap_get_range(changed->vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
2605 break;
2607 wined3d_device_set_vs_consts_f(device, range.offset, range.size, &state->vs_consts_f[range.offset]);
2610 map = changed->vertexShaderConstantsI;
2611 for (start = 0; ; start = range.offset + range.size)
2613 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
2614 break;
2616 wined3d_device_set_vs_consts_i(device, range.offset, range.size, &state->vs_consts_i[range.offset]);
2619 map = changed->vertexShaderConstantsB;
2620 for (start = 0; ; start = range.offset + range.size)
2622 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
2623 break;
2625 wined3d_device_set_vs_consts_b(device, range.offset, range.size, &state->vs_consts_b[range.offset]);
2628 for (start = 0; ; start = range.offset + range.size)
2630 if (!wined3d_bitmap_get_range(changed->ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
2631 break;
2633 wined3d_device_set_ps_consts_f(device, range.offset, range.size, &state->ps_consts_f[range.offset]);
2636 map = changed->pixelShaderConstantsI;
2637 for (start = 0; ; start = range.offset + range.size)
2639 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
2640 break;
2642 wined3d_device_set_ps_consts_i(device, range.offset, range.size, &state->ps_consts_i[range.offset]);
2645 map = changed->pixelShaderConstantsB;
2646 for (start = 0; ; start = range.offset + range.size)
2648 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
2649 break;
2651 wined3d_device_set_ps_consts_b(device, range.offset, range.size, &state->ps_consts_b[range.offset]);
2654 if (changed->lights)
2656 struct wined3d_light_info *light, *cursor;
2658 LIST_FOR_EACH_ENTRY_SAFE(light, cursor, &changed->changed_lights, struct wined3d_light_info, changed_entry)
2660 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
2661 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
2662 list_remove(&light->changed_entry);
2663 light->changed = false;
2667 for (i = 0; i < ARRAY_SIZE(changed->renderState); ++i)
2669 map = changed->renderState[i];
2670 while (map)
2672 j = wined3d_bit_scan(&map);
2673 idx = i * word_bit_count + j;
2675 switch (idx)
2677 case WINED3D_RS_BLENDFACTOR:
2678 case WINED3D_RS_MULTISAMPLEMASK:
2679 case WINED3D_RS_ALPHABLENDENABLE:
2680 case WINED3D_RS_SRCBLEND:
2681 case WINED3D_RS_DESTBLEND:
2682 case WINED3D_RS_BLENDOP:
2683 case WINED3D_RS_SEPARATEALPHABLENDENABLE:
2684 case WINED3D_RS_SRCBLENDALPHA:
2685 case WINED3D_RS_DESTBLENDALPHA:
2686 case WINED3D_RS_BLENDOPALPHA:
2687 case WINED3D_RS_COLORWRITEENABLE:
2688 case WINED3D_RS_COLORWRITEENABLE1:
2689 case WINED3D_RS_COLORWRITEENABLE2:
2690 case WINED3D_RS_COLORWRITEENABLE3:
2691 set_blend_state = true;
2692 break;
2694 case WINED3D_RS_BACK_STENCILFAIL:
2695 case WINED3D_RS_BACK_STENCILFUNC:
2696 case WINED3D_RS_BACK_STENCILPASS:
2697 case WINED3D_RS_BACK_STENCILZFAIL:
2698 case WINED3D_RS_STENCILENABLE:
2699 case WINED3D_RS_STENCILFAIL:
2700 case WINED3D_RS_STENCILFUNC:
2701 case WINED3D_RS_STENCILREF:
2702 case WINED3D_RS_STENCILMASK:
2703 case WINED3D_RS_STENCILPASS:
2704 case WINED3D_RS_STENCILWRITEMASK:
2705 case WINED3D_RS_STENCILZFAIL:
2706 case WINED3D_RS_TWOSIDEDSTENCILMODE:
2707 case WINED3D_RS_ZENABLE:
2708 case WINED3D_RS_ZFUNC:
2709 case WINED3D_RS_ZWRITEENABLE:
2710 set_depth_stencil_state = true;
2711 break;
2713 case WINED3D_RS_FILLMODE:
2714 case WINED3D_RS_CULLMODE:
2715 case WINED3D_RS_SLOPESCALEDEPTHBIAS:
2716 case WINED3D_RS_DEPTHBIAS:
2717 case WINED3D_RS_SCISSORTESTENABLE:
2718 case WINED3D_RS_ANTIALIASEDLINEENABLE:
2719 set_rasterizer_state = true;
2720 break;
2722 case WINED3D_RS_ADAPTIVETESS_X:
2723 case WINED3D_RS_ADAPTIVETESS_Z:
2724 case WINED3D_RS_ADAPTIVETESS_W:
2725 set_depth_bounds = true;
2726 break;
2728 case WINED3D_RS_ADAPTIVETESS_Y:
2729 break;
2731 case WINED3D_RS_ANTIALIAS:
2732 if (state->rs[WINED3D_RS_ANTIALIAS])
2733 FIXME("Antialias not supported yet.\n");
2734 break;
2736 case WINED3D_RS_TEXTUREPERSPECTIVE:
2737 break;
2739 case WINED3D_RS_WRAPU:
2740 if (state->rs[WINED3D_RS_WRAPU])
2741 FIXME("Render state WINED3D_RS_WRAPU not implemented yet.\n");
2742 break;
2744 case WINED3D_RS_WRAPV:
2745 if (state->rs[WINED3D_RS_WRAPV])
2746 FIXME("Render state WINED3D_RS_WRAPV not implemented yet.\n");
2747 break;
2749 case WINED3D_RS_MONOENABLE:
2750 if (state->rs[WINED3D_RS_MONOENABLE])
2751 FIXME("Render state WINED3D_RS_MONOENABLE not implemented yet.\n");
2752 break;
2754 case WINED3D_RS_ROP2:
2755 if (state->rs[WINED3D_RS_ROP2])
2756 FIXME("Render state WINED3D_RS_ROP2 not implemented yet.\n");
2757 break;
2759 case WINED3D_RS_PLANEMASK:
2760 if (state->rs[WINED3D_RS_PLANEMASK])
2761 FIXME("Render state WINED3D_RS_PLANEMASK not implemented yet.\n");
2762 break;
2764 case WINED3D_RS_LASTPIXEL:
2765 if (!state->rs[WINED3D_RS_LASTPIXEL])
2767 static bool warned;
2768 if (!warned)
2770 FIXME("Last Pixel Drawing Disabled, not handled yet.\n");
2771 warned = true;
2774 break;
2776 case WINED3D_RS_ZVISIBLE:
2777 if (state->rs[WINED3D_RS_ZVISIBLE])
2778 FIXME("WINED3D_RS_ZVISIBLE not implemented.\n");
2779 break;
2781 case WINED3D_RS_SUBPIXEL:
2782 if (state->rs[WINED3D_RS_SUBPIXEL])
2783 FIXME("Render state WINED3D_RS_SUBPIXEL not implemented yet.\n");
2784 break;
2786 case WINED3D_RS_SUBPIXELX:
2787 if (state->rs[WINED3D_RS_SUBPIXELX])
2788 FIXME("Render state WINED3D_RS_SUBPIXELX not implemented yet.\n");
2789 break;
2791 case WINED3D_RS_STIPPLEDALPHA:
2792 if (state->rs[WINED3D_RS_STIPPLEDALPHA])
2793 FIXME("Stippled Alpha not supported yet.\n");
2794 break;
2796 case WINED3D_RS_STIPPLEENABLE:
2797 if (state->rs[WINED3D_RS_STIPPLEENABLE])
2798 FIXME("Render state WINED3D_RS_STIPPLEENABLE not implemented yet.\n");
2799 break;
2801 case WINED3D_RS_MIPMAPLODBIAS:
2802 if (state->rs[WINED3D_RS_MIPMAPLODBIAS])
2803 FIXME("Render state WINED3D_RS_MIPMAPLODBIAS not implemented yet.\n");
2804 break;
2806 case WINED3D_RS_ANISOTROPY:
2807 if (state->rs[WINED3D_RS_ANISOTROPY])
2808 FIXME("Render state WINED3D_RS_ANISOTROPY not implemented yet.\n");
2809 break;
2811 case WINED3D_RS_FLUSHBATCH:
2812 if (state->rs[WINED3D_RS_FLUSHBATCH])
2813 FIXME("Render state WINED3D_RS_FLUSHBATCH not implemented yet.\n");
2814 break;
2816 case WINED3D_RS_TRANSLUCENTSORTINDEPENDENT:
2817 if (state->rs[WINED3D_RS_TRANSLUCENTSORTINDEPENDENT])
2818 FIXME("Render state WINED3D_RS_TRANSLUCENTSORTINDEPENDENT not implemented yet.\n");
2819 break;
2821 case WINED3D_RS_WRAP0:
2822 case WINED3D_RS_WRAP1:
2823 case WINED3D_RS_WRAP2:
2824 case WINED3D_RS_WRAP3:
2825 case WINED3D_RS_WRAP4:
2826 case WINED3D_RS_WRAP5:
2827 case WINED3D_RS_WRAP6:
2828 case WINED3D_RS_WRAP7:
2829 case WINED3D_RS_WRAP8:
2830 case WINED3D_RS_WRAP9:
2831 case WINED3D_RS_WRAP10:
2832 case WINED3D_RS_WRAP11:
2833 case WINED3D_RS_WRAP12:
2834 case WINED3D_RS_WRAP13:
2835 case WINED3D_RS_WRAP14:
2836 case WINED3D_RS_WRAP15:
2838 static unsigned int once;
2840 if ((state->rs[idx]) && !once++)
2841 FIXME("(WINED3D_RS_WRAP0) Texture wrapping not yet supported.\n");
2842 break;
2845 case WINED3D_RS_EXTENTS:
2846 if (state->rs[WINED3D_RS_EXTENTS])
2847 FIXME("Render state WINED3D_RS_EXTENTS not implemented yet.\n");
2848 break;
2850 case WINED3D_RS_COLORKEYBLENDENABLE:
2851 if (state->rs[WINED3D_RS_COLORKEYBLENDENABLE])
2852 FIXME("Render state WINED3D_RS_COLORKEYBLENDENABLE not implemented yet.\n");
2853 break;
2855 case WINED3D_RS_SOFTWAREVERTEXPROCESSING:
2857 static unsigned int once;
2859 if ((state->rs[WINED3D_RS_SOFTWAREVERTEXPROCESSING]) && !once++)
2860 FIXME("Software vertex processing not implemented.\n");
2861 break;
2864 case WINED3D_RS_PATCHEDGESTYLE:
2865 if (state->rs[WINED3D_RS_PATCHEDGESTYLE] != WINED3D_PATCH_EDGE_DISCRETE)
2866 FIXME("WINED3D_RS_PATCHEDGESTYLE %#x not yet implemented.\n",
2867 state->rs[WINED3D_RS_PATCHEDGESTYLE]);
2868 break;
2870 case WINED3D_RS_PATCHSEGMENTS:
2872 union
2874 uint32_t d;
2875 float f;
2876 } tmpvalue;
2877 tmpvalue.f = 1.0f;
2879 if (state->rs[WINED3D_RS_PATCHSEGMENTS] != tmpvalue.d)
2881 static bool displayed = false;
2883 tmpvalue.d = state->rs[WINED3D_RS_PATCHSEGMENTS];
2884 if(!displayed)
2885 FIXME("(WINED3D_RS_PATCHSEGMENTS,%f) not yet implemented.\n", tmpvalue.f);
2887 displayed = true;
2889 break;
2892 case WINED3D_RS_DEBUGMONITORTOKEN:
2893 WARN("token: %#x.\n", state->rs[WINED3D_RS_DEBUGMONITORTOKEN]);
2894 break;
2896 case WINED3D_RS_INDEXEDVERTEXBLENDENABLE:
2897 break;
2899 case WINED3D_RS_TWEENFACTOR:
2900 break;
2902 case WINED3D_RS_POSITIONDEGREE:
2903 if (state->rs[WINED3D_RS_POSITIONDEGREE] != WINED3D_DEGREE_CUBIC)
2904 FIXME("WINED3D_RS_POSITIONDEGREE %#x not yet implemented.\n",
2905 state->rs[WINED3D_RS_POSITIONDEGREE]);
2906 break;
2908 case WINED3D_RS_NORMALDEGREE:
2909 if (state->rs[WINED3D_RS_NORMALDEGREE] != WINED3D_DEGREE_LINEAR)
2910 FIXME("WINED3D_RS_NORMALDEGREE %#x not yet implemented.\n",
2911 state->rs[WINED3D_RS_NORMALDEGREE]);
2912 break;
2914 case WINED3D_RS_MINTESSELLATIONLEVEL:
2915 break;
2917 case WINED3D_RS_MAXTESSELLATIONLEVEL:
2918 break;
2920 case WINED3D_RS_ENABLEADAPTIVETESSELLATION:
2921 if (state->rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION])
2922 FIXME("WINED3D_RS_ENABLEADAPTIVETESSELLATION %#x not yet implemented.\n",
2923 state->rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION]);
2924 break;
2926 default:
2927 wined3d_device_set_render_state(device, idx, state->rs[idx]);
2928 break;
2933 if (set_rasterizer_state)
2935 struct wined3d_rasterizer_state *rasterizer_state;
2936 struct wined3d_rasterizer_state_desc desc;
2937 struct wine_rb_entry *entry;
2938 union
2940 DWORD d;
2941 float f;
2942 } bias;
2944 memset(&desc, 0, sizeof(desc));
2945 desc.fill_mode = state->rs[WINED3D_RS_FILLMODE];
2946 desc.cull_mode = state->rs[WINED3D_RS_CULLMODE];
2947 bias.d = state->rs[WINED3D_RS_DEPTHBIAS];
2948 desc.depth_bias = bias.f;
2949 bias.d = state->rs[WINED3D_RS_SLOPESCALEDEPTHBIAS];
2950 desc.scale_bias = bias.f;
2951 desc.depth_clip = TRUE;
2952 desc.scissor = state->rs[WINED3D_RS_SCISSORTESTENABLE];
2953 desc.line_antialias = state->rs[WINED3D_RS_ANTIALIASEDLINEENABLE];
2955 if ((entry = wine_rb_get(&device->rasterizer_states, &desc)))
2957 rasterizer_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
2958 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
2960 else if (SUCCEEDED(wined3d_rasterizer_state_create(device, &desc, NULL,
2961 &wined3d_null_parent_ops, &rasterizer_state)))
2963 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
2964 if (wine_rb_put(&device->rasterizer_states, &desc, &rasterizer_state->entry) == -1)
2966 ERR("Failed to insert rasterizer state.\n");
2967 wined3d_rasterizer_state_decref(rasterizer_state);
2972 if (set_blend_state || changed->alpha_to_coverage
2973 || wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_ADAPTIVETESS_Y))
2975 struct wined3d_blend_state *blend_state;
2976 struct wined3d_blend_state_desc desc;
2977 struct wine_rb_entry *entry;
2978 struct wined3d_color colour;
2979 unsigned int sample_mask;
2981 memset(&desc, 0, sizeof(desc));
2982 desc.alpha_to_coverage = state->alpha_to_coverage;
2983 desc.independent = FALSE;
2984 if (state->rs[WINED3D_RS_ADAPTIVETESS_Y] == WINED3DFMT_ATOC)
2985 desc.alpha_to_coverage = TRUE;
2986 desc.rt[0].enable = state->rs[WINED3D_RS_ALPHABLENDENABLE];
2987 desc.rt[0].src = state->rs[WINED3D_RS_SRCBLEND];
2988 desc.rt[0].dst = state->rs[WINED3D_RS_DESTBLEND];
2989 desc.rt[0].op = state->rs[WINED3D_RS_BLENDOP];
2990 if (state->rs[WINED3D_RS_SEPARATEALPHABLENDENABLE])
2992 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLENDALPHA];
2993 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLENDALPHA];
2994 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOPALPHA];
2996 else
2998 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLEND];
2999 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLEND];
3000 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOP];
3002 desc.rt[0].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE];
3003 desc.rt[1].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE1];
3004 desc.rt[2].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE2];
3005 desc.rt[3].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE3];
3006 if (desc.rt[1].writemask != desc.rt[0].writemask
3007 || desc.rt[2].writemask != desc.rt[0].writemask
3008 || desc.rt[3].writemask != desc.rt[0].writemask)
3010 desc.independent = TRUE;
3011 for (i = 1; i < 4; ++i)
3013 desc.rt[i].enable = desc.rt[0].enable;
3014 desc.rt[i].src = desc.rt[0].src;
3015 desc.rt[i].dst = desc.rt[0].dst;
3016 desc.rt[i].op = desc.rt[0].op;
3017 desc.rt[i].src_alpha = desc.rt[0].src_alpha;
3018 desc.rt[i].dst_alpha = desc.rt[0].dst_alpha;
3019 desc.rt[i].op_alpha = desc.rt[0].op_alpha;
3023 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_BLENDFACTOR))
3024 wined3d_color_from_d3dcolor(&colour, state->rs[WINED3D_RS_BLENDFACTOR]);
3025 else
3026 wined3d_device_context_get_blend_state(context, &colour, &sample_mask);
3028 if ((entry = wine_rb_get(&device->blend_states, &desc)))
3030 blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
3031 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3032 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3034 else if (SUCCEEDED(wined3d_blend_state_create(device, &desc, NULL,
3035 &wined3d_null_parent_ops, &blend_state)))
3037 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3038 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3039 if (wine_rb_put(&device->blend_states, &desc, &blend_state->entry) == -1)
3041 ERR("Failed to insert blend state.\n");
3042 wined3d_blend_state_decref(blend_state);
3047 if (set_depth_stencil_state)
3049 struct wined3d_depth_stencil_state *depth_stencil_state;
3050 struct wined3d_depth_stencil_state_desc desc;
3051 struct wine_rb_entry *entry;
3052 unsigned int stencil_ref;
3054 memset(&desc, 0, sizeof(desc));
3055 switch (state->rs[WINED3D_RS_ZENABLE])
3057 case WINED3D_ZB_FALSE:
3058 desc.depth = FALSE;
3059 break;
3061 case WINED3D_ZB_USEW:
3062 FIXME("W buffer is not well handled.\n");
3063 case WINED3D_ZB_TRUE:
3064 desc.depth = TRUE;
3065 break;
3067 default:
3068 FIXME("Unrecognized depth buffer type %#x.\n", state->rs[WINED3D_RS_ZENABLE]);
3070 desc.depth_write = state->rs[WINED3D_RS_ZWRITEENABLE];
3071 desc.depth_func = state->rs[WINED3D_RS_ZFUNC];
3072 desc.stencil = state->rs[WINED3D_RS_STENCILENABLE];
3073 desc.stencil_read_mask = state->rs[WINED3D_RS_STENCILMASK];
3074 desc.stencil_write_mask = state->rs[WINED3D_RS_STENCILWRITEMASK];
3075 desc.front.fail_op = state->rs[WINED3D_RS_STENCILFAIL];
3076 desc.front.depth_fail_op = state->rs[WINED3D_RS_STENCILZFAIL];
3077 desc.front.pass_op = state->rs[WINED3D_RS_STENCILPASS];
3078 desc.front.func = state->rs[WINED3D_RS_STENCILFUNC];
3080 if (state->rs[WINED3D_RS_TWOSIDEDSTENCILMODE])
3082 desc.back.fail_op = state->rs[WINED3D_RS_BACK_STENCILFAIL];
3083 desc.back.depth_fail_op = state->rs[WINED3D_RS_BACK_STENCILZFAIL];
3084 desc.back.pass_op = state->rs[WINED3D_RS_BACK_STENCILPASS];
3085 desc.back.func = state->rs[WINED3D_RS_BACK_STENCILFUNC];
3087 else
3089 desc.back = desc.front;
3092 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_STENCILREF))
3093 stencil_ref = state->rs[WINED3D_RS_STENCILREF];
3094 else
3095 wined3d_device_context_get_depth_stencil_state(context, &stencil_ref);
3097 if ((entry = wine_rb_get(&device->depth_stencil_states, &desc)))
3099 depth_stencil_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
3100 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3102 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device, &desc, NULL,
3103 &wined3d_null_parent_ops, &depth_stencil_state)))
3105 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3106 if (wine_rb_put(&device->depth_stencil_states, &desc, &depth_stencil_state->entry) == -1)
3108 ERR("Failed to insert depth/stencil state.\n");
3109 wined3d_depth_stencil_state_decref(depth_stencil_state);
3114 if (set_depth_bounds)
3116 wined3d_device_context_set_depth_bounds(context,
3117 state->rs[WINED3D_RS_ADAPTIVETESS_X] == WINED3DFMT_NVDB,
3118 int_to_float(state->rs[WINED3D_RS_ADAPTIVETESS_Z]),
3119 int_to_float(state->rs[WINED3D_RS_ADAPTIVETESS_W]));
3122 for (i = 0; i < ARRAY_SIZE(changed->textureState); ++i)
3124 map = changed->textureState[i];
3125 while (map)
3127 j = wined3d_bit_scan(&map);
3128 wined3d_device_set_texture_stage_state(device, i, j, state->texture_states[i][j]);
3132 for (i = 0; i < ARRAY_SIZE(changed->samplerState); ++i)
3134 map = changed->samplerState[i];
3135 while (map)
3137 j = wined3d_bit_scan(&map);
3138 wined3d_device_set_sampler_state(device, i, j, state->sampler_states[i][j]);
3142 if (changed->transforms)
3144 for (i = 0; i < ARRAY_SIZE(changed->transform); ++i)
3146 map = changed->transform[i];
3147 while (map)
3149 j = wined3d_bit_scan(&map);
3150 idx = i * word_bit_count + j;
3151 wined3d_device_set_transform(device, idx, &state->transforms[idx]);
3156 if (changed->indices)
3157 wined3d_device_context_set_index_buffer(context, state->index_buffer, state->index_format, 0);
3158 wined3d_device_set_base_vertex_index(device, state->base_vertex_index);
3159 if (changed->vertexDecl)
3160 wined3d_device_context_set_vertex_declaration(context, state->vertex_declaration);
3161 if (changed->material)
3162 wined3d_device_set_material(device, &state->material);
3163 if (changed->viewport)
3164 wined3d_device_context_set_viewports(context, 1, &state->viewport);
3165 if (changed->scissorRect)
3166 wined3d_device_context_set_scissor_rects(context, 1, &state->scissor_rect);
3168 map = changed->streamSource | changed->streamFreq;
3169 while (map)
3171 i = wined3d_bit_scan(&map);
3172 wined3d_device_context_set_stream_sources(context, i, 1, &state->streams[i]);
3175 map = changed->textures;
3176 while (map)
3178 i = wined3d_bit_scan(&map);
3179 wined3d_device_set_texture(device, i, state->textures[i]);
3182 map = changed->clipplane;
3183 while (map)
3185 i = wined3d_bit_scan(&map);
3186 wined3d_device_set_clip_plane(device, i, &state->clip_planes[i]);
3189 assert(list_empty(&stateblock->changed.changed_lights));
3190 memset(&stateblock->changed, 0, sizeof(stateblock->changed));
3191 list_init(&stateblock->changed.changed_lights);
3193 TRACE("Applied stateblock %p.\n", stateblock);