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