VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine_new/wined3d/stateblock.c@ 46757

Last change on this file since 46757 was 46521, checked in by vboxsync, 12 years ago

wine/new: export

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette