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_CCW_STENCILFAIL
,
42 WINED3D_RS_CCW_STENCILPASS
,
43 WINED3D_RS_CCW_STENCILZFAIL
,
44 WINED3D_RS_COLORWRITEENABLE
,
45 WINED3D_RS_COLORWRITEENABLE1
,
46 WINED3D_RS_COLORWRITEENABLE2
,
47 WINED3D_RS_COLORWRITEENABLE3
,
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 /* Allocates the correct amount of space for pixel and vertex shader constants,
193 * along with their set/changed flags on the given stateblock object
195 static HRESULT
stateblock_allocate_shader_constants(struct wined3d_stateblock
*object
)
197 const struct wined3d_d3d_info
*d3d_info
= &object
->device
->adapter
->d3d_info
;
199 object
->changed
.pixelShaderConstantsF
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
200 sizeof(BOOL
) * d3d_info
->limits
.ps_uniform_count
);
201 if (!object
->changed
.pixelShaderConstantsF
) goto fail
;
203 object
->changed
.vertexShaderConstantsF
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
204 sizeof(BOOL
) * d3d_info
->limits
.vs_uniform_count
);
205 if (!object
->changed
.vertexShaderConstantsF
) goto fail
;
207 object
->contained_vs_consts_f
= HeapAlloc(GetProcessHeap(), 0,
208 sizeof(DWORD
) * d3d_info
->limits
.vs_uniform_count
);
209 if (!object
->contained_vs_consts_f
) goto fail
;
211 object
->contained_ps_consts_f
= HeapAlloc(GetProcessHeap(), 0,
212 sizeof(DWORD
) * d3d_info
->limits
.ps_uniform_count
);
213 if (!object
->contained_ps_consts_f
) goto fail
;
218 ERR("Failed to allocate memory\n");
219 HeapFree(GetProcessHeap(), 0, object
->changed
.pixelShaderConstantsF
);
220 HeapFree(GetProcessHeap(), 0, object
->changed
.vertexShaderConstantsF
);
221 HeapFree(GetProcessHeap(), 0, object
->contained_vs_consts_f
);
222 HeapFree(GetProcessHeap(), 0, object
->contained_ps_consts_f
);
223 return E_OUTOFMEMORY
;
226 static inline void stateblock_set_bits(DWORD
*map
, UINT map_size
)
228 DWORD mask
= (1 << (map_size
& 0x1f)) - 1;
229 memset(map
, 0xff, (map_size
>> 5) * sizeof(*map
));
230 if (mask
) map
[map_size
>> 5] = mask
;
233 /* Set all members of a stateblock savedstate to the given value */
234 static void stateblock_savedstates_set_all(struct wined3d_saved_states
*states
, DWORD vs_consts
, DWORD ps_consts
)
239 states
->primitive_type
= 1;
241 states
->material
= 1;
242 states
->viewport
= 1;
243 states
->vertexDecl
= 1;
244 states
->pixelShader
= 1;
245 states
->vertexShader
= 1;
246 states
->scissorRect
= 1;
248 /* Fixed size arrays */
249 states
->streamSource
= 0xffff;
250 states
->streamFreq
= 0xffff;
251 states
->textures
= 0xfffff;
252 stateblock_set_bits(states
->transform
, HIGHEST_TRANSFORMSTATE
+ 1);
253 stateblock_set_bits(states
->renderState
, WINEHIGHEST_RENDER_STATE
+ 1);
254 for (i
= 0; i
< MAX_TEXTURES
; ++i
) states
->textureState
[i
] = 0x3ffff;
255 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
) states
->samplerState
[i
] = 0x3ffe;
256 states
->clipplane
= 0xffffffff;
257 states
->pixelShaderConstantsB
= 0xffff;
258 states
->pixelShaderConstantsI
= 0xffff;
259 states
->vertexShaderConstantsB
= 0xffff;
260 states
->vertexShaderConstantsI
= 0xffff;
262 /* Dynamically sized arrays */
263 memset(states
->pixelShaderConstantsF
, TRUE
, sizeof(BOOL
) * ps_consts
);
264 memset(states
->vertexShaderConstantsF
, TRUE
, sizeof(BOOL
) * vs_consts
);
267 static void stateblock_savedstates_set_pixel(struct wined3d_saved_states
*states
, const DWORD num_constants
)
269 DWORD texture_mask
= 0;
270 WORD sampler_mask
= 0;
273 states
->pixelShader
= 1;
275 for (i
= 0; i
< sizeof(pixel_states_render
) / sizeof(*pixel_states_render
); ++i
)
277 DWORD rs
= pixel_states_render
[i
];
278 states
->renderState
[rs
>> 5] |= 1 << (rs
& 0x1f);
281 for (i
= 0; i
< sizeof(pixel_states_texture
) / sizeof(*pixel_states_texture
); ++i
)
282 texture_mask
|= 1 << pixel_states_texture
[i
];
283 for (i
= 0; i
< MAX_TEXTURES
; ++i
) states
->textureState
[i
] = texture_mask
;
284 for (i
= 0; i
< sizeof(pixel_states_sampler
) / sizeof(*pixel_states_sampler
); ++i
)
285 sampler_mask
|= 1 << pixel_states_sampler
[i
];
286 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
) states
->samplerState
[i
] = sampler_mask
;
287 states
->pixelShaderConstantsB
= 0xffff;
288 states
->pixelShaderConstantsI
= 0xffff;
290 memset(states
->pixelShaderConstantsF
, TRUE
, sizeof(BOOL
) * num_constants
);
293 static void stateblock_savedstates_set_vertex(struct wined3d_saved_states
*states
, const DWORD num_constants
)
295 DWORD texture_mask
= 0;
296 WORD sampler_mask
= 0;
299 states
->vertexDecl
= 1;
300 states
->vertexShader
= 1;
302 for (i
= 0; i
< sizeof(vertex_states_render
) / sizeof(*vertex_states_render
); ++i
)
304 DWORD rs
= vertex_states_render
[i
];
305 states
->renderState
[rs
>> 5] |= 1 << (rs
& 0x1f);
308 for (i
= 0; i
< sizeof(vertex_states_texture
) / sizeof(*vertex_states_texture
); ++i
)
309 texture_mask
|= 1 << vertex_states_texture
[i
];
310 for (i
= 0; i
< MAX_TEXTURES
; ++i
) states
->textureState
[i
] = texture_mask
;
311 for (i
= 0; i
< sizeof(vertex_states_sampler
) / sizeof(*vertex_states_sampler
); ++i
)
312 sampler_mask
|= 1 << vertex_states_sampler
[i
];
313 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
) states
->samplerState
[i
] = sampler_mask
;
314 states
->vertexShaderConstantsB
= 0xffff;
315 states
->vertexShaderConstantsI
= 0xffff;
317 memset(states
->vertexShaderConstantsF
, TRUE
, sizeof(BOOL
) * num_constants
);
320 void stateblock_init_contained_states(struct wined3d_stateblock
*stateblock
)
322 const struct wined3d_d3d_info
*d3d_info
= &stateblock
->device
->adapter
->d3d_info
;
325 for (i
= 0; i
<= WINEHIGHEST_RENDER_STATE
>> 5; ++i
)
327 DWORD map
= stateblock
->changed
.renderState
[i
];
328 for (j
= 0; map
; map
>>= 1, ++j
)
330 if (!(map
& 1)) continue;
332 stateblock
->contained_render_states
[stateblock
->num_contained_render_states
] = (i
<< 5) | j
;
333 ++stateblock
->num_contained_render_states
;
337 for (i
= 0; i
<= HIGHEST_TRANSFORMSTATE
>> 5; ++i
)
339 DWORD map
= stateblock
->changed
.transform
[i
];
340 for (j
= 0; map
; map
>>= 1, ++j
)
342 if (!(map
& 1)) continue;
344 stateblock
->contained_transform_states
[stateblock
->num_contained_transform_states
] = (i
<< 5) | j
;
345 ++stateblock
->num_contained_transform_states
;
349 for (i
= 0; i
< d3d_info
->limits
.vs_uniform_count
; ++i
)
351 if (stateblock
->changed
.vertexShaderConstantsF
[i
])
353 stateblock
->contained_vs_consts_f
[stateblock
->num_contained_vs_consts_f
] = i
;
354 ++stateblock
->num_contained_vs_consts_f
;
358 for (i
= 0; i
< MAX_CONST_I
; ++i
)
360 if (stateblock
->changed
.vertexShaderConstantsI
& (1 << i
))
362 stateblock
->contained_vs_consts_i
[stateblock
->num_contained_vs_consts_i
] = i
;
363 ++stateblock
->num_contained_vs_consts_i
;
367 for (i
= 0; i
< MAX_CONST_B
; ++i
)
369 if (stateblock
->changed
.vertexShaderConstantsB
& (1 << i
))
371 stateblock
->contained_vs_consts_b
[stateblock
->num_contained_vs_consts_b
] = i
;
372 ++stateblock
->num_contained_vs_consts_b
;
376 for (i
= 0; i
< d3d_info
->limits
.ps_uniform_count
; ++i
)
378 if (stateblock
->changed
.pixelShaderConstantsF
[i
])
380 stateblock
->contained_ps_consts_f
[stateblock
->num_contained_ps_consts_f
] = i
;
381 ++stateblock
->num_contained_ps_consts_f
;
385 for (i
= 0; i
< MAX_CONST_I
; ++i
)
387 if (stateblock
->changed
.pixelShaderConstantsI
& (1 << i
))
389 stateblock
->contained_ps_consts_i
[stateblock
->num_contained_ps_consts_i
] = i
;
390 ++stateblock
->num_contained_ps_consts_i
;
394 for (i
= 0; i
< MAX_CONST_B
; ++i
)
396 if (stateblock
->changed
.pixelShaderConstantsB
& (1 << i
))
398 stateblock
->contained_ps_consts_b
[stateblock
->num_contained_ps_consts_b
] = i
;
399 ++stateblock
->num_contained_ps_consts_b
;
403 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
405 DWORD map
= stateblock
->changed
.textureState
[i
];
407 for(j
= 0; map
; map
>>= 1, ++j
)
409 if (!(map
& 1)) continue;
411 stateblock
->contained_tss_states
[stateblock
->num_contained_tss_states
].stage
= i
;
412 stateblock
->contained_tss_states
[stateblock
->num_contained_tss_states
].state
= j
;
413 ++stateblock
->num_contained_tss_states
;
417 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
419 DWORD map
= stateblock
->changed
.samplerState
[i
];
421 for (j
= 0; map
; map
>>= 1, ++j
)
423 if (!(map
& 1)) continue;
425 stateblock
->contained_sampler_states
[stateblock
->num_contained_sampler_states
].stage
= i
;
426 stateblock
->contained_sampler_states
[stateblock
->num_contained_sampler_states
].state
= j
;
427 ++stateblock
->num_contained_sampler_states
;
432 static void stateblock_init_lights(struct wined3d_stateblock
*stateblock
, struct list
*light_map
)
436 for (i
= 0; i
< LIGHTMAP_SIZE
; ++i
)
438 const struct wined3d_light_info
*src_light
;
440 LIST_FOR_EACH_ENTRY(src_light
, &light_map
[i
], struct wined3d_light_info
, entry
)
442 struct wined3d_light_info
*dst_light
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_light
));
444 *dst_light
= *src_light
;
445 list_add_tail(&stateblock
->state
.light_map
[i
], &dst_light
->entry
);
450 ULONG CDECL
wined3d_stateblock_incref(struct wined3d_stateblock
*stateblock
)
452 ULONG refcount
= InterlockedIncrement(&stateblock
->ref
);
454 TRACE("%p increasing refcount to %u.\n", stateblock
, refcount
);
459 void state_unbind_resources(struct wined3d_state
*state
)
461 struct wined3d_vertex_declaration
*decl
;
462 struct wined3d_sampler
*sampler
;
463 struct wined3d_texture
*texture
;
464 struct wined3d_buffer
*buffer
;
465 struct wined3d_shader
*shader
;
468 if ((decl
= state
->vertex_declaration
))
470 state
->vertex_declaration
= NULL
;
471 wined3d_vertex_declaration_decref(decl
);
474 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
476 if ((texture
= state
->textures
[i
]))
478 state
->textures
[i
] = NULL
;
479 wined3d_texture_decref(texture
);
483 for (i
= 0; i
< MAX_STREAM_OUT
; ++i
)
485 if ((buffer
= state
->stream_output
[i
].buffer
))
487 state
->stream_output
[i
].buffer
= NULL
;
488 wined3d_buffer_decref(buffer
);
492 for (i
= 0; i
< MAX_STREAMS
; ++i
)
494 if ((buffer
= state
->streams
[i
].buffer
))
496 state
->streams
[i
].buffer
= NULL
;
497 wined3d_buffer_decref(buffer
);
501 if ((buffer
= state
->index_buffer
))
503 state
->index_buffer
= NULL
;
504 wined3d_buffer_decref(buffer
);
507 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
509 if ((shader
= state
->shader
[i
]))
511 state
->shader
[i
] = NULL
;
512 wined3d_shader_decref(shader
);
515 for (j
= 0; j
< MAX_CONSTANT_BUFFERS
; ++j
)
517 if ((buffer
= state
->cb
[i
][j
]))
519 state
->cb
[i
][j
] = NULL
;
520 wined3d_buffer_decref(buffer
);
524 for (j
= 0; j
< MAX_SAMPLER_OBJECTS
; ++j
)
526 if ((sampler
= state
->sampler
[i
][j
]))
528 state
->sampler
[i
][j
] = NULL
;
529 wined3d_sampler_decref(sampler
);
535 void state_cleanup(struct wined3d_state
*state
)
537 unsigned int counter
;
539 if (!(state
->flags
& WINED3D_STATE_NO_REF
))
540 state_unbind_resources(state
);
542 for (counter
= 0; counter
< MAX_ACTIVE_LIGHTS
; ++counter
)
544 state
->lights
[counter
] = NULL
;
547 for (counter
= 0; counter
< LIGHTMAP_SIZE
; ++counter
)
549 struct list
*e1
, *e2
;
550 LIST_FOR_EACH_SAFE(e1
, e2
, &state
->light_map
[counter
])
552 struct wined3d_light_info
*light
= LIST_ENTRY(e1
, struct wined3d_light_info
, entry
);
553 list_remove(&light
->entry
);
554 HeapFree(GetProcessHeap(), 0, light
);
558 HeapFree(GetProcessHeap(), 0, state
->vs_consts_f
);
559 HeapFree(GetProcessHeap(), 0, state
->ps_consts_f
);
562 ULONG CDECL
wined3d_stateblock_decref(struct wined3d_stateblock
*stateblock
)
564 ULONG refcount
= InterlockedDecrement(&stateblock
->ref
);
566 TRACE("%p decreasing refcount to %u\n", stateblock
, refcount
);
570 state_cleanup(&stateblock
->state
);
572 HeapFree(GetProcessHeap(), 0, stateblock
->changed
.vertexShaderConstantsF
);
573 HeapFree(GetProcessHeap(), 0, stateblock
->changed
.pixelShaderConstantsF
);
574 HeapFree(GetProcessHeap(), 0, stateblock
->contained_vs_consts_f
);
575 HeapFree(GetProcessHeap(), 0, stateblock
->contained_ps_consts_f
);
576 HeapFree(GetProcessHeap(), 0, stateblock
);
582 static void wined3d_state_record_lights(struct wined3d_state
*dst_state
, const struct wined3d_state
*src_state
)
586 /* Lights... For a recorded state block, we just had a chain of actions
587 * to perform, so we need to walk that chain and update any actions which
589 for (i
= 0; i
< LIGHTMAP_SIZE
; ++i
)
592 LIST_FOR_EACH(e
, &dst_state
->light_map
[i
])
594 BOOL updated
= FALSE
;
595 struct wined3d_light_info
*src
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
), *realLight
;
597 /* Look up the light in the destination */
598 LIST_FOR_EACH(f
, &src_state
->light_map
[i
])
600 realLight
= LIST_ENTRY(f
, struct wined3d_light_info
, entry
);
601 if (realLight
->OriginalIndex
== src
->OriginalIndex
)
603 src
->OriginalParms
= realLight
->OriginalParms
;
605 if (realLight
->glIndex
== -1 && src
->glIndex
!= -1)
608 dst_state
->lights
[src
->glIndex
] = NULL
;
610 else if (realLight
->glIndex
!= -1 && src
->glIndex
== -1)
613 dst_state
->lights
[realLight
->glIndex
] = src
;
615 src
->glIndex
= realLight
->glIndex
;
623 /* This can happen if the light was originally created as a
624 * default light for SetLightEnable() while recording. */
625 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
626 src
->OriginalIndex
, dst_state
, src_state
);
628 src
->OriginalParms
= WINED3D_default_light
;
629 if (src
->glIndex
!= -1)
631 dst_state
->lights
[src
->glIndex
] = NULL
;
639 void CDECL
wined3d_stateblock_capture(struct wined3d_stateblock
*stateblock
)
641 const struct wined3d_state
*src_state
= &stateblock
->device
->state
;
645 TRACE("stateblock %p.\n", stateblock
);
647 TRACE("Capturing state %p.\n", src_state
);
649 if (stateblock
->changed
.vertexShader
&& stateblock
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
]
650 != src_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
652 TRACE("Updating vertex shader from %p to %p\n",
653 stateblock
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
],
654 src_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]);
656 if (src_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
])
657 wined3d_shader_incref(src_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]);
658 if (stateblock
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
])
659 wined3d_shader_decref(stateblock
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
]);
660 stateblock
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
] = src_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
663 /* Vertex shader float constants. */
664 for (i
= 0; i
< stateblock
->num_contained_vs_consts_f
; ++i
)
666 unsigned int idx
= stateblock
->contained_vs_consts_f
[i
];
668 TRACE("Setting vs_consts_f[%u] to {%.8e, %.8e, %.8e, %.8e}.\n", idx
,
669 src_state
->vs_consts_f
[idx
* 4 + 0],
670 src_state
->vs_consts_f
[idx
* 4 + 1],
671 src_state
->vs_consts_f
[idx
* 4 + 2],
672 src_state
->vs_consts_f
[idx
* 4 + 3]);
674 stateblock
->state
.vs_consts_f
[idx
* 4 + 0] = src_state
->vs_consts_f
[idx
* 4 + 0];
675 stateblock
->state
.vs_consts_f
[idx
* 4 + 1] = src_state
->vs_consts_f
[idx
* 4 + 1];
676 stateblock
->state
.vs_consts_f
[idx
* 4 + 2] = src_state
->vs_consts_f
[idx
* 4 + 2];
677 stateblock
->state
.vs_consts_f
[idx
* 4 + 3] = src_state
->vs_consts_f
[idx
* 4 + 3];
680 /* Vertex shader integer constants. */
681 for (i
= 0; i
< stateblock
->num_contained_vs_consts_i
; ++i
)
683 unsigned int idx
= stateblock
->contained_vs_consts_i
[i
];
685 TRACE("Setting vs_consts[%u] to {%d, %d, %d, %d}.\n", idx
,
686 src_state
->vs_consts_i
[idx
* 4 + 0],
687 src_state
->vs_consts_i
[idx
* 4 + 1],
688 src_state
->vs_consts_i
[idx
* 4 + 2],
689 src_state
->vs_consts_i
[idx
* 4 + 3]);
691 stateblock
->state
.vs_consts_i
[idx
* 4 + 0] = src_state
->vs_consts_i
[idx
* 4 + 0];
692 stateblock
->state
.vs_consts_i
[idx
* 4 + 1] = src_state
->vs_consts_i
[idx
* 4 + 1];
693 stateblock
->state
.vs_consts_i
[idx
* 4 + 2] = src_state
->vs_consts_i
[idx
* 4 + 2];
694 stateblock
->state
.vs_consts_i
[idx
* 4 + 3] = src_state
->vs_consts_i
[idx
* 4 + 3];
697 /* Vertex shader boolean constants. */
698 for (i
= 0; i
< stateblock
->num_contained_vs_consts_b
; ++i
)
700 unsigned int idx
= stateblock
->contained_vs_consts_b
[i
];
702 TRACE("Setting vs_consts_b[%u] to %s.\n",
703 idx
, src_state
->vs_consts_b
[idx
] ? "TRUE" : "FALSE");
705 stateblock
->state
.vs_consts_b
[idx
] = src_state
->vs_consts_b
[idx
];
708 /* Pixel shader float constants. */
709 for (i
= 0; i
< stateblock
->num_contained_ps_consts_f
; ++i
)
711 unsigned int idx
= stateblock
->contained_ps_consts_f
[i
];
713 TRACE("Setting ps_consts_f[%u] to {%.8e, %.8e, %.8e, %.8e}.\n", idx
,
714 src_state
->ps_consts_f
[idx
* 4 + 0],
715 src_state
->ps_consts_f
[idx
* 4 + 1],
716 src_state
->ps_consts_f
[idx
* 4 + 2],
717 src_state
->ps_consts_f
[idx
* 4 + 3]);
719 stateblock
->state
.ps_consts_f
[idx
* 4 + 0] = src_state
->ps_consts_f
[idx
* 4 + 0];
720 stateblock
->state
.ps_consts_f
[idx
* 4 + 1] = src_state
->ps_consts_f
[idx
* 4 + 1];
721 stateblock
->state
.ps_consts_f
[idx
* 4 + 2] = src_state
->ps_consts_f
[idx
* 4 + 2];
722 stateblock
->state
.ps_consts_f
[idx
* 4 + 3] = src_state
->ps_consts_f
[idx
* 4 + 3];
725 /* Pixel shader integer constants. */
726 for (i
= 0; i
< stateblock
->num_contained_ps_consts_i
; ++i
)
728 unsigned int idx
= stateblock
->contained_ps_consts_i
[i
];
729 TRACE("Setting ps_consts_i[%u] to {%d, %d, %d, %d}.\n", idx
,
730 src_state
->ps_consts_i
[idx
* 4 + 0],
731 src_state
->ps_consts_i
[idx
* 4 + 1],
732 src_state
->ps_consts_i
[idx
* 4 + 2],
733 src_state
->ps_consts_i
[idx
* 4 + 3]);
735 stateblock
->state
.ps_consts_i
[idx
* 4 + 0] = src_state
->ps_consts_i
[idx
* 4 + 0];
736 stateblock
->state
.ps_consts_i
[idx
* 4 + 1] = src_state
->ps_consts_i
[idx
* 4 + 1];
737 stateblock
->state
.ps_consts_i
[idx
* 4 + 2] = src_state
->ps_consts_i
[idx
* 4 + 2];
738 stateblock
->state
.ps_consts_i
[idx
* 4 + 3] = src_state
->ps_consts_i
[idx
* 4 + 3];
741 /* Pixel shader boolean constants. */
742 for (i
= 0; i
< stateblock
->num_contained_ps_consts_b
; ++i
)
744 unsigned int idx
= stateblock
->contained_ps_consts_b
[i
];
745 TRACE("Setting ps_consts_b[%u] to %s.\n",
746 idx
, src_state
->ps_consts_b
[idx
] ? "TRUE" : "FALSE");
748 stateblock
->state
.ps_consts_b
[idx
] = src_state
->ps_consts_b
[idx
];
751 /* Others + Render & Texture */
752 for (i
= 0; i
< stateblock
->num_contained_transform_states
; ++i
)
754 enum wined3d_transform_state transform
= stateblock
->contained_transform_states
[i
];
756 TRACE("Updating transform %#x.\n", transform
);
758 stateblock
->state
.transforms
[transform
] = src_state
->transforms
[transform
];
761 if (stateblock
->changed
.primitive_type
)
762 stateblock
->state
.gl_primitive_type
= src_state
->gl_primitive_type
;
764 if (stateblock
->changed
.indices
765 && ((stateblock
->state
.index_buffer
!= src_state
->index_buffer
)
766 || (stateblock
->state
.base_vertex_index
!= src_state
->base_vertex_index
)
767 || (stateblock
->state
.index_format
!= src_state
->index_format
)))
769 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
770 src_state
->index_buffer
, src_state
->base_vertex_index
);
772 if (src_state
->index_buffer
)
773 wined3d_buffer_incref(src_state
->index_buffer
);
774 if (stateblock
->state
.index_buffer
)
775 wined3d_buffer_decref(stateblock
->state
.index_buffer
);
776 stateblock
->state
.index_buffer
= src_state
->index_buffer
;
777 stateblock
->state
.base_vertex_index
= src_state
->base_vertex_index
;
778 stateblock
->state
.index_format
= src_state
->index_format
;
781 if (stateblock
->changed
.vertexDecl
&& stateblock
->state
.vertex_declaration
!= src_state
->vertex_declaration
)
783 TRACE("Updating vertex declaration from %p to %p.\n",
784 stateblock
->state
.vertex_declaration
, src_state
->vertex_declaration
);
786 if (src_state
->vertex_declaration
)
787 wined3d_vertex_declaration_incref(src_state
->vertex_declaration
);
788 if (stateblock
->state
.vertex_declaration
)
789 wined3d_vertex_declaration_decref(stateblock
->state
.vertex_declaration
);
790 stateblock
->state
.vertex_declaration
= src_state
->vertex_declaration
;
793 if (stateblock
->changed
.material
794 && memcmp(&src_state
->material
, &stateblock
->state
.material
, sizeof(stateblock
->state
.material
)))
796 TRACE("Updating material.\n");
798 stateblock
->state
.material
= src_state
->material
;
801 if (stateblock
->changed
.viewport
802 && memcmp(&src_state
->viewport
, &stateblock
->state
.viewport
, sizeof(stateblock
->state
.viewport
)))
804 TRACE("Updating viewport.\n");
806 stateblock
->state
.viewport
= src_state
->viewport
;
809 if (stateblock
->changed
.scissorRect
&& memcmp(&src_state
->scissor_rect
,
810 &stateblock
->state
.scissor_rect
, sizeof(stateblock
->state
.scissor_rect
)))
812 TRACE("Updating scissor rect.\n");
814 stateblock
->state
.scissor_rect
= src_state
->scissor_rect
;
817 map
= stateblock
->changed
.streamSource
;
818 for (i
= 0; map
; map
>>= 1, ++i
)
820 if (!(map
& 1)) continue;
822 if (stateblock
->state
.streams
[i
].stride
!= src_state
->streams
[i
].stride
823 || stateblock
->state
.streams
[i
].buffer
!= src_state
->streams
[i
].buffer
)
825 TRACE("Updating stream source %u to %p, stride to %u.\n",
826 i
, src_state
->streams
[i
].buffer
,
827 src_state
->streams
[i
].stride
);
829 stateblock
->state
.streams
[i
].stride
= src_state
->streams
[i
].stride
;
830 if (src_state
->streams
[i
].buffer
)
831 wined3d_buffer_incref(src_state
->streams
[i
].buffer
);
832 if (stateblock
->state
.streams
[i
].buffer
)
833 wined3d_buffer_decref(stateblock
->state
.streams
[i
].buffer
);
834 stateblock
->state
.streams
[i
].buffer
= src_state
->streams
[i
].buffer
;
838 map
= stateblock
->changed
.streamFreq
;
839 for (i
= 0; map
; map
>>= 1, ++i
)
841 if (!(map
& 1)) continue;
843 if (stateblock
->state
.streams
[i
].frequency
!= src_state
->streams
[i
].frequency
844 || stateblock
->state
.streams
[i
].flags
!= src_state
->streams
[i
].flags
)
846 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
847 i
, src_state
->streams
[i
].frequency
, src_state
->streams
[i
].flags
);
849 stateblock
->state
.streams
[i
].frequency
= src_state
->streams
[i
].frequency
;
850 stateblock
->state
.streams
[i
].flags
= src_state
->streams
[i
].flags
;
854 map
= stateblock
->changed
.clipplane
;
855 for (i
= 0; map
; map
>>= 1, ++i
)
857 if (!(map
& 1)) continue;
859 if (memcmp(&stateblock
->state
.clip_planes
[i
], &src_state
->clip_planes
[i
], sizeof(src_state
->clip_planes
[i
])))
861 TRACE("Updating clipplane %u.\n", i
);
862 stateblock
->state
.clip_planes
[i
] = src_state
->clip_planes
[i
];
867 for (i
= 0; i
< stateblock
->num_contained_render_states
; ++i
)
869 enum wined3d_render_state rs
= stateblock
->contained_render_states
[i
];
871 TRACE("Updating render state %#x to %u.\n", rs
, src_state
->render_states
[rs
]);
873 stateblock
->state
.render_states
[rs
] = src_state
->render_states
[rs
];
877 for (i
= 0; i
< stateblock
->num_contained_tss_states
; ++i
)
879 DWORD stage
= stateblock
->contained_tss_states
[i
].stage
;
880 DWORD state
= stateblock
->contained_tss_states
[i
].state
;
882 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage
, state
,
883 src_state
->texture_states
[stage
][state
], stateblock
->state
.texture_states
[stage
][state
]);
885 stateblock
->state
.texture_states
[stage
][state
] = src_state
->texture_states
[stage
][state
];
889 map
= stateblock
->changed
.textures
;
890 for (i
= 0; map
; map
>>= 1, ++i
)
892 if (!(map
& 1)) continue;
894 TRACE("Updating texture %u to %p (was %p).\n",
895 i
, src_state
->textures
[i
], stateblock
->state
.textures
[i
]);
897 if (src_state
->textures
[i
])
898 wined3d_texture_incref(src_state
->textures
[i
]);
899 if (stateblock
->state
.textures
[i
])
900 wined3d_texture_decref(stateblock
->state
.textures
[i
]);
901 stateblock
->state
.textures
[i
] = src_state
->textures
[i
];
904 for (i
= 0; i
< stateblock
->num_contained_sampler_states
; ++i
)
906 DWORD stage
= stateblock
->contained_sampler_states
[i
].stage
;
907 DWORD state
= stateblock
->contained_sampler_states
[i
].state
;
909 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage
, state
,
910 src_state
->sampler_states
[stage
][state
], stateblock
->state
.sampler_states
[stage
][state
]);
912 stateblock
->state
.sampler_states
[stage
][state
] = src_state
->sampler_states
[stage
][state
];
915 if (stateblock
->changed
.pixelShader
&& stateblock
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
]
916 != src_state
->shader
[WINED3D_SHADER_TYPE_PIXEL
])
918 if (src_state
->shader
[WINED3D_SHADER_TYPE_PIXEL
])
919 wined3d_shader_incref(src_state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]);
920 if (stateblock
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
])
921 wined3d_shader_decref(stateblock
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
]);
922 stateblock
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
] = src_state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
925 wined3d_state_record_lights(&stateblock
->state
, src_state
);
927 TRACE("Capture done.\n");
930 static void apply_lights(struct wined3d_device
*device
, const struct wined3d_state
*state
)
934 for (i
= 0; i
< LIGHTMAP_SIZE
; ++i
)
938 LIST_FOR_EACH(e
, &state
->light_map
[i
])
940 const struct wined3d_light_info
*light
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
942 wined3d_device_set_light(device
, light
->OriginalIndex
, &light
->OriginalParms
);
943 wined3d_device_set_light_enable(device
, light
->OriginalIndex
, light
->glIndex
!= -1);
948 void CDECL
wined3d_stateblock_apply(const struct wined3d_stateblock
*stateblock
)
950 struct wined3d_device
*device
= stateblock
->device
;
954 TRACE("Applying stateblock %p to device %p.\n", stateblock
, device
);
956 if (stateblock
->changed
.vertexShader
)
957 wined3d_device_set_vertex_shader(device
, stateblock
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
]);
959 /* Vertex Shader Constants. */
960 for (i
= 0; i
< stateblock
->num_contained_vs_consts_f
; ++i
)
962 wined3d_device_set_vs_consts_f(device
, stateblock
->contained_vs_consts_f
[i
],
963 stateblock
->state
.vs_consts_f
+ stateblock
->contained_vs_consts_f
[i
] * 4, 1);
965 for (i
= 0; i
< stateblock
->num_contained_vs_consts_i
; ++i
)
967 wined3d_device_set_vs_consts_i(device
, stateblock
->contained_vs_consts_i
[i
],
968 stateblock
->state
.vs_consts_i
+ stateblock
->contained_vs_consts_i
[i
] * 4, 1);
970 for (i
= 0; i
< stateblock
->num_contained_vs_consts_b
; ++i
)
972 wined3d_device_set_vs_consts_b(device
, stateblock
->contained_vs_consts_b
[i
],
973 stateblock
->state
.vs_consts_b
+ stateblock
->contained_vs_consts_b
[i
], 1);
976 apply_lights(device
, &stateblock
->state
);
978 if (stateblock
->changed
.pixelShader
)
979 wined3d_device_set_pixel_shader(device
, stateblock
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
]);
981 /* Pixel Shader Constants. */
982 for (i
= 0; i
< stateblock
->num_contained_ps_consts_f
; ++i
)
984 wined3d_device_set_ps_consts_f(device
, stateblock
->contained_ps_consts_f
[i
],
985 stateblock
->state
.ps_consts_f
+ stateblock
->contained_ps_consts_f
[i
] * 4, 1);
987 for (i
= 0; i
< stateblock
->num_contained_ps_consts_i
; ++i
)
989 wined3d_device_set_ps_consts_i(device
, stateblock
->contained_ps_consts_i
[i
],
990 stateblock
->state
.ps_consts_i
+ stateblock
->contained_ps_consts_i
[i
] * 4, 1);
992 for (i
= 0; i
< stateblock
->num_contained_ps_consts_b
; ++i
)
994 wined3d_device_set_ps_consts_b(device
, stateblock
->contained_ps_consts_b
[i
],
995 stateblock
->state
.ps_consts_b
+ stateblock
->contained_ps_consts_b
[i
], 1);
999 for (i
= 0; i
< stateblock
->num_contained_render_states
; ++i
)
1001 wined3d_device_set_render_state(device
, stateblock
->contained_render_states
[i
],
1002 stateblock
->state
.render_states
[stateblock
->contained_render_states
[i
]]);
1005 /* Texture states. */
1006 for (i
= 0; i
< stateblock
->num_contained_tss_states
; ++i
)
1008 DWORD stage
= stateblock
->contained_tss_states
[i
].stage
;
1009 DWORD state
= stateblock
->contained_tss_states
[i
].state
;
1011 wined3d_device_set_texture_stage_state(device
, stage
, state
, stateblock
->state
.texture_states
[stage
][state
]);
1014 /* Sampler states. */
1015 for (i
= 0; i
< stateblock
->num_contained_sampler_states
; ++i
)
1017 DWORD stage
= stateblock
->contained_sampler_states
[i
].stage
;
1018 DWORD state
= stateblock
->contained_sampler_states
[i
].state
;
1019 DWORD value
= stateblock
->state
.sampler_states
[stage
][state
];
1021 if (stage
>= MAX_FRAGMENT_SAMPLERS
) stage
+= WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
;
1022 wined3d_device_set_sampler_state(device
, stage
, state
, value
);
1025 /* Transform states. */
1026 for (i
= 0; i
< stateblock
->num_contained_transform_states
; ++i
)
1028 wined3d_device_set_transform(device
, stateblock
->contained_transform_states
[i
],
1029 &stateblock
->state
.transforms
[stateblock
->contained_transform_states
[i
]]);
1032 if (stateblock
->changed
.primitive_type
)
1034 GLenum gl_primitive_type
, prev
;
1036 if (device
->recording
)
1037 device
->recording
->changed
.primitive_type
= TRUE
;
1038 gl_primitive_type
= stateblock
->state
.gl_primitive_type
;
1039 prev
= device
->update_state
->gl_primitive_type
;
1040 device
->update_state
->gl_primitive_type
= gl_primitive_type
;
1041 if (gl_primitive_type
!= prev
&& (gl_primitive_type
== GL_POINTS
|| prev
== GL_POINTS
))
1042 device_invalidate_state(device
, STATE_POINT_SIZE_ENABLE
);
1045 if (stateblock
->changed
.indices
)
1047 wined3d_device_set_index_buffer(device
, stateblock
->state
.index_buffer
, stateblock
->state
.index_format
);
1048 wined3d_device_set_base_vertex_index(device
, stateblock
->state
.base_vertex_index
);
1051 if (stateblock
->changed
.vertexDecl
&& stateblock
->state
.vertex_declaration
)
1052 wined3d_device_set_vertex_declaration(device
, stateblock
->state
.vertex_declaration
);
1054 if (stateblock
->changed
.material
)
1055 wined3d_device_set_material(device
, &stateblock
->state
.material
);
1057 if (stateblock
->changed
.viewport
)
1058 wined3d_device_set_viewport(device
, &stateblock
->state
.viewport
);
1060 if (stateblock
->changed
.scissorRect
)
1061 wined3d_device_set_scissor_rect(device
, &stateblock
->state
.scissor_rect
);
1063 map
= stateblock
->changed
.streamSource
;
1064 for (i
= 0; map
; map
>>= 1, ++i
)
1067 wined3d_device_set_stream_source(device
, i
,
1068 stateblock
->state
.streams
[i
].buffer
,
1069 0, stateblock
->state
.streams
[i
].stride
);
1072 map
= stateblock
->changed
.streamFreq
;
1073 for (i
= 0; map
; map
>>= 1, ++i
)
1076 wined3d_device_set_stream_source_freq(device
, i
,
1077 stateblock
->state
.streams
[i
].frequency
| stateblock
->state
.streams
[i
].flags
);
1080 map
= stateblock
->changed
.textures
;
1081 for (i
= 0; map
; map
>>= 1, ++i
)
1085 if (!(map
& 1)) continue;
1087 stage
= i
< MAX_FRAGMENT_SAMPLERS
? i
: WINED3DVERTEXTEXTURESAMPLER0
+ i
- MAX_FRAGMENT_SAMPLERS
;
1088 wined3d_device_set_texture(device
, stage
, stateblock
->state
.textures
[i
]);
1091 map
= stateblock
->changed
.clipplane
;
1092 for (i
= 0; map
; map
>>= 1, ++i
)
1094 if (!(map
& 1)) continue;
1096 wined3d_device_set_clip_plane(device
, i
, &stateblock
->state
.clip_planes
[i
]);
1099 TRACE("Applied stateblock %p.\n", stateblock
);
1102 static void state_init_default(struct wined3d_state
*state
, const struct wined3d_gl_info
*gl_info
)
1106 struct wined3d_line_pattern lp
;
1114 static const struct wined3d_matrix identity
=
1116 1.0f
, 0.0f
, 0.0f
, 0.0f
,
1117 0.0f
, 1.0f
, 0.0f
, 0.0f
,
1118 0.0f
, 0.0f
, 1.0f
, 0.0f
,
1119 0.0f
, 0.0f
, 0.0f
, 1.0f
,
1122 TRACE("state %p, gl_info %p.\n", state
, gl_info
);
1124 state
->gl_primitive_type
= ~0u;
1126 /* Set some of the defaults for lights, transforms etc */
1127 state
->transforms
[WINED3D_TS_PROJECTION
] = identity
;
1128 state
->transforms
[WINED3D_TS_VIEW
] = identity
;
1129 for (i
= 0; i
< 256; ++i
)
1131 state
->transforms
[WINED3D_TS_WORLD_MATRIX(i
)] = identity
;
1134 TRACE("Render states\n");
1135 /* Render states: */
1136 state
->render_states
[WINED3D_RS_ZENABLE
] = WINED3D_ZB_TRUE
;
1137 state
->render_states
[WINED3D_RS_FILLMODE
] = WINED3D_FILL_SOLID
;
1138 state
->render_states
[WINED3D_RS_SHADEMODE
] = WINED3D_SHADE_GOURAUD
;
1139 lp
.lp
.repeat_factor
= 0;
1140 lp
.lp
.line_pattern
= 0;
1141 state
->render_states
[WINED3D_RS_LINEPATTERN
] = lp
.d
;
1142 state
->render_states
[WINED3D_RS_ZWRITEENABLE
] = TRUE
;
1143 state
->render_states
[WINED3D_RS_ALPHATESTENABLE
] = FALSE
;
1144 state
->render_states
[WINED3D_RS_LASTPIXEL
] = TRUE
;
1145 state
->render_states
[WINED3D_RS_SRCBLEND
] = WINED3D_BLEND_ONE
;
1146 state
->render_states
[WINED3D_RS_DESTBLEND
] = WINED3D_BLEND_ZERO
;
1147 state
->render_states
[WINED3D_RS_CULLMODE
] = WINED3D_CULL_CCW
;
1148 state
->render_states
[WINED3D_RS_ZFUNC
] = WINED3D_CMP_LESSEQUAL
;
1149 state
->render_states
[WINED3D_RS_ALPHAFUNC
] = WINED3D_CMP_ALWAYS
;
1150 state
->render_states
[WINED3D_RS_ALPHAREF
] = 0;
1151 state
->render_states
[WINED3D_RS_DITHERENABLE
] = FALSE
;
1152 state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] = FALSE
;
1153 state
->render_states
[WINED3D_RS_FOGENABLE
] = FALSE
;
1154 state
->render_states
[WINED3D_RS_SPECULARENABLE
] = FALSE
;
1155 state
->render_states
[WINED3D_RS_ZVISIBLE
] = 0;
1156 state
->render_states
[WINED3D_RS_FOGCOLOR
] = 0;
1157 state
->render_states
[WINED3D_RS_FOGTABLEMODE
] = WINED3D_FOG_NONE
;
1159 state
->render_states
[WINED3D_RS_FOGSTART
] = tmpfloat
.d
;
1161 state
->render_states
[WINED3D_RS_FOGEND
] = tmpfloat
.d
;
1163 state
->render_states
[WINED3D_RS_FOGDENSITY
] = tmpfloat
.d
;
1164 state
->render_states
[WINED3D_RS_EDGEANTIALIAS
] = FALSE
;
1165 state
->render_states
[WINED3D_RS_RANGEFOGENABLE
] = FALSE
;
1166 state
->render_states
[WINED3D_RS_STENCILENABLE
] = FALSE
;
1167 state
->render_states
[WINED3D_RS_STENCILFAIL
] = WINED3D_STENCIL_OP_KEEP
;
1168 state
->render_states
[WINED3D_RS_STENCILZFAIL
] = WINED3D_STENCIL_OP_KEEP
;
1169 state
->render_states
[WINED3D_RS_STENCILPASS
] = WINED3D_STENCIL_OP_KEEP
;
1170 state
->render_states
[WINED3D_RS_STENCILREF
] = 0;
1171 state
->render_states
[WINED3D_RS_STENCILMASK
] = 0xffffffff;
1172 state
->render_states
[WINED3D_RS_STENCILFUNC
] = WINED3D_CMP_ALWAYS
;
1173 state
->render_states
[WINED3D_RS_STENCILWRITEMASK
] = 0xffffffff;
1174 state
->render_states
[WINED3D_RS_TEXTUREFACTOR
] = 0xffffffff;
1175 state
->render_states
[WINED3D_RS_WRAP0
] = 0;
1176 state
->render_states
[WINED3D_RS_WRAP1
] = 0;
1177 state
->render_states
[WINED3D_RS_WRAP2
] = 0;
1178 state
->render_states
[WINED3D_RS_WRAP3
] = 0;
1179 state
->render_states
[WINED3D_RS_WRAP4
] = 0;
1180 state
->render_states
[WINED3D_RS_WRAP5
] = 0;
1181 state
->render_states
[WINED3D_RS_WRAP6
] = 0;
1182 state
->render_states
[WINED3D_RS_WRAP7
] = 0;
1183 state
->render_states
[WINED3D_RS_CLIPPING
] = TRUE
;
1184 state
->render_states
[WINED3D_RS_LIGHTING
] = TRUE
;
1185 state
->render_states
[WINED3D_RS_AMBIENT
] = 0;
1186 state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] = WINED3D_FOG_NONE
;
1187 state
->render_states
[WINED3D_RS_COLORVERTEX
] = TRUE
;
1188 state
->render_states
[WINED3D_RS_LOCALVIEWER
] = TRUE
;
1189 state
->render_states
[WINED3D_RS_NORMALIZENORMALS
] = FALSE
;
1190 state
->render_states
[WINED3D_RS_DIFFUSEMATERIALSOURCE
] = WINED3D_MCS_COLOR1
;
1191 state
->render_states
[WINED3D_RS_SPECULARMATERIALSOURCE
] = WINED3D_MCS_COLOR2
;
1192 state
->render_states
[WINED3D_RS_AMBIENTMATERIALSOURCE
] = WINED3D_MCS_MATERIAL
;
1193 state
->render_states
[WINED3D_RS_EMISSIVEMATERIALSOURCE
] = WINED3D_MCS_MATERIAL
;
1194 state
->render_states
[WINED3D_RS_VERTEXBLEND
] = WINED3D_VBF_DISABLE
;
1195 state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
] = 0;
1196 state
->render_states
[WINED3D_RS_SOFTWAREVERTEXPROCESSING
] = FALSE
;
1198 state
->render_states
[WINED3D_RS_POINTSIZE
] = tmpfloat
.d
;
1200 state
->render_states
[WINED3D_RS_POINTSIZE_MIN
] = tmpfloat
.d
;
1201 state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
] = FALSE
;
1202 state
->render_states
[WINED3D_RS_POINTSCALEENABLE
] = FALSE
;
1204 state
->render_states
[WINED3D_RS_POINTSCALE_A
] = tmpfloat
.d
;
1206 state
->render_states
[WINED3D_RS_POINTSCALE_B
] = tmpfloat
.d
;
1208 state
->render_states
[WINED3D_RS_POINTSCALE_C
] = tmpfloat
.d
;
1209 state
->render_states
[WINED3D_RS_MULTISAMPLEANTIALIAS
] = TRUE
;
1210 state
->render_states
[WINED3D_RS_MULTISAMPLEMASK
] = 0xffffffff;
1211 state
->render_states
[WINED3D_RS_PATCHEDGESTYLE
] = WINED3D_PATCH_EDGE_DISCRETE
;
1213 state
->render_states
[WINED3D_RS_PATCHSEGMENTS
] = tmpfloat
.d
;
1214 state
->render_states
[WINED3D_RS_DEBUGMONITORTOKEN
] = 0xbaadcafe;
1215 tmpfloat
.f
= gl_info
->limits
.pointsize_max
;
1216 state
->render_states
[WINED3D_RS_POINTSIZE_MAX
] = tmpfloat
.d
;
1217 state
->render_states
[WINED3D_RS_INDEXEDVERTEXBLENDENABLE
] = FALSE
;
1218 state
->render_states
[WINED3D_RS_COLORWRITEENABLE
] = 0x0000000f;
1220 state
->render_states
[WINED3D_RS_TWEENFACTOR
] = tmpfloat
.d
;
1221 state
->render_states
[WINED3D_RS_BLENDOP
] = WINED3D_BLEND_OP_ADD
;
1222 state
->render_states
[WINED3D_RS_POSITIONDEGREE
] = WINED3D_DEGREE_CUBIC
;
1223 state
->render_states
[WINED3D_RS_NORMALDEGREE
] = WINED3D_DEGREE_LINEAR
;
1224 /* states new in d3d9 */
1225 state
->render_states
[WINED3D_RS_SCISSORTESTENABLE
] = FALSE
;
1226 state
->render_states
[WINED3D_RS_SLOPESCALEDEPTHBIAS
] = 0;
1228 state
->render_states
[WINED3D_RS_MINTESSELLATIONLEVEL
] = tmpfloat
.d
;
1229 state
->render_states
[WINED3D_RS_MAXTESSELLATIONLEVEL
] = tmpfloat
.d
;
1230 state
->render_states
[WINED3D_RS_ANTIALIASEDLINEENABLE
] = FALSE
;
1232 state
->render_states
[WINED3D_RS_ADAPTIVETESS_X
] = tmpfloat
.d
;
1233 state
->render_states
[WINED3D_RS_ADAPTIVETESS_Y
] = tmpfloat
.d
;
1235 state
->render_states
[WINED3D_RS_ADAPTIVETESS_Z
] = tmpfloat
.d
;
1237 state
->render_states
[WINED3D_RS_ADAPTIVETESS_W
] = tmpfloat
.d
;
1238 state
->render_states
[WINED3D_RS_ENABLEADAPTIVETESSELLATION
] = FALSE
;
1239 state
->render_states
[WINED3D_RS_TWOSIDEDSTENCILMODE
] = FALSE
;
1240 state
->render_states
[WINED3D_RS_CCW_STENCILFAIL
] = WINED3D_STENCIL_OP_KEEP
;
1241 state
->render_states
[WINED3D_RS_CCW_STENCILZFAIL
] = WINED3D_STENCIL_OP_KEEP
;
1242 state
->render_states
[WINED3D_RS_CCW_STENCILPASS
] = WINED3D_STENCIL_OP_KEEP
;
1243 state
->render_states
[WINED3D_RS_CCW_STENCILFUNC
] = WINED3D_CMP_ALWAYS
;
1244 state
->render_states
[WINED3D_RS_COLORWRITEENABLE1
] = 0x0000000f;
1245 state
->render_states
[WINED3D_RS_COLORWRITEENABLE2
] = 0x0000000f;
1246 state
->render_states
[WINED3D_RS_COLORWRITEENABLE3
] = 0x0000000f;
1247 state
->render_states
[WINED3D_RS_BLENDFACTOR
] = 0xffffffff;
1248 state
->render_states
[WINED3D_RS_SRGBWRITEENABLE
] = 0;
1249 state
->render_states
[WINED3D_RS_DEPTHBIAS
] = 0;
1250 state
->render_states
[WINED3D_RS_WRAP8
] = 0;
1251 state
->render_states
[WINED3D_RS_WRAP9
] = 0;
1252 state
->render_states
[WINED3D_RS_WRAP10
] = 0;
1253 state
->render_states
[WINED3D_RS_WRAP11
] = 0;
1254 state
->render_states
[WINED3D_RS_WRAP12
] = 0;
1255 state
->render_states
[WINED3D_RS_WRAP13
] = 0;
1256 state
->render_states
[WINED3D_RS_WRAP14
] = 0;
1257 state
->render_states
[WINED3D_RS_WRAP15
] = 0;
1258 state
->render_states
[WINED3D_RS_SEPARATEALPHABLENDENABLE
] = FALSE
;
1259 state
->render_states
[WINED3D_RS_SRCBLENDALPHA
] = WINED3D_BLEND_ONE
;
1260 state
->render_states
[WINED3D_RS_DESTBLENDALPHA
] = WINED3D_BLEND_ZERO
;
1261 state
->render_states
[WINED3D_RS_BLENDOPALPHA
] = WINED3D_BLEND_OP_ADD
;
1263 /* Texture Stage States - Put directly into state block, we will call function below */
1264 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1266 TRACE("Setting up default texture states for texture Stage %u.\n", i
);
1267 state
->transforms
[WINED3D_TS_TEXTURE0
+ i
] = identity
;
1268 state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
] = i
? WINED3D_TOP_DISABLE
: WINED3D_TOP_MODULATE
;
1269 state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] = WINED3DTA_TEXTURE
;
1270 state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] = WINED3DTA_CURRENT
;
1271 state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
] = i
? WINED3D_TOP_DISABLE
: WINED3D_TOP_SELECT_ARG1
;
1272 state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] = WINED3DTA_TEXTURE
;
1273 state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] = WINED3DTA_CURRENT
;
1274 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT00
] = 0;
1275 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT01
] = 0;
1276 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT10
] = 0;
1277 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT11
] = 0;
1278 state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
] = i
;
1279 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LSCALE
] = 0;
1280 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LOFFSET
] = 0;
1281 state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
] = WINED3D_TTFF_DISABLE
;
1282 state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] = WINED3DTA_CURRENT
;
1283 state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] = WINED3DTA_CURRENT
;
1284 state
->texture_states
[i
][WINED3D_TSS_RESULT_ARG
] = WINED3DTA_CURRENT
;
1287 for (i
= 0 ; i
< MAX_COMBINED_SAMPLERS
; ++i
)
1289 TRACE("Setting up default samplers states for sampler %u.\n", i
);
1290 state
->sampler_states
[i
][WINED3D_SAMP_ADDRESS_U
] = WINED3D_TADDRESS_WRAP
;
1291 state
->sampler_states
[i
][WINED3D_SAMP_ADDRESS_V
] = WINED3D_TADDRESS_WRAP
;
1292 state
->sampler_states
[i
][WINED3D_SAMP_ADDRESS_W
] = WINED3D_TADDRESS_WRAP
;
1293 state
->sampler_states
[i
][WINED3D_SAMP_BORDER_COLOR
] = 0;
1294 state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] = WINED3D_TEXF_POINT
;
1295 state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] = WINED3D_TEXF_POINT
;
1296 state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] = WINED3D_TEXF_NONE
;
1297 state
->sampler_states
[i
][WINED3D_SAMP_MIPMAP_LOD_BIAS
] = 0;
1298 state
->sampler_states
[i
][WINED3D_SAMP_MAX_MIP_LEVEL
] = 0;
1299 state
->sampler_states
[i
][WINED3D_SAMP_MAX_ANISOTROPY
] = 1;
1300 state
->sampler_states
[i
][WINED3D_SAMP_SRGB_TEXTURE
] = 0;
1301 /* TODO: Indicates which element of a multielement texture to use. */
1302 state
->sampler_states
[i
][WINED3D_SAMP_ELEMENT_INDEX
] = 0;
1303 /* TODO: Vertex offset in the presampled displacement map. */
1304 state
->sampler_states
[i
][WINED3D_SAMP_DMAP_OFFSET
] = 0;
1308 HRESULT
state_init(struct wined3d_state
*state
, struct wined3d_fb_state
*fb
,
1309 const struct wined3d_gl_info
*gl_info
, const struct wined3d_d3d_info
*d3d_info
,
1314 state
->flags
= flags
;
1317 for (i
= 0; i
< LIGHTMAP_SIZE
; i
++)
1319 list_init(&state
->light_map
[i
]);
1322 if (!(state
->vs_consts_f
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1323 4 * sizeof(float) * d3d_info
->limits
.vs_uniform_count
)))
1324 return E_OUTOFMEMORY
;
1326 if (!(state
->ps_consts_f
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1327 4 * sizeof(float) * d3d_info
->limits
.ps_uniform_count
)))
1329 HeapFree(GetProcessHeap(), 0, state
->vs_consts_f
);
1330 return E_OUTOFMEMORY
;
1333 if (flags
& WINED3D_STATE_INIT_DEFAULT
)
1334 state_init_default(state
, gl_info
);
1339 static HRESULT
stateblock_init(struct wined3d_stateblock
*stateblock
,
1340 struct wined3d_device
*device
, enum wined3d_stateblock_type type
)
1343 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
1345 stateblock
->ref
= 1;
1346 stateblock
->device
= device
;
1348 if (FAILED(hr
= state_init(&stateblock
->state
, NULL
, &device
->adapter
->gl_info
, d3d_info
, 0)))
1351 if (FAILED(hr
= stateblock_allocate_shader_constants(stateblock
)))
1353 state_cleanup(&stateblock
->state
);
1357 if (type
== WINED3D_SBT_RECORDED
)
1360 TRACE("Updating changed flags appropriate for type %#x.\n", type
);
1364 case WINED3D_SBT_ALL
:
1365 stateblock_init_lights(stateblock
, device
->state
.light_map
);
1366 stateblock_savedstates_set_all(&stateblock
->changed
,
1367 d3d_info
->limits
.vs_uniform_count
, d3d_info
->limits
.ps_uniform_count
);
1370 case WINED3D_SBT_PIXEL_STATE
:
1371 stateblock_savedstates_set_pixel(&stateblock
->changed
,
1372 d3d_info
->limits
.ps_uniform_count
);
1375 case WINED3D_SBT_VERTEX_STATE
:
1376 stateblock_init_lights(stateblock
, device
->state
.light_map
);
1377 stateblock_savedstates_set_vertex(&stateblock
->changed
,
1378 d3d_info
->limits
.vs_uniform_count
);
1382 FIXME("Unrecognized state block type %#x.\n", type
);
1386 stateblock_init_contained_states(stateblock
);
1387 wined3d_stateblock_capture(stateblock
);
1392 HRESULT CDECL
wined3d_stateblock_create(struct wined3d_device
*device
,
1393 enum wined3d_stateblock_type type
, struct wined3d_stateblock
**stateblock
)
1395 struct wined3d_stateblock
*object
;
1398 TRACE("device %p, type %#x, stateblock %p.\n",
1399 device
, type
, stateblock
);
1401 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1403 return E_OUTOFMEMORY
;
1405 hr
= stateblock_init(object
, device
, type
);
1408 WARN("Failed to initialize stateblock, hr %#x.\n", hr
);
1409 HeapFree(GetProcessHeap(), 0, object
);
1413 TRACE("Created stateblock %p.\n", object
);
1414 *stateblock
= object
;