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 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 (type == WINED3D_CONTAINER_SWAPCHAIN)
101 surface->get_drawable_size = get_drawable_size_swapchain;
105 switch (wined3d_settings.offscreen_rendering_mode)
108 surface->get_drawable_size = get_drawable_size_fbo;
112 surface->get_drawable_size = get_drawable_size_backbuffer;
116 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
121 surface->container.type = type;
122 surface->container.u.base = container;
129 enum tex_types tex_type;
130 GLfloat coords[4][3];
141 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct float_rect *f)
143 f->l = ((r->left * 2.0f) / w) - 1.0f;
144 f->t = ((r->top * 2.0f) / h) - 1.0f;
145 f->r = ((r->right * 2.0f) / w) - 1.0f;
146 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
149 static void surface_get_blt_info(GLenum target, const RECT *rect_in, GLsizei w, GLsizei h, struct blt_info *info)
151 GLfloat (*coords)[3] = info->coords;
168 FIXME("Unsupported texture target %#x\n", target);
169 /* Fall back to GL_TEXTURE_2D */
171 info->binding = GL_TEXTURE_BINDING_2D;
172 info->bind_target = GL_TEXTURE_2D;
173 info->tex_type = tex_2d;
174 coords[0][0] = (float)rect.left / w;
175 coords[0][1] = (float)rect.top / h;
178 coords[1][0] = (float)rect.right / w;
179 coords[1][1] = (float)rect.top / h;
182 coords[2][0] = (float)rect.left / w;
183 coords[2][1] = (float)rect.bottom / h;
186 coords[3][0] = (float)rect.right / w;
187 coords[3][1] = (float)rect.bottom / h;
191 case GL_TEXTURE_RECTANGLE_ARB:
192 info->binding = GL_TEXTURE_BINDING_RECTANGLE_ARB;
193 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
194 info->tex_type = tex_rect;
195 coords[0][0] = rect.left; coords[0][1] = rect.top; coords[0][2] = 0.0f;
196 coords[1][0] = rect.right; coords[1][1] = rect.top; coords[1][2] = 0.0f;
197 coords[2][0] = rect.left; coords[2][1] = rect.bottom; coords[2][2] = 0.0f;
198 coords[3][0] = rect.right; coords[3][1] = rect.bottom; coords[3][2] = 0.0f;
201 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
202 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
203 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
204 info->tex_type = tex_cube;
205 cube_coords_float(&rect, w, h, &f);
207 coords[0][0] = 1.0f; coords[0][1] = -f.t; coords[0][2] = -f.l;
208 coords[1][0] = 1.0f; coords[1][1] = -f.t; coords[1][2] = -f.r;
209 coords[2][0] = 1.0f; coords[2][1] = -f.b; coords[2][2] = -f.l;
210 coords[3][0] = 1.0f; coords[3][1] = -f.b; coords[3][2] = -f.r;
213 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
214 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
215 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
216 info->tex_type = tex_cube;
217 cube_coords_float(&rect, w, h, &f);
219 coords[0][0] = -1.0f; coords[0][1] = -f.t; coords[0][2] = f.l;
220 coords[1][0] = -1.0f; coords[1][1] = -f.t; coords[1][2] = f.r;
221 coords[2][0] = -1.0f; coords[2][1] = -f.b; coords[2][2] = f.l;
222 coords[3][0] = -1.0f; coords[3][1] = -f.b; coords[3][2] = f.r;
225 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
226 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
227 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
228 info->tex_type = tex_cube;
229 cube_coords_float(&rect, w, h, &f);
231 coords[0][0] = f.l; coords[0][1] = 1.0f; coords[0][2] = f.t;
232 coords[1][0] = f.r; coords[1][1] = 1.0f; coords[1][2] = f.t;
233 coords[2][0] = f.l; coords[2][1] = 1.0f; coords[2][2] = f.b;
234 coords[3][0] = f.r; coords[3][1] = 1.0f; coords[3][2] = f.b;
237 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
238 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
239 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
240 info->tex_type = tex_cube;
241 cube_coords_float(&rect, w, h, &f);
243 coords[0][0] = f.l; coords[0][1] = -1.0f; coords[0][2] = -f.t;
244 coords[1][0] = f.r; coords[1][1] = -1.0f; coords[1][2] = -f.t;
245 coords[2][0] = f.l; coords[2][1] = -1.0f; coords[2][2] = -f.b;
246 coords[3][0] = f.r; coords[3][1] = -1.0f; coords[3][2] = -f.b;
249 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
250 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
251 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
252 info->tex_type = tex_cube;
253 cube_coords_float(&rect, w, h, &f);
255 coords[0][0] = f.l; coords[0][1] = -f.t; coords[0][2] = 1.0f;
256 coords[1][0] = f.r; coords[1][1] = -f.t; coords[1][2] = 1.0f;
257 coords[2][0] = f.l; coords[2][1] = -f.b; coords[2][2] = 1.0f;
258 coords[3][0] = f.r; coords[3][1] = -f.b; coords[3][2] = 1.0f;
261 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
262 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
263 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
264 info->tex_type = tex_cube;
265 cube_coords_float(&rect, w, h, &f);
267 coords[0][0] = -f.l; coords[0][1] = -f.t; coords[0][2] = -1.0f;
268 coords[1][0] = -f.r; coords[1][1] = -f.t; coords[1][2] = -1.0f;
269 coords[2][0] = -f.l; coords[2][1] = -f.b; coords[2][2] = -1.0f;
270 coords[3][0] = -f.r; coords[3][1] = -f.b; coords[3][2] = -1.0f;
275 static inline void surface_get_rect(IWineD3DSurfaceImpl *This, const RECT *rect_in, RECT *rect_out)
278 *rect_out = *rect_in;
283 rect_out->right = This->currentDesc.Width;
284 rect_out->bottom = This->currentDesc.Height;
288 /* GL locking and context activation is done by the caller */
289 void draw_textured_quad(IWineD3DSurfaceImpl *src_surface, const RECT *src_rect, const RECT *dst_rect, WINED3DTEXTUREFILTERTYPE Filter)
291 struct blt_info info;
293 surface_get_blt_info(src_surface->texture_target, src_rect, src_surface->pow2Width, src_surface->pow2Height, &info);
295 glEnable(info.bind_target);
296 checkGLcall("glEnable(bind_target)");
298 /* Bind the texture */
299 glBindTexture(info.bind_target, src_surface->texture_name);
300 checkGLcall("glBindTexture");
302 /* Filtering for StretchRect */
303 glTexParameteri(info.bind_target, GL_TEXTURE_MAG_FILTER,
304 wined3d_gl_mag_filter(magLookup, Filter));
305 checkGLcall("glTexParameteri");
306 glTexParameteri(info.bind_target, GL_TEXTURE_MIN_FILTER,
307 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
308 checkGLcall("glTexParameteri");
309 glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
310 glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
311 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
312 checkGLcall("glTexEnvi");
315 glBegin(GL_TRIANGLE_STRIP);
316 glTexCoord3fv(info.coords[0]);
317 glVertex2i(dst_rect->left, dst_rect->top);
319 glTexCoord3fv(info.coords[1]);
320 glVertex2i(dst_rect->right, dst_rect->top);
322 glTexCoord3fv(info.coords[2]);
323 glVertex2i(dst_rect->left, dst_rect->bottom);
325 glTexCoord3fv(info.coords[3]);
326 glVertex2i(dst_rect->right, dst_rect->bottom);
329 /* Unbind the texture */
330 glBindTexture(info.bind_target, 0);
331 checkGLcall("glBindTexture(info->bind_target, 0)");
333 /* We changed the filtering settings on the texture. Inform the
334 * container about this to get the filters reset properly next draw. */
335 if (src_surface->container.type == WINED3D_CONTAINER_TEXTURE)
337 IWineD3DBaseTextureImpl *texture = src_surface->container.u.texture;
338 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
339 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
340 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
344 HRESULT surface_init(IWineD3DSurfaceImpl *surface, WINED3DSURFTYPE surface_type, UINT alignment,
345 UINT width, UINT height, UINT level, BOOL lockable, BOOL discard, WINED3DMULTISAMPLE_TYPE multisample_type,
346 UINT multisample_quality, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
347 WINED3DPOOL pool, IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
349 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
350 const struct wined3d_format_desc *format_desc = getFormatDescEntry(format_id, gl_info);
351 void (*cleanup)(IWineD3DSurfaceImpl *This);
352 unsigned int resource_size;
355 if (multisample_quality > 0)
357 FIXME("multisample_quality set to %u, substituting 0\n", multisample_quality);
358 multisample_quality = 0;
361 /* FIXME: Check that the format is supported by the device. */
363 resource_size = wined3d_format_calculate_size(format_desc, alignment, width, height);
365 /* Look at the implementation and set the correct Vtable. */
366 switch (surface_type)
369 surface->lpVtbl = &IWineD3DSurface_Vtbl;
370 cleanup = surface_cleanup;
374 surface->lpVtbl = &IWineGDISurface_Vtbl;
375 cleanup = surface_gdi_cleanup;
379 ERR("Requested unknown surface implementation %#x.\n", surface_type);
380 return WINED3DERR_INVALIDCALL;
383 hr = resource_init((IWineD3DResource *)surface, WINED3DRTYPE_SURFACE,
384 device, resource_size, usage, format_desc, pool, parent, parent_ops);
387 WARN("Failed to initialize resource, returning %#x.\n", hr);
391 /* "Standalone" surface. */
392 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
394 surface->currentDesc.Width = width;
395 surface->currentDesc.Height = height;
396 surface->currentDesc.MultiSampleType = multisample_type;
397 surface->currentDesc.MultiSampleQuality = multisample_quality;
398 surface->texture_level = level;
399 list_init(&surface->overlays);
402 surface->Flags = SFLAG_NORMCOORD; /* Default to normalized coords. */
403 if (discard) surface->Flags |= SFLAG_DISCARD;
404 if (lockable || format_id == WINED3DFMT_D16_LOCKABLE) surface->Flags |= SFLAG_LOCKABLE;
406 /* Quick lockable sanity check.
407 * TODO: remove this after surfaces, usage and lockability have been debugged properly
408 * this function is too deep to need to care about things like this.
409 * Levels need to be checked too, since they all affect what can be done. */
412 case WINED3DPOOL_SCRATCH:
415 FIXME("Called with a pool of SCRATCH and a lockable of FALSE "
416 "which are mutually exclusive, setting lockable to TRUE.\n");
421 case WINED3DPOOL_SYSTEMMEM:
423 FIXME("Called with a pool of SYSTEMMEM and a lockable of FALSE, this is acceptable but unexpected.\n");
426 case WINED3DPOOL_MANAGED:
427 if (usage & WINED3DUSAGE_DYNAMIC)
428 FIXME("Called with a pool of MANAGED and a usage of DYNAMIC which are mutually exclusive.\n");
431 case WINED3DPOOL_DEFAULT:
432 if (lockable && !(usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
433 WARN("Creating a lockable surface with a POOL of DEFAULT, that doesn't specify DYNAMIC usage.\n");
437 FIXME("Unknown pool %#x.\n", pool);
441 if (usage & WINED3DUSAGE_RENDERTARGET && pool != WINED3DPOOL_DEFAULT)
443 FIXME("Trying to create a render target that isn't in the default pool.\n");
446 /* Mark the texture as dirty so that it gets loaded first time around. */
447 surface_add_dirty_rect(surface, NULL);
448 list_init(&surface->renderbuffers);
450 TRACE("surface %p, memory %p, size %u\n", surface, surface->resource.allocatedMemory, surface->resource.size);
452 /* Call the private setup routine */
453 hr = IWineD3DSurface_PrivateSetup((IWineD3DSurface *)surface);
456 ERR("Private setup failed, returning %#x\n", hr);
464 static void surface_force_reload(IWineD3DSurfaceImpl *surface)
466 surface->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
469 void surface_set_texture_name(IWineD3DSurfaceImpl *surface, GLuint new_name, BOOL srgb)
474 TRACE("surface %p, new_name %u, srgb %#x.\n", surface, new_name, srgb);
478 name = &surface->texture_name_srgb;
479 flag = SFLAG_INSRGBTEX;
483 name = &surface->texture_name;
484 flag = SFLAG_INTEXTURE;
487 if (!*name && new_name)
489 /* FIXME: We shouldn't need to remove SFLAG_INTEXTURE if the
490 * surface has no texture name yet. See if we can get rid of this. */
491 if (surface->Flags & flag)
492 ERR("Surface has %s set, but no texture name.\n", debug_surflocation(flag));
493 surface_modify_location(surface, flag, FALSE);
497 surface_force_reload(surface);
500 void surface_set_texture_target(IWineD3DSurfaceImpl *surface, GLenum target)
502 TRACE("surface %p, target %#x.\n", surface, target);
504 if (surface->texture_target != target)
506 if (target == GL_TEXTURE_RECTANGLE_ARB)
508 surface->Flags &= ~SFLAG_NORMCOORD;
510 else if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
512 surface->Flags |= SFLAG_NORMCOORD;
515 surface->texture_target = target;
516 surface_force_reload(surface);
519 /* Context activation is done by the caller. */
520 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl *This, BOOL srgb) {
521 DWORD active_sampler;
523 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
524 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
525 * gl states. The current texture unit should always be a valid one.
527 * To be more specific, this is tricky because we can implicitly be called
528 * from sampler() in state.c. This means we can't touch anything other than
529 * whatever happens to be the currently active texture, or we would risk
530 * marking already applied sampler states dirty again.
532 * TODO: Track the current active texture per GL context instead of using glGet
534 GLint active_texture;
536 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
538 active_sampler = This->resource.device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
540 if (active_sampler != WINED3D_UNMAPPED_STAGE)
542 IWineD3DDeviceImpl_MarkStateDirty(This->resource.device, STATE_SAMPLER(active_sampler));
544 IWineD3DSurface_BindTexture((IWineD3DSurface *)This, srgb);
547 /* This function checks if the primary render target uses the 8bit paletted format. */
548 static BOOL primary_render_target_is_p8(IWineD3DDeviceImpl *device)
550 if (device->render_targets && device->render_targets[0])
552 IWineD3DSurfaceImpl *render_target = device->render_targets[0];
553 if ((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET)
554 && (render_target->resource.format_desc->format == WINED3DFMT_P8_UINT))
560 /* This call just downloads data, the caller is responsible for binding the
561 * correct texture. */
562 /* Context activation is done by the caller. */
563 static void surface_download_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info)
565 const struct wined3d_format_desc *format_desc = This->resource.format_desc;
567 /* Only support read back of converted P8 surfaces */
568 if (This->Flags & SFLAG_CONVERTED && format_desc->format != WINED3DFMT_P8_UINT)
570 FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(format_desc->format));
576 if (format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED)
578 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p.\n",
579 This, This->texture_level, format_desc->glFormat, format_desc->glType,
580 This->resource.allocatedMemory);
582 if (This->Flags & SFLAG_PBO)
584 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
585 checkGLcall("glBindBufferARB");
586 GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target, This->texture_level, NULL));
587 checkGLcall("glGetCompressedTexImageARB");
588 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
589 checkGLcall("glBindBufferARB");
593 GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target,
594 This->texture_level, This->resource.allocatedMemory));
595 checkGLcall("glGetCompressedTexImageARB");
601 GLenum format = format_desc->glFormat;
602 GLenum type = format_desc->glType;
606 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
607 if (format_desc->format == WINED3DFMT_P8_UINT && primary_render_target_is_p8(This->resource.device))
610 type = GL_UNSIGNED_BYTE;
613 if (This->Flags & SFLAG_NONPOW2) {
614 unsigned char alignment = This->resource.device->surface_alignment;
615 src_pitch = format_desc->byte_count * This->pow2Width;
616 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
617 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
618 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
620 mem = This->resource.allocatedMemory;
623 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n",
624 This, This->texture_level, format, type, mem);
626 if(This->Flags & SFLAG_PBO) {
627 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
628 checkGLcall("glBindBufferARB");
630 glGetTexImage(This->texture_target, This->texture_level, format, type, NULL);
631 checkGLcall("glGetTexImage");
633 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
634 checkGLcall("glBindBufferARB");
636 glGetTexImage(This->texture_target, This->texture_level, format, type, mem);
637 checkGLcall("glGetTexImage");
641 if (This->Flags & SFLAG_NONPOW2) {
642 const BYTE *src_data;
646 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
647 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
648 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
650 * We're doing this...
652 * instead of boxing the texture :
653 * |<-texture width ->| -->pow2width| /\
654 * |111111111111111111| | |
655 * |222 Texture 222222| boxed empty | texture height
656 * |3333 Data 33333333| | |
657 * |444444444444444444| | \/
658 * ----------------------------------- |
659 * | boxed empty | boxed empty | pow2height
661 * -----------------------------------
664 * we're repacking the data to the expected texture width
666 * |<-texture width ->| -->pow2width| /\
667 * |111111111111111111222222222222222| |
668 * |222333333333333333333444444444444| texture height
672 * | empty | pow2height
674 * -----------------------------------
678 * |<-texture width ->| /\
679 * |111111111111111111|
680 * |222222222222222222|texture height
681 * |333333333333333333|
682 * |444444444444444444| \/
683 * --------------------
685 * this also means that any references to allocatedMemory should work with the data as if were a
686 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
688 * internally the texture is still stored in a boxed format so any references to textureName will
689 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
691 * Performance should not be an issue, because applications normally do not lock the surfaces when
692 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
693 * and doesn't have to be re-read.
696 dst_data = This->resource.allocatedMemory;
697 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
698 for (y = 1 ; y < This->currentDesc.Height; y++) {
699 /* skip the first row */
700 src_data += src_pitch;
701 dst_data += dst_pitch;
702 memcpy(dst_data, src_data, dst_pitch);
705 HeapFree(GetProcessHeap(), 0, mem);
709 /* Surface has now been downloaded */
710 This->Flags |= SFLAG_INSYSMEM;
713 /* This call just uploads data, the caller is responsible for binding the
714 * correct texture. */
715 /* Context activation is done by the caller. */
716 static void surface_upload_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
717 const struct wined3d_format_desc *format_desc, BOOL srgb, const GLvoid *data)
719 GLsizei width = This->currentDesc.Width;
720 GLsizei height = This->currentDesc.Height;
725 internal = format_desc->glGammaInternal;
727 else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET && surface_is_offscreen(This))
729 internal = format_desc->rtInternal;
733 internal = format_desc->glInternal;
736 TRACE("This %p, internal %#x, width %d, height %d, format %#x, type %#x, data %p.\n",
737 This, internal, width, height, format_desc->glFormat, format_desc->glType, data);
738 TRACE("target %#x, level %u, resource size %u.\n",
739 This->texture_target, This->texture_level, This->resource.size);
741 if (format_desc->heightscale != 1.0f && format_desc->heightscale != 0.0f) height *= format_desc->heightscale;
745 if (This->Flags & SFLAG_PBO)
747 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
748 checkGLcall("glBindBufferARB");
750 TRACE("(%p) pbo: %#x, data: %p.\n", This, This->pbo, data);
754 if (format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED)
756 TRACE("Calling glCompressedTexSubImage2DARB.\n");
758 GL_EXTCALL(glCompressedTexSubImage2DARB(This->texture_target, This->texture_level,
759 0, 0, width, height, internal, This->resource.size, data));
760 checkGLcall("glCompressedTexSubImage2DARB");
764 TRACE("Calling glTexSubImage2D.\n");
766 glTexSubImage2D(This->texture_target, This->texture_level,
767 0, 0, width, height, format_desc->glFormat, format_desc->glType, data);
768 checkGLcall("glTexSubImage2D");
771 if (This->Flags & SFLAG_PBO)
773 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
774 checkGLcall("glBindBufferARB");
779 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
781 IWineD3DDeviceImpl *device = This->resource.device;
784 for (i = 0; i < device->numContexts; ++i)
786 context_surface_update(device->contexts[i], This);
791 /* This call just allocates the texture, the caller is responsible for binding
792 * the correct texture. */
793 /* Context activation is done by the caller. */
794 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
795 const struct wined3d_format_desc *format_desc, BOOL srgb)
797 BOOL enable_client_storage = FALSE;
798 GLsizei width = This->pow2Width;
799 GLsizei height = This->pow2Height;
800 const BYTE *mem = NULL;
805 internal = format_desc->glGammaInternal;
807 else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET && surface_is_offscreen(This))
809 internal = format_desc->rtInternal;
813 internal = format_desc->glInternal;
816 if (format_desc->heightscale != 1.0f && format_desc->heightscale != 0.0f) height *= format_desc->heightscale;
818 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",
819 This, This->texture_target, This->texture_level, debug_d3dformat(format_desc->format),
820 internal, width, height, format_desc->glFormat, format_desc->glType);
824 if (gl_info->supported[APPLE_CLIENT_STORAGE])
826 if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
827 /* In some cases we want to disable client storage.
828 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
829 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
830 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
831 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
833 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
834 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
835 This->Flags &= ~SFLAG_CLIENT;
836 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_desc->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_desc->glFormat, format_desc->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_desc->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_desc *format = surface->resource.format_desc;
990 IWineD3DDeviceImpl *device = surface->resource.device;
992 switch (format->format)
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->format));
1040 static ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface)
1042 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1043 ULONG ref = InterlockedDecrement(&This->resource.ref);
1044 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
1048 surface_cleanup(This);
1049 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
1051 TRACE("(%p) Released.\n", This);
1052 HeapFree(GetProcessHeap(), 0, This);
1058 /* ****************************************************
1059 IWineD3DSurface IWineD3DResource parts follow
1060 **************************************************** */
1062 void surface_internal_preload(IWineD3DSurfaceImpl *surface, enum WINED3DSRGB srgb)
1064 IWineD3DDeviceImpl *device = surface->resource.device;
1066 TRACE("iface %p, srgb %#x.\n", surface, srgb);
1068 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1070 IWineD3DBaseTextureImpl *texture = surface->container.u.texture;
1072 TRACE("Passing to container.\n");
1073 texture->baseTexture.internal_preload((IWineD3DBaseTexture *)texture, srgb);
1077 struct wined3d_context *context = NULL;
1079 TRACE("(%p) : About to load surface\n", surface);
1081 if (!device->isInDraw) context = context_acquire(device, NULL);
1083 if (surface->resource.format_desc->format == WINED3DFMT_P8_UINT
1084 || surface->resource.format_desc->format == WINED3DFMT_P8_UINT_A8_UNORM)
1086 if (palette9_changed(surface))
1088 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
1089 /* TODO: This is not necessarily needed with hw palettized texture support */
1090 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
1091 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
1092 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
1096 IWineD3DSurface_LoadTexture((IWineD3DSurface *)surface, srgb == SRGB_SRGB ? TRUE : FALSE);
1098 if (surface->resource.pool == WINED3DPOOL_DEFAULT)
1100 /* Tell opengl to try and keep this texture in video ram (well mostly) */
1104 glPrioritizeTextures(1, &surface->texture_name, &tmp);
1108 if (context) context_release(context);
1112 static void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface)
1114 surface_internal_preload((IWineD3DSurfaceImpl *)iface, SRGB_ANY);
1117 /* Context activation is done by the caller. */
1118 static void surface_remove_pbo(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info)
1120 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1121 This->resource.allocatedMemory =
1122 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1125 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1126 checkGLcall("glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
1127 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0, This->resource.size, This->resource.allocatedMemory));
1128 checkGLcall("glGetBufferSubDataARB");
1129 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
1130 checkGLcall("glDeleteBuffersARB");
1134 This->Flags &= ~SFLAG_PBO;
1137 BOOL surface_init_sysmem(IWineD3DSurfaceImpl *surface)
1139 if (!surface->resource.allocatedMemory)
1141 surface->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1142 surface->resource.size + RESOURCE_ALIGNMENT);
1143 if (!surface->resource.heapMemory)
1145 ERR("Out of memory\n");
1148 surface->resource.allocatedMemory =
1149 (BYTE *)(((ULONG_PTR)surface->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1153 memset(surface->resource.allocatedMemory, 0, surface->resource.size);
1156 surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
1161 static void WINAPI IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface *iface)
1163 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1164 IWineD3DDeviceImpl *device = This->resource.device;
1165 const struct wined3d_gl_info *gl_info;
1166 renderbuffer_entry_t *entry, *entry2;
1167 struct wined3d_context *context;
1169 TRACE("(%p)\n", iface);
1171 if(This->resource.pool == WINED3DPOOL_DEFAULT) {
1172 /* Default pool resources are supposed to be destroyed before Reset is called.
1173 * Implicit resources stay however. So this means we have an implicit render target
1174 * or depth stencil. The content may be destroyed, but we still have to tear down
1175 * opengl resources, so we cannot leave early.
1177 * Put the surfaces into sysmem, and reset the content. The D3D content is undefined,
1178 * but we can't set the sysmem INDRAWABLE because when we're rendering the swapchain
1179 * or the depth stencil into an FBO the texture or render buffer will be removed
1180 * and all flags get lost
1182 surface_init_sysmem(This);
1186 /* Load the surface into system memory */
1187 surface_load_location(This, SFLAG_INSYSMEM, NULL);
1188 surface_modify_location(This, SFLAG_INDRAWABLE, FALSE);
1190 surface_modify_location(This, SFLAG_INTEXTURE, FALSE);
1191 surface_modify_location(This, SFLAG_INSRGBTEX, FALSE);
1192 This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
1194 context = context_acquire(device, NULL);
1195 gl_info = context->gl_info;
1197 /* Destroy PBOs, but load them into real sysmem before */
1198 if (This->Flags & SFLAG_PBO)
1199 surface_remove_pbo(This, gl_info);
1201 /* Destroy fbo render buffers. This is needed for implicit render targets, for
1202 * all application-created targets the application has to release the surface
1203 * before calling _Reset
1205 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
1207 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1209 list_remove(&entry->entry);
1210 HeapFree(GetProcessHeap(), 0, entry);
1212 list_init(&This->renderbuffers);
1213 This->current_renderbuffer = NULL;
1215 /* If we're in a texture, the texture name belongs to the texture.
1216 * Otherwise, destroy it. */
1217 if (This->container.type != WINED3D_CONTAINER_TEXTURE)
1220 glDeleteTextures(1, &This->texture_name);
1221 This->texture_name = 0;
1222 glDeleteTextures(1, &This->texture_name_srgb);
1223 This->texture_name_srgb = 0;
1227 context_release(context);
1229 resource_unload((IWineD3DResourceImpl *)This);
1232 /* ******************************************************
1233 IWineD3DSurface IWineD3DSurface parts follow
1234 ****************************************************** */
1236 /* Read the framebuffer back into the surface */
1237 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, const RECT *rect, void *dest, UINT pitch)
1239 IWineD3DDeviceImpl *device = This->resource.device;
1240 const struct wined3d_gl_info *gl_info;
1241 struct wined3d_context *context;
1245 BYTE *row, *top, *bottom;
1249 BOOL srcIsUpsideDown;
1254 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1255 static BOOL warned = FALSE;
1257 ERR("The application tries to lock the render target, but render target locking is disabled\n");
1263 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
1264 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
1265 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
1266 * context->last_was_blit set on the unlock.
1268 context = context_acquire(device, This);
1269 context_apply_blit_state(context, device);
1270 gl_info = context->gl_info;
1274 /* Select the correct read buffer, and give some debug output.
1275 * There is no need to keep track of the current read buffer or reset it, every part of the code
1276 * that reads sets the read buffer as desired.
1278 if (surface_is_offscreen(This))
1280 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
1281 * Read from the back buffer
1283 TRACE("Locking offscreen render target\n");
1284 glReadBuffer(device->offscreenBuffer);
1285 srcIsUpsideDown = TRUE;
1289 /* Onscreen surfaces are always part of a swapchain */
1290 GLenum buffer = surface_get_gl_buffer(This);
1291 TRACE("Locking %#x buffer\n", buffer);
1292 glReadBuffer(buffer);
1293 checkGLcall("glReadBuffer");
1294 srcIsUpsideDown = FALSE;
1297 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
1299 local_rect.left = 0;
1301 local_rect.right = This->currentDesc.Width;
1302 local_rect.bottom = This->currentDesc.Height;
1306 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
1308 switch(This->resource.format_desc->format)
1310 case WINED3DFMT_P8_UINT:
1312 if (primary_render_target_is_p8(device))
1314 /* In case of P8 render targets the index is stored in the alpha component */
1316 type = GL_UNSIGNED_BYTE;
1318 bpp = This->resource.format_desc->byte_count;
1320 /* GL can't return palettized data, so read ARGB pixels into a
1321 * separate block of memory and convert them into palettized format
1322 * in software. Slow, but if the app means to use palettized render
1323 * targets and locks it...
1325 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
1326 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
1327 * for the color channels when palettizing the colors.
1330 type = GL_UNSIGNED_BYTE;
1332 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
1334 ERR("Out of memory\n");
1338 bpp = This->resource.format_desc->byte_count * 3;
1345 fmt = This->resource.format_desc->glFormat;
1346 type = This->resource.format_desc->glType;
1347 bpp = This->resource.format_desc->byte_count;
1350 if(This->Flags & SFLAG_PBO) {
1351 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
1352 checkGLcall("glBindBufferARB");
1354 ERR("mem not null for pbo -- unexpected\n");
1359 /* Save old pixel store pack state */
1360 glGetIntegerv(GL_PACK_ROW_LENGTH, &rowLen);
1361 checkGLcall("glGetIntegerv");
1362 glGetIntegerv(GL_PACK_SKIP_PIXELS, &skipPix);
1363 checkGLcall("glGetIntegerv");
1364 glGetIntegerv(GL_PACK_SKIP_ROWS, &skipRow);
1365 checkGLcall("glGetIntegerv");
1367 /* Setup pixel store pack state -- to glReadPixels into the correct place */
1368 glPixelStorei(GL_PACK_ROW_LENGTH, This->currentDesc.Width);
1369 checkGLcall("glPixelStorei");
1370 glPixelStorei(GL_PACK_SKIP_PIXELS, local_rect.left);
1371 checkGLcall("glPixelStorei");
1372 glPixelStorei(GL_PACK_SKIP_ROWS, local_rect.top);
1373 checkGLcall("glPixelStorei");
1375 glReadPixels(local_rect.left, (!srcIsUpsideDown) ? (This->currentDesc.Height - local_rect.bottom) : local_rect.top ,
1376 local_rect.right - local_rect.left,
1377 local_rect.bottom - local_rect.top,
1379 checkGLcall("glReadPixels");
1381 /* Reset previous pixel store pack state */
1382 glPixelStorei(GL_PACK_ROW_LENGTH, rowLen);
1383 checkGLcall("glPixelStorei");
1384 glPixelStorei(GL_PACK_SKIP_PIXELS, skipPix);
1385 checkGLcall("glPixelStorei");
1386 glPixelStorei(GL_PACK_SKIP_ROWS, skipRow);
1387 checkGLcall("glPixelStorei");
1389 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_desc->format == 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) {
1435 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1436 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1441 context_release(context);
1443 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
1444 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
1445 * the same color but we have no choice.
1446 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
1448 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT && !primary_render_target_is_p8(device))
1450 const PALETTEENTRY *pal = NULL;
1451 DWORD width = pitch / 3;
1455 pal = This->palette->palents;
1457 ERR("Palette is missing, cannot perform inverse palette lookup\n");
1458 HeapFree(GetProcessHeap(), 0, mem);
1462 for(y = local_rect.top; y < local_rect.bottom; y++) {
1463 for(x = local_rect.left; x < local_rect.right; x++) {
1464 /* start lines pixels */
1465 const BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
1466 const BYTE *green = blue + 1;
1467 const BYTE *red = green + 1;
1469 for(c = 0; c < 256; c++) {
1470 if(*red == pal[c].peRed &&
1471 *green == pal[c].peGreen &&
1472 *blue == pal[c].peBlue)
1474 *((BYTE *) dest + y * width + x) = c;
1480 HeapFree(GetProcessHeap(), 0, mem);
1484 /* Read the framebuffer contents into a texture */
1485 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This, BOOL srgb)
1487 IWineD3DDeviceImpl *device = This->resource.device;
1488 const struct wined3d_gl_info *gl_info;
1489 struct wined3d_context *context;
1491 if (!surface_is_offscreen(This))
1493 /* We would need to flip onscreen surfaces, but there's no efficient
1494 * way to do that here. It makes more sense for the caller to
1495 * explicitly go through sysmem. */
1496 ERR("Not supported for onscreen targets.\n");
1500 /* Activate the surface to read from. In some situations it isn't the currently active target(e.g. backbuffer
1501 * locking during offscreen rendering). RESOURCELOAD is ok because glCopyTexSubImage2D isn't affected by any
1502 * states in the stateblock, and no driver was found yet that had bugs in that regard.
1504 context = context_acquire(device, This);
1505 gl_info = context->gl_info;
1507 surface_prepare_texture(This, gl_info, srgb);
1508 surface_bind_and_dirtify(This, srgb);
1510 TRACE("Reading back offscreen render target %p.\n", This);
1514 glReadBuffer(device->offscreenBuffer);
1515 checkGLcall("glReadBuffer");
1517 glCopyTexSubImage2D(This->texture_target, This->texture_level,
1518 0, 0, 0, 0, This->currentDesc.Width, This->currentDesc.Height);
1519 checkGLcall("glCopyTexSubImage2D");
1523 context_release(context);
1526 /* Context activation is done by the caller. */
1527 static void surface_prepare_texture_internal(IWineD3DSurfaceImpl *surface,
1528 const struct wined3d_gl_info *gl_info, BOOL srgb)
1530 DWORD alloc_flag = srgb ? SFLAG_SRGBALLOCATED : SFLAG_ALLOCATED;
1531 CONVERT_TYPES convert;
1532 struct wined3d_format_desc desc;
1534 if (surface->Flags & alloc_flag) return;
1536 d3dfmt_get_conv(surface, TRUE, TRUE, &desc, &convert);
1537 if(convert != NO_CONVERSION || desc.convert) surface->Flags |= SFLAG_CONVERTED;
1538 else surface->Flags &= ~SFLAG_CONVERTED;
1540 surface_bind_and_dirtify(surface, srgb);
1541 surface_allocate_surface(surface, gl_info, &desc, srgb);
1542 surface->Flags |= alloc_flag;
1545 /* Context activation is done by the caller. */
1546 void surface_prepare_texture(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info, BOOL srgb)
1548 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1550 IWineD3DBaseTextureImpl *texture = surface->container.u.texture;
1551 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
1554 TRACE("surface %p is a subresource of texture %p.\n", surface, texture);
1556 for (i = 0; i < sub_count; ++i)
1558 IWineD3DSurfaceImpl *s = (IWineD3DSurfaceImpl *)texture->baseTexture.sub_resources[i];
1559 surface_prepare_texture_internal(s, gl_info, srgb);
1565 surface_prepare_texture_internal(surface, gl_info, srgb);
1568 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This)
1570 IWineD3DDeviceImpl *device = This->resource.device;
1571 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1573 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
1574 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
1577 if(!(This->Flags & SFLAG_DYNLOCK)) {
1579 /* MAXLOCKCOUNT is defined in wined3d_private.h */
1580 if(This->lockCount > MAXLOCKCOUNT) {
1581 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
1582 This->Flags |= SFLAG_DYNLOCK;
1586 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
1587 * Also don't create a PBO for systemmem surfaces.
1589 if (gl_info->supported[ARB_PIXEL_BUFFER_OBJECT] && (This->Flags & SFLAG_DYNLOCK)
1590 && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2))
1591 && (This->resource.pool != WINED3DPOOL_SYSTEMMEM))
1594 struct wined3d_context *context;
1596 context = context_acquire(device, NULL);
1599 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
1600 error = glGetError();
1601 if(This->pbo == 0 || error != GL_NO_ERROR) {
1602 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
1605 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
1607 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1608 checkGLcall("glBindBufferARB");
1610 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
1611 checkGLcall("glBufferDataARB");
1613 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1614 checkGLcall("glBindBufferARB");
1616 /* We don't need the system memory anymore and we can't even use it for PBOs */
1617 if(!(This->Flags & SFLAG_CLIENT)) {
1618 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
1619 This->resource.heapMemory = NULL;
1621 This->resource.allocatedMemory = NULL;
1622 This->Flags |= SFLAG_PBO;
1624 context_release(context);
1626 else if (!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO))
1628 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
1631 if(!This->resource.heapMemory) {
1632 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1634 This->resource.allocatedMemory =
1635 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1636 if(This->Flags & SFLAG_INSYSMEM) {
1637 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1642 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
1643 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1644 IWineD3DDeviceImpl *device = This->resource.device;
1645 const RECT *pass_rect = pRect;
1647 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
1648 iface, pLockedRect, wine_dbgstr_rect(pRect), Flags);
1650 /* This is also done in the base class, but we have to verify this before loading any data from
1651 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1652 * may interfere, and all other bad things may happen
1654 if (This->Flags & SFLAG_LOCKED) {
1655 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1656 return WINED3DERR_INVALIDCALL;
1658 This->Flags |= SFLAG_LOCKED;
1660 if (!(This->Flags & SFLAG_LOCKABLE))
1662 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
1665 if (Flags & WINED3DLOCK_DISCARD) {
1666 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
1667 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
1668 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1669 This->Flags |= SFLAG_INSYSMEM;
1673 if (This->Flags & SFLAG_INSYSMEM) {
1674 TRACE("Local copy is up to date, not downloading data\n");
1675 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
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 == 0 && pRect->left == 0
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_LockRect(iface, pLockedRect, pRect, Flags);
1744 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This, GLenum fmt, GLenum type, UINT bpp, const BYTE *mem) {
1746 GLint prev_rasterpos[4];
1747 GLint skipBytes = 0;
1748 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
1749 IWineD3DDeviceImpl *device = This->resource.device;
1750 const struct wined3d_gl_info *gl_info;
1751 struct wined3d_context *context;
1753 /* Activate the correct context for the render target */
1754 context = context_acquire(device, This);
1755 context_apply_blit_state(context, device);
1756 gl_info = context->gl_info;
1760 if (!surface_is_offscreen(This))
1762 GLenum buffer = surface_get_gl_buffer(This);
1763 TRACE("Unlocking %#x buffer.\n", buffer);
1764 context_set_draw_buffer(context, buffer);
1768 /* Primary offscreen render target */
1769 TRACE("Offscreen render target.\n");
1770 context_set_draw_buffer(context, device->offscreenBuffer);
1773 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
1774 checkGLcall("glGetIntegerv");
1775 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
1776 checkGLcall("glGetIntegerv");
1777 glPixelZoom(1.0f, -1.0f);
1778 checkGLcall("glPixelZoom");
1780 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1781 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
1782 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
1784 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
1785 checkGLcall("glRasterPos3i");
1787 /* Some drivers(radeon dri, others?) don't like exceptions during
1788 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1789 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1790 * catch to put the dib section in InSync mode, which leads to a crash
1791 * and a blocked x server on my radeon card.
1793 * The following lines read the dib section so it is put in InSync mode
1794 * before glDrawPixels is called and the crash is prevented. There won't
1795 * be any interfering gdi accesses, because UnlockRect is called from
1796 * ReleaseDC, and the app won't use the dc any more afterwards.
1798 if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
1800 read = This->resource.allocatedMemory[0];
1803 if(This->Flags & SFLAG_PBO) {
1804 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1805 checkGLcall("glBindBufferARB");
1808 /* When the surface is locked we only have to refresh the locked part else we need to update the whole image */
1809 if(This->Flags & SFLAG_LOCKED) {
1810 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1811 (This->lockedRect.bottom - This->lockedRect.top)-1,
1813 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1814 checkGLcall("glDrawPixels");
1816 glDrawPixels(This->currentDesc.Width,
1817 This->currentDesc.Height,
1819 checkGLcall("glDrawPixels");
1822 if(This->Flags & SFLAG_PBO) {
1823 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1824 checkGLcall("glBindBufferARB");
1827 glPixelZoom(1.0f, 1.0f);
1828 checkGLcall("glPixelZoom");
1830 glRasterPos3iv(&prev_rasterpos[0]);
1831 checkGLcall("glRasterPos3iv");
1833 /* Reset to previous pack row length */
1834 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1835 checkGLcall("glPixelStorei(GL_UNPACK_ROW_LENGTH)");
1838 context_release(context);
1841 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1842 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1843 IWineD3DDeviceImpl *device = This->resource.device;
1846 if (!(This->Flags & SFLAG_LOCKED)) {
1847 WARN("trying to Unlock an unlocked surf@%p\n", This);
1848 return WINEDDERR_NOTLOCKED;
1851 if (This->Flags & SFLAG_PBO)
1853 const struct wined3d_gl_info *gl_info;
1854 struct wined3d_context *context;
1856 TRACE("Freeing PBO memory\n");
1858 context = context_acquire(device, NULL);
1859 gl_info = context->gl_info;
1862 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1863 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1864 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1865 checkGLcall("glUnmapBufferARB");
1867 context_release(context);
1869 This->resource.allocatedMemory = NULL;
1872 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1874 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1875 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1879 if (This->container.type == WINED3D_CONTAINER_SWAPCHAIN
1880 || (device->render_targets && This == device->render_targets[0]))
1882 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1883 static BOOL warned = FALSE;
1885 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1891 if(This->dirtyRect.left == 0 &&
1892 This->dirtyRect.top == 0 &&
1893 This->dirtyRect.right == This->currentDesc.Width &&
1894 This->dirtyRect.bottom == This->currentDesc.Height) {
1897 /* TODO: Proper partial rectangle tracking */
1898 fullsurface = FALSE;
1899 This->Flags |= SFLAG_INSYSMEM;
1902 switch(wined3d_settings.rendertargetlock_mode) {
1904 surface_load_location(This, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1908 surface_load_location(This, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1913 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1914 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1915 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1916 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1917 * not fully up to date because only a subrectangle was read in LockRect.
1919 This->Flags &= ~SFLAG_INSYSMEM;
1920 This->Flags |= SFLAG_INDRAWABLE;
1923 This->dirtyRect.left = This->currentDesc.Width;
1924 This->dirtyRect.top = This->currentDesc.Height;
1925 This->dirtyRect.right = 0;
1926 This->dirtyRect.bottom = 0;
1928 else if (This == device->depth_stencil)
1930 FIXME("Depth Stencil buffer locking is not implemented\n");
1932 /* The rest should be a normal texture */
1933 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1934 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1935 * states need resetting
1937 if (This->container.type == WINED3D_CONTAINER_TEXTURE)
1939 IWineD3DBaseTextureImpl *texture = This->container.u.texture;
1940 if (texture->baseTexture.bindCount)
1941 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_SAMPLER(texture->baseTexture.sampler));
1946 This->Flags &= ~SFLAG_LOCKED;
1947 memset(&This->lockedRect, 0, sizeof(RECT));
1949 /* Overlays have to be redrawn manually after changes with the GL implementation */
1950 if(This->overlay_dest) {
1951 IWineD3DSurface_DrawOverlay(iface);
1956 static void surface_release_client_storage(IWineD3DSurfaceImpl *surface)
1958 struct wined3d_context *context;
1960 context = context_acquire(surface->resource.device, NULL);
1963 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
1964 if (surface->texture_name)
1966 surface_bind_and_dirtify(surface, FALSE);
1967 glTexImage2D(surface->texture_target, surface->texture_level,
1968 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
1970 if (surface->texture_name_srgb)
1972 surface_bind_and_dirtify(surface, TRUE);
1973 glTexImage2D(surface->texture_target, surface->texture_level,
1974 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
1976 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1979 context_release(context);
1981 surface_modify_location(surface, SFLAG_INSRGBTEX, FALSE);
1982 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
1983 surface_force_reload(surface);
1986 static HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC)
1988 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1989 WINED3DLOCKED_RECT lock;
1993 TRACE("(%p)->(%p)\n",This,pHDC);
1995 if(This->Flags & SFLAG_USERPTR) {
1996 ERR("Not supported on surfaces with an application-provided surfaces\n");
1997 return WINEDDERR_NODC;
2000 /* Give more detailed info for ddraw */
2001 if (This->Flags & SFLAG_DCINUSE)
2002 return WINEDDERR_DCALREADYCREATED;
2004 /* Can't GetDC if the surface is locked */
2005 if (This->Flags & SFLAG_LOCKED)
2006 return WINED3DERR_INVALIDCALL;
2008 memset(&lock, 0, sizeof(lock)); /* To be sure */
2010 /* Create a DIB section if there isn't a hdc yet */
2013 if (This->Flags & SFLAG_CLIENT)
2015 surface_load_location(This, SFLAG_INSYSMEM, NULL);
2016 surface_release_client_storage(This);
2018 hr = IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
2019 if(FAILED(hr)) return WINED3DERR_INVALIDCALL;
2021 /* Use the dib section from now on if we are not using a PBO */
2022 if(!(This->Flags & SFLAG_PBO))
2023 This->resource.allocatedMemory = This->dib.bitmap_data;
2026 /* Lock the surface */
2027 hr = IWineD3DSurface_LockRect(iface,
2032 if(This->Flags & SFLAG_PBO) {
2033 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
2034 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
2038 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
2039 /* keep the dib section */
2043 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT
2044 || This->resource.format_desc->format == WINED3DFMT_P8_UINT_A8_UNORM)
2046 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
2047 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
2049 const PALETTEENTRY *pal = NULL;
2052 pal = This->palette->palents;
2054 IWineD3DSurfaceImpl *dds_primary;
2055 IWineD3DSwapChainImpl *swapchain;
2056 swapchain = (IWineD3DSwapChainImpl *)This->resource.device->swapchains[0];
2057 dds_primary = swapchain->front_buffer;
2058 if (dds_primary && dds_primary->palette)
2059 pal = dds_primary->palette->palents;
2063 for (n=0; n<256; n++) {
2064 col[n].rgbRed = pal[n].peRed;
2065 col[n].rgbGreen = pal[n].peGreen;
2066 col[n].rgbBlue = pal[n].peBlue;
2067 col[n].rgbReserved = 0;
2069 SetDIBColorTable(This->hDC, 0, 256, col);
2074 TRACE("returning %p\n",*pHDC);
2075 This->Flags |= SFLAG_DCINUSE;
2080 static HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC)
2082 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2084 TRACE("(%p)->(%p)\n",This,hDC);
2086 if (!(This->Flags & SFLAG_DCINUSE))
2087 return WINEDDERR_NODC;
2089 if (This->hDC !=hDC) {
2090 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
2091 return WINEDDERR_NODC;
2094 if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
2095 /* Copy the contents of the DIB over to the PBO */
2096 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
2099 /* we locked first, so unlock now */
2100 IWineD3DSurface_UnlockRect(iface);
2102 This->Flags &= ~SFLAG_DCINUSE;
2107 /* ******************************************************
2108 IWineD3DSurface Internal (No mapping to directx api) parts follow
2109 ****************************************************** */
2111 HRESULT d3dfmt_get_conv(IWineD3DSurfaceImpl *This, BOOL need_alpha_ck, BOOL use_texturing, struct wined3d_format_desc *desc, CONVERT_TYPES *convert)
2113 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
2114 IWineD3DDeviceImpl *device = This->resource.device;
2115 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2116 BOOL blit_supported = FALSE;
2118 /* Copy the default values from the surface. Below we might perform fixups */
2119 /* TODO: get rid of color keying desc fixups by using e.g. a table. */
2120 *desc = *This->resource.format_desc;
2121 *convert = NO_CONVERSION;
2123 /* Ok, now look if we have to do any conversion */
2124 switch(This->resource.format_desc->format)
2126 case WINED3DFMT_P8_UINT:
2131 /* Below the call to blit_supported is disabled for Wine 1.2 because the function isn't operating correctly yet.
2132 * At the moment 8-bit blits are handled in software and if certain GL extensions are around, surface conversion
2133 * is performed at upload time. The blit_supported call recognizes it as a destination fixup. This type of upload 'fixup'
2134 * and 8-bit to 8-bit blits need to be handled by the blit_shader.
2135 * TODO: get rid of this #if 0
2138 blit_supported = device->blitter->blit_supported(&device->adapter->gl_info, BLIT_OP_BLIT,
2139 &rect, This->resource.usage, This->resource.pool,
2140 This->resource.format_desc, &rect, This->resource.usage,
2141 This->resource.pool, This->resource.format_desc);
2143 blit_supported = gl_info->supported[EXT_PALETTED_TEXTURE] || gl_info->supported[ARB_FRAGMENT_PROGRAM];
2145 /* Use conversion when the blit_shader backend supports it. It only supports this in case of
2146 * texturing. Further also use conversion in case of color keying.
2147 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
2148 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
2149 * conflicts with this.
2151 if (!((blit_supported && device->render_targets && This == device->render_targets[0]))
2152 || colorkey_active || !use_texturing)
2154 desc->glFormat = GL_RGBA;
2155 desc->glInternal = GL_RGBA;
2156 desc->glType = GL_UNSIGNED_BYTE;
2157 desc->conv_byte_count = 4;
2158 if(colorkey_active) {
2159 *convert = CONVERT_PALETTED_CK;
2161 *convert = CONVERT_PALETTED;
2166 case WINED3DFMT_B2G3R3_UNORM:
2167 /* **********************
2168 GL_UNSIGNED_BYTE_3_3_2
2169 ********************** */
2170 if (colorkey_active) {
2171 /* This texture format will never be used.. So do not care about color keying
2172 up until the point in time it will be needed :-) */
2173 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
2177 case WINED3DFMT_B5G6R5_UNORM:
2178 if (colorkey_active) {
2179 *convert = CONVERT_CK_565;
2180 desc->glFormat = GL_RGBA;
2181 desc->glInternal = GL_RGB5_A1;
2182 desc->glType = GL_UNSIGNED_SHORT_5_5_5_1;
2183 desc->conv_byte_count = 2;
2187 case WINED3DFMT_B5G5R5X1_UNORM:
2188 if (colorkey_active) {
2189 *convert = CONVERT_CK_5551;
2190 desc->glFormat = GL_BGRA;
2191 desc->glInternal = GL_RGB5_A1;
2192 desc->glType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
2193 desc->conv_byte_count = 2;
2197 case WINED3DFMT_B8G8R8_UNORM:
2198 if (colorkey_active) {
2199 *convert = CONVERT_CK_RGB24;
2200 desc->glFormat = GL_RGBA;
2201 desc->glInternal = GL_RGBA8;
2202 desc->glType = GL_UNSIGNED_INT_8_8_8_8;
2203 desc->conv_byte_count = 4;
2207 case WINED3DFMT_B8G8R8X8_UNORM:
2208 if (colorkey_active) {
2209 *convert = CONVERT_RGB32_888;
2210 desc->glFormat = GL_RGBA;
2211 desc->glInternal = GL_RGBA8;
2212 desc->glType = GL_UNSIGNED_INT_8_8_8_8;
2213 desc->conv_byte_count = 4;
2224 void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey)
2226 IWineD3DDeviceImpl *device = This->resource.device;
2227 IWineD3DPaletteImpl *pal = This->palette;
2228 BOOL index_in_alpha = FALSE;
2231 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2232 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2233 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2234 * duplicate entries. Store the color key in the unused alpha component to speed the
2235 * download up and to make conversion unneeded. */
2236 index_in_alpha = primary_render_target_is_p8(device);
2240 UINT dxVersion = ((IWineD3DImpl *)device->wined3d)->dxVersion;
2242 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2245 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2248 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2249 * there's no palette at this time. */
2250 for (i = 0; i < 256; i++) table[i][3] = i;
2255 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2256 * alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2257 * capability flag is present (wine does advertise this capability) */
2258 for (i = 0; i < 256; ++i)
2260 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2261 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2262 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2263 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2269 TRACE("Using surface palette %p\n", pal);
2270 /* Get the surface's palette */
2271 for (i = 0; i < 256; ++i)
2273 table[i][0] = pal->palents[i].peRed;
2274 table[i][1] = pal->palents[i].peGreen;
2275 table[i][2] = pal->palents[i].peBlue;
2277 /* When index_in_alpha is set the palette index is stored in the
2278 * alpha component. In case of a readback we can then read
2279 * GL_ALPHA. Color keying is handled in BltOverride using a
2280 * GL_ALPHA_TEST using GL_NOT_EQUAL. In case of index_in_alpha the
2281 * color key itself is passed to glAlphaFunc in other cases the
2282 * alpha component of pixels that should be masked away is set to 0. */
2287 else if (colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue)
2288 && (i <= This->SrcBltCKey.dwColorSpaceHighValue))
2292 else if(pal->Flags & WINEDDPCAPS_ALPHA)
2294 table[i][3] = pal->palents[i].peFlags;
2304 static HRESULT d3dfmt_convert_surface(const BYTE *src, BYTE *dst, UINT pitch, UINT width,
2305 UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This)
2309 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
2314 memcpy(dst, src, pitch * height);
2317 case CONVERT_PALETTED:
2318 case CONVERT_PALETTED_CK:
2320 IWineD3DPaletteImpl* pal = This->palette;
2325 /* TODO: If we are a sublevel, try to get the palette from level 0 */
2328 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2330 for (y = 0; y < height; y++)
2332 source = src + pitch * y;
2333 dest = dst + outpitch * y;
2334 /* This is an 1 bpp format, using the width here is fine */
2335 for (x = 0; x < width; x++) {
2336 BYTE color = *source++;
2337 *dest++ = table[color][0];
2338 *dest++ = table[color][1];
2339 *dest++ = table[color][2];
2340 *dest++ = table[color][3];
2346 case CONVERT_CK_565:
2348 /* Converting the 565 format in 5551 packed to emulate color-keying.
2350 Note : in all these conversion, it would be best to average the averaging
2351 pixels to get the color of the pixel that will be color-keyed to
2352 prevent 'color bleeding'. This will be done later on if ever it is
2355 Note2: Nvidia documents say that their driver does not support alpha + color keying
2356 on the same surface and disables color keying in such a case
2362 TRACE("Color keyed 565\n");
2364 for (y = 0; y < height; y++) {
2365 Source = (const WORD *)(src + y * pitch);
2366 Dest = (WORD *) (dst + y * outpitch);
2367 for (x = 0; x < width; x++ ) {
2368 WORD color = *Source++;
2369 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
2370 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2371 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2380 case CONVERT_CK_5551:
2382 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
2386 TRACE("Color keyed 5551\n");
2387 for (y = 0; y < height; y++) {
2388 Source = (const WORD *)(src + y * pitch);
2389 Dest = (WORD *) (dst + y * outpitch);
2390 for (x = 0; x < width; x++ ) {
2391 WORD color = *Source++;
2393 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2394 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2398 *Dest &= ~(1 << 15);
2406 case CONVERT_CK_RGB24:
2408 /* Converting R8G8B8 format to R8G8B8A8 with color-keying. */
2410 for (y = 0; y < height; y++)
2412 source = src + pitch * y;
2413 dest = dst + outpitch * y;
2414 for (x = 0; x < width; x++) {
2415 DWORD color = ((DWORD)source[0] << 16) + ((DWORD)source[1] << 8) + (DWORD)source[2] ;
2416 DWORD dstcolor = color << 8;
2417 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2418 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2421 *(DWORD*)dest = dstcolor;
2429 case CONVERT_RGB32_888:
2431 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
2433 for (y = 0; y < height; y++)
2435 source = src + pitch * y;
2436 dest = dst + outpitch * y;
2437 for (x = 0; x < width; x++) {
2438 DWORD color = 0xffffff & *(const DWORD*)source;
2439 DWORD dstcolor = color << 8;
2440 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2441 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2444 *(DWORD*)dest = dstcolor;
2453 ERR("Unsupported conversion type %#x.\n", convert);
2458 BOOL palette9_changed(IWineD3DSurfaceImpl *This)
2460 IWineD3DDeviceImpl *device = This->resource.device;
2462 if (This->palette || (This->resource.format_desc->format != WINED3DFMT_P8_UINT
2463 && This->resource.format_desc->format != WINED3DFMT_P8_UINT_A8_UNORM))
2465 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2466 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2473 if (!memcmp(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256))
2478 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2480 memcpy(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2484 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2485 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2486 DWORD flag = srgb_mode ? SFLAG_INSRGBTEX : SFLAG_INTEXTURE;
2488 TRACE("iface %p, srgb %#x.\n", iface, srgb_mode);
2490 if (!(This->Flags & flag)) {
2491 TRACE("Reloading because surface is dirty\n");
2492 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2493 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2494 /* Reload: vice versa OR */
2495 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2496 /* Also reload: Color key is active AND the color key has changed */
2497 ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2498 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2499 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2500 TRACE("Reloading because of color keying\n");
2501 /* To perform the color key conversion we need a sysmem copy of
2502 * the surface. Make sure we have it
2505 surface_load_location(This, SFLAG_INSYSMEM, NULL);
2506 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2507 /* TODO: This is not necessarily needed with hw palettized texture support */
2508 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
2510 TRACE("surface is already in texture\n");
2514 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2515 * These resources are not bound by device size or format restrictions. Because of this,
2516 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2517 * However, these resources can always be created, locked, and copied.
2519 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2521 FIXME("(%p) Operation not supported for scratch textures\n",This);
2522 return WINED3DERR_INVALIDCALL;
2525 surface_load_location(This, flag, NULL /* no partial locking for textures yet */);
2527 if (!(This->Flags & SFLAG_DONOTFREE)) {
2528 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2529 This->resource.allocatedMemory = NULL;
2530 This->resource.heapMemory = NULL;
2531 surface_modify_location(This, SFLAG_INSYSMEM, FALSE);
2537 /* Context activation is done by the caller. */
2538 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface, BOOL srgb)
2540 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2542 TRACE("iface %p, srgb %#x.\n", iface, srgb);
2544 if (This->container.type == WINED3D_CONTAINER_TEXTURE)
2546 TRACE("Passing to container.\n");
2547 IWineD3DBaseTexture_BindTexture((IWineD3DBaseTexture *)This->container.u.texture, srgb);
2553 TRACE("(%p) : Binding surface\n", This);
2555 name = srgb ? &This->texture_name_srgb : &This->texture_name;
2559 if (!This->texture_level)
2562 glGenTextures(1, name);
2563 checkGLcall("glGenTextures");
2564 TRACE("Surface %p given name %d\n", This, *name);
2566 glBindTexture(This->texture_target, *name);
2567 checkGLcall("glBindTexture");
2568 glTexParameteri(This->texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2569 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
2570 glTexParameteri(This->texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2571 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
2572 glTexParameteri(This->texture_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2573 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)");
2574 glTexParameteri(This->texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2575 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
2576 glTexParameteri(This->texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2577 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
2579 /* This is where we should be reducing the amount of GLMemoryUsed */
2581 /* Mipmap surfaces should have a base texture container */
2582 ERR("Mipmap surface has a glTexture bound to it!\n");
2585 glBindTexture(This->texture_target, *name);
2586 checkGLcall("glBindTexture");
2592 static HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, enum wined3d_format_id format)
2594 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2597 TRACE("(%p) : Calling base function first\n", This);
2598 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2600 This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
2601 TRACE("(%p) : glFormat %d, glFormatInternal %d, glType %d\n", This, This->resource.format_desc->glFormat,
2602 This->resource.format_desc->glInternal, This->resource.format_desc->glType);
2607 static HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2608 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2610 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2611 WARN("Surface is locked or the HDC is in use\n");
2612 return WINED3DERR_INVALIDCALL;
2615 if(Mem && Mem != This->resource.allocatedMemory) {
2616 void *release = NULL;
2618 /* Do I have to copy the old surface content? */
2619 if(This->Flags & SFLAG_DIBSECTION) {
2620 /* Release the DC. No need to hold the critical section for the update
2621 * Thread because this thread runs only on front buffers, but this method
2622 * fails for render targets in the check above.
2624 SelectObject(This->hDC, This->dib.holdbitmap);
2625 DeleteDC(This->hDC);
2626 /* Release the DIB section */
2627 DeleteObject(This->dib.DIBsection);
2628 This->dib.bitmap_data = NULL;
2629 This->resource.allocatedMemory = NULL;
2631 This->Flags &= ~SFLAG_DIBSECTION;
2632 } else if(!(This->Flags & SFLAG_USERPTR)) {
2633 release = This->resource.heapMemory;
2634 This->resource.heapMemory = NULL;
2636 This->resource.allocatedMemory = Mem;
2637 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2639 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2640 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
2642 /* For client textures opengl has to be notified */
2643 if (This->Flags & SFLAG_CLIENT)
2644 surface_release_client_storage(This);
2646 /* Now free the old memory if any */
2647 HeapFree(GetProcessHeap(), 0, release);
2648 } else if(This->Flags & SFLAG_USERPTR) {
2649 /* LockRect and GetDC will re-create the dib section and allocated memory */
2650 This->resource.allocatedMemory = NULL;
2651 /* HeapMemory should be NULL already */
2652 if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2653 This->Flags &= ~SFLAG_USERPTR;
2655 if (This->Flags & SFLAG_CLIENT)
2656 surface_release_client_storage(This);
2661 void flip_surface(IWineD3DSurfaceImpl *front, IWineD3DSurfaceImpl *back) {
2663 /* Flip the surface contents */
2668 front->hDC = back->hDC;
2672 /* Flip the DIBsection */
2675 BOOL hasDib = front->Flags & SFLAG_DIBSECTION;
2676 tmp = front->dib.DIBsection;
2677 front->dib.DIBsection = back->dib.DIBsection;
2678 back->dib.DIBsection = tmp;
2680 if(back->Flags & SFLAG_DIBSECTION) front->Flags |= SFLAG_DIBSECTION;
2681 else front->Flags &= ~SFLAG_DIBSECTION;
2682 if(hasDib) back->Flags |= SFLAG_DIBSECTION;
2683 else back->Flags &= ~SFLAG_DIBSECTION;
2686 /* Flip the surface data */
2690 tmp = front->dib.bitmap_data;
2691 front->dib.bitmap_data = back->dib.bitmap_data;
2692 back->dib.bitmap_data = tmp;
2694 tmp = front->resource.allocatedMemory;
2695 front->resource.allocatedMemory = back->resource.allocatedMemory;
2696 back->resource.allocatedMemory = tmp;
2698 tmp = front->resource.heapMemory;
2699 front->resource.heapMemory = back->resource.heapMemory;
2700 back->resource.heapMemory = tmp;
2705 GLuint tmp_pbo = front->pbo;
2706 front->pbo = back->pbo;
2707 back->pbo = tmp_pbo;
2710 /* client_memory should not be different, but just in case */
2713 tmp = front->dib.client_memory;
2714 front->dib.client_memory = back->dib.client_memory;
2715 back->dib.client_memory = tmp;
2718 /* Flip the opengl texture */
2722 tmp = back->texture_name;
2723 back->texture_name = front->texture_name;
2724 front->texture_name = tmp;
2726 tmp = back->texture_name_srgb;
2727 back->texture_name_srgb = front->texture_name_srgb;
2728 front->texture_name_srgb = tmp;
2732 DWORD tmp_flags = back->Flags;
2733 back->Flags = front->Flags;
2734 front->Flags = tmp_flags;
2738 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2739 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2740 IWineD3DSwapChainImpl *swapchain = NULL;
2742 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2744 /* Flipping is only supported on RenderTargets and overlays*/
2745 if( !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_OVERLAY)) ) {
2746 WARN("Tried to flip a non-render target, non-overlay surface\n");
2747 return WINEDDERR_NOTFLIPPABLE;
2750 if(This->resource.usage & WINED3DUSAGE_OVERLAY) {
2751 flip_surface(This, (IWineD3DSurfaceImpl *) override);
2753 /* Update the overlay if it is visible */
2754 if(This->overlay_dest) {
2755 return IWineD3DSurface_DrawOverlay((IWineD3DSurface *) This);
2762 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2763 * FIXME("(%p) Target override is not supported by now\n", This);
2764 * Additionally, it isn't really possible to support triple-buffering
2765 * properly on opengl at all
2769 if (This->container.type != WINED3D_CONTAINER_SWAPCHAIN)
2771 ERR("Flipped surface is not on a swapchain\n");
2772 return WINEDDERR_NOTFLIPPABLE;
2774 swapchain = This->container.u.swapchain;
2776 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2777 * and only d3d8 and d3d9 apps specify the presentation interval
2779 if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
2780 /* Most common case first to avoid wasting time on all the other cases */
2781 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2782 } else if(Flags & WINEDDFLIP_NOVSYNC) {
2783 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2784 } else if(Flags & WINEDDFLIP_INTERVAL2) {
2785 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2786 } else if(Flags & WINEDDFLIP_INTERVAL3) {
2787 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2789 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2792 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2793 return IWineD3DSwapChain_Present((IWineD3DSwapChain *)swapchain,
2794 NULL, NULL, swapchain->win_handle, NULL, 0);
2797 /* Does a direct frame buffer -> texture copy. Stretching is done
2798 * with single pixel copy calls
2800 static void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *dst_surface, IWineD3DSurfaceImpl *src_surface,
2801 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
2803 IWineD3DDeviceImpl *device = dst_surface->resource.device;
2806 struct wined3d_context *context;
2807 BOOL upsidedown = FALSE;
2808 RECT dst_rect = *dst_rect_in;
2810 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2811 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2813 if(dst_rect.top > dst_rect.bottom) {
2814 UINT tmp = dst_rect.bottom;
2815 dst_rect.bottom = dst_rect.top;
2820 context = context_acquire(device, src_surface);
2821 context_apply_blit_state(context, device);
2822 surface_internal_preload(dst_surface, SRGB_RGB);
2825 /* Bind the target texture */
2826 glBindTexture(dst_surface->texture_target, dst_surface->texture_name);
2827 checkGLcall("glBindTexture");
2828 if (surface_is_offscreen(src_surface))
2830 TRACE("Reading from an offscreen target\n");
2831 upsidedown = !upsidedown;
2832 glReadBuffer(device->offscreenBuffer);
2836 glReadBuffer(surface_get_gl_buffer(src_surface));
2838 checkGLcall("glReadBuffer");
2840 xrel = (float) (src_rect->right - src_rect->left) / (float) (dst_rect.right - dst_rect.left);
2841 yrel = (float) (src_rect->bottom - src_rect->top) / (float) (dst_rect.bottom - dst_rect.top);
2843 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2845 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2847 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2848 ERR("Texture filtering not supported in direct blit\n");
2851 else if ((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT)
2852 && ((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
2854 ERR("Texture filtering not supported in direct blit\n");
2858 && !((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2859 && !((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
2861 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2863 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2864 dst_rect.left /*xoffset */, dst_rect.top /* y offset */,
2865 src_rect->left, src_surface->currentDesc.Height - src_rect->bottom,
2866 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
2868 UINT yoffset = src_surface->currentDesc.Height - src_rect->top + dst_rect.top - 1;
2869 /* I have to process this row by row to swap the image,
2870 * otherwise it would be upside down, so stretching in y direction
2871 * doesn't cost extra time
2873 * However, stretching in x direction can be avoided if not necessary
2875 for(row = dst_rect.top; row < dst_rect.bottom; row++) {
2876 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2878 /* Well, that stuff works, but it's very slow.
2879 * find a better way instead
2883 for (col = dst_rect.left; col < dst_rect.right; ++col)
2885 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2886 dst_rect.left + col /* x offset */, row /* y offset */,
2887 src_rect->left + col * xrel, yoffset - (int) (row * yrel), 1, 1);
2892 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2893 dst_rect.left /* x offset */, row /* y offset */,
2894 src_rect->left, yoffset - (int) (row * yrel), dst_rect.right - dst_rect.left, 1);
2898 checkGLcall("glCopyTexSubImage2D");
2901 context_release(context);
2903 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
2904 * path is never entered
2906 surface_modify_location(dst_surface, SFLAG_INTEXTURE, TRUE);
2909 /* Uses the hardware to stretch and flip the image */
2910 static void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *dst_surface, IWineD3DSurfaceImpl *src_surface,
2911 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
2913 IWineD3DDeviceImpl *device = dst_surface->resource.device;
2914 GLuint src, backup = 0;
2915 IWineD3DSwapChainImpl *src_swapchain = NULL;
2916 float left, right, top, bottom; /* Texture coordinates */
2917 UINT fbwidth = src_surface->currentDesc.Width;
2918 UINT fbheight = src_surface->currentDesc.Height;
2919 struct wined3d_context *context;
2920 GLenum drawBuffer = GL_BACK;
2921 GLenum texture_target;
2922 BOOL noBackBufferBackup;
2924 BOOL upsidedown = FALSE;
2925 RECT dst_rect = *dst_rect_in;
2927 TRACE("Using hwstretch blit\n");
2928 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2929 context = context_acquire(device, src_surface);
2930 context_apply_blit_state(context, device);
2931 surface_internal_preload(dst_surface, SRGB_RGB);
2933 src_offscreen = surface_is_offscreen(src_surface);
2934 noBackBufferBackup = src_offscreen && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
2935 if (!noBackBufferBackup && !src_surface->texture_name)
2937 /* Get it a description */
2938 surface_internal_preload(src_surface, SRGB_RGB);
2942 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2943 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2945 if (context->aux_buffers >= 2)
2947 /* Got more than one aux buffer? Use the 2nd aux buffer */
2948 drawBuffer = GL_AUX1;
2950 else if ((!src_offscreen || device->offscreenBuffer == GL_BACK) && context->aux_buffers >= 1)
2952 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2953 drawBuffer = GL_AUX0;
2956 if(noBackBufferBackup) {
2957 glGenTextures(1, &backup);
2958 checkGLcall("glGenTextures");
2959 glBindTexture(GL_TEXTURE_2D, backup);
2960 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2961 texture_target = GL_TEXTURE_2D;
2963 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2964 * we are reading from the back buffer, the backup can be used as source texture
2966 texture_target = src_surface->texture_target;
2967 glBindTexture(texture_target, src_surface->texture_name);
2968 checkGLcall("glBindTexture(texture_target, src_surface->texture_name)");
2969 glEnable(texture_target);
2970 checkGLcall("glEnable(texture_target)");
2972 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2973 src_surface->Flags &= ~SFLAG_INTEXTURE;
2976 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2977 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2979 if(dst_rect.top > dst_rect.bottom) {
2980 UINT tmp = dst_rect.bottom;
2981 dst_rect.bottom = dst_rect.top;
2988 TRACE("Reading from an offscreen target\n");
2989 upsidedown = !upsidedown;
2990 glReadBuffer(device->offscreenBuffer);
2994 glReadBuffer(surface_get_gl_buffer(src_surface));
2997 /* TODO: Only back up the part that will be overwritten */
2998 glCopyTexSubImage2D(texture_target, 0,
2999 0, 0 /* read offsets */,
3004 checkGLcall("glCopyTexSubImage2D");
3006 /* No issue with overriding these - the sampler is dirty due to blit usage */
3007 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
3008 wined3d_gl_mag_filter(magLookup, Filter));
3009 checkGLcall("glTexParameteri");
3010 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
3011 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
3012 checkGLcall("glTexParameteri");
3014 if (src_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3015 src_swapchain = src_surface->container.u.swapchain;
3016 if (!src_swapchain || src_surface == src_swapchain->back_buffers[0])
3018 src = backup ? backup : src_surface->texture_name;
3022 glReadBuffer(GL_FRONT);
3023 checkGLcall("glReadBuffer(GL_FRONT)");
3025 glGenTextures(1, &src);
3026 checkGLcall("glGenTextures(1, &src)");
3027 glBindTexture(GL_TEXTURE_2D, src);
3028 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
3030 /* TODO: Only copy the part that will be read. Use src_rect->left, src_rect->bottom as origin, but with the width watch
3031 * out for power of 2 sizes
3033 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_surface->pow2Width,
3034 src_surface->pow2Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
3035 checkGLcall("glTexImage2D");
3036 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
3037 0, 0 /* read offsets */,
3042 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3043 checkGLcall("glTexParameteri");
3044 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3045 checkGLcall("glTexParameteri");
3047 glReadBuffer(GL_BACK);
3048 checkGLcall("glReadBuffer(GL_BACK)");
3050 if(texture_target != GL_TEXTURE_2D) {
3051 glDisable(texture_target);
3052 glEnable(GL_TEXTURE_2D);
3053 texture_target = GL_TEXTURE_2D;
3056 checkGLcall("glEnd and previous");
3058 left = src_rect->left;
3059 right = src_rect->right;
3063 top = src_surface->currentDesc.Height - src_rect->top;
3064 bottom = src_surface->currentDesc.Height - src_rect->bottom;
3068 top = src_surface->currentDesc.Height - src_rect->bottom;
3069 bottom = src_surface->currentDesc.Height - src_rect->top;
3072 if (src_surface->Flags & SFLAG_NORMCOORD)
3074 left /= src_surface->pow2Width;
3075 right /= src_surface->pow2Width;
3076 top /= src_surface->pow2Height;
3077 bottom /= src_surface->pow2Height;
3080 /* draw the source texture stretched and upside down. The correct surface is bound already */
3081 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3082 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3084 context_set_draw_buffer(context, drawBuffer);
3085 glReadBuffer(drawBuffer);
3089 glTexCoord2f(left, bottom);
3090 glVertex2i(0, fbheight);
3093 glTexCoord2f(left, top);
3094 glVertex2i(0, fbheight - dst_rect.bottom - dst_rect.top);
3097 glTexCoord2f(right, top);
3098 glVertex2i(dst_rect.right - dst_rect.left, fbheight - dst_rect.bottom - dst_rect.top);
3101 glTexCoord2f(right, bottom);
3102 glVertex2i(dst_rect.right - dst_rect.left, fbheight);
3104 checkGLcall("glEnd and previous");
3106 if (texture_target != dst_surface->texture_target)
3108 glDisable(texture_target);
3109 glEnable(dst_surface->texture_target);
3110 texture_target = dst_surface->texture_target;
3113 /* Now read the stretched and upside down image into the destination texture */
3114 glBindTexture(texture_target, dst_surface->texture_name);
3115 checkGLcall("glBindTexture");
3116 glCopyTexSubImage2D(texture_target,
3118 dst_rect.left, dst_rect.top, /* xoffset, yoffset */
3119 0, 0, /* We blitted the image to the origin */
3120 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3121 checkGLcall("glCopyTexSubImage2D");
3123 if(drawBuffer == GL_BACK) {
3124 /* Write the back buffer backup back */
3126 if(texture_target != GL_TEXTURE_2D) {
3127 glDisable(texture_target);
3128 glEnable(GL_TEXTURE_2D);
3129 texture_target = GL_TEXTURE_2D;
3131 glBindTexture(GL_TEXTURE_2D, backup);
3132 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3136 if (texture_target != src_surface->texture_target)
3138 glDisable(texture_target);
3139 glEnable(src_surface->texture_target);
3140 texture_target = src_surface->texture_target;
3142 glBindTexture(src_surface->texture_target, src_surface->texture_name);
3143 checkGLcall("glBindTexture(src_surface->texture_target, src_surface->texture_name)");
3148 glTexCoord2f(0.0f, (float)fbheight / (float)src_surface->pow2Height);
3152 glTexCoord2f(0.0f, 0.0f);
3153 glVertex2i(0, fbheight);
3156 glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width, 0.0f);
3157 glVertex2i(fbwidth, src_surface->currentDesc.Height);
3160 glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width,
3161 (float)fbheight / (float)src_surface->pow2Height);
3162 glVertex2i(fbwidth, 0);
3165 glDisable(texture_target);
3166 checkGLcall("glDisable(texture_target)");
3169 if (src != src_surface->texture_name && src != backup)
3171 glDeleteTextures(1, &src);
3172 checkGLcall("glDeleteTextures(1, &src)");
3175 glDeleteTextures(1, &backup);
3176 checkGLcall("glDeleteTextures(1, &backup)");
3181 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
3183 context_release(context);
3185 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3186 * path is never entered
3188 surface_modify_location(dst_surface, SFLAG_INTEXTURE, TRUE);
3191 /* Until the blit_shader is ready, define some prototypes here. */
3192 static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
3193 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
3194 const struct wined3d_format_desc *src_format_desc,
3195 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
3196 const struct wined3d_format_desc *dst_format_desc);
3198 /* Front buffer coordinates are always full screen coordinates, but our GL
3199 * drawable is limited to the window's client area. The sysmem and texture
3200 * copies do have the full screen size. Note that GL has a bottom-left
3201 * origin, while D3D has a top-left origin. */
3202 void surface_translate_frontbuffer_coords(IWineD3DSurfaceImpl *surface, HWND window, RECT *rect)
3204 POINT offset = {0, surface->currentDesc.Height};
3207 GetClientRect(window, &windowsize);
3208 offset.y -= windowsize.bottom - windowsize.top;
3209 ScreenToClient(window, &offset);
3210 OffsetRect(rect, offset.x, offset.y);
3213 static BOOL surface_is_full_rect(IWineD3DSurfaceImpl *surface, const RECT *r)
3215 if ((r->left && r->right) || abs(r->right - r->left) != surface->currentDesc.Width)
3217 if ((r->top && r->bottom) || abs(r->bottom - r->top) != surface->currentDesc.Height)
3222 /* blit between surface locations. onscreen on different swapchains is not supported.
3223 * depth / stencil is not supported. */
3224 static void surface_blt_fbo(IWineD3DDeviceImpl *device, const WINED3DTEXTUREFILTERTYPE filter,
3225 IWineD3DSurfaceImpl *src_surface, DWORD src_location, const RECT *src_rect_in,
3226 IWineD3DSurfaceImpl *dst_surface, DWORD dst_location, const RECT *dst_rect_in)
3228 const struct wined3d_gl_info *gl_info;
3229 struct wined3d_context *context;
3230 RECT src_rect, dst_rect;
3233 TRACE("device %p, filter %s,\n", device, debug_d3dtexturefiltertype(filter));
3234 TRACE("src_surface %p, src_location %s, src_rect %s,\n",
3235 src_surface, debug_surflocation(src_location), wine_dbgstr_rect(src_rect_in));
3236 TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
3237 dst_surface, debug_surflocation(dst_location), wine_dbgstr_rect(dst_rect_in));
3239 src_rect = *src_rect_in;
3240 dst_rect = *dst_rect_in;
3244 case WINED3DTEXF_LINEAR:
3245 gl_filter = GL_LINEAR;
3249 FIXME("Unsupported filter mode %s (%#x).\n", debug_d3dtexturefiltertype(filter), filter);
3250 case WINED3DTEXF_NONE:
3251 case WINED3DTEXF_POINT:
3252 gl_filter = GL_NEAREST;
3256 if (src_location == SFLAG_INDRAWABLE && surface_is_offscreen(src_surface))
3257 src_location = SFLAG_INTEXTURE;
3258 if (dst_location == SFLAG_INDRAWABLE && surface_is_offscreen(dst_surface))
3259 dst_location = SFLAG_INTEXTURE;
3261 /* Make sure the locations are up-to-date. Loading the destination
3262 * surface isn't required if the entire surface is overwritten. (And is
3263 * in fact harmful if we're being called by surface_load_location() with
3264 * the purpose of loading the destination surface.) */
3265 surface_load_location(src_surface, src_location, NULL);
3266 if (!surface_is_full_rect(dst_surface, &dst_rect))
3267 surface_load_location(dst_surface, dst_location, NULL);
3269 if (src_location == SFLAG_INDRAWABLE) context = context_acquire(device, src_surface);
3270 else if (dst_location == SFLAG_INDRAWABLE) context = context_acquire(device, dst_surface);
3271 else context = context_acquire(device, NULL);
3273 if (!context->valid)
3275 context_release(context);
3276 WARN("Invalid context, skipping blit.\n");
3280 gl_info = context->gl_info;
3282 if (src_location == SFLAG_INDRAWABLE)
3284 GLenum buffer = surface_get_gl_buffer(src_surface);
3286 TRACE("Source surface %p is onscreen.\n", src_surface);
3288 if (buffer == GL_FRONT)
3289 surface_translate_frontbuffer_coords(src_surface, context->win_handle, &src_rect);
3291 src_rect.top = src_surface->currentDesc.Height - src_rect.top;
3292 src_rect.bottom = src_surface->currentDesc.Height - src_rect.bottom;
3295 context_bind_fbo(context, GL_READ_FRAMEBUFFER, NULL);
3296 glReadBuffer(buffer);
3297 checkGLcall("glReadBuffer()");
3301 TRACE("Source surface %p is offscreen.\n", src_surface);
3303 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, src_surface, NULL, src_location);
3304 glReadBuffer(GL_COLOR_ATTACHMENT0);
3305 checkGLcall("glReadBuffer()");
3309 if (dst_location == SFLAG_INDRAWABLE)
3311 GLenum buffer = surface_get_gl_buffer(dst_surface);
3313 TRACE("Destination surface %p is onscreen.\n", dst_surface);
3315 if (buffer == GL_FRONT)
3316 surface_translate_frontbuffer_coords(dst_surface, context->win_handle, &dst_rect);
3318 dst_rect.top = dst_surface->currentDesc.Height - dst_rect.top;
3319 dst_rect.bottom = dst_surface->currentDesc.Height - dst_rect.bottom;
3322 context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, NULL);
3323 context_set_draw_buffer(context, buffer);
3327 TRACE("Destination surface %p is offscreen.\n", dst_surface);
3330 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, dst_surface, NULL, dst_location);
3331 context_set_draw_buffer(context, GL_COLOR_ATTACHMENT0);
3334 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3335 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE));
3336 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1));
3337 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2));
3338 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3));
3340 glDisable(GL_SCISSOR_TEST);
3341 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE));
3343 gl_info->fbo_ops.glBlitFramebuffer(src_rect.left, src_rect.top, src_rect.right, src_rect.bottom,
3344 dst_rect.left, dst_rect.top, dst_rect.right, dst_rect.bottom, GL_COLOR_BUFFER_BIT, gl_filter);
3345 checkGLcall("glBlitFramebuffer()");
3349 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
3351 context_release(context);
3354 /* Not called from the VTable */
3355 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *dst_surface, const RECT *DestRect,
3356 IWineD3DSurfaceImpl *src_surface, const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx,
3357 WINED3DTEXTUREFILTERTYPE Filter)
3359 IWineD3DDeviceImpl *device = dst_surface->resource.device;
3360 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3361 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
3362 RECT dst_rect, src_rect;
3364 TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, blt_fx %p, filter %s.\n",
3365 dst_surface, wine_dbgstr_rect(DestRect), src_surface, wine_dbgstr_rect(SrcRect),
3366 Flags, DDBltFx, debug_d3dtexturefiltertype(Filter));
3368 /* Get the swapchain. One of the surfaces has to be a primary surface */
3369 if (dst_surface->resource.pool == WINED3DPOOL_SYSTEMMEM)
3371 WARN("Destination is in sysmem, rejecting gl blt\n");
3372 return WINED3DERR_INVALIDCALL;
3375 if (dst_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3376 dstSwapchain = dst_surface->container.u.swapchain;
3380 if (src_surface->resource.pool == WINED3DPOOL_SYSTEMMEM)
3382 WARN("Src is in sysmem, rejecting gl blt\n");
3383 return WINED3DERR_INVALIDCALL;
3386 if (src_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3387 srcSwapchain = src_surface->container.u.swapchain;
3390 /* Early sort out of cases where no render target is used */
3391 if (!dstSwapchain && !srcSwapchain
3392 && src_surface != device->render_targets[0]
3393 && dst_surface != device->render_targets[0])
3395 TRACE("No surface is render target, not using hardware blit.\n");
3396 return WINED3DERR_INVALIDCALL;
3399 /* No destination color keying supported */
3400 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
3401 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3402 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3403 return WINED3DERR_INVALIDCALL;
3406 surface_get_rect(dst_surface, DestRect, &dst_rect);
3407 if (src_surface) surface_get_rect(src_surface, SrcRect, &src_rect);
3409 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3410 if (dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->back_buffers
3411 && dst_surface == dstSwapchain->front_buffer
3412 && src_surface == dstSwapchain->back_buffers[0])
3414 /* Half-Life does a Blt from the back buffer to the front buffer,
3415 * Full surface size, no flags... Use present instead
3417 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3420 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3423 TRACE("Looking if a Present can be done...\n");
3424 /* Source Rectangle must be full surface */
3425 if (src_rect.left || src_rect.top
3426 || src_rect.right != src_surface->currentDesc.Width
3427 || src_rect.bottom != src_surface->currentDesc.Height)
3429 TRACE("No, Source rectangle doesn't match\n");
3433 /* No stretching may occur */
3434 if(src_rect.right != dst_rect.right - dst_rect.left ||
3435 src_rect.bottom != dst_rect.bottom - dst_rect.top) {
3436 TRACE("No, stretching is done\n");
3440 /* Destination must be full surface or match the clipping rectangle */
3441 if (dst_surface->clipper && ((IWineD3DClipperImpl *)dst_surface->clipper)->hWnd)
3445 GetClientRect(((IWineD3DClipperImpl *)dst_surface->clipper)->hWnd, &cliprect);
3446 pos[0].x = dst_rect.left;
3447 pos[0].y = dst_rect.top;
3448 pos[1].x = dst_rect.right;
3449 pos[1].y = dst_rect.bottom;
3450 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *)dst_surface->clipper)->hWnd, pos, 2);
3452 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3453 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3455 TRACE("No, dest rectangle doesn't match(clipper)\n");
3456 TRACE("Clip rect at %s\n", wine_dbgstr_rect(&cliprect));
3457 TRACE("Blt dest: %s\n", wine_dbgstr_rect(&dst_rect));
3461 else if (dst_rect.left || dst_rect.top
3462 || dst_rect.right != dst_surface->currentDesc.Width
3463 || dst_rect.bottom != dst_surface->currentDesc.Height)
3465 TRACE("No, dest rectangle doesn't match(surface size)\n");
3471 /* These flags are unimportant for the flag check, remove them */
3472 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
3473 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3475 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3476 * take very long, while a flip is fast.
3477 * This applies to Half-Life, which does such Blts every time it finished
3478 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3479 * menu. This is also used by all apps when they do windowed rendering
3481 * The problem is that flipping is not really the same as copying. After a
3482 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3483 * untouched. Therefore it's necessary to override the swap effect
3484 * and to set it back after the flip.
3486 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3490 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3491 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3493 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3494 IWineD3DSwapChain_Present((IWineD3DSwapChain *)dstSwapchain,
3495 NULL, NULL, dstSwapchain->win_handle, NULL, 0);
3497 dstSwapchain->presentParms.SwapEffect = orig_swap;
3504 TRACE("Unsupported blit between buffers on the same swapchain\n");
3505 return WINED3DERR_INVALIDCALL;
3506 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3507 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3508 return WINED3DERR_INVALIDCALL;
3509 } else if(dstSwapchain && srcSwapchain) {
3510 FIXME("Implement hardware blit between two different swapchains\n");
3511 return WINED3DERR_INVALIDCALL;
3513 else if (dstSwapchain)
3515 /* Handled with regular texture -> swapchain blit */
3516 if (src_surface == device->render_targets[0])
3517 TRACE("Blit from active render target to a swapchain\n");
3519 else if (srcSwapchain && dst_surface == device->render_targets[0])
3521 FIXME("Implement blit from a swapchain to the active render target\n");
3522 return WINED3DERR_INVALIDCALL;
3525 if ((srcSwapchain || src_surface == device->render_targets[0]) && !dstSwapchain)
3527 /* Blit from render target to texture */
3530 /* P8 read back is not implemented */
3531 if (src_surface->resource.format_desc->format == WINED3DFMT_P8_UINT
3532 || dst_surface->resource.format_desc->format == WINED3DFMT_P8_UINT)
3534 TRACE("P8 read back not supported by frame buffer to texture blit\n");
3535 return WINED3DERR_INVALIDCALL;
3538 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3539 TRACE("Color keying not supported by frame buffer to texture blit\n");
3540 return WINED3DERR_INVALIDCALL;
3541 /* Destination color key is checked above */
3544 if(dst_rect.right - dst_rect.left != src_rect.right - src_rect.left) {
3550 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3551 * flip the image nor scale it.
3553 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3554 * -> If the app wants a image width an unscaled width, copy it line per line
3555 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3556 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3557 * back buffer. This is slower than reading line per line, thus not used for flipping
3558 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3561 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3562 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3565 if (fbo_blit_supported(gl_info, BLIT_OP_BLIT,
3566 &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format_desc,
3567 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format_desc))
3569 surface_blt_fbo(device, Filter,
3570 src_surface, SFLAG_INDRAWABLE, &src_rect,
3571 dst_surface, SFLAG_INDRAWABLE, &dst_rect);
3572 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3574 else if (!stretchx || dst_rect.right - dst_rect.left > src_surface->currentDesc.Width
3575 || dst_rect.bottom - dst_rect.top > src_surface->currentDesc.Height)
3577 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3578 fb_copy_to_texture_direct(dst_surface, src_surface, &src_rect, &dst_rect, Filter);
3580 TRACE("Using hardware stretching to flip / stretch the texture\n");
3581 fb_copy_to_texture_hwstretch(dst_surface, src_surface, &src_rect, &dst_rect, Filter);
3584 if (!(dst_surface->Flags & SFLAG_DONOTFREE))
3586 HeapFree(GetProcessHeap(), 0, dst_surface->resource.heapMemory);
3587 dst_surface->resource.allocatedMemory = NULL;
3588 dst_surface->resource.heapMemory = NULL;
3592 dst_surface->Flags &= ~SFLAG_INSYSMEM;
3597 else if (src_surface)
3599 /* Blit from offscreen surface to render target */
3600 DWORD oldCKeyFlags = src_surface->CKeyFlags;
3601 WINEDDCOLORKEY oldBltCKey = src_surface->SrcBltCKey;
3602 struct wined3d_context *context;
3604 TRACE("Blt from surface %p to rendertarget %p\n", src_surface, dst_surface);
3606 if (!(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3607 && fbo_blit_supported(gl_info, BLIT_OP_BLIT,
3608 &src_rect, src_surface->resource.usage, src_surface->resource.pool,
3609 src_surface->resource.format_desc,
3610 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3611 dst_surface->resource.format_desc))
3613 TRACE("Using surface_blt_fbo.\n");
3614 /* The source is always a texture, but never the currently active render target, and the texture
3615 * contents are never upside down. */
3616 surface_blt_fbo(device, Filter,
3617 src_surface, SFLAG_INDRAWABLE, &src_rect,
3618 dst_surface, SFLAG_INDRAWABLE, &dst_rect);
3619 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3623 if (!(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3624 && arbfp_blit.blit_supported(gl_info, BLIT_OP_BLIT,
3625 &src_rect, src_surface->resource.usage, src_surface->resource.pool,
3626 src_surface->resource.format_desc,
3627 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3628 dst_surface->resource.format_desc))
3630 return arbfp_blit_surface(device, src_surface, &src_rect, dst_surface, &dst_rect, BLIT_OP_BLIT, Filter);
3633 /* Color keying: Check if we have to do a color keyed blt,
3634 * and if not check if a color key is activated.
3636 * Just modify the color keying parameters in the surface and restore them afterwards
3637 * The surface keeps track of the color key last used to load the opengl surface.
3638 * PreLoad will catch the change to the flags and color key and reload if necessary.
3640 if(Flags & WINEDDBLT_KEYSRC) {
3641 /* Use color key from surface */
3642 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3643 /* Use color key from DDBltFx */
3644 src_surface->CKeyFlags |= WINEDDSD_CKSRCBLT;
3645 src_surface->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3647 /* Do not use color key */
3648 src_surface->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3651 /* Now load the surface */
3652 surface_internal_preload(src_surface, SRGB_RGB);
3654 /* Activate the destination context, set it up for blitting */
3655 context = context_acquire(device, dst_surface);
3656 context_apply_blit_state(context, device);
3658 if (dstSwapchain && dst_surface == dstSwapchain->front_buffer)
3659 surface_translate_frontbuffer_coords(dst_surface, context->win_handle, &dst_rect);
3661 if (!device->blitter->blit_supported(gl_info, BLIT_OP_BLIT,
3662 &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format_desc,
3663 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format_desc))
3665 FIXME("Unsupported blit operation falling back to software\n");
3666 return WINED3DERR_INVALIDCALL;
3669 device->blitter->set_shader((IWineD3DDevice *)device, src_surface);
3673 /* This is for color keying */
3674 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3675 glEnable(GL_ALPHA_TEST);
3676 checkGLcall("glEnable(GL_ALPHA_TEST)");
3678 /* When the primary render target uses P8, the alpha component contains the palette index.
3679 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3680 * should be masked away have alpha set to 0. */
3681 if (primary_render_target_is_p8(device))
3682 glAlphaFunc(GL_NOTEQUAL, (float)src_surface->SrcBltCKey.dwColorSpaceLowValue / 256.0f);
3684 glAlphaFunc(GL_NOTEQUAL, 0.0f);
3685 checkGLcall("glAlphaFunc");
3687 glDisable(GL_ALPHA_TEST);
3688 checkGLcall("glDisable(GL_ALPHA_TEST)");
3691 /* Draw a textured quad
3693 draw_textured_quad(src_surface, &src_rect, &dst_rect, Filter);
3695 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3696 glDisable(GL_ALPHA_TEST);
3697 checkGLcall("glDisable(GL_ALPHA_TEST)");
3700 /* Restore the color key parameters */
3701 src_surface->CKeyFlags = oldCKeyFlags;
3702 src_surface->SrcBltCKey = oldBltCKey;
3706 /* Leave the opengl state valid for blitting */
3707 device->blitter->unset_shader((IWineD3DDevice *)device);
3709 if (wined3d_settings.strict_draw_ordering || (dstSwapchain
3710 && (dst_surface == dstSwapchain->front_buffer
3711 || dstSwapchain->num_contexts > 1)))
3712 wglFlush(); /* Flush to ensure ordering across contexts. */
3714 context_release(context);
3716 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3717 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3720 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3726 /* Source-Less Blit to render target */
3727 if (Flags & WINEDDBLT_COLORFILL)
3729 WINED3DCOLORVALUE color;
3731 TRACE("Colorfill\n");
3733 /* The color as given in the Blt function is in the surface format. */
3734 if (!surface_convert_color_to_float(dst_surface, DDBltFx->u5.dwFillColor, &color))
3735 return WINED3DERR_INVALIDCALL;
3737 if (ffp_blit.blit_supported(gl_info, BLIT_OP_COLOR_FILL,
3739 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3740 dst_surface->resource.format_desc))
3742 return ffp_blit.color_fill(device, dst_surface, &dst_rect, &color);
3744 else if (cpu_blit.blit_supported(gl_info, BLIT_OP_COLOR_FILL,
3746 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3747 dst_surface->resource.format_desc))
3749 return cpu_blit.color_fill(device, dst_surface, &dst_rect, &color);
3751 return WINED3DERR_INVALIDCALL;
3755 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3756 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3757 return WINED3DERR_INVALIDCALL;
3760 static HRESULT IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, const RECT *DestRect,
3761 IWineD3DSurface *src_surface, const RECT *src_rect, DWORD Flags, const WINEDDBLTFX *DDBltFx)
3763 IWineD3DDeviceImpl *device = This->resource.device;
3766 if (Flags & WINEDDBLT_DEPTHFILL) {
3767 switch(This->resource.format_desc->format)
3769 case WINED3DFMT_D16_UNORM:
3770 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3772 case WINED3DFMT_S1_UINT_D15_UNORM:
3773 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00007fff;
3775 case WINED3DFMT_D24_UNORM_S8_UINT:
3776 case WINED3DFMT_X8D24_UNORM:
3777 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3779 case WINED3DFMT_D32_UNORM:
3780 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3784 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format_desc->format));
3787 return IWineD3DDevice_Clear((IWineD3DDevice *)device, DestRect ? 1 : 0, (const WINED3DRECT *)DestRect,
3788 WINED3DCLEAR_ZBUFFER, 0x00000000, depth, 0x00000000);
3791 FIXME("(%p): Unsupp depthstencil blit\n", This);
3792 return WINED3DERR_INVALIDCALL;
3795 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, const RECT *DestRect,
3796 IWineD3DSurface *src_surface, const RECT *SrcRect, DWORD Flags,
3797 const WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter)
3799 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3800 IWineD3DSurfaceImpl *src = (IWineD3DSurfaceImpl *)src_surface;
3801 IWineD3DDeviceImpl *device = This->resource.device;
3803 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
3804 iface, wine_dbgstr_rect(DestRect), src_surface, wine_dbgstr_rect(SrcRect),
3805 Flags, DDBltFx, debug_d3dtexturefiltertype(Filter));
3806 TRACE("Usage is %s.\n", debug_d3dusage(This->resource.usage));
3808 if ((This->Flags & SFLAG_LOCKED) || (src && (src->Flags & SFLAG_LOCKED)))
3810 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3811 return WINEDDERR_SURFACEBUSY;
3814 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3815 * except depth blits, which seem to work
3817 if (This == device->depth_stencil || (src && src == device->depth_stencil))
3819 if (device->inScene && !(Flags & WINEDDBLT_DEPTHFILL))
3821 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3822 return WINED3DERR_INVALIDCALL;
3824 else if (SUCCEEDED(IWineD3DSurfaceImpl_BltZ(This, DestRect, src_surface, SrcRect, Flags, DDBltFx)))
3826 TRACE("Z Blit override handled the blit\n");
3831 /* Special cases for RenderTargets */
3832 if ((This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3833 || (src && (src->resource.usage & WINED3DUSAGE_RENDERTARGET)))
3835 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This, DestRect, src, SrcRect, Flags, DDBltFx, Filter)))
3839 /* For the rest call the X11 surface implementation.
3840 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3841 * other Blts are rather rare. */
3842 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, src_surface, SrcRect, Flags, DDBltFx, Filter);
3845 static HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty,
3846 IWineD3DSurface *src_surface, const RECT *rsrc, DWORD trans)
3848 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3849 IWineD3DSurfaceImpl *src = (IWineD3DSurfaceImpl *)src_surface;
3850 IWineD3DDeviceImpl *device = This->resource.device;
3852 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3853 iface, dstx, dsty, src_surface, wine_dbgstr_rect(rsrc), trans);
3855 if ((This->Flags & SFLAG_LOCKED) || (src->Flags & SFLAG_LOCKED))
3857 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3858 return WINEDDERR_SURFACEBUSY;
3861 if (device->inScene && (This == device->depth_stencil || src == device->depth_stencil))
3863 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3864 return WINED3DERR_INVALIDCALL;
3867 /* Special cases for RenderTargets */
3868 if ((This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3869 || (src->resource.usage & WINED3DUSAGE_RENDERTARGET))
3872 RECT SrcRect, DstRect;
3875 surface_get_rect(src, rsrc, &SrcRect);
3877 DstRect.left = dstx;
3879 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3880 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3882 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3883 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3884 Flags |= WINEDDBLT_KEYSRC;
3885 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3886 Flags |= WINEDDBLT_KEYDEST;
3887 if(trans & WINEDDBLTFAST_WAIT)
3888 Flags |= WINEDDBLT_WAIT;
3889 if(trans & WINEDDBLTFAST_DONOTWAIT)
3890 Flags |= WINEDDBLT_DONOTWAIT;
3892 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This,
3893 &DstRect, src, &SrcRect, Flags, NULL, WINED3DTEXF_POINT)))
3897 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, src_surface, rsrc, trans);
3900 static HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface)
3902 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3904 IWineD3DPaletteImpl *pal = This->palette;
3906 TRACE("(%p)\n", This);
3908 if (!pal) return WINED3D_OK;
3910 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT
3911 || This->resource.format_desc->format == WINED3DFMT_P8_UINT_A8_UNORM)
3913 if (This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3915 /* Make sure the texture is up to date. This call doesn't do
3916 * anything if the texture is already up to date. */
3917 surface_load_location(This, SFLAG_INTEXTURE, NULL);
3919 /* We want to force a palette refresh, so mark the drawable as not being up to date */
3920 surface_modify_location(This, SFLAG_INDRAWABLE, FALSE);
3924 if (!(This->Flags & SFLAG_INSYSMEM))
3926 TRACE("Palette changed with surface that does not have an up to date system memory copy.\n");
3927 surface_load_location(This, SFLAG_INSYSMEM, NULL);
3929 TRACE("Dirtifying surface\n");
3930 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
3934 if(This->Flags & SFLAG_DIBSECTION) {
3935 TRACE("(%p): Updating the hdc's palette\n", This);
3936 for (n=0; n<256; n++) {
3937 col[n].rgbRed = pal->palents[n].peRed;
3938 col[n].rgbGreen = pal->palents[n].peGreen;
3939 col[n].rgbBlue = pal->palents[n].peBlue;
3940 col[n].rgbReserved = 0;
3942 SetDIBColorTable(This->hDC, 0, 256, col);
3945 /* Propagate the changes to the drawable when we have a palette. */
3946 if (This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3947 surface_load_location(This, SFLAG_INDRAWABLE, NULL);
3952 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3953 /** Check against the maximum texture sizes supported by the video card **/
3954 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3955 const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
3956 unsigned int pow2Width, pow2Height;
3958 This->texture_name = 0;
3959 This->texture_target = GL_TEXTURE_2D;
3961 /* Non-power2 support */
3962 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
3964 pow2Width = This->currentDesc.Width;
3965 pow2Height = This->currentDesc.Height;
3969 /* Find the nearest pow2 match */
3970 pow2Width = pow2Height = 1;
3971 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3972 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3974 This->pow2Width = pow2Width;
3975 This->pow2Height = pow2Height;
3977 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
3978 /** TODO: add support for non power two compressed textures **/
3979 if (This->resource.format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED)
3981 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3982 This, This->currentDesc.Width, This->currentDesc.Height);
3983 return WINED3DERR_NOTAVAILABLE;
3987 if(pow2Width != This->currentDesc.Width ||
3988 pow2Height != This->currentDesc.Height) {
3989 This->Flags |= SFLAG_NONPOW2;
3992 TRACE("%p\n", This);
3993 if ((This->pow2Width > gl_info->limits.texture_size || This->pow2Height > gl_info->limits.texture_size)
3994 && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
3996 /* one of three options
3997 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)
3998 2: Set the texture to the maximum size (bad idea)
3999 3: WARN and return WINED3DERR_NOTAVAILABLE;
4000 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.
4002 if(This->resource.pool == WINED3DPOOL_DEFAULT || This->resource.pool == WINED3DPOOL_MANAGED)
4004 WARN("(%p) Unable to allocate a surface which exceeds the maximum OpenGL texture size\n", This);
4005 return WINED3DERR_NOTAVAILABLE;
4008 /* We should never use this surface in combination with OpenGL! */
4009 TRACE("(%p) Creating an oversized surface: %ux%u\n", This, This->pow2Width, This->pow2Height);
4013 /* Don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
4014 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
4015 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
4017 if (This->Flags & SFLAG_NONPOW2 && gl_info->supported[ARB_TEXTURE_RECTANGLE]
4018 && !(This->resource.format_desc->format == WINED3DFMT_P8_UINT
4019 && gl_info->supported[EXT_PALETTED_TEXTURE]
4020 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
4022 This->texture_target = GL_TEXTURE_RECTANGLE_ARB;
4023 This->pow2Width = This->currentDesc.Width;
4024 This->pow2Height = This->currentDesc.Height;
4025 This->Flags &= ~(SFLAG_NONPOW2 | SFLAG_NORMCOORD);
4029 switch (wined3d_settings.offscreen_rendering_mode)
4032 This->get_drawable_size = get_drawable_size_fbo;
4035 case ORM_BACKBUFFER:
4036 This->get_drawable_size = get_drawable_size_backbuffer;
4040 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
4041 return WINED3DERR_INVALIDCALL;
4044 This->Flags |= SFLAG_INSYSMEM;
4049 /* GL locking is done by the caller */
4050 static void surface_depth_blt(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
4051 GLuint texture, GLsizei w, GLsizei h, GLenum target)
4053 IWineD3DDeviceImpl *device = This->resource.device;
4054 GLint compare_mode = GL_NONE;
4055 struct blt_info info;
4056 GLint old_binding = 0;
4058 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
4060 glDisable(GL_CULL_FACE);
4061 glDisable(GL_BLEND);
4062 glDisable(GL_ALPHA_TEST);
4063 glDisable(GL_SCISSOR_TEST);
4064 glDisable(GL_STENCIL_TEST);
4065 glEnable(GL_DEPTH_TEST);
4066 glDepthFunc(GL_ALWAYS);
4067 glDepthMask(GL_TRUE);
4068 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
4069 glViewport(0, 0, w, h);
4071 surface_get_blt_info(target, NULL, w, h, &info);
4072 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
4073 glGetIntegerv(info.binding, &old_binding);
4074 glBindTexture(info.bind_target, texture);
4075 if (gl_info->supported[ARB_SHADOW])
4077 glGetTexParameteriv(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, &compare_mode);
4078 if (compare_mode != GL_NONE) glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
4081 device->shader_backend->shader_select_depth_blt((IWineD3DDevice *)device,
4082 info.tex_type, &This->ds_current_size);
4084 glBegin(GL_TRIANGLE_STRIP);
4085 glTexCoord3fv(info.coords[0]);
4086 glVertex2f(-1.0f, -1.0f);
4087 glTexCoord3fv(info.coords[1]);
4088 glVertex2f(1.0f, -1.0f);
4089 glTexCoord3fv(info.coords[2]);
4090 glVertex2f(-1.0f, 1.0f);
4091 glTexCoord3fv(info.coords[3]);
4092 glVertex2f(1.0f, 1.0f);
4095 if (compare_mode != GL_NONE) glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, compare_mode);
4096 glBindTexture(info.bind_target, old_binding);
4100 device->shader_backend->shader_deselect_depth_blt((IWineD3DDevice *)device);
4103 void surface_modify_ds_location(IWineD3DSurfaceImpl *surface,
4104 DWORD location, UINT w, UINT h)
4106 TRACE("surface %p, new location %#x, w %u, h %u.\n", surface, location, w, h);
4108 if (location & ~SFLAG_DS_LOCATIONS)
4109 FIXME("Invalid location (%#x) specified.\n", location);
4111 surface->ds_current_size.cx = w;
4112 surface->ds_current_size.cy = h;
4113 surface->Flags &= ~SFLAG_DS_LOCATIONS;
4114 surface->Flags |= location;
4117 /* Context activation is done by the caller. */
4118 void surface_load_ds_location(IWineD3DSurfaceImpl *surface, struct wined3d_context *context, DWORD location)
4120 IWineD3DDeviceImpl *device = surface->resource.device;
4121 const struct wined3d_gl_info *gl_info = context->gl_info;
4123 TRACE("surface %p, new location %#x.\n", surface, location);
4125 /* TODO: Make this work for modes other than FBO */
4126 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
4128 if (!(surface->Flags & location))
4130 surface->ds_current_size.cx = 0;
4131 surface->ds_current_size.cy = 0;
4134 if (surface->ds_current_size.cx == surface->currentDesc.Width
4135 && surface->ds_current_size.cy == surface->currentDesc.Height)
4137 TRACE("Location (%#x) is already up to date.\n", location);
4141 if (surface->current_renderbuffer)
4143 FIXME("Not supported with fixed up depth stencil.\n");
4147 if (!(surface->Flags & SFLAG_LOCATIONS))
4149 FIXME("No up to date depth stencil location.\n");
4150 surface->Flags |= location;
4154 if (location == SFLAG_DS_OFFSCREEN)
4156 GLint old_binding = 0;
4159 TRACE("Copying onscreen depth buffer to depth texture.\n");
4163 if (!device->depth_blt_texture)
4165 glGenTextures(1, &device->depth_blt_texture);
4168 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
4169 * directly on the FBO texture. That's because we need to flip. */
4170 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4171 if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
4173 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
4174 bind_target = GL_TEXTURE_RECTANGLE_ARB;
4178 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
4179 bind_target = GL_TEXTURE_2D;
4181 glBindTexture(bind_target, device->depth_blt_texture);
4182 glCopyTexImage2D(bind_target, surface->texture_level, surface->resource.format_desc->glInternal,
4183 0, 0, surface->currentDesc.Width, surface->currentDesc.Height, 0);
4184 glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4185 glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4186 glTexParameteri(bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
4187 glTexParameteri(bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
4188 glTexParameteri(bind_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
4189 glTexParameteri(bind_target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
4190 glBindTexture(bind_target, old_binding);
4192 /* Setup the destination */
4193 if (!device->depth_blt_rb)
4195 gl_info->fbo_ops.glGenRenderbuffers(1, &device->depth_blt_rb);
4196 checkGLcall("glGenRenderbuffersEXT");
4198 if (device->depth_blt_rb_w != surface->currentDesc.Width
4199 || device->depth_blt_rb_h != surface->currentDesc.Height)
4201 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, device->depth_blt_rb);
4202 checkGLcall("glBindRenderbufferEXT");
4203 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8,
4204 surface->currentDesc.Width, surface->currentDesc.Height);
4205 checkGLcall("glRenderbufferStorageEXT");
4206 device->depth_blt_rb_w = surface->currentDesc.Width;
4207 device->depth_blt_rb_h = surface->currentDesc.Height;
4210 context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
4211 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
4212 GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, device->depth_blt_rb);
4213 checkGLcall("glFramebufferRenderbufferEXT");
4214 context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, surface, FALSE);
4216 /* Do the actual blit */
4217 surface_depth_blt(surface, gl_info, device->depth_blt_texture,
4218 surface->currentDesc.Width, surface->currentDesc.Height, bind_target);
4219 checkGLcall("depth_blt");
4221 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4222 else context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4226 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4228 else if (location == SFLAG_DS_ONSCREEN)
4230 TRACE("Copying depth texture to onscreen depth buffer.\n");
4234 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4235 surface_depth_blt(surface, gl_info, surface->texture_name,
4236 surface->currentDesc.Width, surface->currentDesc.Height, surface->texture_target);
4237 checkGLcall("depth_blt");
4239 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4243 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4247 ERR("Invalid location (%#x) specified.\n", location);
4250 surface->Flags |= location;
4251 surface->ds_current_size.cx = surface->currentDesc.Width;
4252 surface->ds_current_size.cy = surface->currentDesc.Height;
4255 void surface_modify_location(IWineD3DSurfaceImpl *surface, DWORD flag, BOOL persistent)
4257 IWineD3DSurfaceImpl *overlay;
4259 TRACE("surface %p, location %s, persistent %#x.\n",
4260 surface, debug_surflocation(flag), persistent);
4262 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4264 if (surface_is_offscreen(surface))
4266 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4267 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4271 TRACE("Surface %p is an onscreen surface.\n", surface);
4277 if (((surface->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE))
4278 || ((surface->Flags & SFLAG_INSRGBTEX) && !(flag & SFLAG_INSRGBTEX)))
4280 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
4282 TRACE("Passing to container.\n");
4283 IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture *)surface->container.u.texture, TRUE);
4286 surface->Flags &= ~SFLAG_LOCATIONS;
4287 surface->Flags |= flag;
4289 /* Redraw emulated overlays, if any */
4290 if (flag & SFLAG_INDRAWABLE && !list_empty(&surface->overlays))
4292 LIST_FOR_EACH_ENTRY(overlay, &surface->overlays, IWineD3DSurfaceImpl, overlay_entry)
4294 IWineD3DSurface_DrawOverlay((IWineD3DSurface *)overlay);
4300 if ((surface->Flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) && (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)))
4302 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
4304 TRACE("Passing to container\n");
4305 IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture *)surface->container.u.texture, TRUE);
4308 surface->Flags &= ~flag;
4311 if (!(surface->Flags & SFLAG_LOCATIONS))
4313 ERR("Surface %p does not have any up to date location.\n", surface);
4317 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in)
4319 IWineD3DDeviceImpl *device = This->resource.device;
4320 IWineD3DSwapChainImpl *swapchain;
4321 struct wined3d_context *context;
4322 RECT src_rect, dst_rect;
4324 surface_get_rect(This, rect_in, &src_rect);
4326 context = context_acquire(device, This);
4327 context_apply_blit_state(context, device);
4328 if (context->render_offscreen)
4330 dst_rect.left = src_rect.left;
4331 dst_rect.right = src_rect.right;
4332 dst_rect.top = src_rect.bottom;
4333 dst_rect.bottom = src_rect.top;
4337 dst_rect = src_rect;
4340 swapchain = This->container.type == WINED3D_CONTAINER_SWAPCHAIN ? This->container.u.swapchain : NULL;
4341 if (swapchain && This == swapchain->front_buffer)
4342 surface_translate_frontbuffer_coords(This, context->win_handle, &dst_rect);
4344 device->blitter->set_shader((IWineD3DDevice *) device, This);
4347 draw_textured_quad(This, &src_rect, &dst_rect, WINED3DTEXF_POINT);
4350 device->blitter->unset_shader((IWineD3DDevice *) device);
4352 if (wined3d_settings.strict_draw_ordering || (swapchain
4353 && (This == swapchain->front_buffer || swapchain->num_contexts > 1)))
4354 wglFlush(); /* Flush to ensure ordering across contexts. */
4356 context_release(context);
4359 HRESULT surface_load_location(IWineD3DSurfaceImpl *surface, DWORD flag, const RECT *rect)
4361 IWineD3DDeviceImpl *device = surface->resource.device;
4362 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4363 BOOL drawable_read_ok = surface_is_offscreen(surface);
4364 struct wined3d_format_desc desc;
4365 CONVERT_TYPES convert;
4366 int width, pitch, outpitch;
4368 BOOL in_fbo = FALSE;
4370 TRACE("surface %p, location %s, rect %s.\n", surface, debug_surflocation(flag), wine_dbgstr_rect(rect));
4372 if (surface->resource.usage & WINED3DUSAGE_DEPTHSTENCIL)
4374 if (flag == SFLAG_INTEXTURE)
4376 struct wined3d_context *context = context_acquire(device, NULL);
4377 surface_load_ds_location(surface, context, SFLAG_DS_OFFSCREEN);
4378 context_release(context);
4383 FIXME("Unimplemented location %#x for depth/stencil buffers.\n", flag);
4384 return WINED3DERR_INVALIDCALL;
4388 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4390 if (surface_is_offscreen(surface))
4392 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4393 * Prefer SFLAG_INTEXTURE. */
4394 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4395 drawable_read_ok = FALSE;
4400 TRACE("Surface %p is an onscreen surface.\n", surface);
4404 if (surface->Flags & flag)
4406 TRACE("Location already up to date\n");
4410 if (!(surface->Flags & SFLAG_LOCATIONS))
4412 ERR("Surface %p does not have any up to date location.\n", surface);
4413 surface->Flags |= SFLAG_LOST;
4414 return WINED3DERR_DEVICELOST;
4417 if (flag == SFLAG_INSYSMEM)
4419 surface_prepare_system_memory(surface);
4421 /* Download the surface to system memory */
4422 if (surface->Flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX))
4424 struct wined3d_context *context = NULL;
4426 if (!device->isInDraw) context = context_acquire(device, NULL);
4428 surface_bind_and_dirtify(surface, !(surface->Flags & SFLAG_INTEXTURE));
4429 surface_download_data(surface, gl_info);
4431 if (context) context_release(context);
4435 /* Note: It might be faster to download into a texture first. */
4436 read_from_framebuffer(surface, rect, surface->resource.allocatedMemory,
4437 IWineD3DSurface_GetPitch((IWineD3DSurface *)surface));
4440 else if (flag == SFLAG_INDRAWABLE)
4442 if (surface->Flags & SFLAG_INTEXTURE)
4444 surface_blt_to_drawable(surface, rect);
4449 if ((surface->Flags & SFLAG_LOCATIONS) == SFLAG_INSRGBTEX)
4451 /* This needs a shader to convert the srgb data sampled from the GL texture into RGB
4452 * values, otherwise we get incorrect values in the target. For now go the slow way
4453 * via a system memory copy
4455 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4458 d3dfmt_get_conv(surface, FALSE /* We need color keying */,
4459 FALSE /* We won't use textures */, &desc, &convert);
4461 /* The width is in 'length' not in bytes */
4462 width = surface->currentDesc.Width;
4463 pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *)surface);
4465 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4466 * but it isn't set (yet) in all cases it is getting called. */
4467 if ((convert != NO_CONVERSION) && (surface->Flags & SFLAG_PBO))
4469 struct wined3d_context *context = NULL;
4471 TRACE("Removing the pbo attached to surface %p.\n", surface);
4473 if (!device->isInDraw) context = context_acquire(device, NULL);
4474 surface_remove_pbo(surface, gl_info);
4475 if (context) context_release(context);
4478 if ((convert != NO_CONVERSION) && surface->resource.allocatedMemory)
4480 int height = surface->currentDesc.Height;
4481 byte_count = desc.conv_byte_count;
4483 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4484 outpitch = width * byte_count;
4485 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4487 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4489 ERR("Out of memory %d, %d!\n", outpitch, height);
4490 return WINED3DERR_OUTOFVIDEOMEMORY;
4492 d3dfmt_convert_surface(surface->resource.allocatedMemory, mem, pitch,
4493 width, height, outpitch, convert, surface);
4495 surface->Flags |= SFLAG_CONVERTED;
4499 surface->Flags &= ~SFLAG_CONVERTED;
4500 mem = surface->resource.allocatedMemory;
4501 byte_count = desc.byte_count;
4504 flush_to_framebuffer_drawpixels(surface, desc.glFormat, desc.glType, byte_count, mem);
4506 /* Don't delete PBO memory */
4507 if ((mem != surface->resource.allocatedMemory) && !(surface->Flags & SFLAG_PBO))
4508 HeapFree(GetProcessHeap(), 0, mem);
4511 else /* if(flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) */
4513 const DWORD attach_flags = WINED3DFMT_FLAG_FBO_ATTACHABLE | WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB;
4515 if (drawable_read_ok && (surface->Flags & SFLAG_INDRAWABLE))
4517 read_from_framebuffer_texture(surface, flag == SFLAG_INSRGBTEX);
4519 else if (surface->Flags & (SFLAG_INSRGBTEX | SFLAG_INTEXTURE)
4520 && (surface->resource.format_desc->Flags & attach_flags) == attach_flags
4521 && fbo_blit_supported(gl_info, BLIT_OP_BLIT,
4522 NULL, surface->resource.usage, surface->resource.pool, surface->resource.format_desc,
4523 NULL, surface->resource.usage, surface->resource.pool, surface->resource.format_desc))
4525 DWORD src_location = flag == SFLAG_INSRGBTEX ? SFLAG_INTEXTURE : SFLAG_INSRGBTEX;
4526 RECT rect = {0, 0, surface->currentDesc.Width, surface->currentDesc.Height};
4528 surface_blt_fbo(surface->resource.device, WINED3DTEXF_POINT,
4529 surface, src_location, &rect, surface, flag, &rect);
4533 /* Upload from system memory */
4534 BOOL srgb = flag == SFLAG_INSRGBTEX;
4535 struct wined3d_context *context = NULL;
4537 d3dfmt_get_conv(surface, TRUE /* We need color keying */,
4538 TRUE /* We will use textures */, &desc, &convert);
4542 if ((surface->Flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)) == SFLAG_INTEXTURE)
4544 /* Performance warning... */
4545 FIXME("Downloading RGB surface %p to reload it as sRGB.\n", surface);
4546 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4551 if ((surface->Flags & (SFLAG_INSRGBTEX | SFLAG_INSYSMEM)) == SFLAG_INSRGBTEX)
4553 /* Performance warning... */
4554 FIXME("Downloading sRGB surface %p to reload it as RGB.\n", surface);
4555 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4558 if (!(surface->Flags & SFLAG_INSYSMEM))
4560 WARN("Trying to load a texture from sysmem, but SFLAG_INSYSMEM is not set.\n");
4561 /* Lets hope we get it from somewhere... */
4562 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4565 if (!device->isInDraw) context = context_acquire(device, NULL);
4567 surface_prepare_texture(surface, gl_info, srgb);
4568 surface_bind_and_dirtify(surface, srgb);
4570 if (surface->CKeyFlags & WINEDDSD_CKSRCBLT)
4572 surface->Flags |= SFLAG_GLCKEY;
4573 surface->glCKey = surface->SrcBltCKey;
4575 else surface->Flags &= ~SFLAG_GLCKEY;
4577 /* The width is in 'length' not in bytes */
4578 width = surface->currentDesc.Width;
4579 pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *)surface);
4581 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4582 * but it isn't set (yet) in all cases it is getting called. */
4583 if ((convert != NO_CONVERSION || desc.convert) && (surface->Flags & SFLAG_PBO))
4585 TRACE("Removing the pbo attached to surface %p.\n", surface);
4586 surface_remove_pbo(surface, gl_info);
4591 /* This code is entered for texture formats which need a fixup. */
4592 int height = surface->currentDesc.Height;
4594 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4595 outpitch = width * desc.conv_byte_count;
4596 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4598 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4600 ERR("Out of memory %d, %d!\n", outpitch, height);
4601 if (context) context_release(context);
4602 return WINED3DERR_OUTOFVIDEOMEMORY;
4604 desc.convert(surface->resource.allocatedMemory, mem, pitch, width, height);
4606 else if (convert != NO_CONVERSION && surface->resource.allocatedMemory)
4608 /* This code is only entered for color keying fixups */
4609 int height = surface->currentDesc.Height;
4611 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4612 outpitch = width * desc.conv_byte_count;
4613 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4615 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4617 ERR("Out of memory %d, %d!\n", outpitch, height);
4618 if (context) context_release(context);
4619 return WINED3DERR_OUTOFVIDEOMEMORY;
4621 d3dfmt_convert_surface(surface->resource.allocatedMemory, mem, pitch,
4622 width, height, outpitch, convert, surface);
4626 mem = surface->resource.allocatedMemory;
4629 /* Make sure the correct pitch is used */
4631 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4634 if (mem || (surface->Flags & SFLAG_PBO))
4635 surface_upload_data(surface, gl_info, &desc, srgb, mem);
4637 /* Restore the default pitch */
4639 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4642 if (context) context_release(context);
4644 /* Don't delete PBO memory */
4645 if ((mem != surface->resource.allocatedMemory) && !(surface->Flags & SFLAG_PBO))
4646 HeapFree(GetProcessHeap(), 0, mem);
4650 if (!rect) surface->Flags |= flag;
4652 if (in_fbo && (surface->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)))
4654 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4655 surface->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4661 static WINED3DSURFTYPE WINAPI IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface *iface) {
4662 return SURFACE_OPENGL;
4665 static HRESULT WINAPI IWineD3DSurfaceImpl_DrawOverlay(IWineD3DSurface *iface) {
4666 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4669 /* If there's no destination surface there is nothing to do */
4670 if(!This->overlay_dest) return WINED3D_OK;
4672 /* Blt calls ModifyLocation on the dest surface, which in turn calls DrawOverlay to
4673 * update the overlay. Prevent an endless recursion
4675 if(This->overlay_dest->Flags & SFLAG_INOVERLAYDRAW) {
4678 This->overlay_dest->Flags |= SFLAG_INOVERLAYDRAW;
4679 hr = IWineD3DSurfaceImpl_Blt((IWineD3DSurface *) This->overlay_dest, &This->overlay_destrect,
4680 iface, &This->overlay_srcrect, WINEDDBLT_WAIT,
4681 NULL, WINED3DTEXF_LINEAR);
4682 This->overlay_dest->Flags &= ~SFLAG_INOVERLAYDRAW;
4687 BOOL surface_is_offscreen(IWineD3DSurfaceImpl *surface)
4689 IWineD3DSwapChainImpl *swapchain = surface->container.u.swapchain;
4691 /* Not on a swapchain - must be offscreen */
4692 if (surface->container.type != WINED3D_CONTAINER_SWAPCHAIN) return TRUE;
4694 /* The front buffer is always onscreen */
4695 if (surface == swapchain->front_buffer) return FALSE;
4697 /* If the swapchain is rendered to an FBO, the backbuffer is
4698 * offscreen, otherwise onscreen */
4699 return swapchain->render_to_fbo;
4702 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4705 IWineD3DBaseSurfaceImpl_QueryInterface,
4706 IWineD3DBaseSurfaceImpl_AddRef,
4707 IWineD3DSurfaceImpl_Release,
4708 /* IWineD3DResource */
4709 IWineD3DBaseSurfaceImpl_GetParent,
4710 IWineD3DBaseSurfaceImpl_SetPrivateData,
4711 IWineD3DBaseSurfaceImpl_GetPrivateData,
4712 IWineD3DBaseSurfaceImpl_FreePrivateData,
4713 IWineD3DBaseSurfaceImpl_SetPriority,
4714 IWineD3DBaseSurfaceImpl_GetPriority,
4715 IWineD3DSurfaceImpl_PreLoad,
4716 IWineD3DSurfaceImpl_UnLoad,
4717 IWineD3DBaseSurfaceImpl_GetType,
4718 /* IWineD3DSurface */
4719 IWineD3DBaseSurfaceImpl_GetContainer,
4720 IWineD3DBaseSurfaceImpl_GetDesc,
4721 IWineD3DSurfaceImpl_LockRect,
4722 IWineD3DSurfaceImpl_UnlockRect,
4723 IWineD3DSurfaceImpl_GetDC,
4724 IWineD3DSurfaceImpl_ReleaseDC,
4725 IWineD3DSurfaceImpl_Flip,
4726 IWineD3DSurfaceImpl_Blt,
4727 IWineD3DBaseSurfaceImpl_GetBltStatus,
4728 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4729 IWineD3DBaseSurfaceImpl_IsLost,
4730 IWineD3DBaseSurfaceImpl_Restore,
4731 IWineD3DSurfaceImpl_BltFast,
4732 IWineD3DBaseSurfaceImpl_GetPalette,
4733 IWineD3DBaseSurfaceImpl_SetPalette,
4734 IWineD3DSurfaceImpl_RealizePalette,
4735 IWineD3DBaseSurfaceImpl_SetColorKey,
4736 IWineD3DBaseSurfaceImpl_GetPitch,
4737 IWineD3DSurfaceImpl_SetMem,
4738 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4739 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4740 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4741 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4742 IWineD3DBaseSurfaceImpl_SetClipper,
4743 IWineD3DBaseSurfaceImpl_GetClipper,
4745 IWineD3DSurfaceImpl_LoadTexture,
4746 IWineD3DSurfaceImpl_BindTexture,
4747 IWineD3DBaseSurfaceImpl_GetData,
4748 IWineD3DSurfaceImpl_SetFormat,
4749 IWineD3DSurfaceImpl_PrivateSetup,
4750 IWineD3DSurfaceImpl_GetImplType,
4751 IWineD3DSurfaceImpl_DrawOverlay
4754 static HRESULT ffp_blit_alloc(IWineD3DDevice *iface) { return WINED3D_OK; }
4755 /* Context activation is done by the caller. */
4756 static void ffp_blit_free(IWineD3DDevice *iface) { }
4758 /* This function is used in case of 8bit paletted textures using GL_EXT_paletted_texture */
4759 /* Context activation is done by the caller. */
4760 static void ffp_blit_p8_upload_palette(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info)
4763 BOOL colorkey_active = (surface->CKeyFlags & WINEDDSD_CKSRCBLT) ? TRUE : FALSE;
4765 d3dfmt_p8_init_palette(surface, table, colorkey_active);
4767 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
4769 GL_EXTCALL(glColorTableEXT(surface->texture_target, GL_RGBA, 256, GL_RGBA, GL_UNSIGNED_BYTE, table));
4773 /* Context activation is done by the caller. */
4774 static HRESULT ffp_blit_set(IWineD3DDevice *iface, IWineD3DSurfaceImpl *surface)
4776 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
4777 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4778 enum complex_fixup fixup = get_complex_fixup(surface->resource.format_desc->color_fixup);
4780 /* When EXT_PALETTED_TEXTURE is around, palette conversion is done by the GPU
4781 * else the surface is converted in software at upload time in LoadLocation.
4783 if(fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4784 ffp_blit_p8_upload_palette(surface, gl_info);
4787 glEnable(surface->texture_target);
4788 checkGLcall("glEnable(surface->texture_target)");
4793 /* Context activation is done by the caller. */
4794 static void ffp_blit_unset(IWineD3DDevice *iface)
4796 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
4797 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4800 glDisable(GL_TEXTURE_2D);
4801 checkGLcall("glDisable(GL_TEXTURE_2D)");
4802 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
4804 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
4805 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4807 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4809 glDisable(GL_TEXTURE_RECTANGLE_ARB);
4810 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
4815 static BOOL ffp_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4816 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
4817 const struct wined3d_format_desc *src_format_desc,
4818 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
4819 const struct wined3d_format_desc *dst_format_desc)
4821 enum complex_fixup src_fixup;
4823 if (blit_op == BLIT_OP_COLOR_FILL)
4825 if (!(dst_usage & WINED3DUSAGE_RENDERTARGET))
4827 TRACE("Color fill not supported\n");
4834 src_fixup = get_complex_fixup(src_format_desc->color_fixup);
4835 if (TRACE_ON(d3d_surface) && TRACE_ON(d3d))
4837 TRACE("Checking support for fixup:\n");
4838 dump_color_fixup_desc(src_format_desc->color_fixup);
4841 if (blit_op != BLIT_OP_BLIT)
4843 TRACE("Unsupported blit_op=%d\n", blit_op);
4847 if (!is_identity_fixup(dst_format_desc->color_fixup))
4849 TRACE("Destination fixups are not supported\n");
4853 if (src_fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4855 TRACE("P8 fixup supported\n");
4859 /* We only support identity conversions. */
4860 if (is_identity_fixup(src_format_desc->color_fixup))
4866 TRACE("[FAILED]\n");
4870 static HRESULT ffp_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface,
4871 const RECT *dst_rect, const WINED3DCOLORVALUE *color)
4873 const RECT draw_rect = {0, 0, dst_surface->currentDesc.Width, dst_surface->currentDesc.Height};
4875 return device_clear_render_targets(device, 1 /* rt_count */, &dst_surface, 1 /* rect_count */,
4876 dst_rect, &draw_rect, WINED3DCLEAR_TARGET, color, 0.0f /* depth */, 0 /* stencil */);
4879 const struct blit_shader ffp_blit = {
4888 static HRESULT cpu_blit_alloc(IWineD3DDevice *iface)
4893 /* Context activation is done by the caller. */
4894 static void cpu_blit_free(IWineD3DDevice *iface)
4898 /* Context activation is done by the caller. */
4899 static HRESULT cpu_blit_set(IWineD3DDevice *iface, IWineD3DSurfaceImpl *surface)
4904 /* Context activation is done by the caller. */
4905 static void cpu_blit_unset(IWineD3DDevice *iface)
4909 static BOOL cpu_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4910 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
4911 const struct wined3d_format_desc *src_format_desc,
4912 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
4913 const struct wined3d_format_desc *dst_format_desc)
4915 if (blit_op == BLIT_OP_COLOR_FILL)
4923 static HRESULT cpu_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface,
4924 const RECT *dst_rect, const WINED3DCOLORVALUE *color)
4928 memset(&BltFx, 0, sizeof(BltFx));
4929 BltFx.dwSize = sizeof(BltFx);
4930 BltFx.u5.dwFillColor = wined3d_format_convert_from_float(dst_surface->resource.format_desc, color);
4931 return IWineD3DBaseSurfaceImpl_Blt((IWineD3DSurface*)dst_surface, dst_rect,
4932 NULL, NULL, WINEDDBLT_COLORFILL, &BltFx, WINED3DTEXF_POINT);
4935 const struct blit_shader cpu_blit = {
4944 static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4945 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
4946 const struct wined3d_format_desc *src_format_desc,
4947 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
4948 const struct wined3d_format_desc *dst_format_desc)
4950 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
4953 /* We only support blitting. Things like color keying / color fill should
4954 * be handled by other blitters.
4956 if (blit_op != BLIT_OP_BLIT)
4959 /* Source and/or destination need to be on the GL side */
4960 if (src_pool == WINED3DPOOL_SYSTEMMEM || dst_pool == WINED3DPOOL_SYSTEMMEM)
4963 if(!((src_format_desc->Flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (src_usage & WINED3DUSAGE_RENDERTARGET))
4964 && ((dst_format_desc->Flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (dst_usage & WINED3DUSAGE_RENDERTARGET)))
4967 if (!is_identity_fixup(src_format_desc->color_fixup) ||
4968 !is_identity_fixup(dst_format_desc->color_fixup))
4971 if (!(src_format_desc->format == dst_format_desc->format
4972 || (is_identity_fixup(src_format_desc->color_fixup)
4973 && is_identity_fixup(dst_format_desc->color_fixup))))