2 * IWineD3DSurface Implementation
4 * Copyright 1998 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2002-2005 Jason Edmeades
7 * Copyright 2002-2003 Raphael Junqueira
8 * Copyright 2004 Christian Costa
9 * Copyright 2005 Oliver Stieber
10 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
11 * Copyright 2007-2008 Henri Verbeet
12 * Copyright 2006-2008 Roderick Colenbrander
13 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/port.h"
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
35 WINE_DECLARE_DEBUG_CHANNEL(d3d);
37 static void surface_cleanup(IWineD3DSurfaceImpl *This)
39 TRACE("(%p) : Cleaning up.\n", This);
41 if (This->texture_name || (This->flags & SFLAG_PBO) || !list_empty(&This->renderbuffers))
43 const struct wined3d_gl_info *gl_info;
44 renderbuffer_entry_t *entry, *entry2;
45 struct wined3d_context *context;
47 context = context_acquire(This->resource.device, NULL);
48 gl_info = context->gl_info;
52 if (This->texture_name)
54 TRACE("Deleting texture %u.\n", This->texture_name);
55 glDeleteTextures(1, &This->texture_name);
58 if (This->flags & SFLAG_PBO)
60 TRACE("Deleting PBO %u.\n", This->pbo);
61 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
64 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry)
66 TRACE("Deleting renderbuffer %u.\n", entry->id);
67 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
68 HeapFree(GetProcessHeap(), 0, entry);
73 context_release(context);
76 if (This->flags & SFLAG_DIBSECTION)
79 SelectObject(This->hDC, This->dib.holdbitmap);
81 /* Release the DIB section. */
82 DeleteObject(This->dib.DIBsection);
83 This->dib.bitmap_data = NULL;
84 This->resource.allocatedMemory = NULL;
87 if (This->flags & SFLAG_USERPTR) IWineD3DSurface_SetMem((IWineD3DSurface *)This, NULL);
88 if (This->overlay_dest) list_remove(&This->overlay_entry);
90 HeapFree(GetProcessHeap(), 0, This->palette9);
92 resource_cleanup((IWineD3DResource *)This);
95 void surface_set_container(IWineD3DSurfaceImpl *surface, enum wined3d_container_type type, IWineD3DBase *container)
97 TRACE("surface %p, container %p.\n", surface, container);
99 if (!container && type != WINED3D_CONTAINER_NONE)
100 ERR("Setting NULL container of type %#x.\n", type);
102 if (type == WINED3D_CONTAINER_SWAPCHAIN)
104 surface->get_drawable_size = get_drawable_size_swapchain;
108 switch (wined3d_settings.offscreen_rendering_mode)
111 surface->get_drawable_size = get_drawable_size_fbo;
115 surface->get_drawable_size = get_drawable_size_backbuffer;
119 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
124 surface->container.type = type;
125 surface->container.u.base = container;
132 enum tex_types tex_type;
133 GLfloat coords[4][3];
144 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct float_rect *f)
146 f->l = ((r->left * 2.0f) / w) - 1.0f;
147 f->t = ((r->top * 2.0f) / h) - 1.0f;
148 f->r = ((r->right * 2.0f) / w) - 1.0f;
149 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
152 static void surface_get_blt_info(GLenum target, const RECT *rect, GLsizei w, GLsizei h, struct blt_info *info)
154 GLfloat (*coords)[3] = info->coords;
160 FIXME("Unsupported texture target %#x\n", target);
161 /* Fall back to GL_TEXTURE_2D */
163 info->binding = GL_TEXTURE_BINDING_2D;
164 info->bind_target = GL_TEXTURE_2D;
165 info->tex_type = tex_2d;
166 coords[0][0] = (float)rect->left / w;
167 coords[0][1] = (float)rect->top / h;
170 coords[1][0] = (float)rect->right / w;
171 coords[1][1] = (float)rect->top / h;
174 coords[2][0] = (float)rect->left / w;
175 coords[2][1] = (float)rect->bottom / h;
178 coords[3][0] = (float)rect->right / w;
179 coords[3][1] = (float)rect->bottom / h;
183 case GL_TEXTURE_RECTANGLE_ARB:
184 info->binding = GL_TEXTURE_BINDING_RECTANGLE_ARB;
185 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
186 info->tex_type = tex_rect;
187 coords[0][0] = rect->left; coords[0][1] = rect->top; coords[0][2] = 0.0f;
188 coords[1][0] = rect->right; coords[1][1] = rect->top; coords[1][2] = 0.0f;
189 coords[2][0] = rect->left; coords[2][1] = rect->bottom; coords[2][2] = 0.0f;
190 coords[3][0] = rect->right; coords[3][1] = rect->bottom; coords[3][2] = 0.0f;
193 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
194 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
195 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
196 info->tex_type = tex_cube;
197 cube_coords_float(rect, w, h, &f);
199 coords[0][0] = 1.0f; coords[0][1] = -f.t; coords[0][2] = -f.l;
200 coords[1][0] = 1.0f; coords[1][1] = -f.t; coords[1][2] = -f.r;
201 coords[2][0] = 1.0f; coords[2][1] = -f.b; coords[2][2] = -f.l;
202 coords[3][0] = 1.0f; coords[3][1] = -f.b; coords[3][2] = -f.r;
205 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
206 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
207 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
208 info->tex_type = tex_cube;
209 cube_coords_float(rect, w, h, &f);
211 coords[0][0] = -1.0f; coords[0][1] = -f.t; coords[0][2] = f.l;
212 coords[1][0] = -1.0f; coords[1][1] = -f.t; coords[1][2] = f.r;
213 coords[2][0] = -1.0f; coords[2][1] = -f.b; coords[2][2] = f.l;
214 coords[3][0] = -1.0f; coords[3][1] = -f.b; coords[3][2] = f.r;
217 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
218 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
219 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
220 info->tex_type = tex_cube;
221 cube_coords_float(rect, w, h, &f);
223 coords[0][0] = f.l; coords[0][1] = 1.0f; coords[0][2] = f.t;
224 coords[1][0] = f.r; coords[1][1] = 1.0f; coords[1][2] = f.t;
225 coords[2][0] = f.l; coords[2][1] = 1.0f; coords[2][2] = f.b;
226 coords[3][0] = f.r; coords[3][1] = 1.0f; coords[3][2] = f.b;
229 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
230 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
231 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
232 info->tex_type = tex_cube;
233 cube_coords_float(rect, w, h, &f);
235 coords[0][0] = f.l; coords[0][1] = -1.0f; coords[0][2] = -f.t;
236 coords[1][0] = f.r; coords[1][1] = -1.0f; coords[1][2] = -f.t;
237 coords[2][0] = f.l; coords[2][1] = -1.0f; coords[2][2] = -f.b;
238 coords[3][0] = f.r; coords[3][1] = -1.0f; coords[3][2] = -f.b;
241 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
242 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
243 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
244 info->tex_type = tex_cube;
245 cube_coords_float(rect, w, h, &f);
247 coords[0][0] = f.l; coords[0][1] = -f.t; coords[0][2] = 1.0f;
248 coords[1][0] = f.r; coords[1][1] = -f.t; coords[1][2] = 1.0f;
249 coords[2][0] = f.l; coords[2][1] = -f.b; coords[2][2] = 1.0f;
250 coords[3][0] = f.r; coords[3][1] = -f.b; coords[3][2] = 1.0f;
253 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
254 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
255 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
256 info->tex_type = tex_cube;
257 cube_coords_float(rect, w, h, &f);
259 coords[0][0] = -f.l; coords[0][1] = -f.t; coords[0][2] = -1.0f;
260 coords[1][0] = -f.r; coords[1][1] = -f.t; coords[1][2] = -1.0f;
261 coords[2][0] = -f.l; coords[2][1] = -f.b; coords[2][2] = -1.0f;
262 coords[3][0] = -f.r; coords[3][1] = -f.b; coords[3][2] = -1.0f;
267 static inline void surface_get_rect(IWineD3DSurfaceImpl *This, const RECT *rect_in, RECT *rect_out)
270 *rect_out = *rect_in;
275 rect_out->right = This->currentDesc.Width;
276 rect_out->bottom = This->currentDesc.Height;
280 /* GL locking and context activation is done by the caller */
281 void draw_textured_quad(IWineD3DSurfaceImpl *src_surface, const RECT *src_rect, const RECT *dst_rect, WINED3DTEXTUREFILTERTYPE Filter)
283 struct blt_info info;
285 surface_get_blt_info(src_surface->texture_target, src_rect, src_surface->pow2Width, src_surface->pow2Height, &info);
287 glEnable(info.bind_target);
288 checkGLcall("glEnable(bind_target)");
290 /* Bind the texture */
291 glBindTexture(info.bind_target, src_surface->texture_name);
292 checkGLcall("glBindTexture");
294 /* Filtering for StretchRect */
295 glTexParameteri(info.bind_target, GL_TEXTURE_MAG_FILTER,
296 wined3d_gl_mag_filter(magLookup, Filter));
297 checkGLcall("glTexParameteri");
298 glTexParameteri(info.bind_target, GL_TEXTURE_MIN_FILTER,
299 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
300 checkGLcall("glTexParameteri");
301 glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
302 glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
303 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
304 checkGLcall("glTexEnvi");
307 glBegin(GL_TRIANGLE_STRIP);
308 glTexCoord3fv(info.coords[0]);
309 glVertex2i(dst_rect->left, dst_rect->top);
311 glTexCoord3fv(info.coords[1]);
312 glVertex2i(dst_rect->right, dst_rect->top);
314 glTexCoord3fv(info.coords[2]);
315 glVertex2i(dst_rect->left, dst_rect->bottom);
317 glTexCoord3fv(info.coords[3]);
318 glVertex2i(dst_rect->right, dst_rect->bottom);
321 /* Unbind the texture */
322 glBindTexture(info.bind_target, 0);
323 checkGLcall("glBindTexture(info->bind_target, 0)");
325 /* We changed the filtering settings on the texture. Inform the
326 * container about this to get the filters reset properly next draw. */
327 if (src_surface->container.type == WINED3D_CONTAINER_TEXTURE)
329 IWineD3DBaseTextureImpl *texture = src_surface->container.u.texture;
330 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
331 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
332 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
336 HRESULT surface_init(IWineD3DSurfaceImpl *surface, WINED3DSURFTYPE surface_type, UINT alignment,
337 UINT width, UINT height, UINT level, BOOL lockable, BOOL discard, WINED3DMULTISAMPLE_TYPE multisample_type,
338 UINT multisample_quality, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
339 WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
341 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
342 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
343 void (*cleanup)(IWineD3DSurfaceImpl *This);
344 unsigned int resource_size;
347 if (multisample_quality > 0)
349 FIXME("multisample_quality set to %u, substituting 0\n", multisample_quality);
350 multisample_quality = 0;
353 /* FIXME: Check that the format is supported by the device. */
355 resource_size = wined3d_format_calculate_size(format, alignment, width, height);
356 if (!resource_size) return WINED3DERR_INVALIDCALL;
358 /* Look at the implementation and set the correct Vtable. */
359 switch (surface_type)
362 surface->lpVtbl = &IWineD3DSurface_Vtbl;
363 cleanup = surface_cleanup;
367 surface->lpVtbl = &IWineGDISurface_Vtbl;
368 cleanup = surface_gdi_cleanup;
372 ERR("Requested unknown surface implementation %#x.\n", surface_type);
373 return WINED3DERR_INVALIDCALL;
376 hr = resource_init((IWineD3DResource *)surface, WINED3DRTYPE_SURFACE,
377 device, resource_size, usage, format, pool, parent, parent_ops);
380 WARN("Failed to initialize resource, returning %#x.\n", hr);
384 /* "Standalone" surface. */
385 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
387 surface->currentDesc.Width = width;
388 surface->currentDesc.Height = height;
389 surface->currentDesc.MultiSampleType = multisample_type;
390 surface->currentDesc.MultiSampleQuality = multisample_quality;
391 surface->texture_level = level;
392 list_init(&surface->overlays);
395 surface->flags = SFLAG_NORMCOORD; /* Default to normalized coords. */
396 if (discard) surface->flags |= SFLAG_DISCARD;
397 if (lockable || format_id == WINED3DFMT_D16_LOCKABLE) surface->flags |= SFLAG_LOCKABLE;
399 /* Quick lockable sanity check.
400 * TODO: remove this after surfaces, usage and lockability have been debugged properly
401 * this function is too deep to need to care about things like this.
402 * Levels need to be checked too, since they all affect what can be done. */
405 case WINED3DPOOL_SCRATCH:
408 FIXME("Called with a pool of SCRATCH and a lockable of FALSE "
409 "which are mutually exclusive, setting lockable to TRUE.\n");
414 case WINED3DPOOL_SYSTEMMEM:
416 FIXME("Called with a pool of SYSTEMMEM and a lockable of FALSE, this is acceptable but unexpected.\n");
419 case WINED3DPOOL_MANAGED:
420 if (usage & WINED3DUSAGE_DYNAMIC)
421 FIXME("Called with a pool of MANAGED and a usage of DYNAMIC which are mutually exclusive.\n");
424 case WINED3DPOOL_DEFAULT:
425 if (lockable && !(usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
426 WARN("Creating a lockable surface with a POOL of DEFAULT, that doesn't specify DYNAMIC usage.\n");
430 FIXME("Unknown pool %#x.\n", pool);
434 if (usage & WINED3DUSAGE_RENDERTARGET && pool != WINED3DPOOL_DEFAULT)
436 FIXME("Trying to create a render target that isn't in the default pool.\n");
439 /* Mark the texture as dirty so that it gets loaded first time around. */
440 surface_add_dirty_rect(surface, NULL);
441 list_init(&surface->renderbuffers);
443 TRACE("surface %p, memory %p, size %u\n", surface, surface->resource.allocatedMemory, surface->resource.size);
445 /* Call the private setup routine */
446 hr = IWineD3DSurface_PrivateSetup((IWineD3DSurface *)surface);
449 ERR("Private setup failed, returning %#x\n", hr);
457 static void surface_force_reload(IWineD3DSurfaceImpl *surface)
459 surface->flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
462 void surface_set_texture_name(IWineD3DSurfaceImpl *surface, GLuint new_name, BOOL srgb)
467 TRACE("surface %p, new_name %u, srgb %#x.\n", surface, new_name, srgb);
471 name = &surface->texture_name_srgb;
472 flag = SFLAG_INSRGBTEX;
476 name = &surface->texture_name;
477 flag = SFLAG_INTEXTURE;
480 if (!*name && new_name)
482 /* FIXME: We shouldn't need to remove SFLAG_INTEXTURE if the
483 * surface has no texture name yet. See if we can get rid of this. */
484 if (surface->flags & flag)
485 ERR("Surface has %s set, but no texture name.\n", debug_surflocation(flag));
486 surface_modify_location(surface, flag, FALSE);
490 surface_force_reload(surface);
493 void surface_set_texture_target(IWineD3DSurfaceImpl *surface, GLenum target)
495 TRACE("surface %p, target %#x.\n", surface, target);
497 if (surface->texture_target != target)
499 if (target == GL_TEXTURE_RECTANGLE_ARB)
501 surface->flags &= ~SFLAG_NORMCOORD;
503 else if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
505 surface->flags |= SFLAG_NORMCOORD;
508 surface->texture_target = target;
509 surface_force_reload(surface);
512 /* Context activation is done by the caller. */
513 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl *This, BOOL srgb) {
514 DWORD active_sampler;
516 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
517 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
518 * gl states. The current texture unit should always be a valid one.
520 * To be more specific, this is tricky because we can implicitly be called
521 * from sampler() in state.c. This means we can't touch anything other than
522 * whatever happens to be the currently active texture, or we would risk
523 * marking already applied sampler states dirty again.
525 * TODO: Track the current active texture per GL context instead of using glGet
527 GLint active_texture;
529 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
531 active_sampler = This->resource.device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
533 if (active_sampler != WINED3D_UNMAPPED_STAGE)
535 IWineD3DDeviceImpl_MarkStateDirty(This->resource.device, STATE_SAMPLER(active_sampler));
537 IWineD3DSurface_BindTexture((IWineD3DSurface *)This, srgb);
540 /* This function checks if the primary render target uses the 8bit paletted format. */
541 static BOOL primary_render_target_is_p8(IWineD3DDeviceImpl *device)
543 if (device->render_targets && device->render_targets[0])
545 IWineD3DSurfaceImpl *render_target = device->render_targets[0];
546 if ((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET)
547 && (render_target->resource.format->id == WINED3DFMT_P8_UINT))
553 /* This call just downloads data, the caller is responsible for binding the
554 * correct texture. */
555 /* Context activation is done by the caller. */
556 static void surface_download_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info)
558 const struct wined3d_format *format = This->resource.format;
560 /* Only support read back of converted P8 surfaces */
561 if (This->flags & SFLAG_CONVERTED && format->id != WINED3DFMT_P8_UINT)
563 FIXME("Readback conversion not supported for format %s.\n", debug_d3dformat(format->id));
569 if (format->flags & WINED3DFMT_FLAG_COMPRESSED)
571 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p.\n",
572 This, This->texture_level, format->glFormat, format->glType,
573 This->resource.allocatedMemory);
575 if (This->flags & SFLAG_PBO)
577 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
578 checkGLcall("glBindBufferARB");
579 GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target, This->texture_level, NULL));
580 checkGLcall("glGetCompressedTexImageARB");
581 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
582 checkGLcall("glBindBufferARB");
586 GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target,
587 This->texture_level, This->resource.allocatedMemory));
588 checkGLcall("glGetCompressedTexImageARB");
594 GLenum gl_format = format->glFormat;
595 GLenum gl_type = format->glType;
599 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
600 if (format->id == WINED3DFMT_P8_UINT && primary_render_target_is_p8(This->resource.device))
602 gl_format = GL_ALPHA;
603 gl_type = GL_UNSIGNED_BYTE;
606 if (This->flags & SFLAG_NONPOW2)
608 unsigned char alignment = This->resource.device->surface_alignment;
609 src_pitch = format->byte_count * This->pow2Width;
610 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
611 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
612 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
614 mem = This->resource.allocatedMemory;
617 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n",
618 This, This->texture_level, gl_format, gl_type, mem);
620 if (This->flags & SFLAG_PBO)
622 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
623 checkGLcall("glBindBufferARB");
625 glGetTexImage(This->texture_target, This->texture_level, gl_format, gl_type, NULL);
626 checkGLcall("glGetTexImage");
628 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
629 checkGLcall("glBindBufferARB");
631 glGetTexImage(This->texture_target, This->texture_level, gl_format, gl_type, mem);
632 checkGLcall("glGetTexImage");
636 if (This->flags & SFLAG_NONPOW2)
638 const BYTE *src_data;
642 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
643 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
644 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
646 * We're doing this...
648 * instead of boxing the texture :
649 * |<-texture width ->| -->pow2width| /\
650 * |111111111111111111| | |
651 * |222 Texture 222222| boxed empty | texture height
652 * |3333 Data 33333333| | |
653 * |444444444444444444| | \/
654 * ----------------------------------- |
655 * | boxed empty | boxed empty | pow2height
657 * -----------------------------------
660 * we're repacking the data to the expected texture width
662 * |<-texture width ->| -->pow2width| /\
663 * |111111111111111111222222222222222| |
664 * |222333333333333333333444444444444| texture height
668 * | empty | pow2height
670 * -----------------------------------
674 * |<-texture width ->| /\
675 * |111111111111111111|
676 * |222222222222222222|texture height
677 * |333333333333333333|
678 * |444444444444444444| \/
679 * --------------------
681 * this also means that any references to allocatedMemory should work with the data as if were a
682 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
684 * internally the texture is still stored in a boxed format so any references to textureName will
685 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
687 * Performance should not be an issue, because applications normally do not lock the surfaces when
688 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
689 * and doesn't have to be re-read.
692 dst_data = This->resource.allocatedMemory;
693 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
694 for (y = 1 ; y < This->currentDesc.Height; y++) {
695 /* skip the first row */
696 src_data += src_pitch;
697 dst_data += dst_pitch;
698 memcpy(dst_data, src_data, dst_pitch);
701 HeapFree(GetProcessHeap(), 0, mem);
705 /* Surface has now been downloaded */
706 This->flags |= SFLAG_INSYSMEM;
709 /* This call just uploads data, the caller is responsible for binding the
710 * correct texture. */
711 /* Context activation is done by the caller. */
712 static void surface_upload_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
713 const struct wined3d_format *format, BOOL srgb, const GLvoid *data)
715 GLsizei width = This->currentDesc.Width;
716 GLsizei height = This->currentDesc.Height;
721 internal = format->glGammaInternal;
723 else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET && surface_is_offscreen(This))
725 internal = format->rtInternal;
729 internal = format->glInternal;
732 TRACE("This %p, internal %#x, width %d, height %d, format %#x, type %#x, data %p.\n",
733 This, internal, width, height, format->glFormat, format->glType, data);
734 TRACE("target %#x, level %u, resource size %u.\n",
735 This->texture_target, This->texture_level, This->resource.size);
737 if (format->heightscale != 1.0f && format->heightscale != 0.0f) height *= format->heightscale;
741 if (This->flags & SFLAG_PBO)
743 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
744 checkGLcall("glBindBufferARB");
746 TRACE("(%p) pbo: %#x, data: %p.\n", This, This->pbo, data);
750 if (format->flags & WINED3DFMT_FLAG_COMPRESSED)
752 TRACE("Calling glCompressedTexSubImage2DARB.\n");
754 GL_EXTCALL(glCompressedTexSubImage2DARB(This->texture_target, This->texture_level,
755 0, 0, width, height, internal, This->resource.size, data));
756 checkGLcall("glCompressedTexSubImage2DARB");
760 TRACE("Calling glTexSubImage2D.\n");
762 glTexSubImage2D(This->texture_target, This->texture_level,
763 0, 0, width, height, format->glFormat, format->glType, data);
764 checkGLcall("glTexSubImage2D");
767 if (This->flags & SFLAG_PBO)
769 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
770 checkGLcall("glBindBufferARB");
775 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
777 IWineD3DDeviceImpl *device = This->resource.device;
780 for (i = 0; i < device->numContexts; ++i)
782 context_surface_update(device->contexts[i], This);
787 /* This call just allocates the texture, the caller is responsible for binding
788 * the correct texture. */
789 /* Context activation is done by the caller. */
790 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
791 const struct wined3d_format *format, BOOL srgb)
793 BOOL enable_client_storage = FALSE;
794 GLsizei width = This->pow2Width;
795 GLsizei height = This->pow2Height;
796 const BYTE *mem = NULL;
801 internal = format->glGammaInternal;
803 else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET && surface_is_offscreen(This))
805 internal = format->rtInternal;
809 internal = format->glInternal;
812 if (format->heightscale != 1.0f && format->heightscale != 0.0f) height *= format->heightscale;
814 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",
815 This, This->texture_target, This->texture_level, debug_d3dformat(format->id),
816 internal, width, height, format->glFormat, format->glType);
820 if (gl_info->supported[APPLE_CLIENT_STORAGE])
822 if (This->flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_CONVERTED)
823 || !This->resource.allocatedMemory)
825 /* In some cases we want to disable client storage.
826 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
827 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
828 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
829 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
831 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
832 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
833 This->flags &= ~SFLAG_CLIENT;
834 enable_client_storage = TRUE;
838 This->flags |= SFLAG_CLIENT;
840 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
841 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
843 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
847 if (format->flags & WINED3DFMT_FLAG_COMPRESSED && mem)
849 GL_EXTCALL(glCompressedTexImage2DARB(This->texture_target, This->texture_level,
850 internal, width, height, 0, This->resource.size, mem));
851 checkGLcall("glCompressedTexImage2DARB");
855 glTexImage2D(This->texture_target, This->texture_level,
856 internal, width, height, 0, format->glFormat, format->glType, mem);
857 checkGLcall("glTexImage2D");
860 if(enable_client_storage) {
861 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
862 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
867 /* In D3D the depth stencil dimensions have to be greater than or equal to the
868 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
869 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
870 /* GL locking is done by the caller */
871 void surface_set_compatible_renderbuffer(IWineD3DSurfaceImpl *surface, unsigned int width, unsigned int height)
873 const struct wined3d_gl_info *gl_info = &surface->resource.device->adapter->gl_info;
874 renderbuffer_entry_t *entry;
875 GLuint renderbuffer = 0;
876 unsigned int src_width, src_height;
878 src_width = surface->pow2Width;
879 src_height = surface->pow2Height;
881 /* A depth stencil smaller than the render target is not valid */
882 if (width > src_width || height > src_height) return;
884 /* Remove any renderbuffer set if the sizes match */
885 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT]
886 || (width == src_width && height == src_height))
888 surface->current_renderbuffer = NULL;
892 /* Look if we've already got a renderbuffer of the correct dimensions */
893 LIST_FOR_EACH_ENTRY(entry, &surface->renderbuffers, renderbuffer_entry_t, entry)
895 if (entry->width == width && entry->height == height)
897 renderbuffer = entry->id;
898 surface->current_renderbuffer = entry;
905 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
906 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
907 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER,
908 surface->resource.format->glInternal, width, height);
910 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
911 entry->width = width;
912 entry->height = height;
913 entry->id = renderbuffer;
914 list_add_head(&surface->renderbuffers, &entry->entry);
916 surface->current_renderbuffer = entry;
919 checkGLcall("set_compatible_renderbuffer");
922 GLenum surface_get_gl_buffer(IWineD3DSurfaceImpl *surface)
924 IWineD3DSwapChainImpl *swapchain = surface->container.u.swapchain;
926 TRACE("surface %p.\n", surface);
928 if (surface->container.type != WINED3D_CONTAINER_SWAPCHAIN)
930 ERR("Surface %p is not on a swapchain.\n", surface);
934 if (swapchain->back_buffers && swapchain->back_buffers[0] == surface)
936 if (swapchain->render_to_fbo)
938 TRACE("Returning GL_COLOR_ATTACHMENT0\n");
939 return GL_COLOR_ATTACHMENT0;
941 TRACE("Returning GL_BACK\n");
944 else if (surface == swapchain->front_buffer)
946 TRACE("Returning GL_FRONT\n");
950 FIXME("Higher back buffer, returning GL_BACK\n");
954 /* Slightly inefficient way to handle multiple dirty rects but it works :) */
955 void surface_add_dirty_rect(IWineD3DSurfaceImpl *surface, const RECT *dirty_rect)
957 TRACE("surface %p, dirty_rect %s.\n", surface, wine_dbgstr_rect(dirty_rect));
959 if (!(surface->flags & SFLAG_INSYSMEM) && (surface->flags & SFLAG_INTEXTURE))
960 /* No partial locking for textures yet. */
961 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
963 surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
966 surface->dirtyRect.left = min(surface->dirtyRect.left, dirty_rect->left);
967 surface->dirtyRect.top = min(surface->dirtyRect.top, dirty_rect->top);
968 surface->dirtyRect.right = max(surface->dirtyRect.right, dirty_rect->right);
969 surface->dirtyRect.bottom = max(surface->dirtyRect.bottom, dirty_rect->bottom);
973 surface->dirtyRect.left = 0;
974 surface->dirtyRect.top = 0;
975 surface->dirtyRect.right = surface->currentDesc.Width;
976 surface->dirtyRect.bottom = surface->currentDesc.Height;
979 /* if the container is a basetexture then mark it dirty. */
980 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
982 TRACE("Passing to container.\n");
983 IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture *)surface->container.u.texture, TRUE);
987 static BOOL surface_convert_color_to_float(IWineD3DSurfaceImpl *surface, DWORD color, WINED3DCOLORVALUE *float_color)
989 const struct wined3d_format *format = surface->resource.format;
990 IWineD3DDeviceImpl *device = surface->resource.device;
994 case WINED3DFMT_P8_UINT:
995 if (surface->palette)
997 float_color->r = surface->palette->palents[color].peRed / 255.0f;
998 float_color->g = surface->palette->palents[color].peGreen / 255.0f;
999 float_color->b = surface->palette->palents[color].peBlue / 255.0f;
1003 float_color->r = 0.0f;
1004 float_color->g = 0.0f;
1005 float_color->b = 0.0f;
1007 float_color->a = primary_render_target_is_p8(device) ? color / 255.0f : 1.0f;
1010 case WINED3DFMT_B5G6R5_UNORM:
1011 float_color->r = ((color >> 11) & 0x1f) / 31.0f;
1012 float_color->g = ((color >> 5) & 0x3f) / 63.0f;
1013 float_color->b = (color & 0x1f) / 31.0f;
1014 float_color->a = 1.0f;
1017 case WINED3DFMT_B8G8R8_UNORM:
1018 case WINED3DFMT_B8G8R8X8_UNORM:
1019 float_color->r = D3DCOLOR_R(color);
1020 float_color->g = D3DCOLOR_G(color);
1021 float_color->b = D3DCOLOR_B(color);
1022 float_color->a = 1.0f;
1025 case WINED3DFMT_B8G8R8A8_UNORM:
1026 float_color->r = D3DCOLOR_R(color);
1027 float_color->g = D3DCOLOR_G(color);
1028 float_color->b = D3DCOLOR_B(color);
1029 float_color->a = D3DCOLOR_A(color);
1033 ERR("Unhandled conversion from %s to floating point.\n", debug_d3dformat(format->id));
1040 /* Do not call while under the GL lock. */
1041 static ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface)
1043 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1044 ULONG ref = InterlockedDecrement(&This->resource.ref);
1045 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
1049 surface_cleanup(This);
1050 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
1052 TRACE("(%p) Released.\n", This);
1053 HeapFree(GetProcessHeap(), 0, This);
1059 /* ****************************************************
1060 IWineD3DSurface IWineD3DResource parts follow
1061 **************************************************** */
1063 /* Do not call while under the GL lock. */
1064 void surface_internal_preload(IWineD3DSurfaceImpl *surface, enum WINED3DSRGB srgb)
1066 IWineD3DDeviceImpl *device = surface->resource.device;
1068 TRACE("iface %p, srgb %#x.\n", surface, srgb);
1070 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1072 IWineD3DBaseTextureImpl *texture = surface->container.u.texture;
1074 TRACE("Passing to container.\n");
1075 texture->baseTexture.internal_preload((IWineD3DBaseTexture *)texture, srgb);
1079 struct wined3d_context *context = NULL;
1081 TRACE("(%p) : About to load surface\n", surface);
1083 if (!device->isInDraw) context = context_acquire(device, NULL);
1085 if (surface->resource.format->id == WINED3DFMT_P8_UINT
1086 || surface->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
1088 if (palette9_changed(surface))
1090 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
1091 /* TODO: This is not necessarily needed with hw palettized texture support */
1092 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
1093 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
1094 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
1098 IWineD3DSurface_LoadTexture((IWineD3DSurface *)surface, srgb == SRGB_SRGB ? TRUE : FALSE);
1100 if (surface->resource.pool == WINED3DPOOL_DEFAULT)
1102 /* Tell opengl to try and keep this texture in video ram (well mostly) */
1106 glPrioritizeTextures(1, &surface->texture_name, &tmp);
1110 if (context) context_release(context);
1114 static void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface)
1116 surface_internal_preload((IWineD3DSurfaceImpl *)iface, SRGB_ANY);
1119 /* Context activation is done by the caller. */
1120 static void surface_remove_pbo(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info)
1122 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1123 This->resource.allocatedMemory =
1124 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1127 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1128 checkGLcall("glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
1129 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0, This->resource.size, This->resource.allocatedMemory));
1130 checkGLcall("glGetBufferSubDataARB");
1131 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
1132 checkGLcall("glDeleteBuffersARB");
1136 This->flags &= ~SFLAG_PBO;
1139 BOOL surface_init_sysmem(IWineD3DSurfaceImpl *surface)
1141 if (!surface->resource.allocatedMemory)
1143 surface->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1144 surface->resource.size + RESOURCE_ALIGNMENT);
1145 if (!surface->resource.heapMemory)
1147 ERR("Out of memory\n");
1150 surface->resource.allocatedMemory =
1151 (BYTE *)(((ULONG_PTR)surface->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1155 memset(surface->resource.allocatedMemory, 0, surface->resource.size);
1158 surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
1163 /* Do not call while under the GL lock. */
1164 static void WINAPI IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface *iface)
1166 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1167 IWineD3DDeviceImpl *device = This->resource.device;
1168 const struct wined3d_gl_info *gl_info;
1169 renderbuffer_entry_t *entry, *entry2;
1170 struct wined3d_context *context;
1172 TRACE("(%p)\n", iface);
1174 if(This->resource.pool == WINED3DPOOL_DEFAULT) {
1175 /* Default pool resources are supposed to be destroyed before Reset is called.
1176 * Implicit resources stay however. So this means we have an implicit render target
1177 * or depth stencil. The content may be destroyed, but we still have to tear down
1178 * opengl resources, so we cannot leave early.
1180 * Put the surfaces into sysmem, and reset the content. The D3D content is undefined,
1181 * but we can't set the sysmem INDRAWABLE because when we're rendering the swapchain
1182 * or the depth stencil into an FBO the texture or render buffer will be removed
1183 * and all flags get lost
1185 surface_init_sysmem(This);
1189 /* Load the surface into system memory */
1190 surface_load_location(This, SFLAG_INSYSMEM, NULL);
1191 surface_modify_location(This, SFLAG_INDRAWABLE, FALSE);
1193 surface_modify_location(This, SFLAG_INTEXTURE, FALSE);
1194 surface_modify_location(This, SFLAG_INSRGBTEX, FALSE);
1195 This->flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
1197 context = context_acquire(device, NULL);
1198 gl_info = context->gl_info;
1200 /* Destroy PBOs, but load them into real sysmem before */
1201 if (This->flags & SFLAG_PBO)
1202 surface_remove_pbo(This, gl_info);
1204 /* Destroy fbo render buffers. This is needed for implicit render targets, for
1205 * all application-created targets the application has to release the surface
1206 * before calling _Reset
1208 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
1210 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1212 list_remove(&entry->entry);
1213 HeapFree(GetProcessHeap(), 0, entry);
1215 list_init(&This->renderbuffers);
1216 This->current_renderbuffer = NULL;
1218 /* If we're in a texture, the texture name belongs to the texture.
1219 * Otherwise, destroy it. */
1220 if (This->container.type != WINED3D_CONTAINER_TEXTURE)
1223 glDeleteTextures(1, &This->texture_name);
1224 This->texture_name = 0;
1225 glDeleteTextures(1, &This->texture_name_srgb);
1226 This->texture_name_srgb = 0;
1230 context_release(context);
1232 resource_unload((IWineD3DResourceImpl *)This);
1235 /* ******************************************************
1236 IWineD3DSurface IWineD3DSurface parts follow
1237 ****************************************************** */
1239 /* Read the framebuffer back into the surface */
1240 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, const RECT *rect, void *dest, UINT pitch)
1242 IWineD3DDeviceImpl *device = This->resource.device;
1243 const struct wined3d_gl_info *gl_info;
1244 struct wined3d_context *context;
1248 BYTE *row, *top, *bottom;
1252 BOOL srcIsUpsideDown;
1257 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1258 static BOOL warned = FALSE;
1260 ERR("The application tries to lock the render target, but render target locking is disabled\n");
1266 context = context_acquire(device, This);
1267 context_apply_blit_state(context, device);
1268 gl_info = context->gl_info;
1272 /* Select the correct read buffer, and give some debug output.
1273 * There is no need to keep track of the current read buffer or reset it, every part of the code
1274 * that reads sets the read buffer as desired.
1276 if (surface_is_offscreen(This))
1278 /* Mapping the primary render target which is not on a swapchain.
1279 * Read from the back buffer. */
1280 TRACE("Mapping offscreen render target.\n");
1281 glReadBuffer(device->offscreenBuffer);
1282 srcIsUpsideDown = TRUE;
1286 /* Onscreen surfaces are always part of a swapchain */
1287 GLenum buffer = surface_get_gl_buffer(This);
1288 TRACE("Mapping %#x buffer.\n", buffer);
1289 glReadBuffer(buffer);
1290 checkGLcall("glReadBuffer");
1291 srcIsUpsideDown = FALSE;
1294 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
1296 local_rect.left = 0;
1298 local_rect.right = This->currentDesc.Width;
1299 local_rect.bottom = This->currentDesc.Height;
1303 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
1305 switch (This->resource.format->id)
1307 case WINED3DFMT_P8_UINT:
1309 if (primary_render_target_is_p8(device))
1311 /* In case of P8 render targets the index is stored in the alpha component */
1313 type = GL_UNSIGNED_BYTE;
1315 bpp = This->resource.format->byte_count;
1317 /* GL can't return palettized data, so read ARGB pixels into a
1318 * separate block of memory and convert them into palettized format
1319 * in software. Slow, but if the app means to use palettized render
1320 * targets and locks it...
1322 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
1323 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
1324 * for the color channels when palettizing the colors.
1327 type = GL_UNSIGNED_BYTE;
1329 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
1331 ERR("Out of memory\n");
1335 bpp = This->resource.format->byte_count * 3;
1342 fmt = This->resource.format->glFormat;
1343 type = This->resource.format->glType;
1344 bpp = This->resource.format->byte_count;
1347 if (This->flags & SFLAG_PBO)
1349 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
1350 checkGLcall("glBindBufferARB");
1353 ERR("mem not null for pbo -- unexpected\n");
1358 /* Save old pixel store pack state */
1359 glGetIntegerv(GL_PACK_ROW_LENGTH, &rowLen);
1360 checkGLcall("glGetIntegerv");
1361 glGetIntegerv(GL_PACK_SKIP_PIXELS, &skipPix);
1362 checkGLcall("glGetIntegerv");
1363 glGetIntegerv(GL_PACK_SKIP_ROWS, &skipRow);
1364 checkGLcall("glGetIntegerv");
1366 /* Setup pixel store pack state -- to glReadPixels into the correct place */
1367 glPixelStorei(GL_PACK_ROW_LENGTH, This->currentDesc.Width);
1368 checkGLcall("glPixelStorei");
1369 glPixelStorei(GL_PACK_SKIP_PIXELS, local_rect.left);
1370 checkGLcall("glPixelStorei");
1371 glPixelStorei(GL_PACK_SKIP_ROWS, local_rect.top);
1372 checkGLcall("glPixelStorei");
1374 glReadPixels(local_rect.left, (!srcIsUpsideDown) ? (This->currentDesc.Height - local_rect.bottom) : local_rect.top ,
1375 local_rect.right - local_rect.left,
1376 local_rect.bottom - local_rect.top,
1378 checkGLcall("glReadPixels");
1380 /* Reset previous pixel store pack state */
1381 glPixelStorei(GL_PACK_ROW_LENGTH, rowLen);
1382 checkGLcall("glPixelStorei");
1383 glPixelStorei(GL_PACK_SKIP_PIXELS, skipPix);
1384 checkGLcall("glPixelStorei");
1385 glPixelStorei(GL_PACK_SKIP_ROWS, skipRow);
1386 checkGLcall("glPixelStorei");
1388 if (This->flags & SFLAG_PBO)
1390 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
1391 checkGLcall("glBindBufferARB");
1393 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
1394 * to get a pointer to it and perform the flipping in software. This is a lot
1395 * faster than calling glReadPixels for each line. In case we want more speed
1396 * we should rerender it flipped in a FBO and read the data back from the FBO. */
1397 if(!srcIsUpsideDown) {
1398 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1399 checkGLcall("glBindBufferARB");
1401 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1402 checkGLcall("glMapBufferARB");
1406 /* TODO: Merge this with the palettization loop below for P8 targets */
1407 if(!srcIsUpsideDown) {
1409 /* glReadPixels returns the image upside down, and there is no way to prevent this.
1410 Flip the lines in software */
1411 len = (local_rect.right - local_rect.left) * bpp;
1412 off = local_rect.left * bpp;
1414 row = HeapAlloc(GetProcessHeap(), 0, len);
1416 ERR("Out of memory\n");
1417 if (This->resource.format->id == WINED3DFMT_P8_UINT) HeapFree(GetProcessHeap(), 0, mem);
1422 top = mem + pitch * local_rect.top;
1423 bottom = mem + pitch * (local_rect.bottom - 1);
1424 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
1425 memcpy(row, top + off, len);
1426 memcpy(top + off, bottom + off, len);
1427 memcpy(bottom + off, row, len);
1431 HeapFree(GetProcessHeap(), 0, row);
1433 /* Unmap the temp PBO buffer */
1434 if (This->flags & SFLAG_PBO)
1436 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1437 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1442 context_release(context);
1444 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
1445 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
1446 * the same color but we have no choice.
1447 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
1449 if (This->resource.format->id == WINED3DFMT_P8_UINT && !primary_render_target_is_p8(device))
1451 const PALETTEENTRY *pal = NULL;
1452 DWORD width = pitch / 3;
1456 pal = This->palette->palents;
1458 ERR("Palette is missing, cannot perform inverse palette lookup\n");
1459 HeapFree(GetProcessHeap(), 0, mem);
1463 for(y = local_rect.top; y < local_rect.bottom; y++) {
1464 for(x = local_rect.left; x < local_rect.right; x++) {
1465 /* start lines pixels */
1466 const BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
1467 const BYTE *green = blue + 1;
1468 const BYTE *red = green + 1;
1470 for(c = 0; c < 256; c++) {
1471 if(*red == pal[c].peRed &&
1472 *green == pal[c].peGreen &&
1473 *blue == pal[c].peBlue)
1475 *((BYTE *) dest + y * width + x) = c;
1481 HeapFree(GetProcessHeap(), 0, mem);
1485 /* Read the framebuffer contents into a texture */
1486 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This, BOOL srgb)
1488 IWineD3DDeviceImpl *device = This->resource.device;
1489 const struct wined3d_gl_info *gl_info;
1490 struct wined3d_context *context;
1492 if (!surface_is_offscreen(This))
1494 /* We would need to flip onscreen surfaces, but there's no efficient
1495 * way to do that here. It makes more sense for the caller to
1496 * explicitly go through sysmem. */
1497 ERR("Not supported for onscreen targets.\n");
1501 /* Activate the surface to read from. In some situations it isn't the currently active target(e.g. backbuffer
1502 * locking during offscreen rendering). RESOURCELOAD is ok because glCopyTexSubImage2D isn't affected by any
1503 * states in the stateblock, and no driver was found yet that had bugs in that regard.
1505 context = context_acquire(device, This);
1506 gl_info = context->gl_info;
1508 surface_prepare_texture(This, gl_info, srgb);
1509 surface_bind_and_dirtify(This, srgb);
1511 TRACE("Reading back offscreen render target %p.\n", This);
1515 glReadBuffer(device->offscreenBuffer);
1516 checkGLcall("glReadBuffer");
1518 glCopyTexSubImage2D(This->texture_target, This->texture_level,
1519 0, 0, 0, 0, This->currentDesc.Width, This->currentDesc.Height);
1520 checkGLcall("glCopyTexSubImage2D");
1524 context_release(context);
1527 /* Context activation is done by the caller. */
1528 static void surface_prepare_texture_internal(IWineD3DSurfaceImpl *surface,
1529 const struct wined3d_gl_info *gl_info, BOOL srgb)
1531 DWORD alloc_flag = srgb ? SFLAG_SRGBALLOCATED : SFLAG_ALLOCATED;
1532 CONVERT_TYPES convert;
1533 struct wined3d_format format;
1535 if (surface->flags & alloc_flag) return;
1537 d3dfmt_get_conv(surface, TRUE, TRUE, &format, &convert);
1538 if (convert != NO_CONVERSION || format.convert) surface->flags |= SFLAG_CONVERTED;
1539 else surface->flags &= ~SFLAG_CONVERTED;
1541 surface_bind_and_dirtify(surface, srgb);
1542 surface_allocate_surface(surface, gl_info, &format, srgb);
1543 surface->flags |= alloc_flag;
1546 /* Context activation is done by the caller. */
1547 void surface_prepare_texture(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info, BOOL srgb)
1549 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1551 IWineD3DBaseTextureImpl *texture = surface->container.u.texture;
1552 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
1555 TRACE("surface %p is a subresource of texture %p.\n", surface, texture);
1557 for (i = 0; i < sub_count; ++i)
1559 IWineD3DSurfaceImpl *s = (IWineD3DSurfaceImpl *)texture->baseTexture.sub_resources[i];
1560 surface_prepare_texture_internal(s, gl_info, srgb);
1566 surface_prepare_texture_internal(surface, gl_info, srgb);
1569 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This)
1571 IWineD3DDeviceImpl *device = This->resource.device;
1572 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1574 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
1575 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
1578 if (!(This->flags & SFLAG_DYNLOCK))
1581 /* MAXLOCKCOUNT is defined in wined3d_private.h */
1582 if(This->lockCount > MAXLOCKCOUNT) {
1583 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
1584 This->flags |= SFLAG_DYNLOCK;
1588 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
1589 * Also don't create a PBO for systemmem surfaces.
1591 if (gl_info->supported[ARB_PIXEL_BUFFER_OBJECT] && (This->flags & SFLAG_DYNLOCK)
1592 && !(This->flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2))
1593 && (This->resource.pool != WINED3DPOOL_SYSTEMMEM))
1596 struct wined3d_context *context;
1598 context = context_acquire(device, NULL);
1601 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
1602 error = glGetError();
1603 if (!This->pbo || error != GL_NO_ERROR)
1604 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
1606 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
1608 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1609 checkGLcall("glBindBufferARB");
1611 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
1612 checkGLcall("glBufferDataARB");
1614 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1615 checkGLcall("glBindBufferARB");
1617 /* We don't need the system memory anymore and we can't even use it for PBOs */
1618 if (!(This->flags & SFLAG_CLIENT))
1620 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
1621 This->resource.heapMemory = NULL;
1623 This->resource.allocatedMemory = NULL;
1624 This->flags |= SFLAG_PBO;
1626 context_release(context);
1628 else if (!(This->resource.allocatedMemory || This->flags & SFLAG_PBO))
1630 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
1633 if(!This->resource.heapMemory) {
1634 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1636 This->resource.allocatedMemory =
1637 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1638 if (This->flags & SFLAG_INSYSMEM)
1640 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1645 static HRESULT WINAPI IWineD3DSurfaceImpl_Map(IWineD3DSurface *iface,
1646 WINED3DLOCKED_RECT *pLockedRect, const RECT *pRect, DWORD Flags)
1648 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1649 IWineD3DDeviceImpl *device = This->resource.device;
1650 const RECT *pass_rect = pRect;
1652 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
1653 iface, pLockedRect, wine_dbgstr_rect(pRect), Flags);
1655 /* This is also done in the base class, but we have to verify this before loading any data from
1656 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1657 * may interfere, and all other bad things may happen
1659 if (This->flags & SFLAG_LOCKED)
1661 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1662 return WINED3DERR_INVALIDCALL;
1664 This->flags |= SFLAG_LOCKED;
1666 if (!(This->flags & SFLAG_LOCKABLE))
1668 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
1671 if (Flags & WINED3DLOCK_DISCARD)
1673 TRACE("WINED3DLOCK_DISCARD flag passed, marking SYSMEM as up to date.\n");
1674 surface_prepare_system_memory(This);
1675 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
1679 /* surface_load_location() does not check if the rectangle specifies
1680 * the full surface. Most callers don't need that, so do it here. */
1681 if (pRect && !pRect->top && !pRect->left
1682 && pRect->right == This->currentDesc.Width
1683 && pRect->bottom == This->currentDesc.Height)
1688 if (!(wined3d_settings.rendertargetlock_mode == RTL_DISABLE
1689 && ((This->container.type == WINED3D_CONTAINER_SWAPCHAIN) || This == device->render_targets[0])))
1691 surface_load_location(This, SFLAG_INSYSMEM, pass_rect);
1695 if (This->flags & SFLAG_PBO)
1697 const struct wined3d_gl_info *gl_info;
1698 struct wined3d_context *context;
1700 context = context_acquire(device, NULL);
1701 gl_info = context->gl_info;
1704 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1705 checkGLcall("glBindBufferARB");
1707 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1708 if(This->resource.allocatedMemory) {
1709 ERR("The surface already has PBO memory allocated!\n");
1712 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1713 checkGLcall("glMapBufferARB");
1715 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1716 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1717 checkGLcall("glBindBufferARB");
1720 context_release(context);
1723 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
1728 surface_add_dirty_rect(This, pRect);
1730 if (This->container.type == WINED3D_CONTAINER_TEXTURE)
1732 TRACE("Making container dirty.\n");
1733 IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture *)This->container.u.texture, TRUE);
1737 TRACE("Surface is standalone, no need to dirty the container\n");
1741 return IWineD3DBaseSurfaceImpl_Map(iface, pLockedRect, pRect, Flags);
1744 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This,
1745 GLenum fmt, GLenum type, UINT bpp, const BYTE *mem)
1747 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
1748 IWineD3DDeviceImpl *device = This->resource.device;
1749 const struct wined3d_gl_info *gl_info;
1750 struct wined3d_context *context;
1754 if (This->flags & SFLAG_LOCKED)
1755 rect = This->lockedRect;
1757 SetRect(&rect, 0, 0, This->currentDesc.Width, This->currentDesc.Height);
1759 mem += rect.top * pitch + rect.left * bpp;
1760 w = rect.right - rect.left;
1761 h = rect.bottom - rect.top;
1763 /* Activate the correct context for the render target */
1764 context = context_acquire(device, This);
1765 context_apply_blit_state(context, device);
1766 gl_info = context->gl_info;
1770 if (!surface_is_offscreen(This))
1772 GLenum buffer = surface_get_gl_buffer(This);
1773 TRACE("Unlocking %#x buffer.\n", buffer);
1774 context_set_draw_buffer(context, buffer);
1776 surface_translate_drawable_coords(This, context->win_handle, &rect);
1777 glPixelZoom(1.0f, -1.0f);
1781 /* Primary offscreen render target */
1782 TRACE("Offscreen render target.\n");
1783 context_set_draw_buffer(context, device->offscreenBuffer);
1785 glPixelZoom(1.0f, 1.0f);
1788 glRasterPos3i(rect.left, rect.top, 1);
1789 checkGLcall("glRasterPos3i");
1791 /* Some drivers(radeon dri, others?) don't like exceptions during
1792 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1793 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1794 * catch to put the dib section in InSync mode, which leads to a crash
1795 * and a blocked x server on my radeon card.
1797 * The following lines read the dib section so it is put in InSync mode
1798 * before glDrawPixels is called and the crash is prevented. There won't
1799 * be any interfering gdi accesses, because UnlockRect is called from
1800 * ReleaseDC, and the app won't use the dc any more afterwards.
1802 if ((This->flags & SFLAG_DIBSECTION) && !(This->flags & SFLAG_PBO))
1805 read = This->resource.allocatedMemory[0];
1808 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1809 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
1811 if (This->flags & SFLAG_PBO)
1813 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1814 checkGLcall("glBindBufferARB");
1817 glDrawPixels(w, h, fmt, type, mem);
1818 checkGLcall("glDrawPixels");
1820 if (This->flags & SFLAG_PBO)
1822 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1823 checkGLcall("glBindBufferARB");
1826 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1827 checkGLcall("glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)");
1830 context_release(context);
1833 static HRESULT WINAPI IWineD3DSurfaceImpl_Unmap(IWineD3DSurface *iface)
1835 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1836 IWineD3DDeviceImpl *device = This->resource.device;
1839 if (!(This->flags & SFLAG_LOCKED))
1841 WARN("trying to Unlock an unlocked surf@%p\n", This);
1842 return WINEDDERR_NOTLOCKED;
1845 if (This->flags & SFLAG_PBO)
1847 const struct wined3d_gl_info *gl_info;
1848 struct wined3d_context *context;
1850 TRACE("Freeing PBO memory\n");
1852 context = context_acquire(device, NULL);
1853 gl_info = context->gl_info;
1856 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1857 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1858 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1859 checkGLcall("glUnmapBufferARB");
1861 context_release(context);
1863 This->resource.allocatedMemory = NULL;
1866 TRACE("(%p) : dirtyfied(%d)\n", This, This->flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1868 if (This->flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE))
1870 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1874 if (This->container.type == WINED3D_CONTAINER_SWAPCHAIN
1875 || (device->render_targets && This == device->render_targets[0]))
1877 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1878 static BOOL warned = FALSE;
1880 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1886 if (!This->dirtyRect.left && !This->dirtyRect.top
1887 && This->dirtyRect.right == This->currentDesc.Width
1888 && This->dirtyRect.bottom == This->currentDesc.Height)
1892 /* TODO: Proper partial rectangle tracking */
1893 fullsurface = FALSE;
1894 This->flags |= SFLAG_INSYSMEM;
1897 surface_load_location(This, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1901 /* Partial rectangle tracking is not commonly implemented, it is
1902 * only done for render targets. Overwrite the flags to bring
1903 * them back into a sane state. INSYSMEM was set before to tell
1904 * surface_load_location() where to read the rectangle from.
1905 * Indrawable is set because all modifications from the partial
1906 * sysmem copy are written back to the drawable, thus the surface
1907 * is merged again in the drawable. The sysmem copy is not fully
1908 * up to date because only a subrectangle was read in Map(). */
1909 This->flags &= ~SFLAG_INSYSMEM;
1910 This->flags |= SFLAG_INDRAWABLE;
1913 This->dirtyRect.left = This->currentDesc.Width;
1914 This->dirtyRect.top = This->currentDesc.Height;
1915 This->dirtyRect.right = 0;
1916 This->dirtyRect.bottom = 0;
1918 else if (This->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
1920 FIXME("Depth Stencil buffer locking is not implemented\n");
1924 This->flags &= ~SFLAG_LOCKED;
1925 memset(&This->lockedRect, 0, sizeof(RECT));
1927 /* Overlays have to be redrawn manually after changes with the GL implementation */
1928 if(This->overlay_dest) {
1929 IWineD3DSurface_DrawOverlay(iface);
1934 static void surface_release_client_storage(IWineD3DSurfaceImpl *surface)
1936 struct wined3d_context *context;
1938 context = context_acquire(surface->resource.device, NULL);
1941 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
1942 if (surface->texture_name)
1944 surface_bind_and_dirtify(surface, FALSE);
1945 glTexImage2D(surface->texture_target, surface->texture_level,
1946 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
1948 if (surface->texture_name_srgb)
1950 surface_bind_and_dirtify(surface, TRUE);
1951 glTexImage2D(surface->texture_target, surface->texture_level,
1952 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
1954 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1957 context_release(context);
1959 surface_modify_location(surface, SFLAG_INSRGBTEX, FALSE);
1960 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
1961 surface_force_reload(surface);
1964 static HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC)
1966 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1967 WINED3DLOCKED_RECT lock;
1971 TRACE("(%p)->(%p)\n",This,pHDC);
1973 if (This->flags & SFLAG_USERPTR)
1975 ERR("Not supported on surfaces with an application-provided surfaces\n");
1976 return WINEDDERR_NODC;
1979 /* Give more detailed info for ddraw */
1980 if (This->flags & SFLAG_DCINUSE)
1981 return WINEDDERR_DCALREADYCREATED;
1983 /* Can't GetDC if the surface is locked */
1984 if (This->flags & SFLAG_LOCKED)
1985 return WINED3DERR_INVALIDCALL;
1987 memset(&lock, 0, sizeof(lock)); /* To be sure */
1989 /* Create a DIB section if there isn't a hdc yet */
1992 if (This->flags & SFLAG_CLIENT)
1994 surface_load_location(This, SFLAG_INSYSMEM, NULL);
1995 surface_release_client_storage(This);
1997 hr = IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
1998 if(FAILED(hr)) return WINED3DERR_INVALIDCALL;
2000 /* Use the dib section from now on if we are not using a PBO */
2001 if (!(This->flags & SFLAG_PBO))
2002 This->resource.allocatedMemory = This->dib.bitmap_data;
2005 /* Map the surface */
2006 hr = IWineD3DSurface_Map(iface, &lock, NULL, 0);
2008 /* Sync the DIB with the PBO. This can't be done earlier because Map()
2009 * activates the allocatedMemory. */
2010 if (This->flags & SFLAG_PBO)
2011 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
2015 ERR("IWineD3DSurface_Map failed, hr %#x.\n", hr);
2016 /* keep the dib section */
2020 if (This->resource.format->id == WINED3DFMT_P8_UINT
2021 || This->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
2023 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
2024 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
2026 const PALETTEENTRY *pal = NULL;
2029 pal = This->palette->palents;
2031 IWineD3DSurfaceImpl *dds_primary;
2032 IWineD3DSwapChainImpl *swapchain;
2033 swapchain = (IWineD3DSwapChainImpl *)This->resource.device->swapchains[0];
2034 dds_primary = swapchain->front_buffer;
2035 if (dds_primary && dds_primary->palette)
2036 pal = dds_primary->palette->palents;
2040 for (n=0; n<256; n++) {
2041 col[n].rgbRed = pal[n].peRed;
2042 col[n].rgbGreen = pal[n].peGreen;
2043 col[n].rgbBlue = pal[n].peBlue;
2044 col[n].rgbReserved = 0;
2046 SetDIBColorTable(This->hDC, 0, 256, col);
2051 TRACE("returning %p\n",*pHDC);
2052 This->flags |= SFLAG_DCINUSE;
2057 static HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC)
2059 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2061 TRACE("(%p)->(%p)\n",This,hDC);
2063 if (!(This->flags & SFLAG_DCINUSE))
2064 return WINEDDERR_NODC;
2066 if (This->hDC !=hDC) {
2067 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
2068 return WINEDDERR_NODC;
2071 if ((This->flags & SFLAG_PBO) && This->resource.allocatedMemory)
2073 /* Copy the contents of the DIB over to the PBO */
2074 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
2077 /* we locked first, so unlock now */
2078 IWineD3DSurface_Unmap(iface);
2080 This->flags &= ~SFLAG_DCINUSE;
2085 /* ******************************************************
2086 IWineD3DSurface Internal (No mapping to directx api) parts follow
2087 ****************************************************** */
2089 HRESULT d3dfmt_get_conv(IWineD3DSurfaceImpl *This, BOOL need_alpha_ck,
2090 BOOL use_texturing, struct wined3d_format *format, CONVERT_TYPES *convert)
2092 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
2093 IWineD3DDeviceImpl *device = This->resource.device;
2094 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2095 BOOL blit_supported = FALSE;
2097 /* Copy the default values from the surface. Below we might perform fixups */
2098 /* TODO: get rid of color keying desc fixups by using e.g. a table. */
2099 *format = *This->resource.format;
2100 *convert = NO_CONVERSION;
2102 /* Ok, now look if we have to do any conversion */
2103 switch (This->resource.format->id)
2105 case WINED3DFMT_P8_UINT:
2106 /* Below the call to blit_supported is disabled for Wine 1.2
2107 * because the function isn't operating correctly yet. At the
2108 * moment 8-bit blits are handled in software and if certain GL
2109 * extensions are around, surface conversion is performed at
2110 * upload time. The blit_supported call recognizes it as a
2111 * destination fixup. This type of upload 'fixup' and 8-bit to
2112 * 8-bit blits need to be handled by the blit_shader.
2113 * TODO: get rid of this #if 0. */
2115 blit_supported = device->blitter->blit_supported(&device->adapter->gl_info, BLIT_OP_BLIT,
2116 &rect, This->resource.usage, This->resource.pool, This->resource.format,
2117 &rect, This->resource.usage, This->resource.pool, This->resource.format);
2119 blit_supported = gl_info->supported[EXT_PALETTED_TEXTURE] || gl_info->supported[ARB_FRAGMENT_PROGRAM];
2121 /* Use conversion when the blit_shader backend supports it. It only supports this in case of
2122 * texturing. Further also use conversion in case of color keying.
2123 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
2124 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
2125 * conflicts with this.
2127 if (!((blit_supported && device->render_targets && This == device->render_targets[0]))
2128 || colorkey_active || !use_texturing)
2130 format->glFormat = GL_RGBA;
2131 format->glInternal = GL_RGBA;
2132 format->glType = GL_UNSIGNED_BYTE;
2133 format->conv_byte_count = 4;
2134 if (colorkey_active)
2135 *convert = CONVERT_PALETTED_CK;
2137 *convert = CONVERT_PALETTED;
2141 case WINED3DFMT_B2G3R3_UNORM:
2142 /* **********************
2143 GL_UNSIGNED_BYTE_3_3_2
2144 ********************** */
2145 if (colorkey_active) {
2146 /* This texture format will never be used.. So do not care about color keying
2147 up until the point in time it will be needed :-) */
2148 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
2152 case WINED3DFMT_B5G6R5_UNORM:
2153 if (colorkey_active)
2155 *convert = CONVERT_CK_565;
2156 format->glFormat = GL_RGBA;
2157 format->glInternal = GL_RGB5_A1;
2158 format->glType = GL_UNSIGNED_SHORT_5_5_5_1;
2159 format->conv_byte_count = 2;
2163 case WINED3DFMT_B5G5R5X1_UNORM:
2164 if (colorkey_active)
2166 *convert = CONVERT_CK_5551;
2167 format->glFormat = GL_BGRA;
2168 format->glInternal = GL_RGB5_A1;
2169 format->glType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
2170 format->conv_byte_count = 2;
2174 case WINED3DFMT_B8G8R8_UNORM:
2175 if (colorkey_active)
2177 *convert = CONVERT_CK_RGB24;
2178 format->glFormat = GL_RGBA;
2179 format->glInternal = GL_RGBA8;
2180 format->glType = GL_UNSIGNED_INT_8_8_8_8;
2181 format->conv_byte_count = 4;
2185 case WINED3DFMT_B8G8R8X8_UNORM:
2186 if (colorkey_active)
2188 *convert = CONVERT_RGB32_888;
2189 format->glFormat = GL_RGBA;
2190 format->glInternal = GL_RGBA8;
2191 format->glType = GL_UNSIGNED_INT_8_8_8_8;
2192 format->conv_byte_count = 4;
2203 void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey)
2205 IWineD3DDeviceImpl *device = This->resource.device;
2206 IWineD3DPaletteImpl *pal = This->palette;
2207 BOOL index_in_alpha = FALSE;
2210 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2211 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2212 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2213 * duplicate entries. Store the color key in the unused alpha component to speed the
2214 * download up and to make conversion unneeded. */
2215 index_in_alpha = primary_render_target_is_p8(device);
2219 UINT dxVersion = ((IWineD3DImpl *)device->wined3d)->dxVersion;
2221 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2224 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2227 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2228 * there's no palette at this time. */
2229 for (i = 0; i < 256; i++) table[i][3] = i;
2234 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2235 * alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2236 * capability flag is present (wine does advertise this capability) */
2237 for (i = 0; i < 256; ++i)
2239 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2240 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2241 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2242 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2248 TRACE("Using surface palette %p\n", pal);
2249 /* Get the surface's palette */
2250 for (i = 0; i < 256; ++i)
2252 table[i][0] = pal->palents[i].peRed;
2253 table[i][1] = pal->palents[i].peGreen;
2254 table[i][2] = pal->palents[i].peBlue;
2256 /* When index_in_alpha is set the palette index is stored in the
2257 * alpha component. In case of a readback we can then read
2258 * GL_ALPHA. Color keying is handled in BltOverride using a
2259 * GL_ALPHA_TEST using GL_NOT_EQUAL. In case of index_in_alpha the
2260 * color key itself is passed to glAlphaFunc in other cases the
2261 * alpha component of pixels that should be masked away is set to 0. */
2266 else if (colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue)
2267 && (i <= This->SrcBltCKey.dwColorSpaceHighValue))
2271 else if (pal->flags & WINEDDPCAPS_ALPHA)
2273 table[i][3] = pal->palents[i].peFlags;
2283 static HRESULT d3dfmt_convert_surface(const BYTE *src, BYTE *dst, UINT pitch, UINT width,
2284 UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This)
2288 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
2293 memcpy(dst, src, pitch * height);
2296 case CONVERT_PALETTED:
2297 case CONVERT_PALETTED_CK:
2302 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2304 for (y = 0; y < height; y++)
2306 source = src + pitch * y;
2307 dest = dst + outpitch * y;
2308 /* This is an 1 bpp format, using the width here is fine */
2309 for (x = 0; x < width; x++) {
2310 BYTE color = *source++;
2311 *dest++ = table[color][0];
2312 *dest++ = table[color][1];
2313 *dest++ = table[color][2];
2314 *dest++ = table[color][3];
2320 case CONVERT_CK_565:
2322 /* Converting the 565 format in 5551 packed to emulate color-keying.
2324 Note : in all these conversion, it would be best to average the averaging
2325 pixels to get the color of the pixel that will be color-keyed to
2326 prevent 'color bleeding'. This will be done later on if ever it is
2329 Note2: Nvidia documents say that their driver does not support alpha + color keying
2330 on the same surface and disables color keying in such a case
2336 TRACE("Color keyed 565\n");
2338 for (y = 0; y < height; y++) {
2339 Source = (const WORD *)(src + y * pitch);
2340 Dest = (WORD *) (dst + y * outpitch);
2341 for (x = 0; x < width; x++ ) {
2342 WORD color = *Source++;
2343 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
2344 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2345 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2354 case CONVERT_CK_5551:
2356 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
2360 TRACE("Color keyed 5551\n");
2361 for (y = 0; y < height; y++) {
2362 Source = (const WORD *)(src + y * pitch);
2363 Dest = (WORD *) (dst + y * outpitch);
2364 for (x = 0; x < width; x++ ) {
2365 WORD color = *Source++;
2367 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2368 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2372 *Dest &= ~(1 << 15);
2380 case CONVERT_CK_RGB24:
2382 /* Converting R8G8B8 format to R8G8B8A8 with color-keying. */
2384 for (y = 0; y < height; y++)
2386 source = src + pitch * y;
2387 dest = dst + outpitch * y;
2388 for (x = 0; x < width; x++) {
2389 DWORD color = ((DWORD)source[0] << 16) + ((DWORD)source[1] << 8) + (DWORD)source[2] ;
2390 DWORD dstcolor = color << 8;
2391 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2392 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2395 *(DWORD*)dest = dstcolor;
2403 case CONVERT_RGB32_888:
2405 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
2407 for (y = 0; y < height; y++)
2409 source = src + pitch * y;
2410 dest = dst + outpitch * y;
2411 for (x = 0; x < width; x++) {
2412 DWORD color = 0xffffff & *(const DWORD*)source;
2413 DWORD dstcolor = color << 8;
2414 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2415 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2418 *(DWORD*)dest = dstcolor;
2427 ERR("Unsupported conversion type %#x.\n", convert);
2432 BOOL palette9_changed(IWineD3DSurfaceImpl *This)
2434 IWineD3DDeviceImpl *device = This->resource.device;
2436 if (This->palette || (This->resource.format->id != WINED3DFMT_P8_UINT
2437 && This->resource.format->id != WINED3DFMT_P8_UINT_A8_UNORM))
2439 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2440 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2447 if (!memcmp(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256))
2452 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2454 memcpy(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2458 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2459 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2460 DWORD flag = srgb_mode ? SFLAG_INSRGBTEX : SFLAG_INTEXTURE;
2462 TRACE("iface %p, srgb %#x.\n", iface, srgb_mode);
2464 if (!(This->flags & flag))
2466 TRACE("Reloading because surface is dirty\n");
2468 /* Reload if either the texture and sysmem have different ideas about the
2469 * color key, or the actual key values changed. */
2470 else if (!(This->flags & SFLAG_GLCKEY) != !(This->CKeyFlags & WINEDDSD_CKSRCBLT)
2471 || ((This->CKeyFlags & WINEDDSD_CKSRCBLT)
2472 && (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue
2473 || This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))
2475 TRACE("Reloading because of color keying\n");
2476 /* To perform the color key conversion we need a sysmem copy of
2477 * the surface. Make sure we have it
2480 surface_load_location(This, SFLAG_INSYSMEM, NULL);
2481 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2482 /* TODO: This is not necessarily needed with hw palettized texture support */
2483 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
2485 TRACE("surface is already in texture\n");
2489 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2490 * These resources are not bound by device size or format restrictions. Because of this,
2491 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2492 * However, these resources can always be created, locked, and copied.
2494 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2496 FIXME("(%p) Operation not supported for scratch textures\n",This);
2497 return WINED3DERR_INVALIDCALL;
2500 surface_load_location(This, flag, NULL /* no partial locking for textures yet */);
2502 if (!(This->flags & SFLAG_DONOTFREE))
2504 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2505 This->resource.allocatedMemory = NULL;
2506 This->resource.heapMemory = NULL;
2507 surface_modify_location(This, SFLAG_INSYSMEM, FALSE);
2513 /* Context activation is done by the caller. */
2514 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface, BOOL srgb)
2516 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2518 TRACE("iface %p, srgb %#x.\n", iface, srgb);
2520 if (This->container.type == WINED3D_CONTAINER_TEXTURE)
2522 TRACE("Passing to container.\n");
2523 IWineD3DBaseTexture_BindTexture((IWineD3DBaseTexture *)This->container.u.texture, srgb);
2529 TRACE("(%p) : Binding surface\n", This);
2531 name = srgb ? &This->texture_name_srgb : &This->texture_name;
2535 if (!This->texture_level)
2538 glGenTextures(1, name);
2539 checkGLcall("glGenTextures");
2540 TRACE("Surface %p given name %d\n", This, *name);
2542 glBindTexture(This->texture_target, *name);
2543 checkGLcall("glBindTexture");
2544 glTexParameteri(This->texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2545 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
2546 glTexParameteri(This->texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2547 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
2548 glTexParameteri(This->texture_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2549 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)");
2550 glTexParameteri(This->texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2551 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
2552 glTexParameteri(This->texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2553 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
2555 /* This is where we should be reducing the amount of GLMemoryUsed */
2557 /* Mipmap surfaces should have a base texture container */
2558 ERR("Mipmap surface has a glTexture bound to it!\n");
2561 glBindTexture(This->texture_target, *name);
2562 checkGLcall("glBindTexture");
2568 static HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, enum wined3d_format_id format)
2570 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2573 TRACE("(%p) : Calling base function first\n", This);
2574 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2577 This->flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
2578 TRACE("(%p) : glFormat %d, glFormatInternal %d, glType %d\n", This, This->resource.format->glFormat,
2579 This->resource.format->glInternal, This->resource.format->glType);
2584 static HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2585 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2587 TRACE("iface %p, mem %p.\n", iface, Mem);
2589 if (This->flags & (SFLAG_LOCKED | SFLAG_DCINUSE))
2591 WARN("Surface is locked or the HDC is in use\n");
2592 return WINED3DERR_INVALIDCALL;
2595 if(Mem && Mem != This->resource.allocatedMemory) {
2596 void *release = NULL;
2598 /* Do I have to copy the old surface content? */
2599 if (This->flags & SFLAG_DIBSECTION)
2601 SelectObject(This->hDC, This->dib.holdbitmap);
2602 DeleteDC(This->hDC);
2603 /* Release the DIB section */
2604 DeleteObject(This->dib.DIBsection);
2605 This->dib.bitmap_data = NULL;
2606 This->resource.allocatedMemory = NULL;
2608 This->flags &= ~SFLAG_DIBSECTION;
2610 else if (!(This->flags & SFLAG_USERPTR))
2612 release = This->resource.heapMemory;
2613 This->resource.heapMemory = NULL;
2615 This->resource.allocatedMemory = Mem;
2616 This->flags |= SFLAG_USERPTR;
2618 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2619 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
2621 /* For client textures opengl has to be notified */
2622 if (This->flags & SFLAG_CLIENT)
2623 surface_release_client_storage(This);
2625 /* Now free the old memory if any */
2626 HeapFree(GetProcessHeap(), 0, release);
2628 else if (This->flags & SFLAG_USERPTR)
2630 /* Map and GetDC will re-create the dib section and allocated memory. */
2631 This->resource.allocatedMemory = NULL;
2632 /* HeapMemory should be NULL already */
2633 if (This->resource.heapMemory)
2634 ERR("User pointer surface has heap memory allocated.\n");
2635 This->flags &= ~(SFLAG_USERPTR | SFLAG_INSYSMEM);
2637 if (This->flags & SFLAG_CLIENT)
2638 surface_release_client_storage(This);
2640 surface_prepare_system_memory(This);
2641 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
2646 void flip_surface(IWineD3DSurfaceImpl *front, IWineD3DSurfaceImpl *back) {
2648 /* Flip the surface contents */
2653 front->hDC = back->hDC;
2657 /* Flip the DIBsection */
2660 BOOL hasDib = front->flags & SFLAG_DIBSECTION;
2661 tmp = front->dib.DIBsection;
2662 front->dib.DIBsection = back->dib.DIBsection;
2663 back->dib.DIBsection = tmp;
2665 if (back->flags & SFLAG_DIBSECTION) front->flags |= SFLAG_DIBSECTION;
2666 else front->flags &= ~SFLAG_DIBSECTION;
2667 if (hasDib) back->flags |= SFLAG_DIBSECTION;
2668 else back->flags &= ~SFLAG_DIBSECTION;
2671 /* Flip the surface data */
2675 tmp = front->dib.bitmap_data;
2676 front->dib.bitmap_data = back->dib.bitmap_data;
2677 back->dib.bitmap_data = tmp;
2679 tmp = front->resource.allocatedMemory;
2680 front->resource.allocatedMemory = back->resource.allocatedMemory;
2681 back->resource.allocatedMemory = tmp;
2683 tmp = front->resource.heapMemory;
2684 front->resource.heapMemory = back->resource.heapMemory;
2685 back->resource.heapMemory = tmp;
2690 GLuint tmp_pbo = front->pbo;
2691 front->pbo = back->pbo;
2692 back->pbo = tmp_pbo;
2695 /* client_memory should not be different, but just in case */
2698 tmp = front->dib.client_memory;
2699 front->dib.client_memory = back->dib.client_memory;
2700 back->dib.client_memory = tmp;
2703 /* Flip the opengl texture */
2707 tmp = back->texture_name;
2708 back->texture_name = front->texture_name;
2709 front->texture_name = tmp;
2711 tmp = back->texture_name_srgb;
2712 back->texture_name_srgb = front->texture_name_srgb;
2713 front->texture_name_srgb = tmp;
2715 resource_unload((IWineD3DResourceImpl *)back);
2716 resource_unload((IWineD3DResourceImpl *)front);
2720 DWORD tmp_flags = back->flags;
2721 back->flags = front->flags;
2722 front->flags = tmp_flags;
2726 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2727 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2728 IWineD3DSwapChainImpl *swapchain = NULL;
2730 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2732 /* Flipping is only supported on RenderTargets and overlays*/
2733 if( !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_OVERLAY)) ) {
2734 WARN("Tried to flip a non-render target, non-overlay surface\n");
2735 return WINEDDERR_NOTFLIPPABLE;
2738 if(This->resource.usage & WINED3DUSAGE_OVERLAY) {
2739 flip_surface(This, (IWineD3DSurfaceImpl *) override);
2741 /* Update the overlay if it is visible */
2742 if(This->overlay_dest) {
2743 return IWineD3DSurface_DrawOverlay((IWineD3DSurface *) This);
2750 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2751 * FIXME("(%p) Target override is not supported by now\n", This);
2752 * Additionally, it isn't really possible to support triple-buffering
2753 * properly on opengl at all
2757 if (This->container.type != WINED3D_CONTAINER_SWAPCHAIN)
2759 ERR("Flipped surface is not on a swapchain\n");
2760 return WINEDDERR_NOTFLIPPABLE;
2762 swapchain = This->container.u.swapchain;
2764 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2765 * and only d3d8 and d3d9 apps specify the presentation interval
2767 if (!(Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)))
2769 /* Most common case first to avoid wasting time on all the other cases */
2770 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2771 } else if(Flags & WINEDDFLIP_NOVSYNC) {
2772 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2773 } else if(Flags & WINEDDFLIP_INTERVAL2) {
2774 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2775 } else if(Flags & WINEDDFLIP_INTERVAL3) {
2776 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2778 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2781 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2782 return IWineD3DSwapChain_Present((IWineD3DSwapChain *)swapchain,
2783 NULL, NULL, swapchain->win_handle, NULL, 0);
2786 /* Does a direct frame buffer -> texture copy. Stretching is done
2787 * with single pixel copy calls
2789 static void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *dst_surface, IWineD3DSurfaceImpl *src_surface,
2790 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
2792 IWineD3DDeviceImpl *device = dst_surface->resource.device;
2795 struct wined3d_context *context;
2796 BOOL upsidedown = FALSE;
2797 RECT dst_rect = *dst_rect_in;
2799 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2800 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2802 if(dst_rect.top > dst_rect.bottom) {
2803 UINT tmp = dst_rect.bottom;
2804 dst_rect.bottom = dst_rect.top;
2809 context = context_acquire(device, src_surface);
2810 context_apply_blit_state(context, device);
2811 surface_internal_preload(dst_surface, SRGB_RGB);
2814 /* Bind the target texture */
2815 glBindTexture(dst_surface->texture_target, dst_surface->texture_name);
2816 checkGLcall("glBindTexture");
2817 if (surface_is_offscreen(src_surface))
2819 TRACE("Reading from an offscreen target\n");
2820 upsidedown = !upsidedown;
2821 glReadBuffer(device->offscreenBuffer);
2825 glReadBuffer(surface_get_gl_buffer(src_surface));
2827 checkGLcall("glReadBuffer");
2829 xrel = (float) (src_rect->right - src_rect->left) / (float) (dst_rect.right - dst_rect.left);
2830 yrel = (float) (src_rect->bottom - src_rect->top) / (float) (dst_rect.bottom - dst_rect.top);
2832 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2834 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2836 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2837 ERR("Texture filtering not supported in direct blit\n");
2840 else if ((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT)
2841 && ((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
2843 ERR("Texture filtering not supported in direct blit\n");
2847 && !((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2848 && !((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
2850 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2852 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2853 dst_rect.left /*xoffset */, dst_rect.top /* y offset */,
2854 src_rect->left, src_surface->currentDesc.Height - src_rect->bottom,
2855 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
2857 UINT yoffset = src_surface->currentDesc.Height - src_rect->top + dst_rect.top - 1;
2858 /* I have to process this row by row to swap the image,
2859 * otherwise it would be upside down, so stretching in y direction
2860 * doesn't cost extra time
2862 * However, stretching in x direction can be avoided if not necessary
2864 for(row = dst_rect.top; row < dst_rect.bottom; row++) {
2865 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2867 /* Well, that stuff works, but it's very slow.
2868 * find a better way instead
2872 for (col = dst_rect.left; col < dst_rect.right; ++col)
2874 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2875 dst_rect.left + col /* x offset */, row /* y offset */,
2876 src_rect->left + col * xrel, yoffset - (int) (row * yrel), 1, 1);
2881 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2882 dst_rect.left /* x offset */, row /* y offset */,
2883 src_rect->left, yoffset - (int) (row * yrel), dst_rect.right - dst_rect.left, 1);
2887 checkGLcall("glCopyTexSubImage2D");
2890 context_release(context);
2892 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
2893 * path is never entered
2895 surface_modify_location(dst_surface, SFLAG_INTEXTURE, TRUE);
2898 /* Uses the hardware to stretch and flip the image */
2899 static void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *dst_surface, IWineD3DSurfaceImpl *src_surface,
2900 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
2902 IWineD3DDeviceImpl *device = dst_surface->resource.device;
2903 GLuint src, backup = 0;
2904 IWineD3DSwapChainImpl *src_swapchain = NULL;
2905 float left, right, top, bottom; /* Texture coordinates */
2906 UINT fbwidth = src_surface->currentDesc.Width;
2907 UINT fbheight = src_surface->currentDesc.Height;
2908 struct wined3d_context *context;
2909 GLenum drawBuffer = GL_BACK;
2910 GLenum texture_target;
2911 BOOL noBackBufferBackup;
2913 BOOL upsidedown = FALSE;
2914 RECT dst_rect = *dst_rect_in;
2916 TRACE("Using hwstretch blit\n");
2917 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2918 context = context_acquire(device, src_surface);
2919 context_apply_blit_state(context, device);
2920 surface_internal_preload(dst_surface, SRGB_RGB);
2922 src_offscreen = surface_is_offscreen(src_surface);
2923 noBackBufferBackup = src_offscreen && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
2924 if (!noBackBufferBackup && !src_surface->texture_name)
2926 /* Get it a description */
2927 surface_internal_preload(src_surface, SRGB_RGB);
2931 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2932 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2934 if (context->aux_buffers >= 2)
2936 /* Got more than one aux buffer? Use the 2nd aux buffer */
2937 drawBuffer = GL_AUX1;
2939 else if ((!src_offscreen || device->offscreenBuffer == GL_BACK) && context->aux_buffers >= 1)
2941 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2942 drawBuffer = GL_AUX0;
2945 if(noBackBufferBackup) {
2946 glGenTextures(1, &backup);
2947 checkGLcall("glGenTextures");
2948 glBindTexture(GL_TEXTURE_2D, backup);
2949 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2950 texture_target = GL_TEXTURE_2D;
2952 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2953 * we are reading from the back buffer, the backup can be used as source texture
2955 texture_target = src_surface->texture_target;
2956 glBindTexture(texture_target, src_surface->texture_name);
2957 checkGLcall("glBindTexture(texture_target, src_surface->texture_name)");
2958 glEnable(texture_target);
2959 checkGLcall("glEnable(texture_target)");
2961 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2962 src_surface->flags &= ~SFLAG_INTEXTURE;
2965 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2966 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2968 if(dst_rect.top > dst_rect.bottom) {
2969 UINT tmp = dst_rect.bottom;
2970 dst_rect.bottom = dst_rect.top;
2977 TRACE("Reading from an offscreen target\n");
2978 upsidedown = !upsidedown;
2979 glReadBuffer(device->offscreenBuffer);
2983 glReadBuffer(surface_get_gl_buffer(src_surface));
2986 /* TODO: Only back up the part that will be overwritten */
2987 glCopyTexSubImage2D(texture_target, 0,
2988 0, 0 /* read offsets */,
2993 checkGLcall("glCopyTexSubImage2D");
2995 /* No issue with overriding these - the sampler is dirty due to blit usage */
2996 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
2997 wined3d_gl_mag_filter(magLookup, Filter));
2998 checkGLcall("glTexParameteri");
2999 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
3000 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
3001 checkGLcall("glTexParameteri");
3003 if (src_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3004 src_swapchain = src_surface->container.u.swapchain;
3005 if (!src_swapchain || src_surface == src_swapchain->back_buffers[0])
3007 src = backup ? backup : src_surface->texture_name;
3011 glReadBuffer(GL_FRONT);
3012 checkGLcall("glReadBuffer(GL_FRONT)");
3014 glGenTextures(1, &src);
3015 checkGLcall("glGenTextures(1, &src)");
3016 glBindTexture(GL_TEXTURE_2D, src);
3017 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
3019 /* TODO: Only copy the part that will be read. Use src_rect->left, src_rect->bottom as origin, but with the width watch
3020 * out for power of 2 sizes
3022 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_surface->pow2Width,
3023 src_surface->pow2Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
3024 checkGLcall("glTexImage2D");
3025 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
3026 0, 0 /* read offsets */,
3031 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3032 checkGLcall("glTexParameteri");
3033 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3034 checkGLcall("glTexParameteri");
3036 glReadBuffer(GL_BACK);
3037 checkGLcall("glReadBuffer(GL_BACK)");
3039 if(texture_target != GL_TEXTURE_2D) {
3040 glDisable(texture_target);
3041 glEnable(GL_TEXTURE_2D);
3042 texture_target = GL_TEXTURE_2D;
3045 checkGLcall("glEnd and previous");
3047 left = src_rect->left;
3048 right = src_rect->right;
3052 top = src_surface->currentDesc.Height - src_rect->top;
3053 bottom = src_surface->currentDesc.Height - src_rect->bottom;
3057 top = src_surface->currentDesc.Height - src_rect->bottom;
3058 bottom = src_surface->currentDesc.Height - src_rect->top;
3061 if (src_surface->flags & SFLAG_NORMCOORD)
3063 left /= src_surface->pow2Width;
3064 right /= src_surface->pow2Width;
3065 top /= src_surface->pow2Height;
3066 bottom /= src_surface->pow2Height;
3069 /* draw the source texture stretched and upside down. The correct surface is bound already */
3070 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3071 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3073 context_set_draw_buffer(context, drawBuffer);
3074 glReadBuffer(drawBuffer);
3078 glTexCoord2f(left, bottom);
3082 glTexCoord2f(left, top);
3083 glVertex2i(0, dst_rect.bottom - dst_rect.top);
3086 glTexCoord2f(right, top);
3087 glVertex2i(dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3090 glTexCoord2f(right, bottom);
3091 glVertex2i(dst_rect.right - dst_rect.left, 0);
3093 checkGLcall("glEnd and previous");
3095 if (texture_target != dst_surface->texture_target)
3097 glDisable(texture_target);
3098 glEnable(dst_surface->texture_target);
3099 texture_target = dst_surface->texture_target;
3102 /* Now read the stretched and upside down image into the destination texture */
3103 glBindTexture(texture_target, dst_surface->texture_name);
3104 checkGLcall("glBindTexture");
3105 glCopyTexSubImage2D(texture_target,
3107 dst_rect.left, dst_rect.top, /* xoffset, yoffset */
3108 0, 0, /* We blitted the image to the origin */
3109 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3110 checkGLcall("glCopyTexSubImage2D");
3112 if(drawBuffer == GL_BACK) {
3113 /* Write the back buffer backup back */
3115 if(texture_target != GL_TEXTURE_2D) {
3116 glDisable(texture_target);
3117 glEnable(GL_TEXTURE_2D);
3118 texture_target = GL_TEXTURE_2D;
3120 glBindTexture(GL_TEXTURE_2D, backup);
3121 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3125 if (texture_target != src_surface->texture_target)
3127 glDisable(texture_target);
3128 glEnable(src_surface->texture_target);
3129 texture_target = src_surface->texture_target;
3131 glBindTexture(src_surface->texture_target, src_surface->texture_name);
3132 checkGLcall("glBindTexture(src_surface->texture_target, src_surface->texture_name)");
3137 glTexCoord2f(0.0f, 0.0f);
3138 glVertex2i(0, fbheight);
3141 glTexCoord2f(0.0f, (float)fbheight / (float)src_surface->pow2Height);
3145 glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width,
3146 (float)fbheight / (float)src_surface->pow2Height);
3147 glVertex2i(fbwidth, 0);
3150 glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width, 0.0f);
3151 glVertex2i(fbwidth, fbheight);
3154 glDisable(texture_target);
3155 checkGLcall("glDisable(texture_target)");
3158 if (src != src_surface->texture_name && src != backup)
3160 glDeleteTextures(1, &src);
3161 checkGLcall("glDeleteTextures(1, &src)");
3164 glDeleteTextures(1, &backup);
3165 checkGLcall("glDeleteTextures(1, &backup)");
3170 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
3172 context_release(context);
3174 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3175 * path is never entered
3177 surface_modify_location(dst_surface, SFLAG_INTEXTURE, TRUE);
3180 /* Until the blit_shader is ready, define some prototypes here. */
3181 static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
3182 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
3183 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format);
3185 /* Front buffer coordinates are always full screen coordinates, but our GL
3186 * drawable is limited to the window's client area. The sysmem and texture
3187 * copies do have the full screen size. Note that GL has a bottom-left
3188 * origin, while D3D has a top-left origin. */
3189 void surface_translate_drawable_coords(IWineD3DSurfaceImpl *surface, HWND window, RECT *rect)
3191 UINT drawable_height;
3193 if (surface->container.type == WINED3D_CONTAINER_SWAPCHAIN
3194 && surface == surface->container.u.swapchain->front_buffer)
3196 POINT offset = {0, 0};
3199 ScreenToClient(window, &offset);
3200 OffsetRect(rect, offset.x, offset.y);
3202 GetClientRect(window, &windowsize);
3203 drawable_height = windowsize.bottom - windowsize.top;
3207 drawable_height = surface->currentDesc.Height;
3210 rect->top = drawable_height - rect->top;
3211 rect->bottom = drawable_height - rect->bottom;
3214 static BOOL surface_is_full_rect(IWineD3DSurfaceImpl *surface, const RECT *r)
3216 if ((r->left && r->right) || abs(r->right - r->left) != surface->currentDesc.Width)
3218 if ((r->top && r->bottom) || abs(r->bottom - r->top) != surface->currentDesc.Height)
3223 /* blit between surface locations. onscreen on different swapchains is not supported.
3224 * depth / stencil is not supported. */
3225 static void surface_blt_fbo(IWineD3DDeviceImpl *device, const WINED3DTEXTUREFILTERTYPE filter,
3226 IWineD3DSurfaceImpl *src_surface, DWORD src_location, const RECT *src_rect_in,
3227 IWineD3DSurfaceImpl *dst_surface, DWORD dst_location, const RECT *dst_rect_in)
3229 const struct wined3d_gl_info *gl_info;
3230 struct wined3d_context *context;
3231 RECT src_rect, dst_rect;
3234 TRACE("device %p, filter %s,\n", device, debug_d3dtexturefiltertype(filter));
3235 TRACE("src_surface %p, src_location %s, src_rect %s,\n",
3236 src_surface, debug_surflocation(src_location), wine_dbgstr_rect(src_rect_in));
3237 TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
3238 dst_surface, debug_surflocation(dst_location), wine_dbgstr_rect(dst_rect_in));
3240 src_rect = *src_rect_in;
3241 dst_rect = *dst_rect_in;
3245 case WINED3DTEXF_LINEAR:
3246 gl_filter = GL_LINEAR;
3250 FIXME("Unsupported filter mode %s (%#x).\n", debug_d3dtexturefiltertype(filter), filter);
3251 case WINED3DTEXF_NONE:
3252 case WINED3DTEXF_POINT:
3253 gl_filter = GL_NEAREST;
3257 if (src_location == SFLAG_INDRAWABLE && surface_is_offscreen(src_surface))
3258 src_location = SFLAG_INTEXTURE;
3259 if (dst_location == SFLAG_INDRAWABLE && surface_is_offscreen(dst_surface))
3260 dst_location = SFLAG_INTEXTURE;
3262 /* Make sure the locations are up-to-date. Loading the destination
3263 * surface isn't required if the entire surface is overwritten. (And is
3264 * in fact harmful if we're being called by surface_load_location() with
3265 * the purpose of loading the destination surface.) */
3266 surface_load_location(src_surface, src_location, NULL);
3267 if (!surface_is_full_rect(dst_surface, &dst_rect))
3268 surface_load_location(dst_surface, dst_location, NULL);
3270 if (src_location == SFLAG_INDRAWABLE) context = context_acquire(device, src_surface);
3271 else if (dst_location == SFLAG_INDRAWABLE) context = context_acquire(device, dst_surface);
3272 else context = context_acquire(device, NULL);
3274 if (!context->valid)
3276 context_release(context);
3277 WARN("Invalid context, skipping blit.\n");
3281 gl_info = context->gl_info;
3283 if (src_location == SFLAG_INDRAWABLE)
3285 GLenum buffer = surface_get_gl_buffer(src_surface);
3287 TRACE("Source surface %p is onscreen.\n", src_surface);
3289 surface_translate_drawable_coords(src_surface, context->win_handle, &src_rect);
3292 context_bind_fbo(context, GL_READ_FRAMEBUFFER, NULL);
3293 glReadBuffer(buffer);
3294 checkGLcall("glReadBuffer()");
3298 TRACE("Source surface %p is offscreen.\n", src_surface);
3300 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, src_surface, NULL, src_location);
3301 glReadBuffer(GL_COLOR_ATTACHMENT0);
3302 checkGLcall("glReadBuffer()");
3306 if (dst_location == SFLAG_INDRAWABLE)
3308 GLenum buffer = surface_get_gl_buffer(dst_surface);
3310 TRACE("Destination surface %p is onscreen.\n", dst_surface);
3312 surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
3315 context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, NULL);
3316 context_set_draw_buffer(context, buffer);
3320 TRACE("Destination surface %p is offscreen.\n", dst_surface);
3323 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, dst_surface, NULL, dst_location);
3324 context_set_draw_buffer(context, GL_COLOR_ATTACHMENT0);
3327 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3328 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE));
3329 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1));
3330 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2));
3331 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3));
3333 glDisable(GL_SCISSOR_TEST);
3334 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE));
3336 gl_info->fbo_ops.glBlitFramebuffer(src_rect.left, src_rect.top, src_rect.right, src_rect.bottom,
3337 dst_rect.left, dst_rect.top, dst_rect.right, dst_rect.bottom, GL_COLOR_BUFFER_BIT, gl_filter);
3338 checkGLcall("glBlitFramebuffer()");
3342 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
3344 context_release(context);
3347 static void surface_blt_to_drawable(IWineD3DDeviceImpl *device,
3348 WINED3DTEXTUREFILTERTYPE filter, BOOL color_key,
3349 IWineD3DSurfaceImpl *src_surface, const RECT *src_rect_in,
3350 IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect_in)
3352 IWineD3DSwapChainImpl *swapchain = NULL;
3353 struct wined3d_context *context;
3354 RECT src_rect, dst_rect;
3356 src_rect = *src_rect_in;
3357 dst_rect = *dst_rect_in;
3359 if (dst_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3360 swapchain = dst_surface->container.u.swapchain;
3362 /* Make sure the surface is up-to-date. This should probably use
3363 * surface_load_location() and worry about the destination surface too,
3364 * unless we're overwriting it completely. */
3365 surface_internal_preload(src_surface, SRGB_RGB);
3367 /* Activate the destination context, set it up for blitting */
3368 context = context_acquire(device, dst_surface);
3369 context_apply_blit_state(context, device);
3371 if (!surface_is_offscreen(dst_surface))
3372 surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
3374 device->blitter->set_shader((IWineD3DDevice *)device, src_surface);
3380 glEnable(GL_ALPHA_TEST);
3381 checkGLcall("glEnable(GL_ALPHA_TEST)");
3383 /* When the primary render target uses P8, the alpha component
3384 * contains the palette index. Which means that the colorkey is one of
3385 * the palette entries. In other cases pixels that should be masked
3386 * away have alpha set to 0. */
3387 if (primary_render_target_is_p8(device))
3388 glAlphaFunc(GL_NOTEQUAL, (float)src_surface->SrcBltCKey.dwColorSpaceLowValue / 256.0f);
3390 glAlphaFunc(GL_NOTEQUAL, 0.0f);
3391 checkGLcall("glAlphaFunc");
3395 glDisable(GL_ALPHA_TEST);
3396 checkGLcall("glDisable(GL_ALPHA_TEST)");
3399 draw_textured_quad(src_surface, &src_rect, &dst_rect, filter);
3403 glDisable(GL_ALPHA_TEST);
3404 checkGLcall("glDisable(GL_ALPHA_TEST)");
3409 /* Leave the opengl state valid for blitting */
3410 device->blitter->unset_shader((IWineD3DDevice *)device);
3412 if (wined3d_settings.strict_draw_ordering || (swapchain
3413 && (dst_surface == swapchain->front_buffer || swapchain->num_contexts > 1)))
3414 wglFlush(); /* Flush to ensure ordering across contexts. */
3416 context_release(context);
3419 /* Do not call while under the GL lock. */
3420 HRESULT surface_color_fill(IWineD3DSurfaceImpl *s, const RECT *rect, const WINED3DCOLORVALUE *color)
3422 IWineD3DDeviceImpl *device = s->resource.device;
3423 const struct blit_shader *blitter;
3425 blitter = wined3d_select_blitter(&device->adapter->gl_info, BLIT_OP_COLOR_FILL,
3426 NULL, 0, 0, NULL, rect, s->resource.usage, s->resource.pool, s->resource.format);
3429 FIXME("No blitter is capable of performing the requested color fill operation.\n");
3430 return WINED3DERR_INVALIDCALL;
3433 return blitter->color_fill(device, s, rect, color);
3436 /* Not called from the VTable */
3437 /* Do not call while under the GL lock. */
3438 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *dst_surface, const RECT *DestRect,
3439 IWineD3DSurfaceImpl *src_surface, const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx,
3440 WINED3DTEXTUREFILTERTYPE Filter)
3442 IWineD3DDeviceImpl *device = dst_surface->resource.device;
3443 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3444 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
3445 RECT dst_rect, src_rect;
3447 TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, blt_fx %p, filter %s.\n",
3448 dst_surface, wine_dbgstr_rect(DestRect), src_surface, wine_dbgstr_rect(SrcRect),
3449 Flags, DDBltFx, debug_d3dtexturefiltertype(Filter));
3451 /* Get the swapchain. One of the surfaces has to be a primary surface */
3452 if (dst_surface->resource.pool == WINED3DPOOL_SYSTEMMEM)
3454 WARN("Destination is in sysmem, rejecting gl blt\n");
3455 return WINED3DERR_INVALIDCALL;
3458 if (dst_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3459 dstSwapchain = dst_surface->container.u.swapchain;
3463 if (src_surface->resource.pool == WINED3DPOOL_SYSTEMMEM)
3465 WARN("Src is in sysmem, rejecting gl blt\n");
3466 return WINED3DERR_INVALIDCALL;
3469 if (src_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3470 srcSwapchain = src_surface->container.u.swapchain;
3473 /* Early sort out of cases where no render target is used */
3474 if (!dstSwapchain && !srcSwapchain
3475 && src_surface != device->render_targets[0]
3476 && dst_surface != device->render_targets[0])
3478 TRACE("No surface is render target, not using hardware blit.\n");
3479 return WINED3DERR_INVALIDCALL;
3482 /* No destination color keying supported */
3483 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
3484 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3485 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3486 return WINED3DERR_INVALIDCALL;
3489 surface_get_rect(dst_surface, DestRect, &dst_rect);
3490 if (src_surface) surface_get_rect(src_surface, SrcRect, &src_rect);
3492 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3493 if (dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->back_buffers
3494 && dst_surface == dstSwapchain->front_buffer
3495 && src_surface == dstSwapchain->back_buffers[0])
3497 /* Half-Life does a Blt from the back buffer to the front buffer,
3498 * Full surface size, no flags... Use present instead
3500 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3503 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3506 TRACE("Looking if a Present can be done...\n");
3507 /* Source Rectangle must be full surface */
3508 if (src_rect.left || src_rect.top
3509 || src_rect.right != src_surface->currentDesc.Width
3510 || src_rect.bottom != src_surface->currentDesc.Height)
3512 TRACE("No, Source rectangle doesn't match\n");
3516 /* No stretching may occur */
3517 if(src_rect.right != dst_rect.right - dst_rect.left ||
3518 src_rect.bottom != dst_rect.bottom - dst_rect.top) {
3519 TRACE("No, stretching is done\n");
3523 /* Destination must be full surface or match the clipping rectangle */
3524 if (dst_surface->clipper && ((IWineD3DClipperImpl *)dst_surface->clipper)->hWnd)
3528 GetClientRect(((IWineD3DClipperImpl *)dst_surface->clipper)->hWnd, &cliprect);
3529 pos[0].x = dst_rect.left;
3530 pos[0].y = dst_rect.top;
3531 pos[1].x = dst_rect.right;
3532 pos[1].y = dst_rect.bottom;
3533 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *)dst_surface->clipper)->hWnd, pos, 2);
3535 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3536 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3538 TRACE("No, dest rectangle doesn't match(clipper)\n");
3539 TRACE("Clip rect at %s\n", wine_dbgstr_rect(&cliprect));
3540 TRACE("Blt dest: %s\n", wine_dbgstr_rect(&dst_rect));
3544 else if (dst_rect.left || dst_rect.top
3545 || dst_rect.right != dst_surface->currentDesc.Width
3546 || dst_rect.bottom != dst_surface->currentDesc.Height)
3548 TRACE("No, dest rectangle doesn't match(surface size)\n");
3554 /* These flags are unimportant for the flag check, remove them */
3555 if (!(Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)))
3557 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3559 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3560 * take very long, while a flip is fast.
3561 * This applies to Half-Life, which does such Blts every time it finished
3562 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3563 * menu. This is also used by all apps when they do windowed rendering
3565 * The problem is that flipping is not really the same as copying. After a
3566 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3567 * untouched. Therefore it's necessary to override the swap effect
3568 * and to set it back after the flip.
3570 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3574 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3575 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3577 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3578 IWineD3DSwapChain_Present((IWineD3DSwapChain *)dstSwapchain,
3579 NULL, NULL, dstSwapchain->win_handle, NULL, 0);
3581 dstSwapchain->presentParms.SwapEffect = orig_swap;
3588 TRACE("Unsupported blit between buffers on the same swapchain\n");
3589 return WINED3DERR_INVALIDCALL;
3590 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3591 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3592 return WINED3DERR_INVALIDCALL;
3593 } else if(dstSwapchain && srcSwapchain) {
3594 FIXME("Implement hardware blit between two different swapchains\n");
3595 return WINED3DERR_INVALIDCALL;
3597 else if (dstSwapchain)
3599 /* Handled with regular texture -> swapchain blit */
3600 if (src_surface == device->render_targets[0])
3601 TRACE("Blit from active render target to a swapchain\n");
3603 else if (srcSwapchain && dst_surface == device->render_targets[0])
3605 FIXME("Implement blit from a swapchain to the active render target\n");
3606 return WINED3DERR_INVALIDCALL;
3609 if ((srcSwapchain || src_surface == device->render_targets[0]) && !dstSwapchain)
3611 /* Blit from render target to texture */
3614 /* P8 read back is not implemented */
3615 if (src_surface->resource.format->id == WINED3DFMT_P8_UINT
3616 || dst_surface->resource.format->id == WINED3DFMT_P8_UINT)
3618 TRACE("P8 read back not supported by frame buffer to texture blit\n");
3619 return WINED3DERR_INVALIDCALL;
3622 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3623 TRACE("Color keying not supported by frame buffer to texture blit\n");
3624 return WINED3DERR_INVALIDCALL;
3625 /* Destination color key is checked above */
3628 if(dst_rect.right - dst_rect.left != src_rect.right - src_rect.left) {
3634 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3635 * flip the image nor scale it.
3637 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3638 * -> If the app wants a image width an unscaled width, copy it line per line
3639 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3640 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3641 * back buffer. This is slower than reading line per line, thus not used for flipping
3642 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3645 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3646 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3649 if (fbo_blit_supported(gl_info, BLIT_OP_BLIT,
3650 &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
3651 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
3653 surface_blt_fbo(device, Filter,
3654 src_surface, SFLAG_INDRAWABLE, &src_rect,
3655 dst_surface, SFLAG_INDRAWABLE, &dst_rect);
3656 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3658 else if (!stretchx || dst_rect.right - dst_rect.left > src_surface->currentDesc.Width
3659 || dst_rect.bottom - dst_rect.top > src_surface->currentDesc.Height)
3661 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3662 fb_copy_to_texture_direct(dst_surface, src_surface, &src_rect, &dst_rect, Filter);
3664 TRACE("Using hardware stretching to flip / stretch the texture\n");
3665 fb_copy_to_texture_hwstretch(dst_surface, src_surface, &src_rect, &dst_rect, Filter);
3668 if (!(dst_surface->flags & SFLAG_DONOTFREE))
3670 HeapFree(GetProcessHeap(), 0, dst_surface->resource.heapMemory);
3671 dst_surface->resource.allocatedMemory = NULL;
3672 dst_surface->resource.heapMemory = NULL;
3676 dst_surface->flags &= ~SFLAG_INSYSMEM;
3681 else if (src_surface)
3683 /* Blit from offscreen surface to render target */
3684 DWORD oldCKeyFlags = src_surface->CKeyFlags;
3685 WINEDDCOLORKEY oldBltCKey = src_surface->SrcBltCKey;
3687 TRACE("Blt from surface %p to rendertarget %p\n", src_surface, dst_surface);
3689 if (!(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3690 && fbo_blit_supported(gl_info, BLIT_OP_BLIT,
3691 &src_rect, src_surface->resource.usage, src_surface->resource.pool,
3692 src_surface->resource.format,
3693 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3694 dst_surface->resource.format))
3696 TRACE("Using surface_blt_fbo.\n");
3697 /* The source is always a texture, but never the currently active render target, and the texture
3698 * contents are never upside down. */
3699 surface_blt_fbo(device, Filter,
3700 src_surface, SFLAG_INDRAWABLE, &src_rect,
3701 dst_surface, SFLAG_INDRAWABLE, &dst_rect);
3702 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3706 if (!(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3707 && arbfp_blit.blit_supported(gl_info, BLIT_OP_BLIT,
3708 &src_rect, src_surface->resource.usage, src_surface->resource.pool,
3709 src_surface->resource.format,
3710 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3711 dst_surface->resource.format))
3713 return arbfp_blit_surface(device, src_surface, &src_rect, dst_surface, &dst_rect, BLIT_OP_BLIT, Filter);
3716 if (!device->blitter->blit_supported(gl_info, BLIT_OP_BLIT,
3717 &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
3718 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
3720 FIXME("Unsupported blit operation falling back to software\n");
3721 return WINED3DERR_INVALIDCALL;
3724 /* Color keying: Check if we have to do a color keyed blt,
3725 * and if not check if a color key is activated.
3727 * Just modify the color keying parameters in the surface and restore them afterwards
3728 * The surface keeps track of the color key last used to load the opengl surface.
3729 * PreLoad will catch the change to the flags and color key and reload if necessary.
3731 if(Flags & WINEDDBLT_KEYSRC) {
3732 /* Use color key from surface */
3733 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3734 /* Use color key from DDBltFx */
3735 src_surface->CKeyFlags |= WINEDDSD_CKSRCBLT;
3736 src_surface->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3738 /* Do not use color key */
3739 src_surface->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3742 surface_blt_to_drawable(device, Filter, Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE),
3743 src_surface, &src_rect, dst_surface, &dst_rect);
3745 /* Restore the color key parameters */
3746 src_surface->CKeyFlags = oldCKeyFlags;
3747 src_surface->SrcBltCKey = oldBltCKey;
3749 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3755 /* Source-Less Blit to render target */
3756 if (Flags & WINEDDBLT_COLORFILL)
3758 WINED3DCOLORVALUE color;
3760 TRACE("Colorfill\n");
3762 /* The color as given in the Blt function is in the surface format. */
3763 if (!surface_convert_color_to_float(dst_surface, DDBltFx->u5.dwFillColor, &color))
3764 return WINED3DERR_INVALIDCALL;
3766 return surface_color_fill(dst_surface, &dst_rect, &color);
3770 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3771 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3772 return WINED3DERR_INVALIDCALL;
3775 static HRESULT IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, const RECT *DestRect,
3776 IWineD3DSurface *src_surface, const RECT *src_rect, DWORD Flags, const WINEDDBLTFX *DDBltFx)
3778 IWineD3DDeviceImpl *device = This->resource.device;
3781 if (Flags & WINEDDBLT_DEPTHFILL)
3783 switch (This->resource.format->id)
3785 case WINED3DFMT_D16_UNORM:
3786 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3788 case WINED3DFMT_S1_UINT_D15_UNORM:
3789 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00007fff;
3791 case WINED3DFMT_D24_UNORM_S8_UINT:
3792 case WINED3DFMT_X8D24_UNORM:
3793 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3795 case WINED3DFMT_D32_UNORM:
3796 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3800 ERR("Unexpected format for depth fill: %s.\n", debug_d3dformat(This->resource.format->id));
3803 return IWineD3DDevice_Clear((IWineD3DDevice *)device, DestRect ? 1 : 0, DestRect,
3804 WINED3DCLEAR_ZBUFFER, 0x00000000, depth, 0x00000000);
3807 FIXME("(%p): Unsupp depthstencil blit\n", This);
3808 return WINED3DERR_INVALIDCALL;
3811 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, const RECT *DestRect,
3812 IWineD3DSurface *src_surface, const RECT *SrcRect, DWORD Flags,
3813 const WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter)
3815 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3816 IWineD3DSurfaceImpl *src = (IWineD3DSurfaceImpl *)src_surface;
3817 IWineD3DDeviceImpl *device = This->resource.device;
3819 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
3820 iface, wine_dbgstr_rect(DestRect), src_surface, wine_dbgstr_rect(SrcRect),
3821 Flags, DDBltFx, debug_d3dtexturefiltertype(Filter));
3822 TRACE("Usage is %s.\n", debug_d3dusage(This->resource.usage));
3824 if ((This->flags & SFLAG_LOCKED) || (src && (src->flags & SFLAG_LOCKED)))
3826 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3827 return WINEDDERR_SURFACEBUSY;
3830 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3831 * except depth blits, which seem to work
3833 if (This == device->depth_stencil || (src && src == device->depth_stencil))
3835 if (device->inScene && !(Flags & WINEDDBLT_DEPTHFILL))
3837 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3838 return WINED3DERR_INVALIDCALL;
3840 else if (SUCCEEDED(IWineD3DSurfaceImpl_BltZ(This, DestRect, src_surface, SrcRect, Flags, DDBltFx)))
3842 TRACE("Z Blit override handled the blit\n");
3847 /* Special cases for RenderTargets */
3848 if ((This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3849 || (src && (src->resource.usage & WINED3DUSAGE_RENDERTARGET)))
3851 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This, DestRect, src, SrcRect, Flags, DDBltFx, Filter)))
3855 /* For the rest call the X11 surface implementation.
3856 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3857 * other Blts are rather rare. */
3858 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, src_surface, SrcRect, Flags, DDBltFx, Filter);
3861 static HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty,
3862 IWineD3DSurface *src_surface, const RECT *rsrc, DWORD trans)
3864 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3865 IWineD3DSurfaceImpl *src = (IWineD3DSurfaceImpl *)src_surface;
3866 IWineD3DDeviceImpl *device = This->resource.device;
3868 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3869 iface, dstx, dsty, src_surface, wine_dbgstr_rect(rsrc), trans);
3871 if ((This->flags & SFLAG_LOCKED) || (src->flags & SFLAG_LOCKED))
3873 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3874 return WINEDDERR_SURFACEBUSY;
3877 if (device->inScene && (This == device->depth_stencil || src == device->depth_stencil))
3879 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3880 return WINED3DERR_INVALIDCALL;
3883 /* Special cases for RenderTargets */
3884 if ((This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3885 || (src->resource.usage & WINED3DUSAGE_RENDERTARGET))
3888 RECT SrcRect, DstRect;
3891 surface_get_rect(src, rsrc, &SrcRect);
3893 DstRect.left = dstx;
3895 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3896 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3898 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3899 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3900 Flags |= WINEDDBLT_KEYSRC;
3901 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3902 Flags |= WINEDDBLT_KEYDEST;
3903 if(trans & WINEDDBLTFAST_WAIT)
3904 Flags |= WINEDDBLT_WAIT;
3905 if(trans & WINEDDBLTFAST_DONOTWAIT)
3906 Flags |= WINEDDBLT_DONOTWAIT;
3908 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This,
3909 &DstRect, src, &SrcRect, Flags, NULL, WINED3DTEXF_POINT)))
3913 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, src_surface, rsrc, trans);
3916 static HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface)
3918 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3920 IWineD3DPaletteImpl *pal = This->palette;
3922 TRACE("(%p)\n", This);
3924 if (!pal) return WINED3D_OK;
3926 if (This->resource.format->id == WINED3DFMT_P8_UINT
3927 || This->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
3929 if (This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3931 /* Make sure the texture is up to date. This call doesn't do
3932 * anything if the texture is already up to date. */
3933 surface_load_location(This, SFLAG_INTEXTURE, NULL);
3935 /* We want to force a palette refresh, so mark the drawable as not being up to date */
3936 surface_modify_location(This, SFLAG_INDRAWABLE, FALSE);
3940 if (!(This->flags & SFLAG_INSYSMEM))
3942 TRACE("Palette changed with surface that does not have an up to date system memory copy.\n");
3943 surface_load_location(This, SFLAG_INSYSMEM, NULL);
3945 TRACE("Dirtifying surface\n");
3946 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
3950 if (This->flags & SFLAG_DIBSECTION)
3952 TRACE("(%p): Updating the hdc's palette\n", This);
3953 for (n=0; n<256; n++) {
3954 col[n].rgbRed = pal->palents[n].peRed;
3955 col[n].rgbGreen = pal->palents[n].peGreen;
3956 col[n].rgbBlue = pal->palents[n].peBlue;
3957 col[n].rgbReserved = 0;
3959 SetDIBColorTable(This->hDC, 0, 256, col);
3962 /* Propagate the changes to the drawable when we have a palette. */
3963 if (This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3964 surface_load_location(This, SFLAG_INDRAWABLE, NULL);
3969 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3970 /** Check against the maximum texture sizes supported by the video card **/
3971 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3972 const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
3973 unsigned int pow2Width, pow2Height;
3975 This->texture_name = 0;
3976 This->texture_target = GL_TEXTURE_2D;
3978 /* Non-power2 support */
3979 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
3981 pow2Width = This->currentDesc.Width;
3982 pow2Height = This->currentDesc.Height;
3986 /* Find the nearest pow2 match */
3987 pow2Width = pow2Height = 1;
3988 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3989 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3991 This->pow2Width = pow2Width;
3992 This->pow2Height = pow2Height;
3994 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height)
3996 /* TODO: Add support for non power two compressed textures. */
3997 if (This->resource.format->flags & WINED3DFMT_FLAG_COMPRESSED)
3999 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
4000 This, This->currentDesc.Width, This->currentDesc.Height);
4001 return WINED3DERR_NOTAVAILABLE;
4005 if (pow2Width != This->currentDesc.Width
4006 || pow2Height != This->currentDesc.Height)
4008 This->flags |= SFLAG_NONPOW2;
4011 TRACE("%p\n", This);
4012 if ((This->pow2Width > gl_info->limits.texture_size || This->pow2Height > gl_info->limits.texture_size)
4013 && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
4015 /* one of three options
4016 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)
4017 2: Set the texture to the maximum size (bad idea)
4018 3: WARN and return WINED3DERR_NOTAVAILABLE;
4019 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.
4021 if(This->resource.pool == WINED3DPOOL_DEFAULT || This->resource.pool == WINED3DPOOL_MANAGED)
4023 WARN("(%p) Unable to allocate a surface which exceeds the maximum OpenGL texture size\n", This);
4024 return WINED3DERR_NOTAVAILABLE;
4027 /* We should never use this surface in combination with OpenGL! */
4028 TRACE("(%p) Creating an oversized surface: %ux%u\n", This, This->pow2Width, This->pow2Height);
4032 /* Don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
4033 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
4034 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
4036 if (This->flags & SFLAG_NONPOW2 && gl_info->supported[ARB_TEXTURE_RECTANGLE]
4037 && !(This->resource.format->id == WINED3DFMT_P8_UINT
4038 && gl_info->supported[EXT_PALETTED_TEXTURE]
4039 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
4041 This->texture_target = GL_TEXTURE_RECTANGLE_ARB;
4042 This->pow2Width = This->currentDesc.Width;
4043 This->pow2Height = This->currentDesc.Height;
4044 This->flags &= ~(SFLAG_NONPOW2 | SFLAG_NORMCOORD);
4048 switch (wined3d_settings.offscreen_rendering_mode)
4051 This->get_drawable_size = get_drawable_size_fbo;
4054 case ORM_BACKBUFFER:
4055 This->get_drawable_size = get_drawable_size_backbuffer;
4059 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
4060 return WINED3DERR_INVALIDCALL;
4063 This->flags |= SFLAG_INSYSMEM;
4068 /* GL locking is done by the caller */
4069 static void surface_depth_blt(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
4070 GLuint texture, GLsizei w, GLsizei h, GLenum target)
4072 IWineD3DDeviceImpl *device = This->resource.device;
4073 GLint compare_mode = GL_NONE;
4074 struct blt_info info;
4075 GLint old_binding = 0;
4078 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
4080 glDisable(GL_CULL_FACE);
4081 glDisable(GL_BLEND);
4082 glDisable(GL_ALPHA_TEST);
4083 glDisable(GL_SCISSOR_TEST);
4084 glDisable(GL_STENCIL_TEST);
4085 glEnable(GL_DEPTH_TEST);
4086 glDepthFunc(GL_ALWAYS);
4087 glDepthMask(GL_TRUE);
4088 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
4089 glViewport(0, 0, w, h);
4091 SetRect(&rect, 0, h, w, 0);
4092 surface_get_blt_info(target, &rect, w, h, &info);
4093 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
4094 glGetIntegerv(info.binding, &old_binding);
4095 glBindTexture(info.bind_target, texture);
4096 if (gl_info->supported[ARB_SHADOW])
4098 glGetTexParameteriv(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, &compare_mode);
4099 if (compare_mode != GL_NONE) glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
4102 device->shader_backend->shader_select_depth_blt((IWineD3DDevice *)device,
4103 info.tex_type, &This->ds_current_size);
4105 glBegin(GL_TRIANGLE_STRIP);
4106 glTexCoord3fv(info.coords[0]);
4107 glVertex2f(-1.0f, -1.0f);
4108 glTexCoord3fv(info.coords[1]);
4109 glVertex2f(1.0f, -1.0f);
4110 glTexCoord3fv(info.coords[2]);
4111 glVertex2f(-1.0f, 1.0f);
4112 glTexCoord3fv(info.coords[3]);
4113 glVertex2f(1.0f, 1.0f);
4116 if (compare_mode != GL_NONE) glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, compare_mode);
4117 glBindTexture(info.bind_target, old_binding);
4121 device->shader_backend->shader_deselect_depth_blt((IWineD3DDevice *)device);
4124 void surface_modify_ds_location(IWineD3DSurfaceImpl *surface,
4125 DWORD location, UINT w, UINT h)
4127 TRACE("surface %p, new location %#x, w %u, h %u.\n", surface, location, w, h);
4129 if (location & ~SFLAG_DS_LOCATIONS)
4130 FIXME("Invalid location (%#x) specified.\n", location);
4132 surface->ds_current_size.cx = w;
4133 surface->ds_current_size.cy = h;
4134 surface->flags &= ~SFLAG_DS_LOCATIONS;
4135 surface->flags |= location;
4138 /* Context activation is done by the caller. */
4139 void surface_load_ds_location(IWineD3DSurfaceImpl *surface, struct wined3d_context *context, DWORD location)
4141 IWineD3DDeviceImpl *device = surface->resource.device;
4142 const struct wined3d_gl_info *gl_info = context->gl_info;
4144 TRACE("surface %p, new location %#x.\n", surface, location);
4146 /* TODO: Make this work for modes other than FBO */
4147 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
4149 if (!(surface->flags & location))
4151 surface->ds_current_size.cx = 0;
4152 surface->ds_current_size.cy = 0;
4155 if (surface->ds_current_size.cx == surface->currentDesc.Width
4156 && surface->ds_current_size.cy == surface->currentDesc.Height)
4158 TRACE("Location (%#x) is already up to date.\n", location);
4162 if (surface->current_renderbuffer)
4164 FIXME("Not supported with fixed up depth stencil.\n");
4168 if (!(surface->flags & SFLAG_LOCATIONS))
4170 FIXME("No up to date depth stencil location.\n");
4171 surface->flags |= location;
4175 if (location == SFLAG_DS_OFFSCREEN)
4177 GLint old_binding = 0;
4181 /* The render target is allowed to be smaller than the depth/stencil
4182 * buffer, so the onscreen depth/stencil buffer is potentially smaller
4183 * than the offscreen surface. Don't overwrite the offscreen surface
4184 * with undefined data. */
4185 w = min(surface->currentDesc.Width, context->swapchain->presentParms.BackBufferWidth);
4186 h = min(surface->currentDesc.Height, context->swapchain->presentParms.BackBufferHeight);
4188 TRACE("Copying onscreen depth buffer to depth texture.\n");
4192 if (!device->depth_blt_texture)
4194 glGenTextures(1, &device->depth_blt_texture);
4197 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
4198 * directly on the FBO texture. That's because we need to flip. */
4199 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4200 if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
4202 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
4203 bind_target = GL_TEXTURE_RECTANGLE_ARB;
4207 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
4208 bind_target = GL_TEXTURE_2D;
4210 glBindTexture(bind_target, device->depth_blt_texture);
4211 glCopyTexImage2D(bind_target, surface->texture_level, surface->resource.format->glInternal, 0, 0, w, h, 0);
4212 glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4213 glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4214 glTexParameteri(bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
4215 glTexParameteri(bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
4216 glTexParameteri(bind_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
4217 glTexParameteri(bind_target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
4218 glBindTexture(bind_target, old_binding);
4220 /* Setup the destination */
4221 if (!device->depth_blt_rb)
4223 gl_info->fbo_ops.glGenRenderbuffers(1, &device->depth_blt_rb);
4224 checkGLcall("glGenRenderbuffersEXT");
4226 if (device->depth_blt_rb_w != w || device->depth_blt_rb_h != h)
4228 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, device->depth_blt_rb);
4229 checkGLcall("glBindRenderbufferEXT");
4230 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, w, h);
4231 checkGLcall("glRenderbufferStorageEXT");
4232 device->depth_blt_rb_w = w;
4233 device->depth_blt_rb_h = h;
4236 context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
4237 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
4238 GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, device->depth_blt_rb);
4239 checkGLcall("glFramebufferRenderbufferEXT");
4240 context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, surface, FALSE);
4242 /* Do the actual blit */
4243 surface_depth_blt(surface, gl_info, device->depth_blt_texture, w, h, bind_target);
4244 checkGLcall("depth_blt");
4246 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4247 else context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4251 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4253 else if (location == SFLAG_DS_ONSCREEN)
4255 TRACE("Copying depth texture to onscreen depth buffer.\n");
4259 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4260 surface_depth_blt(surface, gl_info, surface->texture_name,
4261 surface->currentDesc.Width, surface->currentDesc.Height, surface->texture_target);
4262 checkGLcall("depth_blt");
4264 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4268 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4272 ERR("Invalid location (%#x) specified.\n", location);
4275 surface->flags |= location;
4276 surface->ds_current_size.cx = surface->currentDesc.Width;
4277 surface->ds_current_size.cy = surface->currentDesc.Height;
4280 void surface_modify_location(IWineD3DSurfaceImpl *surface, DWORD flag, BOOL persistent)
4282 IWineD3DSurfaceImpl *overlay;
4284 TRACE("surface %p, location %s, persistent %#x.\n",
4285 surface, debug_surflocation(flag), persistent);
4287 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4289 if (surface_is_offscreen(surface))
4291 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4292 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4296 TRACE("Surface %p is an onscreen surface.\n", surface);
4302 if (((surface->flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE))
4303 || ((surface->flags & SFLAG_INSRGBTEX) && !(flag & SFLAG_INSRGBTEX)))
4305 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
4307 TRACE("Passing to container.\n");
4308 IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture *)surface->container.u.texture, TRUE);
4311 surface->flags &= ~SFLAG_LOCATIONS;
4312 surface->flags |= flag;
4314 /* Redraw emulated overlays, if any */
4315 if (flag & SFLAG_INDRAWABLE && !list_empty(&surface->overlays))
4317 LIST_FOR_EACH_ENTRY(overlay, &surface->overlays, IWineD3DSurfaceImpl, overlay_entry)
4319 IWineD3DSurface_DrawOverlay((IWineD3DSurface *)overlay);
4325 if ((surface->flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) && (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)))
4327 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
4329 TRACE("Passing to container\n");
4330 IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture *)surface->container.u.texture, TRUE);
4333 surface->flags &= ~flag;
4336 if (!(surface->flags & SFLAG_LOCATIONS))
4338 ERR("Surface %p does not have any up to date location.\n", surface);
4342 HRESULT surface_load_location(IWineD3DSurfaceImpl *surface, DWORD flag, const RECT *rect)
4344 IWineD3DDeviceImpl *device = surface->resource.device;
4345 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4346 BOOL drawable_read_ok = surface_is_offscreen(surface);
4347 struct wined3d_format format;
4348 CONVERT_TYPES convert;
4349 int width, pitch, outpitch;
4351 BOOL in_fbo = FALSE;
4353 TRACE("surface %p, location %s, rect %s.\n", surface, debug_surflocation(flag), wine_dbgstr_rect(rect));
4355 if (surface->resource.usage & WINED3DUSAGE_DEPTHSTENCIL)
4357 if (flag == SFLAG_INTEXTURE)
4359 struct wined3d_context *context = context_acquire(device, NULL);
4360 surface_load_ds_location(surface, context, SFLAG_DS_OFFSCREEN);
4361 context_release(context);
4366 FIXME("Unimplemented location %s for depth/stencil buffers.\n", debug_surflocation(flag));
4367 return WINED3DERR_INVALIDCALL;
4371 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4373 if (surface_is_offscreen(surface))
4375 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4376 * Prefer SFLAG_INTEXTURE. */
4377 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4378 drawable_read_ok = FALSE;
4383 TRACE("Surface %p is an onscreen surface.\n", surface);
4387 if (surface->flags & flag)
4389 TRACE("Location already up to date\n");
4393 if (!(surface->flags & SFLAG_LOCATIONS))
4395 ERR("Surface %p does not have any up to date location.\n", surface);
4396 surface->flags |= SFLAG_LOST;
4397 return WINED3DERR_DEVICELOST;
4400 if (flag == SFLAG_INSYSMEM)
4402 surface_prepare_system_memory(surface);
4404 /* Download the surface to system memory */
4405 if (surface->flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX))
4407 struct wined3d_context *context = NULL;
4409 if (!device->isInDraw) context = context_acquire(device, NULL);
4411 surface_bind_and_dirtify(surface, !(surface->flags & SFLAG_INTEXTURE));
4412 surface_download_data(surface, gl_info);
4414 if (context) context_release(context);
4418 /* Note: It might be faster to download into a texture first. */
4419 read_from_framebuffer(surface, rect, surface->resource.allocatedMemory,
4420 IWineD3DSurface_GetPitch((IWineD3DSurface *)surface));
4423 else if (flag == SFLAG_INDRAWABLE)
4425 if (wined3d_settings.rendertargetlock_mode == RTL_READTEX)
4426 surface_load_location(surface, SFLAG_INTEXTURE, NULL);
4428 if (surface->flags & SFLAG_INTEXTURE)
4432 surface_get_rect(surface, rect, &r);
4433 surface_blt_to_drawable(device, WINED3DTEXF_POINT, FALSE, surface, &r, surface, &r);
4438 if ((surface->flags & SFLAG_LOCATIONS) == SFLAG_INSRGBTEX)
4440 /* This needs a shader to convert the srgb data sampled from the GL texture into RGB
4441 * values, otherwise we get incorrect values in the target. For now go the slow way
4442 * via a system memory copy
4444 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4447 d3dfmt_get_conv(surface, FALSE /* We need color keying */,
4448 FALSE /* We won't use textures */, &format, &convert);
4450 /* The width is in 'length' not in bytes */
4451 width = surface->currentDesc.Width;
4452 pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *)surface);
4454 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4455 * but it isn't set (yet) in all cases it is getting called. */
4456 if ((convert != NO_CONVERSION) && (surface->flags & SFLAG_PBO))
4458 struct wined3d_context *context = NULL;
4460 TRACE("Removing the pbo attached to surface %p.\n", surface);
4462 if (!device->isInDraw) context = context_acquire(device, NULL);
4463 surface_remove_pbo(surface, gl_info);
4464 if (context) context_release(context);
4467 if ((convert != NO_CONVERSION) && surface->resource.allocatedMemory)
4469 int height = surface->currentDesc.Height;
4470 byte_count = format.conv_byte_count;
4472 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4473 outpitch = width * byte_count;
4474 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4476 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4478 ERR("Out of memory %d, %d!\n", outpitch, height);
4479 return WINED3DERR_OUTOFVIDEOMEMORY;
4481 d3dfmt_convert_surface(surface->resource.allocatedMemory, mem, pitch,
4482 width, height, outpitch, convert, surface);
4484 surface->flags |= SFLAG_CONVERTED;
4488 surface->flags &= ~SFLAG_CONVERTED;
4489 mem = surface->resource.allocatedMemory;
4490 byte_count = format.byte_count;
4493 flush_to_framebuffer_drawpixels(surface, format.glFormat, format.glType, byte_count, mem);
4495 /* Don't delete PBO memory */
4496 if ((mem != surface->resource.allocatedMemory) && !(surface->flags & SFLAG_PBO))
4497 HeapFree(GetProcessHeap(), 0, mem);
4500 else /* if(flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) */
4502 const DWORD attach_flags = WINED3DFMT_FLAG_FBO_ATTACHABLE | WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB;
4504 if (drawable_read_ok && (surface->flags & SFLAG_INDRAWABLE))
4506 read_from_framebuffer_texture(surface, flag == SFLAG_INSRGBTEX);
4508 else if (surface->flags & (SFLAG_INSRGBTEX | SFLAG_INTEXTURE)
4509 && (surface->resource.format->flags & attach_flags) == attach_flags
4510 && fbo_blit_supported(gl_info, BLIT_OP_BLIT,
4511 NULL, surface->resource.usage, surface->resource.pool, surface->resource.format,
4512 NULL, surface->resource.usage, surface->resource.pool, surface->resource.format))
4514 DWORD src_location = flag == SFLAG_INSRGBTEX ? SFLAG_INTEXTURE : SFLAG_INSRGBTEX;
4515 RECT rect = {0, 0, surface->currentDesc.Width, surface->currentDesc.Height};
4517 surface_blt_fbo(surface->resource.device, WINED3DTEXF_POINT,
4518 surface, src_location, &rect, surface, flag, &rect);
4522 /* Upload from system memory */
4523 BOOL srgb = flag == SFLAG_INSRGBTEX;
4524 struct wined3d_context *context = NULL;
4526 d3dfmt_get_conv(surface, TRUE /* We need color keying */,
4527 TRUE /* We will use textures */, &format, &convert);
4531 if ((surface->flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)) == SFLAG_INTEXTURE)
4533 /* Performance warning... */
4534 FIXME("Downloading RGB surface %p to reload it as sRGB.\n", surface);
4535 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4540 if ((surface->flags & (SFLAG_INSRGBTEX | SFLAG_INSYSMEM)) == SFLAG_INSRGBTEX)
4542 /* Performance warning... */
4543 FIXME("Downloading sRGB surface %p to reload it as RGB.\n", surface);
4544 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4547 if (!(surface->flags & SFLAG_INSYSMEM))
4549 WARN("Trying to load a texture from sysmem, but SFLAG_INSYSMEM is not set.\n");
4550 /* Lets hope we get it from somewhere... */
4551 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4554 if (!device->isInDraw) context = context_acquire(device, NULL);
4556 surface_prepare_texture(surface, gl_info, srgb);
4557 surface_bind_and_dirtify(surface, srgb);
4559 if (surface->CKeyFlags & WINEDDSD_CKSRCBLT)
4561 surface->flags |= SFLAG_GLCKEY;
4562 surface->glCKey = surface->SrcBltCKey;
4564 else surface->flags &= ~SFLAG_GLCKEY;
4566 /* The width is in 'length' not in bytes */
4567 width = surface->currentDesc.Width;
4568 pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *)surface);
4570 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4571 * but it isn't set (yet) in all cases it is getting called. */
4572 if ((convert != NO_CONVERSION || format.convert) && (surface->flags & SFLAG_PBO))
4574 TRACE("Removing the pbo attached to surface %p.\n", surface);
4575 surface_remove_pbo(surface, gl_info);
4580 /* This code is entered for texture formats which need a fixup. */
4581 int height = surface->currentDesc.Height;
4583 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4584 outpitch = width * format.conv_byte_count;
4585 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4587 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4589 ERR("Out of memory %d, %d!\n", outpitch, height);
4590 if (context) context_release(context);
4591 return WINED3DERR_OUTOFVIDEOMEMORY;
4593 format.convert(surface->resource.allocatedMemory, mem, pitch, width, height);
4595 else if (convert != NO_CONVERSION && surface->resource.allocatedMemory)
4597 /* This code is only entered for color keying fixups */
4598 int height = surface->currentDesc.Height;
4600 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4601 outpitch = width * format.conv_byte_count;
4602 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4604 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4606 ERR("Out of memory %d, %d!\n", outpitch, height);
4607 if (context) context_release(context);
4608 return WINED3DERR_OUTOFVIDEOMEMORY;
4610 d3dfmt_convert_surface(surface->resource.allocatedMemory, mem, pitch,
4611 width, height, outpitch, convert, surface);
4615 mem = surface->resource.allocatedMemory;
4618 /* Make sure the correct pitch is used */
4620 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4623 if (mem || (surface->flags & SFLAG_PBO))
4624 surface_upload_data(surface, gl_info, &format, srgb, mem);
4626 /* Restore the default pitch */
4628 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4631 if (context) context_release(context);
4633 /* Don't delete PBO memory */
4634 if ((mem != surface->resource.allocatedMemory) && !(surface->flags & SFLAG_PBO))
4635 HeapFree(GetProcessHeap(), 0, mem);
4639 if (!rect) surface->flags |= flag;
4641 if (in_fbo && (surface->flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)))
4643 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4644 surface->flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4650 static WINED3DSURFTYPE WINAPI IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface *iface) {
4651 return SURFACE_OPENGL;
4654 static HRESULT WINAPI IWineD3DSurfaceImpl_DrawOverlay(IWineD3DSurface *iface) {
4655 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4658 /* If there's no destination surface there is nothing to do */
4659 if(!This->overlay_dest) return WINED3D_OK;
4661 /* Blt calls ModifyLocation on the dest surface, which in turn calls DrawOverlay to
4662 * update the overlay. Prevent an endless recursion. */
4663 if (This->overlay_dest->flags & SFLAG_INOVERLAYDRAW)
4666 This->overlay_dest->flags |= SFLAG_INOVERLAYDRAW;
4667 hr = IWineD3DSurfaceImpl_Blt((IWineD3DSurface *)This->overlay_dest,
4668 &This->overlay_destrect, iface, &This->overlay_srcrect,
4669 WINEDDBLT_WAIT, NULL, WINED3DTEXF_LINEAR);
4670 This->overlay_dest->flags &= ~SFLAG_INOVERLAYDRAW;
4675 BOOL surface_is_offscreen(IWineD3DSurfaceImpl *surface)
4677 IWineD3DSwapChainImpl *swapchain = surface->container.u.swapchain;
4679 /* Not on a swapchain - must be offscreen */
4680 if (surface->container.type != WINED3D_CONTAINER_SWAPCHAIN) return TRUE;
4682 /* The front buffer is always onscreen */
4683 if (surface == swapchain->front_buffer) return FALSE;
4685 /* If the swapchain is rendered to an FBO, the backbuffer is
4686 * offscreen, otherwise onscreen */
4687 return swapchain->render_to_fbo;
4690 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4693 IWineD3DBaseSurfaceImpl_QueryInterface,
4694 IWineD3DBaseSurfaceImpl_AddRef,
4695 IWineD3DSurfaceImpl_Release,
4696 /* IWineD3DResource */
4697 IWineD3DBaseSurfaceImpl_GetParent,
4698 IWineD3DBaseSurfaceImpl_SetPrivateData,
4699 IWineD3DBaseSurfaceImpl_GetPrivateData,
4700 IWineD3DBaseSurfaceImpl_FreePrivateData,
4701 IWineD3DBaseSurfaceImpl_SetPriority,
4702 IWineD3DBaseSurfaceImpl_GetPriority,
4703 IWineD3DSurfaceImpl_PreLoad,
4704 IWineD3DSurfaceImpl_UnLoad,
4705 IWineD3DBaseSurfaceImpl_GetType,
4706 /* IWineD3DSurface */
4707 IWineD3DBaseSurfaceImpl_GetDesc,
4708 IWineD3DSurfaceImpl_Map,
4709 IWineD3DSurfaceImpl_Unmap,
4710 IWineD3DSurfaceImpl_GetDC,
4711 IWineD3DSurfaceImpl_ReleaseDC,
4712 IWineD3DSurfaceImpl_Flip,
4713 IWineD3DSurfaceImpl_Blt,
4714 IWineD3DBaseSurfaceImpl_GetBltStatus,
4715 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4716 IWineD3DBaseSurfaceImpl_IsLost,
4717 IWineD3DBaseSurfaceImpl_Restore,
4718 IWineD3DSurfaceImpl_BltFast,
4719 IWineD3DBaseSurfaceImpl_GetPalette,
4720 IWineD3DBaseSurfaceImpl_SetPalette,
4721 IWineD3DSurfaceImpl_RealizePalette,
4722 IWineD3DBaseSurfaceImpl_SetColorKey,
4723 IWineD3DBaseSurfaceImpl_GetPitch,
4724 IWineD3DSurfaceImpl_SetMem,
4725 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4726 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4727 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4728 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4729 IWineD3DBaseSurfaceImpl_SetClipper,
4730 IWineD3DBaseSurfaceImpl_GetClipper,
4732 IWineD3DSurfaceImpl_LoadTexture,
4733 IWineD3DSurfaceImpl_BindTexture,
4734 IWineD3DBaseSurfaceImpl_GetData,
4735 IWineD3DSurfaceImpl_SetFormat,
4736 IWineD3DSurfaceImpl_PrivateSetup,
4737 IWineD3DSurfaceImpl_GetImplType,
4738 IWineD3DSurfaceImpl_DrawOverlay
4741 static HRESULT ffp_blit_alloc(IWineD3DDevice *iface) { return WINED3D_OK; }
4742 /* Context activation is done by the caller. */
4743 static void ffp_blit_free(IWineD3DDevice *iface) { }
4745 /* This function is used in case of 8bit paletted textures using GL_EXT_paletted_texture */
4746 /* Context activation is done by the caller. */
4747 static void ffp_blit_p8_upload_palette(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info)
4750 BOOL colorkey_active = (surface->CKeyFlags & WINEDDSD_CKSRCBLT) ? TRUE : FALSE;
4752 d3dfmt_p8_init_palette(surface, table, colorkey_active);
4754 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
4756 GL_EXTCALL(glColorTableEXT(surface->texture_target, GL_RGBA, 256, GL_RGBA, GL_UNSIGNED_BYTE, table));
4760 /* Context activation is done by the caller. */
4761 static HRESULT ffp_blit_set(IWineD3DDevice *iface, IWineD3DSurfaceImpl *surface)
4763 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
4764 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4765 enum complex_fixup fixup = get_complex_fixup(surface->resource.format->color_fixup);
4767 /* When EXT_PALETTED_TEXTURE is around, palette conversion is done by the GPU
4768 * else the surface is converted in software at upload time in LoadLocation.
4770 if(fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4771 ffp_blit_p8_upload_palette(surface, gl_info);
4774 glEnable(surface->texture_target);
4775 checkGLcall("glEnable(surface->texture_target)");
4780 /* Context activation is done by the caller. */
4781 static void ffp_blit_unset(IWineD3DDevice *iface)
4783 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
4784 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4787 glDisable(GL_TEXTURE_2D);
4788 checkGLcall("glDisable(GL_TEXTURE_2D)");
4789 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
4791 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
4792 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4794 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4796 glDisable(GL_TEXTURE_RECTANGLE_ARB);
4797 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
4802 static BOOL ffp_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4803 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
4804 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
4806 enum complex_fixup src_fixup;
4808 if (blit_op == BLIT_OP_COLOR_FILL)
4810 if (!(dst_usage & WINED3DUSAGE_RENDERTARGET))
4812 TRACE("Color fill not supported\n");
4819 src_fixup = get_complex_fixup(src_format->color_fixup);
4820 if (TRACE_ON(d3d_surface) && TRACE_ON(d3d))
4822 TRACE("Checking support for fixup:\n");
4823 dump_color_fixup_desc(src_format->color_fixup);
4826 if (blit_op != BLIT_OP_BLIT)
4828 TRACE("Unsupported blit_op=%d\n", blit_op);
4832 if (!is_identity_fixup(dst_format->color_fixup))
4834 TRACE("Destination fixups are not supported\n");
4838 if (src_fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4840 TRACE("P8 fixup supported\n");
4844 /* We only support identity conversions. */
4845 if (is_identity_fixup(src_format->color_fixup))
4851 TRACE("[FAILED]\n");
4855 /* Do not call while under the GL lock. */
4856 static HRESULT ffp_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface,
4857 const RECT *dst_rect, const WINED3DCOLORVALUE *color)
4859 const RECT draw_rect = {0, 0, dst_surface->currentDesc.Width, dst_surface->currentDesc.Height};
4861 return device_clear_render_targets(device, 1 /* rt_count */, &dst_surface, 1 /* rect_count */,
4862 dst_rect, &draw_rect, WINED3DCLEAR_TARGET, color, 0.0f /* depth */, 0 /* stencil */);
4865 const struct blit_shader ffp_blit = {
4874 static HRESULT cpu_blit_alloc(IWineD3DDevice *iface)
4879 /* Context activation is done by the caller. */
4880 static void cpu_blit_free(IWineD3DDevice *iface)
4884 /* Context activation is done by the caller. */
4885 static HRESULT cpu_blit_set(IWineD3DDevice *iface, IWineD3DSurfaceImpl *surface)
4890 /* Context activation is done by the caller. */
4891 static void cpu_blit_unset(IWineD3DDevice *iface)
4895 static BOOL cpu_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4896 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
4897 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
4899 if (blit_op == BLIT_OP_COLOR_FILL)
4907 /* Do not call while under the GL lock. */
4908 static HRESULT cpu_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface,
4909 const RECT *dst_rect, const WINED3DCOLORVALUE *color)
4913 memset(&BltFx, 0, sizeof(BltFx));
4914 BltFx.dwSize = sizeof(BltFx);
4915 BltFx.u5.dwFillColor = wined3d_format_convert_from_float(dst_surface->resource.format, color);
4916 return IWineD3DBaseSurfaceImpl_Blt((IWineD3DSurface*)dst_surface, dst_rect,
4917 NULL, NULL, WINEDDBLT_COLORFILL, &BltFx, WINED3DTEXF_POINT);
4920 const struct blit_shader cpu_blit = {
4929 static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4930 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
4931 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
4933 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
4936 /* We only support blitting. Things like color keying / color fill should
4937 * be handled by other blitters.
4939 if (blit_op != BLIT_OP_BLIT)
4942 /* Source and/or destination need to be on the GL side */
4943 if (src_pool == WINED3DPOOL_SYSTEMMEM || dst_pool == WINED3DPOOL_SYSTEMMEM)
4946 if (!((src_format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (src_usage & WINED3DUSAGE_RENDERTARGET))
4947 && ((dst_format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (dst_usage & WINED3DUSAGE_RENDERTARGET)))
4950 if (!(src_format->id == dst_format->id
4951 || (is_identity_fixup(src_format->color_fixup)
4952 && is_identity_fixup(dst_format->color_fixup))))