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
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
28 WINE_DECLARE_DEBUG_CHANNEL(fps
);
30 static void swapchain_cleanup(struct wined3d_swapchain
*swapchain
)
35 TRACE("Destroying swapchain %p.\n", swapchain
);
37 wined3d_swapchain_set_gamma_ramp(swapchain
, 0, &swapchain
->orig_gamma
);
39 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
40 * is the last buffer to be destroyed, FindContext() depends on that. */
41 if (swapchain
->front_buffer
)
43 surface_set_swapchain(swapchain
->front_buffer
, NULL
);
44 if (wined3d_surface_decref(swapchain
->front_buffer
))
45 WARN("Something's still holding the front buffer (%p).\n", swapchain
->front_buffer
);
46 swapchain
->front_buffer
= NULL
;
49 if (swapchain
->back_buffers
)
51 i
= swapchain
->desc
.backbuffer_count
;
55 surface_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
56 if (wined3d_surface_decref(swapchain
->back_buffers
[i
]))
57 WARN("Something's still holding back buffer %u (%p).\n", i
, swapchain
->back_buffers
[i
]);
59 HeapFree(GetProcessHeap(), 0, swapchain
->back_buffers
);
60 swapchain
->back_buffers
= NULL
;
63 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
65 context_destroy(swapchain
->device
, swapchain
->context
[i
]);
67 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
69 /* Restore the screen resolution if we rendered in fullscreen.
70 * This will restore the screen resolution to what it was before creating
71 * the swapchain. In case of d3d8 and d3d9 this will be the original
72 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
73 * sets the resolution before starting up Direct3D, thus orig_width and
74 * orig_height will be equal to the modes in the presentation params. */
75 if (!swapchain
->desc
.windowed
&& swapchain
->desc
.auto_restore_display_mode
)
77 if (FAILED(hr
= wined3d_set_adapter_display_mode(swapchain
->device
->wined3d
,
78 swapchain
->device
->adapter
->ordinal
, &swapchain
->original_mode
)))
79 ERR("Failed to restore display mode, hr %#x.\n", hr
);
82 if (swapchain
->backup_dc
)
84 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain
->backup_wnd
, swapchain
->backup_dc
);
86 wined3d_release_dc(swapchain
->backup_wnd
, swapchain
->backup_dc
);
87 DestroyWindow(swapchain
->backup_wnd
);
91 ULONG CDECL
wined3d_swapchain_incref(struct wined3d_swapchain
*swapchain
)
93 ULONG refcount
= InterlockedIncrement(&swapchain
->ref
);
95 TRACE("%p increasing refcount to %u.\n", swapchain
, refcount
);
100 ULONG CDECL
wined3d_swapchain_decref(struct wined3d_swapchain
*swapchain
)
102 ULONG refcount
= InterlockedDecrement(&swapchain
->ref
);
104 TRACE("%p decreasing refcount to %u.\n", swapchain
, refcount
);
108 swapchain_cleanup(swapchain
);
109 swapchain
->parent_ops
->wined3d_object_destroyed(swapchain
->parent
);
110 HeapFree(GetProcessHeap(), 0, swapchain
);
116 void * CDECL
wined3d_swapchain_get_parent(const struct wined3d_swapchain
*swapchain
)
118 TRACE("swapchain %p.\n", swapchain
);
120 return swapchain
->parent
;
123 void CDECL
wined3d_swapchain_set_window(struct wined3d_swapchain
*swapchain
, HWND window
)
126 window
= swapchain
->device_window
;
127 if (window
== swapchain
->win_handle
)
130 TRACE("Setting swapchain %p window from %p to %p.\n",
131 swapchain
, swapchain
->win_handle
, window
);
132 swapchain
->win_handle
= window
;
135 HRESULT CDECL
wined3d_swapchain_present(struct wined3d_swapchain
*swapchain
,
136 const RECT
*src_rect
, const RECT
*dst_rect
, HWND dst_window_override
,
137 const RGNDATA
*dirty_region
, DWORD flags
)
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
);
144 FIXME("Ignoring flags %#x.\n", flags
);
146 if (!swapchain
->back_buffers
)
148 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
149 return WINED3DERR_INVALIDCALL
;
152 wined3d_cs_emit_present(swapchain
->device
->cs
, swapchain
, src_rect
,
153 dst_rect
, dst_window_override
, dirty_region
, flags
);
158 HRESULT CDECL
wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain
*swapchain
,
159 struct wined3d_surface
*dst_surface
)
161 struct wined3d_surface
*src_surface
;
162 RECT src_rect
, dst_rect
;
164 TRACE("swapchain %p, dst_surface %p.\n", swapchain
, dst_surface
);
166 src_surface
= swapchain
->front_buffer
;
167 SetRect(&src_rect
, 0, 0, src_surface
->resource
.width
, src_surface
->resource
.height
);
170 if (swapchain
->desc
.windowed
)
172 MapWindowPoints(swapchain
->win_handle
, NULL
, (POINT
*)&dst_rect
, 2);
173 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
174 wine_dbgstr_rect(&dst_rect
));
177 return wined3d_surface_blt(dst_surface
, &dst_rect
, src_surface
, &src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
180 struct wined3d_surface
* CDECL
wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain
*swapchain
,
181 UINT back_buffer_idx
, enum wined3d_backbuffer_type type
)
183 TRACE("swapchain %p, back_buffer_idx %u, type %#x.\n",
184 swapchain
, back_buffer_idx
, type
);
186 /* Return invalid if there is no backbuffer array, otherwise it will
187 * crash when ddraw is used (there swapchain->back_buffers is always
188 * NULL). We need this because this function is called from
189 * stateblock_init_default_state() to get the default scissorrect
191 if (!swapchain
->back_buffers
|| back_buffer_idx
>= swapchain
->desc
.backbuffer_count
)
193 WARN("Invalid back buffer index.\n");
194 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
195 * here in wined3d to avoid problems in other libs. */
199 TRACE("Returning back buffer %p.\n", swapchain
->back_buffers
[back_buffer_idx
]);
201 return swapchain
->back_buffers
[back_buffer_idx
];
204 HRESULT CDECL
wined3d_swapchain_get_raster_status(const struct wined3d_swapchain
*swapchain
,
205 struct wined3d_raster_status
*raster_status
)
207 TRACE("swapchain %p, raster_status %p.\n", swapchain
, raster_status
);
209 return wined3d_get_adapter_raster_status(swapchain
->device
->wined3d
,
210 swapchain
->device
->adapter
->ordinal
, raster_status
);
213 HRESULT CDECL
wined3d_swapchain_get_display_mode(const struct wined3d_swapchain
*swapchain
,
214 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
218 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain
, mode
, rotation
);
220 hr
= wined3d_get_adapter_display_mode(swapchain
->device
->wined3d
,
221 swapchain
->device
->adapter
->ordinal
, mode
, rotation
);
223 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
224 mode
->width
, mode
->height
, mode
->refresh_rate
, debug_d3dformat(mode
->format_id
));
229 struct wined3d_device
* CDECL
wined3d_swapchain_get_device(const struct wined3d_swapchain
*swapchain
)
231 TRACE("swapchain %p.\n", swapchain
);
233 return swapchain
->device
;
236 void CDECL
wined3d_swapchain_get_desc(const struct wined3d_swapchain
*swapchain
,
237 struct wined3d_swapchain_desc
*desc
)
239 TRACE("swapchain %p, desc %p.\n", swapchain
, desc
);
241 *desc
= swapchain
->desc
;
244 HRESULT CDECL
wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
245 DWORD flags
, const struct wined3d_gamma_ramp
*ramp
)
249 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain
, flags
, ramp
);
252 FIXME("Ignoring flags %#x.\n", flags
);
254 dc
= GetDC(swapchain
->device_window
);
255 SetDeviceGammaRamp(dc
, (void *)ramp
);
256 ReleaseDC(swapchain
->device_window
, dc
);
261 HRESULT CDECL
wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
262 struct wined3d_gamma_ramp
*ramp
)
266 TRACE("swapchain %p, ramp %p.\n", swapchain
, ramp
);
268 dc
= GetDC(swapchain
->device_window
);
269 GetDeviceGammaRamp(dc
, ramp
);
270 ReleaseDC(swapchain
->device_window
, dc
);
275 /* A GL context is provided by the caller */
276 static void swapchain_blit(const struct wined3d_swapchain
*swapchain
,
277 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
279 struct wined3d_surface
*backbuffer
= swapchain
->back_buffers
[0];
280 UINT src_w
= src_rect
->right
- src_rect
->left
;
281 UINT src_h
= src_rect
->bottom
- src_rect
->top
;
283 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
287 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
288 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
290 if (src_w
== dst_rect
->right
- dst_rect
->left
&& src_h
== dst_rect
->bottom
- dst_rect
->top
)
291 gl_filter
= GL_NEAREST
;
293 gl_filter
= GL_LINEAR
;
295 GetClientRect(swapchain
->win_handle
, &win_rect
);
296 win_h
= win_rect
.bottom
- win_rect
.top
;
298 if (gl_info
->fbo_ops
.glBlitFramebuffer
&& is_identity_fixup(backbuffer
->resource
.format
->color_fixup
))
300 DWORD location
= SFLAG_INTEXTURE
;
302 if (backbuffer
->resource
.multisample_type
)
304 location
= SFLAG_INRB_RESOLVED
;
305 surface_load_location(backbuffer
, location
);
308 context_apply_fbo_state_blit(context
, GL_READ_FRAMEBUFFER
, backbuffer
, NULL
, location
);
309 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_COLOR_ATTACHMENT0
);
310 context_check_fbo_status(context
, GL_READ_FRAMEBUFFER
);
312 context_apply_fbo_state_blit(context
, GL_DRAW_FRAMEBUFFER
, swapchain
->front_buffer
, NULL
, SFLAG_INDRAWABLE
);
313 context_set_draw_buffer(context
, GL_BACK
);
314 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
316 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
317 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
318 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
319 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
320 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
322 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
323 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
325 /* Note that the texture is upside down */
326 gl_info
->fbo_ops
.glBlitFramebuffer(src_rect
->left
, src_rect
->top
, src_rect
->right
, src_rect
->bottom
,
327 dst_rect
->left
, win_h
- dst_rect
->top
, dst_rect
->right
, win_h
- dst_rect
->bottom
,
328 GL_COLOR_BUFFER_BIT
, gl_filter
);
329 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
333 struct wined3d_device
*device
= swapchain
->device
;
334 struct wined3d_context
*context2
;
335 float tex_left
= src_rect
->left
;
336 float tex_top
= src_rect
->top
;
337 float tex_right
= src_rect
->right
;
338 float tex_bottom
= src_rect
->bottom
;
340 context2
= context_acquire(device
, swapchain
->back_buffers
[0]);
341 context_apply_blit_state(context2
, device
);
343 if (backbuffer
->flags
& SFLAG_NORMCOORD
)
351 if (is_complex_fixup(backbuffer
->resource
.format
->color_fixup
))
352 gl_filter
= GL_NEAREST
;
354 context_apply_fbo_state_blit(context2
, GL_FRAMEBUFFER
, swapchain
->front_buffer
, NULL
, SFLAG_INDRAWABLE
);
355 context_bind_texture(context2
, backbuffer
->texture_target
, backbuffer
->container
->texture_rgb
.name
);
357 /* Set up the texture. The surface is not in a wined3d_texture
358 * container, so there are no D3D texture settings to dirtify. */
359 device
->blitter
->set_shader(device
->blit_priv
, context2
, backbuffer
);
360 gl_info
->gl_ops
.gl
.p_glTexParameteri(backbuffer
->texture_target
, GL_TEXTURE_MIN_FILTER
, gl_filter
);
361 gl_info
->gl_ops
.gl
.p_glTexParameteri(backbuffer
->texture_target
, GL_TEXTURE_MAG_FILTER
, gl_filter
);
363 context_set_draw_buffer(context
, GL_BACK
);
365 /* Set the viewport to the destination rectandle, disable any projection
366 * transformation set up by context_apply_blit_state(), and draw a
367 * (-1,-1)-(1,1) quad.
369 * Back up viewport and matrix to avoid breaking last_was_blit
371 * Note that context_apply_blit_state() set up viewport and ortho to
372 * match the surface size - we want the GL drawable(=window) size. */
373 gl_info
->gl_ops
.gl
.p_glPushAttrib(GL_VIEWPORT_BIT
);
374 gl_info
->gl_ops
.gl
.p_glViewport(dst_rect
->left
, win_h
- dst_rect
->bottom
,
375 dst_rect
->right
, win_h
- dst_rect
->top
);
376 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
377 gl_info
->gl_ops
.gl
.p_glPushMatrix();
378 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
380 gl_info
->gl_ops
.gl
.p_glBegin(GL_QUADS
);
382 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_left
, tex_bottom
);
383 gl_info
->gl_ops
.gl
.p_glVertex2i(-1, -1);
386 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_left
, tex_top
);
387 gl_info
->gl_ops
.gl
.p_glVertex2i(-1, 1);
390 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_right
, tex_top
);
391 gl_info
->gl_ops
.gl
.p_glVertex2i(1, 1);
394 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_right
, tex_bottom
);
395 gl_info
->gl_ops
.gl
.p_glVertex2i(1, -1);
396 gl_info
->gl_ops
.gl
.p_glEnd();
398 gl_info
->gl_ops
.gl
.p_glPopMatrix();
399 gl_info
->gl_ops
.gl
.p_glPopAttrib();
401 device
->blitter
->unset_shader(context
->gl_info
);
402 checkGLcall("Swapchain present blit(manual)\n");
404 context_release(context2
);
408 static void swapchain_gl_present(struct wined3d_swapchain
*swapchain
, const RECT
*src_rect_in
,
409 const RECT
*dst_rect_in
, const RGNDATA
*dirty_region
, DWORD flags
)
411 struct wined3d_surface
*back_buffer
= swapchain
->back_buffers
[0];
412 const struct wined3d_fb_state
*fb
= &swapchain
->device
->fb
;
413 const struct wined3d_gl_info
*gl_info
;
414 struct wined3d_context
*context
;
415 RECT src_rect
, dst_rect
;
418 context
= context_acquire(swapchain
->device
, back_buffer
);
421 context_release(context
);
422 WARN("Invalid context, skipping present.\n");
426 gl_info
= context
->gl_info
;
428 if (swapchain
->device
->logo_texture
)
430 struct wined3d_surface
*src_surface
= surface_from_resource(
431 wined3d_texture_get_sub_resource(swapchain
->device
->logo_texture
, 0));
432 RECT rect
= {0, 0, src_surface
->resource
.width
, src_surface
->resource
.height
};
434 /* Blit the logo into the upper left corner of the drawable. */
435 wined3d_surface_blt(back_buffer
, &rect
, src_surface
, &rect
, WINEDDBLT_KEYSRC
,
436 NULL
, WINED3D_TEXF_POINT
);
439 if (swapchain
->device
->bCursorVisible
&& swapchain
->device
->cursor_texture
440 && !swapchain
->device
->hardwareCursor
)
442 struct wined3d_surface
*cursor
= surface_from_resource(
443 wined3d_texture_get_sub_resource(swapchain
->device
->cursor_texture
, 0));
446 swapchain
->device
->xScreenSpace
- swapchain
->device
->xHotSpot
,
447 swapchain
->device
->yScreenSpace
- swapchain
->device
->yHotSpot
,
448 swapchain
->device
->xScreenSpace
+ swapchain
->device
->cursorWidth
- swapchain
->device
->xHotSpot
,
449 swapchain
->device
->yScreenSpace
+ swapchain
->device
->cursorHeight
- swapchain
->device
->yHotSpot
,
452 TRACE("Rendering the software cursor.\n");
454 if (swapchain
->desc
.windowed
)
455 MapWindowPoints(NULL
, swapchain
->win_handle
, (POINT
*)&destRect
, 2);
456 wined3d_surface_blt(back_buffer
, &destRect
, cursor
, NULL
, WINEDDBLT_KEYSRC
,
457 NULL
, WINED3D_TEXF_POINT
);
460 TRACE("Presenting HDC %p.\n", context
->hdc
);
462 render_to_fbo
= swapchain
->render_to_fbo
;
466 src_rect
= *src_rect_in
;
467 if (!render_to_fbo
&& (src_rect
.left
|| src_rect
.top
468 || src_rect
.right
!= swapchain
->desc
.backbuffer_width
469 || src_rect
.bottom
!= swapchain
->desc
.backbuffer_height
))
471 render_to_fbo
= TRUE
;
478 src_rect
.right
= swapchain
->desc
.backbuffer_width
;
479 src_rect
.bottom
= swapchain
->desc
.backbuffer_height
;
483 dst_rect
= *dst_rect_in
;
485 GetClientRect(swapchain
->win_handle
, &dst_rect
);
487 if (!render_to_fbo
&& (dst_rect
.left
|| dst_rect
.top
488 || dst_rect
.right
!= swapchain
->desc
.backbuffer_width
489 || dst_rect
.bottom
!= swapchain
->desc
.backbuffer_height
))
490 render_to_fbo
= TRUE
;
492 /* Rendering to a window of different size, presenting partial rectangles,
493 * or rendering to a different window needs help from FBO_blit or a textured
494 * draw. Render the swapchain to a FBO in the future.
496 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
497 * all these issues - this fails if the window is smaller than the backbuffer.
499 if (!swapchain
->render_to_fbo
&& render_to_fbo
&& wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
501 surface_load_location(back_buffer
, SFLAG_INTEXTURE
);
502 surface_invalidate_location(back_buffer
, SFLAG_INDRAWABLE
);
503 swapchain
->render_to_fbo
= TRUE
;
504 swapchain_update_draw_bindings(swapchain
);
508 surface_load_location(back_buffer
, back_buffer
->draw_binding
);
511 if (swapchain
->render_to_fbo
)
513 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
514 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
515 * not allowed(they need the COPY swapeffect)
517 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
519 if (swapchain
->desc
.swap_effect
== WINED3D_SWAP_EFFECT_FLIP
)
520 FIXME("Render-to-fbo with WINED3D_SWAP_EFFECT_FLIP\n");
522 swapchain_blit(swapchain
, context
, &src_rect
, &dst_rect
);
525 if (swapchain
->num_contexts
> 1)
526 gl_info
->gl_ops
.gl
.p_glFinish();
528 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
529 gl_info
->gl_ops
.wgl
.p_wglSwapBuffers(context
->hdc
); /* TODO: cycle through the swapchain buffers */
531 TRACE("SwapBuffers called, Starting new frame\n");
535 DWORD time
= GetTickCount();
538 /* every 1.5 seconds */
539 if (time
- swapchain
->prev_time
> 1500)
541 TRACE_(fps
)("%p @ approx %.2ffps\n",
542 swapchain
, 1000.0 * swapchain
->frames
/ (time
- swapchain
->prev_time
));
543 swapchain
->prev_time
= time
;
544 swapchain
->frames
= 0;
548 if (!swapchain
->render_to_fbo
&& ((swapchain
->front_buffer
->flags
& SFLAG_INSYSMEM
)
549 || (back_buffer
->flags
& SFLAG_INSYSMEM
)))
551 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
552 * Doesn't work with render_to_fbo because we're not flipping
554 struct wined3d_surface
*front
= swapchain
->front_buffer
;
556 if (front
->resource
.size
== back_buffer
->resource
.size
)
558 flip_surface(front
, back_buffer
);
560 /* Tell the front buffer surface that is has been modified. However,
561 * the other locations were preserved during that, so keep the flags.
562 * This serves to update the emulated overlay, if any. */
563 surface_validate_location(front
, SFLAG_INDRAWABLE
);
567 surface_validate_location(front
, SFLAG_INDRAWABLE
);
568 surface_invalidate_location(front
, ~SFLAG_INDRAWABLE
);
569 surface_validate_location(back_buffer
, SFLAG_INDRAWABLE
);
570 surface_invalidate_location(back_buffer
, ~SFLAG_INDRAWABLE
);
575 surface_validate_location(swapchain
->front_buffer
, SFLAG_INDRAWABLE
);
576 surface_invalidate_location(swapchain
->front_buffer
, ~SFLAG_INDRAWABLE
);
577 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
578 * and INTEXTURE copies can keep their old content if they have any defined content.
579 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
580 * the texture / sysmem copy needs to be reloaded from the drawable
582 if (swapchain
->desc
.swap_effect
== WINED3D_SWAP_EFFECT_FLIP
)
584 surface_validate_location(back_buffer
, back_buffer
->draw_binding
);
585 surface_invalidate_location(back_buffer
, ~back_buffer
->draw_binding
);
589 if (fb
->depth_stencil
)
591 if (swapchain
->desc
.flags
& WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
592 || fb
->depth_stencil
->flags
& SFLAG_DISCARD
)
594 surface_modify_ds_location(fb
->depth_stencil
, SFLAG_DISCARDED
,
595 fb
->depth_stencil
->resource
.width
,
596 fb
->depth_stencil
->resource
.height
);
597 if (fb
->depth_stencil
== swapchain
->device
->onscreen_depth_stencil
)
599 wined3d_surface_decref(swapchain
->device
->onscreen_depth_stencil
);
600 swapchain
->device
->onscreen_depth_stencil
= NULL
;
605 context_release(context
);
608 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
610 swapchain_gl_present
,
613 /* Helper function that blits the front buffer contents to the target window. */
614 void x11_copy_to_screen(const struct wined3d_swapchain
*swapchain
, const RECT
*rect
)
616 const struct wined3d_surface
*front
;
617 POINT offset
= {0, 0};
622 TRACE("swapchain %p, rect %s.\n", swapchain
, wine_dbgstr_rect(rect
));
624 front
= swapchain
->front_buffer
;
625 if (!(front
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
))
628 if (front
->resource
.map_count
)
629 ERR("Trying to blit a mapped surface.\n");
631 TRACE("Copying surface %p to screen.\n", front
);
634 window
= swapchain
->win_handle
;
635 dst_dc
= GetDCEx(window
, 0, DCX_CLIPSIBLINGS
| DCX_CACHE
);
637 /* Front buffer coordinates are screen coordinates. Map them to the
638 * destination window if not fullscreened. */
639 if (swapchain
->desc
.windowed
)
640 ClientToScreen(window
, &offset
);
642 TRACE("offset %s.\n", wine_dbgstr_point(&offset
));
645 draw_rect
.right
= front
->resource
.width
;
647 draw_rect
.bottom
= front
->resource
.height
;
650 IntersectRect(&draw_rect
, &draw_rect
, rect
);
652 BitBlt(dst_dc
, draw_rect
.left
- offset
.x
, draw_rect
.top
- offset
.y
,
653 draw_rect
.right
- draw_rect
.left
, draw_rect
.bottom
- draw_rect
.top
,
654 src_dc
, draw_rect
.left
, draw_rect
.top
, SRCCOPY
);
655 ReleaseDC(window
, dst_dc
);
658 static void swapchain_gdi_present(struct wined3d_swapchain
*swapchain
, const RECT
*src_rect_in
,
659 const RECT
*dst_rect_in
, const RGNDATA
*dirty_region
, DWORD flags
)
661 struct wined3d_surface
*front
, *back
;
663 front
= swapchain
->front_buffer
;
664 back
= swapchain
->back_buffers
[0];
670 front
->hDC
= back
->hDC
;
674 /* Flip the DIBsection. */
677 tmp
= front
->dib
.DIBsection
;
678 front
->dib
.DIBsection
= back
->dib
.DIBsection
;
679 back
->dib
.DIBsection
= tmp
;
682 /* Flip the surface data. */
686 tmp
= front
->dib
.bitmap_data
;
687 front
->dib
.bitmap_data
= back
->dib
.bitmap_data
;
688 back
->dib
.bitmap_data
= tmp
;
690 tmp
= front
->resource
.allocatedMemory
;
691 front
->resource
.allocatedMemory
= back
->resource
.allocatedMemory
;
692 back
->resource
.allocatedMemory
= tmp
;
694 if (front
->resource
.heap_memory
)
695 ERR("GDI Surface %p has heap memory allocated.\n", front
);
697 if (back
->resource
.heap_memory
)
698 ERR("GDI Surface %p has heap memory allocated.\n", back
);
704 static LONG prev_time
, frames
;
705 DWORD time
= GetTickCount();
709 /* every 1.5 seconds */
710 if (time
- prev_time
> 1500)
712 TRACE_(fps
)("@ approx %.2ffps\n", 1000.0 * frames
/ (time
- prev_time
));
718 x11_copy_to_screen(swapchain
, NULL
);
721 static const struct wined3d_swapchain_ops swapchain_gdi_ops
=
723 swapchain_gdi_present
,
726 void swapchain_update_render_to_fbo(struct wined3d_swapchain
*swapchain
)
730 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
733 if (!swapchain
->desc
.backbuffer_count
)
735 TRACE("Single buffered rendering.\n");
736 swapchain
->render_to_fbo
= FALSE
;
740 GetClientRect(swapchain
->win_handle
, &client_rect
);
742 TRACE("Backbuffer %ux%u, window %ux%u.\n",
743 swapchain
->desc
.backbuffer_width
,
744 swapchain
->desc
.backbuffer_height
,
745 client_rect
.right
, client_rect
.bottom
);
746 TRACE("Multisample type %#x, quality %#x.\n",
747 swapchain
->desc
.multisample_type
,
748 swapchain
->desc
.multisample_quality
);
750 if (!wined3d_settings
.always_offscreen
&& !swapchain
->desc
.multisample_type
751 && swapchain
->desc
.backbuffer_width
== client_rect
.right
752 && swapchain
->desc
.backbuffer_height
== client_rect
.bottom
)
754 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
755 swapchain
->render_to_fbo
= FALSE
;
759 TRACE("Rendering to FBO.\n");
760 swapchain
->render_to_fbo
= TRUE
;
763 static HRESULT
swapchain_init(struct wined3d_swapchain
*swapchain
, struct wined3d_device
*device
,
764 struct wined3d_swapchain_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
766 const struct wined3d_adapter
*adapter
= device
->adapter
;
767 struct wined3d_resource_desc surface_desc
;
768 BOOL displaymode_set
= FALSE
;
774 if (desc
->backbuffer_count
> WINED3DPRESENT_BACK_BUFFER_MAX
)
776 FIXME("The application requested %u back buffers, this is not supported.\n",
777 desc
->backbuffer_count
);
778 return WINED3DERR_INVALIDCALL
;
781 if (desc
->backbuffer_count
> 1)
783 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
784 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
787 if (device
->wined3d
->flags
& WINED3D_NO3D
)
788 swapchain
->swapchain_ops
= &swapchain_gdi_ops
;
790 swapchain
->swapchain_ops
= &swapchain_gl_ops
;
792 window
= desc
->device_window
? desc
->device_window
: device
->create_parms
.focus_window
;
794 swapchain
->device
= device
;
795 swapchain
->parent
= parent
;
796 swapchain
->parent_ops
= parent_ops
;
798 swapchain
->win_handle
= window
;
799 swapchain
->device_window
= window
;
801 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
,
802 adapter
->ordinal
, &swapchain
->original_mode
, NULL
)))
804 ERR("Failed to get current display mode, hr %#x.\n", hr
);
808 GetClientRect(window
, &client_rect
);
810 && (!desc
->backbuffer_width
|| !desc
->backbuffer_height
811 || desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
))
814 if (!desc
->backbuffer_width
)
816 desc
->backbuffer_width
= client_rect
.right
;
817 TRACE("Updating width to %u.\n", desc
->backbuffer_width
);
820 if (!desc
->backbuffer_height
)
822 desc
->backbuffer_height
= client_rect
.bottom
;
823 TRACE("Updating height to %u.\n", desc
->backbuffer_height
);
826 if (desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
)
828 desc
->backbuffer_format
= swapchain
->original_mode
.format_id
;
829 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain
->original_mode
.format_id
));
832 swapchain
->desc
= *desc
;
833 swapchain_update_render_to_fbo(swapchain
);
835 TRACE("Creating front buffer.\n");
837 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
838 surface_desc
.format
= swapchain
->desc
.backbuffer_format
;
839 surface_desc
.multisample_type
= swapchain
->desc
.multisample_type
;
840 surface_desc
.multisample_quality
= swapchain
->desc
.multisample_quality
;
841 surface_desc
.usage
= 0;
842 surface_desc
.pool
= WINED3D_POOL_DEFAULT
;
843 surface_desc
.width
= swapchain
->desc
.backbuffer_width
;
844 surface_desc
.height
= swapchain
->desc
.backbuffer_height
;
845 surface_desc
.depth
= 1;
846 surface_desc
.size
= 0;
848 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_surface(device
->device_parent
,
849 parent
, &surface_desc
, &swapchain
->front_buffer
)))
851 WARN("Failed to create front buffer, hr %#x.\n", hr
);
855 surface_set_swapchain(swapchain
->front_buffer
, swapchain
);
856 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
858 surface_validate_location(swapchain
->front_buffer
, SFLAG_INDRAWABLE
);
859 surface_invalidate_location(swapchain
->front_buffer
, ~SFLAG_INDRAWABLE
);
862 /* MSDN says we're only allowed a single fullscreen swapchain per device,
863 * so we should really check to see if there is a fullscreen swapchain
864 * already. Does a single head count as full screen? */
868 struct wined3d_display_mode mode
;
870 /* Change the display settings */
871 mode
.width
= desc
->backbuffer_width
;
872 mode
.height
= desc
->backbuffer_height
;
873 mode
.format_id
= desc
->backbuffer_format
;
874 mode
.refresh_rate
= desc
->refresh_rate
;
875 mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
877 if (FAILED(hr
= wined3d_set_adapter_display_mode(device
->wined3d
, adapter
->ordinal
, &mode
)))
879 WARN("Failed to set display mode, hr %#x.\n", hr
);
882 displaymode_set
= TRUE
;
885 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
887 static const enum wined3d_format_id formats
[] =
889 WINED3DFMT_D24_UNORM_S8_UINT
,
890 WINED3DFMT_D32_UNORM
,
891 WINED3DFMT_R24_UNORM_X8_TYPELESS
,
892 WINED3DFMT_D16_UNORM
,
893 WINED3DFMT_S1_UINT_D15_UNORM
896 const struct wined3d_gl_info
*gl_info
= &adapter
->gl_info
;
898 swapchain
->context
= HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain
->context
));
899 if (!swapchain
->context
)
901 ERR("Failed to create the context array.\n");
905 swapchain
->num_contexts
= 1;
907 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
908 * You are able to add a depth + stencil surface at a later stage when you need it.
909 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
910 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
911 * context, need torecreate shaders, textures and other resources.
913 * The context manager already takes care of the state problem and for the other tasks code from Reset
914 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
915 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
916 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
917 * issue needs to be fixed. */
918 for (i
= 0; i
< (sizeof(formats
) / sizeof(*formats
)); i
++)
920 swapchain
->ds_format
= wined3d_get_format(gl_info
, formats
[i
]);
921 swapchain
->context
[0] = context_create(swapchain
, swapchain
->front_buffer
, swapchain
->ds_format
);
922 if (swapchain
->context
[0]) break;
923 TRACE("Depth stencil format %s is not supported, trying next format\n",
924 debug_d3dformat(formats
[i
]));
927 if (!swapchain
->context
[0])
929 WARN("Failed to create context.\n");
930 hr
= WINED3DERR_NOTAVAILABLE
;
934 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
935 && (!desc
->enable_auto_depth_stencil
936 || swapchain
->desc
.auto_depth_stencil_format
!= swapchain
->ds_format
->id
))
938 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
940 context_release(swapchain
->context
[0]);
943 if (swapchain
->desc
.backbuffer_count
> 0)
945 swapchain
->back_buffers
= HeapAlloc(GetProcessHeap(), 0,
946 sizeof(*swapchain
->back_buffers
) * swapchain
->desc
.backbuffer_count
);
947 if (!swapchain
->back_buffers
)
949 ERR("Failed to allocate backbuffer array memory.\n");
954 surface_desc
.usage
|= WINED3DUSAGE_RENDERTARGET
;
955 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
957 TRACE("Creating back buffer %u.\n", i
);
958 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_surface(device
->device_parent
,
959 parent
, &surface_desc
, &swapchain
->back_buffers
[i
])))
961 WARN("Failed to create back buffer %u, hr %#x.\n", i
, hr
);
964 surface_set_swapchain(swapchain
->back_buffers
[i
], swapchain
);
968 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
969 if (desc
->enable_auto_depth_stencil
&& !(device
->wined3d
->flags
& WINED3D_NO3D
))
971 TRACE("Creating depth/stencil buffer.\n");
972 if (!device
->auto_depth_stencil
)
974 surface_desc
.format
= swapchain
->desc
.auto_depth_stencil_format
;
975 surface_desc
.usage
= WINED3DUSAGE_DEPTHSTENCIL
;
977 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_surface(device
->device_parent
,
978 device
->device_parent
, &surface_desc
, &device
->auto_depth_stencil
)))
980 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr
);
986 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
993 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
994 adapter
->ordinal
, &swapchain
->original_mode
)))
995 ERR("Failed to restore display mode.\n");
999 if (swapchain
->back_buffers
)
1001 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1003 if (swapchain
->back_buffers
[i
])
1005 surface_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
1006 wined3d_surface_decref(swapchain
->back_buffers
[i
]);
1009 HeapFree(GetProcessHeap(), 0, swapchain
->back_buffers
);
1012 if (swapchain
->context
)
1014 if (swapchain
->context
[0])
1016 context_release(swapchain
->context
[0]);
1017 context_destroy(device
, swapchain
->context
[0]);
1018 swapchain
->num_contexts
= 0;
1020 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1023 if (swapchain
->front_buffer
)
1025 surface_set_swapchain(swapchain
->front_buffer
, NULL
);
1026 wined3d_surface_decref(swapchain
->front_buffer
);
1032 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
, struct wined3d_swapchain_desc
*desc
,
1033 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_swapchain
**swapchain
)
1035 struct wined3d_swapchain
*object
;
1038 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1039 device
, desc
, parent
, parent_ops
, swapchain
);
1041 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1043 return E_OUTOFMEMORY
;
1045 hr
= swapchain_init(object
, device
, desc
, parent
, parent_ops
);
1048 WARN("Failed to initialize swapchain, hr %#x.\n", hr
);
1049 HeapFree(GetProcessHeap(), 0, object
);
1053 TRACE("Created swapchain %p.\n", object
);
1054 *swapchain
= object
;
1059 static struct wined3d_context
*swapchain_create_context(struct wined3d_swapchain
*swapchain
)
1061 struct wined3d_context
**newArray
;
1062 struct wined3d_context
*ctx
;
1064 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain
, GetCurrentThreadId());
1066 if (!(ctx
= context_create(swapchain
, swapchain
->front_buffer
, swapchain
->ds_format
)))
1068 ERR("Failed to create a new context for the swapchain\n");
1071 context_release(ctx
);
1073 newArray
= HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray
) * (swapchain
->num_contexts
+ 1));
1075 ERR("Out of memory when trying to allocate a new context array\n");
1076 context_destroy(swapchain
->device
, ctx
);
1079 memcpy(newArray
, swapchain
->context
, sizeof(*newArray
) * swapchain
->num_contexts
);
1080 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1081 newArray
[swapchain
->num_contexts
] = ctx
;
1082 swapchain
->context
= newArray
;
1083 swapchain
->num_contexts
++;
1085 TRACE("Returning context %p\n", ctx
);
1089 void swapchain_destroy_contexts(struct wined3d_swapchain
*swapchain
)
1093 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1095 context_destroy(swapchain
->device
, swapchain
->context
[i
]);
1097 swapchain
->num_contexts
= 0;
1100 struct wined3d_context
*swapchain_get_context(struct wined3d_swapchain
*swapchain
)
1102 DWORD tid
= GetCurrentThreadId();
1105 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1107 if (swapchain
->context
[i
]->tid
== tid
)
1108 return swapchain
->context
[i
];
1111 /* Create a new context for the thread */
1112 return swapchain_create_context(swapchain
);
1115 void get_drawable_size_swapchain(const struct wined3d_context
*context
, UINT
*width
, UINT
*height
)
1117 /* The drawable size of an onscreen drawable is the surface size.
1118 * (Actually: The window size, but the surface is created in window size) */
1119 *width
= context
->current_rt
->resource
.width
;
1120 *height
= context
->current_rt
->resource
.height
;
1123 HDC
swapchain_get_backup_dc(struct wined3d_swapchain
*swapchain
)
1125 if (!swapchain
->backup_dc
)
1127 TRACE("Creating the backup window for swapchain %p.\n", swapchain
);
1129 if (!(swapchain
->backup_wnd
= CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, "WineD3D fake window",
1130 WS_OVERLAPPEDWINDOW
, 10, 10, 10, 10, NULL
, NULL
, NULL
, NULL
)))
1132 ERR("Failed to create a window.\n");
1136 if (!(swapchain
->backup_dc
= GetDC(swapchain
->backup_wnd
)))
1138 ERR("Failed to get a DC.\n");
1139 DestroyWindow(swapchain
->backup_wnd
);
1140 swapchain
->backup_wnd
= NULL
;
1145 return swapchain
->backup_dc
;
1148 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1152 surface_update_draw_binding(swapchain
->front_buffer
);
1154 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1156 surface_update_draw_binding(swapchain
->back_buffers
[i
]);