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 "wined3d_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
26 WINE_DECLARE_DEBUG_CHANNEL(fps
);
28 static void swapchain_cleanup(struct wined3d_swapchain
*swapchain
)
33 TRACE("Destroying swapchain %p.\n", swapchain
);
35 wined3d_swapchain_set_gamma_ramp(swapchain
, 0, &swapchain
->orig_gamma
);
37 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
38 * is the last buffer to be destroyed, FindContext() depends on that. */
39 if (swapchain
->front_buffer
)
41 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
42 if (wined3d_texture_decref(swapchain
->front_buffer
))
43 WARN("Something's still holding the front buffer (%p).\n", swapchain
->front_buffer
);
44 swapchain
->front_buffer
= NULL
;
47 if (swapchain
->back_buffers
)
49 i
= swapchain
->desc
.backbuffer_count
;
53 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
54 if (wined3d_texture_decref(swapchain
->back_buffers
[i
]))
55 WARN("Something's still holding back buffer %u (%p).\n", i
, swapchain
->back_buffers
[i
]);
57 HeapFree(GetProcessHeap(), 0, swapchain
->back_buffers
);
58 swapchain
->back_buffers
= NULL
;
61 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
63 context_destroy(swapchain
->device
, swapchain
->context
[i
]);
65 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
67 /* Restore the screen resolution if we rendered in fullscreen.
68 * This will restore the screen resolution to what it was before creating
69 * the swapchain. In case of d3d8 and d3d9 this will be the original
70 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
71 * sets the resolution before starting up Direct3D, thus orig_width and
72 * orig_height will be equal to the modes in the presentation params. */
73 if (!swapchain
->desc
.windowed
&& swapchain
->desc
.auto_restore_display_mode
)
75 if (FAILED(hr
= wined3d_set_adapter_display_mode(swapchain
->device
->wined3d
,
76 swapchain
->device
->adapter
->ordinal
, &swapchain
->original_mode
)))
77 ERR("Failed to restore display mode, hr %#x.\n", hr
);
80 if (swapchain
->backup_dc
)
82 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain
->backup_wnd
, swapchain
->backup_dc
);
84 wined3d_release_dc(swapchain
->backup_wnd
, swapchain
->backup_dc
);
85 DestroyWindow(swapchain
->backup_wnd
);
89 ULONG CDECL
wined3d_swapchain_incref(struct wined3d_swapchain
*swapchain
)
91 ULONG refcount
= InterlockedIncrement(&swapchain
->ref
);
93 TRACE("%p increasing refcount to %u.\n", swapchain
, refcount
);
98 ULONG CDECL
wined3d_swapchain_decref(struct wined3d_swapchain
*swapchain
)
100 ULONG refcount
= InterlockedDecrement(&swapchain
->ref
);
102 TRACE("%p decreasing refcount to %u.\n", swapchain
, refcount
);
106 swapchain_cleanup(swapchain
);
107 swapchain
->parent_ops
->wined3d_object_destroyed(swapchain
->parent
);
108 HeapFree(GetProcessHeap(), 0, swapchain
);
114 void * CDECL
wined3d_swapchain_get_parent(const struct wined3d_swapchain
*swapchain
)
116 TRACE("swapchain %p.\n", swapchain
);
118 return swapchain
->parent
;
121 void CDECL
wined3d_swapchain_set_window(struct wined3d_swapchain
*swapchain
, HWND window
)
124 window
= swapchain
->device_window
;
125 if (window
== swapchain
->win_handle
)
128 TRACE("Setting swapchain %p window from %p to %p.\n",
129 swapchain
, swapchain
->win_handle
, window
);
130 swapchain
->win_handle
= window
;
133 HRESULT CDECL
wined3d_swapchain_present(struct wined3d_swapchain
*swapchain
,
134 const RECT
*src_rect
, const RECT
*dst_rect
, HWND dst_window_override
,
135 const RGNDATA
*dirty_region
, DWORD flags
)
137 static DWORD notified_flags
= 0;
139 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
140 swapchain
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
),
141 dst_window_override
, dirty_region
, flags
);
143 if (flags
& ~notified_flags
)
145 FIXME("Ignoring flags %#x.\n", flags
& ~notified_flags
);
146 notified_flags
|= flags
;
149 if (!swapchain
->back_buffers
)
151 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
152 return WINED3DERR_INVALIDCALL
;
155 wined3d_cs_emit_present(swapchain
->device
->cs
, swapchain
, src_rect
,
156 dst_rect
, dst_window_override
, dirty_region
, flags
);
161 HRESULT CDECL
wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain
*swapchain
,
162 struct wined3d_texture
*dst_texture
, unsigned int sub_resource_idx
)
164 struct wined3d_surface
*src_surface
, *dst_surface
;
165 struct wined3d_resource
*sub_resource
;
166 RECT src_rect
, dst_rect
;
168 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain
, dst_texture
, sub_resource_idx
);
170 if (!(sub_resource
= wined3d_texture_get_sub_resource(dst_texture
, sub_resource_idx
)) ||
171 sub_resource
->type
!= WINED3D_RTYPE_SURFACE
)
172 return WINED3DERR_INVALIDCALL
;
174 dst_surface
= surface_from_resource(sub_resource
);
175 src_surface
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
176 SetRect(&src_rect
, 0, 0, src_surface
->resource
.width
, src_surface
->resource
.height
);
179 if (swapchain
->desc
.windowed
)
181 MapWindowPoints(swapchain
->win_handle
, NULL
, (POINT
*)&dst_rect
, 2);
182 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
183 wine_dbgstr_rect(&dst_rect
));
186 return wined3d_surface_blt(dst_surface
, &dst_rect
, src_surface
, &src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
189 struct wined3d_texture
* CDECL
wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain
*swapchain
,
190 UINT back_buffer_idx
)
192 TRACE("swapchain %p, back_buffer_idx %u.\n",
193 swapchain
, back_buffer_idx
);
195 /* Return invalid if there is no backbuffer array, otherwise it will
196 * crash when ddraw is used (there swapchain->back_buffers is always
197 * NULL). We need this because this function is called from
198 * stateblock_init_default_state() to get the default scissorrect
200 if (!swapchain
->back_buffers
|| back_buffer_idx
>= swapchain
->desc
.backbuffer_count
)
202 WARN("Invalid back buffer index.\n");
203 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
204 * here in wined3d to avoid problems in other libs. */
208 TRACE("Returning back buffer %p.\n", swapchain
->back_buffers
[back_buffer_idx
]);
210 return swapchain
->back_buffers
[back_buffer_idx
];
213 HRESULT CDECL
wined3d_swapchain_get_raster_status(const struct wined3d_swapchain
*swapchain
,
214 struct wined3d_raster_status
*raster_status
)
216 TRACE("swapchain %p, raster_status %p.\n", swapchain
, raster_status
);
218 return wined3d_get_adapter_raster_status(swapchain
->device
->wined3d
,
219 swapchain
->device
->adapter
->ordinal
, raster_status
);
222 HRESULT CDECL
wined3d_swapchain_get_display_mode(const struct wined3d_swapchain
*swapchain
,
223 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
227 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain
, mode
, rotation
);
229 hr
= wined3d_get_adapter_display_mode(swapchain
->device
->wined3d
,
230 swapchain
->device
->adapter
->ordinal
, mode
, rotation
);
232 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
233 mode
->width
, mode
->height
, mode
->refresh_rate
, debug_d3dformat(mode
->format_id
));
238 struct wined3d_device
* CDECL
wined3d_swapchain_get_device(const struct wined3d_swapchain
*swapchain
)
240 TRACE("swapchain %p.\n", swapchain
);
242 return swapchain
->device
;
245 void CDECL
wined3d_swapchain_get_desc(const struct wined3d_swapchain
*swapchain
,
246 struct wined3d_swapchain_desc
*desc
)
248 TRACE("swapchain %p, desc %p.\n", swapchain
, desc
);
250 *desc
= swapchain
->desc
;
253 HRESULT CDECL
wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
254 DWORD flags
, const struct wined3d_gamma_ramp
*ramp
)
258 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain
, flags
, ramp
);
261 FIXME("Ignoring flags %#x.\n", flags
);
263 dc
= GetDCEx(swapchain
->device_window
, 0, DCX_USESTYLE
| DCX_CACHE
);
264 SetDeviceGammaRamp(dc
, (void *)ramp
);
265 ReleaseDC(swapchain
->device_window
, dc
);
270 void CDECL
wined3d_swapchain_set_palette(struct wined3d_swapchain
*swapchain
, struct wined3d_palette
*palette
)
272 TRACE("swapchain %p, palette %p.\n", swapchain
, palette
);
273 swapchain
->palette
= palette
;
276 HRESULT CDECL
wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
277 struct wined3d_gamma_ramp
*ramp
)
281 TRACE("swapchain %p, ramp %p.\n", swapchain
, ramp
);
283 dc
= GetDCEx(swapchain
->device_window
, 0, DCX_USESTYLE
| DCX_CACHE
);
284 GetDeviceGammaRamp(dc
, ramp
);
285 ReleaseDC(swapchain
->device_window
, dc
);
290 /* A GL context is provided by the caller */
291 static void swapchain_blit(const struct wined3d_swapchain
*swapchain
,
292 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
294 struct wined3d_surface
*backbuffer
= surface_from_resource(
295 wined3d_texture_get_sub_resource(swapchain
->back_buffers
[0], 0));
296 UINT src_w
= src_rect
->right
- src_rect
->left
;
297 UINT src_h
= src_rect
->bottom
- src_rect
->top
;
299 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
303 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
304 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
306 if (src_w
== dst_rect
->right
- dst_rect
->left
&& src_h
== dst_rect
->bottom
- dst_rect
->top
)
307 gl_filter
= GL_NEAREST
;
309 gl_filter
= GL_LINEAR
;
311 GetClientRect(swapchain
->win_handle
, &win_rect
);
312 win_h
= win_rect
.bottom
- win_rect
.top
;
314 if (gl_info
->fbo_ops
.glBlitFramebuffer
&& is_identity_fixup(backbuffer
->resource
.format
->color_fixup
))
316 DWORD location
= WINED3D_LOCATION_TEXTURE_RGB
;
318 if (backbuffer
->resource
.multisample_type
)
320 location
= WINED3D_LOCATION_RB_RESOLVED
;
321 #if defined(STAGING_CSMT)
322 wined3d_resource_load_location(&backbuffer
->resource
, context
, location
);
323 #else /* STAGING_CSMT */
324 surface_load_location(backbuffer
, context
, location
);
325 #endif /* STAGING_CSMT */
328 context_apply_fbo_state_blit(context
, GL_READ_FRAMEBUFFER
, backbuffer
, NULL
, location
);
329 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_COLOR_ATTACHMENT0
);
330 context_check_fbo_status(context
, GL_READ_FRAMEBUFFER
);
332 context_apply_fbo_state_blit(context
, GL_DRAW_FRAMEBUFFER
,
333 surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0)),
334 NULL
, WINED3D_LOCATION_DRAWABLE
);
335 context_set_draw_buffer(context
, GL_BACK
);
336 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
338 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
339 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
340 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
341 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
342 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
344 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
345 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
347 /* Note that the texture is upside down */
348 gl_info
->fbo_ops
.glBlitFramebuffer(src_rect
->left
, src_rect
->top
, src_rect
->right
, src_rect
->bottom
,
349 dst_rect
->left
, win_h
- dst_rect
->top
, dst_rect
->right
, win_h
- dst_rect
->bottom
,
350 GL_COLOR_BUFFER_BIT
, gl_filter
);
351 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
355 struct wined3d_device
*device
= swapchain
->device
;
356 struct wined3d_context
*context2
;
357 float tex_left
= src_rect
->left
;
358 float tex_top
= src_rect
->top
;
359 float tex_right
= src_rect
->right
;
360 float tex_bottom
= src_rect
->bottom
;
362 context2
= context_acquire(device
, backbuffer
);
363 context_apply_blit_state(context2
, device
);
365 if (backbuffer
->container
->flags
& WINED3D_TEXTURE_NORMALIZED_COORDS
)
367 tex_left
/= backbuffer
->pow2Width
;
368 tex_right
/= backbuffer
->pow2Width
;
369 tex_top
/= backbuffer
->pow2Height
;
370 tex_bottom
/= backbuffer
->pow2Height
;
373 if (is_complex_fixup(backbuffer
->resource
.format
->color_fixup
))
374 gl_filter
= GL_NEAREST
;
376 context_apply_fbo_state_blit(context2
, GL_FRAMEBUFFER
,
377 surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0)),
378 NULL
, WINED3D_LOCATION_DRAWABLE
);
379 context_bind_texture(context2
, backbuffer
->texture_target
, backbuffer
->container
->texture_rgb
.name
);
381 /* Set up the texture. The surface is not in a wined3d_texture
382 * container, so there are no D3D texture settings to dirtify. */
383 device
->blitter
->set_shader(device
->blit_priv
, context2
, backbuffer
, NULL
);
384 gl_info
->gl_ops
.gl
.p_glTexParameteri(backbuffer
->texture_target
, GL_TEXTURE_MIN_FILTER
, gl_filter
);
385 gl_info
->gl_ops
.gl
.p_glTexParameteri(backbuffer
->texture_target
, GL_TEXTURE_MAG_FILTER
, gl_filter
);
387 context_set_draw_buffer(context
, GL_BACK
);
389 /* Set the viewport to the destination rectandle, disable any projection
390 * transformation set up by context_apply_blit_state(), and draw a
391 * (-1,-1)-(1,1) quad.
393 * Back up viewport and matrix to avoid breaking last_was_blit
395 * Note that context_apply_blit_state() set up viewport and ortho to
396 * match the surface size - we want the GL drawable(=window) size. */
397 gl_info
->gl_ops
.gl
.p_glPushAttrib(GL_VIEWPORT_BIT
);
398 gl_info
->gl_ops
.gl
.p_glViewport(dst_rect
->left
, win_h
- dst_rect
->bottom
,
399 dst_rect
->right
, win_h
- dst_rect
->top
);
400 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
401 gl_info
->gl_ops
.gl
.p_glPushMatrix();
402 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
404 gl_info
->gl_ops
.gl
.p_glBegin(GL_QUADS
);
406 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_left
, tex_bottom
);
407 gl_info
->gl_ops
.gl
.p_glVertex2i(-1, -1);
410 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_left
, tex_top
);
411 gl_info
->gl_ops
.gl
.p_glVertex2i(-1, 1);
414 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_right
, tex_top
);
415 gl_info
->gl_ops
.gl
.p_glVertex2i(1, 1);
418 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_right
, tex_bottom
);
419 gl_info
->gl_ops
.gl
.p_glVertex2i(1, -1);
420 gl_info
->gl_ops
.gl
.p_glEnd();
422 gl_info
->gl_ops
.gl
.p_glPopMatrix();
423 gl_info
->gl_ops
.gl
.p_glPopAttrib();
425 device
->blitter
->unset_shader(context
->gl_info
);
426 checkGLcall("Swapchain present blit(manual)\n");
428 context_release(context2
);
432 static void swapchain_gl_present(struct wined3d_swapchain
*swapchain
, const RECT
*src_rect_in
,
433 #if defined(STAGING_CSMT)
434 const RECT
*dst_rect_in
, const RGNDATA
*dirty_region
, DWORD flags
,
435 struct wined3d_surface
*depth_stencil
)
437 struct wined3d_surface
*back_buffer
= surface_from_resource(
438 wined3d_texture_get_sub_resource(swapchain
->back_buffers
[0], 0));
439 #else /* STAGING_CSMT */
440 const RECT
*dst_rect_in
, const RGNDATA
*dirty_region
, DWORD flags
)
442 struct wined3d_surface
*back_buffer
= surface_from_resource(
443 wined3d_texture_get_sub_resource(swapchain
->back_buffers
[0], 0));
444 const struct wined3d_fb_state
*fb
= &swapchain
->device
->fb
;
445 #endif /* STAGING_CSMT */
446 const struct wined3d_gl_info
*gl_info
;
447 struct wined3d_context
*context
;
448 struct wined3d_surface
*front
;
449 RECT src_rect
, dst_rect
;
452 context
= context_acquire(swapchain
->device
, back_buffer
);
455 context_release(context
);
456 WARN("Invalid context, skipping present.\n");
460 gl_info
= context
->gl_info
;
462 if (swapchain
->device
->logo_texture
)
464 struct wined3d_surface
*src_surface
= surface_from_resource(
465 wined3d_texture_get_sub_resource(swapchain
->device
->logo_texture
, 0));
466 RECT rect
= {0, 0, src_surface
->resource
.width
, src_surface
->resource
.height
};
468 /* Blit the logo into the upper left corner of the drawable. */
469 wined3d_surface_blt(back_buffer
, &rect
, src_surface
, &rect
,
470 WINED3D_BLT_ALPHA_TEST
, NULL
, WINED3D_TEXF_POINT
);
473 #if !defined(STAGING_CSMT)
474 if (swapchain
->device
->bCursorVisible
&& swapchain
->device
->cursor_texture
475 && !swapchain
->device
->hardwareCursor
)
477 struct wined3d_surface
*cursor
= surface_from_resource(
478 wined3d_texture_get_sub_resource(swapchain
->device
->cursor_texture
, 0));
481 swapchain
->device
->xScreenSpace
- swapchain
->device
->xHotSpot
,
482 swapchain
->device
->yScreenSpace
- swapchain
->device
->yHotSpot
,
483 swapchain
->device
->xScreenSpace
+ swapchain
->device
->cursorWidth
- swapchain
->device
->xHotSpot
,
484 swapchain
->device
->yScreenSpace
+ swapchain
->device
->cursorHeight
- swapchain
->device
->yHotSpot
,
489 swapchain
->device
->cursor_texture
->resource
.width
,
490 swapchain
->device
->cursor_texture
->resource
.height
492 const RECT clip_rect
= {0, 0, back_buffer
->resource
.width
, back_buffer
->resource
.height
};
494 TRACE("Rendering the software cursor.\n");
496 if (swapchain
->desc
.windowed
)
497 MapWindowPoints(NULL
, swapchain
->win_handle
, (POINT
*)&destRect
, 2);
498 if (wined3d_clip_blit(&clip_rect
, &destRect
, &src_rect
))
499 wined3d_surface_blt(back_buffer
, &destRect
, cursor
, &src_rect
,
500 WINED3D_BLT_ALPHA_TEST
, NULL
, WINED3D_TEXF_POINT
);
503 #endif /* STAGING_CSMT */
504 TRACE("Presenting HDC %p.\n", context
->hdc
);
506 render_to_fbo
= swapchain
->render_to_fbo
;
510 src_rect
= *src_rect_in
;
511 if (!render_to_fbo
&& (src_rect
.left
|| src_rect
.top
512 || src_rect
.right
!= swapchain
->desc
.backbuffer_width
513 || src_rect
.bottom
!= swapchain
->desc
.backbuffer_height
))
515 render_to_fbo
= TRUE
;
522 src_rect
.right
= swapchain
->desc
.backbuffer_width
;
523 src_rect
.bottom
= swapchain
->desc
.backbuffer_height
;
527 dst_rect
= *dst_rect_in
;
529 GetClientRect(swapchain
->win_handle
, &dst_rect
);
531 if (!render_to_fbo
&& (dst_rect
.left
|| dst_rect
.top
532 || dst_rect
.right
!= swapchain
->desc
.backbuffer_width
533 || dst_rect
.bottom
!= swapchain
->desc
.backbuffer_height
))
534 render_to_fbo
= TRUE
;
536 /* Rendering to a window of different size, presenting partial rectangles,
537 * or rendering to a different window needs help from FBO_blit or a textured
538 * draw. Render the swapchain to a FBO in the future.
540 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
541 * all these issues - this fails if the window is smaller than the backbuffer.
543 if (!swapchain
->render_to_fbo
&& render_to_fbo
&& wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
545 #if defined(STAGING_CSMT)
546 wined3d_resource_load_location(&back_buffer
->resource
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
547 wined3d_resource_invalidate_location(&back_buffer
->resource
, WINED3D_LOCATION_DRAWABLE
);
548 swapchain
->render_to_fbo
= TRUE
;
549 swapchain_update_draw_bindings(swapchain
);
553 wined3d_resource_load_location(&back_buffer
->resource
, context
, back_buffer
->container
->resource
.draw_binding
);
554 #else /* STAGING_CSMT */
555 surface_load_location(back_buffer
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
556 surface_invalidate_location(back_buffer
, WINED3D_LOCATION_DRAWABLE
);
557 swapchain
->render_to_fbo
= TRUE
;
558 swapchain_update_draw_bindings(swapchain
);
562 surface_load_location(back_buffer
, context
, back_buffer
->container
->resource
.draw_binding
);
563 #endif /* STAGING_CSMT */
566 if (swapchain
->render_to_fbo
)
568 static unsigned int once
;
570 if (swapchain
->desc
.swap_effect
== WINED3D_SWAP_EFFECT_FLIP
&& !once
++)
571 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
573 swapchain_blit(swapchain
, context
, &src_rect
, &dst_rect
);
576 #if defined(STAGING_CSMT)
577 if (swapchain
->num_contexts
> 1 && !wined3d_settings
.cs_multithreaded
)
578 gl_info
->gl_ops
.gl
.p_glFlush();
579 #else /* STAGING_CSMT */
580 if (swapchain
->num_contexts
> 1)
581 gl_info
->gl_ops
.gl
.p_glFinish();
582 #endif /* STAGING_CSMT */
584 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
585 gl_info
->gl_ops
.wgl
.p_wglSwapBuffers(context
->hdc
); /* TODO: cycle through the swapchain buffers */
587 TRACE("SwapBuffers called, Starting new frame\n");
591 DWORD time
= GetTickCount();
594 /* every 1.5 seconds */
595 if (time
- swapchain
->prev_time
> 1500)
597 TRACE_(fps
)("%p @ approx %.2ffps\n",
598 swapchain
, 1000.0 * swapchain
->frames
/ (time
- swapchain
->prev_time
));
599 swapchain
->prev_time
= time
;
600 swapchain
->frames
= 0;
604 front
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
606 #if defined(STAGING_CSMT)
607 wined3d_resource_validate_location(&front
->resource
, WINED3D_LOCATION_DRAWABLE
);
608 wined3d_resource_invalidate_location(&front
->resource
, ~WINED3D_LOCATION_DRAWABLE
);
609 switch (swapchain
->desc
.swap_effect
)
611 case WINED3D_SWAP_EFFECT_DISCARD
:
612 wined3d_resource_validate_location(&back_buffer
->resource
, WINED3D_LOCATION_DISCARDED
);
615 case WINED3D_SWAP_EFFECT_FLIP
:
616 wined3d_resource_validate_location(&back_buffer
->resource
, back_buffer
->container
->resource
.draw_binding
);
617 wined3d_resource_invalidate_location(&back_buffer
->resource
, ~back_buffer
->container
->resource
.draw_binding
);
626 if (swapchain
->desc
.flags
& WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
627 || depth_stencil
->flags
& SFLAG_DISCARD
)
629 surface_modify_ds_location(depth_stencil
, WINED3D_LOCATION_DISCARDED
,
630 depth_stencil
->resource
.width
, depth_stencil
->resource
.height
);
631 if (depth_stencil
== swapchain
->device
->cs
->onscreen_depth_stencil
)
633 wined3d_texture_decref(swapchain
->device
->cs
->onscreen_depth_stencil
->container
);
634 swapchain
->device
->cs
->onscreen_depth_stencil
= NULL
;
635 #else /* STAGING_CSMT */
636 surface_validate_location(front
, WINED3D_LOCATION_DRAWABLE
);
637 surface_invalidate_location(front
, ~WINED3D_LOCATION_DRAWABLE
);
638 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
639 * and INTEXTURE copies can keep their old content if they have any defined content.
640 * If the swapeffect is COPY, the content remains the same.
642 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
643 * up to date and hope WGL flipped front and back buffers and read this data into
644 * the FBO. Don't bother about this for now. */
646 if (fb
->depth_stencil
)
648 struct wined3d_surface
*ds
= wined3d_rendertarget_view_get_surface(fb
->depth_stencil
);
650 if (ds
&& (swapchain
->desc
.flags
& WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
651 || ds
->flags
& SFLAG_DISCARD
))
653 surface_modify_ds_location(ds
, WINED3D_LOCATION_DISCARDED
,
654 fb
->depth_stencil
->width
, fb
->depth_stencil
->height
);
655 if (ds
== swapchain
->device
->onscreen_depth_stencil
)
657 wined3d_texture_decref(swapchain
->device
->onscreen_depth_stencil
->container
);
658 swapchain
->device
->onscreen_depth_stencil
= NULL
;
659 #endif /* STAGING_CSMT */
664 context_release(context
);
667 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
669 swapchain_gl_present
,
672 /* Helper function that blits the front buffer contents to the target window. */
673 void x11_copy_to_screen(const struct wined3d_swapchain
*swapchain
, const RECT
*rect
)
675 struct wined3d_surface
*front
;
676 POINT offset
= {0, 0};
681 TRACE("swapchain %p, rect %s.\n", swapchain
, wine_dbgstr_rect(rect
));
683 front
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
684 if (swapchain
->palette
)
685 wined3d_palette_apply_to_dc(swapchain
->palette
, front
->hDC
);
687 if (front
->resource
.map_count
)
688 ERR("Trying to blit a mapped surface.\n");
690 TRACE("Copying surface %p to screen.\n", front
);
692 #if defined(STAGING_CSMT)
693 wined3d_resource_load_location(&front
->resource
, NULL
, WINED3D_LOCATION_DIB
);
694 #else /* STAGING_CSMT */
695 surface_load_location(front
, NULL
, WINED3D_LOCATION_DIB
);
696 #endif /* STAGING_CSMT */
699 window
= swapchain
->win_handle
;
700 dst_dc
= GetDCEx(window
, 0, DCX_CLIPSIBLINGS
| DCX_CACHE
);
702 /* Front buffer coordinates are screen coordinates. Map them to the
703 * destination window if not fullscreened. */
704 if (swapchain
->desc
.windowed
)
705 ClientToScreen(window
, &offset
);
707 TRACE("offset %s.\n", wine_dbgstr_point(&offset
));
710 draw_rect
.right
= front
->resource
.width
;
712 draw_rect
.bottom
= front
->resource
.height
;
715 IntersectRect(&draw_rect
, &draw_rect
, rect
);
717 BitBlt(dst_dc
, draw_rect
.left
- offset
.x
, draw_rect
.top
- offset
.y
,
718 draw_rect
.right
- draw_rect
.left
, draw_rect
.bottom
- draw_rect
.top
,
719 src_dc
, draw_rect
.left
, draw_rect
.top
, SRCCOPY
);
720 ReleaseDC(window
, dst_dc
);
723 static void swapchain_gdi_present(struct wined3d_swapchain
*swapchain
, const RECT
*src_rect_in
,
724 #if defined(STAGING_CSMT)
725 const RECT
*dst_rect_in
, const RGNDATA
*dirty_region
, DWORD flags
,
726 struct wined3d_surface
*depth_stencil
)
727 #else /* STAGING_CSMT */
728 const RECT
*dst_rect_in
, const RGNDATA
*dirty_region
, DWORD flags
)
729 #endif /* STAGING_CSMT */
731 struct wined3d_surface
*front
, *back
;
733 front
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
734 back
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->back_buffers
[0], 0));
740 front
->hDC
= back
->hDC
;
744 /* Flip the DIBsection. */
747 tmp
= front
->dib
.DIBsection
;
748 front
->dib
.DIBsection
= back
->dib
.DIBsection
;
749 back
->dib
.DIBsection
= tmp
;
752 /* Flip the surface data. */
756 #if defined(STAGING_CSMT)
757 tmp
= front
->resource
.bitmap_data
;
758 front
->resource
.bitmap_data
= back
->resource
.bitmap_data
;
759 back
->resource
.bitmap_data
= tmp
;
760 #else /* STAGING_CSMT */
761 tmp
= front
->dib
.bitmap_data
;
762 front
->dib
.bitmap_data
= back
->dib
.bitmap_data
;
763 back
->dib
.bitmap_data
= tmp
;
764 #endif /* STAGING_CSMT */
766 if (front
->resource
.heap_memory
)
767 ERR("GDI Surface %p has heap memory allocated.\n", front
);
769 if (back
->resource
.heap_memory
)
770 ERR("GDI Surface %p has heap memory allocated.\n", back
);
776 static LONG prev_time
, frames
;
777 DWORD time
= GetTickCount();
781 /* every 1.5 seconds */
782 if (time
- prev_time
> 1500)
784 TRACE_(fps
)("@ approx %.2ffps\n", 1000.0 * frames
/ (time
- prev_time
));
790 x11_copy_to_screen(swapchain
, NULL
);
793 static const struct wined3d_swapchain_ops swapchain_gdi_ops
=
795 swapchain_gdi_present
,
798 static void swapchain_update_render_to_fbo(struct wined3d_swapchain
*swapchain
)
802 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
805 if (!swapchain
->desc
.backbuffer_count
)
807 TRACE("Single buffered rendering.\n");
808 swapchain
->render_to_fbo
= FALSE
;
812 GetClientRect(swapchain
->win_handle
, &client_rect
);
814 TRACE("Backbuffer %ux%u, window %ux%u.\n",
815 swapchain
->desc
.backbuffer_width
,
816 swapchain
->desc
.backbuffer_height
,
817 client_rect
.right
, client_rect
.bottom
);
818 TRACE("Multisample type %#x, quality %#x.\n",
819 swapchain
->desc
.multisample_type
,
820 swapchain
->desc
.multisample_quality
);
822 if (!wined3d_settings
.always_offscreen
&& !swapchain
->desc
.multisample_type
823 && swapchain
->desc
.backbuffer_width
== client_rect
.right
824 && swapchain
->desc
.backbuffer_height
== client_rect
.bottom
)
826 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
827 swapchain
->render_to_fbo
= FALSE
;
831 TRACE("Rendering to FBO.\n");
832 swapchain
->render_to_fbo
= TRUE
;
835 #if defined(STAGING_CSMT)
836 HRESULT
swapchain_create_context_cs(struct wined3d_device
*device
, struct wined3d_swapchain
*swapchain
)
838 const struct wined3d_adapter
*adapter
= device
->adapter
;
840 static const enum wined3d_format_id formats
[] =
842 WINED3DFMT_D24_UNORM_S8_UINT
,
843 WINED3DFMT_D32_UNORM
,
844 WINED3DFMT_R24_UNORM_X8_TYPELESS
,
845 WINED3DFMT_D16_UNORM
,
846 WINED3DFMT_S1_UINT_D15_UNORM
848 struct wined3d_surface
*front_buffer
;
849 const struct wined3d_gl_info
*gl_info
= &adapter
->gl_info
;
851 swapchain
->context
= HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain
->context
));
852 if (!swapchain
->context
)
854 ERR("Failed to create the context array.\n");
855 return E_OUTOFMEMORY
;
857 swapchain
->num_contexts
= 1;
859 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
860 * You are able to add a depth + stencil surface at a later stage when you need it.
861 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
862 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
863 * context, need torecreate shaders, textures and other resources.
865 * The context manager already takes care of the state problem and for the other tasks code from Reset
866 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
867 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
868 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
869 * issue needs to be fixed. */
870 front_buffer
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
871 for (i
= 0; i
< (sizeof(formats
) / sizeof(*formats
)); i
++)
873 swapchain
->ds_format
= wined3d_get_format(gl_info
, formats
[i
]);
874 swapchain
->context
[0] = context_create(swapchain
, front_buffer
, swapchain
->ds_format
);
875 if (swapchain
->context
[0]) break;
876 TRACE("Depth stencil format %s is not supported, trying next format\n",
877 debug_d3dformat(formats
[i
]));
880 if (!swapchain
->context
[0])
882 WARN("Failed to create context.\n");
883 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
884 swapchain
->context
= NULL
;
885 return WINED3DERR_NOTAVAILABLE
;
888 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
889 && (!swapchain
->desc
.enable_auto_depth_stencil
890 || swapchain
->desc
.auto_depth_stencil_format
!= swapchain
->ds_format
->id
))
892 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
894 context_release(swapchain
->context
[0]);
899 #endif /* STAGING_CSMT */
900 static HRESULT
swapchain_init(struct wined3d_swapchain
*swapchain
, struct wined3d_device
*device
,
901 struct wined3d_swapchain_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
903 const struct wined3d_adapter
*adapter
= device
->adapter
;
904 struct wined3d_resource_desc texture_desc
;
905 struct wined3d_surface
*front_buffer
;
906 BOOL displaymode_set
= FALSE
;
912 if (desc
->backbuffer_count
> 1)
914 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
915 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
918 if (device
->wined3d
->flags
& WINED3D_NO3D
)
919 swapchain
->swapchain_ops
= &swapchain_gdi_ops
;
921 swapchain
->swapchain_ops
= &swapchain_gl_ops
;
923 window
= desc
->device_window
? desc
->device_window
: device
->create_parms
.focus_window
;
925 swapchain
->device
= device
;
926 swapchain
->parent
= parent
;
927 swapchain
->parent_ops
= parent_ops
;
929 swapchain
->win_handle
= window
;
930 swapchain
->device_window
= window
;
932 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
,
933 adapter
->ordinal
, &swapchain
->original_mode
, NULL
)))
935 ERR("Failed to get current display mode, hr %#x.\n", hr
);
939 GetClientRect(window
, &client_rect
);
941 && (!desc
->backbuffer_width
|| !desc
->backbuffer_height
942 || desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
))
945 if (!desc
->backbuffer_width
)
947 desc
->backbuffer_width
= client_rect
.right
;
948 TRACE("Updating width to %u.\n", desc
->backbuffer_width
);
951 if (!desc
->backbuffer_height
)
953 desc
->backbuffer_height
= client_rect
.bottom
;
954 TRACE("Updating height to %u.\n", desc
->backbuffer_height
);
957 if (desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
)
959 desc
->backbuffer_format
= swapchain
->original_mode
.format_id
;
960 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain
->original_mode
.format_id
));
963 swapchain
->desc
= *desc
;
964 swapchain_update_render_to_fbo(swapchain
);
966 TRACE("Creating front buffer.\n");
968 texture_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
969 texture_desc
.format
= swapchain
->desc
.backbuffer_format
;
970 texture_desc
.multisample_type
= swapchain
->desc
.multisample_type
;
971 texture_desc
.multisample_quality
= swapchain
->desc
.multisample_quality
;
972 texture_desc
.usage
= 0;
973 texture_desc
.pool
= WINED3D_POOL_DEFAULT
;
974 texture_desc
.width
= swapchain
->desc
.backbuffer_width
;
975 texture_desc
.height
= swapchain
->desc
.backbuffer_height
;
976 texture_desc
.depth
= 1;
977 texture_desc
.size
= 0;
979 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
980 parent
, &texture_desc
, &swapchain
->front_buffer
)))
982 WARN("Failed to create front buffer, hr %#x.\n", hr
);
986 wined3d_texture_set_swapchain(swapchain
->front_buffer
, swapchain
);
987 front_buffer
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
988 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
990 #if defined(STAGING_CSMT)
991 wined3d_resource_validate_location(&front_buffer
->resource
, WINED3D_LOCATION_DRAWABLE
);
992 wined3d_resource_invalidate_location(&front_buffer
->resource
, ~WINED3D_LOCATION_DRAWABLE
);
993 #else /* STAGING_CSMT */
994 surface_validate_location(front_buffer
, WINED3D_LOCATION_DRAWABLE
);
995 surface_invalidate_location(front_buffer
, ~WINED3D_LOCATION_DRAWABLE
);
996 #endif /* STAGING_CSMT */
999 /* MSDN says we're only allowed a single fullscreen swapchain per device,
1000 * so we should really check to see if there is a fullscreen swapchain
1001 * already. Does a single head count as full screen? */
1003 if (!desc
->windowed
)
1005 /* Change the display settings */
1006 swapchain
->d3d_mode
.width
= desc
->backbuffer_width
;
1007 swapchain
->d3d_mode
.height
= desc
->backbuffer_height
;
1008 swapchain
->d3d_mode
.format_id
= desc
->backbuffer_format
;
1009 swapchain
->d3d_mode
.refresh_rate
= desc
->refresh_rate
;
1010 swapchain
->d3d_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
1012 if (FAILED(hr
= wined3d_set_adapter_display_mode(device
->wined3d
, adapter
->ordinal
, &swapchain
->d3d_mode
)))
1014 WARN("Failed to set display mode, hr %#x.\n", hr
);
1017 displaymode_set
= TRUE
;
1020 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
1022 #if defined(STAGING_CSMT)
1023 hr
= wined3d_cs_emit_create_swapchain_context(device
->cs
, swapchain
);
1026 #else /* STAGING_CSMT */
1027 static const enum wined3d_format_id formats
[] =
1029 WINED3DFMT_D24_UNORM_S8_UINT
,
1030 WINED3DFMT_D32_UNORM
,
1031 WINED3DFMT_R24_UNORM_X8_TYPELESS
,
1032 WINED3DFMT_D16_UNORM
,
1033 WINED3DFMT_S1_UINT_D15_UNORM
1036 const struct wined3d_gl_info
*gl_info
= &adapter
->gl_info
;
1038 swapchain
->context
= HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain
->context
));
1039 if (!swapchain
->context
)
1041 ERR("Failed to create the context array.\n");
1045 swapchain
->num_contexts
= 1;
1047 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
1048 * You are able to add a depth + stencil surface at a later stage when you need it.
1049 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
1050 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
1051 * context, need torecreate shaders, textures and other resources.
1053 * The context manager already takes care of the state problem and for the other tasks code from Reset
1054 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
1055 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
1056 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
1057 * issue needs to be fixed. */
1058 for (i
= 0; i
< (sizeof(formats
) / sizeof(*formats
)); i
++)
1060 swapchain
->ds_format
= wined3d_get_format(gl_info
, formats
[i
]);
1061 swapchain
->context
[0] = context_create(swapchain
, front_buffer
, swapchain
->ds_format
);
1062 if (swapchain
->context
[0]) break;
1063 TRACE("Depth stencil format %s is not supported, trying next format\n",
1064 debug_d3dformat(formats
[i
]));
1067 if (!swapchain
->context
[0])
1069 WARN("Failed to create context.\n");
1070 hr
= WINED3DERR_NOTAVAILABLE
;
1074 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
1075 && (!desc
->enable_auto_depth_stencil
1076 || swapchain
->desc
.auto_depth_stencil_format
!= swapchain
->ds_format
->id
))
1078 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
1080 context_release(swapchain
->context
[0]);
1081 #endif /* STAGING_CSMT */
1084 if (swapchain
->desc
.backbuffer_count
> 0)
1086 swapchain
->back_buffers
= HeapAlloc(GetProcessHeap(), 0,
1087 sizeof(*swapchain
->back_buffers
) * swapchain
->desc
.backbuffer_count
);
1088 if (!swapchain
->back_buffers
)
1090 ERR("Failed to allocate backbuffer array memory.\n");
1095 texture_desc
.usage
|= WINED3DUSAGE_RENDERTARGET
;
1096 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1098 TRACE("Creating back buffer %u.\n", i
);
1099 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1100 parent
, &texture_desc
, &swapchain
->back_buffers
[i
])))
1102 WARN("Failed to create back buffer %u, hr %#x.\n", i
, hr
);
1103 swapchain
->desc
.backbuffer_count
= i
;
1106 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], swapchain
);
1110 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1111 if (desc
->enable_auto_depth_stencil
&& !(device
->wined3d
->flags
& WINED3D_NO3D
))
1113 TRACE("Creating depth/stencil buffer.\n");
1114 if (!device
->auto_depth_stencil_view
)
1116 struct wined3d_texture
*ds
;
1117 struct wined3d_rendertarget_view_desc desc
;
1119 texture_desc
.format
= swapchain
->desc
.auto_depth_stencil_format
;
1120 texture_desc
.usage
= WINED3DUSAGE_DEPTHSTENCIL
;
1122 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1123 device
->device_parent
, &texture_desc
, &ds
)))
1125 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr
);
1129 desc
.format_id
= ds
->resource
.format
->id
;
1130 desc
.u
.texture
.level_idx
= 0;
1131 desc
.u
.texture
.layer_idx
= 0;
1132 desc
.u
.texture
.layer_count
= 1;
1133 hr
= wined3d_rendertarget_view_create(&desc
, &ds
->resource
, NULL
, &wined3d_null_parent_ops
,
1134 &device
->auto_depth_stencil_view
);
1135 wined3d_texture_decref(ds
);
1138 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
1144 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
1149 if (displaymode_set
)
1151 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
1152 adapter
->ordinal
, &swapchain
->original_mode
)))
1153 ERR("Failed to restore display mode.\n");
1157 if (swapchain
->back_buffers
)
1159 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1161 if (swapchain
->back_buffers
[i
])
1163 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
1164 wined3d_texture_decref(swapchain
->back_buffers
[i
]);
1167 HeapFree(GetProcessHeap(), 0, swapchain
->back_buffers
);
1170 if (swapchain
->context
)
1172 if (swapchain
->context
[0])
1174 context_release(swapchain
->context
[0]);
1175 context_destroy(device
, swapchain
->context
[0]);
1176 swapchain
->num_contexts
= 0;
1178 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1181 if (swapchain
->front_buffer
)
1183 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
1184 wined3d_texture_decref(swapchain
->front_buffer
);
1190 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
, struct wined3d_swapchain_desc
*desc
,
1191 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_swapchain
**swapchain
)
1193 struct wined3d_swapchain
*object
;
1196 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1197 device
, desc
, parent
, parent_ops
, swapchain
);
1199 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1201 return E_OUTOFMEMORY
;
1203 hr
= swapchain_init(object
, device
, desc
, parent
, parent_ops
);
1206 WARN("Failed to initialize swapchain, hr %#x.\n", hr
);
1207 HeapFree(GetProcessHeap(), 0, object
);
1211 TRACE("Created swapchain %p.\n", object
);
1212 *swapchain
= object
;
1217 static struct wined3d_context
*swapchain_create_context(struct wined3d_swapchain
*swapchain
)
1219 struct wined3d_context
**newArray
;
1220 struct wined3d_context
*ctx
;
1222 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain
, GetCurrentThreadId());
1224 if (!(ctx
= context_create(swapchain
,
1225 surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0)),
1226 swapchain
->ds_format
)))
1228 ERR("Failed to create a new context for the swapchain\n");
1231 context_release(ctx
);
1233 newArray
= HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray
) * (swapchain
->num_contexts
+ 1));
1235 ERR("Out of memory when trying to allocate a new context array\n");
1236 context_destroy(swapchain
->device
, ctx
);
1239 memcpy(newArray
, swapchain
->context
, sizeof(*newArray
) * swapchain
->num_contexts
);
1240 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1241 newArray
[swapchain
->num_contexts
] = ctx
;
1242 swapchain
->context
= newArray
;
1243 swapchain
->num_contexts
++;
1245 TRACE("Returning context %p\n", ctx
);
1249 void swapchain_destroy_contexts(struct wined3d_swapchain
*swapchain
)
1253 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1255 context_destroy(swapchain
->device
, swapchain
->context
[i
]);
1257 swapchain
->num_contexts
= 0;
1260 struct wined3d_context
*swapchain_get_context(struct wined3d_swapchain
*swapchain
)
1262 DWORD tid
= GetCurrentThreadId();
1265 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1267 if (swapchain
->context
[i
]->tid
== tid
)
1268 return swapchain
->context
[i
];
1271 /* Create a new context for the thread */
1272 return swapchain_create_context(swapchain
);
1275 HDC
swapchain_get_backup_dc(struct wined3d_swapchain
*swapchain
)
1277 if (!swapchain
->backup_dc
)
1279 TRACE("Creating the backup window for swapchain %p.\n", swapchain
);
1281 if (!(swapchain
->backup_wnd
= CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, "WineD3D fake window",
1282 WS_OVERLAPPEDWINDOW
, 10, 10, 10, 10, NULL
, NULL
, NULL
, NULL
)))
1284 ERR("Failed to create a window.\n");
1288 if (!(swapchain
->backup_dc
= GetDC(swapchain
->backup_wnd
)))
1290 ERR("Failed to get a DC.\n");
1291 DestroyWindow(swapchain
->backup_wnd
);
1292 swapchain
->backup_wnd
= NULL
;
1297 return swapchain
->backup_dc
;
1300 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1304 wined3d_resource_update_draw_binding(&swapchain
->front_buffer
->resource
);
1306 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1308 wined3d_resource_update_draw_binding(&swapchain
->back_buffers
[i
]->resource
);
1312 void wined3d_swapchain_activate(struct wined3d_swapchain
*swapchain
, BOOL activate
)
1314 struct wined3d_device
*device
= swapchain
->device
;
1315 BOOL filter_messages
= device
->filter_messages
;
1317 /* This code is not protected by the wined3d mutex, so it may run while
1318 * wined3d_device_reset is active. Testing on Windows shows that changing
1319 * focus during resets and resetting during focus change events causes
1320 * the application to crash with an invalid memory access. */
1322 device
->filter_messages
= !(device
->wined3d
->flags
& WINED3D_FOCUS_MESSAGES
);
1326 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
))
1328 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1329 * the window but leaves the size untouched, d3d9 sets the size on an
1330 * invisible window, generates messages but doesn't change the window
1331 * properties. The implementation follows d3d9.
1333 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1334 * resume drawing after a focus loss. */
1335 SetWindowPos(swapchain
->device_window
, NULL
, 0, 0,
1336 swapchain
->desc
.backbuffer_width
, swapchain
->desc
.backbuffer_height
,
1337 SWP_NOACTIVATE
| SWP_NOZORDER
);
1340 if (device
->wined3d
->flags
& WINED3D_RESTORE_MODE_ON_ACTIVATE
)
1342 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
1343 device
->adapter
->ordinal
, &swapchain
->d3d_mode
)))
1344 ERR("Failed to set display mode.\n");
1349 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
1350 device
->adapter
->ordinal
, NULL
)))
1351 ERR("Failed to set display mode.\n");
1353 swapchain
->reapply_mode
= TRUE
;
1355 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
)
1356 && IsWindowVisible(swapchain
->device_window
))
1357 ShowWindow(swapchain
->device_window
, SW_MINIMIZE
);
1360 device
->filter_messages
= filter_messages
;
1363 HRESULT CDECL
wined3d_swapchain_resize_buffers(struct wined3d_swapchain
*swapchain
, unsigned int buffer_count
,
1364 unsigned int width
, unsigned int height
, enum wined3d_format_id format_id
,
1365 enum wined3d_multisample_type multisample_type
, unsigned int multisample_quality
)
1367 BOOL update_desc
= FALSE
;
1369 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1370 "multisample_type %#x, multisample_quality %#x.\n",
1371 swapchain
, buffer_count
, width
, height
, debug_d3dformat(format_id
),
1372 multisample_type
, multisample_quality
);
1374 if (buffer_count
&& buffer_count
!= swapchain
->desc
.backbuffer_count
)
1375 FIXME("Cannot change the back buffer count yet.\n");
1377 if (!width
|| !height
)
1379 /* The application is requesting that either the swapchain width or
1380 * height be set to the corresponding dimension in the window's
1385 if (!swapchain
->desc
.windowed
)
1386 return WINED3DERR_INVALIDCALL
;
1388 if (!GetClientRect(swapchain
->device_window
, &client_rect
))
1390 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1391 return WINED3DERR_INVALIDCALL
;
1395 width
= client_rect
.right
;
1398 height
= client_rect
.bottom
;
1401 if (width
!= swapchain
->desc
.backbuffer_width
1402 || height
!= swapchain
->desc
.backbuffer_height
)
1404 swapchain
->desc
.backbuffer_width
= width
;
1405 swapchain
->desc
.backbuffer_height
= height
;
1409 if (format_id
== WINED3DFMT_UNKNOWN
)
1411 if (!swapchain
->desc
.windowed
)
1412 return WINED3DERR_INVALIDCALL
;
1413 format_id
= swapchain
->original_mode
.format_id
;
1416 if (format_id
!= swapchain
->desc
.backbuffer_format
)
1418 swapchain
->desc
.backbuffer_format
= format_id
;
1422 if (multisample_type
!= swapchain
->desc
.multisample_type
1423 || multisample_quality
!= swapchain
->desc
.multisample_quality
)
1425 swapchain
->desc
.multisample_type
= multisample_type
;
1426 swapchain
->desc
.multisample_quality
= multisample_quality
;
1435 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->front_buffer
, swapchain
->desc
.backbuffer_width
,
1436 swapchain
->desc
.backbuffer_height
, swapchain
->desc
.backbuffer_format
,
1437 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
, NULL
, 0)))
1440 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1442 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->back_buffers
[i
], swapchain
->desc
.backbuffer_width
,
1443 swapchain
->desc
.backbuffer_height
, swapchain
->desc
.backbuffer_format
,
1444 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
, NULL
, 0)))
1449 swapchain_update_render_to_fbo(swapchain
);
1450 swapchain_update_draw_bindings(swapchain
);