ntdll: Implement NtCreateDebugObject().
[wine.git] / dlls / wined3d / swapchain.c
blob1671abb698df60f9df5fb7b0d6330c88973dda9f
1 /*
2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29 void wined3d_swapchain_cleanup(struct wined3d_swapchain *swapchain)
31 HRESULT hr;
32 UINT i;
34 TRACE("Destroying swapchain %p.\n", swapchain);
36 wined3d_swapchain_state_cleanup(&swapchain->state);
37 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
39 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
40 * is the last buffer to be destroyed, FindContext() depends on that. */
41 if (swapchain->front_buffer)
43 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
44 if (wined3d_texture_decref(swapchain->front_buffer))
45 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
46 swapchain->front_buffer = NULL;
49 if (swapchain->back_buffers)
51 i = swapchain->state.desc.backbuffer_count;
53 while (i--)
55 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
56 if (wined3d_texture_decref(swapchain->back_buffers[i]))
57 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
59 heap_free(swapchain->back_buffers);
60 swapchain->back_buffers = NULL;
63 /* Restore the screen resolution if we rendered in fullscreen.
64 * This will restore the screen resolution to what it was before creating
65 * the swapchain. In case of d3d8 and d3d9 this will be the original
66 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
67 * sets the resolution before starting up Direct3D, thus orig_width and
68 * orig_height will be equal to the modes in the presentation params. */
69 if (!swapchain->state.desc.windowed)
71 if (swapchain->state.desc.auto_restore_display_mode)
73 if (FAILED(hr = wined3d_restore_display_modes(swapchain->device->wined3d)))
74 ERR("Failed to restore display mode, hr %#x.\n", hr);
76 if (swapchain->state.desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
78 wined3d_swapchain_state_restore_from_fullscreen(&swapchain->state,
79 swapchain->state.device_window, &swapchain->state.original_window_rect);
80 wined3d_device_release_focus_window(swapchain->device);
83 else
85 wined3d_swapchain_state_restore_from_fullscreen(&swapchain->state, swapchain->state.device_window, NULL);
90 static void wined3d_swapchain_gl_destroy_object(void *object)
92 wined3d_swapchain_gl_destroy_contexts(object);
95 void wined3d_swapchain_gl_cleanup(struct wined3d_swapchain_gl *swapchain_gl)
97 struct wined3d_cs *cs = swapchain_gl->s.device->cs;
99 wined3d_swapchain_cleanup(&swapchain_gl->s);
101 wined3d_cs_destroy_object(cs, wined3d_swapchain_gl_destroy_object, swapchain_gl);
102 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
104 if (swapchain_gl->backup_dc)
106 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain_gl->backup_wnd, swapchain_gl->backup_dc);
108 wined3d_release_dc(swapchain_gl->backup_wnd, swapchain_gl->backup_dc);
109 DestroyWindow(swapchain_gl->backup_wnd);
113 static void wined3d_swapchain_vk_destroy_vulkan_swapchain(struct wined3d_swapchain_vk *swapchain_vk)
115 struct wined3d_device_vk *device_vk = wined3d_device_vk(swapchain_vk->s.device);
116 const struct wined3d_vk_info *vk_info;
117 unsigned int i;
118 VkResult vr;
120 TRACE("swapchain_vk %p.\n", swapchain_vk);
122 vk_info = &wined3d_adapter_vk(device_vk->d.adapter)->vk_info;
124 if ((vr = VK_CALL(vkQueueWaitIdle(device_vk->vk_queue))) < 0)
125 ERR("Failed to wait on queue, vr %s.\n", wined3d_debug_vkresult(vr));
126 heap_free(swapchain_vk->vk_images);
127 for (i = 0; i < swapchain_vk->image_count; ++i)
129 VK_CALL(vkDestroySemaphore(device_vk->vk_device, swapchain_vk->vk_semaphores[i].available, NULL));
130 VK_CALL(vkDestroySemaphore(device_vk->vk_device, swapchain_vk->vk_semaphores[i].presentable, NULL));
132 heap_free(swapchain_vk->vk_semaphores);
133 VK_CALL(vkDestroySwapchainKHR(device_vk->vk_device, swapchain_vk->vk_swapchain, NULL));
134 VK_CALL(vkDestroySurfaceKHR(vk_info->instance, swapchain_vk->vk_surface, NULL));
137 static void wined3d_swapchain_vk_destroy_object(void *object)
139 wined3d_swapchain_vk_destroy_vulkan_swapchain(object);
142 void wined3d_swapchain_vk_cleanup(struct wined3d_swapchain_vk *swapchain_vk)
144 struct wined3d_cs *cs = swapchain_vk->s.device->cs;
146 wined3d_cs_destroy_object(cs, wined3d_swapchain_vk_destroy_object, swapchain_vk);
147 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
149 wined3d_swapchain_cleanup(&swapchain_vk->s);
152 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
154 ULONG refcount = InterlockedIncrement(&swapchain->ref);
156 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
158 return refcount;
161 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
163 ULONG refcount = InterlockedDecrement(&swapchain->ref);
165 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
167 if (!refcount)
169 struct wined3d_device *device;
171 wined3d_mutex_lock();
173 device = swapchain->device;
174 if (device->swapchain_count && device->swapchains[0] == swapchain)
175 wined3d_device_uninit_3d(device);
176 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
178 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
179 swapchain->device->adapter->adapter_ops->adapter_destroy_swapchain(swapchain);
181 wined3d_mutex_unlock();
184 return refcount;
187 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
189 TRACE("swapchain %p.\n", swapchain);
191 return swapchain->parent;
194 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
196 if (!window)
197 window = swapchain->state.device_window;
198 if (window == swapchain->win_handle)
199 return;
201 TRACE("Setting swapchain %p window from %p to %p.\n",
202 swapchain, swapchain->win_handle, window);
204 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
206 swapchain->win_handle = window;
209 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
210 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
211 unsigned int swap_interval, DWORD flags)
213 RECT s, d;
215 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, swap_interval %u, flags %#x.\n",
216 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
217 dst_window_override, swap_interval, flags);
219 if (flags)
220 FIXME("Ignoring flags %#x.\n", flags);
222 wined3d_mutex_lock();
224 if (!swapchain->back_buffers)
226 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL.\n");
227 wined3d_mutex_unlock();
228 return WINED3DERR_INVALIDCALL;
231 if (!src_rect)
233 SetRect(&s, 0, 0, swapchain->state.desc.backbuffer_width,
234 swapchain->state.desc.backbuffer_height);
235 src_rect = &s;
238 if (!dst_rect)
240 GetClientRect(swapchain->win_handle, &d);
241 dst_rect = &d;
244 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
245 dst_rect, dst_window_override, swap_interval, flags);
247 wined3d_mutex_unlock();
249 return WINED3D_OK;
252 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
253 struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
255 RECT src_rect, dst_rect;
257 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
259 SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
260 dst_rect = src_rect;
262 if (swapchain->state.desc.windowed)
264 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
265 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
266 wine_dbgstr_rect(&dst_rect));
269 return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
270 swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
273 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
274 UINT back_buffer_idx)
276 TRACE("swapchain %p, back_buffer_idx %u.\n",
277 swapchain, back_buffer_idx);
279 /* Return invalid if there is no backbuffer array, otherwise it will
280 * crash when ddraw is used (there swapchain->back_buffers is always
281 * NULL). We need this because this function is called from
282 * stateblock_init_default_state() to get the default scissorrect
283 * dimensions. */
284 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->state.desc.backbuffer_count)
286 WARN("Invalid back buffer index.\n");
287 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
288 * here in wined3d to avoid problems in other libs. */
289 return NULL;
292 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
294 return swapchain->back_buffers[back_buffer_idx];
297 struct wined3d_output * wined3d_swapchain_get_output(const struct wined3d_swapchain *swapchain)
299 TRACE("swapchain %p.\n", swapchain);
301 return swapchain->state.desc.output;
304 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
305 struct wined3d_raster_status *raster_status)
307 struct wined3d_output *output;
309 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
311 output = wined3d_swapchain_get_output(swapchain);
312 if (!output)
314 ERR("Failed to get output from swapchain %p.\n", swapchain);
315 return E_FAIL;
318 return wined3d_output_get_raster_status(output, raster_status);
321 struct wined3d_swapchain_state * CDECL wined3d_swapchain_get_state(struct wined3d_swapchain *swapchain)
323 return &swapchain->state;
326 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
327 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
329 struct wined3d_output *output;
330 HRESULT hr;
332 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
334 if (!(output = wined3d_swapchain_get_output(swapchain)))
336 ERR("Failed to get output from swapchain %p.\n", swapchain);
337 return E_FAIL;
340 hr = wined3d_output_get_display_mode(output, mode, rotation);
342 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
343 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
345 return hr;
348 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
350 TRACE("swapchain %p.\n", swapchain);
352 return swapchain->device;
355 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
356 struct wined3d_swapchain_desc *desc)
358 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
360 *desc = swapchain->state.desc;
363 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
364 DWORD flags, const struct wined3d_gamma_ramp *ramp)
366 HDC dc;
368 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
370 if (flags)
371 FIXME("Ignoring flags %#x.\n", flags);
373 dc = GetDCEx(swapchain->state.device_window, 0, DCX_USESTYLE | DCX_CACHE);
374 SetDeviceGammaRamp(dc, (void *)ramp);
375 ReleaseDC(swapchain->state.device_window, dc);
377 return WINED3D_OK;
380 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
382 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
384 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
386 swapchain->palette = palette;
389 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
390 struct wined3d_gamma_ramp *ramp)
392 HDC dc;
394 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
396 dc = GetDCEx(swapchain->state.device_window, 0, DCX_USESTYLE | DCX_CACHE);
397 GetDeviceGammaRamp(dc, ramp);
398 ReleaseDC(swapchain->state.device_window, dc);
400 return WINED3D_OK;
403 /* A GL context is provided by the caller */
404 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
405 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
407 struct wined3d_texture *texture = swapchain->back_buffers[0];
408 struct wined3d_device *device = swapchain->device;
409 enum wined3d_texture_filter_type filter;
410 DWORD location;
412 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
413 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
415 if ((src_rect->right - src_rect->left == dst_rect->right - dst_rect->left
416 && src_rect->bottom - src_rect->top == dst_rect->bottom - dst_rect->top)
417 || is_complex_fixup(texture->resource.format->color_fixup))
418 filter = WINED3D_TEXF_NONE;
419 else
420 filter = WINED3D_TEXF_LINEAR;
422 location = WINED3D_LOCATION_TEXTURE_RGB;
423 if (texture->resource.multisample_type)
424 location = WINED3D_LOCATION_RB_RESOLVED;
426 wined3d_texture_validate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
427 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context, texture, 0,
428 location, src_rect, texture, 0, WINED3D_LOCATION_DRAWABLE, dst_rect, NULL, filter);
429 wined3d_texture_invalidate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
432 static void swapchain_gl_set_swap_interval(struct wined3d_swapchain *swapchain,
433 struct wined3d_context_gl *context_gl, unsigned int swap_interval)
435 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
437 swap_interval = swap_interval <= 4 ? swap_interval : 1;
438 if (swapchain->swap_interval == swap_interval)
439 return;
441 swapchain->swap_interval = swap_interval;
443 if (!gl_info->supported[WGL_EXT_SWAP_CONTROL])
444 return;
446 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
448 ERR("Failed to set swap interval %u for context %p, last error %#x.\n",
449 swap_interval, context_gl, GetLastError());
453 /* Context activation is done by the caller. */
454 static void wined3d_swapchain_gl_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context *context)
456 struct wined3d_texture_sub_resource *sub_resource;
457 struct wined3d_texture_gl *texture, *texture_prev;
458 struct gl_texture tex0;
459 GLuint rb0;
460 DWORD locations0;
461 unsigned int i;
462 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
464 if (swapchain->state.desc.backbuffer_count < 2 || !swapchain->render_to_fbo)
465 return;
467 texture_prev = wined3d_texture_gl(swapchain->back_buffers[0]);
469 /* Back buffer 0 is already in the draw binding. */
470 tex0 = texture_prev->texture_rgb;
471 rb0 = texture_prev->rb_multisample;
472 locations0 = texture_prev->t.sub_resources[0].locations;
474 for (i = 1; i < swapchain->state.desc.backbuffer_count; ++i)
476 texture = wined3d_texture_gl(swapchain->back_buffers[i]);
477 sub_resource = &texture->t.sub_resources[0];
479 if (!(sub_resource->locations & supported_locations))
480 wined3d_texture_load_location(&texture->t, 0, context, texture->t.resource.draw_binding);
482 texture_prev->texture_rgb = texture->texture_rgb;
483 texture_prev->rb_multisample = texture->rb_multisample;
485 wined3d_texture_validate_location(&texture_prev->t, 0, sub_resource->locations & supported_locations);
486 wined3d_texture_invalidate_location(&texture_prev->t, 0, ~(sub_resource->locations & supported_locations));
488 texture_prev = texture;
491 texture_prev->texture_rgb = tex0;
492 texture_prev->rb_multisample = rb0;
494 wined3d_texture_validate_location(&texture_prev->t, 0, locations0 & supported_locations);
495 wined3d_texture_invalidate_location(&texture_prev->t, 0, ~(locations0 & supported_locations));
497 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
500 static void swapchain_gl_present(struct wined3d_swapchain *swapchain,
501 const RECT *src_rect, const RECT *dst_rect, unsigned int swap_interval, DWORD flags)
503 struct wined3d_swapchain_gl *swapchain_gl = wined3d_swapchain_gl(swapchain);
504 const struct wined3d_swapchain_desc *desc = &swapchain->state.desc;
505 struct wined3d_texture *back_buffer = swapchain->back_buffers[0];
506 const struct wined3d_gl_info *gl_info;
507 struct wined3d_context_gl *context_gl;
508 struct wined3d_context *context;
509 BOOL render_to_fbo;
511 context = context_acquire(swapchain->device, swapchain->front_buffer, 0);
512 context_gl = wined3d_context_gl(context);
513 if (!context_gl->valid)
515 context_release(context);
516 WARN("Invalid context, skipping present.\n");
517 return;
520 gl_info = context_gl->gl_info;
522 swapchain_gl_set_swap_interval(swapchain, context_gl, swap_interval);
524 TRACE("Presenting DC %p.\n", context_gl->dc);
526 if (!(render_to_fbo = swapchain->render_to_fbo)
527 && (src_rect->left || src_rect->top
528 || src_rect->right != desc->backbuffer_width
529 || src_rect->bottom != desc->backbuffer_height
530 || dst_rect->left || dst_rect->top
531 || dst_rect->right != desc->backbuffer_width
532 || dst_rect->bottom != desc->backbuffer_height))
533 render_to_fbo = TRUE;
535 /* Rendering to a window of different size, presenting partial rectangles,
536 * or rendering to a different window needs help from FBO_blit or a textured
537 * draw. Render the swapchain to a FBO in the future.
539 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
540 * all these issues - this fails if the window is smaller than the backbuffer.
542 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
544 wined3d_texture_load_location(back_buffer, 0, context, WINED3D_LOCATION_TEXTURE_RGB);
545 wined3d_texture_invalidate_location(back_buffer, 0, WINED3D_LOCATION_DRAWABLE);
546 swapchain->render_to_fbo = TRUE;
547 swapchain_update_draw_bindings(swapchain);
549 else
551 wined3d_texture_load_location(back_buffer, 0, context, back_buffer->resource.draw_binding);
554 if (swapchain->render_to_fbo)
555 swapchain_blit(swapchain, context, src_rect, dst_rect);
557 if (swapchain_gl->context_count > 1)
558 gl_info->gl_ops.gl.p_glFinish();
560 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
561 gl_info->gl_ops.wgl.p_wglSwapBuffers(context_gl->dc);
562 wined3d_context_gl_submit_command_fence(context_gl);
564 wined3d_swapchain_gl_rotate(swapchain, context);
566 TRACE("SwapBuffers called, Starting new frame\n");
568 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
569 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
571 context_release(context);
574 static void swapchain_frontbuffer_updated(struct wined3d_swapchain *swapchain)
576 struct wined3d_texture *front_buffer = swapchain->front_buffer;
577 struct wined3d_context *context;
579 context = context_acquire(swapchain->device, front_buffer, 0);
580 wined3d_texture_load_location(front_buffer, 0, context, front_buffer->resource.draw_binding);
581 context_release(context);
582 SetRectEmpty(&swapchain->front_buffer_update);
585 static const struct wined3d_swapchain_ops swapchain_gl_ops =
587 swapchain_gl_present,
588 swapchain_frontbuffer_updated,
591 static bool wined3d_swapchain_vk_present_mode_supported(struct wined3d_swapchain_vk *swapchain_vk,
592 VkPresentModeKHR vk_present_mode)
594 struct wined3d_device_vk *device_vk = wined3d_device_vk(swapchain_vk->s.device);
595 const struct wined3d_vk_info *vk_info;
596 struct wined3d_adapter_vk *adapter_vk;
597 VkPhysicalDevice vk_physical_device;
598 VkPresentModeKHR *vk_modes;
599 bool supported = false;
600 uint32_t count, i;
601 VkResult vr;
603 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
604 vk_physical_device = adapter_vk->physical_device;
605 vk_info = &adapter_vk->vk_info;
607 if ((vr = VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device,
608 swapchain_vk->vk_surface, &count, NULL))) < 0)
610 ERR("Failed to get supported present mode count, vr %s.\n", wined3d_debug_vkresult(vr));
611 return false;
614 if (!(vk_modes = heap_calloc(count, sizeof(*vk_modes))))
615 return false;
617 if ((vr = VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device,
618 swapchain_vk->vk_surface, &count, vk_modes))) < 0)
620 ERR("Failed to get supported present modes, vr %s.\n", wined3d_debug_vkresult(vr));
621 goto done;
624 for (i = 0; i < count; ++i)
626 if (vk_modes[i] == vk_present_mode)
628 supported = true;
629 goto done;
633 done:
634 heap_free(vk_modes);
635 return supported;
638 static VkFormat get_swapchain_fallback_format(VkFormat vk_format)
640 switch (vk_format)
642 case VK_FORMAT_R8G8B8A8_SRGB:
643 return VK_FORMAT_B8G8R8A8_SRGB;
644 case VK_FORMAT_R8G8B8A8_UNORM:
645 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
646 case VK_FORMAT_R16G16B16A16_SFLOAT:
647 return VK_FORMAT_B8G8R8A8_UNORM;
648 default:
649 WARN("Unhandled format %#x.\n", vk_format);
650 return VK_FORMAT_UNDEFINED;
654 static VkFormat wined3d_swapchain_vk_select_vk_format(struct wined3d_swapchain_vk *swapchain_vk,
655 VkSurfaceKHR vk_surface)
657 struct wined3d_device_vk *device_vk = wined3d_device_vk(swapchain_vk->s.device);
658 const struct wined3d_swapchain_desc *desc = &swapchain_vk->s.state.desc;
659 const struct wined3d_vk_info *vk_info;
660 struct wined3d_adapter_vk *adapter_vk;
661 const struct wined3d_format *format;
662 VkPhysicalDevice vk_physical_device;
663 VkSurfaceFormatKHR *vk_formats;
664 uint32_t format_count, i;
665 VkFormat vk_format;
666 VkResult vr;
668 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
669 vk_physical_device = adapter_vk->physical_device;
670 vk_info = &adapter_vk->vk_info;
672 if ((format = wined3d_get_format(&adapter_vk->a, desc->backbuffer_format, WINED3D_BIND_RENDER_TARGET)))
673 vk_format = wined3d_format_vk(format)->vk_format;
674 else
675 vk_format = VK_FORMAT_B8G8R8A8_UNORM;
677 vr = VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device, vk_surface, &format_count, NULL));
678 if (vr < 0 || !format_count)
680 WARN("Failed to get supported surface format count, vr %s.\n", wined3d_debug_vkresult(vr));
681 return VK_FORMAT_UNDEFINED;
684 if (!(vk_formats = heap_calloc(format_count, sizeof(*vk_formats))))
685 return VK_FORMAT_UNDEFINED;
687 if ((vr = VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device,
688 vk_surface, &format_count, vk_formats))) < 0)
690 WARN("Failed to get supported surface formats, vr %s.\n", wined3d_debug_vkresult(vr));
691 heap_free(vk_formats);
692 return VK_FORMAT_UNDEFINED;
695 for (i = 0; i < format_count; ++i)
697 if (vk_formats[i].format == vk_format && vk_formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
698 break;
700 if (i == format_count)
702 /* Try to create a swapchain with format conversion. */
703 vk_format = get_swapchain_fallback_format(vk_format);
704 WARN("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc->backbuffer_format));
705 for (i = 0; i < format_count; ++i)
707 if (vk_formats[i].format == vk_format && vk_formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
708 break;
711 heap_free(vk_formats);
712 if (i == format_count)
714 FIXME("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc->backbuffer_format));
715 return VK_FORMAT_UNDEFINED;
718 TRACE("Using Vulkan swapchain format %#x.\n", vk_format);
720 return vk_format;
723 static bool wined3d_swapchain_vk_create_vulkan_swapchain_images(struct wined3d_swapchain_vk *swapchain_vk,
724 VkSwapchainKHR vk_swapchain)
726 struct wined3d_device_vk *device_vk = wined3d_device_vk(swapchain_vk->s.device);
727 const struct wined3d_vk_info *vk_info;
728 VkSemaphoreCreateInfo semaphore_info;
729 uint32_t image_count, i;
730 VkResult vr;
732 vk_info = &wined3d_adapter_vk(device_vk->d.adapter)->vk_info;
734 if ((vr = VK_CALL(vkGetSwapchainImagesKHR(device_vk->vk_device, vk_swapchain, &image_count, NULL))) < 0)
736 ERR("Failed to get image count, vr %s\n", wined3d_debug_vkresult(vr));
737 return false;
740 if (!(swapchain_vk->vk_images = heap_calloc(image_count, sizeof(*swapchain_vk->vk_images))))
742 ERR("Failed to allocate images array.\n");
743 return false;
746 if ((vr = VK_CALL(vkGetSwapchainImagesKHR(device_vk->vk_device,
747 vk_swapchain, &image_count, swapchain_vk->vk_images))) < 0)
749 ERR("Failed to get swapchain images, vr %s.\n", wined3d_debug_vkresult(vr));
750 heap_free(swapchain_vk->vk_images);
751 return false;
754 if (!(swapchain_vk->vk_semaphores = heap_calloc(image_count, sizeof(*swapchain_vk->vk_semaphores))))
756 ERR("Failed to allocate semaphores array.\n");
757 heap_free(swapchain_vk->vk_images);
758 return false;
761 semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
762 semaphore_info.pNext = NULL;
763 semaphore_info.flags = 0;
764 for (i = 0; i < image_count; ++i)
766 if ((vr = VK_CALL(vkCreateSemaphore(device_vk->vk_device,
767 &semaphore_info, NULL, &swapchain_vk->vk_semaphores[i].available))) < 0)
769 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr));
770 goto fail;
773 if ((vr = VK_CALL(vkCreateSemaphore(device_vk->vk_device,
774 &semaphore_info, NULL, &swapchain_vk->vk_semaphores[i].presentable))) < 0)
776 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr));
777 goto fail;
780 swapchain_vk->image_count = image_count;
782 return true;
784 fail:
785 for (i = 0; i < image_count; ++i)
787 if (swapchain_vk->vk_semaphores[i].available)
788 VK_CALL(vkDestroySemaphore(device_vk->vk_device, swapchain_vk->vk_semaphores[i].available, NULL));
789 if (swapchain_vk->vk_semaphores[i].presentable)
790 VK_CALL(vkDestroySemaphore(device_vk->vk_device, swapchain_vk->vk_semaphores[i].presentable, NULL));
792 heap_free(swapchain_vk->vk_semaphores);
793 heap_free(swapchain_vk->vk_images);
794 return false;
797 static HRESULT wined3d_swapchain_vk_create_vulkan_swapchain(struct wined3d_swapchain_vk *swapchain_vk)
799 struct wined3d_device_vk *device_vk = wined3d_device_vk(swapchain_vk->s.device);
800 const struct wined3d_swapchain_desc *desc = &swapchain_vk->s.state.desc;
801 VkSwapchainCreateInfoKHR vk_swapchain_desc;
802 VkWin32SurfaceCreateInfoKHR surface_desc;
803 unsigned int width, height, image_count;
804 const struct wined3d_vk_info *vk_info;
805 VkSurfaceCapabilitiesKHR surface_caps;
806 struct wined3d_adapter_vk *adapter_vk;
807 VkPresentModeKHR vk_present_mode;
808 VkSwapchainKHR vk_swapchain;
809 VkImageUsageFlags usage;
810 VkSurfaceKHR vk_surface;
811 VkBool32 supported;
812 VkFormat vk_format;
813 VkResult vr;
815 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
816 vk_info = &adapter_vk->vk_info;
818 surface_desc.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
819 surface_desc.pNext = NULL;
820 surface_desc.flags = 0;
821 surface_desc.hinstance = (HINSTANCE)GetWindowLongPtrW(swapchain_vk->s.win_handle, GWLP_HINSTANCE);
822 surface_desc.hwnd = swapchain_vk->s.win_handle;
823 if ((vr = VK_CALL(vkCreateWin32SurfaceKHR(vk_info->instance, &surface_desc, NULL, &vk_surface))) < 0)
825 ERR("Failed to create Vulkan surface, vr %s.\n", wined3d_debug_vkresult(vr));
826 return E_FAIL;
828 swapchain_vk->vk_surface = vk_surface;
830 if ((vr = VK_CALL(vkGetPhysicalDeviceSurfaceSupportKHR(adapter_vk->physical_device,
831 device_vk->vk_queue_family_index, vk_surface, &supported))) < 0 || !supported)
833 ERR("Queue family does not support presentation on this surface, vr %s.\n", wined3d_debug_vkresult(vr));
834 goto fail;
837 if ((vk_format = wined3d_swapchain_vk_select_vk_format(swapchain_vk, vk_surface)) == VK_FORMAT_UNDEFINED)
839 ERR("Failed to select swapchain format.\n");
840 goto fail;
843 if ((vr = VK_CALL(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(adapter_vk->physical_device,
844 swapchain_vk->vk_surface, &surface_caps))) < 0)
846 ERR("Failed to get surface capabilities, vr %s.\n", wined3d_debug_vkresult(vr));
847 goto fail;
850 image_count = desc->backbuffer_count;
851 if (image_count < surface_caps.minImageCount)
852 image_count = surface_caps.minImageCount;
853 else if (surface_caps.maxImageCount && image_count > surface_caps.maxImageCount)
854 image_count = surface_caps.maxImageCount;
856 if (image_count != desc->backbuffer_count)
857 WARN("Image count %u is not supported (%u-%u).\n", desc->backbuffer_count,
858 surface_caps.minImageCount, surface_caps.maxImageCount);
860 width = desc->backbuffer_width;
861 if (width < surface_caps.minImageExtent.width)
862 width = surface_caps.minImageExtent.width;
863 else if (width > surface_caps.maxImageExtent.width)
864 width = surface_caps.maxImageExtent.width;
866 height = desc->backbuffer_height;
867 if (height < surface_caps.minImageExtent.height)
868 height = surface_caps.minImageExtent.height;
869 else if (height > surface_caps.maxImageExtent.height)
870 height = surface_caps.maxImageExtent.height;
872 if (width != desc->backbuffer_width || height != desc->backbuffer_height)
873 WARN("Swapchain dimensions %ux%u are not supported (%u-%u x %u-%u).\n",
874 desc->backbuffer_width, desc->backbuffer_height,
875 surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width,
876 surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height);
878 usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
879 usage |= surface_caps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
880 usage |= surface_caps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT;
881 if (!(usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) || !(usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT))
882 WARN("Transfer not supported for swapchain images.\n");
884 if (!(surface_caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR))
886 FIXME("Unsupported alpha mode, %#x.\n", surface_caps.supportedCompositeAlpha);
887 goto fail;
890 vk_present_mode = VK_PRESENT_MODE_FIFO_KHR;
891 if (!swapchain_vk->s.swap_interval)
893 if (wined3d_swapchain_vk_present_mode_supported(swapchain_vk, VK_PRESENT_MODE_IMMEDIATE_KHR))
894 vk_present_mode = VK_PRESENT_MODE_IMMEDIATE_KHR;
895 else
896 FIXME("Unsupported swap interval %u.\n", swapchain_vk->s.swap_interval);
899 vk_swapchain_desc.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
900 vk_swapchain_desc.pNext = NULL;
901 vk_swapchain_desc.flags = 0;
902 vk_swapchain_desc.surface = vk_surface;
903 vk_swapchain_desc.minImageCount = image_count;
904 vk_swapchain_desc.imageFormat = vk_format;
905 vk_swapchain_desc.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
906 vk_swapchain_desc.imageExtent.width = width;
907 vk_swapchain_desc.imageExtent.height = height;
908 vk_swapchain_desc.imageArrayLayers = 1;
909 vk_swapchain_desc.imageUsage = usage;
910 vk_swapchain_desc.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
911 vk_swapchain_desc.queueFamilyIndexCount = 0;
912 vk_swapchain_desc.pQueueFamilyIndices = NULL;
913 vk_swapchain_desc.preTransform = surface_caps.currentTransform;
914 vk_swapchain_desc.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
915 vk_swapchain_desc.presentMode = vk_present_mode;
916 vk_swapchain_desc.clipped = VK_TRUE;
917 vk_swapchain_desc.oldSwapchain = VK_NULL_HANDLE;
918 if ((vr = VK_CALL(vkCreateSwapchainKHR(device_vk->vk_device, &vk_swapchain_desc, NULL, &vk_swapchain))) < 0)
920 ERR("Failed to create Vulkan swapchain, vr %s.\n", wined3d_debug_vkresult(vr));
921 goto fail;
923 swapchain_vk->vk_swapchain = vk_swapchain;
925 if (!wined3d_swapchain_vk_create_vulkan_swapchain_images(swapchain_vk, vk_swapchain))
927 VK_CALL(vkDestroySwapchainKHR(device_vk->vk_device, vk_swapchain, NULL));
928 goto fail;
931 return WINED3D_OK;
933 fail:
934 VK_CALL(vkDestroySurfaceKHR(vk_info->instance, vk_surface, NULL));
935 return E_FAIL;
938 static HRESULT wined3d_swapchain_vk_recreate(struct wined3d_swapchain_vk *swapchain_vk)
940 TRACE("swapchain_vk %p.\n", swapchain_vk);
942 wined3d_swapchain_vk_destroy_vulkan_swapchain(swapchain_vk);
944 return wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk);
947 static void wined3d_swapchain_vk_set_swap_interval(struct wined3d_swapchain_vk *swapchain_vk,
948 unsigned int swap_interval)
950 if (swap_interval > 1)
952 if (swap_interval <= 4)
953 FIXME("Unsupported swap interval %u.\n", swap_interval);
954 swap_interval = 1;
957 if (swapchain_vk->s.swap_interval == swap_interval)
958 return;
960 swapchain_vk->s.swap_interval = swap_interval;
961 wined3d_swapchain_vk_recreate(swapchain_vk);
964 static void wined3d_swapchain_vk_blit(struct wined3d_swapchain_vk *swapchain_vk,
965 struct wined3d_context_vk *context_vk, const RECT *src_rect, const RECT *dst_rect, unsigned int swap_interval)
967 struct wined3d_texture_vk *back_buffer_vk = wined3d_texture_vk(swapchain_vk->s.back_buffers[0]);
968 struct wined3d_device_vk *device_vk = wined3d_device_vk(swapchain_vk->s.device);
969 const struct wined3d_swapchain_desc *desc = &swapchain_vk->s.state.desc;
970 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
971 VkCommandBuffer vk_command_buffer;
972 VkPresentInfoKHR present_desc;
973 unsigned int present_idx;
974 VkImageLayout vk_layout;
975 uint32_t image_idx;
976 VkImageBlit blit;
977 VkResult vr;
978 HRESULT hr;
980 static const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
982 wined3d_swapchain_vk_set_swap_interval(swapchain_vk, swap_interval);
984 present_idx = swapchain_vk->current++ % swapchain_vk->image_count;
985 wined3d_context_vk_wait_command_buffer(context_vk, swapchain_vk->vk_semaphores[present_idx].command_buffer_id);
986 vr = VK_CALL(vkAcquireNextImageKHR(device_vk->vk_device, swapchain_vk->vk_swapchain, UINT64_MAX,
987 swapchain_vk->vk_semaphores[present_idx].available, VK_NULL_HANDLE, &image_idx));
988 if (vr == VK_ERROR_OUT_OF_DATE_KHR)
990 if (FAILED(hr = wined3d_swapchain_vk_recreate(swapchain_vk)))
992 ERR("Failed to recreate swapchain, hr %#x.\n", hr);
993 return;
995 vr = VK_CALL(vkAcquireNextImageKHR(device_vk->vk_device, swapchain_vk->vk_swapchain, UINT64_MAX,
996 swapchain_vk->vk_semaphores[present_idx].available, VK_NULL_HANDLE, &image_idx));
998 if (vr < 0)
1000 ERR("Failed to acquire next Vulkan image, vr %s.\n", wined3d_debug_vkresult(vr));
1001 return;
1004 vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk);
1006 wined3d_context_vk_end_current_render_pass(context_vk);
1008 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1009 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1010 vk_access_mask_from_bind_flags(back_buffer_vk->t.resource.bind_flags),
1011 VK_ACCESS_TRANSFER_READ_BIT,
1012 back_buffer_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1013 back_buffer_vk->vk_image, VK_IMAGE_ASPECT_COLOR_BIT);
1015 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1016 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1017 0, VK_ACCESS_TRANSFER_WRITE_BIT,
1018 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1019 swapchain_vk->vk_images[image_idx], VK_IMAGE_ASPECT_COLOR_BIT);
1021 blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1022 blit.srcSubresource.mipLevel = 0;
1023 blit.srcSubresource.baseArrayLayer = 0;
1024 blit.srcSubresource.layerCount = 1;
1025 blit.srcOffsets[0].x = src_rect->left;
1026 blit.srcOffsets[0].y = src_rect->top;
1027 blit.srcOffsets[0].z = 0;
1028 blit.srcOffsets[1].x = src_rect->right;
1029 blit.srcOffsets[1].y = src_rect->bottom;
1030 blit.srcOffsets[1].z = 1;
1031 blit.dstSubresource = blit.srcSubresource;
1032 blit.dstOffsets[0].x = dst_rect->left;
1033 blit.dstOffsets[0].y = dst_rect->top;
1034 blit.dstOffsets[0].z = 0;
1035 blit.dstOffsets[1].x = dst_rect->right;
1036 blit.dstOffsets[1].y = dst_rect->bottom;
1037 blit.dstOffsets[1].z = 1;
1038 VK_CALL(vkCmdBlitImage(vk_command_buffer,
1039 back_buffer_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1040 swapchain_vk->vk_images[image_idx], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1041 1, &blit, VK_FILTER_NEAREST));
1043 wined3d_context_vk_reference_texture(context_vk, back_buffer_vk);
1044 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1045 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1046 VK_ACCESS_TRANSFER_WRITE_BIT, 0,
1047 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1048 swapchain_vk->vk_images[image_idx], VK_IMAGE_ASPECT_COLOR_BIT);
1050 if (desc->swap_effect == WINED3D_SWAP_EFFECT_DISCARD || desc->swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
1051 vk_layout = VK_IMAGE_LAYOUT_UNDEFINED;
1052 else
1053 vk_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
1054 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1055 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
1056 VK_ACCESS_TRANSFER_READ_BIT,
1057 vk_access_mask_from_bind_flags(back_buffer_vk->t.resource.bind_flags),
1058 vk_layout, back_buffer_vk->layout,
1059 back_buffer_vk->vk_image, VK_IMAGE_ASPECT_COLOR_BIT);
1060 back_buffer_vk->bind_mask = 0;
1062 swapchain_vk->vk_semaphores[present_idx].command_buffer_id = context_vk->current_command_buffer.id;
1063 wined3d_context_vk_submit_command_buffer(context_vk,
1064 1, &swapchain_vk->vk_semaphores[present_idx].available, &wait_stage,
1065 1, &swapchain_vk->vk_semaphores[present_idx].presentable);
1067 present_desc.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
1068 present_desc.pNext = NULL;
1069 present_desc.waitSemaphoreCount = 1;
1070 present_desc.pWaitSemaphores = &swapchain_vk->vk_semaphores[present_idx].presentable;
1071 present_desc.swapchainCount = 1;
1072 present_desc.pSwapchains = &swapchain_vk->vk_swapchain;
1073 present_desc.pImageIndices = &image_idx;
1074 present_desc.pResults = NULL;
1075 if ((vr = VK_CALL(vkQueuePresentKHR(device_vk->vk_queue, &present_desc))))
1076 ERR("Present returned vr %s.\n", wined3d_debug_vkresult(vr));
1079 static void wined3d_swapchain_vk_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context_vk *context_vk)
1081 struct wined3d_texture_sub_resource *sub_resource;
1082 struct wined3d_texture_vk *texture, *texture_prev;
1083 struct wined3d_allocator_block *memory0;
1084 VkDescriptorImageInfo vk_info0;
1085 VkDeviceMemory vk_memory0;
1086 VkImageLayout vk_layout0;
1087 VkImage vk_image0;
1088 DWORD locations0;
1089 unsigned int i;
1090 uint64_t id0;
1092 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
1094 if (swapchain->state.desc.backbuffer_count < 2)
1095 return;
1097 texture_prev = wined3d_texture_vk(swapchain->back_buffers[0]);
1099 /* Back buffer 0 is already in the draw binding. */
1100 vk_image0 = texture_prev->vk_image;
1101 memory0 = texture_prev->memory;
1102 vk_memory0 = texture_prev->vk_memory;
1103 vk_layout0 = texture_prev->layout;
1104 id0 = texture_prev->command_buffer_id;
1105 vk_info0 = texture_prev->default_image_info;
1106 locations0 = texture_prev->t.sub_resources[0].locations;
1108 for (i = 1; i < swapchain->state.desc.backbuffer_count; ++i)
1110 texture = wined3d_texture_vk(swapchain->back_buffers[i]);
1111 sub_resource = &texture->t.sub_resources[0];
1113 if (!(sub_resource->locations & supported_locations))
1114 wined3d_texture_load_location(&texture->t, 0, &context_vk->c, texture->t.resource.draw_binding);
1116 texture_prev->vk_image = texture->vk_image;
1117 texture_prev->memory = texture->memory;
1118 texture_prev->vk_memory = texture->vk_memory;
1119 texture_prev->layout = texture->layout;
1120 texture_prev->command_buffer_id = texture->command_buffer_id;
1121 texture_prev->default_image_info = texture->default_image_info;
1123 wined3d_texture_validate_location(&texture_prev->t, 0, sub_resource->locations & supported_locations);
1124 wined3d_texture_invalidate_location(&texture_prev->t, 0, ~(sub_resource->locations & supported_locations));
1126 texture_prev = texture;
1129 texture_prev->vk_image = vk_image0;
1130 texture_prev->memory = memory0;
1131 texture_prev->vk_memory = vk_memory0;
1132 texture_prev->layout = vk_layout0;
1133 texture_prev->command_buffer_id = id0;
1134 texture_prev->default_image_info = vk_info0;
1136 wined3d_texture_validate_location(&texture_prev->t, 0, locations0 & supported_locations);
1137 wined3d_texture_invalidate_location(&texture_prev->t, 0, ~(locations0 & supported_locations));
1139 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
1142 static void swapchain_vk_present(struct wined3d_swapchain *swapchain, const RECT *src_rect,
1143 const RECT *dst_rect, unsigned int swap_interval, uint32_t flags)
1145 struct wined3d_swapchain_vk *swapchain_vk = wined3d_swapchain_vk(swapchain);
1146 struct wined3d_texture *back_buffer = swapchain->back_buffers[0];
1147 struct wined3d_context_vk *context_vk;
1149 context_vk = wined3d_context_vk(context_acquire(swapchain->device, back_buffer, 0));
1151 wined3d_texture_load_location(back_buffer, 0, &context_vk->c, back_buffer->resource.draw_binding);
1153 if (swapchain_vk->vk_swapchain)
1154 wined3d_swapchain_vk_blit(swapchain_vk, context_vk, src_rect, dst_rect, swap_interval);
1156 wined3d_swapchain_vk_rotate(swapchain, context_vk);
1158 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
1159 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
1161 TRACE("Starting new frame.\n");
1163 context_release(&context_vk->c);
1166 static const struct wined3d_swapchain_ops swapchain_vk_ops =
1168 swapchain_vk_present,
1169 swapchain_frontbuffer_updated,
1172 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
1174 struct wined3d_dc_info *front;
1175 POINT offset = {0, 0};
1176 HDC src_dc, dst_dc;
1177 RECT draw_rect;
1178 HWND window;
1180 TRACE("swapchain %p.\n", swapchain);
1182 front = &swapchain->front_buffer->dc_info[0];
1183 if (swapchain->palette)
1184 wined3d_palette_apply_to_dc(swapchain->palette, front->dc);
1186 if (swapchain->front_buffer->resource.map_count)
1187 ERR("Trying to blit a mapped surface.\n");
1189 TRACE("Copying surface %p to screen.\n", front);
1191 src_dc = front->dc;
1192 window = swapchain->win_handle;
1193 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
1195 /* Front buffer coordinates are screen coordinates. Map them to the
1196 * destination window if not fullscreened. */
1197 if (swapchain->state.desc.windowed)
1198 ClientToScreen(window, &offset);
1200 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
1202 SetRect(&draw_rect, 0, 0, swapchain->front_buffer->resource.width,
1203 swapchain->front_buffer->resource.height);
1204 IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
1206 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
1207 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
1208 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
1209 ReleaseDC(window, dst_dc);
1211 SetRectEmpty(&swapchain->front_buffer_update);
1214 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
1215 const RECT *src_rect, const RECT *dst_rect, unsigned int swap_interval, DWORD flags)
1217 struct wined3d_dc_info *front, *back;
1218 HBITMAP bitmap;
1219 void *data;
1220 HDC dc;
1222 front = &swapchain->front_buffer->dc_info[0];
1223 back = &swapchain->back_buffers[0]->dc_info[0];
1225 /* Flip the surface data. */
1226 dc = front->dc;
1227 bitmap = front->bitmap;
1228 data = swapchain->front_buffer->resource.heap_memory;
1230 front->dc = back->dc;
1231 front->bitmap = back->bitmap;
1232 swapchain->front_buffer->resource.heap_memory = swapchain->back_buffers[0]->resource.heap_memory;
1234 back->dc = dc;
1235 back->bitmap = bitmap;
1236 swapchain->back_buffers[0]->resource.heap_memory = data;
1238 SetRect(&swapchain->front_buffer_update, 0, 0,
1239 swapchain->front_buffer->resource.width,
1240 swapchain->front_buffer->resource.height);
1241 swapchain_gdi_frontbuffer_updated(swapchain);
1244 static const struct wined3d_swapchain_ops swapchain_no3d_ops =
1246 swapchain_gdi_present,
1247 swapchain_gdi_frontbuffer_updated,
1250 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
1252 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
1253 return;
1255 if (!swapchain->state.desc.backbuffer_count)
1257 TRACE("Single buffered rendering.\n");
1258 swapchain->render_to_fbo = FALSE;
1259 return;
1262 TRACE("Rendering to FBO.\n");
1263 swapchain->render_to_fbo = TRUE;
1266 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
1267 enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
1269 const struct wined3d_adapter *adapter;
1270 const struct wined3d_gl_info *gl_info;
1271 const struct wined3d_format *format;
1272 enum wined3d_multisample_type t;
1274 if (wined3d_settings.sample_count == ~0u)
1275 return;
1277 adapter = swapchain->device->adapter;
1278 gl_info = &adapter->gl_info;
1279 if (!(format = wined3d_get_format(adapter, format_id, WINED3D_BIND_RENDER_TARGET)))
1280 return;
1282 if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
1283 while (!(format->multisample_types & 1u << (t - 1)))
1284 ++t;
1285 TRACE("Using sample count %u.\n", t);
1286 *type = t;
1287 *quality = 0;
1290 void swapchain_set_max_frame_latency(struct wined3d_swapchain *swapchain, const struct wined3d_device *device)
1292 /* Subtract 1 for the implicit OpenGL latency. */
1293 swapchain->max_frame_latency = device->max_frame_latency >= 2 ? device->max_frame_latency - 1 : 1;
1296 static enum wined3d_format_id adapter_format_from_backbuffer_format(const struct wined3d_adapter *adapter,
1297 enum wined3d_format_id format_id)
1299 const struct wined3d_format *backbuffer_format;
1301 backbuffer_format = wined3d_get_format(adapter, format_id, WINED3D_BIND_RENDER_TARGET);
1302 return pixelformat_for_depth(backbuffer_format->byte_count * CHAR_BIT);
1305 static HRESULT wined3d_swapchain_state_init(struct wined3d_swapchain_state *state,
1306 const struct wined3d_swapchain_desc *desc, HWND window, struct wined3d *wined3d,
1307 struct wined3d_swapchain_state_parent *parent)
1309 HRESULT hr;
1311 state->desc = *desc;
1313 if (FAILED(hr = wined3d_output_get_display_mode(desc->output, &state->original_mode, NULL)))
1315 ERR("Failed to get current display mode, hr %#x.\n", hr);
1316 return hr;
1319 if (!desc->windowed)
1321 if (desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1323 state->d3d_mode.width = desc->backbuffer_width;
1324 state->d3d_mode.height = desc->backbuffer_height;
1325 state->d3d_mode.format_id = adapter_format_from_backbuffer_format(desc->output->adapter,
1326 desc->backbuffer_format);
1327 state->d3d_mode.refresh_rate = desc->refresh_rate;
1328 state->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1330 else
1332 state->d3d_mode = state->original_mode;
1336 GetWindowRect(window, &state->original_window_rect);
1337 state->wined3d = wined3d;
1338 state->device_window = window;
1339 state->parent = parent;
1341 if (desc->flags & WINED3D_SWAPCHAIN_REGISTER_STATE)
1342 wined3d_swapchain_state_register(state);
1344 return hr;
1347 static HRESULT wined3d_swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
1348 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain_state_parent *state_parent,
1349 void *parent, const struct wined3d_parent_ops *parent_ops,
1350 const struct wined3d_swapchain_ops *swapchain_ops)
1352 struct wined3d_resource_desc texture_desc;
1353 struct wined3d_output_desc output_desc;
1354 BOOL displaymode_set = FALSE;
1355 DWORD texture_flags = 0;
1356 HRESULT hr = E_FAIL;
1357 RECT client_rect;
1358 unsigned int i;
1359 HWND window;
1361 wined3d_mutex_lock();
1363 if (desc->backbuffer_count > 1)
1365 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
1366 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
1369 if (desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
1370 && desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
1371 && desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
1372 FIXME("Unimplemented swap effect %#x.\n", desc->swap_effect);
1374 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
1375 if (FAILED(hr = wined3d_swapchain_state_init(&swapchain->state, desc, window, device->wined3d, state_parent)))
1377 ERR("Failed to initialise swapchain state, hr %#x.\n", hr);
1378 wined3d_mutex_unlock();
1379 return hr;
1382 swapchain->swapchain_ops = swapchain_ops;
1383 swapchain->device = device;
1384 swapchain->parent = parent;
1385 swapchain->parent_ops = parent_ops;
1386 swapchain->ref = 1;
1387 swapchain->win_handle = window;
1388 swapchain->swap_interval = WINED3D_SWAP_INTERVAL_DEFAULT;
1389 swapchain_set_max_frame_latency(swapchain, device);
1391 GetClientRect(window, &client_rect);
1392 if (desc->windowed)
1394 TRACE("Client rect %s.\n", wine_dbgstr_rect(&client_rect));
1396 if (!desc->backbuffer_width)
1398 desc->backbuffer_width = client_rect.right ? client_rect.right : 8;
1399 TRACE("Updating width to %u.\n", desc->backbuffer_width);
1401 if (!desc->backbuffer_height)
1403 desc->backbuffer_height = client_rect.bottom ? client_rect.bottom : 8;
1404 TRACE("Updating height to %u.\n", desc->backbuffer_height);
1407 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
1409 desc->backbuffer_format = swapchain->state.original_mode.format_id;
1410 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->state.original_mode.format_id));
1413 else
1415 if (FAILED(hr = wined3d_output_get_desc(desc->output, &output_desc)))
1417 ERR("Failed to get output description, hr %#x.\n", hr);
1418 goto err;
1421 wined3d_swapchain_state_setup_fullscreen(&swapchain->state, window,
1422 output_desc.desktop_rect.left, output_desc.desktop_rect.top, desc->backbuffer_width,
1423 desc->backbuffer_height);
1425 swapchain->state.desc = *desc;
1426 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->state.desc.backbuffer_format,
1427 &swapchain->state.desc.multisample_type, &swapchain->state.desc.multisample_quality);
1428 swapchain_update_render_to_fbo(swapchain);
1430 TRACE("Creating front buffer.\n");
1432 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1433 texture_desc.format = swapchain->state.desc.backbuffer_format;
1434 texture_desc.multisample_type = swapchain->state.desc.multisample_type;
1435 texture_desc.multisample_quality = swapchain->state.desc.multisample_quality;
1436 texture_desc.usage = 0;
1437 if (device->wined3d->flags & WINED3D_NO3D)
1438 texture_desc.usage |= WINED3DUSAGE_OWNDC;
1439 texture_desc.bind_flags = 0;
1440 if (device->wined3d->flags & WINED3D_NO3D)
1441 texture_desc.access = WINED3D_RESOURCE_ACCESS_CPU;
1442 else
1443 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
1444 if (swapchain->state.desc.flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
1445 texture_desc.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1446 texture_desc.width = swapchain->state.desc.backbuffer_width;
1447 texture_desc.height = swapchain->state.desc.backbuffer_height;
1448 texture_desc.depth = 1;
1449 texture_desc.size = 0;
1451 if (swapchain->state.desc.flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
1452 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1454 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1455 parent, &texture_desc, texture_flags, &swapchain->front_buffer)))
1457 WARN("Failed to create front buffer, hr %#x.\n", hr);
1458 goto err;
1461 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
1462 if (!(device->wined3d->flags & WINED3D_NO3D))
1464 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
1465 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
1468 /* MSDN says we're only allowed a single fullscreen swapchain per device,
1469 * so we should really check to see if there is a fullscreen swapchain
1470 * already. Does a single head count as full screen? */
1471 if (!desc->windowed && desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1473 /* Change the display settings */
1474 if (FAILED(hr = wined3d_output_set_display_mode(desc->output,
1475 &swapchain->state.d3d_mode)))
1477 WARN("Failed to set display mode, hr %#x.\n", hr);
1478 goto err;
1480 displaymode_set = TRUE;
1483 if (swapchain->state.desc.backbuffer_count > 0)
1485 if (!(swapchain->back_buffers = heap_calloc(swapchain->state.desc.backbuffer_count,
1486 sizeof(*swapchain->back_buffers))))
1488 ERR("Failed to allocate backbuffer array memory.\n");
1489 hr = E_OUTOFMEMORY;
1490 goto err;
1493 texture_desc.bind_flags = swapchain->state.desc.backbuffer_bind_flags;
1494 texture_desc.usage = 0;
1495 if (device->wined3d->flags & WINED3D_NO3D)
1496 texture_desc.usage |= WINED3DUSAGE_OWNDC;
1497 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
1499 TRACE("Creating back buffer %u.\n", i);
1500 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1501 parent, &texture_desc, texture_flags, &swapchain->back_buffers[i])))
1503 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1504 swapchain->state.desc.backbuffer_count = i;
1505 goto err;
1507 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
1511 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1512 if (desc->enable_auto_depth_stencil)
1514 TRACE("Creating depth/stencil buffer.\n");
1515 if (!device->auto_depth_stencil_view)
1517 struct wined3d_view_desc desc;
1518 struct wined3d_texture *ds;
1520 texture_desc.format = swapchain->state.desc.auto_depth_stencil_format;
1521 texture_desc.usage = 0;
1522 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
1523 if (device->wined3d->flags & WINED3D_NO3D)
1524 texture_desc.access = WINED3D_RESOURCE_ACCESS_CPU;
1525 else
1526 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
1528 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1529 device->device_parent, &texture_desc, 0, &ds)))
1531 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
1532 goto err;
1535 desc.format_id = ds->resource.format->id;
1536 desc.flags = 0;
1537 desc.u.texture.level_idx = 0;
1538 desc.u.texture.level_count = 1;
1539 desc.u.texture.layer_idx = 0;
1540 desc.u.texture.layer_count = 1;
1541 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
1542 &device->auto_depth_stencil_view);
1543 wined3d_texture_decref(ds);
1544 if (FAILED(hr))
1546 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1547 goto err;
1552 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1554 wined3d_mutex_unlock();
1556 return WINED3D_OK;
1558 err:
1559 if (displaymode_set)
1561 if (FAILED(wined3d_restore_display_modes(device->wined3d)))
1562 ERR("Failed to restore display mode.\n");
1565 if (swapchain->back_buffers)
1567 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
1569 if (swapchain->back_buffers[i])
1571 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1572 wined3d_texture_decref(swapchain->back_buffers[i]);
1575 heap_free(swapchain->back_buffers);
1578 if (swapchain->front_buffer)
1580 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1581 wined3d_texture_decref(swapchain->front_buffer);
1584 wined3d_swapchain_state_cleanup(&swapchain->state);
1585 wined3d_mutex_unlock();
1587 return hr;
1590 HRESULT wined3d_swapchain_no3d_init(struct wined3d_swapchain *swapchain_no3d, struct wined3d_device *device,
1591 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain_state_parent *state_parent,
1592 void *parent, const struct wined3d_parent_ops *parent_ops)
1594 TRACE("swapchain_no3d %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1595 swapchain_no3d, device, desc, state_parent, parent, parent_ops);
1597 return wined3d_swapchain_init(swapchain_no3d, device, desc, state_parent, parent, parent_ops,
1598 &swapchain_no3d_ops);
1601 HRESULT wined3d_swapchain_gl_init(struct wined3d_swapchain_gl *swapchain_gl, struct wined3d_device *device,
1602 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain_state_parent *state_parent,
1603 void *parent, const struct wined3d_parent_ops *parent_ops)
1605 HRESULT hr;
1607 TRACE("swapchain_gl %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1608 swapchain_gl, device, desc, state_parent, parent, parent_ops);
1610 if (FAILED(hr = wined3d_swapchain_init(&swapchain_gl->s, device, desc, state_parent, parent,
1611 parent_ops, &swapchain_gl_ops)))
1613 /* Cleanup any context that may have been created for the swapchain. */
1614 wined3d_cs_destroy_object(device->cs, wined3d_swapchain_gl_destroy_object, swapchain_gl);
1615 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1618 return hr;
1621 HRESULT wined3d_swapchain_vk_init(struct wined3d_swapchain_vk *swapchain_vk, struct wined3d_device *device,
1622 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain_state_parent *state_parent,
1623 void *parent, const struct wined3d_parent_ops *parent_ops)
1625 HRESULT hr;
1627 TRACE("swapchain_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1628 swapchain_vk, device, desc, parent, parent_ops);
1630 if (FAILED(hr = wined3d_swapchain_init(&swapchain_vk->s, device, desc, state_parent, parent,
1631 parent_ops, &swapchain_vk_ops)))
1632 return hr;
1634 if (swapchain_vk->s.win_handle == GetDesktopWindow())
1636 WARN("Creating a desktop window swapchain.\n");
1637 return hr;
1640 if (FAILED(hr = wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk)))
1642 wined3d_swapchain_cleanup(&swapchain_vk->s);
1643 return hr;
1646 return hr;
1649 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device,
1650 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain_state_parent *state_parent,
1651 void *parent, const struct wined3d_parent_ops *parent_ops,
1652 struct wined3d_swapchain **swapchain)
1654 struct wined3d_swapchain *object;
1655 HRESULT hr;
1657 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_swapchain(device,
1658 desc, state_parent, parent, parent_ops, &object)))
1659 return hr;
1661 if (desc->flags & WINED3D_SWAPCHAIN_IMPLICIT)
1663 wined3d_mutex_lock();
1664 if (FAILED(hr = wined3d_device_set_implicit_swapchain(device, object)))
1666 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1667 device->adapter->adapter_ops->adapter_destroy_swapchain(object);
1668 wined3d_mutex_unlock();
1669 return hr;
1671 wined3d_mutex_unlock();
1674 *swapchain = object;
1676 return hr;
1679 static struct wined3d_context_gl *wined3d_swapchain_gl_create_context(struct wined3d_swapchain_gl *swapchain_gl)
1681 struct wined3d_device *device = swapchain_gl->s.device;
1682 struct wined3d_context_gl *context_gl;
1684 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain_gl, GetCurrentThreadId());
1686 wined3d_from_cs(device->cs);
1688 if (!(context_gl = heap_alloc_zero(sizeof(*context_gl))))
1690 ERR("Failed to allocate context memory.\n");
1691 return NULL;
1694 if (FAILED(wined3d_context_gl_init(context_gl, swapchain_gl)))
1696 WARN("Failed to initialise context.\n");
1697 heap_free(context_gl);
1698 return NULL;
1701 if (!device_context_add(device, &context_gl->c))
1703 ERR("Failed to add the newly created context to the context list.\n");
1704 wined3d_context_gl_destroy(context_gl);
1705 return NULL;
1708 TRACE("Created context %p.\n", context_gl);
1710 context_release(&context_gl->c);
1712 if (!wined3d_array_reserve((void **)&swapchain_gl->contexts, &swapchain_gl->contexts_size,
1713 swapchain_gl->context_count + 1, sizeof(*swapchain_gl->contexts)))
1715 ERR("Failed to allocate new context array memory.\n");
1716 wined3d_context_gl_destroy(context_gl);
1717 return NULL;
1719 swapchain_gl->contexts[swapchain_gl->context_count++] = context_gl;
1721 return context_gl;
1724 void wined3d_swapchain_gl_destroy_contexts(struct wined3d_swapchain_gl *swapchain_gl)
1726 unsigned int i;
1728 for (i = 0; i < swapchain_gl->context_count; ++i)
1730 wined3d_context_gl_destroy(swapchain_gl->contexts[i]);
1732 heap_free(swapchain_gl->contexts);
1733 swapchain_gl->contexts_size = 0;
1734 swapchain_gl->context_count = 0;
1735 swapchain_gl->contexts = NULL;
1738 struct wined3d_context_gl *wined3d_swapchain_gl_get_context(struct wined3d_swapchain_gl *swapchain_gl)
1740 DWORD tid = GetCurrentThreadId();
1741 unsigned int i;
1743 for (i = 0; i < swapchain_gl->context_count; ++i)
1745 if (swapchain_gl->contexts[i]->tid == tid)
1746 return swapchain_gl->contexts[i];
1749 /* Create a new context for the thread. */
1750 return wined3d_swapchain_gl_create_context(swapchain_gl);
1753 HDC wined3d_swapchain_gl_get_backup_dc(struct wined3d_swapchain_gl *swapchain_gl)
1755 if (!swapchain_gl->backup_dc)
1757 TRACE("Creating the backup window for swapchain %p.\n", swapchain_gl);
1759 if (!(swapchain_gl->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1760 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1762 ERR("Failed to create a window.\n");
1763 return NULL;
1766 if (!(swapchain_gl->backup_dc = GetDC(swapchain_gl->backup_wnd)))
1768 ERR("Failed to get a DC.\n");
1769 DestroyWindow(swapchain_gl->backup_wnd);
1770 swapchain_gl->backup_wnd = NULL;
1771 return NULL;
1775 return swapchain_gl->backup_dc;
1778 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1780 UINT i;
1782 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1784 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
1786 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1790 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1792 struct wined3d_device *device = swapchain->device;
1793 HWND window = swapchain->state.device_window;
1794 struct wined3d_output_desc output_desc;
1795 unsigned int screensaver_active;
1796 struct wined3d_output *output;
1797 BOOL focus_messages, filter;
1798 HRESULT hr;
1800 /* This code is not protected by the wined3d mutex, so it may run while
1801 * wined3d_device_reset is active. Testing on Windows shows that changing
1802 * focus during resets and resetting during focus change events causes
1803 * the application to crash with an invalid memory access. */
1805 if (!(focus_messages = device->wined3d->flags & WINED3D_FOCUS_MESSAGES))
1806 filter = wined3d_filter_messages(window, TRUE);
1808 if (activate)
1810 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
1811 if ((device->restore_screensaver = !!screensaver_active))
1812 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
1814 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1816 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1817 * the window but leaves the size untouched, d3d9 sets the size on an
1818 * invisible window, generates messages but doesn't change the window
1819 * properties. The implementation follows d3d9.
1821 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1822 * resume drawing after a focus loss. */
1823 output = wined3d_swapchain_get_output(swapchain);
1824 if (!output)
1826 ERR("Failed to get output from swapchain %p.\n", swapchain);
1827 return;
1830 if (SUCCEEDED(hr = wined3d_output_get_desc(output, &output_desc)))
1831 SetWindowPos(window, NULL, output_desc.desktop_rect.left,
1832 output_desc.desktop_rect.top, swapchain->state.desc.backbuffer_width,
1833 swapchain->state.desc.backbuffer_height, SWP_NOACTIVATE | SWP_NOZORDER);
1834 else
1835 ERR("Failed to get output description, hr %#x.\n", hr);
1838 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1840 output = wined3d_swapchain_get_output(swapchain);
1841 if (!output)
1843 ERR("Failed to get output from swapchain %p.\n", swapchain);
1844 return;
1847 if (FAILED(hr = wined3d_output_set_display_mode(output,
1848 &swapchain->state.d3d_mode)))
1849 ERR("Failed to set display mode, hr %#x.\n", hr);
1852 if (swapchain == device->swapchains[0])
1853 device->device_parent->ops->activate(device->device_parent, TRUE);
1855 else
1857 if (device->restore_screensaver)
1859 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
1860 device->restore_screensaver = FALSE;
1863 if (FAILED(hr = wined3d_restore_display_modes(device->wined3d)))
1864 ERR("Failed to restore display modes, hr %#x.\n", hr);
1866 swapchain->reapply_mode = TRUE;
1868 /* Some DDraw apps (Deus Ex: GOTY, and presumably all UT 1 based games) destroy the device
1869 * during window minimization. Do our housekeeping now, as the device may not exist after
1870 * the ShowWindow call.
1872 * In d3d9, the device is marked lost after the window is minimized. If we find an app
1873 * that needs this behavior (e.g. because it calls TestCooperativeLevel in the window proc)
1874 * we'll have to control this via a create flag. Note that the device and swapchain are not
1875 * safe to access after the ShowWindow call. */
1876 if (swapchain == device->swapchains[0])
1877 device->device_parent->ops->activate(device->device_parent, FALSE);
1879 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES) && IsWindowVisible(window))
1880 ShowWindow(window, SW_MINIMIZE);
1883 if (!focus_messages)
1884 wined3d_filter_messages(window, filter);
1887 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1888 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1889 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1891 struct wined3d_swapchain_desc *desc = &swapchain->state.desc;
1892 BOOL update_desc = FALSE;
1894 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1895 "multisample_type %#x, multisample_quality %#x.\n",
1896 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1897 multisample_type, multisample_quality);
1899 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1901 if (buffer_count && buffer_count != desc->backbuffer_count)
1902 FIXME("Cannot change the back buffer count yet.\n");
1904 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
1906 if (!width || !height)
1908 /* The application is requesting that either the swapchain width or
1909 * height be set to the corresponding dimension in the window's
1910 * client rect. */
1912 RECT client_rect;
1914 if (!desc->windowed)
1915 return WINED3DERR_INVALIDCALL;
1917 if (!GetClientRect(swapchain->state.device_window, &client_rect))
1919 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1920 return WINED3DERR_INVALIDCALL;
1923 if (!width)
1924 width = client_rect.right;
1926 if (!height)
1927 height = client_rect.bottom;
1930 if (width != desc->backbuffer_width || height != desc->backbuffer_height)
1932 desc->backbuffer_width = width;
1933 desc->backbuffer_height = height;
1934 update_desc = TRUE;
1937 if (format_id == WINED3DFMT_UNKNOWN)
1939 if (!desc->windowed)
1940 return WINED3DERR_INVALIDCALL;
1941 format_id = swapchain->state.original_mode.format_id;
1944 if (format_id != desc->backbuffer_format)
1946 desc->backbuffer_format = format_id;
1947 update_desc = TRUE;
1950 if (multisample_type != desc->multisample_type
1951 || multisample_quality != desc->multisample_quality)
1953 desc->multisample_type = multisample_type;
1954 desc->multisample_quality = multisample_quality;
1955 update_desc = TRUE;
1958 if (update_desc)
1960 HRESULT hr;
1961 UINT i;
1963 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, 0, desc->backbuffer_width,
1964 desc->backbuffer_height, desc->backbuffer_format,
1965 desc->multisample_type, desc->multisample_quality, NULL, 0)))
1966 return hr;
1968 for (i = 0; i < desc->backbuffer_count; ++i)
1970 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], 0, desc->backbuffer_width,
1971 desc->backbuffer_height, desc->backbuffer_format,
1972 desc->multisample_type, desc->multisample_quality, NULL, 0)))
1973 return hr;
1977 swapchain_update_render_to_fbo(swapchain);
1978 swapchain_update_draw_bindings(swapchain);
1980 return WINED3D_OK;
1983 static HRESULT wined3d_swapchain_state_set_display_mode(struct wined3d_swapchain_state *state,
1984 struct wined3d_output *output, struct wined3d_display_mode *mode)
1986 HRESULT hr;
1988 if (state->desc.flags & WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE)
1990 if (FAILED(hr = wined3d_output_find_closest_matching_mode(output, mode)))
1992 WARN("Failed to find closest matching mode, hr %#x.\n", hr);
1996 if (output != state->desc.output)
1998 if (FAILED(hr = wined3d_restore_display_modes(state->wined3d)))
2000 WARN("Failed to restore display modes, hr %#x.\n", hr);
2001 return hr;
2004 if (FAILED(hr = wined3d_output_get_display_mode(output, &state->original_mode, NULL)))
2006 WARN("Failed to get current display mode, hr %#x.\n", hr);
2007 return hr;
2011 if (FAILED(hr = wined3d_output_set_display_mode(output, mode)))
2013 WARN("Failed to set display mode, hr %#x.\n", hr);
2014 return WINED3DERR_INVALIDCALL;
2017 return WINED3D_OK;
2020 HRESULT CDECL wined3d_swapchain_state_resize_target(struct wined3d_swapchain_state *state,
2021 const struct wined3d_display_mode *mode)
2023 struct wined3d_display_mode actual_mode;
2024 struct wined3d_output_desc output_desc;
2025 RECT original_window_rect, window_rect;
2026 int x, y, width, height;
2027 HWND window;
2028 HRESULT hr;
2030 TRACE("state %p, mode %p.\n", state, mode);
2032 wined3d_mutex_lock();
2034 window = state->device_window;
2036 if (state->desc.windowed)
2038 SetRect(&window_rect, 0, 0, mode->width, mode->height);
2039 AdjustWindowRectEx(&window_rect,
2040 GetWindowLongW(window, GWL_STYLE), FALSE,
2041 GetWindowLongW(window, GWL_EXSTYLE));
2042 GetWindowRect(window, &original_window_rect);
2044 x = original_window_rect.left;
2045 y = original_window_rect.top;
2046 width = window_rect.right - window_rect.left;
2047 height = window_rect.bottom - window_rect.top;
2049 else
2051 if (state->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
2053 actual_mode = *mode;
2054 if (FAILED(hr = wined3d_swapchain_state_set_display_mode(state, state->desc.output,
2055 &actual_mode)))
2057 ERR("Failed to set display mode, hr %#x.\n", hr);
2058 wined3d_mutex_unlock();
2059 return hr;
2063 if (FAILED(hr = wined3d_output_get_desc(state->desc.output, &output_desc)))
2065 ERR("Failed to get output description, hr %#x.\n", hr);
2066 wined3d_mutex_unlock();
2067 return hr;
2070 x = output_desc.desktop_rect.left;
2071 y = output_desc.desktop_rect.top;
2072 width = output_desc.desktop_rect.right - output_desc.desktop_rect.left;
2073 height = output_desc.desktop_rect.bottom - output_desc.desktop_rect.top;
2076 wined3d_mutex_unlock();
2078 MoveWindow(window, x, y, width, height, TRUE);
2080 return WINED3D_OK;
2083 static LONG fullscreen_style(LONG style)
2085 /* Make sure the window is managed, otherwise we won't get keyboard input. */
2086 style |= WS_POPUP | WS_SYSMENU;
2087 style &= ~(WS_CAPTION | WS_THICKFRAME);
2089 return style;
2092 static LONG fullscreen_exstyle(LONG exstyle)
2094 /* Filter out window decorations. */
2095 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
2097 return exstyle;
2100 HRESULT wined3d_swapchain_state_setup_fullscreen(struct wined3d_swapchain_state *state,
2101 HWND window, int x, int y, int width, int height)
2103 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOACTIVATE;
2104 LONG style, exstyle;
2105 BOOL filter;
2107 TRACE("Setting up window %p for fullscreen mode.\n", window);
2109 if (!IsWindow(window))
2111 WARN("%p is not a valid window.\n", window);
2112 return WINED3DERR_NOTAVAILABLE;
2115 if (state->style || state->exstyle)
2117 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
2118 window, state->style, state->exstyle);
2121 if (state->desc.flags & WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES)
2122 window_pos_flags |= SWP_NOZORDER;
2123 else
2124 window_pos_flags |= SWP_SHOWWINDOW;
2126 state->style = GetWindowLongW(window, GWL_STYLE);
2127 state->exstyle = GetWindowLongW(window, GWL_EXSTYLE);
2129 style = fullscreen_style(state->style);
2130 exstyle = fullscreen_exstyle(state->exstyle);
2132 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
2133 state->style, state->exstyle, style, exstyle);
2135 filter = wined3d_filter_messages(window, TRUE);
2137 SetWindowLongW(window, GWL_STYLE, style);
2138 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
2139 SetWindowPos(window, HWND_TOPMOST, x, y, width, height, window_pos_flags);
2141 wined3d_filter_messages(window, filter);
2143 return WINED3D_OK;
2146 void wined3d_swapchain_state_restore_from_fullscreen(struct wined3d_swapchain_state *state,
2147 HWND window, const RECT *window_rect)
2149 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
2150 HWND window_pos_after = NULL;
2151 LONG style, exstyle;
2152 RECT rect = {0};
2153 BOOL filter;
2155 if (!state->style && !state->exstyle)
2156 return;
2158 if ((state->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_STATE)
2159 && !(state->desc.flags & WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES))
2161 window_pos_after = (state->exstyle & WS_EX_TOPMOST) ? HWND_TOPMOST : HWND_NOTOPMOST;
2162 window_pos_flags |= (state->style & WS_VISIBLE) ? SWP_SHOWWINDOW : SWP_HIDEWINDOW;
2163 window_pos_flags &= ~SWP_NOZORDER;
2166 style = GetWindowLongW(window, GWL_STYLE);
2167 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
2169 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
2170 * application, and we want to ignore them in the test below, since it's
2171 * not the application's fault that they changed. Additionally, we want to
2172 * preserve the current status of these flags (i.e. don't restore them) to
2173 * more closely emulate the behavior of Direct3D, which leaves these flags
2174 * alone when returning to windowed mode. */
2175 state->style ^= (state->style ^ style) & WS_VISIBLE;
2176 state->exstyle ^= (state->exstyle ^ exstyle) & WS_EX_TOPMOST;
2178 TRACE("Restoring window style of window %p to %08x, %08x.\n",
2179 window, state->style, state->exstyle);
2181 filter = wined3d_filter_messages(window, TRUE);
2183 /* Only restore the style if the application didn't modify it during the
2184 * fullscreen phase. Some applications change it before calling Reset()
2185 * when switching between windowed and fullscreen modes (HL2), some
2186 * depend on the original style (Eve Online). */
2187 if (style == fullscreen_style(state->style) && exstyle == fullscreen_exstyle(state->exstyle))
2189 SetWindowLongW(window, GWL_STYLE, state->style);
2190 SetWindowLongW(window, GWL_EXSTYLE, state->exstyle);
2193 if (window_rect)
2194 rect = *window_rect;
2195 else
2196 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
2197 SetWindowPos(window, window_pos_after, rect.left, rect.top,
2198 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
2200 wined3d_filter_messages(window, filter);
2202 /* Delete the old values. */
2203 state->style = 0;
2204 state->exstyle = 0;
2207 HRESULT CDECL wined3d_swapchain_state_set_fullscreen(struct wined3d_swapchain_state *state,
2208 const struct wined3d_swapchain_desc *swapchain_desc,
2209 const struct wined3d_display_mode *mode)
2211 struct wined3d_display_mode actual_mode;
2212 struct wined3d_output_desc output_desc;
2213 BOOL windowed = state->desc.windowed;
2214 HRESULT hr;
2216 TRACE("state %p, swapchain_desc %p, mode %p.\n", state, swapchain_desc, mode);
2218 if (state->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
2220 if (mode)
2222 actual_mode = *mode;
2223 if (FAILED(hr = wined3d_swapchain_state_set_display_mode(state, swapchain_desc->output,
2224 &actual_mode)))
2225 return hr;
2227 else
2229 if (!swapchain_desc->windowed)
2231 actual_mode.width = swapchain_desc->backbuffer_width;
2232 actual_mode.height = swapchain_desc->backbuffer_height;
2233 actual_mode.refresh_rate = swapchain_desc->refresh_rate;
2234 actual_mode.format_id = adapter_format_from_backbuffer_format(swapchain_desc->output->adapter,
2235 swapchain_desc->backbuffer_format);
2236 actual_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
2237 if (FAILED(hr = wined3d_swapchain_state_set_display_mode(state, swapchain_desc->output,
2238 &actual_mode)))
2239 return hr;
2241 else
2243 if (FAILED(hr = wined3d_restore_display_modes(state->wined3d)))
2245 WARN("Failed to restore display modes for all outputs, hr %#x.\n", hr);
2246 return hr;
2251 else
2253 if (mode)
2254 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
2256 if (FAILED(hr = wined3d_output_get_display_mode(swapchain_desc->output, &actual_mode,
2257 NULL)))
2259 ERR("Failed to get display mode, hr %#x.\n", hr);
2260 return WINED3DERR_INVALIDCALL;
2264 if (!swapchain_desc->windowed)
2266 unsigned int width = actual_mode.width;
2267 unsigned int height = actual_mode.height;
2269 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
2271 ERR("Failed to get output description, hr %#x.\n", hr);
2272 return hr;
2275 if (state->desc.windowed)
2277 /* Switch from windowed to fullscreen */
2278 if (FAILED(hr = wined3d_swapchain_state_setup_fullscreen(state, state->device_window,
2279 output_desc.desktop_rect.left, output_desc.desktop_rect.top, width, height)))
2280 return hr;
2282 else
2284 HWND window = state->device_window;
2285 BOOL filter;
2287 /* Fullscreen -> fullscreen mode change */
2288 filter = wined3d_filter_messages(window, TRUE);
2289 MoveWindow(window, output_desc.desktop_rect.left, output_desc.desktop_rect.top, width,
2290 height, TRUE);
2291 ShowWindow(window, SW_SHOW);
2292 wined3d_filter_messages(window, filter);
2294 state->d3d_mode = actual_mode;
2296 else if (!state->desc.windowed)
2298 /* Fullscreen -> windowed switch */
2299 RECT *window_rect = NULL;
2300 if (state->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
2301 window_rect = &state->original_window_rect;
2302 wined3d_swapchain_state_restore_from_fullscreen(state, state->device_window, window_rect);
2305 state->desc.output = swapchain_desc->output;
2306 state->desc.windowed = swapchain_desc->windowed;
2308 if (windowed != state->desc.windowed)
2309 state->parent->ops->windowed_state_changed(state->parent, state->desc.windowed);
2311 return WINED3D_OK;
2314 BOOL CDECL wined3d_swapchain_state_is_windowed(const struct wined3d_swapchain_state *state)
2316 TRACE("state %p.\n", state);
2318 return state->desc.windowed;
2321 void CDECL wined3d_swapchain_state_destroy(struct wined3d_swapchain_state *state)
2323 wined3d_swapchain_state_cleanup(state);
2324 heap_free(state);
2327 HRESULT CDECL wined3d_swapchain_state_create(const struct wined3d_swapchain_desc *desc,
2328 HWND window, struct wined3d *wined3d, struct wined3d_swapchain_state_parent *state_parent,
2329 struct wined3d_swapchain_state **state)
2331 struct wined3d_swapchain_state *s;
2332 HRESULT hr;
2334 TRACE("desc %p, window %p, wined3d %p, state %p.\n", desc, window, wined3d, state);
2336 if (!(s = heap_alloc_zero(sizeof(*s))))
2337 return E_OUTOFMEMORY;
2339 if (FAILED(hr = wined3d_swapchain_state_init(s, desc, window, wined3d, state_parent)))
2341 heap_free(s);
2342 return hr;
2345 *state = s;
2347 return hr;