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 struct wined3d_device *device = object->device;
199 /* Allocate space for floating point constants */
200 object->state.ps_consts_f = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
201 sizeof(float) * device->d3d_pshader_constantF * 4);
202 if (!object->state.ps_consts_f) goto fail;
204 object->changed.pixelShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
205 sizeof(BOOL) * device->d3d_pshader_constantF);
206 if (!object->changed.pixelShaderConstantsF) goto fail;
208 object->state.vs_consts_f = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
209 sizeof(float) * device->d3d_vshader_constantF * 4);
210 if (!object->state.vs_consts_f) goto fail;
212 object->changed.vertexShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
213 sizeof(BOOL) * device->d3d_vshader_constantF);
214 if (!object->changed.vertexShaderConstantsF) goto fail;
216 object->contained_vs_consts_f = HeapAlloc(GetProcessHeap(), 0,
217 sizeof(DWORD) * device->d3d_vshader_constantF);
218 if (!object->contained_vs_consts_f) goto fail;
220 object->contained_ps_consts_f = HeapAlloc(GetProcessHeap(), 0,
221 sizeof(DWORD) * device->d3d_pshader_constantF);
222 if (!object->contained_ps_consts_f) goto fail;
227 ERR("Failed to allocate memory\n");
228 HeapFree(GetProcessHeap(), 0, object->state.ps_consts_f);
229 HeapFree(GetProcessHeap(), 0, object->changed.pixelShaderConstantsF);
230 HeapFree(GetProcessHeap(), 0, object->state.vs_consts_f);
231 HeapFree(GetProcessHeap(), 0, object->changed.vertexShaderConstantsF);
232 HeapFree(GetProcessHeap(), 0, object->contained_vs_consts_f);
233 HeapFree(GetProcessHeap(), 0, object->contained_ps_consts_f);
234 return E_OUTOFMEMORY;
237 static inline void stateblock_set_bits(DWORD *map, UINT map_size)
239 DWORD mask = (1 << (map_size & 0x1f)) - 1;
240 memset(map, 0xff, (map_size >> 5) * sizeof(*map));
241 if (mask) map[map_size >> 5] = mask;
244 /* Set all members of a stateblock savedstate to the given value */
245 static void stateblock_savedstates_set_all(struct wined3d_saved_states *states, DWORD vs_consts, DWORD ps_consts)
250 states->primitive_type = 1;
252 states->material = 1;
253 states->viewport = 1;
254 states->vertexDecl = 1;
255 states->pixelShader = 1;
256 states->vertexShader = 1;
257 states->scissorRect = 1;
259 /* Fixed size arrays */
260 states->streamSource = 0xffff;
261 states->streamFreq = 0xffff;
262 states->textures = 0xfffff;
263 stateblock_set_bits(states->transform, HIGHEST_TRANSFORMSTATE + 1);
264 stateblock_set_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
265 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
266 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3ffe;
267 states->clipplane = 0xffffffff;
268 states->pixelShaderConstantsB = 0xffff;
269 states->pixelShaderConstantsI = 0xffff;
270 states->vertexShaderConstantsB = 0xffff;
271 states->vertexShaderConstantsI = 0xffff;
273 /* Dynamically sized arrays */
274 memset(states->pixelShaderConstantsF, TRUE, sizeof(BOOL) * ps_consts);
275 memset(states->vertexShaderConstantsF, TRUE, sizeof(BOOL) * vs_consts);
278 static void stateblock_savedstates_set_pixel(struct wined3d_saved_states *states, const DWORD num_constants)
280 DWORD texture_mask = 0;
281 WORD sampler_mask = 0;
284 states->pixelShader = 1;
286 for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
288 DWORD rs = pixel_states_render[i];
289 states->renderState[rs >> 5] |= 1 << (rs & 0x1f);
292 for (i = 0; i < sizeof(pixel_states_texture) / sizeof(*pixel_states_texture); ++i)
293 texture_mask |= 1 << pixel_states_texture[i];
294 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
295 for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
296 sampler_mask |= 1 << pixel_states_sampler[i];
297 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
298 states->pixelShaderConstantsB = 0xffff;
299 states->pixelShaderConstantsI = 0xffff;
301 memset(states->pixelShaderConstantsF, TRUE, sizeof(BOOL) * num_constants);
304 static void stateblock_savedstates_set_vertex(struct wined3d_saved_states *states, const DWORD num_constants)
306 DWORD texture_mask = 0;
307 WORD sampler_mask = 0;
310 states->vertexDecl = 1;
311 states->vertexShader = 1;
313 for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
315 DWORD rs = vertex_states_render[i];
316 states->renderState[rs >> 5] |= 1 << (rs & 0x1f);
319 for (i = 0; i < sizeof(vertex_states_texture) / sizeof(*vertex_states_texture); ++i)
320 texture_mask |= 1 << vertex_states_texture[i];
321 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
322 for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
323 sampler_mask |= 1 << vertex_states_sampler[i];
324 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
325 states->vertexShaderConstantsB = 0xffff;
326 states->vertexShaderConstantsI = 0xffff;
328 memset(states->vertexShaderConstantsF, TRUE, sizeof(BOOL) * num_constants);
331 void stateblock_init_contained_states(struct wined3d_stateblock *stateblock)
333 struct wined3d_device *device = stateblock->device;
336 for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
338 DWORD map = stateblock->changed.renderState[i];
339 for (j = 0; map; map >>= 1, ++j)
341 if (!(map & 1)) continue;
343 stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
344 ++stateblock->num_contained_render_states;
348 for (i = 0; i <= HIGHEST_TRANSFORMSTATE >> 5; ++i)
350 DWORD map = stateblock->changed.transform[i];
351 for (j = 0; map; map >>= 1, ++j)
353 if (!(map & 1)) continue;
355 stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
356 ++stateblock->num_contained_transform_states;
360 for (i = 0; i < device->d3d_vshader_constantF; ++i)
362 if (stateblock->changed.vertexShaderConstantsF[i])
364 stateblock->contained_vs_consts_f[stateblock->num_contained_vs_consts_f] = i;
365 ++stateblock->num_contained_vs_consts_f;
369 for (i = 0; i < MAX_CONST_I; ++i)
371 if (stateblock->changed.vertexShaderConstantsI & (1 << i))
373 stateblock->contained_vs_consts_i[stateblock->num_contained_vs_consts_i] = i;
374 ++stateblock->num_contained_vs_consts_i;
378 for (i = 0; i < MAX_CONST_B; ++i)
380 if (stateblock->changed.vertexShaderConstantsB & (1 << i))
382 stateblock->contained_vs_consts_b[stateblock->num_contained_vs_consts_b] = i;
383 ++stateblock->num_contained_vs_consts_b;
387 for (i = 0; i < device->d3d_pshader_constantF; ++i)
389 if (stateblock->changed.pixelShaderConstantsF[i])
391 stateblock->contained_ps_consts_f[stateblock->num_contained_ps_consts_f] = i;
392 ++stateblock->num_contained_ps_consts_f;
396 for (i = 0; i < MAX_CONST_I; ++i)
398 if (stateblock->changed.pixelShaderConstantsI & (1 << i))
400 stateblock->contained_ps_consts_i[stateblock->num_contained_ps_consts_i] = i;
401 ++stateblock->num_contained_ps_consts_i;
405 for (i = 0; i < MAX_CONST_B; ++i)
407 if (stateblock->changed.pixelShaderConstantsB & (1 << i))
409 stateblock->contained_ps_consts_b[stateblock->num_contained_ps_consts_b] = i;
410 ++stateblock->num_contained_ps_consts_b;
414 for (i = 0; i < MAX_TEXTURES; ++i)
416 DWORD map = stateblock->changed.textureState[i];
418 for(j = 0; map; map >>= 1, ++j)
420 if (!(map & 1)) continue;
422 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
423 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
424 ++stateblock->num_contained_tss_states;
428 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
430 DWORD map = stateblock->changed.samplerState[i];
432 for (j = 0; map; map >>= 1, ++j)
434 if (!(map & 1)) continue;
436 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
437 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
438 ++stateblock->num_contained_sampler_states;
443 static void stateblock_init_lights(struct wined3d_stateblock *stateblock, struct list *light_map)
447 for (i = 0; i < LIGHTMAP_SIZE; ++i)
449 const struct wined3d_light_info *src_light;
451 LIST_FOR_EACH_ENTRY(src_light, &light_map[i], struct wined3d_light_info, entry)
453 struct wined3d_light_info *dst_light = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_light));
455 *dst_light = *src_light;
456 list_add_tail(&stateblock->state.light_map[i], &dst_light->entry);
461 ULONG CDECL wined3d_stateblock_incref(struct wined3d_stateblock *stateblock)
463 ULONG refcount = InterlockedIncrement(&stateblock->ref);
465 TRACE("%p increasing refcount to %u.\n", stateblock, refcount);
470 void stateblock_unbind_resources(struct wined3d_stateblock *stateblock)
472 struct wined3d_state *state = &stateblock->state;
473 struct wined3d_vertex_declaration *decl;
474 struct wined3d_texture *texture;
475 struct wined3d_buffer *buffer;
476 struct wined3d_shader *shader;
479 if ((decl = state->vertex_declaration))
481 state->vertex_declaration = NULL;
482 wined3d_vertex_declaration_decref(decl);
485 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
487 if ((texture = state->textures[i]))
489 state->textures[i] = NULL;
490 wined3d_texture_decref(texture);
494 for (i = 0; i < MAX_STREAM_OUT; ++i)
496 if ((buffer = state->stream_output[i].buffer))
498 state->stream_output[i].buffer = NULL;
499 wined3d_buffer_decref(buffer);
503 for (i = 0; i < MAX_STREAMS; ++i)
505 if ((buffer = state->streams[i].buffer))
507 state->streams[i].buffer = NULL;
508 wined3d_buffer_decref(buffer);
512 if ((buffer = state->index_buffer))
514 state->index_buffer = NULL;
515 wined3d_buffer_decref(buffer);
518 if ((shader = state->vertex_shader))
520 state->vertex_shader = NULL;
521 wined3d_shader_decref(shader);
524 for (i = 0; i < MAX_CONSTANT_BUFFERS; ++i)
526 if ((buffer = state->vs_cb[i]))
528 state->vs_cb[i] = NULL;
529 wined3d_buffer_decref(buffer);
533 if ((shader = state->geometry_shader))
535 state->geometry_shader = NULL;
536 wined3d_shader_decref(shader);
539 for (i = 0; i < MAX_CONSTANT_BUFFERS; ++i)
541 if ((buffer = state->gs_cb[i]))
543 state->gs_cb[i] = NULL;
544 wined3d_buffer_decref(buffer);
548 if ((shader = state->pixel_shader))
550 state->pixel_shader = NULL;
551 wined3d_shader_decref(shader);
555 ULONG CDECL wined3d_stateblock_decref(struct wined3d_stateblock *stateblock)
557 ULONG refcount = InterlockedDecrement(&stateblock->ref);
559 TRACE("%p decreasing refcount to %u\n", stateblock, refcount);
565 stateblock_unbind_resources(stateblock);
567 for (counter = 0; counter < LIGHTMAP_SIZE; ++counter)
569 struct list *e1, *e2;
570 LIST_FOR_EACH_SAFE(e1, e2, &stateblock->state.light_map[counter])
572 struct wined3d_light_info *light = LIST_ENTRY(e1, struct wined3d_light_info, entry);
573 list_remove(&light->entry);
574 HeapFree(GetProcessHeap(), 0, light);
578 HeapFree(GetProcessHeap(), 0, stateblock->state.vs_consts_f);
579 HeapFree(GetProcessHeap(), 0, stateblock->changed.vertexShaderConstantsF);
580 HeapFree(GetProcessHeap(), 0, stateblock->state.ps_consts_f);
581 HeapFree(GetProcessHeap(), 0, stateblock->changed.pixelShaderConstantsF);
582 HeapFree(GetProcessHeap(), 0, stateblock->contained_vs_consts_f);
583 HeapFree(GetProcessHeap(), 0, stateblock->contained_ps_consts_f);
584 HeapFree(GetProcessHeap(), 0, stateblock);
590 static void wined3d_state_record_lights(struct wined3d_state *dst_state, const struct wined3d_state *src_state)
594 /* Lights... For a recorded state block, we just had a chain of actions
595 * to perform, so we need to walk that chain and update any actions which
597 for (i = 0; i < LIGHTMAP_SIZE; ++i)
600 LIST_FOR_EACH(e, &dst_state->light_map[i])
602 BOOL updated = FALSE;
603 struct wined3d_light_info *src = LIST_ENTRY(e, struct wined3d_light_info, entry), *realLight;
605 /* Look up the light in the destination */
606 LIST_FOR_EACH(f, &src_state->light_map[i])
608 realLight = LIST_ENTRY(f, struct wined3d_light_info, entry);
609 if (realLight->OriginalIndex == src->OriginalIndex)
611 src->OriginalParms = realLight->OriginalParms;
613 if (realLight->glIndex == -1 && src->glIndex != -1)
616 dst_state->lights[src->glIndex] = NULL;
618 else if (realLight->glIndex != -1 && src->glIndex == -1)
621 dst_state->lights[realLight->glIndex] = src;
623 src->glIndex = realLight->glIndex;
631 /* This can happen if the light was originally created as a
632 * default light for SetLightEnable() while recording. */
633 WARN("Light %u in dst_state %p does not exist in src_state %p.\n",
634 src->OriginalIndex, dst_state, src_state);
636 src->OriginalParms = WINED3D_default_light;
637 if (src->glIndex != -1)
639 dst_state->lights[src->glIndex] = NULL;
647 void CDECL wined3d_stateblock_capture(struct wined3d_stateblock *stateblock)
649 const struct wined3d_state *src_state = &stateblock->device->stateBlock->state;
653 TRACE("stateblock %p.\n", stateblock);
655 TRACE("Capturing state %p.\n", src_state);
657 if (stateblock->changed.vertexShader && stateblock->state.vertex_shader != src_state->vertex_shader)
659 TRACE("Updating vertex shader from %p to %p\n",
660 stateblock->state.vertex_shader, src_state->vertex_shader);
662 if (src_state->vertex_shader)
663 wined3d_shader_incref(src_state->vertex_shader);
664 if (stateblock->state.vertex_shader)
665 wined3d_shader_decref(stateblock->state.vertex_shader);
666 stateblock->state.vertex_shader = src_state->vertex_shader;
669 /* Vertex shader float constants. */
670 for (i = 0; i < stateblock->num_contained_vs_consts_f; ++i)
672 unsigned int idx = stateblock->contained_vs_consts_f[i];
674 TRACE("Setting vs_consts_f[%u] to {%.8e, %.8e, %.8e, %.8e}.\n", idx,
675 src_state->vs_consts_f[idx * 4 + 0],
676 src_state->vs_consts_f[idx * 4 + 1],
677 src_state->vs_consts_f[idx * 4 + 2],
678 src_state->vs_consts_f[idx * 4 + 3]);
680 stateblock->state.vs_consts_f[idx * 4 + 0] = src_state->vs_consts_f[idx * 4 + 0];
681 stateblock->state.vs_consts_f[idx * 4 + 1] = src_state->vs_consts_f[idx * 4 + 1];
682 stateblock->state.vs_consts_f[idx * 4 + 2] = src_state->vs_consts_f[idx * 4 + 2];
683 stateblock->state.vs_consts_f[idx * 4 + 3] = src_state->vs_consts_f[idx * 4 + 3];
686 /* Vertex shader integer constants. */
687 for (i = 0; i < stateblock->num_contained_vs_consts_i; ++i)
689 unsigned int idx = stateblock->contained_vs_consts_i[i];
691 TRACE("Setting vs_consts[%u] to {%d, %d, %d, %d}.\n", idx,
692 src_state->vs_consts_i[idx * 4 + 0],
693 src_state->vs_consts_i[idx * 4 + 1],
694 src_state->vs_consts_i[idx * 4 + 2],
695 src_state->vs_consts_i[idx * 4 + 3]);
697 stateblock->state.vs_consts_i[idx * 4 + 0] = src_state->vs_consts_i[idx * 4 + 0];
698 stateblock->state.vs_consts_i[idx * 4 + 1] = src_state->vs_consts_i[idx * 4 + 1];
699 stateblock->state.vs_consts_i[idx * 4 + 2] = src_state->vs_consts_i[idx * 4 + 2];
700 stateblock->state.vs_consts_i[idx * 4 + 3] = src_state->vs_consts_i[idx * 4 + 3];
703 /* Vertex shader boolean constants. */
704 for (i = 0; i < stateblock->num_contained_vs_consts_b; ++i)
706 unsigned int idx = stateblock->contained_vs_consts_b[i];
708 TRACE("Setting vs_consts_b[%u] to %s.\n",
709 idx, src_state->vs_consts_b[idx] ? "TRUE" : "FALSE");
711 stateblock->state.vs_consts_b[idx] = src_state->vs_consts_b[idx];
714 /* Pixel shader float constants. */
715 for (i = 0; i < stateblock->num_contained_ps_consts_f; ++i)
717 unsigned int idx = stateblock->contained_ps_consts_f[i];
719 TRACE("Setting ps_consts_f[%u] to {%.8e, %.8e, %.8e, %.8e}.\n", idx,
720 src_state->ps_consts_f[idx * 4 + 0],
721 src_state->ps_consts_f[idx * 4 + 1],
722 src_state->ps_consts_f[idx * 4 + 2],
723 src_state->ps_consts_f[idx * 4 + 3]);
725 stateblock->state.ps_consts_f[idx * 4 + 0] = src_state->ps_consts_f[idx * 4 + 0];
726 stateblock->state.ps_consts_f[idx * 4 + 1] = src_state->ps_consts_f[idx * 4 + 1];
727 stateblock->state.ps_consts_f[idx * 4 + 2] = src_state->ps_consts_f[idx * 4 + 2];
728 stateblock->state.ps_consts_f[idx * 4 + 3] = src_state->ps_consts_f[idx * 4 + 3];
731 /* Pixel shader integer constants. */
732 for (i = 0; i < stateblock->num_contained_ps_consts_i; ++i)
734 unsigned int idx = stateblock->contained_ps_consts_i[i];
735 TRACE("Setting ps_consts_i[%u] to {%d, %d, %d, %d}.\n", idx,
736 src_state->ps_consts_i[idx * 4 + 0],
737 src_state->ps_consts_i[idx * 4 + 1],
738 src_state->ps_consts_i[idx * 4 + 2],
739 src_state->ps_consts_i[idx * 4 + 3]);
741 stateblock->state.ps_consts_i[idx * 4 + 0] = src_state->ps_consts_i[idx * 4 + 0];
742 stateblock->state.ps_consts_i[idx * 4 + 1] = src_state->ps_consts_i[idx * 4 + 1];
743 stateblock->state.ps_consts_i[idx * 4 + 2] = src_state->ps_consts_i[idx * 4 + 2];
744 stateblock->state.ps_consts_i[idx * 4 + 3] = src_state->ps_consts_i[idx * 4 + 3];
747 /* Pixel shader boolean constants. */
748 for (i = 0; i < stateblock->num_contained_ps_consts_b; ++i)
750 unsigned int idx = stateblock->contained_ps_consts_b[i];
751 TRACE("Setting ps_consts_b[%u] to %s.\n",
752 idx, src_state->ps_consts_b[idx] ? "TRUE" : "FALSE");
754 stateblock->state.ps_consts_b[idx] = src_state->ps_consts_b[idx];
757 /* Others + Render & Texture */
758 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
760 enum wined3d_transform_state transform = stateblock->contained_transform_states[i];
762 TRACE("Updating transform %#x.\n", transform);
764 stateblock->state.transforms[transform] = src_state->transforms[transform];
767 if (stateblock->changed.primitive_type)
768 stateblock->state.gl_primitive_type = src_state->gl_primitive_type;
770 if (stateblock->changed.indices
771 && ((stateblock->state.index_buffer != src_state->index_buffer)
772 || (stateblock->state.base_vertex_index != src_state->base_vertex_index)
773 || (stateblock->state.index_format != src_state->index_format)))
775 TRACE("Updating index buffer to %p, base vertex index to %d.\n",
776 src_state->index_buffer, src_state->base_vertex_index);
778 if (src_state->index_buffer)
779 wined3d_buffer_incref(src_state->index_buffer);
780 if (stateblock->state.index_buffer)
781 wined3d_buffer_decref(stateblock->state.index_buffer);
782 stateblock->state.index_buffer = src_state->index_buffer;
783 stateblock->state.base_vertex_index = src_state->base_vertex_index;
784 stateblock->state.index_format = src_state->index_format;
787 if (stateblock->changed.vertexDecl && stateblock->state.vertex_declaration != src_state->vertex_declaration)
789 TRACE("Updating vertex declaration from %p to %p.\n",
790 stateblock->state.vertex_declaration, src_state->vertex_declaration);
792 if (src_state->vertex_declaration)
793 wined3d_vertex_declaration_incref(src_state->vertex_declaration);
794 if (stateblock->state.vertex_declaration)
795 wined3d_vertex_declaration_decref(stateblock->state.vertex_declaration);
796 stateblock->state.vertex_declaration = src_state->vertex_declaration;
799 if (stateblock->changed.material
800 && memcmp(&src_state->material, &stateblock->state.material, sizeof(stateblock->state.material)))
802 TRACE("Updating material.\n");
804 stateblock->state.material = src_state->material;
807 if (stateblock->changed.viewport
808 && memcmp(&src_state->viewport, &stateblock->state.viewport, sizeof(stateblock->state.viewport)))
810 TRACE("Updating viewport.\n");
812 stateblock->state.viewport = src_state->viewport;
815 if (stateblock->changed.scissorRect && memcmp(&src_state->scissor_rect,
816 &stateblock->state.scissor_rect, sizeof(stateblock->state.scissor_rect)))
818 TRACE("Updating scissor rect.\n");
820 stateblock->state.scissor_rect = src_state->scissor_rect;
823 map = stateblock->changed.streamSource;
824 for (i = 0; map; map >>= 1, ++i)
826 if (!(map & 1)) continue;
828 if (stateblock->state.streams[i].stride != src_state->streams[i].stride
829 || stateblock->state.streams[i].buffer != src_state->streams[i].buffer)
831 TRACE("Updating stream source %u to %p, stride to %u.\n",
832 i, src_state->streams[i].buffer,
833 src_state->streams[i].stride);
835 stateblock->state.streams[i].stride = src_state->streams[i].stride;
836 if (src_state->streams[i].buffer)
837 wined3d_buffer_incref(src_state->streams[i].buffer);
838 if (stateblock->state.streams[i].buffer)
839 wined3d_buffer_decref(stateblock->state.streams[i].buffer);
840 stateblock->state.streams[i].buffer = src_state->streams[i].buffer;
844 map = stateblock->changed.streamFreq;
845 for (i = 0; map; map >>= 1, ++i)
847 if (!(map & 1)) continue;
849 if (stateblock->state.streams[i].frequency != src_state->streams[i].frequency
850 || stateblock->state.streams[i].flags != src_state->streams[i].flags)
852 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
853 i, src_state->streams[i].frequency, src_state->streams[i].flags);
855 stateblock->state.streams[i].frequency = src_state->streams[i].frequency;
856 stateblock->state.streams[i].flags = src_state->streams[i].flags;
860 map = stateblock->changed.clipplane;
861 for (i = 0; map; map >>= 1, ++i)
863 if (!(map & 1)) continue;
865 if (memcmp(&stateblock->state.clip_planes[i], &src_state->clip_planes[i], sizeof(src_state->clip_planes[i])))
867 TRACE("Updating clipplane %u.\n", i);
868 stateblock->state.clip_planes[i] = src_state->clip_planes[i];
873 for (i = 0; i < stateblock->num_contained_render_states; ++i)
875 enum wined3d_render_state rs = stateblock->contained_render_states[i];
877 TRACE("Updating render state %#x to %u.\n", rs, src_state->render_states[rs]);
879 stateblock->state.render_states[rs] = src_state->render_states[rs];
883 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
885 DWORD stage = stateblock->contained_tss_states[i].stage;
886 DWORD state = stateblock->contained_tss_states[i].state;
888 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage, state,
889 src_state->texture_states[stage][state], stateblock->state.texture_states[stage][state]);
891 stateblock->state.texture_states[stage][state] = src_state->texture_states[stage][state];
895 map = stateblock->changed.textures;
896 for (i = 0; map; map >>= 1, ++i)
898 if (!(map & 1)) continue;
900 TRACE("Updating texture %u to %p (was %p).\n",
901 i, src_state->textures[i], stateblock->state.textures[i]);
903 if (src_state->textures[i])
904 wined3d_texture_incref(src_state->textures[i]);
905 if (stateblock->state.textures[i])
906 wined3d_texture_decref(stateblock->state.textures[i]);
907 stateblock->state.textures[i] = src_state->textures[i];
910 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
912 DWORD stage = stateblock->contained_sampler_states[i].stage;
913 DWORD state = stateblock->contained_sampler_states[i].state;
915 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage, state,
916 src_state->sampler_states[stage][state], stateblock->state.sampler_states[stage][state]);
918 stateblock->state.sampler_states[stage][state] = src_state->sampler_states[stage][state];
921 if (stateblock->changed.pixelShader && stateblock->state.pixel_shader != src_state->pixel_shader)
923 if (src_state->pixel_shader)
924 wined3d_shader_incref(src_state->pixel_shader);
925 if (stateblock->state.pixel_shader)
926 wined3d_shader_decref(stateblock->state.pixel_shader);
927 stateblock->state.pixel_shader = src_state->pixel_shader;
930 wined3d_state_record_lights(&stateblock->state, src_state);
932 TRACE("Capture done.\n");
935 static void apply_lights(struct wined3d_device *device, const struct wined3d_state *state)
939 for (i = 0; i < LIGHTMAP_SIZE; ++i)
943 LIST_FOR_EACH(e, &state->light_map[i])
945 const struct wined3d_light_info *light = LIST_ENTRY(e, struct wined3d_light_info, entry);
947 wined3d_device_set_light(device, light->OriginalIndex, &light->OriginalParms);
948 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
953 void CDECL wined3d_stateblock_apply(const struct wined3d_stateblock *stateblock)
955 struct wined3d_device *device = stateblock->device;
959 TRACE("Applying stateblock %p to device %p.\n", stateblock, device);
961 if (stateblock->changed.vertexShader)
962 wined3d_device_set_vertex_shader(device, stateblock->state.vertex_shader);
964 /* Vertex Shader Constants. */
965 for (i = 0; i < stateblock->num_contained_vs_consts_f; ++i)
967 wined3d_device_set_vs_consts_f(device, stateblock->contained_vs_consts_f[i],
968 stateblock->state.vs_consts_f + stateblock->contained_vs_consts_f[i] * 4, 1);
970 for (i = 0; i < stateblock->num_contained_vs_consts_i; ++i)
972 wined3d_device_set_vs_consts_i(device, stateblock->contained_vs_consts_i[i],
973 stateblock->state.vs_consts_i + stateblock->contained_vs_consts_i[i] * 4, 1);
975 for (i = 0; i < stateblock->num_contained_vs_consts_b; ++i)
977 wined3d_device_set_vs_consts_b(device, stateblock->contained_vs_consts_b[i],
978 stateblock->state.vs_consts_b + stateblock->contained_vs_consts_b[i], 1);
981 apply_lights(device, &stateblock->state);
983 if (stateblock->changed.pixelShader)
984 wined3d_device_set_pixel_shader(device, stateblock->state.pixel_shader);
986 /* Pixel Shader Constants. */
987 for (i = 0; i < stateblock->num_contained_ps_consts_f; ++i)
989 wined3d_device_set_ps_consts_f(device, stateblock->contained_ps_consts_f[i],
990 stateblock->state.ps_consts_f + stateblock->contained_ps_consts_f[i] * 4, 1);
992 for (i = 0; i < stateblock->num_contained_ps_consts_i; ++i)
994 wined3d_device_set_ps_consts_i(device, stateblock->contained_ps_consts_i[i],
995 stateblock->state.ps_consts_i + stateblock->contained_ps_consts_i[i] * 4, 1);
997 for (i = 0; i < stateblock->num_contained_ps_consts_b; ++i)
999 wined3d_device_set_ps_consts_b(device, stateblock->contained_ps_consts_b[i],
1000 stateblock->state.ps_consts_b + stateblock->contained_ps_consts_b[i], 1);
1003 /* Render states. */
1004 for (i = 0; i < stateblock->num_contained_render_states; ++i)
1006 wined3d_device_set_render_state(device, stateblock->contained_render_states[i],
1007 stateblock->state.render_states[stateblock->contained_render_states[i]]);
1010 /* Texture states. */
1011 for (i = 0; i < stateblock->num_contained_tss_states; ++i)
1013 DWORD stage = stateblock->contained_tss_states[i].stage;
1014 DWORD state = stateblock->contained_tss_states[i].state;
1016 wined3d_device_set_texture_stage_state(device, stage, state, stateblock->state.texture_states[stage][state]);
1019 /* Sampler states. */
1020 for (i = 0; i < stateblock->num_contained_sampler_states; ++i)
1022 DWORD stage = stateblock->contained_sampler_states[i].stage;
1023 DWORD state = stateblock->contained_sampler_states[i].state;
1024 DWORD value = stateblock->state.sampler_states[stage][state];
1026 if (stage >= MAX_FRAGMENT_SAMPLERS) stage += WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS;
1027 wined3d_device_set_sampler_state(device, stage, state, value);
1030 /* Transform states. */
1031 for (i = 0; i < stateblock->num_contained_transform_states; ++i)
1033 wined3d_device_set_transform(device, stateblock->contained_transform_states[i],
1034 &stateblock->state.transforms[stateblock->contained_transform_states[i]]);
1037 if (stateblock->changed.primitive_type)
1039 stateblock->device->updateStateBlock->changed.primitive_type = TRUE;
1040 stateblock->device->updateStateBlock->state.gl_primitive_type = stateblock->state.gl_primitive_type;
1043 if (stateblock->changed.indices)
1045 wined3d_device_set_index_buffer(device, stateblock->state.index_buffer, stateblock->state.index_format);
1046 wined3d_device_set_base_vertex_index(device, stateblock->state.base_vertex_index);
1049 if (stateblock->changed.vertexDecl && stateblock->state.vertex_declaration)
1050 wined3d_device_set_vertex_declaration(device, stateblock->state.vertex_declaration);
1052 if (stateblock->changed.material)
1053 wined3d_device_set_material(device, &stateblock->state.material);
1055 if (stateblock->changed.viewport)
1056 wined3d_device_set_viewport(device, &stateblock->state.viewport);
1058 if (stateblock->changed.scissorRect)
1059 wined3d_device_set_scissor_rect(device, &stateblock->state.scissor_rect);
1061 map = stateblock->changed.streamSource;
1062 for (i = 0; map; map >>= 1, ++i)
1065 wined3d_device_set_stream_source(device, i,
1066 stateblock->state.streams[i].buffer,
1067 0, stateblock->state.streams[i].stride);
1070 map = stateblock->changed.streamFreq;
1071 for (i = 0; map; map >>= 1, ++i)
1074 wined3d_device_set_stream_source_freq(device, i,
1075 stateblock->state.streams[i].frequency | stateblock->state.streams[i].flags);
1078 map = stateblock->changed.textures;
1079 for (i = 0; map; map >>= 1, ++i)
1083 if (!(map & 1)) continue;
1085 stage = i < MAX_FRAGMENT_SAMPLERS ? i : WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS;
1086 wined3d_device_set_texture(device, stage, stateblock->state.textures[i]);
1089 map = stateblock->changed.clipplane;
1090 for (i = 0; map; map >>= 1, ++i)
1092 if (!(map & 1)) continue;
1094 wined3d_device_set_clip_plane(device, i, &stateblock->state.clip_planes[i]);
1097 stateblock->device->stateBlock->state.lowest_disabled_stage = MAX_TEXTURES - 1;
1098 for (i = 0; i < MAX_TEXTURES - 1; ++i)
1100 if (stateblock->device->stateBlock->state.texture_states[i][WINED3D_TSS_COLOR_OP] == WINED3D_TOP_DISABLE)
1102 stateblock->device->stateBlock->state.lowest_disabled_stage = i;
1107 TRACE("Applied stateblock %p.\n", stateblock);
1110 void stateblock_init_default_state(struct wined3d_stateblock *stateblock)
1112 struct wined3d_device *device = stateblock->device;
1113 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1114 struct wined3d_state *state = &stateblock->state;
1117 struct wined3d_line_pattern lp;
1125 struct wined3d_swapchain *swapchain;
1126 struct wined3d_surface *backbuffer;
1128 TRACE("stateblock %p.\n", stateblock);
1130 memset(stateblock->changed.pixelShaderConstantsF, 0, device->d3d_pshader_constantF * sizeof(BOOL));
1131 memset(stateblock->changed.vertexShaderConstantsF, 0, device->d3d_vshader_constantF * sizeof(BOOL));
1133 /* Set some of the defaults for lights, transforms etc */
1134 memcpy(&state->transforms[WINED3D_TS_PROJECTION], &identity, sizeof(identity));
1135 memcpy(&state->transforms[WINED3D_TS_VIEW], &identity, sizeof(identity));
1136 for (i = 0; i < 256; ++i)
1138 memcpy(&state->transforms[WINED3D_TS_WORLD_MATRIX(i)], &identity, sizeof(identity));
1141 state->fb = &device->fb;
1143 TRACE("Render states\n");
1144 /* Render states: */
1145 if (device->auto_depth_stencil)
1146 state->render_states[WINED3D_RS_ZENABLE] = WINED3D_ZB_TRUE;
1148 state->render_states[WINED3D_RS_ZENABLE] = WINED3D_ZB_FALSE;
1149 state->render_states[WINED3D_RS_FILLMODE] = WINED3D_FILL_SOLID;
1150 state->render_states[WINED3D_RS_SHADEMODE] = WINED3D_SHADE_GOURAUD;
1151 lp.lp.repeat_factor = 0;
1152 lp.lp.line_pattern = 0;
1153 state->render_states[WINED3D_RS_LINEPATTERN] = lp.d;
1154 state->render_states[WINED3D_RS_ZWRITEENABLE] = TRUE;
1155 state->render_states[WINED3D_RS_ALPHATESTENABLE] = FALSE;
1156 state->render_states[WINED3D_RS_LASTPIXEL] = TRUE;
1157 state->render_states[WINED3D_RS_SRCBLEND] = WINED3D_BLEND_ONE;
1158 state->render_states[WINED3D_RS_DESTBLEND] = WINED3D_BLEND_ZERO;
1159 state->render_states[WINED3D_RS_CULLMODE] = WINED3D_CULL_CCW;
1160 state->render_states[WINED3D_RS_ZFUNC] = WINED3D_CMP_LESSEQUAL;
1161 state->render_states[WINED3D_RS_ALPHAFUNC] = WINED3D_CMP_ALWAYS;
1162 state->render_states[WINED3D_RS_ALPHAREF] = 0;
1163 state->render_states[WINED3D_RS_DITHERENABLE] = FALSE;
1164 state->render_states[WINED3D_RS_ALPHABLENDENABLE] = FALSE;
1165 state->render_states[WINED3D_RS_FOGENABLE] = FALSE;
1166 state->render_states[WINED3D_RS_SPECULARENABLE] = FALSE;
1167 state->render_states[WINED3D_RS_ZVISIBLE] = 0;
1168 state->render_states[WINED3D_RS_FOGCOLOR] = 0;
1169 state->render_states[WINED3D_RS_FOGTABLEMODE] = WINED3D_FOG_NONE;
1171 state->render_states[WINED3D_RS_FOGSTART] = tmpfloat.d;
1173 state->render_states[WINED3D_RS_FOGEND] = tmpfloat.d;
1175 state->render_states[WINED3D_RS_FOGDENSITY] = tmpfloat.d;
1176 state->render_states[WINED3D_RS_EDGEANTIALIAS] = FALSE;
1177 state->render_states[WINED3D_RS_RANGEFOGENABLE] = FALSE;
1178 state->render_states[WINED3D_RS_STENCILENABLE] = FALSE;
1179 state->render_states[WINED3D_RS_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1180 state->render_states[WINED3D_RS_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1181 state->render_states[WINED3D_RS_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1182 state->render_states[WINED3D_RS_STENCILREF] = 0;
1183 state->render_states[WINED3D_RS_STENCILMASK] = 0xffffffff;
1184 state->render_states[WINED3D_RS_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1185 state->render_states[WINED3D_RS_STENCILWRITEMASK] = 0xffffffff;
1186 state->render_states[WINED3D_RS_TEXTUREFACTOR] = 0xffffffff;
1187 state->render_states[WINED3D_RS_WRAP0] = 0;
1188 state->render_states[WINED3D_RS_WRAP1] = 0;
1189 state->render_states[WINED3D_RS_WRAP2] = 0;
1190 state->render_states[WINED3D_RS_WRAP3] = 0;
1191 state->render_states[WINED3D_RS_WRAP4] = 0;
1192 state->render_states[WINED3D_RS_WRAP5] = 0;
1193 state->render_states[WINED3D_RS_WRAP6] = 0;
1194 state->render_states[WINED3D_RS_WRAP7] = 0;
1195 state->render_states[WINED3D_RS_CLIPPING] = TRUE;
1196 state->render_states[WINED3D_RS_LIGHTING] = TRUE;
1197 state->render_states[WINED3D_RS_AMBIENT] = 0;
1198 state->render_states[WINED3D_RS_FOGVERTEXMODE] = WINED3D_FOG_NONE;
1199 state->render_states[WINED3D_RS_COLORVERTEX] = TRUE;
1200 state->render_states[WINED3D_RS_LOCALVIEWER] = TRUE;
1201 state->render_states[WINED3D_RS_NORMALIZENORMALS] = FALSE;
1202 state->render_states[WINED3D_RS_DIFFUSEMATERIALSOURCE] = WINED3D_MCS_COLOR1;
1203 state->render_states[WINED3D_RS_SPECULARMATERIALSOURCE] = WINED3D_MCS_COLOR2;
1204 state->render_states[WINED3D_RS_AMBIENTMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1205 state->render_states[WINED3D_RS_EMISSIVEMATERIALSOURCE] = WINED3D_MCS_MATERIAL;
1206 state->render_states[WINED3D_RS_VERTEXBLEND] = WINED3D_VBF_DISABLE;
1207 state->render_states[WINED3D_RS_CLIPPLANEENABLE] = 0;
1208 state->render_states[WINED3D_RS_SOFTWAREVERTEXPROCESSING] = FALSE;
1210 state->render_states[WINED3D_RS_POINTSIZE] = tmpfloat.d;
1212 state->render_states[WINED3D_RS_POINTSIZE_MIN] = tmpfloat.d;
1213 state->render_states[WINED3D_RS_POINTSPRITEENABLE] = FALSE;
1214 state->render_states[WINED3D_RS_POINTSCALEENABLE] = FALSE;
1216 state->render_states[WINED3D_RS_POINTSCALE_A] = tmpfloat.d;
1218 state->render_states[WINED3D_RS_POINTSCALE_B] = tmpfloat.d;
1220 state->render_states[WINED3D_RS_POINTSCALE_C] = tmpfloat.d;
1221 state->render_states[WINED3D_RS_MULTISAMPLEANTIALIAS] = TRUE;
1222 state->render_states[WINED3D_RS_MULTISAMPLEMASK] = 0xffffffff;
1223 state->render_states[WINED3D_RS_PATCHEDGESTYLE] = WINED3D_PATCH_EDGE_DISCRETE;
1225 state->render_states[WINED3D_RS_PATCHSEGMENTS] = tmpfloat.d;
1226 state->render_states[WINED3D_RS_DEBUGMONITORTOKEN] = 0xbaadcafe;
1227 tmpfloat.f = gl_info->limits.pointsize_max;
1228 state->render_states[WINED3D_RS_POINTSIZE_MAX] = tmpfloat.d;
1229 state->render_states[WINED3D_RS_INDEXEDVERTEXBLENDENABLE] = FALSE;
1230 state->render_states[WINED3D_RS_COLORWRITEENABLE] = 0x0000000f;
1232 state->render_states[WINED3D_RS_TWEENFACTOR] = tmpfloat.d;
1233 state->render_states[WINED3D_RS_BLENDOP] = WINED3D_BLEND_OP_ADD;
1234 state->render_states[WINED3D_RS_POSITIONDEGREE] = WINED3D_DEGREE_CUBIC;
1235 state->render_states[WINED3D_RS_NORMALDEGREE] = WINED3D_DEGREE_LINEAR;
1236 /* states new in d3d9 */
1237 state->render_states[WINED3D_RS_SCISSORTESTENABLE] = FALSE;
1238 state->render_states[WINED3D_RS_SLOPESCALEDEPTHBIAS] = 0;
1240 state->render_states[WINED3D_RS_MINTESSELLATIONLEVEL] = tmpfloat.d;
1241 state->render_states[WINED3D_RS_MAXTESSELLATIONLEVEL] = tmpfloat.d;
1242 state->render_states[WINED3D_RS_ANTIALIASEDLINEENABLE] = FALSE;
1244 state->render_states[WINED3D_RS_ADAPTIVETESS_X] = tmpfloat.d;
1245 state->render_states[WINED3D_RS_ADAPTIVETESS_Y] = tmpfloat.d;
1247 state->render_states[WINED3D_RS_ADAPTIVETESS_Z] = tmpfloat.d;
1249 state->render_states[WINED3D_RS_ADAPTIVETESS_W] = tmpfloat.d;
1250 state->render_states[WINED3D_RS_ENABLEADAPTIVETESSELLATION] = FALSE;
1251 state->render_states[WINED3D_RS_TWOSIDEDSTENCILMODE] = FALSE;
1252 state->render_states[WINED3D_RS_CCW_STENCILFAIL] = WINED3D_STENCIL_OP_KEEP;
1253 state->render_states[WINED3D_RS_CCW_STENCILZFAIL] = WINED3D_STENCIL_OP_KEEP;
1254 state->render_states[WINED3D_RS_CCW_STENCILPASS] = WINED3D_STENCIL_OP_KEEP;
1255 state->render_states[WINED3D_RS_CCW_STENCILFUNC] = WINED3D_CMP_ALWAYS;
1256 state->render_states[WINED3D_RS_COLORWRITEENABLE1] = 0x0000000f;
1257 state->render_states[WINED3D_RS_COLORWRITEENABLE2] = 0x0000000f;
1258 state->render_states[WINED3D_RS_COLORWRITEENABLE3] = 0x0000000f;
1259 state->render_states[WINED3D_RS_BLENDFACTOR] = 0xffffffff;
1260 state->render_states[WINED3D_RS_SRGBWRITEENABLE] = 0;
1261 state->render_states[WINED3D_RS_DEPTHBIAS] = 0;
1262 state->render_states[WINED3D_RS_WRAP8] = 0;
1263 state->render_states[WINED3D_RS_WRAP9] = 0;
1264 state->render_states[WINED3D_RS_WRAP10] = 0;
1265 state->render_states[WINED3D_RS_WRAP11] = 0;
1266 state->render_states[WINED3D_RS_WRAP12] = 0;
1267 state->render_states[WINED3D_RS_WRAP13] = 0;
1268 state->render_states[WINED3D_RS_WRAP14] = 0;
1269 state->render_states[WINED3D_RS_WRAP15] = 0;
1270 state->render_states[WINED3D_RS_SEPARATEALPHABLENDENABLE] = FALSE;
1271 state->render_states[WINED3D_RS_SRCBLENDALPHA] = WINED3D_BLEND_ONE;
1272 state->render_states[WINED3D_RS_DESTBLENDALPHA] = WINED3D_BLEND_ZERO;
1273 state->render_states[WINED3D_RS_BLENDOPALPHA] = WINED3D_BLEND_OP_ADD;
1275 /* Texture Stage States - Put directly into state block, we will call function below */
1276 for (i = 0; i < MAX_TEXTURES; ++i)
1278 TRACE("Setting up default texture states for texture Stage %u.\n", i);
1279 memcpy(&state->transforms[WINED3D_TS_TEXTURE0 + i], &identity, sizeof(identity));
1280 state->texture_states[i][WINED3D_TSS_COLOR_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_MODULATE;
1281 state->texture_states[i][WINED3D_TSS_COLOR_ARG1] = WINED3DTA_TEXTURE;
1282 state->texture_states[i][WINED3D_TSS_COLOR_ARG2] = WINED3DTA_CURRENT;
1283 state->texture_states[i][WINED3D_TSS_ALPHA_OP] = i ? WINED3D_TOP_DISABLE : WINED3D_TOP_SELECT_ARG1;
1284 state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] = WINED3DTA_TEXTURE;
1285 state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] = WINED3DTA_CURRENT;
1286 state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00] = 0;
1287 state->texture_states[i][WINED3D_TSS_BUMPENV_MAT01] = 0;
1288 state->texture_states[i][WINED3D_TSS_BUMPENV_MAT10] = 0;
1289 state->texture_states[i][WINED3D_TSS_BUMPENV_MAT11] = 0;
1290 state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] = i;
1291 state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE] = 0;
1292 state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET] = 0;
1293 state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS] = WINED3D_TTFF_DISABLE;
1294 state->texture_states[i][WINED3D_TSS_COLOR_ARG0] = WINED3DTA_CURRENT;
1295 state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] = WINED3DTA_CURRENT;
1296 state->texture_states[i][WINED3D_TSS_RESULT_ARG] = WINED3DTA_CURRENT;
1298 state->lowest_disabled_stage = 1;
1301 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; ++i)
1303 TRACE("Setting up default samplers states for sampler %u.\n", i);
1304 state->sampler_states[i][WINED3D_SAMP_ADDRESS_U] = WINED3D_TADDRESS_WRAP;
1305 state->sampler_states[i][WINED3D_SAMP_ADDRESS_V] = WINED3D_TADDRESS_WRAP;
1306 state->sampler_states[i][WINED3D_SAMP_ADDRESS_W] = WINED3D_TADDRESS_WRAP;
1307 state->sampler_states[i][WINED3D_SAMP_BORDER_COLOR] = 0;
1308 state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] = WINED3D_TEXF_POINT;
1309 state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] = WINED3D_TEXF_POINT;
1310 state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] = WINED3D_TEXF_NONE;
1311 state->sampler_states[i][WINED3D_SAMP_MIPMAP_LOD_BIAS] = 0;
1312 state->sampler_states[i][WINED3D_SAMP_MAX_MIP_LEVEL] = 0;
1313 state->sampler_states[i][WINED3D_SAMP_MAX_ANISOTROPY] = 1;
1314 state->sampler_states[i][WINED3D_SAMP_SRGB_TEXTURE] = 0;
1315 /* TODO: Indicates which element of a multielement texture to use. */
1316 state->sampler_states[i][WINED3D_SAMP_ELEMENT_INDEX] = 0;
1317 /* TODO: Vertex offset in the presampled displacement map. */
1318 state->sampler_states[i][WINED3D_SAMP_DMAP_OFFSET] = 0;
1321 for (i = 0; i < gl_info->limits.textures; ++i)
1323 state->textures[i] = NULL;
1326 /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1327 if ((swapchain = wined3d_device_get_swapchain(device, 0)))
1329 if ((backbuffer = wined3d_swapchain_get_back_buffer(swapchain, 0, WINED3D_BACKBUFFER_TYPE_MONO)))
1331 struct wined3d_resource_desc desc;
1333 wined3d_resource_get_desc(&backbuffer->resource, &desc);
1335 /* Set the default scissor rect values */
1336 state->scissor_rect.left = 0;
1337 state->scissor_rect.right = desc.width;
1338 state->scissor_rect.top = 0;
1339 state->scissor_rect.bottom = desc.height;
1342 /* Set the default viewport */
1343 state->viewport.x = 0;
1344 state->viewport.y = 0;
1345 state->viewport.width = swapchain->desc.backbuffer_width;
1346 state->viewport.height = swapchain->desc.backbuffer_height;
1347 state->viewport.min_z = 0.0f;
1348 state->viewport.max_z = 1.0f;
1354 static HRESULT stateblock_init(struct wined3d_stateblock *stateblock,
1355 struct wined3d_device *device, enum wined3d_stateblock_type type)
1360 stateblock->ref = 1;
1361 stateblock->device = device;
1363 for (i = 0; i < LIGHTMAP_SIZE; i++)
1365 list_init(&stateblock->state.light_map[i]);
1368 hr = stateblock_allocate_shader_constants(stateblock);
1369 if (FAILED(hr)) return hr;
1371 /* The WINED3D_SBT_INIT stateblock type is used during initialization to
1372 * produce a placeholder stateblock so other functions called can update a
1374 if (type == WINED3D_SBT_INIT || type == WINED3D_SBT_RECORDED) return WINED3D_OK;
1376 TRACE("Updating changed flags appropriate for type %#x.\n", type);
1380 case WINED3D_SBT_ALL:
1381 stateblock_init_lights(stateblock, device->stateBlock->state.light_map);
1382 stateblock_savedstates_set_all(&stateblock->changed, device->d3d_vshader_constantF,
1383 device->d3d_pshader_constantF);
1386 case WINED3D_SBT_PIXEL_STATE:
1387 stateblock_savedstates_set_pixel(&stateblock->changed, device->d3d_pshader_constantF);
1390 case WINED3D_SBT_VERTEX_STATE:
1391 stateblock_init_lights(stateblock, device->stateBlock->state.light_map);
1392 stateblock_savedstates_set_vertex(&stateblock->changed, device->d3d_vshader_constantF);
1396 FIXME("Unrecognized state block type %#x.\n", type);
1400 stateblock_init_contained_states(stateblock);
1401 wined3d_stateblock_capture(stateblock);
1406 HRESULT CDECL wined3d_stateblock_create(struct wined3d_device *device,
1407 enum wined3d_stateblock_type type, struct wined3d_stateblock **stateblock)
1409 struct wined3d_stateblock *object;
1412 TRACE("device %p, type %#x, stateblock %p.\n",
1413 device, type, stateblock);
1415 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1418 ERR("Failed to allocate stateblock memory.\n");
1419 return E_OUTOFMEMORY;
1422 hr = stateblock_init(object, device, type);
1425 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1426 HeapFree(GetProcessHeap(), 0, object);
1430 TRACE("Created stateblock %p.\n", object);
1431 *stateblock = object;