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 surface_set_swapchain(swapchain
->front_buffer
, NULL
);
42 if (wined3d_surface_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 surface_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
54 if (wined3d_surface_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 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
138 swapchain
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
),
139 dst_window_override
, dirty_region
, flags
);
142 FIXME("Ignoring flags %#x.\n", flags
);
144 if (!swapchain
->back_buffers
)
146 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
147 return WINED3DERR_INVALIDCALL
;
150 wined3d_cs_emit_present(swapchain
->device
->cs
, swapchain
, src_rect
,
151 dst_rect
, dst_window_override
, dirty_region
, flags
);
156 HRESULT CDECL
wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain
*swapchain
,
157 struct wined3d_surface
*dst_surface
)
159 struct wined3d_surface
*src_surface
;
160 RECT src_rect
, dst_rect
;
162 TRACE("swapchain %p, dst_surface %p.\n", swapchain
, dst_surface
);
164 src_surface
= swapchain
->front_buffer
;
165 SetRect(&src_rect
, 0, 0, src_surface
->resource
.width
, src_surface
->resource
.height
);
168 if (swapchain
->desc
.windowed
)
170 MapWindowPoints(swapchain
->win_handle
, NULL
, (POINT
*)&dst_rect
, 2);
171 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
172 wine_dbgstr_rect(&dst_rect
));
175 return wined3d_surface_blt(dst_surface
, &dst_rect
, src_surface
, &src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
178 struct wined3d_surface
* CDECL
wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain
*swapchain
,
179 UINT back_buffer_idx
, enum wined3d_backbuffer_type type
)
181 TRACE("swapchain %p, back_buffer_idx %u, type %#x.\n",
182 swapchain
, back_buffer_idx
, type
);
184 /* Return invalid if there is no backbuffer array, otherwise it will
185 * crash when ddraw is used (there swapchain->back_buffers is always
186 * NULL). We need this because this function is called from
187 * stateblock_init_default_state() to get the default scissorrect
189 if (!swapchain
->back_buffers
|| back_buffer_idx
>= swapchain
->desc
.backbuffer_count
)
191 WARN("Invalid back buffer index.\n");
192 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
193 * here in wined3d to avoid problems in other libs. */
197 TRACE("Returning back buffer %p.\n", swapchain
->back_buffers
[back_buffer_idx
]);
199 return swapchain
->back_buffers
[back_buffer_idx
];
202 HRESULT CDECL
wined3d_swapchain_get_raster_status(const struct wined3d_swapchain
*swapchain
,
203 struct wined3d_raster_status
*raster_status
)
205 TRACE("swapchain %p, raster_status %p.\n", swapchain
, raster_status
);
207 return wined3d_get_adapter_raster_status(swapchain
->device
->wined3d
,
208 swapchain
->device
->adapter
->ordinal
, raster_status
);
211 HRESULT CDECL
wined3d_swapchain_get_display_mode(const struct wined3d_swapchain
*swapchain
,
212 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
216 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain
, mode
, rotation
);
218 hr
= wined3d_get_adapter_display_mode(swapchain
->device
->wined3d
,
219 swapchain
->device
->adapter
->ordinal
, mode
, rotation
);
221 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
222 mode
->width
, mode
->height
, mode
->refresh_rate
, debug_d3dformat(mode
->format_id
));
227 struct wined3d_device
* CDECL
wined3d_swapchain_get_device(const struct wined3d_swapchain
*swapchain
)
229 TRACE("swapchain %p.\n", swapchain
);
231 return swapchain
->device
;
234 void CDECL
wined3d_swapchain_get_desc(const struct wined3d_swapchain
*swapchain
,
235 struct wined3d_swapchain_desc
*desc
)
237 TRACE("swapchain %p, desc %p.\n", swapchain
, desc
);
239 *desc
= swapchain
->desc
;
242 HRESULT CDECL
wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
243 DWORD flags
, const struct wined3d_gamma_ramp
*ramp
)
247 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain
, flags
, ramp
);
250 FIXME("Ignoring flags %#x.\n", flags
);
252 dc
= GetDC(swapchain
->device_window
);
253 SetDeviceGammaRamp(dc
, (void *)ramp
);
254 ReleaseDC(swapchain
->device_window
, dc
);
259 HRESULT CDECL
wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
260 struct wined3d_gamma_ramp
*ramp
)
264 TRACE("swapchain %p, ramp %p.\n", swapchain
, ramp
);
266 dc
= GetDC(swapchain
->device_window
);
267 GetDeviceGammaRamp(dc
, ramp
);
268 ReleaseDC(swapchain
->device_window
, dc
);
273 /* A GL context is provided by the caller */
274 static void swapchain_blit(const struct wined3d_swapchain
*swapchain
,
275 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
277 struct wined3d_surface
*backbuffer
= swapchain
->back_buffers
[0];
278 UINT src_w
= src_rect
->right
- src_rect
->left
;
279 UINT src_h
= src_rect
->bottom
- src_rect
->top
;
281 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
285 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
286 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
288 if (src_w
== dst_rect
->right
- dst_rect
->left
&& src_h
== dst_rect
->bottom
- dst_rect
->top
)
289 gl_filter
= GL_NEAREST
;
291 gl_filter
= GL_LINEAR
;
293 GetClientRect(swapchain
->win_handle
, &win_rect
);
294 win_h
= win_rect
.bottom
- win_rect
.top
;
296 if (gl_info
->fbo_ops
.glBlitFramebuffer
&& is_identity_fixup(backbuffer
->resource
.format
->color_fixup
))
298 DWORD location
= WINED3D_LOCATION_TEXTURE_RGB
;
300 if (backbuffer
->resource
.multisample_type
)
302 location
= WINED3D_LOCATION_RB_RESOLVED
;
303 surface_load_location(backbuffer
, location
);
306 context_apply_fbo_state_blit(context
, GL_READ_FRAMEBUFFER
, backbuffer
, NULL
, location
);
307 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_COLOR_ATTACHMENT0
);
308 context_check_fbo_status(context
, GL_READ_FRAMEBUFFER
);
310 context_apply_fbo_state_blit(context
, GL_DRAW_FRAMEBUFFER
, swapchain
->front_buffer
,
311 NULL
, WINED3D_LOCATION_DRAWABLE
);
312 context_set_draw_buffer(context
, GL_BACK
);
313 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
315 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
316 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
317 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
318 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
319 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
321 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
322 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
324 /* Note that the texture is upside down */
325 gl_info
->fbo_ops
.glBlitFramebuffer(src_rect
->left
, src_rect
->top
, src_rect
->right
, src_rect
->bottom
,
326 dst_rect
->left
, win_h
- dst_rect
->top
, dst_rect
->right
, win_h
- dst_rect
->bottom
,
327 GL_COLOR_BUFFER_BIT
, gl_filter
);
328 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
332 struct wined3d_device
*device
= swapchain
->device
;
333 struct wined3d_context
*context2
;
334 float tex_left
= src_rect
->left
;
335 float tex_top
= src_rect
->top
;
336 float tex_right
= src_rect
->right
;
337 float tex_bottom
= src_rect
->bottom
;
339 context2
= context_acquire(device
, swapchain
->back_buffers
[0]);
340 context_apply_blit_state(context2
, device
);
342 if (backbuffer
->flags
& SFLAG_NORMCOORD
)
350 if (is_complex_fixup(backbuffer
->resource
.format
->color_fixup
))
351 gl_filter
= GL_NEAREST
;
353 context_apply_fbo_state_blit(context2
, GL_FRAMEBUFFER
, swapchain
->front_buffer
,
354 NULL
, WINED3D_LOCATION_DRAWABLE
);
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_ALPHATEST
,
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_ALPHATEST
,
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
, WINED3D_LOCATION_TEXTURE_RGB
);
502 surface_invalidate_location(back_buffer
, WINED3D_LOCATION_DRAWABLE
);
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 static unsigned int once
;
515 if (swapchain
->desc
.swap_effect
== WINED3D_SWAP_EFFECT_FLIP
&& !once
++)
516 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
518 swapchain_blit(swapchain
, context
, &src_rect
, &dst_rect
);
521 if (swapchain
->num_contexts
> 1)
522 gl_info
->gl_ops
.gl
.p_glFinish();
524 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
525 gl_info
->gl_ops
.wgl
.p_wglSwapBuffers(context
->hdc
); /* TODO: cycle through the swapchain buffers */
527 TRACE("SwapBuffers called, Starting new frame\n");
531 DWORD time
= GetTickCount();
534 /* every 1.5 seconds */
535 if (time
- swapchain
->prev_time
> 1500)
537 TRACE_(fps
)("%p @ approx %.2ffps\n",
538 swapchain
, 1000.0 * swapchain
->frames
/ (time
- swapchain
->prev_time
));
539 swapchain
->prev_time
= time
;
540 swapchain
->frames
= 0;
544 if (!swapchain
->render_to_fbo
&& ((swapchain
->front_buffer
->locations
& WINED3D_LOCATION_SYSMEM
)
545 || (back_buffer
->locations
& WINED3D_LOCATION_SYSMEM
)))
547 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
548 * Doesn't work with render_to_fbo because we're not flipping
550 struct wined3d_surface
*front
= swapchain
->front_buffer
;
552 if (front
->resource
.size
== back_buffer
->resource
.size
)
554 flip_surface(front
, back_buffer
);
556 /* Tell the front buffer surface that is has been modified. However,
557 * the other locations were preserved during that, so keep the flags.
558 * This serves to update the emulated overlay, if any. */
559 surface_validate_location(front
, WINED3D_LOCATION_DRAWABLE
);
563 surface_validate_location(front
, WINED3D_LOCATION_DRAWABLE
);
564 surface_invalidate_location(front
, ~WINED3D_LOCATION_DRAWABLE
);
565 surface_validate_location(back_buffer
, WINED3D_LOCATION_DRAWABLE
);
566 surface_invalidate_location(back_buffer
, ~WINED3D_LOCATION_DRAWABLE
);
571 surface_validate_location(swapchain
->front_buffer
, WINED3D_LOCATION_DRAWABLE
);
572 surface_invalidate_location(swapchain
->front_buffer
, ~WINED3D_LOCATION_DRAWABLE
);
573 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
574 * and INTEXTURE copies can keep their old content if they have any defined content.
575 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
576 * the texture / sysmem copy needs to be reloaded from the drawable
578 if (swapchain
->desc
.swap_effect
== WINED3D_SWAP_EFFECT_FLIP
)
580 surface_validate_location(back_buffer
, back_buffer
->draw_binding
);
581 surface_invalidate_location(back_buffer
, ~back_buffer
->draw_binding
);
585 if (fb
->depth_stencil
)
587 if (swapchain
->desc
.flags
& WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
588 || fb
->depth_stencil
->flags
& SFLAG_DISCARD
)
590 surface_modify_ds_location(fb
->depth_stencil
, WINED3D_LOCATION_DISCARDED
,
591 fb
->depth_stencil
->resource
.width
,
592 fb
->depth_stencil
->resource
.height
);
593 if (fb
->depth_stencil
== swapchain
->device
->onscreen_depth_stencil
)
595 wined3d_surface_decref(swapchain
->device
->onscreen_depth_stencil
);
596 swapchain
->device
->onscreen_depth_stencil
= NULL
;
601 context_release(context
);
604 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
606 swapchain_gl_present
,
609 /* Helper function that blits the front buffer contents to the target window. */
610 void x11_copy_to_screen(const struct wined3d_swapchain
*swapchain
, const RECT
*rect
)
612 struct wined3d_surface
*front
;
613 POINT offset
= {0, 0};
618 TRACE("swapchain %p, rect %s.\n", swapchain
, wine_dbgstr_rect(rect
));
620 front
= swapchain
->front_buffer
;
621 if (front
->resource
.map_count
)
622 ERR("Trying to blit a mapped surface.\n");
624 TRACE("Copying surface %p to screen.\n", front
);
626 surface_load_location(front
, WINED3D_LOCATION_DIB
);
629 window
= swapchain
->win_handle
;
630 dst_dc
= GetDCEx(window
, 0, DCX_CLIPSIBLINGS
| DCX_CACHE
);
632 /* Front buffer coordinates are screen coordinates. Map them to the
633 * destination window if not fullscreened. */
634 if (swapchain
->desc
.windowed
)
635 ClientToScreen(window
, &offset
);
637 TRACE("offset %s.\n", wine_dbgstr_point(&offset
));
640 draw_rect
.right
= front
->resource
.width
;
642 draw_rect
.bottom
= front
->resource
.height
;
645 IntersectRect(&draw_rect
, &draw_rect
, rect
);
647 BitBlt(dst_dc
, draw_rect
.left
- offset
.x
, draw_rect
.top
- offset
.y
,
648 draw_rect
.right
- draw_rect
.left
, draw_rect
.bottom
- draw_rect
.top
,
649 src_dc
, draw_rect
.left
, draw_rect
.top
, SRCCOPY
);
650 ReleaseDC(window
, dst_dc
);
653 static void swapchain_gdi_present(struct wined3d_swapchain
*swapchain
, const RECT
*src_rect_in
,
654 const RECT
*dst_rect_in
, const RGNDATA
*dirty_region
, DWORD flags
)
656 struct wined3d_surface
*front
, *back
;
658 front
= swapchain
->front_buffer
;
659 back
= swapchain
->back_buffers
[0];
665 front
->hDC
= back
->hDC
;
669 /* Flip the DIBsection. */
672 tmp
= front
->dib
.DIBsection
;
673 front
->dib
.DIBsection
= back
->dib
.DIBsection
;
674 back
->dib
.DIBsection
= tmp
;
677 /* Flip the surface data. */
681 tmp
= front
->dib
.bitmap_data
;
682 front
->dib
.bitmap_data
= back
->dib
.bitmap_data
;
683 back
->dib
.bitmap_data
= tmp
;
685 if (front
->resource
.heap_memory
)
686 ERR("GDI Surface %p has heap memory allocated.\n", front
);
688 if (back
->resource
.heap_memory
)
689 ERR("GDI Surface %p has heap memory allocated.\n", back
);
695 static LONG prev_time
, frames
;
696 DWORD time
= GetTickCount();
700 /* every 1.5 seconds */
701 if (time
- prev_time
> 1500)
703 TRACE_(fps
)("@ approx %.2ffps\n", 1000.0 * frames
/ (time
- prev_time
));
709 x11_copy_to_screen(swapchain
, NULL
);
712 static const struct wined3d_swapchain_ops swapchain_gdi_ops
=
714 swapchain_gdi_present
,
717 void swapchain_update_render_to_fbo(struct wined3d_swapchain
*swapchain
)
721 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
724 if (!swapchain
->desc
.backbuffer_count
)
726 TRACE("Single buffered rendering.\n");
727 swapchain
->render_to_fbo
= FALSE
;
731 GetClientRect(swapchain
->win_handle
, &client_rect
);
733 TRACE("Backbuffer %ux%u, window %ux%u.\n",
734 swapchain
->desc
.backbuffer_width
,
735 swapchain
->desc
.backbuffer_height
,
736 client_rect
.right
, client_rect
.bottom
);
737 TRACE("Multisample type %#x, quality %#x.\n",
738 swapchain
->desc
.multisample_type
,
739 swapchain
->desc
.multisample_quality
);
741 if (!wined3d_settings
.always_offscreen
&& !swapchain
->desc
.multisample_type
742 && swapchain
->desc
.backbuffer_width
== client_rect
.right
743 && swapchain
->desc
.backbuffer_height
== client_rect
.bottom
)
745 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
746 swapchain
->render_to_fbo
= FALSE
;
750 TRACE("Rendering to FBO.\n");
751 swapchain
->render_to_fbo
= TRUE
;
754 static HRESULT
swapchain_init(struct wined3d_swapchain
*swapchain
, struct wined3d_device
*device
,
755 struct wined3d_swapchain_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
757 const struct wined3d_adapter
*adapter
= device
->adapter
;
758 struct wined3d_resource_desc surface_desc
;
759 BOOL displaymode_set
= FALSE
;
765 if (desc
->backbuffer_count
> WINED3DPRESENT_BACK_BUFFER_MAX
)
767 FIXME("The application requested %u back buffers, this is not supported.\n",
768 desc
->backbuffer_count
);
769 return WINED3DERR_INVALIDCALL
;
772 if (desc
->backbuffer_count
> 1)
774 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
775 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
778 if (device
->wined3d
->flags
& WINED3D_NO3D
)
779 swapchain
->swapchain_ops
= &swapchain_gdi_ops
;
781 swapchain
->swapchain_ops
= &swapchain_gl_ops
;
783 window
= desc
->device_window
? desc
->device_window
: device
->create_parms
.focus_window
;
785 swapchain
->device
= device
;
786 swapchain
->parent
= parent
;
787 swapchain
->parent_ops
= parent_ops
;
789 swapchain
->win_handle
= window
;
790 swapchain
->device_window
= window
;
792 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
,
793 adapter
->ordinal
, &swapchain
->original_mode
, NULL
)))
795 ERR("Failed to get current display mode, hr %#x.\n", hr
);
799 GetClientRect(window
, &client_rect
);
801 && (!desc
->backbuffer_width
|| !desc
->backbuffer_height
802 || desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
))
805 if (!desc
->backbuffer_width
)
807 desc
->backbuffer_width
= client_rect
.right
;
808 TRACE("Updating width to %u.\n", desc
->backbuffer_width
);
811 if (!desc
->backbuffer_height
)
813 desc
->backbuffer_height
= client_rect
.bottom
;
814 TRACE("Updating height to %u.\n", desc
->backbuffer_height
);
817 if (desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
)
819 desc
->backbuffer_format
= swapchain
->original_mode
.format_id
;
820 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain
->original_mode
.format_id
));
823 swapchain
->desc
= *desc
;
824 swapchain_update_render_to_fbo(swapchain
);
826 TRACE("Creating front buffer.\n");
828 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
829 surface_desc
.format
= swapchain
->desc
.backbuffer_format
;
830 surface_desc
.multisample_type
= swapchain
->desc
.multisample_type
;
831 surface_desc
.multisample_quality
= swapchain
->desc
.multisample_quality
;
832 surface_desc
.usage
= 0;
833 surface_desc
.pool
= WINED3D_POOL_DEFAULT
;
834 surface_desc
.width
= swapchain
->desc
.backbuffer_width
;
835 surface_desc
.height
= swapchain
->desc
.backbuffer_height
;
836 surface_desc
.depth
= 1;
837 surface_desc
.size
= 0;
839 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_surface(device
->device_parent
,
840 parent
, &surface_desc
, &swapchain
->front_buffer
)))
842 WARN("Failed to create front buffer, hr %#x.\n", hr
);
846 surface_set_swapchain(swapchain
->front_buffer
, swapchain
);
847 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
849 surface_validate_location(swapchain
->front_buffer
, WINED3D_LOCATION_DRAWABLE
);
850 surface_invalidate_location(swapchain
->front_buffer
, ~WINED3D_LOCATION_DRAWABLE
);
853 /* MSDN says we're only allowed a single fullscreen swapchain per device,
854 * so we should really check to see if there is a fullscreen swapchain
855 * already. Does a single head count as full screen? */
859 struct wined3d_display_mode mode
;
861 /* Change the display settings */
862 mode
.width
= desc
->backbuffer_width
;
863 mode
.height
= desc
->backbuffer_height
;
864 mode
.format_id
= desc
->backbuffer_format
;
865 mode
.refresh_rate
= desc
->refresh_rate
;
866 mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
868 if (FAILED(hr
= wined3d_set_adapter_display_mode(device
->wined3d
, adapter
->ordinal
, &mode
)))
870 WARN("Failed to set display mode, hr %#x.\n", hr
);
873 displaymode_set
= TRUE
;
876 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
878 static const enum wined3d_format_id formats
[] =
880 WINED3DFMT_D24_UNORM_S8_UINT
,
881 WINED3DFMT_D32_UNORM
,
882 WINED3DFMT_R24_UNORM_X8_TYPELESS
,
883 WINED3DFMT_D16_UNORM
,
884 WINED3DFMT_S1_UINT_D15_UNORM
887 const struct wined3d_gl_info
*gl_info
= &adapter
->gl_info
;
889 swapchain
->context
= HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain
->context
));
890 if (!swapchain
->context
)
892 ERR("Failed to create the context array.\n");
896 swapchain
->num_contexts
= 1;
898 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
899 * You are able to add a depth + stencil surface at a later stage when you need it.
900 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
901 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
902 * context, need torecreate shaders, textures and other resources.
904 * The context manager already takes care of the state problem and for the other tasks code from Reset
905 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
906 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
907 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
908 * issue needs to be fixed. */
909 for (i
= 0; i
< (sizeof(formats
) / sizeof(*formats
)); i
++)
911 swapchain
->ds_format
= wined3d_get_format(gl_info
, formats
[i
]);
912 swapchain
->context
[0] = context_create(swapchain
, swapchain
->front_buffer
, swapchain
->ds_format
);
913 if (swapchain
->context
[0]) break;
914 TRACE("Depth stencil format %s is not supported, trying next format\n",
915 debug_d3dformat(formats
[i
]));
918 if (!swapchain
->context
[0])
920 WARN("Failed to create context.\n");
921 hr
= WINED3DERR_NOTAVAILABLE
;
925 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
926 && (!desc
->enable_auto_depth_stencil
927 || swapchain
->desc
.auto_depth_stencil_format
!= swapchain
->ds_format
->id
))
929 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
931 context_release(swapchain
->context
[0]);
934 if (swapchain
->desc
.backbuffer_count
> 0)
936 swapchain
->back_buffers
= HeapAlloc(GetProcessHeap(), 0,
937 sizeof(*swapchain
->back_buffers
) * swapchain
->desc
.backbuffer_count
);
938 if (!swapchain
->back_buffers
)
940 ERR("Failed to allocate backbuffer array memory.\n");
945 surface_desc
.usage
|= WINED3DUSAGE_RENDERTARGET
;
946 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
948 TRACE("Creating back buffer %u.\n", i
);
949 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_surface(device
->device_parent
,
950 parent
, &surface_desc
, &swapchain
->back_buffers
[i
])))
952 WARN("Failed to create back buffer %u, hr %#x.\n", i
, hr
);
953 swapchain
->desc
.backbuffer_count
= i
;
956 surface_set_swapchain(swapchain
->back_buffers
[i
], swapchain
);
960 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
961 if (desc
->enable_auto_depth_stencil
&& !(device
->wined3d
->flags
& WINED3D_NO3D
))
963 TRACE("Creating depth/stencil buffer.\n");
964 if (!device
->auto_depth_stencil
)
966 surface_desc
.format
= swapchain
->desc
.auto_depth_stencil_format
;
967 surface_desc
.usage
= WINED3DUSAGE_DEPTHSTENCIL
;
969 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_surface(device
->device_parent
,
970 device
->device_parent
, &surface_desc
, &device
->auto_depth_stencil
)))
972 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr
);
978 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
985 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
986 adapter
->ordinal
, &swapchain
->original_mode
)))
987 ERR("Failed to restore display mode.\n");
991 if (swapchain
->back_buffers
)
993 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
995 if (swapchain
->back_buffers
[i
])
997 surface_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
998 wined3d_surface_decref(swapchain
->back_buffers
[i
]);
1001 HeapFree(GetProcessHeap(), 0, swapchain
->back_buffers
);
1004 if (swapchain
->context
)
1006 if (swapchain
->context
[0])
1008 context_release(swapchain
->context
[0]);
1009 context_destroy(device
, swapchain
->context
[0]);
1010 swapchain
->num_contexts
= 0;
1012 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1015 if (swapchain
->front_buffer
)
1017 surface_set_swapchain(swapchain
->front_buffer
, NULL
);
1018 wined3d_surface_decref(swapchain
->front_buffer
);
1024 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
, struct wined3d_swapchain_desc
*desc
,
1025 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_swapchain
**swapchain
)
1027 struct wined3d_swapchain
*object
;
1030 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1031 device
, desc
, parent
, parent_ops
, swapchain
);
1033 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1035 return E_OUTOFMEMORY
;
1037 hr
= swapchain_init(object
, device
, desc
, parent
, parent_ops
);
1040 WARN("Failed to initialize swapchain, hr %#x.\n", hr
);
1041 HeapFree(GetProcessHeap(), 0, object
);
1045 TRACE("Created swapchain %p.\n", object
);
1046 *swapchain
= object
;
1051 static struct wined3d_context
*swapchain_create_context(struct wined3d_swapchain
*swapchain
)
1053 struct wined3d_context
**newArray
;
1054 struct wined3d_context
*ctx
;
1056 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain
, GetCurrentThreadId());
1058 if (!(ctx
= context_create(swapchain
, swapchain
->front_buffer
, swapchain
->ds_format
)))
1060 ERR("Failed to create a new context for the swapchain\n");
1063 context_release(ctx
);
1065 newArray
= HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray
) * (swapchain
->num_contexts
+ 1));
1067 ERR("Out of memory when trying to allocate a new context array\n");
1068 context_destroy(swapchain
->device
, ctx
);
1071 memcpy(newArray
, swapchain
->context
, sizeof(*newArray
) * swapchain
->num_contexts
);
1072 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1073 newArray
[swapchain
->num_contexts
] = ctx
;
1074 swapchain
->context
= newArray
;
1075 swapchain
->num_contexts
++;
1077 TRACE("Returning context %p\n", ctx
);
1081 void swapchain_destroy_contexts(struct wined3d_swapchain
*swapchain
)
1085 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1087 context_destroy(swapchain
->device
, swapchain
->context
[i
]);
1089 swapchain
->num_contexts
= 0;
1092 struct wined3d_context
*swapchain_get_context(struct wined3d_swapchain
*swapchain
)
1094 DWORD tid
= GetCurrentThreadId();
1097 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1099 if (swapchain
->context
[i
]->tid
== tid
)
1100 return swapchain
->context
[i
];
1103 /* Create a new context for the thread */
1104 return swapchain_create_context(swapchain
);
1107 void get_drawable_size_swapchain(const struct wined3d_context
*context
, UINT
*width
, UINT
*height
)
1109 /* The drawable size of an onscreen drawable is the surface size.
1110 * (Actually: The window size, but the surface is created in window size) */
1111 *width
= context
->current_rt
->resource
.width
;
1112 *height
= context
->current_rt
->resource
.height
;
1115 HDC
swapchain_get_backup_dc(struct wined3d_swapchain
*swapchain
)
1117 if (!swapchain
->backup_dc
)
1119 TRACE("Creating the backup window for swapchain %p.\n", swapchain
);
1121 if (!(swapchain
->backup_wnd
= CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, "WineD3D fake window",
1122 WS_OVERLAPPEDWINDOW
, 10, 10, 10, 10, NULL
, NULL
, NULL
, NULL
)))
1124 ERR("Failed to create a window.\n");
1128 if (!(swapchain
->backup_dc
= GetDC(swapchain
->backup_wnd
)))
1130 ERR("Failed to get a DC.\n");
1131 DestroyWindow(swapchain
->backup_wnd
);
1132 swapchain
->backup_wnd
= NULL
;
1137 return swapchain
->backup_dc
;
1140 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1144 surface_update_draw_binding(swapchain
->front_buffer
);
1146 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1148 surface_update_draw_binding(swapchain
->back_buffers
[i
]);