wined3d: Store shader IDs in the vs and ps impl structures.
[wine] / dlls / wined3d / swapchain_gdi.c
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 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(fps);
29
30 static void WINAPI IWineGDISwapChainImpl_Destroy(IWineD3DSwapChain *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroyRenderback) {
31     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
32     WINED3DDISPLAYMODE mode;
33
34     TRACE("Destroying swapchain %p\n", iface);
35
36     IWineD3DSwapChain_SetGammaRamp(iface, 0, &This->orig_gamma);
37
38     /* release the ref to the front and back buffer parents */
39     if(This->frontBuffer) {
40         IWineD3DSurface_SetContainer(This->frontBuffer, 0);
41         if(D3DCB_DestroyRenderback(This->frontBuffer) > 0) {
42             FIXME("(%p) Something's still holding the front buffer\n",This);
43         }
44     }
45
46     if(This->backBuffer) {
47         int i;
48         for(i = 0; i < This->presentParms.BackBufferCount; i++) {
49             IWineD3DSurface_SetContainer(This->backBuffer[i], 0);
50             if(D3DCB_DestroyRenderback(This->backBuffer[i]) > 0) {
51                 FIXME("(%p) Something's still holding the back buffer\n",This);
52             }
53         }
54         HeapFree(GetProcessHeap(), 0, This->backBuffer);
55     }
56
57     /* Restore the screen resolution if we rendered in fullscreen
58      * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
59      * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
60      * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
61      */
62     if(This->presentParms.Windowed == FALSE && This->presentParms.AutoRestoreDisplayMode) {
63         mode.Width = This->orig_width;
64         mode.Height = This->orig_height;
65         mode.RefreshRate = 0;
66         mode.Format = This->orig_fmt;
67         IWineD3DDevice_SetDisplayMode((IWineD3DDevice *) This->wineD3DDevice, 0, &mode);
68     }
69
70     HeapFree(GetProcessHeap(), 0, This);
71 }
72
73 /*****************************************************************************
74  * x11_copy_to_screen
75  *
76  * Helper function that blts the front buffer contents to the target window
77  *
78  * Params:
79  *  This: Surface to copy from
80  *  rc: Rectangle to copy
81  *
82  *****************************************************************************/
83 void x11_copy_to_screen(IWineD3DSwapChainImpl *This, LPRECT rc) {
84     IWineD3DSurfaceImpl *front = (IWineD3DSurfaceImpl *) This->frontBuffer;
85
86     if(front->resource.usage & WINED3DUSAGE_RENDERTARGET) {
87         POINT offset = {0,0};
88         HWND hDisplayWnd;
89         HDC hDisplayDC;
90         HDC hSurfaceDC = 0;
91         RECT drawrect;
92         TRACE("(%p)->(%p): Copying to screen\n", front, rc);
93
94         hSurfaceDC = front->hDC;
95
96         hDisplayWnd = This->win_handle;
97         hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
98         if(rc) {
99             TRACE(" copying rect (%d,%d)->(%d,%d), offset (%d,%d)\n",
100                   rc->left, rc->top, rc->right, rc->bottom, offset.x, offset.y);
101         }
102
103         /* Front buffer coordinates are screen coordinates. Map them to the destination
104          * window if not fullscreened
105          */
106         if(This->presentParms.Windowed) {
107             ClientToScreen(hDisplayWnd, &offset);
108         }
109 #if 0
110         /* FIXME: This doesn't work... if users really want to run
111         * X in 8bpp, then we need to call directly into display.drv
112         * (or Wine's equivalent), and force a private colormap
113         * without default entries. */
114         if (front->palette) {
115         SelectPalette(hDisplayDC, front->palette->hpal, FALSE);
116         RealizePalette(hDisplayDC); /* sends messages => deadlocks */
117     }
118 #endif
119         drawrect.left   = 0;
120         drawrect.right  = front->currentDesc.Width;
121         drawrect.top    = 0;
122         drawrect.bottom = front->currentDesc.Height;
123
124 #if 0
125         /* TODO: Support clippers */
126         if (front->clipper)
127         {
128         RECT xrc;
129         HWND hwnd = ((IWineD3DClipperImpl *) front->clipper)->hWnd;
130         if (hwnd && GetClientRect(hwnd,&xrc))
131         {
132         OffsetRect(&xrc,offset.x,offset.y);
133         IntersectRect(&drawrect,&drawrect,&xrc);
134     }
135     }
136 #endif
137         if (rc) {
138             IntersectRect(&drawrect,&drawrect,rc);
139         }
140         else {
141             /* Only use this if the caller did not pass a rectangle, since
142             * due to double locking this could be the wrong one ...
143             */
144             if (front->lockedRect.left != front->lockedRect.right) {
145                 IntersectRect(&drawrect,&drawrect,&front->lockedRect);
146             }
147         }
148
149         BitBlt(hDisplayDC,
150                drawrect.left-offset.x, drawrect.top-offset.y,
151                drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
152                hSurfaceDC,
153                drawrect.left, drawrect.top,
154                SRCCOPY);
155         ReleaseDC(hDisplayWnd, hDisplayDC);
156     }
157 }
158
159 static HRESULT WINAPI IWineGDISwapChainImpl_SetDestWindowOverride(IWineD3DSwapChain *iface, HWND window) {
160     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
161
162     This->win_handle = window;
163     return WINED3D_OK;
164 }
165
166 static HRESULT WINAPI IWineGDISwapChainImpl_Present(IWineD3DSwapChain *iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
167     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *) iface;
168     IWineD3DSurfaceImpl *front, *back;
169
170     if(!This->backBuffer) {
171         WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
172         return WINED3DERR_INVALIDCALL;
173     }
174     front = (IWineD3DSurfaceImpl *) This->frontBuffer;
175     back = (IWineD3DSurfaceImpl *) This->backBuffer[0];
176
177     /* Flip the DC */
178     {
179         HDC tmp;
180         tmp = front->hDC;
181         front->hDC = back->hDC;
182         back->hDC = tmp;
183     }
184
185     /* Flip the DIBsection */
186     {
187         HBITMAP tmp;
188         tmp = front->dib.DIBsection;
189         front->dib.DIBsection = back->dib.DIBsection;
190         back->dib.DIBsection = tmp;
191     }
192
193     /* Flip the surface data */
194     {
195         void* tmp;
196
197         tmp = front->dib.bitmap_data;
198         front->dib.bitmap_data = back->dib.bitmap_data;
199         back->dib.bitmap_data = tmp;
200
201         tmp = front->resource.allocatedMemory;
202         front->resource.allocatedMemory = back->resource.allocatedMemory;
203         back->resource.allocatedMemory = tmp;
204
205         if(front->resource.heapMemory) {
206             ERR("GDI Surface %p has heap memory allocated\n", front);
207         }
208         if(back->resource.heapMemory) {
209             ERR("GDI Surface %p has heap memory allocated\n", back);
210         }
211     }
212
213     /* client_memory should not be different, but just in case */
214     {
215         BOOL tmp;
216         tmp = front->dib.client_memory;
217         front->dib.client_memory = back->dib.client_memory;
218         back->dib.client_memory = tmp;
219     }
220
221     /* FPS support */
222     if (TRACE_ON(fps))
223     {
224         static long prev_time, frames;
225
226         DWORD time = GetTickCount();
227         frames++;
228         /* every 1.5 seconds */
229         if (time - prev_time > 1500) {
230             TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
231             prev_time = time;
232             frames = 0;
233         }
234     }
235
236     x11_copy_to_screen(This, NULL);
237
238     return WINED3D_OK;
239 }
240
241 /* FIXME: This should not be needed, the base version is OK */
242 HRESULT WINAPI IWineGDIBaseSwapChainImpl_GetDisplayMode(IWineD3DSwapChain *iface, WINED3DDISPLAYMODE*pMode) {
243     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
244     IWineD3DDeviceImpl *device = This->wineD3DDevice;
245
246     pMode->Width = device->ddraw_width;
247     pMode->Height = device->ddraw_height;
248     pMode->Format = device->ddraw_format;
249     pMode->RefreshRate = 0;
250     return WINED3D_OK;
251 }
252
253 const IWineD3DSwapChainVtbl IWineGDISwapChain_Vtbl =
254 {
255     /* IUnknown */
256     IWineD3DBaseSwapChainImpl_QueryInterface,
257     IWineD3DBaseSwapChainImpl_AddRef,
258     IWineD3DBaseSwapChainImpl_Release,
259     /* IWineD3DSwapChain */
260     IWineD3DBaseSwapChainImpl_GetParent,
261     IWineGDISwapChainImpl_Destroy,
262     IWineD3DBaseSwapChainImpl_GetDevice,
263     IWineGDISwapChainImpl_Present,
264     IWineGDISwapChainImpl_SetDestWindowOverride,
265     IWineD3DBaseSwapChainImpl_GetFrontBufferData,
266     IWineD3DBaseSwapChainImpl_GetBackBuffer,
267     IWineD3DBaseSwapChainImpl_GetRasterStatus,
268     IWineD3DBaseSwapChainImpl_GetDisplayMode,
269     IWineD3DBaseSwapChainImpl_GetPresentParameters,
270     IWineD3DBaseSwapChainImpl_SetGammaRamp,
271     IWineD3DBaseSwapChainImpl_GetGammaRamp
272 };