wined3d: Rewrite IWineD3DSurfaceImpl_BltOverride colorfill codepath.
[wine] / dlls / wined3d / surface.c
1 /*
2  * IWineD3DSurface Implementation
3  *
4  * Copyright 1998 Lionel Ulmer
5  * Copyright 2000-2001 TransGaming Technologies Inc.
6  * Copyright 2002-2005 Jason Edmeades
7  * Copyright 2002-2003 Raphael Junqueira
8  * Copyright 2004 Christian Costa
9  * Copyright 2005 Oliver Stieber
10  * Copyright 2006-2007 Stefan Dösinger for CodeWeavers
11  * Copyright 2007 Henri Verbeet
12  * Copyright 2006-2007 Roderick Colenbrander
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28
29 #include "config.h"
30 #include "wine/port.h"
31 #include "wined3d_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
34 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
35
36 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *surf);
37 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey);
38
39 static void surface_download_data(IWineD3DSurfaceImpl *This) {
40     IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
41
42     if (0 == This->glDescription.textureName) {
43         ERR("Surface does not have a texture, but SFLAG_INTEXTURE is set\n");
44         return;
45     }
46
47     if(myDevice->createParms.BehaviorFlags & WINED3DCREATE_MULTITHREADED) {
48         ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
49     }
50     if(This->Flags & SFLAG_CONVERTED) {
51         FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(This->resource.format));
52         return;
53     }
54
55     ENTER_GL();
56             /* Make sure that a proper texture unit is selected, bind the texture
57     * and dirtify the sampler to restore the texture on the next draw
58             */
59     if (GL_SUPPORT(ARB_MULTITEXTURE)) {
60         GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
61         checkGLcall("glActiveTextureARB");
62     }
63     IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(0));
64     IWineD3DSurface_BindTexture((IWineD3DSurface *) This);
65
66     if (This->resource.format == WINED3DFMT_DXT1 ||
67             This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
68             This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
69         if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) { /* We can assume this as the texture would not have been created otherwise */
70             FIXME("(%p) : Attempting to lock a compressed texture when texture compression isn't supported by opengl\n", This);
71         } else {
72             TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
73                 This->glDescription.glFormat, This->glDescription.glType, This->resource.allocatedMemory);
74
75             if(This->Flags & SFLAG_PBO) {
76                 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
77                 checkGLcall("glBindBufferARB");
78                 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, NULL));
79                 checkGLcall("glGetCompressedTexImageARB()");
80                 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
81                 checkGLcall("glBindBufferARB");
82             } else {
83                 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, This->resource.allocatedMemory));
84                 checkGLcall("glGetCompressedTexImageARB()");
85             }
86         }
87         LEAVE_GL();
88     } else {
89         void *mem;
90         int src_pitch = 0;
91         int dst_pitch = 0;
92
93         if (This->Flags & SFLAG_NONPOW2) {
94             unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
95             src_pitch = This->bytesPerPixel * This->pow2Width;
96             dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
97             src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
98             mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
99         } else {
100             mem = This->resource.allocatedMemory;
101         }
102
103         TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
104                 This->glDescription.glFormat, This->glDescription.glType, mem);
105
106         if(This->Flags & SFLAG_PBO) {
107             GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
108             checkGLcall("glBindBufferARB");
109
110             glGetTexImage(This->glDescription.target, This->glDescription.level, This->glDescription.glFormat,
111                           This->glDescription.glType, NULL);
112             checkGLcall("glGetTexImage()");
113
114             GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
115             checkGLcall("glBindBufferARB");
116         } else {
117             glGetTexImage(This->glDescription.target, This->glDescription.level, This->glDescription.glFormat,
118                           This->glDescription.glType, mem);
119             checkGLcall("glGetTexImage()");
120         }
121         LEAVE_GL();
122
123         if (This->Flags & SFLAG_NONPOW2) {
124             LPBYTE src_data, dst_data;
125             int y;
126             /*
127              * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
128              * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
129              * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
130              *
131              * We're doing this...
132              *
133              * instead of boxing the texture :
134              * |<-texture width ->|  -->pow2width|   /\
135              * |111111111111111111|              |   |
136              * |222 Texture 222222| boxed empty  | texture height
137              * |3333 Data 33333333|              |   |
138              * |444444444444444444|              |   \/
139              * -----------------------------------   |
140              * |     boxed  empty | boxed empty  | pow2height
141              * |                  |              |   \/
142              * -----------------------------------
143              *
144              *
145              * we're repacking the data to the expected texture width
146              *
147              * |<-texture width ->|  -->pow2width|   /\
148              * |111111111111111111222222222222222|   |
149              * |222333333333333333333444444444444| texture height
150              * |444444                           |   |
151              * |                                 |   \/
152              * |                                 |   |
153              * |            empty                | pow2height
154              * |                                 |   \/
155              * -----------------------------------
156              *
157              * == is the same as
158              *
159              * |<-texture width ->|    /\
160              * |111111111111111111|
161              * |222222222222222222|texture height
162              * |333333333333333333|
163              * |444444444444444444|    \/
164              * --------------------
165              *
166              * this also means that any references to allocatedMemory should work with the data as if were a
167              * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
168              *
169              * internally the texture is still stored in a boxed format so any references to textureName will
170              * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
171              *
172              * Performance should not be an issue, because applications normally do not lock the surfaces when
173              * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
174              * and doesn't have to be re-read.
175              */
176             src_data = mem;
177             dst_data = This->resource.allocatedMemory;
178             TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
179             for (y = 1 ; y < This->currentDesc.Height; y++) {
180                 /* skip the first row */
181                 src_data += src_pitch;
182                 dst_data += dst_pitch;
183                 memcpy(dst_data, src_data, dst_pitch);
184             }
185
186             HeapFree(GetProcessHeap(), 0, mem);
187         }
188     }
189
190     /* Surface has now been downloaded */
191     This->Flags |= SFLAG_INSYSMEM;
192 }
193
194 static void surface_upload_data(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *data) {
195     if (This->resource.format == WINED3DFMT_DXT1 ||
196             This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
197             This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
198         if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
199             FIXME("Using DXT1/3/5 without advertized support\n");
200         } else {
201             /* glCompressedTexSubImage2D for uploading and glTexImage2D for allocating does not work well on some drivers(r200 dri, MacOS ATI driver)
202              * glCompressedTexImage2D does not accept NULL pointers. So for compressed textures surface_allocate_surface does nothing, and this
203              * function uses glCompressedTexImage2D instead of the SubImage call
204              */
205             TRACE("(%p) : Calling glCompressedTexSubImage2D w %d, h %d, data %p\n", This, width, height, data);
206             ENTER_GL();
207
208             if(This->Flags & SFLAG_PBO) {
209                 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
210                 checkGLcall("glBindBufferARB");
211                 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
212
213                 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
214                         width, height, 0 /* border */, This->resource.size, NULL));
215                 checkGLcall("glCompressedTexSubImage2D");
216
217                 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
218                 checkGLcall("glBindBufferARB");
219             } else {
220                 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
221                         width, height, 0 /* border */, This->resource.size, data));
222                 checkGLcall("glCompressedTexSubImage2D");
223             }
224             LEAVE_GL();
225         }
226     } else {
227         TRACE("(%p) : Calling glTexSubImage2D w %d,  h %d, data, %p\n", This, width, height, data);
228         ENTER_GL();
229
230         if(This->Flags & SFLAG_PBO) {
231             GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
232             checkGLcall("glBindBufferARB");
233             TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
234
235             glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, NULL);
236             checkGLcall("glTexSubImage2D");
237
238             GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
239             checkGLcall("glBindBufferARB");
240         }
241         else {
242             glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, data);
243             checkGLcall("glTexSubImage2D");
244         }
245
246         LEAVE_GL();
247     }
248 }
249
250 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type) {
251     BOOL enable_client_storage = FALSE;
252     BYTE *mem = NULL;
253
254     TRACE("(%p) : Creating surface (target %#x)  level %d, d3d format %s, internal format %#x, width %d, height %d, gl format %#x, gl type=%#x\n", This,
255             This->glDescription.target, This->glDescription.level, debug_d3dformat(This->resource.format), internal, width, height, format, type);
256
257     if (This->resource.format == WINED3DFMT_DXT1 ||
258             This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
259             This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
260         /* glCompressedTexImage2D does not accept NULL pointers, so we cannot allocate a compressed texture without uploading data */
261         TRACE("Not allocating compressed surfaces, surface_upload_data will specify them\n");
262
263         /* We have to point GL to the client storage memory here, because upload_data might use a PBO. This means a double upload
264          * once, unfortunately
265          */
266         if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
267             /* Neither NONPOW2, DIBSECTION nor OVERSIZE flags can be set on compressed textures */
268             This->Flags |= SFLAG_CLIENT;
269             mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
270             GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
271                        width, height, 0 /* border */, This->resource.size, mem));
272         }
273
274         return;
275     }
276
277     ENTER_GL();
278
279     if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
280         if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_OVERSIZE | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
281             /* In some cases we want to disable client storage.
282              * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
283              * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
284              * SFLAG_OVERSIZE: The gl texture is smaller than the allocated memory
285              * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
286              * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
287              */
288             glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
289             checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
290             This->Flags &= ~SFLAG_CLIENT;
291             enable_client_storage = TRUE;
292         } else {
293             This->Flags |= SFLAG_CLIENT;
294
295             /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
296              * it might point into a pbo. Instead use heapMemory, but get the alignment right.
297              */
298             mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
299         }
300     }
301     glTexImage2D(This->glDescription.target, This->glDescription.level, internal, width, height, 0, format, type, mem);
302     checkGLcall("glTexImage2D");
303
304     if(enable_client_storage) {
305         glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
306         checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
307     }
308     LEAVE_GL();
309
310     This->Flags |= SFLAG_ALLOCATED;
311 }
312
313 /* In D3D the depth stencil dimensions have to be greater than or equal to the
314  * render target dimensions. With FBOs, the dimensions have to be an exact match. */
315 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
316 void surface_set_compatible_renderbuffer(IWineD3DSurface *iface, unsigned int width, unsigned int height) {
317     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
318     renderbuffer_entry_t *entry;
319     GLuint renderbuffer = 0;
320     unsigned int src_width, src_height;
321
322     src_width = This->pow2Width;
323     src_height = This->pow2Height;
324
325     /* A depth stencil smaller than the render target is not valid */
326     if (width > src_width || height > src_height) return;
327
328     /* Remove any renderbuffer set if the sizes match */
329     if (width == src_width && height == src_height) {
330         This->current_renderbuffer = NULL;
331         return;
332     }
333
334     /* Look if we've already got a renderbuffer of the correct dimensions */
335     LIST_FOR_EACH_ENTRY(entry, &This->renderbuffers, renderbuffer_entry_t, entry) {
336         if (entry->width == width && entry->height == height) {
337             renderbuffer = entry->id;
338             This->current_renderbuffer = entry;
339             break;
340         }
341     }
342
343     if (!renderbuffer) {
344         const GlPixelFormatDesc *glDesc;
345         getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
346
347         GL_EXTCALL(glGenRenderbuffersEXT(1, &renderbuffer));
348         GL_EXTCALL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffer));
349         GL_EXTCALL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, glDesc->glFormat, width, height));
350
351         entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
352         entry->width = width;
353         entry->height = height;
354         entry->id = renderbuffer;
355         list_add_head(&This->renderbuffers, &entry->entry);
356
357         This->current_renderbuffer = entry;
358     }
359
360     checkGLcall("set_compatible_renderbuffer");
361 }
362
363 GLenum surface_get_gl_buffer(IWineD3DSurface *iface, IWineD3DSwapChain *swapchain) {
364     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
365     IWineD3DSwapChainImpl *swapchain_impl = (IWineD3DSwapChainImpl *)swapchain;
366
367     TRACE("(%p) : swapchain %p\n", This, swapchain);
368
369     if (swapchain_impl->backBuffer && swapchain_impl->backBuffer[0] == iface) {
370         TRACE("Returning GL_BACK\n");
371         return GL_BACK;
372     } else if (swapchain_impl->frontBuffer == iface) {
373         TRACE("Returning GL_FRONT\n");
374         return GL_FRONT;
375     }
376
377     FIXME("Higher back buffer, returning GL_BACK\n");
378     return GL_BACK;
379 }
380
381 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface) {
382     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
383     ULONG ref = InterlockedDecrement(&This->resource.ref);
384     TRACE("(%p) : Releasing from %d\n", This, ref + 1);
385     if (ref == 0) {
386         IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->resource.wineD3DDevice;
387         renderbuffer_entry_t *entry, *entry2;
388         TRACE("(%p) : cleaning up\n", This);
389
390         if (This->glDescription.textureName != 0) { /* release the openGL texture.. */
391
392             /* Need a context to destroy the texture. Use the currently active render target, but only if
393              * the primary render target exists. Otherwise lastActiveRenderTarget is garbage, see above.
394              * When destroying the primary rt, Uninit3D will activate a context before doing anything
395              */
396             if(device->render_targets && device->render_targets[0]) {
397                 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
398             }
399
400             TRACE("Deleting texture %d\n", This->glDescription.textureName);
401             ENTER_GL();
402             glDeleteTextures(1, &This->glDescription.textureName);
403             LEAVE_GL();
404         }
405
406         if(This->Flags & SFLAG_PBO) {
407             /* Delete the PBO */
408             GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
409         }
410
411         if(This->Flags & SFLAG_DIBSECTION) {
412             /* Release the DC */
413             SelectObject(This->hDC, This->dib.holdbitmap);
414             DeleteDC(This->hDC);
415             /* Release the DIB section */
416             DeleteObject(This->dib.DIBsection);
417             This->dib.bitmap_data = NULL;
418             This->resource.allocatedMemory = NULL;
419         }
420         if(This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem(iface, NULL);
421
422         HeapFree(GetProcessHeap(), 0, This->palette9);
423
424         IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
425         if(iface == device->ddraw_primary)
426             device->ddraw_primary = NULL;
427
428         LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
429             GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
430             HeapFree(GetProcessHeap(), 0, entry);
431         }
432
433         TRACE("(%p) Released\n", This);
434         HeapFree(GetProcessHeap(), 0, This);
435
436     }
437     return ref;
438 }
439
440 /* ****************************************************
441    IWineD3DSurface IWineD3DResource parts follow
442    **************************************************** */
443
444 void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
445     /* TODO: check for locks */
446     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
447     IWineD3DBaseTexture *baseTexture = NULL;
448     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
449
450     TRACE("(%p)Checking to see if the container is a base texture\n", This);
451     if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
452         TRACE("Passing to container\n");
453         IWineD3DBaseTexture_PreLoad(baseTexture);
454         IWineD3DBaseTexture_Release(baseTexture);
455     } else {
456     TRACE("(%p) : About to load surface\n", This);
457
458     if(!device->isInDraw) {
459         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
460     }
461
462     ENTER_GL();
463     glEnable(This->glDescription.target);/* make sure texture support is enabled in this context */
464     if (!This->glDescription.level) {
465         if (!This->glDescription.textureName) {
466             glGenTextures(1, &This->glDescription.textureName);
467             checkGLcall("glGenTextures");
468             TRACE("Surface %p given name %d\n", This, This->glDescription.textureName);
469         }
470         glBindTexture(This->glDescription.target, This->glDescription.textureName);
471         checkGLcall("glBindTexture");
472         IWineD3DSurface_LoadTexture(iface, FALSE);
473         /* This is where we should be reducing the amount of GLMemoryUsed */
474     } else if (This->glDescription.textureName) { /* NOTE: the level 0 surface of a mpmapped texture must be loaded first! */
475         /* assume this is a coding error not a real error for now */
476         FIXME("Mipmap surface has a glTexture bound to it!\n");
477     }
478     if (This->resource.pool == WINED3DPOOL_DEFAULT) {
479        /* Tell opengl to try and keep this texture in video ram (well mostly) */
480        GLclampf tmp;
481        tmp = 0.9f;
482         glPrioritizeTextures(1, &This->glDescription.textureName, &tmp);
483     }
484     LEAVE_GL();
485     }
486     return;
487 }
488
489 /* ******************************************************
490    IWineD3DSurface IWineD3DSurface parts follow
491    ****************************************************** */
492
493 void WINAPI IWineD3DSurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target) {
494     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
495     TRACE("(%p) : setting textureName %u, target %i\n", This, textureName, target);
496     if (This->glDescription.textureName == 0 && textureName != 0) {
497         IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
498         IWineD3DSurface_AddDirtyRect(iface, NULL);
499     }
500     This->glDescription.textureName = textureName;
501     This->glDescription.target      = target;
502     This->Flags &= ~SFLAG_ALLOCATED;
503 }
504
505 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription) {
506     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
507     TRACE("(%p) : returning %p\n", This, &This->glDescription);
508     *glDescription = &This->glDescription;
509 }
510
511 /* TODO: think about moving this down to resource? */
512 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface) {
513     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
514     /* This should only be called for sysmem textures, it may be a good idea to extend this to all pools at some point in the future  */
515     if (This->resource.pool != WINED3DPOOL_SYSTEMMEM) {
516         FIXME(" (%p)Attempting to get system memory for a non-system memory texture\n", iface);
517     }
518     return (CONST void*)(This->resource.allocatedMemory);
519 }
520
521 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, CONST RECT *rect, void *dest, UINT pitch) {
522     IWineD3DSwapChainImpl *swapchain;
523     IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
524     BYTE *mem;
525     GLint fmt;
526     GLint type;
527     BYTE *row, *top, *bottom;
528     int i;
529     BOOL bpp;
530     RECT local_rect;
531     BOOL srcIsUpsideDown;
532
533     if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
534         static BOOL warned = FALSE;
535         if(!warned) {
536             ERR("The application tries to lock the render target, but render target locking is disabled\n");
537             warned = TRUE;
538         }
539         return;
540     }
541
542     IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
543     /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
544      * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
545      * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
546      * context->last_was_blit set on the unlock.
547      */
548     ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
549     ENTER_GL();
550
551     /* Select the correct read buffer, and give some debug output.
552      * There is no need to keep track of the current read buffer or reset it, every part of the code
553      * that reads sets the read buffer as desired.
554      */
555     if(!swapchain) {
556         /* Locking the primary render target which is not on a swapchain(=offscreen render target).
557          * Read from the back buffer
558          */
559         TRACE("Locking offscreen render target\n");
560         glReadBuffer(myDevice->offscreenBuffer);
561         srcIsUpsideDown = TRUE;
562     } else {
563         GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
564         TRACE("Locking %#x buffer\n", buffer);
565         glReadBuffer(buffer);
566         checkGLcall("glReadBuffer");
567
568         IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
569         srcIsUpsideDown = FALSE;
570     }
571
572     /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
573     if(!rect) {
574         local_rect.left = 0;
575         local_rect.top = 0;
576         local_rect.right = This->currentDesc.Width;
577         local_rect.bottom = This->currentDesc.Height;
578     } else {
579         local_rect = *rect;
580     }
581     /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
582
583     switch(This->resource.format)
584     {
585         case WINED3DFMT_P8:
586         {
587             if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
588                 /* In case of P8 render targets the index is stored in the alpha component */
589                 fmt = GL_ALPHA;
590                 type = GL_UNSIGNED_BYTE;
591                 mem = dest;
592                 bpp = This->bytesPerPixel;
593             } else {
594                 /* GL can't return palettized data, so read ARGB pixels into a
595                  * separate block of memory and convert them into palettized format
596                  * in software. Slow, but if the app means to use palettized render
597                  * targets and locks it...
598                  *
599                  * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
600                  * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
601                  * for the color channels when palettizing the colors.
602                  */
603                 fmt = GL_RGB;
604                 type = GL_UNSIGNED_BYTE;
605                 pitch *= 3;
606                 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
607                 if(!mem) {
608                     ERR("Out of memory\n");
609                     LEAVE_GL();
610                     return;
611                 }
612                 bpp = This->bytesPerPixel * 3;
613             }
614         }
615         break;
616
617         default:
618             mem = dest;
619             fmt = This->glDescription.glFormat;
620             type = This->glDescription.glType;
621             bpp = This->bytesPerPixel;
622     }
623
624     if(This->Flags & SFLAG_PBO) {
625         GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
626         checkGLcall("glBindBufferARB");
627     }
628
629     glReadPixels(local_rect.left, local_rect.top,
630                  local_rect.right - local_rect.left,
631                  local_rect.bottom - local_rect.top,
632                  fmt, type, mem);
633     vcheckGLcall("glReadPixels");
634
635     if(This->Flags & SFLAG_PBO) {
636         GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
637         checkGLcall("glBindBufferARB");
638
639         /* Check if we need to flip the image. If we need to flip use glMapBufferARB
640          * to get a pointer to it and perform the flipping in software. This is a lot
641          * faster than calling glReadPixels for each line. In case we want more speed
642          * we should rerender it flipped in a FBO and read the data back from the FBO. */
643         if(!srcIsUpsideDown) {
644             GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
645             checkGLcall("glBindBufferARB");
646
647             mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
648             checkGLcall("glMapBufferARB");
649         }
650     }
651
652     /* TODO: Merge this with the palettization loop below for P8 targets */
653     if(!srcIsUpsideDown) {
654         UINT len, off;
655         /* glReadPixels returns the image upside down, and there is no way to prevent this.
656             Flip the lines in software */
657         len = (local_rect.right - local_rect.left) * bpp;
658         off = local_rect.left * bpp;
659
660         row = HeapAlloc(GetProcessHeap(), 0, len);
661         if(!row) {
662             ERR("Out of memory\n");
663             if(This->resource.format == WINED3DFMT_P8) HeapFree(GetProcessHeap(), 0, mem);
664             LEAVE_GL();
665             return;
666         }
667
668         top = mem + pitch * local_rect.top;
669         bottom = ((BYTE *) mem) + pitch * ( local_rect.bottom - local_rect.top - 1);
670         for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
671             memcpy(row, top + off, len);
672             memcpy(top + off, bottom + off, len);
673             memcpy(bottom + off, row, len);
674             top += pitch;
675             bottom -= pitch;
676         }
677         HeapFree(GetProcessHeap(), 0, row);
678
679         /* Unmap the temp PBO buffer */
680         if(This->Flags & SFLAG_PBO) {
681             GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
682             GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
683         }
684     }
685
686     /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
687      * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
688      * the same color but we have no choice.
689      * In case of render targets, the index is stored in the alpha component so no conversion is needed.
690      */
691     if((This->resource.format == WINED3DFMT_P8) && !(This->resource.usage & WINED3DUSAGE_RENDERTARGET)) {
692         PALETTEENTRY *pal;
693         DWORD width = pitch / 3;
694         int x, y, c;
695         if(This->palette) {
696             pal = This->palette->palents;
697         } else {
698             pal = This->resource.wineD3DDevice->palettes[This->resource.wineD3DDevice->currentPalette];
699         }
700
701         for(y = local_rect.top; y < local_rect.bottom; y++) {
702             for(x = local_rect.left; x < local_rect.right; x++) {
703                 /*                      start              lines            pixels      */
704                 BYTE *blue =  (BYTE *) ((BYTE *) mem) + y * pitch + x * (sizeof(BYTE) * 3);
705                 BYTE *green = blue  + 1;
706                 BYTE *red =   green + 1;
707
708                 for(c = 0; c < 256; c++) {
709                     if(*red   == pal[c].peRed   &&
710                        *green == pal[c].peGreen &&
711                        *blue  == pal[c].peBlue)
712                     {
713                         *((BYTE *) dest + y * width + x) = c;
714                         break;
715                     }
716                 }
717             }
718         }
719         HeapFree(GetProcessHeap(), 0, mem);
720     }
721     LEAVE_GL();
722 }
723
724 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This) {
725     /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
726      * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
727      * changed
728      */
729     if(!(This->Flags & SFLAG_DYNLOCK)) {
730         This->lockCount++;
731         /* MAXLOCKCOUNT is defined in wined3d_private.h */
732         if(This->lockCount > MAXLOCKCOUNT) {
733             TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
734             This->Flags |= SFLAG_DYNLOCK;
735         }
736     }
737
738     /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
739      * Also don't create a PBO for systemmem surfaces.
740      */
741     if(GL_SUPPORT(ARB_PIXEL_BUFFER_OBJECT) && (This->Flags & SFLAG_DYNLOCK) && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2)) && (This->resource.pool != WINED3DPOOL_SYSTEMMEM)) {
742         GLenum error;
743         ENTER_GL();
744
745         GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
746         error = glGetError();
747         if(This->pbo == 0 || error != GL_NO_ERROR) {
748             ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
749         }
750
751         TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
752
753         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
754         checkGLcall("glBindBufferARB");
755
756         GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
757         checkGLcall("glBufferDataARB");
758
759         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
760         checkGLcall("glBindBufferARB");
761
762         /* We don't need the system memory anymore and we can't even use it for PBOs */
763         if(!(This->Flags & SFLAG_CLIENT)) {
764             HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
765             This->resource.heapMemory = NULL;
766         }
767         This->resource.allocatedMemory = NULL;
768         This->Flags |= SFLAG_PBO;
769         LEAVE_GL();
770     } else if(!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO)) {
771         /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
772          * or a pbo to map
773          */
774         if(!This->resource.heapMemory) {
775             This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
776         }
777         This->resource.allocatedMemory =
778                 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
779         if(This->Flags & SFLAG_INSYSMEM) {
780             ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
781         }
782     }
783 }
784
785 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
786     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
787     IWineD3DDeviceImpl  *myDevice = This->resource.wineD3DDevice;
788     IWineD3DSwapChain *swapchain = NULL;
789
790     TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
791
792     /* This is also done in the base class, but we have to verify this before loading any data from
793      * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
794      * may interfere, and all other bad things may happen
795      */
796     if (This->Flags & SFLAG_LOCKED) {
797         WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
798         return WINED3DERR_INVALIDCALL;
799     }
800     This->Flags |= SFLAG_LOCKED;
801
802     if (!(This->Flags & SFLAG_LOCKABLE))
803     {
804         TRACE("Warning: trying to lock unlockable surf@%p\n", This);
805     }
806
807     if (Flags & WINED3DLOCK_DISCARD) {
808         /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
809         TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
810         This->Flags |= SFLAG_INSYSMEM;
811     }
812
813     if (This->Flags & SFLAG_INSYSMEM) {
814         TRACE("Local copy is up to date, not downloading data\n");
815         surface_prepare_system_memory(This); /* Makes sure memory is allocated */
816         goto lock_end;
817     }
818
819     /* Now download the surface content from opengl
820      * Use the render target readback if the surface is on a swapchain(=onscreen render target) or the current primary target
821      * Offscreen targets which are not active at the moment or are higher targets(fbos) can be locked with the texture path
822      */
823     IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
824     if(swapchain || iface == myDevice->render_targets[0]) {
825         const RECT *pass_rect = pRect;
826
827         /* IWineD3DSurface_LoadLocation does not check if the rectangle specifies the full surfaces
828          * because most caller functions do not need that. So do that here
829          */
830         if(pRect &&
831            pRect->top    == 0 &&
832            pRect->left   == 0 &&
833            pRect->right  == This->currentDesc.Width &&
834            pRect->bottom == This->currentDesc.Height) {
835             pass_rect = NULL;
836         }
837
838         switch(wined3d_settings.rendertargetlock_mode) {
839             case RTL_TEXDRAW:
840             case RTL_TEXTEX:
841                 FIXME("Reading from render target with a texture isn't implemented yet, falling back to framebuffer reading\n");
842 #if 0
843                 /* Disabled for now. LoadLocation prefers the texture over the drawable as the source. So if we copy to the
844                  * texture first, then to sysmem, we'll avoid glReadPixels and use glCopyTexImage and glGetTexImage2D instead.
845                  * This may be faster on some cards
846                  */
847                 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* No partial texture copy yet */);
848 #endif
849                 /* drop through */
850
851             case RTL_AUTO:
852             case RTL_READDRAW:
853             case RTL_READTEX:
854                 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, pRect);
855                 break;
856
857             case RTL_DISABLE:
858                 break;
859         }
860         if(swapchain) IWineD3DSwapChain_Release(swapchain);
861
862     } else if(iface == myDevice->stencilBufferTarget) {
863         /** the depth stencil in openGL has a format of GL_FLOAT
864          * which should be good for WINED3DFMT_D16_LOCKABLE
865          * and WINED3DFMT_D16
866          * it is unclear what format the stencil buffer is in except.
867          * 'Each index is converted to fixed point...
868          * If GL_MAP_STENCIL is GL_TRUE, indices are replaced by their
869          * mappings in the table GL_PIXEL_MAP_S_TO_S.
870          * glReadPixels(This->lockedRect.left,
871          *             This->lockedRect.bottom - j - 1,
872          *             This->lockedRect.right - This->lockedRect.left,
873          *             1,
874          *             GL_DEPTH_COMPONENT,
875          *             type,
876          *             (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
877          *
878          * Depth Stencil surfaces which are not the current depth stencil target should have their data in a
879          * gl texture(next path), or in local memory(early return because of set SFLAG_INSYSMEM above). If
880          * none of that is the case the problem is not in this function :-)
881          ********************************************/
882         FIXME("Depth stencil locking not supported yet\n");
883     } else {
884         /* This path is for normal surfaces, offscreen render targets and everything else that is in a gl texture */
885         TRACE("locking an ordinary surface\n");
886         IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
887     }
888
889 lock_end:
890     if(This->Flags & SFLAG_PBO) {
891         ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
892         ENTER_GL();
893         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
894         checkGLcall("glBindBufferARB");
895
896         /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
897         if(This->resource.allocatedMemory) {
898             ERR("The surface already has PBO memory allocated!\n");
899         }
900
901         This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
902         checkGLcall("glMapBufferARB");
903
904         /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
905         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
906         checkGLcall("glBindBufferARB");
907
908         LEAVE_GL();
909     }
910
911     if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
912         /* Don't dirtify */
913     } else {
914         IWineD3DBaseTexture *pBaseTexture;
915         /**
916          * Dirtify on lock
917          * as seen in msdn docs
918          */
919         IWineD3DSurface_AddDirtyRect(iface, pRect);
920
921         /** Dirtify Container if needed */
922         if (WINED3D_OK == IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture) && pBaseTexture != NULL) {
923             TRACE("Making container dirty\n");
924             IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
925             IWineD3DBaseTexture_Release(pBaseTexture);
926         } else {
927             TRACE("Surface is standalone, no need to dirty the container\n");
928         }
929     }
930
931     return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
932 }
933
934 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This) {
935     GLint  prev_store;
936     GLint  prev_rasterpos[4];
937     GLint skipBytes = 0;
938     BOOL storechanged = FALSE, memory_allocated = FALSE;
939     GLint fmt, type;
940     BYTE *mem;
941     UINT bpp;
942     UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);    /* target is argb, 4 byte */
943     IWineD3DDeviceImpl *myDevice = (IWineD3DDeviceImpl *) This->resource.wineD3DDevice;
944     IWineD3DSwapChainImpl *swapchain;
945
946     /* Activate the correct context for the render target */
947     ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
948     ENTER_GL();
949
950     IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
951     if(!swapchain) {
952         /* Primary offscreen render target */
953         TRACE("Offscreen render target\n");
954         glDrawBuffer(myDevice->offscreenBuffer);
955         checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
956     } else {
957         GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
958         TRACE("Unlocking %#x buffer\n", buffer);
959         glDrawBuffer(buffer);
960         checkGLcall("glDrawBuffer");
961
962         IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
963     }
964
965     glFlush();
966     vcheckGLcall("glFlush");
967     glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
968     vcheckGLcall("glIntegerv");
969     glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
970     vcheckGLcall("glIntegerv");
971     glPixelZoom(1.0, -1.0);
972     vcheckGLcall("glPixelZoom");
973
974     /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
975     glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
976     glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
977
978     glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
979     vcheckGLcall("glRasterPos2f");
980
981     /* Some drivers(radeon dri, others?) don't like exceptions during
982      * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
983      * after ReleaseDC. Reading it will cause an exception, which x11drv will
984      * catch to put the dib section in InSync mode, which leads to a crash
985      * and a blocked x server on my radeon card.
986      *
987      * The following lines read the dib section so it is put in inSync mode
988      * before glDrawPixels is called and the crash is prevented. There won't
989      * be any interfering gdi accesses, because UnlockRect is called from
990      * ReleaseDC, and the app won't use the dc any more afterwards.
991      */
992     if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
993         volatile BYTE read;
994         read = This->resource.allocatedMemory[0];
995     }
996
997     switch (This->resource.format) {
998         /* No special care needed */
999         case WINED3DFMT_A4R4G4B4:
1000         case WINED3DFMT_R5G6B5:
1001         case WINED3DFMT_A1R5G5B5:
1002         case WINED3DFMT_R8G8B8:
1003         case WINED3DFMT_X4R4G4B4:
1004         case WINED3DFMT_X1R5G5B5:
1005             type = This->glDescription.glType;
1006             fmt = This->glDescription.glFormat;
1007             mem = This->resource.allocatedMemory;
1008             bpp = This->bytesPerPixel;
1009             break;
1010
1011         /* In the past times we used to set the X channel of X8R8G8B8 and the above formats to
1012          * 1.0 because it happened to fix the intro movie in Pirates. However, this seems wrong.
1013          * If the game uses an X8R8G8B8 back buffer the GL alpha channel should not make any differences,
1014          * and the bug must be somewhere else. If we really have to set the alpha channel to 1.0 in
1015          * this case do it by clearing it after the draw instead of fixing it up in the CPU. Blending
1016          * is disabled via CTXUSAGE_BLIT context setup, so in the glDrawPixels call it does not
1017          * have any effects
1018          */
1019         case WINED3DFMT_X8R8G8B8:
1020         case WINED3DFMT_A8R8G8B8:
1021         {
1022             glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
1023             vcheckGLcall("glPixelStorei");
1024             storechanged = TRUE;
1025             type = This->glDescription.glType;
1026             fmt = This->glDescription.glFormat;
1027             mem = This->resource.allocatedMemory;
1028             bpp = This->bytesPerPixel;
1029         }
1030         break;
1031
1032         case WINED3DFMT_A2R10G10B10:
1033         {
1034             glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
1035             vcheckGLcall("glPixelStorei");
1036             storechanged = TRUE;
1037             type = This->glDescription.glType;
1038             fmt = This->glDescription.glFormat;
1039             mem = This->resource.allocatedMemory;
1040             bpp = This->bytesPerPixel;
1041         }
1042         break;
1043
1044         case WINED3DFMT_P8:
1045         {
1046             int height = This->glRect.bottom - This->glRect.top;
1047             type = GL_UNSIGNED_BYTE;
1048             fmt = GL_RGBA;
1049
1050             mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * sizeof(DWORD));
1051             if(!mem) {
1052                 ERR("Out of memory\n");
1053                 goto cleanup;
1054             }
1055             memory_allocated = TRUE;
1056             d3dfmt_convert_surface(This->resource.allocatedMemory,
1057                                    mem,
1058                                    pitch,
1059                                    pitch,
1060                                    height,
1061                                    pitch * 4,
1062                                    CONVERT_PALETTED,
1063                                    This);
1064             bpp = This->bytesPerPixel * 4;
1065             pitch *= 4;
1066         }
1067         break;
1068
1069         default:
1070             FIXME("Unsupported Format %u in locking func\n", This->resource.format);
1071
1072             /* Give it a try */
1073             type = This->glDescription.glType;
1074             fmt = This->glDescription.glFormat;
1075             mem = This->resource.allocatedMemory;
1076             bpp = This->bytesPerPixel;
1077     }
1078
1079     if(This->Flags & SFLAG_PBO) {
1080         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1081         checkGLcall("glBindBufferARB");
1082     }
1083
1084     glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1085                  (This->lockedRect.bottom - This->lockedRect.top)-1,
1086                  fmt, type,
1087                  mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1088     checkGLcall("glDrawPixels");
1089
1090 cleanup:
1091     if(This->Flags & SFLAG_PBO) {
1092         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1093         checkGLcall("glBindBufferARB");
1094     }
1095
1096     glPixelZoom(1.0,1.0);
1097     vcheckGLcall("glPixelZoom");
1098
1099     glRasterPos3iv(&prev_rasterpos[0]);
1100     vcheckGLcall("glRasterPos3iv");
1101
1102     /* Reset to previous pack row length */
1103     glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1104     vcheckGLcall("glPixelStorei GL_UNPACK_ROW_LENGTH");
1105     if(storechanged) {
1106         glPixelStorei(GL_PACK_SWAP_BYTES, prev_store);
1107         vcheckGLcall("glPixelStorei GL_PACK_SWAP_BYTES");
1108     }
1109
1110     if(memory_allocated) HeapFree(GetProcessHeap(), 0, mem);
1111
1112     if(!swapchain) {
1113         glDrawBuffer(myDevice->offscreenBuffer);
1114         checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1115     } else if(swapchain->backBuffer) {
1116         glDrawBuffer(GL_BACK);
1117         checkGLcall("glDrawBuffer(GL_BACK)");
1118     } else {
1119         glDrawBuffer(GL_FRONT);
1120         checkGLcall("glDrawBuffer(GL_FRONT)");
1121     }
1122     LEAVE_GL();
1123
1124     return;
1125 }
1126
1127 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1128     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1129     IWineD3DDeviceImpl  *myDevice = This->resource.wineD3DDevice;
1130     IWineD3DSwapChainImpl *swapchain = NULL;
1131     BOOL fullsurface;
1132
1133     if (!(This->Flags & SFLAG_LOCKED)) {
1134         WARN("trying to Unlock an unlocked surf@%p\n", This);
1135         return WINED3DERR_INVALIDCALL;
1136     }
1137
1138     if (This->Flags & SFLAG_PBO) {
1139         TRACE("Freeing PBO memory\n");
1140         ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1141         ENTER_GL();
1142         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1143         GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1144         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1145         checkGLcall("glUnmapBufferARB");
1146         LEAVE_GL();
1147         This->resource.allocatedMemory = NULL;
1148     }
1149
1150     TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1151
1152     if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1153         TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1154         goto unlock_end;
1155     }
1156
1157     IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1158     if(swapchain || (myDevice->render_targets && iface == myDevice->render_targets[0])) {
1159         if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1160             static BOOL warned = FALSE;
1161             if(!warned) {
1162                 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1163                 warned = TRUE;
1164             }
1165             if(swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
1166             goto unlock_end;
1167         }
1168
1169         if(This->dirtyRect.left   == 0 &&
1170            This->dirtyRect.top    == 0 &&
1171            This->dirtyRect.right  == This->currentDesc.Width &&
1172            This->dirtyRect.bottom == This->currentDesc.Height) {
1173             fullsurface = TRUE;
1174         } else {
1175             /* TODO: Proper partial rectangle tracking */
1176             fullsurface = FALSE;
1177             This->Flags |= SFLAG_INSYSMEM;
1178         }
1179
1180         switch(wined3d_settings.rendertargetlock_mode) {
1181             case RTL_READTEX:
1182             case RTL_TEXTEX:
1183                 ActivateContext(myDevice, iface, CTXUSAGE_BLIT);
1184                 ENTER_GL();
1185                 if (This->glDescription.textureName == 0) {
1186                     glGenTextures(1, &This->glDescription.textureName);
1187                     checkGLcall("glGenTextures");
1188                 }
1189                 glBindTexture(This->glDescription.target, This->glDescription.textureName);
1190                 checkGLcall("glBindTexture(This->glDescription.target, This->glDescription.textureName)");
1191                 LEAVE_GL();
1192                 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1193                 /* drop through */
1194
1195             case RTL_AUTO:
1196             case RTL_READDRAW:
1197             case RTL_TEXDRAW:
1198                 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1199                 break;
1200         }
1201
1202         if(!fullsurface) {
1203             /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1204              * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1205              * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1206              * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1207              * not fully up to date because only a subrectangle was read in LockRect.
1208              */
1209             This->Flags &= ~SFLAG_INSYSMEM;
1210             This->Flags |= SFLAG_INDRAWABLE;
1211         }
1212
1213         This->dirtyRect.left   = This->currentDesc.Width;
1214         This->dirtyRect.top    = This->currentDesc.Height;
1215         This->dirtyRect.right  = 0;
1216         This->dirtyRect.bottom = 0;
1217     } else if(iface == myDevice->stencilBufferTarget) {
1218         FIXME("Depth Stencil buffer locking is not implemented\n");
1219     } else {
1220         /* The rest should be a normal texture */
1221         IWineD3DBaseTextureImpl *impl;
1222         /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1223          * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1224          * states need resetting
1225          */
1226         if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
1227             if(impl->baseTexture.bindCount) {
1228                 IWineD3DDeviceImpl_MarkStateDirty(myDevice, STATE_SAMPLER(impl->baseTexture.sampler));
1229             }
1230             IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
1231         }
1232     }
1233
1234     unlock_end:
1235     This->Flags &= ~SFLAG_LOCKED;
1236     memset(&This->lockedRect, 0, sizeof(RECT));
1237     return WINED3D_OK;
1238 }
1239
1240 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
1241     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1242     WINED3DLOCKED_RECT lock;
1243     HRESULT hr;
1244     RGBQUAD col[256];
1245
1246     TRACE("(%p)->(%p)\n",This,pHDC);
1247
1248     if(This->Flags & SFLAG_USERPTR) {
1249         ERR("Not supported on surfaces with an application-provided surfaces\n");
1250         return WINEDDERR_NODC;
1251     }
1252
1253     /* Give more detailed info for ddraw */
1254     if (This->Flags & SFLAG_DCINUSE)
1255         return WINEDDERR_DCALREADYCREATED;
1256
1257     /* Can't GetDC if the surface is locked */
1258     if (This->Flags & SFLAG_LOCKED)
1259         return WINED3DERR_INVALIDCALL;
1260
1261     memset(&lock, 0, sizeof(lock)); /* To be sure */
1262
1263     /* Create a DIB section if there isn't a hdc yet */
1264     if(!This->hDC) {
1265         IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
1266         if(This->Flags & SFLAG_CLIENT) {
1267             IWineD3DSurface_PreLoad(iface);
1268         }
1269
1270         /* Use the dib section from now on if we are not using a PBO */
1271         if(!(This->Flags & SFLAG_PBO))
1272             This->resource.allocatedMemory = This->dib.bitmap_data;
1273     }
1274
1275     /* Lock the surface */
1276     hr = IWineD3DSurface_LockRect(iface,
1277                                   &lock,
1278                                   NULL,
1279                                   0);
1280
1281     if(This->Flags & SFLAG_PBO) {
1282         /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
1283         memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
1284     }
1285
1286     if(FAILED(hr)) {
1287         ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
1288         /* keep the dib section */
1289         return hr;
1290     }
1291
1292     if(This->resource.format == WINED3DFMT_P8 ||
1293         This->resource.format == WINED3DFMT_A8P8) {
1294         unsigned int n;
1295         if(This->palette) {
1296             PALETTEENTRY ent[256];
1297
1298             GetPaletteEntries(This->palette->hpal, 0, 256, ent);
1299             for (n=0; n<256; n++) {
1300                 col[n].rgbRed   = ent[n].peRed;
1301                 col[n].rgbGreen = ent[n].peGreen;
1302                 col[n].rgbBlue  = ent[n].peBlue;
1303                 col[n].rgbReserved = 0;
1304             }
1305         } else {
1306             IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1307
1308             for (n=0; n<256; n++) {
1309                 col[n].rgbRed   = device->palettes[device->currentPalette][n].peRed;
1310                 col[n].rgbGreen = device->palettes[device->currentPalette][n].peGreen;
1311                 col[n].rgbBlue  = device->palettes[device->currentPalette][n].peBlue;
1312                 col[n].rgbReserved = 0;
1313             }
1314
1315         }
1316         SetDIBColorTable(This->hDC, 0, 256, col);
1317     }
1318
1319     *pHDC = This->hDC;
1320     TRACE("returning %p\n",*pHDC);
1321     This->Flags |= SFLAG_DCINUSE;
1322
1323     return WINED3D_OK;
1324 }
1325
1326 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
1327     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1328
1329     TRACE("(%p)->(%p)\n",This,hDC);
1330
1331     if (!(This->Flags & SFLAG_DCINUSE))
1332         return WINED3DERR_INVALIDCALL;
1333
1334     if (This->hDC !=hDC) {
1335         WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
1336         return WINED3DERR_INVALIDCALL;
1337     }
1338
1339     if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
1340         /* Copy the contents of the DIB over to the PBO */
1341         memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
1342     }
1343
1344     /* we locked first, so unlock now */
1345     IWineD3DSurface_UnlockRect(iface);
1346
1347     This->Flags &= ~SFLAG_DCINUSE;
1348
1349     return WINED3D_OK;
1350 }
1351
1352 /* ******************************************************
1353    IWineD3DSurface Internal (No mapping to directx api) parts follow
1354    ****************************************************** */
1355
1356 HRESULT d3dfmt_get_conv(IWineD3DSurfaceImpl *This, BOOL need_alpha_ck, BOOL use_texturing, GLenum *format, GLenum *internal, GLenum *type, CONVERT_TYPES *convert, int *target_bpp, BOOL srgb_mode) {
1357     BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
1358     const GlPixelFormatDesc *glDesc;
1359     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1360     BOOL p8_render_target = FALSE;
1361     getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
1362
1363     /* Default values: From the surface */
1364     *format = glDesc->glFormat;
1365     *type = glDesc->glType;
1366     *convert = NO_CONVERSION;
1367     *target_bpp = This->bytesPerPixel;
1368
1369     if(srgb_mode) {
1370         *internal = glDesc->glGammaInternal;
1371     } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
1372         *internal = glDesc->rtInternal;
1373     } else {
1374         *internal = glDesc->glInternal;
1375     }
1376
1377     /* Ok, now look if we have to do any conversion */
1378     switch(This->resource.format) {
1379         case WINED3DFMT_P8:
1380             /* ****************
1381                 Paletted Texture
1382                 **************** */
1383
1384             if (device->render_targets && device->render_targets[0]) {
1385                 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
1386                 if((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET) && (render_target->resource.format == WINED3DFMT_P8))
1387                     p8_render_target = TRUE;
1388             }
1389
1390             /* Use conversion when the paletted texture extension is not available, or when it is available make sure it is used
1391              * for texturing as it won't work for calls like glDraw-/glReadPixels and further also use conversion in case of color keying.
1392              * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which conflicts with this.
1393              */
1394             if( !(GL_SUPPORT(EXT_PALETTED_TEXTURE) || (GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && p8_render_target))  || colorkey_active || (!use_texturing && GL_SUPPORT(EXT_PALETTED_TEXTURE)) ) {
1395                 *format = GL_RGBA;
1396                 *internal = GL_RGBA;
1397                 *type = GL_UNSIGNED_BYTE;
1398                 *target_bpp = 4;
1399                 if(colorkey_active) {
1400                     *convert = CONVERT_PALETTED_CK;
1401                 } else {
1402                     *convert = CONVERT_PALETTED;
1403                 }
1404             }
1405             else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1406                 *format = GL_RED;
1407                 *internal = GL_RGBA;
1408                 *type = GL_UNSIGNED_BYTE;
1409                 *target_bpp = 1;
1410             }
1411
1412             break;
1413
1414         case WINED3DFMT_R3G3B2:
1415             /* **********************
1416                 GL_UNSIGNED_BYTE_3_3_2
1417                 ********************** */
1418             if (colorkey_active) {
1419                 /* This texture format will never be used.. So do not care about color keying
1420                     up until the point in time it will be needed :-) */
1421                 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
1422             }
1423             break;
1424
1425         case WINED3DFMT_R5G6B5:
1426             if (colorkey_active) {
1427                 *convert = CONVERT_CK_565;
1428                 *format = GL_RGBA;
1429                 *internal = GL_RGBA;
1430                 *type = GL_UNSIGNED_SHORT_5_5_5_1;
1431             }
1432             break;
1433
1434         case WINED3DFMT_X1R5G5B5:
1435             if (colorkey_active) {
1436                 *convert = CONVERT_CK_5551;
1437                 *format = GL_BGRA;
1438                 *internal = GL_RGBA;
1439                 *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1440             }
1441             break;
1442
1443         case WINED3DFMT_R8G8B8:
1444             if (colorkey_active) {
1445                 *convert = CONVERT_CK_RGB24;
1446                 *format = GL_RGBA;
1447                 *internal = GL_RGBA;
1448                 *type = GL_UNSIGNED_INT_8_8_8_8;
1449                 *target_bpp = 4;
1450             }
1451             break;
1452
1453         case WINED3DFMT_X8R8G8B8:
1454             if (colorkey_active) {
1455                 *convert = CONVERT_RGB32_888;
1456                 *format = GL_RGBA;
1457                 *internal = GL_RGBA;
1458                 *type = GL_UNSIGNED_INT_8_8_8_8;
1459             }
1460             break;
1461
1462         case WINED3DFMT_V8U8:
1463             if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1464             else if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
1465                 *format = GL_DUDV_ATI;
1466                 *internal = GL_DU8DV8_ATI;
1467                 *type = GL_BYTE;
1468                 /* No conversion - Just change the gl type */
1469                 break;
1470             }
1471             *convert = CONVERT_V8U8;
1472             *format = GL_BGR;
1473             *internal = GL_RGB8;
1474             *type = GL_UNSIGNED_BYTE;
1475             *target_bpp = 3;
1476             break;
1477
1478         case WINED3DFMT_L6V5U5:
1479             *convert = CONVERT_L6V5U5;
1480             if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1481                 *target_bpp = 3;
1482                 /* Use format and types from table */
1483             } else {
1484                 /* Load it into unsigned R5G6B5, swap L and V channels, and revert that in the shader */
1485                 *target_bpp = 2;
1486                 *format = GL_RGB;
1487                 *internal = GL_RGB5;
1488                 *type = GL_UNSIGNED_SHORT_5_6_5;
1489             }
1490             break;
1491
1492         case WINED3DFMT_X8L8V8U8:
1493             *convert = CONVERT_X8L8V8U8;
1494             *target_bpp = 4;
1495             if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1496                 /* Use formats from gl table. It is a bit unfortunate, but the conversion
1497                  * is needed to set the X format to 255 to get 1.0 for alpha when sampling
1498                  * the texture. OpenGL can't use GL_DSDT8_MAG8_NV as internal format with
1499                  * the needed type and format parameter, so the internal format contains a
1500                  * 4th component, which is returned as alpha
1501                  */
1502             } else {
1503                 /* Not supported by GL_ATI_envmap_bumpmap */
1504                 *format = GL_BGRA;
1505                 *internal = GL_RGB8;
1506                 *type = GL_UNSIGNED_INT_8_8_8_8_REV;
1507             }
1508             break;
1509
1510         case WINED3DFMT_Q8W8V8U8:
1511             if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1512             *convert = CONVERT_Q8W8V8U8;
1513             *format = GL_BGRA;
1514             *internal = GL_RGBA8;
1515             *type = GL_UNSIGNED_BYTE;
1516             *target_bpp = 4;
1517             /* Not supported by GL_ATI_envmap_bumpmap */
1518             break;
1519
1520         case WINED3DFMT_V16U16:
1521             if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1522             *convert = CONVERT_V16U16;
1523             *format = GL_BGR;
1524             *internal = GL_RGB16_EXT;
1525             *type = GL_UNSIGNED_SHORT;
1526             *target_bpp = 6;
1527             /* What should I do here about GL_ATI_envmap_bumpmap?
1528              * Convert it or allow data loss by loading it into a 8 bit / channel texture?
1529              */
1530             break;
1531
1532         case WINED3DFMT_A4L4:
1533             /* A4L4 exists as an internal gl format, but for some reason there is not
1534              * format+type combination to load it. Thus convert it to A8L8, then load it
1535              * with A4L4 internal, but A8L8 format+type
1536              */
1537             *convert = CONVERT_A4L4;
1538             *format = GL_LUMINANCE_ALPHA;
1539             *internal = GL_LUMINANCE4_ALPHA4;
1540             *type = GL_UNSIGNED_BYTE;
1541             *target_bpp = 2;
1542             break;
1543
1544         case WINED3DFMT_R32F:
1545             /* Can be loaded in theory with fmt=GL_RED, type=GL_FLOAT, but this fails. The reason
1546              * is that D3D expects the undefined green, blue and alpha channels to return 1.0
1547              * when sampling, but OpenGL sets green and blue to 0.0 instead. Thus we have to inject
1548              * 1.0 instead.
1549              *
1550              * The alpha channel defaults to 1.0 in opengl, so nothing has to be done about it.
1551              */
1552             *convert = CONVERT_R32F;
1553             *format = GL_RGB;
1554             *internal = GL_RGB32F_ARB;
1555             *type = GL_FLOAT;
1556             *target_bpp = 12;
1557             break;
1558
1559         case WINED3DFMT_R16F:
1560             /* Similar to R32F */
1561             *convert = CONVERT_R16F;
1562             *format = GL_RGB;
1563             *internal = GL_RGB16F_ARB;
1564             *type = GL_HALF_FLOAT_ARB;
1565             *target_bpp = 6;
1566             break;
1567
1568         case WINED3DFMT_G16R16:
1569             *convert = CONVERT_G16R16;
1570             *format = GL_RGB;
1571             *internal = GL_RGB16_EXT;
1572             *type = GL_UNSIGNED_SHORT;
1573             *target_bpp = 6;
1574             break;
1575
1576         default:
1577             break;
1578     }
1579
1580     return WINED3D_OK;
1581 }
1582
1583 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This) {
1584     BYTE *source, *dest;
1585     TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
1586
1587     switch (convert) {
1588         case NO_CONVERSION:
1589         {
1590             memcpy(dst, src, pitch * height);
1591             break;
1592         }
1593         case CONVERT_PALETTED:
1594         case CONVERT_PALETTED_CK:
1595         {
1596             IWineD3DPaletteImpl* pal = This->palette;
1597             BYTE table[256][4];
1598             unsigned int x, y;
1599
1600             if( pal == NULL) {
1601                 /* TODO: If we are a sublevel, try to get the palette from level 0 */
1602             }
1603
1604             d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
1605
1606             for (y = 0; y < height; y++)
1607             {
1608                 source = src + pitch * y;
1609                 dest = dst + outpitch * y;
1610                 /* This is an 1 bpp format, using the width here is fine */
1611                 for (x = 0; x < width; x++) {
1612                     BYTE color = *source++;
1613                     *dest++ = table[color][0];
1614                     *dest++ = table[color][1];
1615                     *dest++ = table[color][2];
1616                     *dest++ = table[color][3];
1617                 }
1618             }
1619         }
1620         break;
1621
1622         case CONVERT_CK_565:
1623         {
1624             /* Converting the 565 format in 5551 packed to emulate color-keying.
1625
1626               Note : in all these conversion, it would be best to average the averaging
1627                       pixels to get the color of the pixel that will be color-keyed to
1628                       prevent 'color bleeding'. This will be done later on if ever it is
1629                       too visible.
1630
1631               Note2: Nvidia documents say that their driver does not support alpha + color keying
1632                      on the same surface and disables color keying in such a case
1633             */
1634             unsigned int x, y;
1635             WORD *Source;
1636             WORD *Dest;
1637
1638             TRACE("Color keyed 565\n");
1639
1640             for (y = 0; y < height; y++) {
1641                 Source = (WORD *) (src + y * pitch);
1642                 Dest = (WORD *) (dst + y * outpitch);
1643                 for (x = 0; x < width; x++ ) {
1644                     WORD color = *Source++;
1645                     *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1646                     if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1647                         (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1648                         *Dest |= 0x0001;
1649                     }
1650                     Dest++;
1651                 }
1652             }
1653         }
1654         break;
1655
1656         case CONVERT_CK_5551:
1657         {
1658             /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
1659             unsigned int x, y;
1660             WORD *Source;
1661             WORD *Dest;
1662             TRACE("Color keyed 5551\n");
1663             for (y = 0; y < height; y++) {
1664                 Source = (WORD *) (src + y * pitch);
1665                 Dest = (WORD *) (dst + y * outpitch);
1666                 for (x = 0; x < width; x++ ) {
1667                     WORD color = *Source++;
1668                     *Dest = color;
1669                     if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1670                         (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1671                         *Dest |= (1 << 15);
1672                     }
1673                     else {
1674                         *Dest &= ~(1 << 15);
1675                     }
1676                     Dest++;
1677                 }
1678             }
1679         }
1680         break;
1681
1682         case CONVERT_V8U8:
1683         {
1684             unsigned int x, y;
1685             short *Source;
1686             unsigned char *Dest;
1687             for(y = 0; y < height; y++) {
1688                 Source = (short *) (src + y * pitch);
1689                 Dest = (unsigned char *) (dst + y * outpitch);
1690                 for (x = 0; x < width; x++ ) {
1691                     long color = (*Source++);
1692                     /* B */ Dest[0] = 0xff;
1693                     /* G */ Dest[1] = (color >> 8) + 128; /* V */
1694                     /* R */ Dest[2] = (color) + 128;      /* U */
1695                     Dest += 3;
1696                 }
1697             }
1698             break;
1699         }
1700
1701         case CONVERT_V16U16:
1702         {
1703             unsigned int x, y;
1704             DWORD *Source;
1705             unsigned short *Dest;
1706             for(y = 0; y < height; y++) {
1707                 Source = (DWORD *) (src + y * pitch);
1708                 Dest = (unsigned short *) (dst + y * outpitch);
1709                 for (x = 0; x < width; x++ ) {
1710                     DWORD color = (*Source++);
1711                     /* B */ Dest[0] = 0xffff;
1712                     /* G */ Dest[1] = (color >> 16) + 32768; /* V */
1713                     /* R */ Dest[2] = (color      ) + 32768; /* U */
1714                     Dest += 3;
1715                 }
1716             }
1717             break;
1718         }
1719
1720         case CONVERT_Q8W8V8U8:
1721         {
1722             unsigned int x, y;
1723             DWORD *Source;
1724             unsigned char *Dest;
1725             for(y = 0; y < height; y++) {
1726                 Source = (DWORD *) (src + y * pitch);
1727                 Dest = (unsigned char *) (dst + y * outpitch);
1728                 for (x = 0; x < width; x++ ) {
1729                     long color = (*Source++);
1730                     /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1731                     /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1732                     /* R */ Dest[2] = (color         & 0xff) + 128; /* U */
1733                     /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
1734                     Dest += 4;
1735                 }
1736             }
1737             break;
1738         }
1739
1740         case CONVERT_L6V5U5:
1741         {
1742             unsigned int x, y;
1743             WORD *Source;
1744             unsigned char *Dest;
1745
1746             if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1747                 /* This makes the gl surface bigger(24 bit instead of 16), but it works with
1748                  * fixed function and shaders without further conversion once the surface is
1749                  * loaded
1750                  */
1751                 for(y = 0; y < height; y++) {
1752                     Source = (WORD *) (src + y * pitch);
1753                     Dest = (unsigned char *) (dst + y * outpitch);
1754                     for (x = 0; x < width; x++ ) {
1755                         short color = (*Source++);
1756                         unsigned char l = ((color >> 10) & 0xfc);
1757                                   char v = ((color >>  5) & 0x3e);
1758                                   char u = ((color      ) & 0x1f);
1759
1760                         /* 8 bits destination, 6 bits source, 8th bit is the sign. gl ignores the sign
1761                          * and doubles the positive range. Thus shift left only once, gl does the 2nd
1762                          * shift. GL reads a signed value and converts it into an unsigned value.
1763                          */
1764                         /* M */ Dest[2] = l << 1;
1765
1766                         /* Those are read as signed, but kept signed. Just left-shift 3 times to scale
1767                          * from 5 bit values to 8 bit values.
1768                          */
1769                         /* V */ Dest[1] = v << 3;
1770                         /* U */ Dest[0] = u << 3;
1771                         Dest += 3;
1772                     }
1773                 }
1774             } else {
1775                 for(y = 0; y < height; y++) {
1776                     unsigned short *Dest_s = (unsigned short *) (dst + y * outpitch);
1777                     Source = (WORD *) (src + y * pitch);
1778                     for (x = 0; x < width; x++ ) {
1779                         short color = (*Source++);
1780                         unsigned char l = ((color >> 10) & 0xfc);
1781                                  short v = ((color >>  5) & 0x3e);
1782                                  short u = ((color      ) & 0x1f);
1783                         short v_conv = v + 16;
1784                         short u_conv = u + 16;
1785
1786                         *Dest_s = ((v_conv << 11) & 0xf800) | ((l << 5) & 0x7e0) | (u_conv & 0x1f);
1787                         Dest_s += 1;
1788                     }
1789                 }
1790             }
1791             break;
1792         }
1793
1794         case CONVERT_X8L8V8U8:
1795         {
1796             unsigned int x, y;
1797             DWORD *Source;
1798             unsigned char *Dest;
1799
1800             if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1801                 /* This implementation works with the fixed function pipeline and shaders
1802                  * without further modification after converting the surface.
1803                  */
1804                 for(y = 0; y < height; y++) {
1805                     Source = (DWORD *) (src + y * pitch);
1806                     Dest = (unsigned char *) (dst + y * outpitch);
1807                     for (x = 0; x < width; x++ ) {
1808                         long color = (*Source++);
1809                         /* L */ Dest[2] = ((color >> 16) & 0xff);   /* L */
1810                         /* V */ Dest[1] = ((color >> 8 ) & 0xff);   /* V */
1811                         /* U */ Dest[0] = (color         & 0xff);   /* U */
1812                         /* I */ Dest[3] = 255;                      /* X */
1813                         Dest += 4;
1814                     }
1815                 }
1816             } else {
1817                 /* Doesn't work correctly with the fixed function pipeline, but can work in
1818                  * shaders if the shader is adjusted. (There's no use for this format in gl's
1819                  * standard fixed function pipeline anyway).
1820                  */
1821                 for(y = 0; y < height; y++) {
1822                     Source = (DWORD *) (src + y * pitch);
1823                     Dest = (unsigned char *) (dst + y * outpitch);
1824                     for (x = 0; x < width; x++ ) {
1825                         long color = (*Source++);
1826                         /* B */ Dest[0] = ((color >> 16) & 0xff);       /* L */
1827                         /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1828                         /* R */ Dest[2] = (color         & 0xff) + 128;  /* U */
1829                         Dest += 4;
1830                     }
1831                 }
1832             }
1833             break;
1834         }
1835
1836         case CONVERT_A4L4:
1837         {
1838             unsigned int x, y;
1839             unsigned char *Source;
1840             unsigned char *Dest;
1841             for(y = 0; y < height; y++) {
1842                 Source = (unsigned char *) (src + y * pitch);
1843                 Dest = (unsigned char *) (dst + y * outpitch);
1844                 for (x = 0; x < width; x++ ) {
1845                     unsigned char color = (*Source++);
1846                     /* A */ Dest[1] = (color & 0xf0) << 0;
1847                     /* L */ Dest[0] = (color & 0x0f) << 4;
1848                     Dest += 2;
1849                 }
1850             }
1851             break;
1852         }
1853
1854         case CONVERT_R32F:
1855         {
1856             unsigned int x, y;
1857             float *Source;
1858             float *Dest;
1859             for(y = 0; y < height; y++) {
1860                 Source = (float *) (src + y * pitch);
1861                 Dest = (float *) (dst + y * outpitch);
1862                 for (x = 0; x < width; x++ ) {
1863                     float color = (*Source++);
1864                     Dest[0] = color;
1865                     Dest[1] = 1.0;
1866                     Dest[2] = 1.0;
1867                     Dest += 3;
1868                 }
1869             }
1870             break;
1871         }
1872
1873         case CONVERT_R16F:
1874         {
1875             unsigned int x, y;
1876             WORD *Source;
1877             WORD *Dest;
1878             WORD one = 0x3c00;
1879             for(y = 0; y < height; y++) {
1880                 Source = (WORD *) (src + y * pitch);
1881                 Dest = (WORD *) (dst + y * outpitch);
1882                 for (x = 0; x < width; x++ ) {
1883                     WORD color = (*Source++);
1884                     Dest[0] = color;
1885                     Dest[1] = one;
1886                     Dest[2] = one;
1887                     Dest += 3;
1888                 }
1889             }
1890             break;
1891         }
1892
1893         case CONVERT_G16R16:
1894         {
1895             unsigned int x, y;
1896             WORD *Source;
1897             WORD *Dest;
1898
1899             for(y = 0; y < height; y++) {
1900                 Source = (WORD *) (src + y * pitch);
1901                 Dest = (WORD *) (dst + y * outpitch);
1902                 for (x = 0; x < width; x++ ) {
1903                     WORD green = (*Source++);
1904                     WORD red = (*Source++);
1905                     Dest[0] = green;
1906                     Dest[1] = red;
1907                     Dest[2] = 0xffff;
1908                     Dest += 3;
1909                 }
1910             }
1911             break;
1912         }
1913
1914         default:
1915             ERR("Unsupported conversation type %d\n", convert);
1916     }
1917     return WINED3D_OK;
1918 }
1919
1920 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey) {
1921     IWineD3DPaletteImpl* pal = This->palette;
1922     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1923     BOOL index_in_alpha = FALSE;
1924     int i;
1925
1926     /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
1927     * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
1928     * is slow. Further RGB->P8 conversion is not possible because palettes can have
1929     * duplicate entries. Store the color key in the unused alpha component to speed the
1930     * download up and to make conversion unneeded. */
1931     if (device->render_targets && device->render_targets[0]) {
1932         IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
1933
1934         if(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET)
1935             index_in_alpha = TRUE;
1936     }
1937
1938     if (pal == NULL) {
1939         /* Still no palette? Use the device's palette */
1940         /* Get the surface's palette */
1941         for (i = 0; i < 256; i++) {
1942             table[i][0] = device->palettes[device->currentPalette][i].peRed;
1943             table[i][1] = device->palettes[device->currentPalette][i].peGreen;
1944             table[i][2] = device->palettes[device->currentPalette][i].peBlue;
1945
1946             if(index_in_alpha) {
1947                 table[i][3] = i;
1948             } else if (colorkey &&
1949                 (i >= This->SrcBltCKey.dwColorSpaceLowValue) &&
1950                 (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
1951                 /* We should maybe here put a more 'neutral' color than the standard bright purple
1952                    one often used by application to prevent the nice purple borders when bi-linear
1953                    filtering is on */
1954                 table[i][3] = 0x00;
1955             } else {
1956                 table[i][3] = 0xFF;
1957             }
1958         }
1959     } else {
1960         TRACE("Using surface palette %p\n", pal);
1961         /* Get the surface's palette */
1962         for (i = 0; i < 256; i++) {
1963             table[i][0] = pal->palents[i].peRed;
1964             table[i][1] = pal->palents[i].peGreen;
1965             table[i][2] = pal->palents[i].peBlue;
1966
1967             if(index_in_alpha) {
1968                 table[i][3] = i;
1969             }
1970             else if (colorkey &&
1971                 (i >= This->SrcBltCKey.dwColorSpaceLowValue) &&
1972                 (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
1973                 /* We should maybe here put a more 'neutral' color than the standard bright purple
1974                    one often used by application to prevent the nice purple borders when bi-linear
1975                    filtering is on */
1976                 table[i][3] = 0x00;
1977             } else if(pal->Flags & WINEDDPCAPS_ALPHA) {
1978                 table[i][3] = pal->palents[i].peFlags;
1979             } else {
1980                 table[i][3] = 0xFF;
1981             }
1982         }
1983     }
1984 }
1985
1986 const char *fragment_palette_conversion =
1987     "!!ARBfp1.0\n"
1988     "TEMP index;\n"
1989     "PARAM constants = { 0.996, 0.00195, 0, 0 };\n" /* { 255/256, 0.5/255*255/256, 0, 0 } */
1990     "TEX index.x, fragment.texcoord[0], texture[0], 2D;\n" /* store the red-component of the current pixel */
1991     "MAD index.x, index.x, constants.x, constants.y;\n" /* Scale the index by 255/256 and add a bias of '0.5' in order to sample in the middle */
1992     "TEX result.color, index, texture[1], 1D;\n" /* use the red-component as a index in the palette to get the final color */
1993     "END";
1994
1995 /* This function is used in case of 8bit paletted textures to upload the palette.
1996    It supports GL_EXT_paletted_texture and GL_ARB_fragment_program, support for other
1997    extensions like ATI_fragment_shaders is possible.
1998 */
1999 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert) {
2000     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2001     BYTE table[256][4];
2002     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2003
2004     d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2005
2006     /* Try to use the paletted texture extension */
2007     if(GL_SUPPORT(EXT_PALETTED_TEXTURE))
2008     {
2009         TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
2010         GL_EXTCALL(glColorTableEXT(This->glDescription.target,GL_RGBA,256,GL_RGBA,GL_UNSIGNED_BYTE, table));
2011     }
2012     else
2013     {
2014         /* Let a fragment shader do the color conversion by uploading the palette to a 1D texture.
2015          * The 8bit pixel data will be used as an index in this palette texture to retrieve the final color. */
2016         TRACE("Using fragment shaders for emulating 8-bit paletted texture support\n");
2017
2018         /* Create the fragment program if we don't have it */
2019         if(!device->paletteConversionShader)
2020         {
2021             glEnable(GL_FRAGMENT_PROGRAM_ARB);
2022             GL_EXTCALL(glGenProgramsARB(1, &device->paletteConversionShader));
2023             GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2024             GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fragment_palette_conversion), (const GLbyte *)fragment_palette_conversion));
2025             glDisable(GL_FRAGMENT_PROGRAM_ARB);
2026         }
2027
2028         glEnable(GL_FRAGMENT_PROGRAM_ARB);
2029         GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2030
2031         GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1));
2032         glEnable(GL_TEXTURE_1D);
2033         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2034
2035         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2036         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /* Make sure we have discrete color levels. */
2037         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2038         glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, table); /* Upload the palette */
2039
2040         /* Switch back to unit 0 in which the 2D texture will be stored. */
2041         GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0));
2042
2043         /* Rebind the texture because it isn't bound anymore */
2044         glBindTexture(This->glDescription.target, This->glDescription.textureName);
2045     }
2046 }
2047
2048 static BOOL palette9_changed(IWineD3DSurfaceImpl *This) {
2049     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2050
2051     if(This->palette || (This->resource.format != WINED3DFMT_P8 && This->resource.format != WINED3DFMT_A8P8)) {
2052         /* If a ddraw-style palette is attached assume no d3d9 palette change.
2053          * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2054          */
2055         return FALSE;
2056     }
2057
2058     if(This->palette9) {
2059         if(memcmp(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256) == 0) {
2060             return FALSE;
2061         }
2062     } else {
2063         This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2064     }
2065     memcpy(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2066     return TRUE;
2067 }
2068
2069 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This) {
2070     GLboolean oldwrite[4];
2071
2072     /* Some formats have only some color channels, and the others are 1.0.
2073      * since our rendering renders to all channels, and those pixel formats
2074      * are emulated by using a full texture with the other channels set to 1.0
2075      * manually, clear the unused channels.
2076      *
2077      * This could be done with hacking colorwriteenable to mask the colors,
2078      * but before drawing the buffer would have to be cleared too, so there's
2079      * no gain in that
2080      */
2081     switch(This->resource.format) {
2082         case WINED3DFMT_R16F:
2083         case WINED3DFMT_R32F:
2084             TRACE("R16F or R32F format, clearing green, blue and alpha to 1.0\n");
2085             /* Do not activate a context, the correct drawable is active already
2086              * though just the read buffer is set, make sure to have the correct draw
2087              * buffer too
2088              */
2089             glDrawBuffer(This->resource.wineD3DDevice->offscreenBuffer);
2090             glDisable(GL_SCISSOR_TEST);
2091             glGetBooleanv(GL_COLOR_WRITEMASK, oldwrite);
2092             glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2093             glClearColor(0.0, 1.0, 1.0, 1.0);
2094             glClear(GL_COLOR_BUFFER_BIT);
2095             glColorMask(oldwrite[0], oldwrite[1], oldwrite[2], oldwrite[3]);
2096             if(!This->resource.wineD3DDevice->render_offscreen) glDrawBuffer(GL_BACK);
2097             checkGLcall("Unused channel clear\n");
2098             break;
2099
2100         default: break;
2101     }
2102 }
2103
2104 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2105     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2106
2107     if (!(This->Flags & SFLAG_INTEXTURE)) {
2108         TRACE("Reloading because surface is dirty\n");
2109     } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2110               ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2111               /* Reload: vice versa  OR */
2112               ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2113               /* Also reload: Color key is active AND the color key has changed */
2114               ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2115                 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2116                 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2117         TRACE("Reloading because of color keying\n");
2118         /* To perform the color key conversion we need a sysmem copy of
2119          * the surface. Make sure we have it
2120          */
2121         IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2122     } else if(palette9_changed(This)) {
2123         TRACE("Reloading surface because the d3d8/9 palette was changed\n");
2124         /* TODO: This is not necessarily needed with hw palettized texture support */
2125         IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2126     } else {
2127         TRACE("surface is already in texture\n");
2128         return WINED3D_OK;
2129     }
2130
2131     /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2132      *  These resources are not bound by device size or format restrictions. Because of this,
2133      *  these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2134      *  However, these resources can always be created, locked, and copied.
2135      */
2136     if (This->resource.pool == WINED3DPOOL_SCRATCH )
2137     {
2138         FIXME("(%p) Operation not supported for scratch textures\n",This);
2139         return WINED3DERR_INVALIDCALL;
2140     }
2141
2142     This->srgb = srgb_mode;
2143     IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* no partial locking for textures yet */);
2144
2145 #if 0
2146     {
2147         static unsigned int gen = 0;
2148         char buffer[4096];
2149         ++gen;
2150         if ((gen % 10) == 0) {
2151             snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
2152             IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
2153         }
2154         /*
2155          * debugging crash code
2156          if (gen == 250) {
2157          void** test = NULL;
2158          *test = 0;
2159          }
2160          */
2161     }
2162 #endif
2163
2164     if (!(This->Flags & SFLAG_DONOTFREE)) {
2165         HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2166         This->resource.allocatedMemory = NULL;
2167         This->resource.heapMemory = NULL;
2168         IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, FALSE);
2169     }
2170
2171     return WINED3D_OK;
2172 }
2173
2174 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface) {
2175     /* TODO: check for locks */
2176     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2177     IWineD3DBaseTexture *baseTexture = NULL;
2178     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2179
2180     TRACE("(%p)Checking to see if the container is a base texture\n", This);
2181     if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2182         TRACE("Passing to container\n");
2183         IWineD3DBaseTexture_BindTexture(baseTexture);
2184         IWineD3DBaseTexture_Release(baseTexture);
2185     } else {
2186         TRACE("(%p) : Binding surface\n", This);
2187
2188         if(!device->isInDraw) {
2189             ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
2190         }
2191         ENTER_GL();
2192         glBindTexture(This->glDescription.target, This->glDescription.textureName);
2193         LEAVE_GL();
2194     }
2195     return;
2196 }
2197
2198 #include <errno.h>
2199 #include <stdio.h>
2200 HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename) {
2201     FILE* f = NULL;
2202     UINT i, y;
2203     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2204     char *allocatedMemory;
2205     char *textureRow;
2206     IWineD3DSwapChain *swapChain = NULL;
2207     int width, height;
2208     GLuint tmpTexture = 0;
2209     DWORD color;
2210     /*FIXME:
2211     Textures may not be stored in ->allocatedgMemory and a GlTexture
2212     so we should lock the surface before saving a snapshot, or at least check that
2213     */
2214     /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
2215     by calling GetTexImage and in compressed form by calling
2216     GetCompressedTexImageARB.  Queried compressed images can be saved and
2217     later reused by calling CompressedTexImage[123]DARB.  Pre-compressed
2218     texture images do not need to be processed by the GL and should
2219     significantly improve texture loading performance relative to uncompressed
2220     images. */
2221
2222 /* Setup the width and height to be the internal texture width and height. */
2223     width  = This->pow2Width;
2224     height = This->pow2Height;
2225 /* check to see if we're a 'virtual' texture, e.g. we're not a pbuffer of texture, we're a back buffer*/
2226     IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapChain);
2227
2228     if (This->Flags & SFLAG_INDRAWABLE && !(This->Flags & SFLAG_INTEXTURE)) {
2229         /* if were not a real texture then read the back buffer into a real texture */
2230         /* we don't want to interfere with the back buffer so read the data into a temporary
2231          * texture and then save the data out of the temporary texture
2232          */
2233         GLint prevRead;
2234         ENTER_GL();
2235         TRACE("(%p) Reading render target into texture\n", This);
2236         glEnable(GL_TEXTURE_2D);
2237
2238         glGenTextures(1, &tmpTexture);
2239         glBindTexture(GL_TEXTURE_2D, tmpTexture);
2240
2241         glTexImage2D(GL_TEXTURE_2D,
2242                         0,
2243                         GL_RGBA,
2244                         width,
2245                         height,
2246                         0/*border*/,
2247                         GL_RGBA,
2248                         GL_UNSIGNED_INT_8_8_8_8_REV,
2249                         NULL);
2250
2251         glGetIntegerv(GL_READ_BUFFER, &prevRead);
2252         vcheckGLcall("glGetIntegerv");
2253         glReadBuffer(swapChain ? GL_BACK : This->resource.wineD3DDevice->offscreenBuffer);
2254         vcheckGLcall("glReadBuffer");
2255         glCopyTexImage2D(GL_TEXTURE_2D,
2256                             0,
2257                             GL_RGBA,
2258                             0,
2259                             0,
2260                             width,
2261                             height,
2262                             0);
2263
2264         checkGLcall("glCopyTexImage2D");
2265         glReadBuffer(prevRead);
2266         LEAVE_GL();
2267
2268     } else { /* bind the real texture, and make sure it up to date */
2269         IWineD3DSurface_PreLoad(iface);
2270     }
2271     allocatedMemory = HeapAlloc(GetProcessHeap(), 0, width  * height * 4);
2272     ENTER_GL();
2273     FIXME("Saving texture level %d width %d height %d\n", This->glDescription.level, width, height);
2274     glGetTexImage(GL_TEXTURE_2D,
2275                 This->glDescription.level,
2276                 GL_RGBA,
2277                 GL_UNSIGNED_INT_8_8_8_8_REV,
2278                 allocatedMemory);
2279     checkGLcall("glTexImage2D");
2280     if (tmpTexture) {
2281         glBindTexture(GL_TEXTURE_2D, 0);
2282         glDeleteTextures(1, &tmpTexture);
2283     }
2284     LEAVE_GL();
2285
2286     f = fopen(filename, "w+");
2287     if (NULL == f) {
2288         ERR("opening of %s failed with: %s\n", filename, strerror(errno));
2289         return WINED3DERR_INVALIDCALL;
2290     }
2291 /* Save the data out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha channel */
2292     TRACE("(%p) opened %s with format %s\n", This, filename, debug_d3dformat(This->resource.format));
2293 /* TGA header */
2294     fputc(0,f);
2295     fputc(0,f);
2296     fputc(2,f);
2297     fputc(0,f);
2298     fputc(0,f);
2299     fputc(0,f);
2300     fputc(0,f);
2301     fputc(0,f);
2302     fputc(0,f);
2303     fputc(0,f);
2304     fputc(0,f);
2305     fputc(0,f);
2306 /* short width*/
2307     fwrite(&width,2,1,f);
2308 /* short height */
2309     fwrite(&height,2,1,f);
2310 /* format rgba */
2311     fputc(0x20,f);
2312     fputc(0x28,f);
2313 /* raw data */
2314     /* if the data is upside down if we've fetched it from a back buffer, so it needs flipping again to make it the correct way up */
2315     if(swapChain)
2316         textureRow = allocatedMemory + (width * (height - 1) *4);
2317     else
2318         textureRow = allocatedMemory;
2319     for (y = 0 ; y < height; y++) {
2320         for (i = 0; i < width;  i++) {
2321             color = *((DWORD*)textureRow);
2322             fputc((color >> 16) & 0xFF, f); /* B */
2323             fputc((color >>  8) & 0xFF, f); /* G */
2324             fputc((color >>  0) & 0xFF, f); /* R */
2325             fputc((color >> 24) & 0xFF, f); /* A */
2326             textureRow += 4;
2327         }
2328         /* take two rows of the pointer to the texture memory */
2329         if(swapChain)
2330             (textureRow-= width << 3);
2331
2332     }
2333     TRACE("Closing file\n");
2334     fclose(f);
2335
2336     if(swapChain) {
2337         IWineD3DSwapChain_Release(swapChain);
2338     }
2339     HeapFree(GetProcessHeap(), 0, allocatedMemory);
2340     return WINED3D_OK;
2341 }
2342
2343 /**
2344  *   Slightly inefficient way to handle multiple dirty rects but it works :)
2345  */
2346 extern HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
2347     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2348     IWineD3DBaseTexture *baseTexture = NULL;
2349
2350     if (!(This->Flags & SFLAG_INSYSMEM) && (This->Flags & SFLAG_INTEXTURE))
2351         IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
2352
2353     IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2354     if (NULL != pDirtyRect) {
2355         This->dirtyRect.left   = min(This->dirtyRect.left,   pDirtyRect->left);
2356         This->dirtyRect.top    = min(This->dirtyRect.top,    pDirtyRect->top);
2357         This->dirtyRect.right  = max(This->dirtyRect.right,  pDirtyRect->right);
2358         This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
2359     } else {
2360         This->dirtyRect.left   = 0;
2361         This->dirtyRect.top    = 0;
2362         This->dirtyRect.right  = This->currentDesc.Width;
2363         This->dirtyRect.bottom = This->currentDesc.Height;
2364     }
2365     TRACE("(%p) : Dirty: yes, Rect:(%d,%d,%d,%d)\n", This, This->dirtyRect.left,
2366           This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
2367     /* if the container is a basetexture then mark it dirty. */
2368     if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2369         TRACE("Passing to container\n");
2370         IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
2371         IWineD3DBaseTexture_Release(baseTexture);
2372     }
2373     return WINED3D_OK;
2374 }
2375
2376 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2377     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2378     HRESULT hr;
2379     const GlPixelFormatDesc *glDesc;
2380     getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
2381
2382     TRACE("(%p) : Calling base function first\n", This);
2383     hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2384     if(SUCCEEDED(hr)) {
2385         /* Setup some glformat defaults */
2386         This->glDescription.glFormat         = glDesc->glFormat;
2387         This->glDescription.glFormatInternal = glDesc->glInternal;
2388         This->glDescription.glType           = glDesc->glType;
2389
2390         This->Flags &= ~SFLAG_ALLOCATED;
2391         TRACE("(%p) : glFormat %d, glFotmatInternal %d, glType %d\n", This,
2392               This->glDescription.glFormat, This->glDescription.glFormatInternal, This->glDescription.glType);
2393     }
2394     return hr;
2395 }
2396
2397 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2398     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2399
2400     if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2401         WARN("Surface is locked or the HDC is in use\n");
2402         return WINED3DERR_INVALIDCALL;
2403     }
2404
2405     if(Mem && Mem != This->resource.allocatedMemory) {
2406         void *release = NULL;
2407
2408         /* Do I have to copy the old surface content? */
2409         if(This->Flags & SFLAG_DIBSECTION) {
2410                 /* Release the DC. No need to hold the critical section for the update
2411                  * Thread because this thread runs only on front buffers, but this method
2412                  * fails for render targets in the check above.
2413                  */
2414                 SelectObject(This->hDC, This->dib.holdbitmap);
2415                 DeleteDC(This->hDC);
2416                 /* Release the DIB section */
2417                 DeleteObject(This->dib.DIBsection);
2418                 This->dib.bitmap_data = NULL;
2419                 This->resource.allocatedMemory = NULL;
2420                 This->hDC = NULL;
2421                 This->Flags &= ~SFLAG_DIBSECTION;
2422         } else if(!(This->Flags & SFLAG_USERPTR)) {
2423             release = This->resource.heapMemory;
2424             This->resource.heapMemory = NULL;
2425         }
2426         This->resource.allocatedMemory = Mem;
2427         This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2428
2429         /* Now the surface memory is most up do date. Invalidate drawable and texture */
2430         IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2431
2432         /* For client textures opengl has to be notified */
2433         if(This->Flags & SFLAG_CLIENT) {
2434             This->Flags &= ~SFLAG_ALLOCATED;
2435             IWineD3DSurface_PreLoad(iface);
2436             /* And hope that the app behaves correctly and did not free the old surface memory before setting a new pointer */
2437         }
2438
2439         /* Now free the old memory if any */
2440         HeapFree(GetProcessHeap(), 0, release);
2441     } else if(This->Flags & SFLAG_USERPTR) {
2442         /* Lockrect and GetDC will re-create the dib section and allocated memory */
2443         This->resource.allocatedMemory = NULL;
2444         /* HeapMemory should be NULL already */
2445         if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2446         This->Flags &= ~SFLAG_USERPTR;
2447
2448         if(This->Flags & SFLAG_CLIENT) {
2449             This->Flags &= ~SFLAG_ALLOCATED;
2450             /* This respecifies an empty texture and opengl knows that the old memory is gone */
2451             IWineD3DSurface_PreLoad(iface);
2452         }
2453     }
2454     return WINED3D_OK;
2455 }
2456
2457 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2458     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2459     IWineD3DSwapChainImpl *swapchain = NULL;
2460     HRESULT hr;
2461     TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2462
2463     /* Flipping is only supported on RenderTargets */
2464     if( !(This->resource.usage & WINED3DUSAGE_RENDERTARGET) ) return WINEDDERR_NOTFLIPPABLE;
2465
2466     if(override) {
2467         /* DDraw sets this for the X11 surfaces, so don't confuse the user 
2468          * FIXME("(%p) Target override is not supported by now\n", This);
2469          * Additionally, it isn't really possible to support triple-buffering
2470          * properly on opengl at all
2471          */
2472     }
2473
2474     IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **) &swapchain);
2475     if(!swapchain) {
2476         ERR("Flipped surface is not on a swapchain\n");
2477         return WINEDDERR_NOTFLIPPABLE;
2478     }
2479
2480     /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2481      * and only d3d8 and d3d9 apps specify the presentation interval
2482      */
2483     if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
2484         /* Most common case first to avoid wasting time on all the other cases */
2485         swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2486     } else if(Flags & WINEDDFLIP_NOVSYNC) {
2487         swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2488     } else if(Flags & WINEDDFLIP_INTERVAL2) {
2489         swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2490     } else if(Flags & WINEDDFLIP_INTERVAL3) {
2491         swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2492     } else {
2493         swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2494     }
2495
2496     /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2497     hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *) swapchain, NULL, NULL, 0, NULL, 0);
2498     IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
2499     return hr;
2500 }
2501
2502 /* Does a direct frame buffer -> texture copy. Stretching is done
2503  * with single pixel copy calls
2504  */
2505 static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2506     IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2507     float xrel, yrel;
2508     UINT row;
2509     IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2510
2511
2512     ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2513     ENTER_GL();
2514     IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2515
2516     /* TODO: Do we need GL_TEXTURE_2D enabled fpr copyteximage? */
2517     glEnable(This->glDescription.target);
2518     checkGLcall("glEnable(This->glDescription.target)");
2519
2520     /* Bind the target texture */
2521     glBindTexture(This->glDescription.target, This->glDescription.textureName);
2522     checkGLcall("glBindTexture");
2523     if(!swapchain) {
2524         glReadBuffer(myDevice->offscreenBuffer);
2525     } else {
2526         GLenum buffer = surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain);
2527         glReadBuffer(buffer);
2528     }
2529     checkGLcall("glReadBuffer");
2530
2531     xrel = (float) (srect->x2 - srect->x1) / (float) (drect->x2 - drect->x1);
2532     yrel = (float) (srect->y2 - srect->y1) / (float) (drect->y2 - drect->y1);
2533
2534     if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2535         FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2536
2537         if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2538             ERR("Texture filtering not supported in direct blit\n");
2539         }
2540     } else if((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) && ((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2541         ERR("Texture filtering not supported in direct blit\n");
2542     }
2543
2544     if(upsidedown &&
2545        !((xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) &&
2546        !((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2547         /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2548
2549         glCopyTexSubImage2D(This->glDescription.target,
2550                             This->glDescription.level,
2551                             drect->x1, drect->y1, /* xoffset, yoffset */
2552                             srect->x1, Src->currentDesc.Height - srect->y2,
2553                             drect->x2 - drect->x1, drect->y2 - drect->y1);
2554     } else {
2555         UINT yoffset = Src->currentDesc.Height - srect->y1 + drect->y1 - 1;
2556         /* I have to process this row by row to swap the image,
2557          * otherwise it would be upside down, so stretching in y direction
2558          * doesn't cost extra time
2559          *
2560          * However, stretching in x direction can be avoided if not necessary
2561          */
2562         for(row = drect->y1; row < drect->y2; row++) {
2563             if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2564                 /* Well, that stuff works, but it's very slow.
2565                  * find a better way instead
2566                  */
2567                 UINT col;
2568
2569                 for(col = drect->x1; col < drect->x2; col++) {
2570                     glCopyTexSubImage2D(This->glDescription.target,
2571                                         This->glDescription.level,
2572                                         drect->x1 + col, row, /* xoffset, yoffset */
2573                                         srect->x1 + col * xrel, yoffset - (int) (row * yrel),
2574                                         1, 1);
2575                 }
2576             } else {
2577                 glCopyTexSubImage2D(This->glDescription.target,
2578                                     This->glDescription.level,
2579                                     drect->x1, row, /* xoffset, yoffset */
2580                                     srect->x1, yoffset - (int) (row * yrel),
2581                                     drect->x2-drect->x1, 1);
2582             }
2583         }
2584     }
2585     vcheckGLcall("glCopyTexSubImage2D");
2586
2587     /* Leave the opengl state valid for blitting */
2588     glDisable(This->glDescription.target);
2589     checkGLcall("glDisable(This->glDescription.target)");
2590
2591     LEAVE_GL();
2592 }
2593
2594 /* Uses the hardware to stretch and flip the image */
2595 static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2596     GLuint src, backup = 0;
2597     IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2598     IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2599     float left, right, top, bottom; /* Texture coordinates */
2600     UINT fbwidth = Src->currentDesc.Width;
2601     UINT fbheight = Src->currentDesc.Height;
2602     GLenum drawBuffer = GL_BACK;
2603     GLenum texture_target;
2604
2605     TRACE("Using hwstretch blit\n");
2606     /* Activate the Proper context for reading from the source surface, set it up for blitting */
2607     ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2608     ENTER_GL();
2609
2610     IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2611
2612     /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2613      * This way we don't have to wait for the 2nd readback to finish to leave this function.
2614      */
2615     if(GL_LIMITS(aux_buffers) >= 2) {
2616         /* Got more than one aux buffer? Use the 2nd aux buffer */
2617         drawBuffer = GL_AUX1;
2618     } else if((swapchain || myDevice->offscreenBuffer == GL_BACK) && GL_LIMITS(aux_buffers) >= 1) {
2619         /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2620         drawBuffer = GL_AUX0;
2621     }
2622
2623     if(!swapchain && wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
2624         glGenTextures(1, &backup);
2625         checkGLcall("glGenTextures\n");
2626         glBindTexture(GL_TEXTURE_2D, backup);
2627         checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2628         texture_target = GL_TEXTURE_2D;
2629     } else {
2630         /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2631          * we are reading from the back buffer, the backup can be used as source texture
2632          */
2633         if(Src->glDescription.textureName == 0) {
2634             /* Get it a description */
2635             IWineD3DSurface_PreLoad(SrcSurface);
2636         }
2637         texture_target = Src->glDescription.target;
2638         glBindTexture(texture_target, Src->glDescription.textureName);
2639         checkGLcall("glBindTexture(texture_target, Src->glDescription.textureName)");
2640         glEnable(texture_target);
2641         checkGLcall("glEnable(texture_target)");
2642
2643         /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2644         Src->Flags &= ~SFLAG_INTEXTURE;
2645     }
2646
2647     glReadBuffer(GL_BACK);
2648     checkGLcall("glReadBuffer(GL_BACK)");
2649
2650     /* TODO: Only back up the part that will be overwritten */
2651     glCopyTexSubImage2D(texture_target, 0,
2652                         0, 0 /* read offsets */,
2653                         0, 0,
2654                         fbwidth,
2655                         fbheight);
2656
2657     checkGLcall("glCopyTexSubImage2D");
2658
2659     /* No issue with overriding these - the sampler is dirty due to blit usage */
2660     glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
2661                     stateLookup[WINELOOKUP_MAGFILTER][Filter - minLookup[WINELOOKUP_MAGFILTER]]);
2662     checkGLcall("glTexParameteri");
2663     glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
2664                     minMipLookup[Filter][WINED3DTEXF_NONE]);
2665     checkGLcall("glTexParameteri");
2666
2667     if(!swapchain || (IWineD3DSurface *) Src == swapchain->backBuffer[0]) {
2668         src = backup ? backup : Src->glDescription.textureName;
2669     } else {
2670         glReadBuffer(GL_FRONT);
2671         checkGLcall("glReadBuffer(GL_FRONT)");
2672
2673         glGenTextures(1, &src);
2674         checkGLcall("glGenTextures(1, &src)");
2675         glBindTexture(GL_TEXTURE_2D, src);
2676         checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
2677
2678         /* TODO: Only copy the part that will be read. Use srect->x1, srect->y2 as origin, but with the width watch
2679          * out for power of 2 sizes
2680          */
2681         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
2682                     GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2683         checkGLcall("glTexImage2D");
2684         glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
2685                             0, 0 /* read offsets */,
2686                             0, 0,
2687                             fbwidth,
2688                             fbheight);
2689
2690         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2691         checkGLcall("glTexParameteri");
2692         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2693         checkGLcall("glTexParameteri");
2694
2695         glReadBuffer(GL_BACK);
2696         checkGLcall("glReadBuffer(GL_BACK)");
2697
2698         if(texture_target != GL_TEXTURE_2D) {
2699             glDisable(texture_target);
2700             glEnable(GL_TEXTURE_2D);
2701             texture_target = GL_TEXTURE_2D;
2702         }
2703     }
2704     checkGLcall("glEnd and previous");
2705
2706     left = (float) srect->x1 / (float) Src->pow2Width;
2707     right = (float) srect->x2 / (float) Src->pow2Width;
2708
2709     if(upsidedown) {
2710         top = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2711         bottom = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2712     } else {
2713         top = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2714         bottom = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2715     }
2716
2717     /* draw the source texture stretched and upside down. The correct surface is bound already */
2718     glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
2719     glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
2720
2721     glDrawBuffer(drawBuffer);
2722     glReadBuffer(drawBuffer);
2723
2724     glBegin(GL_QUADS);
2725         /* bottom left */
2726         glTexCoord2f(left, bottom);
2727         glVertex2i(0, fbheight);
2728
2729         /* top left */
2730         glTexCoord2f(left, top);
2731         glVertex2i(0, fbheight - drect->y2 - drect->y1);
2732
2733         /* top right */
2734         glTexCoord2f(right, top);
2735         glVertex2i(drect->x2 - drect->x1, fbheight - drect->y2 - drect->y1);
2736
2737         /* bottom right */
2738         glTexCoord2f(right, bottom);
2739         glVertex2i(drect->x2 - drect->x1, fbheight);
2740     glEnd();
2741     checkGLcall("glEnd and previous");
2742
2743     if(texture_target != This->glDescription.target) {
2744         glDisable(texture_target);
2745         glEnable(This->glDescription.target);
2746         texture_target = This->glDescription.target;
2747     }
2748
2749     /* Now read the stretched and upside down image into the destination texture */
2750     glBindTexture(texture_target, This->glDescription.textureName);
2751     checkGLcall("glBindTexture");
2752     glCopyTexSubImage2D(texture_target,
2753                         0,
2754                         drect->x1, drect->y1, /* xoffset, yoffset */
2755                         0, 0, /* We blitted the image to the origin */
2756                         drect->x2 - drect->x1, drect->y2 - drect->y1);
2757     checkGLcall("glCopyTexSubImage2D");
2758
2759     if(drawBuffer == GL_BACK) {
2760         /* Write the back buffer backup back */
2761         if(backup) {
2762             if(texture_target != GL_TEXTURE_2D) {
2763                 glDisable(texture_target);
2764                 glEnable(GL_TEXTURE_2D);
2765                 texture_target = GL_TEXTURE_2D;
2766             }
2767             glBindTexture(GL_TEXTURE_2D, backup);
2768             checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2769         } else {
2770             if(texture_target != Src->glDescription.target) {
2771                 glDisable(texture_target);
2772                 glEnable(Src->glDescription.target);
2773                 texture_target = Src->glDescription.target;
2774             }
2775             glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
2776             checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2777         }
2778
2779         glBegin(GL_QUADS);
2780             /* top left */
2781             glTexCoord2f(0.0, (float) fbheight / (float) Src->pow2Height);
2782             glVertex2i(0, 0);
2783
2784             /* bottom left */
2785             glTexCoord2f(0.0, 0.0);
2786             glVertex2i(0, fbheight);
2787
2788             /* bottom right */
2789             glTexCoord2f((float) fbwidth / (float) Src->pow2Width, 0.0);
2790             glVertex2i(fbwidth, Src->currentDesc.Height);
2791
2792             /* top right */
2793             glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
2794             glVertex2i(fbwidth, 0);
2795         glEnd();
2796     } else {
2797         /* Restore the old draw buffer */
2798         glDrawBuffer(GL_BACK);
2799     }
2800     glDisable(texture_target);
2801     checkGLcall("glDisable(texture_target)");
2802
2803     /* Cleanup */
2804     if(src != Src->glDescription.textureName && src != backup) {
2805         glDeleteTextures(1, &src);
2806         checkGLcall("glDeleteTextures(1, &src)");
2807     }
2808     if(backup) {
2809         glDeleteTextures(1, &backup);
2810         checkGLcall("glDeleteTextures(1, &backup)");
2811     }
2812
2813     LEAVE_GL();
2814 }
2815
2816 /* Not called from the VTable */
2817 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
2818     WINED3DRECT rect;
2819     IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2820     IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
2821     IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2822
2823     TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
2824
2825     /* Get the swapchain. One of the surfaces has to be a primary surface */
2826     if(This->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2827         WARN("Destination is in sysmem, rejecting gl blt\n");
2828         return WINED3DERR_INVALIDCALL;
2829     }
2830     IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
2831     if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
2832     if(Src) {
2833         if(Src->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2834             WARN("Src is in sysmem, rejecting gl blt\n");
2835             return WINED3DERR_INVALIDCALL;
2836         }
2837         IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
2838         if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
2839     }
2840
2841     /* Early sort out of cases where no render target is used */
2842     if(!dstSwapchain && !srcSwapchain &&
2843         SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
2844         TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
2845         return WINED3DERR_INVALIDCALL;
2846     }
2847
2848     /* No destination color keying supported */
2849     if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
2850         /* Can we support that with glBlendFunc if blitting to the frame buffer? */
2851         TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
2852         return WINED3DERR_INVALIDCALL;
2853     }
2854
2855     if (DestRect) {
2856         rect.x1 = DestRect->left;
2857         rect.y1 = DestRect->top;
2858         rect.x2 = DestRect->right;
2859         rect.y2 = DestRect->bottom;
2860     } else {
2861         rect.x1 = 0;
2862         rect.y1 = 0;
2863         rect.x2 = This->currentDesc.Width;
2864         rect.y2 = This->currentDesc.Height;
2865     }
2866
2867     /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
2868     if(dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->backBuffer &&
2869        ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) && SrcSurface == dstSwapchain->backBuffer[0]) {
2870         /* Half-life does a Blt from the back buffer to the front buffer,
2871          * Full surface size, no flags... Use present instead
2872          *
2873          * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
2874          */
2875
2876         /* Check rects - IWineD3DDevice_Present doesn't handle them */
2877         while(1)
2878         {
2879             RECT mySrcRect;
2880             TRACE("Looking if a Present can be done...\n");
2881             /* Source Rectangle must be full surface */
2882             if( SrcRect ) {
2883                 if(SrcRect->left != 0 || SrcRect->top != 0 ||
2884                    SrcRect->right != Src->currentDesc.Width || SrcRect->bottom != Src->currentDesc.Height) {
2885                     TRACE("No, Source rectangle doesn't match\n");
2886                     break;
2887                 }
2888             }
2889             mySrcRect.left = 0;
2890             mySrcRect.top = 0;
2891             mySrcRect.right = Src->currentDesc.Width;
2892             mySrcRect.bottom = Src->currentDesc.Height;
2893
2894             /* No stretching may occur */
2895             if(mySrcRect.right != rect.x2 - rect.x1 ||
2896                mySrcRect.bottom != rect.y2 - rect.y1) {
2897                 TRACE("No, stretching is done\n");
2898                 break;
2899             }
2900
2901             /* Destination must be full surface or match the clipping rectangle */
2902             if(This->clipper && ((IWineD3DClipperImpl *) This->clipper)->hWnd)
2903             {
2904                 RECT cliprect;
2905                 POINT pos[2];
2906                 GetClientRect(((IWineD3DClipperImpl *) This->clipper)->hWnd, &cliprect);
2907                 pos[0].x = rect.x1;
2908                 pos[0].y = rect.y1;
2909                 pos[1].x = rect.x2;
2910                 pos[1].y = rect.y2;
2911                 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *) This->clipper)->hWnd,
2912                                 pos, 2);
2913
2914                 if(pos[0].x != cliprect.left  || pos[0].y != cliprect.top   ||
2915                    pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
2916                 {
2917                     TRACE("No, dest rectangle doesn't match(clipper)\n");
2918                     TRACE("Clip rect at (%d,%d)-(%d,%d)\n", cliprect.left, cliprect.top, cliprect.right, cliprect.bottom);
2919                     TRACE("Blt dest: (%d,%d)-(%d,%d)\n", rect.x1, rect.y1, rect.x2, rect.y2);
2920                     break;
2921                 }
2922             }
2923             else
2924             {
2925                 if(rect.x1 != 0 || rect.y1 != 0 ||
2926                    rect.x2 != This->currentDesc.Width || rect.y2 != This->currentDesc.Height) {
2927                     TRACE("No, dest rectangle doesn't match(surface size)\n");
2928                     break;
2929                 }
2930             }
2931
2932             TRACE("Yes\n");
2933
2934             /* These flags are unimportant for the flag check, remove them */
2935             if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
2936                 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
2937
2938                 /* The idea behind this is that a glReadPixels and a glDrawPixels call
2939                     * take very long, while a flip is fast.
2940                     * This applies to Half-Life, which does such Blts every time it finished
2941                     * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
2942                     * menu. This is also used by all apps when they do windowed rendering
2943                     *
2944                     * The problem is that flipping is not really the same as copying. After a
2945                     * Blt the front buffer is a copy of the back buffer, and the back buffer is
2946                     * untouched. Therefore it's necessary to override the swap effect
2947                     * and to set it back after the flip.
2948                     *
2949                     * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
2950                     * testcases.
2951                     */
2952
2953                 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
2954                 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2955
2956                 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
2957                 IWineD3DSwapChain_Present((IWineD3DSwapChain *) dstSwapchain, NULL, NULL, 0, NULL, 0);
2958
2959                 dstSwapchain->presentParms.SwapEffect = orig_swap;
2960
2961                 return WINED3D_OK;
2962             }
2963             break;
2964         }
2965
2966         TRACE("Unsupported blit between buffers on the same swapchain\n");
2967         return WINED3DERR_INVALIDCALL;
2968     } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
2969         FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
2970         return WINED3DERR_INVALIDCALL;
2971     } else if(dstSwapchain && srcSwapchain) {
2972         FIXME("Implement hardware blit between two different swapchains\n");
2973         return WINED3DERR_INVALIDCALL;
2974     } else if(dstSwapchain) {
2975         if(SrcSurface != myDevice->render_targets[0]) {
2976             ERR("Unexpected render target -> render target blit\n");
2977         }
2978         TRACE("Blit from active render target to a swapchain\n");
2979         /* Handled with regular texture -> swapchain blit */
2980     } else if(srcSwapchain) {
2981         if(This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
2982             ERR("Unexpected render target -> render target blit\n");
2983         }
2984         FIXME("Implement blit from a swapchain to the active render target\n");
2985         return WINED3DERR_INVALIDCALL;
2986     }
2987
2988     if((srcSwapchain || SrcSurface == myDevice->render_targets[0]) && !dstSwapchain) {
2989         /* Blit from render target to texture */
2990         WINED3DRECT srect;
2991         BOOL upsideDown, stretchx;
2992
2993         if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
2994             TRACE("Color keying not supported by frame buffer to texture blit\n");
2995             return WINED3DERR_INVALIDCALL;
2996             /* Destination color key is checked above */
2997         }
2998
2999         /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3000          * glCopyTexSubImage is a bit picky about the parameters we pass to it
3001          */
3002         if(SrcRect) {
3003             if(SrcRect->top < SrcRect->bottom) {
3004                 srect.y1 = SrcRect->top;
3005                 srect.y2 = SrcRect->bottom;
3006                 upsideDown = FALSE;
3007             } else {
3008                 srect.y1 = SrcRect->bottom;
3009                 srect.y2 = SrcRect->top;
3010                 upsideDown = TRUE;
3011             }
3012             srect.x1 = SrcRect->left;
3013             srect.x2 = SrcRect->right;
3014         } else {
3015             srect.x1 = 0;
3016             srect.y1 = 0;
3017             srect.x2 = Src->currentDesc.Width;
3018             srect.y2 = Src->currentDesc.Height;
3019             upsideDown = FALSE;
3020         }
3021         if(rect.x1 > rect.x2) {
3022             UINT tmp = rect.x2;
3023             rect.x2 = rect.x1;
3024             rect.x1 = tmp;
3025             upsideDown = !upsideDown;
3026         }
3027         if(!srcSwapchain) {
3028             TRACE("Reading from an offscreen target\n");
3029             upsideDown = !upsideDown;
3030         }
3031
3032         if(rect.x2 - rect.x1 != srect.x2 - srect.x1) {
3033             stretchx = TRUE;
3034         } else {
3035             stretchx = FALSE;
3036         }
3037
3038         /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3039          * flip the image nor scale it.
3040          *
3041          * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3042          * -> If the app wants a image width an unscaled width, copy it line per line
3043          * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3044          *    than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3045          *    back buffer. This is slower than reading line per line, thus not used for flipping
3046          * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3047          *    pixel by pixel
3048          *
3049          * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3050          * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3051          * backends.
3052          */
3053         if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT)) {
3054             stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &srect,
3055                     (IWineD3DSurface *)This, &rect, Filter, upsideDown);
3056         } else if((!stretchx) || rect.x2 - rect.x1 > Src->currentDesc.Width ||
3057                                     rect.y2 - rect.y1 > Src->currentDesc.Height) {
3058             TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3059             fb_copy_to_texture_direct(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3060         } else {
3061             TRACE("Using hardware stretching to flip / stretch the texture\n");
3062             fb_copy_to_texture_hwstretch(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3063         }
3064
3065         if(!(This->Flags & SFLAG_DONOTFREE)) {
3066             HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
3067             This->resource.allocatedMemory = NULL;
3068             This->resource.heapMemory = NULL;
3069         } else {
3070             This->Flags &= ~SFLAG_INSYSMEM;
3071         }
3072         /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3073          * path is never entered
3074          */
3075         IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3076
3077         return WINED3D_OK;
3078     } else if(Src) {
3079         /* Blit from offscreen surface to render target */
3080         float glTexCoord[4];
3081         DWORD oldCKeyFlags = Src->CKeyFlags;
3082         WINEDDCOLORKEY oldBltCKey = This->SrcBltCKey;
3083         RECT SourceRectangle;
3084
3085         TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
3086
3087         if(SrcRect) {
3088             SourceRectangle.left = SrcRect->left;
3089             SourceRectangle.right = SrcRect->right;
3090             SourceRectangle.top = SrcRect->top;
3091             SourceRectangle.bottom = SrcRect->bottom;
3092         } else {
3093             SourceRectangle.left = 0;
3094             SourceRectangle.right = Src->currentDesc.Width;
3095             SourceRectangle.top = 0;
3096             SourceRectangle.bottom = Src->currentDesc.Height;
3097         }
3098         if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT) &&
3099             (Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) == 0) {
3100             TRACE("Using stretch_rect_fbo\n");
3101             /* The source is always a texture, but never the currently active render target, and the texture
3102              * contents are never upside down
3103              */
3104             stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, (WINED3DRECT *) &SourceRectangle,
3105                               (IWineD3DSurface *)This, &rect, Filter, FALSE);
3106             return WINED3D_OK;
3107         }
3108
3109         if(!CalculateTexRect(Src, &SourceRectangle, glTexCoord)) {
3110             /* Fall back to software */
3111             WARN("(%p) Source texture area (%d,%d)-(%d,%d) is too big\n", Src,
3112                     SourceRectangle.left, SourceRectangle.top,
3113                     SourceRectangle.right, SourceRectangle.bottom);
3114             return WINED3DERR_INVALIDCALL;
3115         }
3116
3117         /* Color keying: Check if we have to do a color keyed blt,
3118          * and if not check if a color key is activated.
3119          *
3120          * Just modify the color keying parameters in the surface and restore them afterwards
3121          * The surface keeps track of the color key last used to load the opengl surface.
3122          * PreLoad will catch the change to the flags and color key and reload if necessary.
3123          */
3124         if(Flags & WINEDDBLT_KEYSRC) {
3125             /* Use color key from surface */
3126         } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3127             /* Use color key from DDBltFx */
3128             Src->CKeyFlags |= WINEDDSD_CKSRCBLT;
3129             This->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3130         } else {
3131             /* Do not use color key */
3132             Src->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3133         }
3134
3135         /* Now load the surface */
3136         IWineD3DSurface_PreLoad((IWineD3DSurface *) Src);
3137
3138
3139         /* Activate the destination context, set it up for blitting */
3140         ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
3141         ENTER_GL();
3142
3143         glEnable(Src->glDescription.target);
3144         checkGLcall("glEnable(Src->glDescription.target)");
3145
3146         if(!dstSwapchain) {
3147             TRACE("Drawing to offscreen buffer\n");
3148             glDrawBuffer(myDevice->offscreenBuffer);
3149             checkGLcall("glDrawBuffer");
3150         } else {
3151             GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *)This, (IWineD3DSwapChain *)dstSwapchain);
3152             TRACE("Drawing to %#x buffer\n", buffer);
3153             glDrawBuffer(buffer);
3154             checkGLcall("glDrawBuffer");
3155         }
3156
3157         /* Bind the texture */
3158         glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
3159         checkGLcall("glBindTexture");
3160
3161         /* Filtering for StretchRect */
3162         glTexParameteri(Src->glDescription.target, GL_TEXTURE_MAG_FILTER,
3163                         stateLookup[WINELOOKUP_MAGFILTER][Filter - minLookup[WINELOOKUP_MAGFILTER]]);
3164         checkGLcall("glTexParameteri");
3165         glTexParameteri(Src->glDescription.target, GL_TEXTURE_MIN_FILTER,
3166                         minMipLookup[Filter][WINED3DTEXF_NONE]);
3167         checkGLcall("glTexParameteri");
3168         glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3169         glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3170         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3171         checkGLcall("glTexEnvi");
3172
3173         /* This is for color keying */
3174         if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3175             glEnable(GL_ALPHA_TEST);
3176             checkGLcall("glEnable GL_ALPHA_TEST");
3177             glAlphaFunc(GL_NOTEQUAL, 0.0);
3178             checkGLcall("glAlphaFunc\n");
3179         } else {
3180             glDisable(GL_ALPHA_TEST);
3181             checkGLcall("glDisable GL_ALPHA_TEST");
3182         }
3183
3184         /* Draw a textured quad
3185          */
3186         glBegin(GL_QUADS);
3187
3188         glColor3d(1.0f, 1.0f, 1.0f);
3189         glTexCoord2f(glTexCoord[0], glTexCoord[2]);
3190         glVertex3f(rect.x1,
3191                     rect.y1,
3192                     0.0);
3193
3194         glTexCoord2f(glTexCoord[0], glTexCoord[3]);
3195         glVertex3f(rect.x1, rect.y2, 0.0);
3196
3197         glTexCoord2f(glTexCoord[1], glTexCoord[3]);
3198         glVertex3f(rect.x2,
3199                     rect.y2,
3200                     0.0);
3201
3202         glTexCoord2f(glTexCoord[1], glTexCoord[2]);
3203         glVertex3f(rect.x2,
3204                     rect.y1,
3205                     0.0);
3206         glEnd();
3207         checkGLcall("glEnd");
3208
3209         if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3210             glDisable(GL_ALPHA_TEST);
3211             checkGLcall("glDisable(GL_ALPHA_TEST)");
3212         }
3213
3214         /* Flush in case the drawable is used by multiple GL contexts */
3215         if(dstSwapchain && (dstSwapchain->num_contexts >= 2))
3216             glFlush();
3217
3218         glBindTexture(Src->glDescription.target, 0);
3219         checkGLcall("glBindTexture(Src->glDescription.target, 0)");
3220         /* Leave the opengl state valid for blitting */
3221         glDisable(Src->glDescription.target);
3222         checkGLcall("glDisable(Src->glDescription.target)");
3223
3224         /* The draw buffer should only need to be restored if we were drawing to the front buffer, and there is a back buffer.
3225          * otherwise the context manager should choose between GL_BACK / offscreenDrawBuffer
3226          */
3227         if(dstSwapchain && This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer && dstSwapchain->backBuffer) {
3228             glDrawBuffer(GL_BACK);
3229             checkGLcall("glDrawBuffer");
3230         }
3231         /* Restore the color key parameters */
3232         Src->CKeyFlags = oldCKeyFlags;
3233         This->SrcBltCKey = oldBltCKey;
3234
3235         LEAVE_GL();
3236
3237         /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3238         /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3239          * is outdated now
3240          */
3241         IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INDRAWABLE, TRUE);
3242         /* TODO: This should be moved to ModifyLocation() */
3243         if(!(dstSwapchain || wined3d_settings.offscreen_rendering_mode != ORM_FBO)) {
3244             This->Flags |= SFLAG_INTEXTURE;
3245         }
3246
3247         return WINED3D_OK;
3248     } else {
3249         /* Source-Less Blit to render target */
3250         if (Flags & WINEDDBLT_COLORFILL) {
3251             /* This is easy to handle for the D3D Device... */
3252             DWORD color;
3253
3254             TRACE("Colorfill\n");
3255
3256             /* This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0] || dstSwapchain
3257                 must be true if we are here */
3258             if (This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0] &&
3259                     !(This == (IWineD3DSurfaceImpl*) dstSwapchain->frontBuffer ||
3260                       (dstSwapchain->backBuffer && This == (IWineD3DSurfaceImpl*) dstSwapchain->backBuffer[0]))) {
3261                 TRACE("Surface is higher back buffer, falling back to software\n");
3262                 return WINED3DERR_INVALIDCALL;
3263             }
3264
3265             /* The color as given in the Blt function is in the format of the frame-buffer...
3266              * 'clear' expect it in ARGB format => we need to do some conversion :-)
3267              */
3268             if (This->resource.format == WINED3DFMT_P8) {
3269                 if (This->palette) {
3270                     color = ((0xFF000000) |
3271                             (This->palette->palents[DDBltFx->u5.dwFillColor].peRed << 16) |
3272                             (This->palette->palents[DDBltFx->u5.dwFillColor].peGreen << 8) |
3273                             (This->palette->palents[DDBltFx->u5.dwFillColor].peBlue));
3274                 } else {
3275                     color = 0xFF000000;
3276                 }
3277             }
3278             else if (This->resource.format == WINED3DFMT_R5G6B5) {
3279                 if (DDBltFx->u5.dwFillColor == 0xFFFF) {
3280                     color = 0xFFFFFFFF;
3281                 } else {
3282                     color = ((0xFF000000) |
3283                             ((DDBltFx->u5.dwFillColor & 0xF800) << 8) |
3284                             ((DDBltFx->u5.dwFillColor & 0x07E0) << 5) |
3285                             ((DDBltFx->u5.dwFillColor & 0x001F) << 3));
3286                 }
3287             }
3288             else if ((This->resource.format == WINED3DFMT_R8G8B8) ||
3289                     (This->resource.format == WINED3DFMT_X8R8G8B8) ) {
3290                 color = 0xFF000000 | DDBltFx->u5.dwFillColor;
3291             }
3292             else if (This->resource.format == WINED3DFMT_A8R8G8B8) {
3293                 color = DDBltFx->u5.dwFillColor;
3294             }
3295             else {
3296                 ERR("Wrong surface type for BLT override(Format doesn't match) !\n");
3297                 return WINED3DERR_INVALIDCALL;
3298             }
3299
3300             TRACE("(%p) executing Render Target override, color = %x\n", This, color);
3301             IWineD3DDeviceImpl_ClearSurface(myDevice, This,
3302                                             1, /* Number of rectangles */
3303                                             &rect, WINED3DCLEAR_TARGET, color,
3304                                             0.0 /* Z */,
3305                                             0 /* Stencil */);
3306             return WINED3D_OK;
3307         }
3308     }
3309
3310     /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3311     TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3312     return WINED3DERR_INVALIDCALL;
3313 }
3314
3315 static HRESULT WINAPI IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx)
3316 {
3317     IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3318     float depth;
3319
3320     if (Flags & WINEDDBLT_DEPTHFILL) {
3321         switch(This->resource.format) {
3322             case WINED3DFMT_D16:
3323                 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3324                 break;
3325             case WINED3DFMT_D15S1:
3326                 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3327                 break;
3328             case WINED3DFMT_D24S8:
3329             case WINED3DFMT_D24X8:
3330                 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3331                 break;
3332             case WINED3DFMT_D32:
3333                 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3334                 break;
3335             default:
3336                 depth = 0.0;
3337                 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format));
3338         }
3339
3340         return IWineD3DDevice_Clear((IWineD3DDevice *) myDevice,
3341                                     DestRect == NULL ? 0 : 1,
3342                                     (WINED3DRECT *) DestRect,
3343                                     WINED3DCLEAR_ZBUFFER,
3344                                     0x00000000,
3345                                     depth,
3346                                     0x00000000);
3347     }
3348
3349     FIXME("(%p): Unsupp depthstencil blit\n", This);
3350     return WINED3DERR_INVALIDCALL;
3351 }
3352
3353 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3354     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3355     IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3356     IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3357     TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3358     TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3359
3360     /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3361      * except depth blits, which seem to work
3362      */
3363     if(iface == myDevice->stencilBufferTarget || (SrcSurface && SrcSurface == myDevice->stencilBufferTarget)) {
3364         if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
3365             TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3366             return WINED3DERR_INVALIDCALL;
3367         } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3368             TRACE("Z Blit override handled the blit\n");
3369             return WINED3D_OK;
3370         }
3371     }
3372
3373     /* Special cases for RenderTargets */
3374     if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3375         ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3376         if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
3377     }
3378
3379     /* For the rest call the X11 surface implementation.
3380      * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3381      * other Blts are rather rare
3382      */
3383     return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3384 }
3385
3386 HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans) {
3387     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3388     IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3389     IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3390     TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3391
3392     if(myDevice->inScene &&
3393        (iface == myDevice->stencilBufferTarget ||
3394        (Source && Source == myDevice->stencilBufferTarget))) {
3395         TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3396         return WINED3DERR_INVALIDCALL;
3397     }
3398
3399     /* Special cases for RenderTargets */
3400     if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3401         ( srcImpl && (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3402
3403         RECT SrcRect, DstRect;
3404         DWORD Flags=0;
3405
3406         if(rsrc) {
3407             SrcRect.left = rsrc->left;
3408             SrcRect.top= rsrc->top;
3409             SrcRect.bottom = rsrc->bottom;
3410             SrcRect.right = rsrc->right;
3411         } else {
3412             SrcRect.left = 0;
3413             SrcRect.top = 0;
3414             SrcRect.right = srcImpl->currentDesc.Width;
3415             SrcRect.bottom = srcImpl->currentDesc.Height;
3416         }
3417
3418         DstRect.left = dstx;
3419         DstRect.top=dsty;
3420         DstRect.right = dstx + SrcRect.right - SrcRect.left;
3421         DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3422
3423         /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3424         if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3425             Flags |= WINEDDBLT_KEYSRC;
3426         if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3427             Flags |= WINEDDBLT_KEYDEST;
3428         if(trans & WINEDDBLTFAST_WAIT)
3429             Flags |= WINEDDBLT_WAIT;
3430         if(trans & WINEDDBLTFAST_DONOTWAIT)
3431             Flags |= WINEDDBLT_DONOTWAIT;
3432
3433         if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
3434     }
3435
3436
3437     return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3438 }
3439
3440 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3441     /** Check against the maximum texture sizes supported by the video card **/
3442     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3443     unsigned int pow2Width, pow2Height;
3444     const GlPixelFormatDesc *glDesc;
3445
3446     getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
3447     /* Setup some glformat defaults */
3448     This->glDescription.glFormat         = glDesc->glFormat;
3449     This->glDescription.glFormatInternal = glDesc->glInternal;
3450     This->glDescription.glType           = glDesc->glType;
3451
3452     This->glDescription.textureName      = 0;
3453     This->glDescription.target           = GL_TEXTURE_2D;
3454
3455     /* Non-power2 support */
3456     if (GL_SUPPORT(ARB_TEXTURE_NON_POWER_OF_TWO)) {
3457         pow2Width = This->currentDesc.Width;
3458         pow2Height = This->currentDesc.Height;
3459     } else {
3460         /* Find the nearest pow2 match */
3461         pow2Width = pow2Height = 1;
3462         while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3463         while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3464     }
3465     This->pow2Width  = pow2Width;
3466     This->pow2Height = pow2Height;
3467
3468     if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
3469         WINED3DFORMAT Format = This->resource.format;
3470         /** TODO: add support for non power two compressed textures **/
3471         if (Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3
3472             || Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5) {
3473             FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3474                   This, This->currentDesc.Width, This->currentDesc.Height);
3475             return WINED3DERR_NOTAVAILABLE;
3476         }
3477     }
3478
3479     if(pow2Width != This->currentDesc.Width ||
3480        pow2Height != This->currentDesc.Height) {
3481         This->Flags |= SFLAG_NONPOW2;
3482     }
3483
3484     TRACE("%p\n", This);
3485     if ((This->pow2Width > GL_LIMITS(texture_size) || This->pow2Height > GL_LIMITS(texture_size)) && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))) {
3486         /* one of three options
3487         1: Do the same as we do with nonpow 2 and scale the texture, (any texture ops would require the texture to be scaled which is potentially slow)
3488         2: Set the texture to the maximum size (bad idea)
3489         3:    WARN and return WINED3DERR_NOTAVAILABLE;
3490         4: Create the surface, but allow it to be used only for DirectDraw Blts. Some apps(e.g. Swat 3) create textures with a Height of 16 and a Width > 3000 and blt 16x16 letter areas from them to the render target.
3491         */
3492         WARN("(%p) Creating an oversized surface\n", This);
3493         This->Flags |= SFLAG_OVERSIZE;
3494
3495         /* This will be initialized on the first blt */
3496         This->glRect.left = 0;
3497         This->glRect.top = 0;
3498         This->glRect.right = 0;
3499         This->glRect.bottom = 0;
3500     } else {
3501         /* Check this after the oversize check - do not make an oversized surface a texture_rectangle one */
3502         if(This->Flags & SFLAG_NONPOW2 && GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3503             This->glDescription.target = GL_TEXTURE_RECTANGLE_ARB;
3504             This->pow2Width  = This->currentDesc.Width;
3505             This->pow2Height = This->currentDesc.Height;
3506             This->Flags &= ~SFLAG_NONPOW2;
3507         }
3508
3509         /* No oversize, gl rect is the full texture size */
3510         This->Flags &= ~SFLAG_OVERSIZE;
3511         This->glRect.left = 0;
3512         This->glRect.top = 0;
3513         This->glRect.right = This->pow2Width;
3514         This->glRect.bottom = This->pow2Height;
3515     }
3516
3517     if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3518         switch(wined3d_settings.offscreen_rendering_mode) {
3519             case ORM_FBO:        This->get_drawable_size = get_drawable_size_fbo;        break;
3520             case ORM_PBUFFER:    This->get_drawable_size = get_drawable_size_pbuffer;    break;
3521             case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3522         }
3523     }
3524
3525     This->Flags |= SFLAG_INSYSMEM;
3526
3527     return WINED3D_OK;
3528 }
3529
3530 static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
3531     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3532     IWineD3DBaseTexture *texture;
3533
3534     TRACE("(%p)->(%s, %s)\n", iface,
3535           flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3536           persistent ? "TRUE" : "FALSE");
3537
3538     /* TODO: For offscreen textures with fbo offscreen rendering the drawable is the same as the texture.*/
3539     if(persistent) {
3540         if((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) {
3541             if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3542                 TRACE("Passing to container\n");
3543                 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3544                 IWineD3DBaseTexture_Release(texture);
3545             }
3546         }
3547         This->Flags &= ~SFLAG_LOCATIONS;
3548         This->Flags |= flag;
3549     } else {
3550         if((This->Flags & SFLAG_INTEXTURE) && (flag & SFLAG_INTEXTURE)) {
3551             if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3552                 TRACE("Passing to container\n");
3553                 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3554                 IWineD3DBaseTexture_Release(texture);
3555             }
3556         }
3557         This->Flags &= ~flag;
3558     }
3559 }
3560
3561 struct coords {
3562     int x, y, z;
3563 };
3564
3565 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in) {
3566     struct coords coords[4];
3567     RECT rect;
3568     IWineD3DSwapChain *swapchain = NULL;
3569     IWineD3DBaseTexture *texture = NULL;
3570     HRESULT hr;
3571     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3572
3573     if(rect_in) {
3574         rect = *rect_in;
3575     } else {
3576         rect.left = 0;
3577         rect.top = 0;
3578         rect.right = This->currentDesc.Width;
3579         rect.bottom = This->currentDesc.Height;
3580     }
3581
3582     ActivateContext(device, device->render_targets[0], CTXUSAGE_BLIT);
3583     ENTER_GL();
3584
3585     if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB) {
3586         glEnable(GL_TEXTURE_RECTANGLE_ARB);
3587         checkGLcall("glEnable(GL_TEXTURE_RECTANGLE_ARB)");
3588         glBindTexture(GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName);
3589         checkGLcall("GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName)");
3590         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3591         checkGLcall("glTexParameteri");
3592         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3593         checkGLcall("glTexParameteri");
3594
3595         coords[0].x = rect.left;
3596         coords[0].z = 0;
3597
3598         coords[1].x = rect.left;
3599         coords[1].z = 0;
3600
3601         coords[2].x = rect.right;
3602         coords[2].z = 0;
3603
3604         coords[3].x = rect.right;
3605         coords[3].z = 0;
3606
3607         coords[0].y = rect.top;
3608         coords[1].y = rect.bottom;
3609         coords[2].y = rect.bottom;
3610         coords[3].y = rect.top;
3611     } else if(This->glDescription.target == GL_TEXTURE_2D) {
3612         glEnable(GL_TEXTURE_2D);
3613         checkGLcall("glEnable(GL_TEXTURE_2D)");
3614         glBindTexture(GL_TEXTURE_2D, This->glDescription.textureName);
3615         checkGLcall("GL_TEXTURE_2D, This->glDescription.textureName)");
3616         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3617         checkGLcall("glTexParameteri");
3618         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3619         checkGLcall("glTexParameteri");
3620
3621         coords[0].x = rect.left   / This->pow2Width;
3622         coords[0].z = 0;
3623
3624         coords[1].x = rect.left   / This->pow2Width;
3625         coords[1].z = 0;
3626
3627         coords[2].x = rect.right  / This->pow2Width;
3628         coords[2].z = 0;
3629
3630         coords[3].x = rect.right  / This->pow2Width;
3631         coords[3].z = 0;
3632
3633         coords[0].y = rect.top    / This->pow2Height;
3634         coords[1].y = rect.bottom / This->pow2Height;
3635         coords[2].y = rect.bottom / This->pow2Height;
3636         coords[3].y = rect.top    / This->pow2Height;
3637     } else {
3638         /* Must be a cube map */
3639         glEnable(GL_TEXTURE_CUBE_MAP_ARB);
3640         checkGLcall("glEnable(GL_TEXTURE_CUBE_MAP_ARB)");
3641         glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName);
3642         checkGLcall("GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName)");
3643         glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3644         checkGLcall("glTexParameteri");
3645         glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3646         checkGLcall("glTexParameteri");
3647
3648         switch(This->glDescription.target) {
3649             case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3650                 coords[0].x =  1;   coords[0].y = -1;   coords[0].z =  1;
3651                 coords[1].x =  1;   coords[1].y =  1;   coords[1].z =  1;
3652                 coords[2].x =  1;   coords[2].y =  1;   coords[2].z = -1;
3653                 coords[3].x =  1;   coords[3].y = -1;   coords[3].z = -1;
3654                 break;
3655
3656             case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3657                 coords[0].x = -1;   coords[0].y = -1;   coords[0].z =  1;
3658                 coords[1].x = -1;   coords[1].y =  1;   coords[1].z =  1;
3659                 coords[2].x = -1;   coords[2].y =  1;   coords[2].z = -1;
3660                 coords[3].x = -1;   coords[3].y = -1;   coords[3].z = -1;
3661                 break;
3662
3663             case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3664                 coords[0].x = -1;   coords[0].y =  1;   coords[0].z =  1;
3665                 coords[1].x =  1;   coords[1].y =  1;   coords[1].z =  1;
3666                 coords[2].x =  1;   coords[2].y =  1;   coords[2].z = -1;
3667                 coords[3].x = -1;   coords[3].y =  1;   coords[3].z = -1;
3668                 break;
3669
3670             case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3671                 coords[0].x = -1;   coords[0].y = -1;   coords[0].z =  1;
3672                 coords[1].x =  1;   coords[1].y = -1;   coords[1].z =  1;
3673                 coords[2].x =  1;   coords[2].y = -1;   coords[2].z = -1;
3674                 coords[3].x = -1;   coords[3].y = -1;   coords[3].z = -1;
3675                 break;
3676
3677             case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3678                 coords[0].x = -1;   coords[0].y = -1;   coords[0].z =  1;
3679                 coords[1].x =  1;   coords[1].y = -1;   coords[1].z =  1;
3680                 coords[2].x =  1;   coords[2].y = -1;   coords[2].z =  1;
3681                 coords[3].x = -1;   coords[3].y = -1;   coords[3].z =  1;
3682                 break;
3683
3684             case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3685                 coords[0].x = -1;   coords[0].y = -1;   coords[0].z = -1;
3686                 coords[1].x =  1;   coords[1].y = -1;   coords[1].z = -1;
3687                 coords[2].x =  1;   coords[2].y = -1;   coords[2].z = -1;
3688                 coords[3].x = -1;   coords[3].y = -1;   coords[3].z = -1;
3689
3690             default:
3691                 ERR("Unexpected texture target\n");
3692                 LEAVE_GL();
3693                 return;
3694         }
3695     }
3696
3697     glBegin(GL_QUADS);
3698     glTexCoord3iv((GLint *) &coords[0]);
3699     glVertex2i(rect.left, device->render_offscreen ? rect.bottom : rect.top);
3700
3701     glTexCoord3iv((GLint *) &coords[1]);
3702     glVertex2i(0, device->render_offscreen ? rect.top : rect.bottom);
3703
3704     glTexCoord3iv((GLint *) &coords[2]);
3705     glVertex2i(rect.right, device->render_offscreen ? rect.top : rect.bottom);
3706
3707     glTexCoord3iv((GLint *) &coords[3]);
3708     glVertex2i(rect.right, device->render_offscreen ? rect.bottom : rect.top);
3709     glEnd();
3710     checkGLcall("glEnd");
3711
3712     if(This->glDescription.target != GL_TEXTURE_2D) {
3713         glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3714         checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
3715     } else {
3716         glDisable(GL_TEXTURE_2D);
3717         checkGLcall("glDisable(GL_TEXTURE_2D)");
3718     }
3719
3720     hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DSwapChain, (void **) &swapchain);
3721     if(hr == WINED3D_OK && swapchain) {
3722         /* Make sure to flush the buffers. This is needed in apps like Red Alert II and Tiberian SUN that use multiple WGL contexts. */
3723         if(((IWineD3DSwapChainImpl*)swapchain)->num_contexts >= 2)
3724             glFlush();
3725
3726         IWineD3DSwapChain_Release(swapchain);
3727     } else {
3728         /* We changed the filtering settings on the texture. Inform the container about this to get the filters
3729          * reset properly next draw
3730          */
3731         hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DBaseTexture, (void **) &texture);
3732         if(hr == WINED3D_OK && texture) {
3733             ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
3734             ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
3735             ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
3736             IWineD3DBaseTexture_Release(texture);
3737         }
3738     }
3739     LEAVE_GL();
3740 }
3741
3742 /*****************************************************************************
3743  * IWineD3DSurface::LoadLocation
3744  *
3745  * Copies the current surface data from wherever it is to the requested
3746  * location. The location is one of the surface flags, SFLAG_INSYSMEM,
3747  * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
3748  * multiple locations, the gl texture is preferred over the drawable, which is
3749  * preferred over system memory. The PBO counts as system memory. If rect is
3750  * not NULL, only the specified rectangle is copied (only supported for
3751  * sysmem<->drawable copies at the moment). If rect is NULL, the destination
3752  * location is marked up to date after the copy.
3753  *
3754  * Parameters:
3755  *  flag: Surface location flag to be updated
3756  *  rect: rectangle to be copied
3757  *
3758  * Returns:
3759  *  WINED3D_OK on success
3760  *  WINED3DERR_DEVICELOST on an internal error
3761  *
3762  *****************************************************************************/
3763 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
3764     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3765     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3766     GLenum format, internal, type;
3767     CONVERT_TYPES convert;
3768     int bpp;
3769     int width, pitch, outpitch;
3770     BYTE *mem;
3771
3772     TRACE("(%p)->(%s, %p)\n", iface,
3773           flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3774           rect);
3775     if(rect) {
3776         TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
3777     }
3778
3779     /* TODO: For fbo targets, texture == drawable */
3780     if(This->Flags & flag) {
3781         TRACE("Location already up to date\n");
3782         return WINED3D_OK;
3783     }
3784
3785     if(!(This->Flags & SFLAG_LOCATIONS)) {
3786         ERR("Surface does not have any up to date location\n");
3787         This->Flags |= SFLAG_LOST;
3788         return WINED3DERR_DEVICELOST;
3789     }
3790
3791     if(flag == SFLAG_INSYSMEM) {
3792         surface_prepare_system_memory(This);
3793
3794         /* Download the surface to system memory */
3795         if(This->Flags & SFLAG_INTEXTURE) {
3796             surface_download_data(This);
3797         } else {
3798             read_from_framebuffer(This, rect,
3799                                   This->resource.allocatedMemory,
3800                                   IWineD3DSurface_GetPitch(iface));
3801         }
3802     } else if(flag == SFLAG_INDRAWABLE) {
3803         if(This->Flags & SFLAG_INTEXTURE) {
3804             surface_blt_to_drawable(This, rect);
3805         } else {
3806             flush_to_framebuffer_drawpixels(This);
3807         }
3808     } else /* if(flag == SFLAG_INTEXTURE) */ {
3809         d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
3810
3811         if (This->Flags & SFLAG_INDRAWABLE) {
3812             GLint prevRead;
3813
3814             ENTER_GL();
3815             glGetIntegerv(GL_READ_BUFFER, &prevRead);
3816             vcheckGLcall("glGetIntegerv");
3817             glReadBuffer(This->resource.wineD3DDevice->offscreenBuffer);
3818             vcheckGLcall("glReadBuffer");
3819
3820             if(!(This->Flags & SFLAG_ALLOCATED)) {
3821                 surface_allocate_surface(This, internal, This->pow2Width,
3822                                             This->pow2Height, format, type);
3823             }
3824
3825             clear_unused_channels(This);
3826
3827             glCopyTexSubImage2D(This->glDescription.target,
3828                                 This->glDescription.level,
3829                                 0, 0, 0, 0,
3830                                 This->currentDesc.Width,
3831                                 This->currentDesc.Height);
3832             checkGLcall("glCopyTexSubImage2D");
3833
3834             glReadBuffer(prevRead);
3835             vcheckGLcall("glReadBuffer");
3836
3837             LEAVE_GL();
3838
3839             TRACE("Updated target %d\n", This->glDescription.target);
3840         } else {
3841             /* The only place where LoadTexture() might get called when isInDraw=1
3842              * is ActivateContext where lastActiveRenderTarget is preloaded.
3843              */
3844             if(iface == device->lastActiveRenderTarget && device->isInDraw)
3845                 ERR("Reading back render target but SFLAG_INDRAWABLE not set\n");
3846
3847             /* Otherwise: System memory copy must be most up to date */
3848
3849             if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
3850                 This->Flags |= SFLAG_GLCKEY;
3851                 This->glCKey = This->SrcBltCKey;
3852             }
3853             else This->Flags &= ~SFLAG_GLCKEY;
3854
3855             /* The width is in 'length' not in bytes */
3856             width = This->currentDesc.Width;
3857             pitch = IWineD3DSurface_GetPitch(iface);
3858
3859             if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
3860                 int height = This->currentDesc.Height;
3861
3862                 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
3863                 outpitch = width * bpp;
3864                 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
3865
3866                 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
3867                 if(!mem) {
3868                     ERR("Out of memory %d, %d!\n", outpitch, height);
3869                     return WINED3DERR_OUTOFVIDEOMEMORY;
3870                 }
3871                 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
3872
3873                 This->Flags |= SFLAG_CONVERTED;
3874             } else if( (This->resource.format == WINED3DFMT_P8) && (GL_SUPPORT(EXT_PALETTED_TEXTURE) || GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) ) {
3875                 d3dfmt_p8_upload_palette(iface, convert);
3876                 This->Flags &= ~SFLAG_CONVERTED;
3877                 mem = This->resource.allocatedMemory;
3878             } else {
3879                 This->Flags &= ~SFLAG_CONVERTED;
3880                 mem = This->resource.allocatedMemory;
3881             }
3882
3883             /* Make sure the correct pitch is used */
3884             glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
3885
3886             if ((This->Flags & SFLAG_NONPOW2) && !(This->Flags & SFLAG_OVERSIZE)) {
3887                 TRACE("non power of two support\n");
3888                 if(!(This->Flags & SFLAG_ALLOCATED)) {
3889                     surface_allocate_surface(This, internal, This->pow2Width, This->pow2Height, format, type);
3890                 }
3891                 if (mem || (This->Flags & SFLAG_PBO)) {
3892                     surface_upload_data(This, internal, This->currentDesc.Width, This->currentDesc.Height, format, type, mem);
3893                 }
3894             } else {
3895                 /* When making the realloc conditional, keep in mind that GL_APPLE_client_storage may be in use, and This->resource.allocatedMemory
3896                  * changed. So also keep track of memory changes. In this case the texture has to be reallocated
3897                  */
3898                 if(!(This->Flags & SFLAG_ALLOCATED)) {
3899                     surface_allocate_surface(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type);
3900                 }
3901                 if (mem || (This->Flags & SFLAG_PBO)) {
3902                     surface_upload_data(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type, mem);
3903                 }
3904             }
3905
3906             /* Restore the default pitch */
3907             glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3908
3909             /* Don't delete PBO memory */
3910             if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
3911                 HeapFree(GetProcessHeap(), 0, mem);
3912         }
3913     }
3914
3915     if(rect == NULL) {
3916         This->Flags |= flag;
3917     }
3918
3919     return WINED3D_OK;
3920 }
3921
3922 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
3923     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3924     IWineD3DSwapChain *swapchain = NULL;
3925
3926     /* Update the drawable size method */
3927     if(container) {
3928         IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
3929     }
3930     if(swapchain) {
3931         This->get_drawable_size = get_drawable_size_swapchain;
3932         IWineD3DSwapChain_Release(swapchain);
3933     } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3934         switch(wined3d_settings.offscreen_rendering_mode) {
3935             case ORM_FBO:        This->get_drawable_size = get_drawable_size_fbo;        break;
3936             case ORM_PBUFFER:    This->get_drawable_size = get_drawable_size_pbuffer;    break;
3937             case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3938         }
3939     }
3940
3941     return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
3942 }
3943
3944 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
3945 {
3946     /* IUnknown */
3947     IWineD3DBaseSurfaceImpl_QueryInterface,
3948     IWineD3DBaseSurfaceImpl_AddRef,
3949     IWineD3DSurfaceImpl_Release,
3950     /* IWineD3DResource */
3951     IWineD3DBaseSurfaceImpl_GetParent,
3952     IWineD3DBaseSurfaceImpl_GetDevice,
3953     IWineD3DBaseSurfaceImpl_SetPrivateData,
3954     IWineD3DBaseSurfaceImpl_GetPrivateData,
3955     IWineD3DBaseSurfaceImpl_FreePrivateData,
3956     IWineD3DBaseSurfaceImpl_SetPriority,
3957     IWineD3DBaseSurfaceImpl_GetPriority,
3958     IWineD3DSurfaceImpl_PreLoad,
3959     IWineD3DBaseSurfaceImpl_GetType,
3960     /* IWineD3DSurface */
3961     IWineD3DBaseSurfaceImpl_GetContainer,
3962     IWineD3DBaseSurfaceImpl_GetDesc,
3963     IWineD3DSurfaceImpl_LockRect,
3964     IWineD3DSurfaceImpl_UnlockRect,
3965     IWineD3DSurfaceImpl_GetDC,
3966     IWineD3DSurfaceImpl_ReleaseDC,
3967     IWineD3DSurfaceImpl_Flip,
3968     IWineD3DSurfaceImpl_Blt,
3969     IWineD3DBaseSurfaceImpl_GetBltStatus,
3970     IWineD3DBaseSurfaceImpl_GetFlipStatus,
3971     IWineD3DBaseSurfaceImpl_IsLost,
3972     IWineD3DBaseSurfaceImpl_Restore,
3973     IWineD3DSurfaceImpl_BltFast,
3974     IWineD3DBaseSurfaceImpl_GetPalette,
3975     IWineD3DBaseSurfaceImpl_SetPalette,
3976     IWineD3DBaseSurfaceImpl_RealizePalette,
3977     IWineD3DBaseSurfaceImpl_SetColorKey,
3978     IWineD3DBaseSurfaceImpl_GetPitch,
3979     IWineD3DSurfaceImpl_SetMem,
3980     IWineD3DBaseSurfaceImpl_SetOverlayPosition,
3981     IWineD3DBaseSurfaceImpl_GetOverlayPosition,
3982     IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
3983     IWineD3DBaseSurfaceImpl_UpdateOverlay,
3984     IWineD3DBaseSurfaceImpl_SetClipper,
3985     IWineD3DBaseSurfaceImpl_GetClipper,
3986     /* Internal use: */
3987     IWineD3DSurfaceImpl_AddDirtyRect,
3988     IWineD3DSurfaceImpl_LoadTexture,
3989     IWineD3DSurfaceImpl_BindTexture,
3990     IWineD3DSurfaceImpl_SaveSnapshot,
3991     IWineD3DSurfaceImpl_SetContainer,
3992     IWineD3DSurfaceImpl_SetGlTextureDesc,
3993     IWineD3DSurfaceImpl_GetGlDesc,
3994     IWineD3DSurfaceImpl_GetData,
3995     IWineD3DSurfaceImpl_SetFormat,
3996     IWineD3DSurfaceImpl_PrivateSetup,
3997     IWineD3DSurfaceImpl_ModifyLocation,
3998     IWineD3DSurfaceImpl_LoadLocation
3999 };