VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c@ 33252

Last change on this file since 33252 was 33093, checked in by vboxsync, 14 years ago

crOpenGL/wddm: huge aero speedup with GL_EXT_framebuffer_blit, working but more cases to check/fix

  • Property svn:eol-style set to native
File size: 38.9 KB
Line 
1/*
2 *IDirect3DSwapChain9 implementation
3 *
4 *Copyright 2002-2003 Jason Edmeades
5 *Copyright 2002-2003 Raphael Junqueira
6 *Copyright 2005 Oliver Stieber
7 *Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 *
9 *This library is free software; you can redistribute it and/or
10 *modify it under the terms of the GNU Lesser General Public
11 *License as published by the Free Software Foundation; either
12 *version 2.1 of the License, or (at your option) any later version.
13 *
14 *This library is distributed in the hope that it will be useful,
15 *but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 *Lesser General Public License for more details.
18 *
19 *You should have received a copy of the GNU Lesser General Public
20 *License along with this library; if not, write to the Free Software
21 *Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24/*
25 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
26 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
27 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
28 * a choice of LGPL license versions is made available with the language indicating
29 * that LGPLv2 or any later version may be used, or where a choice of which version
30 * of the LGPL is applied is otherwise unspecified.
31 */
32
33#include "config.h"
34#include "wined3d_private.h"
35
36
37/*TODO: some of the additional parameters may be required to
38 set the gamma ramp (for some weird reason microsoft have left swap gammaramp in device
39 but it operates on a swapchain, it may be a good idea to move it to IWineD3DSwapChain for IWineD3D)*/
40
41
42WINE_DEFAULT_DEBUG_CHANNEL(d3d);
43WINE_DECLARE_DEBUG_CHANNEL(fps);
44
45#define GLINFO_LOCATION This->device->adapter->gl_info
46
47/*IWineD3DSwapChain parts follow: */
48static void WINAPI IWineD3DSwapChainImpl_Destroy(IWineD3DSwapChain *iface)
49{
50 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
51 WINED3DDISPLAYMODE mode;
52 unsigned int i;
53
54 TRACE("Destroying swapchain %p\n", iface);
55
56 IWineD3DSwapChain_SetGammaRamp(iface, 0, &This->orig_gamma);
57
58 /* Release the swapchain's draw buffers. Make sure This->backBuffer[0] is
59 * the last buffer to be destroyed, FindContext() depends on that. */
60 if (This->frontBuffer)
61 {
62 IWineD3DSurface_SetContainer(This->frontBuffer, 0);
63 if (IWineD3DSurface_Release(This->frontBuffer))
64 {
65 WARN("(%p) Something's still holding the front buffer (%p).\n",
66 This, This->frontBuffer);
67 }
68 This->frontBuffer = NULL;
69 }
70
71 if (This->backBuffer)
72 {
73 UINT i = This->presentParms.BackBufferCount;
74
75 while (i--)
76 {
77 IWineD3DSurface_SetContainer(This->backBuffer[i], 0);
78 if (IWineD3DSurface_Release(This->backBuffer[i]))
79 WARN("(%p) Something's still holding back buffer %u (%p).\n",
80 This, i, This->backBuffer[i]);
81 }
82 HeapFree(GetProcessHeap(), 0, This->backBuffer);
83 This->backBuffer = NULL;
84 }
85#ifndef VBOX_WITH_WDDM
86 for (i = 0; i < This->num_contexts; ++i)
87 {
88 context_destroy(This->device, This->context[i]);
89 }
90#else
91 IWineD3DDevice_RemoveSwapChain(This->device, This);
92 if (!This->device->NumberOfSwapChains)
93#endif
94 {
95 /* Restore the screen resolution if we rendered in fullscreen
96 * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
97 * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
98 * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
99 */
100 if(This->presentParms.Windowed == FALSE && This->presentParms.AutoRestoreDisplayMode) {
101 mode.Width = This->orig_width;
102 mode.Height = This->orig_height;
103 mode.RefreshRate = 0;
104 mode.Format = This->orig_fmt;
105 IWineD3DDevice_SetDisplayMode((IWineD3DDevice *)This->device, 0, &mode);
106 }
107 }
108#ifdef VBOX_WITH_WDDM
109 if(This->device_window) {
110 ReleaseDC(This->device_window, This->hDC);
111 }
112#else
113 HeapFree(GetProcessHeap(), 0, This->context);
114#endif
115 HeapFree(GetProcessHeap(), 0, This);
116}
117
118/* A GL context is provided by the caller */
119static void swapchain_blit(IWineD3DSwapChainImpl *This, struct wined3d_context *context,
120 const RECT *src_rect, const RECT *dst_rect)
121{
122 IWineD3DDeviceImpl *device = This->device;
123 IWineD3DSurfaceImpl *backbuffer = ((IWineD3DSurfaceImpl *) This->backBuffer[0]);
124 UINT src_w = src_rect->right - src_rect->left;
125 UINT src_h = src_rect->bottom - src_rect->top;
126 GLenum gl_filter;
127 const struct wined3d_gl_info *gl_info = context->gl_info;
128
129 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
130 This, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
131
132 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
133 gl_filter = GL_NEAREST;
134 else
135 gl_filter = GL_LINEAR;
136
137 if (0 && gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format_desc->color_fixup))
138 {
139 ENTER_GL();
140 context_bind_fbo(context, GL_READ_FRAMEBUFFER, &context->src_fbo);
141 context_attach_surface_fbo(context, GL_READ_FRAMEBUFFER, 0, backbuffer);
142 context_attach_depth_stencil_fbo(context, GL_READ_FRAMEBUFFER, NULL, FALSE);
143
144 context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, NULL);
145 context_set_draw_buffer(context, GL_BACK);
146
147 glDisable(GL_SCISSOR_TEST);
148 IWineD3DDeviceImpl_MarkStateDirty(This->device, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE));
149
150 /* Note that the texture is upside down */
151 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
152 dst_rect->left, dst_rect->bottom, dst_rect->right, dst_rect->top,
153 GL_COLOR_BUFFER_BIT, gl_filter);
154 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
155 LEAVE_GL();
156 }
157 else
158 {
159 struct wined3d_context *context2;
160 float tex_left = src_rect->left;
161 float tex_top = src_rect->top;
162 float tex_right = src_rect->right;
163 float tex_bottom = src_rect->bottom;
164
165 context2 = context_acquire(This->device, This->backBuffer[0], CTXUSAGE_BLIT);
166
167 if(backbuffer->Flags & SFLAG_NORMCOORD)
168 {
169 tex_left /= src_w;
170 tex_right /= src_w;
171 tex_top /= src_h;
172 tex_bottom /= src_h;
173 }
174
175 if (is_complex_fixup(backbuffer->resource.format_desc->color_fixup))
176 gl_filter = GL_NEAREST;
177
178 ENTER_GL();
179 context_bind_fbo(context2, GL_DRAW_FRAMEBUFFER, NULL);
180
181 /* Set up the texture. The surface is not in a IWineD3D*Texture container,
182 * so there are no d3d texture settings to dirtify
183 */
184 device->blitter->set_shader((IWineD3DDevice *) device, backbuffer);
185 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
186 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
187
188 context_set_draw_buffer(context, GL_BACK);
189
190 /* Set the viewport to the destination rectandle, disable any projection
191 * transformation set up by CTXUSAGE_BLIT, and draw a (-1,-1)-(1,1) quad.
192 *
193 * Back up viewport and matrix to avoid breaking last_was_blit
194 *
195 * Note that CTXUSAGE_BLIT set up viewport and ortho to match the surface
196 * size - we want the GL drawable(=window) size.
197 */
198 glPushAttrib(GL_VIEWPORT_BIT);
199 glViewport(dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom);
200 glMatrixMode(GL_PROJECTION);
201 glPushMatrix();
202 glLoadIdentity();
203
204 glBegin(GL_QUADS);
205 /* bottom left */
206 glTexCoord2f(tex_left, tex_bottom);
207 glVertex2i(-1, -1);
208
209 /* top left */
210 glTexCoord2f(tex_left, tex_top);
211 glVertex2i(-1, 1);
212
213 /* top right */
214 glTexCoord2f(tex_right, tex_top);
215 glVertex2i(1, 1);
216
217 /* bottom right */
218 glTexCoord2f(tex_right, tex_bottom);
219 glVertex2i(1, -1);
220 glEnd();
221
222 glPopMatrix();
223 glPopAttrib();
224
225 device->blitter->unset_shader((IWineD3DDevice *) device);
226 checkGLcall("Swapchain present blit(manual)\n");
227 LEAVE_GL();
228
229 context_release(context2);
230 }
231}
232
233static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
234 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
235 struct wined3d_context *context;
236 RECT src_rect, dst_rect;
237 BOOL render_to_fbo;
238 unsigned int sync;
239 int retval;
240
241 IWineD3DSwapChain_SetDestWindowOverride(iface, hDestWindowOverride);
242
243 context = context_acquire(This->device, This->backBuffer[0], CTXUSAGE_RESOURCELOAD);
244 if (!context->valid)
245 {
246 context_release(context);
247 WARN("Invalid context, skipping present.\n");
248 return WINED3D_OK;
249 }
250
251 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
252 if (This->device->bCursorVisible && This->device->cursorTexture)
253 {
254 IWineD3DSurfaceImpl cursor;
255 RECT destRect =
256 {
257 This->device->xScreenSpace - This->device->xHotSpot,
258 This->device->yScreenSpace - This->device->yHotSpot,
259 This->device->xScreenSpace + This->device->cursorWidth - This->device->xHotSpot,
260 This->device->yScreenSpace + This->device->cursorHeight - This->device->yHotSpot,
261 };
262 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
263 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
264 * the application because we are only supposed to copy the information out. Using a fake surface
265 * allows to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
266 */
267 memset(&cursor, 0, sizeof(cursor));
268 cursor.lpVtbl = &IWineD3DSurface_Vtbl;
269 cursor.resource.ref = 1;
270 cursor.resource.device = This->device;
271 cursor.resource.pool = WINED3DPOOL_SCRATCH;
272 cursor.resource.format_desc = getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, context->gl_info);
273 cursor.resource.resourceType = WINED3DRTYPE_SURFACE;
274 cursor.texture_name = This->device->cursorTexture;
275 cursor.texture_target = GL_TEXTURE_2D;
276 cursor.texture_level = 0;
277 cursor.currentDesc.Width = This->device->cursorWidth;
278 cursor.currentDesc.Height = This->device->cursorHeight;
279 /* The cursor must have pow2 sizes */
280 cursor.pow2Width = cursor.currentDesc.Width;
281 cursor.pow2Height = cursor.currentDesc.Height;
282 /* The surface is in the texture */
283 cursor.Flags |= SFLAG_INTEXTURE;
284 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
285 * which is exactly what we want :-)
286 */
287 if (This->presentParms.Windowed) {
288 MapWindowPoints(NULL, This->win_handle, (LPPOINT)&destRect, 2);
289 }
290 IWineD3DSurface_Blt(This->backBuffer[0], &destRect, (IWineD3DSurface *)&cursor,
291 NULL, WINEDDBLT_KEYSRC, NULL, WINED3DTEXF_POINT);
292 }
293
294 if (This->device->logo_surface)
295 {
296 /* Blit the logo into the upper left corner of the drawable. */
297 IWineD3DSurface_BltFast(This->backBuffer[0], 0, 0, This->device->logo_surface, NULL, WINEDDBLTFAST_SRCCOLORKEY);
298 }
299
300 TRACE("Presenting HDC %p.\n", context->hdc);
301
302 render_to_fbo = This->render_to_fbo;
303
304 if (pSourceRect)
305 {
306 src_rect = *pSourceRect;
307 if (!render_to_fbo && (src_rect.left || src_rect.top
308 || src_rect.right != This->presentParms.BackBufferWidth
309 || src_rect.bottom != This->presentParms.BackBufferHeight))
310 {
311 render_to_fbo = TRUE;
312 }
313 }
314 else
315 {
316 src_rect.left = 0;
317 src_rect.top = 0;
318 src_rect.right = This->presentParms.BackBufferWidth;
319 src_rect.bottom = This->presentParms.BackBufferHeight;
320 }
321
322 if (pDestRect) dst_rect = *pDestRect;
323 else GetClientRect(This->win_handle, &dst_rect);
324
325 if (!render_to_fbo && (dst_rect.left || dst_rect.top
326 || dst_rect.right != This->presentParms.BackBufferWidth
327 || dst_rect.bottom != This->presentParms.BackBufferHeight))
328 {
329 render_to_fbo = TRUE;
330 }
331
332 /* Rendering to a window of different size, presenting partial rectangles,
333 * or rendering to a different window needs help from FBO_blit or a textured
334 * draw. Render the swapchain to a FBO in the future.
335 *
336 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
337 * all these issues - this fails if the window is smaller than the backbuffer.
338 */
339 if (!This->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
340 {
341 IWineD3DSurface_LoadLocation(This->backBuffer[0], SFLAG_INTEXTURE, NULL);
342 IWineD3DSurface_ModifyLocation(This->backBuffer[0], SFLAG_INDRAWABLE, FALSE);
343 This->render_to_fbo = TRUE;
344
345 /* Force the context manager to update the render target configuration next draw. */
346 context->current_rt = NULL;
347 }
348
349 if(This->render_to_fbo)
350 {
351 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
352 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
353 * not allowed(they need the COPY swapeffect)
354 *
355 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
356 * the swap
357 */
358 if(This->presentParms.SwapEffect == WINED3DSWAPEFFECT_FLIP )
359 {
360 FIXME("Render-to-fbo with WINED3DSWAPEFFECT_FLIP\n");
361 }
362
363 swapchain_blit(This, context, &src_rect, &dst_rect);
364 }
365
366#ifdef VBOX_WITH_WDDM
367 if (This->device->numContexts > 1) wglFinish();
368#else
369 if (This->num_contexts > 1) wglFinish();
370#endif
371 SwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
372
373 TRACE("SwapBuffers called, Starting new frame\n");
374 /* FPS support */
375 if (TRACE_ON(fps))
376 {
377 DWORD time = GetTickCount();
378 This->frames++;
379 /* every 1.5 seconds */
380 if (time - This->prev_time > 1500) {
381 TRACE_(fps)("%p @ approx %.2ffps\n", This, 1000.0*This->frames/(time - This->prev_time));
382 This->prev_time = time;
383 This->frames = 0;
384 }
385 }
386
387#if defined(FRAME_DEBUGGING)
388{
389 if (GetFileAttributesA("C:\\D3DTRACE") != INVALID_FILE_ATTRIBUTES) {
390 if (!isOn) {
391 isOn = TRUE;
392 FIXME("Enabling D3D Trace\n");
393 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 1);
394#if defined(SHOW_FRAME_MAKEUP)
395 FIXME("Singe Frame snapshots Starting\n");
396 isDumpingFrames = TRUE;
397 ENTER_GL();
398 glClear(GL_COLOR_BUFFER_BIT);
399 LEAVE_GL();
400#endif
401
402#if defined(SINGLE_FRAME_DEBUGGING)
403 } else {
404#if defined(SHOW_FRAME_MAKEUP)
405 FIXME("Singe Frame snapshots Finishing\n");
406 isDumpingFrames = FALSE;
407#endif
408 FIXME("Singe Frame trace complete\n");
409 DeleteFileA("C:\\D3DTRACE");
410 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
411#endif
412 }
413 } else {
414 if (isOn) {
415 isOn = FALSE;
416#if defined(SHOW_FRAME_MAKEUP)
417 FIXME("Single Frame snapshots Finishing\n");
418 isDumpingFrames = FALSE;
419#endif
420 FIXME("Disabling D3D Trace\n");
421 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
422 }
423 }
424}
425#endif
426
427 /* This is disabled, but the code left in for debug purposes.
428 *
429 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
430 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
431 * The Debug runtime does the same on Windows. However, a few games do not redraw the
432 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
433 *
434 * Tests show that the content of the back buffer after a discard flip is indeed not
435 * reliable, so no game can depend on the exact content. However, it resembles the
436 * old contents in some way, for example by showing fragments at other locations. In
437 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
438 * gets a dark background image. If we clear it with a bright ugly color, the game's
439 * bug shows up much more than it does on Windows, and the players see single pixels
440 * with wrong colors.
441 * (The Max Payne bug has been confirmed on Windows with the debug runtime)
442 */
443 if (FALSE && This->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD) {
444 TRACE("Clearing the color buffer with cyan color\n");
445
446 IWineD3DDevice_Clear((IWineD3DDevice *)This->device, 0, NULL,
447 WINED3DCLEAR_TARGET, 0xff00ffff, 1.0f, 0);
448 }
449
450 if(!This->render_to_fbo &&
451 ( ((IWineD3DSurfaceImpl *) This->frontBuffer)->Flags & SFLAG_INSYSMEM ||
452 ((IWineD3DSurfaceImpl *) This->backBuffer[0])->Flags & SFLAG_INSYSMEM ) ) {
453 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
454 * Doesn't work with render_to_fbo because we're not flipping
455 */
456 IWineD3DSurfaceImpl *front = (IWineD3DSurfaceImpl *) This->frontBuffer;
457 IWineD3DSurfaceImpl *back = (IWineD3DSurfaceImpl *) This->backBuffer[0];
458
459 if(front->resource.size == back->resource.size) {
460 DWORD fbflags;
461 flip_surface(front, back);
462
463 /* Tell the front buffer surface that is has been modified. However,
464 * the other locations were preserved during that, so keep the flags.
465 * This serves to update the emulated overlay, if any
466 */
467 fbflags = front->Flags;
468 IWineD3DSurface_ModifyLocation(This->frontBuffer, SFLAG_INDRAWABLE, TRUE);
469 front->Flags = fbflags;
470 } else {
471 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) front, SFLAG_INDRAWABLE, TRUE);
472 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) back, SFLAG_INDRAWABLE, TRUE);
473 }
474 } else {
475 IWineD3DSurface_ModifyLocation(This->frontBuffer, SFLAG_INDRAWABLE, TRUE);
476 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
477 * and INTEXTURE copies can keep their old content if they have any defined content.
478 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
479 * the texture / sysmem copy needs to be reloaded from the drawable
480 */
481 if(This->presentParms.SwapEffect == WINED3DSWAPEFFECT_FLIP) {
482 IWineD3DSurface_ModifyLocation(This->backBuffer[0], SFLAG_INDRAWABLE, TRUE);
483 }
484 }
485
486 if (This->device->stencilBufferTarget)
487 {
488 if (This->presentParms.Flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
489 || ((IWineD3DSurfaceImpl *)This->device->stencilBufferTarget)->Flags & SFLAG_DISCARD)
490 {
491 surface_modify_ds_location(This->device->stencilBufferTarget, SFLAG_DS_DISCARDED);
492 }
493 }
494
495 if (This->presentParms.PresentationInterval != WINED3DPRESENT_INTERVAL_IMMEDIATE
496 && context->gl_info->supported[SGI_VIDEO_SYNC])
497 {
498 retval = GL_EXTCALL(glXGetVideoSyncSGI(&sync));
499 if(retval != 0) {
500 ERR("glXGetVideoSyncSGI failed(retval = %d\n", retval);
501 }
502
503 switch(This->presentParms.PresentationInterval) {
504 case WINED3DPRESENT_INTERVAL_DEFAULT:
505 case WINED3DPRESENT_INTERVAL_ONE:
506 if(sync <= This->vSyncCounter) {
507 retval = GL_EXTCALL(glXWaitVideoSyncSGI(1, 0, &This->vSyncCounter));
508 } else {
509 This->vSyncCounter = sync;
510 }
511 break;
512 case WINED3DPRESENT_INTERVAL_TWO:
513 if(sync <= This->vSyncCounter + 1) {
514 retval = GL_EXTCALL(glXWaitVideoSyncSGI(2, This->vSyncCounter & 0x1, &This->vSyncCounter));
515 } else {
516 This->vSyncCounter = sync;
517 }
518 break;
519 case WINED3DPRESENT_INTERVAL_THREE:
520 if(sync <= This->vSyncCounter + 2) {
521 retval = GL_EXTCALL(glXWaitVideoSyncSGI(3, This->vSyncCounter % 0x3, &This->vSyncCounter));
522 } else {
523 This->vSyncCounter = sync;
524 }
525 break;
526 case WINED3DPRESENT_INTERVAL_FOUR:
527 if(sync <= This->vSyncCounter + 3) {
528 retval = GL_EXTCALL(glXWaitVideoSyncSGI(4, This->vSyncCounter & 0x3, &This->vSyncCounter));
529 } else {
530 This->vSyncCounter = sync;
531 }
532 break;
533 default:
534 FIXME("Unknown presentation interval %08x\n", This->presentParms.PresentationInterval);
535 }
536 }
537
538 context_release(context);
539
540 TRACE("returning\n");
541 return WINED3D_OK;
542}
543
544static HRESULT WINAPI IWineD3DSwapChainImpl_SetDestWindowOverride(IWineD3DSwapChain *iface, HWND window)
545{
546 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)iface;
547
548 if (!window) window = swapchain->device_window;
549 if (window == swapchain->win_handle) return WINED3D_OK;
550
551 TRACE("Setting swapchain %p window from %p to %p\n", swapchain, swapchain->win_handle, window);
552 swapchain->win_handle = window;
553
554 return WINED3D_OK;
555}
556
557static const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl =
558{
559 /* IUnknown */
560 IWineD3DBaseSwapChainImpl_QueryInterface,
561 IWineD3DBaseSwapChainImpl_AddRef,
562 IWineD3DBaseSwapChainImpl_Release,
563 /* IWineD3DSwapChain */
564 IWineD3DBaseSwapChainImpl_GetParent,
565 IWineD3DSwapChainImpl_Destroy,
566 IWineD3DBaseSwapChainImpl_GetDevice,
567 IWineD3DSwapChainImpl_Present,
568 IWineD3DSwapChainImpl_SetDestWindowOverride,
569 IWineD3DBaseSwapChainImpl_GetFrontBufferData,
570 IWineD3DBaseSwapChainImpl_GetBackBuffer,
571 IWineD3DBaseSwapChainImpl_GetRasterStatus,
572 IWineD3DBaseSwapChainImpl_GetDisplayMode,
573 IWineD3DBaseSwapChainImpl_GetPresentParameters,
574 IWineD3DBaseSwapChainImpl_SetGammaRamp,
575 IWineD3DBaseSwapChainImpl_GetGammaRamp,
576};
577
578static LONG fullscreen_style(LONG style)
579{
580 /* Make sure the window is managed, otherwise we won't get keyboard input. */
581 style |= WS_POPUP | WS_SYSMENU;
582 style &= ~(WS_CAPTION | WS_THICKFRAME);
583
584 return style;
585}
586
587static LONG fullscreen_exstyle(LONG exstyle)
588{
589 /* Filter out window decorations. */
590 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
591
592 return exstyle;
593}
594
595void swapchain_setup_fullscreen_window(IWineD3DSwapChainImpl *swapchain, UINT w, UINT h)
596{
597 IWineD3DDeviceImpl *device = swapchain->device;
598 HWND window = swapchain->device_window;
599 BOOL filter_messages;
600 LONG style, exstyle;
601
602 TRACE("Setting up window %p for fullscreen mode.\n", window);
603
604 if (device->style || device->exStyle)
605 {
606 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
607 window, device->style, device->exStyle);
608 }
609
610 device->style = GetWindowLongW(window, GWL_STYLE);
611 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
612
613 style = fullscreen_style(device->style);
614 exstyle = fullscreen_exstyle(device->exStyle);
615
616 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
617 device->style, device->exStyle, style, exstyle);
618
619 filter_messages = device->filter_messages;
620 device->filter_messages = TRUE;
621
622 SetWindowLongW(window, GWL_STYLE, style);
623 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
624 SetWindowPos(window, HWND_TOP, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
625
626 device->filter_messages = filter_messages;
627}
628
629void swapchain_restore_fullscreen_window(IWineD3DSwapChainImpl *swapchain)
630{
631 IWineD3DDeviceImpl *device = swapchain->device;
632 HWND window = swapchain->device_window;
633 BOOL filter_messages;
634 LONG style, exstyle;
635
636 if (!device->style && !device->exStyle) return;
637
638 TRACE("Restoring window style of window %p to %08x, %08x.\n",
639 window, device->style, device->exStyle);
640
641 style = GetWindowLongW(window, GWL_STYLE);
642 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
643
644 filter_messages = device->filter_messages;
645 device->filter_messages = TRUE;
646
647 /* Only restore the style if the application didn't modify it during the
648 * fullscreen phase. Some applications change it before calling Reset()
649 * when switching between windowed and fullscreen modes (HL2), some
650 * depend on the original style (Eve Online). */
651 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
652 {
653 SetWindowLongW(window, GWL_STYLE, device->style);
654 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
655 }
656 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
657
658 device->filter_messages = filter_messages;
659
660 /* Delete the old values. */
661 device->style = 0;
662 device->exStyle = 0;
663}
664
665
666HRESULT swapchain_init(IWineD3DSwapChainImpl *swapchain, WINED3DSURFTYPE surface_type,
667 IWineD3DDeviceImpl *device, WINED3DPRESENT_PARAMETERS *present_parameters, IUnknown *parent)
668{
669 const struct wined3d_adapter *adapter = device->adapter;
670 const struct wined3d_format_desc *format_desc;
671 BOOL displaymode_set = FALSE;
672 WINED3DDISPLAYMODE mode;
673 RECT client_rect;
674 HWND window;
675 HRESULT hr;
676 UINT i;
677
678 if (present_parameters->BackBufferCount > WINED3DPRESENT_BACK_BUFFER_MAX)
679 {
680 FIXME("The application requested %u back buffers, this is not supported.\n",
681 present_parameters->BackBufferCount);
682 return WINED3DERR_INVALIDCALL;
683 }
684
685 if (present_parameters->BackBufferCount > 1)
686 {
687 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
688 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
689 }
690
691 switch (surface_type)
692 {
693 case SURFACE_GDI:
694 swapchain->lpVtbl = &IWineGDISwapChain_Vtbl;
695 break;
696
697 case SURFACE_OPENGL:
698 swapchain->lpVtbl = &IWineD3DSwapChain_Vtbl;
699 break;
700
701 case SURFACE_UNKNOWN:
702 FIXME("Caller tried to create a SURFACE_UNKNOWN swapchain.\n");
703 return WINED3DERR_INVALIDCALL;
704 }
705
706 window = present_parameters->hDeviceWindow ? present_parameters->hDeviceWindow : device->createParms.hFocusWindow;
707
708 swapchain->device = device;
709 swapchain->parent = parent;
710 swapchain->ref = 1;
711 swapchain->win_handle = window;
712 swapchain->device_window = window;
713#ifdef VBOX_WITH_WDDM
714 Assert(window);
715 swapchain->hDC = GetDC(window);
716 if (!swapchain->hDC)
717 {
718 DWORD winEr = GetLastError();
719 WARN("Failed to get a window DC, winEr %d.\n", winEr);
720 Assert(0);
721 goto err;
722 }
723#endif
724
725 if (!present_parameters->Windowed && window)
726 {
727 swapchain_setup_fullscreen_window(swapchain, present_parameters->BackBufferWidth,
728 present_parameters->BackBufferHeight);
729 }
730
731 IWineD3D_GetAdapterDisplayMode(device->wined3d, adapter->ordinal, &mode);
732 swapchain->orig_width = mode.Width;
733 swapchain->orig_height = mode.Height;
734 swapchain->orig_fmt = mode.Format;
735 format_desc = getFormatDescEntry(mode.Format, &adapter->gl_info);
736
737 GetClientRect(window, &client_rect);
738 if (present_parameters->Windowed
739 && (!present_parameters->BackBufferWidth || !present_parameters->BackBufferHeight
740 || present_parameters->BackBufferFormat == WINED3DFMT_UNKNOWN))
741 {
742
743 if (!present_parameters->BackBufferWidth)
744 {
745 present_parameters->BackBufferWidth = client_rect.right;
746 TRACE("Updating width to %u.\n", present_parameters->BackBufferWidth);
747 }
748
749 if (!present_parameters->BackBufferHeight)
750 {
751 present_parameters->BackBufferHeight = client_rect.bottom;
752 TRACE("Updating height to %u.\n", present_parameters->BackBufferHeight);
753 }
754
755 if (present_parameters->BackBufferFormat == WINED3DFMT_UNKNOWN)
756 {
757 present_parameters->BackBufferFormat = swapchain->orig_fmt;
758 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->orig_fmt));
759 }
760 }
761 swapchain->presentParms = *present_parameters;
762
763 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
764 && present_parameters->BackBufferCount
765 && (present_parameters->BackBufferWidth != client_rect.right
766 || present_parameters->BackBufferHeight != client_rect.bottom))
767 {
768 TRACE("Rendering to FBO. Backbuffer %ux%u, window %ux%u.\n",
769 present_parameters->BackBufferWidth,
770 present_parameters->BackBufferHeight,
771 client_rect.right, client_rect.bottom);
772 swapchain->render_to_fbo = TRUE;
773 }
774
775 TRACE("Creating front buffer.\n");
776 hr = IWineD3DDeviceParent_CreateRenderTarget(device->device_parent, parent,
777 swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
778 swapchain->presentParms.BackBufferFormat, swapchain->presentParms.MultiSampleType,
779 swapchain->presentParms.MultiSampleQuality, TRUE /* Lockable */, &swapchain->frontBuffer);
780 if (FAILED(hr))
781 {
782 WARN("Failed to create front buffer, hr %#x.\n", hr);
783 goto err;
784 }
785
786 IWineD3DSurface_SetContainer(swapchain->frontBuffer, (IWineD3DBase *)swapchain);
787 ((IWineD3DSurfaceImpl *)swapchain->frontBuffer)->Flags |= SFLAG_SWAPCHAIN;
788 if (surface_type == SURFACE_OPENGL)
789 {
790 IWineD3DSurface_ModifyLocation(swapchain->frontBuffer, SFLAG_INDRAWABLE, TRUE);
791 }
792
793 /* MSDN says we're only allowed a single fullscreen swapchain per device,
794 * so we should really check to see if there is a fullscreen swapchain
795 * already. Does a single head count as full screen? */
796
797 if (!present_parameters->Windowed)
798 {
799 WINED3DDISPLAYMODE mode;
800
801 /* Change the display settings */
802 mode.Width = present_parameters->BackBufferWidth;
803 mode.Height = present_parameters->BackBufferHeight;
804 mode.Format = present_parameters->BackBufferFormat;
805 mode.RefreshRate = present_parameters->FullScreen_RefreshRateInHz;
806
807 hr = IWineD3DDevice_SetDisplayMode((IWineD3DDevice *)device, 0, &mode);
808 if (FAILED(hr))
809 {
810 WARN("Failed to set display mode, hr %#x.\n", hr);
811 goto err;
812 }
813 displaymode_set = TRUE;
814 }
815
816#ifndef VBOX_WITH_WDDM
817 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(swapchain->context));
818 if (!swapchain->context)
819 {
820 ERR("Failed to create the context array.\n");
821 hr = E_OUTOFMEMORY;
822 goto err;
823 }
824 swapchain->num_contexts = 1;
825#endif
826
827 if (surface_type == SURFACE_OPENGL)
828 {
829 struct wined3d_context * swapchainContext;
830 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
831
832 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
833 * You are able to add a depth + stencil surface at a later stage when you need it.
834 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
835 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
836 * context, need torecreate shaders, textures and other resources.
837 *
838 * The context manager already takes care of the state problem and for the other tasks code from Reset
839 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
840 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
841 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
842 * issue needs to be fixed. */
843 if (!present_parameters->EnableAutoDepthStencil
844 || swapchain->presentParms.AutoDepthStencilFormat != WINED3DFMT_D24_UNORM_S8_UINT)
845 {
846 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
847 }
848 swapchain->ds_format = getFormatDescEntry(WINED3DFMT_D24_UNORM_S8_UINT, gl_info);
849
850#ifdef VBOX_WITH_WDDM
851 swapchainContext = context_find_create(device, swapchain, (IWineD3DSurfaceImpl *)swapchain->frontBuffer,
852 swapchain->ds_format);
853 if (!swapchainContext)
854#else
855 swapchain->context[0] = context_create(swapchain, (IWineD3DSurfaceImpl *)swapchain->frontBuffer,
856 swapchain->ds_format);
857 if (!swapchain->context[0])
858#endif
859 {
860 WARN("Failed to create context.\n");
861 hr = WINED3DERR_NOTAVAILABLE;
862 goto err;
863 }
864#ifdef VBOX_WITH_WDDM
865 context_release(swapchainContext);
866#else
867 context_release(swapchain->context[0]);
868#endif
869 }
870 else
871 {
872#ifndef VBOX_WITH_WDDM
873 swapchain->context[0] = NULL;
874#endif
875 }
876
877 if (swapchain->presentParms.BackBufferCount > 0)
878 {
879 swapchain->backBuffer = HeapAlloc(GetProcessHeap(), 0,
880 sizeof(*swapchain->backBuffer) * swapchain->presentParms.BackBufferCount);
881 if (!swapchain->backBuffer)
882 {
883 ERR("Failed to allocate backbuffer array memory.\n");
884 hr = E_OUTOFMEMORY;
885 goto err;
886 }
887
888 for (i = 0; i < swapchain->presentParms.BackBufferCount; ++i)
889 {
890 TRACE("Creating back buffer %u.\n", i);
891 hr = IWineD3DDeviceParent_CreateRenderTarget(device->device_parent, parent,
892 swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
893 swapchain->presentParms.BackBufferFormat, swapchain->presentParms.MultiSampleType,
894 swapchain->presentParms.MultiSampleQuality, TRUE /* Lockable */, &swapchain->backBuffer[i]);
895 if (FAILED(hr))
896 {
897 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
898 goto err;
899 }
900
901 IWineD3DSurface_SetContainer(swapchain->backBuffer[i], (IWineD3DBase *)swapchain);
902 ((IWineD3DSurfaceImpl *)swapchain->backBuffer[i])->Flags |= SFLAG_SWAPCHAIN;
903 }
904 }
905
906 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
907 if (present_parameters->EnableAutoDepthStencil && surface_type == SURFACE_OPENGL)
908 {
909 TRACE("Creating depth/stencil buffer.\n");
910 if (!device->auto_depth_stencil_buffer)
911 {
912 hr = IWineD3DDeviceParent_CreateDepthStencilSurface(device->device_parent, parent,
913 swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
914 swapchain->presentParms.AutoDepthStencilFormat, swapchain->presentParms.MultiSampleType,
915 swapchain->presentParms.MultiSampleQuality, FALSE /* FIXME: Discard */,
916 &device->auto_depth_stencil_buffer);
917 if (FAILED(hr))
918 {
919 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
920 goto err;
921 }
922
923 IWineD3DSurface_SetContainer(device->auto_depth_stencil_buffer, NULL);
924 }
925 }
926
927 IWineD3DSwapChain_GetGammaRamp((IWineD3DSwapChain *)swapchain, &swapchain->orig_gamma);
928
929 return WINED3D_OK;
930
931err:
932 if (displaymode_set)
933 {
934 DEVMODEW devmode;
935
936 ClipCursor(NULL);
937
938 /* Change the display settings */
939 memset(&devmode, 0, sizeof(devmode));
940 devmode.dmSize = sizeof(devmode);
941 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
942 devmode.dmBitsPerPel = format_desc->byte_count * 8;
943 devmode.dmPelsWidth = swapchain->orig_width;
944 devmode.dmPelsHeight = swapchain->orig_height;
945 ChangeDisplaySettingsExW(adapter->DeviceName, &devmode, NULL, CDS_FULLSCREEN, NULL);
946 }
947
948 if (swapchain->backBuffer)
949 {
950 for (i = 0; i < swapchain->presentParms.BackBufferCount; ++i)
951 {
952 if (swapchain->backBuffer[i]) IWineD3DSurface_Release(swapchain->backBuffer[i]);
953 }
954 HeapFree(GetProcessHeap(), 0, swapchain->backBuffer);
955 }
956
957#ifdef VBOX_WITH_WDDM
958 if (!device->NumberOfSwapChains)
959 {
960 while (device->numContexts)
961 {
962 context_destroy(device, device->contexts[0]);
963 }
964 }
965#else
966 if (swapchain->context)
967 {
968 if (swapchain->context[0])
969 {
970 context_release(swapchain->context[0]);
971 context_destroy(device, swapchain->context[0]);
972 swapchain->num_contexts = 0;
973 }
974 HeapFree(GetProcessHeap(), 0, swapchain->context);
975 }
976#endif
977
978 if (swapchain->frontBuffer) IWineD3DSurface_Release(swapchain->frontBuffer);
979
980 return hr;
981}
982
983struct wined3d_context *swapchain_create_context_for_thread(IWineD3DSwapChain *iface)
984{
985 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *) iface;
986 struct wined3d_context **newArray;
987 struct wined3d_context *ctx;
988
989 TRACE("Creating a new context for swapchain %p, thread %d\n", This, GetCurrentThreadId());
990
991 if (!(ctx = context_create(This, (IWineD3DSurfaceImpl *)This->frontBuffer, This->ds_format)))
992 {
993 ERR("Failed to create a new context for the swapchain\n");
994 return NULL;
995 }
996 context_release(ctx);
997#ifdef VBOX_WITH_WDDM
998 /* no need to do anything since context gets added to the device context list within the context_create call */
999#else
1000 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * This->num_contexts + 1);
1001 if(!newArray) {
1002 ERR("Out of memory when trying to allocate a new context array\n");
1003 context_destroy(This->device, ctx);
1004 return NULL;
1005 }
1006 memcpy(newArray, This->context, sizeof(*newArray) * This->num_contexts);
1007 HeapFree(GetProcessHeap(), 0, This->context);
1008 newArray[This->num_contexts] = ctx;
1009 This->context = newArray;
1010 This->num_contexts++;
1011#endif
1012 TRACE("Returning context %p\n", ctx);
1013 return ctx;
1014}
1015
1016void get_drawable_size_swapchain(struct wined3d_context *context, UINT *width, UINT *height)
1017{
1018 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)context->current_rt;
1019 /* The drawable size of an onscreen drawable is the surface size.
1020 * (Actually: The window size, but the surface is created in window size) */
1021 *width = surface->currentDesc.Width;
1022 *height = surface->currentDesc.Height;
1023}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette