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