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