libport: Remove support for PPC32.
[wine.git] / dlls / wined3d / swapchain.c
blobe034bdf62410cd56d7d53ebf6ae5bc04991dcfb6
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 struct wined3d_output *output;
32 HRESULT hr = E_FAIL;
33 UINT i;
35 TRACE("Destroying swapchain %p.\n", swapchain);
37 wined3d_unhook_swapchain(swapchain);
38 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
40 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
41 * is the last buffer to be destroyed, FindContext() depends on that. */
42 if (swapchain->front_buffer)
44 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
45 if (wined3d_texture_decref(swapchain->front_buffer))
46 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
47 swapchain->front_buffer = NULL;
50 if (swapchain->back_buffers)
52 i = swapchain->state.desc.backbuffer_count;
54 while (i--)
56 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
57 if (wined3d_texture_decref(swapchain->back_buffers[i]))
58 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
60 heap_free(swapchain->back_buffers);
61 swapchain->back_buffers = NULL;
64 /* Restore the screen resolution if we rendered in fullscreen.
65 * This will restore the screen resolution to what it was before creating
66 * the swapchain. In case of d3d8 and d3d9 this will be the original
67 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
68 * sets the resolution before starting up Direct3D, thus orig_width and
69 * orig_height will be equal to the modes in the presentation params. */
70 if (!swapchain->state.desc.windowed)
72 if (swapchain->state.desc.auto_restore_display_mode)
74 output = wined3d_swapchain_get_output(swapchain);
75 if (!output || FAILED(hr = wined3d_output_set_display_mode(output,
76 &swapchain->state.original_mode)))
77 ERR("Failed to restore display mode, hr %#x.\n", hr);
79 if (swapchain->state.desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
81 wined3d_swapchain_state_restore_from_fullscreen(&swapchain->state,
82 swapchain->state.device_window, &swapchain->state.original_window_rect);
83 wined3d_device_release_focus_window(swapchain->device);
86 else
88 wined3d_swapchain_state_restore_from_fullscreen(&swapchain->state, swapchain->state.device_window, NULL);
93 static void wined3d_swapchain_gl_destroy_object(void *object)
95 wined3d_swapchain_gl_destroy_contexts(object);
98 void wined3d_swapchain_gl_cleanup(struct wined3d_swapchain_gl *swapchain_gl)
100 struct wined3d_cs *cs = swapchain_gl->s.device->cs;
102 wined3d_swapchain_cleanup(&swapchain_gl->s);
104 wined3d_cs_destroy_object(cs, wined3d_swapchain_gl_destroy_object, swapchain_gl);
105 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
107 if (swapchain_gl->backup_dc)
109 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain_gl->backup_wnd, swapchain_gl->backup_dc);
111 wined3d_release_dc(swapchain_gl->backup_wnd, swapchain_gl->backup_dc);
112 DestroyWindow(swapchain_gl->backup_wnd);
116 static void wined3d_swapchain_vk_destroy_vulkan_swapchain(struct wined3d_swapchain_vk *swapchain_vk)
118 struct wined3d_device_vk *device_vk = wined3d_device_vk(swapchain_vk->s.device);
119 const struct wined3d_vk_info *vk_info;
120 unsigned int i;
121 VkResult vr;
123 TRACE("swapchain_vk %p.\n", swapchain_vk);
125 vk_info = &wined3d_adapter_vk(device_vk->d.adapter)->vk_info;
127 if ((vr = VK_CALL(vkQueueWaitIdle(device_vk->vk_queue))) < 0)
128 ERR("Failed to wait on queue, vr %s.\n", wined3d_debug_vkresult(vr));
129 heap_free(swapchain_vk->vk_images);
130 for (i = 0; i < swapchain_vk->image_count; ++i)
132 VK_CALL(vkDestroySemaphore(device_vk->vk_device, swapchain_vk->vk_semaphores[i].available, NULL));
133 VK_CALL(vkDestroySemaphore(device_vk->vk_device, swapchain_vk->vk_semaphores[i].presentable, NULL));
135 heap_free(swapchain_vk->vk_semaphores);
136 VK_CALL(vkDestroySwapchainKHR(device_vk->vk_device, swapchain_vk->vk_swapchain, NULL));
137 VK_CALL(vkDestroySurfaceKHR(vk_info->instance, swapchain_vk->vk_surface, NULL));
140 static void wined3d_swapchain_vk_destroy_object(void *object)
142 wined3d_swapchain_vk_destroy_vulkan_swapchain(object);
145 void wined3d_swapchain_vk_cleanup(struct wined3d_swapchain_vk *swapchain_vk)
147 struct wined3d_cs *cs = swapchain_vk->s.device->cs;
149 wined3d_cs_destroy_object(cs, wined3d_swapchain_vk_destroy_object, swapchain_vk);
150 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
152 wined3d_swapchain_cleanup(&swapchain_vk->s);
155 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
157 ULONG refcount = InterlockedIncrement(&swapchain->ref);
159 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
161 return refcount;
164 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
166 ULONG refcount = InterlockedDecrement(&swapchain->ref);
168 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
170 if (!refcount)
172 struct wined3d_device *device;
174 wined3d_mutex_lock();
176 device = swapchain->device;
177 if (device->swapchain_count && device->swapchains[0] == swapchain)
178 wined3d_device_uninit_3d(device);
179 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
181 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
182 swapchain->device->adapter->adapter_ops->adapter_destroy_swapchain(swapchain);
184 wined3d_mutex_unlock();
187 return refcount;
190 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
192 TRACE("swapchain %p.\n", swapchain);
194 return swapchain->parent;
197 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
199 if (!window)
200 window = swapchain->state.device_window;
201 if (window == swapchain->win_handle)
202 return;
204 TRACE("Setting swapchain %p window from %p to %p.\n",
205 swapchain, swapchain->win_handle, window);
207 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
209 swapchain->win_handle = window;
212 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
213 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
214 unsigned int swap_interval, DWORD flags)
216 RECT s, d;
218 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, swap_interval %u, flags %#x.\n",
219 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
220 dst_window_override, swap_interval, flags);
222 if (flags)
223 FIXME("Ignoring flags %#x.\n", flags);
225 wined3d_mutex_lock();
227 if (!swapchain->back_buffers)
229 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL.\n");
230 wined3d_mutex_unlock();
231 return WINED3DERR_INVALIDCALL;
234 if (!src_rect)
236 SetRect(&s, 0, 0, swapchain->state.desc.backbuffer_width,
237 swapchain->state.desc.backbuffer_height);
238 src_rect = &s;
241 if (!dst_rect)
243 GetClientRect(swapchain->win_handle, &d);
244 dst_rect = &d;
247 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
248 dst_rect, dst_window_override, swap_interval, flags);
250 wined3d_mutex_unlock();
252 return WINED3D_OK;
255 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
256 struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
258 RECT src_rect, dst_rect;
260 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
262 SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
263 dst_rect = src_rect;
265 if (swapchain->state.desc.windowed)
267 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
268 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
269 wine_dbgstr_rect(&dst_rect));
272 return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
273 swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
276 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
277 UINT back_buffer_idx)
279 TRACE("swapchain %p, back_buffer_idx %u.\n",
280 swapchain, back_buffer_idx);
282 /* Return invalid if there is no backbuffer array, otherwise it will
283 * crash when ddraw is used (there swapchain->back_buffers is always
284 * NULL). We need this because this function is called from
285 * stateblock_init_default_state() to get the default scissorrect
286 * dimensions. */
287 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->state.desc.backbuffer_count)
289 WARN("Invalid back buffer index.\n");
290 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
291 * here in wined3d to avoid problems in other libs. */
292 return NULL;
295 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
297 return swapchain->back_buffers[back_buffer_idx];
300 struct wined3d_output * wined3d_swapchain_get_output(const struct wined3d_swapchain *swapchain)
302 TRACE("swapchain %p.\n", swapchain);
304 return swapchain->state.desc.output;
307 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
308 struct wined3d_raster_status *raster_status)
310 struct wined3d_output *output;
312 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
314 output = wined3d_swapchain_get_output(swapchain);
315 if (!output)
317 ERR("Failed to get output from swapchain %p.\n", swapchain);
318 return E_FAIL;
321 return wined3d_output_get_raster_status(output, raster_status);
324 struct wined3d_swapchain_state * CDECL wined3d_swapchain_get_state(struct wined3d_swapchain *swapchain)
326 return &swapchain->state;
329 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
330 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
332 struct wined3d_output *output;
333 HRESULT hr;
335 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
337 if (!(output = wined3d_swapchain_get_output(swapchain)))
339 ERR("Failed to get output from swapchain %p.\n", swapchain);
340 return E_FAIL;
343 hr = wined3d_output_get_display_mode(output, mode, rotation);
345 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
346 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
348 return hr;
351 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
353 TRACE("swapchain %p.\n", swapchain);
355 return swapchain->device;
358 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
359 struct wined3d_swapchain_desc *desc)
361 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
363 *desc = swapchain->state.desc;
366 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
367 DWORD flags, const struct wined3d_gamma_ramp *ramp)
369 HDC dc;
371 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
373 if (flags)
374 FIXME("Ignoring flags %#x.\n", flags);
376 dc = GetDCEx(swapchain->state.device_window, 0, DCX_USESTYLE | DCX_CACHE);
377 SetDeviceGammaRamp(dc, (void *)ramp);
378 ReleaseDC(swapchain->state.device_window, dc);
380 return WINED3D_OK;
383 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
385 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
387 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
389 swapchain->palette = palette;
392 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
393 struct wined3d_gamma_ramp *ramp)
395 HDC dc;
397 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
399 dc = GetDCEx(swapchain->state.device_window, 0, DCX_USESTYLE | DCX_CACHE);
400 GetDeviceGammaRamp(dc, ramp);
401 ReleaseDC(swapchain->state.device_window, dc);
403 return WINED3D_OK;
406 /* A GL context is provided by the caller */
407 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
408 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
410 struct wined3d_texture *texture = swapchain->back_buffers[0];
411 struct wined3d_device *device = swapchain->device;
412 enum wined3d_texture_filter_type filter;
413 DWORD location;
415 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
416 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
418 if ((src_rect->right - src_rect->left == dst_rect->right - dst_rect->left
419 && src_rect->bottom - src_rect->top == dst_rect->bottom - dst_rect->top)
420 || is_complex_fixup(texture->resource.format->color_fixup))
421 filter = WINED3D_TEXF_NONE;
422 else
423 filter = WINED3D_TEXF_LINEAR;
425 location = WINED3D_LOCATION_TEXTURE_RGB;
426 if (texture->resource.multisample_type)
427 location = WINED3D_LOCATION_RB_RESOLVED;
429 wined3d_texture_validate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
430 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context, texture, 0,
431 location, src_rect, texture, 0, WINED3D_LOCATION_DRAWABLE, dst_rect, NULL, filter);
432 wined3d_texture_invalidate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
435 static void swapchain_gl_set_swap_interval(struct wined3d_swapchain *swapchain,
436 struct wined3d_context_gl *context_gl, unsigned int swap_interval)
438 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
440 swap_interval = swap_interval <= 4 ? swap_interval : 1;
441 if (swapchain->swap_interval == swap_interval)
442 return;
444 swapchain->swap_interval = swap_interval;
446 if (!gl_info->supported[WGL_EXT_SWAP_CONTROL])
447 return;
449 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
451 ERR("Failed to set swap interval %u for context %p, last error %#x.\n",
452 swap_interval, context_gl, GetLastError());
456 /* Context activation is done by the caller. */
457 static void wined3d_swapchain_gl_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context *context)
459 struct wined3d_texture_sub_resource *sub_resource;
460 struct wined3d_texture_gl *texture, *texture_prev;
461 struct gl_texture tex0;
462 GLuint rb0;
463 DWORD locations0;
464 unsigned int i;
465 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
467 if (swapchain->state.desc.backbuffer_count < 2 || !swapchain->render_to_fbo)
468 return;
470 texture_prev = wined3d_texture_gl(swapchain->back_buffers[0]);
472 /* Back buffer 0 is already in the draw binding. */
473 tex0 = texture_prev->texture_rgb;
474 rb0 = texture_prev->rb_multisample;
475 locations0 = texture_prev->t.sub_resources[0].locations;
477 for (i = 1; i < swapchain->state.desc.backbuffer_count; ++i)
479 texture = wined3d_texture_gl(swapchain->back_buffers[i]);
480 sub_resource = &texture->t.sub_resources[0];
482 if (!(sub_resource->locations & supported_locations))
483 wined3d_texture_load_location(&texture->t, 0, context, texture->t.resource.draw_binding);
485 texture_prev->texture_rgb = texture->texture_rgb;
486 texture_prev->rb_multisample = texture->rb_multisample;
488 wined3d_texture_validate_location(&texture_prev->t, 0, sub_resource->locations & supported_locations);
489 wined3d_texture_invalidate_location(&texture_prev->t, 0, ~(sub_resource->locations & supported_locations));
491 texture_prev = texture;
494 texture_prev->texture_rgb = tex0;
495 texture_prev->rb_multisample = rb0;
497 wined3d_texture_validate_location(&texture_prev->t, 0, locations0 & supported_locations);
498 wined3d_texture_invalidate_location(&texture_prev->t, 0, ~(locations0 & supported_locations));
500 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
503 static void swapchain_gl_present(struct wined3d_swapchain *swapchain,
504 const RECT *src_rect, const RECT *dst_rect, unsigned int swap_interval, DWORD flags)
506 struct wined3d_swapchain_gl *swapchain_gl = wined3d_swapchain_gl(swapchain);
507 const struct wined3d_swapchain_desc *desc = &swapchain->state.desc;
508 struct wined3d_texture *back_buffer = swapchain->back_buffers[0];
509 const struct wined3d_gl_info *gl_info;
510 struct wined3d_context_gl *context_gl;
511 struct wined3d_context *context;
512 BOOL render_to_fbo;
514 context = context_acquire(swapchain->device, swapchain->front_buffer, 0);
515 context_gl = wined3d_context_gl(context);
516 if (!context_gl->valid)
518 context_release(context);
519 WARN("Invalid context, skipping present.\n");
520 return;
523 gl_info = context_gl->gl_info;
525 swapchain_gl_set_swap_interval(swapchain, context_gl, swap_interval);
527 TRACE("Presenting DC %p.\n", context_gl->dc);
529 if (!(render_to_fbo = swapchain->render_to_fbo)
530 && (src_rect->left || src_rect->top
531 || src_rect->right != desc->backbuffer_width
532 || src_rect->bottom != desc->backbuffer_height
533 || dst_rect->left || dst_rect->top
534 || dst_rect->right != desc->backbuffer_width
535 || dst_rect->bottom != desc->backbuffer_height))
536 render_to_fbo = TRUE;
538 /* Rendering to a window of different size, presenting partial rectangles,
539 * or rendering to a different window needs help from FBO_blit or a textured
540 * draw. Render the swapchain to a FBO in the future.
542 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
543 * all these issues - this fails if the window is smaller than the backbuffer.
545 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
547 wined3d_texture_load_location(back_buffer, 0, context, WINED3D_LOCATION_TEXTURE_RGB);
548 wined3d_texture_invalidate_location(back_buffer, 0, WINED3D_LOCATION_DRAWABLE);
549 swapchain->render_to_fbo = TRUE;
550 swapchain_update_draw_bindings(swapchain);
552 else
554 wined3d_texture_load_location(back_buffer, 0, context, back_buffer->resource.draw_binding);
557 if (swapchain->render_to_fbo)
558 swapchain_blit(swapchain, context, src_rect, dst_rect);
560 if (swapchain_gl->context_count > 1)
561 gl_info->gl_ops.gl.p_glFinish();
563 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
564 gl_info->gl_ops.wgl.p_wglSwapBuffers(context_gl->dc);
566 wined3d_swapchain_gl_rotate(swapchain, context);
568 TRACE("SwapBuffers called, Starting new frame\n");
570 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
571 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
573 context_release(context);
576 static void swapchain_frontbuffer_updated(struct wined3d_swapchain *swapchain)
578 struct wined3d_texture *front_buffer = swapchain->front_buffer;
579 struct wined3d_context *context;
581 context = context_acquire(swapchain->device, front_buffer, 0);
582 wined3d_texture_load_location(front_buffer, 0, context, front_buffer->resource.draw_binding);
583 context_release(context);
584 SetRectEmpty(&swapchain->front_buffer_update);
587 static const struct wined3d_swapchain_ops swapchain_gl_ops =
589 swapchain_gl_present,
590 swapchain_frontbuffer_updated,
593 static bool wined3d_swapchain_vk_present_mode_supported(struct wined3d_swapchain_vk *swapchain_vk,
594 VkPresentModeKHR vk_present_mode)
596 struct wined3d_device_vk *device_vk = wined3d_device_vk(swapchain_vk->s.device);
597 const struct wined3d_vk_info *vk_info;
598 struct wined3d_adapter_vk *adapter_vk;
599 VkPhysicalDevice vk_physical_device;
600 VkPresentModeKHR *vk_modes;
601 bool supported = false;
602 uint32_t count, i;
603 VkResult vr;
605 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
606 vk_physical_device = adapter_vk->physical_device;
607 vk_info = &adapter_vk->vk_info;
609 if ((vr = VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device,
610 swapchain_vk->vk_surface, &count, NULL))) < 0)
612 ERR("Failed to get supported present mode count, vr %s.\n", wined3d_debug_vkresult(vr));
613 return false;
616 if (!(vk_modes = heap_calloc(count, sizeof(*vk_modes))))
617 return false;
619 if ((vr = VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device,
620 swapchain_vk->vk_surface, &count, vk_modes))) < 0)
622 ERR("Failed to get supported present modes, vr %s.\n", wined3d_debug_vkresult(vr));
623 goto done;
626 for (i = 0; i < count; ++i)
628 if (vk_modes[i] == vk_present_mode)
630 supported = true;
631 goto done;
635 done:
636 heap_free(vk_modes);
637 return supported;
640 static VkFormat wined3d_swapchain_vk_select_vk_format(struct wined3d_swapchain_vk *swapchain_vk,
641 VkSurfaceKHR vk_surface)
643 struct wined3d_device_vk *device_vk = wined3d_device_vk(swapchain_vk->s.device);
644 const struct wined3d_swapchain_desc *desc = &swapchain_vk->s.state.desc;
645 const struct wined3d_vk_info *vk_info;
646 struct wined3d_adapter_vk *adapter_vk;
647 const struct wined3d_format *format;
648 VkPhysicalDevice vk_physical_device;
649 VkSurfaceFormatKHR *vk_formats;
650 uint32_t format_count, i;
651 VkFormat vk_format;
652 VkResult vr;
654 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
655 vk_physical_device = adapter_vk->physical_device;
656 vk_info = &adapter_vk->vk_info;
658 if ((format = wined3d_get_format(&adapter_vk->a, desc->backbuffer_format, WINED3D_BIND_RENDER_TARGET)))
659 vk_format = wined3d_format_vk(format)->vk_format;
660 else
661 vk_format = VK_FORMAT_B8G8R8A8_UNORM;
663 vr = VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device, vk_surface, &format_count, NULL));
664 if (vr < 0 || !format_count)
666 WARN("Failed to get supported surface format count, vr %s.\n", wined3d_debug_vkresult(vr));
667 return VK_FORMAT_UNDEFINED;
670 if (!(vk_formats = heap_calloc(format_count, sizeof(*vk_formats))))
671 return VK_FORMAT_UNDEFINED;
673 if ((vr = VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device,
674 vk_surface, &format_count, vk_formats))) < 0)
676 WARN("Failed to get supported surface formats, vr %s.\n", wined3d_debug_vkresult(vr));
677 heap_free(vk_formats);
678 return VK_FORMAT_UNDEFINED;
681 for (i = 0; i < format_count; ++i)
683 if (vk_formats[i].format == vk_format && vk_formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
684 break;
686 if (i == format_count)
688 /* Try to create a swapchain with format conversion. */
689 vk_format = VK_FORMAT_B8G8R8A8_UNORM;
690 WARN("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc->backbuffer_format));
691 for (i = 0; i < format_count; ++i)
693 if (vk_formats[i].format == vk_format && vk_formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
694 break;
697 heap_free(vk_formats);
698 if (i == format_count)
700 FIXME("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc->backbuffer_format));
701 return VK_FORMAT_UNDEFINED;
704 TRACE("Using Vulkan swapchain format %#x.\n", vk_format);
706 return vk_format;
709 static bool wined3d_swapchain_vk_create_vulkan_swapchain_images(struct wined3d_swapchain_vk *swapchain_vk,
710 VkSwapchainKHR vk_swapchain)
712 struct wined3d_device_vk *device_vk = wined3d_device_vk(swapchain_vk->s.device);
713 const struct wined3d_vk_info *vk_info;
714 VkSemaphoreCreateInfo semaphore_info;
715 uint32_t image_count, i;
716 VkResult vr;
718 vk_info = &wined3d_adapter_vk(device_vk->d.adapter)->vk_info;
720 if ((vr = VK_CALL(vkGetSwapchainImagesKHR(device_vk->vk_device, vk_swapchain, &image_count, NULL))) < 0)
722 ERR("Failed to get image count, vr %s\n", wined3d_debug_vkresult(vr));
723 return false;
726 if (!(swapchain_vk->vk_images = heap_calloc(image_count, sizeof(*swapchain_vk->vk_images))))
728 ERR("Failed to allocate images array.\n");
729 return false;
732 if ((vr = VK_CALL(vkGetSwapchainImagesKHR(device_vk->vk_device,
733 vk_swapchain, &image_count, swapchain_vk->vk_images))) < 0)
735 ERR("Failed to get swapchain images, vr %s.\n", wined3d_debug_vkresult(vr));
736 heap_free(swapchain_vk->vk_images);
737 return false;
740 if (!(swapchain_vk->vk_semaphores = heap_calloc(image_count, sizeof(*swapchain_vk->vk_semaphores))))
742 ERR("Failed to allocate semaphores array.\n");
743 heap_free(swapchain_vk->vk_images);
744 return false;
747 semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
748 semaphore_info.pNext = NULL;
749 semaphore_info.flags = 0;
750 for (i = 0; i < image_count; ++i)
752 if ((vr = VK_CALL(vkCreateSemaphore(device_vk->vk_device,
753 &semaphore_info, NULL, &swapchain_vk->vk_semaphores[i].available))) < 0)
755 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr));
756 goto fail;
759 if ((vr = VK_CALL(vkCreateSemaphore(device_vk->vk_device,
760 &semaphore_info, NULL, &swapchain_vk->vk_semaphores[i].presentable))) < 0)
762 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr));
763 goto fail;
766 swapchain_vk->image_count = image_count;
768 return true;
770 fail:
771 for (i = 0; i < image_count; ++i)
773 if (swapchain_vk->vk_semaphores[i].available)
774 VK_CALL(vkDestroySemaphore(device_vk->vk_device, swapchain_vk->vk_semaphores[i].available, NULL));
775 if (swapchain_vk->vk_semaphores[i].presentable)
776 VK_CALL(vkDestroySemaphore(device_vk->vk_device, swapchain_vk->vk_semaphores[i].presentable, NULL));
778 heap_free(swapchain_vk->vk_semaphores);
779 heap_free(swapchain_vk->vk_images);
780 return false;
783 static HRESULT wined3d_swapchain_vk_create_vulkan_swapchain(struct wined3d_swapchain_vk *swapchain_vk)
785 struct wined3d_device_vk *device_vk = wined3d_device_vk(swapchain_vk->s.device);
786 const struct wined3d_swapchain_desc *desc = &swapchain_vk->s.state.desc;
787 VkSwapchainCreateInfoKHR vk_swapchain_desc;
788 VkWin32SurfaceCreateInfoKHR surface_desc;
789 unsigned int width, height, image_count;
790 const struct wined3d_vk_info *vk_info;
791 VkSurfaceCapabilitiesKHR surface_caps;
792 struct wined3d_adapter_vk *adapter_vk;
793 VkPresentModeKHR vk_present_mode;
794 VkSwapchainKHR vk_swapchain;
795 VkImageUsageFlags usage;
796 VkSurfaceKHR vk_surface;
797 VkBool32 supported;
798 VkFormat vk_format;
799 VkResult vr;
801 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
802 vk_info = &adapter_vk->vk_info;
804 surface_desc.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
805 surface_desc.pNext = NULL;
806 surface_desc.flags = 0;
807 surface_desc.hinstance = (HINSTANCE)GetWindowLongPtrW(swapchain_vk->s.win_handle, GWLP_HINSTANCE);
808 surface_desc.hwnd = swapchain_vk->s.win_handle;
809 if ((vr = VK_CALL(vkCreateWin32SurfaceKHR(vk_info->instance, &surface_desc, NULL, &vk_surface))) < 0)
811 ERR("Failed to create Vulkan surface, vr %s.\n", wined3d_debug_vkresult(vr));
812 return E_FAIL;
814 swapchain_vk->vk_surface = vk_surface;
816 if ((vr = VK_CALL(vkGetPhysicalDeviceSurfaceSupportKHR(adapter_vk->physical_device,
817 device_vk->vk_queue_family_index, vk_surface, &supported))) < 0 || !supported)
819 ERR("Queue family does not support presentation on this surface, vr %s.\n", wined3d_debug_vkresult(vr));
820 goto fail;
823 if ((vk_format = wined3d_swapchain_vk_select_vk_format(swapchain_vk, vk_surface)) == VK_FORMAT_UNDEFINED)
825 ERR("Failed to select swapchain format.\n");
826 goto fail;
829 if ((vr = VK_CALL(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(adapter_vk->physical_device,
830 swapchain_vk->vk_surface, &surface_caps))) < 0)
832 ERR("Failed to get surface capabilities, vr %s.\n", wined3d_debug_vkresult(vr));
833 goto fail;
836 image_count = desc->backbuffer_count;
837 if (image_count < surface_caps.minImageCount)
838 image_count = surface_caps.minImageCount;
839 else if (surface_caps.maxImageCount && image_count > surface_caps.maxImageCount)
840 image_count = surface_caps.maxImageCount;
842 if (image_count != desc->backbuffer_count)
843 WARN("Image count %u is not supported (%u-%u).\n", desc->backbuffer_count,
844 surface_caps.minImageCount, surface_caps.maxImageCount);
846 width = desc->backbuffer_width;
847 if (width < surface_caps.minImageExtent.width)
848 width = surface_caps.minImageExtent.width;
849 else if (width > surface_caps.maxImageExtent.width)
850 width = surface_caps.maxImageExtent.width;
852 height = desc->backbuffer_height;
853 if (height < surface_caps.minImageExtent.height)
854 height = surface_caps.minImageExtent.height;
855 else if (height > surface_caps.maxImageExtent.height)
856 height = surface_caps.maxImageExtent.height;
858 if (width != desc->backbuffer_width || height != desc->backbuffer_height)
859 WARN("Swapchain dimensions %ux%u are not supported (%u-%u x %u-%u).\n",
860 desc->backbuffer_width, desc->backbuffer_height,
861 surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width,
862 surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height);
864 usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
865 usage |= surface_caps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
866 usage |= surface_caps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT;
867 if (!(usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) || !(usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT))
868 WARN("Transfer not supported for swapchain images.\n");
870 if (!(surface_caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR))
872 FIXME("Unsupported alpha mode, %#x.\n", surface_caps.supportedCompositeAlpha);
873 goto fail;
876 vk_present_mode = VK_PRESENT_MODE_FIFO_KHR;
877 if (!swapchain_vk->s.swap_interval)
879 if (wined3d_swapchain_vk_present_mode_supported(swapchain_vk, VK_PRESENT_MODE_IMMEDIATE_KHR))
880 vk_present_mode = VK_PRESENT_MODE_IMMEDIATE_KHR;
881 else
882 FIXME("Unsupported swap interval %u.\n", swapchain_vk->s.swap_interval);
885 vk_swapchain_desc.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
886 vk_swapchain_desc.pNext = NULL;
887 vk_swapchain_desc.flags = 0;
888 vk_swapchain_desc.surface = vk_surface;
889 vk_swapchain_desc.minImageCount = image_count;
890 vk_swapchain_desc.imageFormat = vk_format;
891 vk_swapchain_desc.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
892 vk_swapchain_desc.imageExtent.width = width;
893 vk_swapchain_desc.imageExtent.height = height;
894 vk_swapchain_desc.imageArrayLayers = 1;
895 vk_swapchain_desc.imageUsage = usage;
896 vk_swapchain_desc.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
897 vk_swapchain_desc.queueFamilyIndexCount = 0;
898 vk_swapchain_desc.pQueueFamilyIndices = NULL;
899 vk_swapchain_desc.preTransform = surface_caps.currentTransform;
900 vk_swapchain_desc.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
901 vk_swapchain_desc.presentMode = vk_present_mode;
902 vk_swapchain_desc.clipped = VK_TRUE;
903 vk_swapchain_desc.oldSwapchain = VK_NULL_HANDLE;
904 if ((vr = VK_CALL(vkCreateSwapchainKHR(device_vk->vk_device, &vk_swapchain_desc, NULL, &vk_swapchain))) < 0)
906 ERR("Failed to create Vulkan swapchain, vr %s.\n", wined3d_debug_vkresult(vr));
907 goto fail;
909 swapchain_vk->vk_swapchain = vk_swapchain;
911 if (!wined3d_swapchain_vk_create_vulkan_swapchain_images(swapchain_vk, vk_swapchain))
913 VK_CALL(vkDestroySwapchainKHR(device_vk->vk_device, vk_swapchain, NULL));
914 goto fail;
917 return WINED3D_OK;
919 fail:
920 VK_CALL(vkDestroySurfaceKHR(vk_info->instance, vk_surface, NULL));
921 return E_FAIL;
924 static HRESULT wined3d_swapchain_vk_recreate(struct wined3d_swapchain_vk *swapchain_vk)
926 TRACE("swapchain_vk %p.\n", swapchain_vk);
928 wined3d_swapchain_vk_destroy_vulkan_swapchain(swapchain_vk);
930 return wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk);
933 static void wined3d_swapchain_vk_set_swap_interval(struct wined3d_swapchain_vk *swapchain_vk,
934 unsigned int swap_interval)
936 if (swap_interval > 1)
938 if (swap_interval <= 4)
939 FIXME("Unsupported swap interval %u.\n", swap_interval);
940 swap_interval = 1;
943 if (swapchain_vk->s.swap_interval == swap_interval)
944 return;
946 swapchain_vk->s.swap_interval = swap_interval;
947 wined3d_swapchain_vk_recreate(swapchain_vk);
950 static void wined3d_swapchain_vk_blit(struct wined3d_swapchain_vk *swapchain_vk,
951 struct wined3d_context_vk *context_vk, const RECT *src_rect, const RECT *dst_rect, unsigned int swap_interval)
953 struct wined3d_texture_vk *back_buffer_vk = wined3d_texture_vk(swapchain_vk->s.back_buffers[0]);
954 struct wined3d_device_vk *device_vk = wined3d_device_vk(swapchain_vk->s.device);
955 const struct wined3d_swapchain_desc *desc = &swapchain_vk->s.state.desc;
956 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
957 VkCommandBuffer vk_command_buffer;
958 VkPresentInfoKHR present_desc;
959 unsigned int present_idx;
960 VkImageLayout vk_layout;
961 uint32_t image_idx;
962 VkImageBlit blit;
963 VkResult vr;
964 HRESULT hr;
966 static const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
968 wined3d_swapchain_vk_set_swap_interval(swapchain_vk, swap_interval);
970 present_idx = swapchain_vk->current++ % swapchain_vk->image_count;
971 wined3d_context_vk_wait_command_buffer(context_vk, swapchain_vk->vk_semaphores[present_idx].command_buffer_id);
972 vr = VK_CALL(vkAcquireNextImageKHR(device_vk->vk_device, swapchain_vk->vk_swapchain, UINT64_MAX,
973 swapchain_vk->vk_semaphores[present_idx].available, VK_NULL_HANDLE, &image_idx));
974 if (vr == VK_ERROR_OUT_OF_DATE_KHR)
976 if (FAILED(hr = wined3d_swapchain_vk_recreate(swapchain_vk)))
978 ERR("Failed to recreate swapchain, hr %#x.\n", hr);
979 return;
981 vr = VK_CALL(vkAcquireNextImageKHR(device_vk->vk_device, swapchain_vk->vk_swapchain, UINT64_MAX,
982 swapchain_vk->vk_semaphores[present_idx].available, VK_NULL_HANDLE, &image_idx));
984 if (vr < 0)
986 ERR("Failed to acquire next Vulkan image, vr %s.\n", wined3d_debug_vkresult(vr));
987 return;
990 vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk);
992 wined3d_context_vk_end_current_render_pass(context_vk);
994 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
995 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
996 vk_access_mask_from_bind_flags(back_buffer_vk->t.resource.bind_flags),
997 VK_ACCESS_TRANSFER_READ_BIT,
998 back_buffer_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
999 back_buffer_vk->vk_image, VK_IMAGE_ASPECT_COLOR_BIT);
1001 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1002 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1003 0, VK_ACCESS_TRANSFER_WRITE_BIT,
1004 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1005 swapchain_vk->vk_images[image_idx], VK_IMAGE_ASPECT_COLOR_BIT);
1007 blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1008 blit.srcSubresource.mipLevel = 0;
1009 blit.srcSubresource.baseArrayLayer = 0;
1010 blit.srcSubresource.layerCount = 1;
1011 blit.srcOffsets[0].x = src_rect->left;
1012 blit.srcOffsets[0].y = src_rect->top;
1013 blit.srcOffsets[0].z = 0;
1014 blit.srcOffsets[1].x = src_rect->right;
1015 blit.srcOffsets[1].y = src_rect->bottom;
1016 blit.srcOffsets[1].z = 1;
1017 blit.dstSubresource = blit.srcSubresource;
1018 blit.dstOffsets[0].x = dst_rect->left;
1019 blit.dstOffsets[0].y = dst_rect->top;
1020 blit.dstOffsets[0].z = 0;
1021 blit.dstOffsets[1].x = dst_rect->right;
1022 blit.dstOffsets[1].y = dst_rect->bottom;
1023 blit.dstOffsets[1].z = 1;
1024 VK_CALL(vkCmdBlitImage(vk_command_buffer,
1025 back_buffer_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1026 swapchain_vk->vk_images[image_idx], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1027 1, &blit, VK_FILTER_NEAREST));
1029 wined3d_context_vk_reference_texture(context_vk, back_buffer_vk);
1030 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1031 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1032 VK_ACCESS_TRANSFER_WRITE_BIT, 0,
1033 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1034 swapchain_vk->vk_images[image_idx], VK_IMAGE_ASPECT_COLOR_BIT);
1036 if (desc->swap_effect == WINED3D_SWAP_EFFECT_DISCARD || desc->swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
1037 vk_layout = VK_IMAGE_LAYOUT_UNDEFINED;
1038 else
1039 vk_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
1040 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1041 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
1042 VK_ACCESS_TRANSFER_READ_BIT,
1043 vk_access_mask_from_bind_flags(back_buffer_vk->t.resource.bind_flags),
1044 vk_layout, back_buffer_vk->layout,
1045 back_buffer_vk->vk_image, VK_IMAGE_ASPECT_COLOR_BIT);
1047 swapchain_vk->vk_semaphores[present_idx].command_buffer_id = context_vk->current_command_buffer.id;
1048 wined3d_context_vk_submit_command_buffer(context_vk,
1049 1, &swapchain_vk->vk_semaphores[present_idx].available, &wait_stage,
1050 1, &swapchain_vk->vk_semaphores[present_idx].presentable);
1052 present_desc.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
1053 present_desc.pNext = NULL;
1054 present_desc.waitSemaphoreCount = 1;
1055 present_desc.pWaitSemaphores = &swapchain_vk->vk_semaphores[present_idx].presentable;
1056 present_desc.swapchainCount = 1;
1057 present_desc.pSwapchains = &swapchain_vk->vk_swapchain;
1058 present_desc.pImageIndices = &image_idx;
1059 present_desc.pResults = NULL;
1060 if ((vr = VK_CALL(vkQueuePresentKHR(device_vk->vk_queue, &present_desc))))
1061 ERR("Present returned vr %s.\n", wined3d_debug_vkresult(vr));
1064 static void wined3d_swapchain_vk_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context_vk *context_vk)
1066 struct wined3d_texture_sub_resource *sub_resource;
1067 struct wined3d_texture_vk *texture, *texture_prev;
1068 struct wined3d_allocator_block *memory0;
1069 VkDescriptorImageInfo vk_info0;
1070 VkDeviceMemory vk_memory0;
1071 VkImageLayout vk_layout0;
1072 VkImage vk_image0;
1073 DWORD locations0;
1074 unsigned int i;
1075 uint64_t id0;
1077 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
1079 if (swapchain->state.desc.backbuffer_count < 2)
1080 return;
1082 texture_prev = wined3d_texture_vk(swapchain->back_buffers[0]);
1084 /* Back buffer 0 is already in the draw binding. */
1085 vk_image0 = texture_prev->vk_image;
1086 memory0 = texture_prev->memory;
1087 vk_memory0 = texture_prev->vk_memory;
1088 vk_layout0 = texture_prev->layout;
1089 id0 = texture_prev->command_buffer_id;
1090 vk_info0 = texture_prev->default_image_info;
1091 locations0 = texture_prev->t.sub_resources[0].locations;
1093 for (i = 1; i < swapchain->state.desc.backbuffer_count; ++i)
1095 texture = wined3d_texture_vk(swapchain->back_buffers[i]);
1096 sub_resource = &texture->t.sub_resources[0];
1098 if (!(sub_resource->locations & supported_locations))
1099 wined3d_texture_load_location(&texture->t, 0, &context_vk->c, texture->t.resource.draw_binding);
1101 texture_prev->vk_image = texture->vk_image;
1102 texture_prev->memory = texture->memory;
1103 texture_prev->vk_memory = texture->vk_memory;
1104 texture_prev->layout = texture->layout;
1105 texture_prev->command_buffer_id = texture->command_buffer_id;
1106 texture_prev->default_image_info = texture->default_image_info;
1108 wined3d_texture_validate_location(&texture_prev->t, 0, sub_resource->locations & supported_locations);
1109 wined3d_texture_invalidate_location(&texture_prev->t, 0, ~(sub_resource->locations & supported_locations));
1111 texture_prev = texture;
1114 texture_prev->vk_image = vk_image0;
1115 texture_prev->memory = memory0;
1116 texture_prev->vk_memory = vk_memory0;
1117 texture_prev->layout = vk_layout0;
1118 texture_prev->command_buffer_id = id0;
1119 texture_prev->default_image_info = vk_info0;
1121 wined3d_texture_validate_location(&texture_prev->t, 0, locations0 & supported_locations);
1122 wined3d_texture_invalidate_location(&texture_prev->t, 0, ~(locations0 & supported_locations));
1124 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
1127 static void swapchain_vk_present(struct wined3d_swapchain *swapchain, const RECT *src_rect,
1128 const RECT *dst_rect, unsigned int swap_interval, uint32_t flags)
1130 struct wined3d_swapchain_vk *swapchain_vk = wined3d_swapchain_vk(swapchain);
1131 struct wined3d_texture *back_buffer = swapchain->back_buffers[0];
1132 struct wined3d_context_vk *context_vk;
1134 context_vk = wined3d_context_vk(context_acquire(swapchain->device, back_buffer, 0));
1136 wined3d_texture_load_location(back_buffer, 0, &context_vk->c, back_buffer->resource.draw_binding);
1138 if (swapchain_vk->vk_swapchain)
1139 wined3d_swapchain_vk_blit(swapchain_vk, context_vk, src_rect, dst_rect, swap_interval);
1141 wined3d_swapchain_vk_rotate(swapchain, context_vk);
1143 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
1144 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
1146 TRACE("Starting new frame.\n");
1148 context_release(&context_vk->c);
1151 static const struct wined3d_swapchain_ops swapchain_vk_ops =
1153 swapchain_vk_present,
1154 swapchain_frontbuffer_updated,
1157 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
1159 struct wined3d_dc_info *front;
1160 POINT offset = {0, 0};
1161 HDC src_dc, dst_dc;
1162 RECT draw_rect;
1163 HWND window;
1165 TRACE("swapchain %p.\n", swapchain);
1167 front = &swapchain->front_buffer->dc_info[0];
1168 if (swapchain->palette)
1169 wined3d_palette_apply_to_dc(swapchain->palette, front->dc);
1171 if (swapchain->front_buffer->resource.map_count)
1172 ERR("Trying to blit a mapped surface.\n");
1174 TRACE("Copying surface %p to screen.\n", front);
1176 src_dc = front->dc;
1177 window = swapchain->win_handle;
1178 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
1180 /* Front buffer coordinates are screen coordinates. Map them to the
1181 * destination window if not fullscreened. */
1182 if (swapchain->state.desc.windowed)
1183 ClientToScreen(window, &offset);
1185 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
1187 SetRect(&draw_rect, 0, 0, swapchain->front_buffer->resource.width,
1188 swapchain->front_buffer->resource.height);
1189 IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
1191 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
1192 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
1193 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
1194 ReleaseDC(window, dst_dc);
1196 SetRectEmpty(&swapchain->front_buffer_update);
1199 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
1200 const RECT *src_rect, const RECT *dst_rect, unsigned int swap_interval, DWORD flags)
1202 struct wined3d_dc_info *front, *back;
1203 HBITMAP bitmap;
1204 void *data;
1205 HDC dc;
1207 front = &swapchain->front_buffer->dc_info[0];
1208 back = &swapchain->back_buffers[0]->dc_info[0];
1210 /* Flip the surface data. */
1211 dc = front->dc;
1212 bitmap = front->bitmap;
1213 data = swapchain->front_buffer->resource.heap_memory;
1215 front->dc = back->dc;
1216 front->bitmap = back->bitmap;
1217 swapchain->front_buffer->resource.heap_memory = swapchain->back_buffers[0]->resource.heap_memory;
1219 back->dc = dc;
1220 back->bitmap = bitmap;
1221 swapchain->back_buffers[0]->resource.heap_memory = data;
1223 SetRect(&swapchain->front_buffer_update, 0, 0,
1224 swapchain->front_buffer->resource.width,
1225 swapchain->front_buffer->resource.height);
1226 swapchain_gdi_frontbuffer_updated(swapchain);
1229 static const struct wined3d_swapchain_ops swapchain_no3d_ops =
1231 swapchain_gdi_present,
1232 swapchain_gdi_frontbuffer_updated,
1235 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
1237 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
1238 return;
1240 if (!swapchain->state.desc.backbuffer_count)
1242 TRACE("Single buffered rendering.\n");
1243 swapchain->render_to_fbo = FALSE;
1244 return;
1247 TRACE("Rendering to FBO.\n");
1248 swapchain->render_to_fbo = TRUE;
1251 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
1252 enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
1254 const struct wined3d_adapter *adapter;
1255 const struct wined3d_gl_info *gl_info;
1256 const struct wined3d_format *format;
1257 enum wined3d_multisample_type t;
1259 if (wined3d_settings.sample_count == ~0u)
1260 return;
1262 adapter = swapchain->device->adapter;
1263 gl_info = &adapter->gl_info;
1264 if (!(format = wined3d_get_format(adapter, format_id, WINED3D_BIND_RENDER_TARGET)))
1265 return;
1267 if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
1268 while (!(format->multisample_types & 1u << (t - 1)))
1269 ++t;
1270 TRACE("Using sample count %u.\n", t);
1271 *type = t;
1272 *quality = 0;
1275 void swapchain_set_max_frame_latency(struct wined3d_swapchain *swapchain, const struct wined3d_device *device)
1277 /* Subtract 1 for the implicit OpenGL latency. */
1278 swapchain->max_frame_latency = device->max_frame_latency >= 2 ? device->max_frame_latency - 1 : 1;
1281 static enum wined3d_format_id adapter_format_from_backbuffer_format(const struct wined3d_adapter *adapter,
1282 enum wined3d_format_id format_id)
1284 const struct wined3d_format *backbuffer_format;
1286 backbuffer_format = wined3d_get_format(adapter, format_id, WINED3D_BIND_RENDER_TARGET);
1287 return pixelformat_for_depth(backbuffer_format->byte_count * CHAR_BIT);
1290 static HRESULT wined3d_swapchain_state_init(struct wined3d_swapchain_state *state,
1291 const struct wined3d_swapchain_desc *desc, HWND window)
1293 HRESULT hr;
1295 state->desc = *desc;
1297 if (FAILED(hr = wined3d_output_get_display_mode(desc->output, &state->original_mode, NULL)))
1299 ERR("Failed to get current display mode, hr %#x.\n", hr);
1300 return hr;
1303 if (!desc->windowed)
1305 if (desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1307 state->d3d_mode.width = desc->backbuffer_width;
1308 state->d3d_mode.height = desc->backbuffer_height;
1309 state->d3d_mode.format_id = adapter_format_from_backbuffer_format(desc->output->adapter,
1310 desc->backbuffer_format);
1311 state->d3d_mode.refresh_rate = desc->refresh_rate;
1312 state->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1314 else
1316 state->d3d_mode = state->original_mode;
1320 GetWindowRect(window, &state->original_window_rect);
1321 state->device_window = window;
1323 return hr;
1326 static HRESULT wined3d_swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
1327 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops,
1328 const struct wined3d_swapchain_ops *swapchain_ops)
1330 struct wined3d_resource_desc texture_desc;
1331 struct wined3d_output_desc output_desc;
1332 BOOL displaymode_set = FALSE;
1333 DWORD texture_flags = 0;
1334 HRESULT hr = E_FAIL;
1335 RECT client_rect;
1336 unsigned int i;
1337 HWND window;
1339 wined3d_mutex_lock();
1341 if (desc->backbuffer_count > 1)
1343 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
1344 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
1347 if (desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
1348 && desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
1349 && desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
1350 FIXME("Unimplemented swap effect %#x.\n", desc->swap_effect);
1352 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
1353 if (FAILED(hr = wined3d_swapchain_state_init(&swapchain->state, desc, window)))
1355 ERR("Failed to initialise swapchain state, hr %#x.\n", hr);
1356 goto err;
1359 swapchain->swapchain_ops = swapchain_ops;
1360 swapchain->device = device;
1361 swapchain->parent = parent;
1362 swapchain->parent_ops = parent_ops;
1363 swapchain->ref = 1;
1364 swapchain->win_handle = window;
1365 swapchain->swap_interval = WINED3D_SWAP_INTERVAL_DEFAULT;
1366 swapchain_set_max_frame_latency(swapchain, device);
1368 GetClientRect(window, &client_rect);
1369 if (desc->windowed)
1371 TRACE("Client rect %s.\n", wine_dbgstr_rect(&client_rect));
1373 if (!desc->backbuffer_width)
1375 desc->backbuffer_width = client_rect.right ? client_rect.right : 8;
1376 TRACE("Updating width to %u.\n", desc->backbuffer_width);
1378 if (!desc->backbuffer_height)
1380 desc->backbuffer_height = client_rect.bottom ? client_rect.bottom : 8;
1381 TRACE("Updating height to %u.\n", desc->backbuffer_height);
1384 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
1386 desc->backbuffer_format = swapchain->state.original_mode.format_id;
1387 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->state.original_mode.format_id));
1390 else
1392 if (FAILED(hr = wined3d_output_get_desc(desc->output, &output_desc)))
1394 ERR("Failed to get output description, hr %#x.\n", hr);
1395 goto err;
1398 wined3d_swapchain_state_setup_fullscreen(&swapchain->state, window,
1399 output_desc.desktop_rect.left, output_desc.desktop_rect.top, desc->backbuffer_width,
1400 desc->backbuffer_height);
1402 swapchain->state.desc = *desc;
1403 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->state.desc.backbuffer_format,
1404 &swapchain->state.desc.multisample_type, &swapchain->state.desc.multisample_quality);
1405 swapchain_update_render_to_fbo(swapchain);
1407 TRACE("Creating front buffer.\n");
1409 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1410 texture_desc.format = swapchain->state.desc.backbuffer_format;
1411 texture_desc.multisample_type = swapchain->state.desc.multisample_type;
1412 texture_desc.multisample_quality = swapchain->state.desc.multisample_quality;
1413 texture_desc.usage = 0;
1414 if (device->wined3d->flags & WINED3D_NO3D)
1415 texture_desc.usage |= WINED3DUSAGE_OWNDC;
1416 texture_desc.bind_flags = 0;
1417 if (device->wined3d->flags & WINED3D_NO3D)
1418 texture_desc.access = WINED3D_RESOURCE_ACCESS_CPU;
1419 else
1420 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
1421 if (swapchain->state.desc.flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
1422 texture_desc.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1423 texture_desc.width = swapchain->state.desc.backbuffer_width;
1424 texture_desc.height = swapchain->state.desc.backbuffer_height;
1425 texture_desc.depth = 1;
1426 texture_desc.size = 0;
1428 if (swapchain->state.desc.flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
1429 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1431 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1432 parent, &texture_desc, texture_flags, &swapchain->front_buffer)))
1434 WARN("Failed to create front buffer, hr %#x.\n", hr);
1435 goto err;
1438 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
1439 if (!(device->wined3d->flags & WINED3D_NO3D))
1441 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
1442 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
1445 /* MSDN says we're only allowed a single fullscreen swapchain per device,
1446 * so we should really check to see if there is a fullscreen swapchain
1447 * already. Does a single head count as full screen? */
1448 if (!desc->windowed && desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1450 /* Change the display settings */
1451 if (FAILED(hr = wined3d_output_set_display_mode(desc->output,
1452 &swapchain->state.d3d_mode)))
1454 WARN("Failed to set display mode, hr %#x.\n", hr);
1455 goto err;
1457 displaymode_set = TRUE;
1460 if (swapchain->state.desc.backbuffer_count > 0)
1462 if (!(swapchain->back_buffers = heap_calloc(swapchain->state.desc.backbuffer_count,
1463 sizeof(*swapchain->back_buffers))))
1465 ERR("Failed to allocate backbuffer array memory.\n");
1466 hr = E_OUTOFMEMORY;
1467 goto err;
1470 texture_desc.bind_flags = swapchain->state.desc.backbuffer_bind_flags;
1471 texture_desc.usage = 0;
1472 if (device->wined3d->flags & WINED3D_NO3D)
1473 texture_desc.usage |= WINED3DUSAGE_OWNDC;
1474 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
1476 TRACE("Creating back buffer %u.\n", i);
1477 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1478 parent, &texture_desc, texture_flags, &swapchain->back_buffers[i])))
1480 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1481 swapchain->state.desc.backbuffer_count = i;
1482 goto err;
1484 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
1488 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1489 if (desc->enable_auto_depth_stencil)
1491 TRACE("Creating depth/stencil buffer.\n");
1492 if (!device->auto_depth_stencil_view)
1494 struct wined3d_view_desc desc;
1495 struct wined3d_texture *ds;
1497 texture_desc.format = swapchain->state.desc.auto_depth_stencil_format;
1498 texture_desc.usage = 0;
1499 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
1500 if (device->wined3d->flags & WINED3D_NO3D)
1501 texture_desc.access = WINED3D_RESOURCE_ACCESS_CPU;
1502 else
1503 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
1505 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1506 device->device_parent, &texture_desc, 0, &ds)))
1508 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
1509 goto err;
1512 desc.format_id = ds->resource.format->id;
1513 desc.flags = 0;
1514 desc.u.texture.level_idx = 0;
1515 desc.u.texture.level_count = 1;
1516 desc.u.texture.layer_idx = 0;
1517 desc.u.texture.layer_count = 1;
1518 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
1519 &device->auto_depth_stencil_view);
1520 wined3d_texture_decref(ds);
1521 if (FAILED(hr))
1523 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1524 goto err;
1529 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1531 wined3d_mutex_unlock();
1533 return WINED3D_OK;
1535 err:
1536 if (displaymode_set)
1538 if (FAILED(wined3d_output_set_display_mode(desc->output,
1539 &swapchain->state.original_mode)))
1540 ERR("Failed to restore display mode.\n");
1543 if (swapchain->back_buffers)
1545 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
1547 if (swapchain->back_buffers[i])
1549 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1550 wined3d_texture_decref(swapchain->back_buffers[i]);
1553 heap_free(swapchain->back_buffers);
1556 if (swapchain->front_buffer)
1558 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1559 wined3d_texture_decref(swapchain->front_buffer);
1562 wined3d_mutex_unlock();
1564 return hr;
1567 HRESULT wined3d_swapchain_no3d_init(struct wined3d_swapchain *swapchain_no3d, struct wined3d_device *device,
1568 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
1570 TRACE("swapchain_no3d %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1571 swapchain_no3d, device, desc, parent, parent_ops);
1573 return wined3d_swapchain_init(swapchain_no3d, device, desc, parent, parent_ops, &swapchain_no3d_ops);
1576 HRESULT wined3d_swapchain_gl_init(struct wined3d_swapchain_gl *swapchain_gl, struct wined3d_device *device,
1577 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
1579 HRESULT hr;
1581 TRACE("swapchain_gl %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1582 swapchain_gl, device, desc, parent, parent_ops);
1584 if (FAILED(hr = wined3d_swapchain_init(&swapchain_gl->s, device, desc, parent, parent_ops, &swapchain_gl_ops)))
1586 /* Cleanup any context that may have been created for the swapchain. */
1587 wined3d_cs_destroy_object(device->cs, wined3d_swapchain_gl_destroy_object, swapchain_gl);
1588 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1591 return hr;
1594 HRESULT wined3d_swapchain_vk_init(struct wined3d_swapchain_vk *swapchain_vk, struct wined3d_device *device,
1595 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
1597 HRESULT hr;
1599 TRACE("swapchain_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1600 swapchain_vk, device, desc, parent, parent_ops);
1602 if (FAILED(hr = wined3d_swapchain_init(&swapchain_vk->s, device, desc, parent, parent_ops, &swapchain_vk_ops)))
1603 return hr;
1605 if (swapchain_vk->s.win_handle == GetDesktopWindow())
1607 WARN("Creating a desktop window swapchain.\n");
1608 return hr;
1611 if (FAILED(hr = wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk)))
1613 wined3d_swapchain_cleanup(&swapchain_vk->s);
1614 return hr;
1617 return hr;
1620 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1621 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1623 struct wined3d_swapchain *object;
1624 HRESULT hr;
1626 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_swapchain(device,
1627 desc, parent, parent_ops, &object)))
1628 return hr;
1630 if (desc->flags & WINED3D_SWAPCHAIN_HOOK)
1631 wined3d_hook_swapchain(object);
1633 if (desc->flags & WINED3D_SWAPCHAIN_IMPLICIT)
1635 wined3d_mutex_lock();
1636 if (FAILED(hr = wined3d_device_set_implicit_swapchain(device, object)))
1638 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1639 device->adapter->adapter_ops->adapter_destroy_swapchain(object);
1640 wined3d_mutex_unlock();
1641 return hr;
1643 wined3d_mutex_unlock();
1646 *swapchain = object;
1648 return hr;
1651 static struct wined3d_context_gl *wined3d_swapchain_gl_create_context(struct wined3d_swapchain_gl *swapchain_gl)
1653 struct wined3d_device *device = swapchain_gl->s.device;
1654 struct wined3d_context_gl *context_gl;
1656 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain_gl, GetCurrentThreadId());
1658 wined3d_from_cs(device->cs);
1660 if (!(context_gl = heap_alloc_zero(sizeof(*context_gl))))
1662 ERR("Failed to allocate context memory.\n");
1663 return NULL;
1666 if (FAILED(wined3d_context_gl_init(context_gl, swapchain_gl)))
1668 WARN("Failed to initialise context.\n");
1669 heap_free(context_gl);
1670 return NULL;
1673 if (!device_context_add(device, &context_gl->c))
1675 ERR("Failed to add the newly created context to the context list.\n");
1676 wined3d_context_gl_destroy(context_gl);
1677 return NULL;
1680 TRACE("Created context %p.\n", context_gl);
1682 context_release(&context_gl->c);
1684 if (!wined3d_array_reserve((void **)&swapchain_gl->contexts, &swapchain_gl->contexts_size,
1685 swapchain_gl->context_count + 1, sizeof(*swapchain_gl->contexts)))
1687 ERR("Failed to allocate new context array memory.\n");
1688 wined3d_context_gl_destroy(context_gl);
1689 return NULL;
1691 swapchain_gl->contexts[swapchain_gl->context_count++] = context_gl;
1693 return context_gl;
1696 void wined3d_swapchain_gl_destroy_contexts(struct wined3d_swapchain_gl *swapchain_gl)
1698 unsigned int i;
1700 for (i = 0; i < swapchain_gl->context_count; ++i)
1702 wined3d_context_gl_destroy(swapchain_gl->contexts[i]);
1704 heap_free(swapchain_gl->contexts);
1705 swapchain_gl->contexts_size = 0;
1706 swapchain_gl->context_count = 0;
1707 swapchain_gl->contexts = NULL;
1710 struct wined3d_context_gl *wined3d_swapchain_gl_get_context(struct wined3d_swapchain_gl *swapchain_gl)
1712 DWORD tid = GetCurrentThreadId();
1713 unsigned int i;
1715 for (i = 0; i < swapchain_gl->context_count; ++i)
1717 if (swapchain_gl->contexts[i]->tid == tid)
1718 return swapchain_gl->contexts[i];
1721 /* Create a new context for the thread. */
1722 return wined3d_swapchain_gl_create_context(swapchain_gl);
1725 HDC wined3d_swapchain_gl_get_backup_dc(struct wined3d_swapchain_gl *swapchain_gl)
1727 if (!swapchain_gl->backup_dc)
1729 TRACE("Creating the backup window for swapchain %p.\n", swapchain_gl);
1731 if (!(swapchain_gl->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1732 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1734 ERR("Failed to create a window.\n");
1735 return NULL;
1738 if (!(swapchain_gl->backup_dc = GetDC(swapchain_gl->backup_wnd)))
1740 ERR("Failed to get a DC.\n");
1741 DestroyWindow(swapchain_gl->backup_wnd);
1742 swapchain_gl->backup_wnd = NULL;
1743 return NULL;
1747 return swapchain_gl->backup_dc;
1750 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1752 UINT i;
1754 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1756 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
1758 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1762 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1764 struct wined3d_device *device = swapchain->device;
1765 HWND window = swapchain->state.device_window;
1766 struct wined3d_output_desc output_desc;
1767 unsigned int screensaver_active;
1768 struct wined3d_output *output;
1769 BOOL focus_messages, filter;
1770 HRESULT hr;
1772 /* This code is not protected by the wined3d mutex, so it may run while
1773 * wined3d_device_reset is active. Testing on Windows shows that changing
1774 * focus during resets and resetting during focus change events causes
1775 * the application to crash with an invalid memory access. */
1777 if (!(focus_messages = device->wined3d->flags & WINED3D_FOCUS_MESSAGES))
1778 filter = wined3d_filter_messages(window, TRUE);
1780 output = wined3d_swapchain_get_output(swapchain);
1781 if (!output)
1783 ERR("Failed to get output from swapchain %p.\n", swapchain);
1784 return;
1787 if (activate)
1789 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
1790 if ((device->restore_screensaver = !!screensaver_active))
1791 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
1793 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1795 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1796 * the window but leaves the size untouched, d3d9 sets the size on an
1797 * invisible window, generates messages but doesn't change the window
1798 * properties. The implementation follows d3d9.
1800 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1801 * resume drawing after a focus loss. */
1802 if (SUCCEEDED(hr = wined3d_output_get_desc(output, &output_desc)))
1803 SetWindowPos(window, NULL, output_desc.desktop_rect.left,
1804 output_desc.desktop_rect.top, swapchain->state.desc.backbuffer_width,
1805 swapchain->state.desc.backbuffer_height, SWP_NOACTIVATE | SWP_NOZORDER);
1806 else
1807 ERR("Failed to get output description, hr %#x.\n", hr);
1810 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1812 if (FAILED(hr = wined3d_output_set_display_mode(output,
1813 &swapchain->state.d3d_mode)))
1814 ERR("Failed to set display mode, hr %#x.\n", hr);
1817 if (swapchain == device->swapchains[0])
1818 device->device_parent->ops->activate(device->device_parent, TRUE);
1820 else
1822 if (device->restore_screensaver)
1824 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
1825 device->restore_screensaver = FALSE;
1828 if (FAILED(hr = wined3d_output_set_display_mode(output, NULL)))
1829 ERR("Failed to set display mode, hr %#x.\n", hr);
1831 swapchain->reapply_mode = TRUE;
1833 /* Some DDraw apps (Deus Ex: GOTY, and presumably all UT 1 based games) destroy the device
1834 * during window minimization. Do our housekeeping now, as the device may not exist after
1835 * the ShowWindow call.
1837 * In d3d9, the device is marked lost after the window is minimized. If we find an app
1838 * that needs this behavior (e.g. because it calls TestCooperativeLevel in the window proc)
1839 * we'll have to control this via a create flag. Note that the device and swapchain are not
1840 * safe to access after the ShowWindow call. */
1841 if (swapchain == device->swapchains[0])
1842 device->device_parent->ops->activate(device->device_parent, FALSE);
1844 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES) && IsWindowVisible(window))
1845 ShowWindow(window, SW_MINIMIZE);
1848 if (!focus_messages)
1849 wined3d_filter_messages(window, filter);
1852 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1853 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1854 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1856 struct wined3d_swapchain_desc *desc = &swapchain->state.desc;
1857 BOOL update_desc = FALSE;
1859 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1860 "multisample_type %#x, multisample_quality %#x.\n",
1861 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1862 multisample_type, multisample_quality);
1864 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1866 if (buffer_count && buffer_count != desc->backbuffer_count)
1867 FIXME("Cannot change the back buffer count yet.\n");
1869 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
1871 if (!width || !height)
1873 /* The application is requesting that either the swapchain width or
1874 * height be set to the corresponding dimension in the window's
1875 * client rect. */
1877 RECT client_rect;
1879 if (!desc->windowed)
1880 return WINED3DERR_INVALIDCALL;
1882 if (!GetClientRect(swapchain->state.device_window, &client_rect))
1884 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1885 return WINED3DERR_INVALIDCALL;
1888 if (!width)
1889 width = client_rect.right;
1891 if (!height)
1892 height = client_rect.bottom;
1895 if (width != desc->backbuffer_width || height != desc->backbuffer_height)
1897 desc->backbuffer_width = width;
1898 desc->backbuffer_height = height;
1899 update_desc = TRUE;
1902 if (format_id == WINED3DFMT_UNKNOWN)
1904 if (!desc->windowed)
1905 return WINED3DERR_INVALIDCALL;
1906 format_id = swapchain->state.original_mode.format_id;
1909 if (format_id != desc->backbuffer_format)
1911 desc->backbuffer_format = format_id;
1912 update_desc = TRUE;
1915 if (multisample_type != desc->multisample_type
1916 || multisample_quality != desc->multisample_quality)
1918 desc->multisample_type = multisample_type;
1919 desc->multisample_quality = multisample_quality;
1920 update_desc = TRUE;
1923 if (update_desc)
1925 HRESULT hr;
1926 UINT i;
1928 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, desc->backbuffer_width,
1929 desc->backbuffer_height, desc->backbuffer_format,
1930 desc->multisample_type, desc->multisample_quality, NULL, 0)))
1931 return hr;
1933 for (i = 0; i < desc->backbuffer_count; ++i)
1935 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], desc->backbuffer_width,
1936 desc->backbuffer_height, desc->backbuffer_format,
1937 desc->multisample_type, desc->multisample_quality, NULL, 0)))
1938 return hr;
1942 swapchain_update_render_to_fbo(swapchain);
1943 swapchain_update_draw_bindings(swapchain);
1945 return WINED3D_OK;
1948 static HRESULT wined3d_swapchain_state_set_display_mode(struct wined3d_swapchain_state *state,
1949 struct wined3d_output *output, struct wined3d_display_mode *mode)
1951 HRESULT hr;
1953 if (state->desc.flags & WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE)
1955 if (FAILED(hr = wined3d_output_find_closest_matching_mode(output, mode)))
1957 WARN("Failed to find closest matching mode, hr %#x.\n", hr);
1961 if (output != state->desc.output)
1963 if (FAILED(hr = wined3d_output_set_display_mode(state->desc.output, &state->original_mode)))
1965 WARN("Failed to set display mode, hr %#x.\n", hr);
1966 return hr;
1969 if (FAILED(hr = wined3d_output_get_display_mode(output, &state->original_mode, NULL)))
1971 WARN("Failed to get current display mode, hr %#x.\n", hr);
1972 return hr;
1976 if (FAILED(hr = wined3d_output_set_display_mode(output, mode)))
1978 WARN("Failed to set display mode, hr %#x.\n", hr);
1979 return WINED3DERR_INVALIDCALL;
1982 return WINED3D_OK;
1985 HRESULT CDECL wined3d_swapchain_state_resize_target(struct wined3d_swapchain_state *state,
1986 const struct wined3d_display_mode *mode)
1988 struct wined3d_display_mode actual_mode;
1989 struct wined3d_output_desc output_desc;
1990 RECT original_window_rect, window_rect;
1991 int x, y, width, height;
1992 HWND window;
1993 HRESULT hr;
1995 TRACE("state %p, mode %p.\n", state, mode);
1997 wined3d_mutex_lock();
1999 window = state->device_window;
2001 if (state->desc.windowed)
2003 SetRect(&window_rect, 0, 0, mode->width, mode->height);
2004 AdjustWindowRectEx(&window_rect,
2005 GetWindowLongW(window, GWL_STYLE), FALSE,
2006 GetWindowLongW(window, GWL_EXSTYLE));
2007 GetWindowRect(window, &original_window_rect);
2009 x = original_window_rect.left;
2010 y = original_window_rect.top;
2011 width = window_rect.right - window_rect.left;
2012 height = window_rect.bottom - window_rect.top;
2014 else
2016 if (state->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
2018 actual_mode = *mode;
2019 if (FAILED(hr = wined3d_swapchain_state_set_display_mode(state, state->desc.output,
2020 &actual_mode)))
2022 ERR("Failed to set display mode, hr %#x.\n", hr);
2023 wined3d_mutex_unlock();
2024 return hr;
2028 if (FAILED(hr = wined3d_output_get_desc(state->desc.output, &output_desc)))
2030 ERR("Failed to get output description, hr %#x.\n", hr);
2031 wined3d_mutex_unlock();
2032 return hr;
2035 x = output_desc.desktop_rect.left;
2036 y = output_desc.desktop_rect.top;
2037 width = output_desc.desktop_rect.right - output_desc.desktop_rect.left;
2038 height = output_desc.desktop_rect.bottom - output_desc.desktop_rect.top;
2041 wined3d_mutex_unlock();
2043 MoveWindow(window, x, y, width, height, TRUE);
2045 return WINED3D_OK;
2048 static LONG fullscreen_style(LONG style)
2050 /* Make sure the window is managed, otherwise we won't get keyboard input. */
2051 style |= WS_POPUP | WS_SYSMENU;
2052 style &= ~(WS_CAPTION | WS_THICKFRAME);
2054 return style;
2057 static LONG fullscreen_exstyle(LONG exstyle)
2059 /* Filter out window decorations. */
2060 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
2062 return exstyle;
2065 HRESULT wined3d_swapchain_state_setup_fullscreen(struct wined3d_swapchain_state *state,
2066 HWND window, int x, int y, int width, int height)
2068 LONG style, exstyle;
2069 BOOL filter;
2071 TRACE("Setting up window %p for fullscreen mode.\n", window);
2073 if (!IsWindow(window))
2075 WARN("%p is not a valid window.\n", window);
2076 return WINED3DERR_NOTAVAILABLE;
2079 if (state->style || state->exstyle)
2081 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
2082 window, state->style, state->exstyle);
2085 state->style = GetWindowLongW(window, GWL_STYLE);
2086 state->exstyle = GetWindowLongW(window, GWL_EXSTYLE);
2088 style = fullscreen_style(state->style);
2089 exstyle = fullscreen_exstyle(state->exstyle);
2091 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
2092 state->style, state->exstyle, style, exstyle);
2094 filter = wined3d_filter_messages(window, TRUE);
2096 SetWindowLongW(window, GWL_STYLE, style);
2097 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
2098 SetWindowPos(window, HWND_TOPMOST, x, y, width, height,
2099 SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
2101 wined3d_filter_messages(window, filter);
2103 return WINED3D_OK;
2106 void wined3d_swapchain_state_restore_from_fullscreen(struct wined3d_swapchain_state *state,
2107 HWND window, const RECT *window_rect)
2109 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
2110 LONG style, exstyle;
2111 RECT rect = {0};
2112 BOOL filter;
2114 if (!state->style && !state->exstyle)
2115 return;
2117 style = GetWindowLongW(window, GWL_STYLE);
2118 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
2120 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
2121 * application, and we want to ignore them in the test below, since it's
2122 * not the application's fault that they changed. Additionally, we want to
2123 * preserve the current status of these flags (i.e. don't restore them) to
2124 * more closely emulate the behavior of Direct3D, which leaves these flags
2125 * alone when returning to windowed mode. */
2126 state->style ^= (state->style ^ style) & WS_VISIBLE;
2127 state->exstyle ^= (state->exstyle ^ exstyle) & WS_EX_TOPMOST;
2129 TRACE("Restoring window style of window %p to %08x, %08x.\n",
2130 window, state->style, state->exstyle);
2132 filter = wined3d_filter_messages(window, TRUE);
2134 /* Only restore the style if the application didn't modify it during the
2135 * fullscreen phase. Some applications change it before calling Reset()
2136 * when switching between windowed and fullscreen modes (HL2), some
2137 * depend on the original style (Eve Online). */
2138 if (style == fullscreen_style(state->style) && exstyle == fullscreen_exstyle(state->exstyle))
2140 SetWindowLongW(window, GWL_STYLE, state->style);
2141 SetWindowLongW(window, GWL_EXSTYLE, state->exstyle);
2144 if (window_rect)
2145 rect = *window_rect;
2146 else
2147 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
2148 SetWindowPos(window, 0, rect.left, rect.top,
2149 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
2151 wined3d_filter_messages(window, filter);
2153 /* Delete the old values. */
2154 state->style = 0;
2155 state->exstyle = 0;
2158 HRESULT CDECL wined3d_swapchain_state_set_fullscreen(struct wined3d_swapchain_state *state,
2159 const struct wined3d_swapchain_desc *swapchain_desc,
2160 const struct wined3d_display_mode *mode)
2162 struct wined3d_display_mode actual_mode;
2163 struct wined3d_output_desc output_desc;
2164 HRESULT hr;
2166 TRACE("state %p, swapchain_desc %p, mode %p.\n", state, swapchain_desc, mode);
2168 if (state->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
2170 if (mode)
2172 actual_mode = *mode;
2174 else
2176 if (!swapchain_desc->windowed)
2178 actual_mode.width = swapchain_desc->backbuffer_width;
2179 actual_mode.height = swapchain_desc->backbuffer_height;
2180 actual_mode.refresh_rate = swapchain_desc->refresh_rate;
2181 actual_mode.format_id = adapter_format_from_backbuffer_format(swapchain_desc->output->adapter,
2182 swapchain_desc->backbuffer_format);
2183 actual_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
2185 else
2187 actual_mode = state->original_mode;
2191 if (FAILED(hr = wined3d_swapchain_state_set_display_mode(state, swapchain_desc->output,
2192 &actual_mode)))
2193 return hr;
2195 else
2197 if (mode)
2198 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
2200 if (FAILED(hr = wined3d_output_get_display_mode(swapchain_desc->output, &actual_mode,
2201 NULL)))
2203 ERR("Failed to get display mode, hr %#x.\n", hr);
2204 return WINED3DERR_INVALIDCALL;
2208 if (!swapchain_desc->windowed)
2210 unsigned int width = actual_mode.width;
2211 unsigned int height = actual_mode.height;
2213 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
2215 ERR("Failed to get output description, hr %#x.\n", hr);
2216 return hr;
2219 if (state->desc.windowed)
2221 /* Switch from windowed to fullscreen */
2222 if (FAILED(hr = wined3d_swapchain_state_setup_fullscreen(state, state->device_window,
2223 output_desc.desktop_rect.left, output_desc.desktop_rect.top, width, height)))
2224 return hr;
2226 else
2228 HWND window = state->device_window;
2229 BOOL filter;
2231 /* Fullscreen -> fullscreen mode change */
2232 filter = wined3d_filter_messages(window, TRUE);
2233 MoveWindow(window, output_desc.desktop_rect.left, output_desc.desktop_rect.top, width,
2234 height, TRUE);
2235 ShowWindow(window, SW_SHOW);
2236 wined3d_filter_messages(window, filter);
2238 state->d3d_mode = actual_mode;
2240 else if (!state->desc.windowed)
2242 /* Fullscreen -> windowed switch */
2243 RECT *window_rect = NULL;
2244 if (state->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
2245 window_rect = &state->original_window_rect;
2246 wined3d_swapchain_state_restore_from_fullscreen(state, state->device_window, window_rect);
2249 state->desc.output = swapchain_desc->output;
2250 state->desc.windowed = swapchain_desc->windowed;
2252 return WINED3D_OK;
2255 void CDECL wined3d_swapchain_state_destroy(struct wined3d_swapchain_state *state)
2257 heap_free(state);
2260 HRESULT CDECL wined3d_swapchain_state_create(const struct wined3d_swapchain_desc *desc,
2261 HWND window, struct wined3d_swapchain_state **state)
2263 struct wined3d_swapchain_state *s;
2264 HRESULT hr;
2266 TRACE("desc %p, window %p, state %p.\n", desc, window, state);
2268 if (!(s = heap_alloc_zero(sizeof(*s))))
2269 return E_OUTOFMEMORY;
2271 if (FAILED(hr = wined3d_swapchain_state_init(s, desc, window)))
2273 heap_free(s);
2274 return hr;
2277 *state = s;
2279 return hr;