wined3d: Introduce a wined3d_state_get_ffp_texture() helper.
[wine.git] / dlls / wined3d / stateblock.c
blob6361de4909b58c616b464071cbad01d14c57c2aa
1 /*
2 * state block implementation
4 * Copyright 2002 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2007 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
9 * Copyright 2019,2020,2022 Zebediah Figura for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag);
31 struct wined3d_saved_states
33 uint32_t vs_consts_f[WINED3D_BITMAP_SIZE(WINED3D_MAX_VS_CONSTS_F)];
34 uint16_t vertexShaderConstantsI; /* WINED3D_MAX_CONSTS_I, 16 */
35 uint16_t vertexShaderConstantsB; /* WINED3D_MAX_CONSTS_B, 16 */
36 uint32_t ps_consts_f[WINED3D_BITMAP_SIZE(WINED3D_MAX_PS_CONSTS_F)];
37 uint16_t pixelShaderConstantsI; /* WINED3D_MAX_CONSTS_I, 16 */
38 uint16_t pixelShaderConstantsB; /* WINED3D_MAX_CONSTS_B, 16 */
39 uint32_t transform[WINED3D_BITMAP_SIZE(WINED3D_HIGHEST_TRANSFORM_STATE + 1)];
40 uint16_t streamSource; /* WINED3D_MAX_STREAMS, 16 */
41 uint16_t streamFreq; /* WINED3D_MAX_STREAMS, 16 */
42 uint32_t renderState[WINED3D_BITMAP_SIZE(WINEHIGHEST_RENDER_STATE + 1)];
43 uint32_t textureState[WINED3D_MAX_FFP_TEXTURES]; /* WINED3D_HIGHEST_TEXTURE_STATE + 1, 18 */
44 uint16_t samplerState[WINED3D_MAX_COMBINED_SAMPLERS]; /* WINED3D_HIGHEST_SAMPLER_STATE + 1, 14 */
45 uint32_t clipplane; /* WINED3D_MAX_CLIP_DISTANCES, 8 */
46 uint32_t textures : 20; /* WINED3D_MAX_COMBINED_SAMPLERS, 20 */
47 uint32_t indices : 1;
48 uint32_t material : 1;
49 uint32_t viewport : 1;
50 uint32_t vertexDecl : 1;
51 uint32_t pixelShader : 1;
52 uint32_t vertexShader : 1;
53 uint32_t scissorRect : 1;
54 uint32_t store_stream_offset : 1;
55 uint32_t alpha_to_coverage : 1;
56 uint32_t lights : 1;
57 uint32_t transforms : 1;
58 uint32_t padding : 1;
60 struct list changed_lights;
63 struct stage_state
65 unsigned int stage, state;
68 struct wined3d_stateblock
70 LONG ref;
71 struct wined3d_device *device;
73 struct wined3d_saved_states changed;
75 struct wined3d_stateblock_state stateblock_state;
76 struct wined3d_light_state light_state;
78 unsigned int contained_render_states[WINEHIGHEST_RENDER_STATE + 1];
79 unsigned int num_contained_render_states;
80 unsigned int contained_transform_states[WINED3D_HIGHEST_TRANSFORM_STATE + 1];
81 unsigned int num_contained_transform_states;
82 struct stage_state contained_tss_states[WINED3D_MAX_FFP_TEXTURES * (WINED3D_HIGHEST_TEXTURE_STATE + 1)];
83 unsigned int num_contained_tss_states;
84 struct stage_state contained_sampler_states[WINED3D_MAX_COMBINED_SAMPLERS * WINED3D_HIGHEST_SAMPLER_STATE];
85 unsigned int num_contained_sampler_states;
88 static const DWORD pixel_states_render[] =
90 WINED3D_RS_ALPHABLENDENABLE,
91 WINED3D_RS_ALPHAFUNC,
92 WINED3D_RS_ALPHAREF,
93 WINED3D_RS_ALPHATESTENABLE,
94 WINED3D_RS_ANTIALIASEDLINEENABLE,
95 WINED3D_RS_BLENDFACTOR,
96 WINED3D_RS_BLENDOP,
97 WINED3D_RS_BLENDOPALPHA,
98 WINED3D_RS_BACK_STENCILFAIL,
99 WINED3D_RS_BACK_STENCILPASS,
100 WINED3D_RS_BACK_STENCILZFAIL,
101 WINED3D_RS_COLORWRITEENABLE,
102 WINED3D_RS_COLORWRITEENABLE1,
103 WINED3D_RS_COLORWRITEENABLE2,
104 WINED3D_RS_COLORWRITEENABLE3,
105 WINED3D_RS_DEPTHBIAS,
106 WINED3D_RS_DESTBLEND,
107 WINED3D_RS_DESTBLENDALPHA,
108 WINED3D_RS_DITHERENABLE,
109 WINED3D_RS_FILLMODE,
110 WINED3D_RS_FOGDENSITY,
111 WINED3D_RS_FOGEND,
112 WINED3D_RS_FOGSTART,
113 WINED3D_RS_LASTPIXEL,
114 WINED3D_RS_SCISSORTESTENABLE,
115 WINED3D_RS_SEPARATEALPHABLENDENABLE,
116 WINED3D_RS_SHADEMODE,
117 WINED3D_RS_SLOPESCALEDEPTHBIAS,
118 WINED3D_RS_SRCBLEND,
119 WINED3D_RS_SRCBLENDALPHA,
120 WINED3D_RS_SRGBWRITEENABLE,
121 WINED3D_RS_STENCILENABLE,
122 WINED3D_RS_STENCILFAIL,
123 WINED3D_RS_STENCILFUNC,
124 WINED3D_RS_STENCILMASK,
125 WINED3D_RS_STENCILPASS,
126 WINED3D_RS_STENCILREF,
127 WINED3D_RS_STENCILWRITEMASK,
128 WINED3D_RS_STENCILZFAIL,
129 WINED3D_RS_TEXTUREFACTOR,
130 WINED3D_RS_TWOSIDEDSTENCILMODE,
131 WINED3D_RS_WRAP0,
132 WINED3D_RS_WRAP1,
133 WINED3D_RS_WRAP10,
134 WINED3D_RS_WRAP11,
135 WINED3D_RS_WRAP12,
136 WINED3D_RS_WRAP13,
137 WINED3D_RS_WRAP14,
138 WINED3D_RS_WRAP15,
139 WINED3D_RS_WRAP2,
140 WINED3D_RS_WRAP3,
141 WINED3D_RS_WRAP4,
142 WINED3D_RS_WRAP5,
143 WINED3D_RS_WRAP6,
144 WINED3D_RS_WRAP7,
145 WINED3D_RS_WRAP8,
146 WINED3D_RS_WRAP9,
147 WINED3D_RS_ZENABLE,
148 WINED3D_RS_ZFUNC,
149 WINED3D_RS_ZWRITEENABLE,
152 static const DWORD pixel_states_texture[] =
154 WINED3D_TSS_ALPHA_ARG0,
155 WINED3D_TSS_ALPHA_ARG1,
156 WINED3D_TSS_ALPHA_ARG2,
157 WINED3D_TSS_ALPHA_OP,
158 WINED3D_TSS_BUMPENV_LOFFSET,
159 WINED3D_TSS_BUMPENV_LSCALE,
160 WINED3D_TSS_BUMPENV_MAT00,
161 WINED3D_TSS_BUMPENV_MAT01,
162 WINED3D_TSS_BUMPENV_MAT10,
163 WINED3D_TSS_BUMPENV_MAT11,
164 WINED3D_TSS_COLOR_ARG0,
165 WINED3D_TSS_COLOR_ARG1,
166 WINED3D_TSS_COLOR_ARG2,
167 WINED3D_TSS_COLOR_OP,
168 WINED3D_TSS_RESULT_ARG,
169 WINED3D_TSS_TEXCOORD_INDEX,
170 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,
173 static const DWORD pixel_states_sampler[] =
175 WINED3D_SAMP_ADDRESS_U,
176 WINED3D_SAMP_ADDRESS_V,
177 WINED3D_SAMP_ADDRESS_W,
178 WINED3D_SAMP_BORDER_COLOR,
179 WINED3D_SAMP_MAG_FILTER,
180 WINED3D_SAMP_MIN_FILTER,
181 WINED3D_SAMP_MIP_FILTER,
182 WINED3D_SAMP_MIPMAP_LOD_BIAS,
183 WINED3D_SAMP_MAX_MIP_LEVEL,
184 WINED3D_SAMP_MAX_ANISOTROPY,
185 WINED3D_SAMP_SRGB_TEXTURE,
186 WINED3D_SAMP_ELEMENT_INDEX,
189 static const DWORD vertex_states_render[] =
191 WINED3D_RS_ADAPTIVETESS_W,
192 WINED3D_RS_ADAPTIVETESS_X,
193 WINED3D_RS_ADAPTIVETESS_Y,
194 WINED3D_RS_ADAPTIVETESS_Z,
195 WINED3D_RS_AMBIENT,
196 WINED3D_RS_AMBIENTMATERIALSOURCE,
197 WINED3D_RS_CLIPPING,
198 WINED3D_RS_CLIPPLANEENABLE,
199 WINED3D_RS_COLORVERTEX,
200 WINED3D_RS_CULLMODE,
201 WINED3D_RS_DIFFUSEMATERIALSOURCE,
202 WINED3D_RS_EMISSIVEMATERIALSOURCE,
203 WINED3D_RS_ENABLEADAPTIVETESSELLATION,
204 WINED3D_RS_FOGCOLOR,
205 WINED3D_RS_FOGDENSITY,
206 WINED3D_RS_FOGENABLE,
207 WINED3D_RS_FOGEND,
208 WINED3D_RS_FOGSTART,
209 WINED3D_RS_FOGTABLEMODE,
210 WINED3D_RS_FOGVERTEXMODE,
211 WINED3D_RS_INDEXEDVERTEXBLENDENABLE,
212 WINED3D_RS_LIGHTING,
213 WINED3D_RS_LOCALVIEWER,
214 WINED3D_RS_MAXTESSELLATIONLEVEL,
215 WINED3D_RS_MINTESSELLATIONLEVEL,
216 WINED3D_RS_MULTISAMPLEANTIALIAS,
217 WINED3D_RS_MULTISAMPLEMASK,
218 WINED3D_RS_NORMALDEGREE,
219 WINED3D_RS_NORMALIZENORMALS,
220 WINED3D_RS_PATCHEDGESTYLE,
221 WINED3D_RS_POINTSCALE_A,
222 WINED3D_RS_POINTSCALE_B,
223 WINED3D_RS_POINTSCALE_C,
224 WINED3D_RS_POINTSCALEENABLE,
225 WINED3D_RS_POINTSIZE,
226 WINED3D_RS_POINTSIZE_MAX,
227 WINED3D_RS_POINTSIZE_MIN,
228 WINED3D_RS_POINTSPRITEENABLE,
229 WINED3D_RS_POSITIONDEGREE,
230 WINED3D_RS_RANGEFOGENABLE,
231 WINED3D_RS_SHADEMODE,
232 WINED3D_RS_SPECULARENABLE,
233 WINED3D_RS_SPECULARMATERIALSOURCE,
234 WINED3D_RS_TWEENFACTOR,
235 WINED3D_RS_VERTEXBLEND,
238 static const DWORD vertex_states_texture[] =
240 WINED3D_TSS_TEXCOORD_INDEX,
241 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,
244 static const DWORD vertex_states_sampler[] =
246 WINED3D_SAMP_DMAP_OFFSET,
249 static inline void stateblock_set_all_bits(uint32_t *map, UINT map_size)
251 DWORD mask = (1u << (map_size & 0x1f)) - 1;
252 memset(map, 0xff, (map_size >> 5) * sizeof(*map));
253 if (mask) map[map_size >> 5] = mask;
256 /* Set all members of a stateblock savedstate to the given value */
257 static void stateblock_savedstates_set_all(struct wined3d_saved_states *states, DWORD vs_consts, DWORD ps_consts)
259 unsigned int i;
261 states->indices = 1;
262 states->material = 1;
263 states->viewport = 1;
264 states->vertexDecl = 1;
265 states->pixelShader = 1;
266 states->vertexShader = 1;
267 states->scissorRect = 1;
268 states->alpha_to_coverage = 1;
269 states->lights = 1;
270 states->transforms = 1;
272 states->streamSource = 0xffff;
273 states->streamFreq = 0xffff;
274 states->textures = 0xfffff;
275 stateblock_set_all_bits(states->transform, WINED3D_HIGHEST_TRANSFORM_STATE + 1);
276 stateblock_set_all_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
277 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
278 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3ffe;
279 states->clipplane = wined3d_mask_from_size(WINED3D_MAX_CLIP_DISTANCES);
280 states->pixelShaderConstantsB = 0xffff;
281 states->pixelShaderConstantsI = 0xffff;
282 states->vertexShaderConstantsB = 0xffff;
283 states->vertexShaderConstantsI = 0xffff;
285 memset(states->ps_consts_f, 0xffu, sizeof(states->ps_consts_f));
286 memset(states->vs_consts_f, 0xffu, sizeof(states->vs_consts_f));
289 static void stateblock_savedstates_set_pixel(struct wined3d_saved_states *states, const DWORD num_constants)
291 DWORD texture_mask = 0;
292 WORD sampler_mask = 0;
293 unsigned int i;
295 states->pixelShader = 1;
297 for (i = 0; i < ARRAY_SIZE(pixel_states_render); ++i)
299 DWORD rs = pixel_states_render[i];
300 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
303 for (i = 0; i < ARRAY_SIZE(pixel_states_texture); ++i)
304 texture_mask |= 1u << pixel_states_texture[i];
305 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i) states->textureState[i] = texture_mask;
306 for (i = 0; i < ARRAY_SIZE(pixel_states_sampler); ++i)
307 sampler_mask |= 1u << pixel_states_sampler[i];
308 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
309 states->pixelShaderConstantsB = 0xffff;
310 states->pixelShaderConstantsI = 0xffff;
312 memset(states->ps_consts_f, 0xffu, sizeof(states->ps_consts_f));
315 static void stateblock_savedstates_set_vertex(struct wined3d_saved_states *states, const DWORD num_constants)
317 DWORD texture_mask = 0;
318 WORD sampler_mask = 0;
319 unsigned int i;
321 states->vertexDecl = 1;
322 states->vertexShader = 1;
323 states->alpha_to_coverage = 1;
324 states->lights = 1;
326 for (i = 0; i < ARRAY_SIZE(vertex_states_render); ++i)
328 DWORD rs = vertex_states_render[i];
329 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
332 for (i = 0; i < ARRAY_SIZE(vertex_states_texture); ++i)
333 texture_mask |= 1u << vertex_states_texture[i];
334 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i) states->textureState[i] = texture_mask;
335 for (i = 0; i < ARRAY_SIZE(vertex_states_sampler); ++i)
336 sampler_mask |= 1u << vertex_states_sampler[i];
337 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
338 states->vertexShaderConstantsB = 0xffff;
339 states->vertexShaderConstantsI = 0xffff;
341 memset(states->vs_consts_f, 0xffu, sizeof(states->vs_consts_f));
344 void CDECL wined3d_stateblock_init_contained_states(struct wined3d_stateblock *stateblock)
346 unsigned int i, j;
348 for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
350 DWORD map = stateblock->changed.renderState[i];
351 for (j = 0; map; map >>= 1, ++j)
353 if (!(map & 1)) continue;
355 stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
356 ++stateblock->num_contained_render_states;
360 for (i = 0; i <= WINED3D_HIGHEST_TRANSFORM_STATE >> 5; ++i)
362 DWORD map = stateblock->changed.transform[i];
363 for (j = 0; map; map >>= 1, ++j)
365 if (!(map & 1)) continue;
367 stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
368 ++stateblock->num_contained_transform_states;
372 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
374 DWORD map = stateblock->changed.textureState[i];
376 for(j = 0; map; map >>= 1, ++j)
378 if (!(map & 1)) continue;
380 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
381 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
382 ++stateblock->num_contained_tss_states;
386 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
388 DWORD map = stateblock->changed.samplerState[i];
390 for (j = 0; map; map >>= 1, ++j)
392 if (!(map & 1)) continue;
394 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
395 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
396 ++stateblock->num_contained_sampler_states;
401 static void stateblock_init_lights(struct wined3d_stateblock *stateblock, const struct rb_tree *src_tree)
403 struct rb_tree *dst_tree = &stateblock->stateblock_state.light_state->lights_tree;
404 struct wined3d_light_info *src_light;
406 RB_FOR_EACH_ENTRY(src_light, src_tree, struct wined3d_light_info, entry)
408 struct wined3d_light_info *dst_light = heap_alloc(sizeof(*dst_light));
410 *dst_light = *src_light;
411 rb_put(dst_tree, (void *)(ULONG_PTR)dst_light->OriginalIndex, &dst_light->entry);
412 dst_light->changed = true;
413 list_add_tail(&stateblock->changed.changed_lights, &dst_light->changed_entry);
417 ULONG CDECL wined3d_stateblock_incref(struct wined3d_stateblock *stateblock)
419 unsigned int refcount = InterlockedIncrement(&stateblock->ref);
421 TRACE("%p increasing refcount to %u.\n", stateblock, refcount);
423 return refcount;
426 void state_unbind_resources(struct wined3d_state *state)
428 struct wined3d_unordered_access_view *uav;
429 struct wined3d_shader_resource_view *srv;
430 struct wined3d_vertex_declaration *decl;
431 struct wined3d_blend_state *blend_state;
432 struct wined3d_rendertarget_view *rtv;
433 struct wined3d_sampler *sampler;
434 struct wined3d_texture *texture;
435 struct wined3d_buffer *buffer;
436 struct wined3d_shader *shader;
437 unsigned int i, j;
439 if ((decl = state->vertex_declaration))
441 state->vertex_declaration = NULL;
442 wined3d_vertex_declaration_decref(decl);
445 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
447 if ((texture = state->textures[i]))
449 state->textures[i] = NULL;
450 wined3d_texture_decref(texture);
454 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
456 if ((buffer = state->stream_output[i].buffer))
458 state->stream_output[i].buffer = NULL;
459 wined3d_buffer_decref(buffer);
463 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
465 if ((buffer = state->streams[i].buffer))
467 state->streams[i].buffer = NULL;
468 wined3d_buffer_decref(buffer);
472 if ((buffer = state->index_buffer))
474 state->index_buffer = NULL;
475 wined3d_buffer_decref(buffer);
478 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
480 if ((shader = state->shader[i]))
482 state->shader[i] = NULL;
483 wined3d_shader_decref(shader);
486 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
488 if ((buffer = state->cb[i][j].buffer))
490 state->cb[i][j].buffer = NULL;
491 wined3d_buffer_decref(buffer);
495 for (j = 0; j < MAX_SAMPLER_OBJECTS; ++j)
497 if ((sampler = state->sampler[i][j]))
499 state->sampler[i][j] = NULL;
500 wined3d_sampler_decref(sampler);
504 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
506 if ((srv = state->shader_resource_view[i][j]))
508 state->shader_resource_view[i][j] = NULL;
509 wined3d_srv_bind_count_dec(srv);
510 wined3d_shader_resource_view_decref(srv);
515 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
517 for (j = 0; j < MAX_UNORDERED_ACCESS_VIEWS; ++j)
519 if ((uav = state->unordered_access_view[i][j]))
521 state->unordered_access_view[i][j] = NULL;
522 wined3d_unordered_access_view_decref(uav);
527 if ((blend_state = state->blend_state))
529 state->blend_state = NULL;
530 wined3d_blend_state_decref(blend_state);
533 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
535 if ((rtv = state->fb.render_targets[i]))
537 state->fb.render_targets[i] = NULL;
538 wined3d_rendertarget_view_decref(rtv);
542 if ((rtv = state->fb.depth_stencil))
544 state->fb.depth_stencil = NULL;
545 wined3d_rendertarget_view_decref(rtv);
549 static void wined3d_stateblock_state_cleanup(struct wined3d_stateblock_state *state)
551 struct wined3d_light_info *light, *cursor;
552 struct wined3d_vertex_declaration *decl;
553 struct wined3d_texture *texture;
554 struct wined3d_buffer *buffer;
555 struct wined3d_shader *shader;
556 unsigned int i;
558 if ((decl = state->vertex_declaration))
560 state->vertex_declaration = NULL;
561 wined3d_vertex_declaration_decref(decl);
564 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
566 if ((buffer = state->streams[i].buffer))
568 state->streams[i].buffer = NULL;
569 wined3d_buffer_decref(buffer);
573 if ((buffer = state->index_buffer))
575 state->index_buffer = NULL;
576 wined3d_buffer_decref(buffer);
579 if ((shader = state->vs))
581 state->vs = NULL;
582 wined3d_shader_decref(shader);
585 if ((shader = state->ps))
587 state->ps = NULL;
588 wined3d_shader_decref(shader);
591 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
593 if ((texture = state->textures[i]))
595 state->textures[i] = NULL;
596 wined3d_texture_decref(texture);
600 RB_FOR_EACH_ENTRY_DESTRUCTOR(light, cursor, &state->light_state->lights_tree, struct wined3d_light_info, entry)
602 if (light->changed)
603 list_remove(&light->changed_entry);
604 rb_remove(&state->light_state->lights_tree, &light->entry);
605 heap_free(light);
609 void state_cleanup(struct wined3d_state *state)
611 struct wined3d_light_info *light, *cursor;
612 unsigned int i;
614 if (!(state->flags & WINED3D_STATE_NO_REF))
615 state_unbind_resources(state);
617 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
619 state->light_state.lights[i] = NULL;
622 RB_FOR_EACH_ENTRY_DESTRUCTOR(light, cursor, &state->light_state.lights_tree, struct wined3d_light_info, entry)
624 if (light->changed)
625 list_remove(&light->changed_entry);
626 rb_remove(&state->light_state.lights_tree, &light->entry);
627 heap_free(light);
631 ULONG CDECL wined3d_stateblock_decref(struct wined3d_stateblock *stateblock)
633 unsigned int refcount = InterlockedDecrement(&stateblock->ref);
635 TRACE("%p decreasing refcount to %u\n", stateblock, refcount);
637 if (!refcount)
639 wined3d_mutex_lock();
640 wined3d_stateblock_state_cleanup(&stateblock->stateblock_state);
641 heap_free(stateblock);
642 wined3d_mutex_unlock();
645 return refcount;
648 struct wined3d_light_info *wined3d_light_state_get_light(const struct wined3d_light_state *state, unsigned int idx)
650 struct rb_entry *entry;
652 if (!(entry = rb_get(&state->lights_tree, (void *)(ULONG_PTR)idx)))
653 return NULL;
655 return RB_ENTRY_VALUE(entry, struct wined3d_light_info, entry);
658 static void set_light_changed(struct wined3d_stateblock *stateblock, struct wined3d_light_info *light_info)
660 if (!light_info->changed)
662 list_add_tail(&stateblock->changed.changed_lights, &light_info->changed_entry);
663 light_info->changed = true;
665 stateblock->changed.lights = 1;
668 HRESULT wined3d_light_state_set_light(struct wined3d_light_state *state, DWORD light_idx,
669 const struct wined3d_light *params, struct wined3d_light_info **light_info)
671 struct wined3d_light_info *object;
673 if (!(object = wined3d_light_state_get_light(state, light_idx)))
675 TRACE("Adding new light.\n");
676 if (!(object = heap_alloc_zero(sizeof(*object))))
678 ERR("Failed to allocate light info.\n");
679 return E_OUTOFMEMORY;
682 object->glIndex = -1;
683 object->OriginalIndex = light_idx;
684 rb_put(&state->lights_tree, (void *)(ULONG_PTR)light_idx, &object->entry);
687 object->OriginalParms = *params;
689 *light_info = object;
690 return WINED3D_OK;
693 bool wined3d_light_state_enable_light(struct wined3d_light_state *state, const struct wined3d_d3d_info *d3d_info,
694 struct wined3d_light_info *light_info, BOOL enable)
696 unsigned int light_count, i;
698 if (!(light_info->enabled = enable))
700 if (light_info->glIndex == -1)
702 TRACE("Light already disabled, nothing to do.\n");
703 return false;
706 state->lights[light_info->glIndex] = NULL;
707 light_info->glIndex = -1;
708 return true;
711 if (light_info->glIndex != -1)
713 TRACE("Light already enabled, nothing to do.\n");
714 return false;
717 /* Find a free light. */
718 light_count = d3d_info->limits.active_light_count;
719 for (i = 0; i < light_count; ++i)
721 if (state->lights[i])
722 continue;
724 state->lights[i] = light_info;
725 light_info->glIndex = i;
726 return true;
729 /* Our tests show that Windows returns D3D_OK in this situation, even with
730 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices.
731 * This is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns
732 * TRUE * as well for those lights.
734 * TODO: Test how this affects rendering. */
735 WARN("Too many concurrently active lights.\n");
736 return false;
739 static void wined3d_state_record_lights(struct wined3d_light_state *dst_state,
740 const struct wined3d_light_state *src_state)
742 const struct wined3d_light_info *src;
743 struct wined3d_light_info *dst;
745 /* Lights... For a recorded state block, we just had a chain of actions
746 * to perform, so we need to walk that chain and update any actions which
747 * differ. */
748 RB_FOR_EACH_ENTRY(dst, &dst_state->lights_tree, struct wined3d_light_info, entry)
750 if ((src = wined3d_light_state_get_light(src_state, dst->OriginalIndex)))
752 dst->OriginalParms = src->OriginalParms;
754 if (src->glIndex == -1 && dst->glIndex != -1)
756 /* Light disabled. */
757 dst_state->lights[dst->glIndex] = NULL;
759 else if (src->glIndex != -1 && dst->glIndex == -1)
761 /* Light enabled. */
762 dst_state->lights[src->glIndex] = dst;
764 dst->glIndex = src->glIndex;
766 else
768 /* This can happen if the light was originally created as a
769 * default light for SetLightEnable() while recording. */
770 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
771 dst->OriginalIndex, dst_state, src_state);
773 dst->OriginalParms = WINED3D_default_light;
774 if (dst->glIndex != -1)
776 dst_state->lights[dst->glIndex] = NULL;
777 dst->glIndex = -1;
783 void CDECL wined3d_stateblock_capture(struct wined3d_stateblock *stateblock,
784 const struct wined3d_stateblock *device_state)
786 const struct wined3d_stateblock_state *state = &device_state->stateblock_state;
787 struct wined3d_range range;
788 unsigned int i, start;
789 uint32_t map;
791 TRACE("stateblock %p, device_state %p.\n", stateblock, device_state);
793 if (stateblock->changed.vertexShader && stateblock->stateblock_state.vs != state->vs)
795 TRACE("Updating vertex shader from %p to %p.\n", stateblock->stateblock_state.vs, state->vs);
797 if (state->vs)
798 wined3d_shader_incref(state->vs);
799 if (stateblock->stateblock_state.vs)
800 wined3d_shader_decref(stateblock->stateblock_state.vs);
801 stateblock->stateblock_state.vs = state->vs;
804 for (start = 0; ; start = range.offset + range.size)
806 if (!wined3d_bitmap_get_range(stateblock->changed.vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
807 break;
809 memcpy(&stateblock->stateblock_state.vs_consts_f[range.offset], &state->vs_consts_f[range.offset],
810 sizeof(*state->vs_consts_f) * range.size);
812 map = stateblock->changed.vertexShaderConstantsI;
813 for (start = 0; ; start = range.offset + range.size)
815 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
816 break;
818 memcpy(&stateblock->stateblock_state.vs_consts_i[range.offset], &state->vs_consts_i[range.offset],
819 sizeof(*state->vs_consts_i) * range.size);
821 map = stateblock->changed.vertexShaderConstantsB;
822 for (start = 0; ; start = range.offset + range.size)
824 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
825 break;
827 memcpy(&stateblock->stateblock_state.vs_consts_b[range.offset], &state->vs_consts_b[range.offset],
828 sizeof(*state->vs_consts_b) * range.size);
831 for (start = 0; ; start = range.offset + range.size)
833 if (!wined3d_bitmap_get_range(stateblock->changed.ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
834 break;
836 memcpy(&stateblock->stateblock_state.ps_consts_f[range.offset], &state->ps_consts_f[range.offset],
837 sizeof(*state->ps_consts_f) * range.size);
839 map = stateblock->changed.pixelShaderConstantsI;
840 for (start = 0; ; start = range.offset + range.size)
842 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
843 break;
845 memcpy(&stateblock->stateblock_state.ps_consts_i[range.offset], &state->ps_consts_i[range.offset],
846 sizeof(*state->ps_consts_i) * range.size);
848 map = stateblock->changed.pixelShaderConstantsB;
849 for (start = 0; ; start = range.offset + range.size)
851 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
852 break;
854 memcpy(&stateblock->stateblock_state.ps_consts_b[range.offset], &state->ps_consts_b[range.offset],
855 sizeof(*state->ps_consts_b) * range.size);
858 if (stateblock->changed.transforms)
860 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
862 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
864 TRACE("Updating transform %#x.\n", transform);
866 stateblock->stateblock_state.transforms[transform] = state->transforms[transform];
870 if (stateblock->changed.indices
871 && ((stateblock->stateblock_state.index_buffer != state->index_buffer)
872 || (stateblock->stateblock_state.base_vertex_index != state->base_vertex_index)
873 || (stateblock->stateblock_state.index_format != state->index_format)))
875 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
876 state->index_buffer, state->base_vertex_index);
878 if (state->index_buffer)
879 wined3d_buffer_incref(state->index_buffer);
880 if (stateblock->stateblock_state.index_buffer)
881 wined3d_buffer_decref(stateblock->stateblock_state.index_buffer);
882 stateblock->stateblock_state.index_buffer = state->index_buffer;
883 stateblock->stateblock_state.base_vertex_index = state->base_vertex_index;
884 stateblock->stateblock_state.index_format = state->index_format;
887 if (stateblock->changed.vertexDecl && stateblock->stateblock_state.vertex_declaration != state->vertex_declaration)
889 TRACE("Updating vertex declaration from %p to %p.\n",
890 stateblock->stateblock_state.vertex_declaration, state->vertex_declaration);
892 if (state->vertex_declaration)
893 wined3d_vertex_declaration_incref(state->vertex_declaration);
894 if (stateblock->stateblock_state.vertex_declaration)
895 wined3d_vertex_declaration_decref(stateblock->stateblock_state.vertex_declaration);
896 stateblock->stateblock_state.vertex_declaration = state->vertex_declaration;
899 if (stateblock->changed.material
900 && memcmp(&state->material, &stateblock->stateblock_state.material,
901 sizeof(stateblock->stateblock_state.material)))
903 TRACE("Updating material.\n");
905 stateblock->stateblock_state.material = state->material;
908 if (stateblock->changed.viewport
909 && memcmp(&state->viewport, &stateblock->stateblock_state.viewport, sizeof(state->viewport)))
911 TRACE("Updating viewport.\n");
913 stateblock->stateblock_state.viewport = state->viewport;
916 if (stateblock->changed.scissorRect
917 && memcmp(&state->scissor_rect, &stateblock->stateblock_state.scissor_rect, sizeof(state->scissor_rect)))
919 TRACE("Updating scissor rect.\n");
921 stateblock->stateblock_state.scissor_rect = state->scissor_rect;
924 map = stateblock->changed.streamSource;
925 while (map)
927 i = wined3d_bit_scan(&map);
929 if (stateblock->stateblock_state.streams[i].stride != state->streams[i].stride
930 || stateblock->stateblock_state.streams[i].offset != state->streams[i].offset
931 || stateblock->stateblock_state.streams[i].buffer != state->streams[i].buffer)
933 TRACE("stateblock %p, stream source %u, buffer %p, stride %u, offset %u.\n",
934 stateblock, i, state->streams[i].buffer, state->streams[i].stride,
935 state->streams[i].offset);
937 stateblock->stateblock_state.streams[i].stride = state->streams[i].stride;
938 if (stateblock->changed.store_stream_offset)
939 stateblock->stateblock_state.streams[i].offset = state->streams[i].offset;
941 if (state->streams[i].buffer)
942 wined3d_buffer_incref(state->streams[i].buffer);
943 if (stateblock->stateblock_state.streams[i].buffer)
944 wined3d_buffer_decref(stateblock->stateblock_state.streams[i].buffer);
945 stateblock->stateblock_state.streams[i].buffer = state->streams[i].buffer;
949 map = stateblock->changed.streamFreq;
950 while (map)
952 i = wined3d_bit_scan(&map);
954 if (stateblock->stateblock_state.streams[i].frequency != state->streams[i].frequency
955 || stateblock->stateblock_state.streams[i].flags != state->streams[i].flags)
957 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
958 i, state->streams[i].frequency, state->streams[i].flags);
960 stateblock->stateblock_state.streams[i].frequency = state->streams[i].frequency;
961 stateblock->stateblock_state.streams[i].flags = state->streams[i].flags;
965 map = stateblock->changed.clipplane;
966 while (map)
968 i = wined3d_bit_scan(&map);
970 if (memcmp(&stateblock->stateblock_state.clip_planes[i], &state->clip_planes[i], sizeof(state->clip_planes[i])))
972 TRACE("Updating clipplane %u.\n", i);
973 stateblock->stateblock_state.clip_planes[i] = state->clip_planes[i];
977 /* Render */
978 for (i = 0; i < stateblock->num_contained_render_states; ++i)
980 enum wined3d_render_state rs = stateblock->contained_render_states[i];
982 TRACE("Updating render state %#x to %u.\n", rs, state->rs[rs]);
984 stateblock->stateblock_state.rs[rs] = state->rs[rs];
987 /* Texture states */
988 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
990 unsigned int stage = stateblock->contained_tss_states[i].stage;
991 unsigned int texture_state = stateblock->contained_tss_states[i].state;
993 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage, texture_state,
994 state->texture_states[stage][texture_state],
995 stateblock->stateblock_state.texture_states[stage][texture_state]);
997 stateblock->stateblock_state.texture_states[stage][texture_state] = state->texture_states[stage][texture_state];
1000 /* Samplers */
1001 map = stateblock->changed.textures;
1002 while (map)
1004 i = wined3d_bit_scan(&map);
1006 TRACE("Updating texture %u to %p (was %p).\n",
1007 i, state->textures[i], stateblock->stateblock_state.textures[i]);
1009 if (state->textures[i])
1010 wined3d_texture_incref(state->textures[i]);
1011 if (stateblock->stateblock_state.textures[i])
1012 wined3d_texture_decref(stateblock->stateblock_state.textures[i]);
1013 stateblock->stateblock_state.textures[i] = state->textures[i];
1016 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
1018 unsigned int stage = stateblock->contained_sampler_states[i].stage;
1019 unsigned int sampler_state = stateblock->contained_sampler_states[i].state;
1021 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage, sampler_state,
1022 state->sampler_states[stage][sampler_state],
1023 stateblock->stateblock_state.sampler_states[stage][sampler_state]);
1025 stateblock->stateblock_state.sampler_states[stage][sampler_state] = state->sampler_states[stage][sampler_state];
1028 if (stateblock->changed.pixelShader && stateblock->stateblock_state.ps != state->ps)
1030 if (state->ps)
1031 wined3d_shader_incref(state->ps);
1032 if (stateblock->stateblock_state.ps)
1033 wined3d_shader_decref(stateblock->stateblock_state.ps);
1034 stateblock->stateblock_state.ps = state->ps;
1037 if (stateblock->changed.lights)
1038 wined3d_state_record_lights(stateblock->stateblock_state.light_state, state->light_state);
1040 if (stateblock->changed.alpha_to_coverage)
1041 stateblock->stateblock_state.alpha_to_coverage = state->alpha_to_coverage;
1043 TRACE("Capture done.\n");
1046 void CDECL wined3d_stateblock_apply(const struct wined3d_stateblock *stateblock,
1047 struct wined3d_stateblock *device_state)
1049 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
1050 struct wined3d_range range;
1051 unsigned int i, start;
1052 uint32_t map;
1054 TRACE("stateblock %p, device_state %p.\n", stateblock, device_state);
1056 if (stateblock->changed.vertexShader)
1057 wined3d_stateblock_set_vertex_shader(device_state, state->vs);
1058 if (stateblock->changed.pixelShader)
1059 wined3d_stateblock_set_pixel_shader(device_state, state->ps);
1061 for (start = 0; ; start = range.offset + range.size)
1063 if (!wined3d_bitmap_get_range(stateblock->changed.vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
1064 break;
1065 wined3d_stateblock_set_vs_consts_f(device_state, range.offset, range.size, &state->vs_consts_f[range.offset]);
1067 map = stateblock->changed.vertexShaderConstantsI;
1068 for (start = 0; ; start = range.offset + range.size)
1070 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
1071 break;
1072 wined3d_stateblock_set_vs_consts_i(device_state, range.offset, range.size, &state->vs_consts_i[range.offset]);
1074 map = stateblock->changed.vertexShaderConstantsB;
1075 for (start = 0; ; start = range.offset + range.size)
1077 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
1078 break;
1079 wined3d_stateblock_set_vs_consts_b(device_state, range.offset, range.size, &state->vs_consts_b[range.offset]);
1082 for (start = 0; ; start = range.offset + range.size)
1084 if (!wined3d_bitmap_get_range(stateblock->changed.ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
1085 break;
1086 wined3d_stateblock_set_ps_consts_f(device_state, range.offset, range.size, &state->ps_consts_f[range.offset]);
1088 map = stateblock->changed.pixelShaderConstantsI;
1089 for (start = 0; ; start = range.offset + range.size)
1091 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
1092 break;
1093 wined3d_stateblock_set_ps_consts_i(device_state, range.offset, range.size, &state->ps_consts_i[range.offset]);
1095 map = stateblock->changed.pixelShaderConstantsB;
1096 for (start = 0; ; start = range.offset + range.size)
1098 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
1099 break;
1100 wined3d_stateblock_set_ps_consts_b(device_state, range.offset, range.size, &state->ps_consts_b[range.offset]);
1103 if (stateblock->changed.transforms)
1105 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
1107 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
1109 wined3d_stateblock_set_transform(device_state, transform, &state->transforms[transform]);
1113 if (stateblock->changed.lights)
1115 const struct wined3d_light_info *light;
1117 LIST_FOR_EACH_ENTRY(light, &stateblock->changed.changed_lights, struct wined3d_light_info, changed_entry)
1119 wined3d_stateblock_set_light(device_state, light->OriginalIndex, &light->OriginalParms);
1120 wined3d_stateblock_set_light_enable(device_state, light->OriginalIndex, light->glIndex != -1);
1124 if (stateblock->changed.alpha_to_coverage)
1126 device_state->stateblock_state.alpha_to_coverage = state->alpha_to_coverage;
1127 device_state->changed.alpha_to_coverage = 1;
1130 /* Render states. */
1131 for (i = 0; i < stateblock->num_contained_render_states; ++i)
1133 enum wined3d_render_state rs = stateblock->contained_render_states[i];
1135 wined3d_stateblock_set_render_state(device_state, rs, state->rs[rs]);
1138 /* Texture states. */
1139 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
1141 DWORD stage = stateblock->contained_tss_states[i].stage;
1142 DWORD texture_state = stateblock->contained_tss_states[i].state;
1144 wined3d_stateblock_set_texture_stage_state(device_state, stage, texture_state,
1145 state->texture_states[stage][texture_state]);
1148 /* Sampler states. */
1149 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
1151 DWORD stage = stateblock->contained_sampler_states[i].stage;
1152 DWORD sampler_state = stateblock->contained_sampler_states[i].state;
1154 wined3d_stateblock_set_sampler_state(device_state, stage, sampler_state,
1155 state->sampler_states[stage][sampler_state]);
1158 if (stateblock->changed.indices)
1160 wined3d_stateblock_set_index_buffer(device_state, state->index_buffer, state->index_format);
1161 wined3d_stateblock_set_base_vertex_index(device_state, state->base_vertex_index);
1164 if (stateblock->changed.vertexDecl && state->vertex_declaration)
1165 wined3d_stateblock_set_vertex_declaration(device_state, state->vertex_declaration);
1167 if (stateblock->changed.material)
1168 wined3d_stateblock_set_material(device_state, &state->material);
1170 if (stateblock->changed.viewport)
1171 wined3d_stateblock_set_viewport(device_state, &state->viewport);
1173 if (stateblock->changed.scissorRect)
1174 wined3d_stateblock_set_scissor_rect(device_state, &state->scissor_rect);
1176 map = stateblock->changed.streamSource;
1177 while (map)
1179 i = wined3d_bit_scan(&map);
1180 wined3d_stateblock_set_stream_source(device_state, i, state->streams[i].buffer,
1181 state->streams[i].offset, state->streams[i].stride);
1184 map = stateblock->changed.streamFreq;
1185 while (map)
1187 i = wined3d_bit_scan(&map);
1188 wined3d_stateblock_set_stream_source_freq(device_state, i,
1189 state->streams[i].frequency | state->streams[i].flags);
1192 map = stateblock->changed.textures;
1193 while (map)
1195 i = wined3d_bit_scan(&map);
1196 wined3d_stateblock_set_texture(device_state, i, state->textures[i]);
1199 map = stateblock->changed.clipplane;
1200 while (map)
1202 i = wined3d_bit_scan(&map);
1203 wined3d_stateblock_set_clip_plane(device_state, i, &state->clip_planes[i]);
1206 TRACE("Applied stateblock %p.\n", stateblock);
1209 void CDECL wined3d_stateblock_set_vertex_shader(struct wined3d_stateblock *stateblock, struct wined3d_shader *shader)
1211 TRACE("stateblock %p, shader %p.\n", stateblock, shader);
1213 if (shader)
1214 wined3d_shader_incref(shader);
1215 if (stateblock->stateblock_state.vs)
1216 wined3d_shader_decref(stateblock->stateblock_state.vs);
1217 stateblock->stateblock_state.vs = shader;
1218 stateblock->changed.vertexShader = TRUE;
1221 static void wined3d_bitmap_set_bits(uint32_t *bitmap, unsigned int start, unsigned int count)
1223 const unsigned int word_bit_count = sizeof(*bitmap) * CHAR_BIT;
1224 const unsigned int shift = start % word_bit_count;
1225 uint32_t mask, last_mask;
1226 unsigned int mask_size;
1228 bitmap += start / word_bit_count;
1229 mask = ~0u << shift;
1230 mask_size = word_bit_count - shift;
1231 last_mask = (1u << (start + count) % word_bit_count) - 1;
1232 if (mask_size <= count)
1234 *bitmap |= mask;
1235 ++bitmap;
1236 count -= mask_size;
1237 mask = ~0u;
1239 if (count >= word_bit_count)
1241 memset(bitmap, 0xffu, count / word_bit_count * sizeof(*bitmap));
1242 bitmap += count / word_bit_count;
1243 count = count % word_bit_count;
1245 if (count)
1246 *bitmap |= mask & last_mask;
1249 HRESULT CDECL wined3d_stateblock_set_vs_consts_f(struct wined3d_stateblock *stateblock,
1250 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
1252 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
1254 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1255 stateblock, start_idx, count, constants);
1257 if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.vs_uniform_count))
1258 return WINED3DERR_INVALIDCALL;
1260 memcpy(&stateblock->stateblock_state.vs_consts_f[start_idx], constants, count * sizeof(*constants));
1261 wined3d_bitmap_set_bits(stateblock->changed.vs_consts_f, start_idx, count);
1262 return WINED3D_OK;
1265 HRESULT CDECL wined3d_stateblock_set_vs_consts_i(struct wined3d_stateblock *stateblock,
1266 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
1268 unsigned int i;
1270 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1271 stateblock, start_idx, count, constants);
1273 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1274 return WINED3DERR_INVALIDCALL;
1276 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1277 count = WINED3D_MAX_CONSTS_I - start_idx;
1279 memcpy(&stateblock->stateblock_state.vs_consts_i[start_idx], constants, count * sizeof(*constants));
1280 for (i = start_idx; i < count + start_idx; ++i)
1281 stateblock->changed.vertexShaderConstantsI |= (1u << i);
1282 return WINED3D_OK;
1285 HRESULT CDECL wined3d_stateblock_set_vs_consts_b(struct wined3d_stateblock *stateblock,
1286 unsigned int start_idx, unsigned int count, const BOOL *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_B)
1294 return WINED3DERR_INVALIDCALL;
1296 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1297 count = WINED3D_MAX_CONSTS_B - start_idx;
1299 memcpy(&stateblock->stateblock_state.vs_consts_b[start_idx], constants, count * sizeof(*constants));
1300 for (i = start_idx; i < count + start_idx; ++i)
1301 stateblock->changed.vertexShaderConstantsB |= (1u << i);
1302 return WINED3D_OK;
1305 HRESULT CDECL wined3d_stateblock_get_vs_consts_f(struct wined3d_stateblock *stateblock,
1306 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
1308 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
1310 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", stateblock, start_idx, count, constants);
1312 if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.vs_uniform_count))
1313 return WINED3DERR_INVALIDCALL;
1315 memcpy(constants, &stateblock->stateblock_state.vs_consts_f[start_idx], count * sizeof(*constants));
1316 return WINED3D_OK;
1319 HRESULT CDECL wined3d_stateblock_get_vs_consts_i(struct wined3d_stateblock *stateblock,
1320 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
1322 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", stateblock, start_idx, count, constants);
1324 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1325 return WINED3DERR_INVALIDCALL;
1327 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1328 count = WINED3D_MAX_CONSTS_I - start_idx;
1330 memcpy(constants, &stateblock->stateblock_state.vs_consts_i[start_idx], count * sizeof(*constants));
1331 return WINED3D_OK;
1334 HRESULT CDECL wined3d_stateblock_get_vs_consts_b(struct wined3d_stateblock *stateblock,
1335 unsigned int start_idx, unsigned int count, BOOL *constants)
1337 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", stateblock, start_idx, count, constants);
1339 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1340 return WINED3DERR_INVALIDCALL;
1342 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1343 count = WINED3D_MAX_CONSTS_B - start_idx;
1345 memcpy(constants, &stateblock->stateblock_state.vs_consts_b[start_idx], count * sizeof(*constants));
1346 return WINED3D_OK;
1349 void CDECL wined3d_stateblock_set_pixel_shader(struct wined3d_stateblock *stateblock, struct wined3d_shader *shader)
1351 TRACE("stateblock %p, shader %p.\n", stateblock, shader);
1353 if (shader)
1354 wined3d_shader_incref(shader);
1355 if (stateblock->stateblock_state.ps)
1356 wined3d_shader_decref(stateblock->stateblock_state.ps);
1357 stateblock->stateblock_state.ps = shader;
1358 stateblock->changed.pixelShader = TRUE;
1361 HRESULT CDECL wined3d_stateblock_set_ps_consts_f(struct wined3d_stateblock *stateblock,
1362 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
1364 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
1366 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1367 stateblock, start_idx, count, constants);
1369 if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.ps_uniform_count))
1370 return WINED3DERR_INVALIDCALL;
1372 memcpy(&stateblock->stateblock_state.ps_consts_f[start_idx], constants, count * sizeof(*constants));
1373 wined3d_bitmap_set_bits(stateblock->changed.ps_consts_f, start_idx, count);
1374 return WINED3D_OK;
1377 HRESULT CDECL wined3d_stateblock_set_ps_consts_i(struct wined3d_stateblock *stateblock,
1378 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
1380 unsigned int i;
1382 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1383 stateblock, start_idx, count, constants);
1385 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1386 return WINED3DERR_INVALIDCALL;
1388 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1389 count = WINED3D_MAX_CONSTS_I - start_idx;
1391 memcpy(&stateblock->stateblock_state.ps_consts_i[start_idx], constants, count * sizeof(*constants));
1392 for (i = start_idx; i < count + start_idx; ++i)
1393 stateblock->changed.pixelShaderConstantsI |= (1u << i);
1394 return WINED3D_OK;
1397 HRESULT CDECL wined3d_stateblock_set_ps_consts_b(struct wined3d_stateblock *stateblock,
1398 unsigned int start_idx, unsigned int count, const BOOL *constants)
1400 unsigned int i;
1402 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
1403 stateblock, start_idx, count, constants);
1405 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1406 return WINED3DERR_INVALIDCALL;
1408 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1409 count = WINED3D_MAX_CONSTS_B - start_idx;
1411 memcpy(&stateblock->stateblock_state.ps_consts_b[start_idx], constants, count * sizeof(*constants));
1412 for (i = start_idx; i < count + start_idx; ++i)
1413 stateblock->changed.pixelShaderConstantsB |= (1u << i);
1414 return WINED3D_OK;
1417 HRESULT CDECL wined3d_stateblock_get_ps_consts_f(struct wined3d_stateblock *stateblock,
1418 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
1420 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", stateblock, start_idx, count, constants);
1422 if (!constants || !wined3d_bound_range(start_idx, count, WINED3D_MAX_PS_CONSTS_F))
1423 return WINED3DERR_INVALIDCALL;
1425 memcpy(constants, &stateblock->stateblock_state.ps_consts_f[start_idx], count * sizeof(*constants));
1426 return WINED3D_OK;
1429 HRESULT CDECL wined3d_stateblock_get_ps_consts_i(struct wined3d_stateblock *stateblock,
1430 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
1432 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", stateblock, start_idx, count, constants);
1434 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
1435 return WINED3DERR_INVALIDCALL;
1437 if (count > WINED3D_MAX_CONSTS_I - start_idx)
1438 count = WINED3D_MAX_CONSTS_I - start_idx;
1440 memcpy(constants, &stateblock->stateblock_state.ps_consts_i[start_idx], count * sizeof(*constants));
1441 return WINED3D_OK;
1444 HRESULT CDECL wined3d_stateblock_get_ps_consts_b(struct wined3d_stateblock *stateblock,
1445 unsigned int start_idx, unsigned int count, BOOL *constants)
1447 TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", stateblock, start_idx, count, constants);
1449 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
1450 return WINED3DERR_INVALIDCALL;
1452 if (count > WINED3D_MAX_CONSTS_B - start_idx)
1453 count = WINED3D_MAX_CONSTS_B - start_idx;
1455 memcpy(constants, &stateblock->stateblock_state.ps_consts_b[start_idx], count * sizeof(*constants));
1456 return WINED3D_OK;
1459 void CDECL wined3d_stateblock_set_vertex_declaration(struct wined3d_stateblock *stateblock,
1460 struct wined3d_vertex_declaration *declaration)
1462 TRACE("stateblock %p, declaration %p.\n", stateblock, declaration);
1464 if (declaration)
1465 wined3d_vertex_declaration_incref(declaration);
1466 if (stateblock->stateblock_state.vertex_declaration)
1467 wined3d_vertex_declaration_decref(stateblock->stateblock_state.vertex_declaration);
1468 stateblock->stateblock_state.vertex_declaration = declaration;
1469 stateblock->changed.vertexDecl = TRUE;
1472 void CDECL wined3d_stateblock_set_render_state(struct wined3d_stateblock *stateblock,
1473 enum wined3d_render_state state, unsigned int value)
1475 TRACE("stateblock %p, state %s (%#x), value %#x.\n", stateblock, debug_d3drenderstate(state), state, value);
1477 if (state > WINEHIGHEST_RENDER_STATE)
1479 WARN("Unhandled render state %#x.\n", state);
1480 return;
1483 stateblock->stateblock_state.rs[state] = value;
1484 stateblock->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
1486 if (state == WINED3D_RS_POINTSIZE
1487 && (value == WINED3D_ALPHA_TO_COVERAGE_ENABLE || value == WINED3D_ALPHA_TO_COVERAGE_DISABLE))
1489 stateblock->changed.alpha_to_coverage = 1;
1490 stateblock->stateblock_state.alpha_to_coverage = (value == WINED3D_ALPHA_TO_COVERAGE_ENABLE);
1494 void CDECL wined3d_stateblock_set_sampler_state(struct wined3d_stateblock *stateblock,
1495 UINT sampler_idx, enum wined3d_sampler_state state, unsigned int value)
1497 TRACE("stateblock %p, sampler_idx %u, state %s, value %#x.\n",
1498 stateblock, sampler_idx, debug_d3dsamplerstate(state), value);
1500 if (sampler_idx >= ARRAY_SIZE(stateblock->stateblock_state.sampler_states))
1502 WARN("Invalid sampler %u.\n", sampler_idx);
1503 return;
1506 stateblock->stateblock_state.sampler_states[sampler_idx][state] = value;
1507 stateblock->changed.samplerState[sampler_idx] |= 1u << state;
1510 void CDECL wined3d_stateblock_set_texture_stage_state(struct wined3d_stateblock *stateblock,
1511 UINT stage, enum wined3d_texture_stage_state state, unsigned int value)
1513 TRACE("stateblock %p, stage %u, state %s, value %#x.\n",
1514 stateblock, stage, debug_d3dtexturestate(state), value);
1516 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
1518 WARN("Invalid state %#x passed.\n", state);
1519 return;
1522 if (stage >= WINED3D_MAX_FFP_TEXTURES)
1524 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
1525 stage, WINED3D_MAX_FFP_TEXTURES - 1);
1526 return;
1529 stateblock->stateblock_state.texture_states[stage][state] = value;
1530 stateblock->changed.textureState[stage] |= 1u << state;
1533 void CDECL wined3d_stateblock_set_texture(struct wined3d_stateblock *stateblock,
1534 UINT stage, struct wined3d_texture *texture)
1536 TRACE("stateblock %p, stage %u, texture %p.\n", stateblock, stage, texture);
1538 if (stage >= ARRAY_SIZE(stateblock->stateblock_state.textures))
1540 WARN("Ignoring invalid stage %u.\n", stage);
1541 return;
1544 if (texture)
1545 wined3d_texture_incref(texture);
1546 if (stateblock->stateblock_state.textures[stage])
1547 wined3d_texture_decref(stateblock->stateblock_state.textures[stage]);
1548 stateblock->stateblock_state.textures[stage] = texture;
1549 stateblock->changed.textures |= 1u << stage;
1552 void CDECL wined3d_stateblock_set_transform(struct wined3d_stateblock *stateblock,
1553 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1555 TRACE("stateblock %p, state %s, matrix %p.\n", stateblock, debug_d3dtstype(d3dts), matrix);
1556 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1557 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1558 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1559 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1561 stateblock->stateblock_state.transforms[d3dts] = *matrix;
1562 stateblock->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1563 stateblock->changed.transforms = 1;
1566 void CDECL wined3d_stateblock_multiply_transform(struct wined3d_stateblock *stateblock,
1567 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1569 struct wined3d_matrix *mat = &stateblock->stateblock_state.transforms[d3dts];
1571 TRACE("stateblock %p, state %s, matrix %p.\n", stateblock, debug_d3dtstype(d3dts), matrix);
1572 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1573 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1574 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1575 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1577 multiply_matrix(mat, mat, matrix);
1578 stateblock->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1579 stateblock->changed.transforms = 1;
1582 HRESULT CDECL wined3d_stateblock_set_clip_plane(struct wined3d_stateblock *stateblock,
1583 UINT plane_idx, const struct wined3d_vec4 *plane)
1585 TRACE("stateblock %p, plane_idx %u, plane %p.\n", stateblock, plane_idx, plane);
1587 if (plane_idx >= stateblock->device->adapter->d3d_info.limits.max_clip_distances)
1589 TRACE("Application has requested clipplane this device doesn't support.\n");
1590 return WINED3DERR_INVALIDCALL;
1593 stateblock->stateblock_state.clip_planes[plane_idx] = *plane;
1594 stateblock->changed.clipplane |= 1u << plane_idx;
1595 return S_OK;
1598 void CDECL wined3d_stateblock_set_material(struct wined3d_stateblock *stateblock,
1599 const struct wined3d_material *material)
1601 TRACE("stateblock %p, material %p.\n", stateblock, material);
1603 stateblock->stateblock_state.material = *material;
1604 stateblock->changed.material = TRUE;
1607 void CDECL wined3d_stateblock_set_viewport(struct wined3d_stateblock *stateblock,
1608 const struct wined3d_viewport *viewport)
1610 TRACE("stateblock %p, viewport %p.\n", stateblock, viewport);
1612 stateblock->stateblock_state.viewport = *viewport;
1613 stateblock->changed.viewport = TRUE;
1616 void CDECL wined3d_stateblock_set_scissor_rect(struct wined3d_stateblock *stateblock, const RECT *rect)
1618 TRACE("stateblock %p, rect %s.\n", stateblock, wine_dbgstr_rect(rect));
1620 stateblock->stateblock_state.scissor_rect = *rect;
1621 stateblock->changed.scissorRect = TRUE;
1624 void CDECL wined3d_stateblock_set_index_buffer(struct wined3d_stateblock *stateblock,
1625 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1627 TRACE("stateblock %p, buffer %p, format %s.\n", stateblock, buffer, debug_d3dformat(format_id));
1629 if (buffer)
1630 wined3d_buffer_incref(buffer);
1631 if (stateblock->stateblock_state.index_buffer)
1632 wined3d_buffer_decref(stateblock->stateblock_state.index_buffer);
1633 stateblock->stateblock_state.index_buffer = buffer;
1634 stateblock->stateblock_state.index_format = format_id;
1635 stateblock->changed.indices = TRUE;
1638 void CDECL wined3d_stateblock_set_base_vertex_index(struct wined3d_stateblock *stateblock, INT base_index)
1640 TRACE("stateblock %p, base_index %d.\n", stateblock, base_index);
1642 stateblock->stateblock_state.base_vertex_index = base_index;
1645 HRESULT CDECL wined3d_stateblock_set_stream_source(struct wined3d_stateblock *stateblock,
1646 UINT stream_idx, struct wined3d_buffer *buffer, UINT offset, UINT stride)
1648 struct wined3d_stream_state *stream;
1650 TRACE("stateblock %p, stream_idx %u, buffer %p, stride %u.\n",
1651 stateblock, stream_idx, buffer, stride);
1653 if (stream_idx >= WINED3D_MAX_STREAMS)
1655 WARN("Stream index %u out of range.\n", stream_idx);
1656 return WINED3DERR_INVALIDCALL;
1659 stream = &stateblock->stateblock_state.streams[stream_idx];
1661 if (buffer)
1662 wined3d_buffer_incref(buffer);
1663 if (stream->buffer)
1664 wined3d_buffer_decref(stream->buffer);
1665 stream->buffer = buffer;
1666 stream->stride = stride;
1667 stream->offset = offset;
1668 stateblock->changed.streamSource |= 1u << stream_idx;
1669 return WINED3D_OK;
1672 HRESULT CDECL wined3d_stateblock_set_stream_source_freq(struct wined3d_stateblock *stateblock,
1673 UINT stream_idx, UINT divider)
1675 struct wined3d_stream_state *stream;
1677 TRACE("stateblock %p, stream_idx %u, divider %#x.\n", stateblock, stream_idx, divider);
1679 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1681 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1682 return WINED3DERR_INVALIDCALL;
1684 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1686 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1687 return WINED3DERR_INVALIDCALL;
1689 if (!divider)
1691 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1692 return WINED3DERR_INVALIDCALL;
1695 stream = &stateblock->stateblock_state.streams[stream_idx];
1696 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1697 stream->frequency = divider & 0x7fffff;
1698 stateblock->changed.streamFreq |= 1u << stream_idx;
1699 return WINED3D_OK;
1702 HRESULT CDECL wined3d_stateblock_set_light(struct wined3d_stateblock *stateblock,
1703 UINT light_idx, const struct wined3d_light *light)
1705 struct wined3d_light_info *object = NULL;
1706 HRESULT hr;
1708 TRACE("stateblock %p, light_idx %u, light %p.\n", stateblock, light_idx, light);
1710 /* Check the parameter range. Need for speed most wanted sets junk lights
1711 * which confuse the GL driver. */
1712 if (!light)
1713 return WINED3DERR_INVALIDCALL;
1715 switch (light->type)
1717 case WINED3D_LIGHT_POINT:
1718 case WINED3D_LIGHT_SPOT:
1719 case WINED3D_LIGHT_GLSPOT:
1720 /* Incorrect attenuation values can cause the gl driver to crash.
1721 * Happens with Need for speed most wanted. */
1722 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1724 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1725 return WINED3DERR_INVALIDCALL;
1727 break;
1729 case WINED3D_LIGHT_DIRECTIONAL:
1730 case WINED3D_LIGHT_PARALLELPOINT:
1731 /* Ignores attenuation */
1732 break;
1734 default:
1735 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL.\n");
1736 return WINED3DERR_INVALIDCALL;
1739 if (SUCCEEDED(hr = wined3d_light_state_set_light(stateblock->stateblock_state.light_state, light_idx, light, &object)))
1740 set_light_changed(stateblock, object);
1741 return hr;
1744 HRESULT CDECL wined3d_stateblock_set_light_enable(struct wined3d_stateblock *stateblock, UINT light_idx, BOOL enable)
1746 struct wined3d_light_state *light_state = stateblock->stateblock_state.light_state;
1747 struct wined3d_light_info *light_info;
1748 HRESULT hr;
1750 TRACE("stateblock %p, light_idx %u, enable %#x.\n", stateblock, light_idx, enable);
1752 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1754 if (FAILED(hr = wined3d_light_state_set_light(light_state, light_idx, &WINED3D_default_light, &light_info)))
1755 return hr;
1756 set_light_changed(stateblock, light_info);
1759 if (wined3d_light_state_enable_light(light_state, &stateblock->device->adapter->d3d_info, light_info, enable))
1760 set_light_changed(stateblock, light_info);
1762 return S_OK;
1765 const struct wined3d_stateblock_state * CDECL wined3d_stateblock_get_state(const struct wined3d_stateblock *stateblock)
1767 return &stateblock->stateblock_state;
1770 HRESULT CDECL wined3d_stateblock_get_light(const struct wined3d_stateblock *stateblock,
1771 UINT light_idx, struct wined3d_light *light, BOOL *enabled)
1773 struct wined3d_light_info *light_info;
1775 if (!(light_info = wined3d_light_state_get_light(&stateblock->light_state, light_idx)))
1777 TRACE("Light %u is not defined.\n", light_idx);
1778 return WINED3DERR_INVALIDCALL;
1780 *light = light_info->OriginalParms;
1781 *enabled = light_info->enabled ? 128 : 0;
1782 return WINED3D_OK;
1785 static void init_default_render_states(unsigned int rs[WINEHIGHEST_RENDER_STATE + 1], const struct wined3d_d3d_info *d3d_info)
1787 union
1789 struct wined3d_line_pattern lp;
1790 DWORD d;
1791 } lp;
1792 union
1794 float f;
1795 DWORD d;
1796 } tmpfloat;
1798 rs[WINED3D_RS_ZENABLE] = WINED3D_ZB_TRUE;
1799 rs[WINED3D_RS_FILLMODE] = WINED3D_FILL_SOLID;
1800 rs[WINED3D_RS_SHADEMODE] = WINED3D_SHADE_GOURAUD;
1801 lp.lp.repeat_factor = 0;
1802 lp.lp.line_pattern = 0;
1803 rs[WINED3D_RS_LINEPATTERN] = lp.d;
1804 rs[WINED3D_RS_ZWRITEENABLE] = TRUE;
1805 rs[WINED3D_RS_ALPHATESTENABLE] = FALSE;
1806 rs[WINED3D_RS_LASTPIXEL] = TRUE;
1807 rs[WINED3D_RS_SRCBLEND] = WINED3D_BLEND_ONE;
1808 rs[WINED3D_RS_DESTBLEND] = WINED3D_BLEND_ZERO;
1809 rs[WINED3D_RS_CULLMODE] = WINED3D_CULL_BACK;
1810 rs[WINED3D_RS_ZFUNC] = WINED3D_CMP_LESSEQUAL;
1811 rs[WINED3D_RS_ALPHAFUNC] = WINED3D_CMP_ALWAYS;
1812 rs[WINED3D_RS_ALPHAREF] = 0;
1813 rs[WINED3D_RS_DITHERENABLE] = FALSE;
1814 rs[WINED3D_RS_ALPHABLENDENABLE] = FALSE;
1815 rs[WINED3D_RS_FOGENABLE] = FALSE;
1816 rs[WINED3D_RS_SPECULARENABLE] = FALSE;
1817 rs[WINED3D_RS_ZVISIBLE] = 0;
1818 rs[WINED3D_RS_FOGCOLOR] = 0;
1819 rs[WINED3D_RS_FOGTABLEMODE] = WINED3D_FOG_NONE;
1820 tmpfloat.f = 0.0f;
1821 rs[WINED3D_RS_FOGSTART] = tmpfloat.d;
1822 tmpfloat.f = 1.0f;
1823 rs[WINED3D_RS_FOGEND] = tmpfloat.d;
1824 tmpfloat.f = 1.0f;
1825 rs[WINED3D_RS_FOGDENSITY] = tmpfloat.d;
1826 rs[WINED3D_RS_RANGEFOGENABLE] = FALSE;
1827 rs[WINED3D_RS_STENCILENABLE] = FALSE;
1828 rs[WINED3D_RS_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1829 rs[WINED3D_RS_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1830 rs[WINED3D_RS_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1831 rs[WINED3D_RS_STENCILREF] = 0;
1832 rs[WINED3D_RS_STENCILMASK] = 0xffffffff;
1833 rs[WINED3D_RS_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1834 rs[WINED3D_RS_STENCILWRITEMASK] = 0xffffffff;
1835 rs[WINED3D_RS_TEXTUREFACTOR] = 0xffffffff;
1836 rs[WINED3D_RS_WRAP0] = 0;
1837 rs[WINED3D_RS_WRAP1] = 0;
1838 rs[WINED3D_RS_WRAP2] = 0;
1839 rs[WINED3D_RS_WRAP3] = 0;
1840 rs[WINED3D_RS_WRAP4] = 0;
1841 rs[WINED3D_RS_WRAP5] = 0;
1842 rs[WINED3D_RS_WRAP6] = 0;
1843 rs[WINED3D_RS_WRAP7] = 0;
1844 rs[WINED3D_RS_CLIPPING] = TRUE;
1845 rs[WINED3D_RS_LIGHTING] = TRUE;
1846 rs[WINED3D_RS_AMBIENT] = 0;
1847 rs[WINED3D_RS_FOGVERTEXMODE] = WINED3D_FOG_NONE;
1848 rs[WINED3D_RS_COLORVERTEX] = TRUE;
1849 rs[WINED3D_RS_LOCALVIEWER] = TRUE;
1850 rs[WINED3D_RS_NORMALIZENORMALS] = FALSE;
1851 rs[WINED3D_RS_DIFFUSEMATERIALSOURCE] = WINED3D_MCS_COLOR1;
1852 rs[WINED3D_RS_SPECULARMATERIALSOURCE] = WINED3D_MCS_COLOR2;
1853 rs[WINED3D_RS_AMBIENTMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1854 rs[WINED3D_RS_EMISSIVEMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1855 rs[WINED3D_RS_VERTEXBLEND] = WINED3D_VBF_DISABLE;
1856 rs[WINED3D_RS_CLIPPLANEENABLE] = 0;
1857 rs[WINED3D_RS_SOFTWAREVERTEXPROCESSING] = FALSE;
1858 tmpfloat.f = 1.0f;
1859 rs[WINED3D_RS_POINTSIZE] = tmpfloat.d;
1860 tmpfloat.f = 1.0f;
1861 rs[WINED3D_RS_POINTSIZE_MIN] = tmpfloat.d;
1862 rs[WINED3D_RS_POINTSPRITEENABLE] = FALSE;
1863 rs[WINED3D_RS_POINTSCALEENABLE] = FALSE;
1864 tmpfloat.f = 1.0f;
1865 rs[WINED3D_RS_POINTSCALE_A] = tmpfloat.d;
1866 tmpfloat.f = 0.0f;
1867 rs[WINED3D_RS_POINTSCALE_B] = tmpfloat.d;
1868 tmpfloat.f = 0.0f;
1869 rs[WINED3D_RS_POINTSCALE_C] = tmpfloat.d;
1870 rs[WINED3D_RS_MULTISAMPLEANTIALIAS] = TRUE;
1871 rs[WINED3D_RS_MULTISAMPLEMASK] = 0xffffffff;
1872 rs[WINED3D_RS_PATCHEDGESTYLE] = WINED3D_PATCH_EDGE_DISCRETE;
1873 tmpfloat.f = 1.0f;
1874 rs[WINED3D_RS_PATCHSEGMENTS] = tmpfloat.d;
1875 rs[WINED3D_RS_DEBUGMONITORTOKEN] = 0xbaadcafe;
1876 tmpfloat.f = d3d_info->limits.pointsize_max;
1877 rs[WINED3D_RS_POINTSIZE_MAX] = tmpfloat.d;
1878 rs[WINED3D_RS_INDEXEDVERTEXBLENDENABLE] = FALSE;
1879 rs[WINED3D_RS_COLORWRITEENABLE] = 0x0000000f;
1880 tmpfloat.f = 0.0f;
1881 rs[WINED3D_RS_TWEENFACTOR] = tmpfloat.d;
1882 rs[WINED3D_RS_BLENDOP] = WINED3D_BLEND_OP_ADD;
1883 rs[WINED3D_RS_POSITIONDEGREE] = WINED3D_DEGREE_CUBIC;
1884 rs[WINED3D_RS_NORMALDEGREE] = WINED3D_DEGREE_LINEAR;
1885 /* states new in d3d9 */
1886 rs[WINED3D_RS_SCISSORTESTENABLE] = FALSE;
1887 rs[WINED3D_RS_SLOPESCALEDEPTHBIAS] = 0;
1888 tmpfloat.f = 1.0f;
1889 rs[WINED3D_RS_MINTESSELLATIONLEVEL] = tmpfloat.d;
1890 rs[WINED3D_RS_MAXTESSELLATIONLEVEL] = tmpfloat.d;
1891 rs[WINED3D_RS_ANTIALIASEDLINEENABLE] = FALSE;
1892 tmpfloat.f = 0.0f;
1893 rs[WINED3D_RS_ADAPTIVETESS_X] = tmpfloat.d;
1894 rs[WINED3D_RS_ADAPTIVETESS_Y] = tmpfloat.d;
1895 tmpfloat.f = 1.0f;
1896 rs[WINED3D_RS_ADAPTIVETESS_Z] = tmpfloat.d;
1897 tmpfloat.f = 0.0f;
1898 rs[WINED3D_RS_ADAPTIVETESS_W] = tmpfloat.d;
1899 rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION] = FALSE;
1900 rs[WINED3D_RS_TWOSIDEDSTENCILMODE] = FALSE;
1901 rs[WINED3D_RS_BACK_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1902 rs[WINED3D_RS_BACK_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1903 rs[WINED3D_RS_BACK_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1904 rs[WINED3D_RS_BACK_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1905 rs[WINED3D_RS_COLORWRITEENABLE1] = 0x0000000f;
1906 rs[WINED3D_RS_COLORWRITEENABLE2] = 0x0000000f;
1907 rs[WINED3D_RS_COLORWRITEENABLE3] = 0x0000000f;
1908 rs[WINED3D_RS_BLENDFACTOR] = 0xffffffff;
1909 rs[WINED3D_RS_SRGBWRITEENABLE] = 0;
1910 rs[WINED3D_RS_DEPTHBIAS] = 0;
1911 rs[WINED3D_RS_WRAP8] = 0;
1912 rs[WINED3D_RS_WRAP9] = 0;
1913 rs[WINED3D_RS_WRAP10] = 0;
1914 rs[WINED3D_RS_WRAP11] = 0;
1915 rs[WINED3D_RS_WRAP12] = 0;
1916 rs[WINED3D_RS_WRAP13] = 0;
1917 rs[WINED3D_RS_WRAP14] = 0;
1918 rs[WINED3D_RS_WRAP15] = 0;
1919 rs[WINED3D_RS_SEPARATEALPHABLENDENABLE] = FALSE;
1920 rs[WINED3D_RS_SRCBLENDALPHA] = WINED3D_BLEND_ONE;
1921 rs[WINED3D_RS_DESTBLENDALPHA] = WINED3D_BLEND_ZERO;
1922 rs[WINED3D_RS_BLENDOPALPHA] = WINED3D_BLEND_OP_ADD;
1925 static void init_default_texture_state(unsigned int i, uint32_t stage[WINED3D_HIGHEST_TEXTURE_STATE + 1])
1927 stage[WINED3D_TSS_COLOR_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_MODULATE;
1928 stage[WINED3D_TSS_COLOR_ARG1] = WINED3DTA_TEXTURE;
1929 stage[WINED3D_TSS_COLOR_ARG2] = WINED3DTA_CURRENT;
1930 stage[WINED3D_TSS_ALPHA_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_SELECT_ARG1;
1931 stage[WINED3D_TSS_ALPHA_ARG1] = WINED3DTA_TEXTURE;
1932 stage[WINED3D_TSS_ALPHA_ARG2] = WINED3DTA_CURRENT;
1933 stage[WINED3D_TSS_BUMPENV_MAT00] = 0;
1934 stage[WINED3D_TSS_BUMPENV_MAT01] = 0;
1935 stage[WINED3D_TSS_BUMPENV_MAT10] = 0;
1936 stage[WINED3D_TSS_BUMPENV_MAT11] = 0;
1937 stage[WINED3D_TSS_TEXCOORD_INDEX] = i;
1938 stage[WINED3D_TSS_BUMPENV_LSCALE] = 0;
1939 stage[WINED3D_TSS_BUMPENV_LOFFSET] = 0;
1940 stage[WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS] = WINED3D_TTFF_DISABLE;
1941 stage[WINED3D_TSS_COLOR_ARG0] = WINED3DTA_CURRENT;
1942 stage[WINED3D_TSS_ALPHA_ARG0] = WINED3DTA_CURRENT;
1943 stage[WINED3D_TSS_RESULT_ARG] = WINED3DTA_CURRENT;
1946 static void init_default_sampler_states(uint32_t states[WINED3D_MAX_COMBINED_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1])
1948 unsigned int i;
1950 for (i = 0 ; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1952 TRACE("Setting up default samplers states for sampler %u.\n", i);
1953 states[i][WINED3D_SAMP_ADDRESS_U] = WINED3D_TADDRESS_WRAP;
1954 states[i][WINED3D_SAMP_ADDRESS_V] = WINED3D_TADDRESS_WRAP;
1955 states[i][WINED3D_SAMP_ADDRESS_W] = WINED3D_TADDRESS_WRAP;
1956 states[i][WINED3D_SAMP_BORDER_COLOR] = 0;
1957 states[i][WINED3D_SAMP_MAG_FILTER] = WINED3D_TEXF_POINT;
1958 states[i][WINED3D_SAMP_MIN_FILTER] = WINED3D_TEXF_POINT;
1959 states[i][WINED3D_SAMP_MIP_FILTER] = WINED3D_TEXF_NONE;
1960 states[i][WINED3D_SAMP_MIPMAP_LOD_BIAS] = 0;
1961 states[i][WINED3D_SAMP_MAX_MIP_LEVEL] = 0;
1962 states[i][WINED3D_SAMP_MAX_ANISOTROPY] = 1;
1963 states[i][WINED3D_SAMP_SRGB_TEXTURE] = 0;
1964 /* TODO: Indicates which element of a multielement texture to use. */
1965 states[i][WINED3D_SAMP_ELEMENT_INDEX] = 0;
1966 /* TODO: Vertex offset in the presampled displacement map. */
1967 states[i][WINED3D_SAMP_DMAP_OFFSET] = 0;
1971 static void state_init_default(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info)
1973 struct wined3d_matrix identity;
1974 unsigned int i, j;
1976 TRACE("state %p, d3d_info %p.\n", state, d3d_info);
1978 get_identity_matrix(&identity);
1979 state->primitive_type = WINED3D_PT_UNDEFINED;
1980 state->patch_vertex_count = 0;
1982 /* Set some of the defaults for lights, transforms etc */
1983 state->transforms[WINED3D_TS_PROJECTION] = identity;
1984 state->transforms[WINED3D_TS_VIEW] = identity;
1985 for (i = 0; i < 256; ++i)
1987 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
1990 init_default_render_states(state->render_states, d3d_info);
1992 /* Texture Stage States - Put directly into state block, we will call function below */
1993 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
1995 TRACE("Setting up default texture states for texture Stage %u.\n", i);
1996 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
1997 init_default_texture_state(i, state->texture_states[i]);
2000 state->blend_factor.r = 1.0f;
2001 state->blend_factor.g = 1.0f;
2002 state->blend_factor.b = 1.0f;
2003 state->blend_factor.a = 1.0f;
2005 state->sample_mask = 0xffffffff;
2007 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
2008 state->streams[i].frequency = 1;
2010 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2012 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
2013 state->cb[i][j].size = WINED3D_MAX_CONSTANT_BUFFER_SIZE * 16;
2017 static int lights_compare(const void *key, const struct rb_entry *entry)
2019 const struct wined3d_light_info *light = RB_ENTRY_VALUE(entry, struct wined3d_light_info, entry);
2020 unsigned int original_index = (ULONG_PTR)key;
2022 return wined3d_uint32_compare(light->OriginalIndex, original_index);
2025 void state_init(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info,
2026 uint32_t flags, enum wined3d_feature_level feature_level)
2028 state->feature_level = feature_level;
2029 state->flags = flags;
2031 rb_init(&state->light_state.lights_tree, lights_compare);
2033 if (flags & WINED3D_STATE_INIT_DEFAULT)
2034 state_init_default(state, d3d_info);
2037 static bool wined3d_select_feature_level(const struct wined3d_adapter *adapter,
2038 const enum wined3d_feature_level *levels, unsigned int level_count,
2039 enum wined3d_feature_level *selected_level)
2041 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
2042 unsigned int i;
2044 for (i = 0; i < level_count; ++i)
2046 if (levels[i] && d3d_info->feature_level >= levels[i])
2048 *selected_level = levels[i];
2049 return true;
2053 FIXME_(winediag)("None of the requested D3D feature levels is supported on this GPU "
2054 "with the current shader backend.\n");
2055 return false;
2058 HRESULT CDECL wined3d_state_create(struct wined3d_device *device,
2059 const enum wined3d_feature_level *levels, unsigned int level_count, struct wined3d_state **state)
2061 enum wined3d_feature_level feature_level;
2062 struct wined3d_state *object;
2064 TRACE("device %p, levels %p, level_count %u, state %p.\n", device, levels, level_count, state);
2066 if (!wined3d_select_feature_level(device->adapter, levels, level_count, &feature_level))
2067 return E_FAIL;
2069 TRACE("Selected feature level %s.\n", wined3d_debug_feature_level(feature_level));
2071 if (!(object = heap_alloc_zero(sizeof(*object))))
2072 return E_OUTOFMEMORY;
2073 state_init(object, &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT, feature_level);
2075 *state = object;
2076 return S_OK;
2079 enum wined3d_feature_level CDECL wined3d_state_get_feature_level(const struct wined3d_state *state)
2081 TRACE("state %p.\n", state);
2083 return state->feature_level;
2086 void CDECL wined3d_state_destroy(struct wined3d_state *state)
2088 TRACE("state %p.\n", state);
2090 state_cleanup(state);
2091 heap_free(state);
2094 static void stateblock_state_init_default(struct wined3d_stateblock_state *state,
2095 const struct wined3d_d3d_info *d3d_info)
2097 struct wined3d_matrix identity;
2098 unsigned int i;
2100 get_identity_matrix(&identity);
2102 state->transforms[WINED3D_TS_PROJECTION] = identity;
2103 state->transforms[WINED3D_TS_VIEW] = identity;
2104 for (i = 0; i < 256; ++i)
2106 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
2109 init_default_render_states(state->rs, d3d_info);
2111 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
2113 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
2114 init_default_texture_state(i, state->texture_states[i]);
2117 init_default_sampler_states(state->sampler_states);
2119 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
2120 state->streams[i].frequency = 1;
2123 static void wined3d_stateblock_state_init(struct wined3d_stateblock_state *state,
2124 const struct wined3d_device *device, uint32_t flags)
2126 rb_init(&state->light_state->lights_tree, lights_compare);
2128 if (flags & WINED3D_STATE_INIT_DEFAULT)
2129 stateblock_state_init_default(state, &device->adapter->d3d_info);
2133 static HRESULT stateblock_init(struct wined3d_stateblock *stateblock, const struct wined3d_stateblock *device_state,
2134 struct wined3d_device *device, enum wined3d_stateblock_type type)
2136 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2138 stateblock->ref = 1;
2139 stateblock->device = device;
2140 stateblock->stateblock_state.light_state = &stateblock->light_state;
2141 wined3d_stateblock_state_init(&stateblock->stateblock_state, device,
2142 type == WINED3D_SBT_PRIMARY ? WINED3D_STATE_INIT_DEFAULT : 0);
2144 stateblock->changed.store_stream_offset = 1;
2145 list_init(&stateblock->changed.changed_lights);
2147 if (type == WINED3D_SBT_RECORDED || type == WINED3D_SBT_PRIMARY)
2148 return WINED3D_OK;
2150 TRACE("Updating changed flags appropriate for type %#x.\n", type);
2152 switch (type)
2154 case WINED3D_SBT_ALL:
2155 stateblock_init_lights(stateblock, &device_state->stateblock_state.light_state->lights_tree);
2156 stateblock_savedstates_set_all(&stateblock->changed,
2157 d3d_info->limits.vs_uniform_count, d3d_info->limits.ps_uniform_count);
2158 break;
2160 case WINED3D_SBT_PIXEL_STATE:
2161 stateblock_savedstates_set_pixel(&stateblock->changed,
2162 d3d_info->limits.ps_uniform_count);
2163 break;
2165 case WINED3D_SBT_VERTEX_STATE:
2166 stateblock_init_lights(stateblock, &device_state->stateblock_state.light_state->lights_tree);
2167 stateblock_savedstates_set_vertex(&stateblock->changed,
2168 d3d_info->limits.vs_uniform_count);
2169 break;
2171 default:
2172 FIXME("Unrecognized state block type %#x.\n", type);
2173 break;
2176 wined3d_stateblock_init_contained_states(stateblock);
2177 wined3d_stateblock_capture(stateblock, device_state);
2179 /* According to the tests, stream offset is not updated in the captured state if
2180 * the state was captured on state block creation. This is not the case for
2181 * state blocks initialized with BeginStateBlock / EndStateBlock, multiple
2182 * captures get stream offsets updated. */
2183 stateblock->changed.store_stream_offset = 0;
2185 return WINED3D_OK;
2188 HRESULT CDECL wined3d_stateblock_create(struct wined3d_device *device, const struct wined3d_stateblock *device_state,
2189 enum wined3d_stateblock_type type, struct wined3d_stateblock **stateblock)
2191 struct wined3d_stateblock *object;
2192 HRESULT hr;
2194 TRACE("device %p, device_state %p, type %#x, stateblock %p.\n",
2195 device, device_state, type, stateblock);
2197 if (!(object = heap_alloc_zero(sizeof(*object))))
2198 return E_OUTOFMEMORY;
2200 hr = stateblock_init(object, device_state, device, type);
2201 if (FAILED(hr))
2203 WARN("Failed to initialize stateblock, hr %#lx.\n", hr);
2204 heap_free(object);
2205 return hr;
2208 TRACE("Created stateblock %p.\n", object);
2209 *stateblock = object;
2211 return WINED3D_OK;
2214 void CDECL wined3d_stateblock_reset(struct wined3d_stateblock *stateblock)
2216 TRACE("stateblock %p.\n", stateblock);
2218 wined3d_stateblock_state_cleanup(&stateblock->stateblock_state);
2219 memset(&stateblock->stateblock_state, 0, sizeof(stateblock->stateblock_state));
2220 stateblock->stateblock_state.light_state = &stateblock->light_state;
2221 wined3d_stateblock_state_init(&stateblock->stateblock_state, stateblock->device, WINED3D_STATE_INIT_DEFAULT);
2224 static void wined3d_device_set_base_vertex_index(struct wined3d_device *device, int base_index)
2226 TRACE("device %p, base_index %d.\n", device, base_index);
2228 device->cs->c.state->base_vertex_index = base_index;
2231 static void wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2232 unsigned int start_idx, unsigned int count, const BOOL *constants)
2234 unsigned int i;
2236 TRACE("device %p, start_idx %u, count %u, constants %p.\n", device, start_idx, count, constants);
2238 if (TRACE_ON(d3d))
2240 for (i = 0; i < count; ++i)
2241 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2244 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2247 static void wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2248 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2250 unsigned int i;
2252 TRACE("device %p, start_idx %u, count %u, constants %p.\n", device, start_idx, count, constants);
2254 if (TRACE_ON(d3d))
2256 for (i = 0; i < count; ++i)
2257 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2260 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2263 static void wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2264 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2266 unsigned int i;
2268 TRACE("device %p, start_idx %u, count %u, constants %p.\n", device, start_idx, count, constants);
2270 if (TRACE_ON(d3d))
2272 for (i = 0; i < count; ++i)
2273 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2276 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2279 static void wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2280 unsigned int start_idx, unsigned int count, const BOOL *constants)
2282 unsigned int i;
2284 TRACE("device %p, start_idx %u, count %u, constants %p.\n", device, start_idx, count, constants);
2286 if (TRACE_ON(d3d))
2288 for (i = 0; i < count; ++i)
2289 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2292 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2295 static void wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2296 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2298 unsigned int i;
2300 TRACE("device %p, start_idx %u, count %u, constants %p.\n", device, start_idx, count, constants);
2302 if (TRACE_ON(d3d))
2304 for (i = 0; i < count; ++i)
2305 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2308 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2311 static void wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2312 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2314 unsigned int i;
2316 TRACE("device %p, start_idx %u, count %u, constants %p.\n", device, start_idx, count, constants);
2318 if (TRACE_ON(d3d))
2320 for (i = 0; i < count; ++i)
2321 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2324 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2327 /* Note lights are real special cases. Although the device caps state only
2328 * e.g. 8 are supported, you can reference any indexes you want as long as
2329 * that number max are enabled at any one point in time. Therefore since the
2330 * indices can be anything, we need a hashmap of them. However, this causes
2331 * stateblock problems. When capturing the state block, I duplicate the
2332 * hashmap, but when recording, just build a chain pretty much of commands to
2333 * be replayed. */
2334 static void wined3d_device_context_set_light(struct wined3d_device_context *context,
2335 unsigned int light_idx, const struct wined3d_light *light)
2337 struct wined3d_light_info *object = NULL;
2338 float rho;
2340 if (FAILED(wined3d_light_state_set_light(&context->state->light_state, light_idx, light, &object)))
2341 return;
2343 /* Initialize the object. */
2344 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
2345 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
2346 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
2347 light_idx, light->type, debug_color(&light->diffuse),
2348 debug_color(&light->specular), debug_color(&light->ambient),
2349 light->position.x, light->position.y, light->position.z,
2350 light->direction.x, light->direction.y, light->direction.z,
2351 light->range, light->falloff, light->theta, light->phi);
2353 switch (light->type)
2355 case WINED3D_LIGHT_POINT:
2356 /* Position */
2357 object->position.x = light->position.x;
2358 object->position.y = light->position.y;
2359 object->position.z = light->position.z;
2360 object->position.w = 1.0f;
2361 object->cutoff = 180.0f;
2362 /* FIXME: Range */
2363 break;
2365 case WINED3D_LIGHT_DIRECTIONAL:
2366 /* Direction */
2367 object->direction.x = -light->direction.x;
2368 object->direction.y = -light->direction.y;
2369 object->direction.z = -light->direction.z;
2370 object->direction.w = 0.0f;
2371 object->exponent = 0.0f;
2372 object->cutoff = 180.0f;
2373 break;
2375 case WINED3D_LIGHT_SPOT:
2376 /* Position */
2377 object->position.x = light->position.x;
2378 object->position.y = light->position.y;
2379 object->position.z = light->position.z;
2380 object->position.w = 1.0f;
2382 /* Direction */
2383 object->direction.x = light->direction.x;
2384 object->direction.y = light->direction.y;
2385 object->direction.z = light->direction.z;
2386 object->direction.w = 0.0f;
2388 /* opengl-ish and d3d-ish spot lights use too different models
2389 * for the light "intensity" as a function of the angle towards
2390 * the main light direction, so we only can approximate very
2391 * roughly. However, spot lights are rather rarely used in games
2392 * (if ever used at all). Furthermore if still used, probably
2393 * nobody pays attention to such details. */
2394 if (!light->falloff)
2396 /* Falloff = 0 is easy, because d3d's and opengl's spot light
2397 * equations have the falloff resp. exponent parameter as an
2398 * exponent, so the spot light lighting will always be 1.0 for
2399 * both of them, and we don't have to care for the rest of the
2400 * rather complex calculation. */
2401 object->exponent = 0.0f;
2403 else
2405 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
2406 if (rho < 0.0001f)
2407 rho = 0.0001f;
2408 object->exponent = -0.3f / logf(cosf(rho / 2));
2411 if (object->exponent > 128.0f)
2412 object->exponent = 128.0f;
2414 object->cutoff = (float)(light->phi * 90 / M_PI);
2415 /* FIXME: Range */
2416 break;
2418 case WINED3D_LIGHT_PARALLELPOINT:
2419 object->position.x = light->position.x;
2420 object->position.y = light->position.y;
2421 object->position.z = light->position.z;
2422 object->position.w = 1.0f;
2423 break;
2425 default:
2426 FIXME("Unrecognized light type %#x.\n", light->type);
2429 wined3d_device_context_emit_set_light(context, object);
2432 static void wined3d_device_set_light_enable(struct wined3d_device *device, unsigned int light_idx, bool enable)
2434 struct wined3d_light_state *light_state = &device->cs->c.state->light_state;
2435 struct wined3d_light_info *light_info;
2437 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
2439 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
2440 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
2442 TRACE("Light enabled requested but light not defined, so defining one!\n");
2443 wined3d_device_context_set_light(&device->cs->c, light_idx, &WINED3D_default_light);
2445 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
2447 ERR("Adding default lights has failed dismally.\n");
2448 return;
2452 if (wined3d_light_state_enable_light(light_state, &device->adapter->d3d_info, light_info, enable))
2453 wined3d_device_context_emit_set_light_enable(&device->cs->c, light_idx, enable);
2456 static void wined3d_device_set_clip_plane(struct wined3d_device *device,
2457 unsigned int plane_idx, const struct wined3d_vec4 *plane)
2459 struct wined3d_vec4 *clip_planes = device->cs->c.state->clip_planes;
2461 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2463 if (!memcmp(&clip_planes[plane_idx], plane, sizeof(*plane)))
2465 TRACE("Application is setting old values over, nothing to do.\n");
2466 return;
2469 clip_planes[plane_idx] = *plane;
2471 wined3d_device_context_emit_set_clip_plane(&device->cs->c, plane_idx, plane);
2474 static void resolve_depth_buffer(struct wined3d_device *device)
2476 const struct wined3d_state *state = device->cs->c.state;
2477 struct wined3d_rendertarget_view *src_view;
2478 struct wined3d_resource *dst_resource;
2479 struct wined3d_texture *dst_texture;
2481 if (!(dst_texture = wined3d_state_get_ffp_texture(state, 0)))
2482 return;
2483 dst_resource = &dst_texture->resource;
2484 if (!dst_resource->format->depth_size)
2485 return;
2486 if (!(src_view = state->fb.depth_stencil))
2487 return;
2489 wined3d_device_context_resolve_sub_resource(&device->cs->c, dst_resource, 0,
2490 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
2493 static void wined3d_device_set_render_state(struct wined3d_device *device,
2494 enum wined3d_render_state state, unsigned int value)
2496 if (value == device->cs->c.state->render_states[state])
2498 TRACE("Application is setting the old value over, nothing to do.\n");
2500 else
2502 device->cs->c.state->render_states[state] = value;
2503 wined3d_device_context_emit_set_render_state(&device->cs->c, state, value);
2506 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2508 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2509 resolve_depth_buffer(device);
2513 static void wined3d_device_set_texture_stage_state(struct wined3d_device *device,
2514 unsigned int stage, enum wined3d_texture_stage_state state, uint32_t value)
2516 TRACE("device %p, stage %u, state %s, value %#x.\n",
2517 device, stage, debug_d3dtexturestate(state), value);
2519 if (value == device->cs->c.state->texture_states[stage][state])
2521 TRACE("Application is setting the old value over, nothing to do.\n");
2522 return;
2525 device->cs->c.state->texture_states[stage][state] = value;
2527 wined3d_device_context_emit_set_texture_state(&device->cs->c, stage, state, value);
2530 static void wined3d_device_set_texture(struct wined3d_device *device,
2531 unsigned int stage, struct wined3d_texture *texture)
2533 struct wined3d_state *state = device->cs->c.state;
2534 struct wined3d_texture *prev;
2536 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
2538 prev = state->textures[stage];
2539 TRACE("Previous texture %p.\n", prev);
2541 if (texture == prev)
2543 TRACE("App is setting the same texture again, nothing to do.\n");
2544 return;
2547 TRACE("Setting new texture to %p.\n", texture);
2548 state->textures[stage] = texture;
2550 if (texture)
2551 wined3d_texture_incref(texture);
2552 wined3d_device_context_emit_set_texture(&device->cs->c, stage, texture);
2553 if (prev)
2554 wined3d_texture_decref(prev);
2556 return;
2559 static void wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
2561 TRACE("device %p, material %p.\n", device, material);
2563 device->cs->c.state->material = *material;
2564 wined3d_device_context_emit_set_material(&device->cs->c, material);
2567 static void wined3d_device_set_transform(struct wined3d_device *device,
2568 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
2570 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
2571 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
2572 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
2573 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
2574 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
2576 /* If the new matrix is the same as the current one,
2577 * we cut off any further processing. this seems to be a reasonable
2578 * optimization because as was noticed, some apps (warcraft3 for example)
2579 * tend towards setting the same matrix repeatedly for some reason.
2581 * From here on we assume that the new matrix is different, wherever it matters. */
2582 if (!memcmp(&device->cs->c.state->transforms[state], matrix, sizeof(*matrix)))
2584 TRACE("The application is setting the same matrix over again.\n");
2585 return;
2588 device->cs->c.state->transforms[state] = *matrix;
2589 wined3d_device_context_emit_set_transform(&device->cs->c, state, matrix);
2592 static enum wined3d_texture_address get_texture_address_mode(const struct wined3d_texture *texture,
2593 enum wined3d_texture_address t)
2595 if (t < WINED3D_TADDRESS_WRAP || t > WINED3D_TADDRESS_MIRROR_ONCE)
2597 FIXME("Unrecognized or unsupported texture address mode %#x.\n", t);
2598 return WINED3D_TADDRESS_WRAP;
2601 /* Cubemaps are always set to clamp, regardless of the sampler state. */
2602 if ((texture->resource.usage & WINED3DUSAGE_LEGACY_CUBEMAP)
2603 || ((texture->flags & WINED3D_TEXTURE_COND_NP2) && t == WINED3D_TADDRESS_WRAP))
2604 return WINED3D_TADDRESS_CLAMP;
2606 return t;
2609 static void sampler_desc_from_sampler_states(struct wined3d_sampler_desc *desc,
2610 const uint32_t *sampler_states, const struct wined3d_texture *texture)
2612 const struct wined3d_d3d_info *d3d_info = &texture->resource.device->adapter->d3d_info;
2614 desc->address_u = get_texture_address_mode(texture, sampler_states[WINED3D_SAMP_ADDRESS_U]);
2615 desc->address_v = get_texture_address_mode(texture, sampler_states[WINED3D_SAMP_ADDRESS_V]);
2616 desc->address_w = get_texture_address_mode(texture, sampler_states[WINED3D_SAMP_ADDRESS_W]);
2617 wined3d_color_from_d3dcolor((struct wined3d_color *)desc->border_color,
2618 sampler_states[WINED3D_SAMP_BORDER_COLOR]);
2619 if (sampler_states[WINED3D_SAMP_MAG_FILTER] > WINED3D_TEXF_ANISOTROPIC)
2620 FIXME("Unrecognized or unsupported WINED3D_SAMP_MAG_FILTER %#x.\n",
2621 sampler_states[WINED3D_SAMP_MAG_FILTER]);
2622 desc->mag_filter = min(max(sampler_states[WINED3D_SAMP_MAG_FILTER], WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR);
2623 if (sampler_states[WINED3D_SAMP_MIN_FILTER] > WINED3D_TEXF_ANISOTROPIC)
2624 FIXME("Unrecognized or unsupported WINED3D_SAMP_MIN_FILTER %#x.\n",
2625 sampler_states[WINED3D_SAMP_MIN_FILTER]);
2626 desc->min_filter = min(max(sampler_states[WINED3D_SAMP_MIN_FILTER], WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR);
2627 if (sampler_states[WINED3D_SAMP_MIP_FILTER] > WINED3D_TEXF_ANISOTROPIC)
2628 FIXME("Unrecognized or unsupported WINED3D_SAMP_MIP_FILTER %#x.\n",
2629 sampler_states[WINED3D_SAMP_MIP_FILTER]);
2630 desc->mip_filter = min(max(sampler_states[WINED3D_SAMP_MIP_FILTER], WINED3D_TEXF_NONE), WINED3D_TEXF_LINEAR);
2631 desc->lod_bias = int_to_float(sampler_states[WINED3D_SAMP_MIPMAP_LOD_BIAS]);
2632 desc->min_lod = -1000.0f;
2633 desc->max_lod = 1000.0f;
2635 /* The LOD is already clamped to texture->level_count in wined3d_stateblock_set_texture_lod(). */
2636 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
2637 desc->mip_base_level = 0;
2638 else if (desc->mip_filter == WINED3D_TEXF_NONE)
2639 desc->mip_base_level = texture->lod;
2640 else
2641 desc->mip_base_level = min(max(sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL], texture->lod), texture->level_count - 1);
2643 desc->max_anisotropy = sampler_states[WINED3D_SAMP_MAX_ANISOTROPY];
2644 if ((sampler_states[WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_ANISOTROPIC
2645 && sampler_states[WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_ANISOTROPIC
2646 && sampler_states[WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_ANISOTROPIC)
2647 || (texture->flags & WINED3D_TEXTURE_COND_NP2))
2648 desc->max_anisotropy = 1;
2649 desc->compare = texture->resource.format_caps & WINED3D_FORMAT_CAP_SHADOW;
2650 desc->comparison_func = WINED3D_CMP_LESSEQUAL;
2652 /* Only use the LSB of the WINED3D_SAMP_SRGB_TEXTURE value. This matches
2653 * the behaviour of the AMD Windows driver.
2655 * Might & Magic: Heroes VI - Shades of Darkness sets
2656 * WINED3D_SAMP_SRGB_TEXTURE to a large value that looks like a
2657 * pointer—presumably by accident—and expects sRGB decoding to be
2658 * disabled. */
2659 desc->srgb_decode = sampler_states[WINED3D_SAMP_SRGB_TEXTURE] & 0x1;
2661 if (!(texture->resource.format_caps & WINED3D_FORMAT_CAP_FILTERING))
2663 desc->mag_filter = WINED3D_TEXF_POINT;
2664 desc->min_filter = WINED3D_TEXF_POINT;
2665 desc->mip_filter = WINED3D_TEXF_NONE;
2668 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
2670 desc->mip_filter = WINED3D_TEXF_NONE;
2671 if (d3d_info->normalized_texrect)
2672 desc->min_filter = WINED3D_TEXF_POINT;
2676 void CDECL wined3d_device_apply_stateblock(struct wined3d_device *device,
2677 struct wined3d_stateblock *stateblock)
2679 bool set_blend_state = false, set_depth_stencil_state = false, set_rasterizer_state = false;
2680 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
2681 const struct wined3d_saved_states *changed = &stateblock->changed;
2682 const unsigned int word_bit_count = sizeof(DWORD) * CHAR_BIT;
2683 struct wined3d_device_context *context = &device->cs->c;
2684 unsigned int i, j, start, idx;
2685 bool set_depth_bounds = false;
2686 struct wined3d_range range;
2687 uint32_t map;
2689 TRACE("device %p, stateblock %p.\n", device, stateblock);
2691 if (changed->vertexShader)
2692 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_VERTEX, state->vs);
2693 if (changed->pixelShader)
2694 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_PIXEL, state->ps);
2696 for (start = 0; ; start = range.offset + range.size)
2698 if (!wined3d_bitmap_get_range(changed->vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
2699 break;
2701 wined3d_device_set_vs_consts_f(device, range.offset, range.size, &state->vs_consts_f[range.offset]);
2704 map = changed->vertexShaderConstantsI;
2705 for (start = 0; ; start = range.offset + range.size)
2707 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
2708 break;
2710 wined3d_device_set_vs_consts_i(device, range.offset, range.size, &state->vs_consts_i[range.offset]);
2713 map = changed->vertexShaderConstantsB;
2714 for (start = 0; ; start = range.offset + range.size)
2716 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
2717 break;
2719 wined3d_device_set_vs_consts_b(device, range.offset, range.size, &state->vs_consts_b[range.offset]);
2722 for (start = 0; ; start = range.offset + range.size)
2724 if (!wined3d_bitmap_get_range(changed->ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
2725 break;
2727 wined3d_device_set_ps_consts_f(device, range.offset, range.size, &state->ps_consts_f[range.offset]);
2730 map = changed->pixelShaderConstantsI;
2731 for (start = 0; ; start = range.offset + range.size)
2733 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
2734 break;
2736 wined3d_device_set_ps_consts_i(device, range.offset, range.size, &state->ps_consts_i[range.offset]);
2739 map = changed->pixelShaderConstantsB;
2740 for (start = 0; ; start = range.offset + range.size)
2742 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
2743 break;
2745 wined3d_device_set_ps_consts_b(device, range.offset, range.size, &state->ps_consts_b[range.offset]);
2748 if (changed->lights)
2750 struct wined3d_light_info *light, *cursor;
2752 LIST_FOR_EACH_ENTRY_SAFE(light, cursor, &changed->changed_lights, struct wined3d_light_info, changed_entry)
2754 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
2755 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
2756 list_remove(&light->changed_entry);
2757 light->changed = false;
2761 for (i = 0; i < ARRAY_SIZE(changed->renderState); ++i)
2763 map = changed->renderState[i];
2764 while (map)
2766 j = wined3d_bit_scan(&map);
2767 idx = i * word_bit_count + j;
2769 switch (idx)
2771 case WINED3D_RS_BLENDFACTOR:
2772 case WINED3D_RS_MULTISAMPLEMASK:
2773 case WINED3D_RS_ALPHABLENDENABLE:
2774 case WINED3D_RS_SRCBLEND:
2775 case WINED3D_RS_DESTBLEND:
2776 case WINED3D_RS_BLENDOP:
2777 case WINED3D_RS_SEPARATEALPHABLENDENABLE:
2778 case WINED3D_RS_SRCBLENDALPHA:
2779 case WINED3D_RS_DESTBLENDALPHA:
2780 case WINED3D_RS_BLENDOPALPHA:
2781 case WINED3D_RS_COLORWRITEENABLE:
2782 case WINED3D_RS_COLORWRITEENABLE1:
2783 case WINED3D_RS_COLORWRITEENABLE2:
2784 case WINED3D_RS_COLORWRITEENABLE3:
2785 set_blend_state = true;
2786 break;
2788 case WINED3D_RS_BACK_STENCILFAIL:
2789 case WINED3D_RS_BACK_STENCILFUNC:
2790 case WINED3D_RS_BACK_STENCILPASS:
2791 case WINED3D_RS_BACK_STENCILZFAIL:
2792 case WINED3D_RS_STENCILENABLE:
2793 case WINED3D_RS_STENCILFAIL:
2794 case WINED3D_RS_STENCILFUNC:
2795 case WINED3D_RS_STENCILREF:
2796 case WINED3D_RS_STENCILMASK:
2797 case WINED3D_RS_STENCILPASS:
2798 case WINED3D_RS_STENCILWRITEMASK:
2799 case WINED3D_RS_STENCILZFAIL:
2800 case WINED3D_RS_TWOSIDEDSTENCILMODE:
2801 case WINED3D_RS_ZENABLE:
2802 case WINED3D_RS_ZFUNC:
2803 case WINED3D_RS_ZWRITEENABLE:
2804 set_depth_stencil_state = true;
2805 break;
2807 case WINED3D_RS_FILLMODE:
2808 case WINED3D_RS_CULLMODE:
2809 case WINED3D_RS_SLOPESCALEDEPTHBIAS:
2810 case WINED3D_RS_DEPTHBIAS:
2811 case WINED3D_RS_SCISSORTESTENABLE:
2812 case WINED3D_RS_ANTIALIASEDLINEENABLE:
2813 set_rasterizer_state = true;
2814 break;
2816 case WINED3D_RS_ADAPTIVETESS_X:
2817 case WINED3D_RS_ADAPTIVETESS_Z:
2818 case WINED3D_RS_ADAPTIVETESS_W:
2819 set_depth_bounds = true;
2820 break;
2822 case WINED3D_RS_ADAPTIVETESS_Y:
2823 break;
2825 case WINED3D_RS_ANTIALIAS:
2826 if (state->rs[WINED3D_RS_ANTIALIAS])
2827 FIXME("Antialias not supported yet.\n");
2828 break;
2830 case WINED3D_RS_TEXTUREPERSPECTIVE:
2831 break;
2833 case WINED3D_RS_WRAPU:
2834 if (state->rs[WINED3D_RS_WRAPU])
2835 FIXME("Render state WINED3D_RS_WRAPU not implemented yet.\n");
2836 break;
2838 case WINED3D_RS_WRAPV:
2839 if (state->rs[WINED3D_RS_WRAPV])
2840 FIXME("Render state WINED3D_RS_WRAPV not implemented yet.\n");
2841 break;
2843 case WINED3D_RS_MONOENABLE:
2844 if (state->rs[WINED3D_RS_MONOENABLE])
2845 FIXME("Render state WINED3D_RS_MONOENABLE not implemented yet.\n");
2846 break;
2848 case WINED3D_RS_ROP2:
2849 if (state->rs[WINED3D_RS_ROP2])
2850 FIXME("Render state WINED3D_RS_ROP2 not implemented yet.\n");
2851 break;
2853 case WINED3D_RS_PLANEMASK:
2854 if (state->rs[WINED3D_RS_PLANEMASK])
2855 FIXME("Render state WINED3D_RS_PLANEMASK not implemented yet.\n");
2856 break;
2858 case WINED3D_RS_LASTPIXEL:
2859 if (!state->rs[WINED3D_RS_LASTPIXEL])
2861 static bool warned;
2862 if (!warned)
2864 FIXME("Last Pixel Drawing Disabled, not handled yet.\n");
2865 warned = true;
2868 break;
2870 case WINED3D_RS_ZVISIBLE:
2871 if (state->rs[WINED3D_RS_ZVISIBLE])
2872 FIXME("WINED3D_RS_ZVISIBLE not implemented.\n");
2873 break;
2875 case WINED3D_RS_SUBPIXEL:
2876 if (state->rs[WINED3D_RS_SUBPIXEL])
2877 FIXME("Render state WINED3D_RS_SUBPIXEL not implemented yet.\n");
2878 break;
2880 case WINED3D_RS_SUBPIXELX:
2881 if (state->rs[WINED3D_RS_SUBPIXELX])
2882 FIXME("Render state WINED3D_RS_SUBPIXELX not implemented yet.\n");
2883 break;
2885 case WINED3D_RS_STIPPLEDALPHA:
2886 if (state->rs[WINED3D_RS_STIPPLEDALPHA])
2887 FIXME("Stippled Alpha not supported yet.\n");
2888 break;
2890 case WINED3D_RS_STIPPLEENABLE:
2891 if (state->rs[WINED3D_RS_STIPPLEENABLE])
2892 FIXME("Render state WINED3D_RS_STIPPLEENABLE not implemented yet.\n");
2893 break;
2895 case WINED3D_RS_MIPMAPLODBIAS:
2896 if (state->rs[WINED3D_RS_MIPMAPLODBIAS])
2897 FIXME("Render state WINED3D_RS_MIPMAPLODBIAS not implemented yet.\n");
2898 break;
2900 case WINED3D_RS_ANISOTROPY:
2901 if (state->rs[WINED3D_RS_ANISOTROPY])
2902 FIXME("Render state WINED3D_RS_ANISOTROPY not implemented yet.\n");
2903 break;
2905 case WINED3D_RS_FLUSHBATCH:
2906 if (state->rs[WINED3D_RS_FLUSHBATCH])
2907 FIXME("Render state WINED3D_RS_FLUSHBATCH not implemented yet.\n");
2908 break;
2910 case WINED3D_RS_TRANSLUCENTSORTINDEPENDENT:
2911 if (state->rs[WINED3D_RS_TRANSLUCENTSORTINDEPENDENT])
2912 FIXME("Render state WINED3D_RS_TRANSLUCENTSORTINDEPENDENT not implemented yet.\n");
2913 break;
2915 case WINED3D_RS_WRAP0:
2916 case WINED3D_RS_WRAP1:
2917 case WINED3D_RS_WRAP2:
2918 case WINED3D_RS_WRAP3:
2919 case WINED3D_RS_WRAP4:
2920 case WINED3D_RS_WRAP5:
2921 case WINED3D_RS_WRAP6:
2922 case WINED3D_RS_WRAP7:
2923 case WINED3D_RS_WRAP8:
2924 case WINED3D_RS_WRAP9:
2925 case WINED3D_RS_WRAP10:
2926 case WINED3D_RS_WRAP11:
2927 case WINED3D_RS_WRAP12:
2928 case WINED3D_RS_WRAP13:
2929 case WINED3D_RS_WRAP14:
2930 case WINED3D_RS_WRAP15:
2932 static unsigned int once;
2934 if ((state->rs[idx]) && !once++)
2935 FIXME("(WINED3D_RS_WRAP0) Texture wrapping not yet supported.\n");
2936 break;
2939 case WINED3D_RS_EXTENTS:
2940 if (state->rs[WINED3D_RS_EXTENTS])
2941 FIXME("Render state WINED3D_RS_EXTENTS not implemented yet.\n");
2942 break;
2944 case WINED3D_RS_COLORKEYBLENDENABLE:
2945 if (state->rs[WINED3D_RS_COLORKEYBLENDENABLE])
2946 FIXME("Render state WINED3D_RS_COLORKEYBLENDENABLE not implemented yet.\n");
2947 break;
2949 case WINED3D_RS_SOFTWAREVERTEXPROCESSING:
2951 static unsigned int once;
2953 if ((state->rs[WINED3D_RS_SOFTWAREVERTEXPROCESSING]) && !once++)
2954 FIXME("Software vertex processing not implemented.\n");
2955 break;
2958 case WINED3D_RS_PATCHEDGESTYLE:
2959 if (state->rs[WINED3D_RS_PATCHEDGESTYLE] != WINED3D_PATCH_EDGE_DISCRETE)
2960 FIXME("WINED3D_RS_PATCHEDGESTYLE %#x not yet implemented.\n",
2961 state->rs[WINED3D_RS_PATCHEDGESTYLE]);
2962 break;
2964 case WINED3D_RS_PATCHSEGMENTS:
2966 union
2968 uint32_t d;
2969 float f;
2970 } tmpvalue;
2971 tmpvalue.f = 1.0f;
2973 if (state->rs[WINED3D_RS_PATCHSEGMENTS] != tmpvalue.d)
2975 static bool displayed = false;
2977 tmpvalue.d = state->rs[WINED3D_RS_PATCHSEGMENTS];
2978 if(!displayed)
2979 FIXME("(WINED3D_RS_PATCHSEGMENTS,%f) not yet implemented.\n", tmpvalue.f);
2981 displayed = true;
2983 break;
2986 case WINED3D_RS_DEBUGMONITORTOKEN:
2987 WARN("token: %#x.\n", state->rs[WINED3D_RS_DEBUGMONITORTOKEN]);
2988 break;
2990 case WINED3D_RS_INDEXEDVERTEXBLENDENABLE:
2991 break;
2993 case WINED3D_RS_TWEENFACTOR:
2994 break;
2996 case WINED3D_RS_POSITIONDEGREE:
2997 if (state->rs[WINED3D_RS_POSITIONDEGREE] != WINED3D_DEGREE_CUBIC)
2998 FIXME("WINED3D_RS_POSITIONDEGREE %#x not yet implemented.\n",
2999 state->rs[WINED3D_RS_POSITIONDEGREE]);
3000 break;
3002 case WINED3D_RS_NORMALDEGREE:
3003 if (state->rs[WINED3D_RS_NORMALDEGREE] != WINED3D_DEGREE_LINEAR)
3004 FIXME("WINED3D_RS_NORMALDEGREE %#x not yet implemented.\n",
3005 state->rs[WINED3D_RS_NORMALDEGREE]);
3006 break;
3008 case WINED3D_RS_MINTESSELLATIONLEVEL:
3009 break;
3011 case WINED3D_RS_MAXTESSELLATIONLEVEL:
3012 break;
3014 case WINED3D_RS_ENABLEADAPTIVETESSELLATION:
3015 if (state->rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION])
3016 FIXME("WINED3D_RS_ENABLEADAPTIVETESSELLATION %#x not yet implemented.\n",
3017 state->rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION]);
3018 break;
3020 default:
3021 wined3d_device_set_render_state(device, idx, state->rs[idx]);
3022 break;
3027 if (set_rasterizer_state)
3029 struct wined3d_rasterizer_state *rasterizer_state;
3030 struct wined3d_rasterizer_state_desc desc;
3031 struct wine_rb_entry *entry;
3032 union
3034 DWORD d;
3035 float f;
3036 } bias;
3038 memset(&desc, 0, sizeof(desc));
3039 desc.fill_mode = state->rs[WINED3D_RS_FILLMODE];
3040 desc.cull_mode = state->rs[WINED3D_RS_CULLMODE];
3041 bias.d = state->rs[WINED3D_RS_DEPTHBIAS];
3042 desc.depth_bias = bias.f;
3043 bias.d = state->rs[WINED3D_RS_SLOPESCALEDEPTHBIAS];
3044 desc.scale_bias = bias.f;
3045 desc.depth_clip = TRUE;
3046 desc.scissor = state->rs[WINED3D_RS_SCISSORTESTENABLE];
3047 desc.line_antialias = state->rs[WINED3D_RS_ANTIALIASEDLINEENABLE];
3049 if ((entry = wine_rb_get(&device->rasterizer_states, &desc)))
3051 rasterizer_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
3052 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
3054 else if (SUCCEEDED(wined3d_rasterizer_state_create(device, &desc, NULL,
3055 &wined3d_null_parent_ops, &rasterizer_state)))
3057 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
3058 if (wine_rb_put(&device->rasterizer_states, &desc, &rasterizer_state->entry) == -1)
3060 ERR("Failed to insert rasterizer state.\n");
3061 wined3d_rasterizer_state_decref(rasterizer_state);
3066 if (set_blend_state || changed->alpha_to_coverage
3067 || wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_ADAPTIVETESS_Y))
3069 struct wined3d_blend_state *blend_state;
3070 struct wined3d_blend_state_desc desc;
3071 struct wine_rb_entry *entry;
3072 struct wined3d_color colour;
3073 unsigned int sample_mask;
3075 memset(&desc, 0, sizeof(desc));
3076 desc.alpha_to_coverage = state->alpha_to_coverage;
3077 desc.independent = FALSE;
3078 if (state->rs[WINED3D_RS_ADAPTIVETESS_Y] == WINED3DFMT_ATOC)
3079 desc.alpha_to_coverage = TRUE;
3080 desc.rt[0].enable = state->rs[WINED3D_RS_ALPHABLENDENABLE];
3081 desc.rt[0].src = state->rs[WINED3D_RS_SRCBLEND];
3082 desc.rt[0].dst = state->rs[WINED3D_RS_DESTBLEND];
3083 desc.rt[0].op = state->rs[WINED3D_RS_BLENDOP];
3084 if (state->rs[WINED3D_RS_SEPARATEALPHABLENDENABLE])
3086 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLENDALPHA];
3087 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLENDALPHA];
3088 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOPALPHA];
3090 else
3092 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLEND];
3093 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLEND];
3094 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOP];
3096 desc.rt[0].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE];
3097 desc.rt[1].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE1];
3098 desc.rt[2].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE2];
3099 desc.rt[3].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE3];
3100 if (desc.rt[1].writemask != desc.rt[0].writemask
3101 || desc.rt[2].writemask != desc.rt[0].writemask
3102 || desc.rt[3].writemask != desc.rt[0].writemask)
3104 desc.independent = TRUE;
3105 for (i = 1; i < 4; ++i)
3107 desc.rt[i].enable = desc.rt[0].enable;
3108 desc.rt[i].src = desc.rt[0].src;
3109 desc.rt[i].dst = desc.rt[0].dst;
3110 desc.rt[i].op = desc.rt[0].op;
3111 desc.rt[i].src_alpha = desc.rt[0].src_alpha;
3112 desc.rt[i].dst_alpha = desc.rt[0].dst_alpha;
3113 desc.rt[i].op_alpha = desc.rt[0].op_alpha;
3117 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_BLENDFACTOR))
3118 wined3d_color_from_d3dcolor(&colour, state->rs[WINED3D_RS_BLENDFACTOR]);
3119 else
3120 wined3d_device_context_get_blend_state(context, &colour, &sample_mask);
3122 if ((entry = wine_rb_get(&device->blend_states, &desc)))
3124 blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
3125 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3126 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3128 else if (SUCCEEDED(wined3d_blend_state_create(device, &desc, NULL,
3129 &wined3d_null_parent_ops, &blend_state)))
3131 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3132 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3133 if (wine_rb_put(&device->blend_states, &desc, &blend_state->entry) == -1)
3135 ERR("Failed to insert blend state.\n");
3136 wined3d_blend_state_decref(blend_state);
3141 if (set_depth_stencil_state)
3143 struct wined3d_depth_stencil_state *depth_stencil_state;
3144 struct wined3d_depth_stencil_state_desc desc;
3145 struct wine_rb_entry *entry;
3146 unsigned int stencil_ref;
3148 memset(&desc, 0, sizeof(desc));
3149 switch (state->rs[WINED3D_RS_ZENABLE])
3151 case WINED3D_ZB_FALSE:
3152 desc.depth = FALSE;
3153 break;
3155 case WINED3D_ZB_USEW:
3156 FIXME("W buffer is not well handled.\n");
3157 case WINED3D_ZB_TRUE:
3158 desc.depth = TRUE;
3159 break;
3161 default:
3162 FIXME("Unrecognized depth buffer type %#x.\n", state->rs[WINED3D_RS_ZENABLE]);
3164 desc.depth_write = state->rs[WINED3D_RS_ZWRITEENABLE];
3165 desc.depth_func = state->rs[WINED3D_RS_ZFUNC];
3166 desc.stencil = state->rs[WINED3D_RS_STENCILENABLE];
3167 desc.stencil_read_mask = state->rs[WINED3D_RS_STENCILMASK];
3168 desc.stencil_write_mask = state->rs[WINED3D_RS_STENCILWRITEMASK];
3169 desc.front.fail_op = state->rs[WINED3D_RS_STENCILFAIL];
3170 desc.front.depth_fail_op = state->rs[WINED3D_RS_STENCILZFAIL];
3171 desc.front.pass_op = state->rs[WINED3D_RS_STENCILPASS];
3172 desc.front.func = state->rs[WINED3D_RS_STENCILFUNC];
3174 if (state->rs[WINED3D_RS_TWOSIDEDSTENCILMODE])
3176 desc.back.fail_op = state->rs[WINED3D_RS_BACK_STENCILFAIL];
3177 desc.back.depth_fail_op = state->rs[WINED3D_RS_BACK_STENCILZFAIL];
3178 desc.back.pass_op = state->rs[WINED3D_RS_BACK_STENCILPASS];
3179 desc.back.func = state->rs[WINED3D_RS_BACK_STENCILFUNC];
3181 else
3183 desc.back = desc.front;
3186 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_STENCILREF))
3187 stencil_ref = state->rs[WINED3D_RS_STENCILREF];
3188 else
3189 wined3d_device_context_get_depth_stencil_state(context, &stencil_ref);
3191 if ((entry = wine_rb_get(&device->depth_stencil_states, &desc)))
3193 depth_stencil_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
3194 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3196 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device, &desc, NULL,
3197 &wined3d_null_parent_ops, &depth_stencil_state)))
3199 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3200 if (wine_rb_put(&device->depth_stencil_states, &desc, &depth_stencil_state->entry) == -1)
3202 ERR("Failed to insert depth/stencil state.\n");
3203 wined3d_depth_stencil_state_decref(depth_stencil_state);
3208 if (set_depth_bounds)
3210 wined3d_device_context_set_depth_bounds(context,
3211 state->rs[WINED3D_RS_ADAPTIVETESS_X] == WINED3DFMT_NVDB,
3212 int_to_float(state->rs[WINED3D_RS_ADAPTIVETESS_Z]),
3213 int_to_float(state->rs[WINED3D_RS_ADAPTIVETESS_W]));
3216 for (i = 0; i < ARRAY_SIZE(changed->textureState); ++i)
3218 map = changed->textureState[i];
3219 while (map)
3221 j = wined3d_bit_scan(&map);
3222 wined3d_device_set_texture_stage_state(device, i, j, state->texture_states[i][j]);
3226 for (i = 0; i < ARRAY_SIZE(changed->samplerState); ++i)
3228 enum wined3d_shader_type shader_type = WINED3D_SHADER_TYPE_PIXEL;
3229 struct wined3d_sampler_desc desc;
3230 struct wined3d_texture *texture;
3231 struct wined3d_sampler *sampler;
3232 unsigned int bind_index = i;
3233 struct wine_rb_entry *entry;
3235 if (!changed->samplerState[i] && !(changed->textures & (1u << i)))
3236 continue;
3238 if (!(texture = state->textures[i]))
3239 continue;
3241 memset(&desc, 0, sizeof(desc));
3242 sampler_desc_from_sampler_states(&desc, state->sampler_states[i], texture);
3244 if (i >= WINED3D_VERTEX_SAMPLER_OFFSET)
3246 shader_type = WINED3D_SHADER_TYPE_VERTEX;
3247 bind_index -= WINED3D_VERTEX_SAMPLER_OFFSET;
3250 if ((entry = wine_rb_get(&device->samplers, &desc)))
3252 sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
3254 wined3d_device_context_set_samplers(context, shader_type, bind_index, 1, &sampler);
3256 else if (SUCCEEDED(wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &sampler)))
3258 wined3d_device_context_set_samplers(context, shader_type, bind_index, 1, &sampler);
3260 if (wine_rb_put(&device->samplers, &desc, &sampler->entry) == -1)
3262 ERR("Failed to insert sampler.\n");
3263 wined3d_sampler_decref(sampler);
3268 if (changed->transforms)
3270 for (i = 0; i < ARRAY_SIZE(changed->transform); ++i)
3272 map = changed->transform[i];
3273 while (map)
3275 j = wined3d_bit_scan(&map);
3276 idx = i * word_bit_count + j;
3277 wined3d_device_set_transform(device, idx, &state->transforms[idx]);
3282 if (changed->indices)
3283 wined3d_device_context_set_index_buffer(context, state->index_buffer, state->index_format, 0);
3284 wined3d_device_set_base_vertex_index(device, state->base_vertex_index);
3285 if (changed->vertexDecl)
3286 wined3d_device_context_set_vertex_declaration(context, state->vertex_declaration);
3287 if (changed->material)
3288 wined3d_device_set_material(device, &state->material);
3289 if (changed->viewport)
3290 wined3d_device_context_set_viewports(context, 1, &state->viewport);
3291 if (changed->scissorRect)
3292 wined3d_device_context_set_scissor_rects(context, 1, &state->scissor_rect);
3294 map = changed->streamSource | changed->streamFreq;
3295 while (map)
3297 i = wined3d_bit_scan(&map);
3298 wined3d_device_context_set_stream_sources(context, i, 1, &state->streams[i]);
3301 map = changed->textures;
3302 while (map)
3304 i = wined3d_bit_scan(&map);
3305 wined3d_device_set_texture(device, i, state->textures[i]);
3308 map = changed->clipplane;
3309 while (map)
3311 i = wined3d_bit_scan(&map);
3312 wined3d_device_set_clip_plane(device, i, &state->clip_planes[i]);
3315 assert(list_empty(&stateblock->changed.changed_lights));
3316 memset(&stateblock->changed, 0, sizeof(stateblock->changed));
3317 list_init(&stateblock->changed.changed_lights);
3319 TRACE("Applied stateblock %p.\n", stateblock);
3322 unsigned int CDECL wined3d_stateblock_set_texture_lod(struct wined3d_stateblock *stateblock,
3323 struct wined3d_texture *texture, unsigned int lod)
3325 struct wined3d_resource *resource;
3326 unsigned int old = texture->lod;
3328 TRACE("texture %p, lod %u.\n", texture, lod);
3330 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
3331 * textures. The call always returns 0, and GetLOD always returns 0. */
3332 resource = &texture->resource;
3333 if (!(resource->usage & WINED3DUSAGE_MANAGED))
3335 TRACE("Ignoring LOD on texture with resource access %s.\n",
3336 wined3d_debug_resource_access(resource->access));
3337 return 0;
3340 if (lod >= texture->level_count)
3341 lod = texture->level_count - 1;
3343 if (texture->lod != lod)
3345 texture->lod = lod;
3347 for (unsigned int i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
3349 /* Mark the texture as changed. The next time the appplication
3350 * draws from this texture, wined3d_device_apply_stateblock() will
3351 * recompute the texture LOD.
3353 * We only need to do this for the primary stateblock.
3354 * If a recording stateblock uses a texture whose LOD is changed,
3355 * that texture will be invalidated on the primary stateblock
3356 * anyway when the recording stateblock is applied. */
3357 if (stateblock->stateblock_state.textures[i] == texture)
3358 stateblock->changed.textures |= (1u << i);
3362 return old;