VirtualBox

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

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

wddm/3d: wine multi-swapchain fixes

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