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