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