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