wbemdisp: Added WinMGMTS object stub implementation.
[wine/wine-gecko.git] / dlls / wined3d / swapchain.c
blob970dcdb0e0e084808de148df1ffe02f6bd3b14cb
1 /*
2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(fps);
30 /* Do not call while under the GL lock. */
31 static void swapchain_cleanup(struct wined3d_swapchain *swapchain)
33 HRESULT hr;
34 UINT i;
36 TRACE("Destroying swapchain %p.\n", swapchain);
38 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
40 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
41 * is the last buffer to be destroyed, FindContext() depends on that. */
42 if (swapchain->front_buffer)
44 surface_set_swapchain(swapchain->front_buffer, NULL);
45 if (wined3d_surface_decref(swapchain->front_buffer))
46 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
47 swapchain->front_buffer = NULL;
50 if (swapchain->back_buffers)
52 i = swapchain->desc.backbuffer_count;
54 while (i--)
56 surface_set_swapchain(swapchain->back_buffers[i], NULL);
57 if (wined3d_surface_decref(swapchain->back_buffers[i]))
58 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
60 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
61 swapchain->back_buffers = NULL;
64 for (i = 0; i < swapchain->num_contexts; ++i)
66 context_destroy(swapchain->device, swapchain->context[i]);
68 HeapFree(GetProcessHeap(), 0, swapchain->context);
70 /* Restore the screen resolution if we rendered in fullscreen.
71 * This will restore the screen resolution to what it was before creating
72 * the swapchain. In case of d3d8 and d3d9 this will be the original
73 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
74 * sets the resolution before starting up Direct3D, thus orig_width and
75 * orig_height will be equal to the modes in the presentation params. */
76 if (!swapchain->desc.windowed && swapchain->desc.auto_restore_display_mode)
78 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
79 swapchain->device->adapter->ordinal, &swapchain->original_mode)))
80 ERR("Failed to restore display mode, hr %#x.\n", hr);
83 if (swapchain->backup_dc)
85 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
87 ReleaseDC(swapchain->backup_wnd, swapchain->backup_dc);
88 DestroyWindow(swapchain->backup_wnd);
92 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
94 ULONG refcount = InterlockedIncrement(&swapchain->ref);
96 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
98 return refcount;
101 /* Do not call while under the GL lock. */
102 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
104 ULONG refcount = InterlockedDecrement(&swapchain->ref);
106 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
108 if (!refcount)
110 swapchain_cleanup(swapchain);
111 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
112 HeapFree(GetProcessHeap(), 0, swapchain);
115 return refcount;
118 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
120 TRACE("swapchain %p.\n", swapchain);
122 return swapchain->parent;
125 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
127 if (!window)
128 window = swapchain->device_window;
129 if (window == swapchain->win_handle)
130 return;
132 TRACE("Setting swapchain %p window from %p to %p.\n",
133 swapchain, swapchain->win_handle, window);
134 swapchain->win_handle = window;
137 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
138 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
139 const RGNDATA *dirty_region, DWORD flags)
141 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
142 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
143 dst_window_override, dirty_region, flags);
145 if (flags)
146 FIXME("Ignoring flags %#x.\n", flags);
148 if (!swapchain->back_buffers)
150 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
151 return WINED3DERR_INVALIDCALL;
154 wined3d_swapchain_set_window(swapchain, dst_window_override);
156 swapchain->swapchain_ops->swapchain_present(swapchain, src_rect, dst_rect, dirty_region, flags);
158 return WINED3D_OK;
161 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
162 struct wined3d_surface *dst_surface)
164 struct wined3d_surface *src_surface;
165 RECT src_rect, dst_rect;
167 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
169 src_surface = swapchain->front_buffer;
170 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
171 dst_rect = src_rect;
173 if (swapchain->desc.windowed)
175 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
176 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
177 wine_dbgstr_rect(&dst_rect));
180 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
183 struct wined3d_surface * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
184 UINT back_buffer_idx, enum wined3d_backbuffer_type type)
186 TRACE("swapchain %p, back_buffer_idx %u, type %#x.\n",
187 swapchain, back_buffer_idx, type);
189 /* Return invalid if there is no backbuffer array, otherwise it will
190 * crash when ddraw is used (there swapchain->back_buffers is always
191 * NULL). We need this because this function is called from
192 * stateblock_init_default_state() to get the default scissorrect
193 * dimensions. */
194 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
196 WARN("Invalid back buffer index.\n");
197 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
198 * here in wined3d to avoid problems in other libs. */
199 return NULL;
202 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
204 return swapchain->back_buffers[back_buffer_idx];
207 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
208 struct wined3d_raster_status *raster_status)
210 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
212 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
213 swapchain->device->adapter->ordinal, raster_status);
216 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
217 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
219 HRESULT hr;
221 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
223 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
224 swapchain->device->adapter->ordinal, mode, rotation);
226 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
227 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
229 return hr;
232 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
234 TRACE("swapchain %p.\n", swapchain);
236 return swapchain->device;
239 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
240 struct wined3d_swapchain_desc *desc)
242 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
244 *desc = swapchain->desc;
247 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
248 DWORD flags, const struct wined3d_gamma_ramp *ramp)
250 HDC dc;
252 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
254 if (flags)
255 FIXME("Ignoring flags %#x.\n", flags);
257 dc = GetDC(swapchain->device_window);
258 SetDeviceGammaRamp(dc, (void *)ramp);
259 ReleaseDC(swapchain->device_window, dc);
261 return WINED3D_OK;
264 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
265 struct wined3d_gamma_ramp *ramp)
267 HDC dc;
269 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
271 dc = GetDC(swapchain->device_window);
272 GetDeviceGammaRamp(dc, ramp);
273 ReleaseDC(swapchain->device_window, dc);
275 return WINED3D_OK;
278 /* A GL context is provided by the caller */
279 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
280 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
282 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
283 UINT src_w = src_rect->right - src_rect->left;
284 UINT src_h = src_rect->bottom - src_rect->top;
285 GLenum gl_filter;
286 const struct wined3d_gl_info *gl_info = context->gl_info;
287 RECT win_rect;
288 UINT win_h;
290 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
291 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
293 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
294 gl_filter = GL_NEAREST;
295 else
296 gl_filter = GL_LINEAR;
298 GetClientRect(swapchain->win_handle, &win_rect);
299 win_h = win_rect.bottom - win_rect.top;
301 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
303 DWORD location = SFLAG_INTEXTURE;
305 if (backbuffer->resource.multisample_type)
307 location = SFLAG_INRB_RESOLVED;
308 surface_load_location(backbuffer, location, NULL);
311 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
312 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
313 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
315 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
316 context_set_draw_buffer(context, GL_BACK);
317 context_invalidate_state(context, STATE_FRAMEBUFFER);
319 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
320 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
321 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
322 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
323 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
325 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
326 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
328 /* Note that the texture is upside down */
329 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
330 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
331 GL_COLOR_BUFFER_BIT, gl_filter);
332 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
334 else
336 struct wined3d_device *device = swapchain->device;
337 struct wined3d_context *context2;
338 float tex_left = src_rect->left;
339 float tex_top = src_rect->top;
340 float tex_right = src_rect->right;
341 float tex_bottom = src_rect->bottom;
343 context2 = context_acquire(device, swapchain->back_buffers[0]);
344 context_apply_blit_state(context2, device);
346 if (backbuffer->flags & SFLAG_NORMCOORD)
348 tex_left /= src_w;
349 tex_right /= src_w;
350 tex_top /= src_h;
351 tex_bottom /= src_h;
354 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
355 gl_filter = GL_NEAREST;
357 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
358 context_bind_texture(context2, backbuffer->texture_target, backbuffer->texture_name);
360 /* Set up the texture. The surface is not in a wined3d_texture
361 * container, so there are no D3D texture settings to dirtify. */
362 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
363 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
364 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
366 context_set_draw_buffer(context, GL_BACK);
368 /* Set the viewport to the destination rectandle, disable any projection
369 * transformation set up by context_apply_blit_state(), and draw a
370 * (-1,-1)-(1,1) quad.
372 * Back up viewport and matrix to avoid breaking last_was_blit
374 * Note that context_apply_blit_state() set up viewport and ortho to
375 * match the surface size - we want the GL drawable(=window) size. */
376 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
377 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
378 dst_rect->right, win_h - dst_rect->top);
379 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
380 gl_info->gl_ops.gl.p_glPushMatrix();
381 gl_info->gl_ops.gl.p_glLoadIdentity();
383 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
384 /* bottom left */
385 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
386 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
388 /* top left */
389 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
390 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
392 /* top right */
393 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
394 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
396 /* bottom right */
397 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
398 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
399 gl_info->gl_ops.gl.p_glEnd();
401 gl_info->gl_ops.gl.p_glPopMatrix();
402 gl_info->gl_ops.gl.p_glPopAttrib();
404 device->blitter->unset_shader(context->gl_info);
405 checkGLcall("Swapchain present blit(manual)\n");
407 context_release(context2);
411 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
412 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
414 struct wined3d_surface *back_buffer = swapchain->back_buffers[0];
415 const struct wined3d_fb_state *fb = &swapchain->device->fb;
416 const struct wined3d_gl_info *gl_info;
417 struct wined3d_context *context;
418 RECT src_rect, dst_rect;
419 BOOL render_to_fbo;
421 context = context_acquire(swapchain->device, back_buffer);
422 if (!context->valid)
424 context_release(context);
425 WARN("Invalid context, skipping present.\n");
426 return;
429 gl_info = context->gl_info;
431 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
432 if (swapchain->device->bCursorVisible &&
433 swapchain->device->cursorTexture &&
434 !swapchain->device->hardwareCursor)
436 struct wined3d_surface cursor;
437 RECT destRect =
439 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
440 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
441 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
442 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
444 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
445 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
446 * the application because we are only supposed to copy the information out. Using a fake surface
447 * allows us to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
449 memset(&cursor, 0, sizeof(cursor));
450 cursor.resource.ref = 1;
451 cursor.resource.device = swapchain->device;
452 cursor.resource.pool = WINED3D_POOL_SCRATCH;
453 cursor.resource.format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
454 cursor.resource.type = WINED3D_RTYPE_SURFACE;
455 cursor.texture_name = swapchain->device->cursorTexture;
456 cursor.texture_target = GL_TEXTURE_2D;
457 cursor.texture_level = 0;
458 cursor.resource.width = swapchain->device->cursorWidth;
459 cursor.resource.height = swapchain->device->cursorHeight;
460 /* The cursor must have pow2 sizes */
461 cursor.pow2Width = cursor.resource.width;
462 cursor.pow2Height = cursor.resource.height;
463 /* The surface is in the texture */
464 cursor.flags |= SFLAG_INTEXTURE;
465 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
466 * which is exactly what we want :-)
468 if (swapchain->desc.windowed)
469 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
470 wined3d_surface_blt(back_buffer, &destRect, &cursor, NULL, WINEDDBLT_KEYSRC,
471 NULL, WINED3D_TEXF_POINT);
474 if (swapchain->device->logo_surface)
476 struct wined3d_surface *src_surface = swapchain->device->logo_surface;
477 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
479 /* Blit the logo into the upper left corner of the drawable. */
480 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_KEYSRC,
481 NULL, WINED3D_TEXF_POINT);
484 TRACE("Presenting HDC %p.\n", context->hdc);
486 render_to_fbo = swapchain->render_to_fbo;
488 if (src_rect_in)
490 src_rect = *src_rect_in;
491 if (!render_to_fbo && (src_rect.left || src_rect.top
492 || src_rect.right != swapchain->desc.backbuffer_width
493 || src_rect.bottom != swapchain->desc.backbuffer_height))
495 render_to_fbo = TRUE;
498 else
500 src_rect.left = 0;
501 src_rect.top = 0;
502 src_rect.right = swapchain->desc.backbuffer_width;
503 src_rect.bottom = swapchain->desc.backbuffer_height;
506 if (dst_rect_in)
507 dst_rect = *dst_rect_in;
508 else
509 GetClientRect(swapchain->win_handle, &dst_rect);
511 if (!render_to_fbo && (dst_rect.left || dst_rect.top
512 || dst_rect.right != swapchain->desc.backbuffer_width
513 || dst_rect.bottom != swapchain->desc.backbuffer_height))
514 render_to_fbo = TRUE;
516 /* Rendering to a window of different size, presenting partial rectangles,
517 * or rendering to a different window needs help from FBO_blit or a textured
518 * draw. Render the swapchain to a FBO in the future.
520 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
521 * all these issues - this fails if the window is smaller than the backbuffer.
523 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
525 surface_load_location(back_buffer, SFLAG_INTEXTURE, NULL);
526 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, FALSE);
527 swapchain->render_to_fbo = TRUE;
528 swapchain_update_draw_bindings(swapchain);
530 else
532 surface_load_location(back_buffer, back_buffer->draw_binding, NULL);
535 if (swapchain->render_to_fbo)
537 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
538 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
539 * not allowed(they need the COPY swapeffect)
541 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
542 * the swap. */
543 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
544 FIXME("Render-to-fbo with WINED3D_SWAP_EFFECT_FLIP\n");
546 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
549 if (swapchain->num_contexts > 1)
550 gl_info->gl_ops.gl.p_glFinish();
552 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
553 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
555 TRACE("SwapBuffers called, Starting new frame\n");
556 /* FPS support */
557 if (TRACE_ON(fps))
559 DWORD time = GetTickCount();
560 ++swapchain->frames;
562 /* every 1.5 seconds */
563 if (time - swapchain->prev_time > 1500)
565 TRACE_(fps)("%p @ approx %.2ffps\n",
566 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
567 swapchain->prev_time = time;
568 swapchain->frames = 0;
572 /* This is disabled, but the code left in for debug purposes.
574 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
575 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
576 * The Debug runtime does the same on Windows. However, a few games do not redraw the
577 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
579 * Tests show that the content of the back buffer after a discard flip is indeed not
580 * reliable, so no game can depend on the exact content. However, it resembles the
581 * old contents in some way, for example by showing fragments at other locations. In
582 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
583 * gets a dark background image. If we clear it with a bright ugly color, the game's
584 * bug shows up much more than it does on Windows, and the players see single pixels
585 * with wrong colors.
586 * (The Max Payne bug has been confirmed on Windows with the debug runtime) */
587 if (FALSE && swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD)
589 static const struct wined3d_color cyan = {0.0f, 1.0f, 1.0f, 1.0f};
591 TRACE("Clearing the color buffer with cyan color\n");
593 wined3d_device_clear(swapchain->device, 0, NULL,
594 WINED3DCLEAR_TARGET, &cyan, 1.0f, 0);
597 if (!swapchain->render_to_fbo && ((swapchain->front_buffer->flags & SFLAG_INSYSMEM)
598 || (back_buffer->flags & SFLAG_INSYSMEM)))
600 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
601 * Doesn't work with render_to_fbo because we're not flipping
603 struct wined3d_surface *front = swapchain->front_buffer;
605 if (front->resource.size == back_buffer->resource.size)
607 DWORD fbflags;
608 flip_surface(front, back_buffer);
610 /* Tell the front buffer surface that is has been modified. However,
611 * the other locations were preserved during that, so keep the flags.
612 * This serves to update the emulated overlay, if any. */
613 fbflags = front->flags;
614 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
615 front->flags = fbflags;
617 else
619 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
620 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, TRUE);
623 else
625 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
626 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
627 * and INTEXTURE copies can keep their old content if they have any defined content.
628 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
629 * the texture / sysmem copy needs to be reloaded from the drawable
631 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
632 surface_modify_location(back_buffer, back_buffer->draw_binding, TRUE);
635 if (fb->depth_stencil)
637 if (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
638 || fb->depth_stencil->flags & SFLAG_DISCARD)
640 surface_modify_ds_location(fb->depth_stencil, SFLAG_DISCARDED,
641 fb->depth_stencil->resource.width,
642 fb->depth_stencil->resource.height);
643 if (fb->depth_stencil == swapchain->device->onscreen_depth_stencil)
645 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
646 swapchain->device->onscreen_depth_stencil = NULL;
651 context_release(context);
654 static const struct wined3d_swapchain_ops swapchain_gl_ops =
656 swapchain_gl_present,
659 /* Helper function that blits the front buffer contents to the target window. */
660 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
662 const struct wined3d_surface *front;
663 POINT offset = {0, 0};
664 HDC src_dc, dst_dc;
665 RECT draw_rect;
666 HWND window;
668 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
670 front = swapchain->front_buffer;
671 if (!(front->resource.usage & WINED3DUSAGE_RENDERTARGET))
672 return;
674 if (front->resource.map_count)
675 ERR("Trying to blit a mapped surface.\n");
677 TRACE("Copying surface %p to screen.\n", front);
679 src_dc = front->hDC;
680 window = swapchain->win_handle;
681 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
683 /* Front buffer coordinates are screen coordinates. Map them to the
684 * destination window if not fullscreened. */
685 if (swapchain->desc.windowed)
686 ClientToScreen(window, &offset);
688 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
690 draw_rect.left = 0;
691 draw_rect.right = front->resource.width;
692 draw_rect.top = 0;
693 draw_rect.bottom = front->resource.height;
695 if (rect)
696 IntersectRect(&draw_rect, &draw_rect, rect);
698 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
699 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
700 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
701 ReleaseDC(window, dst_dc);
704 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
705 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
707 struct wined3d_surface *front, *back;
709 front = swapchain->front_buffer;
710 back = swapchain->back_buffers[0];
712 /* Flip the DC. */
714 HDC tmp;
715 tmp = front->hDC;
716 front->hDC = back->hDC;
717 back->hDC = tmp;
720 /* Flip the DIBsection. */
722 HBITMAP tmp;
723 tmp = front->dib.DIBsection;
724 front->dib.DIBsection = back->dib.DIBsection;
725 back->dib.DIBsection = tmp;
728 /* Flip the surface data. */
730 void *tmp;
732 tmp = front->dib.bitmap_data;
733 front->dib.bitmap_data = back->dib.bitmap_data;
734 back->dib.bitmap_data = tmp;
736 tmp = front->resource.allocatedMemory;
737 front->resource.allocatedMemory = back->resource.allocatedMemory;
738 back->resource.allocatedMemory = tmp;
740 if (front->resource.heap_memory)
741 ERR("GDI Surface %p has heap memory allocated.\n", front);
743 if (back->resource.heap_memory)
744 ERR("GDI Surface %p has heap memory allocated.\n", back);
747 /* FPS support */
748 if (TRACE_ON(fps))
750 static LONG prev_time, frames;
751 DWORD time = GetTickCount();
753 ++frames;
755 /* every 1.5 seconds */
756 if (time - prev_time > 1500)
758 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
759 prev_time = time;
760 frames = 0;
764 x11_copy_to_screen(swapchain, NULL);
767 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
769 swapchain_gdi_present,
772 void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
774 RECT client_rect;
776 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
777 return;
779 if (!swapchain->desc.backbuffer_count)
781 TRACE("Single buffered rendering.\n");
782 swapchain->render_to_fbo = FALSE;
783 return;
786 GetClientRect(swapchain->win_handle, &client_rect);
788 TRACE("Backbuffer %ux%u, window %ux%u.\n",
789 swapchain->desc.backbuffer_width,
790 swapchain->desc.backbuffer_height,
791 client_rect.right, client_rect.bottom);
792 TRACE("Multisample type %#x, quality %#x.\n",
793 swapchain->desc.multisample_type,
794 swapchain->desc.multisample_quality);
796 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
797 && swapchain->desc.backbuffer_width == client_rect.right
798 && swapchain->desc.backbuffer_height == client_rect.bottom)
800 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
801 swapchain->render_to_fbo = FALSE;
802 return;
805 TRACE("Rendering to FBO.\n");
806 swapchain->render_to_fbo = TRUE;
809 /* Do not call while under the GL lock. */
810 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
811 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
813 const struct wined3d_adapter *adapter = device->adapter;
814 struct wined3d_resource_desc surface_desc;
815 BOOL displaymode_set = FALSE;
816 RECT client_rect;
817 HWND window;
818 HRESULT hr;
819 UINT i;
821 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
823 FIXME("The application requested %u back buffers, this is not supported.\n",
824 desc->backbuffer_count);
825 return WINED3DERR_INVALIDCALL;
828 if (desc->backbuffer_count > 1)
830 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
831 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
834 if (device->wined3d->flags & WINED3D_NO3D)
835 swapchain->swapchain_ops = &swapchain_gdi_ops;
836 else
837 swapchain->swapchain_ops = &swapchain_gl_ops;
839 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
841 swapchain->device = device;
842 swapchain->parent = parent;
843 swapchain->parent_ops = parent_ops;
844 swapchain->ref = 1;
845 swapchain->win_handle = window;
846 swapchain->device_window = window;
848 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
849 adapter->ordinal, &swapchain->original_mode, NULL)))
851 ERR("Failed to get current display mode, hr %#x.\n", hr);
852 goto err;
855 GetClientRect(window, &client_rect);
856 if (desc->windowed
857 && (!desc->backbuffer_width || !desc->backbuffer_height
858 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
861 if (!desc->backbuffer_width)
863 desc->backbuffer_width = client_rect.right;
864 TRACE("Updating width to %u.\n", desc->backbuffer_width);
867 if (!desc->backbuffer_height)
869 desc->backbuffer_height = client_rect.bottom;
870 TRACE("Updating height to %u.\n", desc->backbuffer_height);
873 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
875 desc->backbuffer_format = swapchain->original_mode.format_id;
876 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
879 swapchain->desc = *desc;
880 swapchain_update_render_to_fbo(swapchain);
882 TRACE("Creating front buffer.\n");
884 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
885 surface_desc.format = swapchain->desc.backbuffer_format;
886 surface_desc.multisample_type = swapchain->desc.multisample_type;
887 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
888 surface_desc.usage = WINED3DUSAGE_RENDERTARGET;
889 surface_desc.pool = WINED3D_POOL_DEFAULT;
890 surface_desc.width = swapchain->desc.backbuffer_width;
891 surface_desc.height = swapchain->desc.backbuffer_height;
892 surface_desc.depth = 1;
893 surface_desc.size = 0;
895 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
896 parent, &surface_desc, &swapchain->front_buffer)))
898 WARN("Failed to create front buffer, hr %#x.\n", hr);
899 goto err;
902 surface_set_swapchain(swapchain->front_buffer, swapchain);
903 if (!(device->wined3d->flags & WINED3D_NO3D))
904 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
906 /* MSDN says we're only allowed a single fullscreen swapchain per device,
907 * so we should really check to see if there is a fullscreen swapchain
908 * already. Does a single head count as full screen? */
910 if (!desc->windowed)
912 struct wined3d_display_mode mode;
914 /* Change the display settings */
915 mode.width = desc->backbuffer_width;
916 mode.height = desc->backbuffer_height;
917 mode.format_id = desc->backbuffer_format;
918 mode.refresh_rate = desc->refresh_rate;
919 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
921 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, adapter->ordinal, &mode)))
923 WARN("Failed to set display mode, hr %#x.\n", hr);
924 goto err;
926 displaymode_set = TRUE;
929 if (!(device->wined3d->flags & WINED3D_NO3D))
931 static const enum wined3d_format_id formats[] =
933 WINED3DFMT_D24_UNORM_S8_UINT,
934 WINED3DFMT_D32_UNORM,
935 WINED3DFMT_R24_UNORM_X8_TYPELESS,
936 WINED3DFMT_D16_UNORM,
937 WINED3DFMT_S1_UINT_D15_UNORM
940 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
942 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
943 if (!swapchain->context)
945 ERR("Failed to create the context array.\n");
946 hr = E_OUTOFMEMORY;
947 goto err;
949 swapchain->num_contexts = 1;
951 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
952 * You are able to add a depth + stencil surface at a later stage when you need it.
953 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
954 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
955 * context, need torecreate shaders, textures and other resources.
957 * The context manager already takes care of the state problem and for the other tasks code from Reset
958 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
959 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
960 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
961 * issue needs to be fixed. */
962 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
964 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
965 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
966 if (swapchain->context[0]) break;
967 TRACE("Depth stencil format %s is not supported, trying next format\n",
968 debug_d3dformat(formats[i]));
971 if (!swapchain->context[0])
973 WARN("Failed to create context.\n");
974 hr = WINED3DERR_NOTAVAILABLE;
975 goto err;
978 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
979 && (!desc->enable_auto_depth_stencil
980 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
982 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
984 context_release(swapchain->context[0]);
987 if (swapchain->desc.backbuffer_count > 0)
989 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
990 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
991 if (!swapchain->back_buffers)
993 ERR("Failed to allocate backbuffer array memory.\n");
994 hr = E_OUTOFMEMORY;
995 goto err;
998 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1000 TRACE("Creating back buffer %u.\n", i);
1001 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1002 parent, &surface_desc, &swapchain->back_buffers[i])))
1004 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1005 goto err;
1007 surface_set_swapchain(swapchain->back_buffers[i], swapchain);
1011 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1012 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
1014 TRACE("Creating depth/stencil buffer.\n");
1015 if (!device->auto_depth_stencil)
1017 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
1018 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
1020 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1021 device->device_parent, &surface_desc, &device->auto_depth_stencil)))
1023 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
1024 goto err;
1029 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1031 return WINED3D_OK;
1033 err:
1034 if (displaymode_set)
1036 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1037 adapter->ordinal, &swapchain->original_mode)))
1038 ERR("Failed to restore display mode.\n");
1039 ClipCursor(NULL);
1042 if (swapchain->back_buffers)
1044 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1046 if (swapchain->back_buffers[i])
1048 surface_set_swapchain(swapchain->back_buffers[i], NULL);
1049 wined3d_surface_decref(swapchain->back_buffers[i]);
1052 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1055 if (swapchain->context)
1057 if (swapchain->context[0])
1059 context_release(swapchain->context[0]);
1060 context_destroy(device, swapchain->context[0]);
1061 swapchain->num_contexts = 0;
1063 HeapFree(GetProcessHeap(), 0, swapchain->context);
1066 if (swapchain->front_buffer)
1068 surface_set_swapchain(swapchain->front_buffer, NULL);
1069 wined3d_surface_decref(swapchain->front_buffer);
1072 return hr;
1075 /* Do not call while under the GL lock. */
1076 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1077 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1079 struct wined3d_swapchain *object;
1080 HRESULT hr;
1082 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1083 device, desc, parent, parent_ops, swapchain);
1085 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1086 if (!object)
1087 return E_OUTOFMEMORY;
1089 hr = swapchain_init(object, device, desc, parent, parent_ops);
1090 if (FAILED(hr))
1092 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1093 HeapFree(GetProcessHeap(), 0, object);
1094 return hr;
1097 TRACE("Created swapchain %p.\n", object);
1098 *swapchain = object;
1100 return WINED3D_OK;
1103 /* Do not call while under the GL lock. */
1104 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1106 struct wined3d_context **newArray;
1107 struct wined3d_context *ctx;
1109 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1111 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1113 ERR("Failed to create a new context for the swapchain\n");
1114 return NULL;
1116 context_release(ctx);
1118 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1119 if(!newArray) {
1120 ERR("Out of memory when trying to allocate a new context array\n");
1121 context_destroy(swapchain->device, ctx);
1122 return NULL;
1124 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1125 HeapFree(GetProcessHeap(), 0, swapchain->context);
1126 newArray[swapchain->num_contexts] = ctx;
1127 swapchain->context = newArray;
1128 swapchain->num_contexts++;
1130 TRACE("Returning context %p\n", ctx);
1131 return ctx;
1134 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1136 unsigned int i;
1138 for (i = 0; i < swapchain->num_contexts; ++i)
1140 context_destroy(swapchain->device, swapchain->context[i]);
1142 swapchain->num_contexts = 0;
1145 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1147 DWORD tid = GetCurrentThreadId();
1148 unsigned int i;
1150 for (i = 0; i < swapchain->num_contexts; ++i)
1152 if (swapchain->context[i]->tid == tid)
1153 return swapchain->context[i];
1156 /* Create a new context for the thread */
1157 return swapchain_create_context(swapchain);
1160 void get_drawable_size_swapchain(const struct wined3d_context *context, UINT *width, UINT *height)
1162 /* The drawable size of an onscreen drawable is the surface size.
1163 * (Actually: The window size, but the surface is created in window size) */
1164 *width = context->current_rt->resource.width;
1165 *height = context->current_rt->resource.height;
1168 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1170 if (!swapchain->backup_dc)
1172 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1174 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1175 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1177 ERR("Failed to create a window.\n");
1178 return NULL;
1181 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1183 ERR("Failed to get a DC.\n");
1184 DestroyWindow(swapchain->backup_wnd);
1185 swapchain->backup_wnd = NULL;
1186 return NULL;
1190 return swapchain->backup_dc;
1193 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1195 UINT i;
1197 surface_update_draw_binding(swapchain->front_buffer);
1199 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1201 surface_update_draw_binding(swapchain->back_buffers[i]);