VirtualBox

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

最後變更 在這個檔案從38331是 38331,由 vboxsync 提交於 14 年 前

wddm/wine: fix winsat crashes (context adjustments for multiswapchains), basics for window creation inside wine

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

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