VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/stateblock.c@ 32891

Last change on this file since 32891 was 28475, checked in by vboxsync, 15 years ago

crOpenGL: update to wine 1.1.43

  • Property svn:eol-style set to native
File size: 56.1 KB
Line 
1/*
2 * state block implementation
3 *
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
9 *
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.
14 *
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.
19 *
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
23 */
24
25/*
26 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
27 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
28 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
29 * a choice of LGPL license versions is made available with the language indicating
30 * that LGPLv2 or any later version may be used, or where a choice of which version
31 * of the LGPL is applied is otherwise unspecified.
32 */
33
34#include "config.h"
35#include "wined3d_private.h"
36
37WINE_DEFAULT_DEBUG_CHANNEL(d3d);
38
39static const DWORD pixel_states_render[] =
40{
41 WINED3DRS_ALPHABLENDENABLE,
42 WINED3DRS_ALPHAFUNC,
43 WINED3DRS_ALPHAREF,
44 WINED3DRS_ALPHATESTENABLE,
45 WINED3DRS_ANTIALIASEDLINEENABLE,
46 WINED3DRS_BLENDFACTOR,
47 WINED3DRS_BLENDOP,
48 WINED3DRS_BLENDOPALPHA,
49 WINED3DRS_CCW_STENCILFAIL,
50 WINED3DRS_CCW_STENCILPASS,
51 WINED3DRS_CCW_STENCILZFAIL,
52 WINED3DRS_COLORWRITEENABLE,
53 WINED3DRS_COLORWRITEENABLE1,
54 WINED3DRS_COLORWRITEENABLE2,
55 WINED3DRS_COLORWRITEENABLE3,
56 WINED3DRS_DEPTHBIAS,
57 WINED3DRS_DESTBLEND,
58 WINED3DRS_DESTBLENDALPHA,
59 WINED3DRS_DITHERENABLE,
60 WINED3DRS_FILLMODE,
61 WINED3DRS_FOGDENSITY,
62 WINED3DRS_FOGEND,
63 WINED3DRS_FOGSTART,
64 WINED3DRS_LASTPIXEL,
65 WINED3DRS_SCISSORTESTENABLE,
66 WINED3DRS_SEPARATEALPHABLENDENABLE,
67 WINED3DRS_SHADEMODE,
68 WINED3DRS_SLOPESCALEDEPTHBIAS,
69 WINED3DRS_SRCBLEND,
70 WINED3DRS_SRCBLENDALPHA,
71 WINED3DRS_SRGBWRITEENABLE,
72 WINED3DRS_STENCILENABLE,
73 WINED3DRS_STENCILFAIL,
74 WINED3DRS_STENCILFUNC,
75 WINED3DRS_STENCILMASK,
76 WINED3DRS_STENCILPASS,
77 WINED3DRS_STENCILREF,
78 WINED3DRS_STENCILWRITEMASK,
79 WINED3DRS_STENCILZFAIL,
80 WINED3DRS_TEXTUREFACTOR,
81 WINED3DRS_TWOSIDEDSTENCILMODE,
82 WINED3DRS_WRAP0,
83 WINED3DRS_WRAP1,
84 WINED3DRS_WRAP10,
85 WINED3DRS_WRAP11,
86 WINED3DRS_WRAP12,
87 WINED3DRS_WRAP13,
88 WINED3DRS_WRAP14,
89 WINED3DRS_WRAP15,
90 WINED3DRS_WRAP2,
91 WINED3DRS_WRAP3,
92 WINED3DRS_WRAP4,
93 WINED3DRS_WRAP5,
94 WINED3DRS_WRAP6,
95 WINED3DRS_WRAP7,
96 WINED3DRS_WRAP8,
97 WINED3DRS_WRAP9,
98 WINED3DRS_ZENABLE,
99 WINED3DRS_ZFUNC,
100 WINED3DRS_ZWRITEENABLE,
101};
102
103static const DWORD pixel_states_texture[] =
104{
105 WINED3DTSS_ALPHAARG0,
106 WINED3DTSS_ALPHAARG1,
107 WINED3DTSS_ALPHAARG2,
108 WINED3DTSS_ALPHAOP,
109 WINED3DTSS_BUMPENVLOFFSET,
110 WINED3DTSS_BUMPENVLSCALE,
111 WINED3DTSS_BUMPENVMAT00,
112 WINED3DTSS_BUMPENVMAT01,
113 WINED3DTSS_BUMPENVMAT10,
114 WINED3DTSS_BUMPENVMAT11,
115 WINED3DTSS_COLORARG0,
116 WINED3DTSS_COLORARG1,
117 WINED3DTSS_COLORARG2,
118 WINED3DTSS_COLOROP,
119 WINED3DTSS_RESULTARG,
120 WINED3DTSS_TEXCOORDINDEX,
121 WINED3DTSS_TEXTURETRANSFORMFLAGS,
122};
123
124static const DWORD pixel_states_sampler[] =
125{
126 WINED3DSAMP_ADDRESSU,
127 WINED3DSAMP_ADDRESSV,
128 WINED3DSAMP_ADDRESSW,
129 WINED3DSAMP_BORDERCOLOR,
130 WINED3DSAMP_MAGFILTER,
131 WINED3DSAMP_MINFILTER,
132 WINED3DSAMP_MIPFILTER,
133 WINED3DSAMP_MIPMAPLODBIAS,
134 WINED3DSAMP_MAXMIPLEVEL,
135 WINED3DSAMP_MAXANISOTROPY,
136 WINED3DSAMP_SRGBTEXTURE,
137 WINED3DSAMP_ELEMENTINDEX,
138};
139
140static const DWORD vertex_states_render[] =
141{
142 WINED3DRS_ADAPTIVETESS_W,
143 WINED3DRS_ADAPTIVETESS_X,
144 WINED3DRS_ADAPTIVETESS_Y,
145 WINED3DRS_ADAPTIVETESS_Z,
146 WINED3DRS_AMBIENT,
147 WINED3DRS_AMBIENTMATERIALSOURCE,
148 WINED3DRS_CLIPPING,
149 WINED3DRS_CLIPPLANEENABLE,
150 WINED3DRS_COLORVERTEX,
151 WINED3DRS_CULLMODE,
152 WINED3DRS_DIFFUSEMATERIALSOURCE,
153 WINED3DRS_EMISSIVEMATERIALSOURCE,
154 WINED3DRS_ENABLEADAPTIVETESSELLATION,
155 WINED3DRS_FOGCOLOR,
156 WINED3DRS_FOGDENSITY,
157 WINED3DRS_FOGENABLE,
158 WINED3DRS_FOGEND,
159 WINED3DRS_FOGSTART,
160 WINED3DRS_FOGTABLEMODE,
161 WINED3DRS_FOGVERTEXMODE,
162 WINED3DRS_INDEXEDVERTEXBLENDENABLE,
163 WINED3DRS_LIGHTING,
164 WINED3DRS_LOCALVIEWER,
165 WINED3DRS_MAXTESSELLATIONLEVEL,
166 WINED3DRS_MINTESSELLATIONLEVEL,
167 WINED3DRS_MULTISAMPLEANTIALIAS,
168 WINED3DRS_MULTISAMPLEMASK,
169 WINED3DRS_NORMALDEGREE,
170 WINED3DRS_NORMALIZENORMALS,
171 WINED3DRS_PATCHEDGESTYLE,
172 WINED3DRS_POINTSCALE_A,
173 WINED3DRS_POINTSCALE_B,
174 WINED3DRS_POINTSCALE_C,
175 WINED3DRS_POINTSCALEENABLE,
176 WINED3DRS_POINTSIZE,
177 WINED3DRS_POINTSIZE_MAX,
178 WINED3DRS_POINTSIZE_MIN,
179 WINED3DRS_POINTSPRITEENABLE,
180 WINED3DRS_POSITIONDEGREE,
181 WINED3DRS_RANGEFOGENABLE,
182 WINED3DRS_SHADEMODE,
183 WINED3DRS_SPECULARENABLE,
184 WINED3DRS_SPECULARMATERIALSOURCE,
185 WINED3DRS_TWEENFACTOR,
186 WINED3DRS_VERTEXBLEND,
187};
188
189static const DWORD vertex_states_texture[] =
190{
191 WINED3DTSS_TEXCOORDINDEX,
192 WINED3DTSS_TEXTURETRANSFORMFLAGS,
193};
194
195static const DWORD vertex_states_sampler[] =
196{
197 WINED3DSAMP_DMAPOFFSET,
198};
199
200/* Allocates the correct amount of space for pixel and vertex shader constants,
201 * along with their set/changed flags on the given stateblock object
202 */
203static HRESULT stateblock_allocate_shader_constants(IWineD3DStateBlockImpl *object)
204{
205 IWineD3DDeviceImpl *device = object->device;
206
207 /* Allocate space for floating point constants */
208 object->pixelShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
209 sizeof(float) * device->d3d_pshader_constantF * 4);
210 if (!object->pixelShaderConstantF) goto fail;
211
212 object->changed.pixelShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
213 sizeof(BOOL) * device->d3d_pshader_constantF);
214 if (!object->changed.pixelShaderConstantsF) goto fail;
215
216 object->vertexShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
217 sizeof(float) * device->d3d_vshader_constantF * 4);
218 if (!object->vertexShaderConstantF) goto fail;
219
220 object->changed.vertexShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
221 sizeof(BOOL) * device->d3d_vshader_constantF);
222 if (!object->changed.vertexShaderConstantsF) goto fail;
223
224 object->contained_vs_consts_f = HeapAlloc(GetProcessHeap(), 0,
225 sizeof(DWORD) * device->d3d_vshader_constantF);
226 if (!object->contained_vs_consts_f) goto fail;
227
228 object->contained_ps_consts_f = HeapAlloc(GetProcessHeap(), 0,
229 sizeof(DWORD) * device->d3d_pshader_constantF);
230 if (!object->contained_ps_consts_f) goto fail;
231
232 return WINED3D_OK;
233
234fail:
235 ERR("Failed to allocate memory\n");
236 HeapFree(GetProcessHeap(), 0, object->pixelShaderConstantF);
237 HeapFree(GetProcessHeap(), 0, object->changed.pixelShaderConstantsF);
238 HeapFree(GetProcessHeap(), 0, object->vertexShaderConstantF);
239 HeapFree(GetProcessHeap(), 0, object->changed.vertexShaderConstantsF);
240 HeapFree(GetProcessHeap(), 0, object->contained_vs_consts_f);
241 HeapFree(GetProcessHeap(), 0, object->contained_ps_consts_f);
242 return E_OUTOFMEMORY;
243}
244
245static inline void stateblock_set_bits(DWORD *map, UINT map_size)
246{
247 DWORD mask = (1 << (map_size & 0x1f)) - 1;
248 memset(map, 0xff, (map_size >> 5) * sizeof(*map));
249 if (mask) map[map_size >> 5] = mask;
250}
251
252/* Set all members of a stateblock savedstate to the given value */
253static void stateblock_savedstates_set_all(SAVEDSTATES *states, DWORD vs_consts, DWORD ps_consts)
254{
255 unsigned int i;
256
257 /* Single values */
258 states->primitive_type = 1;
259 states->indices = 1;
260 states->material = 1;
261 states->viewport = 1;
262 states->vertexDecl = 1;
263 states->pixelShader = 1;
264 states->vertexShader = 1;
265 states->scissorRect = 1;
266
267 /* Fixed size arrays */
268 states->streamSource = 0xffff;
269 states->streamFreq = 0xffff;
270 states->textures = 0xfffff;
271 stateblock_set_bits(states->transform, HIGHEST_TRANSFORMSTATE + 1);
272 stateblock_set_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
273 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
274 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3fff;
275 states->clipplane = 0xffffffff;
276 states->pixelShaderConstantsB = 0xffff;
277 states->pixelShaderConstantsI = 0xffff;
278 states->vertexShaderConstantsB = 0xffff;
279 states->vertexShaderConstantsI = 0xffff;
280
281 /* Dynamically sized arrays */
282 memset(states->pixelShaderConstantsF, TRUE, sizeof(BOOL) * ps_consts);
283 memset(states->vertexShaderConstantsF, TRUE, sizeof(BOOL) * vs_consts);
284}
285
286static void stateblock_savedstates_set_pixel(SAVEDSTATES *states, const DWORD num_constants)
287{
288 DWORD texture_mask = 0;
289 WORD sampler_mask = 0;
290 unsigned int i;
291
292 states->pixelShader = 1;
293
294 for (i = 0; i < sizeof(pixel_states_render) / sizeof(*pixel_states_render); ++i)
295 {
296 DWORD rs = pixel_states_render[i];
297 states->renderState[rs >> 5] |= 1 << (rs & 0x1f);
298 }
299
300 for (i = 0; i < sizeof(pixel_states_texture) / sizeof(*pixel_states_texture); ++i)
301 texture_mask |= 1 << pixel_states_texture[i];
302 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
303 for (i = 0; i < sizeof(pixel_states_sampler) / sizeof(*pixel_states_sampler); ++i)
304 sampler_mask |= 1 << pixel_states_sampler[i];
305 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
306 states->pixelShaderConstantsB = 0xffff;
307 states->pixelShaderConstantsI = 0xffff;
308
309 memset(states->pixelShaderConstantsF, TRUE, sizeof(BOOL) * num_constants);
310}
311
312static void stateblock_savedstates_set_vertex(SAVEDSTATES *states, const DWORD num_constants)
313{
314 DWORD texture_mask = 0;
315 WORD sampler_mask = 0;
316 unsigned int i;
317
318 states->vertexDecl = 1;
319 states->vertexShader = 1;
320
321 for (i = 0; i < sizeof(vertex_states_render) / sizeof(*vertex_states_render); ++i)
322 {
323 DWORD rs = vertex_states_render[i];
324 states->renderState[rs >> 5] |= 1 << (rs & 0x1f);
325 }
326
327 for (i = 0; i < sizeof(vertex_states_texture) / sizeof(*vertex_states_texture); ++i)
328 texture_mask |= 1 << vertex_states_texture[i];
329 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = texture_mask;
330 for (i = 0; i < sizeof(vertex_states_sampler) / sizeof(*vertex_states_sampler); ++i)
331 sampler_mask |= 1 << vertex_states_sampler[i];
332 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = sampler_mask;
333 states->vertexShaderConstantsB = 0xffff;
334 states->vertexShaderConstantsI = 0xffff;
335
336 memset(states->vertexShaderConstantsF, TRUE, sizeof(BOOL) * num_constants);
337}
338
339void stateblock_init_contained_states(IWineD3DStateBlockImpl *stateblock)
340{
341 IWineD3DDeviceImpl *device = stateblock->device;
342 unsigned int i, j;
343
344 for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
345 {
346 DWORD map = stateblock->changed.renderState[i];
347 for (j = 0; map; map >>= 1, ++j)
348 {
349 if (!(map & 1)) continue;
350
351 stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
352 ++stateblock->num_contained_render_states;
353 }
354 }
355
356 for (i = 0; i <= HIGHEST_TRANSFORMSTATE >> 5; ++i)
357 {
358 DWORD map = stateblock->changed.transform[i];
359 for (j = 0; map; map >>= 1, ++j)
360 {
361 if (!(map & 1)) continue;
362
363 stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
364 ++stateblock->num_contained_transform_states;
365 }
366 }
367
368 for (i = 0; i < device->d3d_vshader_constantF; ++i)
369 {
370 if (stateblock->changed.vertexShaderConstantsF[i])
371 {
372 stateblock->contained_vs_consts_f[stateblock->num_contained_vs_consts_f] = i;
373 ++stateblock->num_contained_vs_consts_f;
374 }
375 }
376
377 for (i = 0; i < MAX_CONST_I; ++i)
378 {
379 if (stateblock->changed.vertexShaderConstantsI & (1 << i))
380 {
381 stateblock->contained_vs_consts_i[stateblock->num_contained_vs_consts_i] = i;
382 ++stateblock->num_contained_vs_consts_i;
383 }
384 }
385
386 for (i = 0; i < MAX_CONST_B; ++i)
387 {
388 if (stateblock->changed.vertexShaderConstantsB & (1 << i))
389 {
390 stateblock->contained_vs_consts_b[stateblock->num_contained_vs_consts_b] = i;
391 ++stateblock->num_contained_vs_consts_b;
392 }
393 }
394
395 for (i = 0; i < device->d3d_pshader_constantF; ++i)
396 {
397 if (stateblock->changed.pixelShaderConstantsF[i])
398 {
399 stateblock->contained_ps_consts_f[stateblock->num_contained_ps_consts_f] = i;
400 ++stateblock->num_contained_ps_consts_f;
401 }
402 }
403
404 for (i = 0; i < MAX_CONST_I; ++i)
405 {
406 if (stateblock->changed.pixelShaderConstantsI & (1 << i))
407 {
408 stateblock->contained_ps_consts_i[stateblock->num_contained_ps_consts_i] = i;
409 ++stateblock->num_contained_ps_consts_i;
410 }
411 }
412
413 for (i = 0; i < MAX_CONST_B; ++i)
414 {
415 if (stateblock->changed.pixelShaderConstantsB & (1 << i))
416 {
417 stateblock->contained_ps_consts_b[stateblock->num_contained_ps_consts_b] = i;
418 ++stateblock->num_contained_ps_consts_b;
419 }
420 }
421
422 for (i = 0; i < MAX_TEXTURES; ++i)
423 {
424 DWORD map = stateblock->changed.textureState[i];
425
426 for(j = 0; map; map >>= 1, ++j)
427 {
428 if (!(map & 1)) continue;
429
430 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
431 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
432 ++stateblock->num_contained_tss_states;
433 }
434 }
435
436 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
437 {
438 DWORD map = stateblock->changed.samplerState[i];
439
440 for (j = 0; map; map >>= 1, ++j)
441 {
442 if (!(map & 1)) continue;
443
444 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
445 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
446 ++stateblock->num_contained_sampler_states;
447 }
448 }
449}
450
451static void stateblock_init_lights(IWineD3DStateBlockImpl *stateblock, struct list *light_map)
452{
453 unsigned int i;
454
455 for (i = 0; i < LIGHTMAP_SIZE; ++i)
456 {
457 const struct wined3d_light_info *src_light;
458
459 LIST_FOR_EACH_ENTRY(src_light, &light_map[i], struct wined3d_light_info, entry)
460 {
461 struct wined3d_light_info *dst_light = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_light));
462
463 *dst_light = *src_light;
464 list_add_tail(&stateblock->lightMap[i], &dst_light->entry);
465 }
466 }
467}
468
469/**********************************************************
470 * IWineD3DStateBlockImpl IUnknown parts follows
471 **********************************************************/
472static HRESULT WINAPI IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock *iface,REFIID riid,LPVOID *ppobj)
473{
474 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
475 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
476 if (IsEqualGUID(riid, &IID_IUnknown)
477 || IsEqualGUID(riid, &IID_IWineD3DStateBlock))
478 {
479 IUnknown_AddRef(iface);
480 *ppobj = This;
481 return S_OK;
482 }
483 *ppobj = NULL;
484 return E_NOINTERFACE;
485}
486
487static ULONG WINAPI IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock *iface) {
488 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
489 ULONG refCount = InterlockedIncrement(&This->ref);
490
491 TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
492 return refCount;
493}
494
495static ULONG WINAPI IWineD3DStateBlockImpl_Release(IWineD3DStateBlock *iface) {
496 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
497 ULONG refCount = InterlockedDecrement(&This->ref);
498
499 TRACE("(%p) : Releasing from %d\n", This, refCount + 1);
500
501 if (!refCount) {
502 int counter;
503
504 if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
505
506 for (counter = 0; counter < MAX_COMBINED_SAMPLERS; counter++)
507 {
508 if (This->textures[counter]) IWineD3DBaseTexture_Release(This->textures[counter]);
509 }
510
511 for (counter = 0; counter < MAX_STREAMS; counter++) {
512 if(This->streamSource[counter]) {
513 if (IWineD3DBuffer_Release(This->streamSource[counter]))
514 {
515 TRACE("Vertex buffer still referenced by stateblock, applications has leaked Stream %u, buffer %p\n", counter, This->streamSource[counter]);
516 }
517 }
518 }
519 if(This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
520 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
521 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
522
523 for(counter = 0; counter < LIGHTMAP_SIZE; counter++) {
524 struct list *e1, *e2;
525 LIST_FOR_EACH_SAFE(e1, e2, &This->lightMap[counter])
526 {
527 struct wined3d_light_info *light = LIST_ENTRY(e1, struct wined3d_light_info, entry);
528 list_remove(&light->entry);
529 HeapFree(GetProcessHeap(), 0, light);
530 }
531 }
532
533 HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
534 HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
535 HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
536 HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
537 HeapFree(GetProcessHeap(), 0, This->contained_vs_consts_f);
538 HeapFree(GetProcessHeap(), 0, This->contained_ps_consts_f);
539 HeapFree(GetProcessHeap(), 0, This);
540 }
541 return refCount;
542}
543
544/**********************************************************
545 * IWineD3DStateBlockImpl parts follows
546 **********************************************************/
547static void record_lights(IWineD3DStateBlockImpl *This, const IWineD3DStateBlockImpl *targetStateBlock)
548{
549 UINT i;
550
551 /* Lights... For a recorded state block, we just had a chain of actions to perform,
552 * so we need to walk that chain and update any actions which differ
553 */
554 for(i = 0; i < LIGHTMAP_SIZE; i++) {
555 struct list *e, *f;
556 LIST_FOR_EACH(e, &This->lightMap[i]) {
557 BOOL updated = FALSE;
558 struct wined3d_light_info *src = LIST_ENTRY(e, struct wined3d_light_info, entry), *realLight;
559
560 /* Look up the light in the destination */
561 LIST_FOR_EACH(f, &targetStateBlock->lightMap[i]) {
562 realLight = LIST_ENTRY(f, struct wined3d_light_info, entry);
563 if (realLight->OriginalIndex == src->OriginalIndex)
564 {
565 src->OriginalParms = realLight->OriginalParms;
566
567 if (realLight->glIndex == -1 && src->glIndex != -1)
568 {
569 /* Light disabled */
570 This->activeLights[src->glIndex] = NULL;
571 }
572 else if (realLight->glIndex != -1 && src->glIndex == -1)
573 {
574 /* Light enabled */
575 This->activeLights[realLight->glIndex] = src;
576 }
577 src->glIndex = realLight->glIndex;
578 updated = TRUE;
579 break;
580 }
581 }
582
583 if (!updated)
584 {
585 /* This can happen if the light was originally created as a
586 * default light for SetLightEnable() while recording. */
587 WARN("Light %u in stateblock %p does not exist in device stateblock %p.\n",
588 src->OriginalIndex, This, targetStateBlock);
589
590 src->OriginalParms = WINED3D_default_light;
591 if (src->glIndex != -1)
592 {
593 This->activeLights[src->glIndex] = NULL;
594 src->glIndex = -1;
595 }
596 }
597 }
598 }
599}
600
601static HRESULT WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface)
602{
603 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
604 IWineD3DStateBlockImpl *targetStateBlock = This->device->stateBlock;
605 unsigned int i;
606 DWORD map;
607
608 TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock, This);
609
610 if (This->changed.vertexShader && This->vertexShader != targetStateBlock->vertexShader)
611 {
612 TRACE("Updating vertex shader from %p to %p\n", This->vertexShader, targetStateBlock->vertexShader);
613
614 if (targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
615 if (This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
616 This->vertexShader = targetStateBlock->vertexShader;
617 }
618
619 /* Vertex Shader Float Constants */
620 for (i = 0; i < This->num_contained_vs_consts_f; ++i)
621 {
622 unsigned int idx = This->contained_vs_consts_f[i];
623
624 TRACE("Setting %p from %p %u to {%.8e, %.8e, %.8e, %.8e}.\n",
625 This, targetStateBlock, idx,
626 targetStateBlock->vertexShaderConstantF[idx * 4 + 0],
627 targetStateBlock->vertexShaderConstantF[idx * 4 + 1],
628 targetStateBlock->vertexShaderConstantF[idx * 4 + 2],
629 targetStateBlock->vertexShaderConstantF[idx * 4 + 3]);
630
631 This->vertexShaderConstantF[idx * 4 + 0] = targetStateBlock->vertexShaderConstantF[idx * 4 + 0];
632 This->vertexShaderConstantF[idx * 4 + 1] = targetStateBlock->vertexShaderConstantF[idx * 4 + 1];
633 This->vertexShaderConstantF[idx * 4 + 2] = targetStateBlock->vertexShaderConstantF[idx * 4 + 2];
634 This->vertexShaderConstantF[idx * 4 + 3] = targetStateBlock->vertexShaderConstantF[idx * 4 + 3];
635 }
636
637 /* Vertex Shader Integer Constants */
638 for (i = 0; i < This->num_contained_vs_consts_i; ++i)
639 {
640 unsigned int idx = This->contained_vs_consts_i[i];
641
642 TRACE("Setting %p from %p %u to {%d, %d, %d, %d}.\n",
643 This, targetStateBlock, idx,
644 targetStateBlock->vertexShaderConstantI[idx * 4 + 0],
645 targetStateBlock->vertexShaderConstantI[idx * 4 + 1],
646 targetStateBlock->vertexShaderConstantI[idx * 4 + 2],
647 targetStateBlock->vertexShaderConstantI[idx * 4 + 3]);
648
649 This->vertexShaderConstantI[idx * 4 + 0] = targetStateBlock->vertexShaderConstantI[idx * 4 + 0];
650 This->vertexShaderConstantI[idx * 4 + 1] = targetStateBlock->vertexShaderConstantI[idx * 4 + 1];
651 This->vertexShaderConstantI[idx * 4 + 2] = targetStateBlock->vertexShaderConstantI[idx * 4 + 2];
652 This->vertexShaderConstantI[idx * 4 + 3] = targetStateBlock->vertexShaderConstantI[idx * 4 + 3];
653 }
654
655 /* Vertex Shader Boolean Constants */
656 for (i = 0; i < This->num_contained_vs_consts_b; ++i)
657 {
658 unsigned int idx = This->contained_vs_consts_b[i];
659
660 TRACE("Setting %p from %p %u to %s.\n",
661 This, targetStateBlock, idx,
662 targetStateBlock->vertexShaderConstantB[idx] ? "TRUE" : "FALSE");
663
664 This->vertexShaderConstantB[idx] = targetStateBlock->vertexShaderConstantB[idx];
665 }
666
667 /* Pixel Shader Float Constants */
668 for (i = 0; i < This->num_contained_ps_consts_f; ++i)
669 {
670 unsigned int idx = This->contained_ps_consts_f[i];
671
672 TRACE("Setting %p from %p %u to {%.8e, %.8e, %.8e, %.8e}.\n",
673 This, targetStateBlock, idx,
674 targetStateBlock->pixelShaderConstantF[idx * 4 + 0],
675 targetStateBlock->pixelShaderConstantF[idx * 4 + 1],
676 targetStateBlock->pixelShaderConstantF[idx * 4 + 2],
677 targetStateBlock->pixelShaderConstantF[idx * 4 + 3]);
678
679 This->pixelShaderConstantF[idx * 4 + 0] = targetStateBlock->pixelShaderConstantF[idx * 4 + 0];
680 This->pixelShaderConstantF[idx * 4 + 1] = targetStateBlock->pixelShaderConstantF[idx * 4 + 1];
681 This->pixelShaderConstantF[idx * 4 + 2] = targetStateBlock->pixelShaderConstantF[idx * 4 + 2];
682 This->pixelShaderConstantF[idx * 4 + 3] = targetStateBlock->pixelShaderConstantF[idx * 4 + 3];
683 }
684
685 /* Pixel Shader Integer Constants */
686 for (i = 0; i < This->num_contained_ps_consts_i; ++i)
687 {
688 unsigned int idx = This->contained_ps_consts_i[i];
689 TRACE("Setting %p from %p %u to {%d, %d, %d, %d}.\n",
690 This, targetStateBlock, idx,
691 targetStateBlock->pixelShaderConstantI[idx * 4 + 0],
692 targetStateBlock->pixelShaderConstantI[idx * 4 + 1],
693 targetStateBlock->pixelShaderConstantI[idx * 4 + 2],
694 targetStateBlock->pixelShaderConstantI[idx * 4 + 3]);
695
696 This->pixelShaderConstantI[idx * 4 + 0] = targetStateBlock->pixelShaderConstantI[idx * 4 + 0];
697 This->pixelShaderConstantI[idx * 4 + 1] = targetStateBlock->pixelShaderConstantI[idx * 4 + 1];
698 This->pixelShaderConstantI[idx * 4 + 2] = targetStateBlock->pixelShaderConstantI[idx * 4 + 2];
699 This->pixelShaderConstantI[idx * 4 + 3] = targetStateBlock->pixelShaderConstantI[idx * 4 + 3];
700 }
701
702 /* Pixel Shader Boolean Constants */
703 for (i = 0; i < This->num_contained_ps_consts_b; ++i)
704 {
705 unsigned int idx = This->contained_ps_consts_b[i];
706 TRACE("Setting %p from %p %u to %s.\n", This, targetStateBlock, idx,
707 targetStateBlock->pixelShaderConstantB[idx] ? "TRUE" : "FALSE");
708
709 This->pixelShaderConstantB[idx] = targetStateBlock->pixelShaderConstantB[idx];
710 }
711
712 /* Others + Render & Texture */
713 for (i = 0; i < This->num_contained_transform_states; ++i)
714 {
715 WINED3DTRANSFORMSTATETYPE transform = This->contained_transform_states[i];
716
717 TRACE("Updating transform %#x.\n", transform);
718
719 This->transforms[transform] = targetStateBlock->transforms[transform];
720 }
721
722 if (This->changed.primitive_type) This->gl_primitive_type = targetStateBlock->gl_primitive_type;
723
724 if (This->changed.indices
725 && ((This->pIndexData != targetStateBlock->pIndexData)
726 || (This->baseVertexIndex != targetStateBlock->baseVertexIndex)
727 || (This->IndexFmt != targetStateBlock->IndexFmt)))
728 {
729 TRACE("Updating pIndexData to %p, baseVertexIndex to %d.\n",
730 targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
731
732 if (targetStateBlock->pIndexData) IWineD3DBuffer_AddRef(targetStateBlock->pIndexData);
733 if (This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
734 This->pIndexData = targetStateBlock->pIndexData;
735 This->baseVertexIndex = targetStateBlock->baseVertexIndex;
736 This->IndexFmt = targetStateBlock->IndexFmt;
737 }
738
739 if (This->changed.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl)
740 {
741 TRACE("Updating vertex declaration from %p to %p.\n", This->vertexDecl, targetStateBlock->vertexDecl);
742
743 if (targetStateBlock->vertexDecl) IWineD3DVertexDeclaration_AddRef(targetStateBlock->vertexDecl);
744 if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
745 This->vertexDecl = targetStateBlock->vertexDecl;
746 }
747
748 if (This->changed.material
749 && memcmp(&targetStateBlock->material, &This->material, sizeof(This->material)))
750 {
751 TRACE("Updating material.\n");
752
753 This->material = targetStateBlock->material;
754 }
755
756 if (This->changed.viewport
757 && memcmp(&targetStateBlock->viewport, &This->viewport, sizeof(This->viewport)))
758 {
759 TRACE("Updating viewport.\n");
760
761 This->viewport = targetStateBlock->viewport;
762 }
763
764 if(This->changed.scissorRect
765 && memcmp(&targetStateBlock->scissorRect, &This->scissorRect, sizeof(This->scissorRect)))
766 {
767 TRACE("Updating scissor rect.\n");
768
769 This->scissorRect = targetStateBlock->scissorRect;
770 }
771
772 map = This->changed.streamSource;
773 for (i = 0; map; map >>= 1, ++i)
774 {
775 if (!(map & 1)) continue;
776
777 if (This->streamStride[i] != targetStateBlock->streamStride[i]
778 || This->streamSource[i] != targetStateBlock->streamSource[i])
779 {
780 TRACE("Updating stream source %u to %p, stride to %u.\n",
781 i, targetStateBlock->streamSource[i], targetStateBlock->streamStride[i]);
782
783 This->streamStride[i] = targetStateBlock->streamStride[i];
784 if (targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
785 if (This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
786 This->streamSource[i] = targetStateBlock->streamSource[i];
787 }
788 }
789
790 map = This->changed.streamFreq;
791 for (i = 0; map; map >>= 1, ++i)
792 {
793 if (!(map & 1)) continue;
794
795 if (This->streamFreq[i] != targetStateBlock->streamFreq[i]
796 || This->streamFlags[i] != targetStateBlock->streamFlags[i])
797 {
798 TRACE("Updating stream frequency %u to %u flags to %#x.\n",
799 i, targetStateBlock->streamFreq[i], targetStateBlock->streamFlags[i]);
800
801 This->streamFreq[i] = targetStateBlock->streamFreq[i];
802 This->streamFlags[i] = targetStateBlock->streamFlags[i];
803 }
804 }
805
806 map = This->changed.clipplane;
807 for (i = 0; map; map >>= 1, ++i)
808 {
809 if (!(map & 1)) continue;
810
811 if (memcmp(targetStateBlock->clipplane[i], This->clipplane[i], sizeof(*This->clipplane)))
812 {
813 TRACE("Updating clipplane %u.\n", i);
814 memcpy(This->clipplane[i], targetStateBlock->clipplane[i], sizeof(*This->clipplane));
815 }
816 }
817
818 /* Render */
819 for (i = 0; i < This->num_contained_render_states; ++i)
820 {
821 WINED3DRENDERSTATETYPE rs = This->contained_render_states[i];
822
823 TRACE("Updating renderState %#x to %u.\n", rs, targetStateBlock->renderState[rs]);
824
825 This->renderState[rs] = targetStateBlock->renderState[rs];
826 }
827
828 /* Texture states */
829 for (i = 0; i < This->num_contained_tss_states; ++i)
830 {
831 DWORD stage = This->contained_tss_states[i].stage;
832 DWORD state = This->contained_tss_states[i].state;
833
834 TRACE("Updating texturestage state %u, %u to %u (was %u).\n", stage, state,
835 targetStateBlock->textureState[stage][state], This->textureState[stage][state]);
836
837 This->textureState[stage][state] = targetStateBlock->textureState[stage][state];
838 }
839
840 /* Samplers */
841 map = This->changed.textures;
842 for (i = 0; map; map >>= 1, ++i)
843 {
844 if (!(map & 1)) continue;
845
846 TRACE("Updating texture %u to %p (was %p).\n", i, targetStateBlock->textures[i], This->textures[i]);
847
848 if (targetStateBlock->textures[i]) IWineD3DBaseTexture_AddRef(targetStateBlock->textures[i]);
849 if (This->textures[i]) IWineD3DBaseTexture_Release(This->textures[i]);
850 This->textures[i] = targetStateBlock->textures[i];
851 }
852
853 for (i = 0; i < This->num_contained_sampler_states; ++i)
854 {
855 DWORD stage = This->contained_sampler_states[i].stage;
856 DWORD state = This->contained_sampler_states[i].state;
857
858 TRACE("Updating sampler state %u, %u to %u (was %u).\n", stage, state,
859 targetStateBlock->samplerState[stage][state], This->samplerState[stage][state]);
860
861 This->samplerState[stage][state] = targetStateBlock->samplerState[stage][state];
862 }
863
864 if (This->changed.pixelShader && This->pixelShader != targetStateBlock->pixelShader)
865 {
866 if (targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
867 if (This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
868 This->pixelShader = targetStateBlock->pixelShader;
869 }
870
871 record_lights(This, targetStateBlock);
872
873 TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
874
875 return WINED3D_OK;
876}
877
878static void apply_lights(IWineD3DDevice *pDevice, const IWineD3DStateBlockImpl *This)
879{
880 UINT i;
881 for(i = 0; i < LIGHTMAP_SIZE; i++) {
882 struct list *e;
883
884 LIST_FOR_EACH(e, &This->lightMap[i])
885 {
886 const struct wined3d_light_info *light = LIST_ENTRY(e, struct wined3d_light_info, entry);
887
888 IWineD3DDevice_SetLight(pDevice, light->OriginalIndex, &light->OriginalParms);
889 IWineD3DDevice_SetLightEnable(pDevice, light->OriginalIndex, light->glIndex != -1);
890 }
891 }
892}
893
894static HRESULT WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface)
895{
896 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
897 IWineD3DDevice *pDevice = (IWineD3DDevice *)This->device;
898 unsigned int i;
899 DWORD map;
900
901 TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
902
903 TRACE("Blocktype: %d\n", This->blockType);
904
905 if (This->changed.vertexShader) IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
906
907 /* Vertex Shader Constants */
908 for (i = 0; i < This->num_contained_vs_consts_f; ++i)
909 {
910 IWineD3DDevice_SetVertexShaderConstantF(pDevice, This->contained_vs_consts_f[i],
911 This->vertexShaderConstantF + This->contained_vs_consts_f[i] * 4, 1);
912 }
913 for (i = 0; i < This->num_contained_vs_consts_i; ++i)
914 {
915 IWineD3DDevice_SetVertexShaderConstantI(pDevice, This->contained_vs_consts_i[i],
916 This->vertexShaderConstantI + This->contained_vs_consts_i[i] * 4, 1);
917 }
918 for (i = 0; i < This->num_contained_vs_consts_b; ++i)
919 {
920 IWineD3DDevice_SetVertexShaderConstantB(pDevice, This->contained_vs_consts_b[i],
921 This->vertexShaderConstantB + This->contained_vs_consts_b[i], 1);
922 }
923
924 apply_lights(pDevice, This);
925
926 if (This->changed.pixelShader) IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
927
928 /* Pixel Shader Constants */
929 for (i = 0; i < This->num_contained_ps_consts_f; ++i)
930 {
931 IWineD3DDevice_SetPixelShaderConstantF(pDevice, This->contained_ps_consts_f[i],
932 This->pixelShaderConstantF + This->contained_ps_consts_f[i] * 4, 1);
933 }
934 for (i = 0; i < This->num_contained_ps_consts_i; ++i)
935 {
936 IWineD3DDevice_SetPixelShaderConstantI(pDevice, This->contained_ps_consts_i[i],
937 This->pixelShaderConstantI + This->contained_ps_consts_i[i] * 4, 1);
938 }
939 for (i = 0; i < This->num_contained_ps_consts_b; ++i)
940 {
941 IWineD3DDevice_SetPixelShaderConstantB(pDevice, This->contained_ps_consts_b[i],
942 This->pixelShaderConstantB + This->contained_ps_consts_b[i], 1);
943 }
944
945 /* Render */
946 for (i = 0; i < This->num_contained_render_states; ++i)
947 {
948 IWineD3DDevice_SetRenderState(pDevice, This->contained_render_states[i],
949 This->renderState[This->contained_render_states[i]]);
950 }
951
952 /* Texture states */
953 for (i = 0; i < This->num_contained_tss_states; ++i)
954 {
955 DWORD stage = This->contained_tss_states[i].stage;
956 DWORD state = This->contained_tss_states[i].state;
957
958 IWineD3DDevice_SetTextureStageState(pDevice, stage, state, This->textureState[stage][state]);
959 }
960
961 /* Sampler states */
962 for (i = 0; i < This->num_contained_sampler_states; ++i)
963 {
964 DWORD stage = This->contained_sampler_states[i].stage;
965 DWORD state = This->contained_sampler_states[i].state;
966 DWORD value = This->samplerState[stage][state];
967
968 if (stage >= MAX_FRAGMENT_SAMPLERS) stage += WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS;
969 IWineD3DDevice_SetSamplerState(pDevice, stage, state, value);
970 }
971
972 for (i = 0; i < This->num_contained_transform_states; ++i)
973 {
974 IWineD3DDevice_SetTransform(pDevice, This->contained_transform_states[i],
975 &This->transforms[This->contained_transform_states[i]]);
976 }
977
978 if (This->changed.primitive_type)
979 {
980 This->device->updateStateBlock->changed.primitive_type = TRUE;
981 This->device->updateStateBlock->gl_primitive_type = This->gl_primitive_type;
982 }
983
984 if (This->changed.indices)
985 {
986 IWineD3DDevice_SetIndexBuffer(pDevice, This->pIndexData, This->IndexFmt);
987 IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
988 }
989
990 if (This->changed.vertexDecl && This->vertexDecl)
991 {
992 IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
993 }
994
995 if (This->changed.material)
996 {
997 IWineD3DDevice_SetMaterial(pDevice, &This->material);
998 }
999
1000 if (This->changed.viewport)
1001 {
1002 IWineD3DDevice_SetViewport(pDevice, &This->viewport);
1003 }
1004
1005 if (This->changed.scissorRect)
1006 {
1007 IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
1008 }
1009
1010 map = This->changed.streamSource;
1011 for (i = 0; map; map >>= 1, ++i)
1012 {
1013 if (map & 1) IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
1014 }
1015
1016 map = This->changed.streamFreq;
1017 for (i = 0; map; map >>= 1, ++i)
1018 {
1019 if (map & 1) IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
1020 }
1021
1022 map = This->changed.textures;
1023 for (i = 0; map; map >>= 1, ++i)
1024 {
1025 DWORD stage;
1026
1027 if (!(map & 1)) continue;
1028
1029 stage = i < MAX_FRAGMENT_SAMPLERS ? i : WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS;
1030 IWineD3DDevice_SetTexture(pDevice, stage, This->textures[i]);
1031 }
1032
1033 map = This->changed.clipplane;
1034 for (i = 0; map; map >>= 1, ++i)
1035 {
1036 float clip[4];
1037
1038 if (!(map & 1)) continue;
1039
1040 clip[0] = This->clipplane[i][0];
1041 clip[1] = This->clipplane[i][1];
1042 clip[2] = This->clipplane[i][2];
1043 clip[3] = This->clipplane[i][3];
1044 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
1045 }
1046
1047 This->device->stateBlock->lowest_disabled_stage = MAX_TEXTURES - 1;
1048 for (i = 0; i < MAX_TEXTURES - 1; ++i)
1049 {
1050 if (This->device->stateBlock->textureState[i][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE)
1051 {
1052 This->device->stateBlock->lowest_disabled_stage = i;
1053 break;
1054 }
1055 }
1056 TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
1057
1058 return WINED3D_OK;
1059}
1060
1061static HRESULT WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
1062 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
1063 IWineD3DDevice *device = (IWineD3DDevice *)This->device;
1064 IWineD3DDeviceImpl *ThisDevice = (IWineD3DDeviceImpl *)device;
1065 const struct wined3d_gl_info *gl_info = &ThisDevice->adapter->gl_info;
1066 union {
1067 WINED3DLINEPATTERN lp;
1068 DWORD d;
1069 } lp;
1070 union {
1071 float f;
1072 DWORD d;
1073 } tmpfloat;
1074 unsigned int i;
1075 IWineD3DSwapChain *swapchain;
1076 IWineD3DSurface *backbuffer;
1077 HRESULT hr;
1078
1079 /* Note this may have a large overhead but it should only be executed
1080 once, in order to initialize the complete state of the device and
1081 all opengl equivalents */
1082 TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, ThisDevice);
1083 /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
1084 This->blockType = WINED3DSBT_INIT;
1085
1086 /* Set some of the defaults for lights, transforms etc */
1087 memcpy(&This->transforms[WINED3DTS_PROJECTION], identity, sizeof(identity));
1088 memcpy(&This->transforms[WINED3DTS_VIEW], identity, sizeof(identity));
1089 for (i = 0; i < 256; ++i) {
1090 memcpy(&This->transforms[WINED3DTS_WORLDMATRIX(i)], identity, sizeof(identity));
1091 }
1092
1093 TRACE("Render states\n");
1094 /* Render states: */
1095 if (ThisDevice->auto_depth_stencil_buffer != NULL) {
1096 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, WINED3DZB_TRUE);
1097 } else {
1098 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, WINED3DZB_FALSE);
1099 }
1100 IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE, WINED3DFILL_SOLID);
1101 IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE, WINED3DSHADE_GOURAUD);
1102 lp.lp.wRepeatFactor = 0;
1103 lp.lp.wLinePattern = 0;
1104 IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN, lp.d);
1105 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE, TRUE);
1106 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE, FALSE);
1107 IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL, TRUE);
1108 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND, WINED3DBLEND_ONE);
1109 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND, WINED3DBLEND_ZERO);
1110 IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE, WINED3DCULL_CCW);
1111 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC, WINED3DCMP_LESSEQUAL);
1112 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC, WINED3DCMP_ALWAYS);
1113 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF, 0);
1114 IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE, FALSE);
1115 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
1116 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE, FALSE);
1117 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE, FALSE);
1118 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE, 0);
1119 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR, 0);
1120 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE, WINED3DFOG_NONE);
1121 tmpfloat.f = 0.0f;
1122 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART, tmpfloat.d);
1123 tmpfloat.f = 1.0f;
1124 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND, tmpfloat.d);
1125 tmpfloat.f = 1.0f;
1126 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY, tmpfloat.d);
1127 IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS, FALSE);
1128 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS, 0);
1129 IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE, FALSE);
1130 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE, FALSE);
1131 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL, WINED3DSTENCILOP_KEEP);
1132 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL, WINED3DSTENCILOP_KEEP);
1133 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS, WINED3DSTENCILOP_KEEP);
1134 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF, 0);
1135 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK, 0xFFFFFFFF);
1136 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC, WINED3DCMP_ALWAYS);
1137 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
1138 IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR, 0xFFFFFFFF);
1139 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
1140 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
1141 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
1142 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
1143 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
1144 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
1145 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
1146 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
1147 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING, TRUE);
1148 IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING, TRUE);
1149 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT, 0);
1150 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE, WINED3DFOG_NONE);
1151 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX, TRUE);
1152 IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER, TRUE);
1153 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS, FALSE);
1154 IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE, WINED3DMCS_COLOR1);
1155 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE, WINED3DMCS_COLOR2);
1156 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE, WINED3DMCS_MATERIAL);
1157 IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE, WINED3DMCS_MATERIAL);
1158 IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND, WINED3DVBF_DISABLE);
1159 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE, 0);
1160 IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
1161 tmpfloat.f = 1.0f;
1162 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE, tmpfloat.d);
1163 tmpfloat.f = 1.0f;
1164 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN, tmpfloat.d);
1165 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE, FALSE);
1166 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE, FALSE);
1167 tmpfloat.f = 1.0f;
1168 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A, tmpfloat.d);
1169 tmpfloat.f = 0.0f;
1170 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B, tmpfloat.d);
1171 tmpfloat.f = 0.0f;
1172 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C, tmpfloat.d);
1173 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS, TRUE);
1174 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1175 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE, WINED3DPATCHEDGE_DISCRETE);
1176 tmpfloat.f = 1.0f;
1177 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS, tmpfloat.d);
1178 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN, 0xbaadcafe);
1179 tmpfloat.f = gl_info->limits.pointsize_max;
1180 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX, tmpfloat.d);
1181 IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
1182 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE, 0x0000000F);
1183 tmpfloat.f = 0.0f;
1184 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR, tmpfloat.d);
1185 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP, WINED3DBLENDOP_ADD);
1186 IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE, WINED3DDEGREE_CUBIC);
1187 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE, WINED3DDEGREE_LINEAR);
1188 /* states new in d3d9 */
1189 IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE, FALSE);
1190 IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS, 0);
1191 tmpfloat.f = 1.0f;
1192 IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL, tmpfloat.d);
1193 IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL, tmpfloat.d);
1194 IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE, FALSE);
1195 tmpfloat.f = 0.0f;
1196 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X, tmpfloat.d);
1197 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y, tmpfloat.d);
1198 tmpfloat.f = 1.0f;
1199 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z, tmpfloat.d);
1200 tmpfloat.f = 0.0f;
1201 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W, tmpfloat.d);
1202 IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
1203 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE, FALSE);
1204 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL, WINED3DSTENCILOP_KEEP);
1205 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL, WINED3DSTENCILOP_KEEP);
1206 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS, WINED3DSTENCILOP_KEEP);
1207 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC, WINED3DCMP_ALWAYS);
1208 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1, 0x0000000F);
1209 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2, 0x0000000F);
1210 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3, 0x0000000F);
1211 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR, 0xFFFFFFFF);
1212 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE, 0);
1213 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS, 0);
1214 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8, 0);
1215 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9, 0);
1216 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
1217 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
1218 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
1219 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
1220 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
1221 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
1222 IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1223 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA, WINED3DBLEND_ONE);
1224 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA, WINED3DBLEND_ZERO);
1225 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA, WINED3DBLENDOP_ADD);
1226
1227 /* clipping status */
1228 This->clip_status.ClipUnion = 0;
1229 This->clip_status.ClipIntersection = 0xFFFFFFFF;
1230
1231 /* Texture Stage States - Put directly into state block, we will call function below */
1232 for (i = 0; i < MAX_TEXTURES; i++) {
1233 TRACE("Setting up default texture states for texture Stage %d\n", i);
1234 memcpy(&This->transforms[WINED3DTS_TEXTURE0 + i], identity, sizeof(identity));
1235 This->textureState[i][WINED3DTSS_COLOROP ] = (i==0)? WINED3DTOP_MODULATE : WINED3DTOP_DISABLE;
1236 This->textureState[i][WINED3DTSS_COLORARG1 ] = WINED3DTA_TEXTURE;
1237 This->textureState[i][WINED3DTSS_COLORARG2 ] = WINED3DTA_CURRENT;
1238 This->textureState[i][WINED3DTSS_ALPHAOP ] = (i==0)? WINED3DTOP_SELECTARG1 : WINED3DTOP_DISABLE;
1239 This->textureState[i][WINED3DTSS_ALPHAARG1 ] = WINED3DTA_TEXTURE;
1240 This->textureState[i][WINED3DTSS_ALPHAARG2 ] = WINED3DTA_CURRENT;
1241 This->textureState[i][WINED3DTSS_BUMPENVMAT00 ] = 0;
1242 This->textureState[i][WINED3DTSS_BUMPENVMAT01 ] = 0;
1243 This->textureState[i][WINED3DTSS_BUMPENVMAT10 ] = 0;
1244 This->textureState[i][WINED3DTSS_BUMPENVMAT11 ] = 0;
1245 This->textureState[i][WINED3DTSS_TEXCOORDINDEX ] = i;
1246 This->textureState[i][WINED3DTSS_BUMPENVLSCALE ] = 0;
1247 This->textureState[i][WINED3DTSS_BUMPENVLOFFSET ] = 0;
1248 This->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS ] = WINED3DTTFF_DISABLE;
1249 This->textureState[i][WINED3DTSS_COLORARG0 ] = WINED3DTA_CURRENT;
1250 This->textureState[i][WINED3DTSS_ALPHAARG0 ] = WINED3DTA_CURRENT;
1251 This->textureState[i][WINED3DTSS_RESULTARG ] = WINED3DTA_CURRENT;
1252 }
1253 This->lowest_disabled_stage = 1;
1254
1255 /* Sampler states*/
1256 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; i++) {
1257 TRACE("Setting up default samplers states for sampler %d\n", i);
1258 This->samplerState[i][WINED3DSAMP_ADDRESSU ] = WINED3DTADDRESS_WRAP;
1259 This->samplerState[i][WINED3DSAMP_ADDRESSV ] = WINED3DTADDRESS_WRAP;
1260 This->samplerState[i][WINED3DSAMP_ADDRESSW ] = WINED3DTADDRESS_WRAP;
1261 This->samplerState[i][WINED3DSAMP_BORDERCOLOR ] = 0x00;
1262 This->samplerState[i][WINED3DSAMP_MAGFILTER ] = WINED3DTEXF_POINT;
1263 This->samplerState[i][WINED3DSAMP_MINFILTER ] = WINED3DTEXF_POINT;
1264 This->samplerState[i][WINED3DSAMP_MIPFILTER ] = WINED3DTEXF_NONE;
1265 This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS ] = 0;
1266 This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL ] = 0;
1267 This->samplerState[i][WINED3DSAMP_MAXANISOTROPY ] = 1;
1268 This->samplerState[i][WINED3DSAMP_SRGBTEXTURE ] = 0;
1269 This->samplerState[i][WINED3DSAMP_ELEMENTINDEX ] = 0; /* TODO: Indicates which element of a multielement texture to use */
1270 This->samplerState[i][WINED3DSAMP_DMAPOFFSET ] = 0; /* TODO: Vertex offset in the presampled displacement map */
1271 }
1272
1273 for (i = 0; i < gl_info->limits.textures; ++i)
1274 {
1275 /* Note: This avoids calling SetTexture, so pretend it has been called */
1276 This->changed.textures |= 1 << i;
1277 This->textures[i] = NULL;
1278 }
1279
1280 /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1281 hr = IWineD3DDevice_GetSwapChain(device, 0, &swapchain);
1282 if( hr == WINED3D_OK && swapchain != NULL) {
1283 WINED3DVIEWPORT vp;
1284
1285 hr = IWineD3DSwapChain_GetBackBuffer(swapchain, 0, WINED3DBACKBUFFER_TYPE_MONO, &backbuffer);
1286 if (SUCCEEDED(hr) && backbuffer)
1287 {
1288 WINED3DSURFACE_DESC desc;
1289 RECT scissorrect;
1290
1291 IWineD3DSurface_GetDesc(backbuffer, &desc);
1292 IWineD3DSurface_Release(backbuffer);
1293
1294 /* Set the default scissor rect values */
1295 scissorrect.left = 0;
1296 scissorrect.right = desc.width;
1297 scissorrect.top = 0;
1298 scissorrect.bottom = desc.height;
1299 hr = IWineD3DDevice_SetScissorRect(device, &scissorrect);
1300 if (FAILED(hr)) ERR("This should never happen, expect rendering issues!\n");
1301 }
1302
1303 /* Set the default viewport */
1304 vp.X = 0;
1305 vp.Y = 0;
1306 vp.Width = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferWidth;
1307 vp.Height = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferHeight;
1308 vp.MinZ = 0.0f;
1309 vp.MaxZ = 1.0f;
1310 IWineD3DDevice_SetViewport(device, &vp);
1311
1312 IWineD3DSwapChain_Release(swapchain);
1313 }
1314
1315 TRACE("-----------------------> Device defaults now set up...\n");
1316 return WINED3D_OK;
1317}
1318
1319/**********************************************************
1320 * IWineD3DStateBlock VTbl follows
1321 **********************************************************/
1322
1323static const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
1324{
1325 /* IUnknown */
1326 IWineD3DStateBlockImpl_QueryInterface,
1327 IWineD3DStateBlockImpl_AddRef,
1328 IWineD3DStateBlockImpl_Release,
1329 /* IWineD3DStateBlock */
1330 IWineD3DStateBlockImpl_Capture,
1331 IWineD3DStateBlockImpl_Apply,
1332 IWineD3DStateBlockImpl_InitStartupStateBlock
1333};
1334
1335HRESULT stateblock_init(IWineD3DStateBlockImpl *stateblock, IWineD3DDeviceImpl *device, WINED3DSTATEBLOCKTYPE type)
1336{
1337 unsigned int i;
1338 HRESULT hr;
1339
1340 stateblock->lpVtbl = &IWineD3DStateBlock_Vtbl;
1341 stateblock->ref = 1;
1342 stateblock->device = device;
1343 stateblock->blockType = type;
1344
1345 for (i = 0; i < LIGHTMAP_SIZE; i++)
1346 {
1347 list_init(&stateblock->lightMap[i]);
1348 }
1349
1350 hr = stateblock_allocate_shader_constants(stateblock);
1351 if (FAILED(hr)) return hr;
1352
1353 /* The WINED3DSBT_INIT stateblock type is used during initialization to
1354 * produce a placeholder stateblock so other functions called can update a
1355 * state block. */
1356 if (type == WINED3DSBT_INIT || type == WINED3DSBT_RECORDED) return WINED3D_OK;
1357
1358 TRACE("Updating changed flags appropriate for type %#x.\n", type);
1359
1360 switch (type)
1361 {
1362 case WINED3DSBT_ALL:
1363 stateblock_init_lights(stateblock, device->stateBlock->lightMap);
1364 stateblock_savedstates_set_all(&stateblock->changed, device->d3d_vshader_constantF,
1365 device->d3d_pshader_constantF);
1366 break;
1367
1368 case WINED3DSBT_PIXELSTATE:
1369 stateblock_savedstates_set_pixel(&stateblock->changed, device->d3d_pshader_constantF);
1370 break;
1371
1372 case WINED3DSBT_VERTEXSTATE:
1373 stateblock_init_lights(stateblock, device->stateBlock->lightMap);
1374 stateblock_savedstates_set_vertex(&stateblock->changed, device->d3d_vshader_constantF);
1375 break;
1376
1377 default:
1378 FIXME("Unrecognized state block type %#x.\n", type);
1379 break;
1380 }
1381
1382 stateblock_init_contained_states(stateblock);
1383 IWineD3DStateBlockImpl_Capture((IWineD3DStateBlock *)stateblock);
1384
1385 return WINED3D_OK;
1386}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette