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