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