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