include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / wined3d / device.c
blobe445679ec2f2b397a4bf58ab56e7ec5feb15b839
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
11 * Copyright 2016, 2018 Józef Kucia for CodeWeavers
12 * Copyright 2020 Zebediah Figura
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wined3d_private.h"
30 #include "wined3d_gl.h"
31 #include "wined3d_vk.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
35 WINE_DECLARE_DEBUG_CHANNEL(winediag);
37 struct wined3d_matrix_3x3
39 float _11, _12, _13;
40 float _21, _22, _23;
41 float _31, _32, _33;
44 struct light_transformed
46 struct wined3d_color diffuse, specular, ambient;
47 struct wined3d_vec4 position;
48 struct wined3d_vec3 direction;
49 float range, falloff, c_att, l_att, q_att, cos_htheta, cos_hphi;
52 struct lights_settings
54 struct light_transformed lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
55 struct wined3d_color ambient_light;
56 struct wined3d_matrix modelview_matrix;
57 struct wined3d_matrix_3x3 normal_matrix;
58 struct wined3d_vec4 position_transformed;
60 float fog_start, fog_end, fog_density;
62 uint32_t point_light_count : 8;
63 uint32_t spot_light_count : 8;
64 uint32_t directional_light_count : 8;
65 uint32_t parallel_point_light_count : 8;
66 uint32_t lighting : 1;
67 uint32_t legacy_lighting : 1;
68 uint32_t normalise : 1;
69 uint32_t localviewer : 1;
70 uint32_t fog_coord_mode : 2;
71 uint32_t fog_mode : 2;
72 uint32_t padding : 24;
75 /* Define the default light parameters as specified by MSDN. */
76 const struct wined3d_light WINED3D_default_light =
78 WINED3D_LIGHT_DIRECTIONAL, /* Type */
79 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
80 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
81 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
82 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
83 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
84 0.0f, /* Range */
85 0.0f, /* Falloff */
86 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
87 0.0f, /* Theta */
88 0.0f /* Phi */
91 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
93 struct wined3d_context **new_array;
95 TRACE("Adding context %p.\n", context);
97 if (!device->shader_backend->shader_allocate_context_data(context))
99 ERR("Failed to allocate shader backend context data.\n");
100 return FALSE;
102 device->shader_backend->shader_init_context_state(context);
104 if (!device->adapter->fragment_pipe->allocate_context_data(context))
106 ERR("Failed to allocate fragment pipeline context data.\n");
107 device->shader_backend->shader_free_context_data(context);
108 return FALSE;
111 if (!(new_array = realloc(device->contexts, sizeof(*new_array) * (device->context_count + 1))))
113 ERR("Failed to grow the context array.\n");
114 device->adapter->fragment_pipe->free_context_data(context);
115 device->shader_backend->shader_free_context_data(context);
116 return FALSE;
119 new_array[device->context_count++] = context;
120 device->contexts = new_array;
122 return TRUE;
125 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
127 struct wined3d_context **new_array;
128 BOOL found = FALSE;
129 UINT i;
131 TRACE("Removing context %p.\n", context);
133 device->adapter->fragment_pipe->free_context_data(context);
134 device->shader_backend->shader_free_context_data(context);
136 for (i = 0; i < device->context_count; ++i)
138 if (device->contexts[i] == context)
140 found = TRUE;
141 break;
145 if (!found)
147 ERR("Context %p doesn't exist in context array.\n", context);
148 return;
151 if (!--device->context_count)
153 free(device->contexts);
154 device->contexts = NULL;
155 return;
158 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
159 if (!(new_array = realloc(device->contexts, device->context_count * sizeof(*device->contexts))))
161 ERR("Failed to shrink context array. Oh well.\n");
162 return;
165 device->contexts = new_array;
168 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
170 unsigned int refcount = InterlockedIncrement(&device->ref);
172 TRACE("%p increasing refcount to %u.\n", device, refcount);
174 return refcount;
177 static void device_free_so_desc(struct wine_rb_entry *entry, void *context)
179 struct wined3d_so_desc_entry *s = WINE_RB_ENTRY_VALUE(entry, struct wined3d_so_desc_entry, entry);
181 free(s);
184 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
186 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
188 ERR("Leftover sampler %p.\n", sampler);
191 static void device_leftover_rasterizer_state(struct wine_rb_entry *entry, void *context)
193 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
195 ERR("Leftover rasterizer state %p.\n", state);
198 static void device_leftover_blend_state(struct wine_rb_entry *entry, void *context)
200 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
202 ERR("Leftover blend state %p.\n", blend_state);
205 static void device_leftover_depth_stencil_state(struct wine_rb_entry *entry, void *context)
207 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
209 ERR("Leftover depth/stencil state %p.\n", state);
212 void wined3d_device_cleanup(struct wined3d_device *device)
214 unsigned int i;
216 if (device->swapchain_count)
217 wined3d_device_uninit_3d(device);
219 wined3d_cs_destroy(device->cs);
221 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
223 free(device->multistate_funcs[i]);
224 device->multistate_funcs[i] = NULL;
227 if (!list_empty(&device->resources))
229 struct wined3d_resource *resource;
231 ERR("Device released with resources still bound.\n");
233 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
235 ERR("Leftover resource %p with type %s (%#x).\n",
236 resource, debug_d3dresourcetype(resource->type), resource->type);
240 if (device->contexts)
241 ERR("Context array not freed!\n");
242 if (device->hardwareCursor)
243 DestroyCursor(device->hardwareCursor);
244 device->hardwareCursor = 0;
246 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
247 wine_rb_destroy(&device->rasterizer_states, device_leftover_rasterizer_state, NULL);
248 wine_rb_destroy(&device->blend_states, device_leftover_blend_state, NULL);
249 wine_rb_destroy(&device->depth_stencil_states, device_leftover_depth_stencil_state, NULL);
250 wine_rb_destroy(&device->so_descs, device_free_so_desc, NULL);
252 wined3d_lock_cleanup(&device->bo_map_lock);
254 wined3d_decref(device->wined3d);
255 device->wined3d = NULL;
258 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
260 unsigned int refcount = InterlockedDecrement(&device->ref);
262 TRACE("%p decreasing refcount to %u.\n", device, refcount);
264 if (!refcount)
266 wined3d_mutex_lock();
267 device->adapter->adapter_ops->adapter_destroy_device(device);
268 TRACE("Destroyed device %p.\n", device);
269 wined3d_mutex_unlock();
272 return refcount;
275 ULONG CDECL wined3d_blend_state_incref(struct wined3d_blend_state *state)
277 unsigned int refcount = InterlockedIncrement(&state->refcount);
279 TRACE("%p increasing refcount to %u.\n", state, refcount);
281 return refcount;
284 static void wined3d_blend_state_destroy_object(void *object)
286 TRACE("object %p.\n", object);
288 free(object);
291 ULONG CDECL wined3d_blend_state_decref(struct wined3d_blend_state *state)
293 unsigned int refcount = wined3d_atomic_decrement_mutex_lock(&state->refcount);
294 struct wined3d_device *device = state->device;
296 TRACE("%p decreasing refcount to %u.\n", state, refcount);
298 if (!refcount)
300 state->parent_ops->wined3d_object_destroyed(state->parent);
301 wined3d_cs_destroy_object(device->cs, wined3d_blend_state_destroy_object, state);
302 wined3d_mutex_unlock();
305 return refcount;
308 void * CDECL wined3d_blend_state_get_parent(const struct wined3d_blend_state *state)
310 TRACE("state %p.\n", state);
312 return state->parent;
315 static bool is_dual_source(enum wined3d_blend state)
317 return state >= WINED3D_BLEND_SRC1COLOR && state <= WINED3D_BLEND_INVSRC1ALPHA;
320 HRESULT CDECL wined3d_blend_state_create(struct wined3d_device *device,
321 const struct wined3d_blend_state_desc *desc, void *parent,
322 const struct wined3d_parent_ops *parent_ops, struct wined3d_blend_state **state)
324 struct wined3d_blend_state *object;
326 TRACE("device %p, desc %p, parent %p, parent_ops %p, state %p.\n",
327 device, desc, parent, parent_ops, state);
329 if (!(object = calloc(1, sizeof(*object))))
330 return E_OUTOFMEMORY;
332 object->refcount = 1;
333 object->desc = *desc;
334 object->parent = parent;
335 object->parent_ops = parent_ops;
336 object->device = device;
338 object->dual_source = desc->rt[0].enable
339 && (is_dual_source(desc->rt[0].src)
340 || is_dual_source(desc->rt[0].dst)
341 || is_dual_source(desc->rt[0].src_alpha)
342 || is_dual_source(desc->rt[0].dst_alpha));
344 TRACE("Created blend state %p.\n", object);
345 *state = object;
347 return WINED3D_OK;
350 ULONG CDECL wined3d_depth_stencil_state_incref(struct wined3d_depth_stencil_state *state)
352 unsigned int refcount = InterlockedIncrement(&state->refcount);
354 TRACE("%p increasing refcount to %u.\n", state, refcount);
356 return refcount;
359 static void wined3d_depth_stencil_state_destroy_object(void *object)
361 TRACE("object %p.\n", object);
363 free(object);
366 ULONG CDECL wined3d_depth_stencil_state_decref(struct wined3d_depth_stencil_state *state)
368 unsigned int refcount = wined3d_atomic_decrement_mutex_lock(&state->refcount);
369 struct wined3d_device *device = state->device;
371 TRACE("%p decreasing refcount to %u.\n", state, refcount);
373 if (!refcount)
375 state->parent_ops->wined3d_object_destroyed(state->parent);
376 wined3d_cs_destroy_object(device->cs, wined3d_depth_stencil_state_destroy_object, state);
377 wined3d_mutex_unlock();
380 return refcount;
383 void * CDECL wined3d_depth_stencil_state_get_parent(const struct wined3d_depth_stencil_state *state)
385 TRACE("state %p.\n", state);
387 return state->parent;
390 static bool stencil_op_writes_ds(const struct wined3d_stencil_op_desc *desc)
392 return desc->fail_op != WINED3D_STENCIL_OP_KEEP
393 || desc->depth_fail_op != WINED3D_STENCIL_OP_KEEP
394 || desc->pass_op != WINED3D_STENCIL_OP_KEEP;
397 static bool depth_stencil_state_desc_writes_ds(const struct wined3d_depth_stencil_state_desc *desc)
399 if (desc->depth && desc->depth_write)
400 return true;
402 if (desc->stencil && desc->stencil_write_mask)
404 if (stencil_op_writes_ds(&desc->front) || stencil_op_writes_ds(&desc->back))
405 return true;
408 return false;
411 HRESULT CDECL wined3d_depth_stencil_state_create(struct wined3d_device *device,
412 const struct wined3d_depth_stencil_state_desc *desc, void *parent,
413 const struct wined3d_parent_ops *parent_ops, struct wined3d_depth_stencil_state **state)
415 struct wined3d_depth_stencil_state *object;
417 TRACE("device %p, desc %p, parent %p, parent_ops %p, state %p.\n",
418 device, desc, parent, parent_ops, state);
420 if (!(object = calloc(1, sizeof(*object))))
421 return E_OUTOFMEMORY;
423 object->refcount = 1;
424 object->desc = *desc;
425 object->parent = parent;
426 object->parent_ops = parent_ops;
427 object->device = device;
429 object->writes_ds = depth_stencil_state_desc_writes_ds(desc);
431 TRACE("Created depth/stencil state %p.\n", object);
432 *state = object;
434 return WINED3D_OK;
437 ULONG CDECL wined3d_rasterizer_state_incref(struct wined3d_rasterizer_state *state)
439 unsigned int refcount = InterlockedIncrement(&state->refcount);
441 TRACE("%p increasing refcount to %u.\n", state, refcount);
443 return refcount;
446 static void wined3d_rasterizer_state_destroy_object(void *object)
448 TRACE("object %p.\n", object);
450 free(object);
453 ULONG CDECL wined3d_rasterizer_state_decref(struct wined3d_rasterizer_state *state)
455 unsigned int refcount = wined3d_atomic_decrement_mutex_lock(&state->refcount);
456 struct wined3d_device *device = state->device;
458 TRACE("%p decreasing refcount to %u.\n", state, refcount);
460 if (!refcount)
462 state->parent_ops->wined3d_object_destroyed(state->parent);
463 wined3d_cs_destroy_object(device->cs, wined3d_rasterizer_state_destroy_object, state);
464 wined3d_mutex_unlock();
467 return refcount;
470 void * CDECL wined3d_rasterizer_state_get_parent(const struct wined3d_rasterizer_state *state)
472 TRACE("rasterizer_state %p.\n", state);
474 return state->parent;
477 HRESULT CDECL wined3d_rasterizer_state_create(struct wined3d_device *device,
478 const struct wined3d_rasterizer_state_desc *desc, void *parent,
479 const struct wined3d_parent_ops *parent_ops, struct wined3d_rasterizer_state **state)
481 struct wined3d_rasterizer_state *object;
483 TRACE("device %p, desc %p, parent %p, parent_ops %p, state %p.\n",
484 device, desc, parent, parent_ops, state);
486 if (!(object = calloc(1, sizeof(*object))))
487 return E_OUTOFMEMORY;
489 object->refcount = 1;
490 object->desc = *desc;
491 object->parent = parent;
492 object->parent_ops = parent_ops;
493 object->device = device;
495 TRACE("Created rasterizer state %p.\n", object);
496 *state = object;
498 return WINED3D_OK;
501 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
503 TRACE("device %p.\n", device);
505 return device->swapchain_count;
508 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
510 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
512 if (swapchain_idx >= device->swapchain_count)
514 WARN("swapchain_idx %u >= swapchain_count %u.\n",
515 swapchain_idx, device->swapchain_count);
516 return NULL;
519 return device->swapchains[swapchain_idx];
522 static void device_load_logo(struct wined3d_device *device, const char *filename)
524 struct wined3d_color_key color_key;
525 struct wined3d_resource_desc desc;
526 HBITMAP hbm;
527 BITMAP bm;
528 HRESULT hr;
529 HDC dcb = NULL, dcs = NULL;
531 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
533 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
534 return;
536 GetObjectA(hbm, sizeof(BITMAP), &bm);
538 if (!(dcb = CreateCompatibleDC(NULL)))
539 goto out;
540 SelectObject(dcb, hbm);
542 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
543 desc.format = WINED3DFMT_B5G6R5_UNORM;
544 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
545 desc.multisample_quality = 0;
546 desc.usage = WINED3DUSAGE_DYNAMIC;
547 desc.bind_flags = 0;
548 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
549 desc.width = bm.bmWidth;
550 desc.height = bm.bmHeight;
551 desc.depth = 1;
552 desc.size = 0;
553 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_GET_DC,
554 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
556 ERR("Wine logo requested, but failed to create texture, hr %#lx.\n", hr);
557 goto out;
560 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
562 wined3d_texture_decref(device->logo_texture);
563 device->logo_texture = NULL;
564 goto out;
566 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
567 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
569 color_key.color_space_low_value = 0;
570 color_key.color_space_high_value = 0;
571 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
573 out:
574 if (dcb) DeleteDC(dcb);
575 if (hbm) DeleteObject(hbm);
578 static GLuint64 create_dummy_bindless_handle(const struct wined3d_gl_info *gl_info, GLuint texture)
580 GLuint64 handle;
582 if (!texture || !gl_info->supported[ARB_BINDLESS_TEXTURE])
583 return 0;
585 handle = GL_EXTCALL(glGetTextureHandleARB(texture));
586 GL_EXTCALL(glMakeTextureHandleResidentARB(handle));
587 return handle;
590 /* Context activation is done by the caller. */
591 static void wined3d_device_gl_create_dummy_textures(struct wined3d_device_gl *device_gl,
592 struct wined3d_context_gl *context_gl)
594 struct wined3d_dummy_textures *textures = &device_gl->dummy_textures;
595 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
596 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
597 unsigned int i;
598 DWORD color;
600 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
601 color = 0x000000ff;
602 else
603 color = 0x00000000;
605 /* Under DirectX you can sample even if no texture is bound, whereas
606 * OpenGL will only allow that when a valid texture is bound.
607 * We emulate this by creating dummy textures and binding them
608 * to each texture stage when the currently set D3D texture is NULL. */
609 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
611 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
612 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
613 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
614 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
615 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
617 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
618 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
619 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
620 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
621 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
623 if (gl_info->supported[EXT_TEXTURE3D])
625 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
626 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
627 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
628 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
629 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
632 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
634 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
635 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
636 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
637 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
639 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
640 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
644 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
646 DWORD cube_array_data[6];
648 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
649 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
650 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
651 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
652 cube_array_data[i] = color;
653 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
654 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
657 if (gl_info->supported[EXT_TEXTURE_ARRAY])
659 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
660 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
661 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
662 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
663 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
665 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
666 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
667 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
668 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
669 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
672 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
674 GLuint buffer;
676 GL_EXTCALL(glGenBuffers(1, &buffer));
677 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
678 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
679 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
681 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
682 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
683 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
684 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
685 GL_EXTCALL(glDeleteBuffers(1, &buffer));
688 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
690 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
691 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
692 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
693 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
695 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
696 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
697 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
698 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
700 if (gl_info->supported[ARB_CLEAR_TEXTURE])
702 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
703 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
705 else
707 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
711 textures->bindless.tex_1d = create_dummy_bindless_handle(gl_info, textures->tex_1d);
712 textures->bindless.tex_2d = create_dummy_bindless_handle(gl_info, textures->tex_2d);
713 textures->bindless.tex_rect = create_dummy_bindless_handle(gl_info, textures->tex_rect);
714 textures->bindless.tex_3d = create_dummy_bindless_handle(gl_info, textures->tex_3d);
715 textures->bindless.tex_cube = create_dummy_bindless_handle(gl_info, textures->tex_cube);
716 textures->bindless.tex_cube_array = create_dummy_bindless_handle(gl_info, textures->tex_cube_array);
717 textures->bindless.tex_1d_array = create_dummy_bindless_handle(gl_info, textures->tex_1d_array);
718 textures->bindless.tex_2d_array = create_dummy_bindless_handle(gl_info, textures->tex_2d_array);
719 textures->bindless.tex_buffer = create_dummy_bindless_handle(gl_info, textures->tex_buffer);
720 textures->bindless.tex_2d_ms = create_dummy_bindless_handle(gl_info, textures->tex_2d_ms);
721 textures->bindless.tex_2d_ms_array = create_dummy_bindless_handle(gl_info, textures->tex_2d_ms_array);
723 checkGLcall("create dummy textures");
725 wined3d_context_gl_bind_dummy_textures(context_gl);
728 /* Context activation is done by the caller. */
729 static void wined3d_device_gl_destroy_dummy_textures(struct wined3d_device_gl *device_gl,
730 struct wined3d_context_gl *context_gl)
732 struct wined3d_dummy_textures *dummy_textures = &device_gl->dummy_textures;
733 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
735 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
737 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
738 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
741 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
742 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
744 if (gl_info->supported[EXT_TEXTURE_ARRAY])
746 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
747 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
750 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
751 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
753 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
754 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
756 if (gl_info->supported[EXT_TEXTURE3D])
757 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
759 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
760 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
762 checkGLcall("delete dummy textures");
764 memset(dummy_textures, 0, sizeof(*dummy_textures));
767 /* Context activation is done by the caller. */
768 void wined3d_device_create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
770 struct wined3d_sampler_desc desc;
771 HRESULT hr;
773 desc.address_u = WINED3D_TADDRESS_WRAP;
774 desc.address_v = WINED3D_TADDRESS_WRAP;
775 desc.address_w = WINED3D_TADDRESS_WRAP;
776 memset(desc.border_color, 0, sizeof(desc.border_color));
777 desc.mag_filter = WINED3D_TEXF_POINT;
778 desc.min_filter = WINED3D_TEXF_POINT;
779 desc.mip_filter = WINED3D_TEXF_NONE;
780 desc.lod_bias = 0.0f;
781 desc.min_lod = -1000.0f;
782 desc.max_lod = 1000.0f;
783 desc.mip_base_level = 0;
784 desc.max_anisotropy = 1;
785 desc.compare = FALSE;
786 desc.comparison_func = WINED3D_CMP_NEVER;
787 desc.srgb_decode = TRUE;
789 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
790 * instructions allow access to resources without using samplers.
791 * In GLSL, resources are always accessed through sampler or image variables. The default
792 * sampler object is used to emulate the direct resource access when there is no sampler state
793 * to use.
795 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
797 ERR("Failed to create default sampler, hr %#lx.\n", hr);
798 device->default_sampler = NULL;
801 /* In D3D10+, a NULL sampler maps to the default sampler state. */
802 desc.address_u = WINED3D_TADDRESS_CLAMP;
803 desc.address_v = WINED3D_TADDRESS_CLAMP;
804 desc.address_w = WINED3D_TADDRESS_CLAMP;
805 desc.mag_filter = WINED3D_TEXF_LINEAR;
806 desc.min_filter = WINED3D_TEXF_LINEAR;
807 desc.mip_filter = WINED3D_TEXF_LINEAR;
808 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
810 ERR("Failed to create null sampler, hr %#lx.\n", hr);
811 device->null_sampler = NULL;
815 void wined3d_device_destroy_default_samplers(struct wined3d_device *device)
817 wined3d_sampler_decref(device->default_sampler);
818 device->default_sampler = NULL;
819 wined3d_sampler_decref(device->null_sampler);
820 device->null_sampler = NULL;
823 static bool wined3d_null_image_vk_init(struct wined3d_image_vk *image, struct wined3d_context_vk *context_vk,
824 VkCommandBuffer vk_command_buffer, VkImageType type, unsigned int layer_count, unsigned int sample_count)
826 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
827 VkImageSubresourceRange range;
828 uint32_t flags = 0;
830 static const VkClearColorValue colour = {{0}};
832 TRACE("image %p, context_vk %p, vk_command_buffer %p, type %#x, layer_count %u, sample_count %u.\n",
833 image, context_vk, vk_command_buffer, type, layer_count, sample_count);
835 if (type == VK_IMAGE_TYPE_2D && layer_count >= 6)
836 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
838 if (!wined3d_context_vk_create_image(context_vk, type,
839 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_FORMAT_R8G8B8A8_UNORM,
840 1, 1, 1, sample_count, 1, layer_count, flags, image))
842 return false;
845 wined3d_context_vk_reference_image(context_vk, image);
847 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
848 range.baseMipLevel = 0;
849 range.levelCount = 1;
850 range.baseArrayLayer = 0;
851 range.layerCount = layer_count;
853 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
854 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
855 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, image->vk_image, &range);
857 VK_CALL(vkCmdClearColorImage(vk_command_buffer, image->vk_image,
858 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour, 1, &range));
860 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
861 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0,
862 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, image->vk_image, &range);
864 TRACE("Created NULL image 0x%s, memory 0x%s.\n",
865 wine_dbgstr_longlong(image->vk_image), wine_dbgstr_longlong(image->vk_memory));
867 return true;
870 bool wined3d_device_vk_create_null_resources(struct wined3d_device_vk *device_vk,
871 struct wined3d_context_vk *context_vk)
873 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
874 const struct wined3d_vk_info *vk_info;
875 const struct wined3d_format *format;
876 VkMemoryPropertyFlags memory_type;
877 VkCommandBuffer vk_command_buffer;
878 unsigned int sample_count = 2;
879 VkBufferUsageFlags usage;
881 format = wined3d_get_format(device_vk->d.adapter, WINED3DFMT_R8G8B8A8_UNORM, WINED3D_BIND_SHADER_RESOURCE);
882 while (sample_count && !(sample_count & format->multisample_types))
883 sample_count <<= 1;
885 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
887 ERR("Failed to get command buffer.\n");
888 return false;
891 vk_info = context_vk->vk_info;
893 usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
894 | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
895 memory_type = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
896 if (!wined3d_context_vk_create_bo(context_vk, 16, usage, memory_type, &r->bo))
897 return false;
898 VK_CALL(vkCmdFillBuffer(vk_command_buffer, r->bo.vk_buffer, r->bo.b.buffer_offset, r->bo.size, 0x00000000u));
899 r->buffer_info.buffer = r->bo.vk_buffer;
900 r->buffer_info.offset = r->bo.b.buffer_offset;
901 r->buffer_info.range = r->bo.size;
903 if (!wined3d_null_image_vk_init(&r->image_1d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_1D, 1, 1))
905 ERR("Failed to create 1D image.\n");
906 goto fail;
909 if (!wined3d_null_image_vk_init(&r->image_2d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 6, 1))
911 ERR("Failed to create 2D image.\n");
912 goto fail;
915 if (!wined3d_null_image_vk_init(&r->image_2dms, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 1, sample_count))
917 ERR("Failed to create 2D MSAA image.\n");
918 goto fail;
921 if (!wined3d_null_image_vk_init(&r->image_3d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_3D, 1, 1))
923 ERR("Failed to create 3D image.\n");
924 goto fail;
927 return true;
929 fail:
930 if (r->image_2dms.vk_image)
931 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
932 if (r->image_2d.vk_image)
933 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
934 if (r->image_1d.vk_image)
935 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
936 wined3d_context_vk_reference_bo(context_vk, &r->bo);
937 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
938 return false;
941 void wined3d_device_vk_destroy_null_resources(struct wined3d_device_vk *device_vk,
942 struct wined3d_context_vk *context_vk)
944 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
946 /* We don't track command buffer references to NULL resources. We easily
947 * could, but it doesn't seem worth it. */
948 wined3d_context_vk_reference_image(context_vk, &r->image_3d);
949 wined3d_context_vk_destroy_image(context_vk, &r->image_3d);
950 wined3d_context_vk_reference_image(context_vk, &r->image_2dms);
951 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
952 wined3d_context_vk_reference_image(context_vk, &r->image_2d);
953 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
954 wined3d_context_vk_reference_image(context_vk, &r->image_1d);
955 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
956 wined3d_context_vk_reference_bo(context_vk, &r->bo);
957 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
960 bool wined3d_device_vk_create_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
962 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
963 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
964 VkBufferViewCreateInfo buffer_create_info;
965 VkImageViewUsageCreateInfoKHR usage_desc;
966 const struct wined3d_vk_info *vk_info;
967 VkImageViewCreateInfo view_desc;
968 VkResult vr;
970 vk_info = context_vk->vk_info;
972 buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
973 buffer_create_info.pNext = NULL;
974 buffer_create_info.flags = 0;
975 buffer_create_info.buffer = r->bo.vk_buffer;
976 buffer_create_info.format = VK_FORMAT_R32_UINT;
977 buffer_create_info.offset = r->bo.b.buffer_offset;
978 buffer_create_info.range = r->bo.size;
980 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
981 &buffer_create_info, NULL, &v->vk_view_buffer_uint))) < 0)
983 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
984 return false;
986 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_uint));
988 buffer_create_info.format = VK_FORMAT_R32G32B32A32_SFLOAT;
989 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
990 &buffer_create_info, NULL, &v->vk_view_buffer_float))) < 0)
992 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
993 goto fail;
995 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_float));
997 view_desc.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
998 view_desc.pNext = NULL;
999 view_desc.flags = 0;
1000 view_desc.image = r->image_1d.vk_image;
1001 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D;
1002 view_desc.format = VK_FORMAT_R8G8B8A8_UNORM;
1003 view_desc.components.r = VK_COMPONENT_SWIZZLE_ZERO;
1004 view_desc.components.g = VK_COMPONENT_SWIZZLE_ZERO;
1005 view_desc.components.b = VK_COMPONENT_SWIZZLE_ZERO;
1006 view_desc.components.a = VK_COMPONENT_SWIZZLE_ZERO;
1007 view_desc.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1008 view_desc.subresourceRange.baseMipLevel = 0;
1009 view_desc.subresourceRange.levelCount = 1;
1010 view_desc.subresourceRange.baseArrayLayer = 0;
1011 view_desc.subresourceRange.layerCount = 1;
1013 if (vk_info->supported[WINED3D_VK_KHR_MAINTENANCE2] || vk_info->api_version >= VK_API_VERSION_1_1)
1015 usage_desc.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO_KHR;
1016 usage_desc.pNext = NULL;
1017 usage_desc.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
1019 view_desc.pNext = &usage_desc;
1022 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d.imageView))) < 0)
1024 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
1025 goto fail;
1027 v->vk_info_1d.sampler = VK_NULL_HANDLE;
1028 v->vk_info_1d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1029 TRACE("Created 1D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d.imageView));
1031 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D_ARRAY;
1032 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d_array.imageView))) < 0)
1034 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
1035 goto fail;
1037 v->vk_info_1d_array.sampler = VK_NULL_HANDLE;
1038 v->vk_info_1d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1039 TRACE("Created 1D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d_array.imageView));
1041 view_desc.image = r->image_2d.vk_image;
1042 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
1043 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d.imageView))) < 0)
1045 ERR("Failed to create 2D image view, vr %s.\n", wined3d_debug_vkresult(vr));
1046 goto fail;
1048 v->vk_info_2d.sampler = VK_NULL_HANDLE;
1049 v->vk_info_2d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1050 TRACE("Created 2D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d.imageView));
1052 view_desc.image = r->image_2dms.vk_image;
1053 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
1054 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms.imageView))) < 0)
1056 ERR("Failed to create 2D MSAA image view, vr %s.\n", wined3d_debug_vkresult(vr));
1057 goto fail;
1059 v->vk_info_2dms.sampler = VK_NULL_HANDLE;
1060 v->vk_info_2dms.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1061 TRACE("Created 2D MSAA image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms.imageView));
1063 view_desc.image = r->image_3d.vk_image;
1064 view_desc.viewType = VK_IMAGE_VIEW_TYPE_3D;
1065 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_3d.imageView))) < 0)
1067 ERR("Failed to create 3D image view, vr %s.\n", wined3d_debug_vkresult(vr));
1068 goto fail;
1070 v->vk_info_3d.sampler = VK_NULL_HANDLE;
1071 v->vk_info_3d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1072 TRACE("Created 3D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_3d.imageView));
1074 view_desc.image = r->image_2d.vk_image;
1075 view_desc.subresourceRange.layerCount = 6;
1076 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
1077 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube.imageView))) < 0)
1079 ERR("Failed to create cube image view, vr %s.\n", wined3d_debug_vkresult(vr));
1080 goto fail;
1082 v->vk_info_cube.sampler = VK_NULL_HANDLE;
1083 v->vk_info_cube.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1084 TRACE("Created cube image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube.imageView));
1086 view_desc.subresourceRange.layerCount = 1;
1087 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
1088 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d_array.imageView))) < 0)
1090 ERR("Failed to create 2D array image view, vr %s.\n", wined3d_debug_vkresult(vr));
1091 goto fail;
1093 v->vk_info_2d_array.sampler = VK_NULL_HANDLE;
1094 v->vk_info_2d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1095 TRACE("Created 2D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d_array.imageView));
1097 view_desc.image = r->image_2dms.vk_image;
1098 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
1099 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms_array.imageView))) < 0)
1101 ERR("Failed to create 2D MSAA array image view, vr %s.\n", wined3d_debug_vkresult(vr));
1102 goto fail;
1104 v->vk_info_2dms_array.sampler = VK_NULL_HANDLE;
1105 v->vk_info_2dms_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1106 TRACE("Created 2D MSAA array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms_array.imageView));
1108 view_desc.image = r->image_2d.vk_image;
1109 view_desc.subresourceRange.layerCount = 6;
1110 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
1111 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube_array.imageView))) < 0)
1113 ERR("Failed to create cube array image view, vr %s.\n", wined3d_debug_vkresult(vr));
1114 goto fail;
1116 v->vk_info_cube_array.sampler = VK_NULL_HANDLE;
1117 v->vk_info_cube_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1118 TRACE("Created cube array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube_array.imageView));
1120 return true;
1122 fail:
1123 if (v->vk_info_cube_array.imageView)
1124 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube_array.imageView, NULL));
1125 if (v->vk_info_2d_array.imageView)
1126 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d_array.imageView, NULL));
1127 if (v->vk_info_cube.imageView)
1128 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube.imageView, NULL));
1129 if (v->vk_info_3d.imageView)
1130 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_3d.imageView, NULL));
1131 if (v->vk_info_2dms.imageView)
1132 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2dms.imageView, NULL));
1133 if (v->vk_info_2d.imageView)
1134 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d.imageView, NULL));
1135 if (v->vk_info_1d_array.imageView)
1136 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d_array.imageView, NULL));
1137 if (v->vk_info_1d.imageView)
1138 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d.imageView, NULL));
1139 if (v->vk_view_buffer_float)
1140 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_float, NULL));
1141 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_uint, NULL));
1142 return false;
1145 void wined3d_device_vk_destroy_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
1147 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
1148 uint64_t id = context_vk->current_command_buffer.id;
1150 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube_array.imageView, id);
1151 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms_array.imageView, id);
1152 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d_array.imageView, id);
1153 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube.imageView, id);
1154 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_3d.imageView, id);
1155 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms.imageView, id);
1156 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d.imageView, id);
1157 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d_array.imageView, id);
1158 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d.imageView, id);
1160 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_float, id);
1161 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_uint, id);
1164 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
1166 unsigned int screensaver_active;
1168 TRACE("device %p, window %p.\n", device, window);
1170 if (!wined3d_register_window(NULL, window, device, 0))
1172 ERR("Failed to register window %p.\n", window);
1173 return E_FAIL;
1176 InterlockedExchangePointer((void **)&device->focus_window, window);
1177 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1178 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
1179 if ((device->restore_screensaver = !!screensaver_active))
1180 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
1182 return WINED3D_OK;
1185 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
1187 TRACE("device %p.\n", device);
1189 if (device->focus_window) wined3d_unregister_window(device->focus_window);
1190 InterlockedExchangePointer((void **)&device->focus_window, NULL);
1191 if (device->restore_screensaver)
1193 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
1194 device->restore_screensaver = FALSE;
1198 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1200 struct wined3d_rendertarget_view *views[WINED3D_MAX_RENDER_TARGETS] = {0};
1201 BOOL ds_enable = swapchain->state.desc.enable_auto_depth_stencil;
1202 struct wined3d_device_context *context = &device->cs->c;
1204 if (device->back_buffer_view)
1205 views[0] = device->back_buffer_view;
1206 wined3d_device_context_set_rendertarget_views(context, 0,
1207 device->adapter->d3d_info.limits.max_rt_count, views, !!device->back_buffer_view);
1209 wined3d_device_context_set_depth_stencil_view(context, ds_enable ? device->auto_depth_stencil_view : NULL);
1212 static struct wined3d_allocator_chunk *wined3d_allocator_gl_create_chunk(struct wined3d_allocator *allocator,
1213 struct wined3d_context *context, unsigned int memory_type, size_t chunk_size)
1215 struct wined3d_allocator_chunk_gl *chunk_gl;
1216 struct wined3d_context_gl *context_gl;
1218 TRACE("allocator %p, context %p, memory_type %u, chunk_size %Iu.\n", allocator, context, memory_type, chunk_size);
1220 if (!context)
1221 return NULL;
1222 context_gl = wined3d_context_gl(context);
1224 if (!(chunk_gl = malloc(sizeof(*chunk_gl))))
1225 return NULL;
1227 if (!wined3d_allocator_chunk_init(&chunk_gl->c, allocator))
1229 free(chunk_gl);
1230 return NULL;
1233 chunk_gl->memory_type = memory_type;
1234 if (!(chunk_gl->gl_buffer = wined3d_context_gl_allocate_vram_chunk_buffer(context_gl, memory_type, chunk_size)))
1236 wined3d_allocator_chunk_cleanup(&chunk_gl->c);
1237 free(chunk_gl);
1238 return NULL;
1240 list_add_head(&allocator->pools[memory_type].chunks, &chunk_gl->c.entry);
1242 return &chunk_gl->c;
1245 static void wined3d_allocator_gl_destroy_chunk(struct wined3d_allocator_chunk *chunk)
1247 struct wined3d_device_gl *device_gl = wined3d_device_gl_from_allocator(chunk->allocator);
1248 struct wined3d_allocator_chunk_gl *chunk_gl = wined3d_allocator_chunk_gl(chunk);
1249 const struct wined3d_gl_info *gl_info;
1250 struct wined3d_context_gl *context_gl;
1252 TRACE("chunk %p.\n", chunk);
1254 context_gl = wined3d_context_gl(context_acquire(&device_gl->d, NULL, 0));
1255 gl_info = context_gl->gl_info;
1257 wined3d_context_gl_bind_bo(context_gl, GL_PIXEL_UNPACK_BUFFER, chunk_gl->gl_buffer);
1258 if (chunk_gl->c.map_ptr)
1259 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
1260 GL_EXTCALL(glDeleteBuffers(1, &chunk_gl->gl_buffer));
1261 TRACE("Freed buffer %u.\n", chunk_gl->gl_buffer);
1262 wined3d_allocator_chunk_cleanup(&chunk_gl->c);
1263 free(chunk_gl);
1265 context_release(&context_gl->c);
1268 static const struct wined3d_allocator_ops wined3d_allocator_gl_ops =
1270 .allocator_create_chunk = wined3d_allocator_gl_create_chunk,
1271 .allocator_destroy_chunk = wined3d_allocator_gl_destroy_chunk,
1274 static const struct
1276 GLbitfield flags;
1278 gl_memory_types[] =
1280 {0},
1281 {GL_MAP_READ_BIT},
1282 {GL_MAP_WRITE_BIT},
1283 {GL_MAP_READ_BIT | GL_MAP_WRITE_BIT},
1285 {GL_CLIENT_STORAGE_BIT},
1286 {GL_CLIENT_STORAGE_BIT | GL_MAP_READ_BIT},
1287 {GL_CLIENT_STORAGE_BIT | GL_MAP_WRITE_BIT},
1288 {GL_CLIENT_STORAGE_BIT | GL_MAP_READ_BIT | GL_MAP_WRITE_BIT},
1291 static unsigned int wined3d_device_gl_find_memory_type(GLbitfield flags)
1293 unsigned int i;
1295 for (i = 0; i < ARRAY_SIZE(gl_memory_types); ++i)
1297 if (gl_memory_types[i].flags == flags)
1298 return i;
1301 assert(0);
1302 return 0;
1305 GLbitfield wined3d_device_gl_get_memory_type_flags(unsigned int memory_type_idx)
1307 return gl_memory_types[memory_type_idx].flags;
1310 static struct wined3d_allocator_block *wined3d_device_gl_allocate_memory(struct wined3d_device_gl *device_gl,
1311 struct wined3d_context_gl *context_gl, unsigned int memory_type, GLsizeiptr size, GLuint *id)
1313 struct wined3d_allocator *allocator = &device_gl->allocator;
1314 struct wined3d_allocator_block *block;
1316 wined3d_device_gl_allocator_lock(device_gl);
1318 if (size > WINED3D_ALLOCATOR_CHUNK_SIZE / 2)
1320 if (context_gl)
1321 *id = wined3d_context_gl_allocate_vram_chunk_buffer(context_gl, memory_type, size);
1322 wined3d_device_gl_allocator_unlock(device_gl);
1323 return NULL;
1326 if (!(block = wined3d_allocator_allocate(allocator, context_gl ? &context_gl->c : NULL, memory_type, size)))
1328 wined3d_device_gl_allocator_unlock(device_gl);
1329 *id = 0;
1330 return NULL;
1333 *id = wined3d_allocator_chunk_gl(block->chunk)->gl_buffer;
1335 wined3d_device_gl_allocator_unlock(device_gl);
1336 TRACE("Allocated offset %#Ix from chunk %p.\n", block->offset, block->chunk);
1337 return block;
1340 static bool use_buffer_chunk_suballocation(struct wined3d_device_gl *device_gl,
1341 const struct wined3d_gl_info *gl_info, GLenum binding)
1343 switch (binding)
1345 case GL_ARRAY_BUFFER:
1346 case GL_ATOMIC_COUNTER_BUFFER:
1347 case GL_DRAW_INDIRECT_BUFFER:
1348 case GL_PIXEL_UNPACK_BUFFER:
1349 case GL_UNIFORM_BUFFER:
1350 return true;
1352 case GL_ELEMENT_ARRAY_BUFFER:
1353 /* There is no way to specify an element array buffer offset for
1354 * indirect draws in OpenGL. */
1355 return device_gl->d.wined3d->flags & WINED3D_NO_DRAW_INDIRECT
1356 || !gl_info->supported[ARB_DRAW_INDIRECT];
1358 case GL_TEXTURE_BUFFER:
1359 return gl_info->supported[ARB_TEXTURE_BUFFER_RANGE];
1361 default:
1362 return false;
1366 bool wined3d_device_gl_create_bo(struct wined3d_device_gl *device_gl, struct wined3d_context_gl *context_gl,
1367 GLsizeiptr size, GLenum binding, GLenum usage, bool coherent, GLbitfield flags, struct wined3d_bo_gl *bo)
1369 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl(device_gl->d.adapter)->gl_info;
1370 unsigned int memory_type_idx = wined3d_device_gl_find_memory_type(flags);
1371 struct wined3d_allocator_block *memory = NULL;
1372 GLsizeiptr buffer_offset = 0;
1373 GLuint id = 0;
1375 TRACE("device_gl %p, context_gl %p, size %Iu, binding %#x, usage %#x, coherent %#x, flags %#x, bo %p.\n",
1376 device_gl, context_gl, size, binding, usage, coherent, flags, bo);
1378 if (gl_info->supported[ARB_BUFFER_STORAGE])
1380 /* Only suballocate dynamic buffers.
1382 * We only need suballocation so that we can allocate GL buffers from
1383 * the client thread and thereby accelerate DISCARD maps.
1385 * For other buffer types, suballocating means that a whole-buffer
1386 * upload won't be replacing the whole buffer anymore. If the driver
1387 * isn't smart enough to track individual buffer ranges then it'll
1388 * force synchronizing that BO with the GPU. Even using ARB_sync
1389 * ourselves won't help here, because glBufferSubData() is still
1390 * implicitly synchronized. */
1391 if (flags & GL_CLIENT_STORAGE_BIT)
1393 if (use_buffer_chunk_suballocation(device_gl, gl_info, binding))
1395 if ((memory = wined3d_device_gl_allocate_memory(device_gl, context_gl, memory_type_idx, size, &id)))
1396 buffer_offset = memory->offset;
1397 else if (!context_gl)
1398 WARN_(d3d_perf)("Failed to suballocate buffer from the client thread.\n");
1400 else if (context_gl)
1402 WARN_(d3d_perf)("Not allocating chunk memory for binding type %#x.\n", binding);
1403 id = wined3d_context_gl_allocate_vram_chunk_buffer(context_gl, memory_type_idx, size);
1406 else
1408 id = wined3d_context_gl_allocate_vram_chunk_buffer(context_gl, memory_type_idx, size);
1411 if (!id)
1413 if (context_gl)
1414 WARN("Failed to allocate buffer.\n");
1415 return false;
1418 else
1420 if (!context_gl)
1421 return false;
1423 GL_EXTCALL(glGenBuffers(1, &id));
1424 if (!id)
1426 checkGLcall("buffer object creation");
1427 return false;
1429 TRACE("Created buffer object %u.\n", id);
1430 wined3d_context_gl_bind_bo(context_gl, binding, id);
1432 if (!coherent && gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
1434 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
1435 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
1438 GL_EXTCALL(glBufferData(binding, size, NULL, usage));
1440 wined3d_context_gl_bind_bo(context_gl, binding, 0);
1441 checkGLcall("buffer object creation");
1444 bo->id = id;
1445 bo->memory = memory;
1446 bo->size = size;
1447 bo->binding = binding;
1448 bo->usage = usage;
1449 bo->flags = flags;
1450 bo->b.coherent = coherent;
1451 list_init(&bo->b.users);
1452 bo->command_fence_id = 0;
1453 bo->b.buffer_offset = buffer_offset;
1454 bo->b.memory_offset = bo->b.buffer_offset;
1455 bo->b.map_ptr = NULL;
1456 bo->b.client_map_count = 0;
1457 bo->b.refcount = 1;
1459 return true;
1462 void wined3d_device_gl_delete_opengl_contexts_cs(void *object)
1464 struct wined3d_device_gl *device_gl = object;
1465 struct wined3d_context_gl *context_gl;
1466 struct wined3d_context *context;
1467 struct wined3d_device *device;
1468 struct wined3d_shader *shader;
1470 TRACE("device %p.\n", device_gl);
1472 device = &device_gl->d;
1474 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
1476 device->shader_backend->shader_destroy(shader);
1479 context = context_acquire(device, NULL, 0);
1480 context_gl = wined3d_context_gl(context);
1481 device->blitter->ops->blitter_destroy(device->blitter, context);
1482 device->shader_backend->shader_free_private(device, context);
1483 wined3d_device_gl_destroy_dummy_textures(device_gl, context_gl);
1485 if (context_gl->c.d3d_info->fences)
1487 wined3d_context_gl_submit_command_fence(context_gl);
1488 wined3d_context_gl_wait_command_fence(context_gl,
1489 wined3d_device_gl(context_gl->c.device)->current_fence_id - 1);
1491 wined3d_allocator_cleanup(&device_gl->allocator);
1493 context_release(context);
1495 while (device->context_count)
1496 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1498 if (device_gl->backup_dc)
1500 TRACE("Destroying backup wined3d window %p, dc %p.\n", device_gl->backup_wnd, device_gl->backup_dc);
1502 wined3d_release_dc(device_gl->backup_wnd, device_gl->backup_dc);
1503 DestroyWindow(device_gl->backup_wnd);
1507 void wined3d_device_gl_create_primary_opengl_context_cs(void *object)
1509 struct wined3d_device_gl *device_gl = object;
1510 struct wined3d_context_gl *context_gl;
1511 struct wined3d_swapchain *swapchain;
1512 struct wined3d_context *context;
1513 struct wined3d_texture *target;
1514 struct wined3d_device *device;
1515 HRESULT hr;
1517 TRACE("device %p.\n", device_gl);
1519 device = &device_gl->d;
1520 swapchain = device->swapchains[0];
1521 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1522 if (!(context = context_acquire(device, target, 0)))
1524 WARN("Failed to acquire context.\n");
1525 return;
1528 context_gl = wined3d_context_gl(context);
1530 if (!wined3d_allocator_init(&device_gl->allocator, ARRAY_SIZE(gl_memory_types), &wined3d_allocator_gl_ops))
1532 WARN("Failed to initialise allocator.\n");
1533 context_release(context);
1534 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1535 return;
1538 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1539 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1541 ERR("Failed to allocate shader private data, hr %#lx.\n", hr);
1542 wined3d_allocator_cleanup(&device_gl->allocator);
1543 context_release(context);
1544 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1545 return;
1548 if (!(device->blitter = wined3d_cpu_blitter_create()))
1550 ERR("Failed to create CPU blitter.\n");
1551 device->shader_backend->shader_free_private(device, NULL);
1552 wined3d_allocator_cleanup(&device_gl->allocator);
1553 context_release(context);
1554 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1555 return;
1558 wined3d_ffp_blitter_create(&device->blitter, context_gl->gl_info);
1559 wined3d_glsl_blitter_create(&device->blitter, device);
1560 wined3d_fbo_blitter_create(&device->blitter, context_gl->gl_info);
1561 wined3d_raw_blitter_create(&device->blitter, context_gl->gl_info);
1563 wined3d_device_gl_create_dummy_textures(device_gl, context_gl);
1564 wined3d_device_create_default_samplers(device, context);
1565 context_release(context);
1568 HRESULT wined3d_device_set_implicit_swapchain(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1570 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1571 const struct wined3d_swapchain_desc *swapchain_desc;
1572 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
1573 DWORD clear_flags = 0;
1574 unsigned int i;
1575 HRESULT hr;
1577 TRACE("device %p, swapchain %p.\n", device, swapchain);
1579 if (device->d3d_initialized)
1580 return WINED3DERR_INVALIDCALL;
1582 device->swapchain_count = 1;
1583 if (!(device->swapchains = calloc(device->swapchain_count, sizeof(*device->swapchains))))
1585 ERR("Failed to allocate swapchain array.\n");
1586 hr = E_OUTOFMEMORY;
1587 goto err_out;
1589 device->swapchains[0] = swapchain;
1591 for (i = 0; i < ARRAY_SIZE(fb->render_targets); ++i)
1593 if (fb->render_targets[i])
1594 wined3d_rtv_bind_count_dec(fb->render_targets[i]);
1596 memset(fb->render_targets, 0, sizeof(fb->render_targets));
1598 if (FAILED(hr = device->adapter->adapter_ops->adapter_init_3d(device)))
1599 goto err_out;
1600 device->d3d_initialized = TRUE;
1602 swapchain_desc = &swapchain->state.desc;
1603 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1605 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1606 struct wined3d_view_desc view_desc;
1608 view_desc.format_id = back_buffer->format->id;
1609 view_desc.flags = 0;
1610 view_desc.u.texture.level_idx = 0;
1611 view_desc.u.texture.level_count = 1;
1612 view_desc.u.texture.layer_idx = 0;
1613 view_desc.u.texture.layer_count = 1;
1614 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1615 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1617 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
1618 device->adapter->adapter_ops->adapter_uninit_3d(device);
1619 device->d3d_initialized = FALSE;
1620 goto err_out;
1624 device_init_swapchain_state(device, swapchain);
1626 TRACE("All defaults now set up.\n");
1628 /* Clear the screen. */
1629 if (device->back_buffer_view)
1630 clear_flags |= WINED3DCLEAR_TARGET;
1631 if (swapchain_desc->enable_auto_depth_stencil)
1632 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1633 if (clear_flags)
1634 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1636 if (wined3d_settings.logo)
1637 device_load_logo(device, wined3d_settings.logo);
1639 return WINED3D_OK;
1641 err_out:
1642 free(device->swapchains);
1643 device->swapchains = NULL;
1644 device->swapchain_count = 0;
1646 return hr;
1649 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1651 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1653 wined3d_sampler_decref(sampler);
1656 static void device_free_rasterizer_state(struct wine_rb_entry *entry, void *context)
1658 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
1660 wined3d_rasterizer_state_decref(state);
1663 static void device_free_blend_state(struct wine_rb_entry *entry, void *context)
1665 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
1667 wined3d_blend_state_decref(blend_state);
1670 static void device_free_depth_stencil_state(struct wine_rb_entry *entry, void *context)
1672 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
1674 wined3d_depth_stencil_state_decref(state);
1677 void wined3d_device_uninit_3d(struct wined3d_device *device)
1679 struct wined3d_state *state = device->cs->c.state;
1680 struct wined3d_resource *resource, *cursor;
1681 struct wined3d_rendertarget_view *view;
1682 struct wined3d_texture *texture;
1683 struct wined3d_buffer *buffer;
1684 unsigned int i;
1686 TRACE("device %p.\n", device);
1688 if (!device->d3d_initialized)
1690 ERR("Called while 3D support was not initialised.\n");
1691 return;
1694 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1696 device->swapchain_count = 0;
1698 if ((texture = device->logo_texture))
1700 device->logo_texture = NULL;
1701 wined3d_texture_decref(texture);
1704 if ((texture = device->cursor_texture))
1706 device->cursor_texture = NULL;
1707 wined3d_texture_decref(texture);
1710 for (i = 0; i < ARRAY_SIZE(device->push_constants); ++i)
1712 if ((buffer = device->push_constants[i]))
1713 wined3d_buffer_decref(buffer);
1715 memset(device->push_constants, 0, sizeof(device->push_constants));
1717 wined3d_device_context_emit_reset_state(&device->cs->c, true);
1718 state_cleanup(state);
1720 wine_rb_destroy(&device->samplers, device_free_sampler, NULL);
1721 wine_rb_destroy(&device->rasterizer_states, device_free_rasterizer_state, NULL);
1722 wine_rb_destroy(&device->blend_states, device_free_blend_state, NULL);
1723 wine_rb_destroy(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
1725 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1727 TRACE("Unloading resource %p.\n", resource);
1728 wined3d_cs_emit_unload_resource(device->cs, resource);
1731 device->adapter->adapter_ops->adapter_uninit_3d(device);
1732 device->d3d_initialized = FALSE;
1734 if ((view = device->auto_depth_stencil_view))
1736 device->auto_depth_stencil_view = NULL;
1737 if (wined3d_rendertarget_view_decref(view))
1738 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1741 if ((view = device->back_buffer_view))
1743 device->back_buffer_view = NULL;
1744 wined3d_rendertarget_view_decref(view);
1747 free(device->swapchains);
1748 device->swapchains = NULL;
1750 wined3d_state_reset(state, &device->adapter->d3d_info);
1753 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1754 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1755 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1757 * There is no way to deactivate thread safety once it is enabled.
1759 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1761 TRACE("device %p.\n", device);
1763 /* For now just store the flag (needed in case of ddraw). */
1764 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1767 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1769 const struct wined3d_driver_info *driver_info;
1771 TRACE("device %p.\n", device);
1773 driver_info = &device->adapter->driver_info;
1775 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1776 wine_dbgstr_longlong(driver_info->vram_bytes),
1777 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1778 wine_dbgstr_longlong(driver_info->vram_bytes - device->adapter->vram_bytes_used));
1780 return min(UINT_MAX, driver_info->vram_bytes) - device->adapter->vram_bytes_used;
1783 struct wined3d_buffer * CDECL wined3d_device_context_get_stream_output(struct wined3d_device_context *context,
1784 unsigned int idx, unsigned int *offset)
1786 TRACE("context %p, idx %u, offset %p.\n", context, idx, offset);
1788 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1790 WARN("Invalid stream output %u.\n", idx);
1791 return NULL;
1794 if (offset)
1795 *offset = context->state->stream_output[idx].offset;
1796 return context->state->stream_output[idx].buffer;
1799 HRESULT CDECL wined3d_device_context_get_stream_source(const struct wined3d_device_context *context,
1800 unsigned int stream_idx, struct wined3d_buffer **buffer, unsigned int *offset, unsigned int *stride)
1802 const struct wined3d_stream_state *stream;
1804 TRACE("context %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1805 context, stream_idx, buffer, offset, stride);
1807 if (stream_idx >= WINED3D_MAX_STREAMS)
1809 WARN("Stream index %u out of range.\n", stream_idx);
1810 return WINED3DERR_INVALIDCALL;
1813 stream = &context->state->streams[stream_idx];
1814 *buffer = stream->buffer;
1815 if (offset)
1816 *offset = stream->offset;
1817 *stride = stream->stride;
1819 return WINED3D_OK;
1822 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1823 const struct wined3d_clip_status *clip_status)
1825 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1827 if (!clip_status)
1828 return WINED3DERR_INVALIDCALL;
1830 return WINED3D_OK;
1833 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1834 struct wined3d_clip_status *clip_status)
1836 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1838 if (!clip_status)
1839 return WINED3DERR_INVALIDCALL;
1841 return WINED3D_OK;
1844 struct wined3d_buffer * CDECL wined3d_device_context_get_index_buffer(const struct wined3d_device_context *context,
1845 enum wined3d_format_id *format, unsigned int *offset)
1847 const struct wined3d_state *state = context->state;
1849 TRACE("context %p, format %p, offset %p.\n", context, format, offset);
1851 *format = state->index_format;
1852 if (offset)
1853 *offset = state->index_offset;
1854 return state->index_buffer;
1857 void CDECL wined3d_device_context_get_viewports(const struct wined3d_device_context *context,
1858 unsigned int *viewport_count, struct wined3d_viewport *viewports)
1860 const struct wined3d_state *state = context->state;
1861 unsigned int count;
1863 TRACE("context %p, viewport_count %p, viewports %p.\n", context, viewport_count, viewports);
1865 count = viewport_count ? min(*viewport_count, state->viewport_count) : 1;
1866 if (count && viewports)
1867 memcpy(viewports, state->viewports, count * sizeof(*viewports));
1868 if (viewport_count)
1869 *viewport_count = state->viewport_count;
1872 struct wined3d_blend_state * CDECL wined3d_device_context_get_blend_state(const struct wined3d_device_context *context,
1873 struct wined3d_color *blend_factor, unsigned int *sample_mask)
1875 const struct wined3d_state *state = context->state;
1877 TRACE("context %p, blend_factor %p, sample_mask %p.\n", context, blend_factor, sample_mask);
1879 *blend_factor = state->blend_factor;
1880 *sample_mask = state->sample_mask;
1881 return state->blend_state;
1884 struct wined3d_depth_stencil_state * CDECL wined3d_device_context_get_depth_stencil_state(
1885 const struct wined3d_device_context *context, unsigned int *stencil_ref)
1887 const struct wined3d_state *state = context->state;
1889 TRACE("context %p, stencil_ref %p.\n", context, stencil_ref);
1891 *stencil_ref = state->stencil_ref;
1892 return state->depth_stencil_state;
1895 struct wined3d_rasterizer_state * CDECL wined3d_device_context_get_rasterizer_state(
1896 struct wined3d_device_context *context)
1898 TRACE("context %p.\n", context);
1900 return context->state->rasterizer_state;
1903 void CDECL wined3d_device_context_get_scissor_rects(const struct wined3d_device_context *context,
1904 unsigned int *rect_count, RECT *rects)
1906 const struct wined3d_state *state = context->state;
1907 unsigned int count;
1909 TRACE("context %p, rect_count %p, rects %p.\n", context, rect_count, rects);
1911 if (rects && (count = rect_count ? min(*rect_count, state->scissor_rect_count) : 1))
1912 memcpy(rects, state->scissor_rects, count * sizeof(*rects));
1913 if (rect_count)
1914 *rect_count = state->scissor_rect_count;
1917 void CDECL wined3d_device_context_reset_state(struct wined3d_device_context *context)
1919 TRACE("context %p.\n", context);
1921 wined3d_device_context_lock(context);
1922 state_cleanup(context->state);
1923 wined3d_state_reset(context->state, &context->device->adapter->d3d_info);
1924 wined3d_device_context_emit_reset_state(context, true);
1925 wined3d_device_context_unlock(context);
1928 void CDECL wined3d_device_context_set_state(struct wined3d_device_context *context, struct wined3d_state *state)
1930 unsigned int i;
1932 TRACE("context %p, state %p.\n", context, state);
1934 wined3d_device_context_lock(context);
1935 context->state = state;
1936 wined3d_device_context_emit_set_feature_level(context, state->feature_level);
1938 wined3d_device_context_emit_set_rendertarget_views(context, 0,
1939 ARRAY_SIZE(state->fb.render_targets), state->fb.render_targets);
1941 wined3d_device_context_emit_set_depth_stencil_view(context, state->fb.depth_stencil);
1942 wined3d_device_context_emit_set_vertex_declaration(context, state->vertex_declaration);
1944 wined3d_device_context_emit_set_stream_outputs(context, state->stream_output);
1946 wined3d_device_context_emit_set_stream_sources(context, 0, WINED3D_MAX_STREAMS, state->streams);
1948 wined3d_device_context_emit_set_index_buffer(context, state->index_buffer,
1949 state->index_format, state->index_offset);
1951 wined3d_device_context_emit_set_predication(context, state->predicate, state->predicate_value);
1953 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1955 wined3d_device_context_emit_set_shader(context, i, state->shader[i]);
1956 wined3d_device_context_emit_set_constant_buffers(context, i, 0, MAX_CONSTANT_BUFFERS, state->cb[i]);
1957 wined3d_device_context_emit_set_samplers(context, i, 0, MAX_SAMPLER_OBJECTS, state->sampler[i]);
1958 wined3d_device_context_emit_set_shader_resource_views(context, i, 0,
1959 MAX_SHADER_RESOURCE_VIEWS, state->shader_resource_view[i]);
1962 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
1963 wined3d_device_context_emit_set_unordered_access_views(context, i, 0, MAX_UNORDERED_ACCESS_VIEWS,
1964 state->unordered_access_view[i], NULL);
1966 wined3d_device_context_emit_set_viewports(context, state->viewport_count, state->viewports);
1967 wined3d_device_context_emit_set_scissor_rects(context, state->scissor_rect_count, state->scissor_rects);
1969 wined3d_device_context_emit_set_blend_state(context, state->blend_state, &state->blend_factor, state->sample_mask);
1970 wined3d_device_context_emit_set_depth_stencil_state(context, state->depth_stencil_state, state->stencil_ref);
1971 wined3d_device_context_emit_set_rasterizer_state(context, state->rasterizer_state);
1972 wined3d_device_context_unlock(context);
1975 struct wined3d_state * CDECL wined3d_device_get_state(struct wined3d_device *device)
1977 TRACE("device %p.\n", device);
1979 return device->cs->c.state;
1982 struct wined3d_device_context * CDECL wined3d_device_get_immediate_context(struct wined3d_device *device)
1984 TRACE("device %p.\n", device);
1986 return &device->cs->c;
1989 struct wined3d_vertex_declaration * CDECL wined3d_device_context_get_vertex_declaration(
1990 const struct wined3d_device_context *context)
1992 TRACE("context %p.\n", context);
1994 return context->state->vertex_declaration;
1997 void CDECL wined3d_device_context_set_shader(struct wined3d_device_context *context,
1998 enum wined3d_shader_type type, struct wined3d_shader *shader)
2000 struct wined3d_state *state = context->state;
2001 struct wined3d_shader *prev;
2003 TRACE("context %p, type %#x, shader %p.\n", context, type, shader);
2005 wined3d_device_context_lock(context);
2006 prev = state->shader[type];
2007 if (shader == prev)
2008 goto out;
2010 if (shader)
2011 wined3d_shader_incref(shader);
2012 state->shader[type] = shader;
2013 wined3d_device_context_emit_set_shader(context, type, shader);
2014 if (prev)
2015 wined3d_shader_decref(prev);
2016 out:
2017 wined3d_device_context_unlock(context);
2020 struct wined3d_shader * CDECL wined3d_device_context_get_shader(const struct wined3d_device_context *context,
2021 enum wined3d_shader_type type)
2023 TRACE("context %p, type %#x.\n", context, type);
2025 return context->state->shader[type];
2028 void CDECL wined3d_device_context_set_constant_buffers(struct wined3d_device_context *context,
2029 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
2030 const struct wined3d_constant_buffer_state *buffers)
2032 struct wined3d_state *state = context->state;
2033 unsigned int i;
2035 TRACE("context %p, type %#x, start_idx %u, count %u, buffers %p.\n", context, type, start_idx, count, buffers);
2037 if (!wined3d_bound_range(start_idx, count, MAX_CONSTANT_BUFFERS))
2039 WARN("Invalid constant buffer index %u, count %u.\n", start_idx, count);
2040 return;
2043 wined3d_device_context_lock(context);
2044 if (!memcmp(buffers, &state->cb[type][start_idx], count * sizeof(*buffers)))
2045 goto out;
2047 wined3d_device_context_emit_set_constant_buffers(context, type, start_idx, count, buffers);
2048 for (i = 0; i < count; ++i)
2050 struct wined3d_buffer *prev = state->cb[type][start_idx + i].buffer;
2051 struct wined3d_buffer *buffer = buffers[i].buffer;
2053 if (buffer)
2054 wined3d_buffer_incref(buffer);
2055 state->cb[type][start_idx + i] = buffers[i];
2056 if (prev)
2057 wined3d_buffer_decref(prev);
2059 out:
2060 wined3d_device_context_unlock(context);
2063 void CDECL wined3d_device_context_set_blend_state(struct wined3d_device_context *context,
2064 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
2066 struct wined3d_state *state = context->state;
2067 struct wined3d_blend_state *prev;
2069 TRACE("context %p, blend_state %p, blend_factor %p, sample_mask %#x.\n",
2070 context, blend_state, blend_factor, sample_mask);
2072 wined3d_device_context_lock(context);
2073 prev = state->blend_state;
2074 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor))
2075 && sample_mask == state->sample_mask)
2076 goto out;
2078 if (blend_state)
2079 wined3d_blend_state_incref(blend_state);
2080 state->blend_state = blend_state;
2081 state->blend_factor = *blend_factor;
2082 state->sample_mask = sample_mask;
2083 wined3d_device_context_emit_set_blend_state(context, blend_state, blend_factor, sample_mask);
2084 if (prev)
2085 wined3d_blend_state_decref(prev);
2086 out:
2087 wined3d_device_context_unlock(context);
2090 void CDECL wined3d_device_context_set_depth_stencil_state(struct wined3d_device_context *context,
2091 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
2093 struct wined3d_state *state = context->state;
2094 struct wined3d_depth_stencil_state *prev;
2096 TRACE("context %p, depth_stencil_state %p, stencil_ref %u.\n", context, depth_stencil_state, stencil_ref);
2098 wined3d_device_context_lock(context);
2099 prev = state->depth_stencil_state;
2100 if (prev == depth_stencil_state && state->stencil_ref == stencil_ref)
2101 goto out;
2103 if (depth_stencil_state)
2104 wined3d_depth_stencil_state_incref(depth_stencil_state);
2105 state->depth_stencil_state = depth_stencil_state;
2106 state->stencil_ref = stencil_ref;
2107 wined3d_device_context_emit_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
2108 if (prev)
2109 wined3d_depth_stencil_state_decref(prev);
2110 out:
2111 wined3d_device_context_unlock(context);
2114 void CDECL wined3d_device_context_set_rasterizer_state(struct wined3d_device_context *context,
2115 struct wined3d_rasterizer_state *rasterizer_state)
2117 struct wined3d_state *state = context->state;
2118 struct wined3d_rasterizer_state *prev;
2120 TRACE("context %p, rasterizer_state %p.\n", context, rasterizer_state);
2122 wined3d_device_context_lock(context);
2123 prev = state->rasterizer_state;
2124 if (prev == rasterizer_state)
2125 goto out;
2127 if (rasterizer_state)
2128 wined3d_rasterizer_state_incref(rasterizer_state);
2129 state->rasterizer_state = rasterizer_state;
2130 wined3d_device_context_emit_set_rasterizer_state(context, rasterizer_state);
2131 if (prev)
2132 wined3d_rasterizer_state_decref(prev);
2133 out:
2134 wined3d_device_context_unlock(context);
2137 void CDECL wined3d_device_context_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
2138 const struct wined3d_viewport *viewports)
2140 struct wined3d_state *state = context->state;
2141 unsigned int i;
2143 TRACE("context %p, viewport_count %u, viewports %p.\n", context, viewport_count, viewports);
2145 for (i = 0; i < viewport_count; ++i)
2147 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
2148 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
2151 wined3d_device_context_lock(context);
2152 if (viewport_count)
2153 memcpy(state->viewports, viewports, viewport_count * sizeof(*viewports));
2154 else
2155 memset(state->viewports, 0, sizeof(state->viewports));
2156 state->viewport_count = viewport_count;
2158 wined3d_device_context_emit_set_viewports(context, viewport_count, viewports);
2159 wined3d_device_context_unlock(context);
2162 void CDECL wined3d_device_context_set_scissor_rects(struct wined3d_device_context *context, unsigned int rect_count,
2163 const RECT *rects)
2165 struct wined3d_state *state = context->state;
2166 unsigned int i;
2168 TRACE("context %p, rect_count %u, rects %p.\n", context, rect_count, rects);
2170 for (i = 0; i < rect_count; ++i)
2172 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
2175 wined3d_device_context_lock(context);
2176 if (state->scissor_rect_count == rect_count
2177 && !memcmp(state->scissor_rects, rects, rect_count * sizeof(*rects)))
2179 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
2180 goto out;
2183 if (rect_count)
2184 memcpy(state->scissor_rects, rects, rect_count * sizeof(*rects));
2185 else
2186 memset(state->scissor_rects, 0, sizeof(state->scissor_rects));
2187 state->scissor_rect_count = rect_count;
2189 wined3d_device_context_emit_set_scissor_rects(context, rect_count, rects);
2190 out:
2191 wined3d_device_context_unlock(context);
2194 void CDECL wined3d_device_context_set_shader_resource_views(struct wined3d_device_context *context,
2195 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
2196 struct wined3d_shader_resource_view *const *const views)
2198 struct wined3d_shader_resource_view *real_views[MAX_SHADER_RESOURCE_VIEWS];
2199 struct wined3d_state *state = context->state;
2200 const struct wined3d_rendertarget_view *dsv = state->fb.depth_stencil;
2201 unsigned int i;
2203 TRACE("context %p, type %#x, start_idx %u, count %u, views %p.\n", context, type, start_idx, count, views);
2205 if (!wined3d_bound_range(start_idx, count, MAX_SHADER_RESOURCE_VIEWS))
2207 WARN("Invalid view index %u, count %u.\n", start_idx, count);
2208 return;
2211 wined3d_device_context_lock(context);
2212 if (!memcmp(views, &state->shader_resource_view[type][start_idx], count * sizeof(*views)))
2213 goto out;
2215 memcpy(real_views, views, count * sizeof(*views));
2217 for (i = 0; i < count; ++i)
2219 struct wined3d_shader_resource_view *view = real_views[i];
2221 if (view && (wined3d_is_srv_rtv_bound(state, view)
2222 || (dsv && dsv->resource == view->resource && wined3d_dsv_srv_conflict(dsv, view->format))))
2224 WARN("Application is trying to bind resource which is attached as render target.\n");
2225 real_views[i] = NULL;
2229 wined3d_device_context_emit_set_shader_resource_views(context, type, start_idx, count, real_views);
2230 for (i = 0; i < count; ++i)
2232 struct wined3d_shader_resource_view *prev = state->shader_resource_view[type][start_idx + i];
2233 struct wined3d_shader_resource_view *view = real_views[i];
2235 if (view)
2237 wined3d_shader_resource_view_incref(view);
2238 wined3d_srv_bind_count_inc(view);
2241 state->shader_resource_view[type][start_idx + i] = view;
2242 if (prev)
2244 wined3d_srv_bind_count_dec(prev);
2245 wined3d_shader_resource_view_decref(prev);
2248 out:
2249 wined3d_device_context_unlock(context);
2252 void CDECL wined3d_device_context_set_samplers(struct wined3d_device_context *context, enum wined3d_shader_type type,
2253 unsigned int start_idx, unsigned int count, struct wined3d_sampler *const *samplers)
2255 struct wined3d_state *state = context->state;
2256 unsigned int i;
2258 TRACE("context %p, type %#x, start_idx %u, count %u, samplers %p.\n", context, type, start_idx, count, samplers);
2260 if (!wined3d_bound_range(start_idx, count, MAX_SAMPLER_OBJECTS))
2262 WARN("Invalid sampler index %u, count %u.\n", start_idx, count);
2263 return;
2266 wined3d_device_context_lock(context);
2267 if (!memcmp(samplers, &state->sampler[type][start_idx], count * sizeof(*samplers)))
2268 goto out;
2270 wined3d_device_context_emit_set_samplers(context, type, start_idx, count, samplers);
2271 for (i = 0; i < count; ++i)
2273 struct wined3d_sampler *prev = state->sampler[type][start_idx + i];
2274 struct wined3d_sampler *sampler = samplers[i];
2276 if (sampler)
2277 wined3d_sampler_incref(sampler);
2278 state->sampler[type][start_idx + i] = sampler;
2279 if (prev)
2280 wined3d_sampler_decref(prev);
2282 out:
2283 wined3d_device_context_unlock(context);
2286 void CDECL wined3d_device_context_set_unordered_access_views(struct wined3d_device_context *context,
2287 enum wined3d_pipeline pipeline, unsigned int start_idx, unsigned int count,
2288 struct wined3d_unordered_access_view *const *uavs, const unsigned int *initial_counts)
2290 struct wined3d_state *state = context->state;
2291 unsigned int i;
2293 TRACE("context %p, pipeline %#x, start_idx %u, count %u, uavs %p, initial_counts %p.\n",
2294 context, pipeline, start_idx, count, uavs, initial_counts);
2296 if (!wined3d_bound_range(start_idx, count, MAX_UNORDERED_ACCESS_VIEWS))
2298 WARN("Invalid UAV index %u, count %u.\n", start_idx, count);
2299 return;
2302 wined3d_device_context_lock(context);
2303 if (!memcmp(uavs, &state->unordered_access_view[pipeline][start_idx], count * sizeof(*uavs)) && !initial_counts)
2304 goto out;
2306 wined3d_device_context_emit_set_unordered_access_views(context, pipeline, start_idx, count, uavs, initial_counts);
2307 for (i = 0; i < count; ++i)
2309 struct wined3d_unordered_access_view *prev = state->unordered_access_view[pipeline][start_idx + i];
2310 struct wined3d_unordered_access_view *uav = uavs[i];
2312 if (uav)
2313 wined3d_unordered_access_view_incref(uav);
2314 state->unordered_access_view[pipeline][start_idx + i] = uav;
2315 if (prev)
2316 wined3d_unordered_access_view_decref(prev);
2318 out:
2319 wined3d_device_context_unlock(context);
2322 void CDECL wined3d_device_context_set_render_targets_and_unordered_access_views(struct wined3d_device_context *context,
2323 unsigned int rtv_count, struct wined3d_rendertarget_view *const *render_target_views,
2324 struct wined3d_rendertarget_view *depth_stencil_view, UINT uav_count,
2325 struct wined3d_unordered_access_view *const *unordered_access_views, const unsigned int *initial_counts)
2327 wined3d_device_context_lock(context);
2328 if (rtv_count != ~0u)
2330 if (depth_stencil_view && !(depth_stencil_view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2332 WARN("View resource %p has incompatible %s bind flags.\n",
2333 depth_stencil_view->resource, wined3d_debug_bind_flags(depth_stencil_view->resource->bind_flags));
2334 goto out;
2337 if (FAILED(wined3d_device_context_set_rendertarget_views(context, 0, rtv_count,
2338 render_target_views, FALSE)))
2339 goto out;
2341 wined3d_device_context_set_depth_stencil_view(context, depth_stencil_view);
2344 if (uav_count != ~0u)
2346 wined3d_device_context_set_unordered_access_views(context, WINED3D_PIPELINE_GRAPHICS, 0, uav_count,
2347 unordered_access_views, initial_counts);
2349 out:
2350 wined3d_device_context_unlock(context);
2353 static void wined3d_device_context_unbind_srv_for_rtv(struct wined3d_device_context *context,
2354 const struct wined3d_rendertarget_view *view, BOOL dsv)
2356 const struct wined3d_state *state = context->state;
2357 const struct wined3d_resource *resource;
2359 if (!view)
2360 return;
2361 resource = view->resource;
2363 if (resource->srv_bind_count_device)
2365 const struct wined3d_shader_resource_view *srv;
2366 unsigned int i, j;
2368 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2370 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
2372 if ((srv = state->shader_resource_view[i][j]) && srv->resource == resource
2373 && ((!dsv && wined3d_is_srv_rtv_bound(state, srv))
2374 || (dsv && wined3d_dsv_srv_conflict(view, srv->format))))
2376 static struct wined3d_shader_resource_view *const null_srv;
2378 WARN("Application sets bound resource as render target.\n");
2379 wined3d_device_context_set_shader_resource_views(context, i, j, 1, &null_srv);
2386 HRESULT CDECL wined3d_device_context_set_rendertarget_views(struct wined3d_device_context *context,
2387 unsigned int start_idx, unsigned int count, struct wined3d_rendertarget_view *const *views, BOOL set_viewport)
2389 struct wined3d_state *state = context->state;
2390 unsigned int i, max_rt_count;
2392 TRACE("context %p, start_idx %u, count %u, views %p, set_viewport %#x.\n",
2393 context, start_idx, count, views, set_viewport);
2395 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
2396 if (start_idx >= max_rt_count)
2398 WARN("Only %u render targets are supported.\n", max_rt_count);
2399 return WINED3DERR_INVALIDCALL;
2401 count = min(count, max_rt_count - start_idx);
2403 for (i = 0; i < count; ++i)
2405 if (views[i] && !(views[i]->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
2407 WARN("View resource %p doesn't have render target bind flags.\n", views[i]->resource);
2408 return WINED3DERR_INVALIDCALL;
2412 wined3d_device_context_lock(context);
2413 /* Set the viewport and scissor rectangles, if requested. Tests show that
2414 * stateblock recording is ignored, the change goes directly into the
2415 * primary stateblock. */
2416 if (!start_idx && set_viewport)
2418 state->viewports[0].x = 0;
2419 state->viewports[0].y = 0;
2420 state->viewports[0].width = views[0]->width;
2421 state->viewports[0].height = views[0]->height;
2422 state->viewports[0].min_z = 0.0f;
2423 state->viewports[0].max_z = 1.0f;
2424 state->viewport_count = 1;
2425 wined3d_device_context_emit_set_viewports(context, 1, state->viewports);
2427 SetRect(&state->scissor_rects[0], 0, 0, views[0]->width, views[0]->height);
2428 state->scissor_rect_count = 1;
2429 wined3d_device_context_emit_set_scissor_rects(context, 1, state->scissor_rects);
2432 if (!memcmp(views, &state->fb.render_targets[start_idx], count * sizeof(*views)))
2433 goto out;
2435 wined3d_device_context_emit_set_rendertarget_views(context, start_idx, count, views);
2436 for (i = 0; i < count; ++i)
2438 struct wined3d_rendertarget_view *prev = state->fb.render_targets[start_idx + i];
2439 struct wined3d_rendertarget_view *view = views[i];
2441 if (view)
2443 wined3d_rendertarget_view_incref(view);
2444 wined3d_rtv_bind_count_inc(view);
2446 state->fb.render_targets[start_idx + i] = view;
2447 /* Release after the assignment, to prevent device_resource_released()
2448 * from seeing the resource as still in use. */
2449 if (prev)
2451 wined3d_rtv_bind_count_dec(prev);
2452 wined3d_rendertarget_view_decref(prev);
2455 wined3d_device_context_unbind_srv_for_rtv(context, view, FALSE);
2457 out:
2458 wined3d_device_context_unlock(context);
2459 return WINED3D_OK;
2462 HRESULT CDECL wined3d_device_context_set_depth_stencil_view(struct wined3d_device_context *context,
2463 struct wined3d_rendertarget_view *view)
2465 struct wined3d_fb_state *fb = &context->state->fb;
2466 struct wined3d_rendertarget_view *prev;
2468 TRACE("context %p, view %p.\n", context, view);
2470 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2472 WARN("View resource %p has incompatible %s bind flags.\n",
2473 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
2474 return WINED3DERR_INVALIDCALL;
2477 wined3d_device_context_lock(context);
2478 prev = fb->depth_stencil;
2479 if (prev == view)
2481 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
2482 goto out;
2485 if ((fb->depth_stencil = view))
2486 wined3d_rendertarget_view_incref(view);
2487 wined3d_device_context_emit_set_depth_stencil_view(context, view);
2488 if (prev)
2489 wined3d_rendertarget_view_decref(prev);
2490 wined3d_device_context_unbind_srv_for_rtv(context, view, TRUE);
2491 out:
2492 wined3d_device_context_unlock(context);
2493 return WINED3D_OK;
2496 void CDECL wined3d_device_context_set_predication(struct wined3d_device_context *context,
2497 struct wined3d_query *predicate, BOOL value)
2499 struct wined3d_state *state = context->state;
2500 struct wined3d_query *prev;
2502 TRACE("context %p, predicate %p, value %#x.\n", context, predicate, value);
2504 wined3d_device_context_lock(context);
2505 prev = state->predicate;
2506 if (predicate)
2508 FIXME("Predicated rendering not implemented.\n");
2509 wined3d_query_incref(predicate);
2511 state->predicate = predicate;
2512 state->predicate_value = value;
2513 wined3d_device_context_emit_set_predication(context, predicate, value);
2514 if (prev)
2515 wined3d_query_decref(prev);
2516 wined3d_device_context_unlock(context);
2519 HRESULT CDECL wined3d_device_context_set_stream_sources(struct wined3d_device_context *context,
2520 unsigned int start_idx, unsigned int count, const struct wined3d_stream_state *streams)
2522 struct wined3d_state *state = context->state;
2523 unsigned int i;
2525 TRACE("context %p, start_idx %u, count %u, streams %p.\n", context, start_idx, count, streams);
2527 if (start_idx >= WINED3D_MAX_STREAMS)
2529 WARN("Start index %u is out of range.\n", start_idx);
2530 return WINED3DERR_INVALIDCALL;
2533 count = min(count, WINED3D_MAX_STREAMS - start_idx);
2535 for (i = 0; i < count; ++i)
2537 if (streams[i].offset & 0x3)
2539 WARN("Offset %u is not 4 byte aligned.\n", streams[i].offset);
2540 return WINED3DERR_INVALIDCALL;
2544 wined3d_device_context_lock(context);
2545 if (!memcmp(streams, &state->streams[start_idx], count * sizeof(*streams)))
2546 goto out;
2548 wined3d_device_context_emit_set_stream_sources(context, start_idx, count, streams);
2549 for (i = 0; i < count; ++i)
2551 struct wined3d_buffer *prev = state->streams[start_idx + i].buffer;
2552 struct wined3d_buffer *buffer = streams[i].buffer;
2554 state->streams[start_idx + i] = streams[i];
2556 if (buffer)
2557 wined3d_buffer_incref(buffer);
2558 if (prev)
2559 wined3d_buffer_decref(prev);
2561 out:
2562 wined3d_device_context_unlock(context);
2563 return WINED3D_OK;
2566 void CDECL wined3d_device_context_set_index_buffer(struct wined3d_device_context *context,
2567 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
2569 struct wined3d_state *state = context->state;
2570 enum wined3d_format_id prev_format;
2571 struct wined3d_buffer *prev_buffer;
2572 unsigned int prev_offset;
2574 TRACE("context %p, buffer %p, format %s, offset %u.\n",
2575 context, buffer, debug_d3dformat(format_id), offset);
2577 wined3d_device_context_lock(context);
2578 prev_buffer = state->index_buffer;
2579 prev_format = state->index_format;
2580 prev_offset = state->index_offset;
2582 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
2583 goto out;
2585 if (buffer)
2586 wined3d_buffer_incref(buffer);
2587 state->index_buffer = buffer;
2588 state->index_format = format_id;
2589 state->index_offset = offset;
2590 wined3d_device_context_emit_set_index_buffer(context, buffer, format_id, offset);
2591 if (prev_buffer)
2592 wined3d_buffer_decref(prev_buffer);
2593 out:
2594 wined3d_device_context_unlock(context);
2597 void CDECL wined3d_device_context_set_vertex_declaration(struct wined3d_device_context *context,
2598 struct wined3d_vertex_declaration *declaration)
2600 struct wined3d_state *state = context->state;
2601 struct wined3d_vertex_declaration *prev;
2603 TRACE("context %p, declaration %p.\n", context, declaration);
2605 wined3d_device_context_lock(context);
2606 prev = state->vertex_declaration;
2607 if (declaration == prev)
2608 goto out;
2610 if (declaration)
2611 wined3d_vertex_declaration_incref(declaration);
2612 state->vertex_declaration = declaration;
2613 wined3d_device_context_emit_set_vertex_declaration(context, declaration);
2614 if (prev)
2615 wined3d_vertex_declaration_decref(prev);
2616 out:
2617 wined3d_device_context_unlock(context);
2620 void CDECL wined3d_device_context_set_stream_outputs(struct wined3d_device_context *context,
2621 const struct wined3d_stream_output outputs[WINED3D_MAX_STREAM_OUTPUT_BUFFERS])
2623 struct wined3d_state *state = context->state;
2624 unsigned int i;
2626 TRACE("context %p, outputs %p.\n", context, outputs);
2628 wined3d_device_context_lock(context);
2629 wined3d_device_context_emit_set_stream_outputs(context, outputs);
2630 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
2632 struct wined3d_buffer *prev_buffer = state->stream_output[i].buffer;
2633 struct wined3d_buffer *buffer = outputs[i].buffer;
2635 if (buffer)
2636 wined3d_buffer_incref(buffer);
2637 state->stream_output[i] = outputs[i];
2638 if (prev_buffer)
2639 wined3d_buffer_decref(prev_buffer);
2641 wined3d_device_context_unlock(context);
2644 void CDECL wined3d_device_context_draw(struct wined3d_device_context *context, unsigned int start_vertex,
2645 unsigned int vertex_count, unsigned int start_instance, unsigned int instance_count)
2647 struct wined3d_state *state = context->state;
2649 TRACE("context %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
2650 context, start_vertex, vertex_count, start_instance, instance_count);
2652 wined3d_device_context_lock(context);
2653 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2654 0, start_vertex, vertex_count, start_instance, instance_count, false);
2655 wined3d_device_context_unlock(context);
2658 void CDECL wined3d_device_context_draw_indexed(struct wined3d_device_context *context, int base_vertex_index,
2659 unsigned int start_index, unsigned int index_count, unsigned int start_instance, unsigned int instance_count)
2661 struct wined3d_state *state = context->state;
2663 TRACE("context %p, base_vertex_index %d, start_index %u, index_count %u, start_instance %u, instance_count %u.\n",
2664 context, base_vertex_index, start_index, index_count, start_instance, instance_count);
2666 wined3d_device_context_lock(context);
2667 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2668 base_vertex_index, start_index, index_count, start_instance, instance_count, true);
2669 wined3d_device_context_unlock(context);
2672 void CDECL wined3d_device_context_get_constant_buffer(const struct wined3d_device_context *context,
2673 enum wined3d_shader_type shader_type, unsigned int idx, struct wined3d_constant_buffer_state *state)
2675 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2677 if (idx >= MAX_CONSTANT_BUFFERS)
2679 WARN("Invalid constant buffer index %u.\n", idx);
2680 return;
2683 *state = context->state->cb[shader_type][idx];
2686 struct wined3d_shader_resource_view * CDECL wined3d_device_context_get_shader_resource_view(
2687 const struct wined3d_device_context *context, enum wined3d_shader_type shader_type, unsigned int idx)
2689 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2691 WARN("Invalid view index %u.\n", idx);
2692 return NULL;
2695 return context->state->shader_resource_view[shader_type][idx];
2698 struct wined3d_sampler * CDECL wined3d_device_context_get_sampler(const struct wined3d_device_context *context,
2699 enum wined3d_shader_type shader_type, unsigned int idx)
2701 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2703 if (idx >= MAX_SAMPLER_OBJECTS)
2705 WARN("Invalid sampler index %u.\n", idx);
2706 return NULL;
2709 return context->state->sampler[shader_type][idx];
2712 struct wined3d_unordered_access_view * CDECL wined3d_device_context_get_unordered_access_view(
2713 const struct wined3d_device_context *context, enum wined3d_pipeline pipeline, unsigned int idx)
2715 TRACE("context %p, pipeline %#x, idx %u.\n", context, pipeline, idx);
2717 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2719 WARN("Invalid UAV index %u.\n", idx);
2720 return NULL;
2723 return context->state->unordered_access_view[pipeline][idx];
2726 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
2728 unsigned int i;
2730 if (!latency)
2731 latency = 3;
2733 device->max_frame_latency = latency;
2734 for (i = 0; i < device->swapchain_count; ++i)
2735 swapchain_set_max_frame_latency(device->swapchains[i], device);
2738 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
2740 return device->max_frame_latency;
2743 static unsigned int wined3d_get_flexible_vertex_size(uint32_t fvf)
2745 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2746 unsigned int i, size = 0;
2748 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2749 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2750 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2751 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2752 switch (fvf & WINED3DFVF_POSITION_MASK)
2754 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2755 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2756 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
2757 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
2758 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
2759 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
2760 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
2761 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
2762 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
2764 for (i = 0; i < texcoord_count; ++i)
2766 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
2769 return size;
2772 static void wined3d_format_get_colour(const struct wined3d_format *format,
2773 const void *data, struct wined3d_color *colour)
2775 float *output = &colour->r;
2776 const uint32_t *u32_data;
2777 const uint16_t *u16_data;
2778 const float *f32_data;
2779 unsigned int i;
2781 static const struct wined3d_color default_colour = {0.0f, 0.0f, 0.0f, 1.0f};
2782 static unsigned int warned;
2784 switch (format->id)
2786 case WINED3DFMT_B8G8R8A8_UNORM:
2787 u32_data = data;
2788 wined3d_color_from_d3dcolor(colour, *u32_data);
2789 break;
2791 case WINED3DFMT_R8G8B8A8_UNORM:
2792 u32_data = data;
2793 colour->r = (*u32_data & 0xffu) / 255.0f;
2794 colour->g = ((*u32_data >> 8) & 0xffu) / 255.0f;
2795 colour->b = ((*u32_data >> 16) & 0xffu) / 255.0f;
2796 colour->a = ((*u32_data >> 24) & 0xffu) / 255.0f;
2797 break;
2799 case WINED3DFMT_R16G16_UNORM:
2800 case WINED3DFMT_R16G16B16A16_UNORM:
2801 u16_data = data;
2802 *colour = default_colour;
2803 for (i = 0; i < format->component_count; ++i)
2804 output[i] = u16_data[i] / 65535.0f;
2805 break;
2807 case WINED3DFMT_R32_FLOAT:
2808 case WINED3DFMT_R32G32_FLOAT:
2809 case WINED3DFMT_R32G32B32_FLOAT:
2810 case WINED3DFMT_R32G32B32A32_FLOAT:
2811 f32_data = data;
2812 *colour = default_colour;
2813 for (i = 0; i < format->component_count; ++i)
2814 output[i] = f32_data[i];
2815 break;
2817 default:
2818 *colour = default_colour;
2819 if (!warned++)
2820 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format->id));
2821 break;
2825 static void wined3d_colour_from_mcs(struct wined3d_color *colour, enum wined3d_material_color_source mcs,
2826 const struct wined3d_color *material_colour, unsigned int index,
2827 const struct wined3d_stream_info *stream_info)
2829 const struct wined3d_stream_info_element *element = NULL;
2831 switch (mcs)
2833 case WINED3D_MCS_MATERIAL:
2834 *colour = *material_colour;
2835 return;
2837 case WINED3D_MCS_COLOR1:
2838 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
2840 colour->r = colour->g = colour->b = colour->a = 1.0f;
2841 return;
2843 element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2844 break;
2846 case WINED3D_MCS_COLOR2:
2847 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
2849 colour->r = colour->g = colour->b = colour->a = 0.0f;
2850 return;
2852 element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2853 break;
2855 default:
2856 colour->r = colour->g = colour->b = colour->a = 0.0f;
2857 ERR("Invalid material colour source %#x.\n", mcs);
2858 return;
2861 wined3d_format_get_colour(element->format, &element->data.addr[index * element->stride], colour);
2864 static float wined3d_clamp(float value, float min_value, float max_value)
2866 return value < min_value ? min_value : value > max_value ? max_value : value;
2869 static float wined3d_vec3_dot(const struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2871 return v0->x * v1->x + v0->y * v1->y + v0->z * v1->z;
2874 static void wined3d_vec3_subtract(struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2876 v0->x -= v1->x;
2877 v0->y -= v1->y;
2878 v0->z -= v1->z;
2881 static void wined3d_vec3_scale(struct wined3d_vec3 *v, float s)
2883 v->x *= s;
2884 v->y *= s;
2885 v->z *= s;
2888 static void wined3d_vec3_normalise(struct wined3d_vec3 *v)
2890 float rnorm = 1.0f / sqrtf(wined3d_vec3_dot(v, v));
2892 if (isfinite(rnorm))
2893 wined3d_vec3_scale(v, rnorm);
2896 static void wined3d_vec3_transform(struct wined3d_vec3 *dst,
2897 const struct wined3d_vec3 *v, const struct wined3d_matrix_3x3 *m)
2899 struct wined3d_vec3 tmp;
2901 tmp.x = v->x * m->_11 + v->y * m->_21 + v->z * m->_31;
2902 tmp.y = v->x * m->_12 + v->y * m->_22 + v->z * m->_32;
2903 tmp.z = v->x * m->_13 + v->y * m->_23 + v->z * m->_33;
2905 *dst = tmp;
2908 static void wined3d_color_clamp(struct wined3d_color *dst, const struct wined3d_color *src,
2909 float min_value, float max_value)
2911 dst->r = wined3d_clamp(src->r, min_value, max_value);
2912 dst->g = wined3d_clamp(src->g, min_value, max_value);
2913 dst->b = wined3d_clamp(src->b, min_value, max_value);
2914 dst->a = wined3d_clamp(src->a, min_value, max_value);
2917 static void wined3d_color_rgb_mul_add(struct wined3d_color *dst, const struct wined3d_color *src, float c)
2919 dst->r += src->r * c;
2920 dst->g += src->g * c;
2921 dst->b += src->b * c;
2924 static void init_transformed_lights(struct lights_settings *ls,
2925 const struct wined3d_stateblock_state *state, BOOL legacy_lighting, BOOL compute_lighting)
2927 const struct wined3d_light_info *lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
2928 const struct wined3d_light_info *light_info;
2929 struct wined3d_light_info *light_iter;
2930 struct light_transformed *light;
2931 struct wined3d_vec4 vec4;
2932 unsigned int light_count;
2933 unsigned int i, index;
2935 memset(ls, 0, sizeof(*ls));
2937 ls->lighting = !!compute_lighting;
2938 ls->fog_mode = state->rs[WINED3D_RS_FOGVERTEXMODE];
2939 ls->fog_coord_mode = state->rs[WINED3D_RS_RANGEFOGENABLE]
2940 ? WINED3D_FFP_VS_FOG_RANGE : WINED3D_FFP_VS_FOG_DEPTH;
2941 ls->fog_start = int_to_float(state->rs[WINED3D_RS_FOGSTART]);
2942 ls->fog_end = int_to_float(state->rs[WINED3D_RS_FOGEND]);
2943 ls->fog_density = int_to_float(state->rs[WINED3D_RS_FOGDENSITY]);
2945 if (ls->fog_mode == WINED3D_FOG_NONE && !compute_lighting)
2946 return;
2948 multiply_matrix(&ls->modelview_matrix, &state->transforms[WINED3D_TS_VIEW],
2949 &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
2951 if (!compute_lighting)
2952 return;
2954 compute_normal_matrix(&ls->normal_matrix._11, legacy_lighting, &ls->modelview_matrix);
2956 wined3d_color_from_d3dcolor(&ls->ambient_light, state->rs[WINED3D_RS_AMBIENT]);
2957 ls->legacy_lighting = !!legacy_lighting;
2958 ls->normalise = !!state->rs[WINED3D_RS_NORMALIZENORMALS];
2959 ls->localviewer = !!state->rs[WINED3D_RS_LOCALVIEWER];
2961 index = 0;
2962 RB_FOR_EACH_ENTRY(light_iter, &state->light_state->lights_tree, struct wined3d_light_info, entry)
2964 if (!light_iter->enabled)
2965 continue;
2967 switch (light_iter->OriginalParms.type)
2969 case WINED3D_LIGHT_DIRECTIONAL:
2970 ++ls->directional_light_count;
2971 break;
2973 case WINED3D_LIGHT_POINT:
2974 ++ls->point_light_count;
2975 break;
2977 case WINED3D_LIGHT_SPOT:
2978 ++ls->spot_light_count;
2979 break;
2981 case WINED3D_LIGHT_PARALLELPOINT:
2982 ++ls->parallel_point_light_count;
2983 break;
2985 default:
2986 FIXME("Unhandled light type %#x.\n", light_iter->OriginalParms.type);
2987 continue;
2989 lights[index++] = light_iter;
2990 if (index == WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS)
2991 break;
2994 light_count = index;
2995 for (i = 0, index = 0; i < light_count; ++i)
2997 light_info = lights[i];
2998 if (light_info->OriginalParms.type != WINED3D_LIGHT_DIRECTIONAL)
2999 continue;
3001 light = &ls->lights[index];
3002 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
3003 light->direction = *(struct wined3d_vec3 *)&vec4;
3004 wined3d_vec3_normalise(&light->direction);
3006 light->diffuse = light_info->OriginalParms.diffuse;
3007 light->ambient = light_info->OriginalParms.ambient;
3008 light->specular = light_info->OriginalParms.specular;
3009 ++index;
3012 for (i = 0; i < light_count; ++i)
3014 light_info = lights[i];
3015 if (light_info->OriginalParms.type != WINED3D_LIGHT_POINT)
3016 continue;
3018 light = &ls->lights[index];
3020 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3021 light->range = light_info->OriginalParms.range;
3022 light->c_att = light_info->OriginalParms.attenuation0;
3023 light->l_att = light_info->OriginalParms.attenuation1;
3024 light->q_att = light_info->OriginalParms.attenuation2;
3026 light->diffuse = light_info->OriginalParms.diffuse;
3027 light->ambient = light_info->OriginalParms.ambient;
3028 light->specular = light_info->OriginalParms.specular;
3029 ++index;
3032 for (i = 0; i < light_count; ++i)
3034 light_info = lights[i];
3035 if (light_info->OriginalParms.type != WINED3D_LIGHT_SPOT)
3036 continue;
3038 light = &ls->lights[index];
3040 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3041 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
3042 light->direction = *(struct wined3d_vec3 *)&vec4;
3043 wined3d_vec3_normalise(&light->direction);
3044 light->range = light_info->OriginalParms.range;
3045 light->falloff = light_info->OriginalParms.falloff;
3046 light->c_att = light_info->OriginalParms.attenuation0;
3047 light->l_att = light_info->OriginalParms.attenuation1;
3048 light->q_att = light_info->OriginalParms.attenuation2;
3049 light->cos_htheta = cosf(light_info->OriginalParms.theta / 2.0f);
3050 light->cos_hphi = cosf(light_info->OriginalParms.phi / 2.0f);
3052 light->diffuse = light_info->OriginalParms.diffuse;
3053 light->ambient = light_info->OriginalParms.ambient;
3054 light->specular = light_info->OriginalParms.specular;
3055 ++index;
3058 for (i = 0; i < light_count; ++i)
3060 light_info = lights[i];
3061 if (light_info->OriginalParms.type != WINED3D_LIGHT_PARALLELPOINT)
3062 continue;
3064 light = &ls->lights[index];
3066 wined3d_vec4_transform(&vec4, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3067 *(struct wined3d_vec3 *)&light->position = *(struct wined3d_vec3 *)&vec4;
3068 wined3d_vec3_normalise((struct wined3d_vec3 *)&light->position);
3069 light->diffuse = light_info->OriginalParms.diffuse;
3070 light->ambient = light_info->OriginalParms.ambient;
3071 light->specular = light_info->OriginalParms.specular;
3072 ++index;
3076 static void update_light_diffuse_specular(struct wined3d_color *diffuse, struct wined3d_color *specular,
3077 const struct wined3d_vec3 *dir, float att, float material_shininess,
3078 const struct wined3d_vec3 *normal_transformed,
3079 const struct wined3d_vec3 *position_transformed_normalised,
3080 const struct light_transformed *light, const struct lights_settings *ls)
3082 struct wined3d_vec3 vec3;
3083 float t, c;
3085 c = wined3d_clamp(wined3d_vec3_dot(dir, normal_transformed), 0.0f, 1.0f);
3086 wined3d_color_rgb_mul_add(diffuse, &light->diffuse, c * att);
3088 vec3 = *dir;
3089 if (ls->localviewer)
3090 wined3d_vec3_subtract(&vec3, position_transformed_normalised);
3091 else
3092 vec3.z -= 1.0f;
3093 wined3d_vec3_normalise(&vec3);
3094 t = wined3d_vec3_dot(normal_transformed, &vec3);
3095 if (t > 0.0f && (!ls->legacy_lighting || material_shininess > 0.0f)
3096 && wined3d_vec3_dot(dir, normal_transformed) > 0.0f)
3097 wined3d_color_rgb_mul_add(specular, &light->specular, att * powf(t, material_shininess));
3100 static void light_set_vertex_data(struct lights_settings *ls,
3101 const struct wined3d_vec4 *position)
3103 if (ls->fog_mode == WINED3D_FOG_NONE && !ls->lighting)
3104 return;
3106 wined3d_vec4_transform(&ls->position_transformed, position, &ls->modelview_matrix);
3107 wined3d_vec3_scale((struct wined3d_vec3 *)&ls->position_transformed, 1.0f / ls->position_transformed.w);
3110 static void compute_light(struct wined3d_color *ambient, struct wined3d_color *diffuse,
3111 struct wined3d_color *specular, struct lights_settings *ls, const struct wined3d_vec3 *normal,
3112 float material_shininess)
3114 struct wined3d_vec3 position_transformed_normalised;
3115 struct wined3d_vec3 normal_transformed = {0.0f};
3116 const struct light_transformed *light;
3117 struct wined3d_vec3 dir, dst;
3118 unsigned int i, index;
3119 float att;
3121 position_transformed_normalised = *(const struct wined3d_vec3 *)&ls->position_transformed;
3122 wined3d_vec3_normalise(&position_transformed_normalised);
3124 if (normal)
3126 wined3d_vec3_transform(&normal_transformed, normal, &ls->normal_matrix);
3127 if (ls->normalise)
3128 wined3d_vec3_normalise(&normal_transformed);
3131 diffuse->r = diffuse->g = diffuse->b = diffuse->a = 0.0f;
3132 *specular = *diffuse;
3133 *ambient = ls->ambient_light;
3135 index = 0;
3136 for (i = 0; i < ls->directional_light_count; ++i, ++index)
3138 light = &ls->lights[index];
3140 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3141 if (normal)
3142 update_light_diffuse_specular(diffuse, specular, &light->direction, 1.0f, material_shininess,
3143 &normal_transformed, &position_transformed_normalised, light, ls);
3146 for (i = 0; i < ls->point_light_count; ++i, ++index)
3148 light = &ls->lights[index];
3149 dir.x = light->position.x - ls->position_transformed.x;
3150 dir.y = light->position.y - ls->position_transformed.y;
3151 dir.z = light->position.z - ls->position_transformed.z;
3153 dst.z = wined3d_vec3_dot(&dir, &dir);
3154 dst.y = sqrtf(dst.z);
3155 dst.x = 1.0f;
3156 if (ls->legacy_lighting)
3158 dst.y = (light->range - dst.y) / light->range;
3159 if (!(dst.y > 0.0f))
3160 continue;
3161 dst.z = dst.y * dst.y;
3163 else
3165 if (!(dst.y <= light->range))
3166 continue;
3168 att = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3169 if (!ls->legacy_lighting)
3170 att = 1.0f / att;
3172 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3173 if (normal)
3175 wined3d_vec3_normalise(&dir);
3176 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3177 &normal_transformed, &position_transformed_normalised, light, ls);
3181 for (i = 0; i < ls->spot_light_count; ++i, ++index)
3183 float t;
3185 light = &ls->lights[index];
3187 dir.x = light->position.x - ls->position_transformed.x;
3188 dir.y = light->position.y - ls->position_transformed.y;
3189 dir.z = light->position.z - ls->position_transformed.z;
3191 dst.z = wined3d_vec3_dot(&dir, &dir);
3192 dst.y = sqrtf(dst.z);
3193 dst.x = 1.0f;
3195 if (ls->legacy_lighting)
3197 dst.y = (light->range - dst.y) / light->range;
3198 if (!(dst.y > 0.0f))
3199 continue;
3200 dst.z = dst.y * dst.y;
3202 else
3204 if (!(dst.y <= light->range))
3205 continue;
3207 wined3d_vec3_normalise(&dir);
3208 t = -wined3d_vec3_dot(&dir, &light->direction);
3209 if (t > light->cos_htheta)
3210 att = 1.0f;
3211 else if (t <= light->cos_hphi)
3212 att = 0.0f;
3213 else
3214 att = powf((t - light->cos_hphi) / (light->cos_htheta - light->cos_hphi), light->falloff);
3216 t = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3217 if (ls->legacy_lighting)
3218 att *= t;
3219 else
3220 att /= t;
3222 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3224 if (normal)
3225 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3226 &normal_transformed, &position_transformed_normalised, light, ls);
3229 for (i = 0; i < ls->parallel_point_light_count; ++i, ++index)
3231 light = &ls->lights[index];
3233 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3234 if (normal)
3235 update_light_diffuse_specular(diffuse, specular, (const struct wined3d_vec3 *)&light->position,
3236 1.0f, material_shininess, &normal_transformed, &position_transformed_normalised, light, ls);
3240 static float wined3d_calculate_fog_factor(float fog_coord, const struct lights_settings *ls)
3242 switch (ls->fog_mode)
3244 case WINED3D_FOG_NONE:
3245 return fog_coord;
3246 case WINED3D_FOG_LINEAR:
3247 return (ls->fog_end - fog_coord) / (ls->fog_end - ls->fog_start);
3248 case WINED3D_FOG_EXP:
3249 return expf(-fog_coord * ls->fog_density);
3250 case WINED3D_FOG_EXP2:
3251 return expf(-fog_coord * fog_coord * ls->fog_density * ls->fog_density);
3252 default:
3253 ERR("Unhandled fog mode %#x.\n", ls->fog_mode);
3254 return 0.0f;
3258 static void update_fog_factor(float *fog_factor, struct lights_settings *ls)
3260 float fog_coord;
3262 if (ls->fog_mode == WINED3D_FOG_NONE)
3263 return;
3265 switch (ls->fog_coord_mode)
3267 case WINED3D_FFP_VS_FOG_RANGE:
3268 fog_coord = sqrtf(wined3d_vec3_dot((const struct wined3d_vec3 *)&ls->position_transformed,
3269 (const struct wined3d_vec3 *)&ls->position_transformed));
3270 break;
3272 case WINED3D_FFP_VS_FOG_DEPTH:
3273 fog_coord = fabsf(ls->position_transformed.z);
3274 break;
3276 default:
3277 ERR("Unhandled fog coordinate mode %#x.\n", ls->fog_coord_mode);
3278 return;
3280 *fog_factor = wined3d_calculate_fog_factor(fog_coord, ls);
3283 /* Context activation is done by the caller. */
3284 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3285 static HRESULT process_vertices_strided(const struct wined3d_device *device,
3286 const struct wined3d_stateblock_state *state, DWORD dwDestIndex, DWORD dwCount,
3287 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, uint32_t flags, uint32_t dst_fvf)
3289 enum wined3d_material_color_source diffuse_source, specular_source, ambient_source, emissive_source;
3290 const struct wined3d_state *device_state = device->cs->c.state;
3291 const struct wined3d_matrix *proj_mat, *view_mat, *world_mat;
3292 const struct wined3d_color *material_specular_state_colour;
3293 const struct wined3d_format *output_colour_format;
3294 static const struct wined3d_color black;
3295 struct wined3d_map_desc map_desc;
3296 struct wined3d_box box = {0};
3297 struct wined3d_viewport vp;
3298 unsigned int texture_count;
3299 struct lights_settings ls;
3300 struct wined3d_matrix mat;
3301 unsigned int vertex_size;
3302 BOOL do_clip, lighting;
3303 float min_z, max_z;
3304 unsigned int i;
3305 BYTE *dest_ptr;
3306 HRESULT hr;
3308 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3310 ERR("Source has no position mask.\n");
3311 return WINED3DERR_INVALIDCALL;
3314 if (state->rs[WINED3D_RS_CLIPPING])
3316 static BOOL warned = FALSE;
3318 * The clipping code is not quite correct. Some things need
3319 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3320 * so disable clipping for now.
3321 * (The graphics in Half-Life are broken, and my processvertices
3322 * test crashes with IDirect3DDevice3)
3323 do_clip = TRUE;
3325 do_clip = FALSE;
3326 if (!warned)
3328 warned = TRUE;
3329 FIXME("Clipping is broken and disabled for now\n");
3332 else
3333 do_clip = FALSE;
3335 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3336 box.left = dwDestIndex * vertex_size;
3337 box.right = box.left + dwCount * vertex_size;
3338 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3340 WARN("Failed to map buffer, hr %#lx.\n", hr);
3341 return hr;
3343 dest_ptr = map_desc.data;
3345 view_mat = &state->transforms[WINED3D_TS_VIEW];
3346 proj_mat = &state->transforms[WINED3D_TS_PROJECTION];
3347 world_mat = &state->transforms[WINED3D_TS_WORLD];
3349 TRACE("View mat:\n");
3350 TRACE("%.8e %.8e %.8e %.8e\n", view_mat->_11, view_mat->_12, view_mat->_13, view_mat->_14);
3351 TRACE("%.8e %.8e %.8e %.8e\n", view_mat->_21, view_mat->_22, view_mat->_23, view_mat->_24);
3352 TRACE("%.8e %.8e %.8e %.8e\n", view_mat->_31, view_mat->_32, view_mat->_33, view_mat->_34);
3353 TRACE("%.8e %.8e %.8e %.8e\n", view_mat->_41, view_mat->_42, view_mat->_43, view_mat->_44);
3355 TRACE("Proj mat:\n");
3356 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat->_11, proj_mat->_12, proj_mat->_13, proj_mat->_14);
3357 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat->_21, proj_mat->_22, proj_mat->_23, proj_mat->_24);
3358 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat->_31, proj_mat->_32, proj_mat->_33, proj_mat->_34);
3359 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat->_41, proj_mat->_42, proj_mat->_43, proj_mat->_44);
3361 TRACE("World mat:\n");
3362 TRACE("%.8e %.8e %.8e %.8e\n", world_mat->_11, world_mat->_12, world_mat->_13, world_mat->_14);
3363 TRACE("%.8e %.8e %.8e %.8e\n", world_mat->_21, world_mat->_22, world_mat->_23, world_mat->_24);
3364 TRACE("%.8e %.8e %.8e %.8e\n", world_mat->_31, world_mat->_32, world_mat->_33, world_mat->_34);
3365 TRACE("%.8e %.8e %.8e %.8e\n", world_mat->_41, world_mat->_42, world_mat->_43, world_mat->_44);
3367 /* Get the viewport */
3368 wined3d_device_context_get_viewports(&device->cs->c, NULL, &vp);
3369 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3370 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3372 multiply_matrix(&mat, view_mat, world_mat);
3373 multiply_matrix(&mat, proj_mat, &mat);
3375 texture_count = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3377 lighting = state->rs[WINED3D_RS_LIGHTING]
3378 && (dst_fvf & (WINED3DFVF_DIFFUSE | WINED3DFVF_SPECULAR));
3379 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
3380 &ambient_source, &specular_source, device_state);
3381 output_colour_format = wined3d_get_format(device->adapter, WINED3DFMT_B8G8R8A8_UNORM, 0);
3382 material_specular_state_colour = state->rs[WINED3D_RS_SPECULARENABLE]
3383 ? &state->material.specular : &black;
3384 init_transformed_lights(&ls, state, device->adapter->d3d_info.wined3d_creation_flags
3385 & WINED3D_LEGACY_FFP_LIGHTING, lighting);
3387 wined3d_viewport_get_z_range(&vp, &min_z, &max_z);
3389 for (i = 0; i < dwCount; ++i)
3391 const struct wined3d_stream_info_element *position_element = &stream_info->elements[WINED3D_FFP_POSITION];
3392 const float *p = (const float *)&position_element->data.addr[i * position_element->stride];
3393 struct wined3d_color ambient, diffuse, specular;
3394 struct wined3d_vec4 position;
3395 unsigned int tex_index;
3397 position.x = p[0];
3398 position.y = p[1];
3399 position.z = p[2];
3400 position.w = 1.0f;
3402 light_set_vertex_data(&ls, &position);
3404 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3405 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3406 /* The position first */
3407 float x, y, z, rhw;
3408 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3410 /* Multiplication with world, view and projection matrix. */
3411 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3412 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3413 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3414 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3416 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3418 /* WARNING: The following things are taken from d3d7 and were not yet checked
3419 * against d3d8 or d3d9!
3422 /* Clipping conditions: From msdn
3424 * A vertex is clipped if it does not match the following requirements
3425 * -rhw < x <= rhw
3426 * -rhw < y <= rhw
3427 * 0 < z <= rhw
3428 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3430 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3431 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3435 if (!do_clip || (-rhw - eps < x && -rhw - eps < y && -eps < z && x <= rhw + eps
3436 && y <= rhw + eps && z <= rhw + eps && rhw > eps))
3438 /* "Normal" viewport transformation (not clipped)
3439 * 1) The values are divided by rhw
3440 * 2) The y axis is negative, so multiply it with -1
3441 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3442 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3443 * 4) Multiply x with Width/2 and add Width/2
3444 * 5) The same for the height
3445 * 6) Add the viewpoint X and Y to the 2D coordinates and
3446 * The minimum Z value to z
3447 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3449 * Well, basically it's simply a linear transformation into viewport
3450 * coordinates
3453 x /= rhw;
3454 y /= rhw;
3455 z /= rhw;
3457 y *= -1;
3459 x *= vp.width / 2;
3460 y *= vp.height / 2;
3461 z *= max_z - min_z;
3463 x += vp.width / 2 + vp.x;
3464 y += vp.height / 2 + vp.y;
3465 z += min_z;
3467 rhw = 1 / rhw;
3468 } else {
3469 /* That vertex got clipped
3470 * Contrary to OpenGL it is not dropped completely, it just
3471 * undergoes a different calculation.
3473 TRACE("Vertex got clipped\n");
3474 x += rhw;
3475 y += rhw;
3477 x /= 2;
3478 y /= 2;
3480 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3481 * outside of the main vertex buffer memory. That needs some more
3482 * investigation...
3486 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3489 ( (float *) dest_ptr)[0] = x;
3490 ( (float *) dest_ptr)[1] = y;
3491 ( (float *) dest_ptr)[2] = z;
3492 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3494 dest_ptr += 3 * sizeof(float);
3496 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3497 dest_ptr += sizeof(float);
3500 if (dst_fvf & WINED3DFVF_PSIZE)
3501 dest_ptr += sizeof(DWORD);
3503 if (dst_fvf & WINED3DFVF_NORMAL)
3505 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3506 const float *normal = (const float *)(element->data.addr + i * element->stride);
3507 /* AFAIK this should go into the lighting information */
3508 FIXME("Didn't expect the destination to have a normal\n");
3509 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3512 if (lighting)
3514 const struct wined3d_stream_info_element *element;
3515 struct wined3d_vec3 *normal;
3517 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3519 element = &stream_info->elements[WINED3D_FFP_NORMAL];
3520 normal = (struct wined3d_vec3 *)&element->data.addr[i * element->stride];
3522 else
3524 normal = NULL;
3526 compute_light(&ambient, &diffuse, &specular, &ls, normal,
3527 state->rs[WINED3D_RS_SPECULARENABLE] ? state->material.power : 0.0f);
3530 if (dst_fvf & WINED3DFVF_DIFFUSE)
3532 struct wined3d_color material_diffuse, material_ambient, material_emissive, diffuse_colour;
3534 wined3d_colour_from_mcs(&material_diffuse, diffuse_source,
3535 &state->material.diffuse, i, stream_info);
3537 if (lighting)
3539 wined3d_colour_from_mcs(&material_ambient, ambient_source,
3540 &state->material.ambient, i, stream_info);
3541 wined3d_colour_from_mcs(&material_emissive, emissive_source,
3542 &state->material.emissive, i, stream_info);
3544 diffuse_colour.r = ambient.r * material_ambient.r
3545 + diffuse.r * material_diffuse.r + material_emissive.r;
3546 diffuse_colour.g = ambient.g * material_ambient.g
3547 + diffuse.g * material_diffuse.g + material_emissive.g;
3548 diffuse_colour.b = ambient.b * material_ambient.b
3549 + diffuse.b * material_diffuse.b + material_emissive.b;
3550 diffuse_colour.a = material_diffuse.a;
3552 else
3554 diffuse_colour = material_diffuse;
3556 wined3d_color_clamp(&diffuse_colour, &diffuse_colour, 0.0f, 1.0f);
3557 wined3d_format_convert_from_float(output_colour_format, &diffuse_colour, dest_ptr);
3558 dest_ptr += sizeof(DWORD);
3561 if (dst_fvf & WINED3DFVF_SPECULAR)
3563 struct wined3d_color material_specular, specular_colour;
3565 wined3d_colour_from_mcs(&material_specular, specular_source,
3566 material_specular_state_colour, i, stream_info);
3568 if (lighting)
3570 specular_colour.r = specular.r * material_specular.r;
3571 specular_colour.g = specular.g * material_specular.g;
3572 specular_colour.b = specular.b * material_specular.b;
3573 specular_colour.a = ls.legacy_lighting ? 0.0f : material_specular.a;
3575 else
3577 specular_colour = material_specular;
3579 update_fog_factor(&specular_colour.a, &ls);
3580 wined3d_color_clamp(&specular_colour, &specular_colour, 0.0f, 1.0f);
3581 wined3d_format_convert_from_float(output_colour_format, &specular_colour, dest_ptr);
3582 dest_ptr += sizeof(DWORD);
3585 for (tex_index = 0; tex_index < texture_count; ++tex_index)
3587 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3588 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3589 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3591 ERR("No source texture, but destination requests one\n");
3592 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3594 else
3596 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3601 wined3d_resource_unmap(&dest->resource, 0);
3603 return WINED3D_OK;
3605 #undef copy_and_next
3607 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device, struct wined3d_stateblock *stateblock,
3608 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3609 const struct wined3d_vertex_declaration *declaration, uint32_t flags, uint32_t dst_fvf)
3611 const struct wined3d_stateblock_state *state = wined3d_stateblock_get_state(stateblock);
3612 struct wined3d_state *device_state = device->cs->c.state;
3613 struct wined3d_stream_info stream_info;
3614 struct wined3d_resource *resource;
3615 struct wined3d_box box = {0};
3616 struct wined3d_shader *vs;
3617 unsigned int i, j;
3618 uint32_t map;
3619 HRESULT hr;
3621 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3622 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3623 device, src_start_idx, dst_idx, vertex_count,
3624 dst_buffer, declaration, flags, dst_fvf);
3626 if (declaration)
3627 FIXME("Output vertex declaration not implemented yet.\n");
3629 wined3d_device_apply_stateblock(device, stateblock);
3631 vs = device_state->shader[WINED3D_SHADER_TYPE_VERTEX];
3632 device_state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3633 wined3d_stream_info_from_declaration(&stream_info, device_state, &device->adapter->d3d_info);
3634 device_state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3636 /* We can't convert FROM a VBO, and vertex buffers used to source into
3637 * process_vertices() are unlikely to ever be used for drawing. Release
3638 * VBOs in those buffers and fix up the stream_info structure.
3640 * Also apply the start index. */
3641 map = stream_info.use_map;
3642 while (map)
3644 struct wined3d_stream_info_element *e;
3645 struct wined3d_map_desc map_desc;
3647 i = wined3d_bit_scan(&map);
3648 e = &stream_info.elements[i];
3649 resource = &state->streams[e->stream_idx].buffer->resource;
3650 box.left = src_start_idx * e->stride;
3651 box.right = box.left + vertex_count * e->stride;
3652 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3654 ERR("Failed to map resource.\n");
3655 map = stream_info.use_map;
3656 while (map)
3658 j = wined3d_bit_scan(&map);
3659 if (j >= i)
3660 break;
3662 e = &stream_info.elements[j];
3663 resource = &state->streams[e->stream_idx].buffer->resource;
3664 if (FAILED(wined3d_resource_unmap(resource, 0)))
3665 ERR("Failed to unmap resource.\n");
3667 return WINED3DERR_INVALIDCALL;
3669 e->data.buffer_object = 0;
3670 e->data.addr += (ULONG_PTR)map_desc.data;
3673 hr = process_vertices_strided(device, state, dst_idx, vertex_count,
3674 &stream_info, dst_buffer, flags, dst_fvf);
3676 map = stream_info.use_map;
3677 while (map)
3679 i = wined3d_bit_scan(&map);
3680 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3681 if (FAILED(wined3d_resource_unmap(resource, 0)))
3682 ERR("Failed to unmap resource.\n");
3685 return hr;
3688 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
3690 TRACE("device %p, caps %p.\n", device, caps);
3692 return wined3d_get_device_caps(device->adapter, device->create_parms.device_type, caps);
3695 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3696 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3698 struct wined3d_swapchain *swapchain;
3700 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3701 device, swapchain_idx, mode, rotation);
3703 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3704 return WINED3DERR_INVALIDCALL;
3706 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3709 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3711 /* At the moment we have no need for any functionality at the beginning
3712 * of a scene. */
3713 TRACE("device %p.\n", device);
3715 if (device->inScene)
3717 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3718 return WINED3DERR_INVALIDCALL;
3720 device->inScene = TRUE;
3721 return WINED3D_OK;
3724 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3726 TRACE("device %p.\n", device);
3728 if (!device->inScene)
3730 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3731 return WINED3DERR_INVALIDCALL;
3734 device->inScene = FALSE;
3735 return WINED3D_OK;
3738 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, unsigned int rect_count,
3739 const RECT *rects, uint32_t flags, const struct wined3d_color *color, float depth, unsigned int stencil)
3741 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
3743 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3744 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3746 if (!rect_count && rects)
3748 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3749 return WINED3D_OK;
3752 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3754 struct wined3d_rendertarget_view *ds = fb->depth_stencil;
3755 if (!ds)
3757 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3758 /* TODO: What about depth stencil buffers without stencil bits? */
3759 return WINED3DERR_INVALIDCALL;
3761 else if (flags & WINED3DCLEAR_TARGET)
3763 if (ds->width < fb->render_targets[0]->width
3764 || ds->height < fb->render_targets[0]->height)
3766 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3767 return WINED3D_OK;
3772 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3774 return WINED3D_OK;
3777 struct wined3d_query * CDECL wined3d_device_context_get_predication(struct wined3d_device_context *context, BOOL *value)
3779 struct wined3d_state *state = context->state;
3781 TRACE("context %p, value %p.\n", context, value);
3783 if (value)
3784 *value = state->predicate_value;
3785 return state->predicate;
3788 void CDECL wined3d_device_context_set_primitive_type(struct wined3d_device_context *context,
3789 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
3791 struct wined3d_state *state = context->state;
3793 TRACE("context %p, primitive_type %s, patch_vertex_count %u.\n",
3794 context, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
3796 wined3d_device_context_lock(context);
3797 state->primitive_type = primitive_type;
3798 state->patch_vertex_count = patch_vertex_count;
3799 wined3d_device_context_unlock(context);
3802 void CDECL wined3d_device_context_get_primitive_type(const struct wined3d_device_context *context,
3803 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
3805 const struct wined3d_state *state = context->state;
3807 TRACE("context %p, primitive_type %p, patch_vertex_count %p.\n",
3808 context, primitive_type, patch_vertex_count);
3810 *primitive_type = state->primitive_type;
3811 if (patch_vertex_count)
3812 *patch_vertex_count = state->patch_vertex_count;
3814 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
3817 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3818 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3820 unsigned int src_size, dst_size, src_skip_levels = 0;
3821 unsigned int src_level_count, dst_level_count;
3822 const struct wined3d_dirty_regions *regions;
3823 unsigned int layer_count, level_count, i, j;
3824 enum wined3d_resource_type type;
3825 BOOL entire_texture = TRUE;
3826 struct wined3d_box box;
3828 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3830 /* Verify that the source and destination textures are non-NULL. */
3831 if (!src_texture || !dst_texture)
3833 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3834 return WINED3DERR_INVALIDCALL;
3837 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
3838 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
3840 WARN("Source resource is GPU accessible or a scratch resource.\n");
3841 return WINED3DERR_INVALIDCALL;
3843 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
3845 WARN("Destination resource is CPU accessible.\n");
3846 return WINED3DERR_INVALIDCALL;
3849 /* Verify that the source and destination textures are the same type. */
3850 type = src_texture->resource.type;
3851 if (dst_texture->resource.type != type)
3853 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3854 return WINED3DERR_INVALIDCALL;
3857 layer_count = src_texture->layer_count;
3858 if (layer_count != dst_texture->layer_count)
3860 WARN("Source and destination have different layer counts.\n");
3861 return WINED3DERR_INVALIDCALL;
3864 if (src_texture->resource.format != dst_texture->resource.format)
3866 WARN("Source and destination formats do not match.\n");
3867 return WINED3DERR_INVALIDCALL;
3870 src_level_count = src_texture->level_count;
3871 dst_level_count = dst_texture->level_count;
3872 level_count = min(src_level_count, dst_level_count);
3874 src_size = max(src_texture->resource.width, src_texture->resource.height);
3875 src_size = max(src_size, src_texture->resource.depth);
3876 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3877 dst_size = max(dst_size, dst_texture->resource.depth);
3878 while (src_size > dst_size)
3880 src_size >>= 1;
3881 ++src_skip_levels;
3884 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
3885 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
3886 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
3888 WARN("Source and destination dimensions do not match.\n");
3889 return WINED3DERR_INVALIDCALL;
3892 if ((regions = src_texture->dirty_regions))
3894 for (i = 0; i < layer_count && entire_texture; ++i)
3896 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
3897 continue;
3899 entire_texture = FALSE;
3900 break;
3904 /* Update every surface level of the texture. */
3905 if (entire_texture)
3907 for (i = 0; i < level_count; ++i)
3909 wined3d_texture_get_level_box(dst_texture, i, &box);
3910 for (j = 0; j < layer_count; ++j)
3912 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
3913 &dst_texture->resource, j * dst_level_count + i, &box,
3914 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
3915 0, NULL, WINED3D_TEXF_POINT);
3919 else
3921 unsigned int src_level, box_count, k;
3922 const struct wined3d_box *boxes;
3923 struct wined3d_box b;
3925 for (i = 0; i < layer_count; ++i)
3927 boxes = regions[i].boxes;
3928 box_count = regions[i].box_count;
3929 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
3931 boxes = &b;
3932 box_count = 1;
3933 wined3d_texture_get_level_box(dst_texture, i, &b);
3936 for (j = 0; j < level_count; ++j)
3938 src_level = j + src_skip_levels;
3940 /* TODO: We could pass an array of boxes here to avoid
3941 * multiple context acquisitions for the same resource. */
3942 for (k = 0; k < box_count; ++k)
3944 box = boxes[k];
3945 if (src_level)
3947 box.left >>= src_level;
3948 box.top >>= src_level;
3949 box.right = min((box.right + (1u << src_level) - 1) >> src_level,
3950 wined3d_texture_get_level_width(src_texture, src_level));
3951 box.bottom = min((box.bottom + (1u << src_level) - 1) >> src_level,
3952 wined3d_texture_get_level_height(src_texture, src_level));
3953 box.front >>= src_level;
3954 box.back = min((box.back + (1u << src_level) - 1) >> src_level,
3955 wined3d_texture_get_level_depth(src_texture, src_level));
3958 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
3959 &dst_texture->resource, i * dst_level_count + j, &box,
3960 &src_texture->resource, i * src_level_count + src_level, &box,
3961 0, NULL, WINED3D_TEXF_POINT);
3967 wined3d_texture_clear_dirty_regions(src_texture);
3969 return WINED3D_OK;
3972 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, const struct wined3d_stateblock_state *state, DWORD *num_passes)
3974 const struct wined3d_state *device_state = device->cs->c.state;
3975 struct wined3d_texture *texture;
3976 unsigned i;
3978 TRACE("device %p, num_passes %p.\n", device, num_passes);
3980 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
3982 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3984 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3985 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3987 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3989 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3990 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3993 texture = state->textures[i];
3994 if (!texture || texture->resource.format_caps & WINED3D_FORMAT_CAP_FILTERING)
3995 continue;
3997 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3999 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
4000 return E_FAIL;
4002 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4004 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
4005 return E_FAIL;
4007 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4008 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4010 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
4011 return E_FAIL;
4015 if (state->rs[WINED3D_RS_ZENABLE] || state->rs[WINED3D_RS_ZWRITEENABLE] || state->rs[WINED3D_RS_STENCILENABLE])
4017 struct wined3d_rendertarget_view *rt = device_state->fb.render_targets[0];
4018 struct wined3d_rendertarget_view *ds = device_state->fb.depth_stencil;
4020 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
4022 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4023 return WINED3DERR_CONFLICTINGRENDERSTATE;
4027 /* return a sensible default */
4028 *num_passes = 1;
4030 TRACE("returning D3D_OK\n");
4031 return WINED3D_OK;
4034 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4036 static BOOL warned;
4038 TRACE("device %p, software %#x.\n", device, software);
4040 if (!warned)
4042 FIXME("device %p, software %#x stub!\n", device, software);
4043 warned = TRUE;
4046 device->softwareVertexProcessing = software;
4049 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4051 static BOOL warned;
4053 TRACE("device %p.\n", device);
4055 if (!warned)
4057 TRACE("device %p stub!\n", device);
4058 warned = TRUE;
4061 return device->softwareVertexProcessing;
4064 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4065 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4067 struct wined3d_swapchain *swapchain;
4069 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4070 device, swapchain_idx, raster_status);
4072 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4073 return WINED3DERR_INVALIDCALL;
4075 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4078 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4080 static BOOL warned;
4082 TRACE("device %p, segments %.8e.\n", device, segments);
4084 if (segments != 0.0f)
4086 if (!warned)
4088 FIXME("device %p, segments %.8e stub!\n", device, segments);
4089 warned = TRUE;
4093 return WINED3D_OK;
4096 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4098 static BOOL warned;
4100 TRACE("device %p.\n", device);
4102 if (!warned)
4104 FIXME("device %p stub!\n", device);
4105 warned = TRUE;
4108 return 0.0f;
4111 void CDECL wined3d_device_context_copy_uav_counter(struct wined3d_device_context *context,
4112 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4114 TRACE("context %p, dst_buffer %p, offset %u, uav %p.\n",
4115 context, dst_buffer, offset, uav);
4117 wined3d_device_context_lock(context);
4118 wined3d_device_context_emit_copy_uav_counter(context, dst_buffer, offset, uav);
4119 wined3d_device_context_unlock(context);
4122 static bool resources_format_compatible(const struct wined3d_resource *src_resource,
4123 const struct wined3d_resource *dst_resource)
4125 if (src_resource->format->id == dst_resource->format->id)
4126 return true;
4127 if (src_resource->format->typeless_id && src_resource->format->typeless_id == dst_resource->format->typeless_id)
4128 return true;
4129 if (src_resource->device->cs->c.state->feature_level < WINED3D_FEATURE_LEVEL_10_1)
4130 return false;
4131 if ((src_resource->format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
4132 && (dst_resource->format_attrs & WINED3D_FORMAT_ATTR_CAST_TO_BLOCK))
4133 return src_resource->format->block_byte_count == dst_resource->format->byte_count;
4134 if ((src_resource->format_attrs & WINED3D_FORMAT_ATTR_CAST_TO_BLOCK)
4135 && (dst_resource->format_attrs & WINED3D_FORMAT_ATTR_BLOCKS))
4136 return src_resource->format->byte_count == dst_resource->format->block_byte_count;
4137 return false;
4140 void CDECL wined3d_device_context_copy_resource(struct wined3d_device_context *context,
4141 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4143 unsigned int src_row_block_count, dst_row_block_count;
4144 struct wined3d_texture *dst_texture, *src_texture;
4145 unsigned int src_row_count, dst_row_count;
4146 struct wined3d_box src_box, dst_box;
4147 unsigned int i, j;
4149 TRACE("context %p, dst_resource %p, src_resource %p.\n", context, dst_resource, src_resource);
4151 if (src_resource == dst_resource)
4153 WARN("Source and destination are the same resource.\n");
4154 return;
4157 if (src_resource->type != dst_resource->type)
4159 WARN("Resource types (%s / %s) don't match.\n",
4160 debug_d3dresourcetype(dst_resource->type),
4161 debug_d3dresourcetype(src_resource->type));
4162 return;
4165 if (!resources_format_compatible(src_resource, dst_resource))
4167 WARN("Resource formats %s and %s are incompatible.\n",
4168 debug_d3dformat(dst_resource->format->id),
4169 debug_d3dformat(src_resource->format->id));
4170 return;
4173 src_row_block_count = (src_resource->width + (src_resource->format->block_width - 1))
4174 / src_resource->format->block_width;
4175 dst_row_block_count = (dst_resource->width + (dst_resource->format->block_width - 1))
4176 / dst_resource->format->block_width;
4177 src_row_count = (src_resource->height + (src_resource->format->block_height - 1))
4178 / src_resource->format->block_height;
4179 dst_row_count = (dst_resource->height + (dst_resource->format->block_height - 1))
4180 / dst_resource->format->block_height;
4182 if (src_row_block_count != dst_row_block_count || src_row_count != dst_row_count
4183 || src_resource->depth != dst_resource->depth)
4185 WARN("Resource block dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4186 dst_row_block_count, dst_row_count, dst_resource->depth,
4187 src_row_block_count, src_row_count, src_resource->depth);
4188 return;
4191 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4193 wined3d_box_set(&src_box, 0, 0, src_resource->size, 1, 0, 1);
4194 wined3d_device_context_lock(context);
4195 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, 0, &src_box,
4196 src_resource, 0, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4197 wined3d_device_context_unlock(context);
4198 return;
4201 dst_texture = texture_from_resource(dst_resource);
4202 src_texture = texture_from_resource(src_resource);
4204 if (src_texture->layer_count != dst_texture->layer_count
4205 || src_texture->level_count != dst_texture->level_count)
4207 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4208 dst_texture->layer_count, dst_texture->level_count,
4209 src_texture->layer_count, src_texture->level_count);
4210 return;
4213 wined3d_device_context_lock(context);
4214 for (i = 0; i < dst_texture->level_count; ++i)
4216 wined3d_texture_get_level_box(src_texture, i, &src_box);
4217 wined3d_texture_get_level_box(dst_texture, i, &dst_box);
4218 for (j = 0; j < dst_texture->layer_count; ++j)
4220 unsigned int idx = j * dst_texture->level_count + i;
4222 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, idx, &dst_box,
4223 src_resource, idx, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4226 wined3d_device_context_unlock(context);
4229 HRESULT CDECL wined3d_device_context_copy_sub_resource_region(struct wined3d_device_context *context,
4230 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4231 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4232 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
4234 struct wined3d_box dst_box, b;
4236 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4237 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4238 context, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4239 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
4241 if (flags)
4242 FIXME("Ignoring flags %#x.\n", flags);
4244 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4246 WARN("Source and destination are the same sub-resource.\n");
4247 return WINED3DERR_INVALIDCALL;
4250 if (!resources_format_compatible(src_resource, dst_resource))
4252 WARN("Resource formats %s and %s are incompatible.\n",
4253 debug_d3dformat(dst_resource->format->id),
4254 debug_d3dformat(src_resource->format->id));
4255 return WINED3DERR_INVALIDCALL;
4258 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4260 if (src_resource->type != WINED3D_RTYPE_BUFFER)
4262 WARN("Resource types (%s / %s) don't match.\n",
4263 debug_d3dresourcetype(dst_resource->type),
4264 debug_d3dresourcetype(src_resource->type));
4265 return WINED3DERR_INVALIDCALL;
4268 if (dst_sub_resource_idx)
4270 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4271 return WINED3DERR_INVALIDCALL;
4274 if (src_sub_resource_idx)
4276 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4277 return WINED3DERR_INVALIDCALL;
4280 if (!src_box)
4282 unsigned int dst_w;
4284 dst_w = dst_resource->size - dst_x;
4285 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4286 src_box = &b;
4288 else if ((src_box->left >= src_box->right
4289 || src_box->top >= src_box->bottom
4290 || src_box->front >= src_box->back))
4292 WARN("Invalid box %s specified.\n", debug_box(src_box));
4293 return WINED3DERR_INVALIDCALL;
4296 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4297 || src_box->right - src_box->left > dst_resource->size - dst_x)
4299 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4300 dst_x, src_box->left, src_box->right - src_box->left);
4301 return WINED3DERR_INVALIDCALL;
4304 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4306 else
4308 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4309 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4310 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4311 unsigned int src_row_block_count, src_row_count;
4313 if (!wined3d_texture_validate_sub_resource_idx(dst_texture, dst_sub_resource_idx))
4314 return WINED3DERR_INVALIDCALL;
4316 if (!wined3d_texture_validate_sub_resource_idx(src_texture, src_sub_resource_idx))
4317 return WINED3DERR_INVALIDCALL;
4319 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4321 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4322 return WINED3DERR_INVALIDCALL;
4325 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4327 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4328 return WINED3DERR_INVALIDCALL;
4331 if (!src_box)
4333 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
4335 src_w = wined3d_texture_get_level_width(src_texture, src_level);
4336 src_h = wined3d_texture_get_level_height(src_texture, src_level);
4337 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
4339 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4340 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
4341 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
4342 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
4344 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
4345 src_box = &b;
4347 else if (FAILED(wined3d_resource_check_box_dimensions(src_resource, src_sub_resource_idx, src_box)))
4349 WARN("Invalid source box %s.\n", debug_box(src_box));
4350 return WINED3DERR_INVALIDCALL;
4353 if (src_resource->format->block_width == dst_resource->format->block_width
4354 && src_resource->format->block_height == dst_resource->format->block_height)
4356 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4357 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
4359 else
4361 src_row_block_count = (src_box->right - src_box->left + src_resource->format->block_width - 1)
4362 / src_resource->format->block_width;
4363 src_row_count = (src_box->bottom - src_box->top + src_resource->format->block_height - 1)
4364 / src_resource->format->block_height;
4365 wined3d_box_set(&dst_box, dst_x, dst_y,
4366 dst_x + (src_row_block_count * dst_resource->format->block_width),
4367 dst_y + (src_row_count * dst_resource->format->block_height),
4368 dst_z, dst_z + (src_box->back - src_box->front));
4370 if (FAILED(wined3d_resource_check_box_dimensions(dst_resource, dst_sub_resource_idx, &dst_box)))
4372 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4373 return WINED3DERR_INVALIDCALL;
4377 wined3d_device_context_lock(context);
4378 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, dst_sub_resource_idx, &dst_box,
4379 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4380 wined3d_device_context_unlock(context);
4382 return WINED3D_OK;
4385 void CDECL wined3d_device_context_update_sub_resource(struct wined3d_device_context *context,
4386 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
4387 const void *data, unsigned int row_pitch, unsigned int depth_pitch, unsigned int flags)
4389 struct wined3d_sub_resource_desc desc;
4390 struct wined3d_box b;
4392 TRACE("context %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, flags %#x.\n",
4393 context, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
4395 if (flags)
4396 FIXME("Ignoring flags %#x.\n", flags);
4398 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
4400 WARN("Resource %p is not GPU accessible.\n", resource);
4401 return;
4404 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4405 return;
4407 if (!box)
4409 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4410 box = &b;
4412 else if (box->left >= box->right || box->right > desc.width
4413 || box->top >= box->bottom || box->bottom > desc.height
4414 || box->front >= box->back || box->back > desc.depth)
4416 WARN("Invalid box %s specified.\n", debug_box(box));
4417 return;
4420 wined3d_device_context_lock(context);
4421 wined3d_device_context_emit_update_sub_resource(context, resource,
4422 sub_resource_idx, box, data, row_pitch, depth_pitch);
4423 wined3d_device_context_unlock(context);
4426 void CDECL wined3d_device_context_resolve_sub_resource(struct wined3d_device_context *context,
4427 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
4428 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
4429 enum wined3d_format_id format_id)
4431 struct wined3d_texture *dst_texture, *src_texture;
4432 unsigned int dst_level, src_level;
4433 struct wined3d_blt_fx fx = {0};
4434 RECT dst_rect, src_rect;
4436 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, "
4437 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
4438 context, dst_resource, dst_sub_resource_idx,
4439 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
4441 if (wined3d_format_is_typeless(dst_resource->format)
4442 || wined3d_format_is_typeless(src_resource->format))
4444 FIXME("Multisample resolve is not fully supported for typeless formats "
4445 "(dst_format %s, src_format %s, format %s).\n",
4446 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
4447 debug_d3dformat(format_id));
4449 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4451 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
4452 return;
4454 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4456 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
4457 return;
4460 wined3d_device_context_lock(context);
4461 fx.resolve_format_id = format_id;
4463 dst_texture = texture_from_resource(dst_resource);
4464 src_texture = texture_from_resource(src_resource);
4466 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4467 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
4468 wined3d_texture_get_level_height(dst_texture, dst_level));
4469 src_level = src_sub_resource_idx % src_texture->level_count;
4470 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4471 wined3d_texture_get_level_height(src_texture, src_level));
4472 wined3d_device_context_blt(context, dst_texture, dst_sub_resource_idx, &dst_rect,
4473 src_texture, src_sub_resource_idx, &src_rect, 0, &fx, WINED3D_TEXF_POINT);
4474 wined3d_device_context_unlock(context);
4477 HRESULT CDECL wined3d_device_context_clear_rendertarget_view(struct wined3d_device_context *context,
4478 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
4479 const struct wined3d_color *color, float depth, unsigned int stencil)
4481 struct wined3d_resource *resource;
4482 RECT r;
4484 TRACE("context %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4485 context, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4487 if (!flags)
4488 return WINED3D_OK;
4490 resource = view->resource;
4491 if (resource->type == WINED3D_RTYPE_BUFFER)
4493 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4494 return WINED3DERR_INVALIDCALL;
4497 if (!rect)
4499 SetRect(&r, 0, 0, view->width, view->height);
4500 rect = &r;
4502 else
4504 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4505 HRESULT hr;
4507 if (FAILED(hr = wined3d_resource_check_box_dimensions(resource, view->sub_resource_idx, &b)))
4508 return hr;
4511 wined3d_device_context_lock(context);
4512 wined3d_device_context_emit_clear_rendertarget_view(context, view, rect, flags, color, depth, stencil);
4513 wined3d_device_context_unlock(context);
4515 return WINED3D_OK;
4518 void CDECL wined3d_device_context_clear_uav_float(struct wined3d_device_context *context,
4519 struct wined3d_unordered_access_view *view, const struct wined3d_vec4 *clear_value)
4521 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_vec4(clear_value));
4523 if (!(view->format->attrs & (WINED3D_FORMAT_ATTR_FLOAT | WINED3D_FORMAT_ATTR_NORMALISED)))
4525 WARN("Not supported for view format %s.\n", debug_d3dformat(view->format->id));
4526 return;
4529 wined3d_device_context_lock(context);
4530 wined3d_device_context_emit_clear_uav(context, view, (const struct wined3d_uvec4 *)clear_value, true);
4531 wined3d_device_context_unlock(context);
4534 void CDECL wined3d_device_context_clear_uav_uint(struct wined3d_device_context *context,
4535 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
4537 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_uvec4(clear_value));
4539 wined3d_device_context_lock(context);
4540 wined3d_device_context_emit_clear_uav(context, view, clear_value, false);
4541 wined3d_device_context_unlock(context);
4544 static unsigned int sanitise_map_flags(const struct wined3d_resource *resource, unsigned int flags)
4546 /* Not all flags make sense together, but Windows never returns an error.
4547 * Catch the cases that could cause issues. */
4548 if (flags & WINED3D_MAP_READ)
4550 if (flags & WINED3D_MAP_DISCARD)
4552 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
4553 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4555 if (flags & WINED3D_MAP_NOOVERWRITE)
4557 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
4558 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4561 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4563 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU) || !(resource->usage & WINED3DUSAGE_DYNAMIC))
4565 WARN("DISCARD or NOOVERWRITE map not on dynamic GPU-accessible buffer, ignoring.\n");
4566 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4568 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4569 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4571 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
4572 flags &= ~WINED3D_MAP_DISCARD;
4576 return flags;
4579 HRESULT CDECL wined3d_device_context_map(struct wined3d_device_context *context,
4580 struct wined3d_resource *resource, unsigned int sub_resource_idx,
4581 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
4583 struct wined3d_sub_resource_desc desc;
4584 struct wined3d_box b;
4585 HRESULT hr;
4587 TRACE("context %p, resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
4588 context, resource, sub_resource_idx, map_desc, debug_box(box), flags);
4590 if (!(flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
4592 WARN("No read/write flags specified.\n");
4593 return E_INVALIDARG;
4596 if ((flags & WINED3D_MAP_READ) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_R))
4598 WARN("Resource does not have MAP_R access.\n");
4599 return E_INVALIDARG;
4602 if ((flags & WINED3D_MAP_WRITE) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_W))
4604 WARN("Resource does not have MAP_W access.\n");
4605 return E_INVALIDARG;
4608 flags = sanitise_map_flags(resource, flags);
4610 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4611 return E_INVALIDARG;
4613 if (!box)
4615 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4616 box = &b;
4618 else if (FAILED(wined3d_resource_check_box_dimensions(resource, sub_resource_idx, box)))
4620 WARN("Map box is invalid.\n");
4622 if (resource->type != WINED3D_RTYPE_BUFFER && resource->type != WINED3D_RTYPE_TEXTURE_2D)
4623 return WINED3DERR_INVALIDCALL;
4625 if ((resource->format_attrs & WINED3D_FORMAT_ATTR_BLOCKS) &&
4626 !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
4627 return WINED3DERR_INVALIDCALL;
4630 wined3d_device_context_lock(context);
4631 hr = wined3d_device_context_emit_map(context, resource, sub_resource_idx, map_desc, box, flags);
4632 wined3d_device_context_unlock(context);
4633 return hr;
4636 HRESULT CDECL wined3d_device_context_unmap(struct wined3d_device_context *context,
4637 struct wined3d_resource *resource, unsigned int sub_resource_idx)
4639 HRESULT hr;
4640 TRACE("context %p, resource %p, sub_resource_idx %u.\n", context, resource, sub_resource_idx);
4642 wined3d_device_context_lock(context);
4643 hr = wined3d_device_context_emit_unmap(context, resource, sub_resource_idx);
4644 wined3d_device_context_unlock(context);
4645 return hr;
4648 void CDECL wined3d_device_context_issue_query(struct wined3d_device_context *context,
4649 struct wined3d_query *query, unsigned int flags)
4651 TRACE("context %p, query %p, flags %#x.\n", context, query, flags);
4653 wined3d_device_context_lock(context);
4654 context->ops->issue_query(context, query, flags);
4655 wined3d_device_context_unlock(context);
4658 void CDECL wined3d_device_context_execute_command_list(struct wined3d_device_context *context,
4659 struct wined3d_command_list *list, bool restore_state)
4661 TRACE("context %p, list %p, restore_state %d.\n", context, list, restore_state);
4663 wined3d_device_context_lock(context);
4664 wined3d_device_context_emit_execute_command_list(context, list, restore_state);
4665 wined3d_device_context_unlock(context);
4668 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_rendertarget_view(
4669 const struct wined3d_device_context *context, unsigned int view_idx)
4671 unsigned int max_rt_count;
4673 TRACE("context %p, view_idx %u.\n", context, view_idx);
4675 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
4676 if (view_idx >= max_rt_count)
4678 WARN("Only %u render targets are supported.\n", max_rt_count);
4679 return NULL;
4682 return context->state->fb.render_targets[view_idx];
4685 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_depth_stencil_view(
4686 const struct wined3d_device_context *context)
4688 TRACE("context %p.\n", context);
4690 return context->state->fb.depth_stencil;
4693 void CDECL wined3d_device_context_generate_mipmaps(struct wined3d_device_context *context,
4694 struct wined3d_shader_resource_view *view)
4696 struct wined3d_texture *texture;
4698 TRACE("context %p, view %p.\n", context, view);
4700 if (view->resource->type == WINED3D_RTYPE_BUFFER)
4702 WARN("Called on buffer resource %p.\n", view->resource);
4703 return;
4706 texture = texture_from_resource(view->resource);
4707 if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
4709 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
4710 return;
4713 wined3d_device_context_lock(context);
4714 wined3d_device_context_emit_generate_mipmaps(context, view);
4715 wined3d_device_context_unlock(context);
4718 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4719 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4721 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4722 struct wined3d_sub_resource_data data;
4723 struct wined3d_resource_desc desc;
4724 struct wined3d_map_desc map_desc;
4725 struct wined3d_texture *texture;
4726 HRESULT hr;
4728 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
4730 ERR("Failed to map source texture.\n");
4731 return NULL;
4734 data.data = map_desc.data;
4735 data.row_pitch = map_desc.row_pitch;
4736 data.slice_pitch = map_desc.slice_pitch;
4738 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4739 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4740 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4741 desc.multisample_quality = 0;
4742 desc.usage = WINED3DUSAGE_DYNAMIC;
4743 desc.bind_flags = 0;
4744 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4745 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4746 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4747 desc.depth = 1;
4748 desc.size = 0;
4750 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
4751 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4752 if (FAILED(hr))
4754 ERR("Failed to create cursor texture.\n");
4755 return NULL;
4758 return texture;
4761 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4762 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4764 unsigned int texture_level = sub_resource_idx % texture->level_count;
4765 unsigned int cursor_width, cursor_height;
4766 struct wined3d_map_desc map_desc;
4768 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4769 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4771 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx)
4772 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4773 return WINED3DERR_INVALIDCALL;
4775 if (device->cursor_texture)
4777 wined3d_texture_decref(device->cursor_texture);
4778 device->cursor_texture = NULL;
4781 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4783 WARN("Texture %p has invalid format %s.\n",
4784 texture, debug_d3dformat(texture->resource.format->id));
4785 return WINED3DERR_INVALIDCALL;
4788 /* Cursor width and height must all be powers of two */
4789 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4790 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4791 if ((cursor_width & (cursor_width - 1)) || (cursor_height & (cursor_height - 1)))
4793 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width, cursor_height);
4794 return WINED3DERR_INVALIDCALL;
4797 /* Do not store the surface's pointer because the application may
4798 * release it after setting the cursor image. Windows doesn't
4799 * addref the set surface, so we can't do this either without
4800 * creating circular refcount dependencies. */
4801 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4803 ERR("Failed to create cursor texture.\n");
4804 return WINED3DERR_INVALIDCALL;
4807 if (cursor_width == 32 && cursor_height == 32)
4809 UINT mask_size = cursor_width * cursor_height / 8;
4810 ICONINFO cursor_info;
4811 DWORD *mask_bits;
4812 HCURSOR cursor;
4814 /* 32-bit user32 cursors ignore the alpha channel if it's all
4815 * zeroes, and use the mask instead. Fill the mask with all ones
4816 * to ensure we still get a fully transparent cursor. */
4817 if (!(mask_bits = malloc(mask_size)))
4818 return E_OUTOFMEMORY;
4819 memset(mask_bits, 0xff, mask_size);
4821 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4822 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
4823 cursor_info.fIcon = FALSE;
4824 cursor_info.xHotspot = x_hotspot;
4825 cursor_info.yHotspot = y_hotspot;
4826 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4827 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4828 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4830 /* Create our cursor and clean up. */
4831 cursor = CreateIconIndirect(&cursor_info);
4832 if (cursor_info.hbmMask)
4833 DeleteObject(cursor_info.hbmMask);
4834 if (cursor_info.hbmColor)
4835 DeleteObject(cursor_info.hbmColor);
4836 if (device->hardwareCursor)
4837 DestroyCursor(device->hardwareCursor);
4838 device->hardwareCursor = cursor;
4839 if (device->bCursorVisible)
4840 SetCursor(cursor);
4842 free(mask_bits);
4845 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4846 device->cursorWidth = cursor_width;
4847 device->cursorHeight = cursor_height;
4848 device->xHotSpot = x_hotspot;
4849 device->yHotSpot = y_hotspot;
4851 return WINED3D_OK;
4854 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4855 int x_screen_space, int y_screen_space, uint32_t flags)
4857 TRACE("device %p, x %d, y %d, flags %#x.\n",
4858 device, x_screen_space, y_screen_space, flags);
4860 device->xScreenSpace = x_screen_space;
4861 device->yScreenSpace = y_screen_space;
4863 if (device->hardwareCursor)
4865 POINT pt;
4867 GetCursorPos( &pt );
4868 if (x_screen_space == pt.x && y_screen_space == pt.y)
4869 return;
4870 SetCursorPos( x_screen_space, y_screen_space );
4872 /* Switch to the software cursor if position diverges from the hardware one. */
4873 GetCursorPos( &pt );
4874 if (x_screen_space != pt.x || y_screen_space != pt.y)
4876 if (device->bCursorVisible) SetCursor( NULL );
4877 DestroyCursor( device->hardwareCursor );
4878 device->hardwareCursor = 0;
4883 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4885 BOOL oldVisible = device->bCursorVisible;
4887 TRACE("device %p, show %#x.\n", device, show);
4890 * When ShowCursor is first called it should make the cursor appear at the OS's last
4891 * known cursor position.
4893 if (show && !oldVisible)
4895 POINT pt;
4896 GetCursorPos(&pt);
4897 device->xScreenSpace = pt.x;
4898 device->yScreenSpace = pt.y;
4901 if (device->hardwareCursor)
4903 device->bCursorVisible = show;
4904 if (show)
4905 SetCursor(device->hardwareCursor);
4906 else
4907 SetCursor(NULL);
4909 else if (device->cursor_texture)
4911 device->bCursorVisible = show;
4914 return oldVisible;
4917 static void mark_managed_resource_dirty(struct wined3d_resource *resource)
4919 if (resource->type != WINED3D_RTYPE_BUFFER)
4921 struct wined3d_texture *texture = texture_from_resource(resource);
4922 unsigned int i;
4924 if (texture->dirty_regions)
4926 for (i = 0; i < texture->layer_count; ++i)
4927 wined3d_texture_add_dirty_region(texture, i, NULL);
4932 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4934 struct wined3d_resource *resource, *cursor;
4936 TRACE("device %p.\n", device);
4938 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4940 TRACE("Checking resource %p for eviction.\n", resource);
4942 if ((resource->usage & WINED3DUSAGE_MANAGED) && !resource->map_count)
4944 if (resource->access & WINED3D_RESOURCE_ACCESS_GPU)
4946 TRACE("Evicting %p.\n", resource);
4947 wined3d_cs_emit_unload_resource(device->cs, resource);
4950 mark_managed_resource_dirty(resource);
4955 void CDECL wined3d_device_context_flush(struct wined3d_device_context *context)
4957 TRACE("context %p.\n", context);
4959 wined3d_device_context_lock(context);
4960 context->ops->flush(context);
4961 wined3d_device_context_unlock(context);
4964 static void update_swapchain_flags(struct wined3d_texture *texture)
4966 unsigned int flags = texture->swapchain->state.desc.flags;
4968 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
4969 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
4970 else
4971 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
4973 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4974 texture->flags |= WINED3D_TEXTURE_GET_DC;
4975 else
4976 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
4979 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4980 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4981 wined3d_device_reset_cb callback, BOOL reset_state)
4983 static struct wined3d_rendertarget_view *const views[WINED3D_MAX_RENDER_TARGETS];
4984 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
4985 struct wined3d_device_context *context = &device->cs->c;
4986 struct wined3d_swapchain_state *swapchain_state;
4987 struct wined3d_state *state = context->state;
4988 struct wined3d_swapchain_desc *current_desc;
4989 struct wined3d_resource *resource, *cursor;
4990 struct wined3d_rendertarget_view *view;
4991 struct wined3d_swapchain *swapchain;
4992 struct wined3d_view_desc view_desc;
4993 BOOL backbuffer_resized, windowed;
4994 HRESULT hr = WINED3D_OK;
4995 HWND device_window;
4996 unsigned int i;
4998 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4999 device, swapchain_desc, mode, callback, reset_state);
5001 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
5003 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5005 ERR("Failed to get the first implicit swapchain.\n");
5006 return WINED3DERR_INVALIDCALL;
5008 swapchain_state = &swapchain->state;
5009 current_desc = &swapchain_state->desc;
5011 if (reset_state)
5013 if (device->logo_texture)
5015 wined3d_texture_decref(device->logo_texture);
5016 device->logo_texture = NULL;
5018 if (device->cursor_texture)
5020 wined3d_texture_decref(device->cursor_texture);
5021 device->cursor_texture = NULL;
5023 for (unsigned int i = 0; i < ARRAY_SIZE(device->push_constants); ++i)
5025 if (device->push_constants[i])
5026 wined3d_buffer_decref(device->push_constants[i]);
5027 device->push_constants[i] = NULL;
5029 state_unbind_resources(state);
5032 wined3d_device_context_set_rendertarget_views(context, 0, d3d_info->limits.max_rt_count, views, FALSE);
5033 wined3d_device_context_set_depth_stencil_view(context, NULL);
5035 if (reset_state)
5037 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5039 TRACE("Enumerating resource %p.\n", resource);
5040 if (FAILED(hr = callback(resource)))
5041 return hr;
5045 TRACE("New params:\n");
5046 TRACE("output %p\n", swapchain_desc->output);
5047 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5048 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5049 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5050 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5051 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5052 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5053 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5054 TRACE("device_window %p\n", swapchain_desc->device_window);
5055 TRACE("windowed %#x\n", swapchain_desc->windowed);
5056 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5057 if (swapchain_desc->enable_auto_depth_stencil)
5058 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5059 TRACE("flags %#x\n", swapchain_desc->flags);
5060 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5061 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5063 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
5064 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
5066 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
5067 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
5068 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
5069 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
5071 /* No special treatment of these parameters. Just store them */
5072 current_desc->swap_effect = swapchain_desc->swap_effect;
5073 current_desc->enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
5074 current_desc->auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
5075 current_desc->refresh_rate = swapchain_desc->refresh_rate;
5076 current_desc->auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
5078 device_window = swapchain_desc->device_window ? swapchain_desc->device_window : device->create_parms.focus_window;
5079 if (device_window && device_window != current_desc->device_window)
5081 TRACE("Changing the device window from %p to %p.\n",
5082 current_desc->device_window, device_window);
5083 current_desc->device_window = device_window;
5084 swapchain_state->device_window = device_window;
5085 wined3d_swapchain_set_window(swapchain, NULL);
5088 backbuffer_resized = swapchain_desc->backbuffer_width != current_desc->backbuffer_width
5089 || swapchain_desc->backbuffer_height != current_desc->backbuffer_height;
5090 windowed = current_desc->windowed;
5092 if (!swapchain_desc->windowed != !windowed || swapchain->reapply_mode
5093 || mode || (!swapchain_desc->windowed && backbuffer_resized))
5095 /* Switch from windowed to fullscreen. */
5096 if (windowed && !swapchain_desc->windowed)
5098 HWND focus_window = device->create_parms.focus_window;
5100 if (!focus_window)
5101 focus_window = swapchain->state.device_window;
5102 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5104 ERR("Failed to acquire focus window, hr %#lx.\n", hr);
5105 return hr;
5109 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(&swapchain->state,
5110 swapchain_desc, mode)))
5111 return hr;
5113 /* Switch from fullscreen to windowed. */
5114 if (!windowed && swapchain_desc->windowed)
5115 wined3d_device_release_focus_window(device);
5117 else if (!swapchain_desc->windowed)
5119 DWORD style = swapchain_state->style;
5120 DWORD exstyle = swapchain_state->exstyle;
5121 struct wined3d_output_desc output_desc;
5123 /* If we're in fullscreen, and the mode wasn't changed, we have to get
5124 * the window back into the right position. Some applications
5125 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
5126 * up their mess. Guild Wars also loses the device during that. */
5127 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
5129 ERR("Failed to get output description, hr %#lx.\n", hr);
5130 return hr;
5133 swapchain_state->style = 0;
5134 swapchain_state->exstyle = 0;
5135 wined3d_swapchain_state_setup_fullscreen(swapchain_state, swapchain_state->device_window,
5136 output_desc.desktop_rect.left, output_desc.desktop_rect.top,
5137 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height);
5138 swapchain_state->style = style;
5139 swapchain_state->exstyle = exstyle;
5142 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
5143 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
5144 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
5145 return hr;
5147 if (swapchain_desc->flags != current_desc->flags)
5149 current_desc->flags = swapchain_desc->flags;
5151 update_swapchain_flags(swapchain->front_buffer);
5152 for (i = 0; i < current_desc->backbuffer_count; ++i)
5154 update_swapchain_flags(swapchain->back_buffers[i]);
5158 if ((view = device->auto_depth_stencil_view))
5160 device->auto_depth_stencil_view = NULL;
5161 wined3d_rendertarget_view_decref(view);
5163 if (current_desc->enable_auto_depth_stencil)
5165 struct wined3d_resource_desc texture_desc;
5166 struct wined3d_texture *texture;
5168 TRACE("Creating the depth stencil buffer.\n");
5170 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5171 texture_desc.format = current_desc->auto_depth_stencil_format;
5172 texture_desc.multisample_type = current_desc->multisample_type;
5173 texture_desc.multisample_quality = current_desc->multisample_quality;
5174 texture_desc.usage = 0;
5175 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
5176 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5177 texture_desc.width = current_desc->backbuffer_width;
5178 texture_desc.height = current_desc->backbuffer_height;
5179 texture_desc.depth = 1;
5180 texture_desc.size = 0;
5182 if (FAILED(hr = wined3d_texture_create(device, &texture_desc, 1, 1, 0,
5183 NULL, NULL, &wined3d_null_parent_ops, &texture)))
5185 ERR("Failed to create the auto depth/stencil surface, hr %#lx.\n", hr);
5186 return WINED3DERR_INVALIDCALL;
5189 view_desc.format_id = texture->resource.format->id;
5190 view_desc.flags = 0;
5191 view_desc.u.texture.level_idx = 0;
5192 view_desc.u.texture.level_count = 1;
5193 view_desc.u.texture.layer_idx = 0;
5194 view_desc.u.texture.layer_count = 1;
5195 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
5196 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
5197 wined3d_texture_decref(texture);
5198 if (FAILED(hr))
5200 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
5201 return hr;
5205 if ((view = device->back_buffer_view))
5207 device->back_buffer_view = NULL;
5208 wined3d_rendertarget_view_decref(view);
5210 if (current_desc->backbuffer_count && current_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
5212 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
5214 view_desc.format_id = back_buffer->format->id;
5215 view_desc.flags = 0;
5216 view_desc.u.texture.level_idx = 0;
5217 view_desc.u.texture.level_count = 1;
5218 view_desc.u.texture.layer_idx = 0;
5219 view_desc.u.texture.layer_count = 1;
5220 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
5221 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
5223 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
5224 return hr;
5228 wine_rb_destroy(&device->samplers, device_free_sampler, NULL);
5229 wine_rb_destroy(&device->rasterizer_states, device_free_rasterizer_state, NULL);
5230 wine_rb_destroy(&device->blend_states, device_free_blend_state, NULL);
5231 wine_rb_destroy(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
5233 if (reset_state)
5235 TRACE("Resetting state.\n");
5236 wined3d_device_context_emit_reset_state(&device->cs->c, false);
5237 state_cleanup(state);
5239 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5241 TRACE("Unloading resource %p.\n", resource);
5242 wined3d_cs_emit_unload_resource(device->cs, resource);
5244 if (resource->usage & WINED3DUSAGE_MANAGED)
5245 mark_managed_resource_dirty(resource);
5248 device->adapter->adapter_ops->adapter_uninit_3d(device);
5250 wined3d_state_reset(state, &device->adapter->d3d_info);
5252 device_init_swapchain_state(device, swapchain);
5253 if (wined3d_settings.logo)
5254 device_load_logo(device, wined3d_settings.logo);
5256 hr = device->adapter->adapter_ops->adapter_init_3d(device);
5258 else
5260 if ((view = device->back_buffer_view))
5261 wined3d_device_context_set_rendertarget_views(context, 0, 1, &view, FALSE);
5262 if ((view = device->auto_depth_stencil_view))
5263 wined3d_device_context_set_depth_stencil_view(context, view);
5266 /* All done. There is no need to reload resources or shaders, this will
5267 * happen automatically on the first use. */
5268 return hr;
5271 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5273 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5275 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5277 return WINED3D_OK;
5281 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5282 struct wined3d_device_creation_parameters *parameters)
5284 TRACE("device %p, parameters %p.\n", device, parameters);
5286 *parameters = device->create_parms;
5289 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5291 TRACE("device %p.\n", device);
5293 return device->wined3d;
5296 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5297 UINT swapchain_idx, uint32_t flags, const struct wined3d_gamma_ramp *ramp)
5299 struct wined3d_swapchain *swapchain;
5301 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5302 device, swapchain_idx, flags, ramp);
5304 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5305 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5308 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5309 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5311 struct wined3d_swapchain *swapchain;
5313 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5314 device, swapchain_idx, ramp);
5316 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5317 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5320 HDC wined3d_device_gl_get_backup_dc(struct wined3d_device_gl *device_gl)
5322 TRACE("device_gl %p.\n", device_gl);
5324 if (!device_gl->backup_dc)
5326 TRACE("Creating the backup window for device %p.\n", device_gl);
5328 if (!(device_gl->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
5329 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
5331 ERR("Failed to create a window.\n");
5332 return NULL;
5335 if (!(device_gl->backup_dc = GetDC(device_gl->backup_wnd)))
5337 ERR("Failed to get a DC.\n");
5338 DestroyWindow(device_gl->backup_wnd);
5339 device_gl->backup_wnd = NULL;
5340 return NULL;
5344 return device_gl->backup_dc;
5347 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5349 TRACE("device %p, resource %p.\n", device, resource);
5351 wined3d_not_from_cs(device->cs);
5353 list_add_head(&device->resources, &resource->resource_list_entry);
5356 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5358 TRACE("device %p, resource %p.\n", device, resource);
5360 wined3d_not_from_cs(device->cs);
5362 list_remove(&resource->resource_list_entry);
5365 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5367 enum wined3d_resource_type type = resource->type;
5368 struct wined3d_state *state = device->cs->c.state;
5369 struct wined3d_rendertarget_view *rtv;
5370 unsigned int i;
5372 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5374 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
5376 if ((rtv = state->fb.render_targets[i]) && rtv->resource == resource)
5377 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5380 if ((rtv = state->fb.depth_stencil) && rtv->resource == resource)
5381 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5383 switch (type)
5385 case WINED3D_RTYPE_BUFFER:
5386 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
5388 if (&state->streams[i].buffer->resource == resource)
5390 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5391 state->streams[i].buffer = NULL;
5395 if (&state->index_buffer->resource == resource)
5397 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5398 state->index_buffer = NULL;
5400 break;
5402 default:
5403 break;
5406 /* Remove the resource from the resourceStore */
5407 device_resource_remove(device, resource);
5409 TRACE("Resource released.\n");
5412 static int wined3d_so_desc_compare(const void *key, const struct wine_rb_entry *entry)
5414 const struct wined3d_stream_output_desc *desc = &WINE_RB_ENTRY_VALUE(entry,
5415 struct wined3d_so_desc_entry, entry)->desc;
5416 const struct wined3d_stream_output_desc *k = key;
5417 unsigned int i;
5418 int ret;
5420 if ((ret = wined3d_uint32_compare(k->element_count, desc->element_count)))
5421 return ret;
5422 if ((ret = wined3d_uint32_compare(k->buffer_stride_count, desc->buffer_stride_count)))
5423 return ret;
5424 if ((ret = wined3d_uint32_compare(k->rasterizer_stream_idx, desc->rasterizer_stream_idx)))
5425 return ret;
5427 for (i = 0; i < k->element_count; ++i)
5429 const struct wined3d_stream_output_element *b = &desc->elements[i];
5430 const struct wined3d_stream_output_element *a = &k->elements[i];
5432 if ((ret = wined3d_uint32_compare(a->stream_idx, b->stream_idx)))
5433 return ret;
5434 if ((ret = (!a->semantic_name - !b->semantic_name)))
5435 return ret;
5436 if (a->semantic_name && (ret = strcmp(a->semantic_name, b->semantic_name)))
5437 return ret;
5438 if ((ret = wined3d_uint32_compare(a->semantic_idx, b->semantic_idx)))
5439 return ret;
5440 if ((ret = wined3d_uint32_compare(a->component_idx, b->component_idx)))
5441 return ret;
5442 if ((ret = wined3d_uint32_compare(a->component_count, b->component_count)))
5443 return ret;
5444 if ((ret = wined3d_uint32_compare(a->output_slot, b->output_slot)))
5445 return ret;
5448 for (i = 0; i < k->buffer_stride_count; ++i)
5450 if ((ret = wined3d_uint32_compare(k->buffer_strides[i], desc->buffer_strides[i])))
5451 return ret;
5454 return 0;
5457 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5459 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5461 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5464 static int wined3d_rasterizer_state_compare(const void *key, const struct wine_rb_entry *entry)
5466 const struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
5468 return memcmp(&state->desc, key, sizeof(state->desc));
5471 static int wined3d_blend_state_compare(const void *key, const struct wine_rb_entry *entry)
5473 const struct wined3d_blend_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
5475 return memcmp(&state->desc, key, sizeof(state->desc));
5478 static int wined3d_depth_stencil_state_compare(const void *key, const struct wine_rb_entry *entry)
5480 const struct wined3d_depth_stencil_state *state
5481 = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
5483 return memcmp(&state->desc, key, sizeof(state->desc));
5486 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
5487 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
5488 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
5489 const BOOL *supported_extensions, struct wined3d_device_parent *device_parent)
5491 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
5492 const struct wined3d_fragment_pipe_ops *fragment_pipeline;
5493 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5494 unsigned int i;
5495 HRESULT hr;
5497 device->ref = 1;
5498 device->wined3d = wined3d;
5499 wined3d_incref(device->wined3d);
5500 device->adapter = adapter;
5501 device->device_parent = device_parent;
5502 list_init(&device->resources);
5503 list_init(&device->shaders);
5504 device->surface_alignment = surface_alignment;
5506 /* Save the creation parameters. */
5507 device->create_parms.adapter_idx = adapter_idx;
5508 device->create_parms.device_type = device_type;
5509 device->create_parms.focus_window = focus_window;
5510 device->create_parms.flags = flags;
5512 device->shader_backend = adapter->shader_backend;
5514 vertex_pipeline = adapter->vertex_pipe;
5516 fragment_pipeline = adapter->fragment_pipe;
5518 wine_rb_init(&device->so_descs, wined3d_so_desc_compare);
5519 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5520 wine_rb_init(&device->rasterizer_states, wined3d_rasterizer_state_compare);
5521 wine_rb_init(&device->blend_states, wined3d_blend_state_compare);
5522 wine_rb_init(&device->depth_stencil_states, wined3d_depth_stencil_state_compare);
5524 if (vertex_pipeline->vp_states && fragment_pipeline->states
5525 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
5526 &adapter->d3d_info, supported_extensions, vertex_pipeline,
5527 fragment_pipeline, adapter->misc_state_template)))
5529 ERR("Failed to compile state table, hr %#lx.\n", hr);
5530 wine_rb_destroy(&device->samplers, NULL, NULL);
5531 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5532 wine_rb_destroy(&device->blend_states, NULL, NULL);
5533 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5534 wine_rb_destroy(&device->so_descs, NULL, NULL);
5535 wined3d_decref(device->wined3d);
5536 return hr;
5539 device->max_frame_latency = 3;
5541 if (!(device->cs = wined3d_cs_create(device, levels, level_count)))
5543 WARN("Failed to create command stream.\n");
5544 hr = E_FAIL;
5545 goto err;
5548 wined3d_lock_init(&device->bo_map_lock, "wined3d_device.bo_map_lock");
5550 return WINED3D_OK;
5552 err:
5553 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
5555 free(device->multistate_funcs[i]);
5557 wine_rb_destroy(&device->samplers, NULL, NULL);
5558 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5559 wine_rb_destroy(&device->blend_states, NULL, NULL);
5560 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5561 wine_rb_destroy(&device->so_descs, NULL, NULL);
5562 wined3d_decref(device->wined3d);
5563 return hr;
5566 void device_invalidate_state(const struct wined3d_device *device, unsigned int state_id)
5568 unsigned int representative, i, idx, shift;
5569 struct wined3d_context *context;
5571 wined3d_from_cs(device->cs);
5573 if (STATE_IS_COMPUTE(state_id))
5575 for (i = 0; i < device->context_count; ++i)
5576 context_invalidate_compute_state(device->contexts[i], state_id);
5577 return;
5580 representative = device->state_table[state_id].representative;
5581 idx = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
5582 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
5583 for (i = 0; i < device->context_count; ++i)
5585 device->contexts[i]->dirty_graphics_states[idx] |= (1u << shift);
5589 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5590 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5592 if (message == WM_DESTROY)
5594 TRACE("unregister window %p.\n", window);
5595 wined3d_unregister_window(window);
5597 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5598 ERR("Window %p is not the focus window for device %p.\n", window, device);
5600 else if (message == WM_DISPLAYCHANGE)
5602 device->device_parent->ops->mode_changed(device->device_parent);
5604 else if (message == WM_ACTIVATEAPP)
5606 unsigned int i = device->swapchain_count;
5608 /* Deactivating the implicit swapchain may cause the application
5609 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
5610 * deactivate the implicit swapchain last, and to avoid accessing the
5611 * "device" pointer afterwards. */
5612 while (i--)
5613 wined3d_swapchain_activate(device->swapchains[i], wparam);
5615 else if (message == WM_SYSCOMMAND)
5617 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5619 if (unicode)
5620 DefWindowProcW(window, message, wparam, lparam);
5621 else
5622 DefWindowProcA(window, message, wparam, lparam);
5626 if (unicode)
5627 return CallWindowProcW(proc, window, message, wparam, lparam);
5628 else
5629 return CallWindowProcA(proc, window, message, wparam, lparam);