[D3D8][D3D9][DDRAW][WINED3D] Sync with Wine Staging 2.9. This work couldn't have...
[reactos.git] / reactos / dll / directx / wine / wined3d / stateblock.c
1 /*
2 * state block implementation
3 *
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 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28
29 static const DWORD pixel_states_render[] =
30 {
31 WINED3D_RS_ALPHABLENDENABLE,
32 WINED3D_RS_ALPHAFUNC,
33 WINED3D_RS_ALPHAREF,
34 WINED3D_RS_ALPHATESTENABLE,
35 WINED3D_RS_ANTIALIASEDLINEENABLE,
36 WINED3D_RS_BLENDFACTOR,
37 WINED3D_RS_BLENDOP,
38 WINED3D_RS_BLENDOPALPHA,
39 WINED3D_RS_BACK_STENCILFAIL,
40 WINED3D_RS_BACK_STENCILPASS,
41 WINED3D_RS_BACK_STENCILZFAIL,
42 WINED3D_RS_COLORWRITEENABLE,
43 WINED3D_RS_COLORWRITEENABLE1,
44 WINED3D_RS_COLORWRITEENABLE2,
45 WINED3D_RS_COLORWRITEENABLE3,
46 WINED3D_RS_DEPTHBIAS,
47 WINED3D_RS_DESTBLEND,
48 WINED3D_RS_DESTBLENDALPHA,
49 WINED3D_RS_DITHERENABLE,
50 WINED3D_RS_FILLMODE,
51 WINED3D_RS_FOGDENSITY,
52 WINED3D_RS_FOGEND,
53 WINED3D_RS_FOGSTART,
54 WINED3D_RS_LASTPIXEL,
55 WINED3D_RS_SCISSORTESTENABLE,
56 WINED3D_RS_SEPARATEALPHABLENDENABLE,
57 WINED3D_RS_SHADEMODE,
58 WINED3D_RS_SLOPESCALEDEPTHBIAS,
59 WINED3D_RS_SRCBLEND,
60 WINED3D_RS_SRCBLENDALPHA,
61 WINED3D_RS_SRGBWRITEENABLE,
62 WINED3D_RS_STENCILENABLE,
63 WINED3D_RS_STENCILFAIL,
64 WINED3D_RS_STENCILFUNC,
65 WINED3D_RS_STENCILMASK,
66 WINED3D_RS_STENCILPASS,
67 WINED3D_RS_STENCILREF,
68 WINED3D_RS_STENCILWRITEMASK,
69 WINED3D_RS_STENCILZFAIL,
70 WINED3D_RS_TEXTUREFACTOR,
71 WINED3D_RS_TWOSIDEDSTENCILMODE,
72 WINED3D_RS_WRAP0,
73 WINED3D_RS_WRAP1,
74 WINED3D_RS_WRAP10,
75 WINED3D_RS_WRAP11,
76 WINED3D_RS_WRAP12,
77 WINED3D_RS_WRAP13,
78 WINED3D_RS_WRAP14,
79 WINED3D_RS_WRAP15,
80 WINED3D_RS_WRAP2,
81 WINED3D_RS_WRAP3,
82 WINED3D_RS_WRAP4,
83 WINED3D_RS_WRAP5,
84 WINED3D_RS_WRAP6,
85 WINED3D_RS_WRAP7,
86 WINED3D_RS_WRAP8,
87 WINED3D_RS_WRAP9,
88 WINED3D_RS_ZENABLE,
89 WINED3D_RS_ZFUNC,
90 WINED3D_RS_ZWRITEENABLE,
91 };
92
93 static const DWORD pixel_states_texture[] =
94 {
95 WINED3D_TSS_ALPHA_ARG0,
96 WINED3D_TSS_ALPHA_ARG1,
97 WINED3D_TSS_ALPHA_ARG2,
98 WINED3D_TSS_ALPHA_OP,
99 WINED3D_TSS_BUMPENV_LOFFSET,
100 WINED3D_TSS_BUMPENV_LSCALE,
101 WINED3D_TSS_BUMPENV_MAT00,
102 WINED3D_TSS_BUMPENV_MAT01,
103 WINED3D_TSS_BUMPENV_MAT10,
104 WINED3D_TSS_BUMPENV_MAT11,
105 WINED3D_TSS_COLOR_ARG0,
106 WINED3D_TSS_COLOR_ARG1,
107 WINED3D_TSS_COLOR_ARG2,
108 WINED3D_TSS_COLOR_OP,
109 WINED3D_TSS_RESULT_ARG,
110 WINED3D_TSS_TEXCOORD_INDEX,
111 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,
112 };
113
114 static const DWORD pixel_states_sampler[] =
115 {
116 WINED3D_SAMP_ADDRESS_U,
117 WINED3D_SAMP_ADDRESS_V,
118 WINED3D_SAMP_ADDRESS_W,
119 WINED3D_SAMP_BORDER_COLOR,
120 WINED3D_SAMP_MAG_FILTER,
121 WINED3D_SAMP_MIN_FILTER,
122 WINED3D_SAMP_MIP_FILTER,
123 WINED3D_SAMP_MIPMAP_LOD_BIAS,
124 WINED3D_SAMP_MAX_MIP_LEVEL,
125 WINED3D_SAMP_MAX_ANISOTROPY,
126 WINED3D_SAMP_SRGB_TEXTURE,
127 WINED3D_SAMP_ELEMENT_INDEX,
128 };
129
130 static const DWORD vertex_states_render[] =
131 {
132 WINED3D_RS_ADAPTIVETESS_W,
133 WINED3D_RS_ADAPTIVETESS_X,
134 WINED3D_RS_ADAPTIVETESS_Y,
135 WINED3D_RS_ADAPTIVETESS_Z,
136 WINED3D_RS_AMBIENT,
137 WINED3D_RS_AMBIENTMATERIALSOURCE,
138 WINED3D_RS_CLIPPING,
139 WINED3D_RS_CLIPPLANEENABLE,
140 WINED3D_RS_COLORVERTEX,
141 WINED3D_RS_CULLMODE,
142 WINED3D_RS_DIFFUSEMATERIALSOURCE,
143 WINED3D_RS_EMISSIVEMATERIALSOURCE,
144 WINED3D_RS_ENABLEADAPTIVETESSELLATION,
145 WINED3D_RS_FOGCOLOR,
146 WINED3D_RS_FOGDENSITY,
147 WINED3D_RS_FOGENABLE,
148 WINED3D_RS_FOGEND,
149 WINED3D_RS_FOGSTART,
150 WINED3D_RS_FOGTABLEMODE,
151 WINED3D_RS_FOGVERTEXMODE,
152 WINED3D_RS_INDEXEDVERTEXBLENDENABLE,
153 WINED3D_RS_LIGHTING,
154 WINED3D_RS_LOCALVIEWER,
155 WINED3D_RS_MAXTESSELLATIONLEVEL,
156 WINED3D_RS_MINTESSELLATIONLEVEL,
157 WINED3D_RS_MULTISAMPLEANTIALIAS,
158 WINED3D_RS_MULTISAMPLEMASK,
159 WINED3D_RS_NORMALDEGREE,
160 WINED3D_RS_NORMALIZENORMALS,
161 WINED3D_RS_PATCHEDGESTYLE,
162 WINED3D_RS_POINTSCALE_A,
163 WINED3D_RS_POINTSCALE_B,
164 WINED3D_RS_POINTSCALE_C,
165 WINED3D_RS_POINTSCALEENABLE,
166 WINED3D_RS_POINTSIZE,
167 WINED3D_RS_POINTSIZE_MAX,
168 WINED3D_RS_POINTSIZE_MIN,
169 WINED3D_RS_POINTSPRITEENABLE,
170 WINED3D_RS_POSITIONDEGREE,
171 WINED3D_RS_RANGEFOGENABLE,
172 WINED3D_RS_SHADEMODE,
173 WINED3D_RS_SPECULARENABLE,
174 WINED3D_RS_SPECULARMATERIALSOURCE,
175 WINED3D_RS_TWEENFACTOR,
176 WINED3D_RS_VERTEXBLEND,
177 };
178
179 static const DWORD vertex_states_texture[] =
180 {
181 WINED3D_TSS_TEXCOORD_INDEX,
182 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,
183 };
184
185 static const DWORD vertex_states_sampler[] =
186 {
187 WINED3D_SAMP_DMAP_OFFSET,
188 };
189
190 static inline void stateblock_set_bits(DWORD *map, UINT map_size)
191 {
192 DWORD mask = (1u << (map_size & 0x1f)) - 1;
193 memset(map, 0xff, (map_size >> 5) * sizeof(*map));
194 if (mask) map[map_size >> 5] = mask;
195 }
196
197 /* Set all members of a stateblock savedstate to the given value */
198 static void stateblock_savedstates_set_all(struct wined3d_saved_states *states, DWORD vs_consts, DWORD ps_consts)
199 {
200 unsigned int i;
201
202 /* Single values */
203 states->indices = 1;
204 states->material = 1;
205 states->viewport = 1;
206 states->vertexDecl = 1;
207 states->pixelShader = 1;
208 states->vertexShader = 1;
209 states->scissorRect = 1;
210
211 /* Fixed size arrays */
212 states->streamSource = 0xffff;
213 states->streamFreq = 0xffff;
214 states->textures = 0xfffff;
215 stateblock_set_bits(states->transform, HIGHEST_TRANSFORMSTATE + 1);
216 stateblock_set_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
217 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
218 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3ffe;
219 states->clipplane = 0xffffffff;
220 states->pixelShaderConstantsB = 0xffff;
221 states->pixelShaderConstantsI = 0xffff;
222 states->vertexShaderConstantsB = 0xffff;
223 states->vertexShaderConstantsI = 0xffff;
224
225 /* Dynamically sized arrays */
226 memset(states->ps_consts_f, TRUE, sizeof(BOOL) * ps_consts);
227 memset(states->vs_consts_f, TRUE, sizeof(BOOL) * vs_consts);
228 }
229
230 static void stateblock_savedstates_set_pixel(struct wined3d_saved_states *states, const DWORD num_constants)
231 {
232 DWORD texture_mask = 0;
233 WORD sampler_mask = 0;
234 unsigned int i;
235
236 states->pixelShader = 1;
237
238 for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
239 {
240 DWORD rs = pixel_states_render[i];
241 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
242 }
243
244 for (i = 0; i < sizeof(pixel_states_texture) / sizeof(*pixel_states_texture); ++i)
245 texture_mask |= 1u << pixel_states_texture[i];
246 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
247 for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
248 sampler_mask |= 1u << pixel_states_sampler[i];
249 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
250 states->pixelShaderConstantsB = 0xffff;
251 states->pixelShaderConstantsI = 0xffff;
252
253 memset(states->ps_consts_f, TRUE, sizeof(BOOL) * num_constants);
254 }
255
256 static void stateblock_savedstates_set_vertex(struct wined3d_saved_states *states, const DWORD num_constants)
257 {
258 DWORD texture_mask = 0;
259 WORD sampler_mask = 0;
260 unsigned int i;
261
262 states->vertexDecl = 1;
263 states->vertexShader = 1;
264
265 for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
266 {
267 DWORD rs = vertex_states_render[i];
268 states->renderState[rs >> 5] |= 1u << (rs & 0x1f);
269 }
270
271 for (i = 0; i < sizeof(vertex_states_texture) / sizeof(*vertex_states_texture); ++i)
272 texture_mask |= 1u << vertex_states_texture[i];
273 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
274 for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
275 sampler_mask |= 1u << vertex_states_sampler[i];
276 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
277 states->vertexShaderConstantsB = 0xffff;
278 states->vertexShaderConstantsI = 0xffff;
279
280 memset(states->vs_consts_f, TRUE, sizeof(BOOL) * num_constants);
281 }
282
283 void stateblock_init_contained_states(struct wined3d_stateblock *stateblock)
284 {
285 const struct wined3d_d3d_info *d3d_info = &stateblock->device->adapter->d3d_info;
286 unsigned int i, j;
287
288 for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
289 {
290 DWORD map = stateblock->changed.renderState[i];
291 for (j = 0; map; map >>= 1, ++j)
292 {
293 if (!(map & 1)) continue;
294
295 stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
296 ++stateblock->num_contained_render_states;
297 }
298 }
299
300 for (i = 0; i <= HIGHEST_TRANSFORMSTATE >> 5; ++i)
301 {
302 DWORD map = stateblock->changed.transform[i];
303 for (j = 0; map; map >>= 1, ++j)
304 {
305 if (!(map & 1)) continue;
306
307 stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
308 ++stateblock->num_contained_transform_states;
309 }
310 }
311
312 for (i = 0; i < d3d_info->limits.vs_uniform_count; ++i)
313 {
314 if (stateblock->changed.vs_consts_f[i])
315 {
316 stateblock->contained_vs_consts_f[stateblock->num_contained_vs_consts_f] = i;
317 ++stateblock->num_contained_vs_consts_f;
318 }
319 }
320
321 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
322 {
323 if (stateblock->changed.vertexShaderConstantsI & (1u << i))
324 {
325 stateblock->contained_vs_consts_i[stateblock->num_contained_vs_consts_i] = i;
326 ++stateblock->num_contained_vs_consts_i;
327 }
328 }
329
330 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
331 {
332 if (stateblock->changed.vertexShaderConstantsB & (1u << i))
333 {
334 stateblock->contained_vs_consts_b[stateblock->num_contained_vs_consts_b] = i;
335 ++stateblock->num_contained_vs_consts_b;
336 }
337 }
338
339 for (i = 0; i < d3d_info->limits.ps_uniform_count; ++i)
340 {
341 if (stateblock->changed.ps_consts_f[i])
342 {
343 stateblock->contained_ps_consts_f[stateblock->num_contained_ps_consts_f] = i;
344 ++stateblock->num_contained_ps_consts_f;
345 }
346 }
347
348 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
349 {
350 if (stateblock->changed.pixelShaderConstantsI & (1u << i))
351 {
352 stateblock->contained_ps_consts_i[stateblock->num_contained_ps_consts_i] = i;
353 ++stateblock->num_contained_ps_consts_i;
354 }
355 }
356
357 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
358 {
359 if (stateblock->changed.pixelShaderConstantsB & (1u << i))
360 {
361 stateblock->contained_ps_consts_b[stateblock->num_contained_ps_consts_b] = i;
362 ++stateblock->num_contained_ps_consts_b;
363 }
364 }
365
366 for (i = 0; i < MAX_TEXTURES; ++i)
367 {
368 DWORD map = stateblock->changed.textureState[i];
369
370 for(j = 0; map; map >>= 1, ++j)
371 {
372 if (!(map & 1)) continue;
373
374 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
375 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
376 ++stateblock->num_contained_tss_states;
377 }
378 }
379
380 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
381 {
382 DWORD map = stateblock->changed.samplerState[i];
383
384 for (j = 0; map; map >>= 1, ++j)
385 {
386 if (!(map & 1)) continue;
387
388 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
389 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
390 ++stateblock->num_contained_sampler_states;
391 }
392 }
393 }
394
395 static void stateblock_init_lights(struct wined3d_stateblock *stateblock, struct list *light_map)
396 {
397 unsigned int i;
398
399 for (i = 0; i < LIGHTMAP_SIZE; ++i)
400 {
401 const struct wined3d_light_info *src_light;
402
403 LIST_FOR_EACH_ENTRY(src_light, &light_map[i], struct wined3d_light_info, entry)
404 {
405 struct wined3d_light_info *dst_light = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_light));
406
407 *dst_light = *src_light;
408 list_add_tail(&stateblock->state.light_map[i], &dst_light->entry);
409 }
410 }
411 }
412
413 ULONG CDECL wined3d_stateblock_incref(struct wined3d_stateblock *stateblock)
414 {
415 ULONG refcount = InterlockedIncrement(&stateblock->ref);
416
417 TRACE("%p increasing refcount to %u.\n", stateblock, refcount);
418
419 return refcount;
420 }
421
422 void state_unbind_resources(struct wined3d_state *state)
423 {
424 struct wined3d_unordered_access_view *uav;
425 struct wined3d_shader_resource_view *srv;
426 struct wined3d_vertex_declaration *decl;
427 struct wined3d_sampler *sampler;
428 struct wined3d_texture *texture;
429 struct wined3d_buffer *buffer;
430 struct wined3d_shader *shader;
431 unsigned int i, j;
432
433 if ((decl = state->vertex_declaration))
434 {
435 state->vertex_declaration = NULL;
436 wined3d_vertex_declaration_decref(decl);
437 }
438
439 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
440 {
441 if ((texture = state->textures[i]))
442 {
443 state->textures[i] = NULL;
444 wined3d_texture_decref(texture);
445 }
446 }
447
448 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
449 {
450 if ((buffer = state->stream_output[i].buffer))
451 {
452 state->stream_output[i].buffer = NULL;
453 wined3d_buffer_decref(buffer);
454 }
455 }
456
457 for (i = 0; i < MAX_STREAMS; ++i)
458 {
459 if ((buffer = state->streams[i].buffer))
460 {
461 state->streams[i].buffer = NULL;
462 wined3d_buffer_decref(buffer);
463 }
464 }
465
466 if ((buffer = state->index_buffer))
467 {
468 state->index_buffer = NULL;
469 wined3d_buffer_decref(buffer);
470 }
471
472 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
473 {
474 if ((shader = state->shader[i]))
475 {
476 state->shader[i] = NULL;
477 wined3d_shader_decref(shader);
478 }
479
480 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
481 {
482 if ((buffer = state->cb[i][j]))
483 {
484 state->cb[i][j] = NULL;
485 wined3d_buffer_decref(buffer);
486 }
487 }
488
489 for (j = 0; j < MAX_SAMPLER_OBJECTS; ++j)
490 {
491 if ((sampler = state->sampler[i][j]))
492 {
493 state->sampler[i][j] = NULL;
494 wined3d_sampler_decref(sampler);
495 }
496 }
497
498 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
499 {
500 if ((srv = state->shader_resource_view[i][j]))
501 {
502 state->shader_resource_view[i][j] = NULL;
503 wined3d_shader_resource_view_decref(srv);
504 }
505 }
506 }
507
508 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
509 {
510 for (j = 0; j < MAX_UNORDERED_ACCESS_VIEWS; ++j)
511 {
512 if ((uav = state->unordered_access_view[i][j]))
513 {
514 state->unordered_access_view[i][j] = NULL;
515 wined3d_unordered_access_view_decref(uav);
516 }
517 }
518 }
519 }
520
521 void state_cleanup(struct wined3d_state *state)
522 {
523 unsigned int counter;
524
525 if (!(state->flags & WINED3D_STATE_NO_REF))
526 state_unbind_resources(state);
527
528 for (counter = 0; counter < MAX_ACTIVE_LIGHTS; ++counter)
529 {
530 state->lights[counter] = NULL;
531 }
532
533 for (counter = 0; counter < LIGHTMAP_SIZE; ++counter)
534 {
535 struct list *e1, *e2;
536 LIST_FOR_EACH_SAFE(e1, e2, &state->light_map[counter])
537 {
538 struct wined3d_light_info *light = LIST_ENTRY(e1, struct wined3d_light_info, entry);
539 list_remove(&light->entry);
540 HeapFree(GetProcessHeap(), 0, light);
541 }
542 }
543 }
544
545 ULONG CDECL wined3d_stateblock_decref(struct wined3d_stateblock *stateblock)
546 {
547 ULONG refcount = InterlockedDecrement(&stateblock->ref);
548
549 TRACE("%p decreasing refcount to %u\n", stateblock, refcount);
550
551 if (!refcount)
552 {
553 state_cleanup(&stateblock->state);
554 HeapFree(GetProcessHeap(), 0, stateblock);
555 }
556
557 return refcount;
558 }
559
560 struct wined3d_light_info *wined3d_state_get_light(const struct wined3d_state *state, unsigned int idx)
561 {
562 struct wined3d_light_info *light_info;
563 unsigned int hash_idx;
564
565 hash_idx = LIGHTMAP_HASHFUNC(idx);
566 LIST_FOR_EACH_ENTRY(light_info, &state->light_map[hash_idx], struct wined3d_light_info, entry)
567 {
568 if (light_info->OriginalIndex == idx)
569 return light_info;
570 }
571
572 return NULL;
573 }
574
575 void wined3d_state_enable_light(struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info,
576 struct wined3d_light_info *light_info, BOOL enable)
577 {
578 unsigned int light_count, i;
579
580 if (!(light_info->enabled = enable))
581 {
582 if (light_info->glIndex == -1)
583 {
584 TRACE("Light already disabled, nothing to do.\n");
585 return;
586 }
587
588 state->lights[light_info->glIndex] = NULL;
589 light_info->glIndex = -1;
590 return;
591 }
592
593 if (light_info->glIndex != -1)
594 {
595 TRACE("Light already enabled, nothing to do.\n");
596 return;
597 }
598
599 /* Find a free light. */
600 light_count = d3d_info->limits.active_light_count;
601 for (i = 0; i < light_count; ++i)
602 {
603 if (state->lights[i])
604 continue;
605
606 state->lights[i] = light_info;
607 light_info->glIndex = i;
608 return;
609 }
610
611 /* Our tests show that Windows returns D3D_OK in this situation, even with
612 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices.
613 * This is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns
614 * TRUE * as well for those lights.
615 *
616 * TODO: Test how this affects rendering. */
617 WARN("Too many concurrently active lights.\n");
618 }
619
620 static void wined3d_state_record_lights(struct wined3d_state *dst_state, const struct wined3d_state *src_state)
621 {
622 UINT i;
623
624 /* Lights... For a recorded state block, we just had a chain of actions
625 * to perform, so we need to walk that chain and update any actions which
626 * differ. */
627 for (i = 0; i < LIGHTMAP_SIZE; ++i)
628 {
629 struct list *e, *f;
630 LIST_FOR_EACH(e, &dst_state->light_map[i])
631 {
632 BOOL updated = FALSE;
633 struct wined3d_light_info *src = LIST_ENTRY(e, struct wined3d_light_info, entry), *realLight;
634
635 /* Look up the light in the destination */
636 LIST_FOR_EACH(f, &src_state->light_map[i])
637 {
638 realLight = LIST_ENTRY(f, struct wined3d_light_info, entry);
639 if (realLight->OriginalIndex == src->OriginalIndex)
640 {
641 src->OriginalParms = realLight->OriginalParms;
642
643 if (realLight->glIndex == -1 && src->glIndex != -1)
644 {
645 /* Light disabled */
646 dst_state->lights[src->glIndex] = NULL;
647 }
648 else if (realLight->glIndex != -1 && src->glIndex == -1)
649 {
650 /* Light enabled */
651 dst_state->lights[realLight->glIndex] = src;
652 }
653 src->glIndex = realLight->glIndex;
654 updated = TRUE;
655 break;
656 }
657 }
658
659 if (!updated)
660 {
661 /* This can happen if the light was originally created as a
662 * default light for SetLightEnable() while recording. */
663 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
664 src->OriginalIndex, dst_state, src_state);
665
666 src->OriginalParms = WINED3D_default_light;
667 if (src->glIndex != -1)
668 {
669 dst_state->lights[src->glIndex] = NULL;
670 src->glIndex = -1;
671 }
672 }
673 }
674 }
675 }
676
677 void CDECL wined3d_stateblock_capture(struct wined3d_stateblock *stateblock)
678 {
679 const struct wined3d_state *src_state = &stateblock->device->state;
680 unsigned int i;
681 DWORD map;
682
683 TRACE("stateblock %p.\n", stateblock);
684
685 TRACE("Capturing state %p.\n", src_state);
686
687 if (stateblock->changed.vertexShader && stateblock->state.shader[WINED3D_SHADER_TYPE_VERTEX]
688 != src_state->shader[WINED3D_SHADER_TYPE_VERTEX])
689 {
690 TRACE("Updating vertex shader from %p to %p\n",
691 stateblock->state.shader[WINED3D_SHADER_TYPE_VERTEX],
692 src_state->shader[WINED3D_SHADER_TYPE_VERTEX]);
693
694 if (src_state->shader[WINED3D_SHADER_TYPE_VERTEX])
695 wined3d_shader_incref(src_state->shader[WINED3D_SHADER_TYPE_VERTEX]);
696 if (stateblock->state.shader[WINED3D_SHADER_TYPE_VERTEX])
697 wined3d_shader_decref(stateblock->state.shader[WINED3D_SHADER_TYPE_VERTEX]);
698 stateblock->state.shader[WINED3D_SHADER_TYPE_VERTEX] = src_state->shader[WINED3D_SHADER_TYPE_VERTEX];
699 }
700
701 /* Vertex shader float constants. */
702 for (i = 0; i < stateblock->num_contained_vs_consts_f; ++i)
703 {
704 unsigned int idx = stateblock->contained_vs_consts_f[i];
705
706 TRACE("Setting vs_consts_f[%u] to %s.\n", idx, debug_vec4(&src_state->vs_consts_f[idx]));
707
708 stateblock->state.vs_consts_f[idx] = src_state->vs_consts_f[idx];
709 }
710
711 /* Vertex shader integer constants. */
712 for (i = 0; i < stateblock->num_contained_vs_consts_i; ++i)
713 {
714 unsigned int idx = stateblock->contained_vs_consts_i[i];
715
716 TRACE("Setting vs_consts[%u] to %s.\n", idx, debug_ivec4(&src_state->vs_consts_i[idx]));
717
718 stateblock->state.vs_consts_i[idx] = src_state->vs_consts_i[idx];
719 }
720
721 /* Vertex shader boolean constants. */
722 for (i = 0; i < stateblock->num_contained_vs_consts_b; ++i)
723 {
724 unsigned int idx = stateblock->contained_vs_consts_b[i];
725
726 TRACE("Setting vs_consts_b[%u] to %s.\n",
727 idx, src_state->vs_consts_b[idx] ? "TRUE" : "FALSE");
728
729 stateblock->state.vs_consts_b[idx] = src_state->vs_consts_b[idx];
730 }
731
732 /* Pixel shader float constants. */
733 for (i = 0; i < stateblock->num_contained_ps_consts_f; ++i)
734 {
735 unsigned int idx = stateblock->contained_ps_consts_f[i];
736
737 TRACE("Setting ps_consts_f[%u] to %s.\n", idx, debug_vec4(&src_state->ps_consts_f[idx]));
738
739 stateblock->state.ps_consts_f[idx] = src_state->ps_consts_f[idx];
740 }
741
742 /* Pixel shader integer constants. */
743 for (i = 0; i < stateblock->num_contained_ps_consts_i; ++i)
744 {
745 unsigned int idx = stateblock->contained_ps_consts_i[i];
746
747 TRACE("Setting ps_consts_i[%u] to %s.\n", idx, debug_ivec4(&src_state->ps_consts_i[idx]));
748
749 stateblock->state.ps_consts_i[idx] = src_state->ps_consts_i[idx];
750 }
751
752 /* Pixel shader boolean constants. */
753 for (i = 0; i < stateblock->num_contained_ps_consts_b; ++i)
754 {
755 unsigned int idx = stateblock->contained_ps_consts_b[i];
756 TRACE("Setting ps_consts_b[%u] to %s.\n",
757 idx, src_state->ps_consts_b[idx] ? "TRUE" : "FALSE");
758
759 stateblock->state.ps_consts_b[idx] = src_state->ps_consts_b[idx];
760 }
761
762 /* Others + Render & Texture */
763 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
764 {
765 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
766
767 TRACE("Updating transform %#x.\n", transform);
768
769 stateblock->state.transforms[transform] = src_state->transforms[transform];
770 }
771
772 if (stateblock->changed.indices
773 && ((stateblock->state.index_buffer != src_state->index_buffer)
774 || (stateblock->state.base_vertex_index != src_state->base_vertex_index)
775 || (stateblock->state.index_format != src_state->index_format)
776 || (stateblock->state.index_offset != src_state->index_offset)))
777 {
778 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
779 src_state->index_buffer, src_state->base_vertex_index);
780
781 if (src_state->index_buffer)
782 wined3d_buffer_incref(src_state->index_buffer);
783 if (stateblock->state.index_buffer)
784 wined3d_buffer_decref(stateblock->state.index_buffer);
785 stateblock->state.index_buffer = src_state->index_buffer;
786 stateblock->state.base_vertex_index = src_state->base_vertex_index;
787 stateblock->state.index_format = src_state->index_format;
788 stateblock->state.index_offset = src_state->index_offset;
789 }
790
791 if (stateblock->changed.vertexDecl && stateblock->state.vertex_declaration != src_state->vertex_declaration)
792 {
793 TRACE("Updating vertex declaration from %p to %p.\n",
794 stateblock->state.vertex_declaration, src_state->vertex_declaration);
795
796 if (src_state->vertex_declaration)
797 wined3d_vertex_declaration_incref(src_state->vertex_declaration);
798 if (stateblock->state.vertex_declaration)
799 wined3d_vertex_declaration_decref(stateblock->state.vertex_declaration);
800 stateblock->state.vertex_declaration = src_state->vertex_declaration;
801 }
802
803 if (stateblock->changed.material
804 && memcmp(&src_state->material, &stateblock->state.material, sizeof(stateblock->state.material)))
805 {
806 TRACE("Updating material.\n");
807
808 stateblock->state.material = src_state->material;
809 }
810
811 if (stateblock->changed.viewport
812 && memcmp(&src_state->viewport, &stateblock->state.viewport, sizeof(stateblock->state.viewport)))
813 {
814 TRACE("Updating viewport.\n");
815
816 stateblock->state.viewport = src_state->viewport;
817 }
818
819 if (stateblock->changed.scissorRect && memcmp(&src_state->scissor_rect,
820 &stateblock->state.scissor_rect, sizeof(stateblock->state.scissor_rect)))
821 {
822 TRACE("Updating scissor rect.\n");
823
824 stateblock->state.scissor_rect = src_state->scissor_rect;
825 }
826
827 map = stateblock->changed.streamSource;
828 for (i = 0; map; map >>= 1, ++i)
829 {
830 if (!(map & 1)) continue;
831
832 if (stateblock->state.streams[i].stride != src_state->streams[i].stride
833 || stateblock->state.streams[i].buffer != src_state->streams[i].buffer)
834 {
835 TRACE("Updating stream source %u to %p, stride to %u.\n",
836 i, src_state->streams[i].buffer,
837 src_state->streams[i].stride);
838
839 stateblock->state.streams[i].stride = src_state->streams[i].stride;
840 if (src_state->streams[i].buffer)
841 wined3d_buffer_incref(src_state->streams[i].buffer);
842 if (stateblock->state.streams[i].buffer)
843 wined3d_buffer_decref(stateblock->state.streams[i].buffer);
844 stateblock->state.streams[i].buffer = src_state->streams[i].buffer;
845 }
846 }
847
848 map = stateblock->changed.streamFreq;
849 for (i = 0; map; map >>= 1, ++i)
850 {
851 if (!(map & 1)) continue;
852
853 if (stateblock->state.streams[i].frequency != src_state->streams[i].frequency
854 || stateblock->state.streams[i].flags != src_state->streams[i].flags)
855 {
856 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
857 i, src_state->streams[i].frequency, src_state->streams[i].flags);
858
859 stateblock->state.streams[i].frequency = src_state->streams[i].frequency;
860 stateblock->state.streams[i].flags = src_state->streams[i].flags;
861 }
862 }
863
864 map = stateblock->changed.clipplane;
865 for (i = 0; map; map >>= 1, ++i)
866 {
867 if (!(map & 1)) continue;
868
869 if (memcmp(&stateblock->state.clip_planes[i], &src_state->clip_planes[i], sizeof(src_state->clip_planes[i])))
870 {
871 TRACE("Updating clipplane %u.\n", i);
872 stateblock->state.clip_planes[i] = src_state->clip_planes[i];
873 }
874 }
875
876 /* Render */
877 for (i = 0; i < stateblock->num_contained_render_states; ++i)
878 {
879 enum wined3d_render_state rs = stateblock->contained_render_states[i];
880
881 TRACE("Updating render state %#x to %u.\n", rs, src_state->render_states[rs]);
882
883 stateblock->state.render_states[rs] = src_state->render_states[rs];
884 }
885
886 /* Texture states */
887 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
888 {
889 DWORD stage = stateblock->contained_tss_states[i].stage;
890 DWORD state = stateblock->contained_tss_states[i].state;
891
892 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage, state,
893 src_state->texture_states[stage][state], stateblock->state.texture_states[stage][state]);
894
895 stateblock->state.texture_states[stage][state] = src_state->texture_states[stage][state];
896 }
897
898 /* Samplers */
899 map = stateblock->changed.textures;
900 for (i = 0; map; map >>= 1, ++i)
901 {
902 if (!(map & 1)) continue;
903
904 TRACE("Updating texture %u to %p (was %p).\n",
905 i, src_state->textures[i], stateblock->state.textures[i]);
906
907 if (src_state->textures[i])
908 wined3d_texture_incref(src_state->textures[i]);
909 if (stateblock->state.textures[i])
910 wined3d_texture_decref(stateblock->state.textures[i]);
911 stateblock->state.textures[i] = src_state->textures[i];
912 }
913
914 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
915 {
916 DWORD stage = stateblock->contained_sampler_states[i].stage;
917 DWORD state = stateblock->contained_sampler_states[i].state;
918
919 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage, state,
920 src_state->sampler_states[stage][state], stateblock->state.sampler_states[stage][state]);
921
922 stateblock->state.sampler_states[stage][state] = src_state->sampler_states[stage][state];
923 }
924
925 if (stateblock->changed.pixelShader && stateblock->state.shader[WINED3D_SHADER_TYPE_PIXEL]
926 != src_state->shader[WINED3D_SHADER_TYPE_PIXEL])
927 {
928 if (src_state->shader[WINED3D_SHADER_TYPE_PIXEL])
929 wined3d_shader_incref(src_state->shader[WINED3D_SHADER_TYPE_PIXEL]);
930 if (stateblock->state.shader[WINED3D_SHADER_TYPE_PIXEL])
931 wined3d_shader_decref(stateblock->state.shader[WINED3D_SHADER_TYPE_PIXEL]);
932 stateblock->state.shader[WINED3D_SHADER_TYPE_PIXEL] = src_state->shader[WINED3D_SHADER_TYPE_PIXEL];
933 }
934
935 wined3d_state_record_lights(&stateblock->state, src_state);
936
937 TRACE("Capture done.\n");
938 }
939
940 static void apply_lights(struct wined3d_device *device, const struct wined3d_state *state)
941 {
942 UINT i;
943
944 for (i = 0; i < LIGHTMAP_SIZE; ++i)
945 {
946 struct list *e;
947
948 LIST_FOR_EACH(e, &state->light_map[i])
949 {
950 const struct wined3d_light_info *light = LIST_ENTRY(e, struct wined3d_light_info, entry);
951
952 wined3d_device_set_light(device, light->OriginalIndex, &light->OriginalParms);
953 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
954 }
955 }
956 }
957
958 void CDECL wined3d_stateblock_apply(const struct wined3d_stateblock *stateblock)
959 {
960 struct wined3d_device *device = stateblock->device;
961 unsigned int i;
962 DWORD map;
963
964 TRACE("Applying stateblock %p to device %p.\n", stateblock, device);
965
966 if (stateblock->changed.vertexShader)
967 wined3d_device_set_vertex_shader(device, stateblock->state.shader[WINED3D_SHADER_TYPE_VERTEX]);
968
969 /* Vertex Shader Constants. */
970 for (i = 0; i < stateblock->num_contained_vs_consts_f; ++i)
971 {
972 wined3d_device_set_vs_consts_f(device, stateblock->contained_vs_consts_f[i],
973 1, &stateblock->state.vs_consts_f[stateblock->contained_vs_consts_f[i]]);
974 }
975 for (i = 0; i < stateblock->num_contained_vs_consts_i; ++i)
976 {
977 wined3d_device_set_vs_consts_i(device, stateblock->contained_vs_consts_i[i],
978 1, &stateblock->state.vs_consts_i[stateblock->contained_vs_consts_i[i]]);
979 }
980 for (i = 0; i < stateblock->num_contained_vs_consts_b; ++i)
981 {
982 wined3d_device_set_vs_consts_b(device, stateblock->contained_vs_consts_b[i],
983 1, &stateblock->state.vs_consts_b[stateblock->contained_vs_consts_b[i]]);
984 }
985
986 apply_lights(device, &stateblock->state);
987
988 if (stateblock->changed.pixelShader)
989 wined3d_device_set_pixel_shader(device, stateblock->state.shader[WINED3D_SHADER_TYPE_PIXEL]);
990
991 /* Pixel Shader Constants. */
992 for (i = 0; i < stateblock->num_contained_ps_consts_f; ++i)
993 {
994 wined3d_device_set_ps_consts_f(device, stateblock->contained_ps_consts_f[i],
995 1, &stateblock->state.ps_consts_f[stateblock->contained_ps_consts_f[i]]);
996 }
997 for (i = 0; i < stateblock->num_contained_ps_consts_i; ++i)
998 {
999 wined3d_device_set_ps_consts_i(device, stateblock->contained_ps_consts_i[i],
1000 1, &stateblock->state.ps_consts_i[stateblock->contained_ps_consts_i[i]]);
1001 }
1002 for (i = 0; i < stateblock->num_contained_ps_consts_b; ++i)
1003 {
1004 wined3d_device_set_ps_consts_b(device, stateblock->contained_ps_consts_b[i],
1005 1, &stateblock->state.ps_consts_b[stateblock->contained_ps_consts_b[i]]);
1006 }
1007
1008 /* Render states. */
1009 for (i = 0; i < stateblock->num_contained_render_states; ++i)
1010 {
1011 wined3d_device_set_render_state(device, stateblock->contained_render_states[i],
1012 stateblock->state.render_states[stateblock->contained_render_states[i]]);
1013 }
1014
1015 /* Texture states. */
1016 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
1017 {
1018 DWORD stage = stateblock->contained_tss_states[i].stage;
1019 DWORD state = stateblock->contained_tss_states[i].state;
1020
1021 wined3d_device_set_texture_stage_state(device, stage, state, stateblock->state.texture_states[stage][state]);
1022 }
1023
1024 /* Sampler states. */
1025 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
1026 {
1027 DWORD stage = stateblock->contained_sampler_states[i].stage;
1028 DWORD state = stateblock->contained_sampler_states[i].state;
1029 DWORD value = stateblock->state.sampler_states[stage][state];
1030
1031 if (stage >= MAX_FRAGMENT_SAMPLERS) stage += WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS;
1032 wined3d_device_set_sampler_state(device, stage, state, value);
1033 }
1034
1035 /* Transform states. */
1036 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
1037 {
1038 wined3d_device_set_transform(device, stateblock->contained_transform_states[i],
1039 &stateblock->state.transforms[stateblock->contained_transform_states[i]]);
1040 }
1041
1042 if (stateblock->changed.indices)
1043 {
1044 wined3d_device_set_index_buffer(device, stateblock->state.index_buffer,
1045 stateblock->state.index_format, stateblock->state.index_offset);
1046 wined3d_device_set_base_vertex_index(device, stateblock->state.base_vertex_index);
1047 }
1048
1049 if (stateblock->changed.vertexDecl && stateblock->state.vertex_declaration)
1050 wined3d_device_set_vertex_declaration(device, stateblock->state.vertex_declaration);
1051
1052 if (stateblock->changed.material)
1053 wined3d_device_set_material(device, &stateblock->state.material);
1054
1055 if (stateblock->changed.viewport)
1056 wined3d_device_set_viewport(device, &stateblock->state.viewport);
1057
1058 if (stateblock->changed.scissorRect)
1059 wined3d_device_set_scissor_rect(device, &stateblock->state.scissor_rect);
1060
1061 map = stateblock->changed.streamSource;
1062 for (i = 0; map; map >>= 1, ++i)
1063 {
1064 if (map & 1)
1065 wined3d_device_set_stream_source(device, i,
1066 stateblock->state.streams[i].buffer,
1067 0, stateblock->state.streams[i].stride);
1068 }
1069
1070 map = stateblock->changed.streamFreq;
1071 for (i = 0; map; map >>= 1, ++i)
1072 {
1073 if (map & 1)
1074 wined3d_device_set_stream_source_freq(device, i,
1075 stateblock->state.streams[i].frequency | stateblock->state.streams[i].flags);
1076 }
1077
1078 map = stateblock->changed.textures;
1079 for (i = 0; map; map >>= 1, ++i)
1080 {
1081 DWORD stage;
1082
1083 if (!(map & 1)) continue;
1084
1085 stage = i < MAX_FRAGMENT_SAMPLERS ? i : WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS;
1086 wined3d_device_set_texture(device, stage, stateblock->state.textures[i]);
1087 }
1088
1089 map = stateblock->changed.clipplane;
1090 for (i = 0; map; map >>= 1, ++i)
1091 {
1092 if (!(map & 1)) continue;
1093
1094 wined3d_device_set_clip_plane(device, i, &stateblock->state.clip_planes[i]);
1095 }
1096
1097 TRACE("Applied stateblock %p.\n", stateblock);
1098 }
1099
1100 static void state_init_default(struct wined3d_state *state, const struct wined3d_gl_info *gl_info)
1101 {
1102 union
1103 {
1104 struct wined3d_line_pattern lp;
1105 DWORD d;
1106 } lp;
1107 union {
1108 float f;
1109 DWORD d;
1110 } tmpfloat;
1111 unsigned int i;
1112 struct wined3d_matrix identity;
1113
1114 TRACE("state %p, gl_info %p.\n", state, gl_info);
1115
1116 get_identity_matrix(&identity);
1117 state->gl_primitive_type = ~0u;
1118 state->gl_patch_vertices = 0;
1119
1120 /* Set some of the defaults for lights, transforms etc */
1121 state->transforms[WINED3D_TS_PROJECTION] = identity;
1122 state->transforms[WINED3D_TS_VIEW] = identity;
1123 for (i = 0; i < 256; ++i)
1124 {
1125 state->transforms[WINED3D_TS_WORLD_MATRIX(i)] = identity;
1126 }
1127
1128 TRACE("Render states\n");
1129 /* Render states: */
1130 state->render_states[WINED3D_RS_ZENABLE] = WINED3D_ZB_TRUE;
1131 state->render_states[WINED3D_RS_FILLMODE] = WINED3D_FILL_SOLID;
1132 state->render_states[WINED3D_RS_SHADEMODE] = WINED3D_SHADE_GOURAUD;
1133 lp.lp.repeat_factor = 0;
1134 lp.lp.line_pattern = 0;
1135 state->render_states[WINED3D_RS_LINEPATTERN] = lp.d;
1136 state->render_states[WINED3D_RS_ZWRITEENABLE] = TRUE;
1137 state->render_states[WINED3D_RS_ALPHATESTENABLE] = FALSE;
1138 state->render_states[WINED3D_RS_LASTPIXEL] = TRUE;
1139 state->render_states[WINED3D_RS_SRCBLEND] = WINED3D_BLEND_ONE;
1140 state->render_states[WINED3D_RS_DESTBLEND] = WINED3D_BLEND_ZERO;
1141 state->render_states[WINED3D_RS_CULLMODE] = WINED3D_CULL_BACK;
1142 state->render_states[WINED3D_RS_ZFUNC] = WINED3D_CMP_LESSEQUAL;
1143 state->render_states[WINED3D_RS_ALPHAFUNC] = WINED3D_CMP_ALWAYS;
1144 state->render_states[WINED3D_RS_ALPHAREF] = 0;
1145 state->render_states[WINED3D_RS_DITHERENABLE] = FALSE;
1146 state->render_states[WINED3D_RS_ALPHABLENDENABLE] = FALSE;
1147 state->render_states[WINED3D_RS_FOGENABLE] = FALSE;
1148 state->render_states[WINED3D_RS_SPECULARENABLE] = FALSE;
1149 state->render_states[WINED3D_RS_ZVISIBLE] = 0;
1150 state->render_states[WINED3D_RS_FOGCOLOR] = 0;
1151 state->render_states[WINED3D_RS_FOGTABLEMODE] = WINED3D_FOG_NONE;
1152 tmpfloat.f = 0.0f;
1153 state->render_states[WINED3D_RS_FOGSTART] = tmpfloat.d;
1154 tmpfloat.f = 1.0f;
1155 state->render_states[WINED3D_RS_FOGEND] = tmpfloat.d;
1156 tmpfloat.f = 1.0f;
1157 state->render_states[WINED3D_RS_FOGDENSITY] = tmpfloat.d;
1158 state->render_states[WINED3D_RS_EDGEANTIALIAS] = FALSE;
1159 state->render_states[WINED3D_RS_RANGEFOGENABLE] = FALSE;
1160 state->render_states[WINED3D_RS_STENCILENABLE] = FALSE;
1161 state->render_states[WINED3D_RS_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1162 state->render_states[WINED3D_RS_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1163 state->render_states[WINED3D_RS_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1164 state->render_states[WINED3D_RS_STENCILREF] = 0;
1165 state->render_states[WINED3D_RS_STENCILMASK] = 0xffffffff;
1166 state->render_states[WINED3D_RS_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1167 state->render_states[WINED3D_RS_STENCILWRITEMASK] = 0xffffffff;
1168 state->render_states[WINED3D_RS_TEXTUREFACTOR] = 0xffffffff;
1169 state->render_states[WINED3D_RS_WRAP0] = 0;
1170 state->render_states[WINED3D_RS_WRAP1] = 0;
1171 state->render_states[WINED3D_RS_WRAP2] = 0;
1172 state->render_states[WINED3D_RS_WRAP3] = 0;
1173 state->render_states[WINED3D_RS_WRAP4] = 0;
1174 state->render_states[WINED3D_RS_WRAP5] = 0;
1175 state->render_states[WINED3D_RS_WRAP6] = 0;
1176 state->render_states[WINED3D_RS_WRAP7] = 0;
1177 state->render_states[WINED3D_RS_CLIPPING] = TRUE;
1178 state->render_states[WINED3D_RS_LIGHTING] = TRUE;
1179 state->render_states[WINED3D_RS_AMBIENT] = 0;
1180 state->render_states[WINED3D_RS_FOGVERTEXMODE] = WINED3D_FOG_NONE;
1181 state->render_states[WINED3D_RS_COLORVERTEX] = TRUE;
1182 state->render_states[WINED3D_RS_LOCALVIEWER] = TRUE;
1183 state->render_states[WINED3D_RS_NORMALIZENORMALS] = FALSE;
1184 state->render_states[WINED3D_RS_DIFFUSEMATERIALSOURCE] = WINED3D_MCS_COLOR1;
1185 state->render_states[WINED3D_RS_SPECULARMATERIALSOURCE] = WINED3D_MCS_COLOR2;
1186 state->render_states[WINED3D_RS_AMBIENTMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1187 state->render_states[WINED3D_RS_EMISSIVEMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1188 state->render_states[WINED3D_RS_VERTEXBLEND] = WINED3D_VBF_DISABLE;
1189 state->render_states[WINED3D_RS_CLIPPLANEENABLE] = 0;
1190 state->render_states[WINED3D_RS_SOFTWAREVERTEXPROCESSING] = FALSE;
1191 tmpfloat.f = 1.0f;
1192 state->render_states[WINED3D_RS_POINTSIZE] = tmpfloat.d;
1193 tmpfloat.f = 1.0f;
1194 state->render_states[WINED3D_RS_POINTSIZE_MIN] = tmpfloat.d;
1195 state->render_states[WINED3D_RS_POINTSPRITEENABLE] = FALSE;
1196 state->render_states[WINED3D_RS_POINTSCALEENABLE] = FALSE;
1197 tmpfloat.f = 1.0f;
1198 state->render_states[WINED3D_RS_POINTSCALE_A] = tmpfloat.d;
1199 tmpfloat.f = 0.0f;
1200 state->render_states[WINED3D_RS_POINTSCALE_B] = tmpfloat.d;
1201 tmpfloat.f = 0.0f;
1202 state->render_states[WINED3D_RS_POINTSCALE_C] = tmpfloat.d;
1203 state->render_states[WINED3D_RS_MULTISAMPLEANTIALIAS] = TRUE;
1204 state->render_states[WINED3D_RS_MULTISAMPLEMASK] = 0xffffffff;
1205 state->render_states[WINED3D_RS_PATCHEDGESTYLE] = WINED3D_PATCH_EDGE_DISCRETE;
1206 tmpfloat.f = 1.0f;
1207 state->render_states[WINED3D_RS_PATCHSEGMENTS] = tmpfloat.d;
1208 state->render_states[WINED3D_RS_DEBUGMONITORTOKEN] = 0xbaadcafe;
1209 tmpfloat.f = gl_info->limits.pointsize_max;
1210 state->render_states[WINED3D_RS_POINTSIZE_MAX] = tmpfloat.d;
1211 state->render_states[WINED3D_RS_INDEXEDVERTEXBLENDENABLE] = FALSE;
1212 state->render_states[WINED3D_RS_COLORWRITEENABLE] = 0x0000000f;
1213 tmpfloat.f = 0.0f;
1214 state->render_states[WINED3D_RS_TWEENFACTOR] = tmpfloat.d;
1215 state->render_states[WINED3D_RS_BLENDOP] = WINED3D_BLEND_OP_ADD;
1216 state->render_states[WINED3D_RS_POSITIONDEGREE] = WINED3D_DEGREE_CUBIC;
1217 state->render_states[WINED3D_RS_NORMALDEGREE] = WINED3D_DEGREE_LINEAR;
1218 /* states new in d3d9 */
1219 state->render_states[WINED3D_RS_SCISSORTESTENABLE] = FALSE;
1220 state->render_states[WINED3D_RS_SLOPESCALEDEPTHBIAS] = 0;
1221 tmpfloat.f = 1.0f;
1222 state->render_states[WINED3D_RS_MINTESSELLATIONLEVEL] = tmpfloat.d;
1223 state->render_states[WINED3D_RS_MAXTESSELLATIONLEVEL] = tmpfloat.d;
1224 state->render_states[WINED3D_RS_ANTIALIASEDLINEENABLE] = FALSE;
1225 tmpfloat.f = 0.0f;
1226 state->render_states[WINED3D_RS_ADAPTIVETESS_X] = tmpfloat.d;
1227 state->render_states[WINED3D_RS_ADAPTIVETESS_Y] = tmpfloat.d;
1228 tmpfloat.f = 1.0f;
1229 state->render_states[WINED3D_RS_ADAPTIVETESS_Z] = tmpfloat.d;
1230 tmpfloat.f = 0.0f;
1231 state->render_states[WINED3D_RS_ADAPTIVETESS_W] = tmpfloat.d;
1232 state->render_states[WINED3D_RS_ENABLEADAPTIVETESSELLATION] = FALSE;
1233 state->render_states[WINED3D_RS_TWOSIDEDSTENCILMODE] = FALSE;
1234 state->render_states[WINED3D_RS_BACK_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1235 state->render_states[WINED3D_RS_BACK_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1236 state->render_states[WINED3D_RS_BACK_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1237 state->render_states[WINED3D_RS_BACK_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1238 state->render_states[WINED3D_RS_COLORWRITEENABLE1] = 0x0000000f;
1239 state->render_states[WINED3D_RS_COLORWRITEENABLE2] = 0x0000000f;
1240 state->render_states[WINED3D_RS_COLORWRITEENABLE3] = 0x0000000f;
1241 state->render_states[WINED3D_RS_BLENDFACTOR] = 0xffffffff;
1242 state->render_states[WINED3D_RS_SRGBWRITEENABLE] = 0;
1243 state->render_states[WINED3D_RS_DEPTHBIAS] = 0;
1244 state->render_states[WINED3D_RS_WRAP8] = 0;
1245 state->render_states[WINED3D_RS_WRAP9] = 0;
1246 state->render_states[WINED3D_RS_WRAP10] = 0;
1247 state->render_states[WINED3D_RS_WRAP11] = 0;
1248 state->render_states[WINED3D_RS_WRAP12] = 0;
1249 state->render_states[WINED3D_RS_WRAP13] = 0;
1250 state->render_states[WINED3D_RS_WRAP14] = 0;
1251 state->render_states[WINED3D_RS_WRAP15] = 0;
1252 state->render_states[WINED3D_RS_SEPARATEALPHABLENDENABLE] = FALSE;
1253 state->render_states[WINED3D_RS_SRCBLENDALPHA] = WINED3D_BLEND_ONE;
1254 state->render_states[WINED3D_RS_DESTBLENDALPHA] = WINED3D_BLEND_ZERO;
1255 state->render_states[WINED3D_RS_BLENDOPALPHA] = WINED3D_BLEND_OP_ADD;
1256
1257 /* Texture Stage States - Put directly into state block, we will call function below */
1258 for (i = 0; i < MAX_TEXTURES; ++i)
1259 {
1260 TRACE("Setting up default texture states for texture Stage %u.\n", i);
1261 state->transforms[WINED3D_TS_TEXTURE0 + i] = identity;
1262 state->texture_states[i][WINED3D_TSS_COLOR_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_MODULATE;
1263 state->texture_states[i][WINED3D_TSS_COLOR_ARG1] = WINED3DTA_TEXTURE;
1264 state->texture_states[i][WINED3D_TSS_COLOR_ARG2] = WINED3DTA_CURRENT;
1265 state->texture_states[i][WINED3D_TSS_ALPHA_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_SELECT_ARG1;
1266 state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] = WINED3DTA_TEXTURE;
1267 state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] = WINED3DTA_CURRENT;
1268 state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00] = 0;
1269 state->texture_states[i][WINED3D_TSS_BUMPENV_MAT01] = 0;
1270 state->texture_states[i][WINED3D_TSS_BUMPENV_MAT10] = 0;
1271 state->texture_states[i][WINED3D_TSS_BUMPENV_MAT11] = 0;
1272 state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] = i;
1273 state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE] = 0;
1274 state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET] = 0;
1275 state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS] = WINED3D_TTFF_DISABLE;
1276 state->texture_states[i][WINED3D_TSS_COLOR_ARG0] = WINED3DTA_CURRENT;
1277 state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] = WINED3DTA_CURRENT;
1278 state->texture_states[i][WINED3D_TSS_RESULT_ARG] = WINED3DTA_CURRENT;
1279 }
1280
1281 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; ++i)
1282 {
1283 TRACE("Setting up default samplers states for sampler %u.\n", i);
1284 state->sampler_states[i][WINED3D_SAMP_ADDRESS_U] = WINED3D_TADDRESS_WRAP;
1285 state->sampler_states[i][WINED3D_SAMP_ADDRESS_V] = WINED3D_TADDRESS_WRAP;
1286 state->sampler_states[i][WINED3D_SAMP_ADDRESS_W] = WINED3D_TADDRESS_WRAP;
1287 state->sampler_states[i][WINED3D_SAMP_BORDER_COLOR] = 0;
1288 state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] = WINED3D_TEXF_POINT;
1289 state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] = WINED3D_TEXF_POINT;
1290 state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] = WINED3D_TEXF_NONE;
1291 state->sampler_states[i][WINED3D_SAMP_MIPMAP_LOD_BIAS] = 0;
1292 state->sampler_states[i][WINED3D_SAMP_MAX_MIP_LEVEL] = 0;
1293 state->sampler_states[i][WINED3D_SAMP_MAX_ANISOTROPY] = 1;
1294 state->sampler_states[i][WINED3D_SAMP_SRGB_TEXTURE] = 0;
1295 /* TODO: Indicates which element of a multielement texture to use. */
1296 state->sampler_states[i][WINED3D_SAMP_ELEMENT_INDEX] = 0;
1297 /* TODO: Vertex offset in the presampled displacement map. */
1298 state->sampler_states[i][WINED3D_SAMP_DMAP_OFFSET] = 0;
1299 }
1300 }
1301
1302 void state_init(struct wined3d_state *state, struct wined3d_fb_state *fb,
1303 const struct wined3d_gl_info *gl_info, const struct wined3d_d3d_info *d3d_info,
1304 DWORD flags)
1305 {
1306 unsigned int i;
1307
1308 state->flags = flags;
1309 state->fb = fb;
1310
1311 for (i = 0; i < LIGHTMAP_SIZE; i++)
1312 {
1313 list_init(&state->light_map[i]);
1314 }
1315
1316 if (flags & WINED3D_STATE_INIT_DEFAULT)
1317 state_init_default(state, gl_info);
1318 }
1319
1320 static HRESULT stateblock_init(struct wined3d_stateblock *stateblock,
1321 struct wined3d_device *device, enum wined3d_stateblock_type type)
1322 {
1323 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
1324
1325 stateblock->ref = 1;
1326 stateblock->device = device;
1327 state_init(&stateblock->state, NULL, &device->adapter->gl_info, d3d_info, 0);
1328
1329 if (type == WINED3D_SBT_RECORDED)
1330 return WINED3D_OK;
1331
1332 TRACE("Updating changed flags appropriate for type %#x.\n", type);
1333
1334 switch (type)
1335 {
1336 case WINED3D_SBT_ALL:
1337 stateblock_init_lights(stateblock, device->state.light_map);
1338 stateblock_savedstates_set_all(&stateblock->changed,
1339 d3d_info->limits.vs_uniform_count, d3d_info->limits.ps_uniform_count);
1340 break;
1341
1342 case WINED3D_SBT_PIXEL_STATE:
1343 stateblock_savedstates_set_pixel(&stateblock->changed,
1344 d3d_info->limits.ps_uniform_count);
1345 break;
1346
1347 case WINED3D_SBT_VERTEX_STATE:
1348 stateblock_init_lights(stateblock, device->state.light_map);
1349 stateblock_savedstates_set_vertex(&stateblock->changed,
1350 d3d_info->limits.vs_uniform_count);
1351 break;
1352
1353 default:
1354 FIXME("Unrecognized state block type %#x.\n", type);
1355 break;
1356 }
1357
1358 stateblock_init_contained_states(stateblock);
1359 wined3d_stateblock_capture(stateblock);
1360
1361 return WINED3D_OK;
1362 }
1363
1364 HRESULT CDECL wined3d_stateblock_create(struct wined3d_device *device,
1365 enum wined3d_stateblock_type type, struct wined3d_stateblock **stateblock)
1366 {
1367 struct wined3d_stateblock *object;
1368 HRESULT hr;
1369
1370 TRACE("device %p, type %#x, stateblock %p.\n",
1371 device, type, stateblock);
1372
1373 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1374 if (!object)
1375 return E_OUTOFMEMORY;
1376
1377 hr = stateblock_init(object, device, type);
1378 if (FAILED(hr))
1379 {
1380 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1381 HeapFree(GetProcessHeap(), 0, object);
1382 return hr;
1383 }
1384
1385 TRACE("Created stateblock %p.\n", object);
1386 *stateblock = object;
1387
1388 return WINED3D_OK;
1389 }