gdi32: Use get_object_type for GetObjectType implementation.
[wine.git] / dlls / wined3d / stateblock.c
blob1f1ba262336ede2f6bfcfa2e4c2e5fe32476a647
1 /*
2 * state block implementation
4 * Copyright 2002 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2007 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30 WINE_DECLARE_DEBUG_CHANNEL(winediag);
32 static const DWORD pixel_states_render[] =
34 WINED3D_RS_ALPHABLENDENABLE,
35 WINED3D_RS_ALPHAFUNC,
36 WINED3D_RS_ALPHAREF,
37 WINED3D_RS_ALPHATESTENABLE,
38 WINED3D_RS_ANTIALIASEDLINEENABLE,
39 WINED3D_RS_BLENDFACTOR,
40 WINED3D_RS_BLENDOP,
41 WINED3D_RS_BLENDOPALPHA,
42 WINED3D_RS_BACK_STENCILFAIL,
43 WINED3D_RS_BACK_STENCILPASS,
44 WINED3D_RS_BACK_STENCILZFAIL,
45 WINED3D_RS_COLORWRITEENABLE,
46 WINED3D_RS_COLORWRITEENABLE1,
47 WINED3D_RS_COLORWRITEENABLE2,
48 WINED3D_RS_COLORWRITEENABLE3,
49 WINED3D_RS_DEPTHBIAS,
50 WINED3D_RS_DESTBLEND,
51 WINED3D_RS_DESTBLENDALPHA,
52 WINED3D_RS_DITHERENABLE,
53 WINED3D_RS_FILLMODE,
54 WINED3D_RS_FOGDENSITY,
55 WINED3D_RS_FOGEND,
56 WINED3D_RS_FOGSTART,
57 WINED3D_RS_LASTPIXEL,
58 WINED3D_RS_SCISSORTESTENABLE,
59 WINED3D_RS_SEPARATEALPHABLENDENABLE,
60 WINED3D_RS_SHADEMODE,
61 WINED3D_RS_SLOPESCALEDEPTHBIAS,
62 WINED3D_RS_SRCBLEND,
63 WINED3D_RS_SRCBLENDALPHA,
64 WINED3D_RS_SRGBWRITEENABLE,
65 WINED3D_RS_STENCILENABLE,
66 WINED3D_RS_STENCILFAIL,
67 WINED3D_RS_STENCILFUNC,
68 WINED3D_RS_STENCILMASK,
69 WINED3D_RS_STENCILPASS,
70 WINED3D_RS_STENCILREF,
71 WINED3D_RS_STENCILWRITEMASK,
72 WINED3D_RS_STENCILZFAIL,
73 WINED3D_RS_TEXTUREFACTOR,
74 WINED3D_RS_TWOSIDEDSTENCILMODE,
75 WINED3D_RS_WRAP0,
76 WINED3D_RS_WRAP1,
77 WINED3D_RS_WRAP10,
78 WINED3D_RS_WRAP11,
79 WINED3D_RS_WRAP12,
80 WINED3D_RS_WRAP13,
81 WINED3D_RS_WRAP14,
82 WINED3D_RS_WRAP15,
83 WINED3D_RS_WRAP2,
84 WINED3D_RS_WRAP3,
85 WINED3D_RS_WRAP4,
86 WINED3D_RS_WRAP5,
87 WINED3D_RS_WRAP6,
88 WINED3D_RS_WRAP7,
89 WINED3D_RS_WRAP8,
90 WINED3D_RS_WRAP9,
91 WINED3D_RS_ZENABLE,
92 WINED3D_RS_ZFUNC,
93 WINED3D_RS_ZWRITEENABLE,
96 static const DWORD pixel_states_texture[] =
98 WINED3D_TSS_ALPHA_ARG0,
99 WINED3D_TSS_ALPHA_ARG1,
100 WINED3D_TSS_ALPHA_ARG2,
101 WINED3D_TSS_ALPHA_OP,
102 WINED3D_TSS_BUMPENV_LOFFSET,
103 WINED3D_TSS_BUMPENV_LSCALE,
104 WINED3D_TSS_BUMPENV_MAT00,
105 WINED3D_TSS_BUMPENV_MAT01,
106 WINED3D_TSS_BUMPENV_MAT10,
107 WINED3D_TSS_BUMPENV_MAT11,
108 WINED3D_TSS_COLOR_ARG0,
109 WINED3D_TSS_COLOR_ARG1,
110 WINED3D_TSS_COLOR_ARG2,
111 WINED3D_TSS_COLOR_OP,
112 WINED3D_TSS_RESULT_ARG,
113 WINED3D_TSS_TEXCOORD_INDEX,
114 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,
117 static const DWORD pixel_states_sampler[] =
119 WINED3D_SAMP_ADDRESS_U,
120 WINED3D_SAMP_ADDRESS_V,
121 WINED3D_SAMP_ADDRESS_W,
122 WINED3D_SAMP_BORDER_COLOR,
123 WINED3D_SAMP_MAG_FILTER,
124 WINED3D_SAMP_MIN_FILTER,
125 WINED3D_SAMP_MIP_FILTER,
126 WINED3D_SAMP_MIPMAP_LOD_BIAS,
127 WINED3D_SAMP_MAX_MIP_LEVEL,
128 WINED3D_SAMP_MAX_ANISOTROPY,
129 WINED3D_SAMP_SRGB_TEXTURE,
130 WINED3D_SAMP_ELEMENT_INDEX,
133 static const DWORD vertex_states_render[] =
135 WINED3D_RS_ADAPTIVETESS_W,
136 WINED3D_RS_ADAPTIVETESS_X,
137 WINED3D_RS_ADAPTIVETESS_Y,
138 WINED3D_RS_ADAPTIVETESS_Z,
139 WINED3D_RS_AMBIENT,
140 WINED3D_RS_AMBIENTMATERIALSOURCE,
141 WINED3D_RS_CLIPPING,
142 WINED3D_RS_CLIPPLANEENABLE,
143 WINED3D_RS_COLORVERTEX,
144 WINED3D_RS_CULLMODE,
145 WINED3D_RS_DIFFUSEMATERIALSOURCE,
146 WINED3D_RS_EMISSIVEMATERIALSOURCE,
147 WINED3D_RS_ENABLEADAPTIVETESSELLATION,
148 WINED3D_RS_FOGCOLOR,
149 WINED3D_RS_FOGDENSITY,
150 WINED3D_RS_FOGENABLE,
151 WINED3D_RS_FOGEND,
152 WINED3D_RS_FOGSTART,
153 WINED3D_RS_FOGTABLEMODE,
154 WINED3D_RS_FOGVERTEXMODE,
155 WINED3D_RS_INDEXEDVERTEXBLENDENABLE,
156 WINED3D_RS_LIGHTING,
157 WINED3D_RS_LOCALVIEWER,
158 WINED3D_RS_MAXTESSELLATIONLEVEL,
159 WINED3D_RS_MINTESSELLATIONLEVEL,
160 WINED3D_RS_MULTISAMPLEANTIALIAS,
161 WINED3D_RS_MULTISAMPLEMASK,
162 WINED3D_RS_NORMALDEGREE,
163 WINED3D_RS_NORMALIZENORMALS,
164 WINED3D_RS_PATCHEDGESTYLE,
165 WINED3D_RS_POINTSCALE_A,
166 WINED3D_RS_POINTSCALE_B,
167 WINED3D_RS_POINTSCALE_C,
168 WINED3D_RS_POINTSCALEENABLE,
169 WINED3D_RS_POINTSIZE,
170 WINED3D_RS_POINTSIZE_MAX,
171 WINED3D_RS_POINTSIZE_MIN,
172 WINED3D_RS_POINTSPRITEENABLE,
173 WINED3D_RS_POSITIONDEGREE,
174 WINED3D_RS_RANGEFOGENABLE,
175 WINED3D_RS_SHADEMODE,
176 WINED3D_RS_SPECULARENABLE,
177 WINED3D_RS_SPECULARMATERIALSOURCE,
178 WINED3D_RS_TWEENFACTOR,
179 WINED3D_RS_VERTEXBLEND,
182 static const DWORD vertex_states_texture[] =
184 WINED3D_TSS_TEXCOORD_INDEX,
185 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,
188 static const DWORD vertex_states_sampler[] =
190 WINED3D_SAMP_DMAP_OFFSET,
193 static inline void stateblock_set_all_bits(DWORD *map, UINT map_size)
195 DWORD mask = (1u << (map_size & 0x1f)) - 1;
196 memset(map, 0xff, (map_size >> 5) * sizeof(*map));
197 if (mask) map[map_size >> 5] = mask;
200 /* Set all members of a stateblock savedstate to the given value */
201 static void stateblock_savedstates_set_all(struct wined3d_saved_states *states, DWORD vs_consts, DWORD ps_consts)
203 unsigned int i;
205 states->indices = 1;
206 states->material = 1;
207 states->viewport = 1;
208 states->vertexDecl = 1;
209 states->pixelShader = 1;
210 states->vertexShader = 1;
211 states->scissorRect = 1;
212 states->alpha_to_coverage = 1;
213 states->lights = 1;
214 states->transforms = 1;
216 states->streamSource = 0xffff;
217 states->streamFreq = 0xffff;
218 states->textures = 0xfffff;
219 stateblock_set_all_bits(states->transform, WINED3D_HIGHEST_TRANSFORM_STATE + 1);
220 stateblock_set_all_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
221 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
222 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3ffe;
223 states->clipplane = (1u << WINED3D_MAX_CLIP_DISTANCES) - 1;
224 states->pixelShaderConstantsB = 0xffff;
225 states->pixelShaderConstantsI = 0xffff;
226 states->vertexShaderConstantsB = 0xffff;
227 states->vertexShaderConstantsI = 0xffff;
229 memset(states->ps_consts_f, 0xffu, sizeof(states->ps_consts_f));
230 memset(states->vs_consts_f, 0xffu, sizeof(states->vs_consts_f));
233 static void stateblock_savedstates_set_pixel(struct wined3d_saved_states *states, const DWORD num_constants)
235 DWORD texture_mask = 0;
236 WORD sampler_mask = 0;
237 unsigned int i;
239 states->pixelShader = 1;
241 for (i = 0; i < ARRAY_SIZE(pixel_states_render); ++i)
243 DWORD rs = pixel_states_render[i];
244 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
247 for (i = 0; i < ARRAY_SIZE(pixel_states_texture); ++i)
248 texture_mask |= 1u << pixel_states_texture[i];
249 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
250 for (i = 0; i < ARRAY_SIZE(pixel_states_sampler); ++i)
251 sampler_mask |= 1u << pixel_states_sampler[i];
252 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
253 states->pixelShaderConstantsB = 0xffff;
254 states->pixelShaderConstantsI = 0xffff;
256 memset(states->ps_consts_f, 0xffu, sizeof(states->ps_consts_f));
259 static void stateblock_savedstates_set_vertex(struct wined3d_saved_states *states, const DWORD num_constants)
261 DWORD texture_mask = 0;
262 WORD sampler_mask = 0;
263 unsigned int i;
265 states->vertexDecl = 1;
266 states->vertexShader = 1;
267 states->alpha_to_coverage = 1;
268 states->lights = 1;
270 for (i = 0; i < ARRAY_SIZE(vertex_states_render); ++i)
272 DWORD rs = vertex_states_render[i];
273 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
276 for (i = 0; i < ARRAY_SIZE(vertex_states_texture); ++i)
277 texture_mask |= 1u << vertex_states_texture[i];
278 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
279 for (i = 0; i < ARRAY_SIZE(vertex_states_sampler); ++i)
280 sampler_mask |= 1u << vertex_states_sampler[i];
281 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
282 states->vertexShaderConstantsB = 0xffff;
283 states->vertexShaderConstantsI = 0xffff;
285 memset(states->vs_consts_f, 0xffu, sizeof(states->vs_consts_f));
288 void CDECL wined3d_stateblock_init_contained_states(struct wined3d_stateblock *stateblock)
290 unsigned int i, j;
292 for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
294 DWORD map = stateblock->changed.renderState[i];
295 for (j = 0; map; map >>= 1, ++j)
297 if (!(map & 1)) continue;
299 stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
300 ++stateblock->num_contained_render_states;
304 for (i = 0; i <= WINED3D_HIGHEST_TRANSFORM_STATE >> 5; ++i)
306 DWORD map = stateblock->changed.transform[i];
307 for (j = 0; map; map >>= 1, ++j)
309 if (!(map & 1)) continue;
311 stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
312 ++stateblock->num_contained_transform_states;
316 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
318 DWORD map = stateblock->changed.textureState[i];
320 for(j = 0; map; map >>= 1, ++j)
322 if (!(map & 1)) continue;
324 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
325 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
326 ++stateblock->num_contained_tss_states;
330 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
332 DWORD map = stateblock->changed.samplerState[i];
334 for (j = 0; map; map >>= 1, ++j)
336 if (!(map & 1)) continue;
338 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
339 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
340 ++stateblock->num_contained_sampler_states;
345 static void stateblock_init_lights(struct list *dst_map, const struct list *src_map)
347 unsigned int i;
349 for (i = 0; i < LIGHTMAP_SIZE; ++i)
351 const struct wined3d_light_info *src_light;
353 LIST_FOR_EACH_ENTRY(src_light, &src_map[i], struct wined3d_light_info, entry)
355 struct wined3d_light_info *dst_light = heap_alloc(sizeof(*dst_light));
357 *dst_light = *src_light;
358 list_add_tail(&dst_map[i], &dst_light->entry);
363 ULONG CDECL wined3d_stateblock_incref(struct wined3d_stateblock *stateblock)
365 ULONG refcount = InterlockedIncrement(&stateblock->ref);
367 TRACE("%p increasing refcount to %u.\n", stateblock, refcount);
369 return refcount;
372 void state_unbind_resources(struct wined3d_state *state)
374 struct wined3d_unordered_access_view *uav;
375 struct wined3d_shader_resource_view *srv;
376 struct wined3d_vertex_declaration *decl;
377 struct wined3d_blend_state *blend_state;
378 struct wined3d_rendertarget_view *rtv;
379 struct wined3d_sampler *sampler;
380 struct wined3d_texture *texture;
381 struct wined3d_buffer *buffer;
382 struct wined3d_shader *shader;
383 unsigned int i, j;
385 if ((decl = state->vertex_declaration))
387 state->vertex_declaration = NULL;
388 wined3d_vertex_declaration_decref(decl);
391 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
393 if ((texture = state->textures[i]))
395 state->textures[i] = NULL;
396 wined3d_texture_decref(texture);
400 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
402 if ((buffer = state->stream_output[i].buffer))
404 state->stream_output[i].buffer = NULL;
405 wined3d_buffer_decref(buffer);
409 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
411 if ((buffer = state->streams[i].buffer))
413 state->streams[i].buffer = NULL;
414 wined3d_buffer_decref(buffer);
418 if ((buffer = state->index_buffer))
420 state->index_buffer = NULL;
421 wined3d_buffer_decref(buffer);
424 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
426 if ((shader = state->shader[i]))
428 state->shader[i] = NULL;
429 wined3d_shader_decref(shader);
432 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
434 if ((buffer = state->cb[i][j]))
436 state->cb[i][j] = NULL;
437 wined3d_buffer_decref(buffer);
441 for (j = 0; j < MAX_SAMPLER_OBJECTS; ++j)
443 if ((sampler = state->sampler[i][j]))
445 state->sampler[i][j] = NULL;
446 wined3d_sampler_decref(sampler);
450 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
452 if ((srv = state->shader_resource_view[i][j]))
454 state->shader_resource_view[i][j] = NULL;
455 wined3d_srv_bind_count_dec(srv);
456 wined3d_shader_resource_view_decref(srv);
461 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
463 for (j = 0; j < MAX_UNORDERED_ACCESS_VIEWS; ++j)
465 if ((uav = state->unordered_access_view[i][j]))
467 state->unordered_access_view[i][j] = NULL;
468 wined3d_unordered_access_view_decref(uav);
473 if ((blend_state = state->blend_state))
475 state->blend_state = NULL;
476 wined3d_blend_state_decref(blend_state);
479 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
481 if ((rtv = state->fb.render_targets[i]))
483 state->fb.render_targets[i] = NULL;
484 wined3d_rendertarget_view_decref(rtv);
488 if ((rtv = state->fb.depth_stencil))
490 state->fb.depth_stencil = NULL;
491 wined3d_rendertarget_view_decref(rtv);
495 void wined3d_stateblock_state_cleanup(struct wined3d_stateblock_state *state)
497 struct wined3d_light_info *light, *cursor;
498 struct wined3d_vertex_declaration *decl;
499 struct wined3d_texture *texture;
500 struct wined3d_buffer *buffer;
501 struct wined3d_shader *shader;
502 unsigned int i;
504 if ((decl = state->vertex_declaration))
506 state->vertex_declaration = NULL;
507 wined3d_vertex_declaration_decref(decl);
510 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
512 if ((buffer = state->streams[i].buffer))
514 state->streams[i].buffer = NULL;
515 wined3d_buffer_decref(buffer);
519 if ((buffer = state->index_buffer))
521 state->index_buffer = NULL;
522 wined3d_buffer_decref(buffer);
525 if ((shader = state->vs))
527 state->vs = NULL;
528 wined3d_shader_decref(shader);
531 if ((shader = state->ps))
533 state->ps = NULL;
534 wined3d_shader_decref(shader);
537 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
539 if ((texture = state->textures[i]))
541 state->textures[i] = NULL;
542 wined3d_texture_decref(texture);
546 for (i = 0; i < LIGHTMAP_SIZE; ++i)
548 LIST_FOR_EACH_ENTRY_SAFE(light, cursor, &state->light_state->light_map[i], struct wined3d_light_info, entry)
550 list_remove(&light->entry);
551 heap_free(light);
556 void state_cleanup(struct wined3d_state *state)
558 unsigned int counter;
560 if (!(state->flags & WINED3D_STATE_NO_REF))
561 state_unbind_resources(state);
563 for (counter = 0; counter < WINED3D_MAX_ACTIVE_LIGHTS; ++counter)
565 state->light_state.lights[counter] = NULL;
568 for (counter = 0; counter < LIGHTMAP_SIZE; ++counter)
570 struct list *e1, *e2;
571 LIST_FOR_EACH_SAFE(e1, e2, &state->light_state.light_map[counter])
573 struct wined3d_light_info *light = LIST_ENTRY(e1, struct wined3d_light_info, entry);
574 list_remove(&light->entry);
575 heap_free(light);
580 ULONG CDECL wined3d_stateblock_decref(struct wined3d_stateblock *stateblock)
582 ULONG refcount = InterlockedDecrement(&stateblock->ref);
584 TRACE("%p decreasing refcount to %u\n", stateblock, refcount);
586 if (!refcount)
588 wined3d_stateblock_state_cleanup(&stateblock->stateblock_state);
589 heap_free(stateblock);
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 || start_idx >= d3d_info->limits.vs_uniform_count
1208 || count > d3d_info->limits.vs_uniform_count - start_idx)
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 || start_idx >= d3d_info->limits.ps_uniform_count
1277 || count > d3d_info->limits.ps_uniform_count - start_idx)
1278 return WINED3DERR_INVALIDCALL;
1280 memcpy(&stateblock->stateblock_state.ps_consts_f[start_idx], constants, count * sizeof(*constants));
1281 wined3d_bitmap_set_bits(stateblock->changed.ps_consts_f, start_idx, count);
1282 return WINED3D_OK;
1285 HRESULT CDECL wined3d_stateblock_set_ps_consts_i(struct wined3d_stateblock *stateblock,
1286 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
1288 unsigned int i;
1290 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1291 stateblock, start_idx, count, constants);
1293 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1294 return WINED3DERR_INVALIDCALL;
1296 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1297 count = WINED3D_MAX_CONSTS_I - start_idx;
1299 memcpy(&stateblock->stateblock_state.ps_consts_i[start_idx], constants, count * sizeof(*constants));
1300 for (i = start_idx; i < count + start_idx; ++i)
1301 stateblock->changed.pixelShaderConstantsI |= (1u << i);
1302 return WINED3D_OK;
1305 HRESULT CDECL wined3d_stateblock_set_ps_consts_b(struct wined3d_stateblock *stateblock,
1306 unsigned int start_idx, unsigned int count, const BOOL *constants)
1308 unsigned int i;
1310 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1311 stateblock, start_idx, count, constants);
1313 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1314 return WINED3DERR_INVALIDCALL;
1316 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1317 count = WINED3D_MAX_CONSTS_B - start_idx;
1319 memcpy(&stateblock->stateblock_state.ps_consts_b[start_idx], constants, count * sizeof(*constants));
1320 for (i = start_idx; i < count + start_idx; ++i)
1321 stateblock->changed.pixelShaderConstantsB |= (1u << i);
1322 return WINED3D_OK;
1325 void CDECL wined3d_stateblock_set_vertex_declaration(struct wined3d_stateblock *stateblock,
1326 struct wined3d_vertex_declaration *declaration)
1328 TRACE("stateblock %p, declaration %p.\n", stateblock, declaration);
1330 if (declaration)
1331 wined3d_vertex_declaration_incref(declaration);
1332 if (stateblock->stateblock_state.vertex_declaration)
1333 wined3d_vertex_declaration_decref(stateblock->stateblock_state.vertex_declaration);
1334 stateblock->stateblock_state.vertex_declaration = declaration;
1335 stateblock->changed.vertexDecl = TRUE;
1338 void CDECL wined3d_stateblock_set_render_state(struct wined3d_stateblock *stateblock,
1339 enum wined3d_render_state state, DWORD value)
1341 TRACE("stateblock %p, state %s (%#x), value %#x.\n", stateblock, debug_d3drenderstate(state), state, value);
1343 if (state > WINEHIGHEST_RENDER_STATE)
1345 WARN("Unhandled render state %#x.\n", state);
1346 return;
1349 stateblock->stateblock_state.rs[state] = value;
1350 stateblock->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
1352 if (state == WINED3D_RS_POINTSIZE
1353 && (value == WINED3D_ALPHA_TO_COVERAGE_ENABLE || value == WINED3D_ALPHA_TO_COVERAGE_DISABLE))
1355 stateblock->changed.alpha_to_coverage = 1;
1356 stateblock->stateblock_state.alpha_to_coverage = (value == WINED3D_ALPHA_TO_COVERAGE_ENABLE);
1360 void CDECL wined3d_stateblock_set_sampler_state(struct wined3d_stateblock *stateblock,
1361 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1363 TRACE("stateblock %p, sampler_idx %u, state %s, value %#x.\n",
1364 stateblock, sampler_idx, debug_d3dsamplerstate(state), value);
1366 if (sampler_idx >= ARRAY_SIZE(stateblock->stateblock_state.sampler_states))
1368 WARN("Invalid sampler %u.\n", sampler_idx);
1369 return;
1372 stateblock->stateblock_state.sampler_states[sampler_idx][state] = value;
1373 stateblock->changed.samplerState[sampler_idx] |= 1u << state;
1376 void CDECL wined3d_stateblock_set_texture_stage_state(struct wined3d_stateblock *stateblock,
1377 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
1379 TRACE("stateblock %p, stage %u, state %s, value %#x.\n",
1380 stateblock, stage, debug_d3dtexturestate(state), value);
1382 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
1384 WARN("Invalid state %#x passed.\n", state);
1385 return;
1388 if (stage >= WINED3D_MAX_TEXTURES)
1390 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
1391 stage, WINED3D_MAX_TEXTURES - 1);
1392 return;
1395 stateblock->stateblock_state.texture_states[stage][state] = value;
1396 stateblock->changed.textureState[stage] |= 1u << state;
1399 void CDECL wined3d_stateblock_set_texture(struct wined3d_stateblock *stateblock,
1400 UINT stage, struct wined3d_texture *texture)
1402 TRACE("stateblock %p, stage %u, texture %p.\n", stateblock, stage, texture);
1404 if (stage >= ARRAY_SIZE(stateblock->stateblock_state.textures))
1406 WARN("Ignoring invalid stage %u.\n", stage);
1407 return;
1410 if (texture)
1411 wined3d_texture_incref(texture);
1412 if (stateblock->stateblock_state.textures[stage])
1413 wined3d_texture_decref(stateblock->stateblock_state.textures[stage]);
1414 stateblock->stateblock_state.textures[stage] = texture;
1415 stateblock->changed.textures |= 1u << stage;
1418 void CDECL wined3d_stateblock_set_transform(struct wined3d_stateblock *stateblock,
1419 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1421 TRACE("stateblock %p, state %s, matrix %p.\n", stateblock, debug_d3dtstype(d3dts), matrix);
1422 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1423 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1424 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1425 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1427 stateblock->stateblock_state.transforms[d3dts] = *matrix;
1428 stateblock->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1429 stateblock->changed.transforms = 1;
1432 void CDECL wined3d_stateblock_multiply_transform(struct wined3d_stateblock *stateblock,
1433 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1435 struct wined3d_matrix *mat = &stateblock->stateblock_state.transforms[d3dts];
1437 TRACE("stateblock %p, state %s, matrix %p.\n", stateblock, debug_d3dtstype(d3dts), matrix);
1438 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1439 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1440 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1441 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1443 multiply_matrix(mat, mat, matrix);
1444 stateblock->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1445 stateblock->changed.transforms = 1;
1448 HRESULT CDECL wined3d_stateblock_set_clip_plane(struct wined3d_stateblock *stateblock,
1449 UINT plane_idx, const struct wined3d_vec4 *plane)
1451 TRACE("stateblock %p, plane_idx %u, plane %p.\n", stateblock, plane_idx, plane);
1453 if (plane_idx >= stateblock->device->adapter->d3d_info.limits.max_clip_distances)
1455 TRACE("Application has requested clipplane this device doesn't support.\n");
1456 return WINED3DERR_INVALIDCALL;
1459 stateblock->stateblock_state.clip_planes[plane_idx] = *plane;
1460 stateblock->changed.clipplane |= 1u << plane_idx;
1461 return S_OK;
1464 void CDECL wined3d_stateblock_set_material(struct wined3d_stateblock *stateblock,
1465 const struct wined3d_material *material)
1467 TRACE("stateblock %p, material %p.\n", stateblock, material);
1469 stateblock->stateblock_state.material = *material;
1470 stateblock->changed.material = TRUE;
1473 void CDECL wined3d_stateblock_set_viewport(struct wined3d_stateblock *stateblock,
1474 const struct wined3d_viewport *viewport)
1476 TRACE("stateblock %p, viewport %p.\n", stateblock, viewport);
1478 stateblock->stateblock_state.viewport = *viewport;
1479 stateblock->changed.viewport = TRUE;
1482 void CDECL wined3d_stateblock_set_scissor_rect(struct wined3d_stateblock *stateblock, const RECT *rect)
1484 TRACE("stateblock %p, rect %s.\n", stateblock, wine_dbgstr_rect(rect));
1486 stateblock->stateblock_state.scissor_rect = *rect;
1487 stateblock->changed.scissorRect = TRUE;
1490 void CDECL wined3d_stateblock_set_index_buffer(struct wined3d_stateblock *stateblock,
1491 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1493 TRACE("stateblock %p, buffer %p, format %s.\n", stateblock, buffer, debug_d3dformat(format_id));
1495 if (buffer)
1496 wined3d_buffer_incref(buffer);
1497 if (stateblock->stateblock_state.index_buffer)
1498 wined3d_buffer_decref(stateblock->stateblock_state.index_buffer);
1499 stateblock->stateblock_state.index_buffer = buffer;
1500 stateblock->stateblock_state.index_format = format_id;
1501 stateblock->changed.indices = TRUE;
1504 void CDECL wined3d_stateblock_set_base_vertex_index(struct wined3d_stateblock *stateblock, INT base_index)
1506 TRACE("stateblock %p, base_index %d.\n", stateblock, base_index);
1508 stateblock->stateblock_state.base_vertex_index = base_index;
1511 HRESULT CDECL wined3d_stateblock_set_stream_source(struct wined3d_stateblock *stateblock,
1512 UINT stream_idx, struct wined3d_buffer *buffer, UINT offset, UINT stride)
1514 struct wined3d_stream_state *stream;
1516 TRACE("stateblock %p, stream_idx %u, buffer %p, stride %u.\n",
1517 stateblock, stream_idx, buffer, stride);
1519 if (stream_idx >= WINED3D_MAX_STREAMS)
1521 WARN("Stream index %u out of range.\n", stream_idx);
1522 return WINED3DERR_INVALIDCALL;
1525 stream = &stateblock->stateblock_state.streams[stream_idx];
1527 if (buffer)
1528 wined3d_buffer_incref(buffer);
1529 if (stream->buffer)
1530 wined3d_buffer_decref(stream->buffer);
1531 stream->buffer = buffer;
1532 stream->stride = stride;
1533 stream->offset = offset;
1534 stateblock->changed.streamSource |= 1u << stream_idx;
1535 return WINED3D_OK;
1538 HRESULT CDECL wined3d_stateblock_set_stream_source_freq(struct wined3d_stateblock *stateblock,
1539 UINT stream_idx, UINT divider)
1541 struct wined3d_stream_state *stream;
1543 TRACE("stateblock %p, stream_idx %u, divider %#x.\n", stateblock, stream_idx, divider);
1545 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1547 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1548 return WINED3DERR_INVALIDCALL;
1550 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1552 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1553 return WINED3DERR_INVALIDCALL;
1555 if (!divider)
1557 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1558 return WINED3DERR_INVALIDCALL;
1561 stream = &stateblock->stateblock_state.streams[stream_idx];
1562 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1563 stream->frequency = divider & 0x7fffff;
1564 stateblock->changed.streamFreq |= 1u << stream_idx;
1565 return WINED3D_OK;
1568 HRESULT CDECL wined3d_stateblock_set_light(struct wined3d_stateblock *stateblock,
1569 UINT light_idx, const struct wined3d_light *light)
1571 struct wined3d_light_info *object = NULL;
1573 TRACE("stateblock %p, light_idx %u, light %p.\n", stateblock, light_idx, light);
1575 /* Check the parameter range. Need for speed most wanted sets junk lights
1576 * which confuse the GL driver. */
1577 if (!light)
1578 return WINED3DERR_INVALIDCALL;
1580 switch (light->type)
1582 case WINED3D_LIGHT_POINT:
1583 case WINED3D_LIGHT_SPOT:
1584 case WINED3D_LIGHT_GLSPOT:
1585 /* Incorrect attenuation values can cause the gl driver to crash.
1586 * Happens with Need for speed most wanted. */
1587 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1589 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1590 return WINED3DERR_INVALIDCALL;
1592 break;
1594 case WINED3D_LIGHT_DIRECTIONAL:
1595 case WINED3D_LIGHT_PARALLELPOINT:
1596 /* Ignores attenuation */
1597 break;
1599 default:
1600 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL.\n");
1601 return WINED3DERR_INVALIDCALL;
1604 stateblock->changed.lights = 1;
1605 return wined3d_light_state_set_light(stateblock->stateblock_state.light_state, light_idx, light, &object);
1608 HRESULT CDECL wined3d_stateblock_set_light_enable(struct wined3d_stateblock *stateblock, UINT light_idx, BOOL enable)
1610 struct wined3d_light_state *light_state = stateblock->stateblock_state.light_state;
1611 struct wined3d_light_info *light_info;
1612 HRESULT hr;
1614 TRACE("stateblock %p, light_idx %u, enable %#x.\n", stateblock, light_idx, enable);
1616 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1618 if (FAILED(hr = wined3d_light_state_set_light(light_state, light_idx, &WINED3D_default_light, &light_info)))
1619 return hr;
1621 wined3d_light_state_enable_light(light_state, &stateblock->device->adapter->d3d_info, light_info, enable);
1622 stateblock->changed.lights = 1;
1623 return S_OK;
1626 const struct wined3d_stateblock_state * CDECL wined3d_stateblock_get_state(const struct wined3d_stateblock *stateblock)
1628 return &stateblock->stateblock_state;
1631 HRESULT CDECL wined3d_stateblock_get_light(const struct wined3d_stateblock *stateblock,
1632 UINT light_idx, struct wined3d_light *light, BOOL *enabled)
1634 struct wined3d_light_info *light_info;
1636 if (!(light_info = wined3d_light_state_get_light(&stateblock->light_state, light_idx)))
1638 TRACE("Light %u is not defined.\n", light_idx);
1639 return WINED3DERR_INVALIDCALL;
1641 *light = light_info->OriginalParms;
1642 *enabled = light_info->enabled ? 128 : 0;
1643 return WINED3D_OK;
1646 static void init_default_render_states(DWORD rs[WINEHIGHEST_RENDER_STATE + 1], const struct wined3d_d3d_info *d3d_info)
1648 union
1650 struct wined3d_line_pattern lp;
1651 DWORD d;
1652 } lp;
1653 union
1655 float f;
1656 DWORD d;
1657 } tmpfloat;
1659 rs[WINED3D_RS_ZENABLE] = WINED3D_ZB_TRUE;
1660 rs[WINED3D_RS_FILLMODE] = WINED3D_FILL_SOLID;
1661 rs[WINED3D_RS_SHADEMODE] = WINED3D_SHADE_GOURAUD;
1662 lp.lp.repeat_factor = 0;
1663 lp.lp.line_pattern = 0;
1664 rs[WINED3D_RS_LINEPATTERN] = lp.d;
1665 rs[WINED3D_RS_ZWRITEENABLE] = TRUE;
1666 rs[WINED3D_RS_ALPHATESTENABLE] = FALSE;
1667 rs[WINED3D_RS_LASTPIXEL] = TRUE;
1668 rs[WINED3D_RS_SRCBLEND] = WINED3D_BLEND_ONE;
1669 rs[WINED3D_RS_DESTBLEND] = WINED3D_BLEND_ZERO;
1670 rs[WINED3D_RS_CULLMODE] = WINED3D_CULL_BACK;
1671 rs[WINED3D_RS_ZFUNC] = WINED3D_CMP_LESSEQUAL;
1672 rs[WINED3D_RS_ALPHAFUNC] = WINED3D_CMP_ALWAYS;
1673 rs[WINED3D_RS_ALPHAREF] = 0;
1674 rs[WINED3D_RS_DITHERENABLE] = FALSE;
1675 rs[WINED3D_RS_ALPHABLENDENABLE] = FALSE;
1676 rs[WINED3D_RS_FOGENABLE] = FALSE;
1677 rs[WINED3D_RS_SPECULARENABLE] = FALSE;
1678 rs[WINED3D_RS_ZVISIBLE] = 0;
1679 rs[WINED3D_RS_FOGCOLOR] = 0;
1680 rs[WINED3D_RS_FOGTABLEMODE] = WINED3D_FOG_NONE;
1681 tmpfloat.f = 0.0f;
1682 rs[WINED3D_RS_FOGSTART] = tmpfloat.d;
1683 tmpfloat.f = 1.0f;
1684 rs[WINED3D_RS_FOGEND] = tmpfloat.d;
1685 tmpfloat.f = 1.0f;
1686 rs[WINED3D_RS_FOGDENSITY] = tmpfloat.d;
1687 rs[WINED3D_RS_RANGEFOGENABLE] = FALSE;
1688 rs[WINED3D_RS_STENCILENABLE] = FALSE;
1689 rs[WINED3D_RS_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1690 rs[WINED3D_RS_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1691 rs[WINED3D_RS_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1692 rs[WINED3D_RS_STENCILREF] = 0;
1693 rs[WINED3D_RS_STENCILMASK] = 0xffffffff;
1694 rs[WINED3D_RS_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1695 rs[WINED3D_RS_STENCILWRITEMASK] = 0xffffffff;
1696 rs[WINED3D_RS_TEXTUREFACTOR] = 0xffffffff;
1697 rs[WINED3D_RS_WRAP0] = 0;
1698 rs[WINED3D_RS_WRAP1] = 0;
1699 rs[WINED3D_RS_WRAP2] = 0;
1700 rs[WINED3D_RS_WRAP3] = 0;
1701 rs[WINED3D_RS_WRAP4] = 0;
1702 rs[WINED3D_RS_WRAP5] = 0;
1703 rs[WINED3D_RS_WRAP6] = 0;
1704 rs[WINED3D_RS_WRAP7] = 0;
1705 rs[WINED3D_RS_CLIPPING] = TRUE;
1706 rs[WINED3D_RS_LIGHTING] = TRUE;
1707 rs[WINED3D_RS_AMBIENT] = 0;
1708 rs[WINED3D_RS_FOGVERTEXMODE] = WINED3D_FOG_NONE;
1709 rs[WINED3D_RS_COLORVERTEX] = TRUE;
1710 rs[WINED3D_RS_LOCALVIEWER] = TRUE;
1711 rs[WINED3D_RS_NORMALIZENORMALS] = FALSE;
1712 rs[WINED3D_RS_DIFFUSEMATERIALSOURCE] = WINED3D_MCS_COLOR1;
1713 rs[WINED3D_RS_SPECULARMATERIALSOURCE] = WINED3D_MCS_COLOR2;
1714 rs[WINED3D_RS_AMBIENTMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1715 rs[WINED3D_RS_EMISSIVEMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1716 rs[WINED3D_RS_VERTEXBLEND] = WINED3D_VBF_DISABLE;
1717 rs[WINED3D_RS_CLIPPLANEENABLE] = 0;
1718 rs[WINED3D_RS_SOFTWAREVERTEXPROCESSING] = FALSE;
1719 tmpfloat.f = 1.0f;
1720 rs[WINED3D_RS_POINTSIZE] = tmpfloat.d;
1721 tmpfloat.f = 1.0f;
1722 rs[WINED3D_RS_POINTSIZE_MIN] = tmpfloat.d;
1723 rs[WINED3D_RS_POINTSPRITEENABLE] = FALSE;
1724 rs[WINED3D_RS_POINTSCALEENABLE] = FALSE;
1725 tmpfloat.f = 1.0f;
1726 rs[WINED3D_RS_POINTSCALE_A] = tmpfloat.d;
1727 tmpfloat.f = 0.0f;
1728 rs[WINED3D_RS_POINTSCALE_B] = tmpfloat.d;
1729 tmpfloat.f = 0.0f;
1730 rs[WINED3D_RS_POINTSCALE_C] = tmpfloat.d;
1731 rs[WINED3D_RS_MULTISAMPLEANTIALIAS] = TRUE;
1732 rs[WINED3D_RS_MULTISAMPLEMASK] = 0xffffffff;
1733 rs[WINED3D_RS_PATCHEDGESTYLE] = WINED3D_PATCH_EDGE_DISCRETE;
1734 tmpfloat.f = 1.0f;
1735 rs[WINED3D_RS_PATCHSEGMENTS] = tmpfloat.d;
1736 rs[WINED3D_RS_DEBUGMONITORTOKEN] = 0xbaadcafe;
1737 tmpfloat.f = d3d_info->limits.pointsize_max;
1738 rs[WINED3D_RS_POINTSIZE_MAX] = tmpfloat.d;
1739 rs[WINED3D_RS_INDEXEDVERTEXBLENDENABLE] = FALSE;
1740 rs[WINED3D_RS_COLORWRITEENABLE] = 0x0000000f;
1741 tmpfloat.f = 0.0f;
1742 rs[WINED3D_RS_TWEENFACTOR] = tmpfloat.d;
1743 rs[WINED3D_RS_BLENDOP] = WINED3D_BLEND_OP_ADD;
1744 rs[WINED3D_RS_POSITIONDEGREE] = WINED3D_DEGREE_CUBIC;
1745 rs[WINED3D_RS_NORMALDEGREE] = WINED3D_DEGREE_LINEAR;
1746 /* states new in d3d9 */
1747 rs[WINED3D_RS_SCISSORTESTENABLE] = FALSE;
1748 rs[WINED3D_RS_SLOPESCALEDEPTHBIAS] = 0;
1749 tmpfloat.f = 1.0f;
1750 rs[WINED3D_RS_MINTESSELLATIONLEVEL] = tmpfloat.d;
1751 rs[WINED3D_RS_MAXTESSELLATIONLEVEL] = tmpfloat.d;
1752 rs[WINED3D_RS_ANTIALIASEDLINEENABLE] = FALSE;
1753 tmpfloat.f = 0.0f;
1754 rs[WINED3D_RS_ADAPTIVETESS_X] = tmpfloat.d;
1755 rs[WINED3D_RS_ADAPTIVETESS_Y] = tmpfloat.d;
1756 tmpfloat.f = 1.0f;
1757 rs[WINED3D_RS_ADAPTIVETESS_Z] = tmpfloat.d;
1758 tmpfloat.f = 0.0f;
1759 rs[WINED3D_RS_ADAPTIVETESS_W] = tmpfloat.d;
1760 rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION] = FALSE;
1761 rs[WINED3D_RS_TWOSIDEDSTENCILMODE] = FALSE;
1762 rs[WINED3D_RS_BACK_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1763 rs[WINED3D_RS_BACK_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1764 rs[WINED3D_RS_BACK_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1765 rs[WINED3D_RS_BACK_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1766 rs[WINED3D_RS_COLORWRITEENABLE1] = 0x0000000f;
1767 rs[WINED3D_RS_COLORWRITEENABLE2] = 0x0000000f;
1768 rs[WINED3D_RS_COLORWRITEENABLE3] = 0x0000000f;
1769 rs[WINED3D_RS_BLENDFACTOR] = 0xffffffff;
1770 rs[WINED3D_RS_SRGBWRITEENABLE] = 0;
1771 rs[WINED3D_RS_DEPTHBIAS] = 0;
1772 rs[WINED3D_RS_WRAP8] = 0;
1773 rs[WINED3D_RS_WRAP9] = 0;
1774 rs[WINED3D_RS_WRAP10] = 0;
1775 rs[WINED3D_RS_WRAP11] = 0;
1776 rs[WINED3D_RS_WRAP12] = 0;
1777 rs[WINED3D_RS_WRAP13] = 0;
1778 rs[WINED3D_RS_WRAP14] = 0;
1779 rs[WINED3D_RS_WRAP15] = 0;
1780 rs[WINED3D_RS_SEPARATEALPHABLENDENABLE] = FALSE;
1781 rs[WINED3D_RS_SRCBLENDALPHA] = WINED3D_BLEND_ONE;
1782 rs[WINED3D_RS_DESTBLENDALPHA] = WINED3D_BLEND_ZERO;
1783 rs[WINED3D_RS_BLENDOPALPHA] = WINED3D_BLEND_OP_ADD;
1786 static void init_default_texture_state(unsigned int i, DWORD stage[WINED3D_HIGHEST_TEXTURE_STATE + 1])
1788 stage[WINED3D_TSS_COLOR_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_MODULATE;
1789 stage[WINED3D_TSS_COLOR_ARG1] = WINED3DTA_TEXTURE;
1790 stage[WINED3D_TSS_COLOR_ARG2] = WINED3DTA_CURRENT;
1791 stage[WINED3D_TSS_ALPHA_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_SELECT_ARG1;
1792 stage[WINED3D_TSS_ALPHA_ARG1] = WINED3DTA_TEXTURE;
1793 stage[WINED3D_TSS_ALPHA_ARG2] = WINED3DTA_CURRENT;
1794 stage[WINED3D_TSS_BUMPENV_MAT00] = 0;
1795 stage[WINED3D_TSS_BUMPENV_MAT01] = 0;
1796 stage[WINED3D_TSS_BUMPENV_MAT10] = 0;
1797 stage[WINED3D_TSS_BUMPENV_MAT11] = 0;
1798 stage[WINED3D_TSS_TEXCOORD_INDEX] = i;
1799 stage[WINED3D_TSS_BUMPENV_LSCALE] = 0;
1800 stage[WINED3D_TSS_BUMPENV_LOFFSET] = 0;
1801 stage[WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS] = WINED3D_TTFF_DISABLE;
1802 stage[WINED3D_TSS_COLOR_ARG0] = WINED3DTA_CURRENT;
1803 stage[WINED3D_TSS_ALPHA_ARG0] = WINED3DTA_CURRENT;
1804 stage[WINED3D_TSS_RESULT_ARG] = WINED3DTA_CURRENT;
1807 static void init_default_sampler_states(DWORD states[WINED3D_MAX_COMBINED_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1])
1809 unsigned int i;
1811 for (i = 0 ; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1813 TRACE("Setting up default samplers states for sampler %u.\n", i);
1814 states[i][WINED3D_SAMP_ADDRESS_U] = WINED3D_TADDRESS_WRAP;
1815 states[i][WINED3D_SAMP_ADDRESS_V] = WINED3D_TADDRESS_WRAP;
1816 states[i][WINED3D_SAMP_ADDRESS_W] = WINED3D_TADDRESS_WRAP;
1817 states[i][WINED3D_SAMP_BORDER_COLOR] = 0;
1818 states[i][WINED3D_SAMP_MAG_FILTER] = WINED3D_TEXF_POINT;
1819 states[i][WINED3D_SAMP_MIN_FILTER] = WINED3D_TEXF_POINT;
1820 states[i][WINED3D_SAMP_MIP_FILTER] = WINED3D_TEXF_NONE;
1821 states[i][WINED3D_SAMP_MIPMAP_LOD_BIAS] = 0;
1822 states[i][WINED3D_SAMP_MAX_MIP_LEVEL] = 0;
1823 states[i][WINED3D_SAMP_MAX_ANISOTROPY] = 1;
1824 states[i][WINED3D_SAMP_SRGB_TEXTURE] = 0;
1825 /* TODO: Indicates which element of a multielement texture to use. */
1826 states[i][WINED3D_SAMP_ELEMENT_INDEX] = 0;
1827 /* TODO: Vertex offset in the presampled displacement map. */
1828 states[i][WINED3D_SAMP_DMAP_OFFSET] = 0;
1832 static void state_init_default(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info)
1834 unsigned int i;
1835 struct wined3d_matrix identity;
1837 TRACE("state %p, d3d_info %p.\n", state, d3d_info);
1839 get_identity_matrix(&identity);
1840 state->primitive_type = WINED3D_PT_UNDEFINED;
1841 state->patch_vertex_count = 0;
1843 /* Set some of the defaults for lights, transforms etc */
1844 state->transforms[WINED3D_TS_PROJECTION] = identity;
1845 state->transforms[WINED3D_TS_VIEW] = identity;
1846 for (i = 0; i < 256; ++i)
1848 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
1851 init_default_render_states(state->render_states, d3d_info);
1853 /* Texture Stage States - Put directly into state block, we will call function below */
1854 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1856 TRACE("Setting up default texture states for texture Stage %u.\n", i);
1857 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
1858 init_default_texture_state(i, state->texture_states[i]);
1861 init_default_sampler_states(state->sampler_states);
1863 state->blend_factor.r = 1.0f;
1864 state->blend_factor.g = 1.0f;
1865 state->blend_factor.b = 1.0f;
1866 state->blend_factor.a = 1.0f;
1868 state->sample_mask = 0xffffffff;
1870 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1871 state->streams[i].frequency = 1;
1874 void state_init(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info,
1875 uint32_t flags, enum wined3d_feature_level feature_level)
1877 unsigned int i;
1879 state->feature_level = feature_level;
1880 state->flags = flags;
1882 for (i = 0; i < LIGHTMAP_SIZE; i++)
1884 list_init(&state->light_state.light_map[i]);
1887 if (flags & WINED3D_STATE_INIT_DEFAULT)
1888 state_init_default(state, d3d_info);
1891 static bool wined3d_select_feature_level(const struct wined3d_adapter *adapter,
1892 const enum wined3d_feature_level *levels, unsigned int level_count,
1893 enum wined3d_feature_level *selected_level)
1895 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
1896 unsigned int i;
1898 for (i = 0; i < level_count; ++i)
1900 if (levels[i] && d3d_info->feature_level >= levels[i])
1902 *selected_level = levels[i];
1903 return true;
1907 FIXME_(winediag)("None of the requested D3D feature levels is supported on this GPU "
1908 "with the current shader backend.\n");
1909 return false;
1912 HRESULT CDECL wined3d_state_create(struct wined3d_device *device,
1913 const enum wined3d_feature_level *levels, unsigned int level_count, struct wined3d_state **state)
1915 enum wined3d_feature_level feature_level;
1916 struct wined3d_state *object;
1918 TRACE("device %p, levels %p, level_count %u, state %p.\n", device, levels, level_count, state);
1920 if (!wined3d_select_feature_level(device->adapter, levels, level_count, &feature_level))
1921 return E_FAIL;
1923 TRACE("Selected feature level %s.\n", wined3d_debug_feature_level(feature_level));
1925 if (!(object = heap_alloc_zero(sizeof(*object))))
1926 return E_OUTOFMEMORY;
1927 state_init(object, &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT, feature_level);
1929 *state = object;
1930 return S_OK;
1933 enum wined3d_feature_level CDECL wined3d_state_get_feature_level(const struct wined3d_state *state)
1935 TRACE("state %p.\n", state);
1937 return state->feature_level;
1940 void CDECL wined3d_state_destroy(struct wined3d_state *state)
1942 TRACE("state %p.\n", state);
1944 state_cleanup(state);
1945 heap_free(state);
1948 static void stateblock_state_init_default(struct wined3d_stateblock_state *state,
1949 const struct wined3d_d3d_info *d3d_info)
1951 struct wined3d_matrix identity;
1952 unsigned int i;
1954 get_identity_matrix(&identity);
1956 state->transforms[WINED3D_TS_PROJECTION] = identity;
1957 state->transforms[WINED3D_TS_VIEW] = identity;
1958 for (i = 0; i < 256; ++i)
1960 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
1963 init_default_render_states(state->rs, d3d_info);
1965 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1967 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
1968 init_default_texture_state(i, state->texture_states[i]);
1971 init_default_sampler_states(state->sampler_states);
1973 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1974 state->streams[i].frequency = 1;
1977 void wined3d_stateblock_state_init(struct wined3d_stateblock_state *state,
1978 const struct wined3d_device *device, DWORD flags)
1980 unsigned int i;
1982 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); i++)
1984 list_init(&state->light_state->light_map[i]);
1987 if (flags & WINED3D_STATE_INIT_DEFAULT)
1988 stateblock_state_init_default(state, &device->adapter->d3d_info);
1992 static HRESULT stateblock_init(struct wined3d_stateblock *stateblock, const struct wined3d_stateblock *device_state,
1993 struct wined3d_device *device, enum wined3d_stateblock_type type)
1995 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
1997 stateblock->ref = 1;
1998 stateblock->device = device;
1999 stateblock->stateblock_state.light_state = &stateblock->light_state;
2000 wined3d_stateblock_state_init(&stateblock->stateblock_state, device,
2001 type == WINED3D_SBT_PRIMARY ? WINED3D_STATE_INIT_DEFAULT : 0);
2003 stateblock->changed.store_stream_offset = 1;
2005 if (type == WINED3D_SBT_RECORDED || type == WINED3D_SBT_PRIMARY)
2006 return WINED3D_OK;
2008 TRACE("Updating changed flags appropriate for type %#x.\n", type);
2010 switch (type)
2012 case WINED3D_SBT_ALL:
2013 stateblock_init_lights(stateblock->stateblock_state.light_state->light_map,
2014 device_state->stateblock_state.light_state->light_map);
2015 stateblock_savedstates_set_all(&stateblock->changed,
2016 d3d_info->limits.vs_uniform_count, d3d_info->limits.ps_uniform_count);
2017 break;
2019 case WINED3D_SBT_PIXEL_STATE:
2020 stateblock_savedstates_set_pixel(&stateblock->changed,
2021 d3d_info->limits.ps_uniform_count);
2022 break;
2024 case WINED3D_SBT_VERTEX_STATE:
2025 stateblock_init_lights(stateblock->stateblock_state.light_state->light_map,
2026 device_state->stateblock_state.light_state->light_map);
2027 stateblock_savedstates_set_vertex(&stateblock->changed,
2028 d3d_info->limits.vs_uniform_count);
2029 break;
2031 default:
2032 FIXME("Unrecognized state block type %#x.\n", type);
2033 break;
2036 wined3d_stateblock_init_contained_states(stateblock);
2037 wined3d_stateblock_capture(stateblock, device_state);
2039 /* According to the tests, stream offset is not updated in the captured state if
2040 * the state was captured on state block creation. This is not the case for
2041 * state blocks initialized with BeginStateBlock / EndStateBlock, multiple
2042 * captures get stream offsets updated. */
2043 stateblock->changed.store_stream_offset = 0;
2045 return WINED3D_OK;
2048 HRESULT CDECL wined3d_stateblock_create(struct wined3d_device *device, const struct wined3d_stateblock *device_state,
2049 enum wined3d_stateblock_type type, struct wined3d_stateblock **stateblock)
2051 struct wined3d_stateblock *object;
2052 HRESULT hr;
2054 TRACE("device %p, device_state %p, type %#x, stateblock %p.\n",
2055 device, device_state, type, stateblock);
2057 if (!(object = heap_alloc_zero(sizeof(*object))))
2058 return E_OUTOFMEMORY;
2060 hr = stateblock_init(object, device_state, device, type);
2061 if (FAILED(hr))
2063 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2064 heap_free(object);
2065 return hr;
2068 TRACE("Created stateblock %p.\n", object);
2069 *stateblock = object;
2071 return WINED3D_OK;
2074 void CDECL wined3d_stateblock_reset(struct wined3d_stateblock *stateblock)
2076 TRACE("stateblock %p.\n", stateblock);
2078 wined3d_stateblock_state_cleanup(&stateblock->stateblock_state);
2079 memset(&stateblock->stateblock_state, 0, sizeof(stateblock->stateblock_state));
2080 stateblock->stateblock_state.light_state = &stateblock->light_state;
2081 wined3d_stateblock_state_init(&stateblock->stateblock_state, stateblock->device, WINED3D_STATE_INIT_DEFAULT);