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