d3d11/tests: Add some tests for ID3DUserDefinedAnnotation.
[wine.git] / dlls / wined3d / device.c
blob71b5aaa5742720efca61f00bfb45346bd54142e5
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
29 #include "wined3d_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32 WINE_DECLARE_DEBUG_CHANNEL(winediag);
34 struct wined3d_matrix_3x3
36 float _11, _12, _13;
37 float _21, _22, _23;
38 float _31, _32, _33;
41 struct light_transformed
43 struct wined3d_color diffuse, specular, ambient;
44 struct wined3d_vec4 position;
45 struct wined3d_vec3 direction;
46 float range, falloff, c_att, l_att, q_att, cos_htheta, cos_hphi;
49 struct lights_settings
51 struct light_transformed lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
52 struct wined3d_color ambient_light;
53 struct wined3d_matrix modelview_matrix;
54 struct wined3d_matrix_3x3 normal_matrix;
55 struct wined3d_vec4 position_transformed;
57 float fog_start, fog_end, fog_density;
59 uint32_t point_light_count : 8;
60 uint32_t spot_light_count : 8;
61 uint32_t directional_light_count : 8;
62 uint32_t parallel_point_light_count : 8;
63 uint32_t lighting : 1;
64 uint32_t legacy_lighting : 1;
65 uint32_t normalise : 1;
66 uint32_t localviewer : 1;
67 uint32_t fog_coord_mode : 2;
68 uint32_t fog_mode : 2;
69 uint32_t padding : 24;
72 /* Define the default light parameters as specified by MSDN. */
73 const struct wined3d_light WINED3D_default_light =
75 WINED3D_LIGHT_DIRECTIONAL, /* Type */
76 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
77 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
78 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
79 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
80 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
81 0.0f, /* Range */
82 0.0f, /* Falloff */
83 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
84 0.0f, /* Theta */
85 0.0f /* Phi */
88 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
90 struct wined3d_context **new_array;
92 TRACE("Adding context %p.\n", context);
94 if (!device->shader_backend->shader_allocate_context_data(context))
96 ERR("Failed to allocate shader backend context data.\n");
97 return FALSE;
99 device->shader_backend->shader_init_context_state(context);
101 if (!device->adapter->fragment_pipe->allocate_context_data(context))
103 ERR("Failed to allocate fragment pipeline context data.\n");
104 device->shader_backend->shader_free_context_data(context);
105 return FALSE;
108 if (!(new_array = heap_realloc(device->contexts, sizeof(*new_array) * (device->context_count + 1))))
110 ERR("Failed to grow the context array.\n");
111 device->adapter->fragment_pipe->free_context_data(context);
112 device->shader_backend->shader_free_context_data(context);
113 return FALSE;
116 new_array[device->context_count++] = context;
117 device->contexts = new_array;
119 return TRUE;
122 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
124 struct wined3d_context **new_array;
125 BOOL found = FALSE;
126 UINT i;
128 TRACE("Removing context %p.\n", context);
130 device->adapter->fragment_pipe->free_context_data(context);
131 device->shader_backend->shader_free_context_data(context);
133 for (i = 0; i < device->context_count; ++i)
135 if (device->contexts[i] == context)
137 found = TRUE;
138 break;
142 if (!found)
144 ERR("Context %p doesn't exist in context array.\n", context);
145 return;
148 if (!--device->context_count)
150 heap_free(device->contexts);
151 device->contexts = NULL;
152 return;
155 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
156 if (!(new_array = heap_realloc(device->contexts, device->context_count * sizeof(*device->contexts))))
158 ERR("Failed to shrink context array. Oh well.\n");
159 return;
162 device->contexts = new_array;
165 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
167 ULONG refcount = InterlockedIncrement(&device->ref);
169 TRACE("%p increasing refcount to %u.\n", device, refcount);
171 return refcount;
174 static void device_free_so_desc(struct wine_rb_entry *entry, void *context)
176 struct wined3d_so_desc_entry *s = WINE_RB_ENTRY_VALUE(entry, struct wined3d_so_desc_entry, entry);
178 heap_free(s);
181 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
183 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
185 ERR("Leftover sampler %p.\n", sampler);
188 static void device_leftover_rasterizer_state(struct wine_rb_entry *entry, void *context)
190 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
192 ERR("Leftover rasterizer state %p.\n", state);
195 static void device_leftover_blend_state(struct wine_rb_entry *entry, void *context)
197 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
199 ERR("Leftover blend state %p.\n", blend_state);
202 static void device_leftover_depth_stencil_state(struct wine_rb_entry *entry, void *context)
204 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
206 ERR("Leftover depth/stencil state %p.\n", state);
209 void wined3d_device_cleanup(struct wined3d_device *device)
211 unsigned int i;
213 if (device->swapchain_count)
214 wined3d_device_uninit_3d(device);
216 wined3d_cs_destroy(device->cs);
218 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
220 heap_free(device->multistate_funcs[i]);
221 device->multistate_funcs[i] = NULL;
224 if (!list_empty(&device->resources))
226 struct wined3d_resource *resource;
228 ERR("Device released with resources still bound.\n");
230 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
232 ERR("Leftover resource %p with type %s (%#x).\n",
233 resource, debug_d3dresourcetype(resource->type), resource->type);
237 if (device->contexts)
238 ERR("Context array not freed!\n");
239 if (device->hardwareCursor)
240 DestroyCursor(device->hardwareCursor);
241 device->hardwareCursor = 0;
243 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
244 wine_rb_destroy(&device->rasterizer_states, device_leftover_rasterizer_state, NULL);
245 wine_rb_destroy(&device->blend_states, device_leftover_blend_state, NULL);
246 wine_rb_destroy(&device->depth_stencil_states, device_leftover_depth_stencil_state, NULL);
247 wine_rb_destroy(&device->so_descs, device_free_so_desc, NULL);
249 wined3d_decref(device->wined3d);
250 device->wined3d = NULL;
253 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
255 ULONG refcount = InterlockedDecrement(&device->ref);
257 TRACE("%p decreasing refcount to %u.\n", device, refcount);
259 if (!refcount)
261 wined3d_mutex_lock();
262 device->adapter->adapter_ops->adapter_destroy_device(device);
263 TRACE("Destroyed device %p.\n", device);
264 wined3d_mutex_unlock();
267 return refcount;
270 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
272 TRACE("device %p.\n", device);
274 return device->swapchain_count;
277 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
279 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
281 if (swapchain_idx >= device->swapchain_count)
283 WARN("swapchain_idx %u >= swapchain_count %u.\n",
284 swapchain_idx, device->swapchain_count);
285 return NULL;
288 return device->swapchains[swapchain_idx];
291 static void device_load_logo(struct wined3d_device *device, const char *filename)
293 struct wined3d_color_key color_key;
294 struct wined3d_resource_desc desc;
295 HBITMAP hbm;
296 BITMAP bm;
297 HRESULT hr;
298 HDC dcb = NULL, dcs = NULL;
300 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
302 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
303 return;
305 GetObjectA(hbm, sizeof(BITMAP), &bm);
307 if (!(dcb = CreateCompatibleDC(NULL)))
308 goto out;
309 SelectObject(dcb, hbm);
311 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
312 desc.format = WINED3DFMT_B5G6R5_UNORM;
313 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
314 desc.multisample_quality = 0;
315 desc.usage = WINED3DUSAGE_DYNAMIC;
316 desc.bind_flags = 0;
317 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
318 desc.width = bm.bmWidth;
319 desc.height = bm.bmHeight;
320 desc.depth = 1;
321 desc.size = 0;
322 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_GET_DC,
323 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
325 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
326 goto out;
329 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
331 wined3d_texture_decref(device->logo_texture);
332 device->logo_texture = NULL;
333 goto out;
335 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
336 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
338 color_key.color_space_low_value = 0;
339 color_key.color_space_high_value = 0;
340 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
342 out:
343 if (dcb) DeleteDC(dcb);
344 if (hbm) DeleteObject(hbm);
347 /* Context activation is done by the caller. */
348 static void wined3d_device_gl_create_dummy_textures(struct wined3d_device_gl *device_gl,
349 struct wined3d_context_gl *context_gl)
351 struct wined3d_dummy_textures *textures = &device_gl->dummy_textures;
352 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
353 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
354 unsigned int i;
355 DWORD color;
357 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
358 color = 0x000000ff;
359 else
360 color = 0x00000000;
362 /* Under DirectX you can sample even if no texture is bound, whereas
363 * OpenGL will only allow that when a valid texture is bound.
364 * We emulate this by creating dummy textures and binding them
365 * to each texture stage when the currently set D3D texture is NULL. */
366 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
368 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
369 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
370 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
371 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
372 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
374 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
375 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
376 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
377 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
378 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
380 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
382 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
383 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
384 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
385 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
386 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
389 if (gl_info->supported[EXT_TEXTURE3D])
391 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
392 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
393 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
394 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
395 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
398 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
400 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
401 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
402 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
403 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
405 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
406 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
410 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
412 DWORD cube_array_data[6];
414 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
415 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
416 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
417 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
418 cube_array_data[i] = color;
419 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
420 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
423 if (gl_info->supported[EXT_TEXTURE_ARRAY])
425 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
426 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
427 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
428 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
429 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
431 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
432 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
433 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
434 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
435 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
438 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
440 GLuint buffer;
442 GL_EXTCALL(glGenBuffers(1, &buffer));
443 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
444 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
445 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
447 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
448 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
449 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
450 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
451 GL_EXTCALL(glDeleteBuffers(1, &buffer));
454 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
456 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
457 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
458 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
459 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
461 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
462 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
463 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
464 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
466 if (gl_info->supported[ARB_CLEAR_TEXTURE])
468 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
469 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
471 else
473 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
477 checkGLcall("create dummy textures");
479 wined3d_context_gl_bind_dummy_textures(context_gl);
482 /* Context activation is done by the caller. */
483 static void wined3d_device_gl_destroy_dummy_textures(struct wined3d_device_gl *device_gl,
484 struct wined3d_context_gl *context_gl)
486 struct wined3d_dummy_textures *dummy_textures = &device_gl->dummy_textures;
487 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
489 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
491 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
492 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
495 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
496 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
498 if (gl_info->supported[EXT_TEXTURE_ARRAY])
500 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
501 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
504 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
505 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
507 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
508 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
510 if (gl_info->supported[EXT_TEXTURE3D])
511 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
513 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
514 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
516 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
517 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
519 checkGLcall("delete dummy textures");
521 memset(dummy_textures, 0, sizeof(*dummy_textures));
524 /* Context activation is done by the caller. */
525 void wined3d_device_create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
527 struct wined3d_sampler_desc desc;
528 HRESULT hr;
530 desc.address_u = WINED3D_TADDRESS_WRAP;
531 desc.address_v = WINED3D_TADDRESS_WRAP;
532 desc.address_w = WINED3D_TADDRESS_WRAP;
533 memset(desc.border_color, 0, sizeof(desc.border_color));
534 desc.mag_filter = WINED3D_TEXF_POINT;
535 desc.min_filter = WINED3D_TEXF_POINT;
536 desc.mip_filter = WINED3D_TEXF_NONE;
537 desc.lod_bias = 0.0f;
538 desc.min_lod = -1000.0f;
539 desc.max_lod = 1000.0f;
540 desc.mip_base_level = 0;
541 desc.max_anisotropy = 1;
542 desc.compare = FALSE;
543 desc.comparison_func = WINED3D_CMP_NEVER;
544 desc.srgb_decode = TRUE;
546 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
547 * instructions allow access to resources without using samplers.
548 * In GLSL, resources are always accessed through sampler or image variables. The default
549 * sampler object is used to emulate the direct resource access when there is no sampler state
550 * to use.
552 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
554 ERR("Failed to create default sampler, hr %#x.\n", hr);
555 device->default_sampler = NULL;
558 /* In D3D10+, a NULL sampler maps to the default sampler state. */
559 desc.address_u = WINED3D_TADDRESS_CLAMP;
560 desc.address_v = WINED3D_TADDRESS_CLAMP;
561 desc.address_w = WINED3D_TADDRESS_CLAMP;
562 desc.mag_filter = WINED3D_TEXF_LINEAR;
563 desc.min_filter = WINED3D_TEXF_LINEAR;
564 desc.mip_filter = WINED3D_TEXF_LINEAR;
565 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
567 ERR("Failed to create null sampler, hr %#x.\n", hr);
568 device->null_sampler = NULL;
572 void wined3d_device_destroy_default_samplers(struct wined3d_device *device)
574 wined3d_sampler_decref(device->default_sampler);
575 device->default_sampler = NULL;
576 wined3d_sampler_decref(device->null_sampler);
577 device->null_sampler = NULL;
580 static bool wined3d_null_image_vk_init(struct wined3d_image_vk *image, struct wined3d_context_vk *context_vk,
581 VkCommandBuffer vk_command_buffer, VkImageType type, unsigned int layer_count, unsigned int sample_count)
583 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
584 VkImageSubresourceRange range;
585 uint32_t flags = 0;
587 static const VkClearColorValue colour = {{0}};
589 TRACE("image %p, context_vk %p, vk_command_buffer %p, type %#x, layer_count %u, sample_count %u.\n",
590 image, context_vk, vk_command_buffer, type, layer_count, sample_count);
592 if (type == VK_IMAGE_TYPE_2D && layer_count >= 6)
593 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
595 if (!wined3d_context_vk_create_image(context_vk, type,
596 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_FORMAT_R8G8B8A8_UNORM,
597 1, 1, 1, sample_count, 1, layer_count, flags, image))
599 return false;
602 wined3d_context_vk_reference_image(context_vk, image);
604 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
605 range.baseMipLevel = 0;
606 range.levelCount = 1;
607 range.baseArrayLayer = 0;
608 range.layerCount = layer_count;
610 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
611 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
612 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, image->vk_image, &range);
614 VK_CALL(vkCmdClearColorImage(vk_command_buffer, image->vk_image,
615 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour, 1, &range));
617 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
618 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0,
619 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, image->vk_image, &range);
621 TRACE("Created NULL image 0x%s, memory 0x%s.\n",
622 wine_dbgstr_longlong(image->vk_image), wine_dbgstr_longlong(image->vk_memory));
624 return true;
627 bool wined3d_device_vk_create_null_resources(struct wined3d_device_vk *device_vk,
628 struct wined3d_context_vk *context_vk)
630 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
631 const struct wined3d_vk_info *vk_info;
632 const struct wined3d_format *format;
633 VkMemoryPropertyFlags memory_type;
634 VkCommandBuffer vk_command_buffer;
635 unsigned int sample_count = 2;
636 VkBufferUsageFlags usage;
638 format = wined3d_get_format(device_vk->d.adapter, WINED3DFMT_R8G8B8A8_UNORM, WINED3D_BIND_SHADER_RESOURCE);
639 while (sample_count && !(sample_count & format->multisample_types))
640 sample_count <<= 1;
642 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
644 ERR("Failed to get command buffer.\n");
645 return false;
648 vk_info = context_vk->vk_info;
650 usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
651 | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
652 memory_type = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
653 if (!wined3d_context_vk_create_bo(context_vk, 16, usage, memory_type, &r->bo))
654 return false;
655 VK_CALL(vkCmdFillBuffer(vk_command_buffer, r->bo.vk_buffer, r->bo.b.buffer_offset, r->bo.size, 0x00000000u));
656 r->buffer_info.buffer = r->bo.vk_buffer;
657 r->buffer_info.offset = r->bo.b.buffer_offset;
658 r->buffer_info.range = r->bo.size;
660 if (!wined3d_null_image_vk_init(&r->image_1d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_1D, 1, 1))
662 ERR("Failed to create 1D image.\n");
663 goto fail;
666 if (!wined3d_null_image_vk_init(&r->image_2d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 6, 1))
668 ERR("Failed to create 2D image.\n");
669 goto fail;
672 if (!wined3d_null_image_vk_init(&r->image_2dms, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 1, sample_count))
674 ERR("Failed to create 2D MSAA image.\n");
675 goto fail;
678 if (!wined3d_null_image_vk_init(&r->image_3d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_3D, 1, 1))
680 ERR("Failed to create 3D image.\n");
681 goto fail;
684 return true;
686 fail:
687 if (r->image_2dms.vk_image)
688 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
689 if (r->image_2d.vk_image)
690 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
691 if (r->image_1d.vk_image)
692 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
693 wined3d_context_vk_reference_bo(context_vk, &r->bo);
694 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
695 return false;
698 void wined3d_device_vk_destroy_null_resources(struct wined3d_device_vk *device_vk,
699 struct wined3d_context_vk *context_vk)
701 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
703 /* We don't track command buffer references to NULL resources. We easily
704 * could, but it doesn't seem worth it. */
705 wined3d_context_vk_reference_image(context_vk, &r->image_3d);
706 wined3d_context_vk_destroy_image(context_vk, &r->image_3d);
707 wined3d_context_vk_reference_image(context_vk, &r->image_2dms);
708 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
709 wined3d_context_vk_reference_image(context_vk, &r->image_2d);
710 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
711 wined3d_context_vk_reference_image(context_vk, &r->image_1d);
712 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
713 wined3d_context_vk_reference_bo(context_vk, &r->bo);
714 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
717 bool wined3d_device_vk_create_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
719 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
720 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
721 VkBufferViewCreateInfo buffer_create_info;
722 const struct wined3d_vk_info *vk_info;
723 VkImageViewCreateInfo view_desc;
724 VkResult vr;
726 vk_info = context_vk->vk_info;
728 buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
729 buffer_create_info.pNext = NULL;
730 buffer_create_info.flags = 0;
731 buffer_create_info.buffer = r->bo.vk_buffer;
732 buffer_create_info.format = VK_FORMAT_R32_UINT;
733 buffer_create_info.offset = r->bo.b.buffer_offset;
734 buffer_create_info.range = r->bo.size;
736 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
737 &buffer_create_info, NULL, &v->vk_view_buffer_uint))) < 0)
739 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
740 return false;
742 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_uint));
744 buffer_create_info.format = VK_FORMAT_R32G32B32A32_SFLOAT;
745 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
746 &buffer_create_info, NULL, &v->vk_view_buffer_float))) < 0)
748 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
749 goto fail;
751 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_float));
753 view_desc.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
754 view_desc.pNext = NULL;
755 view_desc.flags = 0;
756 view_desc.image = r->image_1d.vk_image;
757 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D;
758 view_desc.format = VK_FORMAT_R8G8B8A8_UNORM;
759 view_desc.components.r = VK_COMPONENT_SWIZZLE_ZERO;
760 view_desc.components.g = VK_COMPONENT_SWIZZLE_ZERO;
761 view_desc.components.b = VK_COMPONENT_SWIZZLE_ZERO;
762 view_desc.components.a = VK_COMPONENT_SWIZZLE_ZERO;
763 view_desc.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
764 view_desc.subresourceRange.baseMipLevel = 0;
765 view_desc.subresourceRange.levelCount = 1;
766 view_desc.subresourceRange.baseArrayLayer = 0;
767 view_desc.subresourceRange.layerCount = 1;
768 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d.imageView))) < 0)
770 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
771 goto fail;
773 v->vk_info_1d.sampler = VK_NULL_HANDLE;
774 v->vk_info_1d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
775 TRACE("Created 1D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d.imageView));
777 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D_ARRAY;
778 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d_array.imageView))) < 0)
780 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
781 goto fail;
783 v->vk_info_1d_array.sampler = VK_NULL_HANDLE;
784 v->vk_info_1d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
785 TRACE("Created 1D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d_array.imageView));
787 view_desc.image = r->image_2d.vk_image;
788 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
789 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d.imageView))) < 0)
791 ERR("Failed to create 2D image view, vr %s.\n", wined3d_debug_vkresult(vr));
792 goto fail;
794 v->vk_info_2d.sampler = VK_NULL_HANDLE;
795 v->vk_info_2d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
796 TRACE("Created 2D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d.imageView));
798 view_desc.image = r->image_2dms.vk_image;
799 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
800 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms.imageView))) < 0)
802 ERR("Failed to create 2D MSAA image view, vr %s.\n", wined3d_debug_vkresult(vr));
803 goto fail;
805 v->vk_info_2dms.sampler = VK_NULL_HANDLE;
806 v->vk_info_2dms.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
807 TRACE("Created 2D MSAA image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms.imageView));
809 view_desc.image = r->image_3d.vk_image;
810 view_desc.viewType = VK_IMAGE_VIEW_TYPE_3D;
811 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_3d.imageView))) < 0)
813 ERR("Failed to create 3D image view, vr %s.\n", wined3d_debug_vkresult(vr));
814 goto fail;
816 v->vk_info_3d.sampler = VK_NULL_HANDLE;
817 v->vk_info_3d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
818 TRACE("Created 3D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_3d.imageView));
820 view_desc.image = r->image_2d.vk_image;
821 view_desc.subresourceRange.layerCount = 6;
822 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
823 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube.imageView))) < 0)
825 ERR("Failed to create cube image view, vr %s.\n", wined3d_debug_vkresult(vr));
826 goto fail;
828 v->vk_info_cube.sampler = VK_NULL_HANDLE;
829 v->vk_info_cube.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
830 TRACE("Created cube image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube.imageView));
832 view_desc.subresourceRange.layerCount = 1;
833 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
834 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d_array.imageView))) < 0)
836 ERR("Failed to create 2D array image view, vr %s.\n", wined3d_debug_vkresult(vr));
837 goto fail;
839 v->vk_info_2d_array.sampler = VK_NULL_HANDLE;
840 v->vk_info_2d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
841 TRACE("Created 2D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d_array.imageView));
843 view_desc.image = r->image_2dms.vk_image;
844 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
845 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms_array.imageView))) < 0)
847 ERR("Failed to create 2D MSAA array image view, vr %s.\n", wined3d_debug_vkresult(vr));
848 goto fail;
850 v->vk_info_2dms_array.sampler = VK_NULL_HANDLE;
851 v->vk_info_2dms_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
852 TRACE("Created 2D MSAA array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms_array.imageView));
854 view_desc.image = r->image_2d.vk_image;
855 view_desc.subresourceRange.layerCount = 6;
856 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
857 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube_array.imageView))) < 0)
859 ERR("Failed to create cube array image view, vr %s.\n", wined3d_debug_vkresult(vr));
860 goto fail;
862 v->vk_info_cube_array.sampler = VK_NULL_HANDLE;
863 v->vk_info_cube_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
864 TRACE("Created cube array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube_array.imageView));
866 return true;
868 fail:
869 if (v->vk_info_cube_array.imageView)
870 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube_array.imageView, NULL));
871 if (v->vk_info_2d_array.imageView)
872 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d_array.imageView, NULL));
873 if (v->vk_info_cube.imageView)
874 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube.imageView, NULL));
875 if (v->vk_info_3d.imageView)
876 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_3d.imageView, NULL));
877 if (v->vk_info_2dms.imageView)
878 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2dms.imageView, NULL));
879 if (v->vk_info_2d.imageView)
880 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d.imageView, NULL));
881 if (v->vk_info_1d_array.imageView)
882 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d_array.imageView, NULL));
883 if (v->vk_info_1d.imageView)
884 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d.imageView, NULL));
885 if (v->vk_view_buffer_float)
886 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_float, NULL));
887 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_uint, NULL));
888 return false;
891 void wined3d_device_vk_destroy_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
893 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
894 uint64_t id = context_vk->current_command_buffer.id;
896 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube_array.imageView, id);
897 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms_array.imageView, id);
898 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d_array.imageView, id);
899 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube.imageView, id);
900 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_3d.imageView, id);
901 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms.imageView, id);
902 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d.imageView, id);
903 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d_array.imageView, id);
904 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d.imageView, id);
906 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_float, id);
907 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_uint, id);
910 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
912 unsigned int screensaver_active;
914 TRACE("device %p, window %p.\n", device, window);
916 if (!wined3d_register_window(NULL, window, device, 0))
918 ERR("Failed to register window %p.\n", window);
919 return E_FAIL;
922 InterlockedExchangePointer((void **)&device->focus_window, window);
923 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
924 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
925 if ((device->restore_screensaver = !!screensaver_active))
926 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
928 return WINED3D_OK;
931 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
933 TRACE("device %p.\n", device);
935 if (device->focus_window) wined3d_unregister_window(device->focus_window);
936 InterlockedExchangePointer((void **)&device->focus_window, NULL);
937 if (device->restore_screensaver)
939 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
940 device->restore_screensaver = FALSE;
944 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
946 struct wined3d_rendertarget_view *views[WINED3D_MAX_RENDER_TARGETS] = {0};
947 BOOL ds_enable = swapchain->state.desc.enable_auto_depth_stencil;
948 struct wined3d_device_context *context = &device->cs->c;
950 if (device->back_buffer_view)
951 views[0] = device->back_buffer_view;
952 wined3d_device_context_set_rendertarget_views(context, 0,
953 device->adapter->d3d_info.limits.max_rt_count, views, !!device->back_buffer_view);
955 wined3d_device_context_set_depth_stencil_view(context, ds_enable ? device->auto_depth_stencil_view : NULL);
958 void wined3d_device_gl_delete_opengl_contexts_cs(void *object)
960 struct wined3d_device_gl *device_gl = object;
961 struct wined3d_swapchain_gl *swapchain_gl;
962 struct wined3d_context_gl *context_gl;
963 struct wined3d_context *context;
964 struct wined3d_device *device;
965 struct wined3d_shader *shader;
967 TRACE("device %p.\n", device_gl);
969 device = &device_gl->d;
971 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
973 device->shader_backend->shader_destroy(shader);
976 context = context_acquire(device, NULL, 0);
977 context_gl = wined3d_context_gl(context);
978 device->blitter->ops->blitter_destroy(device->blitter, context);
979 device->shader_backend->shader_free_private(device, context);
980 wined3d_device_gl_destroy_dummy_textures(device_gl, context_gl);
981 context_release(context);
983 while (device->context_count)
985 if ((swapchain_gl = wined3d_swapchain_gl(device->contexts[0]->swapchain)))
986 wined3d_swapchain_gl_destroy_contexts(swapchain_gl);
987 else
988 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
992 void wined3d_device_gl_create_primary_opengl_context_cs(void *object)
994 struct wined3d_device_gl *device_gl = object;
995 struct wined3d_context_gl *context_gl;
996 struct wined3d_swapchain *swapchain;
997 struct wined3d_context *context;
998 struct wined3d_texture *target;
999 struct wined3d_device *device;
1000 HRESULT hr;
1002 TRACE("device %p.\n", device_gl);
1004 device = &device_gl->d;
1005 swapchain = device->swapchains[0];
1006 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1007 if (!(context = context_acquire(device, target, 0)))
1009 WARN("Failed to acquire context.\n");
1010 return;
1013 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1014 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1016 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1017 context_release(context);
1018 return;
1021 if (!(device->blitter = wined3d_cpu_blitter_create()))
1023 ERR("Failed to create CPU blitter.\n");
1024 device->shader_backend->shader_free_private(device, NULL);
1025 context_release(context);
1026 return;
1029 context_gl = wined3d_context_gl(context);
1031 wined3d_ffp_blitter_create(&device->blitter, context_gl->gl_info);
1032 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1033 wined3d_arbfp_blitter_create(&device->blitter, device);
1034 wined3d_fbo_blitter_create(&device->blitter, context_gl->gl_info);
1035 wined3d_raw_blitter_create(&device->blitter, context_gl->gl_info);
1037 wined3d_device_gl_create_dummy_textures(device_gl, context_gl);
1038 wined3d_device_create_default_samplers(device, context);
1039 context_release(context);
1042 HRESULT wined3d_device_set_implicit_swapchain(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1044 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1045 const struct wined3d_swapchain_desc *swapchain_desc;
1046 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
1047 DWORD clear_flags = 0;
1048 unsigned int i;
1049 HRESULT hr;
1051 TRACE("device %p, swapchain %p.\n", device, swapchain);
1053 if (device->d3d_initialized)
1054 return WINED3DERR_INVALIDCALL;
1056 device->swapchain_count = 1;
1057 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1059 ERR("Failed to allocate swapchain array.\n");
1060 hr = E_OUTOFMEMORY;
1061 goto err_out;
1063 device->swapchains[0] = swapchain;
1065 for (i = 0; i < ARRAY_SIZE(fb->render_targets); ++i)
1067 if (fb->render_targets[i])
1068 wined3d_rtv_bind_count_dec(fb->render_targets[i]);
1070 memset(fb->render_targets, 0, sizeof(fb->render_targets));
1072 if (FAILED(hr = device->adapter->adapter_ops->adapter_init_3d(device)))
1073 goto err_out;
1074 device->d3d_initialized = TRUE;
1076 swapchain_desc = &swapchain->state.desc;
1077 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1079 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1080 struct wined3d_view_desc view_desc;
1082 view_desc.format_id = back_buffer->format->id;
1083 view_desc.flags = 0;
1084 view_desc.u.texture.level_idx = 0;
1085 view_desc.u.texture.level_count = 1;
1086 view_desc.u.texture.layer_idx = 0;
1087 view_desc.u.texture.layer_count = 1;
1088 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1089 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1091 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1092 device->adapter->adapter_ops->adapter_uninit_3d(device);
1093 device->d3d_initialized = FALSE;
1094 goto err_out;
1098 device_init_swapchain_state(device, swapchain);
1100 TRACE("All defaults now set up.\n");
1102 /* Clear the screen. */
1103 if (device->back_buffer_view)
1104 clear_flags |= WINED3DCLEAR_TARGET;
1105 if (swapchain_desc->enable_auto_depth_stencil)
1106 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1107 if (clear_flags)
1108 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1110 if (wined3d_settings.logo)
1111 device_load_logo(device, wined3d_settings.logo);
1113 return WINED3D_OK;
1115 err_out:
1116 heap_free(device->swapchains);
1117 device->swapchains = NULL;
1118 device->swapchain_count = 0;
1120 return hr;
1123 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1125 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1127 wined3d_sampler_decref(sampler);
1130 static void device_free_rasterizer_state(struct wine_rb_entry *entry, void *context)
1132 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
1134 wined3d_rasterizer_state_decref(state);
1137 static void device_free_blend_state(struct wine_rb_entry *entry, void *context)
1139 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
1141 wined3d_blend_state_decref(blend_state);
1144 static void device_free_depth_stencil_state(struct wine_rb_entry *entry, void *context)
1146 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
1148 wined3d_depth_stencil_state_decref(state);
1151 void wined3d_device_uninit_3d(struct wined3d_device *device)
1153 struct wined3d_state *state = device->cs->c.state;
1154 struct wined3d_resource *resource, *cursor;
1155 struct wined3d_rendertarget_view *view;
1156 struct wined3d_texture *texture;
1158 TRACE("device %p.\n", device);
1160 if (!device->d3d_initialized)
1162 ERR("Called while 3D support was not initialised.\n");
1163 return;
1166 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1168 device->swapchain_count = 0;
1170 if ((texture = device->logo_texture))
1172 device->logo_texture = NULL;
1173 wined3d_texture_decref(texture);
1176 if ((texture = device->cursor_texture))
1178 device->cursor_texture = NULL;
1179 wined3d_texture_decref(texture);
1182 wined3d_device_context_emit_reset_state(&device->cs->c, true);
1183 state_cleanup(state);
1185 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1186 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
1187 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
1188 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
1190 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1192 TRACE("Unloading resource %p.\n", resource);
1193 wined3d_cs_emit_unload_resource(device->cs, resource);
1196 device->adapter->adapter_ops->adapter_uninit_3d(device);
1197 device->d3d_initialized = FALSE;
1199 if ((view = device->auto_depth_stencil_view))
1201 device->auto_depth_stencil_view = NULL;
1202 if (wined3d_rendertarget_view_decref(view))
1203 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1206 if ((view = device->back_buffer_view))
1208 device->back_buffer_view = NULL;
1209 wined3d_rendertarget_view_decref(view);
1212 heap_free(device->swapchains);
1213 device->swapchains = NULL;
1215 wined3d_state_reset(state, &device->adapter->d3d_info);
1218 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1219 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1220 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1222 * There is no way to deactivate thread safety once it is enabled.
1224 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1226 TRACE("device %p.\n", device);
1228 /* For now just store the flag (needed in case of ddraw). */
1229 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1232 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1234 const struct wined3d_driver_info *driver_info;
1236 TRACE("device %p.\n", device);
1238 driver_info = &device->adapter->driver_info;
1240 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1241 wine_dbgstr_longlong(driver_info->vram_bytes),
1242 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1243 wine_dbgstr_longlong(driver_info->vram_bytes - device->adapter->vram_bytes_used));
1245 return min(UINT_MAX, driver_info->vram_bytes - device->adapter->vram_bytes_used);
1248 struct wined3d_buffer * CDECL wined3d_device_context_get_stream_output(struct wined3d_device_context *context,
1249 unsigned int idx, unsigned int *offset)
1251 TRACE("context %p, idx %u, offset %p.\n", context, idx, offset);
1253 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1255 WARN("Invalid stream output %u.\n", idx);
1256 return NULL;
1259 if (offset)
1260 *offset = context->state->stream_output[idx].offset;
1261 return context->state->stream_output[idx].buffer;
1264 HRESULT CDECL wined3d_device_context_get_stream_source(const struct wined3d_device_context *context,
1265 unsigned int stream_idx, struct wined3d_buffer **buffer, unsigned int *offset, unsigned int *stride)
1267 const struct wined3d_stream_state *stream;
1269 TRACE("context %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1270 context, stream_idx, buffer, offset, stride);
1272 if (stream_idx >= WINED3D_MAX_STREAMS)
1274 WARN("Stream index %u out of range.\n", stream_idx);
1275 return WINED3DERR_INVALIDCALL;
1278 stream = &context->state->streams[stream_idx];
1279 *buffer = stream->buffer;
1280 if (offset)
1281 *offset = stream->offset;
1282 *stride = stream->stride;
1284 return WINED3D_OK;
1287 static void wined3d_device_set_transform(struct wined3d_device *device,
1288 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1290 TRACE("device %p, state %s, matrix %p.\n",
1291 device, debug_d3dtstype(state), matrix);
1292 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1293 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1294 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1295 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1297 /* If the new matrix is the same as the current one,
1298 * we cut off any further processing. this seems to be a reasonable
1299 * optimization because as was noticed, some apps (warcraft3 for example)
1300 * tend towards setting the same matrix repeatedly for some reason.
1302 * From here on we assume that the new matrix is different, wherever it matters. */
1303 if (!memcmp(&device->cs->c.state->transforms[state], matrix, sizeof(*matrix)))
1305 TRACE("The application is setting the same matrix over again.\n");
1306 return;
1309 device->cs->c.state->transforms[state] = *matrix;
1310 wined3d_device_context_emit_set_transform(&device->cs->c, state, matrix);
1313 static void wined3d_device_get_transform(const struct wined3d_device *device,
1314 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1316 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1318 *matrix = device->cs->c.state->transforms[state];
1321 /* Note lights are real special cases. Although the device caps state only
1322 * e.g. 8 are supported, you can reference any indexes you want as long as
1323 * that number max are enabled at any one point in time. Therefore since the
1324 * indices can be anything, we need a hashmap of them. However, this causes
1325 * stateblock problems. When capturing the state block, I duplicate the
1326 * hashmap, but when recording, just build a chain pretty much of commands to
1327 * be replayed. */
1328 static void wined3d_device_context_set_light(struct wined3d_device_context *context,
1329 unsigned int light_idx, const struct wined3d_light *light)
1331 struct wined3d_light_info *object = NULL;
1332 float rho;
1334 if (FAILED(wined3d_light_state_set_light(&context->state->light_state, light_idx, light, &object)))
1335 return;
1337 /* Initialize the object. */
1338 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1339 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1340 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1341 light_idx, light->type, debug_color(&light->diffuse),
1342 debug_color(&light->specular), debug_color(&light->ambient),
1343 light->position.x, light->position.y, light->position.z,
1344 light->direction.x, light->direction.y, light->direction.z,
1345 light->range, light->falloff, light->theta, light->phi);
1347 switch (light->type)
1349 case WINED3D_LIGHT_POINT:
1350 /* Position */
1351 object->position.x = light->position.x;
1352 object->position.y = light->position.y;
1353 object->position.z = light->position.z;
1354 object->position.w = 1.0f;
1355 object->cutoff = 180.0f;
1356 /* FIXME: Range */
1357 break;
1359 case WINED3D_LIGHT_DIRECTIONAL:
1360 /* Direction */
1361 object->direction.x = -light->direction.x;
1362 object->direction.y = -light->direction.y;
1363 object->direction.z = -light->direction.z;
1364 object->direction.w = 0.0f;
1365 object->exponent = 0.0f;
1366 object->cutoff = 180.0f;
1367 break;
1369 case WINED3D_LIGHT_SPOT:
1370 /* Position */
1371 object->position.x = light->position.x;
1372 object->position.y = light->position.y;
1373 object->position.z = light->position.z;
1374 object->position.w = 1.0f;
1376 /* Direction */
1377 object->direction.x = light->direction.x;
1378 object->direction.y = light->direction.y;
1379 object->direction.z = light->direction.z;
1380 object->direction.w = 0.0f;
1382 /* opengl-ish and d3d-ish spot lights use too different models
1383 * for the light "intensity" as a function of the angle towards
1384 * the main light direction, so we only can approximate very
1385 * roughly. However, spot lights are rather rarely used in games
1386 * (if ever used at all). Furthermore if still used, probably
1387 * nobody pays attention to such details. */
1388 if (!light->falloff)
1390 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1391 * equations have the falloff resp. exponent parameter as an
1392 * exponent, so the spot light lighting will always be 1.0 for
1393 * both of them, and we don't have to care for the rest of the
1394 * rather complex calculation. */
1395 object->exponent = 0.0f;
1397 else
1399 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1400 if (rho < 0.0001f)
1401 rho = 0.0001f;
1402 object->exponent = -0.3f / logf(cosf(rho / 2));
1405 if (object->exponent > 128.0f)
1406 object->exponent = 128.0f;
1408 object->cutoff = (float)(light->phi * 90 / M_PI);
1409 /* FIXME: Range */
1410 break;
1412 case WINED3D_LIGHT_PARALLELPOINT:
1413 object->position.x = light->position.x;
1414 object->position.y = light->position.y;
1415 object->position.z = light->position.z;
1416 object->position.w = 1.0f;
1417 break;
1419 default:
1420 FIXME("Unrecognized light type %#x.\n", light->type);
1423 wined3d_device_context_emit_set_light(context, object);
1426 static void wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1428 struct wined3d_light_state *light_state = &device->cs->c.state->light_state;
1429 struct wined3d_light_info *light_info;
1431 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1433 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1434 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1436 TRACE("Light enabled requested but light not defined, so defining one!\n");
1437 wined3d_device_context_set_light(&device->cs->c, light_idx, &WINED3D_default_light);
1439 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1441 FIXME("Adding default lights has failed dismally\n");
1442 return;
1446 wined3d_light_state_enable_light(light_state, &device->adapter->d3d_info, light_info, enable);
1447 wined3d_device_context_emit_set_light_enable(&device->cs->c, light_idx, enable);
1450 static HRESULT wined3d_device_set_clip_plane(struct wined3d_device *device,
1451 UINT plane_idx, const struct wined3d_vec4 *plane)
1453 struct wined3d_vec4 *clip_planes = device->cs->c.state->clip_planes;
1455 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1457 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1459 TRACE("Application has requested clipplane this device doesn't support.\n");
1460 return WINED3DERR_INVALIDCALL;
1463 if (!memcmp(&clip_planes[plane_idx], plane, sizeof(*plane)))
1465 TRACE("Application is setting old values over, nothing to do.\n");
1466 return WINED3D_OK;
1469 clip_planes[plane_idx] = *plane;
1471 wined3d_device_context_emit_set_clip_plane(&device->cs->c, plane_idx, plane);
1473 return WINED3D_OK;
1476 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1477 const struct wined3d_clip_status *clip_status)
1479 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1481 if (!clip_status)
1482 return WINED3DERR_INVALIDCALL;
1484 return WINED3D_OK;
1487 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1488 struct wined3d_clip_status *clip_status)
1490 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1492 if (!clip_status)
1493 return WINED3DERR_INVALIDCALL;
1495 return WINED3D_OK;
1498 static void wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1500 TRACE("device %p, material %p.\n", device, material);
1502 device->cs->c.state->material = *material;
1503 wined3d_device_context_emit_set_material(&device->cs->c, material);
1506 struct wined3d_buffer * CDECL wined3d_device_context_get_index_buffer(const struct wined3d_device_context *context,
1507 enum wined3d_format_id *format, unsigned int *offset)
1509 const struct wined3d_state *state = context->state;
1511 TRACE("context %p, format %p, offset %p.\n", context, format, offset);
1513 *format = state->index_format;
1514 if (offset)
1515 *offset = state->index_offset;
1516 return state->index_buffer;
1519 static void wined3d_device_set_base_vertex_index(struct wined3d_device *device, int base_index)
1521 TRACE("device %p, base_index %d.\n", device, base_index);
1523 device->cs->c.state->base_vertex_index = base_index;
1526 void CDECL wined3d_device_context_get_viewports(const struct wined3d_device_context *context,
1527 unsigned int *viewport_count, struct wined3d_viewport *viewports)
1529 const struct wined3d_state *state = context->state;
1530 unsigned int count;
1532 TRACE("context %p, viewport_count %p, viewports %p.\n", context, viewport_count, viewports);
1534 count = viewport_count ? min(*viewport_count, state->viewport_count) : 1;
1535 if (count && viewports)
1536 memcpy(viewports, state->viewports, count * sizeof(*viewports));
1537 if (viewport_count)
1538 *viewport_count = state->viewport_count;
1541 static void resolve_depth_buffer(struct wined3d_device *device)
1543 const struct wined3d_state *state = device->cs->c.state;
1544 struct wined3d_rendertarget_view *src_view;
1545 struct wined3d_resource *dst_resource;
1546 struct wined3d_texture *dst_texture;
1548 if (!(dst_texture = state->textures[0]))
1549 return;
1550 dst_resource = &dst_texture->resource;
1551 if (!dst_resource->format->depth_size)
1552 return;
1553 if (!(src_view = state->fb.depth_stencil))
1554 return;
1556 wined3d_device_context_resolve_sub_resource(&device->cs->c, dst_resource, 0,
1557 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1560 struct wined3d_blend_state * CDECL wined3d_device_context_get_blend_state(const struct wined3d_device_context *context,
1561 struct wined3d_color *blend_factor, unsigned int *sample_mask)
1563 const struct wined3d_state *state = context->state;
1565 TRACE("context %p, blend_factor %p, sample_mask %p.\n", context, blend_factor, sample_mask);
1567 *blend_factor = state->blend_factor;
1568 *sample_mask = state->sample_mask;
1569 return state->blend_state;
1572 struct wined3d_depth_stencil_state * CDECL wined3d_device_context_get_depth_stencil_state(
1573 const struct wined3d_device_context *context, unsigned int *stencil_ref)
1575 const struct wined3d_state *state = context->state;
1577 TRACE("context %p, stencil_ref %p.\n", context, stencil_ref);
1579 *stencil_ref = state->stencil_ref;
1580 return state->depth_stencil_state;
1583 struct wined3d_rasterizer_state * CDECL wined3d_device_context_get_rasterizer_state(
1584 struct wined3d_device_context *context)
1586 TRACE("context %p.\n", context);
1588 return context->state->rasterizer_state;
1591 static void wined3d_device_set_render_state(struct wined3d_device *device,
1592 enum wined3d_render_state state, DWORD value)
1594 if (state > WINEHIGHEST_RENDER_STATE)
1596 WARN("Unhandled render state %#x.\n", state);
1597 return;
1600 if (value == device->cs->c.state->render_states[state])
1601 TRACE("Application is setting the old value over, nothing to do.\n");
1602 else
1604 device->cs->c.state->render_states[state] = value;
1605 wined3d_device_context_emit_set_render_state(&device->cs->c, state, value);
1608 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1610 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1611 resolve_depth_buffer(device);
1615 static void wined3d_device_set_sampler_state(struct wined3d_device *device,
1616 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1618 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1619 device, sampler_idx, debug_d3dsamplerstate(state), value);
1621 if (value == device->cs->c.state->sampler_states[sampler_idx][state])
1623 TRACE("Application is setting the old value over, nothing to do.\n");
1624 return;
1627 device->cs->c.state->sampler_states[sampler_idx][state] = value;
1628 wined3d_device_context_emit_set_sampler_state(&device->cs->c, sampler_idx, state, value);
1631 void CDECL wined3d_device_context_get_scissor_rects(const struct wined3d_device_context *context,
1632 unsigned int *rect_count, RECT *rects)
1634 const struct wined3d_state *state = context->state;
1635 unsigned int count;
1637 TRACE("context %p, rect_count %p, rects %p.\n", context, rect_count, rects);
1639 if (rects && (count = rect_count ? min(*rect_count, state->scissor_rect_count) : 1))
1640 memcpy(rects, state->scissor_rects, count * sizeof(*rects));
1641 if (rect_count)
1642 *rect_count = state->scissor_rect_count;
1645 void CDECL wined3d_device_context_reset_state(struct wined3d_device_context *context)
1647 TRACE("context %p.\n", context);
1649 wined3d_device_context_lock(context);
1650 state_cleanup(context->state);
1651 wined3d_state_reset(context->state, &context->device->adapter->d3d_info);
1652 wined3d_device_context_emit_reset_state(context, true);
1653 wined3d_device_context_unlock(context);
1656 void CDECL wined3d_device_context_set_state(struct wined3d_device_context *context, struct wined3d_state *state)
1658 const struct wined3d_light_info *light;
1659 unsigned int i, j;
1661 TRACE("context %p, state %p.\n", context, state);
1663 wined3d_device_context_lock(context);
1664 context->state = state;
1665 wined3d_device_context_emit_set_feature_level(context, state->feature_level);
1667 wined3d_device_context_emit_set_rendertarget_views(context, 0,
1668 ARRAY_SIZE(state->fb.render_targets), state->fb.render_targets);
1670 wined3d_device_context_emit_set_depth_stencil_view(context, state->fb.depth_stencil);
1671 wined3d_device_context_emit_set_vertex_declaration(context, state->vertex_declaration);
1673 wined3d_device_context_emit_set_stream_outputs(context, state->stream_output);
1675 wined3d_device_context_emit_set_stream_sources(context, 0, WINED3D_MAX_STREAMS, state->streams);
1677 wined3d_device_context_emit_set_index_buffer(context, state->index_buffer,
1678 state->index_format, state->index_offset);
1680 wined3d_device_context_emit_set_predication(context, state->predicate, state->predicate_value);
1682 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1684 wined3d_device_context_emit_set_shader(context, i, state->shader[i]);
1685 wined3d_device_context_emit_set_constant_buffers(context, i, 0, MAX_CONSTANT_BUFFERS, state->cb[i]);
1686 wined3d_device_context_emit_set_samplers(context, i, 0, MAX_SAMPLER_OBJECTS, state->sampler[i]);
1687 wined3d_device_context_emit_set_shader_resource_views(context, i, 0,
1688 MAX_SHADER_RESOURCE_VIEWS, state->shader_resource_view[i]);
1691 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
1692 wined3d_device_context_emit_set_unordered_access_views(context, i, 0, MAX_UNORDERED_ACCESS_VIEWS,
1693 state->unordered_access_view[i], NULL);
1695 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_F,
1696 0, WINED3D_MAX_VS_CONSTS_F, state->vs_consts_f);
1697 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_I,
1698 0, WINED3D_MAX_CONSTS_I, state->vs_consts_i);
1699 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_B,
1700 0, WINED3D_MAX_CONSTS_B, state->vs_consts_b);
1702 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_F,
1703 0, WINED3D_MAX_PS_CONSTS_F, state->ps_consts_f);
1704 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_I,
1705 0, WINED3D_MAX_CONSTS_I, state->ps_consts_i);
1706 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_B,
1707 0, WINED3D_MAX_CONSTS_B, state->ps_consts_b);
1709 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1711 wined3d_device_context_emit_set_texture(context, i, state->textures[i]);
1712 for (j = 0; j < WINED3D_HIGHEST_SAMPLER_STATE + 1; ++j)
1714 wined3d_device_context_emit_set_sampler_state(context, i, j, state->sampler_states[i][j]);
1718 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1720 for (j = 0; j < WINED3D_HIGHEST_TEXTURE_STATE + 1; ++j)
1722 wined3d_device_context_emit_set_texture_state(context, i, j, state->texture_states[i][j]);
1726 for (i = 0; i < WINED3D_HIGHEST_TRANSFORM_STATE + 1; ++i)
1728 if (context->device->state_table[STATE_TRANSFORM(i)].representative)
1729 wined3d_device_context_emit_set_transform(context, i, state->transforms + i);
1732 for (i = 0; i < WINED3D_MAX_CLIP_DISTANCES; ++i)
1734 wined3d_device_context_emit_set_clip_plane(context, i, state->clip_planes + i);
1737 wined3d_device_context_emit_set_material(context, &state->material);
1739 wined3d_device_context_emit_set_viewports(context, state->viewport_count, state->viewports);
1740 wined3d_device_context_emit_set_scissor_rects(context, state->scissor_rect_count, state->scissor_rects);
1742 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1744 LIST_FOR_EACH_ENTRY(light, &state->light_state.light_map[i], struct wined3d_light_info, entry)
1746 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
1747 wined3d_device_context_emit_set_light_enable(context, light->OriginalIndex, light->glIndex != -1);
1751 for (i = 0; i < WINEHIGHEST_RENDER_STATE + 1; ++i)
1753 if (context->device->state_table[STATE_RENDER(i)].representative)
1754 wined3d_device_context_emit_set_render_state(context, i, state->render_states[i]);
1757 wined3d_device_context_emit_set_blend_state(context, state->blend_state, &state->blend_factor, state->sample_mask);
1758 wined3d_device_context_emit_set_depth_stencil_state(context, state->depth_stencil_state, state->stencil_ref);
1759 wined3d_device_context_emit_set_rasterizer_state(context, state->rasterizer_state);
1760 wined3d_device_context_unlock(context);
1763 struct wined3d_state * CDECL wined3d_device_get_state(struct wined3d_device *device)
1765 TRACE("device %p.\n", device);
1767 return device->cs->c.state;
1770 struct wined3d_device_context * CDECL wined3d_device_get_immediate_context(struct wined3d_device *device)
1772 TRACE("device %p.\n", device);
1774 return &device->cs->c;
1777 struct wined3d_vertex_declaration * CDECL wined3d_device_context_get_vertex_declaration(
1778 const struct wined3d_device_context *context)
1780 TRACE("context %p.\n", context);
1782 return context->state->vertex_declaration;
1785 void CDECL wined3d_device_context_set_shader(struct wined3d_device_context *context,
1786 enum wined3d_shader_type type, struct wined3d_shader *shader)
1788 struct wined3d_state *state = context->state;
1789 struct wined3d_shader *prev;
1791 TRACE("context %p, type %#x, shader %p.\n", context, type, shader);
1793 wined3d_device_context_lock(context);
1794 prev = state->shader[type];
1795 if (shader == prev)
1796 goto out;
1798 if (shader)
1799 wined3d_shader_incref(shader);
1800 state->shader[type] = shader;
1801 wined3d_device_context_emit_set_shader(context, type, shader);
1802 if (prev)
1803 wined3d_shader_decref(prev);
1804 out:
1805 wined3d_device_context_unlock(context);
1808 struct wined3d_shader * CDECL wined3d_device_context_get_shader(const struct wined3d_device_context *context,
1809 enum wined3d_shader_type type)
1811 TRACE("context %p, type %#x.\n", context, type);
1813 return context->state->shader[type];
1816 void CDECL wined3d_device_context_set_constant_buffers(struct wined3d_device_context *context,
1817 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1818 const struct wined3d_constant_buffer_state *buffers)
1820 struct wined3d_state *state = context->state;
1821 unsigned int i;
1823 TRACE("context %p, type %#x, start_idx %u, count %u, buffers %p.\n", context, type, start_idx, count, buffers);
1825 if (!wined3d_bound_range(start_idx, count, MAX_CONSTANT_BUFFERS))
1827 WARN("Invalid constant buffer index %u, count %u.\n", start_idx, count);
1828 return;
1831 wined3d_device_context_lock(context);
1832 if (!memcmp(buffers, &state->cb[type][start_idx], count * sizeof(*buffers)))
1833 goto out;
1835 wined3d_device_context_emit_set_constant_buffers(context, type, start_idx, count, buffers);
1836 for (i = 0; i < count; ++i)
1838 struct wined3d_buffer *prev = state->cb[type][start_idx + i].buffer;
1839 struct wined3d_buffer *buffer = buffers[i].buffer;
1841 if (buffer)
1842 wined3d_buffer_incref(buffer);
1843 state->cb[type][start_idx + i] = buffers[i];
1844 if (prev)
1845 wined3d_buffer_decref(prev);
1847 out:
1848 wined3d_device_context_unlock(context);
1851 void CDECL wined3d_device_context_set_blend_state(struct wined3d_device_context *context,
1852 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1854 struct wined3d_state *state = context->state;
1855 struct wined3d_blend_state *prev;
1857 TRACE("context %p, blend_state %p, blend_factor %p, sample_mask %#x.\n",
1858 context, blend_state, blend_factor, sample_mask);
1860 wined3d_device_context_lock(context);
1861 prev = state->blend_state;
1862 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor))
1863 && sample_mask == state->sample_mask)
1864 goto out;
1866 if (blend_state)
1867 wined3d_blend_state_incref(blend_state);
1868 state->blend_state = blend_state;
1869 state->blend_factor = *blend_factor;
1870 state->sample_mask = sample_mask;
1871 wined3d_device_context_emit_set_blend_state(context, blend_state, blend_factor, sample_mask);
1872 if (prev)
1873 wined3d_blend_state_decref(prev);
1874 out:
1875 wined3d_device_context_unlock(context);
1878 void CDECL wined3d_device_context_set_depth_stencil_state(struct wined3d_device_context *context,
1879 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
1881 struct wined3d_state *state = context->state;
1882 struct wined3d_depth_stencil_state *prev;
1884 TRACE("context %p, depth_stencil_state %p, stencil_ref %u.\n", context, depth_stencil_state, stencil_ref);
1886 wined3d_device_context_lock(context);
1887 prev = state->depth_stencil_state;
1888 if (prev == depth_stencil_state && state->stencil_ref == stencil_ref)
1889 goto out;
1891 if (depth_stencil_state)
1892 wined3d_depth_stencil_state_incref(depth_stencil_state);
1893 state->depth_stencil_state = depth_stencil_state;
1894 state->stencil_ref = stencil_ref;
1895 wined3d_device_context_emit_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
1896 if (prev)
1897 wined3d_depth_stencil_state_decref(prev);
1898 out:
1899 wined3d_device_context_unlock(context);
1902 void CDECL wined3d_device_context_set_rasterizer_state(struct wined3d_device_context *context,
1903 struct wined3d_rasterizer_state *rasterizer_state)
1905 struct wined3d_state *state = context->state;
1906 struct wined3d_rasterizer_state *prev;
1908 TRACE("context %p, rasterizer_state %p.\n", context, rasterizer_state);
1910 wined3d_device_context_lock(context);
1911 prev = state->rasterizer_state;
1912 if (prev == rasterizer_state)
1913 goto out;
1915 if (rasterizer_state)
1916 wined3d_rasterizer_state_incref(rasterizer_state);
1917 state->rasterizer_state = rasterizer_state;
1918 wined3d_device_context_emit_set_rasterizer_state(context, rasterizer_state);
1919 if (prev)
1920 wined3d_rasterizer_state_decref(prev);
1921 out:
1922 wined3d_device_context_unlock(context);
1925 void CDECL wined3d_device_context_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
1926 const struct wined3d_viewport *viewports)
1928 struct wined3d_state *state = context->state;
1929 unsigned int i;
1931 TRACE("context %p, viewport_count %u, viewports %p.\n", context, viewport_count, viewports);
1933 for (i = 0; i < viewport_count; ++i)
1935 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
1936 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
1939 wined3d_device_context_lock(context);
1940 if (viewport_count)
1941 memcpy(state->viewports, viewports, viewport_count * sizeof(*viewports));
1942 else
1943 memset(state->viewports, 0, sizeof(state->viewports));
1944 state->viewport_count = viewport_count;
1946 wined3d_device_context_emit_set_viewports(context, viewport_count, viewports);
1947 wined3d_device_context_unlock(context);
1950 void CDECL wined3d_device_context_set_scissor_rects(struct wined3d_device_context *context, unsigned int rect_count,
1951 const RECT *rects)
1953 struct wined3d_state *state = context->state;
1954 unsigned int i;
1956 TRACE("context %p, rect_count %u, rects %p.\n", context, rect_count, rects);
1958 for (i = 0; i < rect_count; ++i)
1960 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
1963 wined3d_device_context_lock(context);
1964 if (state->scissor_rect_count == rect_count
1965 && !memcmp(state->scissor_rects, rects, rect_count * sizeof(*rects)))
1967 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
1968 goto out;
1971 if (rect_count)
1972 memcpy(state->scissor_rects, rects, rect_count * sizeof(*rects));
1973 else
1974 memset(state->scissor_rects, 0, sizeof(state->scissor_rects));
1975 state->scissor_rect_count = rect_count;
1977 wined3d_device_context_emit_set_scissor_rects(context, rect_count, rects);
1978 out:
1979 wined3d_device_context_unlock(context);
1982 void CDECL wined3d_device_context_set_shader_resource_views(struct wined3d_device_context *context,
1983 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1984 struct wined3d_shader_resource_view *const *const views)
1986 struct wined3d_shader_resource_view *real_views[MAX_SHADER_RESOURCE_VIEWS];
1987 struct wined3d_state *state = context->state;
1988 const struct wined3d_rendertarget_view *dsv = state->fb.depth_stencil;
1989 unsigned int i;
1991 TRACE("context %p, type %#x, start_idx %u, count %u, views %p.\n", context, type, start_idx, count, views);
1993 if (!wined3d_bound_range(start_idx, count, MAX_SHADER_RESOURCE_VIEWS))
1995 WARN("Invalid view index %u, count %u.\n", start_idx, count);
1996 return;
1999 wined3d_device_context_lock(context);
2000 if (!memcmp(views, &state->shader_resource_view[type][start_idx], count * sizeof(*views)))
2001 goto out;
2003 memcpy(real_views, views, count * sizeof(*views));
2005 for (i = 0; i < count; ++i)
2007 struct wined3d_shader_resource_view *view = real_views[i];
2009 if (view && (wined3d_is_srv_rtv_bound(state, view)
2010 || (dsv && dsv->resource == view->resource && wined3d_dsv_srv_conflict(dsv, view->format))))
2012 WARN("Application is trying to bind resource which is attached as render target.\n");
2013 real_views[i] = NULL;
2017 wined3d_device_context_emit_set_shader_resource_views(context, type, start_idx, count, real_views);
2018 for (i = 0; i < count; ++i)
2020 struct wined3d_shader_resource_view *prev = state->shader_resource_view[type][start_idx + i];
2021 struct wined3d_shader_resource_view *view = real_views[i];
2023 if (view)
2025 wined3d_shader_resource_view_incref(view);
2026 wined3d_srv_bind_count_inc(view);
2029 state->shader_resource_view[type][start_idx + i] = view;
2030 if (prev)
2032 wined3d_srv_bind_count_dec(prev);
2033 wined3d_shader_resource_view_decref(prev);
2036 out:
2037 wined3d_device_context_unlock(context);
2040 void CDECL wined3d_device_context_set_samplers(struct wined3d_device_context *context, enum wined3d_shader_type type,
2041 unsigned int start_idx, unsigned int count, struct wined3d_sampler *const *samplers)
2043 struct wined3d_state *state = context->state;
2044 unsigned int i;
2046 TRACE("context %p, type %#x, start_idx %u, count %u, samplers %p.\n", context, type, start_idx, count, samplers);
2048 if (!wined3d_bound_range(start_idx, count, MAX_SAMPLER_OBJECTS))
2050 WARN("Invalid sampler index %u, count %u.\n", start_idx, count);
2051 return;
2054 wined3d_device_context_lock(context);
2055 if (!memcmp(samplers, &state->sampler[type][start_idx], count * sizeof(*samplers)))
2056 goto out;
2058 wined3d_device_context_emit_set_samplers(context, type, start_idx, count, samplers);
2059 for (i = 0; i < count; ++i)
2061 struct wined3d_sampler *prev = state->sampler[type][start_idx + i];
2062 struct wined3d_sampler *sampler = samplers[i];
2064 if (sampler)
2065 wined3d_sampler_incref(sampler);
2066 state->sampler[type][start_idx + i] = sampler;
2067 if (prev)
2068 wined3d_sampler_decref(prev);
2070 out:
2071 wined3d_device_context_unlock(context);
2074 void CDECL wined3d_device_context_set_unordered_access_views(struct wined3d_device_context *context,
2075 enum wined3d_pipeline pipeline, unsigned int start_idx, unsigned int count,
2076 struct wined3d_unordered_access_view *const *uavs, const unsigned int *initial_counts)
2078 struct wined3d_state *state = context->state;
2079 unsigned int i;
2081 TRACE("context %p, pipeline %#x, start_idx %u, count %u, uavs %p, initial_counts %p.\n",
2082 context, pipeline, start_idx, count, uavs, initial_counts);
2084 if (!wined3d_bound_range(start_idx, count, MAX_UNORDERED_ACCESS_VIEWS))
2086 WARN("Invalid UAV index %u, count %u.\n", start_idx, count);
2087 return;
2090 wined3d_device_context_lock(context);
2091 if (!memcmp(uavs, &state->unordered_access_view[pipeline][start_idx], count * sizeof(*uavs)) && !initial_counts)
2092 goto out;
2094 wined3d_device_context_emit_set_unordered_access_views(context, pipeline, start_idx, count, uavs, initial_counts);
2095 for (i = 0; i < count; ++i)
2097 struct wined3d_unordered_access_view *prev = state->unordered_access_view[pipeline][start_idx + i];
2098 struct wined3d_unordered_access_view *uav = uavs[i];
2100 if (uav)
2101 wined3d_unordered_access_view_incref(uav);
2102 state->unordered_access_view[pipeline][start_idx + i] = uav;
2103 if (prev)
2104 wined3d_unordered_access_view_decref(prev);
2106 out:
2107 wined3d_device_context_unlock(context);
2110 void CDECL wined3d_device_context_set_render_targets_and_unordered_access_views(struct wined3d_device_context *context,
2111 unsigned int rtv_count, struct wined3d_rendertarget_view *const *render_target_views,
2112 struct wined3d_rendertarget_view *depth_stencil_view, UINT uav_count,
2113 struct wined3d_unordered_access_view *const *unordered_access_views, const unsigned int *initial_counts)
2115 wined3d_device_context_lock(context);
2116 if (rtv_count != ~0u)
2118 if (depth_stencil_view && !(depth_stencil_view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2120 WARN("View resource %p has incompatible %s bind flags.\n",
2121 depth_stencil_view->resource, wined3d_debug_bind_flags(depth_stencil_view->resource->bind_flags));
2122 goto out;
2125 if (FAILED(wined3d_device_context_set_rendertarget_views(context, 0, rtv_count,
2126 render_target_views, FALSE)))
2127 goto out;
2129 wined3d_device_context_set_depth_stencil_view(context, depth_stencil_view);
2132 if (uav_count != ~0u)
2134 wined3d_device_context_set_unordered_access_views(context, WINED3D_PIPELINE_GRAPHICS, 0, uav_count,
2135 unordered_access_views, initial_counts);
2137 out:
2138 wined3d_device_context_unlock(context);
2141 static void wined3d_device_context_unbind_srv_for_rtv(struct wined3d_device_context *context,
2142 const struct wined3d_rendertarget_view *view, BOOL dsv)
2144 const struct wined3d_state *state = context->state;
2145 const struct wined3d_resource *resource;
2147 if (!view)
2148 return;
2149 resource = view->resource;
2151 if (resource->srv_bind_count_device)
2153 const struct wined3d_shader_resource_view *srv;
2154 unsigned int i, j;
2156 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2158 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
2160 if ((srv = state->shader_resource_view[i][j]) && srv->resource == resource
2161 && ((!dsv && wined3d_is_srv_rtv_bound(state, srv))
2162 || (dsv && wined3d_dsv_srv_conflict(view, srv->format))))
2164 static struct wined3d_shader_resource_view *const null_srv;
2166 WARN("Application sets bound resource as render target.\n");
2167 wined3d_device_context_set_shader_resource_views(context, i, j, 1, &null_srv);
2174 HRESULT CDECL wined3d_device_context_set_rendertarget_views(struct wined3d_device_context *context,
2175 unsigned int start_idx, unsigned int count, struct wined3d_rendertarget_view *const *views, BOOL set_viewport)
2177 struct wined3d_state *state = context->state;
2178 unsigned int i, max_rt_count;
2180 TRACE("context %p, start_idx %u, count %u, views %p, set_viewport %#x.\n",
2181 context, start_idx, count, views, set_viewport);
2183 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
2184 if (start_idx >= max_rt_count)
2186 WARN("Only %u render targets are supported.\n", max_rt_count);
2187 return WINED3DERR_INVALIDCALL;
2189 count = min(count, max_rt_count - start_idx);
2191 for (i = 0; i < count; ++i)
2193 if (views[i] && !(views[i]->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
2195 WARN("View resource %p doesn't have render target bind flags.\n", views[i]->resource);
2196 return WINED3DERR_INVALIDCALL;
2200 wined3d_device_context_lock(context);
2201 /* Set the viewport and scissor rectangles, if requested. Tests show that
2202 * stateblock recording is ignored, the change goes directly into the
2203 * primary stateblock. */
2204 if (!start_idx && set_viewport)
2206 state->viewports[0].x = 0;
2207 state->viewports[0].y = 0;
2208 state->viewports[0].width = views[0]->width;
2209 state->viewports[0].height = views[0]->height;
2210 state->viewports[0].min_z = 0.0f;
2211 state->viewports[0].max_z = 1.0f;
2212 state->viewport_count = 1;
2213 wined3d_device_context_emit_set_viewports(context, 1, state->viewports);
2215 SetRect(&state->scissor_rects[0], 0, 0, views[0]->width, views[0]->height);
2216 state->scissor_rect_count = 1;
2217 wined3d_device_context_emit_set_scissor_rects(context, 1, state->scissor_rects);
2220 if (!memcmp(views, &state->fb.render_targets[start_idx], count * sizeof(*views)))
2221 goto out;
2223 wined3d_device_context_emit_set_rendertarget_views(context, start_idx, count, views);
2224 for (i = 0; i < count; ++i)
2226 struct wined3d_rendertarget_view *prev = state->fb.render_targets[start_idx + i];
2227 struct wined3d_rendertarget_view *view = views[i];
2229 if (view)
2231 wined3d_rendertarget_view_incref(view);
2232 wined3d_rtv_bind_count_inc(view);
2234 state->fb.render_targets[start_idx + i] = view;
2235 /* Release after the assignment, to prevent device_resource_released()
2236 * from seeing the resource as still in use. */
2237 if (prev)
2239 wined3d_rtv_bind_count_dec(prev);
2240 wined3d_rendertarget_view_decref(prev);
2243 wined3d_device_context_unbind_srv_for_rtv(context, view, FALSE);
2245 out:
2246 wined3d_device_context_unlock(context);
2247 return WINED3D_OK;
2250 HRESULT CDECL wined3d_device_context_set_depth_stencil_view(struct wined3d_device_context *context,
2251 struct wined3d_rendertarget_view *view)
2253 struct wined3d_fb_state *fb = &context->state->fb;
2254 struct wined3d_rendertarget_view *prev;
2256 TRACE("context %p, view %p.\n", context, view);
2258 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2260 WARN("View resource %p has incompatible %s bind flags.\n",
2261 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
2262 return WINED3DERR_INVALIDCALL;
2265 wined3d_device_context_lock(context);
2266 prev = fb->depth_stencil;
2267 if (prev == view)
2269 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
2270 goto out;
2273 if ((fb->depth_stencil = view))
2274 wined3d_rendertarget_view_incref(view);
2275 wined3d_device_context_emit_set_depth_stencil_view(context, view);
2276 if (prev)
2277 wined3d_rendertarget_view_decref(prev);
2278 wined3d_device_context_unbind_srv_for_rtv(context, view, TRUE);
2279 out:
2280 wined3d_device_context_unlock(context);
2281 return WINED3D_OK;
2284 void CDECL wined3d_device_context_set_predication(struct wined3d_device_context *context,
2285 struct wined3d_query *predicate, BOOL value)
2287 struct wined3d_state *state = context->state;
2288 struct wined3d_query *prev;
2290 TRACE("context %p, predicate %p, value %#x.\n", context, predicate, value);
2292 wined3d_device_context_lock(context);
2293 prev = state->predicate;
2294 if (predicate)
2296 FIXME("Predicated rendering not implemented.\n");
2297 wined3d_query_incref(predicate);
2299 state->predicate = predicate;
2300 state->predicate_value = value;
2301 wined3d_device_context_emit_set_predication(context, predicate, value);
2302 if (prev)
2303 wined3d_query_decref(prev);
2304 wined3d_device_context_unlock(context);
2307 HRESULT CDECL wined3d_device_context_set_stream_sources(struct wined3d_device_context *context,
2308 unsigned int start_idx, unsigned int count, const struct wined3d_stream_state *streams)
2310 struct wined3d_state *state = context->state;
2311 unsigned int i;
2313 TRACE("context %p, start_idx %u, count %u, streams %p.\n", context, start_idx, count, streams);
2315 if (start_idx >= WINED3D_MAX_STREAMS)
2317 WARN("Start index %u is out of range.\n", start_idx);
2318 return WINED3DERR_INVALIDCALL;
2321 count = min(count, WINED3D_MAX_STREAMS - start_idx);
2323 for (i = 0; i < count; ++i)
2325 if (streams[i].offset & 0x3)
2327 WARN("Offset %u is not 4 byte aligned.\n", streams[i].offset);
2328 return WINED3DERR_INVALIDCALL;
2332 wined3d_device_context_lock(context);
2333 if (!memcmp(streams, &state->streams[start_idx], count * sizeof(*streams)))
2334 goto out;
2336 wined3d_device_context_emit_set_stream_sources(context, start_idx, count, streams);
2337 for (i = 0; i < count; ++i)
2339 struct wined3d_buffer *prev = state->streams[start_idx + i].buffer;
2340 struct wined3d_buffer *buffer = streams[i].buffer;
2342 state->streams[start_idx + i] = streams[i];
2344 if (buffer)
2345 wined3d_buffer_incref(buffer);
2346 if (prev)
2347 wined3d_buffer_decref(prev);
2349 out:
2350 wined3d_device_context_unlock(context);
2351 return WINED3D_OK;
2354 void CDECL wined3d_device_context_set_index_buffer(struct wined3d_device_context *context,
2355 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
2357 struct wined3d_state *state = context->state;
2358 enum wined3d_format_id prev_format;
2359 struct wined3d_buffer *prev_buffer;
2360 unsigned int prev_offset;
2362 TRACE("context %p, buffer %p, format %s, offset %u.\n",
2363 context, buffer, debug_d3dformat(format_id), offset);
2365 wined3d_device_context_lock(context);
2366 prev_buffer = state->index_buffer;
2367 prev_format = state->index_format;
2368 prev_offset = state->index_offset;
2370 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
2371 goto out;
2373 if (buffer)
2374 wined3d_buffer_incref(buffer);
2375 state->index_buffer = buffer;
2376 state->index_format = format_id;
2377 state->index_offset = offset;
2378 wined3d_device_context_emit_set_index_buffer(context, buffer, format_id, offset);
2379 if (prev_buffer)
2380 wined3d_buffer_decref(prev_buffer);
2381 out:
2382 wined3d_device_context_unlock(context);
2385 void CDECL wined3d_device_context_set_vertex_declaration(struct wined3d_device_context *context,
2386 struct wined3d_vertex_declaration *declaration)
2388 struct wined3d_state *state = context->state;
2389 struct wined3d_vertex_declaration *prev;
2391 TRACE("context %p, declaration %p.\n", context, declaration);
2393 wined3d_device_context_lock(context);
2394 prev = state->vertex_declaration;
2395 if (declaration == prev)
2396 goto out;
2398 if (declaration)
2399 wined3d_vertex_declaration_incref(declaration);
2400 state->vertex_declaration = declaration;
2401 wined3d_device_context_emit_set_vertex_declaration(context, declaration);
2402 if (prev)
2403 wined3d_vertex_declaration_decref(prev);
2404 out:
2405 wined3d_device_context_unlock(context);
2408 void CDECL wined3d_device_context_set_stream_outputs(struct wined3d_device_context *context,
2409 const struct wined3d_stream_output outputs[WINED3D_MAX_STREAM_OUTPUT_BUFFERS])
2411 struct wined3d_state *state = context->state;
2412 unsigned int i;
2414 TRACE("context %p, outputs %p.\n", context, outputs);
2416 wined3d_device_context_lock(context);
2417 wined3d_device_context_emit_set_stream_outputs(context, outputs);
2418 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
2420 struct wined3d_buffer *prev_buffer = state->stream_output[i].buffer;
2421 struct wined3d_buffer *buffer = outputs[i].buffer;
2423 if (buffer)
2424 wined3d_buffer_incref(buffer);
2425 state->stream_output[i] = outputs[i];
2426 if (prev_buffer)
2427 wined3d_buffer_decref(prev_buffer);
2429 wined3d_device_context_unlock(context);
2432 void CDECL wined3d_device_context_draw(struct wined3d_device_context *context, unsigned int start_vertex,
2433 unsigned int vertex_count, unsigned int start_instance, unsigned int instance_count)
2435 struct wined3d_state *state = context->state;
2437 TRACE("context %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
2438 context, start_vertex, vertex_count, start_instance, instance_count);
2440 wined3d_device_context_lock(context);
2441 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2442 0, start_vertex, vertex_count, start_instance, instance_count, false);
2443 wined3d_device_context_unlock(context);
2446 void CDECL wined3d_device_context_draw_indexed(struct wined3d_device_context *context, int base_vertex_index,
2447 unsigned int start_index, unsigned int index_count, unsigned int start_instance, unsigned int instance_count)
2449 struct wined3d_state *state = context->state;
2451 TRACE("context %p, base_vertex_index %d, start_index %u, index_count %u, start_instance %u, instance_count %u.\n",
2452 context, base_vertex_index, start_index, index_count, start_instance, instance_count);
2454 wined3d_device_context_lock(context);
2455 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2456 base_vertex_index, start_index, index_count, start_instance, instance_count, true);
2457 wined3d_device_context_unlock(context);
2460 void CDECL wined3d_device_context_get_constant_buffer(const struct wined3d_device_context *context,
2461 enum wined3d_shader_type shader_type, unsigned int idx, struct wined3d_constant_buffer_state *state)
2463 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2465 if (idx >= MAX_CONSTANT_BUFFERS)
2467 WARN("Invalid constant buffer index %u.\n", idx);
2468 return;
2471 *state = context->state->cb[shader_type][idx];
2474 struct wined3d_shader_resource_view * CDECL wined3d_device_context_get_shader_resource_view(
2475 const struct wined3d_device_context *context, enum wined3d_shader_type shader_type, unsigned int idx)
2477 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2479 WARN("Invalid view index %u.\n", idx);
2480 return NULL;
2483 return context->state->shader_resource_view[shader_type][idx];
2486 struct wined3d_sampler * CDECL wined3d_device_context_get_sampler(const struct wined3d_device_context *context,
2487 enum wined3d_shader_type shader_type, unsigned int idx)
2489 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2491 if (idx >= MAX_SAMPLER_OBJECTS)
2493 WARN("Invalid sampler index %u.\n", idx);
2494 return NULL;
2497 return context->state->sampler[shader_type][idx];
2500 static void wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2501 unsigned int start_idx, unsigned int count, const BOOL *constants)
2503 unsigned int i;
2505 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2506 device, start_idx, count, constants);
2508 memcpy(&device->cs->c.state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2509 if (TRACE_ON(d3d))
2511 for (i = 0; i < count; ++i)
2512 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2515 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2518 static void wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2519 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2521 unsigned int i;
2523 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2524 device, start_idx, count, constants);
2526 memcpy(&device->cs->c.state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2527 if (TRACE_ON(d3d))
2529 for (i = 0; i < count; ++i)
2530 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2533 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2536 static void wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2537 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2539 unsigned int i;
2541 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2542 device, start_idx, count, constants);
2544 memcpy(&device->cs->c.state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2545 if (TRACE_ON(d3d))
2547 for (i = 0; i < count; ++i)
2548 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2551 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2554 static void wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2555 unsigned int start_idx, unsigned int count, const BOOL *constants)
2557 unsigned int i;
2559 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2560 device, start_idx, count, constants);
2562 memcpy(&device->cs->c.state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2563 if (TRACE_ON(d3d))
2565 for (i = 0; i < count; ++i)
2566 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2569 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2572 static void wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2573 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2575 unsigned int i;
2577 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2578 device, start_idx, count, constants);
2580 memcpy(&device->cs->c.state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2581 if (TRACE_ON(d3d))
2583 for (i = 0; i < count; ++i)
2584 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2587 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2590 static void wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2591 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2593 unsigned int i;
2595 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2596 device, start_idx, count, constants);
2598 memcpy(&device->cs->c.state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2599 if (TRACE_ON(d3d))
2601 for (i = 0; i < count; ++i)
2602 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2605 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2608 struct wined3d_unordered_access_view * CDECL wined3d_device_context_get_unordered_access_view(
2609 const struct wined3d_device_context *context, enum wined3d_pipeline pipeline, unsigned int idx)
2611 TRACE("context %p, pipeline %#x, idx %u.\n", context, pipeline, idx);
2613 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2615 WARN("Invalid UAV index %u.\n", idx);
2616 return NULL;
2619 return context->state->unordered_access_view[pipeline][idx];
2622 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
2624 unsigned int i;
2626 if (!latency)
2627 latency = 3;
2629 device->max_frame_latency = latency;
2630 for (i = 0; i < device->swapchain_count; ++i)
2631 swapchain_set_max_frame_latency(device->swapchains[i], device);
2634 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
2636 return device->max_frame_latency;
2639 static unsigned int wined3d_get_flexible_vertex_size(DWORD fvf)
2641 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2642 unsigned int i, size = 0;
2644 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2645 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2646 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2647 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2648 switch (fvf & WINED3DFVF_POSITION_MASK)
2650 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2651 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2652 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
2653 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
2654 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
2655 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
2656 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
2657 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
2658 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
2660 for (i = 0; i < texcoord_count; ++i)
2662 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
2665 return size;
2668 static void wined3d_format_get_colour(const struct wined3d_format *format,
2669 const void *data, struct wined3d_color *colour)
2671 float *output = &colour->r;
2672 const uint32_t *u32_data;
2673 const uint16_t *u16_data;
2674 const float *f32_data;
2675 unsigned int i;
2677 static const struct wined3d_color default_colour = {0.0f, 0.0f, 0.0f, 1.0f};
2678 static unsigned int warned;
2680 switch (format->id)
2682 case WINED3DFMT_B8G8R8A8_UNORM:
2683 u32_data = data;
2684 wined3d_color_from_d3dcolor(colour, *u32_data);
2685 break;
2687 case WINED3DFMT_R8G8B8A8_UNORM:
2688 u32_data = data;
2689 colour->r = (*u32_data & 0xffu) / 255.0f;
2690 colour->g = ((*u32_data >> 8) & 0xffu) / 255.0f;
2691 colour->b = ((*u32_data >> 16) & 0xffu) / 255.0f;
2692 colour->a = ((*u32_data >> 24) & 0xffu) / 255.0f;
2693 break;
2695 case WINED3DFMT_R16G16_UNORM:
2696 case WINED3DFMT_R16G16B16A16_UNORM:
2697 u16_data = data;
2698 *colour = default_colour;
2699 for (i = 0; i < format->component_count; ++i)
2700 output[i] = u16_data[i] / 65535.0f;
2701 break;
2703 case WINED3DFMT_R32_FLOAT:
2704 case WINED3DFMT_R32G32_FLOAT:
2705 case WINED3DFMT_R32G32B32_FLOAT:
2706 case WINED3DFMT_R32G32B32A32_FLOAT:
2707 f32_data = data;
2708 *colour = default_colour;
2709 for (i = 0; i < format->component_count; ++i)
2710 output[i] = f32_data[i];
2711 break;
2713 default:
2714 *colour = default_colour;
2715 if (!warned++)
2716 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format->id));
2717 break;
2721 static void wined3d_colour_from_mcs(struct wined3d_color *colour, enum wined3d_material_color_source mcs,
2722 const struct wined3d_color *material_colour, unsigned int index,
2723 const struct wined3d_stream_info *stream_info)
2725 const struct wined3d_stream_info_element *element = NULL;
2727 switch (mcs)
2729 case WINED3D_MCS_MATERIAL:
2730 *colour = *material_colour;
2731 return;
2733 case WINED3D_MCS_COLOR1:
2734 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
2736 colour->r = colour->g = colour->b = colour->a = 1.0f;
2737 return;
2739 element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2740 break;
2742 case WINED3D_MCS_COLOR2:
2743 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
2745 colour->r = colour->g = colour->b = colour->a = 0.0f;
2746 return;
2748 element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2749 break;
2751 default:
2752 colour->r = colour->g = colour->b = colour->a = 0.0f;
2753 ERR("Invalid material colour source %#x.\n", mcs);
2754 return;
2757 wined3d_format_get_colour(element->format, &element->data.addr[index * element->stride], colour);
2760 static float wined3d_clamp(float value, float min_value, float max_value)
2762 return value < min_value ? min_value : value > max_value ? max_value : value;
2765 static float wined3d_vec3_dot(const struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2767 return v0->x * v1->x + v0->y * v1->y + v0->z * v1->z;
2770 static void wined3d_vec3_subtract(struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2772 v0->x -= v1->x;
2773 v0->y -= v1->y;
2774 v0->z -= v1->z;
2777 static void wined3d_vec3_scale(struct wined3d_vec3 *v, float s)
2779 v->x *= s;
2780 v->y *= s;
2781 v->z *= s;
2784 static void wined3d_vec3_normalise(struct wined3d_vec3 *v)
2786 float rnorm = 1.0f / sqrtf(wined3d_vec3_dot(v, v));
2788 if (isfinite(rnorm))
2789 wined3d_vec3_scale(v, rnorm);
2792 static void wined3d_vec3_transform(struct wined3d_vec3 *dst,
2793 const struct wined3d_vec3 *v, const struct wined3d_matrix_3x3 *m)
2795 struct wined3d_vec3 tmp;
2797 tmp.x = v->x * m->_11 + v->y * m->_21 + v->z * m->_31;
2798 tmp.y = v->x * m->_12 + v->y * m->_22 + v->z * m->_32;
2799 tmp.z = v->x * m->_13 + v->y * m->_23 + v->z * m->_33;
2801 *dst = tmp;
2804 static void wined3d_color_clamp(struct wined3d_color *dst, const struct wined3d_color *src,
2805 float min_value, float max_value)
2807 dst->r = wined3d_clamp(src->r, min_value, max_value);
2808 dst->g = wined3d_clamp(src->g, min_value, max_value);
2809 dst->b = wined3d_clamp(src->b, min_value, max_value);
2810 dst->a = wined3d_clamp(src->a, min_value, max_value);
2813 static void wined3d_color_rgb_mul_add(struct wined3d_color *dst, const struct wined3d_color *src, float c)
2815 dst->r += src->r * c;
2816 dst->g += src->g * c;
2817 dst->b += src->b * c;
2820 static void init_transformed_lights(struct lights_settings *ls,
2821 const struct wined3d_state *state, BOOL legacy_lighting, BOOL compute_lighting)
2823 const struct wined3d_light_info *lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
2824 const struct wined3d_light_info *light_info;
2825 struct light_transformed *light;
2826 struct wined3d_vec4 vec4;
2827 unsigned int light_count;
2828 unsigned int i, index;
2830 memset(ls, 0, sizeof(*ls));
2832 ls->lighting = !!compute_lighting;
2833 ls->fog_mode = state->render_states[WINED3D_RS_FOGVERTEXMODE];
2834 ls->fog_coord_mode = state->render_states[WINED3D_RS_RANGEFOGENABLE]
2835 ? WINED3D_FFP_VS_FOG_RANGE : WINED3D_FFP_VS_FOG_DEPTH;
2836 ls->fog_start = wined3d_get_float_state(state, WINED3D_RS_FOGSTART);
2837 ls->fog_end = wined3d_get_float_state(state, WINED3D_RS_FOGEND);
2838 ls->fog_density = wined3d_get_float_state(state, WINED3D_RS_FOGDENSITY);
2840 if (ls->fog_mode == WINED3D_FOG_NONE && !compute_lighting)
2841 return;
2843 multiply_matrix(&ls->modelview_matrix, &state->transforms[WINED3D_TS_VIEW],
2844 &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
2846 if (!compute_lighting)
2847 return;
2849 compute_normal_matrix(&ls->normal_matrix._11, legacy_lighting, &ls->modelview_matrix);
2851 wined3d_color_from_d3dcolor(&ls->ambient_light, state->render_states[WINED3D_RS_AMBIENT]);
2852 ls->legacy_lighting = !!legacy_lighting;
2853 ls->normalise = !!state->render_states[WINED3D_RS_NORMALIZENORMALS];
2854 ls->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
2856 for (i = 0, index = 0; i < LIGHTMAP_SIZE && index < ARRAY_SIZE(lights); ++i)
2858 LIST_FOR_EACH_ENTRY(light_info, &state->light_state.light_map[i], struct wined3d_light_info, entry)
2860 if (!light_info->enabled)
2861 continue;
2863 switch (light_info->OriginalParms.type)
2865 case WINED3D_LIGHT_DIRECTIONAL:
2866 ++ls->directional_light_count;
2867 break;
2869 case WINED3D_LIGHT_POINT:
2870 ++ls->point_light_count;
2871 break;
2873 case WINED3D_LIGHT_SPOT:
2874 ++ls->spot_light_count;
2875 break;
2877 case WINED3D_LIGHT_PARALLELPOINT:
2878 ++ls->parallel_point_light_count;
2879 break;
2881 default:
2882 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
2883 continue;
2885 lights[index++] = light_info;
2886 if (index == WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS)
2887 break;
2891 light_count = index;
2892 for (i = 0, index = 0; i < light_count; ++i)
2894 light_info = lights[i];
2895 if (light_info->OriginalParms.type != WINED3D_LIGHT_DIRECTIONAL)
2896 continue;
2898 light = &ls->lights[index];
2899 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
2900 light->direction = *(struct wined3d_vec3 *)&vec4;
2901 wined3d_vec3_normalise(&light->direction);
2903 light->diffuse = light_info->OriginalParms.diffuse;
2904 light->ambient = light_info->OriginalParms.ambient;
2905 light->specular = light_info->OriginalParms.specular;
2906 ++index;
2909 for (i = 0; i < light_count; ++i)
2911 light_info = lights[i];
2912 if (light_info->OriginalParms.type != WINED3D_LIGHT_POINT)
2913 continue;
2915 light = &ls->lights[index];
2917 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2918 light->range = light_info->OriginalParms.range;
2919 light->c_att = light_info->OriginalParms.attenuation0;
2920 light->l_att = light_info->OriginalParms.attenuation1;
2921 light->q_att = light_info->OriginalParms.attenuation2;
2923 light->diffuse = light_info->OriginalParms.diffuse;
2924 light->ambient = light_info->OriginalParms.ambient;
2925 light->specular = light_info->OriginalParms.specular;
2926 ++index;
2929 for (i = 0; i < light_count; ++i)
2931 light_info = lights[i];
2932 if (light_info->OriginalParms.type != WINED3D_LIGHT_SPOT)
2933 continue;
2935 light = &ls->lights[index];
2937 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2938 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
2939 light->direction = *(struct wined3d_vec3 *)&vec4;
2940 wined3d_vec3_normalise(&light->direction);
2941 light->range = light_info->OriginalParms.range;
2942 light->falloff = light_info->OriginalParms.falloff;
2943 light->c_att = light_info->OriginalParms.attenuation0;
2944 light->l_att = light_info->OriginalParms.attenuation1;
2945 light->q_att = light_info->OriginalParms.attenuation2;
2946 light->cos_htheta = cosf(light_info->OriginalParms.theta / 2.0f);
2947 light->cos_hphi = cosf(light_info->OriginalParms.phi / 2.0f);
2949 light->diffuse = light_info->OriginalParms.diffuse;
2950 light->ambient = light_info->OriginalParms.ambient;
2951 light->specular = light_info->OriginalParms.specular;
2952 ++index;
2955 for (i = 0; i < light_count; ++i)
2957 light_info = lights[i];
2958 if (light_info->OriginalParms.type != WINED3D_LIGHT_PARALLELPOINT)
2959 continue;
2961 light = &ls->lights[index];
2963 wined3d_vec4_transform(&vec4, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2964 *(struct wined3d_vec3 *)&light->position = *(struct wined3d_vec3 *)&vec4;
2965 wined3d_vec3_normalise((struct wined3d_vec3 *)&light->position);
2966 light->diffuse = light_info->OriginalParms.diffuse;
2967 light->ambient = light_info->OriginalParms.ambient;
2968 light->specular = light_info->OriginalParms.specular;
2969 ++index;
2973 static void update_light_diffuse_specular(struct wined3d_color *diffuse, struct wined3d_color *specular,
2974 const struct wined3d_vec3 *dir, float att, float material_shininess,
2975 const struct wined3d_vec3 *normal_transformed,
2976 const struct wined3d_vec3 *position_transformed_normalised,
2977 const struct light_transformed *light, const struct lights_settings *ls)
2979 struct wined3d_vec3 vec3;
2980 float t, c;
2982 c = wined3d_clamp(wined3d_vec3_dot(dir, normal_transformed), 0.0f, 1.0f);
2983 wined3d_color_rgb_mul_add(diffuse, &light->diffuse, c * att);
2985 vec3 = *dir;
2986 if (ls->localviewer)
2987 wined3d_vec3_subtract(&vec3, position_transformed_normalised);
2988 else
2989 vec3.z -= 1.0f;
2990 wined3d_vec3_normalise(&vec3);
2991 t = wined3d_vec3_dot(normal_transformed, &vec3);
2992 if (t > 0.0f && (!ls->legacy_lighting || material_shininess > 0.0f)
2993 && wined3d_vec3_dot(dir, normal_transformed) > 0.0f)
2994 wined3d_color_rgb_mul_add(specular, &light->specular, att * powf(t, material_shininess));
2997 static void light_set_vertex_data(struct lights_settings *ls,
2998 const struct wined3d_vec4 *position)
3000 if (ls->fog_mode == WINED3D_FOG_NONE && !ls->lighting)
3001 return;
3003 wined3d_vec4_transform(&ls->position_transformed, position, &ls->modelview_matrix);
3004 wined3d_vec3_scale((struct wined3d_vec3 *)&ls->position_transformed, 1.0f / ls->position_transformed.w);
3007 static void compute_light(struct wined3d_color *ambient, struct wined3d_color *diffuse,
3008 struct wined3d_color *specular, struct lights_settings *ls, const struct wined3d_vec3 *normal,
3009 float material_shininess)
3011 struct wined3d_vec3 position_transformed_normalised;
3012 struct wined3d_vec3 normal_transformed = {0.0f};
3013 const struct light_transformed *light;
3014 struct wined3d_vec3 dir, dst;
3015 unsigned int i, index;
3016 float att;
3018 position_transformed_normalised = *(const struct wined3d_vec3 *)&ls->position_transformed;
3019 wined3d_vec3_normalise(&position_transformed_normalised);
3021 if (normal)
3023 wined3d_vec3_transform(&normal_transformed, normal, &ls->normal_matrix);
3024 if (ls->normalise)
3025 wined3d_vec3_normalise(&normal_transformed);
3028 diffuse->r = diffuse->g = diffuse->b = diffuse->a = 0.0f;
3029 *specular = *diffuse;
3030 *ambient = ls->ambient_light;
3032 index = 0;
3033 for (i = 0; i < ls->directional_light_count; ++i, ++index)
3035 light = &ls->lights[index];
3037 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3038 if (normal)
3039 update_light_diffuse_specular(diffuse, specular, &light->direction, 1.0f, material_shininess,
3040 &normal_transformed, &position_transformed_normalised, light, ls);
3043 for (i = 0; i < ls->point_light_count; ++i, ++index)
3045 light = &ls->lights[index];
3046 dir.x = light->position.x - ls->position_transformed.x;
3047 dir.y = light->position.y - ls->position_transformed.y;
3048 dir.z = light->position.z - ls->position_transformed.z;
3050 dst.z = wined3d_vec3_dot(&dir, &dir);
3051 dst.y = sqrtf(dst.z);
3052 dst.x = 1.0f;
3053 if (ls->legacy_lighting)
3055 dst.y = (light->range - dst.y) / light->range;
3056 if (!(dst.y > 0.0f))
3057 continue;
3058 dst.z = dst.y * dst.y;
3060 else
3062 if (!(dst.y <= light->range))
3063 continue;
3065 att = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3066 if (!ls->legacy_lighting)
3067 att = 1.0f / att;
3069 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3070 if (normal)
3072 wined3d_vec3_normalise(&dir);
3073 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3074 &normal_transformed, &position_transformed_normalised, light, ls);
3078 for (i = 0; i < ls->spot_light_count; ++i, ++index)
3080 float t;
3082 light = &ls->lights[index];
3084 dir.x = light->position.x - ls->position_transformed.x;
3085 dir.y = light->position.y - ls->position_transformed.y;
3086 dir.z = light->position.z - ls->position_transformed.z;
3088 dst.z = wined3d_vec3_dot(&dir, &dir);
3089 dst.y = sqrtf(dst.z);
3090 dst.x = 1.0f;
3092 if (ls->legacy_lighting)
3094 dst.y = (light->range - dst.y) / light->range;
3095 if (!(dst.y > 0.0f))
3096 continue;
3097 dst.z = dst.y * dst.y;
3099 else
3101 if (!(dst.y <= light->range))
3102 continue;
3104 wined3d_vec3_normalise(&dir);
3105 t = -wined3d_vec3_dot(&dir, &light->direction);
3106 if (t > light->cos_htheta)
3107 att = 1.0f;
3108 else if (t <= light->cos_hphi)
3109 att = 0.0f;
3110 else
3111 att = powf((t - light->cos_hphi) / (light->cos_htheta - light->cos_hphi), light->falloff);
3113 t = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3114 if (ls->legacy_lighting)
3115 att *= t;
3116 else
3117 att /= t;
3119 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3121 if (normal)
3122 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3123 &normal_transformed, &position_transformed_normalised, light, ls);
3126 for (i = 0; i < ls->parallel_point_light_count; ++i, ++index)
3128 light = &ls->lights[index];
3130 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3131 if (normal)
3132 update_light_diffuse_specular(diffuse, specular, (const struct wined3d_vec3 *)&light->position,
3133 1.0f, material_shininess, &normal_transformed, &position_transformed_normalised, light, ls);
3137 static float wined3d_calculate_fog_factor(float fog_coord, const struct lights_settings *ls)
3139 switch (ls->fog_mode)
3141 case WINED3D_FOG_NONE:
3142 return fog_coord;
3143 case WINED3D_FOG_LINEAR:
3144 return (ls->fog_end - fog_coord) / (ls->fog_end - ls->fog_start);
3145 case WINED3D_FOG_EXP:
3146 return expf(-fog_coord * ls->fog_density);
3147 case WINED3D_FOG_EXP2:
3148 return expf(-fog_coord * fog_coord * ls->fog_density * ls->fog_density);
3149 default:
3150 ERR("Unhandled fog mode %#x.\n", ls->fog_mode);
3151 return 0.0f;
3155 static void update_fog_factor(float *fog_factor, struct lights_settings *ls)
3157 float fog_coord;
3159 if (ls->fog_mode == WINED3D_FOG_NONE)
3160 return;
3162 switch (ls->fog_coord_mode)
3164 case WINED3D_FFP_VS_FOG_RANGE:
3165 fog_coord = sqrtf(wined3d_vec3_dot((const struct wined3d_vec3 *)&ls->position_transformed,
3166 (const struct wined3d_vec3 *)&ls->position_transformed));
3167 break;
3169 case WINED3D_FFP_VS_FOG_DEPTH:
3170 fog_coord = fabsf(ls->position_transformed.z);
3171 break;
3173 default:
3174 ERR("Unhandled fog coordinate mode %#x.\n", ls->fog_coord_mode);
3175 return;
3177 *fog_factor = wined3d_calculate_fog_factor(fog_coord, ls);
3180 /* Context activation is done by the caller. */
3181 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3182 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3183 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags, DWORD dst_fvf)
3185 enum wined3d_material_color_source diffuse_source, specular_source, ambient_source, emissive_source;
3186 const struct wined3d_color *material_specular_state_colour;
3187 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3188 const struct wined3d_state *state = device->cs->c.state;
3189 const struct wined3d_format *output_colour_format;
3190 static const struct wined3d_color black;
3191 struct wined3d_map_desc map_desc;
3192 struct wined3d_box box = {0};
3193 struct wined3d_viewport vp;
3194 unsigned int texture_count;
3195 struct lights_settings ls;
3196 unsigned int vertex_size;
3197 BOOL do_clip, lighting;
3198 float min_z, max_z;
3199 unsigned int i;
3200 BYTE *dest_ptr;
3201 HRESULT hr;
3203 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3205 ERR("Source has no position mask.\n");
3206 return WINED3DERR_INVALIDCALL;
3209 if (state->render_states[WINED3D_RS_CLIPPING])
3211 static BOOL warned = FALSE;
3213 * The clipping code is not quite correct. Some things need
3214 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3215 * so disable clipping for now.
3216 * (The graphics in Half-Life are broken, and my processvertices
3217 * test crashes with IDirect3DDevice3)
3218 do_clip = TRUE;
3220 do_clip = FALSE;
3221 if (!warned)
3223 warned = TRUE;
3224 FIXME("Clipping is broken and disabled for now\n");
3227 else
3228 do_clip = FALSE;
3230 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3231 box.left = dwDestIndex * vertex_size;
3232 box.right = box.left + dwCount * vertex_size;
3233 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3235 WARN("Failed to map buffer, hr %#x.\n", hr);
3236 return hr;
3238 dest_ptr = map_desc.data;
3240 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3241 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3242 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3244 TRACE("View mat:\n");
3245 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3246 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3247 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3248 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3250 TRACE("Proj mat:\n");
3251 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3252 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3253 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3254 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3256 TRACE("World mat:\n");
3257 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3258 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3259 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3260 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3262 /* Get the viewport */
3263 wined3d_device_context_get_viewports(&device->cs->c, NULL, &vp);
3264 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3265 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3267 multiply_matrix(&mat,&view_mat,&world_mat);
3268 multiply_matrix(&mat,&proj_mat,&mat);
3270 texture_count = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3272 lighting = state->render_states[WINED3D_RS_LIGHTING]
3273 && (dst_fvf & (WINED3DFVF_DIFFUSE | WINED3DFVF_SPECULAR));
3274 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
3275 &ambient_source, &specular_source, state, stream_info);
3276 output_colour_format = wined3d_get_format(device->adapter, WINED3DFMT_B8G8R8A8_UNORM, 0);
3277 material_specular_state_colour = state->render_states[WINED3D_RS_SPECULARENABLE]
3278 ? &state->material.specular : &black;
3279 init_transformed_lights(&ls, state, device->adapter->d3d_info.wined3d_creation_flags
3280 & WINED3D_LEGACY_FFP_LIGHTING, lighting);
3282 wined3d_viewport_get_z_range(&vp, &min_z, &max_z);
3284 for (i = 0; i < dwCount; ++i)
3286 const struct wined3d_stream_info_element *position_element = &stream_info->elements[WINED3D_FFP_POSITION];
3287 const float *p = (const float *)&position_element->data.addr[i * position_element->stride];
3288 struct wined3d_color ambient, diffuse, specular;
3289 struct wined3d_vec4 position;
3290 unsigned int tex_index;
3292 position.x = p[0];
3293 position.y = p[1];
3294 position.z = p[2];
3295 position.w = 1.0f;
3297 light_set_vertex_data(&ls, &position);
3299 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3300 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3301 /* The position first */
3302 float x, y, z, rhw;
3303 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3305 /* Multiplication with world, view and projection matrix. */
3306 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3307 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3308 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3309 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3311 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3313 /* WARNING: The following things are taken from d3d7 and were not yet checked
3314 * against d3d8 or d3d9!
3317 /* Clipping conditions: From msdn
3319 * A vertex is clipped if it does not match the following requirements
3320 * -rhw < x <= rhw
3321 * -rhw < y <= rhw
3322 * 0 < z <= rhw
3323 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3325 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3326 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3330 if (!do_clip || (-rhw - eps < x && -rhw - eps < y && -eps < z && x <= rhw + eps
3331 && y <= rhw + eps && z <= rhw + eps && rhw > eps))
3333 /* "Normal" viewport transformation (not clipped)
3334 * 1) The values are divided by rhw
3335 * 2) The y axis is negative, so multiply it with -1
3336 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3337 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3338 * 4) Multiply x with Width/2 and add Width/2
3339 * 5) The same for the height
3340 * 6) Add the viewpoint X and Y to the 2D coordinates and
3341 * The minimum Z value to z
3342 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3344 * Well, basically it's simply a linear transformation into viewport
3345 * coordinates
3348 x /= rhw;
3349 y /= rhw;
3350 z /= rhw;
3352 y *= -1;
3354 x *= vp.width / 2;
3355 y *= vp.height / 2;
3356 z *= max_z - min_z;
3358 x += vp.width / 2 + vp.x;
3359 y += vp.height / 2 + vp.y;
3360 z += min_z;
3362 rhw = 1 / rhw;
3363 } else {
3364 /* That vertex got clipped
3365 * Contrary to OpenGL it is not dropped completely, it just
3366 * undergoes a different calculation.
3368 TRACE("Vertex got clipped\n");
3369 x += rhw;
3370 y += rhw;
3372 x /= 2;
3373 y /= 2;
3375 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3376 * outside of the main vertex buffer memory. That needs some more
3377 * investigation...
3381 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3384 ( (float *) dest_ptr)[0] = x;
3385 ( (float *) dest_ptr)[1] = y;
3386 ( (float *) dest_ptr)[2] = z;
3387 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3389 dest_ptr += 3 * sizeof(float);
3391 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3392 dest_ptr += sizeof(float);
3395 if (dst_fvf & WINED3DFVF_PSIZE)
3396 dest_ptr += sizeof(DWORD);
3398 if (dst_fvf & WINED3DFVF_NORMAL)
3400 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3401 const float *normal = (const float *)(element->data.addr + i * element->stride);
3402 /* AFAIK this should go into the lighting information */
3403 FIXME("Didn't expect the destination to have a normal\n");
3404 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3407 if (lighting)
3409 const struct wined3d_stream_info_element *element;
3410 struct wined3d_vec3 *normal;
3412 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3414 element = &stream_info->elements[WINED3D_FFP_NORMAL];
3415 normal = (struct wined3d_vec3 *)&element->data.addr[i * element->stride];
3417 else
3419 normal = NULL;
3421 compute_light(&ambient, &diffuse, &specular, &ls, normal,
3422 state->render_states[WINED3D_RS_SPECULARENABLE] ? state->material.power : 0.0f);
3425 if (dst_fvf & WINED3DFVF_DIFFUSE)
3427 struct wined3d_color material_diffuse, material_ambient, material_emissive, diffuse_colour;
3429 wined3d_colour_from_mcs(&material_diffuse, diffuse_source,
3430 &state->material.diffuse, i, stream_info);
3432 if (lighting)
3434 wined3d_colour_from_mcs(&material_ambient, ambient_source,
3435 &state->material.ambient, i, stream_info);
3436 wined3d_colour_from_mcs(&material_emissive, emissive_source,
3437 &state->material.emissive, i, stream_info);
3439 diffuse_colour.r = ambient.r * material_ambient.r
3440 + diffuse.r * material_diffuse.r + material_emissive.r;
3441 diffuse_colour.g = ambient.g * material_ambient.g
3442 + diffuse.g * material_diffuse.g + material_emissive.g;
3443 diffuse_colour.b = ambient.b * material_ambient.b
3444 + diffuse.b * material_diffuse.b + material_emissive.b;
3445 diffuse_colour.a = material_diffuse.a;
3447 else
3449 diffuse_colour = material_diffuse;
3451 wined3d_color_clamp(&diffuse_colour, &diffuse_colour, 0.0f, 1.0f);
3452 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &diffuse_colour);
3453 dest_ptr += sizeof(DWORD);
3456 if (dst_fvf & WINED3DFVF_SPECULAR)
3458 struct wined3d_color material_specular, specular_colour;
3460 wined3d_colour_from_mcs(&material_specular, specular_source,
3461 material_specular_state_colour, i, stream_info);
3463 if (lighting)
3465 specular_colour.r = specular.r * material_specular.r;
3466 specular_colour.g = specular.g * material_specular.g;
3467 specular_colour.b = specular.b * material_specular.b;
3468 specular_colour.a = ls.legacy_lighting ? 0.0f : material_specular.a;
3470 else
3472 specular_colour = material_specular;
3474 update_fog_factor(&specular_colour.a, &ls);
3475 wined3d_color_clamp(&specular_colour, &specular_colour, 0.0f, 1.0f);
3476 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &specular_colour);
3477 dest_ptr += sizeof(DWORD);
3480 for (tex_index = 0; tex_index < texture_count; ++tex_index)
3482 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3483 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3484 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3486 ERR("No source texture, but destination requests one\n");
3487 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3489 else
3491 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3496 wined3d_resource_unmap(&dest->resource, 0);
3498 return WINED3D_OK;
3500 #undef copy_and_next
3502 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3503 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3504 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3506 struct wined3d_state *state = device->cs->c.state;
3507 struct wined3d_stream_info stream_info;
3508 struct wined3d_resource *resource;
3509 struct wined3d_box box = {0};
3510 struct wined3d_shader *vs;
3511 unsigned int i, j;
3512 HRESULT hr;
3513 WORD map;
3515 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3516 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3517 device, src_start_idx, dst_idx, vertex_count,
3518 dst_buffer, declaration, flags, dst_fvf);
3520 if (declaration)
3521 FIXME("Output vertex declaration not implemented yet.\n");
3523 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3524 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3525 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->d3d_info);
3526 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3528 /* We can't convert FROM a VBO, and vertex buffers used to source into
3529 * process_vertices() are unlikely to ever be used for drawing. Release
3530 * VBOs in those buffers and fix up the stream_info structure.
3532 * Also apply the start index. */
3533 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3535 struct wined3d_stream_info_element *e;
3536 struct wined3d_map_desc map_desc;
3538 if (!(map & 1))
3539 continue;
3541 e = &stream_info.elements[i];
3542 resource = &state->streams[e->stream_idx].buffer->resource;
3543 box.left = src_start_idx * e->stride;
3544 box.right = box.left + vertex_count * e->stride;
3545 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3547 ERR("Failed to map resource.\n");
3548 for (j = 0, map = stream_info.use_map; map && j < i; map >>= 1, ++j)
3550 if (!(map & 1))
3551 continue;
3553 e = &stream_info.elements[j];
3554 resource = &state->streams[e->stream_idx].buffer->resource;
3555 if (FAILED(wined3d_resource_unmap(resource, 0)))
3556 ERR("Failed to unmap resource.\n");
3558 return WINED3DERR_INVALIDCALL;
3560 e->data.buffer_object = 0;
3561 e->data.addr += (ULONG_PTR)map_desc.data;
3564 hr = process_vertices_strided(device, dst_idx, vertex_count,
3565 &stream_info, dst_buffer, flags, dst_fvf);
3567 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3569 if (!(map & 1))
3570 continue;
3572 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3573 if (FAILED(wined3d_resource_unmap(resource, 0)))
3574 ERR("Failed to unmap resource.\n");
3577 return hr;
3580 static void wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3581 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3583 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3585 TRACE("device %p, stage %u, state %s, value %#x.\n",
3586 device, stage, debug_d3dtexturestate(state), value);
3588 if (stage >= d3d_info->limits.ffp_blend_stages)
3590 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3591 stage, d3d_info->limits.ffp_blend_stages - 1);
3592 return;
3595 if (value == device->cs->c.state->texture_states[stage][state])
3597 TRACE("Application is setting the old value over, nothing to do.\n");
3598 return;
3601 device->cs->c.state->texture_states[stage][state] = value;
3603 wined3d_device_context_emit_set_texture_state(&device->cs->c, stage, state, value);
3606 static void wined3d_device_set_texture(struct wined3d_device *device,
3607 UINT stage, struct wined3d_texture *texture)
3609 struct wined3d_state *state = device->cs->c.state;
3610 struct wined3d_texture *prev;
3612 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3614 /* Windows accepts overflowing this array... we do not. */
3615 if (stage >= ARRAY_SIZE(state->textures))
3617 WARN("Ignoring invalid stage %u.\n", stage);
3618 return;
3621 prev = state->textures[stage];
3622 TRACE("Previous texture %p.\n", prev);
3624 if (texture == prev)
3626 TRACE("App is setting the same texture again, nothing to do.\n");
3627 return;
3630 TRACE("Setting new texture to %p.\n", texture);
3631 state->textures[stage] = texture;
3633 if (texture)
3634 wined3d_texture_incref(texture);
3635 wined3d_device_context_emit_set_texture(&device->cs->c, stage, texture);
3636 if (prev)
3637 wined3d_texture_decref(prev);
3639 return;
3642 void CDECL wined3d_device_apply_stateblock(struct wined3d_device *device,
3643 struct wined3d_stateblock *stateblock)
3645 BOOL set_blend_state = FALSE, set_depth_stencil_state = FALSE, set_rasterizer_state = FALSE;
3646 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
3647 const struct wined3d_saved_states *changed = &stateblock->changed;
3648 const unsigned int word_bit_count = sizeof(DWORD) * CHAR_BIT;
3649 struct wined3d_device_context *context = &device->cs->c;
3650 unsigned int i, j, start, idx;
3651 struct wined3d_range range;
3652 uint32_t map;
3654 TRACE("device %p, stateblock %p.\n", device, stateblock);
3656 if (changed->vertexShader)
3657 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_VERTEX, state->vs);
3658 if (changed->pixelShader)
3659 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_PIXEL, state->ps);
3661 for (start = 0; ; start = range.offset + range.size)
3663 if (!wined3d_bitmap_get_range(changed->vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
3664 break;
3666 wined3d_device_set_vs_consts_f(device, range.offset, range.size, &state->vs_consts_f[range.offset]);
3669 map = changed->vertexShaderConstantsI;
3670 for (start = 0; ; start = range.offset + range.size)
3672 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3673 break;
3675 wined3d_device_set_vs_consts_i(device, range.offset, range.size, &state->vs_consts_i[range.offset]);
3678 map = changed->vertexShaderConstantsB;
3679 for (start = 0; ; start = range.offset + range.size)
3681 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3682 break;
3684 wined3d_device_set_vs_consts_b(device, range.offset, range.size, &state->vs_consts_b[range.offset]);
3687 for (start = 0; ; start = range.offset + range.size)
3689 if (!wined3d_bitmap_get_range(changed->ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
3690 break;
3692 wined3d_device_set_ps_consts_f(device, range.offset, range.size, &state->ps_consts_f[range.offset]);
3695 map = changed->pixelShaderConstantsI;
3696 for (start = 0; ; start = range.offset + range.size)
3698 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3699 break;
3701 wined3d_device_set_ps_consts_i(device, range.offset, range.size, &state->ps_consts_i[range.offset]);
3704 map = changed->pixelShaderConstantsB;
3705 for (start = 0; ; start = range.offset + range.size)
3707 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3708 break;
3710 wined3d_device_set_ps_consts_b(device, range.offset, range.size, &state->ps_consts_b[range.offset]);
3713 if (changed->lights)
3715 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); ++i)
3717 const struct wined3d_light_info *light;
3719 LIST_FOR_EACH_ENTRY(light, &state->light_state->light_map[i], struct wined3d_light_info, entry)
3721 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
3722 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
3727 for (i = 0; i < ARRAY_SIZE(changed->renderState); ++i)
3729 map = changed->renderState[i];
3730 while (map)
3732 j = wined3d_bit_scan(&map);
3733 idx = i * word_bit_count + j;
3735 switch (idx)
3737 case WINED3D_RS_BLENDFACTOR:
3738 case WINED3D_RS_MULTISAMPLEMASK:
3739 case WINED3D_RS_ALPHABLENDENABLE:
3740 case WINED3D_RS_SRCBLEND:
3741 case WINED3D_RS_DESTBLEND:
3742 case WINED3D_RS_BLENDOP:
3743 case WINED3D_RS_SEPARATEALPHABLENDENABLE:
3744 case WINED3D_RS_SRCBLENDALPHA:
3745 case WINED3D_RS_DESTBLENDALPHA:
3746 case WINED3D_RS_BLENDOPALPHA:
3747 case WINED3D_RS_COLORWRITEENABLE:
3748 case WINED3D_RS_COLORWRITEENABLE1:
3749 case WINED3D_RS_COLORWRITEENABLE2:
3750 case WINED3D_RS_COLORWRITEENABLE3:
3751 set_blend_state = TRUE;
3752 break;
3754 case WINED3D_RS_BACK_STENCILFAIL:
3755 case WINED3D_RS_BACK_STENCILFUNC:
3756 case WINED3D_RS_BACK_STENCILPASS:
3757 case WINED3D_RS_BACK_STENCILZFAIL:
3758 case WINED3D_RS_STENCILENABLE:
3759 case WINED3D_RS_STENCILFAIL:
3760 case WINED3D_RS_STENCILFUNC:
3761 case WINED3D_RS_STENCILREF:
3762 case WINED3D_RS_STENCILMASK:
3763 case WINED3D_RS_STENCILPASS:
3764 case WINED3D_RS_STENCILWRITEMASK:
3765 case WINED3D_RS_STENCILZFAIL:
3766 case WINED3D_RS_TWOSIDEDSTENCILMODE:
3767 case WINED3D_RS_ZENABLE:
3768 case WINED3D_RS_ZFUNC:
3769 case WINED3D_RS_ZWRITEENABLE:
3770 set_depth_stencil_state = TRUE;
3771 break;
3773 case WINED3D_RS_FILLMODE:
3774 case WINED3D_RS_CULLMODE:
3775 case WINED3D_RS_SLOPESCALEDEPTHBIAS:
3776 case WINED3D_RS_DEPTHBIAS:
3777 case WINED3D_RS_SCISSORTESTENABLE:
3778 case WINED3D_RS_ANTIALIASEDLINEENABLE:
3779 set_rasterizer_state = TRUE;
3780 break;
3782 default:
3783 wined3d_device_set_render_state(device, idx, state->rs[idx]);
3784 break;
3789 if (set_rasterizer_state)
3791 struct wined3d_rasterizer_state *rasterizer_state;
3792 struct wined3d_rasterizer_state_desc desc;
3793 struct wine_rb_entry *entry;
3794 union
3796 DWORD d;
3797 float f;
3798 } bias;
3800 memset(&desc, 0, sizeof(desc));
3801 desc.fill_mode = state->rs[WINED3D_RS_FILLMODE];
3802 desc.cull_mode = state->rs[WINED3D_RS_CULLMODE];
3803 bias.d = state->rs[WINED3D_RS_DEPTHBIAS];
3804 desc.depth_bias = bias.f;
3805 bias.d = state->rs[WINED3D_RS_SLOPESCALEDEPTHBIAS];
3806 desc.scale_bias = bias.f;
3807 desc.depth_clip = TRUE;
3808 desc.scissor = state->rs[WINED3D_RS_SCISSORTESTENABLE];
3809 desc.line_antialias = state->rs[WINED3D_RS_ANTIALIASEDLINEENABLE];
3811 if ((entry = wine_rb_get(&device->rasterizer_states, &desc)))
3813 rasterizer_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
3814 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
3816 else if (SUCCEEDED(wined3d_rasterizer_state_create(device, &desc, NULL,
3817 &wined3d_null_parent_ops, &rasterizer_state)))
3819 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
3820 if (wine_rb_put(&device->rasterizer_states, &desc, &rasterizer_state->entry) == -1)
3822 ERR("Failed to insert rasterizer state.\n");
3823 wined3d_rasterizer_state_decref(rasterizer_state);
3828 if (set_blend_state || changed->alpha_to_coverage
3829 || wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_ADAPTIVETESS_Y))
3831 struct wined3d_blend_state *blend_state;
3832 struct wined3d_blend_state_desc desc;
3833 struct wine_rb_entry *entry;
3834 struct wined3d_color colour;
3835 unsigned int sample_mask;
3837 memset(&desc, 0, sizeof(desc));
3838 desc.alpha_to_coverage = state->alpha_to_coverage;
3839 desc.independent = FALSE;
3840 if (state->rs[WINED3D_RS_ADAPTIVETESS_Y] == WINED3DFMT_ATOC)
3841 desc.alpha_to_coverage = TRUE;
3842 desc.rt[0].enable = state->rs[WINED3D_RS_ALPHABLENDENABLE];
3843 desc.rt[0].src = state->rs[WINED3D_RS_SRCBLEND];
3844 desc.rt[0].dst = state->rs[WINED3D_RS_DESTBLEND];
3845 desc.rt[0].op = state->rs[WINED3D_RS_BLENDOP];
3846 if (state->rs[WINED3D_RS_SEPARATEALPHABLENDENABLE])
3848 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLENDALPHA];
3849 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLENDALPHA];
3850 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOPALPHA];
3852 else
3854 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLEND];
3855 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLEND];
3856 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOP];
3858 desc.rt[0].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE];
3859 desc.rt[1].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE1];
3860 desc.rt[2].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE2];
3861 desc.rt[3].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE3];
3862 if (desc.rt[1].writemask != desc.rt[0].writemask
3863 || desc.rt[2].writemask != desc.rt[0].writemask
3864 || desc.rt[3].writemask != desc.rt[0].writemask)
3866 desc.independent = TRUE;
3867 for (i = 1; i < 4; ++i)
3869 desc.rt[i].enable = desc.rt[0].enable;
3870 desc.rt[i].src = desc.rt[0].src;
3871 desc.rt[i].dst = desc.rt[0].dst;
3872 desc.rt[i].op = desc.rt[0].op;
3873 desc.rt[i].src_alpha = desc.rt[0].src_alpha;
3874 desc.rt[i].dst_alpha = desc.rt[0].dst_alpha;
3875 desc.rt[i].op_alpha = desc.rt[0].op_alpha;
3879 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_BLENDFACTOR))
3880 wined3d_color_from_d3dcolor(&colour, state->rs[WINED3D_RS_BLENDFACTOR]);
3881 else
3882 wined3d_device_context_get_blend_state(context, &colour, &sample_mask);
3884 if ((entry = wine_rb_get(&device->blend_states, &desc)))
3886 blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
3887 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3888 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3890 else if (SUCCEEDED(wined3d_blend_state_create(device, &desc, NULL,
3891 &wined3d_null_parent_ops, &blend_state)))
3893 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3894 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3895 if (wine_rb_put(&device->blend_states, &desc, &blend_state->entry) == -1)
3897 ERR("Failed to insert blend state.\n");
3898 wined3d_blend_state_decref(blend_state);
3903 if (set_depth_stencil_state)
3905 struct wined3d_depth_stencil_state *depth_stencil_state;
3906 struct wined3d_depth_stencil_state_desc desc;
3907 struct wine_rb_entry *entry;
3908 unsigned int stencil_ref;
3910 memset(&desc, 0, sizeof(desc));
3911 switch (state->rs[WINED3D_RS_ZENABLE])
3913 case WINED3D_ZB_FALSE:
3914 desc.depth = FALSE;
3915 break;
3917 case WINED3D_ZB_USEW:
3918 FIXME("W buffer is not well handled.\n");
3919 case WINED3D_ZB_TRUE:
3920 desc.depth = TRUE;
3921 break;
3923 default:
3924 FIXME("Unrecognized depth buffer type %#x.\n", state->rs[WINED3D_RS_ZENABLE]);
3926 desc.depth_write = state->rs[WINED3D_RS_ZWRITEENABLE];
3927 desc.depth_func = state->rs[WINED3D_RS_ZFUNC];
3928 desc.stencil = state->rs[WINED3D_RS_STENCILENABLE];
3929 desc.stencil_read_mask = state->rs[WINED3D_RS_STENCILMASK];
3930 desc.stencil_write_mask = state->rs[WINED3D_RS_STENCILWRITEMASK];
3931 desc.front.fail_op = state->rs[WINED3D_RS_STENCILFAIL];
3932 desc.front.depth_fail_op = state->rs[WINED3D_RS_STENCILZFAIL];
3933 desc.front.pass_op = state->rs[WINED3D_RS_STENCILPASS];
3934 desc.front.func = state->rs[WINED3D_RS_STENCILFUNC];
3936 if (state->rs[WINED3D_RS_TWOSIDEDSTENCILMODE])
3938 desc.back.fail_op = state->rs[WINED3D_RS_BACK_STENCILFAIL];
3939 desc.back.depth_fail_op = state->rs[WINED3D_RS_BACK_STENCILZFAIL];
3940 desc.back.pass_op = state->rs[WINED3D_RS_BACK_STENCILPASS];
3941 desc.back.func = state->rs[WINED3D_RS_BACK_STENCILFUNC];
3943 else
3945 desc.back = desc.front;
3948 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_STENCILREF))
3949 stencil_ref = state->rs[WINED3D_RS_STENCILREF];
3950 else
3951 wined3d_device_context_get_depth_stencil_state(context, &stencil_ref);
3953 if ((entry = wine_rb_get(&device->depth_stencil_states, &desc)))
3955 depth_stencil_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
3956 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3958 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device, &desc, NULL,
3959 &wined3d_null_parent_ops, &depth_stencil_state)))
3961 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3962 if (wine_rb_put(&device->depth_stencil_states, &desc, &depth_stencil_state->entry) == -1)
3964 ERR("Failed to insert depth/stencil state.\n");
3965 wined3d_depth_stencil_state_decref(depth_stencil_state);
3970 for (i = 0; i < ARRAY_SIZE(changed->textureState); ++i)
3972 map = changed->textureState[i];
3973 while (map)
3975 j = wined3d_bit_scan(&map);
3976 wined3d_device_set_texture_stage_state(device, i, j, state->texture_states[i][j]);
3980 for (i = 0; i < ARRAY_SIZE(changed->samplerState); ++i)
3982 map = changed->samplerState[i];
3983 while (map)
3985 j = wined3d_bit_scan(&map);
3986 wined3d_device_set_sampler_state(device, i, j, state->sampler_states[i][j]);
3990 if (changed->transforms)
3992 for (i = 0; i < ARRAY_SIZE(changed->transform); ++i)
3994 map = changed->transform[i];
3995 while (map)
3997 j = wined3d_bit_scan(&map);
3998 idx = i * word_bit_count + j;
3999 wined3d_device_set_transform(device, idx, &state->transforms[idx]);
4004 if (changed->indices)
4005 wined3d_device_context_set_index_buffer(context, state->index_buffer, state->index_format, 0);
4006 wined3d_device_set_base_vertex_index(device, state->base_vertex_index);
4007 if (changed->vertexDecl)
4008 wined3d_device_context_set_vertex_declaration(context, state->vertex_declaration);
4009 if (changed->material)
4010 wined3d_device_set_material(device, &state->material);
4011 if (changed->viewport)
4012 wined3d_device_context_set_viewports(context, 1, &state->viewport);
4013 if (changed->scissorRect)
4014 wined3d_device_context_set_scissor_rects(context, 1, &state->scissor_rect);
4016 map = changed->streamSource | changed->streamFreq;
4017 while (map)
4019 i = wined3d_bit_scan(&map);
4020 wined3d_device_context_set_stream_sources(context, i, 1, &state->streams[i]);
4023 map = changed->textures;
4024 while (map)
4026 i = wined3d_bit_scan(&map);
4027 wined3d_device_set_texture(device, i, state->textures[i]);
4030 map = changed->clipplane;
4031 while (map)
4033 i = wined3d_bit_scan(&map);
4034 wined3d_device_set_clip_plane(device, i, &state->clip_planes[i]);
4037 memset(&stateblock->changed, 0, sizeof(stateblock->changed));
4039 TRACE("Applied stateblock %p.\n", stateblock);
4042 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
4044 TRACE("device %p, caps %p.\n", device, caps);
4046 return wined3d_get_device_caps(device->adapter, device->create_parms.device_type, caps);
4049 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
4050 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
4052 struct wined3d_swapchain *swapchain;
4054 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
4055 device, swapchain_idx, mode, rotation);
4057 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4058 return WINED3DERR_INVALIDCALL;
4060 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
4063 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
4065 /* At the moment we have no need for any functionality at the beginning
4066 * of a scene. */
4067 TRACE("device %p.\n", device);
4069 if (device->inScene)
4071 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
4072 return WINED3DERR_INVALIDCALL;
4074 device->inScene = TRUE;
4075 return WINED3D_OK;
4078 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
4080 TRACE("device %p.\n", device);
4082 if (!device->inScene)
4084 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
4085 return WINED3DERR_INVALIDCALL;
4088 device->inScene = FALSE;
4089 return WINED3D_OK;
4092 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
4093 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
4095 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
4097 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
4098 device, rect_count, rects, flags, debug_color(color), depth, stencil);
4100 if (!rect_count && rects)
4102 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
4103 return WINED3D_OK;
4106 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4108 struct wined3d_rendertarget_view *ds = fb->depth_stencil;
4109 if (!ds)
4111 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4112 /* TODO: What about depth stencil buffers without stencil bits? */
4113 return WINED3DERR_INVALIDCALL;
4115 else if (flags & WINED3DCLEAR_TARGET)
4117 if (ds->width < fb->render_targets[0]->width
4118 || ds->height < fb->render_targets[0]->height)
4120 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4121 return WINED3D_OK;
4126 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
4128 return WINED3D_OK;
4131 struct wined3d_query * CDECL wined3d_device_context_get_predication(struct wined3d_device_context *context, BOOL *value)
4133 struct wined3d_state *state = context->state;
4135 TRACE("context %p, value %p.\n", context, value);
4137 if (value)
4138 *value = state->predicate_value;
4139 return state->predicate;
4142 void CDECL wined3d_device_context_set_primitive_type(struct wined3d_device_context *context,
4143 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
4145 struct wined3d_state *state = context->state;
4147 TRACE("context %p, primitive_type %s, patch_vertex_count %u.\n",
4148 context, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
4150 wined3d_device_context_lock(context);
4151 state->primitive_type = primitive_type;
4152 state->patch_vertex_count = patch_vertex_count;
4153 wined3d_device_context_unlock(context);
4156 void CDECL wined3d_device_context_get_primitive_type(const struct wined3d_device_context *context,
4157 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
4159 const struct wined3d_state *state = context->state;
4161 TRACE("context %p, primitive_type %p, patch_vertex_count %p.\n",
4162 context, primitive_type, patch_vertex_count);
4164 *primitive_type = state->primitive_type;
4165 if (patch_vertex_count)
4166 *patch_vertex_count = state->patch_vertex_count;
4168 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
4171 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4172 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4174 unsigned int src_size, dst_size, src_skip_levels = 0;
4175 unsigned int src_level_count, dst_level_count;
4176 const struct wined3d_dirty_regions *regions;
4177 unsigned int layer_count, level_count, i, j;
4178 enum wined3d_resource_type type;
4179 BOOL entire_texture = TRUE;
4180 struct wined3d_box box;
4182 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4184 /* Verify that the source and destination textures are non-NULL. */
4185 if (!src_texture || !dst_texture)
4187 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4188 return WINED3DERR_INVALIDCALL;
4191 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
4192 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
4194 WARN("Source resource is GPU accessible or a scratch resource.\n");
4195 return WINED3DERR_INVALIDCALL;
4197 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
4199 WARN("Destination resource is CPU accessible.\n");
4200 return WINED3DERR_INVALIDCALL;
4203 /* Verify that the source and destination textures are the same type. */
4204 type = src_texture->resource.type;
4205 if (dst_texture->resource.type != type)
4207 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4208 return WINED3DERR_INVALIDCALL;
4211 layer_count = src_texture->layer_count;
4212 if (layer_count != dst_texture->layer_count)
4214 WARN("Source and destination have different layer counts.\n");
4215 return WINED3DERR_INVALIDCALL;
4218 if (src_texture->resource.format != dst_texture->resource.format)
4220 WARN("Source and destination formats do not match.\n");
4221 return WINED3DERR_INVALIDCALL;
4224 src_level_count = src_texture->level_count;
4225 dst_level_count = dst_texture->level_count;
4226 level_count = min(src_level_count, dst_level_count);
4228 src_size = max(src_texture->resource.width, src_texture->resource.height);
4229 src_size = max(src_size, src_texture->resource.depth);
4230 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
4231 dst_size = max(dst_size, dst_texture->resource.depth);
4232 while (src_size > dst_size)
4234 src_size >>= 1;
4235 ++src_skip_levels;
4238 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
4239 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
4240 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
4242 WARN("Source and destination dimensions do not match.\n");
4243 return WINED3DERR_INVALIDCALL;
4246 if ((regions = src_texture->dirty_regions))
4248 for (i = 0; i < layer_count && entire_texture; ++i)
4250 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4251 continue;
4253 entire_texture = FALSE;
4254 break;
4258 /* Update every surface level of the texture. */
4259 if (entire_texture)
4261 for (i = 0; i < level_count; ++i)
4263 wined3d_texture_get_level_box(dst_texture, i, &box);
4264 for (j = 0; j < layer_count; ++j)
4266 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4267 &dst_texture->resource, j * dst_level_count + i, &box,
4268 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
4269 0, NULL, WINED3D_TEXF_POINT);
4273 else
4275 unsigned int src_level, box_count, k;
4276 const struct wined3d_box *boxes;
4277 struct wined3d_box b;
4279 for (i = 0; i < layer_count; ++i)
4281 boxes = regions[i].boxes;
4282 box_count = regions[i].box_count;
4283 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4285 boxes = &b;
4286 box_count = 1;
4287 wined3d_texture_get_level_box(dst_texture, i, &b);
4290 for (j = 0; j < level_count; ++j)
4292 src_level = j + src_skip_levels;
4294 /* TODO: We could pass an array of boxes here to avoid
4295 * multiple context acquisitions for the same resource. */
4296 for (k = 0; k < box_count; ++k)
4298 box = boxes[k];
4299 if (src_level)
4301 box.left >>= src_level;
4302 box.top >>= src_level;
4303 box.right = min((box.right + (1u << src_level) - 1) >> src_level,
4304 wined3d_texture_get_level_width(src_texture, src_level));
4305 box.bottom = min((box.bottom + (1u << src_level) - 1) >> src_level,
4306 wined3d_texture_get_level_height(src_texture, src_level));
4307 box.front >>= src_level;
4308 box.back = min((box.back + (1u << src_level) - 1) >> src_level,
4309 wined3d_texture_get_level_depth(src_texture, src_level));
4312 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4313 &dst_texture->resource, i * dst_level_count + j, &box,
4314 &src_texture->resource, i * src_level_count + src_level, &box,
4315 0, NULL, WINED3D_TEXF_POINT);
4321 wined3d_texture_clear_dirty_regions(src_texture);
4323 return WINED3D_OK;
4326 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4328 const struct wined3d_state *state = device->cs->c.state;
4329 struct wined3d_texture *texture;
4330 DWORD i;
4332 TRACE("device %p, num_passes %p.\n", device, num_passes);
4334 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
4336 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4338 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4339 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4341 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4343 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4344 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4347 texture = state->textures[i];
4348 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
4350 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4352 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
4353 return E_FAIL;
4355 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4357 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
4358 return E_FAIL;
4360 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4361 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4363 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
4364 return E_FAIL;
4368 if (wined3d_state_uses_depth_buffer(state)
4369 || (state->depth_stencil_state && state->depth_stencil_state->desc.stencil))
4371 struct wined3d_rendertarget_view *rt = state->fb.render_targets[0];
4372 struct wined3d_rendertarget_view *ds = state->fb.depth_stencil;
4374 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
4376 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4377 return WINED3DERR_CONFLICTINGRENDERSTATE;
4381 /* return a sensible default */
4382 *num_passes = 1;
4384 TRACE("returning D3D_OK\n");
4385 return WINED3D_OK;
4388 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4390 static BOOL warned;
4392 TRACE("device %p, software %#x.\n", device, software);
4394 if (!warned)
4396 FIXME("device %p, software %#x stub!\n", device, software);
4397 warned = TRUE;
4400 device->softwareVertexProcessing = software;
4403 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4405 static BOOL warned;
4407 TRACE("device %p.\n", device);
4409 if (!warned)
4411 TRACE("device %p stub!\n", device);
4412 warned = TRUE;
4415 return device->softwareVertexProcessing;
4418 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4419 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4421 struct wined3d_swapchain *swapchain;
4423 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4424 device, swapchain_idx, raster_status);
4426 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4427 return WINED3DERR_INVALIDCALL;
4429 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4432 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4434 static BOOL warned;
4436 TRACE("device %p, segments %.8e.\n", device, segments);
4438 if (segments != 0.0f)
4440 if (!warned)
4442 FIXME("device %p, segments %.8e stub!\n", device, segments);
4443 warned = TRUE;
4447 return WINED3D_OK;
4450 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4452 static BOOL warned;
4454 TRACE("device %p.\n", device);
4456 if (!warned)
4458 FIXME("device %p stub!\n", device);
4459 warned = TRUE;
4462 return 0.0f;
4465 void CDECL wined3d_device_context_copy_uav_counter(struct wined3d_device_context *context,
4466 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4468 TRACE("context %p, dst_buffer %p, offset %u, uav %p.\n",
4469 context, dst_buffer, offset, uav);
4471 wined3d_device_context_lock(context);
4472 wined3d_device_context_emit_copy_uav_counter(context, dst_buffer, offset, uav);
4473 wined3d_device_context_unlock(context);
4476 static bool resources_format_compatible(const struct wined3d_resource *src_resource,
4477 const struct wined3d_resource *dst_resource)
4479 if (src_resource->format->id == dst_resource->format->id)
4480 return true;
4481 if (src_resource->format->typeless_id && src_resource->format->typeless_id == dst_resource->format->typeless_id)
4482 return true;
4483 if (src_resource->device->cs->c.state->feature_level < WINED3D_FEATURE_LEVEL_10_1)
4484 return false;
4485 if ((src_resource->format_flags & WINED3DFMT_FLAG_BLOCKS)
4486 && (dst_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK))
4487 return src_resource->format->block_byte_count == dst_resource->format->byte_count;
4488 if ((src_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK)
4489 && (dst_resource->format_flags & WINED3DFMT_FLAG_BLOCKS))
4490 return src_resource->format->byte_count == dst_resource->format->block_byte_count;
4491 return false;
4494 void CDECL wined3d_device_context_copy_resource(struct wined3d_device_context *context,
4495 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4497 unsigned int src_row_block_count, dst_row_block_count;
4498 struct wined3d_texture *dst_texture, *src_texture;
4499 unsigned int src_row_count, dst_row_count;
4500 struct wined3d_box src_box, dst_box;
4501 unsigned int i, j;
4503 TRACE("context %p, dst_resource %p, src_resource %p.\n", context, dst_resource, src_resource);
4505 if (src_resource == dst_resource)
4507 WARN("Source and destination are the same resource.\n");
4508 return;
4511 if (src_resource->type != dst_resource->type)
4513 WARN("Resource types (%s / %s) don't match.\n",
4514 debug_d3dresourcetype(dst_resource->type),
4515 debug_d3dresourcetype(src_resource->type));
4516 return;
4519 if (!resources_format_compatible(src_resource, dst_resource))
4521 WARN("Resource formats %s and %s are incompatible.\n",
4522 debug_d3dformat(dst_resource->format->id),
4523 debug_d3dformat(src_resource->format->id));
4524 return;
4527 src_row_block_count = (src_resource->width + (src_resource->format->block_width - 1))
4528 / src_resource->format->block_width;
4529 dst_row_block_count = (dst_resource->width + (dst_resource->format->block_width - 1))
4530 / dst_resource->format->block_width;
4531 src_row_count = (src_resource->height + (src_resource->format->block_height - 1))
4532 / src_resource->format->block_height;
4533 dst_row_count = (dst_resource->height + (dst_resource->format->block_height - 1))
4534 / dst_resource->format->block_height;
4536 if (src_row_block_count != dst_row_block_count || src_row_count != dst_row_count
4537 || src_resource->depth != dst_resource->depth)
4539 WARN("Resource block dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4540 dst_row_block_count, dst_row_count, dst_resource->depth,
4541 src_row_block_count, src_row_count, src_resource->depth);
4542 return;
4545 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4547 wined3d_box_set(&src_box, 0, 0, src_resource->size, 1, 0, 1);
4548 wined3d_device_context_lock(context);
4549 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, 0, &src_box,
4550 src_resource, 0, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4551 wined3d_device_context_unlock(context);
4552 return;
4555 dst_texture = texture_from_resource(dst_resource);
4556 src_texture = texture_from_resource(src_resource);
4558 if (src_texture->layer_count != dst_texture->layer_count
4559 || src_texture->level_count != dst_texture->level_count)
4561 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4562 dst_texture->layer_count, dst_texture->level_count,
4563 src_texture->layer_count, src_texture->level_count);
4564 return;
4567 wined3d_device_context_lock(context);
4568 for (i = 0; i < dst_texture->level_count; ++i)
4570 wined3d_texture_get_level_box(src_texture, i, &src_box);
4571 wined3d_texture_get_level_box(dst_texture, i, &dst_box);
4572 for (j = 0; j < dst_texture->layer_count; ++j)
4574 unsigned int idx = j * dst_texture->level_count + i;
4576 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, idx, &dst_box,
4577 src_resource, idx, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4580 wined3d_device_context_unlock(context);
4583 HRESULT CDECL wined3d_device_context_copy_sub_resource_region(struct wined3d_device_context *context,
4584 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4585 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4586 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
4588 struct wined3d_box dst_box, b;
4590 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4591 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4592 context, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4593 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
4595 if (flags)
4596 FIXME("Ignoring flags %#x.\n", flags);
4598 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4600 WARN("Source and destination are the same sub-resource.\n");
4601 return WINED3DERR_INVALIDCALL;
4604 if (!resources_format_compatible(src_resource, dst_resource))
4606 WARN("Resource formats %s and %s are incompatible.\n",
4607 debug_d3dformat(dst_resource->format->id),
4608 debug_d3dformat(src_resource->format->id));
4609 return WINED3DERR_INVALIDCALL;
4612 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4614 if (src_resource->type != WINED3D_RTYPE_BUFFER)
4616 WARN("Resource types (%s / %s) don't match.\n",
4617 debug_d3dresourcetype(dst_resource->type),
4618 debug_d3dresourcetype(src_resource->type));
4619 return WINED3DERR_INVALIDCALL;
4622 if (dst_sub_resource_idx)
4624 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4625 return WINED3DERR_INVALIDCALL;
4628 if (src_sub_resource_idx)
4630 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4631 return WINED3DERR_INVALIDCALL;
4634 if (!src_box)
4636 unsigned int dst_w;
4638 dst_w = dst_resource->size - dst_x;
4639 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4640 src_box = &b;
4642 else if ((src_box->left >= src_box->right
4643 || src_box->top >= src_box->bottom
4644 || src_box->front >= src_box->back))
4646 WARN("Invalid box %s specified.\n", debug_box(src_box));
4647 return WINED3DERR_INVALIDCALL;
4650 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4651 || src_box->right - src_box->left > dst_resource->size - dst_x)
4653 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4654 dst_x, src_box->left, src_box->right - src_box->left);
4655 return WINED3DERR_INVALIDCALL;
4658 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4660 else
4662 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4663 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4664 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4665 unsigned int src_row_block_count, src_row_count;
4667 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4669 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
4670 return WINED3DERR_INVALIDCALL;
4673 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
4675 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4676 return WINED3DERR_INVALIDCALL;
4679 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4681 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4682 return WINED3DERR_INVALIDCALL;
4685 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4687 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4688 return WINED3DERR_INVALIDCALL;
4691 if (!src_box)
4693 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
4695 src_w = wined3d_texture_get_level_width(src_texture, src_level);
4696 src_h = wined3d_texture_get_level_height(src_texture, src_level);
4697 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
4699 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4700 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
4701 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
4702 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
4704 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
4705 src_box = &b;
4707 else if (FAILED(wined3d_resource_check_box_dimensions(src_resource, src_sub_resource_idx, src_box)))
4709 WARN("Invalid source box %s.\n", debug_box(src_box));
4710 return WINED3DERR_INVALIDCALL;
4713 if (src_resource->format->block_width == dst_resource->format->block_width
4714 && src_resource->format->block_height == dst_resource->format->block_height)
4716 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4717 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
4719 else
4721 src_row_block_count = (src_box->right - src_box->left + src_resource->format->block_width - 1)
4722 / src_resource->format->block_width;
4723 src_row_count = (src_box->bottom - src_box->top + src_resource->format->block_height - 1)
4724 / src_resource->format->block_height;
4725 wined3d_box_set(&dst_box, dst_x, dst_y,
4726 dst_x + (src_row_block_count * dst_resource->format->block_width),
4727 dst_y + (src_row_count * dst_resource->format->block_height),
4728 dst_z, dst_z + (src_box->back - src_box->front));
4730 if (FAILED(wined3d_resource_check_box_dimensions(dst_resource, dst_sub_resource_idx, &dst_box)))
4732 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4733 return WINED3DERR_INVALIDCALL;
4737 wined3d_device_context_lock(context);
4738 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, dst_sub_resource_idx, &dst_box,
4739 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4740 wined3d_device_context_unlock(context);
4742 return WINED3D_OK;
4745 void CDECL wined3d_device_context_update_sub_resource(struct wined3d_device_context *context,
4746 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
4747 const void *data, unsigned int row_pitch, unsigned int depth_pitch, unsigned int flags)
4749 struct wined3d_sub_resource_desc desc;
4750 struct wined3d_box b;
4752 TRACE("context %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, flags %#x.\n",
4753 context, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
4755 if (flags)
4756 FIXME("Ignoring flags %#x.\n", flags);
4758 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
4760 WARN("Resource %p is not GPU accessible.\n", resource);
4761 return;
4764 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4765 return;
4767 if (!box)
4769 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4770 box = &b;
4772 else if (box->left >= box->right || box->right > desc.width
4773 || box->top >= box->bottom || box->bottom > desc.height
4774 || box->front >= box->back || box->back > desc.depth)
4776 WARN("Invalid box %s specified.\n", debug_box(box));
4777 return;
4780 wined3d_device_context_lock(context);
4781 wined3d_device_context_emit_update_sub_resource(context, resource,
4782 sub_resource_idx, box, data, row_pitch, depth_pitch);
4783 wined3d_device_context_unlock(context);
4786 void CDECL wined3d_device_context_resolve_sub_resource(struct wined3d_device_context *context,
4787 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
4788 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
4789 enum wined3d_format_id format_id)
4791 struct wined3d_texture *dst_texture, *src_texture;
4792 unsigned int dst_level, src_level;
4793 struct wined3d_blt_fx fx = {0};
4794 RECT dst_rect, src_rect;
4796 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, "
4797 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
4798 context, dst_resource, dst_sub_resource_idx,
4799 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
4801 if (wined3d_format_is_typeless(dst_resource->format)
4802 || wined3d_format_is_typeless(src_resource->format))
4804 FIXME("Multisample resolve is not fully supported for typeless formats "
4805 "(dst_format %s, src_format %s, format %s).\n",
4806 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
4807 debug_d3dformat(format_id));
4809 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4811 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
4812 return;
4814 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4816 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
4817 return;
4820 wined3d_device_context_lock(context);
4821 fx.resolve_format_id = format_id;
4823 dst_texture = texture_from_resource(dst_resource);
4824 src_texture = texture_from_resource(src_resource);
4826 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4827 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
4828 wined3d_texture_get_level_height(dst_texture, dst_level));
4829 src_level = src_sub_resource_idx % src_texture->level_count;
4830 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4831 wined3d_texture_get_level_height(src_texture, src_level));
4832 wined3d_device_context_blt(context, dst_texture, dst_sub_resource_idx, &dst_rect,
4833 src_texture, src_sub_resource_idx, &src_rect, 0, &fx, WINED3D_TEXF_POINT);
4834 wined3d_device_context_unlock(context);
4837 HRESULT CDECL wined3d_device_context_clear_rendertarget_view(struct wined3d_device_context *context,
4838 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
4839 const struct wined3d_color *color, float depth, unsigned int stencil)
4841 struct wined3d_resource *resource;
4842 RECT r;
4844 TRACE("context %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4845 context, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4847 if (!flags)
4848 return WINED3D_OK;
4850 resource = view->resource;
4851 if (resource->type == WINED3D_RTYPE_BUFFER)
4853 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4854 return WINED3DERR_INVALIDCALL;
4857 if (!rect)
4859 SetRect(&r, 0, 0, view->width, view->height);
4860 rect = &r;
4862 else
4864 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4865 HRESULT hr;
4867 if (FAILED(hr = wined3d_resource_check_box_dimensions(resource, view->sub_resource_idx, &b)))
4868 return hr;
4871 wined3d_device_context_lock(context);
4872 wined3d_device_context_emit_clear_rendertarget_view(context, view, rect, flags, color, depth, stencil);
4873 wined3d_device_context_unlock(context);
4875 return WINED3D_OK;
4878 void CDECL wined3d_device_context_clear_uav_float(struct wined3d_device_context *context,
4879 struct wined3d_unordered_access_view *view, const struct wined3d_vec4 *clear_value)
4881 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_vec4(clear_value));
4883 if (!(view->format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & (WINED3DFMT_FLAG_FLOAT | WINED3DFMT_FLAG_NORMALISED)))
4885 WARN("Not supported for view format %s.\n", debug_d3dformat(view->format->id));
4886 return;
4889 wined3d_device_context_lock(context);
4890 wined3d_device_context_emit_clear_uav(context, view, (const struct wined3d_uvec4 *)clear_value, true);
4891 wined3d_device_context_unlock(context);
4894 void CDECL wined3d_device_context_clear_uav_uint(struct wined3d_device_context *context,
4895 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
4897 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_uvec4(clear_value));
4899 wined3d_device_context_lock(context);
4900 wined3d_device_context_emit_clear_uav(context, view, clear_value, false);
4901 wined3d_device_context_unlock(context);
4904 static unsigned int sanitise_map_flags(const struct wined3d_resource *resource, unsigned int flags)
4906 /* Not all flags make sense together, but Windows never returns an error.
4907 * Catch the cases that could cause issues. */
4908 if (flags & WINED3D_MAP_READ)
4910 if (flags & WINED3D_MAP_DISCARD)
4912 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
4913 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4915 if (flags & WINED3D_MAP_NOOVERWRITE)
4917 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
4918 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4921 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4923 if (!(resource->usage & WINED3DUSAGE_DYNAMIC))
4925 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
4926 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4928 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4929 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4931 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
4932 flags &= ~WINED3D_MAP_DISCARD;
4936 return flags;
4939 HRESULT CDECL wined3d_device_context_map(struct wined3d_device_context *context,
4940 struct wined3d_resource *resource, unsigned int sub_resource_idx,
4941 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
4943 struct wined3d_sub_resource_desc desc;
4944 struct wined3d_box b;
4945 HRESULT hr;
4947 TRACE("context %p, resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
4948 context, resource, sub_resource_idx, map_desc, debug_box(box), flags);
4950 if (!(flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
4952 WARN("No read/write flags specified.\n");
4953 return E_INVALIDARG;
4956 if ((flags & WINED3D_MAP_READ) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_R))
4958 WARN("Resource does not have MAP_R access.\n");
4959 return E_INVALIDARG;
4962 if ((flags & WINED3D_MAP_WRITE) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_W))
4964 WARN("Resource does not have MAP_W access.\n");
4965 return E_INVALIDARG;
4968 flags = sanitise_map_flags(resource, flags);
4970 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4971 return E_INVALIDARG;
4973 if (!box)
4975 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4976 box = &b;
4978 else if (FAILED(wined3d_resource_check_box_dimensions(resource, sub_resource_idx, box)))
4980 WARN("Map box is invalid.\n");
4982 if (resource->type != WINED3D_RTYPE_BUFFER && resource->type != WINED3D_RTYPE_TEXTURE_2D)
4983 return WINED3DERR_INVALIDCALL;
4985 if ((resource->format_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
4986 return WINED3DERR_INVALIDCALL;
4989 wined3d_device_context_lock(context);
4990 hr = wined3d_device_context_emit_map(context, resource, sub_resource_idx, map_desc, box, flags);
4991 wined3d_device_context_unlock(context);
4992 return hr;
4995 HRESULT CDECL wined3d_device_context_unmap(struct wined3d_device_context *context,
4996 struct wined3d_resource *resource, unsigned int sub_resource_idx)
4998 HRESULT hr;
4999 TRACE("context %p, resource %p, sub_resource_idx %u.\n", context, resource, sub_resource_idx);
5001 wined3d_device_context_lock(context);
5002 hr = wined3d_device_context_emit_unmap(context, resource, sub_resource_idx);
5003 wined3d_device_context_unlock(context);
5004 return hr;
5007 void CDECL wined3d_device_context_issue_query(struct wined3d_device_context *context,
5008 struct wined3d_query *query, unsigned int flags)
5010 TRACE("context %p, query %p, flags %#x.\n", context, query, flags);
5012 wined3d_device_context_lock(context);
5013 context->ops->issue_query(context, query, flags);
5014 wined3d_device_context_unlock(context);
5017 void CDECL wined3d_device_context_execute_command_list(struct wined3d_device_context *context,
5018 struct wined3d_command_list *list, bool restore_state)
5020 TRACE("context %p, list %p, restore_state %d.\n", context, list, restore_state);
5022 wined3d_device_context_lock(context);
5023 wined3d_device_context_emit_execute_command_list(context, list, restore_state);
5024 wined3d_device_context_unlock(context);
5027 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_rendertarget_view(
5028 const struct wined3d_device_context *context, unsigned int view_idx)
5030 unsigned int max_rt_count;
5032 TRACE("context %p, view_idx %u.\n", context, view_idx);
5034 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
5035 if (view_idx >= max_rt_count)
5037 WARN("Only %u render targets are supported.\n", max_rt_count);
5038 return NULL;
5041 return context->state->fb.render_targets[view_idx];
5044 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_depth_stencil_view(
5045 const struct wined3d_device_context *context)
5047 TRACE("context %p.\n", context);
5049 return context->state->fb.depth_stencil;
5052 void CDECL wined3d_device_context_generate_mipmaps(struct wined3d_device_context *context,
5053 struct wined3d_shader_resource_view *view)
5055 struct wined3d_texture *texture;
5057 TRACE("context %p, view %p.\n", context, view);
5059 if (view->resource->type == WINED3D_RTYPE_BUFFER)
5061 WARN("Called on buffer resource %p.\n", view->resource);
5062 return;
5065 texture = texture_from_resource(view->resource);
5066 if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
5068 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
5069 return;
5072 wined3d_device_context_lock(context);
5073 wined3d_device_context_emit_generate_mipmaps(context, view);
5074 wined3d_device_context_unlock(context);
5077 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
5078 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
5080 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
5081 struct wined3d_sub_resource_data data;
5082 struct wined3d_resource_desc desc;
5083 struct wined3d_map_desc map_desc;
5084 struct wined3d_texture *texture;
5085 HRESULT hr;
5087 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
5089 ERR("Failed to map source texture.\n");
5090 return NULL;
5093 data.data = map_desc.data;
5094 data.row_pitch = map_desc.row_pitch;
5095 data.slice_pitch = map_desc.slice_pitch;
5097 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5098 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
5099 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5100 desc.multisample_quality = 0;
5101 desc.usage = WINED3DUSAGE_DYNAMIC;
5102 desc.bind_flags = 0;
5103 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5104 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
5105 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
5106 desc.depth = 1;
5107 desc.size = 0;
5109 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
5110 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
5111 if (FAILED(hr))
5113 ERR("Failed to create cursor texture.\n");
5114 return NULL;
5117 return texture;
5120 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
5121 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
5123 unsigned int texture_level = sub_resource_idx % texture->level_count;
5124 unsigned int cursor_width, cursor_height;
5125 struct wined3d_map_desc map_desc;
5127 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
5128 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
5130 if (sub_resource_idx >= texture->level_count * texture->layer_count
5131 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
5132 return WINED3DERR_INVALIDCALL;
5134 if (device->cursor_texture)
5136 wined3d_texture_decref(device->cursor_texture);
5137 device->cursor_texture = NULL;
5140 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
5142 WARN("Texture %p has invalid format %s.\n",
5143 texture, debug_d3dformat(texture->resource.format->id));
5144 return WINED3DERR_INVALIDCALL;
5147 /* Cursor width and height must all be powers of two */
5148 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
5149 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
5150 if ((cursor_width & (cursor_width - 1)) || (cursor_height & (cursor_height - 1)))
5152 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width, cursor_height);
5153 return WINED3DERR_INVALIDCALL;
5156 /* Do not store the surface's pointer because the application may
5157 * release it after setting the cursor image. Windows doesn't
5158 * addref the set surface, so we can't do this either without
5159 * creating circular refcount dependencies. */
5160 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
5162 ERR("Failed to create cursor texture.\n");
5163 return WINED3DERR_INVALIDCALL;
5166 if (cursor_width == 32 && cursor_height == 32)
5168 UINT mask_size = cursor_width * cursor_height / 8;
5169 ICONINFO cursor_info;
5170 DWORD *mask_bits;
5171 HCURSOR cursor;
5173 /* 32-bit user32 cursors ignore the alpha channel if it's all
5174 * zeroes, and use the mask instead. Fill the mask with all ones
5175 * to ensure we still get a fully transparent cursor. */
5176 if (!(mask_bits = heap_alloc(mask_size)))
5177 return E_OUTOFMEMORY;
5178 memset(mask_bits, 0xff, mask_size);
5180 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
5181 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
5182 cursor_info.fIcon = FALSE;
5183 cursor_info.xHotspot = x_hotspot;
5184 cursor_info.yHotspot = y_hotspot;
5185 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
5186 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
5187 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
5189 /* Create our cursor and clean up. */
5190 cursor = CreateIconIndirect(&cursor_info);
5191 if (cursor_info.hbmMask)
5192 DeleteObject(cursor_info.hbmMask);
5193 if (cursor_info.hbmColor)
5194 DeleteObject(cursor_info.hbmColor);
5195 if (device->hardwareCursor)
5196 DestroyCursor(device->hardwareCursor);
5197 device->hardwareCursor = cursor;
5198 if (device->bCursorVisible)
5199 SetCursor(cursor);
5201 heap_free(mask_bits);
5204 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
5205 device->cursorWidth = cursor_width;
5206 device->cursorHeight = cursor_height;
5207 device->xHotSpot = x_hotspot;
5208 device->yHotSpot = y_hotspot;
5210 return WINED3D_OK;
5213 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
5214 int x_screen_space, int y_screen_space, DWORD flags)
5216 TRACE("device %p, x %d, y %d, flags %#x.\n",
5217 device, x_screen_space, y_screen_space, flags);
5219 device->xScreenSpace = x_screen_space;
5220 device->yScreenSpace = y_screen_space;
5222 if (device->hardwareCursor)
5224 POINT pt;
5226 GetCursorPos( &pt );
5227 if (x_screen_space == pt.x && y_screen_space == pt.y)
5228 return;
5229 SetCursorPos( x_screen_space, y_screen_space );
5231 /* Switch to the software cursor if position diverges from the hardware one. */
5232 GetCursorPos( &pt );
5233 if (x_screen_space != pt.x || y_screen_space != pt.y)
5235 if (device->bCursorVisible) SetCursor( NULL );
5236 DestroyCursor( device->hardwareCursor );
5237 device->hardwareCursor = 0;
5242 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5244 BOOL oldVisible = device->bCursorVisible;
5246 TRACE("device %p, show %#x.\n", device, show);
5249 * When ShowCursor is first called it should make the cursor appear at the OS's last
5250 * known cursor position.
5252 if (show && !oldVisible)
5254 POINT pt;
5255 GetCursorPos(&pt);
5256 device->xScreenSpace = pt.x;
5257 device->yScreenSpace = pt.y;
5260 if (device->hardwareCursor)
5262 device->bCursorVisible = show;
5263 if (show)
5264 SetCursor(device->hardwareCursor);
5265 else
5266 SetCursor(NULL);
5268 else if (device->cursor_texture)
5270 device->bCursorVisible = show;
5273 return oldVisible;
5276 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5278 struct wined3d_resource *resource, *cursor;
5280 TRACE("device %p.\n", device);
5282 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5284 TRACE("Checking resource %p for eviction.\n", resource);
5286 if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
5288 TRACE("Evicting %p.\n", resource);
5289 wined3d_cs_emit_unload_resource(device->cs, resource);
5294 void CDECL wined3d_device_context_flush(struct wined3d_device_context *context)
5296 TRACE("context %p.\n", context);
5298 wined3d_device_context_lock(context);
5299 context->ops->flush(context);
5300 wined3d_device_context_unlock(context);
5303 static void update_swapchain_flags(struct wined3d_texture *texture)
5305 unsigned int flags = texture->swapchain->state.desc.flags;
5307 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
5308 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
5309 else
5310 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
5312 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
5313 texture->flags |= WINED3D_TEXTURE_GET_DC;
5314 else
5315 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
5318 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5319 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5320 wined3d_device_reset_cb callback, BOOL reset_state)
5322 static struct wined3d_rendertarget_view *const views[WINED3D_MAX_RENDER_TARGETS];
5323 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
5324 struct wined3d_device_context *context = &device->cs->c;
5325 struct wined3d_swapchain_state *swapchain_state;
5326 struct wined3d_state *state = context->state;
5327 struct wined3d_swapchain_desc *current_desc;
5328 struct wined3d_resource *resource, *cursor;
5329 struct wined3d_rendertarget_view *view;
5330 struct wined3d_swapchain *swapchain;
5331 struct wined3d_view_desc view_desc;
5332 BOOL backbuffer_resized, windowed;
5333 HRESULT hr = WINED3D_OK;
5334 unsigned int i;
5336 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
5337 device, swapchain_desc, mode, callback, reset_state);
5339 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
5341 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5343 ERR("Failed to get the first implicit swapchain.\n");
5344 return WINED3DERR_INVALIDCALL;
5346 swapchain_state = &swapchain->state;
5347 current_desc = &swapchain_state->desc;
5349 if (reset_state)
5351 if (device->logo_texture)
5353 wined3d_texture_decref(device->logo_texture);
5354 device->logo_texture = NULL;
5356 if (device->cursor_texture)
5358 wined3d_texture_decref(device->cursor_texture);
5359 device->cursor_texture = NULL;
5361 state_unbind_resources(state);
5364 wined3d_device_context_set_rendertarget_views(context, 0, d3d_info->limits.max_rt_count, views, FALSE);
5365 wined3d_device_context_set_depth_stencil_view(context, NULL);
5367 if (reset_state)
5369 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5371 TRACE("Enumerating resource %p.\n", resource);
5372 if (FAILED(hr = callback(resource)))
5373 return hr;
5377 TRACE("New params:\n");
5378 TRACE("output %p\n", swapchain_desc->output);
5379 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5380 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5381 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5382 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5383 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5384 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5385 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5386 TRACE("device_window %p\n", swapchain_desc->device_window);
5387 TRACE("windowed %#x\n", swapchain_desc->windowed);
5388 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5389 if (swapchain_desc->enable_auto_depth_stencil)
5390 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5391 TRACE("flags %#x\n", swapchain_desc->flags);
5392 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5393 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5395 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
5396 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
5398 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
5399 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
5400 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
5401 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
5403 /* No special treatment of these parameters. Just store them */
5404 current_desc->swap_effect = swapchain_desc->swap_effect;
5405 current_desc->enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
5406 current_desc->auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
5407 current_desc->refresh_rate = swapchain_desc->refresh_rate;
5408 current_desc->auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
5410 if (swapchain_desc->device_window && swapchain_desc->device_window != current_desc->device_window)
5412 TRACE("Changing the device window from %p to %p.\n",
5413 current_desc->device_window, swapchain_desc->device_window);
5414 current_desc->device_window = swapchain_desc->device_window;
5415 swapchain_state->device_window = swapchain_desc->device_window;
5416 wined3d_swapchain_set_window(swapchain, NULL);
5419 backbuffer_resized = swapchain_desc->backbuffer_width != current_desc->backbuffer_width
5420 || swapchain_desc->backbuffer_height != current_desc->backbuffer_height;
5421 windowed = current_desc->windowed;
5423 if (!swapchain_desc->windowed != !windowed || swapchain->reapply_mode
5424 || mode || (!swapchain_desc->windowed && backbuffer_resized))
5426 /* Switch from windowed to fullscreen. */
5427 if (windowed && !swapchain_desc->windowed)
5429 HWND focus_window = device->create_parms.focus_window;
5431 if (!focus_window)
5432 focus_window = swapchain->state.device_window;
5433 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5435 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5436 return hr;
5440 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(&swapchain->state,
5441 swapchain_desc, mode)))
5442 return hr;
5444 /* Switch from fullscreen to windowed. */
5445 if (!windowed && swapchain_desc->windowed)
5446 wined3d_device_release_focus_window(device);
5448 else if (!swapchain_desc->windowed)
5450 DWORD style = swapchain_state->style;
5451 DWORD exstyle = swapchain_state->exstyle;
5452 struct wined3d_output_desc output_desc;
5454 /* If we're in fullscreen, and the mode wasn't changed, we have to get
5455 * the window back into the right position. Some applications
5456 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
5457 * up their mess. Guild Wars also loses the device during that. */
5458 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
5460 ERR("Failed to get output description, hr %#x.\n", hr);
5461 return hr;
5464 swapchain_state->style = 0;
5465 swapchain_state->exstyle = 0;
5466 wined3d_swapchain_state_setup_fullscreen(swapchain_state, swapchain_state->device_window,
5467 output_desc.desktop_rect.left, output_desc.desktop_rect.top,
5468 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height);
5469 swapchain_state->style = style;
5470 swapchain_state->exstyle = exstyle;
5473 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
5474 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
5475 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
5476 return hr;
5478 if (swapchain_desc->flags != current_desc->flags)
5480 current_desc->flags = swapchain_desc->flags;
5482 update_swapchain_flags(swapchain->front_buffer);
5483 for (i = 0; i < current_desc->backbuffer_count; ++i)
5485 update_swapchain_flags(swapchain->back_buffers[i]);
5489 if ((view = device->auto_depth_stencil_view))
5491 device->auto_depth_stencil_view = NULL;
5492 wined3d_rendertarget_view_decref(view);
5494 if (current_desc->enable_auto_depth_stencil)
5496 struct wined3d_resource_desc texture_desc;
5497 struct wined3d_texture *texture;
5499 TRACE("Creating the depth stencil buffer.\n");
5501 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5502 texture_desc.format = current_desc->auto_depth_stencil_format;
5503 texture_desc.multisample_type = current_desc->multisample_type;
5504 texture_desc.multisample_quality = current_desc->multisample_quality;
5505 texture_desc.usage = 0;
5506 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
5507 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5508 texture_desc.width = current_desc->backbuffer_width;
5509 texture_desc.height = current_desc->backbuffer_height;
5510 texture_desc.depth = 1;
5511 texture_desc.size = 0;
5513 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
5514 device->device_parent, &texture_desc, 0, &texture)))
5516 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
5517 return WINED3DERR_INVALIDCALL;
5520 view_desc.format_id = texture->resource.format->id;
5521 view_desc.flags = 0;
5522 view_desc.u.texture.level_idx = 0;
5523 view_desc.u.texture.level_count = 1;
5524 view_desc.u.texture.layer_idx = 0;
5525 view_desc.u.texture.layer_count = 1;
5526 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
5527 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
5528 wined3d_texture_decref(texture);
5529 if (FAILED(hr))
5531 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5532 return hr;
5536 if ((view = device->back_buffer_view))
5538 device->back_buffer_view = NULL;
5539 wined3d_rendertarget_view_decref(view);
5541 if (current_desc->backbuffer_count && current_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
5543 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
5545 view_desc.format_id = back_buffer->format->id;
5546 view_desc.flags = 0;
5547 view_desc.u.texture.level_idx = 0;
5548 view_desc.u.texture.level_count = 1;
5549 view_desc.u.texture.layer_idx = 0;
5550 view_desc.u.texture.layer_count = 1;
5551 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
5552 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
5554 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5555 return hr;
5559 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
5560 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
5561 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
5562 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
5564 if (reset_state)
5566 TRACE("Resetting state.\n");
5567 wined3d_device_context_emit_reset_state(&device->cs->c, false);
5568 state_cleanup(state);
5570 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5572 TRACE("Unloading resource %p.\n", resource);
5573 wined3d_cs_emit_unload_resource(device->cs, resource);
5576 device->adapter->adapter_ops->adapter_uninit_3d(device);
5578 wined3d_state_reset(state, &device->adapter->d3d_info);
5580 device_init_swapchain_state(device, swapchain);
5581 if (wined3d_settings.logo)
5582 device_load_logo(device, wined3d_settings.logo);
5584 else
5586 if ((view = device->back_buffer_view))
5587 wined3d_device_context_set_rendertarget_views(context, 0, 1, &view, FALSE);
5588 if ((view = device->auto_depth_stencil_view))
5589 wined3d_device_context_set_depth_stencil_view(context, view);
5592 if (reset_state)
5593 hr = device->adapter->adapter_ops->adapter_init_3d(device);
5595 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5596 * first use
5598 return hr;
5601 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5603 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5605 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5607 return WINED3D_OK;
5611 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5612 struct wined3d_device_creation_parameters *parameters)
5614 TRACE("device %p, parameters %p.\n", device, parameters);
5616 *parameters = device->create_parms;
5619 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5621 TRACE("device %p.\n", device);
5623 return device->wined3d;
5626 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5627 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5629 struct wined3d_swapchain *swapchain;
5631 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5632 device, swapchain_idx, flags, ramp);
5634 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5635 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5638 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5639 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5641 struct wined3d_swapchain *swapchain;
5643 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5644 device, swapchain_idx, ramp);
5646 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5647 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5650 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5652 TRACE("device %p, resource %p.\n", device, resource);
5654 wined3d_not_from_cs(device->cs);
5656 list_add_head(&device->resources, &resource->resource_list_entry);
5659 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5661 TRACE("device %p, resource %p.\n", device, resource);
5663 wined3d_not_from_cs(device->cs);
5665 list_remove(&resource->resource_list_entry);
5668 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5670 enum wined3d_resource_type type = resource->type;
5671 struct wined3d_state *state = device->cs->c.state;
5672 struct wined3d_rendertarget_view *rtv;
5673 unsigned int i;
5675 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5677 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
5679 if ((rtv = state->fb.render_targets[i]) && rtv->resource == resource)
5680 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5683 if ((rtv = state->fb.depth_stencil) && rtv->resource == resource)
5684 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5686 switch (type)
5688 case WINED3D_RTYPE_TEXTURE_1D:
5689 case WINED3D_RTYPE_TEXTURE_2D:
5690 case WINED3D_RTYPE_TEXTURE_3D:
5691 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
5693 if (&state->textures[i]->resource == resource)
5695 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
5696 state->textures[i] = NULL;
5699 break;
5701 case WINED3D_RTYPE_BUFFER:
5702 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
5704 if (&state->streams[i].buffer->resource == resource)
5706 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5707 state->streams[i].buffer = NULL;
5711 if (&state->index_buffer->resource == resource)
5713 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5714 state->index_buffer = NULL;
5716 break;
5718 default:
5719 break;
5722 /* Remove the resource from the resourceStore */
5723 device_resource_remove(device, resource);
5725 TRACE("Resource released.\n");
5728 static int wined3d_so_desc_compare(const void *key, const struct wine_rb_entry *entry)
5730 const struct wined3d_stream_output_desc *desc = &WINE_RB_ENTRY_VALUE(entry,
5731 struct wined3d_so_desc_entry, entry)->desc;
5732 const struct wined3d_stream_output_desc *k = key;
5733 unsigned int i;
5734 int ret;
5736 if ((ret = wined3d_uint32_compare(k->element_count, desc->element_count)))
5737 return ret;
5738 if ((ret = wined3d_uint32_compare(k->buffer_stride_count, desc->buffer_stride_count)))
5739 return ret;
5740 if ((ret = wined3d_uint32_compare(k->rasterizer_stream_idx, desc->rasterizer_stream_idx)))
5741 return ret;
5743 for (i = 0; i < k->element_count; ++i)
5745 const struct wined3d_stream_output_element *b = &desc->elements[i];
5746 const struct wined3d_stream_output_element *a = &k->elements[i];
5748 if ((ret = wined3d_uint32_compare(a->stream_idx, b->stream_idx)))
5749 return ret;
5750 if ((ret = (!a->semantic_name - !b->semantic_name)))
5751 return ret;
5752 if (a->semantic_name && (ret = strcmp(a->semantic_name, b->semantic_name)))
5753 return ret;
5754 if ((ret = wined3d_uint32_compare(a->semantic_idx, b->semantic_idx)))
5755 return ret;
5756 if ((ret = wined3d_uint32_compare(a->component_idx, b->component_idx)))
5757 return ret;
5758 if ((ret = wined3d_uint32_compare(a->component_count, b->component_count)))
5759 return ret;
5760 if ((ret = wined3d_uint32_compare(a->output_slot, b->output_slot)))
5761 return ret;
5764 for (i = 0; i < k->buffer_stride_count; ++i)
5766 if ((ret = wined3d_uint32_compare(k->buffer_strides[i], desc->buffer_strides[i])))
5767 return ret;
5770 return 0;
5773 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5775 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5777 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5780 static int wined3d_rasterizer_state_compare(const void *key, const struct wine_rb_entry *entry)
5782 const struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
5784 return memcmp(&state->desc, key, sizeof(state->desc));
5787 static int wined3d_blend_state_compare(const void *key, const struct wine_rb_entry *entry)
5789 const struct wined3d_blend_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
5791 return memcmp(&state->desc, key, sizeof(state->desc));
5794 static int wined3d_depth_stencil_state_compare(const void *key, const struct wine_rb_entry *entry)
5796 const struct wined3d_depth_stencil_state *state
5797 = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
5799 return memcmp(&state->desc, key, sizeof(state->desc));
5802 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
5803 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
5804 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
5805 const BOOL *supported_extensions, struct wined3d_device_parent *device_parent)
5807 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
5808 const struct wined3d_fragment_pipe_ops *fragment_pipeline;
5809 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5810 unsigned int i;
5811 HRESULT hr;
5813 device->ref = 1;
5814 device->wined3d = wined3d;
5815 wined3d_incref(device->wined3d);
5816 device->adapter = adapter;
5817 device->device_parent = device_parent;
5818 list_init(&device->resources);
5819 list_init(&device->shaders);
5820 device->surface_alignment = surface_alignment;
5822 /* Save the creation parameters. */
5823 device->create_parms.adapter_idx = adapter_idx;
5824 device->create_parms.device_type = device_type;
5825 device->create_parms.focus_window = focus_window;
5826 device->create_parms.flags = flags;
5828 device->shader_backend = adapter->shader_backend;
5830 vertex_pipeline = adapter->vertex_pipe;
5832 fragment_pipeline = adapter->fragment_pipe;
5834 wine_rb_init(&device->so_descs, wined3d_so_desc_compare);
5835 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5836 wine_rb_init(&device->rasterizer_states, wined3d_rasterizer_state_compare);
5837 wine_rb_init(&device->blend_states, wined3d_blend_state_compare);
5838 wine_rb_init(&device->depth_stencil_states, wined3d_depth_stencil_state_compare);
5840 if (vertex_pipeline->vp_states && fragment_pipeline->states
5841 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
5842 &adapter->d3d_info, supported_extensions, vertex_pipeline,
5843 fragment_pipeline, adapter->misc_state_template)))
5845 ERR("Failed to compile state table, hr %#x.\n", hr);
5846 wine_rb_destroy(&device->samplers, NULL, NULL);
5847 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5848 wine_rb_destroy(&device->blend_states, NULL, NULL);
5849 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5850 wine_rb_destroy(&device->so_descs, NULL, NULL);
5851 wined3d_decref(device->wined3d);
5852 return hr;
5855 device->max_frame_latency = 3;
5857 if (!(device->cs = wined3d_cs_create(device, levels, level_count)))
5859 WARN("Failed to create command stream.\n");
5860 hr = E_FAIL;
5861 goto err;
5864 return WINED3D_OK;
5866 err:
5867 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
5869 heap_free(device->multistate_funcs[i]);
5871 wine_rb_destroy(&device->samplers, NULL, NULL);
5872 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5873 wine_rb_destroy(&device->blend_states, NULL, NULL);
5874 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5875 wine_rb_destroy(&device->so_descs, NULL, NULL);
5876 wined3d_decref(device->wined3d);
5877 return hr;
5880 void device_invalidate_state(const struct wined3d_device *device, unsigned int state_id)
5882 unsigned int representative, i, idx, shift;
5883 struct wined3d_context *context;
5885 wined3d_from_cs(device->cs);
5887 if (STATE_IS_COMPUTE(state_id))
5889 for (i = 0; i < device->context_count; ++i)
5890 context_invalidate_compute_state(device->contexts[i], state_id);
5891 return;
5894 representative = device->state_table[state_id].representative;
5895 idx = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
5896 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
5897 for (i = 0; i < device->context_count; ++i)
5899 device->contexts[i]->dirty_graphics_states[idx] |= (1u << shift);
5903 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5904 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5906 if (message == WM_DESTROY)
5908 TRACE("unregister window %p.\n", window);
5909 wined3d_unregister_window(window);
5911 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5912 ERR("Window %p is not the focus window for device %p.\n", window, device);
5914 else if (message == WM_DISPLAYCHANGE)
5916 device->device_parent->ops->mode_changed(device->device_parent);
5918 else if (message == WM_ACTIVATEAPP)
5920 unsigned int i = device->swapchain_count;
5922 /* Deactivating the implicit swapchain may cause the application
5923 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
5924 * deactivate the implicit swapchain last, and to avoid accessing the
5925 * "device" pointer afterwards. */
5926 while (i--)
5927 wined3d_swapchain_activate(device->swapchains[i], wparam);
5929 else if (message == WM_SYSCOMMAND)
5931 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5933 if (unicode)
5934 DefWindowProcW(window, message, wparam, lparam);
5935 else
5936 DefWindowProcA(window, message, wparam, lparam);
5940 if (unicode)
5941 return CallWindowProcW(proc, window, message, wparam, lparam);
5942 else
5943 return CallWindowProcA(proc, window, message, wparam, lparam);