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