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
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
29 static const DWORD pixel_states_render
[] =
31 WINED3D_RS_ALPHABLENDENABLE
,
34 WINED3D_RS_ALPHATESTENABLE
,
35 WINED3D_RS_ANTIALIASEDLINEENABLE
,
36 WINED3D_RS_BLENDFACTOR
,
38 WINED3D_RS_BLENDOPALPHA
,
39 WINED3D_RS_CCW_STENCILFAIL
,
40 WINED3D_RS_CCW_STENCILPASS
,
41 WINED3D_RS_CCW_STENCILZFAIL
,
42 WINED3D_RS_COLORWRITEENABLE
,
43 WINED3D_RS_COLORWRITEENABLE1
,
44 WINED3D_RS_COLORWRITEENABLE2
,
45 WINED3D_RS_COLORWRITEENABLE3
,
48 WINED3D_RS_DESTBLENDALPHA
,
49 WINED3D_RS_DITHERENABLE
,
51 WINED3D_RS_FOGDENSITY
,
55 WINED3D_RS_SCISSORTESTENABLE
,
56 WINED3D_RS_SEPARATEALPHABLENDENABLE
,
58 WINED3D_RS_SLOPESCALEDEPTHBIAS
,
60 WINED3D_RS_SRCBLENDALPHA
,
61 WINED3D_RS_SRGBWRITEENABLE
,
62 WINED3D_RS_STENCILENABLE
,
63 WINED3D_RS_STENCILFAIL
,
64 WINED3D_RS_STENCILFUNC
,
65 WINED3D_RS_STENCILMASK
,
66 WINED3D_RS_STENCILPASS
,
67 WINED3D_RS_STENCILREF
,
68 WINED3D_RS_STENCILWRITEMASK
,
69 WINED3D_RS_STENCILZFAIL
,
70 WINED3D_RS_TEXTUREFACTOR
,
71 WINED3D_RS_TWOSIDEDSTENCILMODE
,
90 WINED3D_RS_ZWRITEENABLE
,
93 static const DWORD pixel_states_texture
[] =
95 WINED3D_TSS_ALPHA_ARG0
,
96 WINED3D_TSS_ALPHA_ARG1
,
97 WINED3D_TSS_ALPHA_ARG2
,
99 WINED3D_TSS_BUMPENV_LOFFSET
,
100 WINED3D_TSS_BUMPENV_LSCALE
,
101 WINED3D_TSS_BUMPENV_MAT00
,
102 WINED3D_TSS_BUMPENV_MAT01
,
103 WINED3D_TSS_BUMPENV_MAT10
,
104 WINED3D_TSS_BUMPENV_MAT11
,
105 WINED3D_TSS_COLOR_ARG0
,
106 WINED3D_TSS_COLOR_ARG1
,
107 WINED3D_TSS_COLOR_ARG2
,
108 WINED3D_TSS_COLOR_OP
,
109 WINED3D_TSS_RESULT_ARG
,
110 WINED3D_TSS_TEXCOORD_INDEX
,
111 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
,
114 static const DWORD pixel_states_sampler
[] =
116 WINED3D_SAMP_ADDRESS_U
,
117 WINED3D_SAMP_ADDRESS_V
,
118 WINED3D_SAMP_ADDRESS_W
,
119 WINED3D_SAMP_BORDER_COLOR
,
120 WINED3D_SAMP_MAG_FILTER
,
121 WINED3D_SAMP_MIN_FILTER
,
122 WINED3D_SAMP_MIP_FILTER
,
123 WINED3D_SAMP_MIPMAP_LOD_BIAS
,
124 WINED3D_SAMP_MAX_MIP_LEVEL
,
125 WINED3D_SAMP_MAX_ANISOTROPY
,
126 WINED3D_SAMP_SRGB_TEXTURE
,
127 WINED3D_SAMP_ELEMENT_INDEX
,
130 static const DWORD vertex_states_render
[] =
132 WINED3D_RS_ADAPTIVETESS_W
,
133 WINED3D_RS_ADAPTIVETESS_X
,
134 WINED3D_RS_ADAPTIVETESS_Y
,
135 WINED3D_RS_ADAPTIVETESS_Z
,
137 WINED3D_RS_AMBIENTMATERIALSOURCE
,
139 WINED3D_RS_CLIPPLANEENABLE
,
140 WINED3D_RS_COLORVERTEX
,
142 WINED3D_RS_DIFFUSEMATERIALSOURCE
,
143 WINED3D_RS_EMISSIVEMATERIALSOURCE
,
144 WINED3D_RS_ENABLEADAPTIVETESSELLATION
,
146 WINED3D_RS_FOGDENSITY
,
147 WINED3D_RS_FOGENABLE
,
150 WINED3D_RS_FOGTABLEMODE
,
151 WINED3D_RS_FOGVERTEXMODE
,
152 WINED3D_RS_INDEXEDVERTEXBLENDENABLE
,
154 WINED3D_RS_LOCALVIEWER
,
155 WINED3D_RS_MAXTESSELLATIONLEVEL
,
156 WINED3D_RS_MINTESSELLATIONLEVEL
,
157 WINED3D_RS_MULTISAMPLEANTIALIAS
,
158 WINED3D_RS_MULTISAMPLEMASK
,
159 WINED3D_RS_NORMALDEGREE
,
160 WINED3D_RS_NORMALIZENORMALS
,
161 WINED3D_RS_PATCHEDGESTYLE
,
162 WINED3D_RS_POINTSCALE_A
,
163 WINED3D_RS_POINTSCALE_B
,
164 WINED3D_RS_POINTSCALE_C
,
165 WINED3D_RS_POINTSCALEENABLE
,
166 WINED3D_RS_POINTSIZE
,
167 WINED3D_RS_POINTSIZE_MAX
,
168 WINED3D_RS_POINTSIZE_MIN
,
169 WINED3D_RS_POINTSPRITEENABLE
,
170 WINED3D_RS_POSITIONDEGREE
,
171 WINED3D_RS_RANGEFOGENABLE
,
172 WINED3D_RS_SHADEMODE
,
173 WINED3D_RS_SPECULARENABLE
,
174 WINED3D_RS_SPECULARMATERIALSOURCE
,
175 WINED3D_RS_TWEENFACTOR
,
176 WINED3D_RS_VERTEXBLEND
,
179 static const DWORD vertex_states_texture
[] =
181 WINED3D_TSS_TEXCOORD_INDEX
,
182 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
,
185 static const DWORD vertex_states_sampler
[] =
187 WINED3D_SAMP_DMAP_OFFSET
,
190 /* Allocates the correct amount of space for pixel and vertex shader constants,
191 * along with their set/changed flags on the given stateblock object
193 static HRESULT
stateblock_allocate_shader_constants(struct wined3d_stateblock
*object
)
195 const struct wined3d_d3d_info
*d3d_info
= &object
->device
->adapter
->d3d_info
;
197 object
->changed
.pixelShaderConstantsF
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
198 sizeof(BOOL
) * d3d_info
->limits
.ps_uniform_count
);
199 if (!object
->changed
.pixelShaderConstantsF
) goto fail
;
201 object
->changed
.vertexShaderConstantsF
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
202 sizeof(BOOL
) * d3d_info
->limits
.vs_uniform_count
);
203 if (!object
->changed
.vertexShaderConstantsF
) goto fail
;
205 object
->contained_vs_consts_f
= HeapAlloc(GetProcessHeap(), 0,
206 sizeof(DWORD
) * d3d_info
->limits
.vs_uniform_count
);
207 if (!object
->contained_vs_consts_f
) goto fail
;
209 object
->contained_ps_consts_f
= HeapAlloc(GetProcessHeap(), 0,
210 sizeof(DWORD
) * d3d_info
->limits
.ps_uniform_count
);
211 if (!object
->contained_ps_consts_f
) goto fail
;
216 ERR("Failed to allocate memory\n");
217 HeapFree(GetProcessHeap(), 0, object
->changed
.pixelShaderConstantsF
);
218 HeapFree(GetProcessHeap(), 0, object
->changed
.vertexShaderConstantsF
);
219 HeapFree(GetProcessHeap(), 0, object
->contained_vs_consts_f
);
220 HeapFree(GetProcessHeap(), 0, object
->contained_ps_consts_f
);
221 return E_OUTOFMEMORY
;
224 static inline void stateblock_set_bits(DWORD
*map
, UINT map_size
)
226 DWORD mask
= (1 << (map_size
& 0x1f)) - 1;
227 memset(map
, 0xff, (map_size
>> 5) * sizeof(*map
));
228 if (mask
) map
[map_size
>> 5] = mask
;
231 /* Set all members of a stateblock savedstate to the given value */
232 static void stateblock_savedstates_set_all(struct wined3d_saved_states
*states
, DWORD vs_consts
, DWORD ps_consts
)
237 states
->primitive_type
= 1;
239 states
->material
= 1;
240 states
->viewport
= 1;
241 states
->vertexDecl
= 1;
242 states
->pixelShader
= 1;
243 states
->vertexShader
= 1;
244 states
->scissorRect
= 1;
246 /* Fixed size arrays */
247 states
->streamSource
= 0xffff;
248 states
->streamFreq
= 0xffff;
249 states
->textures
= 0xfffff;
250 stateblock_set_bits(states
->transform
, HIGHEST_TRANSFORMSTATE
+ 1);
251 stateblock_set_bits(states
->renderState
, WINEHIGHEST_RENDER_STATE
+ 1);
252 for (i
= 0; i
< MAX_TEXTURES
; ++i
) states
->textureState
[i
] = 0x3ffff;
253 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
) states
->samplerState
[i
] = 0x3ffe;
254 states
->clipplane
= 0xffffffff;
255 states
->pixelShaderConstantsB
= 0xffff;
256 states
->pixelShaderConstantsI
= 0xffff;
257 states
->vertexShaderConstantsB
= 0xffff;
258 states
->vertexShaderConstantsI
= 0xffff;
260 /* Dynamically sized arrays */
261 memset(states
->pixelShaderConstantsF
, TRUE
, sizeof(BOOL
) * ps_consts
);
262 memset(states
->vertexShaderConstantsF
, TRUE
, sizeof(BOOL
) * vs_consts
);
265 static void stateblock_savedstates_set_pixel(struct wined3d_saved_states
*states
, const DWORD num_constants
)
267 DWORD texture_mask
= 0;
268 WORD sampler_mask
= 0;
271 states
->pixelShader
= 1;
273 for (i
= 0; i
< sizeof(pixel_states_render
) / sizeof(*pixel_states_render
); ++i
)
275 DWORD rs
= pixel_states_render
[i
];
276 states
->renderState
[rs
>> 5] |= 1 << (rs
& 0x1f);
279 for (i
= 0; i
< sizeof(pixel_states_texture
) / sizeof(*pixel_states_texture
); ++i
)
280 texture_mask
|= 1 << pixel_states_texture
[i
];
281 for (i
= 0; i
< MAX_TEXTURES
; ++i
) states
->textureState
[i
] = texture_mask
;
282 for (i
= 0; i
< sizeof(pixel_states_sampler
) / sizeof(*pixel_states_sampler
); ++i
)
283 sampler_mask
|= 1 << pixel_states_sampler
[i
];
284 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
) states
->samplerState
[i
] = sampler_mask
;
285 states
->pixelShaderConstantsB
= 0xffff;
286 states
->pixelShaderConstantsI
= 0xffff;
288 memset(states
->pixelShaderConstantsF
, TRUE
, sizeof(BOOL
) * num_constants
);
291 static void stateblock_savedstates_set_vertex(struct wined3d_saved_states
*states
, const DWORD num_constants
)
293 DWORD texture_mask
= 0;
294 WORD sampler_mask
= 0;
297 states
->vertexDecl
= 1;
298 states
->vertexShader
= 1;
300 for (i
= 0; i
< sizeof(vertex_states_render
) / sizeof(*vertex_states_render
); ++i
)
302 DWORD rs
= vertex_states_render
[i
];
303 states
->renderState
[rs
>> 5] |= 1 << (rs
& 0x1f);
306 for (i
= 0; i
< sizeof(vertex_states_texture
) / sizeof(*vertex_states_texture
); ++i
)
307 texture_mask
|= 1 << vertex_states_texture
[i
];
308 for (i
= 0; i
< MAX_TEXTURES
; ++i
) states
->textureState
[i
] = texture_mask
;
309 for (i
= 0; i
< sizeof(vertex_states_sampler
) / sizeof(*vertex_states_sampler
); ++i
)
310 sampler_mask
|= 1 << vertex_states_sampler
[i
];
311 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
) states
->samplerState
[i
] = sampler_mask
;
312 states
->vertexShaderConstantsB
= 0xffff;
313 states
->vertexShaderConstantsI
= 0xffff;
315 memset(states
->vertexShaderConstantsF
, TRUE
, sizeof(BOOL
) * num_constants
);
318 void stateblock_init_contained_states(struct wined3d_stateblock
*stateblock
)
320 const struct wined3d_d3d_info
*d3d_info
= &stateblock
->device
->adapter
->d3d_info
;
323 for (i
= 0; i
<= WINEHIGHEST_RENDER_STATE
>> 5; ++i
)
325 DWORD map
= stateblock
->changed
.renderState
[i
];
326 for (j
= 0; map
; map
>>= 1, ++j
)
328 if (!(map
& 1)) continue;
330 stateblock
->contained_render_states
[stateblock
->num_contained_render_states
] = (i
<< 5) | j
;
331 ++stateblock
->num_contained_render_states
;
335 for (i
= 0; i
<= HIGHEST_TRANSFORMSTATE
>> 5; ++i
)
337 DWORD map
= stateblock
->changed
.transform
[i
];
338 for (j
= 0; map
; map
>>= 1, ++j
)
340 if (!(map
& 1)) continue;
342 stateblock
->contained_transform_states
[stateblock
->num_contained_transform_states
] = (i
<< 5) | j
;
343 ++stateblock
->num_contained_transform_states
;
347 for (i
= 0; i
< d3d_info
->limits
.vs_uniform_count
; ++i
)
349 if (stateblock
->changed
.vertexShaderConstantsF
[i
])
351 stateblock
->contained_vs_consts_f
[stateblock
->num_contained_vs_consts_f
] = i
;
352 ++stateblock
->num_contained_vs_consts_f
;
356 for (i
= 0; i
< MAX_CONST_I
; ++i
)
358 if (stateblock
->changed
.vertexShaderConstantsI
& (1 << i
))
360 stateblock
->contained_vs_consts_i
[stateblock
->num_contained_vs_consts_i
] = i
;
361 ++stateblock
->num_contained_vs_consts_i
;
365 for (i
= 0; i
< MAX_CONST_B
; ++i
)
367 if (stateblock
->changed
.vertexShaderConstantsB
& (1 << i
))
369 stateblock
->contained_vs_consts_b
[stateblock
->num_contained_vs_consts_b
] = i
;
370 ++stateblock
->num_contained_vs_consts_b
;
374 for (i
= 0; i
< d3d_info
->limits
.ps_uniform_count
; ++i
)
376 if (stateblock
->changed
.pixelShaderConstantsF
[i
])
378 stateblock
->contained_ps_consts_f
[stateblock
->num_contained_ps_consts_f
] = i
;
379 ++stateblock
->num_contained_ps_consts_f
;
383 for (i
= 0; i
< MAX_CONST_I
; ++i
)
385 if (stateblock
->changed
.pixelShaderConstantsI
& (1 << i
))
387 stateblock
->contained_ps_consts_i
[stateblock
->num_contained_ps_consts_i
] = i
;
388 ++stateblock
->num_contained_ps_consts_i
;
392 for (i
= 0; i
< MAX_CONST_B
; ++i
)
394 if (stateblock
->changed
.pixelShaderConstantsB
& (1 << i
))
396 stateblock
->contained_ps_consts_b
[stateblock
->num_contained_ps_consts_b
] = i
;
397 ++stateblock
->num_contained_ps_consts_b
;
401 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
403 DWORD map
= stateblock
->changed
.textureState
[i
];
405 for(j
= 0; map
; map
>>= 1, ++j
)
407 if (!(map
& 1)) continue;
409 stateblock
->contained_tss_states
[stateblock
->num_contained_tss_states
].stage
= i
;
410 stateblock
->contained_tss_states
[stateblock
->num_contained_tss_states
].state
= j
;
411 ++stateblock
->num_contained_tss_states
;
415 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
417 DWORD map
= stateblock
->changed
.samplerState
[i
];
419 for (j
= 0; map
; map
>>= 1, ++j
)
421 if (!(map
& 1)) continue;
423 stateblock
->contained_sampler_states
[stateblock
->num_contained_sampler_states
].stage
= i
;
424 stateblock
->contained_sampler_states
[stateblock
->num_contained_sampler_states
].state
= j
;
425 ++stateblock
->num_contained_sampler_states
;
430 static void stateblock_init_lights(struct wined3d_stateblock
*stateblock
, struct list
*light_map
)
434 for (i
= 0; i
< LIGHTMAP_SIZE
; ++i
)
436 const struct wined3d_light_info
*src_light
;
438 LIST_FOR_EACH_ENTRY(src_light
, &light_map
[i
], struct wined3d_light_info
, entry
)
440 struct wined3d_light_info
*dst_light
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_light
));
442 *dst_light
= *src_light
;
443 list_add_tail(&stateblock
->state
.light_map
[i
], &dst_light
->entry
);
448 ULONG CDECL
wined3d_stateblock_incref(struct wined3d_stateblock
*stateblock
)
450 ULONG refcount
= InterlockedIncrement(&stateblock
->ref
);
452 TRACE("%p increasing refcount to %u.\n", stateblock
, refcount
);
457 void state_unbind_resources(struct wined3d_state
*state
)
459 struct wined3d_vertex_declaration
*decl
;
460 struct wined3d_sampler
*sampler
;
461 struct wined3d_texture
*texture
;
462 struct wined3d_buffer
*buffer
;
463 struct wined3d_shader
*shader
;
466 if ((decl
= state
->vertex_declaration
))
468 state
->vertex_declaration
= NULL
;
469 wined3d_vertex_declaration_decref(decl
);
472 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
474 if ((texture
= state
->textures
[i
]))
476 state
->textures
[i
] = NULL
;
477 wined3d_texture_decref(texture
);
481 for (i
= 0; i
< MAX_STREAM_OUT
; ++i
)
483 if ((buffer
= state
->stream_output
[i
].buffer
))
485 state
->stream_output
[i
].buffer
= NULL
;
486 wined3d_buffer_decref(buffer
);
490 for (i
= 0; i
< MAX_STREAMS
; ++i
)
492 if ((buffer
= state
->streams
[i
].buffer
))
494 state
->streams
[i
].buffer
= NULL
;
495 wined3d_buffer_decref(buffer
);
499 if ((buffer
= state
->index_buffer
))
501 state
->index_buffer
= NULL
;
502 wined3d_buffer_decref(buffer
);
505 if ((shader
= state
->vertex_shader
))
507 state
->vertex_shader
= NULL
;
508 wined3d_shader_decref(shader
);
511 for (i
= 0; i
< MAX_CONSTANT_BUFFERS
; ++i
)
513 if ((buffer
= state
->vs_cb
[i
]))
515 state
->vs_cb
[i
] = NULL
;
516 wined3d_buffer_decref(buffer
);
520 for (i
= 0; i
< MAX_SAMPLER_OBJECTS
; ++i
)
522 if ((sampler
= state
->vs_sampler
[i
]))
524 state
->vs_sampler
[i
] = NULL
;
525 wined3d_sampler_decref(sampler
);
529 if ((shader
= state
->geometry_shader
))
531 state
->geometry_shader
= NULL
;
532 wined3d_shader_decref(shader
);
535 for (i
= 0; i
< MAX_CONSTANT_BUFFERS
; ++i
)
537 if ((buffer
= state
->gs_cb
[i
]))
539 state
->gs_cb
[i
] = NULL
;
540 wined3d_buffer_decref(buffer
);
544 for (i
= 0; i
< MAX_SAMPLER_OBJECTS
; ++i
)
546 if ((sampler
= state
->gs_sampler
[i
]))
548 state
->gs_sampler
[i
] = NULL
;
549 wined3d_sampler_decref(sampler
);
553 if ((shader
= state
->pixel_shader
))
555 state
->pixel_shader
= NULL
;
556 wined3d_shader_decref(shader
);
559 for (i
= 0; i
< MAX_SAMPLER_OBJECTS
; ++i
)
561 if ((sampler
= state
->ps_sampler
[i
]))
563 state
->ps_sampler
[i
] = NULL
;
564 wined3d_sampler_decref(sampler
);
568 for (i
= 0; i
< MAX_CONSTANT_BUFFERS
; ++i
)
570 if ((buffer
= state
->ps_cb
[i
]))
572 state
->ps_cb
[i
] = NULL
;
573 wined3d_buffer_decref(buffer
);
578 void state_cleanup(struct wined3d_state
*state
)
580 unsigned int counter
;
582 state_unbind_resources(state
);
584 for (counter
= 0; counter
< LIGHTMAP_SIZE
; ++counter
)
586 struct list
*e1
, *e2
;
587 LIST_FOR_EACH_SAFE(e1
, e2
, &state
->light_map
[counter
])
589 struct wined3d_light_info
*light
= LIST_ENTRY(e1
, struct wined3d_light_info
, entry
);
590 list_remove(&light
->entry
);
591 HeapFree(GetProcessHeap(), 0, light
);
595 HeapFree(GetProcessHeap(), 0, state
->vs_consts_f
);
596 HeapFree(GetProcessHeap(), 0, state
->ps_consts_f
);
599 HRESULT
state_init(struct wined3d_state
*state
, const struct wined3d_d3d_info
*d3d_info
)
603 for (i
= 0; i
< LIGHTMAP_SIZE
; i
++)
605 list_init(&state
->light_map
[i
]);
608 if (!(state
->vs_consts_f
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
609 4 * sizeof(float) * d3d_info
->limits
.vs_uniform_count
)))
610 return E_OUTOFMEMORY
;
612 if (!(state
->ps_consts_f
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
613 4 * sizeof(float) * d3d_info
->limits
.ps_uniform_count
)))
615 HeapFree(GetProcessHeap(), 0, state
->vs_consts_f
);
616 return E_OUTOFMEMORY
;
622 ULONG CDECL
wined3d_stateblock_decref(struct wined3d_stateblock
*stateblock
)
624 ULONG refcount
= InterlockedDecrement(&stateblock
->ref
);
626 TRACE("%p decreasing refcount to %u\n", stateblock
, refcount
);
630 state_cleanup(&stateblock
->state
);
632 HeapFree(GetProcessHeap(), 0, stateblock
->changed
.vertexShaderConstantsF
);
633 HeapFree(GetProcessHeap(), 0, stateblock
->changed
.pixelShaderConstantsF
);
634 HeapFree(GetProcessHeap(), 0, stateblock
->contained_vs_consts_f
);
635 HeapFree(GetProcessHeap(), 0, stateblock
->contained_ps_consts_f
);
636 HeapFree(GetProcessHeap(), 0, stateblock
);
642 static void wined3d_state_record_lights(struct wined3d_state
*dst_state
, const struct wined3d_state
*src_state
)
646 /* Lights... For a recorded state block, we just had a chain of actions
647 * to perform, so we need to walk that chain and update any actions which
649 for (i
= 0; i
< LIGHTMAP_SIZE
; ++i
)
652 LIST_FOR_EACH(e
, &dst_state
->light_map
[i
])
654 BOOL updated
= FALSE
;
655 struct wined3d_light_info
*src
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
), *realLight
;
657 /* Look up the light in the destination */
658 LIST_FOR_EACH(f
, &src_state
->light_map
[i
])
660 realLight
= LIST_ENTRY(f
, struct wined3d_light_info
, entry
);
661 if (realLight
->OriginalIndex
== src
->OriginalIndex
)
663 src
->OriginalParms
= realLight
->OriginalParms
;
665 if (realLight
->glIndex
== -1 && src
->glIndex
!= -1)
668 dst_state
->lights
[src
->glIndex
] = NULL
;
670 else if (realLight
->glIndex
!= -1 && src
->glIndex
== -1)
673 dst_state
->lights
[realLight
->glIndex
] = src
;
675 src
->glIndex
= realLight
->glIndex
;
683 /* This can happen if the light was originally created as a
684 * default light for SetLightEnable() while recording. */
685 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
686 src
->OriginalIndex
, dst_state
, src_state
);
688 src
->OriginalParms
= WINED3D_default_light
;
689 if (src
->glIndex
!= -1)
691 dst_state
->lights
[src
->glIndex
] = NULL
;
699 void CDECL
wined3d_stateblock_capture(struct wined3d_stateblock
*stateblock
)
701 const struct wined3d_state
*src_state
= &stateblock
->device
->state
;
705 TRACE("stateblock %p.\n", stateblock
);
707 TRACE("Capturing state %p.\n", src_state
);
709 if (stateblock
->changed
.vertexShader
&& stateblock
->state
.vertex_shader
!= src_state
->vertex_shader
)
711 TRACE("Updating vertex shader from %p to %p\n",
712 stateblock
->state
.vertex_shader
, src_state
->vertex_shader
);
714 if (src_state
->vertex_shader
)
715 wined3d_shader_incref(src_state
->vertex_shader
);
716 if (stateblock
->state
.vertex_shader
)
717 wined3d_shader_decref(stateblock
->state
.vertex_shader
);
718 stateblock
->state
.vertex_shader
= src_state
->vertex_shader
;
721 /* Vertex shader float constants. */
722 for (i
= 0; i
< stateblock
->num_contained_vs_consts_f
; ++i
)
724 unsigned int idx
= stateblock
->contained_vs_consts_f
[i
];
726 TRACE("Setting vs_consts_f[%u] to {%.8e, %.8e, %.8e, %.8e}.\n", idx
,
727 src_state
->vs_consts_f
[idx
* 4 + 0],
728 src_state
->vs_consts_f
[idx
* 4 + 1],
729 src_state
->vs_consts_f
[idx
* 4 + 2],
730 src_state
->vs_consts_f
[idx
* 4 + 3]);
732 stateblock
->state
.vs_consts_f
[idx
* 4 + 0] = src_state
->vs_consts_f
[idx
* 4 + 0];
733 stateblock
->state
.vs_consts_f
[idx
* 4 + 1] = src_state
->vs_consts_f
[idx
* 4 + 1];
734 stateblock
->state
.vs_consts_f
[idx
* 4 + 2] = src_state
->vs_consts_f
[idx
* 4 + 2];
735 stateblock
->state
.vs_consts_f
[idx
* 4 + 3] = src_state
->vs_consts_f
[idx
* 4 + 3];
738 /* Vertex shader integer constants. */
739 for (i
= 0; i
< stateblock
->num_contained_vs_consts_i
; ++i
)
741 unsigned int idx
= stateblock
->contained_vs_consts_i
[i
];
743 TRACE("Setting vs_consts[%u] to {%d, %d, %d, %d}.\n", idx
,
744 src_state
->vs_consts_i
[idx
* 4 + 0],
745 src_state
->vs_consts_i
[idx
* 4 + 1],
746 src_state
->vs_consts_i
[idx
* 4 + 2],
747 src_state
->vs_consts_i
[idx
* 4 + 3]);
749 stateblock
->state
.vs_consts_i
[idx
* 4 + 0] = src_state
->vs_consts_i
[idx
* 4 + 0];
750 stateblock
->state
.vs_consts_i
[idx
* 4 + 1] = src_state
->vs_consts_i
[idx
* 4 + 1];
751 stateblock
->state
.vs_consts_i
[idx
* 4 + 2] = src_state
->vs_consts_i
[idx
* 4 + 2];
752 stateblock
->state
.vs_consts_i
[idx
* 4 + 3] = src_state
->vs_consts_i
[idx
* 4 + 3];
755 /* Vertex shader boolean constants. */
756 for (i
= 0; i
< stateblock
->num_contained_vs_consts_b
; ++i
)
758 unsigned int idx
= stateblock
->contained_vs_consts_b
[i
];
760 TRACE("Setting vs_consts_b[%u] to %s.\n",
761 idx
, src_state
->vs_consts_b
[idx
] ? "TRUE" : "FALSE");
763 stateblock
->state
.vs_consts_b
[idx
] = src_state
->vs_consts_b
[idx
];
766 /* Pixel shader float constants. */
767 for (i
= 0; i
< stateblock
->num_contained_ps_consts_f
; ++i
)
769 unsigned int idx
= stateblock
->contained_ps_consts_f
[i
];
771 TRACE("Setting ps_consts_f[%u] to {%.8e, %.8e, %.8e, %.8e}.\n", idx
,
772 src_state
->ps_consts_f
[idx
* 4 + 0],
773 src_state
->ps_consts_f
[idx
* 4 + 1],
774 src_state
->ps_consts_f
[idx
* 4 + 2],
775 src_state
->ps_consts_f
[idx
* 4 + 3]);
777 stateblock
->state
.ps_consts_f
[idx
* 4 + 0] = src_state
->ps_consts_f
[idx
* 4 + 0];
778 stateblock
->state
.ps_consts_f
[idx
* 4 + 1] = src_state
->ps_consts_f
[idx
* 4 + 1];
779 stateblock
->state
.ps_consts_f
[idx
* 4 + 2] = src_state
->ps_consts_f
[idx
* 4 + 2];
780 stateblock
->state
.ps_consts_f
[idx
* 4 + 3] = src_state
->ps_consts_f
[idx
* 4 + 3];
783 /* Pixel shader integer constants. */
784 for (i
= 0; i
< stateblock
->num_contained_ps_consts_i
; ++i
)
786 unsigned int idx
= stateblock
->contained_ps_consts_i
[i
];
787 TRACE("Setting ps_consts_i[%u] to {%d, %d, %d, %d}.\n", idx
,
788 src_state
->ps_consts_i
[idx
* 4 + 0],
789 src_state
->ps_consts_i
[idx
* 4 + 1],
790 src_state
->ps_consts_i
[idx
* 4 + 2],
791 src_state
->ps_consts_i
[idx
* 4 + 3]);
793 stateblock
->state
.ps_consts_i
[idx
* 4 + 0] = src_state
->ps_consts_i
[idx
* 4 + 0];
794 stateblock
->state
.ps_consts_i
[idx
* 4 + 1] = src_state
->ps_consts_i
[idx
* 4 + 1];
795 stateblock
->state
.ps_consts_i
[idx
* 4 + 2] = src_state
->ps_consts_i
[idx
* 4 + 2];
796 stateblock
->state
.ps_consts_i
[idx
* 4 + 3] = src_state
->ps_consts_i
[idx
* 4 + 3];
799 /* Pixel shader boolean constants. */
800 for (i
= 0; i
< stateblock
->num_contained_ps_consts_b
; ++i
)
802 unsigned int idx
= stateblock
->contained_ps_consts_b
[i
];
803 TRACE("Setting ps_consts_b[%u] to %s.\n",
804 idx
, src_state
->ps_consts_b
[idx
] ? "TRUE" : "FALSE");
806 stateblock
->state
.ps_consts_b
[idx
] = src_state
->ps_consts_b
[idx
];
809 /* Others + Render & Texture */
810 for (i
= 0; i
< stateblock
->num_contained_transform_states
; ++i
)
812 enum wined3d_transform_state transform
= stateblock
->contained_transform_states
[i
];
814 TRACE("Updating transform %#x.\n", transform
);
816 stateblock
->state
.transforms
[transform
] = src_state
->transforms
[transform
];
819 if (stateblock
->changed
.primitive_type
)
820 stateblock
->state
.gl_primitive_type
= src_state
->gl_primitive_type
;
822 if (stateblock
->changed
.indices
823 && ((stateblock
->state
.index_buffer
!= src_state
->index_buffer
)
824 || (stateblock
->state
.base_vertex_index
!= src_state
->base_vertex_index
)
825 || (stateblock
->state
.index_format
!= src_state
->index_format
)))
827 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
828 src_state
->index_buffer
, src_state
->base_vertex_index
);
830 if (src_state
->index_buffer
)
831 wined3d_buffer_incref(src_state
->index_buffer
);
832 if (stateblock
->state
.index_buffer
)
833 wined3d_buffer_decref(stateblock
->state
.index_buffer
);
834 stateblock
->state
.index_buffer
= src_state
->index_buffer
;
835 stateblock
->state
.base_vertex_index
= src_state
->base_vertex_index
;
836 stateblock
->state
.index_format
= src_state
->index_format
;
839 if (stateblock
->changed
.vertexDecl
&& stateblock
->state
.vertex_declaration
!= src_state
->vertex_declaration
)
841 TRACE("Updating vertex declaration from %p to %p.\n",
842 stateblock
->state
.vertex_declaration
, src_state
->vertex_declaration
);
844 if (src_state
->vertex_declaration
)
845 wined3d_vertex_declaration_incref(src_state
->vertex_declaration
);
846 if (stateblock
->state
.vertex_declaration
)
847 wined3d_vertex_declaration_decref(stateblock
->state
.vertex_declaration
);
848 stateblock
->state
.vertex_declaration
= src_state
->vertex_declaration
;
851 if (stateblock
->changed
.material
852 && memcmp(&src_state
->material
, &stateblock
->state
.material
, sizeof(stateblock
->state
.material
)))
854 TRACE("Updating material.\n");
856 stateblock
->state
.material
= src_state
->material
;
859 if (stateblock
->changed
.viewport
860 && memcmp(&src_state
->viewport
, &stateblock
->state
.viewport
, sizeof(stateblock
->state
.viewport
)))
862 TRACE("Updating viewport.\n");
864 stateblock
->state
.viewport
= src_state
->viewport
;
867 if (stateblock
->changed
.scissorRect
&& memcmp(&src_state
->scissor_rect
,
868 &stateblock
->state
.scissor_rect
, sizeof(stateblock
->state
.scissor_rect
)))
870 TRACE("Updating scissor rect.\n");
872 stateblock
->state
.scissor_rect
= src_state
->scissor_rect
;
875 map
= stateblock
->changed
.streamSource
;
876 for (i
= 0; map
; map
>>= 1, ++i
)
878 if (!(map
& 1)) continue;
880 if (stateblock
->state
.streams
[i
].stride
!= src_state
->streams
[i
].stride
881 || stateblock
->state
.streams
[i
].buffer
!= src_state
->streams
[i
].buffer
)
883 TRACE("Updating stream source %u to %p, stride to %u.\n",
884 i
, src_state
->streams
[i
].buffer
,
885 src_state
->streams
[i
].stride
);
887 stateblock
->state
.streams
[i
].stride
= src_state
->streams
[i
].stride
;
888 if (src_state
->streams
[i
].buffer
)
889 wined3d_buffer_incref(src_state
->streams
[i
].buffer
);
890 if (stateblock
->state
.streams
[i
].buffer
)
891 wined3d_buffer_decref(stateblock
->state
.streams
[i
].buffer
);
892 stateblock
->state
.streams
[i
].buffer
= src_state
->streams
[i
].buffer
;
896 map
= stateblock
->changed
.streamFreq
;
897 for (i
= 0; map
; map
>>= 1, ++i
)
899 if (!(map
& 1)) continue;
901 if (stateblock
->state
.streams
[i
].frequency
!= src_state
->streams
[i
].frequency
902 || stateblock
->state
.streams
[i
].flags
!= src_state
->streams
[i
].flags
)
904 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
905 i
, src_state
->streams
[i
].frequency
, src_state
->streams
[i
].flags
);
907 stateblock
->state
.streams
[i
].frequency
= src_state
->streams
[i
].frequency
;
908 stateblock
->state
.streams
[i
].flags
= src_state
->streams
[i
].flags
;
912 map
= stateblock
->changed
.clipplane
;
913 for (i
= 0; map
; map
>>= 1, ++i
)
915 if (!(map
& 1)) continue;
917 if (memcmp(&stateblock
->state
.clip_planes
[i
], &src_state
->clip_planes
[i
], sizeof(src_state
->clip_planes
[i
])))
919 TRACE("Updating clipplane %u.\n", i
);
920 stateblock
->state
.clip_planes
[i
] = src_state
->clip_planes
[i
];
925 for (i
= 0; i
< stateblock
->num_contained_render_states
; ++i
)
927 enum wined3d_render_state rs
= stateblock
->contained_render_states
[i
];
929 TRACE("Updating render state %#x to %u.\n", rs
, src_state
->render_states
[rs
]);
931 stateblock
->state
.render_states
[rs
] = src_state
->render_states
[rs
];
935 for (i
= 0; i
< stateblock
->num_contained_tss_states
; ++i
)
937 DWORD stage
= stateblock
->contained_tss_states
[i
].stage
;
938 DWORD state
= stateblock
->contained_tss_states
[i
].state
;
940 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage
, state
,
941 src_state
->texture_states
[stage
][state
], stateblock
->state
.texture_states
[stage
][state
]);
943 stateblock
->state
.texture_states
[stage
][state
] = src_state
->texture_states
[stage
][state
];
947 map
= stateblock
->changed
.textures
;
948 for (i
= 0; map
; map
>>= 1, ++i
)
950 if (!(map
& 1)) continue;
952 TRACE("Updating texture %u to %p (was %p).\n",
953 i
, src_state
->textures
[i
], stateblock
->state
.textures
[i
]);
955 if (src_state
->textures
[i
])
956 wined3d_texture_incref(src_state
->textures
[i
]);
957 if (stateblock
->state
.textures
[i
])
958 wined3d_texture_decref(stateblock
->state
.textures
[i
]);
959 stateblock
->state
.textures
[i
] = src_state
->textures
[i
];
962 for (i
= 0; i
< stateblock
->num_contained_sampler_states
; ++i
)
964 DWORD stage
= stateblock
->contained_sampler_states
[i
].stage
;
965 DWORD state
= stateblock
->contained_sampler_states
[i
].state
;
967 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage
, state
,
968 src_state
->sampler_states
[stage
][state
], stateblock
->state
.sampler_states
[stage
][state
]);
970 stateblock
->state
.sampler_states
[stage
][state
] = src_state
->sampler_states
[stage
][state
];
973 if (stateblock
->changed
.pixelShader
&& stateblock
->state
.pixel_shader
!= src_state
->pixel_shader
)
975 if (src_state
->pixel_shader
)
976 wined3d_shader_incref(src_state
->pixel_shader
);
977 if (stateblock
->state
.pixel_shader
)
978 wined3d_shader_decref(stateblock
->state
.pixel_shader
);
979 stateblock
->state
.pixel_shader
= src_state
->pixel_shader
;
982 wined3d_state_record_lights(&stateblock
->state
, src_state
);
984 TRACE("Capture done.\n");
987 static void apply_lights(struct wined3d_device
*device
, const struct wined3d_state
*state
)
991 for (i
= 0; i
< LIGHTMAP_SIZE
; ++i
)
995 LIST_FOR_EACH(e
, &state
->light_map
[i
])
997 const struct wined3d_light_info
*light
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
999 wined3d_device_set_light(device
, light
->OriginalIndex
, &light
->OriginalParms
);
1000 wined3d_device_set_light_enable(device
, light
->OriginalIndex
, light
->glIndex
!= -1);
1005 void CDECL
wined3d_stateblock_apply(const struct wined3d_stateblock
*stateblock
)
1007 struct wined3d_device
*device
= stateblock
->device
;
1011 TRACE("Applying stateblock %p to device %p.\n", stateblock
, device
);
1013 if (stateblock
->changed
.vertexShader
)
1014 wined3d_device_set_vertex_shader(device
, stateblock
->state
.vertex_shader
);
1016 /* Vertex Shader Constants. */
1017 for (i
= 0; i
< stateblock
->num_contained_vs_consts_f
; ++i
)
1019 wined3d_device_set_vs_consts_f(device
, stateblock
->contained_vs_consts_f
[i
],
1020 stateblock
->state
.vs_consts_f
+ stateblock
->contained_vs_consts_f
[i
] * 4, 1);
1022 for (i
= 0; i
< stateblock
->num_contained_vs_consts_i
; ++i
)
1024 wined3d_device_set_vs_consts_i(device
, stateblock
->contained_vs_consts_i
[i
],
1025 stateblock
->state
.vs_consts_i
+ stateblock
->contained_vs_consts_i
[i
] * 4, 1);
1027 for (i
= 0; i
< stateblock
->num_contained_vs_consts_b
; ++i
)
1029 wined3d_device_set_vs_consts_b(device
, stateblock
->contained_vs_consts_b
[i
],
1030 stateblock
->state
.vs_consts_b
+ stateblock
->contained_vs_consts_b
[i
], 1);
1033 apply_lights(device
, &stateblock
->state
);
1035 if (stateblock
->changed
.pixelShader
)
1036 wined3d_device_set_pixel_shader(device
, stateblock
->state
.pixel_shader
);
1038 /* Pixel Shader Constants. */
1039 for (i
= 0; i
< stateblock
->num_contained_ps_consts_f
; ++i
)
1041 wined3d_device_set_ps_consts_f(device
, stateblock
->contained_ps_consts_f
[i
],
1042 stateblock
->state
.ps_consts_f
+ stateblock
->contained_ps_consts_f
[i
] * 4, 1);
1044 for (i
= 0; i
< stateblock
->num_contained_ps_consts_i
; ++i
)
1046 wined3d_device_set_ps_consts_i(device
, stateblock
->contained_ps_consts_i
[i
],
1047 stateblock
->state
.ps_consts_i
+ stateblock
->contained_ps_consts_i
[i
] * 4, 1);
1049 for (i
= 0; i
< stateblock
->num_contained_ps_consts_b
; ++i
)
1051 wined3d_device_set_ps_consts_b(device
, stateblock
->contained_ps_consts_b
[i
],
1052 stateblock
->state
.ps_consts_b
+ stateblock
->contained_ps_consts_b
[i
], 1);
1055 /* Render states. */
1056 for (i
= 0; i
< stateblock
->num_contained_render_states
; ++i
)
1058 wined3d_device_set_render_state(device
, stateblock
->contained_render_states
[i
],
1059 stateblock
->state
.render_states
[stateblock
->contained_render_states
[i
]]);
1062 /* Texture states. */
1063 for (i
= 0; i
< stateblock
->num_contained_tss_states
; ++i
)
1065 DWORD stage
= stateblock
->contained_tss_states
[i
].stage
;
1066 DWORD state
= stateblock
->contained_tss_states
[i
].state
;
1068 wined3d_device_set_texture_stage_state(device
, stage
, state
, stateblock
->state
.texture_states
[stage
][state
]);
1071 /* Sampler states. */
1072 for (i
= 0; i
< stateblock
->num_contained_sampler_states
; ++i
)
1074 DWORD stage
= stateblock
->contained_sampler_states
[i
].stage
;
1075 DWORD state
= stateblock
->contained_sampler_states
[i
].state
;
1076 DWORD value
= stateblock
->state
.sampler_states
[stage
][state
];
1078 if (stage
>= MAX_FRAGMENT_SAMPLERS
) stage
+= WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
;
1079 wined3d_device_set_sampler_state(device
, stage
, state
, value
);
1082 /* Transform states. */
1083 for (i
= 0; i
< stateblock
->num_contained_transform_states
; ++i
)
1085 wined3d_device_set_transform(device
, stateblock
->contained_transform_states
[i
],
1086 &stateblock
->state
.transforms
[stateblock
->contained_transform_states
[i
]]);
1089 if (stateblock
->changed
.primitive_type
)
1091 GLenum gl_primitive_type
, prev
;
1093 if (device
->recording
)
1094 device
->recording
->changed
.primitive_type
= TRUE
;
1095 gl_primitive_type
= stateblock
->state
.gl_primitive_type
;
1096 prev
= device
->update_state
->gl_primitive_type
;
1097 device
->update_state
->gl_primitive_type
= gl_primitive_type
;
1098 if (gl_primitive_type
!= prev
&& (gl_primitive_type
== GL_POINTS
|| prev
== GL_POINTS
))
1099 device_invalidate_state(device
, STATE_POINT_SIZE_ENABLE
);
1102 if (stateblock
->changed
.indices
)
1104 wined3d_device_set_index_buffer(device
, stateblock
->state
.index_buffer
, stateblock
->state
.index_format
);
1105 wined3d_device_set_base_vertex_index(device
, stateblock
->state
.base_vertex_index
);
1108 if (stateblock
->changed
.vertexDecl
&& stateblock
->state
.vertex_declaration
)
1109 wined3d_device_set_vertex_declaration(device
, stateblock
->state
.vertex_declaration
);
1111 if (stateblock
->changed
.material
)
1112 wined3d_device_set_material(device
, &stateblock
->state
.material
);
1114 if (stateblock
->changed
.viewport
)
1115 wined3d_device_set_viewport(device
, &stateblock
->state
.viewport
);
1117 if (stateblock
->changed
.scissorRect
)
1118 wined3d_device_set_scissor_rect(device
, &stateblock
->state
.scissor_rect
);
1120 map
= stateblock
->changed
.streamSource
;
1121 for (i
= 0; map
; map
>>= 1, ++i
)
1124 wined3d_device_set_stream_source(device
, i
,
1125 stateblock
->state
.streams
[i
].buffer
,
1126 0, stateblock
->state
.streams
[i
].stride
);
1129 map
= stateblock
->changed
.streamFreq
;
1130 for (i
= 0; map
; map
>>= 1, ++i
)
1133 wined3d_device_set_stream_source_freq(device
, i
,
1134 stateblock
->state
.streams
[i
].frequency
| stateblock
->state
.streams
[i
].flags
);
1137 map
= stateblock
->changed
.textures
;
1138 for (i
= 0; map
; map
>>= 1, ++i
)
1142 if (!(map
& 1)) continue;
1144 stage
= i
< MAX_FRAGMENT_SAMPLERS
? i
: WINED3DVERTEXTEXTURESAMPLER0
+ i
- MAX_FRAGMENT_SAMPLERS
;
1145 wined3d_device_set_texture(device
, stage
, stateblock
->state
.textures
[i
]);
1148 map
= stateblock
->changed
.clipplane
;
1149 for (i
= 0; map
; map
>>= 1, ++i
)
1151 if (!(map
& 1)) continue;
1153 wined3d_device_set_clip_plane(device
, i
, &stateblock
->state
.clip_planes
[i
]);
1156 stateblock
->device
->state
.lowest_disabled_stage
= MAX_TEXTURES
- 1;
1157 for (i
= 0; i
< MAX_TEXTURES
- 1; ++i
)
1159 if (stateblock
->device
->state
.texture_states
[i
][WINED3D_TSS_COLOR_OP
] == WINED3D_TOP_DISABLE
)
1161 stateblock
->device
->state
.lowest_disabled_stage
= i
;
1166 TRACE("Applied stateblock %p.\n", stateblock
);
1169 void state_init_default(struct wined3d_state
*state
, struct wined3d_device
*device
)
1171 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1174 struct wined3d_line_pattern lp
;
1182 struct wined3d_swapchain
*swapchain
;
1183 struct wined3d_surface
*backbuffer
;
1184 static const struct wined3d_matrix identity
=
1186 1.0f
, 0.0f
, 0.0f
, 0.0f
,
1187 0.0f
, 1.0f
, 0.0f
, 0.0f
,
1188 0.0f
, 0.0f
, 1.0f
, 0.0f
,
1189 0.0f
, 0.0f
, 0.0f
, 1.0f
,
1192 TRACE("state %p, device %p.\n", state
, device
);
1194 /* Set some of the defaults for lights, transforms etc */
1195 state
->transforms
[WINED3D_TS_PROJECTION
] = identity
;
1196 state
->transforms
[WINED3D_TS_VIEW
] = identity
;
1197 for (i
= 0; i
< 256; ++i
)
1199 state
->transforms
[WINED3D_TS_WORLD_MATRIX(i
)] = identity
;
1202 state
->fb
= &device
->fb
;
1204 TRACE("Render states\n");
1205 /* Render states: */
1206 if (device
->auto_depth_stencil
)
1207 state
->render_states
[WINED3D_RS_ZENABLE
] = WINED3D_ZB_TRUE
;
1209 state
->render_states
[WINED3D_RS_ZENABLE
] = WINED3D_ZB_FALSE
;
1210 state
->render_states
[WINED3D_RS_FILLMODE
] = WINED3D_FILL_SOLID
;
1211 state
->render_states
[WINED3D_RS_SHADEMODE
] = WINED3D_SHADE_GOURAUD
;
1212 lp
.lp
.repeat_factor
= 0;
1213 lp
.lp
.line_pattern
= 0;
1214 state
->render_states
[WINED3D_RS_LINEPATTERN
] = lp
.d
;
1215 state
->render_states
[WINED3D_RS_ZWRITEENABLE
] = TRUE
;
1216 state
->render_states
[WINED3D_RS_ALPHATESTENABLE
] = FALSE
;
1217 state
->render_states
[WINED3D_RS_LASTPIXEL
] = TRUE
;
1218 state
->render_states
[WINED3D_RS_SRCBLEND
] = WINED3D_BLEND_ONE
;
1219 state
->render_states
[WINED3D_RS_DESTBLEND
] = WINED3D_BLEND_ZERO
;
1220 state
->render_states
[WINED3D_RS_CULLMODE
] = WINED3D_CULL_CCW
;
1221 state
->render_states
[WINED3D_RS_ZFUNC
] = WINED3D_CMP_LESSEQUAL
;
1222 state
->render_states
[WINED3D_RS_ALPHAFUNC
] = WINED3D_CMP_ALWAYS
;
1223 state
->render_states
[WINED3D_RS_ALPHAREF
] = 0;
1224 state
->render_states
[WINED3D_RS_DITHERENABLE
] = FALSE
;
1225 state
->render_states
[WINED3D_RS_ALPHABLENDENABLE
] = FALSE
;
1226 state
->render_states
[WINED3D_RS_FOGENABLE
] = FALSE
;
1227 state
->render_states
[WINED3D_RS_SPECULARENABLE
] = FALSE
;
1228 state
->render_states
[WINED3D_RS_ZVISIBLE
] = 0;
1229 state
->render_states
[WINED3D_RS_FOGCOLOR
] = 0;
1230 state
->render_states
[WINED3D_RS_FOGTABLEMODE
] = WINED3D_FOG_NONE
;
1232 state
->render_states
[WINED3D_RS_FOGSTART
] = tmpfloat
.d
;
1234 state
->render_states
[WINED3D_RS_FOGEND
] = tmpfloat
.d
;
1236 state
->render_states
[WINED3D_RS_FOGDENSITY
] = tmpfloat
.d
;
1237 state
->render_states
[WINED3D_RS_EDGEANTIALIAS
] = FALSE
;
1238 state
->render_states
[WINED3D_RS_RANGEFOGENABLE
] = FALSE
;
1239 state
->render_states
[WINED3D_RS_STENCILENABLE
] = FALSE
;
1240 state
->render_states
[WINED3D_RS_STENCILFAIL
] = WINED3D_STENCIL_OP_KEEP
;
1241 state
->render_states
[WINED3D_RS_STENCILZFAIL
] = WINED3D_STENCIL_OP_KEEP
;
1242 state
->render_states
[WINED3D_RS_STENCILPASS
] = WINED3D_STENCIL_OP_KEEP
;
1243 state
->render_states
[WINED3D_RS_STENCILREF
] = 0;
1244 state
->render_states
[WINED3D_RS_STENCILMASK
] = 0xffffffff;
1245 state
->render_states
[WINED3D_RS_STENCILFUNC
] = WINED3D_CMP_ALWAYS
;
1246 state
->render_states
[WINED3D_RS_STENCILWRITEMASK
] = 0xffffffff;
1247 state
->render_states
[WINED3D_RS_TEXTUREFACTOR
] = 0xffffffff;
1248 state
->render_states
[WINED3D_RS_WRAP0
] = 0;
1249 state
->render_states
[WINED3D_RS_WRAP1
] = 0;
1250 state
->render_states
[WINED3D_RS_WRAP2
] = 0;
1251 state
->render_states
[WINED3D_RS_WRAP3
] = 0;
1252 state
->render_states
[WINED3D_RS_WRAP4
] = 0;
1253 state
->render_states
[WINED3D_RS_WRAP5
] = 0;
1254 state
->render_states
[WINED3D_RS_WRAP6
] = 0;
1255 state
->render_states
[WINED3D_RS_WRAP7
] = 0;
1256 state
->render_states
[WINED3D_RS_CLIPPING
] = TRUE
;
1257 state
->render_states
[WINED3D_RS_LIGHTING
] = TRUE
;
1258 state
->render_states
[WINED3D_RS_AMBIENT
] = 0;
1259 state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] = WINED3D_FOG_NONE
;
1260 state
->render_states
[WINED3D_RS_COLORVERTEX
] = TRUE
;
1261 state
->render_states
[WINED3D_RS_LOCALVIEWER
] = TRUE
;
1262 state
->render_states
[WINED3D_RS_NORMALIZENORMALS
] = FALSE
;
1263 state
->render_states
[WINED3D_RS_DIFFUSEMATERIALSOURCE
] = WINED3D_MCS_COLOR1
;
1264 state
->render_states
[WINED3D_RS_SPECULARMATERIALSOURCE
] = WINED3D_MCS_COLOR2
;
1265 state
->render_states
[WINED3D_RS_AMBIENTMATERIALSOURCE
] = WINED3D_MCS_MATERIAL
;
1266 state
->render_states
[WINED3D_RS_EMISSIVEMATERIALSOURCE
] = WINED3D_MCS_MATERIAL
;
1267 state
->render_states
[WINED3D_RS_VERTEXBLEND
] = WINED3D_VBF_DISABLE
;
1268 state
->render_states
[WINED3D_RS_CLIPPLANEENABLE
] = 0;
1269 state
->render_states
[WINED3D_RS_SOFTWAREVERTEXPROCESSING
] = FALSE
;
1271 state
->render_states
[WINED3D_RS_POINTSIZE
] = tmpfloat
.d
;
1273 state
->render_states
[WINED3D_RS_POINTSIZE_MIN
] = tmpfloat
.d
;
1274 state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
] = FALSE
;
1275 state
->render_states
[WINED3D_RS_POINTSCALEENABLE
] = FALSE
;
1277 state
->render_states
[WINED3D_RS_POINTSCALE_A
] = tmpfloat
.d
;
1279 state
->render_states
[WINED3D_RS_POINTSCALE_B
] = tmpfloat
.d
;
1281 state
->render_states
[WINED3D_RS_POINTSCALE_C
] = tmpfloat
.d
;
1282 state
->render_states
[WINED3D_RS_MULTISAMPLEANTIALIAS
] = TRUE
;
1283 state
->render_states
[WINED3D_RS_MULTISAMPLEMASK
] = 0xffffffff;
1284 state
->render_states
[WINED3D_RS_PATCHEDGESTYLE
] = WINED3D_PATCH_EDGE_DISCRETE
;
1286 state
->render_states
[WINED3D_RS_PATCHSEGMENTS
] = tmpfloat
.d
;
1287 state
->render_states
[WINED3D_RS_DEBUGMONITORTOKEN
] = 0xbaadcafe;
1288 tmpfloat
.f
= gl_info
->limits
.pointsize_max
;
1289 state
->render_states
[WINED3D_RS_POINTSIZE_MAX
] = tmpfloat
.d
;
1290 state
->render_states
[WINED3D_RS_INDEXEDVERTEXBLENDENABLE
] = FALSE
;
1291 state
->render_states
[WINED3D_RS_COLORWRITEENABLE
] = 0x0000000f;
1293 state
->render_states
[WINED3D_RS_TWEENFACTOR
] = tmpfloat
.d
;
1294 state
->render_states
[WINED3D_RS_BLENDOP
] = WINED3D_BLEND_OP_ADD
;
1295 state
->render_states
[WINED3D_RS_POSITIONDEGREE
] = WINED3D_DEGREE_CUBIC
;
1296 state
->render_states
[WINED3D_RS_NORMALDEGREE
] = WINED3D_DEGREE_LINEAR
;
1297 /* states new in d3d9 */
1298 state
->render_states
[WINED3D_RS_SCISSORTESTENABLE
] = FALSE
;
1299 state
->render_states
[WINED3D_RS_SLOPESCALEDEPTHBIAS
] = 0;
1301 state
->render_states
[WINED3D_RS_MINTESSELLATIONLEVEL
] = tmpfloat
.d
;
1302 state
->render_states
[WINED3D_RS_MAXTESSELLATIONLEVEL
] = tmpfloat
.d
;
1303 state
->render_states
[WINED3D_RS_ANTIALIASEDLINEENABLE
] = FALSE
;
1305 state
->render_states
[WINED3D_RS_ADAPTIVETESS_X
] = tmpfloat
.d
;
1306 state
->render_states
[WINED3D_RS_ADAPTIVETESS_Y
] = tmpfloat
.d
;
1308 state
->render_states
[WINED3D_RS_ADAPTIVETESS_Z
] = tmpfloat
.d
;
1310 state
->render_states
[WINED3D_RS_ADAPTIVETESS_W
] = tmpfloat
.d
;
1311 state
->render_states
[WINED3D_RS_ENABLEADAPTIVETESSELLATION
] = FALSE
;
1312 state
->render_states
[WINED3D_RS_TWOSIDEDSTENCILMODE
] = FALSE
;
1313 state
->render_states
[WINED3D_RS_CCW_STENCILFAIL
] = WINED3D_STENCIL_OP_KEEP
;
1314 state
->render_states
[WINED3D_RS_CCW_STENCILZFAIL
] = WINED3D_STENCIL_OP_KEEP
;
1315 state
->render_states
[WINED3D_RS_CCW_STENCILPASS
] = WINED3D_STENCIL_OP_KEEP
;
1316 state
->render_states
[WINED3D_RS_CCW_STENCILFUNC
] = WINED3D_CMP_ALWAYS
;
1317 state
->render_states
[WINED3D_RS_COLORWRITEENABLE1
] = 0x0000000f;
1318 state
->render_states
[WINED3D_RS_COLORWRITEENABLE2
] = 0x0000000f;
1319 state
->render_states
[WINED3D_RS_COLORWRITEENABLE3
] = 0x0000000f;
1320 state
->render_states
[WINED3D_RS_BLENDFACTOR
] = 0xffffffff;
1321 state
->render_states
[WINED3D_RS_SRGBWRITEENABLE
] = 0;
1322 state
->render_states
[WINED3D_RS_DEPTHBIAS
] = 0;
1323 state
->render_states
[WINED3D_RS_WRAP8
] = 0;
1324 state
->render_states
[WINED3D_RS_WRAP9
] = 0;
1325 state
->render_states
[WINED3D_RS_WRAP10
] = 0;
1326 state
->render_states
[WINED3D_RS_WRAP11
] = 0;
1327 state
->render_states
[WINED3D_RS_WRAP12
] = 0;
1328 state
->render_states
[WINED3D_RS_WRAP13
] = 0;
1329 state
->render_states
[WINED3D_RS_WRAP14
] = 0;
1330 state
->render_states
[WINED3D_RS_WRAP15
] = 0;
1331 state
->render_states
[WINED3D_RS_SEPARATEALPHABLENDENABLE
] = FALSE
;
1332 state
->render_states
[WINED3D_RS_SRCBLENDALPHA
] = WINED3D_BLEND_ONE
;
1333 state
->render_states
[WINED3D_RS_DESTBLENDALPHA
] = WINED3D_BLEND_ZERO
;
1334 state
->render_states
[WINED3D_RS_BLENDOPALPHA
] = WINED3D_BLEND_OP_ADD
;
1336 /* Texture Stage States - Put directly into state block, we will call function below */
1337 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1339 TRACE("Setting up default texture states for texture Stage %u.\n", i
);
1340 state
->transforms
[WINED3D_TS_TEXTURE0
+ i
] = identity
;
1341 state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
] = i
? WINED3D_TOP_DISABLE
: WINED3D_TOP_MODULATE
;
1342 state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] = WINED3DTA_TEXTURE
;
1343 state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] = WINED3DTA_CURRENT
;
1344 state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
] = i
? WINED3D_TOP_DISABLE
: WINED3D_TOP_SELECT_ARG1
;
1345 state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] = WINED3DTA_TEXTURE
;
1346 state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] = WINED3DTA_CURRENT
;
1347 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT00
] = 0;
1348 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT01
] = 0;
1349 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT10
] = 0;
1350 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT11
] = 0;
1351 state
->texture_states
[i
][WINED3D_TSS_TEXCOORD_INDEX
] = i
;
1352 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LSCALE
] = 0;
1353 state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LOFFSET
] = 0;
1354 state
->texture_states
[i
][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
] = WINED3D_TTFF_DISABLE
;
1355 state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] = WINED3DTA_CURRENT
;
1356 state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] = WINED3DTA_CURRENT
;
1357 state
->texture_states
[i
][WINED3D_TSS_RESULT_ARG
] = WINED3DTA_CURRENT
;
1359 state
->lowest_disabled_stage
= 1;
1362 for (i
= 0 ; i
< MAX_COMBINED_SAMPLERS
; ++i
)
1364 TRACE("Setting up default samplers states for sampler %u.\n", i
);
1365 state
->sampler_states
[i
][WINED3D_SAMP_ADDRESS_U
] = WINED3D_TADDRESS_WRAP
;
1366 state
->sampler_states
[i
][WINED3D_SAMP_ADDRESS_V
] = WINED3D_TADDRESS_WRAP
;
1367 state
->sampler_states
[i
][WINED3D_SAMP_ADDRESS_W
] = WINED3D_TADDRESS_WRAP
;
1368 state
->sampler_states
[i
][WINED3D_SAMP_BORDER_COLOR
] = 0;
1369 state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] = WINED3D_TEXF_POINT
;
1370 state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] = WINED3D_TEXF_POINT
;
1371 state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] = WINED3D_TEXF_NONE
;
1372 state
->sampler_states
[i
][WINED3D_SAMP_MIPMAP_LOD_BIAS
] = 0;
1373 state
->sampler_states
[i
][WINED3D_SAMP_MAX_MIP_LEVEL
] = 0;
1374 state
->sampler_states
[i
][WINED3D_SAMP_MAX_ANISOTROPY
] = 1;
1375 state
->sampler_states
[i
][WINED3D_SAMP_SRGB_TEXTURE
] = 0;
1376 /* TODO: Indicates which element of a multielement texture to use. */
1377 state
->sampler_states
[i
][WINED3D_SAMP_ELEMENT_INDEX
] = 0;
1378 /* TODO: Vertex offset in the presampled displacement map. */
1379 state
->sampler_states
[i
][WINED3D_SAMP_DMAP_OFFSET
] = 0;
1382 for (i
= 0; i
< gl_info
->limits
.textures
; ++i
)
1384 state
->textures
[i
] = NULL
;
1387 /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1388 if ((swapchain
= wined3d_device_get_swapchain(device
, 0)))
1390 if ((backbuffer
= wined3d_swapchain_get_back_buffer(swapchain
, 0, WINED3D_BACKBUFFER_TYPE_MONO
)))
1392 struct wined3d_resource_desc desc
;
1394 wined3d_resource_get_desc(&backbuffer
->resource
, &desc
);
1396 /* Set the default scissor rect values */
1397 state
->scissor_rect
.left
= 0;
1398 state
->scissor_rect
.right
= desc
.width
;
1399 state
->scissor_rect
.top
= 0;
1400 state
->scissor_rect
.bottom
= desc
.height
;
1403 /* Set the default viewport */
1404 state
->viewport
.x
= 0;
1405 state
->viewport
.y
= 0;
1406 state
->viewport
.width
= swapchain
->desc
.backbuffer_width
;
1407 state
->viewport
.height
= swapchain
->desc
.backbuffer_height
;
1408 state
->viewport
.min_z
= 0.0f
;
1409 state
->viewport
.max_z
= 1.0f
;
1415 static HRESULT
stateblock_init(struct wined3d_stateblock
*stateblock
,
1416 struct wined3d_device
*device
, enum wined3d_stateblock_type type
)
1419 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
1421 stateblock
->ref
= 1;
1422 stateblock
->device
= device
;
1424 if (FAILED(hr
= state_init(&stateblock
->state
, d3d_info
)))
1427 if (FAILED(hr
= stateblock_allocate_shader_constants(stateblock
)))
1429 state_cleanup(&stateblock
->state
);
1433 if (type
== WINED3D_SBT_RECORDED
)
1436 TRACE("Updating changed flags appropriate for type %#x.\n", type
);
1440 case WINED3D_SBT_ALL
:
1441 stateblock_init_lights(stateblock
, device
->state
.light_map
);
1442 stateblock_savedstates_set_all(&stateblock
->changed
,
1443 d3d_info
->limits
.vs_uniform_count
, d3d_info
->limits
.ps_uniform_count
);
1446 case WINED3D_SBT_PIXEL_STATE
:
1447 stateblock_savedstates_set_pixel(&stateblock
->changed
,
1448 d3d_info
->limits
.ps_uniform_count
);
1451 case WINED3D_SBT_VERTEX_STATE
:
1452 stateblock_init_lights(stateblock
, device
->state
.light_map
);
1453 stateblock_savedstates_set_vertex(&stateblock
->changed
,
1454 d3d_info
->limits
.vs_uniform_count
);
1458 FIXME("Unrecognized state block type %#x.\n", type
);
1462 stateblock_init_contained_states(stateblock
);
1463 wined3d_stateblock_capture(stateblock
);
1468 HRESULT CDECL
wined3d_stateblock_create(struct wined3d_device
*device
,
1469 enum wined3d_stateblock_type type
, struct wined3d_stateblock
**stateblock
)
1471 struct wined3d_stateblock
*object
;
1474 TRACE("device %p, type %#x, stateblock %p.\n",
1475 device
, type
, stateblock
);
1477 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1479 return E_OUTOFMEMORY
;
1481 hr
= stateblock_init(object
, device
, type
);
1484 WARN("Failed to initialize stateblock, hr %#x.\n", hr
);
1485 HeapFree(GetProcessHeap(), 0, object
);
1489 TRACE("Created stateblock %p.\n", object
);
1490 *stateblock
= object
;