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 "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
30 static const DWORD pixel_states_render
[] =
32 WINED3DRS_ALPHABLENDENABLE
,
35 WINED3DRS_ALPHATESTENABLE
,
36 WINED3DRS_ANTIALIASEDLINEENABLE
,
37 WINED3DRS_BLENDFACTOR
,
39 WINED3DRS_BLENDOPALPHA
,
40 WINED3DRS_CCW_STENCILFAIL
,
41 WINED3DRS_CCW_STENCILPASS
,
42 WINED3DRS_CCW_STENCILZFAIL
,
43 WINED3DRS_COLORWRITEENABLE
,
44 WINED3DRS_COLORWRITEENABLE1
,
45 WINED3DRS_COLORWRITEENABLE2
,
46 WINED3DRS_COLORWRITEENABLE3
,
49 WINED3DRS_DESTBLENDALPHA
,
50 WINED3DRS_DITHERENABLE
,
56 WINED3DRS_SCISSORTESTENABLE
,
57 WINED3DRS_SEPARATEALPHABLENDENABLE
,
59 WINED3DRS_SLOPESCALEDEPTHBIAS
,
61 WINED3DRS_SRCBLENDALPHA
,
62 WINED3DRS_SRGBWRITEENABLE
,
63 WINED3DRS_STENCILENABLE
,
64 WINED3DRS_STENCILFAIL
,
65 WINED3DRS_STENCILFUNC
,
66 WINED3DRS_STENCILMASK
,
67 WINED3DRS_STENCILPASS
,
69 WINED3DRS_STENCILWRITEMASK
,
70 WINED3DRS_STENCILZFAIL
,
71 WINED3DRS_TEXTUREFACTOR
,
72 WINED3DRS_TWOSIDEDSTENCILMODE
,
91 WINED3DRS_ZWRITEENABLE
,
94 static const DWORD pixel_states_texture
[] =
100 WINED3DTSS_BUMPENVLOFFSET
,
101 WINED3DTSS_BUMPENVLSCALE
,
102 WINED3DTSS_BUMPENVMAT00
,
103 WINED3DTSS_BUMPENVMAT01
,
104 WINED3DTSS_BUMPENVMAT10
,
105 WINED3DTSS_BUMPENVMAT11
,
106 WINED3DTSS_COLORARG0
,
107 WINED3DTSS_COLORARG1
,
108 WINED3DTSS_COLORARG2
,
110 WINED3DTSS_RESULTARG
,
111 WINED3DTSS_TEXCOORDINDEX
,
112 WINED3DTSS_TEXTURETRANSFORMFLAGS
,
115 static const DWORD pixel_states_sampler
[] =
117 WINED3DSAMP_ADDRESSU
,
118 WINED3DSAMP_ADDRESSV
,
119 WINED3DSAMP_ADDRESSW
,
120 WINED3DSAMP_BORDERCOLOR
,
121 WINED3DSAMP_MAGFILTER
,
122 WINED3DSAMP_MINFILTER
,
123 WINED3DSAMP_MIPFILTER
,
124 WINED3DSAMP_MIPMAPLODBIAS
,
125 WINED3DSAMP_MAXMIPLEVEL
,
126 WINED3DSAMP_MAXANISOTROPY
,
127 WINED3DSAMP_SRGBTEXTURE
,
128 WINED3DSAMP_ELEMENTINDEX
,
131 static const DWORD vertex_states_render
[] =
133 WINED3DRS_ADAPTIVETESS_W
,
134 WINED3DRS_ADAPTIVETESS_X
,
135 WINED3DRS_ADAPTIVETESS_Y
,
136 WINED3DRS_ADAPTIVETESS_Z
,
138 WINED3DRS_AMBIENTMATERIALSOURCE
,
140 WINED3DRS_CLIPPLANEENABLE
,
141 WINED3DRS_COLORVERTEX
,
143 WINED3DRS_DIFFUSEMATERIALSOURCE
,
144 WINED3DRS_EMISSIVEMATERIALSOURCE
,
145 WINED3DRS_ENABLEADAPTIVETESSELLATION
,
147 WINED3DRS_FOGDENSITY
,
151 WINED3DRS_FOGTABLEMODE
,
152 WINED3DRS_FOGVERTEXMODE
,
153 WINED3DRS_INDEXEDVERTEXBLENDENABLE
,
155 WINED3DRS_LOCALVIEWER
,
156 WINED3DRS_MAXTESSELLATIONLEVEL
,
157 WINED3DRS_MINTESSELLATIONLEVEL
,
158 WINED3DRS_MULTISAMPLEANTIALIAS
,
159 WINED3DRS_MULTISAMPLEMASK
,
160 WINED3DRS_NORMALDEGREE
,
161 WINED3DRS_NORMALIZENORMALS
,
162 WINED3DRS_PATCHEDGESTYLE
,
163 WINED3DRS_POINTSCALE_A
,
164 WINED3DRS_POINTSCALE_B
,
165 WINED3DRS_POINTSCALE_C
,
166 WINED3DRS_POINTSCALEENABLE
,
168 WINED3DRS_POINTSIZE_MAX
,
169 WINED3DRS_POINTSIZE_MIN
,
170 WINED3DRS_POINTSPRITEENABLE
,
171 WINED3DRS_POSITIONDEGREE
,
172 WINED3DRS_RANGEFOGENABLE
,
174 WINED3DRS_SPECULARENABLE
,
175 WINED3DRS_SPECULARMATERIALSOURCE
,
176 WINED3DRS_TWEENFACTOR
,
177 WINED3DRS_VERTEXBLEND
,
180 static const DWORD vertex_states_texture
[] =
182 WINED3DTSS_TEXCOORDINDEX
,
183 WINED3DTSS_TEXTURETRANSFORMFLAGS
,
186 static const DWORD vertex_states_sampler
[] =
188 WINED3DSAMP_DMAPOFFSET
,
191 /* Allocates the correct amount of space for pixel and vertex shader constants,
192 * along with their set/changed flags on the given stateblock object
194 static HRESULT
stateblock_allocate_shader_constants(IWineD3DStateBlockImpl
*object
)
196 IWineD3DDeviceImpl
*device
= object
->device
;
198 /* Allocate space for floating point constants */
199 object
->pixelShaderConstantF
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
200 sizeof(float) * device
->d3d_pshader_constantF
* 4);
201 if (!object
->pixelShaderConstantF
) goto fail
;
203 object
->changed
.pixelShaderConstantsF
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
204 sizeof(BOOL
) * device
->d3d_pshader_constantF
);
205 if (!object
->changed
.pixelShaderConstantsF
) goto fail
;
207 object
->vertexShaderConstantF
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
208 sizeof(float) * device
->d3d_vshader_constantF
* 4);
209 if (!object
->vertexShaderConstantF
) goto fail
;
211 object
->changed
.vertexShaderConstantsF
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
212 sizeof(BOOL
) * device
->d3d_vshader_constantF
);
213 if (!object
->changed
.vertexShaderConstantsF
) goto fail
;
215 object
->contained_vs_consts_f
= HeapAlloc(GetProcessHeap(), 0,
216 sizeof(DWORD
) * device
->d3d_vshader_constantF
);
217 if (!object
->contained_vs_consts_f
) goto fail
;
219 object
->contained_ps_consts_f
= HeapAlloc(GetProcessHeap(), 0,
220 sizeof(DWORD
) * device
->d3d_pshader_constantF
);
221 if (!object
->contained_ps_consts_f
) goto fail
;
226 ERR("Failed to allocate memory\n");
227 HeapFree(GetProcessHeap(), 0, object
->pixelShaderConstantF
);
228 HeapFree(GetProcessHeap(), 0, object
->changed
.pixelShaderConstantsF
);
229 HeapFree(GetProcessHeap(), 0, object
->vertexShaderConstantF
);
230 HeapFree(GetProcessHeap(), 0, object
->changed
.vertexShaderConstantsF
);
231 HeapFree(GetProcessHeap(), 0, object
->contained_vs_consts_f
);
232 HeapFree(GetProcessHeap(), 0, object
->contained_ps_consts_f
);
233 return E_OUTOFMEMORY
;
236 static inline void stateblock_set_bits(DWORD
*map
, UINT map_size
)
238 DWORD mask
= (1 << (map_size
& 0x1f)) - 1;
239 memset(map
, 0xff, (map_size
>> 5) * sizeof(*map
));
240 if (mask
) map
[map_size
>> 5] = mask
;
243 /* Set all members of a stateblock savedstate to the given value */
244 static void stateblock_savedstates_set_all(SAVEDSTATES
*states
, DWORD vs_consts
, DWORD ps_consts
)
249 states
->primitive_type
= 1;
251 states
->material
= 1;
252 states
->viewport
= 1;
253 states
->vertexDecl
= 1;
254 states
->pixelShader
= 1;
255 states
->vertexShader
= 1;
256 states
->scissorRect
= 1;
258 /* Fixed size arrays */
259 states
->streamSource
= 0xffff;
260 states
->streamFreq
= 0xffff;
261 states
->textures
= 0xfffff;
262 stateblock_set_bits(states
->transform
, HIGHEST_TRANSFORMSTATE
+ 1);
263 stateblock_set_bits(states
->renderState
, WINEHIGHEST_RENDER_STATE
+ 1);
264 for (i
= 0; i
< MAX_TEXTURES
; ++i
) states
->textureState
[i
] = 0x3ffff;
265 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
) states
->samplerState
[i
] = 0x3ffe;
266 states
->clipplane
= 0xffffffff;
267 states
->pixelShaderConstantsB
= 0xffff;
268 states
->pixelShaderConstantsI
= 0xffff;
269 states
->vertexShaderConstantsB
= 0xffff;
270 states
->vertexShaderConstantsI
= 0xffff;
272 /* Dynamically sized arrays */
273 memset(states
->pixelShaderConstantsF
, TRUE
, sizeof(BOOL
) * ps_consts
);
274 memset(states
->vertexShaderConstantsF
, TRUE
, sizeof(BOOL
) * vs_consts
);
277 static void stateblock_savedstates_set_pixel(SAVEDSTATES
*states
, const DWORD num_constants
)
279 DWORD texture_mask
= 0;
280 WORD sampler_mask
= 0;
283 states
->pixelShader
= 1;
285 for (i
= 0; i
< sizeof(pixel_states_render
) / sizeof(*pixel_states_render
); ++i
)
287 DWORD rs
= pixel_states_render
[i
];
288 states
->renderState
[rs
>> 5] |= 1 << (rs
& 0x1f);
291 for (i
= 0; i
< sizeof(pixel_states_texture
) / sizeof(*pixel_states_texture
); ++i
)
292 texture_mask
|= 1 << pixel_states_texture
[i
];
293 for (i
= 0; i
< MAX_TEXTURES
; ++i
) states
->textureState
[i
] = texture_mask
;
294 for (i
= 0; i
< sizeof(pixel_states_sampler
) / sizeof(*pixel_states_sampler
); ++i
)
295 sampler_mask
|= 1 << pixel_states_sampler
[i
];
296 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
) states
->samplerState
[i
] = sampler_mask
;
297 states
->pixelShaderConstantsB
= 0xffff;
298 states
->pixelShaderConstantsI
= 0xffff;
300 memset(states
->pixelShaderConstantsF
, TRUE
, sizeof(BOOL
) * num_constants
);
303 static void stateblock_savedstates_set_vertex(SAVEDSTATES
*states
, const DWORD num_constants
)
305 DWORD texture_mask
= 0;
306 WORD sampler_mask
= 0;
309 states
->vertexDecl
= 1;
310 states
->vertexShader
= 1;
312 for (i
= 0; i
< sizeof(vertex_states_render
) / sizeof(*vertex_states_render
); ++i
)
314 DWORD rs
= vertex_states_render
[i
];
315 states
->renderState
[rs
>> 5] |= 1 << (rs
& 0x1f);
318 for (i
= 0; i
< sizeof(vertex_states_texture
) / sizeof(*vertex_states_texture
); ++i
)
319 texture_mask
|= 1 << vertex_states_texture
[i
];
320 for (i
= 0; i
< MAX_TEXTURES
; ++i
) states
->textureState
[i
] = texture_mask
;
321 for (i
= 0; i
< sizeof(vertex_states_sampler
) / sizeof(*vertex_states_sampler
); ++i
)
322 sampler_mask
|= 1 << vertex_states_sampler
[i
];
323 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
) states
->samplerState
[i
] = sampler_mask
;
324 states
->vertexShaderConstantsB
= 0xffff;
325 states
->vertexShaderConstantsI
= 0xffff;
327 memset(states
->vertexShaderConstantsF
, TRUE
, sizeof(BOOL
) * num_constants
);
330 void stateblock_init_contained_states(IWineD3DStateBlockImpl
*stateblock
)
332 IWineD3DDeviceImpl
*device
= stateblock
->device
;
335 for (i
= 0; i
<= WINEHIGHEST_RENDER_STATE
>> 5; ++i
)
337 DWORD map
= stateblock
->changed
.renderState
[i
];
338 for (j
= 0; map
; map
>>= 1, ++j
)
340 if (!(map
& 1)) continue;
342 stateblock
->contained_render_states
[stateblock
->num_contained_render_states
] = (i
<< 5) | j
;
343 ++stateblock
->num_contained_render_states
;
347 for (i
= 0; i
<= HIGHEST_TRANSFORMSTATE
>> 5; ++i
)
349 DWORD map
= stateblock
->changed
.transform
[i
];
350 for (j
= 0; map
; map
>>= 1, ++j
)
352 if (!(map
& 1)) continue;
354 stateblock
->contained_transform_states
[stateblock
->num_contained_transform_states
] = (i
<< 5) | j
;
355 ++stateblock
->num_contained_transform_states
;
359 for (i
= 0; i
< device
->d3d_vshader_constantF
; ++i
)
361 if (stateblock
->changed
.vertexShaderConstantsF
[i
])
363 stateblock
->contained_vs_consts_f
[stateblock
->num_contained_vs_consts_f
] = i
;
364 ++stateblock
->num_contained_vs_consts_f
;
368 for (i
= 0; i
< MAX_CONST_I
; ++i
)
370 if (stateblock
->changed
.vertexShaderConstantsI
& (1 << i
))
372 stateblock
->contained_vs_consts_i
[stateblock
->num_contained_vs_consts_i
] = i
;
373 ++stateblock
->num_contained_vs_consts_i
;
377 for (i
= 0; i
< MAX_CONST_B
; ++i
)
379 if (stateblock
->changed
.vertexShaderConstantsB
& (1 << i
))
381 stateblock
->contained_vs_consts_b
[stateblock
->num_contained_vs_consts_b
] = i
;
382 ++stateblock
->num_contained_vs_consts_b
;
386 for (i
= 0; i
< device
->d3d_pshader_constantF
; ++i
)
388 if (stateblock
->changed
.pixelShaderConstantsF
[i
])
390 stateblock
->contained_ps_consts_f
[stateblock
->num_contained_ps_consts_f
] = i
;
391 ++stateblock
->num_contained_ps_consts_f
;
395 for (i
= 0; i
< MAX_CONST_I
; ++i
)
397 if (stateblock
->changed
.pixelShaderConstantsI
& (1 << i
))
399 stateblock
->contained_ps_consts_i
[stateblock
->num_contained_ps_consts_i
] = i
;
400 ++stateblock
->num_contained_ps_consts_i
;
404 for (i
= 0; i
< MAX_CONST_B
; ++i
)
406 if (stateblock
->changed
.pixelShaderConstantsB
& (1 << i
))
408 stateblock
->contained_ps_consts_b
[stateblock
->num_contained_ps_consts_b
] = i
;
409 ++stateblock
->num_contained_ps_consts_b
;
413 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
415 DWORD map
= stateblock
->changed
.textureState
[i
];
417 for(j
= 0; map
; map
>>= 1, ++j
)
419 if (!(map
& 1)) continue;
421 stateblock
->contained_tss_states
[stateblock
->num_contained_tss_states
].stage
= i
;
422 stateblock
->contained_tss_states
[stateblock
->num_contained_tss_states
].state
= j
;
423 ++stateblock
->num_contained_tss_states
;
427 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
429 DWORD map
= stateblock
->changed
.samplerState
[i
];
431 for (j
= 0; map
; map
>>= 1, ++j
)
433 if (!(map
& 1)) continue;
435 stateblock
->contained_sampler_states
[stateblock
->num_contained_sampler_states
].stage
= i
;
436 stateblock
->contained_sampler_states
[stateblock
->num_contained_sampler_states
].state
= j
;
437 ++stateblock
->num_contained_sampler_states
;
442 static void stateblock_init_lights(IWineD3DStateBlockImpl
*stateblock
, struct list
*light_map
)
446 for (i
= 0; i
< LIGHTMAP_SIZE
; ++i
)
448 const struct wined3d_light_info
*src_light
;
450 LIST_FOR_EACH_ENTRY(src_light
, &light_map
[i
], struct wined3d_light_info
, entry
)
452 struct wined3d_light_info
*dst_light
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_light
));
454 *dst_light
= *src_light
;
455 list_add_tail(&stateblock
->lightMap
[i
], &dst_light
->entry
);
460 /**********************************************************
461 * IWineD3DStateBlockImpl IUnknown parts follows
462 **********************************************************/
463 static HRESULT WINAPI
IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock
*iface
,REFIID riid
,LPVOID
*ppobj
)
465 IWineD3DStateBlockImpl
*This
= (IWineD3DStateBlockImpl
*)iface
;
466 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
467 if (IsEqualGUID(riid
, &IID_IUnknown
)
468 || IsEqualGUID(riid
, &IID_IWineD3DStateBlock
))
470 IUnknown_AddRef(iface
);
475 return E_NOINTERFACE
;
478 static ULONG WINAPI
IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock
*iface
) {
479 IWineD3DStateBlockImpl
*This
= (IWineD3DStateBlockImpl
*)iface
;
480 ULONG refCount
= InterlockedIncrement(&This
->ref
);
482 TRACE("(%p) : AddRef increasing from %d\n", This
, refCount
- 1);
486 static ULONG WINAPI
IWineD3DStateBlockImpl_Release(IWineD3DStateBlock
*iface
) {
487 IWineD3DStateBlockImpl
*This
= (IWineD3DStateBlockImpl
*)iface
;
488 ULONG refCount
= InterlockedDecrement(&This
->ref
);
490 TRACE("(%p) : Releasing from %d\n", This
, refCount
+ 1);
495 if (This
->vertexDecl
) IWineD3DVertexDeclaration_Release(This
->vertexDecl
);
497 for (counter
= 0; counter
< MAX_COMBINED_SAMPLERS
; counter
++)
499 if (This
->textures
[counter
]) IWineD3DBaseTexture_Release(This
->textures
[counter
]);
502 for (counter
= 0; counter
< MAX_STREAMS
; ++counter
)
504 struct wined3d_buffer
*buffer
= This
->streams
[counter
].buffer
;
507 if (IWineD3DBuffer_Release((IWineD3DBuffer
*)buffer
))
509 WARN("Buffer %p still referenced by stateblock, stream %u.\n", buffer
, counter
);
513 if(This
->pIndexData
) IWineD3DBuffer_Release(This
->pIndexData
);
514 if(This
->vertexShader
) IWineD3DVertexShader_Release(This
->vertexShader
);
515 if(This
->pixelShader
) IWineD3DPixelShader_Release(This
->pixelShader
);
517 for(counter
= 0; counter
< LIGHTMAP_SIZE
; counter
++) {
518 struct list
*e1
, *e2
;
519 LIST_FOR_EACH_SAFE(e1
, e2
, &This
->lightMap
[counter
])
521 struct wined3d_light_info
*light
= LIST_ENTRY(e1
, struct wined3d_light_info
, entry
);
522 list_remove(&light
->entry
);
523 HeapFree(GetProcessHeap(), 0, light
);
527 HeapFree(GetProcessHeap(), 0, This
->vertexShaderConstantF
);
528 HeapFree(GetProcessHeap(), 0, This
->changed
.vertexShaderConstantsF
);
529 HeapFree(GetProcessHeap(), 0, This
->pixelShaderConstantF
);
530 HeapFree(GetProcessHeap(), 0, This
->changed
.pixelShaderConstantsF
);
531 HeapFree(GetProcessHeap(), 0, This
->contained_vs_consts_f
);
532 HeapFree(GetProcessHeap(), 0, This
->contained_ps_consts_f
);
533 HeapFree(GetProcessHeap(), 0, This
);
538 /**********************************************************
539 * IWineD3DStateBlockImpl parts follows
540 **********************************************************/
541 static void record_lights(IWineD3DStateBlockImpl
*This
, const IWineD3DStateBlockImpl
*targetStateBlock
)
545 /* Lights... For a recorded state block, we just had a chain of actions to perform,
546 * so we need to walk that chain and update any actions which differ
548 for(i
= 0; i
< LIGHTMAP_SIZE
; i
++) {
550 LIST_FOR_EACH(e
, &This
->lightMap
[i
]) {
551 BOOL updated
= FALSE
;
552 struct wined3d_light_info
*src
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
), *realLight
;
554 /* Look up the light in the destination */
555 LIST_FOR_EACH(f
, &targetStateBlock
->lightMap
[i
]) {
556 realLight
= LIST_ENTRY(f
, struct wined3d_light_info
, entry
);
557 if (realLight
->OriginalIndex
== src
->OriginalIndex
)
559 src
->OriginalParms
= realLight
->OriginalParms
;
561 if (realLight
->glIndex
== -1 && src
->glIndex
!= -1)
564 This
->activeLights
[src
->glIndex
] = NULL
;
566 else if (realLight
->glIndex
!= -1 && src
->glIndex
== -1)
569 This
->activeLights
[realLight
->glIndex
] = src
;
571 src
->glIndex
= realLight
->glIndex
;
579 /* This can happen if the light was originally created as a
580 * default light for SetLightEnable() while recording. */
581 WARN("Light %u in stateblock %p does not exist in device stateblock %p.\n",
582 src
->OriginalIndex
, This
, targetStateBlock
);
584 src
->OriginalParms
= WINED3D_default_light
;
585 if (src
->glIndex
!= -1)
587 This
->activeLights
[src
->glIndex
] = NULL
;
595 static HRESULT WINAPI
IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock
*iface
)
597 IWineD3DStateBlockImpl
*This
= (IWineD3DStateBlockImpl
*)iface
;
598 IWineD3DStateBlockImpl
*targetStateBlock
= This
->device
->stateBlock
;
602 TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock
, This
);
604 if (This
->changed
.vertexShader
&& This
->vertexShader
!= targetStateBlock
->vertexShader
)
606 TRACE("Updating vertex shader from %p to %p\n", This
->vertexShader
, targetStateBlock
->vertexShader
);
608 if (targetStateBlock
->vertexShader
) IWineD3DVertexShader_AddRef(targetStateBlock
->vertexShader
);
609 if (This
->vertexShader
) IWineD3DVertexShader_Release(This
->vertexShader
);
610 This
->vertexShader
= targetStateBlock
->vertexShader
;
613 /* Vertex Shader Float Constants */
614 for (i
= 0; i
< This
->num_contained_vs_consts_f
; ++i
)
616 unsigned int idx
= This
->contained_vs_consts_f
[i
];
618 TRACE("Setting %p from %p %u to {%.8e, %.8e, %.8e, %.8e}.\n",
619 This
, targetStateBlock
, idx
,
620 targetStateBlock
->vertexShaderConstantF
[idx
* 4 + 0],
621 targetStateBlock
->vertexShaderConstantF
[idx
* 4 + 1],
622 targetStateBlock
->vertexShaderConstantF
[idx
* 4 + 2],
623 targetStateBlock
->vertexShaderConstantF
[idx
* 4 + 3]);
625 This
->vertexShaderConstantF
[idx
* 4 + 0] = targetStateBlock
->vertexShaderConstantF
[idx
* 4 + 0];
626 This
->vertexShaderConstantF
[idx
* 4 + 1] = targetStateBlock
->vertexShaderConstantF
[idx
* 4 + 1];
627 This
->vertexShaderConstantF
[idx
* 4 + 2] = targetStateBlock
->vertexShaderConstantF
[idx
* 4 + 2];
628 This
->vertexShaderConstantF
[idx
* 4 + 3] = targetStateBlock
->vertexShaderConstantF
[idx
* 4 + 3];
631 /* Vertex Shader Integer Constants */
632 for (i
= 0; i
< This
->num_contained_vs_consts_i
; ++i
)
634 unsigned int idx
= This
->contained_vs_consts_i
[i
];
636 TRACE("Setting %p from %p %u to {%d, %d, %d, %d}.\n",
637 This
, targetStateBlock
, idx
,
638 targetStateBlock
->vertexShaderConstantI
[idx
* 4 + 0],
639 targetStateBlock
->vertexShaderConstantI
[idx
* 4 + 1],
640 targetStateBlock
->vertexShaderConstantI
[idx
* 4 + 2],
641 targetStateBlock
->vertexShaderConstantI
[idx
* 4 + 3]);
643 This
->vertexShaderConstantI
[idx
* 4 + 0] = targetStateBlock
->vertexShaderConstantI
[idx
* 4 + 0];
644 This
->vertexShaderConstantI
[idx
* 4 + 1] = targetStateBlock
->vertexShaderConstantI
[idx
* 4 + 1];
645 This
->vertexShaderConstantI
[idx
* 4 + 2] = targetStateBlock
->vertexShaderConstantI
[idx
* 4 + 2];
646 This
->vertexShaderConstantI
[idx
* 4 + 3] = targetStateBlock
->vertexShaderConstantI
[idx
* 4 + 3];
649 /* Vertex Shader Boolean Constants */
650 for (i
= 0; i
< This
->num_contained_vs_consts_b
; ++i
)
652 unsigned int idx
= This
->contained_vs_consts_b
[i
];
654 TRACE("Setting %p from %p %u to %s.\n",
655 This
, targetStateBlock
, idx
,
656 targetStateBlock
->vertexShaderConstantB
[idx
] ? "TRUE" : "FALSE");
658 This
->vertexShaderConstantB
[idx
] = targetStateBlock
->vertexShaderConstantB
[idx
];
661 /* Pixel Shader Float Constants */
662 for (i
= 0; i
< This
->num_contained_ps_consts_f
; ++i
)
664 unsigned int idx
= This
->contained_ps_consts_f
[i
];
666 TRACE("Setting %p from %p %u to {%.8e, %.8e, %.8e, %.8e}.\n",
667 This
, targetStateBlock
, idx
,
668 targetStateBlock
->pixelShaderConstantF
[idx
* 4 + 0],
669 targetStateBlock
->pixelShaderConstantF
[idx
* 4 + 1],
670 targetStateBlock
->pixelShaderConstantF
[idx
* 4 + 2],
671 targetStateBlock
->pixelShaderConstantF
[idx
* 4 + 3]);
673 This
->pixelShaderConstantF
[idx
* 4 + 0] = targetStateBlock
->pixelShaderConstantF
[idx
* 4 + 0];
674 This
->pixelShaderConstantF
[idx
* 4 + 1] = targetStateBlock
->pixelShaderConstantF
[idx
* 4 + 1];
675 This
->pixelShaderConstantF
[idx
* 4 + 2] = targetStateBlock
->pixelShaderConstantF
[idx
* 4 + 2];
676 This
->pixelShaderConstantF
[idx
* 4 + 3] = targetStateBlock
->pixelShaderConstantF
[idx
* 4 + 3];
679 /* Pixel Shader Integer Constants */
680 for (i
= 0; i
< This
->num_contained_ps_consts_i
; ++i
)
682 unsigned int idx
= This
->contained_ps_consts_i
[i
];
683 TRACE("Setting %p from %p %u to {%d, %d, %d, %d}.\n",
684 This
, targetStateBlock
, idx
,
685 targetStateBlock
->pixelShaderConstantI
[idx
* 4 + 0],
686 targetStateBlock
->pixelShaderConstantI
[idx
* 4 + 1],
687 targetStateBlock
->pixelShaderConstantI
[idx
* 4 + 2],
688 targetStateBlock
->pixelShaderConstantI
[idx
* 4 + 3]);
690 This
->pixelShaderConstantI
[idx
* 4 + 0] = targetStateBlock
->pixelShaderConstantI
[idx
* 4 + 0];
691 This
->pixelShaderConstantI
[idx
* 4 + 1] = targetStateBlock
->pixelShaderConstantI
[idx
* 4 + 1];
692 This
->pixelShaderConstantI
[idx
* 4 + 2] = targetStateBlock
->pixelShaderConstantI
[idx
* 4 + 2];
693 This
->pixelShaderConstantI
[idx
* 4 + 3] = targetStateBlock
->pixelShaderConstantI
[idx
* 4 + 3];
696 /* Pixel Shader Boolean Constants */
697 for (i
= 0; i
< This
->num_contained_ps_consts_b
; ++i
)
699 unsigned int idx
= This
->contained_ps_consts_b
[i
];
700 TRACE("Setting %p from %p %u to %s.\n", This
, targetStateBlock
, idx
,
701 targetStateBlock
->pixelShaderConstantB
[idx
] ? "TRUE" : "FALSE");
703 This
->pixelShaderConstantB
[idx
] = targetStateBlock
->pixelShaderConstantB
[idx
];
706 /* Others + Render & Texture */
707 for (i
= 0; i
< This
->num_contained_transform_states
; ++i
)
709 WINED3DTRANSFORMSTATETYPE transform
= This
->contained_transform_states
[i
];
711 TRACE("Updating transform %#x.\n", transform
);
713 This
->state
.transforms
[transform
] = targetStateBlock
->state
.transforms
[transform
];
716 if (This
->changed
.primitive_type
) This
->gl_primitive_type
= targetStateBlock
->gl_primitive_type
;
718 if (This
->changed
.indices
719 && ((This
->pIndexData
!= targetStateBlock
->pIndexData
)
720 || (This
->baseVertexIndex
!= targetStateBlock
->baseVertexIndex
)
721 || (This
->IndexFmt
!= targetStateBlock
->IndexFmt
)))
723 TRACE("Updating pIndexData to %p, baseVertexIndex to %d.\n",
724 targetStateBlock
->pIndexData
, targetStateBlock
->baseVertexIndex
);
726 if (targetStateBlock
->pIndexData
) IWineD3DBuffer_AddRef(targetStateBlock
->pIndexData
);
727 if (This
->pIndexData
) IWineD3DBuffer_Release(This
->pIndexData
);
728 This
->pIndexData
= targetStateBlock
->pIndexData
;
729 This
->baseVertexIndex
= targetStateBlock
->baseVertexIndex
;
730 This
->IndexFmt
= targetStateBlock
->IndexFmt
;
733 if (This
->changed
.vertexDecl
&& This
->vertexDecl
!= targetStateBlock
->vertexDecl
)
735 TRACE("Updating vertex declaration from %p to %p.\n", This
->vertexDecl
, targetStateBlock
->vertexDecl
);
737 if (targetStateBlock
->vertexDecl
) IWineD3DVertexDeclaration_AddRef(targetStateBlock
->vertexDecl
);
738 if (This
->vertexDecl
) IWineD3DVertexDeclaration_Release(This
->vertexDecl
);
739 This
->vertexDecl
= targetStateBlock
->vertexDecl
;
742 if (This
->changed
.material
&& memcmp(&targetStateBlock
->state
.material
,
743 &This
->state
.material
, sizeof(This
->state
.material
)))
745 TRACE("Updating material.\n");
747 This
->state
.material
= targetStateBlock
->state
.material
;
750 if (This
->changed
.viewport
&& memcmp(&targetStateBlock
->state
.viewport
,
751 &This
->state
.viewport
, sizeof(This
->state
.viewport
)))
753 TRACE("Updating viewport.\n");
755 This
->state
.viewport
= targetStateBlock
->state
.viewport
;
758 if (This
->changed
.scissorRect
&& memcmp(&targetStateBlock
->state
.scissor_rect
,
759 &This
->state
.scissor_rect
, sizeof(This
->state
.scissor_rect
)))
761 TRACE("Updating scissor rect.\n");
763 This
->state
.scissor_rect
= targetStateBlock
->state
.scissor_rect
;
766 map
= This
->changed
.streamSource
;
767 for (i
= 0; map
; map
>>= 1, ++i
)
769 if (!(map
& 1)) continue;
771 if (This
->streams
[i
].stride
!= targetStateBlock
->streams
[i
].stride
772 || This
->streams
[i
].buffer
!= targetStateBlock
->streams
[i
].buffer
)
774 TRACE("Updating stream source %u to %p, stride to %u.\n",
775 i
, targetStateBlock
->streams
[i
].buffer
, targetStateBlock
->streams
[i
].stride
);
777 This
->streams
[i
].stride
= targetStateBlock
->streams
[i
].stride
;
778 if (targetStateBlock
->streams
[i
].buffer
)
779 IWineD3DBuffer_AddRef((IWineD3DBuffer
*)targetStateBlock
->streams
[i
].buffer
);
780 if (This
->streams
[i
].buffer
)
781 IWineD3DBuffer_Release((IWineD3DBuffer
*)This
->streams
[i
].buffer
);
782 This
->streams
[i
].buffer
= targetStateBlock
->streams
[i
].buffer
;
786 map
= This
->changed
.streamFreq
;
787 for (i
= 0; map
; map
>>= 1, ++i
)
789 if (!(map
& 1)) continue;
791 if (This
->streams
[i
].frequency
!= targetStateBlock
->streams
[i
].frequency
792 || This
->streams
[i
].flags
!= targetStateBlock
->streams
[i
].flags
)
794 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
795 i
, targetStateBlock
->streams
[i
].frequency
, targetStateBlock
->streams
[i
].flags
);
797 This
->streams
[i
].frequency
= targetStateBlock
->streams
[i
].frequency
;
798 This
->streams
[i
].flags
= targetStateBlock
->streams
[i
].flags
;
802 map
= This
->changed
.clipplane
;
803 for (i
= 0; map
; map
>>= 1, ++i
)
805 if (!(map
& 1)) continue;
807 if (memcmp(targetStateBlock
->clipplane
[i
], This
->clipplane
[i
], sizeof(*This
->clipplane
)))
809 TRACE("Updating clipplane %u.\n", i
);
810 memcpy(This
->clipplane
[i
], targetStateBlock
->clipplane
[i
], sizeof(*This
->clipplane
));
815 for (i
= 0; i
< This
->num_contained_render_states
; ++i
)
817 WINED3DRENDERSTATETYPE rs
= This
->contained_render_states
[i
];
819 TRACE("Updating render state %#x to %u.\n", rs
, targetStateBlock
->state
.render_states
[rs
]);
821 This
->state
.render_states
[rs
] = targetStateBlock
->state
.render_states
[rs
];
825 for (i
= 0; i
< This
->num_contained_tss_states
; ++i
)
827 DWORD stage
= This
->contained_tss_states
[i
].stage
;
828 DWORD state
= This
->contained_tss_states
[i
].state
;
830 TRACE("Updating texturestage state %u, %u to %#x (was %#x).\n", stage
, state
,
831 targetStateBlock
->state
.texture_states
[stage
][state
],
832 This
->state
.texture_states
[stage
][state
]);
834 This
->state
.texture_states
[stage
][state
] = targetStateBlock
->state
.texture_states
[stage
][state
];
838 map
= This
->changed
.textures
;
839 for (i
= 0; map
; map
>>= 1, ++i
)
841 if (!(map
& 1)) continue;
843 TRACE("Updating texture %u to %p (was %p).\n", i
, targetStateBlock
->textures
[i
], This
->textures
[i
]);
845 if (targetStateBlock
->textures
[i
]) IWineD3DBaseTexture_AddRef(targetStateBlock
->textures
[i
]);
846 if (This
->textures
[i
]) IWineD3DBaseTexture_Release(This
->textures
[i
]);
847 This
->textures
[i
] = targetStateBlock
->textures
[i
];
850 for (i
= 0; i
< This
->num_contained_sampler_states
; ++i
)
852 DWORD stage
= This
->contained_sampler_states
[i
].stage
;
853 DWORD state
= This
->contained_sampler_states
[i
].state
;
855 TRACE("Updating sampler state %u, %u to %#x (was %#x).\n", stage
, state
,
856 targetStateBlock
->state
.sampler_states
[stage
][state
],
857 This
->state
.sampler_states
[stage
][state
]);
859 This
->state
.sampler_states
[stage
][state
] = targetStateBlock
->state
.sampler_states
[stage
][state
];
862 if (This
->changed
.pixelShader
&& This
->pixelShader
!= targetStateBlock
->pixelShader
)
864 if (targetStateBlock
->pixelShader
) IWineD3DPixelShader_AddRef(targetStateBlock
->pixelShader
);
865 if (This
->pixelShader
) IWineD3DPixelShader_Release(This
->pixelShader
);
866 This
->pixelShader
= targetStateBlock
->pixelShader
;
869 record_lights(This
, targetStateBlock
);
871 TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock
, This
);
876 static void apply_lights(IWineD3DDevice
*device
, const IWineD3DStateBlockImpl
*This
)
879 for(i
= 0; i
< LIGHTMAP_SIZE
; i
++) {
882 LIST_FOR_EACH(e
, &This
->lightMap
[i
])
884 const struct wined3d_light_info
*light
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
886 IWineD3DDevice_SetLight(device
, light
->OriginalIndex
, &light
->OriginalParms
);
887 IWineD3DDevice_SetLightEnable(device
, light
->OriginalIndex
, light
->glIndex
!= -1);
892 static HRESULT WINAPI
IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock
*iface
)
894 IWineD3DStateBlockImpl
*This
= (IWineD3DStateBlockImpl
*)iface
;
895 IWineD3DDevice
*device
= (IWineD3DDevice
*)This
->device
;
899 TRACE("(%p) : Applying state block %p ------------------v\n", This
, device
);
901 TRACE("Blocktype: %d\n", This
->blockType
);
903 if (This
->changed
.vertexShader
) IWineD3DDevice_SetVertexShader(device
, This
->vertexShader
);
905 /* Vertex Shader Constants */
906 for (i
= 0; i
< This
->num_contained_vs_consts_f
; ++i
)
908 IWineD3DDevice_SetVertexShaderConstantF(device
, This
->contained_vs_consts_f
[i
],
909 This
->vertexShaderConstantF
+ This
->contained_vs_consts_f
[i
] * 4, 1);
911 for (i
= 0; i
< This
->num_contained_vs_consts_i
; ++i
)
913 IWineD3DDevice_SetVertexShaderConstantI(device
, This
->contained_vs_consts_i
[i
],
914 This
->vertexShaderConstantI
+ This
->contained_vs_consts_i
[i
] * 4, 1);
916 for (i
= 0; i
< This
->num_contained_vs_consts_b
; ++i
)
918 IWineD3DDevice_SetVertexShaderConstantB(device
, This
->contained_vs_consts_b
[i
],
919 This
->vertexShaderConstantB
+ This
->contained_vs_consts_b
[i
], 1);
922 apply_lights(device
, This
);
924 if (This
->changed
.pixelShader
) IWineD3DDevice_SetPixelShader(device
, This
->pixelShader
);
926 /* Pixel Shader Constants */
927 for (i
= 0; i
< This
->num_contained_ps_consts_f
; ++i
)
929 IWineD3DDevice_SetPixelShaderConstantF(device
, This
->contained_ps_consts_f
[i
],
930 This
->pixelShaderConstantF
+ This
->contained_ps_consts_f
[i
] * 4, 1);
932 for (i
= 0; i
< This
->num_contained_ps_consts_i
; ++i
)
934 IWineD3DDevice_SetPixelShaderConstantI(device
, This
->contained_ps_consts_i
[i
],
935 This
->pixelShaderConstantI
+ This
->contained_ps_consts_i
[i
] * 4, 1);
937 for (i
= 0; i
< This
->num_contained_ps_consts_b
; ++i
)
939 IWineD3DDevice_SetPixelShaderConstantB(device
, This
->contained_ps_consts_b
[i
],
940 This
->pixelShaderConstantB
+ This
->contained_ps_consts_b
[i
], 1);
944 for (i
= 0; i
< This
->num_contained_render_states
; ++i
)
946 IWineD3DDevice_SetRenderState(device
, This
->contained_render_states
[i
],
947 This
->state
.render_states
[This
->contained_render_states
[i
]]);
951 for (i
= 0; i
< This
->num_contained_tss_states
; ++i
)
953 DWORD stage
= This
->contained_tss_states
[i
].stage
;
954 DWORD state
= This
->contained_tss_states
[i
].state
;
956 IWineD3DDevice_SetTextureStageState(device
, stage
, state
, This
->state
.texture_states
[stage
][state
]);
960 for (i
= 0; i
< This
->num_contained_sampler_states
; ++i
)
962 DWORD stage
= This
->contained_sampler_states
[i
].stage
;
963 DWORD state
= This
->contained_sampler_states
[i
].state
;
964 DWORD value
= This
->state
.sampler_states
[stage
][state
];
966 if (stage
>= MAX_FRAGMENT_SAMPLERS
) stage
+= WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
;
967 IWineD3DDevice_SetSamplerState(device
, stage
, state
, value
);
970 for (i
= 0; i
< This
->num_contained_transform_states
; ++i
)
972 IWineD3DDevice_SetTransform(device
, This
->contained_transform_states
[i
],
973 &This
->state
.transforms
[This
->contained_transform_states
[i
]]);
976 if (This
->changed
.primitive_type
)
978 This
->device
->updateStateBlock
->changed
.primitive_type
= TRUE
;
979 This
->device
->updateStateBlock
->gl_primitive_type
= This
->gl_primitive_type
;
982 if (This
->changed
.indices
)
984 IWineD3DDevice_SetIndexBuffer(device
, This
->pIndexData
, This
->IndexFmt
);
985 IWineD3DDevice_SetBaseVertexIndex(device
, This
->baseVertexIndex
);
988 if (This
->changed
.vertexDecl
&& This
->vertexDecl
)
990 IWineD3DDevice_SetVertexDeclaration(device
, This
->vertexDecl
);
993 if (This
->changed
.material
)
995 IWineD3DDevice_SetMaterial(device
, &This
->state
.material
);
998 if (This
->changed
.viewport
)
1000 IWineD3DDevice_SetViewport(device
, &This
->state
.viewport
);
1003 if (This
->changed
.scissorRect
)
1005 IWineD3DDevice_SetScissorRect(device
, &This
->state
.scissor_rect
);
1008 map
= This
->changed
.streamSource
;
1009 for (i
= 0; map
; map
>>= 1, ++i
)
1012 IWineD3DDevice_SetStreamSource(device
, i
,
1013 (IWineD3DBuffer
*)This
->streams
[i
].buffer
,
1014 0, This
->streams
[i
].stride
);
1017 map
= This
->changed
.streamFreq
;
1018 for (i
= 0; map
; map
>>= 1, ++i
)
1021 IWineD3DDevice_SetStreamSourceFreq(device
, i
, This
->streams
[i
].frequency
| This
->streams
[i
].flags
);
1024 map
= This
->changed
.textures
;
1025 for (i
= 0; map
; map
>>= 1, ++i
)
1029 if (!(map
& 1)) continue;
1031 stage
= i
< MAX_FRAGMENT_SAMPLERS
? i
: WINED3DVERTEXTEXTURESAMPLER0
+ i
- MAX_FRAGMENT_SAMPLERS
;
1032 IWineD3DDevice_SetTexture(device
, stage
, This
->textures
[i
]);
1035 map
= This
->changed
.clipplane
;
1036 for (i
= 0; map
; map
>>= 1, ++i
)
1040 if (!(map
& 1)) continue;
1042 clip
[0] = This
->clipplane
[i
][0];
1043 clip
[1] = This
->clipplane
[i
][1];
1044 clip
[2] = This
->clipplane
[i
][2];
1045 clip
[3] = This
->clipplane
[i
][3];
1046 IWineD3DDevice_SetClipPlane(device
, i
, clip
);
1049 This
->device
->stateBlock
->state
.lowest_disabled_stage
= MAX_TEXTURES
- 1;
1050 for (i
= 0; i
< MAX_TEXTURES
- 1; ++i
)
1052 if (This
->device
->stateBlock
->state
.texture_states
[i
][WINED3DTSS_COLOROP
] == WINED3DTOP_DISABLE
)
1054 This
->device
->stateBlock
->state
.lowest_disabled_stage
= i
;
1058 TRACE("(%p) : Applied state block %p ------------------^\n", This
, device
);
1063 static HRESULT WINAPI
IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock
* iface
) {
1064 IWineD3DStateBlockImpl
*This
= (IWineD3DStateBlockImpl
*)iface
;
1065 IWineD3DDevice
*device
= (IWineD3DDevice
*)This
->device
;
1066 IWineD3DDeviceImpl
*device_impl
= (IWineD3DDeviceImpl
*)device
;
1067 const struct wined3d_gl_info
*gl_info
= &device_impl
->adapter
->gl_info
;
1069 WINED3DLINEPATTERN lp
;
1077 IWineD3DSwapChain
*swapchain
;
1078 IWineD3DSurface
*backbuffer
;
1081 /* Note this may have a large overhead but it should only be executed
1082 once, in order to initialize the complete state of the device and
1083 all opengl equivalents */
1084 TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This
, device
);
1085 /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
1086 This
->blockType
= WINED3DSBT_INIT
;
1088 /* Set some of the defaults for lights, transforms etc */
1089 memcpy(&This
->state
.transforms
[WINED3DTS_PROJECTION
], identity
, sizeof(identity
));
1090 memcpy(&This
->state
.transforms
[WINED3DTS_VIEW
], identity
, sizeof(identity
));
1091 for (i
= 0; i
< 256; ++i
)
1093 memcpy(&This
->state
.transforms
[WINED3DTS_WORLDMATRIX(i
)], identity
, sizeof(identity
));
1096 TRACE("Render states\n");
1097 /* Render states: */
1098 if (device_impl
->auto_depth_stencil
)
1099 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ZENABLE
, WINED3DZB_TRUE
);
1101 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ZENABLE
, WINED3DZB_FALSE
);
1102 IWineD3DDevice_SetRenderState(device
, WINED3DRS_FILLMODE
, WINED3DFILL_SOLID
);
1103 IWineD3DDevice_SetRenderState(device
, WINED3DRS_SHADEMODE
, WINED3DSHADE_GOURAUD
);
1104 lp
.lp
.wRepeatFactor
= 0;
1105 lp
.lp
.wLinePattern
= 0;
1106 IWineD3DDevice_SetRenderState(device
, WINED3DRS_LINEPATTERN
, lp
.d
);
1107 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ZWRITEENABLE
, TRUE
);
1108 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ALPHATESTENABLE
, FALSE
);
1109 IWineD3DDevice_SetRenderState(device
, WINED3DRS_LASTPIXEL
, TRUE
);
1110 IWineD3DDevice_SetRenderState(device
, WINED3DRS_SRCBLEND
, WINED3DBLEND_ONE
);
1111 IWineD3DDevice_SetRenderState(device
, WINED3DRS_DESTBLEND
, WINED3DBLEND_ZERO
);
1112 IWineD3DDevice_SetRenderState(device
, WINED3DRS_CULLMODE
, WINED3DCULL_CCW
);
1113 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ZFUNC
, WINED3DCMP_LESSEQUAL
);
1114 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ALPHAFUNC
, WINED3DCMP_ALWAYS
);
1115 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ALPHAREF
, 0);
1116 IWineD3DDevice_SetRenderState(device
, WINED3DRS_DITHERENABLE
, FALSE
);
1117 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ALPHABLENDENABLE
, FALSE
);
1118 IWineD3DDevice_SetRenderState(device
, WINED3DRS_FOGENABLE
, FALSE
);
1119 IWineD3DDevice_SetRenderState(device
, WINED3DRS_SPECULARENABLE
, FALSE
);
1120 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ZVISIBLE
, 0);
1121 IWineD3DDevice_SetRenderState(device
, WINED3DRS_FOGCOLOR
, 0);
1122 IWineD3DDevice_SetRenderState(device
, WINED3DRS_FOGTABLEMODE
, WINED3DFOG_NONE
);
1124 IWineD3DDevice_SetRenderState(device
, WINED3DRS_FOGSTART
, tmpfloat
.d
);
1126 IWineD3DDevice_SetRenderState(device
, WINED3DRS_FOGEND
, tmpfloat
.d
);
1128 IWineD3DDevice_SetRenderState(device
, WINED3DRS_FOGDENSITY
, tmpfloat
.d
);
1129 IWineD3DDevice_SetRenderState(device
, WINED3DRS_EDGEANTIALIAS
, FALSE
);
1130 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ZBIAS
, 0);
1131 IWineD3DDevice_SetRenderState(device
, WINED3DRS_RANGEFOGENABLE
, FALSE
);
1132 IWineD3DDevice_SetRenderState(device
, WINED3DRS_STENCILENABLE
, FALSE
);
1133 IWineD3DDevice_SetRenderState(device
, WINED3DRS_STENCILFAIL
, WINED3DSTENCILOP_KEEP
);
1134 IWineD3DDevice_SetRenderState(device
, WINED3DRS_STENCILZFAIL
, WINED3DSTENCILOP_KEEP
);
1135 IWineD3DDevice_SetRenderState(device
, WINED3DRS_STENCILPASS
, WINED3DSTENCILOP_KEEP
);
1136 IWineD3DDevice_SetRenderState(device
, WINED3DRS_STENCILREF
, 0);
1137 IWineD3DDevice_SetRenderState(device
, WINED3DRS_STENCILMASK
, 0xFFFFFFFF);
1138 IWineD3DDevice_SetRenderState(device
, WINED3DRS_STENCILFUNC
, WINED3DCMP_ALWAYS
);
1139 IWineD3DDevice_SetRenderState(device
, WINED3DRS_STENCILWRITEMASK
, 0xFFFFFFFF);
1140 IWineD3DDevice_SetRenderState(device
, WINED3DRS_TEXTUREFACTOR
, 0xFFFFFFFF);
1141 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP0
, 0);
1142 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP1
, 0);
1143 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP2
, 0);
1144 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP3
, 0);
1145 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP4
, 0);
1146 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP5
, 0);
1147 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP6
, 0);
1148 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP7
, 0);
1149 IWineD3DDevice_SetRenderState(device
, WINED3DRS_CLIPPING
, TRUE
);
1150 IWineD3DDevice_SetRenderState(device
, WINED3DRS_LIGHTING
, TRUE
);
1151 IWineD3DDevice_SetRenderState(device
, WINED3DRS_AMBIENT
, 0);
1152 IWineD3DDevice_SetRenderState(device
, WINED3DRS_FOGVERTEXMODE
, WINED3DFOG_NONE
);
1153 IWineD3DDevice_SetRenderState(device
, WINED3DRS_COLORVERTEX
, TRUE
);
1154 IWineD3DDevice_SetRenderState(device
, WINED3DRS_LOCALVIEWER
, TRUE
);
1155 IWineD3DDevice_SetRenderState(device
, WINED3DRS_NORMALIZENORMALS
, FALSE
);
1156 IWineD3DDevice_SetRenderState(device
, WINED3DRS_DIFFUSEMATERIALSOURCE
, WINED3DMCS_COLOR1
);
1157 IWineD3DDevice_SetRenderState(device
, WINED3DRS_SPECULARMATERIALSOURCE
, WINED3DMCS_COLOR2
);
1158 IWineD3DDevice_SetRenderState(device
, WINED3DRS_AMBIENTMATERIALSOURCE
, WINED3DMCS_MATERIAL
);
1159 IWineD3DDevice_SetRenderState(device
, WINED3DRS_EMISSIVEMATERIALSOURCE
, WINED3DMCS_MATERIAL
);
1160 IWineD3DDevice_SetRenderState(device
, WINED3DRS_VERTEXBLEND
, WINED3DVBF_DISABLE
);
1161 IWineD3DDevice_SetRenderState(device
, WINED3DRS_CLIPPLANEENABLE
, 0);
1162 IWineD3DDevice_SetRenderState(device
, WINED3DRS_SOFTWAREVERTEXPROCESSING
, FALSE
);
1164 IWineD3DDevice_SetRenderState(device
, WINED3DRS_POINTSIZE
, tmpfloat
.d
);
1166 IWineD3DDevice_SetRenderState(device
, WINED3DRS_POINTSIZE_MIN
, tmpfloat
.d
);
1167 IWineD3DDevice_SetRenderState(device
, WINED3DRS_POINTSPRITEENABLE
, FALSE
);
1168 IWineD3DDevice_SetRenderState(device
, WINED3DRS_POINTSCALEENABLE
, FALSE
);
1170 IWineD3DDevice_SetRenderState(device
, WINED3DRS_POINTSCALE_A
, tmpfloat
.d
);
1172 IWineD3DDevice_SetRenderState(device
, WINED3DRS_POINTSCALE_B
, tmpfloat
.d
);
1174 IWineD3DDevice_SetRenderState(device
, WINED3DRS_POINTSCALE_C
, tmpfloat
.d
);
1175 IWineD3DDevice_SetRenderState(device
, WINED3DRS_MULTISAMPLEANTIALIAS
, TRUE
);
1176 IWineD3DDevice_SetRenderState(device
, WINED3DRS_MULTISAMPLEMASK
, 0xFFFFFFFF);
1177 IWineD3DDevice_SetRenderState(device
, WINED3DRS_PATCHEDGESTYLE
, WINED3DPATCHEDGE_DISCRETE
);
1179 IWineD3DDevice_SetRenderState(device
, WINED3DRS_PATCHSEGMENTS
, tmpfloat
.d
);
1180 IWineD3DDevice_SetRenderState(device
, WINED3DRS_DEBUGMONITORTOKEN
, 0xbaadcafe);
1181 tmpfloat
.f
= gl_info
->limits
.pointsize_max
;
1182 IWineD3DDevice_SetRenderState(device
, WINED3DRS_POINTSIZE_MAX
, tmpfloat
.d
);
1183 IWineD3DDevice_SetRenderState(device
, WINED3DRS_INDEXEDVERTEXBLENDENABLE
, FALSE
);
1184 IWineD3DDevice_SetRenderState(device
, WINED3DRS_COLORWRITEENABLE
, 0x0000000F);
1186 IWineD3DDevice_SetRenderState(device
, WINED3DRS_TWEENFACTOR
, tmpfloat
.d
);
1187 IWineD3DDevice_SetRenderState(device
, WINED3DRS_BLENDOP
, WINED3DBLENDOP_ADD
);
1188 IWineD3DDevice_SetRenderState(device
, WINED3DRS_POSITIONDEGREE
, WINED3DDEGREE_CUBIC
);
1189 IWineD3DDevice_SetRenderState(device
, WINED3DRS_NORMALDEGREE
, WINED3DDEGREE_LINEAR
);
1190 /* states new in d3d9 */
1191 IWineD3DDevice_SetRenderState(device
, WINED3DRS_SCISSORTESTENABLE
, FALSE
);
1192 IWineD3DDevice_SetRenderState(device
, WINED3DRS_SLOPESCALEDEPTHBIAS
, 0);
1194 IWineD3DDevice_SetRenderState(device
, WINED3DRS_MINTESSELLATIONLEVEL
, tmpfloat
.d
);
1195 IWineD3DDevice_SetRenderState(device
, WINED3DRS_MAXTESSELLATIONLEVEL
, tmpfloat
.d
);
1196 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ANTIALIASEDLINEENABLE
, FALSE
);
1198 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ADAPTIVETESS_X
, tmpfloat
.d
);
1199 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ADAPTIVETESS_Y
, tmpfloat
.d
);
1201 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ADAPTIVETESS_Z
, tmpfloat
.d
);
1203 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ADAPTIVETESS_W
, tmpfloat
.d
);
1204 IWineD3DDevice_SetRenderState(device
, WINED3DRS_ENABLEADAPTIVETESSELLATION
, FALSE
);
1205 IWineD3DDevice_SetRenderState(device
, WINED3DRS_TWOSIDEDSTENCILMODE
, FALSE
);
1206 IWineD3DDevice_SetRenderState(device
, WINED3DRS_CCW_STENCILFAIL
, WINED3DSTENCILOP_KEEP
);
1207 IWineD3DDevice_SetRenderState(device
, WINED3DRS_CCW_STENCILZFAIL
, WINED3DSTENCILOP_KEEP
);
1208 IWineD3DDevice_SetRenderState(device
, WINED3DRS_CCW_STENCILPASS
, WINED3DSTENCILOP_KEEP
);
1209 IWineD3DDevice_SetRenderState(device
, WINED3DRS_CCW_STENCILFUNC
, WINED3DCMP_ALWAYS
);
1210 IWineD3DDevice_SetRenderState(device
, WINED3DRS_COLORWRITEENABLE1
, 0x0000000F);
1211 IWineD3DDevice_SetRenderState(device
, WINED3DRS_COLORWRITEENABLE2
, 0x0000000F);
1212 IWineD3DDevice_SetRenderState(device
, WINED3DRS_COLORWRITEENABLE3
, 0x0000000F);
1213 IWineD3DDevice_SetRenderState(device
, WINED3DRS_BLENDFACTOR
, 0xFFFFFFFF);
1214 IWineD3DDevice_SetRenderState(device
, WINED3DRS_SRGBWRITEENABLE
, 0);
1215 IWineD3DDevice_SetRenderState(device
, WINED3DRS_DEPTHBIAS
, 0);
1216 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP8
, 0);
1217 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP9
, 0);
1218 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP10
, 0);
1219 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP11
, 0);
1220 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP12
, 0);
1221 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP13
, 0);
1222 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP14
, 0);
1223 IWineD3DDevice_SetRenderState(device
, WINED3DRS_WRAP15
, 0);
1224 IWineD3DDevice_SetRenderState(device
, WINED3DRS_SEPARATEALPHABLENDENABLE
, FALSE
);
1225 IWineD3DDevice_SetRenderState(device
, WINED3DRS_SRCBLENDALPHA
, WINED3DBLEND_ONE
);
1226 IWineD3DDevice_SetRenderState(device
, WINED3DRS_DESTBLENDALPHA
, WINED3DBLEND_ZERO
);
1227 IWineD3DDevice_SetRenderState(device
, WINED3DRS_BLENDOPALPHA
, WINED3DBLENDOP_ADD
);
1229 /* clipping status */
1230 This
->clip_status
.ClipUnion
= 0;
1231 This
->clip_status
.ClipIntersection
= 0xFFFFFFFF;
1233 /* Texture Stage States - Put directly into state block, we will call function below */
1234 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1236 TRACE("Setting up default texture states for texture Stage %u.\n", i
);
1237 memcpy(&This
->state
.transforms
[WINED3DTS_TEXTURE0
+ i
], identity
, sizeof(identity
));
1238 This
->state
.texture_states
[i
][WINED3DTSS_COLOROP
] = i
? WINED3DTOP_DISABLE
: WINED3DTOP_MODULATE
;
1239 This
->state
.texture_states
[i
][WINED3DTSS_COLORARG1
] = WINED3DTA_TEXTURE
;
1240 This
->state
.texture_states
[i
][WINED3DTSS_COLORARG2
] = WINED3DTA_CURRENT
;
1241 This
->state
.texture_states
[i
][WINED3DTSS_ALPHAOP
] = i
? WINED3DTOP_DISABLE
: WINED3DTOP_SELECTARG1
;
1242 This
->state
.texture_states
[i
][WINED3DTSS_ALPHAARG1
] = WINED3DTA_TEXTURE
;
1243 This
->state
.texture_states
[i
][WINED3DTSS_ALPHAARG2
] = WINED3DTA_CURRENT
;
1244 This
->state
.texture_states
[i
][WINED3DTSS_BUMPENVMAT00
] = 0;
1245 This
->state
.texture_states
[i
][WINED3DTSS_BUMPENVMAT01
] = 0;
1246 This
->state
.texture_states
[i
][WINED3DTSS_BUMPENVMAT10
] = 0;
1247 This
->state
.texture_states
[i
][WINED3DTSS_BUMPENVMAT11
] = 0;
1248 This
->state
.texture_states
[i
][WINED3DTSS_TEXCOORDINDEX
] = i
;
1249 This
->state
.texture_states
[i
][WINED3DTSS_BUMPENVLSCALE
] = 0;
1250 This
->state
.texture_states
[i
][WINED3DTSS_BUMPENVLOFFSET
] = 0;
1251 This
->state
.texture_states
[i
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] = WINED3DTTFF_DISABLE
;
1252 This
->state
.texture_states
[i
][WINED3DTSS_COLORARG0
] = WINED3DTA_CURRENT
;
1253 This
->state
.texture_states
[i
][WINED3DTSS_ALPHAARG0
] = WINED3DTA_CURRENT
;
1254 This
->state
.texture_states
[i
][WINED3DTSS_RESULTARG
] = WINED3DTA_CURRENT
;
1256 This
->state
.lowest_disabled_stage
= 1;
1259 for (i
= 0 ; i
< MAX_COMBINED_SAMPLERS
; ++i
)
1261 TRACE("Setting up default samplers states for sampler %u.\n", i
);
1262 This
->state
.sampler_states
[i
][WINED3DSAMP_ADDRESSU
] = WINED3DTADDRESS_WRAP
;
1263 This
->state
.sampler_states
[i
][WINED3DSAMP_ADDRESSV
] = WINED3DTADDRESS_WRAP
;
1264 This
->state
.sampler_states
[i
][WINED3DSAMP_ADDRESSW
] = WINED3DTADDRESS_WRAP
;
1265 This
->state
.sampler_states
[i
][WINED3DSAMP_BORDERCOLOR
] = 0;
1266 This
->state
.sampler_states
[i
][WINED3DSAMP_MAGFILTER
] = WINED3DTEXF_POINT
;
1267 This
->state
.sampler_states
[i
][WINED3DSAMP_MINFILTER
] = WINED3DTEXF_POINT
;
1268 This
->state
.sampler_states
[i
][WINED3DSAMP_MIPFILTER
] = WINED3DTEXF_NONE
;
1269 This
->state
.sampler_states
[i
][WINED3DSAMP_MIPMAPLODBIAS
] = 0;
1270 This
->state
.sampler_states
[i
][WINED3DSAMP_MAXMIPLEVEL
] = 0;
1271 This
->state
.sampler_states
[i
][WINED3DSAMP_MAXANISOTROPY
] = 1;
1272 This
->state
.sampler_states
[i
][WINED3DSAMP_SRGBTEXTURE
] = 0;
1273 /* TODO: Indicates which element of a multielement texture to use. */
1274 This
->state
.sampler_states
[i
][WINED3DSAMP_ELEMENTINDEX
] = 0;
1275 /* TODO: Vertex offset in the presampled displacement map. */
1276 This
->state
.sampler_states
[i
][WINED3DSAMP_DMAPOFFSET
] = 0;
1279 for (i
= 0; i
< gl_info
->limits
.textures
; ++i
)
1281 /* Note: This avoids calling SetTexture, so pretend it has been called */
1282 This
->changed
.textures
|= 1 << i
;
1283 This
->textures
[i
] = NULL
;
1286 /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1287 hr
= IWineD3DDevice_GetSwapChain(device
, 0, &swapchain
);
1288 if (SUCCEEDED(hr
) && swapchain
)
1292 hr
= IWineD3DSwapChain_GetBackBuffer(swapchain
, 0, WINED3DBACKBUFFER_TYPE_MONO
, &backbuffer
);
1293 if (SUCCEEDED(hr
) && backbuffer
)
1295 WINED3DSURFACE_DESC desc
;
1298 IWineD3DSurface_GetDesc(backbuffer
, &desc
);
1299 IWineD3DSurface_Release(backbuffer
);
1301 /* Set the default scissor rect values */
1302 scissorrect
.left
= 0;
1303 scissorrect
.right
= desc
.width
;
1304 scissorrect
.top
= 0;
1305 scissorrect
.bottom
= desc
.height
;
1306 hr
= IWineD3DDevice_SetScissorRect(device
, &scissorrect
);
1307 if (FAILED(hr
)) ERR("This should never happen, expect rendering issues!\n");
1310 /* Set the default viewport */
1313 vp
.Width
= ((IWineD3DSwapChainImpl
*)swapchain
)->presentParms
.BackBufferWidth
;
1314 vp
.Height
= ((IWineD3DSwapChainImpl
*)swapchain
)->presentParms
.BackBufferHeight
;
1317 IWineD3DDevice_SetViewport(device
, &vp
);
1319 IWineD3DSwapChain_Release(swapchain
);
1322 TRACE("-----------------------> Device defaults now set up...\n");
1326 /**********************************************************
1327 * IWineD3DStateBlock VTbl follows
1328 **********************************************************/
1330 static const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl
=
1333 IWineD3DStateBlockImpl_QueryInterface
,
1334 IWineD3DStateBlockImpl_AddRef
,
1335 IWineD3DStateBlockImpl_Release
,
1336 /* IWineD3DStateBlock */
1337 IWineD3DStateBlockImpl_Capture
,
1338 IWineD3DStateBlockImpl_Apply
,
1339 IWineD3DStateBlockImpl_InitStartupStateBlock
1342 HRESULT
stateblock_init(IWineD3DStateBlockImpl
*stateblock
, IWineD3DDeviceImpl
*device
, WINED3DSTATEBLOCKTYPE type
)
1347 stateblock
->lpVtbl
= &IWineD3DStateBlock_Vtbl
;
1348 stateblock
->ref
= 1;
1349 stateblock
->device
= device
;
1350 stateblock
->blockType
= type
;
1352 for (i
= 0; i
< LIGHTMAP_SIZE
; i
++)
1354 list_init(&stateblock
->lightMap
[i
]);
1357 hr
= stateblock_allocate_shader_constants(stateblock
);
1358 if (FAILED(hr
)) return hr
;
1360 /* The WINED3DSBT_INIT stateblock type is used during initialization to
1361 * produce a placeholder stateblock so other functions called can update a
1363 if (type
== WINED3DSBT_INIT
|| type
== WINED3DSBT_RECORDED
) return WINED3D_OK
;
1365 TRACE("Updating changed flags appropriate for type %#x.\n", type
);
1369 case WINED3DSBT_ALL
:
1370 stateblock_init_lights(stateblock
, device
->stateBlock
->lightMap
);
1371 stateblock_savedstates_set_all(&stateblock
->changed
, device
->d3d_vshader_constantF
,
1372 device
->d3d_pshader_constantF
);
1375 case WINED3DSBT_PIXELSTATE
:
1376 stateblock_savedstates_set_pixel(&stateblock
->changed
, device
->d3d_pshader_constantF
);
1379 case WINED3DSBT_VERTEXSTATE
:
1380 stateblock_init_lights(stateblock
, device
->stateBlock
->lightMap
);
1381 stateblock_savedstates_set_vertex(&stateblock
->changed
, device
->d3d_vshader_constantF
);
1385 FIXME("Unrecognized state block type %#x.\n", type
);
1389 stateblock_init_contained_states(stateblock
);
1390 IWineD3DStateBlockImpl_Capture((IWineD3DStateBlock
*)stateblock
);