2 * state block implementation
4 * Copyright 2002 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2007 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
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.
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.
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
26 #include "wine/port.h"
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
31 static const DWORD pixel_states_render
[] =
33 WINED3D_RS_ALPHABLENDENABLE
,
36 WINED3D_RS_ALPHATESTENABLE
,
37 WINED3D_RS_ANTIALIASEDLINEENABLE
,
38 WINED3D_RS_BLENDFACTOR
,
40 WINED3D_RS_BLENDOPALPHA
,
41 WINED3D_RS_BACK_STENCILFAIL
,
42 WINED3D_RS_BACK_STENCILPASS
,
43 WINED3D_RS_BACK_STENCILZFAIL
,
44 WINED3D_RS_COLORWRITEENABLE
,
45 WINED3D_RS_COLORWRITEENABLE1
,
46 WINED3D_RS_COLORWRITEENABLE2
,
47 WINED3D_RS_COLORWRITEENABLE3
,
50 WINED3D_RS_DESTBLENDALPHA
,
51 WINED3D_RS_DITHERENABLE
,
53 WINED3D_RS_FOGDENSITY
,
57 WINED3D_RS_SCISSORTESTENABLE
,
58 WINED3D_RS_SEPARATEALPHABLENDENABLE
,
60 WINED3D_RS_SLOPESCALEDEPTHBIAS
,
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
,
92 WINED3D_RS_ZWRITEENABLE
,
95 static const DWORD pixel_states_texture
[] =
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
,
116 static const DWORD pixel_states_sampler
[] =
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
,
132 static const DWORD vertex_states_render
[] =
134 WINED3D_RS_ADAPTIVETESS_W
,
135 WINED3D_RS_ADAPTIVETESS_X
,
136 WINED3D_RS_ADAPTIVETESS_Y
,
137 WINED3D_RS_ADAPTIVETESS_Z
,
139 WINED3D_RS_AMBIENTMATERIALSOURCE
,
141 WINED3D_RS_CLIPPLANEENABLE
,
142 WINED3D_RS_COLORVERTEX
,
144 WINED3D_RS_DIFFUSEMATERIALSOURCE
,
145 WINED3D_RS_EMISSIVEMATERIALSOURCE
,
146 WINED3D_RS_ENABLEADAPTIVETESSELLATION
,
148 WINED3D_RS_FOGDENSITY
,
149 WINED3D_RS_FOGENABLE
,
152 WINED3D_RS_FOGTABLEMODE
,
153 WINED3D_RS_FOGVERTEXMODE
,
154 WINED3D_RS_INDEXEDVERTEXBLENDENABLE
,
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
,
181 static const DWORD vertex_states_texture
[] =
183 WINED3D_TSS_TEXCOORD_INDEX
,
184 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
,
187 static const DWORD vertex_states_sampler
[] =
189 WINED3D_SAMP_DMAP_OFFSET
,
192 static inline void stateblock_set_bits(DWORD
*map
, UINT map_size
)
194 DWORD mask
= (1u << (map_size
& 0x1f)) - 1;
195 memset(map
, 0xff, (map_size
>> 5) * sizeof(*map
));
196 if (mask
) map
[map_size
>> 5] = mask
;
199 /* Set all members of a stateblock savedstate to the given value */
200 static void stateblock_savedstates_set_all(struct wined3d_saved_states
*states
, DWORD vs_consts
, DWORD ps_consts
)
206 states
->material
= 1;
207 states
->viewport
= 1;
208 states
->vertexDecl
= 1;
209 states
->pixelShader
= 1;
210 states
->vertexShader
= 1;
211 states
->scissorRect
= 1;
213 /* Fixed size arrays */
214 states
->streamSource
= 0xffff;
215 states
->streamFreq
= 0xffff;
216 states
->textures
= 0xfffff;
217 stateblock_set_bits(states
->transform
, HIGHEST_TRANSFORMSTATE
+ 1);
218 stateblock_set_bits(states
->renderState
, WINEHIGHEST_RENDER_STATE
+ 1);
219 for (i
= 0; i
< MAX_TEXTURES
; ++i
) states
->textureState
[i
] = 0x3ffff;
220 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
) states
->samplerState
[i
] = 0x3ffe;
221 states
->clipplane
= (1u << MAX_CLIP_DISTANCES
) - 1;
222 states
->pixelShaderConstantsB
= 0xffff;
223 states
->pixelShaderConstantsI
= 0xffff;
224 states
->vertexShaderConstantsB
= 0xffff;
225 states
->vertexShaderConstantsI
= 0xffff;
227 /* Dynamically sized arrays */
228 memset(states
->ps_consts_f
, TRUE
, sizeof(BOOL
) * ps_consts
);
229 memset(states
->vs_consts_f
, TRUE
, sizeof(BOOL
) * vs_consts
);
232 static void stateblock_savedstates_set_pixel(struct wined3d_saved_states
*states
, const DWORD num_constants
)
234 DWORD texture_mask
= 0;
235 WORD sampler_mask
= 0;
238 states
->pixelShader
= 1;
240 for (i
= 0; i
< ARRAY_SIZE(pixel_states_render
); ++i
)
242 DWORD rs
= pixel_states_render
[i
];
243 states
->renderState
[rs
>> 5] |= 1u << (rs
& 0x1f);
246 for (i
= 0; i
< ARRAY_SIZE(pixel_states_texture
); ++i
)
247 texture_mask
|= 1u << pixel_states_texture
[i
];
248 for (i
= 0; i
< MAX_TEXTURES
; ++i
) states
->textureState
[i
] = texture_mask
;
249 for (i
= 0; i
< ARRAY_SIZE(pixel_states_sampler
); ++i
)
250 sampler_mask
|= 1u << pixel_states_sampler
[i
];
251 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
) states
->samplerState
[i
] = sampler_mask
;
252 states
->pixelShaderConstantsB
= 0xffff;
253 states
->pixelShaderConstantsI
= 0xffff;
255 memset(states
->ps_consts_f
, TRUE
, sizeof(BOOL
) * num_constants
);
258 static void stateblock_savedstates_set_vertex(struct wined3d_saved_states
*states
, const DWORD num_constants
)
260 DWORD texture_mask
= 0;
261 WORD sampler_mask
= 0;
264 states
->vertexDecl
= 1;
265 states
->vertexShader
= 1;
267 for (i
= 0; i
< ARRAY_SIZE(vertex_states_render
); ++i
)
269 DWORD rs
= vertex_states_render
[i
];
270 states
->renderState
[rs
>> 5] |= 1u << (rs
& 0x1f);
273 for (i
= 0; i
< ARRAY_SIZE(vertex_states_texture
); ++i
)
274 texture_mask
|= 1u << vertex_states_texture
[i
];
275 for (i
= 0; i
< MAX_TEXTURES
; ++i
) states
->textureState
[i
] = texture_mask
;
276 for (i
= 0; i
< ARRAY_SIZE(vertex_states_sampler
); ++i
)
277 sampler_mask
|= 1u << vertex_states_sampler
[i
];
278 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
) states
->samplerState
[i
] = sampler_mask
;
279 states
->vertexShaderConstantsB
= 0xffff;
280 states
->vertexShaderConstantsI
= 0xffff;
282 memset(states
->vs_consts_f
, TRUE
, sizeof(BOOL
) * num_constants
);
285 void stateblock_init_contained_states(struct wined3d_stateblock
*stateblock
)
287 const struct wined3d_d3d_info
*d3d_info
= &stateblock
->device
->adapter
->d3d_info
;
290 for (i
= 0; i
<= WINEHIGHEST_RENDER_STATE
>> 5; ++i
)
292 DWORD map
= stateblock
->changed
.renderState
[i
];
293 for (j
= 0; map
; map
>>= 1, ++j
)
295 if (!(map
& 1)) continue;
297 stateblock
->contained_render_states
[stateblock
->num_contained_render_states
] = (i
<< 5) | j
;
298 ++stateblock
->num_contained_render_states
;
302 for (i
= 0; i
<= HIGHEST_TRANSFORMSTATE
>> 5; ++i
)
304 DWORD map
= stateblock
->changed
.transform
[i
];
305 for (j
= 0; map
; map
>>= 1, ++j
)
307 if (!(map
& 1)) continue;
309 stateblock
->contained_transform_states
[stateblock
->num_contained_transform_states
] = (i
<< 5) | j
;
310 ++stateblock
->num_contained_transform_states
;
314 for (i
= 0; i
< d3d_info
->limits
.vs_uniform_count
; ++i
)
316 if (stateblock
->changed
.vs_consts_f
[i
])
318 stateblock
->contained_vs_consts_f
[stateblock
->num_contained_vs_consts_f
] = i
;
319 ++stateblock
->num_contained_vs_consts_f
;
323 for (i
= 0; i
< WINED3D_MAX_CONSTS_I
; ++i
)
325 if (stateblock
->changed
.vertexShaderConstantsI
& (1u << i
))
327 stateblock
->contained_vs_consts_i
[stateblock
->num_contained_vs_consts_i
] = i
;
328 ++stateblock
->num_contained_vs_consts_i
;
332 for (i
= 0; i
< WINED3D_MAX_CONSTS_B
; ++i
)
334 if (stateblock
->changed
.vertexShaderConstantsB
& (1u << i
))
336 stateblock
->contained_vs_consts_b
[stateblock
->num_contained_vs_consts_b
] = i
;
337 ++stateblock
->num_contained_vs_consts_b
;
341 for (i
= 0; i
< d3d_info
->limits
.ps_uniform_count
; ++i
)
343 if (stateblock
->changed
.ps_consts_f
[i
])
345 stateblock
->contained_ps_consts_f
[stateblock
->num_contained_ps_consts_f
] = i
;
346 ++stateblock
->num_contained_ps_consts_f
;
350 for (i
= 0; i
< WINED3D_MAX_CONSTS_I
; ++i
)
352 if (stateblock
->changed
.pixelShaderConstantsI
& (1u << i
))
354 stateblock
->contained_ps_consts_i
[stateblock
->num_contained_ps_consts_i
] = i
;
355 ++stateblock
->num_contained_ps_consts_i
;
359 for (i
= 0; i
< WINED3D_MAX_CONSTS_B
; ++i
)
361 if (stateblock
->changed
.pixelShaderConstantsB
& (1u << i
))
363 stateblock
->contained_ps_consts_b
[stateblock
->num_contained_ps_consts_b
] = i
;
364 ++stateblock
->num_contained_ps_consts_b
;
368 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
370 DWORD map
= stateblock
->changed
.textureState
[i
];
372 for(j
= 0; map
; map
>>= 1, ++j
)
374 if (!(map
& 1)) continue;
376 stateblock
->contained_tss_states
[stateblock
->num_contained_tss_states
].stage
= i
;
377 stateblock
->contained_tss_states
[stateblock
->num_contained_tss_states
].state
= j
;
378 ++stateblock
->num_contained_tss_states
;
382 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
384 DWORD map
= stateblock
->changed
.samplerState
[i
];
386 for (j
= 0; map
; map
>>= 1, ++j
)
388 if (!(map
& 1)) continue;
390 stateblock
->contained_sampler_states
[stateblock
->num_contained_sampler_states
].stage
= i
;
391 stateblock
->contained_sampler_states
[stateblock
->num_contained_sampler_states
].state
= j
;
392 ++stateblock
->num_contained_sampler_states
;
397 static void stateblock_init_lights(struct wined3d_stateblock
*stateblock
, struct list
*light_map
)
401 for (i
= 0; i
< LIGHTMAP_SIZE
; ++i
)
403 const struct wined3d_light_info
*src_light
;
405 LIST_FOR_EACH_ENTRY(src_light
, &light_map
[i
], struct wined3d_light_info
, entry
)
407 struct wined3d_light_info
*dst_light
= heap_alloc(sizeof(*dst_light
));
409 *dst_light
= *src_light
;
410 list_add_tail(&stateblock
->state
.light_map
[i
], &dst_light
->entry
);
415 ULONG CDECL
wined3d_stateblock_incref(struct wined3d_stateblock
*stateblock
)
417 ULONG refcount
= InterlockedIncrement(&stateblock
->ref
);
419 TRACE("%p increasing refcount to %u.\n", stateblock
, refcount
);
424 void state_unbind_resources(struct wined3d_state
*state
)
426 struct wined3d_unordered_access_view
*uav
;
427 struct wined3d_shader_resource_view
*srv
;
428 struct wined3d_vertex_declaration
*decl
;
429 struct wined3d_sampler
*sampler
;
430 struct wined3d_texture
*texture
;
431 struct wined3d_buffer
*buffer
;
432 struct wined3d_shader
*shader
;
435 if ((decl
= state
->vertex_declaration
))
437 state
->vertex_declaration
= NULL
;
438 wined3d_vertex_declaration_decref(decl
);
441 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
443 if ((texture
= state
->textures
[i
]))
445 state
->textures
[i
] = NULL
;
446 wined3d_texture_decref(texture
);
450 for (i
= 0; i
< WINED3D_MAX_STREAM_OUTPUT_BUFFERS
; ++i
)
452 if ((buffer
= state
->stream_output
[i
].buffer
))
454 state
->stream_output
[i
].buffer
= NULL
;
455 wined3d_buffer_decref(buffer
);
459 for (i
= 0; i
< MAX_STREAMS
; ++i
)
461 if ((buffer
= state
->streams
[i
].buffer
))
463 state
->streams
[i
].buffer
= NULL
;
464 wined3d_buffer_decref(buffer
);
468 if ((buffer
= state
->index_buffer
))
470 state
->index_buffer
= NULL
;
471 wined3d_buffer_decref(buffer
);
474 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
476 if ((shader
= state
->shader
[i
]))
478 state
->shader
[i
] = NULL
;
479 wined3d_shader_decref(shader
);
482 for (j
= 0; j
< MAX_CONSTANT_BUFFERS
; ++j
)
484 if ((buffer
= state
->cb
[i
][j
]))
486 state
->cb
[i
][j
] = NULL
;
487 wined3d_buffer_decref(buffer
);
491 for (j
= 0; j
< MAX_SAMPLER_OBJECTS
; ++j
)
493 if ((sampler
= state
->sampler
[i
][j
]))
495 state
->sampler
[i
][j
] = NULL
;
496 wined3d_sampler_decref(sampler
);
500 for (j
= 0; j
< MAX_SHADER_RESOURCE_VIEWS
; ++j
)
502 if ((srv
= state
->shader_resource_view
[i
][j
]))
504 state
->shader_resource_view
[i
][j
] = NULL
;
505 wined3d_shader_resource_view_decref(srv
);
510 for (i
= 0; i
< WINED3D_PIPELINE_COUNT
; ++i
)
512 for (j
= 0; j
< MAX_UNORDERED_ACCESS_VIEWS
; ++j
)
514 if ((uav
= state
->unordered_access_view
[i
][j
]))
516 state
->unordered_access_view
[i
][j
] = NULL
;
517 wined3d_unordered_access_view_decref(uav
);
523 void state_cleanup(struct wined3d_state
*state
)
525 unsigned int counter
;
527 if (!(state
->flags
& WINED3D_STATE_NO_REF
))
528 state_unbind_resources(state
);
530 for (counter
= 0; counter
< MAX_ACTIVE_LIGHTS
; ++counter
)
532 state
->lights
[counter
] = NULL
;
535 for (counter
= 0; counter
< LIGHTMAP_SIZE
; ++counter
)
537 struct list
*e1
, *e2
;
538 LIST_FOR_EACH_SAFE(e1
, e2
, &state
->light_map
[counter
])
540 struct wined3d_light_info
*light
= LIST_ENTRY(e1
, struct wined3d_light_info
, entry
);
541 list_remove(&light
->entry
);
547 ULONG CDECL
wined3d_stateblock_decref(struct wined3d_stateblock
*stateblock
)
549 ULONG refcount
= InterlockedDecrement(&stateblock
->ref
);
551 TRACE("%p decreasing refcount to %u\n", stateblock
, refcount
);
555 state_cleanup(&stateblock
->state
);
556 heap_free(stateblock
);
562 struct wined3d_light_info
*wined3d_state_get_light(const struct wined3d_state
*state
, unsigned int idx
)
564 struct wined3d_light_info
*light_info
;
565 unsigned int hash_idx
;
567 hash_idx
= LIGHTMAP_HASHFUNC(idx
);
568 LIST_FOR_EACH_ENTRY(light_info
, &state
->light_map
[hash_idx
], struct wined3d_light_info
, entry
)
570 if (light_info
->OriginalIndex
== idx
)
577 void wined3d_state_enable_light(struct wined3d_state
*state
, const struct wined3d_d3d_info
*d3d_info
,
578 struct wined3d_light_info
*light_info
, BOOL enable
)
580 unsigned int light_count
, i
;
582 if (!(light_info
->enabled
= enable
))
584 if (light_info
->glIndex
== -1)
586 TRACE("Light already disabled, nothing to do.\n");
590 state
->lights
[light_info
->glIndex
] = NULL
;
591 light_info
->glIndex
= -1;
595 if (light_info
->glIndex
!= -1)
597 TRACE("Light already enabled, nothing to do.\n");
601 /* Find a free light. */
602 light_count
= d3d_info
->limits
.active_light_count
;
603 for (i
= 0; i
< light_count
; ++i
)
605 if (state
->lights
[i
])
608 state
->lights
[i
] = light_info
;
609 light_info
->glIndex
= i
;
613 /* Our tests show that Windows returns D3D_OK in this situation, even with
614 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices.
615 * This is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns
616 * TRUE * as well for those lights.
618 * TODO: Test how this affects rendering. */
619 WARN("Too many concurrently active lights.\n");
622 static void wined3d_state_record_lights(struct wined3d_state
*dst_state
, const struct wined3d_state
*src_state
)
626 /* Lights... For a recorded state block, we just had a chain of actions
627 * to perform, so we need to walk that chain and update any actions which
629 for (i
= 0; i
< LIGHTMAP_SIZE
; ++i
)
632 LIST_FOR_EACH(e
, &dst_state
->light_map
[i
])
634 BOOL updated
= FALSE
;
635 struct wined3d_light_info
*src
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
), *realLight
;
637 /* Look up the light in the destination */
638 LIST_FOR_EACH(f
, &src_state
->light_map
[i
])
640 realLight
= LIST_ENTRY(f
, struct wined3d_light_info
, entry
);
641 if (realLight
->OriginalIndex
== src
->OriginalIndex
)
643 src
->OriginalParms
= realLight
->OriginalParms
;
645 if (realLight
->glIndex
== -1 && src
->glIndex
!= -1)
648 dst_state
->lights
[src
->glIndex
] = NULL
;
650 else if (realLight
->glIndex
!= -1 && src
->glIndex
== -1)
653 dst_state
->lights
[realLight
->glIndex
] = src
;
655 src
->glIndex
= realLight
->glIndex
;
663 /* This can happen if the light was originally created as a
664 * default light for SetLightEnable() while recording. */
665 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
666 src
->OriginalIndex
, dst_state
, src_state
);
668 src
->OriginalParms
= WINED3D_default_light
;
669 if (src
->glIndex
!= -1)
671 dst_state
->lights
[src
->glIndex
] = NULL
;
679 void CDECL
wined3d_stateblock_capture(struct wined3d_stateblock
*stateblock
)
681 const struct wined3d_state
*src_state
= &stateblock
->device
->state
;
685 TRACE("stateblock %p.\n", stateblock
);
687 TRACE("Capturing state %p.\n", src_state
);
689 if (stateblock
->changed
.vertexShader
&& stateblock
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
]
690 != src_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
692 TRACE("Updating vertex shader from %p to %p\n",
693 stateblock
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
],
694 src_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]);
696 if (src_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
697 wined3d_shader_incref(src_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]);
698 if (stateblock
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
])
699 wined3d_shader_decref(stateblock
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
]);
700 stateblock
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
] = src_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
703 /* Vertex shader float constants. */
704 for (i
= 0; i
< stateblock
->num_contained_vs_consts_f
; ++i
)
706 unsigned int idx
= stateblock
->contained_vs_consts_f
[i
];
708 TRACE("Setting vs_consts_f[%u] to %s.\n", idx
, debug_vec4(&src_state
->vs_consts_f
[idx
]));
710 stateblock
->state
.vs_consts_f
[idx
] = src_state
->vs_consts_f
[idx
];
713 /* Vertex shader integer constants. */
714 for (i
= 0; i
< stateblock
->num_contained_vs_consts_i
; ++i
)
716 unsigned int idx
= stateblock
->contained_vs_consts_i
[i
];
718 TRACE("Setting vs_consts[%u] to %s.\n", idx
, debug_ivec4(&src_state
->vs_consts_i
[idx
]));
720 stateblock
->state
.vs_consts_i
[idx
] = src_state
->vs_consts_i
[idx
];
723 /* Vertex shader boolean constants. */
724 for (i
= 0; i
< stateblock
->num_contained_vs_consts_b
; ++i
)
726 unsigned int idx
= stateblock
->contained_vs_consts_b
[i
];
728 TRACE("Setting vs_consts_b[%u] to %s.\n",
729 idx
, src_state
->vs_consts_b
[idx
] ? "TRUE" : "FALSE");
731 stateblock
->state
.vs_consts_b
[idx
] = src_state
->vs_consts_b
[idx
];
734 /* Pixel shader float constants. */
735 for (i
= 0; i
< stateblock
->num_contained_ps_consts_f
; ++i
)
737 unsigned int idx
= stateblock
->contained_ps_consts_f
[i
];
739 TRACE("Setting ps_consts_f[%u] to %s.\n", idx
, debug_vec4(&src_state
->ps_consts_f
[idx
]));
741 stateblock
->state
.ps_consts_f
[idx
] = src_state
->ps_consts_f
[idx
];
744 /* Pixel shader integer constants. */
745 for (i
= 0; i
< stateblock
->num_contained_ps_consts_i
; ++i
)
747 unsigned int idx
= stateblock
->contained_ps_consts_i
[i
];
749 TRACE("Setting ps_consts_i[%u] to %s.\n", idx
, debug_ivec4(&src_state
->ps_consts_i
[idx
]));
751 stateblock
->state
.ps_consts_i
[idx
] = src_state
->ps_consts_i
[idx
];
754 /* Pixel shader boolean constants. */
755 for (i
= 0; i
< stateblock
->num_contained_ps_consts_b
; ++i
)
757 unsigned int idx
= stateblock
->contained_ps_consts_b
[i
];
758 TRACE("Setting ps_consts_b[%u] to %s.\n",
759 idx
, src_state
->ps_consts_b
[idx
] ? "TRUE" : "FALSE");
761 stateblock
->state
.ps_consts_b
[idx
] = src_state
->ps_consts_b
[idx
];
764 /* Others + Render & Texture */
765 for (i
= 0; i
< stateblock
->num_contained_transform_states
; ++i
)
767 enum wined3d_transform_state transform
= stateblock
->contained_transform_states
[i
];
769 TRACE("Updating transform %#x.\n", transform
);
771 stateblock
->state
.transforms
[transform
] = src_state
->transforms
[transform
];
774 if (stateblock
->changed
.indices
775 && ((stateblock
->state
.index_buffer
!= src_state
->index_buffer
)
776 || (stateblock
->state
.base_vertex_index
!= src_state
->base_vertex_index
)
777 || (stateblock
->state
.index_format
!= src_state
->index_format
)
778 || (stateblock
->state
.index_offset
!= src_state
->index_offset
)))
780 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
781 src_state
->index_buffer
, src_state
->base_vertex_index
);
783 if (src_state
->index_buffer
)
784 wined3d_buffer_incref(src_state
->index_buffer
);
785 if (stateblock
->state
.index_buffer
)
786 wined3d_buffer_decref(stateblock
->state
.index_buffer
);
787 stateblock
->state
.index_buffer
= src_state
->index_buffer
;
788 stateblock
->state
.base_vertex_index
= src_state
->base_vertex_index
;
789 stateblock
->state
.index_format
= src_state
->index_format
;
790 stateblock
->state
.index_offset
= src_state
->index_offset
;
793 if (stateblock
->changed
.vertexDecl
&& stateblock
->state
.vertex_declaration
!= src_state
->vertex_declaration
)
795 TRACE("Updating vertex declaration from %p to %p.\n",
796 stateblock
->state
.vertex_declaration
, src_state
->vertex_declaration
);
798 if (src_state
->vertex_declaration
)
799 wined3d_vertex_declaration_incref(src_state
->vertex_declaration
);
800 if (stateblock
->state
.vertex_declaration
)
801 wined3d_vertex_declaration_decref(stateblock
->state
.vertex_declaration
);
802 stateblock
->state
.vertex_declaration
= src_state
->vertex_declaration
;
805 if (stateblock
->changed
.material
806 && memcmp(&src_state
->material
, &stateblock
->state
.material
, sizeof(stateblock
->state
.material
)))
808 TRACE("Updating material.\n");
810 stateblock
->state
.material
= src_state
->material
;
813 assert(src_state
->viewport_count
<= 1);
815 if (stateblock
->changed
.viewport
816 && (src_state
->viewport_count
!= stateblock
->state
.viewport_count
817 || memcmp(src_state
->viewports
, stateblock
->state
.viewports
,
818 src_state
->viewport_count
* sizeof(*stateblock
->state
.viewports
))))
820 TRACE("Updating viewports.\n");
822 if ((stateblock
->state
.viewport_count
= src_state
->viewport_count
))
823 memcpy(stateblock
->state
.viewports
, src_state
->viewports
, sizeof(src_state
->viewports
));
825 memset(stateblock
->state
.viewports
, 0, sizeof(*stateblock
->state
.viewports
));
828 if (stateblock
->changed
.scissorRect
829 && (src_state
->scissor_rect_count
!= stateblock
->state
.scissor_rect_count
830 || memcmp(src_state
->scissor_rects
, stateblock
->state
.scissor_rects
,
831 src_state
->scissor_rect_count
* sizeof(*stateblock
->state
.scissor_rects
))))
833 TRACE("Updating scissor rects.\n");
835 if ((stateblock
->state
.scissor_rect_count
= src_state
->scissor_rect_count
))
836 memcpy(stateblock
->state
.scissor_rects
, src_state
->scissor_rects
,
837 src_state
->scissor_rect_count
* sizeof(*src_state
->scissor_rects
));
839 SetRectEmpty(stateblock
->state
.scissor_rects
);
842 map
= stateblock
->changed
.streamSource
;
843 for (i
= 0; map
; map
>>= 1, ++i
)
845 if (!(map
& 1)) continue;
847 if (stateblock
->state
.streams
[i
].stride
!= src_state
->streams
[i
].stride
848 || stateblock
->state
.streams
[i
].buffer
!= src_state
->streams
[i
].buffer
)
850 TRACE("Updating stream source %u to %p, stride to %u.\n",
851 i
, src_state
->streams
[i
].buffer
,
852 src_state
->streams
[i
].stride
);
854 stateblock
->state
.streams
[i
].stride
= src_state
->streams
[i
].stride
;
855 if (src_state
->streams
[i
].buffer
)
856 wined3d_buffer_incref(src_state
->streams
[i
].buffer
);
857 if (stateblock
->state
.streams
[i
].buffer
)
858 wined3d_buffer_decref(stateblock
->state
.streams
[i
].buffer
);
859 stateblock
->state
.streams
[i
].buffer
= src_state
->streams
[i
].buffer
;
863 map
= stateblock
->changed
.streamFreq
;
864 for (i
= 0; map
; map
>>= 1, ++i
)
866 if (!(map
& 1)) continue;
868 if (stateblock
->state
.streams
[i
].frequency
!= src_state
->streams
[i
].frequency
869 || stateblock
->state
.streams
[i
].flags
!= src_state
->streams
[i
].flags
)
871 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
872 i
, src_state
->streams
[i
].frequency
, src_state
->streams
[i
].flags
);
874 stateblock
->state
.streams
[i
].frequency
= src_state
->streams
[i
].frequency
;
875 stateblock
->state
.streams
[i
].flags
= src_state
->streams
[i
].flags
;
879 map
= stateblock
->changed
.clipplane
;
880 for (i
= 0; map
; map
>>= 1, ++i
)
882 if (!(map
& 1)) continue;
884 if (memcmp(&stateblock
->state
.clip_planes
[i
], &src_state
->clip_planes
[i
], sizeof(src_state
->clip_planes
[i
])))
886 TRACE("Updating clipplane %u.\n", i
);
887 stateblock
->state
.clip_planes
[i
] = src_state
->clip_planes
[i
];
892 for (i
= 0; i
< stateblock
->num_contained_render_states
; ++i
)
894 enum wined3d_render_state rs
= stateblock
->contained_render_states
[i
];
896 TRACE("Updating render state %#x to %u.\n", rs
, src_state
->render_states
[rs
]);
898 stateblock
->state
.render_states
[rs
] = src_state
->render_states
[rs
];
902 for (i
= 0; i
< stateblock
->num_contained_tss_states
; ++i
)
904 DWORD stage
= stateblock
->contained_tss_states
[i
].stage
;
905 DWORD state
= stateblock
->contained_tss_states
[i
].state
;
907 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage
, state
,
908 src_state
->texture_states
[stage
][state
], stateblock
->state
.texture_states
[stage
][state
]);
910 stateblock
->state
.texture_states
[stage
][state
] = src_state
->texture_states
[stage
][state
];
914 map
= stateblock
->changed
.textures
;
915 for (i
= 0; map
; map
>>= 1, ++i
)
917 if (!(map
& 1)) continue;
919 TRACE("Updating texture %u to %p (was %p).\n",
920 i
, src_state
->textures
[i
], stateblock
->state
.textures
[i
]);
922 if (src_state
->textures
[i
])
923 wined3d_texture_incref(src_state
->textures
[i
]);
924 if (stateblock
->state
.textures
[i
])
925 wined3d_texture_decref(stateblock
->state
.textures
[i
]);
926 stateblock
->state
.textures
[i
] = src_state
->textures
[i
];
929 for (i
= 0; i
< stateblock
->num_contained_sampler_states
; ++i
)
931 DWORD stage
= stateblock
->contained_sampler_states
[i
].stage
;
932 DWORD state
= stateblock
->contained_sampler_states
[i
].state
;
934 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage
, state
,
935 src_state
->sampler_states
[stage
][state
], stateblock
->state
.sampler_states
[stage
][state
]);
937 stateblock
->state
.sampler_states
[stage
][state
] = src_state
->sampler_states
[stage
][state
];
940 if (stateblock
->changed
.pixelShader
&& stateblock
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
]
941 != src_state
->shader
[WINED3D_SHADER_TYPE_PIXEL
])
943 if (src_state
->shader
[WINED3D_SHADER_TYPE_PIXEL
])
944 wined3d_shader_incref(src_state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]);
945 if (stateblock
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
])
946 wined3d_shader_decref(stateblock
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
]);
947 stateblock
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
] = src_state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
950 wined3d_state_record_lights(&stateblock
->state
, src_state
);
952 TRACE("Capture done.\n");
955 static void apply_lights(struct wined3d_device
*device
, const struct wined3d_state
*state
)
959 for (i
= 0; i
< LIGHTMAP_SIZE
; ++i
)
963 LIST_FOR_EACH(e
, &state
->light_map
[i
])
965 const struct wined3d_light_info
*light
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
967 wined3d_device_set_light(device
, light
->OriginalIndex
, &light
->OriginalParms
);
968 wined3d_device_set_light_enable(device
, light
->OriginalIndex
, light
->glIndex
!= -1);
973 void CDECL
wined3d_stateblock_apply(const struct wined3d_stateblock
*stateblock
)
975 struct wined3d_device
*device
= stateblock
->device
;
979 TRACE("Applying stateblock %p to device %p.\n", stateblock
, device
);
981 if (stateblock
->changed
.vertexShader
)
982 wined3d_device_set_vertex_shader(device
, stateblock
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
]);
984 /* Vertex Shader Constants. */
985 for (i
= 0; i
< stateblock
->num_contained_vs_consts_f
; ++i
)
987 wined3d_device_set_vs_consts_f(device
, stateblock
->contained_vs_consts_f
[i
],
988 1, &stateblock
->state
.vs_consts_f
[stateblock
->contained_vs_consts_f
[i
]]);
990 for (i
= 0; i
< stateblock
->num_contained_vs_consts_i
; ++i
)
992 wined3d_device_set_vs_consts_i(device
, stateblock
->contained_vs_consts_i
[i
],
993 1, &stateblock
->state
.vs_consts_i
[stateblock
->contained_vs_consts_i
[i
]]);
995 for (i
= 0; i
< stateblock
->num_contained_vs_consts_b
; ++i
)
997 wined3d_device_set_vs_consts_b(device
, stateblock
->contained_vs_consts_b
[i
],
998 1, &stateblock
->state
.vs_consts_b
[stateblock
->contained_vs_consts_b
[i
]]);
1001 apply_lights(device
, &stateblock
->state
);
1003 if (stateblock
->changed
.pixelShader
)
1004 wined3d_device_set_pixel_shader(device
, stateblock
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
]);
1006 /* Pixel Shader Constants. */
1007 for (i
= 0; i
< stateblock
->num_contained_ps_consts_f
; ++i
)
1009 wined3d_device_set_ps_consts_f(device
, stateblock
->contained_ps_consts_f
[i
],
1010 1, &stateblock
->state
.ps_consts_f
[stateblock
->contained_ps_consts_f
[i
]]);
1012 for (i
= 0; i
< stateblock
->num_contained_ps_consts_i
; ++i
)
1014 wined3d_device_set_ps_consts_i(device
, stateblock
->contained_ps_consts_i
[i
],
1015 1, &stateblock
->state
.ps_consts_i
[stateblock
->contained_ps_consts_i
[i
]]);
1017 for (i
= 0; i
< stateblock
->num_contained_ps_consts_b
; ++i
)
1019 wined3d_device_set_ps_consts_b(device
, stateblock
->contained_ps_consts_b
[i
],
1020 1, &stateblock
->state
.ps_consts_b
[stateblock
->contained_ps_consts_b
[i
]]);
1023 /* Render states. */
1024 for (i
= 0; i
< stateblock
->num_contained_render_states
; ++i
)
1026 wined3d_device_set_render_state(device
, stateblock
->contained_render_states
[i
],
1027 stateblock
->state
.render_states
[stateblock
->contained_render_states
[i
]]);
1030 /* Texture states. */
1031 for (i
= 0; i
< stateblock
->num_contained_tss_states
; ++i
)
1033 DWORD stage
= stateblock
->contained_tss_states
[i
].stage
;
1034 DWORD state
= stateblock
->contained_tss_states
[i
].state
;
1036 wined3d_device_set_texture_stage_state(device
, stage
, state
, stateblock
->state
.texture_states
[stage
][state
]);
1039 /* Sampler states. */
1040 for (i
= 0; i
< stateblock
->num_contained_sampler_states
; ++i
)
1042 DWORD stage
= stateblock
->contained_sampler_states
[i
].stage
;
1043 DWORD state
= stateblock
->contained_sampler_states
[i
].state
;
1044 DWORD value
= stateblock
->state
.sampler_states
[stage
][state
];
1046 if (stage
>= MAX_FRAGMENT_SAMPLERS
) stage
+= WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
;
1047 wined3d_device_set_sampler_state(device
, stage
, state
, value
);
1050 /* Transform states. */
1051 for (i
= 0; i
< stateblock
->num_contained_transform_states
; ++i
)
1053 wined3d_device_set_transform(device
, stateblock
->contained_transform_states
[i
],
1054 &stateblock
->state
.transforms
[stateblock
->contained_transform_states
[i
]]);
1057 if (stateblock
->changed
.indices
)
1059 wined3d_device_set_index_buffer(device
, stateblock
->state
.index_buffer
,
1060 stateblock
->state
.index_format
, stateblock
->state
.index_offset
);
1061 wined3d_device_set_base_vertex_index(device
, stateblock
->state
.base_vertex_index
);
1064 if (stateblock
->changed
.vertexDecl
&& stateblock
->state
.vertex_declaration
)
1065 wined3d_device_set_vertex_declaration(device
, stateblock
->state
.vertex_declaration
);
1067 if (stateblock
->changed
.material
)
1068 wined3d_device_set_material(device
, &stateblock
->state
.material
);
1070 if (stateblock
->changed
.viewport
)
1071 wined3d_device_set_viewports(device
, stateblock
->state
.viewport_count
, stateblock
->state
.viewports
);
1073 if (stateblock
->changed
.scissorRect
)
1074 wined3d_device_set_scissor_rects(device
, stateblock
->state
.scissor_rect_count
,
1075 stateblock
->state
.scissor_rects
);
1077 map
= stateblock
->changed
.streamSource
;
1078 for (i
= 0; map
; map
>>= 1, ++i
)
1081 wined3d_device_set_stream_source(device
, i
,
1082 stateblock
->state
.streams
[i
].buffer
,
1083 0, stateblock
->state
.streams
[i
].stride
);
1086 map
= stateblock
->changed
.streamFreq
;
1087 for (i
= 0; map
; map
>>= 1, ++i
)
1090 wined3d_device_set_stream_source_freq(device
, i
,
1091 stateblock
->state
.streams
[i
].frequency
| stateblock
->state
.streams
[i
].flags
);
1094 map
= stateblock
->changed
.textures
;
1095 for (i
= 0; map
; map
>>= 1, ++i
)
1099 if (!(map
& 1)) continue;
1101 stage
= i
< MAX_FRAGMENT_SAMPLERS
? i
: WINED3DVERTEXTEXTURESAMPLER0
+ i
- MAX_FRAGMENT_SAMPLERS
;
1102 wined3d_device_set_texture(device
, stage
, stateblock
->state
.textures
[i
]);
1105 map
= stateblock
->changed
.clipplane
;
1106 for (i
= 0; map
; map
>>= 1, ++i
)
1108 if (!(map
& 1)) continue;
1110 wined3d_device_set_clip_plane(device
, i
, &stateblock
->state
.clip_planes
[i
]);
1113 TRACE("Applied stateblock %p.\n", stateblock
);
1116 static void state_init_default(struct wined3d_state
*state
, const struct wined3d_d3d_info
*d3d_info
)
1120 struct wined3d_line_pattern lp
;
1129 struct wined3d_matrix identity
;
1131 TRACE("state %p, d3d_info %p.\n", state
, d3d_info
);
1133 get_identity_matrix(&identity
);
1134 state
->gl_primitive_type
= ~0u;
1135 state
->gl_patch_vertices
= 0;
1137 /* Set some of the defaults for lights, transforms etc */
1138 state
->transforms
[WINED3D_TS_PROJECTION
] = identity
;
1139 state
->transforms
[WINED3D_TS_VIEW
] = identity
;
1140 for (i
= 0; i
< 256; ++i
)
1142 state
->transforms
[WINED3D_TS_WORLD_MATRIX(i
)] = identity
;
1145 TRACE("Render states\n");
1146 /* Render states: */
1147 state
->render_states
[WINED3D_RS_ZENABLE
] = WINED3D_ZB_TRUE
;
1148 state
->render_states
[WINED3D_RS_FILLMODE
] = WINED3D_FILL_SOLID
;
1149 state
->render_states
[WINED3D_RS_SHADEMODE
] = WINED3D_SHADE_GOURAUD
;
1150 lp
.lp
.repeat_factor
= 0;
1151 lp
.lp
.line_pattern
= 0;
1152 state
->render_states
[WINED3D_RS_LINEPATTERN
] = lp
.d
;
1153 state
->render_states
[WINED3D_RS_ZWRITEENABLE
] = TRUE
;
1154 state
->render_states
[WINED3D_RS_ALPHATESTENABLE
] = FALSE
;
1155 state
->render_states
[WINED3D_RS_LASTPIXEL
] = TRUE
;
1156 state
->render_states
[WINED3D_RS_SRCBLEND
] = WINED3D_BLEND_ONE
;
1157 state
->render_states
[WINED3D_RS_DESTBLEND
] = WINED3D_BLEND_ZERO
;
1158 state
->render_states
[WINED3D_RS_CULLMODE
] = WINED3D_CULL_BACK
;
1159 state
->render_states
[WINED3D_RS_ZFUNC
] = WINED3D_CMP_LESSEQUAL
;
1160 state
->render_states
[WINED3D_RS_ALPHAFUNC
] = WINED3D_CMP_ALWAYS
;
1161 state
->render_states
[WINED3D_RS_ALPHAREF
] = 0;
1162 state
->render_states
[WINED3D_RS_DITHERENABLE
] = FALSE
;
1163 state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] = FALSE
;
1164 state
->render_states
[WINED3D_RS_FOGENABLE
] = FALSE
;
1165 state
->render_states
[WINED3D_RS_SPECULARENABLE
] = FALSE
;
1166 state
->render_states
[WINED3D_RS_ZVISIBLE
] = 0;
1167 state
->render_states
[WINED3D_RS_FOGCOLOR
] = 0;
1168 state
->render_states
[WINED3D_RS_FOGTABLEMODE
] = WINED3D_FOG_NONE
;
1170 state
->render_states
[WINED3D_RS_FOGSTART
] = tmpfloat
.d
;
1172 state
->render_states
[WINED3D_RS_FOGEND
] = tmpfloat
.d
;
1174 state
->render_states
[WINED3D_RS_FOGDENSITY
] = tmpfloat
.d
;
1175 state
->render_states
[WINED3D_RS_EDGEANTIALIAS
] = FALSE
;
1176 state
->render_states
[WINED3D_RS_RANGEFOGENABLE
] = FALSE
;
1177 state
->render_states
[WINED3D_RS_STENCILENABLE
] = FALSE
;
1178 state
->render_states
[WINED3D_RS_STENCILFAIL
] = WINED3D_STENCIL_OP_KEEP
;
1179 state
->render_states
[WINED3D_RS_STENCILZFAIL
] = WINED3D_STENCIL_OP_KEEP
;
1180 state
->render_states
[WINED3D_RS_STENCILPASS
] = WINED3D_STENCIL_OP_KEEP
;
1181 state
->render_states
[WINED3D_RS_STENCILREF
] = 0;
1182 state
->render_states
[WINED3D_RS_STENCILMASK
] = 0xffffffff;
1183 state
->render_states
[WINED3D_RS_STENCILFUNC
] = WINED3D_CMP_ALWAYS
;
1184 state
->render_states
[WINED3D_RS_STENCILWRITEMASK
] = 0xffffffff;
1185 state
->render_states
[WINED3D_RS_TEXTUREFACTOR
] = 0xffffffff;
1186 state
->render_states
[WINED3D_RS_WRAP0
] = 0;
1187 state
->render_states
[WINED3D_RS_WRAP1
] = 0;
1188 state
->render_states
[WINED3D_RS_WRAP2
] = 0;
1189 state
->render_states
[WINED3D_RS_WRAP3
] = 0;
1190 state
->render_states
[WINED3D_RS_WRAP4
] = 0;
1191 state
->render_states
[WINED3D_RS_WRAP5
] = 0;
1192 state
->render_states
[WINED3D_RS_WRAP6
] = 0;
1193 state
->render_states
[WINED3D_RS_WRAP7
] = 0;
1194 state
->render_states
[WINED3D_RS_CLIPPING
] = TRUE
;
1195 state
->render_states
[WINED3D_RS_LIGHTING
] = TRUE
;
1196 state
->render_states
[WINED3D_RS_AMBIENT
] = 0;
1197 state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] = WINED3D_FOG_NONE
;
1198 state
->render_states
[WINED3D_RS_COLORVERTEX
] = TRUE
;
1199 state
->render_states
[WINED3D_RS_LOCALVIEWER
] = TRUE
;
1200 state
->render_states
[WINED3D_RS_NORMALIZENORMALS
] = FALSE
;
1201 state
->render_states
[WINED3D_RS_DIFFUSEMATERIALSOURCE
] = WINED3D_MCS_COLOR1
;
1202 state
->render_states
[WINED3D_RS_SPECULARMATERIALSOURCE
] = WINED3D_MCS_COLOR2
;
1203 state
->render_states
[WINED3D_RS_AMBIENTMATERIALSOURCE
] = WINED3D_MCS_MATERIAL
;
1204 state
->render_states
[WINED3D_RS_EMISSIVEMATERIALSOURCE
] = WINED3D_MCS_MATERIAL
;
1205 state
->render_states
[WINED3D_RS_VERTEXBLEND
] = WINED3D_VBF_DISABLE
;
1206 state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
] = 0;
1207 state
->render_states
[WINED3D_RS_SOFTWAREVERTEXPROCESSING
] = FALSE
;
1209 state
->render_states
[WINED3D_RS_POINTSIZE
] = tmpfloat
.d
;
1211 state
->render_states
[WINED3D_RS_POINTSIZE_MIN
] = tmpfloat
.d
;
1212 state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
] = FALSE
;
1213 state
->render_states
[WINED3D_RS_POINTSCALEENABLE
] = FALSE
;
1215 state
->render_states
[WINED3D_RS_POINTSCALE_A
] = tmpfloat
.d
;
1217 state
->render_states
[WINED3D_RS_POINTSCALE_B
] = tmpfloat
.d
;
1219 state
->render_states
[WINED3D_RS_POINTSCALE_C
] = tmpfloat
.d
;
1220 state
->render_states
[WINED3D_RS_MULTISAMPLEANTIALIAS
] = TRUE
;
1221 state
->render_states
[WINED3D_RS_MULTISAMPLEMASK
] = 0xffffffff;
1222 state
->render_states
[WINED3D_RS_PATCHEDGESTYLE
] = WINED3D_PATCH_EDGE_DISCRETE
;
1224 state
->render_states
[WINED3D_RS_PATCHSEGMENTS
] = tmpfloat
.d
;
1225 state
->render_states
[WINED3D_RS_DEBUGMONITORTOKEN
] = 0xbaadcafe;
1226 tmpfloat
.f
= d3d_info
->limits
.pointsize_max
;
1227 state
->render_states
[WINED3D_RS_POINTSIZE_MAX
] = tmpfloat
.d
;
1228 state
->render_states
[WINED3D_RS_INDEXEDVERTEXBLENDENABLE
] = FALSE
;
1229 state
->render_states
[WINED3D_RS_COLORWRITEENABLE
] = 0x0000000f;
1231 state
->render_states
[WINED3D_RS_TWEENFACTOR
] = tmpfloat
.d
;
1232 state
->render_states
[WINED3D_RS_BLENDOP
] = WINED3D_BLEND_OP_ADD
;
1233 state
->render_states
[WINED3D_RS_POSITIONDEGREE
] = WINED3D_DEGREE_CUBIC
;
1234 state
->render_states
[WINED3D_RS_NORMALDEGREE
] = WINED3D_DEGREE_LINEAR
;
1235 /* states new in d3d9 */
1236 state
->render_states
[WINED3D_RS_SCISSORTESTENABLE
] = FALSE
;
1237 state
->render_states
[WINED3D_RS_SLOPESCALEDEPTHBIAS
] = 0;
1239 state
->render_states
[WINED3D_RS_MINTESSELLATIONLEVEL
] = tmpfloat
.d
;
1240 state
->render_states
[WINED3D_RS_MAXTESSELLATIONLEVEL
] = tmpfloat
.d
;
1241 state
->render_states
[WINED3D_RS_ANTIALIASEDLINEENABLE
] = FALSE
;
1243 state
->render_states
[WINED3D_RS_ADAPTIVETESS_X
] = tmpfloat
.d
;
1244 state
->render_states
[WINED3D_RS_ADAPTIVETESS_Y
] = tmpfloat
.d
;
1246 state
->render_states
[WINED3D_RS_ADAPTIVETESS_Z
] = tmpfloat
.d
;
1248 state
->render_states
[WINED3D_RS_ADAPTIVETESS_W
] = tmpfloat
.d
;
1249 state
->render_states
[WINED3D_RS_ENABLEADAPTIVETESSELLATION
] = FALSE
;
1250 state
->render_states
[WINED3D_RS_TWOSIDEDSTENCILMODE
] = FALSE
;
1251 state
->render_states
[WINED3D_RS_BACK_STENCILFAIL
] = WINED3D_STENCIL_OP_KEEP
;
1252 state
->render_states
[WINED3D_RS_BACK_STENCILZFAIL
] = WINED3D_STENCIL_OP_KEEP
;
1253 state
->render_states
[WINED3D_RS_BACK_STENCILPASS
] = WINED3D_STENCIL_OP_KEEP
;
1254 state
->render_states
[WINED3D_RS_BACK_STENCILFUNC
] = WINED3D_CMP_ALWAYS
;
1255 state
->render_states
[WINED3D_RS_COLORWRITEENABLE1
] = 0x0000000f;
1256 state
->render_states
[WINED3D_RS_COLORWRITEENABLE2
] = 0x0000000f;
1257 state
->render_states
[WINED3D_RS_COLORWRITEENABLE3
] = 0x0000000f;
1258 state
->render_states
[WINED3D_RS_BLENDFACTOR
] = 0xffffffff;
1259 state
->render_states
[WINED3D_RS_SRGBWRITEENABLE
] = 0;
1260 state
->render_states
[WINED3D_RS_DEPTHBIAS
] = 0;
1261 state
->render_states
[WINED3D_RS_WRAP8
] = 0;
1262 state
->render_states
[WINED3D_RS_WRAP9
] = 0;
1263 state
->render_states
[WINED3D_RS_WRAP10
] = 0;
1264 state
->render_states
[WINED3D_RS_WRAP11
] = 0;
1265 state
->render_states
[WINED3D_RS_WRAP12
] = 0;
1266 state
->render_states
[WINED3D_RS_WRAP13
] = 0;
1267 state
->render_states
[WINED3D_RS_WRAP14
] = 0;
1268 state
->render_states
[WINED3D_RS_WRAP15
] = 0;
1269 state
->render_states
[WINED3D_RS_SEPARATEALPHABLENDENABLE
] = FALSE
;
1270 state
->render_states
[WINED3D_RS_SRCBLENDALPHA
] = WINED3D_BLEND_ONE
;
1271 state
->render_states
[WINED3D_RS_DESTBLENDALPHA
] = WINED3D_BLEND_ZERO
;
1272 state
->render_states
[WINED3D_RS_BLENDOPALPHA
] = WINED3D_BLEND_OP_ADD
;
1274 /* Texture Stage States - Put directly into state block, we will call function below */
1275 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1277 TRACE("Setting up default texture states for texture Stage %u.\n", i
);
1278 state
->transforms
[WINED3D_TS_TEXTURE0
+ i
] = identity
;
1279 state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
] = i
? WINED3D_TOP_DISABLE
: WINED3D_TOP_MODULATE
;
1280 state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] = WINED3DTA_TEXTURE
;
1281 state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] = WINED3DTA_CURRENT
;
1282 state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
] = i
? WINED3D_TOP_DISABLE
: WINED3D_TOP_SELECT_ARG1
;
1283 state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] = WINED3DTA_TEXTURE
;
1284 state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] = WINED3DTA_CURRENT
;
1285 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT00
] = 0;
1286 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT01
] = 0;
1287 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT10
] = 0;
1288 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT11
] = 0;
1289 state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
] = i
;
1290 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LSCALE
] = 0;
1291 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LOFFSET
] = 0;
1292 state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
] = WINED3D_TTFF_DISABLE
;
1293 state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] = WINED3DTA_CURRENT
;
1294 state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] = WINED3DTA_CURRENT
;
1295 state
->texture_states
[i
][WINED3D_TSS_RESULT_ARG
] = WINED3DTA_CURRENT
;
1298 for (i
= 0 ; i
< MAX_COMBINED_SAMPLERS
; ++i
)
1300 TRACE("Setting up default samplers states for sampler %u.\n", i
);
1301 state
->sampler_states
[i
][WINED3D_SAMP_ADDRESS_U
] = WINED3D_TADDRESS_WRAP
;
1302 state
->sampler_states
[i
][WINED3D_SAMP_ADDRESS_V
] = WINED3D_TADDRESS_WRAP
;
1303 state
->sampler_states
[i
][WINED3D_SAMP_ADDRESS_W
] = WINED3D_TADDRESS_WRAP
;
1304 state
->sampler_states
[i
][WINED3D_SAMP_BORDER_COLOR
] = 0;
1305 state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] = WINED3D_TEXF_POINT
;
1306 state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] = WINED3D_TEXF_POINT
;
1307 state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] = WINED3D_TEXF_NONE
;
1308 state
->sampler_states
[i
][WINED3D_SAMP_MIPMAP_LOD_BIAS
] = 0;
1309 state
->sampler_states
[i
][WINED3D_SAMP_MAX_MIP_LEVEL
] = 0;
1310 state
->sampler_states
[i
][WINED3D_SAMP_MAX_ANISOTROPY
] = 1;
1311 state
->sampler_states
[i
][WINED3D_SAMP_SRGB_TEXTURE
] = 0;
1312 /* TODO: Indicates which element of a multielement texture to use. */
1313 state
->sampler_states
[i
][WINED3D_SAMP_ELEMENT_INDEX
] = 0;
1314 /* TODO: Vertex offset in the presampled displacement map. */
1315 state
->sampler_states
[i
][WINED3D_SAMP_DMAP_OFFSET
] = 0;
1319 void state_init(struct wined3d_state
*state
, struct wined3d_fb_state
*fb
,
1320 const struct wined3d_d3d_info
*d3d_info
, DWORD flags
)
1324 state
->flags
= flags
;
1327 for (i
= 0; i
< LIGHTMAP_SIZE
; i
++)
1329 list_init(&state
->light_map
[i
]);
1332 if (flags
& WINED3D_STATE_INIT_DEFAULT
)
1333 state_init_default(state
, d3d_info
);
1336 static HRESULT
stateblock_init(struct wined3d_stateblock
*stateblock
,
1337 struct wined3d_device
*device
, enum wined3d_stateblock_type type
)
1339 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
1341 stateblock
->ref
= 1;
1342 stateblock
->device
= device
;
1343 state_init(&stateblock
->state
, NULL
, d3d_info
, 0);
1345 if (type
== WINED3D_SBT_RECORDED
)
1348 TRACE("Updating changed flags appropriate for type %#x.\n", type
);
1352 case WINED3D_SBT_ALL
:
1353 stateblock_init_lights(stateblock
, device
->state
.light_map
);
1354 stateblock_savedstates_set_all(&stateblock
->changed
,
1355 d3d_info
->limits
.vs_uniform_count
, d3d_info
->limits
.ps_uniform_count
);
1358 case WINED3D_SBT_PIXEL_STATE
:
1359 stateblock_savedstates_set_pixel(&stateblock
->changed
,
1360 d3d_info
->limits
.ps_uniform_count
);
1363 case WINED3D_SBT_VERTEX_STATE
:
1364 stateblock_init_lights(stateblock
, device
->state
.light_map
);
1365 stateblock_savedstates_set_vertex(&stateblock
->changed
,
1366 d3d_info
->limits
.vs_uniform_count
);
1370 FIXME("Unrecognized state block type %#x.\n", type
);
1374 stateblock_init_contained_states(stateblock
);
1375 wined3d_stateblock_capture(stateblock
);
1380 HRESULT CDECL
wined3d_stateblock_create(struct wined3d_device
*device
,
1381 enum wined3d_stateblock_type type
, struct wined3d_stateblock
**stateblock
)
1383 struct wined3d_stateblock
*object
;
1386 TRACE("device %p, type %#x, stateblock %p.\n",
1387 device
, type
, stateblock
);
1389 if (!(object
= heap_alloc_zero(sizeof(*object
))))
1390 return E_OUTOFMEMORY
;
1392 hr
= stateblock_init(object
, device
, type
);
1395 WARN("Failed to initialize stateblock, hr %#x.\n", hr
);
1400 TRACE("Created stateblock %p.\n", object
);
1401 *stateblock
= object
;