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