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