2 * IWineD3DSurface Implementation
4 * Copyright 1997-2000 Marcus Meissner
5 * Copyright 1998-2000 Lionel Ulmer
6 * Copyright 2000-2001 TransGaming Technologies Inc.
7 * Copyright 2002-2005 Jason Edmeades
8 * Copyright 2002-2003 Raphael Junqueira
9 * Copyright 2004 Christian Costa
10 * Copyright 2005 Oliver Stieber
11 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
12 * Copyright 2007-2008 Henri Verbeet
13 * Copyright 2006-2008 Roderick Colenbrander
14 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include "wine/port.h"
33 #include "wined3d_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
36 WINE_DECLARE_DEBUG_CHANNEL(d3d);
38 static void surface_cleanup(IWineD3DSurfaceImpl *This)
40 TRACE("(%p) : Cleaning up.\n", This);
42 if (This->texture_name || (This->flags & SFLAG_PBO) || !list_empty(&This->renderbuffers))
44 const struct wined3d_gl_info *gl_info;
45 renderbuffer_entry_t *entry, *entry2;
46 struct wined3d_context *context;
48 context = context_acquire(This->resource.device, NULL);
49 gl_info = context->gl_info;
53 if (This->texture_name)
55 TRACE("Deleting texture %u.\n", This->texture_name);
56 glDeleteTextures(1, &This->texture_name);
59 if (This->flags & SFLAG_PBO)
61 TRACE("Deleting PBO %u.\n", This->pbo);
62 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
65 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry)
67 TRACE("Deleting renderbuffer %u.\n", entry->id);
68 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
69 HeapFree(GetProcessHeap(), 0, entry);
74 context_release(context);
77 if (This->flags & SFLAG_DIBSECTION)
80 SelectObject(This->hDC, This->dib.holdbitmap);
82 /* Release the DIB section. */
83 DeleteObject(This->dib.DIBsection);
84 This->dib.bitmap_data = NULL;
85 This->resource.allocatedMemory = NULL;
88 if (This->flags & SFLAG_USERPTR) IWineD3DSurface_SetMem((IWineD3DSurface *)This, NULL);
89 if (This->overlay_dest) list_remove(&This->overlay_entry);
91 HeapFree(GetProcessHeap(), 0, This->palette9);
93 resource_cleanup(&This->resource);
96 void surface_set_container(IWineD3DSurfaceImpl *surface, enum wined3d_container_type type, void *container)
98 TRACE("surface %p, container %p.\n", surface, container);
100 if (!container && type != WINED3D_CONTAINER_NONE)
101 ERR("Setting NULL container of type %#x.\n", type);
103 if (type == WINED3D_CONTAINER_SWAPCHAIN)
105 surface->get_drawable_size = get_drawable_size_swapchain;
109 switch (wined3d_settings.offscreen_rendering_mode)
112 surface->get_drawable_size = get_drawable_size_fbo;
116 surface->get_drawable_size = get_drawable_size_backbuffer;
120 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
125 surface->container.type = type;
126 surface->container.u.base = container;
133 enum tex_types tex_type;
134 GLfloat coords[4][3];
145 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct float_rect *f)
147 f->l = ((r->left * 2.0f) / w) - 1.0f;
148 f->t = ((r->top * 2.0f) / h) - 1.0f;
149 f->r = ((r->right * 2.0f) / w) - 1.0f;
150 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
153 static void surface_get_blt_info(GLenum target, const RECT *rect, GLsizei w, GLsizei h, struct blt_info *info)
155 GLfloat (*coords)[3] = info->coords;
161 FIXME("Unsupported texture target %#x\n", target);
162 /* Fall back to GL_TEXTURE_2D */
164 info->binding = GL_TEXTURE_BINDING_2D;
165 info->bind_target = GL_TEXTURE_2D;
166 info->tex_type = tex_2d;
167 coords[0][0] = (float)rect->left / w;
168 coords[0][1] = (float)rect->top / h;
171 coords[1][0] = (float)rect->right / w;
172 coords[1][1] = (float)rect->top / h;
175 coords[2][0] = (float)rect->left / w;
176 coords[2][1] = (float)rect->bottom / h;
179 coords[3][0] = (float)rect->right / w;
180 coords[3][1] = (float)rect->bottom / h;
184 case GL_TEXTURE_RECTANGLE_ARB:
185 info->binding = GL_TEXTURE_BINDING_RECTANGLE_ARB;
186 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
187 info->tex_type = tex_rect;
188 coords[0][0] = rect->left; coords[0][1] = rect->top; coords[0][2] = 0.0f;
189 coords[1][0] = rect->right; coords[1][1] = rect->top; coords[1][2] = 0.0f;
190 coords[2][0] = rect->left; coords[2][1] = rect->bottom; coords[2][2] = 0.0f;
191 coords[3][0] = rect->right; coords[3][1] = rect->bottom; coords[3][2] = 0.0f;
194 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
195 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
196 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
197 info->tex_type = tex_cube;
198 cube_coords_float(rect, w, h, &f);
200 coords[0][0] = 1.0f; coords[0][1] = -f.t; coords[0][2] = -f.l;
201 coords[1][0] = 1.0f; coords[1][1] = -f.t; coords[1][2] = -f.r;
202 coords[2][0] = 1.0f; coords[2][1] = -f.b; coords[2][2] = -f.l;
203 coords[3][0] = 1.0f; coords[3][1] = -f.b; coords[3][2] = -f.r;
206 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
207 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
208 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
209 info->tex_type = tex_cube;
210 cube_coords_float(rect, w, h, &f);
212 coords[0][0] = -1.0f; coords[0][1] = -f.t; coords[0][2] = f.l;
213 coords[1][0] = -1.0f; coords[1][1] = -f.t; coords[1][2] = f.r;
214 coords[2][0] = -1.0f; coords[2][1] = -f.b; coords[2][2] = f.l;
215 coords[3][0] = -1.0f; coords[3][1] = -f.b; coords[3][2] = f.r;
218 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
219 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
220 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
221 info->tex_type = tex_cube;
222 cube_coords_float(rect, w, h, &f);
224 coords[0][0] = f.l; coords[0][1] = 1.0f; coords[0][2] = f.t;
225 coords[1][0] = f.r; coords[1][1] = 1.0f; coords[1][2] = f.t;
226 coords[2][0] = f.l; coords[2][1] = 1.0f; coords[2][2] = f.b;
227 coords[3][0] = f.r; coords[3][1] = 1.0f; coords[3][2] = f.b;
230 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
231 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
232 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
233 info->tex_type = tex_cube;
234 cube_coords_float(rect, w, h, &f);
236 coords[0][0] = f.l; coords[0][1] = -1.0f; coords[0][2] = -f.t;
237 coords[1][0] = f.r; coords[1][1] = -1.0f; coords[1][2] = -f.t;
238 coords[2][0] = f.l; coords[2][1] = -1.0f; coords[2][2] = -f.b;
239 coords[3][0] = f.r; coords[3][1] = -1.0f; coords[3][2] = -f.b;
242 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
243 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
244 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
245 info->tex_type = tex_cube;
246 cube_coords_float(rect, w, h, &f);
248 coords[0][0] = f.l; coords[0][1] = -f.t; coords[0][2] = 1.0f;
249 coords[1][0] = f.r; coords[1][1] = -f.t; coords[1][2] = 1.0f;
250 coords[2][0] = f.l; coords[2][1] = -f.b; coords[2][2] = 1.0f;
251 coords[3][0] = f.r; coords[3][1] = -f.b; coords[3][2] = 1.0f;
254 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
255 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
256 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
257 info->tex_type = tex_cube;
258 cube_coords_float(rect, w, h, &f);
260 coords[0][0] = -f.l; coords[0][1] = -f.t; coords[0][2] = -1.0f;
261 coords[1][0] = -f.r; coords[1][1] = -f.t; coords[1][2] = -1.0f;
262 coords[2][0] = -f.l; coords[2][1] = -f.b; coords[2][2] = -1.0f;
263 coords[3][0] = -f.r; coords[3][1] = -f.b; coords[3][2] = -1.0f;
268 static inline void surface_get_rect(IWineD3DSurfaceImpl *This, const RECT *rect_in, RECT *rect_out)
271 *rect_out = *rect_in;
276 rect_out->right = This->resource.width;
277 rect_out->bottom = This->resource.height;
281 /* GL locking and context activation is done by the caller */
282 void draw_textured_quad(IWineD3DSurfaceImpl *src_surface, const RECT *src_rect, const RECT *dst_rect, WINED3DTEXTUREFILTERTYPE Filter)
284 struct blt_info info;
286 surface_get_blt_info(src_surface->texture_target, src_rect, src_surface->pow2Width, src_surface->pow2Height, &info);
288 glEnable(info.bind_target);
289 checkGLcall("glEnable(bind_target)");
291 /* Bind the texture */
292 glBindTexture(info.bind_target, src_surface->texture_name);
293 checkGLcall("glBindTexture");
295 /* Filtering for StretchRect */
296 glTexParameteri(info.bind_target, GL_TEXTURE_MAG_FILTER,
297 wined3d_gl_mag_filter(magLookup, Filter));
298 checkGLcall("glTexParameteri");
299 glTexParameteri(info.bind_target, GL_TEXTURE_MIN_FILTER,
300 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
301 checkGLcall("glTexParameteri");
302 glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
303 glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
304 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
305 checkGLcall("glTexEnvi");
308 glBegin(GL_TRIANGLE_STRIP);
309 glTexCoord3fv(info.coords[0]);
310 glVertex2i(dst_rect->left, dst_rect->top);
312 glTexCoord3fv(info.coords[1]);
313 glVertex2i(dst_rect->right, dst_rect->top);
315 glTexCoord3fv(info.coords[2]);
316 glVertex2i(dst_rect->left, dst_rect->bottom);
318 glTexCoord3fv(info.coords[3]);
319 glVertex2i(dst_rect->right, dst_rect->bottom);
322 /* Unbind the texture */
323 glBindTexture(info.bind_target, 0);
324 checkGLcall("glBindTexture(info->bind_target, 0)");
326 /* We changed the filtering settings on the texture. Inform the
327 * container about this to get the filters reset properly next draw. */
328 if (src_surface->container.type == WINED3D_CONTAINER_TEXTURE)
330 struct wined3d_texture *texture = src_surface->container.u.texture;
331 texture->texture_rgb.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
332 texture->texture_rgb.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
333 texture->texture_rgb.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
337 static void surface_realize_palette(IWineD3DSurfaceImpl *surface)
339 struct wined3d_palette *palette = surface->palette;
341 TRACE("surface %p.\n", surface);
343 if (!palette) return;
345 if (surface->resource.format->id == WINED3DFMT_P8_UINT
346 || surface->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
348 if (surface->resource.usage & WINED3DUSAGE_RENDERTARGET)
350 /* Make sure the texture is up to date. This call doesn't do
351 * anything if the texture is already up to date. */
352 surface_load_location(surface, SFLAG_INTEXTURE, NULL);
354 /* We want to force a palette refresh, so mark the drawable as not being up to date */
355 surface_modify_location(surface, SFLAG_INDRAWABLE, FALSE);
359 if (!(surface->flags & SFLAG_INSYSMEM))
361 TRACE("Palette changed with surface that does not have an up to date system memory copy.\n");
362 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
364 surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
368 if (surface->flags & SFLAG_DIBSECTION)
373 TRACE("Updating the DC's palette.\n");
375 for (i = 0; i < 256; ++i)
377 col[i].rgbRed = palette->palents[i].peRed;
378 col[i].rgbGreen = palette->palents[i].peGreen;
379 col[i].rgbBlue = palette->palents[i].peBlue;
380 col[i].rgbReserved = 0;
382 SetDIBColorTable(surface->hDC, 0, 256, col);
385 /* Propagate the changes to the drawable when we have a palette. */
386 if (surface->resource.usage & WINED3DUSAGE_RENDERTARGET)
387 surface_load_location(surface, SFLAG_INDRAWABLE, NULL);
390 static HRESULT surface_draw_overlay(IWineD3DSurfaceImpl *surface)
394 /* If there's no destination surface there is nothing to do. */
395 if (!surface->overlay_dest)
398 /* Blt calls ModifyLocation on the dest surface, which in turn calls
399 * DrawOverlay to update the overlay. Prevent an endless recursion. */
400 if (surface->overlay_dest->flags & SFLAG_INOVERLAYDRAW)
403 surface->overlay_dest->flags |= SFLAG_INOVERLAYDRAW;
404 hr = IWineD3DSurface_Blt((IWineD3DSurface *)surface->overlay_dest,
405 &surface->overlay_destrect, (IWineD3DSurface *)surface, &surface->overlay_srcrect,
406 WINEDDBLT_WAIT, NULL, WINED3DTEXF_LINEAR);
407 surface->overlay_dest->flags &= ~SFLAG_INOVERLAYDRAW;
412 /* Context activation is done by the caller. */
413 static void surface_remove_pbo(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info)
415 if (!surface->resource.heapMemory)
417 surface->resource.heapMemory = HeapAlloc(GetProcessHeap(), 0, surface->resource.size + RESOURCE_ALIGNMENT);
418 surface->resource.allocatedMemory = (BYTE *)(((ULONG_PTR)surface->resource.heapMemory
419 + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
423 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, surface->pbo));
424 checkGLcall("glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, surface->pbo)");
425 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0,
426 surface->resource.size, surface->resource.allocatedMemory));
427 checkGLcall("glGetBufferSubDataARB");
428 GL_EXTCALL(glDeleteBuffersARB(1, &surface->pbo));
429 checkGLcall("glDeleteBuffersARB");
433 surface->flags &= ~SFLAG_PBO;
436 /* Do not call while under the GL lock. */
437 static void surface_unload(struct wined3d_resource *resource)
439 IWineD3DSurfaceImpl *surface = surface_from_resource(resource);
440 IWineD3DDeviceImpl *device = resource->device;
441 const struct wined3d_gl_info *gl_info;
442 renderbuffer_entry_t *entry, *entry2;
443 struct wined3d_context *context;
445 TRACE("surface %p.\n", surface);
447 if (resource->pool == WINED3DPOOL_DEFAULT)
449 /* Default pool resources are supposed to be destroyed before Reset is called.
450 * Implicit resources stay however. So this means we have an implicit render target
451 * or depth stencil. The content may be destroyed, but we still have to tear down
452 * opengl resources, so we cannot leave early.
454 * Put the surfaces into sysmem, and reset the content. The D3D content is undefined,
455 * but we can't set the sysmem INDRAWABLE because when we're rendering the swapchain
456 * or the depth stencil into an FBO the texture or render buffer will be removed
457 * and all flags get lost
459 surface_init_sysmem(surface);
463 /* Load the surface into system memory */
464 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
465 surface_modify_location(surface, SFLAG_INDRAWABLE, FALSE);
467 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
468 surface_modify_location(surface, SFLAG_INSRGBTEX, FALSE);
469 surface->flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
471 context = context_acquire(device, NULL);
472 gl_info = context->gl_info;
474 /* Destroy PBOs, but load them into real sysmem before */
475 if (surface->flags & SFLAG_PBO)
476 surface_remove_pbo(surface, gl_info);
478 /* Destroy fbo render buffers. This is needed for implicit render targets, for
479 * all application-created targets the application has to release the surface
480 * before calling _Reset
482 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, renderbuffer_entry_t, entry)
485 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
487 list_remove(&entry->entry);
488 HeapFree(GetProcessHeap(), 0, entry);
490 list_init(&surface->renderbuffers);
491 surface->current_renderbuffer = NULL;
493 /* If we're in a texture, the texture name belongs to the texture.
494 * Otherwise, destroy it. */
495 if (surface->container.type != WINED3D_CONTAINER_TEXTURE)
498 glDeleteTextures(1, &surface->texture_name);
499 surface->texture_name = 0;
500 glDeleteTextures(1, &surface->texture_name_srgb);
501 surface->texture_name_srgb = 0;
505 context_release(context);
507 resource_unload(resource);
510 static const struct wined3d_resource_ops surface_resource_ops =
515 static const struct wined3d_surface_ops surface_ops =
517 surface_realize_palette,
518 surface_draw_overlay,
521 static void surface_gdi_cleanup(IWineD3DSurfaceImpl *surface)
523 TRACE("surface %p.\n", surface);
525 if (surface->flags & SFLAG_DIBSECTION)
527 /* Release the DC. */
528 SelectObject(surface->hDC, surface->dib.holdbitmap);
529 DeleteDC(surface->hDC);
530 /* Release the DIB section. */
531 DeleteObject(surface->dib.DIBsection);
532 surface->dib.bitmap_data = NULL;
533 surface->resource.allocatedMemory = NULL;
536 if (surface->flags & SFLAG_USERPTR)
537 IWineD3DSurface_SetMem((IWineD3DSurface *)surface, NULL);
538 if (surface->overlay_dest)
539 list_remove(&surface->overlay_entry);
541 HeapFree(GetProcessHeap(), 0, surface->palette9);
543 resource_cleanup(&surface->resource);
546 static void gdi_surface_realize_palette(IWineD3DSurfaceImpl *surface)
548 struct wined3d_palette *palette = surface->palette;
550 TRACE("surface %p.\n", surface);
552 if (!palette) return;
554 if (surface->flags & SFLAG_DIBSECTION)
559 TRACE("Updating the DC's palette.\n");
561 for (i = 0; i < 256; ++i)
563 col[i].rgbRed = palette->palents[i].peRed;
564 col[i].rgbGreen = palette->palents[i].peGreen;
565 col[i].rgbBlue = palette->palents[i].peBlue;
566 col[i].rgbReserved = 0;
568 SetDIBColorTable(surface->hDC, 0, 256, col);
571 /* Update the image because of the palette change. Some games like e.g.
572 * Red Alert call SetEntries a lot to implement fading. */
573 /* Tell the swapchain to update the screen. */
574 if (surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
576 struct wined3d_swapchain *swapchain = surface->container.u.swapchain;
577 if (surface == swapchain->front_buffer)
579 x11_copy_to_screen(swapchain, NULL);
584 static HRESULT gdi_surface_draw_overlay(IWineD3DSurfaceImpl *surface)
586 FIXME("GDI surfaces can't draw overlays yet.\n");
590 static const struct wined3d_surface_ops gdi_surface_ops =
592 gdi_surface_realize_palette,
593 gdi_surface_draw_overlay,
596 static void surface_force_reload(IWineD3DSurfaceImpl *surface)
598 surface->flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
601 void surface_set_texture_name(IWineD3DSurfaceImpl *surface, GLuint new_name, BOOL srgb)
606 TRACE("surface %p, new_name %u, srgb %#x.\n", surface, new_name, srgb);
610 name = &surface->texture_name_srgb;
611 flag = SFLAG_INSRGBTEX;
615 name = &surface->texture_name;
616 flag = SFLAG_INTEXTURE;
619 if (!*name && new_name)
621 /* FIXME: We shouldn't need to remove SFLAG_INTEXTURE if the
622 * surface has no texture name yet. See if we can get rid of this. */
623 if (surface->flags & flag)
624 ERR("Surface has %s set, but no texture name.\n", debug_surflocation(flag));
625 surface_modify_location(surface, flag, FALSE);
629 surface_force_reload(surface);
632 void surface_set_texture_target(IWineD3DSurfaceImpl *surface, GLenum target)
634 TRACE("surface %p, target %#x.\n", surface, target);
636 if (surface->texture_target != target)
638 if (target == GL_TEXTURE_RECTANGLE_ARB)
640 surface->flags &= ~SFLAG_NORMCOORD;
642 else if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
644 surface->flags |= SFLAG_NORMCOORD;
647 surface->texture_target = target;
648 surface_force_reload(surface);
651 /* Context activation is done by the caller. */
652 void surface_bind(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info, BOOL srgb)
654 TRACE("surface %p, gl_info %p, srgb %#x.\n", surface, gl_info, srgb);
656 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
658 struct wined3d_texture *texture = surface->container.u.texture;
660 TRACE("Passing to container (%p).\n", texture);
661 texture->texture_ops->texture_bind(texture, gl_info, srgb);
665 if (surface->texture_level)
667 ERR("Standalone surface %p is non-zero texture level %u.\n",
668 surface, surface->texture_level);
672 ERR("Trying to bind standalone surface %p as sRGB.\n", surface);
676 if (!surface->texture_name)
678 glGenTextures(1, &surface->texture_name);
679 checkGLcall("glGenTextures");
681 TRACE("Surface %p given name %u.\n", surface, surface->texture_name);
683 glBindTexture(surface->texture_target, surface->texture_name);
684 checkGLcall("glBindTexture");
685 glTexParameteri(surface->texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
686 glTexParameteri(surface->texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
687 glTexParameteri(surface->texture_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
688 glTexParameteri(surface->texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
689 glTexParameteri(surface->texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
690 checkGLcall("glTexParameteri");
694 glBindTexture(surface->texture_target, surface->texture_name);
695 checkGLcall("glBindTexture");
702 /* Context activation is done by the caller. */
703 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl *surface,
704 const struct wined3d_gl_info *gl_info, BOOL srgb)
706 IWineD3DDeviceImpl *device = surface->resource.device;
707 DWORD active_sampler;
709 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
710 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
711 * gl states. The current texture unit should always be a valid one.
713 * To be more specific, this is tricky because we can implicitly be called
714 * from sampler() in state.c. This means we can't touch anything other than
715 * whatever happens to be the currently active texture, or we would risk
716 * marking already applied sampler states dirty again.
718 * TODO: Track the current active texture per GL context instead of using glGet
720 GLint active_texture;
722 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
724 active_sampler = device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
726 if (active_sampler != WINED3D_UNMAPPED_STAGE)
728 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_SAMPLER(active_sampler));
730 surface_bind(surface, gl_info, srgb);
733 /* This function checks if the primary render target uses the 8bit paletted format. */
734 static BOOL primary_render_target_is_p8(IWineD3DDeviceImpl *device)
736 if (device->render_targets && device->render_targets[0])
738 IWineD3DSurfaceImpl *render_target = device->render_targets[0];
739 if ((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET)
740 && (render_target->resource.format->id == WINED3DFMT_P8_UINT))
746 /* This call just downloads data, the caller is responsible for binding the
747 * correct texture. */
748 /* Context activation is done by the caller. */
749 static void surface_download_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info)
751 const struct wined3d_format *format = This->resource.format;
753 /* Only support read back of converted P8 surfaces */
754 if (This->flags & SFLAG_CONVERTED && format->id != WINED3DFMT_P8_UINT)
756 FIXME("Readback conversion not supported for format %s.\n", debug_d3dformat(format->id));
762 if (format->flags & WINED3DFMT_FLAG_COMPRESSED)
764 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p.\n",
765 This, This->texture_level, format->glFormat, format->glType,
766 This->resource.allocatedMemory);
768 if (This->flags & SFLAG_PBO)
770 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
771 checkGLcall("glBindBufferARB");
772 GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target, This->texture_level, NULL));
773 checkGLcall("glGetCompressedTexImageARB");
774 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
775 checkGLcall("glBindBufferARB");
779 GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target,
780 This->texture_level, This->resource.allocatedMemory));
781 checkGLcall("glGetCompressedTexImageARB");
787 GLenum gl_format = format->glFormat;
788 GLenum gl_type = format->glType;
792 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
793 if (format->id == WINED3DFMT_P8_UINT && primary_render_target_is_p8(This->resource.device))
795 gl_format = GL_ALPHA;
796 gl_type = GL_UNSIGNED_BYTE;
799 if (This->flags & SFLAG_NONPOW2)
801 unsigned char alignment = This->resource.device->surface_alignment;
802 src_pitch = format->byte_count * This->pow2Width;
803 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
804 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
805 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
807 mem = This->resource.allocatedMemory;
810 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n",
811 This, This->texture_level, gl_format, gl_type, mem);
813 if (This->flags & SFLAG_PBO)
815 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
816 checkGLcall("glBindBufferARB");
818 glGetTexImage(This->texture_target, This->texture_level, gl_format, gl_type, NULL);
819 checkGLcall("glGetTexImage");
821 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
822 checkGLcall("glBindBufferARB");
824 glGetTexImage(This->texture_target, This->texture_level, gl_format, gl_type, mem);
825 checkGLcall("glGetTexImage");
829 if (This->flags & SFLAG_NONPOW2)
831 const BYTE *src_data;
835 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
836 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
837 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
839 * We're doing this...
841 * instead of boxing the texture :
842 * |<-texture width ->| -->pow2width| /\
843 * |111111111111111111| | |
844 * |222 Texture 222222| boxed empty | texture height
845 * |3333 Data 33333333| | |
846 * |444444444444444444| | \/
847 * ----------------------------------- |
848 * | boxed empty | boxed empty | pow2height
850 * -----------------------------------
853 * we're repacking the data to the expected texture width
855 * |<-texture width ->| -->pow2width| /\
856 * |111111111111111111222222222222222| |
857 * |222333333333333333333444444444444| texture height
861 * | empty | pow2height
863 * -----------------------------------
867 * |<-texture width ->| /\
868 * |111111111111111111|
869 * |222222222222222222|texture height
870 * |333333333333333333|
871 * |444444444444444444| \/
872 * --------------------
874 * this also means that any references to allocatedMemory should work with the data as if were a
875 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
877 * internally the texture is still stored in a boxed format so any references to textureName will
878 * get a boxed texture with width pow2width and not a texture of width resource.width.
880 * Performance should not be an issue, because applications normally do not lock the surfaces when
881 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
882 * and doesn't have to be re-read.
885 dst_data = This->resource.allocatedMemory;
886 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
887 for (y = 1; y < This->resource.height; ++y)
889 /* skip the first row */
890 src_data += src_pitch;
891 dst_data += dst_pitch;
892 memcpy(dst_data, src_data, dst_pitch);
895 HeapFree(GetProcessHeap(), 0, mem);
899 /* Surface has now been downloaded */
900 This->flags |= SFLAG_INSYSMEM;
903 /* This call just uploads data, the caller is responsible for binding the
904 * correct texture. */
905 /* Context activation is done by the caller. */
906 static void surface_upload_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
907 const struct wined3d_format *format, BOOL srgb, const GLvoid *data)
909 GLsizei width = This->resource.width;
910 GLsizei height = This->resource.height;
915 internal = format->glGammaInternal;
917 else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET && surface_is_offscreen(This))
919 internal = format->rtInternal;
923 internal = format->glInternal;
926 TRACE("This %p, internal %#x, width %d, height %d, format %#x, type %#x, data %p.\n",
927 This, internal, width, height, format->glFormat, format->glType, data);
928 TRACE("target %#x, level %u, resource size %u.\n",
929 This->texture_target, This->texture_level, This->resource.size);
931 if (format->heightscale != 1.0f && format->heightscale != 0.0f) height *= format->heightscale;
935 if (This->flags & SFLAG_PBO)
937 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
938 checkGLcall("glBindBufferARB");
940 TRACE("(%p) pbo: %#x, data: %p.\n", This, This->pbo, data);
944 if (format->flags & WINED3DFMT_FLAG_COMPRESSED)
946 TRACE("Calling glCompressedTexSubImage2DARB.\n");
948 GL_EXTCALL(glCompressedTexSubImage2DARB(This->texture_target, This->texture_level,
949 0, 0, width, height, internal, This->resource.size, data));
950 checkGLcall("glCompressedTexSubImage2DARB");
954 TRACE("Calling glTexSubImage2D.\n");
956 glTexSubImage2D(This->texture_target, This->texture_level,
957 0, 0, width, height, format->glFormat, format->glType, data);
958 checkGLcall("glTexSubImage2D");
961 if (This->flags & SFLAG_PBO)
963 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
964 checkGLcall("glBindBufferARB");
969 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
971 IWineD3DDeviceImpl *device = This->resource.device;
974 for (i = 0; i < device->context_count; ++i)
976 context_surface_update(device->contexts[i], This);
981 /* This call just allocates the texture, the caller is responsible for binding
982 * the correct texture. */
983 /* Context activation is done by the caller. */
984 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
985 const struct wined3d_format *format, BOOL srgb)
987 BOOL enable_client_storage = FALSE;
988 GLsizei width = This->pow2Width;
989 GLsizei height = This->pow2Height;
990 const BYTE *mem = NULL;
995 internal = format->glGammaInternal;
997 else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET && surface_is_offscreen(This))
999 internal = format->rtInternal;
1003 internal = format->glInternal;
1006 if (format->heightscale != 1.0f && format->heightscale != 0.0f) height *= format->heightscale;
1008 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",
1009 This, This->texture_target, This->texture_level, debug_d3dformat(format->id),
1010 internal, width, height, format->glFormat, format->glType);
1014 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1016 if (This->flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_CONVERTED)
1017 || !This->resource.allocatedMemory)
1019 /* In some cases we want to disable client storage.
1020 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
1021 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
1022 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
1023 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
1025 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
1026 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1027 This->flags &= ~SFLAG_CLIENT;
1028 enable_client_storage = TRUE;
1032 This->flags |= SFLAG_CLIENT;
1034 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
1035 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
1037 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1041 if (format->flags & WINED3DFMT_FLAG_COMPRESSED && mem)
1043 GL_EXTCALL(glCompressedTexImage2DARB(This->texture_target, This->texture_level,
1044 internal, width, height, 0, This->resource.size, mem));
1045 checkGLcall("glCompressedTexImage2DARB");
1049 glTexImage2D(This->texture_target, This->texture_level,
1050 internal, width, height, 0, format->glFormat, format->glType, mem);
1051 checkGLcall("glTexImage2D");
1054 if(enable_client_storage) {
1055 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1056 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1061 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1062 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1063 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1064 /* GL locking is done by the caller */
1065 void surface_set_compatible_renderbuffer(IWineD3DSurfaceImpl *surface, IWineD3DSurfaceImpl *rt)
1067 const struct wined3d_gl_info *gl_info = &surface->resource.device->adapter->gl_info;
1068 renderbuffer_entry_t *entry;
1069 GLuint renderbuffer = 0;
1070 unsigned int src_width, src_height;
1071 unsigned int width, height;
1073 if (rt && rt->resource.format->id != WINED3DFMT_NULL)
1075 width = rt->pow2Width;
1076 height = rt->pow2Height;
1080 width = surface->pow2Width;
1081 height = surface->pow2Height;
1084 src_width = surface->pow2Width;
1085 src_height = surface->pow2Height;
1087 /* A depth stencil smaller than the render target is not valid */
1088 if (width > src_width || height > src_height) return;
1090 /* Remove any renderbuffer set if the sizes match */
1091 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT]
1092 || (width == src_width && height == src_height))
1094 surface->current_renderbuffer = NULL;
1098 /* Look if we've already got a renderbuffer of the correct dimensions */
1099 LIST_FOR_EACH_ENTRY(entry, &surface->renderbuffers, renderbuffer_entry_t, entry)
1101 if (entry->width == width && entry->height == height)
1103 renderbuffer = entry->id;
1104 surface->current_renderbuffer = entry;
1111 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1112 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1113 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER,
1114 surface->resource.format->glInternal, width, height);
1116 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
1117 entry->width = width;
1118 entry->height = height;
1119 entry->id = renderbuffer;
1120 list_add_head(&surface->renderbuffers, &entry->entry);
1122 surface->current_renderbuffer = entry;
1125 checkGLcall("set_compatible_renderbuffer");
1128 GLenum surface_get_gl_buffer(IWineD3DSurfaceImpl *surface)
1130 struct wined3d_swapchain *swapchain = surface->container.u.swapchain;
1132 TRACE("surface %p.\n", surface);
1134 if (surface->container.type != WINED3D_CONTAINER_SWAPCHAIN)
1136 ERR("Surface %p is not on a swapchain.\n", surface);
1140 if (swapchain->back_buffers && swapchain->back_buffers[0] == surface)
1142 if (swapchain->render_to_fbo)
1144 TRACE("Returning GL_COLOR_ATTACHMENT0\n");
1145 return GL_COLOR_ATTACHMENT0;
1147 TRACE("Returning GL_BACK\n");
1150 else if (surface == swapchain->front_buffer)
1152 TRACE("Returning GL_FRONT\n");
1156 FIXME("Higher back buffer, returning GL_BACK\n");
1160 /* Slightly inefficient way to handle multiple dirty rects but it works :) */
1161 void surface_add_dirty_rect(IWineD3DSurfaceImpl *surface, const WINED3DBOX *dirty_rect)
1163 TRACE("surface %p, dirty_rect %p.\n", surface, dirty_rect);
1165 if (!(surface->flags & SFLAG_INSYSMEM) && (surface->flags & SFLAG_INTEXTURE))
1166 /* No partial locking for textures yet. */
1167 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
1169 surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
1172 surface->dirtyRect.left = min(surface->dirtyRect.left, dirty_rect->Left);
1173 surface->dirtyRect.top = min(surface->dirtyRect.top, dirty_rect->Top);
1174 surface->dirtyRect.right = max(surface->dirtyRect.right, dirty_rect->Right);
1175 surface->dirtyRect.bottom = max(surface->dirtyRect.bottom, dirty_rect->Bottom);
1179 surface->dirtyRect.left = 0;
1180 surface->dirtyRect.top = 0;
1181 surface->dirtyRect.right = surface->resource.width;
1182 surface->dirtyRect.bottom = surface->resource.height;
1185 /* if the container is a texture then mark it dirty. */
1186 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1188 TRACE("Passing to container.\n");
1189 wined3d_texture_set_dirty(surface->container.u.texture, TRUE);
1193 static BOOL surface_convert_color_to_float(IWineD3DSurfaceImpl *surface, DWORD color, WINED3DCOLORVALUE *float_color)
1195 const struct wined3d_format *format = surface->resource.format;
1196 IWineD3DDeviceImpl *device = surface->resource.device;
1200 case WINED3DFMT_P8_UINT:
1201 if (surface->palette)
1203 float_color->r = surface->palette->palents[color].peRed / 255.0f;
1204 float_color->g = surface->palette->palents[color].peGreen / 255.0f;
1205 float_color->b = surface->palette->palents[color].peBlue / 255.0f;
1209 float_color->r = 0.0f;
1210 float_color->g = 0.0f;
1211 float_color->b = 0.0f;
1213 float_color->a = primary_render_target_is_p8(device) ? color / 255.0f : 1.0f;
1216 case WINED3DFMT_B5G6R5_UNORM:
1217 float_color->r = ((color >> 11) & 0x1f) / 31.0f;
1218 float_color->g = ((color >> 5) & 0x3f) / 63.0f;
1219 float_color->b = (color & 0x1f) / 31.0f;
1220 float_color->a = 1.0f;
1223 case WINED3DFMT_B8G8R8_UNORM:
1224 case WINED3DFMT_B8G8R8X8_UNORM:
1225 float_color->r = D3DCOLOR_R(color);
1226 float_color->g = D3DCOLOR_G(color);
1227 float_color->b = D3DCOLOR_B(color);
1228 float_color->a = 1.0f;
1231 case WINED3DFMT_B8G8R8A8_UNORM:
1232 float_color->r = D3DCOLOR_R(color);
1233 float_color->g = D3DCOLOR_G(color);
1234 float_color->b = D3DCOLOR_B(color);
1235 float_color->a = D3DCOLOR_A(color);
1239 ERR("Unhandled conversion from %s to floating point.\n", debug_d3dformat(format->id));
1246 static BOOL surface_convert_depth_to_float(IWineD3DSurfaceImpl *surface, DWORD depth, float *float_depth)
1248 const struct wined3d_format *format = surface->resource.format;
1252 case WINED3DFMT_S1_UINT_D15_UNORM:
1253 *float_depth = depth / (float)0x00007fff;
1256 case WINED3DFMT_D16_UNORM:
1257 *float_depth = depth / (float)0x0000ffff;
1260 case WINED3DFMT_D24_UNORM_S8_UINT:
1261 case WINED3DFMT_X8D24_UNORM:
1262 *float_depth = depth / (float)0x00ffffff;
1265 case WINED3DFMT_D32_UNORM:
1266 *float_depth = depth / (float)0xffffffff;
1270 ERR("Unhandled conversion from %s to floating point.\n", debug_d3dformat(format->id));
1277 static void surface_evict_sysmem(IWineD3DSurfaceImpl *surface)
1279 if (surface->flags & SFLAG_DONOTFREE)
1282 HeapFree(GetProcessHeap(), 0, surface->resource.heapMemory);
1283 surface->resource.allocatedMemory = NULL;
1284 surface->resource.heapMemory = NULL;
1285 surface_modify_location(surface, SFLAG_INSYSMEM, FALSE);
1288 HRESULT surface_load(IWineD3DSurfaceImpl *surface, BOOL srgb)
1290 DWORD flag = srgb ? SFLAG_INSRGBTEX : SFLAG_INTEXTURE;
1292 TRACE("surface %p, srgb %#x.\n", surface, srgb);
1294 if (surface->resource.pool == WINED3DPOOL_SCRATCH)
1296 ERR("Not supported on scratch surfaces.\n");
1297 return WINED3DERR_INVALIDCALL;
1300 if (!(surface->flags & flag))
1302 TRACE("Reloading because surface is dirty\n");
1304 /* Reload if either the texture and sysmem have different ideas about the
1305 * color key, or the actual key values changed. */
1306 else if (!(surface->flags & SFLAG_GLCKEY) != !(surface->CKeyFlags & WINEDDSD_CKSRCBLT)
1307 || ((surface->CKeyFlags & WINEDDSD_CKSRCBLT)
1308 && (surface->glCKey.dwColorSpaceLowValue != surface->SrcBltCKey.dwColorSpaceLowValue
1309 || surface->glCKey.dwColorSpaceHighValue != surface->SrcBltCKey.dwColorSpaceHighValue)))
1311 TRACE("Reloading because of color keying\n");
1312 /* To perform the color key conversion we need a sysmem copy of
1313 * the surface. Make sure we have it. */
1315 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
1316 /* Make sure the texture is reloaded because of the color key change,
1317 * this kills performance though :( */
1318 /* TODO: This is not necessarily needed with hw palettized texture support. */
1319 surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
1323 TRACE("surface is already in texture\n");
1327 /* No partial locking for textures yet. */
1328 surface_load_location(surface, flag, NULL);
1329 surface_evict_sysmem(surface);
1334 /* Do not call while under the GL lock. */
1335 static ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface)
1337 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
1340 TRACE("Surface %p, container %p of type %#x.\n",
1341 surface, surface->container.u.base, surface->container.type);
1343 switch (surface->container.type)
1345 case WINED3D_CONTAINER_TEXTURE:
1346 return wined3d_texture_decref(surface->container.u.texture);
1348 case WINED3D_CONTAINER_SWAPCHAIN:
1349 return wined3d_swapchain_decref(surface->container.u.swapchain);
1352 ERR("Unhandled container type %#x.\n", surface->container.type);
1353 case WINED3D_CONTAINER_NONE:
1357 refcount = InterlockedDecrement(&surface->resource.ref);
1358 TRACE("%p decreasing refcount to %u.\n", surface, refcount);
1362 surface_cleanup(surface);
1363 surface->resource.parent_ops->wined3d_object_destroyed(surface->resource.parent);
1365 TRACE("Destroyed surface %p.\n", surface);
1366 HeapFree(GetProcessHeap(), 0, surface);
1372 /* ****************************************************
1373 IWineD3DSurface IWineD3DResource parts follow
1374 **************************************************** */
1376 /* Do not call while under the GL lock. */
1377 void surface_internal_preload(IWineD3DSurfaceImpl *surface, enum WINED3DSRGB srgb)
1379 IWineD3DDeviceImpl *device = surface->resource.device;
1381 TRACE("iface %p, srgb %#x.\n", surface, srgb);
1383 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1385 struct wined3d_texture *texture = surface->container.u.texture;
1387 TRACE("Passing to container (%p).\n", texture);
1388 texture->texture_ops->texture_preload(texture, srgb);
1392 struct wined3d_context *context = NULL;
1394 TRACE("(%p) : About to load surface\n", surface);
1396 if (!device->isInDraw) context = context_acquire(device, NULL);
1398 if (surface->resource.format->id == WINED3DFMT_P8_UINT
1399 || surface->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
1401 if (palette9_changed(surface))
1403 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
1404 /* TODO: This is not necessarily needed with hw palettized texture support */
1405 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
1406 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
1407 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
1411 surface_load(surface, srgb == SRGB_SRGB ? TRUE : FALSE);
1413 if (surface->resource.pool == WINED3DPOOL_DEFAULT)
1415 /* Tell opengl to try and keep this texture in video ram (well mostly) */
1419 glPrioritizeTextures(1, &surface->texture_name, &tmp);
1423 if (context) context_release(context);
1427 static void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface)
1429 surface_internal_preload((IWineD3DSurfaceImpl *)iface, SRGB_ANY);
1432 BOOL surface_init_sysmem(IWineD3DSurfaceImpl *surface)
1434 if (!surface->resource.allocatedMemory)
1436 surface->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1437 surface->resource.size + RESOURCE_ALIGNMENT);
1438 if (!surface->resource.heapMemory)
1440 ERR("Out of memory\n");
1443 surface->resource.allocatedMemory =
1444 (BYTE *)(((ULONG_PTR)surface->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1448 memset(surface->resource.allocatedMemory, 0, surface->resource.size);
1451 surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
1456 /* ******************************************************
1457 IWineD3DSurface IWineD3DSurface parts follow
1458 ****************************************************** */
1460 /* Read the framebuffer back into the surface */
1461 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, const RECT *rect, void *dest, UINT pitch)
1463 IWineD3DDeviceImpl *device = This->resource.device;
1464 const struct wined3d_gl_info *gl_info;
1465 struct wined3d_context *context;
1469 BYTE *row, *top, *bottom;
1473 BOOL srcIsUpsideDown;
1478 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1479 static BOOL warned = FALSE;
1481 ERR("The application tries to lock the render target, but render target locking is disabled\n");
1487 context = context_acquire(device, This);
1488 context_apply_blit_state(context, device);
1489 gl_info = context->gl_info;
1493 /* Select the correct read buffer, and give some debug output.
1494 * There is no need to keep track of the current read buffer or reset it, every part of the code
1495 * that reads sets the read buffer as desired.
1497 if (surface_is_offscreen(This))
1499 /* Mapping the primary render target which is not on a swapchain.
1500 * Read from the back buffer. */
1501 TRACE("Mapping offscreen render target.\n");
1502 glReadBuffer(device->offscreenBuffer);
1503 srcIsUpsideDown = TRUE;
1507 /* Onscreen surfaces are always part of a swapchain */
1508 GLenum buffer = surface_get_gl_buffer(This);
1509 TRACE("Mapping %#x buffer.\n", buffer);
1510 glReadBuffer(buffer);
1511 checkGLcall("glReadBuffer");
1512 srcIsUpsideDown = FALSE;
1515 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
1517 local_rect.left = 0;
1519 local_rect.right = This->resource.width;
1520 local_rect.bottom = This->resource.height;
1526 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
1528 switch (This->resource.format->id)
1530 case WINED3DFMT_P8_UINT:
1532 if (primary_render_target_is_p8(device))
1534 /* In case of P8 render targets the index is stored in the alpha component */
1536 type = GL_UNSIGNED_BYTE;
1538 bpp = This->resource.format->byte_count;
1540 /* GL can't return palettized data, so read ARGB pixels into a
1541 * separate block of memory and convert them into palettized format
1542 * in software. Slow, but if the app means to use palettized render
1543 * targets and locks it...
1545 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
1546 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
1547 * for the color channels when palettizing the colors.
1550 type = GL_UNSIGNED_BYTE;
1552 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
1554 ERR("Out of memory\n");
1558 bpp = This->resource.format->byte_count * 3;
1565 fmt = This->resource.format->glFormat;
1566 type = This->resource.format->glType;
1567 bpp = This->resource.format->byte_count;
1570 if (This->flags & SFLAG_PBO)
1572 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
1573 checkGLcall("glBindBufferARB");
1576 ERR("mem not null for pbo -- unexpected\n");
1581 /* Save old pixel store pack state */
1582 glGetIntegerv(GL_PACK_ROW_LENGTH, &rowLen);
1583 checkGLcall("glGetIntegerv");
1584 glGetIntegerv(GL_PACK_SKIP_PIXELS, &skipPix);
1585 checkGLcall("glGetIntegerv");
1586 glGetIntegerv(GL_PACK_SKIP_ROWS, &skipRow);
1587 checkGLcall("glGetIntegerv");
1589 /* Setup pixel store pack state -- to glReadPixels into the correct place */
1590 glPixelStorei(GL_PACK_ROW_LENGTH, This->resource.width);
1591 checkGLcall("glPixelStorei");
1592 glPixelStorei(GL_PACK_SKIP_PIXELS, local_rect.left);
1593 checkGLcall("glPixelStorei");
1594 glPixelStorei(GL_PACK_SKIP_ROWS, local_rect.top);
1595 checkGLcall("glPixelStorei");
1597 glReadPixels(local_rect.left, !srcIsUpsideDown ? (This->resource.height - local_rect.bottom) : local_rect.top,
1598 local_rect.right - local_rect.left,
1599 local_rect.bottom - local_rect.top,
1601 checkGLcall("glReadPixels");
1603 /* Reset previous pixel store pack state */
1604 glPixelStorei(GL_PACK_ROW_LENGTH, rowLen);
1605 checkGLcall("glPixelStorei");
1606 glPixelStorei(GL_PACK_SKIP_PIXELS, skipPix);
1607 checkGLcall("glPixelStorei");
1608 glPixelStorei(GL_PACK_SKIP_ROWS, skipRow);
1609 checkGLcall("glPixelStorei");
1611 if (This->flags & SFLAG_PBO)
1613 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
1614 checkGLcall("glBindBufferARB");
1616 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
1617 * to get a pointer to it and perform the flipping in software. This is a lot
1618 * faster than calling glReadPixels for each line. In case we want more speed
1619 * we should rerender it flipped in a FBO and read the data back from the FBO. */
1620 if(!srcIsUpsideDown) {
1621 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1622 checkGLcall("glBindBufferARB");
1624 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1625 checkGLcall("glMapBufferARB");
1629 /* TODO: Merge this with the palettization loop below for P8 targets */
1630 if(!srcIsUpsideDown) {
1632 /* glReadPixels returns the image upside down, and there is no way to prevent this.
1633 Flip the lines in software */
1634 len = (local_rect.right - local_rect.left) * bpp;
1635 off = local_rect.left * bpp;
1637 row = HeapAlloc(GetProcessHeap(), 0, len);
1639 ERR("Out of memory\n");
1640 if (This->resource.format->id == WINED3DFMT_P8_UINT) HeapFree(GetProcessHeap(), 0, mem);
1645 top = mem + pitch * local_rect.top;
1646 bottom = mem + pitch * (local_rect.bottom - 1);
1647 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
1648 memcpy(row, top + off, len);
1649 memcpy(top + off, bottom + off, len);
1650 memcpy(bottom + off, row, len);
1654 HeapFree(GetProcessHeap(), 0, row);
1656 /* Unmap the temp PBO buffer */
1657 if (This->flags & SFLAG_PBO)
1659 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1660 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1665 context_release(context);
1667 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
1668 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
1669 * the same color but we have no choice.
1670 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
1672 if (This->resource.format->id == WINED3DFMT_P8_UINT && !primary_render_target_is_p8(device))
1674 const PALETTEENTRY *pal = NULL;
1675 DWORD width = pitch / 3;
1679 pal = This->palette->palents;
1681 ERR("Palette is missing, cannot perform inverse palette lookup\n");
1682 HeapFree(GetProcessHeap(), 0, mem);
1686 for(y = local_rect.top; y < local_rect.bottom; y++) {
1687 for(x = local_rect.left; x < local_rect.right; x++) {
1688 /* start lines pixels */
1689 const BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
1690 const BYTE *green = blue + 1;
1691 const BYTE *red = green + 1;
1693 for(c = 0; c < 256; c++) {
1694 if(*red == pal[c].peRed &&
1695 *green == pal[c].peGreen &&
1696 *blue == pal[c].peBlue)
1698 *((BYTE *) dest + y * width + x) = c;
1704 HeapFree(GetProcessHeap(), 0, mem);
1708 /* Read the framebuffer contents into a texture */
1709 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This, BOOL srgb)
1711 IWineD3DDeviceImpl *device = This->resource.device;
1712 const struct wined3d_gl_info *gl_info;
1713 struct wined3d_context *context;
1715 if (!surface_is_offscreen(This))
1717 /* We would need to flip onscreen surfaces, but there's no efficient
1718 * way to do that here. It makes more sense for the caller to
1719 * explicitly go through sysmem. */
1720 ERR("Not supported for onscreen targets.\n");
1724 /* Activate the surface to read from. In some situations it isn't the currently active target(e.g. backbuffer
1725 * locking during offscreen rendering). RESOURCELOAD is ok because glCopyTexSubImage2D isn't affected by any
1726 * states in the stateblock, and no driver was found yet that had bugs in that regard.
1728 context = context_acquire(device, This);
1729 gl_info = context->gl_info;
1731 surface_prepare_texture(This, gl_info, srgb);
1732 surface_bind_and_dirtify(This, gl_info, srgb);
1734 TRACE("Reading back offscreen render target %p.\n", This);
1738 glReadBuffer(device->offscreenBuffer);
1739 checkGLcall("glReadBuffer");
1741 glCopyTexSubImage2D(This->texture_target, This->texture_level,
1742 0, 0, 0, 0, This->resource.width, This->resource.height);
1743 checkGLcall("glCopyTexSubImage2D");
1747 context_release(context);
1750 /* Context activation is done by the caller. */
1751 static void surface_prepare_texture_internal(IWineD3DSurfaceImpl *surface,
1752 const struct wined3d_gl_info *gl_info, BOOL srgb)
1754 DWORD alloc_flag = srgb ? SFLAG_SRGBALLOCATED : SFLAG_ALLOCATED;
1755 CONVERT_TYPES convert;
1756 struct wined3d_format format;
1758 if (surface->flags & alloc_flag) return;
1760 d3dfmt_get_conv(surface, TRUE, TRUE, &format, &convert);
1761 if (convert != NO_CONVERSION || format.convert) surface->flags |= SFLAG_CONVERTED;
1762 else surface->flags &= ~SFLAG_CONVERTED;
1764 surface_bind_and_dirtify(surface, gl_info, srgb);
1765 surface_allocate_surface(surface, gl_info, &format, srgb);
1766 surface->flags |= alloc_flag;
1769 /* Context activation is done by the caller. */
1770 void surface_prepare_texture(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info, BOOL srgb)
1772 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1774 struct wined3d_texture *texture = surface->container.u.texture;
1775 UINT sub_count = texture->level_count * texture->layer_count;
1778 TRACE("surface %p is a subresource of texture %p.\n", surface, texture);
1780 for (i = 0; i < sub_count; ++i)
1782 IWineD3DSurfaceImpl *s = surface_from_resource(texture->sub_resources[i]);
1783 surface_prepare_texture_internal(s, gl_info, srgb);
1789 surface_prepare_texture_internal(surface, gl_info, srgb);
1792 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This)
1794 IWineD3DDeviceImpl *device = This->resource.device;
1795 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1797 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
1798 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
1801 if (!(This->flags & SFLAG_DYNLOCK))
1804 /* MAXLOCKCOUNT is defined in wined3d_private.h */
1805 if(This->lockCount > MAXLOCKCOUNT) {
1806 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
1807 This->flags |= SFLAG_DYNLOCK;
1811 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
1812 * Also don't create a PBO for systemmem surfaces.
1814 if (gl_info->supported[ARB_PIXEL_BUFFER_OBJECT] && (This->flags & SFLAG_DYNLOCK)
1815 && !(This->flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2))
1816 && (This->resource.pool != WINED3DPOOL_SYSTEMMEM))
1819 struct wined3d_context *context;
1821 context = context_acquire(device, NULL);
1824 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
1825 error = glGetError();
1826 if (!This->pbo || error != GL_NO_ERROR)
1827 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
1829 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
1831 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1832 checkGLcall("glBindBufferARB");
1834 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
1835 checkGLcall("glBufferDataARB");
1837 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1838 checkGLcall("glBindBufferARB");
1840 /* We don't need the system memory anymore and we can't even use it for PBOs */
1841 if (!(This->flags & SFLAG_CLIENT))
1843 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
1844 This->resource.heapMemory = NULL;
1846 This->resource.allocatedMemory = NULL;
1847 This->flags |= SFLAG_PBO;
1849 context_release(context);
1851 else if (!(This->resource.allocatedMemory || This->flags & SFLAG_PBO))
1853 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
1856 if(!This->resource.heapMemory) {
1857 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1859 This->resource.allocatedMemory =
1860 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1861 if (This->flags & SFLAG_INSYSMEM)
1863 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1868 static HRESULT WINAPI IWineD3DSurfaceImpl_Map(IWineD3DSurface *iface,
1869 WINED3DLOCKED_RECT *pLockedRect, const RECT *pRect, DWORD flags)
1871 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1872 IWineD3DDeviceImpl *device = This->resource.device;
1873 const RECT *pass_rect = pRect;
1875 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
1876 iface, pLockedRect, wine_dbgstr_rect(pRect), flags);
1878 /* This is also done in the base class, but we have to verify this before loading any data from
1879 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1880 * may interfere, and all other bad things may happen
1882 if (This->flags & SFLAG_LOCKED)
1884 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1885 return WINED3DERR_INVALIDCALL;
1887 This->flags |= SFLAG_LOCKED;
1889 if (!(This->flags & SFLAG_LOCKABLE))
1891 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
1894 if (flags & WINED3DLOCK_DISCARD)
1896 TRACE("WINED3DLOCK_DISCARD flag passed, marking SYSMEM as up to date.\n");
1897 surface_prepare_system_memory(This);
1898 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
1902 /* surface_load_location() does not check if the rectangle specifies
1903 * the full surface. Most callers don't need that, so do it here. */
1904 if (pRect && !pRect->top && !pRect->left
1905 && pRect->right == This->resource.width
1906 && pRect->bottom == This->resource.height)
1911 if (!(wined3d_settings.rendertargetlock_mode == RTL_DISABLE
1912 && ((This->container.type == WINED3D_CONTAINER_SWAPCHAIN) || This == device->render_targets[0])))
1914 surface_load_location(This, SFLAG_INSYSMEM, pass_rect);
1918 if (This->flags & SFLAG_PBO)
1920 const struct wined3d_gl_info *gl_info;
1921 struct wined3d_context *context;
1923 context = context_acquire(device, NULL);
1924 gl_info = context->gl_info;
1927 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1928 checkGLcall("glBindBufferARB");
1930 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1931 if(This->resource.allocatedMemory) {
1932 ERR("The surface already has PBO memory allocated!\n");
1935 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1936 checkGLcall("glMapBufferARB");
1938 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1939 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1940 checkGLcall("glBindBufferARB");
1943 context_release(context);
1946 if (!(flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)))
1949 surface_add_dirty_rect(This, NULL);
1954 b.Left = pRect->left;
1956 b.Right = pRect->right;
1957 b.Bottom = pRect->bottom;
1960 surface_add_dirty_rect(This, &b);
1964 return IWineD3DBaseSurfaceImpl_Map(iface, pLockedRect, pRect, flags);
1967 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *surface,
1968 const RECT *rect, GLenum fmt, GLenum type, UINT bpp, const BYTE *mem)
1970 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *)surface); /* target is argb, 4 byte */
1971 IWineD3DDeviceImpl *device = surface->resource.device;
1972 const struct wined3d_gl_info *gl_info;
1973 struct wined3d_context *context;
1977 surface_get_rect(surface, rect, &local_rect);
1979 mem += local_rect.top * pitch + local_rect.left * bpp;
1980 w = local_rect.right - local_rect.left;
1981 h = local_rect.bottom - local_rect.top;
1983 /* Activate the correct context for the render target */
1984 context = context_acquire(device, surface);
1985 context_apply_blit_state(context, device);
1986 gl_info = context->gl_info;
1990 if (!surface_is_offscreen(surface))
1992 GLenum buffer = surface_get_gl_buffer(surface);
1993 TRACE("Unlocking %#x buffer.\n", buffer);
1994 context_set_draw_buffer(context, buffer);
1996 surface_translate_drawable_coords(surface, context->win_handle, &local_rect);
1997 glPixelZoom(1.0f, -1.0f);
2001 /* Primary offscreen render target */
2002 TRACE("Offscreen render target.\n");
2003 context_set_draw_buffer(context, device->offscreenBuffer);
2005 glPixelZoom(1.0f, 1.0f);
2008 glRasterPos3i(local_rect.left, local_rect.top, 1);
2009 checkGLcall("glRasterPos3i");
2011 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
2012 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->resource.width);
2014 if (surface->flags & SFLAG_PBO)
2016 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, surface->pbo));
2017 checkGLcall("glBindBufferARB");
2020 glDrawPixels(w, h, fmt, type, mem);
2021 checkGLcall("glDrawPixels");
2023 if (surface->flags & SFLAG_PBO)
2025 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
2026 checkGLcall("glBindBufferARB");
2029 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2030 checkGLcall("glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)");
2034 if (wined3d_settings.strict_draw_ordering
2035 || (surface->container.type == WINED3D_CONTAINER_SWAPCHAIN
2036 && surface->container.u.swapchain->front_buffer == surface))
2039 context_release(context);
2042 static HRESULT WINAPI IWineD3DSurfaceImpl_Unmap(IWineD3DSurface *iface)
2044 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2045 IWineD3DDeviceImpl *device = This->resource.device;
2048 if (!(This->flags & SFLAG_LOCKED))
2050 WARN("trying to Unlock an unlocked surf@%p\n", This);
2051 return WINEDDERR_NOTLOCKED;
2054 This->flags &= ~SFLAG_LOCKED;
2055 memset(&This->lockedRect, 0, sizeof(This->lockedRect));
2057 if (This->flags & SFLAG_PBO)
2059 const struct wined3d_gl_info *gl_info;
2060 struct wined3d_context *context;
2062 TRACE("Freeing PBO memory\n");
2064 context = context_acquire(device, NULL);
2065 gl_info = context->gl_info;
2068 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
2069 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
2070 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
2071 checkGLcall("glUnmapBufferARB");
2073 context_release(context);
2075 This->resource.allocatedMemory = NULL;
2078 TRACE("(%p) : dirtyfied(%d)\n", This, This->flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
2080 if (This->flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE))
2082 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
2086 if (This->container.type == WINED3D_CONTAINER_SWAPCHAIN
2087 || (device->render_targets && This == device->render_targets[0]))
2089 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
2090 static BOOL warned = FALSE;
2092 ERR("The application tries to write to the render target, but render target locking is disabled\n");
2098 if (!This->dirtyRect.left && !This->dirtyRect.top
2099 && This->dirtyRect.right == This->resource.width
2100 && This->dirtyRect.bottom == This->resource.height)
2104 /* TODO: Proper partial rectangle tracking */
2105 fullsurface = FALSE;
2106 This->flags |= SFLAG_INSYSMEM;
2109 surface_load_location(This, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
2111 /* Partial rectangle tracking is not commonly implemented, it is only
2112 * done for render targets. INSYSMEM was set before to tell
2113 * surface_load_location() where to read the rectangle from.
2114 * Indrawable is set because all modifications from the partial
2115 * sysmem copy are written back to the drawable, thus the surface is
2116 * merged again in the drawable. The sysmem copy is not fully up to
2117 * date because only a subrectangle was read in Map(). */
2120 surface_modify_location(This, SFLAG_INDRAWABLE, TRUE);
2121 surface_evict_sysmem(This);
2124 This->dirtyRect.left = This->resource.width;
2125 This->dirtyRect.top = This->resource.height;
2126 This->dirtyRect.right = 0;
2127 This->dirtyRect.bottom = 0;
2129 else if (This->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
2131 FIXME("Depth Stencil buffer locking is not implemented\n");
2135 /* Overlays have to be redrawn manually after changes with the GL implementation */
2136 if (This->overlay_dest)
2137 This->surface_ops->surface_draw_overlay(This);
2142 static void surface_release_client_storage(IWineD3DSurfaceImpl *surface)
2144 struct wined3d_context *context;
2146 context = context_acquire(surface->resource.device, NULL);
2149 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
2150 if (surface->texture_name)
2152 surface_bind_and_dirtify(surface, context->gl_info, FALSE);
2153 glTexImage2D(surface->texture_target, surface->texture_level,
2154 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
2156 if (surface->texture_name_srgb)
2158 surface_bind_and_dirtify(surface, context->gl_info, TRUE);
2159 glTexImage2D(surface->texture_target, surface->texture_level,
2160 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
2162 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
2165 context_release(context);
2167 surface_modify_location(surface, SFLAG_INSRGBTEX, FALSE);
2168 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
2169 surface_force_reload(surface);
2172 static HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC)
2174 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2175 WINED3DLOCKED_RECT lock;
2179 TRACE("(%p)->(%p)\n",This,pHDC);
2181 if (This->flags & SFLAG_USERPTR)
2183 ERR("Not supported on surfaces with an application-provided surfaces\n");
2184 return WINEDDERR_NODC;
2187 /* Give more detailed info for ddraw */
2188 if (This->flags & SFLAG_DCINUSE)
2189 return WINEDDERR_DCALREADYCREATED;
2191 /* Can't GetDC if the surface is locked */
2192 if (This->flags & SFLAG_LOCKED)
2193 return WINED3DERR_INVALIDCALL;
2195 memset(&lock, 0, sizeof(lock)); /* To be sure */
2197 /* Create a DIB section if there isn't a hdc yet */
2200 if (This->flags & SFLAG_CLIENT)
2202 surface_load_location(This, SFLAG_INSYSMEM, NULL);
2203 surface_release_client_storage(This);
2205 hr = IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
2206 if(FAILED(hr)) return WINED3DERR_INVALIDCALL;
2208 /* Use the dib section from now on if we are not using a PBO */
2209 if (!(This->flags & SFLAG_PBO))
2210 This->resource.allocatedMemory = This->dib.bitmap_data;
2213 /* Map the surface */
2214 hr = IWineD3DSurface_Map(iface, &lock, NULL, 0);
2216 /* Sync the DIB with the PBO. This can't be done earlier because Map()
2217 * activates the allocatedMemory. */
2218 if (This->flags & SFLAG_PBO)
2219 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
2223 ERR("IWineD3DSurface_Map failed, hr %#x.\n", hr);
2224 /* keep the dib section */
2228 if (This->resource.format->id == WINED3DFMT_P8_UINT
2229 || This->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
2231 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
2232 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
2234 const PALETTEENTRY *pal = NULL;
2237 pal = This->palette->palents;
2239 IWineD3DSurfaceImpl *dds_primary;
2240 struct wined3d_swapchain *swapchain;
2241 swapchain = This->resource.device->swapchains[0];
2242 dds_primary = swapchain->front_buffer;
2243 if (dds_primary && dds_primary->palette)
2244 pal = dds_primary->palette->palents;
2248 for (n=0; n<256; n++) {
2249 col[n].rgbRed = pal[n].peRed;
2250 col[n].rgbGreen = pal[n].peGreen;
2251 col[n].rgbBlue = pal[n].peBlue;
2252 col[n].rgbReserved = 0;
2254 SetDIBColorTable(This->hDC, 0, 256, col);
2259 TRACE("returning %p\n",*pHDC);
2260 This->flags |= SFLAG_DCINUSE;
2265 static HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC)
2267 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2269 TRACE("(%p)->(%p)\n",This,hDC);
2271 if (!(This->flags & SFLAG_DCINUSE))
2272 return WINEDDERR_NODC;
2274 if (This->hDC !=hDC) {
2275 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
2276 return WINEDDERR_NODC;
2279 if ((This->flags & SFLAG_PBO) && This->resource.allocatedMemory)
2281 /* Copy the contents of the DIB over to the PBO */
2282 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
2285 /* we locked first, so unlock now */
2286 IWineD3DSurface_Unmap(iface);
2288 This->flags &= ~SFLAG_DCINUSE;
2293 /* ******************************************************
2294 IWineD3DSurface Internal (No mapping to directx api) parts follow
2295 ****************************************************** */
2297 HRESULT d3dfmt_get_conv(IWineD3DSurfaceImpl *This, BOOL need_alpha_ck,
2298 BOOL use_texturing, struct wined3d_format *format, CONVERT_TYPES *convert)
2300 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
2301 IWineD3DDeviceImpl *device = This->resource.device;
2302 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2303 BOOL blit_supported = FALSE;
2305 /* Copy the default values from the surface. Below we might perform fixups */
2306 /* TODO: get rid of color keying desc fixups by using e.g. a table. */
2307 *format = *This->resource.format;
2308 *convert = NO_CONVERSION;
2310 /* Ok, now look if we have to do any conversion */
2311 switch (This->resource.format->id)
2313 case WINED3DFMT_P8_UINT:
2314 /* Below the call to blit_supported is disabled for Wine 1.2
2315 * because the function isn't operating correctly yet. At the
2316 * moment 8-bit blits are handled in software and if certain GL
2317 * extensions are around, surface conversion is performed at
2318 * upload time. The blit_supported call recognizes it as a
2319 * destination fixup. This type of upload 'fixup' and 8-bit to
2320 * 8-bit blits need to be handled by the blit_shader.
2321 * TODO: get rid of this #if 0. */
2323 blit_supported = device->blitter->blit_supported(&device->adapter->gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
2324 &rect, This->resource.usage, This->resource.pool, This->resource.format,
2325 &rect, This->resource.usage, This->resource.pool, This->resource.format);
2327 blit_supported = gl_info->supported[EXT_PALETTED_TEXTURE] || gl_info->supported[ARB_FRAGMENT_PROGRAM];
2329 /* Use conversion when the blit_shader backend supports it. It only supports this in case of
2330 * texturing. Further also use conversion in case of color keying.
2331 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
2332 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
2333 * conflicts with this.
2335 if (!((blit_supported && device->render_targets && This == device->render_targets[0]))
2336 || colorkey_active || !use_texturing)
2338 format->glFormat = GL_RGBA;
2339 format->glInternal = GL_RGBA;
2340 format->glType = GL_UNSIGNED_BYTE;
2341 format->conv_byte_count = 4;
2342 if (colorkey_active)
2343 *convert = CONVERT_PALETTED_CK;
2345 *convert = CONVERT_PALETTED;
2349 case WINED3DFMT_B2G3R3_UNORM:
2350 /* **********************
2351 GL_UNSIGNED_BYTE_3_3_2
2352 ********************** */
2353 if (colorkey_active) {
2354 /* This texture format will never be used.. So do not care about color keying
2355 up until the point in time it will be needed :-) */
2356 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
2360 case WINED3DFMT_B5G6R5_UNORM:
2361 if (colorkey_active)
2363 *convert = CONVERT_CK_565;
2364 format->glFormat = GL_RGBA;
2365 format->glInternal = GL_RGB5_A1;
2366 format->glType = GL_UNSIGNED_SHORT_5_5_5_1;
2367 format->conv_byte_count = 2;
2371 case WINED3DFMT_B5G5R5X1_UNORM:
2372 if (colorkey_active)
2374 *convert = CONVERT_CK_5551;
2375 format->glFormat = GL_BGRA;
2376 format->glInternal = GL_RGB5_A1;
2377 format->glType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
2378 format->conv_byte_count = 2;
2382 case WINED3DFMT_B8G8R8_UNORM:
2383 if (colorkey_active)
2385 *convert = CONVERT_CK_RGB24;
2386 format->glFormat = GL_RGBA;
2387 format->glInternal = GL_RGBA8;
2388 format->glType = GL_UNSIGNED_INT_8_8_8_8;
2389 format->conv_byte_count = 4;
2393 case WINED3DFMT_B8G8R8X8_UNORM:
2394 if (colorkey_active)
2396 *convert = CONVERT_RGB32_888;
2397 format->glFormat = GL_RGBA;
2398 format->glInternal = GL_RGBA8;
2399 format->glType = GL_UNSIGNED_INT_8_8_8_8;
2400 format->conv_byte_count = 4;
2411 void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey)
2413 IWineD3DDeviceImpl *device = This->resource.device;
2414 struct wined3d_palette *pal = This->palette;
2415 BOOL index_in_alpha = FALSE;
2418 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2419 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2420 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2421 * duplicate entries. Store the color key in the unused alpha component to speed the
2422 * download up and to make conversion unneeded. */
2423 index_in_alpha = primary_render_target_is_p8(device);
2427 UINT dxVersion = device->wined3d->dxVersion;
2429 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2432 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2435 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2436 * there's no palette at this time. */
2437 for (i = 0; i < 256; i++) table[i][3] = i;
2442 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2443 * alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2444 * capability flag is present (wine does advertise this capability) */
2445 for (i = 0; i < 256; ++i)
2447 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2448 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2449 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2450 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2456 TRACE("Using surface palette %p\n", pal);
2457 /* Get the surface's palette */
2458 for (i = 0; i < 256; ++i)
2460 table[i][0] = pal->palents[i].peRed;
2461 table[i][1] = pal->palents[i].peGreen;
2462 table[i][2] = pal->palents[i].peBlue;
2464 /* When index_in_alpha is set the palette index is stored in the
2465 * alpha component. In case of a readback we can then read
2466 * GL_ALPHA. Color keying is handled in BltOverride using a
2467 * GL_ALPHA_TEST using GL_NOT_EQUAL. In case of index_in_alpha the
2468 * color key itself is passed to glAlphaFunc in other cases the
2469 * alpha component of pixels that should be masked away is set to 0. */
2474 else if (colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue)
2475 && (i <= This->SrcBltCKey.dwColorSpaceHighValue))
2479 else if (pal->flags & WINEDDPCAPS_ALPHA)
2481 table[i][3] = pal->palents[i].peFlags;
2491 static HRESULT d3dfmt_convert_surface(const BYTE *src, BYTE *dst, UINT pitch, UINT width,
2492 UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This)
2496 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
2501 memcpy(dst, src, pitch * height);
2504 case CONVERT_PALETTED:
2505 case CONVERT_PALETTED_CK:
2510 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2512 for (y = 0; y < height; y++)
2514 source = src + pitch * y;
2515 dest = dst + outpitch * y;
2516 /* This is an 1 bpp format, using the width here is fine */
2517 for (x = 0; x < width; x++) {
2518 BYTE color = *source++;
2519 *dest++ = table[color][0];
2520 *dest++ = table[color][1];
2521 *dest++ = table[color][2];
2522 *dest++ = table[color][3];
2528 case CONVERT_CK_565:
2530 /* Converting the 565 format in 5551 packed to emulate color-keying.
2532 Note : in all these conversion, it would be best to average the averaging
2533 pixels to get the color of the pixel that will be color-keyed to
2534 prevent 'color bleeding'. This will be done later on if ever it is
2537 Note2: Nvidia documents say that their driver does not support alpha + color keying
2538 on the same surface and disables color keying in such a case
2544 TRACE("Color keyed 565\n");
2546 for (y = 0; y < height; y++) {
2547 Source = (const WORD *)(src + y * pitch);
2548 Dest = (WORD *) (dst + y * outpitch);
2549 for (x = 0; x < width; x++ ) {
2550 WORD color = *Source++;
2551 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
2552 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2553 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2562 case CONVERT_CK_5551:
2564 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
2568 TRACE("Color keyed 5551\n");
2569 for (y = 0; y < height; y++) {
2570 Source = (const WORD *)(src + y * pitch);
2571 Dest = (WORD *) (dst + y * outpitch);
2572 for (x = 0; x < width; x++ ) {
2573 WORD color = *Source++;
2575 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2576 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2580 *Dest &= ~(1 << 15);
2588 case CONVERT_CK_RGB24:
2590 /* Converting R8G8B8 format to R8G8B8A8 with color-keying. */
2592 for (y = 0; y < height; y++)
2594 source = src + pitch * y;
2595 dest = dst + outpitch * y;
2596 for (x = 0; x < width; x++) {
2597 DWORD color = ((DWORD)source[0] << 16) + ((DWORD)source[1] << 8) + (DWORD)source[2] ;
2598 DWORD dstcolor = color << 8;
2599 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2600 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2603 *(DWORD*)dest = dstcolor;
2611 case CONVERT_RGB32_888:
2613 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
2615 for (y = 0; y < height; y++)
2617 source = src + pitch * y;
2618 dest = dst + outpitch * y;
2619 for (x = 0; x < width; x++) {
2620 DWORD color = 0xffffff & *(const DWORD*)source;
2621 DWORD dstcolor = color << 8;
2622 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2623 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2626 *(DWORD*)dest = dstcolor;
2635 ERR("Unsupported conversion type %#x.\n", convert);
2640 BOOL palette9_changed(IWineD3DSurfaceImpl *This)
2642 IWineD3DDeviceImpl *device = This->resource.device;
2644 if (This->palette || (This->resource.format->id != WINED3DFMT_P8_UINT
2645 && This->resource.format->id != WINED3DFMT_P8_UINT_A8_UNORM))
2647 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2648 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2655 if (!memcmp(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256))
2660 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2662 memcpy(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2666 static HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, enum wined3d_format_id format)
2668 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2671 TRACE("(%p) : Calling base function first\n", This);
2672 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2675 This->flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
2676 TRACE("(%p) : glFormat %d, glFormatInternal %d, glType %d\n", This, This->resource.format->glFormat,
2677 This->resource.format->glInternal, This->resource.format->glType);
2682 static HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2683 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2685 TRACE("iface %p, mem %p.\n", iface, Mem);
2687 if (This->flags & (SFLAG_LOCKED | SFLAG_DCINUSE))
2689 WARN("Surface is locked or the HDC is in use\n");
2690 return WINED3DERR_INVALIDCALL;
2693 if(Mem && Mem != This->resource.allocatedMemory) {
2694 void *release = NULL;
2696 /* Do I have to copy the old surface content? */
2697 if (This->flags & SFLAG_DIBSECTION)
2699 SelectObject(This->hDC, This->dib.holdbitmap);
2700 DeleteDC(This->hDC);
2701 /* Release the DIB section */
2702 DeleteObject(This->dib.DIBsection);
2703 This->dib.bitmap_data = NULL;
2704 This->resource.allocatedMemory = NULL;
2706 This->flags &= ~SFLAG_DIBSECTION;
2708 else if (!(This->flags & SFLAG_USERPTR))
2710 release = This->resource.heapMemory;
2711 This->resource.heapMemory = NULL;
2713 This->resource.allocatedMemory = Mem;
2714 This->flags |= SFLAG_USERPTR;
2716 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2717 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
2719 /* For client textures opengl has to be notified */
2720 if (This->flags & SFLAG_CLIENT)
2721 surface_release_client_storage(This);
2723 /* Now free the old memory if any */
2724 HeapFree(GetProcessHeap(), 0, release);
2726 else if (This->flags & SFLAG_USERPTR)
2728 /* Map and GetDC will re-create the dib section and allocated memory. */
2729 This->resource.allocatedMemory = NULL;
2730 /* HeapMemory should be NULL already */
2731 if (This->resource.heapMemory)
2732 ERR("User pointer surface has heap memory allocated.\n");
2733 This->flags &= ~(SFLAG_USERPTR | SFLAG_INSYSMEM);
2735 if (This->flags & SFLAG_CLIENT)
2736 surface_release_client_storage(This);
2738 surface_prepare_system_memory(This);
2739 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
2744 void flip_surface(IWineD3DSurfaceImpl *front, IWineD3DSurfaceImpl *back) {
2746 /* Flip the surface contents */
2751 front->hDC = back->hDC;
2755 /* Flip the DIBsection */
2758 BOOL hasDib = front->flags & SFLAG_DIBSECTION;
2759 tmp = front->dib.DIBsection;
2760 front->dib.DIBsection = back->dib.DIBsection;
2761 back->dib.DIBsection = tmp;
2763 if (back->flags & SFLAG_DIBSECTION) front->flags |= SFLAG_DIBSECTION;
2764 else front->flags &= ~SFLAG_DIBSECTION;
2765 if (hasDib) back->flags |= SFLAG_DIBSECTION;
2766 else back->flags &= ~SFLAG_DIBSECTION;
2769 /* Flip the surface data */
2773 tmp = front->dib.bitmap_data;
2774 front->dib.bitmap_data = back->dib.bitmap_data;
2775 back->dib.bitmap_data = tmp;
2777 tmp = front->resource.allocatedMemory;
2778 front->resource.allocatedMemory = back->resource.allocatedMemory;
2779 back->resource.allocatedMemory = tmp;
2781 tmp = front->resource.heapMemory;
2782 front->resource.heapMemory = back->resource.heapMemory;
2783 back->resource.heapMemory = tmp;
2788 GLuint tmp_pbo = front->pbo;
2789 front->pbo = back->pbo;
2790 back->pbo = tmp_pbo;
2793 /* client_memory should not be different, but just in case */
2796 tmp = front->dib.client_memory;
2797 front->dib.client_memory = back->dib.client_memory;
2798 back->dib.client_memory = tmp;
2801 /* Flip the opengl texture */
2805 tmp = back->texture_name;
2806 back->texture_name = front->texture_name;
2807 front->texture_name = tmp;
2809 tmp = back->texture_name_srgb;
2810 back->texture_name_srgb = front->texture_name_srgb;
2811 front->texture_name_srgb = tmp;
2813 resource_unload(&back->resource);
2814 resource_unload(&front->resource);
2818 DWORD tmp_flags = back->flags;
2819 back->flags = front->flags;
2820 front->flags = tmp_flags;
2824 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD flags)
2826 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2827 struct wined3d_swapchain *swapchain = NULL;
2829 TRACE("iface %p, override %p, flags %#x.\n", iface, override, flags);
2831 /* Flipping is only supported on RenderTargets and overlays*/
2832 if( !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_OVERLAY)) ) {
2833 WARN("Tried to flip a non-render target, non-overlay surface\n");
2834 return WINEDDERR_NOTFLIPPABLE;
2837 if(This->resource.usage & WINED3DUSAGE_OVERLAY) {
2838 flip_surface(This, (IWineD3DSurfaceImpl *) override);
2840 /* Update the overlay if it is visible */
2841 if (This->overlay_dest)
2842 return This->surface_ops->surface_draw_overlay(This);
2848 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2849 * FIXME("(%p) Target override is not supported by now\n", This);
2850 * Additionally, it isn't really possible to support triple-buffering
2851 * properly on opengl at all
2855 if (This->container.type != WINED3D_CONTAINER_SWAPCHAIN)
2857 ERR("Flipped surface is not on a swapchain\n");
2858 return WINEDDERR_NOTFLIPPABLE;
2860 swapchain = This->container.u.swapchain;
2862 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2863 * and only d3d8 and d3d9 apps specify the presentation interval
2865 if (!(flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)))
2866 /* Most common case first to avoid wasting time on all the other cases */
2867 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2868 else if (flags & WINEDDFLIP_NOVSYNC)
2869 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2870 else if (flags & WINEDDFLIP_INTERVAL2)
2871 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2872 else if (flags & WINEDDFLIP_INTERVAL3)
2873 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2875 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2877 /* Flipping a OpenGL surface -> present. */
2878 return wined3d_swapchain_present(swapchain, NULL, NULL, swapchain->win_handle, NULL, 0);
2881 /* Does a direct frame buffer -> texture copy. Stretching is done
2882 * with single pixel copy calls
2884 static void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *dst_surface, IWineD3DSurfaceImpl *src_surface,
2885 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
2887 IWineD3DDeviceImpl *device = dst_surface->resource.device;
2890 struct wined3d_context *context;
2891 BOOL upsidedown = FALSE;
2892 RECT dst_rect = *dst_rect_in;
2894 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2895 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2897 if(dst_rect.top > dst_rect.bottom) {
2898 UINT tmp = dst_rect.bottom;
2899 dst_rect.bottom = dst_rect.top;
2904 context = context_acquire(device, src_surface);
2905 context_apply_blit_state(context, device);
2906 surface_internal_preload(dst_surface, SRGB_RGB);
2909 /* Bind the target texture */
2910 glBindTexture(dst_surface->texture_target, dst_surface->texture_name);
2911 checkGLcall("glBindTexture");
2912 if (surface_is_offscreen(src_surface))
2914 TRACE("Reading from an offscreen target\n");
2915 upsidedown = !upsidedown;
2916 glReadBuffer(device->offscreenBuffer);
2920 glReadBuffer(surface_get_gl_buffer(src_surface));
2922 checkGLcall("glReadBuffer");
2924 xrel = (float) (src_rect->right - src_rect->left) / (float) (dst_rect.right - dst_rect.left);
2925 yrel = (float) (src_rect->bottom - src_rect->top) / (float) (dst_rect.bottom - dst_rect.top);
2927 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2929 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2931 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2932 ERR("Texture filtering not supported in direct blit\n");
2935 else if ((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT)
2936 && ((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
2938 ERR("Texture filtering not supported in direct blit\n");
2942 && !((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2943 && !((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
2945 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2947 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2948 dst_rect.left /*xoffset */, dst_rect.top /* y offset */,
2949 src_rect->left, src_surface->resource.height - src_rect->bottom,
2950 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
2954 UINT yoffset = src_surface->resource.height - src_rect->top + dst_rect.top - 1;
2955 /* I have to process this row by row to swap the image,
2956 * otherwise it would be upside down, so stretching in y direction
2957 * doesn't cost extra time
2959 * However, stretching in x direction can be avoided if not necessary
2961 for(row = dst_rect.top; row < dst_rect.bottom; row++) {
2962 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2964 /* Well, that stuff works, but it's very slow.
2965 * find a better way instead
2969 for (col = dst_rect.left; col < dst_rect.right; ++col)
2971 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2972 dst_rect.left + col /* x offset */, row /* y offset */,
2973 src_rect->left + col * xrel, yoffset - (int) (row * yrel), 1, 1);
2978 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2979 dst_rect.left /* x offset */, row /* y offset */,
2980 src_rect->left, yoffset - (int) (row * yrel), dst_rect.right - dst_rect.left, 1);
2984 checkGLcall("glCopyTexSubImage2D");
2987 context_release(context);
2989 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
2990 * path is never entered
2992 surface_modify_location(dst_surface, SFLAG_INTEXTURE, TRUE);
2995 /* Uses the hardware to stretch and flip the image */
2996 static void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *dst_surface, IWineD3DSurfaceImpl *src_surface,
2997 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
2999 IWineD3DDeviceImpl *device = dst_surface->resource.device;
3000 struct wined3d_swapchain *src_swapchain = NULL;
3001 GLuint src, backup = 0;
3002 float left, right, top, bottom; /* Texture coordinates */
3003 UINT fbwidth = src_surface->resource.width;
3004 UINT fbheight = src_surface->resource.height;
3005 struct wined3d_context *context;
3006 GLenum drawBuffer = GL_BACK;
3007 GLenum texture_target;
3008 BOOL noBackBufferBackup;
3010 BOOL upsidedown = FALSE;
3011 RECT dst_rect = *dst_rect_in;
3013 TRACE("Using hwstretch blit\n");
3014 /* Activate the Proper context for reading from the source surface, set it up for blitting */
3015 context = context_acquire(device, src_surface);
3016 context_apply_blit_state(context, device);
3017 surface_internal_preload(dst_surface, SRGB_RGB);
3019 src_offscreen = surface_is_offscreen(src_surface);
3020 noBackBufferBackup = src_offscreen && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
3021 if (!noBackBufferBackup && !src_surface->texture_name)
3023 /* Get it a description */
3024 surface_internal_preload(src_surface, SRGB_RGB);
3028 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
3029 * This way we don't have to wait for the 2nd readback to finish to leave this function.
3031 if (context->aux_buffers >= 2)
3033 /* Got more than one aux buffer? Use the 2nd aux buffer */
3034 drawBuffer = GL_AUX1;
3036 else if ((!src_offscreen || device->offscreenBuffer == GL_BACK) && context->aux_buffers >= 1)
3038 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
3039 drawBuffer = GL_AUX0;
3042 if(noBackBufferBackup) {
3043 glGenTextures(1, &backup);
3044 checkGLcall("glGenTextures");
3045 glBindTexture(GL_TEXTURE_2D, backup);
3046 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3047 texture_target = GL_TEXTURE_2D;
3049 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
3050 * we are reading from the back buffer, the backup can be used as source texture
3052 texture_target = src_surface->texture_target;
3053 glBindTexture(texture_target, src_surface->texture_name);
3054 checkGLcall("glBindTexture(texture_target, src_surface->texture_name)");
3055 glEnable(texture_target);
3056 checkGLcall("glEnable(texture_target)");
3058 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
3059 src_surface->flags &= ~SFLAG_INTEXTURE;
3062 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3063 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3065 if(dst_rect.top > dst_rect.bottom) {
3066 UINT tmp = dst_rect.bottom;
3067 dst_rect.bottom = dst_rect.top;
3074 TRACE("Reading from an offscreen target\n");
3075 upsidedown = !upsidedown;
3076 glReadBuffer(device->offscreenBuffer);
3080 glReadBuffer(surface_get_gl_buffer(src_surface));
3083 /* TODO: Only back up the part that will be overwritten */
3084 glCopyTexSubImage2D(texture_target, 0,
3085 0, 0 /* read offsets */,
3090 checkGLcall("glCopyTexSubImage2D");
3092 /* No issue with overriding these - the sampler is dirty due to blit usage */
3093 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
3094 wined3d_gl_mag_filter(magLookup, Filter));
3095 checkGLcall("glTexParameteri");
3096 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
3097 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
3098 checkGLcall("glTexParameteri");
3100 if (src_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3101 src_swapchain = src_surface->container.u.swapchain;
3102 if (!src_swapchain || src_surface == src_swapchain->back_buffers[0])
3104 src = backup ? backup : src_surface->texture_name;
3108 glReadBuffer(GL_FRONT);
3109 checkGLcall("glReadBuffer(GL_FRONT)");
3111 glGenTextures(1, &src);
3112 checkGLcall("glGenTextures(1, &src)");
3113 glBindTexture(GL_TEXTURE_2D, src);
3114 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
3116 /* TODO: Only copy the part that will be read. Use src_rect->left, src_rect->bottom as origin, but with the width watch
3117 * out for power of 2 sizes
3119 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_surface->pow2Width,
3120 src_surface->pow2Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
3121 checkGLcall("glTexImage2D");
3122 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
3123 0, 0 /* read offsets */,
3128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3129 checkGLcall("glTexParameteri");
3130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3131 checkGLcall("glTexParameteri");
3133 glReadBuffer(GL_BACK);
3134 checkGLcall("glReadBuffer(GL_BACK)");
3136 if(texture_target != GL_TEXTURE_2D) {
3137 glDisable(texture_target);
3138 glEnable(GL_TEXTURE_2D);
3139 texture_target = GL_TEXTURE_2D;
3142 checkGLcall("glEnd and previous");
3144 left = src_rect->left;
3145 right = src_rect->right;
3149 top = src_surface->resource.height - src_rect->top;
3150 bottom = src_surface->resource.height - src_rect->bottom;
3154 top = src_surface->resource.height - src_rect->bottom;
3155 bottom = src_surface->resource.height - src_rect->top;
3158 if (src_surface->flags & SFLAG_NORMCOORD)
3160 left /= src_surface->pow2Width;
3161 right /= src_surface->pow2Width;
3162 top /= src_surface->pow2Height;
3163 bottom /= src_surface->pow2Height;
3166 /* draw the source texture stretched and upside down. The correct surface is bound already */
3167 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3168 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3170 context_set_draw_buffer(context, drawBuffer);
3171 glReadBuffer(drawBuffer);
3175 glTexCoord2f(left, bottom);
3179 glTexCoord2f(left, top);
3180 glVertex2i(0, dst_rect.bottom - dst_rect.top);
3183 glTexCoord2f(right, top);
3184 glVertex2i(dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3187 glTexCoord2f(right, bottom);
3188 glVertex2i(dst_rect.right - dst_rect.left, 0);
3190 checkGLcall("glEnd and previous");
3192 if (texture_target != dst_surface->texture_target)
3194 glDisable(texture_target);
3195 glEnable(dst_surface->texture_target);
3196 texture_target = dst_surface->texture_target;
3199 /* Now read the stretched and upside down image into the destination texture */
3200 glBindTexture(texture_target, dst_surface->texture_name);
3201 checkGLcall("glBindTexture");
3202 glCopyTexSubImage2D(texture_target,
3204 dst_rect.left, dst_rect.top, /* xoffset, yoffset */
3205 0, 0, /* We blitted the image to the origin */
3206 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3207 checkGLcall("glCopyTexSubImage2D");
3209 if(drawBuffer == GL_BACK) {
3210 /* Write the back buffer backup back */
3212 if(texture_target != GL_TEXTURE_2D) {
3213 glDisable(texture_target);
3214 glEnable(GL_TEXTURE_2D);
3215 texture_target = GL_TEXTURE_2D;
3217 glBindTexture(GL_TEXTURE_2D, backup);
3218 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3222 if (texture_target != src_surface->texture_target)
3224 glDisable(texture_target);
3225 glEnable(src_surface->texture_target);
3226 texture_target = src_surface->texture_target;
3228 glBindTexture(src_surface->texture_target, src_surface->texture_name);
3229 checkGLcall("glBindTexture(src_surface->texture_target, src_surface->texture_name)");
3234 glTexCoord2f(0.0f, 0.0f);
3235 glVertex2i(0, fbheight);
3238 glTexCoord2f(0.0f, (float)fbheight / (float)src_surface->pow2Height);
3242 glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width,
3243 (float)fbheight / (float)src_surface->pow2Height);
3244 glVertex2i(fbwidth, 0);
3247 glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width, 0.0f);
3248 glVertex2i(fbwidth, fbheight);
3251 glDisable(texture_target);
3252 checkGLcall("glDisable(texture_target)");
3255 if (src != src_surface->texture_name && src != backup)
3257 glDeleteTextures(1, &src);
3258 checkGLcall("glDeleteTextures(1, &src)");
3261 glDeleteTextures(1, &backup);
3262 checkGLcall("glDeleteTextures(1, &backup)");
3267 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
3269 context_release(context);
3271 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3272 * path is never entered
3274 surface_modify_location(dst_surface, SFLAG_INTEXTURE, TRUE);
3277 /* Until the blit_shader is ready, define some prototypes here. */
3278 static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
3279 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
3280 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format);
3282 /* Front buffer coordinates are always full screen coordinates, but our GL
3283 * drawable is limited to the window's client area. The sysmem and texture
3284 * copies do have the full screen size. Note that GL has a bottom-left
3285 * origin, while D3D has a top-left origin. */
3286 void surface_translate_drawable_coords(IWineD3DSurfaceImpl *surface, HWND window, RECT *rect)
3288 UINT drawable_height;
3290 if (surface->container.type == WINED3D_CONTAINER_SWAPCHAIN
3291 && surface == surface->container.u.swapchain->front_buffer)
3293 POINT offset = {0, 0};
3296 ScreenToClient(window, &offset);
3297 OffsetRect(rect, offset.x, offset.y);
3299 GetClientRect(window, &windowsize);
3300 drawable_height = windowsize.bottom - windowsize.top;
3304 drawable_height = surface->resource.height;
3307 rect->top = drawable_height - rect->top;
3308 rect->bottom = drawable_height - rect->bottom;
3311 static BOOL surface_is_full_rect(IWineD3DSurfaceImpl *surface, const RECT *r)
3313 if ((r->left && r->right) || abs(r->right - r->left) != surface->resource.width)
3315 if ((r->top && r->bottom) || abs(r->bottom - r->top) != surface->resource.height)
3320 /* blit between surface locations. onscreen on different swapchains is not supported.
3321 * depth / stencil is not supported. */
3322 static void surface_blt_fbo(IWineD3DDeviceImpl *device, const WINED3DTEXTUREFILTERTYPE filter,
3323 IWineD3DSurfaceImpl *src_surface, DWORD src_location, const RECT *src_rect_in,
3324 IWineD3DSurfaceImpl *dst_surface, DWORD dst_location, const RECT *dst_rect_in)
3326 const struct wined3d_gl_info *gl_info;
3327 struct wined3d_context *context;
3328 RECT src_rect, dst_rect;
3331 TRACE("device %p, filter %s,\n", device, debug_d3dtexturefiltertype(filter));
3332 TRACE("src_surface %p, src_location %s, src_rect %s,\n",
3333 src_surface, debug_surflocation(src_location), wine_dbgstr_rect(src_rect_in));
3334 TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
3335 dst_surface, debug_surflocation(dst_location), wine_dbgstr_rect(dst_rect_in));
3337 src_rect = *src_rect_in;
3338 dst_rect = *dst_rect_in;
3342 case WINED3DTEXF_LINEAR:
3343 gl_filter = GL_LINEAR;
3347 FIXME("Unsupported filter mode %s (%#x).\n", debug_d3dtexturefiltertype(filter), filter);
3348 case WINED3DTEXF_NONE:
3349 case WINED3DTEXF_POINT:
3350 gl_filter = GL_NEAREST;
3354 if (src_location == SFLAG_INDRAWABLE && surface_is_offscreen(src_surface))
3355 src_location = SFLAG_INTEXTURE;
3356 if (dst_location == SFLAG_INDRAWABLE && surface_is_offscreen(dst_surface))
3357 dst_location = SFLAG_INTEXTURE;
3359 /* Make sure the locations are up-to-date. Loading the destination
3360 * surface isn't required if the entire surface is overwritten. (And is
3361 * in fact harmful if we're being called by surface_load_location() with
3362 * the purpose of loading the destination surface.) */
3363 surface_load_location(src_surface, src_location, NULL);
3364 if (!surface_is_full_rect(dst_surface, &dst_rect))
3365 surface_load_location(dst_surface, dst_location, NULL);
3367 if (src_location == SFLAG_INDRAWABLE) context = context_acquire(device, src_surface);
3368 else if (dst_location == SFLAG_INDRAWABLE) context = context_acquire(device, dst_surface);
3369 else context = context_acquire(device, NULL);
3371 if (!context->valid)
3373 context_release(context);
3374 WARN("Invalid context, skipping blit.\n");
3378 gl_info = context->gl_info;
3380 if (src_location == SFLAG_INDRAWABLE)
3382 GLenum buffer = surface_get_gl_buffer(src_surface);
3384 TRACE("Source surface %p is onscreen.\n", src_surface);
3386 surface_translate_drawable_coords(src_surface, context->win_handle, &src_rect);
3389 context_bind_fbo(context, GL_READ_FRAMEBUFFER, NULL);
3390 glReadBuffer(buffer);
3391 checkGLcall("glReadBuffer()");
3395 TRACE("Source surface %p is offscreen.\n", src_surface);
3397 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, src_surface, NULL, src_location);
3398 glReadBuffer(GL_COLOR_ATTACHMENT0);
3399 checkGLcall("glReadBuffer()");
3401 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
3404 if (dst_location == SFLAG_INDRAWABLE)
3406 GLenum buffer = surface_get_gl_buffer(dst_surface);
3408 TRACE("Destination surface %p is onscreen.\n", dst_surface);
3410 surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
3413 context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, NULL);
3414 context_set_draw_buffer(context, buffer);
3418 TRACE("Destination surface %p is offscreen.\n", dst_surface);
3421 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, dst_surface, NULL, dst_location);
3422 context_set_draw_buffer(context, GL_COLOR_ATTACHMENT0);
3424 context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
3426 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3427 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE));
3428 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1));
3429 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2));
3430 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3));
3432 glDisable(GL_SCISSOR_TEST);
3433 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE));
3435 gl_info->fbo_ops.glBlitFramebuffer(src_rect.left, src_rect.top, src_rect.right, src_rect.bottom,
3436 dst_rect.left, dst_rect.top, dst_rect.right, dst_rect.bottom, GL_COLOR_BUFFER_BIT, gl_filter);
3437 checkGLcall("glBlitFramebuffer()");
3441 if (wined3d_settings.strict_draw_ordering
3442 || (dst_location == SFLAG_INDRAWABLE
3443 && dst_surface->container.u.swapchain->front_buffer == dst_surface))
3446 context_release(context);
3449 static void wined3d_surface_depth_blt_fbo(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *src_surface,
3450 const RECT *src_rect, IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect)
3452 const struct wined3d_gl_info *gl_info;
3453 struct wined3d_context *context;
3454 DWORD src_mask, dst_mask;
3457 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_rect %s.\n",
3458 device, src_surface, wine_dbgstr_rect(src_rect),
3459 dst_surface, wine_dbgstr_rect(dst_rect));
3461 src_mask = src_surface->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
3462 dst_mask = dst_surface->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
3464 if (src_mask != dst_mask)
3466 ERR("Incompatible formats %s and %s.\n",
3467 debug_d3dformat(src_surface->resource.format->id),
3468 debug_d3dformat(dst_surface->resource.format->id));
3474 ERR("Not a depth / stencil format: %s.\n",
3475 debug_d3dformat(src_surface->resource.format->id));
3480 if (src_mask & WINED3DFMT_FLAG_DEPTH)
3481 gl_mask |= GL_DEPTH_BUFFER_BIT;
3482 if (src_mask & WINED3DFMT_FLAG_STENCIL)
3483 gl_mask |= GL_STENCIL_BUFFER_BIT;
3485 /* Make sure the locations are up-to-date. Loading the destination
3486 * surface isn't required if the entire surface is overwritten. */
3487 surface_load_location(src_surface, SFLAG_INTEXTURE, NULL);
3488 if (!surface_is_full_rect(dst_surface, dst_rect))
3489 surface_load_location(dst_surface, SFLAG_INTEXTURE, NULL);
3491 context = context_acquire(device, NULL);
3492 if (!context->valid)
3494 context_release(context);
3495 WARN("Invalid context, skipping blit.\n");
3499 gl_info = context->gl_info;
3503 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, NULL, src_surface, SFLAG_INTEXTURE);
3504 glReadBuffer(GL_NONE);
3505 checkGLcall("glReadBuffer()");
3506 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
3508 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, NULL, dst_surface, SFLAG_INTEXTURE);
3509 context_set_draw_buffer(context, GL_NONE);
3510 context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
3512 if (gl_mask & GL_DEPTH_BUFFER_BIT)
3514 glDepthMask(GL_TRUE);
3515 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_ZWRITEENABLE));
3517 if (gl_mask & GL_STENCIL_BUFFER_BIT)
3519 if (context->gl_info->supported[EXT_STENCIL_TWO_SIDE])
3521 glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
3522 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_TWOSIDEDSTENCILMODE));
3525 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_STENCILWRITEMASK));
3528 glDisable(GL_SCISSOR_TEST);
3529 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE));
3531 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
3532 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
3533 checkGLcall("glBlitFramebuffer()");
3537 if (wined3d_settings.strict_draw_ordering)
3538 wglFlush(); /* Flush to ensure ordering across contexts. */
3540 context_release(context);
3543 static void surface_blt_to_drawable(IWineD3DDeviceImpl *device,
3544 WINED3DTEXTUREFILTERTYPE filter, BOOL color_key,
3545 IWineD3DSurfaceImpl *src_surface, const RECT *src_rect_in,
3546 IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect_in)
3548 struct wined3d_context *context;
3549 RECT src_rect, dst_rect;
3551 src_rect = *src_rect_in;
3552 dst_rect = *dst_rect_in;
3554 /* Make sure the surface is up-to-date. This should probably use
3555 * surface_load_location() and worry about the destination surface too,
3556 * unless we're overwriting it completely. */
3557 surface_internal_preload(src_surface, SRGB_RGB);
3559 /* Activate the destination context, set it up for blitting */
3560 context = context_acquire(device, dst_surface);
3561 context_apply_blit_state(context, device);
3563 if (!surface_is_offscreen(dst_surface))
3564 surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
3566 device->blitter->set_shader(device->blit_priv, context->gl_info, src_surface);
3572 glEnable(GL_ALPHA_TEST);
3573 checkGLcall("glEnable(GL_ALPHA_TEST)");
3575 /* When the primary render target uses P8, the alpha component
3576 * contains the palette index. Which means that the colorkey is one of
3577 * the palette entries. In other cases pixels that should be masked
3578 * away have alpha set to 0. */
3579 if (primary_render_target_is_p8(device))
3580 glAlphaFunc(GL_NOTEQUAL, (float)src_surface->SrcBltCKey.dwColorSpaceLowValue / 256.0f);
3582 glAlphaFunc(GL_NOTEQUAL, 0.0f);
3583 checkGLcall("glAlphaFunc");
3587 glDisable(GL_ALPHA_TEST);
3588 checkGLcall("glDisable(GL_ALPHA_TEST)");
3591 draw_textured_quad(src_surface, &src_rect, &dst_rect, filter);
3595 glDisable(GL_ALPHA_TEST);
3596 checkGLcall("glDisable(GL_ALPHA_TEST)");
3601 /* Leave the opengl state valid for blitting */
3602 device->blitter->unset_shader(context->gl_info);
3604 if (wined3d_settings.strict_draw_ordering
3605 || (dst_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN
3606 && (dst_surface->container.u.swapchain->front_buffer == dst_surface)))
3607 wglFlush(); /* Flush to ensure ordering across contexts. */
3609 context_release(context);
3612 /* Do not call while under the GL lock. */
3613 HRESULT surface_color_fill(IWineD3DSurfaceImpl *s, const RECT *rect, const WINED3DCOLORVALUE *color)
3615 IWineD3DDeviceImpl *device = s->resource.device;
3616 const struct blit_shader *blitter;
3618 blitter = wined3d_select_blitter(&device->adapter->gl_info, WINED3D_BLIT_OP_COLOR_FILL,
3619 NULL, 0, 0, NULL, rect, s->resource.usage, s->resource.pool, s->resource.format);
3622 FIXME("No blitter is capable of performing the requested color fill operation.\n");
3623 return WINED3DERR_INVALIDCALL;
3626 return blitter->color_fill(device, s, rect, color);
3629 /* Not called from the VTable */
3630 /* Do not call while under the GL lock. */
3631 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *dst_surface, const RECT *DestRect,
3632 IWineD3DSurfaceImpl *src_surface, const RECT *SrcRect, DWORD flags, const WINEDDBLTFX *DDBltFx,
3633 WINED3DTEXTUREFILTERTYPE Filter)
3635 IWineD3DDeviceImpl *device = dst_surface->resource.device;
3636 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3637 struct wined3d_swapchain *srcSwapchain = NULL, *dstSwapchain = NULL;
3638 RECT dst_rect, src_rect;
3640 TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, blt_fx %p, filter %s.\n",
3641 dst_surface, wine_dbgstr_rect(DestRect), src_surface, wine_dbgstr_rect(SrcRect),
3642 flags, DDBltFx, debug_d3dtexturefiltertype(Filter));
3644 /* Get the swapchain. One of the surfaces has to be a primary surface */
3645 if (dst_surface->resource.pool == WINED3DPOOL_SYSTEMMEM)
3647 WARN("Destination is in sysmem, rejecting gl blt\n");
3648 return WINED3DERR_INVALIDCALL;
3651 if (dst_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3652 dstSwapchain = dst_surface->container.u.swapchain;
3656 if (src_surface->resource.pool == WINED3DPOOL_SYSTEMMEM)
3658 WARN("Src is in sysmem, rejecting gl blt\n");
3659 return WINED3DERR_INVALIDCALL;
3662 if (src_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3663 srcSwapchain = src_surface->container.u.swapchain;
3666 /* Early sort out of cases where no render target is used */
3667 if (!dstSwapchain && !srcSwapchain
3668 && src_surface != device->render_targets[0]
3669 && dst_surface != device->render_targets[0])
3671 TRACE("No surface is render target, not using hardware blit.\n");
3672 return WINED3DERR_INVALIDCALL;
3675 /* No destination color keying supported */
3676 if (flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE))
3678 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3679 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3680 return WINED3DERR_INVALIDCALL;
3683 surface_get_rect(dst_surface, DestRect, &dst_rect);
3684 if (src_surface) surface_get_rect(src_surface, SrcRect, &src_rect);
3686 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3687 if (dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->back_buffers
3688 && dst_surface == dstSwapchain->front_buffer
3689 && src_surface == dstSwapchain->back_buffers[0])
3691 /* Half-Life does a Blt from the back buffer to the front buffer,
3692 * Full surface size, no flags... Use present instead
3694 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3697 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3700 TRACE("Looking if a Present can be done...\n");
3701 /* Source Rectangle must be full surface */
3702 if (src_rect.left || src_rect.top
3703 || src_rect.right != src_surface->resource.width
3704 || src_rect.bottom != src_surface->resource.height)
3706 TRACE("No, Source rectangle doesn't match\n");
3710 /* No stretching may occur */
3711 if(src_rect.right != dst_rect.right - dst_rect.left ||
3712 src_rect.bottom != dst_rect.bottom - dst_rect.top) {
3713 TRACE("No, stretching is done\n");
3717 /* Destination must be full surface or match the clipping rectangle */
3718 if (dst_surface->clipper && dst_surface->clipper->hWnd)
3722 GetClientRect(dst_surface->clipper->hWnd, &cliprect);
3723 pos[0].x = dst_rect.left;
3724 pos[0].y = dst_rect.top;
3725 pos[1].x = dst_rect.right;
3726 pos[1].y = dst_rect.bottom;
3727 MapWindowPoints(GetDesktopWindow(), dst_surface->clipper->hWnd, pos, 2);
3729 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3730 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3732 TRACE("No, dest rectangle doesn't match(clipper)\n");
3733 TRACE("Clip rect at %s\n", wine_dbgstr_rect(&cliprect));
3734 TRACE("Blt dest: %s\n", wine_dbgstr_rect(&dst_rect));
3738 else if (dst_rect.left || dst_rect.top
3739 || dst_rect.right != dst_surface->resource.width
3740 || dst_rect.bottom != dst_surface->resource.height)
3742 TRACE("No, dest rectangle doesn't match(surface size)\n");
3748 /* These flags are unimportant for the flag check, remove them */
3749 if (!(flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)))
3751 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3753 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3754 * take very long, while a flip is fast.
3755 * This applies to Half-Life, which does such Blts every time it finished
3756 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3757 * menu. This is also used by all apps when they do windowed rendering
3759 * The problem is that flipping is not really the same as copying. After a
3760 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3761 * untouched. Therefore it's necessary to override the swap effect
3762 * and to set it back after the flip.
3764 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3768 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3769 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3771 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead.\n");
3772 wined3d_swapchain_present(dstSwapchain, NULL, NULL, dstSwapchain->win_handle, NULL, 0);
3774 dstSwapchain->presentParms.SwapEffect = orig_swap;
3781 TRACE("Unsupported blit between buffers on the same swapchain\n");
3782 return WINED3DERR_INVALIDCALL;
3783 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3784 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3785 return WINED3DERR_INVALIDCALL;
3786 } else if(dstSwapchain && srcSwapchain) {
3787 FIXME("Implement hardware blit between two different swapchains\n");
3788 return WINED3DERR_INVALIDCALL;
3790 else if (dstSwapchain)
3792 /* Handled with regular texture -> swapchain blit */
3793 if (src_surface == device->render_targets[0])
3794 TRACE("Blit from active render target to a swapchain\n");
3796 else if (srcSwapchain && dst_surface == device->render_targets[0])
3798 FIXME("Implement blit from a swapchain to the active render target\n");
3799 return WINED3DERR_INVALIDCALL;
3802 if ((srcSwapchain || src_surface == device->render_targets[0]) && !dstSwapchain)
3804 /* Blit from render target to texture */
3807 /* P8 read back is not implemented */
3808 if (src_surface->resource.format->id == WINED3DFMT_P8_UINT
3809 || dst_surface->resource.format->id == WINED3DFMT_P8_UINT)
3811 TRACE("P8 read back not supported by frame buffer to texture blit\n");
3812 return WINED3DERR_INVALIDCALL;
3815 if (flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3817 TRACE("Color keying not supported by frame buffer to texture blit\n");
3818 return WINED3DERR_INVALIDCALL;
3819 /* Destination color key is checked above */
3822 if(dst_rect.right - dst_rect.left != src_rect.right - src_rect.left) {
3828 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3829 * flip the image nor scale it.
3831 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3832 * -> If the app wants a image width an unscaled width, copy it line per line
3833 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3834 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3835 * back buffer. This is slower than reading line per line, thus not used for flipping
3836 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3839 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3840 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3842 if (fbo_blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
3843 &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
3844 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
3846 surface_blt_fbo(device, Filter,
3847 src_surface, SFLAG_INDRAWABLE, &src_rect,
3848 dst_surface, SFLAG_INDRAWABLE, &dst_rect);
3849 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3851 else if (!stretchx || dst_rect.right - dst_rect.left > src_surface->resource.width
3852 || dst_rect.bottom - dst_rect.top > src_surface->resource.height)
3854 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3855 fb_copy_to_texture_direct(dst_surface, src_surface, &src_rect, &dst_rect, Filter);
3857 TRACE("Using hardware stretching to flip / stretch the texture\n");
3858 fb_copy_to_texture_hwstretch(dst_surface, src_surface, &src_rect, &dst_rect, Filter);
3861 if (!(dst_surface->flags & SFLAG_DONOTFREE))
3863 HeapFree(GetProcessHeap(), 0, dst_surface->resource.heapMemory);
3864 dst_surface->resource.allocatedMemory = NULL;
3865 dst_surface->resource.heapMemory = NULL;
3869 dst_surface->flags &= ~SFLAG_INSYSMEM;
3874 else if (src_surface)
3876 /* Blit from offscreen surface to render target */
3877 DWORD oldCKeyFlags = src_surface->CKeyFlags;
3878 WINEDDCOLORKEY oldBltCKey = src_surface->SrcBltCKey;
3880 TRACE("Blt from surface %p to rendertarget %p\n", src_surface, dst_surface);
3882 if (!(flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3883 && fbo_blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
3884 &src_rect, src_surface->resource.usage, src_surface->resource.pool,
3885 src_surface->resource.format,
3886 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3887 dst_surface->resource.format))
3889 TRACE("Using surface_blt_fbo.\n");
3890 /* The source is always a texture, but never the currently active render target, and the texture
3891 * contents are never upside down. */
3892 surface_blt_fbo(device, Filter,
3893 src_surface, SFLAG_INDRAWABLE, &src_rect,
3894 dst_surface, SFLAG_INDRAWABLE, &dst_rect);
3895 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3899 if (!(flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3900 && arbfp_blit.blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
3901 &src_rect, src_surface->resource.usage, src_surface->resource.pool,
3902 src_surface->resource.format,
3903 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3904 dst_surface->resource.format))
3906 return arbfp_blit_surface(device, src_surface, &src_rect, dst_surface, &dst_rect,
3907 WINED3D_BLIT_OP_COLOR_BLIT, Filter);
3910 if (!device->blitter->blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
3911 &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
3912 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
3914 FIXME("Unsupported blit operation falling back to software\n");
3915 return WINED3DERR_INVALIDCALL;
3918 /* Color keying: Check if we have to do a color keyed blt,
3919 * and if not check if a color key is activated.
3921 * Just modify the color keying parameters in the surface and restore them afterwards
3922 * The surface keeps track of the color key last used to load the opengl surface.
3923 * PreLoad will catch the change to the flags and color key and reload if necessary.
3925 if (flags & WINEDDBLT_KEYSRC)
3927 /* Use color key from surface */
3929 else if (flags & WINEDDBLT_KEYSRCOVERRIDE)
3931 /* Use color key from DDBltFx */
3932 src_surface->CKeyFlags |= WINEDDSD_CKSRCBLT;
3933 src_surface->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3937 /* Do not use color key */
3938 src_surface->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3941 surface_blt_to_drawable(device, Filter, flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE),
3942 src_surface, &src_rect, dst_surface, &dst_rect);
3944 /* Restore the color key parameters */
3945 src_surface->CKeyFlags = oldCKeyFlags;
3946 src_surface->SrcBltCKey = oldBltCKey;
3948 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3954 /* Source-Less Blit to render target */
3955 if (flags & WINEDDBLT_COLORFILL)
3957 WINED3DCOLORVALUE color;
3959 TRACE("Colorfill\n");
3961 /* The color as given in the Blt function is in the surface format. */
3962 if (!surface_convert_color_to_float(dst_surface, DDBltFx->u5.dwFillColor, &color))
3963 return WINED3DERR_INVALIDCALL;
3965 return surface_color_fill(dst_surface, &dst_rect, &color);
3969 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3970 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3971 return WINED3DERR_INVALIDCALL;
3974 /* Do not call while under the GL lock. */
3975 static HRESULT wined3d_surface_depth_fill(IWineD3DSurfaceImpl *surface, const RECT *rect, float depth)
3977 const struct wined3d_resource *resource = &surface->resource;
3978 IWineD3DDeviceImpl *device = resource->device;
3979 const struct blit_shader *blitter;
3981 blitter = wined3d_select_blitter(&device->adapter->gl_info, WINED3D_BLIT_OP_DEPTH_FILL,
3982 NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format);
3985 FIXME("No blitter is capable of performing the requested depth fill operation.\n");
3986 return WINED3DERR_INVALIDCALL;
3989 return blitter->depth_fill(device, surface, rect, depth);
3992 static HRESULT wined3d_surface_depth_blt(IWineD3DSurfaceImpl *src_surface, const RECT *src_rect,
3993 IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect)
3995 IWineD3DDeviceImpl *device = src_surface->resource.device;
3997 if (!fbo_blit_supported(&device->adapter->gl_info, WINED3D_BLIT_OP_DEPTH_BLIT,
3998 src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
3999 dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
4000 return WINED3DERR_INVALIDCALL;
4002 wined3d_surface_depth_blt_fbo(device, src_surface, src_rect, dst_surface, dst_rect);
4004 surface_modify_ds_location(dst_surface, SFLAG_DS_OFFSCREEN,
4005 dst_surface->ds_current_size.cx, dst_surface->ds_current_size.cy);
4006 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
4011 /* Do not call while under the GL lock. */
4012 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, const RECT *DestRect,
4013 IWineD3DSurface *src_surface, const RECT *SrcRect, DWORD flags,
4014 const WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter)
4016 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
4017 IWineD3DSurfaceImpl *src = (IWineD3DSurfaceImpl *)src_surface;
4018 IWineD3DDeviceImpl *device = This->resource.device;
4019 DWORD src_ds_flags, dst_ds_flags;
4021 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
4022 iface, wine_dbgstr_rect(DestRect), src_surface, wine_dbgstr_rect(SrcRect),
4023 flags, DDBltFx, debug_d3dtexturefiltertype(Filter));
4024 TRACE("Usage is %s.\n", debug_d3dusage(This->resource.usage));
4026 if ((This->flags & SFLAG_LOCKED) || (src && (src->flags & SFLAG_LOCKED)))
4028 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
4029 return WINEDDERR_SURFACEBUSY;
4032 dst_ds_flags = This->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
4034 src_ds_flags = src->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
4038 if (src_ds_flags || dst_ds_flags)
4040 if (flags & WINEDDBLT_DEPTHFILL)
4045 TRACE("Depth fill.\n");
4047 surface_get_rect(This, DestRect, &rect);
4049 if (!surface_convert_depth_to_float(This, DDBltFx->u5.dwFillDepth, &depth))
4050 return WINED3DERR_INVALIDCALL;
4052 if (SUCCEEDED(wined3d_surface_depth_fill(This, &rect, depth)))
4057 RECT src_rect, dst_rect;
4059 /* Accessing depth / stencil surfaces is supposed to fail while in
4060 * a scene, except for fills, which seem to work. */
4061 if (device->inScene)
4063 WARN("Rejecting depth / stencil access while in scene.\n");
4064 return WINED3DERR_INVALIDCALL;
4067 if (src_ds_flags != dst_ds_flags)
4069 WARN("Rejecting depth / stencil blit between incompatible formats.\n");
4070 return WINED3DERR_INVALIDCALL;
4073 if (SrcRect && (SrcRect->top || SrcRect->left
4074 || SrcRect->bottom != src->resource.height
4075 || SrcRect->right != src->resource.width))
4077 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
4078 wine_dbgstr_rect(SrcRect));
4079 return WINED3DERR_INVALIDCALL;
4082 if (DestRect && (DestRect->top || DestRect->left
4083 || DestRect->bottom != This->resource.height
4084 || DestRect->right != This->resource.width))
4086 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
4087 wine_dbgstr_rect(SrcRect));
4088 return WINED3DERR_INVALIDCALL;
4091 if (src->resource.height != This->resource.height
4092 || src->resource.width != This->resource.width)
4094 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
4095 return WINED3DERR_INVALIDCALL;
4098 surface_get_rect(src, SrcRect, &src_rect);
4099 surface_get_rect(This, DestRect, &dst_rect);
4101 if (SUCCEEDED(wined3d_surface_depth_blt(src, &src_rect, This, &dst_rect)))
4106 /* Special cases for RenderTargets */
4107 if ((This->resource.usage & WINED3DUSAGE_RENDERTARGET)
4108 || (src && (src->resource.usage & WINED3DUSAGE_RENDERTARGET)))
4110 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This, DestRect, src, SrcRect, flags, DDBltFx, Filter)))
4114 /* For the rest call the X11 surface implementation.
4115 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
4116 * other Blts are rather rare. */
4117 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, src_surface, SrcRect, flags, DDBltFx, Filter);
4120 static HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty,
4121 IWineD3DSurface *src_surface, const RECT *rsrc, DWORD trans)
4123 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
4124 IWineD3DSurfaceImpl *src = (IWineD3DSurfaceImpl *)src_surface;
4125 IWineD3DDeviceImpl *device = This->resource.device;
4127 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4128 iface, dstx, dsty, src_surface, wine_dbgstr_rect(rsrc), trans);
4130 if ((This->flags & SFLAG_LOCKED) || (src->flags & SFLAG_LOCKED))
4132 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
4133 return WINEDDERR_SURFACEBUSY;
4136 if (device->inScene && (This == device->depth_stencil || src == device->depth_stencil))
4138 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
4139 return WINED3DERR_INVALIDCALL;
4142 /* Special cases for RenderTargets */
4143 if ((This->resource.usage & WINED3DUSAGE_RENDERTARGET)
4144 || (src->resource.usage & WINED3DUSAGE_RENDERTARGET))
4147 RECT SrcRect, DstRect;
4150 surface_get_rect(src, rsrc, &SrcRect);
4152 DstRect.left = dstx;
4154 DstRect.right = dstx + SrcRect.right - SrcRect.left;
4155 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
4157 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
4158 if (trans & WINEDDBLTFAST_SRCCOLORKEY)
4159 flags |= WINEDDBLT_KEYSRC;
4160 if (trans & WINEDDBLTFAST_DESTCOLORKEY)
4161 flags |= WINEDDBLT_KEYDEST;
4162 if (trans & WINEDDBLTFAST_WAIT)
4163 flags |= WINEDDBLT_WAIT;
4164 if (trans & WINEDDBLTFAST_DONOTWAIT)
4165 flags |= WINEDDBLT_DONOTWAIT;
4167 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This,
4168 &DstRect, src, &SrcRect, flags, NULL, WINED3DTEXF_POINT)))
4172 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, src_surface, rsrc, trans);
4175 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
4176 /** Check against the maximum texture sizes supported by the video card **/
4177 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4178 const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
4179 unsigned int pow2Width, pow2Height;
4181 This->surface_ops = &surface_ops;
4183 This->texture_name = 0;
4184 This->texture_target = GL_TEXTURE_2D;
4186 /* Non-power2 support */
4187 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
4189 pow2Width = This->resource.width;
4190 pow2Height = This->resource.height;
4194 /* Find the nearest pow2 match */
4195 pow2Width = pow2Height = 1;
4196 while (pow2Width < This->resource.width) pow2Width <<= 1;
4197 while (pow2Height < This->resource.height) pow2Height <<= 1;
4199 This->pow2Width = pow2Width;
4200 This->pow2Height = pow2Height;
4202 if (pow2Width > This->resource.width || pow2Height > This->resource.height)
4204 /* TODO: Add support for non power two compressed textures. */
4205 if (This->resource.format->flags & WINED3DFMT_FLAG_COMPRESSED)
4207 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
4208 This, This->resource.width, This->resource.height);
4209 return WINED3DERR_NOTAVAILABLE;
4213 if (pow2Width != This->resource.width
4214 || pow2Height != This->resource.height)
4216 This->flags |= SFLAG_NONPOW2;
4219 TRACE("%p\n", This);
4220 if ((This->pow2Width > gl_info->limits.texture_size || This->pow2Height > gl_info->limits.texture_size)
4221 && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
4223 /* one of three options
4224 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)
4225 2: Set the texture to the maximum size (bad idea)
4226 3: WARN and return WINED3DERR_NOTAVAILABLE;
4227 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.
4229 if(This->resource.pool == WINED3DPOOL_DEFAULT || This->resource.pool == WINED3DPOOL_MANAGED)
4231 WARN("(%p) Unable to allocate a surface which exceeds the maximum OpenGL texture size\n", This);
4232 return WINED3DERR_NOTAVAILABLE;
4235 /* We should never use this surface in combination with OpenGL! */
4236 TRACE("(%p) Creating an oversized surface: %ux%u\n", This, This->pow2Width, This->pow2Height);
4240 /* Don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
4241 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
4242 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
4244 if (This->flags & SFLAG_NONPOW2 && gl_info->supported[ARB_TEXTURE_RECTANGLE]
4245 && !(This->resource.format->id == WINED3DFMT_P8_UINT
4246 && gl_info->supported[EXT_PALETTED_TEXTURE]
4247 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
4249 This->texture_target = GL_TEXTURE_RECTANGLE_ARB;
4250 This->pow2Width = This->resource.width;
4251 This->pow2Height = This->resource.height;
4252 This->flags &= ~(SFLAG_NONPOW2 | SFLAG_NORMCOORD);
4256 switch (wined3d_settings.offscreen_rendering_mode)
4259 This->get_drawable_size = get_drawable_size_fbo;
4262 case ORM_BACKBUFFER:
4263 This->get_drawable_size = get_drawable_size_backbuffer;
4267 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
4268 return WINED3DERR_INVALIDCALL;
4271 This->flags |= SFLAG_INSYSMEM;
4276 /* GL locking is done by the caller */
4277 static void surface_depth_blt(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
4278 GLuint texture, GLsizei w, GLsizei h, GLenum target)
4280 IWineD3DDeviceImpl *device = This->resource.device;
4281 GLint compare_mode = GL_NONE;
4282 struct blt_info info;
4283 GLint old_binding = 0;
4286 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
4288 glDisable(GL_CULL_FACE);
4289 glDisable(GL_BLEND);
4290 glDisable(GL_ALPHA_TEST);
4291 glDisable(GL_SCISSOR_TEST);
4292 glDisable(GL_STENCIL_TEST);
4293 glEnable(GL_DEPTH_TEST);
4294 glDepthFunc(GL_ALWAYS);
4295 glDepthMask(GL_TRUE);
4296 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
4297 glViewport(0, This->pow2Height - h, w, h);
4299 SetRect(&rect, 0, h, w, 0);
4300 surface_get_blt_info(target, &rect, This->pow2Width, This->pow2Height, &info);
4301 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
4302 glGetIntegerv(info.binding, &old_binding);
4303 glBindTexture(info.bind_target, texture);
4304 if (gl_info->supported[ARB_SHADOW])
4306 glGetTexParameteriv(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, &compare_mode);
4307 if (compare_mode != GL_NONE) glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
4310 device->shader_backend->shader_select_depth_blt(device->shader_priv,
4311 gl_info, info.tex_type, &This->ds_current_size);
4313 glBegin(GL_TRIANGLE_STRIP);
4314 glTexCoord3fv(info.coords[0]);
4315 glVertex2f(-1.0f, -1.0f);
4316 glTexCoord3fv(info.coords[1]);
4317 glVertex2f(1.0f, -1.0f);
4318 glTexCoord3fv(info.coords[2]);
4319 glVertex2f(-1.0f, 1.0f);
4320 glTexCoord3fv(info.coords[3]);
4321 glVertex2f(1.0f, 1.0f);
4324 if (compare_mode != GL_NONE) glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, compare_mode);
4325 glBindTexture(info.bind_target, old_binding);
4329 device->shader_backend->shader_deselect_depth_blt(device->shader_priv, gl_info);
4332 void surface_modify_ds_location(IWineD3DSurfaceImpl *surface,
4333 DWORD location, UINT w, UINT h)
4335 TRACE("surface %p, new location %#x, w %u, h %u.\n", surface, location, w, h);
4337 if (location & ~SFLAG_DS_LOCATIONS)
4338 FIXME("Invalid location (%#x) specified.\n", location);
4340 surface->ds_current_size.cx = w;
4341 surface->ds_current_size.cy = h;
4342 surface->flags &= ~SFLAG_DS_LOCATIONS;
4343 surface->flags |= location;
4346 /* Context activation is done by the caller. */
4347 void surface_load_ds_location(IWineD3DSurfaceImpl *surface, struct wined3d_context *context, DWORD location)
4349 IWineD3DDeviceImpl *device = surface->resource.device;
4350 const struct wined3d_gl_info *gl_info = context->gl_info;
4353 TRACE("surface %p, new location %#x.\n", surface, location);
4355 /* TODO: Make this work for modes other than FBO */
4356 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
4358 if (!(surface->flags & location))
4360 w = surface->ds_current_size.cx;
4361 h = surface->ds_current_size.cy;
4362 surface->ds_current_size.cx = 0;
4363 surface->ds_current_size.cy = 0;
4367 w = surface->resource.width;
4368 h = surface->resource.height;
4371 if (surface->ds_current_size.cx == surface->resource.width
4372 && surface->ds_current_size.cy == surface->resource.height)
4374 TRACE("Location (%#x) is already up to date.\n", location);
4378 if (surface->current_renderbuffer)
4380 FIXME("Not supported with fixed up depth stencil.\n");
4384 if (!(surface->flags & SFLAG_DS_LOCATIONS))
4386 /* This mostly happens when a depth / stencil is used without being
4387 * cleared first. In principle we could upload from sysmem, or
4388 * explicitly clear before first usage. For the moment there don't
4389 * appear to be a lot of applications depending on this, so a FIXME
4391 FIXME("No up to date depth stencil location.\n");
4392 surface->flags |= location;
4393 surface->ds_current_size.cx = surface->resource.width;
4394 surface->ds_current_size.cy = surface->resource.height;
4398 if (location == SFLAG_DS_OFFSCREEN)
4400 GLint old_binding = 0;
4403 /* The render target is allowed to be smaller than the depth/stencil
4404 * buffer, so the onscreen depth/stencil buffer is potentially smaller
4405 * than the offscreen surface. Don't overwrite the offscreen surface
4406 * with undefined data. */
4407 w = min(w, context->swapchain->presentParms.BackBufferWidth);
4408 h = min(h, context->swapchain->presentParms.BackBufferHeight);
4410 TRACE("Copying onscreen depth buffer to depth texture.\n");
4414 if (!device->depth_blt_texture)
4416 glGenTextures(1, &device->depth_blt_texture);
4419 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
4420 * directly on the FBO texture. That's because we need to flip. */
4421 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4422 if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
4424 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
4425 bind_target = GL_TEXTURE_RECTANGLE_ARB;
4429 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
4430 bind_target = GL_TEXTURE_2D;
4432 glBindTexture(bind_target, device->depth_blt_texture);
4433 glCopyTexImage2D(bind_target, surface->texture_level, surface->resource.format->glInternal, 0, 0, w, h, 0);
4434 glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4435 glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4436 glTexParameteri(bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
4437 glTexParameteri(bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
4438 glTexParameteri(bind_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
4439 glTexParameteri(bind_target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
4440 glBindTexture(bind_target, old_binding);
4442 /* Setup the destination */
4443 if (!device->depth_blt_rb)
4445 gl_info->fbo_ops.glGenRenderbuffers(1, &device->depth_blt_rb);
4446 checkGLcall("glGenRenderbuffersEXT");
4448 if (device->depth_blt_rb_w != w || device->depth_blt_rb_h != h)
4450 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, device->depth_blt_rb);
4451 checkGLcall("glBindRenderbufferEXT");
4452 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, w, h);
4453 checkGLcall("glRenderbufferStorageEXT");
4454 device->depth_blt_rb_w = w;
4455 device->depth_blt_rb_h = h;
4458 context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
4459 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
4460 GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, device->depth_blt_rb);
4461 checkGLcall("glFramebufferRenderbufferEXT");
4462 context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, surface, FALSE);
4464 /* Do the actual blit */
4465 surface_depth_blt(surface, gl_info, device->depth_blt_texture, w, h, bind_target);
4466 checkGLcall("depth_blt");
4468 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4469 else context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4473 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4475 else if (location == SFLAG_DS_ONSCREEN)
4477 TRACE("Copying depth texture to onscreen depth buffer.\n");
4481 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4482 surface_depth_blt(surface, gl_info, surface->texture_name,
4483 w, h, surface->texture_target);
4484 checkGLcall("depth_blt");
4486 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4490 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4494 ERR("Invalid location (%#x) specified.\n", location);
4497 surface->flags |= location;
4498 surface->ds_current_size.cx = surface->resource.width;
4499 surface->ds_current_size.cy = surface->resource.height;
4502 void surface_modify_location(IWineD3DSurfaceImpl *surface, DWORD flag, BOOL persistent)
4504 const struct wined3d_gl_info *gl_info = &surface->resource.device->adapter->gl_info;
4505 IWineD3DSurfaceImpl *overlay;
4507 TRACE("surface %p, location %s, persistent %#x.\n",
4508 surface, debug_surflocation(flag), persistent);
4510 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4512 if (surface_is_offscreen(surface))
4514 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4515 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4519 TRACE("Surface %p is an onscreen surface.\n", surface);
4523 if (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)
4524 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
4526 flag |= (SFLAG_INTEXTURE | SFLAG_INSRGBTEX);
4531 if (((surface->flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE))
4532 || ((surface->flags & SFLAG_INSRGBTEX) && !(flag & SFLAG_INSRGBTEX)))
4534 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
4536 TRACE("Passing to container.\n");
4537 wined3d_texture_set_dirty(surface->container.u.texture, TRUE);
4540 surface->flags &= ~SFLAG_LOCATIONS;
4541 surface->flags |= flag;
4543 /* Redraw emulated overlays, if any */
4544 if (flag & SFLAG_INDRAWABLE && !list_empty(&surface->overlays))
4546 LIST_FOR_EACH_ENTRY(overlay, &surface->overlays, IWineD3DSurfaceImpl, overlay_entry)
4548 overlay->surface_ops->surface_draw_overlay(overlay);
4554 if ((surface->flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) && (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)))
4556 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
4558 TRACE("Passing to container\n");
4559 wined3d_texture_set_dirty(surface->container.u.texture, TRUE);
4562 surface->flags &= ~flag;
4565 if (!(surface->flags & SFLAG_LOCATIONS))
4567 ERR("Surface %p does not have any up to date location.\n", surface);
4571 HRESULT surface_load_location(IWineD3DSurfaceImpl *surface, DWORD flag, const RECT *rect)
4573 IWineD3DDeviceImpl *device = surface->resource.device;
4574 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4575 BOOL drawable_read_ok = surface_is_offscreen(surface);
4576 struct wined3d_format format;
4577 CONVERT_TYPES convert;
4578 int width, pitch, outpitch;
4580 BOOL in_fbo = FALSE;
4582 TRACE("surface %p, location %s, rect %s.\n", surface, debug_surflocation(flag), wine_dbgstr_rect(rect));
4584 if (surface->resource.usage & WINED3DUSAGE_DEPTHSTENCIL)
4586 if (flag == SFLAG_INTEXTURE)
4588 struct wined3d_context *context = context_acquire(device, NULL);
4589 surface_load_ds_location(surface, context, SFLAG_DS_OFFSCREEN);
4590 context_release(context);
4595 FIXME("Unimplemented location %s for depth/stencil buffers.\n", debug_surflocation(flag));
4596 return WINED3DERR_INVALIDCALL;
4600 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4602 if (surface_is_offscreen(surface))
4604 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4605 * Prefer SFLAG_INTEXTURE. */
4606 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4607 drawable_read_ok = FALSE;
4612 TRACE("Surface %p is an onscreen surface.\n", surface);
4616 if (flag == SFLAG_INSRGBTEX && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
4618 flag = SFLAG_INTEXTURE;
4621 if (surface->flags & flag)
4623 TRACE("Location already up to date\n");
4627 if (!(surface->flags & SFLAG_LOCATIONS))
4629 ERR("Surface %p does not have any up to date location.\n", surface);
4630 surface->flags |= SFLAG_LOST;
4631 return WINED3DERR_DEVICELOST;
4634 if (flag == SFLAG_INSYSMEM)
4636 surface_prepare_system_memory(surface);
4638 /* Download the surface to system memory */
4639 if (surface->flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX))
4641 struct wined3d_context *context = NULL;
4643 if (!device->isInDraw) context = context_acquire(device, NULL);
4645 surface_bind_and_dirtify(surface, gl_info, !(surface->flags & SFLAG_INTEXTURE));
4646 surface_download_data(surface, gl_info);
4648 if (context) context_release(context);
4652 /* Note: It might be faster to download into a texture first. */
4653 read_from_framebuffer(surface, rect, surface->resource.allocatedMemory,
4654 IWineD3DSurface_GetPitch((IWineD3DSurface *)surface));
4657 else if (flag == SFLAG_INDRAWABLE)
4659 if (wined3d_settings.rendertargetlock_mode == RTL_READTEX)
4660 surface_load_location(surface, SFLAG_INTEXTURE, NULL);
4662 if (surface->flags & SFLAG_INTEXTURE)
4666 surface_get_rect(surface, rect, &r);
4667 surface_blt_to_drawable(device, WINED3DTEXF_POINT, FALSE, surface, &r, surface, &r);
4672 if ((surface->flags & SFLAG_LOCATIONS) == SFLAG_INSRGBTEX)
4674 /* This needs a shader to convert the srgb data sampled from the GL texture into RGB
4675 * values, otherwise we get incorrect values in the target. For now go the slow way
4676 * via a system memory copy
4678 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4681 d3dfmt_get_conv(surface, FALSE /* We need color keying */,
4682 FALSE /* We won't use textures */, &format, &convert);
4684 /* The width is in 'length' not in bytes */
4685 width = surface->resource.width;
4686 pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *)surface);
4688 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4689 * but it isn't set (yet) in all cases it is getting called. */
4690 if ((convert != NO_CONVERSION) && (surface->flags & SFLAG_PBO))
4692 struct wined3d_context *context = NULL;
4694 TRACE("Removing the pbo attached to surface %p.\n", surface);
4696 if (!device->isInDraw) context = context_acquire(device, NULL);
4697 surface_remove_pbo(surface, gl_info);
4698 if (context) context_release(context);
4701 if ((convert != NO_CONVERSION) && surface->resource.allocatedMemory)
4703 int height = surface->resource.height;
4704 byte_count = format.conv_byte_count;
4706 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4707 outpitch = width * byte_count;
4708 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4710 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4712 ERR("Out of memory %d, %d!\n", outpitch, height);
4713 return WINED3DERR_OUTOFVIDEOMEMORY;
4715 d3dfmt_convert_surface(surface->resource.allocatedMemory, mem, pitch,
4716 width, height, outpitch, convert, surface);
4718 surface->flags |= SFLAG_CONVERTED;
4722 surface->flags &= ~SFLAG_CONVERTED;
4723 mem = surface->resource.allocatedMemory;
4724 byte_count = format.byte_count;
4727 flush_to_framebuffer_drawpixels(surface, rect, format.glFormat, format.glType, byte_count, mem);
4729 /* Don't delete PBO memory */
4730 if ((mem != surface->resource.allocatedMemory) && !(surface->flags & SFLAG_PBO))
4731 HeapFree(GetProcessHeap(), 0, mem);
4734 else /* if(flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) */
4736 const DWORD attach_flags = WINED3DFMT_FLAG_FBO_ATTACHABLE | WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB;
4738 if (drawable_read_ok && (surface->flags & SFLAG_INDRAWABLE))
4740 read_from_framebuffer_texture(surface, flag == SFLAG_INSRGBTEX);
4742 else if (surface->flags & (SFLAG_INSRGBTEX | SFLAG_INTEXTURE)
4743 && (surface->resource.format->flags & attach_flags) == attach_flags
4744 && fbo_blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
4745 NULL, surface->resource.usage, surface->resource.pool, surface->resource.format,
4746 NULL, surface->resource.usage, surface->resource.pool, surface->resource.format))
4748 DWORD src_location = flag == SFLAG_INSRGBTEX ? SFLAG_INTEXTURE : SFLAG_INSRGBTEX;
4749 RECT rect = {0, 0, surface->resource.width, surface->resource.height};
4751 surface_blt_fbo(surface->resource.device, WINED3DTEXF_POINT,
4752 surface, src_location, &rect, surface, flag, &rect);
4756 /* Upload from system memory */
4757 BOOL srgb = flag == SFLAG_INSRGBTEX;
4758 struct wined3d_context *context = NULL;
4760 d3dfmt_get_conv(surface, TRUE /* We need color keying */,
4761 TRUE /* We will use textures */, &format, &convert);
4765 if ((surface->flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)) == SFLAG_INTEXTURE)
4767 /* Performance warning... */
4768 FIXME("Downloading RGB surface %p to reload it as sRGB.\n", surface);
4769 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4774 if ((surface->flags & (SFLAG_INSRGBTEX | SFLAG_INSYSMEM)) == SFLAG_INSRGBTEX)
4776 /* Performance warning... */
4777 FIXME("Downloading sRGB surface %p to reload it as RGB.\n", surface);
4778 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4781 if (!(surface->flags & SFLAG_INSYSMEM))
4783 WARN("Trying to load a texture from sysmem, but SFLAG_INSYSMEM is not set.\n");
4784 /* Lets hope we get it from somewhere... */
4785 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4788 if (!device->isInDraw) context = context_acquire(device, NULL);
4790 surface_prepare_texture(surface, gl_info, srgb);
4791 surface_bind_and_dirtify(surface, gl_info, srgb);
4793 if (surface->CKeyFlags & WINEDDSD_CKSRCBLT)
4795 surface->flags |= SFLAG_GLCKEY;
4796 surface->glCKey = surface->SrcBltCKey;
4798 else surface->flags &= ~SFLAG_GLCKEY;
4800 /* The width is in 'length' not in bytes */
4801 width = surface->resource.width;
4802 pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *)surface);
4804 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4805 * but it isn't set (yet) in all cases it is getting called. */
4806 if ((convert != NO_CONVERSION || format.convert) && (surface->flags & SFLAG_PBO))
4808 TRACE("Removing the pbo attached to surface %p.\n", surface);
4809 surface_remove_pbo(surface, gl_info);
4814 /* This code is entered for texture formats which need a fixup. */
4815 UINT height = surface->resource.height;
4817 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4818 outpitch = width * format.conv_byte_count;
4819 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4821 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4823 ERR("Out of memory %d, %d!\n", outpitch, height);
4824 if (context) context_release(context);
4825 return WINED3DERR_OUTOFVIDEOMEMORY;
4827 format.convert(surface->resource.allocatedMemory, mem, pitch, width, height);
4829 else if (convert != NO_CONVERSION && surface->resource.allocatedMemory)
4831 /* This code is only entered for color keying fixups */
4832 UINT height = surface->resource.height;
4834 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4835 outpitch = width * format.conv_byte_count;
4836 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4838 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4840 ERR("Out of memory %d, %d!\n", outpitch, height);
4841 if (context) context_release(context);
4842 return WINED3DERR_OUTOFVIDEOMEMORY;
4844 d3dfmt_convert_surface(surface->resource.allocatedMemory, mem, pitch,
4845 width, height, outpitch, convert, surface);
4849 mem = surface->resource.allocatedMemory;
4852 /* Make sure the correct pitch is used */
4854 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4857 if (mem || (surface->flags & SFLAG_PBO))
4858 surface_upload_data(surface, gl_info, &format, srgb, mem);
4860 /* Restore the default pitch */
4862 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4865 if (context) context_release(context);
4867 /* Don't delete PBO memory */
4868 if ((mem != surface->resource.allocatedMemory) && !(surface->flags & SFLAG_PBO))
4869 HeapFree(GetProcessHeap(), 0, mem);
4875 surface->flags |= flag;
4877 if (flag != SFLAG_INSYSMEM && (surface->flags & SFLAG_INSYSMEM))
4878 surface_evict_sysmem(surface);
4881 if (in_fbo && (surface->flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)))
4883 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4884 surface->flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4887 if (surface->flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)
4888 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
4890 surface->flags |= (SFLAG_INTEXTURE | SFLAG_INSRGBTEX);
4896 BOOL surface_is_offscreen(IWineD3DSurfaceImpl *surface)
4898 struct wined3d_swapchain *swapchain = surface->container.u.swapchain;
4900 /* Not on a swapchain - must be offscreen */
4901 if (surface->container.type != WINED3D_CONTAINER_SWAPCHAIN) return TRUE;
4903 /* The front buffer is always onscreen */
4904 if (surface == swapchain->front_buffer) return FALSE;
4906 /* If the swapchain is rendered to an FBO, the backbuffer is
4907 * offscreen, otherwise onscreen */
4908 return swapchain->render_to_fbo;
4911 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4914 IWineD3DBaseSurfaceImpl_QueryInterface,
4915 IWineD3DBaseSurfaceImpl_AddRef,
4916 IWineD3DSurfaceImpl_Release,
4917 /* IWineD3DResource */
4918 IWineD3DBaseSurfaceImpl_GetParent,
4919 IWineD3DBaseSurfaceImpl_SetPrivateData,
4920 IWineD3DBaseSurfaceImpl_GetPrivateData,
4921 IWineD3DBaseSurfaceImpl_FreePrivateData,
4922 IWineD3DBaseSurfaceImpl_SetPriority,
4923 IWineD3DBaseSurfaceImpl_GetPriority,
4924 IWineD3DSurfaceImpl_PreLoad,
4925 /* IWineD3DSurface */
4926 IWineD3DBaseSurfaceImpl_GetResource,
4927 IWineD3DSurfaceImpl_Map,
4928 IWineD3DSurfaceImpl_Unmap,
4929 IWineD3DSurfaceImpl_GetDC,
4930 IWineD3DSurfaceImpl_ReleaseDC,
4931 IWineD3DSurfaceImpl_Flip,
4932 IWineD3DSurfaceImpl_Blt,
4933 IWineD3DBaseSurfaceImpl_GetBltStatus,
4934 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4935 IWineD3DBaseSurfaceImpl_IsLost,
4936 IWineD3DBaseSurfaceImpl_Restore,
4937 IWineD3DSurfaceImpl_BltFast,
4938 IWineD3DBaseSurfaceImpl_GetPalette,
4939 IWineD3DBaseSurfaceImpl_SetPalette,
4940 IWineD3DBaseSurfaceImpl_SetColorKey,
4941 IWineD3DBaseSurfaceImpl_GetPitch,
4942 IWineD3DSurfaceImpl_SetMem,
4943 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4944 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4945 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4946 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4947 IWineD3DBaseSurfaceImpl_SetClipper,
4948 IWineD3DBaseSurfaceImpl_GetClipper,
4950 IWineD3DSurfaceImpl_SetFormat,
4951 IWineD3DSurfaceImpl_PrivateSetup,
4954 static HRESULT ffp_blit_alloc(IWineD3DDeviceImpl *device) { return WINED3D_OK; }
4955 /* Context activation is done by the caller. */
4956 static void ffp_blit_free(IWineD3DDeviceImpl *device) { }
4958 /* This function is used in case of 8bit paletted textures using GL_EXT_paletted_texture */
4959 /* Context activation is done by the caller. */
4960 static void ffp_blit_p8_upload_palette(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info)
4963 BOOL colorkey_active = (surface->CKeyFlags & WINEDDSD_CKSRCBLT) ? TRUE : FALSE;
4965 d3dfmt_p8_init_palette(surface, table, colorkey_active);
4967 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
4969 GL_EXTCALL(glColorTableEXT(surface->texture_target, GL_RGBA, 256, GL_RGBA, GL_UNSIGNED_BYTE, table));
4973 /* Context activation is done by the caller. */
4974 static HRESULT ffp_blit_set(void *blit_priv, const struct wined3d_gl_info *gl_info, IWineD3DSurfaceImpl *surface)
4976 enum complex_fixup fixup = get_complex_fixup(surface->resource.format->color_fixup);
4978 /* When EXT_PALETTED_TEXTURE is around, palette conversion is done by the GPU
4979 * else the surface is converted in software at upload time in LoadLocation.
4981 if(fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4982 ffp_blit_p8_upload_palette(surface, gl_info);
4985 glEnable(surface->texture_target);
4986 checkGLcall("glEnable(surface->texture_target)");
4991 /* Context activation is done by the caller. */
4992 static void ffp_blit_unset(const struct wined3d_gl_info *gl_info)
4995 glDisable(GL_TEXTURE_2D);
4996 checkGLcall("glDisable(GL_TEXTURE_2D)");
4997 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
4999 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
5000 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
5002 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5004 glDisable(GL_TEXTURE_RECTANGLE_ARB);
5005 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
5010 static BOOL ffp_blit_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
5011 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
5012 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
5014 enum complex_fixup src_fixup;
5018 case WINED3D_BLIT_OP_COLOR_BLIT:
5019 src_fixup = get_complex_fixup(src_format->color_fixup);
5020 if (TRACE_ON(d3d_surface) && TRACE_ON(d3d))
5022 TRACE("Checking support for fixup:\n");
5023 dump_color_fixup_desc(src_format->color_fixup);
5026 if (!is_identity_fixup(dst_format->color_fixup))
5028 TRACE("Destination fixups are not supported\n");
5032 if (src_fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
5034 TRACE("P8 fixup supported\n");
5038 /* We only support identity conversions. */
5039 if (is_identity_fixup(src_format->color_fixup))
5045 TRACE("[FAILED]\n");
5048 case WINED3D_BLIT_OP_COLOR_FILL:
5049 if (!(dst_usage & WINED3DUSAGE_RENDERTARGET))
5051 TRACE("Color fill not supported\n");
5057 case WINED3D_BLIT_OP_DEPTH_FILL:
5061 TRACE("Unsupported blit_op=%d\n", blit_op);
5066 /* Do not call while under the GL lock. */
5067 static HRESULT ffp_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface,
5068 const RECT *dst_rect, const WINED3DCOLORVALUE *color)
5070 const RECT draw_rect = {0, 0, dst_surface->resource.width, dst_surface->resource.height};
5072 return device_clear_render_targets(device, 1, &dst_surface, NULL,
5073 1, dst_rect, &draw_rect, WINED3DCLEAR_TARGET, color, 0.0f, 0);
5076 /* Do not call while under the GL lock. */
5077 static HRESULT ffp_blit_depth_fill(IWineD3DDeviceImpl *device,
5078 IWineD3DSurfaceImpl *surface, const RECT *rect, float depth)
5080 const RECT draw_rect = {0, 0, surface->resource.width, surface->resource.height};
5082 return device_clear_render_targets(device, 0, NULL, surface,
5083 1, rect, &draw_rect, WINED3DCLEAR_ZBUFFER, 0, depth, 0);
5086 const struct blit_shader ffp_blit = {
5092 ffp_blit_color_fill,
5093 ffp_blit_depth_fill,
5096 static HRESULT cpu_blit_alloc(IWineD3DDeviceImpl *device)
5101 /* Context activation is done by the caller. */
5102 static void cpu_blit_free(IWineD3DDeviceImpl *device)
5106 /* Context activation is done by the caller. */
5107 static HRESULT cpu_blit_set(void *blit_priv, const struct wined3d_gl_info *gl_info, IWineD3DSurfaceImpl *surface)
5112 /* Context activation is done by the caller. */
5113 static void cpu_blit_unset(const struct wined3d_gl_info *gl_info)
5117 static BOOL cpu_blit_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
5118 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
5119 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
5121 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
5129 /* Do not call while under the GL lock. */
5130 static HRESULT cpu_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface,
5131 const RECT *dst_rect, const WINED3DCOLORVALUE *color)
5135 memset(&BltFx, 0, sizeof(BltFx));
5136 BltFx.dwSize = sizeof(BltFx);
5137 BltFx.u5.dwFillColor = wined3d_format_convert_from_float(dst_surface->resource.format, color);
5138 return IWineD3DBaseSurfaceImpl_Blt((IWineD3DSurface*)dst_surface, dst_rect,
5139 NULL, NULL, WINEDDBLT_COLORFILL, &BltFx, WINED3DTEXF_POINT);
5142 /* Do not call while under the GL lock. */
5143 static HRESULT cpu_blit_depth_fill(IWineD3DDeviceImpl *device,
5144 IWineD3DSurfaceImpl *surface, const RECT *rect, float depth)
5146 FIXME("Depth filling not implemented by cpu_blit.\n");
5147 return WINED3DERR_INVALIDCALL;
5150 const struct blit_shader cpu_blit = {
5156 cpu_blit_color_fill,
5157 cpu_blit_depth_fill,
5160 static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
5161 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
5162 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
5164 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
5167 /* Source and/or destination need to be on the GL side */
5168 if (src_pool == WINED3DPOOL_SYSTEMMEM || dst_pool == WINED3DPOOL_SYSTEMMEM)
5173 case WINED3D_BLIT_OP_COLOR_BLIT:
5174 if (!((src_format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (src_usage & WINED3DUSAGE_RENDERTARGET)))
5176 if (!((dst_format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (dst_usage & WINED3DUSAGE_RENDERTARGET)))
5180 case WINED3D_BLIT_OP_DEPTH_BLIT:
5181 if (!(src_format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
5183 if (!(dst_format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
5191 if (!(src_format->id == dst_format->id
5192 || (is_identity_fixup(src_format->color_fixup)
5193 && is_identity_fixup(dst_format->color_fixup))))
5199 static ULONG WINAPI IWineGDISurfaceImpl_Release(IWineD3DSurface *iface)
5201 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5204 TRACE("Surface %p, container %p of type %#x.\n",
5205 surface, surface->container.u.base, surface->container.type);
5207 switch (surface->container.type)
5209 case WINED3D_CONTAINER_TEXTURE:
5210 return wined3d_texture_decref(surface->container.u.texture);
5212 case WINED3D_CONTAINER_SWAPCHAIN:
5213 return wined3d_swapchain_decref(surface->container.u.swapchain);
5216 ERR("Unhandled container type %#x.\n", surface->container.type);
5217 case WINED3D_CONTAINER_NONE:
5221 refcount = InterlockedDecrement(&surface->resource.ref);
5222 TRACE("%p decreasing refcount to %u.\n", surface, refcount);
5226 surface_gdi_cleanup(surface);
5227 surface->resource.parent_ops->wined3d_object_destroyed(surface->resource.parent);
5229 TRACE("Destroyed surface %p.\n", surface);
5230 HeapFree(GetProcessHeap(), 0, surface);
5236 static void WINAPI IWineGDISurfaceImpl_PreLoad(IWineD3DSurface *iface)
5238 ERR("(%p): PreLoad is not supported on X11 surfaces!\n", iface);
5239 ERR("(%p): Most likely the parent library did something wrong.\n", iface);
5240 ERR("(%p): Please report to wine-devel\n", iface);
5243 static HRESULT WINAPI IWineGDISurfaceImpl_Map(IWineD3DSurface *iface,
5244 WINED3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
5246 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5248 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
5249 iface, locked_rect, wine_dbgstr_rect(rect), flags);
5251 /* Already locked? */
5252 if (surface->flags & SFLAG_LOCKED)
5254 WARN("Surface already mapped.\n");
5255 /* What should I return here? */
5256 return WINED3DERR_INVALIDCALL;
5258 surface->flags |= SFLAG_LOCKED;
5260 if (!surface->resource.allocatedMemory)
5262 /* This happens on gdi surfaces if the application set a user pointer
5263 * and resets it. Recreate the DIB section. */
5264 IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
5265 surface->resource.allocatedMemory = surface->dib.bitmap_data;
5268 return IWineD3DBaseSurfaceImpl_Map(iface, locked_rect, rect, flags);
5271 static HRESULT WINAPI IWineGDISurfaceImpl_Unmap(IWineD3DSurface *iface)
5273 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5275 TRACE("iface %p.\n", iface);
5277 if (!(surface->flags & SFLAG_LOCKED))
5279 WARN("Trying to unmap unmapped surface.\n");
5280 return WINEDDERR_NOTLOCKED;
5283 /* Tell the swapchain to update the screen. */
5284 if (surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
5286 struct wined3d_swapchain *swapchain = surface->container.u.swapchain;
5287 if (surface == swapchain->front_buffer)
5289 x11_copy_to_screen(swapchain, &surface->lockedRect);
5293 surface->flags &= ~SFLAG_LOCKED;
5294 memset(&surface->lockedRect, 0, sizeof(RECT));
5299 /*****************************************************************************
5300 * IWineD3DSurface::Flip, GDI version
5302 * Flips 2 flipping enabled surfaces. Determining the 2 targets is done by
5303 * the parent library. This implementation changes the data pointers of the
5304 * surfaces and copies the new front buffer content to the screen
5307 * override: Flipping target(e.g. back buffer)
5310 * WINED3D_OK on success
5312 *****************************************************************************/
5313 static HRESULT WINAPI IWineGDISurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD flags)
5315 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5316 struct wined3d_swapchain *swapchain;
5319 if (surface->container.type != WINED3D_CONTAINER_SWAPCHAIN)
5321 ERR("Flipped surface is not on a swapchain\n");
5322 return WINEDDERR_NOTFLIPPABLE;
5325 swapchain = surface->container.u.swapchain;
5326 hr = wined3d_swapchain_present(swapchain, NULL, NULL, swapchain->win_handle, NULL, 0);
5331 static HRESULT WINAPI IWineGDISurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *dc)
5333 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5334 WINED3DLOCKED_RECT lock;
5338 TRACE("iface %p, dc %p.\n", iface, dc);
5340 if (!(surface->flags & SFLAG_DIBSECTION))
5342 WARN("DC not supported on this surface\n");
5343 return WINED3DERR_INVALIDCALL;
5346 if (surface->flags & SFLAG_USERPTR)
5348 ERR("Not supported on surfaces with application-provided memory.\n");
5349 return WINEDDERR_NODC;
5352 /* Give more detailed info for ddraw. */
5353 if (surface->flags & SFLAG_DCINUSE)
5354 return WINEDDERR_DCALREADYCREATED;
5356 /* Can't GetDC if the surface is locked. */
5357 if (surface->flags & SFLAG_LOCKED)
5358 return WINED3DERR_INVALIDCALL;
5360 memset(&lock, 0, sizeof(lock)); /* To be sure */
5362 /* Should have a DIB section already. */
5364 /* Map the surface. */
5365 hr = IWineD3DSurface_Map(iface, &lock, NULL, 0);
5368 ERR("IWineD3DSurface_Map failed, hr %#x.\n", hr);
5369 /* keep the dib section */
5373 if (surface->resource.format->id == WINED3DFMT_P8_UINT
5374 || surface->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
5376 const PALETTEENTRY *pal = NULL;
5378 if (surface->palette)
5380 pal = surface->palette->palents;
5384 struct wined3d_swapchain *swapchain = surface->resource.device->swapchains[0];
5385 IWineD3DSurfaceImpl *dds_primary = swapchain->front_buffer;
5387 if (dds_primary && dds_primary->palette)
5388 pal = dds_primary->palette->palents;
5395 for (i = 0; i < 256; ++i)
5397 col[i].rgbRed = pal[i].peRed;
5398 col[i].rgbGreen = pal[i].peGreen;
5399 col[i].rgbBlue = pal[i].peBlue;
5400 col[i].rgbReserved = 0;
5402 SetDIBColorTable(surface->hDC, 0, 256, col);
5406 surface->flags |= SFLAG_DCINUSE;
5409 TRACE("Returning dc %p.\n", *dc);
5414 static HRESULT WINAPI IWineGDISurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC dc)
5416 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5418 TRACE("iface %p, dc %p.\n", iface, dc);
5420 if (!(surface->flags & SFLAG_DCINUSE))
5421 return WINEDDERR_NODC;
5423 if (surface->hDC != dc)
5425 WARN("Application tries to release invalid DC %p, surface DC is %p.\n",
5427 return WINEDDERR_NODC;
5430 /* We locked first, so unlock now. */
5431 IWineD3DSurface_Unmap(iface);
5433 surface->flags &= ~SFLAG_DCINUSE;
5438 /*****************************************************************************
5439 * IWineD3DSurface::PrivateSetup, GDI version
5441 * Initializes the GDI surface, aka creates the DIB section we render to
5442 * The DIB section creation is done by calling GetDC, which will create the
5443 * section and releasing the dc to allow the app to use it. The dib section
5444 * will stay until the surface is released
5446 * GDI surfaces do not need to be a power of 2 in size, so the pow2 sizes
5447 * are set to the real sizes to save memory. The NONPOW2 flag is unset to
5448 * avoid confusion in the shared surface code.
5451 * WINED3D_OK on success
5452 * The return values of called methods on failure
5454 *****************************************************************************/
5455 static HRESULT WINAPI IWineGDISurfaceImpl_PrivateSetup(IWineD3DSurface *iface)
5457 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5460 surface->surface_ops = &gdi_surface_ops;
5462 if (surface->resource.usage & WINED3DUSAGE_OVERLAY)
5464 ERR("Overlays not yet supported by GDI surfaces.\n");
5465 return WINED3DERR_INVALIDCALL;
5468 /* Sysmem textures have memory already allocated - release it,
5469 * this avoids an unnecessary memcpy. */
5470 hr = IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
5473 HeapFree(GetProcessHeap(), 0, surface->resource.heapMemory);
5474 surface->resource.heapMemory = NULL;
5475 surface->resource.allocatedMemory = surface->dib.bitmap_data;
5478 /* We don't mind the nonpow2 stuff in GDI */
5479 surface->pow2Width = surface->resource.width;
5480 surface->pow2Height = surface->resource.height;
5485 static HRESULT WINAPI IWineGDISurfaceImpl_SetMem(IWineD3DSurface *iface, void *mem)
5487 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5489 /* Render targets depend on their hdc, and we can't create an hdc on a user pointer. */
5490 if (surface->resource.usage & WINED3DUSAGE_RENDERTARGET)
5492 ERR("Not supported on render targets.\n");
5493 return WINED3DERR_INVALIDCALL;
5496 if (surface->flags & (SFLAG_LOCKED | SFLAG_DCINUSE))
5498 WARN("Surface is locked or the DC is in use.\n");
5499 return WINED3DERR_INVALIDCALL;
5502 if (mem && mem != surface->resource.allocatedMemory)
5504 void *release = NULL;
5506 /* Do I have to copy the old surface content? */
5507 if (surface->flags & SFLAG_DIBSECTION)
5509 SelectObject(surface->hDC, surface->dib.holdbitmap);
5510 DeleteDC(surface->hDC);
5511 /* Release the DIB section. */
5512 DeleteObject(surface->dib.DIBsection);
5513 surface->dib.bitmap_data = NULL;
5514 surface->resource.allocatedMemory = NULL;
5515 surface->hDC = NULL;
5516 surface->flags &= ~SFLAG_DIBSECTION;
5518 else if (!(surface->flags & SFLAG_USERPTR))
5520 release = surface->resource.allocatedMemory;
5522 surface->resource.allocatedMemory = mem;
5523 surface->flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
5525 /* Now free the old memory, if any. */
5526 HeapFree(GetProcessHeap(), 0, release);
5528 else if (surface->flags & SFLAG_USERPTR)
5530 /* Map() and GetDC() will re-create the dib section and allocated memory. */
5531 surface->resource.allocatedMemory = NULL;
5532 surface->flags &= ~SFLAG_USERPTR;
5538 static const IWineD3DSurfaceVtbl IWineGDISurface_Vtbl =
5541 IWineD3DBaseSurfaceImpl_QueryInterface,
5542 IWineD3DBaseSurfaceImpl_AddRef,
5543 IWineGDISurfaceImpl_Release,
5544 /* IWineD3DResource */
5545 IWineD3DBaseSurfaceImpl_GetParent,
5546 IWineD3DBaseSurfaceImpl_SetPrivateData,
5547 IWineD3DBaseSurfaceImpl_GetPrivateData,
5548 IWineD3DBaseSurfaceImpl_FreePrivateData,
5549 IWineD3DBaseSurfaceImpl_SetPriority,
5550 IWineD3DBaseSurfaceImpl_GetPriority,
5551 IWineGDISurfaceImpl_PreLoad,
5552 /* IWineD3DSurface */
5553 IWineD3DBaseSurfaceImpl_GetResource,
5554 IWineGDISurfaceImpl_Map,
5555 IWineGDISurfaceImpl_Unmap,
5556 IWineGDISurfaceImpl_GetDC,
5557 IWineGDISurfaceImpl_ReleaseDC,
5558 IWineGDISurfaceImpl_Flip,
5559 IWineD3DBaseSurfaceImpl_Blt,
5560 IWineD3DBaseSurfaceImpl_GetBltStatus,
5561 IWineD3DBaseSurfaceImpl_GetFlipStatus,
5562 IWineD3DBaseSurfaceImpl_IsLost,
5563 IWineD3DBaseSurfaceImpl_Restore,
5564 IWineD3DBaseSurfaceImpl_BltFast,
5565 IWineD3DBaseSurfaceImpl_GetPalette,
5566 IWineD3DBaseSurfaceImpl_SetPalette,
5567 IWineD3DBaseSurfaceImpl_SetColorKey,
5568 IWineD3DBaseSurfaceImpl_GetPitch,
5569 IWineGDISurfaceImpl_SetMem,
5570 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
5571 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
5572 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
5573 IWineD3DBaseSurfaceImpl_UpdateOverlay,
5574 IWineD3DBaseSurfaceImpl_SetClipper,
5575 IWineD3DBaseSurfaceImpl_GetClipper,
5577 IWineD3DBaseSurfaceImpl_SetFormat,
5578 IWineGDISurfaceImpl_PrivateSetup,
5581 HRESULT surface_init(IWineD3DSurfaceImpl *surface, WINED3DSURFTYPE surface_type, UINT alignment,
5582 UINT width, UINT height, UINT level, BOOL lockable, BOOL discard, WINED3DMULTISAMPLE_TYPE multisample_type,
5583 UINT multisample_quality, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
5584 WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
5586 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
5587 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
5588 void (*cleanup)(IWineD3DSurfaceImpl *This);
5589 unsigned int resource_size;
5592 if (multisample_quality > 0)
5594 FIXME("multisample_quality set to %u, substituting 0.\n", multisample_quality);
5595 multisample_quality = 0;
5598 /* Quick lockable sanity check.
5599 * TODO: remove this after surfaces, usage and lockability have been debugged properly
5600 * this function is too deep to need to care about things like this.
5601 * Levels need to be checked too, since they all affect what can be done. */
5604 case WINED3DPOOL_SCRATCH:
5607 FIXME("Called with a pool of SCRATCH and a lockable of FALSE "
5608 "which are mutually exclusive, setting lockable to TRUE.\n");
5613 case WINED3DPOOL_SYSTEMMEM:
5615 FIXME("Called with a pool of SYSTEMMEM and a lockable of FALSE, this is acceptable but unexpected.\n");
5618 case WINED3DPOOL_MANAGED:
5619 if (usage & WINED3DUSAGE_DYNAMIC)
5620 FIXME("Called with a pool of MANAGED and a usage of DYNAMIC which are mutually exclusive.\n");
5623 case WINED3DPOOL_DEFAULT:
5624 if (lockable && !(usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
5625 WARN("Creating a lockable surface with a POOL of DEFAULT, that doesn't specify DYNAMIC usage.\n");
5629 FIXME("Unknown pool %#x.\n", pool);
5633 if (usage & WINED3DUSAGE_RENDERTARGET && pool != WINED3DPOOL_DEFAULT)
5634 FIXME("Trying to create a render target that isn't in the default pool.\n");
5636 /* FIXME: Check that the format is supported by the device. */
5638 resource_size = wined3d_format_calculate_size(format, alignment, width, height);
5640 return WINED3DERR_INVALIDCALL;
5642 surface->surface_type = surface_type;
5644 /* Look at the implementation and set the correct Vtable. */
5645 switch (surface_type)
5647 case SURFACE_OPENGL:
5648 surface->lpVtbl = &IWineD3DSurface_Vtbl;
5649 cleanup = surface_cleanup;
5653 surface->lpVtbl = &IWineGDISurface_Vtbl;
5654 cleanup = surface_gdi_cleanup;
5658 ERR("Requested unknown surface implementation %#x.\n", surface_type);
5659 return WINED3DERR_INVALIDCALL;
5662 hr = resource_init(&surface->resource, device, WINED3DRTYPE_SURFACE, format,
5663 multisample_type, multisample_quality, usage, pool, width, height, 1,
5664 resource_size, parent, parent_ops, &surface_resource_ops);
5667 WARN("Failed to initialize resource, returning %#x.\n", hr);
5671 /* "Standalone" surface. */
5672 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
5674 surface->texture_level = level;
5675 list_init(&surface->overlays);
5678 surface->flags = SFLAG_NORMCOORD; /* Default to normalized coords. */
5680 surface->flags |= SFLAG_DISCARD;
5681 if (lockable || format_id == WINED3DFMT_D16_LOCKABLE)
5682 surface->flags |= SFLAG_LOCKABLE;
5684 /* Mark the texture as dirty so that it gets loaded first time around. */
5685 surface_add_dirty_rect(surface, NULL);
5686 list_init(&surface->renderbuffers);
5688 TRACE("surface %p, memory %p, size %u\n",
5689 surface, surface->resource.allocatedMemory, surface->resource.size);
5691 /* Call the private setup routine */
5692 hr = IWineD3DSurface_PrivateSetup((IWineD3DSurface *)surface);
5695 ERR("Private setup failed, returning %#x\n", hr);