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