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