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