wined3d: Use wined3d_mask_from_size() in shader_glsl_gather4().
[wine.git] / dlls / wined3d / stateblock.c
blobc5ca59900097be0a5451b0bf72cf25b0420fec76
1 /*
2 * state block implementation
4 * Copyright 2002 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2007 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "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(DWORD *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 list *dst_map, const struct list *src_map)
346 unsigned int i;
348 for (i = 0; i < LIGHTMAP_SIZE; ++i)
350 const struct wined3d_light_info *src_light;
352 LIST_FOR_EACH_ENTRY(src_light, &src_map[i], struct wined3d_light_info, entry)
354 struct wined3d_light_info *dst_light = heap_alloc(sizeof(*dst_light));
356 *dst_light = *src_light;
357 list_add_tail(&dst_map[i], &dst_light->entry);
362 ULONG CDECL wined3d_stateblock_incref(struct wined3d_stateblock *stateblock)
364 ULONG refcount = InterlockedIncrement(&stateblock->ref);
366 TRACE("%p increasing refcount to %u.\n", stateblock, refcount);
368 return refcount;
371 void state_unbind_resources(struct wined3d_state *state)
373 struct wined3d_unordered_access_view *uav;
374 struct wined3d_shader_resource_view *srv;
375 struct wined3d_vertex_declaration *decl;
376 struct wined3d_blend_state *blend_state;
377 struct wined3d_rendertarget_view *rtv;
378 struct wined3d_sampler *sampler;
379 struct wined3d_texture *texture;
380 struct wined3d_buffer *buffer;
381 struct wined3d_shader *shader;
382 unsigned int i, j;
384 if ((decl = state->vertex_declaration))
386 state->vertex_declaration = NULL;
387 wined3d_vertex_declaration_decref(decl);
390 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
392 if ((texture = state->textures[i]))
394 state->textures[i] = NULL;
395 wined3d_texture_decref(texture);
399 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
401 if ((buffer = state->stream_output[i].buffer))
403 state->stream_output[i].buffer = NULL;
404 wined3d_buffer_decref(buffer);
408 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
410 if ((buffer = state->streams[i].buffer))
412 state->streams[i].buffer = NULL;
413 wined3d_buffer_decref(buffer);
417 if ((buffer = state->index_buffer))
419 state->index_buffer = NULL;
420 wined3d_buffer_decref(buffer);
423 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
425 if ((shader = state->shader[i]))
427 state->shader[i] = NULL;
428 wined3d_shader_decref(shader);
431 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
433 if ((buffer = state->cb[i][j].buffer))
435 state->cb[i][j].buffer = NULL;
436 wined3d_buffer_decref(buffer);
440 for (j = 0; j < MAX_SAMPLER_OBJECTS; ++j)
442 if ((sampler = state->sampler[i][j]))
444 state->sampler[i][j] = NULL;
445 wined3d_sampler_decref(sampler);
449 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
451 if ((srv = state->shader_resource_view[i][j]))
453 state->shader_resource_view[i][j] = NULL;
454 wined3d_srv_bind_count_dec(srv);
455 wined3d_shader_resource_view_decref(srv);
460 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
462 for (j = 0; j < MAX_UNORDERED_ACCESS_VIEWS; ++j)
464 if ((uav = state->unordered_access_view[i][j]))
466 state->unordered_access_view[i][j] = NULL;
467 wined3d_unordered_access_view_decref(uav);
472 if ((blend_state = state->blend_state))
474 state->blend_state = NULL;
475 wined3d_blend_state_decref(blend_state);
478 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
480 if ((rtv = state->fb.render_targets[i]))
482 state->fb.render_targets[i] = NULL;
483 wined3d_rendertarget_view_decref(rtv);
487 if ((rtv = state->fb.depth_stencil))
489 state->fb.depth_stencil = NULL;
490 wined3d_rendertarget_view_decref(rtv);
494 void wined3d_stateblock_state_cleanup(struct wined3d_stateblock_state *state)
496 struct wined3d_light_info *light, *cursor;
497 struct wined3d_vertex_declaration *decl;
498 struct wined3d_texture *texture;
499 struct wined3d_buffer *buffer;
500 struct wined3d_shader *shader;
501 unsigned int i;
503 if ((decl = state->vertex_declaration))
505 state->vertex_declaration = NULL;
506 wined3d_vertex_declaration_decref(decl);
509 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
511 if ((buffer = state->streams[i].buffer))
513 state->streams[i].buffer = NULL;
514 wined3d_buffer_decref(buffer);
518 if ((buffer = state->index_buffer))
520 state->index_buffer = NULL;
521 wined3d_buffer_decref(buffer);
524 if ((shader = state->vs))
526 state->vs = NULL;
527 wined3d_shader_decref(shader);
530 if ((shader = state->ps))
532 state->ps = NULL;
533 wined3d_shader_decref(shader);
536 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
538 if ((texture = state->textures[i]))
540 state->textures[i] = NULL;
541 wined3d_texture_decref(texture);
545 for (i = 0; i < LIGHTMAP_SIZE; ++i)
547 LIST_FOR_EACH_ENTRY_SAFE(light, cursor, &state->light_state->light_map[i], struct wined3d_light_info, entry)
549 list_remove(&light->entry);
550 heap_free(light);
555 void state_cleanup(struct wined3d_state *state)
557 unsigned int counter;
559 if (!(state->flags & WINED3D_STATE_NO_REF))
560 state_unbind_resources(state);
562 for (counter = 0; counter < WINED3D_MAX_ACTIVE_LIGHTS; ++counter)
564 state->light_state.lights[counter] = NULL;
567 for (counter = 0; counter < LIGHTMAP_SIZE; ++counter)
569 struct list *e1, *e2;
570 LIST_FOR_EACH_SAFE(e1, e2, &state->light_state.light_map[counter])
572 struct wined3d_light_info *light = LIST_ENTRY(e1, struct wined3d_light_info, entry);
573 list_remove(&light->entry);
574 heap_free(light);
579 ULONG CDECL wined3d_stateblock_decref(struct wined3d_stateblock *stateblock)
581 ULONG refcount = InterlockedDecrement(&stateblock->ref);
583 TRACE("%p decreasing refcount to %u\n", stateblock, refcount);
585 if (!refcount)
587 wined3d_mutex_lock();
588 wined3d_stateblock_state_cleanup(&stateblock->stateblock_state);
589 heap_free(stateblock);
590 wined3d_mutex_unlock();
593 return refcount;
596 struct wined3d_light_info *wined3d_light_state_get_light(const struct wined3d_light_state *state, unsigned int idx)
598 struct wined3d_light_info *light_info;
599 unsigned int hash_idx;
601 hash_idx = LIGHTMAP_HASHFUNC(idx);
602 LIST_FOR_EACH_ENTRY(light_info, &state->light_map[hash_idx], struct wined3d_light_info, entry)
604 if (light_info->OriginalIndex == idx)
605 return light_info;
608 return NULL;
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;
615 unsigned int hash_idx;
617 if (!(object = wined3d_light_state_get_light(state, light_idx)))
619 TRACE("Adding new light.\n");
620 if (!(object = heap_alloc_zero(sizeof(*object))))
622 ERR("Failed to allocate light info.\n");
623 return E_OUTOFMEMORY;
626 hash_idx = LIGHTMAP_HASHFUNC(light_idx);
627 list_add_head(&state->light_map[hash_idx], &object->entry);
628 object->glIndex = -1;
629 object->OriginalIndex = light_idx;
632 object->OriginalParms = *params;
634 *light_info = object;
635 return WINED3D_OK;
638 void wined3d_light_state_enable_light(struct wined3d_light_state *state, const struct wined3d_d3d_info *d3d_info,
639 struct wined3d_light_info *light_info, BOOL enable)
641 unsigned int light_count, i;
643 if (!(light_info->enabled = enable))
645 if (light_info->glIndex == -1)
647 TRACE("Light already disabled, nothing to do.\n");
648 return;
651 state->lights[light_info->glIndex] = NULL;
652 light_info->glIndex = -1;
653 return;
656 if (light_info->glIndex != -1)
658 TRACE("Light already enabled, nothing to do.\n");
659 return;
662 /* Find a free light. */
663 light_count = d3d_info->limits.active_light_count;
664 for (i = 0; i < light_count; ++i)
666 if (state->lights[i])
667 continue;
669 state->lights[i] = light_info;
670 light_info->glIndex = i;
671 return;
674 /* Our tests show that Windows returns D3D_OK in this situation, even with
675 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices.
676 * This is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns
677 * TRUE * as well for those lights.
679 * TODO: Test how this affects rendering. */
680 WARN("Too many concurrently active lights.\n");
683 static void wined3d_state_record_lights(struct wined3d_light_state *dst_state,
684 const struct wined3d_light_state *src_state)
686 const struct wined3d_light_info *src;
687 struct wined3d_light_info *dst;
688 UINT i;
690 /* Lights... For a recorded state block, we just had a chain of actions
691 * to perform, so we need to walk that chain and update any actions which
692 * differ. */
693 for (i = 0; i < LIGHTMAP_SIZE; ++i)
695 LIST_FOR_EACH_ENTRY(dst, &dst_state->light_map[i], struct wined3d_light_info, entry)
697 if ((src = wined3d_light_state_get_light(src_state, dst->OriginalIndex)))
699 dst->OriginalParms = src->OriginalParms;
701 if (src->glIndex == -1 && dst->glIndex != -1)
703 /* Light disabled. */
704 dst_state->lights[dst->glIndex] = NULL;
706 else if (src->glIndex != -1 && dst->glIndex == -1)
708 /* Light enabled. */
709 dst_state->lights[src->glIndex] = dst;
711 dst->glIndex = src->glIndex;
713 else
715 /* This can happen if the light was originally created as a
716 * default light for SetLightEnable() while recording. */
717 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
718 dst->OriginalIndex, dst_state, src_state);
720 dst->OriginalParms = WINED3D_default_light;
721 if (dst->glIndex != -1)
723 dst_state->lights[dst->glIndex] = NULL;
724 dst->glIndex = -1;
731 void CDECL wined3d_stateblock_capture(struct wined3d_stateblock *stateblock,
732 const struct wined3d_stateblock *device_state)
734 const struct wined3d_stateblock_state *state = &device_state->stateblock_state;
735 struct wined3d_range range;
736 unsigned int i, start;
737 DWORD map;
739 TRACE("stateblock %p, device_state %p.\n", stateblock, device_state);
741 if (stateblock->changed.vertexShader && stateblock->stateblock_state.vs != state->vs)
743 TRACE("Updating vertex shader from %p to %p.\n", stateblock->stateblock_state.vs, state->vs);
745 if (state->vs)
746 wined3d_shader_incref(state->vs);
747 if (stateblock->stateblock_state.vs)
748 wined3d_shader_decref(stateblock->stateblock_state.vs);
749 stateblock->stateblock_state.vs = state->vs;
752 for (start = 0; ; start = range.offset + range.size)
754 if (!wined3d_bitmap_get_range(stateblock->changed.vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
755 break;
757 memcpy(&stateblock->stateblock_state.vs_consts_f[range.offset], &state->vs_consts_f[range.offset],
758 sizeof(*state->vs_consts_f) * range.size);
760 map = stateblock->changed.vertexShaderConstantsI;
761 for (start = 0; ; start = range.offset + range.size)
763 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
764 break;
766 memcpy(&stateblock->stateblock_state.vs_consts_i[range.offset], &state->vs_consts_i[range.offset],
767 sizeof(*state->vs_consts_i) * range.size);
769 map = stateblock->changed.vertexShaderConstantsB;
770 for (start = 0; ; start = range.offset + range.size)
772 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
773 break;
775 memcpy(&stateblock->stateblock_state.vs_consts_b[range.offset], &state->vs_consts_b[range.offset],
776 sizeof(*state->vs_consts_b) * range.size);
779 for (start = 0; ; start = range.offset + range.size)
781 if (!wined3d_bitmap_get_range(stateblock->changed.ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
782 break;
784 memcpy(&stateblock->stateblock_state.ps_consts_f[range.offset], &state->ps_consts_f[range.offset],
785 sizeof(*state->ps_consts_f) * range.size);
787 map = stateblock->changed.pixelShaderConstantsI;
788 for (start = 0; ; start = range.offset + range.size)
790 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
791 break;
793 memcpy(&stateblock->stateblock_state.ps_consts_i[range.offset], &state->ps_consts_i[range.offset],
794 sizeof(*state->ps_consts_i) * range.size);
796 map = stateblock->changed.pixelShaderConstantsB;
797 for (start = 0; ; start = range.offset + range.size)
799 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
800 break;
802 memcpy(&stateblock->stateblock_state.ps_consts_b[range.offset], &state->ps_consts_b[range.offset],
803 sizeof(*state->ps_consts_b) * range.size);
806 if (stateblock->changed.transforms)
808 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
810 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
812 TRACE("Updating transform %#x.\n", transform);
814 stateblock->stateblock_state.transforms[transform] = state->transforms[transform];
818 if (stateblock->changed.indices
819 && ((stateblock->stateblock_state.index_buffer != state->index_buffer)
820 || (stateblock->stateblock_state.base_vertex_index != state->base_vertex_index)
821 || (stateblock->stateblock_state.index_format != state->index_format)))
823 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
824 state->index_buffer, state->base_vertex_index);
826 if (state->index_buffer)
827 wined3d_buffer_incref(state->index_buffer);
828 if (stateblock->stateblock_state.index_buffer)
829 wined3d_buffer_decref(stateblock->stateblock_state.index_buffer);
830 stateblock->stateblock_state.index_buffer = state->index_buffer;
831 stateblock->stateblock_state.base_vertex_index = state->base_vertex_index;
832 stateblock->stateblock_state.index_format = state->index_format;
835 if (stateblock->changed.vertexDecl && stateblock->stateblock_state.vertex_declaration != state->vertex_declaration)
837 TRACE("Updating vertex declaration from %p to %p.\n",
838 stateblock->stateblock_state.vertex_declaration, state->vertex_declaration);
840 if (state->vertex_declaration)
841 wined3d_vertex_declaration_incref(state->vertex_declaration);
842 if (stateblock->stateblock_state.vertex_declaration)
843 wined3d_vertex_declaration_decref(stateblock->stateblock_state.vertex_declaration);
844 stateblock->stateblock_state.vertex_declaration = state->vertex_declaration;
847 if (stateblock->changed.material
848 && memcmp(&state->material, &stateblock->stateblock_state.material,
849 sizeof(stateblock->stateblock_state.material)))
851 TRACE("Updating material.\n");
853 stateblock->stateblock_state.material = state->material;
856 if (stateblock->changed.viewport
857 && memcmp(&state->viewport, &stateblock->stateblock_state.viewport, sizeof(state->viewport)))
859 TRACE("Updating viewport.\n");
861 stateblock->stateblock_state.viewport = state->viewport;
864 if (stateblock->changed.scissorRect
865 && memcmp(&state->scissor_rect, &stateblock->stateblock_state.scissor_rect, sizeof(state->scissor_rect)))
867 TRACE("Updating scissor rect.\n");
869 stateblock->stateblock_state.scissor_rect = state->scissor_rect;
872 map = stateblock->changed.streamSource;
873 while (map)
875 i = wined3d_bit_scan(&map);
877 if (stateblock->stateblock_state.streams[i].stride != state->streams[i].stride
878 || stateblock->stateblock_state.streams[i].offset != state->streams[i].offset
879 || stateblock->stateblock_state.streams[i].buffer != state->streams[i].buffer)
881 TRACE("stateblock %p, stream source %u, buffer %p, stride %u, offset %u.\n",
882 stateblock, i, state->streams[i].buffer, state->streams[i].stride,
883 state->streams[i].offset);
885 stateblock->stateblock_state.streams[i].stride = state->streams[i].stride;
886 if (stateblock->changed.store_stream_offset)
887 stateblock->stateblock_state.streams[i].offset = state->streams[i].offset;
889 if (state->streams[i].buffer)
890 wined3d_buffer_incref(state->streams[i].buffer);
891 if (stateblock->stateblock_state.streams[i].buffer)
892 wined3d_buffer_decref(stateblock->stateblock_state.streams[i].buffer);
893 stateblock->stateblock_state.streams[i].buffer = state->streams[i].buffer;
897 map = stateblock->changed.streamFreq;
898 while (map)
900 i = wined3d_bit_scan(&map);
902 if (stateblock->stateblock_state.streams[i].frequency != state->streams[i].frequency
903 || stateblock->stateblock_state.streams[i].flags != state->streams[i].flags)
905 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
906 i, state->streams[i].frequency, state->streams[i].flags);
908 stateblock->stateblock_state.streams[i].frequency = state->streams[i].frequency;
909 stateblock->stateblock_state.streams[i].flags = state->streams[i].flags;
913 map = stateblock->changed.clipplane;
914 while (map)
916 i = wined3d_bit_scan(&map);
918 if (memcmp(&stateblock->stateblock_state.clip_planes[i], &state->clip_planes[i], sizeof(state->clip_planes[i])))
920 TRACE("Updating clipplane %u.\n", i);
921 stateblock->stateblock_state.clip_planes[i] = state->clip_planes[i];
925 /* Render */
926 for (i = 0; i < stateblock->num_contained_render_states; ++i)
928 enum wined3d_render_state rs = stateblock->contained_render_states[i];
930 TRACE("Updating render state %#x to %u.\n", rs, state->rs[rs]);
932 stateblock->stateblock_state.rs[rs] = state->rs[rs];
935 /* Texture states */
936 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
938 DWORD stage = stateblock->contained_tss_states[i].stage;
939 DWORD texture_state = stateblock->contained_tss_states[i].state;
941 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage, texture_state,
942 state->texture_states[stage][texture_state],
943 stateblock->stateblock_state.texture_states[stage][texture_state]);
945 stateblock->stateblock_state.texture_states[stage][texture_state] = state->texture_states[stage][texture_state];
948 /* Samplers */
949 map = stateblock->changed.textures;
950 while (map)
952 i = wined3d_bit_scan(&map);
954 TRACE("Updating texture %u to %p (was %p).\n",
955 i, state->textures[i], stateblock->stateblock_state.textures[i]);
957 if (state->textures[i])
958 wined3d_texture_incref(state->textures[i]);
959 if (stateblock->stateblock_state.textures[i])
960 wined3d_texture_decref(stateblock->stateblock_state.textures[i]);
961 stateblock->stateblock_state.textures[i] = state->textures[i];
964 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
966 DWORD stage = stateblock->contained_sampler_states[i].stage;
967 DWORD sampler_state = stateblock->contained_sampler_states[i].state;
969 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage, sampler_state,
970 state->sampler_states[stage][sampler_state],
971 stateblock->stateblock_state.sampler_states[stage][sampler_state]);
973 stateblock->stateblock_state.sampler_states[stage][sampler_state] = state->sampler_states[stage][sampler_state];
976 if (stateblock->changed.pixelShader && stateblock->stateblock_state.ps != state->ps)
978 if (state->ps)
979 wined3d_shader_incref(state->ps);
980 if (stateblock->stateblock_state.ps)
981 wined3d_shader_decref(stateblock->stateblock_state.ps);
982 stateblock->stateblock_state.ps = state->ps;
985 if (stateblock->changed.lights)
986 wined3d_state_record_lights(stateblock->stateblock_state.light_state, state->light_state);
988 if (stateblock->changed.alpha_to_coverage)
989 stateblock->stateblock_state.alpha_to_coverage = state->alpha_to_coverage;
991 TRACE("Capture done.\n");
994 void CDECL wined3d_stateblock_apply(const struct wined3d_stateblock *stateblock,
995 struct wined3d_stateblock *device_state)
997 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
998 struct wined3d_range range;
999 unsigned int i, start;
1000 DWORD map;
1002 TRACE("stateblock %p, device_state %p.\n", stateblock, device_state);
1004 if (stateblock->changed.vertexShader)
1005 wined3d_stateblock_set_vertex_shader(device_state, state->vs);
1006 if (stateblock->changed.pixelShader)
1007 wined3d_stateblock_set_pixel_shader(device_state, state->ps);
1009 for (start = 0; ; start = range.offset + range.size)
1011 if (!wined3d_bitmap_get_range(stateblock->changed.vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
1012 break;
1013 wined3d_stateblock_set_vs_consts_f(device_state, range.offset, range.size, &state->vs_consts_f[range.offset]);
1015 map = stateblock->changed.vertexShaderConstantsI;
1016 for (start = 0; ; start = range.offset + range.size)
1018 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
1019 break;
1020 wined3d_stateblock_set_vs_consts_i(device_state, range.offset, range.size, &state->vs_consts_i[range.offset]);
1022 map = stateblock->changed.vertexShaderConstantsB;
1023 for (start = 0; ; start = range.offset + range.size)
1025 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
1026 break;
1027 wined3d_stateblock_set_vs_consts_b(device_state, range.offset, range.size, &state->vs_consts_b[range.offset]);
1030 for (start = 0; ; start = range.offset + range.size)
1032 if (!wined3d_bitmap_get_range(stateblock->changed.ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
1033 break;
1034 wined3d_stateblock_set_ps_consts_f(device_state, range.offset, range.size, &state->ps_consts_f[range.offset]);
1036 map = stateblock->changed.pixelShaderConstantsI;
1037 for (start = 0; ; start = range.offset + range.size)
1039 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
1040 break;
1041 wined3d_stateblock_set_ps_consts_i(device_state, range.offset, range.size, &state->ps_consts_i[range.offset]);
1043 map = stateblock->changed.pixelShaderConstantsB;
1044 for (start = 0; ; start = range.offset + range.size)
1046 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
1047 break;
1048 wined3d_stateblock_set_ps_consts_b(device_state, range.offset, range.size, &state->ps_consts_b[range.offset]);
1051 if (stateblock->changed.transforms)
1053 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
1055 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
1057 wined3d_stateblock_set_transform(device_state, transform, &state->transforms[transform]);
1061 if (stateblock->changed.lights)
1063 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); ++i)
1065 const struct wined3d_light_info *light;
1067 LIST_FOR_EACH_ENTRY(light, &state->light_state->light_map[i], struct wined3d_light_info, entry)
1069 wined3d_stateblock_set_light(device_state, light->OriginalIndex, &light->OriginalParms);
1070 wined3d_stateblock_set_light_enable(device_state, light->OriginalIndex, light->glIndex != -1);
1075 if (stateblock->changed.alpha_to_coverage)
1077 device_state->stateblock_state.alpha_to_coverage = state->alpha_to_coverage;
1078 device_state->changed.alpha_to_coverage = 1;
1081 /* Render states. */
1082 for (i = 0; i < stateblock->num_contained_render_states; ++i)
1084 enum wined3d_render_state rs = stateblock->contained_render_states[i];
1086 wined3d_stateblock_set_render_state(device_state, rs, state->rs[rs]);
1089 /* Texture states. */
1090 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
1092 DWORD stage = stateblock->contained_tss_states[i].stage;
1093 DWORD texture_state = stateblock->contained_tss_states[i].state;
1095 wined3d_stateblock_set_texture_stage_state(device_state, stage, texture_state,
1096 state->texture_states[stage][texture_state]);
1099 /* Sampler states. */
1100 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
1102 DWORD stage = stateblock->contained_sampler_states[i].stage;
1103 DWORD sampler_state = stateblock->contained_sampler_states[i].state;
1105 wined3d_stateblock_set_sampler_state(device_state, stage, sampler_state,
1106 state->sampler_states[stage][sampler_state]);
1109 if (stateblock->changed.indices)
1111 wined3d_stateblock_set_index_buffer(device_state, state->index_buffer, state->index_format);
1112 wined3d_stateblock_set_base_vertex_index(device_state, state->base_vertex_index);
1115 if (stateblock->changed.vertexDecl && state->vertex_declaration)
1116 wined3d_stateblock_set_vertex_declaration(device_state, state->vertex_declaration);
1118 if (stateblock->changed.material)
1119 wined3d_stateblock_set_material(device_state, &state->material);
1121 if (stateblock->changed.viewport)
1122 wined3d_stateblock_set_viewport(device_state, &state->viewport);
1124 if (stateblock->changed.scissorRect)
1125 wined3d_stateblock_set_scissor_rect(device_state, &state->scissor_rect);
1127 map = stateblock->changed.streamSource;
1128 while (map)
1130 i = wined3d_bit_scan(&map);
1131 wined3d_stateblock_set_stream_source(device_state, i, state->streams[i].buffer,
1132 state->streams[i].offset, state->streams[i].stride);
1135 map = stateblock->changed.streamFreq;
1136 while (map)
1138 i = wined3d_bit_scan(&map);
1139 wined3d_stateblock_set_stream_source_freq(device_state, i,
1140 state->streams[i].frequency | state->streams[i].flags);
1143 map = stateblock->changed.textures;
1144 while (map)
1146 i = wined3d_bit_scan(&map);
1147 wined3d_stateblock_set_texture(device_state, i, state->textures[i]);
1150 map = stateblock->changed.clipplane;
1151 while (map)
1153 i = wined3d_bit_scan(&map);
1154 wined3d_stateblock_set_clip_plane(device_state, i, &state->clip_planes[i]);
1157 TRACE("Applied stateblock %p.\n", stateblock);
1160 void CDECL wined3d_stateblock_set_vertex_shader(struct wined3d_stateblock *stateblock, struct wined3d_shader *shader)
1162 TRACE("stateblock %p, shader %p.\n", stateblock, shader);
1164 if (shader)
1165 wined3d_shader_incref(shader);
1166 if (stateblock->stateblock_state.vs)
1167 wined3d_shader_decref(stateblock->stateblock_state.vs);
1168 stateblock->stateblock_state.vs = shader;
1169 stateblock->changed.vertexShader = TRUE;
1172 static void wined3d_bitmap_set_bits(uint32_t *bitmap, unsigned int start, unsigned int count)
1174 const unsigned int word_bit_count = sizeof(*bitmap) * CHAR_BIT;
1175 const unsigned int shift = start % word_bit_count;
1176 uint32_t mask, last_mask;
1177 unsigned int mask_size;
1179 bitmap += start / word_bit_count;
1180 mask = ~0u << shift;
1181 mask_size = word_bit_count - shift;
1182 last_mask = (1u << (start + count) % word_bit_count) - 1;
1183 if (mask_size <= count)
1185 *bitmap |= mask;
1186 ++bitmap;
1187 count -= mask_size;
1188 mask = ~0u;
1190 if (count >= word_bit_count)
1192 memset(bitmap, 0xffu, count / word_bit_count * sizeof(*bitmap));
1193 bitmap += count / word_bit_count;
1194 count = count % word_bit_count;
1196 if (count)
1197 *bitmap |= mask & last_mask;
1200 HRESULT CDECL wined3d_stateblock_set_vs_consts_f(struct wined3d_stateblock *stateblock,
1201 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
1203 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
1205 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1206 stateblock, start_idx, count, constants);
1208 if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.vs_uniform_count))
1209 return WINED3DERR_INVALIDCALL;
1211 memcpy(&stateblock->stateblock_state.vs_consts_f[start_idx], constants, count * sizeof(*constants));
1212 wined3d_bitmap_set_bits(stateblock->changed.vs_consts_f, start_idx, count);
1213 return WINED3D_OK;
1216 HRESULT CDECL wined3d_stateblock_set_vs_consts_i(struct wined3d_stateblock *stateblock,
1217 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
1219 unsigned int i;
1221 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1222 stateblock, start_idx, count, constants);
1224 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1225 return WINED3DERR_INVALIDCALL;
1227 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1228 count = WINED3D_MAX_CONSTS_I - start_idx;
1230 memcpy(&stateblock->stateblock_state.vs_consts_i[start_idx], constants, count * sizeof(*constants));
1231 for (i = start_idx; i < count + start_idx; ++i)
1232 stateblock->changed.vertexShaderConstantsI |= (1u << i);
1233 return WINED3D_OK;
1236 HRESULT CDECL wined3d_stateblock_set_vs_consts_b(struct wined3d_stateblock *stateblock,
1237 unsigned int start_idx, unsigned int count, const BOOL *constants)
1239 unsigned int i;
1241 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1242 stateblock, start_idx, count, constants);
1244 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1245 return WINED3DERR_INVALIDCALL;
1247 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1248 count = WINED3D_MAX_CONSTS_B - start_idx;
1250 memcpy(&stateblock->stateblock_state.vs_consts_b[start_idx], constants, count * sizeof(*constants));
1251 for (i = start_idx; i < count + start_idx; ++i)
1252 stateblock->changed.vertexShaderConstantsB |= (1u << i);
1253 return WINED3D_OK;
1256 void CDECL wined3d_stateblock_set_pixel_shader(struct wined3d_stateblock *stateblock, struct wined3d_shader *shader)
1258 TRACE("stateblock %p, shader %p.\n", stateblock, shader);
1260 if (shader)
1261 wined3d_shader_incref(shader);
1262 if (stateblock->stateblock_state.ps)
1263 wined3d_shader_decref(stateblock->stateblock_state.ps);
1264 stateblock->stateblock_state.ps = shader;
1265 stateblock->changed.pixelShader = TRUE;
1268 HRESULT CDECL wined3d_stateblock_set_ps_consts_f(struct wined3d_stateblock *stateblock,
1269 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
1271 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
1273 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1274 stateblock, start_idx, count, constants);
1276 if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.ps_uniform_count))
1277 return WINED3DERR_INVALIDCALL;
1279 memcpy(&stateblock->stateblock_state.ps_consts_f[start_idx], constants, count * sizeof(*constants));
1280 wined3d_bitmap_set_bits(stateblock->changed.ps_consts_f, start_idx, count);
1281 return WINED3D_OK;
1284 HRESULT CDECL wined3d_stateblock_set_ps_consts_i(struct wined3d_stateblock *stateblock,
1285 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
1287 unsigned int i;
1289 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1290 stateblock, start_idx, count, constants);
1292 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1293 return WINED3DERR_INVALIDCALL;
1295 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1296 count = WINED3D_MAX_CONSTS_I - start_idx;
1298 memcpy(&stateblock->stateblock_state.ps_consts_i[start_idx], constants, count * sizeof(*constants));
1299 for (i = start_idx; i < count + start_idx; ++i)
1300 stateblock->changed.pixelShaderConstantsI |= (1u << i);
1301 return WINED3D_OK;
1304 HRESULT CDECL wined3d_stateblock_set_ps_consts_b(struct wined3d_stateblock *stateblock,
1305 unsigned int start_idx, unsigned int count, const BOOL *constants)
1307 unsigned int i;
1309 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1310 stateblock, start_idx, count, constants);
1312 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1313 return WINED3DERR_INVALIDCALL;
1315 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1316 count = WINED3D_MAX_CONSTS_B - start_idx;
1318 memcpy(&stateblock->stateblock_state.ps_consts_b[start_idx], constants, count * sizeof(*constants));
1319 for (i = start_idx; i < count + start_idx; ++i)
1320 stateblock->changed.pixelShaderConstantsB |= (1u << i);
1321 return WINED3D_OK;
1324 void CDECL wined3d_stateblock_set_vertex_declaration(struct wined3d_stateblock *stateblock,
1325 struct wined3d_vertex_declaration *declaration)
1327 TRACE("stateblock %p, declaration %p.\n", stateblock, declaration);
1329 if (declaration)
1330 wined3d_vertex_declaration_incref(declaration);
1331 if (stateblock->stateblock_state.vertex_declaration)
1332 wined3d_vertex_declaration_decref(stateblock->stateblock_state.vertex_declaration);
1333 stateblock->stateblock_state.vertex_declaration = declaration;
1334 stateblock->changed.vertexDecl = TRUE;
1337 void CDECL wined3d_stateblock_set_render_state(struct wined3d_stateblock *stateblock,
1338 enum wined3d_render_state state, DWORD value)
1340 TRACE("stateblock %p, state %s (%#x), value %#x.\n", stateblock, debug_d3drenderstate(state), state, value);
1342 if (state > WINEHIGHEST_RENDER_STATE)
1344 WARN("Unhandled render state %#x.\n", state);
1345 return;
1348 stateblock->stateblock_state.rs[state] = value;
1349 stateblock->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
1351 if (state == WINED3D_RS_POINTSIZE
1352 && (value == WINED3D_ALPHA_TO_COVERAGE_ENABLE || value == WINED3D_ALPHA_TO_COVERAGE_DISABLE))
1354 stateblock->changed.alpha_to_coverage = 1;
1355 stateblock->stateblock_state.alpha_to_coverage = (value == WINED3D_ALPHA_TO_COVERAGE_ENABLE);
1359 void CDECL wined3d_stateblock_set_sampler_state(struct wined3d_stateblock *stateblock,
1360 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1362 TRACE("stateblock %p, sampler_idx %u, state %s, value %#x.\n",
1363 stateblock, sampler_idx, debug_d3dsamplerstate(state), value);
1365 if (sampler_idx >= ARRAY_SIZE(stateblock->stateblock_state.sampler_states))
1367 WARN("Invalid sampler %u.\n", sampler_idx);
1368 return;
1371 stateblock->stateblock_state.sampler_states[sampler_idx][state] = value;
1372 stateblock->changed.samplerState[sampler_idx] |= 1u << state;
1375 void CDECL wined3d_stateblock_set_texture_stage_state(struct wined3d_stateblock *stateblock,
1376 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
1378 TRACE("stateblock %p, stage %u, state %s, value %#x.\n",
1379 stateblock, stage, debug_d3dtexturestate(state), value);
1381 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
1383 WARN("Invalid state %#x passed.\n", state);
1384 return;
1387 if (stage >= WINED3D_MAX_TEXTURES)
1389 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
1390 stage, WINED3D_MAX_TEXTURES - 1);
1391 return;
1394 stateblock->stateblock_state.texture_states[stage][state] = value;
1395 stateblock->changed.textureState[stage] |= 1u << state;
1398 void CDECL wined3d_stateblock_set_texture(struct wined3d_stateblock *stateblock,
1399 UINT stage, struct wined3d_texture *texture)
1401 TRACE("stateblock %p, stage %u, texture %p.\n", stateblock, stage, texture);
1403 if (stage >= ARRAY_SIZE(stateblock->stateblock_state.textures))
1405 WARN("Ignoring invalid stage %u.\n", stage);
1406 return;
1409 if (texture)
1410 wined3d_texture_incref(texture);
1411 if (stateblock->stateblock_state.textures[stage])
1412 wined3d_texture_decref(stateblock->stateblock_state.textures[stage]);
1413 stateblock->stateblock_state.textures[stage] = texture;
1414 stateblock->changed.textures |= 1u << stage;
1417 void CDECL wined3d_stateblock_set_transform(struct wined3d_stateblock *stateblock,
1418 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1420 TRACE("stateblock %p, state %s, matrix %p.\n", stateblock, debug_d3dtstype(d3dts), matrix);
1421 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1422 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1423 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1424 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1426 stateblock->stateblock_state.transforms[d3dts] = *matrix;
1427 stateblock->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1428 stateblock->changed.transforms = 1;
1431 void CDECL wined3d_stateblock_multiply_transform(struct wined3d_stateblock *stateblock,
1432 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1434 struct wined3d_matrix *mat = &stateblock->stateblock_state.transforms[d3dts];
1436 TRACE("stateblock %p, state %s, matrix %p.\n", stateblock, debug_d3dtstype(d3dts), matrix);
1437 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1438 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1439 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1440 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1442 multiply_matrix(mat, mat, matrix);
1443 stateblock->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1444 stateblock->changed.transforms = 1;
1447 HRESULT CDECL wined3d_stateblock_set_clip_plane(struct wined3d_stateblock *stateblock,
1448 UINT plane_idx, const struct wined3d_vec4 *plane)
1450 TRACE("stateblock %p, plane_idx %u, plane %p.\n", stateblock, plane_idx, plane);
1452 if (plane_idx >= stateblock->device->adapter->d3d_info.limits.max_clip_distances)
1454 TRACE("Application has requested clipplane this device doesn't support.\n");
1455 return WINED3DERR_INVALIDCALL;
1458 stateblock->stateblock_state.clip_planes[plane_idx] = *plane;
1459 stateblock->changed.clipplane |= 1u << plane_idx;
1460 return S_OK;
1463 void CDECL wined3d_stateblock_set_material(struct wined3d_stateblock *stateblock,
1464 const struct wined3d_material *material)
1466 TRACE("stateblock %p, material %p.\n", stateblock, material);
1468 stateblock->stateblock_state.material = *material;
1469 stateblock->changed.material = TRUE;
1472 void CDECL wined3d_stateblock_set_viewport(struct wined3d_stateblock *stateblock,
1473 const struct wined3d_viewport *viewport)
1475 TRACE("stateblock %p, viewport %p.\n", stateblock, viewport);
1477 stateblock->stateblock_state.viewport = *viewport;
1478 stateblock->changed.viewport = TRUE;
1481 void CDECL wined3d_stateblock_set_scissor_rect(struct wined3d_stateblock *stateblock, const RECT *rect)
1483 TRACE("stateblock %p, rect %s.\n", stateblock, wine_dbgstr_rect(rect));
1485 stateblock->stateblock_state.scissor_rect = *rect;
1486 stateblock->changed.scissorRect = TRUE;
1489 void CDECL wined3d_stateblock_set_index_buffer(struct wined3d_stateblock *stateblock,
1490 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1492 TRACE("stateblock %p, buffer %p, format %s.\n", stateblock, buffer, debug_d3dformat(format_id));
1494 if (buffer)
1495 wined3d_buffer_incref(buffer);
1496 if (stateblock->stateblock_state.index_buffer)
1497 wined3d_buffer_decref(stateblock->stateblock_state.index_buffer);
1498 stateblock->stateblock_state.index_buffer = buffer;
1499 stateblock->stateblock_state.index_format = format_id;
1500 stateblock->changed.indices = TRUE;
1503 void CDECL wined3d_stateblock_set_base_vertex_index(struct wined3d_stateblock *stateblock, INT base_index)
1505 TRACE("stateblock %p, base_index %d.\n", stateblock, base_index);
1507 stateblock->stateblock_state.base_vertex_index = base_index;
1510 HRESULT CDECL wined3d_stateblock_set_stream_source(struct wined3d_stateblock *stateblock,
1511 UINT stream_idx, struct wined3d_buffer *buffer, UINT offset, UINT stride)
1513 struct wined3d_stream_state *stream;
1515 TRACE("stateblock %p, stream_idx %u, buffer %p, stride %u.\n",
1516 stateblock, stream_idx, buffer, stride);
1518 if (stream_idx >= WINED3D_MAX_STREAMS)
1520 WARN("Stream index %u out of range.\n", stream_idx);
1521 return WINED3DERR_INVALIDCALL;
1524 stream = &stateblock->stateblock_state.streams[stream_idx];
1526 if (buffer)
1527 wined3d_buffer_incref(buffer);
1528 if (stream->buffer)
1529 wined3d_buffer_decref(stream->buffer);
1530 stream->buffer = buffer;
1531 stream->stride = stride;
1532 stream->offset = offset;
1533 stateblock->changed.streamSource |= 1u << stream_idx;
1534 return WINED3D_OK;
1537 HRESULT CDECL wined3d_stateblock_set_stream_source_freq(struct wined3d_stateblock *stateblock,
1538 UINT stream_idx, UINT divider)
1540 struct wined3d_stream_state *stream;
1542 TRACE("stateblock %p, stream_idx %u, divider %#x.\n", stateblock, stream_idx, divider);
1544 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1546 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1547 return WINED3DERR_INVALIDCALL;
1549 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1551 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1552 return WINED3DERR_INVALIDCALL;
1554 if (!divider)
1556 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1557 return WINED3DERR_INVALIDCALL;
1560 stream = &stateblock->stateblock_state.streams[stream_idx];
1561 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1562 stream->frequency = divider & 0x7fffff;
1563 stateblock->changed.streamFreq |= 1u << stream_idx;
1564 return WINED3D_OK;
1567 HRESULT CDECL wined3d_stateblock_set_light(struct wined3d_stateblock *stateblock,
1568 UINT light_idx, const struct wined3d_light *light)
1570 struct wined3d_light_info *object = NULL;
1572 TRACE("stateblock %p, light_idx %u, light %p.\n", stateblock, light_idx, light);
1574 /* Check the parameter range. Need for speed most wanted sets junk lights
1575 * which confuse the GL driver. */
1576 if (!light)
1577 return WINED3DERR_INVALIDCALL;
1579 switch (light->type)
1581 case WINED3D_LIGHT_POINT:
1582 case WINED3D_LIGHT_SPOT:
1583 case WINED3D_LIGHT_GLSPOT:
1584 /* Incorrect attenuation values can cause the gl driver to crash.
1585 * Happens with Need for speed most wanted. */
1586 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1588 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1589 return WINED3DERR_INVALIDCALL;
1591 break;
1593 case WINED3D_LIGHT_DIRECTIONAL:
1594 case WINED3D_LIGHT_PARALLELPOINT:
1595 /* Ignores attenuation */
1596 break;
1598 default:
1599 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL.\n");
1600 return WINED3DERR_INVALIDCALL;
1603 stateblock->changed.lights = 1;
1604 return wined3d_light_state_set_light(stateblock->stateblock_state.light_state, light_idx, light, &object);
1607 HRESULT CDECL wined3d_stateblock_set_light_enable(struct wined3d_stateblock *stateblock, UINT light_idx, BOOL enable)
1609 struct wined3d_light_state *light_state = stateblock->stateblock_state.light_state;
1610 struct wined3d_light_info *light_info;
1611 HRESULT hr;
1613 TRACE("stateblock %p, light_idx %u, enable %#x.\n", stateblock, light_idx, enable);
1615 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1617 if (FAILED(hr = wined3d_light_state_set_light(light_state, light_idx, &WINED3D_default_light, &light_info)))
1618 return hr;
1620 wined3d_light_state_enable_light(light_state, &stateblock->device->adapter->d3d_info, light_info, enable);
1621 stateblock->changed.lights = 1;
1622 return S_OK;
1625 const struct wined3d_stateblock_state * CDECL wined3d_stateblock_get_state(const struct wined3d_stateblock *stateblock)
1627 return &stateblock->stateblock_state;
1630 HRESULT CDECL wined3d_stateblock_get_light(const struct wined3d_stateblock *stateblock,
1631 UINT light_idx, struct wined3d_light *light, BOOL *enabled)
1633 struct wined3d_light_info *light_info;
1635 if (!(light_info = wined3d_light_state_get_light(&stateblock->light_state, light_idx)))
1637 TRACE("Light %u is not defined.\n", light_idx);
1638 return WINED3DERR_INVALIDCALL;
1640 *light = light_info->OriginalParms;
1641 *enabled = light_info->enabled ? 128 : 0;
1642 return WINED3D_OK;
1645 static void init_default_render_states(DWORD rs[WINEHIGHEST_RENDER_STATE + 1], const struct wined3d_d3d_info *d3d_info)
1647 union
1649 struct wined3d_line_pattern lp;
1650 DWORD d;
1651 } lp;
1652 union
1654 float f;
1655 DWORD d;
1656 } tmpfloat;
1658 rs[WINED3D_RS_ZENABLE] = WINED3D_ZB_TRUE;
1659 rs[WINED3D_RS_FILLMODE] = WINED3D_FILL_SOLID;
1660 rs[WINED3D_RS_SHADEMODE] = WINED3D_SHADE_GOURAUD;
1661 lp.lp.repeat_factor = 0;
1662 lp.lp.line_pattern = 0;
1663 rs[WINED3D_RS_LINEPATTERN] = lp.d;
1664 rs[WINED3D_RS_ZWRITEENABLE] = TRUE;
1665 rs[WINED3D_RS_ALPHATESTENABLE] = FALSE;
1666 rs[WINED3D_RS_LASTPIXEL] = TRUE;
1667 rs[WINED3D_RS_SRCBLEND] = WINED3D_BLEND_ONE;
1668 rs[WINED3D_RS_DESTBLEND] = WINED3D_BLEND_ZERO;
1669 rs[WINED3D_RS_CULLMODE] = WINED3D_CULL_BACK;
1670 rs[WINED3D_RS_ZFUNC] = WINED3D_CMP_LESSEQUAL;
1671 rs[WINED3D_RS_ALPHAFUNC] = WINED3D_CMP_ALWAYS;
1672 rs[WINED3D_RS_ALPHAREF] = 0;
1673 rs[WINED3D_RS_DITHERENABLE] = FALSE;
1674 rs[WINED3D_RS_ALPHABLENDENABLE] = FALSE;
1675 rs[WINED3D_RS_FOGENABLE] = FALSE;
1676 rs[WINED3D_RS_SPECULARENABLE] = FALSE;
1677 rs[WINED3D_RS_ZVISIBLE] = 0;
1678 rs[WINED3D_RS_FOGCOLOR] = 0;
1679 rs[WINED3D_RS_FOGTABLEMODE] = WINED3D_FOG_NONE;
1680 tmpfloat.f = 0.0f;
1681 rs[WINED3D_RS_FOGSTART] = tmpfloat.d;
1682 tmpfloat.f = 1.0f;
1683 rs[WINED3D_RS_FOGEND] = tmpfloat.d;
1684 tmpfloat.f = 1.0f;
1685 rs[WINED3D_RS_FOGDENSITY] = tmpfloat.d;
1686 rs[WINED3D_RS_RANGEFOGENABLE] = FALSE;
1687 rs[WINED3D_RS_STENCILENABLE] = FALSE;
1688 rs[WINED3D_RS_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1689 rs[WINED3D_RS_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1690 rs[WINED3D_RS_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1691 rs[WINED3D_RS_STENCILREF] = 0;
1692 rs[WINED3D_RS_STENCILMASK] = 0xffffffff;
1693 rs[WINED3D_RS_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1694 rs[WINED3D_RS_STENCILWRITEMASK] = 0xffffffff;
1695 rs[WINED3D_RS_TEXTUREFACTOR] = 0xffffffff;
1696 rs[WINED3D_RS_WRAP0] = 0;
1697 rs[WINED3D_RS_WRAP1] = 0;
1698 rs[WINED3D_RS_WRAP2] = 0;
1699 rs[WINED3D_RS_WRAP3] = 0;
1700 rs[WINED3D_RS_WRAP4] = 0;
1701 rs[WINED3D_RS_WRAP5] = 0;
1702 rs[WINED3D_RS_WRAP6] = 0;
1703 rs[WINED3D_RS_WRAP7] = 0;
1704 rs[WINED3D_RS_CLIPPING] = TRUE;
1705 rs[WINED3D_RS_LIGHTING] = TRUE;
1706 rs[WINED3D_RS_AMBIENT] = 0;
1707 rs[WINED3D_RS_FOGVERTEXMODE] = WINED3D_FOG_NONE;
1708 rs[WINED3D_RS_COLORVERTEX] = TRUE;
1709 rs[WINED3D_RS_LOCALVIEWER] = TRUE;
1710 rs[WINED3D_RS_NORMALIZENORMALS] = FALSE;
1711 rs[WINED3D_RS_DIFFUSEMATERIALSOURCE] = WINED3D_MCS_COLOR1;
1712 rs[WINED3D_RS_SPECULARMATERIALSOURCE] = WINED3D_MCS_COLOR2;
1713 rs[WINED3D_RS_AMBIENTMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1714 rs[WINED3D_RS_EMISSIVEMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1715 rs[WINED3D_RS_VERTEXBLEND] = WINED3D_VBF_DISABLE;
1716 rs[WINED3D_RS_CLIPPLANEENABLE] = 0;
1717 rs[WINED3D_RS_SOFTWAREVERTEXPROCESSING] = FALSE;
1718 tmpfloat.f = 1.0f;
1719 rs[WINED3D_RS_POINTSIZE] = tmpfloat.d;
1720 tmpfloat.f = 1.0f;
1721 rs[WINED3D_RS_POINTSIZE_MIN] = tmpfloat.d;
1722 rs[WINED3D_RS_POINTSPRITEENABLE] = FALSE;
1723 rs[WINED3D_RS_POINTSCALEENABLE] = FALSE;
1724 tmpfloat.f = 1.0f;
1725 rs[WINED3D_RS_POINTSCALE_A] = tmpfloat.d;
1726 tmpfloat.f = 0.0f;
1727 rs[WINED3D_RS_POINTSCALE_B] = tmpfloat.d;
1728 tmpfloat.f = 0.0f;
1729 rs[WINED3D_RS_POINTSCALE_C] = tmpfloat.d;
1730 rs[WINED3D_RS_MULTISAMPLEANTIALIAS] = TRUE;
1731 rs[WINED3D_RS_MULTISAMPLEMASK] = 0xffffffff;
1732 rs[WINED3D_RS_PATCHEDGESTYLE] = WINED3D_PATCH_EDGE_DISCRETE;
1733 tmpfloat.f = 1.0f;
1734 rs[WINED3D_RS_PATCHSEGMENTS] = tmpfloat.d;
1735 rs[WINED3D_RS_DEBUGMONITORTOKEN] = 0xbaadcafe;
1736 tmpfloat.f = d3d_info->limits.pointsize_max;
1737 rs[WINED3D_RS_POINTSIZE_MAX] = tmpfloat.d;
1738 rs[WINED3D_RS_INDEXEDVERTEXBLENDENABLE] = FALSE;
1739 rs[WINED3D_RS_COLORWRITEENABLE] = 0x0000000f;
1740 tmpfloat.f = 0.0f;
1741 rs[WINED3D_RS_TWEENFACTOR] = tmpfloat.d;
1742 rs[WINED3D_RS_BLENDOP] = WINED3D_BLEND_OP_ADD;
1743 rs[WINED3D_RS_POSITIONDEGREE] = WINED3D_DEGREE_CUBIC;
1744 rs[WINED3D_RS_NORMALDEGREE] = WINED3D_DEGREE_LINEAR;
1745 /* states new in d3d9 */
1746 rs[WINED3D_RS_SCISSORTESTENABLE] = FALSE;
1747 rs[WINED3D_RS_SLOPESCALEDEPTHBIAS] = 0;
1748 tmpfloat.f = 1.0f;
1749 rs[WINED3D_RS_MINTESSELLATIONLEVEL] = tmpfloat.d;
1750 rs[WINED3D_RS_MAXTESSELLATIONLEVEL] = tmpfloat.d;
1751 rs[WINED3D_RS_ANTIALIASEDLINEENABLE] = FALSE;
1752 tmpfloat.f = 0.0f;
1753 rs[WINED3D_RS_ADAPTIVETESS_X] = tmpfloat.d;
1754 rs[WINED3D_RS_ADAPTIVETESS_Y] = tmpfloat.d;
1755 tmpfloat.f = 1.0f;
1756 rs[WINED3D_RS_ADAPTIVETESS_Z] = tmpfloat.d;
1757 tmpfloat.f = 0.0f;
1758 rs[WINED3D_RS_ADAPTIVETESS_W] = tmpfloat.d;
1759 rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION] = FALSE;
1760 rs[WINED3D_RS_TWOSIDEDSTENCILMODE] = FALSE;
1761 rs[WINED3D_RS_BACK_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1762 rs[WINED3D_RS_BACK_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1763 rs[WINED3D_RS_BACK_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1764 rs[WINED3D_RS_BACK_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1765 rs[WINED3D_RS_COLORWRITEENABLE1] = 0x0000000f;
1766 rs[WINED3D_RS_COLORWRITEENABLE2] = 0x0000000f;
1767 rs[WINED3D_RS_COLORWRITEENABLE3] = 0x0000000f;
1768 rs[WINED3D_RS_BLENDFACTOR] = 0xffffffff;
1769 rs[WINED3D_RS_SRGBWRITEENABLE] = 0;
1770 rs[WINED3D_RS_DEPTHBIAS] = 0;
1771 rs[WINED3D_RS_WRAP8] = 0;
1772 rs[WINED3D_RS_WRAP9] = 0;
1773 rs[WINED3D_RS_WRAP10] = 0;
1774 rs[WINED3D_RS_WRAP11] = 0;
1775 rs[WINED3D_RS_WRAP12] = 0;
1776 rs[WINED3D_RS_WRAP13] = 0;
1777 rs[WINED3D_RS_WRAP14] = 0;
1778 rs[WINED3D_RS_WRAP15] = 0;
1779 rs[WINED3D_RS_SEPARATEALPHABLENDENABLE] = FALSE;
1780 rs[WINED3D_RS_SRCBLENDALPHA] = WINED3D_BLEND_ONE;
1781 rs[WINED3D_RS_DESTBLENDALPHA] = WINED3D_BLEND_ZERO;
1782 rs[WINED3D_RS_BLENDOPALPHA] = WINED3D_BLEND_OP_ADD;
1785 static void init_default_texture_state(unsigned int i, DWORD stage[WINED3D_HIGHEST_TEXTURE_STATE + 1])
1787 stage[WINED3D_TSS_COLOR_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_MODULATE;
1788 stage[WINED3D_TSS_COLOR_ARG1] = WINED3DTA_TEXTURE;
1789 stage[WINED3D_TSS_COLOR_ARG2] = WINED3DTA_CURRENT;
1790 stage[WINED3D_TSS_ALPHA_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_SELECT_ARG1;
1791 stage[WINED3D_TSS_ALPHA_ARG1] = WINED3DTA_TEXTURE;
1792 stage[WINED3D_TSS_ALPHA_ARG2] = WINED3DTA_CURRENT;
1793 stage[WINED3D_TSS_BUMPENV_MAT00] = 0;
1794 stage[WINED3D_TSS_BUMPENV_MAT01] = 0;
1795 stage[WINED3D_TSS_BUMPENV_MAT10] = 0;
1796 stage[WINED3D_TSS_BUMPENV_MAT11] = 0;
1797 stage[WINED3D_TSS_TEXCOORD_INDEX] = i;
1798 stage[WINED3D_TSS_BUMPENV_LSCALE] = 0;
1799 stage[WINED3D_TSS_BUMPENV_LOFFSET] = 0;
1800 stage[WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS] = WINED3D_TTFF_DISABLE;
1801 stage[WINED3D_TSS_COLOR_ARG0] = WINED3DTA_CURRENT;
1802 stage[WINED3D_TSS_ALPHA_ARG0] = WINED3DTA_CURRENT;
1803 stage[WINED3D_TSS_RESULT_ARG] = WINED3DTA_CURRENT;
1806 static void init_default_sampler_states(DWORD states[WINED3D_MAX_COMBINED_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1])
1808 unsigned int i;
1810 for (i = 0 ; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1812 TRACE("Setting up default samplers states for sampler %u.\n", i);
1813 states[i][WINED3D_SAMP_ADDRESS_U] = WINED3D_TADDRESS_WRAP;
1814 states[i][WINED3D_SAMP_ADDRESS_V] = WINED3D_TADDRESS_WRAP;
1815 states[i][WINED3D_SAMP_ADDRESS_W] = WINED3D_TADDRESS_WRAP;
1816 states[i][WINED3D_SAMP_BORDER_COLOR] = 0;
1817 states[i][WINED3D_SAMP_MAG_FILTER] = WINED3D_TEXF_POINT;
1818 states[i][WINED3D_SAMP_MIN_FILTER] = WINED3D_TEXF_POINT;
1819 states[i][WINED3D_SAMP_MIP_FILTER] = WINED3D_TEXF_NONE;
1820 states[i][WINED3D_SAMP_MIPMAP_LOD_BIAS] = 0;
1821 states[i][WINED3D_SAMP_MAX_MIP_LEVEL] = 0;
1822 states[i][WINED3D_SAMP_MAX_ANISOTROPY] = 1;
1823 states[i][WINED3D_SAMP_SRGB_TEXTURE] = 0;
1824 /* TODO: Indicates which element of a multielement texture to use. */
1825 states[i][WINED3D_SAMP_ELEMENT_INDEX] = 0;
1826 /* TODO: Vertex offset in the presampled displacement map. */
1827 states[i][WINED3D_SAMP_DMAP_OFFSET] = 0;
1831 static void state_init_default(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info)
1833 struct wined3d_matrix identity;
1834 unsigned int i, j;
1836 TRACE("state %p, d3d_info %p.\n", state, d3d_info);
1838 get_identity_matrix(&identity);
1839 state->primitive_type = WINED3D_PT_UNDEFINED;
1840 state->patch_vertex_count = 0;
1842 /* Set some of the defaults for lights, transforms etc */
1843 state->transforms[WINED3D_TS_PROJECTION] = identity;
1844 state->transforms[WINED3D_TS_VIEW] = identity;
1845 for (i = 0; i < 256; ++i)
1847 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
1850 init_default_render_states(state->render_states, d3d_info);
1852 /* Texture Stage States - Put directly into state block, we will call function below */
1853 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1855 TRACE("Setting up default texture states for texture Stage %u.\n", i);
1856 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
1857 init_default_texture_state(i, state->texture_states[i]);
1860 init_default_sampler_states(state->sampler_states);
1862 state->blend_factor.r = 1.0f;
1863 state->blend_factor.g = 1.0f;
1864 state->blend_factor.b = 1.0f;
1865 state->blend_factor.a = 1.0f;
1867 state->sample_mask = 0xffffffff;
1869 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1870 state->streams[i].frequency = 1;
1872 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1874 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
1875 state->cb[i][j].size = WINED3D_MAX_CONSTANT_BUFFER_SIZE * 16;
1879 void state_init(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info,
1880 uint32_t flags, enum wined3d_feature_level feature_level)
1882 unsigned int i;
1884 state->feature_level = feature_level;
1885 state->flags = flags;
1887 for (i = 0; i < LIGHTMAP_SIZE; i++)
1889 list_init(&state->light_state.light_map[i]);
1892 if (flags & WINED3D_STATE_INIT_DEFAULT)
1893 state_init_default(state, d3d_info);
1896 static bool wined3d_select_feature_level(const struct wined3d_adapter *adapter,
1897 const enum wined3d_feature_level *levels, unsigned int level_count,
1898 enum wined3d_feature_level *selected_level)
1900 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
1901 unsigned int i;
1903 for (i = 0; i < level_count; ++i)
1905 if (levels[i] && d3d_info->feature_level >= levels[i])
1907 *selected_level = levels[i];
1908 return true;
1912 FIXME_(winediag)("None of the requested D3D feature levels is supported on this GPU "
1913 "with the current shader backend.\n");
1914 return false;
1917 HRESULT CDECL wined3d_state_create(struct wined3d_device *device,
1918 const enum wined3d_feature_level *levels, unsigned int level_count, struct wined3d_state **state)
1920 enum wined3d_feature_level feature_level;
1921 struct wined3d_state *object;
1923 TRACE("device %p, levels %p, level_count %u, state %p.\n", device, levels, level_count, state);
1925 if (!wined3d_select_feature_level(device->adapter, levels, level_count, &feature_level))
1926 return E_FAIL;
1928 TRACE("Selected feature level %s.\n", wined3d_debug_feature_level(feature_level));
1930 if (!(object = heap_alloc_zero(sizeof(*object))))
1931 return E_OUTOFMEMORY;
1932 state_init(object, &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT, feature_level);
1934 *state = object;
1935 return S_OK;
1938 enum wined3d_feature_level CDECL wined3d_state_get_feature_level(const struct wined3d_state *state)
1940 TRACE("state %p.\n", state);
1942 return state->feature_level;
1945 void CDECL wined3d_state_destroy(struct wined3d_state *state)
1947 TRACE("state %p.\n", state);
1949 state_cleanup(state);
1950 heap_free(state);
1953 static void stateblock_state_init_default(struct wined3d_stateblock_state *state,
1954 const struct wined3d_d3d_info *d3d_info)
1956 struct wined3d_matrix identity;
1957 unsigned int i;
1959 get_identity_matrix(&identity);
1961 state->transforms[WINED3D_TS_PROJECTION] = identity;
1962 state->transforms[WINED3D_TS_VIEW] = identity;
1963 for (i = 0; i < 256; ++i)
1965 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
1968 init_default_render_states(state->rs, d3d_info);
1970 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1972 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
1973 init_default_texture_state(i, state->texture_states[i]);
1976 init_default_sampler_states(state->sampler_states);
1978 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1979 state->streams[i].frequency = 1;
1982 void wined3d_stateblock_state_init(struct wined3d_stateblock_state *state,
1983 const struct wined3d_device *device, DWORD flags)
1985 unsigned int i;
1987 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); i++)
1989 list_init(&state->light_state->light_map[i]);
1992 if (flags & WINED3D_STATE_INIT_DEFAULT)
1993 stateblock_state_init_default(state, &device->adapter->d3d_info);
1997 static HRESULT stateblock_init(struct wined3d_stateblock *stateblock, const struct wined3d_stateblock *device_state,
1998 struct wined3d_device *device, enum wined3d_stateblock_type type)
2000 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2002 stateblock->ref = 1;
2003 stateblock->device = device;
2004 stateblock->stateblock_state.light_state = &stateblock->light_state;
2005 wined3d_stateblock_state_init(&stateblock->stateblock_state, device,
2006 type == WINED3D_SBT_PRIMARY ? WINED3D_STATE_INIT_DEFAULT : 0);
2008 stateblock->changed.store_stream_offset = 1;
2010 if (type == WINED3D_SBT_RECORDED || type == WINED3D_SBT_PRIMARY)
2011 return WINED3D_OK;
2013 TRACE("Updating changed flags appropriate for type %#x.\n", type);
2015 switch (type)
2017 case WINED3D_SBT_ALL:
2018 stateblock_init_lights(stateblock->stateblock_state.light_state->light_map,
2019 device_state->stateblock_state.light_state->light_map);
2020 stateblock_savedstates_set_all(&stateblock->changed,
2021 d3d_info->limits.vs_uniform_count, d3d_info->limits.ps_uniform_count);
2022 break;
2024 case WINED3D_SBT_PIXEL_STATE:
2025 stateblock_savedstates_set_pixel(&stateblock->changed,
2026 d3d_info->limits.ps_uniform_count);
2027 break;
2029 case WINED3D_SBT_VERTEX_STATE:
2030 stateblock_init_lights(stateblock->stateblock_state.light_state->light_map,
2031 device_state->stateblock_state.light_state->light_map);
2032 stateblock_savedstates_set_vertex(&stateblock->changed,
2033 d3d_info->limits.vs_uniform_count);
2034 break;
2036 default:
2037 FIXME("Unrecognized state block type %#x.\n", type);
2038 break;
2041 wined3d_stateblock_init_contained_states(stateblock);
2042 wined3d_stateblock_capture(stateblock, device_state);
2044 /* According to the tests, stream offset is not updated in the captured state if
2045 * the state was captured on state block creation. This is not the case for
2046 * state blocks initialized with BeginStateBlock / EndStateBlock, multiple
2047 * captures get stream offsets updated. */
2048 stateblock->changed.store_stream_offset = 0;
2050 return WINED3D_OK;
2053 HRESULT CDECL wined3d_stateblock_create(struct wined3d_device *device, const struct wined3d_stateblock *device_state,
2054 enum wined3d_stateblock_type type, struct wined3d_stateblock **stateblock)
2056 struct wined3d_stateblock *object;
2057 HRESULT hr;
2059 TRACE("device %p, device_state %p, type %#x, stateblock %p.\n",
2060 device, device_state, type, stateblock);
2062 if (!(object = heap_alloc_zero(sizeof(*object))))
2063 return E_OUTOFMEMORY;
2065 hr = stateblock_init(object, device_state, device, type);
2066 if (FAILED(hr))
2068 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2069 heap_free(object);
2070 return hr;
2073 TRACE("Created stateblock %p.\n", object);
2074 *stateblock = object;
2076 return WINED3D_OK;
2079 void CDECL wined3d_stateblock_reset(struct wined3d_stateblock *stateblock)
2081 TRACE("stateblock %p.\n", stateblock);
2083 wined3d_stateblock_state_cleanup(&stateblock->stateblock_state);
2084 memset(&stateblock->stateblock_state, 0, sizeof(stateblock->stateblock_state));
2085 stateblock->stateblock_state.light_state = &stateblock->light_state;
2086 wined3d_stateblock_state_init(&stateblock->stateblock_state, stateblock->device, WINED3D_STATE_INIT_DEFAULT);