wined3d: Release push constant buffers in wined3d_device_uninit_3d().
[wine.git] / dlls / wined3d / device.c
blob0b4f56000bdcf640dfe5e97fb0b6a46f0d0442f7
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 "wined3d_private.h"
28 #include "wined3d_vk.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
31 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
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 unsigned int 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_lock_cleanup(&device->bo_map_lock);
251 wined3d_decref(device->wined3d);
252 device->wined3d = NULL;
255 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
257 unsigned int refcount = InterlockedDecrement(&device->ref);
259 TRACE("%p decreasing refcount to %u.\n", device, refcount);
261 if (!refcount)
263 wined3d_mutex_lock();
264 device->adapter->adapter_ops->adapter_destroy_device(device);
265 TRACE("Destroyed device %p.\n", device);
266 wined3d_mutex_unlock();
269 return refcount;
272 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
274 TRACE("device %p.\n", device);
276 return device->swapchain_count;
279 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
281 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
283 if (swapchain_idx >= device->swapchain_count)
285 WARN("swapchain_idx %u >= swapchain_count %u.\n",
286 swapchain_idx, device->swapchain_count);
287 return NULL;
290 return device->swapchains[swapchain_idx];
293 static void device_load_logo(struct wined3d_device *device, const char *filename)
295 struct wined3d_color_key color_key;
296 struct wined3d_resource_desc desc;
297 HBITMAP hbm;
298 BITMAP bm;
299 HRESULT hr;
300 HDC dcb = NULL, dcs = NULL;
302 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
304 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
305 return;
307 GetObjectA(hbm, sizeof(BITMAP), &bm);
309 if (!(dcb = CreateCompatibleDC(NULL)))
310 goto out;
311 SelectObject(dcb, hbm);
313 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
314 desc.format = WINED3DFMT_B5G6R5_UNORM;
315 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
316 desc.multisample_quality = 0;
317 desc.usage = WINED3DUSAGE_DYNAMIC;
318 desc.bind_flags = 0;
319 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
320 desc.width = bm.bmWidth;
321 desc.height = bm.bmHeight;
322 desc.depth = 1;
323 desc.size = 0;
324 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_GET_DC,
325 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
327 ERR("Wine logo requested, but failed to create texture, hr %#lx.\n", hr);
328 goto out;
331 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
333 wined3d_texture_decref(device->logo_texture);
334 device->logo_texture = NULL;
335 goto out;
337 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
338 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
340 color_key.color_space_low_value = 0;
341 color_key.color_space_high_value = 0;
342 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
344 out:
345 if (dcb) DeleteDC(dcb);
346 if (hbm) DeleteObject(hbm);
349 /* Context activation is done by the caller. */
350 static void wined3d_device_gl_create_dummy_textures(struct wined3d_device_gl *device_gl,
351 struct wined3d_context_gl *context_gl)
353 struct wined3d_dummy_textures *textures = &device_gl->dummy_textures;
354 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
355 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
356 unsigned int i;
357 DWORD color;
359 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
360 color = 0x000000ff;
361 else
362 color = 0x00000000;
364 /* Under DirectX you can sample even if no texture is bound, whereas
365 * OpenGL will only allow that when a valid texture is bound.
366 * We emulate this by creating dummy textures and binding them
367 * to each texture stage when the currently set D3D texture is NULL. */
368 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
370 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
371 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
372 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
373 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
374 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
376 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
377 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
378 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
379 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
380 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
382 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
384 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
385 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
386 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
387 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
388 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
391 if (gl_info->supported[EXT_TEXTURE3D])
393 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
394 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
395 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
396 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
397 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
400 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
402 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
403 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
404 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
405 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
407 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
408 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
412 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
414 DWORD cube_array_data[6];
416 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
417 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
418 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
419 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
420 cube_array_data[i] = color;
421 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
422 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
425 if (gl_info->supported[EXT_TEXTURE_ARRAY])
427 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
428 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
429 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
430 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
431 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
433 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
434 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
435 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
436 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
437 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
440 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
442 GLuint buffer;
444 GL_EXTCALL(glGenBuffers(1, &buffer));
445 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
446 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
447 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
449 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
450 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
451 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
452 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
453 GL_EXTCALL(glDeleteBuffers(1, &buffer));
456 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
458 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
459 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
460 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
461 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
463 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
464 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
465 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
466 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
468 if (gl_info->supported[ARB_CLEAR_TEXTURE])
470 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
471 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
473 else
475 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
479 checkGLcall("create dummy textures");
481 wined3d_context_gl_bind_dummy_textures(context_gl);
484 /* Context activation is done by the caller. */
485 static void wined3d_device_gl_destroy_dummy_textures(struct wined3d_device_gl *device_gl,
486 struct wined3d_context_gl *context_gl)
488 struct wined3d_dummy_textures *dummy_textures = &device_gl->dummy_textures;
489 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
491 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
493 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
494 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
497 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
498 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
500 if (gl_info->supported[EXT_TEXTURE_ARRAY])
502 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
503 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
506 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
507 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
509 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
510 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
512 if (gl_info->supported[EXT_TEXTURE3D])
513 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
515 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
516 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
518 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
519 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
521 checkGLcall("delete dummy textures");
523 memset(dummy_textures, 0, sizeof(*dummy_textures));
526 /* Context activation is done by the caller. */
527 void wined3d_device_create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
529 struct wined3d_sampler_desc desc;
530 HRESULT hr;
532 desc.address_u = WINED3D_TADDRESS_WRAP;
533 desc.address_v = WINED3D_TADDRESS_WRAP;
534 desc.address_w = WINED3D_TADDRESS_WRAP;
535 memset(desc.border_color, 0, sizeof(desc.border_color));
536 desc.mag_filter = WINED3D_TEXF_POINT;
537 desc.min_filter = WINED3D_TEXF_POINT;
538 desc.mip_filter = WINED3D_TEXF_NONE;
539 desc.lod_bias = 0.0f;
540 desc.min_lod = -1000.0f;
541 desc.max_lod = 1000.0f;
542 desc.mip_base_level = 0;
543 desc.max_anisotropy = 1;
544 desc.compare = FALSE;
545 desc.comparison_func = WINED3D_CMP_NEVER;
546 desc.srgb_decode = TRUE;
548 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
549 * instructions allow access to resources without using samplers.
550 * In GLSL, resources are always accessed through sampler or image variables. The default
551 * sampler object is used to emulate the direct resource access when there is no sampler state
552 * to use.
554 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
556 ERR("Failed to create default sampler, hr %#lx.\n", hr);
557 device->default_sampler = NULL;
560 /* In D3D10+, a NULL sampler maps to the default sampler state. */
561 desc.address_u = WINED3D_TADDRESS_CLAMP;
562 desc.address_v = WINED3D_TADDRESS_CLAMP;
563 desc.address_w = WINED3D_TADDRESS_CLAMP;
564 desc.mag_filter = WINED3D_TEXF_LINEAR;
565 desc.min_filter = WINED3D_TEXF_LINEAR;
566 desc.mip_filter = WINED3D_TEXF_LINEAR;
567 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
569 ERR("Failed to create null sampler, hr %#lx.\n", hr);
570 device->null_sampler = NULL;
574 void wined3d_device_destroy_default_samplers(struct wined3d_device *device)
576 wined3d_sampler_decref(device->default_sampler);
577 device->default_sampler = NULL;
578 wined3d_sampler_decref(device->null_sampler);
579 device->null_sampler = NULL;
582 static bool wined3d_null_image_vk_init(struct wined3d_image_vk *image, struct wined3d_context_vk *context_vk,
583 VkCommandBuffer vk_command_buffer, VkImageType type, unsigned int layer_count, unsigned int sample_count)
585 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
586 VkImageSubresourceRange range;
587 uint32_t flags = 0;
589 static const VkClearColorValue colour = {{0}};
591 TRACE("image %p, context_vk %p, vk_command_buffer %p, type %#x, layer_count %u, sample_count %u.\n",
592 image, context_vk, vk_command_buffer, type, layer_count, sample_count);
594 if (type == VK_IMAGE_TYPE_2D && layer_count >= 6)
595 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
597 if (!wined3d_context_vk_create_image(context_vk, type,
598 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_FORMAT_R8G8B8A8_UNORM,
599 1, 1, 1, sample_count, 1, layer_count, flags, image))
601 return false;
604 wined3d_context_vk_reference_image(context_vk, image);
606 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
607 range.baseMipLevel = 0;
608 range.levelCount = 1;
609 range.baseArrayLayer = 0;
610 range.layerCount = layer_count;
612 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
613 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
614 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, image->vk_image, &range);
616 VK_CALL(vkCmdClearColorImage(vk_command_buffer, image->vk_image,
617 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour, 1, &range));
619 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
620 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0,
621 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, image->vk_image, &range);
623 TRACE("Created NULL image 0x%s, memory 0x%s.\n",
624 wine_dbgstr_longlong(image->vk_image), wine_dbgstr_longlong(image->vk_memory));
626 return true;
629 bool wined3d_device_vk_create_null_resources(struct wined3d_device_vk *device_vk,
630 struct wined3d_context_vk *context_vk)
632 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
633 const struct wined3d_vk_info *vk_info;
634 const struct wined3d_format *format;
635 VkMemoryPropertyFlags memory_type;
636 VkCommandBuffer vk_command_buffer;
637 unsigned int sample_count = 2;
638 VkBufferUsageFlags usage;
640 format = wined3d_get_format(device_vk->d.adapter, WINED3DFMT_R8G8B8A8_UNORM, WINED3D_BIND_SHADER_RESOURCE);
641 while (sample_count && !(sample_count & format->multisample_types))
642 sample_count <<= 1;
644 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
646 ERR("Failed to get command buffer.\n");
647 return false;
650 vk_info = context_vk->vk_info;
652 usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
653 | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
654 memory_type = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
655 if (!wined3d_context_vk_create_bo(context_vk, 16, usage, memory_type, &r->bo))
656 return false;
657 VK_CALL(vkCmdFillBuffer(vk_command_buffer, r->bo.vk_buffer, r->bo.b.buffer_offset, r->bo.size, 0x00000000u));
658 r->buffer_info.buffer = r->bo.vk_buffer;
659 r->buffer_info.offset = r->bo.b.buffer_offset;
660 r->buffer_info.range = r->bo.size;
662 if (!wined3d_null_image_vk_init(&r->image_1d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_1D, 1, 1))
664 ERR("Failed to create 1D image.\n");
665 goto fail;
668 if (!wined3d_null_image_vk_init(&r->image_2d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 6, 1))
670 ERR("Failed to create 2D image.\n");
671 goto fail;
674 if (!wined3d_null_image_vk_init(&r->image_2dms, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 1, sample_count))
676 ERR("Failed to create 2D MSAA image.\n");
677 goto fail;
680 if (!wined3d_null_image_vk_init(&r->image_3d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_3D, 1, 1))
682 ERR("Failed to create 3D image.\n");
683 goto fail;
686 return true;
688 fail:
689 if (r->image_2dms.vk_image)
690 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
691 if (r->image_2d.vk_image)
692 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
693 if (r->image_1d.vk_image)
694 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
695 wined3d_context_vk_reference_bo(context_vk, &r->bo);
696 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
697 return false;
700 void wined3d_device_vk_destroy_null_resources(struct wined3d_device_vk *device_vk,
701 struct wined3d_context_vk *context_vk)
703 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
705 /* We don't track command buffer references to NULL resources. We easily
706 * could, but it doesn't seem worth it. */
707 wined3d_context_vk_reference_image(context_vk, &r->image_3d);
708 wined3d_context_vk_destroy_image(context_vk, &r->image_3d);
709 wined3d_context_vk_reference_image(context_vk, &r->image_2dms);
710 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
711 wined3d_context_vk_reference_image(context_vk, &r->image_2d);
712 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
713 wined3d_context_vk_reference_image(context_vk, &r->image_1d);
714 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
715 wined3d_context_vk_reference_bo(context_vk, &r->bo);
716 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
719 bool wined3d_device_vk_create_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
721 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
722 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
723 VkBufferViewCreateInfo buffer_create_info;
724 const struct wined3d_vk_info *vk_info;
725 VkImageViewCreateInfo view_desc;
726 VkResult vr;
728 vk_info = context_vk->vk_info;
730 buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
731 buffer_create_info.pNext = NULL;
732 buffer_create_info.flags = 0;
733 buffer_create_info.buffer = r->bo.vk_buffer;
734 buffer_create_info.format = VK_FORMAT_R32_UINT;
735 buffer_create_info.offset = r->bo.b.buffer_offset;
736 buffer_create_info.range = r->bo.size;
738 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
739 &buffer_create_info, NULL, &v->vk_view_buffer_uint))) < 0)
741 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
742 return false;
744 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_uint));
746 buffer_create_info.format = VK_FORMAT_R32G32B32A32_SFLOAT;
747 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
748 &buffer_create_info, NULL, &v->vk_view_buffer_float))) < 0)
750 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
751 goto fail;
753 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_float));
755 view_desc.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
756 view_desc.pNext = NULL;
757 view_desc.flags = 0;
758 view_desc.image = r->image_1d.vk_image;
759 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D;
760 view_desc.format = VK_FORMAT_R8G8B8A8_UNORM;
761 view_desc.components.r = VK_COMPONENT_SWIZZLE_ZERO;
762 view_desc.components.g = VK_COMPONENT_SWIZZLE_ZERO;
763 view_desc.components.b = VK_COMPONENT_SWIZZLE_ZERO;
764 view_desc.components.a = VK_COMPONENT_SWIZZLE_ZERO;
765 view_desc.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
766 view_desc.subresourceRange.baseMipLevel = 0;
767 view_desc.subresourceRange.levelCount = 1;
768 view_desc.subresourceRange.baseArrayLayer = 0;
769 view_desc.subresourceRange.layerCount = 1;
770 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d.imageView))) < 0)
772 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
773 goto fail;
775 v->vk_info_1d.sampler = VK_NULL_HANDLE;
776 v->vk_info_1d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
777 TRACE("Created 1D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d.imageView));
779 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D_ARRAY;
780 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d_array.imageView))) < 0)
782 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
783 goto fail;
785 v->vk_info_1d_array.sampler = VK_NULL_HANDLE;
786 v->vk_info_1d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
787 TRACE("Created 1D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d_array.imageView));
789 view_desc.image = r->image_2d.vk_image;
790 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
791 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d.imageView))) < 0)
793 ERR("Failed to create 2D image view, vr %s.\n", wined3d_debug_vkresult(vr));
794 goto fail;
796 v->vk_info_2d.sampler = VK_NULL_HANDLE;
797 v->vk_info_2d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
798 TRACE("Created 2D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d.imageView));
800 view_desc.image = r->image_2dms.vk_image;
801 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
802 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms.imageView))) < 0)
804 ERR("Failed to create 2D MSAA image view, vr %s.\n", wined3d_debug_vkresult(vr));
805 goto fail;
807 v->vk_info_2dms.sampler = VK_NULL_HANDLE;
808 v->vk_info_2dms.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
809 TRACE("Created 2D MSAA image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms.imageView));
811 view_desc.image = r->image_3d.vk_image;
812 view_desc.viewType = VK_IMAGE_VIEW_TYPE_3D;
813 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_3d.imageView))) < 0)
815 ERR("Failed to create 3D image view, vr %s.\n", wined3d_debug_vkresult(vr));
816 goto fail;
818 v->vk_info_3d.sampler = VK_NULL_HANDLE;
819 v->vk_info_3d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
820 TRACE("Created 3D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_3d.imageView));
822 view_desc.image = r->image_2d.vk_image;
823 view_desc.subresourceRange.layerCount = 6;
824 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
825 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube.imageView))) < 0)
827 ERR("Failed to create cube image view, vr %s.\n", wined3d_debug_vkresult(vr));
828 goto fail;
830 v->vk_info_cube.sampler = VK_NULL_HANDLE;
831 v->vk_info_cube.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
832 TRACE("Created cube image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube.imageView));
834 view_desc.subresourceRange.layerCount = 1;
835 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
836 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d_array.imageView))) < 0)
838 ERR("Failed to create 2D array image view, vr %s.\n", wined3d_debug_vkresult(vr));
839 goto fail;
841 v->vk_info_2d_array.sampler = VK_NULL_HANDLE;
842 v->vk_info_2d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
843 TRACE("Created 2D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d_array.imageView));
845 view_desc.image = r->image_2dms.vk_image;
846 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
847 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms_array.imageView))) < 0)
849 ERR("Failed to create 2D MSAA array image view, vr %s.\n", wined3d_debug_vkresult(vr));
850 goto fail;
852 v->vk_info_2dms_array.sampler = VK_NULL_HANDLE;
853 v->vk_info_2dms_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
854 TRACE("Created 2D MSAA array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms_array.imageView));
856 view_desc.image = r->image_2d.vk_image;
857 view_desc.subresourceRange.layerCount = 6;
858 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
859 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube_array.imageView))) < 0)
861 ERR("Failed to create cube array image view, vr %s.\n", wined3d_debug_vkresult(vr));
862 goto fail;
864 v->vk_info_cube_array.sampler = VK_NULL_HANDLE;
865 v->vk_info_cube_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
866 TRACE("Created cube array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube_array.imageView));
868 return true;
870 fail:
871 if (v->vk_info_cube_array.imageView)
872 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube_array.imageView, NULL));
873 if (v->vk_info_2d_array.imageView)
874 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d_array.imageView, NULL));
875 if (v->vk_info_cube.imageView)
876 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube.imageView, NULL));
877 if (v->vk_info_3d.imageView)
878 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_3d.imageView, NULL));
879 if (v->vk_info_2dms.imageView)
880 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2dms.imageView, NULL));
881 if (v->vk_info_2d.imageView)
882 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d.imageView, NULL));
883 if (v->vk_info_1d_array.imageView)
884 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d_array.imageView, NULL));
885 if (v->vk_info_1d.imageView)
886 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d.imageView, NULL));
887 if (v->vk_view_buffer_float)
888 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_float, NULL));
889 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_uint, NULL));
890 return false;
893 void wined3d_device_vk_destroy_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
895 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
896 uint64_t id = context_vk->current_command_buffer.id;
898 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube_array.imageView, id);
899 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms_array.imageView, id);
900 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d_array.imageView, id);
901 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube.imageView, id);
902 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_3d.imageView, id);
903 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms.imageView, id);
904 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d.imageView, id);
905 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d_array.imageView, id);
906 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d.imageView, id);
908 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_float, id);
909 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_uint, id);
912 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
914 unsigned int screensaver_active;
916 TRACE("device %p, window %p.\n", device, window);
918 if (!wined3d_register_window(NULL, window, device, 0))
920 ERR("Failed to register window %p.\n", window);
921 return E_FAIL;
924 InterlockedExchangePointer((void **)&device->focus_window, window);
925 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
926 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
927 if ((device->restore_screensaver = !!screensaver_active))
928 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
930 return WINED3D_OK;
933 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
935 TRACE("device %p.\n", device);
937 if (device->focus_window) wined3d_unregister_window(device->focus_window);
938 InterlockedExchangePointer((void **)&device->focus_window, NULL);
939 if (device->restore_screensaver)
941 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
942 device->restore_screensaver = FALSE;
946 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
948 struct wined3d_rendertarget_view *views[WINED3D_MAX_RENDER_TARGETS] = {0};
949 BOOL ds_enable = swapchain->state.desc.enable_auto_depth_stencil;
950 struct wined3d_device_context *context = &device->cs->c;
952 if (device->back_buffer_view)
953 views[0] = device->back_buffer_view;
954 wined3d_device_context_set_rendertarget_views(context, 0,
955 device->adapter->d3d_info.limits.max_rt_count, views, !!device->back_buffer_view);
957 wined3d_device_context_set_depth_stencil_view(context, ds_enable ? device->auto_depth_stencil_view : NULL);
960 static struct wined3d_allocator_chunk *wined3d_allocator_gl_create_chunk(struct wined3d_allocator *allocator,
961 struct wined3d_context *context, unsigned int memory_type, size_t chunk_size)
963 struct wined3d_allocator_chunk_gl *chunk_gl;
964 struct wined3d_context_gl *context_gl;
966 TRACE("allocator %p, context %p, memory_type %u, chunk_size %Iu.\n", allocator, context, memory_type, chunk_size);
968 if (!context)
969 return NULL;
970 context_gl = wined3d_context_gl(context);
972 if (!(chunk_gl = heap_alloc(sizeof(*chunk_gl))))
973 return NULL;
975 if (!wined3d_allocator_chunk_init(&chunk_gl->c, allocator))
977 heap_free(chunk_gl);
978 return NULL;
981 chunk_gl->memory_type = memory_type;
982 if (!(chunk_gl->gl_buffer = wined3d_context_gl_allocate_vram_chunk_buffer(context_gl, memory_type, chunk_size)))
984 wined3d_allocator_chunk_cleanup(&chunk_gl->c);
985 heap_free(chunk_gl);
986 return NULL;
988 list_add_head(&allocator->pools[memory_type].chunks, &chunk_gl->c.entry);
990 return &chunk_gl->c;
993 static void wined3d_allocator_gl_destroy_chunk(struct wined3d_allocator_chunk *chunk)
995 struct wined3d_device_gl *device_gl = wined3d_device_gl_from_allocator(chunk->allocator);
996 struct wined3d_allocator_chunk_gl *chunk_gl = wined3d_allocator_chunk_gl(chunk);
997 const struct wined3d_gl_info *gl_info;
998 struct wined3d_context_gl *context_gl;
1000 TRACE("chunk %p.\n", chunk);
1002 context_gl = wined3d_context_gl(context_acquire(&device_gl->d, NULL, 0));
1003 gl_info = context_gl->gl_info;
1005 wined3d_context_gl_bind_bo(context_gl, GL_PIXEL_UNPACK_BUFFER, chunk_gl->gl_buffer);
1006 if (chunk_gl->c.map_ptr)
1007 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
1008 GL_EXTCALL(glDeleteBuffers(1, &chunk_gl->gl_buffer));
1009 TRACE("Freed buffer %u.\n", chunk_gl->gl_buffer);
1010 wined3d_allocator_chunk_cleanup(&chunk_gl->c);
1011 heap_free(chunk_gl);
1013 context_release(&context_gl->c);
1016 static const struct wined3d_allocator_ops wined3d_allocator_gl_ops =
1018 .allocator_create_chunk = wined3d_allocator_gl_create_chunk,
1019 .allocator_destroy_chunk = wined3d_allocator_gl_destroy_chunk,
1022 static const struct
1024 GLbitfield flags;
1026 gl_memory_types[] =
1028 {0},
1029 {GL_MAP_READ_BIT},
1030 {GL_MAP_WRITE_BIT},
1031 {GL_MAP_READ_BIT | GL_MAP_WRITE_BIT},
1033 {GL_CLIENT_STORAGE_BIT},
1034 {GL_CLIENT_STORAGE_BIT | GL_MAP_READ_BIT},
1035 {GL_CLIENT_STORAGE_BIT | GL_MAP_WRITE_BIT},
1036 {GL_CLIENT_STORAGE_BIT | GL_MAP_READ_BIT | GL_MAP_WRITE_BIT},
1039 static unsigned int wined3d_device_gl_find_memory_type(GLbitfield flags)
1041 unsigned int i;
1043 for (i = 0; i < ARRAY_SIZE(gl_memory_types); ++i)
1045 if (gl_memory_types[i].flags == flags)
1046 return i;
1049 assert(0);
1050 return 0;
1053 GLbitfield wined3d_device_gl_get_memory_type_flags(unsigned int memory_type_idx)
1055 return gl_memory_types[memory_type_idx].flags;
1058 static struct wined3d_allocator_block *wined3d_device_gl_allocate_memory(struct wined3d_device_gl *device_gl,
1059 struct wined3d_context_gl *context_gl, unsigned int memory_type, GLsizeiptr size, GLuint *id)
1061 struct wined3d_allocator *allocator = &device_gl->allocator;
1062 struct wined3d_allocator_block *block;
1064 wined3d_device_gl_allocator_lock(device_gl);
1066 if (size > WINED3D_ALLOCATOR_CHUNK_SIZE / 2)
1068 if (context_gl)
1069 *id = wined3d_context_gl_allocate_vram_chunk_buffer(context_gl, memory_type, size);
1070 wined3d_device_gl_allocator_unlock(device_gl);
1071 return NULL;
1074 if (!(block = wined3d_allocator_allocate(allocator, context_gl ? &context_gl->c : NULL, memory_type, size)))
1076 wined3d_device_gl_allocator_unlock(device_gl);
1077 *id = 0;
1078 return NULL;
1081 *id = wined3d_allocator_chunk_gl(block->chunk)->gl_buffer;
1083 wined3d_device_gl_allocator_unlock(device_gl);
1084 TRACE("Allocated offset %#Ix from chunk %p.\n", block->offset, block->chunk);
1085 return block;
1088 static bool use_buffer_chunk_suballocation(struct wined3d_device_gl *device_gl,
1089 const struct wined3d_gl_info *gl_info, GLenum binding)
1091 switch (binding)
1093 case GL_ARRAY_BUFFER:
1094 case GL_ATOMIC_COUNTER_BUFFER:
1095 case GL_DRAW_INDIRECT_BUFFER:
1096 case GL_PIXEL_UNPACK_BUFFER:
1097 case GL_UNIFORM_BUFFER:
1098 return true;
1100 case GL_ELEMENT_ARRAY_BUFFER:
1101 /* There is no way to specify an element array buffer offset for
1102 * indirect draws in OpenGL. */
1103 return device_gl->d.wined3d->flags & WINED3D_NO_DRAW_INDIRECT
1104 || !gl_info->supported[ARB_DRAW_INDIRECT];
1106 case GL_TEXTURE_BUFFER:
1107 return gl_info->supported[ARB_TEXTURE_BUFFER_RANGE];
1109 default:
1110 return false;
1114 bool wined3d_device_gl_create_bo(struct wined3d_device_gl *device_gl, struct wined3d_context_gl *context_gl,
1115 GLsizeiptr size, GLenum binding, GLenum usage, bool coherent, GLbitfield flags, struct wined3d_bo_gl *bo)
1117 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl(device_gl->d.adapter)->gl_info;
1118 unsigned int memory_type_idx = wined3d_device_gl_find_memory_type(flags);
1119 struct wined3d_allocator_block *memory = NULL;
1120 GLsizeiptr buffer_offset = 0;
1121 GLuint id = 0;
1123 TRACE("device_gl %p, context_gl %p, size %Iu, binding %#x, usage %#x, coherent %#x, flags %#x, bo %p.\n",
1124 device_gl, context_gl, size, binding, usage, coherent, flags, bo);
1126 if (gl_info->supported[ARB_BUFFER_STORAGE])
1128 if (use_buffer_chunk_suballocation(device_gl, gl_info, binding))
1130 if ((memory = wined3d_device_gl_allocate_memory(device_gl, context_gl, memory_type_idx, size, &id)))
1131 buffer_offset = memory->offset;
1133 else if (context_gl)
1135 WARN_(d3d_perf)("Not allocating chunk memory for binding type %#x.\n", binding);
1136 id = wined3d_context_gl_allocate_vram_chunk_buffer(context_gl, memory_type_idx, size);
1139 if (!id)
1141 WARN("Failed to allocate buffer.\n");
1142 return false;
1145 else
1147 if (!context_gl)
1148 return false;
1150 GL_EXTCALL(glGenBuffers(1, &id));
1151 if (!id)
1153 checkGLcall("buffer object creation");
1154 return false;
1156 TRACE("Created buffer object %u.\n", id);
1157 wined3d_context_gl_bind_bo(context_gl, binding, id);
1159 if (!coherent && gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
1161 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
1162 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
1165 GL_EXTCALL(glBufferData(binding, size, NULL, usage));
1167 wined3d_context_gl_bind_bo(context_gl, binding, 0);
1168 checkGLcall("buffer object creation");
1171 bo->id = id;
1172 bo->memory = memory;
1173 bo->size = size;
1174 bo->binding = binding;
1175 bo->usage = usage;
1176 bo->flags = flags;
1177 bo->b.coherent = coherent;
1178 list_init(&bo->b.users);
1179 bo->command_fence_id = 0;
1180 bo->b.buffer_offset = buffer_offset;
1181 bo->b.memory_offset = bo->b.buffer_offset;
1182 bo->b.map_ptr = NULL;
1183 bo->b.client_map_count = 0;
1185 return true;
1188 void wined3d_device_gl_delete_opengl_contexts_cs(void *object)
1190 struct wined3d_device_gl *device_gl = object;
1191 struct wined3d_context_gl *context_gl;
1192 struct wined3d_context *context;
1193 struct wined3d_device *device;
1194 struct wined3d_shader *shader;
1196 TRACE("device %p.\n", device_gl);
1198 device = &device_gl->d;
1200 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
1202 device->shader_backend->shader_destroy(shader);
1205 context = context_acquire(device, NULL, 0);
1206 context_gl = wined3d_context_gl(context);
1207 device->blitter->ops->blitter_destroy(device->blitter, context);
1208 device->shader_backend->shader_free_private(device, context);
1209 wined3d_device_gl_destroy_dummy_textures(device_gl, context_gl);
1211 if (context_gl->c.d3d_info->fences)
1213 wined3d_context_gl_submit_command_fence(context_gl);
1214 wined3d_context_gl_wait_command_fence(context_gl,
1215 wined3d_device_gl(context_gl->c.device)->current_fence_id - 1);
1217 wined3d_allocator_cleanup(&device_gl->allocator);
1219 context_release(context);
1221 while (device->context_count)
1222 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1224 if (device_gl->backup_dc)
1226 TRACE("Destroying backup wined3d window %p, dc %p.\n", device_gl->backup_wnd, device_gl->backup_dc);
1228 wined3d_release_dc(device_gl->backup_wnd, device_gl->backup_dc);
1229 DestroyWindow(device_gl->backup_wnd);
1233 void wined3d_device_gl_create_primary_opengl_context_cs(void *object)
1235 struct wined3d_device_gl *device_gl = object;
1236 struct wined3d_context_gl *context_gl;
1237 struct wined3d_swapchain *swapchain;
1238 struct wined3d_context *context;
1239 struct wined3d_texture *target;
1240 struct wined3d_device *device;
1241 HRESULT hr;
1243 TRACE("device %p.\n", device_gl);
1245 device = &device_gl->d;
1246 swapchain = device->swapchains[0];
1247 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1248 if (!(context = context_acquire(device, target, 0)))
1250 WARN("Failed to acquire context.\n");
1251 return;
1254 context_gl = wined3d_context_gl(context);
1256 if (!wined3d_allocator_init(&device_gl->allocator, ARRAY_SIZE(gl_memory_types), &wined3d_allocator_gl_ops))
1258 WARN("Failed to initialise allocator.\n");
1259 context_release(context);
1260 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1261 return;
1264 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1265 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1267 ERR("Failed to allocate shader private data, hr %#lx.\n", hr);
1268 wined3d_allocator_cleanup(&device_gl->allocator);
1269 context_release(context);
1270 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1271 return;
1274 if (!(device->blitter = wined3d_cpu_blitter_create()))
1276 ERR("Failed to create CPU blitter.\n");
1277 device->shader_backend->shader_free_private(device, NULL);
1278 wined3d_allocator_cleanup(&device_gl->allocator);
1279 context_release(context);
1280 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1281 return;
1284 wined3d_ffp_blitter_create(&device->blitter, context_gl->gl_info);
1285 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1286 wined3d_arbfp_blitter_create(&device->blitter, device);
1287 wined3d_fbo_blitter_create(&device->blitter, context_gl->gl_info);
1288 wined3d_raw_blitter_create(&device->blitter, context_gl->gl_info);
1290 wined3d_device_gl_create_dummy_textures(device_gl, context_gl);
1291 wined3d_device_create_default_samplers(device, context);
1292 context_release(context);
1295 HRESULT wined3d_device_set_implicit_swapchain(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1297 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1298 const struct wined3d_swapchain_desc *swapchain_desc;
1299 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
1300 DWORD clear_flags = 0;
1301 unsigned int i;
1302 HRESULT hr;
1304 TRACE("device %p, swapchain %p.\n", device, swapchain);
1306 if (device->d3d_initialized)
1307 return WINED3DERR_INVALIDCALL;
1309 device->swapchain_count = 1;
1310 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1312 ERR("Failed to allocate swapchain array.\n");
1313 hr = E_OUTOFMEMORY;
1314 goto err_out;
1316 device->swapchains[0] = swapchain;
1318 for (i = 0; i < ARRAY_SIZE(fb->render_targets); ++i)
1320 if (fb->render_targets[i])
1321 wined3d_rtv_bind_count_dec(fb->render_targets[i]);
1323 memset(fb->render_targets, 0, sizeof(fb->render_targets));
1325 if (FAILED(hr = device->adapter->adapter_ops->adapter_init_3d(device)))
1326 goto err_out;
1327 device->d3d_initialized = TRUE;
1329 swapchain_desc = &swapchain->state.desc;
1330 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1332 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1333 struct wined3d_view_desc view_desc;
1335 view_desc.format_id = back_buffer->format->id;
1336 view_desc.flags = 0;
1337 view_desc.u.texture.level_idx = 0;
1338 view_desc.u.texture.level_count = 1;
1339 view_desc.u.texture.layer_idx = 0;
1340 view_desc.u.texture.layer_count = 1;
1341 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1342 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1344 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
1345 device->adapter->adapter_ops->adapter_uninit_3d(device);
1346 device->d3d_initialized = FALSE;
1347 goto err_out;
1351 device_init_swapchain_state(device, swapchain);
1353 TRACE("All defaults now set up.\n");
1355 /* Clear the screen. */
1356 if (device->back_buffer_view)
1357 clear_flags |= WINED3DCLEAR_TARGET;
1358 if (swapchain_desc->enable_auto_depth_stencil)
1359 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1360 if (clear_flags)
1361 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1363 if (wined3d_settings.logo)
1364 device_load_logo(device, wined3d_settings.logo);
1366 return WINED3D_OK;
1368 err_out:
1369 heap_free(device->swapchains);
1370 device->swapchains = NULL;
1371 device->swapchain_count = 0;
1373 return hr;
1376 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1378 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1380 wined3d_sampler_decref(sampler);
1383 static void device_free_rasterizer_state(struct wine_rb_entry *entry, void *context)
1385 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
1387 wined3d_rasterizer_state_decref(state);
1390 static void device_free_blend_state(struct wine_rb_entry *entry, void *context)
1392 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
1394 wined3d_blend_state_decref(blend_state);
1397 static void device_free_depth_stencil_state(struct wine_rb_entry *entry, void *context)
1399 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
1401 wined3d_depth_stencil_state_decref(state);
1404 void wined3d_device_uninit_3d(struct wined3d_device *device)
1406 struct wined3d_state *state = device->cs->c.state;
1407 struct wined3d_resource *resource, *cursor;
1408 struct wined3d_rendertarget_view *view;
1409 struct wined3d_texture *texture;
1410 struct wined3d_buffer *buffer;
1411 unsigned int i;
1413 TRACE("device %p.\n", device);
1415 if (!device->d3d_initialized)
1417 ERR("Called while 3D support was not initialised.\n");
1418 return;
1421 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1423 device->swapchain_count = 0;
1425 if ((texture = device->logo_texture))
1427 device->logo_texture = NULL;
1428 wined3d_texture_decref(texture);
1431 if ((texture = device->cursor_texture))
1433 device->cursor_texture = NULL;
1434 wined3d_texture_decref(texture);
1437 for (i = 0; i < ARRAY_SIZE(device->push_constants); ++i)
1439 if ((buffer = device->push_constants[i]))
1440 wined3d_buffer_decref(buffer);
1442 memset(device->push_constants, 0, sizeof(device->push_constants));
1444 wined3d_device_context_emit_reset_state(&device->cs->c, true);
1445 state_cleanup(state);
1447 wine_rb_destroy(&device->samplers, device_free_sampler, NULL);
1448 wine_rb_destroy(&device->rasterizer_states, device_free_rasterizer_state, NULL);
1449 wine_rb_destroy(&device->blend_states, device_free_blend_state, NULL);
1450 wine_rb_destroy(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
1452 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1454 TRACE("Unloading resource %p.\n", resource);
1455 wined3d_cs_emit_unload_resource(device->cs, resource);
1458 device->adapter->adapter_ops->adapter_uninit_3d(device);
1459 device->d3d_initialized = FALSE;
1461 if ((view = device->auto_depth_stencil_view))
1463 device->auto_depth_stencil_view = NULL;
1464 if (wined3d_rendertarget_view_decref(view))
1465 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1468 if ((view = device->back_buffer_view))
1470 device->back_buffer_view = NULL;
1471 wined3d_rendertarget_view_decref(view);
1474 heap_free(device->swapchains);
1475 device->swapchains = NULL;
1477 wined3d_state_reset(state, &device->adapter->d3d_info);
1480 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1481 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1482 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1484 * There is no way to deactivate thread safety once it is enabled.
1486 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1488 TRACE("device %p.\n", device);
1490 /* For now just store the flag (needed in case of ddraw). */
1491 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1494 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1496 const struct wined3d_driver_info *driver_info;
1498 TRACE("device %p.\n", device);
1500 driver_info = &device->adapter->driver_info;
1502 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1503 wine_dbgstr_longlong(driver_info->vram_bytes),
1504 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1505 wine_dbgstr_longlong(driver_info->vram_bytes - device->adapter->vram_bytes_used));
1507 return min(UINT_MAX, driver_info->vram_bytes) - device->adapter->vram_bytes_used;
1510 struct wined3d_buffer * CDECL wined3d_device_context_get_stream_output(struct wined3d_device_context *context,
1511 unsigned int idx, unsigned int *offset)
1513 TRACE("context %p, idx %u, offset %p.\n", context, idx, offset);
1515 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1517 WARN("Invalid stream output %u.\n", idx);
1518 return NULL;
1521 if (offset)
1522 *offset = context->state->stream_output[idx].offset;
1523 return context->state->stream_output[idx].buffer;
1526 HRESULT CDECL wined3d_device_context_get_stream_source(const struct wined3d_device_context *context,
1527 unsigned int stream_idx, struct wined3d_buffer **buffer, unsigned int *offset, unsigned int *stride)
1529 const struct wined3d_stream_state *stream;
1531 TRACE("context %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1532 context, stream_idx, buffer, offset, stride);
1534 if (stream_idx >= WINED3D_MAX_STREAMS)
1536 WARN("Stream index %u out of range.\n", stream_idx);
1537 return WINED3DERR_INVALIDCALL;
1540 stream = &context->state->streams[stream_idx];
1541 *buffer = stream->buffer;
1542 if (offset)
1543 *offset = stream->offset;
1544 *stride = stream->stride;
1546 return WINED3D_OK;
1549 static void wined3d_device_get_transform(const struct wined3d_device *device,
1550 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1552 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1554 *matrix = device->cs->c.state->transforms[state];
1557 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1558 const struct wined3d_clip_status *clip_status)
1560 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1562 if (!clip_status)
1563 return WINED3DERR_INVALIDCALL;
1565 return WINED3D_OK;
1568 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1569 struct wined3d_clip_status *clip_status)
1571 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1573 if (!clip_status)
1574 return WINED3DERR_INVALIDCALL;
1576 return WINED3D_OK;
1579 struct wined3d_buffer * CDECL wined3d_device_context_get_index_buffer(const struct wined3d_device_context *context,
1580 enum wined3d_format_id *format, unsigned int *offset)
1582 const struct wined3d_state *state = context->state;
1584 TRACE("context %p, format %p, offset %p.\n", context, format, offset);
1586 *format = state->index_format;
1587 if (offset)
1588 *offset = state->index_offset;
1589 return state->index_buffer;
1592 void CDECL wined3d_device_context_get_viewports(const struct wined3d_device_context *context,
1593 unsigned int *viewport_count, struct wined3d_viewport *viewports)
1595 const struct wined3d_state *state = context->state;
1596 unsigned int count;
1598 TRACE("context %p, viewport_count %p, viewports %p.\n", context, viewport_count, viewports);
1600 count = viewport_count ? min(*viewport_count, state->viewport_count) : 1;
1601 if (count && viewports)
1602 memcpy(viewports, state->viewports, count * sizeof(*viewports));
1603 if (viewport_count)
1604 *viewport_count = state->viewport_count;
1607 struct wined3d_blend_state * CDECL wined3d_device_context_get_blend_state(const struct wined3d_device_context *context,
1608 struct wined3d_color *blend_factor, unsigned int *sample_mask)
1610 const struct wined3d_state *state = context->state;
1612 TRACE("context %p, blend_factor %p, sample_mask %p.\n", context, blend_factor, sample_mask);
1614 *blend_factor = state->blend_factor;
1615 *sample_mask = state->sample_mask;
1616 return state->blend_state;
1619 struct wined3d_depth_stencil_state * CDECL wined3d_device_context_get_depth_stencil_state(
1620 const struct wined3d_device_context *context, unsigned int *stencil_ref)
1622 const struct wined3d_state *state = context->state;
1624 TRACE("context %p, stencil_ref %p.\n", context, stencil_ref);
1626 *stencil_ref = state->stencil_ref;
1627 return state->depth_stencil_state;
1630 struct wined3d_rasterizer_state * CDECL wined3d_device_context_get_rasterizer_state(
1631 struct wined3d_device_context *context)
1633 TRACE("context %p.\n", context);
1635 return context->state->rasterizer_state;
1638 void CDECL wined3d_device_context_get_scissor_rects(const struct wined3d_device_context *context,
1639 unsigned int *rect_count, RECT *rects)
1641 const struct wined3d_state *state = context->state;
1642 unsigned int count;
1644 TRACE("context %p, rect_count %p, rects %p.\n", context, rect_count, rects);
1646 if (rects && (count = rect_count ? min(*rect_count, state->scissor_rect_count) : 1))
1647 memcpy(rects, state->scissor_rects, count * sizeof(*rects));
1648 if (rect_count)
1649 *rect_count = state->scissor_rect_count;
1652 void CDECL wined3d_device_context_reset_state(struct wined3d_device_context *context)
1654 TRACE("context %p.\n", context);
1656 wined3d_device_context_lock(context);
1657 state_cleanup(context->state);
1658 wined3d_state_reset(context->state, &context->device->adapter->d3d_info);
1659 wined3d_device_context_emit_reset_state(context, true);
1660 wined3d_device_context_unlock(context);
1663 void CDECL wined3d_device_context_set_state(struct wined3d_device_context *context, struct wined3d_state *state)
1665 unsigned int i;
1667 TRACE("context %p, state %p.\n", context, state);
1669 wined3d_device_context_lock(context);
1670 context->state = state;
1671 wined3d_device_context_emit_set_feature_level(context, state->feature_level);
1673 wined3d_device_context_emit_set_rendertarget_views(context, 0,
1674 ARRAY_SIZE(state->fb.render_targets), state->fb.render_targets);
1676 wined3d_device_context_emit_set_depth_stencil_view(context, state->fb.depth_stencil);
1677 wined3d_device_context_emit_set_vertex_declaration(context, state->vertex_declaration);
1679 wined3d_device_context_emit_set_stream_outputs(context, state->stream_output);
1681 wined3d_device_context_emit_set_stream_sources(context, 0, WINED3D_MAX_STREAMS, state->streams);
1683 wined3d_device_context_emit_set_index_buffer(context, state->index_buffer,
1684 state->index_format, state->index_offset);
1686 wined3d_device_context_emit_set_predication(context, state->predicate, state->predicate_value);
1688 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1690 wined3d_device_context_emit_set_shader(context, i, state->shader[i]);
1691 wined3d_device_context_emit_set_constant_buffers(context, i, 0, MAX_CONSTANT_BUFFERS, state->cb[i]);
1692 wined3d_device_context_emit_set_samplers(context, i, 0, MAX_SAMPLER_OBJECTS, state->sampler[i]);
1693 wined3d_device_context_emit_set_shader_resource_views(context, i, 0,
1694 MAX_SHADER_RESOURCE_VIEWS, state->shader_resource_view[i]);
1697 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
1698 wined3d_device_context_emit_set_unordered_access_views(context, i, 0, MAX_UNORDERED_ACCESS_VIEWS,
1699 state->unordered_access_view[i], NULL);
1701 wined3d_device_context_emit_set_viewports(context, state->viewport_count, state->viewports);
1702 wined3d_device_context_emit_set_scissor_rects(context, state->scissor_rect_count, state->scissor_rects);
1704 wined3d_device_context_emit_set_blend_state(context, state->blend_state, &state->blend_factor, state->sample_mask);
1705 wined3d_device_context_emit_set_depth_stencil_state(context, state->depth_stencil_state, state->stencil_ref);
1706 wined3d_device_context_emit_set_rasterizer_state(context, state->rasterizer_state);
1707 wined3d_device_context_unlock(context);
1710 struct wined3d_state * CDECL wined3d_device_get_state(struct wined3d_device *device)
1712 TRACE("device %p.\n", device);
1714 return device->cs->c.state;
1717 struct wined3d_device_context * CDECL wined3d_device_get_immediate_context(struct wined3d_device *device)
1719 TRACE("device %p.\n", device);
1721 return &device->cs->c;
1724 struct wined3d_vertex_declaration * CDECL wined3d_device_context_get_vertex_declaration(
1725 const struct wined3d_device_context *context)
1727 TRACE("context %p.\n", context);
1729 return context->state->vertex_declaration;
1732 void CDECL wined3d_device_context_set_shader(struct wined3d_device_context *context,
1733 enum wined3d_shader_type type, struct wined3d_shader *shader)
1735 struct wined3d_state *state = context->state;
1736 struct wined3d_shader *prev;
1738 TRACE("context %p, type %#x, shader %p.\n", context, type, shader);
1740 wined3d_device_context_lock(context);
1741 prev = state->shader[type];
1742 if (shader == prev)
1743 goto out;
1745 if (shader)
1746 wined3d_shader_incref(shader);
1747 state->shader[type] = shader;
1748 wined3d_device_context_emit_set_shader(context, type, shader);
1749 if (prev)
1750 wined3d_shader_decref(prev);
1751 out:
1752 wined3d_device_context_unlock(context);
1755 struct wined3d_shader * CDECL wined3d_device_context_get_shader(const struct wined3d_device_context *context,
1756 enum wined3d_shader_type type)
1758 TRACE("context %p, type %#x.\n", context, type);
1760 return context->state->shader[type];
1763 void CDECL wined3d_device_context_set_constant_buffers(struct wined3d_device_context *context,
1764 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1765 const struct wined3d_constant_buffer_state *buffers)
1767 struct wined3d_state *state = context->state;
1768 unsigned int i;
1770 TRACE("context %p, type %#x, start_idx %u, count %u, buffers %p.\n", context, type, start_idx, count, buffers);
1772 if (!wined3d_bound_range(start_idx, count, MAX_CONSTANT_BUFFERS))
1774 WARN("Invalid constant buffer index %u, count %u.\n", start_idx, count);
1775 return;
1778 wined3d_device_context_lock(context);
1779 if (!memcmp(buffers, &state->cb[type][start_idx], count * sizeof(*buffers)))
1780 goto out;
1782 wined3d_device_context_emit_set_constant_buffers(context, type, start_idx, count, buffers);
1783 for (i = 0; i < count; ++i)
1785 struct wined3d_buffer *prev = state->cb[type][start_idx + i].buffer;
1786 struct wined3d_buffer *buffer = buffers[i].buffer;
1788 if (buffer)
1789 wined3d_buffer_incref(buffer);
1790 state->cb[type][start_idx + i] = buffers[i];
1791 if (prev)
1792 wined3d_buffer_decref(prev);
1794 out:
1795 wined3d_device_context_unlock(context);
1798 void CDECL wined3d_device_context_set_blend_state(struct wined3d_device_context *context,
1799 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1801 struct wined3d_state *state = context->state;
1802 struct wined3d_blend_state *prev;
1804 TRACE("context %p, blend_state %p, blend_factor %p, sample_mask %#x.\n",
1805 context, blend_state, blend_factor, sample_mask);
1807 wined3d_device_context_lock(context);
1808 prev = state->blend_state;
1809 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor))
1810 && sample_mask == state->sample_mask)
1811 goto out;
1813 if (blend_state)
1814 wined3d_blend_state_incref(blend_state);
1815 state->blend_state = blend_state;
1816 state->blend_factor = *blend_factor;
1817 state->sample_mask = sample_mask;
1818 wined3d_device_context_emit_set_blend_state(context, blend_state, blend_factor, sample_mask);
1819 if (prev)
1820 wined3d_blend_state_decref(prev);
1821 out:
1822 wined3d_device_context_unlock(context);
1825 void CDECL wined3d_device_context_set_depth_stencil_state(struct wined3d_device_context *context,
1826 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
1828 struct wined3d_state *state = context->state;
1829 struct wined3d_depth_stencil_state *prev;
1831 TRACE("context %p, depth_stencil_state %p, stencil_ref %u.\n", context, depth_stencil_state, stencil_ref);
1833 wined3d_device_context_lock(context);
1834 prev = state->depth_stencil_state;
1835 if (prev == depth_stencil_state && state->stencil_ref == stencil_ref)
1836 goto out;
1838 if (depth_stencil_state)
1839 wined3d_depth_stencil_state_incref(depth_stencil_state);
1840 state->depth_stencil_state = depth_stencil_state;
1841 state->stencil_ref = stencil_ref;
1842 wined3d_device_context_emit_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
1843 if (prev)
1844 wined3d_depth_stencil_state_decref(prev);
1845 out:
1846 wined3d_device_context_unlock(context);
1849 void CDECL wined3d_device_context_set_rasterizer_state(struct wined3d_device_context *context,
1850 struct wined3d_rasterizer_state *rasterizer_state)
1852 struct wined3d_state *state = context->state;
1853 struct wined3d_rasterizer_state *prev;
1855 TRACE("context %p, rasterizer_state %p.\n", context, rasterizer_state);
1857 wined3d_device_context_lock(context);
1858 prev = state->rasterizer_state;
1859 if (prev == rasterizer_state)
1860 goto out;
1862 if (rasterizer_state)
1863 wined3d_rasterizer_state_incref(rasterizer_state);
1864 state->rasterizer_state = rasterizer_state;
1865 wined3d_device_context_emit_set_rasterizer_state(context, rasterizer_state);
1866 if (prev)
1867 wined3d_rasterizer_state_decref(prev);
1868 out:
1869 wined3d_device_context_unlock(context);
1872 void CDECL wined3d_device_context_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
1873 const struct wined3d_viewport *viewports)
1875 struct wined3d_state *state = context->state;
1876 unsigned int i;
1878 TRACE("context %p, viewport_count %u, viewports %p.\n", context, viewport_count, viewports);
1880 for (i = 0; i < viewport_count; ++i)
1882 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
1883 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
1886 wined3d_device_context_lock(context);
1887 if (viewport_count)
1888 memcpy(state->viewports, viewports, viewport_count * sizeof(*viewports));
1889 else
1890 memset(state->viewports, 0, sizeof(state->viewports));
1891 state->viewport_count = viewport_count;
1893 wined3d_device_context_emit_set_viewports(context, viewport_count, viewports);
1894 wined3d_device_context_unlock(context);
1897 void CDECL wined3d_device_context_set_scissor_rects(struct wined3d_device_context *context, unsigned int rect_count,
1898 const RECT *rects)
1900 struct wined3d_state *state = context->state;
1901 unsigned int i;
1903 TRACE("context %p, rect_count %u, rects %p.\n", context, rect_count, rects);
1905 for (i = 0; i < rect_count; ++i)
1907 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
1910 wined3d_device_context_lock(context);
1911 if (state->scissor_rect_count == rect_count
1912 && !memcmp(state->scissor_rects, rects, rect_count * sizeof(*rects)))
1914 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
1915 goto out;
1918 if (rect_count)
1919 memcpy(state->scissor_rects, rects, rect_count * sizeof(*rects));
1920 else
1921 memset(state->scissor_rects, 0, sizeof(state->scissor_rects));
1922 state->scissor_rect_count = rect_count;
1924 wined3d_device_context_emit_set_scissor_rects(context, rect_count, rects);
1925 out:
1926 wined3d_device_context_unlock(context);
1929 void CDECL wined3d_device_context_set_shader_resource_views(struct wined3d_device_context *context,
1930 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1931 struct wined3d_shader_resource_view *const *const views)
1933 struct wined3d_shader_resource_view *real_views[MAX_SHADER_RESOURCE_VIEWS];
1934 struct wined3d_state *state = context->state;
1935 const struct wined3d_rendertarget_view *dsv = state->fb.depth_stencil;
1936 unsigned int i;
1938 TRACE("context %p, type %#x, start_idx %u, count %u, views %p.\n", context, type, start_idx, count, views);
1940 if (!wined3d_bound_range(start_idx, count, MAX_SHADER_RESOURCE_VIEWS))
1942 WARN("Invalid view index %u, count %u.\n", start_idx, count);
1943 return;
1946 wined3d_device_context_lock(context);
1947 if (!memcmp(views, &state->shader_resource_view[type][start_idx], count * sizeof(*views)))
1948 goto out;
1950 memcpy(real_views, views, count * sizeof(*views));
1952 for (i = 0; i < count; ++i)
1954 struct wined3d_shader_resource_view *view = real_views[i];
1956 if (view && (wined3d_is_srv_rtv_bound(state, view)
1957 || (dsv && dsv->resource == view->resource && wined3d_dsv_srv_conflict(dsv, view->format))))
1959 WARN("Application is trying to bind resource which is attached as render target.\n");
1960 real_views[i] = NULL;
1964 wined3d_device_context_emit_set_shader_resource_views(context, type, start_idx, count, real_views);
1965 for (i = 0; i < count; ++i)
1967 struct wined3d_shader_resource_view *prev = state->shader_resource_view[type][start_idx + i];
1968 struct wined3d_shader_resource_view *view = real_views[i];
1970 if (view)
1972 wined3d_shader_resource_view_incref(view);
1973 wined3d_srv_bind_count_inc(view);
1976 state->shader_resource_view[type][start_idx + i] = view;
1977 if (prev)
1979 wined3d_srv_bind_count_dec(prev);
1980 wined3d_shader_resource_view_decref(prev);
1983 out:
1984 wined3d_device_context_unlock(context);
1987 void CDECL wined3d_device_context_set_samplers(struct wined3d_device_context *context, enum wined3d_shader_type type,
1988 unsigned int start_idx, unsigned int count, struct wined3d_sampler *const *samplers)
1990 struct wined3d_state *state = context->state;
1991 unsigned int i;
1993 TRACE("context %p, type %#x, start_idx %u, count %u, samplers %p.\n", context, type, start_idx, count, samplers);
1995 if (!wined3d_bound_range(start_idx, count, MAX_SAMPLER_OBJECTS))
1997 WARN("Invalid sampler index %u, count %u.\n", start_idx, count);
1998 return;
2001 wined3d_device_context_lock(context);
2002 if (!memcmp(samplers, &state->sampler[type][start_idx], count * sizeof(*samplers)))
2003 goto out;
2005 wined3d_device_context_emit_set_samplers(context, type, start_idx, count, samplers);
2006 for (i = 0; i < count; ++i)
2008 struct wined3d_sampler *prev = state->sampler[type][start_idx + i];
2009 struct wined3d_sampler *sampler = samplers[i];
2011 if (sampler)
2012 wined3d_sampler_incref(sampler);
2013 state->sampler[type][start_idx + i] = sampler;
2014 if (prev)
2015 wined3d_sampler_decref(prev);
2017 out:
2018 wined3d_device_context_unlock(context);
2021 void CDECL wined3d_device_context_set_unordered_access_views(struct wined3d_device_context *context,
2022 enum wined3d_pipeline pipeline, unsigned int start_idx, unsigned int count,
2023 struct wined3d_unordered_access_view *const *uavs, const unsigned int *initial_counts)
2025 struct wined3d_state *state = context->state;
2026 unsigned int i;
2028 TRACE("context %p, pipeline %#x, start_idx %u, count %u, uavs %p, initial_counts %p.\n",
2029 context, pipeline, start_idx, count, uavs, initial_counts);
2031 if (!wined3d_bound_range(start_idx, count, MAX_UNORDERED_ACCESS_VIEWS))
2033 WARN("Invalid UAV index %u, count %u.\n", start_idx, count);
2034 return;
2037 wined3d_device_context_lock(context);
2038 if (!memcmp(uavs, &state->unordered_access_view[pipeline][start_idx], count * sizeof(*uavs)) && !initial_counts)
2039 goto out;
2041 wined3d_device_context_emit_set_unordered_access_views(context, pipeline, start_idx, count, uavs, initial_counts);
2042 for (i = 0; i < count; ++i)
2044 struct wined3d_unordered_access_view *prev = state->unordered_access_view[pipeline][start_idx + i];
2045 struct wined3d_unordered_access_view *uav = uavs[i];
2047 if (uav)
2048 wined3d_unordered_access_view_incref(uav);
2049 state->unordered_access_view[pipeline][start_idx + i] = uav;
2050 if (prev)
2051 wined3d_unordered_access_view_decref(prev);
2053 out:
2054 wined3d_device_context_unlock(context);
2057 void CDECL wined3d_device_context_set_render_targets_and_unordered_access_views(struct wined3d_device_context *context,
2058 unsigned int rtv_count, struct wined3d_rendertarget_view *const *render_target_views,
2059 struct wined3d_rendertarget_view *depth_stencil_view, UINT uav_count,
2060 struct wined3d_unordered_access_view *const *unordered_access_views, const unsigned int *initial_counts)
2062 wined3d_device_context_lock(context);
2063 if (rtv_count != ~0u)
2065 if (depth_stencil_view && !(depth_stencil_view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2067 WARN("View resource %p has incompatible %s bind flags.\n",
2068 depth_stencil_view->resource, wined3d_debug_bind_flags(depth_stencil_view->resource->bind_flags));
2069 goto out;
2072 if (FAILED(wined3d_device_context_set_rendertarget_views(context, 0, rtv_count,
2073 render_target_views, FALSE)))
2074 goto out;
2076 wined3d_device_context_set_depth_stencil_view(context, depth_stencil_view);
2079 if (uav_count != ~0u)
2081 wined3d_device_context_set_unordered_access_views(context, WINED3D_PIPELINE_GRAPHICS, 0, uav_count,
2082 unordered_access_views, initial_counts);
2084 out:
2085 wined3d_device_context_unlock(context);
2088 static void wined3d_device_context_unbind_srv_for_rtv(struct wined3d_device_context *context,
2089 const struct wined3d_rendertarget_view *view, BOOL dsv)
2091 const struct wined3d_state *state = context->state;
2092 const struct wined3d_resource *resource;
2094 if (!view)
2095 return;
2096 resource = view->resource;
2098 if (resource->srv_bind_count_device)
2100 const struct wined3d_shader_resource_view *srv;
2101 unsigned int i, j;
2103 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2105 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
2107 if ((srv = state->shader_resource_view[i][j]) && srv->resource == resource
2108 && ((!dsv && wined3d_is_srv_rtv_bound(state, srv))
2109 || (dsv && wined3d_dsv_srv_conflict(view, srv->format))))
2111 static struct wined3d_shader_resource_view *const null_srv;
2113 WARN("Application sets bound resource as render target.\n");
2114 wined3d_device_context_set_shader_resource_views(context, i, j, 1, &null_srv);
2121 HRESULT CDECL wined3d_device_context_set_rendertarget_views(struct wined3d_device_context *context,
2122 unsigned int start_idx, unsigned int count, struct wined3d_rendertarget_view *const *views, BOOL set_viewport)
2124 struct wined3d_state *state = context->state;
2125 unsigned int i, max_rt_count;
2127 TRACE("context %p, start_idx %u, count %u, views %p, set_viewport %#x.\n",
2128 context, start_idx, count, views, set_viewport);
2130 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
2131 if (start_idx >= max_rt_count)
2133 WARN("Only %u render targets are supported.\n", max_rt_count);
2134 return WINED3DERR_INVALIDCALL;
2136 count = min(count, max_rt_count - start_idx);
2138 for (i = 0; i < count; ++i)
2140 if (views[i] && !(views[i]->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
2142 WARN("View resource %p doesn't have render target bind flags.\n", views[i]->resource);
2143 return WINED3DERR_INVALIDCALL;
2147 wined3d_device_context_lock(context);
2148 /* Set the viewport and scissor rectangles, if requested. Tests show that
2149 * stateblock recording is ignored, the change goes directly into the
2150 * primary stateblock. */
2151 if (!start_idx && set_viewport)
2153 state->viewports[0].x = 0;
2154 state->viewports[0].y = 0;
2155 state->viewports[0].width = views[0]->width;
2156 state->viewports[0].height = views[0]->height;
2157 state->viewports[0].min_z = 0.0f;
2158 state->viewports[0].max_z = 1.0f;
2159 state->viewport_count = 1;
2160 wined3d_device_context_emit_set_viewports(context, 1, state->viewports);
2162 SetRect(&state->scissor_rects[0], 0, 0, views[0]->width, views[0]->height);
2163 state->scissor_rect_count = 1;
2164 wined3d_device_context_emit_set_scissor_rects(context, 1, state->scissor_rects);
2167 if (!memcmp(views, &state->fb.render_targets[start_idx], count * sizeof(*views)))
2168 goto out;
2170 wined3d_device_context_emit_set_rendertarget_views(context, start_idx, count, views);
2171 for (i = 0; i < count; ++i)
2173 struct wined3d_rendertarget_view *prev = state->fb.render_targets[start_idx + i];
2174 struct wined3d_rendertarget_view *view = views[i];
2176 if (view)
2178 wined3d_rendertarget_view_incref(view);
2179 wined3d_rtv_bind_count_inc(view);
2181 state->fb.render_targets[start_idx + i] = view;
2182 /* Release after the assignment, to prevent device_resource_released()
2183 * from seeing the resource as still in use. */
2184 if (prev)
2186 wined3d_rtv_bind_count_dec(prev);
2187 wined3d_rendertarget_view_decref(prev);
2190 wined3d_device_context_unbind_srv_for_rtv(context, view, FALSE);
2192 out:
2193 wined3d_device_context_unlock(context);
2194 return WINED3D_OK;
2197 HRESULT CDECL wined3d_device_context_set_depth_stencil_view(struct wined3d_device_context *context,
2198 struct wined3d_rendertarget_view *view)
2200 struct wined3d_fb_state *fb = &context->state->fb;
2201 struct wined3d_rendertarget_view *prev;
2203 TRACE("context %p, view %p.\n", context, view);
2205 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2207 WARN("View resource %p has incompatible %s bind flags.\n",
2208 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
2209 return WINED3DERR_INVALIDCALL;
2212 wined3d_device_context_lock(context);
2213 prev = fb->depth_stencil;
2214 if (prev == view)
2216 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
2217 goto out;
2220 if ((fb->depth_stencil = view))
2221 wined3d_rendertarget_view_incref(view);
2222 wined3d_device_context_emit_set_depth_stencil_view(context, view);
2223 if (prev)
2224 wined3d_rendertarget_view_decref(prev);
2225 wined3d_device_context_unbind_srv_for_rtv(context, view, TRUE);
2226 out:
2227 wined3d_device_context_unlock(context);
2228 return WINED3D_OK;
2231 void CDECL wined3d_device_context_set_predication(struct wined3d_device_context *context,
2232 struct wined3d_query *predicate, BOOL value)
2234 struct wined3d_state *state = context->state;
2235 struct wined3d_query *prev;
2237 TRACE("context %p, predicate %p, value %#x.\n", context, predicate, value);
2239 wined3d_device_context_lock(context);
2240 prev = state->predicate;
2241 if (predicate)
2243 FIXME("Predicated rendering not implemented.\n");
2244 wined3d_query_incref(predicate);
2246 state->predicate = predicate;
2247 state->predicate_value = value;
2248 wined3d_device_context_emit_set_predication(context, predicate, value);
2249 if (prev)
2250 wined3d_query_decref(prev);
2251 wined3d_device_context_unlock(context);
2254 HRESULT CDECL wined3d_device_context_set_stream_sources(struct wined3d_device_context *context,
2255 unsigned int start_idx, unsigned int count, const struct wined3d_stream_state *streams)
2257 struct wined3d_state *state = context->state;
2258 unsigned int i;
2260 TRACE("context %p, start_idx %u, count %u, streams %p.\n", context, start_idx, count, streams);
2262 if (start_idx >= WINED3D_MAX_STREAMS)
2264 WARN("Start index %u is out of range.\n", start_idx);
2265 return WINED3DERR_INVALIDCALL;
2268 count = min(count, WINED3D_MAX_STREAMS - start_idx);
2270 for (i = 0; i < count; ++i)
2272 if (streams[i].offset & 0x3)
2274 WARN("Offset %u is not 4 byte aligned.\n", streams[i].offset);
2275 return WINED3DERR_INVALIDCALL;
2279 wined3d_device_context_lock(context);
2280 if (!memcmp(streams, &state->streams[start_idx], count * sizeof(*streams)))
2281 goto out;
2283 wined3d_device_context_emit_set_stream_sources(context, start_idx, count, streams);
2284 for (i = 0; i < count; ++i)
2286 struct wined3d_buffer *prev = state->streams[start_idx + i].buffer;
2287 struct wined3d_buffer *buffer = streams[i].buffer;
2289 state->streams[start_idx + i] = streams[i];
2291 if (buffer)
2292 wined3d_buffer_incref(buffer);
2293 if (prev)
2294 wined3d_buffer_decref(prev);
2296 out:
2297 wined3d_device_context_unlock(context);
2298 return WINED3D_OK;
2301 void CDECL wined3d_device_context_set_index_buffer(struct wined3d_device_context *context,
2302 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
2304 struct wined3d_state *state = context->state;
2305 enum wined3d_format_id prev_format;
2306 struct wined3d_buffer *prev_buffer;
2307 unsigned int prev_offset;
2309 TRACE("context %p, buffer %p, format %s, offset %u.\n",
2310 context, buffer, debug_d3dformat(format_id), offset);
2312 wined3d_device_context_lock(context);
2313 prev_buffer = state->index_buffer;
2314 prev_format = state->index_format;
2315 prev_offset = state->index_offset;
2317 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
2318 goto out;
2320 if (buffer)
2321 wined3d_buffer_incref(buffer);
2322 state->index_buffer = buffer;
2323 state->index_format = format_id;
2324 state->index_offset = offset;
2325 wined3d_device_context_emit_set_index_buffer(context, buffer, format_id, offset);
2326 if (prev_buffer)
2327 wined3d_buffer_decref(prev_buffer);
2328 out:
2329 wined3d_device_context_unlock(context);
2332 void CDECL wined3d_device_context_set_vertex_declaration(struct wined3d_device_context *context,
2333 struct wined3d_vertex_declaration *declaration)
2335 struct wined3d_state *state = context->state;
2336 struct wined3d_vertex_declaration *prev;
2338 TRACE("context %p, declaration %p.\n", context, declaration);
2340 wined3d_device_context_lock(context);
2341 prev = state->vertex_declaration;
2342 if (declaration == prev)
2343 goto out;
2345 if (declaration)
2346 wined3d_vertex_declaration_incref(declaration);
2347 state->vertex_declaration = declaration;
2348 wined3d_device_context_emit_set_vertex_declaration(context, declaration);
2349 if (prev)
2350 wined3d_vertex_declaration_decref(prev);
2351 out:
2352 wined3d_device_context_unlock(context);
2355 void CDECL wined3d_device_context_set_stream_outputs(struct wined3d_device_context *context,
2356 const struct wined3d_stream_output outputs[WINED3D_MAX_STREAM_OUTPUT_BUFFERS])
2358 struct wined3d_state *state = context->state;
2359 unsigned int i;
2361 TRACE("context %p, outputs %p.\n", context, outputs);
2363 wined3d_device_context_lock(context);
2364 wined3d_device_context_emit_set_stream_outputs(context, outputs);
2365 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
2367 struct wined3d_buffer *prev_buffer = state->stream_output[i].buffer;
2368 struct wined3d_buffer *buffer = outputs[i].buffer;
2370 if (buffer)
2371 wined3d_buffer_incref(buffer);
2372 state->stream_output[i] = outputs[i];
2373 if (prev_buffer)
2374 wined3d_buffer_decref(prev_buffer);
2376 wined3d_device_context_unlock(context);
2379 void CDECL wined3d_device_context_draw(struct wined3d_device_context *context, unsigned int start_vertex,
2380 unsigned int vertex_count, unsigned int start_instance, unsigned int instance_count)
2382 struct wined3d_state *state = context->state;
2384 TRACE("context %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
2385 context, start_vertex, vertex_count, start_instance, instance_count);
2387 wined3d_device_context_lock(context);
2388 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2389 0, start_vertex, vertex_count, start_instance, instance_count, false);
2390 wined3d_device_context_unlock(context);
2393 void CDECL wined3d_device_context_draw_indexed(struct wined3d_device_context *context, int base_vertex_index,
2394 unsigned int start_index, unsigned int index_count, unsigned int start_instance, unsigned int instance_count)
2396 struct wined3d_state *state = context->state;
2398 TRACE("context %p, base_vertex_index %d, start_index %u, index_count %u, start_instance %u, instance_count %u.\n",
2399 context, base_vertex_index, start_index, index_count, start_instance, instance_count);
2401 wined3d_device_context_lock(context);
2402 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2403 base_vertex_index, start_index, index_count, start_instance, instance_count, true);
2404 wined3d_device_context_unlock(context);
2407 void CDECL wined3d_device_context_get_constant_buffer(const struct wined3d_device_context *context,
2408 enum wined3d_shader_type shader_type, unsigned int idx, struct wined3d_constant_buffer_state *state)
2410 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2412 if (idx >= MAX_CONSTANT_BUFFERS)
2414 WARN("Invalid constant buffer index %u.\n", idx);
2415 return;
2418 *state = context->state->cb[shader_type][idx];
2421 struct wined3d_shader_resource_view * CDECL wined3d_device_context_get_shader_resource_view(
2422 const struct wined3d_device_context *context, enum wined3d_shader_type shader_type, unsigned int idx)
2424 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2426 WARN("Invalid view index %u.\n", idx);
2427 return NULL;
2430 return context->state->shader_resource_view[shader_type][idx];
2433 struct wined3d_sampler * CDECL wined3d_device_context_get_sampler(const struct wined3d_device_context *context,
2434 enum wined3d_shader_type shader_type, unsigned int idx)
2436 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2438 if (idx >= MAX_SAMPLER_OBJECTS)
2440 WARN("Invalid sampler index %u.\n", idx);
2441 return NULL;
2444 return context->state->sampler[shader_type][idx];
2447 struct wined3d_unordered_access_view * CDECL wined3d_device_context_get_unordered_access_view(
2448 const struct wined3d_device_context *context, enum wined3d_pipeline pipeline, unsigned int idx)
2450 TRACE("context %p, pipeline %#x, idx %u.\n", context, pipeline, idx);
2452 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2454 WARN("Invalid UAV index %u.\n", idx);
2455 return NULL;
2458 return context->state->unordered_access_view[pipeline][idx];
2461 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
2463 unsigned int i;
2465 if (!latency)
2466 latency = 3;
2468 device->max_frame_latency = latency;
2469 for (i = 0; i < device->swapchain_count; ++i)
2470 swapchain_set_max_frame_latency(device->swapchains[i], device);
2473 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
2475 return device->max_frame_latency;
2478 static unsigned int wined3d_get_flexible_vertex_size(uint32_t fvf)
2480 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2481 unsigned int i, size = 0;
2483 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2484 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2485 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2486 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2487 switch (fvf & WINED3DFVF_POSITION_MASK)
2489 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2490 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2491 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
2492 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
2493 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
2494 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
2495 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
2496 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
2497 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
2499 for (i = 0; i < texcoord_count; ++i)
2501 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
2504 return size;
2507 static void wined3d_format_get_colour(const struct wined3d_format *format,
2508 const void *data, struct wined3d_color *colour)
2510 float *output = &colour->r;
2511 const uint32_t *u32_data;
2512 const uint16_t *u16_data;
2513 const float *f32_data;
2514 unsigned int i;
2516 static const struct wined3d_color default_colour = {0.0f, 0.0f, 0.0f, 1.0f};
2517 static unsigned int warned;
2519 switch (format->id)
2521 case WINED3DFMT_B8G8R8A8_UNORM:
2522 u32_data = data;
2523 wined3d_color_from_d3dcolor(colour, *u32_data);
2524 break;
2526 case WINED3DFMT_R8G8B8A8_UNORM:
2527 u32_data = data;
2528 colour->r = (*u32_data & 0xffu) / 255.0f;
2529 colour->g = ((*u32_data >> 8) & 0xffu) / 255.0f;
2530 colour->b = ((*u32_data >> 16) & 0xffu) / 255.0f;
2531 colour->a = ((*u32_data >> 24) & 0xffu) / 255.0f;
2532 break;
2534 case WINED3DFMT_R16G16_UNORM:
2535 case WINED3DFMT_R16G16B16A16_UNORM:
2536 u16_data = data;
2537 *colour = default_colour;
2538 for (i = 0; i < format->component_count; ++i)
2539 output[i] = u16_data[i] / 65535.0f;
2540 break;
2542 case WINED3DFMT_R32_FLOAT:
2543 case WINED3DFMT_R32G32_FLOAT:
2544 case WINED3DFMT_R32G32B32_FLOAT:
2545 case WINED3DFMT_R32G32B32A32_FLOAT:
2546 f32_data = data;
2547 *colour = default_colour;
2548 for (i = 0; i < format->component_count; ++i)
2549 output[i] = f32_data[i];
2550 break;
2552 default:
2553 *colour = default_colour;
2554 if (!warned++)
2555 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format->id));
2556 break;
2560 static void wined3d_colour_from_mcs(struct wined3d_color *colour, enum wined3d_material_color_source mcs,
2561 const struct wined3d_color *material_colour, unsigned int index,
2562 const struct wined3d_stream_info *stream_info)
2564 const struct wined3d_stream_info_element *element = NULL;
2566 switch (mcs)
2568 case WINED3D_MCS_MATERIAL:
2569 *colour = *material_colour;
2570 return;
2572 case WINED3D_MCS_COLOR1:
2573 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
2575 colour->r = colour->g = colour->b = colour->a = 1.0f;
2576 return;
2578 element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2579 break;
2581 case WINED3D_MCS_COLOR2:
2582 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
2584 colour->r = colour->g = colour->b = colour->a = 0.0f;
2585 return;
2587 element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2588 break;
2590 default:
2591 colour->r = colour->g = colour->b = colour->a = 0.0f;
2592 ERR("Invalid material colour source %#x.\n", mcs);
2593 return;
2596 wined3d_format_get_colour(element->format, &element->data.addr[index * element->stride], colour);
2599 static float wined3d_clamp(float value, float min_value, float max_value)
2601 return value < min_value ? min_value : value > max_value ? max_value : value;
2604 static float wined3d_vec3_dot(const struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2606 return v0->x * v1->x + v0->y * v1->y + v0->z * v1->z;
2609 static void wined3d_vec3_subtract(struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2611 v0->x -= v1->x;
2612 v0->y -= v1->y;
2613 v0->z -= v1->z;
2616 static void wined3d_vec3_scale(struct wined3d_vec3 *v, float s)
2618 v->x *= s;
2619 v->y *= s;
2620 v->z *= s;
2623 static void wined3d_vec3_normalise(struct wined3d_vec3 *v)
2625 float rnorm = 1.0f / sqrtf(wined3d_vec3_dot(v, v));
2627 if (isfinite(rnorm))
2628 wined3d_vec3_scale(v, rnorm);
2631 static void wined3d_vec3_transform(struct wined3d_vec3 *dst,
2632 const struct wined3d_vec3 *v, const struct wined3d_matrix_3x3 *m)
2634 struct wined3d_vec3 tmp;
2636 tmp.x = v->x * m->_11 + v->y * m->_21 + v->z * m->_31;
2637 tmp.y = v->x * m->_12 + v->y * m->_22 + v->z * m->_32;
2638 tmp.z = v->x * m->_13 + v->y * m->_23 + v->z * m->_33;
2640 *dst = tmp;
2643 static void wined3d_color_clamp(struct wined3d_color *dst, const struct wined3d_color *src,
2644 float min_value, float max_value)
2646 dst->r = wined3d_clamp(src->r, min_value, max_value);
2647 dst->g = wined3d_clamp(src->g, min_value, max_value);
2648 dst->b = wined3d_clamp(src->b, min_value, max_value);
2649 dst->a = wined3d_clamp(src->a, min_value, max_value);
2652 static void wined3d_color_rgb_mul_add(struct wined3d_color *dst, const struct wined3d_color *src, float c)
2654 dst->r += src->r * c;
2655 dst->g += src->g * c;
2656 dst->b += src->b * c;
2659 static void init_transformed_lights(struct lights_settings *ls,
2660 const struct wined3d_state *state, BOOL legacy_lighting, BOOL compute_lighting)
2662 const struct wined3d_light_info *lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
2663 const struct wined3d_light_info *light_info;
2664 struct wined3d_light_info *light_iter;
2665 struct light_transformed *light;
2666 struct wined3d_vec4 vec4;
2667 unsigned int light_count;
2668 unsigned int i, index;
2670 memset(ls, 0, sizeof(*ls));
2672 ls->lighting = !!compute_lighting;
2673 ls->fog_mode = state->render_states[WINED3D_RS_FOGVERTEXMODE];
2674 ls->fog_coord_mode = state->render_states[WINED3D_RS_RANGEFOGENABLE]
2675 ? WINED3D_FFP_VS_FOG_RANGE : WINED3D_FFP_VS_FOG_DEPTH;
2676 ls->fog_start = wined3d_get_float_state(state, WINED3D_RS_FOGSTART);
2677 ls->fog_end = wined3d_get_float_state(state, WINED3D_RS_FOGEND);
2678 ls->fog_density = wined3d_get_float_state(state, WINED3D_RS_FOGDENSITY);
2680 if (ls->fog_mode == WINED3D_FOG_NONE && !compute_lighting)
2681 return;
2683 multiply_matrix(&ls->modelview_matrix, &state->transforms[WINED3D_TS_VIEW],
2684 &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
2686 if (!compute_lighting)
2687 return;
2689 compute_normal_matrix(&ls->normal_matrix._11, legacy_lighting, &ls->modelview_matrix);
2691 wined3d_color_from_d3dcolor(&ls->ambient_light, state->render_states[WINED3D_RS_AMBIENT]);
2692 ls->legacy_lighting = !!legacy_lighting;
2693 ls->normalise = !!state->render_states[WINED3D_RS_NORMALIZENORMALS];
2694 ls->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
2696 index = 0;
2697 RB_FOR_EACH_ENTRY(light_iter, &state->light_state.lights_tree, struct wined3d_light_info, entry)
2699 if (!light_iter->enabled)
2700 continue;
2702 switch (light_iter->OriginalParms.type)
2704 case WINED3D_LIGHT_DIRECTIONAL:
2705 ++ls->directional_light_count;
2706 break;
2708 case WINED3D_LIGHT_POINT:
2709 ++ls->point_light_count;
2710 break;
2712 case WINED3D_LIGHT_SPOT:
2713 ++ls->spot_light_count;
2714 break;
2716 case WINED3D_LIGHT_PARALLELPOINT:
2717 ++ls->parallel_point_light_count;
2718 break;
2720 default:
2721 FIXME("Unhandled light type %#x.\n", light_iter->OriginalParms.type);
2722 continue;
2724 lights[index++] = light_iter;
2725 if (index == WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS)
2726 break;
2729 light_count = index;
2730 for (i = 0, index = 0; i < light_count; ++i)
2732 light_info = lights[i];
2733 if (light_info->OriginalParms.type != WINED3D_LIGHT_DIRECTIONAL)
2734 continue;
2736 light = &ls->lights[index];
2737 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
2738 light->direction = *(struct wined3d_vec3 *)&vec4;
2739 wined3d_vec3_normalise(&light->direction);
2741 light->diffuse = light_info->OriginalParms.diffuse;
2742 light->ambient = light_info->OriginalParms.ambient;
2743 light->specular = light_info->OriginalParms.specular;
2744 ++index;
2747 for (i = 0; i < light_count; ++i)
2749 light_info = lights[i];
2750 if (light_info->OriginalParms.type != WINED3D_LIGHT_POINT)
2751 continue;
2753 light = &ls->lights[index];
2755 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2756 light->range = light_info->OriginalParms.range;
2757 light->c_att = light_info->OriginalParms.attenuation0;
2758 light->l_att = light_info->OriginalParms.attenuation1;
2759 light->q_att = light_info->OriginalParms.attenuation2;
2761 light->diffuse = light_info->OriginalParms.diffuse;
2762 light->ambient = light_info->OriginalParms.ambient;
2763 light->specular = light_info->OriginalParms.specular;
2764 ++index;
2767 for (i = 0; i < light_count; ++i)
2769 light_info = lights[i];
2770 if (light_info->OriginalParms.type != WINED3D_LIGHT_SPOT)
2771 continue;
2773 light = &ls->lights[index];
2775 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2776 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
2777 light->direction = *(struct wined3d_vec3 *)&vec4;
2778 wined3d_vec3_normalise(&light->direction);
2779 light->range = light_info->OriginalParms.range;
2780 light->falloff = light_info->OriginalParms.falloff;
2781 light->c_att = light_info->OriginalParms.attenuation0;
2782 light->l_att = light_info->OriginalParms.attenuation1;
2783 light->q_att = light_info->OriginalParms.attenuation2;
2784 light->cos_htheta = cosf(light_info->OriginalParms.theta / 2.0f);
2785 light->cos_hphi = cosf(light_info->OriginalParms.phi / 2.0f);
2787 light->diffuse = light_info->OriginalParms.diffuse;
2788 light->ambient = light_info->OriginalParms.ambient;
2789 light->specular = light_info->OriginalParms.specular;
2790 ++index;
2793 for (i = 0; i < light_count; ++i)
2795 light_info = lights[i];
2796 if (light_info->OriginalParms.type != WINED3D_LIGHT_PARALLELPOINT)
2797 continue;
2799 light = &ls->lights[index];
2801 wined3d_vec4_transform(&vec4, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2802 *(struct wined3d_vec3 *)&light->position = *(struct wined3d_vec3 *)&vec4;
2803 wined3d_vec3_normalise((struct wined3d_vec3 *)&light->position);
2804 light->diffuse = light_info->OriginalParms.diffuse;
2805 light->ambient = light_info->OriginalParms.ambient;
2806 light->specular = light_info->OriginalParms.specular;
2807 ++index;
2811 static void update_light_diffuse_specular(struct wined3d_color *diffuse, struct wined3d_color *specular,
2812 const struct wined3d_vec3 *dir, float att, float material_shininess,
2813 const struct wined3d_vec3 *normal_transformed,
2814 const struct wined3d_vec3 *position_transformed_normalised,
2815 const struct light_transformed *light, const struct lights_settings *ls)
2817 struct wined3d_vec3 vec3;
2818 float t, c;
2820 c = wined3d_clamp(wined3d_vec3_dot(dir, normal_transformed), 0.0f, 1.0f);
2821 wined3d_color_rgb_mul_add(diffuse, &light->diffuse, c * att);
2823 vec3 = *dir;
2824 if (ls->localviewer)
2825 wined3d_vec3_subtract(&vec3, position_transformed_normalised);
2826 else
2827 vec3.z -= 1.0f;
2828 wined3d_vec3_normalise(&vec3);
2829 t = wined3d_vec3_dot(normal_transformed, &vec3);
2830 if (t > 0.0f && (!ls->legacy_lighting || material_shininess > 0.0f)
2831 && wined3d_vec3_dot(dir, normal_transformed) > 0.0f)
2832 wined3d_color_rgb_mul_add(specular, &light->specular, att * powf(t, material_shininess));
2835 static void light_set_vertex_data(struct lights_settings *ls,
2836 const struct wined3d_vec4 *position)
2838 if (ls->fog_mode == WINED3D_FOG_NONE && !ls->lighting)
2839 return;
2841 wined3d_vec4_transform(&ls->position_transformed, position, &ls->modelview_matrix);
2842 wined3d_vec3_scale((struct wined3d_vec3 *)&ls->position_transformed, 1.0f / ls->position_transformed.w);
2845 static void compute_light(struct wined3d_color *ambient, struct wined3d_color *diffuse,
2846 struct wined3d_color *specular, struct lights_settings *ls, const struct wined3d_vec3 *normal,
2847 float material_shininess)
2849 struct wined3d_vec3 position_transformed_normalised;
2850 struct wined3d_vec3 normal_transformed = {0.0f};
2851 const struct light_transformed *light;
2852 struct wined3d_vec3 dir, dst;
2853 unsigned int i, index;
2854 float att;
2856 position_transformed_normalised = *(const struct wined3d_vec3 *)&ls->position_transformed;
2857 wined3d_vec3_normalise(&position_transformed_normalised);
2859 if (normal)
2861 wined3d_vec3_transform(&normal_transformed, normal, &ls->normal_matrix);
2862 if (ls->normalise)
2863 wined3d_vec3_normalise(&normal_transformed);
2866 diffuse->r = diffuse->g = diffuse->b = diffuse->a = 0.0f;
2867 *specular = *diffuse;
2868 *ambient = ls->ambient_light;
2870 index = 0;
2871 for (i = 0; i < ls->directional_light_count; ++i, ++index)
2873 light = &ls->lights[index];
2875 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
2876 if (normal)
2877 update_light_diffuse_specular(diffuse, specular, &light->direction, 1.0f, material_shininess,
2878 &normal_transformed, &position_transformed_normalised, light, ls);
2881 for (i = 0; i < ls->point_light_count; ++i, ++index)
2883 light = &ls->lights[index];
2884 dir.x = light->position.x - ls->position_transformed.x;
2885 dir.y = light->position.y - ls->position_transformed.y;
2886 dir.z = light->position.z - ls->position_transformed.z;
2888 dst.z = wined3d_vec3_dot(&dir, &dir);
2889 dst.y = sqrtf(dst.z);
2890 dst.x = 1.0f;
2891 if (ls->legacy_lighting)
2893 dst.y = (light->range - dst.y) / light->range;
2894 if (!(dst.y > 0.0f))
2895 continue;
2896 dst.z = dst.y * dst.y;
2898 else
2900 if (!(dst.y <= light->range))
2901 continue;
2903 att = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
2904 if (!ls->legacy_lighting)
2905 att = 1.0f / att;
2907 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
2908 if (normal)
2910 wined3d_vec3_normalise(&dir);
2911 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
2912 &normal_transformed, &position_transformed_normalised, light, ls);
2916 for (i = 0; i < ls->spot_light_count; ++i, ++index)
2918 float t;
2920 light = &ls->lights[index];
2922 dir.x = light->position.x - ls->position_transformed.x;
2923 dir.y = light->position.y - ls->position_transformed.y;
2924 dir.z = light->position.z - ls->position_transformed.z;
2926 dst.z = wined3d_vec3_dot(&dir, &dir);
2927 dst.y = sqrtf(dst.z);
2928 dst.x = 1.0f;
2930 if (ls->legacy_lighting)
2932 dst.y = (light->range - dst.y) / light->range;
2933 if (!(dst.y > 0.0f))
2934 continue;
2935 dst.z = dst.y * dst.y;
2937 else
2939 if (!(dst.y <= light->range))
2940 continue;
2942 wined3d_vec3_normalise(&dir);
2943 t = -wined3d_vec3_dot(&dir, &light->direction);
2944 if (t > light->cos_htheta)
2945 att = 1.0f;
2946 else if (t <= light->cos_hphi)
2947 att = 0.0f;
2948 else
2949 att = powf((t - light->cos_hphi) / (light->cos_htheta - light->cos_hphi), light->falloff);
2951 t = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
2952 if (ls->legacy_lighting)
2953 att *= t;
2954 else
2955 att /= t;
2957 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
2959 if (normal)
2960 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
2961 &normal_transformed, &position_transformed_normalised, light, ls);
2964 for (i = 0; i < ls->parallel_point_light_count; ++i, ++index)
2966 light = &ls->lights[index];
2968 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
2969 if (normal)
2970 update_light_diffuse_specular(diffuse, specular, (const struct wined3d_vec3 *)&light->position,
2971 1.0f, material_shininess, &normal_transformed, &position_transformed_normalised, light, ls);
2975 static float wined3d_calculate_fog_factor(float fog_coord, const struct lights_settings *ls)
2977 switch (ls->fog_mode)
2979 case WINED3D_FOG_NONE:
2980 return fog_coord;
2981 case WINED3D_FOG_LINEAR:
2982 return (ls->fog_end - fog_coord) / (ls->fog_end - ls->fog_start);
2983 case WINED3D_FOG_EXP:
2984 return expf(-fog_coord * ls->fog_density);
2985 case WINED3D_FOG_EXP2:
2986 return expf(-fog_coord * fog_coord * ls->fog_density * ls->fog_density);
2987 default:
2988 ERR("Unhandled fog mode %#x.\n", ls->fog_mode);
2989 return 0.0f;
2993 static void update_fog_factor(float *fog_factor, struct lights_settings *ls)
2995 float fog_coord;
2997 if (ls->fog_mode == WINED3D_FOG_NONE)
2998 return;
3000 switch (ls->fog_coord_mode)
3002 case WINED3D_FFP_VS_FOG_RANGE:
3003 fog_coord = sqrtf(wined3d_vec3_dot((const struct wined3d_vec3 *)&ls->position_transformed,
3004 (const struct wined3d_vec3 *)&ls->position_transformed));
3005 break;
3007 case WINED3D_FFP_VS_FOG_DEPTH:
3008 fog_coord = fabsf(ls->position_transformed.z);
3009 break;
3011 default:
3012 ERR("Unhandled fog coordinate mode %#x.\n", ls->fog_coord_mode);
3013 return;
3015 *fog_factor = wined3d_calculate_fog_factor(fog_coord, ls);
3018 /* Context activation is done by the caller. */
3019 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3020 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3021 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, uint32_t flags, uint32_t dst_fvf)
3023 enum wined3d_material_color_source diffuse_source, specular_source, ambient_source, emissive_source;
3024 const struct wined3d_color *material_specular_state_colour;
3025 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3026 const struct wined3d_state *state = device->cs->c.state;
3027 const struct wined3d_format *output_colour_format;
3028 static const struct wined3d_color black;
3029 struct wined3d_map_desc map_desc;
3030 struct wined3d_box box = {0};
3031 struct wined3d_viewport vp;
3032 unsigned int texture_count;
3033 struct lights_settings ls;
3034 unsigned int vertex_size;
3035 BOOL do_clip, lighting;
3036 float min_z, max_z;
3037 unsigned int i;
3038 BYTE *dest_ptr;
3039 HRESULT hr;
3041 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3043 ERR("Source has no position mask.\n");
3044 return WINED3DERR_INVALIDCALL;
3047 if (state->render_states[WINED3D_RS_CLIPPING])
3049 static BOOL warned = FALSE;
3051 * The clipping code is not quite correct. Some things need
3052 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3053 * so disable clipping for now.
3054 * (The graphics in Half-Life are broken, and my processvertices
3055 * test crashes with IDirect3DDevice3)
3056 do_clip = TRUE;
3058 do_clip = FALSE;
3059 if (!warned)
3061 warned = TRUE;
3062 FIXME("Clipping is broken and disabled for now\n");
3065 else
3066 do_clip = FALSE;
3068 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3069 box.left = dwDestIndex * vertex_size;
3070 box.right = box.left + dwCount * vertex_size;
3071 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3073 WARN("Failed to map buffer, hr %#lx.\n", hr);
3074 return hr;
3076 dest_ptr = map_desc.data;
3078 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3079 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3080 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3082 TRACE("View mat:\n");
3083 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3084 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3085 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3086 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3088 TRACE("Proj mat:\n");
3089 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3090 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3091 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3092 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3094 TRACE("World mat:\n");
3095 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3096 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3097 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3098 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3100 /* Get the viewport */
3101 wined3d_device_context_get_viewports(&device->cs->c, NULL, &vp);
3102 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3103 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3105 multiply_matrix(&mat,&view_mat,&world_mat);
3106 multiply_matrix(&mat,&proj_mat,&mat);
3108 texture_count = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3110 lighting = state->render_states[WINED3D_RS_LIGHTING]
3111 && (dst_fvf & (WINED3DFVF_DIFFUSE | WINED3DFVF_SPECULAR));
3112 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
3113 &ambient_source, &specular_source, state, stream_info);
3114 output_colour_format = wined3d_get_format(device->adapter, WINED3DFMT_B8G8R8A8_UNORM, 0);
3115 material_specular_state_colour = state->render_states[WINED3D_RS_SPECULARENABLE]
3116 ? &state->material.specular : &black;
3117 init_transformed_lights(&ls, state, device->adapter->d3d_info.wined3d_creation_flags
3118 & WINED3D_LEGACY_FFP_LIGHTING, lighting);
3120 wined3d_viewport_get_z_range(&vp, &min_z, &max_z);
3122 for (i = 0; i < dwCount; ++i)
3124 const struct wined3d_stream_info_element *position_element = &stream_info->elements[WINED3D_FFP_POSITION];
3125 const float *p = (const float *)&position_element->data.addr[i * position_element->stride];
3126 struct wined3d_color ambient, diffuse, specular;
3127 struct wined3d_vec4 position;
3128 unsigned int tex_index;
3130 position.x = p[0];
3131 position.y = p[1];
3132 position.z = p[2];
3133 position.w = 1.0f;
3135 light_set_vertex_data(&ls, &position);
3137 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3138 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3139 /* The position first */
3140 float x, y, z, rhw;
3141 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3143 /* Multiplication with world, view and projection matrix. */
3144 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3145 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3146 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3147 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3149 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3151 /* WARNING: The following things are taken from d3d7 and were not yet checked
3152 * against d3d8 or d3d9!
3155 /* Clipping conditions: From msdn
3157 * A vertex is clipped if it does not match the following requirements
3158 * -rhw < x <= rhw
3159 * -rhw < y <= rhw
3160 * 0 < z <= rhw
3161 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3163 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3164 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3168 if (!do_clip || (-rhw - eps < x && -rhw - eps < y && -eps < z && x <= rhw + eps
3169 && y <= rhw + eps && z <= rhw + eps && rhw > eps))
3171 /* "Normal" viewport transformation (not clipped)
3172 * 1) The values are divided by rhw
3173 * 2) The y axis is negative, so multiply it with -1
3174 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3175 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3176 * 4) Multiply x with Width/2 and add Width/2
3177 * 5) The same for the height
3178 * 6) Add the viewpoint X and Y to the 2D coordinates and
3179 * The minimum Z value to z
3180 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3182 * Well, basically it's simply a linear transformation into viewport
3183 * coordinates
3186 x /= rhw;
3187 y /= rhw;
3188 z /= rhw;
3190 y *= -1;
3192 x *= vp.width / 2;
3193 y *= vp.height / 2;
3194 z *= max_z - min_z;
3196 x += vp.width / 2 + vp.x;
3197 y += vp.height / 2 + vp.y;
3198 z += min_z;
3200 rhw = 1 / rhw;
3201 } else {
3202 /* That vertex got clipped
3203 * Contrary to OpenGL it is not dropped completely, it just
3204 * undergoes a different calculation.
3206 TRACE("Vertex got clipped\n");
3207 x += rhw;
3208 y += rhw;
3210 x /= 2;
3211 y /= 2;
3213 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3214 * outside of the main vertex buffer memory. That needs some more
3215 * investigation...
3219 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3222 ( (float *) dest_ptr)[0] = x;
3223 ( (float *) dest_ptr)[1] = y;
3224 ( (float *) dest_ptr)[2] = z;
3225 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3227 dest_ptr += 3 * sizeof(float);
3229 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3230 dest_ptr += sizeof(float);
3233 if (dst_fvf & WINED3DFVF_PSIZE)
3234 dest_ptr += sizeof(DWORD);
3236 if (dst_fvf & WINED3DFVF_NORMAL)
3238 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3239 const float *normal = (const float *)(element->data.addr + i * element->stride);
3240 /* AFAIK this should go into the lighting information */
3241 FIXME("Didn't expect the destination to have a normal\n");
3242 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3245 if (lighting)
3247 const struct wined3d_stream_info_element *element;
3248 struct wined3d_vec3 *normal;
3250 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3252 element = &stream_info->elements[WINED3D_FFP_NORMAL];
3253 normal = (struct wined3d_vec3 *)&element->data.addr[i * element->stride];
3255 else
3257 normal = NULL;
3259 compute_light(&ambient, &diffuse, &specular, &ls, normal,
3260 state->render_states[WINED3D_RS_SPECULARENABLE] ? state->material.power : 0.0f);
3263 if (dst_fvf & WINED3DFVF_DIFFUSE)
3265 struct wined3d_color material_diffuse, material_ambient, material_emissive, diffuse_colour;
3267 wined3d_colour_from_mcs(&material_diffuse, diffuse_source,
3268 &state->material.diffuse, i, stream_info);
3270 if (lighting)
3272 wined3d_colour_from_mcs(&material_ambient, ambient_source,
3273 &state->material.ambient, i, stream_info);
3274 wined3d_colour_from_mcs(&material_emissive, emissive_source,
3275 &state->material.emissive, i, stream_info);
3277 diffuse_colour.r = ambient.r * material_ambient.r
3278 + diffuse.r * material_diffuse.r + material_emissive.r;
3279 diffuse_colour.g = ambient.g * material_ambient.g
3280 + diffuse.g * material_diffuse.g + material_emissive.g;
3281 diffuse_colour.b = ambient.b * material_ambient.b
3282 + diffuse.b * material_diffuse.b + material_emissive.b;
3283 diffuse_colour.a = material_diffuse.a;
3285 else
3287 diffuse_colour = material_diffuse;
3289 wined3d_color_clamp(&diffuse_colour, &diffuse_colour, 0.0f, 1.0f);
3290 wined3d_format_convert_from_float(output_colour_format, &diffuse_colour, dest_ptr);
3291 dest_ptr += sizeof(DWORD);
3294 if (dst_fvf & WINED3DFVF_SPECULAR)
3296 struct wined3d_color material_specular, specular_colour;
3298 wined3d_colour_from_mcs(&material_specular, specular_source,
3299 material_specular_state_colour, i, stream_info);
3301 if (lighting)
3303 specular_colour.r = specular.r * material_specular.r;
3304 specular_colour.g = specular.g * material_specular.g;
3305 specular_colour.b = specular.b * material_specular.b;
3306 specular_colour.a = ls.legacy_lighting ? 0.0f : material_specular.a;
3308 else
3310 specular_colour = material_specular;
3312 update_fog_factor(&specular_colour.a, &ls);
3313 wined3d_color_clamp(&specular_colour, &specular_colour, 0.0f, 1.0f);
3314 wined3d_format_convert_from_float(output_colour_format, &specular_colour, dest_ptr);
3315 dest_ptr += sizeof(DWORD);
3318 for (tex_index = 0; tex_index < texture_count; ++tex_index)
3320 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3321 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3322 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3324 ERR("No source texture, but destination requests one\n");
3325 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3327 else
3329 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3334 wined3d_resource_unmap(&dest->resource, 0);
3336 return WINED3D_OK;
3338 #undef copy_and_next
3340 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3341 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3342 const struct wined3d_vertex_declaration *declaration, uint32_t flags, uint32_t dst_fvf)
3344 struct wined3d_state *state = device->cs->c.state;
3345 struct wined3d_stream_info stream_info;
3346 struct wined3d_resource *resource;
3347 struct wined3d_box box = {0};
3348 struct wined3d_shader *vs;
3349 unsigned int i, j;
3350 uint32_t map;
3351 HRESULT hr;
3353 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3354 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3355 device, src_start_idx, dst_idx, vertex_count,
3356 dst_buffer, declaration, flags, dst_fvf);
3358 if (declaration)
3359 FIXME("Output vertex declaration not implemented yet.\n");
3361 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3362 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3363 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->d3d_info);
3364 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3366 /* We can't convert FROM a VBO, and vertex buffers used to source into
3367 * process_vertices() are unlikely to ever be used for drawing. Release
3368 * VBOs in those buffers and fix up the stream_info structure.
3370 * Also apply the start index. */
3371 map = stream_info.use_map;
3372 while (map)
3374 struct wined3d_stream_info_element *e;
3375 struct wined3d_map_desc map_desc;
3377 i = wined3d_bit_scan(&map);
3378 e = &stream_info.elements[i];
3379 resource = &state->streams[e->stream_idx].buffer->resource;
3380 box.left = src_start_idx * e->stride;
3381 box.right = box.left + vertex_count * e->stride;
3382 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3384 ERR("Failed to map resource.\n");
3385 map = stream_info.use_map;
3386 while (map)
3388 j = wined3d_bit_scan(&map);
3389 if (j >= i)
3390 break;
3392 e = &stream_info.elements[j];
3393 resource = &state->streams[e->stream_idx].buffer->resource;
3394 if (FAILED(wined3d_resource_unmap(resource, 0)))
3395 ERR("Failed to unmap resource.\n");
3397 return WINED3DERR_INVALIDCALL;
3399 e->data.buffer_object = 0;
3400 e->data.addr += (ULONG_PTR)map_desc.data;
3403 hr = process_vertices_strided(device, dst_idx, vertex_count,
3404 &stream_info, dst_buffer, flags, dst_fvf);
3406 map = stream_info.use_map;
3407 while (map)
3409 i = wined3d_bit_scan(&map);
3410 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3411 if (FAILED(wined3d_resource_unmap(resource, 0)))
3412 ERR("Failed to unmap resource.\n");
3415 return hr;
3418 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
3420 TRACE("device %p, caps %p.\n", device, caps);
3422 return wined3d_get_device_caps(device->adapter, device->create_parms.device_type, caps);
3425 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3426 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3428 struct wined3d_swapchain *swapchain;
3430 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3431 device, swapchain_idx, mode, rotation);
3433 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3434 return WINED3DERR_INVALIDCALL;
3436 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3439 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3441 /* At the moment we have no need for any functionality at the beginning
3442 * of a scene. */
3443 TRACE("device %p.\n", device);
3445 if (device->inScene)
3447 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3448 return WINED3DERR_INVALIDCALL;
3450 device->inScene = TRUE;
3451 return WINED3D_OK;
3454 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3456 TRACE("device %p.\n", device);
3458 if (!device->inScene)
3460 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3461 return WINED3DERR_INVALIDCALL;
3464 device->inScene = FALSE;
3465 return WINED3D_OK;
3468 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, unsigned int rect_count,
3469 const RECT *rects, uint32_t flags, const struct wined3d_color *color, float depth, unsigned int stencil)
3471 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
3473 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3474 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3476 if (!rect_count && rects)
3478 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3479 return WINED3D_OK;
3482 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3484 struct wined3d_rendertarget_view *ds = fb->depth_stencil;
3485 if (!ds)
3487 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3488 /* TODO: What about depth stencil buffers without stencil bits? */
3489 return WINED3DERR_INVALIDCALL;
3491 else if (flags & WINED3DCLEAR_TARGET)
3493 if (ds->width < fb->render_targets[0]->width
3494 || ds->height < fb->render_targets[0]->height)
3496 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3497 return WINED3D_OK;
3502 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3504 return WINED3D_OK;
3507 struct wined3d_query * CDECL wined3d_device_context_get_predication(struct wined3d_device_context *context, BOOL *value)
3509 struct wined3d_state *state = context->state;
3511 TRACE("context %p, value %p.\n", context, value);
3513 if (value)
3514 *value = state->predicate_value;
3515 return state->predicate;
3518 void CDECL wined3d_device_context_set_primitive_type(struct wined3d_device_context *context,
3519 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
3521 struct wined3d_state *state = context->state;
3523 TRACE("context %p, primitive_type %s, patch_vertex_count %u.\n",
3524 context, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
3526 wined3d_device_context_lock(context);
3527 state->primitive_type = primitive_type;
3528 state->patch_vertex_count = patch_vertex_count;
3529 wined3d_device_context_unlock(context);
3532 void CDECL wined3d_device_context_get_primitive_type(const struct wined3d_device_context *context,
3533 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
3535 const struct wined3d_state *state = context->state;
3537 TRACE("context %p, primitive_type %p, patch_vertex_count %p.\n",
3538 context, primitive_type, patch_vertex_count);
3540 *primitive_type = state->primitive_type;
3541 if (patch_vertex_count)
3542 *patch_vertex_count = state->patch_vertex_count;
3544 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
3547 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3548 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3550 unsigned int src_size, dst_size, src_skip_levels = 0;
3551 unsigned int src_level_count, dst_level_count;
3552 const struct wined3d_dirty_regions *regions;
3553 unsigned int layer_count, level_count, i, j;
3554 enum wined3d_resource_type type;
3555 BOOL entire_texture = TRUE;
3556 struct wined3d_box box;
3558 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3560 /* Verify that the source and destination textures are non-NULL. */
3561 if (!src_texture || !dst_texture)
3563 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3564 return WINED3DERR_INVALIDCALL;
3567 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
3568 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
3570 WARN("Source resource is GPU accessible or a scratch resource.\n");
3571 return WINED3DERR_INVALIDCALL;
3573 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
3575 WARN("Destination resource is CPU accessible.\n");
3576 return WINED3DERR_INVALIDCALL;
3579 /* Verify that the source and destination textures are the same type. */
3580 type = src_texture->resource.type;
3581 if (dst_texture->resource.type != type)
3583 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3584 return WINED3DERR_INVALIDCALL;
3587 layer_count = src_texture->layer_count;
3588 if (layer_count != dst_texture->layer_count)
3590 WARN("Source and destination have different layer counts.\n");
3591 return WINED3DERR_INVALIDCALL;
3594 if (src_texture->resource.format != dst_texture->resource.format)
3596 WARN("Source and destination formats do not match.\n");
3597 return WINED3DERR_INVALIDCALL;
3600 src_level_count = src_texture->level_count;
3601 dst_level_count = dst_texture->level_count;
3602 level_count = min(src_level_count, dst_level_count);
3604 src_size = max(src_texture->resource.width, src_texture->resource.height);
3605 src_size = max(src_size, src_texture->resource.depth);
3606 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3607 dst_size = max(dst_size, dst_texture->resource.depth);
3608 while (src_size > dst_size)
3610 src_size >>= 1;
3611 ++src_skip_levels;
3614 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
3615 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
3616 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
3618 WARN("Source and destination dimensions do not match.\n");
3619 return WINED3DERR_INVALIDCALL;
3622 if ((regions = src_texture->dirty_regions))
3624 for (i = 0; i < layer_count && entire_texture; ++i)
3626 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
3627 continue;
3629 entire_texture = FALSE;
3630 break;
3634 /* Update every surface level of the texture. */
3635 if (entire_texture)
3637 for (i = 0; i < level_count; ++i)
3639 wined3d_texture_get_level_box(dst_texture, i, &box);
3640 for (j = 0; j < layer_count; ++j)
3642 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
3643 &dst_texture->resource, j * dst_level_count + i, &box,
3644 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
3645 0, NULL, WINED3D_TEXF_POINT);
3649 else
3651 unsigned int src_level, box_count, k;
3652 const struct wined3d_box *boxes;
3653 struct wined3d_box b;
3655 for (i = 0; i < layer_count; ++i)
3657 boxes = regions[i].boxes;
3658 box_count = regions[i].box_count;
3659 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
3661 boxes = &b;
3662 box_count = 1;
3663 wined3d_texture_get_level_box(dst_texture, i, &b);
3666 for (j = 0; j < level_count; ++j)
3668 src_level = j + src_skip_levels;
3670 /* TODO: We could pass an array of boxes here to avoid
3671 * multiple context acquisitions for the same resource. */
3672 for (k = 0; k < box_count; ++k)
3674 box = boxes[k];
3675 if (src_level)
3677 box.left >>= src_level;
3678 box.top >>= src_level;
3679 box.right = min((box.right + (1u << src_level) - 1) >> src_level,
3680 wined3d_texture_get_level_width(src_texture, src_level));
3681 box.bottom = min((box.bottom + (1u << src_level) - 1) >> src_level,
3682 wined3d_texture_get_level_height(src_texture, src_level));
3683 box.front >>= src_level;
3684 box.back = min((box.back + (1u << src_level) - 1) >> src_level,
3685 wined3d_texture_get_level_depth(src_texture, src_level));
3688 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
3689 &dst_texture->resource, i * dst_level_count + j, &box,
3690 &src_texture->resource, i * src_level_count + src_level, &box,
3691 0, NULL, WINED3D_TEXF_POINT);
3697 wined3d_texture_clear_dirty_regions(src_texture);
3699 return WINED3D_OK;
3702 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3704 const struct wined3d_state *state = device->cs->c.state;
3705 struct wined3d_texture *texture;
3706 unsigned i;
3708 TRACE("device %p, num_passes %p.\n", device, num_passes);
3710 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
3712 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3714 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3715 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3717 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3719 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3720 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3723 texture = state->textures[i];
3724 if (!texture || texture->resource.format_caps & WINED3D_FORMAT_CAP_FILTERING)
3725 continue;
3727 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3729 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3730 return E_FAIL;
3732 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3734 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3735 return E_FAIL;
3737 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3738 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3740 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3741 return E_FAIL;
3745 if (wined3d_state_uses_depth_buffer(state)
3746 || (state->depth_stencil_state && state->depth_stencil_state->desc.stencil))
3748 struct wined3d_rendertarget_view *rt = state->fb.render_targets[0];
3749 struct wined3d_rendertarget_view *ds = state->fb.depth_stencil;
3751 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3753 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3754 return WINED3DERR_CONFLICTINGRENDERSTATE;
3758 /* return a sensible default */
3759 *num_passes = 1;
3761 TRACE("returning D3D_OK\n");
3762 return WINED3D_OK;
3765 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3767 static BOOL warned;
3769 TRACE("device %p, software %#x.\n", device, software);
3771 if (!warned)
3773 FIXME("device %p, software %#x stub!\n", device, software);
3774 warned = TRUE;
3777 device->softwareVertexProcessing = software;
3780 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3782 static BOOL warned;
3784 TRACE("device %p.\n", device);
3786 if (!warned)
3788 TRACE("device %p stub!\n", device);
3789 warned = TRUE;
3792 return device->softwareVertexProcessing;
3795 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3796 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3798 struct wined3d_swapchain *swapchain;
3800 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3801 device, swapchain_idx, raster_status);
3803 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3804 return WINED3DERR_INVALIDCALL;
3806 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3809 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3811 static BOOL warned;
3813 TRACE("device %p, segments %.8e.\n", device, segments);
3815 if (segments != 0.0f)
3817 if (!warned)
3819 FIXME("device %p, segments %.8e stub!\n", device, segments);
3820 warned = TRUE;
3824 return WINED3D_OK;
3827 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3829 static BOOL warned;
3831 TRACE("device %p.\n", device);
3833 if (!warned)
3835 FIXME("device %p stub!\n", device);
3836 warned = TRUE;
3839 return 0.0f;
3842 void CDECL wined3d_device_context_copy_uav_counter(struct wined3d_device_context *context,
3843 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
3845 TRACE("context %p, dst_buffer %p, offset %u, uav %p.\n",
3846 context, dst_buffer, offset, uav);
3848 wined3d_device_context_lock(context);
3849 wined3d_device_context_emit_copy_uav_counter(context, dst_buffer, offset, uav);
3850 wined3d_device_context_unlock(context);
3853 static bool resources_format_compatible(const struct wined3d_resource *src_resource,
3854 const struct wined3d_resource *dst_resource)
3856 if (src_resource->format->id == dst_resource->format->id)
3857 return true;
3858 if (src_resource->format->typeless_id && src_resource->format->typeless_id == dst_resource->format->typeless_id)
3859 return true;
3860 if (src_resource->device->cs->c.state->feature_level < WINED3D_FEATURE_LEVEL_10_1)
3861 return false;
3862 if ((src_resource->format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
3863 && (dst_resource->format_attrs & WINED3D_FORMAT_ATTR_CAST_TO_BLOCK))
3864 return src_resource->format->block_byte_count == dst_resource->format->byte_count;
3865 if ((src_resource->format_attrs & WINED3D_FORMAT_ATTR_CAST_TO_BLOCK)
3866 && (dst_resource->format_attrs & WINED3D_FORMAT_ATTR_BLOCKS))
3867 return src_resource->format->byte_count == dst_resource->format->block_byte_count;
3868 return false;
3871 void CDECL wined3d_device_context_copy_resource(struct wined3d_device_context *context,
3872 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3874 unsigned int src_row_block_count, dst_row_block_count;
3875 struct wined3d_texture *dst_texture, *src_texture;
3876 unsigned int src_row_count, dst_row_count;
3877 struct wined3d_box src_box, dst_box;
3878 unsigned int i, j;
3880 TRACE("context %p, dst_resource %p, src_resource %p.\n", context, dst_resource, src_resource);
3882 if (src_resource == dst_resource)
3884 WARN("Source and destination are the same resource.\n");
3885 return;
3888 if (src_resource->type != dst_resource->type)
3890 WARN("Resource types (%s / %s) don't match.\n",
3891 debug_d3dresourcetype(dst_resource->type),
3892 debug_d3dresourcetype(src_resource->type));
3893 return;
3896 if (!resources_format_compatible(src_resource, dst_resource))
3898 WARN("Resource formats %s and %s are incompatible.\n",
3899 debug_d3dformat(dst_resource->format->id),
3900 debug_d3dformat(src_resource->format->id));
3901 return;
3904 src_row_block_count = (src_resource->width + (src_resource->format->block_width - 1))
3905 / src_resource->format->block_width;
3906 dst_row_block_count = (dst_resource->width + (dst_resource->format->block_width - 1))
3907 / dst_resource->format->block_width;
3908 src_row_count = (src_resource->height + (src_resource->format->block_height - 1))
3909 / src_resource->format->block_height;
3910 dst_row_count = (dst_resource->height + (dst_resource->format->block_height - 1))
3911 / dst_resource->format->block_height;
3913 if (src_row_block_count != dst_row_block_count || src_row_count != dst_row_count
3914 || src_resource->depth != dst_resource->depth)
3916 WARN("Resource block dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3917 dst_row_block_count, dst_row_count, dst_resource->depth,
3918 src_row_block_count, src_row_count, src_resource->depth);
3919 return;
3922 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3924 wined3d_box_set(&src_box, 0, 0, src_resource->size, 1, 0, 1);
3925 wined3d_device_context_lock(context);
3926 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, 0, &src_box,
3927 src_resource, 0, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
3928 wined3d_device_context_unlock(context);
3929 return;
3932 dst_texture = texture_from_resource(dst_resource);
3933 src_texture = texture_from_resource(src_resource);
3935 if (src_texture->layer_count != dst_texture->layer_count
3936 || src_texture->level_count != dst_texture->level_count)
3938 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3939 dst_texture->layer_count, dst_texture->level_count,
3940 src_texture->layer_count, src_texture->level_count);
3941 return;
3944 wined3d_device_context_lock(context);
3945 for (i = 0; i < dst_texture->level_count; ++i)
3947 wined3d_texture_get_level_box(src_texture, i, &src_box);
3948 wined3d_texture_get_level_box(dst_texture, i, &dst_box);
3949 for (j = 0; j < dst_texture->layer_count; ++j)
3951 unsigned int idx = j * dst_texture->level_count + i;
3953 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, idx, &dst_box,
3954 src_resource, idx, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
3957 wined3d_device_context_unlock(context);
3960 HRESULT CDECL wined3d_device_context_copy_sub_resource_region(struct wined3d_device_context *context,
3961 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3962 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3963 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
3965 struct wined3d_box dst_box, b;
3967 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3968 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
3969 context, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3970 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
3972 if (flags)
3973 FIXME("Ignoring flags %#x.\n", flags);
3975 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3977 WARN("Source and destination are the same sub-resource.\n");
3978 return WINED3DERR_INVALIDCALL;
3981 if (!resources_format_compatible(src_resource, dst_resource))
3983 WARN("Resource formats %s and %s are incompatible.\n",
3984 debug_d3dformat(dst_resource->format->id),
3985 debug_d3dformat(src_resource->format->id));
3986 return WINED3DERR_INVALIDCALL;
3989 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3991 if (src_resource->type != WINED3D_RTYPE_BUFFER)
3993 WARN("Resource types (%s / %s) don't match.\n",
3994 debug_d3dresourcetype(dst_resource->type),
3995 debug_d3dresourcetype(src_resource->type));
3996 return WINED3DERR_INVALIDCALL;
3999 if (dst_sub_resource_idx)
4001 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4002 return WINED3DERR_INVALIDCALL;
4005 if (src_sub_resource_idx)
4007 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4008 return WINED3DERR_INVALIDCALL;
4011 if (!src_box)
4013 unsigned int dst_w;
4015 dst_w = dst_resource->size - dst_x;
4016 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4017 src_box = &b;
4019 else if ((src_box->left >= src_box->right
4020 || src_box->top >= src_box->bottom
4021 || src_box->front >= src_box->back))
4023 WARN("Invalid box %s specified.\n", debug_box(src_box));
4024 return WINED3DERR_INVALIDCALL;
4027 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4028 || src_box->right - src_box->left > dst_resource->size - dst_x)
4030 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4031 dst_x, src_box->left, src_box->right - src_box->left);
4032 return WINED3DERR_INVALIDCALL;
4035 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4037 else
4039 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4040 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4041 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4042 unsigned int src_row_block_count, src_row_count;
4044 if (!wined3d_texture_validate_sub_resource_idx(dst_texture, dst_sub_resource_idx))
4045 return WINED3DERR_INVALIDCALL;
4047 if (!wined3d_texture_validate_sub_resource_idx(src_texture, src_sub_resource_idx))
4048 return WINED3DERR_INVALIDCALL;
4050 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4052 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4053 return WINED3DERR_INVALIDCALL;
4056 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4058 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4059 return WINED3DERR_INVALIDCALL;
4062 if (!src_box)
4064 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
4066 src_w = wined3d_texture_get_level_width(src_texture, src_level);
4067 src_h = wined3d_texture_get_level_height(src_texture, src_level);
4068 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
4070 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4071 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
4072 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
4073 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
4075 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
4076 src_box = &b;
4078 else if (FAILED(wined3d_resource_check_box_dimensions(src_resource, src_sub_resource_idx, src_box)))
4080 WARN("Invalid source box %s.\n", debug_box(src_box));
4081 return WINED3DERR_INVALIDCALL;
4084 if (src_resource->format->block_width == dst_resource->format->block_width
4085 && src_resource->format->block_height == dst_resource->format->block_height)
4087 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4088 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
4090 else
4092 src_row_block_count = (src_box->right - src_box->left + src_resource->format->block_width - 1)
4093 / src_resource->format->block_width;
4094 src_row_count = (src_box->bottom - src_box->top + src_resource->format->block_height - 1)
4095 / src_resource->format->block_height;
4096 wined3d_box_set(&dst_box, dst_x, dst_y,
4097 dst_x + (src_row_block_count * dst_resource->format->block_width),
4098 dst_y + (src_row_count * dst_resource->format->block_height),
4099 dst_z, dst_z + (src_box->back - src_box->front));
4101 if (FAILED(wined3d_resource_check_box_dimensions(dst_resource, dst_sub_resource_idx, &dst_box)))
4103 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4104 return WINED3DERR_INVALIDCALL;
4108 wined3d_device_context_lock(context);
4109 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, dst_sub_resource_idx, &dst_box,
4110 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4111 wined3d_device_context_unlock(context);
4113 return WINED3D_OK;
4116 void CDECL wined3d_device_context_update_sub_resource(struct wined3d_device_context *context,
4117 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
4118 const void *data, unsigned int row_pitch, unsigned int depth_pitch, unsigned int flags)
4120 struct wined3d_sub_resource_desc desc;
4121 struct wined3d_box b;
4123 TRACE("context %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, flags %#x.\n",
4124 context, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
4126 if (flags)
4127 FIXME("Ignoring flags %#x.\n", flags);
4129 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
4131 WARN("Resource %p is not GPU accessible.\n", resource);
4132 return;
4135 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4136 return;
4138 if (!box)
4140 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4141 box = &b;
4143 else if (box->left >= box->right || box->right > desc.width
4144 || box->top >= box->bottom || box->bottom > desc.height
4145 || box->front >= box->back || box->back > desc.depth)
4147 WARN("Invalid box %s specified.\n", debug_box(box));
4148 return;
4151 wined3d_device_context_lock(context);
4152 wined3d_device_context_emit_update_sub_resource(context, resource,
4153 sub_resource_idx, box, data, row_pitch, depth_pitch);
4154 wined3d_device_context_unlock(context);
4157 void CDECL wined3d_device_context_resolve_sub_resource(struct wined3d_device_context *context,
4158 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
4159 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
4160 enum wined3d_format_id format_id)
4162 struct wined3d_texture *dst_texture, *src_texture;
4163 unsigned int dst_level, src_level;
4164 struct wined3d_blt_fx fx = {0};
4165 RECT dst_rect, src_rect;
4167 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, "
4168 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
4169 context, dst_resource, dst_sub_resource_idx,
4170 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
4172 if (wined3d_format_is_typeless(dst_resource->format)
4173 || wined3d_format_is_typeless(src_resource->format))
4175 FIXME("Multisample resolve is not fully supported for typeless formats "
4176 "(dst_format %s, src_format %s, format %s).\n",
4177 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
4178 debug_d3dformat(format_id));
4180 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4182 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
4183 return;
4185 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4187 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
4188 return;
4191 wined3d_device_context_lock(context);
4192 fx.resolve_format_id = format_id;
4194 dst_texture = texture_from_resource(dst_resource);
4195 src_texture = texture_from_resource(src_resource);
4197 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4198 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
4199 wined3d_texture_get_level_height(dst_texture, dst_level));
4200 src_level = src_sub_resource_idx % src_texture->level_count;
4201 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4202 wined3d_texture_get_level_height(src_texture, src_level));
4203 wined3d_device_context_blt(context, dst_texture, dst_sub_resource_idx, &dst_rect,
4204 src_texture, src_sub_resource_idx, &src_rect, 0, &fx, WINED3D_TEXF_POINT);
4205 wined3d_device_context_unlock(context);
4208 HRESULT CDECL wined3d_device_context_clear_rendertarget_view(struct wined3d_device_context *context,
4209 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
4210 const struct wined3d_color *color, float depth, unsigned int stencil)
4212 struct wined3d_resource *resource;
4213 RECT r;
4215 TRACE("context %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4216 context, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4218 if (!flags)
4219 return WINED3D_OK;
4221 resource = view->resource;
4222 if (resource->type == WINED3D_RTYPE_BUFFER)
4224 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4225 return WINED3DERR_INVALIDCALL;
4228 if (!rect)
4230 SetRect(&r, 0, 0, view->width, view->height);
4231 rect = &r;
4233 else
4235 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4236 HRESULT hr;
4238 if (FAILED(hr = wined3d_resource_check_box_dimensions(resource, view->sub_resource_idx, &b)))
4239 return hr;
4242 wined3d_device_context_lock(context);
4243 wined3d_device_context_emit_clear_rendertarget_view(context, view, rect, flags, color, depth, stencil);
4244 wined3d_device_context_unlock(context);
4246 return WINED3D_OK;
4249 void CDECL wined3d_device_context_clear_uav_float(struct wined3d_device_context *context,
4250 struct wined3d_unordered_access_view *view, const struct wined3d_vec4 *clear_value)
4252 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_vec4(clear_value));
4254 if (!(view->format->attrs & (WINED3D_FORMAT_ATTR_FLOAT | WINED3D_FORMAT_ATTR_NORMALISED)))
4256 WARN("Not supported for view format %s.\n", debug_d3dformat(view->format->id));
4257 return;
4260 wined3d_device_context_lock(context);
4261 wined3d_device_context_emit_clear_uav(context, view, (const struct wined3d_uvec4 *)clear_value, true);
4262 wined3d_device_context_unlock(context);
4265 void CDECL wined3d_device_context_clear_uav_uint(struct wined3d_device_context *context,
4266 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
4268 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_uvec4(clear_value));
4270 wined3d_device_context_lock(context);
4271 wined3d_device_context_emit_clear_uav(context, view, clear_value, false);
4272 wined3d_device_context_unlock(context);
4275 static unsigned int sanitise_map_flags(const struct wined3d_resource *resource, unsigned int flags)
4277 /* Not all flags make sense together, but Windows never returns an error.
4278 * Catch the cases that could cause issues. */
4279 if (flags & WINED3D_MAP_READ)
4281 if (flags & WINED3D_MAP_DISCARD)
4283 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
4284 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4286 if (flags & WINED3D_MAP_NOOVERWRITE)
4288 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
4289 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4292 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4294 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU) || !(resource->usage & WINED3DUSAGE_DYNAMIC))
4296 WARN("DISCARD or NOOVERWRITE map not on dynamic GPU-accessible buffer, ignoring.\n");
4297 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4299 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4300 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4302 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
4303 flags &= ~WINED3D_MAP_DISCARD;
4307 return flags;
4310 HRESULT CDECL wined3d_device_context_map(struct wined3d_device_context *context,
4311 struct wined3d_resource *resource, unsigned int sub_resource_idx,
4312 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
4314 struct wined3d_sub_resource_desc desc;
4315 struct wined3d_box b;
4316 HRESULT hr;
4318 TRACE("context %p, resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
4319 context, resource, sub_resource_idx, map_desc, debug_box(box), flags);
4321 if (!(flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
4323 WARN("No read/write flags specified.\n");
4324 return E_INVALIDARG;
4327 if ((flags & WINED3D_MAP_READ) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_R))
4329 WARN("Resource does not have MAP_R access.\n");
4330 return E_INVALIDARG;
4333 if ((flags & WINED3D_MAP_WRITE) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_W))
4335 WARN("Resource does not have MAP_W access.\n");
4336 return E_INVALIDARG;
4339 flags = sanitise_map_flags(resource, flags);
4341 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4342 return E_INVALIDARG;
4344 if (!box)
4346 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4347 box = &b;
4349 else if (FAILED(wined3d_resource_check_box_dimensions(resource, sub_resource_idx, box)))
4351 WARN("Map box is invalid.\n");
4353 if (resource->type != WINED3D_RTYPE_BUFFER && resource->type != WINED3D_RTYPE_TEXTURE_2D)
4354 return WINED3DERR_INVALIDCALL;
4356 if ((resource->format_attrs & WINED3D_FORMAT_ATTR_BLOCKS) &&
4357 !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
4358 return WINED3DERR_INVALIDCALL;
4361 wined3d_device_context_lock(context);
4362 hr = wined3d_device_context_emit_map(context, resource, sub_resource_idx, map_desc, box, flags);
4363 wined3d_device_context_unlock(context);
4364 return hr;
4367 HRESULT CDECL wined3d_device_context_unmap(struct wined3d_device_context *context,
4368 struct wined3d_resource *resource, unsigned int sub_resource_idx)
4370 HRESULT hr;
4371 TRACE("context %p, resource %p, sub_resource_idx %u.\n", context, resource, sub_resource_idx);
4373 wined3d_device_context_lock(context);
4374 hr = wined3d_device_context_emit_unmap(context, resource, sub_resource_idx);
4375 wined3d_device_context_unlock(context);
4376 return hr;
4379 void CDECL wined3d_device_context_issue_query(struct wined3d_device_context *context,
4380 struct wined3d_query *query, unsigned int flags)
4382 TRACE("context %p, query %p, flags %#x.\n", context, query, flags);
4384 wined3d_device_context_lock(context);
4385 context->ops->issue_query(context, query, flags);
4386 wined3d_device_context_unlock(context);
4389 void CDECL wined3d_device_context_execute_command_list(struct wined3d_device_context *context,
4390 struct wined3d_command_list *list, bool restore_state)
4392 TRACE("context %p, list %p, restore_state %d.\n", context, list, restore_state);
4394 wined3d_device_context_lock(context);
4395 wined3d_device_context_emit_execute_command_list(context, list, restore_state);
4396 wined3d_device_context_unlock(context);
4399 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_rendertarget_view(
4400 const struct wined3d_device_context *context, unsigned int view_idx)
4402 unsigned int max_rt_count;
4404 TRACE("context %p, view_idx %u.\n", context, view_idx);
4406 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
4407 if (view_idx >= max_rt_count)
4409 WARN("Only %u render targets are supported.\n", max_rt_count);
4410 return NULL;
4413 return context->state->fb.render_targets[view_idx];
4416 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_depth_stencil_view(
4417 const struct wined3d_device_context *context)
4419 TRACE("context %p.\n", context);
4421 return context->state->fb.depth_stencil;
4424 void CDECL wined3d_device_context_generate_mipmaps(struct wined3d_device_context *context,
4425 struct wined3d_shader_resource_view *view)
4427 struct wined3d_texture *texture;
4429 TRACE("context %p, view %p.\n", context, view);
4431 if (view->resource->type == WINED3D_RTYPE_BUFFER)
4433 WARN("Called on buffer resource %p.\n", view->resource);
4434 return;
4437 texture = texture_from_resource(view->resource);
4438 if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
4440 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
4441 return;
4444 wined3d_device_context_lock(context);
4445 wined3d_device_context_emit_generate_mipmaps(context, view);
4446 wined3d_device_context_unlock(context);
4449 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4450 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4452 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4453 struct wined3d_sub_resource_data data;
4454 struct wined3d_resource_desc desc;
4455 struct wined3d_map_desc map_desc;
4456 struct wined3d_texture *texture;
4457 HRESULT hr;
4459 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
4461 ERR("Failed to map source texture.\n");
4462 return NULL;
4465 data.data = map_desc.data;
4466 data.row_pitch = map_desc.row_pitch;
4467 data.slice_pitch = map_desc.slice_pitch;
4469 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4470 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4471 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4472 desc.multisample_quality = 0;
4473 desc.usage = WINED3DUSAGE_DYNAMIC;
4474 desc.bind_flags = 0;
4475 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4476 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4477 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4478 desc.depth = 1;
4479 desc.size = 0;
4481 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
4482 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4483 if (FAILED(hr))
4485 ERR("Failed to create cursor texture.\n");
4486 return NULL;
4489 return texture;
4492 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4493 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4495 unsigned int texture_level = sub_resource_idx % texture->level_count;
4496 unsigned int cursor_width, cursor_height;
4497 struct wined3d_map_desc map_desc;
4499 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4500 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4502 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx)
4503 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4504 return WINED3DERR_INVALIDCALL;
4506 if (device->cursor_texture)
4508 wined3d_texture_decref(device->cursor_texture);
4509 device->cursor_texture = NULL;
4512 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4514 WARN("Texture %p has invalid format %s.\n",
4515 texture, debug_d3dformat(texture->resource.format->id));
4516 return WINED3DERR_INVALIDCALL;
4519 /* Cursor width and height must all be powers of two */
4520 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4521 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4522 if ((cursor_width & (cursor_width - 1)) || (cursor_height & (cursor_height - 1)))
4524 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width, cursor_height);
4525 return WINED3DERR_INVALIDCALL;
4528 /* Do not store the surface's pointer because the application may
4529 * release it after setting the cursor image. Windows doesn't
4530 * addref the set surface, so we can't do this either without
4531 * creating circular refcount dependencies. */
4532 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4534 ERR("Failed to create cursor texture.\n");
4535 return WINED3DERR_INVALIDCALL;
4538 if (cursor_width == 32 && cursor_height == 32)
4540 UINT mask_size = cursor_width * cursor_height / 8;
4541 ICONINFO cursor_info;
4542 DWORD *mask_bits;
4543 HCURSOR cursor;
4545 /* 32-bit user32 cursors ignore the alpha channel if it's all
4546 * zeroes, and use the mask instead. Fill the mask with all ones
4547 * to ensure we still get a fully transparent cursor. */
4548 if (!(mask_bits = heap_alloc(mask_size)))
4549 return E_OUTOFMEMORY;
4550 memset(mask_bits, 0xff, mask_size);
4552 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4553 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
4554 cursor_info.fIcon = FALSE;
4555 cursor_info.xHotspot = x_hotspot;
4556 cursor_info.yHotspot = y_hotspot;
4557 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4558 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4559 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4561 /* Create our cursor and clean up. */
4562 cursor = CreateIconIndirect(&cursor_info);
4563 if (cursor_info.hbmMask)
4564 DeleteObject(cursor_info.hbmMask);
4565 if (cursor_info.hbmColor)
4566 DeleteObject(cursor_info.hbmColor);
4567 if (device->hardwareCursor)
4568 DestroyCursor(device->hardwareCursor);
4569 device->hardwareCursor = cursor;
4570 if (device->bCursorVisible)
4571 SetCursor(cursor);
4573 heap_free(mask_bits);
4576 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4577 device->cursorWidth = cursor_width;
4578 device->cursorHeight = cursor_height;
4579 device->xHotSpot = x_hotspot;
4580 device->yHotSpot = y_hotspot;
4582 return WINED3D_OK;
4585 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4586 int x_screen_space, int y_screen_space, uint32_t flags)
4588 TRACE("device %p, x %d, y %d, flags %#x.\n",
4589 device, x_screen_space, y_screen_space, flags);
4591 device->xScreenSpace = x_screen_space;
4592 device->yScreenSpace = y_screen_space;
4594 if (device->hardwareCursor)
4596 POINT pt;
4598 GetCursorPos( &pt );
4599 if (x_screen_space == pt.x && y_screen_space == pt.y)
4600 return;
4601 SetCursorPos( x_screen_space, y_screen_space );
4603 /* Switch to the software cursor if position diverges from the hardware one. */
4604 GetCursorPos( &pt );
4605 if (x_screen_space != pt.x || y_screen_space != pt.y)
4607 if (device->bCursorVisible) SetCursor( NULL );
4608 DestroyCursor( device->hardwareCursor );
4609 device->hardwareCursor = 0;
4614 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4616 BOOL oldVisible = device->bCursorVisible;
4618 TRACE("device %p, show %#x.\n", device, show);
4621 * When ShowCursor is first called it should make the cursor appear at the OS's last
4622 * known cursor position.
4624 if (show && !oldVisible)
4626 POINT pt;
4627 GetCursorPos(&pt);
4628 device->xScreenSpace = pt.x;
4629 device->yScreenSpace = pt.y;
4632 if (device->hardwareCursor)
4634 device->bCursorVisible = show;
4635 if (show)
4636 SetCursor(device->hardwareCursor);
4637 else
4638 SetCursor(NULL);
4640 else if (device->cursor_texture)
4642 device->bCursorVisible = show;
4645 return oldVisible;
4648 static void mark_managed_resource_dirty(struct wined3d_resource *resource)
4650 if (resource->type != WINED3D_RTYPE_BUFFER)
4652 struct wined3d_texture *texture = texture_from_resource(resource);
4653 unsigned int i;
4655 if (texture->dirty_regions)
4657 for (i = 0; i < texture->layer_count; ++i)
4658 wined3d_texture_add_dirty_region(texture, i, NULL);
4663 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4665 struct wined3d_resource *resource, *cursor;
4667 TRACE("device %p.\n", device);
4669 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4671 TRACE("Checking resource %p for eviction.\n", resource);
4673 if ((resource->usage & WINED3DUSAGE_MANAGED) && !resource->map_count)
4675 if (resource->access & WINED3D_RESOURCE_ACCESS_GPU)
4677 TRACE("Evicting %p.\n", resource);
4678 wined3d_cs_emit_unload_resource(device->cs, resource);
4681 mark_managed_resource_dirty(resource);
4686 void CDECL wined3d_device_context_flush(struct wined3d_device_context *context)
4688 TRACE("context %p.\n", context);
4690 wined3d_device_context_lock(context);
4691 context->ops->flush(context);
4692 wined3d_device_context_unlock(context);
4695 static void update_swapchain_flags(struct wined3d_texture *texture)
4697 unsigned int flags = texture->swapchain->state.desc.flags;
4699 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
4700 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
4701 else
4702 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
4704 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4705 texture->flags |= WINED3D_TEXTURE_GET_DC;
4706 else
4707 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
4710 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4711 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4712 wined3d_device_reset_cb callback, BOOL reset_state)
4714 static struct wined3d_rendertarget_view *const views[WINED3D_MAX_RENDER_TARGETS];
4715 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
4716 struct wined3d_device_context *context = &device->cs->c;
4717 struct wined3d_swapchain_state *swapchain_state;
4718 struct wined3d_state *state = context->state;
4719 struct wined3d_swapchain_desc *current_desc;
4720 struct wined3d_resource *resource, *cursor;
4721 struct wined3d_rendertarget_view *view;
4722 struct wined3d_swapchain *swapchain;
4723 struct wined3d_view_desc view_desc;
4724 BOOL backbuffer_resized, windowed;
4725 HRESULT hr = WINED3D_OK;
4726 HWND device_window;
4727 unsigned int i;
4729 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4730 device, swapchain_desc, mode, callback, reset_state);
4732 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4734 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4736 ERR("Failed to get the first implicit swapchain.\n");
4737 return WINED3DERR_INVALIDCALL;
4739 swapchain_state = &swapchain->state;
4740 current_desc = &swapchain_state->desc;
4742 if (reset_state)
4744 if (device->logo_texture)
4746 wined3d_texture_decref(device->logo_texture);
4747 device->logo_texture = NULL;
4749 if (device->cursor_texture)
4751 wined3d_texture_decref(device->cursor_texture);
4752 device->cursor_texture = NULL;
4754 state_unbind_resources(state);
4757 wined3d_device_context_set_rendertarget_views(context, 0, d3d_info->limits.max_rt_count, views, FALSE);
4758 wined3d_device_context_set_depth_stencil_view(context, NULL);
4760 if (reset_state)
4762 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4764 TRACE("Enumerating resource %p.\n", resource);
4765 if (FAILED(hr = callback(resource)))
4766 return hr;
4770 TRACE("New params:\n");
4771 TRACE("output %p\n", swapchain_desc->output);
4772 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4773 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4774 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4775 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4776 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4777 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4778 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4779 TRACE("device_window %p\n", swapchain_desc->device_window);
4780 TRACE("windowed %#x\n", swapchain_desc->windowed);
4781 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4782 if (swapchain_desc->enable_auto_depth_stencil)
4783 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4784 TRACE("flags %#x\n", swapchain_desc->flags);
4785 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4786 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4788 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
4789 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
4791 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
4792 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
4793 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
4794 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
4796 /* No special treatment of these parameters. Just store them */
4797 current_desc->swap_effect = swapchain_desc->swap_effect;
4798 current_desc->enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4799 current_desc->auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4800 current_desc->refresh_rate = swapchain_desc->refresh_rate;
4801 current_desc->auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4803 device_window = swapchain_desc->device_window ? swapchain_desc->device_window : device->create_parms.focus_window;
4804 if (device_window && device_window != current_desc->device_window)
4806 TRACE("Changing the device window from %p to %p.\n",
4807 current_desc->device_window, device_window);
4808 current_desc->device_window = device_window;
4809 swapchain_state->device_window = device_window;
4810 wined3d_swapchain_set_window(swapchain, NULL);
4813 backbuffer_resized = swapchain_desc->backbuffer_width != current_desc->backbuffer_width
4814 || swapchain_desc->backbuffer_height != current_desc->backbuffer_height;
4815 windowed = current_desc->windowed;
4817 if (!swapchain_desc->windowed != !windowed || swapchain->reapply_mode
4818 || mode || (!swapchain_desc->windowed && backbuffer_resized))
4820 /* Switch from windowed to fullscreen. */
4821 if (windowed && !swapchain_desc->windowed)
4823 HWND focus_window = device->create_parms.focus_window;
4825 if (!focus_window)
4826 focus_window = swapchain->state.device_window;
4827 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4829 ERR("Failed to acquire focus window, hr %#lx.\n", hr);
4830 return hr;
4834 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(&swapchain->state,
4835 swapchain_desc, mode)))
4836 return hr;
4838 /* Switch from fullscreen to windowed. */
4839 if (!windowed && swapchain_desc->windowed)
4840 wined3d_device_release_focus_window(device);
4842 else if (!swapchain_desc->windowed)
4844 DWORD style = swapchain_state->style;
4845 DWORD exstyle = swapchain_state->exstyle;
4846 struct wined3d_output_desc output_desc;
4848 /* If we're in fullscreen, and the mode wasn't changed, we have to get
4849 * the window back into the right position. Some applications
4850 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
4851 * up their mess. Guild Wars also loses the device during that. */
4852 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
4854 ERR("Failed to get output description, hr %#lx.\n", hr);
4855 return hr;
4858 swapchain_state->style = 0;
4859 swapchain_state->exstyle = 0;
4860 wined3d_swapchain_state_setup_fullscreen(swapchain_state, swapchain_state->device_window,
4861 output_desc.desktop_rect.left, output_desc.desktop_rect.top,
4862 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height);
4863 swapchain_state->style = style;
4864 swapchain_state->exstyle = exstyle;
4867 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4868 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4869 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4870 return hr;
4872 if (swapchain_desc->flags != current_desc->flags)
4874 current_desc->flags = swapchain_desc->flags;
4876 update_swapchain_flags(swapchain->front_buffer);
4877 for (i = 0; i < current_desc->backbuffer_count; ++i)
4879 update_swapchain_flags(swapchain->back_buffers[i]);
4883 if ((view = device->auto_depth_stencil_view))
4885 device->auto_depth_stencil_view = NULL;
4886 wined3d_rendertarget_view_decref(view);
4888 if (current_desc->enable_auto_depth_stencil)
4890 struct wined3d_resource_desc texture_desc;
4891 struct wined3d_texture *texture;
4893 TRACE("Creating the depth stencil buffer.\n");
4895 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4896 texture_desc.format = current_desc->auto_depth_stencil_format;
4897 texture_desc.multisample_type = current_desc->multisample_type;
4898 texture_desc.multisample_quality = current_desc->multisample_quality;
4899 texture_desc.usage = 0;
4900 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
4901 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4902 texture_desc.width = current_desc->backbuffer_width;
4903 texture_desc.height = current_desc->backbuffer_height;
4904 texture_desc.depth = 1;
4905 texture_desc.size = 0;
4907 if (FAILED(hr = wined3d_texture_create(device, &texture_desc, 1, 1, 0,
4908 NULL, NULL, &wined3d_null_parent_ops, &texture)))
4910 ERR("Failed to create the auto depth/stencil surface, hr %#lx.\n", hr);
4911 return WINED3DERR_INVALIDCALL;
4914 view_desc.format_id = texture->resource.format->id;
4915 view_desc.flags = 0;
4916 view_desc.u.texture.level_idx = 0;
4917 view_desc.u.texture.level_count = 1;
4918 view_desc.u.texture.layer_idx = 0;
4919 view_desc.u.texture.layer_count = 1;
4920 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4921 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4922 wined3d_texture_decref(texture);
4923 if (FAILED(hr))
4925 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
4926 return hr;
4930 if ((view = device->back_buffer_view))
4932 device->back_buffer_view = NULL;
4933 wined3d_rendertarget_view_decref(view);
4935 if (current_desc->backbuffer_count && current_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
4937 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4939 view_desc.format_id = back_buffer->format->id;
4940 view_desc.flags = 0;
4941 view_desc.u.texture.level_idx = 0;
4942 view_desc.u.texture.level_count = 1;
4943 view_desc.u.texture.layer_idx = 0;
4944 view_desc.u.texture.layer_count = 1;
4945 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4946 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4948 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
4949 return hr;
4953 wine_rb_destroy(&device->samplers, device_free_sampler, NULL);
4954 wine_rb_destroy(&device->rasterizer_states, device_free_rasterizer_state, NULL);
4955 wine_rb_destroy(&device->blend_states, device_free_blend_state, NULL);
4956 wine_rb_destroy(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
4958 if (reset_state)
4960 TRACE("Resetting state.\n");
4961 wined3d_device_context_emit_reset_state(&device->cs->c, false);
4962 state_cleanup(state);
4964 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4966 TRACE("Unloading resource %p.\n", resource);
4967 wined3d_cs_emit_unload_resource(device->cs, resource);
4969 if (resource->usage & WINED3DUSAGE_MANAGED)
4970 mark_managed_resource_dirty(resource);
4973 device->adapter->adapter_ops->adapter_uninit_3d(device);
4975 wined3d_state_reset(state, &device->adapter->d3d_info);
4977 device_init_swapchain_state(device, swapchain);
4978 if (wined3d_settings.logo)
4979 device_load_logo(device, wined3d_settings.logo);
4981 hr = device->adapter->adapter_ops->adapter_init_3d(device);
4983 else
4985 if ((view = device->back_buffer_view))
4986 wined3d_device_context_set_rendertarget_views(context, 0, 1, &view, FALSE);
4987 if ((view = device->auto_depth_stencil_view))
4988 wined3d_device_context_set_depth_stencil_view(context, view);
4991 /* All done. There is no need to reload resources or shaders, this will
4992 * happen automatically on the first use. */
4993 return hr;
4996 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4998 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5000 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5002 return WINED3D_OK;
5006 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5007 struct wined3d_device_creation_parameters *parameters)
5009 TRACE("device %p, parameters %p.\n", device, parameters);
5011 *parameters = device->create_parms;
5014 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5016 TRACE("device %p.\n", device);
5018 return device->wined3d;
5021 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5022 UINT swapchain_idx, uint32_t flags, const struct wined3d_gamma_ramp *ramp)
5024 struct wined3d_swapchain *swapchain;
5026 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5027 device, swapchain_idx, flags, ramp);
5029 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5030 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5033 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5034 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5036 struct wined3d_swapchain *swapchain;
5038 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5039 device, swapchain_idx, ramp);
5041 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5042 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5045 HDC wined3d_device_gl_get_backup_dc(struct wined3d_device_gl *device_gl)
5047 TRACE("device_gl %p.\n", device_gl);
5049 if (!device_gl->backup_dc)
5051 TRACE("Creating the backup window for device %p.\n", device_gl);
5053 if (!(device_gl->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
5054 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
5056 ERR("Failed to create a window.\n");
5057 return NULL;
5060 if (!(device_gl->backup_dc = GetDC(device_gl->backup_wnd)))
5062 ERR("Failed to get a DC.\n");
5063 DestroyWindow(device_gl->backup_wnd);
5064 device_gl->backup_wnd = NULL;
5065 return NULL;
5069 return device_gl->backup_dc;
5072 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5074 TRACE("device %p, resource %p.\n", device, resource);
5076 wined3d_not_from_cs(device->cs);
5078 list_add_head(&device->resources, &resource->resource_list_entry);
5081 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5083 TRACE("device %p, resource %p.\n", device, resource);
5085 wined3d_not_from_cs(device->cs);
5087 list_remove(&resource->resource_list_entry);
5090 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5092 enum wined3d_resource_type type = resource->type;
5093 struct wined3d_state *state = device->cs->c.state;
5094 struct wined3d_rendertarget_view *rtv;
5095 unsigned int i;
5097 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5099 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
5101 if ((rtv = state->fb.render_targets[i]) && rtv->resource == resource)
5102 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5105 if ((rtv = state->fb.depth_stencil) && rtv->resource == resource)
5106 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5108 switch (type)
5110 case WINED3D_RTYPE_TEXTURE_1D:
5111 case WINED3D_RTYPE_TEXTURE_2D:
5112 case WINED3D_RTYPE_TEXTURE_3D:
5113 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
5115 if (&state->textures[i]->resource == resource)
5117 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
5118 state->textures[i] = NULL;
5121 break;
5123 case WINED3D_RTYPE_BUFFER:
5124 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
5126 if (&state->streams[i].buffer->resource == resource)
5128 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5129 state->streams[i].buffer = NULL;
5133 if (&state->index_buffer->resource == resource)
5135 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5136 state->index_buffer = NULL;
5138 break;
5140 default:
5141 break;
5144 /* Remove the resource from the resourceStore */
5145 device_resource_remove(device, resource);
5147 TRACE("Resource released.\n");
5150 static int wined3d_so_desc_compare(const void *key, const struct wine_rb_entry *entry)
5152 const struct wined3d_stream_output_desc *desc = &WINE_RB_ENTRY_VALUE(entry,
5153 struct wined3d_so_desc_entry, entry)->desc;
5154 const struct wined3d_stream_output_desc *k = key;
5155 unsigned int i;
5156 int ret;
5158 if ((ret = wined3d_uint32_compare(k->element_count, desc->element_count)))
5159 return ret;
5160 if ((ret = wined3d_uint32_compare(k->buffer_stride_count, desc->buffer_stride_count)))
5161 return ret;
5162 if ((ret = wined3d_uint32_compare(k->rasterizer_stream_idx, desc->rasterizer_stream_idx)))
5163 return ret;
5165 for (i = 0; i < k->element_count; ++i)
5167 const struct wined3d_stream_output_element *b = &desc->elements[i];
5168 const struct wined3d_stream_output_element *a = &k->elements[i];
5170 if ((ret = wined3d_uint32_compare(a->stream_idx, b->stream_idx)))
5171 return ret;
5172 if ((ret = (!a->semantic_name - !b->semantic_name)))
5173 return ret;
5174 if (a->semantic_name && (ret = strcmp(a->semantic_name, b->semantic_name)))
5175 return ret;
5176 if ((ret = wined3d_uint32_compare(a->semantic_idx, b->semantic_idx)))
5177 return ret;
5178 if ((ret = wined3d_uint32_compare(a->component_idx, b->component_idx)))
5179 return ret;
5180 if ((ret = wined3d_uint32_compare(a->component_count, b->component_count)))
5181 return ret;
5182 if ((ret = wined3d_uint32_compare(a->output_slot, b->output_slot)))
5183 return ret;
5186 for (i = 0; i < k->buffer_stride_count; ++i)
5188 if ((ret = wined3d_uint32_compare(k->buffer_strides[i], desc->buffer_strides[i])))
5189 return ret;
5192 return 0;
5195 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5197 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5199 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5202 static int wined3d_rasterizer_state_compare(const void *key, const struct wine_rb_entry *entry)
5204 const struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
5206 return memcmp(&state->desc, key, sizeof(state->desc));
5209 static int wined3d_blend_state_compare(const void *key, const struct wine_rb_entry *entry)
5211 const struct wined3d_blend_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
5213 return memcmp(&state->desc, key, sizeof(state->desc));
5216 static int wined3d_depth_stencil_state_compare(const void *key, const struct wine_rb_entry *entry)
5218 const struct wined3d_depth_stencil_state *state
5219 = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
5221 return memcmp(&state->desc, key, sizeof(state->desc));
5224 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
5225 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
5226 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
5227 const BOOL *supported_extensions, struct wined3d_device_parent *device_parent)
5229 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
5230 const struct wined3d_fragment_pipe_ops *fragment_pipeline;
5231 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5232 unsigned int i;
5233 HRESULT hr;
5235 device->ref = 1;
5236 device->wined3d = wined3d;
5237 wined3d_incref(device->wined3d);
5238 device->adapter = adapter;
5239 device->device_parent = device_parent;
5240 list_init(&device->resources);
5241 list_init(&device->shaders);
5242 device->surface_alignment = surface_alignment;
5244 /* Save the creation parameters. */
5245 device->create_parms.adapter_idx = adapter_idx;
5246 device->create_parms.device_type = device_type;
5247 device->create_parms.focus_window = focus_window;
5248 device->create_parms.flags = flags;
5250 device->shader_backend = adapter->shader_backend;
5252 vertex_pipeline = adapter->vertex_pipe;
5254 fragment_pipeline = adapter->fragment_pipe;
5256 wine_rb_init(&device->so_descs, wined3d_so_desc_compare);
5257 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5258 wine_rb_init(&device->rasterizer_states, wined3d_rasterizer_state_compare);
5259 wine_rb_init(&device->blend_states, wined3d_blend_state_compare);
5260 wine_rb_init(&device->depth_stencil_states, wined3d_depth_stencil_state_compare);
5262 if (vertex_pipeline->vp_states && fragment_pipeline->states
5263 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
5264 &adapter->d3d_info, supported_extensions, vertex_pipeline,
5265 fragment_pipeline, adapter->misc_state_template)))
5267 ERR("Failed to compile state table, hr %#lx.\n", hr);
5268 wine_rb_destroy(&device->samplers, NULL, NULL);
5269 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5270 wine_rb_destroy(&device->blend_states, NULL, NULL);
5271 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5272 wine_rb_destroy(&device->so_descs, NULL, NULL);
5273 wined3d_decref(device->wined3d);
5274 return hr;
5277 device->max_frame_latency = 3;
5279 if (!(device->cs = wined3d_cs_create(device, levels, level_count)))
5281 WARN("Failed to create command stream.\n");
5282 hr = E_FAIL;
5283 goto err;
5286 wined3d_lock_init(&device->bo_map_lock, "wined3d_device.bo_map_lock");
5288 return WINED3D_OK;
5290 err:
5291 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
5293 heap_free(device->multistate_funcs[i]);
5295 wine_rb_destroy(&device->samplers, NULL, NULL);
5296 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5297 wine_rb_destroy(&device->blend_states, NULL, NULL);
5298 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5299 wine_rb_destroy(&device->so_descs, NULL, NULL);
5300 wined3d_decref(device->wined3d);
5301 return hr;
5304 void device_invalidate_state(const struct wined3d_device *device, unsigned int state_id)
5306 unsigned int representative, i, idx, shift;
5307 struct wined3d_context *context;
5309 wined3d_from_cs(device->cs);
5311 if (STATE_IS_COMPUTE(state_id))
5313 for (i = 0; i < device->context_count; ++i)
5314 context_invalidate_compute_state(device->contexts[i], state_id);
5315 return;
5318 representative = device->state_table[state_id].representative;
5319 idx = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
5320 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
5321 for (i = 0; i < device->context_count; ++i)
5323 device->contexts[i]->dirty_graphics_states[idx] |= (1u << shift);
5327 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5328 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5330 if (message == WM_DESTROY)
5332 TRACE("unregister window %p.\n", window);
5333 wined3d_unregister_window(window);
5335 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5336 ERR("Window %p is not the focus window for device %p.\n", window, device);
5338 else if (message == WM_DISPLAYCHANGE)
5340 device->device_parent->ops->mode_changed(device->device_parent);
5342 else if (message == WM_ACTIVATEAPP)
5344 unsigned int i = device->swapchain_count;
5346 /* Deactivating the implicit swapchain may cause the application
5347 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
5348 * deactivate the implicit swapchain last, and to avoid accessing the
5349 * "device" pointer afterwards. */
5350 while (i--)
5351 wined3d_swapchain_activate(device->swapchains[i], wparam);
5353 else if (message == WM_SYSCOMMAND)
5355 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5357 if (unicode)
5358 DefWindowProcW(window, message, wparam, lparam);
5359 else
5360 DefWindowProcA(window, message, wparam, lparam);
5364 if (unicode)
5365 return CallWindowProcW(proc, window, message, wparam, lparam);
5366 else
5367 return CallWindowProcA(proc, window, message, wparam, lparam);