wined3d: Store a struct wined3d pointer in struct wined3d_swapchain_state.
[wine.git] / dlls / wined3d / swapchain.c
blob847907c6d4cb067db8040fe965d36b71b0421d9f
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_swapchain_state_cleanup(&swapchain->state);
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, struct wined3d *wined3d,
1292 struct wined3d_swapchain_state_parent *parent)
1294 HRESULT hr;
1296 state->desc = *desc;
1298 if (FAILED(hr = wined3d_output_get_display_mode(desc->output, &state->original_mode, NULL)))
1300 ERR("Failed to get current display mode, hr %#x.\n", hr);
1301 return hr;
1304 if (!desc->windowed)
1306 if (desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1308 state->d3d_mode.width = desc->backbuffer_width;
1309 state->d3d_mode.height = desc->backbuffer_height;
1310 state->d3d_mode.format_id = adapter_format_from_backbuffer_format(desc->output->adapter,
1311 desc->backbuffer_format);
1312 state->d3d_mode.refresh_rate = desc->refresh_rate;
1313 state->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1315 else
1317 state->d3d_mode = state->original_mode;
1321 GetWindowRect(window, &state->original_window_rect);
1322 state->wined3d = wined3d;
1323 state->device_window = window;
1324 state->parent = parent;
1326 if (desc->flags & WINED3D_SWAPCHAIN_REGISTER_STATE)
1327 wined3d_swapchain_state_register(state);
1329 return hr;
1332 static HRESULT wined3d_swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
1333 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain_state_parent *state_parent,
1334 void *parent, const struct wined3d_parent_ops *parent_ops,
1335 const struct wined3d_swapchain_ops *swapchain_ops)
1337 struct wined3d_resource_desc texture_desc;
1338 struct wined3d_output_desc output_desc;
1339 BOOL displaymode_set = FALSE;
1340 DWORD texture_flags = 0;
1341 HRESULT hr = E_FAIL;
1342 RECT client_rect;
1343 unsigned int i;
1344 HWND window;
1346 wined3d_mutex_lock();
1348 if (desc->backbuffer_count > 1)
1350 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
1351 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
1354 if (desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
1355 && desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
1356 && desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
1357 FIXME("Unimplemented swap effect %#x.\n", desc->swap_effect);
1359 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
1360 if (FAILED(hr = wined3d_swapchain_state_init(&swapchain->state, desc, window, device->wined3d, state_parent)))
1362 ERR("Failed to initialise swapchain state, hr %#x.\n", hr);
1363 wined3d_mutex_unlock();
1364 return hr;
1367 swapchain->swapchain_ops = swapchain_ops;
1368 swapchain->device = device;
1369 swapchain->parent = parent;
1370 swapchain->parent_ops = parent_ops;
1371 swapchain->ref = 1;
1372 swapchain->win_handle = window;
1373 swapchain->swap_interval = WINED3D_SWAP_INTERVAL_DEFAULT;
1374 swapchain_set_max_frame_latency(swapchain, device);
1376 GetClientRect(window, &client_rect);
1377 if (desc->windowed)
1379 TRACE("Client rect %s.\n", wine_dbgstr_rect(&client_rect));
1381 if (!desc->backbuffer_width)
1383 desc->backbuffer_width = client_rect.right ? client_rect.right : 8;
1384 TRACE("Updating width to %u.\n", desc->backbuffer_width);
1386 if (!desc->backbuffer_height)
1388 desc->backbuffer_height = client_rect.bottom ? client_rect.bottom : 8;
1389 TRACE("Updating height to %u.\n", desc->backbuffer_height);
1392 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
1394 desc->backbuffer_format = swapchain->state.original_mode.format_id;
1395 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->state.original_mode.format_id));
1398 else
1400 if (FAILED(hr = wined3d_output_get_desc(desc->output, &output_desc)))
1402 ERR("Failed to get output description, hr %#x.\n", hr);
1403 goto err;
1406 wined3d_swapchain_state_setup_fullscreen(&swapchain->state, window,
1407 output_desc.desktop_rect.left, output_desc.desktop_rect.top, desc->backbuffer_width,
1408 desc->backbuffer_height);
1410 swapchain->state.desc = *desc;
1411 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->state.desc.backbuffer_format,
1412 &swapchain->state.desc.multisample_type, &swapchain->state.desc.multisample_quality);
1413 swapchain_update_render_to_fbo(swapchain);
1415 TRACE("Creating front buffer.\n");
1417 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1418 texture_desc.format = swapchain->state.desc.backbuffer_format;
1419 texture_desc.multisample_type = swapchain->state.desc.multisample_type;
1420 texture_desc.multisample_quality = swapchain->state.desc.multisample_quality;
1421 texture_desc.usage = 0;
1422 if (device->wined3d->flags & WINED3D_NO3D)
1423 texture_desc.usage |= WINED3DUSAGE_OWNDC;
1424 texture_desc.bind_flags = 0;
1425 if (device->wined3d->flags & WINED3D_NO3D)
1426 texture_desc.access = WINED3D_RESOURCE_ACCESS_CPU;
1427 else
1428 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
1429 if (swapchain->state.desc.flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
1430 texture_desc.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1431 texture_desc.width = swapchain->state.desc.backbuffer_width;
1432 texture_desc.height = swapchain->state.desc.backbuffer_height;
1433 texture_desc.depth = 1;
1434 texture_desc.size = 0;
1436 if (swapchain->state.desc.flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
1437 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1439 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1440 parent, &texture_desc, texture_flags, &swapchain->front_buffer)))
1442 WARN("Failed to create front buffer, hr %#x.\n", hr);
1443 goto err;
1446 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
1447 if (!(device->wined3d->flags & WINED3D_NO3D))
1449 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
1450 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
1453 /* MSDN says we're only allowed a single fullscreen swapchain per device,
1454 * so we should really check to see if there is a fullscreen swapchain
1455 * already. Does a single head count as full screen? */
1456 if (!desc->windowed && desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1458 /* Change the display settings */
1459 if (FAILED(hr = wined3d_output_set_display_mode(desc->output,
1460 &swapchain->state.d3d_mode)))
1462 WARN("Failed to set display mode, hr %#x.\n", hr);
1463 goto err;
1465 displaymode_set = TRUE;
1468 if (swapchain->state.desc.backbuffer_count > 0)
1470 if (!(swapchain->back_buffers = heap_calloc(swapchain->state.desc.backbuffer_count,
1471 sizeof(*swapchain->back_buffers))))
1473 ERR("Failed to allocate backbuffer array memory.\n");
1474 hr = E_OUTOFMEMORY;
1475 goto err;
1478 texture_desc.bind_flags = swapchain->state.desc.backbuffer_bind_flags;
1479 texture_desc.usage = 0;
1480 if (device->wined3d->flags & WINED3D_NO3D)
1481 texture_desc.usage |= WINED3DUSAGE_OWNDC;
1482 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
1484 TRACE("Creating back buffer %u.\n", i);
1485 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1486 parent, &texture_desc, texture_flags, &swapchain->back_buffers[i])))
1488 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1489 swapchain->state.desc.backbuffer_count = i;
1490 goto err;
1492 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
1496 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1497 if (desc->enable_auto_depth_stencil)
1499 TRACE("Creating depth/stencil buffer.\n");
1500 if (!device->auto_depth_stencil_view)
1502 struct wined3d_view_desc desc;
1503 struct wined3d_texture *ds;
1505 texture_desc.format = swapchain->state.desc.auto_depth_stencil_format;
1506 texture_desc.usage = 0;
1507 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
1508 if (device->wined3d->flags & WINED3D_NO3D)
1509 texture_desc.access = WINED3D_RESOURCE_ACCESS_CPU;
1510 else
1511 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
1513 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1514 device->device_parent, &texture_desc, 0, &ds)))
1516 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
1517 goto err;
1520 desc.format_id = ds->resource.format->id;
1521 desc.flags = 0;
1522 desc.u.texture.level_idx = 0;
1523 desc.u.texture.level_count = 1;
1524 desc.u.texture.layer_idx = 0;
1525 desc.u.texture.layer_count = 1;
1526 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
1527 &device->auto_depth_stencil_view);
1528 wined3d_texture_decref(ds);
1529 if (FAILED(hr))
1531 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1532 goto err;
1537 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1539 wined3d_mutex_unlock();
1541 return WINED3D_OK;
1543 err:
1544 if (displaymode_set)
1546 if (FAILED(wined3d_output_set_display_mode(desc->output,
1547 &swapchain->state.original_mode)))
1548 ERR("Failed to restore display mode.\n");
1551 if (swapchain->back_buffers)
1553 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
1555 if (swapchain->back_buffers[i])
1557 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1558 wined3d_texture_decref(swapchain->back_buffers[i]);
1561 heap_free(swapchain->back_buffers);
1564 if (swapchain->front_buffer)
1566 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1567 wined3d_texture_decref(swapchain->front_buffer);
1570 wined3d_swapchain_state_cleanup(&swapchain->state);
1571 wined3d_mutex_unlock();
1573 return hr;
1576 HRESULT wined3d_swapchain_no3d_init(struct wined3d_swapchain *swapchain_no3d, struct wined3d_device *device,
1577 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain_state_parent *state_parent,
1578 void *parent, const struct wined3d_parent_ops *parent_ops)
1580 TRACE("swapchain_no3d %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1581 swapchain_no3d, device, desc, state_parent, parent, parent_ops);
1583 return wined3d_swapchain_init(swapchain_no3d, device, desc, state_parent, parent, parent_ops,
1584 &swapchain_no3d_ops);
1587 HRESULT wined3d_swapchain_gl_init(struct wined3d_swapchain_gl *swapchain_gl, struct wined3d_device *device,
1588 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain_state_parent *state_parent,
1589 void *parent, const struct wined3d_parent_ops *parent_ops)
1591 HRESULT hr;
1593 TRACE("swapchain_gl %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1594 swapchain_gl, device, desc, state_parent, parent, parent_ops);
1596 if (FAILED(hr = wined3d_swapchain_init(&swapchain_gl->s, device, desc, state_parent, parent,
1597 parent_ops, &swapchain_gl_ops)))
1599 /* Cleanup any context that may have been created for the swapchain. */
1600 wined3d_cs_destroy_object(device->cs, wined3d_swapchain_gl_destroy_object, swapchain_gl);
1601 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1604 return hr;
1607 HRESULT wined3d_swapchain_vk_init(struct wined3d_swapchain_vk *swapchain_vk, struct wined3d_device *device,
1608 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain_state_parent *state_parent,
1609 void *parent, const struct wined3d_parent_ops *parent_ops)
1611 HRESULT hr;
1613 TRACE("swapchain_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1614 swapchain_vk, device, desc, parent, parent_ops);
1616 if (FAILED(hr = wined3d_swapchain_init(&swapchain_vk->s, device, desc, state_parent, parent,
1617 parent_ops, &swapchain_vk_ops)))
1618 return hr;
1620 if (swapchain_vk->s.win_handle == GetDesktopWindow())
1622 WARN("Creating a desktop window swapchain.\n");
1623 return hr;
1626 if (FAILED(hr = wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk)))
1628 wined3d_swapchain_cleanup(&swapchain_vk->s);
1629 return hr;
1632 return hr;
1635 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device,
1636 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain_state_parent *state_parent,
1637 void *parent, const struct wined3d_parent_ops *parent_ops,
1638 struct wined3d_swapchain **swapchain)
1640 struct wined3d_swapchain *object;
1641 HRESULT hr;
1643 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_swapchain(device,
1644 desc, state_parent, parent, parent_ops, &object)))
1645 return hr;
1647 if (desc->flags & WINED3D_SWAPCHAIN_IMPLICIT)
1649 wined3d_mutex_lock();
1650 if (FAILED(hr = wined3d_device_set_implicit_swapchain(device, object)))
1652 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1653 device->adapter->adapter_ops->adapter_destroy_swapchain(object);
1654 wined3d_mutex_unlock();
1655 return hr;
1657 wined3d_mutex_unlock();
1660 *swapchain = object;
1662 return hr;
1665 static struct wined3d_context_gl *wined3d_swapchain_gl_create_context(struct wined3d_swapchain_gl *swapchain_gl)
1667 struct wined3d_device *device = swapchain_gl->s.device;
1668 struct wined3d_context_gl *context_gl;
1670 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain_gl, GetCurrentThreadId());
1672 wined3d_from_cs(device->cs);
1674 if (!(context_gl = heap_alloc_zero(sizeof(*context_gl))))
1676 ERR("Failed to allocate context memory.\n");
1677 return NULL;
1680 if (FAILED(wined3d_context_gl_init(context_gl, swapchain_gl)))
1682 WARN("Failed to initialise context.\n");
1683 heap_free(context_gl);
1684 return NULL;
1687 if (!device_context_add(device, &context_gl->c))
1689 ERR("Failed to add the newly created context to the context list.\n");
1690 wined3d_context_gl_destroy(context_gl);
1691 return NULL;
1694 TRACE("Created context %p.\n", context_gl);
1696 context_release(&context_gl->c);
1698 if (!wined3d_array_reserve((void **)&swapchain_gl->contexts, &swapchain_gl->contexts_size,
1699 swapchain_gl->context_count + 1, sizeof(*swapchain_gl->contexts)))
1701 ERR("Failed to allocate new context array memory.\n");
1702 wined3d_context_gl_destroy(context_gl);
1703 return NULL;
1705 swapchain_gl->contexts[swapchain_gl->context_count++] = context_gl;
1707 return context_gl;
1710 void wined3d_swapchain_gl_destroy_contexts(struct wined3d_swapchain_gl *swapchain_gl)
1712 unsigned int i;
1714 for (i = 0; i < swapchain_gl->context_count; ++i)
1716 wined3d_context_gl_destroy(swapchain_gl->contexts[i]);
1718 heap_free(swapchain_gl->contexts);
1719 swapchain_gl->contexts_size = 0;
1720 swapchain_gl->context_count = 0;
1721 swapchain_gl->contexts = NULL;
1724 struct wined3d_context_gl *wined3d_swapchain_gl_get_context(struct wined3d_swapchain_gl *swapchain_gl)
1726 DWORD tid = GetCurrentThreadId();
1727 unsigned int i;
1729 for (i = 0; i < swapchain_gl->context_count; ++i)
1731 if (swapchain_gl->contexts[i]->tid == tid)
1732 return swapchain_gl->contexts[i];
1735 /* Create a new context for the thread. */
1736 return wined3d_swapchain_gl_create_context(swapchain_gl);
1739 HDC wined3d_swapchain_gl_get_backup_dc(struct wined3d_swapchain_gl *swapchain_gl)
1741 if (!swapchain_gl->backup_dc)
1743 TRACE("Creating the backup window for swapchain %p.\n", swapchain_gl);
1745 if (!(swapchain_gl->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1746 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1748 ERR("Failed to create a window.\n");
1749 return NULL;
1752 if (!(swapchain_gl->backup_dc = GetDC(swapchain_gl->backup_wnd)))
1754 ERR("Failed to get a DC.\n");
1755 DestroyWindow(swapchain_gl->backup_wnd);
1756 swapchain_gl->backup_wnd = NULL;
1757 return NULL;
1761 return swapchain_gl->backup_dc;
1764 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1766 UINT i;
1768 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1770 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
1772 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1776 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1778 struct wined3d_device *device = swapchain->device;
1779 HWND window = swapchain->state.device_window;
1780 struct wined3d_output_desc output_desc;
1781 unsigned int screensaver_active;
1782 struct wined3d_output *output;
1783 BOOL focus_messages, filter;
1784 HRESULT hr;
1786 /* This code is not protected by the wined3d mutex, so it may run while
1787 * wined3d_device_reset is active. Testing on Windows shows that changing
1788 * focus during resets and resetting during focus change events causes
1789 * the application to crash with an invalid memory access. */
1791 if (!(focus_messages = device->wined3d->flags & WINED3D_FOCUS_MESSAGES))
1792 filter = wined3d_filter_messages(window, TRUE);
1794 output = wined3d_swapchain_get_output(swapchain);
1795 if (!output)
1797 ERR("Failed to get output from swapchain %p.\n", swapchain);
1798 return;
1801 if (activate)
1803 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
1804 if ((device->restore_screensaver = !!screensaver_active))
1805 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
1807 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1809 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1810 * the window but leaves the size untouched, d3d9 sets the size on an
1811 * invisible window, generates messages but doesn't change the window
1812 * properties. The implementation follows d3d9.
1814 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1815 * resume drawing after a focus loss. */
1816 if (SUCCEEDED(hr = wined3d_output_get_desc(output, &output_desc)))
1817 SetWindowPos(window, NULL, output_desc.desktop_rect.left,
1818 output_desc.desktop_rect.top, swapchain->state.desc.backbuffer_width,
1819 swapchain->state.desc.backbuffer_height, SWP_NOACTIVATE | SWP_NOZORDER);
1820 else
1821 ERR("Failed to get output description, hr %#x.\n", hr);
1824 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1826 if (FAILED(hr = wined3d_output_set_display_mode(output,
1827 &swapchain->state.d3d_mode)))
1828 ERR("Failed to set display mode, hr %#x.\n", hr);
1831 if (swapchain == device->swapchains[0])
1832 device->device_parent->ops->activate(device->device_parent, TRUE);
1834 else
1836 if (device->restore_screensaver)
1838 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
1839 device->restore_screensaver = FALSE;
1842 if (FAILED(hr = wined3d_output_set_display_mode(output, NULL)))
1843 ERR("Failed to set display mode, hr %#x.\n", hr);
1845 swapchain->reapply_mode = TRUE;
1847 /* Some DDraw apps (Deus Ex: GOTY, and presumably all UT 1 based games) destroy the device
1848 * during window minimization. Do our housekeeping now, as the device may not exist after
1849 * the ShowWindow call.
1851 * In d3d9, the device is marked lost after the window is minimized. If we find an app
1852 * that needs this behavior (e.g. because it calls TestCooperativeLevel in the window proc)
1853 * we'll have to control this via a create flag. Note that the device and swapchain are not
1854 * safe to access after the ShowWindow call. */
1855 if (swapchain == device->swapchains[0])
1856 device->device_parent->ops->activate(device->device_parent, FALSE);
1858 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES) && IsWindowVisible(window))
1859 ShowWindow(window, SW_MINIMIZE);
1862 if (!focus_messages)
1863 wined3d_filter_messages(window, filter);
1866 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1867 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1868 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1870 struct wined3d_swapchain_desc *desc = &swapchain->state.desc;
1871 BOOL update_desc = FALSE;
1873 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1874 "multisample_type %#x, multisample_quality %#x.\n",
1875 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1876 multisample_type, multisample_quality);
1878 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1880 if (buffer_count && buffer_count != desc->backbuffer_count)
1881 FIXME("Cannot change the back buffer count yet.\n");
1883 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
1885 if (!width || !height)
1887 /* The application is requesting that either the swapchain width or
1888 * height be set to the corresponding dimension in the window's
1889 * client rect. */
1891 RECT client_rect;
1893 if (!desc->windowed)
1894 return WINED3DERR_INVALIDCALL;
1896 if (!GetClientRect(swapchain->state.device_window, &client_rect))
1898 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1899 return WINED3DERR_INVALIDCALL;
1902 if (!width)
1903 width = client_rect.right;
1905 if (!height)
1906 height = client_rect.bottom;
1909 if (width != desc->backbuffer_width || height != desc->backbuffer_height)
1911 desc->backbuffer_width = width;
1912 desc->backbuffer_height = height;
1913 update_desc = TRUE;
1916 if (format_id == WINED3DFMT_UNKNOWN)
1918 if (!desc->windowed)
1919 return WINED3DERR_INVALIDCALL;
1920 format_id = swapchain->state.original_mode.format_id;
1923 if (format_id != desc->backbuffer_format)
1925 desc->backbuffer_format = format_id;
1926 update_desc = TRUE;
1929 if (multisample_type != desc->multisample_type
1930 || multisample_quality != desc->multisample_quality)
1932 desc->multisample_type = multisample_type;
1933 desc->multisample_quality = multisample_quality;
1934 update_desc = TRUE;
1937 if (update_desc)
1939 HRESULT hr;
1940 UINT i;
1942 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, 0, desc->backbuffer_width,
1943 desc->backbuffer_height, desc->backbuffer_format,
1944 desc->multisample_type, desc->multisample_quality, NULL, 0)))
1945 return hr;
1947 for (i = 0; i < desc->backbuffer_count; ++i)
1949 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], 0, desc->backbuffer_width,
1950 desc->backbuffer_height, desc->backbuffer_format,
1951 desc->multisample_type, desc->multisample_quality, NULL, 0)))
1952 return hr;
1956 swapchain_update_render_to_fbo(swapchain);
1957 swapchain_update_draw_bindings(swapchain);
1959 return WINED3D_OK;
1962 static HRESULT wined3d_swapchain_state_set_display_mode(struct wined3d_swapchain_state *state,
1963 struct wined3d_output *output, struct wined3d_display_mode *mode)
1965 HRESULT hr;
1967 if (state->desc.flags & WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE)
1969 if (FAILED(hr = wined3d_output_find_closest_matching_mode(output, mode)))
1971 WARN("Failed to find closest matching mode, hr %#x.\n", hr);
1975 if (output != state->desc.output)
1977 if (FAILED(hr = wined3d_output_set_display_mode(state->desc.output, &state->original_mode)))
1979 WARN("Failed to set display mode, hr %#x.\n", hr);
1980 return hr;
1983 if (FAILED(hr = wined3d_output_get_display_mode(output, &state->original_mode, NULL)))
1985 WARN("Failed to get current display mode, hr %#x.\n", hr);
1986 return hr;
1990 if (FAILED(hr = wined3d_output_set_display_mode(output, mode)))
1992 WARN("Failed to set display mode, hr %#x.\n", hr);
1993 return WINED3DERR_INVALIDCALL;
1996 return WINED3D_OK;
1999 HRESULT CDECL wined3d_swapchain_state_resize_target(struct wined3d_swapchain_state *state,
2000 const struct wined3d_display_mode *mode)
2002 struct wined3d_display_mode actual_mode;
2003 struct wined3d_output_desc output_desc;
2004 RECT original_window_rect, window_rect;
2005 int x, y, width, height;
2006 HWND window;
2007 HRESULT hr;
2009 TRACE("state %p, mode %p.\n", state, mode);
2011 wined3d_mutex_lock();
2013 window = state->device_window;
2015 if (state->desc.windowed)
2017 SetRect(&window_rect, 0, 0, mode->width, mode->height);
2018 AdjustWindowRectEx(&window_rect,
2019 GetWindowLongW(window, GWL_STYLE), FALSE,
2020 GetWindowLongW(window, GWL_EXSTYLE));
2021 GetWindowRect(window, &original_window_rect);
2023 x = original_window_rect.left;
2024 y = original_window_rect.top;
2025 width = window_rect.right - window_rect.left;
2026 height = window_rect.bottom - window_rect.top;
2028 else
2030 if (state->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
2032 actual_mode = *mode;
2033 if (FAILED(hr = wined3d_swapchain_state_set_display_mode(state, state->desc.output,
2034 &actual_mode)))
2036 ERR("Failed to set display mode, hr %#x.\n", hr);
2037 wined3d_mutex_unlock();
2038 return hr;
2042 if (FAILED(hr = wined3d_output_get_desc(state->desc.output, &output_desc)))
2044 ERR("Failed to get output description, hr %#x.\n", hr);
2045 wined3d_mutex_unlock();
2046 return hr;
2049 x = output_desc.desktop_rect.left;
2050 y = output_desc.desktop_rect.top;
2051 width = output_desc.desktop_rect.right - output_desc.desktop_rect.left;
2052 height = output_desc.desktop_rect.bottom - output_desc.desktop_rect.top;
2055 wined3d_mutex_unlock();
2057 MoveWindow(window, x, y, width, height, TRUE);
2059 return WINED3D_OK;
2062 static LONG fullscreen_style(LONG style)
2064 /* Make sure the window is managed, otherwise we won't get keyboard input. */
2065 style |= WS_POPUP | WS_SYSMENU;
2066 style &= ~(WS_CAPTION | WS_THICKFRAME);
2068 return style;
2071 static LONG fullscreen_exstyle(LONG exstyle)
2073 /* Filter out window decorations. */
2074 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
2076 return exstyle;
2079 HRESULT wined3d_swapchain_state_setup_fullscreen(struct wined3d_swapchain_state *state,
2080 HWND window, int x, int y, int width, int height)
2082 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOACTIVATE;
2083 LONG style, exstyle;
2084 BOOL filter;
2086 TRACE("Setting up window %p for fullscreen mode.\n", window);
2088 if (!IsWindow(window))
2090 WARN("%p is not a valid window.\n", window);
2091 return WINED3DERR_NOTAVAILABLE;
2094 if (state->style || state->exstyle)
2096 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
2097 window, state->style, state->exstyle);
2100 if (state->desc.flags & WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES)
2101 window_pos_flags |= SWP_NOZORDER;
2102 else
2103 window_pos_flags |= SWP_SHOWWINDOW;
2105 state->style = GetWindowLongW(window, GWL_STYLE);
2106 state->exstyle = GetWindowLongW(window, GWL_EXSTYLE);
2108 style = fullscreen_style(state->style);
2109 exstyle = fullscreen_exstyle(state->exstyle);
2111 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
2112 state->style, state->exstyle, style, exstyle);
2114 filter = wined3d_filter_messages(window, TRUE);
2116 SetWindowLongW(window, GWL_STYLE, style);
2117 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
2118 SetWindowPos(window, HWND_TOPMOST, x, y, width, height, window_pos_flags);
2120 wined3d_filter_messages(window, filter);
2122 return WINED3D_OK;
2125 void wined3d_swapchain_state_restore_from_fullscreen(struct wined3d_swapchain_state *state,
2126 HWND window, const RECT *window_rect)
2128 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
2129 HWND window_pos_after = NULL;
2130 LONG style, exstyle;
2131 RECT rect = {0};
2132 BOOL filter;
2134 if (!state->style && !state->exstyle)
2135 return;
2137 if ((state->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_STATE)
2138 && !(state->desc.flags & WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES))
2140 window_pos_after = (state->exstyle & WS_EX_TOPMOST) ? HWND_TOPMOST : HWND_NOTOPMOST;
2141 window_pos_flags |= (state->style & WS_VISIBLE) ? SWP_SHOWWINDOW : SWP_HIDEWINDOW;
2142 window_pos_flags &= ~SWP_NOZORDER;
2145 style = GetWindowLongW(window, GWL_STYLE);
2146 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
2148 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
2149 * application, and we want to ignore them in the test below, since it's
2150 * not the application's fault that they changed. Additionally, we want to
2151 * preserve the current status of these flags (i.e. don't restore them) to
2152 * more closely emulate the behavior of Direct3D, which leaves these flags
2153 * alone when returning to windowed mode. */
2154 state->style ^= (state->style ^ style) & WS_VISIBLE;
2155 state->exstyle ^= (state->exstyle ^ exstyle) & WS_EX_TOPMOST;
2157 TRACE("Restoring window style of window %p to %08x, %08x.\n",
2158 window, state->style, state->exstyle);
2160 filter = wined3d_filter_messages(window, TRUE);
2162 /* Only restore the style if the application didn't modify it during the
2163 * fullscreen phase. Some applications change it before calling Reset()
2164 * when switching between windowed and fullscreen modes (HL2), some
2165 * depend on the original style (Eve Online). */
2166 if (style == fullscreen_style(state->style) && exstyle == fullscreen_exstyle(state->exstyle))
2168 SetWindowLongW(window, GWL_STYLE, state->style);
2169 SetWindowLongW(window, GWL_EXSTYLE, state->exstyle);
2172 if (window_rect)
2173 rect = *window_rect;
2174 else
2175 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
2176 SetWindowPos(window, window_pos_after, rect.left, rect.top,
2177 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
2179 wined3d_filter_messages(window, filter);
2181 /* Delete the old values. */
2182 state->style = 0;
2183 state->exstyle = 0;
2186 HRESULT CDECL wined3d_swapchain_state_set_fullscreen(struct wined3d_swapchain_state *state,
2187 const struct wined3d_swapchain_desc *swapchain_desc,
2188 const struct wined3d_display_mode *mode)
2190 struct wined3d_display_mode actual_mode;
2191 struct wined3d_output_desc output_desc;
2192 BOOL windowed = state->desc.windowed;
2193 HRESULT hr;
2195 TRACE("state %p, swapchain_desc %p, mode %p.\n", state, swapchain_desc, mode);
2197 if (state->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
2199 if (mode)
2201 actual_mode = *mode;
2203 else
2205 if (!swapchain_desc->windowed)
2207 actual_mode.width = swapchain_desc->backbuffer_width;
2208 actual_mode.height = swapchain_desc->backbuffer_height;
2209 actual_mode.refresh_rate = swapchain_desc->refresh_rate;
2210 actual_mode.format_id = adapter_format_from_backbuffer_format(swapchain_desc->output->adapter,
2211 swapchain_desc->backbuffer_format);
2212 actual_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
2214 else
2216 actual_mode = state->original_mode;
2220 if (FAILED(hr = wined3d_swapchain_state_set_display_mode(state, swapchain_desc->output,
2221 &actual_mode)))
2222 return hr;
2224 else
2226 if (mode)
2227 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
2229 if (FAILED(hr = wined3d_output_get_display_mode(swapchain_desc->output, &actual_mode,
2230 NULL)))
2232 ERR("Failed to get display mode, hr %#x.\n", hr);
2233 return WINED3DERR_INVALIDCALL;
2237 if (!swapchain_desc->windowed)
2239 unsigned int width = actual_mode.width;
2240 unsigned int height = actual_mode.height;
2242 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
2244 ERR("Failed to get output description, hr %#x.\n", hr);
2245 return hr;
2248 if (state->desc.windowed)
2250 /* Switch from windowed to fullscreen */
2251 if (FAILED(hr = wined3d_swapchain_state_setup_fullscreen(state, state->device_window,
2252 output_desc.desktop_rect.left, output_desc.desktop_rect.top, width, height)))
2253 return hr;
2255 else
2257 HWND window = state->device_window;
2258 BOOL filter;
2260 /* Fullscreen -> fullscreen mode change */
2261 filter = wined3d_filter_messages(window, TRUE);
2262 MoveWindow(window, output_desc.desktop_rect.left, output_desc.desktop_rect.top, width,
2263 height, TRUE);
2264 ShowWindow(window, SW_SHOW);
2265 wined3d_filter_messages(window, filter);
2267 state->d3d_mode = actual_mode;
2269 else if (!state->desc.windowed)
2271 /* Fullscreen -> windowed switch */
2272 RECT *window_rect = NULL;
2273 if (state->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
2274 window_rect = &state->original_window_rect;
2275 wined3d_swapchain_state_restore_from_fullscreen(state, state->device_window, window_rect);
2278 state->desc.output = swapchain_desc->output;
2279 state->desc.windowed = swapchain_desc->windowed;
2281 if (windowed != state->desc.windowed)
2282 state->parent->ops->windowed_state_changed(state->parent, state->desc.windowed);
2284 return WINED3D_OK;
2287 BOOL CDECL wined3d_swapchain_state_is_windowed(const struct wined3d_swapchain_state *state)
2289 TRACE("state %p.\n", state);
2291 return state->desc.windowed;
2294 void CDECL wined3d_swapchain_state_destroy(struct wined3d_swapchain_state *state)
2296 wined3d_swapchain_state_cleanup(state);
2297 heap_free(state);
2300 HRESULT CDECL wined3d_swapchain_state_create(const struct wined3d_swapchain_desc *desc,
2301 HWND window, struct wined3d *wined3d, struct wined3d_swapchain_state_parent *state_parent,
2302 struct wined3d_swapchain_state **state)
2304 struct wined3d_swapchain_state *s;
2305 HRESULT hr;
2307 TRACE("desc %p, window %p, wined3d %p, state %p.\n", desc, window, wined3d, state);
2309 if (!(s = heap_alloc_zero(sizeof(*s))))
2310 return E_OUTOFMEMORY;
2312 if (FAILED(hr = wined3d_swapchain_state_init(s, desc, window, wined3d, state_parent)))
2314 heap_free(s);
2315 return hr;
2318 *state = s;
2320 return hr;