VirtualBox

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

Last change on this file since 47755 was 44306, checked in by vboxsync, 12 years ago

wined3d: 1.fix shader gen 2.use vboxogl PixelFormat API directly

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

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