VirtualBox

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

Last change on this file since 69362 was 48345, checked in by vboxsync, 11 years ago

header fixes

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