po: Update Finnish translation.
[wine.git] / dlls / wined3d / stateblock.c
blob1b82d387de800fe2d3a730fd792ab713111aca36
1 /*
2 * state block implementation
4 * Copyright 2002 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2007 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(winediag);
30 static const DWORD pixel_states_render[] =
32 WINED3D_RS_ALPHABLENDENABLE,
33 WINED3D_RS_ALPHAFUNC,
34 WINED3D_RS_ALPHAREF,
35 WINED3D_RS_ALPHATESTENABLE,
36 WINED3D_RS_ANTIALIASEDLINEENABLE,
37 WINED3D_RS_BLENDFACTOR,
38 WINED3D_RS_BLENDOP,
39 WINED3D_RS_BLENDOPALPHA,
40 WINED3D_RS_BACK_STENCILFAIL,
41 WINED3D_RS_BACK_STENCILPASS,
42 WINED3D_RS_BACK_STENCILZFAIL,
43 WINED3D_RS_COLORWRITEENABLE,
44 WINED3D_RS_COLORWRITEENABLE1,
45 WINED3D_RS_COLORWRITEENABLE2,
46 WINED3D_RS_COLORWRITEENABLE3,
47 WINED3D_RS_DEPTHBIAS,
48 WINED3D_RS_DESTBLEND,
49 WINED3D_RS_DESTBLENDALPHA,
50 WINED3D_RS_DITHERENABLE,
51 WINED3D_RS_FILLMODE,
52 WINED3D_RS_FOGDENSITY,
53 WINED3D_RS_FOGEND,
54 WINED3D_RS_FOGSTART,
55 WINED3D_RS_LASTPIXEL,
56 WINED3D_RS_SCISSORTESTENABLE,
57 WINED3D_RS_SEPARATEALPHABLENDENABLE,
58 WINED3D_RS_SHADEMODE,
59 WINED3D_RS_SLOPESCALEDEPTHBIAS,
60 WINED3D_RS_SRCBLEND,
61 WINED3D_RS_SRCBLENDALPHA,
62 WINED3D_RS_SRGBWRITEENABLE,
63 WINED3D_RS_STENCILENABLE,
64 WINED3D_RS_STENCILFAIL,
65 WINED3D_RS_STENCILFUNC,
66 WINED3D_RS_STENCILMASK,
67 WINED3D_RS_STENCILPASS,
68 WINED3D_RS_STENCILREF,
69 WINED3D_RS_STENCILWRITEMASK,
70 WINED3D_RS_STENCILZFAIL,
71 WINED3D_RS_TEXTUREFACTOR,
72 WINED3D_RS_TWOSIDEDSTENCILMODE,
73 WINED3D_RS_WRAP0,
74 WINED3D_RS_WRAP1,
75 WINED3D_RS_WRAP10,
76 WINED3D_RS_WRAP11,
77 WINED3D_RS_WRAP12,
78 WINED3D_RS_WRAP13,
79 WINED3D_RS_WRAP14,
80 WINED3D_RS_WRAP15,
81 WINED3D_RS_WRAP2,
82 WINED3D_RS_WRAP3,
83 WINED3D_RS_WRAP4,
84 WINED3D_RS_WRAP5,
85 WINED3D_RS_WRAP6,
86 WINED3D_RS_WRAP7,
87 WINED3D_RS_WRAP8,
88 WINED3D_RS_WRAP9,
89 WINED3D_RS_ZENABLE,
90 WINED3D_RS_ZFUNC,
91 WINED3D_RS_ZWRITEENABLE,
94 static const DWORD pixel_states_texture[] =
96 WINED3D_TSS_ALPHA_ARG0,
97 WINED3D_TSS_ALPHA_ARG1,
98 WINED3D_TSS_ALPHA_ARG2,
99 WINED3D_TSS_ALPHA_OP,
100 WINED3D_TSS_BUMPENV_LOFFSET,
101 WINED3D_TSS_BUMPENV_LSCALE,
102 WINED3D_TSS_BUMPENV_MAT00,
103 WINED3D_TSS_BUMPENV_MAT01,
104 WINED3D_TSS_BUMPENV_MAT10,
105 WINED3D_TSS_BUMPENV_MAT11,
106 WINED3D_TSS_COLOR_ARG0,
107 WINED3D_TSS_COLOR_ARG1,
108 WINED3D_TSS_COLOR_ARG2,
109 WINED3D_TSS_COLOR_OP,
110 WINED3D_TSS_RESULT_ARG,
111 WINED3D_TSS_TEXCOORD_INDEX,
112 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,
115 static const DWORD pixel_states_sampler[] =
117 WINED3D_SAMP_ADDRESS_U,
118 WINED3D_SAMP_ADDRESS_V,
119 WINED3D_SAMP_ADDRESS_W,
120 WINED3D_SAMP_BORDER_COLOR,
121 WINED3D_SAMP_MAG_FILTER,
122 WINED3D_SAMP_MIN_FILTER,
123 WINED3D_SAMP_MIP_FILTER,
124 WINED3D_SAMP_MIPMAP_LOD_BIAS,
125 WINED3D_SAMP_MAX_MIP_LEVEL,
126 WINED3D_SAMP_MAX_ANISOTROPY,
127 WINED3D_SAMP_SRGB_TEXTURE,
128 WINED3D_SAMP_ELEMENT_INDEX,
131 static const DWORD vertex_states_render[] =
133 WINED3D_RS_ADAPTIVETESS_W,
134 WINED3D_RS_ADAPTIVETESS_X,
135 WINED3D_RS_ADAPTIVETESS_Y,
136 WINED3D_RS_ADAPTIVETESS_Z,
137 WINED3D_RS_AMBIENT,
138 WINED3D_RS_AMBIENTMATERIALSOURCE,
139 WINED3D_RS_CLIPPING,
140 WINED3D_RS_CLIPPLANEENABLE,
141 WINED3D_RS_COLORVERTEX,
142 WINED3D_RS_CULLMODE,
143 WINED3D_RS_DIFFUSEMATERIALSOURCE,
144 WINED3D_RS_EMISSIVEMATERIALSOURCE,
145 WINED3D_RS_ENABLEADAPTIVETESSELLATION,
146 WINED3D_RS_FOGCOLOR,
147 WINED3D_RS_FOGDENSITY,
148 WINED3D_RS_FOGENABLE,
149 WINED3D_RS_FOGEND,
150 WINED3D_RS_FOGSTART,
151 WINED3D_RS_FOGTABLEMODE,
152 WINED3D_RS_FOGVERTEXMODE,
153 WINED3D_RS_INDEXEDVERTEXBLENDENABLE,
154 WINED3D_RS_LIGHTING,
155 WINED3D_RS_LOCALVIEWER,
156 WINED3D_RS_MAXTESSELLATIONLEVEL,
157 WINED3D_RS_MINTESSELLATIONLEVEL,
158 WINED3D_RS_MULTISAMPLEANTIALIAS,
159 WINED3D_RS_MULTISAMPLEMASK,
160 WINED3D_RS_NORMALDEGREE,
161 WINED3D_RS_NORMALIZENORMALS,
162 WINED3D_RS_PATCHEDGESTYLE,
163 WINED3D_RS_POINTSCALE_A,
164 WINED3D_RS_POINTSCALE_B,
165 WINED3D_RS_POINTSCALE_C,
166 WINED3D_RS_POINTSCALEENABLE,
167 WINED3D_RS_POINTSIZE,
168 WINED3D_RS_POINTSIZE_MAX,
169 WINED3D_RS_POINTSIZE_MIN,
170 WINED3D_RS_POINTSPRITEENABLE,
171 WINED3D_RS_POSITIONDEGREE,
172 WINED3D_RS_RANGEFOGENABLE,
173 WINED3D_RS_SHADEMODE,
174 WINED3D_RS_SPECULARENABLE,
175 WINED3D_RS_SPECULARMATERIALSOURCE,
176 WINED3D_RS_TWEENFACTOR,
177 WINED3D_RS_VERTEXBLEND,
180 static const DWORD vertex_states_texture[] =
182 WINED3D_TSS_TEXCOORD_INDEX,
183 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,
186 static const DWORD vertex_states_sampler[] =
188 WINED3D_SAMP_DMAP_OFFSET,
191 static inline void stateblock_set_all_bits(DWORD *map, UINT map_size)
193 DWORD mask = (1u << (map_size & 0x1f)) - 1;
194 memset(map, 0xff, (map_size >> 5) * sizeof(*map));
195 if (mask) map[map_size >> 5] = mask;
198 /* Set all members of a stateblock savedstate to the given value */
199 static void stateblock_savedstates_set_all(struct wined3d_saved_states *states, DWORD vs_consts, DWORD ps_consts)
201 unsigned int i;
203 states->indices = 1;
204 states->material = 1;
205 states->viewport = 1;
206 states->vertexDecl = 1;
207 states->pixelShader = 1;
208 states->vertexShader = 1;
209 states->scissorRect = 1;
210 states->alpha_to_coverage = 1;
211 states->lights = 1;
212 states->transforms = 1;
214 states->streamSource = 0xffff;
215 states->streamFreq = 0xffff;
216 states->textures = 0xfffff;
217 stateblock_set_all_bits(states->transform, WINED3D_HIGHEST_TRANSFORM_STATE + 1);
218 stateblock_set_all_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
219 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
220 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3ffe;
221 states->clipplane = wined3d_mask_from_size(WINED3D_MAX_CLIP_DISTANCES);
222 states->pixelShaderConstantsB = 0xffff;
223 states->pixelShaderConstantsI = 0xffff;
224 states->vertexShaderConstantsB = 0xffff;
225 states->vertexShaderConstantsI = 0xffff;
227 memset(states->ps_consts_f, 0xffu, sizeof(states->ps_consts_f));
228 memset(states->vs_consts_f, 0xffu, sizeof(states->vs_consts_f));
231 static void stateblock_savedstates_set_pixel(struct wined3d_saved_states *states, const DWORD num_constants)
233 DWORD texture_mask = 0;
234 WORD sampler_mask = 0;
235 unsigned int i;
237 states->pixelShader = 1;
239 for (i = 0; i < ARRAY_SIZE(pixel_states_render); ++i)
241 DWORD rs = pixel_states_render[i];
242 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
245 for (i = 0; i < ARRAY_SIZE(pixel_states_texture); ++i)
246 texture_mask |= 1u << pixel_states_texture[i];
247 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
248 for (i = 0; i < ARRAY_SIZE(pixel_states_sampler); ++i)
249 sampler_mask |= 1u << pixel_states_sampler[i];
250 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
251 states->pixelShaderConstantsB = 0xffff;
252 states->pixelShaderConstantsI = 0xffff;
254 memset(states->ps_consts_f, 0xffu, sizeof(states->ps_consts_f));
257 static void stateblock_savedstates_set_vertex(struct wined3d_saved_states *states, const DWORD num_constants)
259 DWORD texture_mask = 0;
260 WORD sampler_mask = 0;
261 unsigned int i;
263 states->vertexDecl = 1;
264 states->vertexShader = 1;
265 states->alpha_to_coverage = 1;
266 states->lights = 1;
268 for (i = 0; i < ARRAY_SIZE(vertex_states_render); ++i)
270 DWORD rs = vertex_states_render[i];
271 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
274 for (i = 0; i < ARRAY_SIZE(vertex_states_texture); ++i)
275 texture_mask |= 1u << vertex_states_texture[i];
276 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
277 for (i = 0; i < ARRAY_SIZE(vertex_states_sampler); ++i)
278 sampler_mask |= 1u << vertex_states_sampler[i];
279 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
280 states->vertexShaderConstantsB = 0xffff;
281 states->vertexShaderConstantsI = 0xffff;
283 memset(states->vs_consts_f, 0xffu, sizeof(states->vs_consts_f));
286 void CDECL wined3d_stateblock_init_contained_states(struct wined3d_stateblock *stateblock)
288 unsigned int i, j;
290 for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
292 DWORD map = stateblock->changed.renderState[i];
293 for (j = 0; map; map >>= 1, ++j)
295 if (!(map & 1)) continue;
297 stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
298 ++stateblock->num_contained_render_states;
302 for (i = 0; i <= WINED3D_HIGHEST_TRANSFORM_STATE >> 5; ++i)
304 DWORD map = stateblock->changed.transform[i];
305 for (j = 0; map; map >>= 1, ++j)
307 if (!(map & 1)) continue;
309 stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
310 ++stateblock->num_contained_transform_states;
314 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
316 DWORD map = stateblock->changed.textureState[i];
318 for(j = 0; map; map >>= 1, ++j)
320 if (!(map & 1)) continue;
322 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
323 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
324 ++stateblock->num_contained_tss_states;
328 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
330 DWORD map = stateblock->changed.samplerState[i];
332 for (j = 0; map; map >>= 1, ++j)
334 if (!(map & 1)) continue;
336 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
337 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
338 ++stateblock->num_contained_sampler_states;
343 static void stateblock_init_lights(struct list *dst_map, const struct list *src_map)
345 unsigned int i;
347 for (i = 0; i < LIGHTMAP_SIZE; ++i)
349 const struct wined3d_light_info *src_light;
351 LIST_FOR_EACH_ENTRY(src_light, &src_map[i], struct wined3d_light_info, entry)
353 struct wined3d_light_info *dst_light = heap_alloc(sizeof(*dst_light));
355 *dst_light = *src_light;
356 list_add_tail(&dst_map[i], &dst_light->entry);
361 ULONG CDECL wined3d_stateblock_incref(struct wined3d_stateblock *stateblock)
363 ULONG refcount = InterlockedIncrement(&stateblock->ref);
365 TRACE("%p increasing refcount to %u.\n", stateblock, refcount);
367 return refcount;
370 void state_unbind_resources(struct wined3d_state *state)
372 struct wined3d_unordered_access_view *uav;
373 struct wined3d_shader_resource_view *srv;
374 struct wined3d_vertex_declaration *decl;
375 struct wined3d_blend_state *blend_state;
376 struct wined3d_rendertarget_view *rtv;
377 struct wined3d_sampler *sampler;
378 struct wined3d_texture *texture;
379 struct wined3d_buffer *buffer;
380 struct wined3d_shader *shader;
381 unsigned int i, j;
383 if ((decl = state->vertex_declaration))
385 state->vertex_declaration = NULL;
386 wined3d_vertex_declaration_decref(decl);
389 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
391 if ((texture = state->textures[i]))
393 state->textures[i] = NULL;
394 wined3d_texture_decref(texture);
398 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
400 if ((buffer = state->stream_output[i].buffer))
402 state->stream_output[i].buffer = NULL;
403 wined3d_buffer_decref(buffer);
407 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
409 if ((buffer = state->streams[i].buffer))
411 state->streams[i].buffer = NULL;
412 wined3d_buffer_decref(buffer);
416 if ((buffer = state->index_buffer))
418 state->index_buffer = NULL;
419 wined3d_buffer_decref(buffer);
422 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
424 if ((shader = state->shader[i]))
426 state->shader[i] = NULL;
427 wined3d_shader_decref(shader);
430 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
432 if ((buffer = state->cb[i][j].buffer))
434 state->cb[i][j].buffer = NULL;
435 wined3d_buffer_decref(buffer);
439 for (j = 0; j < MAX_SAMPLER_OBJECTS; ++j)
441 if ((sampler = state->sampler[i][j]))
443 state->sampler[i][j] = NULL;
444 wined3d_sampler_decref(sampler);
448 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
450 if ((srv = state->shader_resource_view[i][j]))
452 state->shader_resource_view[i][j] = NULL;
453 wined3d_srv_bind_count_dec(srv);
454 wined3d_shader_resource_view_decref(srv);
459 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
461 for (j = 0; j < MAX_UNORDERED_ACCESS_VIEWS; ++j)
463 if ((uav = state->unordered_access_view[i][j]))
465 state->unordered_access_view[i][j] = NULL;
466 wined3d_unordered_access_view_decref(uav);
471 if ((blend_state = state->blend_state))
473 state->blend_state = NULL;
474 wined3d_blend_state_decref(blend_state);
477 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
479 if ((rtv = state->fb.render_targets[i]))
481 state->fb.render_targets[i] = NULL;
482 wined3d_rendertarget_view_decref(rtv);
486 if ((rtv = state->fb.depth_stencil))
488 state->fb.depth_stencil = NULL;
489 wined3d_rendertarget_view_decref(rtv);
493 void wined3d_stateblock_state_cleanup(struct wined3d_stateblock_state *state)
495 struct wined3d_light_info *light, *cursor;
496 struct wined3d_vertex_declaration *decl;
497 struct wined3d_texture *texture;
498 struct wined3d_buffer *buffer;
499 struct wined3d_shader *shader;
500 unsigned int i;
502 if ((decl = state->vertex_declaration))
504 state->vertex_declaration = NULL;
505 wined3d_vertex_declaration_decref(decl);
508 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
510 if ((buffer = state->streams[i].buffer))
512 state->streams[i].buffer = NULL;
513 wined3d_buffer_decref(buffer);
517 if ((buffer = state->index_buffer))
519 state->index_buffer = NULL;
520 wined3d_buffer_decref(buffer);
523 if ((shader = state->vs))
525 state->vs = NULL;
526 wined3d_shader_decref(shader);
529 if ((shader = state->ps))
531 state->ps = NULL;
532 wined3d_shader_decref(shader);
535 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
537 if ((texture = state->textures[i]))
539 state->textures[i] = NULL;
540 wined3d_texture_decref(texture);
544 for (i = 0; i < LIGHTMAP_SIZE; ++i)
546 LIST_FOR_EACH_ENTRY_SAFE(light, cursor, &state->light_state->light_map[i], struct wined3d_light_info, entry)
548 list_remove(&light->entry);
549 heap_free(light);
554 void state_cleanup(struct wined3d_state *state)
556 unsigned int counter;
558 if (!(state->flags & WINED3D_STATE_NO_REF))
559 state_unbind_resources(state);
561 for (counter = 0; counter < WINED3D_MAX_ACTIVE_LIGHTS; ++counter)
563 state->light_state.lights[counter] = NULL;
566 for (counter = 0; counter < LIGHTMAP_SIZE; ++counter)
568 struct list *e1, *e2;
569 LIST_FOR_EACH_SAFE(e1, e2, &state->light_state.light_map[counter])
571 struct wined3d_light_info *light = LIST_ENTRY(e1, struct wined3d_light_info, entry);
572 list_remove(&light->entry);
573 heap_free(light);
578 ULONG CDECL wined3d_stateblock_decref(struct wined3d_stateblock *stateblock)
580 ULONG refcount = InterlockedDecrement(&stateblock->ref);
582 TRACE("%p decreasing refcount to %u\n", stateblock, refcount);
584 if (!refcount)
586 wined3d_mutex_lock();
587 wined3d_stateblock_state_cleanup(&stateblock->stateblock_state);
588 heap_free(stateblock);
589 wined3d_mutex_unlock();
592 return refcount;
595 struct wined3d_light_info *wined3d_light_state_get_light(const struct wined3d_light_state *state, unsigned int idx)
597 struct wined3d_light_info *light_info;
598 unsigned int hash_idx;
600 hash_idx = LIGHTMAP_HASHFUNC(idx);
601 LIST_FOR_EACH_ENTRY(light_info, &state->light_map[hash_idx], struct wined3d_light_info, entry)
603 if (light_info->OriginalIndex == idx)
604 return light_info;
607 return NULL;
610 HRESULT wined3d_light_state_set_light(struct wined3d_light_state *state, DWORD light_idx,
611 const struct wined3d_light *params, struct wined3d_light_info **light_info)
613 struct wined3d_light_info *object;
614 unsigned int hash_idx;
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 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
626 list_add_head(&state->light_map[hash_idx], &object->entry);
627 object->glIndex = -1;
628 object->OriginalIndex = light_idx;
631 object->OriginalParms = *params;
633 *light_info = object;
634 return WINED3D_OK;
637 void wined3d_light_state_enable_light(struct wined3d_light_state *state, const struct wined3d_d3d_info *d3d_info,
638 struct wined3d_light_info *light_info, BOOL enable)
640 unsigned int light_count, i;
642 if (!(light_info->enabled = enable))
644 if (light_info->glIndex == -1)
646 TRACE("Light already disabled, nothing to do.\n");
647 return;
650 state->lights[light_info->glIndex] = NULL;
651 light_info->glIndex = -1;
652 return;
655 if (light_info->glIndex != -1)
657 TRACE("Light already enabled, nothing to do.\n");
658 return;
661 /* Find a free light. */
662 light_count = d3d_info->limits.active_light_count;
663 for (i = 0; i < light_count; ++i)
665 if (state->lights[i])
666 continue;
668 state->lights[i] = light_info;
669 light_info->glIndex = i;
670 return;
673 /* Our tests show that Windows returns D3D_OK in this situation, even with
674 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices.
675 * This is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns
676 * TRUE * as well for those lights.
678 * TODO: Test how this affects rendering. */
679 WARN("Too many concurrently active lights.\n");
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;
687 UINT i;
689 /* Lights... For a recorded state block, we just had a chain of actions
690 * to perform, so we need to walk that chain and update any actions which
691 * differ. */
692 for (i = 0; i < LIGHTMAP_SIZE; ++i)
694 LIST_FOR_EACH_ENTRY(dst, &dst_state->light_map[i], struct wined3d_light_info, entry)
696 if ((src = wined3d_light_state_get_light(src_state, dst->OriginalIndex)))
698 dst->OriginalParms = src->OriginalParms;
700 if (src->glIndex == -1 && dst->glIndex != -1)
702 /* Light disabled. */
703 dst_state->lights[dst->glIndex] = NULL;
705 else if (src->glIndex != -1 && dst->glIndex == -1)
707 /* Light enabled. */
708 dst_state->lights[src->glIndex] = dst;
710 dst->glIndex = src->glIndex;
712 else
714 /* This can happen if the light was originally created as a
715 * default light for SetLightEnable() while recording. */
716 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
717 dst->OriginalIndex, dst_state, src_state);
719 dst->OriginalParms = WINED3D_default_light;
720 if (dst->glIndex != -1)
722 dst_state->lights[dst->glIndex] = NULL;
723 dst->glIndex = -1;
730 void CDECL wined3d_stateblock_capture(struct wined3d_stateblock *stateblock,
731 const struct wined3d_stateblock *device_state)
733 const struct wined3d_stateblock_state *state = &device_state->stateblock_state;
734 struct wined3d_range range;
735 unsigned int i, start;
736 DWORD map;
738 TRACE("stateblock %p, device_state %p.\n", stateblock, device_state);
740 if (stateblock->changed.vertexShader && stateblock->stateblock_state.vs != state->vs)
742 TRACE("Updating vertex shader from %p to %p.\n", stateblock->stateblock_state.vs, state->vs);
744 if (state->vs)
745 wined3d_shader_incref(state->vs);
746 if (stateblock->stateblock_state.vs)
747 wined3d_shader_decref(stateblock->stateblock_state.vs);
748 stateblock->stateblock_state.vs = state->vs;
751 for (start = 0; ; start = range.offset + range.size)
753 if (!wined3d_bitmap_get_range(stateblock->changed.vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
754 break;
756 memcpy(&stateblock->stateblock_state.vs_consts_f[range.offset], &state->vs_consts_f[range.offset],
757 sizeof(*state->vs_consts_f) * range.size);
759 map = stateblock->changed.vertexShaderConstantsI;
760 for (start = 0; ; start = range.offset + range.size)
762 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
763 break;
765 memcpy(&stateblock->stateblock_state.vs_consts_i[range.offset], &state->vs_consts_i[range.offset],
766 sizeof(*state->vs_consts_i) * range.size);
768 map = stateblock->changed.vertexShaderConstantsB;
769 for (start = 0; ; start = range.offset + range.size)
771 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
772 break;
774 memcpy(&stateblock->stateblock_state.vs_consts_b[range.offset], &state->vs_consts_b[range.offset],
775 sizeof(*state->vs_consts_b) * range.size);
778 for (start = 0; ; start = range.offset + range.size)
780 if (!wined3d_bitmap_get_range(stateblock->changed.ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
781 break;
783 memcpy(&stateblock->stateblock_state.ps_consts_f[range.offset], &state->ps_consts_f[range.offset],
784 sizeof(*state->ps_consts_f) * range.size);
786 map = stateblock->changed.pixelShaderConstantsI;
787 for (start = 0; ; start = range.offset + range.size)
789 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
790 break;
792 memcpy(&stateblock->stateblock_state.ps_consts_i[range.offset], &state->ps_consts_i[range.offset],
793 sizeof(*state->ps_consts_i) * range.size);
795 map = stateblock->changed.pixelShaderConstantsB;
796 for (start = 0; ; start = range.offset + range.size)
798 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
799 break;
801 memcpy(&stateblock->stateblock_state.ps_consts_b[range.offset], &state->ps_consts_b[range.offset],
802 sizeof(*state->ps_consts_b) * range.size);
805 if (stateblock->changed.transforms)
807 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
809 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
811 TRACE("Updating transform %#x.\n", transform);
813 stateblock->stateblock_state.transforms[transform] = state->transforms[transform];
817 if (stateblock->changed.indices
818 && ((stateblock->stateblock_state.index_buffer != state->index_buffer)
819 || (stateblock->stateblock_state.base_vertex_index != state->base_vertex_index)
820 || (stateblock->stateblock_state.index_format != state->index_format)))
822 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
823 state->index_buffer, state->base_vertex_index);
825 if (state->index_buffer)
826 wined3d_buffer_incref(state->index_buffer);
827 if (stateblock->stateblock_state.index_buffer)
828 wined3d_buffer_decref(stateblock->stateblock_state.index_buffer);
829 stateblock->stateblock_state.index_buffer = state->index_buffer;
830 stateblock->stateblock_state.base_vertex_index = state->base_vertex_index;
831 stateblock->stateblock_state.index_format = state->index_format;
834 if (stateblock->changed.vertexDecl && stateblock->stateblock_state.vertex_declaration != state->vertex_declaration)
836 TRACE("Updating vertex declaration from %p to %p.\n",
837 stateblock->stateblock_state.vertex_declaration, state->vertex_declaration);
839 if (state->vertex_declaration)
840 wined3d_vertex_declaration_incref(state->vertex_declaration);
841 if (stateblock->stateblock_state.vertex_declaration)
842 wined3d_vertex_declaration_decref(stateblock->stateblock_state.vertex_declaration);
843 stateblock->stateblock_state.vertex_declaration = state->vertex_declaration;
846 if (stateblock->changed.material
847 && memcmp(&state->material, &stateblock->stateblock_state.material,
848 sizeof(stateblock->stateblock_state.material)))
850 TRACE("Updating material.\n");
852 stateblock->stateblock_state.material = state->material;
855 if (stateblock->changed.viewport
856 && memcmp(&state->viewport, &stateblock->stateblock_state.viewport, sizeof(state->viewport)))
858 TRACE("Updating viewport.\n");
860 stateblock->stateblock_state.viewport = state->viewport;
863 if (stateblock->changed.scissorRect
864 && memcmp(&state->scissor_rect, &stateblock->stateblock_state.scissor_rect, sizeof(state->scissor_rect)))
866 TRACE("Updating scissor rect.\n");
868 stateblock->stateblock_state.scissor_rect = state->scissor_rect;
871 map = stateblock->changed.streamSource;
872 while (map)
874 i = wined3d_bit_scan(&map);
876 if (stateblock->stateblock_state.streams[i].stride != state->streams[i].stride
877 || stateblock->stateblock_state.streams[i].offset != state->streams[i].offset
878 || stateblock->stateblock_state.streams[i].buffer != state->streams[i].buffer)
880 TRACE("stateblock %p, stream source %u, buffer %p, stride %u, offset %u.\n",
881 stateblock, i, state->streams[i].buffer, state->streams[i].stride,
882 state->streams[i].offset);
884 stateblock->stateblock_state.streams[i].stride = state->streams[i].stride;
885 if (stateblock->changed.store_stream_offset)
886 stateblock->stateblock_state.streams[i].offset = state->streams[i].offset;
888 if (state->streams[i].buffer)
889 wined3d_buffer_incref(state->streams[i].buffer);
890 if (stateblock->stateblock_state.streams[i].buffer)
891 wined3d_buffer_decref(stateblock->stateblock_state.streams[i].buffer);
892 stateblock->stateblock_state.streams[i].buffer = state->streams[i].buffer;
896 map = stateblock->changed.streamFreq;
897 while (map)
899 i = wined3d_bit_scan(&map);
901 if (stateblock->stateblock_state.streams[i].frequency != state->streams[i].frequency
902 || stateblock->stateblock_state.streams[i].flags != state->streams[i].flags)
904 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
905 i, state->streams[i].frequency, state->streams[i].flags);
907 stateblock->stateblock_state.streams[i].frequency = state->streams[i].frequency;
908 stateblock->stateblock_state.streams[i].flags = state->streams[i].flags;
912 map = stateblock->changed.clipplane;
913 while (map)
915 i = wined3d_bit_scan(&map);
917 if (memcmp(&stateblock->stateblock_state.clip_planes[i], &state->clip_planes[i], sizeof(state->clip_planes[i])))
919 TRACE("Updating clipplane %u.\n", i);
920 stateblock->stateblock_state.clip_planes[i] = state->clip_planes[i];
924 /* Render */
925 for (i = 0; i < stateblock->num_contained_render_states; ++i)
927 enum wined3d_render_state rs = stateblock->contained_render_states[i];
929 TRACE("Updating render state %#x to %u.\n", rs, state->rs[rs]);
931 stateblock->stateblock_state.rs[rs] = state->rs[rs];
934 /* Texture states */
935 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
937 DWORD stage = stateblock->contained_tss_states[i].stage;
938 DWORD texture_state = stateblock->contained_tss_states[i].state;
940 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage, texture_state,
941 state->texture_states[stage][texture_state],
942 stateblock->stateblock_state.texture_states[stage][texture_state]);
944 stateblock->stateblock_state.texture_states[stage][texture_state] = state->texture_states[stage][texture_state];
947 /* Samplers */
948 map = stateblock->changed.textures;
949 while (map)
951 i = wined3d_bit_scan(&map);
953 TRACE("Updating texture %u to %p (was %p).\n",
954 i, state->textures[i], stateblock->stateblock_state.textures[i]);
956 if (state->textures[i])
957 wined3d_texture_incref(state->textures[i]);
958 if (stateblock->stateblock_state.textures[i])
959 wined3d_texture_decref(stateblock->stateblock_state.textures[i]);
960 stateblock->stateblock_state.textures[i] = state->textures[i];
963 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
965 DWORD stage = stateblock->contained_sampler_states[i].stage;
966 DWORD sampler_state = stateblock->contained_sampler_states[i].state;
968 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage, sampler_state,
969 state->sampler_states[stage][sampler_state],
970 stateblock->stateblock_state.sampler_states[stage][sampler_state]);
972 stateblock->stateblock_state.sampler_states[stage][sampler_state] = state->sampler_states[stage][sampler_state];
975 if (stateblock->changed.pixelShader && stateblock->stateblock_state.ps != state->ps)
977 if (state->ps)
978 wined3d_shader_incref(state->ps);
979 if (stateblock->stateblock_state.ps)
980 wined3d_shader_decref(stateblock->stateblock_state.ps);
981 stateblock->stateblock_state.ps = state->ps;
984 if (stateblock->changed.lights)
985 wined3d_state_record_lights(stateblock->stateblock_state.light_state, state->light_state);
987 if (stateblock->changed.alpha_to_coverage)
988 stateblock->stateblock_state.alpha_to_coverage = state->alpha_to_coverage;
990 TRACE("Capture done.\n");
993 void CDECL wined3d_stateblock_apply(const struct wined3d_stateblock *stateblock,
994 struct wined3d_stateblock *device_state)
996 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
997 struct wined3d_range range;
998 unsigned int i, start;
999 DWORD map;
1001 TRACE("stateblock %p, device_state %p.\n", stateblock, device_state);
1003 if (stateblock->changed.vertexShader)
1004 wined3d_stateblock_set_vertex_shader(device_state, state->vs);
1005 if (stateblock->changed.pixelShader)
1006 wined3d_stateblock_set_pixel_shader(device_state, state->ps);
1008 for (start = 0; ; start = range.offset + range.size)
1010 if (!wined3d_bitmap_get_range(stateblock->changed.vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
1011 break;
1012 wined3d_stateblock_set_vs_consts_f(device_state, range.offset, range.size, &state->vs_consts_f[range.offset]);
1014 map = stateblock->changed.vertexShaderConstantsI;
1015 for (start = 0; ; start = range.offset + range.size)
1017 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
1018 break;
1019 wined3d_stateblock_set_vs_consts_i(device_state, range.offset, range.size, &state->vs_consts_i[range.offset]);
1021 map = stateblock->changed.vertexShaderConstantsB;
1022 for (start = 0; ; start = range.offset + range.size)
1024 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
1025 break;
1026 wined3d_stateblock_set_vs_consts_b(device_state, range.offset, range.size, &state->vs_consts_b[range.offset]);
1029 for (start = 0; ; start = range.offset + range.size)
1031 if (!wined3d_bitmap_get_range(stateblock->changed.ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
1032 break;
1033 wined3d_stateblock_set_ps_consts_f(device_state, range.offset, range.size, &state->ps_consts_f[range.offset]);
1035 map = stateblock->changed.pixelShaderConstantsI;
1036 for (start = 0; ; start = range.offset + range.size)
1038 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
1039 break;
1040 wined3d_stateblock_set_ps_consts_i(device_state, range.offset, range.size, &state->ps_consts_i[range.offset]);
1042 map = stateblock->changed.pixelShaderConstantsB;
1043 for (start = 0; ; start = range.offset + range.size)
1045 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
1046 break;
1047 wined3d_stateblock_set_ps_consts_b(device_state, range.offset, range.size, &state->ps_consts_b[range.offset]);
1050 if (stateblock->changed.transforms)
1052 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
1054 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
1056 wined3d_stateblock_set_transform(device_state, transform, &state->transforms[transform]);
1060 if (stateblock->changed.lights)
1062 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); ++i)
1064 const struct wined3d_light_info *light;
1066 LIST_FOR_EACH_ENTRY(light, &state->light_state->light_map[i], struct wined3d_light_info, entry)
1068 wined3d_stateblock_set_light(device_state, light->OriginalIndex, &light->OriginalParms);
1069 wined3d_stateblock_set_light_enable(device_state, light->OriginalIndex, light->glIndex != -1);
1074 if (stateblock->changed.alpha_to_coverage)
1076 device_state->stateblock_state.alpha_to_coverage = state->alpha_to_coverage;
1077 device_state->changed.alpha_to_coverage = 1;
1080 /* Render states. */
1081 for (i = 0; i < stateblock->num_contained_render_states; ++i)
1083 enum wined3d_render_state rs = stateblock->contained_render_states[i];
1085 wined3d_stateblock_set_render_state(device_state, rs, state->rs[rs]);
1088 /* Texture states. */
1089 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
1091 DWORD stage = stateblock->contained_tss_states[i].stage;
1092 DWORD texture_state = stateblock->contained_tss_states[i].state;
1094 wined3d_stateblock_set_texture_stage_state(device_state, stage, texture_state,
1095 state->texture_states[stage][texture_state]);
1098 /* Sampler states. */
1099 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
1101 DWORD stage = stateblock->contained_sampler_states[i].stage;
1102 DWORD sampler_state = stateblock->contained_sampler_states[i].state;
1104 wined3d_stateblock_set_sampler_state(device_state, stage, sampler_state,
1105 state->sampler_states[stage][sampler_state]);
1108 if (stateblock->changed.indices)
1110 wined3d_stateblock_set_index_buffer(device_state, state->index_buffer, state->index_format);
1111 wined3d_stateblock_set_base_vertex_index(device_state, state->base_vertex_index);
1114 if (stateblock->changed.vertexDecl && state->vertex_declaration)
1115 wined3d_stateblock_set_vertex_declaration(device_state, state->vertex_declaration);
1117 if (stateblock->changed.material)
1118 wined3d_stateblock_set_material(device_state, &state->material);
1120 if (stateblock->changed.viewport)
1121 wined3d_stateblock_set_viewport(device_state, &state->viewport);
1123 if (stateblock->changed.scissorRect)
1124 wined3d_stateblock_set_scissor_rect(device_state, &state->scissor_rect);
1126 map = stateblock->changed.streamSource;
1127 while (map)
1129 i = wined3d_bit_scan(&map);
1130 wined3d_stateblock_set_stream_source(device_state, i, state->streams[i].buffer,
1131 state->streams[i].offset, state->streams[i].stride);
1134 map = stateblock->changed.streamFreq;
1135 while (map)
1137 i = wined3d_bit_scan(&map);
1138 wined3d_stateblock_set_stream_source_freq(device_state, i,
1139 state->streams[i].frequency | state->streams[i].flags);
1142 map = stateblock->changed.textures;
1143 while (map)
1145 i = wined3d_bit_scan(&map);
1146 wined3d_stateblock_set_texture(device_state, i, state->textures[i]);
1149 map = stateblock->changed.clipplane;
1150 while (map)
1152 i = wined3d_bit_scan(&map);
1153 wined3d_stateblock_set_clip_plane(device_state, i, &state->clip_planes[i]);
1156 TRACE("Applied stateblock %p.\n", stateblock);
1159 void CDECL wined3d_stateblock_set_vertex_shader(struct wined3d_stateblock *stateblock, struct wined3d_shader *shader)
1161 TRACE("stateblock %p, shader %p.\n", stateblock, shader);
1163 if (shader)
1164 wined3d_shader_incref(shader);
1165 if (stateblock->stateblock_state.vs)
1166 wined3d_shader_decref(stateblock->stateblock_state.vs);
1167 stateblock->stateblock_state.vs = shader;
1168 stateblock->changed.vertexShader = TRUE;
1171 static void wined3d_bitmap_set_bits(uint32_t *bitmap, unsigned int start, unsigned int count)
1173 const unsigned int word_bit_count = sizeof(*bitmap) * CHAR_BIT;
1174 const unsigned int shift = start % word_bit_count;
1175 uint32_t mask, last_mask;
1176 unsigned int mask_size;
1178 bitmap += start / word_bit_count;
1179 mask = ~0u << shift;
1180 mask_size = word_bit_count - shift;
1181 last_mask = (1u << (start + count) % word_bit_count) - 1;
1182 if (mask_size <= count)
1184 *bitmap |= mask;
1185 ++bitmap;
1186 count -= mask_size;
1187 mask = ~0u;
1189 if (count >= word_bit_count)
1191 memset(bitmap, 0xffu, count / word_bit_count * sizeof(*bitmap));
1192 bitmap += count / word_bit_count;
1193 count = count % word_bit_count;
1195 if (count)
1196 *bitmap |= mask & last_mask;
1199 HRESULT CDECL wined3d_stateblock_set_vs_consts_f(struct wined3d_stateblock *stateblock,
1200 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
1202 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
1204 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1205 stateblock, start_idx, count, constants);
1207 if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.vs_uniform_count))
1208 return WINED3DERR_INVALIDCALL;
1210 memcpy(&stateblock->stateblock_state.vs_consts_f[start_idx], constants, count * sizeof(*constants));
1211 wined3d_bitmap_set_bits(stateblock->changed.vs_consts_f, start_idx, count);
1212 return WINED3D_OK;
1215 HRESULT CDECL wined3d_stateblock_set_vs_consts_i(struct wined3d_stateblock *stateblock,
1216 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
1218 unsigned int i;
1220 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1221 stateblock, start_idx, count, constants);
1223 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1224 return WINED3DERR_INVALIDCALL;
1226 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1227 count = WINED3D_MAX_CONSTS_I - start_idx;
1229 memcpy(&stateblock->stateblock_state.vs_consts_i[start_idx], constants, count * sizeof(*constants));
1230 for (i = start_idx; i < count + start_idx; ++i)
1231 stateblock->changed.vertexShaderConstantsI |= (1u << i);
1232 return WINED3D_OK;
1235 HRESULT CDECL wined3d_stateblock_set_vs_consts_b(struct wined3d_stateblock *stateblock,
1236 unsigned int start_idx, unsigned int count, const BOOL *constants)
1238 unsigned int i;
1240 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1241 stateblock, start_idx, count, constants);
1243 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1244 return WINED3DERR_INVALIDCALL;
1246 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1247 count = WINED3D_MAX_CONSTS_B - start_idx;
1249 memcpy(&stateblock->stateblock_state.vs_consts_b[start_idx], constants, count * sizeof(*constants));
1250 for (i = start_idx; i < count + start_idx; ++i)
1251 stateblock->changed.vertexShaderConstantsB |= (1u << i);
1252 return WINED3D_OK;
1255 void CDECL wined3d_stateblock_set_pixel_shader(struct wined3d_stateblock *stateblock, struct wined3d_shader *shader)
1257 TRACE("stateblock %p, shader %p.\n", stateblock, shader);
1259 if (shader)
1260 wined3d_shader_incref(shader);
1261 if (stateblock->stateblock_state.ps)
1262 wined3d_shader_decref(stateblock->stateblock_state.ps);
1263 stateblock->stateblock_state.ps = shader;
1264 stateblock->changed.pixelShader = TRUE;
1267 HRESULT CDECL wined3d_stateblock_set_ps_consts_f(struct wined3d_stateblock *stateblock,
1268 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
1270 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
1272 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1273 stateblock, start_idx, count, constants);
1275 if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.ps_uniform_count))
1276 return WINED3DERR_INVALIDCALL;
1278 memcpy(&stateblock->stateblock_state.ps_consts_f[start_idx], constants, count * sizeof(*constants));
1279 wined3d_bitmap_set_bits(stateblock->changed.ps_consts_f, start_idx, count);
1280 return WINED3D_OK;
1283 HRESULT CDECL wined3d_stateblock_set_ps_consts_i(struct wined3d_stateblock *stateblock,
1284 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
1286 unsigned int i;
1288 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1289 stateblock, start_idx, count, constants);
1291 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1292 return WINED3DERR_INVALIDCALL;
1294 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1295 count = WINED3D_MAX_CONSTS_I - start_idx;
1297 memcpy(&stateblock->stateblock_state.ps_consts_i[start_idx], constants, count * sizeof(*constants));
1298 for (i = start_idx; i < count + start_idx; ++i)
1299 stateblock->changed.pixelShaderConstantsI |= (1u << i);
1300 return WINED3D_OK;
1303 HRESULT CDECL wined3d_stateblock_set_ps_consts_b(struct wined3d_stateblock *stateblock,
1304 unsigned int start_idx, unsigned int count, const BOOL *constants)
1306 unsigned int i;
1308 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1309 stateblock, start_idx, count, constants);
1311 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1312 return WINED3DERR_INVALIDCALL;
1314 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1315 count = WINED3D_MAX_CONSTS_B - start_idx;
1317 memcpy(&stateblock->stateblock_state.ps_consts_b[start_idx], constants, count * sizeof(*constants));
1318 for (i = start_idx; i < count + start_idx; ++i)
1319 stateblock->changed.pixelShaderConstantsB |= (1u << i);
1320 return WINED3D_OK;
1323 void CDECL wined3d_stateblock_set_vertex_declaration(struct wined3d_stateblock *stateblock,
1324 struct wined3d_vertex_declaration *declaration)
1326 TRACE("stateblock %p, declaration %p.\n", stateblock, declaration);
1328 if (declaration)
1329 wined3d_vertex_declaration_incref(declaration);
1330 if (stateblock->stateblock_state.vertex_declaration)
1331 wined3d_vertex_declaration_decref(stateblock->stateblock_state.vertex_declaration);
1332 stateblock->stateblock_state.vertex_declaration = declaration;
1333 stateblock->changed.vertexDecl = TRUE;
1336 void CDECL wined3d_stateblock_set_render_state(struct wined3d_stateblock *stateblock,
1337 enum wined3d_render_state state, DWORD value)
1339 TRACE("stateblock %p, state %s (%#x), value %#x.\n", stateblock, debug_d3drenderstate(state), state, value);
1341 if (state > WINEHIGHEST_RENDER_STATE)
1343 WARN("Unhandled render state %#x.\n", state);
1344 return;
1347 stateblock->stateblock_state.rs[state] = value;
1348 stateblock->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
1350 if (state == WINED3D_RS_POINTSIZE
1351 && (value == WINED3D_ALPHA_TO_COVERAGE_ENABLE || value == WINED3D_ALPHA_TO_COVERAGE_DISABLE))
1353 stateblock->changed.alpha_to_coverage = 1;
1354 stateblock->stateblock_state.alpha_to_coverage = (value == WINED3D_ALPHA_TO_COVERAGE_ENABLE);
1358 void CDECL wined3d_stateblock_set_sampler_state(struct wined3d_stateblock *stateblock,
1359 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1361 TRACE("stateblock %p, sampler_idx %u, state %s, value %#x.\n",
1362 stateblock, sampler_idx, debug_d3dsamplerstate(state), value);
1364 if (sampler_idx >= ARRAY_SIZE(stateblock->stateblock_state.sampler_states))
1366 WARN("Invalid sampler %u.\n", sampler_idx);
1367 return;
1370 stateblock->stateblock_state.sampler_states[sampler_idx][state] = value;
1371 stateblock->changed.samplerState[sampler_idx] |= 1u << state;
1374 void CDECL wined3d_stateblock_set_texture_stage_state(struct wined3d_stateblock *stateblock,
1375 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
1377 TRACE("stateblock %p, stage %u, state %s, value %#x.\n",
1378 stateblock, stage, debug_d3dtexturestate(state), value);
1380 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
1382 WARN("Invalid state %#x passed.\n", state);
1383 return;
1386 if (stage >= WINED3D_MAX_TEXTURES)
1388 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
1389 stage, WINED3D_MAX_TEXTURES - 1);
1390 return;
1393 stateblock->stateblock_state.texture_states[stage][state] = value;
1394 stateblock->changed.textureState[stage] |= 1u << state;
1397 void CDECL wined3d_stateblock_set_texture(struct wined3d_stateblock *stateblock,
1398 UINT stage, struct wined3d_texture *texture)
1400 TRACE("stateblock %p, stage %u, texture %p.\n", stateblock, stage, texture);
1402 if (stage >= ARRAY_SIZE(stateblock->stateblock_state.textures))
1404 WARN("Ignoring invalid stage %u.\n", stage);
1405 return;
1408 if (texture)
1409 wined3d_texture_incref(texture);
1410 if (stateblock->stateblock_state.textures[stage])
1411 wined3d_texture_decref(stateblock->stateblock_state.textures[stage]);
1412 stateblock->stateblock_state.textures[stage] = texture;
1413 stateblock->changed.textures |= 1u << stage;
1416 void CDECL wined3d_stateblock_set_transform(struct wined3d_stateblock *stateblock,
1417 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1419 TRACE("stateblock %p, state %s, matrix %p.\n", stateblock, debug_d3dtstype(d3dts), matrix);
1420 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1421 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1422 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1423 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1425 stateblock->stateblock_state.transforms[d3dts] = *matrix;
1426 stateblock->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1427 stateblock->changed.transforms = 1;
1430 void CDECL wined3d_stateblock_multiply_transform(struct wined3d_stateblock *stateblock,
1431 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1433 struct wined3d_matrix *mat = &stateblock->stateblock_state.transforms[d3dts];
1435 TRACE("stateblock %p, state %s, matrix %p.\n", stateblock, debug_d3dtstype(d3dts), matrix);
1436 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1437 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1438 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1439 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1441 multiply_matrix(mat, mat, matrix);
1442 stateblock->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1443 stateblock->changed.transforms = 1;
1446 HRESULT CDECL wined3d_stateblock_set_clip_plane(struct wined3d_stateblock *stateblock,
1447 UINT plane_idx, const struct wined3d_vec4 *plane)
1449 TRACE("stateblock %p, plane_idx %u, plane %p.\n", stateblock, plane_idx, plane);
1451 if (plane_idx >= stateblock->device->adapter->d3d_info.limits.max_clip_distances)
1453 TRACE("Application has requested clipplane this device doesn't support.\n");
1454 return WINED3DERR_INVALIDCALL;
1457 stateblock->stateblock_state.clip_planes[plane_idx] = *plane;
1458 stateblock->changed.clipplane |= 1u << plane_idx;
1459 return S_OK;
1462 void CDECL wined3d_stateblock_set_material(struct wined3d_stateblock *stateblock,
1463 const struct wined3d_material *material)
1465 TRACE("stateblock %p, material %p.\n", stateblock, material);
1467 stateblock->stateblock_state.material = *material;
1468 stateblock->changed.material = TRUE;
1471 void CDECL wined3d_stateblock_set_viewport(struct wined3d_stateblock *stateblock,
1472 const struct wined3d_viewport *viewport)
1474 TRACE("stateblock %p, viewport %p.\n", stateblock, viewport);
1476 stateblock->stateblock_state.viewport = *viewport;
1477 stateblock->changed.viewport = TRUE;
1480 void CDECL wined3d_stateblock_set_scissor_rect(struct wined3d_stateblock *stateblock, const RECT *rect)
1482 TRACE("stateblock %p, rect %s.\n", stateblock, wine_dbgstr_rect(rect));
1484 stateblock->stateblock_state.scissor_rect = *rect;
1485 stateblock->changed.scissorRect = TRUE;
1488 void CDECL wined3d_stateblock_set_index_buffer(struct wined3d_stateblock *stateblock,
1489 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1491 TRACE("stateblock %p, buffer %p, format %s.\n", stateblock, buffer, debug_d3dformat(format_id));
1493 if (buffer)
1494 wined3d_buffer_incref(buffer);
1495 if (stateblock->stateblock_state.index_buffer)
1496 wined3d_buffer_decref(stateblock->stateblock_state.index_buffer);
1497 stateblock->stateblock_state.index_buffer = buffer;
1498 stateblock->stateblock_state.index_format = format_id;
1499 stateblock->changed.indices = TRUE;
1502 void CDECL wined3d_stateblock_set_base_vertex_index(struct wined3d_stateblock *stateblock, INT base_index)
1504 TRACE("stateblock %p, base_index %d.\n", stateblock, base_index);
1506 stateblock->stateblock_state.base_vertex_index = base_index;
1509 HRESULT CDECL wined3d_stateblock_set_stream_source(struct wined3d_stateblock *stateblock,
1510 UINT stream_idx, struct wined3d_buffer *buffer, UINT offset, UINT stride)
1512 struct wined3d_stream_state *stream;
1514 TRACE("stateblock %p, stream_idx %u, buffer %p, stride %u.\n",
1515 stateblock, stream_idx, buffer, stride);
1517 if (stream_idx >= WINED3D_MAX_STREAMS)
1519 WARN("Stream index %u out of range.\n", stream_idx);
1520 return WINED3DERR_INVALIDCALL;
1523 stream = &stateblock->stateblock_state.streams[stream_idx];
1525 if (buffer)
1526 wined3d_buffer_incref(buffer);
1527 if (stream->buffer)
1528 wined3d_buffer_decref(stream->buffer);
1529 stream->buffer = buffer;
1530 stream->stride = stride;
1531 stream->offset = offset;
1532 stateblock->changed.streamSource |= 1u << stream_idx;
1533 return WINED3D_OK;
1536 HRESULT CDECL wined3d_stateblock_set_stream_source_freq(struct wined3d_stateblock *stateblock,
1537 UINT stream_idx, UINT divider)
1539 struct wined3d_stream_state *stream;
1541 TRACE("stateblock %p, stream_idx %u, divider %#x.\n", stateblock, stream_idx, divider);
1543 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1545 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1546 return WINED3DERR_INVALIDCALL;
1548 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1550 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1551 return WINED3DERR_INVALIDCALL;
1553 if (!divider)
1555 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1556 return WINED3DERR_INVALIDCALL;
1559 stream = &stateblock->stateblock_state.streams[stream_idx];
1560 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1561 stream->frequency = divider & 0x7fffff;
1562 stateblock->changed.streamFreq |= 1u << stream_idx;
1563 return WINED3D_OK;
1566 HRESULT CDECL wined3d_stateblock_set_light(struct wined3d_stateblock *stateblock,
1567 UINT light_idx, const struct wined3d_light *light)
1569 struct wined3d_light_info *object = NULL;
1571 TRACE("stateblock %p, light_idx %u, light %p.\n", stateblock, light_idx, light);
1573 /* Check the parameter range. Need for speed most wanted sets junk lights
1574 * which confuse the GL driver. */
1575 if (!light)
1576 return WINED3DERR_INVALIDCALL;
1578 switch (light->type)
1580 case WINED3D_LIGHT_POINT:
1581 case WINED3D_LIGHT_SPOT:
1582 case WINED3D_LIGHT_GLSPOT:
1583 /* Incorrect attenuation values can cause the gl driver to crash.
1584 * Happens with Need for speed most wanted. */
1585 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1587 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1588 return WINED3DERR_INVALIDCALL;
1590 break;
1592 case WINED3D_LIGHT_DIRECTIONAL:
1593 case WINED3D_LIGHT_PARALLELPOINT:
1594 /* Ignores attenuation */
1595 break;
1597 default:
1598 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL.\n");
1599 return WINED3DERR_INVALIDCALL;
1602 stateblock->changed.lights = 1;
1603 return wined3d_light_state_set_light(stateblock->stateblock_state.light_state, light_idx, light, &object);
1606 HRESULT CDECL wined3d_stateblock_set_light_enable(struct wined3d_stateblock *stateblock, UINT light_idx, BOOL enable)
1608 struct wined3d_light_state *light_state = stateblock->stateblock_state.light_state;
1609 struct wined3d_light_info *light_info;
1610 HRESULT hr;
1612 TRACE("stateblock %p, light_idx %u, enable %#x.\n", stateblock, light_idx, enable);
1614 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1616 if (FAILED(hr = wined3d_light_state_set_light(light_state, light_idx, &WINED3D_default_light, &light_info)))
1617 return hr;
1619 wined3d_light_state_enable_light(light_state, &stateblock->device->adapter->d3d_info, light_info, enable);
1620 stateblock->changed.lights = 1;
1621 return S_OK;
1624 const struct wined3d_stateblock_state * CDECL wined3d_stateblock_get_state(const struct wined3d_stateblock *stateblock)
1626 return &stateblock->stateblock_state;
1629 HRESULT CDECL wined3d_stateblock_get_light(const struct wined3d_stateblock *stateblock,
1630 UINT light_idx, struct wined3d_light *light, BOOL *enabled)
1632 struct wined3d_light_info *light_info;
1634 if (!(light_info = wined3d_light_state_get_light(&stateblock->light_state, light_idx)))
1636 TRACE("Light %u is not defined.\n", light_idx);
1637 return WINED3DERR_INVALIDCALL;
1639 *light = light_info->OriginalParms;
1640 *enabled = light_info->enabled ? 128 : 0;
1641 return WINED3D_OK;
1644 static void init_default_render_states(DWORD rs[WINEHIGHEST_RENDER_STATE + 1], const struct wined3d_d3d_info *d3d_info)
1646 union
1648 struct wined3d_line_pattern lp;
1649 DWORD d;
1650 } lp;
1651 union
1653 float f;
1654 DWORD d;
1655 } tmpfloat;
1657 rs[WINED3D_RS_ZENABLE] = WINED3D_ZB_TRUE;
1658 rs[WINED3D_RS_FILLMODE] = WINED3D_FILL_SOLID;
1659 rs[WINED3D_RS_SHADEMODE] = WINED3D_SHADE_GOURAUD;
1660 lp.lp.repeat_factor = 0;
1661 lp.lp.line_pattern = 0;
1662 rs[WINED3D_RS_LINEPATTERN] = lp.d;
1663 rs[WINED3D_RS_ZWRITEENABLE] = TRUE;
1664 rs[WINED3D_RS_ALPHATESTENABLE] = FALSE;
1665 rs[WINED3D_RS_LASTPIXEL] = TRUE;
1666 rs[WINED3D_RS_SRCBLEND] = WINED3D_BLEND_ONE;
1667 rs[WINED3D_RS_DESTBLEND] = WINED3D_BLEND_ZERO;
1668 rs[WINED3D_RS_CULLMODE] = WINED3D_CULL_BACK;
1669 rs[WINED3D_RS_ZFUNC] = WINED3D_CMP_LESSEQUAL;
1670 rs[WINED3D_RS_ALPHAFUNC] = WINED3D_CMP_ALWAYS;
1671 rs[WINED3D_RS_ALPHAREF] = 0;
1672 rs[WINED3D_RS_DITHERENABLE] = FALSE;
1673 rs[WINED3D_RS_ALPHABLENDENABLE] = FALSE;
1674 rs[WINED3D_RS_FOGENABLE] = FALSE;
1675 rs[WINED3D_RS_SPECULARENABLE] = FALSE;
1676 rs[WINED3D_RS_ZVISIBLE] = 0;
1677 rs[WINED3D_RS_FOGCOLOR] = 0;
1678 rs[WINED3D_RS_FOGTABLEMODE] = WINED3D_FOG_NONE;
1679 tmpfloat.f = 0.0f;
1680 rs[WINED3D_RS_FOGSTART] = tmpfloat.d;
1681 tmpfloat.f = 1.0f;
1682 rs[WINED3D_RS_FOGEND] = tmpfloat.d;
1683 tmpfloat.f = 1.0f;
1684 rs[WINED3D_RS_FOGDENSITY] = tmpfloat.d;
1685 rs[WINED3D_RS_RANGEFOGENABLE] = FALSE;
1686 rs[WINED3D_RS_STENCILENABLE] = FALSE;
1687 rs[WINED3D_RS_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1688 rs[WINED3D_RS_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1689 rs[WINED3D_RS_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1690 rs[WINED3D_RS_STENCILREF] = 0;
1691 rs[WINED3D_RS_STENCILMASK] = 0xffffffff;
1692 rs[WINED3D_RS_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1693 rs[WINED3D_RS_STENCILWRITEMASK] = 0xffffffff;
1694 rs[WINED3D_RS_TEXTUREFACTOR] = 0xffffffff;
1695 rs[WINED3D_RS_WRAP0] = 0;
1696 rs[WINED3D_RS_WRAP1] = 0;
1697 rs[WINED3D_RS_WRAP2] = 0;
1698 rs[WINED3D_RS_WRAP3] = 0;
1699 rs[WINED3D_RS_WRAP4] = 0;
1700 rs[WINED3D_RS_WRAP5] = 0;
1701 rs[WINED3D_RS_WRAP6] = 0;
1702 rs[WINED3D_RS_WRAP7] = 0;
1703 rs[WINED3D_RS_CLIPPING] = TRUE;
1704 rs[WINED3D_RS_LIGHTING] = TRUE;
1705 rs[WINED3D_RS_AMBIENT] = 0;
1706 rs[WINED3D_RS_FOGVERTEXMODE] = WINED3D_FOG_NONE;
1707 rs[WINED3D_RS_COLORVERTEX] = TRUE;
1708 rs[WINED3D_RS_LOCALVIEWER] = TRUE;
1709 rs[WINED3D_RS_NORMALIZENORMALS] = FALSE;
1710 rs[WINED3D_RS_DIFFUSEMATERIALSOURCE] = WINED3D_MCS_COLOR1;
1711 rs[WINED3D_RS_SPECULARMATERIALSOURCE] = WINED3D_MCS_COLOR2;
1712 rs[WINED3D_RS_AMBIENTMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1713 rs[WINED3D_RS_EMISSIVEMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1714 rs[WINED3D_RS_VERTEXBLEND] = WINED3D_VBF_DISABLE;
1715 rs[WINED3D_RS_CLIPPLANEENABLE] = 0;
1716 rs[WINED3D_RS_SOFTWAREVERTEXPROCESSING] = FALSE;
1717 tmpfloat.f = 1.0f;
1718 rs[WINED3D_RS_POINTSIZE] = tmpfloat.d;
1719 tmpfloat.f = 1.0f;
1720 rs[WINED3D_RS_POINTSIZE_MIN] = tmpfloat.d;
1721 rs[WINED3D_RS_POINTSPRITEENABLE] = FALSE;
1722 rs[WINED3D_RS_POINTSCALEENABLE] = FALSE;
1723 tmpfloat.f = 1.0f;
1724 rs[WINED3D_RS_POINTSCALE_A] = tmpfloat.d;
1725 tmpfloat.f = 0.0f;
1726 rs[WINED3D_RS_POINTSCALE_B] = tmpfloat.d;
1727 tmpfloat.f = 0.0f;
1728 rs[WINED3D_RS_POINTSCALE_C] = tmpfloat.d;
1729 rs[WINED3D_RS_MULTISAMPLEANTIALIAS] = TRUE;
1730 rs[WINED3D_RS_MULTISAMPLEMASK] = 0xffffffff;
1731 rs[WINED3D_RS_PATCHEDGESTYLE] = WINED3D_PATCH_EDGE_DISCRETE;
1732 tmpfloat.f = 1.0f;
1733 rs[WINED3D_RS_PATCHSEGMENTS] = tmpfloat.d;
1734 rs[WINED3D_RS_DEBUGMONITORTOKEN] = 0xbaadcafe;
1735 tmpfloat.f = d3d_info->limits.pointsize_max;
1736 rs[WINED3D_RS_POINTSIZE_MAX] = tmpfloat.d;
1737 rs[WINED3D_RS_INDEXEDVERTEXBLENDENABLE] = FALSE;
1738 rs[WINED3D_RS_COLORWRITEENABLE] = 0x0000000f;
1739 tmpfloat.f = 0.0f;
1740 rs[WINED3D_RS_TWEENFACTOR] = tmpfloat.d;
1741 rs[WINED3D_RS_BLENDOP] = WINED3D_BLEND_OP_ADD;
1742 rs[WINED3D_RS_POSITIONDEGREE] = WINED3D_DEGREE_CUBIC;
1743 rs[WINED3D_RS_NORMALDEGREE] = WINED3D_DEGREE_LINEAR;
1744 /* states new in d3d9 */
1745 rs[WINED3D_RS_SCISSORTESTENABLE] = FALSE;
1746 rs[WINED3D_RS_SLOPESCALEDEPTHBIAS] = 0;
1747 tmpfloat.f = 1.0f;
1748 rs[WINED3D_RS_MINTESSELLATIONLEVEL] = tmpfloat.d;
1749 rs[WINED3D_RS_MAXTESSELLATIONLEVEL] = tmpfloat.d;
1750 rs[WINED3D_RS_ANTIALIASEDLINEENABLE] = FALSE;
1751 tmpfloat.f = 0.0f;
1752 rs[WINED3D_RS_ADAPTIVETESS_X] = tmpfloat.d;
1753 rs[WINED3D_RS_ADAPTIVETESS_Y] = tmpfloat.d;
1754 tmpfloat.f = 1.0f;
1755 rs[WINED3D_RS_ADAPTIVETESS_Z] = tmpfloat.d;
1756 tmpfloat.f = 0.0f;
1757 rs[WINED3D_RS_ADAPTIVETESS_W] = tmpfloat.d;
1758 rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION] = FALSE;
1759 rs[WINED3D_RS_TWOSIDEDSTENCILMODE] = FALSE;
1760 rs[WINED3D_RS_BACK_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1761 rs[WINED3D_RS_BACK_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1762 rs[WINED3D_RS_BACK_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1763 rs[WINED3D_RS_BACK_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1764 rs[WINED3D_RS_COLORWRITEENABLE1] = 0x0000000f;
1765 rs[WINED3D_RS_COLORWRITEENABLE2] = 0x0000000f;
1766 rs[WINED3D_RS_COLORWRITEENABLE3] = 0x0000000f;
1767 rs[WINED3D_RS_BLENDFACTOR] = 0xffffffff;
1768 rs[WINED3D_RS_SRGBWRITEENABLE] = 0;
1769 rs[WINED3D_RS_DEPTHBIAS] = 0;
1770 rs[WINED3D_RS_WRAP8] = 0;
1771 rs[WINED3D_RS_WRAP9] = 0;
1772 rs[WINED3D_RS_WRAP10] = 0;
1773 rs[WINED3D_RS_WRAP11] = 0;
1774 rs[WINED3D_RS_WRAP12] = 0;
1775 rs[WINED3D_RS_WRAP13] = 0;
1776 rs[WINED3D_RS_WRAP14] = 0;
1777 rs[WINED3D_RS_WRAP15] = 0;
1778 rs[WINED3D_RS_SEPARATEALPHABLENDENABLE] = FALSE;
1779 rs[WINED3D_RS_SRCBLENDALPHA] = WINED3D_BLEND_ONE;
1780 rs[WINED3D_RS_DESTBLENDALPHA] = WINED3D_BLEND_ZERO;
1781 rs[WINED3D_RS_BLENDOPALPHA] = WINED3D_BLEND_OP_ADD;
1784 static void init_default_texture_state(unsigned int i, DWORD stage[WINED3D_HIGHEST_TEXTURE_STATE + 1])
1786 stage[WINED3D_TSS_COLOR_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_MODULATE;
1787 stage[WINED3D_TSS_COLOR_ARG1] = WINED3DTA_TEXTURE;
1788 stage[WINED3D_TSS_COLOR_ARG2] = WINED3DTA_CURRENT;
1789 stage[WINED3D_TSS_ALPHA_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_SELECT_ARG1;
1790 stage[WINED3D_TSS_ALPHA_ARG1] = WINED3DTA_TEXTURE;
1791 stage[WINED3D_TSS_ALPHA_ARG2] = WINED3DTA_CURRENT;
1792 stage[WINED3D_TSS_BUMPENV_MAT00] = 0;
1793 stage[WINED3D_TSS_BUMPENV_MAT01] = 0;
1794 stage[WINED3D_TSS_BUMPENV_MAT10] = 0;
1795 stage[WINED3D_TSS_BUMPENV_MAT11] = 0;
1796 stage[WINED3D_TSS_TEXCOORD_INDEX] = i;
1797 stage[WINED3D_TSS_BUMPENV_LSCALE] = 0;
1798 stage[WINED3D_TSS_BUMPENV_LOFFSET] = 0;
1799 stage[WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS] = WINED3D_TTFF_DISABLE;
1800 stage[WINED3D_TSS_COLOR_ARG0] = WINED3DTA_CURRENT;
1801 stage[WINED3D_TSS_ALPHA_ARG0] = WINED3DTA_CURRENT;
1802 stage[WINED3D_TSS_RESULT_ARG] = WINED3DTA_CURRENT;
1805 static void init_default_sampler_states(DWORD states[WINED3D_MAX_COMBINED_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1])
1807 unsigned int i;
1809 for (i = 0 ; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1811 TRACE("Setting up default samplers states for sampler %u.\n", i);
1812 states[i][WINED3D_SAMP_ADDRESS_U] = WINED3D_TADDRESS_WRAP;
1813 states[i][WINED3D_SAMP_ADDRESS_V] = WINED3D_TADDRESS_WRAP;
1814 states[i][WINED3D_SAMP_ADDRESS_W] = WINED3D_TADDRESS_WRAP;
1815 states[i][WINED3D_SAMP_BORDER_COLOR] = 0;
1816 states[i][WINED3D_SAMP_MAG_FILTER] = WINED3D_TEXF_POINT;
1817 states[i][WINED3D_SAMP_MIN_FILTER] = WINED3D_TEXF_POINT;
1818 states[i][WINED3D_SAMP_MIP_FILTER] = WINED3D_TEXF_NONE;
1819 states[i][WINED3D_SAMP_MIPMAP_LOD_BIAS] = 0;
1820 states[i][WINED3D_SAMP_MAX_MIP_LEVEL] = 0;
1821 states[i][WINED3D_SAMP_MAX_ANISOTROPY] = 1;
1822 states[i][WINED3D_SAMP_SRGB_TEXTURE] = 0;
1823 /* TODO: Indicates which element of a multielement texture to use. */
1824 states[i][WINED3D_SAMP_ELEMENT_INDEX] = 0;
1825 /* TODO: Vertex offset in the presampled displacement map. */
1826 states[i][WINED3D_SAMP_DMAP_OFFSET] = 0;
1830 static void state_init_default(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info)
1832 struct wined3d_matrix identity;
1833 unsigned int i, j;
1835 TRACE("state %p, d3d_info %p.\n", state, d3d_info);
1837 get_identity_matrix(&identity);
1838 state->primitive_type = WINED3D_PT_UNDEFINED;
1839 state->patch_vertex_count = 0;
1841 /* Set some of the defaults for lights, transforms etc */
1842 state->transforms[WINED3D_TS_PROJECTION] = identity;
1843 state->transforms[WINED3D_TS_VIEW] = identity;
1844 for (i = 0; i < 256; ++i)
1846 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
1849 init_default_render_states(state->render_states, d3d_info);
1851 /* Texture Stage States - Put directly into state block, we will call function below */
1852 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1854 TRACE("Setting up default texture states for texture Stage %u.\n", i);
1855 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
1856 init_default_texture_state(i, state->texture_states[i]);
1859 init_default_sampler_states(state->sampler_states);
1861 state->blend_factor.r = 1.0f;
1862 state->blend_factor.g = 1.0f;
1863 state->blend_factor.b = 1.0f;
1864 state->blend_factor.a = 1.0f;
1866 state->sample_mask = 0xffffffff;
1868 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1869 state->streams[i].frequency = 1;
1871 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1873 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
1874 state->cb[i][j].size = WINED3D_MAX_CONSTANT_BUFFER_SIZE * 16;
1878 void state_init(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info,
1879 uint32_t flags, enum wined3d_feature_level feature_level)
1881 unsigned int i;
1883 state->feature_level = feature_level;
1884 state->flags = flags;
1886 for (i = 0; i < LIGHTMAP_SIZE; i++)
1888 list_init(&state->light_state.light_map[i]);
1891 if (flags & WINED3D_STATE_INIT_DEFAULT)
1892 state_init_default(state, d3d_info);
1895 static bool wined3d_select_feature_level(const struct wined3d_adapter *adapter,
1896 const enum wined3d_feature_level *levels, unsigned int level_count,
1897 enum wined3d_feature_level *selected_level)
1899 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
1900 unsigned int i;
1902 for (i = 0; i < level_count; ++i)
1904 if (levels[i] && d3d_info->feature_level >= levels[i])
1906 *selected_level = levels[i];
1907 return true;
1911 FIXME_(winediag)("None of the requested D3D feature levels is supported on this GPU "
1912 "with the current shader backend.\n");
1913 return false;
1916 HRESULT CDECL wined3d_state_create(struct wined3d_device *device,
1917 const enum wined3d_feature_level *levels, unsigned int level_count, struct wined3d_state **state)
1919 enum wined3d_feature_level feature_level;
1920 struct wined3d_state *object;
1922 TRACE("device %p, levels %p, level_count %u, state %p.\n", device, levels, level_count, state);
1924 if (!wined3d_select_feature_level(device->adapter, levels, level_count, &feature_level))
1925 return E_FAIL;
1927 TRACE("Selected feature level %s.\n", wined3d_debug_feature_level(feature_level));
1929 if (!(object = heap_alloc_zero(sizeof(*object))))
1930 return E_OUTOFMEMORY;
1931 state_init(object, &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT, feature_level);
1933 *state = object;
1934 return S_OK;
1937 enum wined3d_feature_level CDECL wined3d_state_get_feature_level(const struct wined3d_state *state)
1939 TRACE("state %p.\n", state);
1941 return state->feature_level;
1944 void CDECL wined3d_state_destroy(struct wined3d_state *state)
1946 TRACE("state %p.\n", state);
1948 state_cleanup(state);
1949 heap_free(state);
1952 static void stateblock_state_init_default(struct wined3d_stateblock_state *state,
1953 const struct wined3d_d3d_info *d3d_info)
1955 struct wined3d_matrix identity;
1956 unsigned int i;
1958 get_identity_matrix(&identity);
1960 state->transforms[WINED3D_TS_PROJECTION] = identity;
1961 state->transforms[WINED3D_TS_VIEW] = identity;
1962 for (i = 0; i < 256; ++i)
1964 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
1967 init_default_render_states(state->rs, d3d_info);
1969 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1971 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
1972 init_default_texture_state(i, state->texture_states[i]);
1975 init_default_sampler_states(state->sampler_states);
1977 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1978 state->streams[i].frequency = 1;
1981 void wined3d_stateblock_state_init(struct wined3d_stateblock_state *state,
1982 const struct wined3d_device *device, DWORD flags)
1984 unsigned int i;
1986 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); i++)
1988 list_init(&state->light_state->light_map[i]);
1991 if (flags & WINED3D_STATE_INIT_DEFAULT)
1992 stateblock_state_init_default(state, &device->adapter->d3d_info);
1996 static HRESULT stateblock_init(struct wined3d_stateblock *stateblock, const struct wined3d_stateblock *device_state,
1997 struct wined3d_device *device, enum wined3d_stateblock_type type)
1999 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2001 stateblock->ref = 1;
2002 stateblock->device = device;
2003 stateblock->stateblock_state.light_state = &stateblock->light_state;
2004 wined3d_stateblock_state_init(&stateblock->stateblock_state, device,
2005 type == WINED3D_SBT_PRIMARY ? WINED3D_STATE_INIT_DEFAULT : 0);
2007 stateblock->changed.store_stream_offset = 1;
2009 if (type == WINED3D_SBT_RECORDED || type == WINED3D_SBT_PRIMARY)
2010 return WINED3D_OK;
2012 TRACE("Updating changed flags appropriate for type %#x.\n", type);
2014 switch (type)
2016 case WINED3D_SBT_ALL:
2017 stateblock_init_lights(stateblock->stateblock_state.light_state->light_map,
2018 device_state->stateblock_state.light_state->light_map);
2019 stateblock_savedstates_set_all(&stateblock->changed,
2020 d3d_info->limits.vs_uniform_count, d3d_info->limits.ps_uniform_count);
2021 break;
2023 case WINED3D_SBT_PIXEL_STATE:
2024 stateblock_savedstates_set_pixel(&stateblock->changed,
2025 d3d_info->limits.ps_uniform_count);
2026 break;
2028 case WINED3D_SBT_VERTEX_STATE:
2029 stateblock_init_lights(stateblock->stateblock_state.light_state->light_map,
2030 device_state->stateblock_state.light_state->light_map);
2031 stateblock_savedstates_set_vertex(&stateblock->changed,
2032 d3d_info->limits.vs_uniform_count);
2033 break;
2035 default:
2036 FIXME("Unrecognized state block type %#x.\n", type);
2037 break;
2040 wined3d_stateblock_init_contained_states(stateblock);
2041 wined3d_stateblock_capture(stateblock, device_state);
2043 /* According to the tests, stream offset is not updated in the captured state if
2044 * the state was captured on state block creation. This is not the case for
2045 * state blocks initialized with BeginStateBlock / EndStateBlock, multiple
2046 * captures get stream offsets updated. */
2047 stateblock->changed.store_stream_offset = 0;
2049 return WINED3D_OK;
2052 HRESULT CDECL wined3d_stateblock_create(struct wined3d_device *device, const struct wined3d_stateblock *device_state,
2053 enum wined3d_stateblock_type type, struct wined3d_stateblock **stateblock)
2055 struct wined3d_stateblock *object;
2056 HRESULT hr;
2058 TRACE("device %p, device_state %p, type %#x, stateblock %p.\n",
2059 device, device_state, type, stateblock);
2061 if (!(object = heap_alloc_zero(sizeof(*object))))
2062 return E_OUTOFMEMORY;
2064 hr = stateblock_init(object, device_state, device, type);
2065 if (FAILED(hr))
2067 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2068 heap_free(object);
2069 return hr;
2072 TRACE("Created stateblock %p.\n", object);
2073 *stateblock = object;
2075 return WINED3D_OK;
2078 void CDECL wined3d_stateblock_reset(struct wined3d_stateblock *stateblock)
2080 TRACE("stateblock %p.\n", stateblock);
2082 wined3d_stateblock_state_cleanup(&stateblock->stateblock_state);
2083 memset(&stateblock->stateblock_state, 0, sizeof(stateblock->stateblock_state));
2084 stateblock->stateblock_state.light_state = &stateblock->light_state;
2085 wined3d_stateblock_state_init(&stateblock->stateblock_state, stateblock->device, WINED3D_STATE_INIT_DEFAULT);