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