dinput: Introduce new dinput_device_internal_unacquire helper.
[wine.git] / dlls / wined3d / device.c
blob436c4dfe8542d667f2144e29fbff347cf38519aa
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #ifdef HAVE_FLOAT_H
31 # include <float.h>
32 #endif
34 #include "wined3d_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
37 WINE_DECLARE_DEBUG_CHANNEL(winediag);
39 struct wined3d_matrix_3x3
41 float _11, _12, _13;
42 float _21, _22, _23;
43 float _31, _32, _33;
46 struct light_transformed
48 struct wined3d_color diffuse, specular, ambient;
49 struct wined3d_vec4 position;
50 struct wined3d_vec3 direction;
51 float range, falloff, c_att, l_att, q_att, cos_htheta, cos_hphi;
54 struct lights_settings
56 struct light_transformed lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
57 struct wined3d_color ambient_light;
58 struct wined3d_matrix modelview_matrix;
59 struct wined3d_matrix_3x3 normal_matrix;
60 struct wined3d_vec4 position_transformed;
62 float fog_start, fog_end, fog_density;
64 uint32_t point_light_count : 8;
65 uint32_t spot_light_count : 8;
66 uint32_t directional_light_count : 8;
67 uint32_t parallel_point_light_count : 8;
68 uint32_t lighting : 1;
69 uint32_t legacy_lighting : 1;
70 uint32_t normalise : 1;
71 uint32_t localviewer : 1;
72 uint32_t fog_coord_mode : 2;
73 uint32_t fog_mode : 2;
74 uint32_t padding : 24;
77 /* Define the default light parameters as specified by MSDN. */
78 const struct wined3d_light WINED3D_default_light =
80 WINED3D_LIGHT_DIRECTIONAL, /* Type */
81 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
82 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
83 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
84 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
85 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
86 0.0f, /* Range */
87 0.0f, /* Falloff */
88 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
89 0.0f, /* Theta */
90 0.0f /* Phi */
93 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
95 struct wined3d_context **new_array;
97 TRACE("Adding context %p.\n", context);
99 if (!device->shader_backend->shader_allocate_context_data(context))
101 ERR("Failed to allocate shader backend context data.\n");
102 return FALSE;
104 device->shader_backend->shader_init_context_state(context);
106 if (!device->adapter->fragment_pipe->allocate_context_data(context))
108 ERR("Failed to allocate fragment pipeline context data.\n");
109 device->shader_backend->shader_free_context_data(context);
110 return FALSE;
113 if (!(new_array = heap_realloc(device->contexts, sizeof(*new_array) * (device->context_count + 1))))
115 ERR("Failed to grow the context array.\n");
116 device->adapter->fragment_pipe->free_context_data(context);
117 device->shader_backend->shader_free_context_data(context);
118 return FALSE;
121 new_array[device->context_count++] = context;
122 device->contexts = new_array;
124 return TRUE;
127 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
129 struct wined3d_context **new_array;
130 BOOL found = FALSE;
131 UINT i;
133 TRACE("Removing context %p.\n", context);
135 device->adapter->fragment_pipe->free_context_data(context);
136 device->shader_backend->shader_free_context_data(context);
138 for (i = 0; i < device->context_count; ++i)
140 if (device->contexts[i] == context)
142 found = TRUE;
143 break;
147 if (!found)
149 ERR("Context %p doesn't exist in context array.\n", context);
150 return;
153 if (!--device->context_count)
155 heap_free(device->contexts);
156 device->contexts = NULL;
157 return;
160 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
161 if (!(new_array = heap_realloc(device->contexts, device->context_count * sizeof(*device->contexts))))
163 ERR("Failed to shrink context array. Oh well.\n");
164 return;
167 device->contexts = new_array;
170 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
172 ULONG refcount = InterlockedIncrement(&device->ref);
174 TRACE("%p increasing refcount to %u.\n", device, refcount);
176 return refcount;
179 static void device_free_so_desc(struct wine_rb_entry *entry, void *context)
181 struct wined3d_so_desc_entry *s = WINE_RB_ENTRY_VALUE(entry, struct wined3d_so_desc_entry, entry);
183 heap_free(s);
186 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
188 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
190 ERR("Leftover sampler %p.\n", sampler);
193 static void device_leftover_rasterizer_state(struct wine_rb_entry *entry, void *context)
195 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
197 ERR("Leftover rasterizer state %p.\n", state);
200 static void device_leftover_blend_state(struct wine_rb_entry *entry, void *context)
202 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
204 ERR("Leftover blend state %p.\n", blend_state);
207 static void device_leftover_depth_stencil_state(struct wine_rb_entry *entry, void *context)
209 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
211 ERR("Leftover depth/stencil state %p.\n", state);
214 void wined3d_device_cleanup(struct wined3d_device *device)
216 unsigned int i;
218 if (device->swapchain_count)
219 wined3d_device_uninit_3d(device);
221 wined3d_cs_destroy(device->cs);
223 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
225 heap_free(device->multistate_funcs[i]);
226 device->multistate_funcs[i] = NULL;
229 if (!list_empty(&device->resources))
231 struct wined3d_resource *resource;
233 ERR("Device released with resources still bound.\n");
235 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
237 ERR("Leftover resource %p with type %s (%#x).\n",
238 resource, debug_d3dresourcetype(resource->type), resource->type);
242 if (device->contexts)
243 ERR("Context array not freed!\n");
244 if (device->hardwareCursor)
245 DestroyCursor(device->hardwareCursor);
246 device->hardwareCursor = 0;
248 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
249 wine_rb_destroy(&device->rasterizer_states, device_leftover_rasterizer_state, NULL);
250 wine_rb_destroy(&device->blend_states, device_leftover_blend_state, NULL);
251 wine_rb_destroy(&device->depth_stencil_states, device_leftover_depth_stencil_state, NULL);
252 wine_rb_destroy(&device->so_descs, device_free_so_desc, NULL);
254 wined3d_decref(device->wined3d);
255 device->wined3d = NULL;
258 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
260 ULONG refcount = InterlockedDecrement(&device->ref);
262 TRACE("%p decreasing refcount to %u.\n", device, refcount);
264 if (!refcount)
266 device->adapter->adapter_ops->adapter_destroy_device(device);
267 TRACE("Destroyed device %p.\n", device);
270 return refcount;
273 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
275 TRACE("device %p.\n", device);
277 return device->swapchain_count;
280 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
282 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
284 if (swapchain_idx >= device->swapchain_count)
286 WARN("swapchain_idx %u >= swapchain_count %u.\n",
287 swapchain_idx, device->swapchain_count);
288 return NULL;
291 return device->swapchains[swapchain_idx];
294 static void device_load_logo(struct wined3d_device *device, const char *filename)
296 struct wined3d_color_key color_key;
297 struct wined3d_resource_desc desc;
298 HBITMAP hbm;
299 BITMAP bm;
300 HRESULT hr;
301 HDC dcb = NULL, dcs = NULL;
303 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
305 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
306 return;
308 GetObjectA(hbm, sizeof(BITMAP), &bm);
310 if (!(dcb = CreateCompatibleDC(NULL)))
311 goto out;
312 SelectObject(dcb, hbm);
314 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
315 desc.format = WINED3DFMT_B5G6R5_UNORM;
316 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
317 desc.multisample_quality = 0;
318 desc.usage = WINED3DUSAGE_DYNAMIC;
319 desc.bind_flags = 0;
320 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
321 desc.width = bm.bmWidth;
322 desc.height = bm.bmHeight;
323 desc.depth = 1;
324 desc.size = 0;
325 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_GET_DC,
326 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
328 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
329 goto out;
332 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
334 wined3d_texture_decref(device->logo_texture);
335 device->logo_texture = NULL;
336 goto out;
338 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
339 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
341 color_key.color_space_low_value = 0;
342 color_key.color_space_high_value = 0;
343 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
345 out:
346 if (dcb) DeleteDC(dcb);
347 if (hbm) DeleteObject(hbm);
350 /* Context activation is done by the caller. */
351 static void wined3d_device_gl_create_dummy_textures(struct wined3d_device_gl *device_gl,
352 struct wined3d_context_gl *context_gl)
354 struct wined3d_dummy_textures *textures = &device_gl->dummy_textures;
355 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
356 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
357 unsigned int i;
358 DWORD color;
360 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
361 color = 0x000000ff;
362 else
363 color = 0x00000000;
365 /* Under DirectX you can sample even if no texture is bound, whereas
366 * OpenGL will only allow that when a valid texture is bound.
367 * We emulate this by creating dummy textures and binding them
368 * to each texture stage when the currently set D3D texture is NULL. */
369 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
371 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
372 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
373 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
374 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
375 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
377 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
378 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
379 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
380 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
381 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
383 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
385 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
386 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
387 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
388 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
389 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
392 if (gl_info->supported[EXT_TEXTURE3D])
394 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
395 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
396 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
397 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
398 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
401 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
403 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
404 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
405 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
406 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
408 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
409 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
413 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
415 DWORD cube_array_data[6];
417 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
418 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
419 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
420 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
421 cube_array_data[i] = color;
422 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
423 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
426 if (gl_info->supported[EXT_TEXTURE_ARRAY])
428 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
429 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
430 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
431 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
432 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
434 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
435 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
436 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
437 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
438 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
441 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
443 GLuint buffer;
445 GL_EXTCALL(glGenBuffers(1, &buffer));
446 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
447 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
448 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
450 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
451 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
452 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
453 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
454 GL_EXTCALL(glDeleteBuffers(1, &buffer));
457 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
459 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
460 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
461 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
462 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
464 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
465 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
466 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
467 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
469 if (gl_info->supported[ARB_CLEAR_TEXTURE])
471 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
472 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
474 else
476 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
480 checkGLcall("create dummy textures");
482 wined3d_context_gl_bind_dummy_textures(context_gl);
485 /* Context activation is done by the caller. */
486 static void wined3d_device_gl_destroy_dummy_textures(struct wined3d_device_gl *device_gl,
487 struct wined3d_context_gl *context_gl)
489 struct wined3d_dummy_textures *dummy_textures = &device_gl->dummy_textures;
490 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
492 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
494 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
495 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
498 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
499 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
501 if (gl_info->supported[EXT_TEXTURE_ARRAY])
503 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
504 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
507 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
508 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
510 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
511 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
513 if (gl_info->supported[EXT_TEXTURE3D])
514 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
516 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
517 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
519 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
520 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
522 checkGLcall("delete dummy textures");
524 memset(dummy_textures, 0, sizeof(*dummy_textures));
527 /* Context activation is done by the caller. */
528 void wined3d_device_create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
530 struct wined3d_sampler_desc desc;
531 HRESULT hr;
533 desc.address_u = WINED3D_TADDRESS_WRAP;
534 desc.address_v = WINED3D_TADDRESS_WRAP;
535 desc.address_w = WINED3D_TADDRESS_WRAP;
536 memset(desc.border_color, 0, sizeof(desc.border_color));
537 desc.mag_filter = WINED3D_TEXF_POINT;
538 desc.min_filter = WINED3D_TEXF_POINT;
539 desc.mip_filter = WINED3D_TEXF_NONE;
540 desc.lod_bias = 0.0f;
541 desc.min_lod = -1000.0f;
542 desc.max_lod = 1000.0f;
543 desc.mip_base_level = 0;
544 desc.max_anisotropy = 1;
545 desc.compare = FALSE;
546 desc.comparison_func = WINED3D_CMP_NEVER;
547 desc.srgb_decode = TRUE;
549 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
550 * instructions allow access to resources without using samplers.
551 * In GLSL, resources are always accessed through sampler or image variables. The default
552 * sampler object is used to emulate the direct resource access when there is no sampler state
553 * to use.
555 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
557 ERR("Failed to create default sampler, hr %#x.\n", hr);
558 device->default_sampler = NULL;
561 /* In D3D10+, a NULL sampler maps to the default sampler state. */
562 desc.address_u = WINED3D_TADDRESS_CLAMP;
563 desc.address_v = WINED3D_TADDRESS_CLAMP;
564 desc.address_w = WINED3D_TADDRESS_CLAMP;
565 desc.mag_filter = WINED3D_TEXF_LINEAR;
566 desc.min_filter = WINED3D_TEXF_LINEAR;
567 desc.mip_filter = WINED3D_TEXF_LINEAR;
568 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
570 ERR("Failed to create null sampler, hr %#x.\n", hr);
571 device->null_sampler = NULL;
575 void wined3d_device_destroy_default_samplers(struct wined3d_device *device)
577 wined3d_sampler_decref(device->default_sampler);
578 device->default_sampler = NULL;
579 wined3d_sampler_decref(device->null_sampler);
580 device->null_sampler = NULL;
583 static bool wined3d_null_image_vk_init(struct wined3d_image_vk *image, struct wined3d_context_vk *context_vk,
584 VkCommandBuffer vk_command_buffer, VkImageType type, unsigned int layer_count, unsigned int sample_count)
586 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
587 VkImageSubresourceRange range;
588 uint32_t flags = 0;
590 static const VkClearColorValue colour = {{0}};
592 TRACE("image %p, context_vk %p, vk_command_buffer %p, type %#x, layer_count %u, sample_count %u.\n",
593 image, context_vk, vk_command_buffer, type, layer_count, sample_count);
595 if (type == VK_IMAGE_TYPE_2D && layer_count >= 6)
596 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
598 if (!wined3d_context_vk_create_image(context_vk, type,
599 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_FORMAT_R8G8B8A8_UNORM,
600 1, 1, 1, sample_count, 1, layer_count, flags, image))
602 return false;
605 wined3d_context_vk_reference_image(context_vk, image);
607 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
608 range.baseMipLevel = 0;
609 range.levelCount = 1;
610 range.baseArrayLayer = 0;
611 range.layerCount = layer_count;
613 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
614 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
615 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, image->vk_image, &range);
617 VK_CALL(vkCmdClearColorImage(vk_command_buffer, image->vk_image,
618 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour, 1, &range));
620 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
621 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0,
622 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, image->vk_image, &range);
624 TRACE("Created NULL image 0x%s, memory 0x%s.\n",
625 wine_dbgstr_longlong(image->vk_image), wine_dbgstr_longlong(image->vk_memory));
627 return true;
630 bool wined3d_device_vk_create_null_resources(struct wined3d_device_vk *device_vk,
631 struct wined3d_context_vk *context_vk)
633 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
634 const struct wined3d_vk_info *vk_info;
635 const struct wined3d_format *format;
636 VkMemoryPropertyFlags memory_type;
637 VkCommandBuffer vk_command_buffer;
638 unsigned int sample_count = 2;
639 VkBufferUsageFlags usage;
641 format = wined3d_get_format(device_vk->d.adapter, WINED3DFMT_R8G8B8A8_UNORM, WINED3D_BIND_SHADER_RESOURCE);
642 while (sample_count && !(sample_count & format->multisample_types))
643 sample_count <<= 1;
645 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
647 ERR("Failed to get command buffer.\n");
648 return false;
651 vk_info = context_vk->vk_info;
653 usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
654 | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
655 memory_type = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
656 if (!wined3d_context_vk_create_bo(context_vk, 16, usage, memory_type, &r->bo))
657 return false;
658 VK_CALL(vkCmdFillBuffer(vk_command_buffer, r->bo.vk_buffer, r->bo.buffer_offset, r->bo.size, 0x00000000u));
659 r->buffer_info.buffer = r->bo.vk_buffer;
660 r->buffer_info.offset = r->bo.buffer_offset;
661 r->buffer_info.range = r->bo.size;
663 if (!wined3d_null_image_vk_init(&r->image_1d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_1D, 1, 1))
665 ERR("Failed to create 1D image.\n");
666 goto fail;
669 if (!wined3d_null_image_vk_init(&r->image_2d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 6, 1))
671 ERR("Failed to create 2D image.\n");
672 goto fail;
675 if (!wined3d_null_image_vk_init(&r->image_2dms, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 1, sample_count))
677 ERR("Failed to create 2D MSAA image.\n");
678 goto fail;
681 if (!wined3d_null_image_vk_init(&r->image_3d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_3D, 1, 1))
683 ERR("Failed to create 3D image.\n");
684 goto fail;
687 return true;
689 fail:
690 if (r->image_2dms.vk_image)
691 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
692 if (r->image_2d.vk_image)
693 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
694 if (r->image_1d.vk_image)
695 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
696 wined3d_context_vk_reference_bo(context_vk, &r->bo);
697 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
698 return false;
701 void wined3d_device_vk_destroy_null_resources(struct wined3d_device_vk *device_vk,
702 struct wined3d_context_vk *context_vk)
704 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
706 /* We don't track command buffer references to NULL resources. We easily
707 * could, but it doesn't seem worth it. */
708 wined3d_context_vk_reference_image(context_vk, &r->image_3d);
709 wined3d_context_vk_destroy_image(context_vk, &r->image_3d);
710 wined3d_context_vk_reference_image(context_vk, &r->image_2dms);
711 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
712 wined3d_context_vk_reference_image(context_vk, &r->image_2d);
713 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
714 wined3d_context_vk_reference_image(context_vk, &r->image_1d);
715 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
716 wined3d_context_vk_reference_bo(context_vk, &r->bo);
717 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
720 bool wined3d_device_vk_create_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
722 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
723 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
724 VkBufferViewCreateInfo buffer_create_info;
725 const struct wined3d_vk_info *vk_info;
726 VkImageViewCreateInfo view_desc;
727 VkResult vr;
729 vk_info = context_vk->vk_info;
731 buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
732 buffer_create_info.pNext = NULL;
733 buffer_create_info.flags = 0;
734 buffer_create_info.buffer = r->bo.vk_buffer;
735 buffer_create_info.format = VK_FORMAT_R32_UINT;
736 buffer_create_info.offset = r->bo.buffer_offset;
737 buffer_create_info.range = r->bo.size;
739 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
740 &buffer_create_info, NULL, &v->vk_view_buffer_uint))) < 0)
742 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
743 return false;
745 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_uint));
747 buffer_create_info.format = VK_FORMAT_R32G32B32A32_SFLOAT;
748 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
749 &buffer_create_info, NULL, &v->vk_view_buffer_float))) < 0)
751 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
752 goto fail;
754 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_float));
756 view_desc.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
757 view_desc.pNext = NULL;
758 view_desc.flags = 0;
759 view_desc.image = r->image_1d.vk_image;
760 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D;
761 view_desc.format = VK_FORMAT_R8G8B8A8_UNORM;
762 view_desc.components.r = VK_COMPONENT_SWIZZLE_ZERO;
763 view_desc.components.g = VK_COMPONENT_SWIZZLE_ZERO;
764 view_desc.components.b = VK_COMPONENT_SWIZZLE_ZERO;
765 view_desc.components.a = VK_COMPONENT_SWIZZLE_ZERO;
766 view_desc.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
767 view_desc.subresourceRange.baseMipLevel = 0;
768 view_desc.subresourceRange.levelCount = 1;
769 view_desc.subresourceRange.baseArrayLayer = 0;
770 view_desc.subresourceRange.layerCount = 1;
771 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d.imageView))) < 0)
773 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
774 goto fail;
776 v->vk_info_1d.sampler = VK_NULL_HANDLE;
777 v->vk_info_1d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
778 TRACE("Created 1D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d.imageView));
780 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D_ARRAY;
781 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d_array.imageView))) < 0)
783 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
784 goto fail;
786 v->vk_info_1d_array.sampler = VK_NULL_HANDLE;
787 v->vk_info_1d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
788 TRACE("Created 1D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d_array.imageView));
790 view_desc.image = r->image_2d.vk_image;
791 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
792 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d.imageView))) < 0)
794 ERR("Failed to create 2D image view, vr %s.\n", wined3d_debug_vkresult(vr));
795 goto fail;
797 v->vk_info_2d.sampler = VK_NULL_HANDLE;
798 v->vk_info_2d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
799 TRACE("Created 2D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d.imageView));
801 view_desc.image = r->image_2dms.vk_image;
802 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
803 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms.imageView))) < 0)
805 ERR("Failed to create 2D MSAA image view, vr %s.\n", wined3d_debug_vkresult(vr));
806 goto fail;
808 v->vk_info_2dms.sampler = VK_NULL_HANDLE;
809 v->vk_info_2dms.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
810 TRACE("Created 2D MSAA image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms.imageView));
812 view_desc.image = r->image_3d.vk_image;
813 view_desc.viewType = VK_IMAGE_VIEW_TYPE_3D;
814 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_3d.imageView))) < 0)
816 ERR("Failed to create 3D image view, vr %s.\n", wined3d_debug_vkresult(vr));
817 goto fail;
819 v->vk_info_3d.sampler = VK_NULL_HANDLE;
820 v->vk_info_3d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
821 TRACE("Created 3D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_3d.imageView));
823 view_desc.image = r->image_2d.vk_image;
824 view_desc.subresourceRange.layerCount = 6;
825 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
826 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube.imageView))) < 0)
828 ERR("Failed to create cube image view, vr %s.\n", wined3d_debug_vkresult(vr));
829 goto fail;
831 v->vk_info_cube.sampler = VK_NULL_HANDLE;
832 v->vk_info_cube.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
833 TRACE("Created cube image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube.imageView));
835 view_desc.subresourceRange.layerCount = 1;
836 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
837 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d_array.imageView))) < 0)
839 ERR("Failed to create 2D array image view, vr %s.\n", wined3d_debug_vkresult(vr));
840 goto fail;
842 v->vk_info_2d_array.sampler = VK_NULL_HANDLE;
843 v->vk_info_2d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
844 TRACE("Created 2D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d_array.imageView));
846 view_desc.image = r->image_2dms.vk_image;
847 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
848 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms_array.imageView))) < 0)
850 ERR("Failed to create 2D MSAA array image view, vr %s.\n", wined3d_debug_vkresult(vr));
851 goto fail;
853 v->vk_info_2dms_array.sampler = VK_NULL_HANDLE;
854 v->vk_info_2dms_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
855 TRACE("Created 2D MSAA array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms_array.imageView));
857 view_desc.image = r->image_2d.vk_image;
858 view_desc.subresourceRange.layerCount = 6;
859 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
860 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube_array.imageView))) < 0)
862 ERR("Failed to create cube array image view, vr %s.\n", wined3d_debug_vkresult(vr));
863 goto fail;
865 v->vk_info_cube_array.sampler = VK_NULL_HANDLE;
866 v->vk_info_cube_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
867 TRACE("Created cube array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube_array.imageView));
869 return true;
871 fail:
872 if (v->vk_info_cube_array.imageView)
873 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube_array.imageView, NULL));
874 if (v->vk_info_2d_array.imageView)
875 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d_array.imageView, NULL));
876 if (v->vk_info_cube.imageView)
877 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube.imageView, NULL));
878 if (v->vk_info_3d.imageView)
879 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_3d.imageView, NULL));
880 if (v->vk_info_2dms.imageView)
881 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2dms.imageView, NULL));
882 if (v->vk_info_2d.imageView)
883 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d.imageView, NULL));
884 if (v->vk_info_1d_array.imageView)
885 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d_array.imageView, NULL));
886 if (v->vk_info_1d.imageView)
887 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d.imageView, NULL));
888 if (v->vk_view_buffer_float)
889 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_float, NULL));
890 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_uint, NULL));
891 return false;
894 void wined3d_device_vk_destroy_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
896 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
897 uint64_t id = context_vk->current_command_buffer.id;
899 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube_array.imageView, id);
900 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms_array.imageView, id);
901 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d_array.imageView, id);
902 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube.imageView, id);
903 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_3d.imageView, id);
904 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms.imageView, id);
905 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d.imageView, id);
906 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d_array.imageView, id);
907 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d.imageView, id);
909 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_float, id);
910 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_uint, id);
913 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
915 unsigned int screensaver_active;
917 TRACE("device %p, window %p.\n", device, window);
919 if (!wined3d_register_window(NULL, window, device, 0))
921 ERR("Failed to register window %p.\n", window);
922 return E_FAIL;
925 InterlockedExchangePointer((void **)&device->focus_window, window);
926 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
927 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
928 if ((device->restore_screensaver = !!screensaver_active))
929 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
931 return WINED3D_OK;
934 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
936 TRACE("device %p.\n", device);
938 if (device->focus_window) wined3d_unregister_window(device->focus_window);
939 InterlockedExchangePointer((void **)&device->focus_window, NULL);
940 if (device->restore_screensaver)
942 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
943 device->restore_screensaver = FALSE;
947 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
949 struct wined3d_rendertarget_view *views[WINED3D_MAX_RENDER_TARGETS] = {0};
950 BOOL ds_enable = swapchain->state.desc.enable_auto_depth_stencil;
951 struct wined3d_device_context *context = &device->cs->c;
953 if (device->back_buffer_view)
954 views[0] = device->back_buffer_view;
955 wined3d_device_context_set_rendertarget_views(context, 0,
956 device->adapter->d3d_info.limits.max_rt_count, views, !!device->back_buffer_view);
958 wined3d_device_context_set_depth_stencil_view(context, ds_enable ? device->auto_depth_stencil_view : NULL);
961 void wined3d_device_delete_opengl_contexts_cs(void *object)
963 struct wined3d_swapchain_gl *swapchain_gl;
964 struct wined3d_device *device = object;
965 struct wined3d_context_gl *context_gl;
966 struct wined3d_device_gl *device_gl;
967 struct wined3d_context *context;
968 struct wined3d_shader *shader;
970 TRACE("device %p.\n", device);
972 device_gl = wined3d_device_gl(device);
974 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
976 device->shader_backend->shader_destroy(shader);
979 context = context_acquire(device, NULL, 0);
980 context_gl = wined3d_context_gl(context);
981 device->blitter->ops->blitter_destroy(device->blitter, context);
982 device->shader_backend->shader_free_private(device, context);
983 wined3d_device_gl_destroy_dummy_textures(device_gl, context_gl);
984 context_release(context);
986 while (device->context_count)
988 if ((swapchain_gl = wined3d_swapchain_gl(device->contexts[0]->swapchain)))
989 wined3d_swapchain_gl_destroy_contexts(swapchain_gl);
990 else
991 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
995 void wined3d_device_create_primary_opengl_context_cs(void *object)
997 struct wined3d_device *device = object;
998 struct wined3d_context_gl *context_gl;
999 struct wined3d_swapchain *swapchain;
1000 struct wined3d_context *context;
1001 struct wined3d_texture *target;
1002 HRESULT hr;
1004 TRACE("device %p.\n", device);
1006 swapchain = device->swapchains[0];
1007 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1008 if (!(context = context_acquire(device, target, 0)))
1010 WARN("Failed to acquire context.\n");
1011 return;
1014 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1015 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1017 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1018 context_release(context);
1019 return;
1022 if (!(device->blitter = wined3d_cpu_blitter_create()))
1024 ERR("Failed to create CPU blitter.\n");
1025 device->shader_backend->shader_free_private(device, NULL);
1026 context_release(context);
1027 return;
1030 context_gl = wined3d_context_gl(context);
1032 wined3d_ffp_blitter_create(&device->blitter, context_gl->gl_info);
1033 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1034 wined3d_arbfp_blitter_create(&device->blitter, device);
1035 wined3d_fbo_blitter_create(&device->blitter, context_gl->gl_info);
1036 wined3d_raw_blitter_create(&device->blitter, context_gl->gl_info);
1038 wined3d_device_gl_create_dummy_textures(wined3d_device_gl(device), context_gl);
1039 wined3d_device_create_default_samplers(device, context);
1040 context_release(context);
1043 HRESULT wined3d_device_set_implicit_swapchain(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1045 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1046 const struct wined3d_swapchain_desc *swapchain_desc;
1047 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
1048 DWORD clear_flags = 0;
1049 unsigned int i;
1050 HRESULT hr;
1052 TRACE("device %p, swapchain %p.\n", device, swapchain);
1054 if (device->d3d_initialized)
1055 return WINED3DERR_INVALIDCALL;
1057 device->swapchain_count = 1;
1058 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1060 ERR("Failed to allocate swapchain array.\n");
1061 hr = E_OUTOFMEMORY;
1062 goto err_out;
1064 device->swapchains[0] = swapchain;
1066 for (i = 0; i < ARRAY_SIZE(fb->render_targets); ++i)
1068 if (fb->render_targets[i])
1069 wined3d_rtv_bind_count_dec(fb->render_targets[i]);
1071 memset(fb->render_targets, 0, sizeof(fb->render_targets));
1073 if (FAILED(hr = device->adapter->adapter_ops->adapter_init_3d(device)))
1074 goto err_out;
1075 device->d3d_initialized = TRUE;
1077 swapchain_desc = &swapchain->state.desc;
1078 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1080 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1081 struct wined3d_view_desc view_desc;
1083 view_desc.format_id = back_buffer->format->id;
1084 view_desc.flags = 0;
1085 view_desc.u.texture.level_idx = 0;
1086 view_desc.u.texture.level_count = 1;
1087 view_desc.u.texture.layer_idx = 0;
1088 view_desc.u.texture.layer_count = 1;
1089 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1090 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1092 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1093 device->adapter->adapter_ops->adapter_uninit_3d(device);
1094 device->d3d_initialized = FALSE;
1095 goto err_out;
1099 device_init_swapchain_state(device, swapchain);
1101 TRACE("All defaults now set up.\n");
1103 /* Clear the screen. */
1104 if (device->back_buffer_view)
1105 clear_flags |= WINED3DCLEAR_TARGET;
1106 if (swapchain_desc->enable_auto_depth_stencil)
1107 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1108 if (clear_flags)
1109 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1111 if (wined3d_settings.logo)
1112 device_load_logo(device, wined3d_settings.logo);
1114 return WINED3D_OK;
1116 err_out:
1117 heap_free(device->swapchains);
1118 device->swapchains = NULL;
1119 device->swapchain_count = 0;
1121 return hr;
1124 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1126 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1128 wined3d_sampler_decref(sampler);
1131 static void device_free_rasterizer_state(struct wine_rb_entry *entry, void *context)
1133 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
1135 wined3d_rasterizer_state_decref(state);
1138 static void device_free_blend_state(struct wine_rb_entry *entry, void *context)
1140 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
1142 wined3d_blend_state_decref(blend_state);
1145 static void device_free_depth_stencil_state(struct wine_rb_entry *entry, void *context)
1147 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
1149 wined3d_depth_stencil_state_decref(state);
1152 void wined3d_device_uninit_3d(struct wined3d_device *device)
1154 struct wined3d_state *state = device->cs->c.state;
1155 struct wined3d_resource *resource, *cursor;
1156 struct wined3d_rendertarget_view *view;
1157 struct wined3d_texture *texture;
1159 TRACE("device %p.\n", device);
1161 if (!device->d3d_initialized)
1163 ERR("Called while 3D support was not initialised.\n");
1164 return;
1167 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1169 device->swapchain_count = 0;
1171 if ((texture = device->logo_texture))
1173 device->logo_texture = NULL;
1174 wined3d_texture_decref(texture);
1177 if ((texture = device->cursor_texture))
1179 device->cursor_texture = NULL;
1180 wined3d_texture_decref(texture);
1183 wined3d_device_context_emit_reset_state(&device->cs->c, false);
1184 state_cleanup(state);
1186 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1187 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
1188 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
1189 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
1191 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1193 TRACE("Unloading resource %p.\n", resource);
1194 wined3d_cs_emit_unload_resource(device->cs, resource);
1197 device->adapter->adapter_ops->adapter_uninit_3d(device);
1198 device->d3d_initialized = FALSE;
1200 if ((view = device->auto_depth_stencil_view))
1202 device->auto_depth_stencil_view = NULL;
1203 if (wined3d_rendertarget_view_decref(view))
1204 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1207 if ((view = device->back_buffer_view))
1209 device->back_buffer_view = NULL;
1210 wined3d_rendertarget_view_decref(view);
1213 heap_free(device->swapchains);
1214 device->swapchains = NULL;
1216 wined3d_state_reset(state, &device->adapter->d3d_info);
1219 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1220 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1221 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1223 * There is no way to deactivate thread safety once it is enabled.
1225 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1227 TRACE("device %p.\n", device);
1229 /* For now just store the flag (needed in case of ddraw). */
1230 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1233 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1235 const struct wined3d_driver_info *driver_info;
1237 TRACE("device %p.\n", device);
1239 driver_info = &device->adapter->driver_info;
1241 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1242 wine_dbgstr_longlong(driver_info->vram_bytes),
1243 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1244 wine_dbgstr_longlong(driver_info->vram_bytes - device->adapter->vram_bytes_used));
1246 return min(UINT_MAX, driver_info->vram_bytes - device->adapter->vram_bytes_used);
1249 struct wined3d_buffer * CDECL wined3d_device_context_get_stream_output(struct wined3d_device_context *context,
1250 unsigned int idx, unsigned int *offset)
1252 TRACE("context %p, idx %u, offset %p.\n", context, idx, offset);
1254 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1256 WARN("Invalid stream output %u.\n", idx);
1257 return NULL;
1260 if (offset)
1261 *offset = context->state->stream_output[idx].offset;
1262 return context->state->stream_output[idx].buffer;
1265 HRESULT CDECL wined3d_device_context_get_stream_source(const struct wined3d_device_context *context,
1266 unsigned int stream_idx, struct wined3d_buffer **buffer, unsigned int *offset, unsigned int *stride)
1268 const struct wined3d_stream_state *stream;
1270 TRACE("context %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1271 context, stream_idx, buffer, offset, stride);
1273 if (stream_idx >= WINED3D_MAX_STREAMS)
1275 WARN("Stream index %u out of range.\n", stream_idx);
1276 return WINED3DERR_INVALIDCALL;
1279 stream = &context->state->streams[stream_idx];
1280 *buffer = stream->buffer;
1281 if (offset)
1282 *offset = stream->offset;
1283 *stride = stream->stride;
1285 return WINED3D_OK;
1288 static void wined3d_device_set_transform(struct wined3d_device *device,
1289 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1291 TRACE("device %p, state %s, matrix %p.\n",
1292 device, debug_d3dtstype(state), matrix);
1293 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1294 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1295 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1296 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1298 /* If the new matrix is the same as the current one,
1299 * we cut off any further processing. this seems to be a reasonable
1300 * optimization because as was noticed, some apps (warcraft3 for example)
1301 * tend towards setting the same matrix repeatedly for some reason.
1303 * From here on we assume that the new matrix is different, wherever it matters. */
1304 if (!memcmp(&device->cs->c.state->transforms[state], matrix, sizeof(*matrix)))
1306 TRACE("The application is setting the same matrix over again.\n");
1307 return;
1310 device->cs->c.state->transforms[state] = *matrix;
1311 wined3d_device_context_emit_set_transform(&device->cs->c, state, matrix);
1314 static void wined3d_device_get_transform(const struct wined3d_device *device,
1315 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1317 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1319 *matrix = device->cs->c.state->transforms[state];
1322 /* Note lights are real special cases. Although the device caps state only
1323 * e.g. 8 are supported, you can reference any indexes you want as long as
1324 * that number max are enabled at any one point in time. Therefore since the
1325 * indices can be anything, we need a hashmap of them. However, this causes
1326 * stateblock problems. When capturing the state block, I duplicate the
1327 * hashmap, but when recording, just build a chain pretty much of commands to
1328 * be replayed. */
1329 static void wined3d_device_context_set_light(struct wined3d_device_context *context,
1330 unsigned int light_idx, const struct wined3d_light *light)
1332 struct wined3d_light_info *object = NULL;
1333 float rho;
1335 if (FAILED(wined3d_light_state_set_light(&context->state->light_state, light_idx, light, &object)))
1336 return;
1338 /* Initialize the object. */
1339 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1340 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1341 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1342 light_idx, light->type, debug_color(&light->diffuse),
1343 debug_color(&light->specular), debug_color(&light->ambient),
1344 light->position.x, light->position.y, light->position.z,
1345 light->direction.x, light->direction.y, light->direction.z,
1346 light->range, light->falloff, light->theta, light->phi);
1348 switch (light->type)
1350 case WINED3D_LIGHT_POINT:
1351 /* Position */
1352 object->position.x = light->position.x;
1353 object->position.y = light->position.y;
1354 object->position.z = light->position.z;
1355 object->position.w = 1.0f;
1356 object->cutoff = 180.0f;
1357 /* FIXME: Range */
1358 break;
1360 case WINED3D_LIGHT_DIRECTIONAL:
1361 /* Direction */
1362 object->direction.x = -light->direction.x;
1363 object->direction.y = -light->direction.y;
1364 object->direction.z = -light->direction.z;
1365 object->direction.w = 0.0f;
1366 object->exponent = 0.0f;
1367 object->cutoff = 180.0f;
1368 break;
1370 case WINED3D_LIGHT_SPOT:
1371 /* Position */
1372 object->position.x = light->position.x;
1373 object->position.y = light->position.y;
1374 object->position.z = light->position.z;
1375 object->position.w = 1.0f;
1377 /* Direction */
1378 object->direction.x = light->direction.x;
1379 object->direction.y = light->direction.y;
1380 object->direction.z = light->direction.z;
1381 object->direction.w = 0.0f;
1383 /* opengl-ish and d3d-ish spot lights use too different models
1384 * for the light "intensity" as a function of the angle towards
1385 * the main light direction, so we only can approximate very
1386 * roughly. However, spot lights are rather rarely used in games
1387 * (if ever used at all). Furthermore if still used, probably
1388 * nobody pays attention to such details. */
1389 if (!light->falloff)
1391 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1392 * equations have the falloff resp. exponent parameter as an
1393 * exponent, so the spot light lighting will always be 1.0 for
1394 * both of them, and we don't have to care for the rest of the
1395 * rather complex calculation. */
1396 object->exponent = 0.0f;
1398 else
1400 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1401 if (rho < 0.0001f)
1402 rho = 0.0001f;
1403 object->exponent = -0.3f / logf(cosf(rho / 2));
1406 if (object->exponent > 128.0f)
1407 object->exponent = 128.0f;
1409 object->cutoff = (float)(light->phi * 90 / M_PI);
1410 /* FIXME: Range */
1411 break;
1413 case WINED3D_LIGHT_PARALLELPOINT:
1414 object->position.x = light->position.x;
1415 object->position.y = light->position.y;
1416 object->position.z = light->position.z;
1417 object->position.w = 1.0f;
1418 break;
1420 default:
1421 FIXME("Unrecognized light type %#x.\n", light->type);
1424 wined3d_device_context_emit_set_light(context, object);
1427 static void wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1429 struct wined3d_light_state *light_state = &device->cs->c.state->light_state;
1430 struct wined3d_light_info *light_info;
1432 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1434 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1435 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1437 TRACE("Light enabled requested but light not defined, so defining one!\n");
1438 wined3d_device_context_set_light(&device->cs->c, light_idx, &WINED3D_default_light);
1440 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1442 FIXME("Adding default lights has failed dismally\n");
1443 return;
1447 wined3d_light_state_enable_light(light_state, &device->adapter->d3d_info, light_info, enable);
1448 wined3d_device_context_emit_set_light_enable(&device->cs->c, light_idx, enable);
1451 static HRESULT wined3d_device_set_clip_plane(struct wined3d_device *device,
1452 UINT plane_idx, const struct wined3d_vec4 *plane)
1454 struct wined3d_vec4 *clip_planes = device->cs->c.state->clip_planes;
1456 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1458 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1460 TRACE("Application has requested clipplane this device doesn't support.\n");
1461 return WINED3DERR_INVALIDCALL;
1464 if (!memcmp(&clip_planes[plane_idx], plane, sizeof(*plane)))
1466 TRACE("Application is setting old values over, nothing to do.\n");
1467 return WINED3D_OK;
1470 clip_planes[plane_idx] = *plane;
1472 wined3d_device_context_emit_set_clip_plane(&device->cs->c, plane_idx, plane);
1474 return WINED3D_OK;
1477 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1478 const struct wined3d_clip_status *clip_status)
1480 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1482 if (!clip_status)
1483 return WINED3DERR_INVALIDCALL;
1485 return WINED3D_OK;
1488 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1489 struct wined3d_clip_status *clip_status)
1491 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1493 if (!clip_status)
1494 return WINED3DERR_INVALIDCALL;
1496 return WINED3D_OK;
1499 static void wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1501 TRACE("device %p, material %p.\n", device, material);
1503 device->cs->c.state->material = *material;
1504 wined3d_device_context_emit_set_material(&device->cs->c, material);
1507 struct wined3d_buffer * CDECL wined3d_device_context_get_index_buffer(const struct wined3d_device_context *context,
1508 enum wined3d_format_id *format, unsigned int *offset)
1510 const struct wined3d_state *state = context->state;
1512 TRACE("context %p, format %p, offset %p.\n", context, format, offset);
1514 *format = state->index_format;
1515 if (offset)
1516 *offset = state->index_offset;
1517 return state->index_buffer;
1520 static void wined3d_device_set_base_vertex_index(struct wined3d_device *device, int base_index)
1522 TRACE("device %p, base_index %d.\n", device, base_index);
1524 device->cs->c.state->base_vertex_index = base_index;
1527 void CDECL wined3d_device_context_get_viewports(const struct wined3d_device_context *context,
1528 unsigned int *viewport_count, struct wined3d_viewport *viewports)
1530 const struct wined3d_state *state = context->state;
1531 unsigned int count;
1533 TRACE("context %p, viewport_count %p, viewports %p.\n", context, viewport_count, viewports);
1535 count = viewport_count ? min(*viewport_count, state->viewport_count) : 1;
1536 if (count && viewports)
1537 memcpy(viewports, state->viewports, count * sizeof(*viewports));
1538 if (viewport_count)
1539 *viewport_count = state->viewport_count;
1542 static void resolve_depth_buffer(struct wined3d_device *device)
1544 const struct wined3d_state *state = device->cs->c.state;
1545 struct wined3d_rendertarget_view *src_view;
1546 struct wined3d_resource *dst_resource;
1547 struct wined3d_texture *dst_texture;
1549 if (!(dst_texture = state->textures[0]))
1550 return;
1551 dst_resource = &dst_texture->resource;
1552 if (!dst_resource->format->depth_size)
1553 return;
1554 if (!(src_view = state->fb.depth_stencil))
1555 return;
1557 wined3d_device_context_resolve_sub_resource(&device->cs->c, dst_resource, 0,
1558 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1561 struct wined3d_blend_state * CDECL wined3d_device_context_get_blend_state(const struct wined3d_device_context *context,
1562 struct wined3d_color *blend_factor, unsigned int *sample_mask)
1564 const struct wined3d_state *state = context->state;
1566 TRACE("context %p, blend_factor %p, sample_mask %p.\n", context, blend_factor, sample_mask);
1568 *blend_factor = state->blend_factor;
1569 *sample_mask = state->sample_mask;
1570 return state->blend_state;
1573 struct wined3d_depth_stencil_state * CDECL wined3d_device_context_get_depth_stencil_state(
1574 const struct wined3d_device_context *context, unsigned int *stencil_ref)
1576 const struct wined3d_state *state = context->state;
1578 TRACE("context %p, stencil_ref %p.\n", context, stencil_ref);
1580 *stencil_ref = state->stencil_ref;
1581 return state->depth_stencil_state;
1584 struct wined3d_rasterizer_state * CDECL wined3d_device_context_get_rasterizer_state(
1585 struct wined3d_device_context *context)
1587 TRACE("context %p.\n", context);
1589 return context->state->rasterizer_state;
1592 static void wined3d_device_set_render_state(struct wined3d_device *device,
1593 enum wined3d_render_state state, DWORD value)
1595 if (state > WINEHIGHEST_RENDER_STATE)
1597 WARN("Unhandled render state %#x.\n", state);
1598 return;
1601 if (value == device->cs->c.state->render_states[state])
1602 TRACE("Application is setting the old value over, nothing to do.\n");
1603 else
1605 device->cs->c.state->render_states[state] = value;
1606 wined3d_device_context_emit_set_render_state(&device->cs->c, state, value);
1609 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1611 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1612 resolve_depth_buffer(device);
1616 static void wined3d_device_set_sampler_state(struct wined3d_device *device,
1617 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1619 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1620 device, sampler_idx, debug_d3dsamplerstate(state), value);
1622 if (value == device->cs->c.state->sampler_states[sampler_idx][state])
1624 TRACE("Application is setting the old value over, nothing to do.\n");
1625 return;
1628 device->cs->c.state->sampler_states[sampler_idx][state] = value;
1629 wined3d_device_context_emit_set_sampler_state(&device->cs->c, sampler_idx, state, value);
1632 void CDECL wined3d_device_context_get_scissor_rects(const struct wined3d_device_context *context,
1633 unsigned int *rect_count, RECT *rects)
1635 const struct wined3d_state *state = context->state;
1636 unsigned int count;
1638 TRACE("context %p, rect_count %p, rects %p.\n", context, rect_count, rects);
1640 if (rects && (count = rect_count ? min(*rect_count, state->scissor_rect_count) : 1))
1641 memcpy(rects, state->scissor_rects, count * sizeof(*rects));
1642 if (rect_count)
1643 *rect_count = state->scissor_rect_count;
1646 void CDECL wined3d_device_context_reset_state(struct wined3d_device_context *context)
1648 TRACE("context %p.\n", context);
1650 wined3d_mutex_lock();
1651 state_cleanup(context->state);
1652 wined3d_state_reset(context->state, &context->device->adapter->d3d_info);
1653 wined3d_device_context_emit_reset_state(context, true);
1654 wined3d_mutex_unlock();
1657 void CDECL wined3d_device_context_set_state(struct wined3d_device_context *context, struct wined3d_state *state)
1659 const struct wined3d_light_info *light;
1660 unsigned int i, j;
1662 TRACE("context %p, state %p.\n", context, state);
1664 wined3d_mutex_lock();
1665 context->state = state;
1666 wined3d_device_context_emit_set_feature_level(context, state->feature_level);
1668 wined3d_device_context_emit_set_rendertarget_views(context, 0,
1669 ARRAY_SIZE(state->fb.render_targets), state->fb.render_targets);
1671 wined3d_device_context_emit_set_depth_stencil_view(context, state->fb.depth_stencil);
1672 wined3d_device_context_emit_set_vertex_declaration(context, state->vertex_declaration);
1674 wined3d_device_context_emit_set_stream_outputs(context, state->stream_output);
1676 wined3d_device_context_emit_set_stream_sources(context, 0, WINED3D_MAX_STREAMS, state->streams);
1678 wined3d_device_context_emit_set_index_buffer(context, state->index_buffer,
1679 state->index_format, state->index_offset);
1681 wined3d_device_context_emit_set_predication(context, state->predicate, state->predicate_value);
1683 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1685 wined3d_device_context_emit_set_shader(context, i, state->shader[i]);
1686 wined3d_device_context_emit_set_constant_buffers(context, i, 0, MAX_CONSTANT_BUFFERS, state->cb[i]);
1687 wined3d_device_context_emit_set_samplers(context, i, 0, MAX_SAMPLER_OBJECTS, state->sampler[i]);
1688 wined3d_device_context_emit_set_shader_resource_views(context, i, 0,
1689 MAX_SHADER_RESOURCE_VIEWS, state->shader_resource_view[i]);
1692 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
1693 wined3d_device_context_emit_set_unordered_access_views(context, i, 0, MAX_UNORDERED_ACCESS_VIEWS,
1694 state->unordered_access_view[i], NULL);
1696 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_F,
1697 0, WINED3D_MAX_VS_CONSTS_F, state->vs_consts_f);
1698 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_I,
1699 0, WINED3D_MAX_CONSTS_I, state->vs_consts_i);
1700 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_B,
1701 0, WINED3D_MAX_CONSTS_B, state->vs_consts_b);
1703 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_F,
1704 0, WINED3D_MAX_PS_CONSTS_F, state->ps_consts_f);
1705 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_I,
1706 0, WINED3D_MAX_CONSTS_I, state->ps_consts_i);
1707 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_B,
1708 0, WINED3D_MAX_CONSTS_B, state->ps_consts_b);
1710 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1712 wined3d_device_context_emit_set_texture(context, i, state->textures[i]);
1713 for (j = 0; j < WINED3D_HIGHEST_SAMPLER_STATE + 1; ++j)
1715 wined3d_device_context_emit_set_sampler_state(context, i, j, state->sampler_states[i][j]);
1719 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1721 for (j = 0; j < WINED3D_HIGHEST_TEXTURE_STATE + 1; ++j)
1723 wined3d_device_context_emit_set_texture_state(context, i, j, state->texture_states[i][j]);
1727 for (i = 0; i < WINED3D_HIGHEST_TRANSFORM_STATE + 1; ++i)
1729 if (context->device->state_table[STATE_TRANSFORM(i)].representative)
1730 wined3d_device_context_emit_set_transform(context, i, state->transforms + i);
1733 for (i = 0; i < WINED3D_MAX_CLIP_DISTANCES; ++i)
1735 wined3d_device_context_emit_set_clip_plane(context, i, state->clip_planes + i);
1738 wined3d_device_context_emit_set_material(context, &state->material);
1740 wined3d_device_context_emit_set_viewports(context, state->viewport_count, state->viewports);
1741 wined3d_device_context_emit_set_scissor_rects(context, state->scissor_rect_count, state->scissor_rects);
1743 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1745 LIST_FOR_EACH_ENTRY(light, &state->light_state.light_map[i], struct wined3d_light_info, entry)
1747 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
1748 wined3d_device_context_emit_set_light_enable(context, light->OriginalIndex, light->glIndex != -1);
1752 for (i = 0; i < WINEHIGHEST_RENDER_STATE + 1; ++i)
1754 if (context->device->state_table[STATE_RENDER(i)].representative)
1755 wined3d_device_context_emit_set_render_state(context, i, state->render_states[i]);
1758 wined3d_device_context_emit_set_blend_state(context, state->blend_state, &state->blend_factor, state->sample_mask);
1759 wined3d_device_context_emit_set_depth_stencil_state(context, state->depth_stencil_state, state->stencil_ref);
1760 wined3d_device_context_emit_set_rasterizer_state(context, state->rasterizer_state);
1761 wined3d_mutex_unlock();
1764 struct wined3d_state * CDECL wined3d_device_get_state(struct wined3d_device *device)
1766 TRACE("device %p.\n", device);
1768 return device->cs->c.state;
1771 struct wined3d_device_context * CDECL wined3d_device_get_immediate_context(struct wined3d_device *device)
1773 TRACE("device %p.\n", device);
1775 return &device->cs->c;
1778 struct wined3d_vertex_declaration * CDECL wined3d_device_context_get_vertex_declaration(
1779 const struct wined3d_device_context *context)
1781 TRACE("context %p.\n", context);
1783 return context->state->vertex_declaration;
1786 void CDECL wined3d_device_context_set_shader(struct wined3d_device_context *context,
1787 enum wined3d_shader_type type, struct wined3d_shader *shader)
1789 struct wined3d_state *state = context->state;
1790 struct wined3d_shader *prev;
1792 TRACE("context %p, type %#x, shader %p.\n", context, type, shader);
1794 wined3d_mutex_lock();
1795 prev = state->shader[type];
1796 if (shader == prev)
1797 goto out;
1799 if (shader)
1800 wined3d_shader_incref(shader);
1801 state->shader[type] = shader;
1802 wined3d_device_context_emit_set_shader(context, type, shader);
1803 if (prev)
1804 wined3d_shader_decref(prev);
1805 out:
1806 wined3d_mutex_unlock();
1809 struct wined3d_shader * CDECL wined3d_device_context_get_shader(const struct wined3d_device_context *context,
1810 enum wined3d_shader_type type)
1812 TRACE("context %p, type %#x.\n", context, type);
1814 return context->state->shader[type];
1817 void CDECL wined3d_device_context_set_constant_buffers(struct wined3d_device_context *context,
1818 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1819 const struct wined3d_constant_buffer_state *buffers)
1821 struct wined3d_state *state = context->state;
1822 unsigned int i;
1824 TRACE("context %p, type %#x, start_idx %u, count %u, buffers %p.\n", context, type, start_idx, count, buffers);
1826 if (!wined3d_bound_range(start_idx, count, MAX_CONSTANT_BUFFERS))
1828 WARN("Invalid constant buffer index %u, count %u.\n", start_idx, count);
1829 return;
1832 wined3d_mutex_lock();
1833 if (!memcmp(buffers, &state->cb[type][start_idx], count * sizeof(*buffers)))
1834 goto out;
1836 wined3d_device_context_emit_set_constant_buffers(context, type, start_idx, count, buffers);
1837 for (i = 0; i < count; ++i)
1839 struct wined3d_buffer *prev = state->cb[type][start_idx + i].buffer;
1840 struct wined3d_buffer *buffer = buffers[i].buffer;
1842 if (buffer)
1843 wined3d_buffer_incref(buffer);
1844 state->cb[type][start_idx + i] = buffers[i];
1845 if (prev)
1846 wined3d_buffer_decref(prev);
1848 out:
1849 wined3d_mutex_unlock();
1852 void CDECL wined3d_device_context_set_blend_state(struct wined3d_device_context *context,
1853 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1855 struct wined3d_state *state = context->state;
1856 struct wined3d_blend_state *prev;
1858 TRACE("context %p, blend_state %p, blend_factor %p, sample_mask %#x.\n",
1859 context, blend_state, blend_factor, sample_mask);
1861 wined3d_mutex_lock();
1862 prev = state->blend_state;
1863 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor))
1864 && sample_mask == state->sample_mask)
1865 goto out;
1867 if (blend_state)
1868 wined3d_blend_state_incref(blend_state);
1869 state->blend_state = blend_state;
1870 state->blend_factor = *blend_factor;
1871 state->sample_mask = sample_mask;
1872 wined3d_device_context_emit_set_blend_state(context, blend_state, blend_factor, sample_mask);
1873 if (prev)
1874 wined3d_blend_state_decref(prev);
1875 out:
1876 wined3d_mutex_unlock();
1879 void CDECL wined3d_device_context_set_depth_stencil_state(struct wined3d_device_context *context,
1880 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
1882 struct wined3d_state *state = context->state;
1883 struct wined3d_depth_stencil_state *prev;
1885 TRACE("context %p, depth_stencil_state %p, stencil_ref %u.\n", context, depth_stencil_state, stencil_ref);
1887 wined3d_mutex_lock();
1888 prev = state->depth_stencil_state;
1889 if (prev == depth_stencil_state && state->stencil_ref == stencil_ref)
1890 goto out;
1892 if (depth_stencil_state)
1893 wined3d_depth_stencil_state_incref(depth_stencil_state);
1894 state->depth_stencil_state = depth_stencil_state;
1895 state->stencil_ref = stencil_ref;
1896 wined3d_device_context_emit_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
1897 if (prev)
1898 wined3d_depth_stencil_state_decref(prev);
1899 out:
1900 wined3d_mutex_unlock();
1903 void CDECL wined3d_device_context_set_rasterizer_state(struct wined3d_device_context *context,
1904 struct wined3d_rasterizer_state *rasterizer_state)
1906 struct wined3d_state *state = context->state;
1907 struct wined3d_rasterizer_state *prev;
1909 TRACE("context %p, rasterizer_state %p.\n", context, rasterizer_state);
1911 wined3d_mutex_lock();
1912 prev = state->rasterizer_state;
1913 if (prev == rasterizer_state)
1914 goto out;
1916 if (rasterizer_state)
1917 wined3d_rasterizer_state_incref(rasterizer_state);
1918 state->rasterizer_state = rasterizer_state;
1919 wined3d_device_context_emit_set_rasterizer_state(context, rasterizer_state);
1920 if (prev)
1921 wined3d_rasterizer_state_decref(prev);
1922 out:
1923 wined3d_mutex_unlock();
1926 void CDECL wined3d_device_context_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
1927 const struct wined3d_viewport *viewports)
1929 struct wined3d_state *state = context->state;
1930 unsigned int i;
1932 TRACE("context %p, viewport_count %u, viewports %p.\n", context, viewport_count, viewports);
1934 for (i = 0; i < viewport_count; ++i)
1936 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
1937 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
1940 wined3d_mutex_lock();
1941 if (viewport_count)
1942 memcpy(state->viewports, viewports, viewport_count * sizeof(*viewports));
1943 else
1944 memset(state->viewports, 0, sizeof(state->viewports));
1945 state->viewport_count = viewport_count;
1947 wined3d_device_context_emit_set_viewports(context, viewport_count, viewports);
1948 wined3d_mutex_unlock();
1951 void CDECL wined3d_device_context_set_scissor_rects(struct wined3d_device_context *context, unsigned int rect_count,
1952 const RECT *rects)
1954 struct wined3d_state *state = context->state;
1955 unsigned int i;
1957 TRACE("context %p, rect_count %u, rects %p.\n", context, rect_count, rects);
1959 for (i = 0; i < rect_count; ++i)
1961 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
1964 wined3d_mutex_lock();
1965 if (state->scissor_rect_count == rect_count
1966 && !memcmp(state->scissor_rects, rects, rect_count * sizeof(*rects)))
1968 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
1969 goto out;
1972 if (rect_count)
1973 memcpy(state->scissor_rects, rects, rect_count * sizeof(*rects));
1974 else
1975 memset(state->scissor_rects, 0, sizeof(state->scissor_rects));
1976 state->scissor_rect_count = rect_count;
1978 wined3d_device_context_emit_set_scissor_rects(context, rect_count, rects);
1979 out:
1980 wined3d_mutex_unlock();
1983 void CDECL wined3d_device_context_set_shader_resource_views(struct wined3d_device_context *context,
1984 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1985 struct wined3d_shader_resource_view *const *const views)
1987 struct wined3d_shader_resource_view *real_views[MAX_SHADER_RESOURCE_VIEWS];
1988 struct wined3d_state *state = context->state;
1989 const struct wined3d_rendertarget_view *dsv = state->fb.depth_stencil;
1990 unsigned int i;
1992 TRACE("context %p, type %#x, start_idx %u, count %u, views %p.\n", context, type, start_idx, count, views);
1994 if (!wined3d_bound_range(start_idx, count, MAX_SHADER_RESOURCE_VIEWS))
1996 WARN("Invalid view index %u, count %u.\n", start_idx, count);
1997 return;
2000 wined3d_mutex_lock();
2001 if (!memcmp(views, &state->shader_resource_view[type][start_idx], count * sizeof(*views)))
2002 goto out;
2004 memcpy(real_views, views, count * sizeof(*views));
2006 for (i = 0; i < count; ++i)
2008 struct wined3d_shader_resource_view *view = real_views[i];
2010 if (view && (wined3d_is_srv_rtv_bound(state, view)
2011 || (dsv && dsv->resource == view->resource && wined3d_dsv_srv_conflict(dsv, view->format))))
2013 WARN("Application is trying to bind resource which is attached as render target.\n");
2014 real_views[i] = NULL;
2018 wined3d_device_context_emit_set_shader_resource_views(context, type, start_idx, count, real_views);
2019 for (i = 0; i < count; ++i)
2021 struct wined3d_shader_resource_view *prev = state->shader_resource_view[type][start_idx + i];
2022 struct wined3d_shader_resource_view *view = real_views[i];
2024 if (view)
2026 wined3d_shader_resource_view_incref(view);
2027 wined3d_srv_bind_count_inc(view);
2030 state->shader_resource_view[type][start_idx + i] = view;
2031 if (prev)
2033 wined3d_srv_bind_count_dec(prev);
2034 wined3d_shader_resource_view_decref(prev);
2037 out:
2038 wined3d_mutex_unlock();
2041 void CDECL wined3d_device_context_set_samplers(struct wined3d_device_context *context, enum wined3d_shader_type type,
2042 unsigned int start_idx, unsigned int count, struct wined3d_sampler *const *samplers)
2044 struct wined3d_state *state = context->state;
2045 unsigned int i;
2047 TRACE("context %p, type %#x, start_idx %u, count %u, samplers %p.\n", context, type, start_idx, count, samplers);
2049 if (!wined3d_bound_range(start_idx, count, MAX_SAMPLER_OBJECTS))
2051 WARN("Invalid sampler index %u, count %u.\n", start_idx, count);
2052 return;
2055 wined3d_mutex_lock();
2056 if (!memcmp(samplers, &state->sampler[type][start_idx], count * sizeof(*samplers)))
2057 goto out;
2059 wined3d_device_context_emit_set_samplers(context, type, start_idx, count, samplers);
2060 for (i = 0; i < count; ++i)
2062 struct wined3d_sampler *prev = state->sampler[type][start_idx + i];
2063 struct wined3d_sampler *sampler = samplers[i];
2065 if (sampler)
2066 wined3d_sampler_incref(sampler);
2067 state->sampler[type][start_idx + i] = sampler;
2068 if (prev)
2069 wined3d_sampler_decref(prev);
2071 out:
2072 wined3d_mutex_unlock();
2075 void CDECL wined3d_device_context_set_unordered_access_views(struct wined3d_device_context *context,
2076 enum wined3d_pipeline pipeline, unsigned int start_idx, unsigned int count,
2077 struct wined3d_unordered_access_view *const *uavs, const unsigned int *initial_counts)
2079 struct wined3d_state *state = context->state;
2080 unsigned int i;
2082 TRACE("context %p, pipeline %#x, start_idx %u, count %u, uavs %p, initial_counts %p.\n",
2083 context, pipeline, start_idx, count, uavs, initial_counts);
2085 if (!wined3d_bound_range(start_idx, count, MAX_UNORDERED_ACCESS_VIEWS))
2087 WARN("Invalid UAV index %u, count %u.\n", start_idx, count);
2088 return;
2091 wined3d_mutex_lock();
2092 if (!memcmp(uavs, &state->unordered_access_view[pipeline][start_idx], count * sizeof(*uavs)) && !initial_counts)
2093 goto out;
2095 wined3d_device_context_emit_set_unordered_access_views(context, pipeline, start_idx, count, uavs, initial_counts);
2096 for (i = 0; i < count; ++i)
2098 struct wined3d_unordered_access_view *prev = state->unordered_access_view[pipeline][start_idx + i];
2099 struct wined3d_unordered_access_view *uav = uavs[i];
2101 if (uav)
2102 wined3d_unordered_access_view_incref(uav);
2103 state->unordered_access_view[pipeline][start_idx + i] = uav;
2104 if (prev)
2105 wined3d_unordered_access_view_decref(prev);
2107 out:
2108 wined3d_mutex_unlock();
2111 static void wined3d_device_context_unbind_srv_for_rtv(struct wined3d_device_context *context,
2112 const struct wined3d_rendertarget_view *view, BOOL dsv)
2114 const struct wined3d_state *state = context->state;
2115 const struct wined3d_resource *resource;
2117 if (!view)
2118 return;
2119 resource = view->resource;
2121 if (resource->srv_bind_count_device)
2123 const struct wined3d_shader_resource_view *srv;
2124 unsigned int i, j;
2126 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2128 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
2130 if ((srv = state->shader_resource_view[i][j]) && srv->resource == resource
2131 && ((!dsv && wined3d_is_srv_rtv_bound(state, srv))
2132 || (dsv && wined3d_dsv_srv_conflict(view, srv->format))))
2134 static struct wined3d_shader_resource_view *const null_srv;
2136 WARN("Application sets bound resource as render target.\n");
2137 wined3d_device_context_set_shader_resource_views(context, i, j, 1, &null_srv);
2144 HRESULT CDECL wined3d_device_context_set_rendertarget_views(struct wined3d_device_context *context,
2145 unsigned int start_idx, unsigned int count, struct wined3d_rendertarget_view *const *views, BOOL set_viewport)
2147 struct wined3d_state *state = context->state;
2148 unsigned int i, max_rt_count;
2150 TRACE("context %p, start_idx %u, count %u, views %p, set_viewport %#x.\n",
2151 context, start_idx, count, views, set_viewport);
2153 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
2154 if (start_idx >= max_rt_count)
2156 WARN("Only %u render targets are supported.\n", max_rt_count);
2157 return WINED3DERR_INVALIDCALL;
2159 count = min(count, max_rt_count - start_idx);
2161 for (i = 0; i < count; ++i)
2163 if (views[i] && !(views[i]->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
2165 WARN("View resource %p doesn't have render target bind flags.\n", views[i]->resource);
2166 return WINED3DERR_INVALIDCALL;
2170 wined3d_mutex_lock();
2171 /* Set the viewport and scissor rectangles, if requested. Tests show that
2172 * stateblock recording is ignored, the change goes directly into the
2173 * primary stateblock. */
2174 if (!start_idx && set_viewport)
2176 state->viewports[0].x = 0;
2177 state->viewports[0].y = 0;
2178 state->viewports[0].width = views[0]->width;
2179 state->viewports[0].height = views[0]->height;
2180 state->viewports[0].min_z = 0.0f;
2181 state->viewports[0].max_z = 1.0f;
2182 state->viewport_count = 1;
2183 wined3d_device_context_emit_set_viewports(context, 1, state->viewports);
2185 SetRect(&state->scissor_rects[0], 0, 0, views[0]->width, views[0]->height);
2186 state->scissor_rect_count = 1;
2187 wined3d_device_context_emit_set_scissor_rects(context, 1, state->scissor_rects);
2190 if (!memcmp(views, &state->fb.render_targets[start_idx], count * sizeof(*views)))
2191 goto out;
2193 wined3d_device_context_emit_set_rendertarget_views(context, start_idx, count, views);
2194 for (i = 0; i < count; ++i)
2196 struct wined3d_rendertarget_view *prev = state->fb.render_targets[start_idx + i];
2197 struct wined3d_rendertarget_view *view = views[i];
2199 if (view)
2201 wined3d_rendertarget_view_incref(view);
2202 wined3d_rtv_bind_count_inc(view);
2204 state->fb.render_targets[start_idx + i] = view;
2205 /* Release after the assignment, to prevent device_resource_released()
2206 * from seeing the resource as still in use. */
2207 if (prev)
2209 wined3d_rtv_bind_count_dec(prev);
2210 wined3d_rendertarget_view_decref(prev);
2213 wined3d_device_context_unbind_srv_for_rtv(context, view, FALSE);
2215 out:
2216 wined3d_mutex_unlock();
2217 return WINED3D_OK;
2220 HRESULT CDECL wined3d_device_context_set_depth_stencil_view(struct wined3d_device_context *context,
2221 struct wined3d_rendertarget_view *view)
2223 struct wined3d_fb_state *fb = &context->state->fb;
2224 struct wined3d_rendertarget_view *prev;
2226 TRACE("context %p, view %p.\n", context, view);
2228 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2230 WARN("View resource %p has incompatible %s bind flags.\n",
2231 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
2232 return WINED3DERR_INVALIDCALL;
2235 wined3d_mutex_lock();
2236 prev = fb->depth_stencil;
2237 if (prev == view)
2239 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
2240 goto out;
2243 if ((fb->depth_stencil = view))
2244 wined3d_rendertarget_view_incref(view);
2245 wined3d_device_context_emit_set_depth_stencil_view(context, view);
2246 if (prev)
2247 wined3d_rendertarget_view_decref(prev);
2248 wined3d_device_context_unbind_srv_for_rtv(context, view, TRUE);
2249 out:
2250 wined3d_mutex_unlock();
2251 return WINED3D_OK;
2254 void CDECL wined3d_device_context_set_predication(struct wined3d_device_context *context,
2255 struct wined3d_query *predicate, BOOL value)
2257 struct wined3d_state *state = context->state;
2258 struct wined3d_query *prev;
2260 TRACE("context %p, predicate %p, value %#x.\n", context, predicate, value);
2262 wined3d_mutex_lock();
2263 prev = state->predicate;
2264 if (predicate)
2266 FIXME("Predicated rendering not implemented.\n");
2267 wined3d_query_incref(predicate);
2269 state->predicate = predicate;
2270 state->predicate_value = value;
2271 wined3d_device_context_emit_set_predication(context, predicate, value);
2272 if (prev)
2273 wined3d_query_decref(prev);
2274 wined3d_mutex_unlock();
2277 HRESULT CDECL wined3d_device_context_set_stream_sources(struct wined3d_device_context *context,
2278 unsigned int start_idx, unsigned int count, const struct wined3d_stream_state *streams)
2280 struct wined3d_state *state = context->state;
2281 unsigned int i;
2283 TRACE("context %p, start_idx %u, count %u, streams %p.\n", context, start_idx, count, streams);
2285 if (start_idx >= WINED3D_MAX_STREAMS)
2287 WARN("Start index %u is out of range.\n", start_idx);
2288 return WINED3DERR_INVALIDCALL;
2291 count = min(count, WINED3D_MAX_STREAMS - start_idx);
2293 for (i = 0; i < count; ++i)
2295 if (streams[i].offset & 0x3)
2297 WARN("Offset %u is not 4 byte aligned.\n", streams[i].offset);
2298 return WINED3DERR_INVALIDCALL;
2302 wined3d_mutex_lock();
2303 if (!memcmp(streams, &state->streams[start_idx], count * sizeof(*streams)))
2304 goto out;
2306 wined3d_device_context_emit_set_stream_sources(context, start_idx, count, streams);
2307 for (i = 0; i < count; ++i)
2309 struct wined3d_buffer *prev = state->streams[start_idx + i].buffer;
2310 struct wined3d_buffer *buffer = streams[i].buffer;
2312 state->streams[start_idx + i] = streams[i];
2314 if (buffer)
2315 wined3d_buffer_incref(buffer);
2316 if (prev)
2317 wined3d_buffer_decref(prev);
2319 out:
2320 wined3d_mutex_unlock();
2321 return WINED3D_OK;
2324 void CDECL wined3d_device_context_set_index_buffer(struct wined3d_device_context *context,
2325 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
2327 struct wined3d_state *state = context->state;
2328 enum wined3d_format_id prev_format;
2329 struct wined3d_buffer *prev_buffer;
2330 unsigned int prev_offset;
2332 TRACE("context %p, buffer %p, format %s, offset %u.\n",
2333 context, buffer, debug_d3dformat(format_id), offset);
2335 wined3d_mutex_lock();
2336 prev_buffer = state->index_buffer;
2337 prev_format = state->index_format;
2338 prev_offset = state->index_offset;
2340 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
2341 goto out;
2343 if (buffer)
2344 wined3d_buffer_incref(buffer);
2345 state->index_buffer = buffer;
2346 state->index_format = format_id;
2347 state->index_offset = offset;
2348 wined3d_device_context_emit_set_index_buffer(context, buffer, format_id, offset);
2349 if (prev_buffer)
2350 wined3d_buffer_decref(prev_buffer);
2351 out:
2352 wined3d_mutex_unlock();
2355 void CDECL wined3d_device_context_set_vertex_declaration(struct wined3d_device_context *context,
2356 struct wined3d_vertex_declaration *declaration)
2358 struct wined3d_state *state = context->state;
2359 struct wined3d_vertex_declaration *prev;
2361 TRACE("context %p, declaration %p.\n", context, declaration);
2363 wined3d_mutex_lock();
2364 prev = state->vertex_declaration;
2365 if (declaration == prev)
2366 goto out;
2368 if (declaration)
2369 wined3d_vertex_declaration_incref(declaration);
2370 state->vertex_declaration = declaration;
2371 wined3d_device_context_emit_set_vertex_declaration(context, declaration);
2372 if (prev)
2373 wined3d_vertex_declaration_decref(prev);
2374 out:
2375 wined3d_mutex_unlock();
2378 void CDECL wined3d_device_context_set_stream_outputs(struct wined3d_device_context *context,
2379 const struct wined3d_stream_output outputs[WINED3D_MAX_STREAM_OUTPUT_BUFFERS])
2381 struct wined3d_state *state = context->state;
2382 unsigned int i;
2384 TRACE("context %p, outputs %p.\n", context, outputs);
2386 wined3d_mutex_lock();
2387 wined3d_device_context_emit_set_stream_outputs(context, outputs);
2388 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
2390 struct wined3d_buffer *prev_buffer = state->stream_output[i].buffer;
2391 struct wined3d_buffer *buffer = outputs[i].buffer;
2393 if (buffer)
2394 wined3d_buffer_incref(buffer);
2395 state->stream_output[i] = outputs[i];
2396 if (prev_buffer)
2397 wined3d_buffer_decref(prev_buffer);
2399 wined3d_mutex_unlock();
2402 void CDECL wined3d_device_context_draw(struct wined3d_device_context *context, unsigned int start_vertex,
2403 unsigned int vertex_count, unsigned int start_instance, unsigned int instance_count)
2405 struct wined3d_state *state = context->state;
2407 TRACE("context %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
2408 context, start_vertex, vertex_count, start_instance, instance_count);
2410 wined3d_mutex_lock();
2411 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2412 0, start_vertex, vertex_count, start_instance, instance_count, false);
2413 wined3d_mutex_unlock();
2416 void CDECL wined3d_device_context_draw_indexed(struct wined3d_device_context *context, int base_vertex_index,
2417 unsigned int start_index, unsigned int index_count, unsigned int start_instance, unsigned int instance_count)
2419 struct wined3d_state *state = context->state;
2421 TRACE("context %p, base_vertex_index %d, start_index %u, index_count %u, start_instance %u, instance_count %u.\n",
2422 context, base_vertex_index, start_index, index_count, start_instance, instance_count);
2424 wined3d_mutex_lock();
2425 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2426 base_vertex_index, start_index, index_count, start_instance, instance_count, true);
2427 wined3d_mutex_unlock();
2430 void CDECL wined3d_device_context_get_constant_buffer(const struct wined3d_device_context *context,
2431 enum wined3d_shader_type shader_type, unsigned int idx, struct wined3d_constant_buffer_state *state)
2433 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2435 if (idx >= MAX_CONSTANT_BUFFERS)
2437 WARN("Invalid constant buffer index %u.\n", idx);
2438 return;
2441 *state = context->state->cb[shader_type][idx];
2444 struct wined3d_shader_resource_view * CDECL wined3d_device_context_get_shader_resource_view(
2445 const struct wined3d_device_context *context, enum wined3d_shader_type shader_type, unsigned int idx)
2447 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2449 WARN("Invalid view index %u.\n", idx);
2450 return NULL;
2453 return context->state->shader_resource_view[shader_type][idx];
2456 struct wined3d_sampler * CDECL wined3d_device_context_get_sampler(const struct wined3d_device_context *context,
2457 enum wined3d_shader_type shader_type, unsigned int idx)
2459 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2461 if (idx >= MAX_SAMPLER_OBJECTS)
2463 WARN("Invalid sampler index %u.\n", idx);
2464 return NULL;
2467 return context->state->sampler[shader_type][idx];
2470 static void wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2471 unsigned int start_idx, unsigned int count, const BOOL *constants)
2473 unsigned int i;
2475 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2476 device, start_idx, count, constants);
2478 memcpy(&device->cs->c.state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2479 if (TRACE_ON(d3d))
2481 for (i = 0; i < count; ++i)
2482 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2485 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2488 static void wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2489 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2491 unsigned int i;
2493 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2494 device, start_idx, count, constants);
2496 memcpy(&device->cs->c.state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2497 if (TRACE_ON(d3d))
2499 for (i = 0; i < count; ++i)
2500 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2503 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2506 static void wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2507 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2509 unsigned int i;
2511 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2512 device, start_idx, count, constants);
2514 memcpy(&device->cs->c.state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2515 if (TRACE_ON(d3d))
2517 for (i = 0; i < count; ++i)
2518 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2521 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2524 static void wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2525 unsigned int start_idx, unsigned int count, const BOOL *constants)
2527 unsigned int i;
2529 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2530 device, start_idx, count, constants);
2532 memcpy(&device->cs->c.state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2533 if (TRACE_ON(d3d))
2535 for (i = 0; i < count; ++i)
2536 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2539 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2542 static void wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2543 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2545 unsigned int i;
2547 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2548 device, start_idx, count, constants);
2550 memcpy(&device->cs->c.state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2551 if (TRACE_ON(d3d))
2553 for (i = 0; i < count; ++i)
2554 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2557 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2560 static void wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2561 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2563 unsigned int i;
2565 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2566 device, start_idx, count, constants);
2568 memcpy(&device->cs->c.state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2569 if (TRACE_ON(d3d))
2571 for (i = 0; i < count; ++i)
2572 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2575 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2578 struct wined3d_unordered_access_view * CDECL wined3d_device_context_get_unordered_access_view(
2579 const struct wined3d_device_context *context, enum wined3d_pipeline pipeline, unsigned int idx)
2581 TRACE("context %p, pipeline %#x, idx %u.\n", context, pipeline, idx);
2583 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2585 WARN("Invalid UAV index %u.\n", idx);
2586 return NULL;
2589 return context->state->unordered_access_view[pipeline][idx];
2592 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
2594 unsigned int i;
2596 if (!latency)
2597 latency = 3;
2599 device->max_frame_latency = latency;
2600 for (i = 0; i < device->swapchain_count; ++i)
2601 swapchain_set_max_frame_latency(device->swapchains[i], device);
2604 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
2606 return device->max_frame_latency;
2609 static unsigned int wined3d_get_flexible_vertex_size(DWORD fvf)
2611 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2612 unsigned int i, size = 0;
2614 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2615 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2616 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2617 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2618 switch (fvf & WINED3DFVF_POSITION_MASK)
2620 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2621 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2622 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
2623 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
2624 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
2625 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
2626 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
2627 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
2628 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
2630 for (i = 0; i < texcoord_count; ++i)
2632 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
2635 return size;
2638 static void wined3d_format_get_colour(const struct wined3d_format *format,
2639 const void *data, struct wined3d_color *colour)
2641 float *output = &colour->r;
2642 const uint32_t *u32_data;
2643 const uint16_t *u16_data;
2644 const float *f32_data;
2645 unsigned int i;
2647 static const struct wined3d_color default_colour = {0.0f, 0.0f, 0.0f, 1.0f};
2648 static unsigned int warned;
2650 switch (format->id)
2652 case WINED3DFMT_B8G8R8A8_UNORM:
2653 u32_data = data;
2654 wined3d_color_from_d3dcolor(colour, *u32_data);
2655 break;
2657 case WINED3DFMT_R8G8B8A8_UNORM:
2658 u32_data = data;
2659 colour->r = (*u32_data & 0xffu) / 255.0f;
2660 colour->g = ((*u32_data >> 8) & 0xffu) / 255.0f;
2661 colour->b = ((*u32_data >> 16) & 0xffu) / 255.0f;
2662 colour->a = ((*u32_data >> 24) & 0xffu) / 255.0f;
2663 break;
2665 case WINED3DFMT_R16G16_UNORM:
2666 case WINED3DFMT_R16G16B16A16_UNORM:
2667 u16_data = data;
2668 *colour = default_colour;
2669 for (i = 0; i < format->component_count; ++i)
2670 output[i] = u16_data[i] / 65535.0f;
2671 break;
2673 case WINED3DFMT_R32_FLOAT:
2674 case WINED3DFMT_R32G32_FLOAT:
2675 case WINED3DFMT_R32G32B32_FLOAT:
2676 case WINED3DFMT_R32G32B32A32_FLOAT:
2677 f32_data = data;
2678 *colour = default_colour;
2679 for (i = 0; i < format->component_count; ++i)
2680 output[i] = f32_data[i];
2681 break;
2683 default:
2684 *colour = default_colour;
2685 if (!warned++)
2686 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format->id));
2687 break;
2691 static void wined3d_colour_from_mcs(struct wined3d_color *colour, enum wined3d_material_color_source mcs,
2692 const struct wined3d_color *material_colour, unsigned int index,
2693 const struct wined3d_stream_info *stream_info)
2695 const struct wined3d_stream_info_element *element = NULL;
2697 switch (mcs)
2699 case WINED3D_MCS_MATERIAL:
2700 *colour = *material_colour;
2701 return;
2703 case WINED3D_MCS_COLOR1:
2704 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
2706 colour->r = colour->g = colour->b = colour->a = 1.0f;
2707 return;
2709 element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2710 break;
2712 case WINED3D_MCS_COLOR2:
2713 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
2715 colour->r = colour->g = colour->b = colour->a = 0.0f;
2716 return;
2718 element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2719 break;
2721 default:
2722 colour->r = colour->g = colour->b = colour->a = 0.0f;
2723 ERR("Invalid material colour source %#x.\n", mcs);
2724 return;
2727 wined3d_format_get_colour(element->format, &element->data.addr[index * element->stride], colour);
2730 static float wined3d_clamp(float value, float min_value, float max_value)
2732 return value < min_value ? min_value : value > max_value ? max_value : value;
2735 static float wined3d_vec3_dot(const struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2737 return v0->x * v1->x + v0->y * v1->y + v0->z * v1->z;
2740 static void wined3d_vec3_subtract(struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2742 v0->x -= v1->x;
2743 v0->y -= v1->y;
2744 v0->z -= v1->z;
2747 static void wined3d_vec3_scale(struct wined3d_vec3 *v, float s)
2749 v->x *= s;
2750 v->y *= s;
2751 v->z *= s;
2754 static void wined3d_vec3_normalise(struct wined3d_vec3 *v)
2756 float rnorm = 1.0f / sqrtf(wined3d_vec3_dot(v, v));
2758 if (isfinite(rnorm))
2759 wined3d_vec3_scale(v, rnorm);
2762 static void wined3d_vec3_transform(struct wined3d_vec3 *dst,
2763 const struct wined3d_vec3 *v, const struct wined3d_matrix_3x3 *m)
2765 struct wined3d_vec3 tmp;
2767 tmp.x = v->x * m->_11 + v->y * m->_21 + v->z * m->_31;
2768 tmp.y = v->x * m->_12 + v->y * m->_22 + v->z * m->_32;
2769 tmp.z = v->x * m->_13 + v->y * m->_23 + v->z * m->_33;
2771 *dst = tmp;
2774 static void wined3d_color_clamp(struct wined3d_color *dst, const struct wined3d_color *src,
2775 float min_value, float max_value)
2777 dst->r = wined3d_clamp(src->r, min_value, max_value);
2778 dst->g = wined3d_clamp(src->g, min_value, max_value);
2779 dst->b = wined3d_clamp(src->b, min_value, max_value);
2780 dst->a = wined3d_clamp(src->a, min_value, max_value);
2783 static void wined3d_color_rgb_mul_add(struct wined3d_color *dst, const struct wined3d_color *src, float c)
2785 dst->r += src->r * c;
2786 dst->g += src->g * c;
2787 dst->b += src->b * c;
2790 static void init_transformed_lights(struct lights_settings *ls,
2791 const struct wined3d_state *state, BOOL legacy_lighting, BOOL compute_lighting)
2793 const struct wined3d_light_info *lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
2794 const struct wined3d_light_info *light_info;
2795 struct light_transformed *light;
2796 struct wined3d_vec4 vec4;
2797 unsigned int light_count;
2798 unsigned int i, index;
2800 memset(ls, 0, sizeof(*ls));
2802 ls->lighting = !!compute_lighting;
2803 ls->fog_mode = state->render_states[WINED3D_RS_FOGVERTEXMODE];
2804 ls->fog_coord_mode = state->render_states[WINED3D_RS_RANGEFOGENABLE]
2805 ? WINED3D_FFP_VS_FOG_RANGE : WINED3D_FFP_VS_FOG_DEPTH;
2806 ls->fog_start = wined3d_get_float_state(state, WINED3D_RS_FOGSTART);
2807 ls->fog_end = wined3d_get_float_state(state, WINED3D_RS_FOGEND);
2808 ls->fog_density = wined3d_get_float_state(state, WINED3D_RS_FOGDENSITY);
2810 if (ls->fog_mode == WINED3D_FOG_NONE && !compute_lighting)
2811 return;
2813 multiply_matrix(&ls->modelview_matrix, &state->transforms[WINED3D_TS_VIEW],
2814 &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
2816 if (!compute_lighting)
2817 return;
2819 compute_normal_matrix(&ls->normal_matrix._11, legacy_lighting, &ls->modelview_matrix);
2821 wined3d_color_from_d3dcolor(&ls->ambient_light, state->render_states[WINED3D_RS_AMBIENT]);
2822 ls->legacy_lighting = !!legacy_lighting;
2823 ls->normalise = !!state->render_states[WINED3D_RS_NORMALIZENORMALS];
2824 ls->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
2826 for (i = 0, index = 0; i < LIGHTMAP_SIZE && index < ARRAY_SIZE(lights); ++i)
2828 LIST_FOR_EACH_ENTRY(light_info, &state->light_state.light_map[i], struct wined3d_light_info, entry)
2830 if (!light_info->enabled)
2831 continue;
2833 switch (light_info->OriginalParms.type)
2835 case WINED3D_LIGHT_DIRECTIONAL:
2836 ++ls->directional_light_count;
2837 break;
2839 case WINED3D_LIGHT_POINT:
2840 ++ls->point_light_count;
2841 break;
2843 case WINED3D_LIGHT_SPOT:
2844 ++ls->spot_light_count;
2845 break;
2847 case WINED3D_LIGHT_PARALLELPOINT:
2848 ++ls->parallel_point_light_count;
2849 break;
2851 default:
2852 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
2853 continue;
2855 lights[index++] = light_info;
2856 if (index == WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS)
2857 break;
2861 light_count = index;
2862 for (i = 0, index = 0; i < light_count; ++i)
2864 light_info = lights[i];
2865 if (light_info->OriginalParms.type != WINED3D_LIGHT_DIRECTIONAL)
2866 continue;
2868 light = &ls->lights[index];
2869 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
2870 light->direction = *(struct wined3d_vec3 *)&vec4;
2871 wined3d_vec3_normalise(&light->direction);
2873 light->diffuse = light_info->OriginalParms.diffuse;
2874 light->ambient = light_info->OriginalParms.ambient;
2875 light->specular = light_info->OriginalParms.specular;
2876 ++index;
2879 for (i = 0; i < light_count; ++i)
2881 light_info = lights[i];
2882 if (light_info->OriginalParms.type != WINED3D_LIGHT_POINT)
2883 continue;
2885 light = &ls->lights[index];
2887 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2888 light->range = light_info->OriginalParms.range;
2889 light->c_att = light_info->OriginalParms.attenuation0;
2890 light->l_att = light_info->OriginalParms.attenuation1;
2891 light->q_att = light_info->OriginalParms.attenuation2;
2893 light->diffuse = light_info->OriginalParms.diffuse;
2894 light->ambient = light_info->OriginalParms.ambient;
2895 light->specular = light_info->OriginalParms.specular;
2896 ++index;
2899 for (i = 0; i < light_count; ++i)
2901 light_info = lights[i];
2902 if (light_info->OriginalParms.type != WINED3D_LIGHT_SPOT)
2903 continue;
2905 light = &ls->lights[index];
2907 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2908 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
2909 light->direction = *(struct wined3d_vec3 *)&vec4;
2910 wined3d_vec3_normalise(&light->direction);
2911 light->range = light_info->OriginalParms.range;
2912 light->falloff = light_info->OriginalParms.falloff;
2913 light->c_att = light_info->OriginalParms.attenuation0;
2914 light->l_att = light_info->OriginalParms.attenuation1;
2915 light->q_att = light_info->OriginalParms.attenuation2;
2916 light->cos_htheta = cosf(light_info->OriginalParms.theta / 2.0f);
2917 light->cos_hphi = cosf(light_info->OriginalParms.phi / 2.0f);
2919 light->diffuse = light_info->OriginalParms.diffuse;
2920 light->ambient = light_info->OriginalParms.ambient;
2921 light->specular = light_info->OriginalParms.specular;
2922 ++index;
2925 for (i = 0; i < light_count; ++i)
2927 light_info = lights[i];
2928 if (light_info->OriginalParms.type != WINED3D_LIGHT_PARALLELPOINT)
2929 continue;
2931 light = &ls->lights[index];
2933 wined3d_vec4_transform(&vec4, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2934 *(struct wined3d_vec3 *)&light->position = *(struct wined3d_vec3 *)&vec4;
2935 wined3d_vec3_normalise((struct wined3d_vec3 *)&light->position);
2936 light->diffuse = light_info->OriginalParms.diffuse;
2937 light->ambient = light_info->OriginalParms.ambient;
2938 light->specular = light_info->OriginalParms.specular;
2939 ++index;
2943 static void update_light_diffuse_specular(struct wined3d_color *diffuse, struct wined3d_color *specular,
2944 const struct wined3d_vec3 *dir, float att, float material_shininess,
2945 const struct wined3d_vec3 *normal_transformed,
2946 const struct wined3d_vec3 *position_transformed_normalised,
2947 const struct light_transformed *light, const struct lights_settings *ls)
2949 struct wined3d_vec3 vec3;
2950 float t, c;
2952 c = wined3d_clamp(wined3d_vec3_dot(dir, normal_transformed), 0.0f, 1.0f);
2953 wined3d_color_rgb_mul_add(diffuse, &light->diffuse, c * att);
2955 vec3 = *dir;
2956 if (ls->localviewer)
2957 wined3d_vec3_subtract(&vec3, position_transformed_normalised);
2958 else
2959 vec3.z -= 1.0f;
2960 wined3d_vec3_normalise(&vec3);
2961 t = wined3d_vec3_dot(normal_transformed, &vec3);
2962 if (t > 0.0f && (!ls->legacy_lighting || material_shininess > 0.0f)
2963 && wined3d_vec3_dot(dir, normal_transformed) > 0.0f)
2964 wined3d_color_rgb_mul_add(specular, &light->specular, att * powf(t, material_shininess));
2967 static void light_set_vertex_data(struct lights_settings *ls,
2968 const struct wined3d_vec4 *position)
2970 if (ls->fog_mode == WINED3D_FOG_NONE && !ls->lighting)
2971 return;
2973 wined3d_vec4_transform(&ls->position_transformed, position, &ls->modelview_matrix);
2974 wined3d_vec3_scale((struct wined3d_vec3 *)&ls->position_transformed, 1.0f / ls->position_transformed.w);
2977 static void compute_light(struct wined3d_color *ambient, struct wined3d_color *diffuse,
2978 struct wined3d_color *specular, struct lights_settings *ls, const struct wined3d_vec3 *normal,
2979 float material_shininess)
2981 struct wined3d_vec3 position_transformed_normalised;
2982 struct wined3d_vec3 normal_transformed = {0.0f};
2983 const struct light_transformed *light;
2984 struct wined3d_vec3 dir, dst;
2985 unsigned int i, index;
2986 float att;
2988 position_transformed_normalised = *(const struct wined3d_vec3 *)&ls->position_transformed;
2989 wined3d_vec3_normalise(&position_transformed_normalised);
2991 if (normal)
2993 wined3d_vec3_transform(&normal_transformed, normal, &ls->normal_matrix);
2994 if (ls->normalise)
2995 wined3d_vec3_normalise(&normal_transformed);
2998 diffuse->r = diffuse->g = diffuse->b = diffuse->a = 0.0f;
2999 *specular = *diffuse;
3000 *ambient = ls->ambient_light;
3002 index = 0;
3003 for (i = 0; i < ls->directional_light_count; ++i, ++index)
3005 light = &ls->lights[index];
3007 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3008 if (normal)
3009 update_light_diffuse_specular(diffuse, specular, &light->direction, 1.0f, material_shininess,
3010 &normal_transformed, &position_transformed_normalised, light, ls);
3013 for (i = 0; i < ls->point_light_count; ++i, ++index)
3015 light = &ls->lights[index];
3016 dir.x = light->position.x - ls->position_transformed.x;
3017 dir.y = light->position.y - ls->position_transformed.y;
3018 dir.z = light->position.z - ls->position_transformed.z;
3020 dst.z = wined3d_vec3_dot(&dir, &dir);
3021 dst.y = sqrtf(dst.z);
3022 dst.x = 1.0f;
3023 if (ls->legacy_lighting)
3025 dst.y = (light->range - dst.y) / light->range;
3026 if (!(dst.y > 0.0f))
3027 continue;
3028 dst.z = dst.y * dst.y;
3030 else
3032 if (!(dst.y <= light->range))
3033 continue;
3035 att = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3036 if (!ls->legacy_lighting)
3037 att = 1.0f / att;
3039 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3040 if (normal)
3042 wined3d_vec3_normalise(&dir);
3043 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3044 &normal_transformed, &position_transformed_normalised, light, ls);
3048 for (i = 0; i < ls->spot_light_count; ++i, ++index)
3050 float t;
3052 light = &ls->lights[index];
3054 dir.x = light->position.x - ls->position_transformed.x;
3055 dir.y = light->position.y - ls->position_transformed.y;
3056 dir.z = light->position.z - ls->position_transformed.z;
3058 dst.z = wined3d_vec3_dot(&dir, &dir);
3059 dst.y = sqrtf(dst.z);
3060 dst.x = 1.0f;
3062 if (ls->legacy_lighting)
3064 dst.y = (light->range - dst.y) / light->range;
3065 if (!(dst.y > 0.0f))
3066 continue;
3067 dst.z = dst.y * dst.y;
3069 else
3071 if (!(dst.y <= light->range))
3072 continue;
3074 wined3d_vec3_normalise(&dir);
3075 t = -wined3d_vec3_dot(&dir, &light->direction);
3076 if (t > light->cos_htheta)
3077 att = 1.0f;
3078 else if (t <= light->cos_hphi)
3079 att = 0.0f;
3080 else
3081 att = powf((t - light->cos_hphi) / (light->cos_htheta - light->cos_hphi), light->falloff);
3083 t = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3084 if (ls->legacy_lighting)
3085 att *= t;
3086 else
3087 att /= t;
3089 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3091 if (normal)
3092 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3093 &normal_transformed, &position_transformed_normalised, light, ls);
3096 for (i = 0; i < ls->parallel_point_light_count; ++i, ++index)
3098 light = &ls->lights[index];
3100 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3101 if (normal)
3102 update_light_diffuse_specular(diffuse, specular, (const struct wined3d_vec3 *)&light->position,
3103 1.0f, material_shininess, &normal_transformed, &position_transformed_normalised, light, ls);
3107 static float wined3d_calculate_fog_factor(float fog_coord, const struct lights_settings *ls)
3109 switch (ls->fog_mode)
3111 case WINED3D_FOG_NONE:
3112 return fog_coord;
3113 case WINED3D_FOG_LINEAR:
3114 return (ls->fog_end - fog_coord) / (ls->fog_end - ls->fog_start);
3115 case WINED3D_FOG_EXP:
3116 return expf(-fog_coord * ls->fog_density);
3117 case WINED3D_FOG_EXP2:
3118 return expf(-fog_coord * fog_coord * ls->fog_density * ls->fog_density);
3119 default:
3120 ERR("Unhandled fog mode %#x.\n", ls->fog_mode);
3121 return 0.0f;
3125 static void update_fog_factor(float *fog_factor, struct lights_settings *ls)
3127 float fog_coord;
3129 if (ls->fog_mode == WINED3D_FOG_NONE)
3130 return;
3132 switch (ls->fog_coord_mode)
3134 case WINED3D_FFP_VS_FOG_RANGE:
3135 fog_coord = sqrtf(wined3d_vec3_dot((const struct wined3d_vec3 *)&ls->position_transformed,
3136 (const struct wined3d_vec3 *)&ls->position_transformed));
3137 break;
3139 case WINED3D_FFP_VS_FOG_DEPTH:
3140 fog_coord = fabsf(ls->position_transformed.z);
3141 break;
3143 default:
3144 ERR("Unhandled fog coordinate mode %#x.\n", ls->fog_coord_mode);
3145 return;
3147 *fog_factor = wined3d_calculate_fog_factor(fog_coord, ls);
3150 /* Context activation is done by the caller. */
3151 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3152 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3153 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags, DWORD dst_fvf)
3155 enum wined3d_material_color_source diffuse_source, specular_source, ambient_source, emissive_source;
3156 const struct wined3d_color *material_specular_state_colour;
3157 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3158 const struct wined3d_state *state = device->cs->c.state;
3159 const struct wined3d_format *output_colour_format;
3160 static const struct wined3d_color black;
3161 struct wined3d_map_desc map_desc;
3162 struct wined3d_box box = {0};
3163 struct wined3d_viewport vp;
3164 unsigned int texture_count;
3165 struct lights_settings ls;
3166 unsigned int vertex_size;
3167 BOOL do_clip, lighting;
3168 float min_z, max_z;
3169 unsigned int i;
3170 BYTE *dest_ptr;
3171 HRESULT hr;
3173 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3175 ERR("Source has no position mask.\n");
3176 return WINED3DERR_INVALIDCALL;
3179 if (state->render_states[WINED3D_RS_CLIPPING])
3181 static BOOL warned = FALSE;
3183 * The clipping code is not quite correct. Some things need
3184 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3185 * so disable clipping for now.
3186 * (The graphics in Half-Life are broken, and my processvertices
3187 * test crashes with IDirect3DDevice3)
3188 do_clip = TRUE;
3190 do_clip = FALSE;
3191 if (!warned)
3193 warned = TRUE;
3194 FIXME("Clipping is broken and disabled for now\n");
3197 else
3198 do_clip = FALSE;
3200 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3201 box.left = dwDestIndex * vertex_size;
3202 box.right = box.left + dwCount * vertex_size;
3203 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3205 WARN("Failed to map buffer, hr %#x.\n", hr);
3206 return hr;
3208 dest_ptr = map_desc.data;
3210 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3211 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3212 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3214 TRACE("View mat:\n");
3215 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3216 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3217 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3218 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3220 TRACE("Proj mat:\n");
3221 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3222 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3223 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3224 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3226 TRACE("World mat:\n");
3227 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3228 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3229 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3230 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3232 /* Get the viewport */
3233 wined3d_device_context_get_viewports(&device->cs->c, NULL, &vp);
3234 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3235 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3237 multiply_matrix(&mat,&view_mat,&world_mat);
3238 multiply_matrix(&mat,&proj_mat,&mat);
3240 texture_count = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3242 lighting = state->render_states[WINED3D_RS_LIGHTING]
3243 && (dst_fvf & (WINED3DFVF_DIFFUSE | WINED3DFVF_SPECULAR));
3244 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
3245 &ambient_source, &specular_source, state, stream_info);
3246 output_colour_format = wined3d_get_format(device->adapter, WINED3DFMT_B8G8R8A8_UNORM, 0);
3247 material_specular_state_colour = state->render_states[WINED3D_RS_SPECULARENABLE]
3248 ? &state->material.specular : &black;
3249 init_transformed_lights(&ls, state, device->adapter->d3d_info.wined3d_creation_flags
3250 & WINED3D_LEGACY_FFP_LIGHTING, lighting);
3252 wined3d_viewport_get_z_range(&vp, &min_z, &max_z);
3254 for (i = 0; i < dwCount; ++i)
3256 const struct wined3d_stream_info_element *position_element = &stream_info->elements[WINED3D_FFP_POSITION];
3257 const float *p = (const float *)&position_element->data.addr[i * position_element->stride];
3258 struct wined3d_color ambient, diffuse, specular;
3259 struct wined3d_vec4 position;
3260 unsigned int tex_index;
3262 position.x = p[0];
3263 position.y = p[1];
3264 position.z = p[2];
3265 position.w = 1.0f;
3267 light_set_vertex_data(&ls, &position);
3269 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3270 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3271 /* The position first */
3272 float x, y, z, rhw;
3273 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3275 /* Multiplication with world, view and projection matrix. */
3276 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3277 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3278 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3279 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3281 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3283 /* WARNING: The following things are taken from d3d7 and were not yet checked
3284 * against d3d8 or d3d9!
3287 /* Clipping conditions: From msdn
3289 * A vertex is clipped if it does not match the following requirements
3290 * -rhw < x <= rhw
3291 * -rhw < y <= rhw
3292 * 0 < z <= rhw
3293 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3295 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3296 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3300 if (!do_clip || (-rhw - eps < x && -rhw - eps < y && -eps < z && x <= rhw + eps
3301 && y <= rhw + eps && z <= rhw + eps && rhw > eps))
3303 /* "Normal" viewport transformation (not clipped)
3304 * 1) The values are divided by rhw
3305 * 2) The y axis is negative, so multiply it with -1
3306 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3307 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3308 * 4) Multiply x with Width/2 and add Width/2
3309 * 5) The same for the height
3310 * 6) Add the viewpoint X and Y to the 2D coordinates and
3311 * The minimum Z value to z
3312 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3314 * Well, basically it's simply a linear transformation into viewport
3315 * coordinates
3318 x /= rhw;
3319 y /= rhw;
3320 z /= rhw;
3322 y *= -1;
3324 x *= vp.width / 2;
3325 y *= vp.height / 2;
3326 z *= max_z - min_z;
3328 x += vp.width / 2 + vp.x;
3329 y += vp.height / 2 + vp.y;
3330 z += min_z;
3332 rhw = 1 / rhw;
3333 } else {
3334 /* That vertex got clipped
3335 * Contrary to OpenGL it is not dropped completely, it just
3336 * undergoes a different calculation.
3338 TRACE("Vertex got clipped\n");
3339 x += rhw;
3340 y += rhw;
3342 x /= 2;
3343 y /= 2;
3345 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3346 * outside of the main vertex buffer memory. That needs some more
3347 * investigation...
3351 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3354 ( (float *) dest_ptr)[0] = x;
3355 ( (float *) dest_ptr)[1] = y;
3356 ( (float *) dest_ptr)[2] = z;
3357 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3359 dest_ptr += 3 * sizeof(float);
3361 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3362 dest_ptr += sizeof(float);
3365 if (dst_fvf & WINED3DFVF_PSIZE)
3366 dest_ptr += sizeof(DWORD);
3368 if (dst_fvf & WINED3DFVF_NORMAL)
3370 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3371 const float *normal = (const float *)(element->data.addr + i * element->stride);
3372 /* AFAIK this should go into the lighting information */
3373 FIXME("Didn't expect the destination to have a normal\n");
3374 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3377 if (lighting)
3379 const struct wined3d_stream_info_element *element;
3380 struct wined3d_vec3 *normal;
3382 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3384 element = &stream_info->elements[WINED3D_FFP_NORMAL];
3385 normal = (struct wined3d_vec3 *)&element->data.addr[i * element->stride];
3387 else
3389 normal = NULL;
3391 compute_light(&ambient, &diffuse, &specular, &ls, normal,
3392 state->render_states[WINED3D_RS_SPECULARENABLE] ? state->material.power : 0.0f);
3395 if (dst_fvf & WINED3DFVF_DIFFUSE)
3397 struct wined3d_color material_diffuse, material_ambient, material_emissive, diffuse_colour;
3399 wined3d_colour_from_mcs(&material_diffuse, diffuse_source,
3400 &state->material.diffuse, i, stream_info);
3402 if (lighting)
3404 wined3d_colour_from_mcs(&material_ambient, ambient_source,
3405 &state->material.ambient, i, stream_info);
3406 wined3d_colour_from_mcs(&material_emissive, emissive_source,
3407 &state->material.emissive, i, stream_info);
3409 diffuse_colour.r = ambient.r * material_ambient.r
3410 + diffuse.r * material_diffuse.r + material_emissive.r;
3411 diffuse_colour.g = ambient.g * material_ambient.g
3412 + diffuse.g * material_diffuse.g + material_emissive.g;
3413 diffuse_colour.b = ambient.b * material_ambient.b
3414 + diffuse.b * material_diffuse.b + material_emissive.b;
3415 diffuse_colour.a = material_diffuse.a;
3417 else
3419 diffuse_colour = material_diffuse;
3421 wined3d_color_clamp(&diffuse_colour, &diffuse_colour, 0.0f, 1.0f);
3422 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &diffuse_colour);
3423 dest_ptr += sizeof(DWORD);
3426 if (dst_fvf & WINED3DFVF_SPECULAR)
3428 struct wined3d_color material_specular, specular_colour;
3430 wined3d_colour_from_mcs(&material_specular, specular_source,
3431 material_specular_state_colour, i, stream_info);
3433 if (lighting)
3435 specular_colour.r = specular.r * material_specular.r;
3436 specular_colour.g = specular.g * material_specular.g;
3437 specular_colour.b = specular.b * material_specular.b;
3438 specular_colour.a = ls.legacy_lighting ? 0.0f : material_specular.a;
3440 else
3442 specular_colour = material_specular;
3444 update_fog_factor(&specular_colour.a, &ls);
3445 wined3d_color_clamp(&specular_colour, &specular_colour, 0.0f, 1.0f);
3446 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &specular_colour);
3447 dest_ptr += sizeof(DWORD);
3450 for (tex_index = 0; tex_index < texture_count; ++tex_index)
3452 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3453 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3454 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3456 ERR("No source texture, but destination requests one\n");
3457 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3459 else
3461 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3466 wined3d_resource_unmap(&dest->resource, 0);
3468 return WINED3D_OK;
3470 #undef copy_and_next
3472 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3473 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3474 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3476 struct wined3d_state *state = device->cs->c.state;
3477 struct wined3d_stream_info stream_info;
3478 struct wined3d_resource *resource;
3479 struct wined3d_box box = {0};
3480 struct wined3d_shader *vs;
3481 unsigned int i, j;
3482 HRESULT hr;
3483 WORD map;
3485 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3486 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3487 device, src_start_idx, dst_idx, vertex_count,
3488 dst_buffer, declaration, flags, dst_fvf);
3490 if (declaration)
3491 FIXME("Output vertex declaration not implemented yet.\n");
3493 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3494 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3495 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->d3d_info);
3496 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3498 /* We can't convert FROM a VBO, and vertex buffers used to source into
3499 * process_vertices() are unlikely to ever be used for drawing. Release
3500 * VBOs in those buffers and fix up the stream_info structure.
3502 * Also apply the start index. */
3503 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3505 struct wined3d_stream_info_element *e;
3506 struct wined3d_map_desc map_desc;
3508 if (!(map & 1))
3509 continue;
3511 e = &stream_info.elements[i];
3512 resource = &state->streams[e->stream_idx].buffer->resource;
3513 box.left = src_start_idx * e->stride;
3514 box.right = box.left + vertex_count * e->stride;
3515 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3517 ERR("Failed to map resource.\n");
3518 for (j = 0, map = stream_info.use_map; map && j < i; map >>= 1, ++j)
3520 if (!(map & 1))
3521 continue;
3523 e = &stream_info.elements[j];
3524 resource = &state->streams[e->stream_idx].buffer->resource;
3525 if (FAILED(wined3d_resource_unmap(resource, 0)))
3526 ERR("Failed to unmap resource.\n");
3528 return WINED3DERR_INVALIDCALL;
3530 e->data.buffer_object = 0;
3531 e->data.addr += (ULONG_PTR)map_desc.data;
3534 hr = process_vertices_strided(device, dst_idx, vertex_count,
3535 &stream_info, dst_buffer, flags, dst_fvf);
3537 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3539 if (!(map & 1))
3540 continue;
3542 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3543 if (FAILED(wined3d_resource_unmap(resource, 0)))
3544 ERR("Failed to unmap resource.\n");
3547 return hr;
3550 static void wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3551 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3553 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3555 TRACE("device %p, stage %u, state %s, value %#x.\n",
3556 device, stage, debug_d3dtexturestate(state), value);
3558 if (stage >= d3d_info->limits.ffp_blend_stages)
3560 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3561 stage, d3d_info->limits.ffp_blend_stages - 1);
3562 return;
3565 if (value == device->cs->c.state->texture_states[stage][state])
3567 TRACE("Application is setting the old value over, nothing to do.\n");
3568 return;
3571 device->cs->c.state->texture_states[stage][state] = value;
3573 wined3d_device_context_emit_set_texture_state(&device->cs->c, stage, state, value);
3576 static void wined3d_device_set_texture(struct wined3d_device *device,
3577 UINT stage, struct wined3d_texture *texture)
3579 struct wined3d_state *state = device->cs->c.state;
3580 struct wined3d_texture *prev;
3582 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3584 /* Windows accepts overflowing this array... we do not. */
3585 if (stage >= ARRAY_SIZE(state->textures))
3587 WARN("Ignoring invalid stage %u.\n", stage);
3588 return;
3591 prev = state->textures[stage];
3592 TRACE("Previous texture %p.\n", prev);
3594 if (texture == prev)
3596 TRACE("App is setting the same texture again, nothing to do.\n");
3597 return;
3600 TRACE("Setting new texture to %p.\n", texture);
3601 state->textures[stage] = texture;
3603 if (texture)
3604 wined3d_texture_incref(texture);
3605 wined3d_device_context_emit_set_texture(&device->cs->c, stage, texture);
3606 if (prev)
3607 wined3d_texture_decref(prev);
3609 return;
3612 void CDECL wined3d_device_apply_stateblock(struct wined3d_device *device,
3613 struct wined3d_stateblock *stateblock)
3615 BOOL set_blend_state = FALSE, set_depth_stencil_state = FALSE, set_rasterizer_state = FALSE;
3616 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
3617 const struct wined3d_saved_states *changed = &stateblock->changed;
3618 const unsigned int word_bit_count = sizeof(DWORD) * CHAR_BIT;
3619 struct wined3d_device_context *context = &device->cs->c;
3620 unsigned int i, j, start, idx;
3621 struct wined3d_range range;
3622 uint32_t map;
3624 TRACE("device %p, stateblock %p.\n", device, stateblock);
3626 if (changed->vertexShader)
3627 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_VERTEX, state->vs);
3628 if (changed->pixelShader)
3629 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_PIXEL, state->ps);
3631 for (start = 0; ; start = range.offset + range.size)
3633 if (!wined3d_bitmap_get_range(changed->vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
3634 break;
3636 wined3d_device_set_vs_consts_f(device, range.offset, range.size, &state->vs_consts_f[range.offset]);
3639 map = changed->vertexShaderConstantsI;
3640 for (start = 0; ; start = range.offset + range.size)
3642 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3643 break;
3645 wined3d_device_set_vs_consts_i(device, range.offset, range.size, &state->vs_consts_i[range.offset]);
3648 map = changed->vertexShaderConstantsB;
3649 for (start = 0; ; start = range.offset + range.size)
3651 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3652 break;
3654 wined3d_device_set_vs_consts_b(device, range.offset, range.size, &state->vs_consts_b[range.offset]);
3657 for (start = 0; ; start = range.offset + range.size)
3659 if (!wined3d_bitmap_get_range(changed->ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
3660 break;
3662 wined3d_device_set_ps_consts_f(device, range.offset, range.size, &state->ps_consts_f[range.offset]);
3665 map = changed->pixelShaderConstantsI;
3666 for (start = 0; ; start = range.offset + range.size)
3668 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3669 break;
3671 wined3d_device_set_ps_consts_i(device, range.offset, range.size, &state->ps_consts_i[range.offset]);
3674 map = changed->pixelShaderConstantsB;
3675 for (start = 0; ; start = range.offset + range.size)
3677 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3678 break;
3680 wined3d_device_set_ps_consts_b(device, range.offset, range.size, &state->ps_consts_b[range.offset]);
3683 if (changed->lights)
3685 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); ++i)
3687 const struct wined3d_light_info *light;
3689 LIST_FOR_EACH_ENTRY(light, &state->light_state->light_map[i], struct wined3d_light_info, entry)
3691 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
3692 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
3697 for (i = 0; i < ARRAY_SIZE(changed->renderState); ++i)
3699 map = changed->renderState[i];
3700 while (map)
3702 j = wined3d_bit_scan(&map);
3703 idx = i * word_bit_count + j;
3705 switch (idx)
3707 case WINED3D_RS_BLENDFACTOR:
3708 case WINED3D_RS_MULTISAMPLEMASK:
3709 case WINED3D_RS_ALPHABLENDENABLE:
3710 case WINED3D_RS_SRCBLEND:
3711 case WINED3D_RS_DESTBLEND:
3712 case WINED3D_RS_BLENDOP:
3713 case WINED3D_RS_SEPARATEALPHABLENDENABLE:
3714 case WINED3D_RS_SRCBLENDALPHA:
3715 case WINED3D_RS_DESTBLENDALPHA:
3716 case WINED3D_RS_BLENDOPALPHA:
3717 case WINED3D_RS_COLORWRITEENABLE:
3718 case WINED3D_RS_COLORWRITEENABLE1:
3719 case WINED3D_RS_COLORWRITEENABLE2:
3720 case WINED3D_RS_COLORWRITEENABLE3:
3721 set_blend_state = TRUE;
3722 break;
3724 case WINED3D_RS_BACK_STENCILFAIL:
3725 case WINED3D_RS_BACK_STENCILFUNC:
3726 case WINED3D_RS_BACK_STENCILPASS:
3727 case WINED3D_RS_BACK_STENCILZFAIL:
3728 case WINED3D_RS_STENCILENABLE:
3729 case WINED3D_RS_STENCILFAIL:
3730 case WINED3D_RS_STENCILFUNC:
3731 case WINED3D_RS_STENCILREF:
3732 case WINED3D_RS_STENCILMASK:
3733 case WINED3D_RS_STENCILPASS:
3734 case WINED3D_RS_STENCILWRITEMASK:
3735 case WINED3D_RS_STENCILZFAIL:
3736 case WINED3D_RS_TWOSIDEDSTENCILMODE:
3737 case WINED3D_RS_ZENABLE:
3738 case WINED3D_RS_ZFUNC:
3739 case WINED3D_RS_ZWRITEENABLE:
3740 set_depth_stencil_state = TRUE;
3741 break;
3743 case WINED3D_RS_FILLMODE:
3744 case WINED3D_RS_CULLMODE:
3745 case WINED3D_RS_SLOPESCALEDEPTHBIAS:
3746 case WINED3D_RS_DEPTHBIAS:
3747 case WINED3D_RS_SCISSORTESTENABLE:
3748 case WINED3D_RS_ANTIALIASEDLINEENABLE:
3749 set_rasterizer_state = TRUE;
3750 break;
3752 default:
3753 wined3d_device_set_render_state(device, idx, state->rs[idx]);
3754 break;
3759 if (set_rasterizer_state)
3761 struct wined3d_rasterizer_state *rasterizer_state;
3762 struct wined3d_rasterizer_state_desc desc;
3763 struct wine_rb_entry *entry;
3764 union
3766 DWORD d;
3767 float f;
3768 } bias;
3770 memset(&desc, 0, sizeof(desc));
3771 desc.fill_mode = state->rs[WINED3D_RS_FILLMODE];
3772 desc.cull_mode = state->rs[WINED3D_RS_CULLMODE];
3773 bias.d = state->rs[WINED3D_RS_DEPTHBIAS];
3774 desc.depth_bias = bias.f;
3775 bias.d = state->rs[WINED3D_RS_SLOPESCALEDEPTHBIAS];
3776 desc.scale_bias = bias.f;
3777 desc.depth_clip = TRUE;
3778 desc.scissor = state->rs[WINED3D_RS_SCISSORTESTENABLE];
3779 desc.line_antialias = state->rs[WINED3D_RS_ANTIALIASEDLINEENABLE];
3781 if ((entry = wine_rb_get(&device->rasterizer_states, &desc)))
3783 rasterizer_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
3784 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
3786 else if (SUCCEEDED(wined3d_rasterizer_state_create(device, &desc, NULL,
3787 &wined3d_null_parent_ops, &rasterizer_state)))
3789 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
3790 if (wine_rb_put(&device->rasterizer_states, &desc, &rasterizer_state->entry) == -1)
3792 ERR("Failed to insert rasterizer state.\n");
3793 wined3d_rasterizer_state_decref(rasterizer_state);
3798 if (set_blend_state || changed->alpha_to_coverage
3799 || wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_ADAPTIVETESS_Y))
3801 struct wined3d_blend_state *blend_state;
3802 struct wined3d_blend_state_desc desc;
3803 struct wine_rb_entry *entry;
3804 struct wined3d_color colour;
3805 unsigned int sample_mask;
3807 memset(&desc, 0, sizeof(desc));
3808 desc.alpha_to_coverage = state->alpha_to_coverage;
3809 desc.independent = FALSE;
3810 if (state->rs[WINED3D_RS_ADAPTIVETESS_Y] == WINED3DFMT_ATOC)
3811 desc.alpha_to_coverage = TRUE;
3812 desc.rt[0].enable = state->rs[WINED3D_RS_ALPHABLENDENABLE];
3813 desc.rt[0].src = state->rs[WINED3D_RS_SRCBLEND];
3814 desc.rt[0].dst = state->rs[WINED3D_RS_DESTBLEND];
3815 desc.rt[0].op = state->rs[WINED3D_RS_BLENDOP];
3816 if (state->rs[WINED3D_RS_SEPARATEALPHABLENDENABLE])
3818 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLENDALPHA];
3819 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLENDALPHA];
3820 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOPALPHA];
3822 else
3824 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLEND];
3825 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLEND];
3826 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOP];
3828 desc.rt[0].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE];
3829 desc.rt[1].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE1];
3830 desc.rt[2].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE2];
3831 desc.rt[3].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE3];
3832 if (desc.rt[1].writemask != desc.rt[0].writemask
3833 || desc.rt[2].writemask != desc.rt[0].writemask
3834 || desc.rt[3].writemask != desc.rt[0].writemask)
3836 desc.independent = TRUE;
3837 for (i = 1; i < 4; ++i)
3839 desc.rt[i].enable = desc.rt[0].enable;
3840 desc.rt[i].src = desc.rt[0].src;
3841 desc.rt[i].dst = desc.rt[0].dst;
3842 desc.rt[i].op = desc.rt[0].op;
3843 desc.rt[i].src_alpha = desc.rt[0].src_alpha;
3844 desc.rt[i].dst_alpha = desc.rt[0].dst_alpha;
3845 desc.rt[i].op_alpha = desc.rt[0].op_alpha;
3849 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_BLENDFACTOR))
3850 wined3d_color_from_d3dcolor(&colour, state->rs[WINED3D_RS_BLENDFACTOR]);
3851 else
3852 wined3d_device_context_get_blend_state(context, &colour, &sample_mask);
3854 if ((entry = wine_rb_get(&device->blend_states, &desc)))
3856 blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
3857 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3858 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3860 else if (SUCCEEDED(wined3d_blend_state_create(device, &desc, NULL,
3861 &wined3d_null_parent_ops, &blend_state)))
3863 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3864 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3865 if (wine_rb_put(&device->blend_states, &desc, &blend_state->entry) == -1)
3867 ERR("Failed to insert blend state.\n");
3868 wined3d_blend_state_decref(blend_state);
3873 if (set_depth_stencil_state)
3875 struct wined3d_depth_stencil_state *depth_stencil_state;
3876 struct wined3d_depth_stencil_state_desc desc;
3877 struct wine_rb_entry *entry;
3878 unsigned int stencil_ref;
3880 memset(&desc, 0, sizeof(desc));
3881 switch (state->rs[WINED3D_RS_ZENABLE])
3883 case WINED3D_ZB_FALSE:
3884 desc.depth = FALSE;
3885 break;
3887 case WINED3D_ZB_USEW:
3888 FIXME("W buffer is not well handled.\n");
3889 case WINED3D_ZB_TRUE:
3890 desc.depth = TRUE;
3891 break;
3893 default:
3894 FIXME("Unrecognized depth buffer type %#x.\n", state->rs[WINED3D_RS_ZENABLE]);
3896 desc.depth_write = state->rs[WINED3D_RS_ZWRITEENABLE];
3897 desc.depth_func = state->rs[WINED3D_RS_ZFUNC];
3898 desc.stencil = state->rs[WINED3D_RS_STENCILENABLE];
3899 desc.stencil_read_mask = state->rs[WINED3D_RS_STENCILMASK];
3900 desc.stencil_write_mask = state->rs[WINED3D_RS_STENCILWRITEMASK];
3901 desc.front.fail_op = state->rs[WINED3D_RS_STENCILFAIL];
3902 desc.front.depth_fail_op = state->rs[WINED3D_RS_STENCILZFAIL];
3903 desc.front.pass_op = state->rs[WINED3D_RS_STENCILPASS];
3904 desc.front.func = state->rs[WINED3D_RS_STENCILFUNC];
3906 if (state->rs[WINED3D_RS_TWOSIDEDSTENCILMODE])
3908 desc.back.fail_op = state->rs[WINED3D_RS_BACK_STENCILFAIL];
3909 desc.back.depth_fail_op = state->rs[WINED3D_RS_BACK_STENCILZFAIL];
3910 desc.back.pass_op = state->rs[WINED3D_RS_BACK_STENCILPASS];
3911 desc.back.func = state->rs[WINED3D_RS_BACK_STENCILFUNC];
3913 else
3915 desc.back = desc.front;
3918 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_STENCILREF))
3919 stencil_ref = state->rs[WINED3D_RS_STENCILREF];
3920 else
3921 wined3d_device_context_get_depth_stencil_state(context, &stencil_ref);
3923 if ((entry = wine_rb_get(&device->depth_stencil_states, &desc)))
3925 depth_stencil_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
3926 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3928 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device, &desc, NULL,
3929 &wined3d_null_parent_ops, &depth_stencil_state)))
3931 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3932 if (wine_rb_put(&device->depth_stencil_states, &desc, &depth_stencil_state->entry) == -1)
3934 ERR("Failed to insert depth/stencil state.\n");
3935 wined3d_depth_stencil_state_decref(depth_stencil_state);
3940 for (i = 0; i < ARRAY_SIZE(changed->textureState); ++i)
3942 map = changed->textureState[i];
3943 while (map)
3945 j = wined3d_bit_scan(&map);
3946 wined3d_device_set_texture_stage_state(device, i, j, state->texture_states[i][j]);
3950 for (i = 0; i < ARRAY_SIZE(changed->samplerState); ++i)
3952 map = changed->samplerState[i];
3953 while (map)
3955 j = wined3d_bit_scan(&map);
3956 wined3d_device_set_sampler_state(device, i, j, state->sampler_states[i][j]);
3960 if (changed->transforms)
3962 for (i = 0; i < ARRAY_SIZE(changed->transform); ++i)
3964 map = changed->transform[i];
3965 while (map)
3967 j = wined3d_bit_scan(&map);
3968 idx = i * word_bit_count + j;
3969 wined3d_device_set_transform(device, idx, &state->transforms[idx]);
3974 if (changed->indices)
3975 wined3d_device_context_set_index_buffer(context, state->index_buffer, state->index_format, 0);
3976 wined3d_device_set_base_vertex_index(device, state->base_vertex_index);
3977 if (changed->vertexDecl)
3978 wined3d_device_context_set_vertex_declaration(context, state->vertex_declaration);
3979 if (changed->material)
3980 wined3d_device_set_material(device, &state->material);
3981 if (changed->viewport)
3982 wined3d_device_context_set_viewports(context, 1, &state->viewport);
3983 if (changed->scissorRect)
3984 wined3d_device_context_set_scissor_rects(context, 1, &state->scissor_rect);
3986 map = changed->streamSource | changed->streamFreq;
3987 while (map)
3989 i = wined3d_bit_scan(&map);
3990 wined3d_device_context_set_stream_sources(context, i, 1, &state->streams[i]);
3993 map = changed->textures;
3994 while (map)
3996 i = wined3d_bit_scan(&map);
3997 wined3d_device_set_texture(device, i, state->textures[i]);
4000 map = changed->clipplane;
4001 while (map)
4003 i = wined3d_bit_scan(&map);
4004 wined3d_device_set_clip_plane(device, i, &state->clip_planes[i]);
4007 memset(&stateblock->changed, 0, sizeof(stateblock->changed));
4009 TRACE("Applied stateblock %p.\n", stateblock);
4012 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
4014 TRACE("device %p, caps %p.\n", device, caps);
4016 return wined3d_get_device_caps(device->adapter, device->create_parms.device_type, caps);
4019 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
4020 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
4022 struct wined3d_swapchain *swapchain;
4024 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
4025 device, swapchain_idx, mode, rotation);
4027 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4028 return WINED3DERR_INVALIDCALL;
4030 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
4033 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
4035 /* At the moment we have no need for any functionality at the beginning
4036 * of a scene. */
4037 TRACE("device %p.\n", device);
4039 if (device->inScene)
4041 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
4042 return WINED3DERR_INVALIDCALL;
4044 device->inScene = TRUE;
4045 return WINED3D_OK;
4048 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
4050 TRACE("device %p.\n", device);
4052 if (!device->inScene)
4054 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
4055 return WINED3DERR_INVALIDCALL;
4058 device->inScene = FALSE;
4059 return WINED3D_OK;
4062 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
4063 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
4065 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
4067 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
4068 device, rect_count, rects, flags, debug_color(color), depth, stencil);
4070 if (!rect_count && rects)
4072 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
4073 return WINED3D_OK;
4076 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4078 struct wined3d_rendertarget_view *ds = fb->depth_stencil;
4079 if (!ds)
4081 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4082 /* TODO: What about depth stencil buffers without stencil bits? */
4083 return WINED3DERR_INVALIDCALL;
4085 else if (flags & WINED3DCLEAR_TARGET)
4087 if (ds->width < fb->render_targets[0]->width
4088 || ds->height < fb->render_targets[0]->height)
4090 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4091 return WINED3D_OK;
4096 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
4098 return WINED3D_OK;
4101 struct wined3d_query * CDECL wined3d_device_context_get_predication(struct wined3d_device_context *context, BOOL *value)
4103 struct wined3d_state *state = context->state;
4105 TRACE("context %p, value %p.\n", context, value);
4107 if (value)
4108 *value = state->predicate_value;
4109 return state->predicate;
4112 void CDECL wined3d_device_context_set_primitive_type(struct wined3d_device_context *context,
4113 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
4115 struct wined3d_state *state = context->state;
4117 TRACE("context %p, primitive_type %s, patch_vertex_count %u.\n",
4118 context, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
4120 wined3d_mutex_lock();
4121 state->primitive_type = primitive_type;
4122 state->patch_vertex_count = patch_vertex_count;
4123 wined3d_mutex_unlock();
4126 void CDECL wined3d_device_context_get_primitive_type(const struct wined3d_device_context *context,
4127 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
4129 const struct wined3d_state *state = context->state;
4131 TRACE("context %p, primitive_type %p, patch_vertex_count %p.\n",
4132 context, primitive_type, patch_vertex_count);
4134 *primitive_type = state->primitive_type;
4135 if (patch_vertex_count)
4136 *patch_vertex_count = state->patch_vertex_count;
4138 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
4141 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4142 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4144 unsigned int src_size, dst_size, src_skip_levels = 0;
4145 unsigned int src_level_count, dst_level_count;
4146 const struct wined3d_dirty_regions *regions;
4147 unsigned int layer_count, level_count, i, j;
4148 enum wined3d_resource_type type;
4149 BOOL entire_texture = TRUE;
4150 struct wined3d_box box;
4152 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4154 /* Verify that the source and destination textures are non-NULL. */
4155 if (!src_texture || !dst_texture)
4157 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4158 return WINED3DERR_INVALIDCALL;
4161 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
4162 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
4164 WARN("Source resource is GPU accessible or a scratch resource.\n");
4165 return WINED3DERR_INVALIDCALL;
4167 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
4169 WARN("Destination resource is CPU accessible.\n");
4170 return WINED3DERR_INVALIDCALL;
4173 /* Verify that the source and destination textures are the same type. */
4174 type = src_texture->resource.type;
4175 if (dst_texture->resource.type != type)
4177 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4178 return WINED3DERR_INVALIDCALL;
4181 layer_count = src_texture->layer_count;
4182 if (layer_count != dst_texture->layer_count)
4184 WARN("Source and destination have different layer counts.\n");
4185 return WINED3DERR_INVALIDCALL;
4188 if (src_texture->resource.format != dst_texture->resource.format)
4190 WARN("Source and destination formats do not match.\n");
4191 return WINED3DERR_INVALIDCALL;
4194 src_level_count = src_texture->level_count;
4195 dst_level_count = dst_texture->level_count;
4196 level_count = min(src_level_count, dst_level_count);
4198 src_size = max(src_texture->resource.width, src_texture->resource.height);
4199 src_size = max(src_size, src_texture->resource.depth);
4200 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
4201 dst_size = max(dst_size, dst_texture->resource.depth);
4202 while (src_size > dst_size)
4204 src_size >>= 1;
4205 ++src_skip_levels;
4208 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
4209 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
4210 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
4212 WARN("Source and destination dimensions do not match.\n");
4213 return WINED3DERR_INVALIDCALL;
4216 if ((regions = src_texture->dirty_regions))
4218 for (i = 0; i < layer_count && entire_texture; ++i)
4220 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4221 continue;
4223 entire_texture = FALSE;
4224 break;
4228 /* Update every surface level of the texture. */
4229 if (entire_texture)
4231 for (i = 0; i < level_count; ++i)
4233 wined3d_texture_get_level_box(dst_texture, i, &box);
4234 for (j = 0; j < layer_count; ++j)
4236 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4237 &dst_texture->resource, j * dst_level_count + i, &box,
4238 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
4239 0, NULL, WINED3D_TEXF_POINT);
4243 else
4245 unsigned int src_level, box_count, k;
4246 const struct wined3d_box *boxes;
4247 struct wined3d_box b;
4249 for (i = 0; i < layer_count; ++i)
4251 boxes = regions[i].boxes;
4252 box_count = regions[i].box_count;
4253 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4255 boxes = &b;
4256 box_count = 1;
4257 wined3d_texture_get_level_box(dst_texture, i, &b);
4260 for (j = 0; j < level_count; ++j)
4262 src_level = j + src_skip_levels;
4264 /* TODO: We could pass an array of boxes here to avoid
4265 * multiple context acquisitions for the same resource. */
4266 for (k = 0; k < box_count; ++k)
4268 box = boxes[k];
4269 if (src_level)
4271 box.left >>= src_level;
4272 box.top >>= src_level;
4273 box.right = min((box.right + (1u << src_level) - 1) >> src_level,
4274 wined3d_texture_get_level_width(src_texture, src_level));
4275 box.bottom = min((box.bottom + (1u << src_level) - 1) >> src_level,
4276 wined3d_texture_get_level_height(src_texture, src_level));
4277 box.front >>= src_level;
4278 box.back = min((box.back + (1u << src_level) - 1) >> src_level,
4279 wined3d_texture_get_level_depth(src_texture, src_level));
4282 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4283 &dst_texture->resource, i * dst_level_count + j, &box,
4284 &src_texture->resource, i * src_level_count + src_level, &box,
4285 0, NULL, WINED3D_TEXF_POINT);
4291 wined3d_texture_clear_dirty_regions(src_texture);
4293 return WINED3D_OK;
4296 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4298 const struct wined3d_state *state = device->cs->c.state;
4299 struct wined3d_texture *texture;
4300 DWORD i;
4302 TRACE("device %p, num_passes %p.\n", device, num_passes);
4304 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
4306 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4308 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4309 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4311 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4313 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4314 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4317 texture = state->textures[i];
4318 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
4320 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4322 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
4323 return E_FAIL;
4325 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4327 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
4328 return E_FAIL;
4330 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4331 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4333 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
4334 return E_FAIL;
4338 if (wined3d_state_uses_depth_buffer(state)
4339 || (state->depth_stencil_state && state->depth_stencil_state->desc.stencil))
4341 struct wined3d_rendertarget_view *rt = state->fb.render_targets[0];
4342 struct wined3d_rendertarget_view *ds = state->fb.depth_stencil;
4344 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
4346 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4347 return WINED3DERR_CONFLICTINGRENDERSTATE;
4351 /* return a sensible default */
4352 *num_passes = 1;
4354 TRACE("returning D3D_OK\n");
4355 return WINED3D_OK;
4358 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4360 static BOOL warned;
4362 TRACE("device %p, software %#x.\n", device, software);
4364 if (!warned)
4366 FIXME("device %p, software %#x stub!\n", device, software);
4367 warned = TRUE;
4370 device->softwareVertexProcessing = software;
4373 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4375 static BOOL warned;
4377 TRACE("device %p.\n", device);
4379 if (!warned)
4381 TRACE("device %p stub!\n", device);
4382 warned = TRUE;
4385 return device->softwareVertexProcessing;
4388 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4389 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4391 struct wined3d_swapchain *swapchain;
4393 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4394 device, swapchain_idx, raster_status);
4396 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4397 return WINED3DERR_INVALIDCALL;
4399 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4402 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4404 static BOOL warned;
4406 TRACE("device %p, segments %.8e.\n", device, segments);
4408 if (segments != 0.0f)
4410 if (!warned)
4412 FIXME("device %p, segments %.8e stub!\n", device, segments);
4413 warned = TRUE;
4417 return WINED3D_OK;
4420 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4422 static BOOL warned;
4424 TRACE("device %p.\n", device);
4426 if (!warned)
4428 FIXME("device %p stub!\n", device);
4429 warned = TRUE;
4432 return 0.0f;
4435 void CDECL wined3d_device_context_copy_uav_counter(struct wined3d_device_context *context,
4436 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4438 TRACE("context %p, dst_buffer %p, offset %u, uav %p.\n",
4439 context, dst_buffer, offset, uav);
4441 wined3d_mutex_lock();
4442 wined3d_device_context_emit_copy_uav_counter(context, dst_buffer, offset, uav);
4443 wined3d_mutex_unlock();
4446 static bool resources_format_compatible(const struct wined3d_resource *src_resource,
4447 const struct wined3d_resource *dst_resource)
4449 if (src_resource->format->id == dst_resource->format->id)
4450 return true;
4451 if (src_resource->format->typeless_id && src_resource->format->typeless_id == dst_resource->format->typeless_id)
4452 return true;
4453 if (src_resource->device->cs->c.state->feature_level < WINED3D_FEATURE_LEVEL_10_1)
4454 return false;
4455 if ((src_resource->format_flags & WINED3DFMT_FLAG_BLOCKS)
4456 && (dst_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK))
4457 return src_resource->format->block_byte_count == dst_resource->format->byte_count;
4458 if ((src_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK)
4459 && (dst_resource->format_flags & WINED3DFMT_FLAG_BLOCKS))
4460 return src_resource->format->byte_count == dst_resource->format->block_byte_count;
4461 return false;
4464 void CDECL wined3d_device_context_copy_resource(struct wined3d_device_context *context,
4465 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4467 unsigned int src_row_block_count, dst_row_block_count;
4468 struct wined3d_texture *dst_texture, *src_texture;
4469 unsigned int src_row_count, dst_row_count;
4470 struct wined3d_box src_box, dst_box;
4471 unsigned int i, j;
4473 TRACE("context %p, dst_resource %p, src_resource %p.\n", context, dst_resource, src_resource);
4475 if (src_resource == dst_resource)
4477 WARN("Source and destination are the same resource.\n");
4478 return;
4481 if (src_resource->type != dst_resource->type)
4483 WARN("Resource types (%s / %s) don't match.\n",
4484 debug_d3dresourcetype(dst_resource->type),
4485 debug_d3dresourcetype(src_resource->type));
4486 return;
4489 if (!resources_format_compatible(src_resource, dst_resource))
4491 WARN("Resource formats %s and %s are incompatible.\n",
4492 debug_d3dformat(dst_resource->format->id),
4493 debug_d3dformat(src_resource->format->id));
4494 return;
4497 src_row_block_count = (src_resource->width + (src_resource->format->block_width - 1))
4498 / src_resource->format->block_width;
4499 dst_row_block_count = (dst_resource->width + (dst_resource->format->block_width - 1))
4500 / dst_resource->format->block_width;
4501 src_row_count = (src_resource->height + (src_resource->format->block_height - 1))
4502 / src_resource->format->block_height;
4503 dst_row_count = (dst_resource->height + (dst_resource->format->block_height - 1))
4504 / dst_resource->format->block_height;
4506 if (src_row_block_count != dst_row_block_count || src_row_count != dst_row_count
4507 || src_resource->depth != dst_resource->depth)
4509 WARN("Resource block dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4510 dst_row_block_count, dst_row_count, dst_resource->depth,
4511 src_row_block_count, src_row_count, src_resource->depth);
4512 return;
4515 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4517 wined3d_box_set(&src_box, 0, 0, src_resource->size, 1, 0, 1);
4518 wined3d_mutex_lock();
4519 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, 0, &src_box,
4520 src_resource, 0, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4521 wined3d_mutex_unlock();
4522 return;
4525 dst_texture = texture_from_resource(dst_resource);
4526 src_texture = texture_from_resource(src_resource);
4528 if (src_texture->layer_count != dst_texture->layer_count
4529 || src_texture->level_count != dst_texture->level_count)
4531 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4532 dst_texture->layer_count, dst_texture->level_count,
4533 src_texture->layer_count, src_texture->level_count);
4534 return;
4537 wined3d_mutex_lock();
4538 for (i = 0; i < dst_texture->level_count; ++i)
4540 wined3d_texture_get_level_box(src_texture, i, &src_box);
4541 wined3d_texture_get_level_box(dst_texture, i, &dst_box);
4542 for (j = 0; j < dst_texture->layer_count; ++j)
4544 unsigned int idx = j * dst_texture->level_count + i;
4546 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, idx, &dst_box,
4547 src_resource, idx, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4550 wined3d_mutex_unlock();
4553 HRESULT CDECL wined3d_device_context_copy_sub_resource_region(struct wined3d_device_context *context,
4554 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4555 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4556 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
4558 struct wined3d_box dst_box, b;
4560 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4561 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4562 context, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4563 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
4565 if (flags)
4566 FIXME("Ignoring flags %#x.\n", flags);
4568 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4570 WARN("Source and destination are the same sub-resource.\n");
4571 return WINED3DERR_INVALIDCALL;
4574 if (!resources_format_compatible(src_resource, dst_resource))
4576 WARN("Resource formats %s and %s are incompatible.\n",
4577 debug_d3dformat(dst_resource->format->id),
4578 debug_d3dformat(src_resource->format->id));
4579 return WINED3DERR_INVALIDCALL;
4582 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4584 if (src_resource->type != WINED3D_RTYPE_BUFFER)
4586 WARN("Resource types (%s / %s) don't match.\n",
4587 debug_d3dresourcetype(dst_resource->type),
4588 debug_d3dresourcetype(src_resource->type));
4589 return WINED3DERR_INVALIDCALL;
4592 if (dst_sub_resource_idx)
4594 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4595 return WINED3DERR_INVALIDCALL;
4598 if (src_sub_resource_idx)
4600 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4601 return WINED3DERR_INVALIDCALL;
4604 if (!src_box)
4606 unsigned int dst_w;
4608 dst_w = dst_resource->size - dst_x;
4609 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4610 src_box = &b;
4612 else if ((src_box->left >= src_box->right
4613 || src_box->top >= src_box->bottom
4614 || src_box->front >= src_box->back))
4616 WARN("Invalid box %s specified.\n", debug_box(src_box));
4617 return WINED3DERR_INVALIDCALL;
4620 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4621 || src_box->right - src_box->left > dst_resource->size - dst_x)
4623 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4624 dst_x, src_box->left, src_box->right - src_box->left);
4625 return WINED3DERR_INVALIDCALL;
4628 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4630 else
4632 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4633 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4634 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4635 unsigned int src_row_block_count, src_row_count;
4637 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4639 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
4640 return WINED3DERR_INVALIDCALL;
4643 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
4645 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4646 return WINED3DERR_INVALIDCALL;
4649 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4651 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4652 return WINED3DERR_INVALIDCALL;
4655 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4657 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4658 return WINED3DERR_INVALIDCALL;
4661 if (!src_box)
4663 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
4665 src_w = wined3d_texture_get_level_width(src_texture, src_level);
4666 src_h = wined3d_texture_get_level_height(src_texture, src_level);
4667 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
4669 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4670 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
4671 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
4672 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
4674 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
4675 src_box = &b;
4677 else if (FAILED(wined3d_resource_check_box_dimensions(src_resource, src_sub_resource_idx, src_box)))
4679 WARN("Invalid source box %s.\n", debug_box(src_box));
4680 return WINED3DERR_INVALIDCALL;
4683 if (src_resource->format->block_width == dst_resource->format->block_width
4684 && src_resource->format->block_height == dst_resource->format->block_height)
4686 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4687 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
4689 else
4691 src_row_block_count = (src_box->right - src_box->left + src_resource->format->block_width - 1)
4692 / src_resource->format->block_width;
4693 src_row_count = (src_box->bottom - src_box->top + src_resource->format->block_height - 1)
4694 / src_resource->format->block_height;
4695 wined3d_box_set(&dst_box, dst_x, dst_y,
4696 dst_x + (src_row_block_count * dst_resource->format->block_width),
4697 dst_y + (src_row_count * dst_resource->format->block_height),
4698 dst_z, dst_z + (src_box->back - src_box->front));
4700 if (FAILED(wined3d_resource_check_box_dimensions(dst_resource, dst_sub_resource_idx, &dst_box)))
4702 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4703 return WINED3DERR_INVALIDCALL;
4707 wined3d_mutex_lock();
4708 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, dst_sub_resource_idx, &dst_box,
4709 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4710 wined3d_mutex_unlock();
4712 return WINED3D_OK;
4715 void CDECL wined3d_device_context_update_sub_resource(struct wined3d_device_context *context,
4716 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
4717 const void *data, unsigned int row_pitch, unsigned int depth_pitch, unsigned int flags)
4719 struct wined3d_sub_resource_desc desc;
4720 struct wined3d_box b;
4722 TRACE("context %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, flags %#x.\n",
4723 context, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
4725 if (flags)
4726 FIXME("Ignoring flags %#x.\n", flags);
4728 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
4730 WARN("Resource %p is not GPU accessible.\n", resource);
4731 return;
4734 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4735 return;
4737 if (!box)
4739 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4740 box = &b;
4742 else if (box->left >= box->right || box->right > desc.width
4743 || box->top >= box->bottom || box->bottom > desc.height
4744 || box->front >= box->back || box->back > desc.depth)
4746 WARN("Invalid box %s specified.\n", debug_box(box));
4747 return;
4750 wined3d_mutex_lock();
4751 wined3d_device_context_emit_update_sub_resource(context, resource,
4752 sub_resource_idx, box, data, row_pitch, depth_pitch);
4753 wined3d_mutex_unlock();
4756 void CDECL wined3d_device_context_resolve_sub_resource(struct wined3d_device_context *context,
4757 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
4758 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
4759 enum wined3d_format_id format_id)
4761 struct wined3d_texture *dst_texture, *src_texture;
4762 unsigned int dst_level, src_level;
4763 struct wined3d_blt_fx fx = {0};
4764 RECT dst_rect, src_rect;
4766 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, "
4767 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
4768 context, dst_resource, dst_sub_resource_idx,
4769 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
4771 if (wined3d_format_is_typeless(dst_resource->format)
4772 || wined3d_format_is_typeless(src_resource->format))
4774 FIXME("Multisample resolve is not fully supported for typeless formats "
4775 "(dst_format %s, src_format %s, format %s).\n",
4776 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
4777 debug_d3dformat(format_id));
4779 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4781 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
4782 return;
4784 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4786 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
4787 return;
4790 wined3d_mutex_lock();
4791 fx.resolve_format_id = format_id;
4793 dst_texture = texture_from_resource(dst_resource);
4794 src_texture = texture_from_resource(src_resource);
4796 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4797 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
4798 wined3d_texture_get_level_height(dst_texture, dst_level));
4799 src_level = src_sub_resource_idx % src_texture->level_count;
4800 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4801 wined3d_texture_get_level_height(src_texture, src_level));
4802 wined3d_device_context_blt(context, dst_texture, dst_sub_resource_idx, &dst_rect,
4803 src_texture, src_sub_resource_idx, &src_rect, 0, &fx, WINED3D_TEXF_POINT);
4804 wined3d_mutex_unlock();
4807 HRESULT CDECL wined3d_device_context_clear_rendertarget_view(struct wined3d_device_context *context,
4808 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
4809 const struct wined3d_color *color, float depth, unsigned int stencil)
4811 struct wined3d_resource *resource;
4812 RECT r;
4814 TRACE("context %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4815 context, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4817 if (!flags)
4818 return WINED3D_OK;
4820 resource = view->resource;
4821 if (resource->type == WINED3D_RTYPE_BUFFER)
4823 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4824 return WINED3DERR_INVALIDCALL;
4827 if (!rect)
4829 SetRect(&r, 0, 0, view->width, view->height);
4830 rect = &r;
4832 else
4834 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4835 HRESULT hr;
4837 if (FAILED(hr = wined3d_resource_check_box_dimensions(resource, view->sub_resource_idx, &b)))
4838 return hr;
4841 wined3d_mutex_lock();
4842 wined3d_device_context_emit_clear_rendertarget_view(context, view, rect, flags, color, depth, stencil);
4843 wined3d_mutex_unlock();
4845 return WINED3D_OK;
4848 void CDECL wined3d_device_context_clear_uav_float(struct wined3d_device_context *context,
4849 struct wined3d_unordered_access_view *view, const struct wined3d_vec4 *clear_value)
4851 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_vec4(clear_value));
4853 if (!(view->format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & (WINED3DFMT_FLAG_FLOAT | WINED3DFMT_FLAG_NORMALISED)))
4855 WARN("Not supported for view format %s.\n", debug_d3dformat(view->format->id));
4856 return;
4859 wined3d_mutex_lock();
4860 wined3d_device_context_emit_clear_uav(context, view, (const struct wined3d_uvec4 *)clear_value, true);
4861 wined3d_mutex_unlock();
4864 void CDECL wined3d_device_context_clear_uav_uint(struct wined3d_device_context *context,
4865 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
4867 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_uvec4(clear_value));
4869 wined3d_mutex_lock();
4870 wined3d_device_context_emit_clear_uav(context, view, clear_value, false);
4871 wined3d_mutex_unlock();
4874 static unsigned int sanitise_map_flags(const struct wined3d_resource *resource, unsigned int flags)
4876 /* Not all flags make sense together, but Windows never returns an error.
4877 * Catch the cases that could cause issues. */
4878 if (flags & WINED3D_MAP_READ)
4880 if (flags & WINED3D_MAP_DISCARD)
4882 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
4883 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4885 if (flags & WINED3D_MAP_NOOVERWRITE)
4887 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
4888 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4891 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4893 if (!(resource->usage & WINED3DUSAGE_DYNAMIC))
4895 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
4896 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4898 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4899 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4901 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
4902 flags &= ~WINED3D_MAP_DISCARD;
4906 return flags;
4909 HRESULT CDECL wined3d_device_context_map(struct wined3d_device_context *context,
4910 struct wined3d_resource *resource, unsigned int sub_resource_idx,
4911 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
4913 struct wined3d_sub_resource_desc desc;
4914 struct wined3d_box b;
4915 HRESULT hr;
4917 TRACE("context %p, resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
4918 context, resource, sub_resource_idx, map_desc, debug_box(box), flags);
4920 if (!(flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
4922 WARN("No read/write flags specified.\n");
4923 return E_INVALIDARG;
4926 if ((flags & WINED3D_MAP_READ) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_R))
4928 WARN("Resource does not have MAP_R access.\n");
4929 return E_INVALIDARG;
4932 if ((flags & WINED3D_MAP_WRITE) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_W))
4934 WARN("Resource does not have MAP_W access.\n");
4935 return E_INVALIDARG;
4938 flags = sanitise_map_flags(resource, flags);
4940 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4941 return E_INVALIDARG;
4943 if (!box)
4945 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4946 box = &b;
4948 else if (FAILED(wined3d_resource_check_box_dimensions(resource, sub_resource_idx, box)))
4950 WARN("Map box is invalid.\n");
4952 if (resource->type != WINED3D_RTYPE_BUFFER && resource->type != WINED3D_RTYPE_TEXTURE_2D)
4953 return WINED3DERR_INVALIDCALL;
4955 if ((resource->format_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
4956 return WINED3DERR_INVALIDCALL;
4959 wined3d_mutex_lock();
4960 if (SUCCEEDED(hr = wined3d_device_context_emit_map(context, resource,
4961 sub_resource_idx, &map_desc->data, box, flags)))
4962 wined3d_resource_get_sub_resource_map_pitch(resource, sub_resource_idx,
4963 &map_desc->row_pitch, &map_desc->slice_pitch);
4964 wined3d_mutex_unlock();
4965 return hr;
4968 HRESULT CDECL wined3d_device_context_unmap(struct wined3d_device_context *context,
4969 struct wined3d_resource *resource, unsigned int sub_resource_idx)
4971 HRESULT hr;
4972 TRACE("context %p, resource %p, sub_resource_idx %u.\n", context, resource, sub_resource_idx);
4974 wined3d_mutex_lock();
4975 hr = wined3d_device_context_emit_unmap(context, resource, sub_resource_idx);
4976 wined3d_mutex_unlock();
4977 return hr;
4980 void CDECL wined3d_device_context_issue_query(struct wined3d_device_context *context,
4981 struct wined3d_query *query, unsigned int flags)
4983 TRACE("context %p, query %p, flags %#x.\n", context, query, flags);
4985 wined3d_mutex_lock();
4986 context->ops->issue_query(context, query, flags);
4987 wined3d_mutex_unlock();
4990 void CDECL wined3d_device_context_execute_command_list(struct wined3d_device_context *context,
4991 struct wined3d_command_list *list, bool restore_state)
4993 TRACE("context %p, list %p, restore_state %d.\n", context, list, restore_state);
4995 wined3d_mutex_lock();
4996 wined3d_device_context_emit_execute_command_list(context, list, restore_state);
4997 wined3d_mutex_unlock();
5000 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_rendertarget_view(
5001 const struct wined3d_device_context *context, unsigned int view_idx)
5003 unsigned int max_rt_count;
5005 TRACE("context %p, view_idx %u.\n", context, view_idx);
5007 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
5008 if (view_idx >= max_rt_count)
5010 WARN("Only %u render targets are supported.\n", max_rt_count);
5011 return NULL;
5014 return context->state->fb.render_targets[view_idx];
5017 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_depth_stencil_view(
5018 const struct wined3d_device_context *context)
5020 TRACE("context %p.\n", context);
5022 return context->state->fb.depth_stencil;
5025 void CDECL wined3d_device_context_generate_mipmaps(struct wined3d_device_context *context,
5026 struct wined3d_shader_resource_view *view)
5028 struct wined3d_texture *texture;
5030 TRACE("context %p, view %p.\n", context, view);
5032 if (view->resource->type == WINED3D_RTYPE_BUFFER)
5034 WARN("Called on buffer resource %p.\n", view->resource);
5035 return;
5038 texture = texture_from_resource(view->resource);
5039 if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
5041 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
5042 return;
5045 wined3d_mutex_lock();
5046 wined3d_device_context_emit_generate_mipmaps(context, view);
5047 wined3d_mutex_unlock();
5050 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
5051 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
5053 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
5054 struct wined3d_sub_resource_data data;
5055 struct wined3d_resource_desc desc;
5056 struct wined3d_map_desc map_desc;
5057 struct wined3d_texture *texture;
5058 HRESULT hr;
5060 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
5062 ERR("Failed to map source texture.\n");
5063 return NULL;
5066 data.data = map_desc.data;
5067 data.row_pitch = map_desc.row_pitch;
5068 data.slice_pitch = map_desc.slice_pitch;
5070 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5071 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
5072 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5073 desc.multisample_quality = 0;
5074 desc.usage = WINED3DUSAGE_DYNAMIC;
5075 desc.bind_flags = 0;
5076 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5077 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
5078 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
5079 desc.depth = 1;
5080 desc.size = 0;
5082 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
5083 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
5084 if (FAILED(hr))
5086 ERR("Failed to create cursor texture.\n");
5087 return NULL;
5090 return texture;
5093 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
5094 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
5096 unsigned int texture_level = sub_resource_idx % texture->level_count;
5097 unsigned int cursor_width, cursor_height;
5098 struct wined3d_map_desc map_desc;
5100 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
5101 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
5103 if (sub_resource_idx >= texture->level_count * texture->layer_count
5104 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
5105 return WINED3DERR_INVALIDCALL;
5107 if (device->cursor_texture)
5109 wined3d_texture_decref(device->cursor_texture);
5110 device->cursor_texture = NULL;
5113 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
5115 WARN("Texture %p has invalid format %s.\n",
5116 texture, debug_d3dformat(texture->resource.format->id));
5117 return WINED3DERR_INVALIDCALL;
5120 /* Cursor width and height must all be powers of two */
5121 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
5122 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
5123 if ((cursor_width & (cursor_width - 1)) || (cursor_height & (cursor_height - 1)))
5125 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width, cursor_height);
5126 return WINED3DERR_INVALIDCALL;
5129 /* Do not store the surface's pointer because the application may
5130 * release it after setting the cursor image. Windows doesn't
5131 * addref the set surface, so we can't do this either without
5132 * creating circular refcount dependencies. */
5133 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
5135 ERR("Failed to create cursor texture.\n");
5136 return WINED3DERR_INVALIDCALL;
5139 if (cursor_width == 32 && cursor_height == 32)
5141 UINT mask_size = cursor_width * cursor_height / 8;
5142 ICONINFO cursor_info;
5143 DWORD *mask_bits;
5144 HCURSOR cursor;
5146 /* 32-bit user32 cursors ignore the alpha channel if it's all
5147 * zeroes, and use the mask instead. Fill the mask with all ones
5148 * to ensure we still get a fully transparent cursor. */
5149 if (!(mask_bits = heap_alloc(mask_size)))
5150 return E_OUTOFMEMORY;
5151 memset(mask_bits, 0xff, mask_size);
5153 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
5154 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
5155 cursor_info.fIcon = FALSE;
5156 cursor_info.xHotspot = x_hotspot;
5157 cursor_info.yHotspot = y_hotspot;
5158 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
5159 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
5160 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
5162 /* Create our cursor and clean up. */
5163 cursor = CreateIconIndirect(&cursor_info);
5164 if (cursor_info.hbmMask)
5165 DeleteObject(cursor_info.hbmMask);
5166 if (cursor_info.hbmColor)
5167 DeleteObject(cursor_info.hbmColor);
5168 if (device->hardwareCursor)
5169 DestroyCursor(device->hardwareCursor);
5170 device->hardwareCursor = cursor;
5171 if (device->bCursorVisible)
5172 SetCursor(cursor);
5174 heap_free(mask_bits);
5177 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
5178 device->cursorWidth = cursor_width;
5179 device->cursorHeight = cursor_height;
5180 device->xHotSpot = x_hotspot;
5181 device->yHotSpot = y_hotspot;
5183 return WINED3D_OK;
5186 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
5187 int x_screen_space, int y_screen_space, DWORD flags)
5189 TRACE("device %p, x %d, y %d, flags %#x.\n",
5190 device, x_screen_space, y_screen_space, flags);
5192 device->xScreenSpace = x_screen_space;
5193 device->yScreenSpace = y_screen_space;
5195 if (device->hardwareCursor)
5197 POINT pt;
5199 GetCursorPos( &pt );
5200 if (x_screen_space == pt.x && y_screen_space == pt.y)
5201 return;
5202 SetCursorPos( x_screen_space, y_screen_space );
5204 /* Switch to the software cursor if position diverges from the hardware one. */
5205 GetCursorPos( &pt );
5206 if (x_screen_space != pt.x || y_screen_space != pt.y)
5208 if (device->bCursorVisible) SetCursor( NULL );
5209 DestroyCursor( device->hardwareCursor );
5210 device->hardwareCursor = 0;
5215 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5217 BOOL oldVisible = device->bCursorVisible;
5219 TRACE("device %p, show %#x.\n", device, show);
5222 * When ShowCursor is first called it should make the cursor appear at the OS's last
5223 * known cursor position.
5225 if (show && !oldVisible)
5227 POINT pt;
5228 GetCursorPos(&pt);
5229 device->xScreenSpace = pt.x;
5230 device->yScreenSpace = pt.y;
5233 if (device->hardwareCursor)
5235 device->bCursorVisible = show;
5236 if (show)
5237 SetCursor(device->hardwareCursor);
5238 else
5239 SetCursor(NULL);
5241 else if (device->cursor_texture)
5243 device->bCursorVisible = show;
5246 return oldVisible;
5249 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5251 struct wined3d_resource *resource, *cursor;
5253 TRACE("device %p.\n", device);
5255 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5257 TRACE("Checking resource %p for eviction.\n", resource);
5259 if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
5261 TRACE("Evicting %p.\n", resource);
5262 wined3d_cs_emit_unload_resource(device->cs, resource);
5267 void CDECL wined3d_device_context_flush(struct wined3d_device_context *context)
5269 TRACE("context %p.\n", context);
5271 wined3d_mutex_lock();
5272 context->ops->flush(context);
5273 wined3d_mutex_unlock();
5276 static void update_swapchain_flags(struct wined3d_texture *texture)
5278 unsigned int flags = texture->swapchain->state.desc.flags;
5280 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
5281 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
5282 else
5283 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
5285 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
5286 texture->flags |= WINED3D_TEXTURE_GET_DC;
5287 else
5288 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
5291 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5292 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5293 wined3d_device_reset_cb callback, BOOL reset_state)
5295 static struct wined3d_rendertarget_view *const views[WINED3D_MAX_RENDER_TARGETS];
5296 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
5297 struct wined3d_device_context *context = &device->cs->c;
5298 struct wined3d_swapchain_state *swapchain_state;
5299 struct wined3d_state *state = context->state;
5300 struct wined3d_swapchain_desc *current_desc;
5301 struct wined3d_resource *resource, *cursor;
5302 struct wined3d_rendertarget_view *view;
5303 struct wined3d_swapchain *swapchain;
5304 struct wined3d_view_desc view_desc;
5305 BOOL backbuffer_resized, windowed;
5306 HRESULT hr = WINED3D_OK;
5307 unsigned int i;
5309 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
5310 device, swapchain_desc, mode, callback, reset_state);
5312 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
5314 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5316 ERR("Failed to get the first implicit swapchain.\n");
5317 return WINED3DERR_INVALIDCALL;
5319 swapchain_state = &swapchain->state;
5320 current_desc = &swapchain_state->desc;
5322 if (reset_state)
5324 if (device->logo_texture)
5326 wined3d_texture_decref(device->logo_texture);
5327 device->logo_texture = NULL;
5329 if (device->cursor_texture)
5331 wined3d_texture_decref(device->cursor_texture);
5332 device->cursor_texture = NULL;
5334 state_unbind_resources(state);
5337 wined3d_device_context_set_rendertarget_views(context, 0, d3d_info->limits.max_rt_count, views, FALSE);
5338 wined3d_device_context_set_depth_stencil_view(context, NULL);
5340 if (reset_state)
5342 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5344 TRACE("Enumerating resource %p.\n", resource);
5345 if (FAILED(hr = callback(resource)))
5346 return hr;
5350 TRACE("New params:\n");
5351 TRACE("output %p\n", swapchain_desc->output);
5352 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5353 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5354 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5355 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5356 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5357 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5358 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5359 TRACE("device_window %p\n", swapchain_desc->device_window);
5360 TRACE("windowed %#x\n", swapchain_desc->windowed);
5361 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5362 if (swapchain_desc->enable_auto_depth_stencil)
5363 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5364 TRACE("flags %#x\n", swapchain_desc->flags);
5365 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5366 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5368 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
5369 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
5371 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
5372 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
5373 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
5374 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
5376 /* No special treatment of these parameters. Just store them */
5377 current_desc->swap_effect = swapchain_desc->swap_effect;
5378 current_desc->enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
5379 current_desc->auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
5380 current_desc->refresh_rate = swapchain_desc->refresh_rate;
5381 current_desc->auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
5383 if (swapchain_desc->device_window && swapchain_desc->device_window != current_desc->device_window)
5385 TRACE("Changing the device window from %p to %p.\n",
5386 current_desc->device_window, swapchain_desc->device_window);
5387 current_desc->device_window = swapchain_desc->device_window;
5388 swapchain_state->device_window = swapchain_desc->device_window;
5389 wined3d_swapchain_set_window(swapchain, NULL);
5392 backbuffer_resized = swapchain_desc->backbuffer_width != current_desc->backbuffer_width
5393 || swapchain_desc->backbuffer_height != current_desc->backbuffer_height;
5394 windowed = current_desc->windowed;
5396 if (!swapchain_desc->windowed != !windowed || swapchain->reapply_mode
5397 || mode || (!swapchain_desc->windowed && backbuffer_resized))
5399 /* Switch from windowed to fullscreen. */
5400 if (windowed && !swapchain_desc->windowed)
5402 HWND focus_window = device->create_parms.focus_window;
5404 if (!focus_window)
5405 focus_window = swapchain->state.device_window;
5406 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5408 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5409 return hr;
5413 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(&swapchain->state,
5414 swapchain_desc, mode)))
5415 return hr;
5417 /* Switch from fullscreen to windowed. */
5418 if (!windowed && swapchain_desc->windowed)
5419 wined3d_device_release_focus_window(device);
5421 else if (!swapchain_desc->windowed)
5423 DWORD style = swapchain_state->style;
5424 DWORD exstyle = swapchain_state->exstyle;
5425 struct wined3d_output_desc output_desc;
5427 /* If we're in fullscreen, and the mode wasn't changed, we have to get
5428 * the window back into the right position. Some applications
5429 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
5430 * up their mess. Guild Wars also loses the device during that. */
5431 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
5433 ERR("Failed to get output description, hr %#x.\n", hr);
5434 return hr;
5437 swapchain_state->style = 0;
5438 swapchain_state->exstyle = 0;
5439 wined3d_swapchain_state_setup_fullscreen(swapchain_state, swapchain_state->device_window,
5440 output_desc.desktop_rect.left, output_desc.desktop_rect.top,
5441 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height);
5442 swapchain_state->style = style;
5443 swapchain_state->exstyle = exstyle;
5446 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
5447 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
5448 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
5449 return hr;
5451 if (swapchain_desc->flags != current_desc->flags)
5453 current_desc->flags = swapchain_desc->flags;
5455 update_swapchain_flags(swapchain->front_buffer);
5456 for (i = 0; i < current_desc->backbuffer_count; ++i)
5458 update_swapchain_flags(swapchain->back_buffers[i]);
5462 if ((view = device->auto_depth_stencil_view))
5464 device->auto_depth_stencil_view = NULL;
5465 wined3d_rendertarget_view_decref(view);
5467 if (current_desc->enable_auto_depth_stencil)
5469 struct wined3d_resource_desc texture_desc;
5470 struct wined3d_texture *texture;
5472 TRACE("Creating the depth stencil buffer.\n");
5474 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5475 texture_desc.format = current_desc->auto_depth_stencil_format;
5476 texture_desc.multisample_type = current_desc->multisample_type;
5477 texture_desc.multisample_quality = current_desc->multisample_quality;
5478 texture_desc.usage = 0;
5479 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
5480 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5481 texture_desc.width = current_desc->backbuffer_width;
5482 texture_desc.height = current_desc->backbuffer_height;
5483 texture_desc.depth = 1;
5484 texture_desc.size = 0;
5486 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
5487 device->device_parent, &texture_desc, 0, &texture)))
5489 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
5490 return WINED3DERR_INVALIDCALL;
5493 view_desc.format_id = texture->resource.format->id;
5494 view_desc.flags = 0;
5495 view_desc.u.texture.level_idx = 0;
5496 view_desc.u.texture.level_count = 1;
5497 view_desc.u.texture.layer_idx = 0;
5498 view_desc.u.texture.layer_count = 1;
5499 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
5500 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
5501 wined3d_texture_decref(texture);
5502 if (FAILED(hr))
5504 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5505 return hr;
5509 if ((view = device->back_buffer_view))
5511 device->back_buffer_view = NULL;
5512 wined3d_rendertarget_view_decref(view);
5514 if (current_desc->backbuffer_count && current_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
5516 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
5518 view_desc.format_id = back_buffer->format->id;
5519 view_desc.flags = 0;
5520 view_desc.u.texture.level_idx = 0;
5521 view_desc.u.texture.level_count = 1;
5522 view_desc.u.texture.layer_idx = 0;
5523 view_desc.u.texture.layer_count = 1;
5524 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
5525 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
5527 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5528 return hr;
5532 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
5533 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
5534 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
5535 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
5537 if (reset_state)
5539 TRACE("Resetting state.\n");
5540 wined3d_device_context_emit_reset_state(&device->cs->c, false);
5541 state_cleanup(state);
5543 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5545 TRACE("Unloading resource %p.\n", resource);
5546 wined3d_cs_emit_unload_resource(device->cs, resource);
5549 device->adapter->adapter_ops->adapter_uninit_3d(device);
5551 wined3d_state_reset(state, &device->adapter->d3d_info);
5553 device_init_swapchain_state(device, swapchain);
5554 if (wined3d_settings.logo)
5555 device_load_logo(device, wined3d_settings.logo);
5557 else
5559 if ((view = device->back_buffer_view))
5560 wined3d_device_context_set_rendertarget_views(context, 0, 1, &view, FALSE);
5561 if ((view = device->auto_depth_stencil_view))
5562 wined3d_device_context_set_depth_stencil_view(context, view);
5565 if (reset_state)
5566 hr = device->adapter->adapter_ops->adapter_init_3d(device);
5568 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5569 * first use
5571 return hr;
5574 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5576 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5578 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5580 return WINED3D_OK;
5584 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5585 struct wined3d_device_creation_parameters *parameters)
5587 TRACE("device %p, parameters %p.\n", device, parameters);
5589 *parameters = device->create_parms;
5592 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5594 TRACE("device %p.\n", device);
5596 return device->wined3d;
5599 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5600 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5602 struct wined3d_swapchain *swapchain;
5604 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5605 device, swapchain_idx, flags, ramp);
5607 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5608 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5611 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5612 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5614 struct wined3d_swapchain *swapchain;
5616 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5617 device, swapchain_idx, ramp);
5619 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5620 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5623 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5625 TRACE("device %p, resource %p.\n", device, resource);
5627 wined3d_not_from_cs(device->cs);
5629 list_add_head(&device->resources, &resource->resource_list_entry);
5632 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5634 TRACE("device %p, resource %p.\n", device, resource);
5636 wined3d_not_from_cs(device->cs);
5638 list_remove(&resource->resource_list_entry);
5641 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5643 enum wined3d_resource_type type = resource->type;
5644 struct wined3d_state *state = device->cs->c.state;
5645 struct wined3d_rendertarget_view *rtv;
5646 unsigned int i;
5648 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5650 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
5652 if ((rtv = state->fb.render_targets[i]) && rtv->resource == resource)
5653 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5656 if ((rtv = state->fb.depth_stencil) && rtv->resource == resource)
5657 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5659 switch (type)
5661 case WINED3D_RTYPE_TEXTURE_1D:
5662 case WINED3D_RTYPE_TEXTURE_2D:
5663 case WINED3D_RTYPE_TEXTURE_3D:
5664 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
5666 if (&state->textures[i]->resource == resource)
5668 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
5669 state->textures[i] = NULL;
5672 break;
5674 case WINED3D_RTYPE_BUFFER:
5675 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
5677 if (&state->streams[i].buffer->resource == resource)
5679 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5680 state->streams[i].buffer = NULL;
5684 if (&state->index_buffer->resource == resource)
5686 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5687 state->index_buffer = NULL;
5689 break;
5691 default:
5692 break;
5695 /* Remove the resource from the resourceStore */
5696 device_resource_remove(device, resource);
5698 TRACE("Resource released.\n");
5701 static int wined3d_so_desc_compare(const void *key, const struct wine_rb_entry *entry)
5703 const struct wined3d_stream_output_desc *desc = &WINE_RB_ENTRY_VALUE(entry,
5704 struct wined3d_so_desc_entry, entry)->desc;
5705 const struct wined3d_stream_output_desc *k = key;
5706 unsigned int i;
5707 int ret;
5709 if ((ret = (k->element_count - desc->element_count)))
5710 return ret;
5711 if ((ret = (k->buffer_stride_count - desc->buffer_stride_count)))
5712 return ret;
5713 if ((ret = (k->rasterizer_stream_idx - desc->rasterizer_stream_idx)))
5714 return ret;
5716 for (i = 0; i < k->element_count; ++i)
5718 const struct wined3d_stream_output_element *b = &desc->elements[i];
5719 const struct wined3d_stream_output_element *a = &k->elements[i];
5721 if ((ret = (a->stream_idx - b->stream_idx)))
5722 return ret;
5723 if ((ret = (!a->semantic_name - !b->semantic_name)))
5724 return ret;
5725 if (a->semantic_name && (ret = strcmp(a->semantic_name, b->semantic_name)))
5726 return ret;
5727 if ((ret = (a->semantic_idx - b->semantic_idx)))
5728 return ret;
5729 if ((ret = (a->component_idx - b->component_idx)))
5730 return ret;
5731 if ((ret = (a->component_count - b->component_count)))
5732 return ret;
5733 if ((ret = (a->output_slot - b->output_slot)))
5734 return ret;
5737 for (i = 0; i < k->buffer_stride_count; ++i)
5739 if ((ret = (k->buffer_strides[i] - desc->buffer_strides[i])))
5740 return ret;
5743 return 0;
5746 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5748 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5750 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5753 static int wined3d_rasterizer_state_compare(const void *key, const struct wine_rb_entry *entry)
5755 const struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
5757 return memcmp(&state->desc, key, sizeof(state->desc));
5760 static int wined3d_blend_state_compare(const void *key, const struct wine_rb_entry *entry)
5762 const struct wined3d_blend_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
5764 return memcmp(&state->desc, key, sizeof(state->desc));
5767 static int wined3d_depth_stencil_state_compare(const void *key, const struct wine_rb_entry *entry)
5769 const struct wined3d_depth_stencil_state *state
5770 = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
5772 return memcmp(&state->desc, key, sizeof(state->desc));
5775 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
5776 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
5777 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
5778 const BOOL *supported_extensions, struct wined3d_device_parent *device_parent)
5780 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
5781 const struct wined3d_fragment_pipe_ops *fragment_pipeline;
5782 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5783 unsigned int i;
5784 HRESULT hr;
5786 device->ref = 1;
5787 device->wined3d = wined3d;
5788 wined3d_incref(device->wined3d);
5789 device->adapter = adapter;
5790 device->device_parent = device_parent;
5791 list_init(&device->resources);
5792 list_init(&device->shaders);
5793 device->surface_alignment = surface_alignment;
5795 /* Save the creation parameters. */
5796 device->create_parms.adapter_idx = adapter_idx;
5797 device->create_parms.device_type = device_type;
5798 device->create_parms.focus_window = focus_window;
5799 device->create_parms.flags = flags;
5801 device->shader_backend = adapter->shader_backend;
5803 vertex_pipeline = adapter->vertex_pipe;
5805 fragment_pipeline = adapter->fragment_pipe;
5807 wine_rb_init(&device->so_descs, wined3d_so_desc_compare);
5808 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5809 wine_rb_init(&device->rasterizer_states, wined3d_rasterizer_state_compare);
5810 wine_rb_init(&device->blend_states, wined3d_blend_state_compare);
5811 wine_rb_init(&device->depth_stencil_states, wined3d_depth_stencil_state_compare);
5813 if (vertex_pipeline->vp_states && fragment_pipeline->states
5814 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
5815 &adapter->d3d_info, supported_extensions, vertex_pipeline,
5816 fragment_pipeline, adapter->misc_state_template)))
5818 ERR("Failed to compile state table, hr %#x.\n", hr);
5819 wine_rb_destroy(&device->samplers, NULL, NULL);
5820 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5821 wine_rb_destroy(&device->blend_states, NULL, NULL);
5822 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5823 wine_rb_destroy(&device->so_descs, NULL, NULL);
5824 wined3d_decref(device->wined3d);
5825 return hr;
5828 device->max_frame_latency = 3;
5830 if (!(device->cs = wined3d_cs_create(device, levels, level_count)))
5832 WARN("Failed to create command stream.\n");
5833 hr = E_FAIL;
5834 goto err;
5837 return WINED3D_OK;
5839 err:
5840 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
5842 heap_free(device->multistate_funcs[i]);
5844 wine_rb_destroy(&device->samplers, NULL, NULL);
5845 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5846 wine_rb_destroy(&device->blend_states, NULL, NULL);
5847 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5848 wine_rb_destroy(&device->so_descs, NULL, NULL);
5849 wined3d_decref(device->wined3d);
5850 return hr;
5853 void device_invalidate_state(const struct wined3d_device *device, unsigned int state_id)
5855 unsigned int representative, i, idx, shift;
5856 struct wined3d_context *context;
5858 wined3d_from_cs(device->cs);
5860 if (STATE_IS_COMPUTE(state_id))
5862 for (i = 0; i < device->context_count; ++i)
5863 context_invalidate_compute_state(device->contexts[i], state_id);
5864 return;
5867 representative = device->state_table[state_id].representative;
5868 idx = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
5869 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
5870 for (i = 0; i < device->context_count; ++i)
5872 device->contexts[i]->dirty_graphics_states[idx] |= (1u << shift);
5876 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5877 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5879 if (message == WM_DESTROY)
5881 TRACE("unregister window %p.\n", window);
5882 wined3d_unregister_window(window);
5884 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5885 ERR("Window %p is not the focus window for device %p.\n", window, device);
5887 else if (message == WM_DISPLAYCHANGE)
5889 device->device_parent->ops->mode_changed(device->device_parent);
5891 else if (message == WM_ACTIVATEAPP)
5893 unsigned int i = device->swapchain_count;
5895 /* Deactivating the implicit swapchain may cause the application
5896 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
5897 * deactivate the implicit swapchain last, and to avoid accessing the
5898 * "device" pointer afterwards. */
5899 while (i--)
5900 wined3d_swapchain_activate(device->swapchains[i], wparam);
5902 else if (message == WM_SYSCOMMAND)
5904 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5906 if (unicode)
5907 DefWindowProcW(window, message, wparam, lparam);
5908 else
5909 DefWindowProcA(window, message, wparam, lparam);
5913 if (unicode)
5914 return CallWindowProcW(proc, window, message, wparam, lparam);
5915 else
5916 return CallWindowProcA(proc, window, message, wparam, lparam);